Skip to content

Commit 803fbdd

Browse files
committed
[PowerPC] Report proper error for invalid relocation specifier
Generalize the test from https://reviews.llvm.org/D83255 Replace getAccessVariant with MCValue::getSpecifier Simplify code after MCValue improvement 94821ce
1 parent d5893fc commit 803fbdd

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,15 @@ PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
3838
Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC,
3939
/*HasRelocationAddend*/ true) {}
4040

41-
static PPCMCExpr::Specifier getAccessVariant(const MCValue &Target,
42-
const MCFixup &Fixup) {
43-
const MCExpr *Expr = Fixup.getValue();
44-
45-
if (Expr->getKind() != MCExpr::Target)
46-
return PPCMCExpr::Specifier(Target.getAccessVariant());
47-
return cast<PPCMCExpr>(Expr)->getSpecifier();
48-
}
49-
5041
unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
5142
const MCFixup &Fixup,
5243
bool IsPCRel) const {
5344
MCFixupKind Kind = Fixup.getKind();
5445
if (Kind >= FirstLiteralRelocationKind)
5546
return Kind - FirstLiteralRelocationKind;
56-
auto RefKind = static_cast<PPCMCExpr::Specifier>(Target.getRefKind());
57-
auto Modifier = getAccessVariant(Target, Fixup);
58-
59-
switch (PPCMCExpr::Specifier(Modifier)) {
47+
SMLoc Loc = Fixup.getValue()->getLoc();
48+
auto Spec = static_cast<PPCMCExpr::Specifier>(Target.getSpecifier());
49+
switch (Spec) {
6050
case PPCMCExpr::VK_DTPMOD:
6151
case PPCMCExpr::VK_DTPREL:
6252
case PPCMCExpr::VK_DTPREL_HA:
@@ -108,16 +98,18 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
10898
}
10999

110100
// determine the type of the relocation
111-
unsigned Type;
101+
unsigned Type = 0;
112102
if (IsPCRel) {
113103
switch (Fixup.getTargetKind()) {
114104
default:
115105
llvm_unreachable("Unimplemented");
116106
case PPC::fixup_ppc_br24:
117107
case PPC::fixup_ppc_br24abs:
118108
case PPC::fixup_ppc_br24_notoc:
119-
switch (Modifier) {
120-
default: llvm_unreachable("Unsupported Modifier");
109+
switch (Spec) {
110+
default:
111+
Ctx.reportError(Loc, "unsupported relocation type");
112+
break;
121113
case PPCMCExpr::VK_None:
122114
Type = ELF::R_PPC_REL24;
123115
break;
@@ -137,9 +129,9 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
137129
Type = ELF::R_PPC_REL14;
138130
break;
139131
case PPC::fixup_ppc_half16:
140-
switch (RefKind) {
132+
switch (Spec) {
141133
default:
142-
Ctx.reportError(Fixup.getLoc(), "invalid VariantKind");
134+
Ctx.reportError(Loc, "unsupported relocation type");
143135
return ELF::R_PPC_NONE;
144136
case PPCMCExpr::VK_None:
145137
return ELF::R_PPC_REL16;
@@ -157,9 +149,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
157149
errs() << '\n';
158150
report_fatal_error("Invalid PC-relative half16ds relocation");
159151
case PPC::fixup_ppc_pcrel34:
160-
switch (Modifier) {
152+
switch (Spec) {
161153
default:
162-
llvm_unreachable("Unsupported Modifier for fixup_ppc_pcrel34");
154+
Ctx.reportError(Loc, "unsupported relocation type");
155+
break;
163156
case PPCMCExpr::VK_PCREL:
164157
Type = ELF::R_PPC64_PCREL34;
165158
break;
@@ -196,9 +189,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
196189
Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
197190
break;
198191
case PPC::fixup_ppc_half16:
199-
switch (Modifier) {
192+
switch (Spec) {
200193
default:
201-
llvm_unreachable("Unsupported specifier");
194+
Ctx.reportError(Loc, "unsupported relocation type");
195+
break;
202196
case PPCMCExpr::VK_LO:
203197
return ELF::R_PPC_ADDR16_LO;
204198
case PPCMCExpr::VK_HI:
@@ -371,10 +365,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
371365
break;
372366
case PPC::fixup_ppc_half16ds:
373367
case PPC::fixup_ppc_half16dq:
374-
switch (Modifier) {
368+
switch (Spec) {
375369
default:
376-
Ctx.reportError(Fixup.getLoc(), "invalid VariantKind");
377-
return ELF::R_PPC64_NONE;
370+
Ctx.reportError(Loc, "unsupported relocation type");
371+
break;
378372
case PPCMCExpr::VK_LO:
379373
return ELF::R_PPC64_ADDR16_LO_DS;
380374
case PPCMCExpr::VK_None:
@@ -419,8 +413,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
419413
}
420414
break;
421415
case PPC::fixup_ppc_nofixup:
422-
switch (Modifier) {
423-
default: llvm_unreachable("Unsupported Modifier");
416+
switch (Spec) {
417+
default:
418+
Ctx.reportError(Loc, "unsupported relocation type");
419+
break;
424420
case PPCMCExpr::VK_TLSGD:
425421
if (is64Bit())
426422
Type = ELF::R_PPC64_TLSGD;
@@ -445,9 +441,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
445441
}
446442
break;
447443
case PPC::fixup_ppc_imm34:
448-
switch (Modifier) {
444+
switch (Spec) {
449445
default:
450-
report_fatal_error("Unsupported Modifier for fixup_ppc_imm34.");
446+
Ctx.reportError(Loc, "unsupported relocation type");
447+
break;
451448
case PPCMCExpr::VK_DTPREL:
452449
Type = ELF::R_PPC64_DTPREL34;
453450
break;
@@ -457,8 +454,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
457454
}
458455
break;
459456
case FK_Data_8:
460-
switch (Modifier) {
461-
default: llvm_unreachable("Unsupported Modifier");
457+
switch (Spec) {
458+
default:
459+
Ctx.reportError(Loc, "unsupported relocation type");
460+
break;
462461
case PPCMCExpr::VK_TOCBASE:
463462
Type = ELF::R_PPC64_TOC;
464463
break;
@@ -477,7 +476,7 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
477476
}
478477
break;
479478
case FK_Data_4:
480-
switch (Modifier) {
479+
switch (Spec) {
481480
case PPCMCExpr::VK_DTPREL:
482481
Type = ELF::R_PPC_DTPREL32;
483482
break;

llvm/test/MC/PowerPC/ppc64-errors-emit-obj.s

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# RUN: not llvm-mc -triple powerpc64 --filetype=obj %s -o %t 2>&1 | FileCheck %s
2+
# RUN: not llvm-mc -triple powerpc64le --filetype=obj %s -o %t 2>&1 | FileCheck %s
3+
4+
# CHECK: [[#@LINE+1]]:4: error: unsupported relocation type
5+
bl foo@toc
6+
7+
# CHECK: [[#@LINE+1]]:12: error: unsupported relocation type
8+
addi 3, 3, foo@plt
9+
10+
# CHECK: [[#@LINE+1]]:14: error: unsupported relocation type
11+
paddi 3, 13, foo@toc, 0
12+
13+
# CHECK: [[#@LINE+1]]:7: error: unsupported relocation type
14+
.quad foo@toc

0 commit comments

Comments
 (0)