Skip to content

Commit bab5d5b

Browse files
committed
[ELF] Pass Ctx & to ICF and SymbolTable
1 parent 2ab9233 commit bab5d5b

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

lld/ELF/Driver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void Ctx::reset() {
109109

110110
in.reset();
111111
sym = ElfSym{};
112-
symtab = std::make_unique<SymbolTable>();
112+
symtab = std::make_unique<SymbolTable>(*this);
113113

114114
memoryBuffers.clear();
115115
objectFiles.clear();
@@ -167,7 +167,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
167167
LinkerScript script(ctx);
168168
ctx.script = &script;
169169
ctx.symAux.emplace_back();
170-
ctx.symtab = std::make_unique<SymbolTable>();
170+
ctx.symtab = std::make_unique<SymbolTable>(ctx);
171171

172172
ctx.partitions.clear();
173173
ctx.partitions.emplace_back();
@@ -3201,7 +3201,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
32013201
// ICF runs after processSectionCommands() so that we know the output sections.
32023202
if (ctx.arg.icf != ICFLevel::None) {
32033203
findKeepUniqueSections<ELFT>(ctx, args);
3204-
doIcf<ELFT>();
3204+
doIcf<ELFT>(ctx);
32053205
}
32063206

32073207
// Read the callgraph now that we know what was gced or icfed

lld/ELF/ICF.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ using namespace lld::elf;
9797
namespace {
9898
template <class ELFT> class ICF {
9999
public:
100+
ICF(Ctx &ctx) : ctx(ctx) {}
100101
void run();
101102

102103
private:
@@ -120,6 +121,7 @@ template <class ELFT> class ICF {
120121

121122
void forEachClass(llvm::function_ref<void(size_t, size_t)> fn);
122123

124+
Ctx &ctx;
123125
SmallVector<InputSection *, 0> sections;
124126

125127
// We repeat the main loop while `Repeat` is true.
@@ -457,7 +459,7 @@ static void combineRelocHashes(unsigned cnt, InputSection *isec,
457459
isec->eqClass[(cnt + 1) % 2] = hash | (1U << 31);
458460
}
459461

460-
static void print(const Twine &s) {
462+
static void print(Ctx &ctx, const Twine &s) {
461463
if (ctx.arg.printIcfSections)
462464
message(s);
463465
}
@@ -546,9 +548,9 @@ template <class ELFT> void ICF<ELFT>::run() {
546548
forEachClassRange(0, sections.size(), [&](size_t begin, size_t end) {
547549
if (end - begin == 1)
548550
return;
549-
print("selected section " + toString(sections[begin]));
551+
print(ctx, "selected section " + toString(sections[begin]));
550552
for (size_t i = begin + 1; i < end; ++i) {
551-
print(" removing identical section " + toString(sections[i]));
553+
print(ctx, " removing identical section " + toString(sections[i]));
552554
sections[begin]->replace(sections[i]);
553555

554556
// At this point we know sections merged are fully identical and hence
@@ -586,12 +588,12 @@ template <class ELFT> void ICF<ELFT>::run() {
586588
}
587589

588590
// ICF entry point function.
589-
template <class ELFT> void elf::doIcf() {
591+
template <class ELFT> void elf::doIcf(Ctx &ctx) {
590592
llvm::TimeTraceScope timeScope("ICF");
591-
ICF<ELFT>().run();
593+
ICF<ELFT>(ctx).run();
592594
}
593595

594-
template void elf::doIcf<ELF32LE>();
595-
template void elf::doIcf<ELF32BE>();
596-
template void elf::doIcf<ELF64LE>();
597-
template void elf::doIcf<ELF64BE>();
596+
template void elf::doIcf<ELF32LE>(Ctx &);
597+
template void elf::doIcf<ELF32BE>(Ctx &);
598+
template void elf::doIcf<ELF64LE>(Ctx &);
599+
template void elf::doIcf<ELF64BE>(Ctx &);

lld/ELF/ICF.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#define LLD_ELF_ICF_H
1111

1212
namespace lld::elf {
13+
struct Ctx;
1314

14-
template <class ELFT> void doIcf();
15-
15+
template <class ELFT> void doIcf(Ctx &);
1616
}
1717

1818
#endif

lld/ELF/LTO.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
281281
// If LazyObjFile has not been added to link, emit empty index files.
282282
// This is needed because this is what GNU gold plugin does and we have a
283283
// distributed build system that depends on that behavior.
284-
static void thinLTOCreateEmptyIndexFiles() {
284+
static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) {
285285
DenseSet<StringRef> linkedBitCodeFiles;
286286
for (BitcodeFile *f : ctx.bitcodeFiles)
287287
linkedBitCodeFiles.insert(f->getName());
@@ -345,7 +345,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
345345
}
346346

347347
if (ctx.arg.thinLTOEmitIndexFiles)
348-
thinLTOCreateEmptyIndexFiles();
348+
thinLTOCreateEmptyIndexFiles(ctx);
349349

350350
if (ctx.arg.thinLTOIndexOnly) {
351351
if (!ctx.arg.ltoObjPath.empty())

lld/ELF/SymbolTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ bool SymbolTable::assignExactVersion(SymbolVersion ver, uint16_t versionId,
215215
// Get a list of symbols which we need to assign the version to.
216216
SmallVector<Symbol *, 0> syms = findByVersion(ver);
217217

218-
auto getName = [](uint16_t ver) -> std::string {
218+
auto getName = [&ctx = ctx](uint16_t ver) -> std::string {
219219
if (ver == VER_NDX_LOCAL)
220220
return "VER_NDX_LOCAL";
221221
if (ver == VER_NDX_GLOBAL)

lld/ELF/SymbolTable.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "llvm/Support/Compiler.h"
1616

1717
namespace lld::elf {
18-
18+
struct Ctx;
1919
class InputFile;
2020
class SharedFile;
2121

@@ -38,6 +38,7 @@ struct ArmCmseEntryFunction {
3838
// is one add* function per symbol type.
3939
class SymbolTable {
4040
public:
41+
SymbolTable(Ctx &ctx) : ctx(ctx) {}
4142
ArrayRef<Symbol *> getSymbols() const { return symVector; }
4243

4344
void wrap(Symbol *sym, Symbol *real, Symbol *wrap);
@@ -91,6 +92,8 @@ class SymbolTable {
9192
void assignWildcardVersion(SymbolVersion ver, uint16_t versionId,
9293
bool includeNonDefault);
9394

95+
Ctx &ctx;
96+
9497
// Global symbols and a map from symbol name to the index. The order is not
9598
// defined. We can use an arbitrary order, but it has to be deterministic even
9699
// when cross linking.

0 commit comments

Comments
 (0)