c++ - What exactly is in a .o / .a / .so file? -


i wondering stored in .o or .so file results compiling c++ program. this post gives quite overview of compilation process , function of .o file in it, , far understand this post, .a , .so files multiple .o files merged single file linked in static (.a) or dynamic (.so) way.

but wanted check if understand correctly stored in such file. after compiling following code

void f(); void f2(int);  const int x = 25;  void g() {   f();   f2(x); }  void h() {   g(); } 

i expect find following items in .o file:

  • machine code g(), containing placeholder addresses f() , f2(int) called.
  • machine code h(), no placeholders
  • machine code x, number 25
  • some kind of table specifies @ addresses in file symbols g(), h() , x can found
  • another table specifies placeholders used refer undefined symbols f() , f2(int), have resolved during linking.

then program nm list symbol names both tables.

i suppose compiler optimize call f2(x) calling f2(25) instead, still need keep symbol x in .o file since there no way know if used different .o file.

would correct? same .a , .so files?

thanks help!

you're pretty correct in general idea object files. in "table specifies @ addresses in file" replace "addresses" "offsets", that's wording.

.a files archives (an old format predates tar, same thing). replace .a files tar files long taught linker unpack them , link .o files contained in them (more or less, there's little bit more logic not link object files in archive aren't necessary, that's optimization).

.so files different. closer final binary object file. .so file symbols resolved can @ least theoretically run program. in fact, pie (position independent executables) difference between shared library , program (at least in theory) few bits in header. contain instructions dynamic linker how load library (more or less same instructions normal program) , relocation table contains instructions telling dynamic linker how resolve external symbols (again, same in program). unresolved symbols in dynamic library (and program) accessed through indirection tables populated @ dynamic linking time (program start or dlopen).

if simplify lot, difference between objects , shared libraries more work has been done in shared library not text relocation (this not strictly necessary , enforced, it's general idea). means in object files assembler has generated placeholders addresses linker fills in, shared library addresses filled in addresses jump tables text of library doesn't need changed, limited jump table.

btw. i'm talking elf. older formats had more differences between programs , libraries.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -