Go: Object File & Relocations
ℹ️ This article is based on Go 1.14.
The relocation is a stage during the linking process that assigns proper addresses to each external symbol. Since the packages are compiled separately, they have no idea where the actual functions or variables from other packages are. Let’s start with a trivial example where relocation is needed.
Compilation
The following program involves two different packages: main
and fmt
.
Building this program will first involve the compiler that compiles each package separately:
With those intermediate files, we can look at them to see temporary instruction addresses thanks to the command go tool compile -S -l main.go
:
Once our program is compiled, we can look at the generated file with the command go tool compile -S -l main.go
, which displays the assembly code.
To look at the generated instruction by the compiler, you have different options: