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

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