diff options
| author | Paul E. Murphy <murp@ibm.com> | 2022-04-15 17:06:48 -0500 |
|---|---|---|
| committer | Paul Murphy <murp@ibm.com> | 2022-05-06 17:56:28 +0000 |
| commit | 9c9090eb1da540c7d757df0c60423319a28759d3 (patch) | |
| tree | b4f296409d38b8f55654d523cc47a34f2395f16d /src/cmd/link | |
| parent | 4a5711c3cf944b3ff51af261166ef34b2ed22b8a (diff) | |
| download | go-9c9090eb1da540c7d757df0c60423319a28759d3.tar.xz | |
cmd/link: generate PPC64 ABI register save/restore functions if needed
They are usually needed when internally linking gcc code
compiled with -Os. These are typically generated by ld
or gold, but are missing when linking internally.
The PPC64 ELF ABI describes a set of functions to save/restore
non-volatile, callee-save registers using R1/R0/R12:
_savegpr0_n: Save Rn-R31 relative to R1, save LR (in R0), return
_restgpr0_n: Restore Rn-R31 from R1, and return to saved LR
_savefpr_n: Save Fn-F31 based on R1, and save LR (in R0), return
_restfpr_n: Restore Fn-F31 from R1, and return to 16(R1)
_savegpr1_n: Save Rn-R31 based on R12, return
_restgpr1_n: Restore Rn-R31 based on R12, return
_savevr_m: Save VRm-VR31 based on R0, R12 is scratch, return
_restvr_m: Restore VRm-VR31 based on R0, R12 is scratch, return
m is a value 20<=m<=31
n is a value 14<=n<=31
Add several new functions similar to those suggested by the
PPC64 ELFv2 ABI. And update the linker to scan external relocs
for these calls, and redirect them to runtime.elf_<func>+offset
in runtime/asm_ppc64x.go.
Similarly, code which generates plt stubs is moved into
a dedicated function. This avoids an extra scan of relocs.
fixes #52336
Change-Id: I2f0f8b5b081a7b294dff5c92b4b1db8eba9a9400
Reviewed-on: https://go-review.googlesource.com/c/go/+/400796
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Paul Murphy <murp@ibm.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd/link')
| -rw-r--r-- | src/cmd/link/internal/ppc64/asm.go | 180 |
1 files changed, 139 insertions, 41 deletions
diff --git a/src/cmd/link/internal/ppc64/asm.go b/src/cmd/link/internal/ppc64/asm.go index 73c2718a33..5d5fbe2a97 100644 --- a/src/cmd/link/internal/ppc64/asm.go +++ b/src/cmd/link/internal/ppc64/asm.go @@ -40,10 +40,11 @@ import ( "encoding/binary" "fmt" "log" + "strconv" "strings" ) -func genplt(ctxt *ld.Link, ldr *loader.Loader) { +func genpltstub(ctxt *ld.Link, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (sym loader.Sym, firstUse bool) { // The ppc64 ABI PLT has similar concepts to other // architectures, but is laid out quite differently. When we // see an R_PPC64_REL24 relocation to a dynamic symbol @@ -92,53 +93,82 @@ func genplt(ctxt *ld.Link, ldr *loader.Loader) { // // This assumes "case 1" from the ABI, where the caller needs // us to save and restore the TOC pointer. - var stubs []loader.Sym - for _, s := range ctxt.Textp { - relocs := ldr.Relocs(s) - for i := 0; i < relocs.Count(); i++ { - r := relocs.At(i) - if r.Type() != objabi.ElfRelocOffset+objabi.RelocType(elf.R_PPC64_REL24) || ldr.SymType(r.Sym()) != sym.SDYNIMPORT { - continue - } - // Reserve PLT entry and generate symbol - // resolver - addpltsym(ctxt, ldr, r.Sym()) + // Reserve PLT entry and generate symbol + // resolver + addpltsym(ctxt, ldr, r.Sym()) - // Generate call stub. Important to note that we're looking - // up the stub using the same version as the parent symbol (s), - // needed so that symtoc() will select the right .TOC. symbol - // when processing the stub. In older versions of the linker - // this was done by setting stub.Outer to the parent, but - // if the stub has the right version initially this is not needed. - n := fmt.Sprintf("%s.%s", ldr.SymName(s), ldr.SymName(r.Sym())) - stub := ldr.CreateSymForUpdate(n, ldr.SymVersion(s)) - if stub.Size() == 0 { - stubs = append(stubs, stub.Sym()) - gencallstub(ctxt, ldr, 1, stub, r.Sym()) - } + // Generate call stub. Important to note that we're looking + // up the stub using the same version as the parent symbol (s), + // needed so that symtoc() will select the right .TOC. symbol + // when processing the stub. In older versions of the linker + // this was done by setting stub.Outer to the parent, but + // if the stub has the right version initially this is not needed. + n := fmt.Sprintf("%s.%s", ldr.SymName(s), ldr.SymName(r.Sym())) + stub := ldr.CreateSymForUpdate(n, ldr.SymVersion(s)) + firstUse = stub.Size() == 0 + if firstUse { + gencallstub(ctxt, ldr, 1, stub, r.Sym()) + } - // Update the relocation to use the call stub - r.SetSym(stub.Sym()) + // Update the relocation to use the call stub + r.SetSym(stub.Sym()) - // Make the symbol writeable so we can fixup toc. - su := ldr.MakeSymbolUpdater(s) - su.MakeWritable() - p := su.Data() + // Make the symbol writeable so we can fixup toc. + su := ldr.MakeSymbolUpdater(s) + su.MakeWritable() + p := su.Data() - // Check for toc restore slot (a nop), and replace with toc restore. - var nop uint32 - if len(p) >= int(r.Off()+8) { - nop = ctxt.Arch.ByteOrder.Uint32(p[r.Off()+4:]) - } - if nop != 0x60000000 { - ldr.Errorf(s, "Symbol %s is missing toc restoration slot at offset %d", ldr.SymName(s), r.Off()+4) + // Check for toc restore slot (a nop), and replace with toc restore. + var nop uint32 + if len(p) >= int(r.Off()+8) { + nop = ctxt.Arch.ByteOrder.Uint32(p[r.Off()+4:]) + } + if nop != 0x60000000 { + ldr.Errorf(s, "Symbol %s is missing toc restoration slot at offset %d", ldr.SymName(s), r.Off()+4) + } + const o1 = 0xe8410018 // ld r2,24(r1) + ctxt.Arch.ByteOrder.PutUint32(p[r.Off()+4:], o1) + + return stub.Sym(), firstUse +} + +// Scan relocs and generate PLT stubs and generate/fixup ABI defined functions created by the linker +func genstubs(ctxt *ld.Link, ldr *loader.Loader) { + var stubs []loader.Sym + var abifuncs []loader.Sym + for _, s := range ctxt.Textp { + relocs := ldr.Relocs(s) + for i := 0; i < relocs.Count(); i++ { + if r := relocs.At(i); r.Type() == objabi.ElfRelocOffset+objabi.RelocType(elf.R_PPC64_REL24) { + switch ldr.SymType(r.Sym()) { + case sym.SDYNIMPORT: + // This call goes throught the PLT, generate and call through a PLT stub. + if sym, firstUse := genpltstub(ctxt, ldr, r, s); firstUse { + stubs = append(stubs, sym) + } + + case sym.SXREF: + // Is this an ELF ABI defined function which is (in practice) + // generated by the linker to save/restore callee save registers? + // These are defined similarly for both PPC64 ELF and ELFv2. + targName := ldr.SymName(r.Sym()) + if strings.HasPrefix(targName, "_save") || strings.HasPrefix(targName, "_rest") { + if sym, firstUse := rewriteABIFuncReloc(ctxt, ldr, targName, r); firstUse { + abifuncs = append(abifuncs, sym) + } + } + } } - const o1 = 0xe8410018 // ld r2,24(r1) - ctxt.Arch.ByteOrder.PutUint32(p[r.Off()+4:], o1) } } - // Put call stubs at the beginning (instead of the end). + + // Append any usage of the go versions of ELF save/restore + // functions to the end of the callstub list to minimize + // chances a trampoline might be needed. + stubs = append(stubs, abifuncs...) + + // Put stubs at the beginning (instead of the end). // So when resolving the relocations to calls to the stubs, // the addresses are known and trampolines can be inserted // when necessary. @@ -202,13 +232,74 @@ func genaddmoduledata(ctxt *ld.Link, ldr *loader.Loader) { o(0x4e800020) } +// Rewrite ELF (v1 or v2) calls to _savegpr0_n, _savegpr1_n, _savefpr_n, _restfpr_n, _savevr_m, or +// _restvr_m (14<=n<=31, 20<=m<=31). Redirect them to runtime.elf_restgpr0+(n-14)*4, +// runtime.elf_restvr+(m-20)*8, and similar. +// +// These functions are defined in the ELFv2 ABI (generated when using gcc -Os option) to save and +// restore callee-saved registers (as defined in the PPC64 ELF ABIs) from registers n or m to 31 of +// the named type. R12 and R0 are sometimes used in exceptional ways described in the ABI. +// +// Final note, this is only needed when linking internally. The external linker will generate these +// functions if they are used. +func rewriteABIFuncReloc(ctxt *ld.Link, ldr *loader.Loader, tname string, r loader.Reloc) (sym loader.Sym, firstUse bool) { + s := strings.Split(tname, "_") + // A valid call will split like {"", "savegpr0", "20"} + if len(s) != 3 { + return 0, false // Not an abi func. + } + minReg := 14 // _savegpr0_{n}, _savegpr1_{n}, _savefpr_{n}, 14 <= n <= 31 + offMul := 4 // 1 instruction per register op. + switch s[1] { + case "savegpr0", "savegpr1", "savefpr": + case "restgpr0", "restgpr1", "restfpr": + case "savevr", "restvr": + minReg = 20 // _savevr_{n} or _restvr_{n}, 20 <= n <= 31 + offMul = 8 // 2 instructions per register op. + default: + return 0, false // Not an abi func + } + n, e := strconv.Atoi(s[2]) + if e != nil || n < minReg || n > 31 || r.Add() != 0 { + return 0, false // Invalid register number, or non-zero addend. Not an abi func. + } + + // tname is a valid relocation to an ABI defined register save/restore function. Re-relocate + // them to a go version of these functions in runtime/asm_ppc64x.s + ts := ldr.LookupOrCreateSym("runtime.elf_"+s[1], 0) + r.SetSym(ts) + r.SetAdd(int64((n - minReg) * offMul)) + firstUse = !ldr.AttrReachable(ts) + if firstUse { + ldr.SetAttrReachable(ts, true) + // This function only becomes reachable now. It has been dropped from + // the text section (it was unreachable until now), it needs included. + // + // Similarly, TOC regeneration should not happen for these functions, + // remove it from this save/restore function. + if ldr.AttrShared(ts) { + sb := ldr.MakeSymbolUpdater(ts) + sb.SetData(sb.Data()[8:]) + sb.SetSize(sb.Size() - 8) + relocs := sb.Relocs() + // Only one PCREL reloc to .TOC. should be present. + if relocs.Count() != 1 { + log.Fatalf("Unexpected number of relocs in %s\n", ldr.SymName(ts)) + } + sb.ResetRelocs() + + } + } + return ts, firstUse +} + func gentext(ctxt *ld.Link, ldr *loader.Loader) { if ctxt.DynlinkingGo() { genaddmoduledata(ctxt, ldr) } if ctxt.LinkMode == ld.LinkInternal { - genplt(ctxt, ldr) + genstubs(ctxt, ldr) } } @@ -863,6 +954,13 @@ func gentramp(ctxt *ld.Link, ldr *loader.Loader, tramp *loader.SymbolBuilder, ta o1 = uint32(0x3c000000) | 12<<21 // lis r12,targetaddr hi o2 = uint32(0x38000000) | 12<<21 | 12<<16 // addi r12,r12,targetaddr lo + // ELFv2 save/restore functions use R0/R12 in special ways, therefore trampolines + // as generated here will not always work correctly. + if strings.HasPrefix(ldr.SymName(target), "runtime.elf_") { + log.Fatalf("Internal linker does not support trampolines to ELFv2 ABI"+ + " register save/restore function %s", ldr.SymName(target)) + } + t := ldr.SymValue(target) if t == 0 || r2Valid(ctxt) || ctxt.IsExternal() { // Target address is unknown, generate relocations |
