github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/init.1.13.go (about)

     1  //go:build go1.13 && !go1.21
     2  // +build go1.13,!go1.21
     3  
     4  package goloader
     5  
     6  import (
     7  	"unsafe"
     8  
     9  	"github.com/pkujhd/goloader/obj"
    10  )
    11  
    12  const (
    13  	_InitTaskSuffix = "..inittask"
    14  )
    15  
    16  func getInitFuncName(packagename string) string {
    17  	return obj.PathToPrefix(packagename) + _InitTaskSuffix
    18  }
    19  
    20  //go:linkname doInit runtime.doInit
    21  func doInit(t unsafe.Pointer) // t should be a *runtime.initTask
    22  
    23  func (linker *Linker) doInitialize(symPtr, symbolMap map[string]uintptr) error {
    24  	for _, pkg := range linker.Packages {
    25  		name := getInitFuncName(pkg.PkgPath)
    26  		if funcPtr, ok := symbolMap[name]; ok {
    27  			doInit(adduintptr(funcPtr, 0))
    28  		}
    29  	}
    30  	return nil
    31  }