ELF [DRAFT]
Created at 2017-06-25T11:00:28+09:00

It is much more non-trivial than I thought...

TODO

  • object (.o)
  • shared library (.so)
  • executable
  • symbol visibility (C, C++)
  • (static) linker (ld, gold, lld)
  • dynamic linker (ld.so)
  • x86_64 way
  • debug symbol (DWARF)
  • non-freestanding code
  • mmap .so text sharing
  • kernel exec implementation

Example

Input files:

[exe.c]
int f(int x);

int main() {
  return f(2);
}

[lib.c]
int f(int x) {
  return x + x;
}

Build commands:

$ clang -c exe.c -o exe.o
$ clang -c lib.c -o lib.o
$ clang -fPIC -shared lib.o -o lib.so
$ clang lib.o exe.o -o exe_static
$ clang exe.o lib.so -o exe_dynamic

Output files:

  • exe.c => exe.o
  • lib.c => lib.o
  • lib.o => lib.so
  • lib.o + exe.o => exe_static
  • exe.o => exe_dynamic

Reference

  • http://wiki.osdev.org/System_V_ABI¨B¨K7K¨B
  • llvm/include/llvm/BinaryFormat/ELF.h (ELF format struct)
  • llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def (relocation types e.g. R_X86_64_PC32)
  • (kernel exec implementation)
  • (glibc ld.so implementation)