Move and meaning of the symbol table

I have a main.c file that contains a call to the external function fun ()

int main()
{
 fun();
}

      

and the result of readelf -r looks like this

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 what is how an info field (which is a symbol table entry) maps to a fun symbol and why is sym.value 0000 ??

+3


source to share


1 answer


Keep in mind that the C standard does not actually specify how this works under the covers, the description that follows has a very general implementation method.


With one translation unit containing the code:

int main() { fun(); }

      

the information available from this compiled (unlinked) object file is basically:

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

      

This is because he knows where he main

is, but has no information about fun

- he will need to be found later. Therefore, reading an object file naturally returns an unknown value for fun

.



Of course, you need some code to define fun

, for example, in a different translation unit:

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

      

When compiled, this will yield the following information:

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

      

This is the linking stage that ties them together. It takes both object files (and object / library files for any other dependencies, such as the C runtime library containing puts

), and links them together, making adjustments to all code that uses undefined characters.

So what you get is an executable file format where all symbols are known and all links are allowed.

+1


source







All Articles