1 //===-- PPCELFObjectWriter.cpp - PPC ELF Writer ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/PPCMCTargetDesc.h"
11 #include "MCTargetDesc/PPCFixupKinds.h"
12 #include "MCTargetDesc/PPCMCExpr.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.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_HIGHERA:
58 return MCSymbolRefExpr::VK_PPC_HIGHERA;
59 case PPCMCExpr::VK_PPC_HIGHER:
60 return MCSymbolRefExpr::VK_PPC_HIGHER;
61 case PPCMCExpr::VK_PPC_HIGHEST:
62 return MCSymbolRefExpr::VK_PPC_HIGHEST;
63 case PPCMCExpr::VK_PPC_HIGHESTA:
64 return MCSymbolRefExpr::VK_PPC_HIGHESTA;
66 llvm_unreachable("unknown PPCMCExpr kind");
69 unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
72 MCSymbolRefExpr::VariantKind Modifier = getAccessVariant(Target, Fixup);
74 // determine the type of the relocation
77 switch ((unsigned)Fixup.getKind()) {
79 llvm_unreachable("Unimplemented");
80 case PPC::fixup_ppc_br24:
81 case PPC::fixup_ppc_br24abs:
83 default: llvm_unreachable("Unsupported Modifier");
84 case MCSymbolRefExpr::VK_None:
85 Type = ELF::R_PPC_REL24;
87 case MCSymbolRefExpr::VK_PLT:
88 Type = ELF::R_PPC_PLTREL24;
90 case MCSymbolRefExpr::VK_PPC_LOCAL:
91 Type = ELF::R_PPC_LOCAL24PC;
95 case PPC::fixup_ppc_brcond14:
96 case PPC::fixup_ppc_brcond14abs:
97 Type = ELF::R_PPC_REL14;
99 case PPC::fixup_ppc_half16:
101 default: llvm_unreachable("Unsupported Modifier");
102 case MCSymbolRefExpr::VK_None:
103 Type = ELF::R_PPC_REL16;
105 case MCSymbolRefExpr::VK_PPC_LO:
106 Type = ELF::R_PPC_REL16_LO;
108 case MCSymbolRefExpr::VK_PPC_HI:
109 Type = ELF::R_PPC_REL16_HI;
111 case MCSymbolRefExpr::VK_PPC_HA:
112 Type = ELF::R_PPC_REL16_HA;
116 case PPC::fixup_ppc_half16ds:
117 Target.print(errs());
119 report_fatal_error("Invalid PC-relative half16ds relocation");
122 Type = ELF::R_PPC_REL32;
126 Type = ELF::R_PPC64_REL64;
130 switch ((unsigned)Fixup.getKind()) {
131 default: llvm_unreachable("invalid fixup kind!");
132 case PPC::fixup_ppc_br24abs:
133 Type = ELF::R_PPC_ADDR24;
135 case PPC::fixup_ppc_brcond14abs:
136 Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
138 case PPC::fixup_ppc_half16:
140 default: llvm_unreachable("Unsupported Modifier");
141 case MCSymbolRefExpr::VK_None:
142 Type = ELF::R_PPC_ADDR16;
144 case MCSymbolRefExpr::VK_PPC_LO:
145 Type = ELF::R_PPC_ADDR16_LO;
147 case MCSymbolRefExpr::VK_PPC_HI:
148 Type = ELF::R_PPC_ADDR16_HI;
150 case MCSymbolRefExpr::VK_PPC_HA:
151 Type = ELF::R_PPC_ADDR16_HA;
153 case MCSymbolRefExpr::VK_PPC_HIGHER:
154 Type = ELF::R_PPC64_ADDR16_HIGHER;
156 case MCSymbolRefExpr::VK_PPC_HIGHERA:
157 Type = ELF::R_PPC64_ADDR16_HIGHERA;
159 case MCSymbolRefExpr::VK_PPC_HIGHEST:
160 Type = ELF::R_PPC64_ADDR16_HIGHEST;
162 case MCSymbolRefExpr::VK_PPC_HIGHESTA:
163 Type = ELF::R_PPC64_ADDR16_HIGHESTA;
165 case MCSymbolRefExpr::VK_GOT:
166 Type = ELF::R_PPC_GOT16;
168 case MCSymbolRefExpr::VK_PPC_GOT_LO:
169 Type = ELF::R_PPC_GOT16_LO;
171 case MCSymbolRefExpr::VK_PPC_GOT_HI:
172 Type = ELF::R_PPC_GOT16_HI;
174 case MCSymbolRefExpr::VK_PPC_GOT_HA:
175 Type = ELF::R_PPC_GOT16_HA;
177 case MCSymbolRefExpr::VK_PPC_TOC:
178 Type = ELF::R_PPC64_TOC16;
180 case MCSymbolRefExpr::VK_PPC_TOC_LO:
181 Type = ELF::R_PPC64_TOC16_LO;
183 case MCSymbolRefExpr::VK_PPC_TOC_HI:
184 Type = ELF::R_PPC64_TOC16_HI;
186 case MCSymbolRefExpr::VK_PPC_TOC_HA:
187 Type = ELF::R_PPC64_TOC16_HA;
189 case MCSymbolRefExpr::VK_TPREL:
190 Type = ELF::R_PPC_TPREL16;
192 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
193 Type = ELF::R_PPC_TPREL16_LO;
195 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
196 Type = ELF::R_PPC_TPREL16_HI;
198 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
199 Type = ELF::R_PPC_TPREL16_HA;
201 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
202 Type = ELF::R_PPC64_TPREL16_HIGHER;
204 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
205 Type = ELF::R_PPC64_TPREL16_HIGHERA;
207 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
208 Type = ELF::R_PPC64_TPREL16_HIGHEST;
210 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
211 Type = ELF::R_PPC64_TPREL16_HIGHESTA;
213 case MCSymbolRefExpr::VK_DTPREL:
214 Type = ELF::R_PPC64_DTPREL16;
216 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
217 Type = ELF::R_PPC64_DTPREL16_LO;
219 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
220 Type = ELF::R_PPC64_DTPREL16_HI;
222 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
223 Type = ELF::R_PPC64_DTPREL16_HA;
225 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
226 Type = ELF::R_PPC64_DTPREL16_HIGHER;
228 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
229 Type = ELF::R_PPC64_DTPREL16_HIGHERA;
231 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
232 Type = ELF::R_PPC64_DTPREL16_HIGHEST;
234 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
235 Type = ELF::R_PPC64_DTPREL16_HIGHESTA;
237 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
239 Type = ELF::R_PPC64_GOT_TLSGD16;
241 Type = ELF::R_PPC_GOT_TLSGD16;
243 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
244 Type = ELF::R_PPC64_GOT_TLSGD16_LO;
246 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
247 Type = ELF::R_PPC64_GOT_TLSGD16_HI;
249 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
250 Type = ELF::R_PPC64_GOT_TLSGD16_HA;
252 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
254 Type = ELF::R_PPC64_GOT_TLSLD16;
256 Type = ELF::R_PPC_GOT_TLSLD16;
258 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
259 Type = ELF::R_PPC64_GOT_TLSLD16_LO;
261 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
262 Type = ELF::R_PPC64_GOT_TLSLD16_HI;
264 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
265 Type = ELF::R_PPC64_GOT_TLSLD16_HA;
267 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
268 /* We don't have R_PPC64_GOT_TPREL16, but since GOT offsets
269 are always 4-aligned, we can use R_PPC64_GOT_TPREL16_DS. */
270 Type = ELF::R_PPC64_GOT_TPREL16_DS;
272 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
273 /* We don't have R_PPC64_GOT_TPREL16_LO, but since GOT offsets
274 are always 4-aligned, we can use R_PPC64_GOT_TPREL16_LO_DS. */
275 Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
277 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
278 Type = ELF::R_PPC64_GOT_TPREL16_HI;
280 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
281 /* We don't have R_PPC64_GOT_DTPREL16, but since GOT offsets
282 are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_DS. */
283 Type = ELF::R_PPC64_GOT_DTPREL16_DS;
285 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
286 /* We don't have R_PPC64_GOT_DTPREL16_LO, but since GOT offsets
287 are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_LO_DS. */
288 Type = ELF::R_PPC64_GOT_DTPREL16_LO_DS;
290 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
291 Type = ELF::R_PPC64_GOT_TPREL16_HA;
293 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
294 Type = ELF::R_PPC64_GOT_DTPREL16_HI;
296 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
297 Type = ELF::R_PPC64_GOT_DTPREL16_HA;
301 case PPC::fixup_ppc_half16ds:
303 default: llvm_unreachable("Unsupported Modifier");
304 case MCSymbolRefExpr::VK_None:
305 Type = ELF::R_PPC64_ADDR16_DS;
307 case MCSymbolRefExpr::VK_PPC_LO:
308 Type = ELF::R_PPC64_ADDR16_LO_DS;
310 case MCSymbolRefExpr::VK_GOT:
311 Type = ELF::R_PPC64_GOT16_DS;
313 case MCSymbolRefExpr::VK_PPC_GOT_LO:
314 Type = ELF::R_PPC64_GOT16_LO_DS;
316 case MCSymbolRefExpr::VK_PPC_TOC:
317 Type = ELF::R_PPC64_TOC16_DS;
319 case MCSymbolRefExpr::VK_PPC_TOC_LO:
320 Type = ELF::R_PPC64_TOC16_LO_DS;
322 case MCSymbolRefExpr::VK_TPREL:
323 Type = ELF::R_PPC64_TPREL16_DS;
325 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
326 Type = ELF::R_PPC64_TPREL16_LO_DS;
328 case MCSymbolRefExpr::VK_DTPREL:
329 Type = ELF::R_PPC64_DTPREL16_DS;
331 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
332 Type = ELF::R_PPC64_DTPREL16_LO_DS;
334 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
335 Type = ELF::R_PPC64_GOT_TPREL16_DS;
337 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
338 Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
340 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
341 Type = ELF::R_PPC64_GOT_DTPREL16_DS;
343 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
344 Type = ELF::R_PPC64_GOT_DTPREL16_LO_DS;
348 case PPC::fixup_ppc_nofixup:
350 default: llvm_unreachable("Unsupported Modifier");
351 case MCSymbolRefExpr::VK_PPC_TLSGD:
353 Type = ELF::R_PPC64_TLSGD;
355 Type = ELF::R_PPC_TLSGD;
357 case MCSymbolRefExpr::VK_PPC_TLSLD:
359 Type = ELF::R_PPC64_TLSLD;
361 Type = ELF::R_PPC_TLSLD;
363 case MCSymbolRefExpr::VK_PPC_TLS:
365 Type = ELF::R_PPC64_TLS;
367 Type = ELF::R_PPC_TLS;
373 default: llvm_unreachable("Unsupported Modifier");
374 case MCSymbolRefExpr::VK_PPC_TOCBASE:
375 Type = ELF::R_PPC64_TOC;
377 case MCSymbolRefExpr::VK_None:
378 Type = ELF::R_PPC64_ADDR64;
380 case MCSymbolRefExpr::VK_PPC_DTPMOD:
381 Type = ELF::R_PPC64_DTPMOD64;
383 case MCSymbolRefExpr::VK_TPREL:
384 Type = ELF::R_PPC64_TPREL64;
386 case MCSymbolRefExpr::VK_DTPREL:
387 Type = ELF::R_PPC64_DTPREL64;
392 Type = ELF::R_PPC_ADDR32;
395 Type = ELF::R_PPC_ADDR16;
402 bool PPCELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
403 unsigned Type) const {
408 case ELF::R_PPC_REL24:
409 // If the target symbol has a local entry point, we must keep the
410 // target symbol to preserve that information for the linker.
411 // The "other" values are stored in the last 6 bits of the second byte.
412 // The traditional defines for STO values assume the full byte and thus
413 // the shift to pack it.
414 unsigned Other = cast<MCSymbolELF>(Sym).getOther() << 2;
415 return (Other & ELF::STO_PPC64_LOCAL_MASK) != 0;
419 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_pwrite_stream &OS,
423 MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
424 return createELFObjectWriter(MOTW, OS, IsLittleEndian);