Linking
The linker takes object files and libraries and produces one executable (or one shared library). It does two jobs:
- Symbol resolution: match every undefined symbol (like
greetinmain.o) with exactly one definition somewhere in the inputs. - Relocation: decide the final address of every section, then patch every hole the assembler left, as in the model at the end of Object Files (-c).
On Linux the linker is ld (GNU BFD ld; gold, lld and mold are faster alternatives). You almost never call it directly: gcc passes the right start-up files and libraries to it. If you are curious about calling ld by hand, Linking with C in the assembly course does it for programs without the C runtime.
Linking two objects
mkdir -p lnk && cd lnk
cat > greet.c <<'SRC'
#include <stdio.h>
void greet(const char *who) { printf("Hello, %s!\n", who); }
SRC
cat > main.c <<'SRC'
void greet(const char *who);
int main(void) { greet("linker"); return 0; }
SRC
gcc -c -O1 greet.c main.c
gcc greet.o main.o -o app
./app
file app | cut -d, -f1-2
objdump -d --no-show-raw-insn -M intel app | sed -n '/<main>:/,/ret/p'Hello, linker!
app: ELF 64-bit LSB pie executable, x86-64
000000000000116f <main>:
116f: endbr64
1173: sub rsp,0x8
1177: lea rdi,[rip+0xe92] # 2010 <_IO_stdin_used+0x10>
117e: call 1149 <greet>
1183: mov eax,0x0
1188: add rsp,0x8
118c: ret
Compare the call with the one in main.o: the placeholder is gone and the call now targets greet at its final address. gcc -c greet.c main.c compiles two files in one command but still produces two independent objects.
What the linker actually read
-Wl,<option> passes an option through the driver to the linker. --trace makes ld print each input file it opens, which shows everything gcc added on your behalf:
mkdir -p lnk && cd lnk
printf 'int main(void) { return 0; }\n' > main.c
gcc -c main.c
gcc main.o -o app -Wl,--trace | sed 's#/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/#<libdir>/#; s#/usr/lib/gcc/x86_64-linux-gnu/13/#<gccdir>/#'<libdir>/Scrt1.o
<libdir>/crti.o
<gccdir>/crtbeginS.o
main.o
<gccdir>/libgcc.a
<gccdir>/libgcc_s.so
<libdir>/libgcc_s.so.1
<gccdir>/libgcc.a
<libdir>/libc.so
/lib/x86_64-linux-gnu/libc.so.6
/usr/lib/x86_64-linux-gnu/libc_nonshared.a
/lib64/ld-linux-x86-64.so.2
/usr/lib/x86_64-linux-gnu/libc_nonshared.a
/lib64/ld-linux-x86-64.so.2
<gccdir>/libgcc.a
<gccdir>/libgcc_s.so
<libdir>/libgcc_s.so.1
<gccdir>/libgcc.a
<gccdir>/crtendS.o
<libdir>/crtn.o
The long directories are shortened by sed. Your one object is surrounded by start-up objects (Scrt1.o, crti.o, crtbeginS.o …), libgcc (helper routines the compiler may call, such as 128-bit division) and the C library libc.so. Note that libc.so is followed by the files it really is: on glibc systems libc.so is a small linker script pointing at libc.so.6 and libc_nonshared.a, plus the dynamic loader. Some files appear twice because the driver passes the libraries both before and after libc, so that symbols needed by one of them can still be found in the other; Link Errors & Link Order explains why order matters.
Static and dynamic linking
By default, library code from libc is not copied into your program. The executable records "I need libc.so.6" and the dynamic loader maps the library at start-up. With -static, the linker copies the needed parts of libc.a into the executable instead:
mkdir -p lnk && cd lnk
printf '#include <stdio.h>\nint main(void) { puts("static or not?"); return 0; }\n' > main.c
gcc main.c -o app-dynamic
gcc -static main.c -o app-static
./app-dynamic && ./app-static
for f in app-dynamic app-static; do printf '%-12s %8s bytes ' $f $(stat -c %s $f); file -b $f | grep -oE '(dynamically|statically) linked'; donestatic or not?
static or not?
app-dynamic 15960 bytes dynamically linked
app-static 785384 bytes statically linked
| Dynamic (default) | Static (-static) |
|
|---|---|---|
| Size | small: only your code | large: includes the library code you use |
| Library updates | a fixed libc.so.6 fixes every program at once |
each program must be rebuilt |
| Deployment | the right shared libraries must exist on the target | one file runs anywhere with the same OS and CPU |
| Start-up | the loader resolves symbols at start-up | no loader work |
Many languages pick static by default (Go and Rust link their own code statically), while C and C++ on Linux default to dynamic linking against the system libraries. The next section of this course builds both kinds of library yourself.
A linker map
-Wl,-Map=app.map writes a map file: where every section and symbol from every input landed. When a binary is unexpectedly large, the map shows who contributed what:
mkdir -p lnk && cd lnk
cat > greet.c <<'SRC'
#include <stdio.h>
void greet(const char *who) { printf("Hello, %s!\n", who); }
SRC
printf 'void greet(const char *);\nint main(void) { greet("map"); return 0; }\n' > main.c
gcc -c -O1 greet.c main.c
gcc greet.o main.o -o app -Wl,-Map=app.map
grep -E '^ \.text +0x[0-9a-f]+ +0x[0-9a-f]+ (greet|main)\.o