github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/runtime/symtab.go (about) 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. 4 5 package runtime 6 7 import "unsafe" 8 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. 13 14 // A Func represents a Go function in the running binary. 15 type Func struct { 16 opaque struct{} // unexported field to disallow conversions 17 } 18 19 func (f *Func) raw() *_func { 20 return (*_func)(unsafe.Pointer(f)) 21 } 22 23 // funcdata.h 24 const ( 25 _PCDATA_StackMapIndex = 0 26 _FUNCDATA_ArgsPointerMaps = 0 27 _FUNCDATA_LocalsPointerMaps = 1 28 _FUNCDATA_DeadValueMaps = 2 29 _ArgsSizeUnknown = -0x80000000 30 ) 31 32 var ( 33 pclntable []byte 34 ftab []functab 35 filetab []uint32 36 37 pclntab, epclntab struct{} // linker symbols 38 ) 39 40 type functab struct { 41 entry uintptr 42 funcoff uintptr 43 } 44 45 func symtabinit() { 46 // See golang.org/s/go12symtab for header: 0xfffffffb, 47 // two zero bytes, a byte giving the PC quantum, 48 // and a byte giving the pointer width in bytes. 49 pcln := (*[8]byte)(unsafe.Pointer(&pclntab)) 50 pcln32 := (*[2]uint32)(unsafe.Pointer(&pclntab)) 51 if pcln32[0] != 0xfffffffb || pcln[4] != 0 || pcln[5] != 0 || pcln[6] != _PCQuantum || pcln[7] != ptrSize { 52 println("runtime: function symbol table header:", hex(pcln32[0]), hex(pcln[4]), hex(pcln[5]), hex(pcln[6]), hex(pcln[7])) 53 gothrow("invalid function symbol table\n") 54 } 55 56 // pclntable is all bytes of pclntab symbol. 57 sp := (*sliceStruct)(unsafe.Pointer(&pclntable)) 58 sp.array = unsafe.Pointer(&pclntab) 59 sp.len = int(uintptr(unsafe.Pointer(&epclntab)) - uintptr(unsafe.Pointer(&pclntab))) 60 sp.cap = sp.len 61 62 // ftab is lookup table for function by program counter. 63 nftab := int(*(*uintptr)(add(unsafe.Pointer(pcln), 8))) 64 p := add(unsafe.Pointer(pcln), 8+ptrSize) 65 sp = (*sliceStruct)(unsafe.Pointer(&ftab)) 66 sp.array = p 67 sp.len = nftab + 1 68 sp.cap = sp.len 69 for i := 0; i < nftab; i++ { 70 // NOTE: ftab[nftab].entry is legal; it is the address beyond the final function. 71 if ftab[i].entry > ftab[i+1].entry { 72 f1 := (*_func)(unsafe.Pointer(&pclntable[ftab[i].funcoff])) 73 f2 := (*_func)(unsafe.Pointer(&pclntable[ftab[i+1].funcoff])) 74 f2name := "end" 75 if i+1 < nftab { 76 f2name = gofuncname(f2) 77 } 78 println("function symbol table not sorted by program counter:", hex(ftab[i].entry), gofuncname(f1), ">", hex(ftab[i+1].entry), f2name) 79 for j := 0; j <= i; j++ { 80 print("\t", hex(ftab[j].entry), " ", gofuncname((*_func)(unsafe.Pointer(&pclntable[ftab[j].funcoff]))), "\n") 81 } 82 gothrow("invalid runtime symbol table") 83 } 84 } 85 86 // The ftab ends with a half functab consisting only of 87 // 'entry', followed by a uint32 giving the pcln-relative 88 // offset of the file table. 89 sp = (*sliceStruct)(unsafe.Pointer(&filetab)) 90 end := unsafe.Pointer(&ftab[nftab].funcoff) // just beyond ftab 91 fileoffset := *(*uint32)(end) 92 sp.array = unsafe.Pointer(&pclntable[fileoffset]) 93 // length is in first element of array. 94 // set len to 1 so we can get first element. 95 sp.len = 1 96 sp.cap = 1 97 sp.len = int(filetab[0]) 98 sp.cap = sp.len 99 } 100 101 // FuncForPC returns a *Func describing the function that contains the 102 // given program counter address, or else nil. 103 func FuncForPC(pc uintptr) *Func { 104 return (*Func)(unsafe.Pointer(findfunc(pc))) 105 } 106 107 // Name returns the name of the function. 108 func (f *Func) Name() string { 109 return gofuncname(f.raw()) 110 } 111 112 // Entry returns the entry address of the function. 113 func (f *Func) Entry() uintptr { 114 return f.raw().entry 115 } 116 117 // FileLine returns the file name and line number of the 118 // source code corresponding to the program counter pc. 119 // The result will not be accurate if pc is not a program 120 // counter within f. 121 func (f *Func) FileLine(pc uintptr) (file string, line int) { 122 // Pass strict=false here, because anyone can call this function, 123 // and they might just be wrong about targetpc belonging to f. 124 file, line32 := funcline1(f.raw(), pc, false) 125 return file, int(line32) 126 } 127 128 func findfunc(pc uintptr) *_func { 129 if len(ftab) == 0 { 130 return nil 131 } 132 133 if pc < ftab[0].entry || pc >= ftab[len(ftab)-1].entry { 134 return nil 135 } 136 137 // binary search to find func with entry <= pc. 138 lo := 0 139 nf := len(ftab) - 1 // last entry is sentinel 140 for nf > 0 { 141 n := nf / 2 142 f := &ftab[lo+n] 143 if f.entry <= pc && pc < ftab[lo+n+1].entry { 144 return (*_func)(unsafe.Pointer(&pclntable[f.funcoff])) 145 } else if pc < f.entry { 146 nf = n 147 } else { 148 lo += n + 1 149 nf -= n + 1 150 } 151 } 152 153 gothrow("findfunc: binary search failed") 154 return nil 155 } 156 157 func pcvalue(f *_func, off int32, targetpc uintptr, strict bool) int32 { 158 if off == 0 { 159 return -1 160 } 161 p := pclntable[off:] 162 pc := f.entry 163 val := int32(-1) 164 for { 165 var ok bool 166 p, ok = step(p, &pc, &val, pc == f.entry) 167 if !ok { 168 break 169 } 170 if targetpc < pc { 171 return val 172 } 173 } 174 175 // If there was a table, it should have covered all program counters. 176 // If not, something is wrong. 177 if panicking != 0 || !strict { 178 return -1 179 } 180 181 print("runtime: invalid pc-encoded table f=", gofuncname(f), " pc=", hex(pc), " targetpc=", hex(targetpc), " tab=", p, "\n") 182 183 p = pclntable[off:] 184 pc = f.entry 185 val = -1 186 for { 187 var ok bool 188 p, ok = step(p, &pc, &val, pc == f.entry) 189 if !ok { 190 break 191 } 192 print("\tvalue=", val, " until pc=", hex(pc), "\n") 193 } 194 195 gothrow("invalid runtime symbol table") 196 return -1 197 } 198 199 func funcname(f *_func) *byte { 200 if f == nil || f.nameoff == 0 { 201 return nil 202 } 203 return (*byte)(unsafe.Pointer(&pclntable[f.nameoff])) 204 } 205 206 func gofuncname(f *_func) string { 207 return gostringnocopy(funcname(f)) 208 } 209 210 func funcline1(f *_func, targetpc uintptr, strict bool) (file string, line int32) { 211 fileno := int(pcvalue(f, f.pcfile, targetpc, strict)) 212 line = pcvalue(f, f.pcln, targetpc, strict) 213 if fileno == -1 || line == -1 || fileno >= len(filetab) { 214 // print("looking for ", hex(targetpc), " in ", gofuncname(f), " got file=", fileno, " line=", lineno, "\n") 215 return "?", 0 216 } 217 file = gostringnocopy(&pclntable[filetab[fileno]]) 218 return 219 } 220 221 func funcline(f *_func, targetpc uintptr) (file string, line int32) { 222 return funcline1(f, targetpc, true) 223 } 224 225 func funcspdelta(f *_func, targetpc uintptr) int32 { 226 x := pcvalue(f, f.pcsp, targetpc, true) 227 if x&(ptrSize-1) != 0 { 228 print("invalid spdelta ", hex(f.entry), " ", hex(targetpc), " ", hex(f.pcsp), " ", x, "\n") 229 } 230 return x 231 } 232 233 func pcdatavalue(f *_func, table int32, targetpc uintptr) int32 { 234 if table < 0 || table >= f.npcdata { 235 return -1 236 } 237 off := *(*int32)(add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(table)*4)) 238 return pcvalue(f, off, targetpc, true) 239 } 240 241 func funcdata(f *_func, i int32) unsafe.Pointer { 242 if i < 0 || i >= f.nfuncdata { 243 return nil 244 } 245 p := add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(f.npcdata)*4) 246 if ptrSize == 8 && uintptr(p)&4 != 0 { 247 if uintptr(unsafe.Pointer(f))&4 != 0 { 248 println("runtime: misaligned func", f) 249 } 250 p = add(p, 4) 251 } 252 return *(*unsafe.Pointer)(add(p, uintptr(i)*ptrSize)) 253 } 254 255 // step advances to the next pc, value pair in the encoded table. 256 func step(p []byte, pc *uintptr, val *int32, first bool) (newp []byte, ok bool) { 257 p, uvdelta := readvarint(p) 258 if uvdelta == 0 && !first { 259 return nil, false 260 } 261 if uvdelta&1 != 0 { 262 uvdelta = ^(uvdelta >> 1) 263 } else { 264 uvdelta >>= 1 265 } 266 vdelta := int32(uvdelta) 267 p, pcdelta := readvarint(p) 268 *pc += uintptr(pcdelta * _PCQuantum) 269 *val += vdelta 270 return p, true 271 } 272 273 // readvarint reads a varint from p. 274 func readvarint(p []byte) (newp []byte, val uint32) { 275 var v, shift uint32 276 for { 277 b := p[0] 278 p = p[1:] 279 v |= (uint32(b) & 0x7F) << shift 280 if b&0x80 == 0 { 281 break 282 } 283 shift += 7 284 } 285 return p, v 286 }