@@ -400,18 +400,20 @@ InputSectionBase *InputSection::getRelocatedSection() const {
400400}
401401
402402template <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.
437439template <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.
975976template <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
11251127template <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.
11431145static 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.
11911193template <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
12831285void 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 &,
14711473template 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
14791481template RelsOrRelas<ELF32LE>
14801482InputSectionBase::relsOrRelas<ELF32LE>(bool ) const ;
0 commit comments