[PATCH] RISC-V: Add missing insn types for XiangShan Nanhu scheduler model
[official-gcc.git] / libgo / go / debug / gosym / pclntab.go
blobd9ae8b73a93bc89cd722a076cd7a5cd6372dc24b
1 // Copyright 2009 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.
5 /*
6 * Line tables
7 */
9 package gosym
11 import (
12 "bytes"
13 "encoding/binary"
14 "sort"
15 "sync"
18 // version of the pclntab
19 type version int
21 const (
22 verUnknown version = iota
23 ver11
24 ver12
25 ver116
26 ver118
29 // A LineTable is a data structure mapping program counters to line numbers.
31 // In Go 1.1 and earlier, each function (represented by a Func) had its own LineTable,
32 // and the line number corresponded to a numbering of all source lines in the
33 // program, across all files. That absolute line number would then have to be
34 // converted separately to a file name and line number within the file.
36 // In Go 1.2, the format of the data changed so that there is a single LineTable
37 // for the entire program, shared by all Funcs, and there are no absolute line
38 // numbers, just line numbers within specific files.
40 // For the most part, LineTable's methods should be treated as an internal
41 // detail of the package; callers should use the methods on Table instead.
42 type LineTable struct {
43 Data []byte
44 PC uint64
45 Line int
47 // This mutex is used to keep parsing of pclntab synchronous.
48 mu sync.Mutex
50 // Contains the version of the pclntab section.
51 version version
53 // Go 1.2/1.16/1.18 state
54 binary binary.ByteOrder
55 quantum uint32
56 ptrsize uint32
57 textStart uint64 // address of runtime.text symbol (1.18+)
58 funcnametab []byte
59 cutab []byte
60 funcdata []byte
61 functab []byte
62 nfunctab uint32
63 filetab []byte
64 pctab []byte // points to the pctables.
65 nfiletab uint32
66 funcNames map[uint32]string // cache the function names
67 strings map[uint32]string // interned substrings of Data, keyed by offset
68 // fileMap varies depending on the version of the object file.
69 // For ver12, it maps the name to the index in the file table.
70 // For ver116, it maps the name to the offset in filetab.
71 fileMap map[string]uint32
74 // NOTE(rsc): This is wrong for GOARCH=arm, which uses a quantum of 4,
75 // but we have no idea whether we're using arm or not. This only
76 // matters in the old (pre-Go 1.2) symbol table format, so it's not worth
77 // fixing.
78 const oldQuantum = 1
80 func (t *LineTable) parse(targetPC uint64, targetLine int) (b []byte, pc uint64, line int) {
81 // The PC/line table can be thought of as a sequence of
82 // <pc update>* <line update>
83 // batches. Each update batch results in a (pc, line) pair,
84 // where line applies to every PC from pc up to but not
85 // including the pc of the next pair.
87 // Here we process each update individually, which simplifies
88 // the code, but makes the corner cases more confusing.
89 b, pc, line = t.Data, t.PC, t.Line
90 for pc <= targetPC && line != targetLine && len(b) > 0 {
91 code := b[0]
92 b = b[1:]
93 switch {
94 case code == 0:
95 if len(b) < 4 {
96 b = b[0:0]
97 break
99 val := binary.BigEndian.Uint32(b)
100 b = b[4:]
101 line += int(val)
102 case code <= 64:
103 line += int(code)
104 case code <= 128:
105 line -= int(code - 64)
106 default:
107 pc += oldQuantum * uint64(code-128)
108 continue
110 pc += oldQuantum
112 return b, pc, line
115 func (t *LineTable) slice(pc uint64) *LineTable {
116 data, pc, line := t.parse(pc, -1)
117 return &LineTable{Data: data, PC: pc, Line: line}
120 // PCToLine returns the line number for the given program counter.
122 // Deprecated: Use Table's PCToLine method instead.
123 func (t *LineTable) PCToLine(pc uint64) int {
124 if t.isGo12() {
125 return t.go12PCToLine(pc)
127 _, _, line := t.parse(pc, -1)
128 return line
131 // LineToPC returns the program counter for the given line number,
132 // considering only program counters before maxpc.
134 // Deprecated: Use Table's LineToPC method instead.
135 func (t *LineTable) LineToPC(line int, maxpc uint64) uint64 {
136 if t.isGo12() {
137 return 0
139 _, pc, line1 := t.parse(maxpc, line)
140 if line1 != line {
141 return 0
143 // Subtract quantum from PC to account for post-line increment
144 return pc - oldQuantum
147 // NewLineTable returns a new PC/line table
148 // corresponding to the encoded data.
149 // Text must be the start address of the
150 // corresponding text segment.
151 func NewLineTable(data []byte, text uint64) *LineTable {
152 return &LineTable{Data: data, PC: text, Line: 0, funcNames: make(map[uint32]string), strings: make(map[uint32]string)}
155 // Go 1.2 symbol table format.
156 // See golang.org/s/go12symtab.
158 // A general note about the methods here: rather than try to avoid
159 // index out of bounds errors, we trust Go to detect them, and then
160 // we recover from the panics and treat them as indicative of a malformed
161 // or incomplete table.
163 // The methods called by symtab.go, which begin with "go12" prefixes,
164 // are expected to have that recovery logic.
166 // isGo12 reports whether this is a Go 1.2 (or later) symbol table.
167 func (t *LineTable) isGo12() bool {
168 t.parsePclnTab()
169 return t.version >= ver12
172 const (
173 go12magic = 0xfffffffb
174 go116magic = 0xfffffffa
175 go118magic = 0xfffffff0
178 // uintptr returns the pointer-sized value encoded at b.
179 // The pointer size is dictated by the table being read.
180 func (t *LineTable) uintptr(b []byte) uint64 {
181 if t.ptrsize == 4 {
182 return uint64(t.binary.Uint32(b))
184 return t.binary.Uint64(b)
187 // parsePclnTab parses the pclntab, setting the version.
188 func (t *LineTable) parsePclnTab() {
189 t.mu.Lock()
190 defer t.mu.Unlock()
191 if t.version != verUnknown {
192 return
195 // Note that during this function, setting the version is the last thing we do.
196 // If we set the version too early, and parsing failed (likely as a panic on
197 // slice lookups), we'd have a mistaken version.
199 // Error paths through this code will default the version to 1.1.
200 t.version = ver11
202 if !disableRecover {
203 defer func() {
204 // If we panic parsing, assume it's a Go 1.1 pclntab.
205 recover()
209 // Check header: 4-byte magic, two zeros, pc quantum, pointer size.
210 if len(t.Data) < 16 || t.Data[4] != 0 || t.Data[5] != 0 ||
211 (t.Data[6] != 1 && t.Data[6] != 2 && t.Data[6] != 4) || // pc quantum
212 (t.Data[7] != 4 && t.Data[7] != 8) { // pointer size
213 return
216 var possibleVersion version
217 leMagic := binary.LittleEndian.Uint32(t.Data)
218 beMagic := binary.BigEndian.Uint32(t.Data)
219 switch {
220 case leMagic == go12magic:
221 t.binary, possibleVersion = binary.LittleEndian, ver12
222 case beMagic == go12magic:
223 t.binary, possibleVersion = binary.BigEndian, ver12
224 case leMagic == go116magic:
225 t.binary, possibleVersion = binary.LittleEndian, ver116
226 case beMagic == go116magic:
227 t.binary, possibleVersion = binary.BigEndian, ver116
228 case leMagic == go118magic:
229 t.binary, possibleVersion = binary.LittleEndian, ver118
230 case beMagic == go118magic:
231 t.binary, possibleVersion = binary.BigEndian, ver118
232 default:
233 return
235 t.version = possibleVersion
237 // quantum and ptrSize are the same between 1.2, 1.16, and 1.18
238 t.quantum = uint32(t.Data[6])
239 t.ptrsize = uint32(t.Data[7])
241 offset := func(word uint32) uint64 {
242 return t.uintptr(t.Data[8+word*t.ptrsize:])
244 data := func(word uint32) []byte {
245 return t.Data[offset(word):]
248 switch possibleVersion {
249 case ver118:
250 t.nfunctab = uint32(offset(0))
251 t.nfiletab = uint32(offset(1))
252 t.textStart = t.PC // use the start PC instead of reading from the table, which may be unrelocated
253 t.funcnametab = data(3)
254 t.cutab = data(4)
255 t.filetab = data(5)
256 t.pctab = data(6)
257 t.funcdata = data(7)
258 t.functab = data(7)
259 functabsize := (int(t.nfunctab)*2 + 1) * t.functabFieldSize()
260 t.functab = t.functab[:functabsize]
261 case ver116:
262 t.nfunctab = uint32(offset(0))
263 t.nfiletab = uint32(offset(1))
264 t.funcnametab = data(2)
265 t.cutab = data(3)
266 t.filetab = data(4)
267 t.pctab = data(5)
268 t.funcdata = data(6)
269 t.functab = data(6)
270 functabsize := (int(t.nfunctab)*2 + 1) * t.functabFieldSize()
271 t.functab = t.functab[:functabsize]
272 case ver12:
273 t.nfunctab = uint32(t.uintptr(t.Data[8:]))
274 t.funcdata = t.Data
275 t.funcnametab = t.Data
276 t.functab = t.Data[8+t.ptrsize:]
277 t.pctab = t.Data
278 functabsize := (int(t.nfunctab)*2 + 1) * t.functabFieldSize()
279 fileoff := t.binary.Uint32(t.functab[functabsize:])
280 t.functab = t.functab[:functabsize]
281 t.filetab = t.Data[fileoff:]
282 t.nfiletab = t.binary.Uint32(t.filetab)
283 t.filetab = t.filetab[:t.nfiletab*4]
284 default:
285 panic("unreachable")
289 // go12Funcs returns a slice of Funcs derived from the Go 1.2+ pcln table.
290 func (t *LineTable) go12Funcs() []Func {
291 // Assume it is malformed and return nil on error.
292 if !disableRecover {
293 defer func() {
294 recover()
298 ft := t.funcTab()
299 funcs := make([]Func, ft.Count())
300 syms := make([]Sym, len(funcs))
301 for i := range funcs {
302 f := &funcs[i]
303 f.Entry = ft.pc(i)
304 f.End = ft.pc(i + 1)
305 info := t.funcData(uint32(i))
306 f.LineTable = t
307 f.FrameSize = int(info.deferreturn())
308 syms[i] = Sym{
309 Value: f.Entry,
310 Type: 'T',
311 Name: t.funcName(info.nameoff()),
312 GoType: 0,
313 Func: f,
315 f.Sym = &syms[i]
317 return funcs
320 // findFunc returns the funcData corresponding to the given program counter.
321 func (t *LineTable) findFunc(pc uint64) funcData {
322 ft := t.funcTab()
323 if pc < ft.pc(0) || pc >= ft.pc(ft.Count()) {
324 return funcData{}
326 idx := sort.Search(int(t.nfunctab), func(i int) bool {
327 return ft.pc(i) > pc
329 idx--
330 return t.funcData(uint32(idx))
333 // readvarint reads, removes, and returns a varint from *pp.
334 func (t *LineTable) readvarint(pp *[]byte) uint32 {
335 var v, shift uint32
336 p := *pp
337 for shift = 0; ; shift += 7 {
338 b := p[0]
339 p = p[1:]
340 v |= (uint32(b) & 0x7F) << shift
341 if b&0x80 == 0 {
342 break
345 *pp = p
346 return v
349 // funcName returns the name of the function found at off.
350 func (t *LineTable) funcName(off uint32) string {
351 if s, ok := t.funcNames[off]; ok {
352 return s
354 i := bytes.IndexByte(t.funcnametab[off:], 0)
355 s := string(t.funcnametab[off : off+uint32(i)])
356 t.funcNames[off] = s
357 return s
360 // stringFrom returns a Go string found at off from a position.
361 func (t *LineTable) stringFrom(arr []byte, off uint32) string {
362 if s, ok := t.strings[off]; ok {
363 return s
365 i := bytes.IndexByte(arr[off:], 0)
366 s := string(arr[off : off+uint32(i)])
367 t.strings[off] = s
368 return s
371 // string returns a Go string found at off.
372 func (t *LineTable) string(off uint32) string {
373 return t.stringFrom(t.funcdata, off)
376 // functabFieldSize returns the size in bytes of a single functab field.
377 func (t *LineTable) functabFieldSize() int {
378 if t.version >= ver118 {
379 return 4
381 return int(t.ptrsize)
384 // funcTab returns t's funcTab.
385 func (t *LineTable) funcTab() funcTab {
386 return funcTab{LineTable: t, sz: t.functabFieldSize()}
389 // funcTab is memory corresponding to a slice of functab structs, followed by an invalid PC.
390 // A functab struct is a PC and a func offset.
391 type funcTab struct {
392 *LineTable
393 sz int // cached result of t.functabFieldSize
396 // Count returns the number of func entries in f.
397 func (f funcTab) Count() int {
398 return int(f.nfunctab)
401 // pc returns the PC of the i'th func in f.
402 func (f funcTab) pc(i int) uint64 {
403 u := f.uint(f.functab[2*i*f.sz:])
404 if f.version >= ver118 {
405 u += f.textStart
407 return u
410 // funcOff returns the funcdata offset of the i'th func in f.
411 func (f funcTab) funcOff(i int) uint64 {
412 return f.uint(f.functab[(2*i+1)*f.sz:])
415 // uint returns the uint stored at b.
416 func (f funcTab) uint(b []byte) uint64 {
417 if f.sz == 4 {
418 return uint64(f.binary.Uint32(b))
420 return f.binary.Uint64(b)
423 // funcData is memory corresponding to an _func struct.
424 type funcData struct {
425 t *LineTable // LineTable this data is a part of
426 data []byte // raw memory for the function
429 // funcData returns the ith funcData in t.functab.
430 func (t *LineTable) funcData(i uint32) funcData {
431 data := t.funcdata[t.funcTab().funcOff(int(i)):]
432 return funcData{t: t, data: data}
435 // IsZero reports whether f is the zero value.
436 func (f funcData) IsZero() bool {
437 return f.t == nil && f.data == nil
440 // entryPC returns the func's entry PC.
441 func (f *funcData) entryPC() uint64 {
442 // In Go 1.18, the first field of _func changed
443 // from a uintptr entry PC to a uint32 entry offset.
444 if f.t.version >= ver118 {
445 // TODO: support multiple text sections.
446 // See runtime/symtab.go:(*moduledata).textAddr.
447 return uint64(f.t.binary.Uint32(f.data)) + f.t.textStart
449 return f.t.uintptr(f.data)
452 func (f funcData) nameoff() uint32 { return f.field(1) }
453 func (f funcData) deferreturn() uint32 { return f.field(3) }
454 func (f funcData) pcfile() uint32 { return f.field(5) }
455 func (f funcData) pcln() uint32 { return f.field(6) }
456 func (f funcData) cuOffset() uint32 { return f.field(8) }
458 // field returns the nth field of the _func struct.
459 // It panics if n == 0 or n > 9; for n == 0, call f.entryPC.
460 // Most callers should use a named field accessor (just above).
461 func (f funcData) field(n uint32) uint32 {
462 if n == 0 || n > 9 {
463 panic("bad funcdata field")
465 // In Go 1.18, the first field of _func changed
466 // from a uintptr entry PC to a uint32 entry offset.
467 sz0 := f.t.ptrsize
468 if f.t.version >= ver118 {
469 sz0 = 4
471 off := sz0 + (n-1)*4 // subsequent fields are 4 bytes each
472 data := f.data[off:]
473 return f.t.binary.Uint32(data)
476 // step advances to the next pc, value pair in the encoded table.
477 func (t *LineTable) step(p *[]byte, pc *uint64, val *int32, first bool) bool {
478 uvdelta := t.readvarint(p)
479 if uvdelta == 0 && !first {
480 return false
482 if uvdelta&1 != 0 {
483 uvdelta = ^(uvdelta >> 1)
484 } else {
485 uvdelta >>= 1
487 vdelta := int32(uvdelta)
488 pcdelta := t.readvarint(p) * t.quantum
489 *pc += uint64(pcdelta)
490 *val += vdelta
491 return true
494 // pcvalue reports the value associated with the target pc.
495 // off is the offset to the beginning of the pc-value table,
496 // and entry is the start PC for the corresponding function.
497 func (t *LineTable) pcvalue(off uint32, entry, targetpc uint64) int32 {
498 p := t.pctab[off:]
500 val := int32(-1)
501 pc := entry
502 for t.step(&p, &pc, &val, pc == entry) {
503 if targetpc < pc {
504 return val
507 return -1
510 // findFileLine scans one function in the binary looking for a
511 // program counter in the given file on the given line.
512 // It does so by running the pc-value tables mapping program counter
513 // to file number. Since most functions come from a single file, these
514 // are usually short and quick to scan. If a file match is found, then the
515 // code goes to the expense of looking for a simultaneous line number match.
516 func (t *LineTable) findFileLine(entry uint64, filetab, linetab uint32, filenum, line int32, cutab []byte) uint64 {
517 if filetab == 0 || linetab == 0 {
518 return 0
521 fp := t.pctab[filetab:]
522 fl := t.pctab[linetab:]
523 fileVal := int32(-1)
524 filePC := entry
525 lineVal := int32(-1)
526 linePC := entry
527 fileStartPC := filePC
528 for t.step(&fp, &filePC, &fileVal, filePC == entry) {
529 fileIndex := fileVal
530 if t.version == ver116 || t.version == ver118 {
531 fileIndex = int32(t.binary.Uint32(cutab[fileVal*4:]))
533 if fileIndex == filenum && fileStartPC < filePC {
534 // fileIndex is in effect starting at fileStartPC up to
535 // but not including filePC, and it's the file we want.
536 // Run the PC table looking for a matching line number
537 // or until we reach filePC.
538 lineStartPC := linePC
539 for linePC < filePC && t.step(&fl, &linePC, &lineVal, linePC == entry) {
540 // lineVal is in effect until linePC, and lineStartPC < filePC.
541 if lineVal == line {
542 if fileStartPC <= lineStartPC {
543 return lineStartPC
545 if fileStartPC < linePC {
546 return fileStartPC
549 lineStartPC = linePC
552 fileStartPC = filePC
554 return 0
557 // go12PCToLine maps program counter to line number for the Go 1.2+ pcln table.
558 func (t *LineTable) go12PCToLine(pc uint64) (line int) {
559 defer func() {
560 if !disableRecover && recover() != nil {
561 line = -1
565 f := t.findFunc(pc)
566 if f.IsZero() {
567 return -1
569 entry := f.entryPC()
570 linetab := f.pcln()
571 return int(t.pcvalue(linetab, entry, pc))
574 // go12PCToFile maps program counter to file name for the Go 1.2+ pcln table.
575 func (t *LineTable) go12PCToFile(pc uint64) (file string) {
576 defer func() {
577 if !disableRecover && recover() != nil {
578 file = ""
582 f := t.findFunc(pc)
583 if f.IsZero() {
584 return ""
586 entry := f.entryPC()
587 filetab := f.pcfile()
588 fno := t.pcvalue(filetab, entry, pc)
589 if t.version == ver12 {
590 if fno <= 0 {
591 return ""
593 return t.string(t.binary.Uint32(t.filetab[4*fno:]))
595 // Go ≥ 1.16
596 if fno < 0 { // 0 is valid for ≥ 1.16
597 return ""
599 cuoff := f.cuOffset()
600 if fnoff := t.binary.Uint32(t.cutab[(cuoff+uint32(fno))*4:]); fnoff != ^uint32(0) {
601 return t.stringFrom(t.filetab, fnoff)
603 return ""
606 // go12LineToPC maps a (file, line) pair to a program counter for the Go 1.2+ pcln table.
607 func (t *LineTable) go12LineToPC(file string, line int) (pc uint64) {
608 defer func() {
609 if !disableRecover && recover() != nil {
610 pc = 0
614 t.initFileMap()
615 filenum, ok := t.fileMap[file]
616 if !ok {
617 return 0
620 // Scan all functions.
621 // If this turns out to be a bottleneck, we could build a map[int32][]int32
622 // mapping file number to a list of functions with code from that file.
623 var cutab []byte
624 for i := uint32(0); i < t.nfunctab; i++ {
625 f := t.funcData(i)
626 entry := f.entryPC()
627 filetab := f.pcfile()
628 linetab := f.pcln()
629 if t.version == ver116 || t.version == ver118 {
630 cutab = t.cutab[f.cuOffset()*4:]
632 pc := t.findFileLine(entry, filetab, linetab, int32(filenum), int32(line), cutab)
633 if pc != 0 {
634 return pc
637 return 0
640 // initFileMap initializes the map from file name to file number.
641 func (t *LineTable) initFileMap() {
642 t.mu.Lock()
643 defer t.mu.Unlock()
645 if t.fileMap != nil {
646 return
648 m := make(map[string]uint32)
650 if t.version == ver12 {
651 for i := uint32(1); i < t.nfiletab; i++ {
652 s := t.string(t.binary.Uint32(t.filetab[4*i:]))
653 m[s] = i
655 } else {
656 var pos uint32
657 for i := uint32(0); i < t.nfiletab; i++ {
658 s := t.stringFrom(t.filetab, pos)
659 m[s] = pos
660 pos += uint32(len(s) + 1)
663 t.fileMap = m
666 // go12MapFiles adds to m a key for every file in the Go 1.2 LineTable.
667 // Every key maps to obj. That's not a very interesting map, but it provides
668 // a way for callers to obtain the list of files in the program.
669 func (t *LineTable) go12MapFiles(m map[string]*Obj, obj *Obj) {
670 if !disableRecover {
671 defer func() {
672 recover()
676 t.initFileMap()
677 for file := range t.fileMap {
678 m[file] = obj
682 // disableRecover causes this package not to swallow panics.
683 // This is useful when making changes.
684 const disableRecover = false