Reputation: 14418
I have the below sample code. I would like to see the relocation table entries from the object file. For this, i have used
objdump -r test.o
Sample Code:
#include <stdio.h>
char * myfunction ();
int x=20;
int main()
{
printf (" \n Inside main ");
char * p = myfunction ();
printf (" \n My string is %s ", p);
}
char * myfunction ()
{
char * key ="jaka";
return key;
}
Outputof Objdump -r test.o
test.o: file format elf64-x86-64
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
0000000000000009 R_X86_64_32 .rodata
0000000000000013 R_X86_64_PC32 printf-0x0000000000000004
000000000000001d R_X86_64_PC32 myfunction-0x0000000000000004
000000000000002d R_X86_64_32 .rodata+0x0000000000000010
0000000000000037 R_X86_64_PC32 printf-0x0000000000000004
0000000000000045 R_X86_64_32S .rodata+0x0000000000000024
RELOCATION RECORDS FOR [.eh_frame]:
OFFSET TYPE VALUE
0000000000000020 R_X86_64_PC32 .text
0000000000000040 R_X86_64_PC32 .text+0x000000000000003d
QUESTION:
According to my understanding the global variable 'x' should have been in the relocation table somewhere, which i m unable to locate.
Please help me if i overlooked something here ?
Upvotes: 1
Views: 1590
Reputation: 8209
There are shouldn't be any relocation entries related to x
because your code doesn't use it. Nevertheless it presents in symbol table, and you can check it with objdump -t
Saying in few words - relocation entry is the thing that helps your code to reference to some object, to link against them. So, if you don't reference x
in your code - there won't be any x
targeted relocations.
To check it - add, for example, x = 40;
in the main()
and you'll get the next:
test.o: file format elf64-x86-64
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
0000000000000009 R_X86_64_32 .rodata
0000000000000013 R_X86_64_PC32 printf-0x0000000000000004
000000000000001d R_X86_64_PC32 myfunction-0x0000000000000004
000000000000002d R_X86_64_32 .rodata+0x0000000000000010
0000000000000037 R_X86_64_PC32 printf-0x0000000000000004
000000000000003d R_X86_64_PC32 x-0x0000000000000008 # bingo!
000000000000004f R_X86_64_32S .rodata+0x0000000000000024
RELOCATION RECORDS FOR [.eh_frame]:
OFFSET TYPE VALUE
0000000000000020 R_X86_64_PC32 .text
0000000000000040 R_X86_64_PC32 .text+0x0000000000000047
Upvotes: 2