Comment 3 for bug 1523315

Revision history for this message
Andre Vieira (andre-simoesdiasvieira) wrote :

Hi Justin,

So using the following code:

$ cat t.c
char * function (void)
{
      char * local_var[] = { "a", "b", "c" };
      return *local_var;
}

and using your compile command and to generate a t.o. look at the generated dumps by
'arm-none-eabi-readelf -Wa t.o > dump.a' and 'arm-none-eabi-objdump -D t.o > dump.D'.

From dump.D you can see:
00000000 <function>:
...
   6: 4b07 ldr r3, [pc, #28] ; (24 <function+0x24>)
   8: f859 3003 ldr.w r3, [r9, r3]
...
  24: 00000000 andeq r0, r0, r0

And from dump.a
Relocation section '.rel.text.function' at offset 0xa3c contains 1 entries:
 Offset Info Type Sym. Value Symbol's Name
00000024 0000081a R_ARM_GOT_BREL 00000000 .LC4

From the ELF for ARM Architecture doc in
http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044f/IHI0044F_aaelf.pdf

You can read that R_ARM_GOT_BREL indicates the following relocation operation: GOT(S) + A – GOT_ORG, Where A is addend (0 here) and S is .LC4 so GOT (LC4) - GOT_ORG

Now if you go back to dump.D you will also find:

Disassembly of section .data:

00000000 <.LC4>:
   0: 00000000 andeq r0, r0, r0
   4: 00000004 andeq r0, r0, r4
   8: 00000008 andeq r0, r0, r8

And from dump.a:

Relocation section '.rel.data' at offset 0xa24 contains 3 entries:
 Offset Info Type Sym. Value Symbol's Name
00000000 00000502 R_ARM_ABS32 00000000 .rodata
00000004 00000502 R_ARM_ABS32 00000000 .rodata
00000008 00000502 R_ARM_ABS32 00000000 .rodata

So .LC4 I guess is pointing at rodata, which contains "a", "b" and "c' as you may inspect.

And I believe this is how it is all linked.

The function loads the address of our "a", "b", "c" array by...

Loading the index of the address of the array into the GOT table with:
   6: 4b07 ldr r3, [pc, #28] ; (24 <function+0x24>)

since function+0x24 will hold 'GOT(LC4) - GOT_ORG'. And then it adds that index to r9 and loads the value there:
   8: f859 3003 ldr.w r3, [r9, r3]

r9 is the default pic register, so it should be holding the address of the GOT, i.e. GOT_ORG, so that instruction should be loading the address of the relocated .rodata. Since .rel.data points to .rodata, which is the address of our array.

I'm hoping this answers your question, or at least helps you a little further.

Cheers,
Andre