Skip to content

Commit 81edc83

Browse files
committed
[AMDGPU] Add support for the .reloc directive
Differential Revision: https://reviews.llvm.org/D127117
1 parent cc5a1b3 commit 81edc83

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ class AMDGPUAsmBackend : public MCAsmBackend {
5050
bool writeNopData(raw_ostream &OS, uint64_t Count,
5151
const MCSubtargetInfo *STI) const override;
5252

53+
Optional<MCFixupKind> getFixupKind(StringRef Name) const override;
5354
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
55+
bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
56+
const MCValue &Target) override;
5457
};
5558

5659
} //End anonymous namespace
@@ -137,6 +140,9 @@ void AMDGPUAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
137140
MutableArrayRef<char> Data, uint64_t Value,
138141
bool IsResolved,
139142
const MCSubtargetInfo *STI) const {
143+
if (Fixup.getKind() >= FirstLiteralRelocationKind)
144+
return;
145+
140146
Value = adjustFixupValue(Fixup, Value, &Asm.getContext());
141147
if (!Value)
142148
return; // Doesn't change encoding.
@@ -156,19 +162,37 @@ void AMDGPUAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
156162
Data[Offset + i] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
157163
}
158164

165+
Optional<MCFixupKind> AMDGPUAsmBackend::getFixupKind(StringRef Name) const {
166+
return StringSwitch<Optional<MCFixupKind>>(Name)
167+
#define ELF_RELOC(Name, Value) \
168+
.Case(#Name, MCFixupKind(FirstLiteralRelocationKind + Value))
169+
#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
170+
#undef ELF_RELOC
171+
.Default(None);
172+
}
173+
159174
const MCFixupKindInfo &AMDGPUAsmBackend::getFixupKindInfo(
160175
MCFixupKind Kind) const {
161176
const static MCFixupKindInfo Infos[AMDGPU::NumTargetFixupKinds] = {
162177
// name offset bits flags
163178
{ "fixup_si_sopp_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
164179
};
165180

181+
if (Kind >= FirstLiteralRelocationKind)
182+
return MCAsmBackend::getFixupKindInfo(FK_NONE);
183+
166184
if (Kind < FirstTargetFixupKind)
167185
return MCAsmBackend::getFixupKindInfo(Kind);
168186

169187
return Infos[Kind - FirstTargetFixupKind];
170188
}
171189

190+
bool AMDGPUAsmBackend::shouldForceRelocation(const MCAssembler &,
191+
const MCFixup &Fixup,
192+
const MCValue &) {
193+
return Fixup.getKind() >= FirstLiteralRelocationKind;
194+
}
195+
172196
unsigned AMDGPUAsmBackend::getMinimumNopSize() const {
173197
return 4;
174198
}

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
6565
return ELF::R_AMDGPU_REL64;
6666
}
6767

68-
switch (Fixup.getKind()) {
68+
MCFixupKind Kind = Fixup.getKind();
69+
if (Kind >= FirstLiteralRelocationKind)
70+
return Kind - FirstLiteralRelocationKind;
71+
switch (Kind) {
6972
default: break;
7073
case FK_PCRel_4:
7174
return ELF::R_AMDGPU_REL32;

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# RUN: llvm-mc -triple=amdgcn--amdhsa %s | FileCheck --check-prefix=PRINT %s
2+
3+
# RUN: llvm-mc -filetype=obj -triple=amdgcn--amdhsa %s -o %t
4+
# RUN: llvm-readobj -r %t | FileCheck %s
5+
6+
# PRINT: .reloc 2, R_AMDGPU_NONE, .data
7+
# PRINT-NEXT: .reloc 1, R_AMDGPU_NONE, foo+4
8+
# PRINT-NEXT: .reloc 0, R_AMDGPU_NONE, 8
9+
# PRINT-NEXT: .reloc 0, R_AMDGPU_ABS32_LO, .data
10+
# PRINT-NEXT: .reloc 0, R_AMDGPU_ABS32_HI, .data
11+
# PRINT-NEXT: .reloc 0, R_AMDGPU_ABS64, .data
12+
# PRINT-NEXT: .reloc 0, R_AMDGPU_REL32, .data
13+
# PRINT-NEXT: .reloc 0, R_AMDGPU_REL64, .data
14+
# PRINT-NEXT: .reloc 0, R_AMDGPU_ABS32, .data
15+
# PRINT-NEXT: .reloc 0, R_AMDGPU_GOTPCREL, .data
16+
# PRINT-NEXT: .reloc 0, R_AMDGPU_GOTPCREL32_LO, .data
17+
# PRINT-NEXT: .reloc 0, R_AMDGPU_GOTPCREL32_HI, .data
18+
# PRINT-NEXT: .reloc 0, R_AMDGPU_REL32_LO, .data
19+
# PRINT-NEXT: .reloc 0, R_AMDGPU_REL32_HI, .data
20+
# PRINT-NEXT: .reloc 0, R_AMDGPU_RELATIVE64, .data
21+
# PRINT-NEXT: .reloc 0, R_AMDGPU_REL16, .data
22+
23+
# CHECK: 0x2 R_AMDGPU_NONE .data
24+
# CHECK-NEXT: 0x1 R_AMDGPU_NONE foo 0x4
25+
# CHECK-NEXT: 0x0 R_AMDGPU_NONE - 0x8
26+
# CHECK-NEXT: 0x0 R_AMDGPU_ABS32_LO .data
27+
# CHECK-NEXT: 0x0 R_AMDGPU_ABS32_HI .data
28+
# CHECK-NEXT: 0x0 R_AMDGPU_ABS64 .data
29+
# CHECK-NEXT: 0x0 R_AMDGPU_REL32 .data
30+
# CHECK-NEXT: 0x0 R_AMDGPU_REL64 .data
31+
# CHECK-NEXT: 0x0 R_AMDGPU_ABS32 .data
32+
# CHECK-NEXT: 0x0 R_AMDGPU_GOTPCREL .data
33+
# CHECK-NEXT: 0x0 R_AMDGPU_GOTPCREL32_LO .data
34+
# CHECK-NEXT: 0x0 R_AMDGPU_GOTPCREL32_HI .data
35+
# CHECK-NEXT: 0x0 R_AMDGPU_REL32_LO .data
36+
# CHECK-NEXT: 0x0 R_AMDGPU_REL32_HI .data
37+
# CHECK-NEXT: 0x0 R_AMDGPU_RELATIVE64 .data
38+
# CHECK-NEXT: 0x0 R_AMDGPU_REL16 .data
39+
40+
.text
41+
s_nop 0
42+
s_nop 0
43+
.reloc 2, R_AMDGPU_NONE, .data
44+
.reloc 1, R_AMDGPU_NONE, foo+4
45+
.reloc 0, R_AMDGPU_NONE, 8
46+
.reloc 0, R_AMDGPU_ABS32_LO, .data
47+
.reloc 0, R_AMDGPU_ABS32_HI, .data
48+
.reloc 0, R_AMDGPU_ABS64, .data
49+
.reloc 0, R_AMDGPU_REL32, .data
50+
.reloc 0, R_AMDGPU_REL64, .data
51+
.reloc 0, R_AMDGPU_ABS32, .data
52+
.reloc 0, R_AMDGPU_GOTPCREL, .data
53+
.reloc 0, R_AMDGPU_GOTPCREL32_LO, .data
54+
.reloc 0, R_AMDGPU_GOTPCREL32_HI, .data
55+
.reloc 0, R_AMDGPU_REL32_LO, .data
56+
.reloc 0, R_AMDGPU_REL32_HI, .data
57+
.reloc 0, R_AMDGPU_RELATIVE64, .data
58+
.reloc 0, R_AMDGPU_REL16, .data
59+
60+
.data
61+
.globl foo
62+
foo:
63+
.long 0
64+
.long 0

0 commit comments

Comments
 (0)