How to check if the golang binary compiled with -ldflags = "- s -w"

i know --ldflags="-s -w"will make the Go binary file smaller, but without comparing it with ldflags, how do we know if Go is a binary compiled with or without ldflags="-s -w"?

+4
source share
1 answer

Disclaimer in the first place: I am not an expert in compilation toolkits and executable file formats. I will try not to say stupid things, but correct me if you see any mistake!

I ran these tests on an x86_64 ArchLinux laptop. Go version 1.8.1.

First, we need to know where these flags are used:

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

-, go tool link. , :

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

ELF, , , :

, .

DWARF, .

, , DWARF? ELF. , , , , , . , , Golang -ldflags='-s' -ldflags='-w':

  • Go , .symtab
  • Go DWARF, .debug_info. , , .

Linux , . readelf nm , , , .

, Go debug/elf, . , :

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'")
    }
}

Golang Hello World, , -s, -w -s -w. , -s DWARF, . , , .

ELF , Windows ( debug/pe).

+6

Source: https://habr.com/ru/post/1677809/


All Articles