Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 6bbc345

Browse files
committed
[llvm-objcopy] Add --dump-section
Differential Revision: https://reviews.llvm.org/D49979 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339358 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5a282e5 commit 6bbc345

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

test/tools/llvm-objcopy/dump-section.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# RUN: yaml2obj %s > %t
22
# RUN: llvm-objcopy -O binary -j .text %t %t2
33
# RUN: llvm-objcopy -O binary -only-keep .text %t %t3
4+
# RUN: llvm-objcopy --dump-section .text=%t4 %t %t5
5+
# RUN: llvm-objcopy --dump-section .foo=%t6 %t %t7
6+
# RUN: not llvm-objcopy --dump-section .bar=%t8 %t %t9 2>&1 | FileCheck %s --check-prefix=NOBITS
47
# RUN: od -t x1 %t2 | FileCheck %s
8+
# RUN: od -t x1 %t6 | FileCheck %s --check-prefix=NON-ALLOC
59
# RUN: wc -c %t2 | FileCheck %s --check-prefix=SIZE
610
# RUN: diff %t2 %t3
11+
# RUN: diff %t4 %t3
712

813
!ELF
914
FileHeader:
@@ -17,6 +22,13 @@ Sections:
1722
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
1823
AddressAlign: 0x0000000000001000
1924
Content: "DEADBEEF"
25+
- Name: .foo
26+
Type: SHT_PROGBITS
27+
Flags: [ SHF_WRITE ]
28+
Content: "CAFE"
29+
- Name: .bar
30+
Type: SHT_NOBITS
31+
Flags: [ SHF_WRITE ]
2032
ProgramHeaders:
2133
- Type: PT_LOAD
2234
Flags: [ PF_X, PF_R ]
@@ -25,4 +37,8 @@ ProgramHeaders:
2537

2638
#CHECK: 0000000 de ad be ef
2739

40+
#NON-ALLOC: 0000000 ca fe
41+
2842
#SIZE: 4
43+
44+
#NOBITS: Can't dump section ".bar": it has no contents

tools/llvm-objcopy/ObjcopyOpts.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ def strip_unneeded : Flag<["-", "--"], "strip-unneeded">,
102102
HelpText<"Remove all symbols not needed by relocations">;
103103
def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">,
104104
HelpText<"Do not remove file symbols">;
105+
defm dump_section : Eq<"dump-section">,
106+
MetaVarName<"section=file">,
107+
HelpText<"Dump contents of section named <section> into file <file>">;

tools/llvm-objcopy/Object.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,9 @@ template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
853853
Sec.Align = Shdr.sh_addralign;
854854
Sec.EntrySize = Shdr.sh_entsize;
855855
Sec.Index = Index++;
856+
Sec.OriginalData =
857+
ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
858+
(Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size);
856859
}
857860

858861
// If a section index table exists we'll need to initialize it before we

tools/llvm-objcopy/Object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ class SectionBase {
250250
uint64_t Offset = 0;
251251
uint64_t Size = 0;
252252
uint64_t Type = ELF::SHT_NULL;
253+
ArrayRef<uint8_t> OriginalData;
253254

254255
virtual ~SectionBase() = default;
255256

tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct CopyConfig {
137137
std::vector<StringRef> Keep;
138138
std::vector<StringRef> OnlyKeep;
139139
std::vector<StringRef> AddSection;
140+
std::vector<StringRef> DumpSection;
140141
std::vector<StringRef> SymbolsToLocalize;
141142
std::vector<StringRef> SymbolsToGlobalize;
142143
std::vector<StringRef> SymbolsToWeaken;
@@ -323,6 +324,30 @@ static void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
323324
Writer->write();
324325
}
325326

327+
static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
328+
Object &Obj) {
329+
for (auto &Sec : Obj.sections()) {
330+
if (Sec.Name == SecName) {
331+
if (Sec.OriginalData.size() == 0)
332+
return make_error<StringError>("Can't dump section \"" + SecName +
333+
"\": it has no contents",
334+
object_error::parse_failed);
335+
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
336+
FileOutputBuffer::create(Filename, Sec.OriginalData.size());
337+
if (!BufferOrErr)
338+
return BufferOrErr.takeError();
339+
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
340+
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
341+
Buf->getBufferStart());
342+
if (Error E = Buf->commit())
343+
return E;
344+
return Error::success();
345+
}
346+
}
347+
return make_error<StringError>("Section not found",
348+
object_error::parse_failed);
349+
}
350+
326351
// This function handles the high level operations of GNU objcopy including
327352
// handling command line options. It's important to outline certain properties
328353
// we expect to hold of the command line operations. Any operation that "keeps"
@@ -555,6 +580,16 @@ static void HandleArgs(const CopyConfig &Config, Object &Obj,
555580
}
556581
}
557582

583+
if (!Config.DumpSection.empty()) {
584+
for (const auto &Flag : Config.DumpSection) {
585+
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
586+
StringRef SecName = SecPair.first;
587+
StringRef File = SecPair.second;
588+
if (Error E = dumpSectionToFile(SecName, File, Obj))
589+
reportError(Config.InputFilename, std::move(E));
590+
}
591+
}
592+
558593
if (!Config.AddGnuDebugLink.empty())
559594
Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
560595
}
@@ -711,6 +746,8 @@ static CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
711746
Config.OnlyKeep.push_back(Arg->getValue());
712747
for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
713748
Config.AddSection.push_back(Arg->getValue());
749+
for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
750+
Config.DumpSection.push_back(Arg->getValue());
714751
Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
715752
Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
716753
Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);

0 commit comments

Comments
 (0)