LLVM  14.0.0git
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 
40 unsigned X86WinCOFFObjectWriter::getRelocType(MCContext &Ctx,
41  const MCValue &Target,
42  const MCFixup &Fixup,
43  bool IsCrossSection,
44  const MCAsmBackend &MAB) const {
45  const bool Is64Bit = getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64;
46  unsigned FixupKind = Fixup.getKind();
47  if (IsCrossSection) {
48  // IMAGE_REL_AMD64_REL64 does not exist. We treat FK_Data_8 as FK_PCRel_4 so
49  // that .quad a-b can lower to IMAGE_REL_AMD64_REL32. This allows generic
50  // instrumentation to not bother with the COFF limitation. A negative value
51  // needs attention.
53  (FixupKind == FK_Data_8 && Is64Bit)) {
55  } else {
56  Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
58  }
59  }
60 
61  MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
62  MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
63 
64  if (Is64Bit) {
65  switch (FixupKind) {
66  case FK_PCRel_4:
73  case FK_Data_4:
76  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
78  if (Modifier == MCSymbolRefExpr::VK_SECREL)
81  case FK_Data_8:
83  case FK_SecRel_2:
85  case FK_SecRel_4:
87  default:
88  Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
90  }
91  } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
92  switch (FixupKind) {
93  case FK_PCRel_4:
97  case FK_Data_4:
100  if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
102  if (Modifier == MCSymbolRefExpr::VK_SECREL)
105  case FK_SecRel_2:
107  case FK_SecRel_4:
109  default:
110  Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
112  }
113  } else
114  llvm_unreachable("Unsupported COFF machine type.");
115 }
116 
117 std::unique_ptr<MCObjectTargetWriter>
119  return std::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
120 }
llvm::COFF::IMAGE_REL_I386_SECTION
@ IMAGE_REL_I386_SECTION
Definition: COFF.h:338
llvm::COFF::IMAGE_REL_AMD64_ADDR32
@ IMAGE_REL_AMD64_ADDR32
Definition: COFF.h:348
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::FixupKind
static Lanai::Fixups FixupKind(const MCExpr *Expr)
Definition: LanaiMCCodeEmitter.cpp:90
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
ErrorHandling.h
llvm::COFF::IMAGE_FILE_MACHINE_AMD64
@ IMAGE_FILE_MACHINE_AMD64
Definition: COFF.h:98
COFF.h
llvm::COFF::IMAGE_REL_I386_DIR32NB
@ IMAGE_REL_I386_DIR32NB
Definition: COFF.h:336
llvm::COFF::IMAGE_REL_AMD64_REL32
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:350
llvm::createX86WinCOFFObjectWriter
std::unique_ptr< MCObjectTargetWriter > createX86WinCOFFObjectWriter(bool Is64Bit)
Construct an X86 Win COFF object writer.
Definition: X86WinCOFFObjectWriter.cpp:118
llvm::X86::reloc_riprel_4byte
@ reloc_riprel_4byte
Definition: X86FixupKinds.h:17
llvm::X86::reloc_signed_4byte_relax
@ reloc_signed_4byte_relax
Definition: X86FixupKinds.h:26
llvm::FK_Data_4
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
llvm::COFF::IMAGE_FILE_MACHINE_I386
@ IMAGE_FILE_MACHINE_I386
Definition: COFF.h:103
llvm::MCAsmBackend
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
llvm::FK_SecRel_4
@ FK_SecRel_4
A four-byte section relative fixup.
Definition: MCFixup.h:42
MCContext.h
llvm::FK_SecRel_2
@ FK_SecRel_2
A two-byte section relative fixup.
Definition: MCFixup.h:41
llvm::COFF::IMAGE_REL_I386_DIR32
@ IMAGE_REL_I386_DIR32
Definition: COFF.h:335
llvm::X86::reloc_signed_4byte
@ reloc_signed_4byte
Definition: X86FixupKinds.h:23
X86MCTargetDesc.h
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
llvm::COFF::IMAGE_REL_I386_REL32
@ IMAGE_REL_I386_REL32
Definition: COFF.h:342
llvm::COFF::IMAGE_REL_AMD64_ADDR64
@ IMAGE_REL_AMD64_ADDR64
Definition: COFF.h:347
llvm::MCContext::reportError
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:963
llvm::FK_PCRel_4
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:235
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
MCFixup.h
llvm::MCWinCOFFObjectTargetWriter
Definition: MCWinCOFFObjectWriter.h:23
llvm::COFF::IMAGE_REL_AMD64_SECREL
@ IMAGE_REL_AMD64_SECREL
Definition: COFF.h:357
llvm::X86::reloc_riprel_4byte_relax
@ reloc_riprel_4byte_relax
Definition: X86FixupKinds.h:19
MCObjectWriter.h
llvm::X86::reloc_riprel_4byte_movq_load
@ reloc_riprel_4byte_movq_load
Definition: X86FixupKinds.h:18
llvm::COFF::IMAGE_REL_AMD64_ADDR32NB
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:349
llvm::MCSymbolRefExpr::VK_COFF_IMGREL32
@ VK_COFF_IMGREL32
Definition: MCExpr.h:315
MCValue.h
llvm::COFF::IMAGE_REL_I386_SECREL
@ IMAGE_REL_I386_SECREL
Definition: COFF.h:339
llvm::FK_Data_8
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
llvm::COFF::IMAGE_REL_AMD64_SECTION
@ IMAGE_REL_AMD64_SECTION
Definition: COFF.h:356
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
MCWinCOFFObjectWriter.h
llvm::X86::reloc_riprel_4byte_relax_rex
@ reloc_riprel_4byte_relax_rex
Definition: X86FixupKinds.h:21
MCExpr.h
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::X86::reloc_branch_4byte_pcrel
@ reloc_branch_4byte_pcrel
Definition: X86FixupKinds.h:32
llvm::MCSymbolRefExpr::VK_SECREL
@ VK_SECREL
Definition: MCExpr.h:222
X86FixupKinds.h