Skip to content

Commit 0bd1a22

Browse files
pmurthanm
authored andcommitted
[release-branch.go1.21] cmd/internal/obj/ppc64: don't modify runtime.elf_* symbols
The runtime.elf_* symbols are assembly functions which are used to support the gcc/llvm -Os option when used with cgo. When compiling Go for shared code, we attempt to strip out the TOC regenation code added by the go assembler for these symbols. This causes the symbol to no longer appear as an assembly function which causes problems later on when handling other implicit symbols. Avoid adding a TOC regeneration prologue to these functions to avoid this issue. Fixes #66411 Change-Id: Icbf8e4438d177082a57bb228e39b232e7a0d7ada Reviewed-on: https://go-review.googlesource.com/c/go/+/571835 Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Paul Murphy <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Lynn Boger <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/572876
1 parent 140b37d commit 0bd1a22

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/cmd/go/testdata/script/test_ppc64_linker_funcs.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ go build -ldflags='-linkmode=internal'
1414
exec ./abitest
1515
stdout success
1616

17+
go build -buildmode=pie -o abitest.pie -ldflags='-linkmode=internal'
18+
exec ./abitest.pie
19+
stdout success
20+
1721
-- go.mod --
1822
module abitest
1923

src/cmd/internal/obj/ppc64/obj9.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,25 @@ import (
3636
"cmd/internal/sys"
3737
"internal/abi"
3838
"log"
39+
"strings"
3940
)
4041

42+
// Is this a symbol which should never have a TOC prologue generated?
43+
// These are special functions which should not have a TOC regeneration
44+
// prologue.
45+
func isNOTOCfunc(name string) bool {
46+
switch {
47+
case name == "runtime.duffzero":
48+
return true
49+
case name == "runtime.duffcopy":
50+
return true
51+
case strings.HasPrefix(name, "runtime.elf_"):
52+
return true
53+
default:
54+
return false
55+
}
56+
}
57+
4158
func progedit(ctxt *obj.Link, p *obj.Prog, newprog obj.ProgAlloc) {
4259
p.From.Class = 0
4360
p.To.Class = 0
@@ -643,7 +660,7 @@ func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
643660

644661
q = p
645662

646-
if NeedTOCpointer(c.ctxt) && c.cursym.Name != "runtime.duffzero" && c.cursym.Name != "runtime.duffcopy" {
663+
if NeedTOCpointer(c.ctxt) && !isNOTOCfunc(c.cursym.Name) {
647664
// When compiling Go into PIC, without PCrel support, all functions must start
648665
// with instructions to load the TOC pointer into r2:
649666
//

src/cmd/link/internal/ppc64/asm.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,24 +475,9 @@ func rewriteABIFuncReloc(ctxt *ld.Link, ldr *loader.Loader, tname string, r load
475475
r.SetAdd(int64((n - minReg) * offMul))
476476
firstUse = !ldr.AttrReachable(ts)
477477
if firstUse {
478-
ldr.SetAttrReachable(ts, true)
479478
// This function only becomes reachable now. It has been dropped from
480479
// the text section (it was unreachable until now), it needs included.
481-
//
482-
// Similarly, TOC regeneration should not happen for these functions,
483-
// remove it from this save/restore function.
484-
if ldr.AttrShared(ts) {
485-
sb := ldr.MakeSymbolUpdater(ts)
486-
sb.SetData(sb.Data()[8:])
487-
sb.SetSize(sb.Size() - 8)
488-
relocs := sb.Relocs()
489-
// Only one PCREL reloc to .TOC. should be present.
490-
if relocs.Count() != 1 {
491-
log.Fatalf("Unexpected number of relocs in %s\n", ldr.SymName(ts))
492-
}
493-
sb.ResetRelocs()
494-
495-
}
480+
ldr.SetAttrReachable(ts, true)
496481
}
497482
return ts, firstUse
498483
}

0 commit comments

Comments
 (0)