您的位置:首页技术文章

golang 如何删除二进制文件中的源码路径信息

【字号: 日期:2023-10-23 19:04:43浏览:28作者:馨心
方法

go v1.13 go build 新增 -trimpath参数,不用以前那么麻烦了。

➜ 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

#之前➜ awesomeProject go tool objdump hello_wordTEXT go.buildid(SB).....TEXT main.main(SB) /Users/xxxx/go/src/awesomeProject/hello_word.go hello_word.go:30x104e58065488b0c2530000000MOVQ GS:0x30, CX hello_word.go:30x104e589483b6110CMPQ 0x10(CX), SP hello_word.go:30x104e58d763bJBE 0x104e5ca hello_word.go:30x104e58f4883ec18SUBQ $0x18, SP hello_word.go:30x104e59348896c2410MOVQ BP, 0x10(SP) hello_word.go:30x104e598488d6c2410LEAQ 0x10(SP), BP hello_word.go:40x104e59de89e50fdffCALL runtime.printlock(SB) hello_word.go:40x104e5a2488d059eef0100LEAQ go.string.*+2759(SB), AX hello_word.go:40x104e5a948890424MOVQ AX, 0(SP) hello_word.go:40x104e5ad48c74424080d000000MOVQ $0xd, 0x8(SP) hello_word.go:40x104e5b6e8b559fdffCALL runtime.printstring(SB) hello_word.go:40x104e5bbe80051fdffCALL runtime.printunlock(SB) hello_word.go:50x104e5c0488b6c2410MOVQ 0x10(SP), BP hello_word.go:50x104e5c54883c418ADDQ $0x18, SP hello_word.go:50x104e5c9c3RET hello_word.go:30x104e5cae8f184ffffCALL runtime.morestack_noctxt(SB) hello_word.go:30x104e5cfebafJMP main.main(SB) :-10x104e5d1ccINT $0x3 :-10x104e5d2ccINT $0x3# 重新编译➜ 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.goruntime/internal/sysruntime/internal/atomicinternal/cpuruntime/internal/mathinternal/bytealgruntimecommand-line-arguments➜ awesomeProject# 或者➜ awesomeProject CGO_ENABLED=0 go build -v -a -ldflags='-w -s' -gcflags=-trimpath=$GOPATH/src -asmflags=-trimpath=$GOPATH/src -o ./hello_word hello_word.goruntime/internal/sysruntime/internal/atomicinternal/cpuruntime/internal/mathinternal/bytealgruntimecommand-line-arguments➜ awesomeProject# 效果➜ awesomeProject go tool objdump hello_wordTEXT go.buildid(SB).....TEXT main.main(SB) awesomeProject/hello_word.go hello_word.go:30x104e58065488b0c2530000000MOVQ GS:0x30, CX hello_word.go:30x104e589483b6110CMPQ 0x10(CX), SP hello_word.go:30x104e58d763bJBE 0x104e5ca hello_word.go:30x104e58f4883ec18SUBQ $0x18, SP hello_word.go:30x104e59348896c2410MOVQ BP, 0x10(SP) hello_word.go:30x104e598488d6c2410LEAQ 0x10(SP), BP hello_word.go:40x104e59de89e50fdffCALL runtime.printlock(SB) hello_word.go:40x104e5a2488d059eef0100LEAQ go.string.*+2759(SB), AX hello_word.go:40x104e5a948890424MOVQ AX, 0(SP) hello_word.go:40x104e5ad48c74424080d000000MOVQ $0xd, 0x8(SP) hello_word.go:40x104e5b6e8b559fdffCALL runtime.printstring(SB) hello_word.go:40x104e5bbe80051fdffCALL runtime.printunlock(SB) hello_word.go:50x104e5c0488b6c2410MOVQ 0x10(SP), BP hello_word.go:50x104e5c54883c418ADDQ $0x18, SP hello_word.go:50x104e5c9c3RET hello_word.go:30x104e5cae8f184ffffCALL runtime.morestack_noctxt(SB) hello_word.go:30x104e5cfebafJMP main.main(SB)trimpath说明

-trimpath prefix Remove prefix from recorded source file paths.

补充:Go 编译时去除 bin 文件中的编译路径 GOPATH 信息

问题原因

当 golang 程序 panic,或者通过 runtime.Caller(0) 获取当前出错的文件位置作为日志记录时,会暴露程序编译机器上的项目路径、以及账户,不如下面这些信息, 这些信息我们并不想让对方看到。

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

当我们通过 strings panic_demo | grep /Users 静态分析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

这些信息我们可能并不想让对方知道, 所以我们需要对这些信息进行处理, 剔除这些信息。

解决方式在编译是通过传入以下参数来剔除

go build -gcflags=-trimpath=${GOPATH}-asmflags=-trimpath=${GOPATH}更彻底的方式

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

处理完后显示是这个样子,不带 ${GOPATH}信息也不影响正常的堆栈信息。

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持优爱好网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Golang
相关文章: