1 //===-- PPCELFObjectWriter.cpp - PPC ELF Writer ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MCTargetDesc/PPCFixupKinds.h"
10 #include "MCTargetDesc/PPCMCExpr.h"
11 #include "MCTargetDesc/PPCMCTargetDesc.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSymbolELF.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/ErrorHandling.h"
23 class PPCELFObjectWriter
: public MCELFObjectTargetWriter
{
25 PPCELFObjectWriter(bool Is64Bit
, uint8_t OSABI
);
28 unsigned getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
29 const MCFixup
&Fixup
, bool IsPCRel
) const override
;
31 bool needsRelocateWithSymbol(const MCSymbol
&Sym
,
32 unsigned Type
) const override
;
36 PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit
, uint8_t OSABI
)
37 : MCELFObjectTargetWriter(Is64Bit
, OSABI
,
38 Is64Bit
? ELF::EM_PPC64
: ELF::EM_PPC
,
39 /*HasRelocationAddend*/ true) {}
41 static MCSymbolRefExpr::VariantKind
getAccessVariant(const MCValue
&Target
,
42 const MCFixup
&Fixup
) {
43 const MCExpr
*Expr
= Fixup
.getValue();
45 if (Expr
->getKind() != MCExpr::Target
)
46 return Target
.getAccessVariant();
48 switch (cast
<PPCMCExpr
>(Expr
)->getKind()) {
49 case PPCMCExpr::VK_PPC_None
:
50 return MCSymbolRefExpr::VK_None
;
51 case PPCMCExpr::VK_PPC_LO
:
52 return MCSymbolRefExpr::VK_PPC_LO
;
53 case PPCMCExpr::VK_PPC_HI
:
54 return MCSymbolRefExpr::VK_PPC_HI
;
55 case PPCMCExpr::VK_PPC_HA
:
56 return MCSymbolRefExpr::VK_PPC_HA
;
57 case PPCMCExpr::VK_PPC_HIGH
:
58 return MCSymbolRefExpr::VK_PPC_HIGH
;
59 case PPCMCExpr::VK_PPC_HIGHA
:
60 return MCSymbolRefExpr::VK_PPC_HIGHA
;
61 case PPCMCExpr::VK_PPC_HIGHERA
:
62 return MCSymbolRefExpr::VK_PPC_HIGHERA
;
63 case PPCMCExpr::VK_PPC_HIGHER
:
64 return MCSymbolRefExpr::VK_PPC_HIGHER
;
65 case PPCMCExpr::VK_PPC_HIGHEST
:
66 return MCSymbolRefExpr::VK_PPC_HIGHEST
;
67 case PPCMCExpr::VK_PPC_HIGHESTA
:
68 return MCSymbolRefExpr::VK_PPC_HIGHESTA
;
70 llvm_unreachable("unknown PPCMCExpr kind");
73 unsigned PPCELFObjectWriter::getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
76 MCSymbolRefExpr::VariantKind Modifier
= getAccessVariant(Target
, Fixup
);
78 // determine the type of the relocation
81 switch (Fixup
.getTargetKind()) {
83 llvm_unreachable("Unimplemented");
84 case PPC::fixup_ppc_br24
:
85 case PPC::fixup_ppc_br24abs
:
87 default: llvm_unreachable("Unsupported Modifier");
88 case MCSymbolRefExpr::VK_None
:
89 Type
= ELF::R_PPC_REL24
;
91 case MCSymbolRefExpr::VK_PLT
:
92 Type
= ELF::R_PPC_PLTREL24
;
94 case MCSymbolRefExpr::VK_PPC_LOCAL
:
95 Type
= ELF::R_PPC_LOCAL24PC
;
99 case PPC::fixup_ppc_brcond14
:
100 case PPC::fixup_ppc_brcond14abs
:
101 Type
= ELF::R_PPC_REL14
;
103 case PPC::fixup_ppc_half16
:
105 default: llvm_unreachable("Unsupported Modifier");
106 case MCSymbolRefExpr::VK_None
:
107 Type
= ELF::R_PPC_REL16
;
109 case MCSymbolRefExpr::VK_PPC_LO
:
110 Type
= ELF::R_PPC_REL16_LO
;
112 case MCSymbolRefExpr::VK_PPC_HI
:
113 Type
= ELF::R_PPC_REL16_HI
;
115 case MCSymbolRefExpr::VK_PPC_HA
:
116 Type
= ELF::R_PPC_REL16_HA
;
120 case PPC::fixup_ppc_half16ds
:
121 Target
.print(errs());
123 report_fatal_error("Invalid PC-relative half16ds relocation");
126 Type
= ELF::R_PPC_REL32
;
130 Type
= ELF::R_PPC64_REL64
;
134 switch (Fixup
.getTargetKind()) {
135 default: llvm_unreachable("invalid fixup kind!");
137 Type
= ELF::R_PPC_NONE
;
139 case PPC::fixup_ppc_br24abs
:
140 Type
= ELF::R_PPC_ADDR24
;
142 case PPC::fixup_ppc_brcond14abs
:
143 Type
= ELF::R_PPC_ADDR14
; // XXX: or BRNTAKEN?_
145 case PPC::fixup_ppc_half16
:
147 default: llvm_unreachable("Unsupported Modifier");
148 case MCSymbolRefExpr::VK_None
:
149 Type
= ELF::R_PPC_ADDR16
;
151 case MCSymbolRefExpr::VK_PPC_LO
:
152 Type
= ELF::R_PPC_ADDR16_LO
;
154 case MCSymbolRefExpr::VK_PPC_HI
:
155 Type
= ELF::R_PPC_ADDR16_HI
;
157 case MCSymbolRefExpr::VK_PPC_HA
:
158 Type
= ELF::R_PPC_ADDR16_HA
;
160 case MCSymbolRefExpr::VK_PPC_HIGH
:
161 Type
= ELF::R_PPC64_ADDR16_HIGH
;
163 case MCSymbolRefExpr::VK_PPC_HIGHA
:
164 Type
= ELF::R_PPC64_ADDR16_HIGHA
;
166 case MCSymbolRefExpr::VK_PPC_HIGHER
:
167 Type
= ELF::R_PPC64_ADDR16_HIGHER
;
169 case MCSymbolRefExpr::VK_PPC_HIGHERA
:
170 Type
= ELF::R_PPC64_ADDR16_HIGHERA
;
172 case MCSymbolRefExpr::VK_PPC_HIGHEST
:
173 Type
= ELF::R_PPC64_ADDR16_HIGHEST
;
175 case MCSymbolRefExpr::VK_PPC_HIGHESTA
:
176 Type
= ELF::R_PPC64_ADDR16_HIGHESTA
;
178 case MCSymbolRefExpr::VK_GOT
:
179 Type
= ELF::R_PPC_GOT16
;
181 case MCSymbolRefExpr::VK_PPC_GOT_LO
:
182 Type
= ELF::R_PPC_GOT16_LO
;
184 case MCSymbolRefExpr::VK_PPC_GOT_HI
:
185 Type
= ELF::R_PPC_GOT16_HI
;
187 case MCSymbolRefExpr::VK_PPC_GOT_HA
:
188 Type
= ELF::R_PPC_GOT16_HA
;
190 case MCSymbolRefExpr::VK_PPC_TOC
:
191 Type
= ELF::R_PPC64_TOC16
;
193 case MCSymbolRefExpr::VK_PPC_TOC_LO
:
194 Type
= ELF::R_PPC64_TOC16_LO
;
196 case MCSymbolRefExpr::VK_PPC_TOC_HI
:
197 Type
= ELF::R_PPC64_TOC16_HI
;
199 case MCSymbolRefExpr::VK_PPC_TOC_HA
:
200 Type
= ELF::R_PPC64_TOC16_HA
;
202 case MCSymbolRefExpr::VK_TPREL
:
203 Type
= ELF::R_PPC_TPREL16
;
205 case MCSymbolRefExpr::VK_PPC_TPREL_LO
:
206 Type
= ELF::R_PPC_TPREL16_LO
;
208 case MCSymbolRefExpr::VK_PPC_TPREL_HI
:
209 Type
= ELF::R_PPC_TPREL16_HI
;
211 case MCSymbolRefExpr::VK_PPC_TPREL_HA
:
212 Type
= ELF::R_PPC_TPREL16_HA
;
214 case MCSymbolRefExpr::VK_PPC_TPREL_HIGH
:
215 Type
= ELF::R_PPC64_TPREL16_HIGH
;
217 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA
:
218 Type
= ELF::R_PPC64_TPREL16_HIGHA
;
220 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER
:
221 Type
= ELF::R_PPC64_TPREL16_HIGHER
;
223 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA
:
224 Type
= ELF::R_PPC64_TPREL16_HIGHERA
;
226 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST
:
227 Type
= ELF::R_PPC64_TPREL16_HIGHEST
;
229 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA
:
230 Type
= ELF::R_PPC64_TPREL16_HIGHESTA
;
232 case MCSymbolRefExpr::VK_DTPREL
:
233 Type
= ELF::R_PPC64_DTPREL16
;
235 case MCSymbolRefExpr::VK_PPC_DTPREL_LO
:
236 Type
= ELF::R_PPC64_DTPREL16_LO
;
238 case MCSymbolRefExpr::VK_PPC_DTPREL_HI
:
239 Type
= ELF::R_PPC64_DTPREL16_HI
;
241 case MCSymbolRefExpr::VK_PPC_DTPREL_HA
:
242 Type
= ELF::R_PPC64_DTPREL16_HA
;
244 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH
:
245 Type
= ELF::R_PPC64_DTPREL16_HIGH
;
247 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA
:
248 Type
= ELF::R_PPC64_DTPREL16_HIGHA
;
250 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER
:
251 Type
= ELF::R_PPC64_DTPREL16_HIGHER
;
253 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA
:
254 Type
= ELF::R_PPC64_DTPREL16_HIGHERA
;
256 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST
:
257 Type
= ELF::R_PPC64_DTPREL16_HIGHEST
;
259 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA
:
260 Type
= ELF::R_PPC64_DTPREL16_HIGHESTA
;
262 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD
:
264 Type
= ELF::R_PPC64_GOT_TLSGD16
;
266 Type
= ELF::R_PPC_GOT_TLSGD16
;
268 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO
:
269 Type
= ELF::R_PPC64_GOT_TLSGD16_LO
;
271 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI
:
272 Type
= ELF::R_PPC64_GOT_TLSGD16_HI
;
274 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA
:
275 Type
= ELF::R_PPC64_GOT_TLSGD16_HA
;
277 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD
:
279 Type
= ELF::R_PPC64_GOT_TLSLD16
;
281 Type
= ELF::R_PPC_GOT_TLSLD16
;
283 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO
:
284 Type
= ELF::R_PPC64_GOT_TLSLD16_LO
;
286 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI
:
287 Type
= ELF::R_PPC64_GOT_TLSLD16_HI
;
289 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA
:
290 Type
= ELF::R_PPC64_GOT_TLSLD16_HA
;
292 case MCSymbolRefExpr::VK_PPC_GOT_TPREL
:
293 /* We don't have R_PPC64_GOT_TPREL16, but since GOT offsets
294 are always 4-aligned, we can use R_PPC64_GOT_TPREL16_DS. */
295 Type
= ELF::R_PPC64_GOT_TPREL16_DS
;
297 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO
:
298 /* We don't have R_PPC64_GOT_TPREL16_LO, but since GOT offsets
299 are always 4-aligned, we can use R_PPC64_GOT_TPREL16_LO_DS. */
300 Type
= ELF::R_PPC64_GOT_TPREL16_LO_DS
;
302 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI
:
303 Type
= ELF::R_PPC64_GOT_TPREL16_HI
;
305 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL
:
306 /* We don't have R_PPC64_GOT_DTPREL16, but since GOT offsets
307 are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_DS. */
308 Type
= ELF::R_PPC64_GOT_DTPREL16_DS
;
310 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO
:
311 /* We don't have R_PPC64_GOT_DTPREL16_LO, but since GOT offsets
312 are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_LO_DS. */
313 Type
= ELF::R_PPC64_GOT_DTPREL16_LO_DS
;
315 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA
:
316 Type
= ELF::R_PPC64_GOT_TPREL16_HA
;
318 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI
:
319 Type
= ELF::R_PPC64_GOT_DTPREL16_HI
;
321 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA
:
322 Type
= ELF::R_PPC64_GOT_DTPREL16_HA
;
326 case PPC::fixup_ppc_half16ds
:
328 default: llvm_unreachable("Unsupported Modifier");
329 case MCSymbolRefExpr::VK_None
:
330 Type
= ELF::R_PPC64_ADDR16_DS
;
332 case MCSymbolRefExpr::VK_PPC_LO
:
333 Type
= ELF::R_PPC64_ADDR16_LO_DS
;
335 case MCSymbolRefExpr::VK_GOT
:
336 Type
= ELF::R_PPC64_GOT16_DS
;
338 case MCSymbolRefExpr::VK_PPC_GOT_LO
:
339 Type
= ELF::R_PPC64_GOT16_LO_DS
;
341 case MCSymbolRefExpr::VK_PPC_TOC
:
342 Type
= ELF::R_PPC64_TOC16_DS
;
344 case MCSymbolRefExpr::VK_PPC_TOC_LO
:
345 Type
= ELF::R_PPC64_TOC16_LO_DS
;
347 case MCSymbolRefExpr::VK_TPREL
:
348 Type
= ELF::R_PPC64_TPREL16_DS
;
350 case MCSymbolRefExpr::VK_PPC_TPREL_LO
:
351 Type
= ELF::R_PPC64_TPREL16_LO_DS
;
353 case MCSymbolRefExpr::VK_DTPREL
:
354 Type
= ELF::R_PPC64_DTPREL16_DS
;
356 case MCSymbolRefExpr::VK_PPC_DTPREL_LO
:
357 Type
= ELF::R_PPC64_DTPREL16_LO_DS
;
359 case MCSymbolRefExpr::VK_PPC_GOT_TPREL
:
360 Type
= ELF::R_PPC64_GOT_TPREL16_DS
;
362 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO
:
363 Type
= ELF::R_PPC64_GOT_TPREL16_LO_DS
;
365 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL
:
366 Type
= ELF::R_PPC64_GOT_DTPREL16_DS
;
368 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO
:
369 Type
= ELF::R_PPC64_GOT_DTPREL16_LO_DS
;
373 case PPC::fixup_ppc_nofixup
:
375 default: llvm_unreachable("Unsupported Modifier");
376 case MCSymbolRefExpr::VK_PPC_TLSGD
:
378 Type
= ELF::R_PPC64_TLSGD
;
380 Type
= ELF::R_PPC_TLSGD
;
382 case MCSymbolRefExpr::VK_PPC_TLSLD
:
384 Type
= ELF::R_PPC64_TLSLD
;
386 Type
= ELF::R_PPC_TLSLD
;
388 case MCSymbolRefExpr::VK_PPC_TLS
:
390 Type
= ELF::R_PPC64_TLS
;
392 Type
= ELF::R_PPC_TLS
;
398 default: llvm_unreachable("Unsupported Modifier");
399 case MCSymbolRefExpr::VK_PPC_TOCBASE
:
400 Type
= ELF::R_PPC64_TOC
;
402 case MCSymbolRefExpr::VK_None
:
403 Type
= ELF::R_PPC64_ADDR64
;
405 case MCSymbolRefExpr::VK_PPC_DTPMOD
:
406 Type
= ELF::R_PPC64_DTPMOD64
;
408 case MCSymbolRefExpr::VK_TPREL
:
409 Type
= ELF::R_PPC64_TPREL64
;
411 case MCSymbolRefExpr::VK_DTPREL
:
412 Type
= ELF::R_PPC64_DTPREL64
;
417 Type
= ELF::R_PPC_ADDR32
;
420 Type
= ELF::R_PPC_ADDR16
;
427 bool PPCELFObjectWriter::needsRelocateWithSymbol(const MCSymbol
&Sym
,
428 unsigned Type
) const {
433 case ELF::R_PPC_REL24
:
434 // If the target symbol has a local entry point, we must keep the
435 // target symbol to preserve that information for the linker.
436 // The "other" values are stored in the last 6 bits of the second byte.
437 // The traditional defines for STO values assume the full byte and thus
438 // the shift to pack it.
439 unsigned Other
= cast
<MCSymbolELF
>(Sym
).getOther() << 2;
440 return (Other
& ELF::STO_PPC64_LOCAL_MASK
) != 0;
444 std::unique_ptr
<MCObjectTargetWriter
>
445 llvm::createPPCELFObjectWriter(bool Is64Bit
, uint8_t OSABI
) {
446 return std::make_unique
<PPCELFObjectWriter
>(Is64Bit
, OSABI
);