How does golang delete source path information in binary files

Time:2021-10-26

method

Go v1.13 go build adds the – trimpath parameter, which is not as troublesome as before.


➜  awesomeProject CGO_ENABLED=0 go build -v -a -ldflags="-w -s" -trimpath \
    -o ./hello_word hello_word.go
➜  awesomeProject strings hello_word|grep src
➜  awesomeProject 
#Before
➜  awesomeProject go tool objdump hello_word
TEXT go.buildid(SB)
.....
TEXT main.main(SB) /Users/xxxx/go/src/awesomeProject/hello_word.go
  hello_word.go:3	0x104e580		65488b0c2530000000	MOVQ GS:0x30, CX
  hello_word.go:3	0x104e589		483b6110		CMPQ 0x10(CX), SP
  hello_word.go:3	0x104e58d		763b			JBE 0x104e5ca
  hello_word.go:3	0x104e58f		4883ec18		SUBQ $0x18, SP
  hello_word.go:3	0x104e593		48896c2410		MOVQ BP, 0x10(SP)
  hello_word.go:3	0x104e598		488d6c2410		LEAQ 0x10(SP), BP
  hello_word.go:4	0x104e59d		e89e50fdff		CALL runtime.printlock(SB)
  hello_word.go:4	0x104e5a2		488d059eef0100		LEAQ go.string.*+2759(SB), AX
  hello_word.go:4	0x104e5a9		48890424		MOVQ AX, 0(SP)
  hello_word.go:4	0x104e5ad		48c74424080d000000	MOVQ $0xd, 0x8(SP)
  hello_word.go:4	0x104e5b6		e8b559fdff		CALL runtime.printstring(SB)
  hello_word.go:4	0x104e5bb		e80051fdff		CALL runtime.printunlock(SB)
  hello_word.go:5	0x104e5c0		488b6c2410		MOVQ 0x10(SP), BP
  hello_word.go:5	0x104e5c5		4883c418		ADDQ $0x18, SP
  hello_word.go:5	0x104e5c9		c3			RET
  hello_word.go:3	0x104e5ca		e8f184ffff		CALL runtime.morestack_noctxt(SB)
  hello_word.go:3	0x104e5cf		ebaf			JMP main.main(SB)
  :-1			0x104e5d1		cc			INT $0x3
  :-1			0x104e5d2		cc			INT $0x3
#Recompile
➜  awesomeProject CGO_ENABLED=0 go build -v -a -ldflags="-w -s" \
    -gcflags=-trimpath=/Users/xxxx/go/src \
    -asmflags=-trimpath=/Users/xxxx/src \
    -o ./hello_word hello_word.go
runtime/internal/sys
runtime/internal/atomic
internal/cpu
runtime/internal/math
internal/bytealg
runtime
command-line-arguments
➜  awesomeProject
#Or
➜  awesomeProject CGO_ENABLED=0 go build -v -a -ldflags="-w -s" \
    -gcflags=-trimpath=$GOPATH/src \
    -asmflags=-trimpath=$GOPATH/src \
    -o ./hello_word hello_word.go
runtime/internal/sys
runtime/internal/atomic
internal/cpu
runtime/internal/math
internal/bytealg
runtime
command-line-arguments
➜  awesomeProject
#Effect
➜  awesomeProject go tool objdump hello_word
TEXT go.buildid(SB)
.....
TEXT main.main(SB) awesomeProject/hello_word.go
  hello_word.go:3	0x104e580		65488b0c2530000000	MOVQ GS:0x30, CX
  hello_word.go:3	0x104e589		483b6110		CMPQ 0x10(CX), SP
  hello_word.go:3	0x104e58d		763b			JBE 0x104e5ca
  hello_word.go:3	0x104e58f		4883ec18		SUBQ $0x18, SP
  hello_word.go:3	0x104e593		48896c2410		MOVQ BP, 0x10(SP)
  hello_word.go:3	0x104e598		488d6c2410		LEAQ 0x10(SP), BP
  hello_word.go:4	0x104e59d		e89e50fdff		CALL runtime.printlock(SB)
  hello_word.go:4	0x104e5a2		488d059eef0100		LEAQ go.string.*+2759(SB), AX
  hello_word.go:4	0x104e5a9		48890424		MOVQ AX, 0(SP)
  hello_word.go:4	0x104e5ad		48c74424080d000000	MOVQ $0xd, 0x8(SP)
  hello_word.go:4	0x104e5b6		e8b559fdff		CALL runtime.printstring(SB)
  hello_word.go:4	0x104e5bb		e80051fdff		CALL runtime.printunlock(SB)
  hello_word.go:5	0x104e5c0		488b6c2410		MOVQ 0x10(SP), BP
  hello_word.go:5	0x104e5c5		4883c418		ADDQ $0x18, SP
  hello_word.go:5	0x104e5c9		c3			RET
  hello_word.go:3	0x104e5ca		e8f184ffff		CALL runtime.morestack_noctxt(SB)
  hello_word.go:3	0x104e5cf		ebaf			JMP main.main(SB)

Trimpath description


-trimpath prefix
 Remove prefix from recorded source file paths.

Supplement: the compilation path gopath information in the bin file is removed during go compilation

Cause of problem

When the golang program panic or obtain the current error file location through runtime. Caller (0) as a log record, the project path and account on the program compilation machine will be exposed. We don’t want the other party to see the following information.


panic: oh! no!
goroutine 1 [running]:
main.main()
        /Users/jerry/go/src/demo/panic_demo/main.go:10 +0x64

Problem phenomenon

When we pass strings panic_ Demo | grep / users can obtain complete source code path information by statically analyzing the binary compiled by golang:


/Users/jerry/go/src/demo/panic_demo/main.go
/Users/jerry/go/src/demo/panic_demo/main.go
/Users/jerry/go/src/demo/panic_demo

We may not want the other party to know this information, so we need to process it and eliminate it.

Solution

During compilation, the following parameters are passed in to eliminate


go build -gcflags=-trimpath=${GOPATH}-asmflags=-trimpath=${GOPATH}

A more thorough way


go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-w -s"

After processing, the display is like this, without ${gopath} information and normal stack information.


panic: oh! no!
goroutine 1 [running]:
main.main()
        src/demo/myssl_demo/getcert_demo.go:10 +0x64

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.

Recommended Today

Hero League Python crawler

Article catalog Hero League Python crawler 1. Hero crawling 2. JS obtains all hero information 3. Crawl the game data First lol page crawl The second lol web page data crawling The third lol web page data crawling 4. Multi thread crawling lol hero skin image Hero League Python crawler Hero main interface QQhttps://lol.qq.com/data/info-heros.shtml 1. […]