llvm.org GIT mirror llvm / 9c23bd0
Run clang-format on llvm-size.cpp to tidy things up. No functional changes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212141 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 5 years ago
1 changed file(s) with 92 addition(s) and 109 deletion(s). Raw diff Collapse all Expand all
3232 using namespace llvm;
3333 using namespace object;
3434
35 enum OutputFormatTy {berkeley, sysv, darwin};
35 enum OutputFormatTy { berkeley, sysv, darwin };
3636 static cl::opt
37 OutputFormat("format",
38 cl::desc("Specify output format"),
39 cl::values(clEnumVal(sysv, "System V format"),
40 clEnumVal(berkeley, "Berkeley format"),
41 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
42 cl::init(berkeley));
43
44 static cl::opt
45 OutputFormatShort(cl::desc("Specify output format"),
46 cl::values(clEnumValN(sysv, "A", "System V format"),
47 clEnumValN(berkeley, "B", "Berkeley format"),
48 clEnumValN(darwin, "m", "Darwin -m format"),
49 clEnumValEnd),
50 cl::init(berkeley));
37 OutputFormat("format", cl::desc("Specify output format"),
38 cl::values(clEnumVal(sysv, "System V format"),
39 clEnumVal(berkeley, "Berkeley format"),
40 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
41 cl::init(berkeley));
42
43 static cl::opt OutputFormatShort(
44 cl::desc("Specify output format"),
45 cl::values(clEnumValN(sysv, "A", "System V format"),
46 clEnumValN(berkeley, "B", "Berkeley format"),
47 clEnumValN(darwin, "m", "Darwin -m format"), clEnumValEnd),
48 cl::init(berkeley));
5149
5250 static bool berkeleyHeaderPrinted = false;
5351 static bool moreThanOneFile = false;
5452
55 cl::opt DarwinLongFormat("l",
56 cl::desc("When format is darwin, use long format "
57 "to include addresses and offsets."));
53 cl::opt
54 DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
55 "to include addresses and offsets."));
5856
5957 static cl::list
60 ArchFlags("arch",
61 cl::desc("architecture(s) from a Mach-O file to dump"),
62 cl::ZeroOrMore);
58 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
59 cl::ZeroOrMore);
6360 bool ArchAll = false;
6461
65 enum RadixTy {octal = 8, decimal = 10, hexadecimal = 16};
62 enum RadixTy { octal = 8, decimal = 10, hexadecimal = 16 };
6663 static cl::opt
67 Radix("-radix",
68 cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
69 cl::init(decimal));
64 Radix("-radix", cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
65 cl::init(decimal));
7066
7167 static cl::opt
72 RadixShort(cl::desc("Print size in radix:"),
73 cl::values(clEnumValN(octal, "o", "Print size in octal"),
74 clEnumValN(decimal, "d", "Print size in decimal"),
75 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
76 clEnumValEnd),
77 cl::init(decimal));
68 RadixShort(cl::desc("Print size in radix:"),
69 cl::values(clEnumValN(octal, "o", "Print size in octal"),
70 clEnumValN(decimal, "d", "Print size in decimal"),
71 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
72 clEnumValEnd),
73 cl::init(decimal));
7874
7975 static cl::list
80 InputFilenames(cl::Positional, cl::desc(""),
81 cl::ZeroOrMore);
76 InputFilenames(cl::Positional, cl::desc(""), cl::ZeroOrMore);
8277
8378 static std::string ToolName;
8479
8580 /// @brief If ec is not success, print the error and return true.
8681 static bool error(std::error_code ec) {
87 if (!ec) return false;
82 if (!ec)
83 return false;
8884
8985 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
9086 outs().flush();
10197 }
10298
10399 /// @brief Return the the printing format for the Radix.
104 static const char * getRadixFmt(void) {
100 static const char *getRadixFmt(void) {
105101 switch (Radix) {
106102 case octal:
107103 return PRIo64;
130126 MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
131127
132128 uint64_t total = 0;
133 for (unsigned I = 0; ; ++I) {
129 for (unsigned I = 0;; ++I) {
134130 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
135131 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
136132 outs() << "Segment " << Seg.segname << ": "
137133 << format(fmt.str().c_str(), Seg.vmsize);
138134 if (DarwinLongFormat)
139 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr)
140 << " fileoff " << Seg.fileoff << ")";
135 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
136 << Seg.fileoff << ")";
141137 outs() << "\n";
142138 total += Seg.vmsize;
143139 uint64_t sec_total = 0;
150146 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
151147 outs() << format(fmt.str().c_str(), Sec.size);
152148 if (DarwinLongFormat)
153 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr)
154 << " offset " << Sec.offset << ")";
149 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
150 << Sec.offset << ")";
155151 outs() << "\n";
156152 sec_total += Sec.size;
157153 }
158154 if (Seg.nsects != 0)
159155 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
160 }
161 else if (Load.C.cmd == MachO::LC_SEGMENT) {
156 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
162157 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
163158 outs() << "Segment " << Seg.segname << ": "
164159 << format(fmt.str().c_str(), Seg.vmsize);
165160 if (DarwinLongFormat)
166 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr)
167 << " fileoff " << Seg.fileoff << ")";
161 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
162 << Seg.fileoff << ")";
168163 outs() << "\n";
169164 total += Seg.vmsize;
170165 uint64_t sec_total = 0;
177172 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
178173 outs() << format(fmt.str().c_str(), Sec.size);
179174 if (DarwinLongFormat)
180 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr)
181 << " offset " << Sec.offset << ")";
175 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
176 << Sec.offset << ")";
182177 outs() << "\n";
183178 sec_total += Sec.size;
184179 }
205200 uint64_t total_data = 0;
206201 uint64_t total_objc = 0;
207202 uint64_t total_others = 0;
208 for (unsigned I = 0; ; ++I) {
203 for (unsigned I = 0;; ++I) {
209204 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
210205 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
211206 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
220215 total_objc += Sec.size;
221216 else
222217 total_others += Sec.size;
223 }
218 }
224219 } else {
225220 StringRef SegmentName = StringRef(Seg.segname);
226221 if (SegmentName == "__TEXT")
232227 else
233228 total_others += Seg.vmsize;
234229 }
235 }
236 else if (Load.C.cmd == MachO::LC_SEGMENT) {
230 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
237231 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
238232 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
239233 for (unsigned J = 0; J < Seg.nsects; ++J) {
247241 total_objc += Sec.size;
248242 else
249243 total_others += Sec.size;
250 }
244 }
251245 } else {
252246 StringRef SegmentName = StringRef(Seg.segname);
253247 if (SegmentName == "__TEXT")
329323 << "%" << max_addr_len << "s\n";
330324
331325 // Print header
332 outs() << format(fmt.str().c_str(),
333 static_cast("section"),
334 static_cast("size"),
335 static_cast("addr"));
326 outs() << format(fmt.str().c_str(), static_cast("section"),
327 static_cast("size"),
328 static_cast("addr"));
336329 fmtbuf.clear();
337330
338331 // Setup per section format.
360353 fmtbuf.clear();
361354 fmt << "%-" << max_name_len << "s "
362355 << "%#" << max_size_len << radix_fmt << "\n";
363 outs() << format(fmt.str().c_str(),
364 static_cast*>("Total"),
356 outs() << format(fmt.str().c_str(), static_cast*>("Total"),
365357 total);
366358 } else {
367359 // The Berkeley format does not display individual section sizes. It
396388
397389 if (!berkeleyHeaderPrinted) {
398390 outs() << " text data bss "
399 << (Radix == octal ? "oct" : "dec")
400 << " hex filename\n";
391 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
401392 berkeleyHeaderPrinted = true;
402393 }
403394
405396 fmt << "%#7" << radix_fmt << " "
406397 << "%#7" << radix_fmt << " "
407398 << "%#7" << radix_fmt << " ";
408 outs() << format(fmt.str().c_str(),
409 total_text,
410 total_data,
411 total_bss);
399 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
412400 fmtbuf.clear();
413401 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
414402 << "%7" PRIx64 " ";
415 outs() << format(fmt.str().c_str(),
416 total,
417 total);
403 outs() << format(fmt.str().c_str(), total, total);
418404 }
419405 }
420406
438424 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
439425 }
440426 unsigned i;
441 for (i = 0; i < ArchFlags.size(); ++i){
427 for (i = 0; i < ArchFlags.size(); ++i) {
442428 if (ArchFlags[i] == T.getArchName())
443429 ArchFound = true;
444430 break;
459445 if (file != "-") {
460446 bool exists;
461447 if (sys::fs::exists(file, exists) || !exists) {
462 errs() << ToolName << ": '" << file << "': " << "No such file\n";
448 errs() << ToolName << ": '" << file << "': "
449 << "No such file\n";
463450 return;
464451 }
465452 }
475462 if (Archive *a = dyn_cast(binary.get())) {
476463 // This is an archive. Iterate over each member and display its sizes.
477464 for (object::Archive::child_iterator i = a->child_begin(),
478 e = a->child_end(); i != e; ++i) {
465 e = a->child_end();
466 i != e; ++i) {
479467 ErrorOr> ChildOrErr = i->getAsBinary();
480468 if (std::error_code EC = ChildOrErr.getError()) {
481469 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
486474 if (!checkMachOAndArchFlags(o, file))
487475 return;
488476 if (OutputFormat == sysv)
489 outs() << o->getFileName() << " (ex " << a->getFileName()
490 << "):\n";
491 else if(MachO && OutputFormat == darwin)
492 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
477 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
478 else if (MachO && OutputFormat == darwin)
479 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
493480 PrintObjectSectionSizes(o);
494481 if (OutputFormat == berkeley) {
495482 if (MachO)
500487 }
501488 }
502489 } else if (MachOUniversalBinary *UB =
503 dyn_cast(binary.get())) {
490 dyn_cast(binary.get())) {
504491 // If we have a list of architecture flags specified dump only those.
505492 if (!ArchAll && ArchFlags.size() != 0) {
506493 // Look for a slice in the universal binary that matches each ArchFlag.
507494 bool ArchFound;
508 for (unsigned i = 0; i < ArchFlags.size(); ++i){
495 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
509496 ArchFound = false;
510497 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
511498 E = UB->end_objects();
512499 I != E; ++I) {
513 if (ArchFlags[i] == I->getArchTypeName()){
500 if (ArchFlags[i] == I->getArchTypeName()) {
514501 ArchFound = true;
515502 ErrorOr> UO = I->getAsObjectFile();
516503 std::unique_ptr UA;
519506 MachOObjectFile *MachO = dyn_cast(o);
520507 if (OutputFormat == sysv)
521508 outs() << o->getFileName() << " :\n";
522 else if(MachO && OutputFormat == darwin) {
509 else if (MachO && OutputFormat == darwin) {
523510 if (moreThanOneFile || ArchFlags.size() > 1)
524511 outs() << o->getFileName() << " (for architecture "
525512 << I->getArchTypeName() << "): \n";
532519 outs() << "\n";
533520 }
534521 }
535 }
536 else if (!I->getAsArchive(UA)) {
522 } else if (!I->getAsArchive(UA)) {
537523 // This is an archive. Iterate over each member and display its
538 //sizes.
524 // sizes.
539525 for (object::Archive::child_iterator i = UA->child_begin(),
540526 e = UA->child_end();
541 i != e; ++i) {
527 i != e; ++i) {
542528 ErrorOr> ChildOrErr = i->getAsBinary();
543529 if (std::error_code EC = ChildOrErr.getError()) {
544530 errs() << ToolName << ": " << file << ": " << EC.message()
550536 if (OutputFormat == sysv)
551537 outs() << o->getFileName() << " (ex " << UA->getFileName()
552538 << "):\n";
553 else if(MachO && OutputFormat == darwin)
539 else if (MachO && OutputFormat == darwin)
554540 outs() << UA->getFileName() << "(" << o->getFileName()
555 << ")" << " (for architecture "
556 << I->getArchTypeName() << "):\n";
541 << ")"
542 << " (for architecture " << I->getArchTypeName()
543 << "):\n";
557544 PrintObjectSectionSizes(o);
558545 if (OutputFormat == berkeley) {
559546 if (MachO) {
560547 outs() << UA->getFileName() << "(" << o->getFileName()
561548 << ")";
562549 if (ArchFlags.size() > 1)
563 outs() << " (for architecture "
564 << I->getArchTypeName() << ")";
550 outs() << " (for architecture " << I->getArchTypeName()
551 << ")";
565552 outs() << "\n";
566 }
567 else
553 } else
568554 outs() << o->getFileName() << " (ex " << UA->getFileName()
569555 << ")\n";
570556 }
584570 // No architecture flags were specified so if this contains a slice that
585571 // matches the host architecture dump only that.
586572 if (!ArchAll) {
587 StringRef HostArchName =
588 MachOObjectFile::getHostArch().getArchName();
573 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
589574 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
590575 E = UB->end_objects();
591576 I != E; ++I) {
592 if (HostArchName == I->getArchTypeName()){
577 if (HostArchName == I->getArchTypeName()) {
593578 ErrorOr> UO = I->getAsObjectFile();
594579 std::unique_ptr UA;
595580 if (UO) {
597582 MachOObjectFile *MachO = dyn_cast(o);
598583 if (OutputFormat == sysv)
599584 outs() << o->getFileName() << " :\n";
600 else if(MachO && OutputFormat == darwin) {
585 else if (MachO && OutputFormat == darwin) {
601586 if (moreThanOneFile)
602587 outs() << o->getFileName() << " (for architecture "
603588 << I->getArchTypeName() << "):\n";
610595 outs() << "\n";
611596 }
612597 }
613 }
614 else if (!I->getAsArchive(UA)) {
598 } else if (!I->getAsArchive(UA)) {
615599 // This is an archive. Iterate over each member and display its
616600 // sizes.
617601 for (object::Archive::child_iterator i = UA->child_begin(),
618602 e = UA->child_end();
619 i != e; ++i) {
603 i != e; ++i) {
620604 ErrorOr> ChildOrErr = i->getAsBinary();
621605 if (std::error_code EC = ChildOrErr.getError()) {
622606 errs() << ToolName << ": " << file << ": " << EC.message()
628612 if (OutputFormat == sysv)
629613 outs() << o->getFileName() << " (ex " << UA->getFileName()
630614 << "):\n";
631 else if(MachO && OutputFormat == darwin)
615 else if (MachO && OutputFormat == darwin)
632616 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
633617 << " (for architecture " << I->getArchTypeName()
634618 << "):\n";
661645 MachOObjectFile *MachO = dyn_cast(o);
662646 if (OutputFormat == sysv)
663647 outs() << o->getFileName() << " :\n";
664 else if(MachO && OutputFormat == darwin) {
648 else if (MachO && OutputFormat == darwin) {
665649 if (moreThanOneFile || moreThanOneArch)
666650 outs() << o->getFileName() << " (for architecture "
667651 << I->getArchTypeName() << "):";
675659 outs() << "\n";
676660 }
677661 }
678 }
679 else if (!I->getAsArchive(UA)) {
662 } else if (!I->getAsArchive(UA)) {
680663 // This is an archive. Iterate over each member and display its sizes.
681664 for (object::Archive::child_iterator i = UA->child_begin(),
682 e = UA->child_end(); i != e; ++i) {
665 e = UA->child_end();
666 i != e; ++i) {
683667 ErrorOr> ChildOrErr = i->getAsBinary();
684668 if (std::error_code EC = ChildOrErr.getError()) {
685669 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
690674 if (OutputFormat == sysv)
691675 outs() << o->getFileName() << " (ex " << UA->getFileName()
692676 << "):\n";
693 else if(MachO && OutputFormat == darwin)
677 else if (MachO && OutputFormat == darwin)
694678 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
695 << " (for architecture " << I->getArchTypeName()
696 << "):\n";
679 << " (for architecture " << I->getArchTypeName() << "):\n";
697680 PrintObjectSectionSizes(o);
698681 if (OutputFormat == berkeley) {
699682 if (MachO)
721704 outs() << "\n";
722705 }
723706 } else {
724 errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n";
707 errs() << ToolName << ": " << file << ": "
708 << "Unrecognized file type.\n";
725709 }
726710 // System V adds an extra newline at the end of each file.
727711 if (OutputFormat == sysv)
733717 sys::PrintStackTraceOnErrorSignal();
734718 PrettyStackTraceProgram X(argc, argv);
735719
736 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
720 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
737721 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
738722
739723 ToolName = argv[0];
742726 if (RadixShort.getNumOccurrences())
743727 Radix = RadixShort;
744728
745 for (unsigned i = 0; i < ArchFlags.size(); ++i){
729 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
746730 if (ArchFlags[i] == "all") {
747731 ArchAll = true;
748 }
749 else {
732 } else {
750733 Triple T = MachOObjectFile::getArch(ArchFlags[i]);
751 if (T.getArch() == Triple::UnknownArch){
734 if (T.getArch() == Triple::UnknownArch) {
752735 outs() << ToolName << ": for the -arch option: Unknown architecture "
753736 << "named '" << ArchFlags[i] << "'";
754737 return 1;