From 7f81c885acf8780beb4b923e989172527f78478c Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 13 Jan 2016 22:56:57 +0000 Subject: [PATCH] Convert a few assert failures into proper errors. Fixes PR25944. llvm-svn: 257697 --- include/llvm/MC/MCELFObjectWriter.h | 5 ++-- lib/MC/ELFObjectWriter.cpp | 8 +++--- .../MCTargetDesc/AArch64ELFObjectWriter.cpp | 11 ++++---- .../MCTargetDesc/AMDGPUELFObjectWriter.cpp | 4 +-- .../ARM/MCTargetDesc/ARMELFObjectWriter.cpp | 6 ++-- .../BPF/MCTargetDesc/BPFELFObjectWriter.cpp | 6 ++-- .../MCTargetDesc/HexagonELFObjectWriter.cpp | 7 +++-- .../Mips/MCTargetDesc/MipsELFObjectWriter.cpp | 7 +++-- .../MCTargetDesc/PPCELFObjectWriter.cpp | 6 ++-- .../MCTargetDesc/SparcELFObjectWriter.cpp | 7 +++-- .../MCTargetDesc/SystemZMCObjectWriter.cpp | 7 +++-- .../X86/MCTargetDesc/X86ELFObjectWriter.cpp | 28 ++++++++++++------- test/MC/X86/error-reloc.s | 21 ++++++++++++++ 13 files changed, 79 insertions(+), 44 deletions(-) create mode 100644 test/MC/X86/error-reloc.s diff --git a/include/llvm/MC/MCELFObjectWriter.h b/include/llvm/MC/MCELFObjectWriter.h index 193dac018b2..83726257ad2 100644 --- a/include/llvm/MC/MCELFObjectWriter.h +++ b/include/llvm/MC/MCELFObjectWriter.h @@ -17,6 +17,7 @@ namespace llvm { class MCAssembler; +class MCContext; class MCFixup; class MCFragment; class MCObjectWriter; @@ -64,8 +65,8 @@ public: virtual ~MCELFObjectTargetWriter() {} - virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const = 0; + virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const = 0; virtual bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const; diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index f3b2a86f6bc..7e3ddda1815 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -129,9 +129,9 @@ class ELFObjectWriter : public MCObjectWriter { bool hasRelocationAddend() const { return TargetObjectWriter->hasRelocationAddend(); } - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const { - return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel); + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const { + return TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel); } void align(unsigned Alignment); @@ -682,7 +682,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm, } } - unsigned Type = GetRelocType(Target, Fixup, IsPCRel); + unsigned Type = getRelocType(Ctx, Target, Fixup, IsPCRel); bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type); if (!RelocateWithSymbol && SymA && !SymA->isUndefined()) C += Layout.getSymbolOffset(*SymA); diff --git a/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp b/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp index 1f516d1db89..e4f8e27a254 100644 --- a/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp +++ b/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp @@ -29,8 +29,8 @@ public: ~AArch64ELFObjectWriter() override; protected: - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; private: }; @@ -43,9 +43,10 @@ AArch64ELFObjectWriter::AArch64ELFObjectWriter(uint8_t OSABI, AArch64ELFObjectWriter::~AArch64ELFObjectWriter() {} -unsigned AArch64ELFObjectWriter::GetRelocType(const MCValue &Target, - const MCFixup &Fixup, - bool IsPCRel) const { +unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx, + const MCValue &Target, + const MCFixup &Fixup, + bool IsPCRel) const { AArch64MCExpr::VariantKind RefKind = static_cast(Target.getRefKind()); AArch64MCExpr::VariantKind SymLoc = AArch64MCExpr::getSymbolLoc(RefKind); diff --git a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp index 820f17df896..4302737396f 100644 --- a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp +++ b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp @@ -20,8 +20,8 @@ class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter { public: AMDGPUELFObjectWriter(bool Is64Bit); protected: - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override { + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override { return Fixup.getKind(); } diff --git a/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp b/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp index 52eba8be288..388ce799b6a 100644 --- a/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp +++ b/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp @@ -34,8 +34,8 @@ namespace { ~ARMELFObjectWriter() override; - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const override; @@ -67,7 +67,7 @@ bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym, // Need to examine the Fixup when determining whether to // emit the relocation as an explicit symbol or as a section relative // offset -unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, +unsigned ARMELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const { return GetRelocTypeInner(Target, Fixup, IsPCRel); diff --git a/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp b/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp index 87cdd5eb9da..115f01006ca 100644 --- a/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp +++ b/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp @@ -22,8 +22,8 @@ public: ~BPFELFObjectWriter() override; protected: - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; }; } @@ -33,7 +33,7 @@ BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI) BPFELFObjectWriter::~BPFELFObjectWriter() {} -unsigned BPFELFObjectWriter::GetRelocType(const MCValue &Target, +unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const { // determine the type of the relocation diff --git a/lib/Target/Hexagon/MCTargetDesc/HexagonELFObjectWriter.cpp b/lib/Target/Hexagon/MCTargetDesc/HexagonELFObjectWriter.cpp index da5d4d1da69..cd5d7c7d3f6 100644 --- a/lib/Target/Hexagon/MCTargetDesc/HexagonELFObjectWriter.cpp +++ b/lib/Target/Hexagon/MCTargetDesc/HexagonELFObjectWriter.cpp @@ -28,8 +28,8 @@ private: public: HexagonELFObjectWriter(uint8_t OSABI, StringRef C); - unsigned GetRelocType(MCValue const &Target, MCFixup const &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, MCValue const &Target, + MCFixup const &Fixup, bool IsPCRel) const override; }; } @@ -38,7 +38,8 @@ HexagonELFObjectWriter::HexagonELFObjectWriter(uint8_t OSABI, StringRef C) /*HasRelocationAddend*/ true), CPU(C) {} -unsigned HexagonELFObjectWriter::GetRelocType(MCValue const & /*Target*/, +unsigned HexagonELFObjectWriter::getRelocType(MCContext &Ctx, + MCValue const & /*Target*/, MCFixup const &Fixup, bool IsPCRel) const { switch ((unsigned)Fixup.getKind()) { diff --git a/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp b/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp index 5b9f02b89be..3b702fe4bee 100644 --- a/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp +++ b/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp @@ -44,8 +44,8 @@ struct MipsRelocationEntry { ~MipsELFObjectWriter() override; - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const override; virtual void sortRelocs(const MCAssembler &Asm, @@ -61,7 +61,8 @@ MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, MipsELFObjectWriter::~MipsELFObjectWriter() {} -unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target, +unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx, + const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const { // Determine the type of the relocation. diff --git a/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp b/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp index dd994956870..e54d4903a6c 100644 --- a/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp +++ b/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp @@ -25,8 +25,8 @@ namespace { PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI); protected: - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const override; @@ -66,7 +66,7 @@ static MCSymbolRefExpr::VariantKind getAccessVariant(const MCValue &Target, llvm_unreachable("unknown PPCMCExpr kind"); } -unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target, +unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const { MCSymbolRefExpr::VariantKind Modifier = getAccessVariant(Target, Fixup); diff --git a/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp b/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp index 0be60fd7a05..d35e45e0346 100644 --- a/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp +++ b/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp @@ -29,8 +29,8 @@ namespace { ~SparcELFObjectWriter() override {} protected: - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const override; @@ -38,7 +38,8 @@ namespace { }; } -unsigned SparcELFObjectWriter::GetRelocType(const MCValue &Target, +unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx, + const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const { diff --git a/lib/Target/SystemZ/MCTargetDesc/SystemZMCObjectWriter.cpp b/lib/Target/SystemZ/MCTargetDesc/SystemZMCObjectWriter.cpp index ee1af023769..368c95f7bac 100644 --- a/lib/Target/SystemZ/MCTargetDesc/SystemZMCObjectWriter.cpp +++ b/lib/Target/SystemZ/MCTargetDesc/SystemZMCObjectWriter.cpp @@ -24,8 +24,8 @@ public: protected: // Override MCELFObjectTargetWriter. - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; }; } // end anonymous namespace @@ -106,7 +106,8 @@ static unsigned getPLTReloc(unsigned Kind) { llvm_unreachable("Unsupported absolute address"); } -unsigned SystemZObjectWriter::GetRelocType(const MCValue &Target, +unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx, + const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const { MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant(); diff --git a/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp b/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp index 736c39dfb6f..f2e519aef3b 100644 --- a/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp +++ b/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp @@ -9,6 +9,7 @@ #include "MCTargetDesc/X86FixupKinds.h" #include "MCTargetDesc/X86MCTargetDesc.h" +#include "llvm/MC/MCContext.h" #include "llvm/MC/MCELFObjectWriter.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCValue.h" @@ -25,8 +26,8 @@ namespace { ~X86ELFObjectWriter() override; protected: - unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) const override; + unsigned getRelocType(MCContext &Ctx, const MCValue &Target, + const MCFixup &Fixup, bool IsPCRel) const override; }; } @@ -77,7 +78,14 @@ static X86_64RelType getType64(unsigned Kind, } } -static unsigned getRelocType64(MCSymbolRefExpr::VariantKind Modifier, +static void checkIs32(MCContext &Ctx, SMLoc Loc, X86_64RelType Type) { + if (Type != RT64_32) + Ctx.reportError(Loc, + "32 bit reloc applied to a field with a different size"); +} + +static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc, + MCSymbolRefExpr::VariantKind Modifier, X86_64RelType Type, bool IsPCRel) { switch (Modifier) { default: @@ -147,19 +155,19 @@ static unsigned getRelocType64(MCSymbolRefExpr::VariantKind Modifier, llvm_unreachable("Unimplemented"); } case MCSymbolRefExpr::VK_TLSGD: - assert(Type == RT64_32); + checkIs32(Ctx, Loc, Type); return ELF::R_X86_64_TLSGD; case MCSymbolRefExpr::VK_GOTTPOFF: - assert(Type == RT64_32); + checkIs32(Ctx, Loc, Type); return ELF::R_X86_64_GOTTPOFF; case MCSymbolRefExpr::VK_TLSLD: - assert(Type == RT64_32); + checkIs32(Ctx, Loc, Type); return ELF::R_X86_64_TLSLD; case MCSymbolRefExpr::VK_PLT: - assert(Type == RT64_32); + checkIs32(Ctx, Loc, Type); return ELF::R_X86_64_PLT32; case MCSymbolRefExpr::VK_GOTPCREL: - assert(Type == RT64_32); + checkIs32(Ctx, Loc, Type); return ELF::R_X86_64_GOTPCREL; } } @@ -240,13 +248,13 @@ static unsigned getRelocType32(MCSymbolRefExpr::VariantKind Modifier, } } -unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target, +unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const { MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant(); X86_64RelType Type = getType64(Fixup.getKind(), Modifier, IsPCRel); if (getEMachine() == ELF::EM_X86_64) - return getRelocType64(Modifier, Type, IsPCRel); + return getRelocType64(Ctx, Fixup.getLoc(), Modifier, Type, IsPCRel); assert((getEMachine() == ELF::EM_386 || getEMachine() == ELF::EM_IAMCU) && "Unsupported ELF machine type."); diff --git a/test/MC/X86/error-reloc.s b/test/MC/X86/error-reloc.s new file mode 100644 index 00000000000..8dd93de4e76 --- /dev/null +++ b/test/MC/X86/error-reloc.s @@ -0,0 +1,21 @@ +// RUN: not llvm-mc %s -o %t.o -filetype=obj 2>&1 | FileCheck %s + + .quad foo@gotpcrel +// CHECK: 32 bit reloc applied to a field with a different size +// CHECK-NEXT: .quad foo@gotpcrel + + .quad foo@plt +// CHECK: 32 bit reloc applied to a field with a different size +// CHECK-NEXT: .quad foo@plt + + .quad foo@tlsld +// CHECK: 32 bit reloc applied to a field with a different size +// CHECK-NEXT: .quad foo@tlsld + + .quad foo@gottpoff +// CHECK: 32 bit reloc applied to a field with a different size +// CHECK-NEXT: .quad foo@gottpoff + + .quad foo@tlsgd +// CHECK: 32 bit reloc applied to a field with a different size +// CHECK-NEXT: .quad foo@tlsgd