From e8437de727ed208cc4a8b7b36b6165dca89fafcb Mon Sep 17 00:00:00 2001 From: Jake Ehrlich Date: Tue, 19 Dec 2017 00:47:30 +0000 Subject: [llvm-objcopy] Add option to add a progbits section from a file This change adds support for adding progbits sections with contents from a file Differential Revision: https://reviews.llvm.org/D41212 llvm-svn: 321047 --- llvm/tools/llvm-objcopy/Object.cpp | 12 ++++++++++++ llvm/tools/llvm-objcopy/Object.h | 15 +++++++++++++++ llvm/tools/llvm-objcopy/llvm-objcopy.cpp | 22 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) (limited to 'llvm/tools/llvm-objcopy') diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp index bd5bcd7fc18..d5dfcac40e4 100644 --- a/llvm/tools/llvm-objcopy/Object.cpp +++ b/llvm/tools/llvm-objcopy/Object.cpp @@ -81,6 +81,11 @@ void Section::writeSection(FileOutputBuffer &Out) const { std::copy(std::begin(Contents), std::end(Contents), Buf); } +void OwnedDataSection::writeSection(FileOutputBuffer &Out) const { + uint8_t *Buf = Out.getBufferStart() + Offset; + std::copy(std::begin(Data), std::end(Data), Buf); +} + void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); Size = StrTabBuilder.getSize(); @@ -676,6 +681,13 @@ void Object::removeSections( Sections.erase(Iter, std::end(Sections)); } +template +void Object::addSection(StringRef SecName, ArrayRef Data) { + auto Sec = llvm::make_unique(SecName, Data); + Sec->OriginalOffset = ~0ULL; + Sections.push_back(std::move(Sec)); +} + template void ELFObject::sortSections() { // Put all sections in offset order. Maintain the ordering as closely as // possible while meeting that demand however. diff --git a/llvm/tools/llvm-objcopy/Object.h b/llvm/tools/llvm-objcopy/Object.h index 9f98c04ad9b..b04b0c1a641 100644 --- a/llvm/tools/llvm-objcopy/Object.h +++ b/llvm/tools/llvm-objcopy/Object.h @@ -126,6 +126,20 @@ public: void writeSection(FileOutputBuffer &Out) const override; }; +class OwnedDataSection : public SectionBase { +private: + std::vector Data; + +public: + OwnedDataSection(StringRef SecName, ArrayRef Data) + : Data(std::begin(Data), std::end(Data)) { + Name = SecName; + Type = ELF::SHT_PROGBITS; + Size = Data.size(); + } + void writeSection(FileOutputBuffer &Out) const override; +}; + // There are two types of string tables that can exist, dynamic and not dynamic. // In the dynamic case the string table is allocated. Changing a dynamic string // table would mean altering virtual addresses and thus the memory image. So @@ -372,6 +386,7 @@ public: const SymbolTableSection *getSymTab() const { return SymbolTable; } const SectionBase *getSectionHeaderStrTab() const { return SectionNames; } void removeSections(std::function ToRemove); + void addSection(StringRef SecName, ArrayRef Data); virtual size_t totalSize() const = 0; virtual void finalize() = 0; virtual void write(FileOutputBuffer &Out) const = 0; diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index 9d60ae42639..20ce93bb40e 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -113,6 +113,10 @@ static cl::opt cl::desc("Equivalent to extract-dwo on the input file to " ", then strip-dwo on the input file"), cl::value_desc("dwo-file")); +static cl::list AddSection( + "add-section", + cl::desc("Make a section named
with the contents of ."), + cl::value_desc("section=file")); using SectionPred = std::function; @@ -174,7 +178,7 @@ template void CopyBinary(const ELFObjectFile &ObjFile) { Obj = llvm::make_unique>(ObjFile); if (!SplitDWO.empty()) - SplitDWOToFile(ObjFile, SplitDWO.getValue()); + SplitDWOToFile(ObjFile, SplitDWO.getValue()); SectionPred RemovePred = [](const SectionBase &) { return false; }; @@ -286,6 +290,22 @@ template void CopyBinary(const ELFObjectFile &ObjFile) { } Obj->removeSections(RemovePred); + + if (!AddSection.empty()) { + for (const auto &Flag : AddSection) { + auto SecPair = StringRef(Flag).split("="); + auto SecName = SecPair.first; + auto File = SecPair.second; + auto BufOrErr = MemoryBuffer::getFile(File); + if (!BufOrErr) + reportError(File, BufOrErr.getError()); + auto Buf = std::move(*BufOrErr); + auto BufPtr = reinterpret_cast(Buf->getBufferStart()); + auto BufSize = Buf->getBufferSize(); + Obj->addSection(SecName, ArrayRef(BufPtr, BufSize)); + } + } + Obj->finalize(); WriteObjectFile(*Obj, OutputFilename.getValue()); } -- cgit v1.2.3