github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/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/pkujhd/goloader/objabi/dataindex"
    11  )
    12  
    13  func (linker *Linker) addDeferReturn(_func *_func, module *moduledata) (err error) {
    14  	funcname := getfuncname(_func, module)
    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.SymName == 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  }