[ELF] Optimize replaceCommonSymbols

This decreases the 0.2% time (no debug info) to nearly no.
This commit is contained in:
Fangrui Song 2021-12-24 19:01:50 -08:00
parent 745420d3f4
commit 40fae4d8fc
3 changed files with 18 additions and 10 deletions

View File

@ -1821,17 +1821,21 @@ static void writeDependencyFile() {
// symbols of type CommonSymbol.
static void replaceCommonSymbols() {
llvm::TimeTraceScope timeScope("Replace common symbols");
for (Symbol *sym : symtab->symbols()) {
auto *s = dyn_cast<CommonSymbol>(sym);
if (!s)
for (ELFFileBase *file : objectFiles) {
if (!file->hasCommonSyms)
continue;
for (Symbol *sym : file->getGlobalSymbols()) {
auto *s = dyn_cast<CommonSymbol>(sym);
if (!s)
continue;
auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
bss->file = s->file;
bss->markDead();
inputSections.push_back(bss);
s->replace(Defined{s->file, s->getName(), s->binding, s->stOther, s->type,
/*value=*/0, s->size, bss});
auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
bss->file = s->file;
bss->markDead();
inputSections.push_back(bss);
s->replace(Defined{s->file, s->getName(), s->binding, s->stOther, s->type,
/*value=*/0, s->size, bss});
}
}
}

View File

@ -1123,6 +1123,7 @@ template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
if (value == 0 || value >= UINT32_MAX)
fatal(toString(this) + ": common symbol '" + name +
"' has invalid alignment: " + Twine(value));
hasCommonSyms = true;
sym->resolve(
CommonSymbol{this, name, binding, stOther, type, value, size});
continue;

View File

@ -205,12 +205,15 @@ protected:
// Initializes this class's member variables.
template <typename ELFT> void init();
StringRef stringTable;
const void *elfShdrs = nullptr;
const void *elfSyms = nullptr;
uint32_t numELFShdrs = 0;
uint32_t numELFSyms = 0;
uint32_t firstGlobal = 0;
StringRef stringTable;
public:
bool hasCommonSyms = false;
};
// .o file.