From: Jake Ehrlich Date: Wed, 11 Oct 2017 02:42:29 +0000 (+0000) Subject: Revert "[llvm-objcopy] Add support for --strip-sections to remove all section headers... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9f81bdc36b453f32667d09c84d1b6add3a759a85;p=llvm Revert "[llvm-objcopy] Add support for --strip-sections to remove all section headers leaving only program headers and loadable segment data" This reverts commit rL315412 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315417 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/tools/llvm-objcopy/strip-sections.test b/test/tools/llvm-objcopy/strip-sections.test deleted file mode 100644 index 66cb34e89b0..00000000000 --- a/test/tools/llvm-objcopy/strip-sections.test +++ /dev/null @@ -1,66 +0,0 @@ -# RUN: yaml2obj %s > %t -# RUN: llvm-objcopy --strip-sections %t %t2 -# RUN: llvm-readobj -file-headers -program-headers %t2 | FileCheck %s -# RUN: od -t x1 -j 4096 %t2 | FileCheck %s --check-prefix=DATA - -!ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_EXEC - Machine: EM_X86_64 -Sections: - - Name: .text - Type: SHT_PROGBITS - Flags: [ SHF_ALLOC, SHF_EXECINSTR ] - AddressAlign: 0x0000000000001000 - Content: "DEADBEEF" -ProgramHeaders: -- Type: PT_LOAD - Flags: [ PF_X, PF_R ] - Sections: - - Section: .text - -#DATA: 0010000 de ad be ef - -#CHECK: ElfHeader { -#CHECK-NEXT: Ident { -#CHECK-NEXT: Magic: (7F 45 4C 46) -#CHECK-NEXT: Class: 64-bit (0x2) -#CHECK-NEXT: DataEncoding: LittleEndian (0x1) -#CHECK-NEXT: FileVersion: 1 -#CHECK-NEXT: OS/ABI: SystemV (0x0) -#CHECK-NEXT: ABIVersion: 0 -#CHECK-NEXT: Unused: (00 00 00 00 00 00 00) -#CHECK-NEXT: } -#CHECK-NEXT: Type: Executable (0x2) -#CHECK-NEXT: Machine: EM_X86_64 (0x3E) -#CHECK-NEXT: Version: 1 -#CHECK-NEXT: Entry: 0x0 -#CHECK-NEXT: ProgramHeaderOffset: 0x40 -#CHECK-NEXT: SectionHeaderOffset: 0x0 -#CHECK-NEXT: Flags [ (0x0) -#CHECK-NEXT: ] -#CHECK-NEXT: HeaderSize: 64 -#CHECK-NEXT: ProgramHeaderEntrySize: 56 -#CHECK-NEXT: ProgramHeaderCount: 1 -#CHECK-NEXT: SectionHeaderEntrySize: 64 -#CHECK-NEXT: SectionHeaderCount: 0 -#CHECK-NEXT: StringTableSectionIndex: 0 -#CHECK-NEXT: } - -#CHECK: ProgramHeaders [ -#CHECK-NEXT: ProgramHeader { -#CHECK-NEXT: Type: PT_LOAD (0x1) -#CHECK-NEXT: Offset: 0x1000 -#CHECK-NEXT: VirtualAddress: 0x0 -#CHECK-NEXT: PhysicalAddress: 0x0 -#CHECK-NEXT: FileSize: 4 -#CHECK-NEXT: MemSize: 4 -#CHECK-NEXT: Flags [ (0x5) -#CHECK-NEXT: PF_R (0x4) -#CHECK-NEXT: PF_X (0x1) -#CHECK-NEXT: ] -#CHECK-NEXT: Alignment: 4096 -#CHECK-NEXT: } -#CHECK-NEXT:] diff --git a/tools/llvm-objcopy/Object.cpp b/tools/llvm-objcopy/Object.cpp index 797d8f961e4..2bd3aa16f0b 100644 --- a/tools/llvm-objcopy/Object.cpp +++ b/tools/llvm-objcopy/Object.cpp @@ -575,20 +575,14 @@ void Object::writeHeader(FileOutputBuffer &Out) const { Ehdr.e_version = Version; Ehdr.e_entry = Entry; Ehdr.e_phoff = sizeof(Elf_Ehdr); + Ehdr.e_shoff = SHOffset; Ehdr.e_flags = Flags; Ehdr.e_ehsize = sizeof(Elf_Ehdr); Ehdr.e_phentsize = sizeof(Elf_Phdr); Ehdr.e_phnum = Segments.size(); Ehdr.e_shentsize = sizeof(Elf_Shdr); - if (WriteSectionHeaders) { - Ehdr.e_shoff = SHOffset; - Ehdr.e_shnum = Sections.size() + 1; - Ehdr.e_shstrndx = SectionNames->Index; - } else { - Ehdr.e_shoff = 0; - Ehdr.e_shnum = 0; - Ehdr.e_shstrndx = 0; - } + Ehdr.e_shnum = Sections.size() + 1; + Ehdr.e_shstrndx = SectionNames->Index; } template @@ -639,10 +633,12 @@ void Object::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."); - SectionNames = nullptr; + // Right now llvm-objcopy always outputs section headers. This will not + // always be the case. Eventully the section header table will become + // optional and if no section header is output then there dosn't need to be + // a section header string table. + error("Cannot remove " + SectionNames->Name + + " because it is the section header string table."); } // 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 @@ -736,34 +732,29 @@ template void ELFObject::assignOffsets() { } } - if (this->WriteSectionHeaders) { - Offset = alignTo(Offset, sizeof(typename ELFT::Addr)); - } + Offset = alignTo(Offset, sizeof(typename ELFT::Addr)); this->SHOffset = Offset; } template size_t ELFObject::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) + - NullSectionSize; + sizeof(Elf_Shdr); } template void ELFObject::write(FileOutputBuffer &Out) const { this->writeHeader(Out); this->writeProgramHeaders(Out); this->writeSectionData(Out); - if (this->WriteSectionHeaders) - this->writeSectionHeaders(Out); + this->writeSectionHeaders(Out); } template void ELFObject::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); - } + for (const auto &Section : this->Sections) { + this->SectionNames->addString(Section->Name); + } // Make sure we add the names of all the symbols. if (this->SymbolTable != nullptr) this->SymbolTable->addSymbolNames(); @@ -772,16 +763,14 @@ template void ELFObject::finalize() { assignOffsets(); // Finalize SectionNames first so that we can assign name indexes. - if (this->SectionNames != nullptr) - this->SectionNames->finalize(); + this->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; Offset += sizeof(Elf_Shdr); - if (this->WriteSectionHeaders) - Section->NameIndex = this->SectionNames->findIndex(Section->Name); + Section->NameIndex = this->SectionNames->findIndex(Section->Name); Section->finalize(); } } diff --git a/tools/llvm-objcopy/Object.h b/tools/llvm-objcopy/Object.h index f6088434805..391465db65e 100644 --- a/tools/llvm-objcopy/Object.h +++ b/tools/llvm-objcopy/Object.h @@ -346,7 +346,6 @@ public: uint32_t Machine; uint32_t Version; uint32_t Flags; - bool WriteSectionHeaders = true; Object(const llvm::object::ELFObjectFile &Obj); void removeSections(std::function ToRemove); diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp index 7f55a434b33..d76735482d6 100644 --- a/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/tools/llvm-objcopy/llvm-objcopy.cpp @@ -56,14 +56,11 @@ cl::opt OutputFilename(cl::Positional, cl::desc(""), cl::opt OutputFormat("O", cl::desc("set output format to one of the following:" "\n\tbinary")); + cl::list ToRemove("remove-section", cl::desc("Remove a specific section")); cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"), cl::aliasopt(ToRemove)); -cl::opt StripSections("strip-sections", - cl::desc("Remove all section headers")); - -typedef std::function SectionPred; void CopyBinary(const ELFObjectFile &ObjFile) { std::unique_ptr Buffer; @@ -74,25 +71,12 @@ void CopyBinary(const ELFObjectFile &ObjFile) { Obj = llvm::make_unique>(ObjFile); else Obj = llvm::make_unique>(ObjFile); - - SectionPred RemovePred = [](const SectionBase &) { return false; }; - if (!ToRemove.empty()) { - RemovePred = [&](const SectionBase &Sec) { + Obj->removeSections([&](const SectionBase &Sec) { return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != std::end(ToRemove); - }; + }); } - - if (StripSections) { - RemovePred = [RemovePred](const SectionBase &Sec) { - return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; - }; - Obj->WriteSectionHeaders = false; - } - - Obj->removeSections(RemovePred); - Obj->finalize(); ErrorOr> BufferOrErr = FileOutputBuffer::create(OutputFilename, Obj->totalSize(),