Skip to content

Commit 34d7751

Browse files
committed
[MC][AArch64] Make .reloc support arbitrary relocation types
Depends on D76746. Generalizes D61973. Differential Revision: https://reviews.llvm.org/D76754
1 parent c389526 commit 34d7751

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class AArch64AsmBackend : public MCAsmBackend {
6969
{"fixup_aarch64_pcrel_call26", 0, 26, PCRelFlagVal},
7070
{"fixup_aarch64_tlsdesc_call", 0, 0, 0}};
7171

72+
// Fixup kinds from .reloc directive are like R_AARCH64_NONE. They do not
73+
// require any extra processing.
74+
if (Kind >= FirstLiteralRelocationKind)
75+
return MCAsmBackend::getFixupKindInfo(FK_NONE);
76+
7277
if (Kind < FirstTargetFixupKind)
7378
return MCAsmBackend::getFixupKindInfo(Kind);
7479

@@ -109,7 +114,6 @@ static unsigned getFixupKindNumBytes(unsigned Kind) {
109114
default:
110115
llvm_unreachable("Unknown fixup kind!");
111116

112-
case FK_NONE:
113117
case AArch64::fixup_aarch64_tlsdesc_call:
114118
return 0;
115119

@@ -330,17 +334,24 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
330334
if (!valueFitsIntoFixupKind(Fixup.getTargetKind(), Value))
331335
Ctx.reportError(Fixup.getLoc(), "fixup value too large for data type!");
332336
LLVM_FALLTHROUGH;
333-
case FK_NONE:
334337
case FK_SecRel_2:
335338
case FK_SecRel_4:
336339
return Value;
337340
}
338341
}
339342

340343
Optional<MCFixupKind> AArch64AsmBackend::getFixupKind(StringRef Name) const {
341-
if (TheTriple.isOSBinFormatELF() && Name == "R_AARCH64_NONE")
342-
return FK_NONE;
343-
return MCAsmBackend::getFixupKind(Name);
344+
if (!TheTriple.isOSBinFormatELF())
345+
return None;
346+
347+
unsigned Type = llvm::StringSwitch<unsigned>(Name)
348+
#define ELF_RELOC(X, Y) .Case(#X, Y)
349+
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
350+
#undef ELF_RELOC
351+
.Default(-1u);
352+
if (Type == -1u)
353+
return None;
354+
return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
344355
}
345356

346357
/// getFixupKindContainereSizeInBytes - The number of bytes of the
@@ -387,9 +398,12 @@ void AArch64AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
387398
MutableArrayRef<char> Data, uint64_t Value,
388399
bool IsResolved,
389400
const MCSubtargetInfo *STI) const {
390-
unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
391401
if (!Value)
392402
return; // Doesn't change encoding.
403+
unsigned Kind = Fixup.getKind();
404+
if (Kind >= FirstLiteralRelocationKind)
405+
return;
406+
unsigned NumBytes = getFixupKindNumBytes(Kind);
393407
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
394408
MCContext &Ctx = Asm.getContext();
395409
int64_t SignedValue = static_cast<int64_t>(Value);
@@ -475,7 +489,7 @@ bool AArch64AsmBackend::shouldForceRelocation(const MCAssembler &Asm,
475489
const MCFixup &Fixup,
476490
const MCValue &Target) {
477491
unsigned Kind = Fixup.getKind();
478-
if (Kind == FK_NONE)
492+
if (Kind >= FirstLiteralRelocationKind)
479493
return true;
480494

481495
// The ADRP instruction adds some multiple of 0x1000 to the current PC &

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
106106
const MCValue &Target,
107107
const MCFixup &Fixup,
108108
bool IsPCRel) const {
109+
unsigned Kind = Fixup.getTargetKind();
110+
if (Kind >= FirstLiteralRelocationKind)
111+
return Kind - FirstLiteralRelocationKind;
109112
AArch64MCExpr::VariantKind RefKind =
110113
static_cast<AArch64MCExpr::VariantKind>(Target.getRefKind());
111114
AArch64MCExpr::VariantKind SymLoc = AArch64MCExpr::getSymbolLoc(RefKind);
@@ -120,7 +123,7 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
120123
"Should only be expression-level modifiers here");
121124

122125
if (IsPCRel) {
123-
switch (Fixup.getTargetKind()) {
126+
switch (Kind) {
124127
case FK_Data_1:
125128
Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported");
126129
return ELF::R_AARCH64_NONE;
@@ -185,8 +188,6 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
185188
if (IsILP32 && isNonILP32reloc(Fixup, RefKind, Ctx))
186189
return ELF::R_AARCH64_NONE;
187190
switch (Fixup.getTargetKind()) {
188-
case FK_NONE:
189-
return ELF::R_AARCH64_NONE;
190191
case FK_Data_1:
191192
Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported");
192193
return ELF::R_AARCH64_NONE;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# RUN: llvm-mc -triple=aarch64 %s 2>&1 | FileCheck --check-prefix=PRINT %s
2+
# RUN: not llvm-mc -filetype=obj -triple=aarch64 %s -o /dev/null 2>&1 | FileCheck %s
3+
4+
# PRINT: .reloc 0, R_INVALID, 0
5+
# CHECK: {{.*}}.s:[[# @LINE+1]]:11: error: unknown relocation name
6+
.reloc 0, R_INVALID, 0

llvm/test/MC/AArch64/reloc-directive.s

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# PRINT: .reloc 8, R_AARCH64_NONE, .data
66
# PRINT: .reloc 4, R_AARCH64_NONE, foo+4
77
# PRINT: .reloc 0, R_AARCH64_NONE, 8
8+
# PRINT: .reloc 0, R_AARCH64_ABS64, .data+2
9+
# PRINT: .reloc 0, R_AARCH64_TLSDESC, foo+3
10+
# PRINT: .reloc 0, R_AARCH64_IRELATIVE, 5
811
.text
912
ret
1013
nop
@@ -13,6 +16,10 @@
1316
.reloc 4, R_AARCH64_NONE, foo+4
1417
.reloc 0, R_AARCH64_NONE, 8
1518

19+
.reloc 0, R_AARCH64_ABS64, .data+2
20+
.reloc 0, R_AARCH64_TLSDESC, foo+3
21+
.reloc 0, R_AARCH64_IRELATIVE, 5
22+
1623
.data
1724
.globl foo
1825
foo:
@@ -23,3 +30,6 @@ foo:
2330
# CHECK: 0x8 R_AARCH64_NONE .data 0x0
2431
# CHECK-NEXT: 0x4 R_AARCH64_NONE foo 0x4
2532
# CHECK-NEXT: 0x0 R_AARCH64_NONE - 0x8
33+
# CHECK-NEXT: 0x0 R_AARCH64_ABS64 .data 0x2
34+
# CHECK-NEXT: 0x0 R_AARCH64_TLSDESC foo 0x3
35+
# CHECK-NEXT: 0x0 R_AARCH64_IRELATIVE - 0x5

0 commit comments

Comments
 (0)