Skip to content

Commit 8401ab8

Browse files
committed
ELF: Introduce 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 TODO: - Agree on semantics and relocation type number. Pull Request: llvm#133534
1 parent c42406c commit 8401ab8

File tree

7 files changed

+122
-1
lines changed

7 files changed

+122
-1
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
156156
case R_AARCH64_MOVW_UABS_G2_NC:
157157
case R_AARCH64_MOVW_UABS_G3:
158158
return R_ABS;
159+
case R_AARCH64_PATCHINST:
160+
if (!isAbsolute(s))
161+
Err(ctx) << getErrorLoc(ctx, loc)
162+
<< "R_AARCH64_PATCHINST relocation against non-absolute symbol "
163+
<< &s;
164+
return R_ABS;
159165
case R_AARCH64_AUTH_ABS64:
160166
return RE_AARCH64_AUTH;
161167
case R_AARCH64_TLSDESC_ADR_PAGE21:
@@ -509,6 +515,12 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
509515
checkIntUInt(ctx, loc, val, 32, rel);
510516
write32(ctx, loc, val);
511517
break;
518+
case R_AARCH64_PATCHINST:
519+
if (!rel.sym->isUndefined()) {
520+
checkUInt(ctx, loc, val, 32, rel);
521+
write32le(loc, val);
522+
}
523+
break;
512524
case R_AARCH64_PLT32:
513525
case R_AARCH64_GOTPCREL32:
514526
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
@@ -164,6 +164,8 @@ void addGotEntry(Ctx &ctx, Symbol &sym);
164164
void hexagonTLSSymbolUpdate(Ctx &ctx);
165165
bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);
166166

167+
bool isAbsolute(const Symbol &sym);
168+
167169
class ThunkSection;
168170
class Thunk;
169171
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:

llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ ELF_RELOC(R_AARCH64_LD64_GOT_LO12_NC, 0x138)
6161
ELF_RELOC(R_AARCH64_LD64_GOTPAGE_LO15, 0x139)
6262
ELF_RELOC(R_AARCH64_PLT32, 0x13a)
6363
ELF_RELOC(R_AARCH64_GOTPCREL32, 0x13b)
64+
ELF_RELOC(R_AARCH64_PATCHINST, 0x13c)
6465
// General dynamic TLS relocations
6566
ELF_RELOC(R_AARCH64_TLSGD_ADR_PREL21, 0x200)
6667
ELF_RELOC(R_AARCH64_TLSGD_ADR_PAGE21, 0x201)

llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class AArch64ELFObjectWriter : public MCELFObjectTargetWriter {
4040
bool IsPCRel) const override;
4141
bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override;
4242
bool isNonILP32reloc(const MCFixup &Fixup, AArch64::Specifier RefKind) const;
43+
void sortRelocs(std::vector<ELFRelocationEntry> &Relocs) override;
4344

4445
bool IsILP32;
4546
};
@@ -499,6 +500,17 @@ bool AArch64ELFObjectWriter::needsRelocateWithSymbol(const MCValue &Val,
499500
Val.getSpecifier());
500501
}
501502

503+
void AArch64ELFObjectWriter::sortRelocs(
504+
std::vector<ELFRelocationEntry> &Relocs) {
505+
// PATCHINST relocations should be applied last because they may overwrite the
506+
// whole instruction and so should take precedence over other relocations that
507+
// modify operands of the original instruction.
508+
std::stable_partition(Relocs.begin(), Relocs.end(),
509+
[](const ELFRelocationEntry &R) {
510+
return R.Type != ELF::R_AARCH64_PATCHINST;
511+
});
512+
}
513+
502514
std::unique_ptr<MCObjectTargetWriter>
503515
llvm::createAArch64ELFObjectWriter(uint8_t OSABI, bool IsILP32) {
504516
return std::make_unique<AArch64ELFObjectWriter>(OSABI, IsILP32);

llvm/test/MC/AArch64/patchinst.s

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: llvm-mc -triple aarch64-elf -filetype=obj %s -o - | llvm-objdump -r - | FileCheck %s
2+
3+
// Test that PATCHINST appears after JUMP26.
4+
// CHECK: R_AARCH64_JUMP26
5+
// CHECK-NEXT: R_AARCH64_PATCHINST
6+
.reloc ., R_AARCH64_PATCHINST, ds
7+
b f1

0 commit comments

Comments
 (0)