user188276
user188276

Reputation:

Object code, linking time in C language

When compiling, C produces object code before linking time. I wonder if object code is in the form of binary yet? If so, what happened next in the linking time?

Upvotes: 0

Views: 3973

Answers (3)

Jacob
Jacob

Reputation: 34621

Wikipedia says,

In computer science, an object file is an organised collection of named objects, and typically these objects are sequences of computer instructions in a machine code format, which may be directly executed by a computer's CPU.

Object files are typically produced by a compiler as a result of processing a source code file. Object files contain compact code, and are often called "binaries".

A linker is typically used to generate an executable or library by amalgamating parts of object files together. Object files for embedded systems typically contain nothing but machine code but generally, object files also contain data for use by the code at runtime: relocation information, stack unwinding information, comments, program symbols (names of variables and functions) for linking and/or debugging purposes, and other debugging information.

Another great site has much more detailed info and a useful diagram, here: Diagram

Upvotes: 6

Pascal Cuoq
Pascal Cuoq

Reputation: 80355

Object files as produced by the C compiler essentially contain binary code with holes in each place where an address should go that is yet unknown (addresses of function from other files -- including libraries -- called, addresses of variables from other files that are accessed in this one, ...).

It also contains a table indexed by symbol names ("x" or "_x" for variable x, "f" or "_f" for function f). For each such symbol, there is a status code ("defined here", "not defined here but used", ...) and the addresses of holes in the binary code that need to be filed with each address when it becomes known.

If you are using Unix (or gcc on Windows), you can print the later table with the command "nm file.o".

Upvotes: 1

Employed Russian
Employed Russian

Reputation: 213935

Yes, object code is usually in binary form. Just try opening it in your favorite text editor.

You can learn what linkers do here or here.

Upvotes: 0

Related Questions