diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 6b4b8033bb6d..29fa7a6323ee 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -77,23 +77,41 @@ InputSectionBase::getOffset(const DefinedRegular &Sym) { return getOffset(Sym.Value); } -// Returns a section that Rel relocation is pointing to. template -InputSectionBase * -InputSectionBase::getRelocTarget(const Elf_Rel &Rel) const { - // Global symbol +static DefinedRegular *getRelocTargetSym(ObjectFile *File, + const typename ELFT::Rel &Rel) { uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL); SymbolBody &B = File->getSymbolBody(SymIndex).repl(); if (auto *D = dyn_cast>(&B)) if (D->Section) - return D->Section->Repl; + return D; return nullptr; } +// Returns a section that Rel relocation is pointing to. template -InputSectionBase * +std::pair *, typename ELFT::uint> +InputSectionBase::getRelocTarget(const Elf_Rel &Rel) const { + auto *D = getRelocTargetSym(File, Rel); + if (!D) + return std::make_pair(nullptr, 0); + if (!D->isSection()) + return std::make_pair(D->Section->Repl, D->Value); + const uint8_t *BufLoc = getSectionData().begin() + Rel.r_offset; + uintX_t Addend = + Target->getImplicitAddend(BufLoc, Rel.getType(Config->Mips64EL)); + return std::make_pair(D->Section->Repl, D->Value + Addend); +} + +template +std::pair *, typename ELFT::uint> InputSectionBase::getRelocTarget(const Elf_Rela &Rel) const { - return getRelocTarget(reinterpret_cast(Rel)); + auto *D = getRelocTargetSym(File, Rel); + if (!D) + return std::make_pair(nullptr, 0); + if (!D->isSection()) + return std::make_pair(D->Section->Repl, D->Value); + return std::make_pair(D->Section->Repl, D->Value + Rel.r_addend); } template @@ -368,10 +386,49 @@ typename ELFT::uint EHInputSection::getOffset(uintX_t Offset) { return Base + Addend; } +static size_t findNull(StringRef S, size_t EntSize) { + // Optimize the common case. + if (EntSize == 1) + return S.find(0); + + for (unsigned I = 0, N = S.size(); I != N; I += EntSize) { + const char *B = S.begin() + I; + if (std::all_of(B, B + EntSize, [](char C) { return C == 0; })) + return I; + } + return StringRef::npos; +} + template MergeInputSection::MergeInputSection(elf::ObjectFile *F, const Elf_Shdr *Header) - : SplitInputSection(F, Header, InputSectionBase::Merge) {} + : SplitInputSection(F, Header, InputSectionBase::Merge) { + uintX_t EntSize = Header->sh_entsize; + ArrayRef D = this->getSectionData(); + StringRef Data((const char *)D.data(), D.size()); + std::vector> &Offsets = this->Offsets; + + uintX_t V = Config->GcSections ? -1 : 0; + if (Header->sh_flags & SHF_STRINGS) { + uintX_t Offset = 0; + while (!Data.empty()) { + size_t End = findNull(Data, EntSize); + if (End == StringRef::npos) + fatal("string is not null terminated"); + Offsets.push_back(std::make_pair(Offset, V)); + uintX_t Size = End + EntSize; + Data = Data.substr(Size); + Offset += Size; + } + return; + } + + // If this is not of type string, every entry has the same size. + size_t Size = Data.size(); + assert((Size % EntSize) == 0); + for (unsigned I = 0, N = Size; I != N; I += EntSize) + Offsets.push_back(std::make_pair(I, V)); +} template bool MergeInputSection::classof(const InputSectionBase *S) { diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index d182e0ae0658..a97b5048b2d5 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -124,8 +124,10 @@ public: ArrayRef getSectionData() const; // Returns a section that Rel is pointing to. Used by the garbage collector. - InputSectionBase *getRelocTarget(const Elf_Rel &Rel) const; - InputSectionBase *getRelocTarget(const Elf_Rela &Rel) const; + std::pair *, uintX_t> + getRelocTarget(const Elf_Rel &Rel) const; + std::pair *, uintX_t> + getRelocTarget(const Elf_Rela &Rel) const; void relocate(uint8_t *Buf, uint8_t *BufEnd); std::vector Relocations; diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index 2511fc14ae4c..93d12a024cf1 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -40,20 +40,29 @@ using namespace lld::elf; // Calls Fn for each section that Sec refers to via relocations. template -static void forEachSuccessor(InputSection *Sec, - std::function *)> Fn) { +static void forEachSuccessor( + InputSection *Sec, + std::function *, typename ELFT::uint Offset)> + Fn) { typedef typename ELFT::Rel Elf_Rel; typedef typename ELFT::Rela Elf_Rela; typedef typename ELFT::Shdr Elf_Shdr; + typedef typename ELFT::uint uintX_t; ELFFile &Obj = Sec->getFile()->getObj(); for (const Elf_Shdr *RelSec : Sec->RelocSections) { if (RelSec->sh_type == SHT_RELA) { - for (const Elf_Rela &RI : Obj.relas(RelSec)) - Fn(Sec->getRelocTarget(RI)); + for (const Elf_Rela &RI : Obj.relas(RelSec)) { + std::pair *, uintX_t> P = + Sec->getRelocTarget(RI); + Fn(P.first, P.second); + } } else { - for (const Elf_Rel &RI : Obj.rels(RelSec)) - Fn(Sec->getRelocTarget(RI)); + for (const Elf_Rel &RI : Obj.rels(RelSec)) { + std::pair *, uintX_t> P = + Sec->getRelocTarget(RI); + Fn(P.first, P.second); + } } } } @@ -85,10 +94,18 @@ template static bool isReserved(InputSectionBase *Sec) { // Starting from GC-root sections, this function visits all reachable // sections to set their "Live" bits. template void elf::markLive(SymbolTable *Symtab) { + typedef typename ELFT::uint uintX_t; SmallVector *, 256> Q; - auto Enqueue = [&](InputSectionBase *Sec) { - if (!Sec || Sec->Live) + auto Enqueue = [&](InputSectionBase *Sec, uintX_t Offset) { + if (!Sec) + return; + if (auto *MS = dyn_cast>(Sec)) { + std::pair *, uintX_t> T = + MS->getRangeAndSize(Offset); + T.first->second = 0; + } + if (Sec->Live) return; Sec->Live = true; if (InputSection *S = dyn_cast>(Sec)) @@ -98,7 +115,7 @@ template void elf::markLive(SymbolTable *Symtab) { auto MarkSymbol = [&](SymbolBody *Sym) { if (Sym) if (auto *D = dyn_cast>(Sym)) - Enqueue(D->Section); + Enqueue(D->Section, D->Value); }; // Add GC root symbols. @@ -125,7 +142,7 @@ template void elf::markLive(SymbolTable *Symtab) { for (InputSectionBase *Sec : F->getSections()) if (Sec && Sec != &InputSection::Discarded) if (isReserved(Sec) || Script::X->shouldKeep(Sec)) - Enqueue(Sec); + Enqueue(Sec, 0); // Mark all reachable sections. while (!Q.empty()) diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 9052aec88f82..1add512982b1 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -1132,7 +1132,7 @@ void EHOutputSection::addSectionAux(EHInputSection *S, } else { if (!HasReloc) fatal("FDE doesn't reference another section"); - InputSectionBase *Target = S->getRelocTarget(*RelI); + InputSectionBase *Target = S->getRelocTarget(*RelI).first; if (Target && Target->Live) { uint32_t CieOffset = Offset + 4 - ID; auto I = OffsetToIndex.find(CieOffset); @@ -1227,19 +1227,6 @@ template void MergeOutputSection::writeTo(uint8_t *Buf) { } } -static size_t findNull(StringRef S, size_t EntSize) { - // Optimize the common case. - if (EntSize == 1) - return S.find(0); - - for (unsigned I = 0, N = S.size(); I != N; I += EntSize) { - const char *B = S.begin() + I; - if (std::all_of(B, B + EntSize, [](char C) { return C == 0; })) - return I; - } - return StringRef::npos; -} - template void MergeOutputSection::addSection(InputSectionBase *C) { auto *S = cast>(C); @@ -1250,31 +1237,32 @@ void MergeOutputSection::addSection(InputSectionBase *C) { StringRef Data((const char *)D.data(), D.size()); uintX_t EntSize = S->getSectionHdr()->sh_entsize; this->Header.sh_entsize = EntSize; + MutableArrayRef> Offsets = S->Offsets; // If this is of type string, the contents are null-terminated strings. if (this->Header.sh_flags & SHF_STRINGS) { - uintX_t Offset = 0; - while (!Data.empty()) { - size_t End = findNull(Data, EntSize); - if (End == StringRef::npos) - fatal("string is not null terminated"); - StringRef Entry = Data.substr(0, End + EntSize); + for (unsigned I = 0, N = Offsets.size(); I != N; ++I) { + auto &P = Offsets[I]; + if (P.second == (uintX_t)-1) + continue; + + uintX_t Start = P.first; + uintX_t End = (I == N - 1) ? Data.size() : Offsets[I + 1].first; + StringRef Entry = Data.substr(Start, End - Start); uintX_t OutputOffset = Builder.add(Entry); if (shouldTailMerge()) OutputOffset = -1; - S->Offsets.push_back(std::make_pair(Offset, OutputOffset)); - uintX_t Size = End + EntSize; - Data = Data.substr(Size); - Offset += Size; + P.second = OutputOffset; } return; } // If this is not of type string, every entry has the same size. - for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) { - StringRef Entry = Data.substr(I, EntSize); - size_t OutputOffset = Builder.add(Entry); - S->Offsets.push_back(std::make_pair(I, OutputOffset)); + for (auto &P : Offsets) { + if (P.second == (uintX_t)-1) + continue; + StringRef Entry = Data.substr(P.first, EntSize); + P.second = Builder.add(Entry); } } diff --git a/lld/test/ELF/gc-sections-merge-addend.s b/lld/test/ELF/gc-sections-merge-addend.s new file mode 100644 index 000000000000..8595f5802be5 --- /dev/null +++ b/lld/test/ELF/gc-sections-merge-addend.s @@ -0,0 +1,39 @@ +// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux +// RUN: ld.lld %t.o -o %t.so -shared --gc-sections +// RUN: llvm-readobj -s -section-data %t.so | FileCheck %s + + +// CHECK: Name: .rodata +// CHECK-NEXT: Type: SHT_PROGBITS +// CHECK-NEXT: Flags [ +// CHECK-NEXT: SHF_ALLOC +// CHECK-NEXT: SHF_MERGE +// CHECK-NEXT: SHF_STRINGS +// CHECK-NEXT: ] +// CHECK-NEXT: Address: +// CHECK-NEXT: Offset: +// CHECK-NEXT: Size: 4 +// CHECK-NEXT: Link: 0 +// CHECK-NEXT: Info: 0 +// CHECK-NEXT: AddressAlignment: 1 +// CHECK-NEXT: EntrySize: 1 +// CHECK-NEXT: SectionData ( +// CHECK-NEXT: 0000: 62617200 |bar.| +// CHECK-NEXT: ) + + .section .data.f,"aw",@progbits + .globl f +f: + .quad .rodata.str1.1 + 4 + + .section .data.g,"aw",@progbits + .hidden g + .globl g +g: + .quad .rodata.str1.1 + + .section .rodata.str1.1,"aMS",@progbits,1 +.L.str: + .asciz "foo" +.L.str.1: + .asciz "bar" diff --git a/lld/test/ELF/gc-sections-merge-implicit-addend.s b/lld/test/ELF/gc-sections-merge-implicit-addend.s new file mode 100644 index 000000000000..8a7c804a830a --- /dev/null +++ b/lld/test/ELF/gc-sections-merge-implicit-addend.s @@ -0,0 +1,39 @@ +// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=i386-pc-linux +// RUN: ld.lld %t.o -o %t.so -shared --gc-sections +// RUN: llvm-readobj -s -section-data %t.so | FileCheck %s + + +// CHECK: Name: .rodata +// CHECK-NEXT: Type: SHT_PROGBITS +// CHECK-NEXT: Flags [ +// CHECK-NEXT: SHF_ALLOC +// CHECK-NEXT: SHF_MERGE +// CHECK-NEXT: SHF_STRINGS +// CHECK-NEXT: ] +// CHECK-NEXT: Address: +// CHECK-NEXT: Offset: +// CHECK-NEXT: Size: 4 +// CHECK-NEXT: Link: 0 +// CHECK-NEXT: Info: 0 +// CHECK-NEXT: AddressAlignment: 1 +// CHECK-NEXT: EntrySize: 1 +// CHECK-NEXT: SectionData ( +// CHECK-NEXT: 0000: 62617200 |bar.| +// CHECK-NEXT: ) + + .section .data.f,"aw",@progbits + .globl f +f: + .long .rodata.str1.1 + 4 + + .section .data.g,"aw",@progbits + .hidden g + .globl g +g: + .long .rodata.str1.1 + + .section .rodata.str1.1,"aMS",@progbits,1 +.L.str: + .asciz "foo" +.L.str.1: + .asciz "bar" diff --git a/lld/test/ELF/gc-sections-merge.s b/lld/test/ELF/gc-sections-merge.s new file mode 100644 index 000000000000..ef2688659871 --- /dev/null +++ b/lld/test/ELF/gc-sections-merge.s @@ -0,0 +1,61 @@ +// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux +// RUN: ld.lld %t.o -o %t.so -shared +// RUN: ld.lld %t.o -o %t.gc.so -shared --gc-sections +// RUN: llvm-readobj -s -section-data %t.so | FileCheck %s +// RUN: llvm-readobj -s -section-data %t.gc.so | FileCheck --check-prefix=GC %s + + +// CHECK: Name: .rodata +// CHECK-NEXT: Type: SHT_PROGBITS +// CHECK-NEXT: Flags [ +// CHECK-NEXT: SHF_ALLOC +// CHECK-NEXT: SHF_MERGE +// CHECK-NEXT: SHF_STRINGS +// CHECK-NEXT: ] +// CHECK-NEXT: Address: +// CHECK-NEXT: Offset: +// CHECK-NEXT: Size: 8 +// CHECK-NEXT: Link: 0 +// CHECK-NEXT: Info: 0 +// CHECK-NEXT: AddressAlignment: 1 +// CHECK-NEXT: EntrySize: 1 +// CHECK-NEXT: SectionData ( +// CHECK-NEXT: 0000: 666F6F00 62617200 |foo.bar.| +// CHECK-NEXT: ) + +// GC: Name: .rodata +// GC-NEXT: Type: SHT_PROGBITS +// GC-NEXT: Flags [ +// GC-NEXT: SHF_ALLOC +// GC-NEXT: SHF_MERGE +// GC-NEXT: SHF_STRINGS +// GC-NEXT: ] +// GC-NEXT: Address: +// GC-NEXT: Offset: +// GC-NEXT: Size: 4 +// GC-NEXT: Link: 0 +// GC-NEXT: Info: 0 +// GC-NEXT: AddressAlignment: 1 +// GC-NEXT: EntrySize: 1 +// GC-NEXT: SectionData ( +// GC-NEXT: 0000: 666F6F00 |foo.| +// GC-NEXT: ) + + .section .text.f,"ax",@progbits + .globl f +f: + leaq .L.str(%rip), %rax + retq + + .section .text.g,"ax",@progbits + .hidden g + .globl g +g: + leaq .L.str.1(%rip), %rax + retq + + .section .rodata.str1.1,"aMS",@progbits,1 +.L.str: + .asciz "foo" +.L.str.1: + .asciz "bar"