]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link,runtime: make textsectmap fields more convenient for runtime
authorJosh Bleecher Snyder <josharian@gmail.com>
Tue, 5 Oct 2021 18:26:25 +0000 (11:26 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 6 Oct 2021 19:54:45 +0000 (19:54 +0000)
They're only used in a single place.
Instead of calculating the end every time,
calculate it in the linker.

It'd be nice to recalculate baseaddr-vaddr,
but that generates relocations that are too large.

While we're here, remove some pointless uintptr -> uintptr conversions.

Change-Id: I91758f9bff11b365bc3a63fee172dbdc3d90b966
Reviewed-on: https://go-review.googlesource.com/c/go/+/354089
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/link/internal/ld/symtab.go
src/runtime/symtab.go

index 8eca250131047f1433fb8e29ef57bae0f789e231..4be2634f44934663461cedef500a09a32f8a21a2 100644 (file)
@@ -406,22 +406,21 @@ func textsectionmap(ctxt *Link) (loader.Sym, uint32) {
                if sect.Name != ".text" {
                        break
                }
-               off = t.SetUint(ctxt.Arch, off, sect.Vaddr-textbase)
-               off = t.SetUint(ctxt.Arch, off, sect.Length)
-               if n == 0 {
-                       s := ldr.Lookup("runtime.text", 0)
-                       if s == 0 {
-                               ctxt.Errorf(s, "Unable to find symbol runtime.text\n")
-                       }
-                       off = t.SetAddr(ctxt.Arch, off, s)
-
-               } else {
-                       s := ldr.Lookup(fmt.Sprintf("runtime.text.%d", n), 0)
-                       if s == 0 {
-                               ctxt.Errorf(s, "Unable to find symbol runtime.text.%d\n", n)
-                       }
-                       off = t.SetAddr(ctxt.Arch, off, s)
+               // The fields written should match runtime/symtab.go:textsect.
+               // They are designed to minimize runtime calculations.
+               vaddr := sect.Vaddr - textbase
+               off = t.SetUint(ctxt.Arch, off, vaddr) // field vaddr
+               end := vaddr + sect.Length
+               off = t.SetUint(ctxt.Arch, off, end) // field end
+               name := "runtime.text"
+               if n != 0 {
+                       name = fmt.Sprintf("runtime.text.%d", n)
+               }
+               s := ldr.Lookup(name, 0)
+               if s == 0 {
+                       ctxt.Errorf(s, "Unable to find symbol %s\n", name)
                }
+               off = t.SetAddr(ctxt.Arch, off, s) // field baseaddr
                n++
        }
        return t.Sym(), uint32(n)
index e26c05bc0c50d13bbde73cf534202ce61fa4e872..37abdb6dfacb471f4115186d2b77b58095b5ecc7 100644 (file)
@@ -561,7 +561,7 @@ type functab struct {
 
 type textsect struct {
        vaddr    uintptr // prelinked section vaddr
-       length   uintptr // section length
+       end      uintptr // vaddr + section length
        baseaddr uintptr // relocated section address
 }
 
@@ -650,9 +650,9 @@ func moduledataverify1(datap *moduledata) {
 // To resolve the large text issue, the text is split into multiple text sections
 // to allow the linker to generate long calls when necessary.
 // When this happens, the vaddr for each text section is set to its offset within the text.
-// Each function's offset is compared against the section vaddrs and sizes to determine the containing section.
+// Each function's offset is compared against the section vaddrs and ends to determine the containing section.
 // Then the section relative offset is added to the section's
-// relocated baseaddr to compute the function addess.
+// relocated baseaddr to compute the function address.
 //
 // It is nosplit because it is part of the findfunc implementation.
 //go:nosplit
@@ -660,16 +660,14 @@ func (md *moduledata) textAddr(off uintptr) uintptr {
        var res uintptr
        if len(md.textsectmap) > 1 {
                for i := range md.textsectmap {
-                       sectaddr := md.textsectmap[i].vaddr
-                       sectlen := md.textsectmap[i].length
-                       if uintptr(off) >= sectaddr && uintptr(off) < sectaddr+sectlen {
-                               res = md.textsectmap[i].baseaddr + uintptr(off) - uintptr(md.textsectmap[i].vaddr)
+                       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 + uintptr(off)
+               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))