Skip to content

Commit fe3406e

Browse files
uweigandtuliom
andauthored
[lld] Add target support for SystemZ (s390x) (#75643)
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 4c93109 commit fe3406e

36 files changed

+1959
-10
lines changed

lld/ELF/Arch/SystemZ.cpp

Lines changed: 607 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)
@@ -1137,7 +1138,7 @@ static SmallVector<StringRef, 0> getSymbolOrderingFile(MemoryBufferRef mb) {
11371138
static bool getIsRela(opt::InputArgList &args) {
11381139
// The psABI specifies the default relocation entry format.
11391140
bool rela = is_contained({EM_AARCH64, EM_AMDGPU, EM_HEXAGON, EM_LOONGARCH,
1140-
EM_PPC, EM_PPC64, EM_RISCV, EM_X86_64},
1141+
EM_PPC, EM_PPC64, EM_RISCV, EM_S390, EM_X86_64},
11411142
config->emachine);
11421143
// If -z rel or -z rela is specified, use the last option.
11431144
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
@@ -655,6 +655,7 @@ static int64_t getTlsTpOffset(const Symbol &s) {
655655

656656
// Variant 2.
657657
case EM_HEXAGON:
658+
case EM_S390:
658659
case EM_SPARCV9:
659660
case EM_386:
660661
case EM_X86_64:
@@ -717,6 +718,10 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
717718
case R_GOT_PC:
718719
case R_RELAX_TLS_GD_TO_IE:
719720
return sym.getGotVA() + a - p;
721+
case R_GOTPLT_GOTREL:
722+
return sym.getGotPltVA() + a - in.got->getVA();
723+
case R_GOTPLT_PC:
724+
return sym.getGotPltVA() + a - p;
720725
case R_LOONGARCH_GOT_PAGE_PC:
721726
if (sym.hasFlag(NEEDS_TLSGD))
722727
return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type);
@@ -808,6 +813,8 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
808813
return getLoongArchPageDelta(sym.getPltVA() + a, p, type);
809814
case R_PLT_GOTPLT:
810815
return sym.getPltVA() + a - in.gotPlt->getVA();
816+
case R_PLT_GOTREL:
817+
return sym.getPltVA() + a - in.got->getVA();
811818
case R_PPC32_PLTREL:
812819
// R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30
813820
// stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for

lld/ELF/Relocations.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ static bool isAbsoluteValue(const Symbol &sym) {
203203

204204
// Returns true if Expr refers a PLT entry.
205205
static bool needsPlt(RelExpr expr) {
206-
return oneof<R_PLT, R_PLT_PC, R_PLT_GOTPLT, R_LOONGARCH_PLT_PAGE_PC,
207-
R_PPC32_PLTREL, R_PPC64_CALL_PLT>(expr);
206+
return oneof<R_PLT, R_PLT_PC, R_PLT_GOTREL, R_PLT_GOTPLT, R_GOTPLT_GOTREL,
207+
R_GOTPLT_PC, R_LOONGARCH_PLT_PAGE_PC, R_PPC32_PLTREL,
208+
R_PPC64_CALL_PLT>(expr);
208209
}
209210

210211
bool lld::elf::needsGot(RelExpr expr) {
@@ -233,6 +234,8 @@ static RelExpr toPlt(RelExpr expr) {
233234
return R_PLT_PC;
234235
case R_ABS:
235236
return R_PLT;
237+
case R_GOTREL:
238+
return R_PLT_GOTREL;
236239
default:
237240
return expr;
238241
}
@@ -253,6 +256,8 @@ static RelExpr fromPlt(RelExpr expr) {
253256
return R_ABS;
254257
case R_PLT_GOTPLT:
255258
return R_GOTPLTREL;
259+
case R_PLT_GOTREL:
260+
return R_GOTREL;
256261
default:
257262
return expr;
258263
}
@@ -979,10 +984,10 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
979984
if (oneof<R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, R_MIPS_GOT_LOCAL_PAGE,
980985
R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
981986
R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC,
982-
R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT,
983-
R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE,
984-
R_LOONGARCH_PLT_PAGE_PC, R_LOONGARCH_GOT, R_LOONGARCH_GOT_PAGE_PC>(
985-
e))
987+
R_PLT_PC, R_PLT_GOTREL, R_PLT_GOTPLT, R_GOTPLT_GOTREL, R_GOTPLT_PC,
988+
R_PPC32_PLTREL, R_PPC64_CALL_PLT, R_PPC64_RELAX_TOC, R_RISCV_ADD,
989+
R_AARCH64_GOT_PAGE, R_LOONGARCH_PLT_PAGE_PC, R_LOONGARCH_GOT,
990+
R_LOONGARCH_GOT_PAGE_PC>(e))
986991
return true;
987992

988993
// These never do, except if the entire file is position dependent or if
@@ -1374,8 +1379,8 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym,
13741379
R_LOONGARCH_GOT_PAGE_PC, R_GOT_OFF, R_TLSIE_HINT>(expr)) {
13751380
ctx.hasTlsIe.store(true, std::memory_order_relaxed);
13761381
// Initial-Exec relocs can be optimized to Local-Exec if the symbol is
1377-
// locally defined.
1378-
if (execOptimize && isLocalInExecutable) {
1382+
// locally defined. This is not supported on SystemZ.
1383+
if (execOptimize && isLocalInExecutable && config->emachine != EM_S390) {
13791384
c.addReloc({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
13801385
} else if (expr != R_TLSIE_HINT) {
13811386
sym.setFlags(NEEDS_TLSIE);
@@ -1534,8 +1539,10 @@ void RelocationScanner::scan(ArrayRef<RelTy> rels) {
15341539
// For EhInputSection, OffsetGetter expects the relocations to be sorted by
15351540
// r_offset. In rare cases (.eh_frame pieces are reordered by a linker
15361541
// script), the relocations may be unordered.
1542+
// On SystemZ, all sections need to be sorted by r_offset, to allow TLS
1543+
// relaxation to be handled correctly - see SystemZ::getTlsGdRelaxSkip.
15371544
SmallVector<RelTy, 0> storage;
1538-
if (isa<EhInputSection>(sec))
1545+
if (isa<EhInputSection>(sec) || config->emachine == EM_S390)
15391546
rels = sortRels(rels, storage);
15401547

15411548
end = static_cast<const void *>(rels.end());

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_GOTREL,
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
default:

0 commit comments

Comments
 (0)