From 321a220d506fe1445302706f1725017a42fc1510 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Thu, 1 Sep 2022 19:31:04 -0400 Subject: [PATCH] cmd/link: only add dummy XCOFF reference if the symbol exists On AIX when external linking, for some symbols we need to add dummy references to prevent the external linker from discarding them. Currently we add the reference unconditionally. But if the symbol doesn't exist, the linking fails in a later stage for generating external relocation of a nonexistent symbol. The symbols are special symbols that almost always exist, except that go:buildid may not exist if the linker is invoked without the -buildid flag. The go command invokes the linker with the flag, so this can only happen with manual linker invocation. Specifically, test/run.go does this in some cases. Fix this by checking the symbol existence before adding the reference. Re-enable tests on AIX. Perhaps the linker should always emit a dummy buildid even if the flag is not set... Fixes #54814. Change-Id: I43d81587151595309e189e38960cbda9a1c5ca32 Reviewed-on: https://go-review.googlesource.com/c/go/+/427620 Run-TryBot: Cherry Mui Reviewed-by: Cuong Manh Le TryBot-Result: Gopher Robot Reviewed-by: Than McIntosh --- src/cmd/link/internal/ld/symtab.go | 6 +++++- test/fixedbugs/bug514.go | 2 +- test/fixedbugs/issue40954.go | 2 +- test/fixedbugs/issue42032.go | 2 +- test/fixedbugs/issue42076.go | 2 +- test/fixedbugs/issue46903.go | 2 +- test/fixedbugs/issue51733.go | 2 +- 7 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cmd/link/internal/ld/symtab.go b/src/cmd/link/internal/ld/symtab.go index 02b384ba9df..5074ffa8c9e 100644 --- a/src/cmd/link/internal/ld/symtab.go +++ b/src/cmd/link/internal/ld/symtab.go @@ -684,8 +684,12 @@ func (ctxt *Link) symtab(pcln *pclntab) []sym.SymKind { // Add R_XCOFFREF relocation to prevent ld's garbage collection of // the following symbols. They might not be referenced in the program. addRef := func(name string) { + s := ldr.Lookup(name, 0) + if s == 0 { + return + } r, _ := moduledata.AddRel(objabi.R_XCOFFREF) - r.SetSym(ldr.Lookup(name, 0)) + r.SetSym(s) r.SetSiz(uint8(ctxt.Arch.PtrSize)) } addRef("runtime.rodata") diff --git a/test/fixedbugs/bug514.go b/test/fixedbugs/bug514.go index 1a6c7f14dd7..9b231853378 100644 --- a/test/fixedbugs/bug514.go +++ b/test/fixedbugs/bug514.go @@ -4,7 +4,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build cgo && !aix +//go:build cgo package main diff --git a/test/fixedbugs/issue40954.go b/test/fixedbugs/issue40954.go index e268b808ca1..0beaabb7439 100644 --- a/test/fixedbugs/issue40954.go +++ b/test/fixedbugs/issue40954.go @@ -4,7 +4,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build cgo && !aix +//go:build cgo package main diff --git a/test/fixedbugs/issue42032.go b/test/fixedbugs/issue42032.go index 68fbc890458..eb118591019 100644 --- a/test/fixedbugs/issue42032.go +++ b/test/fixedbugs/issue42032.go @@ -4,7 +4,7 @@ // source code is governed by a BSD-style license that can be found in // the LICENSE file. -//go:build cgo && !aix +//go:build cgo package main diff --git a/test/fixedbugs/issue42076.go b/test/fixedbugs/issue42076.go index b958d0eeb50..ef8db2da30d 100644 --- a/test/fixedbugs/issue42076.go +++ b/test/fixedbugs/issue42076.go @@ -4,7 +4,7 @@ // source code is governed by a BSD-style license that can be found in // the LICENSE file. -//go:build cgo && !aix +//go:build cgo package main diff --git a/test/fixedbugs/issue46903.go b/test/fixedbugs/issue46903.go index 90ceb9a86c2..28cb43df3ba 100644 --- a/test/fixedbugs/issue46903.go +++ b/test/fixedbugs/issue46903.go @@ -1,5 +1,5 @@ // run -//go:build goexperiment.unified && cgo && !aix +//go:build goexperiment.unified && cgo // TODO(mdempsky): Enable test unconditionally. This test should pass // for non-unified mode too. diff --git a/test/fixedbugs/issue51733.go b/test/fixedbugs/issue51733.go index 757ef733c3f..933c3e868c0 100644 --- a/test/fixedbugs/issue51733.go +++ b/test/fixedbugs/issue51733.go @@ -4,7 +4,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build cgo && !aix +//go:build cgo package main