github.com/eh-steve/goloader@v0.0.0-20240111193454-90ff3cfdae39/deferreturn.go (about)

     1  //go:build go1.14 && !go1.23
     2  // +build go1.14,!go1.23
     3  
     4  package goloader
     5  
     6  import (
     7  	"cmd/objfile/sys"
     8  	"fmt"
     9  
    10  	"github.com/eh-steve/goloader/objabi/dataindex"
    11  )
    12  
    13  func (linker *Linker) addDeferReturn(_func *_func) (err error) {
    14  	funcname := gostringnocopy(&linker.funcnametab[_func.nameoff])
    15  	Func := linker.symMap[funcname].Func
    16  	if Func != nil && len(Func.FuncData) > dataindex.FUNCDATA_OpenCodedDeferInfo {
    17  		sym := linker.symMap[funcname]
    18  		for _, r := range sym.Reloc {
    19  			if r.Sym.Name == RuntimeDeferReturn {
    20  				// ../cmd/link/internal/ld/pcln.go:pclntab
    21  				switch linker.Arch.Name {
    22  				case sys.Arch386.Name, sys.ArchAMD64.Name:
    23  					_func.deferreturn = uint32(r.Offset) - uint32(sym.Offset) - 1
    24  				case sys.ArchARM.Name, sys.ArchARM64.Name:
    25  					_func.deferreturn = uint32(r.Offset) - uint32(sym.Offset)
    26  				default:
    27  					err = fmt.Errorf("not support arch:%s", linker.Arch.Name)
    28  				}
    29  				break
    30  			}
    31  		}
    32  	}
    33  	return err
    34  }