From ce10842036320b5cc1b4f7057662fb3ef2d1ea10 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 19 Jan 2016 23:05:27 +0000 Subject: [PATCH] [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 --- llvm/include/llvm/MC/MCAsmBackend.h | 5 +-- llvm/lib/MC/MCAsmBackend.cpp | 4 +- llvm/lib/MC/MCObjectStreamer.cpp | 9 ++++- llvm/lib/MC/MCParser/AsmParser.cpp | 3 ++ .../Mips/MCTargetDesc/MipsAsmBackend.cpp | 15 +++---- .../Target/Mips/MCTargetDesc/MipsAsmBackend.h | 2 +- .../Target/X86/MCTargetDesc/X86AsmBackend.cpp | 19 ++++++--- .../MCTargetDesc/X86WinCOFFObjectWriter.cpp | 4 ++ llvm/test/MC/Mips/reloc-directive-negative.s | 6 +++ llvm/test/MC/X86/reloc-directive.s | 40 +++++++++++++++++++ 10 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 llvm/test/MC/Mips/reloc-directive-negative.s create mode 100644 llvm/test/MC/X86/reloc-directive.s diff --git a/llvm/include/llvm/MC/MCAsmBackend.h b/llvm/include/llvm/MC/MCAsmBackend.h index 51312ff80447..f815b24c856f 100644 --- a/llvm/include/llvm/MC/MCAsmBackend.h +++ b/llvm/include/llvm/MC/MCAsmBackend.h @@ -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 getFixupKind(StringRef Name) const; /// Get information on a fixup kind. virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const; diff --git a/llvm/lib/MC/MCAsmBackend.cpp b/llvm/lib/MC/MCAsmBackend.cpp index fcf139b72537..281bba27a0e8 100644 --- a/llvm/lib/MC/MCAsmBackend.cpp +++ b/llvm/lib/MC/MCAsmBackend.cpp @@ -16,8 +16,8 @@ MCAsmBackend::MCAsmBackend() : HasDataInCodeSupport(false) {} MCAsmBackend::~MCAsmBackend() {} -bool MCAsmBackend::getFixupKind(StringRef Name, MCFixupKind &MappedKind) const { - return false; +Optional MCAsmBackend::getFixupKind(StringRef Name) const { + return None; } const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp index 8ee247869673..19ce9113f275 100644 --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -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 MaybeKind = Assembler->getBackend().getFixupKind(Name); + if (!MaybeKind.hasValue()) return true; + MCFixupKind Kind = *MaybeKind; + if (Expr == nullptr) Expr = MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext()); diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index 646cbb43cae8..939b54dd3c81 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -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(); diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp index e4865e2455ee..2681e47e8062 100644 --- a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp +++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp @@ -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 MipsAsmBackend::getFixupKind(StringRef Name) const { + return StringSwitch>(Name) + .Case("R_MIPS_NONE", (MCFixupKind)Mips::fixup_Mips_NONE) + .Case("R_MIPS_32", FK_Data_4) + .Default(MCAsmBackend::getFixupKind(Name)); } const MCFixupKindInfo &MipsAsmBackend:: diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h index 1c9af9227ffe..3b65a4b0edde 100644 --- a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h +++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h @@ -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 getFixupKind(StringRef Name) const override; const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override; unsigned getNumFixupKinds() const override { diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp index 133bd0e1772a..19cf1fe39b35 100644 --- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp +++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp @@ -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 getFixupKind(StringRef Name) const override { + return StringSwitch>(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()); diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp index bd1bc9943b6d..cd0ab73060ad 100644 --- a/llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp +++ b/llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp @@ -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; diff --git a/llvm/test/MC/Mips/reloc-directive-negative.s b/llvm/test/MC/Mips/reloc-directive-negative.s new file mode 100644 index 000000000000..41dd39a17f62 --- /dev/null +++ b/llvm/test/MC/Mips/reloc-directive-negative.s @@ -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 diff --git a/llvm/test/MC/X86/reloc-directive.s b/llvm/test/MC/X86/reloc-directive.s new file mode 100644 index 000000000000..0cc96b3d7419 --- /dev/null +++ b/llvm/test/MC/X86/reloc-directive.s @@ -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