From 14f996dca8a2b5b17d6917528bfd9ee71ba6192a Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 16 Oct 2022 00:49:48 -0700 Subject: [PATCH] [ELF] Move inputSections/ehInputSections into Ctx. NFC --- lld/ELF/Arch/PPC64.cpp | 2 +- lld/ELF/Config.h | 3 +++ lld/ELF/Driver.cpp | 18 +++++++++--------- lld/ELF/ICF.cpp | 2 +- lld/ELF/InputSection.cpp | 2 -- lld/ELF/InputSection.h | 4 ---- lld/ELF/LinkerScript.cpp | 8 ++++---- lld/ELF/MarkLive.cpp | 10 +++++----- lld/ELF/SyntheticSections.cpp | 14 +++++++------- lld/ELF/Target.cpp | 2 +- lld/ELF/Writer.cpp | 38 +++++++++++++++++++------------------- 11 files changed, 50 insertions(+), 53 deletions(-) diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp index c2834f0..b33adef 100644 --- a/lld/ELF/Arch/PPC64.cpp +++ b/lld/ELF/Arch/PPC64.cpp @@ -233,7 +233,7 @@ static void writeSequence(MutableArrayRef buf, const char *prefix, makeArrayRef(reinterpret_cast(buf.data() + first), 4 * (buf.size() - first)), ".text"); - inputSections.push_back(sec); + ctx.inputSections.push_back(sec); for (Defined *sym : defined) { sym->section = sec; sym->value -= 4 * first; diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index c5da9b4..1a69bd8 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -37,6 +37,7 @@ class BitcodeFile; class ELFFileBase; class SharedFile; class InputSectionBase; +class EhInputSection; class Symbol; class BitcodeCompiler; @@ -418,6 +419,8 @@ struct Ctx { SmallVector binaryFiles; SmallVector bitcodeFiles; SmallVector lazyBitcodeFiles; + SmallVector inputSections; + SmallVector ehInputSections; // Duplicate symbol candidates. SmallVector duplicates; // Symbols in a non-prevailing COMDAT group which should be changed to an diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 0271f31..dd6a77a 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -95,6 +95,8 @@ void Ctx::reset() { binaryFiles.clear(); bitcodeFiles.clear(); lazyBitcodeFiles.clear(); + inputSections.clear(); + ehInputSections.clear(); duplicates.clear(); nonPrevailingSyms.clear(); whyExtractRecords.clear(); @@ -114,8 +116,6 @@ bool elf::link(ArrayRef args, llvm::raw_ostream &stdoutOS, elf::ctx.reset(); symtab = SymbolTable(); - inputSections.clear(); - ehInputSections.clear(); outputSections.clear(); symAux.clear(); @@ -2013,7 +2013,7 @@ static void replaceCommonSymbols() { auto *bss = make("COMMON", s->size, s->alignment); bss->file = s->file; - inputSections.push_back(bss); + ctx.inputSections.push_back(bss); Defined(s->file, StringRef(), s->binding, s->stOther, s->type, /*value=*/0, s->size, bss) .overwrite(*s); @@ -2702,20 +2702,20 @@ void LinkerDriver::link(opt::InputArgList &args) { if (!s || s == &InputSection::discarded) continue; if (LLVM_UNLIKELY(isa(s))) - ehInputSections.push_back(cast(s)); + ctx.ehInputSections.push_back(cast(s)); else - inputSections.push_back(s); + ctx.inputSections.push_back(s); } } for (BinaryFile *f : ctx.binaryFiles) for (InputSectionBase *s : f->getSections()) - inputSections.push_back(cast(s)); + ctx.inputSections.push_back(cast(s)); } { llvm::TimeTraceScope timeScope("Strip sections"); if (ctx.hasSympart.load(std::memory_order_relaxed)) { - llvm::erase_if(inputSections, [](InputSectionBase *s) { + llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { if (s->type != SHT_LLVM_SYMPART) return false; invokeELFT(readSymbolPartitionSection, s); @@ -2725,7 +2725,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // We do not want to emit debug sections if --strip-all // or --strip-debug are given. if (config->strip != StripPolicy::None) { - llvm::erase_if(inputSections, [](InputSectionBase *s) { + llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { if (isDebugSection(*s)) return true; if (auto *isec = dyn_cast(s)) @@ -2782,7 +2782,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // This adds a .comment section containing a version string. if (!config->relocatable) - inputSections.push_back(createCommentSection()); + ctx.inputSections.push_back(createCommentSection()); // Split SHF_MERGE and .eh_frame sections into pieces in preparation for garbage collection. invokeELFT(splitSections); diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp index 3e76a9b..169826a 100644 --- a/lld/ELF/ICF.cpp +++ b/lld/ELF/ICF.cpp @@ -477,7 +477,7 @@ template void ICF::run() { [&](InputSection &s) { s.eqClass[0] = s.eqClass[1] = ++uniqueId; }); // Collect sections to merge. - for (InputSectionBase *sec : inputSections) { + for (InputSectionBase *sec : ctx.inputSections) { auto *s = dyn_cast(sec); if (s && s->eqClass[0] == 0) { if (isEligible(s)) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index a599786..aa41da62 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -33,8 +33,6 @@ using namespace llvm::sys; using namespace lld; using namespace lld::elf; -SmallVector elf::inputSections; -SmallVector elf::ehInputSections; DenseSet> elf::ppc64noTocRelax; // Returns a string to construct an error message. diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index 0663b27..6cb2081 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -423,10 +423,6 @@ inline bool isDebugSection(const InputSectionBase &sec) { sec.name.startswith(".debug"); } -// The list of all input sections. -extern SmallVector inputSections; -extern SmallVector ehInputSections; - // The set of TOC entries (.toc + addend) for which we should not apply // toc-indirect to toc-relative relaxation. const Symbol * refers to the // STT_SECTION symbol associated to the .toc input section. diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index e19da00..7c34f02 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -589,7 +589,7 @@ LinkerScript::createInputSectionList(OutputSection &outCmd) { for (SectionCommand *cmd : outCmd.commands) { if (auto *isd = dyn_cast(cmd)) { - isd->sectionBases = computeInputSections(isd, inputSections); + isd->sectionBases = computeInputSections(isd, ctx.inputSections); for (InputSectionBase *s : isd->sectionBases) s->parent = &outCmd; ret.insert(ret.end(), isd->sectionBases.begin(), isd->sectionBases.end()); @@ -847,10 +847,10 @@ void LinkerScript::addOrphanSections() { // to create target sections first. We do not want priority handling // for synthetic sections because them are special. size_t n = 0; - for (InputSectionBase *isec : inputSections) { + for (InputSectionBase *isec : ctx.inputSections) { // Process InputSection and MergeInputSection. if (LLVM_LIKELY(isa(isec))) - inputSections[n++] = isec; + ctx.inputSections[n++] = isec; // In -r links, SHF_LINK_ORDER sections are added while adding their parent // sections because we need to know the parent's output section before we @@ -869,7 +869,7 @@ void LinkerScript::addOrphanSections() { add(depSec); } // Keep just InputSection. - inputSections.resize(n); + ctx.inputSections.resize(n); // If no SECTIONS command was given, we should insert sections commands // before others, so that we can handle scripts which refers them, diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index 2654248..2b309f0 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -235,14 +235,14 @@ template void MarkLive::run() { // that point to .eh_frames. Otherwise, the garbage collector would drop // all of them. We also want to preserve personality routines and LSDA // referenced by .eh_frame sections, so we scan them for that here. - for (EhInputSection *eh : ehInputSections) { + for (EhInputSection *eh : ctx.ehInputSections) { const RelsOrRelas rels = eh->template relsOrRelas(); if (rels.areRelocsRel()) scanEhFrameSection(*eh, rels.rels); else if (rels.relas.size()) scanEhFrameSection(*eh, rels.relas); } - for (InputSectionBase *sec : inputSections) { + for (InputSectionBase *sec : ctx.inputSections) { if (sec->flags & SHF_GNU_RETAIN) { enqueue(sec, 0); continue; @@ -335,7 +335,7 @@ template void MarkLive::moveToMain() { d->section->isLive()) markSymbol(s); - for (InputSectionBase *sec : inputSections) { + for (InputSectionBase *sec : ctx.inputSections) { if (!sec->isLive() || !isValidCIdentifier(sec->name)) continue; if (symtab.find(("__start_" + sec->name).str()) || @@ -361,7 +361,7 @@ template void elf::markLive() { return; } - for (InputSectionBase *sec : inputSections) + for (InputSectionBase *sec : ctx.inputSections) sec->markDead(); // Follow the graph to mark all live sections. @@ -376,7 +376,7 @@ template void elf::markLive() { // Report garbage-collected sections. if (config->printGcSections) - for (InputSectionBase *sec : inputSections) + for (InputSectionBase *sec : ctx.inputSections) if (!sec->isLive()) message("removing unused section " + toString(sec)); } diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 52f8948..50478e7 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -107,7 +107,7 @@ std::unique_ptr> MipsAbiFlagsSection::create() { Elf_Mips_ABIFlags flags = {}; bool create = false; - for (InputSectionBase *sec : inputSections) { + for (InputSectionBase *sec : ctx.inputSections) { if (sec->type != SHT_MIPS_ABIFLAGS) continue; sec->markDead(); @@ -174,7 +174,7 @@ std::unique_ptr> MipsOptionsSection::create() { return nullptr; SmallVector sections; - for (InputSectionBase *sec : inputSections) + for (InputSectionBase *sec : ctx.inputSections) if (sec->type == SHT_MIPS_OPTIONS) sections.push_back(sec); @@ -231,7 +231,7 @@ std::unique_ptr> MipsReginfoSection::create() { return nullptr; SmallVector sections; - for (InputSectionBase *sec : inputSections) + for (InputSectionBase *sec : ctx.inputSections) if (sec->type == SHT_MIPS_REGINFO) sections.push_back(sec); @@ -2840,7 +2840,7 @@ template GdbIndexSection *GdbIndexSection::create() { // note that isec->data() may uncompress the full content, which should be // parallelized. SetVector files; - for (InputSectionBase *s : inputSections) { + for (InputSectionBase *s : ctx.inputSections) { InputSection *isec = dyn_cast(s); if (!isec) continue; @@ -2853,7 +2853,7 @@ template GdbIndexSection *GdbIndexSection::create() { files.insert(isec->file); } // Drop .rel[a].debug_gnu_pub{names,types} for --emit-relocs. - llvm::erase_if(inputSections, [](InputSectionBase *s) { + llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { if (auto *isec = dyn_cast(s)) if (InputSectionBase *rel = isec->getRelocatedSection()) return !rel->isLive(); @@ -3309,7 +3309,7 @@ template void elf::splitSections() { void elf::combineEhSections() { llvm::TimeTraceScope timeScope("Combine EH sections"); - for (EhInputSection *sec : ehInputSections) { + for (EhInputSection *sec : ctx.ehInputSections) { EhFrameSection &eh = *sec->getPartition().ehFrame; sec->parent = &eh; eh.alignment = std::max(eh.alignment, sec->alignment); @@ -3319,7 +3319,7 @@ void elf::combineEhSections() { if (!mainPart->armExidx) return; - llvm::erase_if(inputSections, [](InputSectionBase *s) { + llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { // Ignore dead sections and the partition end marker (.part.end), // whose partition number is out of bounds. if (!s->isLive() || s->partition == 255) diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp index cfc160e..cb3ce6d 100644 --- a/lld/ELF/Target.cpp +++ b/lld/ELF/Target.cpp @@ -93,7 +93,7 @@ TargetInfo *elf::getTarget() { ErrorPlace elf::getErrorPlace(const uint8_t *loc) { assert(loc != nullptr); - for (InputSectionBase *d : inputSections) { + for (InputSectionBase *d : ctx.inputSections) { auto *isec = dyn_cast(d); if (!isec || !isec->getParent() || (isec->type & SHT_NOBITS)) continue; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index ef80082..918490b 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -121,9 +121,9 @@ static void removeEmptyPTLoad(SmallVector &phdrs) { void elf::copySectionsIntoPartitions() { SmallVector newSections; - const size_t ehSize = ehInputSections.size(); + const size_t ehSize = ctx.ehInputSections.size(); for (unsigned part = 2; part != partitions.size() + 1; ++part) { - for (InputSectionBase *s : inputSections) { + for (InputSectionBase *s : ctx.inputSections) { if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE) continue; auto *copy = make(cast(*s)); @@ -131,15 +131,15 @@ void elf::copySectionsIntoPartitions() { newSections.push_back(copy); } for (size_t i = 0; i != ehSize; ++i) { - assert(ehInputSections[i]->isLive()); - auto *copy = make(*ehInputSections[i]); + assert(ctx.ehInputSections[i]->isLive()); + auto *copy = make(*ctx.ehInputSections[i]); copy->partition = part; - ehInputSections.push_back(copy); + ctx.ehInputSections.push_back(copy); } } - inputSections.insert(inputSections.end(), newSections.begin(), - newSections.end()); + ctx.inputSections.insert(ctx.inputSections.end(), newSections.begin(), + newSections.end()); } static Defined *addOptionalRegular(StringRef name, SectionBase *sec, @@ -273,11 +273,11 @@ template void elf::createSyntheticSections() { for (size_t i = 1; i <= partitions.size(); ++i) { InputSection *sec = createInterpSection(); sec->partition = i; - inputSections.push_back(sec); + ctx.inputSections.push_back(sec); } } - auto add = [](SyntheticSection &sec) { inputSections.push_back(&sec); }; + auto add = [](SyntheticSection &sec) { ctx.inputSections.push_back(&sec); }; in.shStrTab = std::make_unique(".shstrtab", false); @@ -321,7 +321,7 @@ template void elf::createSyntheticSections() { for (Partition &part : partitions) { auto add = [&](SyntheticSection &sec) { sec.partition = part.getNumber(); - inputSections.push_back(&sec); + ctx.inputSections.push_back(&sec); }; if (!part.name.empty()) { @@ -1202,7 +1202,7 @@ static void maybeShuffle(DenseMap &order) { if (config->shuffleSections.empty()) return; - SmallVector matched, sections = inputSections; + SmallVector matched, sections = ctx.inputSections; matched.reserve(sections.size()); for (const auto &patAndSeed : config->shuffleSections) { matched.clear(); @@ -1793,23 +1793,23 @@ static void removeUnusedSyntheticSections() { // All input synthetic sections that can be empty are placed after // all regular ones. Reverse iterate to find the first synthetic section // after a non-synthetic one which will be our starting point. - auto start = std::find_if(inputSections.rbegin(), inputSections.rend(), - [](InputSectionBase *s) { - return !isa(s); - }) - .base(); + auto start = + std::find_if( + ctx.inputSections.rbegin(), ctx.inputSections.rend(), + [](InputSectionBase *s) { return !isa(s); }) + .base(); - // Remove unused synthetic sections from inputSections; + // Remove unused synthetic sections from ctx.inputSections; DenseSet unused; auto end = - std::remove_if(start, inputSections.end(), [&](InputSectionBase *s) { + std::remove_if(start, ctx.inputSections.end(), [&](InputSectionBase *s) { auto *sec = cast(s); if (sec->getParent() && sec->isNeeded()) return false; unused.insert(sec); return true; }); - inputSections.erase(end, inputSections.end()); + ctx.inputSections.erase(end, ctx.inputSections.end()); // Remove unused synthetic sections from the corresponding input section // description and orphanSections. -- 2.7.4