Skip to content

Commit b672071

Browse files
committed
[ELF] Pass Ctx & to InputFile
1 parent f0bd62d commit b672071

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

lld/ELF/Driver.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
288288
MemoryBufferRef mbref = *buffer;
289289

290290
if (ctx.arg.formatBinary) {
291-
files.push_back(make<BinaryFile>(mbref));
291+
files.push_back(make<BinaryFile>(ctx, mbref));
292292
return;
293293
}
294294

@@ -304,7 +304,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
304304
files.push_back(
305305
make<BitcodeFile>(ctx, p.first, path, p.second, false));
306306
else if (!tryAddFatLTOFile(p.first, path, p.second, false))
307-
files.push_back(createObjFile(p.first, path));
307+
files.push_back(createObjFile(ctx, p.first, path));
308308
}
309309
return;
310310
}
@@ -328,7 +328,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
328328
auto magic = identify_magic(p.first.getBuffer());
329329
if (magic == file_magic::elf_relocatable) {
330330
if (!tryAddFatLTOFile(p.first, path, p.second, true))
331-
files.push_back(createObjFile(p.first, path, true));
331+
files.push_back(createObjFile(ctx, p.first, path, true));
332332
} else if (magic == file_magic::bitcode)
333333
files.push_back(make<BitcodeFile>(ctx, p.first, path, p.second, true));
334334
else
@@ -361,7 +361,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
361361
break;
362362
case file_magic::elf_relocatable:
363363
if (!tryAddFatLTOFile(mbref, "", 0, inLib))
364-
files.push_back(createObjFile(mbref, "", inLib));
364+
files.push_back(createObjFile(ctx, mbref, "", inLib));
365365
break;
366366
default:
367367
error(path + ": unknown file type");
@@ -1992,7 +1992,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
19921992
break;
19931993
case OPT_just_symbols:
19941994
if (std::optional<MemoryBufferRef> mb = readFile(ctx, arg->getValue())) {
1995-
files.push_back(createObjFile(*mb));
1995+
files.push_back(createObjFile(ctx, *mb));
19961996
files.back()->justSymbols = true;
19971997
}
19981998
break;
@@ -2001,7 +2001,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
20012001
error("multiple CMSE import libraries not supported");
20022002
else if (std::optional<MemoryBufferRef> mb =
20032003
readFile(ctx, arg->getValue()))
2004-
armCmseImpLib = createObjFile(*mb);
2004+
armCmseImpLib = createObjFile(ctx, *mb);
20052005
break;
20062006
case OPT_start_group:
20072007
if (isInGroup)
@@ -2872,7 +2872,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
28722872
for (auto *arg : args.filtered(OPT_trace_symbol))
28732873
ctx.symtab->insert(arg->getValue())->traced = true;
28742874

2875-
ctx.internalFile = createInternalFile("<internal>");
2875+
ctx.internalFile = createInternalFile(ctx, "<internal>");
28762876

28772877
// Handle -u/--undefined before input files. If both a.a and b.so define foo,
28782878
// -u foo a.a b.so will extract a.a.

lld/ELF/InputFiles.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ static void updateSupportedARMFeatures(Ctx &ctx,
202202
ctx.arg.armHasThumb2ISA |= thumb && *thumb >= ARMBuildAttrs::AllowThumb32;
203203
}
204204

205-
InputFile::InputFile(Kind k, MemoryBufferRef m)
206-
: mb(m), groupId(ctx.driver.nextGroupId), fileKind(k) {
205+
InputFile::InputFile(Ctx &ctx, Kind k, MemoryBufferRef m)
206+
: ctx(ctx), mb(m), groupId(ctx.driver.nextGroupId), fileKind(k) {
207207
// All files within the same --{start,end}-group get the same group ID.
208208
// Otherwise, a new file will get a new group ID.
209209
if (!ctx.driver.isInGroup)
@@ -509,8 +509,8 @@ ObjFile<ELFT>::getDILineInfo(const InputSectionBase *s, uint64_t offset) {
509509
return getDwarf()->getDILineInfo(offset, sectionIndex);
510510
}
511511

512-
ELFFileBase::ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef mb)
513-
: InputFile(k, mb) {
512+
ELFFileBase::ELFFileBase(Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef mb)
513+
: InputFile(ctx, k, mb) {
514514
this->ekind = ekind;
515515
}
516516

@@ -950,7 +950,8 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
950950
// hardware-assisted call flow control;
951951
// - AArch64 PAuth ABI core info (16 bytes).
952952
template <class ELFT>
953-
void readGnuProperty(const InputSection &sec, ObjFile<ELFT> &f) {
953+
static void readGnuProperty(Ctx &ctx, const InputSection &sec,
954+
ObjFile<ELFT> &f) {
954955
using Elf_Nhdr = typename ELFT::Nhdr;
955956
using Elf_Note = typename ELFT::Note;
956957

@@ -1070,7 +1071,7 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
10701071
// .note.gnu.property containing a single AND'ed bitmap, we discard an input
10711072
// file's .note.gnu.property section.
10721073
if (name == ".note.gnu.property") {
1073-
readGnuProperty<ELFT>(InputSection(*this, sec, name), *this);
1074+
readGnuProperty<ELFT>(ctx, InputSection(*this, sec, name), *this);
10741075
return &InputSection::discarded;
10751076
}
10761077

@@ -1127,10 +1128,10 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
11271128
}
11281129

11291130
// Some entries have been filled by LazyObjFile.
1131+
auto *symtab = ctx.symtab.get();
11301132
for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
11311133
if (!symbols[i])
1132-
symbols[i] =
1133-
ctx.symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
1134+
symbols[i] = symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
11341135

11351136
// Perform symbol resolution on non-local symbols.
11361137
SmallVector<unsigned, 32> undefineds;
@@ -1325,7 +1326,7 @@ static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName,
13251326
template <class ELFT>
13261327
static bool isNonCommonDef(ELFKind ekind, MemoryBufferRef mb, StringRef symName,
13271328
StringRef archiveName) {
1328-
ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ekind, mb, archiveName);
1329+
ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ctx, ekind, mb, archiveName);
13291330
obj->init();
13301331
StringRef stringtable = obj->getStringTable();
13311332

@@ -1357,7 +1358,7 @@ static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
13571358
unsigned SharedFile::vernauxNum;
13581359

13591360
SharedFile::SharedFile(Ctx &ctx, MemoryBufferRef m, StringRef defaultSoName)
1360-
: ELFFileBase(SharedKind, getELFKind(m, ""), m), soName(defaultSoName),
1361+
: ELFFileBase(ctx, SharedKind, getELFKind(m, ""), m), soName(defaultSoName),
13611362
isNeeded(!ctx.arg.asNeeded) {}
13621363

13631364
// Parse the version definitions in the object file if present, and return a
@@ -1697,7 +1698,7 @@ static uint8_t getOsAbi(const Triple &t) {
16971698

16981699
BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
16991700
uint64_t offsetInArchive, bool lazy)
1700-
: InputFile(BitcodeKind, mb) {
1701+
: InputFile(ctx, BitcodeKind, mb) {
17011702
this->archiveName = archiveName;
17021703
this->lazy = lazy;
17031704

@@ -1859,30 +1860,30 @@ void BinaryFile::parse() {
18591860
data.size(), 0, nullptr});
18601861
}
18611862

1862-
InputFile *elf::createInternalFile(StringRef name) {
1863+
InputFile *elf::createInternalFile(Ctx &ctx, StringRef name) {
18631864
auto *file =
1864-
make<InputFile>(InputFile::InternalKind, MemoryBufferRef("", name));
1865+
make<InputFile>(ctx, InputFile::InternalKind, MemoryBufferRef("", name));
18651866
// References from an internal file do not lead to --warn-backrefs
18661867
// diagnostics.
18671868
file->groupId = 0;
18681869
return file;
18691870
}
18701871

1871-
ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName,
1872-
bool lazy) {
1872+
ELFFileBase *elf::createObjFile(Ctx &ctx, MemoryBufferRef mb,
1873+
StringRef archiveName, bool lazy) {
18731874
ELFFileBase *f;
18741875
switch (getELFKind(mb, archiveName)) {
18751876
case ELF32LEKind:
1876-
f = make<ObjFile<ELF32LE>>(ELF32LEKind, mb, archiveName);
1877+
f = make<ObjFile<ELF32LE>>(ctx, ELF32LEKind, mb, archiveName);
18771878
break;
18781879
case ELF32BEKind:
1879-
f = make<ObjFile<ELF32BE>>(ELF32BEKind, mb, archiveName);
1880+
f = make<ObjFile<ELF32BE>>(ctx, ELF32BEKind, mb, archiveName);
18801881
break;
18811882
case ELF64LEKind:
1882-
f = make<ObjFile<ELF64LE>>(ELF64LEKind, mb, archiveName);
1883+
f = make<ObjFile<ELF64LE>>(ctx, ELF64LEKind, mb, archiveName);
18831884
break;
18841885
case ELF64BEKind:
1885-
f = make<ObjFile<ELF64BE>>(ELF64BEKind, mb, archiveName);
1886+
f = make<ObjFile<ELF64BE>>(ctx, ELF64BEKind, mb, archiveName);
18861887
break;
18871888
default:
18881889
llvm_unreachable("getELFKind");

lld/ELF/InputFiles.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void parseFiles(Ctx &, const std::vector<InputFile *> &files);
4949
// The root class of input files.
5050
class InputFile {
5151
protected:
52+
Ctx &ctx;
5253
std::unique_ptr<Symbol *[]> symbols;
5354
uint32_t numSymbols = 0;
5455
SmallVector<InputSectionBase *, 0> sections;
@@ -62,7 +63,7 @@ class InputFile {
6263
InternalKind,
6364
};
6465

65-
InputFile(Kind k, MemoryBufferRef m);
66+
InputFile(Ctx &, Kind k, MemoryBufferRef m);
6667
Kind kind() const { return fileKind; }
6768

6869
bool isElf() const {
@@ -175,7 +176,7 @@ class InputFile {
175176

176177
class ELFFileBase : public InputFile {
177178
public:
178-
ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef m);
179+
ELFFileBase(Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef m);
179180
static bool classof(const InputFile *f) { return f->isElf(); }
180181

181182
void init();
@@ -239,8 +240,8 @@ template <class ELFT> class ObjFile : public ELFFileBase {
239240
return this->ELFFileBase::getObj<ELFT>();
240241
}
241242

242-
ObjFile(ELFKind ekind, MemoryBufferRef m, StringRef archiveName)
243-
: ELFFileBase(ObjKind, ekind, m) {
243+
ObjFile(Ctx &ctx, ELFKind ekind, MemoryBufferRef m, StringRef archiveName)
244+
: ELFFileBase(ctx, ObjKind, ekind, m) {
244245
this->archiveName = archiveName;
245246
}
246247

@@ -371,14 +372,15 @@ class SharedFile : public ELFFileBase {
371372

372373
class BinaryFile : public InputFile {
373374
public:
374-
explicit BinaryFile(MemoryBufferRef m) : InputFile(BinaryKind, m) {}
375+
explicit BinaryFile(Ctx &ctx, MemoryBufferRef m)
376+
: InputFile(ctx, BinaryKind, m) {}
375377
static bool classof(const InputFile *f) { return f->kind() == BinaryKind; }
376378
void parse();
377379
};
378380

379-
InputFile *createInternalFile(StringRef name);
380-
ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "",
381-
bool lazy = false);
381+
InputFile *createInternalFile(Ctx &, StringRef name);
382+
ELFFileBase *createObjFile(Ctx &, MemoryBufferRef mb,
383+
StringRef archiveName = "", bool lazy = false);
382384

383385
std::string replaceThinLTOSuffix(Ctx &, StringRef path);
384386

lld/ELF/LTO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
413413
if (savePrelink || ctx.arg.ltoEmitAsm)
414414
saveBuffer(buf[i].second, ltoObjName);
415415
if (!ctx.arg.ltoEmitAsm)
416-
ret.push_back(createObjFile(MemoryBufferRef(objBuf, ltoObjName)));
416+
ret.push_back(createObjFile(ctx, MemoryBufferRef(objBuf, ltoObjName)));
417417
}
418418
return ret;
419419
}

lld/ELF/LinkerScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void LinkerScript::addSymbol(SymbolAssignment *cmd) {
227227
// write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
228228
uint64_t symValue = value.sec ? 0 : value.getValue();
229229

230-
Defined newSym(createInternalFile(cmd->location), cmd->name, STB_GLOBAL,
230+
Defined newSym(createInternalFile(ctx, cmd->location), cmd->name, STB_GLOBAL,
231231
visibility, value.type, symValue, 0, sec);
232232

233233
Symbol *sym = ctx.symtab->insert(cmd->name);

0 commit comments

Comments
 (0)