Convert a few assert failures into proper errors.

Fixes PR25944.

llvm-svn: 257697
This commit is contained in:
Rafael Espindola 2016-01-13 22:56:57 +00:00
parent 3dd0ff3b62
commit 8340f94df1
13 changed files with 79 additions and 44 deletions

View File

@ -17,6 +17,7 @@
namespace llvm { namespace llvm {
class MCAssembler; class MCAssembler;
class MCContext;
class MCFixup; class MCFixup;
class MCFragment; class MCFragment;
class MCObjectWriter; class MCObjectWriter;
@ -64,8 +65,8 @@ public:
virtual ~MCELFObjectTargetWriter() {} virtual ~MCELFObjectTargetWriter() {}
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const = 0; const MCFixup &Fixup, bool IsPCRel) const = 0;
virtual bool needsRelocateWithSymbol(const MCSymbol &Sym, virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
unsigned Type) const; unsigned Type) const;

View File

@ -129,9 +129,9 @@ class ELFObjectWriter : public MCObjectWriter {
bool hasRelocationAddend() const { bool hasRelocationAddend() const {
return TargetObjectWriter->hasRelocationAddend(); return TargetObjectWriter->hasRelocationAddend();
} }
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const { const MCFixup &Fixup, bool IsPCRel) const {
return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel); return TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
} }
void align(unsigned Alignment); 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); bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
if (!RelocateWithSymbol && SymA && !SymA->isUndefined()) if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
C += Layout.getSymbolOffset(*SymA); C += Layout.getSymbolOffset(*SymA);

View File

@ -29,8 +29,8 @@ public:
~AArch64ELFObjectWriter() override; ~AArch64ELFObjectWriter() override;
protected: protected:
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; const MCFixup &Fixup, bool IsPCRel) const override;
private: private:
}; };
@ -43,9 +43,10 @@ AArch64ELFObjectWriter::AArch64ELFObjectWriter(uint8_t OSABI,
AArch64ELFObjectWriter::~AArch64ELFObjectWriter() {} AArch64ELFObjectWriter::~AArch64ELFObjectWriter() {}
unsigned AArch64ELFObjectWriter::GetRelocType(const MCValue &Target, unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
const MCFixup &Fixup, const MCValue &Target,
bool IsPCRel) const { const MCFixup &Fixup,
bool IsPCRel) const {
AArch64MCExpr::VariantKind RefKind = AArch64MCExpr::VariantKind RefKind =
static_cast<AArch64MCExpr::VariantKind>(Target.getRefKind()); static_cast<AArch64MCExpr::VariantKind>(Target.getRefKind());
AArch64MCExpr::VariantKind SymLoc = AArch64MCExpr::getSymbolLoc(RefKind); AArch64MCExpr::VariantKind SymLoc = AArch64MCExpr::getSymbolLoc(RefKind);

View File

@ -20,8 +20,8 @@ class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter {
public: public:
AMDGPUELFObjectWriter(bool Is64Bit); AMDGPUELFObjectWriter(bool Is64Bit);
protected: protected:
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override { const MCFixup &Fixup, bool IsPCRel) const override {
return Fixup.getKind(); return Fixup.getKind();
} }

View File

@ -34,8 +34,8 @@ namespace {
~ARMELFObjectWriter() override; ~ARMELFObjectWriter() override;
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCSymbol &Sym, bool needsRelocateWithSymbol(const MCSymbol &Sym,
unsigned Type) const override; unsigned Type) const override;
@ -67,7 +67,7 @@ bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
// Need to examine the Fixup when determining whether to // Need to examine the Fixup when determining whether to
// emit the relocation as an explicit symbol or as a section relative // emit the relocation as an explicit symbol or as a section relative
// offset // offset
unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, unsigned ARMELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, const MCFixup &Fixup,
bool IsPCRel) const { bool IsPCRel) const {
return GetRelocTypeInner(Target, Fixup, IsPCRel); return GetRelocTypeInner(Target, Fixup, IsPCRel);

View File

@ -22,8 +22,8 @@ public:
~BPFELFObjectWriter() override; ~BPFELFObjectWriter() override;
protected: protected:
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; const MCFixup &Fixup, bool IsPCRel) const override;
}; };
} }
@ -33,7 +33,7 @@ BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
BPFELFObjectWriter::~BPFELFObjectWriter() {} BPFELFObjectWriter::~BPFELFObjectWriter() {}
unsigned BPFELFObjectWriter::GetRelocType(const MCValue &Target, unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, const MCFixup &Fixup,
bool IsPCRel) const { bool IsPCRel) const {
// determine the type of the relocation // determine the type of the relocation

View File

@ -28,8 +28,8 @@ private:
public: public:
HexagonELFObjectWriter(uint8_t OSABI, StringRef C); HexagonELFObjectWriter(uint8_t OSABI, StringRef C);
unsigned GetRelocType(MCValue const &Target, MCFixup const &Fixup, unsigned getRelocType(MCContext &Ctx, MCValue const &Target,
bool IsPCRel) const override; MCFixup const &Fixup, bool IsPCRel) const override;
}; };
} }
@ -38,7 +38,8 @@ HexagonELFObjectWriter::HexagonELFObjectWriter(uint8_t OSABI, StringRef C)
/*HasRelocationAddend*/ true), /*HasRelocationAddend*/ true),
CPU(C) {} CPU(C) {}
unsigned HexagonELFObjectWriter::GetRelocType(MCValue const & /*Target*/, unsigned HexagonELFObjectWriter::getRelocType(MCContext &Ctx,
MCValue const & /*Target*/,
MCFixup const &Fixup, MCFixup const &Fixup,
bool IsPCRel) const { bool IsPCRel) const {
switch ((unsigned)Fixup.getKind()) { switch ((unsigned)Fixup.getKind()) {

View File

@ -44,8 +44,8 @@ struct MipsRelocationEntry {
~MipsELFObjectWriter() override; ~MipsELFObjectWriter() override;
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCSymbol &Sym, bool needsRelocateWithSymbol(const MCSymbol &Sym,
unsigned Type) const override; unsigned Type) const override;
virtual void sortRelocs(const MCAssembler &Asm, virtual void sortRelocs(const MCAssembler &Asm,
@ -61,7 +61,8 @@ MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
MipsELFObjectWriter::~MipsELFObjectWriter() {} MipsELFObjectWriter::~MipsELFObjectWriter() {}
unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target, unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
const MCValue &Target,
const MCFixup &Fixup, const MCFixup &Fixup,
bool IsPCRel) const { bool IsPCRel) const {
// Determine the type of the relocation. // Determine the type of the relocation.

View File

@ -25,8 +25,8 @@ namespace {
PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI); PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
protected: protected:
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCSymbol &Sym, bool needsRelocateWithSymbol(const MCSymbol &Sym,
unsigned Type) const override; unsigned Type) const override;
@ -66,7 +66,7 @@ static MCSymbolRefExpr::VariantKind getAccessVariant(const MCValue &Target,
llvm_unreachable("unknown PPCMCExpr kind"); llvm_unreachable("unknown PPCMCExpr kind");
} }
unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target, unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, const MCFixup &Fixup,
bool IsPCRel) const { bool IsPCRel) const {
MCSymbolRefExpr::VariantKind Modifier = getAccessVariant(Target, Fixup); MCSymbolRefExpr::VariantKind Modifier = getAccessVariant(Target, Fixup);

View File

@ -29,8 +29,8 @@ namespace {
~SparcELFObjectWriter() override {} ~SparcELFObjectWriter() override {}
protected: protected:
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCSymbol &Sym, bool needsRelocateWithSymbol(const MCSymbol &Sym,
unsigned Type) const override; 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, const MCFixup &Fixup,
bool IsPCRel) const { bool IsPCRel) const {

View File

@ -24,8 +24,8 @@ public:
protected: protected:
// Override MCELFObjectTargetWriter. // Override MCELFObjectTargetWriter.
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; const MCFixup &Fixup, bool IsPCRel) const override;
}; };
} // end anonymous namespace } // end anonymous namespace
@ -106,7 +106,8 @@ static unsigned getPLTReloc(unsigned Kind) {
llvm_unreachable("Unsupported absolute address"); llvm_unreachable("Unsupported absolute address");
} }
unsigned SystemZObjectWriter::GetRelocType(const MCValue &Target, unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx,
const MCValue &Target,
const MCFixup &Fixup, const MCFixup &Fixup,
bool IsPCRel) const { bool IsPCRel) const {
MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant(); MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();

View File

@ -9,6 +9,7 @@
#include "MCTargetDesc/X86FixupKinds.h" #include "MCTargetDesc/X86FixupKinds.h"
#include "MCTargetDesc/X86MCTargetDesc.h" #include "MCTargetDesc/X86MCTargetDesc.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCELFObjectWriter.h" #include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCValue.h" #include "llvm/MC/MCValue.h"
@ -25,8 +26,8 @@ namespace {
~X86ELFObjectWriter() override; ~X86ELFObjectWriter() override;
protected: protected:
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
bool IsPCRel) const override; 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) { X86_64RelType Type, bool IsPCRel) {
switch (Modifier) { switch (Modifier) {
default: default:
@ -147,19 +155,19 @@ static unsigned getRelocType64(MCSymbolRefExpr::VariantKind Modifier,
llvm_unreachable("Unimplemented"); llvm_unreachable("Unimplemented");
} }
case MCSymbolRefExpr::VK_TLSGD: case MCSymbolRefExpr::VK_TLSGD:
assert(Type == RT64_32); checkIs32(Ctx, Loc, Type);
return ELF::R_X86_64_TLSGD; return ELF::R_X86_64_TLSGD;
case MCSymbolRefExpr::VK_GOTTPOFF: case MCSymbolRefExpr::VK_GOTTPOFF:
assert(Type == RT64_32); checkIs32(Ctx, Loc, Type);
return ELF::R_X86_64_GOTTPOFF; return ELF::R_X86_64_GOTTPOFF;
case MCSymbolRefExpr::VK_TLSLD: case MCSymbolRefExpr::VK_TLSLD:
assert(Type == RT64_32); checkIs32(Ctx, Loc, Type);
return ELF::R_X86_64_TLSLD; return ELF::R_X86_64_TLSLD;
case MCSymbolRefExpr::VK_PLT: case MCSymbolRefExpr::VK_PLT:
assert(Type == RT64_32); checkIs32(Ctx, Loc, Type);
return ELF::R_X86_64_PLT32; return ELF::R_X86_64_PLT32;
case MCSymbolRefExpr::VK_GOTPCREL: case MCSymbolRefExpr::VK_GOTPCREL:
assert(Type == RT64_32); checkIs32(Ctx, Loc, Type);
return ELF::R_X86_64_GOTPCREL; 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, const MCFixup &Fixup,
bool IsPCRel) const { bool IsPCRel) const {
MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant(); MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
X86_64RelType Type = getType64(Fixup.getKind(), Modifier, IsPCRel); X86_64RelType Type = getType64(Fixup.getKind(), Modifier, IsPCRel);
if (getEMachine() == ELF::EM_X86_64) 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) && assert((getEMachine() == ELF::EM_386 || getEMachine() == ELF::EM_IAMCU) &&
"Unsupported ELF machine type."); "Unsupported ELF machine type.");

View File

@ -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