1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // NOTE: Func does not expose the actual unexported fields, because we return *Func
10 // values to users, and we want to keep them from being able to overwrite the data
11 // with (say) *f = Func{}.
12 // All code operating on a *Func must call raw to get the *_func instead.
14 // A Func represents a Go function in the running binary.
16 opaque struct{} // unexported field to disallow conversions
19 func (f *Func) raw() *_func {
20 return (*_func)(unsafe.Pointer(f))
25 _PCDATA_StackMapIndex = 0
26 _FUNCDATA_ArgsPointerMaps = 0
27 _FUNCDATA_LocalsPointerMaps = 1
28 _ArgsSizeUnknown = -0x80000000
31 // moduledata records information about the layout of the executable
32 // image. It is written by the linker. Any changes here must be
33 // matched changes to the code in cmd/internal/ld/symtab.go:symtab.
34 // moduledata is stored in read-only memory; none of the pointers here
35 // are visible to the garbage collector.
36 type moduledata struct {
44 noptrdata, enoptrdata uintptr
47 noptrbss, enoptrbss uintptr
48 end, gcdata, gcbss uintptr
53 modulehashes []modulehash
55 gcdatamask, gcbssmask bitvector
60 // For each shared library a module links against, the linker creates an entry in the
61 // moduledata.modulehashes slice containing the name of the module, the abi hash seen
62 // at link time and a pointer to the runtime abi hash. These are checked in
63 // moduledataverify1 below.
64 type modulehash struct {
70 var firstmoduledata moduledata // linker symbol
71 var lastmoduledatap *moduledata // linker symbol
78 const minfunc = 16 // minimum function size
79 const pcbucketsize = 256 * minfunc // size of bucket in the pc->func lookup table
81 // findfunctab is an array of these structures.
82 // Each bucket represents 4096 bytes of the text segment.
83 // Each subbucket represents 256 bytes of the text segment.
84 // To find a function given a pc, locate the bucket and subbucket for
85 // that pc. Add together the idx and subbucket value to obtain a
86 // function index. Then scan the functab array starting at that
87 // index to find the target function.
88 // This table uses 20 bytes for every 4096 bytes of code, or ~0.5% overhead.
89 type findfuncbucket struct {
94 func moduledataverify() {
95 for datap := &firstmoduledata; datap != nil; datap = datap.next {
96 moduledataverify1(datap)
100 const debugPcln = false
102 func moduledataverify1(datap *moduledata) {
103 // See golang.org/s/go12symtab for header: 0xfffffffb,
104 // two zero bytes, a byte giving the PC quantum,
105 // and a byte giving the pointer width in bytes.
106 pcln := *(**[8]byte)(unsafe.Pointer(&datap.pclntable))
107 pcln32 := *(**[2]uint32)(unsafe.Pointer(&datap.pclntable))
108 if pcln32[0] != 0xfffffffb || pcln[4] != 0 || pcln[5] != 0 || pcln[6] != _PCQuantum || pcln[7] != ptrSize {
109 println("runtime: function symbol table header:", hex(pcln32[0]), hex(pcln[4]), hex(pcln[5]), hex(pcln[6]), hex(pcln[7]))
110 throw("invalid function symbol table\n")
113 // ftab is lookup table for function by program counter.
114 nftab := len(datap.ftab) - 1
115 var pcCache pcvalueCache
116 for i := 0; i < nftab; i++ {
117 // NOTE: ftab[nftab].entry is legal; it is the address beyond the final function.
118 if datap.ftab[i].entry > datap.ftab[i+1].entry {
119 f1 := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i].funcoff]))
120 f2 := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i+1].funcoff]))
123 f2name = funcname(f2)
125 println("function symbol table not sorted by program counter:", hex(datap.ftab[i].entry), funcname(f1), ">", hex(datap.ftab[i+1].entry), f2name)
126 for j := 0; j <= i; j++ {
127 print("\t", hex(datap.ftab[j].entry), " ", funcname((*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[j].funcoff]))), "\n")
129 throw("invalid runtime symbol table")
132 if debugPcln || nftab-i < 5 {
133 // Check a PC near but not at the very end.
134 // The very end might be just padding that is not covered by the tables.
135 // No architecture rounds function entries to more than 16 bytes,
136 // but if one came along we'd need to subtract more here.
137 // But don't use the next PC if it corresponds to a foreign object chunk
138 // (no pcln table, f2.pcln == 0). That chunk might have an alignment
139 // more than 16 bytes.
140 f := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i].funcoff]))
143 f2 := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i+1].funcoff]))
151 pcvalue(f, f.pcfile, end, &pcCache, true)
152 pcvalue(f, f.pcln, end, &pcCache, true)
153 pcvalue(f, f.pcsp, end, &pcCache, true)
157 if datap.minpc != datap.ftab[0].entry ||
158 datap.maxpc != datap.ftab[nftab].entry {
159 throw("minpc or maxpc invalid")
162 for _, modulehash := range datap.modulehashes {
163 if modulehash.linktimehash != *modulehash.runtimehash {
164 println("abi mismatch detected between", datap.modulename, "and", modulehash.modulename)
165 throw("abi mismatch")
170 // FuncForPC returns a *Func describing the function that contains the
171 // given program counter address, or else nil.
172 func FuncForPC(pc uintptr) *Func {
173 return (*Func)(unsafe.Pointer(findfunc(pc)))
176 // Name returns the name of the function.
177 func (f *Func) Name() string {
178 return funcname(f.raw())
181 // Entry returns the entry address of the function.
182 func (f *Func) Entry() uintptr {
186 // FileLine returns the file name and line number of the
187 // source code corresponding to the program counter pc.
188 // The result will not be accurate if pc is not a program
190 func (f *Func) FileLine(pc uintptr) (file string, line int) {
191 // Pass strict=false here, because anyone can call this function,
192 // and they might just be wrong about targetpc belonging to f.
193 file, line32 := funcline1(f.raw(), pc, false)
194 return file, int(line32)
197 func findmoduledatap(pc uintptr) *moduledata {
198 for datap := &firstmoduledata; datap != nil; datap = datap.next {
199 if datap.minpc <= pc && pc <= datap.maxpc {
206 func findfunc(pc uintptr) *_func {
207 datap := findmoduledatap(pc)
211 const nsub = uintptr(len(findfuncbucket{}.subbuckets))
213 x := pc - datap.minpc
214 b := x / pcbucketsize
215 i := x % pcbucketsize / (pcbucketsize / nsub)
217 ffb := (*findfuncbucket)(add(unsafe.Pointer(datap.findfunctab), b*unsafe.Sizeof(findfuncbucket{})))
218 idx := ffb.idx + uint32(ffb.subbuckets[i])
219 if pc < datap.ftab[idx].entry {
220 throw("findfunc: bad findfunctab entry")
223 // linear search to find func with pc >= entry.
224 for datap.ftab[idx+1].entry <= pc {
227 return (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[idx].funcoff]))
230 type pcvalueCache struct {
231 entries [16]pcvalueCacheEnt
234 type pcvalueCacheEnt struct {
235 // targetpc and off together are the key of this cache entry.
238 // val is the value of this cached pcvalue entry.
242 func pcvalue(f *_func, off int32, targetpc uintptr, cache *pcvalueCache, strict bool) int32 {
247 // Check the cache. This speeds up walks of deep stacks, which
248 // tend to have the same recursive functions over and over.
250 // This cache is small enough that full associativity is
251 // cheaper than doing the hashing for a less associative
254 for _, ent := range cache.entries {
255 // We check off first because we're more
256 // likely to have multiple entries with
257 // different offsets for the same targetpc
258 // than the other way around, so we'll usually
259 // fail in the first clause.
260 if ent.off == off && ent.targetpc == targetpc {
266 datap := findmoduledatap(f.entry) // inefficient
268 if strict && panicking == 0 {
269 print("runtime: no module data for ", hex(f.entry), "\n")
270 throw("no module data")
274 p := datap.pclntable[off:]
279 p, ok = step(p, &pc, &val, pc == f.entry)
284 // Replace a random entry in the cache. Random
285 // replacement prevents a performance cliff if
286 // a recursive stack's cycle is slightly
287 // larger than the cache.
289 ci := fastrand1() % uint32(len(cache.entries))
290 cache.entries[ci] = pcvalueCacheEnt{
301 // If there was a table, it should have covered all program counters.
302 // If not, something is wrong.
303 if panicking != 0 || !strict {
307 print("runtime: invalid pc-encoded table f=", funcname(f), " pc=", hex(pc), " targetpc=", hex(targetpc), " tab=", p, "\n")
309 p = datap.pclntable[off:]
314 p, ok = step(p, &pc, &val, pc == f.entry)
318 print("\tvalue=", val, " until pc=", hex(pc), "\n")
321 throw("invalid runtime symbol table")
325 func cfuncname(f *_func) *byte {
326 if f == nil || f.nameoff == 0 {
329 datap := findmoduledatap(f.entry) // inefficient
333 return &datap.pclntable[f.nameoff]
336 func funcname(f *_func) string {
337 return gostringnocopy(cfuncname(f))
340 func funcline1(f *_func, targetpc uintptr, strict bool) (file string, line int32) {
341 datap := findmoduledatap(f.entry) // inefficient
345 fileno := int(pcvalue(f, f.pcfile, targetpc, nil, strict))
346 line = pcvalue(f, f.pcln, targetpc, nil, strict)
347 if fileno == -1 || line == -1 || fileno >= len(datap.filetab) {
348 // print("looking for ", hex(targetpc), " in ", funcname(f), " got file=", fileno, " line=", lineno, "\n")
351 file = gostringnocopy(&datap.pclntable[datap.filetab[fileno]])
355 func funcline(f *_func, targetpc uintptr) (file string, line int32) {
356 return funcline1(f, targetpc, true)
359 func funcspdelta(f *_func, targetpc uintptr, cache *pcvalueCache) int32 {
360 x := pcvalue(f, f.pcsp, targetpc, cache, true)
361 if x&(ptrSize-1) != 0 {
362 print("invalid spdelta ", funcname(f), " ", hex(f.entry), " ", hex(targetpc), " ", hex(f.pcsp), " ", x, "\n")
367 func pcdatavalue(f *_func, table int32, targetpc uintptr, cache *pcvalueCache) int32 {
368 if table < 0 || table >= f.npcdata {
371 off := *(*int32)(add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(table)*4))
372 return pcvalue(f, off, targetpc, cache, true)
375 func funcdata(f *_func, i int32) unsafe.Pointer {
376 if i < 0 || i >= f.nfuncdata {
379 p := add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(f.npcdata)*4)
380 if ptrSize == 8 && uintptr(p)&4 != 0 {
381 if uintptr(unsafe.Pointer(f))&4 != 0 {
382 println("runtime: misaligned func", f)
386 return *(*unsafe.Pointer)(add(p, uintptr(i)*ptrSize))
389 // step advances to the next pc, value pair in the encoded table.
390 func step(p []byte, pc *uintptr, val *int32, first bool) (newp []byte, ok bool) {
391 p, uvdelta := readvarint(p)
392 if uvdelta == 0 && !first {
396 uvdelta = ^(uvdelta >> 1)
400 vdelta := int32(uvdelta)
401 p, pcdelta := readvarint(p)
402 *pc += uintptr(pcdelta * _PCQuantum)
407 // readvarint reads a varint from p.
408 func readvarint(p []byte) (newp []byte, val uint32) {
413 v |= (uint32(b) & 0x7F) << shift
422 type stackmap struct {
423 n int32 // number of bitmaps
424 nbit int32 // number of bits in each bitmap
425 bytedata [1]byte // bitmaps, each starting on a 32-bit boundary
429 func stackmapdata(stkmap *stackmap, n int32) bitvector {
430 if n < 0 || n >= stkmap.n {
431 throw("stackmapdata: index out of range")
433 return bitvector{stkmap.nbit, (*byte)(add(unsafe.Pointer(&stkmap.bytedata), uintptr(n*((stkmap.nbit+31)/32*4))))}