LLVM  4.0.0
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/MC/MCExpr.h"
13 #include "llvm/MC/MCValue.h"
15 #include "llvm/Support/COFF.h"
17 
18 using namespace llvm;
19 
20 namespace llvm {
21  class MCObjectWriter;
22 }
23 
24 namespace {
25  class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
26  public:
27  X86WinCOFFObjectWriter(bool Is64Bit);
28  ~X86WinCOFFObjectWriter() override;
29 
30  unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
31  bool IsCrossSection,
32  const MCAsmBackend &MAB) const override;
33  };
34 }
35 
36 X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
38  : COFF::IMAGE_FILE_MACHINE_I386) {}
39 
40 X86WinCOFFObjectWriter::~X86WinCOFFObjectWriter() {}
41 
43  const MCFixup &Fixup,
44  bool IsCrossSection,
45  const MCAsmBackend &MAB) const {
46  unsigned FixupKind = IsCrossSection ? FK_PCRel_4 : Fixup.getKind();
47 
48  MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
49  MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
50 
51  if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
52  switch (FixupKind) {
53  case FK_PCRel_4:
59  case FK_Data_4:
62  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
64  if (Modifier == MCSymbolRefExpr::VK_SECREL)
67  case FK_Data_8:
69  case FK_SecRel_2:
71  case FK_SecRel_4:
73  default:
74  llvm_unreachable("unsupported relocation type");
75  }
76  } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
77  switch (FixupKind) {
78  case FK_PCRel_4:
82  case FK_Data_4:
85  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
87  if (Modifier == MCSymbolRefExpr::VK_SECREL)
90  case FK_SecRel_2:
92  case FK_SecRel_4:
94  default:
95  llvm_unreachable("unsupported relocation type");
96  }
97  } else
98  llvm_unreachable("Unsupported COFF machine type.");
99 }
100 
102  bool Is64Bit) {
103  MCWinCOFFObjectTargetWriter *MOTW = new X86WinCOFFObjectWriter(Is64Bit);
104  return createWinCOFFObjectWriter(MOTW, OS);
105 }
This represents an "assembler immediate".
Definition: MCValue.h:40
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
MCObjectWriter * createX86WinCOFFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit)
Construct an X86 Win COFF object writer.
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
A two-byte section relative fixup.
Definition: MCFixup.h:41
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:52
MCFixupKind getKind() const
Definition: MCFixup.h:93
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
PowerPC TLS Dynamic Call Fixup
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
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:333
VariantKind getKind() const
Definition: MCExpr.h:313
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
MCObjectWriter * createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.