Skip to content

Commit 53c41f1

Browse files
authored
ELF: Add support for R_AARCH64_PATCHINST relocation type.
The R_AARCH64_PATCHINST relocation type is to support deactivation symbols. For more information, see the RFC: https://discourse.llvm.org/t/rfc-deactivation-symbols/85556 Part of the AArch64 psABI extension: ARM-software/abi-aa#340 Reviewers: smithp35, davemgreen, MaskRay Reviewed By: MaskRay, davemgreen, smithp35 Pull Request: llvm#133534
1 parent a922731 commit 53c41f1

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
154154
case R_AARCH64_MOVW_UABS_G2_NC:
155155
case R_AARCH64_MOVW_UABS_G3:
156156
return R_ABS;
157+
case R_AARCH64_PATCHINST:
158+
if (!isAbsolute(s))
159+
Err(ctx) << getErrorLoc(ctx, loc)
160+
<< "R_AARCH64_PATCHINST relocation against non-absolute symbol "
161+
<< &s;
162+
return R_ABS;
157163
case R_AARCH64_AUTH_ABS64:
158164
return RE_AARCH64_AUTH;
159165
case R_AARCH64_TLSDESC_ADR_PAGE21:
@@ -506,6 +512,12 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
506512
checkIntUInt(ctx, loc, val, 32, rel);
507513
write32(ctx, loc, val);
508514
break;
515+
case R_AARCH64_PATCHINST:
516+
if (!rel.sym->isUndefined()) {
517+
checkUInt(ctx, loc, val, 32, rel);
518+
write32le(loc, val);
519+
}
520+
break;
509521
case R_AARCH64_PLT32:
510522
case R_AARCH64_GOTPCREL32:
511523
checkInt(ctx, loc, val, 32, rel);

lld/ELF/Relocations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static RelType getMipsPairType(RelType type, bool isLocal) {
176176

177177
// True if non-preemptable symbol always has the same value regardless of where
178178
// the DSO is loaded.
179-
static bool isAbsolute(const Symbol &sym) {
179+
bool elf::isAbsolute(const Symbol &sym) {
180180
if (sym.isUndefined())
181181
return true;
182182
if (const auto *dr = dyn_cast<Defined>(&sym))

lld/ELF/Relocations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ void addGotEntry(Ctx &ctx, Symbol &sym);
165165
void hexagonTLSSymbolUpdate(Ctx &ctx);
166166
bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);
167167

168+
bool isAbsolute(const Symbol &sym);
169+
168170
class ThunkSection;
169171
class Thunk;
170172
class InputSectionDescription;

lld/test/ELF/aarch64-patchinst.s

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# RUN: rm -rf %t && split-file %s %t
2+
# RUN: llvm-mc -filetype=obj -triple=aarch64 %t/use.s -o %t/use-le.o
3+
# RUN: llvm-mc -filetype=obj -triple=aarch64 %t/def.s -o %t/def-le.o
4+
# RUN: llvm-mc -filetype=obj -triple=aarch64 %t/rel.s -o %t/rel-le.o
5+
6+
## Deactivation symbol used without being defined: instruction emitted as usual.
7+
# RUN: ld.lld -o %t/undef-le %t/use-le.o --emit-relocs
8+
# RUN: llvm-objdump -r %t/undef-le | FileCheck --check-prefix=RELOC %s
9+
# RUN: llvm-objdump -d %t/undef-le | FileCheck --check-prefix=UNDEF %s
10+
# RUN: ld.lld -pie -o %t/undef-le %t/use-le.o --emit-relocs
11+
# RUN: llvm-objdump -r %t/undef-le | FileCheck --check-prefix=RELOC %s
12+
# RUN: llvm-objdump -d %t/undef-le | FileCheck --check-prefix=UNDEF %s
13+
14+
## Deactivation symbol defined: instructions overwritten with NOPs.
15+
# RUN: ld.lld -o %t/def-le %t/use-le.o %t/def-le.o --emit-relocs
16+
# RUN: llvm-objdump -r %t/def-le | FileCheck --check-prefix=RELOC %s
17+
# RUN: llvm-objdump -d %t/def-le | FileCheck --check-prefix=DEF %s
18+
# RUN: ld.lld -pie -o %t/def-le %t/use-le.o %t/def-le.o --emit-relocs
19+
# RUN: llvm-objdump -r %t/def-le | FileCheck --check-prefix=RELOC %s
20+
# RUN: llvm-objdump -d %t/def-le | FileCheck --check-prefix=DEF %s
21+
22+
## Relocation pointing to a non-SHN_UNDEF non-SHN_ABS symbol is an error.
23+
# RUN: not ld.lld -o %t/rel-le %t/use-le.o %t/rel-le.o 2>&1 | FileCheck --check-prefix=ERROR %s
24+
# RUN: not ld.lld -pie -o %t/rel-le %t/use-le.o %t/rel-le.o 2>&1 | FileCheck --check-prefix=ERROR %s
25+
26+
## Behavior unchanged by endianness: relocation always written as little endian.
27+
# RUN: llvm-mc -filetype=obj -triple=aarch64_be %t/use.s -o %t/use-be.o
28+
# RUN: llvm-mc -filetype=obj -triple=aarch64_be %t/def.s -o %t/def-be.o
29+
# RUN: llvm-mc -filetype=obj -triple=aarch64_be %t/rel.s -o %t/rel-be.o
30+
# RUN: ld.lld -o %t/undef-be %t/use-be.o --emit-relocs
31+
# RUN: llvm-objdump -r %t/undef-be | FileCheck --check-prefix=RELOC %s
32+
# RUN: llvm-objdump -d %t/undef-be | FileCheck --check-prefix=UNDEF %s
33+
# RUN: ld.lld -pie -o %t/undef-be %t/use-be.o --emit-relocs
34+
# RUN: llvm-objdump -r %t/undef-be | FileCheck --check-prefix=RELOC %s
35+
# RUN: llvm-objdump -d %t/undef-be | FileCheck --check-prefix=UNDEF %s
36+
# RUN: ld.lld -o %t/def-be %t/use-be.o %t/def-be.o --emit-relocs
37+
# RUN: llvm-objdump -r %t/def-be | FileCheck --check-prefix=RELOC %s
38+
# RUN: llvm-objdump -d %t/def-be | FileCheck --check-prefix=DEF %s
39+
# RUN: ld.lld -pie -o %t/def-be %t/use-be.o %t/def-be.o --emit-relocs
40+
# RUN: llvm-objdump -r %t/def-be | FileCheck --check-prefix=RELOC %s
41+
# RUN: llvm-objdump -d %t/def-be | FileCheck --check-prefix=DEF %s
42+
# RUN: not ld.lld -o %t/rel-be %t/use-be.o %t/rel-be.o 2>&1 | FileCheck --check-prefix=ERROR %s
43+
# RUN: not ld.lld -pie -o %t/rel-be %t/use-be.o %t/rel-be.o 2>&1 | FileCheck --check-prefix=ERROR %s
44+
45+
# RELOC: R_AARCH64_JUMP26
46+
# RELOC-NEXT: R_AARCH64_PATCHINST ds
47+
# RELOC-NEXT: R_AARCH64_PATCHINST ds
48+
# RELOC-NEXT: R_AARCH64_PATCHINST ds0+0xd503201f
49+
50+
#--- use.s
51+
.weak ds
52+
.weak ds0
53+
# This instruction has a single relocation: the DS relocation.
54+
# UNDEF: add x0, x1, x2
55+
# DEF: nop
56+
# ERROR: R_AARCH64_PATCHINST relocation against non-absolute symbol ds
57+
.reloc ., R_AARCH64_PATCHINST, ds
58+
add x0, x1, x2
59+
# This instruction has two relocations: the DS relocation and the JUMP26 to f1.
60+
# Make sure that the DS relocation takes precedence.
61+
.reloc ., R_AARCH64_PATCHINST, ds
62+
# UNDEF: b {{.*}} <f1>
63+
# DEF: nop
64+
# ERROR: R_AARCH64_PATCHINST relocation against non-absolute symbol ds
65+
b f1
66+
# Alternative representation: instruction opcode stored in addend.
67+
# UNDEF: add x3, x4, x5
68+
# DEF: nop
69+
# ERROR: R_AARCH64_PATCHINST relocation against non-absolute symbol ds0
70+
.reloc ., R_AARCH64_PATCHINST, ds0 + 0xd503201f
71+
add x3, x4, x5
72+
73+
.section .text.f1,"ax",@progbits
74+
f1:
75+
ret
76+
77+
#--- def.s
78+
.globl ds
79+
ds = 0xd503201f
80+
.globl ds0
81+
ds0 = 0
82+
83+
#--- rel.s
84+
.globl ds
85+
ds:
86+
.globl ds0
87+
ds0:

0 commit comments

Comments
 (0)