On this page

The linker has no idea where your GPU lives: it merges sections and patches symbol addresses, while calling an external function in PIE goes through a jump table.

A single .asm file already builds cleanly, yet real binaries almost always combine multiple object files and libraries. In this lesson, I wire together three modules, package one inside a static archive, and trace how a libc function’s address lands in the PLT and GOT.

Mental Model: Names Instead of Addresses

What it looks like in C99 What happens in hardware
A function “lives in a library” The object file only carries a symbol name and a relocation entry
The linker knows about hardware The linker only knows about sections, symbols, and relocation tables
A weak symbol is a compiler flag It’s a symbol binding type in the ELF symbol table; a strong definition overrides a weak one
Calling printf is a direct call In PIE it’s call printf@plt, and the target address comes from the GOT

Three Modules and an Archive

math.asm exports asm_scale, main.asm calls both our symbols and libc functions, and weak_logger.asm declares asm_log_value as a weak symbol. A standard global symbol is exported like this:

math.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; math.asm — standard object module exporting a strong global symbol.

default rel

section .text
    global asm_scale

; ------------------------------------------------------------------------------
; int32_t asm_scale(int32_t value)
; Input:     edi = value
; Output:    eax = value * 3 + 1
; Clobbers:  nothing except eax (flags untouched)
; ------------------------------------------------------------------------------
asm_scale:
    lea eax, [rdi + rdi * 2 + 1]

    ret

section .note.GNU-stack noalloc noexec nowrite progbits

global asm_scale enters the object file’s symbol table with type T. We can pack this module into an archive:

$ ar rcs build/libmath.a build/math.o
$ ar t build/libmath.a
math.o
$ nm build/libmath.a

math.o:
0000000000000000 T asm_scale

ar t lists the archive’s members, and nm shows the symbol inside its single object file. When linking, passing -Lbuild -lmath lets the linker pull in math.o automatically.

Weak Symbols

A weak definition uses the :weak qualifier:

weak_logger.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; weak_logger.asm — weak logger definition that can be omitted at link time.

default rel

%define SYS_WRITE 1
%define STDERR 2

section .rodata
    logger_msg: db "logger: weak asm_log_value", 10
    logger_len: equ $ - logger_msg

section .text
    global asm_log_value:weak

; ------------------------------------------------------------------------------
; void asm_log_value(int32_t value)
; Input:     edi = value (unused; only the fact of being called matters)
; Output:    none; writes a message to stderr
; Clobbers:  rax, rcx, rdx, rsi, rdi, r11 (syscall; flags untouched)
; ------------------------------------------------------------------------------
asm_log_value:
    ; syscall: write(STDERR, logger_msg, logger_len)
    mov eax, SYS_WRITE
    mov edi, STDERR
    lea rsi, [logger_msg]
    mov edx, logger_len
    syscall

    ret

section .note.GNU-stack noalloc noexec nowrite progbits

The value parameter arriving in edi is intentionally unused: the logger only signals that it was called, and by the time syscall executes, edi holds the stderr file descriptor.

main.asm imports the same symbol as extern asm_log_value:weak and checks whether its address is non-zero before calling it:

main.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; main.asm — PIE program: libc calls go through PLT, logger is weakly linked.

default rel

section .rodata
    msg_ok:     db "asm_scale: ok", 0
    msg_fail:   db "asm_scale: fail", 0

section .text
    extern asm_scale
    extern asm_log_value:weak
    extern puts
    extern exit

    global _start

_start:
    ; the kernel hands over control with rsp % 16 == 0, no extra shift needed
    and rsp, -16

    mov edi, 7
    call asm_scale

    cmp eax, 22
    jne .fail

    lea rdi, [msg_ok]
    call puts wrt ..plt

    ; an undefined weak extern resolves to zero in the GOT
    mov rax, [rel asm_log_value wrt ..got]
    test rax, rax
    jz .no_logger

    mov edi, 22
    call rax

.no_logger:
    xor edi, edi
    call exit wrt ..plt

.fail:
    lea rdi, [msg_fail]
    call puts wrt ..plt
    mov edi, 1
    call exit wrt ..plt

section .note.GNU-stack noalloc noexec nowrite progbits

The wrt ..plt and wrt ..got modifiers stand for “with respect to”—telling the assembler which table to compute the displacement against:

  • call puts wrt ..plt emits R_X86_64_PLT32: the offset targets the PLT stub rather than the libc function itself;
  • mov rax, [rel asm_log_value wrt ..got] emits R_X86_64_GOTPCREL: the offset targets the GOT slot holding the symbol’s address.

and rsp, -16 in _start is defensive alignment before call puts: even though the kernel already provides rsp % 16 == 0, an explicit mask guarantees the ABI contract regardless of startup environment.

Without the second modifier, [rel asm_log_value] would emit R_X86_64_PC32, which is forbidden for an undefined weak symbol in a PIE binary.

If weak_logger.o isn’t passed to the linker, the reference resolves to zero and .no_logger skips the call. The only difference between the two builds is the object list (libmath.a is already built from math.o):

$ nasm -f elf64 -g -F dwarf -o build/main.o main.asm
$ nasm -f elf64 -g -F dwarf -o build/weak_logger.o weak_logger.asm
$ gcc -nostartfiles -pie -o build/main_weak build/main.o build/weak_logger.o -Lbuild -lmath
$ gcc -nostartfiles -pie -o build/main_noweak build/main.o -Lbuild -lmath

In the linked binary, the case of the letter in nm tells you the symbol’s state:

$ nm build/main_weak | grep asm_log_value
0000000000001080 W asm_log_value
$ nm build/main_noweak | grep asm_log_value
                 w asm_log_value
  • W — a weak definition is present in the binary.
  • w — an unresolved weak reference; its value evaluates to zero.
  • T on asm_scale — an ordinary strong global symbol in .text.

Both binaries succeed and print asm_scale: ok; the only difference is the logger message written to stderr.

PLT, GOT, and Relocations

Before linking, main.o still holds symbol names and relocation types. Here are the entries for our calls and the weak symbol:

$ readelf -W -r build/main.o | grep -E 'PLT32|GOTPCREL|asm_scale'
000000000000000a  ... R_X86_64_PC32      ... asm_scale - 4
000000000000001b  ... R_X86_64_PLT32     ... puts - 4
0000000000000022  ... R_X86_64_GOTPCREL  ... asm_log_value - 4
0000000000000035  ... R_X86_64_PLT32     ... exit - 4
... (two more PLT32 relocations for puts and exit in .fail)
  • R_X86_64_PC32 — ordinary PC-relative offset within the binary, used to call asm_scale.
  • R_X86_64_PLT32 — call through a PLT stub, used for puts and exit.
  • R_X86_64_GOTPCREL — load the address from the GOT, required so a weak symbol can resolve to zero in PIE.

The ellipses omit the repetitive Info and Symbol's Value columns. After linking, PLT relocations become .rela.plt entries:

$ readelf -W -r build/main_weak | grep JUMP_SLOT
0000000000004000  ... R_X86_64_JUMP_SLOT ... puts@GLIBC_2.2.5 + 0
0000000000004008  ... R_X86_64_JUMP_SLOT ... exit@GLIBC_2.2.5 + 0

Addresses 0x4000 and 0x4008 live in .got.plt: these are the slots where the dynamic linker stores the real addresses of puts and exit. The PLT stub itself is tiny:

$ objdump -d -M intel -j .plt build/main_weak | sed -n '/<puts@plt>:/,+2p'
0000000000001010 <puts@plt>:
    1010: ff 25 ea 2f 00 00  jmp    QWORD PTR [rip+0x2fea]        # 4000 <puts@GLIBC_2.2.5>
    1016: 68 00 00 00 00     push   0x0

The first jmp jumps indirectly through the GOT slot at 0x4000. Before the symbol is resolved, that slot points back to the stub’s next instruction: push 0x0 pushes the symbol index and transfers control to the dynamic resolver. After the first call, the real address stays in the GOT slot, so subsequent calls execute a single indirect jump.

1st call: call puts@plt

jmp via GOT slot 0x4000

push 0

ld-linux resolver

puts() in libc

2nd call: call puts@plt

jmp via GOT slot 0x4000

GOT unresolvedwrites address into GOTGOT already resolved

1st call: call puts@plt

jmp via GOT slot 0x4000

push 0

ld-linux resolver

puts() in libc

2nd call: call puts@plt

jmp via GOT slot 0x4000

GOT unresolvedwrites address into GOTGOT already resolved
Fig. 1. First and subsequent calls through PLT and GOT

The unresolved weak reference in main_noweak stays in the GOT as R_X86_64_GLOB_DAT:

$ readelf -W -r build/main_noweak | grep GLOB_DAT
0000000000003fe0  0000000300000006 R_X86_64_GLOB_DAT      0000000000000000 asm_log_value + 0

Slot 0x3fe0 is the exact location main_noweak reads; Symbol's Value is zero because no definition exists. In main_weak, the linker sees the weak definition in the same executable and relaxes the GOT load into lea:

$ objdump -d -M intel build/main_weak | grep -A1 'lea.*asm_log_value'
    104f: 48 8d 05 2a 00 00 00  lea    rax,[rip+0x2a]        # 1080 <asm_log_value>
    1056: 48 85 c0              test   rax,rax

In main_noweak, the same instruction remains a memory load from the GOT slot:

$ objdump -d -M intel build/main_noweak | grep -A1 'mov.*asm_log_value'
    104f: 48 8b 05 8a 2f 00 00  mov    rax,QWORD PTR [rip+0x2f8a]        # 3fe0 <asm_log_value>
    1056: 48 85 c0              test   rax,rax

That optimization is completely legal: since the address is known at link time, main_weak eliminates an unnecessary memory read.

The five relocation types we encountered fit into one summary table—the first three live in object files, while the last two appear in the linked ELF:

Relocation Meaning Where it appears
R_X86_64_PC32 PC-relative offset inside the binary readelf -r on main.o: calling local asm_scale
R_X86_64_PLT32 function call via a PLT stub main.o: call puts wrt ..plt and call exit wrt ..plt
R_X86_64_GOTPCREL load global symbol address from GOT main.o: mov rax, [rel asm_log_value wrt ..got]
R_X86_64_JUMP_SLOT dynamic .got.plt slot patched by the dynamic linker .rela.plt after linking: puts and exit
R_X86_64_GLOB_DAT dynamic GOT slot for data/weak symbols .rela.dyn in main_noweak: asm_log_value

The “Where it appears” column separates two phases:

  • PC32, PLT32, and GOTPCREL — unresolved references in .o files;
  • JUMP_SLOT and GLOB_DAT — dynamic-linker records in the final ELF executable.

Verification in the Terminal

Building and testing both variants takes one command:

$ cd lessons/17-linking-plt-got
$ make build test
$ ./build/main_weak
asm_scale: ok
$ ./build/main_weak 2>&1 | cat
logger: weak asm_log_value
asm_scale: ok
$ ./build/main_noweak
asm_scale: ok

In the second run, 2>&1 merges stderr into stdout, so the logger line appears ahead of puts because stdout flushes later. main_noweak proves that omitting a weak definition doesn’t break linking—as long as you check the address for zero before calling it.

Common Pitfalls

  1. “PIE rejects a weak reference.”
    Fact: ld: relocation R_X86_64_PC32 against undefined symbol 'asm_log_value' can not be used when making a PIE object.
    Cause: lea and mov with R_X86_64_PC32 require a fixed in-image address, whereas an undefined weak symbol may resolve to zero.
    Fix: load the address through the GOT: mov rax, [rel asm_log_value wrt ..got].

  2. “The static archive can’t see the symbol.”
    Fact:

    text
    $ gcc -nostartfiles -pie -o build/main_nomath -Lbuild -lmath build/main.o
    /usr/bin/ld: build/main.o: warning: relocation against `asm_scale' in read-only section `.text'
    /usr/bin/ld: build/main.o: in function `_start':
    .../17-linking-plt-got/main.asm:24:(.text+0xa): undefined reference to `asm_scale'
    /usr/bin/ld: warning: creating DT_TEXTREL in a PIE
    collect2: error: ld returned 1 exit status

    Line 24 points to call asm_scale in main.asm above; the warnings (including DT_TEXTREL—attempting a runtime text relocation in read-only .text) occur because the reference stayed unresolved while linking a PIE.
    Cause: GNU ld scans archives left to right in a single pass and extracts from .a only members that satisfy already-seen undefined symbols; -lmath appeared before main.o.
    Fix: place -lmath after main.o and weak_logger.o on the link line.

  3. “The logger is silent even though everything built.”
    Fact: ./build/main_noweak prints only asm_scale: ok.
    Cause: weak_logger.o wasn’t passed to the linker, so asm_log_value resolved to zero.
    Fix: add the object file to the link command, or check nm for lowercase w instead of W.

Lab

Goal. Write a strong second logger strong_logger.asm defining the same symbol asm_log_value and make the build pick it over the weak version.

Contract and requirements.

  • strong_logger.asm exports asm_log_value as a regular global symbol and prints a different message to stderr.
  • When linked with strong_logger.o, the binary must contain no weak definition, and nm must report T asm_log_value.
  • The weak variant from weak_logger.o must keep working on its own.

Verification criteria.

  1. nm build/main_strong | grep asm_log_value prints T, not W.
  2. Running with 2>&1 prints the strong logger’s line and asm_scale: ok.
  3. readelf -W -r build/main_strong contains no R_X86_64_GLOB_DAT entry for asm_log_value.
  4. make -C lessons/17-linking-plt-got test passes.
Hints, expected output, and walkthrough

Declare a strong definition without any suffix: global asm_log_value. The linker picks it because a strong symbol always overrides a weak one from another object file, regardless of object order.

Because the target address is now defined in the same executable, the linker converts the GOT load (GOTPCREL) into a direct lea via linker relaxation, and the R_X86_64_GLOB_DAT entry for asm_log_value disappears.

Expected output:

$ ./build/main_strong 2>&1
logger: strong asm_log_value
asm_scale: ok

Testbed sources, Makefile, and automated tests for this lesson: lessons/17-linking-plt-got.

Summary

  1. global and extern operate on symbol names, not physical device addresses.
  2. A .a archive bundles object files with a symbol index; the linker extracts only needed members.
  3. A weak definition uses :weak and can be overridden by a strong symbol.
  4. Calling an external shared-library function in PIE routes through the PLT and caches its address in the GOT.
  5. PC32, PLT32, GOTPCREL, JUMP_SLOT, and GLOB_DAT represent successive stages of symbol resolution.
Bridge to the Next Lesson
Symbols are resolved and addresses are patched, yet calling a function still rests on an informal agreement about registers. In lesson 18, I unpack the System V AMD64 ABI: why arguments go in rdi–r9, what al tells printf, and why movaps crashes on an unaligned stack.