-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[llvm-size] Add --exclude-pagezero option for Mach-O to exclude __PAGEZERO size. #159574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-llvm-binary-utilities Author: Ryan Mansfield (rjmansfield) ChangesFixes #86644 Full diff: https://github.com/llvm/llvm-project/pull/159574.diff 3 Files Affected:
diff --git a/llvm/test/tools/llvm-size/macho-pagezero.test b/llvm/test/tools/llvm-size/macho-pagezero.test
new file mode 100644
index 0000000000000..d53067504c3ad
--- /dev/null
+++ b/llvm/test/tools/llvm-size/macho-pagezero.test
@@ -0,0 +1,60 @@
+# Test the -z option to skip __PAGEZERO segment in Mach-O files
+
+# RUN: yaml2obj %s --docnum=1 -o %t-pagezero.o
+# RUN: llvm-size %t-pagezero.o | \
+# RUN: FileCheck %s --check-prefix=NORMAL --match-full-lines \
+# RUN: --strict-whitespace --implicit-check-not={{.}}
+# RUN: llvm-size -z %t-pagezero.o | \
+# RUN: FileCheck %s --check-prefix=SKIP --match-full-lines \
+# RUN: --strict-whitespace --implicit-check-not={{.}}
+
+# NORMAL:__TEXT __DATA __OBJC others dec hex
+# NORMAL-NEXT:20 100 0 4096 4216 1078
+
+# SKIP:__TEXT __DATA __OBJC others dec hex
+# SKIP-NEXT:20 100 0 0 120 78
+
+--- !mach-o
+FileHeader:
+ magic: 0xFEEDFACF
+ cputype: 0x100000C
+ cpusubtype: 0x0
+ filetype: 0x2
+ ncmds: 3
+ sizeofcmds: 216
+ flags: 0x2000
+ reserved: 0x0
+LoadCommands:
+ - cmd: LC_SEGMENT_64
+ cmdsize: 72
+ segname: __PAGEZERO
+ vmaddr: 0x0
+ vmsize: 4096
+ fileoff: 0
+ filesize: 0
+ maxprot: 0
+ initprot: 0
+ nsects: 0
+ flags: 0
+ - cmd: LC_SEGMENT_64
+ cmdsize: 72
+ segname: __TEXT
+ vmaddr: 0x100000000
+ vmsize: 20
+ fileoff: 248
+ filesize: 20
+ maxprot: 7
+ initprot: 5
+ nsects: 0
+ flags: 0
+ - cmd: LC_SEGMENT_64
+ cmdsize: 72
+ segname: __DATA
+ vmaddr: 0x100001000
+ vmsize: 100
+ fileoff: 268
+ filesize: 100
+ maxprot: 7
+ initprot: 3
+ nsects: 0
+ flags: 0
diff --git a/llvm/tools/llvm-size/Opts.td b/llvm/tools/llvm-size/Opts.td
index edae43f1abd24..65478730c2801 100644
--- a/llvm/tools/llvm-size/Opts.td
+++ b/llvm/tools/llvm-size/Opts.td
@@ -21,6 +21,8 @@ def grp_mach_o : OptionGroup<"kind">, HelpText<"OPTIONS (Mach-O specific)">;
def arch_EQ : Joined<["--"], "arch=">, HelpText<"architecture(s) from a Mach-O file to dump">, Group<grp_mach_o>;
def : Separate<["--", "-"], "arch">, Alias<arch_EQ>;
def l : F<"l", "When format is darwin, use long format to include addresses and offsets">, Group<grp_mach_o>;
+def z : F<"z", "Do not include __PAGEZERO segment in totals">,
+ Group<grp_mach_o>;
def : F<"A", "Alias for --format">, Alias<format_EQ>, AliasArgs<["sysv"]>;
def : F<"B", "Alias for --format">, Alias<format_EQ>, AliasArgs<["berkeley"]>;
diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp
index acc7843ffac8b..805f8ed1e6dcd 100644
--- a/llvm/tools/llvm-size/llvm-size.cpp
+++ b/llvm/tools/llvm-size/llvm-size.cpp
@@ -79,6 +79,7 @@ static bool DarwinLongFormat;
static RadixTy Radix = RadixTy::decimal;
static bool TotalSizes;
static bool HasMachOFiles = false;
+static bool SkipPageZero = false;
static std::vector<std::string> InputFilenames;
@@ -307,7 +308,9 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
}
} else {
StringRef SegmentName = StringRef(Seg.segname);
- if (SegmentName == "__TEXT")
+ if (SkipPageZero && SegmentName == "__PAGEZERO")
+ ; // Skip __PAGEZERO segment
+ else if (SegmentName == "__TEXT")
total_text += Seg.vmsize;
else if (SegmentName == "__DATA")
total_data += Seg.vmsize;
@@ -333,7 +336,9 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
}
} else {
StringRef SegmentName = StringRef(Seg.segname);
- if (SegmentName == "__TEXT")
+ if (SkipPageZero && SegmentName == "__PAGEZERO")
+ ; // Skip __PAGEZERO segment
+ else if (SegmentName == "__TEXT")
total_text += Seg.vmsize;
else if (SegmentName == "__DATA")
total_data += Seg.vmsize;
@@ -914,6 +919,7 @@ int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
ELFCommons = Args.hasArg(OPT_common);
DarwinLongFormat = Args.hasArg(OPT_l);
+ SkipPageZero = Args.hasArg(OPT_z);
TotalSizes = Args.hasArg(OPT_totals);
StringRef V = Args.getLastArgValue(OPT_format_EQ, "berkeley");
if (V == "berkeley")
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure to update the llvm-size command guide (llvm/docs/CommandGuide/llvm-size.rst) with the new option.
This will need a Mach-O developer to review, since I don't know the intricacies of the segment layout for these files. @drodriguez / @danzimm / @alexander-shaposhnikov, can any of you help?
# RUN: FileCheck %s --check-prefix=NORMAL --match-full-lines \ | ||
# RUN: --strict-whitespace --implicit-check-not={{.}} | ||
# RUN: llvm-size -z %t-pagezero.o | \ | ||
# RUN: FileCheck %s --check-prefix=SKIP --match-full-lines \ | ||
# RUN: --strict-whitespace --implicit-check-not={{.}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this test isn't about the format of the output, you can get away with omitting the --strict-whitespace
and --implicit-check-not
options. You might want to keep --match-full-lines
, so that you don't accidentally miss a leading/trailing digit in the first/last number.
If we use Xcode's
The man page for But in the Xcode man page one can also find the following:
Which explains the "others" category including If we want to provide this special behaviour because of a feature request, it should be obvious that it is not a compatibility flag or similar. |
Something like |
I'd go with |
Address code review comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation still needs updating, as stated in my earlier comment.
Also, the test only covers the LC_SEGMENT_64 case, but you've modified both that and the LC_SEGMENT path.
Add test for 32 bit __PAGEZERO segment.
llvm/docs/CommandGuide/llvm-size.rst
Outdated
.. option:: --exclude-pagezero | ||
|
||
Do not include the ``__PAGEZERO`` segment when calculating size information | ||
for Mach-O files. ``__PAGEZERO`` segment is a virtual memory region used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for Mach-O files. ``__PAGEZERO`` segment is a virtual memory region used | |
for Mach-O files. The ``__PAGEZERO`` segment is a virtual memory region used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks. Let me know if you need me to merge this for you again.
@jh7370 If you could merge it, I'd appreciate it. Thanks. |
…EZERO size. (llvm#159574) Do not include the ``__PAGEZERO`` segment when calculating size information for Mach-O files when `--exclude-pagezero` is used. The ``__PAGEZERO`` segment is a virtual memory region used for memory protection that does not contribute to actual size, and excluding can provide a better representation of actual size. Fixes llvm#86644
Fixes #86644