package main
import "unsafe"
// @Time    : 2020-10-28 18:49
// @Author  : mu
// @File    : sp_address.go
func funAddr(i int) (v int) {
   bp := (*int64)((unsafe.Pointer)(uintptr(unsafe.Pointer(&i)) - uintptr(16)))
   println("bp", unsafe.Pointer(uintptr(*bp)))
   fun := (*int64)((unsafe.Pointer)(uintptr(unsafe.Pointer(&i)) - uintptr(8)))
   println("fun", *fun)
   t2 := unsafe.Pointer(&i)
   println(t2)
   b := funAddB
 c := *(*int64)(unsafe.Pointer(&b))
   d := (*int64)(unsafe.Pointer(uintptr(c)))
   println("d", *d)
   println("diff", *fun - *d)
   println("funAddB", funAddB)
   return
}
func funAddB(i int) {
   j := "sasa"
 b := "sasa"
 c := "sasa"
 i = 10
 i = 111
 _, _, _ = c, b ,j
 funAddr(12121)
   //t2 := unsafe.Pointer(&i)
 //println("funAddB", t2)}
func main() {
   funAddB(1)
}

运行结果

bp 0xc000036770
fun 17099834
fun 0x104ec3a
0xc000036760
d 17099792
diff 42
funAddB 0x10715d8

diff是42, 正好是funAddB 函数调用funAddr的汇编代码的差,
所以golang函数调用是栈(sp)会push一个指针,指针指向原函数被调用的位置。


xxx小M
30 声望11 粉丝

暂时放一些读书笔记, 很多内容没有整理好