Skip to content

Commit a626eb2

Browse files
committed
[ELF] Pass ctx to bAlloc/saver/uniqueSaver
1 parent baf59be commit a626eb2

17 files changed

+101
-85
lines changed

lld/ELF/AArch64ErrataFix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,9 @@ Patch843419Section::Patch843419Section(Ctx &ctx, InputSection *p, uint64_t off)
398398
patchee(p), patcheeOffset(off) {
399399
this->parent = p->getParent();
400400
patchSym = addSyntheticLocal(
401-
ctx, saver().save("__CortexA53843419_" + utohexstr(getLDSTAddr())),
401+
ctx, saver(ctx).save("__CortexA53843419_" + utohexstr(getLDSTAddr())),
402402
STT_FUNC, 0, getSize(), *this);
403-
addSyntheticLocal(ctx, saver().save("$x"), STT_NOTYPE, 0, 0, *this);
403+
addSyntheticLocal(ctx, saver(ctx).save("$x"), STT_NOTYPE, 0, 0, *this);
404404
}
405405

406406
uint64_t Patch843419Section::getLDSTAddr() const {

lld/ELF/ARMErrataFix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ Patch657417Section::Patch657417Section(Ctx &ctx, InputSection *p, uint64_t off,
141141
patchee(p), patcheeOffset(off), instr(instr), isARM(isARM) {
142142
parent = p->getParent();
143143
patchSym = addSyntheticLocal(
144-
ctx, saver().save("__CortexA8657417_" + utohexstr(getBranchAddr())),
144+
ctx, saver(ctx).save("__CortexA8657417_" + utohexstr(getBranchAddr())),
145145
STT_FUNC, isARM ? 0 : 1, getSize(), *this);
146-
addSyntheticLocal(ctx, saver().save(isARM ? "$a" : "$t"), STT_NOTYPE, 0, 0,
146+
addSyntheticLocal(ctx, saver(ctx).save(isARM ? "$a" : "$t"), STT_NOTYPE, 0, 0,
147147
*this);
148148
}
149149

lld/ELF/Arch/LoongArch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ void LoongArch::finalizeRelax(int passes) const {
851851
MutableArrayRef<Relocation> rels = sec->relocs();
852852
ArrayRef<uint8_t> old = sec->content();
853853
size_t newSize = old.size() - aux.relocDeltas[rels.size() - 1];
854-
uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize);
854+
uint8_t *p = bAlloc(ctx).Allocate<uint8_t>(newSize);
855855
uint64_t offset = 0;
856856
int64_t delta = 0;
857857
sec->content_ = p;

lld/ELF/Arch/RISCV.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ void RISCV::finalizeRelax(int passes) const {
947947
ArrayRef<uint8_t> old = sec->content();
948948
size_t newSize = old.size() - aux.relocDeltas[rels.size() - 1];
949949
size_t writesIdx = 0;
950-
uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize);
950+
uint8_t *p = bAlloc(ctx).Allocate<uint8_t>(newSize);
951951
uint64_t offset = 0;
952952
int64_t delta = 0;
953953
sec->content_ = p;
@@ -1257,7 +1257,7 @@ mergeAttributesSection(Ctx &ctx,
12571257
if (hasArch && xlen != 0) {
12581258
if (auto result = RISCVISAInfo::createFromExtMap(xlen, exts)) {
12591259
merged.strAttr.try_emplace(RISCVAttrs::ARCH,
1260-
saver().save((*result)->toString()));
1260+
saver(ctx).save((*result)->toString()));
12611261
} else {
12621262
Err(ctx) << result.takeError();
12631263
}

lld/ELF/Config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLD_ELF_CONFIG_H
1010
#define LLD_ELF_CONFIG_H
1111

12+
#include "lld/Common/CommonLinkerContext.h"
1213
#include "lld/Common/ErrorHandler.h"
1314
#include "llvm/ADT/CachedHashString.h"
1415
#include "llvm/ADT/DenseSet.h"
@@ -546,6 +547,7 @@ struct Ctx {
546547
LinkerScript *script;
547548
std::unique_ptr<TargetInfo> target;
548549

550+
CommonLinkerContext *commonCtx;
549551
ErrorHandler *errHandler;
550552

551553
// These variables are initialized by Writer and should not be used before
@@ -673,6 +675,14 @@ static inline ArrayRef<VersionDefinition> namedVersionDefs(Ctx &ctx) {
673675
return llvm::ArrayRef(ctx.arg.versionDefinitions).slice(2);
674676
}
675677

678+
inline llvm::BumpPtrAllocator &bAlloc(Ctx &ctx) {
679+
return ctx.commonCtx->bAlloc;
680+
}
681+
inline llvm::StringSaver &saver(Ctx &ctx) { return ctx.commonCtx->saver; }
682+
inline llvm::UniqueStringSaver &uniqueSaver(Ctx &ctx) {
683+
return ctx.commonCtx->uniqueSaver;
684+
}
685+
676686
struct ELFSyncStream : SyncStream {
677687
Ctx &ctx;
678688
ELFSyncStream(Ctx &ctx, DiagLevel level)

lld/ELF/Driver.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void Ctx::reset() {
109109
script = nullptr;
110110
target.reset();
111111

112+
commonCtx = nullptr;
112113
errHandler = nullptr;
113114

114115
bufferStart = nullptr;
@@ -179,6 +180,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
179180
Ctx &ctx = elf::ctx;
180181
LinkerScript script(ctx);
181182
ctx.script = &script;
183+
ctx.commonCtx = context;
182184
ctx.errHandler = &context->e;
183185
ctx.symAux.emplace_back();
184186
ctx.symtab = std::make_unique<SymbolTable>(ctx);
@@ -386,7 +388,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
386388
// Add a given library by searching it from input search paths.
387389
void LinkerDriver::addLibrary(StringRef name) {
388390
if (std::optional<std::string> path = searchLibrary(ctx, name))
389-
addFile(saver().save(*path), /*withLOption=*/true);
391+
addFile(saver(ctx).save(*path), /*withLOption=*/true);
390392
else
391393
ctx.errHandler->error("unable to find library -l" + name,
392394
ErrorTag::LibNotFound, {name});
@@ -1675,7 +1677,8 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
16751677

16761678
// Parse LTO options.
16771679
if (auto *arg = args.getLastArg(OPT_plugin_opt_mcpu_eq))
1678-
parseClangOption(ctx, saver().save("-mcpu=" + StringRef(arg->getValue())),
1680+
parseClangOption(ctx,
1681+
saver(ctx).save("-mcpu=" + StringRef(arg->getValue())),
16791682
arg->getSpelling());
16801683

16811684
for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq_minus))
@@ -2622,7 +2625,7 @@ static std::vector<WrappedSymbol> addWrappedSymbols(Ctx &ctx,
26222625
opt::InputArgList &args) {
26232626
std::vector<WrappedSymbol> v;
26242627
DenseSet<StringRef> seen;
2625-
2628+
auto &ss = saver(ctx);
26262629
for (auto *arg : args.filtered(OPT_wrap)) {
26272630
StringRef name = arg->getValue();
26282631
if (!seen.insert(name).second)
@@ -2632,12 +2635,12 @@ static std::vector<WrappedSymbol> addWrappedSymbols(Ctx &ctx,
26322635
if (!sym)
26332636
continue;
26342637

2635-
Symbol *wrap = ctx.symtab->addUnusedUndefined(
2636-
saver().save("__wrap_" + name), sym->binding);
2638+
Symbol *wrap =
2639+
ctx.symtab->addUnusedUndefined(ss.save("__wrap_" + name), sym->binding);
26372640

26382641
// If __real_ is referenced, pull in the symbol if it is lazy. Do this after
26392642
// processing __wrap_ as that may have referenced __real_.
2640-
StringRef realName = saver().save("__real_" + name);
2643+
StringRef realName = saver(ctx).save("__real_" + name);
26412644
if (Symbol *real = ctx.symtab->find(realName)) {
26422645
ctx.symtab->addUnusedUndefined(name, sym->binding);
26432646
// Update sym's binding, which will replace real's later in

lld/ELF/DriverUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static void concatLTOPluginOptions(SmallVectorImpl<const char *> &args) {
9494
for (size_t i = 0, e = args.size(); i != e; ++i) {
9595
StringRef s = args[i];
9696
if ((s == "-plugin-opt" || s == "--plugin-opt") && i + 1 != e) {
97-
v.push_back(saver().save(s + "=" + args[i + 1]).data());
97+
v.push_back(saver(ctx).save(s + "=" + args[i + 1]).data());
9898
++i;
9999
} else {
100100
v.push_back(args[i]);
@@ -117,7 +117,7 @@ opt::InputArgList ELFOptTable::parse(Ctx &ctx, ArrayRef<const char *> argv) {
117117

118118
// Expand response files (arguments in the form of @<filename>)
119119
// and then parse the argument again.
120-
cl::ExpandResponseFiles(saver(), getQuotingStyle(ctx, args), vec);
120+
cl::ExpandResponseFiles(saver(ctx), getQuotingStyle(ctx, args), vec);
121121
concatLTOPluginOptions(vec);
122122
args = this->ParseArgs(vec, missingIndex, missingCount);
123123

lld/ELF/InputFiles.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ std::optional<MemoryBufferRef> elf::readFile(Ctx &ctx, StringRef path) {
222222
// The --chroot option changes our virtual root directory.
223223
// This is useful when you are dealing with files created by --reproduce.
224224
if (!ctx.arg.chroot.empty() && path.starts_with("/"))
225-
path = saver().save(ctx.arg.chroot + path);
225+
path = saver(ctx).save(ctx.arg.chroot + path);
226226

227227
bool remapped = false;
228228
auto it = ctx.arg.remapInputs.find(path);
@@ -427,9 +427,9 @@ static void addDependentLibrary(Ctx &ctx, StringRef specifier,
427427
if (!ctx.arg.dependentLibraries)
428428
return;
429429
if (std::optional<std::string> s = searchLibraryBaseName(ctx, specifier))
430-
ctx.driver.addFile(saver().save(*s), /*withLOption=*/true);
430+
ctx.driver.addFile(saver(ctx).save(*s), /*withLOption=*/true);
431431
else if (std::optional<std::string> s = findFromSearchPaths(ctx, specifier))
432-
ctx.driver.addFile(saver().save(*s), /*withLOption=*/true);
432+
ctx.driver.addFile(saver(ctx).save(*s), /*withLOption=*/true);
433433
else if (fs::exists(specifier))
434434
ctx.driver.addFile(specifier, /*withLOption=*/false);
435435
else
@@ -1515,6 +1515,7 @@ template <class ELFT> void SharedFile::parse() {
15151515
}
15161516

15171517
// DSOs are uniquified not by filename but by soname.
1518+
StringSaver &ss = saver(ctx);
15181519
DenseMap<CachedHashStringRef, SharedFile *>::iterator it;
15191520
bool wasInserted;
15201521
std::tie(it, wasInserted) =
@@ -1581,8 +1582,7 @@ template <class ELFT> void SharedFile::parse() {
15811582
}
15821583
StringRef verName = stringTable.data() + verneeds[idx];
15831584
versionedNameBuffer.clear();
1584-
name = saver().save(
1585-
(name + "@" + verName).toStringRef(versionedNameBuffer));
1585+
name = ss.save((name + "@" + verName).toStringRef(versionedNameBuffer));
15861586
}
15871587
Symbol *s = ctx.symtab->addSymbol(
15881588
Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()});
@@ -1627,7 +1627,7 @@ template <class ELFT> void SharedFile::parse() {
16271627
versionedNameBuffer.clear();
16281628
name = (name + "@" + verName).toStringRef(versionedNameBuffer);
16291629
auto *s = ctx.symtab->addSymbol(
1630-
SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other,
1630+
SharedSymbol{*this, ss.save(name), sym.getBinding(), sym.st_other,
16311631
sym.getType(), sym.st_value, sym.st_size, alignment});
16321632
s->dsoDefined = true;
16331633
if (s->file == this)
@@ -1723,10 +1723,11 @@ BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
17231723
// into consideration at LTO time (which very likely causes undefined
17241724
// symbols later in the link stage). So we append file offset to make
17251725
// filename unique.
1726+
StringSaver &ss = saver(ctx);
17261727
StringRef name = archiveName.empty()
1727-
? saver().save(path)
1728-
: saver().save(archiveName + "(" + path::filename(path) +
1729-
" at " + utostr(offsetInArchive) + ")");
1728+
? ss.save(path)
1729+
: ss.save(archiveName + "(" + path::filename(path) +
1730+
" at " + utostr(offsetInArchive) + ")");
17301731
MemoryBufferRef mbref(mb.getBuffer(), name);
17311732

17321733
obj = CHECK2(lto::InputFile::create(mbref), this);
@@ -1763,7 +1764,7 @@ static void createBitcodeSymbol(Ctx &ctx, Symbol *&sym,
17631764
// Update objSym.Name to reference (via StringRef) the string saver's copy;
17641765
// this way LTO can reference the same string saver's copy rather than
17651766
// keeping copies of its own.
1766-
objSym.Name = uniqueSaver().save(objSym.getName());
1767+
objSym.Name = uniqueSaver(ctx).save(objSym.getName());
17671768
sym = ctx.symtab->insert(objSym.getName());
17681769
}
17691770

@@ -1816,13 +1817,14 @@ void BitcodeFile::parse() {
18161817
void BitcodeFile::parseLazy() {
18171818
numSymbols = obj->symbols().size();
18181819
symbols = std::make_unique<Symbol *[]>(numSymbols);
1820+
auto &ss = uniqueSaver(ctx);
18191821
for (auto [i, irSym] : llvm::enumerate(obj->symbols())) {
18201822
// Symbols can be duplicated in bitcode files because of '#include' and
18211823
// linkonce_odr. Use uniqueSaver to save symbol names for de-duplication.
18221824
// Update objSym.Name to reference (via StringRef) the string saver's copy;
18231825
// this way LTO can reference the same string saver's copy rather than
18241826
// keeping copies of its own.
1825-
irSym.Name = uniqueSaver().save(irSym.getName());
1827+
irSym.Name = ss.save(irSym.getName());
18261828
if (!irSym.isUndefined()) {
18271829
auto *sym = ctx.symtab->insert(irSym.getName());
18281830
sym->resolve(ctx, LazySymbol{*this});
@@ -1859,16 +1861,15 @@ void BinaryFile::parse() {
18591861
if (!isAlnum(c))
18601862
c = '_';
18611863

1862-
llvm::StringSaver &saver = lld::saver();
1863-
1864+
llvm::StringSaver &ss = saver(ctx);
18641865
ctx.symtab->addAndCheckDuplicate(
1865-
ctx, Defined{ctx, this, saver.save(s + "_start"), STB_GLOBAL, STV_DEFAULT,
1866+
ctx, Defined{ctx, this, ss.save(s + "_start"), STB_GLOBAL, STV_DEFAULT,
18661867
STT_OBJECT, 0, 0, section});
18671868
ctx.symtab->addAndCheckDuplicate(
1868-
ctx, Defined{ctx, this, saver.save(s + "_end"), STB_GLOBAL, STV_DEFAULT,
1869+
ctx, Defined{ctx, this, ss.save(s + "_end"), STB_GLOBAL, STV_DEFAULT,
18691870
STT_OBJECT, data.size(), 0, section});
18701871
ctx.symtab->addAndCheckDuplicate(
1871-
ctx, Defined{ctx, this, saver.save(s + "_size"), STB_GLOBAL, STV_DEFAULT,
1872+
ctx, Defined{ctx, this, ss.save(s + "_size"), STB_GLOBAL, STV_DEFAULT,
18721873
STT_OBJECT, data.size(), 0, nullptr});
18731874
}
18741875

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void InputSectionBase::decompress() const {
130130
{
131131
static std::mutex mu;
132132
std::lock_guard<std::mutex> lock(mu);
133-
uncompressedBuf = bAlloc().Allocate<uint8_t>(size);
133+
uncompressedBuf = bAlloc(ctx).Allocate<uint8_t>(size);
134134
}
135135

136136
invokeELFT(decompressAux, ctx, *this, uncompressedBuf, size);

lld/ELF/LTO.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
396396
StringRef ltoObjName;
397397
if (bitcodeFilePath == "ld-temp.o") {
398398
ltoObjName =
399-
saver().save(Twine(ctx.arg.outputFile) + ".lto" +
400-
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
399+
saver(ctx).save(Twine(ctx.arg.outputFile) + ".lto" +
400+
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
401401
} else {
402402
StringRef directory = sys::path::parent_path(bitcodeFilePath);
403403
// For an archive member, which has an identifier like "d/a.a(coll.o at
@@ -411,7 +411,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
411411
sys::path::append(path, directory,
412412
outputFileBaseName + ".lto." + baseName + ext);
413413
sys::path::remove_dots(path, true);
414-
ltoObjName = saver().save(path.str());
414+
ltoObjName = saver(ctx).save(path.str());
415415
}
416416
if (savePrelink || ctx.arg.ltoEmitAsm)
417417
saveBuffer(buf[i].second, ltoObjName);

0 commit comments

Comments
 (0)