Skip to content

Commit 166483a

Browse files
uweigandtuliom
andcommitted
[lld] Add target support for SystemZ (s390x)
This patch adds full support for linking SystemZ (ELF s390x) object files. Support should be generally complete: - All relocation types are supported. - Full shared library support (DYNAMIC, GOT, PLT, ifunc). - Relaxation of TLS and GOT relocations where appropriate. - Platform-specific test cases. In addition to new platform code and the obvious changes, there were a few additional changes to common code: - Add three new RelExpr members (R_GOTPLT_OFF, R_GOTPLT_PC, and R_PLT_GOTREL) needed to support certain s390x relocations. I chose not to use a platform-specific name since nothing in the definition of these relocs is actually platform-specific; it is well possible that other platforms will need the same. - A couple of tweaks to TLS relocation handling, as the particular semantics of the s390x versions differ slightly. See comments in the code. This was tested by building and testing >1500 Fedora packages, with only a handful of failures; as these also have issues when building with LLD on other architectures, they seem unrelated. Co-authored-by: Tulio Magno Quites Machado Filho <[email protected]>
1 parent 59f7f35 commit 166483a

38 files changed

+1881
-3
lines changed

lld/ELF/Arch/SystemZ.cpp

Lines changed: 587 additions & 0 deletions
Large diffs are not rendered by default.

lld/ELF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_lld_library(lldELF
3333
Arch/PPC64.cpp
3434
Arch/RISCV.cpp
3535
Arch/SPARCV9.cpp
36+
Arch/SystemZ.cpp
3637
Arch/X86.cpp
3738
Arch/X86_64.cpp
3839
ARMErrataFix.cpp

lld/ELF/Driver.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef emul) {
199199
.Case("msp430elf", {ELF32LEKind, EM_MSP430})
200200
.Case("elf64_amdgpu", {ELF64LEKind, EM_AMDGPU})
201201
.Case("elf64loongarch", {ELF64LEKind, EM_LOONGARCH})
202+
.Case("elf64_s390", {ELF64BEKind, EM_S390})
202203
.Default({ELFNoneKind, EM_NONE});
203204

204205
if (ret.first == ELFNoneKind)
@@ -1171,7 +1172,7 @@ static bool getIsRela(opt::InputArgList &args) {
11711172
uint16_t m = config->emachine;
11721173
return m == EM_AARCH64 || m == EM_AMDGPU || m == EM_HEXAGON ||
11731174
m == EM_LOONGARCH || m == EM_PPC || m == EM_PPC64 || m == EM_RISCV ||
1174-
m == EM_X86_64;
1175+
m == EM_S390 || m == EM_X86_64;
11751176
}
11761177

11771178
static void parseClangOption(StringRef opt, const Twine &msg) {

lld/ELF/InputFiles.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,8 @@ static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) {
16121612
return EM_RISCV;
16131613
case Triple::sparcv9:
16141614
return EM_SPARCV9;
1615+
case Triple::systemz:
1616+
return EM_S390;
16151617
case Triple::x86:
16161618
return t.isOSIAMCU() ? EM_IAMCU : EM_386;
16171619
case Triple::x86_64:

lld/ELF/InputSection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ static int64_t getTlsTpOffset(const Symbol &s) {
652652

653653
// Variant 2.
654654
case EM_HEXAGON:
655+
case EM_S390:
655656
case EM_SPARCV9:
656657
case EM_386:
657658
case EM_X86_64:
@@ -705,6 +706,8 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
705706
case R_GOT_OFF:
706707
case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
707708
return sym.getGotOffset() + a;
709+
case R_GOTPLT_OFF:
710+
return sym.getGotPltOffset() + a;
708711
case R_AARCH64_GOT_PAGE_PC:
709712
case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
710713
return getAArch64Page(sym.getGotVA() + a) - getAArch64Page(p);
@@ -713,6 +716,8 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
713716
case R_GOT_PC:
714717
case R_RELAX_TLS_GD_TO_IE:
715718
return sym.getGotVA() + a - p;
719+
case R_GOTPLT_PC:
720+
return sym.getGotPltVA() + a - p;
716721
case R_LOONGARCH_GOT_PAGE_PC:
717722
if (sym.hasFlag(NEEDS_TLSGD))
718723
return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p);
@@ -804,6 +809,8 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
804809
return getLoongArchPageDelta(sym.getPltVA() + a, p);
805810
case R_PLT_GOTPLT:
806811
return sym.getPltVA() + a - in.gotPlt->getVA();
812+
case R_PLT_GOTREL:
813+
return sym.getPltVA() + a - in.got->getVA();
807814
case R_PPC32_PLTREL:
808815
// R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30
809816
// stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for

lld/ELF/Relocations.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,8 +1357,8 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym,
13571357
R_LOONGARCH_GOT_PAGE_PC, R_GOT_OFF, R_TLSIE_HINT>(expr)) {
13581358
ctx.hasTlsIe.store(true, std::memory_order_relaxed);
13591359
// Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
1360-
// defined.
1361-
if (toExecRelax && isLocalInExecutable) {
1360+
// defined. This is not supported on SystemZ.
1361+
if (toExecRelax && isLocalInExecutable && config->emachine != EM_S390) {
13621362
c.addReloc({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
13631363
} else if (expr != R_TLSIE_HINT) {
13641364
sym.setFlags(NEEDS_TLSIE);
@@ -1404,6 +1404,26 @@ template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
14041404
if (expr == R_NONE)
14051405
return;
14061406

1407+
// Like other platforms, calls to the TLS helper routine on SystemZ carry
1408+
// two relocations, one for the helper routine itself, and a TLS marker
1409+
// relocation. When relaxing the TLS model, the helper routine is no longer
1410+
// needed, and its relocation should be removed. Unlike other platforms,
1411+
// on SystemZ the TLS marker routine typically comes *after* the helper
1412+
// routine relocation, so the getTlsGdRelaxSkip mechanism used by
1413+
// handleTlsRelocation does not work on this platform.
1414+
//
1415+
// Instead, check for this case here: if we are building a main executable
1416+
// (i.e. TLS relaxation applies), and the relocation *after* the current one
1417+
// is a TLS call marker instruction matching the current instruction, then
1418+
// skip this relocation.
1419+
if (config->emachine == EM_S390 && !config->shared) {
1420+
if (i < end && getter.get(i->r_offset) == offset - 2) {
1421+
RelType nextType = i->getType(/*isMips64EL=*/false);
1422+
if (nextType == R_390_TLS_GDCALL || nextType == R_390_TLS_LDCALL)
1423+
return;
1424+
}
1425+
}
1426+
14071427
// Error if the target symbol is undefined. Symbol index 0 may be used by
14081428
// marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
14091429
if (sym.isUndefined() && symIndex != 0 &&

lld/ELF/Relocations.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ enum RelExpr {
4040
R_GOTPLT,
4141
R_GOTPLTREL,
4242
R_GOTREL,
43+
R_GOTPLT_OFF,
44+
R_GOTPLT_PC,
4345
R_NONE,
4446
R_PC,
4547
R_PLT,
4648
R_PLT_PC,
4749
R_PLT_GOTPLT,
50+
R_PLT_GOTREL,
4851
R_RELAX_HINT,
4952
R_RELAX_GOT_PC,
5053
R_RELAX_GOT_PC_NOPIC,

lld/ELF/ScriptParser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) {
444444
.Case("elf32-msp430", {ELF32LEKind, EM_MSP430})
445445
.Case("elf32-loongarch", {ELF32LEKind, EM_LOONGARCH})
446446
.Case("elf64-loongarch", {ELF64LEKind, EM_LOONGARCH})
447+
.Case("elf64-s390", {ELF64BEKind, EM_S390})
447448
.Default({ELFNoneKind, EM_NONE});
448449
}
449450

lld/ELF/SyntheticSections.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,9 @@ DynamicSection<ELFT>::computeContents() {
14191419
case EM_MIPS:
14201420
addInSec(DT_MIPS_PLTGOT, *in.gotPlt);
14211421
break;
1422+
case EM_S390:
1423+
addInSec(DT_PLTGOT, *in.got);
1424+
break;
14221425
case EM_SPARCV9:
14231426
addInSec(DT_PLTGOT, *in.plt);
14241427
break;

lld/ELF/Target.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ TargetInfo *elf::getTarget() {
8787
return getRISCVTargetInfo();
8888
case EM_SPARCV9:
8989
return getSPARCV9TargetInfo();
90+
case EM_S390:
91+
return getSystemZTargetInfo();
9092
case EM_X86_64:
9193
return getX86_64TargetInfo();
9294
}

0 commit comments

Comments
 (0)