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:
73 case FK_Data_4:
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
117std::unique_ptr<MCObjectTargetWriter>
119 return std::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
120}
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:1068
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_signed_4byte_relax
Definition: X86FixupKinds.h:26
@ reloc_branch_4byte_pcrel
Definition: X86FixupKinds.h:32
@ reloc_riprel_4byte_relax
Definition: X86FixupKinds.h:19
@ reloc_signed_4byte
Definition: X86FixupKinds.h:23
@ reloc_riprel_4byte_relax_rex
Definition: X86FixupKinds.h:21
@ reloc_riprel_4byte_movq_load
Definition: X86FixupKinds.h:18
@ reloc_riprel_4byte
Definition: X86FixupKinds.h:17
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