[llvm] 803fbdd - [PowerPC] Report proper error for invalid relocation specifier

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 5 22:14:00 PDT 2025


Author: Fangrui Song
Date: 2025-04-05T22:13:55-07:00
New Revision: 803fbdd1faa813303cda3d93b3364eca2344ab6a

URL: https://github.com/llvm/llvm-project/commit/803fbdd1faa813303cda3d93b3364eca2344ab6a
DIFF: https://github.com/llvm/llvm-project/commit/803fbdd1faa813303cda3d93b3364eca2344ab6a.diff

LOG: [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 94821ce45fe93aa78cc5ea03cd9deac91b7af127

Added: 
    llvm/test/MC/PowerPC/relocation-specifier-err.s

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

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


################################################################################
diff  --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
index e33961a973d1e..d7ff92f64bf4f 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
@@ -38,25 +38,15 @@ PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
                             Is64Bit ?  ELF::EM_PPC64 : ELF::EM_PPC,
                             /*HasRelocationAddend*/ true) {}
 
-static PPCMCExpr::Specifier getAccessVariant(const MCValue &Target,
-                                             const MCFixup &Fixup) {
-  const MCExpr *Expr = Fixup.getValue();
-
-  if (Expr->getKind() != MCExpr::Target)
-    return PPCMCExpr::Specifier(Target.getAccessVariant());
-  return cast<PPCMCExpr>(Expr)->getSpecifier();
-}
-
 unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
                                           const MCFixup &Fixup,
                                           bool IsPCRel) const {
   MCFixupKind Kind = Fixup.getKind();
   if (Kind >= FirstLiteralRelocationKind)
     return Kind - FirstLiteralRelocationKind;
-  auto RefKind = static_cast<PPCMCExpr::Specifier>(Target.getRefKind());
-  auto Modifier = getAccessVariant(Target, Fixup);
-
-  switch (PPCMCExpr::Specifier(Modifier)) {
+  SMLoc Loc = Fixup.getValue()->getLoc();
+  auto Spec = static_cast<PPCMCExpr::Specifier>(Target.getSpecifier());
+  switch (Spec) {
   case PPCMCExpr::VK_DTPMOD:
   case PPCMCExpr::VK_DTPREL:
   case PPCMCExpr::VK_DTPREL_HA:
@@ -108,7 +98,7 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
   }
 
   // determine the type of the relocation
-  unsigned Type;
+  unsigned Type = 0;
   if (IsPCRel) {
     switch (Fixup.getTargetKind()) {
     default:
@@ -116,8 +106,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
     case PPC::fixup_ppc_br24:
     case PPC::fixup_ppc_br24abs:
     case PPC::fixup_ppc_br24_notoc:
-      switch (Modifier) {
-      default: llvm_unreachable("Unsupported Modifier");
+      switch (Spec) {
+      default:
+        Ctx.reportError(Loc, "unsupported relocation type");
+        break;
       case PPCMCExpr::VK_None:
         Type = ELF::R_PPC_REL24;
         break;
@@ -137,9 +129,9 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       Type = ELF::R_PPC_REL14;
       break;
     case PPC::fixup_ppc_half16:
-      switch (RefKind) {
+      switch (Spec) {
       default:
-        Ctx.reportError(Fixup.getLoc(), "invalid VariantKind");
+        Ctx.reportError(Loc, "unsupported relocation type");
         return ELF::R_PPC_NONE;
       case PPCMCExpr::VK_None:
         return ELF::R_PPC_REL16;
@@ -157,9 +149,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       errs() << '\n';
       report_fatal_error("Invalid PC-relative half16ds relocation");
     case PPC::fixup_ppc_pcrel34:
-      switch (Modifier) {
+      switch (Spec) {
       default:
-        llvm_unreachable("Unsupported Modifier for fixup_ppc_pcrel34");
+        Ctx.reportError(Loc, "unsupported relocation type");
+        break;
       case PPCMCExpr::VK_PCREL:
         Type = ELF::R_PPC64_PCREL34;
         break;
@@ -196,9 +189,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
       break;
     case PPC::fixup_ppc_half16:
-      switch (Modifier) {
+      switch (Spec) {
       default:
-        llvm_unreachable("Unsupported specifier");
+        Ctx.reportError(Loc, "unsupported relocation type");
+        break;
       case PPCMCExpr::VK_LO:
         return ELF::R_PPC_ADDR16_LO;
       case PPCMCExpr::VK_HI:
@@ -371,10 +365,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       break;
     case PPC::fixup_ppc_half16ds:
     case PPC::fixup_ppc_half16dq:
-      switch (Modifier) {
+      switch (Spec) {
       default:
-        Ctx.reportError(Fixup.getLoc(), "invalid VariantKind");
-        return ELF::R_PPC64_NONE;
+        Ctx.reportError(Loc, "unsupported relocation type");
+        break;
       case PPCMCExpr::VK_LO:
         return ELF::R_PPC64_ADDR16_LO_DS;
       case PPCMCExpr::VK_None:
@@ -419,8 +413,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       }
       break;
     case PPC::fixup_ppc_nofixup:
-      switch (Modifier) {
-      default: llvm_unreachable("Unsupported Modifier");
+      switch (Spec) {
+      default:
+        Ctx.reportError(Loc, "unsupported relocation type");
+        break;
       case PPCMCExpr::VK_TLSGD:
         if (is64Bit())
           Type = ELF::R_PPC64_TLSGD;
@@ -445,9 +441,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       }
       break;
     case PPC::fixup_ppc_imm34:
-      switch (Modifier) {
+      switch (Spec) {
       default:
-        report_fatal_error("Unsupported Modifier for fixup_ppc_imm34.");
+        Ctx.reportError(Loc, "unsupported relocation type");
+        break;
       case PPCMCExpr::VK_DTPREL:
         Type = ELF::R_PPC64_DTPREL34;
         break;
@@ -457,8 +454,10 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       }
       break;
     case FK_Data_8:
-      switch (Modifier) {
-      default: llvm_unreachable("Unsupported Modifier");
+      switch (Spec) {
+      default:
+        Ctx.reportError(Loc, "unsupported relocation type");
+        break;
       case PPCMCExpr::VK_TOCBASE:
         Type = ELF::R_PPC64_TOC;
         break;
@@ -477,7 +476,7 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       }
       break;
     case FK_Data_4:
-      switch (Modifier) {
+      switch (Spec) {
       case PPCMCExpr::VK_DTPREL:
         Type = ELF::R_PPC_DTPREL32;
         break;

diff  --git a/llvm/test/MC/PowerPC/ppc64-errors-emit-obj.s b/llvm/test/MC/PowerPC/ppc64-errors-emit-obj.s
deleted file mode 100644
index 0d2c879380e0a..0000000000000
--- a/llvm/test/MC/PowerPC/ppc64-errors-emit-obj.s
+++ /dev/null
@@ -1,7 +0,0 @@
-# RUN: not --crash llvm-mc -triple powerpc64-- --filetype=obj < %s 2> %t
-# RUN: FileCheck < %t %s
-# RUN: not --crash llvm-mc -triple powerpc64le-- --filetype=obj < %s 2> %t
-# RUN: FileCheck < %t %s
-
-# CHECK: Unsupported Modifier for fixup_ppc_imm34.
-paddi 3, 13, symbol at toc, 0

diff  --git a/llvm/test/MC/PowerPC/relocation-specifier-err.s b/llvm/test/MC/PowerPC/relocation-specifier-err.s
new file mode 100644
index 0000000000000..835fde7519ace
--- /dev/null
+++ b/llvm/test/MC/PowerPC/relocation-specifier-err.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc -triple powerpc64 --filetype=obj %s -o %t 2>&1 | FileCheck %s
+# RUN: not llvm-mc -triple powerpc64le --filetype=obj %s -o %t 2>&1 | FileCheck %s
+
+# CHECK: [[#@LINE+1]]:4: error: unsupported relocation type
+bl foo at toc
+
+# CHECK: [[#@LINE+1]]:12: error: unsupported relocation type
+addi 3, 3, foo at plt
+
+# CHECK: [[#@LINE+1]]:14: error: unsupported relocation type
+paddi 3, 13, foo at toc, 0
+
+# CHECK: [[#@LINE+1]]:7: error: unsupported relocation type
+.quad foo at toc


        


More information about the llvm-commits mailing list