LLVM  6.0.0svn
X86WinCOFFObjectWriter.cpp
Go to the documentation of this file.
1 //===-- X86WinCOFFObjectWriter.cpp - X86 Win COFF Writer ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include "llvm/BinaryFormat/COFF.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCValue.h"
20 
21 using namespace llvm;
22 
23 namespace {
24 
25 class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
26 public:
27  X86WinCOFFObjectWriter(bool Is64Bit);
28  ~X86WinCOFFObjectWriter() override = default;
29 
30  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
31  const MCFixup &Fixup, bool IsCrossSection,
32  const MCAsmBackend &MAB) const override;
33 };
34 
35 } // end anonymous namespace
36 
37 X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
39  : COFF::IMAGE_FILE_MACHINE_I386) {}
40 
42  const MCValue &Target,
43  const MCFixup &Fixup,
44  bool IsCrossSection,
45  const MCAsmBackend &MAB) const {
46  unsigned FixupKind = Fixup.getKind();
47  if (IsCrossSection) {
48  if (FixupKind != FK_Data_4 && FixupKind != llvm::X86::reloc_signed_4byte) {
49  Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
51  }
52  FixupKind = FK_PCRel_4;
53  }
54 
55  MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
57 
58  if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
59  switch (FixupKind) {
60  case FK_PCRel_4:
66  case FK_Data_4:
69  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
71  if (Modifier == MCSymbolRefExpr::VK_SECREL)
74  case FK_Data_8:
76  case FK_SecRel_2:
78  case FK_SecRel_4:
80  default:
81  llvm_unreachable("unsupported relocation type");
82  }
83  } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
84  switch (FixupKind) {
85  case FK_PCRel_4:
89  case FK_Data_4:
92  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
94  if (Modifier == MCSymbolRefExpr::VK_SECREL)
97  case FK_SecRel_2:
99  case FK_SecRel_4:
101  default:
102  llvm_unreachable("unsupported relocation type");
103  }
104  } else
105  llvm_unreachable("Unsupported COFF machine type.");
106 }
107 
108 std::unique_ptr<MCObjectWriter>
110  auto MOTW = llvm::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
111  return createWinCOFFObjectWriter(std::move(MOTW), OS);
112 }
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
VariantKind getKind() const
Definition: MCExpr.h:320
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:53
static Lanai::Fixups FixupKind(const MCExpr *Expr)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
A four-byte section relative fixup.
Definition: MCFixup.h:42
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:59
A two-byte section relative fixup.
Definition: MCFixup.h:41
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:570
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
SMLoc getLoc() const
Definition: MCFixup.h:112
A four-byte pc relative fixup.
Definition: MCFixup.h:30
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:27
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:337
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
std::unique_ptr< MCObjectWriter > createX86WinCOFFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit)
Construct an X86 Win COFF object writer.
MCFixupKind getKind() const
Definition: MCFixup.h:93