From d14d8664e3e44a0b87b39c3eeace3d82fabbbd27 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 27 Feb 2022 20:33:28 +0000 Subject: [PATCH] [ELF] Change global variable backwardReferences to a LinkerDriver member variable. NFC Similar to whyExtract. --- lld/ELF/Driver.cpp | 20 +++++++++++++++++++- lld/ELF/Driver.h | 6 ++++++ lld/ELF/Symbols.cpp | 27 ++++----------------------- lld/ELF/Symbols.h | 6 ------ 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 7835026..3f767b5 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -102,7 +102,6 @@ bool elf::link(ArrayRef args, llvm::raw_ostream &stdoutOS, lazyBitcodeFiles.clear(); objectFiles.clear(); sharedFiles.clear(); - backwardReferences.clear(); symAux.clear(); tar = nullptr; @@ -1781,6 +1780,25 @@ void LinkerDriver::writeWhyExtract() const { } } +void LinkerDriver::reportBackrefs() const { + for (auto &ref : backwardReferences) { + const Symbol &sym = *ref.first; + std::string to = toString(ref.second.second); + // Some libraries have known problems and can cause noise. Filter them out + // with --warn-backrefs-exclude=. The value may look like (for --start-lib) + // *.o or (archive member) *.a(*.o). + bool exclude = false; + for (const llvm::GlobPattern &pat : config->warnBackrefsExclude) + if (pat.match(to)) { + exclude = true; + break; + } + if (!exclude) + warn("backward reference detected: " + sym.getName() + " in " + + toString(ref.second.first) + " refers to " + to); + } +} + // Handle --dependency-file=. If that option is given, lld creates a // file at a given path with the following contents: // diff --git a/lld/ELF/Driver.h b/lld/ELF/Driver.h index 0ec47d2..09d2f76 100644 --- a/lld/ELF/Driver.h +++ b/lld/ELF/Driver.h @@ -35,6 +35,7 @@ private: template void compileBitcodeFiles(bool skipLinkedOutput); void writeArchiveStats() const; void writeWhyExtract() const; + void reportBackrefs() const; // True if we are in --whole-archive and --no-whole-archive. bool inWholeArchive = false; @@ -52,6 +53,11 @@ public: // A tuple of (reference, extractedFile, sym). Used by --why-extract=. SmallVector, 0> whyExtract; + // A mapping from a symbol to an InputFile referencing it backward. Used by + // --warn-backrefs. + llvm::DenseMap> + backwardReferences; }; // Parses command line options. diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp index 4fab75a..484ad8e 100644 --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -49,8 +49,6 @@ Defined *ElfSym::relaIpltStart; Defined *ElfSym::relaIpltEnd; Defined *ElfSym::riscvGlobalPointer; Defined *ElfSym::tlsModuleBase; -DenseMap> - elf::backwardReferences; SmallVector elf::symAux; static uint64_t getSymVA(const Symbol &sym, int64_t addend) { @@ -350,24 +348,6 @@ bool elf::computeIsPreemptible(const Symbol &sym) { return true; } -void elf::reportBackrefs() { - for (auto &it : backwardReferences) { - const Symbol &sym = *it.first; - std::string to = toString(it.second.second); - // Some libraries have known problems and can cause noise. Filter them out - // with --warn-backrefs-exclude=. to may look like *.o or *.a(*.o). - bool exclude = false; - for (const llvm::GlobPattern &pat : config->warnBackrefsExclude) - if (pat.match(to)) { - exclude = true; - break; - } - if (!exclude) - warn("backward reference detected: " + sym.getName() + " in " + - toString(it.second.first) + " refers to " + to); - } -} - static uint8_t getMinVisibility(uint8_t va, uint8_t vb) { if (va == STV_DEFAULT) return vb; @@ -509,7 +489,8 @@ void Symbol::resolveUndefined(const Undefined &other) { // definition. this->file needs to be saved because in the case of LTO it // may be reset to nullptr or be replaced with a file named lto.tmp. if (backref && !isWeak()) - backwardReferences.try_emplace(this, std::make_pair(other.file, file)); + driver->backwardReferences.try_emplace(this, + std::make_pair(other.file, file)); return; } @@ -633,7 +614,7 @@ void Symbol::resolveLazy(const LazyObject &other) { // should be extracted as the canonical definition instead. if (LLVM_UNLIKELY(isCommon()) && elf::config->fortranCommon && other.file->shouldExtractForCommon(getName())) { - backwardReferences.erase(this); + driver->backwardReferences.erase(this); replace(other); other.extract(); return; @@ -642,7 +623,7 @@ void Symbol::resolveLazy(const LazyObject &other) { if (!isUndefined()) { // See the comment in resolveUndefined(). if (isDefined()) - backwardReferences.erase(this); + driver->backwardReferences.erase(this); return; } diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h index 4108cb8..b210659 100644 --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -563,12 +563,6 @@ void maybeWarnUnorderableSymbol(const Symbol *sym); bool computeIsPreemptible(const Symbol &sym); void reportBackrefs(); -// A mapping from a symbol to an InputFile referencing it backward. Used by -// --warn-backrefs. -extern llvm::DenseMap> - backwardReferences; - } // namespace elf } // namespace lld -- 2.7.4