[llvm] e71fa03 - [VE] Change error handling of data references

Kazushi Marukawa via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 8 04:07:02 PST 2021


Author: Kazushi (Jam) Marukawa
Date: 2021-12-08T21:06:57+09:00
New Revision: e71fa0336f91ff7d55e3b41c1afa15019a214ef9

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

LOG: [VE] Change error handling of data references

Change to use Ctx.reportError() instead of llvm_unreachable for
better error handling.  Also correct evaluateAsRelocatableImpl().

Reviewed By: simoll

Differential Revision: https://reviews.llvm.org/D115251

Added: 
    llvm/test/MC/VE/data-reloc-error.s
    llvm/test/MC/VE/data-reloc.s

Modified: 
    llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
    llvm/lib/Target/VE/MCTargetDesc/VEMCCodeEmitter.cpp
    llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
index c1d18dc4f9daa..3b0a469441091 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
@@ -9,6 +9,7 @@
 #include "VEFixupKinds.h"
 #include "VEMCExpr.h"
 #include "VEMCTargetDesc.h"
+#include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCELFObjectWriter.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCObjectWriter.h"
@@ -46,16 +47,26 @@ unsigned VEELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
   if (IsPCRel) {
     switch (Fixup.getTargetKind()) {
     default:
-      llvm_unreachable("Unimplemented fixup -> relocation");
+      Ctx.reportError(Fixup.getLoc(), "Unsupported pc-relative fixup kind");
+      return ELF::R_VE_NONE;
+    case FK_Data_1:
     case FK_PCRel_1:
-      llvm_unreachable("Unimplemented fixup fk_data_1 -> relocation");
+      Ctx.reportError(Fixup.getLoc(),
+                      "1-byte pc-relative data relocation is not supported");
+      return ELF::R_VE_NONE;
+    case FK_Data_2:
     case FK_PCRel_2:
-      llvm_unreachable("Unimplemented fixup fk_data_2 -> relocation");
-    // FIXME: relative kind?
+      Ctx.reportError(Fixup.getLoc(),
+                      "2-byte pc-relative data relocation is not supported");
+      return ELF::R_VE_NONE;
+    case FK_Data_4:
     case FK_PCRel_4:
-      return ELF::R_VE_REFLONG;
+      return ELF::R_VE_SREL32;
+    case FK_Data_8:
     case FK_PCRel_8:
-      return ELF::R_VE_REFQUAD;
+      Ctx.reportError(Fixup.getLoc(),
+                      "8-byte pc-relative data relocation is not supported");
+      return ELF::R_VE_NONE;
     case VE::fixup_ve_srel32:
       return ELF::R_VE_SREL32;
     case VE::fixup_ve_pc_hi32:
@@ -67,11 +78,14 @@ unsigned VEELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
 
   switch (Fixup.getTargetKind()) {
   default:
-    llvm_unreachable("Unimplemented fixup -> relocation");
+    Ctx.reportError(Fixup.getLoc(), "Unknown ELF relocation type");
+    return ELF::R_VE_NONE;
   case FK_Data_1:
-    llvm_unreachable("Unimplemented fixup fk_data_1 -> relocation");
+    Ctx.reportError(Fixup.getLoc(), "1-byte data relocation is not supported");
+    return ELF::R_VE_NONE;
   case FK_Data_2:
-    llvm_unreachable("Unimplemented fixup fk_data_2 -> relocation");
+    Ctx.reportError(Fixup.getLoc(), "2-byte data relocation is not supported");
+    return ELF::R_VE_NONE;
   case FK_Data_4:
     return ELF::R_VE_REFLONG;
   case FK_Data_8:
@@ -79,15 +93,21 @@ unsigned VEELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
   case VE::fixup_ve_reflong:
     return ELF::R_VE_REFLONG;
   case VE::fixup_ve_srel32:
-    llvm_unreachable("Unimplemented fixup pc_hi32 -> relocation");
+    Ctx.reportError(Fixup.getLoc(),
+                    "A non pc-relative srel32 relocation is not supported");
+    return ELF::R_VE_NONE;
   case VE::fixup_ve_hi32:
     return ELF::R_VE_HI32;
   case VE::fixup_ve_lo32:
     return ELF::R_VE_LO32;
   case VE::fixup_ve_pc_hi32:
-    llvm_unreachable("Unimplemented fixup pc_hi32 -> relocation");
+    Ctx.reportError(Fixup.getLoc(),
+                    "A non pc-relative pc_hi32 relocation is not supported");
+    return ELF::R_VE_NONE;
   case VE::fixup_ve_pc_lo32:
-    llvm_unreachable("Unimplemented fixup pc_lo32 -> relocation");
+    Ctx.reportError(Fixup.getLoc(),
+                    "A non pc-relative pc_lo32 relocation is not supported");
+    return ELF::R_VE_NONE;
   case VE::fixup_ve_got_hi32:
     return ELF::R_VE_GOT_HI32;
   case VE::fixup_ve_got_lo32:

diff  --git a/llvm/lib/Target/VE/MCTargetDesc/VEMCCodeEmitter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEMCCodeEmitter.cpp
index 878a3f6e69dce..65bb0cf8b0d78 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEMCCodeEmitter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEMCCodeEmitter.cpp
@@ -102,11 +102,11 @@ unsigned VEMCCodeEmitter::getMachineOpValue(const MCInst &MI,
                                             const MCSubtargetInfo &STI) const {
   if (MO.isReg())
     return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
-
   if (MO.isImm())
-    return MO.getImm();
+    return static_cast<unsigned>(MO.getImm());
 
   assert(MO.isExpr());
+
   const MCExpr *Expr = MO.getExpr();
   if (const VEMCExpr *SExpr = dyn_cast<VEMCExpr>(Expr)) {
     MCFixupKind Kind = (MCFixupKind)SExpr->getFixupKind();

diff  --git a/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
index a3ce3b3309be8..4d45918ad0aa7 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
@@ -12,11 +12,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "VEMCExpr.h"
+#include "llvm/BinaryFormat/ELF.h"
 #include "llvm/MC/MCAssembler.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCObjectStreamer.h"
 #include "llvm/MC/MCSymbolELF.h"
-#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/MC/MCValue.h"
 
 using namespace llvm;
 
@@ -174,7 +175,13 @@ VE::Fixups VEMCExpr::getFixupKind(VEMCExpr::VariantKind Kind) {
 bool VEMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
                                          const MCAsmLayout *Layout,
                                          const MCFixup *Fixup) const {
-  return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
+  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
+    return false;
+
+  Res =
+      MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
+
+  return true;
 }
 
 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {

diff  --git a/llvm/test/MC/VE/data-reloc-error.s b/llvm/test/MC/VE/data-reloc-error.s
new file mode 100644
index 0000000000000..e312ac4e02d1a
--- /dev/null
+++ b/llvm/test/MC/VE/data-reloc-error.s
@@ -0,0 +1,26 @@
+# RUN: not llvm-mc -filetype=obj -triple=ve %s -o /dev/null 2>&1 | \
+# RUN:     FileCheck %s
+# RUN: not llvm-mc -filetype=obj -triple=ve -position-independent %s \
+# RUN:     -o /dev/null 2>&1 | FileCheck %s
+
+.data
+a:
+## An undefined reference of _GLOBAL_OFFSET_TABLE_ causes .got[0] to be
+## allocated to store _DYNAMIC.
+.byte _GLOBAL_OFFSET_TABLE_
+.byte _GLOBAL_OFFSET_TABLE_ - .
+.2byte _GLOBAL_OFFSET_TABLE_
+.2byte _GLOBAL_OFFSET_TABLE_ - .
+.8byte _GLOBAL_OFFSET_TABLE_ - .
+
+# CHECK:      data-reloc-error.s:10:7: error: 1-byte data relocation is not supported
+# CHECK-NEXT: .byte _GLOBAL_OFFSET_TABLE_
+# CHECK:      data-reloc-error.s:11:7: error: 1-byte pc-relative data relocation is not supported
+# CHECK-NEXT: .byte _GLOBAL_OFFSET_TABLE_ - .
+# CHECK:      data-reloc-error.s:12:8: error: 2-byte data relocation is not supported
+# CHECK-NEXT: .2byte _GLOBAL_OFFSET_TABLE_
+# CHECK:      data-reloc-error.s:13:8: error: 2-byte pc-relative data relocation is not supported
+# CHECK-NEXT: .2byte _GLOBAL_OFFSET_TABLE_ - .
+# CHECK:      data-reloc-error.s:14:8: error: 8-byte pc-relative data relocation is not supported
+# CHECK-NEXT: .8byte _GLOBAL_OFFSET_TABLE_ - .
+

diff  --git a/llvm/test/MC/VE/data-reloc.s b/llvm/test/MC/VE/data-reloc.s
new file mode 100644
index 0000000000000..e64a4364ee77a
--- /dev/null
+++ b/llvm/test/MC/VE/data-reloc.s
@@ -0,0 +1,16 @@
+# RUN: llvm-mc -triple=ve -filetype=obj %s -o - | \
+# RUN:     llvm-objdump -r - | FileCheck %s
+# RUN: llvm-mc -triple=ve -filetype=obj -position-independent %s -o - | \
+# RUN:     llvm-objdump -r - | FileCheck %s
+
+.data
+a:
+## An undefined reference of _GLOBAL_OFFSET_TABLE_ causes .got[0] to be
+## allocated to store _DYNAMIC.
+.4byte _GLOBAL_OFFSET_TABLE_ - 8
+.4byte _GLOBAL_OFFSET_TABLE_ - . - 8
+.8byte _GLOBAL_OFFSET_TABLE_ - 8
+
+# CHECK: 0000000000000000 R_VE_REFLONG _GLOBAL_OFFSET_TABLE_-0x8
+# CHECK: 0000000000000004 R_VE_SREL32 _GLOBAL_OFFSET_TABLE_-0x8
+# CHECK: 0000000000000008 R_VE_REFQUAD _GLOBAL_OFFSET_TABLE_-0x8


        


More information about the llvm-commits mailing list