Akshay
Akshay

Reputation: 191

relocation and symbol table value

I have main.c file which contains call to external function fun()

int main()
{
 fun();
}

and result of readelf -r is as follows

Relocation section '.rela.text' at offset 0x298 contains 3 entries:
Offset          Info           Type           Sym. Value    Sym. Name +Addend
00000000000a  000b00000002 R_X86_64_PC32     0000000000000000 fun - 4

I just want to know that how info field(which is symbol table entry) is mapped with symbol fun and why sym.value is 0000??

Upvotes: 4

Views: 1025

Answers (1)

paxdiablo
paxdiablo

Reputation: 881093

Keep in mind that the C standard doesn't actually specify how this works under the covers, the description that follows is of a very common implementation method.


With a single translation unit holding the code:

int main() { fun(); }

the information available from that compiled (not yet linked) object file is basically:

symbol  status   value
------  ------   -----
main    defined  pointer to main within object
fun     needed   zero

That's because it knows where main is but has no information on fun - it will need to be found later. So reading the object file will naturally return an unknown value for fun.

Of course, you will need some code to define fun as well, such as in another translation unit:

void fun(void) { puts("Hello, world."); }

Compiling this would result in the following information:

symbol  status   value
------  ------   -----
fun     defined  pointer to fun within object
puts    needed   zero

It's the link stage that ties these together. It takes both object files (and the object/library files for the any other dependencies, such as the C run-time library containing puts) and binds them together, making adjustments to all code that uses undefined symbols.

So what you end up with an executable file format where all symbols are known and all references are resolved.

Upvotes: 2

Related Questions