1 //===- PPC64.cpp ----------------------------------------------------------===//
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
9 #include "Symbols.h"
10 #include "SyntheticSections.h"
11 #include "Target.h"
12 #include "Thunks.h"
13 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/Support/Endian.h"
15
16 using namespace llvm;
17 using namespace llvm::object;
18 using namespace llvm::support::endian;
19 using namespace llvm::ELF;
20
21 namespace lld {
22 namespace elf {
23
24 static uint64_t ppc64TocOffset = 0x8000;
25 static uint64_t dynamicThreadPointerOffset = 0x8000;
26
27 // The instruction encoding of bits 21-30 from the ISA for the Xform and Dform
28 // instructions that can be used as part of the initial exec TLS sequence.
29 enum XFormOpcd {
30 LBZX = 87,
31 LHZX = 279,
32 LWZX = 23,
33 LDX = 21,
34 STBX = 215,
35 STHX = 407,
36 STWX = 151,
37 STDX = 149,
38 ADD = 266,
39 };
40
41 enum DFormOpcd {
42 LBZ = 34,
43 LBZU = 35,
44 LHZ = 40,
45 LHZU = 41,
46 LHAU = 43,
47 LWZ = 32,
48 LWZU = 33,
49 LFSU = 49,
50 LD = 58,
51 LFDU = 51,
52 STB = 38,
53 STBU = 39,
54 STH = 44,
55 STHU = 45,
56 STW = 36,
57 STWU = 37,
58 STFSU = 53,
59 STFDU = 55,
60 STD = 62,
61 ADDI = 14
62 };
63
getPPC64TocBase()64 uint64_t getPPC64TocBase() {
65 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
66 // TOC starts where the first of these sections starts. We always create a
67 // .got when we see a relocation that uses it, so for us the start is always
68 // the .got.
69 uint64_t tocVA = in.got->getVA();
70
71 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
72 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
73 // code (crt1.o) assumes that you can get from the TOC base to the
74 // start of the .toc section with only a single (signed) 16-bit relocation.
75 return tocVA + ppc64TocOffset;
76 }
77
getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther)78 unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther) {
79 // The offset is encoded into the 3 most significant bits of the st_other
80 // field, with some special values described in section 3.4.1 of the ABI:
81 // 0 --> Zero offset between the GEP and LEP, and the function does NOT use
82 // the TOC pointer (r2). r2 will hold the same value on returning from
83 // the function as it did on entering the function.
84 // 1 --> Zero offset between the GEP and LEP, and r2 should be treated as a
85 // caller-saved register for all callers.
86 // 2-6 --> The binary logarithm of the offset eg:
87 // 2 --> 2^2 = 4 bytes --> 1 instruction.
88 // 6 --> 2^6 = 64 bytes --> 16 instructions.
89 // 7 --> Reserved.
90 uint8_t gepToLep = (stOther >> 5) & 7;
91 if (gepToLep < 2)
92 return 0;
93
94 // The value encoded in the st_other bits is the
95 // log-base-2(offset).
96 if (gepToLep < 7)
97 return 1 << gepToLep;
98
99 error("reserved value of 7 in the 3 most-significant-bits of st_other");
100 return 0;
101 }
102
isPPC64SmallCodeModelTocReloc(RelType type)103 bool isPPC64SmallCodeModelTocReloc(RelType type) {
104 // The only small code model relocations that access the .toc section.
105 return type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS;
106 }
107
108 // Find the R_PPC64_ADDR64 in .rela.toc with matching offset.
109 template <typename ELFT>
110 static std::pair<Defined *, int64_t>
getRelaTocSymAndAddend(InputSectionBase * tocSec,uint64_t offset)111 getRelaTocSymAndAddend(InputSectionBase *tocSec, uint64_t offset) {
112 if (tocSec->numRelocations == 0)
113 return {};
114
115 // .rela.toc contains exclusively R_PPC64_ADDR64 relocations sorted by
116 // r_offset: 0, 8, 16, etc. For a given Offset, Offset / 8 gives us the
117 // relocation index in most cases.
118 //
119 // In rare cases a TOC entry may store a constant that doesn't need an
120 // R_PPC64_ADDR64, the corresponding r_offset is therefore missing. Offset / 8
121 // points to a relocation with larger r_offset. Do a linear probe then.
122 // Constants are extremely uncommon in .toc and the extra number of array
123 // accesses can be seen as a small constant.
124 ArrayRef<typename ELFT::Rela> relas = tocSec->template relas<ELFT>();
125 uint64_t index = std::min<uint64_t>(offset / 8, relas.size() - 1);
126 for (;;) {
127 if (relas[index].r_offset == offset) {
128 Symbol &sym = tocSec->getFile<ELFT>()->getRelocTargetSym(relas[index]);
129 return {dyn_cast<Defined>(&sym), getAddend<ELFT>(relas[index])};
130 }
131 if (relas[index].r_offset < offset || index == 0)
132 break;
133 --index;
134 }
135 return {};
136 }
137
138 // When accessing a symbol defined in another translation unit, compilers
139 // reserve a .toc entry, allocate a local label and generate toc-indirect
140 // instuctions:
141 //
142 // addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA
143 // ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry
144 // ld/lwa 3, 0(3) # load the value from the address
145 //
146 // .section .toc,"aw",@progbits
147 // .LC0: .tc var[TC],var
148 //
149 // If var is defined, non-preemptable and addressable with a 32-bit signed
150 // offset from the toc base, the address of var can be computed by adding an
151 // offset to the toc base, saving a load.
152 //
153 // addis 3,2,var@toc@ha # this may be relaxed to a nop,
154 // addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc
155 // ld/lwa 3, 0(3) # load the value from the address
156 //
157 // Returns true if the relaxation is performed.
tryRelaxPPC64TocIndirection(RelType type,const Relocation & rel,uint8_t * bufLoc)158 bool tryRelaxPPC64TocIndirection(RelType type, const Relocation &rel,
159 uint8_t *bufLoc) {
160 assert(config->tocOptimize);
161 if (rel.addend < 0)
162 return false;
163
164 // If the symbol is not the .toc section, this isn't a toc-indirection.
165 Defined *defSym = dyn_cast<Defined>(rel.sym);
166 if (!defSym || !defSym->isSection() || defSym->section->name != ".toc")
167 return false;
168
169 Defined *d;
170 int64_t addend;
171 auto *tocISB = cast<InputSectionBase>(defSym->section);
172 std::tie(d, addend) =
173 config->isLE ? getRelaTocSymAndAddend<ELF64LE>(tocISB, rel.addend)
174 : getRelaTocSymAndAddend<ELF64BE>(tocISB, rel.addend);
175
176 // Only non-preemptable defined symbols can be relaxed.
177 if (!d || d->isPreemptible)
178 return false;
179
180 // R_PPC64_ADDR64 should have created a canonical PLT for the non-preemptable
181 // ifunc and changed its type to STT_FUNC.
182 assert(!d->isGnuIFunc());
183
184 // Two instructions can materialize a 32-bit signed offset from the toc base.
185 uint64_t tocRelative = d->getVA(addend) - getPPC64TocBase();
186 if (!isInt<32>(tocRelative))
187 return false;
188
189 // Add PPC64TocOffset that will be subtracted by relocateOne().
190 target->relaxGot(bufLoc, type, tocRelative + ppc64TocOffset);
191 return true;
192 }
193
194 namespace {
195 class PPC64 final : public TargetInfo {
196 public:
197 PPC64();
198 int getTlsGdRelaxSkip(RelType type) const override;
199 uint32_t calcEFlags() const override;
200 RelExpr getRelExpr(RelType type, const Symbol &s,
201 const uint8_t *loc) const override;
202 RelType getDynRel(RelType type) const override;
203 void writePltHeader(uint8_t *buf) const override;
204 void writePlt(uint8_t *buf, const Symbol &sym,
205 uint64_t pltEntryAddr) const override;
206 void writeIplt(uint8_t *buf, const Symbol &sym,
207 uint64_t pltEntryAddr) const override;
208 void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
209 void writeGotHeader(uint8_t *buf) const override;
210 bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
211 uint64_t branchAddr, const Symbol &s,
212 int64_t a) const override;
213 uint32_t getThunkSectionSpacing() const override;
214 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
215 RelExpr adjustRelaxExpr(RelType type, const uint8_t *data,
216 RelExpr expr) const override;
217 void relaxGot(uint8_t *loc, RelType type, uint64_t val) const override;
218 void relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const override;
219 void relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const override;
220 void relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const override;
221 void relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const override;
222
223 bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
224 uint8_t stOther) const override;
225 };
226 } // namespace
227
228 // Relocation masks following the #lo(value), #hi(value), #ha(value),
229 // #higher(value), #highera(value), #highest(value), and #highesta(value)
230 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
231 // document.
lo(uint64_t v)232 static uint16_t lo(uint64_t v) { return v; }
hi(uint64_t v)233 static uint16_t hi(uint64_t v) { return v >> 16; }
ha(uint64_t v)234 static uint16_t ha(uint64_t v) { return (v + 0x8000) >> 16; }
higher(uint64_t v)235 static uint16_t higher(uint64_t v) { return v >> 32; }
highera(uint64_t v)236 static uint16_t highera(uint64_t v) { return (v + 0x8000) >> 32; }
highest(uint64_t v)237 static uint16_t highest(uint64_t v) { return v >> 48; }
highesta(uint64_t v)238 static uint16_t highesta(uint64_t v) { return (v + 0x8000) >> 48; }
239
240 // Extracts the 'PO' field of an instruction encoding.
getPrimaryOpCode(uint32_t encoding)241 static uint8_t getPrimaryOpCode(uint32_t encoding) { return (encoding >> 26); }
242
isDQFormInstruction(uint32_t encoding)243 static bool isDQFormInstruction(uint32_t encoding) {
244 switch (getPrimaryOpCode(encoding)) {
245 default:
246 return false;
247 case 56:
248 // The only instruction with a primary opcode of 56 is `lq`.
249 return true;
250 case 61:
251 // There are both DS and DQ instruction forms with this primary opcode.
252 // Namely `lxv` and `stxv` are the DQ-forms that use it.
253 // The DS 'XO' bits being set to 01 is restricted to DQ form.
254 return (encoding & 3) == 0x1;
255 }
256 }
257
isInstructionUpdateForm(uint32_t encoding)258 static bool isInstructionUpdateForm(uint32_t encoding) {
259 switch (getPrimaryOpCode(encoding)) {
260 default:
261 return false;
262 case LBZU:
263 case LHAU:
264 case LHZU:
265 case LWZU:
266 case LFSU:
267 case LFDU:
268 case STBU:
269 case STHU:
270 case STWU:
271 case STFSU:
272 case STFDU:
273 return true;
274 // LWA has the same opcode as LD, and the DS bits is what differentiates
275 // between LD/LDU/LWA
276 case LD:
277 case STD:
278 return (encoding & 3) == 1;
279 }
280 }
281
282 // There are a number of places when we either want to read or write an
283 // instruction when handling a half16 relocation type. On big-endian the buffer
284 // pointer is pointing into the middle of the word we want to extract, and on
285 // little-endian it is pointing to the start of the word. These 2 helpers are to
286 // simplify reading and writing in that context.
writeFromHalf16(uint8_t * loc,uint32_t insn)287 static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
288 write32(config->isLE ? loc : loc - 2, insn);
289 }
290
readFromHalf16(const uint8_t * loc)291 static uint32_t readFromHalf16(const uint8_t *loc) {
292 return read32(config->isLE ? loc : loc - 2);
293 }
294
PPC64()295 PPC64::PPC64() {
296 copyRel = R_PPC64_COPY;
297 gotRel = R_PPC64_GLOB_DAT;
298 noneRel = R_PPC64_NONE;
299 pltRel = R_PPC64_JMP_SLOT;
300 relativeRel = R_PPC64_RELATIVE;
301 iRelativeRel = R_PPC64_IRELATIVE;
302 symbolicRel = R_PPC64_ADDR64;
303 pltHeaderSize = 60;
304 pltEntrySize = 4;
305 ipltEntrySize = 16; // PPC64PltCallStub::size
306 gotBaseSymInGotPlt = false;
307 gotHeaderEntriesNum = 1;
308 gotPltHeaderEntriesNum = 2;
309 needsThunks = true;
310
311 tlsModuleIndexRel = R_PPC64_DTPMOD64;
312 tlsOffsetRel = R_PPC64_DTPREL64;
313
314 tlsGotRel = R_PPC64_TPREL64;
315
316 needsMoreStackNonSplit = false;
317
318 // We need 64K pages (at least under glibc/Linux, the loader won't
319 // set different permissions on a finer granularity than that).
320 defaultMaxPageSize = 65536;
321
322 // The PPC64 ELF ABI v1 spec, says:
323 //
324 // It is normally desirable to put segments with different characteristics
325 // in separate 256 Mbyte portions of the address space, to give the
326 // operating system full paging flexibility in the 64-bit address space.
327 //
328 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
329 // use 0x10000000 as the starting address.
330 defaultImageBase = 0x10000000;
331
332 write32(trapInstr.data(), 0x7fe00008);
333 }
334
getTlsGdRelaxSkip(RelType type) const335 int PPC64::getTlsGdRelaxSkip(RelType type) const {
336 // A __tls_get_addr call instruction is marked with 2 relocations:
337 //
338 // R_PPC64_TLSGD / R_PPC64_TLSLD: marker relocation
339 // R_PPC64_REL24: __tls_get_addr
340 //
341 // After the relaxation we no longer call __tls_get_addr and should skip both
342 // relocations to not create a false dependence on __tls_get_addr being
343 // defined.
344 if (type == R_PPC64_TLSGD || type == R_PPC64_TLSLD)
345 return 2;
346 return 1;
347 }
348
getEFlags(InputFile * file)349 static uint32_t getEFlags(InputFile *file) {
350 if (config->ekind == ELF64BEKind)
351 return cast<ObjFile<ELF64BE>>(file)->getObj().getHeader()->e_flags;
352 return cast<ObjFile<ELF64LE>>(file)->getObj().getHeader()->e_flags;
353 }
354
355 // This file implements v2 ABI. This function makes sure that all
356 // object files have v2 or an unspecified version as an ABI version.
calcEFlags() const357 uint32_t PPC64::calcEFlags() const {
358 for (InputFile *f : objectFiles) {
359 uint32_t flag = getEFlags(f);
360 if (flag == 1)
361 error(toString(f) + ": ABI version 1 is not supported");
362 else if (flag > 2)
363 error(toString(f) + ": unrecognized e_flags: " + Twine(flag));
364 }
365 return 2;
366 }
367
relaxGot(uint8_t * loc,RelType type,uint64_t val) const368 void PPC64::relaxGot(uint8_t *loc, RelType type, uint64_t val) const {
369 switch (type) {
370 case R_PPC64_TOC16_HA:
371 // Convert "addis reg, 2, .LC0@toc@h" to "addis reg, 2, var@toc@h" or "nop".
372 relocateOne(loc, type, val);
373 break;
374 case R_PPC64_TOC16_LO_DS: {
375 // Convert "ld reg, .LC0@toc@l(reg)" to "addi reg, reg, var@toc@l" or
376 // "addi reg, 2, var@toc".
377 uint32_t insn = readFromHalf16(loc);
378 if (getPrimaryOpCode(insn) != LD)
379 error("expected a 'ld' for got-indirect to toc-relative relaxing");
380 writeFromHalf16(loc, (insn & 0x03ffffff) | 0x38000000);
381 relocateOne(loc, R_PPC64_TOC16_LO, val);
382 break;
383 }
384 default:
385 llvm_unreachable("unexpected relocation type");
386 }
387 }
388
relaxTlsGdToLe(uint8_t * loc,RelType type,uint64_t val) const389 void PPC64::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const {
390 // Reference: 3.7.4.2 of the 64-bit ELF V2 abi supplement.
391 // The general dynamic code sequence for a global `x` will look like:
392 // Instruction Relocation Symbol
393 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
394 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x
395 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
396 // R_PPC64_REL24 __tls_get_addr
397 // nop None None
398
399 // Relaxing to local exec entails converting:
400 // addis r3, r2, x@got@tlsgd@ha into nop
401 // addi r3, r3, x@got@tlsgd@l into addis r3, r13, x@tprel@ha
402 // bl __tls_get_addr(x@tlsgd) into nop
403 // nop into addi r3, r3, x@tprel@l
404
405 switch (type) {
406 case R_PPC64_GOT_TLSGD16_HA:
407 writeFromHalf16(loc, 0x60000000); // nop
408 break;
409 case R_PPC64_GOT_TLSGD16:
410 case R_PPC64_GOT_TLSGD16_LO:
411 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13
412 relocateOne(loc, R_PPC64_TPREL16_HA, val);
413 break;
414 case R_PPC64_TLSGD:
415 write32(loc, 0x60000000); // nop
416 write32(loc + 4, 0x38630000); // addi r3, r3
417 // Since we are relocating a half16 type relocation and Loc + 4 points to
418 // the start of an instruction we need to advance the buffer by an extra
419 // 2 bytes on BE.
420 relocateOne(loc + 4 + (config->ekind == ELF64BEKind ? 2 : 0),
421 R_PPC64_TPREL16_LO, val);
422 break;
423 default:
424 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
425 }
426 }
427
relaxTlsLdToLe(uint8_t * loc,RelType type,uint64_t val) const428 void PPC64::relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const {
429 // Reference: 3.7.4.3 of the 64-bit ELF V2 abi supplement.
430 // The local dynamic code sequence for a global `x` will look like:
431 // Instruction Relocation Symbol
432 // addis r3, r2, x@got@tlsld@ha R_PPC64_GOT_TLSLD16_HA x
433 // addi r3, r3, x@got@tlsld@l R_PPC64_GOT_TLSLD16_LO x
434 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSLD x
435 // R_PPC64_REL24 __tls_get_addr
436 // nop None None
437
438 // Relaxing to local exec entails converting:
439 // addis r3, r2, x@got@tlsld@ha into nop
440 // addi r3, r3, x@got@tlsld@l into addis r3, r13, 0
441 // bl __tls_get_addr(x@tlsgd) into nop
442 // nop into addi r3, r3, 4096
443
444 switch (type) {
445 case R_PPC64_GOT_TLSLD16_HA:
446 writeFromHalf16(loc, 0x60000000); // nop
447 break;
448 case R_PPC64_GOT_TLSLD16_LO:
449 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13, 0
450 break;
451 case R_PPC64_TLSLD:
452 write32(loc, 0x60000000); // nop
453 write32(loc + 4, 0x38631000); // addi r3, r3, 4096
454 break;
455 case R_PPC64_DTPREL16:
456 case R_PPC64_DTPREL16_HA:
457 case R_PPC64_DTPREL16_HI:
458 case R_PPC64_DTPREL16_DS:
459 case R_PPC64_DTPREL16_LO:
460 case R_PPC64_DTPREL16_LO_DS:
461 relocateOne(loc, type, val);
462 break;
463 default:
464 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
465 }
466 }
467
getPPCDFormOp(unsigned secondaryOp)468 unsigned getPPCDFormOp(unsigned secondaryOp) {
469 switch (secondaryOp) {
470 case LBZX:
471 return LBZ;
472 case LHZX:
473 return LHZ;
474 case LWZX:
475 return LWZ;
476 case LDX:
477 return LD;
478 case STBX:
479 return STB;
480 case STHX:
481 return STH;
482 case STWX:
483 return STW;
484 case STDX:
485 return STD;
486 case ADD:
487 return ADDI;
488 default:
489 return 0;
490 }
491 }
492
relaxTlsIeToLe(uint8_t * loc,RelType type,uint64_t val) const493 void PPC64::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const {
494 // The initial exec code sequence for a global `x` will look like:
495 // Instruction Relocation Symbol
496 // addis r9, r2, x@got@tprel@ha R_PPC64_GOT_TPREL16_HA x
497 // ld r9, x@got@tprel@l(r9) R_PPC64_GOT_TPREL16_LO_DS x
498 // add r9, r9, x@tls R_PPC64_TLS x
499
500 // Relaxing to local exec entails converting:
501 // addis r9, r2, x@got@tprel@ha into nop
502 // ld r9, x@got@tprel@l(r9) into addis r9, r13, x@tprel@ha
503 // add r9, r9, x@tls into addi r9, r9, x@tprel@l
504
505 // x@tls R_PPC64_TLS is a relocation which does not compute anything,
506 // it is replaced with r13 (thread pointer).
507
508 // The add instruction in the initial exec sequence has multiple variations
509 // that need to be handled. If we are building an address it will use an add
510 // instruction, if we are accessing memory it will use any of the X-form
511 // indexed load or store instructions.
512
513 unsigned offset = (config->ekind == ELF64BEKind) ? 2 : 0;
514 switch (type) {
515 case R_PPC64_GOT_TPREL16_HA:
516 write32(loc - offset, 0x60000000); // nop
517 break;
518 case R_PPC64_GOT_TPREL16_LO_DS:
519 case R_PPC64_GOT_TPREL16_DS: {
520 uint32_t regNo = read32(loc - offset) & 0x03E00000; // bits 6-10
521 write32(loc - offset, 0x3C0D0000 | regNo); // addis RegNo, r13
522 relocateOne(loc, R_PPC64_TPREL16_HA, val);
523 break;
524 }
525 case R_PPC64_TLS: {
526 uint32_t primaryOp = getPrimaryOpCode(read32(loc));
527 if (primaryOp != 31)
528 error("unrecognized instruction for IE to LE R_PPC64_TLS");
529 uint32_t secondaryOp = (read32(loc) & 0x000007FE) >> 1; // bits 21-30
530 uint32_t dFormOp = getPPCDFormOp(secondaryOp);
531 if (dFormOp == 0)
532 error("unrecognized instruction for IE to LE R_PPC64_TLS");
533 write32(loc, ((dFormOp << 26) | (read32(loc) & 0x03FFFFFF)));
534 relocateOne(loc + offset, R_PPC64_TPREL16_LO, val);
535 break;
536 }
537 default:
538 llvm_unreachable("unknown relocation for IE to LE");
539 break;
540 }
541 }
542
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const543 RelExpr PPC64::getRelExpr(RelType type, const Symbol &s,
544 const uint8_t *loc) const {
545 switch (type) {
546 case R_PPC64_NONE:
547 return R_NONE;
548 case R_PPC64_ADDR16:
549 case R_PPC64_ADDR16_DS:
550 case R_PPC64_ADDR16_HA:
551 case R_PPC64_ADDR16_HI:
552 case R_PPC64_ADDR16_HIGHER:
553 case R_PPC64_ADDR16_HIGHERA:
554 case R_PPC64_ADDR16_HIGHEST:
555 case R_PPC64_ADDR16_HIGHESTA:
556 case R_PPC64_ADDR16_LO:
557 case R_PPC64_ADDR16_LO_DS:
558 case R_PPC64_ADDR32:
559 case R_PPC64_ADDR64:
560 return R_ABS;
561 case R_PPC64_GOT16:
562 case R_PPC64_GOT16_DS:
563 case R_PPC64_GOT16_HA:
564 case R_PPC64_GOT16_HI:
565 case R_PPC64_GOT16_LO:
566 case R_PPC64_GOT16_LO_DS:
567 return R_GOT_OFF;
568 case R_PPC64_TOC16:
569 case R_PPC64_TOC16_DS:
570 case R_PPC64_TOC16_HI:
571 case R_PPC64_TOC16_LO:
572 return R_GOTREL;
573 case R_PPC64_TOC16_HA:
574 case R_PPC64_TOC16_LO_DS:
575 return config->tocOptimize ? R_PPC64_RELAX_TOC : R_GOTREL;
576 case R_PPC64_TOC:
577 return R_PPC64_TOCBASE;
578 case R_PPC64_REL14:
579 case R_PPC64_REL24:
580 return R_PPC64_CALL_PLT;
581 case R_PPC64_REL16_LO:
582 case R_PPC64_REL16_HA:
583 case R_PPC64_REL16_HI:
584 case R_PPC64_REL32:
585 case R_PPC64_REL64:
586 return R_PC;
587 case R_PPC64_GOT_TLSGD16:
588 case R_PPC64_GOT_TLSGD16_HA:
589 case R_PPC64_GOT_TLSGD16_HI:
590 case R_PPC64_GOT_TLSGD16_LO:
591 return R_TLSGD_GOT;
592 case R_PPC64_GOT_TLSLD16:
593 case R_PPC64_GOT_TLSLD16_HA:
594 case R_PPC64_GOT_TLSLD16_HI:
595 case R_PPC64_GOT_TLSLD16_LO:
596 return R_TLSLD_GOT;
597 case R_PPC64_GOT_TPREL16_HA:
598 case R_PPC64_GOT_TPREL16_LO_DS:
599 case R_PPC64_GOT_TPREL16_DS:
600 case R_PPC64_GOT_TPREL16_HI:
601 return R_GOT_OFF;
602 case R_PPC64_GOT_DTPREL16_HA:
603 case R_PPC64_GOT_DTPREL16_LO_DS:
604 case R_PPC64_GOT_DTPREL16_DS:
605 case R_PPC64_GOT_DTPREL16_HI:
606 return R_TLSLD_GOT_OFF;
607 case R_PPC64_TPREL16:
608 case R_PPC64_TPREL16_HA:
609 case R_PPC64_TPREL16_LO:
610 case R_PPC64_TPREL16_HI:
611 case R_PPC64_TPREL16_DS:
612 case R_PPC64_TPREL16_LO_DS:
613 case R_PPC64_TPREL16_HIGHER:
614 case R_PPC64_TPREL16_HIGHERA:
615 case R_PPC64_TPREL16_HIGHEST:
616 case R_PPC64_TPREL16_HIGHESTA:
617 return R_TLS;
618 case R_PPC64_DTPREL16:
619 case R_PPC64_DTPREL16_DS:
620 case R_PPC64_DTPREL16_HA:
621 case R_PPC64_DTPREL16_HI:
622 case R_PPC64_DTPREL16_HIGHER:
623 case R_PPC64_DTPREL16_HIGHERA:
624 case R_PPC64_DTPREL16_HIGHEST:
625 case R_PPC64_DTPREL16_HIGHESTA:
626 case R_PPC64_DTPREL16_LO:
627 case R_PPC64_DTPREL16_LO_DS:
628 case R_PPC64_DTPREL64:
629 return R_DTPREL;
630 case R_PPC64_TLSGD:
631 return R_TLSDESC_CALL;
632 case R_PPC64_TLSLD:
633 return R_TLSLD_HINT;
634 case R_PPC64_TLS:
635 return R_TLSIE_HINT;
636 default:
637 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
638 ") against symbol " + toString(s));
639 return R_NONE;
640 }
641 }
642
getDynRel(RelType type) const643 RelType PPC64::getDynRel(RelType type) const {
644 if (type == R_PPC64_ADDR64 || type == R_PPC64_TOC)
645 return R_PPC64_ADDR64;
646 return R_PPC64_NONE;
647 }
648
writeGotHeader(uint8_t * buf) const649 void PPC64::writeGotHeader(uint8_t *buf) const {
650 write64(buf, getPPC64TocBase());
651 }
652
writePltHeader(uint8_t * buf) const653 void PPC64::writePltHeader(uint8_t *buf) const {
654 // The generic resolver stub goes first.
655 write32(buf + 0, 0x7c0802a6); // mflr r0
656 write32(buf + 4, 0x429f0005); // bcl 20,4*cr7+so,8 <_glink+0x8>
657 write32(buf + 8, 0x7d6802a6); // mflr r11
658 write32(buf + 12, 0x7c0803a6); // mtlr r0
659 write32(buf + 16, 0x7d8b6050); // subf r12, r11, r12
660 write32(buf + 20, 0x380cffcc); // subi r0,r12,52
661 write32(buf + 24, 0x7800f082); // srdi r0,r0,62,2
662 write32(buf + 28, 0xe98b002c); // ld r12,44(r11)
663 write32(buf + 32, 0x7d6c5a14); // add r11,r12,r11
664 write32(buf + 36, 0xe98b0000); // ld r12,0(r11)
665 write32(buf + 40, 0xe96b0008); // ld r11,8(r11)
666 write32(buf + 44, 0x7d8903a6); // mtctr r12
667 write32(buf + 48, 0x4e800420); // bctr
668
669 // The 'bcl' instruction will set the link register to the address of the
670 // following instruction ('mflr r11'). Here we store the offset from that
671 // instruction to the first entry in the GotPlt section.
672 int64_t gotPltOffset = in.gotPlt->getVA() - (in.plt->getVA() + 8);
673 write64(buf + 52, gotPltOffset);
674 }
675
writePlt(uint8_t * buf,const Symbol & sym,uint64_t) const676 void PPC64::writePlt(uint8_t *buf, const Symbol &sym,
677 uint64_t /*pltEntryAddr*/) const {
678 int32_t offset = pltHeaderSize + sym.pltIndex * pltEntrySize;
679 // bl __glink_PLTresolve
680 write32(buf, 0x48000000 | ((-offset) & 0x03FFFFFc));
681 }
682
writeIplt(uint8_t * buf,const Symbol & sym,uint64_t) const683 void PPC64::writeIplt(uint8_t *buf, const Symbol &sym,
684 uint64_t /*pltEntryAddr*/) const {
685 writePPC64LoadAndBranch(buf, sym.getGotPltVA() - getPPC64TocBase());
686 }
687
toAddr16Rel(RelType type,uint64_t val)688 static std::pair<RelType, uint64_t> toAddr16Rel(RelType type, uint64_t val) {
689 // Relocations relative to the toc-base need to be adjusted by the Toc offset.
690 uint64_t tocBiasedVal = val - ppc64TocOffset;
691 // Relocations relative to dtv[dtpmod] need to be adjusted by the DTP offset.
692 uint64_t dtpBiasedVal = val - dynamicThreadPointerOffset;
693
694 switch (type) {
695 // TOC biased relocation.
696 case R_PPC64_GOT16:
697 case R_PPC64_GOT_TLSGD16:
698 case R_PPC64_GOT_TLSLD16:
699 case R_PPC64_TOC16:
700 return {R_PPC64_ADDR16, tocBiasedVal};
701 case R_PPC64_GOT16_DS:
702 case R_PPC64_TOC16_DS:
703 case R_PPC64_GOT_TPREL16_DS:
704 case R_PPC64_GOT_DTPREL16_DS:
705 return {R_PPC64_ADDR16_DS, tocBiasedVal};
706 case R_PPC64_GOT16_HA:
707 case R_PPC64_GOT_TLSGD16_HA:
708 case R_PPC64_GOT_TLSLD16_HA:
709 case R_PPC64_GOT_TPREL16_HA:
710 case R_PPC64_GOT_DTPREL16_HA:
711 case R_PPC64_TOC16_HA:
712 return {R_PPC64_ADDR16_HA, tocBiasedVal};
713 case R_PPC64_GOT16_HI:
714 case R_PPC64_GOT_TLSGD16_HI:
715 case R_PPC64_GOT_TLSLD16_HI:
716 case R_PPC64_GOT_TPREL16_HI:
717 case R_PPC64_GOT_DTPREL16_HI:
718 case R_PPC64_TOC16_HI:
719 return {R_PPC64_ADDR16_HI, tocBiasedVal};
720 case R_PPC64_GOT16_LO:
721 case R_PPC64_GOT_TLSGD16_LO:
722 case R_PPC64_GOT_TLSLD16_LO:
723 case R_PPC64_TOC16_LO:
724 return {R_PPC64_ADDR16_LO, tocBiasedVal};
725 case R_PPC64_GOT16_LO_DS:
726 case R_PPC64_TOC16_LO_DS:
727 case R_PPC64_GOT_TPREL16_LO_DS:
728 case R_PPC64_GOT_DTPREL16_LO_DS:
729 return {R_PPC64_ADDR16_LO_DS, tocBiasedVal};
730
731 // Dynamic Thread pointer biased relocation types.
732 case R_PPC64_DTPREL16:
733 return {R_PPC64_ADDR16, dtpBiasedVal};
734 case R_PPC64_DTPREL16_DS:
735 return {R_PPC64_ADDR16_DS, dtpBiasedVal};
736 case R_PPC64_DTPREL16_HA:
737 return {R_PPC64_ADDR16_HA, dtpBiasedVal};
738 case R_PPC64_DTPREL16_HI:
739 return {R_PPC64_ADDR16_HI, dtpBiasedVal};
740 case R_PPC64_DTPREL16_HIGHER:
741 return {R_PPC64_ADDR16_HIGHER, dtpBiasedVal};
742 case R_PPC64_DTPREL16_HIGHERA:
743 return {R_PPC64_ADDR16_HIGHERA, dtpBiasedVal};
744 case R_PPC64_DTPREL16_HIGHEST:
745 return {R_PPC64_ADDR16_HIGHEST, dtpBiasedVal};
746 case R_PPC64_DTPREL16_HIGHESTA:
747 return {R_PPC64_ADDR16_HIGHESTA, dtpBiasedVal};
748 case R_PPC64_DTPREL16_LO:
749 return {R_PPC64_ADDR16_LO, dtpBiasedVal};
750 case R_PPC64_DTPREL16_LO_DS:
751 return {R_PPC64_ADDR16_LO_DS, dtpBiasedVal};
752 case R_PPC64_DTPREL64:
753 return {R_PPC64_ADDR64, dtpBiasedVal};
754
755 default:
756 return {type, val};
757 }
758 }
759
isTocOptType(RelType type)760 static bool isTocOptType(RelType type) {
761 switch (type) {
762 case R_PPC64_GOT16_HA:
763 case R_PPC64_GOT16_LO_DS:
764 case R_PPC64_TOC16_HA:
765 case R_PPC64_TOC16_LO_DS:
766 case R_PPC64_TOC16_LO:
767 return true;
768 default:
769 return false;
770 }
771 }
772
relocateOne(uint8_t * loc,RelType type,uint64_t val) const773 void PPC64::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
774 // We need to save the original relocation type to use in diagnostics, and
775 // use the original type to determine if we should toc-optimize the
776 // instructions being relocated.
777 RelType originalType = type;
778 bool shouldTocOptimize = isTocOptType(type);
779 // For dynamic thread pointer relative, toc-relative, and got-indirect
780 // relocations, proceed in terms of the corresponding ADDR16 relocation type.
781 std::tie(type, val) = toAddr16Rel(type, val);
782
783 switch (type) {
784 case R_PPC64_ADDR14: {
785 checkAlignment(loc, val, 4, type);
786 // Preserve the AA/LK bits in the branch instruction
787 uint8_t aalk = loc[3];
788 write16(loc + 2, (aalk & 3) | (val & 0xfffc));
789 break;
790 }
791 case R_PPC64_ADDR16:
792 checkIntUInt(loc, val, 16, originalType);
793 write16(loc, val);
794 break;
795 case R_PPC64_ADDR32:
796 checkIntUInt(loc, val, 32, originalType);
797 write32(loc, val);
798 break;
799 case R_PPC64_ADDR16_DS:
800 case R_PPC64_TPREL16_DS: {
801 checkInt(loc, val, 16, originalType);
802 // DQ-form instructions use bits 28-31 as part of the instruction encoding
803 // DS-form instructions only use bits 30-31.
804 uint16_t mask = isDQFormInstruction(readFromHalf16(loc)) ? 0xf : 0x3;
805 checkAlignment(loc, lo(val), mask + 1, originalType);
806 write16(loc, (read16(loc) & mask) | lo(val));
807 } break;
808 case R_PPC64_ADDR16_HA:
809 case R_PPC64_REL16_HA:
810 case R_PPC64_TPREL16_HA:
811 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0)
812 writeFromHalf16(loc, 0x60000000);
813 else
814 write16(loc, ha(val));
815 break;
816 case R_PPC64_ADDR16_HI:
817 case R_PPC64_REL16_HI:
818 case R_PPC64_TPREL16_HI:
819 write16(loc, hi(val));
820 break;
821 case R_PPC64_ADDR16_HIGHER:
822 case R_PPC64_TPREL16_HIGHER:
823 write16(loc, higher(val));
824 break;
825 case R_PPC64_ADDR16_HIGHERA:
826 case R_PPC64_TPREL16_HIGHERA:
827 write16(loc, highera(val));
828 break;
829 case R_PPC64_ADDR16_HIGHEST:
830 case R_PPC64_TPREL16_HIGHEST:
831 write16(loc, highest(val));
832 break;
833 case R_PPC64_ADDR16_HIGHESTA:
834 case R_PPC64_TPREL16_HIGHESTA:
835 write16(loc, highesta(val));
836 break;
837 case R_PPC64_ADDR16_LO:
838 case R_PPC64_REL16_LO:
839 case R_PPC64_TPREL16_LO:
840 // When the high-adjusted part of a toc relocation evaluates to 0, it is
841 // changed into a nop. The lo part then needs to be updated to use the
842 // toc-pointer register r2, as the base register.
843 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) {
844 uint32_t insn = readFromHalf16(loc);
845 if (isInstructionUpdateForm(insn))
846 error(getErrorLocation(loc) +
847 "can't toc-optimize an update instruction: 0x" +
848 utohexstr(insn));
849 writeFromHalf16(loc, (insn & 0xffe00000) | 0x00020000 | lo(val));
850 } else {
851 write16(loc, lo(val));
852 }
853 break;
854 case R_PPC64_ADDR16_LO_DS:
855 case R_PPC64_TPREL16_LO_DS: {
856 // DQ-form instructions use bits 28-31 as part of the instruction encoding
857 // DS-form instructions only use bits 30-31.
858 uint32_t insn = readFromHalf16(loc);
859 uint16_t mask = isDQFormInstruction(insn) ? 0xf : 0x3;
860 checkAlignment(loc, lo(val), mask + 1, originalType);
861 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) {
862 // When the high-adjusted part of a toc relocation evaluates to 0, it is
863 // changed into a nop. The lo part then needs to be updated to use the toc
864 // pointer register r2, as the base register.
865 if (isInstructionUpdateForm(insn))
866 error(getErrorLocation(loc) +
867 "Can't toc-optimize an update instruction: 0x" +
868 Twine::utohexstr(insn));
869 insn &= 0xffe00000 | mask;
870 writeFromHalf16(loc, insn | 0x00020000 | lo(val));
871 } else {
872 write16(loc, (read16(loc) & mask) | lo(val));
873 }
874 } break;
875 case R_PPC64_TPREL16:
876 checkInt(loc, val, 16, originalType);
877 write16(loc, val);
878 break;
879 case R_PPC64_REL32:
880 checkInt(loc, val, 32, type);
881 write32(loc, val);
882 break;
883 case R_PPC64_ADDR64:
884 case R_PPC64_REL64:
885 case R_PPC64_TOC:
886 write64(loc, val);
887 break;
888 case R_PPC64_REL14: {
889 uint32_t mask = 0x0000FFFC;
890 checkInt(loc, val, 16, type);
891 checkAlignment(loc, val, 4, type);
892 write32(loc, (read32(loc) & ~mask) | (val & mask));
893 break;
894 }
895 case R_PPC64_REL24: {
896 uint32_t mask = 0x03FFFFFC;
897 checkInt(loc, val, 26, type);
898 checkAlignment(loc, val, 4, type);
899 write32(loc, (read32(loc) & ~mask) | (val & mask));
900 break;
901 }
902 case R_PPC64_DTPREL64:
903 write64(loc, val - dynamicThreadPointerOffset);
904 break;
905 default:
906 llvm_unreachable("unknown relocation");
907 }
908 }
909
needsThunk(RelExpr expr,RelType type,const InputFile * file,uint64_t branchAddr,const Symbol & s,int64_t a) const910 bool PPC64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
911 uint64_t branchAddr, const Symbol &s, int64_t a) const {
912 if (type != R_PPC64_REL14 && type != R_PPC64_REL24)
913 return false;
914
915 // If a function is in the Plt it needs to be called with a call-stub.
916 if (s.isInPlt())
917 return true;
918
919 // If a symbol is a weak undefined and we are compiling an executable
920 // it doesn't need a range-extending thunk since it can't be called.
921 if (s.isUndefWeak() && !config->shared)
922 return false;
923
924 // If the offset exceeds the range of the branch type then it will need
925 // a range-extending thunk.
926 // See the comment in getRelocTargetVA() about R_PPC64_CALL.
927 return !inBranchRange(type, branchAddr,
928 s.getVA(a) +
929 getPPC64GlobalEntryToLocalEntryOffset(s.stOther));
930 }
931
getThunkSectionSpacing() const932 uint32_t PPC64::getThunkSectionSpacing() const {
933 // See comment in Arch/ARM.cpp for a more detailed explanation of
934 // getThunkSectionSpacing(). For PPC64 we pick the constant here based on
935 // R_PPC64_REL24, which is used by unconditional branch instructions.
936 // 0x2000000 = (1 << 24-1) * 4
937 return 0x2000000;
938 }
939
inBranchRange(RelType type,uint64_t src,uint64_t dst) const940 bool PPC64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
941 int64_t offset = dst - src;
942 if (type == R_PPC64_REL14)
943 return isInt<16>(offset);
944 if (type == R_PPC64_REL24)
945 return isInt<26>(offset);
946 llvm_unreachable("unsupported relocation type used in branch");
947 }
948
adjustRelaxExpr(RelType type,const uint8_t * data,RelExpr expr) const949 RelExpr PPC64::adjustRelaxExpr(RelType type, const uint8_t *data,
950 RelExpr expr) const {
951 if (expr == R_RELAX_TLS_GD_TO_IE)
952 return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
953 if (expr == R_RELAX_TLS_LD_TO_LE)
954 return R_RELAX_TLS_LD_TO_LE_ABS;
955 return expr;
956 }
957
958 // Reference: 3.7.4.1 of the 64-bit ELF V2 abi supplement.
959 // The general dynamic code sequence for a global `x` uses 4 instructions.
960 // Instruction Relocation Symbol
961 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
962 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x
963 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
964 // R_PPC64_REL24 __tls_get_addr
965 // nop None None
966 //
967 // Relaxing to initial-exec entails:
968 // 1) Convert the addis/addi pair that builds the address of the tls_index
969 // struct for 'x' to an addis/ld pair that loads an offset from a got-entry.
970 // 2) Convert the call to __tls_get_addr to a nop.
971 // 3) Convert the nop following the call to an add of the loaded offset to the
972 // thread pointer.
973 // Since the nop must directly follow the call, the R_PPC64_TLSGD relocation is
974 // used as the relaxation hint for both steps 2 and 3.
relaxTlsGdToIe(uint8_t * loc,RelType type,uint64_t val) const975 void PPC64::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const {
976 switch (type) {
977 case R_PPC64_GOT_TLSGD16_HA:
978 // This is relaxed from addis rT, r2, sym@got@tlsgd@ha to
979 // addis rT, r2, sym@got@tprel@ha.
980 relocateOne(loc, R_PPC64_GOT_TPREL16_HA, val);
981 return;
982 case R_PPC64_GOT_TLSGD16:
983 case R_PPC64_GOT_TLSGD16_LO: {
984 // Relax from addi r3, rA, sym@got@tlsgd@l to
985 // ld r3, sym@got@tprel@l(rA)
986 uint32_t ra = (readFromHalf16(loc) & (0x1f << 16));
987 writeFromHalf16(loc, 0xe8600000 | ra);
988 relocateOne(loc, R_PPC64_GOT_TPREL16_LO_DS, val);
989 return;
990 }
991 case R_PPC64_TLSGD:
992 write32(loc, 0x60000000); // bl __tls_get_addr(sym@tlsgd) --> nop
993 write32(loc + 4, 0x7c636A14); // nop --> add r3, r3, r13
994 return;
995 default:
996 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
997 }
998 }
999
1000 // The prologue for a split-stack function is expected to look roughly
1001 // like this:
1002 // .Lglobal_entry_point:
1003 // # TOC pointer initialization.
1004 // ...
1005 // .Llocal_entry_point:
1006 // # load the __private_ss member of the threads tcbhead.
1007 // ld r0,-0x7000-64(r13)
1008 // # subtract the functions stack size from the stack pointer.
1009 // addis r12, r1, ha(-stack-frame size)
1010 // addi r12, r12, l(-stack-frame size)
1011 // # compare needed to actual and branch to allocate_more_stack if more
1012 // # space is needed, otherwise fallthrough to 'normal' function body.
1013 // cmpld cr7,r12,r0
1014 // blt- cr7, .Lallocate_more_stack
1015 //
1016 // -) The allocate_more_stack block might be placed after the split-stack
1017 // prologue and the `blt-` replaced with a `bge+ .Lnormal_func_body`
1018 // instead.
1019 // -) If either the addis or addi is not needed due to the stack size being
1020 // smaller then 32K or a multiple of 64K they will be replaced with a nop,
1021 // but there will always be 2 instructions the linker can overwrite for the
1022 // adjusted stack size.
1023 //
1024 // The linkers job here is to increase the stack size used in the addis/addi
1025 // pair by split-stack-size-adjust.
1026 // addis r12, r1, ha(-stack-frame size - split-stack-adjust-size)
1027 // addi r12, r12, l(-stack-frame size - split-stack-adjust-size)
adjustPrologueForCrossSplitStack(uint8_t * loc,uint8_t * end,uint8_t stOther) const1028 bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
1029 uint8_t stOther) const {
1030 // If the caller has a global entry point adjust the buffer past it. The start
1031 // of the split-stack prologue will be at the local entry point.
1032 loc += getPPC64GlobalEntryToLocalEntryOffset(stOther);
1033
1034 // At the very least we expect to see a load of some split-stack data from the
1035 // tcb, and 2 instructions that calculate the ending stack address this
1036 // function will require. If there is not enough room for at least 3
1037 // instructions it can't be a split-stack prologue.
1038 if (loc + 12 >= end)
1039 return false;
1040
1041 // First instruction must be `ld r0, -0x7000-64(r13)`
1042 if (read32(loc) != 0xe80d8fc0)
1043 return false;
1044
1045 int16_t hiImm = 0;
1046 int16_t loImm = 0;
1047 // First instruction can be either an addis if the frame size is larger then
1048 // 32K, or an addi if the size is less then 32K.
1049 int32_t firstInstr = read32(loc + 4);
1050 if (getPrimaryOpCode(firstInstr) == 15) {
1051 hiImm = firstInstr & 0xFFFF;
1052 } else if (getPrimaryOpCode(firstInstr) == 14) {
1053 loImm = firstInstr & 0xFFFF;
1054 } else {
1055 return false;
1056 }
1057
1058 // Second instruction is either an addi or a nop. If the first instruction was
1059 // an addi then LoImm is set and the second instruction must be a nop.
1060 uint32_t secondInstr = read32(loc + 8);
1061 if (!loImm && getPrimaryOpCode(secondInstr) == 14) {
1062 loImm = secondInstr & 0xFFFF;
1063 } else if (secondInstr != 0x60000000) {
1064 return false;
1065 }
1066
1067 // The register operands of the first instruction should be the stack-pointer
1068 // (r1) as the input (RA) and r12 as the output (RT). If the second
1069 // instruction is not a nop, then it should use r12 as both input and output.
1070 auto checkRegOperands = [](uint32_t instr, uint8_t expectedRT,
1071 uint8_t expectedRA) {
1072 return ((instr & 0x3E00000) >> 21 == expectedRT) &&
1073 ((instr & 0x1F0000) >> 16 == expectedRA);
1074 };
1075 if (!checkRegOperands(firstInstr, 12, 1))
1076 return false;
1077 if (secondInstr != 0x60000000 && !checkRegOperands(secondInstr, 12, 12))
1078 return false;
1079
1080 int32_t stackFrameSize = (hiImm * 65536) + loImm;
1081 // Check that the adjusted size doesn't overflow what we can represent with 2
1082 // instructions.
1083 if (stackFrameSize < config->splitStackAdjustSize + INT32_MIN) {
1084 error(getErrorLocation(loc) + "split-stack prologue adjustment overflows");
1085 return false;
1086 }
1087
1088 int32_t adjustedStackFrameSize =
1089 stackFrameSize - config->splitStackAdjustSize;
1090
1091 loImm = adjustedStackFrameSize & 0xFFFF;
1092 hiImm = (adjustedStackFrameSize + 0x8000) >> 16;
1093 if (hiImm) {
1094 write32(loc + 4, 0x3D810000 | (uint16_t)hiImm);
1095 // If the low immediate is zero the second instruction will be a nop.
1096 secondInstr = loImm ? 0x398C0000 | (uint16_t)loImm : 0x60000000;
1097 write32(loc + 8, secondInstr);
1098 } else {
1099 // addi r12, r1, imm
1100 write32(loc + 4, (0x39810000) | (uint16_t)loImm);
1101 write32(loc + 8, 0x60000000);
1102 }
1103
1104 return true;
1105 }
1106
getPPC64TargetInfo()1107 TargetInfo *getPPC64TargetInfo() {
1108 static PPC64 target;
1109 return ⌖
1110 }
1111
1112 } // namespace elf
1113 } // namespace lld
1114