Hareesh Nagarajan Pramod Kumar Both from 7 th  Semester CSE  at RV College of Engineering A hands-on introduction to the ELF Object file format
Our experience with ELF In our 6 th  semester we developed miASMa - a 2 pass Macro Assembler for an x86 machine. miASMa generates Relocatable Object Files that conforming to the ELF Format.  To achieve the latter we developed a simple code generation library called libmiASMaELF.
Types of Object Files Relocatable File Executable File Shared Object File
The ELF Header
Sections in ELF
Special Sections Various  sections  in ELF are predefined and hold program and control Information.
The Quick look at the String table  This is how most string tables in the ELF look like.
The Symbol Table (I'm bored, I thought this was talk made use of a hands-on approach :( )
The RelocationTable (We are almost there !)
Now, Let us write a ( hello.o)  relocatable object file byte by byte! char text[] = {  '\xB8', '\x04', '\x00', '\x00', '\x00',  // mov eax, 4 '\xBB', '\x01', '\x00', '\x00', '\x00',  // mov ebx, 1 '\xB9', '\x00', '\x00', '\x00', '\x00',  // mov ecx, msg '\xBA', '\x0E', '\x00', '\x00', '\x00',  // mov edx, 14 '\xCD', '\x80',  // int 0x80 '\xB8', '\x01', '\x00', '\x00', '\x00',  // mov eax, 1 '\xCD', '\x80'  // int 0x80 }; char data[] = {  '\x48', '\x65', '\x6C', '\x6C', '\x6F',  '\x2C', '\x20', '\x57', '\x6F', '\x72',  // Hello, World! In Hexadecimal. '\x6C', '\x64', '\x21', '\x0A'  }; vector<char> vtext(&text[0], &text[29]); vector<char> vdata(&data[0], &data[14]); miasmaELF obj; sample.cpp
obj.InitializeELFHeader(); obj.InitializeSymbolTable(); bool AddNewSection(string _SectionName , Elf32_Word shtype, Elf32_Word shflags, Elf32_Word shaddr,  Elf32_Word shlink, Elf32_Word shinfo, Elf32_Word shaddralign, Elf32_Word shentsize); obj.AddNewSection(&quot;.shstrtab&quot;,SHT_STRTAB,  0,0,0,0,0,0);  obj.AddNewSection(&quot;.text&quot;,  SHT_PROGBITS,6,0,0,0,16,0);  obj.AddNewSection(&quot;.data&quot;,  SHT_PROGBITS,3,0,0,0,16,0);  obj.AddNewSection(&quot;.symtab&quot;,  SHT_SYMTAB,  0,0, obj.GetSectionIndexOfType(SHT_STRTAB, &quot;.strtab&quot;),  0,  4,sizeof(Elf32_Sym));  obj.AddNewSection(&quot;.rel.text&quot;,SHT_REL,0,0,   obj.GetSectionIndexOfType(SHT_SYMTAB), obj.GetSectionIndexOfType(SHT_PROGBITS, &quot;.text&quot;),   4,sizeof(Elf32_Rel));  Prototype
bool AddContents(const vector<char>,  int SectionIndex); obj.AddContents(vtext, obj.GetSectionIndexOfType(SHT_PROGBITS,&quot;.text&quot;)); obj.AddContents(vdata, obj.GetSectionIndexOfType(SHT_PROGBITS,&quot;.data&quot;)); bool AddSymbol(string _Symbol,  Elf32_Addr stvalue, Elf32_Word stsize, unsigned char stbind, unsigned char sttype, Elf32_Half st_shndx); obj.AddSymbol( &quot;_start&quot; ,0,0, STB_WEAK, STT_FUNC, obj.GetSectionIndexOfType(SHT_PROGBITS, &quot;.text&quot;)); obj.AddSymbol(&quot;myvariable&quot;,0,0, STB_GLOBAL, STT_OBJECT, obj.GetSectionIndexOfType(SHT_PROGBITS, &quot;.data&quot;)); And then… Prototype Prototype
bool AddRelocationEntry(Elf32_Addr roffset,  unsigned char rsym,  unsigned char rtype,  int SectionIndex); obj.AddRelocationEntry(11, obj.ReturnSymbolIndex(&quot;myvariable&quot;), R_386_RELATIVE, obj.GetSectionIndexOfType(SHT_REL, &quot;.rel.text&quot;)); obj.PrepareFile(); obj.WriteFile(&quot;hello.o&quot;);  //Yippie! The OBJECT FILE  is created Finally… Prototype
Memory map of hello.o  $ g++ sample.cpp libmiasmaelf.o  $./a.out  ---> hello.o written!
For more info on ELF A terrific article on ELF http://www.linuxjournal.com/article.php?sid=1059 The Specification http://x86.ddj.com/ftp/manuals/tools/elf.pdf For more info on ELF Pramod Kumar T  [email_address] .co.in Hareesh Nagarajan [email_address] http://puggy.symonds.net/~hareesh miASMa is hosted at: http://freshmeat.net/miasma To Contact Us Thank You! Hope it was fun!

A hands-on introduction to the ELF Object file format

  • 1.
    Hareesh Nagarajan PramodKumar Both from 7 th Semester CSE at RV College of Engineering A hands-on introduction to the ELF Object file format
  • 2.
    Our experience withELF In our 6 th semester we developed miASMa - a 2 pass Macro Assembler for an x86 machine. miASMa generates Relocatable Object Files that conforming to the ELF Format. To achieve the latter we developed a simple code generation library called libmiASMaELF.
  • 3.
    Types of ObjectFiles Relocatable File Executable File Shared Object File
  • 4.
  • 5.
  • 6.
    Special Sections Various sections in ELF are predefined and hold program and control Information.
  • 7.
    The Quick lookat the String table This is how most string tables in the ELF look like.
  • 8.
    The Symbol Table(I'm bored, I thought this was talk made use of a hands-on approach :( )
  • 9.
    The RelocationTable (Weare almost there !)
  • 10.
    Now, Let uswrite a ( hello.o) relocatable object file byte by byte! char text[] = { '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4 '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1 '\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14 '\xCD', '\x80', // int 0x80 '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 '\xCD', '\x80' // int 0x80 }; char data[] = { '\x48', '\x65', '\x6C', '\x6C', '\x6F', '\x2C', '\x20', '\x57', '\x6F', '\x72', // Hello, World! In Hexadecimal. '\x6C', '\x64', '\x21', '\x0A' }; vector<char> vtext(&text[0], &text[29]); vector<char> vdata(&data[0], &data[14]); miasmaELF obj; sample.cpp
  • 11.
    obj.InitializeELFHeader(); obj.InitializeSymbolTable(); boolAddNewSection(string _SectionName , Elf32_Word shtype, Elf32_Word shflags, Elf32_Word shaddr, Elf32_Word shlink, Elf32_Word shinfo, Elf32_Word shaddralign, Elf32_Word shentsize); obj.AddNewSection(&quot;.shstrtab&quot;,SHT_STRTAB, 0,0,0,0,0,0); obj.AddNewSection(&quot;.text&quot;, SHT_PROGBITS,6,0,0,0,16,0); obj.AddNewSection(&quot;.data&quot;, SHT_PROGBITS,3,0,0,0,16,0); obj.AddNewSection(&quot;.symtab&quot;, SHT_SYMTAB, 0,0, obj.GetSectionIndexOfType(SHT_STRTAB, &quot;.strtab&quot;), 0, 4,sizeof(Elf32_Sym)); obj.AddNewSection(&quot;.rel.text&quot;,SHT_REL,0,0, obj.GetSectionIndexOfType(SHT_SYMTAB), obj.GetSectionIndexOfType(SHT_PROGBITS, &quot;.text&quot;), 4,sizeof(Elf32_Rel)); Prototype
  • 12.
    bool AddContents(const vector<char>, int SectionIndex); obj.AddContents(vtext, obj.GetSectionIndexOfType(SHT_PROGBITS,&quot;.text&quot;)); obj.AddContents(vdata, obj.GetSectionIndexOfType(SHT_PROGBITS,&quot;.data&quot;)); bool AddSymbol(string _Symbol, Elf32_Addr stvalue, Elf32_Word stsize, unsigned char stbind, unsigned char sttype, Elf32_Half st_shndx); obj.AddSymbol( &quot;_start&quot; ,0,0, STB_WEAK, STT_FUNC, obj.GetSectionIndexOfType(SHT_PROGBITS, &quot;.text&quot;)); obj.AddSymbol(&quot;myvariable&quot;,0,0, STB_GLOBAL, STT_OBJECT, obj.GetSectionIndexOfType(SHT_PROGBITS, &quot;.data&quot;)); And then… Prototype Prototype
  • 13.
    bool AddRelocationEntry(Elf32_Addr roffset, unsigned char rsym, unsigned char rtype, int SectionIndex); obj.AddRelocationEntry(11, obj.ReturnSymbolIndex(&quot;myvariable&quot;), R_386_RELATIVE, obj.GetSectionIndexOfType(SHT_REL, &quot;.rel.text&quot;)); obj.PrepareFile(); obj.WriteFile(&quot;hello.o&quot;); //Yippie! The OBJECT FILE is created Finally… Prototype
  • 14.
    Memory map ofhello.o $ g++ sample.cpp libmiasmaelf.o $./a.out ---> hello.o written!
  • 15.
    For more infoon ELF A terrific article on ELF http://www.linuxjournal.com/article.php?sid=1059 The Specification http://x86.ddj.com/ftp/manuals/tools/elf.pdf For more info on ELF Pramod Kumar T [email_address] .co.in Hareesh Nagarajan [email_address] http://puggy.symonds.net/~hareesh miASMa is hosted at: http://freshmeat.net/miasma To Contact Us Thank You! Hope it was fun!