Skip to content

Commit dcd0a2e

Browse files
rjmansfieldjh7370
andauthored
[llvm-size] Fix --totals option for Mach-O files (#157904)
The --totals option was not working for Mach-O files because the Darwin segment size calculation skipped the totals accumulation. --------- Co-authored-by: James Henderson <[email protected]>
1 parent 01b4b2a commit dcd0a2e

File tree

2 files changed

+182
-10
lines changed

2 files changed

+182
-10
lines changed

llvm/test/tools/llvm-size/totals.test

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
# CHECK-NEXT: [[FILE2]]
1717
# CHECK-NEXT: 18 36 72 126 7e (TOTALS)
1818

19+
# RUN: yaml2obj %s --docnum=3 -o %t-macho.o
20+
# RUN: yaml2obj %s --docnum=4 -o %t-macho2.o
21+
# RUN: llvm-size --totals %t-macho.o %t-macho2.o | \
22+
# RUN: FileCheck %s --check-prefix=MACHO-CHECK --strict-whitespace \
23+
# RUN: --match-full-lines --implicit-check-not={{.}} -DFILE1=%t-macho.o -DFILE2=%t-macho2.o
24+
25+
# MACHO-CHECK:__TEXT __DATA __OBJC others dec hex
26+
# MACHO-CHECK-NEXT:20 100 0 32 152 98 [[FILE1]]
27+
# MACHO-CHECK-NEXT:20 200 0 32 252 fc [[FILE2]]
28+
# MACHO-CHECK-NEXT:40 300 0 64 404 194 (TOTALS)
29+
1930
--- !ELF
2031
FileHeader:
2132
Class: ELFCLASS64
@@ -55,3 +66,141 @@ Sections:
5566
Type: SHT_NOBITS
5667
Flags: [SHF_ALLOC, SHF_WRITE]
5768
Size: 32
69+
70+
--- !mach-o
71+
FileHeader:
72+
magic: 0xFEEDFACF
73+
cputype: 0x100000C
74+
cpusubtype: 0x0
75+
filetype: 0x1
76+
ncmds: 2
77+
sizeofcmds: 352
78+
flags: 0x2000
79+
reserved: 0x0
80+
LoadCommands:
81+
- cmd: LC_SEGMENT_64
82+
cmdsize: 312
83+
segname: ''
84+
vmaddr: 0
85+
vmsize: 152
86+
fileoff: 384
87+
filesize: 152
88+
maxprot: 7
89+
initprot: 7
90+
nsects: 3
91+
flags: 0
92+
Sections:
93+
- sectname: __text
94+
segname: __TEXT
95+
addr: 0x0
96+
size: 20
97+
offset: 0x180
98+
align: 2
99+
reloff: 0x0
100+
nreloc: 0
101+
flags: 0x80000400
102+
reserved1: 0x0
103+
reserved2: 0x0
104+
reserved3: 0x0
105+
- sectname: __data
106+
segname: __DATA
107+
addr: 0x18
108+
size: 100
109+
offset: 0x198
110+
align: 2
111+
reloff: 0x0
112+
nreloc: 0
113+
flags: 0x0
114+
reserved1: 0x0
115+
reserved2: 0x0
116+
reserved3: 0x0
117+
- sectname: __compact_unwind
118+
segname: __LD
119+
addr: 0x20
120+
size: 32
121+
offset: 0x200
122+
align: 3
123+
reloff: 0x0
124+
nreloc: 0
125+
flags: 0x2000000
126+
reserved1: 0x0
127+
reserved2: 0x0
128+
reserved3: 0x0
129+
- cmd: LC_BUILD_VERSION
130+
cmdsize: 32
131+
platform: 1
132+
minos: 851968
133+
sdk: 983040
134+
ntools: 1
135+
Tools:
136+
- tool: 3
137+
version: 68157696
138+
139+
--- !mach-o
140+
FileHeader:
141+
magic: 0xFEEDFACF
142+
cputype: 0x100000C
143+
cpusubtype: 0x0
144+
filetype: 0x1
145+
ncmds: 2
146+
sizeofcmds: 352
147+
flags: 0x2000
148+
reserved: 0x0
149+
LoadCommands:
150+
- cmd: LC_SEGMENT_64
151+
cmdsize: 312
152+
segname: ''
153+
vmaddr: 0
154+
vmsize: 252
155+
fileoff: 384
156+
filesize: 252
157+
maxprot: 7
158+
initprot: 7
159+
nsects: 3
160+
flags: 0
161+
Sections:
162+
- sectname: __text
163+
segname: __TEXT
164+
addr: 0x0
165+
size: 20
166+
offset: 0x180
167+
align: 2
168+
reloff: 0x0
169+
nreloc: 0
170+
flags: 0x80000400
171+
reserved1: 0x0
172+
reserved2: 0x0
173+
reserved3: 0x0
174+
- sectname: __data
175+
segname: __DATA
176+
addr: 0x18
177+
size: 200
178+
offset: 0x198
179+
align: 2
180+
reloff: 0x0
181+
nreloc: 0
182+
flags: 0x0
183+
reserved1: 0x0
184+
reserved2: 0x0
185+
reserved3: 0x0
186+
- sectname: __compact_unwind
187+
segname: __LD
188+
addr: 0x20
189+
size: 32
190+
offset: 0x260
191+
align: 3
192+
reloff: 0x0
193+
nreloc: 0
194+
flags: 0x2000000
195+
reserved1: 0x0
196+
reserved2: 0x0
197+
reserved3: 0x0
198+
- cmd: LC_BUILD_VERSION
199+
cmdsize: 32
200+
platform: 1
201+
minos: 851968
202+
sdk: 983040
203+
ntools: 1
204+
Tools:
205+
- tool: 3
206+
version: 68157696

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static OutputFormatTy OutputFormat;
7878
static bool DarwinLongFormat;
7979
static RadixTy Radix = RadixTy::decimal;
8080
static bool TotalSizes;
81+
static bool HasMachOFiles = false;
8182

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

@@ -92,6 +93,10 @@ static uint64_t TotalObjectData = 0;
9293
static uint64_t TotalObjectBss = 0;
9394
static uint64_t TotalObjectTotal = 0;
9495

96+
// Darwin-specific totals
97+
static uint64_t TotalObjectObjc = 0;
98+
static uint64_t TotalObjectOthers = 0;
99+
95100
static void error(const Twine &Message, StringRef File = "") {
96101
HadError = true;
97102
if (File.empty())
@@ -283,6 +288,7 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
283288
uint64_t total_data = 0;
284289
uint64_t total_objc = 0;
285290
uint64_t total_others = 0;
291+
HasMachOFiles = true;
286292
for (const auto &Load : MachO->load_commands()) {
287293
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
288294
MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
@@ -340,6 +346,14 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
340346
}
341347
uint64_t total = total_text + total_data + total_objc + total_others;
342348

349+
if (TotalSizes) {
350+
TotalObjectText += total_text;
351+
TotalObjectData += total_data;
352+
TotalObjectObjc += total_objc;
353+
TotalObjectOthers += total_others;
354+
TotalObjectTotal += total;
355+
}
356+
343357
if (!BerkeleyHeaderPrinted) {
344358
outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
345359
BerkeleyHeaderPrinted = true;
@@ -852,16 +866,25 @@ static void printBerkeleyTotals() {
852866
std::string fmtbuf;
853867
raw_string_ostream fmt(fmtbuf);
854868
const char *radix_fmt = getRadixFmt();
855-
fmt << "%#7" << radix_fmt << "\t"
856-
<< "%#7" << radix_fmt << "\t"
857-
<< "%#7" << radix_fmt << "\t";
858-
outs() << format(fmtbuf.c_str(), TotalObjectText, TotalObjectData,
859-
TotalObjectBss);
860-
fmtbuf.clear();
861-
fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t"
862-
<< "%7" PRIx64 "\t";
863-
outs() << format(fmtbuf.c_str(), TotalObjectTotal, TotalObjectTotal)
864-
<< "(TOTALS)\n";
869+
870+
if (HasMachOFiles) {
871+
// Darwin format totals: __TEXT __DATA __OBJC others dec hex
872+
outs() << TotalObjectText << "\t" << TotalObjectData << "\t"
873+
<< TotalObjectObjc << "\t" << TotalObjectOthers << "\t"
874+
<< TotalObjectTotal << "\t" << format("%" PRIx64, TotalObjectTotal)
875+
<< "\t(TOTALS)\n";
876+
} else {
877+
fmt << "%#7" << radix_fmt << "\t"
878+
<< "%#7" << radix_fmt << "\t"
879+
<< "%#7" << radix_fmt << "\t";
880+
outs() << format(fmtbuf.c_str(), TotalObjectText, TotalObjectData,
881+
TotalObjectBss);
882+
fmtbuf.clear();
883+
fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t"
884+
<< "%7" PRIx64 "\t";
885+
outs() << format(fmtbuf.c_str(), TotalObjectTotal, TotalObjectTotal)
886+
<< "(TOTALS)\n";
887+
}
865888
}
866889

867890
int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {

0 commit comments

Comments
 (0)