Skip to content

Commit e980f16

Browse files
committed
[ELF] Move whyExtract/backwardReferences from LinkerDriver to Ctx. NFC
Ctx was recently added as a more suitable place for such singletons.
1 parent bd2044c commit e980f16

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

lld/ELF/Config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ struct Ctx {
380380
SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;
381381
// True if SHT_LLVM_SYMPART is used.
382382
std::atomic<bool> hasSympart{false};
383+
// A tuple of (reference, extractedFile, sym). Used by --why-extract=.
384+
SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0>
385+
whyExtractRecords;
386+
// A mapping from a symbol to an InputFile referencing it backward. Used by
387+
// --warn-backrefs.
388+
llvm::DenseMap<const Symbol *,
389+
std::pair<const InputFile *, const InputFile *>>
390+
backwardReferences;
383391
};
384392

385393
// The only instance of Ctx struct.

lld/ELF/Driver.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,7 @@ static void handleUndefined(Symbol *sym, const char *option) {
17741774
return;
17751775
sym->extract();
17761776
if (!config->whyExtract.empty())
1777-
driver->whyExtract.emplace_back(option, sym->file, *sym);
1777+
ctx->whyExtractRecords.emplace_back(option, sym->file, *sym);
17781778
}
17791779

17801780
// As an extension to GNU linkers, lld supports a variant of `-u`
@@ -1810,7 +1810,7 @@ static void handleLibcall(StringRef name) {
18101810
sym->extract();
18111811
}
18121812

1813-
void LinkerDriver::writeArchiveStats() const {
1813+
static void writeArchiveStats() {
18141814
if (config->printArchiveStats.empty())
18151815
return;
18161816

@@ -1832,15 +1832,15 @@ void LinkerDriver::writeArchiveStats() const {
18321832
for (BitcodeFile *file : bitcodeFiles)
18331833
if (file->archiveName.size())
18341834
++extracted[CachedHashStringRef(file->archiveName)];
1835-
for (std::pair<StringRef, unsigned> f : archiveFiles) {
1835+
for (std::pair<StringRef, unsigned> f : driver->archiveFiles) {
18361836
unsigned &v = extracted[CachedHashString(f.first)];
18371837
os << f.second << '\t' << v << '\t' << f.first << '\n';
18381838
// If the archive occurs multiple times, other instances have a count of 0.
18391839
v = 0;
18401840
}
18411841
}
18421842

1843-
void LinkerDriver::writeWhyExtract() const {
1843+
static void writeWhyExtract() {
18441844
if (config->whyExtract.empty())
18451845
return;
18461846

@@ -1853,14 +1853,14 @@ void LinkerDriver::writeWhyExtract() const {
18531853
}
18541854

18551855
os << "reference\textracted\tsymbol\n";
1856-
for (auto &entry : whyExtract) {
1856+
for (auto &entry : ctx->whyExtractRecords) {
18571857
os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
18581858
<< toString(std::get<2>(entry)) << '\n';
18591859
}
18601860
}
18611861

1862-
void LinkerDriver::reportBackrefs() const {
1863-
for (auto &ref : backwardReferences) {
1862+
static void reportBackrefs() {
1863+
for (auto &ref : ctx->backwardReferences) {
18641864
const Symbol &sym = *ref.first;
18651865
std::string to = toString(ref.second.second);
18661866
// Some libraries have known problems and can cause noise. Filter them out

lld/ELF/Driver.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ class LinkerDriver {
3333
void inferMachineType();
3434
void link(llvm::opt::InputArgList &args);
3535
template <class ELFT> void compileBitcodeFiles(bool skipLinkedOutput);
36-
void writeArchiveStats() const;
37-
void writeWhyExtract() const;
38-
void reportBackrefs() const;
3936

4037
// True if we are in --whole-archive and --no-whole-archive.
4138
bool inWholeArchive = false;
@@ -47,17 +44,9 @@ class LinkerDriver {
4744
std::unique_ptr<BitcodeCompiler> lto;
4845

4946
std::vector<InputFile *> files;
50-
SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles;
5147

5248
public:
53-
// A tuple of (reference, extractedFile, sym). Used by --why-extract=.
54-
SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0>
55-
whyExtract;
56-
// A mapping from a symbol to an InputFile referencing it backward. Used by
57-
// --warn-backrefs.
58-
llvm::DenseMap<const Symbol *,
59-
std::pair<const InputFile *, const InputFile *>>
60-
backwardReferences;
49+
SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles;
6150
};
6251

6352
// Parses command line options.

lld/ELF/Symbols.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void elf::printTraceSymbol(const Symbol &sym, StringRef name) {
304304

305305
static void recordWhyExtract(const InputFile *reference,
306306
const InputFile &extracted, const Symbol &sym) {
307-
driver->whyExtract.emplace_back(toString(reference), &extracted, sym);
307+
ctx->whyExtractRecords.emplace_back(toString(reference), &extracted, sym);
308308
}
309309

310310
void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
@@ -508,8 +508,8 @@ void Symbol::resolveUndefined(const Undefined &other) {
508508
// definition. this->file needs to be saved because in the case of LTO it
509509
// may be reset to nullptr or be replaced with a file named lto.tmp.
510510
if (backref && !isWeak())
511-
driver->backwardReferences.try_emplace(this,
512-
std::make_pair(other.file, file));
511+
ctx->backwardReferences.try_emplace(this,
512+
std::make_pair(other.file, file));
513513
return;
514514
}
515515

@@ -647,7 +647,7 @@ void Symbol::resolveLazy(const LazyObject &other) {
647647
// should be extracted as the canonical definition instead.
648648
if (LLVM_UNLIKELY(isCommon()) && elf::config->fortranCommon &&
649649
other.file->shouldExtractForCommon(getName())) {
650-
driver->backwardReferences.erase(this);
650+
ctx->backwardReferences.erase(this);
651651
replace(other);
652652
other.extract();
653653
return;
@@ -656,7 +656,7 @@ void Symbol::resolveLazy(const LazyObject &other) {
656656
if (!isUndefined()) {
657657
// See the comment in resolveUndefined().
658658
if (isDefined())
659-
driver->backwardReferences.erase(this);
659+
ctx->backwardReferences.erase(this);
660660
return;
661661
}
662662

lld/ELF/Symbols.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ void reportDuplicate(const Symbol &sym, const InputFile *newFile,
554554
InputSectionBase *errSec, uint64_t errOffset);
555555
void maybeWarnUnorderableSymbol(const Symbol *sym);
556556
bool computeIsPreemptible(const Symbol &sym);
557-
void reportBackrefs();
558557

559558
} // namespace elf
560559
} // namespace lld

0 commit comments

Comments
 (0)