Object Files & Linking
Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution
Object Sections ELF sections.text : Executable code read/executable.rodata : Read Only Data read.data : Data read/write.bss : Uninitialized Data read/write
Object at Runtime At runtime segments loaded into process address space
Object at Runtime Addresses need to be relocatable – Linker needs to move as it combines objects
Relocation Addresses stored as offset in section Address = section start + offset Mov instruction = Code start + Offset = 0xXXXXF xXXXX0080
Relocation Code sample: – Array in.data – getNum accesses – getBiggerNum calls others
Relocation Offsets
Relocation Do not have final addresses for branches & loads
Relocation Table Relocation Table stores addresses that need fixing at link time: Offset of item Address it needs
Relocation Linker fixes up
Static Libraries Static Libraries: – Collection of 1+ object files designed to bake into executable at compile time – Windows : mylibrary.lib – Linux mylibrary.a
Static Libraries Static Issues: – All library code included in every executable – Every running program has own copies of in memory – Need to recompile to use new library code
Static Libraries Dynamic Libraries: – Collection of 1+ object files designed to be linked at run time – Windows : mylibrary.dll (dynamically linked library) – Linux mylibrary.so (shared object)
Static Libraries Dynamic Wins: – Executable does not need lib code – Every running program can share one copy of library – May not need to recompile to use new library code
Static Libraries Dynamic Wins: – Every running program can share one copy of library Library code must not be modifiable in.text
Static Libraries Dynamic Wins: – May not need to recompile to use new library code As long as no features removed/reordered
Dynamic Sample Program to make use of dynamic library:
Dynamic Sample Program to make use of dynamic library: – Function calls now go to
Dynamic Sample Program to make use of dynamic library: – Function calls now go to
PLT Procedure Linkage Table Jump table in.data used to look up dynamic functions
PLT First call, Global Offset Table points back to code to dynamic loader
PLT Loader replaces GOT record with
Other Dynamic Tricks Can load library at run time:
Other Dynamic Tricks Can load library at run time:
Other Dynamic Tricks Can hijack dynamic library calls – Hacking – Debugging