Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid debugging information on golang

Tags:

go

I think that gc includes debugging information by default. However, I want to avoid decompilation.

How can I remove the debugging information when compiling go code with gc?

Note: Using gccgo doesn't solve the problem. If I don't compile with '-g' the executable is broken and only outputs:

no debug info in ELF executable errno -1 fatal error: no debug info in ELF executable

runtime stack: no debug info in ELF executable errno -1 panic during panic"

like image 482
dv1729 Avatar asked May 02 '15 18:05

dv1729


People also ask

Does Golang have a debugger?

It allows you to build, edit, and debug programs. If you use VS Code to run your Go code and have the official Go extension installed on the editor, you can debug your code by pressing F5 or Run and Debug on your computer.

What debugger does GoLand use?

GoLand is a Go IDE from JetBrains, available from the JetBrains website. GoLand uses the Delve debugger internally to support time travel debugging.


2 Answers

I recommend usage of -ldflags="-s -w" which removes symbol table and debugging information.

As a bonus with Go 1.13 -trimpath can be used to reduce length of file paths stored in the file

like image 153
Tomas Avatar answered Oct 16 '22 20:10

Tomas


The go linker has a flag -w which disables DWARF debugging information generation. You can supply linker flags to go tool build commands as follows:

go build -ldflags '-w'

Another approach on Linux/Unix platforms is using command strip against the compiled binary. This seems to produce smaller binaries than the above linker option.

like image 29
snap Avatar answered Oct 16 '22 22:10

snap