少し試したものの、忘れそうなのでメモ書きとして残します。
サンプルコード
package main import "fmt" func main() { fmt.Println("hello world") }
オブジェクトファイルの生成
-S
が1つだと標準出力のみです。2つだと.o
ファイルが生成されます。
$ go tool compile -S -S main.go "".main STEXT size=120 args=0x0 locals=0x48 0x0000 00000 (main.go:5) TEXT "".main(SB), $72-0 (中略) 0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 76 62 48 eH..%....H;a.vbH 0x0010 83 ec 48 48 89 6c 24 40 48 8d 6c 24 40 48 c7 44 ..HH.l$@H.l$@H.D 0x0020 24 30 00 00 00 00 48 c7 44 24 38 00 00 00 00 48 $0....H.D$8....H 0x0030 8d 05 00 00 00 00 48 89 44 24 30 48 8d 05 00 00 ......H.D$0H.... 0x0040 00 00 48 89 44 24 38 48 8d 44 24 30 48 89 04 24 ..H.D$8H.D$0H..$ 0x0050 48 c7 44 24 08 01 00 00 00 48 c7 44 24 10 01 00 H.D$.....H.D$... 0x0060 00 00 e8 00 00 00 00 48 8b 6c 24 40 48 83 c4 48 .......H.l$@H..H 0x0070 c3 e8 00 00 00 00 eb 88 ........ rel 5+4 t=16 TLS+0 rel 50+4 t=15 type.string+0 rel 62+4 t=15 "".statictmp_0+0 rel 99+4 t=8 fmt.Println+0 rel 114+4 t=8 runtime.morestack_noctxt+0 (中略) type..importpath.fmt. SRODATA dupok size=6 0x0000 00 00 03 66 6d 74 ...fmt gclocals·69c1753bd5f81501d95132d08af04464 SRODATA dupok size=8 0x0000 02 00 00 00 00 00 00 00 ........ gclocals·e226d4ae4a7cad8835311c6a4683c14f SRODATA dupok size=10 0x0000 02 00 00 00 02 00 00 00 00 03 .......... gclocals·33cdeccccebe80329f1fdbee7f5874cb SRODATA dupok size=8 0x0000 01 00 00 00 00 00 00 00 ........
オブジェクトファイルから実行コードを生成
$ go tool link main.o $ ls a.out main.go main.o $ ./a.out hello world
オブジェクトファイルに定義されているシンボルの表示
$ go tool nm main.o U 44c T %22%22.init 517 B %22%22.initdone· 3b5 T %22%22.main 507 R %22%22.statictmp_0 U fmt.Println U fmt.init 6de R gclocals·33cdeccccebe80329f1fdbee7f5874cb 6cc R gclocals·69c1753bd5f81501d95132d08af04464 6d4 R gclocals·e226d4ae4a7cad8835311c6a4683c14f 4ea ? go.info.%22%22.init 4cd ? go.info.%22%22.main 507 ? go.range.%22%22.init 4ea ? go.range.%22%22.main 4c2 R go.string."hello world" U runtime.algarray 517 R runtime.gcbits.01 560 R runtime.gcbits.03 U runtime.morestack_noctxt U runtime.throwinit 646 R type.*[1]interface {} 5c3 R type.*[]interface {} 528 R type.*interface {} 6c6 R type..importpath.fmt. 633 R type..namedata.*[1]interface {}- 5b1 R type..namedata.*[]interface {}- 518 R type..namedata.*interface {}- 67e R type.[1]interface {} 5fb R type.[]interface {} 561 R type.interface {} U type.string
実行コードの逆アセンブリ
$ go tool objdump a.out (略)