Lines Matching +full:slice +full:- +full:per +full:- +full:line

1 //===- InputSection.cpp ---------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
41 return (toString(sec->file) + ":(" + sec->name + ")").str();
71 this->addralign = v;
81 // --force-group-allocation, the SHF_GROUP flag and section groups are retained.
84 if (config->resolveGroups)
106 return s->getSize();
107 return size - bytesDropped;
115 .slice(sizeof(typename ELFT::Chdr));
116 if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
142 typename ELFT::Shdr shdr = f->template getELFShdrs<ELFT>()[relSecIdx];
147 (const uint8_t *)f->mb.getBufferStart() + shdr.sh_offset);
150 InputSectionBase *const &relSec = f->getSections()[relSecIdx];
152 // called for the first time, relSec is null (without --emit-relocs) or an
154 if (!relSec || !cast<InputSection>(relSec)->decodedCrel) {
156 f->cacheDecodedCrel(relSecIdx, sec);
157 sec->type = SHT_RELA;
158 sec->decodedCrel = true;
160 RelocsCrel<ELFT::Is64Bits> entries(sec->content_);
161 sec->size = entries.size() * sizeof(typename ELFT::Rela);
163 sec->content_ = reinterpret_cast<uint8_t *>(relas);
171 reinterpret_cast<const typename ELFT::Rela *>(relSec->content_),
172 relSec->size / sizeof(typename ELFT::Rela))};
176 const void *content = f->mb.getBufferStart() + shdr.sh_offset;
194 // For output sections we treat offset -1 as the end of the section.
195 return offset == uint64_t(-1) ? os->size : offset;
200 return cast<InputSection>(this)->outSecOff + offset;
209 if (!es->content().empty())
210 if (InputSection *isec = es->getParent())
211 return isec->outSecOff + es->getParentOffset(offset);
216 if (InputSection *isec = ms->getParent())
217 return isec->outSecOff + ms->getParentOffset(offset);
218 return ms->getParentOffset(offset);
225 return (out ? out->addr : 0) + getOffset(offset);
233 sec = ms->getParent();
235 sec = eh->getParent();
238 return sec ? sec->getParent() : nullptr;
242 // by zlib-compressed data. This function parses a header to initialize
247 // New-style header
254 if (hdr->ch_type == ELFCOMPRESS_ZLIB) {
258 } else if (hdr->ch_type == ELFCOMPRESS_ZSTD) {
264 Twine(hdr->ch_type) + ")");
270 size = hdr->ch_size;
271 addralign = std::max<uint32_t>(hdr->ch_addralign, 1);
278 return cast<InputSection>(file->getSections()[link]);
284 if (file->isInternal())
286 for (Symbol *b : file->getSymbols())
288 if (d->section == this && d->value <= offset &&
289 offset < d->value + d->size && (type == 0 || type == d->type))
301 return (config->outputFile + ":(" + secAndOffset).str();
315 // Returns an empty string if there's no way to get line info.
318 return file->getSrcMsg(sym, *this, offset);
331 std::string filename = std::string(file->getName());
334 if (!file->archiveName.empty())
335 archive = (" in archive " + file->archiveName).str();
370 // Copy SHT_GROUP section contents. Used only for the -r option.
372 // ELFT::Word is the 32-bit integral type in the target endianness.
383 ArrayRef<InputSectionBase *> sections = file->getSections();
385 for (uint32_t idx : from.slice(1)) {
386 OutputSection *osec = sections[idx]->getOutputSection();
387 if (osec && seen.insert(osec->sectionIndex).second)
388 *to++ = osec->sectionIndex;
393 if (file->isInternal() || !isStaticRelSecType(type))
395 ArrayRef<InputSectionBase *> sections = file->getSections();
401 if (config->relax && !config->relocatable &&
402 (config->emachine == EM_RISCV || config->emachine == EM_LOONGARCH)) {
403 // On LoongArch and RISC-V, relaxation might change relocations: copy
406 copyRelocations<ELFT, RelTy>(buf, llvm::make_range(sec->relocations.begin(),
407 sec->relocations.end()));
415 return Relocation{R_NONE, rel.getType(config->isMips64EL), rel.r_offset,
431 // This is used for -r and --emit-relocs. We can't use memcpy to copy
439 (void)sec->contentMaybeDecompress(); // uncompress if needed
450 p->r_addend = rel.addend;
452 // Output section VA is zero for -r, so r_offset is an offset within the
453 // section, but for --emit-relocs it is a virtual address.
454 p->r_offset = sec->getVA(rel.offset);
455 p->setSymbolAndType(in.symTab->getSymbolIndex(sym), type,
456 config->isMips64EL);
459 // We combine multiple section symbols into only one per
473 if (!isDebugSection(*sec) && sec->name != ".eh_frame" &&
474 sec->name != ".gcc_except_table" && sec->name != ".got2" &&
475 sec->name != ".toc") {
477 Elf_Shdr_Impl<ELFT> sec = file->template getELFShdrs<ELFT>()[secIdx];
479 CHECK(file->getObj().getSectionName(sec), file) +
480 "\n>>> referenced by " + getObjMsg(p->r_offset));
482 p->setSymbolAndType(0, 0, false);
485 SectionBase *section = d->section;
486 assert(section->isLive());
489 const uint8_t *bufLoc = sec->content().begin() + rel.offset;
493 if (config->emachine == EM_MIPS &&
506 addend += sec->getFile<ELFT>()->mipsGp0;
510 p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr;
512 // sec->relocations so that relocateAlloc transitively called by
513 // writeSections will update the implicit addend. Non-SHF_ALLOC sections
515 // this sec->relocations change.
516 else if (config->relocatable && (sec->flags & SHF_ALLOC) &&
518 sec->addReloc({R_ABS, type, rel.offset, addend, &sym});
519 } else if (config->emachine == EM_PPC && type == R_PPC_PLTREL24 &&
520 p->r_addend >= 0x8000 && sec->file->ppc32Got2) {
525 // ppc32Got->outSecOff.
526 p->r_addend += sec->file->ppc32Got2->outSecOff;
531 // The ARM and AArch64 ABI handle pc-relative relocations to undefined weak
555 // Unresolved non branch pc-relative relocations
557 // targets a weak-reference.
570 llvm_unreachable("ARM pc-relative relocation expected\n");
583 // Unresolved non branch pc-relative relocations
592 llvm_unreachable("AArch64 pc-relative relocation expected\n");
610 // ARM SBREL relocations are of the form S + A - B where B is the static base
619 if (!os || !os->ptLoad || !os->ptLoad->firstSec)
621 return os->ptLoad->firstSec->addr;
632 if (!d->section) {
634 sym->getName());
637 InputSection *isec = cast<InputSection>(d->section);
640 warn("non-zero addend in R_RISCV_PCREL_LO12 relocation to " +
641 isec->getObjMsg(d->value) + " is ignored");
646 r.offset = d->value;
648 std::equal_range(isec->relocs().begin(), isec->relocs().end(), r,
654 if (it->type == R_RISCV_PCREL_HI20 || it->type == R_RISCV_GOT_HI20 ||
655 it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20)
659 isec->getObjMsg(d->value) +
665 // target-specific adjustment to produce a thread-pointer-relative offset.
680 // before TP. The alignment padding is added so that (TP - padding -
683 switch (config->emachine) {
687 return s.getVA(0) + config->wordsize * 2 +
688 ((tls->p_vaddr - config->wordsize * 2) & (tls->p_align - 1));
693 // to allow a signed 16-bit offset to reach 0x1000 of TCB/thread-library
695 return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000;
703 return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1));
711 return s.getVA(0) - tls->p_memsz -
712 ((-tls->p_vaddr - tls->p_memsz) & (tls->p_align - 1));
714 llvm_unreachable("unhandled Config->EMachine");
735 return sym.getVA(a) - getARMStaticBase(sym);
745 return in.got->getGlobalDynAddr(sym) + a;
748 return in.got->getVA() + a - p;
750 return in.gotPlt->getVA() + a - p;
753 return sym.getVA(a) - in.got->getVA();
755 return sym.getVA(a) - in.gotPlt->getVA();
758 return sym.getGotVA() + a - in.gotPlt->getVA();
765 return getAArch64Page(sym.getGotVA() + a) - getAArch64Page(p);
767 return sym.getGotVA() + a - getAArch64Page(in.got->getVA());
770 return sym.getGotVA() + a - p;
772 return sym.getGotPltVA() + a - in.got->getVA();
774 return sym.getGotPltVA() + a - p;
777 return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type);
780 return sym.getVA(a) - in.mipsGot->getGp(file);
782 return in.mipsGot->getGp(file) + a;
786 // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at
787 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
789 // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()
790 // to correctly handle less-significant bit of the microMIPS symbol.
791 uint64_t v = in.mipsGot->getGp(file) + a - p;
795 v -= 1;
800 // should be initialized by 'page address'. This address is high 16-bits
802 return in.mipsGot->getVA() + in.mipsGot->getPageEntryOffset(file, sym, a) -
803 in.mipsGot->getGp(file);
806 // In case of MIPS if a GOT relocation has non-zero addend this addend
809 return in.mipsGot->getVA() + in.mipsGot->getSymEntryOffset(file, sym, a) -
810 in.mipsGot->getGp(file);
812 return in.mipsGot->getVA() + in.mipsGot->getGlobalDynOffset(file, sym) -
813 in.mipsGot->getGp(file);
815 return in.mipsGot->getVA() + in.mipsGot->getTlsIndexOffset(file) -
816 in.mipsGot->getGp(file);
819 return getAArch64Page(val) - getAArch64Page(p);
823 return getRelocTargetVA(file, hiRel->type, hiRel->addend, sym.getVA(),
824 *hiRel->sym, hiRel->expr);
837 // instruction, otherwise the place. On RISC-V, resolve an undefined weak
842 if (config->emachine == EM_ARM)
844 else if (config->emachine == EM_AARCH64)
846 else if (config->emachine == EM_PPC)
848 else if (config->emachine == EM_RISCV)
855 return dest - p;
861 return sym.getPltVA() + a - p;
865 return sym.getPltVA() + a - in.gotPlt->getVA();
867 return sym.getPltVA() + a - in.got->getVA();
872 return sym.getPltVA() - p;
887 return symVA - p + getPPC64GlobalEntryToLocalEntryOffset(sym.stOther);
893 return sym.getVA(a) - p;
899 // --noinhibit-exec, even a non-weak undefined reference may reach here.
909 return -getTlsTpOffset(sym) + a;
913 return in.got->getTlsDescAddr(sym) + a;
915 return in.got->getTlsDescAddr(sym) + a - p;
917 return in.got->getTlsDescAddr(sym) + a - in.gotPlt->getVA();
919 return getAArch64Page(in.got->getTlsDescAddr(sym) + a) - getAArch64Page(p);
921 return getLoongArchPageDelta(in.got->getTlsDescAddr(sym) + a, p, type);
923 return in.got->getGlobalDynOffset(sym) + a;
925 return in.got->getGlobalDynAddr(sym) + a - in.gotPlt->getVA();
927 return in.got->getGlobalDynAddr(sym) + a - p;
929 return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type);
931 return in.got->getVA() + in.got->getTlsIndexOff() + a - in.gotPlt->getVA();
933 return in.got->getTlsIndexOff() + a;
935 return in.got->getTlsIndexVA() + a - p;
943 // an example. Relocations in non-alloc sections are much easier to
946 // So, we handle relocations for non-alloc sections directly in this
952 const auto emachine = config->emachine;
964 for (const auto &patAndValue : llvm::reverse(config->deadRelocInNonAlloc))
965 if (patAndValue.first.match(this->name)) {
970 const InputFile *f = this->file;
973 const RelType type = rel.getType(config->isMips64EL);
980 Symbol &sym = f->getRelocTargetSym(rel);
988 it->getType(/*isMips64EL=*/false) == R_RISCV_SUB_ULEB128 &&
989 it->r_offset == offset) {
994 val = sym.getVA(addend) -
995 (f->getRelocTargetSym(*it).getVA(0) + getAddend<ELFT>(*it));
1015 // To address the problems, we use -1 as a tombstone value for most
1017 // to resolve an address attribute (which may have a non-zero addend) to
1018 // -1+addend (wrap around to a low address).
1021 // vector. The computed value is st_value plus a non-negative offset.
1022 // Negative values are invalid, so -1 can be used as the tombstone value.
1025 // --gc-sections, COMDAT, etc), it has been converted to a Undefined.
1026 // `ds->folded` catches the ICF folded case. However, resolving a
1027 // relocation in .debug_line to -1 would stop debugger users from setting
1028 // breakpoints on the folded-in function, so exclude .debug_line.
1030 // For pre-DWARF-v5 .debug_loc and .debug_ranges, -1 is a reserved value
1034 // TODO To reduce disruption, we use 0 instead of -1 as the tombstone
1035 // value. Enable -1 in a future release.
1036 if (!ds || (ds->folded && !isDebugLine)) {
1037 // If -z dead-reloc-in-nonalloc= is specified, respect it.
1039 // For a 32-bit local TU reference in .debug_names, X86_64::relocate
1041 // 32-bit. Other 64-bit targets's don't discern signed/unsigned 32-bit
1054 if (config->relocatable && (RelTy::HasAddend || sym.type != STT_SECTION))
1057 // R_ABS/R_DTPREL and some other relocations can be used from non-SHF_ALLOC
1071 std::string msg = getLocation(offset) + ": has non-ABS relocation " +
1079 // If the control reaches here, we found a PC-relative relocation in a
1080 // non-ALLOC section. Since non-ALLOC section is not loaded into memory
1081 // at runtime, the notion of PC-relative doesn't make sense here. So,
1084 // address 0. For bug-compatibility, we accept them with warnings. We
1090 // keep this bug-compatible code for a while.
1094 SignExtend64<bits>(sym.getVA(addend - offset - outSecOff)));
1100 if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack))
1104 target->relocateAlloc(*this, buf);
1111 invokeOnRelocs(*sec, sec->relocateNonAlloc<ELFT>, buf);
1114 // For each function-defining prologue, find any calls to __morestack,
1125 error("mixing split-stack objects requires a definition of "
1132 return l->offset < r->offset;
1136 return l->value < r->value;
1142 while (it != morestackCalls.end() && (*it)->offset < f->value)
1145 while (it != morestackCalls.end() && (*it)->offset < f->value + f->size) {
1146 (*it)->sym = moreStackNonSplit;
1155 if (f->value <= offset && offset < f->value + f->size)
1171 // Ignore calls into the split-stack api.
1172 if (rel.sym->getName().starts_with("__morestack")) {
1173 if (rel.sym->getName() == "__morestack")
1178 // A relocation to non-function isn't relevant. Sometimes
1181 if (rel.sym->type != STT_FUNC)
1184 // If the callee's-file was compiled with split stack, nothing to do. In
1190 if (InputSection *isec = cast_or_null<InputSection>(d->section))
1191 if (!isec || !isec->getFile<ELFT>() || isec->getFile<ELFT>()->splitStack)
1199 if (target->adjustPrologueForCrossSplitStack(buf + f->value, end,
1200 f->stOther))
1202 if (!getFile<ELFT>()->someNoSplitStack)
1203 error(lld::toString(this) + ": " + f->getName() +
1204 " (with -fsplit-stack) calls " + rel.sym->getName() +
1205 " (without -fsplit-stack), but couldn't adjust its prologue");
1209 if (target->needsMoreStackNonSplit)
1216 // If -r or --emit-relocs is given, then an InputSection
1227 // If -r is given, we may have a SHT_GROUP section.
1238 .slice(sizeof(typename ELFT::Chdr));
1239 size_t size = this->size;
1240 if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
1257 addralign = std::max(addralign, other->addralign);
1263 if (partition != other->partition) {
1266 isec->partition = 1;
1269 other->repl = repl;
1270 other->markDead();
1324 const uint64_t off = d.data() - content().data();
1327 unsigned firstRel = -1;
1331 d = d.slice(size);
1335 getObjMsg(d.data() - content().data()));
1342 if (it == fdes.begin() || it[-1].inputOff + it[-1].size <= offset) {
1348 if (it[-1].outputOff == -1) // invalid piece
1349 return offset - it[-1].inputOff;
1350 return it[-1].outputOff + (offset - it[-1].inputOff);
1363 // null-terminated strings.
1365 const bool live = !(flags & SHF_ALLOC) || !config->gcSections;
1367 if (!std::all_of(end - entSize, end, [](char c) { return c == 0; }))
1373 pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live);
1378 size_t size = findNull(StringRef(p, end - p), entSize);
1379 pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live);
1385 // Split non-SHF_STRINGS section. Such section is a sequence of
1391 const bool live = !(flags & SHF_ALLOC) || !config->gcSections;
1395 pieces[j] = {i, (uint32_t)xxh3_64bits(data.slice(i, entSize)), live};
1415 // thread-safe (i.e. no memory allocation from the pools).
1429 pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1];
1435 return piece.outputOff + (offset - piece.inputOff);