Skip to content

Commit c331332

Browse files
committed
[ELF] Pass Ctx & to InputSection
1 parent 23c64be commit c331332

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

lld/ELF/InputSection.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,20 @@ InputSectionBase *InputSection::getRelocatedSection() const {
400400
}
401401

402402
template <class ELFT, class RelTy>
403-
void InputSection::copyRelocations(uint8_t *buf) {
403+
void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf) {
404404
if (ctx.arg.relax && !ctx.arg.relocatable &&
405405
(ctx.arg.emachine == EM_RISCV || ctx.arg.emachine == EM_LOONGARCH)) {
406406
// On LoongArch and RISC-V, relaxation might change relocations: copy
407407
// from internal ones that are updated by relaxation.
408408
InputSectionBase *sec = getRelocatedSection();
409-
copyRelocations<ELFT, RelTy>(buf, llvm::make_range(sec->relocations.begin(),
410-
sec->relocations.end()));
409+
copyRelocations<ELFT, RelTy>(
410+
ctx, buf,
411+
llvm::make_range(sec->relocations.begin(), sec->relocations.end()));
411412
} else {
412413
// Convert the raw relocations in the input section into Relocation objects
413414
// suitable to be used by copyRelocations below.
414415
struct MapRel {
416+
Ctx &ctx;
415417
const ObjFile<ELFT> &file;
416418
Relocation operator()(const RelTy &rel) const {
417419
// RelExpr is not used so set to a dummy value.
@@ -423,21 +425,20 @@ void InputSection::copyRelocations(uint8_t *buf) {
423425
using RawRels = ArrayRef<RelTy>;
424426
using MapRelIter =
425427
llvm::mapped_iterator<typename RawRels::iterator, MapRel>;
426-
auto mapRel = MapRel{*getFile<ELFT>()};
428+
auto mapRel = MapRel{ctx, *getFile<ELFT>()};
427429
RawRels rawRels = getDataAs<RelTy>();
428430
auto rels = llvm::make_range(MapRelIter(rawRels.begin(), mapRel),
429431
MapRelIter(rawRels.end(), mapRel));
430-
copyRelocations<ELFT, RelTy>(buf, rels);
432+
copyRelocations<ELFT, RelTy>(ctx, buf, rels);
431433
}
432434
}
433435

434436
// This is used for -r and --emit-relocs. We can't use memcpy to copy
435437
// relocations because we need to update symbol table offset and section index
436438
// for each relocation. So we copy relocations one by one.
437439
template <class ELFT, class RelTy, class RelIt>
438-
void InputSection::copyRelocations(uint8_t *buf,
440+
void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf,
439441
llvm::iterator_range<RelIt> rels) {
440-
Ctx &ctx = getCtx();
441442
const TargetInfo &target = *ctx.target;
442443
InputSectionBase *sec = getRelocatedSection();
443444
(void)sec->contentMaybeDecompress(); // uncompress if needed
@@ -973,9 +974,10 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
973974
// So, we handle relocations for non-alloc sections directly in this
974975
// function as a performance optimization.
975976
template <class ELFT, class RelTy>
976-
void InputSection::relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels) {
977+
void InputSection::relocateNonAlloc(Ctx &ctx, uint8_t *buf,
978+
Relocs<RelTy> rels) {
977979
const unsigned bits = sizeof(typename ELFT::uint) * 8;
978-
const TargetInfo &target = *elf::ctx.target;
980+
const TargetInfo &target = *ctx.target;
979981
const auto emachine = ctx.arg.emachine;
980982
const bool isDebug = isDebugSection(*this);
981983
const bool isDebugLine = isDebug && name == ".debug_line";
@@ -1123,9 +1125,9 @@ void InputSection::relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels) {
11231125
}
11241126

11251127
template <class ELFT>
1126-
void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) {
1128+
void InputSectionBase::relocate(Ctx &ctx, uint8_t *buf, uint8_t *bufEnd) {
11271129
if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack))
1128-
adjustSplitStackFunctionPrologues<ELFT>(buf, bufEnd);
1130+
adjustSplitStackFunctionPrologues<ELFT>(ctx, buf, bufEnd);
11291131

11301132
if (flags & SHF_ALLOC) {
11311133
ctx.target->relocateAlloc(*this, buf);
@@ -1135,13 +1137,13 @@ void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) {
11351137
auto *sec = cast<InputSection>(this);
11361138
// For a relocatable link, also call relocateNonAlloc() to rewrite applicable
11371139
// locations with tombstone values.
1138-
invokeOnRelocs(*sec, sec->relocateNonAlloc<ELFT>, buf);
1140+
invokeOnRelocs(*sec, sec->relocateNonAlloc<ELFT>, ctx, buf);
11391141
}
11401142

11411143
// For each function-defining prologue, find any calls to __morestack,
11421144
// and replace them with calls to __morestack_non_split.
11431145
static void switchMorestackCallsToMorestackNonSplit(
1144-
DenseSet<Defined *> &prologues,
1146+
Ctx &ctx, DenseSet<Defined *> &prologues,
11451147
SmallVector<Relocation *, 0> &morestackCalls) {
11461148

11471149
// If the target adjusted a function's prologue, all calls to
@@ -1189,7 +1191,7 @@ static bool enclosingPrologueAttempted(uint64_t offset,
11891191
// adjusted to ensure that the called function will have enough stack
11901192
// available. Find those functions, and adjust their prologues.
11911193
template <class ELFT>
1192-
void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
1194+
void InputSectionBase::adjustSplitStackFunctionPrologues(Ctx &ctx, uint8_t *buf,
11931195
uint8_t *end) {
11941196
DenseSet<Defined *> prologues;
11951197
SmallVector<Relocation *, 0> morestackCalls;
@@ -1234,20 +1236,20 @@ void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
12341236
}
12351237

12361238
if (ctx.target->needsMoreStackNonSplit)
1237-
switchMorestackCallsToMorestackNonSplit(prologues, morestackCalls);
1239+
switchMorestackCallsToMorestackNonSplit(ctx, prologues, morestackCalls);
12381240
}
12391241

1240-
template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
1242+
template <class ELFT> void InputSection::writeTo(Ctx &ctx, uint8_t *buf) {
12411243
if (LLVM_UNLIKELY(type == SHT_NOBITS))
12421244
return;
12431245
// If -r or --emit-relocs is given, then an InputSection
12441246
// may be a relocation section.
12451247
if (LLVM_UNLIKELY(type == SHT_RELA)) {
1246-
copyRelocations<ELFT, typename ELFT::Rela>(buf);
1248+
copyRelocations<ELFT, typename ELFT::Rela>(ctx, buf);
12471249
return;
12481250
}
12491251
if (LLVM_UNLIKELY(type == SHT_REL)) {
1250-
copyRelocations<ELFT, typename ELFT::Rel>(buf);
1252+
copyRelocations<ELFT, typename ELFT::Rel>(ctx, buf);
12511253
return;
12521254
}
12531255

@@ -1270,14 +1272,14 @@ template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
12701272
fatal(toString(this) +
12711273
": decompress failed: " + llvm::toString(std::move(e)));
12721274
uint8_t *bufEnd = buf + size;
1273-
relocate<ELFT>(buf, bufEnd);
1275+
relocate<ELFT>(ctx, buf, bufEnd);
12741276
return;
12751277
}
12761278

12771279
// Copy section contents from source object file to output file
12781280
// and then apply relocations.
12791281
memcpy(buf, content().data(), content().size());
1280-
relocate<ELFT>(buf, buf + content().size());
1282+
relocate<ELFT>(ctx, buf, buf + content().size());
12811283
}
12821284

12831285
void InputSection::replace(InputSection *other) {
@@ -1415,7 +1417,7 @@ void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> data,
14151417
size_t entSize) {
14161418
size_t size = data.size();
14171419
assert((size % entSize) == 0);
1418-
const bool live = !(flags & SHF_ALLOC) || !ctx.arg.gcSections;
1420+
const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections;
14191421

14201422
pieces.resize_for_overwrite(size / entSize);
14211423
for (size_t i = 0, j = 0; i != size; i += entSize, j++)
@@ -1471,10 +1473,10 @@ template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &,
14711473
template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &,
14721474
StringRef);
14731475

1474-
template void InputSection::writeTo<ELF32LE>(uint8_t *);
1475-
template void InputSection::writeTo<ELF32BE>(uint8_t *);
1476-
template void InputSection::writeTo<ELF64LE>(uint8_t *);
1477-
template void InputSection::writeTo<ELF64BE>(uint8_t *);
1476+
template void InputSection::writeTo<ELF32LE>(Ctx &, uint8_t *);
1477+
template void InputSection::writeTo<ELF32BE>(Ctx &, uint8_t *);
1478+
template void InputSection::writeTo<ELF64LE>(Ctx &, uint8_t *);
1479+
template void InputSection::writeTo<ELF64BE>(Ctx &, uint8_t *);
14781480

14791481
template RelsOrRelas<ELF32LE>
14801482
InputSectionBase::relsOrRelas<ELF32LE>(bool) const;

lld/ELF/InputSection.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class InputSectionBase : public SectionBase {
245245
// Each section knows how to relocate itself. These functions apply
246246
// relocations, assuming that Buf points to this section's copy in
247247
// the mmap'ed output buffer.
248-
template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
248+
template <class ELFT> void relocate(Ctx &, uint8_t *buf, uint8_t *bufEnd);
249249
uint64_t getRelocTargetVA(Ctx &, const Relocation &r, uint64_t p) const;
250250

251251
// The native ELF reloc data type is not very convenient to handle.
@@ -278,8 +278,7 @@ class InputSectionBase : public SectionBase {
278278
// to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
279279
// information.
280280
template <typename ELFT>
281-
void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
282-
281+
void adjustSplitStackFunctionPrologues(Ctx &, uint8_t *buf, uint8_t *end);
283282

284283
template <typename T> llvm::ArrayRef<T> getDataAs() const {
285284
size_t s = content().size();
@@ -410,7 +409,7 @@ class InputSection : public InputSectionBase {
410409

411410
// Write this section to a mmap'ed file, assuming Buf is pointing to
412411
// beginning of the output section.
413-
template <class ELFT> void writeTo(uint8_t *buf);
412+
template <class ELFT> void writeTo(Ctx &, uint8_t *buf);
414413

415414
OutputSection *getParent() const {
416415
return reinterpret_cast<OutputSection *>(parent);
@@ -425,7 +424,7 @@ class InputSection : public InputSectionBase {
425424
InputSectionBase *getRelocatedSection() const;
426425

427426
template <class ELFT, class RelTy>
428-
void relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels);
427+
void relocateNonAlloc(Ctx &, uint8_t *buf, Relocs<RelTy> rels);
429428

430429
// Points to the canonical section. If ICF folds two sections, repl pointer of
431430
// one section points to the other.
@@ -440,10 +439,10 @@ class InputSection : public InputSectionBase {
440439
static InputSection discarded;
441440

442441
private:
443-
template <class ELFT, class RelTy> void copyRelocations(uint8_t *buf);
442+
template <class ELFT, class RelTy> void copyRelocations(Ctx &, uint8_t *buf);
444443

445444
template <class ELFT, class RelTy, class RelIt>
446-
void copyRelocations(uint8_t *buf, llvm::iterator_range<RelIt> rels);
445+
void copyRelocations(Ctx &, uint8_t *buf, llvm::iterator_range<RelIt> rels);
447446

448447
template <class ELFT> void copyShtGroup(uint8_t *buf);
449448
};

lld/ELF/OutputSections.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ void OutputSection::writeTo(Ctx &ctx, uint8_t *buf, parallel::TaskGroup &tg) {
529529
if (auto *s = dyn_cast<SyntheticSection>(isec))
530530
s->writeTo(buf + isec->outSecOff);
531531
else
532-
isec->writeTo<ELFT>(buf + isec->outSecOff);
532+
isec->writeTo<ELFT>(ctx, buf + isec->outSecOff);
533533

534534
// When in Arm BE8 mode, the linker has to convert the big-endian
535535
// instructions to little-endian, leaving the data big-endian.

0 commit comments

Comments
 (0)