Page 1 of 3
elf relocation on arm
Posted: Fri May 31, 2013 8:24 am
by matiashr
hi.
i want to do relocation of a elf file with chibios on stm32f4. ive managed to get it working with
a statically linked app. but now i want dynamical loading of functions. like shared libraries.
(i want to have something similar to what vxworks does). however i dont think i understand
the dynsym section properly. can anyone tell me how the plt section is supposted to be filled by my loader?
om able to find functions inside the strings offset. so now the plt needs to be updated somehow with addresses.
in the assembly code of what i try to load i havw a blx instruction
from what i know of x86 this is suppost to call function@plt wich should lookup the sym in question and put the pointer in the plt... should note that what i want is not lazy binding but rather linking before execution.
but on target of course.
i could post the code if anyone is interested.
br
matias
Re: elf relocation on arm
Posted: Fri May 31, 2013 10:05 pm
by skute
To dynamically link an elf you would need to generate a lookup table and store it in flash somewhere which contains a name-value pair for every function-address you are linking against. You can use a tool like 'nm' to generate the information for you base image (nm -n yourBinary.elf) where yourBinary.elf is the image running on the device which will be doing the loading. Then you would create a file/header combo with this table and rebuild. Then your loader can search this table for the string and find the load address to be filled in.
Re: elf relocation on arm
Posted: Sat Jun 01, 2013 2:05 pm
by matiashr
skute, yes i know howto solve that part. but i dont understand how to/ where to put the address.
when i parse the elf and find let say as an example printf. where do i write the adress in the loaded elf?
as far as i know its suppost to be in the got section. at the offset given by dynsym. i tried that but
i just get a unhandled exception when i try to single step the location where i assume the branch is occuring
that location contains a blx instrucion. wich i assume is trying to call got+offset.is this correct?
/matias
Re: elf relocation on arm
Posted: Sun Jun 02, 2013 5:37 pm
by skute
Once you have the relocation type and relocation address, you perform the specific relocation on the relocation address itself. There is an example here:
http://infocenter.arm.com/help/index.js ... HDBCB.htmlIn the example they are using a relocation type of R_ARM_ABS32 (described here
http://infocenter.arm.com/help/topic/co ... _aaelf.pdf).
So, for this example, to perform the relocation of this type you basically do *(uint32_t)relocationAddr += st_value where st_value is part of the symbol table entry (see
http://www.skyfree.org/linux/references/ELF_Format.pdf).
I've never fully implemented an elf loader but it is something that I had played around with, so I apologize if my posts aren't as helpful as you'd like, or worse, incorrect/misleading.
Re: elf relocation on arm
Posted: Sun Jun 02, 2013 8:57 pm
by matiashr
Hi,Well any help is appriciated

Ive checked the example in the link, but it does not mention R_ARM_JUMP_SLOT...however here is a bit more details on what I dont quite grasp:Disassembly of section .rel.plt:0000023c <.rel.plt>: 23c:  00008348     andeq  r8, r0, r8, asr #6       <--------  This is the address I parse from the elf file, it's a jumpslot as seen by readelf command later, 240:  00000416     andeq  r0, r0, r6, lsl r4Disassembly of section .plt:00000244 <.plt>: 244:  e52de004     push   {lr}       ; (str lr, [sp, #-4]!) 248:  e59fe004     ldr   lr, [pc, #4]   ; 254 <main-0x1c> 24c:  e08fe00e     add   lr, pc, lr 250:  e5bef008     ldr   pc, [lr, #8]! 254:  000080e8     andeq  r8, r0, r8, ror #1 258:  e28fc600     add   ip, pc, #0                <--------  (A) whats this??, dont quite understand whats going on here, I thought this was going to be a jump instruction to the address given at .rel.plt+0x23c 25c:  e28cca08     add   ip, ip, #32768  ; 0x8000 260:  e5bcf0e8     ldr   pc, [ip, #232]! ; 0xe8Disassembly of section .text:00000270 <main>: 270:  f04f 0301    mov.w  r3, #1 274:  4618       mov   r0, r3 276:  4770       bx    lr 278:  f3af 8000    nop.w 27c:  f3af 8000    nop.w00000280 <_runmain>: 280:  b500       push   {lr} 282:  b083       sub   sp, #12 284:  9001       str   r0, [sp, #4] 286:  9100       str   r1, [sp, #0] 288:  f7ff efe6    blx   258 <main-0x18>  <-------- This is a function call to listTasks(), jumps to (A) above I suppose 28c:  f04f 0300    mov.w  r3, #0 290:  4618       mov   r0, r3readelf givesRelocation section '.rel.plt' at offset 0x23c contains 1 entries: Offset   Info   Type       Sym.Value  Sym. Name00008348  00000416 R_ARM_JUMP_SLOT  00000000  listTasks ^---- am i suppost to write address of "listTasks" af this offset ?, thats what I tried anyway, but with no success...//Matias
Re: elf relocation on arm
Posted: Mon Jun 03, 2013 10:37 am
by matiashr
for anyone interested the code is available at github.com/matiashr/elfloader
Re: elf relocation on arm
Posted: Mon Jun 03, 2013 12:20 pm
by Giovanni
Hi,
Consider using the forum "code" tags, the code would be more readable.
Giovanni
Re: elf relocation on arm
Posted: Sat Jun 08, 2013 7:07 pm
by matiashr
is there anone who knows why gcc produces large binary when i skip -pie flag?
my binary becomes 4 times larger (?!). im using nostdlib nostartfiles aswell.
it seems program headers move to offset 8000hex. any reason for this?
br matias
will use code tag from now on.
Re: elf relocation on arm
Posted: Tue Jun 11, 2013 11:09 am
by matiashr
i get exception invalid state on the blx instruction. from what i anderstand this is because of switching between thum and arm. is this possible on stm32f4.?
the manual says its thumb2 and does not mention arm mode?
anyways where is one suppost to set the use_thumb that is mentioned in the chibi makefile. doxygen docs suggest that many diffrent thumb modes are available. thuminterworking etc..
/matias
Re: elf relocation on arm
Posted: Tue Jun 11, 2013 12:29 pm
by Giovanni
Nope, Cortex-M processors only execute Thumb code so the LSb in the program counter must alwais be one.
Interworking, of course, is not needed.
Giovanni