Go: Object File & Relocations

Vincent
A Journey With Go
Published in
5 min readJul 1, 2020

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ 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:

--

--