LLVM 20.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
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCFixup.h"
16#include "llvm/MC/MCValue.h"
19
20using namespace llvm;
21
22namespace {
23
24class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
25public:
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
36X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
39
40unsigned 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.
52 if (FixupKind == FK_Data_4 || FixupKind == llvm::X86::reloc_signed_4byte ||
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:
76 case FK_Data_4:
81 if (Modifier == MCSymbolRefExpr::VK_SECREL)
84 case FK_Data_8:
86 case FK_SecRel_2:
88 case FK_SecRel_4:
90 default:
91 Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
93 }
94 } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
95 switch (FixupKind) {
96 case FK_PCRel_4:
100 case FK_Data_4:
103 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
105 if (Modifier == MCSymbolRefExpr::VK_SECREL)
108 case FK_SecRel_2:
110 case FK_SecRel_4:
112 default:
113 Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
115 }
116 } else
117 llvm_unreachable("Unsupported COFF machine type.");
118}
119
120std::unique_ptr<MCObjectTargetWriter>
122 return std::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
123}
PowerPC TLS Dynamic Call Fixup
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
Context object for machine code objects.
Definition: MCContext.h:83
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1072
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
This represents an "assembler immediate".
Definition: MCValue.h:36
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsCrossSection, const MCAsmBackend &MAB) const =0
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ IMAGE_FILE_MACHINE_AMD64
Definition: COFF.h:97
@ IMAGE_FILE_MACHINE_I386
Definition: COFF.h:104
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:364
@ IMAGE_REL_AMD64_ADDR64
Definition: COFF.h:361
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:363
@ IMAGE_REL_AMD64_ADDR32
Definition: COFF.h:362
@ IMAGE_REL_AMD64_SECTION
Definition: COFF.h:370
@ IMAGE_REL_AMD64_SECREL
Definition: COFF.h:371
@ IMAGE_REL_I386_SECTION
Definition: COFF.h:352
@ IMAGE_REL_I386_REL32
Definition: COFF.h:356
@ IMAGE_REL_I386_SECREL
Definition: COFF.h:353
@ IMAGE_REL_I386_DIR32
Definition: COFF.h:349
@ IMAGE_REL_I386_DIR32NB
Definition: COFF.h:350
@ reloc_riprel_4byte_movq_load_rex2
Definition: X86FixupKinds.h:19
@ reloc_signed_4byte_relax
Definition: X86FixupKinds.h:33
@ reloc_branch_4byte_pcrel
Definition: X86FixupKinds.h:39
@ reloc_riprel_4byte_relax
Definition: X86FixupKinds.h:21
@ reloc_riprel_4byte_relax_evex
Definition: X86FixupKinds.h:27
@ reloc_signed_4byte
Definition: X86FixupKinds.h:30
@ reloc_riprel_4byte_relax_rex
Definition: X86FixupKinds.h:23
@ reloc_riprel_4byte_movq_load
Definition: X86FixupKinds.h:18
@ reloc_riprel_4byte
Definition: X86FixupKinds.h:17
@ reloc_riprel_4byte_relax_rex2
Definition: X86FixupKinds.h:25
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectTargetWriter > createX86WinCOFFObjectWriter(bool Is64Bit)
Construct an X86 Win COFF object writer.
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
@ FK_SecRel_2
A two-byte section relative fixup.
Definition: MCFixup.h:41
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_SecRel_4
A four-byte section relative fixup.
Definition: MCFixup.h:42