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.
18 // version of the pclntab
22 verUnknown version
= iota
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 {
47 // This mutex is used to keep parsing of pclntab synchronous.
50 // Contains the version of the pclntab section.
53 // Go 1.2/1.16/1.18 state
54 binary binary
.ByteOrder
57 textStart
uint64 // address of runtime.text symbol (1.18+)
64 pctab
[]byte // points to the pctables.
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
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 {
99 val
:= binary
.BigEndian
.Uint32(b
)
105 line
-= int(code
- 64)
107 pc
+= oldQuantum
* uint64(code
-128)
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 {
125 return t
.go12PCToLine(pc
)
127 _
, _
, line
:= t
.parse(pc
, -1)
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 {
139 _
, pc
, line1
:= t
.parse(maxpc
, line
)
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 {
169 return t
.version
>= ver12
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 {
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() {
191 if t
.version
!= verUnknown
{
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.
204 // If we panic parsing, assume it's a Go 1.1 pclntab.
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
216 var possibleVersion version
217 leMagic
:= binary
.LittleEndian
.Uint32(t
.Data
)
218 beMagic
:= binary
.BigEndian
.Uint32(t
.Data
)
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
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
{
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)
259 functabsize
:= (int(t
.nfunctab
)*2 + 1) * t
.functabFieldSize()
260 t
.functab
= t
.functab
[:functabsize
]
262 t
.nfunctab
= uint32(offset(0))
263 t
.nfiletab
= uint32(offset(1))
264 t
.funcnametab
= data(2)
270 functabsize
:= (int(t
.nfunctab
)*2 + 1) * t
.functabFieldSize()
271 t
.functab
= t
.functab
[:functabsize
]
273 t
.nfunctab
= uint32(t
.uintptr(t
.Data
[8:]))
275 t
.funcnametab
= t
.Data
276 t
.functab
= t
.Data
[8+t
.ptrsize
:]
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]
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.
299 funcs
:= make([]Func
, ft
.Count())
300 syms
:= make([]Sym
, len(funcs
))
301 for i
:= range funcs
{
305 info
:= t
.funcData(uint32(i
))
307 f
.FrameSize
= int(info
.deferreturn())
311 Name
: t
.funcName(info
.nameoff()),
320 // findFunc returns the funcData corresponding to the given program counter.
321 func (t
*LineTable
) findFunc(pc
uint64) funcData
{
323 if pc
< ft
.pc(0) || pc
>= ft
.pc(ft
.Count()) {
326 idx
:= sort
.Search(int(t
.nfunctab
), func(i
int) bool {
330 return t
.funcData(uint32(idx
))
333 // readvarint reads, removes, and returns a varint from *pp.
334 func (t
*LineTable
) readvarint(pp
*[]byte) uint32 {
337 for shift
= 0; ; shift
+= 7 {
340 v |
= (uint32(b
) & 0x7F) << shift
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
{
354 i
:= bytes
.IndexByte(t
.funcnametab
[off
:], 0)
355 s
:= string(t
.funcnametab
[off
: off
+uint32(i
)])
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
{
365 i
:= bytes
.IndexByte(arr
[off
:], 0)
366 s
:= string(arr
[off
: off
+uint32(i
)])
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
{
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 {
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
{
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 {
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 {
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.
468 if f
.t
.version
>= ver118
{
471 off
:= sz0
+ (n
-1)*4 // subsequent fields are 4 bytes each
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
{
483 uvdelta
= ^(uvdelta
>> 1)
487 vdelta
:= int32(uvdelta
)
488 pcdelta
:= t
.readvarint(p
) * t
.quantum
489 *pc
+= uint64(pcdelta
)
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 {
502 for t
.step(&p
, &pc
, &val
, pc
== entry
) {
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 {
521 fp
:= t
.pctab
[filetab
:]
522 fl
:= t
.pctab
[linetab
:]
527 fileStartPC
:= filePC
528 for t
.step(&fp
, &filePC
, &fileVal
, filePC
== entry
) {
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.
542 if fileStartPC
<= lineStartPC
{
545 if fileStartPC
< linePC
{
557 // go12PCToLine maps program counter to line number for the Go 1.2+ pcln table.
558 func (t
*LineTable
) go12PCToLine(pc
uint64) (line
int) {
560 if !disableRecover
&& recover() != nil {
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) {
577 if !disableRecover
&& recover() != nil {
587 filetab
:= f
.pcfile()
588 fno
:= t
.pcvalue(filetab
, entry
, pc
)
589 if t
.version
== ver12
{
593 return t
.string(t
.binary
.Uint32(t
.filetab
[4*fno
:]))
596 if fno
< 0 { // 0 is valid for ≥ 1.16
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
)
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) {
609 if !disableRecover
&& recover() != nil {
615 filenum
, ok
:= t
.fileMap
[file
]
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.
624 for i
:= uint32(0); i
< t
.nfunctab
; i
++ {
627 filetab
:= f
.pcfile()
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
)
640 // initFileMap initializes the map from file name to file number.
641 func (t
*LineTable
) initFileMap() {
645 if t
.fileMap
!= nil {
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
:]))
657 for i
:= uint32(0); i
< t
.nfiletab
; i
++ {
658 s
:= t
.stringFrom(t
.filetab
, pos
)
660 pos
+= uint32(len(s
) + 1)
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
) {
677 for file
:= range t
.fileMap
{
682 // disableRecover causes this package not to swallow panics.
683 // This is useful when making changes.
684 const disableRecover
= false