LLVM 20.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
13#include "llvm/MC/MCFixup.h"
14#include "llvm/MC/MCValue.h"
16
17using namespace llvm;
18
19namespace {
20class PPCXCOFFObjectWriter : public MCXCOFFObjectTargetWriter {
21 static constexpr uint8_t SignBitMask = 0x80;
22
23public:
24 PPCXCOFFObjectWriter(bool Is64Bit);
25
26 std::pair<uint8_t, uint8_t>
28 bool IsPCRel) const override;
29};
30} // end anonymous namespace
31
32PPCXCOFFObjectWriter::PPCXCOFFObjectWriter(bool Is64Bit)
33 : MCXCOFFObjectTargetWriter(Is64Bit) {}
34
35std::unique_ptr<MCObjectTargetWriter>
37 return std::make_unique<PPCXCOFFObjectWriter>(Is64Bit);
38}
39
40std::pair<uint8_t, uint8_t> PPCXCOFFObjectWriter::getRelocTypeAndSignSize(
41 const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const {
42 const MCSymbolRefExpr::VariantKind Modifier =
43 Target.isAbsolute() ? MCSymbolRefExpr::VK_None
44 : Target.getSymA()->getKind();
45 // People from AIX OS team says AIX link editor does not care about
46 // the sign bit in the relocation entry "most" of the time.
47 // The system assembler seems to set the sign bit on relocation entry
48 // based on similar property of IsPCRel. So we will do the same here.
49 // TODO: More investigation on how assembler decides to set the sign
50 // bit, and we might want to match that.
51 const uint8_t EncodedSignednessIndicator = IsPCRel ? SignBitMask : 0u;
52
53 // The magic number we use in SignAndSize has a strong relationship with
54 // the corresponding MCFixupKind. In most cases, it's the MCFixupKind
55 // number - 1, because SignAndSize encodes the bit length being
56 // relocated minus 1.
57 switch ((unsigned)Fixup.getKind()) {
58 default:
59 report_fatal_error("Unimplemented fixup kind.");
61 const uint8_t SignAndSizeForHalf16 = EncodedSignednessIndicator | 15;
62 switch (Modifier) {
63 default:
64 report_fatal_error("Unsupported modifier for half16 fixup.");
66 return {XCOFF::RelocationType::R_TOC, SignAndSizeForHalf16};
68 return {XCOFF::RelocationType::R_TOCU, SignAndSizeForHalf16};
70 return {XCOFF::RelocationType::R_TOCL, SignAndSizeForHalf16};
72 return {XCOFF::RelocationType::R_TLS_LE, SignAndSizeForHalf16};
74 return {XCOFF::RelocationType::R_TLS_LD, SignAndSizeForHalf16};
75 }
76 } break;
79 if (IsPCRel)
80 report_fatal_error("Invalid PC-relative relocation.");
81 switch (Modifier) {
82 default:
83 llvm_unreachable("Unsupported Modifier");
85 return {XCOFF::RelocationType::R_TOC, 15};
87 return {XCOFF::RelocationType::R_TOCL, 15};
89 return {XCOFF::RelocationType::R_TLS_LE, 15};
91 return {XCOFF::RelocationType::R_TLS_LD, 15};
92 }
93 } break;
95 // Branches are 4 byte aligned, so the 24 bits we encode in
96 // the instruction actually represents a 26 bit offset.
97 return {XCOFF::RelocationType::R_RBR, EncodedSignednessIndicator | 25};
99 return {XCOFF::RelocationType::R_RBA, EncodedSignednessIndicator | 25};
101 if (Modifier == MCSymbolRefExpr::VK_None)
102 return {XCOFF::RelocationType::R_REF, 0};
103 else
104 llvm_unreachable("Unsupported Modifier");
105 } break;
106 case FK_Data_4:
107 case FK_Data_8:
108 const uint8_t SignAndSizeForFKData =
109 EncodedSignednessIndicator |
110 ((unsigned)Fixup.getKind() == FK_Data_4 ? 31 : 63);
111 switch (Modifier) {
112 default:
113 report_fatal_error("Unsupported modifier");
115 return {XCOFF::RelocationType::R_TLS, SignAndSizeForFKData};
117 return {XCOFF::RelocationType::R_TLSM, SignAndSizeForFKData};
119 return {XCOFF::RelocationType::R_TLS_IE, SignAndSizeForFKData};
121 return {XCOFF::RelocationType::R_TLS_LE, SignAndSizeForFKData};
123 return {XCOFF::RelocationType::R_TLS_LD, SignAndSizeForFKData};
125 return {XCOFF::RelocationType::R_TLSML, SignAndSizeForFKData};
127 return {XCOFF::RelocationType::R_POS, SignAndSizeForFKData};
128 }
129 }
130}
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:71
This represents an "assembler immediate".
Definition: MCValue.h:36
virtual std::pair< uint8_t, uint8_t > getRelocTypeAndSignSize(const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
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'.
Definition: PPCFixupKinds.h:37
@ fixup_ppc_half16dq
A 16-bit fixup corresponding to lo16(_foo) with implied 3 zero bits for instrs like 'lxv'.
Definition: PPCFixupKinds.h:57
@ fixup_ppc_half16ds
A 14-bit fixup corresponding to lo16(_foo) with implied 2 zero bits for instrs like 'std'.
Definition: PPCFixupKinds.h:41
@ fixup_ppc_nofixup
Not a true fixup, but ties a symbol to a call to __tls_get_addr for the TLS general and local dynamic...
Definition: PPCFixupKinds.h:53
@ fixup_ppc_br24abs
24-bit absolute relocation for direct branches like 'ba' and 'bla'.
Definition: PPCFixupKinds.h:30
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectTargetWriter > createPPCXCOFFObjectWriter(bool Is64Bit)
Construct a PPC XCOFF object writer.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25