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

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