Lines Matching defs:Sec

66 template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
68 reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + Sec.HeaderOffset;
70 Shdr.sh_name = Sec.NameIndex;
71 Shdr.sh_type = Sec.Type;
72 Shdr.sh_flags = Sec.Flags;
73 Shdr.sh_addr = Sec.Addr;
74 Shdr.sh_offset = Sec.Offset;
75 Shdr.sh_size = Sec.Size;
76 Shdr.sh_link = Sec.Link;
77 Shdr.sh_info = Sec.Info;
78 Shdr.sh_addralign = Sec.Align;
79 Shdr.sh_entsize = Sec.EntrySize;
100 Error ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) {
101 Sec.EntrySize = sizeof(Elf_Sym);
102 Sec.Size = Sec.Symbols.size() * Sec.EntrySize;
104 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
122 Error ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
123 if (Sec.Type == SHT_CREL) {
124 Sec.Size = encodeCrel<ELFT::Is64Bits>(Sec.Relocations).size();
126 Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela);
127 Sec.Size = Sec.Relocations.size() * Sec.EntrySize;
129 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
139 template <class ELFT> Error ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {
140 Sec.Size = sizeof(Elf_Word) + Sec.GroupMembers.size() * sizeof(Elf_Word);
158 Error BinarySectionWriter::visit(const SectionIndexSection &Sec) {
161 Sec.Name + "' ");
164 Error BinarySectionWriter::visit(const SymbolTableSection &Sec) {
166 "cannot write symbol table '" + Sec.Name +
170 Error BinarySectionWriter::visit(const RelocationSection &Sec) {
172 "cannot write relocation section '" + Sec.Name +
176 Error BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
178 "cannot write '" + Sec.Name + "' out to binary");
181 Error BinarySectionWriter::visit(const GroupSection &Sec) {
183 "cannot write '" + Sec.Name + "' out to binary");
186 Error SectionWriter::visit(const Section &Sec) {
187 if (Sec.Type != SHT_NOBITS)
188 llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset);
343 static uint64_t sectionPhysicalAddr(const SectionBase *Sec) {
344 Segment *Seg = Sec->ParentSegment;
347 return Seg ? Seg->PAddr + Sec->OriginalOffset - Seg->OriginalOffset
348 : Sec->Addr;
351 void IHexSectionWriterBase::writeSection(const SectionBase *Sec,
353 assert(Data.size() == Sec->Size);
355 uint32_t Addr = sectionPhysicalAddr(Sec) & 0xFFFFFFFFU;
400 Error IHexSectionWriterBase::visit(const Section &Sec) {
401 writeSection(&Sec, Sec.Contents);
405 Error IHexSectionWriterBase::visit(const OwnedDataSection &Sec) {
406 writeSection(&Sec, Sec.Data);
410 Error IHexSectionWriterBase::visit(const StringTableSection &Sec) {
412 assert(Sec.Size == Sec.StrTabBuilder.getSize());
416 writeSection(&Sec, {nullptr, static_cast<size_t>(Sec.Size)});
420 Error IHexSectionWriterBase::visit(const DynamicRelocationSection &Sec) {
421 writeSection(&Sec, Sec.Contents);
432 Error IHexSectionWriter::visit(const StringTableSection &Sec) {
433 assert(Sec.Size == Sec.StrTabBuilder.getSize());
434 std::vector<uint8_t> Data(Sec.Size);
435 Sec.StrTabBuilder.write(Data.data());
436 writeSection(&Sec, Data);
455 Error SectionWriter::visit(const OwnedDataSection &Sec) {
456 llvm::copy(Sec.Data, Out.getBufferStart() + Sec.Offset);
461 Error ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
463 Sec.OriginalData.slice(sizeof(Elf_Chdr_Impl<ELFT>));
466 switch (Sec.ChType) {
476 Twine(Sec.ChType) + ") of section '" +
477 Sec.Name + "' is unsupported");
482 "failed to decompress section '" + Sec.Name +
485 static_cast<size_t>(Sec.Size)))
487 "failed to decompress section '" + Sec.Name +
490 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
496 Error BinarySectionWriter::visit(const DecompressedSection &Sec) {
498 "cannot write compressed section '" + Sec.Name +
527 Error BinarySectionWriter::visit(const CompressedSection &Sec) {
529 "cannot write compressed section '" + Sec.Name +
534 Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
535 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
537 switch (Sec.CompressionType) {
539 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
548 Chdr.ch_size = Sec.DecompressedSize;
549 Chdr.ch_addralign = Sec.DecompressedAlign;
553 std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
557 CompressedSection::CompressedSection(const SectionBase &Sec,
560 : SectionBase(Sec), CompressionType(CompressionType),
561 DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
600 Error SectionWriter::visit(const StringTableSection &Sec) {
601 Sec.StrTabBuilder.write(reinterpret_cast<uint8_t *>(Out.getBufferStart()) +
602 Sec.Offset);
615 Error ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
616 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
617 llvm::copy(Sec.Indexes, reinterpret_cast<Elf_Word *>(Buf));
623 Expected<SymbolTableSection *> Sec =
630 if (!Sec)
631 return Sec.takeError();
633 setSymTab(*Sec);
788 Expected<StringTableSection *> Sec =
795 if (!Sec)
796 return Sec.takeError();
798 setStrTab(*Sec);
862 Error ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
863 Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Out.getBufferStart() + Sec.Offset);
865 for (const std::unique_ptr<Symbol> &Symbol : Sec.Symbols) {
930 Expected<SymTabType *> Sec = SecTable.getSectionOfType<SymTabType>(
936 if (!Sec)
937 return Sec.takeError();
939 setSymTab(*Sec);
943 Expected<SectionBase *> Sec =
946 if (!Sec)
947 return Sec.takeError();
949 setSection(*Sec);
984 Error ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
985 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
986 if (Sec.Type == SHT_CREL) {
987 auto Content = encodeCrel<ELFT::Is64Bits>(Sec.Relocations);
989 } else if (Sec.Type == SHT_REL) {
990 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf),
991 Sec.getObject().IsMips64EL);
993 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf),
994 Sec.getObject().IsMips64EL);
1031 Error SectionWriter::visit(const DynamicRelocationSection &Sec) {
1032 llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset);
1121 for (SectionBase *&Sec : GroupMembers)
1122 if (SectionBase *To = FromTo.lookup(Sec))
1123 Sec = To;
1129 for (SectionBase *Sec : GroupMembers)
1130 Sec->Flags &= ~SHF_GROUP;
1137 Expected<SectionBase *> Sec =
1140 if (!Sec)
1141 return Sec.takeError();
1143 LinkSection = *Sec;
1179 Error ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
1181 reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
1183 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
1184 *CRC = Sec.CRC32;
1185 llvm::copy(Sec.FileName, Buf);
1198 Error ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
1200 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
1201 endian::write32<ELFT::Endianness>(Buf++, Sec.FlagWord);
1202 for (SectionBase *S : Sec.GroupMembers)
1216 static bool sectionWithinSegment(const SectionBase &Sec, const Segment &Seg) {
1221 uint64_t SecSize = Sec.Size ? Sec.Size : 1;
1224 if (Sec.OriginalOffset == std::numeric_limits<uint64_t>::max())
1227 if (Sec.Type == SHT_NOBITS) {
1228 if (!(Sec.Flags & SHF_ALLOC))
1231 bool SectionIsTLS = Sec.Flags & SHF_TLS;
1236 return Seg.VAddr <= Sec.Addr &&
1237 Seg.VAddr + Seg.MemSize >= Sec.Addr + SecSize;
1240 return Seg.Offset <= Sec.OriginalOffset &&
1241 Seg.Offset + Seg.FileSize >= Sec.OriginalOffset + SecSize;
1303 for (SectionBase &Sec : Obj->sections())
1304 if (Error Err = Sec.initialize(Obj->sections()))
1435 for (const SectionBase &Sec : Obj.sections()) {
1436 if (Sec.Type == SHT_LLVM_PART_EHDR && Sec.Name == *ExtractPartition) {
1437 EhdrOffset = Sec.Offset;
1476 for (SectionBase &Sec : Obj.sections())
1477 if (sectionWithinSegment(Sec, Seg)) {
1478 Seg.addSection(&Sec);
1479 if (!Sec.ParentSegment || Sec.ParentSegment->Offset > Seg.Offset)
1480 Sec.ParentSegment = &Seg;
1551 Expected<SectionBase *> Sec = SecTable.getSection(
1554 if (!Sec)
1555 return Sec.takeError();
1557 GroupSec->addMember(*Sec);
1612 Expected<SectionBase *> Sec = Obj.sections().getSection(
1615 if (!Sec)
1616 return Sec.takeError();
1618 DefSection = *Sec;
1629 Expected<SectionBase *> Sec = Obj.sections().getSection(
1633 if (!Sec)
1634 return Sec.takeError();
1636 DefSection = *Sec;
1697 if (T *Sec = dyn_cast<T>(*BaseSec))
1698 return Sec;
1796 Expected<SectionBase &> Sec = makeSection(Shdr);
1797 if (!Sec)
1798 return Sec.takeError();
1803 Sec->Name = SecName->str();
1804 Sec->Type = Sec->OriginalType = Shdr.sh_type;
1805 Sec->Flags = Sec->OriginalFlags = Shdr.sh_flags;
1806 Sec->Addr = Shdr.sh_addr;
1807 Sec->Offset = Shdr.sh_offset;
1808 Sec->OriginalOffset = Shdr.sh_offset;
1809 Sec->Size = Shdr.sh_size;
1810 Sec->Link = Shdr.sh_link;
1811 Sec->Info = Shdr.sh_info;
1812 Sec->Align = Shdr.sh_addralign;
1813 Sec->EntrySize = Shdr.sh_entsize;
1814 Sec->Index = Index++;
1815 Sec->OriginalIndex = Sec->Index;
1816 Sec->OriginalData = ArrayRef<uint8_t>(
1827 Expected<const Elf_Shdr *> Sec = ElfFile.getSection(0);
1828 if (!Sec)
1829 return Sec.takeError();
1831 ShstrIndex = (*Sec)->sh_link;
1837 Expected<StringTableSection *> Sec =
1844 if (!Sec)
1845 return Sec.takeError();
1847 Obj.SectionNames = *Sec;
1873 for (SectionBase &Sec : Obj.sections()) {
1874 if (&Sec == Obj.SymbolTable)
1876 if (Error Err = Sec.initialize(Obj.sections()))
1878 if (auto RelSec = dyn_cast<RelocationSection>(&Sec)) {
1911 } else if (auto GroupSec = dyn_cast<GroupSection>(&Sec)) {
2106 for (SectionBase &Sec : Obj.sections())
2107 writeShdr(Sec);
2111 for (SectionBase &Sec : Obj.sections())
2115 if (Sec.ParentSegment == nullptr)
2116 if (Error Err = Sec.accept(*SecWriter))
2130 SectionBase *Sec = it.first;
2133 auto *Parent = Sec->ParentSegment;
2136 Sec->OriginalOffset - Parent->OriginalOffset + Parent->Offset;
2141 for (auto &Sec : Obj.removedSections()) {
2142 Segment *Parent = Sec.ParentSegment;
2143 if (Parent == nullptr || Sec.Type == SHT_NOBITS || Sec.Size == 0)
2146 Sec.OriginalOffset - Parent->OriginalOffset + Parent->Offset;
2147 std::memset(Buf->getBufferStart() + Offset, 0, Sec.Size);
2157 Error Object::updateSectionData(SecPtr &Sec, ArrayRef<uint8_t> Data) {
2158 if (!Sec->hasContents())
2162 Sec->Name.c_str());
2164 if (Data.size() > Sec->Size && Sec->ParentSegment)
2168 Data.size(), Sec->Name.c_str(), Sec->Size);
2170 if (!Sec->ParentSegment) {
2171 Sec = std::make_unique<OwnedDataSection>(*Sec, Data);
2174 Sec->Size = Data.size();
2175 UpdatedSections[Sec.get()] = Data;
2183 [&](const SecPtr &Sec) { return Sec->Name == Name; });
2192 [&](const SecPtr &Sec) { return Sec.get() == &S; });
2201 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
2202 if (ToRemove(*Sec))
2206 if (isa<CompressedSection>(Sec))
2208 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
2213 if (Sec->Type == ELF::SHT_GROUP) {
2214 auto GroupSec = cast<GroupSection>(Sec.get());
2244 AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) {
2245 return RemoveSections.find(Sec) != RemoveSections.end();
2271 for (auto &Sec : Sections)
2272 Sec->replaceSectionReferences(FromTo);
2276 [=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; }))
2284 for (const SecPtr &Sec : Sections)
2285 if (Error E = Sec->removeSymbols(ToRemove))
2295 for (SectionBase &Sec : sections()) {
2296 if (Sec.Type == ELF::SHT_STRTAB && !(Sec.Flags & SHF_ALLOC)) {
2297 StrTab = static_cast<StringTableSection *>(&Sec);
2301 if (SectionNames != &Sec)
2372 for (auto &Sec : Sections) {
2373 Sec.Index = Index++;
2374 if (Sec.ParentSegment != nullptr) {
2375 const Segment &Segment = *Sec.ParentSegment;
2376 Sec.Offset =
2377 Segment.Offset + (Sec.OriginalOffset - Segment.OriginalOffset);
2379 OutOfSegmentSections.push_back(&Sec);
2386 for (auto *Sec : OutOfSegmentSections) {
2387 Offset = alignTo(Offset, Sec->Align == 0 ? 1 : Sec->Align);
2388 Sec->Offset = Offset;
2389 if (Sec->Type != SHT_NOBITS)
2390 Offset += Sec->Size;
2403 for (auto &Sec : Obj.sections()) {
2404 Sec.Index = Index++;
2405 Sections.push_back(&Sec);
2412 for (auto *Sec : Sections) {
2413 auto *FirstSec = Sec->ParentSegment && Sec->ParentSegment->Type == PT_LOAD
2414 ? Sec->ParentSegment->firstSection()
2419 if (FirstSec && FirstSec == Sec)
2420 Off = alignTo(Off, Sec->ParentSegment->Align, Sec->Addr);
2425 if (Sec->Type == SHT_NOBITS) {
2426 Sec->Offset = Off;
2431 // FirstSec being nullptr generally means that Sec does not have the
2433 Off = Sec->Align ? alignTo(Off, Sec->Align) : Off;
2434 } else if (FirstSec != Sec) {
2437 Off = Sec->OriginalOffset - FirstSec->OriginalOffset + FirstSec->Offset;
2439 Sec->Offset = Off;
2440 Off += Sec->Size;
2465 for (const SectionBase *Sec : Seg->Sections) {
2466 uint64_t Size = Sec->Type == SHT_NOBITS ? 0 : Sec->Size;
2467 if (Sec->Offset + Size > Offset)
2468 FileSize = std::max(FileSize, Sec->Offset + Size - Offset);
2571 return Obj.removeSections(false, [&](const SectionBase &Sec) {
2572 return &Sec == Obj.SymbolTable || &Sec == StrTab;
2591 for (SectionBase &Sec : Obj.sections())
2592 Sec.restoreSymTabLink(*Obj.SymbolTable);
2604 [](const SectionBase &Sec) { return Sec.HasSymbol; });
2625 [this](const SectionBase &Sec) {
2626 return &Sec == Obj.SectionIndexTable;
2635 for (const SectionBase &Sec : Obj.sections())
2636 Obj.SectionNames->addString(Sec.Name);
2645 for (SectionBase &Sec : Obj.sections()) {
2646 Sec.Index = Index++;
2647 if (Error Err = Sec.accept(*SecSizer))
2659 for (SectionBase &Sec : Obj.sections())
2660 if (auto StrTab = dyn_cast<StringTableSection>(&Sec))
2673 for (SectionBase &Sec : Obj.sections()) {
2674 Sec.HeaderOffset = Offset;
2677 Sec.NameIndex = Obj.SectionNames->findIndex(Sec.Name);
2678 Sec.finalize();
2694 for (const SectionBase &Sec : Obj.allocSections()) {
2695 if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
2696 SectionsToWrite.push_back(&Sec);
2710 const SectionBase &Sec = *SectionsToWrite[i];
2711 if (Error Err = Sec.accept(*SecWriter))
2719 assert(Sec.Offset + Sec.Size <= PadOffset);
2720 std::fill(Buf->getBufferStart() + Sec.Offset + Sec.Size,
2736 for (SectionBase &Sec : Obj.allocSections()) {
2737 if (Sec.ParentSegment != nullptr)
2738 Sec.Addr =
2739 Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr;
2740 if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
2741 MinAddr = std::min(MinAddr, Sec.Addr);
2749 for (SectionBase &Sec : Obj.allocSections())
2750 if (Sec.Type != SHT_NOBITS && Sec.Size > 0) {
2751 Sec.Offset = Sec.Addr - MinAddr;
2752 TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size);
2842 for (const SectionBase *Sec : Sections)
2843 if (Error Err = Sec->accept(LengthCalc))
2856 for (const SectionBase *Sec : Sections)
2857 if (Error Err = Sec->accept(Writer))
2875 Error SRECSectionWriterBase::visit(const StringTableSection &Sec) {
2877 assert(Sec.Size == Sec.StrTabBuilder.getSize() &&
2884 Error SRECSectionWriterBase::visit(const Section &Sec) {
2885 writeSection(Sec, Sec.Contents);
2889 Error SRECSectionWriterBase::visit(const OwnedDataSection &Sec) {
2890 writeSection(Sec, Sec.Data);
2894 Error SRECSectionWriterBase::visit(const DynamicRelocationSection &Sec) {
2895 writeSection(Sec, Sec.Contents);
2932 Error SRECSectionWriter::visit(const StringTableSection &Sec) {
2933 assert(Sec.Size == Sec.StrTabBuilder.getSize() &&
2935 std::vector<uint8_t> Data(Sec.Size);
2936 Sec.StrTabBuilder.write(Data.data());
2937 writeSection(Sec, Data);
3035 for (const SectionBase *Sec : Sections)
3036 if (Error Err = Sec->accept(SizeCalc))