What does an object file contain?

后端 未结 8 951
长情又很酷
长情又很酷 2020-12-13 06:14

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 sin

8条回答
  •  自闭症患者
    2020-12-13 06:32

    There is 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.

提交回复
热议问题