doukun1450 2017-07-24 11:25
浏览 89
已采纳

从go二进制文件中的TEXT指令中删除文件路径

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.

  • 写回答

2条回答 默认 最新

  • dreamMyDream2014 2017-07-25 11:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建