LLVM  10.0.0svn
X86WinCOFFObjectWriter.cpp
Go to the documentation of this file.
1 //===-- X86WinCOFFObjectWriter.cpp - X86 Win COFF Writer ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "llvm/BinaryFormat/COFF.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCFixup.h"
15 #include "llvm/MC/MCObjectWriter.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() ?
56 
57  if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
58  switch (FixupKind) {
59  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  Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
83  }
84  } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
85  switch (FixupKind) {
86  case FK_PCRel_4:
90  case FK_Data_4:
93  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
95  if (Modifier == MCSymbolRefExpr::VK_SECREL)
98  case FK_SecRel_2:
100  case FK_SecRel_4:
102  default:
103  Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
105  }
106  } else
107  llvm_unreachable("Unsupported COFF machine type.");
108 }
109 
110 std::unique_ptr<MCObjectTargetWriter>
112  return std::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
113 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
VariantKind getKind() const
Definition: MCExpr.h:346
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:52
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:77
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:43
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:65
A two-byte section relative fixup.
Definition: MCFixup.h:42
std::unique_ptr< MCObjectTargetWriter > createX86WinCOFFObjectWriter(bool Is64Bit)
Construct an X86 Win COFF object writer.
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:686
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SMLoc getLoc() const
Definition: MCFixup.h:195
A four-byte pc relative fixup.
Definition: MCFixup.h:31
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:27
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
MCFixupKind getKind() const
Definition: MCFixup.h:126