24

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"

dv1729
  • 987
  • 1
  • 8
  • 25

2 Answers2

17

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

Tomas
  • 1,531
  • 1
  • 16
  • 22
12

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.

snap
  • 2,751
  • 22
  • 33
  • 1
    I forced a panic and the stack trace clearly displayed function names and line numbers using both solutions :S – dv1729 May 02 '15 at 22:12
  • 4
    Any information that the Go runtime may require still stays in the binary. This includes information required for panic messages, type information, etc. I am not aware of any way of stripping it out. This is different from pure debugging information. – snap May 03 '15 at 10:55
  • 9
    Strip is not recommended by the Go Team, it may break the executable. It is better to use `go build -ldflags '-s'` to omit the symbol table, however I'm not sure if you can hide every symbol name completely. – siritinga May 04 '15 at 08:29