0

I want to change some bits in memory by STR instruction.

    .text
.equ    ram_address,0x4000
.equ    pattern,0x55
.equ    counter,50  
    mov r0,#pattern
    mov r1,#counter
    mov r2,#ram_address

back:   str r0,[r2]
    add r2,#4
    subs r1,r1,#1
    bne back
here:   b here
    
        .data
i:  .word 0xffffffff

and using such a makefile:

TOOLCHAIN=arm-none-eabi
Assembler=${TOOLCHAIN}-as
Linker=${TOOLCHAIN}-ld
Objcpy=${TOOLCHAIN}-objcopy
Compile_Options= -g
Link_Options=-Ttext=0x0 -Tbss=0x4000 # -Tdata=0x4000 # 
.PHONY : clean
.PRECIOUS : %.bin %.elf %.o
all : create

create : flash.bin


flash.bin:main.bin  
    dd if=/dev/zero of=flash.bin bs=4096 count=4096 
    dd if=main.bin of=flash.bin bs=4096 conv=notrunc

%.bin:%.elf
    $(Objcpy) -O binary $< $@   

%.elf:%.o
    $(Linker) $(Link_Options) -o $@ $<

%.o:%.S
    $(Assembler) $(Compile_Options) $< -o $@
    
clean :
    rm -f *.o *.bin *.elf

And this is qemu command:

qemu-system-arm -S -M connex -pflash flash.bin -nographic -serial /dev/null

QEMU emulator version 6.1.0

I check memory by qemu-arm-system and gdbserver and x/16xw 0x4000 command. results is:

0xffffffff 0x00000000 0x00000000 0x00000000

It means .data section is readonly. how could I set it writable?

  • 1
    Where is your entrypoint? If it's `start`, then why is it in your data section, and why is there no branch from there to your loop? – Michael Dec 07 '21 at 06:59
  • @Michael I just remove data section(no need).It is same. – peyman khalili Dec 07 '21 at 07:07
  • What do you see when you single-step your code in a debugger? If `start:` is now after your code in the `.text` section, then it's still not going to execute. – Peter Cordes Dec 07 '21 at 08:36
  • Are you sure that your emulated machine actually has RAM at address 0x4000? Can you check using QEMU monitor? – Michael Dec 07 '21 at 10:13
  • You don't say what your QEMU command line is, or what QEMU version you're using. I agree with Michael that the probable cause is that 0x4000 is either nothing-at-all or ROM/flash. You need to set your linker map to match whatever (emulated) hardware you're running on. – Peter Maydell Dec 07 '21 at 10:20
  • @PeterMaydell I add piece of code(I will update code), and define something in .data section and now can see that contetnt by x/4xw 0x4000. I use this "qemu-system-arm -S -M connex -pflash flash.bin -nographic -serial /dev/null" version is 6.1.0 – peyman khalili Dec 07 '21 at 10:42

2 Answers2

1

This happens because the Connex machine does not have RAM at address zero, it has ROM (strictly speaking, it's a cfi01 flash device). So you can load your binary there, and you can execute from there, and read data, but trying to write data there will not work. This is the same as it would be on real hardware of this type. (You can also see that you're loading your binary into flash because you're using the '-pflash' option to QEMU to load it.)

The RAM on the Connex board starts at address 0xa0000000. You need to use a linker map which correctly puts at least the data and bss sections into RAM. You could put the entire binary including the code into RAM if you like: this is probably the simplest thing to get something working. Note that if you want the code in flash and the data in RAM then you'll need to do something more complicated than have a single binary blob loaded via -pflash. Options here include "load an ELF file via the generic-loader device" (which will then put the ELF file's different segments into the right places in the memory map even if they're not contiguous) or "have the blob that's loaded into flash be able to relocate (copy) its own data into RAM on startup".

You'll also need to make sure that your code's stack is in RAM. Accidentally setting the stack pointer to point into ROM can produce some odd failure modes where code seems to execute fine until something, usually a function-return, needs to read something back off the stack again...

As a side note, Connex is a bit of an odd board choice unless you specifically wanted to run old PXA255 code.

Peter Maydell
  • 9,707
  • 1
  • 19
  • 25
0

As mensioned this page: [http://www.bravegnu.org/gnu-eprog/using-ram.html][1]

The connex board has a 64 MB of RAM starting at address 0xA0000000, in which variables can be stored.

so I changed ram_address to 0xA0000000 and it worked, and by x/4xw 0xA0000000 I can see changes in RAM.