1 //===- SystemZ.cpp --------------------------------------------------------===//
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 "OutputSections.h"
11 #include "SyntheticSections.h"
13 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/Support/Endian.h"
18 using namespace llvm::support::endian
;
19 using namespace llvm::ELF
;
21 using namespace lld::elf
;
24 class SystemZ
: public TargetInfo
{
27 int getTlsGdRelaxSkip(RelType type
) const override
;
28 RelExpr
getRelExpr(RelType type
, const Symbol
&s
,
29 const uint8_t *loc
) const override
;
30 RelType
getDynRel(RelType type
) const override
;
31 void writeGotHeader(uint8_t *buf
) const override
;
32 void writeGotPlt(uint8_t *buf
, const Symbol
&s
) const override
;
33 void writeIgotPlt(uint8_t *buf
, const Symbol
&s
) const override
;
34 void writePltHeader(uint8_t *buf
) const override
;
35 void addPltHeaderSymbols(InputSection
&isd
) const override
;
36 void writePlt(uint8_t *buf
, const Symbol
&sym
,
37 uint64_t pltEntryAddr
) const override
;
38 RelExpr
adjustTlsExpr(RelType type
, RelExpr expr
) const override
;
39 RelExpr
adjustGotPcExpr(RelType type
, int64_t addend
,
40 const uint8_t *loc
) const override
;
41 bool relaxOnce(int pass
) const override
;
42 void relocate(uint8_t *loc
, const Relocation
&rel
,
43 uint64_t val
) const override
;
44 int64_t getImplicitAddend(const uint8_t *buf
, RelType type
) const override
;
47 void relaxGot(uint8_t *loc
, const Relocation
&rel
, uint64_t val
) const;
48 void relaxTlsGdToIe(uint8_t *loc
, const Relocation
&rel
, uint64_t val
) const;
49 void relaxTlsGdToLe(uint8_t *loc
, const Relocation
&rel
, uint64_t val
) const;
50 void relaxTlsLdToLe(uint8_t *loc
, const Relocation
&rel
, uint64_t val
) const;
56 gotRel
= R_390_GLOB_DAT
;
57 pltRel
= R_390_JMP_SLOT
;
58 relativeRel
= R_390_RELATIVE
;
59 iRelativeRel
= R_390_IRELATIVE
;
60 symbolicRel
= R_390_64
;
61 tlsGotRel
= R_390_TLS_TPOFF
;
62 tlsModuleIndexRel
= R_390_TLS_DTPMOD
;
63 tlsOffsetRel
= R_390_TLS_DTPOFF
;
64 gotHeaderEntriesNum
= 3;
65 gotPltHeaderEntriesNum
= 0;
71 // This "trap instruction" is used to fill gaps between sections.
72 // On SystemZ, the behavior of the GNU ld is to fill those gaps
73 // with nop instructions instead - and unfortunately the default
74 // glibc crt object files (used to) rely on that behavior since
75 // they use an alignment on the .init section fragments that causes
76 // gaps which must be filled with nops as they are being executed.
77 // Therefore, we provide a nop instruction as "trapInstr" here.
78 trapInstr
= {0x07, 0x07, 0x07, 0x07};
80 defaultImageBase
= 0x1000000;
83 RelExpr
SystemZ::getRelExpr(RelType type
, const Symbol
&s
,
84 const uint8_t *loc
) const {
88 // Relocations targeting the symbol value.
105 case R_390_GOTOFF
: // a.k.a. R_390_GOTOFF32
108 // Relocations targeting the PLT associated with the symbol.
120 // Relocations targeting the GOT entry associated with the symbol.
129 // Relocations targeting the GOTPLT entry associated with the symbol.
130 case R_390_GOTPLTENT
:
137 return R_GOTPLT_GOTREL
;
138 // Relocations targeting _GLOBAL_OFFSET_TABLE_.
142 // TLS-related relocations.
145 case R_390_TLS_GDCALL
:
147 case R_390_TLS_LDCALL
:
152 case R_390_TLS_LDM32
:
153 case R_390_TLS_LDM64
:
155 case R_390_TLS_LDO32
:
156 case R_390_TLS_LDO64
:
164 case R_390_TLS_GOTIE12
:
165 case R_390_TLS_GOTIE20
:
166 case R_390_TLS_GOTIE32
:
167 case R_390_TLS_GOTIE64
:
169 case R_390_TLS_IEENT
:
173 error(getErrorLocation(loc
) + "unknown relocation (" + Twine(type
) +
174 ") against symbol " + toString(s
));
179 void SystemZ::writeGotHeader(uint8_t *buf
) const {
180 // _GLOBAL_OFFSET_TABLE_[0] holds the value of _DYNAMIC.
181 // _GLOBAL_OFFSET_TABLE_[1] and [2] are reserved.
182 write64be(buf
, mainPart
->dynamic
->getVA());
185 void SystemZ::writeGotPlt(uint8_t *buf
, const Symbol
&s
) const {
186 write64be(buf
, s
.getPltVA() + 14);
189 void SystemZ::writeIgotPlt(uint8_t *buf
, const Symbol
&s
) const {
190 if (config
->writeAddends
)
191 write64be(buf
, s
.getVA());
194 void SystemZ::writePltHeader(uint8_t *buf
) const {
195 const uint8_t pltData
[] = {
196 0xe3, 0x10, 0xf0, 0x38, 0x00, 0x24, // stg %r1,56(%r15)
197 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, // larl %r1,_GLOBAL_OFFSET_TABLE_
198 0xd2, 0x07, 0xf0, 0x30, 0x10, 0x08, // mvc 48(8,%r15),8(%r1)
199 0xe3, 0x10, 0x10, 0x10, 0x00, 0x04, // lg %r1,16(%r1)
200 0x07, 0xf1, // br %r1
205 memcpy(buf
, pltData
, sizeof(pltData
));
206 uint64_t got
= in
.got
->getVA();
207 uint64_t plt
= in
.plt
->getVA();
208 write32be(buf
+ 8, (got
- plt
- 6) >> 1);
211 void SystemZ::addPltHeaderSymbols(InputSection
&isec
) const {
212 // The PLT header needs a reference to _GLOBAL_OFFSET_TABLE_, so we
213 // must ensure the .got section is created even if otherwise unused.
214 in
.got
->hasGotOffRel
.store(true, std::memory_order_relaxed
);
217 void SystemZ::writePlt(uint8_t *buf
, const Symbol
&sym
,
218 uint64_t pltEntryAddr
) const {
219 const uint8_t inst
[] = {
220 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, // larl %r1,<.got.plt slot>
221 0xe3, 0x10, 0x10, 0x00, 0x00, 0x04, // lg %r1,0(%r1)
222 0x07, 0xf1, // br %r1
223 0x0d, 0x10, // basr %r1,%r0
224 0xe3, 0x10, 0x10, 0x0c, 0x00, 0x14, // lgf %r1,12(%r1)
225 0xc0, 0xf4, 0x00, 0x00, 0x00, 0x00, // jg <plt header>
226 0x00, 0x00, 0x00, 0x00, // <relocation offset>
228 memcpy(buf
, inst
, sizeof(inst
));
230 write32be(buf
+ 2, (sym
.getGotPltVA() - pltEntryAddr
) >> 1);
231 write32be(buf
+ 24, (in
.plt
->getVA() - pltEntryAddr
- 22) >> 1);
232 write32be(buf
+ 28, in
.relaPlt
->entsize
* sym
.getPltIdx());
235 int64_t SystemZ::getImplicitAddend(const uint8_t *buf
, RelType type
) const {
238 return SignExtend64
<8>(*buf
);
241 return SignExtend64
<16>(read16be(buf
));
243 return SignExtend64
<16>(read16be(buf
)) << 1;
246 return SignExtend64
<32>(read32be(buf
));
248 return SignExtend64
<32>(read32be(buf
)) << 1;
251 case R_390_TLS_DTPMOD
:
252 case R_390_TLS_DTPOFF
:
253 case R_390_TLS_TPOFF
:
256 case R_390_IRELATIVE
:
257 return read64be(buf
);
261 // These relocations are defined as not having an implicit addend.
264 internalLinkerError(getErrorLocation(buf
),
265 "cannot read addend for relocation " + toString(type
));
270 RelType
SystemZ::getDynRel(RelType type
) const {
271 if (type
== R_390_64
|| type
== R_390_PC64
)
276 RelExpr
SystemZ::adjustTlsExpr(RelType type
, RelExpr expr
) const {
277 if (expr
== R_RELAX_TLS_GD_TO_IE
)
278 return R_RELAX_TLS_GD_TO_IE_GOT_OFF
;
282 int SystemZ::getTlsGdRelaxSkip(RelType type
) const {
283 // A __tls_get_offset call instruction is marked with 2 relocations:
285 // R_390_TLS_GDCALL / R_390_TLS_LDCALL: marker relocation
286 // R_390_PLT32DBL: __tls_get_offset
288 // After the relaxation we no longer call __tls_get_offset and should skip
289 // both relocations to not create a false dependence on __tls_get_offset
292 // Note that this mechanism only works correctly if the R_390_TLS_[GL]DCALL
293 // is seen immediately *before* the R_390_PLT32DBL. Unfortunately, current
294 // compilers on the platform will typically generate the inverse sequence.
295 // To fix this, we sort relocations by offset in RelocationScanner::scan;
296 // this ensures the correct sequence as the R_390_TLS_[GL]DCALL applies to
297 // the first byte of the brasl instruction, while the R_390_PLT32DBL applies
298 // to its third byte (the relative displacement).
300 if (type
== R_390_TLS_GDCALL
|| type
== R_390_TLS_LDCALL
)
305 void SystemZ::relaxTlsGdToIe(uint8_t *loc
, const Relocation
&rel
,
306 uint64_t val
) const {
307 // The general-dynamic code sequence for a global `x`:
309 // Instruction Relocation Symbol
313 // larl %r12,_GLOBAL_OFFSET_TABLE_ R_390_GOTPCDBL _GLOBAL_OFFSET_TABLE_
314 // lgrl %r2,.LC0 R_390_PC32DBL .LC0
315 // brasl %r14,__tls_get_offset@plt R_390_TLS_GDCALL x
316 // :tls_gdcall:x R_390_PLT32DBL __tls_get_offset
320 // .quad x@TLSGD R_390_TLS_GD64 x
322 // Relaxing to initial-exec entails:
323 // 1) Replacing the call by a load from the GOT.
324 // 2) Replacing the relocation on the constant LC0 by R_390_TLS_GOTIE64.
327 case R_390_TLS_GDCALL
:
328 // brasl %r14,__tls_get_offset@plt -> lg %r2,0(%r2,%r12)
329 write16be(loc
, 0xe322);
330 write32be(loc
+ 2, 0xc0000004);
333 relocateNoSym(loc
, R_390_TLS_GOTIE64
, val
);
336 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
340 void SystemZ::relaxTlsGdToLe(uint8_t *loc
, const Relocation
&rel
,
341 uint64_t val
) const {
342 // The general-dynamic code sequence for a global `x`:
344 // Instruction Relocation Symbol
348 // larl %r12,_GLOBAL_OFFSET_TABLE_ R_390_GOTPCDBL _GLOBAL_OFFSET_TABLE_
349 // lgrl %r2,.LC0 R_390_PC32DBL .LC0
350 // brasl %r14,__tls_get_offset@plt R_390_TLS_GDCALL x
351 // :tls_gdcall:x R_390_PLT32DBL __tls_get_offset
355 // .quad x@tlsgd R_390_TLS_GD64 x
357 // Relaxing to local-exec entails:
358 // 1) Replacing the call by a nop.
359 // 2) Replacing the relocation on the constant LC0 by R_390_TLS_LE64.
362 case R_390_TLS_GDCALL
:
363 // brasl %r14,__tls_get_offset@plt -> brcl 0,.
364 write16be(loc
, 0xc004);
365 write32be(loc
+ 2, 0x00000000);
368 relocateNoSym(loc
, R_390_TLS_LE64
, val
);
371 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
375 void SystemZ::relaxTlsLdToLe(uint8_t *loc
, const Relocation
&rel
,
376 uint64_t val
) const {
377 // The local-dynamic code sequence for a global `x`:
379 // Instruction Relocation Symbol
383 // larl %r12,_GLOBAL_OFFSET_TABLE_ R_390_GOTPCDBL _GLOBAL_OFFSET_TABLE_
384 // lgrl %r2,.LC0 R_390_PC32DBL .LC0
385 // brasl %r14,__tls_get_offset@plt R_390_TLS_LDCALL <sym>
386 // :tls_ldcall:<sym> R_390_PLT32DBL __tls_get_offset
388 // lgrl %rY,.LC1 R_390_PC32DBL .LC1
392 // .quad <sym>@tlsldm R_390_TLS_LDM64 <sym>
394 // .quad x@dtpoff R_390_TLS_LDO64 x
396 // Relaxing to local-exec entails:
397 // 1) Replacing the call by a nop.
398 // 2) Replacing the constant LC0 by 0 (i.e. ignoring the relocation).
399 // 3) Replacing the relocation on the constant LC1 by R_390_TLS_LE64.
402 case R_390_TLS_LDCALL
:
403 // brasl %r14,__tls_get_offset@plt -> brcl 0,.
404 write16be(loc
, 0xc004);
405 write32be(loc
+ 2, 0x00000000);
407 case R_390_TLS_LDM64
:
409 case R_390_TLS_LDO64
:
410 relocateNoSym(loc
, R_390_TLS_LE64
, val
);
413 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
417 RelExpr
SystemZ::adjustGotPcExpr(RelType type
, int64_t addend
,
418 const uint8_t *loc
) const {
419 // Only R_390_GOTENT with addend 2 can be relaxed.
420 if (!config
->relax
|| addend
!= 2 || type
!= R_390_GOTENT
)
422 const uint16_t op
= read16be(loc
- 2);
424 // lgrl rx,sym@GOTENT -> larl rx, sym
425 // This relaxation is legal if "sym" binds locally (which was already
426 // verified by our caller) and is in-range and properly aligned for a
427 // LARL instruction. We cannot verify the latter constraint here, so
428 // we assume it is true and revert the decision later on in relaxOnce
430 if ((op
& 0xff0f) == 0xc408)
431 return R_RELAX_GOT_PC
;
436 bool SystemZ::relaxOnce(int pass
) const {
437 // If we decided in adjustGotPcExpr to relax a R_390_GOTENT,
438 // we need to validate the target symbol is in-range and aligned.
439 SmallVector
<InputSection
*, 0> storage
;
440 bool changed
= false;
441 for (OutputSection
*osec
: outputSections
) {
442 if (!(osec
->flags
& SHF_EXECINSTR
))
444 for (InputSection
*sec
: getInputSections(*osec
, storage
)) {
445 for (Relocation
&rel
: sec
->relocs()) {
446 if (rel
.expr
!= R_RELAX_GOT_PC
)
449 uint64_t v
= sec
->getRelocTargetVA(
450 sec
->file
, rel
.type
, rel
.addend
,
451 sec
->getOutputSection()->addr
+ rel
.offset
, *rel
.sym
, rel
.expr
);
452 if (isInt
<33>(v
) && !(v
& 1))
454 if (rel
.sym
->auxIdx
== 0) {
455 rel
.sym
->allocateAux();
456 addGotEntry(*rel
.sym
);
466 void SystemZ::relaxGot(uint8_t *loc
, const Relocation
&rel
,
467 uint64_t val
) const {
468 assert(isInt
<33>(val
) &&
469 "R_390_GOTENT should not have been relaxed if it overflows");
471 "R_390_GOTENT should not have been relaxed if it is misaligned");
472 const uint16_t op
= read16be(loc
- 2);
474 // lgrl rx,sym@GOTENT -> larl rx, sym
475 if ((op
& 0xff0f) == 0xc408) {
476 write16be(loc
- 2, 0xc000 | (op
& 0x00f0));
477 write32be(loc
, val
>> 1);
481 void SystemZ::relocate(uint8_t *loc
, const Relocation
&rel
,
482 uint64_t val
) const {
485 return relaxGot(loc
, rel
, val
);
486 case R_RELAX_TLS_GD_TO_IE_GOT_OFF
:
487 return relaxTlsGdToIe(loc
, rel
, val
);
488 case R_RELAX_TLS_GD_TO_LE
:
489 return relaxTlsGdToLe(loc
, rel
, val
);
490 case R_RELAX_TLS_LD_TO_LE
:
491 return relaxTlsLdToLe(loc
, rel
, val
);
497 checkIntUInt(loc
, val
, 8, rel
);
503 case R_390_TLS_GOTIE12
:
504 checkUInt(loc
, val
, 12, rel
);
505 write16be(loc
, (read16be(loc
) & 0xF000) | val
);
509 checkInt(loc
, val
, 13, rel
);
510 checkAlignment(loc
, val
, 2, rel
);
511 write16be(loc
, (read16be(loc
) & 0xF000) | ((val
>> 1) & 0x0FFF));
518 checkIntUInt(loc
, val
, 16, rel
);
522 checkInt(loc
, val
, 16, rel
);
527 checkInt(loc
, val
, 17, rel
);
528 checkAlignment(loc
, val
, 2, rel
);
529 write16be(loc
, val
>> 1);
534 case R_390_TLS_GOTIE20
:
535 checkInt(loc
, val
, 20, rel
);
536 write32be(loc
, (read32be(loc
) & 0xF00000FF) | ((val
& 0xFFF) << 16) |
537 ((val
& 0xFF000) >> 4));
541 checkInt(loc
, val
, 25, rel
);
542 checkAlignment(loc
, val
, 2, rel
);
553 case R_390_TLS_GOTIE32
:
555 case R_390_TLS_LDM32
:
556 case R_390_TLS_LDO32
:
558 checkIntUInt(loc
, val
, 32, rel
);
563 checkInt(loc
, val
, 32, rel
);
570 case R_390_GOTPLTENT
:
571 case R_390_TLS_IEENT
:
572 checkInt(loc
, val
, 33, rel
);
573 checkAlignment(loc
, val
, 2, rel
);
574 write32be(loc
, val
>> 1);
585 case R_390_TLS_GOTIE64
:
587 case R_390_TLS_LDM64
:
588 case R_390_TLS_LDO64
:
590 case R_390_TLS_DTPMOD
:
591 case R_390_TLS_DTPOFF
:
592 case R_390_TLS_TPOFF
:
596 case R_390_TLS_GDCALL
:
597 case R_390_TLS_LDCALL
:
600 llvm_unreachable("unknown relocation");
604 TargetInfo
*elf::getSystemZTargetInfo() {