golang strip symbols with go build vs strip

huangapple go评论96阅读模式
英文:

golang strip symbols with go build vs strip

问题

为什么在stripgo build -ldflags "-s -w"之间剥离符号的结果不同(参见这里)?以下是要翻译的代码部分:

$ go build -ldflags "-s -w" -o primes_no_symbols_from_go_build primes.go
$ go build -o primes primes.go && strip -s primes -o primes_no_symbols_from_strip
$ objdump -D primes_no_symbols_from_go_build > primes_no_symbols_from_go_build.human
$ objdump -D primes_no_symbols_from_strip > primes_no_symbols_from_strip.human
$ grep -rn "add" primes_no_symbols_from_go_build.human | wc -l
149568
$ grep -rn "add" primes_no_symbols_from_strip.human | wc -l
149556

是什么原因导致这些变化?(底层的Go程序可能与此无关,可以在这里找到)

英文:

Why does stripping symbols differ between strip and go build -ldflags "-s -w" (see here)?

$ go build -ldflags "-s -w" -o primes_no_symbols_from_go_build primes.go
$ go build -o primes primes.go && strip -s primes -o primes_no_symbols_from_strip
$ objdump -D primes_no_symbols_from_go_build > primes_no_symbols_from_go_build.human
$ objdump -D primes_no_symbols_from_strip > primes_no_symbols_from_strip.human
$ grep -rn "add" primes_no_symbols_from_go_build.human | wc -l
149568
$ grep -rn "add" primes_no_symbols_from_strip.human | wc -l
149556

what can account for these changes? (the underlying go program, probably irrelevant, is from here).

答案1

得分: 1

strip和ld工具是特定于操作系统的,来自于man strip的说明:

当在可执行文件上使用strip命令时,如果文件使用动态链接编辑器,strip会检查该文件。如果使用了动态链接编辑器,strip命令的效果与使用-u和-r选项相同。如果文件不使用动态链接编辑器,则strip没有任何选项的效果与使用ld(1)的-s选项相同。选项-S、-x和-X具有与ld(1)选项相同的效果。strip(1)的选项可以组合使用,以将符号表修剪为所需的内容。

即大多数ld没有-w选项,这意味着在OSX上“抑制所有警告消息”。

在Linux/Debian上:

1187840 Jul  7 09:05 primes_no_symbols_from_go_build_s
1187840 Jul  7 09:05 primes_no_symbols_from_go_build_s_w
1755360 Jul  7 09:05 primes_simple_go_build
1187004 Jul  7 09:06 primes_simple_go_build_strip

在OSX上:

2076146 Jul  7 16:02 primes_no_symbols_from_go_build_s
1580786 Jul  7 16:02 primes_no_symbols_from_go_build_s_w
2076146 Jul  7 16:02 primes_simple_go_build
1997872 Jul  7 16:04 primes_simple_go_build_strip
英文:

strip and ld tools are OS specific, from

man strip

> When strip is used with no options on an executable file, it checks
> that file to see
> if it uses the dynamic link editor. If it does, the effect of the strip command is
> the same as using the -u and -r options. If the file does not use the dynamic link
> editor, the effect of strip without any options is the same as using the -s option of
> ld(1). The options -S, -x, and -X have the same effect as the ld(1) options. The
> options to strip(1) can be combined to trim the symbol table to just what is desired.

i.e. most ld's don't have -w, which means on OSX "suppress all warning messages"

on Linux/Debian

1187840 Jul  7 09:05 primes_no_symbols_from_go_build_s
1187840 Jul  7 09:05 primes_no_symbols_from_go_build_s_w
1755360 Jul  7 09:05 primes_simple_go_build
1187004 Jul  7 09:06 primes_simple_go_build_strip

on OSX

2076146 Jul  7 16:02 primes_no_symbols_from_go_build_s
1580786 Jul  7 16:02 primes_no_symbols_from_go_build_s_w
2076146 Jul  7 16:02 primes_simple_go_build
1997872 Jul  7 16:04 primes_simple_go_build_strip

huangapple
  • 本文由 发表于 2021年7月7日 16:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/68282531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定