Definition
A linker is the program that combines separately compiled pieces of code into one program. Its inputs are object files, each the machine code that a compiler produced from one source file, and libraries, which are collections of object files. Its output is normally one executable file. The linker decides which input pieces become part of the output. It connects every name that one piece uses to the piece that defines that name. It gives every piece its final memory address and corrects the instructions that depend on those addresses.
The idea
A compiler, the tool that translates source code into machine code, works on one source file at a time. When main.c calls a function named checksum that is defined in another file, the compiler cannot know where that function will be in memory. It leaves a gap in the call instruction and writes two notes into the object file: this file needs something named checksum, and these bytes must be corrected once the address is known. The linker is the tool that reads all the notes and fills all the gaps.
The picture is a book put together from chapters by different authors. Each author writes “see page ??” where the text refers to another chapter. The editor puts the chapters in order, numbers the pages and replaces every “??”. The picture leaves out that the linker also chooses the chapters: it omits library chapters that nobody refers to, and when two libraries offer a chapter with the same title, it takes the first one it finds and never compares their contents.
How it works
The linker works with these inputs and outputs.
Object file: sections (blocks of code or data), a symbol table §12.8 (the names that the file defines and the names that it needs, each called a symbol), and relocation records §12.50 (the places to correct).
Static library, also called an archive: one file that bundles many object files with an index of the symbols they define. Its name conventionally ends in
.a.Linker script §12.5: the rules for the memory layout.
Executable file: the output.
Map file: an optional report of what was linked and where it was placed.
The steps below are those of the GNU linker ld. GNU, a name whose letters stand for GNU’s Not Unix, is the free-software project that publishes it.
Read the inputs in the order of the command line. Every object file named there is included.
Keep two lists: the symbols defined so far, and the symbols that are needed and not yet defined.
On reaching a library, take out those of its object files, called members, that define a symbol on the needed list. A new member can need further symbols, so repeat until the library offers nothing more. Then move on. The library is not looked at again later.
At the end, report an error for every symbol that is still needed and not defined, and for every symbol that two included pieces both define. Symbols marked as weak are an exception, which the precise rules below state.
Lay out the output: put sections of the same kind together and give each one an address, following the linker script.
Perform each relocation: compute the final address of the symbol and write it into the recorded place.
Write the executable file and, if asked, the map file.
Step 3 has a consequence. When two libraries that follow the file needing a function both define that function, the library that comes first on the command line supplies it. The member of the other library is not taken out for that name, and the linker prints no warning. If that member is taken out for a different name that it also defines, two definitions meet and the link stops with a multiple-definition error.
Worked example
main.c calls checksum on the three bytes 1, 2 and 3 and returns the result as the exit status §13.24 of the program, the number that a program hands back to whatever started it. Two libraries define a function with that name. In libsum.a the function adds the bytes. In libxor.a it combines them with exclusive or, the bit operation written . The program is linked four ways with version 2.42 of ld, which is started through the compiler command gcc. The option -lsum asks for the library libsum.a, and -L. tells the linker to look for libraries in the current directory.
| arguments of the link | result | |
|---|---|---|
| 1 | main.o -L. -lsum -lxor |
exit status 6 |
| 2 | main.o -L. -lxor -lsum |
exit status 0 |
| 3 | -L. -lsum main.o |
error: undefined reference to checksum |
| 4 | main.o sum.o xor.o |
error: multiple definition of checksum |
Row 1: , so the adding version was linked. Row 2: exclusive or gives a 1 bit wherever the two inputs differ. In binary, is , which is 3, and is , which is 0. The exclusive-or version was linked. The source file and both libraries are identical in the two rows. Only the order differs, and neither link prints a warning. Row 3: the library is searched before main.o has put checksum on the needed list, so nothing is taken from it. Row 4: object files named directly are always included, so two definitions meet.
The map file settles which version was used. Linking row 1 with the added option -Wl,-Map=app.map writes a map file that begins:
Archive member included to satisfy reference by file (symbol)
./libsum.a(sum.o) main.o (checksum)
It reads: member sum.o of libsum.a was included because main.o needed checksum.
Stated precisely
The rules below are those of ELF §12.6 (Executable and Linking Format), the object file format that ld reads and writes on Linux and for Arm microcontrollers §6.1, which are small computers on one chip. The ELF specification calls the linker the link editor. A symbol that other files may use has one of two bindings: global, the normal case, or weak, which marks a definition as replaceable. A use of a name can also be marked weak, which makes the name optional. Let be a name that some included piece needs.
If the included pieces contain exactly one global definition of , every use of is connected to it.
Two global definitions of among the included pieces are an error.
A global definition takes precedence over weak definitions of among the included pieces, without an error. If the included pieces hold only weak definitions of ,
lduses the first one that it read. The specification leaves that case to each linker.While is needed and not defined, the linker takes from a library a member that defines , unless every use of so far is marked weak. It searches each library once, at the position of the library on the command line. A member that is never taken out contributes no definition.
If is still not defined at the end, the link fails, unless every use of is marked weak. In that case gets the value zero.
The connection is made by name alone. The linker does not compare the number or the types of the arguments, the type of the result, or the calling rules that the two sides were compiled with. Those are part of the ABI §12.47 (application binary interface), the binary rules that both sides must have been compiled to follow.
Limits and common mistakes
A source file in the project is not necessarily in the program. A library member that nothing needs is left out. With the option --gc-sections the linker also discards sections of included files that cannot be reached through references from the program’s entry point, the place where execution starts. Sections that the linker script marks with KEEP are spared.
The order of libraries matters. A library must come after the files that need it. Libraries that need each other must be listed twice, or wrapped in --start-group and --end-group, which makes the linker search the group repeatedly.
With shared libraries, which are libraries kept in separate files and loaded when a program starts, the final connection is made later. The linker only records that a symbol is expected from a shared library, and the dynamic linker §12.49, the part of the system that loads shared libraries, connects it when the program starts, using the libraries present on that machine. To tell the two apart, the linker of this entry is also called the static linker. Programs for small microcontrollers are normally linked statically, with no such later step.
The optimizer of a compiler can remove a call before the linker sees it, by copying the called function into its caller. The function can then still be in the program, with its symbol, although that caller never jumps to it.
Questions answered
Which library implementation satisfied a referenced function?
The implementation that satisfied a referenced function is the one whose definition the linker connected to the reference, and the linker’s map file, a report that the linker writes on request, names it. The linker is the tool that joins compiled files into one program, and a reference is a use, in one compiled file, of a function that is defined in another. The source code and the list of libraries do not settle the question, because several libraries can define a function with the same name, and the linker then uses one of them without a warning.
To find the answer for a particular program:
Link with a map file, using the option
-Wl,-Map=app.mapwhen the link is run throughgcc. The first table of the map file lists each library member that was included, the file that needed it and the name that caused it.Or add the option
-Wl,--trace-symbol=checksum, with the name of the function in place ofchecksum. The linker then prints every file that refers to the name and the file that defines it.
The rule behind the answer, for the GNU linker, is this. An object file named on the command line always supplies its definitions. Among the static libraries that follow the file needing the name on the command line, the first one that defines the name supplies it. Knowing which implementation was linked says nothing about whether it behaves as the caller expects, because the linker matches names only.
Related entries: Linker script (§12.5), ELF (§12.6), Symbol table (§12.8), Native application binary interface (ABI) (§12.47), Relocation: turning a symbolic reference into exact encoded bits (§12.50), Program loading: from an ELF file to a running native process (§12.49). Further reading: [109, 110].