Skip to content

Commit 91ba62d

Browse files
committed
[llvm-size] Add -z option for Mach-O to exclude __PAGEZERO size.
Fixes #86644
1 parent d62505f commit 91ba62d

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Test the -z option to skip __PAGEZERO segment in Mach-O files
2+
3+
# RUN: yaml2obj %s --docnum=1 -o %t-pagezero.o
4+
# RUN: llvm-size %t-pagezero.o | \
5+
# RUN: FileCheck %s --check-prefix=NORMAL --match-full-lines \
6+
# RUN: --strict-whitespace --implicit-check-not={{.}}
7+
# RUN: llvm-size -z %t-pagezero.o | \
8+
# RUN: FileCheck %s --check-prefix=SKIP --match-full-lines \
9+
# RUN: --strict-whitespace --implicit-check-not={{.}}
10+
11+
# NORMAL:__TEXT __DATA __OBJC others dec hex
12+
# NORMAL-NEXT:20 100 0 4096 4216 1078
13+
14+
# SKIP:__TEXT __DATA __OBJC others dec hex
15+
# SKIP-NEXT:20 100 0 0 120 78
16+
17+
--- !mach-o
18+
FileHeader:
19+
magic: 0xFEEDFACF
20+
cputype: 0x100000C
21+
cpusubtype: 0x0
22+
filetype: 0x2
23+
ncmds: 3
24+
sizeofcmds: 216
25+
flags: 0x2000
26+
reserved: 0x0
27+
LoadCommands:
28+
- cmd: LC_SEGMENT_64
29+
cmdsize: 72
30+
segname: __PAGEZERO
31+
vmaddr: 0x0
32+
vmsize: 4096
33+
fileoff: 0
34+
filesize: 0
35+
maxprot: 0
36+
initprot: 0
37+
nsects: 0
38+
flags: 0
39+
- cmd: LC_SEGMENT_64
40+
cmdsize: 72
41+
segname: __TEXT
42+
vmaddr: 0x100000000
43+
vmsize: 20
44+
fileoff: 248
45+
filesize: 20
46+
maxprot: 7
47+
initprot: 5
48+
nsects: 0
49+
flags: 0
50+
- cmd: LC_SEGMENT_64
51+
cmdsize: 72
52+
segname: __DATA
53+
vmaddr: 0x100001000
54+
vmsize: 100
55+
fileoff: 268
56+
filesize: 100
57+
maxprot: 7
58+
initprot: 3
59+
nsects: 0
60+
flags: 0

llvm/tools/llvm-size/Opts.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def grp_mach_o : OptionGroup<"kind">, HelpText<"OPTIONS (Mach-O specific)">;
2121
def arch_EQ : Joined<["--"], "arch=">, HelpText<"architecture(s) from a Mach-O file to dump">, Group<grp_mach_o>;
2222
def : Separate<["--", "-"], "arch">, Alias<arch_EQ>;
2323
def l : F<"l", "When format is darwin, use long format to include addresses and offsets">, Group<grp_mach_o>;
24+
def z : F<"z", "Do not include __PAGEZERO segment in totals">,
25+
Group<grp_mach_o>;
2426

2527
def : F<"A", "Alias for --format">, Alias<format_EQ>, AliasArgs<["sysv"]>;
2628
def : F<"B", "Alias for --format">, Alias<format_EQ>, AliasArgs<["berkeley"]>;

llvm/tools/llvm-size/llvm-size.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static bool DarwinLongFormat;
7979
static RadixTy Radix = RadixTy::decimal;
8080
static bool TotalSizes;
8181
static bool HasMachOFiles = false;
82+
static bool SkipPageZero = false;
8283

8384
static std::vector<std::string> InputFilenames;
8485

@@ -307,7 +308,9 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
307308
}
308309
} else {
309310
StringRef SegmentName = StringRef(Seg.segname);
310-
if (SegmentName == "__TEXT")
311+
if (SkipPageZero && SegmentName == "__PAGEZERO")
312+
; // Skip __PAGEZERO segment
313+
else if (SegmentName == "__TEXT")
311314
total_text += Seg.vmsize;
312315
else if (SegmentName == "__DATA")
313316
total_data += Seg.vmsize;
@@ -333,7 +336,9 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
333336
}
334337
} else {
335338
StringRef SegmentName = StringRef(Seg.segname);
336-
if (SegmentName == "__TEXT")
339+
if (SkipPageZero && SegmentName == "__PAGEZERO")
340+
; // Skip __PAGEZERO segment
341+
else if (SegmentName == "__TEXT")
337342
total_text += Seg.vmsize;
338343
else if (SegmentName == "__DATA")
339344
total_data += Seg.vmsize;
@@ -914,6 +919,7 @@ int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
914919

915920
ELFCommons = Args.hasArg(OPT_common);
916921
DarwinLongFormat = Args.hasArg(OPT_l);
922+
SkipPageZero = Args.hasArg(OPT_z);
917923
TotalSizes = Args.hasArg(OPT_totals);
918924
StringRef V = Args.getLastArgValue(OPT_format_EQ, "berkeley");
919925
if (V == "berkeley")

0 commit comments

Comments
 (0)