Skip to content

Commit 9bf2e20

Browse files
committed
[ELF] Pass Ctx & to OutputSection
1 parent 6dd773b commit 9bf2e20

File tree

8 files changed

+41
-32
lines changed

8 files changed

+41
-32
lines changed

lld/ELF/Arch/ARM.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,9 +1452,11 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
14521452
make<SymbolTableSection<ELFT>>(ctx, *strtab);
14531453

14541454
SmallVector<std::pair<OutputSection *, SyntheticSection *>, 0> osIsPairs;
1455-
osIsPairs.emplace_back(make<OutputSection>(strtab->name, 0, 0), strtab);
1456-
osIsPairs.emplace_back(make<OutputSection>(impSymTab->name, 0, 0), impSymTab);
1457-
osIsPairs.emplace_back(make<OutputSection>(shstrtab->name, 0, 0), shstrtab);
1455+
osIsPairs.emplace_back(make<OutputSection>(ctx, strtab->name, 0, 0), strtab);
1456+
osIsPairs.emplace_back(make<OutputSection>(ctx, impSymTab->name, 0, 0),
1457+
impSymTab);
1458+
osIsPairs.emplace_back(make<OutputSection>(ctx, shstrtab->name, 0, 0),
1459+
shstrtab);
14581460

14591461
std::sort(ctx.symtab->cmseSymMap.begin(), ctx.symtab->cmseSymMap.end(),
14601462
[](const auto &a, const auto &b) -> bool {
@@ -1473,7 +1475,7 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
14731475
for (auto &[osec, isec] : osIsPairs) {
14741476
osec->sectionIndex = ++idx;
14751477
osec->recordSection(isec);
1476-
osec->finalizeInputSections(ctx);
1478+
osec->finalizeInputSections();
14771479
osec->shName = shstrtab->addString(osec->name);
14781480
osec->size = isec->getSize();
14791481
isec->finalizeContents();

lld/ELF/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,7 +2975,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
29752975

29762976
// Create elfHeader early. We need a dummy section in
29772977
// addReservedSymbols to mark the created symbols as not absolute.
2978-
ctx.out.elfHeader = make<OutputSection>("", 0, SHF_ALLOC);
2978+
ctx.out.elfHeader = make<OutputSection>(ctx, "", 0, SHF_ALLOC);
29792979

29802980
// We need to create some reserved symbols such as _end. Create them.
29812981
if (!ctx.arg.relocatable)
@@ -3200,7 +3200,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
32003200
// sectionBases.
32013201
for (SectionCommand *cmd : ctx.script->sectionCommands)
32023202
if (auto *osd = dyn_cast<OutputDesc>(cmd))
3203-
osd->osec.finalizeInputSections(ctx);
3203+
osd->osec.finalizeInputSections();
32043204
}
32053205

32063206
// Two input sections with different output sections should not be folded.

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ static size_t findNull(StringRef s, size_t entSize) {
13891389
// Split SHF_STRINGS section. Such section is a sequence of
13901390
// null-terminated strings.
13911391
void MergeInputSection::splitStrings(StringRef s, size_t entSize) {
1392-
const bool live = !(flags & SHF_ALLOC) || !ctx.arg.gcSections;
1392+
const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections;
13931393
const char *p = s.data(), *end = s.data() + s.size();
13941394
if (!std::all_of(end - entSize, end, [](char c) { return c == 0; }))
13951395
fatal(toString(this) + ": string is not null terminated");

lld/ELF/LinkerScript.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ OutputDesc *LinkerScript::createOutputSection(StringRef name,
140140
// There was a forward reference.
141141
sec = secRef;
142142
} else {
143-
sec = make<OutputDesc>(name, SHT_PROGBITS, 0);
143+
sec = make<OutputDesc>(ctx, name, SHT_PROGBITS, 0);
144144
if (!secRef)
145145
secRef = sec;
146146
}
@@ -151,7 +151,7 @@ OutputDesc *LinkerScript::createOutputSection(StringRef name,
151151
OutputDesc *LinkerScript::getOrCreateOutputSection(StringRef name) {
152152
OutputDesc *&cmdRef = nameToOutputSection[CachedHashStringRef(name)];
153153
if (!cmdRef)
154-
cmdRef = make<OutputDesc>(name, SHT_PROGBITS, 0);
154+
cmdRef = make<OutputDesc>(ctx, name, SHT_PROGBITS, 0);
155155
return cmdRef;
156156
}
157157

@@ -830,7 +830,7 @@ void LinkerScript::processSymbolAssignments() {
830830
// sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
831831
// that fills the void outside a section. It has an index of one, which is
832832
// indistinguishable from any other regular section index.
833-
aether = make<OutputSection>("", 0, SHF_ALLOC);
833+
aether = make<OutputSection>(ctx, "", 0, SHF_ALLOC);
834834
aether->sectionIndex = 1;
835835

836836
// `st` captures the local AddressState and makes it accessible deliberately.

lld/ELF/OutputSections.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
6565
shdr->sh_name = shName;
6666
}
6767

68-
OutputSection::OutputSection(StringRef name, uint32_t type, uint64_t flags)
68+
OutputSection::OutputSection(Ctx &ctx, StringRef name, uint32_t type,
69+
uint64_t flags)
6970
: SectionBase(Output, ctx.internalFile, name, flags, /*entsize=*/0,
7071
/*addralign=*/1, type,
71-
/*info=*/0, /*link=*/0) {}
72+
/*info=*/0, /*link=*/0),
73+
ctx(ctx) {}
7274

7375
// We allow sections of types listed below to merged into a
7476
// single progbits section. This is typically done by linker
@@ -106,7 +108,7 @@ void OutputSection::recordSection(InputSectionBase *isec) {
106108
// Update fields (type, flags, alignment, etc) according to the InputSection
107109
// isec. Also check whether the InputSection flags and type are consistent with
108110
// other InputSections.
109-
void OutputSection::commitSection(Ctx &ctx, InputSection *isec) {
111+
void OutputSection::commitSection(InputSection *isec) {
110112
if (LLVM_UNLIKELY(type != isec->type)) {
111113
if (!hasInputSections && !typeIsSet) {
112114
type = isec->type;
@@ -189,7 +191,7 @@ static MergeSyntheticSection *createMergeSynthetic(Ctx &ctx, StringRef name,
189191
// new synthetic sections at the location of the first input section
190192
// that it replaces. It then finalizes each synthetic section in order
191193
// to compute an output offset for each piece of each input section.
192-
void OutputSection::finalizeInputSections(Ctx &ctx) {
194+
void OutputSection::finalizeInputSections() {
193195
auto *script = ctx.script;
194196
std::vector<MergeSyntheticSection *> mergeSections;
195197
for (SectionCommand *cmd : commands) {
@@ -245,7 +247,7 @@ void OutputSection::finalizeInputSections(Ctx &ctx) {
245247

246248
// Some input sections may be removed from the list after ICF.
247249
for (InputSection *s : isd->sections)
248-
commitSection(ctx, s);
250+
commitSection(s);
249251
}
250252
for (auto *ms : mergeSections)
251253
ms->finalizeContents();
@@ -610,8 +612,9 @@ static void finalizeShtGroup(Ctx &ctx, OutputSection *os,
610612

611613
template <class uint>
612614
LLVM_ATTRIBUTE_ALWAYS_INLINE static void
613-
encodeOneCrel(raw_svector_ostream &os, Elf_Crel<sizeof(uint) == 8> &out,
614-
uint offset, const Symbol &sym, uint32_t type, uint addend) {
615+
encodeOneCrel(Ctx &ctx, raw_svector_ostream &os,
616+
Elf_Crel<sizeof(uint) == 8> &out, uint offset, const Symbol &sym,
617+
uint32_t type, uint addend) {
615618
const auto deltaOffset = static_cast<uint64_t>(offset - out.r_offset);
616619
out.r_offset = offset;
617620
int64_t symidx = ctx.in.symTab->getSymbolIndex(sym);
@@ -652,8 +655,9 @@ encodeOneCrel(raw_svector_ostream &os, Elf_Crel<sizeof(uint) == 8> &out,
652655
}
653656

654657
template <class ELFT>
655-
static size_t relToCrel(raw_svector_ostream &os, Elf_Crel<ELFT::Is64Bits> &out,
656-
InputSection *relSec, InputSectionBase *sec) {
658+
static size_t relToCrel(Ctx &ctx, raw_svector_ostream &os,
659+
Elf_Crel<ELFT::Is64Bits> &out, InputSection *relSec,
660+
InputSectionBase *sec) {
657661
const auto &file = *cast<ELFFileBase>(relSec->file);
658662
if (relSec->type == SHT_REL) {
659663
// REL conversion is complex and unsupported yet.
@@ -663,7 +667,7 @@ static size_t relToCrel(raw_svector_ostream &os, Elf_Crel<ELFT::Is64Bits> &out,
663667
auto rels = relSec->getDataAs<typename ELFT::Rela>();
664668
for (auto rel : rels) {
665669
encodeOneCrel<typename ELFT::uint>(
666-
os, out, sec->getVA(rel.r_offset), file.getRelocTargetSym(rel),
670+
ctx, os, out, sec->getVA(rel.r_offset), file.getRelocTargetSym(rel),
667671
rel.getType(ctx.arg.isMips64EL), getAddend<ELFT>(rel));
668672
}
669673
return rels.size();
@@ -685,19 +689,21 @@ template <bool is64> void OutputSection::finalizeNonAllocCrel(Ctx &ctx) {
685689
RelocsCrel<is64> entries(relSec->content_);
686690
totalCount += entries.size();
687691
for (Elf_Crel_Impl<is64> r : entries) {
688-
encodeOneCrel<uint>(os, out, uint(sec->getVA(r.r_offset)),
692+
encodeOneCrel<uint>(ctx, os, out, uint(sec->getVA(r.r_offset)),
689693
file.getSymbol(r.r_symidx), r.r_type, r.r_addend);
690694
}
691695
continue;
692696
}
693697

694698
// Convert REL[A] to CREL.
695699
if constexpr (is64) {
696-
totalCount += ctx.arg.isLE ? relToCrel<ELF64LE>(os, out, relSec, sec)
697-
: relToCrel<ELF64BE>(os, out, relSec, sec);
700+
totalCount += ctx.arg.isLE
701+
? relToCrel<ELF64LE>(ctx, os, out, relSec, sec)
702+
: relToCrel<ELF64BE>(ctx, os, out, relSec, sec);
698703
} else {
699-
totalCount += ctx.arg.isLE ? relToCrel<ELF32LE>(os, out, relSec, sec)
700-
: relToCrel<ELF32BE>(os, out, relSec, sec);
704+
totalCount += ctx.arg.isLE
705+
? relToCrel<ELF32LE>(ctx, os, out, relSec, sec)
706+
: relToCrel<ELF32BE>(ctx, os, out, relSec, sec);
701707
}
702708
}
703709

lld/ELF/OutputSections.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct CompressedData {
3535
// non-overlapping file offsets and VAs.
3636
class OutputSection final : public SectionBase {
3737
public:
38-
OutputSection(StringRef name, uint32_t type, uint64_t flags);
38+
OutputSection(Ctx &, StringRef name, uint32_t type, uint64_t flags);
3939

4040
static bool classof(const SectionBase *s) {
4141
return s->kind() == SectionBase::Output;
@@ -44,6 +44,7 @@ class OutputSection final : public SectionBase {
4444
uint64_t getLMA() const { return ptLoad ? addr + ptLoad->lmaOffset : addr; }
4545
template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *sHdr);
4646

47+
Ctx &ctx;
4748
uint32_t sectionIndex = UINT32_MAX;
4849
unsigned sortRank;
4950

@@ -74,8 +75,8 @@ class OutputSection final : public SectionBase {
7475
uint32_t shName = 0;
7576

7677
void recordSection(InputSectionBase *isec);
77-
void commitSection(Ctx &ctx, InputSection *isec);
78-
void finalizeInputSections(Ctx &ctx);
78+
void commitSection(InputSection *isec);
79+
void finalizeInputSections();
7980

8081
// The following members are normally only used in linker scripts.
8182
MemoryRegion *memRegion = nullptr;
@@ -135,8 +136,8 @@ class OutputSection final : public SectionBase {
135136

136137
struct OutputDesc final : SectionCommand {
137138
OutputSection osec;
138-
OutputDesc(StringRef name, uint32_t type, uint64_t flags)
139-
: SectionCommand(OutputSectionKind), osec(name, type, flags) {}
139+
OutputDesc(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags)
140+
: SectionCommand(OutputSectionKind), osec(ctx, name, type, flags) {}
140141

141142
static bool classof(const SectionCommand *c) {
142143
return c->kind == OutputSectionKind;

lld/ELF/Relocations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ template <class ELFT> static void addCopyRelSymbol(Ctx &ctx, SharedSymbol &ss) {
392392
osec->commands.push_back(make<InputSectionDescription>(""));
393393
auto *isd = cast<InputSectionDescription>(osec->commands.back());
394394
isd->sections.push_back(sec);
395-
osec->commitSection(ctx, sec);
395+
osec->commitSection(sec);
396396

397397
// Look through the DSO's dynamic symbol table for aliases and create a
398398
// dynamic symbol for each one. This causes the copy relocation to correctly

lld/ELF/SyntheticSections.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4692,7 +4692,7 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
46924692
ctx.in.shStrTab =
46934693
std::make_unique<StringTableSection>(ctx, ".shstrtab", false);
46944694

4695-
ctx.out.programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
4695+
ctx.out.programHeaders = make<OutputSection>(ctx, "", 0, SHF_ALLOC);
46964696
ctx.out.programHeaders->addralign = ctx.arg.wordsize;
46974697

46984698
if (ctx.arg.strip != StripPolicy::All) {

0 commit comments

Comments
 (0)