From 803b120ba1f765014b33a0aa59bae7e24f6ce547 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 13 Jul 2016 18:55:14 +0000 Subject: [PATCH] Add GotEntrySize/GotPltEntrySize to ELF target. Patch by H.J Lu. For x86-64 psABI, the entry size of .got and .got.plt sections is 8 bytes for both LP64 and ILP32. Add GotEntrySize and GotPltEntrySize to ELF target instead of using size of ELFT::uint. Now we can generate a simple working x32 executable. Differential Revision: http://reviews.llvm.org/D22288 llvm-svn: 275301 --- lld/ELF/OutputSections.cpp | 10 +++++----- lld/ELF/Symbols.cpp | 4 ++-- lld/ELF/Target.cpp | 12 ++++++++++++ lld/ELF/Target.h | 2 ++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index a3d228c0b22b..7c7ec0a05c80 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -47,7 +47,7 @@ void OutputSectionBase::writeHeaderTo(Elf_Shdr *Shdr) { template GotPltSection::GotPltSection() : OutputSectionBase(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { - this->Header.sh_addralign = sizeof(uintX_t); + this->Header.sh_addralign = Target->GotPltEntrySize; } template void GotPltSection::addEntry(SymbolBody &Sym) { @@ -60,13 +60,13 @@ template bool GotPltSection::empty() const { } template void GotPltSection::finalize() { - this->Header.sh_size = - (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t); + this->Header.sh_size = (Target->GotPltHeaderEntriesNum + Entries.size()) * + Target->GotPltEntrySize; } template void GotPltSection::writeTo(uint8_t *Buf) { Target->writeGotPltHeader(Buf); - Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t); + Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize; for (const SymbolBody *B : Entries) { Target->writeGotPlt(Buf, *B); Buf += sizeof(uintX_t); @@ -78,7 +78,7 @@ GotSection::GotSection() : OutputSectionBase(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { if (Config->EMachine == EM_MIPS) this->Header.sh_flags |= SHF_MIPS_GPREL; - this->Header.sh_addralign = sizeof(uintX_t); + this->Header.sh_addralign = Target->GotEntrySize; } template diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp index 85c71e301c62..0c3a116b371c 100644 --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -165,7 +165,7 @@ template typename ELFT::uint SymbolBody::getGotVA() const { } template typename ELFT::uint SymbolBody::getGotOffset() const { - return GotIndex * sizeof(typename ELFT::uint); + return GotIndex * Target->GotEntrySize; } template typename ELFT::uint SymbolBody::getGotPltVA() const { @@ -173,7 +173,7 @@ template typename ELFT::uint SymbolBody::getGotPltVA() const { } template typename ELFT::uint SymbolBody::getGotPltOffset() const { - return GotPltIndex * sizeof(typename ELFT::uint); + return GotPltIndex * Target->GotPltEntrySize; } template typename ELFT::uint SymbolBody::getPltVA() const { diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp index 8e520fbfaf15..6908d8ffe3f3 100644 --- a/lld/ELF/Target.cpp +++ b/lld/ELF/Target.cpp @@ -308,6 +308,8 @@ X86TargetInfo::X86TargetInfo() { TlsGotRel = R_386_TLS_TPOFF; TlsModuleIndexRel = R_386_TLS_DTPMOD32; TlsOffsetRel = R_386_TLS_DTPOFF32; + GotEntrySize = 4; + GotPltEntrySize = 4; PltEntrySize = 16; PltHeaderSize = 16; TlsGdRelaxSkip = 2; @@ -551,6 +553,8 @@ template X86_64TargetInfo::X86_64TargetInfo() { TlsGotRel = R_X86_64_TPOFF64; TlsModuleIndexRel = R_X86_64_DTPMOD64; TlsOffsetRel = R_X86_64_DTPOFF64; + GotEntrySize = 8; + GotPltEntrySize = 8; PltEntrySize = 16; PltHeaderSize = 16; TlsGdRelaxSkip = 2; @@ -976,6 +980,8 @@ RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { PPC64TargetInfo::PPC64TargetInfo() { PltRel = GotRel = R_PPC64_GLOB_DAT; RelativeRel = R_PPC64_RELATIVE; + GotEntrySize = 8; + GotPltEntrySize = 8; PltEntrySize = 32; PltHeaderSize = 0; @@ -1140,6 +1146,8 @@ AArch64TargetInfo::AArch64TargetInfo() { PltRel = R_AARCH64_JUMP_SLOT; TlsDescRel = R_AARCH64_TLSDESC; TlsGotRel = R_AARCH64_TLS_TPREL64; + GotEntrySize = 8; + GotPltEntrySize = 8; PltEntrySize = 16; PltHeaderSize = 32; @@ -1480,6 +1488,8 @@ ARMTargetInfo::ARMTargetInfo() { TlsGotRel = R_ARM_TLS_TPOFF32; TlsModuleIndexRel = R_ARM_TLS_DTPMOD32; TlsOffsetRel = R_ARM_TLS_DTPOFF32; + GotEntrySize = 4; + GotPltEntrySize = 4; PltEntrySize = 16; PltHeaderSize = 20; } @@ -1787,6 +1797,8 @@ uint64_t ARMTargetInfo::getImplicitAddend(const uint8_t *Buf, template MipsTargetInfo::MipsTargetInfo() { GotPltHeaderEntriesNum = 2; PageSize = 65536; + GotEntrySize = sizeof(typename ELFT::uint); + GotPltEntrySize = sizeof(typename ELFT::uint); PltEntrySize = 16; PltHeaderSize = 32; CopyRel = R_MIPS_COPY; diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h index c1efb20c38d6..df4ceb17c98a 100644 --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -81,6 +81,8 @@ public: uint32_t TlsGotRel; uint32_t TlsModuleIndexRel; uint32_t TlsOffsetRel; + unsigned GotEntrySize; + unsigned GotPltEntrySize; unsigned PltEntrySize; unsigned PltHeaderSize;