[MC, COFF] Add .reloc support for WinCOFF

This adds rudimentary support for a few relocations that we will use for
the CodeView debug format.

llvm-svn: 258216
This commit is contained in:
David Majnemer 2016-01-19 23:05:27 +00:00
parent 4b919b2ab3
commit ce10842036
10 changed files with 84 additions and 23 deletions

View File

@ -11,6 +11,7 @@
#define LLVM_MC_MCASMBACKEND_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/MC/MCDirectives.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCFixup.h"
@ -68,9 +69,7 @@ public:
virtual unsigned getNumFixupKinds() const = 0;
/// Map a relocation name used in .reloc to a fixup kind.
/// Returns true and sets MappedKind if Name is successfully mapped.
/// Otherwise returns false and leaves MappedKind unchanged.
virtual bool getFixupKind(StringRef Name, MCFixupKind &MappedKind) const;
virtual Optional<MCFixupKind> getFixupKind(StringRef Name) const;
/// Get information on a fixup kind.
virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;

View File

@ -16,8 +16,8 @@ MCAsmBackend::MCAsmBackend() : HasDataInCodeSupport(false) {}
MCAsmBackend::~MCAsmBackend() {}
bool MCAsmBackend::getFixupKind(StringRef Name, MCFixupKind &MappedKind) const {
return false;
Optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const {
return None;
}
const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {

View File

@ -420,13 +420,18 @@ bool MCObjectStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name,
if (!Offset.evaluateAsAbsolute(OffsetValue))
llvm_unreachable("Offset is not absolute");
if (OffsetValue < 0)
llvm_unreachable("Offset is negative");
MCDataFragment *DF = getOrCreateDataFragment();
flushPendingLabels(DF, DF->getContents().size());
MCFixupKind Kind;
if (!Assembler->getBackend().getFixupKind(Name, Kind))
Optional<MCFixupKind> MaybeKind = Assembler->getBackend().getFixupKind(Name);
if (!MaybeKind.hasValue())
return true;
MCFixupKind Kind = *MaybeKind;
if (Expr == nullptr)
Expr =
MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());

View File

@ -2485,6 +2485,9 @@ bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
if (!Offset->evaluateAsAbsolute(OffsetValue))
return Error(OffsetLoc, "expression is not a constant value");
if (OffsetValue < 0)
return Error(OffsetLoc, "expression is negative");
if (Lexer.isNot(AsmToken::Comma))
return TokError("expected comma");
Lexer.Lex();

View File

@ -248,16 +248,11 @@ void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
}
}
bool MipsAsmBackend::getFixupKind(StringRef Name, MCFixupKind &MappedKind) const {
if (Name == "R_MIPS_NONE") {
MappedKind = (MCFixupKind)Mips::fixup_Mips_NONE;
return true;
}
if (Name == "R_MIPS_32") {
MappedKind = FK_Data_4;
return true;
}
return MCAsmBackend::getFixupKind(Name, MappedKind);
Optional<MCFixupKind> MipsAsmBackend::getFixupKind(StringRef Name) const {
return StringSwitch<Optional<MCFixupKind>>(Name)
.Case("R_MIPS_NONE", (MCFixupKind)Mips::fixup_Mips_NONE)
.Case("R_MIPS_32", FK_Data_4)
.Default(MCAsmBackend::getFixupKind(Name));
}
const MCFixupKindInfo &MipsAsmBackend::

View File

@ -41,7 +41,7 @@ public:
void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
uint64_t Value, bool IsPCRel) const override;
bool getFixupKind(StringRef Name, MCFixupKind &MappedKind) const override;
Optional<MCFixupKind> getFixupKind(StringRef Name) const override;
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
unsigned getNumFixupKinds() const override {

View File

@ -90,10 +90,11 @@ public:
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
const static MCFixupKindInfo Infos[X86::NumTargetFixupKinds] = {
{ "reloc_riprel_4byte", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel },
{ "reloc_riprel_4byte_movq_load", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel},
{ "reloc_riprel_4byte", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel, },
{ "reloc_riprel_4byte_movq_load", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel,},
{ "reloc_signed_4byte", 0, 4 * 8, 0},
{ "reloc_global_offset_table", 0, 4 * 8, 0}
{ "reloc_global_offset_table", 0, 4 * 8, 0},
{ "reloc_global_offset_table8", 0, 8 * 8, 0},
};
if (Kind < FirstTargetFixupKind)
@ -423,6 +424,14 @@ public:
, Is64Bit(is64Bit) {
}
Optional<MCFixupKind> getFixupKind(StringRef Name) const override {
return StringSwitch<Optional<MCFixupKind>>(Name)
.Case("dir32", FK_Data_4)
.Case("secrel32", FK_SecRel_4)
.Case("secidx", FK_SecRel_2)
.Default(MCAsmBackend::getFixupKind(Name));
}
MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
return createX86WinCOFFObjectWriter(OS, Is64Bit);
}
@ -821,7 +830,7 @@ MCAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
if (TheTriple.isOSBinFormatMachO())
return new DarwinX86_32AsmBackend(T, MRI, CPU);
if (TheTriple.isOSWindows() && !TheTriple.isOSBinFormatELF())
if (TheTriple.isOSWindows() && TheTriple.isOSBinFormatCOFF())
return new WindowsX86AsmBackend(T, false, CPU);
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
@ -844,7 +853,7 @@ MCAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
return new DarwinX86_64AsmBackend(T, MRI, CPU, CS);
}
if (TheTriple.isOSWindows() && !TheTriple.isOSBinFormatELF())
if (TheTriple.isOSWindows() && TheTriple.isOSBinFormatCOFF())
return new WindowsX86AsmBackend(T, true, CPU);
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());

View File

@ -58,6 +58,8 @@ unsigned X86WinCOFFObjectWriter::getRelocType(const MCValue &Target,
case X86::reloc_signed_4byte:
if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
return COFF::IMAGE_REL_AMD64_ADDR32NB;
if (Modifier == MCSymbolRefExpr::VK_SECREL)
return COFF::IMAGE_REL_AMD64_SECREL;
return COFF::IMAGE_REL_AMD64_ADDR32;
case FK_Data_8:
return COFF::IMAGE_REL_AMD64_ADDR64;
@ -78,6 +80,8 @@ unsigned X86WinCOFFObjectWriter::getRelocType(const MCValue &Target,
case X86::reloc_signed_4byte:
if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
return COFF::IMAGE_REL_I386_DIR32NB;
if (Modifier == MCSymbolRefExpr::VK_SECREL)
return COFF::IMAGE_REL_AMD64_SECREL;
return COFF::IMAGE_REL_I386_DIR32;
case FK_SecRel_2:
return COFF::IMAGE_REL_I386_SECTION;

View File

@ -0,0 +1,6 @@
# RUN: not llvm-mc -triple mips-unknown-linux < %s -show-encoding -target-abi=o32 \
# RUN: 2>&1 | FileCheck %s
.text
foo:
.reloc -1, R_MIPS_32, .text # CHECK: :[[@LINE]]:9: error: expression is negative
nop

View File

@ -0,0 +1,40 @@
# RUN: llvm-mc -triple i686-pc-win32 < %s -show-encoding \
# RUN: | FileCheck -check-prefix=ASM %s
# RUN: llvm-mc -triple i686-pc-win32 < %s -show-encoding \
# RUN: -filetype=obj | llvm-readobj -sections -section-data -r | \
# RUN: FileCheck -check-prefix=OBJ-32 %s
# RUN: llvm-mc -triple x86_64-pc-win32 < %s -show-encoding \
# RUN: -filetype=obj | llvm-readobj -sections -section-data -r | \
# RUN: FileCheck -check-prefix=OBJ-64 %s
.text
foo:
.long 0
.long 0
.long 0
.long 0
.long 0
.reloc 4, dir32, foo # ASM: .reloc 4, dir32, foo
.reloc 0, secrel32, foo+4 # ASM: .reloc 0, secrel32, foo+4
.reloc 8, secidx, foo+8 # ASM: .reloc 8, secidx, foo+8
.reloc 12, dir32, foo@secrel32 # ASM: .reloc 12, dir32, foo@SECREL32
.reloc 16, dir32, foo@imgrel # ASM: .reloc 16, dir32, foo@IMGREL
# OBJ-32-LABEL: Name: .text
# OBJ-32: 0000: 04000000 00000000 08000000
# OBJ-32-LABEL: }
# OBJ-32-LABEL: Relocations [
# OBJ-32: 0x4 IMAGE_REL_I386_DIR32 foo
# OBJ-32: 0x0 IMAGE_REL_I386_SECREL foo
# OBJ-32: 0x8 IMAGE_REL_I386_SECTION foo
# OBJ-32: 0xC IMAGE_REL_I386_SECREL foo
# OBJ-32: 0x10 IMAGE_REL_I386_DIR32NB foo
# OBJ-64-LABEL: Name: .text
# OBJ-64: 0000: 04000000 00000000 08000000
# OBJ-64-LABEL: }
# OBJ-64-LABEL: Relocations [
# OBJ-64: 0x4 IMAGE_REL_AMD64_ADDR32 foo
# OBJ-64: 0x0 IMAGE_REL_AMD64_SECREL foo
# OBJ-64: 0x8 IMAGE_REL_AMD64_SECTION foo
# OBJ-64: 0xC IMAGE_REL_AMD64_SECREL foo
# OBJ-64: 0x10 IMAGE_REL_AMD64_ADDR32NB foo