Go: How to Take Advantage of the Symbols Table
ℹ️ This article is based on Go 1.13.
A symbol table is created and maintained by the compiler and stores information relating to the program, such as functions or global variables. Understanding the symbols table will help us to interact with and take advantage of it.
Symbol table
Each binary compiled with Go embeds the symbols table by default. Let’s take an example and inspect its symbols table. Here is our code:
var AppVersion string
func main() {
fmt.Println(`Version: `+AppVersion)
}
The symbols table can be displayed via the command nm
; here is an extract of the result on OSX:
0000000001177220 b io.ErrUnexpectedEOF
[...]
0000000001177250 b main.AppVersion
00000000010994c0 t main.main
[...]
0000000001170b00 d runtime.buildVersion
The symbol marked with b
(shortcode for bss) is uninitialized data. Since our previous variable AppVersion
is not initialized, it is part of it. The other symbols stand for initialized data (d
) and text symbols (t
), which comprises functions.
Go also ships the nm
command, accessible from the command go tool nm
, and produces the same results:
1177220 B io.ErrUnexpectedEOF
[...]
1177250 B main.AppVersion
10994c0 T main.main…