Lines Matching full:sec

69 template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
71 reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + Sec.HeaderOffset;
73 Shdr.sh_name = Sec.NameIndex;
74 Shdr.sh_type = Sec.Type;
75 Shdr.sh_flags = Sec.Flags;
76 Shdr.sh_addr = Sec.Addr;
77 Shdr.sh_offset = Sec.Offset;
78 Shdr.sh_size = Sec.Size;
79 Shdr.sh_link = Sec.Link;
80 Shdr.sh_info = Sec.Info;
81 Shdr.sh_addralign = Sec.Align;
82 Shdr.sh_entsize = Sec.EntrySize;
103 Error ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) {
104 Sec.EntrySize = sizeof(Elf_Sym);
105 Sec.Size = Sec.Symbols.size() * Sec.EntrySize;
107 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
125 Error ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
126 if (Sec.Type == SHT_CREL) {
127 Sec.Size = encodeCrel<ELFT::Is64Bits>(Sec.Relocations).size();
129 Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela);
130 Sec.Size = Sec.Relocations.size() * Sec.EntrySize;
132 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
142 template <class ELFT> Error ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {
143 Sec.Size = sizeof(Elf_Word) + Sec.GroupMembers.size() * sizeof(Elf_Word);
161 Error BinarySectionWriter::visit(const SectionIndexSection &Sec) {
164 Sec.Name + "' ");
167 Error BinarySectionWriter::visit(const SymbolTableSection &Sec) {
169 "cannot write symbol table '" + Sec.Name +
173 Error BinarySectionWriter::visit(const RelocationSection &Sec) {
175 "cannot write relocation section '" + Sec.Name +
179 Error BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
181 "cannot write '" + Sec.Name + "' out to binary");
184 Error BinarySectionWriter::visit(const GroupSection &Sec) {
186 "cannot write '" + Sec.Name + "' out to binary");
189 Error SectionWriter::visit(const Section &Sec) {
190 if (Sec.Type != SHT_NOBITS)
191 llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset);
346 static uint64_t sectionPhysicalAddr(const SectionBase *Sec) {
347 Segment *Seg = Sec->ParentSegment;
350 return Seg ? Seg->PAddr + Sec->OriginalOffset - Seg->OriginalOffset
351 : Sec->Addr;
354 void IHexSectionWriterBase::writeSection(const SectionBase *Sec,
356 assert(Data.size() == Sec->Size);
358 uint32_t Addr = sectionPhysicalAddr(Sec) & 0xFFFFFFFFU;
403 Error IHexSectionWriterBase::visit(const Section &Sec) {
404 writeSection(&Sec, Sec.Contents);
408 Error IHexSectionWriterBase::visit(const OwnedDataSection &Sec) {
409 writeSection(&Sec, Sec.Data);
413 Error IHexSectionWriterBase::visit(const StringTableSection &Sec) {
415 assert(Sec.Size == Sec.StrTabBuilder.getSize());
419 writeSection(&Sec, {nullptr, static_cast<size_t>(Sec.Size)});
423 Error IHexSectionWriterBase::visit(const DynamicRelocationSection &Sec) {
424 writeSection(&Sec, Sec.Contents);
435 Error IHexSectionWriter::visit(const StringTableSection &Sec) {
436 assert(Sec.Size == Sec.StrTabBuilder.getSize());
437 std::vector<uint8_t> Data(Sec.Size);
438 Sec.StrTabBuilder.write(Data.data());
439 writeSection(&Sec, Data);
458 Error SectionWriter::visit(const OwnedDataSection &Sec) {
459 llvm::copy(Sec.Data, Out.getBufferStart() + Sec.Offset);
464 Error ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
466 Sec.OriginalData.slice(sizeof(Elf_Chdr_Impl<ELFT>));
469 switch (Sec.ChType) {
479 Twine(Sec.ChType) + ") of section '" +
480 Sec.Name + "' is unsupported");
485 "failed to decompress section '" + Sec.Name +
488 static_cast<size_t>(Sec.Size)))
490 "failed to decompress section '" + Sec.Name +
493 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
499 Error BinarySectionWriter::visit(const DecompressedSection &Sec) {
501 "cannot write compressed section '" + Sec.Name +
530 Error BinarySectionWriter::visit(const CompressedSection &Sec) {
532 "cannot write compressed section '" + Sec.Name +
537 Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
538 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
540 switch (Sec.CompressionType) {
542 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
551 Chdr.ch_size = Sec.DecompressedSize;
552 Chdr.ch_addralign = Sec.DecompressedAlign;
556 std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
560 CompressedSection::CompressedSection(const SectionBase &Sec,
563 : SectionBase(Sec), CompressionType(CompressionType),
564 DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
603 Error SectionWriter::visit(const StringTableSection &Sec) {
604 Sec.StrTabBuilder.write(reinterpret_cast<uint8_t *>(Out.getBufferStart()) +
605 Sec.Offset);
618 Error ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
619 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
620 llvm::copy(Sec.Indexes, reinterpret_cast<Elf_Word *>(Buf));
626 Expected<SymbolTableSection *> Sec =
633 if (!Sec)
634 return Sec.takeError();
636 setSymTab(*Sec);
791 Expected<StringTableSection *> Sec =
798 if (!Sec)
799 return Sec.takeError();
801 setStrTab(*Sec);
865 Error ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
866 Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Out.getBufferStart() + Sec.Offset);
868 for (const std::unique_ptr<Symbol> &Symbol : Sec.Symbols) {
933 Expected<SymTabType *> Sec = SecTable.getSectionOfType<SymTabType>(
939 if (!Sec)
940 return Sec.takeError();
942 setSymTab(*Sec);
946 Expected<SectionBase *> Sec =
949 if (!Sec)
950 return Sec.takeError();
952 setSection(*Sec);
987 Error ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
988 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
989 if (Sec.Type == SHT_CREL) {
990 auto Content = encodeCrel<ELFT::Is64Bits>(Sec.Relocations);
992 } else if (Sec.Type == SHT_REL) {
993 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf),
994 Sec.getObject().IsMips64EL);
996 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf),
997 Sec.getObject().IsMips64EL);
1034 Error SectionWriter::visit(const DynamicRelocationSection &Sec) {
1035 llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset);
1124 for (SectionBase *&Sec : GroupMembers)
1125 if (SectionBase *To = FromTo.lookup(Sec))
1126 Sec = To;
1132 for (SectionBase *Sec : GroupMembers)
1133 Sec->Flags &= ~SHF_GROUP;
1140 Expected<SectionBase *> Sec =
1143 if (!Sec)
1144 return Sec.takeError();
1146 LinkSection = *Sec;
1182 Error ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
1184 reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
1186 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
1187 *CRC = Sec.CRC32;
1188 llvm::copy(Sec.FileName, Buf);
1201 Error ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
1203 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
1204 endian::write32<ELFT::Endianness>(Buf++, Sec.FlagWord);
1205 for (SectionBase *S : Sec.GroupMembers)
1219 static bool sectionWithinSegment(const SectionBase &Sec, const Segment &Seg) {
1224 uint64_t SecSize = Sec.Size ? Sec.Size : 1;
1227 if (Sec.OriginalOffset == std::numeric_limits<uint64_t>::max())
1230 if (Sec.Type == SHT_NOBITS) {
1231 if (!(Sec.Flags & SHF_ALLOC))
1234 bool SectionIsTLS = Sec.Flags & SHF_TLS;
1239 return Seg.VAddr <= Sec.Addr &&
1240 Seg.VAddr + Seg.MemSize >= Sec.Addr + SecSize;
1243 return Seg.Offset <= Sec.OriginalOffset &&
1244 Seg.Offset + Seg.FileSize >= Sec.OriginalOffset + SecSize;
1306 for (SectionBase &Sec : Obj->sections())
1307 if (Error Err = Sec.initialize(Obj->sections()))
1371 ".sec" + std::to_string(SecNo), RecAddr,
1438 for (const SectionBase &Sec : Obj.sections()) {
1439 if (Sec.Type == SHT_LLVM_PART_EHDR && Sec.Name == *ExtractPartition) {
1440 EhdrOffset = Sec.Offset;
1479 for (SectionBase &Sec : Obj.sections())
1480 if (sectionWithinSegment(Sec, Seg)) {
1481 Seg.addSection(&Sec);
1482 if (!Sec.ParentSegment || Sec.ParentSegment->Offset > Seg.Offset)
1483 Sec.ParentSegment = &Seg;
1554 Expected<SectionBase *> Sec = SecTable.getSection(
1557 if (!Sec)
1558 return Sec.takeError();
1560 GroupSec->addMember(*Sec);
1615 Expected<SectionBase *> Sec = Obj.sections().getSection(
1618 if (!Sec)
1619 return Sec.takeError();
1621 DefSection = *Sec;
1632 Expected<SectionBase *> Sec = Obj.sections().getSection(
1636 if (!Sec)
1637 return Sec.takeError();
1639 DefSection = *Sec;
1700 if (T *Sec = dyn_cast<T>(*BaseSec))
1701 return Sec;
1799 Expected<SectionBase &> Sec = makeSection(Shdr);
1800 if (!Sec)
1801 return Sec.takeError();
1806 Sec->Name = SecName->str();
1807 Sec->Type = Sec->OriginalType = Shdr.sh_type;
1808 Sec->Flags = Sec->OriginalFlags = Shdr.sh_flags;
1809 Sec->Addr = Shdr.sh_addr;
1810 Sec->Offset = Shdr.sh_offset;
1811 Sec->OriginalOffset = Shdr.sh_offset;
1812 Sec->Size = Shdr.sh_size;
1813 Sec->Link = Shdr.sh_link;
1814 Sec->Info = Shdr.sh_info;
1815 Sec->Align = Shdr.sh_addralign;
1816 Sec->EntrySize = Shdr.sh_entsize;
1817 Sec->Index = Index++;
1818 Sec->OriginalIndex = Sec->Index;
1819 Sec->OriginalData = ArrayRef<uint8_t>(
1830 Expected<const Elf_Shdr *> Sec = ElfFile.getSection(0);
1831 if (!Sec)
1832 return Sec.takeError();
1834 ShstrIndex = (*Sec)->sh_link;
1840 Expected<StringTableSection *> Sec =
1847 if (!Sec)
1848 return Sec.takeError();
1850 Obj.SectionNames = *Sec;
1876 for (SectionBase &Sec : Obj.sections()) {
1877 if (&Sec == Obj.SymbolTable)
1879 if (Error Err = Sec.initialize(Obj.sections()))
1881 if (auto RelSec = dyn_cast<RelocationSection>(&Sec)) {
1914 } else if (auto GroupSec = dyn_cast<GroupSection>(&Sec)) {
2109 for (SectionBase &Sec : Obj.sections())
2110 writeShdr(Sec);
2114 for (SectionBase &Sec : Obj.sections())
2118 if (Sec.ParentSegment == nullptr)
2119 if (Error Err = Sec.accept(*SecWriter))
2133 SectionBase *Sec = it.first;
2136 auto *Parent = Sec->ParentSegment;
2139 Sec->OriginalOffset - Parent->OriginalOffset + Parent->Offset;
2144 for (auto &Sec : Obj.removedSections()) {
2145 Segment *Parent = Sec.ParentSegment;
2146 if (Parent == nullptr || Sec.Type == SHT_NOBITS || Sec.Size == 0)
2149 Sec.OriginalOffset - Parent->OriginalOffset + Parent->Offset;
2150 std::memset(Buf->getBufferStart() + Offset, 0, Sec.Size);
2162 [&](const SecPtr &Sec) { return Sec->Name == Name; });
2195 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
2196 if (ToRemove(*Sec))
2200 if (isa<CompressedSection>(Sec))
2202 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
2207 if (Sec->Type == ELF::SHT_GROUP) {
2208 auto GroupSec = cast<GroupSection>(Sec.get());
2238 AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) {
2239 return RemoveSections.find(Sec) != RemoveSections.end();
2265 for (auto &Sec : Sections)
2266 Sec->replaceSectionReferences(FromTo);
2270 [=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; }))
2278 for (const SecPtr &Sec : Sections)
2279 if (Error E = Sec->removeSymbols(ToRemove))
2289 for (SectionBase &Sec : sections()) {
2290 if (Sec.Type == ELF::SHT_STRTAB && !(Sec.Flags & SHF_ALLOC)) {
2291 StrTab = static_cast<StringTableSection *>(&Sec);
2295 if (SectionNames != &Sec)
2366 for (auto &Sec : Sections) {
2367 Sec.Index = Index++;
2368 if (Sec.ParentSegment != nullptr) {
2369 const Segment &Segment = *Sec.ParentSegment;
2370 Sec.Offset =
2371 Segment.Offset + (Sec.OriginalOffset - Segment.OriginalOffset);
2373 OutOfSegmentSections.push_back(&Sec);
2380 for (auto *Sec : OutOfSegmentSections) {
2381 Offset = alignTo(Offset, Sec->Align == 0 ? 1 : Sec->Align);
2382 Sec->Offset = Offset;
2383 if (Sec->Type != SHT_NOBITS)
2384 Offset += Sec->Size;
2397 for (auto &Sec : Obj.sections()) {
2398 Sec.Index = Index++;
2399 Sections.push_back(&Sec);
2406 for (auto *Sec : Sections) {
2407 auto *FirstSec = Sec->ParentSegment && Sec->ParentSegment->Type == PT_LOAD
2408 ? Sec->ParentSegment->firstSection()
2413 if (FirstSec && FirstSec == Sec)
2414 Off = alignTo(Off, Sec->ParentSegment->Align, Sec->Addr);
2419 if (Sec->Type == SHT_NOBITS) {
2420 Sec->Offset = Off;
2425 // FirstSec being nullptr generally means that Sec does not have the
2427 Off = Sec->Align ? alignTo(Off, Sec->Align) : Off;
2428 } else if (FirstSec != Sec) {
2431 Off = Sec->OriginalOffset - FirstSec->OriginalOffset + FirstSec->Offset;
2433 Sec->Offset = Off;
2434 Off += Sec->Size;
2459 for (const SectionBase *Sec : Seg->Sections) {
2460 uint64_t Size = Sec->Type == SHT_NOBITS ? 0 : Sec->Size;
2461 if (Sec->Offset + Size > Offset)
2462 FileSize = std::max(FileSize, Sec->Offset + Size - Offset);
2565 return Obj.removeSections(false, [&](const SectionBase &Sec) {
2566 return &Sec == Obj.SymbolTable || &Sec == StrTab;
2585 for (SectionBase &Sec : Obj.sections())
2586 Sec.restoreSymTabLink(*Obj.SymbolTable);
2598 [](const SectionBase &Sec) { return Sec.HasSymbol; });
2619 [this](const SectionBase &Sec) {
2620 return &Sec == Obj.SectionIndexTable;
2629 for (const SectionBase &Sec : Obj.sections())
2630 Obj.SectionNames->addString(Sec.Name);
2639 for (SectionBase &Sec : Obj.sections()) {
2640 Sec.Index = Index++;
2641 if (Error Err = Sec.accept(*SecSizer))
2653 for (SectionBase &Sec : Obj.sections())
2654 if (auto StrTab = dyn_cast<StringTableSection>(&Sec))
2667 for (SectionBase &Sec : Obj.sections()) {
2668 Sec.HeaderOffset = Offset;
2671 Sec.NameIndex = Obj.SectionNames->findIndex(Sec.Name);
2672 Sec.finalize();
2688 for (const SectionBase &Sec : Obj.allocSections()) {
2689 if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
2690 SectionsToWrite.push_back(&Sec);
2704 const SectionBase &Sec = *SectionsToWrite[i];
2705 if (Error Err = Sec.accept(*SecWriter))
2713 assert(Sec.Offset + Sec.Size <= PadOffset);
2714 std::fill(Buf->getBufferStart() + Sec.Offset + Sec.Size,
2730 for (SectionBase &Sec : Obj.allocSections()) {
2731 if (Sec.ParentSegment != nullptr)
2732 Sec.Addr =
2733 Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr;
2734 if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
2735 MinAddr = std::min(MinAddr, Sec.Addr);
2743 for (SectionBase &Sec : Obj.allocSections())
2744 if (Sec.Type != SHT_NOBITS && Sec.Size > 0) {
2745 Sec.Offset = Sec.Addr - MinAddr;
2746 TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size);
2836 for (const SectionBase *Sec : Sections)
2837 if (Error Err = Sec->accept(LengthCalc))
2850 for (const SectionBase *Sec : Sections)
2851 if (Error Err = Sec->accept(Writer))
2869 Error SRECSectionWriterBase::visit(const StringTableSection &Sec) {
2871 assert(Sec.Size == Sec.StrTabBuilder.getSize() &&
2878 Error SRECSectionWriterBase::visit(const Section &Sec) {
2879 writeSection(Sec, Sec.Contents);
2883 Error SRECSectionWriterBase::visit(const OwnedDataSection &Sec) {
2884 writeSection(Sec, Sec.Data);
2888 Error SRECSectionWriterBase::visit(const DynamicRelocationSection &Sec) {
2889 writeSection(Sec, Sec.Contents);
2926 Error SRECSectionWriter::visit(const StringTableSection &Sec) {
2927 assert(Sec.Size == Sec.StrTabBuilder.getSize() &&
2929 std::vector<uint8_t> Data(Sec.Size);
2930 Sec.StrTabBuilder.write(Data.data());
2931 writeSection(Sec, Data);
3029 for (const SectionBase *Sec : Sections)
3030 if (Error Err = Sec->accept(SizeCalc))