Skip to content

Commit

Permalink
[ELF] Change global variable backwardReferences to a LinkerDriver mem…
Browse files Browse the repository at this point in the history
…ber variable. NFC

Similar to whyExtract.
  • Loading branch information
MaskRay committed Feb 27, 2022
1 parent 7fd3849 commit d14d866
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
20 changes: 19 additions & 1 deletion lld/ELF/Driver.cpp
Expand Up @@ -102,7 +102,6 @@ bool elf::link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
lazyBitcodeFiles.clear();
objectFiles.clear();
sharedFiles.clear();
backwardReferences.clear();
symAux.clear();

tar = nullptr;
Expand Down Expand Up @@ -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=<path>. If that option is given, lld creates a
// file at a given path with the following contents:
//
Expand Down
6 changes: 6 additions & 0 deletions lld/ELF/Driver.h
Expand Up @@ -35,6 +35,7 @@ class LinkerDriver {
template <class ELFT> 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;
Expand All @@ -52,6 +53,11 @@ class LinkerDriver {
// A tuple of (reference, extractedFile, sym). Used by --why-extract=.
SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0>
whyExtract;
// A mapping from a symbol to an InputFile referencing it backward. Used by
// --warn-backrefs.
llvm::DenseMap<const Symbol *,
std::pair<const InputFile *, const InputFile *>>
backwardReferences;
};

// Parses command line options.
Expand Down
27 changes: 4 additions & 23 deletions lld/ELF/Symbols.cpp
Expand Up @@ -49,8 +49,6 @@ Defined *ElfSym::relaIpltStart;
Defined *ElfSym::relaIpltEnd;
Defined *ElfSym::riscvGlobalPointer;
Defined *ElfSym::tlsModuleBase;
DenseMap<const Symbol *, std::pair<const InputFile *, const InputFile *>>
elf::backwardReferences;
SmallVector<SymbolAux, 0> elf::symAux;

static uint64_t getSymVA(const Symbol &sym, int64_t addend) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
6 changes: 0 additions & 6 deletions lld/ELF/Symbols.h
Expand Up @@ -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<const Symbol *,
std::pair<const InputFile *, const InputFile *>>
backwardReferences;

} // namespace elf
} // namespace lld

Expand Down

0 comments on commit d14d866

Please sign in to comment.