How to relcation .rodata with .text in Cortex-m3

Asked by qs10086

使用arm-none-eabi-gcc编写可以试用与cortex-m3的程序时,发现生成的可执行的ELF文件是静态链接进来的,没有.rel.plt与.got等表项。当编译成共享库文件(.so)时,跳转入.plt段使用的命令为blx,从而引起一个fault。

(1)目前cortex-m3能不能支持动态链接?如果支持的话编译选项该怎么写?

(2)如果无法使用动态链接的话,当我对ELF文件进行Load的时候如对其中的.rodata与.text段进行重定位?因为没有.rel段。
    我就写了一个简单的printf("Hello World!");
看到的机器码如下:
.text:000098E4 EXPORT fun_test
.text:000098E4 fun_test ; CODE XREF: main+4p
.text:000098E4 80 B5 PUSH {R7,LR}
.text:000098E6 00 AF ADD R7, SP, #0
.text:000098E8 02 48 LDR R0, =aHelloWorld ; "Hello World!"
.text:000098EA 00 F0 3D F8 BL puts
.text:000098EE 00 BF NOP
.text:000098F0 80 BD POP {R7,PC}
.text:000098F0 ; End of function fun_test
.text:000098F0
.text:000098F0 ; ---------------------------------------------------------------------------
.text:000098F2 00 BF ALIGN 4
.text:000098F4 C8 A4 00 00 off_98F4 DCD aHelloWorld ; DATA XREF: fun_test+4r

.rodata:0000A4C8 48 65 6C 6C 6F 20 57 6F+aHelloWorld DCB "Hello World!",0

在.text段的000098E8处的 LDR R0, =aHelloWorld ; "Hello World!"定位到000098F4,但是此时给定的直接是一个绝对地址,位于.rodata段的:0000A4C8地址处,当我在系统上加载此ELF文件时需要对此进行重定位。
请问大家有没有什么好的办法,多谢!

Question information

Language:
Chinese Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#1

Hi,

[Sorry, I am unable to answer you in Chinese.]

Dynamic linking requires an operating system and Cortex-M3 is typically used for bare-metal programming (ie no operating system).

As to your second question, as far as I know the boot up sequence is as follows:

1) device is reset
2) processor loads the address of the reset interrupt handler at address 0x4 and the base of the stack at address 0x0.
3) the processor execute the code of the interrupt handler at the address it just read

One thing to bear in mind is that the memory map for Cortex-M3 is as follows:

0x00000000 -> 0x1FFFFFFF flash
0x20000000 -> 0x3FFFFFFF RAM
0x40000000 -> 0x5FFFFFFF peripherals
0xE0000000 -> 0xFFFFFFFF system level addresses

The way it works is that you create a vector table (an array of pointers) in your code and put the base address of the stack and the address of the bootup code in the entries 0 and 1 of that table. The initialization of this table must be made statically as the table will be read before any code is executed. Then, use a linker script to put the vector table at the beginning of the .text section and the rest of the code after. You should also set the load address to be 0x0 and set the runtime address to a RAM address (for instance 0x20000000). Remember that the symbols in your code will resolve to the runtime address (ie if you do vector_table[1] = reset_handler it will use the runtime address of the reset_handler) but you need to write the load address of the reset handler because at first everything is in flash/ROM.

Then, when the device is turned on, the reset handler (your code whose address you put in the entry 1 of the vector table) is executed and it must copy everything (code and data) in RAM. Then you can call the main function and the program will execute. Note that typically all this is done by the boot code which you get from the manufacturer of the board.

You can find a bit more details at [1].

[1] http://markdingst.blogspot.co.uk/2012/06/make-own-bootloader-for-arm-cortex-m3.html

Revision history for this message
qs10086 (745763497-6) said :
#2

Hi Thomas,

Thanks for you solution!

I want to implement a Loader in the Cortex-m3 as one part of the operating system. So my Loader will boot up as you've said.

Now, I want my loader to loader other ELF file in the runtime. So I want to change the absolute address in the literal pool. I have do this after several weeks research. But there have still some problems.

Revision history for this message
qs10086 (745763497-6) said :
#3

Hi Thomas,

Thanks for you solution!

I want to implement a Loader in the Cortex-m3 as one part of the operating system. So my Loader will boot up as you've said.

Now, I want my loader to loader other ELF file in the runtime. So I want to change the absolute address in the literal pool. I have do this after several weeks research. But there have still some problems.

Can you help with this problem?

Provide an answer of your own, or ask qs10086 for more information if necessary.

To post a message you must log in.