Skip to content

Commit dd326b1

Browse files
committed
[ELF] Pass Ctx &
1 parent d0606c2 commit dd326b1

File tree

8 files changed

+37
-28
lines changed

8 files changed

+37
-28
lines changed

lld/ELF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ static void readCallGraph(Ctx &ctx, MemoryBufferRef mb) {
969969
warn(mb.getBufferIdentifier() + ": no such symbol: " + name);
970970
return nullptr;
971971
}
972-
maybeWarnUnorderableSymbol(sym);
972+
maybeWarnUnorderableSymbol(ctx, sym);
973973

974974
if (Defined *dr = dyn_cast_or_null<Defined>(sym))
975975
return dyn_cast_or_null<InputSectionBase>(dr->section);

lld/ELF/DriverUtils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ std::string elf::createResponseFile(const opt::InputArgList &args) {
210210

211211
// Find a file by concatenating given paths. If a resulting path
212212
// starts with "=", the character is replaced with a --sysroot value.
213-
static std::optional<std::string> findFile(StringRef path1,
213+
static std::optional<std::string> findFile(Ctx &ctx, StringRef path1,
214214
const Twine &path2) {
215215
SmallString<128> s;
216216
if (path1.starts_with("="))
@@ -225,7 +225,7 @@ static std::optional<std::string> findFile(StringRef path1,
225225

226226
std::optional<std::string> elf::findFromSearchPaths(Ctx &ctx, StringRef path) {
227227
for (StringRef dir : ctx.arg.searchPaths)
228-
if (std::optional<std::string> s = findFile(dir, path))
228+
if (std::optional<std::string> s = findFile(ctx, dir, path))
229229
return s;
230230
return std::nullopt;
231231
}
@@ -236,9 +236,10 @@ std::optional<std::string> elf::searchLibraryBaseName(Ctx &ctx,
236236
StringRef name) {
237237
for (StringRef dir : ctx.arg.searchPaths) {
238238
if (!ctx.arg.isStatic)
239-
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
239+
if (std::optional<std::string> s =
240+
findFile(ctx, dir, "lib" + name + ".so"))
240241
return s;
241-
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".a"))
242+
if (std::optional<std::string> s = findFile(ctx, dir, "lib" + name + ".a"))
242243
return s;
243244
}
244245
return std::nullopt;

lld/ELF/InputFiles.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ static bool isNonCommonDef(Ctx &ctx, ELFKind ekind, MemoryBufferRef mb,
13391339
return false;
13401340
}
13411341

1342-
static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
1342+
static bool isNonCommonDef(Ctx &ctx, MemoryBufferRef mb, StringRef symName,
13431343
StringRef archiveName) {
13441344
switch (getELFKind(mb, archiveName)) {
13451345
case ELF32LEKind:
@@ -1916,7 +1916,7 @@ bool InputFile::shouldExtractForCommon(StringRef name) const {
19161916
if (isa<BitcodeFile>(this))
19171917
return isBitcodeNonCommonDef(mb, name, archiveName);
19181918

1919-
return isNonCommonDef(mb, name, archiveName);
1919+
return isNonCommonDef(ctx, mb, name, archiveName);
19201920
}
19211921

19221922
std::string elf::replaceThinLTOSuffix(Ctx &ctx, StringRef path) {

lld/ELF/Symbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static void recordWhyExtract(const InputFile *reference,
298298
ctx.whyExtractRecords.emplace_back(toString(reference), &extracted, sym);
299299
}
300300

301-
void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
301+
void elf::maybeWarnUnorderableSymbol(Ctx &ctx, const Symbol *sym) {
302302
if (!ctx.arg.warnSymbolOrdering)
303303
return;
304304

lld/ELF/Symbols.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ template <typename... T> Defined *makeDefined(T &&...args) {
527527

528528
void reportDuplicate(Ctx &, const Symbol &sym, const InputFile *newFile,
529529
InputSectionBase *errSec, uint64_t errOffset);
530-
void maybeWarnUnorderableSymbol(const Symbol *sym);
530+
void maybeWarnUnorderableSymbol(Ctx &, const Symbol *sym);
531531
bool computeIsPreemptible(Ctx &, const Symbol &sym);
532532

533533
} // namespace elf

lld/ELF/SyntheticSections.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,9 @@ uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
16301630

16311631
size_t index = symTab->getSymbolIndex(*sym);
16321632
assert((index != 0 ||
1633-
(type != ctx.target->gotRel && type != ctx.target->pltRel) ||
1634-
!ctx.mainPart->dynSymTab->getParent()) &&
1633+
(type != symTab->ctx.target->gotRel &&
1634+
type != symTab->ctx.target->pltRel) ||
1635+
!symTab->ctx.mainPart->dynSymTab->getParent()) &&
16351636
"GOT or PLT relocation must refer to symbol in dynamic symbol table");
16361637
return index;
16371638
}
@@ -2241,8 +2242,8 @@ SymbolTableSection<ELFT>::SymbolTableSection(Ctx &ctx,
22412242
this->entsize = sizeof(Elf_Sym);
22422243
}
22432244

2244-
static BssSection *getCommonSec(Symbol *sym) {
2245-
if (ctx.arg.relocatable)
2245+
static BssSection *getCommonSec(bool relocatable, Symbol *sym) {
2246+
if (relocatable)
22462247
if (auto *d = dyn_cast<Defined>(sym))
22472248
return dyn_cast_or_null<BssSection>(d->section);
22482249
return nullptr;
@@ -2264,7 +2265,7 @@ template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
22642265
buf += sizeof(Elf_Sym);
22652266

22662267
auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2267-
2268+
bool relocatable = ctx.arg.relocatable;
22682269
for (SymbolTableEntry &ent : symbols) {
22692270
Symbol *sym = ent.sym;
22702271
bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition;
@@ -2274,7 +2275,7 @@ template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
22742275
eSym->setBindingAndType(sym->binding, sym->type);
22752276
eSym->st_other = sym->stOther;
22762277

2277-
if (BssSection *commonSec = getCommonSec(sym)) {
2278+
if (BssSection *commonSec = getCommonSec(relocatable, sym)) {
22782279
// When -r is specified, a COMMON symbol is not allocated. Its st_shndx
22792280
// holds SHN_COMMON and st_value holds the alignment.
22802281
eSym->st_shndx = SHN_COMMON;
@@ -2347,8 +2348,10 @@ void SymtabShndxSection::writeTo(uint8_t *buf) {
23472348
// SHN_XINDEX, we need to write actual index, otherwise, we must write
23482349
// SHN_UNDEF(0).
23492350
buf += 4; // Ignore .symtab[0] entry.
2351+
bool relocatable = ctx.arg.relocatable;
23502352
for (const SymbolTableEntry &entry : ctx.in.symTab->getSymbols()) {
2351-
if (!getCommonSec(entry.sym) && getSymSectionIndex(entry.sym) == SHN_XINDEX)
2353+
if (!getCommonSec(relocatable, entry.sym) &&
2354+
getSymSectionIndex(entry.sym) == SHN_XINDEX)
23522355
write32(buf, entry.sym->getOutputSection()->sectionIndex);
23532356
buf += 4;
23542357
}
@@ -4384,7 +4387,8 @@ static uint8_t getAbiVersion(Ctx &ctx) {
43844387
return 0;
43854388
}
43864389

4387-
template <typename ELFT> void elf::writeEhdr(uint8_t *buf, Partition &part) {
4390+
template <typename ELFT>
4391+
void elf::writeEhdr(Ctx &ctx, uint8_t *buf, Partition &part) {
43884392
memcpy(buf, "\177ELF", 4);
43894393

43904394
auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
@@ -4434,7 +4438,7 @@ size_t PartitionElfHeaderSection<ELFT>::getSize() const {
44344438

44354439
template <typename ELFT>
44364440
void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {
4437-
writeEhdr<ELFT>(buf, getPartition());
4441+
writeEhdr<ELFT>(ctx, buf, getPartition());
44384442

44394443
// Loadable partitions are always ET_DYN.
44404444
auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
@@ -4970,10 +4974,10 @@ template class elf::SymbolTableSection<ELF32BE>;
49704974
template class elf::SymbolTableSection<ELF64LE>;
49714975
template class elf::SymbolTableSection<ELF64BE>;
49724976

4973-
template void elf::writeEhdr<ELF32LE>(uint8_t *Buf, Partition &Part);
4974-
template void elf::writeEhdr<ELF32BE>(uint8_t *Buf, Partition &Part);
4975-
template void elf::writeEhdr<ELF64LE>(uint8_t *Buf, Partition &Part);
4976-
template void elf::writeEhdr<ELF64BE>(uint8_t *Buf, Partition &Part);
4977+
template void elf::writeEhdr<ELF32LE>(Ctx &, uint8_t *Buf, Partition &Part);
4978+
template void elf::writeEhdr<ELF32BE>(Ctx &, uint8_t *Buf, Partition &Part);
4979+
template void elf::writeEhdr<ELF64LE>(Ctx &, uint8_t *Buf, Partition &Part);
4980+
template void elf::writeEhdr<ELF64BE>(Ctx &, uint8_t *Buf, Partition &Part);
49774981

49784982
template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part);
49794983
template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);

lld/ELF/SyntheticSections.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ void combineEhSections(Ctx &);
14361436
bool hasMemtag(Ctx &);
14371437
bool canHaveMemtagGlobals(Ctx &);
14381438

1439-
template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
1439+
template <typename ELFT> void writeEhdr(Ctx &, uint8_t *buf, Partition &part);
14401440
template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
14411441

14421442
Defined *addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,

lld/ELF/Writer.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
777777
return rank;
778778
}
779779

780-
static bool compareSections(const SectionCommand *aCmd,
780+
static bool compareSections(Ctx &ctx, const SectionCommand *aCmd,
781781
const SectionCommand *bCmd) {
782782
const OutputSection *a = &cast<OutputDesc>(aCmd)->osec;
783783
const OutputSection *b = &cast<OutputDesc>(bCmd)->osec;
@@ -1098,7 +1098,7 @@ static DenseMap<const InputSectionBase *, int> buildSectionOrder(Ctx &ctx) {
10981098
SymbolOrderEntry &ent = it->second;
10991099
ent.present = true;
11001100

1101-
maybeWarnUnorderableSymbol(&sym);
1101+
maybeWarnUnorderableSymbol(ctx, &sym);
11021102

11031103
if (auto *d = dyn_cast<Defined>(&sym)) {
11041104
if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
@@ -1273,7 +1273,9 @@ template <class ELFT> void Writer<ELFT>::sortSections() {
12731273
auto mid = std::stable_partition(
12741274
ctx.script->sectionCommands.begin(), ctx.script->sectionCommands.end(),
12751275
[](SectionCommand *cmd) { return isa<OutputDesc>(cmd); });
1276-
std::stable_sort(ctx.script->sectionCommands.begin(), mid, compareSections);
1276+
std::stable_sort(
1277+
ctx.script->sectionCommands.begin(), mid,
1278+
[&ctx = ctx](auto *l, auto *r) { return compareSections(ctx, l, r); });
12771279
}
12781280

12791281
// Process INSERT commands and update output section attributes. From this
@@ -1336,7 +1338,9 @@ template <class ELFT> void Writer<ELFT>::sortOrphanSections() {
13361338
});
13371339

13381340
// Sort the orphan sections.
1339-
std::stable_sort(nonScriptI, e, compareSections);
1341+
std::stable_sort(nonScriptI, e, [&ctx = ctx](auto *l, auto *r) {
1342+
return compareSections(ctx, l, r);
1343+
});
13401344

13411345
// As a horrible special case, skip the first . assignment if it is before any
13421346
// section. We do this because it is common to set a load address by starting
@@ -2730,7 +2734,7 @@ static uint16_t getELFType(Ctx &ctx) {
27302734
}
27312735

27322736
template <class ELFT> void Writer<ELFT>::writeHeader() {
2733-
writeEhdr<ELFT>(ctx.bufferStart, *ctx.mainPart);
2737+
writeEhdr<ELFT>(ctx, ctx.bufferStart, *ctx.mainPart);
27342738
writePhdrs<ELFT>(ctx.bufferStart + sizeof(Elf_Ehdr), *ctx.mainPart);
27352739

27362740
auto *eHdr = reinterpret_cast<Elf_Ehdr *>(ctx.bufferStart);

0 commit comments

Comments
 (0)