最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

golang 如何刪除二進(jìn)制文件中的源碼路徑信息

 更新時間:2021年04月30日 09:39:20   作者:ishouyong  
這篇文章主要介紹了golang 如何刪除二進(jìn)制文件中的源碼路徑信息,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

方法

go v1.13 go build 新增 -trimpath參數(shù),不用以前那么麻煩了。

➜  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_word
TEXT go.buildid(SB)
.....
TEXT main.main(SB) /Users/xxxx/go/src/awesomeProject/hello_word.go
  hello_word.go:3	0x104e580		65488b0c2530000000	MOVQ GS:0x30, CX
  hello_word.go:3	0x104e589		483b6110		CMPQ 0x10(CX), SP
  hello_word.go:3	0x104e58d		763b			JBE 0x104e5ca
  hello_word.go:3	0x104e58f		4883ec18		SUBQ $0x18, SP
  hello_word.go:3	0x104e593		48896c2410		MOVQ BP, 0x10(SP)
  hello_word.go:3	0x104e598		488d6c2410		LEAQ 0x10(SP), BP
  hello_word.go:4	0x104e59d		e89e50fdff		CALL runtime.printlock(SB)
  hello_word.go:4	0x104e5a2		488d059eef0100		LEAQ go.string.*+2759(SB), AX
  hello_word.go:4	0x104e5a9		48890424		MOVQ AX, 0(SP)
  hello_word.go:4	0x104e5ad		48c74424080d000000	MOVQ $0xd, 0x8(SP)
  hello_word.go:4	0x104e5b6		e8b559fdff		CALL runtime.printstring(SB)
  hello_word.go:4	0x104e5bb		e80051fdff		CALL runtime.printunlock(SB)
  hello_word.go:5	0x104e5c0		488b6c2410		MOVQ 0x10(SP), BP
  hello_word.go:5	0x104e5c5		4883c418		ADDQ $0x18, SP
  hello_word.go:5	0x104e5c9		c3			RET
  hello_word.go:3	0x104e5ca		e8f184ffff		CALL runtime.morestack_noctxt(SB)
  hello_word.go:3	0x104e5cf		ebaf			JMP main.main(SB)
  :-1			0x104e5d1		cc			INT $0x3
  :-1			0x104e5d2		cc			INT $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.go
runtime/internal/sys
runtime/internal/atomic
internal/cpu
runtime/internal/math
internal/bytealg
runtime
command-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.go
runtime/internal/sys
runtime/internal/atomic
internal/cpu
runtime/internal/math
internal/bytealg
runtime
command-line-arguments
➜  awesomeProject
# 效果
➜  awesomeProject go tool objdump hello_word
TEXT go.buildid(SB)
.....
TEXT main.main(SB) awesomeProject/hello_word.go
  hello_word.go:3	0x104e580		65488b0c2530000000	MOVQ GS:0x30, CX
  hello_word.go:3	0x104e589		483b6110		CMPQ 0x10(CX), SP
  hello_word.go:3	0x104e58d		763b			JBE 0x104e5ca
  hello_word.go:3	0x104e58f		4883ec18		SUBQ $0x18, SP
  hello_word.go:3	0x104e593		48896c2410		MOVQ BP, 0x10(SP)
  hello_word.go:3	0x104e598		488d6c2410		LEAQ 0x10(SP), BP
  hello_word.go:4	0x104e59d		e89e50fdff		CALL runtime.printlock(SB)
  hello_word.go:4	0x104e5a2		488d059eef0100		LEAQ go.string.*+2759(SB), AX
  hello_word.go:4	0x104e5a9		48890424		MOVQ AX, 0(SP)
  hello_word.go:4	0x104e5ad		48c74424080d000000	MOVQ $0xd, 0x8(SP)
  hello_word.go:4	0x104e5b6		e8b559fdff		CALL runtime.printstring(SB)
  hello_word.go:4	0x104e5bb		e80051fdff		CALL runtime.printunlock(SB)
  hello_word.go:5	0x104e5c0		488b6c2410		MOVQ 0x10(SP), BP
  hello_word.go:5	0x104e5c5		4883c418		ADDQ $0x18, SP
  hello_word.go:5	0x104e5c9		c3			RET
  hello_word.go:3	0x104e5ca		e8f184ffff		CALL runtime.morestack_noctxt(SB)
  hello_word.go:3	0x104e5cf		ebaf			JMP main.main(SB)

trimpath說明

-trimpath prefix
 Remove prefix from recorded source file paths.

補(bǔ)充:Go 編譯時去除 bin 文件中的編譯路徑 GOPATH 信息

問題原因

當(dāng) golang 程序 panic,或者通過 runtime.Caller(0) 獲取當(dāng)前出錯的文件位置作為日志記錄時,會暴露程序編譯機(jī)器上的項目路徑、以及賬戶,不如下面這些信息, 這些信息我們并不想讓對方看到。

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

問題現(xiàn)象

當(dāng)我們通過 strings panic_demo | grep /Users 靜態(tài)分析golang 編譯后的二進(jìn)制就可以得到完整的源碼路徑信息:

/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

這些信息我們可能并不想讓對方知道, 所以我們需要對這些信息進(jìn)行處理, 剔除這些信息。

解決方式

在編譯是通過傳入以下參數(shù)來剔除

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • Go語言變量初始化的實(shí)現(xiàn)示例

    Go語言變量初始化的實(shí)現(xiàn)示例

    在Go語言中,變量的初始化是編寫程序時經(jīng)常遇到的重要操作之一,本文主要介紹了Go語言變量初始化的實(shí)現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-04-04
  • Golang算法問題之整數(shù)拆分實(shí)現(xiàn)方法分析

    Golang算法問題之整數(shù)拆分實(shí)現(xiàn)方法分析

    這篇文章主要介紹了Golang算法問題之整數(shù)拆分實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Go語言數(shù)值運(yùn)算與數(shù)組遍歷相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • GoLang中的加密方法小結(jié)

    GoLang中的加密方法小結(jié)

    這篇文章主要介紹了GoLang中的加密方法。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 重學(xué)Go語言之變量與常量的聲明與使用詳解

    重學(xué)Go語言之變量與常量的聲明與使用詳解

    變量、常量的聲明與使用是掌握一門編程語言的基礎(chǔ),在這篇文章中,小編就來帶大家學(xué)習(xí)一下Go語言是怎么樣聲明和使用變量與常量吧
    2023-03-03
  • go語言LeetCode題解1030距離順序排列矩陣單元格

    go語言LeetCode題解1030距離順序排列矩陣單元格

    這篇文章主要為大家介紹了go語言LeetCode題解1030距離順序排列矩陣單元格,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • go程序部署到linux上運(yùn)行的實(shí)現(xiàn)方法

    go程序部署到linux上運(yùn)行的實(shí)現(xiàn)方法

    本文主要介紹了go程序部署到linux上運(yùn)行的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • go嵌套匿名結(jié)構(gòu)體的初始化詳解

    go嵌套匿名結(jié)構(gòu)體的初始化詳解

    這篇文章主要介紹了go嵌套匿名結(jié)構(gòu)體的初始化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Golang之如何讀取文件內(nèi)容

    Golang之如何讀取文件內(nèi)容

    這篇文章主要介紹了Golang之如何讀取文件內(nèi)容問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Go語言應(yīng)該什么情況使用指針

    Go語言應(yīng)該什么情況使用指針

    go語言的指針類型和C/C++的指針類型用法是一樣的,那么Go語言應(yīng)該什么情況使用指針,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2021-07-07
  • go nil處理如何正確返回nil的error

    go nil處理如何正確返回nil的error

    這篇文章主要為大家介紹了go中的nil處理,如何正確返回nil的error實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10

最新評論

基隆市| 尉犁县| 陇西县| 凌源市| 彰武县| 壶关县| 汽车| 体育| 泾源县| 楚雄市| 南充市| 徐闻县| 布拖县| 日照市| 德惠市| 华阴市| 福泉市| 瑞金市| 体育| 隆子县| 香格里拉县| 施甸县| 江津市| 建湖县| 西吉县| 滕州市| 扎鲁特旗| 崇信县| 西充县| 漳州市| 迁西县| 随州市| 达日县| 于都县| 自贡市| 榆中县| 交城县| 武汉市| 敦煌市| 环江| 鹤壁市|