[llvm] fc79ca6 - [MC] x86-32: properly report error for .quad relocation

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun May 7 20:57:12 PDT 2023


Author: Fangrui Song
Date: 2023-05-07T20:57:07-07:00
New Revision: fc79ca6be840c1c9b4b2c4a01c6f808de867dac7

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

LOG: [MC] x86-32: properly report error for .quad relocation

Fix a llvm_unreachable crash in -DLLVM_ENABLE_ASSERTIONS=on builds and
possible accept-invalid in -DLLVM_ENABLE_ASSERTIONS=off builds.

Added: 
    llvm/test/MC/X86/directive-quad.s

Modified: 
    llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
index 8ab86f46ffe6f..d083bf245af22 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
@@ -229,23 +229,6 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
 
 enum X86_32RelType { RT32_NONE, RT32_32, RT32_16, RT32_8 };
 
-static X86_32RelType getType32(X86_64RelType T) {
-  switch (T) {
-  case RT64_NONE:
-    return RT32_NONE;
-  case RT64_64:
-    llvm_unreachable("Unimplemented");
-  case RT64_32:
-  case RT64_32S:
-    return RT32_32;
-  case RT64_16:
-    return RT32_16;
-  case RT64_8:
-    return RT32_8;
-  }
-  llvm_unreachable("unexpected relocation type!");
-}
-
 static unsigned getRelocType32(MCContext &Ctx,
                                MCSymbolRefExpr::VariantKind Modifier,
                                X86_32RelType Type, bool IsPCRel,
@@ -339,7 +322,26 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
 
   assert((getEMachine() == ELF::EM_386 || getEMachine() == ELF::EM_IAMCU) &&
          "Unsupported ELF machine type.");
-  return getRelocType32(Ctx, Modifier, getType32(Type), IsPCRel, Kind);
+
+  X86_32RelType RelType = RT32_NONE;
+  switch (Type) {
+  case RT64_NONE:
+    break;
+  case RT64_64:
+    Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
+    break;
+  case RT64_32:
+  case RT64_32S:
+    RelType = RT32_32;
+    break;
+  case RT64_16:
+    RelType = RT32_16;
+    break;
+  case RT64_8:
+    RelType = RT32_8;
+    break;
+  }
+  return getRelocType32(Ctx, Modifier, RelType, IsPCRel, Kind);
 }
 
 std::unique_ptr<MCObjectTargetWriter>

diff  --git a/llvm/test/MC/X86/directive-quad.s b/llvm/test/MC/X86/directive-quad.s
new file mode 100644
index 0000000000000..b6048ebbe8703
--- /dev/null
+++ b/llvm/test/MC/X86/directive-quad.s
@@ -0,0 +1,6 @@
+# RUN: not llvm-mc -filetype=obj -triple=i386 %s -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: :[[#@LINE+1]]:7: error: unsupported relocation type
+.quad foo
+# CHECK: :[[#@LINE+1]]:8: error: unsupported relocation type
+.8byte foo


        


More information about the llvm-commits mailing list