関連記事
GitHub - devlights/blog-summary: ブログ「いろいろ備忘録日記」のまとめ · GitHub
概要
以下、自分用のメモです。忘れないうちにメモメモ。。。
Go 1.22 以降、ldflagsにて-sを指定するだけで-wも暗黙的に含むことになっていることを今更しりました。
なので、これまで
go build -ldflags="-s -w" main.go
のようにやっていたのですが、-ldflags="-s" の指定で良くなっていたのですね。知らなかったです。
Go 1.22のリリースノートにちゃんと書いてありました。。
The linker’s -s and -w flags are now behave more consistently across all platforms. The -w flag suppresses DWARF debug information generation. The -s flag suppresses symbol table generation. The -s flag also implies the -w flag, which can be negated with -w=0. That is, -s -w=0 will generate a binary with DWARF debug information generation but without the symbol table.
試してみる
main.go
package main import "fmt" func main() { // 処理に特に意味は無い var ( p = func() <-chan int { out := make(chan int) go func() { defer close(out) for i := range 5 { out <- i } }() return out } c = func(in <-chan int) <-chan bool { out := make(chan bool) go func() { defer close(out) for v := range in { fmt.Println(v) } }() return out } ) <-c(p()) }
Taskfile.yml
# yaml-language-server: $schema=https://taskfile.dev/schema.json version: '3' tasks: default: cmds: - task: build - task: verify build: cmds: - go build -o app0 main.go - go build -ldflags="-s" -o app1 main.go - go build -ldflags="-s -w=0" -o app2 main.go verify: cmds: - file app0 app1 app2 - readelf -S app0 | grep -E '\.debug|\.symtab|\.strtab' - readelf -S app1 | grep -E '\.debug|\.symtab|\.strtab' - readelf -S app2 | grep -E '\.debug|\.symtab|\.strtab' ignore_error: true
実行結果
$ task task: [build] go build -o app0 main.go task: [build] go build -ldflags="-s" -o app1 main.go task: [build] go build -ldflags="-s -w=0" -o app2 main.go task: [verify] file app0 app1 app2 app0: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, ..., with debug_info, not stripped app1: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, ..., stripped app2: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, ..., with debug_info, not stripped task: [verify] readelf -S app0 | grep -E '\.debug|\.symtab|\.strtab' [15] .debug_abbrev PROGBITS 0000000000000000 00180000 [16] .debug_line PROGBITS 0000000000000000 00180160 [17] .debug_frame PROGBITS 0000000000000000 001a1ad7 [18] .debug_gdb_s[...] PROGBITS 0000000000000000 001a8c29 [19] .debug_info PROGBITS 0000000000000000 001a8c59 [20] .debug_loclists PROGBITS 0000000000000000 001f1d60 [21] .debug_rnglists PROGBITS 0000000000000000 0020bdfe [22] .debug_addr PROGBITS 0000000000000000 0021a4ce [23] .symtab SYMTAB 0000000000000000 0021b548 [24] .strtab STRTAB 0000000000000000 00229a20 task: [verify] readelf -S app1 | grep -E '\.debug|\.symtab|\.strtab' task: [verify] readelf -S app2 | grep -E '\.debug|\.symtab|\.strtab' [15] .debug_abbrev PROGBITS 0000000000000000 00180000 [16] .debug_line PROGBITS 0000000000000000 00180160 [17] .debug_frame PROGBITS 0000000000000000 001a1ad7 [18] .debug_gdb_s[...] PROGBITS 0000000000000000 001a8c29 [19] .debug_info PROGBITS 0000000000000000 001a8c59 [20] .debug_loclists PROGBITS 0000000000000000 001f1d60 [21] .debug_rnglists PROGBITS 0000000000000000 0020bdfe [22] .debug_addr PROGBITS 0000000000000000 0021a4ce
app0は通常通りなので割愛。
app1は想定通りstrippedと表示されていますね。なので、シンボルテーブル無しでDWARFも無し。ちゃんと-s -wの状態になってる。
app2の方は、想定ではシンボルテーブル無しでDWARF有りとなるはずですが、not strippedと表示されています。
これは、fileコマンドがDWARFが存在しているので、このような表示になっています。
readelfコマンドで各テーブルの情報を確認してみると、app2の方は想定通りsymtabとstrtabは存在せずdebugのみとなっています。
なので、シンボルテーブル無しでDWARF有りの状態になっていますね。
参考情報
個人的Goのおすすめ書籍
個人的に読んでとても勉強になった書籍さんたちです。
過去の記事については、以下のページからご参照下さい。
サンプルコードは、以下の場所で公開しています。