Skip to content

Commit b954f58

Browse files
cherrymuicagedmantis
authored andcommitted
[release-branch.go1.17] 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 #49479 Change-Id: I64e02b56f1d792f8c20ca61b78623ef5c3e34d7e Reviewed-on: https://go-review.googlesource.com/c/go/+/360895 Trust: Cherry Mui <[email protected]> Run-TryBot: Cherry Mui <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Than McIntosh <[email protected]> (cherry picked from commit 988efd5) Reviewed-on: https://go-review.googlesource.com/c/go/+/363014 Run-TryBot: Carlos Amedee <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent f1935e2 commit b954f58

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func pereloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym,
602602
rs := r.Xsym
603603
rt := r.Type
604604

605-
if r.Xadd != signext21(r.Xadd) {
605+
if rt == objabi.R_ADDRARM64 && r.Xadd != signext21(r.Xadd) {
606606
// If the relocation target would overflow the addend, then target
607607
// a linker-manufactured label symbol with a smaller addend instead.
608608
label := ldr.Lookup(offsetLabelName(ldr, rs, r.Xadd/peRelocLimit*peRelocLimit), ldr.SymVersion(rs))

src/cmd/link/link_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,31 @@ package main
993993
994994
var x = [1<<25]byte{1<<23: 23, 1<<24: 24}
995995
996+
var addr = [...]*byte{
997+
&x[1<<23-1],
998+
&x[1<<23],
999+
&x[1<<23+1],
1000+
&x[1<<24-1],
1001+
&x[1<<24],
1002+
&x[1<<24+1],
1003+
}
1004+
9961005
func main() {
1006+
// check relocations in instructions
9971007
check(x[1<<23-1], 0)
9981008
check(x[1<<23], 23)
9991009
check(x[1<<23+1], 0)
10001010
check(x[1<<24-1], 0)
10011011
check(x[1<<24], 24)
10021012
check(x[1<<24+1], 0)
1013+
1014+
// check absolute address relocations in data
1015+
check(*addr[0], 0)
1016+
check(*addr[1], 23)
1017+
check(*addr[2], 0)
1018+
check(*addr[3], 0)
1019+
check(*addr[4], 24)
1020+
check(*addr[5], 0)
10031021
}
10041022
10051023
func check(x, y byte) {

0 commit comments

Comments
 (0)