LLVM  5.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/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/MCValue.h"
19 
20 using namespace llvm;
21 
22 namespace {
23 
24 class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
25 public:
26  X86WinCOFFObjectWriter(bool Is64Bit);
27  ~X86WinCOFFObjectWriter() override = default;
28 
29  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
30  const MCFixup &Fixup, bool IsCrossSection,
31  const MCAsmBackend &MAB) const override;
32 };
33 
34 } // end anonymous namespace
35 
36 X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
38  : COFF::IMAGE_FILE_MACHINE_I386) {}
39 
41  const MCValue &Target,
42  const MCFixup &Fixup,
43  bool IsCrossSection,
44  const MCAsmBackend &MAB) const {
45  unsigned FixupKind = Fixup.getKind();
46  if (IsCrossSection) {
47  if (FixupKind != FK_Data_4 && FixupKind != llvm::X86::reloc_signed_4byte) {
48  Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
50  }
51  FixupKind = FK_PCRel_4;
52  }
53 
54  MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
55  MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
56 
57  if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
58  switch (FixupKind) {
59  case FK_PCRel_4:
65  case FK_Data_4:
68  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
70  if (Modifier == MCSymbolRefExpr::VK_SECREL)
73  case FK_Data_8:
75  case FK_SecRel_2:
77  case FK_SecRel_4:
79  default:
80  llvm_unreachable("unsupported relocation type");
81  }
82  } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
83  switch (FixupKind) {
84  case FK_PCRel_4:
88  case FK_Data_4:
91  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
93  if (Modifier == MCSymbolRefExpr::VK_SECREL)
96  case FK_SecRel_2:
98  case FK_SecRel_4:
100  default:
101  llvm_unreachable("unsupported relocation type");
102  }
103  } else
104  llvm_unreachable("Unsupported COFF machine type.");
105 }
106 
108  bool Is64Bit) {
109  MCWinCOFFObjectTargetWriter *MOTW = new X86WinCOFFObjectWriter(Is64Bit);
110  return createWinCOFFObjectWriter(MOTW, OS);
111 }
SMLoc getLoc() const
Definition: MCFixup.h:112
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
Context object for machine code objects.
Definition: MCContext.h:59
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
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:608
MCFixupKind getKind() const
Definition: MCFixup.h:93
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.
llvm_unreachable("Unknown RegBank!")
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:320
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
MCObjectWriter * createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.