LLVM 22.0.0git
PPCXCOFFObjectWriter.cpp
Go to the documentation of this file.
1//===-- PPCXCOFFObjectWriter.cpp - PowerPC XCOFF Writer -------------------===//
2//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
12#include "PPCMCAsmInfo.h"
14#include "llvm/MC/MCFixup.h"
15#include "llvm/MC/MCValue.h"
17
18using namespace llvm;
19
20namespace {
21class PPCXCOFFObjectWriter : public MCXCOFFObjectTargetWriter {
22 static constexpr uint8_t SignBitMask = 0x80;
23
24public:
25 PPCXCOFFObjectWriter(bool Is64Bit);
26
27 std::pair<uint8_t, uint8_t>
28 getRelocTypeAndSignSize(const MCValue &Target, const MCFixup &Fixup,
29 bool IsPCRel) const override;
30};
31} // end anonymous namespace
32
33PPCXCOFFObjectWriter::PPCXCOFFObjectWriter(bool Is64Bit)
34 : MCXCOFFObjectTargetWriter(Is64Bit) {}
35
36std::unique_ptr<MCObjectTargetWriter>
38 return std::make_unique<PPCXCOFFObjectWriter>(Is64Bit);
39}
40
41std::pair<uint8_t, uint8_t> PPCXCOFFObjectWriter::getRelocTypeAndSignSize(
42 const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const {
43 const auto Specifier = Target.getSpecifier();
44 // People from AIX OS team says AIX link editor does not care about
45 // the sign bit in the relocation entry "most" of the time.
46 // The system assembler seems to set the sign bit on relocation entry
47 // based on similar property of IsPCRel. So we will do the same here.
48 // TODO: More investigation on how assembler decides to set the sign
49 // bit, and we might want to match that.
50 const uint8_t EncodedSignednessIndicator = IsPCRel ? SignBitMask : 0u;
51
52 // The magic number we use in SignAndSize has a strong relationship with
53 // the corresponding MCFixupKind. In most cases, it's the MCFixupKind
54 // number - 1, because SignAndSize encodes the bit length being
55 // relocated minus 1.
56 switch ((unsigned)Fixup.getKind()) {
57 default:
58 report_fatal_error("Unimplemented fixup kind.");
62 const uint8_t SignAndSizeForHalf16 = EncodedSignednessIndicator | 15;
63 switch (Specifier) {
64 default:
65 report_fatal_error("Unsupported modifier for half16 fixup.");
66 case PPC::S_None:
67 return {XCOFF::RelocationType::R_TOC, SignAndSizeForHalf16};
68 case PPC::S_U:
69 return {XCOFF::RelocationType::R_TOCU, SignAndSizeForHalf16};
70 case PPC::S_L:
71 return {XCOFF::RelocationType::R_TOCL, SignAndSizeForHalf16};
73 return {XCOFF::RelocationType::R_TLS_LE, SignAndSizeForHalf16};
75 return {XCOFF::RelocationType::R_TLS_LD, SignAndSizeForHalf16};
76 }
77 } break;
80 if (IsPCRel)
81 report_fatal_error("Invalid PC-relative relocation.");
82 switch (Specifier) {
83 default:
84 llvm_unreachable("Unsupported Modifier");
85 case PPC::S_None:
86 return {XCOFF::RelocationType::R_TOC, 15};
87 case PPC::S_L:
88 return {XCOFF::RelocationType::R_TOCL, 15};
90 return {XCOFF::RelocationType::R_TLS_LE, 15};
92 return {XCOFF::RelocationType::R_TLS_LD, 15};
93 }
94 } break;
96 // Branches are 4 byte aligned, so the 24 bits we encode in
97 // the instruction actually represents a 26 bit offset.
98 return {XCOFF::RelocationType::R_RBR, EncodedSignednessIndicator | 25};
100 return {XCOFF::RelocationType::R_RBA, EncodedSignednessIndicator | 25};
101 case FK_Data_4:
102 case FK_Data_8:
103 const uint8_t SignAndSizeForFKData =
104 EncodedSignednessIndicator |
105 ((unsigned)Fixup.getKind() == FK_Data_4 ? 31 : 63);
106 switch (Specifier) {
107 default:
108 report_fatal_error("Unsupported modifier");
109 case PPC::S_AIX_TLSGD:
110 return {XCOFF::RelocationType::R_TLS, SignAndSizeForFKData};
112 return {XCOFF::RelocationType::R_TLSM, SignAndSizeForFKData};
113 case PPC::S_AIX_TLSIE:
114 return {XCOFF::RelocationType::R_TLS_IE, SignAndSizeForFKData};
115 case PPC::S_AIX_TLSLE:
116 return {XCOFF::RelocationType::R_TLS_LE, SignAndSizeForFKData};
117 case PPC::S_AIX_TLSLD:
118 return {XCOFF::RelocationType::R_TLS_LD, SignAndSizeForFKData};
119 case PPC::S_AIX_TLSML:
120 return {XCOFF::RelocationType::R_TLSML, SignAndSizeForFKData};
121 case PPC::S_None:
122 return {XCOFF::RelocationType::R_POS, SignAndSizeForFKData};
123 }
124 }
125}
PowerPC TLS Dynamic Call Fixup
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.
@ fixup_ppc_half16
A 16-bit fixup corresponding to lo16(_foo) or ha16(_foo) for instrs like 'li' or 'addis'.
@ fixup_ppc_half16dq
A 16-bit fixup corresponding to lo16(_foo) with implied 3 zero bits for instrs like 'lxv'.
@ fixup_ppc_half16ds
A 14-bit fixup corresponding to lo16(_foo) with implied 2 zero bits for instrs like 'std'.
@ fixup_ppc_br24abs
24-bit absolute relocation for direct branches like 'ba' and 'bla'.
@ R_REF
A non-relocating relocation.
Definition XCOFF.h:294
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectTargetWriter > createPPCXCOFFObjectWriter(bool Is64Bit)
Construct a PPC XCOFF object writer.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:37
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:36