[llvm] 8cad4dd - [MC,X86] Property report error for modifiers with incorrect size

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 19 22:14:31 PDT 2023


Author: Fangrui Song
Date: 2023-09-19T22:14:25-07:00
New Revision: 8cad4dd00077226f9fca5d176b7ef6ed9f668008

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

LOG: [MC,X86] Property report error for modifiers with incorrect size

Added: 
    

Modified: 
    llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
    llvm/test/MC/ELF/relocation-386.s
    llvm/test/MC/ELF/relocation.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
index d083bf245af22db..373e29bf6a835f8 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
@@ -140,8 +140,9 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
     }
     llvm_unreachable("unexpected relocation type!");
   case MCSymbolRefExpr::VK_GOTOFF:
-    assert(Type == RT64_64);
     assert(!IsPCRel);
+    if (Type != RT64_64)
+      Ctx.reportError(Loc, "unsupported relocation type");
     return ELF::R_X86_64_GOTOFF64;
   case MCSymbolRefExpr::VK_TPOFF:
     assert(!IsPCRel);
@@ -229,7 +230,7 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
 
 enum X86_32RelType { RT32_NONE, RT32_32, RT32_16, RT32_8 };
 
-static unsigned getRelocType32(MCContext &Ctx,
+static unsigned getRelocType32(MCContext &Ctx, SMLoc Loc,
                                MCSymbolRefExpr::VariantKind Modifier,
                                X86_32RelType Type, bool IsPCRel,
                                MCFixupKind Kind) {
@@ -252,7 +253,8 @@ static unsigned getRelocType32(MCContext &Ctx,
     }
     llvm_unreachable("unexpected relocation type!");
   case MCSymbolRefExpr::VK_GOT:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     if (IsPCRel)
       return ELF::R_386_GOTPC;
     // Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we
@@ -264,49 +266,61 @@ static unsigned getRelocType32(MCContext &Ctx,
                ? ELF::R_386_GOT32X
                : ELF::R_386_GOT32;
   case MCSymbolRefExpr::VK_GOTOFF:
-    assert(Type == RT32_32);
     assert(!IsPCRel);
+    if (Type != RT32_32)
+      break;
     return ELF::R_386_GOTOFF;
   case MCSymbolRefExpr::VK_TLSCALL:
     return ELF::R_386_TLS_DESC_CALL;
   case MCSymbolRefExpr::VK_TLSDESC:
     return ELF::R_386_TLS_GOTDESC;
   case MCSymbolRefExpr::VK_TPOFF:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_LE_32;
   case MCSymbolRefExpr::VK_DTPOFF:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_LDO_32;
   case MCSymbolRefExpr::VK_TLSGD:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_GD;
   case MCSymbolRefExpr::VK_GOTTPOFF:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_IE_32;
   case MCSymbolRefExpr::VK_PLT:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     return ELF::R_386_PLT32;
   case MCSymbolRefExpr::VK_INDNTPOFF:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_IE;
   case MCSymbolRefExpr::VK_NTPOFF:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_LE;
   case MCSymbolRefExpr::VK_GOTNTPOFF:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_GOTIE;
   case MCSymbolRefExpr::VK_TLSLDM:
-    assert(Type == RT32_32);
+    if (Type != RT32_32)
+      break;
     assert(!IsPCRel);
     return ELF::R_386_TLS_LDM;
   }
+  Ctx.reportError(Loc, "unsupported relocation type");
+  return ELF::R_386_NONE;
 }
 
 unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
@@ -329,7 +343,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
     break;
   case RT64_64:
     Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
-    break;
+    return ELF::R_386_NONE;
   case RT64_32:
   case RT64_32S:
     RelType = RT32_32;
@@ -341,7 +355,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
     RelType = RT32_8;
     break;
   }
-  return getRelocType32(Ctx, Modifier, RelType, IsPCRel, Kind);
+  return getRelocType32(Ctx, Fixup.getLoc(), Modifier, RelType, IsPCRel, Kind);
 }
 
 std::unique_ptr<MCObjectTargetWriter>

diff  --git a/llvm/test/MC/ELF/relocation-386.s b/llvm/test/MC/ELF/relocation-386.s
index e49b25a25ce53bc..dd252f5ff74cb27 100644
--- a/llvm/test/MC/ELF/relocation-386.s
+++ b/llvm/test/MC/ELF/relocation-386.s
@@ -1,5 +1,6 @@
 // RUN: llvm-mc -filetype=obj -triple i386-pc-linux-gnu %s -relax-relocations=false -o - | llvm-readobj -r  - | FileCheck  %s --check-prefix=CHECK --check-prefix=I386
 // RUN: llvm-mc -filetype=obj -triple i386-pc-elfiamcu %s -relax-relocations=false  -o - | llvm-readobj -r  - | FileCheck  %s --check-prefix=CHECK --check-prefix=IAMCU
+// RUN: not llvm-mc -filetype=obj -triple=i686 --defsym ERR=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
 
 // Test that we produce the correct relocation types and that the relocations
 // correctly point to the section or the symbol.
@@ -127,6 +128,19 @@ bar2:
         .word foo
         .byte foo
 
+.ifdef ERR
+// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
+.quad foo at GOT
+// ERR: [[#@LINE+1]]:8: error: unsupported relocation type
+.short foo at GOTOFF
+// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
+.dc.w foo at TPOFF
+// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
+.dc.w foo at INDNTPOFF
+// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
+.dc.w foo at NTPOFF
+.endif
+
         .section        zedsec,"awT", at progbits
 zed:
         .long 0

diff  --git a/llvm/test/MC/ELF/relocation.s b/llvm/test/MC/ELF/relocation.s
index 8802330c90b7642..797e31f529b3db7 100644
--- a/llvm/test/MC/ELF/relocation.s
+++ b/llvm/test/MC/ELF/relocation.s
@@ -1,4 +1,5 @@
 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -S --sr  - | FileCheck  %s
+// RUN: not llvm-mc -filetype=obj -triple x86_64 --defsym ERR=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
 
 // Test that we produce the correct relocation.
 
@@ -110,3 +111,8 @@ weak_sym:
 // CHECK-NEXT:       0x105 R_X86_64_PC32 pr23272 0x0
 // CHECK-NEXT:     ]
 // CHECK-NEXT:   }
+
+.ifdef ERR
+// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
+.long foo at gotoff
+.endif


        


More information about the llvm-commits mailing list