Skip to content

Commit

Permalink
runtime: streamline moduledata.textAddr
Browse files Browse the repository at this point in the history
Accept a uint32 instead of a uintptr to make call sites simpler.

Do less work in the common case in which len(textsectmap) == 1.

Change-Id: Idd6cdc3fdad7a9356864c83790463b5d3000171b
Reviewed-on: https://go-review.googlesource.com/c/go/+/354132
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
  • Loading branch information
josharian committed Oct 6, 2021
1 parent f580b75 commit 8238f82
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/runtime/plugin.go
Expand Up @@ -96,7 +96,7 @@ func plugin_lastmoduleinit() (path string, syms map[string]interface{}, errstr s
func pluginftabverify(md *moduledata) {
badtable := false
for i := 0; i < len(md.ftab); i++ {
entry := md.textAddr(uintptr(md.ftab[i].entryoff))
entry := md.textAddr(md.ftab[i].entryoff)
if md.minpc <= entry && entry <= md.maxpc {
continue
}
Expand Down
28 changes: 13 additions & 15 deletions src/runtime/symtab.go
Expand Up @@ -622,10 +622,10 @@ func moduledataverify1(datap *moduledata) {
}
}

min := datap.textAddr(uintptr(datap.ftab[0].entryoff))
min := datap.textAddr(datap.ftab[0].entryoff)
// The max PC is outside of the text section.
// Subtract 1 to get a PC inside the text section, look it up, then add 1 back in.
max := datap.textAddr(uintptr(datap.ftab[nftab].entryoff-1)) + 1
max := datap.textAddr(datap.ftab[nftab].entryoff-1) + 1
if datap.minpc != min || datap.maxpc != max {
println("minpc=", hex(datap.minpc), "min=", hex(min), "maxpc=", hex(datap.maxpc), "max=", hex(max))
throw("minpc or maxpc invalid")
Expand Down Expand Up @@ -656,22 +656,20 @@ func moduledataverify1(datap *moduledata) {
//
// It is nosplit because it is part of the findfunc implementation.
//go:nosplit
func (md *moduledata) textAddr(off uintptr) uintptr {
var res uintptr
func (md *moduledata) textAddr(off32 uint32) uintptr {
off := uintptr(off32)
res := md.text + off
if len(md.textsectmap) > 1 {
for i := range md.textsectmap {
if off >= md.textsectmap[i].vaddr && off < md.textsectmap[i].end {
res = md.textsectmap[i].baseaddr + off - md.textsectmap[i].vaddr
break
}
}
} else {
// single text section
res = md.text + off
}
if res > md.etext && GOARCH != "wasm" { // on wasm, functions do not live in the same address space as the linear memory
println("runtime: textOff", hex(off), "out of range", hex(md.text), "-", hex(md.etext))
throw("runtime: text offset out of range")
if res > md.etext && GOARCH != "wasm" { // on wasm, functions do not live in the same address space as the linear memory
println("runtime: textAddr", hex(res), "out of range", hex(md.text), "-", hex(md.etext))
throw("runtime: text offset out of range")
}
}
return res
}
Expand Down Expand Up @@ -783,7 +781,7 @@ func (f *_func) isInlined() bool {

// entry returns the entry PC for f.
func (f funcInfo) entry() uintptr {
return f.datap.textAddr(uintptr(f.entryoff))
return f.datap.textAddr(f.entryoff)
}

// findfunc looks up function metadata for a PC.
Expand Down Expand Up @@ -819,18 +817,18 @@ func findfunc(pc uintptr) funcInfo {
if idx >= uint32(len(datap.ftab)) {
idx = uint32(len(datap.ftab) - 1)
}
if pc < datap.textAddr(uintptr(datap.ftab[idx].entryoff)) {
if pc < datap.textAddr(datap.ftab[idx].entryoff) {
// The idx might reference a function address that
// is higher than the pcOff being searched, so search backward until the matching address is found.
for datap.textAddr(uintptr(datap.ftab[idx].entryoff)) > pc && idx > 0 {
for datap.textAddr(datap.ftab[idx].entryoff) > pc && idx > 0 {
idx--
}
if idx == 0 {
throw("findfunc: bad findfunctab entry idx")
}
} else {
// linear search to find func with pc >= entry.
for datap.textAddr(uintptr(datap.ftab[idx+1].entryoff)) <= pc {
for datap.textAddr(datap.ftab[idx+1].entryoff) <= pc {
idx++
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/type.go
Expand Up @@ -288,7 +288,7 @@ func (t *_type) textOff(off textOff) unsafe.Pointer {
}
return res
}
res := md.textAddr(uintptr(off))
res := md.textAddr(uint32(off))
return unsafe.Pointer(res)
}

Expand Down

0 comments on commit 8238f82

Please sign in to comment.