blob: 408bb4a18804d180f80677423188529632471c73 [file] [log] [blame]
Michael J. Spencerc4ad4662011-09-28 20:57:46 +00001//===-- llvm-size.cpp - Print the size of each object section -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program is a utility that works like traditional Unix "size",
11// that is, it prints out the size of each section, and the total size of all
12// sections.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/ADT/APInt.h"
17#include "llvm/Object/Archive.h"
18#include "llvm/Object/ObjectFile.h"
Kevin Enderby246a4602014-06-17 17:54:13 +000019#include "llvm/Object/MachO.h"
Kevin Enderby4b8fc282014-06-18 22:04:40 +000020#include "llvm/Object/MachOUniversal.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000021#include "llvm/Support/Casting.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/Format.h"
25#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000028#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000030#include <algorithm>
31#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000032#include <system_error>
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000033using namespace llvm;
34using namespace object;
35
Kevin Enderby246a4602014-06-17 17:54:13 +000036enum OutputFormatTy {berkeley, sysv, darwin};
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000037static cl::opt<OutputFormatTy>
38 OutputFormat("format",
39 cl::desc("Specify output format"),
40 cl::values(clEnumVal(sysv, "System V format"),
41 clEnumVal(berkeley, "Berkeley format"),
Kevin Enderby246a4602014-06-17 17:54:13 +000042 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000043 cl::init(berkeley));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000044
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000045static cl::opt<OutputFormatTy>
46 OutputFormatShort(cl::desc("Specify output format"),
47 cl::values(clEnumValN(sysv, "A", "System V format"),
48 clEnumValN(berkeley, "B", "Berkeley format"),
49 clEnumValEnd),
50 cl::init(berkeley));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000051
Kevin Enderby246a4602014-06-17 17:54:13 +000052static bool berkeleyHeaderPrinted = false;
53static bool moreThanOneFile = false;
54
55cl::opt<bool> DarwinLongFormat("l",
56 cl::desc("When format is darwin, use long format "
57 "to include addresses and offsets."));
58
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000059enum RadixTy {octal = 8, decimal = 10, hexadecimal = 16};
60static cl::opt<unsigned int>
61 Radix("-radix",
62 cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
63 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000064
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000065static cl::opt<RadixTy>
66 RadixShort(cl::desc("Print size in radix:"),
67 cl::values(clEnumValN(octal, "o", "Print size in octal"),
68 clEnumValN(decimal, "d", "Print size in decimal"),
69 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
70 clEnumValEnd),
71 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000072
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000073static cl::list<std::string>
74 InputFilenames(cl::Positional, cl::desc("<input files>"),
75 cl::ZeroOrMore);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000076
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000077static std::string ToolName;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000078
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000079/// @brief If ec is not success, print the error and return true.
Rafael Espindola4453e42942014-06-13 03:07:50 +000080static bool error(std::error_code ec) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000081 if (!ec) return false;
82
83 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
84 outs().flush();
85 return true;
86}
87
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000088/// @brief Get the length of the string that represents @p num in Radix
89/// including the leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +000090static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000091 APInt conv(64, num);
92 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000093 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000094 return result.size();
95}
96
Kevin Enderby246a4602014-06-17 17:54:13 +000097/// @brief Return the the printing format for the Radix.
98static const char * getRadixFmt(void) {
99 switch (Radix) {
100 case octal:
101 return PRIo64;
102 case decimal:
103 return PRIu64;
104 case hexadecimal:
105 return PRIx64;
106 }
107 return nullptr;
108}
109
110/// @brief Print the size of each Mach-O segment and section in @p MachO.
111///
112/// This is when used when @c OutputFormat is darwin and produces the same
113/// output as darwin's size(1) -m output.
114static void PrintDarwinSectionSizes(MachOObjectFile *MachO) {
115 std::string fmtbuf;
116 raw_string_ostream fmt(fmtbuf);
117 const char *radix_fmt = getRadixFmt();
118 if (Radix == hexadecimal)
119 fmt << "0x";
120 fmt << "%" << radix_fmt;
121
122 uint32_t LoadCommandCount = MachO->getHeader().ncmds;
123 uint32_t Filetype = MachO->getHeader().filetype;
124 MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
125
126 uint64_t total = 0;
127 for (unsigned I = 0; ; ++I) {
128 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
129 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
130 outs() << "Segment " << Seg.segname << ": "
131 << format(fmt.str().c_str(), Seg.vmsize);
132 if (DarwinLongFormat)
133 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr)
134 << " fileoff " << Seg.fileoff << ")";
135 outs() << "\n";
136 total += Seg.vmsize;
137 uint64_t sec_total = 0;
138 for (unsigned J = 0; J < Seg.nsects; ++J) {
139 MachO::section_64 Sec = MachO->getSection64(Load, J);
140 if (Filetype == MachO::MH_OBJECT)
141 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
142 << format("%.16s", &Sec.sectname) << "): ";
143 else
144 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
145 outs() << format(fmt.str().c_str(), Sec.size);
146 if (DarwinLongFormat)
147 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr)
148 << " offset " << Sec.offset << ")";
149 outs() << "\n";
150 sec_total += Sec.size;
151 }
152 if (Seg.nsects != 0)
153 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
154 }
155 else if (Load.C.cmd == MachO::LC_SEGMENT) {
156 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
157 outs() << "Segment " << Seg.segname << ": "
158 << format(fmt.str().c_str(), Seg.vmsize);
159 if (DarwinLongFormat)
160 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr)
161 << " fileoff " << Seg.fileoff << ")";
162 outs() << "\n";
163 total += Seg.vmsize;
164 uint64_t sec_total = 0;
165 for (unsigned J = 0; J < Seg.nsects; ++J) {
166 MachO::section Sec = MachO->getSection(Load, J);
167 if (Filetype == MachO::MH_OBJECT)
168 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
169 << format("%.16s", &Sec.sectname) << "): ";
170 else
171 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
172 outs() << format(fmt.str().c_str(), Sec.size);
173 if (DarwinLongFormat)
174 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr)
175 << " offset " << Sec.offset << ")";
176 outs() << "\n";
177 sec_total += Sec.size;
178 }
179 if (Seg.nsects != 0)
180 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
181 }
182 if (I == LoadCommandCount - 1)
183 break;
184 else
185 Load = MachO->getNextLoadCommandInfo(Load);
186 }
187 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
188}
189
190/// @brief Print the summary sizes of the standard Mach-O segments in @p MachO.
191///
192/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
193/// produces the same output as darwin's size(1) default output.
194static void PrintDarwinSegmentSizes(MachOObjectFile *MachO) {
195 uint32_t LoadCommandCount = MachO->getHeader().ncmds;
196 MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
197
198 uint64_t total_text = 0;
199 uint64_t total_data = 0;
200 uint64_t total_objc = 0;
201 uint64_t total_others = 0;
202 for (unsigned I = 0; ; ++I) {
203 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
204 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
205 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
206 for (unsigned J = 0; J < Seg.nsects; ++J) {
207 MachO::section_64 Sec = MachO->getSection64(Load, J);
208 StringRef SegmentName = StringRef(Sec.segname);
209 if (SegmentName == "__TEXT")
210 total_text += Sec.size;
211 else if (SegmentName == "__DATA")
212 total_data += Sec.size;
213 else if (SegmentName == "__OBJC")
214 total_objc += Sec.size;
215 else
216 total_others += Sec.size;
217 }
218 } else {
219 StringRef SegmentName = StringRef(Seg.segname);
220 if (SegmentName == "__TEXT")
221 total_text += Seg.vmsize;
222 else if (SegmentName == "__DATA")
223 total_data += Seg.vmsize;
224 else if (SegmentName == "__OBJC")
225 total_objc += Seg.vmsize;
226 else
227 total_others += Seg.vmsize;
228 }
229 }
230 else if (Load.C.cmd == MachO::LC_SEGMENT) {
231 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
232 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
233 for (unsigned J = 0; J < Seg.nsects; ++J) {
234 MachO::section Sec = MachO->getSection(Load, J);
235 StringRef SegmentName = StringRef(Sec.segname);
236 if (SegmentName == "__TEXT")
237 total_text += Sec.size;
238 else if (SegmentName == "__DATA")
239 total_data += Sec.size;
240 else if (SegmentName == "__OBJC")
241 total_objc += Sec.size;
242 else
243 total_others += Sec.size;
244 }
245 } else {
246 StringRef SegmentName = StringRef(Seg.segname);
247 if (SegmentName == "__TEXT")
248 total_text += Seg.vmsize;
249 else if (SegmentName == "__DATA")
250 total_data += Seg.vmsize;
251 else if (SegmentName == "__OBJC")
252 total_objc += Seg.vmsize;
253 else
254 total_others += Seg.vmsize;
255 }
256 }
257 if (I == LoadCommandCount - 1)
258 break;
259 else
260 Load = MachO->getNextLoadCommandInfo(Load);
261 }
262 uint64_t total = total_text + total_data + total_objc + total_others;
263
264 if (!berkeleyHeaderPrinted) {
265 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
266 berkeleyHeaderPrinted = true;
267 }
268 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
269 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
270 << "\t";
271}
272
Alexey Samsonov48803e52014-03-13 14:37:36 +0000273/// @brief Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000274///
275/// The format used is determined by @c OutputFormat and @c Radix.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000276static void PrintObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000277 uint64_t total = 0;
278 std::string fmtbuf;
279 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000280 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000281
Kevin Enderby246a4602014-06-17 17:54:13 +0000282 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
283 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
284 // let it fall through to OutputFormat berkeley.
285 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
286 if (OutputFormat == darwin && MachO)
287 PrintDarwinSectionSizes(MachO);
288 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
289 // darwin's default berkeley format for Mach-O files.
290 else if (MachO && OutputFormat == berkeley)
291 PrintDarwinSegmentSizes(MachO);
292 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000293 // Run two passes over all sections. The first gets the lengths needed for
294 // formatting the output. The second actually does the output.
295 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000296 std::size_t max_size_len = strlen("size");
297 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000298 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000299 uint64_t size = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000300 if (error(Section.getSize(size)))
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000301 return;
302 total += size;
303
304 StringRef name;
305 uint64_t addr = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000306 if (error(Section.getName(name)))
307 return;
308 if (error(Section.getAddress(addr)))
309 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000310 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000311 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
312 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000313 }
314
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000315 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000316 max_name_len += 2;
317 max_size_len += 2;
318 max_addr_len += 2;
319
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000320 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000321 fmt << "%-" << max_name_len << "s "
322 << "%" << max_size_len << "s "
323 << "%" << max_addr_len << "s\n";
324
325 // Print header
326 outs() << format(fmt.str().c_str(),
327 static_cast<const char*>("section"),
328 static_cast<const char*>("size"),
329 static_cast<const char*>("addr"));
330 fmtbuf.clear();
331
332 // Setup per section format.
333 fmt << "%-" << max_name_len << "s "
334 << "%#" << max_size_len << radix_fmt << " "
335 << "%#" << max_addr_len << radix_fmt << "\n";
336
337 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000338 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000339 StringRef name;
340 uint64_t size = 0;
341 uint64_t addr = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000342 if (error(Section.getName(name)))
343 return;
344 if (error(Section.getSize(size)))
345 return;
346 if (error(Section.getAddress(addr)))
347 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000348 std::string namestr = name;
349
Alexey Samsonov48803e52014-03-13 14:37:36 +0000350 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000351 }
352
353 // Print total.
354 fmtbuf.clear();
355 fmt << "%-" << max_name_len << "s "
356 << "%#" << max_size_len << radix_fmt << "\n";
357 outs() << format(fmt.str().c_str(),
358 static_cast<const char*>("Total"),
359 total);
360 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000361 // The Berkeley format does not display individual section sizes. It
362 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000363 uint64_t total_text = 0;
364 uint64_t total_data = 0;
365 uint64_t total_bss = 0;
366
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000367 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000368 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000369 uint64_t size = 0;
370 bool isText = false;
371 bool isData = false;
372 bool isBSS = false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000373 if (error(Section.getSize(size)))
374 return;
375 if (error(Section.isText(isText)))
376 return;
377 if (error(Section.isData(isData)))
378 return;
379 if (error(Section.isBSS(isBSS)))
380 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000381 if (isText)
382 total_text += size;
383 else if (isData)
384 total_data += size;
385 else if (isBSS)
386 total_bss += size;
387 }
388
389 total = total_text + total_data + total_bss;
390
Kevin Enderby246a4602014-06-17 17:54:13 +0000391 if (!berkeleyHeaderPrinted) {
392 outs() << " text data bss "
393 << (Radix == octal ? "oct" : "dec")
394 << " hex filename\n";
395 berkeleyHeaderPrinted = true;
396 }
397
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000398 // Print result.
399 fmt << "%#7" << radix_fmt << " "
400 << "%#7" << radix_fmt << " "
401 << "%#7" << radix_fmt << " ";
402 outs() << format(fmt.str().c_str(),
403 total_text,
404 total_data,
405 total_bss);
406 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000407 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
408 << "%7" PRIx64 " ";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000409 outs() << format(fmt.str().c_str(),
410 total,
411 total);
412 }
413}
414
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000415/// @brief Print the section sizes for @p file. If @p file is an archive, print
416/// the section sizes for each archive member.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000417static void PrintFileSectionSizes(StringRef file) {
418 // If file is not stdin, check that it exists.
419 if (file != "-") {
420 bool exists;
421 if (sys::fs::exists(file, exists) || !exists) {
422 errs() << ToolName << ": '" << file << "': " << "No such file\n";
423 return;
424 }
425 }
426
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000427 // Attempt to open the binary.
Rafael Espindola63da2952014-01-15 19:37:43 +0000428 ErrorOr<Binary *> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000429 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000430 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000431 return;
432 }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000433 std::unique_ptr<Binary> binary(BinaryOrErr.get());
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000434
435 if (Archive *a = dyn_cast<Archive>(binary.get())) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000436 // This is an archive. Iterate over each member and display its sizes.
Rafael Espindola23a97502014-01-21 16:09:45 +0000437 for (object::Archive::child_iterator i = a->child_begin(),
438 e = a->child_end(); i != e; ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000439 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
440 if (std::error_code EC = ChildOrErr.getError()) {
441 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000442 continue;
443 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000444 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000445 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000446 if (OutputFormat == sysv)
447 outs() << o->getFileName() << " (ex " << a->getFileName()
448 << "):\n";
Kevin Enderby246a4602014-06-17 17:54:13 +0000449 else if(MachO && OutputFormat == darwin)
450 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000451 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000452 if (OutputFormat == berkeley) {
453 if (MachO)
454 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
455 else
456 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
457 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000458 }
459 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000460 } else if (MachOUniversalBinary *UB =
461 dyn_cast<MachOUniversalBinary>(binary.get())) {
462 // This is a Mach-O universal binary. Iterate over each object and
463 // display its sizes.
464 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
465 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
466 E = UB->end_objects();
467 I != E; ++I) {
468 std::unique_ptr<ObjectFile> UO;
469 std::unique_ptr<Archive> UA;
470 if (!I->getAsObjectFile(UO)) {
471 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000472 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000473 if (OutputFormat == sysv)
474 outs() << o->getFileName() << " :\n";
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000475 else if(MachO && OutputFormat == darwin) {
476 if (moreThanOneFile || moreThanOneArch)
477 outs() << o->getFileName() << " (for architecture "
478 << I->getArchTypeName() << "):";
479 outs() << "\n";
480 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000481 PrintObjectSectionSizes(o);
482 if (OutputFormat == berkeley) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000483 if (!MachO || moreThanOneFile || moreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000484 outs() << o->getFileName() << " (for architecture "
485 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000486 outs() << "\n";
487 }
488 }
489 }
490 else if (!I->getAsArchive(UA)) {
491 // This is an archive. Iterate over each member and display its sizes.
492 for (object::Archive::child_iterator i = UA->child_begin(),
493 e = UA->child_end(); i != e; ++i) {
494 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
495 if (std::error_code EC = ChildOrErr.getError()) {
496 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
497 continue;
498 }
499 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
500 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
501 if (OutputFormat == sysv)
502 outs() << o->getFileName() << " (ex " << UA->getFileName()
503 << "):\n";
504 else if(MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000505 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
506 << " (for architecture " << I->getArchTypeName()
507 << "):\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000508 PrintObjectSectionSizes(o);
509 if (OutputFormat == berkeley) {
510 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000511 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
512 << " (for architecture " << I->getArchTypeName()
513 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000514 else
515 outs() << o->getFileName() << " (ex " << UA->getFileName()
516 << ")\n";
517 }
518 }
519 }
520 }
521 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000522 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
523 if (OutputFormat == sysv)
524 outs() << o->getFileName() << " :\n";
525 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000526 if (OutputFormat == berkeley) {
527 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
528 if (!MachO || moreThanOneFile)
529 outs() << o->getFileName();
530 outs() << "\n";
531 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000532 } else {
533 errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n";
534 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000535 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000536 if (OutputFormat == sysv)
537 outs() << "\n";
538}
539
540int main(int argc, char **argv) {
541 // Print a stack trace if we signal out.
542 sys::PrintStackTraceOnErrorSignal();
543 PrettyStackTraceProgram X(argc, argv);
544
545 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
546 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
547
548 ToolName = argv[0];
549 if (OutputFormatShort.getNumOccurrences())
550 OutputFormat = OutputFormatShort;
551 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000552 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000553
554 if (InputFilenames.size() == 0)
555 InputFilenames.push_back("a.out");
556
Kevin Enderby246a4602014-06-17 17:54:13 +0000557 moreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000558 std::for_each(InputFilenames.begin(), InputFilenames.end(),
559 PrintFileSectionSizes);
560
561 return 0;
562}