Object files are generated by compilers and assemblers when they operate on source files written in a High Level Language like C/C++. They are in binary format and thus cannot be opened directly using a normal text editor. An object file contains five different kinds of information.

Header information Shows overall information of the file like ABI version, type, size of the code, name of the source file
Object Code Binary instructions generated by the assembler
Relocation Adjust program addresses to account for non-zero segment origins and resolve references to external symbols
Symbols Global symbols that need to imported from other modules and the one's defined in current module
Debugging Info Include other information for debug purposes like file/line number, local symbols, etc

In this session, let us create a simple C program, convert it into object file and analyze the output.

  
  
// File : helloworld.c

#include 

int main () {
   printf ("Hello World !");
}
  

You can compile the above C code using gcc.

$> gcc -m32 -c helloworld.c
[/pre]

  • -m32 option generates code for 32-bit environment, where int and long are set to 32-bits.
  • -c option compiles and assembles the source files, but does not link them.

Now, you'll see an output by the name helloworld.o. If you try to open it using any text editor, you may get a warning saying the file is in binary format. You can use readelf command to view the binary information in an object file. Let's print out the header first.

$> readelf -aW helloworld.o 
[/pre]

In the output, you'll see the different categories of information we talked about earlier.

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          208 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         11
  Section header string table index: 8

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000000 000034 000026 00  AX  0   0  4
  [ 2] .rel.text         REL             00000000 000344 000010 08      9   1  4
  [ 3] .data             PROGBITS        00000000 00005c 000000 00  WA  0   0  4
  [ 4] .bss              NOBITS          00000000 00005c 000000 00  WA  0   0  4
  [ 5] .rodata           PROGBITS        00000000 00005c 00000e 00   A  0   0  1
  [ 6] .comment          PROGBITS        00000000 00006a 000012 00      0   0  1
  [ 7] .note.GNU-stack   PROGBITS        00000000 00007c 000000 00      0   0  1
  [ 8] .shstrtab         STRTAB          00000000 00007c 000051 00      0   0  1
  [ 9] .symtab           SYMTAB          00000000 000288 0000a0 10     10   8  4
  [10] .strtab           STRTAB          00000000 000328 00001a 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

There are no section groups in this file.

There are no program headers in this file.

Relocation section '.rel.text' at offset 0x344 contains 2 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name
00000014  00000501 R_386_32               00000000   .rodata
00000019  00000902 R_386_PC32             00000000   printf

There are no unwind sections in this file.

Symbol table '.symtab' contains 10 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS helloworld.c
     2: 00000000     0 SECTION LOCAL  DEFAULT    1
     3: 00000000     0 SECTION LOCAL  DEFAULT    3
     4: 00000000     0 SECTION LOCAL  DEFAULT    4
     5: 00000000     0 SECTION LOCAL  DEFAULT    5
     6: 00000000     0 SECTION LOCAL  DEFAULT    7
     7: 00000000     0 SECTION LOCAL  DEFAULT    6
     8: 00000000    38 FUNC    GLOBAL DEFAULT    1 main
     9: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND printf

No version information found in this file.
[/pre]