Inspect linker relocations and DWARF
Inspect linker relocations and DWARF
Compilation produces object code with symbols and references that may not yet have final addresses. A relocation describes a reference requiring adjustment as code and data are laid out. The linker resolves symbols, removes eligible unreachable code and lays out the executable; some dynamic relocations may remain for a loader. DWARF is debugging metadata describing source locations, types and other information for debuggers. It is not the executable instruction stream. Use trusted, locally built files for these inspection exercises.
package main
import ( "fmt")
//go:noinlinefunc add(a, b int) int { return a + b }func main() { fmt.Println(add(2, 3))}Run go build -gcflags=-S -o demo main.go 2>asm.txt. Look for a relocation line referring to main.add in the compiler listing and inspect the resolved symbol using go tool nm demo and go tool objdump -s "main\.main$" demo. Object relocations and the final executable relocation table are different; absence of a particular dynamic relocation in a statically linked executable does not mean no relocation was processed.
Expected output:
5Check embedded Go build information
package main
import ( "debug/buildinfo" "fmt" "os" "runtime")
func main() { p, e := os.Executable() if e != nil { panic(e) } info, e := buildinfo.ReadFile(p) if e != nil { panic(e) } fmt.Println(info.GoVersion == runtime.Version())}Build information is distinct from DWARF. go version -m demo also reports available module/build information. On Linux, compare an ordinary build with go build -ldflags="-s -w" -o stripped main.go; these flags omit symbol/debug information and DWARF. Use readelf -S demo if available, or debug/elf.Section and File.DWARF on trusted binaries to inspect the difference. Stripping does not change arithmetic behavior, but can reduce debugger visibility. Do not equate missing DWARF with absence of every runtime symbol or trace feature.
Expected output:
trueTry it locally
Use debug.ReadBuildInfo in a running program. Print whether build information exists and whether its GoVersion matches runtime.Version, guarding against an absent result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compiler pipeline 08: Inspect linker relocations and DWARF
Check the lesson objective before moving on.
What is an object relocation for?
What is DWARF used for?