Index: test/tools/llvm-objcopy/remove-shstrtab-error.test =================================================================== --- test/tools/llvm-objcopy/remove-shstrtab-error.test +++ test/tools/llvm-objcopy/remove-shstrtab-error.test @@ -8,4 +8,4 @@ Type: ET_REL Machine: EM_X86_64 -# CHECK: Cannot remove .shstrtab because it is the section header string table. +# CHECK: Cannot write section header table because section header string table was removed. Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -16,6 +16,7 @@ #include "llvm/BinaryFormat/ELF.h" #include "llvm/MC/StringTableBuilder.h" #include "llvm/Object/ELFObjectFile.h" +#include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/JamCRC.h" #include #include @@ -26,25 +27,132 @@ namespace llvm { -class FileOutputBuffer; class SectionBase; +class Section; +class OwnedDataSection; +class StringTableSection; +class SymbolTableSection; +class RelocationSection; +class DynamicRelocationSection; +class GnuDebugLinkSection; class Segment; +class Object; class SectionTableRef { private: - ArrayRef> Sections; + MutableArrayRef> Sections; public: - SectionTableRef(ArrayRef> Secs) + using iterator = pointee_iterator *>; + + SectionTableRef(MutableArrayRef> Secs) : Sections(Secs) {} SectionTableRef(const SectionTableRef &) = default; + iterator begin() { return iterator(Sections.data()); } + iterator end() { return iterator(Sections.data() + Sections.size()); } + SectionBase *getSection(uint16_t Index, Twine ErrMsg); template T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg); }; +enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE }; + +class SectionVisitor { +public: + virtual ~SectionVisitor(); + + virtual void visit(const Section &Sec) = 0; + virtual void visit(const OwnedDataSection &Sec) = 0; + virtual void visit(const StringTableSection &Sec) = 0; + virtual void visit(const SymbolTableSection &Sec) = 0; + virtual void visit(const RelocationSection &Sec) = 0; + virtual void visit(const DynamicRelocationSection &Sec) = 0; + virtual void visit(const GnuDebugLinkSection &Sec) = 0; +}; + +class Writer { +public: + virtual ~Writer(); + + virtual void finalize() = 0; + virtual void write() = 0; +}; + +template class SectionWriter : public SectionVisitor { +private: + using Elf_Word = typename ELFT::Word; + using Elf_Rel = typename ELFT::Rel; + using Elf_Rela = typename ELFT::Rela; + + FileOutputBuffer &Out; + +public: + virtual ~SectionWriter() {} + void visit(const Section &Sec) override; + void visit(const OwnedDataSection &Sec) override; + void visit(const StringTableSection &Sec) override; + void visit(const SymbolTableSection &Sec) override; + void visit(const RelocationSection &Sec) override; + void visit(const DynamicRelocationSection &Sec) override; + void visit(const GnuDebugLinkSection &Sec) override; + + SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {} +}; + +#define MAKE_SEC_WRITER_FRIEND template friend class SectionWriter; + +template class ELFWriter : public Writer { +private: + using Elf_Shdr = typename ELFT::Shdr; + using Elf_Phdr = typename ELFT::Phdr; + using Elf_Ehdr = typename ELFT::Ehdr; + + void writeEhdr(); + void writePhdr(const Segment &Seg); + void writeShdr(const SectionBase &Sec); + + void writePhdrs(); + void writeShdrs(); + void writeSectionData(); + + void assignOffsets(); + + StringRef File; + Object &Obj; + std::unique_ptr BufPtr; + std::unique_ptr> SecWriter; + + size_t totalSize() const; + +public: + virtual ~ELFWriter() {} + bool WriteSectionHeaders = true; + + void finalize() override; + void write() override; + ELFWriter(StringRef File, Object &Obj, bool WSH) + : File(File), Obj(Obj), WriteSectionHeaders(WSH) {} +}; + +class BinaryWriter : public Writer { +private: + StringRef File; + Object &Obj; + std::unique_ptr BufPtr; + std::unique_ptr SecWriter; + + uint64_t TotalSize; + +public: + ~BinaryWriter() {} + void finalize() override; + void write() override; + BinaryWriter(StringRef File, Object &Obj) : File(File), Obj(Obj) {} +}; + class SectionBase { public: StringRef Name; @@ -69,8 +177,7 @@ virtual void initialize(SectionTableRef SecTable); virtual void finalize(); virtual void removeSectionReferences(const SectionBase *Sec); - template void writeHeader(FileOutputBuffer &Out) const; - virtual void writeSection(FileOutputBuffer &Out) const = 0; + virtual void accept(SectionVisitor &Visitor) const = 0; }; class Segment { @@ -113,21 +220,23 @@ void removeSection(const SectionBase *Sec) { Sections.erase(Sec); } void addSection(const SectionBase *Sec) { Sections.insert(Sec); } - template void writeHeader(FileOutputBuffer &Out) const; - void writeSegment(FileOutputBuffer &Out) const; }; class Section : public SectionBase { + MAKE_SEC_WRITER_FRIEND + private: ArrayRef Contents; public: Section(ArrayRef Data) : Contents(Data) {} - void writeSection(FileOutputBuffer &Out) const override; + void accept(SectionVisitor &Visitor) const override; }; class OwnedDataSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + private: std::vector Data; @@ -137,8 +246,10 @@ Name = SecName; Type = ELF::SHT_PROGBITS; Size = Data.size(); + OriginalOffset = std::numeric_limits::max(); } - void writeSection(FileOutputBuffer &Out) const override; + + void accept(SectionVisitor &Sec) const override; }; // There are two types of string tables that can exist, dynamic and not dynamic. @@ -150,6 +261,8 @@ // classof method checks that the particular instance is not allocated. This // then agrees with the makeSection method used to construct most sections. class StringTableSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + private: StringTableBuilder StrTabBuilder; @@ -161,7 +274,7 @@ void addString(StringRef Name); uint32_t findIndex(StringRef Name) const; void finalize() override; - void writeSection(FileOutputBuffer &Out) const override; + void accept(SectionVisitor &Visitor) const override; static bool classof(const SectionBase *S) { if (S->Flags & ELF::SHF_ALLOC) @@ -200,6 +313,8 @@ }; class SymbolTableSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + protected: std::vector> Symbols; StringTableSection *SymbolNames = nullptr; @@ -218,17 +333,13 @@ void localize(std::function ToLocalize); void initialize(SectionTableRef SecTable) override; void finalize() override; + void accept(SectionVisitor &Visitor) const override; static bool classof(const SectionBase *S) { return S->Type == ELF::SHT_SYMTAB; } }; -// Only writeSection depends on the ELF type so we implement it in a subclass. -template class SymbolTableSectionImpl : public SymbolTableSection { - void writeSection(FileOutputBuffer &Out) const override; -}; - struct Relocation { const Symbol *RelocSymbol = nullptr; uint64_t Offset; @@ -275,20 +386,16 @@ void finalize() override; }; -template class RelocationSection : public RelocSectionWithSymtabBase { -private: - using Elf_Rel = typename ELFT::Rel; - using Elf_Rela = typename ELFT::Rela; + MAKE_SEC_WRITER_FRIEND +private: std::vector Relocations; - template void writeRel(T *Buf) const; - public: void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } - void writeSection(FileOutputBuffer &Out) const override; + void accept(SectionVisitor &Visitor) const override; static bool classof(const SectionBase *S) { if (S->Flags & ELF::SHF_ALLOC) @@ -331,13 +438,15 @@ class DynamicRelocationSection : public RelocSectionWithSymtabBase { + MAKE_SEC_WRITER_FRIEND + private: ArrayRef Contents; public: DynamicRelocationSection(ArrayRef Data) : Contents(Data) {} - void writeSection(FileOutputBuffer &Out) const override; + void accept(SectionVisitor &) const override; static bool classof(const SectionBase *S) { if (!(S->Flags & ELF::SHF_ALLOC)) @@ -346,12 +455,10 @@ } }; -template class GnuDebugLinkSection : public SectionBase { +class GnuDebugLinkSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + private: - // Elf_Word is 4-bytes on every format but has the same endianess as the elf - // type ELFT. We'll need to write the CRC32 out in the proper endianess so - // we'll make sure to use this type. - using Elf_Word = typename ELFT::Word; StringRef FileName; uint32_t CRC32; @@ -361,92 +468,99 @@ public: // If we add this section from an external source we can use this ctor. GnuDebugLinkSection(StringRef File); - void writeSection(FileOutputBuffer &Out) const override; + void accept(SectionVisitor &Visitor) const override; }; -template class Object { -private: - using SecPtr = std::unique_ptr; - using SegPtr = std::unique_ptr; +class Reader { +public: + virtual ~Reader(); + virtual std::unique_ptr create() const = 0; +}; +template class ELFBuilder { +private: using Elf_Shdr = typename ELFT::Shdr; - using Elf_Ehdr = typename ELFT::Ehdr; - using Elf_Phdr = typename ELFT::Phdr; - void initSymbolTable(const object::ELFFile &ElfFile, - SymbolTableSection *SymTab, SectionTableRef SecTable); - SecPtr makeSection(const object::ELFFile &ElfFile, - const Elf_Shdr &Shdr); - void readProgramHeaders(const object::ELFFile &ElfFile); - SectionTableRef readSectionHeaders(const object::ELFFile &ElfFile); - -protected: - StringTableSection *SectionNames = nullptr; - SymbolTableSection *SymbolTable = nullptr; - std::vector Sections; - std::vector Segments; + const object::ELFFile &ElfFile; + Object &Obj; - void writeHeader(FileOutputBuffer &Out) const; - void writeProgramHeaders(FileOutputBuffer &Out) const; - void writeSectionData(FileOutputBuffer &Out) const; - void writeSectionHeaders(FileOutputBuffer &Out) const; + void readProgramHeaders(); + void initSymbolTable(SymbolTableSection *SymTab); + void readSectionHeaders(); + SectionBase &makeSection(const Elf_Shdr &Shdr); public: - uint8_t Ident[16]; - uint64_t Entry; - uint64_t SHOffset; - uint32_t Type; - uint32_t Machine; - uint32_t Version; - uint32_t Flags; - bool WriteSectionHeaders = true; + ELFBuilder(const object::ELFObjectFile &ElfObj, Object &Obj) + : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {} - Object(const object::ELFObjectFile &Obj); - virtual ~Object() = default; - - SymbolTableSection *getSymTab() const { return SymbolTable; } - const SectionBase *getSectionHeaderStrTab() const { return SectionNames; } - void removeSections(std::function ToRemove); - void addSection(StringRef SecName, ArrayRef Data); - void addGnuDebugLink(StringRef File); - virtual size_t totalSize() const = 0; - virtual void finalize() = 0; - virtual void write(FileOutputBuffer &Out) const = 0; + void build(); }; -template class ELFObject : public Object { -private: - using SecPtr = std::unique_ptr; - using SegPtr = std::unique_ptr; - - using Elf_Shdr = typename ELFT::Shdr; - using Elf_Ehdr = typename ELFT::Ehdr; - using Elf_Phdr = typename ELFT::Phdr; +using namespace object; - void sortSections(); - void assignOffsets(); +class ELFReader : public Reader { +private: + // We keep this around so that Objects can be constructed from objects owned + // by the binary object and they won't be deallocated. + OwningBinary Binary; public: - ELFObject(const object::ELFObjectFile &Obj) : Object(Obj) {} - - void finalize() override; - size_t totalSize() const override; - void write(FileOutputBuffer &Out) const override; + ElfType getElfType() const; + std::unique_ptr create() const override; + ELFReader(StringRef File); }; -template class BinaryObject : public Object { +class Object { private: using SecPtr = std::unique_ptr; using SegPtr = std::unique_ptr; - uint64_t TotalSize; +protected: + std::vector Sections; + std::vector Segments; public: - BinaryObject(const object::ELFObjectFile &Obj) : Object(Obj) {} + template + using Range = iterator_range< + pointee_iterator>::iterator>>; - void finalize() override; - size_t totalSize() const override; - void write(FileOutputBuffer &Out) const override; + template + using ConstRange = iterator_range>::const_iterator>>; + + uint8_t Ident[16]; + uint64_t Entry; + uint64_t SHOffset; + uint32_t Type; + uint32_t Machine; + uint32_t Version; + uint32_t Flags; + + StringTableSection *SectionNames = nullptr; + SymbolTableSection *SymbolTable = nullptr; + + Object() {} + virtual ~Object() = default; + + void sortSections(); + SectionTableRef sections() { return SectionTableRef(Sections); } + ConstRange sections() const { + return make_pointee_range(Sections); + } + Range segments() { return make_pointee_range(Segments); } + ConstRange segments() const { return make_pointee_range(Segments); } + + void removeSections(std::function ToRemove); + template T &addSection(Ts &&... Args) { + auto Sec = llvm::make_unique(std::forward(Args)...); + auto Ptr = Sec.get(); + Sections.emplace_back(std::move(Sec)); + return *Ptr; + } + Segment &addSegment(ArrayRef Data) { + Segments.emplace_back(llvm::make_unique(Data)); + return *Segments.back(); + } }; } // end namespace llvm Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -30,61 +30,62 @@ using namespace object; using namespace ELF; -template void Segment::writeHeader(FileOutputBuffer &Out) const { +template void ELFWriter::writePhdr(const Segment &Seg) { using Elf_Ehdr = typename ELFT::Ehdr; using Elf_Phdr = typename ELFT::Phdr; - uint8_t *Buf = Out.getBufferStart(); - Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr); + uint8_t *Buf = BufPtr->getBufferStart(); + Buf += sizeof(Elf_Ehdr) + Seg.Index * sizeof(Elf_Phdr); Elf_Phdr &Phdr = *reinterpret_cast(Buf); - Phdr.p_type = Type; - Phdr.p_flags = Flags; - Phdr.p_offset = Offset; - Phdr.p_vaddr = VAddr; - Phdr.p_paddr = PAddr; - Phdr.p_filesz = FileSize; - Phdr.p_memsz = MemSize; - Phdr.p_align = Align; -} - -void Segment::writeSegment(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart() + Offset; - // We want to maintain segments' interstitial data and contents exactly. - // This lets us just copy segments directly. - std::copy(std::begin(Contents), std::end(Contents), Buf); + Phdr.p_type = Seg.Type; + Phdr.p_flags = Seg.Flags; + Phdr.p_offset = Seg.Offset; + Phdr.p_vaddr = Seg.VAddr; + Phdr.p_paddr = Seg.PAddr; + Phdr.p_filesz = Seg.FileSize; + Phdr.p_memsz = Seg.MemSize; + Phdr.p_align = Seg.Align; } void SectionBase::removeSectionReferences(const SectionBase *Sec) {} void SectionBase::initialize(SectionTableRef SecTable) {} void SectionBase::finalize() {} -template -void SectionBase::writeHeader(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart(); - Buf += HeaderOffset; +template void ELFWriter::writeShdr(const SectionBase &Sec) { + uint8_t *Buf = BufPtr->getBufferStart(); + Buf += Sec.HeaderOffset; typename ELFT::Shdr &Shdr = *reinterpret_cast(Buf); - Shdr.sh_name = NameIndex; - Shdr.sh_type = Type; - Shdr.sh_flags = Flags; - Shdr.sh_addr = Addr; - Shdr.sh_offset = Offset; - Shdr.sh_size = Size; - Shdr.sh_link = Link; - Shdr.sh_info = Info; - Shdr.sh_addralign = Align; - Shdr.sh_entsize = EntrySize; -} - -void Section::writeSection(FileOutputBuffer &Out) const { - if (Type == SHT_NOBITS) + Shdr.sh_name = Sec.NameIndex; + Shdr.sh_type = Sec.Type; + Shdr.sh_flags = Sec.Flags; + Shdr.sh_addr = Sec.Addr; + Shdr.sh_offset = Sec.Offset; + Shdr.sh_size = Sec.Size; + Shdr.sh_link = Sec.Link; + Shdr.sh_info = Sec.Info; + Shdr.sh_addralign = Sec.Align; + Shdr.sh_entsize = Sec.EntrySize; +} + +template void SectionWriter::visit(const Section &Sec) { + if (Sec.Type == SHT_NOBITS) return; - uint8_t *Buf = Out.getBufferStart() + Offset; - std::copy(std::begin(Contents), std::end(Contents), Buf); + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf); +} + +SectionVisitor::~SectionVisitor() {} + +void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } + +template +void SectionWriter::visit(const OwnedDataSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf); } -void OwnedDataSection::writeSection(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart() + Offset; - std::copy(std::begin(Data), std::end(Data), Buf); +void OwnedDataSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); } void StringTableSection::addString(StringRef Name) { @@ -98,8 +99,13 @@ void StringTableSection::finalize() { StrTabBuilder.finalize(); } -void StringTableSection::writeSection(FileOutputBuffer &Out) const { - StrTabBuilder.write(Out.getBufferStart() + Offset); +template +void SectionWriter::visit(const StringTableSection &Sec) { + Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset); +} + +void StringTableSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); } static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) { @@ -234,12 +240,12 @@ } template -void SymbolTableSectionImpl::writeSection(FileOutputBuffer &Out) const { +void SectionWriter::visit(const SymbolTableSection &Sec) { uint8_t *Buf = Out.getBufferStart(); - Buf += Offset; + Buf += Sec.Offset; typename ELFT::Sym *Sym = reinterpret_cast(Buf); // Loop though symbols setting each entry of the symbol table. - for (auto &Symbol : Symbols) { + for (auto &Symbol : Sec.Symbols) { Sym->st_name = Symbol->NameIndex; Sym->st_value = Symbol->Value; Sym->st_size = Symbol->Size; @@ -251,6 +257,10 @@ } } +void SymbolTableSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + template void RelocSectionWithSymtabBase::removeSectionReferences( const SectionBase *Sec) { @@ -294,9 +304,8 @@ Rela.r_addend = Addend; } -template -template -void RelocationSection::writeRel(T *Buf) const { +template +void writeRel(const RelRange &Relocations, T *Buf) { for (const auto &Reloc : Relocations) { Buf->r_offset = Reloc.Offset; setAddend(*Buf, Reloc.Addend); @@ -306,17 +315,26 @@ } template -void RelocationSection::writeSection(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart() + Offset; - if (Type == SHT_REL) - writeRel(reinterpret_cast(Buf)); +void SectionWriter::visit(const RelocationSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + if (Sec.Type == SHT_REL) + writeRel(Sec.Relocations, reinterpret_cast(Buf)); else - writeRel(reinterpret_cast(Buf)); + writeRel(Sec.Relocations, reinterpret_cast(Buf)); +} + +void RelocationSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +template +void SectionWriter::visit(const DynamicRelocationSection &Sec) { + std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), + Out.getBufferStart() + Sec.Offset); } -void DynamicRelocationSection::writeSection(FileOutputBuffer &Out) const { - std::copy(std::begin(Contents), std::end(Contents), - Out.getBufferStart() + Offset); +void DynamicRelocationSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); } void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) { @@ -344,8 +362,7 @@ void SectionWithStrTab::finalize() { this->Link = StrTab->Index; } -template -void GnuDebugLinkSection::init(StringRef File, StringRef Data) { +void GnuDebugLinkSection::init(StringRef File, StringRef Data) { FileName = sys::path::stem(File); // The format for the .gnu_debuglink starts with the stemmed file name and is // followed by a null terminator and then the CRC32 of the file. The CRC32 @@ -368,9 +385,7 @@ CRC32 = ~crc.getCRC(); } -template -GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) - : FileName(File) { +GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) { // Read in the file to compute the CRC of it. auto DebugOrErr = MemoryBuffer::getFile(File); if (!DebugOrErr) @@ -380,12 +395,17 @@ } template -void GnuDebugLinkSection::writeSection(FileOutputBuffer &Out) const { - auto Buf = Out.getBufferStart() + Offset; +void SectionWriter::visit(const GnuDebugLinkSection &Sec) { + auto Buf = Out.getBufferStart() + Sec.Offset; char *File = reinterpret_cast(Buf); - Elf_Word *CRC = reinterpret_cast(Buf + Size - sizeof(Elf_Word)); - *CRC = CRC32; - std::copy(std::begin(FileName), std::end(FileName), File); + Elf_Word *CRC = + reinterpret_cast(Buf + Sec.Size - sizeof(Elf_Word)); + *CRC = Sec.CRC32; + std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File); +} + +void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); } // Returns true IFF a section is wholly inside the range of a segment @@ -419,14 +439,12 @@ return A->Index < B->Index; } -template -void Object::readProgramHeaders(const ELFFile &ElfFile) { +template void ELFBuilder::readProgramHeaders() { uint32_t Index = 0; for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) { ArrayRef Data{ElfFile.base() + Phdr.p_offset, (size_t)Phdr.p_filesz}; - Segments.emplace_back(llvm::make_unique(Data)); - Segment &Seg = *Segments.back(); + Segment &Seg = Obj.addSegment(Data); Seg.Type = Phdr.p_type; Seg.Flags = Phdr.p_flags; Seg.OriginalOffset = Phdr.p_offset; @@ -437,29 +455,29 @@ Seg.MemSize = Phdr.p_memsz; Seg.Align = Phdr.p_align; Seg.Index = Index++; - for (auto &Section : Sections) { - if (sectionWithinSegment(*Section, Seg)) { - Seg.addSection(&*Section); - if (!Section->ParentSegment || - Section->ParentSegment->Offset > Seg.Offset) { - Section->ParentSegment = &Seg; + for (auto &Section : Obj.sections()) { + if (sectionWithinSegment(Section, Seg)) { + Seg.addSection(&Section); + if (!Section.ParentSegment || + Section.ParentSegment->Offset > Seg.Offset) { + Section.ParentSegment = &Seg; } } } } // Now we do an O(n^2) loop through the segments in order to match up // segments. - for (auto &Child : Segments) { - for (auto &Parent : Segments) { + for (auto &Child : Obj.segments()) { + for (auto &Parent : Obj.segments()) { // Every segment will overlap with itself but we don't want a segment to // be it's own parent so we avoid that situation. - if (&Child != &Parent && segmentOverlapsSegment(*Child, *Parent)) { + if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) { // We want a canonical "most parental" segment but this requires // inspecting the ParentSegment. - if (compareSegments(Parent.get(), Child.get())) - if (Child->ParentSegment == nullptr || - compareSegments(Parent.get(), Child->ParentSegment)) { - Child->ParentSegment = Parent.get(); + if (compareSegments(&Parent, &Child)) + if (Child.ParentSegment == nullptr || + compareSegments(&Parent, Child.ParentSegment)) { + Child.ParentSegment = &Parent; } } } @@ -467,9 +485,7 @@ } template -void Object::initSymbolTable(const object::ELFFile &ElfFile, - SymbolTableSection *SymTab, - SectionTableRef SecTable) { +void ELFBuilder::initSymbolTable(SymbolTableSection *SymTab) { const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index)); StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr)); @@ -478,14 +494,14 @@ StringRef Name = unwrapOrError(Sym.getName(StrTabData)); if (Sym.st_shndx >= SHN_LORESERVE) { - if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) { + if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) { error( "Symbol '" + Name + "' has unsupported value greater than or equal to SHN_LORESERVE: " + Twine(Sym.st_shndx)); } } else if (Sym.st_shndx != SHN_UNDEF) { - DefSection = SecTable.getSection( + DefSection = Obj.sections().getSection( Sym.st_shndx, "Symbol '" + Name + "' is defined in invalid section with index " + Twine(Sym.st_shndx)); @@ -504,9 +520,9 @@ ToSet = Rela.r_addend; } -template -void initRelocations(RelocationSection *Relocs, - SymbolTableSection *SymbolTable, T RelRange) { +template +void initRelocations(RelocationSection *Relocs, SymbolTableSection *SymbolTable, + T RelRange) { for (const auto &Rel : RelRange) { Relocation ToAdd; ToAdd.Offset = Rel.r_offset; @@ -532,147 +548,191 @@ } template -std::unique_ptr -Object::makeSection(const object::ELFFile &ElfFile, - const Elf_Shdr &Shdr) { +SectionBase &ELFBuilder::makeSection(const Elf_Shdr &Shdr) { ArrayRef Data; switch (Shdr.sh_type) { case SHT_REL: case SHT_RELA: if (Shdr.sh_flags & SHF_ALLOC) { Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique(Data); + return Obj.addSection(Data); } - return llvm::make_unique>(); + return Obj.addSection(); case SHT_STRTAB: // If a string table is allocated we don't want to mess with it. That would // mean altering the memory image. There are no special link types or // anything so we can just use a Section. if (Shdr.sh_flags & SHF_ALLOC) { Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique
(Data); + return Obj.addSection
(Data); } - return llvm::make_unique(); + return Obj.addSection(); case SHT_HASH: case SHT_GNU_HASH: // Hash tables should refer to SHT_DYNSYM which we're not going to change. // Because of this we don't need to mess with the hash tables either. Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique
(Data); + return Obj.addSection
(Data); case SHT_DYNSYM: Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique(Data); + return Obj.addSection(Data); case SHT_DYNAMIC: Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique(Data); + return Obj.addSection(Data); case SHT_SYMTAB: { - auto SymTab = llvm::make_unique>(); - SymbolTable = SymTab.get(); - return std::move(SymTab); + auto &SymTab = Obj.addSection(); + Obj.SymbolTable = &SymTab; + return SymTab; } case SHT_NOBITS: - return llvm::make_unique
(Data); + return Obj.addSection
(Data); default: Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique
(Data); + return Obj.addSection
(Data); } } -template -SectionTableRef Object::readSectionHeaders(const ELFFile &ElfFile) { +template void ELFBuilder::readSectionHeaders() { uint32_t Index = 0; for (const auto &Shdr : unwrapOrError(ElfFile.sections())) { if (Index == 0) { ++Index; continue; } - SecPtr Sec = makeSection(ElfFile, Shdr); - Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr)); - Sec->Type = Shdr.sh_type; - Sec->Flags = Shdr.sh_flags; - Sec->Addr = Shdr.sh_addr; - Sec->Offset = Shdr.sh_offset; - Sec->OriginalOffset = Shdr.sh_offset; - Sec->Size = Shdr.sh_size; - Sec->Link = Shdr.sh_link; - Sec->Info = Shdr.sh_info; - Sec->Align = Shdr.sh_addralign; - Sec->EntrySize = Shdr.sh_entsize; - Sec->Index = Index++; - Sections.push_back(std::move(Sec)); - } - - SectionTableRef SecTable(Sections); + auto &Sec = makeSection(Shdr); + Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr)); + Sec.Type = Shdr.sh_type; + Sec.Flags = Shdr.sh_flags; + Sec.Addr = Shdr.sh_addr; + Sec.Offset = Shdr.sh_offset; + Sec.OriginalOffset = Shdr.sh_offset; + Sec.Size = Shdr.sh_size; + Sec.Link = Shdr.sh_link; + Sec.Info = Shdr.sh_info; + Sec.Align = Shdr.sh_addralign; + Sec.EntrySize = Shdr.sh_entsize; + Sec.Index = Index++; + } // Now that all of the sections have been added we can fill out some extra // details about symbol tables. We need the symbol table filled out before // any relocations. - if (SymbolTable) { - SymbolTable->initialize(SecTable); - initSymbolTable(ElfFile, SymbolTable, SecTable); + if (Obj.SymbolTable) { + Obj.SymbolTable->initialize(Obj.sections()); + initSymbolTable(Obj.SymbolTable); } // Now that all sections and symbols have been added we can add // relocations that reference symbols and set the link and info fields for // relocation sections. - for (auto &Section : Sections) { - if (Section.get() == SymbolTable) + for (auto &Section : Obj.sections()) { + if (&Section == Obj.SymbolTable) continue; - Section->initialize(SecTable); - if (auto RelSec = dyn_cast>(Section.get())) { + Section.initialize(Obj.sections()); + if (auto RelSec = dyn_cast(&Section)) { auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index; if (RelSec->Type == SHT_REL) - initRelocations(RelSec, SymbolTable, unwrapOrError(ElfFile.rels(Shdr))); + initRelocations(RelSec, Obj.SymbolTable, + unwrapOrError(ElfFile.rels(Shdr))); else - initRelocations(RelSec, SymbolTable, + initRelocations(RelSec, Obj.SymbolTable, unwrapOrError(ElfFile.relas(Shdr))); } } - - return SecTable; } -template Object::Object(const ELFObjectFile &Obj) { - const auto &ElfFile = *Obj.getELFFile(); +template void ELFBuilder::build() { const auto &Ehdr = *ElfFile.getHeader(); - std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident); - Type = Ehdr.e_type; - Machine = Ehdr.e_machine; - Version = Ehdr.e_version; - Entry = Ehdr.e_entry; - Flags = Ehdr.e_flags; - - SectionTableRef SecTable = readSectionHeaders(ElfFile); - readProgramHeaders(ElfFile); - - SectionNames = SecTable.getSectionOfType( - Ehdr.e_shstrndx, - "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " + - " is invalid", - "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " + - " is not a string table"); + std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Obj.Ident); + Obj.Type = Ehdr.e_type; + Obj.Machine = Ehdr.e_machine; + Obj.Version = Ehdr.e_version; + Obj.Entry = Ehdr.e_entry; + Obj.Flags = Ehdr.e_flags; + + readSectionHeaders(); + readProgramHeaders(); + + Obj.SectionNames = + Obj.sections().template getSectionOfType( + Ehdr.e_shstrndx, + "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + + " in elf header " + " is invalid", + "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + + " in elf header " + " is not a string table"); } -template -void Object::writeHeader(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart(); +// A generic size function which computes sizes of any random access range. +template size_t size(R &&Range) { + return static_cast(std::end(Range) - std::begin(Range)); +} + +Writer::~Writer() {} + +Reader::~Reader() {} + +ELFReader::ELFReader(StringRef File) { + auto BinaryOrErr = createBinary(File); + if (!BinaryOrErr) + reportError(File, BinaryOrErr.takeError()); + Binary = std::move(BinaryOrErr.get()); +} + +ElfType ELFReader::getElfType() const { + const auto &Bin = *Binary.getBinary(); + if (auto *o = dyn_cast>(&Bin)) + return ELFT_ELF32LE; + if (auto *o = dyn_cast>(&Bin)) + return ELFT_ELF64LE; + if (auto *o = dyn_cast>(&Bin)) + return ELFT_ELF32BE; + if (auto *o = dyn_cast>(&Bin)) + return ELFT_ELF64BE; + llvm_unreachable("Invalid ELFType"); +} + +std::unique_ptr ELFReader::create() const { + const auto &Bin = *Binary.getBinary(); + auto Obj = llvm::make_unique(); + if (auto *o = dyn_cast>(&Bin)) { + ELFBuilder Builder(*o, *Obj); + Builder.build(); + return Obj; + } else if (auto *o = dyn_cast>(&Bin)) { + ELFBuilder Builder(*o, *Obj); + Builder.build(); + return Obj; + } else if (auto *o = dyn_cast>(&Bin)) { + ELFBuilder Builder(*o, *Obj); + Builder.build(); + return Obj; + } else if (auto *o = dyn_cast>(&Bin)) { + ELFBuilder Builder(*o, *Obj); + Builder.build(); + return Obj; + } + error("Invalid file type"); +} + +template void ELFWriter::writeEhdr() { + uint8_t *Buf = BufPtr->getBufferStart(); Elf_Ehdr &Ehdr = *reinterpret_cast(Buf); - std::copy(Ident, Ident + 16, Ehdr.e_ident); - Ehdr.e_type = Type; - Ehdr.e_machine = Machine; - Ehdr.e_version = Version; - Ehdr.e_entry = Entry; + std::copy(Obj.Ident, Obj.Ident + 16, Ehdr.e_ident); + Ehdr.e_type = Obj.Type; + Ehdr.e_machine = Obj.Machine; + Ehdr.e_version = Obj.Version; + Ehdr.e_entry = Obj.Entry; Ehdr.e_phoff = sizeof(Elf_Ehdr); - Ehdr.e_flags = Flags; + Ehdr.e_flags = Obj.Flags; Ehdr.e_ehsize = sizeof(Elf_Ehdr); Ehdr.e_phentsize = sizeof(Elf_Phdr); - Ehdr.e_phnum = Segments.size(); + Ehdr.e_phnum = size(Obj.segments()); Ehdr.e_shentsize = sizeof(Elf_Shdr); if (WriteSectionHeaders) { - Ehdr.e_shoff = SHOffset; - Ehdr.e_shnum = Sections.size() + 1; - Ehdr.e_shstrndx = SectionNames->Index; + Ehdr.e_shoff = Obj.SHOffset; + Ehdr.e_shnum = size(Obj.sections()) + 1; + Ehdr.e_shstrndx = Obj.SectionNames->Index; } else { Ehdr.e_shoff = 0; Ehdr.e_shnum = 0; @@ -680,15 +740,13 @@ } } -template -void Object::writeProgramHeaders(FileOutputBuffer &Out) const { - for (auto &Phdr : Segments) - Phdr->template writeHeader(Out); +template void ELFWriter::writePhdrs() { + for (auto &Seg : Obj.segments()) + writePhdr(Seg); } -template -void Object::writeSectionHeaders(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart() + SHOffset; +template void ELFWriter::writeShdrs() { + uint8_t *Buf = BufPtr->getBufferStart() + Obj.SHOffset; // This reference serves to write the dummy section header at the begining // of the file. It is not used for anything else Elf_Shdr &Shdr = *reinterpret_cast(Buf); @@ -703,19 +761,16 @@ Shdr.sh_addralign = 0; Shdr.sh_entsize = 0; - for (auto &Section : Sections) - Section->template writeHeader(Out); + for (auto &Sec : Obj.sections()) + writeShdr(Sec); } -template -void Object::writeSectionData(FileOutputBuffer &Out) const { - for (auto &Section : Sections) - Section->writeSection(Out); +template void ELFWriter::writeSectionData() { + for (auto &Sec : Obj.sections()) + Sec.accept(*SecWriter); } -template -void Object::removeSections( - std::function ToRemove) { +void Object::removeSections(std::function ToRemove) { auto Iter = std::stable_partition( std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) { @@ -729,10 +784,7 @@ }); if (SymbolTable != nullptr && ToRemove(*SymbolTable)) SymbolTable = nullptr; - if (ToRemove(*SectionNames)) { - if (WriteSectionHeaders) - error("Cannot remove " + SectionNames->Name + - " because it is the section header string table."); + if (SectionNames != nullptr && ToRemove(*SectionNames)) { SectionNames = nullptr; } // Now make sure there are no remaining references to the sections that will @@ -748,18 +800,7 @@ Sections.erase(Iter, std::end(Sections)); } -template -void Object::addSection(StringRef SecName, ArrayRef Data) { - auto Sec = llvm::make_unique(SecName, Data); - Sec->OriginalOffset = ~0ULL; - Sections.push_back(std::move(Sec)); -} - -template void Object::addGnuDebugLink(StringRef File) { - Sections.emplace_back(llvm::make_unique>(File)); -} - -template void ELFObject::sortSections() { +void Object::sortSections() { // Put all sections in offset order. Maintain the ordering as closely as // possible while meeting that demand however. auto CompareSections = [](const SecPtr &A, const SecPtr &B) { @@ -823,8 +864,8 @@ // does not have a ParentSegment. It returns either the offset given if all // sections had a ParentSegment or an offset one past the last section if there // was a section that didn't have a ParentSegment. -template -static uint64_t LayoutSections(std::vector &Sections, uint64_t Offset) { +template +static uint64_t LayoutSections(Range Sections, uint64_t Offset) { // Now the offset of every segment has been set we can assign the offsets // of each section. For sections that are covered by a segment we should use // the segment's original offset and the section's original offset to compute @@ -833,28 +874,28 @@ // covered by segments we can just bump Offset to the next valid location. uint32_t Index = 1; for (auto &Section : Sections) { - Section->Index = Index++; - if (Section->ParentSegment != nullptr) { - auto Segment = Section->ParentSegment; - Section->Offset = - Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset); + Section.Index = Index++; + if (Section.ParentSegment != nullptr) { + auto Segment = *Section.ParentSegment; + Section.Offset = + Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset); } else { - Offset = alignTo(Offset, Section->Align == 0 ? 1 : Section->Align); - Section->Offset = Offset; - if (Section->Type != SHT_NOBITS) - Offset += Section->Size; + Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align); + Section.Offset = Offset; + if (Section.Type != SHT_NOBITS) + Offset += Section.Size; } } return Offset; } -template void ELFObject::assignOffsets() { +template void ELFWriter::assignOffsets() { // We need a temporary list of segments that has a special order to it // so that we know that anytime ->ParentSegment is set that segment has // already had its offset properly set. std::vector OrderedSegments; - for (auto &Segment : this->Segments) - OrderedSegments.push_back(Segment.get()); + for (auto &Segment : Obj.segments()) + OrderedSegments.push_back(&Segment); OrderSegments(OrderedSegments); // The size of ELF + program headers will not change so it is ok to assume // that the first offset of the first segment is a good place to start @@ -867,72 +908,85 @@ Offset = sizeof(Elf_Ehdr); } Offset = LayoutSegments(OrderedSegments, Offset); - Offset = LayoutSections(this->Sections, Offset); + Offset = LayoutSections(Obj.sections(), Offset); // If we need to write the section header table out then we need to align the // Offset so that SHOffset is valid. - if (this->WriteSectionHeaders) + if (WriteSectionHeaders) Offset = alignTo(Offset, sizeof(typename ELFT::Addr)); - this->SHOffset = Offset; + Obj.SHOffset = Offset; } -template size_t ELFObject::totalSize() const { +template size_t ELFWriter::totalSize() const { // We already have the section header offset so we can calculate the total // size by just adding up the size of each section header. - auto NullSectionSize = this->WriteSectionHeaders ? sizeof(Elf_Shdr) : 0; - return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) + + auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0; + return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) + NullSectionSize; } -template void ELFObject::write(FileOutputBuffer &Out) const { - this->writeHeader(Out); - this->writeProgramHeaders(Out); - this->writeSectionData(Out); - if (this->WriteSectionHeaders) - this->writeSectionHeaders(Out); +template void ELFWriter::write() { + writeEhdr(); + writePhdrs(); + writeSectionData(); + if (WriteSectionHeaders) + writeShdrs(); + if (auto E = BufPtr->commit()) + reportError(File, errorToErrorCode(std::move(E))); } -template void ELFObject::finalize() { +template void ELFWriter::finalize() { + // It could happen that SectionNames has been removed and yet the user wants + // a section header table output. We need to throw an error if a user tries + // to do that. + if (Obj.SectionNames == nullptr && WriteSectionHeaders) + error("Cannot write section header table because section header string " + "table was removed."); + // Make sure we add the names of all the sections. - if (this->SectionNames != nullptr) - for (const auto &Section : this->Sections) { - this->SectionNames->addString(Section->Name); + if (Obj.SectionNames != nullptr) + for (const auto &Section : Obj.sections()) { + Obj.SectionNames->addString(Section.Name); } // Make sure we add the names of all the symbols. - if (this->SymbolTable != nullptr) - this->SymbolTable->addSymbolNames(); + if (Obj.SymbolTable != nullptr) + Obj.SymbolTable->addSymbolNames(); - sortSections(); + Obj.sortSections(); assignOffsets(); // Finalize SectionNames first so that we can assign name indexes. - if (this->SectionNames != nullptr) - this->SectionNames->finalize(); + if (Obj.SectionNames != nullptr) + Obj.SectionNames->finalize(); // Finally now that all offsets and indexes have been set we can finalize any // remaining issues. - uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr); - for (auto &Section : this->Sections) { - Section->HeaderOffset = Offset; + uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr); + for (auto &Section : Obj.sections()) { + Section.HeaderOffset = Offset; Offset += sizeof(Elf_Shdr); - if (this->WriteSectionHeaders) - Section->NameIndex = this->SectionNames->findIndex(Section->Name); - Section->finalize(); + if (WriteSectionHeaders) { + Section.NameIndex = Obj.SectionNames->findIndex(Section.Name); + } + Section.finalize(); } -} -template size_t BinaryObject::totalSize() const { - return TotalSize; + auto BufferOrErr = FileOutputBuffer::create(File, totalSize(), + FileOutputBuffer::F_executable); + handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &) { + error("failed to open " + File); + }); + BufPtr = std::move(*BufferOrErr); + SecWriter = llvm::make_unique>(*BufPtr); } -template -void BinaryObject::write(FileOutputBuffer &Out) const { - for (auto &Section : this->Sections) { - if ((Section->Flags & SHF_ALLOC) == 0) +void BinaryWriter::write() { + for (auto &Section : Obj.sections()) { + if ((Section.Flags & SHF_ALLOC) == 0) continue; - Section->writeSection(Out); + Section.accept(*SecWriter); } } -template void BinaryObject::finalize() { +void BinaryWriter::finalize() { // TODO: Create a filter range to construct OrderedSegments from so that this // code can be deduped with assignOffsets above. This should also solve the // todo below for LayoutSections. @@ -941,10 +995,9 @@ // already had it's offset properly set. We only want to consider the segments // that will affect layout of allocated sections so we only add those. std::vector OrderedSegments; - for (auto &Section : this->Sections) { - if ((Section->Flags & SHF_ALLOC) != 0 && - Section->ParentSegment != nullptr) { - OrderedSegments.push_back(Section->ParentSegment); + for (auto &Section : Obj.sections()) { + if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) { + OrderedSegments.push_back(Section.ParentSegment); } } OrderSegments(OrderedSegments); @@ -981,12 +1034,12 @@ // not hold. Then pass such a range to LayoutSections instead of constructing // AllocatedSections here. std::vector AllocatedSections; - for (auto &Section : this->Sections) { - if ((Section->Flags & SHF_ALLOC) == 0) + for (auto &Section : Obj.sections()) { + if ((Section.Flags & SHF_ALLOC) == 0) continue; - AllocatedSections.push_back(Section.get()); + AllocatedSections.push_back(&Section); } - LayoutSections(AllocatedSections, Offset); + LayoutSections(make_pointee_range(AllocatedSections), Offset); // Now that every section has been laid out we just need to compute the total // file size. This might not be the same as the offset returned by @@ -997,23 +1050,26 @@ if (Section->Type != SHT_NOBITS) TotalSize = std::max(TotalSize, Section->Offset + Section->Size); } + + auto BufferOrErr = + FileOutputBuffer::create(File, TotalSize, FileOutputBuffer::F_executable); + handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &) { + error("faild to open " + File); + }); + BufPtr = std::move(*BufferOrErr); + SecWriter = llvm::make_unique>(*BufPtr); } namespace llvm { -template class Object; -template class Object; -template class Object; -template class Object; - -template class ELFObject; -template class ELFObject; -template class ELFObject; -template class ELFObject; +template class ELFBuilder; +template class ELFBuilder; +template class ELFBuilder; +template class ELFBuilder; -template class BinaryObject; -template class BinaryObject; -template class BinaryObject; -template class BinaryObject; +template class ELFWriter; +template class ELFWriter; +template class ELFWriter; +template class ELFWriter; } // end namespace llvm Index: tools/llvm-objcopy/llvm-objcopy.h =================================================================== --- tools/llvm-objcopy/llvm-objcopy.h +++ tools/llvm-objcopy/llvm-objcopy.h @@ -19,6 +19,9 @@ namespace llvm { LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message); +LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, Error E); +LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, + std::error_code EC); // This is taken from llvm-readobj. // [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38) Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -130,42 +130,37 @@ bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); } -template -bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { +bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { // We can't remove the section header string table. - if (&Sec == Obj.getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; // Short of keeping the string table we want to keep everything that is a DWO // section and remove everything else. return !IsDWOSection(Sec); } -template -void WriteObjectFile(const Object &Obj, StringRef File) { - std::unique_ptr Buffer; - Expected> BufferOrErr = - FileOutputBuffer::create(File, Obj.totalSize(), - FileOutputBuffer::F_executable); - handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) { - error("failed to open " + OutputFilename); - }); - Buffer = std::move(*BufferOrErr); - - Obj.write(*Buffer); - if (auto E = Buffer->commit()) - reportError(File, errorToErrorCode(std::move(E))); -} - -template -void SplitDWOToFile(const ELFObjectFile &ObjFile, StringRef File) { - // Construct a second output file for the DWO sections. - ELFObject DWOFile(ObjFile); +static ElfType OutputElfType; - DWOFile.removeSections([&](const SectionBase &Sec) { - return OnlyKeepDWOPred(DWOFile, Sec); - }); - DWOFile.finalize(); - WriteObjectFile(DWOFile, File); +std::unique_ptr CreateWriter(Object &Obj) { + if (OutputFormat == "binary") { + return llvm::make_unique(OutputFilename, Obj); + } + // Depending on the initial ELFT and OutputFormat we need a different Writer. + switch (OutputElfType) { + case ELFT_ELF32LE: + return llvm::make_unique>(OutputFilename, Obj, + !StripSections); + case ELFT_ELF64LE: + return llvm::make_unique>(OutputFilename, Obj, + !StripSections); + case ELFT_ELF32BE: + return llvm::make_unique>(OutputFilename, Obj, + !StripSections); + case ELFT_ELF64BE: + return llvm::make_unique>(OutputFilename, Obj, + !StripSections); + } + llvm_unreachable("Invalid output format"); } // This function handles the high level operations of GNU objcopy including @@ -175,23 +170,21 @@ // any previous removals. Lastly whether or not something is removed shouldn't // depend a) on the order the options occur in or b) on some opaque priority // system. The only priority is that keeps/copies overrule removes. -template void CopyBinary(const ELFObjectFile &ObjFile) { - std::unique_ptr> Obj; - - if (!OutputFormat.empty() && OutputFormat != "binary") - error("invalid output format '" + OutputFormat + "'"); - if (!OutputFormat.empty() && OutputFormat == "binary") - Obj = llvm::make_unique>(ObjFile); - else - Obj = llvm::make_unique>(ObjFile); - - if (!SplitDWO.empty()) - SplitDWOToFile(ObjFile, SplitDWO.getValue()); +void HandleArgs(Object &Obj, const Reader &Reader) { + + if (!SplitDWO.empty()) { + auto DWOFile = Reader.create(); + DWOFile->removeSections( + [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); }); + auto Writer = CreateWriter(*DWOFile); + Writer->finalize(); + Writer->write(); + } // Localize: if (LocalizeHidden) { - Obj->getSymTab()->localize([](const Symbol &Sym) { + Obj.SymbolTable->localize([](const Symbol &Sym) { return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL; }); } @@ -214,7 +207,7 @@ if (ExtractDWO) RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { - return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec); + return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); }; if (StripAllGNU) @@ -223,7 +216,7 @@ return true; if ((Sec.Flags & SHF_ALLOC) != 0) return false; - if (&Sec == Obj->getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; switch (Sec.Type) { case SHT_SYMTAB: @@ -239,7 +232,6 @@ RemovePred = [RemovePred](const SectionBase &Sec) { return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; }; - Obj->WriteSectionHeaders = false; } if (StripDebug) { @@ -252,7 +244,7 @@ RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { if (RemovePred(Sec)) return true; - if (&Sec == Obj->getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; return (Sec.Flags & SHF_ALLOC) == 0; }; @@ -261,7 +253,7 @@ RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { if (RemovePred(Sec)) return true; - if (&Sec == Obj->getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; if (Sec.Name.startswith(".gnu.warning")) return false; @@ -278,17 +270,15 @@ return false; // Allow all implicit removes. - if (RemovePred(Sec)) { + if (RemovePred(Sec)) return true; - } // Keep special sections. - if (Obj->getSectionHeaderStrTab() == &Sec) { + if (Obj.SectionNames == &Sec) return false; - } - if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) { + if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec) return false; - } + // Remove everything else. return true; }; @@ -305,7 +295,7 @@ }; } - Obj->removeSections(RemovePred); + Obj.removeSections(RemovePred); if (!AddSection.empty()) { for (const auto &Flag : AddSection) { @@ -318,18 +308,25 @@ auto Buf = std::move(*BufOrErr); auto BufPtr = reinterpret_cast(Buf->getBufferStart()); auto BufSize = Buf->getBufferSize(); - Obj->addSection(SecName, ArrayRef(BufPtr, BufSize)); + Obj.addSection(SecName, + ArrayRef(BufPtr, BufSize)); } } if (!AddGnuDebugLink.empty()) { - Obj->addGnuDebugLink(AddGnuDebugLink); + auto Sec = Obj.addSection(StringRef(AddGnuDebugLink)); } +} - Obj->finalize(); - WriteObjectFile(*Obj, OutputFilename.getValue()); +std::unique_ptr CreateReader() { + // Right now we can only read ELF files so there's only one reader; + auto Out = llvm::make_unique(StringRef(InputFilename)); + // We need to set the default ElfType for output. + OutputElfType = Out->getElfType(); + return std::move(Out); } +// TODO: uh...this is complicated. int main(int argc, char **argv) { // Print a stack trace if we signal out. sys::PrintStackTraceOnErrorSignal(argv[0]); @@ -341,25 +338,11 @@ cl::PrintHelpMessage(); return 2; } - Expected> BinaryOrErr = createBinary(InputFilename); - if (!BinaryOrErr) - reportError(InputFilename, BinaryOrErr.takeError()); - Binary &Binary = *BinaryOrErr.get().getBinary(); - if (auto *o = dyn_cast>(&Binary)) { - CopyBinary(*o); - return 0; - } - if (auto *o = dyn_cast>(&Binary)) { - CopyBinary(*o); - return 0; - } - if (auto *o = dyn_cast>(&Binary)) { - CopyBinary(*o); - return 0; - } - if (auto *o = dyn_cast>(&Binary)) { - CopyBinary(*o); - return 0; - } - reportError(InputFilename, object_error::invalid_file_type); + + auto Reader = CreateReader(); + auto Obj = Reader->create(); + auto Writer = CreateWriter(*Obj); + HandleArgs(*Obj, *Reader); + Writer->finalize(); + Writer->write(); }