Skip to content

Commit d624134

Browse files
committed
[lld][X86] Restore gotEntrySize.
D62727 removed GotEntrySize and GotPltEntrySize with a comment that they are always equal to wordsize(), but that is not entirely true: X32 has a word size of 4, but needs 8-byte GOT entries. This restores gotEntrySize for both, adjusted for current naming conventions, but defaults it to config->wordsize to keep things simple for architectures other than x86_64. This partially reverts D62727. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D102509
1 parent 7f5d8e6 commit d624134

File tree

5 files changed

+68
-11
lines changed

5 files changed

+68
-11
lines changed

lld/ELF/Arch/X86_64.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ X86_64::X86_64() {
8585
tlsGotRel = R_X86_64_TPOFF64;
8686
tlsModuleIndexRel = R_X86_64_DTPMOD64;
8787
tlsOffsetRel = R_X86_64_DTPOFF64;
88+
gotEntrySize = 8;
8889
pltHeaderSize = 16;
8990
pltEntrySize = 16;
9091
ipltEntrySize = 16;

lld/ELF/Symbols.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ uint64_t Symbol::getGotVA() const {
160160
return in.got->getVA() + getGotOffset();
161161
}
162162

163-
uint64_t Symbol::getGotOffset() const { return gotIndex * config->wordsize; }
163+
uint64_t Symbol::getGotOffset() const {
164+
return gotIndex * target->gotEntrySize;
165+
}
164166

165167
uint64_t Symbol::getGotPltVA() const {
166168
if (isInIplt)
@@ -170,8 +172,8 @@ uint64_t Symbol::getGotPltVA() const {
170172

171173
uint64_t Symbol::getGotPltOffset() const {
172174
if (isInIplt)
173-
return pltIndex * config->wordsize;
174-
return (pltIndex + target->gotPltHeaderEntriesNum) * config->wordsize;
175+
return pltIndex * target->gotEntrySize;
176+
return (pltIndex + target->gotPltHeaderEntriesNum) * target->gotEntrySize;
175177
}
176178

177179
uint64_t Symbol::getPltVA() const {

lld/ELF/SyntheticSections.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ void EhFrameSection::writeTo(uint8_t *buf) {
644644
}
645645

646646
GotSection::GotSection()
647-
: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,
648-
".got") {
647+
: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
648+
target->gotEntrySize, ".got") {
649649
numEntries = target->gotHeaderEntriesNum;
650650
}
651651

@@ -1145,15 +1145,16 @@ void GotPltSection::addEntry(Symbol &sym) {
11451145
}
11461146

11471147
size_t GotPltSection::getSize() const {
1148-
return (target->gotPltHeaderEntriesNum + entries.size()) * config->wordsize;
1148+
return (target->gotPltHeaderEntriesNum + entries.size()) *
1149+
target->gotEntrySize;
11491150
}
11501151

11511152
void GotPltSection::writeTo(uint8_t *buf) {
11521153
target->writeGotPltHeader(buf);
1153-
buf += target->gotPltHeaderEntriesNum * config->wordsize;
1154+
buf += target->gotPltHeaderEntriesNum * target->gotEntrySize;
11541155
for (const Symbol *b : entries) {
11551156
target->writeGotPlt(buf, *b);
1156-
buf += config->wordsize;
1157+
buf += target->gotEntrySize;
11571158
}
11581159
}
11591160

@@ -1181,21 +1182,21 @@ static StringRef getIgotPltName() {
11811182
IgotPltSection::IgotPltSection()
11821183
: SyntheticSection(SHF_ALLOC | SHF_WRITE,
11831184
config->emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1184-
config->wordsize, getIgotPltName()) {}
1185+
target->gotEntrySize, getIgotPltName()) {}
11851186

11861187
void IgotPltSection::addEntry(Symbol &sym) {
11871188
assert(sym.pltIndex == entries.size());
11881189
entries.push_back(&sym);
11891190
}
11901191

11911192
size_t IgotPltSection::getSize() const {
1192-
return entries.size() * config->wordsize;
1193+
return entries.size() * target->gotEntrySize;
11931194
}
11941195

11951196
void IgotPltSection::writeTo(uint8_t *buf) {
11961197
for (const Symbol *b : entries) {
11971198
target->writeIgotPlt(buf, *b);
1198-
buf += config->wordsize;
1199+
buf += target->gotEntrySize;
11991200
}
12001201
}
12011202

lld/ELF/Target.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class TargetInfo {
122122
RelType tlsGotRel;
123123
RelType tlsModuleIndexRel;
124124
RelType tlsOffsetRel;
125+
unsigned gotEntrySize = config->wordsize;
125126
unsigned pltEntrySize;
126127
unsigned pltHeaderSize;
127128
unsigned ipltEntrySize;

lld/test/ELF/x86-x32-plt.s

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-gnux32 %s -o %t.o
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64-gnux32 %p/Inputs/shared.s -o %t2.o
4+
# RUN: ld.lld -shared -soname=t2 %t2.o -o %t2.so
5+
6+
# RUN: ld.lld %t.o %t2.so -o %t
7+
# RUN: llvm-readelf -S -r %t | FileCheck %s
8+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefixes=DISASM
9+
10+
# CHECK: Name Type Address Off Size ES Flg Lk Inf Al
11+
# CHECK: .plt PROGBITS 002011e0 0001e0 000030 00 AX 0 0 16
12+
# CHECK: .got.plt PROGBITS 00203278 000278 000028 00 WA 0 0 8
13+
# CHECK: Relocation section '.rela.plt' at offset {{.*}} contains 2 entries:
14+
# CHECK: 00203290 {{.*}} R_X86_64_JUMP_SLOT 00000000 bar + 0
15+
# CHECK-NEXT: 00203298 {{.*}} R_X86_64_JUMP_SLOT 00000000 weak + 0
16+
17+
# DISASM: <_start>:
18+
# DISASM-NEXT: callq {{.*}} <local>
19+
# DISASM-NEXT: callq {{.*}} <bar@plt>
20+
# DISASM-NEXT: jmp {{.*}} <bar@plt>
21+
# DISASM-NEXT: jmp {{.*}} <weak@plt>
22+
23+
# DISASM: Disassembly of section .plt:
24+
# DISASM-EMPTY:
25+
# DISASM-NEXT: <.plt>:
26+
# DISASM-NEXT: 2011e0: pushq 8346(%rip) # 203280
27+
# DISASM-NEXT: jmpq *8348(%rip) # 203288
28+
# DISASM-NEXT: nopl (%rax)
29+
# DISASM-EMPTY:
30+
# DISASM-NEXT: <bar@plt>:
31+
# DISASM-NEXT: 2011f0: jmpq *8346(%rip) # 203290
32+
# DISASM-NEXT: pushq $0
33+
# DISASM-NEXT: jmp 0x2011e0 <.plt>
34+
# DISASM-EMPTY:
35+
# DISASM-NEXT: <weak@plt>:
36+
# DISASM-NEXT: 201200: jmpq *8338(%rip) # 203298
37+
# DISASM-NEXT: pushq $1
38+
# DISASM-NEXT: jmp 0x2011e0 <.plt>
39+
# DISASM-NOT: {{.}}
40+
41+
.global _start
42+
.weak weak
43+
44+
_start:
45+
call local
46+
call bar
47+
jmp bar@plt
48+
jmp weak
49+
50+
## foo is local and non-preemptale, no PLT is generated.
51+
local:
52+
ret

0 commit comments

Comments
 (0)