Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an object file contain?

Tags:

c++

c

compilation

During the various stages of compilation in C or C++, I know that an object file gets generated (i.e., any_name.o file). What does this .o file contain? I can't open it since it's a binary file.

Could anybody please help me? Are the contents of the object file mainly dependent on the compiler which we use on Unix?

like image 786
Vijay Avatar asked Jun 15 '10 13:06

Vijay


People also ask

What does object file contains C++?

The file contains binary data which must be run through a linker to generate an executable. It is essentially a bunch of machine code instructions with named sections (corresponding to your functions).

What is object file and what are symbols?

Object files define and reference symbols, where each symbol corresponds to a function, a global variable, or a static variable (i.e., any C variable declared with the static attribute). The purpose of symbol resolution is to associate each symbol reference with exactly one symbol definition.

Are object files text files?

The OBJ file format is a text file format, which means you can edit OBJ files in a text editor if you are hard-core.

Why is it called an object file?

(Computers) A computer program which has been translated into machine language by a compiler and assembler, but not yet linked into an executable program; sometimes called an obj file, because its file name typically has the extension "obj" . Webster's Revised Unabridged Dictionary, published 1913 by G.


2 Answers

Object files can contain a bunch of stuff: Basically it's some or all of the list below:

  • Symbol Names
  • Compiled code
  • Constant data, eg. strings
  • Imports - which symbols the compiled code references (gets fixed up by linker)
  • Exports - which symbols the object file makes available to OTHER object files.

The linker turns a bunch of object files into an executable, by matching up all the imports and exports, and modifying the compiled code so the correct functions get called.

like image 73
Roddy Avatar answered Sep 28 '22 00:09

Roddy


There are several standardized formats (COFF, ELF on Unix), basically they are variants of the same formats that those used for executables but missing some informations. These missing informations will be completed when linking.

Objects files formats basically contains the same informations:

  • binary code resulting of compilation (for a target processor)
  • static data used by that part of the program (like constant strings, etc). You can make a finer distinction between BSS (exported data) and Text (data that won't be modified by the program). But that is mostly important for compiler and linker. Note that like binary code, data are also dependant on target (big-endian, little-endian, 32bits, 64bits).
  • tables of symbols exported by this part of the program (mostly functions entry points)
  • tables of external symbols used by this part of the program

When objects will be linked together the parts of the code that refers to external symbols will be replaced by actual values (well, that is still oversimplified, there is a last part that will be done at loading time when running the program, but that's the idea).

The object file may also contain more symbols information that strictly necessary for resolving imports and export (useful for debug). That information can be removed using the strip command.

like image 44
kriss Avatar answered Sep 28 '22 00:09

kriss