Go编译生成的是一个可执行文件,因此可以使用相关的工具来查看可执行文件的程序入口
使用gobjdump查看程序入口
以生成的 helloworld 文件为例
由于使用的是mac,所以使用gobjdump来查看
1. 安装 gobjdump
brew update && brew install binutils
2. 找到程序的入口地址
执行 gobjdump -f helloworld 返回如下
helloworld: file format mach-o-x86-64
architecture: i386:x86-64, flags 0x00000012:
EXEC_P, HAS_SYMS
start address 0x000000000105c780
可以看到,程序的开始地址为 0x000000000105c780
3. 找到对应地址
执行 gobjdump -x helloworld | grep 000000000105c780
打印如下,即我们程序的入口是 _rt0_amd64_daewin
000000000105c780 l 0e SECT 01 0000 [.text] __rt0_amd64_darwin
4. 找到对应的go源码
从源码中 runtime 包中可以找到对应的文件 runtime/rt0_darwin_amd64.s
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
TEXT _rt0_amd64_darwin(SB),NOSPLIT,$-8
JMP _rt0_amd64(SB)
// When linking with -shared, this symbol is called when the shared library
// is loaded.
TEXT _rt0_amd64_darwin_lib(SB),NOSPLIT,$0
JMP _rt0_amd64_lib(SB)