1 //===- Object.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
11 #define LLVM_TOOLS_OBJCOPY_OBJECT_H
14 #include "CopyConfig.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/MC/StringTableBuilder.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Support/FileOutputBuffer.h"
22 #include "llvm/Support/JamCRC.h"
31 enum class DebugCompressionType;
37 class OwnedDataSection;
38 class StringTableSection;
39 class SymbolTableSection;
40 class RelocationSection;
41 class DynamicRelocationSection;
42 class GnuDebugLinkSection;
44 class SectionIndexSection;
45 class CompressedSection;
46 class DecompressedSection;
51 class SectionTableRef {
52 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
55 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
57 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
59 SectionTableRef(const SectionTableRef &) = default;
61 iterator begin() { return iterator(Sections.data()); }
62 iterator end() { return iterator(Sections.data() + Sections.size()); }
64 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
67 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
70 enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
72 class SectionVisitor {
74 virtual ~SectionVisitor() = default;
76 virtual void visit(const Section &Sec) = 0;
77 virtual void visit(const OwnedDataSection &Sec) = 0;
78 virtual void visit(const StringTableSection &Sec) = 0;
79 virtual void visit(const SymbolTableSection &Sec) = 0;
80 virtual void visit(const RelocationSection &Sec) = 0;
81 virtual void visit(const DynamicRelocationSection &Sec) = 0;
82 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
83 virtual void visit(const GroupSection &Sec) = 0;
84 virtual void visit(const SectionIndexSection &Sec) = 0;
85 virtual void visit(const CompressedSection &Sec) = 0;
86 virtual void visit(const DecompressedSection &Sec) = 0;
89 class MutableSectionVisitor {
91 virtual ~MutableSectionVisitor() = default;
93 virtual void visit(Section &Sec) = 0;
94 virtual void visit(OwnedDataSection &Sec) = 0;
95 virtual void visit(StringTableSection &Sec) = 0;
96 virtual void visit(SymbolTableSection &Sec) = 0;
97 virtual void visit(RelocationSection &Sec) = 0;
98 virtual void visit(DynamicRelocationSection &Sec) = 0;
99 virtual void visit(GnuDebugLinkSection &Sec) = 0;
100 virtual void visit(GroupSection &Sec) = 0;
101 virtual void visit(SectionIndexSection &Sec) = 0;
102 virtual void visit(CompressedSection &Sec) = 0;
103 virtual void visit(DecompressedSection &Sec) = 0;
106 class SectionWriter : public SectionVisitor {
111 virtual ~SectionWriter(){};
113 void visit(const Section &Sec) override;
114 void visit(const OwnedDataSection &Sec) override;
115 void visit(const StringTableSection &Sec) override;
116 void visit(const DynamicRelocationSection &Sec) override;
117 virtual void visit(const SymbolTableSection &Sec) override = 0;
118 virtual void visit(const RelocationSection &Sec) override = 0;
119 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
120 virtual void visit(const GroupSection &Sec) override = 0;
121 virtual void visit(const SectionIndexSection &Sec) override = 0;
122 virtual void visit(const CompressedSection &Sec) override = 0;
123 virtual void visit(const DecompressedSection &Sec) override = 0;
125 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
128 template <class ELFT> class ELFSectionWriter : public SectionWriter {
130 using Elf_Word = typename ELFT::Word;
131 using Elf_Rel = typename ELFT::Rel;
132 using Elf_Rela = typename ELFT::Rela;
133 using Elf_Sym = typename ELFT::Sym;
136 virtual ~ELFSectionWriter() {}
137 void visit(const SymbolTableSection &Sec) override;
138 void visit(const RelocationSection &Sec) override;
139 void visit(const GnuDebugLinkSection &Sec) override;
140 void visit(const GroupSection &Sec) override;
141 void visit(const SectionIndexSection &Sec) override;
142 void visit(const CompressedSection &Sec) override;
143 void visit(const DecompressedSection &Sec) override;
145 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
148 template <class ELFT> class ELFSectionSizer : public MutableSectionVisitor {
150 using Elf_Rel = typename ELFT::Rel;
151 using Elf_Rela = typename ELFT::Rela;
152 using Elf_Sym = typename ELFT::Sym;
153 using Elf_Word = typename ELFT::Word;
154 using Elf_Xword = typename ELFT::Xword;
157 void visit(Section &Sec) override;
158 void visit(OwnedDataSection &Sec) override;
159 void visit(StringTableSection &Sec) override;
160 void visit(DynamicRelocationSection &Sec) override;
161 void visit(SymbolTableSection &Sec) override;
162 void visit(RelocationSection &Sec) override;
163 void visit(GnuDebugLinkSection &Sec) override;
164 void visit(GroupSection &Sec) override;
165 void visit(SectionIndexSection &Sec) override;
166 void visit(CompressedSection &Sec) override;
167 void visit(DecompressedSection &Sec) override;
170 #define MAKE_SEC_WRITER_FRIEND \
171 friend class SectionWriter; \
172 template <class ELFT> friend class ELFSectionWriter; \
173 template <class ELFT> friend class ELFSectionSizer;
175 class BinarySectionWriter : public SectionWriter {
177 virtual ~BinarySectionWriter() {}
179 void visit(const SymbolTableSection &Sec) override;
180 void visit(const RelocationSection &Sec) override;
181 void visit(const GnuDebugLinkSection &Sec) override;
182 void visit(const GroupSection &Sec) override;
183 void visit(const SectionIndexSection &Sec) override;
184 void visit(const CompressedSection &Sec) override;
185 void visit(const DecompressedSection &Sec) override;
187 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
197 virtual void finalize() = 0;
198 virtual void write() = 0;
200 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
203 template <class ELFT> class ELFWriter : public Writer {
205 using Elf_Addr = typename ELFT::Addr;
206 using Elf_Shdr = typename ELFT::Shdr;
207 using Elf_Phdr = typename ELFT::Phdr;
208 using Elf_Ehdr = typename ELFT::Ehdr;
210 void initEhdrSegment();
213 void writePhdr(const Segment &Seg);
214 void writeShdr(const SectionBase &Sec);
218 void writeSectionData();
220 void assignOffsets();
222 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
224 size_t totalSize() const;
227 virtual ~ELFWriter() {}
228 bool WriteSectionHeaders = true;
230 void finalize() override;
231 void write() override;
232 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
233 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
236 class BinaryWriter : public Writer {
238 std::unique_ptr<BinarySectionWriter> SecWriter;
244 void finalize() override;
245 void write() override;
246 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
252 Segment *ParentSegment = nullptr;
253 uint64_t HeaderOffset;
254 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
256 bool HasSymbol = false;
260 uint32_t EntrySize = 0;
263 uint64_t Link = ELF::SHN_UNDEF;
264 uint64_t NameIndex = 0;
267 uint64_t Type = ELF::SHT_NULL;
268 ArrayRef<uint8_t> OriginalData;
270 SectionBase() = default;
271 SectionBase(const SectionBase &) = default;
273 virtual ~SectionBase() = default;
275 virtual void initialize(SectionTableRef SecTable);
276 virtual void finalize();
277 virtual void removeSectionReferences(const SectionBase *Sec);
278 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
279 virtual void accept(SectionVisitor &Visitor) const = 0;
280 virtual void accept(MutableSectionVisitor &Visitor) = 0;
281 virtual void markSymbols();
286 struct SectionCompare {
287 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
288 // Some sections might have the same address if one of them is empty. To
289 // fix this we can use the lexicographic ordering on ->Addr and the
290 // address of the actully stored section.
291 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
293 return Lhs->OriginalOffset < Rhs->OriginalOffset;
297 std::set<const SectionBase *, SectionCompare> Sections;
310 uint64_t OriginalOffset;
311 Segment *ParentSegment = nullptr;
312 ArrayRef<uint8_t> Contents;
314 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
317 const SectionBase *firstSection() const {
318 if (!Sections.empty())
319 return *Sections.begin();
323 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
324 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
327 class Section : public SectionBase {
328 MAKE_SEC_WRITER_FRIEND
330 ArrayRef<uint8_t> Contents;
331 SectionBase *LinkSection = nullptr;
334 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
336 void accept(SectionVisitor &Visitor) const override;
337 void accept(MutableSectionVisitor &Visitor) override;
338 void removeSectionReferences(const SectionBase *Sec) override;
339 void initialize(SectionTableRef SecTable) override;
340 void finalize() override;
343 class OwnedDataSection : public SectionBase {
344 MAKE_SEC_WRITER_FRIEND
346 std::vector<uint8_t> Data;
349 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
350 : Data(std::begin(Data), std::end(Data)) {
351 Name = SecName.str();
352 Type = ELF::SHT_PROGBITS;
354 OriginalOffset = std::numeric_limits<uint64_t>::max();
357 void accept(SectionVisitor &Sec) const override;
358 void accept(MutableSectionVisitor &Visitor) override;
361 class CompressedSection : public SectionBase {
362 MAKE_SEC_WRITER_FRIEND
364 DebugCompressionType CompressionType;
365 uint64_t DecompressedSize;
366 uint64_t DecompressedAlign;
367 SmallVector<char, 128> CompressedData;
370 CompressedSection(const SectionBase &Sec,
371 DebugCompressionType CompressionType);
372 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
373 uint64_t DecompressedAlign);
375 uint64_t getDecompressedSize() const { return DecompressedSize; }
376 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
378 void accept(SectionVisitor &Visitor) const override;
379 void accept(MutableSectionVisitor &Visitor) override;
381 static bool classof(const SectionBase *S) {
382 return (S->Flags & ELF::SHF_COMPRESSED) ||
383 (StringRef(S->Name).startswith(".zdebug"));
387 class DecompressedSection : public SectionBase {
388 MAKE_SEC_WRITER_FRIEND
391 explicit DecompressedSection(const CompressedSection &Sec)
393 Size = Sec.getDecompressedSize();
394 Align = Sec.getDecompressedAlign();
395 Flags = (Flags & ~ELF::SHF_COMPRESSED);
396 if (StringRef(Name).startswith(".zdebug"))
397 Name = "." + Name.substr(2);
400 void accept(SectionVisitor &Visitor) const override;
401 void accept(MutableSectionVisitor &Visitor) override;
404 // There are two types of string tables that can exist, dynamic and not dynamic.
405 // In the dynamic case the string table is allocated. Changing a dynamic string
406 // table would mean altering virtual addresses and thus the memory image. So
407 // dynamic string tables should not have an interface to modify them or
408 // reconstruct them. This type lets us reconstruct a string table. To avoid
409 // this class being used for dynamic string tables (which has happened) the
410 // classof method checks that the particular instance is not allocated. This
411 // then agrees with the makeSection method used to construct most sections.
412 class StringTableSection : public SectionBase {
413 MAKE_SEC_WRITER_FRIEND
415 StringTableBuilder StrTabBuilder;
418 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
419 Type = ELF::SHT_STRTAB;
422 void addString(StringRef Name);
423 uint32_t findIndex(StringRef Name) const;
424 void finalize() override;
425 void accept(SectionVisitor &Visitor) const override;
426 void accept(MutableSectionVisitor &Visitor) override;
428 static bool classof(const SectionBase *S) {
429 if (S->Flags & ELF::SHF_ALLOC)
431 return S->Type == ELF::SHT_STRTAB;
435 // Symbols have a st_shndx field that normally stores an index but occasionally
436 // stores a different special value. This enum keeps track of what the st_shndx
437 // field means. Most of the values are just copies of the special SHN_* values.
438 // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
439 enum SymbolShndxType {
440 SYMBOL_SIMPLE_INDEX = 0,
441 SYMBOL_ABS = ELF::SHN_ABS,
442 SYMBOL_COMMON = ELF::SHN_COMMON,
443 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
444 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
445 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
446 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
447 SYMBOL_XINDEX = ELF::SHN_XINDEX,
452 SectionBase *DefinedIn = nullptr;
453 SymbolShndxType ShndxType;
461 bool Referenced = false;
463 uint16_t getShndx() const;
464 bool isCommon() const;
467 class SectionIndexSection : public SectionBase {
468 MAKE_SEC_WRITER_FRIEND
471 std::vector<uint32_t> Indexes;
472 SymbolTableSection *Symbols = nullptr;
475 virtual ~SectionIndexSection() {}
476 void addIndex(uint32_t Index) {
477 Indexes.push_back(Index);
480 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
481 void initialize(SectionTableRef SecTable) override;
482 void finalize() override;
483 void accept(SectionVisitor &Visitor) const override;
484 void accept(MutableSectionVisitor &Visitor) override;
486 SectionIndexSection() {
487 Name = ".symtab_shndx";
490 Type = ELF::SHT_SYMTAB_SHNDX;
494 class SymbolTableSection : public SectionBase {
495 MAKE_SEC_WRITER_FRIEND
497 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
498 void assignIndices();
501 std::vector<std::unique_ptr<Symbol>> Symbols;
502 StringTableSection *SymbolNames = nullptr;
503 SectionIndexSection *SectionIndexTable = nullptr;
505 using SymPtr = std::unique_ptr<Symbol>;
508 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
510 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
511 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
513 void prepareForLayout();
514 // An 'empty' symbol table still contains a null symbol.
515 bool empty() const { return Symbols.size() == 1; }
516 void setShndxTable(SectionIndexSection *ShndxTable) {
517 SectionIndexTable = ShndxTable;
519 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
520 const SectionBase *getStrTab() const { return SymbolNames; }
521 const Symbol *getSymbolByIndex(uint32_t Index) const;
522 Symbol *getSymbolByIndex(uint32_t Index);
523 void updateSymbols(function_ref<void(Symbol &)> Callable);
525 void removeSectionReferences(const SectionBase *Sec) override;
526 void initialize(SectionTableRef SecTable) override;
527 void finalize() override;
528 void accept(SectionVisitor &Visitor) const override;
529 void accept(MutableSectionVisitor &Visitor) override;
530 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
532 static bool classof(const SectionBase *S) {
533 return S->Type == ELF::SHT_SYMTAB;
538 Symbol *RelocSymbol = nullptr;
544 // All relocation sections denote relocations to apply to another section.
545 // However, some relocation sections use a dynamic symbol table and others use
546 // a regular symbol table. Because the types of the two symbol tables differ in
547 // our system (because they should behave differently) we can't uniformly
548 // represent all relocations with the same base class if we expose an interface
549 // that mentions the symbol table type. So we split the two base types into two
550 // different classes, one which handles the section the relocation is applied to
551 // and another which handles the symbol table type. The symbol table type is
552 // taken as a type parameter to the class (see RelocSectionWithSymtabBase).
553 class RelocationSectionBase : public SectionBase {
555 SectionBase *SecToApplyRel = nullptr;
558 const SectionBase *getSection() const { return SecToApplyRel; }
559 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
561 static bool classof(const SectionBase *S) {
562 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
566 // Takes the symbol table type to use as a parameter so that we can deduplicate
567 // that code between the two symbol table types.
568 template <class SymTabType>
569 class RelocSectionWithSymtabBase : public RelocationSectionBase {
570 SymTabType *Symbols = nullptr;
571 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
574 RelocSectionWithSymtabBase() = default;
577 void removeSectionReferences(const SectionBase *Sec) override;
578 void initialize(SectionTableRef SecTable) override;
579 void finalize() override;
582 class RelocationSection
583 : public RelocSectionWithSymtabBase<SymbolTableSection> {
584 MAKE_SEC_WRITER_FRIEND
586 std::vector<Relocation> Relocations;
589 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
590 void accept(SectionVisitor &Visitor) const override;
591 void accept(MutableSectionVisitor &Visitor) override;
592 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
593 void markSymbols() override;
595 static bool classof(const SectionBase *S) {
596 if (S->Flags & ELF::SHF_ALLOC)
598 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
602 // TODO: The way stripping and groups interact is complicated
603 // and still needs to be worked on.
605 class GroupSection : public SectionBase {
606 MAKE_SEC_WRITER_FRIEND
607 const SymbolTableSection *SymTab = nullptr;
608 Symbol *Sym = nullptr;
609 ELF::Elf32_Word FlagWord;
610 SmallVector<SectionBase *, 3> GroupMembers;
613 // TODO: Contents is present in several classes of the hierarchy.
614 // This needs to be refactored to avoid duplication.
615 ArrayRef<uint8_t> Contents;
617 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
619 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
620 void setSymbol(Symbol *S) { Sym = S; }
621 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
622 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
624 void accept(SectionVisitor &) const override;
625 void accept(MutableSectionVisitor &Visitor) override;
626 void finalize() override;
627 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
628 void markSymbols() override;
630 static bool classof(const SectionBase *S) {
631 return S->Type == ELF::SHT_GROUP;
635 class DynamicSymbolTableSection : public Section {
637 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
639 static bool classof(const SectionBase *S) {
640 return S->Type == ELF::SHT_DYNSYM;
644 class DynamicSection : public Section {
646 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
648 static bool classof(const SectionBase *S) {
649 return S->Type == ELF::SHT_DYNAMIC;
653 class DynamicRelocationSection
654 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
655 MAKE_SEC_WRITER_FRIEND
658 ArrayRef<uint8_t> Contents;
661 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
663 void accept(SectionVisitor &) const override;
664 void accept(MutableSectionVisitor &Visitor) override;
666 static bool classof(const SectionBase *S) {
667 if (!(S->Flags & ELF::SHF_ALLOC))
669 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
673 class GnuDebugLinkSection : public SectionBase {
674 MAKE_SEC_WRITER_FRIEND
680 void init(StringRef File, StringRef Data);
683 // If we add this section from an external source we can use this ctor.
684 explicit GnuDebugLinkSection(StringRef File);
685 void accept(SectionVisitor &Visitor) const override;
686 void accept(MutableSectionVisitor &Visitor) override;
692 virtual std::unique_ptr<Object> create() const = 0;
695 using object::Binary;
696 using object::ELFFile;
697 using object::ELFObjectFile;
698 using object::OwningBinary;
700 class BinaryELFBuilder {
702 MemoryBuffer *MemBuf;
703 std::unique_ptr<Object> Obj;
705 void initFileHeader();
706 void initHeaderSegment();
707 StringTableSection *addStrTab();
708 SymbolTableSection *addSymTab(StringTableSection *StrTab);
709 void addData(SymbolTableSection *SymTab);
713 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
714 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
716 std::unique_ptr<Object> build();
719 template <class ELFT> class ELFBuilder {
721 using Elf_Addr = typename ELFT::Addr;
722 using Elf_Shdr = typename ELFT::Shdr;
723 using Elf_Word = typename ELFT::Word;
725 const ELFFile<ELFT> &ElfFile;
728 void setParentSegment(Segment &Child);
729 void readProgramHeaders();
730 void initGroupSection(GroupSection *GroupSec);
731 void initSymbolTable(SymbolTableSection *SymTab);
732 void readSectionHeaders();
733 SectionBase &makeSection(const Elf_Shdr &Shdr);
736 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
737 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
742 class BinaryReader : public Reader {
743 const MachineInfo &MInfo;
744 MemoryBuffer *MemBuf;
747 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
748 : MInfo(MI), MemBuf(MB) {}
749 std::unique_ptr<Object> create() const override;
752 class ELFReader : public Reader {
756 std::unique_ptr<Object> create() const override;
757 explicit ELFReader(Binary *B) : Bin(B) {}
762 using SecPtr = std::unique_ptr<SectionBase>;
763 using SegPtr = std::unique_ptr<Segment>;
765 std::vector<SecPtr> Sections;
766 std::vector<SegPtr> Segments;
770 using Range = iterator_range<
771 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
774 using ConstRange = iterator_range<pointee_iterator<
775 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
777 // It is often the case that the ELF header and the program header table are
778 // not present in any segment. This could be a problem during file layout,
779 // because other segments may get assigned an offset where either of the
780 // two should reside, which will effectively corrupt the resulting binary.
781 // Other than that we use these segments to track program header offsets
782 // when they may not follow the ELF header.
783 Segment ElfHdrSegment;
784 Segment ProgramHdrSegment;
795 StringTableSection *SectionNames = nullptr;
796 SymbolTableSection *SymbolTable = nullptr;
797 SectionIndexSection *SectionIndexTable = nullptr;
800 SectionTableRef sections() { return SectionTableRef(Sections); }
801 ConstRange<SectionBase> sections() const {
802 return make_pointee_range(Sections);
804 Range<Segment> segments() { return make_pointee_range(Segments); }
805 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
807 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
808 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
809 template <class T, class... Ts> T &addSection(Ts &&... Args) {
810 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
811 auto Ptr = Sec.get();
812 Sections.emplace_back(std::move(Sec));
813 Ptr->Index = Sections.size();
816 Segment &addSegment(ArrayRef<uint8_t> Data) {
817 Segments.emplace_back(llvm::make_unique<Segment>(Data));
818 return *Segments.back();
822 } // end namespace elf
823 } // end namespace objcopy
824 } // end namespace llvm
826 #endif // LLVM_TOOLS_OBJCOPY_OBJECT_H