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

     1  //go:build go1.21
     2  // +build go1.21
     3  
     4  package forceexport
     5  
     6  import (
     7  	"runtime"
     8  	"unsafe"
     9  )
    10  
    11  type (
    12  
    13  	// pcHeader holds data used by the pclntab lookups.
    14  	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  	newModuleWrapper moduledata
    30  
    31  	Moduledata struct {
    32  		pcHeader *pcHeader
    33  	}
    34  
    35  	Functab1_18 struct {
    36  		entry   uint32
    37  		funcoff uint32
    38  	}
    39  )
    40  
    41  // moduledata records information about the layout of the executable
    42  // image. It is written by the linker. Any changes here must be
    43  // matched changes to the code in cmd/link/internal/ld/symtab.go:symtab.
    44  // moduledata is stored in statically allocated non-pointer memory;
    45  // none of the pointers here are visible to the garbage collector.
    46  type moduledata struct {
    47  	pcHeader     *pcHeader
    48  	funcnametab  []byte
    49  	cutab        []uint32
    50  	filetab      []byte
    51  	pctab        []byte
    52  	pclntable    []byte
    53  	ftab         []functab
    54  	findfunctab  uintptr
    55  	minpc, maxpc uintptr
    56  
    57  	text, etext           uintptr
    58  	noptrdata, enoptrdata uintptr
    59  	data, edata           uintptr
    60  	bss, ebss             uintptr
    61  	noptrbss, enoptrbss   uintptr
    62  	covctrs, ecovctrs     uintptr
    63  	end, gcdata, gcbss    uintptr
    64  	types, etypes         uintptr
    65  	rodata                uintptr
    66  	gofunc                uintptr // go.func.*
    67  
    68  	textsectmap []textsect
    69  	typelinks   []int32 // offsets from types
    70  	itablinks   []*itab
    71  
    72  	ptab []ptabEntry
    73  
    74  	pluginpath string
    75  	pkghashes  []modulehash
    76  
    77  	// This slice records the initializing tasks that need to be
    78  	// done to start up the program. It is built by the linker.
    79  	inittasks []uintptr //[]*initTask
    80  
    81  	modulename   string
    82  	modulehashes []modulehash
    83  
    84  	hasmain uint8 // 1 if module contains the main function, 0 otherwise
    85  
    86  	gcdatamask, gcbssmask bitvector
    87  
    88  	typemap map[typeOff]*_type // offset to *_rtype in previous module
    89  
    90  	bad bool // module failed to load and should be ignored
    91  
    92  	next *moduledata
    93  }
    94  
    95  type functab struct {
    96  	entryoff uint32 // relative to runtime.text
    97  	funcoff  uint32
    98  }
    99  
   100  // Mapping information for secondary text sections
   101  
   102  type textsect struct {
   103  	vaddr    uintptr // prelinked section vaddr
   104  	end      uintptr // vaddr + section length
   105  	baseaddr uintptr // relocated section address
   106  }
   107  
   108  // layout of Itab known to compilers
   109  // allocated in non-garbage-collected memory
   110  // Needs to be in sync with
   111  // ../cmd/compile/internal/reflectdata/reflect.go:/^func.WriteTabs.
   112  type itab struct {
   113  	inter *interfacetype
   114  	_type *_type
   115  	hash  uint32 // copy of _type.hash. Used for type switches.
   116  	_     [4]byte
   117  	fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
   118  }
   119  
   120  type interfacetype struct {
   121  	typ     _type
   122  	pkgpath name
   123  	mhdr    []imethod
   124  }
   125  
   126  // Needs to be in sync with ../cmd/link/internal/ld/decodesym.go:/^func.commonsize,
   127  // ../cmd/compile/internal/reflectdata/reflect.go:/^func.dcommontype and
   128  // ../reflect/type.go:/^type.rtype.
   129  // ../internal/reflectlite/type.go:/^type.rtype.
   130  type _type struct {
   131  	size       uintptr
   132  	ptrdata    uintptr // size of memory prefix holding all pointers
   133  	hash       uint32
   134  	tflag      tflag
   135  	align      uint8
   136  	fieldAlign uint8
   137  	kind       uint8
   138  	// function for comparing objects of this type
   139  	// (ptr to object A, ptr to object B) -> ==?
   140  	equal func(unsafe.Pointer, unsafe.Pointer) bool
   141  	// gcdata stores the GC type data for the garbage collector.
   142  	// If the KindGCProg bit is set in kind, gcdata is a GC program.
   143  	// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
   144  	gcdata    *byte
   145  	str       nameOff
   146  	ptrToThis typeOff
   147  }
   148  
   149  type nameOff int32
   150  type typeOff int32
   151  type textOff int32
   152  
   153  // A ptabEntry is generated by the compiler for each exported function
   154  // and global variable in the main package of a plugin. It is used to
   155  // initialize the plugin module's symbol map.
   156  type ptabEntry struct {
   157  	name nameOff
   158  	typ  typeOff
   159  }
   160  
   161  // A modulehash is used to compare the ABI of a new module or a
   162  // package in a new module with the loaded program.
   163  //
   164  // For each shared library a module links against, the linker creates an entry in the
   165  // moduledata.modulehashes slice containing the name of the module, the abi hash seen
   166  // at link time and a pointer to the runtime abi hash. These are checked in
   167  // moduledataverify1 below.
   168  //
   169  // For each loaded plugin, the pkghashes slice has a modulehash of the
   170  // newly loaded package that can be used to check the plugin's version of
   171  // a package against any previously loaded version of the package.
   172  // This is done in plugin.lastmoduleinit.
   173  type modulehash struct {
   174  	modulename   string
   175  	linktimehash string
   176  	runtimehash  *string
   177  }
   178  
   179  // name is an encoded type name with optional extra data.
   180  // See reflect/type.go for details.
   181  type name struct {
   182  	bytes *byte
   183  }
   184  
   185  type imethod struct {
   186  	name nameOff
   187  	ityp typeOff
   188  }
   189  
   190  // tflag is documented in reflect/type.go.
   191  //
   192  // tflag values must be kept in sync with copies in:
   193  //
   194  //		cmd/compile/internal/reflectdata/reflect.go
   195  //		cmd/link/internal/ld/decodesym.go
   196  //		reflect/type.go
   197  //	     internal/reflectlite/type.go
   198  type tflag uint8
   199  
   200  func (me *newModuleWrapper) GetNext() moduleWrapper {
   201  	if me.next != nil {
   202  		return (*newModuleWrapper)(me.next)
   203  	}
   204  	return nil
   205  }
   206  
   207  func (me *newModuleWrapper) GetFtab() []functab {
   208  	return me.ftab
   209  }
   210  
   211  func (me *newModuleWrapper) GetFunc(ftab functab) *runtime.Func {
   212  	ftab1_18 := (*Functab1_18)(unsafe.Pointer(&ftab))
   213  	return (*runtime.Func)(unsafe.Pointer(uintptr(unsafe.Pointer(me.pcHeader)) + uintptr(me.pcHeader.pclnOffset) + uintptr(ftab1_18.funcoff)))
   214  	//return (*runtime.Func)(unsafe.Pointer(&(*pcIntable)[ftab.funcoff]))
   215  }
   216  
   217  func getModuleWrapper() moduleWrapper {
   218  	new1_18 := (*newModuleWrapper)(unsafe.Pointer(&Firstmoduledata))
   219  	return new1_18
   220  }
   221  
   222  //go:linkname Firstmoduledata runtime.firstmoduledata
   223  var Firstmoduledata Moduledata