[ELF] Optimize "Strip sections"

If SHT_LLVM_SYMPART is unused, don't iterate over inputSections.
If neither --strip-debug/--strip-all, don't iterate over inputSections.
This commit is contained in:
Fangrui Song 2022-03-15 23:15:43 -07:00
parent 9daf576583
commit 1a590232f4
3 changed files with 24 additions and 16 deletions

View File

@ -363,6 +363,8 @@ struct Ctx {
// Symbols in a non-prevailing COMDAT group which should be changed to an
// Undefined.
SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;
// True if SHT_LLVM_SYMPART is used.
std::atomic<bool> hasSympart{false};
};
// The only instance of Ctx struct.

View File

@ -2601,26 +2601,28 @@ void LinkerDriver::link(opt::InputArgList &args) {
{
llvm::TimeTraceScope timeScope("Strip sections");
llvm::erase_if(inputSections, [](InputSectionBase *s) {
if (s->type == SHT_LLVM_SYMPART) {
if (ctx->hasSympart.load(std::memory_order_relaxed)) {
llvm::erase_if(inputSections, [](InputSectionBase *s) {
if (s->type != SHT_LLVM_SYMPART)
return false;
invokeELFT(readSymbolPartitionSection, s);
return true;
}
});
}
// 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) {
if (isDebugSection(*s))
return true;
if (auto *isec = dyn_cast<InputSection>(s))
if (InputSectionBase *rel = isec->getRelocatedSection())
if (isDebugSection(*rel))
return true;
// We do not want to emit debug sections if --strip-all
// or --strip-debug are given.
if (config->strip == StripPolicy::None)
return false;
if (isDebugSection(*s))
return true;
if (auto *isec = dyn_cast<InputSection>(s))
if (InputSectionBase *rel = isec->getRelocatedSection())
if (isDebugSection(*rel))
return true;
return false;
});
});
}
}
// Since we now have a complete set of input files, we can create

View File

@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "InputFiles.h"
#include "Config.h"
#include "DWARF.h"
#include "Driver.h"
#include "InputSection.h"
@ -600,6 +601,9 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
case SHT_RELA:
case SHT_NULL:
break;
case SHT_LLVM_SYMPART:
ctx->hasSympart.store(true, std::memory_order_relaxed);
LLVM_FALLTHROUGH;
default:
this->sections[i] =
createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));