From ec4f06a77ded520747e119fdac1f70a010d6b6a8 Mon Sep 17 00:00:00 2001 From: Luís Marques Date: Thu, 19 Dec 2019 17:20:02 +0000 Subject: [RISCV] Don't crash on unsupported relocations Summary: Instead of crashing due to the `llvm_unreachable`, provide a proper error when invalid fixups/relocations are encountered. Reviewers: asb, lenary Reviewed By: asb Tags: #llvm Differential Revision: https://reviews.llvm.org/D71536 --- llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp | 13 +++++++++++-- llvm/test/MC/RISCV/fixups-invalid.s | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 llvm/test/MC/RISCV/fixups-invalid.s (limited to 'llvm') diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp index cab2bbcb81b..08b75795ed4 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp @@ -9,6 +9,7 @@ #include "MCTargetDesc/RISCVFixupKinds.h" #include "MCTargetDesc/RISCVMCExpr.h" #include "MCTargetDesc/RISCVMCTargetDesc.h" +#include "llvm/MC/MCContext.h" #include "llvm/MC/MCELFObjectWriter.h" #include "llvm/MC/MCFixup.h" #include "llvm/MC/MCObjectWriter.h" @@ -54,7 +55,8 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx, if (IsPCRel) { switch (Kind) { default: - llvm_unreachable("invalid fixup kind!"); + Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type"); + return ELF::R_RISCV_NONE; case FK_Data_4: case FK_PCRel_4: return ELF::R_RISCV_32_PCREL; @@ -87,7 +89,14 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx, switch (Kind) { default: - llvm_unreachable("invalid fixup kind!"); + Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type"); + return ELF::R_RISCV_NONE; + case FK_Data_1: + Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported"); + return ELF::R_RISCV_NONE; + case FK_Data_2: + Ctx.reportError(Fixup.getLoc(), "2-byte data relocations not supported"); + return ELF::R_RISCV_NONE; case FK_Data_4: if (Expr->getKind() == MCExpr::Target && cast(Expr)->getKind() == RISCVMCExpr::VK_RISCV_32_PCREL) diff --git a/llvm/test/MC/RISCV/fixups-invalid.s b/llvm/test/MC/RISCV/fixups-invalid.s new file mode 100644 index 00000000000..55ced21e1c9 --- /dev/null +++ b/llvm/test/MC/RISCV/fixups-invalid.s @@ -0,0 +1,7 @@ +# RUN: not llvm-mc -filetype=obj %s -triple=riscv32 -o /dev/null 2>&1 \ +# RUN: | FileCheck %s +# RUN: not llvm-mc -filetype=obj %s -triple=riscv64 -o /dev/null 2>&1 \ +# RUN: | FileCheck %s + +.byte foo # CHECK: [[@LINE]]:7: error: 1-byte data relocations not supported +.2byte foo # CHECK: [[@LINE]]:8: error: 2-byte data relocations not supported -- cgit v1.2.3