2
votes

background: flash at 0x02000000/2M, SDRAM at 0x10000/16M, processor: ks8695.

the Bootloader and OS are burned into the flash, when resetting, OS is copied to SDRAM at address 0x10000, then set PC(program counter) to 0x10000 (that is, run the OS).

since the PC is set to 0x10000(since the processor can execute the first instruction of OS at this point), why is it necessary to specify the absolute address of the Text Section of the OS(through setting -Ttext=0x10000) when linking it? (when I set -Ttext to 0x0, the OS won't run properly).

Best regards,

wenlujon

3
You should edit your question, and put your answers inside the question, so that it is easier to read.shodanex
your answer below is a good one, others can just refer to it.wenlujon

3 Answers

1
votes

I think you sort of answered your own question - the RAM in your system is located at 0x10000. The two mainstream ways of executing code are store-and-download (SnD) and eXecute-in-place (XIP). It seems like you're storing the code in flash and copying it to RAM. So all the addresses in your binary must be offset with the RAM start address, otherwise they will be wrong in the binary.

If your flash is NOR you could technically leave it in NOR and run the code in place (XIP) though it may not be suitable for your platform.

Does that help?

0
votes

Your PC is at 0x10000, so you have to link it at 0x10000, because your code is doing absolute addressing.

The bootloader is not doing any linking or symbol resolution, it is just copying some binary blob to 0x10000 and then setting the PC to 0x10000. So your code has to be prepared to run at 0x10000, that is why you need to specify this in the linker.

function call are usually done using PC relative addressing, but this is not necessary the case when you want to have access to data. Assume you have a table T. If you are linked at 0x0, and your table is at 0x1234. You may have some instruction that refer to this address.

Now you move your code to 0x10000. Your table address is now 0x11234, but your code does not now it has been moved, so it tries to load data at 0x1234, where there is nothing, or crap.

Now when you link your code with an offset, the set of instruction that was used to access T is modified accordingly. That is all what linking is for, resolving symbol into adrresses !

0
votes

yes, absolute addressing is a good reason, but what about the first instruction? It should not have anything to do with absolute addressing.

given the first instruction of the OS is 0xe1a00000, the instruction at offset 0x10000 of the OS is 0xe3a01303. if -Ttext is set to 0x0, by looking into the map file, we get 0xe1a00000 at address 0x0, 0xe3a01303 at address 0x10000 (CPU in the target board still does not know this at all!).

when bootloader copies the OS to the address 0x10000, it will copies 0xe1a00000 to 0x10000 and 0xe3a01303 to 0x20000(they are nothing but data), am I right? then set PC to 0x10000, CPU should execute 0xe1a00000 because the instruction occupies the address 0x10000, but CPU actually executes 0xe3a01303 which is at address 0x20000.

  1. since 0xe1a00000 is at 0x10000 of the SDRAM, when PC is set to 0x10000, why CPU does not excute 0xe1a00000?
  2. how does the CPU know 0xe3a01303 is assigned to 0x10000 since this work is done by the linker in my personal computer?