LLVM 19.0.0git
SystemZELFObjectWriter.cpp
Go to the documentation of this file.
1//===-- SystemZELFObjectWriter.cpp - SystemZ ELF 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"
14#include "llvm/MC/MCExpr.h"
15#include "llvm/MC/MCFixup.h"
17#include "llvm/MC/MCValue.h"
19#include <cassert>
20#include <cstdint>
21#include <memory>
22
23using namespace llvm;
24
25namespace {
26
27class SystemZELFObjectWriter : public MCELFObjectTargetWriter {
28public:
29 SystemZELFObjectWriter(uint8_t OSABI);
30 ~SystemZELFObjectWriter() override = default;
31
32protected:
33 // Override MCELFObjectTargetWriter.
34 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
35 const MCFixup &Fixup, bool IsPCRel) const override;
36};
37
38} // end anonymous namespace
39
40SystemZELFObjectWriter::SystemZELFObjectWriter(uint8_t OSABI)
41 : MCELFObjectTargetWriter(/*Is64Bit_=*/true, OSABI, ELF::EM_S390,
42 /*HasRelocationAddend_=*/true) {}
43
44// Return the relocation type for an absolute value of MCFixupKind Kind.
45static unsigned getAbsoluteReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind) {
46 switch (Kind) {
47 case FK_Data_1:
50 return ELF::R_390_8;
52 return ELF::R_390_12;
53 case FK_Data_2:
56 return ELF::R_390_16;
58 return ELF::R_390_20;
59 case FK_Data_4:
62 return ELF::R_390_32;
63 case FK_Data_8:
64 return ELF::R_390_64;
65 }
66 Ctx.reportError(Loc, "Unsupported absolute address");
67 return 0;
68}
69
70// Return the relocation type for a PC-relative value of MCFixupKind Kind.
71static unsigned getPCRelReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind) {
72 switch (Kind) {
73 case FK_Data_2:
76 return ELF::R_390_PC16;
77 case FK_Data_4:
80 return ELF::R_390_PC32;
81 case FK_Data_8:
82 return ELF::R_390_PC64;
84 return ELF::R_390_PC12DBL;
86 return ELF::R_390_PC16DBL;
88 return ELF::R_390_PC24DBL;
90 return ELF::R_390_PC32DBL;
91 }
92 Ctx.reportError(Loc, "Unsupported PC-relative address");
93 return 0;
94}
95
96// Return the R_390_TLS_LE* relocation type for MCFixupKind Kind.
97static unsigned getTLSLEReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind) {
98 switch (Kind) {
99 case FK_Data_4: return ELF::R_390_TLS_LE32;
100 case FK_Data_8: return ELF::R_390_TLS_LE64;
101 }
102 Ctx.reportError(Loc, "Unsupported thread-local address (local-exec)");
103 return 0;
104}
105
106// Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind.
107static unsigned getTLSLDOReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind) {
108 switch (Kind) {
109 case FK_Data_4: return ELF::R_390_TLS_LDO32;
110 case FK_Data_8: return ELF::R_390_TLS_LDO64;
111 }
112 Ctx.reportError(Loc, "Unsupported thread-local address (local-dynamic)");
113 return 0;
114}
115
116// Return the R_390_TLS_LDM* relocation type for MCFixupKind Kind.
117static unsigned getTLSLDMReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind) {
118 switch (Kind) {
119 case FK_Data_4: return ELF::R_390_TLS_LDM32;
120 case FK_Data_8: return ELF::R_390_TLS_LDM64;
121 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL;
122 }
123 Ctx.reportError(Loc, "Unsupported thread-local address (local-dynamic)");
124 return 0;
125}
126
127// Return the R_390_TLS_GD* relocation type for MCFixupKind Kind.
128static unsigned getTLSGDReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind) {
129 switch (Kind) {
130 case FK_Data_4: return ELF::R_390_TLS_GD32;
131 case FK_Data_8: return ELF::R_390_TLS_GD64;
132 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL;
133 }
134 Ctx.reportError(Loc, "Unsupported thread-local address (general-dynamic)");
135 return 0;
136}
137
138// Return the PLT relocation counterpart of MCFixupKind Kind.
139static unsigned getPLTReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind) {
140 switch (Kind) {
141 case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL;
142 case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL;
143 case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL;
144 case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL;
145 }
146 Ctx.reportError(Loc, "Unsupported PC-relative PLT address");
147 return 0;
148}
149
150unsigned SystemZELFObjectWriter::getRelocType(MCContext &Ctx,
151 const MCValue &Target,
152 const MCFixup &Fixup,
153 bool IsPCRel) const {
154 SMLoc Loc = Fixup.getLoc();
155 unsigned Kind = Fixup.getKind();
156 if (Kind >= FirstLiteralRelocationKind)
158 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
159 switch (Modifier) {
161 if (IsPCRel)
162 return getPCRelReloc(Ctx, Loc, Kind);
163 return getAbsoluteReloc(Ctx, Loc, Kind);
164
166 assert(!IsPCRel && "NTPOFF shouldn't be PC-relative");
167 return getTLSLEReloc(Ctx, Loc, Kind);
168
170 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
171 return ELF::R_390_TLS_IEENT;
172 Ctx.reportError(Loc, "Only PC-relative INDNTPOFF accesses are supported for now");
173 return 0;
174
176 assert(!IsPCRel && "DTPOFF shouldn't be PC-relative");
177 return getTLSLDOReloc(Ctx, Loc, Kind);
178
180 assert(!IsPCRel && "TLSLDM shouldn't be PC-relative");
181 return getTLSLDMReloc(Ctx, Loc, Kind);
182
184 assert(!IsPCRel && "TLSGD shouldn't be PC-relative");
185 return getTLSGDReloc(Ctx, Loc, Kind);
186
188 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
189 return ELF::R_390_GOTENT;
190 Ctx.reportError(Loc, "Only PC-relative GOT accesses are supported for now");
191 return 0;
192
194 assert(IsPCRel && "@PLT shouldn't be PC-relative");
195 return getPLTReloc(Ctx, Loc, Kind);
196
197 default:
198 llvm_unreachable("Modifier not supported");
199 }
200}
201
202std::unique_ptr<MCObjectTargetWriter>
204 return std::make_unique<SystemZELFObjectWriter>(OSABI);
205}
basic Basic Alias true
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static unsigned getPLTReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind)
static unsigned getTLSGDReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind)
static unsigned getPCRelReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind)
static unsigned getTLSLDOReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind)
static unsigned getTLSLDMReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind)
static unsigned getAbsoluteReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind)
static unsigned getTLSLEReloc(MCContext &Ctx, SMLoc Loc, unsigned Kind)
Context object for machine code objects.
Definition: MCContext.h:81
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1069
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
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
Represents a location in source code.
Definition: SMLoc.h:23
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ EM_S390
Definition: ELF.h:150
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24
std::unique_ptr< MCObjectTargetWriter > createSystemZELFObjectWriter(uint8_t OSABI)