Skip to content

Commit 448be06

Browse files
labogercagedmantis
authored andcommitted
[release-branch.go1.15] cmd/link/internal: fix use of DynlinkingGo with ppc64le trampolines
When creating programs with large text sections on ppc64le, trampolines are needed for calls that are too far; however they are not created if the code is generated such that the TOC register r2 is initialized and maintained in the code because then the external linker can create the trampolines. Previously the function DynlinkingGo was used to determine this but in the case where plugins are used, this could return true even though r2 is not valid. To fix this problem I've added a new function r2Valid which returns true when the build options indicate that the r2 is initialized and maintained. Because of the ways that DynlinkingGo is used I wanted to maintain its previous behavior. Fixes #46002 Change-Id: I6d902eba6ad41757aa6474948b79acdbd479cb38 Reviewed-on: https://go-review.googlesource.com/c/go/+/315289 Trust: Lynn Boger <laboger@linux.vnet.ibm.com> Run-TryBot: Lynn Boger <laboger@linux.vnet.ibm.com> Reviewed-by: Cherry Zhang <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> (cherry picked from commit 9ed736a) Reviewed-on: https://go-review.googlesource.com/c/go/+/317974 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
1 parent e3c9537 commit 448be06

File tree

1 file changed

+12
-2
lines changed
  • src/cmd/link/internal/ppc64

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,14 +658,24 @@ func archrelocaddr(target *ld.Target, syms *ld.ArchSyms, r *sym.Reloc, s *sym.Sy
658658
return int64(o2)<<32 | int64(o1)
659659
}
660660

661+
// Determine if the code was compiled so that the TOC register R2 is initialized and maintained
662+
func r2Valid(ctxt *ld.Link) bool {
663+
switch ctxt.BuildMode {
664+
case ld.BuildModeCArchive, ld.BuildModeCShared, ld.BuildModePIE, ld.BuildModeShared, ld.BuildModePlugin:
665+
return true
666+
}
667+
// -linkshared option
668+
return ctxt.IsSharedGoLink()
669+
}
670+
661671
// resolve direct jump relocation r in s, and add trampoline if necessary
662672
func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) {
663673

664674
// Trampolines are created if the branch offset is too large and the linker cannot insert a call stub to handle it.
665675
// For internal linking, trampolines are always created for long calls.
666676
// For external linking, the linker can insert a call stub to handle a long call, but depends on having the TOC address in
667677
// r2. For those build modes with external linking where the TOC address is not maintained in r2, trampolines must be created.
668-
if ctxt.IsExternal() && (ctxt.DynlinkingGo() || ctxt.BuildMode == ld.BuildModeCArchive || ctxt.BuildMode == ld.BuildModeCShared || ctxt.BuildMode == ld.BuildModePIE) {
678+
if ctxt.IsExternal() && r2Valid(ctxt) {
669679
// No trampolines needed since r2 contains the TOC
670680
return
671681
}
@@ -719,7 +729,7 @@ func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) {
719729
}
720730
}
721731
if ldr.SymType(tramp) == 0 {
722-
if ctxt.DynlinkingGo() || ctxt.BuildMode == ld.BuildModeCArchive || ctxt.BuildMode == ld.BuildModeCShared || ctxt.BuildMode == ld.BuildModePIE {
732+
if r2Valid(ctxt) {
723733
// Should have returned for above cases
724734
ctxt.Errorf(s, "unexpected trampoline for shared or dynamic linking")
725735
} else {

0 commit comments

Comments
 (0)