Linking object files
Splitting into objects
Each source assembles to one object. Symbols you export are global; symbols you import are extern. The linker joins them by name:
; main.asm extern print_string section .data msg db "hello from another object", 10 len equ $ - msg section .text global _start _start: mov rdi, msg mov rsi, len call print_string mov rax, 60 xor rdi, rdi syscall
; print.asm global print_string section .text print_string: mov rax, 1 mov rdx, rsi mov rsi, rdi mov rdi, 1 syscall ret
Linking
List the objects after the output file. Order matters for static libraries but not for plain objects, and every referenced symbol must be defined somewhere or ld reports an undefined reference:
nasm -f elf64 -o main.o main.asm
nasm -f elf64 -o print.o print.asm
ld -o program main.o print.o
./program
echo $?
hello from another object
0
Static linking
ld links statically by default for plain objects. For libc programs, link with gcc -static to embed the C library, or leave it dynamic (the default with gcc) and let the loader resolve symbols at start.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
extern print_stringtell the assembler?Answer
The symbol is defined in another object
extern names a symbol this file calls but does not define, and the linker supplies its address.
-
What does
global print_stringdo?Answer
It makes the label visible to other objects
A global symbol goes into the object symbol table, where the linker can find it.
-
Which command links main.o and print.o into the executable program?
Answer
ld -o program main.o print.o
ld takes the objects as inputs and -o names the output file.
-
What does ld report when a referenced symbol is defined nowhere?
Answer
An undefined reference
The linker must resolve every external symbol, and an undefined reference stops the link.
-
For which inputs does the order on the ld command line matter?
Answer
Static libraries
The linker reads a static library once, so a library must follow the object that uses it.
-
What does this two-object program print?
main.asmcallsprint_stringwith the messagehello from another object, andprint.asmwrites the buffer it receives.Answer
hello from another object
The data label and the write syscall of the second object produce the message.
-
Why is
print.asmon this page not a program by itself?Answer
It defines no _start entry point
It is a library object with one exported procedure; main.asm supplies _start.
-
What carries the return address from
call print_stringback toret?Answer
The stack
call pushes the address of the next instruction, and ret pops it.
-
What exit status does the linked program return?
Answer
0
main.asm clears rdi before sys_exit, so the status is 0.
-
What does the linker use to join
call print_stringwith the code in print.o?Answer
The symbol table of each object
Each object lists its defined and undefined symbols, and the linker matches them by name.
The code on this page was assembled with NASM 3.01 and linked with GNU ld 2.45.1, and the program was run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev