Skip to content

Commit 12e5b02

Browse files
sammy-1234smeenai
authored andcommitted
[llvm-objcopy] Reorder --dump-section for MachO
Reorder `DumpSection` under `handleArgs` in file `MachOObjcopy.cpp`. The operation to dump a section is now performed before both add and remove section operations for MachO file format. Change for the ELF format at D81097. Together fixes https://bugs.llvm.org/show_bug.cgi?id=44283 Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D81123
1 parent eb7db87 commit 12e5b02

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# RUN: yaml2obj %s -o %t
2+
3+
## Verify that section is dumped before it is removed.
4+
# RUN: llvm-objcopy --dump-section __TEXT,__text=%t1.dump --remove-section __TEXT,__text %t %t1
5+
# RUN: FileCheck %s --input-file=%t1.dump --check-prefix=CONTENTS --implicit-check-not={{.}}
6+
7+
# CONTENTS: abcd
8+
9+
## Verify that the newly added section is not dumped.
10+
# RUN: echo CAFE > %t2.txt
11+
# RUN: not llvm-objcopy --dump-section __TEXT,__const=%t2.dump --add-section __TEXT,__const=%t2.txt %t %t2 2>&1 | \
12+
# RUN: FileCheck %s --check-prefix=NODUMP -DINPUT=%t
13+
14+
# NODUMP: error: '[[INPUT]]': section '__TEXT,__const' not found
15+
16+
--- !mach-o
17+
FileHeader:
18+
magic: 0xFEEDFACF
19+
cputype: 0x01000007
20+
cpusubtype: 0x00000003
21+
filetype: 0x00000001
22+
ncmds: 1
23+
sizeofcmds: 312
24+
flags: 0x00002000
25+
reserved: 0x00000000
26+
LoadCommands:
27+
- cmd: LC_SEGMENT_64
28+
cmdsize: 312
29+
segname: ''
30+
vmaddr: 0
31+
vmsize: 12
32+
fileoff: 344
33+
filesize: 12
34+
maxprot: 7
35+
initprot: 7
36+
nsects: 3
37+
flags: 0
38+
Sections:
39+
- sectname: __text
40+
segname: __TEXT
41+
addr: 0x0000000000000000
42+
content: '61626364'
43+
size: 4
44+
offset: 344
45+
align: 0
46+
reloff: 0x00000000
47+
nreloc: 0
48+
flags: 0x80000400
49+
reserved1: 0x00000000
50+
reserved2: 0x00000000
51+
- sectname: __data
52+
segname: __DATA
53+
addr: 0x0000000000000004
54+
content: 'EEFFEEFF'
55+
size: 4
56+
offset: 348
57+
align: 0
58+
reloff: 0x00000000
59+
nreloc: 0
60+
flags: 0x00000000
61+
reserved1: 0x00000000
62+
reserved2: 0x00000000

llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
187187
"option not supported by llvm-objcopy for MachO");
188188
}
189189

190+
// Dump sections before add/remove for compatibility with GNU objcopy.
191+
for (StringRef Flag : Config.DumpSection) {
192+
StringRef SectionName;
193+
StringRef FileName;
194+
std::tie(SectionName, FileName) = Flag.split('=');
195+
if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
196+
return E;
197+
}
198+
190199
if (Error E = removeSections(Config, Obj))
191200
return E;
192201

@@ -201,14 +210,6 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
201210
for (std::unique_ptr<Section> &Sec : LC.Sections)
202211
Sec->Relocations.clear();
203212

204-
for (const StringRef &Flag : Config.DumpSection) {
205-
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
206-
StringRef SecName = SecPair.first;
207-
StringRef File = SecPair.second;
208-
if (Error E = dumpSectionToFile(SecName, File, Obj))
209-
return E;
210-
}
211-
212213
for (const auto &Flag : Config.AddSection) {
213214
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
214215
StringRef SecName = SecPair.first;

0 commit comments

Comments
 (0)