本文介绍了在二进制文件中删除TEXT指令中的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要删除所有路径信息,例如 / code>和 -asmflags 将删除elf二进制文件中的任何路径信息。

  $ go工具asm -help 2>& 1 | grep -A1 trimpath 
-trimpath字符串
从记录的源文件路径中删除前缀

$ go工具编译-help | grep -A1 trimpath
-trimpath字符串
从记录的源文件路径中删除前缀

您可以使用 go tool objdump :

  $ go tool objdump ./fooapi 


TEXT main.init(SB)api / spikes / mongoapi.go
mongoapi.go:60 0x12768c0 65488b0c25a0080000 GS MOVQ GS:0x8a0,CX
mongoapi.go:60 0x12768c9 483b6110 CMPQ 0x10 CX),SP
mongoapi.go:60 0x12768cd 7663 JBE 0x1276932







使用 strip 工具在社区中仍然存在一些争议,尽管据说它已被修复。有人说有时会出现未知和不可预知的错误。请阅读和举例。


I want to remove all path information like /Users/myuser/dev/go/src/fooapi/spikes/mongoapi.go from the executable that I created with go build.

I'm compiling the code like this:

CGO_ENABLED=0 go build -v -a -ldflags="-w -s" -o ./fooapi spikes/mongoapi.go

Some part of the example assembly from the go build command above:

$ go tool objdump ./fooapi
.
.
TEXT main.init(SB) /Users/myuser/dev/go/src/api/spikes/mongoapi.go
mongoapi.go:60  0x12768c0   65488b0c25a0080000  GS MOVQ GS:0x8a0, CX
mongoapi.go:60  0x12768c9   483b6110        CMPQ 0x10(CX), SP
mongoapi.go:60  0x12768cd   7663            JBE 0x1276932
.
.


Note that: strip is not recommended and can lead to broken executables if you're going to recommend it as a solution.

解决方案

Use -trimpath flags to remove path information:

CGO_ENABLED=0 go build -v -a -ldflags="-w -s" \
    -gcflags=-trimpath=/Users/myuser/dev/go/src \
    -asmflags=-trimpath=/Users/myuser/dev/go/src \
    -o ./fooapi spikes/mongoapi.go


More Information:

Passing -trimpath to -gcflags and -asmflags will remove any path information from the elf binary.

$ go tool asm -help 2>&1 | grep -A1 trimpath
-trimpath string
    remove prefix from recorded source file paths

$ go tool compile -help|grep -A1 trimpath
-trimpath string
    remove prefix from recorded source file paths

You can check the result with go tool objdump:

$ go tool objdump ./fooapi
.
.
TEXT main.init(SB) api/spikes/mongoapi.go
mongoapi.go:60  0x12768c0   65488b0c25a0080000  GS MOVQ GS:0x8a0, CX
mongoapi.go:60  0x12768c9   483b6110        CMPQ 0x10(CX), SP
mongoapi.go:60  0x12768cd   7663            JBE 0x1276932
.
.


Using strip tool has still some controversies in go community, although it's been said that it's been fixed. Some say that unknown and unpredictable bugs occur sometimes. Read here and here for examples.

这篇关于在二进制文件中删除TEXT指令中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:55