github.com/podhmo/reflect-shape@v0.4.3/metadata/internal/unsaferuntime/find_module_datap.go (about)

     1  package unsaferuntime
     2  
     3  import "unsafe"
     4  
     5  //go:linkname runtime_findmoduledatap runtime.findmoduledatap
     6  func runtime_findmoduledatap(pc uintptr) *moduledata
     7  
     8  //go:linkname runtime_firstmoduledata runtime.firstmoduledata
     9  var runtime_firstmoduledata moduledata
    10  
    11  // // findmoduledatap looks up the moduledata for a PC.
    12  // func findmoduledatap(pc uintptr) *moduledata {
    13  // 	for datap := &runtime_firstmoduledata; datap != nil; datap = datap.next {
    14  // 		if datap.minpc <= pc && pc < datap.maxpc {
    15  // 			return datap
    16  // 		}
    17  // 	}
    18  // 	return nil
    19  // }
    20  
    21  // copy from go/src/runtime/symtab.go
    22  
    23  type pcHeader struct {
    24  	magic          uint32  // 0xFFFFFFF0
    25  	pad1, pad2     uint8   // 0,0
    26  	minLC          uint8   // min instruction size
    27  	ptrSize        uint8   // size of a ptr in bytes
    28  	nfunc          int     // number of functions in the module
    29  	nfiles         uint    // number of entries in the file tab
    30  	textStart      uintptr // base for function entry PC offsets in this module, equal to moduledata.text
    31  	funcnameOffset uintptr // offset to the funcnametab variable from pcHeader
    32  	cuOffset       uintptr // offset to the cutab variable from pcHeader
    33  	filetabOffset  uintptr // offset to the filetab variable from pcHeader
    34  	pctabOffset    uintptr // offset to the pctab variable from pcHeader
    35  	pclnOffset     uintptr // offset to the pclntab variable from pcHeader
    36  }
    37  
    38  type functab struct {
    39  	entryoff uint32 // relative to runtime.text
    40  	funcoff  uint32
    41  }
    42  
    43  type textsect struct {
    44  	vaddr    uintptr // prelinked section vaddr
    45  	end      uintptr // vaddr + section length
    46  	baseaddr uintptr // relocated section address
    47  }
    48  
    49  type moduledata struct {
    50  	pcHeader     *pcHeader
    51  	funcnametab  []byte
    52  	cutab        []uint32
    53  	filetab      []byte
    54  	pctab        []byte
    55  	pclntable    []byte
    56  	ftab         []functab
    57  	findfunctab  uintptr
    58  	minpc, maxpc uintptr
    59  
    60  	text, etext           uintptr
    61  	noptrdata, enoptrdata uintptr
    62  	data, edata           uintptr
    63  	bss, ebss             uintptr
    64  	noptrbss, enoptrbss   uintptr
    65  	end, gcdata, gcbss    uintptr
    66  	types, etypes         uintptr
    67  	rodata                uintptr
    68  	gofunc                uintptr // go.func.*
    69  
    70  	textsectmap []textsect
    71  	typelinks   []int32 // offsets from types
    72  	itablinks   []*itab
    73  
    74  	ptab []ptabEntry
    75  
    76  	pluginpath string
    77  	pkghashes  []modulehash
    78  
    79  	modulename   string
    80  	modulehashes []modulehash
    81  
    82  	hasmain uint8 // 1 if module contains the main function, 0 otherwise
    83  
    84  	gcdatamask, gcbssmask bitvector
    85  
    86  	typemap map[typeOff]*_type // offset to *_rtype in previous module
    87  
    88  	bad bool // module failed to load and should be ignored
    89  
    90  	next *moduledata
    91  }
    92  
    93  type nameOff int32
    94  type typeOff int32
    95  type textOff int32
    96  
    97  type bitvector struct {
    98  	n        int32 // # of bits
    99  	bytedata *uint8
   100  }
   101  
   102  type modulehash struct {
   103  	modulename   string
   104  	linktimehash string
   105  	runtimehash  *string
   106  }
   107  
   108  type interfacetype struct {
   109  	typ     _type
   110  	pkgpath name
   111  	mhdr    []imethod
   112  }
   113  
   114  type imethod struct {
   115  	name nameOff
   116  	ityp typeOff
   117  }
   118  
   119  type name struct {
   120  	bytes *byte
   121  }
   122  
   123  type tflag uint8
   124  
   125  const (
   126  	tflagUncommon      tflag = 1 << 0
   127  	tflagExtraStar     tflag = 1 << 1
   128  	tflagNamed         tflag = 1 << 2
   129  	tflagRegularMemory tflag = 1 << 3 // equal and hash can treat values of this type as a single region of t.size bytes
   130  )
   131  
   132  type _type struct {
   133  	size       uintptr
   134  	ptrdata    uintptr // size of memory prefix holding all pointers
   135  	hash       uint32
   136  	tflag      tflag
   137  	align      uint8
   138  	fieldAlign uint8
   139  	kind       uint8
   140  	// function for comparing objects of this type
   141  	// (ptr to object A, ptr to object B) -> ==?
   142  	equal func(unsafe.Pointer, unsafe.Pointer) bool
   143  	// gcdata stores the GC type data for the garbage collector.
   144  	// If the KindGCProg bit is set in kind, gcdata is a GC program.
   145  	// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
   146  	gcdata    *byte
   147  	str       nameOff
   148  	ptrToThis typeOff
   149  }
   150  
   151  type ptabEntry struct {
   152  	name nameOff
   153  	typ  typeOff
   154  }
   155  
   156  type itab struct {
   157  	inter *interfacetype
   158  	_type *_type
   159  	hash  uint32 // copy of _type.hash. Used for type switches.
   160  	_     [4]byte
   161  	fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement intfindfunctab
   162  }