diff options
Diffstat (limited to 'tools/llvm-objcopy')
-rw-r--r-- | tools/llvm-objcopy/CMakeLists.txt | 16 | ||||
-rw-r--r-- | tools/llvm-objcopy/LLVMBuild.txt | 2 | ||||
-rw-r--r-- | tools/llvm-objcopy/ObjcopyOpts.td | 99 | ||||
-rw-r--r-- | tools/llvm-objcopy/Object.cpp | 1125 | ||||
-rw-r--r-- | tools/llvm-objcopy/Object.h | 513 | ||||
-rw-r--r-- | tools/llvm-objcopy/StripOpts.td | 49 | ||||
-rw-r--r-- | tools/llvm-objcopy/llvm-objcopy.cpp | 675 | ||||
-rw-r--r-- | tools/llvm-objcopy/llvm-objcopy.h | 5 |
8 files changed, 1879 insertions, 605 deletions
diff --git a/tools/llvm-objcopy/CMakeLists.txt b/tools/llvm-objcopy/CMakeLists.txt index 05aa727ab9d8..b0cd66be5b3a 100644 --- a/tools/llvm-objcopy/CMakeLists.txt +++ b/tools/llvm-objcopy/CMakeLists.txt @@ -1,13 +1,29 @@ set(LLVM_LINK_COMPONENTS Object + Option Support MC ) + +set(LLVM_TARGET_DEFINITIONS ObjcopyOpts.td) +tablegen(LLVM ObjcopyOpts.inc -gen-opt-parser-defs) +add_public_tablegen_target(ObjcopyOptsTableGen) + +set(LLVM_TARGET_DEFINITIONS StripOpts.td) +tablegen(LLVM StripOpts.inc -gen-opt-parser-defs) +add_public_tablegen_target(StripOptsTableGen) + add_llvm_tool(llvm-objcopy llvm-objcopy.cpp Object.cpp + DEPENDS + ObjcopyOptsTableGen + StripOptsTableGen ) +add_llvm_tool_symlink(llvm-strip llvm-objcopy) + if(LLVM_INSTALL_BINUTILS_SYMLINKS) add_llvm_tool_symlink(objcopy llvm-objcopy) + add_llvm_tool_symlink(strip llvm-objcopy) endif() diff --git a/tools/llvm-objcopy/LLVMBuild.txt b/tools/llvm-objcopy/LLVMBuild.txt index 0a3473a222b0..a26e13932b21 100644 --- a/tools/llvm-objcopy/LLVMBuild.txt +++ b/tools/llvm-objcopy/LLVMBuild.txt @@ -18,4 +18,4 @@ type = Tool name = llvm-objcopy parent = Tools -required_libraries = Object Support MC +required_libraries = Object Option Support MC diff --git a/tools/llvm-objcopy/ObjcopyOpts.td b/tools/llvm-objcopy/ObjcopyOpts.td new file mode 100644 index 000000000000..2af2108d98d3 --- /dev/null +++ b/tools/llvm-objcopy/ObjcopyOpts.td @@ -0,0 +1,99 @@ +include "llvm/Option/OptParser.td" + +multiclass Eq<string name> { + def NAME: Separate<["--", "-"], name>; + def NAME # _eq: Joined<["--", "-"], name # "=">, Alias<!cast<Separate>(NAME)>; +} + +def help : Flag<["-", "--"], "help">; +defm binary_architecture : Eq<"binary-architecture">, + HelpText<"Used when transforming an architecture-less format (such as binary) to another format">; +def B : JoinedOrSeparate<["-"], "B">, + Alias<binary_architecture>; +defm input_target : Eq<"input-target">, + HelpText<"Format of the input file">, + Values<"binary">; +defm output_target : Eq<"output-target">, + HelpText<"Format of the output file">, + Values<"binary">; +def O : JoinedOrSeparate<["-"], "O">, + Alias<output_target>; +defm split_dwo : Eq<"split-dwo">, + MetaVarName<"dwo-file">, + HelpText<"Equivalent to extract-dwo on the input file to <dwo-file>, then strip-dwo on the input file">; +defm add_gnu_debuglink : Eq<"add-gnu-debuglink">, + MetaVarName<"debug-file">, + HelpText<"Add a .gnu_debuglink for <debug-file>">; +defm remove_section : Eq<"remove-section">, + MetaVarName<"section">, + HelpText<"Remove <section>">; +defm rename_section : Eq<"rename-section">, + MetaVarName<"old=new">, + HelpText<"Renames a section from old to new">; +defm redefine_symbol : Eq<"redefine-sym">, + MetaVarName<"old=new">, + HelpText<"Change the name of a symbol old to new">; +def R : JoinedOrSeparate<["-"], "R">, + Alias<remove_section>; +defm keep : Eq<"keep">, + MetaVarName<"section">, + HelpText<"Keep <section>">; +defm only_keep : Eq<"only-keep">, + MetaVarName<"section">, + HelpText<"Remove all but <section>">; +def j : JoinedOrSeparate<["-"], "j">, + Alias<only_keep>; +defm add_section : Eq<"add-section">, + MetaVarName<"section=file">, + HelpText<"Make a section named <section> with the contents of <file>.">; +def strip_all : Flag<["-", "--"], "strip-all">, + HelpText<"Remove non-allocated sections other than .gnu.warning* sections">; +def strip_all_gnu : Flag<["-", "--"], "strip-all-gnu">, + HelpText<"Compaitable with GNU objcopy's --strip-all">; +def strip_debug : Flag<["-", "--"], "strip-debug">, + HelpText<"Remove all debug information">; +def strip_dwo : Flag<["-", "--"], "strip-dwo">, + HelpText<"Remove all DWARF .dwo sections from file">; +def strip_sections : Flag<["-", "--"], "strip-sections">, + HelpText<"Remove all section headers">; +def strip_non_alloc : Flag<["-", "--"], "strip-non-alloc">, + HelpText<"Remove all non-allocated sections">; +def extract_dwo : Flag<["-", "--"], "extract-dwo">, + HelpText<"Remove all sections that are not DWARF .dwo sections from file">; +def localize_hidden : Flag<["-", "--"], "localize-hidden">, + HelpText<"Mark all symbols that have hidden or internal visibility as local">; +defm localize_symbol : Eq<"localize-symbol">, + MetaVarName<"symbol">, + HelpText<"Mark <symbol> as local">; +def L : JoinedOrSeparate<["-"], "L">, + Alias<localize_symbol>; +defm globalize_symbol : Eq<"globalize-symbol">, + MetaVarName<"symbol">, + HelpText<"Mark <symbol> as global">; +defm weaken_symbol : Eq<"weaken-symbol">, + MetaVarName<"symbol">, + HelpText<"Mark <symbol> as weak">; +def W : JoinedOrSeparate<["-"], "W">, + Alias<weaken_symbol>; +def weaken : Flag<["-", "--"], "weaken">, + HelpText<"Mark all global symbols as weak">; +def discard_all : Flag<["-", "--"], "discard-all">, + HelpText<"Remove all local symbols except file and section symbols">; +def x : Flag<["-"], "x">, + Alias<discard_all>; +defm strip_symbol : Eq<"strip-symbol">, + MetaVarName<"symbol">, + HelpText<"Remove symbol <symbol>">; +def N : JoinedOrSeparate<["-"], "N">, + Alias<strip_symbol>; +defm keep_symbol : Eq<"keep-symbol">, + MetaVarName<"symbol">, + HelpText<"Do not remove symbol <symbol>">; +def K : JoinedOrSeparate<["-"], "K">, + Alias<keep_symbol>; +def only_keep_debug : Flag<["-", "--"], "only-keep-debug">, + HelpText<"Currently ignored. Only for compaitability with GNU objcopy.">; +def strip_unneeded : Flag<["-", "--"], "strip-unneeded">, + HelpText<"Remove all symbols not needed by relocations">; +def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">, + HelpText<"Do not remove file symbols">; diff --git a/tools/llvm-objcopy/Object.cpp b/tools/llvm-objcopy/Object.cpp index d5dfcac40e4e..7e88f5263a39 100644 --- a/tools/llvm-objcopy/Object.cpp +++ b/tools/llvm-objcopy/Object.cpp @@ -18,6 +18,7 @@ #include "llvm/Object/ELFObjectFile.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/Path.h" #include <algorithm> #include <cstddef> #include <cstdint> @@ -26,64 +27,117 @@ #include <vector> using namespace llvm; +using namespace llvm::objcopy; using namespace object; using namespace ELF; -template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const { - using Elf_Ehdr = typename ELFT::Ehdr; - using Elf_Phdr = typename ELFT::Phdr; +Buffer::~Buffer() {} - uint8_t *Buf = Out.getBufferStart(); - Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr); - Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(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 FileBuffer::allocate(size_t Size) { + Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = + FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); + handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) { + error("failed to open " + getName() + ": " + E.message()); + }); + Buf = std::move(*BufferOrErr); +} + +Error FileBuffer::commit() { return Buf->commit(); } + +uint8_t *FileBuffer::getBufferStart() { + return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); } -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); +void MemBuffer::allocate(size_t Size) { + Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName()); +} + +Error MemBuffer::commit() { return Error::success(); } + +uint8_t *MemBuffer::getBufferStart() { + return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); +} + +std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() { + return std::move(Buf); +} + +template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) { + using Elf_Phdr = typename ELFT::Phdr; + + uint8_t *B = Buf.getBufferStart(); + B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr); + Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B); + 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::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {} void SectionBase::initialize(SectionTableRef SecTable) {} void SectionBase::finalize() {} +void SectionBase::markSymbols() {} + +template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) { + uint8_t *B = Buf.getBufferStart(); + B += Sec.HeaderOffset; + typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(B); + 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 <class ELFT> -void SectionBase::writeHeader(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart(); - Buf += HeaderOffset; - typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(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) +SectionVisitor::~SectionVisitor() {} + +void BinarySectionWriter::visit(const SectionIndexSection &Sec) { + error("Cannot write symbol section index table '" + Sec.Name + "' "); +} + +void BinarySectionWriter::visit(const SymbolTableSection &Sec) { + error("Cannot write symbol table '" + Sec.Name + "' out to binary"); +} + +void BinarySectionWriter::visit(const RelocationSection &Sec) { + error("Cannot write relocation section '" + Sec.Name + "' out to binary"); +} + +void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) { + error("Cannot write '" + Sec.Name + "' out to binary"); +} + +void BinarySectionWriter::visit(const GroupSection &Sec) { + error("Cannot write '" + Sec.Name + "' out to binary"); +} + +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); } -void OwnedDataSection::writeSection(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart() + Offset; - std::copy(std::begin(Data), std::end(Data), Buf); +void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } + +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::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); } void StringTableSection::addString(StringRef Name) { @@ -97,8 +151,35 @@ uint32_t StringTableSection::findIndex(StringRef Name) const { void StringTableSection::finalize() { StrTabBuilder.finalize(); } -void StringTableSection::writeSection(FileOutputBuffer &Out) const { - StrTabBuilder.write(Out.getBufferStart() + Offset); +void SectionWriter::visit(const StringTableSection &Sec) { + Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset); +} + +void StringTableSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +template <class ELFT> +void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + auto *IndexesBuffer = reinterpret_cast<typename ELFT::Word *>(Buf); + std::copy(std::begin(Sec.Indexes), std::end(Sec.Indexes), IndexesBuffer); +} + +void SectionIndexSection::initialize(SectionTableRef SecTable) { + Size = 0; + setSymTab(SecTable.getSectionOfType<SymbolTableSection>( + Link, + "Link field value " + Twine(Link) + " in section " + Name + " is invalid", + "Link field value " + Twine(Link) + " in section " + Name + + " is not a symbol table")); + Symbols->setShndxTable(this); +} + +void SectionIndexSection::finalize() { Link = Symbols->Index; } + +void SectionIndexSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); } static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) { @@ -119,8 +200,13 @@ static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) { return false; } +// Large indexes force us to clarify exactly what this function should do. This +// function should return the value that will appear in st_shndx when written +// out. uint16_t Symbol::getShndx() const { if (DefinedIn != nullptr) { + if (DefinedIn->Index >= SHN_LORESERVE) + return SHN_XINDEX; return DefinedIn->Index; } switch (ShndxType) { @@ -134,19 +220,29 @@ uint16_t Symbol::getShndx() const { case SYMBOL_HEXAGON_SCOMMON_2: case SYMBOL_HEXAGON_SCOMMON_4: case SYMBOL_HEXAGON_SCOMMON_8: + case SYMBOL_XINDEX: return static_cast<uint16_t>(ShndxType); } llvm_unreachable("Symbol with invalid ShndxType encountered"); } +void SymbolTableSection::assignIndices() { + uint32_t Index = 0; + for (auto &Sym : Symbols) + Sym->Index = Index++; +} + void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn, uint64_t Value, - uint16_t Shndx, uint64_t Sz) { + uint8_t Visibility, uint16_t Shndx, + uint64_t Sz) { Symbol Sym; Sym.Name = Name; Sym.Binding = Bind; Sym.Type = Type; Sym.DefinedIn = DefinedIn; + if (DefinedIn != nullptr) + DefinedIn->HasSymbol = true; if (DefinedIn == nullptr) { if (Shndx >= SHN_LORESERVE) Sym.ShndxType = static_cast<SymbolShndxType>(Shndx); @@ -154,6 +250,7 @@ void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type, Sym.ShndxType = SYMBOL_SIMPLE_INDEX; } Sym.Value = Value; + Sym.Visibility = Visibility; Sym.Size = Sz; Sym.Index = Symbols.size(); Symbols.emplace_back(llvm::make_unique<Symbol>(Sym)); @@ -161,16 +258,33 @@ void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type, } void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) { + if (SectionIndexTable == Sec) + SectionIndexTable = nullptr; if (SymbolNames == Sec) { error("String table " + SymbolNames->Name + " cannot be removed because it is referenced by the symbol table " + this->Name); } - auto Iter = - std::remove_if(std::begin(Symbols), std::end(Symbols), - [=](const SymPtr &Sym) { return Sym->DefinedIn == Sec; }); - Size -= (std::end(Symbols) - Iter) * this->EntrySize; - Symbols.erase(Iter, std::end(Symbols)); + removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; }); +} + +void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) { + std::for_each(std::begin(Symbols) + 1, std::end(Symbols), + [Callable](SymPtr &Sym) { Callable(*Sym); }); + std::stable_partition( + std::begin(Symbols), std::end(Symbols), + [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; }); + assignIndices(); +} + +void SymbolTableSection::removeSymbols( + function_ref<bool(const Symbol &)> ToRemove) { + Symbols.erase( + std::remove_if(std::begin(Symbols) + 1, std::end(Symbols), + [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }), + std::end(Symbols)); + Size = Symbols.size() * EntrySize; + assignIndices(); } void SymbolTableSection::initialize(SectionTableRef SecTable) { @@ -198,7 +312,17 @@ void SymbolTableSection::finalize() { Info = MaxLocalIndex + 1; } -void SymbolTableSection::addSymbolNames() { +void SymbolTableSection::prepareForLayout() { + // Add all potential section indexes before file layout so that the section + // index section has the approprite size. + if (SectionIndexTable != nullptr) { + for (const auto &Sym : Symbols) { + if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE) + SectionIndexTable->addIndex(Sym->DefinedIn->Index); + else + SectionIndexTable->addIndex(SHN_UNDEF); + } + } // Add all of our strings to SymbolNames so that SymbolNames has the right // size before layout is decided. for (auto &Sym : Symbols) @@ -211,16 +335,22 @@ const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const { return Symbols[Index].get(); } +Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) { + return const_cast<Symbol *>( + static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index)); +} + template <class ELFT> -void SymbolTableSectionImpl<ELFT>::writeSection(FileOutputBuffer &Out) const { +void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) { uint8_t *Buf = Out.getBufferStart(); - Buf += Offset; + Buf += Sec.Offset; typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(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; + Sym->st_other = Symbol->Visibility; Sym->setBinding(Symbol->Binding); Sym->setType(Symbol->Type); Sym->st_shndx = Symbol->getShndx(); @@ -228,13 +358,18 @@ void SymbolTableSectionImpl<ELFT>::writeSection(FileOutputBuffer &Out) const { } } +void SymbolTableSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + template <class SymTabType> void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences( const SectionBase *Sec) { if (Symbols == Sec) { - error("Symbol table " + Symbols->Name + " cannot be removed because it is " - "referenced by the relocation " - "section " + + error("Symbol table " + Symbols->Name + + " cannot be removed because it is " + "referenced by the relocation " + "section " + this->Name); } } @@ -249,9 +384,9 @@ void RelocSectionWithSymtabBase<SymTabType>::initialize( " is not a symbol table")); if (Info != SHN_UNDEF) - setSection(SecTable.getSection(Info, - "Info field value " + Twine(Info) + - " in section " + Name + " is invalid")); + setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) + + " in section " + Name + + " is invalid")); else setSection(nullptr); } @@ -264,16 +399,15 @@ void RelocSectionWithSymtabBase<SymTabType>::finalize() { } template <class ELFT> -void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {} +static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {} template <class ELFT> -void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) { +static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) { Rela.r_addend = Addend; } -template <class ELFT> -template <class T> -void RelocationSection<ELFT>::writeRel(T *Buf) const { +template <class RelRange, class T> +static void writeRel(const RelRange &Relocations, T *Buf) { for (const auto &Reloc : Relocations) { Buf->r_offset = Reloc.Offset; setAddend(*Buf, Reloc.Addend); @@ -283,43 +417,138 @@ void RelocationSection<ELFT>::writeRel(T *Buf) const { } template <class ELFT> -void RelocationSection<ELFT>::writeSection(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart() + Offset; - if (Type == SHT_REL) - writeRel(reinterpret_cast<Elf_Rel *>(Buf)); +void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + if (Sec.Type == SHT_REL) + writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf)); else - writeRel(reinterpret_cast<Elf_Rela *>(Buf)); + writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf)); } -void DynamicRelocationSection::writeSection(FileOutputBuffer &Out) const { - std::copy(std::begin(Contents), std::end(Contents), - Out.getBufferStart() + Offset); +void RelocationSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); } -void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) { - if (StrTab == Sec) { - error("String table " + StrTab->Name + " cannot be removed because it is " - "referenced by the section " + +void RelocationSection::removeSymbols( + function_ref<bool(const Symbol &)> ToRemove) { + for (const Relocation &Reloc : Relocations) + if (ToRemove(*Reloc.RelocSymbol)) + error("not stripping symbol `" + Reloc.RelocSymbol->Name + + "' because it is named in a relocation"); +} + +void RelocationSection::markSymbols() { + for (const Relocation &Reloc : Relocations) + Reloc.RelocSymbol->Referenced = true; +} + +void SectionWriter::visit(const DynamicRelocationSection &Sec) { + std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), + Out.getBufferStart() + Sec.Offset); +} + +void DynamicRelocationSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void Section::removeSectionReferences(const SectionBase *Sec) { + if (LinkSection == Sec) { + error("Section " + LinkSection->Name + + " cannot be removed because it is " + "referenced by the section " + this->Name); } } -bool SectionWithStrTab::classof(const SectionBase *S) { - return isa<DynamicSymbolTableSection>(S) || isa<DynamicSection>(S); +void GroupSection::finalize() { + this->Info = Sym->Index; + this->Link = SymTab->Index; } -void SectionWithStrTab::initialize(SectionTableRef SecTable) { - auto StrTab = SecTable.getSection(Link, - "Link field value " + Twine(Link) + - " in section " + Name + " is invalid"); - if (StrTab->Type != SHT_STRTAB) { - error("Link field value " + Twine(Link) + " in section " + Name + - " is not a string table"); +void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { + if (ToRemove(*Sym)) { + error("Symbol " + Sym->Name + + " cannot be removed because it is " + "referenced by the section " + + this->Name + "[" + Twine(this->Index) + "]"); } - setStrTab(StrTab); } -void SectionWithStrTab::finalize() { this->Link = StrTab->Index; } +void GroupSection::markSymbols() { + if (Sym) + Sym->Referenced = true; +} + +void Section::initialize(SectionTableRef SecTable) { + if (Link != ELF::SHN_UNDEF) { + LinkSection = + SecTable.getSection(Link, "Link field value " + Twine(Link) + + " in section " + Name + " is invalid"); + if (LinkSection->Type == ELF::SHT_SYMTAB) + LinkSection = nullptr; + } +} + +void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; } + +void GnuDebugLinkSection::init(StringRef File, StringRef Data) { + FileName = sys::path::filename(File); + // The format for the .gnu_debuglink starts with the file name and is + // followed by a null terminator and then the CRC32 of the file. The CRC32 + // should be 4 byte aligned. So we add the FileName size, a 1 for the null + // byte, and then finally push the size to alignment and add 4. + Size = alignTo(FileName.size() + 1, 4) + 4; + // The CRC32 will only be aligned if we align the whole section. + Align = 4; + Type = ELF::SHT_PROGBITS; + Name = ".gnu_debuglink"; + // For sections not found in segments, OriginalOffset is only used to + // establish the order that sections should go in. By using the maximum + // possible offset we cause this section to wind up at the end. + OriginalOffset = std::numeric_limits<uint64_t>::max(); + JamCRC crc; + crc.update(ArrayRef<char>(Data.data(), Data.size())); + // The CRC32 value needs to be complemented because the JamCRC dosn't + // finalize the CRC32 value. It also dosn't negate the initial CRC32 value + // but it starts by default at 0xFFFFFFFF which is the complement of zero. + CRC32 = ~crc.getCRC(); +} + +GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) { + // Read in the file to compute the CRC of it. + auto DebugOrErr = MemoryBuffer::getFile(File); + if (!DebugOrErr) + error("'" + File + "': " + DebugOrErr.getError().message()); + auto Debug = std::move(*DebugOrErr); + init(File, Debug->getBuffer()); +} + +template <class ELFT> +void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) { + auto Buf = Out.getBufferStart() + Sec.Offset; + char *File = reinterpret_cast<char *>(Buf); + Elf_Word *CRC = + reinterpret_cast<Elf_Word *>(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); +} + +template <class ELFT> +void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) { + ELF::Elf32_Word *Buf = + reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset); + *Buf++ = Sec.FlagWord; + for (const auto *S : Sec.GroupMembers) + support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index); +} + +void GroupSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} // Returns true IFF a section is wholly inside the range of a segment static bool sectionWithinSegment(const SectionBase &Section, @@ -342,7 +571,7 @@ static bool segmentOverlapsSegment(const Segment &Child, Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset; } -static bool compareSegments(const Segment *A, const Segment *B) { +static bool compareSegmentsByOffset(const Segment *A, const Segment *B) { // Any segment without a parent segment should come before a segment // that has a parent segment. if (A->OriginalOffset < B->OriginalOffset) @@ -352,14 +581,36 @@ static bool compareSegments(const Segment *A, const Segment *B) { return A->Index < B->Index; } -template <class ELFT> -void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) { +static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) { + if (A->PAddr < B->PAddr) + return true; + if (A->PAddr > B->PAddr) + return false; + return A->Index < B->Index; +} + +template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) { + 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)) { + // We want a canonical "most parental" segment but this requires + // inspecting the ParentSegment. + if (compareSegmentsByOffset(&Parent, &Child)) + if (Child.ParentSegment == nullptr || + compareSegmentsByOffset(&Parent, Child.ParentSegment)) { + Child.ParentSegment = &Parent; + } + } + } +} + +template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() { uint32_t Index = 0; for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) { ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset, (size_t)Phdr.p_filesz}; - Segments.emplace_back(llvm::make_unique<Segment>(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; @@ -370,62 +621,128 @@ void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) { 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; } } } } + + auto &ElfHdr = Obj.ElfHdrSegment; + // Creating multiple PT_PHDR segments technically is not valid, but PT_LOAD + // segments must not overlap, and other types fit even less. + ElfHdr.Type = PT_PHDR; + ElfHdr.Flags = 0; + ElfHdr.OriginalOffset = ElfHdr.Offset = 0; + ElfHdr.VAddr = 0; + ElfHdr.PAddr = 0; + ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr); + ElfHdr.Align = 0; + ElfHdr.Index = Index++; + + const auto &Ehdr = *ElfFile.getHeader(); + auto &PrHdr = Obj.ProgramHdrSegment; + PrHdr.Type = PT_PHDR; + PrHdr.Flags = 0; + // The spec requires us to have p_vaddr % p_align == p_offset % p_align. + // Whereas this works automatically for ElfHdr, here OriginalOffset is + // always non-zero and to ensure the equation we assign the same value to + // VAddr as well. + PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff; + PrHdr.PAddr = 0; + PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum; + // The spec requires us to naturally align all the fields. + PrHdr.Align = sizeof(Elf_Addr); + PrHdr.Index = Index++; + // 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) { - // 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)) { - // 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(); - } - } - } + for (auto &Child : Obj.segments()) + setParentSegment(Child); + setParentSegment(ElfHdr); + setParentSegment(PrHdr); +} + +template <class ELFT> +void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) { + auto SecTable = Obj.sections(); + auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>( + GroupSec->Link, + "Link field value " + Twine(GroupSec->Link) + " in section " + + GroupSec->Name + " is invalid", + "Link field value " + Twine(GroupSec->Link) + " in section " + + GroupSec->Name + " is not a symbol table"); + auto Sym = SymTab->getSymbolByIndex(GroupSec->Info); + if (!Sym) + error("Info field value " + Twine(GroupSec->Info) + " in section " + + GroupSec->Name + " is not a valid symbol index"); + GroupSec->setSymTab(SymTab); + GroupSec->setSymbol(Sym); + if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) || + GroupSec->Contents.empty()) + error("The content of the section " + GroupSec->Name + " is malformed"); + const ELF::Elf32_Word *Word = + reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data()); + const ELF::Elf32_Word *End = + Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word); + GroupSec->setFlagWord(*Word++); + for (; Word != End; ++Word) { + uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word); + GroupSec->addMember(SecTable.getSection( + Index, "Group member index " + Twine(Index) + " in section " + + GroupSec->Name + " is invalid")); } } template <class ELFT> -void Object<ELFT>::initSymbolTable(const object::ELFFile<ELFT> &ElfFile, - SymbolTableSection *SymTab, - SectionTableRef SecTable) { +void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) { const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index)); StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr)); + ArrayRef<Elf_Word> ShndxData; - for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) { + auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr)); + for (const auto &Sym : Symbols) { SectionBase *DefSection = nullptr; StringRef Name = unwrapOrError(Sym.getName(StrTabData)); - if (Sym.st_shndx >= SHN_LORESERVE) { - if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) { + if (Sym.st_shndx == SHN_XINDEX) { + if (SymTab->getShndxTable() == nullptr) + error("Symbol '" + Name + + "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists."); + if (ShndxData.data() == nullptr) { + const Elf_Shdr &ShndxSec = + *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index)); + ShndxData = unwrapOrError( + ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec)); + if (ShndxData.size() != Symbols.size()) + error("Symbol section index table does not have the same number of " + "entries as the symbol table."); + } + Elf_Word Index = ShndxData[&Sym - Symbols.begin()]; + DefSection = Obj.sections().getSection( + Index, + "Symbol '" + Name + "' has invalid section index " + + Twine(Index)); + } else if (Sym.st_shndx >= SHN_LORESERVE) { + 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( - Sym.st_shndx, - "Symbol '" + Name + "' is defined in invalid section with index " + - Twine(Sym.st_shndx)); + DefSection = Obj.sections().getSection( + Sym.st_shndx, "Symbol '" + Name + + "' is defined has invalid section index " + + Twine(Sym.st_shndx)); } SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection, - Sym.getValue(), Sym.st_shndx, Sym.st_size); + Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size); } } @@ -437,9 +754,9 @@ static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) { ToSet = Rela.r_addend; } -template <class ELFT, class T> -void initRelocations(RelocationSection<ELFT> *Relocs, - SymbolTableSection *SymbolTable, T RelRange) { +template <class T> +static void initRelocations(RelocationSection *Relocs, + SymbolTableSection *SymbolTable, T RelRange) { for (const auto &Rel : RelRange) { Relocation ToAdd; ToAdd.Offset = Rel.r_offset; @@ -450,14 +767,14 @@ void initRelocations(RelocationSection<ELFT> *Relocs, } } -SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) { +SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) { if (Index == SHN_UNDEF || Index > Sections.size()) error(ErrMsg); return Sections[Index - 1].get(); } template <class T> -T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg, +T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg) { if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg))) return Sec; @@ -465,147 +782,221 @@ T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg, } template <class ELFT> -std::unique_ptr<SectionBase> -Object<ELFT>::makeSection(const object::ELFFile<ELFT> &ElfFile, - const Elf_Shdr &Shdr) { +SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) { ArrayRef<uint8_t> 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<DynamicRelocationSection>(Data); + return Obj.addSection<DynamicRelocationSection>(Data); } - return llvm::make_unique<RelocationSection<ELFT>>(); + return Obj.addSection<RelocationSection>(); 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<Section>(Data); + return Obj.addSection<Section>(Data); } - return llvm::make_unique<StringTableSection>(); + return Obj.addSection<StringTableSection>(); 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<Section>(Data); + return Obj.addSection<Section>(Data); + case SHT_GROUP: + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return Obj.addSection<GroupSection>(Data); case SHT_DYNSYM: Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique<DynamicSymbolTableSection>(Data); + return Obj.addSection<DynamicSymbolTableSection>(Data); case SHT_DYNAMIC: Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique<DynamicSection>(Data); + return Obj.addSection<DynamicSection>(Data); case SHT_SYMTAB: { - auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>(); - SymbolTable = SymTab.get(); - return std::move(SymTab); + auto &SymTab = Obj.addSection<SymbolTableSection>(); + Obj.SymbolTable = &SymTab; + return SymTab; + } + case SHT_SYMTAB_SHNDX: { + auto &ShndxSection = Obj.addSection<SectionIndexSection>(); + Obj.SectionIndexTable = &ShndxSection; + return ShndxSection; } case SHT_NOBITS: - return llvm::make_unique<Section>(Data); + return Obj.addSection<Section>(Data); default: Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); - return llvm::make_unique<Section>(Data); + return Obj.addSection<Section>(Data); } } -template <class ELFT> -SectionTableRef Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) { +template <class ELFT> void ELFBuilder<ELFT>::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++; + } + + // If a section index table exists we'll need to initialize it before we + // initialize the symbol table because the symbol table might need to + // reference it. + if (Obj.SectionIndexTable) + Obj.SectionIndexTable->initialize(Obj.sections()); // 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<RelocationSection<ELFT>>(Section.get())) { + Section.initialize(Obj.sections()); + if (auto RelSec = dyn_cast<RelocationSection>(&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))); + } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) { + initGroupSection(GroupSec); } } - - return SecTable; } -template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) { - const auto &ElfFile = *Obj.getELFFile(); +template <class ELFT> void ELFBuilder<ELFT>::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; + 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(); + + uint32_t ShstrIndex = Ehdr.e_shstrndx; + if (ShstrIndex == SHN_XINDEX) + ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link; + + Obj.SectionNames = + Obj.sections().template getSectionOfType<StringTableSection>( + ShstrIndex, + "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"); +} + +// A generic size function which computes sizes of any random access range. +template <class R> size_t size(R &&Range) { + return static_cast<size_t>(std::end(Range) - std::begin(Range)); +} + +Writer::~Writer() {} - SectionTableRef SecTable = readSectionHeaders(ElfFile); - readProgramHeaders(ElfFile); +Reader::~Reader() {} - SectionNames = SecTable.getSectionOfType<StringTableSection>( - 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"); +ElfType ELFReader::getElfType() const { + if (isa<ELFObjectFile<ELF32LE>>(Bin)) + return ELFT_ELF32LE; + if (isa<ELFObjectFile<ELF64LE>>(Bin)) + return ELFT_ELF64LE; + if (isa<ELFObjectFile<ELF32BE>>(Bin)) + return ELFT_ELF32BE; + if (isa<ELFObjectFile<ELF64BE>>(Bin)) + return ELFT_ELF64BE; + llvm_unreachable("Invalid ELFType"); } -template <class ELFT> -void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart(); - Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(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; - Ehdr.e_phoff = sizeof(Elf_Ehdr); - Ehdr.e_flags = Flags; +std::unique_ptr<Object> ELFReader::create() const { + auto Obj = llvm::make_unique<Object>(); + if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) { + ELFBuilder<ELF32LE> Builder(*o, *Obj); + Builder.build(); + return Obj; + } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) { + ELFBuilder<ELF64LE> Builder(*o, *Obj); + Builder.build(); + return Obj; + } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) { + ELFBuilder<ELF32BE> Builder(*o, *Obj); + Builder.build(); + return Obj; + } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) { + ELFBuilder<ELF64BE> Builder(*o, *Obj); + Builder.build(); + return Obj; + } + error("Invalid file type"); +} + +template <class ELFT> void ELFWriter<ELFT>::writeEhdr() { + uint8_t *B = Buf.getBufferStart(); + Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B); + 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 = Obj.ProgramHdrSegment.Offset; + 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; + // """ + // If the number of sections is greater than or equal to + // SHN_LORESERVE (0xff00), this member has the value zero and the actual + // number of section header table entries is contained in the sh_size field + // of the section header at index 0. + // """ + auto Shnum = size(Obj.sections()) + 1; + if (Shnum >= SHN_LORESERVE) + Ehdr.e_shnum = 0; + else + Ehdr.e_shnum = Shnum; + // """ + // If the section name string table section index is greater than or equal + // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff) + // and the actual index of the section name string table section is + // contained in the sh_link field of the section header at index 0. + // """ + if (Obj.SectionNames->Index >= SHN_LORESERVE) + Ehdr.e_shstrndx = SHN_XINDEX; + else + Ehdr.e_shstrndx = Obj.SectionNames->Index; } else { Ehdr.e_shoff = 0; Ehdr.e_shnum = 0; @@ -613,42 +1004,46 @@ void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const { } } -template <class ELFT> -void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const { - for (auto &Phdr : Segments) - Phdr->template writeHeader<ELFT>(Out); +template <class ELFT> void ELFWriter<ELFT>::writePhdrs() { + for (auto &Seg : Obj.segments()) + writePhdr(Seg); } -template <class ELFT> -void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const { - uint8_t *Buf = Out.getBufferStart() + SHOffset; +template <class ELFT> void ELFWriter<ELFT>::writeShdrs() { + uint8_t *B = Buf.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<Elf_Shdr *>(Buf); + Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B); Shdr.sh_name = 0; Shdr.sh_type = SHT_NULL; Shdr.sh_flags = 0; Shdr.sh_addr = 0; Shdr.sh_offset = 0; - Shdr.sh_size = 0; - Shdr.sh_link = 0; + // See writeEhdr for why we do this. + uint64_t Shnum = size(Obj.sections()) + 1; + if (Shnum >= SHN_LORESERVE) + Shdr.sh_size = Shnum; + else + Shdr.sh_size = 0; + // See writeEhdr for why we do this. + if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE) + Shdr.sh_link = Obj.SectionNames->Index; + else + Shdr.sh_link = 0; Shdr.sh_info = 0; Shdr.sh_addralign = 0; Shdr.sh_entsize = 0; - for (auto &Section : Sections) - Section->template writeHeader<ELFT>(Out); + for (auto &Sec : Obj.sections()) + writeShdr(Sec); } -template <class ELFT> -void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const { - for (auto &Section : Sections) - Section->writeSection(Out); +template <class ELFT> void ELFWriter<ELFT>::writeSectionData() { + for (auto &Sec : Obj.sections()) + Sec.accept(*SecWriter); } -template <class ELFT> -void Object<ELFT>::removeSections( - std::function<bool(const SectionBase &)> ToRemove) { +void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) { auto Iter = std::stable_partition( std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) { @@ -662,12 +1057,10 @@ void Object<ELFT>::removeSections( }); 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; - } + if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable)) + SectionIndexTable = nullptr; // Now make sure there are no remaining references to the sections that will // be removed. Sometimes it is impossible to remove a reference so we emit // an error here instead. @@ -681,14 +1074,15 @@ void Object<ELFT>::removeSections( Sections.erase(Iter, std::end(Sections)); } -template <class ELFT> -void Object<ELFT>::addSection(StringRef SecName, ArrayRef<uint8_t> Data) { - auto Sec = llvm::make_unique<OwnedDataSection>(SecName, Data); - Sec->OriginalOffset = ~0ULL; - Sections.push_back(std::move(Sec)); +void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { + if (!SymbolTable) + return; + + for (const SecPtr &Sec : Sections) + Sec->removeSymbols(ToRemove); } -template <class ELFT> void ELFObject<ELFT>::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) { @@ -713,7 +1107,8 @@ static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) { // Orders segments such that if x = y->ParentSegment then y comes before x. static void OrderSegments(std::vector<Segment *> &Segments) { - std::stable_sort(std::begin(Segments), std::end(Segments), compareSegments); + std::stable_sort(std::begin(Segments), std::end(Segments), + compareSegmentsByOffset); } // This function finds a consistent layout for a list of segments starting from @@ -722,7 +1117,7 @@ static void OrderSegments(std::vector<Segment *> &Segments) { static uint64_t LayoutSegments(std::vector<Segment *> &Segments, uint64_t Offset) { assert(std::is_sorted(std::begin(Segments), std::end(Segments), - compareSegments)); + compareSegmentsByOffset)); // The only way a segment should move is if a section was between two // segments and that section was removed. If that section isn't in a segment // then it's acceptable, but not ideal, to simply move it to after the @@ -752,8 +1147,8 @@ static uint64_t LayoutSegments(std::vector<Segment *> &Segments, // 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 <class SecPtr> -static uint64_t LayoutSections(std::vector<SecPtr> &Sections, uint64_t Offset) { +template <class Range> +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 @@ -762,106 +1157,154 @@ static uint64_t LayoutSections(std::vector<SecPtr> &Sections, uint64_t Offset) { // 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 <class ELFT> void ELFObject<ELFT>::assignOffsets() { +template <class ELFT> void ELFWriter<ELFT>::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<Segment *> OrderedSegments; - for (auto &Segment : this->Segments) - OrderedSegments.push_back(Segment.get()); + for (auto &Segment : Obj.segments()) + OrderedSegments.push_back(&Segment); + OrderedSegments.push_back(&Obj.ElfHdrSegment); + OrderedSegments.push_back(&Obj.ProgramHdrSegment); 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 - // outputting sections. This covers both the standard case and the PT_PHDR - // case. - uint64_t Offset; - if (!OrderedSegments.empty()) { - Offset = OrderedSegments[0]->Offset; - } else { - Offset = sizeof(Elf_Ehdr); - } + // Offset is used as the start offset of the first segment to be laid out. + // Since the ELF Header (ElfHdrSegment) must be at the start of the file, + // we start at offset 0. + uint64_t Offset = 0; 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 <class ELFT> size_t ELFObject<ELFT>::totalSize() const { +template <class ELFT> size_t ELFWriter<ELFT>::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 <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const { - this->writeHeader(Out); - this->writeProgramHeaders(Out); - this->writeSectionData(Out); - if (this->WriteSectionHeaders) - this->writeSectionHeaders(Out); +template <class ELFT> void ELFWriter<ELFT>::write() { + writeEhdr(); + writePhdrs(); + writeSectionData(); + if (WriteSectionHeaders) + writeShdrs(); + if (auto E = Buf.commit()) + reportError(Buf.getName(), errorToErrorCode(std::move(E))); } -template <class ELFT> void ELFObject<ELFT>::finalize() { - // 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); +template <class ELFT> void ELFWriter<ELFT>::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."); + + Obj.sortSections(); + + // We need to assign indexes before we perform layout because we need to know + // if we need large indexes or not. We can assign indexes first and check as + // we go to see if we will actully need large indexes. + bool NeedsLargeIndexes = false; + if (size(Obj.sections()) >= SHN_LORESERVE) { + auto Sections = Obj.sections(); + NeedsLargeIndexes = + std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(), + [](const SectionBase &Sec) { return Sec.HasSymbol; }); + // TODO: handle case where only one section needs the large index table but + // only needs it because the large index table hasn't been removed yet. + } + + if (NeedsLargeIndexes) { + // This means we definitely need to have a section index table but if we + // already have one then we should use it instead of making a new one. + if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) { + // Addition of a section to the end does not invalidate the indexes of + // other sections and assigns the correct index to the new section. + auto &Shndx = Obj.addSection<SectionIndexSection>(); + Obj.SymbolTable->setShndxTable(&Shndx); + Shndx.setSymTab(Obj.SymbolTable); + } + } else { + // Since we don't need SectionIndexTable we should remove it and all + // references to it. + if (Obj.SectionIndexTable != nullptr) { + Obj.removeSections([this](const SectionBase &Sec) { + return &Sec == Obj.SectionIndexTable; + }); + } + } + + // Make sure we add the names of all the sections. Importantly this must be + // done after we decide to add or remove SectionIndexes. + 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(); - sortSections(); + // Before we can prepare for layout the indexes need to be finalized. + uint64_t Index = 0; + for (auto &Sec : Obj.sections()) + Sec.Index = Index++; + + // The symbol table does not update all other sections on update. For + // instance, symbol names are not added as new symbols are added. This means + // that some sections, like .strtab, don't yet have their final size. + if (Obj.SymbolTable != nullptr) + Obj.SymbolTable->prepareForLayout(); + 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 <class ELFT> size_t BinaryObject<ELFT>::totalSize() const { - return TotalSize; + Buf.allocate(totalSize()); + SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf); } -template <class ELFT> -void BinaryObject<ELFT>::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); } + if (auto E = Buf.commit()) + reportError(Buf.getName(), errorToErrorCode(std::move(E))); } -template <class ELFT> void BinaryObject<ELFT>::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. @@ -870,13 +1313,25 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() { // 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<Segment *> 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); + + // For binary output, we're going to use physical addresses instead of + // virtual addresses, since a binary output is used for cases like ROM + // loading and physical addresses are intended for ROM loading. + // However, if no segment has a physical address, we'll fallback to using + // virtual addresses for all. + if (std::all_of(std::begin(OrderedSegments), std::end(OrderedSegments), + [](const Segment *Segment) { return Segment->PAddr == 0; })) + for (const auto &Segment : OrderedSegments) + Segment->PAddr = Segment->VAddr; + + std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments), + compareSegmentsByPAddr); + // Because we add a ParentSegment for each section we might have duplicate // segments in OrderedSegments. If there were duplicates then LayoutSegments // would do very strange things. @@ -884,6 +1339,8 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() { std::unique(std::begin(OrderedSegments), std::end(OrderedSegments)); OrderedSegments.erase(End, std::end(OrderedSegments)); + uint64_t Offset = 0; + // Modify the first segment so that there is no gap at the start. This allows // our layout algorithm to proceed as expected while not out writing out the // gap at the start. @@ -892,30 +1349,29 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() { auto Sec = Seg->firstSection(); auto Diff = Sec->OriginalOffset - Seg->OriginalOffset; Seg->OriginalOffset += Diff; - // The size needs to be shrunk as well + // The size needs to be shrunk as well. Seg->FileSize -= Diff; - Seg->MemSize -= Diff; - // The VAddr needs to be adjusted so that the alignment is correct as well - Seg->VAddr += Diff; - Seg->PAddr = Seg->VAddr; - // We don't want this to be shifted by alignment so we need to set the - // alignment to zero. - Seg->Align = 0; + // The PAddr needs to be increased to remove the gap before the first + // section. + Seg->PAddr += Diff; + uint64_t LowestPAddr = Seg->PAddr; + for (auto &Segment : OrderedSegments) { + Segment->Offset = Segment->PAddr - LowestPAddr; + Offset = std::max(Offset, Segment->Offset + Segment->FileSize); + } } - uint64_t Offset = LayoutSegments(OrderedSegments, 0); - // TODO: generalize LayoutSections to take a range. Pass a special range // constructed from an iterator that skips values for which a predicate does // not hold. Then pass such a range to LayoutSections instead of constructing // AllocatedSections here. std::vector<SectionBase *> 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 @@ -926,23 +1382,22 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() { if (Section->Type != SHT_NOBITS) TotalSize = std::max(TotalSize, Section->Offset + Section->Size); } + + Buf.allocate(TotalSize); + SecWriter = llvm::make_unique<BinarySectionWriter>(Buf); } namespace llvm { - -template class Object<ELF64LE>; -template class Object<ELF64BE>; -template class Object<ELF32LE>; -template class Object<ELF32BE>; - -template class ELFObject<ELF64LE>; -template class ELFObject<ELF64BE>; -template class ELFObject<ELF32LE>; -template class ELFObject<ELF32BE>; - -template class BinaryObject<ELF64LE>; -template class BinaryObject<ELF64BE>; -template class BinaryObject<ELF32LE>; -template class BinaryObject<ELF32BE>; - +namespace objcopy { + +template class ELFBuilder<ELF64LE>; +template class ELFBuilder<ELF64BE>; +template class ELFBuilder<ELF32LE>; +template class ELFBuilder<ELF32BE>; + +template class ELFWriter<ELF64LE>; +template class ELFWriter<ELF64BE>; +template class ELFWriter<ELF32LE>; +template class ELFWriter<ELF32BE>; +} // end namespace objcopy } // end namespace llvm diff --git a/tools/llvm-objcopy/Object.h b/tools/llvm-objcopy/Object.h index b04b0c1a6415..76748d5fc641 100644 --- a/tools/llvm-objcopy/Object.h +++ b/tools/llvm-objcopy/Object.h @@ -16,6 +16,8 @@ #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 <cstddef> #include <cstdint> #include <functional> @@ -24,24 +26,209 @@ #include <vector> namespace llvm { +namespace objcopy { -class FileOutputBuffer; +class Buffer; class SectionBase; +class Section; +class OwnedDataSection; +class StringTableSection; +class SymbolTableSection; +class RelocationSection; +class DynamicRelocationSection; +class GnuDebugLinkSection; +class GroupSection; +class SectionIndexSection; class Segment; +class Object; +struct Symbol; class SectionTableRef { -private: - ArrayRef<std::unique_ptr<SectionBase>> Sections; + MutableArrayRef<std::unique_ptr<SectionBase>> Sections; public: - SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs) + using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>; + + explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs) : Sections(Secs) {} SectionTableRef(const SectionTableRef &) = default; - SectionBase *getSection(uint16_t Index, Twine ErrMsg); + iterator begin() { return iterator(Sections.data()); } + iterator end() { return iterator(Sections.data() + Sections.size()); } + + SectionBase *getSection(uint32_t Index, Twine ErrMsg); template <class T> - T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg); + T *getSectionOfType(uint32_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; + virtual void visit(const GroupSection &Sec) = 0; + virtual void visit(const SectionIndexSection &Sec) = 0; +}; + +class SectionWriter : public SectionVisitor { +protected: + Buffer &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 DynamicRelocationSection &Sec) override; + virtual void visit(const SymbolTableSection &Sec) override = 0; + virtual void visit(const RelocationSection &Sec) override = 0; + virtual void visit(const GnuDebugLinkSection &Sec) override = 0; + virtual void visit(const GroupSection &Sec) override = 0; + virtual void visit(const SectionIndexSection &Sec) override = 0; + + explicit SectionWriter(Buffer &Buf) : Out(Buf) {} +}; + +template <class ELFT> class ELFSectionWriter : public SectionWriter { +private: + using Elf_Word = typename ELFT::Word; + using Elf_Rel = typename ELFT::Rel; + using Elf_Rela = typename ELFT::Rela; + +public: + virtual ~ELFSectionWriter() {} + void visit(const SymbolTableSection &Sec) override; + void visit(const RelocationSection &Sec) override; + void visit(const GnuDebugLinkSection &Sec) override; + void visit(const GroupSection &Sec) override; + void visit(const SectionIndexSection &Sec) override; + + explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {} +}; + +#define MAKE_SEC_WRITER_FRIEND \ + friend class SectionWriter; \ + template <class ELFT> friend class ELFSectionWriter; + +class BinarySectionWriter : public SectionWriter { +public: + virtual ~BinarySectionWriter() {} + + void visit(const SymbolTableSection &Sec) override; + void visit(const RelocationSection &Sec) override; + void visit(const GnuDebugLinkSection &Sec) override; + void visit(const GroupSection &Sec) override; + void visit(const SectionIndexSection &Sec) override; + + explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {} +}; + +// The class Buffer abstracts out the common interface of FileOutputBuffer and +// WritableMemoryBuffer so that the hierarchy of Writers depends on this +// abstract interface and doesn't depend on a particular implementation. +// TODO: refactor the buffer classes in LLVM to enable us to use them here +// directly. +class Buffer { + StringRef Name; + +public: + virtual ~Buffer(); + virtual void allocate(size_t Size) = 0; + virtual uint8_t *getBufferStart() = 0; + virtual Error commit() = 0; + + explicit Buffer(StringRef Name) : Name(Name) {} + StringRef getName() const { return Name; } +}; + +class FileBuffer : public Buffer { + std::unique_ptr<FileOutputBuffer> Buf; + +public: + void allocate(size_t Size) override; + uint8_t *getBufferStart() override; + Error commit() override; + + explicit FileBuffer(StringRef FileName) : Buffer(FileName) {} +}; + +class MemBuffer : public Buffer { + std::unique_ptr<WritableMemoryBuffer> Buf; + +public: + void allocate(size_t Size) override; + uint8_t *getBufferStart() override; + Error commit() override; + + explicit MemBuffer(StringRef Name) : Buffer(Name) {} + + std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer(); +}; + +class Writer { +protected: + Object &Obj; + Buffer &Buf; + +public: + virtual ~Writer(); + virtual void finalize() = 0; + virtual void write() = 0; + + Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {} +}; + +template <class ELFT> 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(); + + std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter; + + size_t totalSize() const; + +public: + virtual ~ELFWriter() {} + bool WriteSectionHeaders = true; + + void finalize() override; + void write() override; + ELFWriter(Object &Obj, Buffer &Buf, bool WSH) + : Writer(Obj, Buf), WriteSectionHeaders(WSH) {} +}; + +class BinaryWriter : public Writer { +private: + std::unique_ptr<BinarySectionWriter> SecWriter; + + uint64_t TotalSize; + +public: + ~BinaryWriter() {} + void finalize() override; + void write() override; + BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {} }; class SectionBase { @@ -49,8 +236,9 @@ public: StringRef Name; Segment *ParentSegment = nullptr; uint64_t HeaderOffset; - uint64_t OriginalOffset; + uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max(); uint32_t Index; + bool HasSymbol = false; uint64_t Addr = 0; uint64_t Align = 1; @@ -68,8 +256,9 @@ public: virtual void initialize(SectionTableRef SecTable); virtual void finalize(); virtual void removeSectionReferences(const SectionBase *Sec); - template <class ELFT> void writeHeader(FileOutputBuffer &Out) const; - virtual void writeSection(FileOutputBuffer &Out) const = 0; + virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove); + virtual void accept(SectionVisitor &Visitor) const = 0; + virtual void markSymbols(); }; class Segment { @@ -102,7 +291,8 @@ public: uint64_t OriginalOffset; Segment *ParentSegment = nullptr; - Segment(ArrayRef<uint8_t> Data) : Contents(Data) {} + explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {} + Segment() {} const SectionBase *firstSection() const { if (!Sections.empty()) @@ -112,22 +302,26 @@ public: void removeSection(const SectionBase *Sec) { Sections.erase(Sec); } void addSection(const SectionBase *Sec) { Sections.insert(Sec); } - template <class ELFT> void writeHeader(FileOutputBuffer &Out) const; - void writeSegment(FileOutputBuffer &Out) const; }; class Section : public SectionBase { -private: + MAKE_SEC_WRITER_FRIEND + ArrayRef<uint8_t> Contents; + SectionBase *LinkSection = nullptr; public: - Section(ArrayRef<uint8_t> Data) : Contents(Data) {} + explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {} - void writeSection(FileOutputBuffer &Out) const override; + void accept(SectionVisitor &Visitor) const override; + void removeSectionReferences(const SectionBase *Sec) override; + void initialize(SectionTableRef SecTable) override; + void finalize() override; }; class OwnedDataSection : public SectionBase { -private: + MAKE_SEC_WRITER_FRIEND + std::vector<uint8_t> Data; public: @@ -136,8 +330,10 @@ public: Name = SecName; Type = ELF::SHT_PROGBITS; Size = Data.size(); + OriginalOffset = std::numeric_limits<uint64_t>::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. @@ -149,7 +345,8 @@ public: // 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 { -private: + MAKE_SEC_WRITER_FRIEND + StringTableBuilder StrTabBuilder; public: @@ -160,7 +357,7 @@ public: 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) @@ -181,6 +378,7 @@ enum SymbolShndxType { SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2, SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4, SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8, + SYMBOL_XINDEX = ELF::SHN_XINDEX, }; struct Symbol { @@ -193,41 +391,80 @@ struct Symbol { uint64_t Size; uint8_t Type; uint64_t Value; + uint8_t Visibility; + bool Referenced = false; uint16_t getShndx() const; }; +class SectionIndexSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + +private: + std::vector<uint32_t> Indexes; + SymbolTableSection *Symbols = nullptr; + +public: + virtual ~SectionIndexSection() {} + void addIndex(uint32_t Index) { + Indexes.push_back(Index); + Size += 4; + } + void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; } + void initialize(SectionTableRef SecTable) override; + void finalize() override; + void accept(SectionVisitor &Visitor) const override; + + SectionIndexSection() { + Name = ".symtab_shndx"; + Align = 4; + EntrySize = 4; + Type = ELF::SHT_SYMTAB_SHNDX; + } +}; + class SymbolTableSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } + void assignIndices(); + protected: std::vector<std::unique_ptr<Symbol>> Symbols; StringTableSection *SymbolNames = nullptr; + SectionIndexSection *SectionIndexTable = nullptr; using SymPtr = std::unique_ptr<Symbol>; public: - void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type, - SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx, - uint64_t Sz); - void addSymbolNames(); + SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility, + uint16_t Shndx, uint64_t Sz); + void prepareForLayout(); + // An 'empty' symbol table still contains a null symbol. + bool empty() const { return Symbols.size() == 1; } + void setShndxTable(SectionIndexSection *ShndxTable) { + SectionIndexTable = ShndxTable; + } + const SectionIndexSection *getShndxTable() const { return SectionIndexTable; } const SectionBase *getStrTab() const { return SymbolNames; } const Symbol *getSymbolByIndex(uint32_t Index) const; + Symbol *getSymbolByIndex(uint32_t Index); + void updateSymbols(function_ref<void(Symbol &)> Callable); + void removeSectionReferences(const SectionBase *Sec) override; void initialize(SectionTableRef SecTable) override; void finalize() override; + void accept(SectionVisitor &Visitor) const override; + void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) 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 ELFT> class SymbolTableSectionImpl : public SymbolTableSection { - void writeSection(FileOutputBuffer &Out) const override; -}; - struct Relocation { - const Symbol *RelocSymbol = nullptr; + Symbol *RelocSymbol = nullptr; uint64_t Offset; uint64_t Addend; uint32_t Type; @@ -259,33 +496,29 @@ public: // that code between the two symbol table types. template <class SymTabType> class RelocSectionWithSymtabBase : public RelocationSectionBase { -private: SymTabType *Symbols = nullptr; + void setSymTab(SymTabType *SymTab) { Symbols = SymTab; } protected: RelocSectionWithSymtabBase() = default; public: - void setSymTab(SymTabType *StrTab) { Symbols = StrTab; } void removeSectionReferences(const SectionBase *Sec) override; void initialize(SectionTableRef SecTable) override; void finalize() override; }; -template <class ELFT> class RelocationSection : public RelocSectionWithSymtabBase<SymbolTableSection> { -private: - using Elf_Rel = typename ELFT::Rel; - using Elf_Rela = typename ELFT::Rela; + MAKE_SEC_WRITER_FRIEND std::vector<Relocation> Relocations; - template <class T> 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; + void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override; + void markSymbols() override; static bool classof(const SectionBase *S) { if (S->Flags & ELF::SHF_ALLOC) @@ -294,32 +527,51 @@ public: } }; -class SectionWithStrTab : public Section { -private: - const SectionBase *StrTab = nullptr; +// TODO: The way stripping and groups interact is complicated +// and still needs to be worked on. + +class GroupSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + const SymbolTableSection *SymTab = nullptr; + Symbol *Sym = nullptr; + ELF::Elf32_Word FlagWord; + SmallVector<SectionBase *, 3> GroupMembers; public: - SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {} + // TODO: Contents is present in several classes of the hierarchy. + // This needs to be refactored to avoid duplication. + ArrayRef<uint8_t> Contents; - void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; } - void removeSectionReferences(const SectionBase *Sec) override; - void initialize(SectionTableRef SecTable) override; + explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {} + + void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; } + void setSymbol(Symbol *S) { Sym = S; } + void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; } + void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); } + + void initialize(SectionTableRef SecTable) override{}; + void accept(SectionVisitor &) const override; void finalize() override; - static bool classof(const SectionBase *S); + void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override; + void markSymbols() override; + + static bool classof(const SectionBase *S) { + return S->Type == ELF::SHT_GROUP; + } }; -class DynamicSymbolTableSection : public SectionWithStrTab { +class DynamicSymbolTableSection : public Section { public: - DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} + explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {} static bool classof(const SectionBase *S) { return S->Type == ELF::SHT_DYNSYM; } }; -class DynamicSection : public SectionWithStrTab { +class DynamicSection : public Section { public: - DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} + explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {} static bool classof(const SectionBase *S) { return S->Type == ELF::SHT_DYNAMIC; @@ -328,13 +580,15 @@ public: class DynamicRelocationSection : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> { + MAKE_SEC_WRITER_FRIEND + private: ArrayRef<uint8_t> Contents; public: - DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {} + explicit DynamicRelocationSection(ArrayRef<uint8_t> 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)) @@ -343,90 +597,125 @@ public: } }; -template <class ELFT> class Object { -private: - using SecPtr = std::unique_ptr<SectionBase>; - using SegPtr = std::unique_ptr<Segment>; - - using Elf_Shdr = typename ELFT::Shdr; - using Elf_Ehdr = typename ELFT::Ehdr; - using Elf_Phdr = typename ELFT::Phdr; - - void initSymbolTable(const object::ELFFile<ELFT> &ElfFile, - SymbolTableSection *SymTab, SectionTableRef SecTable); - SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile, - const Elf_Shdr &Shdr); - void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile); - SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile); +class GnuDebugLinkSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND -protected: - StringTableSection *SectionNames = nullptr; - SymbolTableSection *SymbolTable = nullptr; - std::vector<SecPtr> Sections; - std::vector<SegPtr> Segments; +private: + StringRef FileName; + uint32_t CRC32; - void writeHeader(FileOutputBuffer &Out) const; - void writeProgramHeaders(FileOutputBuffer &Out) const; - void writeSectionData(FileOutputBuffer &Out) const; - void writeSectionHeaders(FileOutputBuffer &Out) const; + void init(StringRef File, StringRef Data); 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; - - Object(const object::ELFObjectFile<ELFT> &Obj); - virtual ~Object() = default; + // If we add this section from an external source we can use this ctor. + explicit GnuDebugLinkSection(StringRef File); + void accept(SectionVisitor &Visitor) const override; +}; - const SymbolTableSection *getSymTab() const { return SymbolTable; } - const SectionBase *getSectionHeaderStrTab() const { return SectionNames; } - void removeSections(std::function<bool(const SectionBase &)> ToRemove); - void addSection(StringRef SecName, ArrayRef<uint8_t> Data); - virtual size_t totalSize() const = 0; - virtual void finalize() = 0; - virtual void write(FileOutputBuffer &Out) const = 0; +class Reader { +public: + virtual ~Reader(); + virtual std::unique_ptr<Object> create() const = 0; }; -template <class ELFT> class ELFObject : public Object<ELFT> { -private: - using SecPtr = std::unique_ptr<SectionBase>; - using SegPtr = std::unique_ptr<Segment>; +using object::Binary; +using object::ELFFile; +using object::ELFObjectFile; +using object::OwningBinary; +template <class ELFT> class ELFBuilder { +private: + using Elf_Addr = typename ELFT::Addr; using Elf_Shdr = typename ELFT::Shdr; using Elf_Ehdr = typename ELFT::Ehdr; - using Elf_Phdr = typename ELFT::Phdr; + using Elf_Word = typename ELFT::Word; - void sortSections(); - void assignOffsets(); + const ELFFile<ELFT> &ElfFile; + Object &Obj; + + void setParentSegment(Segment &Child); + void readProgramHeaders(); + void initGroupSection(GroupSection *GroupSec); + void initSymbolTable(SymbolTableSection *SymTab); + void readSectionHeaders(); + SectionBase &makeSection(const Elf_Shdr &Shdr); public: - ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} + ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj) + : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {} - void finalize() override; - size_t totalSize() const override; - void write(FileOutputBuffer &Out) const override; + void build(); }; -template <class ELFT> class BinaryObject : public Object<ELFT> { +class ELFReader : public Reader { + Binary *Bin; + +public: + ElfType getElfType() const; + std::unique_ptr<Object> create() const override; + explicit ELFReader(Binary *B) : Bin(B){}; +}; + +class Object { private: using SecPtr = std::unique_ptr<SectionBase>; using SegPtr = std::unique_ptr<Segment>; - uint64_t TotalSize; + std::vector<SecPtr> Sections; + std::vector<SegPtr> Segments; public: - BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} + template <class T> + using Range = iterator_range< + pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>; - void finalize() override; - size_t totalSize() const override; - void write(FileOutputBuffer &Out) const override; -}; + template <class T> + using ConstRange = iterator_range<pointee_iterator< + typename std::vector<std::unique_ptr<T>>::const_iterator>>; + + // It is often the case that the ELF header and the program header table are + // not present in any segment. This could be a problem during file layout, + // because other segments may get assigned an offset where either of the + // two should reside, which will effectively corrupt the resulting binary. + // Other than that we use these segments to track program header offsets + // when they may not follow the ELF header. + Segment ElfHdrSegment; + Segment ProgramHdrSegment; + 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; + SectionIndexSection *SectionIndexTable = nullptr; + + void sortSections(); + SectionTableRef sections() { return SectionTableRef(Sections); } + ConstRange<SectionBase> sections() const { + return make_pointee_range(Sections); + } + Range<Segment> segments() { return make_pointee_range(Segments); } + ConstRange<Segment> segments() const { return make_pointee_range(Segments); } + + void removeSections(std::function<bool(const SectionBase &)> ToRemove); + void removeSymbols(function_ref<bool(const Symbol &)> ToRemove); + template <class T, class... Ts> T &addSection(Ts &&... Args) { + auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...); + auto Ptr = Sec.get(); + Sections.emplace_back(std::move(Sec)); + return *Ptr; + } + Segment &addSegment(ArrayRef<uint8_t> Data) { + Segments.emplace_back(llvm::make_unique<Segment>(Data)); + return *Segments.back(); + } +}; +} // end namespace objcopy } // end namespace llvm #endif // LLVM_TOOLS_OBJCOPY_OBJECT_H diff --git a/tools/llvm-objcopy/StripOpts.td b/tools/llvm-objcopy/StripOpts.td new file mode 100644 index 000000000000..333b0d288efa --- /dev/null +++ b/tools/llvm-objcopy/StripOpts.td @@ -0,0 +1,49 @@ +include "llvm/Option/OptParser.td" + +multiclass Eq<string name> { + def NAME: Separate<["--", "-"], name>; + def NAME # _eq: Joined<["--", "-"], name # "=">, Alias<!cast<Separate>(NAME)>; +} + +def help : Flag<["-", "--"], "help">; + +defm output : Eq<"o">, + MetaVarName<"output">, + HelpText<"Write output to <file>">; + +def strip_all : Flag<["-", "--"], "strip-all">, + HelpText<"Remove non-allocated sections other than .gnu.warning* sections">; + +def strip_debug : Flag<["-", "--"], "strip-debug">, + HelpText<"Remove debugging symbols only">; + +def d : Flag<["-"], "d">, + Alias<strip_debug>; + +def g : Flag<["-"], "g">, + Alias<strip_debug>; + +def S : Flag<["-"], "S">, + Alias<strip_debug>; + +defm remove_section : Eq<"remove-section">, + MetaVarName<"section">, + HelpText<"Remove <section>">; + +def R : JoinedOrSeparate<["-"], "R">, + Alias<remove_section>; + +defm keep_symbol : Eq<"keep-symbol">, + MetaVarName<"symbol">, + HelpText<"Do not remove symbol <symbol>">; + +def K : JoinedOrSeparate<["-"], "K">, + Alias<keep_symbol>; + +def discard_all : Flag<["-", "--"], "discard-all">, + HelpText<"Remove all local symbols except file and section symbols">; +def x : Flag<["-"], "x">, + Alias<discard_all>; + +def strip_unneeded : Flag<["-", "--"], "strip-unneeded">, + HelpText<"Remove all symbols not needed by relocations">; diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp index 20ce93bb40e8..4ccc67cc75db 100644 --- a/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/tools/llvm-objcopy/llvm-objcopy.cpp @@ -13,10 +13,15 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" #include "llvm/BinaryFormat/ELF.h" +#include "llvm/Object/Archive.h" +#include "llvm/Object/ArchiveWriter.h" #include "llvm/Object/Binary.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/ELFTypes.h" #include "llvm/Object/Error.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" @@ -24,9 +29,8 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileOutputBuffer.h" -#include "llvm/Support/ManagedStatic.h" -#include "llvm/Support/PrettyStackTrace.h" -#include "llvm/Support/Signals.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> #include <cassert> @@ -39,13 +43,122 @@ #include <utility> using namespace llvm; +using namespace llvm::objcopy; using namespace object; using namespace ELF; -// The name this program was invoked as. -static StringRef ToolName; +namespace { + +enum ObjcopyID { + OBJCOPY_INVALID = 0, // This is not an option ID. +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + OBJCOPY_##ID, +#include "ObjcopyOpts.inc" +#undef OPTION +}; + +#define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE; +#include "ObjcopyOpts.inc" +#undef PREFIX + +static const opt::OptTable::Info ObjcopyInfoTable[] = { +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + {OBJCOPY_##PREFIX, \ + NAME, \ + HELPTEXT, \ + METAVAR, \ + OBJCOPY_##ID, \ + opt::Option::KIND##Class, \ + PARAM, \ + FLAGS, \ + OBJCOPY_##GROUP, \ + OBJCOPY_##ALIAS, \ + ALIASARGS, \ + VALUES}, +#include "ObjcopyOpts.inc" +#undef OPTION +}; + +class ObjcopyOptTable : public opt::OptTable { +public: + ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {} +}; + +enum StripID { + STRIP_INVALID = 0, // This is not an option ID. +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + STRIP_##ID, +#include "StripOpts.inc" +#undef OPTION +}; + +#define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE; +#include "StripOpts.inc" +#undef PREFIX + +static const opt::OptTable::Info StripInfoTable[] = { +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + {STRIP_##PREFIX, NAME, HELPTEXT, \ + METAVAR, STRIP_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, STRIP_##GROUP, \ + STRIP_##ALIAS, ALIASARGS, VALUES}, +#include "StripOpts.inc" +#undef OPTION +}; + +class StripOptTable : public opt::OptTable { +public: + StripOptTable() : OptTable(StripInfoTable, true) {} +}; + +struct CopyConfig { + StringRef OutputFilename; + StringRef InputFilename; + StringRef OutputFormat; + StringRef InputFormat; + StringRef BinaryArch; + + StringRef SplitDWO; + StringRef AddGnuDebugLink; + std::vector<StringRef> ToRemove; + std::vector<StringRef> Keep; + std::vector<StringRef> OnlyKeep; + std::vector<StringRef> AddSection; + std::vector<StringRef> SymbolsToLocalize; + std::vector<StringRef> SymbolsToGlobalize; + std::vector<StringRef> SymbolsToWeaken; + std::vector<StringRef> SymbolsToRemove; + std::vector<StringRef> SymbolsToKeep; + StringMap<StringRef> SectionsToRename; + StringMap<StringRef> SymbolsToRename; + bool StripAll = false; + bool StripAllGNU = false; + bool StripDebug = false; + bool StripSections = false; + bool StripNonAlloc = false; + bool StripDWO = false; + bool StripUnneeded = false; + bool ExtractDWO = false; + bool LocalizeHidden = false; + bool Weaken = false; + bool DiscardAll = false; + bool OnlyKeepDebug = false; + bool KeepFileSymbols = false; +}; + +using SectionPred = std::function<bool(const SectionBase &Sec)>; + +} // namespace namespace llvm { +namespace objcopy { + +// The name this program was invoked as. +StringRef ToolName; LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { errs() << ToolName << ": " << Message << ".\n"; @@ -69,95 +182,55 @@ LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { exit(1); } +} // end namespace objcopy } // end namespace llvm -static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>")); -static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"), - cl::init("-")); -static cl::opt<std::string> - OutputFormat("O", cl::desc("Set output format to one of the following:" - "\n\tbinary")); -static cl::list<std::string> ToRemove("remove-section", - cl::desc("Remove <section>"), - cl::value_desc("section")); -static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"), - cl::aliasopt(ToRemove)); -static cl::opt<bool> StripAll( - "strip-all", - cl::desc( - "Removes non-allocated sections other than .gnu.warning* sections")); -static cl::opt<bool> - StripAllGNU("strip-all-gnu", - cl::desc("Removes symbol, relocation, and debug information")); -static cl::list<std::string> Keep("keep", cl::desc("Keep <section>"), - cl::value_desc("section")); -static cl::list<std::string> OnlyKeep("only-keep", - cl::desc("Remove all but <section>"), - cl::value_desc("section")); -static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"), - cl::aliasopt(OnlyKeep)); -static cl::opt<bool> StripDebug("strip-debug", - cl::desc("Removes all debug information")); -static cl::opt<bool> StripSections("strip-sections", - cl::desc("Remove all section headers")); -static cl::opt<bool> - StripNonAlloc("strip-non-alloc", - cl::desc("Remove all non-allocated sections")); -static cl::opt<bool> - StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file")); -static cl::opt<bool> ExtractDWO( - "extract-dwo", - cl::desc("Remove all sections that are not DWARF .dwo sections from file")); -static cl::opt<std::string> - SplitDWO("split-dwo", - cl::desc("Equivalent to extract-dwo on the input file to " - "<dwo-file>, then strip-dwo on the input file"), - cl::value_desc("dwo-file")); -static cl::list<std::string> AddSection( - "add-section", - cl::desc("Make a section named <section> with the contents of <file>."), - cl::value_desc("section=file")); - -using SectionPred = std::function<bool(const SectionBase &Sec)>; - -bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); } +static bool IsDWOSection(const SectionBase &Sec) { + return Sec.Name.endswith(".dwo"); +} -template <class ELFT> -bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) { +static 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 <class ELFT> -void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) { - std::unique_ptr<FileOutputBuffer> Buffer; - Expected<std::unique_ptr<FileOutputBuffer>> 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))); +static std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, + Object &Obj, Buffer &Buf, + ElfType OutputElfType) { + if (Config.OutputFormat == "binary") { + return llvm::make_unique<BinaryWriter>(Obj, Buf); + } + // Depending on the initial ELFT and OutputFormat we need a different Writer. + switch (OutputElfType) { + case ELFT_ELF32LE: + return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf, + !Config.StripSections); + case ELFT_ELF64LE: + return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf, + !Config.StripSections); + case ELFT_ELF32BE: + return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf, + !Config.StripSections); + case ELFT_ELF64BE: + return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf, + !Config.StripSections); + } + llvm_unreachable("Invalid output format"); } -template <class ELFT> -void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) { - // Construct a second output file for the DWO sections. - ELFObject<ELFT> DWOFile(ObjFile); - - DWOFile.removeSections([&](const SectionBase &Sec) { - return OnlyKeepDWOPred<ELFT>(DWOFile, Sec); - }); - DWOFile.finalize(); - WriteObjectFile(DWOFile, File); +static void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader, + StringRef File, ElfType OutputElfType) { + auto DWOFile = Reader.create(); + DWOFile->removeSections( + [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); }); + FileBuffer FB(File); + auto Writer = CreateWriter(Config, *DWOFile, FB, OutputElfType); + Writer->finalize(); + Writer->write(); } // This function handles the high level operations of GNU objcopy including @@ -167,47 +240,104 @@ void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) { // 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 <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { - std::unique_ptr<Object<ELFT>> Obj; +static void HandleArgs(const CopyConfig &Config, Object &Obj, + const Reader &Reader, ElfType OutputElfType) { - if (!OutputFormat.empty() && OutputFormat != "binary") - error("invalid output format '" + OutputFormat + "'"); - if (!OutputFormat.empty() && OutputFormat == "binary") - Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile); - else - Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile); + if (!Config.SplitDWO.empty()) { + SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType); + } + + // TODO: update or remove symbols only if there is an option that affects + // them. + if (Obj.SymbolTable) { + Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { + if ((Config.LocalizeHidden && + (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || + (!Config.SymbolsToLocalize.empty() && + is_contained(Config.SymbolsToLocalize, Sym.Name))) + Sym.Binding = STB_LOCAL; + + if (!Config.SymbolsToGlobalize.empty() && + is_contained(Config.SymbolsToGlobalize, Sym.Name)) + Sym.Binding = STB_GLOBAL; + + if (!Config.SymbolsToWeaken.empty() && + is_contained(Config.SymbolsToWeaken, Sym.Name) && + Sym.Binding == STB_GLOBAL) + Sym.Binding = STB_WEAK; + + if (Config.Weaken && Sym.Binding == STB_GLOBAL && + Sym.getShndx() != SHN_UNDEF) + Sym.Binding = STB_WEAK; + + const auto I = Config.SymbolsToRename.find(Sym.Name); + if (I != Config.SymbolsToRename.end()) + Sym.Name = I->getValue(); + }); + + // The purpose of this loop is to mark symbols referenced by sections + // (like GroupSection or RelocationSection). This way, we know which + // symbols are still 'needed' and wich are not. + if (Config.StripUnneeded) { + for (auto &Section : Obj.sections()) + Section.markSymbols(); + } + + Obj.removeSymbols([&](const Symbol &Sym) { + if ((!Config.SymbolsToKeep.empty() && + is_contained(Config.SymbolsToKeep, Sym.Name)) || + (Config.KeepFileSymbols && Sym.Type == STT_FILE)) + return false; + + if (Config.DiscardAll && Sym.Binding == STB_LOCAL && + Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE && + Sym.Type != STT_SECTION) + return true; - if (!SplitDWO.empty()) - SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue()); + if (Config.StripAll || Config.StripAllGNU) + return true; + + if (!Config.SymbolsToRemove.empty() && + is_contained(Config.SymbolsToRemove, Sym.Name)) { + return true; + } + + if (Config.StripUnneeded && !Sym.Referenced && + (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && + Sym.Type != STT_FILE && Sym.Type != STT_SECTION) + return true; + + return false; + }); + } SectionPred RemovePred = [](const SectionBase &) { return false; }; // Removes: - - if (!ToRemove.empty()) { - RemovePred = [&](const SectionBase &Sec) { - return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != - std::end(ToRemove); + if (!Config.ToRemove.empty()) { + RemovePred = [&Config](const SectionBase &Sec) { + return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove), + Sec.Name) != std::end(Config.ToRemove); }; } - if (StripDWO || !SplitDWO.empty()) + if (Config.StripDWO || !Config.SplitDWO.empty()) RemovePred = [RemovePred](const SectionBase &Sec) { return IsDWOSection(Sec) || RemovePred(Sec); }; - if (ExtractDWO) + if (Config.ExtractDWO) RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { - return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec); + return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); }; - if (StripAllGNU) + if (Config.StripAllGNU) RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { if (RemovePred(Sec)) 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: @@ -219,33 +349,32 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { return Sec.Name.startswith(".debug"); }; - if (StripSections) { + if (Config.StripSections) { RemovePred = [RemovePred](const SectionBase &Sec) { return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; }; - Obj->WriteSectionHeaders = false; } - if (StripDebug) { + if (Config.StripDebug) { RemovePred = [RemovePred](const SectionBase &Sec) { return RemovePred(Sec) || Sec.Name.startswith(".debug"); }; } - if (StripNonAlloc) + if (Config.StripNonAlloc) 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; }; - if (StripAll) + if (Config.StripAll) 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; @@ -253,47 +382,67 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { }; // Explicit copies: - - if (!OnlyKeep.empty()) { - RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + if (!Config.OnlyKeep.empty()) { + RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) { // Explicitly keep these sections regardless of previous removes. - if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) != - std::end(OnlyKeep)) + if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep), + Sec.Name) != std::end(Config.OnlyKeep)) 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 && Obj.SymbolTable->getStrTab() == &Sec)) return false; - } + // Remove everything else. return true; }; } - if (!Keep.empty()) { - RemovePred = [RemovePred](const SectionBase &Sec) { + if (!Config.Keep.empty()) { + RemovePred = [Config, RemovePred](const SectionBase &Sec) { // Explicitly keep these sections regardless of previous removes. - if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) != - std::end(Keep)) + if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) != + std::end(Config.Keep)) return false; // Otherwise defer to RemovePred. return RemovePred(Sec); }; } - Obj->removeSections(RemovePred); + // This has to be the last predicate assignment. + // If the option --keep-symbol has been specified + // and at least one of those symbols is present + // (equivalently, the updated symbol table is not empty) + // the symbol table and the string table should not be removed. + if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) && + Obj.SymbolTable && !Obj.SymbolTable->empty()) { + RemovePred = [&Obj, RemovePred](const SectionBase &Sec) { + if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) + return false; + return RemovePred(Sec); + }; + } - if (!AddSection.empty()) { - for (const auto &Flag : AddSection) { - auto SecPair = StringRef(Flag).split("="); + Obj.removeSections(RemovePred); + + if (!Config.SectionsToRename.empty()) { + for (auto &Sec : Obj.sections()) { + const auto Iter = Config.SectionsToRename.find(Sec.Name); + if (Iter != Config.SectionsToRename.end()) + Sec.Name = Iter->second; + } + } + + if (!Config.AddSection.empty()) { + for (const auto &Flag : Config.AddSection) { + auto SecPair = Flag.split("="); auto SecName = SecPair.first; auto File = SecPair.second; auto BufOrErr = MemoryBuffer::getFile(File); @@ -302,44 +451,256 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { auto Buf = std::move(*BufOrErr); auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart()); auto BufSize = Buf->getBufferSize(); - Obj->addSection(SecName, ArrayRef<uint8_t>(BufPtr, BufSize)); + Obj.addSection<OwnedDataSection>(SecName, + ArrayRef<uint8_t>(BufPtr, BufSize)); } } - Obj->finalize(); - WriteObjectFile(*Obj, OutputFilename.getValue()); + if (!Config.AddGnuDebugLink.empty()) + Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink); } -int main(int argc, char **argv) { - // Print a stack trace if we signal out. - sys::PrintStackTraceOnErrorSignal(argv[0]); - PrettyStackTraceProgram X(argc, argv); - llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. - cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n"); - ToolName = argv[0]; - if (InputFilename.empty()) { - cl::PrintHelpMessage(); - return 2; +static void ExecuteElfObjcopyOnBinary(const CopyConfig &Config, Binary &Binary, + Buffer &Out) { + ELFReader Reader(&Binary); + std::unique_ptr<Object> Obj = Reader.create(); + + HandleArgs(Config, *Obj, Reader, Reader.getElfType()); + + std::unique_ptr<Writer> Writer = + CreateWriter(Config, *Obj, Out, Reader.getElfType()); + Writer->finalize(); + Writer->write(); +} + +// For regular archives this function simply calls llvm::writeArchive, +// For thin archives it writes the archive file itself as well as its members. +static Error deepWriteArchive(StringRef ArcName, + ArrayRef<NewArchiveMember> NewMembers, + bool WriteSymtab, object::Archive::Kind Kind, + bool Deterministic, bool Thin) { + Error E = + writeArchive(ArcName, NewMembers, WriteSymtab, Kind, Deterministic, Thin); + if (!Thin || E) + return E; + for (const NewArchiveMember &Member : NewMembers) { + // Internally, FileBuffer will use the buffer created by + // FileOutputBuffer::create, for regular files (that is the case for + // deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer. + // OnDiskBuffer uses a temporary file and then renames it. So in reality + // there is no inefficiency / duplicated in-memory buffers in this case. For + // now in-memory buffers can not be completely avoided since + // NewArchiveMember still requires them even though writeArchive does not + // write them on disk. + FileBuffer FB(Member.MemberName); + FB.allocate(Member.Buf->getBufferSize()); + std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(), + FB.getBufferStart()); + if (auto E = FB.commit()) + return E; + } + return Error::success(); +} + +static void ExecuteElfObjcopyOnArchive(const CopyConfig &Config, const Archive &Ar) { + std::vector<NewArchiveMember> NewArchiveMembers; + Error Err = Error::success(); + for (const Archive::Child &Child : Ar.children(Err)) { + Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(); + if (!ChildOrErr) + reportError(Ar.getFileName(), ChildOrErr.takeError()); + Expected<StringRef> ChildNameOrErr = Child.getName(); + if (!ChildNameOrErr) + reportError(Ar.getFileName(), ChildNameOrErr.takeError()); + + MemBuffer MB(ChildNameOrErr.get()); + ExecuteElfObjcopyOnBinary(Config, **ChildOrErr, MB); + + Expected<NewArchiveMember> Member = + NewArchiveMember::getOldMember(Child, true); + if (!Member) + reportError(Ar.getFileName(), Member.takeError()); + Member->Buf = MB.releaseMemoryBuffer(); + Member->MemberName = Member->Buf->getBufferIdentifier(); + NewArchiveMembers.push_back(std::move(*Member)); } - Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename); + + if (Err) + reportError(Config.InputFilename, std::move(Err)); + if (Error E = + deepWriteArchive(Config.OutputFilename, NewArchiveMembers, + Ar.hasSymbolTable(), Ar.kind(), true, Ar.isThin())) + reportError(Config.OutputFilename, std::move(E)); +} + +static void ExecuteElfObjcopy(const CopyConfig &Config) { + Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr = + createBinary(Config.InputFilename); if (!BinaryOrErr) - reportError(InputFilename, BinaryOrErr.takeError()); - Binary &Binary = *BinaryOrErr.get().getBinary(); - if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) { - CopyBinary(*o); - return 0; + reportError(Config.InputFilename, BinaryOrErr.takeError()); + + if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) + return ExecuteElfObjcopyOnArchive(Config, *Ar); + + FileBuffer FB(Config.OutputFilename); + ExecuteElfObjcopyOnBinary(Config, *BinaryOrErr.get().getBinary(), FB); +} + +// ParseObjcopyOptions returns the config and sets the input arguments. If a +// help flag is set then ParseObjcopyOptions will print the help messege and +// exit. +static CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) { + ObjcopyOptTable T; + unsigned MissingArgumentIndex, MissingArgumentCount; + llvm::opt::InputArgList InputArgs = + T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); + + if (InputArgs.size() == 0) { + T.PrintHelp(errs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool"); + exit(1); + } + + if (InputArgs.hasArg(OBJCOPY_help)) { + T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool"); + exit(0); + } + + SmallVector<const char *, 2> Positional; + + for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) + error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); + + for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) + Positional.push_back(Arg->getValue()); + + if (Positional.empty()) + error("No input file specified"); + + if (Positional.size() > 2) + error("Too many positional arguments"); + + CopyConfig Config; + Config.InputFilename = Positional[0]; + Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; + Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); + Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); + Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); + + Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); + Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); + + for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { + if (!StringRef(Arg->getValue()).contains('=')) + error("Bad format for --redefine-sym"); + auto Old2New = StringRef(Arg->getValue()).split('='); + if (!Config.SymbolsToRename.insert(Old2New).second) + error("Multiple redefinition of symbol " + Old2New.first); } - if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) { - CopyBinary(*o); - return 0; + + for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { + if (!StringRef(Arg->getValue()).contains('=')) + error("Bad format for --rename-section"); + auto Old2New = StringRef(Arg->getValue()).split('='); + if (!Config.SectionsToRename.insert(Old2New).second) + error("Already have a section rename for " + Old2New.first); } - if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) { - CopyBinary(*o); - return 0; + + for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) + Config.ToRemove.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_keep)) + Config.Keep.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep)) + Config.OnlyKeep.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) + Config.AddSection.push_back(Arg->getValue()); + Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); + Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); + Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); + Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); + Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); + Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); + Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); + Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); + Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); + Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); + Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all); + Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); + Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); + for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) + Config.SymbolsToLocalize.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) + Config.SymbolsToGlobalize.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) + Config.SymbolsToWeaken.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) + Config.SymbolsToRemove.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) + Config.SymbolsToKeep.push_back(Arg->getValue()); + + return Config; +} + +// ParseStripOptions returns the config and sets the input arguments. If a +// help flag is set then ParseStripOptions will print the help messege and +// exit. +static CopyConfig ParseStripOptions(ArrayRef<const char *> ArgsArr) { + StripOptTable T; + unsigned MissingArgumentIndex, MissingArgumentCount; + llvm::opt::InputArgList InputArgs = + T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); + + if (InputArgs.size() == 0) { + T.PrintHelp(errs(), "llvm-strip <input> [ <output> ]", "strip tool"); + exit(1); } - if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) { - CopyBinary(*o); - return 0; + + if (InputArgs.hasArg(STRIP_help)) { + T.PrintHelp(outs(), "llvm-strip <input> [ <output> ]", "strip tool"); + exit(0); } - reportError(InputFilename, object_error::invalid_file_type); + + SmallVector<const char *, 2> Positional; + for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) + error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); + for (auto Arg : InputArgs.filtered(STRIP_INPUT)) + Positional.push_back(Arg->getValue()); + + if (Positional.empty()) + error("No input file specified"); + + if (Positional.size() > 2) + error("Support for multiple input files is not implemented yet"); + + CopyConfig Config; + Config.InputFilename = Positional[0]; + Config.OutputFilename = + InputArgs.getLastArgValue(STRIP_output, Positional[0]); + + Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); + + Config.DiscardAll = InputArgs.hasArg(STRIP_discard_all); + Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); + Config.StripAll = InputArgs.hasArg(STRIP_strip_all); + + if (!Config.StripDebug && !Config.StripUnneeded && !Config.DiscardAll) + Config.StripAll = true; + + for (auto Arg : InputArgs.filtered(STRIP_remove_section)) + Config.ToRemove.push_back(Arg->getValue()); + + for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) + Config.SymbolsToKeep.push_back(Arg->getValue()); + + return Config; +} + +int main(int argc, char **argv) { + InitLLVM X(argc, argv); + ToolName = argv[0]; + CopyConfig Config; + if (sys::path::stem(ToolName).endswith_lower("strip")) + Config = ParseStripOptions(makeArrayRef(argv + 1, argc)); + else + Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc)); + ExecuteElfObjcopy(Config); } diff --git a/tools/llvm-objcopy/llvm-objcopy.h b/tools/llvm-objcopy/llvm-objcopy.h index 6732e410d8e0..e222b65dc78f 100644 --- a/tools/llvm-objcopy/llvm-objcopy.h +++ b/tools/llvm-objcopy/llvm-objcopy.h @@ -17,8 +17,12 @@ #include <string> namespace llvm { +namespace objcopy { 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) @@ -32,6 +36,7 @@ template <class T> T unwrapOrError(Expected<T> EO) { error(Buf); } +} // end namespace objcopy } // end namespace llvm #endif // LLVM_TOOLS_OBJCOPY_OBJCOPY_H |