cmd/internal/objfile, debug/gosym: use the address of runtime.text as textStart

Tools like objdump uses the pcln table to find the line number of
a given PC. For a PIE binary, at least in some cases such as on
macOS 12 with ld64-711, the table contains unrelocated address,
which does not match the address in the symbol table, causing the
lookup to fail.

In Go 1.18 the pcln table is essentually position independent,
except the start PC. Instead of reading the static content from
the table, use the PC of runtime.text from the symbol table.

While here, change the type of textStart to uint64. What matters
here is the word size of the target program, not the host, so it
shouldn't be uintptr.

Fixes #49700.

Change-Id: I517d79be7ba02dd4dd0275e75a11a136b08d76cd
Reviewed-on: https://go-review.googlesource.com/c/go/+/366695
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
Cherry Mui 2021-11-23 18:03:47 -05:00
parent 14f2b2a4c5
commit b38ab0ac5f
2 changed files with 13 additions and 4 deletions

View file

@ -152,6 +152,15 @@ func (e *Entry) PCLineTable() (Liner, error) {
if err != nil {
return nil, err
}
syms, err := e.raw.symbols()
if err == nil {
for _, s := range syms {
if s.Name == "runtime.text" {
textStart = s.Addr
break
}
}
}
return gosym.NewTable(symtab, gosym.NewLineTable(pclntab, textStart))
}

View file

@ -54,7 +54,7 @@ type LineTable struct {
binary binary.ByteOrder
quantum uint32
ptrsize uint32
textStart uintptr // address of runtime.text symbol (1.18+)
textStart uint64 // address of runtime.text symbol (1.18+)
funcnametab []byte
cutab []byte
funcdata []byte
@ -249,7 +249,7 @@ func (t *LineTable) parsePclnTab() {
case ver118:
t.nfunctab = uint32(offset(0))
t.nfiletab = uint32(offset(1))
t.textStart = uintptr(offset(2))
t.textStart = t.PC // use the start PC instead of reading from the table, which may be unrelocated
t.funcnametab = data(3)
t.cutab = data(4)
t.filetab = data(5)
@ -402,7 +402,7 @@ func (f funcTab) Count() int {
func (f funcTab) pc(i int) uint64 {
u := f.uint(f.functab[2*i*f.sz:])
if f.version >= ver118 {
u += uint64(f.textStart)
u += f.textStart
}
return u
}
@ -444,7 +444,7 @@ func (f *funcData) entryPC() uint64 {
if f.t.version >= ver118 {
// TODO: support multiple text sections.
// See runtime/symtab.go:(*moduledata).textAddr.
return uint64(f.t.binary.Uint32(f.data)) + uint64(f.t.textStart)
return uint64(f.t.binary.Uint32(f.data)) + f.t.textStart
}
return f.t.uintptr(f.data)
}