Skip to content

Commit e20e013

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 d7fb9eb commit e20e013

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
@@ -200,6 +200,7 @@ static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef emul) {
200200
.Case("msp430elf", {ELF32LEKind, EM_MSP430})
201201
.Case("elf64_amdgpu", {ELF64LEKind, EM_AMDGPU})
202202
.Case("elf64loongarch", {ELF64LEKind, EM_LOONGARCH})
203+
.Case("elf64_s390", {ELF64BEKind, EM_S390})
203204
.Default({ELFNoneKind, EM_NONE});
204205

205206
if (ret.first == ELFNoneKind)
@@ -1136,7 +1137,7 @@ static SmallVector<StringRef, 0> getSymbolOrderingFile(MemoryBufferRef mb) {
11361137
static bool getIsRela(opt::InputArgList &args) {
11371138
// The psABI specifies the default relocation entry format.
11381139
bool rela = is_contained({EM_AARCH64, EM_AMDGPU, EM_HEXAGON, EM_LOONGARCH,
1139-
EM_PPC, EM_PPC64, EM_RISCV, EM_X86_64},
1140+
EM_PPC, EM_PPC64, EM_RISCV, EM_S390, EM_X86_64},
11401141
config->emachine);
11411142
// If -z rel or -z rela is specified, use the last option.
11421143
for (auto *arg : args.filtered(OPT_z)) {

lld/ELF/InputFiles.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,8 @@ static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) {
16141614
return EM_RISCV;
16151615
case Triple::sparcv9:
16161616
return EM_SPARCV9;
1617+
case Triple::systemz:
1618+
return EM_S390;
16171619
case Triple::x86:
16181620
return t.isOSIAMCU() ? EM_IAMCU : EM_386;
16191621
case Triple::x86_64:

lld/ELF/InputSection.cpp

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

655655
// Variant 2.
656656
case EM_HEXAGON:
657+
case EM_S390:
657658
case EM_SPARCV9:
658659
case EM_386:
659660
case EM_X86_64:
@@ -708,6 +709,8 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
708709
case R_GOT_OFF:
709710
case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
710711
return sym.getGotOffset() + a;
712+
case R_GOTPLT_OFF:
713+
return sym.getGotPltOffset() + a;
711714
case R_AARCH64_GOT_PAGE_PC:
712715
case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
713716
return getAArch64Page(sym.getGotVA() + a) - getAArch64Page(p);
@@ -716,6 +719,8 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
716719
case R_GOT_PC:
717720
case R_RELAX_TLS_GD_TO_IE:
718721
return sym.getGotVA() + a - p;
722+
case R_GOTPLT_PC:
723+
return sym.getGotPltVA() + a - p;
719724
case R_LOONGARCH_GOT_PAGE_PC:
720725
if (sym.hasFlag(NEEDS_TLSGD))
721726
return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type);
@@ -807,6 +812,8 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
807812
return getLoongArchPageDelta(sym.getPltVA() + a, p, type);
808813
case R_PLT_GOTPLT:
809814
return sym.getPltVA() + a - in.gotPlt->getVA();
815+
case R_PLT_GOTREL:
816+
return sym.getPltVA() + a - in.got->getVA();
810817
case R_PPC32_PLTREL:
811818
// R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30
812819
// 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
@@ -1364,8 +1364,8 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym,
13641364
R_LOONGARCH_GOT_PAGE_PC, R_GOT_OFF, R_TLSIE_HINT>(expr)) {
13651365
ctx.hasTlsIe.store(true, std::memory_order_relaxed);
13661366
// Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
1367-
// defined.
1368-
if (toExecRelax && isLocalInExecutable) {
1367+
// defined. This is not supported on SystemZ.
1368+
if (toExecRelax && isLocalInExecutable && config->emachine != EM_S390) {
13691369
c.addReloc({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
13701370
} else if (expr != R_TLSIE_HINT) {
13711371
sym.setFlags(NEEDS_TLSIE);
@@ -1411,6 +1411,26 @@ template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
14111411
if (expr == R_NONE)
14121412
return;
14131413

1414+
// Like other platforms, calls to the TLS helper routine on SystemZ carry
1415+
// two relocations, one for the helper routine itself, and a TLS marker
1416+
// relocation. When relaxing the TLS model, the helper routine is no longer
1417+
// needed, and its relocation should be removed. Unlike other platforms,
1418+
// on SystemZ the TLS marker routine typically comes *after* the helper
1419+
// routine relocation, so the getTlsGdRelaxSkip mechanism used by
1420+
// handleTlsRelocation does not work on this platform.
1421+
//
1422+
// Instead, check for this case here: if we are building a main executable
1423+
// (i.e. TLS relaxation applies), and the relocation *after* the current one
1424+
// is a TLS call marker instruction matching the current instruction, then
1425+
// skip this relocation.
1426+
if (config->emachine == EM_S390 && !config->shared) {
1427+
if (i < end && getter.get(i->r_offset) == offset - 2) {
1428+
RelType nextType = i->getType(/*isMips64EL=*/false);
1429+
if (nextType == R_390_TLS_GDCALL || nextType == R_390_TLS_LDCALL)
1430+
return;
1431+
}
1432+
}
1433+
14141434
// Error if the target symbol is undefined. Symbol index 0 may be used by
14151435
// marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
14161436
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
@@ -445,6 +445,7 @@ static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) {
445445
.Case("elf32-msp430", {ELF32LEKind, EM_MSP430})
446446
.Case("elf32-loongarch", {ELF32LEKind, EM_LOONGARCH})
447447
.Case("elf64-loongarch", {ELF64LEKind, EM_LOONGARCH})
448+
.Case("elf64-s390", {ELF64BEKind, EM_S390})
448449
.Default({ELFNoneKind, EM_NONE});
449450
}
450451

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)