Golang源码分析 | 程序引导过程

环境说明

CentOS Linux release 7.2 (Final)
go version go1.16.3 linux/amd64
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7

使用gdb查看程序入口

编写一个简单的go程序

// main.go
package main

func main() {
        print("Hello world")
}
编译
  go build -gcflags "-N -l" -o simple main.go
使用gdb查看entry
gdb simple
(gdb) info files
Symbols from "/data/project/windeal/golang/simple/simple".
Local exec file:
        `/data/project/windeal/golang/simple/simple', file type elf64-x86-64.
        Entry point: 0x45cd80
        0x0000000000401000 - 0x000000000045ecb6 is .text
        0x000000000045f000 - 0x000000000048bdb5 is .rodata
        0x000000000048bf40 - 0x000000000048c3e0 is .typelink
        0x000000000048c3e0 - 0x000000000048c3e8 is .itablink
        0x000000000048c3e8 - 0x000000000048c3e8 is .gosymtab
        0x000000000048c400 - 0x00000000004c7b68 is .gopclntab
        0x00000000004c8000 - 0x00000000004c8020 is .go.buildinfo
        0x00000000004c8020 - 0x00000000004c9240 is .noptrdata
        0x00000000004c9240 - 0x00000000004cb3f0 is .data
        0x00000000004cb400 - 0x00000000004f86b0 is .bss
        0x00000000004f86c0 - 0x00000000004fd990 is .noptrbss
        0x0000000000400f9c - 0x0000000000401000 is .note.go.buildid
(gdb) 

可以看到程序的Entry point为 0x45cd80, 对应分段的地址范围,可以算出来程序0x45cd80在.text段。
添加断点,可以看到 Entry point: 0x45cd80 对应的内容

(gdb) b *0x45cd80
Breakpoint 1 at 0x45cd80: file /data/opt/go/src/runtime/rt0_linux_amd64.s, line 8.
(gdb) 

可以得出这个go程序的入口在 file /data/opt/go/src/runtime/rt0_linux_amd64.s, line 8.

在gdb中通过

  • b-设置断点,
  • run-启动程序,
  • n-逐步执行

可以看到程序的引导过程

rt0_linux_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_linux(SB),NOSPLIT,$-8
	JMP	_rt0_amd64(SB)

TEXT _rt0_amd64_linux_lib(SB),NOSPLIT,$0
	JMP	_rt0_amd64_lib(SB)

可以看到这部分没有太多内容,程序直接跳转执行到全局符号 _rt0_amd64(SB)

_rt0_amd64:_rt0_amd64

// _rt0_amd64 is common startup code for most amd64 systems when using
// internal linking. This is the entry point for the program from the
// kernel for an ordinary -buildmode=exe program. The stack holds the
// number of arguments and the C-style argv.
TEXT _rt0_amd64(SB),NOSPLIT,$-8
	MOVQ	0(SP), DI	// argc
	LEAQ	8(SP), SI	// argv
	JMP	runtime·rt0_go(SB)

这段代码把参数个数argc复制到DI寄存器。把参数值地址argv拷贝到SI寄存器。
关联知识:
我们分析的是amd64的源码,汇编指令按64bit寻址,每次操作8个字节的数据。 这里使用的汇编指令都带一个Q表示操作的是8个字节,如果是32bit则指定为MOVL、LEAL等,表示操作4个字节)
这里有个问题,就是为什么起始时0(SP)和8(SP)是argc和argv。 这里看了一些文章结合自己的理解,应该是操作系统的约定(需要进一步确认,留个坑后续补充)

_rt0_amd64:rt0_go

rt0_go 内容比较多,比较复杂, 逐段分析。

命令行参数拷贝

// asm_amd64.s

// Defined as ABIInternal since it does not use the stack-based Go ABI (and
// in addition there are no calls to this entry point from Go code).
TEXT runtime·rt0_go<ABIInternal>(SB),NOSPLIT,$0
	// copy arguments forward on an even stack
	MOVQ	DI, AX		// argc,
	MOVQ	SI, BX		// argv
	SUBQ	$(4*8+7), SP		// 2args 2auto
	ANDQ	$~15, SP	// 最后16位清0,实现16字节对齐
	MOVQ	AX, 16(SP)
	MOVQ	BX, 24(SP)

	// ......  

这一段代码是做命令行参数的拷贝和栈顶指针SP偏移的。

前面两行是把argc、argv拷贝到寄存器AX、BX。
然后SP指针向下移动4*8+7个字节,预留空间用来存放命令行参数

栈空间的寻址是自高地址向低地址

我们看下这个4*8+7的值是怎么来的。实际上是2*8+2*8+7
引导程序先把argc和argv下移,即第一个2*8。即最终的SP+16和SP+4,
第二个2*8字节,在这里并未填充值,它是用来后面给G0传递参数的,让G0启动向一个普通的调用一样。
SP+0和SP+8 可以在rt0_go的后面部分看到赋值

TEXT runtime·rt0_go<ABIInternal>(SB),NOSPLIT,$0
	......
ok:
	......
	MOVL	16(SP), AX		// copy argc
	MOVL	AX, 0(SP)
	MOVQ	24(SP), AX		// copy argv
	MOVQ	AX, 8(SP)
	......

多偏移的7字节是哪里来的,还没有搞懂。看到很多材料写的是为了后面的16字节对齐,但是如果仅仅只是为了16字节对齐,后面的ANDQ $~15, SP看起来就已经足够了。 先留个坑,后面搞懂了回来补充。

关于16字节对齐
关联知识:CPU有一组SSE指令,这些指令中出现的内存地址必须是16的倍数。
在 SUBQ $(4*8+7), SP之前,因为64bit机器的寻址是8字节为单元, SP对应的内存地址有2中可能:

  • 0x*****0: 最后一位是0,本身是16字节对齐
  • 0x*****8: 最后一位是8,不是16字节对齐。

如果是0x*****0这种情况,那么4*8本身就是16字节对齐的,不需要额外操作。单是如果是0x*****8这种情况的话,就需要做16字节对齐。

G0执行栈初步初始化

继续往下分析

TEXT runtime·rt0_go<ABIInternal>(SB),NOSPLIT,$0
	......
	// create istack out of the given (operating system) stack.
	// _cgo_init may update stackguard.
	MOVQ	$runtime·g0(SB), DI			// DI = g0
	LEAQ	(-64*1024+104)(SP), BX	   
	MOVQ	BX, g_stackguard0(DI)		// g0.stackguard0 = SP + (-64*1024+104)
	MOVQ	BX, g_stackguard1(DI)		// g0.stackguard1 = SP + (-64*1024+104)
	MOVQ	BX, (g_stack+stack_lo)(DI) // g0.stack.stack_lo = SP + (-64*1024+104)
	MOVQ	SP, (g_stack+stack_hi)(DI) // g0.stack.stack_hi = SP + (-64*1024+104)

    // find out information about the processor we're on,确定CPU处理器信息
    MOVL	$0, AX
    CPUID
    MOVL	AX, SI
    CMPL	AX, $0
    JE	nocpuinfo

这一部分是初始化g0的执行栈。
参考结构体g的定义:https://github.com/golang/go/blob/9baddd3f21230c55f0ad2a10f5f20579dcf0a0bb/src/runtime/runtime2.go#L404

TLS 线程本地存储

代码链接

	LEAQ	runtime·m0+m_tls(SB), DI	// DI = m0.tls, 
	CALL	runtime·settls(SB)				 // 设置TLS, 还没完全看懂,待进一步分析

	// store through it, to make sure it works
	get_tls(BX)
	MOVQ	$0x123, g(BX)
	MOVQ	runtime·m0+m_tls(SB), AX
	CMPQ	AX, $0x123				// 判断 TLS 是否设置成功
	JEQ 2(PC)							// 如果相等则向后跳转两条指令
	CALL	runtime·abort(SB)	  // 使用 INT 指令执行中断
ok:

关联g0和m0

代码链接

	// set the per-goroutine and per-mach "registers"
	// g0和m0是全局变量,先获取他们的地址分别存在寄存器CX和AX
	get_tls(BX)
	LEAQ	runtime·g0(SB), CX
	MOVQ	CX, g(BX)
	LEAQ	runtime·m0(SB), AX

	// 关联g0和m0	
	// save m->g0 = g0
	MOVQ	CX, m_g0(AX)
	// save m0 to g0->m
	MOVQ	AX, g_m(CX)

运行时检查

	CLD				// convention is D is always left cleared
	CALL	runtime·check(SB)

runtime·check(SB)的代码链接, check会进行各种检查,如果检查未通过,直接抛出异常,一般是编译过程发生了错误。
系统级的初始化
代码链接

	MOVL	16(SP), AX		// copy argc
	MOVL	AX, 0(SP)
	MOVQ	24(SP), AX		// copy argv
	MOVQ	AX, 8(SP)
	CALL	runtime·args(SB)	// 参数的初始化
	CALL	runtime·osinit(SB)	// 
	CALL	runtime·schedinit(SB)

前面四行是做argc和argv的再一次拷贝。(这里没搞懂为什么需要做多次的参数拷贝,看到一些解释是为了让g0模拟普通goroutine调用)

后面三行是3个函数调用

runtime.args

func args() 代码链接

func args(c int32, v **byte) {
	argc = c
	argv = v
	sysargs(c, v)	
}

把参数存放在全局变量argc和argv中,供其他初始化函数使用。
func sysargs()的代码链接
sysargs()用于将一些内核级别的信息存放到执行栈中(是放在主调的栈中)
对这方面感兴趣的可以搜索golang linux 函数调用栈相关的内容

runtime·osinit

代码链接 osinit()

func osinit() {
	ncpu = getproccount()								// 获取CPU核心数
	physHugePageSize = getHugePageSize()	  // 获取内存物理页代销
	......
	osArchInit()	//  目前看是个空函数
}

运行时组件初始化

runtime·schedinit(SB) 开始是golang 运行时组件相关的初始化
代码链接

	CALL	runtime·schedinit(SB)

schedinit的代码链接

// The new G calls runtime·main.
func schedinit() {
	
  	// 各种加锁
  	......

	// raceinit must be the first call to race detector.
	// In particular, it must be done before mallocinit below calls racemapshadow.
	_g_ := getg()
	if raceenabled {
		_g_.racectx, raceprocctx0 = raceinit()
	}

	sched.maxmcount = 10000

	// The world starts stopped.
	worldStopped()

  	// 栈、内存分配器、调度器相关初始化
	moduledataverify()
	stackinit()			// 初始化执行栈
	mallocinit()		// 初始化内存分配器malloc
	fastrandinit() // must run before mcommoninit
	mcommoninit(_g_.m, -1)   // 初始化当前系统线程,只完成部分通用的初始化	
	cpuinit()       // must run before alginit
	alginit()       // maps must not be used before this call
	modulesinit()   // provides activeModules
	typelinksinit() // uses maps, activeModules
	itabsinit()     // uses activeModules

	sigsave(&_g_.m.sigmask)
	initSigmask = _g_.m.sigmask

	goargs()
	goenvs()
	parsedebugvars()
	gcinit()

  	// 创建 P, 通过 CPU 核心数和 GOMAXPROCS 环境变量确定 P 的数量
	lock(&sched.lock)
	sched.lastpoll = uint64(nanotime())
	procs := ncpu
	if n, ok := atoi32(gogetenv("GOMAXPROCS")); ok && n > 0 {
		procs = n
	}
	if procresize(procs) != nil {
		throw("unknown runnable goroutine during bootstrap")
	}
	unlock(&sched.lock)

	// World is effectively started now, as P's can run.
	worldStarted()

	// For cgocheck > 1, we turn on the write barrier at all times
	// and check all pointer writes. We can't do this until after
	// procresize because the write barrier needs a P.
	if debug.cgocheck > 1 {
		writeBarrier.cgo = true
		writeBarrier.enabled = true
		for _, p := range allp {
			p.wbBuf.reset()
		}
	}

	if buildVersion == "" {
		// Condition should never trigger. This code just serves
		// to ensure runtime·buildVersion is kept in the resulting binary.
		buildVersion = "unknown"
	}
	if len(modinfo) == 1 {
		// Condition should never trigger. This code just serves
		// to ensure runtime·modinfo is kept in the resulting binary.
		modinfo = ""
	}
}

主goroutine启动

代码链接

	// create a new goroutine to start program
	MOVQ	$runtime·mainPC(SB), AX		// entry, 主 goroutine 入口地址runtime.main
	PUSHQ	AX
	PUSHQ	$0			// arg size
	CALL	runtime·newproc(SB)		// 创建执行单元,创建g
	POPQ	AX
	POPQ	AX

	// start this M
	CALL	runtime·mstart(SB)			// 开始启动调度器的调度循环

	CALL	runtime·abort(SB)	// mstart should never return
	RET

	// Prevent dead-code elimination of debugCallV1, which is
	// intended to be called by debuggers.
	MOVQ	$runtime·debugCallV1<ABIInternal>(SB), AX
RET

newproc(SB)的代码链接, newproc 会创建一个g

func newproc(siz int32, fn *funcval) {
	argp := add(unsafe.Pointer(&fn), sys.PtrSize)
	gp := getg()
	pc := getcallerpc()
	systemstack(func() {
		newg := newproc1(fn, argp, siz, gp, pc)

		_p_ := getg().m.p.ptr()
		runqput(_p_, newg, true)

		if mainStarted {
			wakep()
		}
	})
}

mstart()

runtime·mstart 相对比较复杂,后面新开一篇文章介绍。
主要调用链路是

mstart()==>mstart1()==>schedule()

主要功能是启动调度器,在shedule()中进行循环调度

我的公众号

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值