Skip to content

Commit 6be4670

Browse files
authored
[ELF] Consistently use gotEntrySize for GOT entries (#142064)
d624134 ("[lld][X86] Restore gotEntrySize.") (re-)introduced gotEntrySize and used it for various GOT calculations, but was not exhaustive (nor consistent; Symbol::getGotOffset was modified but not GotSection::finalizeContents, so we undercompute the size, on top of computing the wrong offsets for TLS), and since then even more uses have been added that use wordsize instead of gotEntrySize (presumably due to looking at the existing incorrect ones). This doesn't really matter upstream, as the only architecture where the two differ is X32, and from looking at the code it's not properly supported (e.g. TLS relaxation assumes LP64 sequences), but downstream in CHERI LLVM it does matter, as CHERI's pointers are more than just an integer address (a machine word). Note this ignores the special MipsGotSection; on MIPS, wordsize and gotEntrySize are the same, CHERI-MIPS is no longer something we support downstream and even when we did we didn't reuse that implementation.
1 parent 63861d6 commit 6be4670

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lld/ELF/SyntheticSections.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,8 @@ void GotSection::addEntry(const Symbol &sym) {
668668
}
669669

670670
void GotSection::addAuthEntry(const Symbol &sym) {
671-
authEntries.push_back({(numEntries - 1) * ctx.arg.wordsize, sym.isFunc()});
671+
authEntries.push_back(
672+
{(numEntries - 1) * ctx.target->gotEntrySize, sym.isFunc()});
672673
}
673674

674675
bool GotSection::addTlsDescEntry(const Symbol &sym) {
@@ -679,8 +680,8 @@ bool GotSection::addTlsDescEntry(const Symbol &sym) {
679680
}
680681

681682
void GotSection::addTlsDescAuthEntry() {
682-
authEntries.push_back({(numEntries - 2) * ctx.arg.wordsize, true});
683-
authEntries.push_back({(numEntries - 1) * ctx.arg.wordsize, false});
683+
authEntries.push_back({(numEntries - 2) * ctx.target->gotEntrySize, true});
684+
authEntries.push_back({(numEntries - 1) * ctx.target->gotEntrySize, false});
684685
}
685686

686687
bool GotSection::addDynTlsEntry(const Symbol &sym) {
@@ -696,25 +697,25 @@ bool GotSection::addDynTlsEntry(const Symbol &sym) {
696697
bool GotSection::addTlsIndex() {
697698
if (tlsIndexOff != uint32_t(-1))
698699
return false;
699-
tlsIndexOff = numEntries * ctx.arg.wordsize;
700+
tlsIndexOff = numEntries * ctx.target->gotEntrySize;
700701
numEntries += 2;
701702
return true;
702703
}
703704

704705
uint32_t GotSection::getTlsDescOffset(const Symbol &sym) const {
705-
return sym.getTlsDescIdx(ctx) * ctx.arg.wordsize;
706+
return sym.getTlsDescIdx(ctx) * ctx.target->gotEntrySize;
706707
}
707708

708709
uint64_t GotSection::getTlsDescAddr(const Symbol &sym) const {
709710
return getVA() + getTlsDescOffset(sym);
710711
}
711712

712713
uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const {
713-
return this->getVA() + b.getTlsGdIdx(ctx) * ctx.arg.wordsize;
714+
return this->getVA() + b.getTlsGdIdx(ctx) * ctx.target->gotEntrySize;
714715
}
715716

716717
uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const {
717-
return b.getTlsGdIdx(ctx) * ctx.arg.wordsize;
718+
return b.getTlsGdIdx(ctx) * ctx.target->gotEntrySize;
718719
}
719720

720721
void GotSection::finalizeContents() {
@@ -723,7 +724,7 @@ void GotSection::finalizeContents() {
723724
!ctx.sym.globalOffsetTable)
724725
size = 0;
725726
else
726-
size = numEntries * ctx.arg.wordsize;
727+
size = numEntries * ctx.target->gotEntrySize;
727728
}
728729

729730
bool GotSection::isNeeded() const {
@@ -1199,7 +1200,7 @@ void MipsGotSection::writeTo(uint8_t *buf) {
11991200
// consistent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.
12001201
GotPltSection::GotPltSection(Ctx &ctx)
12011202
: SyntheticSection(ctx, ".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
1202-
ctx.arg.wordsize) {
1203+
ctx.target->gotEntrySize) {
12031204
if (ctx.arg.emachine == EM_PPC) {
12041205
name = ".plt";
12051206
} else if (ctx.arg.emachine == EM_PPC64) {

0 commit comments

Comments
 (0)