Delete file paths from TEXT directives in go binaries

I want to delete all the path information, for example, /Users/myuser/dev/go/src/fooapi/spikes/mongoapi.gofrom an executable file that I created with go build.

I compile the code as follows:

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

Some part of the build example 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
.
.

Please note that: it is stripnot recommended and may lead to corruption of executable files if you intend to recommend it as a solution.

+9
source share
2 answers

Use the -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

Additional Information:

-trimpath -gcflags -asmflags -asmflags .

$ 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

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 , , . , . .

+6

trimpath - , , go, 24976.

, -trimpath -trimpath ,

; , , trimpath , .

CL 173344 ( Go 1.13)

cmd/internal/objabi: -trimpath

CL -trimpath cmd/asm cmd/compile.

, .

CL .

, =>replacement " =>replacement ", , , , .

CL cmd/go -trimpath , .

CL 173345:

cmd/go: -trimpath

" go build -trimpath " , .
, , " go/src/... " ( ) , .

16860, Go , .

0

All Articles