Skip to content

Commit

Permalink
[ELF] Optimize "Strip sections"
Browse files Browse the repository at this point in the history
If SHT_LLVM_SYMPART is unused, don't iterate over inputSections.
If neither --strip-debug/--strip-all, don't iterate over inputSections.
  • Loading branch information
MaskRay committed Mar 16, 2022
1 parent 9daf576 commit 1a59023
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 2 additions & 0 deletions lld/ELF/Config.h
Expand Up @@ -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.
Expand Down
34 changes: 18 additions & 16 deletions lld/ELF/Driver.cpp
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lld/ELF/InputFiles.cpp
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "InputFiles.h"
#include "Config.h"
#include "DWARF.h"
#include "Driver.h"
#include "InputSection.h"
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 1a59023

Please sign in to comment.