dongze8698 2017-05-24 03:28
浏览 570
已采纳

如何检查golang二进制文件是否使用--ldflags =“-s -w”进行编译

i know --ldflags="-s -w" will make Go binary size smaller, but without comparing to the one without ldflags, how do we know whether Go binary compiled with or without ldflags="-s -w" ?

  • 写回答

1条回答 默认 最新

  • dongzhang4301 2017-05-24 21:40
    关注

    Disclaimer first: I'm no expert of compilation toolchains and executable file formats. I'll try not to say stupid things, but correct me if you see any mistake !

    I ran those tests on an ArchLinux laptop, with x86_64 architecture. Go version is 1.8.1.

    First, we need to know where those flags are actually used:

    $ go help build
    ...
    -ldflags 'flag list'
        arguments to pass on each go tool link invocation
    ...
    

    Apparently, flags are merely passed to the go tool link invocation. Looking at the help, we have a bit more information:

    $ go tool link
    ...
    -s  disable symbol table
    ...
    -w  disable DWARF generation
    ...
    

    Taken from the short introduction to ELF that I found here, here is the header text concerning the Symbol Table:

    An object file’s symbol table holds information needed to locate and relocate a program’s symbolic definitions and references.

    As for DWARF, this is a format for debugging data.

    From there, how do we know if a binary has a symbol table, or DWARF enabled ? The answer lies in ELF sections. There are probably other ways, but it is the one I found and is easy to check for. Here are the rules that I've used to determine if a Golang binary has been compiled with -ldflags='-s' or -ldflags='-w':

    • If a Go binary has a symbol table, the section .symtab is present
    • If a Go binary has DWARF debug, the section .debug_info is present. This section is not the only one to be present, but serves as an indicator.

    On Linux, there are some tools which can read section names to extract those informations. readelf and nm are examples, yet probably more exist.

    It also turns out that Go provide a debug/elf package which can be used to get those informations. Here is a sample program that does this job:

    package main
    
    import "fmt"
    import "os"
    import "debug/elf"
    
    func main() {
        fileName := "path/to/main"
        fp, err := elf.Open(fileName)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    
        symtab := fp.Section(".symtab")
        if symtab == nil {
            fmt.Println("No Symbol Table : compiled with -ldflags='-s'")
        }
    
        debugInfo := fp.Section(".debug_info")
        if debugInfo == nil {
            fmt.Println("No DWARF data : compiled with -ldflags='-w'")
        }
    }
    

    I tested this program against the basic Golang Hello World, using no flags, only the -s flag, only the -w flag and both -s -w flags. I noted that while compiling with -s only, it also removed DWARF data, but I did not search why. Aside from that, the results were as expected.

    Though ELF was the focus format, the same method can be applied for Windows executables (there is a debug/pe package in Golang).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 我在一个购物网站的排队系统排队,这个排队到号后重新定向到目标网站进行购物,但是有技术牛通过技术方法直接跳过排队系统进入目标网址购物,有没有什么软件或者脚本可以用
  • ¥15 ios可以实现ymodem-1k协议 1024字节传输吗?
  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)