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

     1  //go:build go1.16 && !go1.18
     2  // +build go1.16,!go1.18
     3  
     4  package goloader
     5  
     6  import (
     7  	"unsafe"
     8  )
     9  
    10  const magic uint32 = 0xFFFFFFFA
    11  
    12  // pcHeader holds data used by the pclntab lookups.
    13  type pcHeader struct {
    14  	magic          uint32  // 0xFFFFFFFA
    15  	pad1, pad2     uint8   // 0,0
    16  	minLC          uint8   // min instruction size
    17  	ptrSize        uint8   // size of a ptr in bytes
    18  	nfunc          int     // number of functions in the module
    19  	nfiles         uint    // number of entries in the file tab.
    20  	funcnameOffset uintptr // offset to the funcnametab variable from pcHeader
    21  	cuOffset       uintptr // offset to the cutab variable from pcHeader
    22  	filetabOffset  uintptr // offset to the filetab variable from pcHeader
    23  	pctabOffset    uintptr // offset to the pctab varible from pcHeader
    24  	pclnOffset     uintptr // offset to the pclntab variable from pcHeader
    25  }
    26  
    27  // moduledata records information about the layout of the executable
    28  // image. It is written by the linker. Any changes here must be
    29  // matched changes to the code in cmd/link/internal/ld/symtab.go:symtab.
    30  // moduledata is stored in statically allocated non-pointer memory;
    31  // none of the pointers here are visible to the garbage collector.
    32  type moduledata struct {
    33  	pcHeader     *pcHeader
    34  	funcnametab  []byte
    35  	cutab        []uint32
    36  	filetab      []byte
    37  	pctab        []byte
    38  	pclntable    []byte
    39  	ftab         []functab
    40  	findfunctab  uintptr
    41  	minpc, maxpc uintptr
    42  
    43  	text, etext           uintptr
    44  	noptrdata, enoptrdata uintptr
    45  	data, edata           uintptr
    46  	bss, ebss             uintptr
    47  	noptrbss, enoptrbss   uintptr
    48  	end, gcdata, gcbss    uintptr
    49  	types, etypes         uintptr
    50  
    51  	textsectmap []textsect
    52  	typelinks   []int32 // offsets from types
    53  	itablinks   []*itab
    54  
    55  	ptab []ptabEntry
    56  
    57  	pluginpath string
    58  	pkghashes  []modulehash
    59  
    60  	modulename   string
    61  	modulehashes []modulehash
    62  
    63  	hasmain uint8 // 1 if module contains the main function, 0 otherwise
    64  
    65  	gcdatamask, gcbssmask bitvector
    66  
    67  	typemap map[typeOff]uintptr // offset to *_rtype in previous module
    68  
    69  	bad bool // module failed to load and should be ignored
    70  
    71  	next *moduledata
    72  }
    73  
    74  func initmodule(module *moduledata, linker *Linker) {
    75  	module.pcHeader = (*pcHeader)(unsafe.Pointer(&(module.pclntable[0])))
    76  	module.pcHeader.nfunc = len(module.ftab)
    77  	module.pcHeader.nfiles = (uint)(len(module.filetab))
    78  	module.funcnametab = module.pclntable
    79  	module.pctab = module.pclntable
    80  	module.cutab = linker.Filetab
    81  	module.filetab = module.pclntable
    82  	module.hasmain = 0
    83  	module.bad = false
    84  }