[llvm] r258216 - [MC, COFF] Add .reloc support for WinCOFF

David Majnemer via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 19 15:05:28 PST 2016


Author: majnemer
Date: Tue Jan 19 17:05:27 2016
New Revision: 258216

URL: http://llvm.org/viewvc/llvm-project?rev=258216&view=rev
Log:
[MC, COFF] Add .reloc support for WinCOFF

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

Added:
    llvm/trunk/test/MC/Mips/reloc-directive-negative.s
    llvm/trunk/test/MC/X86/reloc-directive.s
Modified:
    llvm/trunk/include/llvm/MC/MCAsmBackend.h
    llvm/trunk/lib/MC/MCAsmBackend.cpp
    llvm/trunk/lib/MC/MCObjectStreamer.cpp
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp
    llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
    llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
    llvm/trunk/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
    llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp

Modified: llvm/trunk/include/llvm/MC/MCAsmBackend.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCAsmBackend.h?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCAsmBackend.h (original)
+++ llvm/trunk/include/llvm/MC/MCAsmBackend.h Tue Jan 19 17:05:27 2016
@@ -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;

Modified: llvm/trunk/lib/MC/MCAsmBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmBackend.cpp?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAsmBackend.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmBackend.cpp Tue Jan 19 17:05:27 2016
@@ -16,8 +16,8 @@ MCAsmBackend::MCAsmBackend() : HasDataIn
 
 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 {

Modified: llvm/trunk/lib/MC/MCObjectStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCObjectStreamer.cpp?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCObjectStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCObjectStreamer.cpp Tue Jan 19 17:05:27 2016
@@ -420,13 +420,18 @@ bool MCObjectStreamer::EmitRelocDirectiv
   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());

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Tue Jan 19 17:05:27 2016
@@ -2485,6 +2485,9 @@ bool AsmParser::parseDirectiveReloc(SMLo
   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();

Modified: llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp (original)
+++ llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp Tue Jan 19 17:05:27 2016
@@ -248,16 +248,11 @@ void MipsAsmBackend::applyFixup(const MC
   }
 }
 
-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::

Modified: llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h (original)
+++ llvm/trunk/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h Tue Jan 19 17:05:27 2016
@@ -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 {

Modified: llvm/trunk/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp (original)
+++ llvm/trunk/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp Tue Jan 19 17:05:27 2016
@@ -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_32AsmBacke
   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_64AsmBacke
     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());

Modified: llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp?rev=258216&r1=258215&r2=258216&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp (original)
+++ llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp Tue Jan 19 17:05:27 2016
@@ -58,6 +58,8 @@ unsigned X86WinCOFFObjectWriter::getRelo
     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::getRelo
     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;

Added: llvm/trunk/test/MC/Mips/reloc-directive-negative.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/Mips/reloc-directive-negative.s?rev=258216&view=auto
==============================================================================
--- llvm/trunk/test/MC/Mips/reloc-directive-negative.s (added)
+++ llvm/trunk/test/MC/Mips/reloc-directive-negative.s Tue Jan 19 17:05:27 2016
@@ -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

Added: llvm/trunk/test/MC/X86/reloc-directive.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/X86/reloc-directive.s?rev=258216&view=auto
==============================================================================
--- llvm/trunk/test/MC/X86/reloc-directive.s (added)
+++ llvm/trunk/test/MC/X86/reloc-directive.s Tue Jan 19 17:05:27 2016
@@ -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 at secrel32 # ASM: .reloc 12, dir32, foo at SECREL32
+	.reloc 16, dir32,   foo at imgrel   # ASM: .reloc 16, dir32, foo at 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




More information about the llvm-commits mailing list