Index: test/tools/llvm-objcopy/dump-section.test =================================================================== --- test/tools/llvm-objcopy/dump-section.test +++ test/tools/llvm-objcopy/dump-section.test @@ -1,9 +1,13 @@ # RUN: yaml2obj %s > %t # RUN: llvm-objcopy -O binary -j .text %t %t2 # RUN: llvm-objcopy -O binary -only-keep .text %t %t3 +# RUN: llvm-objcopy --dump-section .text=%t4 %t %t5 +# RUN: llvm-objcopy --dump-section .symtab=%t6 %t %t7 # RUN: od -t x1 %t2 | FileCheck %s +# RUN: od -t x1 %t6 | FileCheck %s --check-prefix=NON-ALLOC # RUN: wc -c %t2 | FileCheck %s --check-prefix=SIZE # RUN: diff %t2 %t3 +# RUN: diff %t4 %t3 !ELF FileHeader: @@ -22,7 +26,22 @@ Flags: [ PF_X, PF_R ] Sections: - Section: .text +Symbols: + Local: + - Name: foo + Type: STT_FILE + Section: .text + Global: + - Name: bar + Type: STT_FUNC + Section: .text #CHECK: 0000000 de ad be ef #SIZE: 4 + +#NON-ALLOC: 0000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +#NON-ALLOC: 0000020 00 00 00 00 00 00 00 00 05 00 00 00 04 00 01 00 +#NON-ALLOC: 0000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +#NON-ALLOC: 0000060 01 00 00 00 12 00 01 00 00 00 00 00 00 00 00 00 +#NON-ALLOC: 0000100 00 00 00 00 00 00 00 00 Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -97,3 +97,6 @@ HelpText<"Remove all symbols not needed by relocations">; def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">, HelpText<"Do not remove file symbols">; +defm dump_section : Eq<"dump-section">, + MetaVarName<"section=file">, + HelpText<"Dump contents of section named
into file ">; Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -250,6 +250,7 @@ uint64_t Offset = 0; uint64_t Size = 0; uint64_t Type = ELF::SHT_NULL; + ArrayRef OriginalData; virtual ~SectionBase() = default; Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -854,6 +854,9 @@ Sec.Align = Shdr.sh_addralign; Sec.EntrySize = Shdr.sh_entsize; Sec.Index = Index++; + Sec.OriginalData = + ArrayRef(ElfFile.base() + Shdr.sh_offset, + (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size); } // If a section index table exists we'll need to initialize it before we Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -128,6 +128,7 @@ std::vector Keep; std::vector OnlyKeep; std::vector AddSection; + std::vector DumpSection; std::vector SymbolsToLocalize; std::vector SymbolsToGlobalize; std::vector SymbolsToWeaken; @@ -237,6 +238,26 @@ Writer->write(); } +static Error dumpSectionToFile(StringRef SecName, StringRef Filename, + Object &Obj) { + for (auto &Sec : Obj.sections()) { + if (Sec.Name == SecName) { + Expected> BufferOrErr = + FileOutputBuffer::create(Filename, Sec.OriginalData.size()); + if (!BufferOrErr) + return BufferOrErr.takeError(); + std::unique_ptr Buf = std::move(*BufferOrErr); + std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), + Buf->getBufferStart()); + if (Error E = Buf->commit()) + return E; + return Error::success(); + } + } + return make_error("Section not found", + object_error::parse_failed); +} + // This function handles the high level operations of GNU objcopy including // handling command line options. It's important to outline certain properties // we expect to hold of the command line operations. Any operation that "keeps" @@ -457,6 +478,16 @@ } } + if (!Config.DumpSection.empty()) { + for (const auto &Flag : Config.DumpSection) { + std::pair SecPair = Flag.split("="); + StringRef SecName = SecPair.first; + StringRef File = SecPair.second; + if (Error E = dumpSectionToFile(SecName, File, Obj)) + reportError(Config.InputFilename, std::move(E)); + } + } + if (!Config.AddGnuDebugLink.empty()) Obj.addSection(Config.AddGnuDebugLink); } @@ -614,6 +645,8 @@ Config.OnlyKeep.push_back(Arg->getValue()); for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) Config.AddSection.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section)) + Config.DumpSection.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);