A hands-on introduction to the ELF Object file format

4,192 views

Published on

In our 6th semester we developed miASMa - a 2 pass Macro Assembler for an x86 machine. miASMa generates Relocatable Object Files that conforming to the ELF Format.

Published in: Technology, Art & Photos
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,192
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
61
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

A hands-on introduction to the ELF Object file format

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

×