Section 2.7 Linking
As mentioned in Section 2.3, the linker binds different object files together into one executable. We will give a short overview over linking here because it is also relevant for languages like C/C++ that compile down different translation units of code into separate object files.
The main object of the linker is to give each instruction and element of the data segment a definitive address and resolve references to global labels. As mentioned in Section 2.3 one object file can reference a label in another object file. To be able to resolve global labels, each object file carries a symbol table that lists all global labels that the object file defines and references. The linker merges the code and data sections of the individual object files respectively and orders them linearly. Except for code and data from dynamically linked libraries 5 , which we ignore here, the address of each datum and each instruction is then fixed. The linker can then patch absolute addresses global relative addresses. Patching means that the bytes take the address of a referenced label are overwritten with the final absolute or relative address.
.data
.globl y
y: .word 42
m: .asciiz "Please enter a number: "
.text
.globl main
main:
li $v0 4
la $a0 m
syscall
li $v0 5
syscall
move $a0 $v0
jal add_y
move $a0 $v0
li $v0 1
syscall
Run
Symbol | Properties |
y |
defined, global |
m |
defined |
main |
defined, global |
add_y |
referenced |
.text
.globl add_y
add_y:
lw $v0 y
addu $v0 $v0 $a0
jr $ra
Run
Symbol | Properties |
add_y |
defined, global |
y |
referenced |
libraries that are only linked against when the program is loaded