Skip to content

Commit e16b5c6

Browse files
authored
Merge pull request #6850 from compnerd/objdump
ObjCopy: support `--dump-section` on COFF
2 parents d0bb139 + 7ed3977 commit e16b5c6

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

llvm/lib/ObjCopy/COFF/COFFObjcopy.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,37 @@ static uint32_t flagsToCharacteristics(SectionFlag AllFlags, uint32_t OldChar) {
130130
return NewCharacteristics;
131131
}
132132

133+
static Error dumpSection(Object &O, StringRef SectionName, StringRef FileName) {
134+
for (const coff::Section &Section : O.getSections()) {
135+
if (Section.Name != SectionName)
136+
continue;
137+
138+
ArrayRef<uint8_t> Contents = Section.getContents();
139+
140+
std::unique_ptr<FileOutputBuffer> Buffer;
141+
if (auto B = FileOutputBuffer::create(FileName, Contents.size()))
142+
Buffer = std::move(*B);
143+
else
144+
return B.takeError();
145+
146+
llvm::copy(Contents, Buffer->getBufferStart());
147+
if (Error E = Buffer->commit())
148+
return E;
149+
150+
return Error::success();
151+
}
152+
return createStringError(object_error::parse_failed, "section '%s' not found",
153+
SectionName.str().c_str());
154+
}
155+
133156
static Error handleArgs(const CommonConfig &Config,
134157
const COFFConfig &COFFConfig, Object &Obj) {
158+
for (StringRef Op : Config.DumpSection) {
159+
auto [Section, File] = Op.split('=');
160+
if (Error E = dumpSection(Obj, Section, File))
161+
return E;
162+
}
163+
135164
// Perform the actual section removals.
136165
Obj.removeSections([&Config](const Section &Sec) {
137166
// Contrary to --only-keep-debug, --only-section fully removes sections that

llvm/lib/ObjCopy/ConfigManager.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ namespace objcopy {
1515

1616
Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
1717
if (!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
18-
!Common.AllocSectionsPrefix.empty() || !Common.DumpSection.empty() ||
19-
!Common.KeepSection.empty() || !Common.SymbolsToGlobalize.empty() ||
20-
!Common.SymbolsToKeep.empty() || !Common.SymbolsToLocalize.empty() ||
21-
!Common.SymbolsToWeaken.empty() || !Common.SymbolsToKeepGlobal.empty() ||
22-
!Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
23-
!Common.SetSectionType.empty() || Common.ExtractDWO ||
24-
Common.PreserveDates || Common.StripDWO || Common.StripNonAlloc ||
25-
Common.StripSections || Common.Weaken || Common.DecompressDebugSections ||
18+
!Common.AllocSectionsPrefix.empty() || !Common.KeepSection.empty() ||
19+
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToKeep.empty() ||
20+
!Common.SymbolsToLocalize.empty() || !Common.SymbolsToWeaken.empty() ||
21+
!Common.SymbolsToKeepGlobal.empty() || !Common.SectionsToRename.empty() ||
22+
!Common.SetSectionAlignment.empty() || !Common.SetSectionType.empty() ||
23+
Common.ExtractDWO || Common.PreserveDates || Common.StripDWO ||
24+
Common.StripNonAlloc || Common.StripSections || Common.Weaken ||
25+
Common.DecompressDebugSections ||
2626
Common.DiscardMode == DiscardType::Locals || !Common.SymbolsToAdd.empty())
2727
return createStringError(llvm::errc::invalid_argument,
2828
"option is not supported for COFF");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# RUN: yaml2obj %s -o %t.obj
2+
# RUN: llvm-objcopy --dump-section .data=%t.dat %t.obj
3+
# RUN: wc -c %t.dat | FileCheck %s --ignore-case -check-prefix CHECK-EMPTY-SIZE
4+
# RUN: llvm-objcopy --dump-section .text.f=%t.txt %t.obj
5+
# RUN: od -t x1 %t.txt | FileCheck %s --ignore-case -check-prefix CHECK-TEXT-F
6+
# RUN: not llvm-objcopy --dump-section non-existent=/dev/null %t.obj 2>&1 | FileCheck %s -check-prefix CHECK-NO-SECTION
7+
# RUN: not llvm-objcopy --dump-section .text=%T %t.obj 2>&1 | FileCheck -DOBJ=%t.obj -DMSG=%errc_EISDIR %s -check-prefix CHECK-INVALID-DESTINATION
8+
9+
# CHECK-EMPTY-SIZE: 0
10+
11+
# CHECK-TEXT-F: 0000000 b8 20 00 00 00 c3
12+
13+
# CHECK-NO-SECTION: section 'non-existent' not found
14+
15+
# CHECK-INVALID-DESTINATION: error: '[[OBJ]]': [[MSG]]
16+
17+
--- !COFF
18+
header:
19+
Machine: IMAGE_FILE_MACHINE_AMD64
20+
Characteristics: [ ]
21+
sections:
22+
- Name: .text
23+
Characteristics: [ ]
24+
Alignment: 4
25+
SectionData: ''
26+
- Name: .data
27+
Characteristics: [ ]
28+
Alignment: 4
29+
SectionData: ''
30+
- Name: .text.f
31+
Characteristics: [ ]
32+
Alignment: 16
33+
SectionData: B820000000C3
34+
symbols:
35+
...

0 commit comments

Comments
 (0)