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

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