github.com/szmcdull/go-forceexport@v0.0.0-20230908151957-3dac42f564da/go_1_16.go (about)

     1  //go:build go1.16 && !go1.18
     2  // +build go1.16,!go1.18
     3  
     4  package forceexport
     5  
     6  import (
     7  	"runtime"
     8  	"unsafe"
     9  )
    10  
    11  type (
    12  	functab struct {
    13  		entry   uintptr
    14  		funcoff uintptr
    15  	}
    16  
    17  	newModuleWrapper struct {
    18  		pcHeader     *pcHeader
    19  		funcnametab  []byte
    20  		cutab        []uint32
    21  		filetab      []byte
    22  		pctab        []byte
    23  		pclntable    []byte
    24  		ftab         []functab
    25  		findfunctab  uintptr
    26  		minpc, maxpc uintptr
    27  
    28  		text, etext           uintptr
    29  		noptrdata, enoptrdata uintptr
    30  		data, edata           uintptr
    31  		bss, ebss             uintptr
    32  		noptrbss, enoptrbss   uintptr
    33  		end, gcdata, gcbss    uintptr
    34  		types, etypes         uintptr
    35  
    36  		textsectmap []byte
    37  		typelinks   []int32 // offsets from types
    38  		itablinks   []byte
    39  
    40  		ptab []byte
    41  
    42  		pluginpath string
    43  		pkghashes  []byte
    44  
    45  		modulename   string
    46  		modulehashes []byte
    47  
    48  		hasmain uint8 // 1 if module contains the main function, 0 otherwise
    49  
    50  		gcdatamask, gcbssmask bitvector
    51  
    52  		typemap map[int32]*byte // offset to *_rtype in previous module
    53  
    54  		bad bool // module failed to load and should be ignored
    55  
    56  		next *newModuleWrapper
    57  	}
    58  
    59  	Moduledata struct {
    60  		pcHeader *pcHeader
    61  	}
    62  
    63  	// pcHeader holds data used by the pclntab lookups.
    64  	pcHeader struct {
    65  		magic          uint32  // 0xFFFFFFFA
    66  		pad1, pad2     uint8   // 0,0
    67  		minLC          uint8   // min instruction size
    68  		ptrSize        uint8   // size of a ptr in bytes
    69  		nfunc          int     // number of functions in the module
    70  		nfiles         uint    // number of entries in the file tab.
    71  		funcnameOffset uintptr // offset to the funcnametab variable from pcHeader
    72  		cuOffset       uintptr // offset to the cutab variable from pcHeader
    73  		filetabOffset  uintptr // offset to the filetab variable from pcHeader
    74  		pctabOffset    uintptr // offset to the pctab varible from pcHeader
    75  		pclnOffset     uintptr // offset to the pclntab variable from pcHeader
    76  	}
    77  
    78  	// pcHeader holds data used by the pclntab lookups.
    79  	pcHeader1_18 struct {
    80  		magic          uint32  // 0xFFFFFFF0
    81  		pad1, pad2     uint8   // 0,0
    82  		minLC          uint8   // min instruction size
    83  		ptrSize        uint8   // size of a ptr in bytes
    84  		nfunc          int     // number of functions in the module
    85  		nfiles         uint    // number of entries in the file tab
    86  		textStart      uintptr // base for function entry PC offsets in this module, equal to moduledata.text
    87  		funcnameOffset uintptr // offset to the funcnametab variable from pcHeader
    88  		cuOffset       uintptr // offset to the cutab variable from pcHeader
    89  		filetabOffset  uintptr // offset to the filetab variable from pcHeader
    90  		pctabOffset    uintptr // offset to the pctab variable from pcHeader
    91  		pclnOffset     uintptr // offset to the pclntab variable from pcHeader
    92  	}
    93  
    94  	newModuleWrapper1_18 struct {
    95  		pcHeader     *pcHeader1_18
    96  		funcnametab  []byte
    97  		cutab        []uint32
    98  		filetab      []byte
    99  		pctab        []byte
   100  		pclntable    []byte
   101  		ftab         []functab
   102  		findfunctab  uintptr
   103  		minpc, maxpc uintptr
   104  
   105  		text, etext           uintptr
   106  		noptrdata, enoptrdata uintptr
   107  		data, edata           uintptr
   108  		bss, ebss             uintptr
   109  		noptrbss, enoptrbss   uintptr
   110  		end, gcdata, gcbss    uintptr
   111  		types, etypes         uintptr
   112  		rodata                uintptr
   113  		gofunc                uintptr // go.func.*
   114  
   115  		textsectmap []byte
   116  		typelinks   []int32 // offsets from types
   117  		itablinks   []byte
   118  
   119  		ptab []byte
   120  
   121  		pluginpath string
   122  		pkghashes  []byte
   123  
   124  		modulename   string
   125  		modulehashes []byte
   126  
   127  		hasmain uint8 // 1 if module contains the main function, 0 otherwise
   128  
   129  		gcdatamask, gcbssmask bitvector
   130  
   131  		typemap map[int32]*byte // offset to *_rtype in previous module
   132  
   133  		bad bool // module failed to load and should be ignored
   134  
   135  		next *newModuleWrapper1_18
   136  	}
   137  )
   138  
   139  func (me *newModuleWrapper) GetFtab() []functab {
   140  	return me.ftab
   141  }
   142  
   143  func (me *newModuleWrapper) GetFunc(ftab functab) *runtime.Func {
   144  	return (*runtime.Func)(unsafe.Pointer(uintptr(unsafe.Pointer(me.pcHeader)) + uintptr(me.pcHeader.pclnOffset) + ftab.funcoff))
   145  	//return (*runtime.Func)(unsafe.Pointer(&(*pcIntable)[ftab.funcoff]))
   146  }
   147  
   148  func (me *newModuleWrapper) GetNext() moduleWrapper {
   149  	if me.next != nil {
   150  		return me.next
   151  	}
   152  	return nil
   153  }
   154  
   155  func getModuleWrapper() moduleWrapper {
   156  	new := (*newModuleWrapper)(unsafe.Pointer(&Firstmoduledata))
   157  	return new
   158  }
   159  
   160  //go:linkname Firstmoduledata runtime.firstmoduledata
   161  var Firstmoduledata Moduledata