From c65ae14766f0802fd33d4fa9c1b2a41a2ad1c412 Mon Sep 17 00:00:00 2001 From: Zaara Syeda Date: Mon, 26 Mar 2018 17:50:52 +0000 Subject: [PATCH] [ELF] GotSection increment NumEntries when Target saves GlobalOffsetTable in the .got When the target saves ElfSym::GlobalOffsetTable in the .got rather than .got.plt, Target->GotHeaderEntriesNum states the number of extra entries required in the .got. Rather than having to add Target->GotHeaderEntriesNum to NumEntries in every function which refers to NumEntries, this patch changes the initial value of NumEntries in the constructor. Differential Revision: https://reviews.llvm.org/D44744 llvm-svn: 328559 --- lld/ELF/SyntheticSections.cpp | 14 +++++++++++--- lld/test/ELF/ppc64_entry_point.s | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index c55b4fe8626b..fb5b3238bccc 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -587,10 +587,18 @@ void EhFrameSection::writeTo(uint8_t *Buf) { GotSection::GotSection() : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, - Target->GotEntrySize, ".got") {} + Target->GotEntrySize, ".got") { + // PPC64 saves the ElfSym::GlobalOffsetTable .TOC. as the first entry in the + // .got. If there are no references to .TOC. in the symbol table, + // ElfSym::GlobalOffsetTable will not be defined and we won't need to save + // .TOC. in the .got. When it is defined, we increase NumEntries by the number + // of entries used to emit ElfSym::GlobalOffsetTable. + if (ElfSym::GlobalOffsetTable && !Target->GotBaseSymInGotPlt) + NumEntries += Target->GotHeaderEntriesNum; +} void GotSection::addEntry(Symbol &Sym) { - Sym.GotIndex = Target->GotHeaderEntriesNum + NumEntries; + Sym.GotIndex = NumEntries; ++NumEntries; } @@ -622,7 +630,7 @@ uint64_t GotSection::getGlobalDynOffset(const Symbol &B) const { } void GotSection::finalizeContents() { - Size = (NumEntries + Target->GotHeaderEntriesNum) * Config->Wordsize; + Size = NumEntries * Config->Wordsize; } bool GotSection::empty() const { diff --git a/lld/test/ELF/ppc64_entry_point.s b/lld/test/ELF/ppc64_entry_point.s index 733c4cc32bc5..b3bd5ec5a923 100644 --- a/lld/test/ELF/ppc64_entry_point.s +++ b/lld/test/ELF/ppc64_entry_point.s @@ -1,7 +1,7 @@ # REQUIRES: ppc # RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t # RUN: ld.lld %t -o %t2 -# RUN: llvm-objdump -d %t2 | FileCheck %s +# RUN: llvm-objdump -D %t2 | FileCheck %s .text .abiversion 2 @@ -31,3 +31,6 @@ _start: // CHECK-NEXT: 10010004: 00 00 84 38 addi 4, 4, 0 // CHECK-NEXT: 10010008: 02 00 a0 3c lis 5, 2 // CHECK-NEXT: 1001000c: 00 80 a5 38 addi 5, 5, -32768 +// CHECK: Disassembly of section .got: +// CHECK-NEXT: .got: +// CHECK-NEXT: 10020000: 00 80 02 10