diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -90,6 +90,7 @@ BitcodeFile::instances.clear(); memset(MergeChunk::instances, 0, sizeof(MergeChunk::instances)); TpiSource::clear(); + OutputSection::outputSections.clear(); return !errorCount(); } diff --git a/lld/COFF/Writer.h b/lld/COFF/Writer.h --- a/lld/COFF/Writer.h +++ b/lld/COFF/Writer.h @@ -74,6 +74,7 @@ std::vector origChunks; std::vector contribSections; + static std::vector outputSections; private: uint32_t stringTableOff = 0; diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp --- a/lld/COFF/Writer.cpp +++ b/lld/COFF/Writer.cpp @@ -81,10 +81,10 @@ // Global vector of all output sections. After output sections are finalized, // this can be indexed by Chunk::getOutputSection. -static std::vector outputSections; +std::vector OutputSection::outputSections; OutputSection *Chunk::getOutputSection() const { - return osidx == 0 ? nullptr : outputSections[osidx - 1]; + return osidx == 0 ? nullptr : OutputSection::outputSections[osidx - 1]; } namespace { @@ -540,7 +540,7 @@ return; size_t origNumChunks = 0; - for (OutputSection *sec : outputSections) { + for (OutputSection *sec : OutputSection::outputSections) { sec->origChunks = sec->chunks; origNumChunks += sec->chunks.size(); } @@ -552,7 +552,7 @@ // adding them turned out ok. bool rangesOk = true; size_t numChunks = 0; - for (OutputSection *sec : outputSections) { + for (OutputSection *sec : OutputSection::outputSections) { if (!verifyRanges(sec->chunks)) { rangesOk = false; break; @@ -573,7 +573,7 @@ // If the previous pass didn't work out, reset everything back to the // original conditions before retrying with a wider margin. This should // ideally never happen under real circumstances. - for (OutputSection *sec : outputSections) + for (OutputSection *sec : OutputSection::outputSections) sec->chunks = sec->origChunks; margin *= 2; } @@ -581,7 +581,7 @@ // Try adding thunks everywhere where it is needed, with a margin // to avoid things going out of range due to the added thunks. bool addressesChanged = false; - for (OutputSection *sec : outputSections) + for (OutputSection *sec : OutputSection::outputSections) addressesChanged |= createThunks(sec, margin); // If the verification above thought we needed thunks, we should have // added some. @@ -629,12 +629,12 @@ if (!config->pdbPath.empty() && config->debug) { assert(buildId); - createPDB(symtab, outputSections, sectionTable, buildId->buildId); + createPDB(symtab, OutputSection::outputSections, sectionTable, buildId->buildId); } writeBuildId(); - writeLLDMapFile(outputSections); - writeMapFile(outputSections); + writeLLDMapFile(OutputSection::outputSections); + writeMapFile(OutputSection::outputSections); if (errorCount()) return; @@ -814,7 +814,7 @@ OutputSection *&sec = sections[{name, outChars}]; if (!sec) { sec = make(name, outChars); - outputSections.push_back(sec); + OutputSection::outputSections.push_back(sec); } return sec; }; @@ -911,7 +911,7 @@ return 1; return 0; }; - llvm::stable_sort(outputSections, + llvm::stable_sort(OutputSection::outputSections, [&](const OutputSection *s, const OutputSection *t) { return sectionOrder(s) < sectionOrder(t); }); @@ -1062,25 +1062,25 @@ // later. Only remove sections that have no Chunks at all. return s->chunks.empty(); }; - outputSections.erase( - std::remove_if(outputSections.begin(), outputSections.end(), isUnused), - outputSections.end()); + OutputSection::outputSections.erase( + std::remove_if(OutputSection::outputSections.begin(), OutputSection::outputSections.end(), isUnused), + OutputSection::outputSections.end()); } // The Windows loader doesn't seem to like empty sections, // so we remove them if any. void Writer::removeEmptySections() { auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; }; - outputSections.erase( - std::remove_if(outputSections.begin(), outputSections.end(), isEmpty), - outputSections.end()); + OutputSection::outputSections.erase( + std::remove_if(OutputSection::outputSections.begin(), OutputSection::outputSections.end(), isEmpty), + OutputSection::outputSections.end()); } void Writer::assignOutputSectionIndices() { // Assign final output section indices, and assign each chunk to its output // section. uint32_t idx = 1; - for (OutputSection *os : outputSections) { + for (OutputSection *os : OutputSection::outputSections) { os->sectionIndex = idx; for (Chunk *c : os->chunks) c->setOutputSectionIdx(idx); @@ -1167,7 +1167,7 @@ // solution where discardable sections have long names preserved and // non-discardable sections have their names truncated, to ensure that any // section which is mapped at runtime also has its name mapped at runtime. - for (OutputSection *sec : outputSections) { + for (OutputSection *sec : OutputSection::outputSections) { if (sec->name.size() <= COFF::NameSize) continue; if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0) @@ -1241,7 +1241,7 @@ void Writer::assignAddresses() { sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) + sizeof(data_directory) * numberOfDataDirectory + - sizeof(coff_section) * outputSections.size(); + sizeof(coff_section) * OutputSection::outputSections.size(); sizeOfHeaders += config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header); sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign); @@ -1250,7 +1250,7 @@ // The first page is kept unmapped. uint64_t rva = alignTo(sizeOfHeaders, config->align); - for (OutputSection *sec : outputSections) { + for (OutputSection *sec : OutputSection::outputSections) { if (sec == relocSec) addBaserels(); uint64_t rawSize = 0, virtualSize = 0; @@ -1320,7 +1320,7 @@ auto *coff = reinterpret_cast(buf); buf += sizeof(*coff); coff->Machine = config->machine; - coff->NumberOfSections = outputSections.size(); + coff->NumberOfSections = OutputSection::outputSections.size(); coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE; if (config->largeAddressAware) coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE; @@ -1469,12 +1469,12 @@ } // Write section table - for (OutputSection *sec : outputSections) { + for (OutputSection *sec : OutputSection::outputSections) { sec->writeHeaderTo(buf); buf += sizeof(coff_section); } sectionTable = ArrayRef( - buf - outputSections.size() * sizeof(coff_section), buf); + buf - OutputSection::outputSections.size() * sizeof(coff_section), buf); if (outputSymtab.empty() && strtab.empty()) return; @@ -1773,7 +1773,7 @@ for (auto &p : config->section) { StringRef name = p.first; uint32_t perm = p.second; - for (OutputSection *sec : outputSections) + for (OutputSection *sec : OutputSection::outputSections) if (sec->name == name) sec->setPermissions(perm); } @@ -1783,10 +1783,10 @@ void Writer::writeSections() { // Record the number of sections to apply section index relocations // against absolute symbols. See applySecIdx in Chunks.cpp.. - DefinedAbsolute::numOutputSections = outputSections.size(); + DefinedAbsolute::numOutputSections = OutputSection::outputSections.size(); uint8_t *buf = buffer->getBufferStart(); - for (OutputSection *sec : outputSections) { + for (OutputSection *sec : OutputSection::outputSections) { uint8_t *secBuf = buf + sec->getFileOff(); // Fill gaps between functions in .text with INT3 instructions // instead of leaving as NUL bytes (which can be interpreted as @@ -1916,7 +1916,7 @@ } OutputSection *Writer::findSection(StringRef name) { - for (OutputSection *sec : outputSections) + for (OutputSection *sec : OutputSection::outputSections) if (sec->name == name) return sec; return nullptr; @@ -1924,7 +1924,7 @@ uint32_t Writer::getSizeOfInitializedData() { uint32_t res = 0; - for (OutputSection *s : outputSections) + for (OutputSection *s : OutputSection::outputSections) if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) res += s->getRawSize(); return res; @@ -1936,7 +1936,7 @@ return; relocSec->chunks.clear(); std::vector v; - for (OutputSection *sec : outputSections) { + for (OutputSection *sec : OutputSection::outputSections) { if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) continue; // Collect all locations for base relocations.