What does an object file contain?
Solution 1:
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.
Solution 2:
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.
Solution 3:
First read the wiki page. You can use objdump to examine such a file :)
Solution 4:
Use the file command for things like this. It's an ELF object file on a modern Linux system. E.g. if compiled for 32-bit x86.
ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
In contrast, a dynamically linked executable might look like:
ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
To see headers, including section names, you can use:
objdump -x any_name.o
To disassemble:
objdump -d any_name.o
Solution 5:
First, binary files can be opened! Don't be scared of it, you need just the right tools! Being binary data, a text editor is not the right tool of course; a right tool could be a hex editor, or an advanced editor like emacs, or a tool that instead of simply "outputting" bytes in their "hex" representation and letting you alone with your interpretation of the data, knows that particular format and "interprets" the data properly, at some level (e.g. GIMP interprets a PNG file as an image and shows it, a PNG analyser will "decompose" the data inside PNG sections showing telling you the flags in certain bytes, ...etc).
In your case, the general answer is that the object file contains your compiled code (and data), plus all extra informations needed by the linker, and eventually more.
How these informantions are "organized" and in some case in what the "eventually more" consists, it depends on the specific object format. Some wikipedia links listing some of the possibilities are this, this, this, this ...
Each of these may have its tools to analyse the content; e.g. readelf
for ELF, objdump
for several formats (try objdump -i
) depending on how it was compiled.