Skip to content

Commit

Permalink
[release-branch.go1.22] cmd/link: handle dynamic import variables on …
Browse files Browse the repository at this point in the history
…Darwin in plugin mode

CL 501855 added support for cgo_dynamic_import variables on Darwin.
But it didn't support the plugin build mode on amd64, where the
assembler turns a direct load (R_PCREL) to a load via GOT
(R_GOTPCREL). This CL adds the support. We just need to handle
external linking mode, as this can only occur in plugin or shared
build mode, which requires external linking.

Fixes #68122.
Updates #67976.
Updates #50891.

Change-Id: I0f56265d50bfcb36047fa5538ad7a5ec77e7ef96
Reviewed-on: https://go-review.googlesource.com/c/go/+/592499
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
(cherry picked from commit 44f1870)
Reviewed-on: https://go-review.googlesource.com/c/go/+/595175
Reviewed-by: Joedian Reid <joedian@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
  • Loading branch information
cherrymui authored and joedian committed Jun 26, 2024
1 parent 3222951 commit c2d4f85
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cmd/cgo/internal/testplugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,11 @@ func TestTextSectionSplit(t *testing.T) {
t.Errorf("runtime.text.1 not found, text section not split?")
}
}

func TestIssue67976(t *testing.T) {
// Issue 67976: build failure with loading a dynimport variable (the runtime/pprof
// package does this on darwin) in a plugin on darwin/amd64.
// The test program uses runtime/pprof in a plugin.
globalSkip(t)
goCmd(t, "build", "-buildmode=plugin", "-o", "issue67976.so", "./issue67976/plugin.go")
}
16 changes: 16 additions & 0 deletions src/cmd/cgo/internal/testplugin/testdata/issue67976/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"io"
"runtime/pprof"
)

func main() {}

func Start() {
pprof.StartCPUProfile(io.Discard)
}
7 changes: 7 additions & 0 deletions src/cmd/link/internal/amd64/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
// (e.g. go version).
return true
}
case objabi.R_GOTPCREL:
if target.IsExternal() {
// External linker will do this relocation.
return true
}
// We only need to handle external linking mode, as R_GOTPCREL can
// only occur in plugin or shared build modes.
}

return false
Expand Down

0 comments on commit c2d4f85

Please sign in to comment.