LLVM  8.0.1
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:
67  case FK_Data_4:
70  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
72  if (Modifier == MCSymbolRefExpr::VK_SECREL)
75  case FK_Data_8:
77  case FK_SecRel_2:
79  case FK_SecRel_4:
81  default:
82  Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
84  }
85  } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
86  switch (FixupKind) {
87  case FK_PCRel_4:
91  case FK_Data_4:
94  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
96  if (Modifier == MCSymbolRefExpr::VK_SECREL)
99  case FK_SecRel_2:
101  case FK_SecRel_4:
103  default:
104  Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
106  }
107  } else
108  llvm_unreachable("Unsupported COFF machine type.");
109 }
110 
111 std::unique_ptr<MCObjectTargetWriter>
113  return llvm::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
114 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
VariantKind getKind() const
Definition: MCExpr.h:338
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:74
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:63
A two-byte section relative fixup.
Definition: MCFixup.h:41
std::unique_ptr< MCObjectTargetWriter > createX86WinCOFFObjectWriter(bool Is64Bit)
Construct an X86 Win COFF object writer.
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:612
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SMLoc getLoc() const
Definition: MCFixup.h:166
A four-byte pc relative fixup.
Definition: MCFixup.h:30
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:27
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
MCFixupKind getKind() const
Definition: MCFixup.h:123