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

[ELF] Move demoteSymbols to Writer.cpp. NFC

History of demoteSharedSymbols:

* https://reviews.llvm.org/D45536 demotes SharedSymbol
* https://reviews.llvm.org/D111365 demotes lazy symbols
* The pending #69295 will demote symbols defined in discarded sections

The pass is placed after markLive just to be clear that it needs `isNeeded`
information computed by markLive. The remaining passes in Driver.cpp do not use
symbol information. Move the pass to Writer.cpp to be closer to other
symbol-related passes.
parent d4088e7d
Loading
Loading
Loading
Loading
+0 −19
Original line number Diff line number Diff line
@@ -2248,24 +2248,6 @@ static void replaceCommonSymbols() {
  }
}

// If all references to a DSO happen to be weak, the DSO is not added to
// 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 demoteSharedAndLazySymbols() {
  llvm::TimeTraceScope timeScope("Demote shared and lazy symbols");
  for (Symbol *sym : symtab.getSymbols()) {
    auto *s = dyn_cast<SharedSymbol>(sym);
    if (!(s && !cast<SharedFile>(s->file)->isNeeded) && !sym->isLazy())
      continue;

    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;
  }
}

// The section referred to by `s` is considered address-significant. Set the
// keepUnique flag on the section if appropriate.
static void markAddrsig(Symbol *s) {
@@ -3023,7 +3005,6 @@ void LinkerDriver::link(opt::InputArgList &args) {

  // Garbage collection and removal of shared symbols from unused shared objects.
  invokeELFT(markLive,);
  demoteSharedAndLazySymbols();

  // Make copies of any input sections that need to be copied into each
  // partition.
+23 −5
Original line number Diff line number Diff line
@@ -251,6 +251,23 @@ void elf::addReservedSymbols() {
  ElfSym::edata2 = add("_edata", -1);
}

// If all references to a DSO happen to be weak, the DSO is not added to
// 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() {
  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;
    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;
  }
}

// Fully static executables don't support MTE globals at this point in time, as
// we currently rely on:
//   - A dynamic loader to process relocations, and
@@ -1935,13 +1952,14 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
      for (Partition &part : partitions)
        finalizeSynthetic(part.ehFrame.get());
    }
  }

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

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