LLVM 22.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
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCExpr.h"
15#include "llvm/MC/MCFixup.h"
17#include "llvm/MC/MCValue.h"
20
21using namespace llvm;
22
23namespace {
24
25class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
26public:
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
37X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
40
41unsigned X86WinCOFFObjectWriter::getRelocType(MCContext &Ctx,
42 const MCValue &Target,
43 const MCFixup &Fixup,
44 bool IsCrossSection,
45 const MCAsmBackend &MAB) const {
46 const bool Is64Bit = getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64;
47 unsigned FixupKind = Fixup.getKind();
48 bool PCRel = Fixup.isPCRel();
49 if (IsCrossSection) {
50 // IMAGE_REL_AMD64_REL64 does not exist. We treat FK_Data_8 as FK_PCRel_4 so
51 // that .quad a-b can lower to IMAGE_REL_AMD64_REL32. This allows generic
52 // instrumentation to not bother with the COFF limitation. A negative value
53 // needs attention.
54 if (!PCRel &&
56 (FixupKind == FK_Data_8 && Is64Bit))) {
58 PCRel = true;
59 } else {
60 Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
62 }
63 }
64
65 auto Spec = Target.getSpecifier();
66 if (Is64Bit) {
67 switch (FixupKind) {
77 case FK_Data_4:
78 if (PCRel)
80 [[fallthrough]];
85 if (Spec == X86::S_COFF_SECREL)
88 case FK_Data_8:
90 case FK_SecRel_2:
92 case FK_SecRel_4:
94 default:
95 Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
97 }
98 } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
99 switch (FixupKind) {
103 case FK_Data_4:
104 if (PCRel)
106 [[fallthrough]];
111 if (Spec == X86::S_COFF_SECREL)
114 case FK_SecRel_2:
116 case FK_SecRel_4:
118 default:
119 Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
121 }
122 } else
123 llvm_unreachable("Unsupported COFF machine type.");
124}
125
126std::unique_ptr<MCObjectTargetWriter>
128 return std::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
129}
PowerPC TLS Dynamic Call Fixup
Generic interface to target specific assembler backends.
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
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:98
@ IMAGE_FILE_MACHINE_I386
Definition COFF.h:105
@ IMAGE_REL_AMD64_REL32
Definition COFF.h:365
@ IMAGE_REL_AMD64_ADDR64
Definition COFF.h:362
@ IMAGE_REL_AMD64_ADDR32NB
Definition COFF.h:364
@ IMAGE_REL_AMD64_ADDR32
Definition COFF.h:363
@ IMAGE_REL_AMD64_SECTION
Definition COFF.h:371
@ IMAGE_REL_AMD64_SECREL
Definition COFF.h:372
@ IMAGE_REL_I386_SECTION
Definition COFF.h:353
@ IMAGE_REL_I386_REL32
Definition COFF.h:357
@ IMAGE_REL_I386_SECREL
Definition COFF.h:354
@ IMAGE_REL_I386_DIR32
Definition COFF.h:350
@ IMAGE_REL_I386_DIR32NB
Definition COFF.h:351
@ reloc_riprel_4byte_movq_load_rex2
@ reloc_signed_4byte_relax
@ reloc_branch_4byte_pcrel
@ reloc_riprel_4byte_relax
@ reloc_riprel_4byte_relax_evex
@ reloc_riprel_4byte_relax_rex
@ reloc_riprel_4byte_movq_load
@ reloc_riprel_4byte_relax_rex2
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectTargetWriter > createX86WinCOFFObjectWriter(bool Is64Bit)
Construct an X86 Win COFF object writer.
static Lanai::Fixups FixupKind(const MCExpr *Expr)
@ FK_SecRel_2
A two-byte section relative fixup.
Definition MCFixup.h:40
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:37
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:36
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:41