Teach llvm-size to know about Mach-O universal files (aka fat files) and
[oota-llvm.git] / tools / llvm-size / llvm-size.cpp
1 //===-- 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"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Object/MachOUniversal.h"
21 #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"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31 #include <string>
32 #include <system_error>
33 using namespace llvm;
34 using namespace object;
35
36 enum OutputFormatTy {berkeley, sysv, darwin};
37 static 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"),
42                     clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
43          cl::init(berkeley));
44
45 static 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));
51
52 static bool berkeleyHeaderPrinted = false;
53 static bool moreThanOneFile = false;
54
55 cl::opt<bool> DarwinLongFormat("l",
56          cl::desc("When format is darwin, use long format "
57                   "to include addresses and offsets."));
58
59 enum RadixTy {octal = 8, decimal = 10, hexadecimal = 16};
60 static 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));
64
65 static 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));
72
73 static cl::list<std::string>
74        InputFilenames(cl::Positional, cl::desc("<input files>"),
75                       cl::ZeroOrMore);
76
77 static std::string ToolName;
78
79 ///  @brief If ec is not success, print the error and return true.
80 static bool error(std::error_code ec) {
81   if (!ec) return false;
82
83   outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
84   outs().flush();
85   return true;
86 }
87
88 /// @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.
90 static size_t getNumLengthAsString(uint64_t num) {
91   APInt conv(64, num);
92   SmallString<32> result;
93   conv.toString(result, Radix, false, true);
94   return result.size();
95 }
96
97 /// @brief Return the the printing format for the Radix.
98 static 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.
114 static 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.
194 static 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
273 /// @brief Print the size of each section in @p Obj.
274 ///
275 /// The format used is determined by @c OutputFormat and @c Radix.
276 static void PrintObjectSectionSizes(ObjectFile *Obj) {
277   uint64_t total = 0;
278   std::string fmtbuf;
279   raw_string_ostream fmt(fmtbuf);
280   const char *radix_fmt = getRadixFmt();
281
282   // 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) {
293     // 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");
296     std::size_t max_size_len = strlen("size");
297     std::size_t max_addr_len = strlen("addr");
298     for (const SectionRef &Section : Obj->sections()) {
299       uint64_t size = 0;
300       if (error(Section.getSize(size)))
301         return;
302       total += size;
303
304       StringRef name;
305       uint64_t addr = 0;
306       if (error(Section.getName(name)))
307         return;
308       if (error(Section.getAddress(addr)))
309         return;
310       max_name_len = std::max(max_name_len, name.size());
311       max_size_len = std::max(max_size_len, getNumLengthAsString(size));
312       max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
313     }
314
315     // Add extra padding.
316     max_name_len += 2;
317     max_size_len += 2;
318     max_addr_len += 2;
319
320     // Setup header format.
321     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.
338     for (const SectionRef &Section : Obj->sections()) {
339       StringRef name;
340       uint64_t size = 0;
341       uint64_t addr = 0;
342       if (error(Section.getName(name)))
343         return;
344       if (error(Section.getSize(size)))
345         return;
346       if (error(Section.getAddress(addr)))
347         return;
348       std::string namestr = name;
349
350       outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
351     }
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 {
361     // The Berkeley format does not display individual section sizes. It
362     // displays the cumulative size for each section type.
363     uint64_t total_text = 0;
364     uint64_t total_data = 0;
365     uint64_t total_bss = 0;
366
367     // Make one pass over the section table to calculate sizes.
368     for (const SectionRef &Section : Obj->sections()) {
369       uint64_t size = 0;
370       bool isText = false;
371       bool isData = false;
372       bool isBSS = false;
373       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;
381       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
391     if (!berkeleyHeaderPrinted) {
392       outs() << "   text    data     bss     "
393              << (Radix == octal ? "oct" : "dec")
394              << "     hex filename\n";
395       berkeleyHeaderPrinted = true;
396     }
397
398     // 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();
407     fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
408         << "%7" PRIx64 " ";
409     outs() << format(fmt.str().c_str(),
410                      total,
411                      total);
412   }
413 }
414
415 /// @brief Print the section sizes for @p file. If @p file is an archive, print
416 ///        the section sizes for each archive member.
417 static 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
427   // Attempt to open the binary.
428   ErrorOr<Binary *> BinaryOrErr = createBinary(file);
429   if (std::error_code EC = BinaryOrErr.getError()) {
430     errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
431     return;
432   }
433   std::unique_ptr<Binary> binary(BinaryOrErr.get());
434
435   if (Archive *a = dyn_cast<Archive>(binary.get())) {
436     // This is an archive. Iterate over each member and display its sizes.
437     for (object::Archive::child_iterator i = a->child_begin(),
438                                          e = a->child_end(); i != e; ++i) {
439       ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
440       if (std::error_code EC = ChildOrErr.getError()) {
441         errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
442         continue;
443       }
444       if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
445         MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
446         if (OutputFormat == sysv)
447           outs() << o->getFileName() << "   (ex " << a->getFileName()
448                   << "):\n";
449         else if(MachO && OutputFormat == darwin)
450             outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
451         PrintObjectSectionSizes(o);
452         if (OutputFormat == berkeley) {
453           if (MachO)
454             outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
455           else
456             outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
457         }
458       }
459     }
460   } 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())) {
472           if (OutputFormat == sysv)
473             outs() << o->getFileName() << "  :\n";
474           PrintObjectSectionSizes(o);
475           if (OutputFormat == berkeley) {
476             MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
477             if (!MachO || moreThanOneFile || moreThanOneArch)
478               outs() << o->getFileName();
479             outs() << "\n";
480           }
481         }
482       }
483       else if (!I->getAsArchive(UA)) {
484         // This is an archive. Iterate over each member and display its sizes.
485         for (object::Archive::child_iterator i = UA->child_begin(),
486                                              e = UA->child_end(); i != e; ++i) {
487           ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
488           if (std::error_code EC = ChildOrErr.getError()) {
489             errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
490             continue;
491           }
492           if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
493             MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
494             if (OutputFormat == sysv)
495               outs() << o->getFileName() << "   (ex " << UA->getFileName()
496                      << "):\n";
497             else if(MachO && OutputFormat == darwin)
498               outs() << UA->getFileName() << "(" << o->getFileName() << "):\n";
499             PrintObjectSectionSizes(o);
500             if (OutputFormat == berkeley) {
501               if (MachO)
502                 outs() << UA->getFileName() << "(" << o->getFileName() << ")\n";
503               else
504                 outs() << o->getFileName() << " (ex " << UA->getFileName()
505                        << ")\n";
506             }
507           }
508         }
509       }
510     }
511   } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
512     if (OutputFormat == sysv)
513       outs() << o->getFileName() << "  :\n";
514     PrintObjectSectionSizes(o);
515     if (OutputFormat == berkeley) {
516       MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
517       if (!MachO || moreThanOneFile)
518         outs() << o->getFileName();
519       outs() << "\n";
520     }
521   } else {
522     errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n";
523   }
524   // System V adds an extra newline at the end of each file.
525   if (OutputFormat == sysv)
526     outs() << "\n";
527 }
528
529 int main(int argc, char **argv) {
530   // Print a stack trace if we signal out.
531   sys::PrintStackTraceOnErrorSignal();
532   PrettyStackTraceProgram X(argc, argv);
533
534   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
535   cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
536
537   ToolName = argv[0];
538   if (OutputFormatShort.getNumOccurrences())
539     OutputFormat = OutputFormatShort;
540   if (RadixShort.getNumOccurrences())
541     Radix = RadixShort;
542
543   if (InputFilenames.size() == 0)
544     InputFilenames.push_back("a.out");
545
546   moreThanOneFile = InputFilenames.size() > 1;
547   std::for_each(InputFilenames.begin(), InputFilenames.end(),
548                 PrintFileSectionSizes);
549
550   return 0;
551 }