From e8a6102fa9ae49813b12871dc2bd48b8523948c2 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 5 Nov 2016 23:05:47 +0000 Subject: [PATCH] Rewrite CommonInputSection as a synthetic input section. A CommonInputSection is a section containing all common symbols. That was an input section but was abstracted in a different way than the synthetic input sections because it was written before the synthetic input section was invented. This patch rewrites CommonInputSection as a synthetic input section so that it behaves better with other sections. llvm-svn: 286053 --- lld/ELF/InputSection.cpp | 25 ----------------- lld/ELF/InputSection.h | 11 -------- lld/ELF/LinkerScript.cpp | 8 ++---- lld/ELF/OutputSections.cpp | 3 +- lld/ELF/Symbols.cpp | 4 +-- lld/ELF/SyntheticSections.cpp | 43 ++++++++++++++++++++++++++++ lld/ELF/SyntheticSections.h | 12 ++++++-- lld/ELF/Writer.cpp | 51 +++++++++++++--------------------- lld/test/ELF/linkerscript/double-bss.s | 11 ++++++-- 9 files changed, 87 insertions(+), 81 deletions(-) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index efff4b5..d1c96fd 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -825,31 +825,6 @@ bool MipsAbiFlagsInputSection::classof(const InputSectionData *S) { return S->kind() == InputSectionBase::MipsAbiFlags; } -template -InputSection InputSection::createCommonInputSection( - std::vector Syms) { - // Sort the common symbols by alignment as an heuristic to pack them better. - std::stable_sort(Syms.begin(), Syms.end(), - [](const DefinedCommon *A, const DefinedCommon *B) { - return A->Alignment > B->Alignment; - }); - - size_t Size = 0; - uintX_t Alignment = 1; - for (DefinedCommon *Sym : Syms) { - Alignment = std::max(Alignment, Sym->Alignment); - Size = alignTo(Size, Sym->Alignment); - - // Compute symbol offset relative to beginning of input section. - Sym->Offset = Size; - Size += Sym->Size; - } - ArrayRef Data = makeArrayRef(nullptr, Size); - InputSection Ret(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, Alignment, Data, ""); - Ret.Live = true; - return Ret; -} - template class elf::InputSectionBase; template class elf::InputSectionBase; template class elf::InputSectionBase; diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index d54d019..62fc5e6 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -273,14 +273,6 @@ public: template void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef Rels); - // Common symbols don't belong to any section. But it is easier for us - // to handle them as if they belong to some input section. So we defined - // this section that "contains" all common symbols. - static InputSection *CommonInputSection; - - static InputSection - createCommonInputSection(std::vector Syms); - private: template void copyRelocations(uint8_t *Buf, llvm::ArrayRef Rels); @@ -294,9 +286,6 @@ private: llvm::TinyPtrVector *> Thunks; }; -template -InputSection *InputSection::CommonInputSection; - // MIPS .reginfo section provides information on the registers used by the code // in the object file. Linker should collect this information and write a single // .reginfo section in the output file. The output section contains a union of diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index dd46fae..53d2cc5 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -202,13 +202,9 @@ void LinkerScript::computeInputSections(InputSectionDescription *I) { if (elf::ObjectFile *F = S->getFile()) Filename = sys::path::filename(F->getName()); - if (!I->FilePat.match(Filename) || Pat.ExcludedFilePat.match(Filename)) - continue; - - if (Pat.SectionPat.match(S->Name)) + if (I->FilePat.match(Filename) && !Pat.ExcludedFilePat.match(Filename) && + Pat.SectionPat.match(S->Name)) I->Sections.push_back(S); - if (Pat.SectionPat.match("COMMON")) - I->Sections.push_back(InputSection::CommonInputSection); } // Sort sections as instructed by SORT-family commands and --sort-section diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 9179d19..fa39213 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -15,6 +15,7 @@ #include "Memory.h" #include "Strings.h" #include "SymbolTable.h" +#include "SyntheticSections.h" #include "Target.h" #include "lld/Core/Parallel.h" #include "llvm/Support/Dwarf.h" @@ -1556,7 +1557,7 @@ SymbolTableSection::getOutputSection(SymbolBody *Sym) { break; } case SymbolBody::DefinedCommonKind: - return InputSection::CommonInputSection->OutSec; + return In::Common->OutSec; case SymbolBody::SharedKind: if (cast>(Sym)->needsCopy()) return Out::Bss; diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp index f59132a..1c8c9a8 100644 --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -12,6 +12,7 @@ #include "InputFiles.h" #include "InputSection.h" #include "OutputSections.h" +#include "SyntheticSections.h" #include "Target.h" #include "llvm/ADT/STLExtras.h" @@ -69,8 +70,7 @@ static typename ELFT::uint getSymVA(const SymbolBody &Body, return VA; } case SymbolBody::DefinedCommonKind: - return InputSection::CommonInputSection->OutSec->getVA() + - InputSection::CommonInputSection->OutSecOff + + return In::Common->OutSec->getVA() + In::Common->OutSecOff + cast(Body).Offset; case SymbolBody::SharedKind: { auto &SS = cast>(Body); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 0026566..09183d0 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -21,6 +21,7 @@ #include "Memory.h" #include "OutputSections.h" #include "Strings.h" +#include "SymbolTable.h" #include "llvm/Support/Endian.h" #include "llvm/Support/MD5.h" @@ -37,6 +38,43 @@ using namespace llvm::support::endian; using namespace lld; using namespace lld::elf; +template static std::vector getCommonSymbols() { + std::vector V; + for (Symbol *S : Symtab::X->getSymbols()) + if (auto *B = dyn_cast(S->body())) + V.push_back(B); + return V; +} + +// Find all common symbols and allocate space for them. +template +CommonSection::CommonSection() + : InputSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1, + ArrayRef(), "COMMON") { + this->Live = true; + + // Sort the common symbols by alignment as an heuristic to pack them better. + std::vector Syms = getCommonSymbols(); + std::stable_sort(Syms.begin(), Syms.end(), + [](const DefinedCommon *A, const DefinedCommon *B) { + return A->Alignment > B->Alignment; + }); + + // Assign offsets to symbols. + size_t Size = 0; + size_t Alignment = 1; + for (DefinedCommon *Sym : Syms) { + Alignment = std::max(Alignment, Sym->Alignment); + Size = alignTo(Size, Sym->Alignment); + + // Compute symbol offset relative to beginning of input section. + Sym->Offset = Size; + Size += Sym->Size; + } + this->Alignment = Alignment; + this->Data = makeArrayRef(nullptr, Size); +} + static ArrayRef createInterp() { // StringSaver guarantees that the returned string ends with '\0'. StringRef S = Saver.save(Config->DynamicLinker); @@ -111,6 +149,11 @@ void BuildIdHexstring::writeBuildId(MutableArrayRef Buf) { Config->BuildIdVector.size()); } +template class elf::CommonSection; +template class elf::CommonSection; +template class elf::CommonSection; +template class elf::CommonSection; + template class elf::InterpSection; template class elf::InterpSection; template class elf::InterpSection; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index a98d94e..5f122e5 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -15,11 +15,19 @@ namespace lld { namespace elf { +// This class represents a BSS section containing all common symbols. +template class CommonSection final : public InputSection { +public: + CommonSection(); +}; + +// .interp section. template class InterpSection final : public InputSection { public: InterpSection(); }; +// .note.gnu.build-id section. template class BuildIdSection : public InputSection { public: virtual void writeBuildId(llvm::MutableArrayRef Buf) = 0; @@ -67,13 +75,13 @@ public: // Linker generated sections which can be used as inputs. template struct In { static BuildIdSection *BuildId; + static CommonSection *Common; static InterpSection *Interp; - static std::vector *> Sections; }; template BuildIdSection *In::BuildId; +template CommonSection *In::Common; template InterpSection *In::Interp; -template std::vector *> In::Sections; } // namespace elf } // namespace lld diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 18dbccf..18d1c7d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -105,6 +105,11 @@ StringRef elf::getOutputSectionName(StringRef Name) { return Prefix; } + // CommonSection is identified as "COMMON" in linker scripts. + // By default, it should go to .bss section. + if (Name == "COMMON") + return ".bss"; + // ".zdebug_" is a prefix for ZLIB-compressed sections. // Because we decompressed input sections, we want to remove 'z'. if (Name.startswith(".zdebug_")) @@ -130,14 +135,6 @@ template void elf::writeResult() { Writer().run(); } -template static std::vector getCommonSymbols() { - std::vector V; - for (Symbol *S : Symtab::X->getSymbols()) - if (auto *B = dyn_cast(S->body())) - V.push_back(B); - return V; -} - // The main function of the writer. template void Writer::run() { createSyntheticSections(); @@ -146,10 +143,6 @@ template void Writer::run() { if (Target->NeedsThunks) forEachRelSec(createThunks); - InputSection Common = - InputSection::createCommonInputSection(getCommonSymbols()); - InputSection::CommonInputSection = &Common; - Script::X->OutputSections = &OutputSections; if (ScriptConfig->HasSections) { Script::X->createSections(Factory); @@ -234,8 +227,12 @@ template void Writer::createSyntheticSections() { Out::ProgramHeaders = make>("", 0, SHF_ALLOC); Out::ProgramHeaders->updateAlignment(sizeof(uintX_t)); - if (needsInterpSection()) + if (needsInterpSection()) { In::Interp = make>(); + Symtab::X->Sections.push_back(In::Interp); + } else { + In::Interp = nullptr; + } if (!Symtab::X->getSharedFiles().empty() || Config->Pic) { Out::DynSymTab = @@ -284,8 +281,17 @@ template void Writer::createSyntheticSections() { In::BuildId = make>(); else if (Config->BuildId == BuildIdKind::Hexstring) In::BuildId = make>(); + else + In::BuildId = nullptr; - In::Sections = {In::BuildId, In::Interp}; + if (In::BuildId) + Symtab::X->Sections.push_back(In::BuildId); + + CommonSection *Common = make>(); + if (!Common->Data.empty()) { + In::Common = Common; + Symtab::X->Sections.push_back(Common); + } } template @@ -803,22 +809,10 @@ template void Writer::finalizeSections() { if (HasError) return; - // If linker script processor hasn't added common symbol section yet, - // then add it to .bss now. - if (!InputSection::CommonInputSection->OutSec) { - Out::Bss->addSection(InputSection::CommonInputSection); - Out::Bss->assignOffsets(); - } - // So far we have added sections from input object files. // This function adds linker-created Out::* sections. addPredefinedSections(); - // Adds linker generated input sections to - // corresponding output sections. - for (InputSection *S : In::Sections) - addInputSec(S); - sortSections(); unsigned I = 1; @@ -827,11 +821,6 @@ template void Writer::finalizeSections() { Sec->setSHName(Out::ShStrTab->addString(Sec->getName())); } - // Finalize linker generated sections. - for (InputSection *S : In::Sections) - if (S && S->OutSec) - S->OutSec->assignOffsets(); - // Finalizers fix each section's size. // .dynsym is finalized early since that may fill up .gnu.hash. if (Out::DynSymTab) diff --git a/lld/test/ELF/linkerscript/double-bss.s b/lld/test/ELF/linkerscript/double-bss.s index 9f04733..c24332f 100644 --- a/lld/test/ELF/linkerscript/double-bss.s +++ b/lld/test/ELF/linkerscript/double-bss.s @@ -1,10 +1,15 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t -# RUN: echo "SECTIONS { . = SIZEOF_HEADERS; .text : { *(.text*) } }" > %t.script +# RUN: echo "SECTIONS { . = SIZEOF_HEADERS; " > %t.script +# RUN: echo ".text : { *(.text*) }" >> %t.script +# RUN: echo ".bss1 : { *(.bss) }" >> %t.script +# RUN: echo ".bss2 : { *(COMMON) }" >> %t.script +# RUN: echo "}" >> %t.script + # RUN: ld.lld -o %t1 --script %t.script %t # RUN: llvm-objdump -section-headers %t1 | FileCheck %s -# CHECK: .bss 00000004 0000000000000122 BSS -# CHECK-NEXT: .bss 00000080 0000000000000128 BSS +# CHECK: .bss1 00000004 0000000000000122 BSS +# CHECK-NEXT: .bss2 00000080 0000000000000128 BSS .globl _start _start: -- 2.7.4