Skip to content

Commit 30dd63c

Browse files
committed
[ELF] Do not create .plt.got for ppc64/ppc64le
1 parent 2dbbc54 commit 30dd63c

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

elf/arch-ppc64v1.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,10 @@ void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
105105
*(ub32 *)(buf + 4) = 0x4b00'0000 | (offset & 0x00ff'ffff); // b plt0
106106
}
107107

108+
// .plt.got is not necessary on PPC64 because range extension thunks
109+
// directly read GOT entries and jump there.
108110
template <>
109-
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
110-
// No one uses .got.plt at runtime because all calls to .got.plt are
111-
// made via range extension thunks. Range extension thunks directly
112-
// calls the final destination by reading a .got entry. Here, we just
113-
// set a dummy instruction.
114-
*(ub32 *)buf = 0x6000'0000; // nop
115-
}
111+
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {}
116112

117113
template <>
118114
void EhFrameSection<E>::apply_reloc(Context<E> &ctx, const ElfRel<E> &rel,

elf/arch-ppc64v2.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,10 @@ void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
121121
*(ul32 *)buf |= (ctx.plt->shdr.sh_addr - sym.get_plt_addr(ctx)) & 0x00ff'ffff;
122122
}
123123

124+
// .plt.got is not necessary on PPC64 because range extension thunks
125+
// directly read GOT entries and jump there.
124126
template <>
125-
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
126-
// No one uses .got.plt at runtime because all calls to .got.plt are
127-
// made via range extension thunks. Range extension thunks directly
128-
// calls the final destination by reading a .got entry. Here, we just
129-
// set a dummy instruction.
130-
//
131-
// I believe we can completely elimnate .got.plt, but saving 4 bytes
132-
// for each GOTPLT entry doesn't seem to be worth its complexity.
133-
*(ul32 *)buf = 0x6000'0000; // nop
134-
}
127+
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {}
135128

136129
template <>
137130
void EhFrameSection<E>::apply_reloc(Context<E> &ctx, const ElfRel<E> &rel,

elf/arch-s390x.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@
1414
// aligned to 2 bytes boundaries. Despite unfamiliarty, I found that it
1515
// just feels like a 64-bit i386 in a parallel universe.
1616
//
17-
// Its psABI reserves %r0 and %r1 as scratch registers so we can use them
18-
// in our PLT. %r2-%r6 are used for parameter passing. %r2 is also used to
19-
// return a value. In position independent code, %r12 usually contains the
20-
// address of GOT. %r14 usually contains a return address. %r15 is a stack
21-
// pointer. Access registers %a0 and %a1 contain the upper 32 bits and
22-
// the lower 32 bits of the thread pointer, respectively.
17+
// Here is the register usage in this ABI:
2318
//
24-
// https://uclibc.org/docs/psABI-s390x.pdf
19+
// r0-r1: reserved as scratch registers so we can use them in our PLT
20+
// r2: parameter passing and return values
21+
// r3-r6: parameter passing
22+
// r12: address of GOT if position-independent code
23+
// r14: return address
24+
// r15: stack pointer
25+
// a1: upper 32 bits of TP (thread pointer)
26+
// a2: lower 32 bits of TP (thread pointer)
27+
//
28+
// TLS is supported on s390x in the same way as it is on other targets
29+
// with one exeption. On other targets, __tls_get_addr is used to get an
30+
// address of a thread-local variable. On s390x, __tls_get_offset is used
31+
// instead. The difference is __tls_get_offset returns an address of a
32+
// thread-local variable as an offset from TP. So we need to add TP to a
33+
// return value before use. I don't know why it is different, but that is
34+
// the way it is.
35+
//
36+
// https://github.com/IBM/s390x-abi/releases/download/v1.6/lzsabi_s390x.pdf
2537

2638
#include "mold.h"
2739

@@ -33,7 +45,7 @@ template <>
3345
void write_plt_header(Context<E> &ctx, u8 *buf) {
3446
static u8 insn[] = {
3547
0xe3, 0x00, 0xf0, 0x38, 0x00, 0x24, // stg %r0, 56(%r15)
36-
0xc0, 0x10, 0, 0, 0, 0, // larl %r1, GOT_OFFSET
48+
0xc0, 0x10, 0, 0, 0, 0, // larl %r1, GOTPLT_OFFSET
3749
0xd2, 0x07, 0xf0, 0x30, 0x10, 0x08, // mvc 48(8, %r15), 8(%r1)
3850
0xe3, 0x10, 0x10, 0x10, 0x00, 0x04, // lg %r1, 16(%r1)
3951
0x07, 0xf1, // br %r1

elf/elf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,7 @@ struct PPC64V1 {
26692669
static constexpr u32 e_machine = EM_PPC64;
26702670
static constexpr u32 plt_hdr_size = 52;
26712671
static constexpr u32 plt_size = 8;
2672-
static constexpr u32 pltgot_size = 4;
2672+
static constexpr u32 pltgot_size = 0;
26732673
static constexpr u32 tls_dtv_offset = 0x8000;
26742674
static constexpr u32 thunk_hdr_size = 0;
26752675
static constexpr u32 thunk_size = 28;
@@ -2706,7 +2706,7 @@ struct PPC64V2 {
27062706
static constexpr u32 e_machine = EM_PPC64;
27072707
static constexpr u32 plt_hdr_size = 60;
27082708
static constexpr u32 plt_size = 4;
2709-
static constexpr u32 pltgot_size = 4;
2709+
static constexpr u32 pltgot_size = 0;
27102710
static constexpr u32 tls_dtv_offset = 0x8000;
27112711
static constexpr u32 thunk_hdr_size = 0;
27122712
static constexpr u32 thunk_size = 20;

elf/output-chunks.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,9 +1496,9 @@ void PltGotSection<E>::add_symbol(Context<E> &ctx, Symbol<E> *sym) {
14961496
assert(!sym->has_plt(ctx));
14971497
assert(sym->has_got(ctx));
14981498

1499-
sym->set_pltgot_idx(ctx, this->shdr.sh_size / E::pltgot_size);
1500-
this->shdr.sh_size += E::pltgot_size;
1499+
sym->set_pltgot_idx(ctx, symbols.size());
15011500
symbols.push_back(sym);
1501+
this->shdr.sh_size = symbols.size() * E::pltgot_size;
15021502
}
15031503

15041504
template <typename E>

0 commit comments

Comments
 (0)