Skip to content

Commit

Permalink
cmd/link: only add dummy XCOFF reference if the symbol exists
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Cuong Manh Le <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
  • Loading branch information
cherrymui committed Sep 2, 2022
1 parent 202b7e7 commit 321a220
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/cmd/link/internal/ld/symtab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/fixedbugs/bug514.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/fixedbugs/issue40954.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/fixedbugs/issue42032.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/fixedbugs/issue42076.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/fixedbugs/issue46903.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/fixedbugs/issue51733.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 321a220

Please sign in to comment.