aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/link
diff options
context:
space:
mode:
authorCherry Mui <cherryyz@google.com>2021-11-02 18:30:08 -0400
committerCherry Mui <cherryyz@google.com>2021-11-04 19:58:59 +0000
commit988efd58197205060ace508d29984fbab6eb3840 (patch)
tree1965e6e35f0552a19ddce458278819663950abdf /src/cmd/link
parent5af93a2d1556226656f003f65ac1de034b8fb55f (diff)
downloadgo-988efd58197205060ace508d29984fbab6eb3840.tar.gz
go-988efd58197205060ace508d29984fbab6eb3840.zip
cmd/link: don't use label symbol for absolute address relocations on ARM64 PE
On ARM64 PE, when external linking, the PE relocation does not have an explicit addend, and instead has the addend encoded in the instruction or data. An instruction (e.g. ADRP, ADD) has limited width for the addend, so when the addend is large we use a label symbol, which points to the middle of the original target symbol, and a smaller addend. But for an absolute address relocation in the data section, we have the full width to encode the addend and we should not use the label symbol. Also, since we do not adjust the addend in the data, using the label symbol will actually make it point to the wrong address. E.g for an R_ADDR relocation targeting x+0x123456, we should emit 0x123456 in the data with an IMAGE_REL_ARM64_ADDR64 relocation pointing to x, whereas the current code emits 0x123456 in the data with an IMAGE_REL_ARM64_ADDR64 relocation pointing to the label symbol x+1MB, so it will actually be resolved to x+0x223456. This CL fixes this. Fixes #47557. Change-Id: I64e02b56f1d792f8c20ca61b78623ef5c3e34d7e Reviewed-on: https://go-review.googlesource.com/c/go/+/360895 Trust: Cherry Mui <cherryyz@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
Diffstat (limited to 'src/cmd/link')
-rw-r--r--src/cmd/link/internal/arm64/asm.go2
-rw-r--r--src/cmd/link/link_test.go18
2 files changed, 19 insertions, 1 deletions
diff --git a/src/cmd/link/internal/arm64/asm.go b/src/cmd/link/internal/arm64/asm.go
index f56c9c1222..abae0f87bc 100644
--- a/src/cmd/link/internal/arm64/asm.go
+++ b/src/cmd/link/internal/arm64/asm.go
@@ -602,7 +602,7 @@ func pereloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym,
rs := r.Xsym
rt := r.Type
- if r.Xadd != signext21(r.Xadd) {
+ if rt == objabi.R_ADDRARM64 && r.Xadd != signext21(r.Xadd) {
// If the relocation target would overflow the addend, then target
// a linker-manufactured label symbol with a smaller addend instead.
label := ldr.Lookup(offsetLabelName(ldr, rs, r.Xadd/peRelocLimit*peRelocLimit), ldr.SymVersion(rs))
diff --git a/src/cmd/link/link_test.go b/src/cmd/link/link_test.go
index 2b0b2dc4a1..ad7658bb25 100644
--- a/src/cmd/link/link_test.go
+++ b/src/cmd/link/link_test.go
@@ -997,13 +997,31 @@ package main
var x = [1<<25]byte{1<<23: 23, 1<<24: 24}
+var addr = [...]*byte{
+ &x[1<<23-1],
+ &x[1<<23],
+ &x[1<<23+1],
+ &x[1<<24-1],
+ &x[1<<24],
+ &x[1<<24+1],
+}
+
func main() {
+ // check relocations in instructions
check(x[1<<23-1], 0)
check(x[1<<23], 23)
check(x[1<<23+1], 0)
check(x[1<<24-1], 0)
check(x[1<<24], 24)
check(x[1<<24+1], 0)
+
+ // check absolute address relocations in data
+ check(*addr[0], 0)
+ check(*addr[1], 23)
+ check(*addr[2], 0)
+ check(*addr[3], 0)
+ check(*addr[4], 24)
+ check(*addr[5], 0)
}
func check(x, y byte) {