Commit fc5d815d authored by Fangrui Song's avatar Fangrui Song
Browse files

[ELF] Merge demoteSymbols and isPreemptible computation. NFC

Remove one iteration of symtab and slightly improve the performance.
parent 3472d4d4
Loading
Loading
Loading
Loading
+11 −13
Original line number Diff line number Diff line
@@ -255,17 +255,20 @@ void elf::addReservedSymbols() {
// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
// dangling references to an unneeded DSO. Use a weak binding to avoid
// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
static void demoteSymbols() {
static void demoteSymbolsAndComputeIsPreemptible() {
  llvm::TimeTraceScope timeScope("Demote symbols");
  for (Symbol *sym : symtab.getSymbols()) {
    auto *s = dyn_cast<SharedSymbol>(sym);
    if (!(s && !cast<SharedFile>(s->file)->isNeeded) && !sym->isLazy())
      continue;
    if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {
      uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
      Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type)
          .overwrite(*sym);
      sym->versionId = VER_NDX_GLOBAL;
    }

    if (config->hasDynSymTab)
      sym->isPreemptible = computeIsPreemptible(*sym);
  }
}

// Fully static executables don't support MTE globals at this point in time, as
@@ -1954,12 +1957,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
    }
  }

  demoteSymbols();
  if (config->hasDynSymTab) {
    parallelForEach(symtab.getSymbols(), [](Symbol *sym) {
      sym->isPreemptible = computeIsPreemptible(*sym);
    });
  }
  demoteSymbolsAndComputeIsPreemptible();

  // Change values of linker-script-defined symbols from placeholders (assigned
  // by declareSymbols) to actual definitions.