162cfcf62SDimitry Andric //===-- llvm-size.cpp - Print the size of each object section ---*- C++ -*-===//
262cfcf62SDimitry Andric //
362cfcf62SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
462cfcf62SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
562cfcf62SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
662cfcf62SDimitry Andric //
762cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
862cfcf62SDimitry Andric //
962cfcf62SDimitry Andric // This program is a utility that works like traditional Unix "size",
1062cfcf62SDimitry Andric // that is, it prints out the size of each section, and the total size of all
1162cfcf62SDimitry Andric // sections.
1262cfcf62SDimitry Andric //
1362cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
1462cfcf62SDimitry Andric
1562cfcf62SDimitry Andric #include "llvm/ADT/APInt.h"
1662cfcf62SDimitry Andric #include "llvm/Object/Archive.h"
1762cfcf62SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
1862cfcf62SDimitry Andric #include "llvm/Object/MachO.h"
1962cfcf62SDimitry Andric #include "llvm/Object/MachOUniversal.h"
2062cfcf62SDimitry Andric #include "llvm/Object/ObjectFile.h"
21fe6060f1SDimitry Andric #include "llvm/Option/Arg.h"
22fe6060f1SDimitry Andric #include "llvm/Option/ArgList.h"
23fe6060f1SDimitry Andric #include "llvm/Option/Option.h"
2462cfcf62SDimitry Andric #include "llvm/Support/Casting.h"
2562cfcf62SDimitry Andric #include "llvm/Support/CommandLine.h"
2662cfcf62SDimitry Andric #include "llvm/Support/FileSystem.h"
2762cfcf62SDimitry Andric #include "llvm/Support/Format.h"
2806c3fb27SDimitry Andric #include "llvm/Support/LLVMDriver.h"
2962cfcf62SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
3062cfcf62SDimitry Andric #include "llvm/Support/WithColor.h"
3162cfcf62SDimitry Andric #include "llvm/Support/raw_ostream.h"
3262cfcf62SDimitry Andric #include <algorithm>
3362cfcf62SDimitry Andric #include <string>
3462cfcf62SDimitry Andric #include <system_error>
3562cfcf62SDimitry Andric
3662cfcf62SDimitry Andric using namespace llvm;
3762cfcf62SDimitry Andric using namespace object;
3862cfcf62SDimitry Andric
39fe6060f1SDimitry Andric namespace {
40fe6060f1SDimitry Andric using namespace llvm::opt; // for HelpHidden in Opts.inc
41fe6060f1SDimitry Andric enum ID {
42fe6060f1SDimitry Andric OPT_INVALID = 0, // This is not an option ID.
43*5f757f3fSDimitry Andric #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
44fe6060f1SDimitry Andric #include "Opts.inc"
45fe6060f1SDimitry Andric #undef OPTION
46fe6060f1SDimitry Andric };
47fe6060f1SDimitry Andric
48bdd1243dSDimitry Andric #define PREFIX(NAME, VALUE) \
49bdd1243dSDimitry Andric static constexpr StringLiteral NAME##_init[] = VALUE; \
50bdd1243dSDimitry Andric static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
51bdd1243dSDimitry Andric std::size(NAME##_init) - 1);
52fe6060f1SDimitry Andric #include "Opts.inc"
53fe6060f1SDimitry Andric #undef PREFIX
54fe6060f1SDimitry Andric
55bdd1243dSDimitry Andric static constexpr opt::OptTable::Info InfoTable[] = {
56*5f757f3fSDimitry Andric #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
57fe6060f1SDimitry Andric #include "Opts.inc"
58fe6060f1SDimitry Andric #undef OPTION
59fe6060f1SDimitry Andric };
60fe6060f1SDimitry Andric
61bdd1243dSDimitry Andric class SizeOptTable : public opt::GenericOptTable {
62fe6060f1SDimitry Andric public:
SizeOptTable()63bdd1243dSDimitry Andric SizeOptTable() : GenericOptTable(InfoTable) { setGroupedShortOptions(true); }
64fe6060f1SDimitry Andric };
6562cfcf62SDimitry Andric
6662cfcf62SDimitry Andric enum OutputFormatTy { berkeley, sysv, darwin };
67fe6060f1SDimitry Andric enum RadixTy { octal = 8, decimal = 10, hexadecimal = 16 };
68fe6060f1SDimitry Andric } // namespace
6962cfcf62SDimitry Andric
70fe6060f1SDimitry Andric static bool ArchAll = false;
71fe6060f1SDimitry Andric static std::vector<StringRef> ArchFlags;
72fe6060f1SDimitry Andric static bool ELFCommons;
73fe6060f1SDimitry Andric static OutputFormatTy OutputFormat;
74fe6060f1SDimitry Andric static bool DarwinLongFormat;
75fe6060f1SDimitry Andric static RadixTy Radix;
76fe6060f1SDimitry Andric static bool TotalSizes;
7762cfcf62SDimitry Andric
78fe6060f1SDimitry Andric static std::vector<std::string> InputFilenames;
79fe6060f1SDimitry Andric
80fe6060f1SDimitry Andric static std::string ToolName;
81fe6060f1SDimitry Andric
82fe6060f1SDimitry Andric // States
83fe6060f1SDimitry Andric static bool HadError = false;
8462cfcf62SDimitry Andric static bool BerkeleyHeaderPrinted = false;
8562cfcf62SDimitry Andric static bool MoreThanOneFile = false;
8662cfcf62SDimitry Andric static uint64_t TotalObjectText = 0;
8762cfcf62SDimitry Andric static uint64_t TotalObjectData = 0;
8862cfcf62SDimitry Andric static uint64_t TotalObjectBss = 0;
8962cfcf62SDimitry Andric static uint64_t TotalObjectTotal = 0;
9062cfcf62SDimitry Andric
error(const Twine & Message,StringRef File="")91fe6060f1SDimitry Andric static void error(const Twine &Message, StringRef File = "") {
9262cfcf62SDimitry Andric HadError = true;
93fe6060f1SDimitry Andric if (File.empty())
94fe6060f1SDimitry Andric WithColor::error(errs(), ToolName) << Message << '\n';
95fe6060f1SDimitry Andric else
96fe6060f1SDimitry Andric WithColor::error(errs(), ToolName)
97fe6060f1SDimitry Andric << "'" << File << "': " << Message << '\n';
9862cfcf62SDimitry Andric }
9962cfcf62SDimitry Andric
10062cfcf62SDimitry Andric // This version of error() prints the archive name and member name, for example:
10162cfcf62SDimitry Andric // "libx.a(foo.o)" after the ToolName before the error message. It sets
10262cfcf62SDimitry Andric // HadError but returns allowing the code to move on to other archive members.
error(llvm::Error E,StringRef FileName,const Archive::Child & C,StringRef ArchitectureName=StringRef ())10362cfcf62SDimitry Andric static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
10462cfcf62SDimitry Andric StringRef ArchitectureName = StringRef()) {
10562cfcf62SDimitry Andric HadError = true;
10662cfcf62SDimitry Andric WithColor::error(errs(), ToolName) << "'" << FileName << "'";
10762cfcf62SDimitry Andric
10862cfcf62SDimitry Andric Expected<StringRef> NameOrErr = C.getName();
10962cfcf62SDimitry Andric // TODO: if we have a error getting the name then it would be nice to print
11062cfcf62SDimitry Andric // the index of which archive member this is and or its offset in the
11162cfcf62SDimitry Andric // archive instead of "???" as the name.
11262cfcf62SDimitry Andric if (!NameOrErr) {
11362cfcf62SDimitry Andric consumeError(NameOrErr.takeError());
11462cfcf62SDimitry Andric errs() << "(" << "???" << ")";
11562cfcf62SDimitry Andric } else
11662cfcf62SDimitry Andric errs() << "(" << NameOrErr.get() << ")";
11762cfcf62SDimitry Andric
11862cfcf62SDimitry Andric if (!ArchitectureName.empty())
11962cfcf62SDimitry Andric errs() << " (for architecture " << ArchitectureName << ") ";
12062cfcf62SDimitry Andric
12162cfcf62SDimitry Andric std::string Buf;
12262cfcf62SDimitry Andric raw_string_ostream OS(Buf);
12362cfcf62SDimitry Andric logAllUnhandledErrors(std::move(E), OS);
12462cfcf62SDimitry Andric OS.flush();
12562cfcf62SDimitry Andric errs() << ": " << Buf << "\n";
12662cfcf62SDimitry Andric }
12762cfcf62SDimitry Andric
12862cfcf62SDimitry Andric // This version of error() prints the file name and which architecture slice it // is from, for example: "foo.o (for architecture i386)" after the ToolName
12962cfcf62SDimitry Andric // before the error message. It sets HadError but returns allowing the code to
13062cfcf62SDimitry Andric // move on to other architecture slices.
error(llvm::Error E,StringRef FileName,StringRef ArchitectureName=StringRef ())13162cfcf62SDimitry Andric static void error(llvm::Error E, StringRef FileName,
13262cfcf62SDimitry Andric StringRef ArchitectureName = StringRef()) {
13362cfcf62SDimitry Andric HadError = true;
13462cfcf62SDimitry Andric WithColor::error(errs(), ToolName) << "'" << FileName << "'";
13562cfcf62SDimitry Andric
13662cfcf62SDimitry Andric if (!ArchitectureName.empty())
13762cfcf62SDimitry Andric errs() << " (for architecture " << ArchitectureName << ") ";
13862cfcf62SDimitry Andric
13962cfcf62SDimitry Andric std::string Buf;
14062cfcf62SDimitry Andric raw_string_ostream OS(Buf);
14162cfcf62SDimitry Andric logAllUnhandledErrors(std::move(E), OS);
14262cfcf62SDimitry Andric OS.flush();
14362cfcf62SDimitry Andric errs() << ": " << Buf << "\n";
14462cfcf62SDimitry Andric }
14562cfcf62SDimitry Andric
14662cfcf62SDimitry Andric /// Get the length of the string that represents @p num in Radix including the
14762cfcf62SDimitry Andric /// leading 0x or 0 for hexadecimal and octal respectively.
getNumLengthAsString(uint64_t num)14862cfcf62SDimitry Andric static size_t getNumLengthAsString(uint64_t num) {
14962cfcf62SDimitry Andric APInt conv(64, num);
15062cfcf62SDimitry Andric SmallString<32> result;
15162cfcf62SDimitry Andric conv.toString(result, Radix, false, true);
15262cfcf62SDimitry Andric return result.size();
15362cfcf62SDimitry Andric }
15462cfcf62SDimitry Andric
15562cfcf62SDimitry Andric /// Return the printing format for the Radix.
getRadixFmt()15662cfcf62SDimitry Andric static const char *getRadixFmt() {
15762cfcf62SDimitry Andric switch (Radix) {
15862cfcf62SDimitry Andric case octal:
15962cfcf62SDimitry Andric return PRIo64;
16062cfcf62SDimitry Andric case decimal:
16162cfcf62SDimitry Andric return PRIu64;
16262cfcf62SDimitry Andric case hexadecimal:
16362cfcf62SDimitry Andric return PRIx64;
16462cfcf62SDimitry Andric }
16562cfcf62SDimitry Andric return nullptr;
16662cfcf62SDimitry Andric }
16762cfcf62SDimitry Andric
16862cfcf62SDimitry Andric /// Remove unneeded ELF sections from calculation
considerForSize(ObjectFile * Obj,SectionRef Section)16962cfcf62SDimitry Andric static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
17062cfcf62SDimitry Andric if (!Obj->isELF())
17162cfcf62SDimitry Andric return true;
17262cfcf62SDimitry Andric switch (static_cast<ELFSectionRef>(Section).getType()) {
17362cfcf62SDimitry Andric case ELF::SHT_NULL:
17462cfcf62SDimitry Andric case ELF::SHT_SYMTAB:
1755ffd83dbSDimitry Andric return false;
17662cfcf62SDimitry Andric case ELF::SHT_STRTAB:
17762cfcf62SDimitry Andric case ELF::SHT_REL:
17862cfcf62SDimitry Andric case ELF::SHT_RELA:
1795ffd83dbSDimitry Andric return static_cast<ELFSectionRef>(Section).getFlags() & ELF::SHF_ALLOC;
18062cfcf62SDimitry Andric }
18162cfcf62SDimitry Andric return true;
18262cfcf62SDimitry Andric }
18362cfcf62SDimitry Andric
18462cfcf62SDimitry Andric /// Total size of all ELF common symbols
getCommonSize(ObjectFile * Obj)1855ffd83dbSDimitry Andric static Expected<uint64_t> getCommonSize(ObjectFile *Obj) {
18662cfcf62SDimitry Andric uint64_t TotalCommons = 0;
1875ffd83dbSDimitry Andric for (auto &Sym : Obj->symbols()) {
1885ffd83dbSDimitry Andric Expected<uint32_t> SymFlagsOrErr =
1895ffd83dbSDimitry Andric Obj->getSymbolFlags(Sym.getRawDataRefImpl());
1905ffd83dbSDimitry Andric if (!SymFlagsOrErr)
1915ffd83dbSDimitry Andric return SymFlagsOrErr.takeError();
1925ffd83dbSDimitry Andric if (*SymFlagsOrErr & SymbolRef::SF_Common)
19362cfcf62SDimitry Andric TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
1945ffd83dbSDimitry Andric }
19562cfcf62SDimitry Andric return TotalCommons;
19662cfcf62SDimitry Andric }
19762cfcf62SDimitry Andric
19862cfcf62SDimitry Andric /// Print the size of each Mach-O segment and section in @p MachO.
19962cfcf62SDimitry Andric ///
20062cfcf62SDimitry Andric /// This is when used when @c OutputFormat is darwin and produces the same
20162cfcf62SDimitry Andric /// output as darwin's size(1) -m output.
printDarwinSectionSizes(MachOObjectFile * MachO)20262cfcf62SDimitry Andric static void printDarwinSectionSizes(MachOObjectFile *MachO) {
20362cfcf62SDimitry Andric std::string fmtbuf;
20462cfcf62SDimitry Andric raw_string_ostream fmt(fmtbuf);
20562cfcf62SDimitry Andric const char *radix_fmt = getRadixFmt();
20662cfcf62SDimitry Andric if (Radix == hexadecimal)
20762cfcf62SDimitry Andric fmt << "0x";
20862cfcf62SDimitry Andric fmt << "%" << radix_fmt;
20962cfcf62SDimitry Andric
21062cfcf62SDimitry Andric uint32_t Filetype = MachO->getHeader().filetype;
21162cfcf62SDimitry Andric
21262cfcf62SDimitry Andric uint64_t total = 0;
21362cfcf62SDimitry Andric for (const auto &Load : MachO->load_commands()) {
21462cfcf62SDimitry Andric if (Load.C.cmd == MachO::LC_SEGMENT_64) {
21562cfcf62SDimitry Andric MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
21662cfcf62SDimitry Andric outs() << "Segment " << Seg.segname << ": "
21762cfcf62SDimitry Andric << format(fmt.str().c_str(), Seg.vmsize);
21862cfcf62SDimitry Andric if (DarwinLongFormat)
21962cfcf62SDimitry Andric outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
22062cfcf62SDimitry Andric << Seg.fileoff << ")";
22162cfcf62SDimitry Andric outs() << "\n";
22262cfcf62SDimitry Andric total += Seg.vmsize;
22362cfcf62SDimitry Andric uint64_t sec_total = 0;
22462cfcf62SDimitry Andric for (unsigned J = 0; J < Seg.nsects; ++J) {
22562cfcf62SDimitry Andric MachO::section_64 Sec = MachO->getSection64(Load, J);
22662cfcf62SDimitry Andric if (Filetype == MachO::MH_OBJECT)
22762cfcf62SDimitry Andric outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
22862cfcf62SDimitry Andric << format("%.16s", &Sec.sectname) << "): ";
22962cfcf62SDimitry Andric else
23062cfcf62SDimitry Andric outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
23162cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), Sec.size);
23262cfcf62SDimitry Andric if (DarwinLongFormat)
23362cfcf62SDimitry Andric outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
23462cfcf62SDimitry Andric << Sec.offset << ")";
23562cfcf62SDimitry Andric outs() << "\n";
23662cfcf62SDimitry Andric sec_total += Sec.size;
23762cfcf62SDimitry Andric }
23862cfcf62SDimitry Andric if (Seg.nsects != 0)
23962cfcf62SDimitry Andric outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
24062cfcf62SDimitry Andric } else if (Load.C.cmd == MachO::LC_SEGMENT) {
24162cfcf62SDimitry Andric MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
24262cfcf62SDimitry Andric uint64_t Seg_vmsize = Seg.vmsize;
24362cfcf62SDimitry Andric outs() << "Segment " << Seg.segname << ": "
24462cfcf62SDimitry Andric << format(fmt.str().c_str(), Seg_vmsize);
24562cfcf62SDimitry Andric if (DarwinLongFormat)
24662cfcf62SDimitry Andric outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
24762cfcf62SDimitry Andric << Seg.fileoff << ")";
24862cfcf62SDimitry Andric outs() << "\n";
24962cfcf62SDimitry Andric total += Seg.vmsize;
25062cfcf62SDimitry Andric uint64_t sec_total = 0;
25162cfcf62SDimitry Andric for (unsigned J = 0; J < Seg.nsects; ++J) {
25262cfcf62SDimitry Andric MachO::section Sec = MachO->getSection(Load, J);
25362cfcf62SDimitry Andric if (Filetype == MachO::MH_OBJECT)
25462cfcf62SDimitry Andric outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
25562cfcf62SDimitry Andric << format("%.16s", &Sec.sectname) << "): ";
25662cfcf62SDimitry Andric else
25762cfcf62SDimitry Andric outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
25862cfcf62SDimitry Andric uint64_t Sec_size = Sec.size;
25962cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), Sec_size);
26062cfcf62SDimitry Andric if (DarwinLongFormat)
26162cfcf62SDimitry Andric outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
26262cfcf62SDimitry Andric << Sec.offset << ")";
26362cfcf62SDimitry Andric outs() << "\n";
26462cfcf62SDimitry Andric sec_total += Sec.size;
26562cfcf62SDimitry Andric }
26662cfcf62SDimitry Andric if (Seg.nsects != 0)
26762cfcf62SDimitry Andric outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
26862cfcf62SDimitry Andric }
26962cfcf62SDimitry Andric }
27062cfcf62SDimitry Andric outs() << "total " << format(fmt.str().c_str(), total) << "\n";
27162cfcf62SDimitry Andric }
27262cfcf62SDimitry Andric
27362cfcf62SDimitry Andric /// Print the summary sizes of the standard Mach-O segments in @p MachO.
27462cfcf62SDimitry Andric ///
27562cfcf62SDimitry Andric /// This is when used when @c OutputFormat is berkeley with a Mach-O file and
27662cfcf62SDimitry Andric /// produces the same output as darwin's size(1) default output.
printDarwinSegmentSizes(MachOObjectFile * MachO)27762cfcf62SDimitry Andric static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
27862cfcf62SDimitry Andric uint64_t total_text = 0;
27962cfcf62SDimitry Andric uint64_t total_data = 0;
28062cfcf62SDimitry Andric uint64_t total_objc = 0;
28162cfcf62SDimitry Andric uint64_t total_others = 0;
28262cfcf62SDimitry Andric for (const auto &Load : MachO->load_commands()) {
28362cfcf62SDimitry Andric if (Load.C.cmd == MachO::LC_SEGMENT_64) {
28462cfcf62SDimitry Andric MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
28562cfcf62SDimitry Andric if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
28662cfcf62SDimitry Andric for (unsigned J = 0; J < Seg.nsects; ++J) {
28762cfcf62SDimitry Andric MachO::section_64 Sec = MachO->getSection64(Load, J);
28862cfcf62SDimitry Andric StringRef SegmentName = StringRef(Sec.segname);
28962cfcf62SDimitry Andric if (SegmentName == "__TEXT")
29062cfcf62SDimitry Andric total_text += Sec.size;
29162cfcf62SDimitry Andric else if (SegmentName == "__DATA")
29262cfcf62SDimitry Andric total_data += Sec.size;
29362cfcf62SDimitry Andric else if (SegmentName == "__OBJC")
29462cfcf62SDimitry Andric total_objc += Sec.size;
29562cfcf62SDimitry Andric else
29662cfcf62SDimitry Andric total_others += Sec.size;
29762cfcf62SDimitry Andric }
29862cfcf62SDimitry Andric } else {
29962cfcf62SDimitry Andric StringRef SegmentName = StringRef(Seg.segname);
30062cfcf62SDimitry Andric if (SegmentName == "__TEXT")
30162cfcf62SDimitry Andric total_text += Seg.vmsize;
30262cfcf62SDimitry Andric else if (SegmentName == "__DATA")
30362cfcf62SDimitry Andric total_data += Seg.vmsize;
30462cfcf62SDimitry Andric else if (SegmentName == "__OBJC")
30562cfcf62SDimitry Andric total_objc += Seg.vmsize;
30662cfcf62SDimitry Andric else
30762cfcf62SDimitry Andric total_others += Seg.vmsize;
30862cfcf62SDimitry Andric }
30962cfcf62SDimitry Andric } else if (Load.C.cmd == MachO::LC_SEGMENT) {
31062cfcf62SDimitry Andric MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
31162cfcf62SDimitry Andric if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
31262cfcf62SDimitry Andric for (unsigned J = 0; J < Seg.nsects; ++J) {
31362cfcf62SDimitry Andric MachO::section Sec = MachO->getSection(Load, J);
31462cfcf62SDimitry Andric StringRef SegmentName = StringRef(Sec.segname);
31562cfcf62SDimitry Andric if (SegmentName == "__TEXT")
31662cfcf62SDimitry Andric total_text += Sec.size;
31762cfcf62SDimitry Andric else if (SegmentName == "__DATA")
31862cfcf62SDimitry Andric total_data += Sec.size;
31962cfcf62SDimitry Andric else if (SegmentName == "__OBJC")
32062cfcf62SDimitry Andric total_objc += Sec.size;
32162cfcf62SDimitry Andric else
32262cfcf62SDimitry Andric total_others += Sec.size;
32362cfcf62SDimitry Andric }
32462cfcf62SDimitry Andric } else {
32562cfcf62SDimitry Andric StringRef SegmentName = StringRef(Seg.segname);
32662cfcf62SDimitry Andric if (SegmentName == "__TEXT")
32762cfcf62SDimitry Andric total_text += Seg.vmsize;
32862cfcf62SDimitry Andric else if (SegmentName == "__DATA")
32962cfcf62SDimitry Andric total_data += Seg.vmsize;
33062cfcf62SDimitry Andric else if (SegmentName == "__OBJC")
33162cfcf62SDimitry Andric total_objc += Seg.vmsize;
33262cfcf62SDimitry Andric else
33362cfcf62SDimitry Andric total_others += Seg.vmsize;
33462cfcf62SDimitry Andric }
33562cfcf62SDimitry Andric }
33662cfcf62SDimitry Andric }
33762cfcf62SDimitry Andric uint64_t total = total_text + total_data + total_objc + total_others;
33862cfcf62SDimitry Andric
33962cfcf62SDimitry Andric if (!BerkeleyHeaderPrinted) {
34062cfcf62SDimitry Andric outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
34162cfcf62SDimitry Andric BerkeleyHeaderPrinted = true;
34262cfcf62SDimitry Andric }
34362cfcf62SDimitry Andric outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
34462cfcf62SDimitry Andric << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
34562cfcf62SDimitry Andric << "\t";
34662cfcf62SDimitry Andric }
34762cfcf62SDimitry Andric
34862cfcf62SDimitry Andric /// Print the size of each section in @p Obj.
34962cfcf62SDimitry Andric ///
35062cfcf62SDimitry Andric /// The format used is determined by @c OutputFormat and @c Radix.
printObjectSectionSizes(ObjectFile * Obj)35162cfcf62SDimitry Andric static void printObjectSectionSizes(ObjectFile *Obj) {
35262cfcf62SDimitry Andric uint64_t total = 0;
35362cfcf62SDimitry Andric std::string fmtbuf;
35462cfcf62SDimitry Andric raw_string_ostream fmt(fmtbuf);
35562cfcf62SDimitry Andric const char *radix_fmt = getRadixFmt();
35662cfcf62SDimitry Andric
35762cfcf62SDimitry Andric // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
35862cfcf62SDimitry Andric // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
35962cfcf62SDimitry Andric // let it fall through to OutputFormat berkeley.
36062cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
36162cfcf62SDimitry Andric if (OutputFormat == darwin && MachO)
36262cfcf62SDimitry Andric printDarwinSectionSizes(MachO);
36362cfcf62SDimitry Andric // If we have a MachOObjectFile and the OutputFormat is berkeley print as
36462cfcf62SDimitry Andric // darwin's default berkeley format for Mach-O files.
36562cfcf62SDimitry Andric else if (MachO && OutputFormat == berkeley)
36662cfcf62SDimitry Andric printDarwinSegmentSizes(MachO);
36762cfcf62SDimitry Andric else if (OutputFormat == sysv) {
36862cfcf62SDimitry Andric // Run two passes over all sections. The first gets the lengths needed for
36962cfcf62SDimitry Andric // formatting the output. The second actually does the output.
37062cfcf62SDimitry Andric std::size_t max_name_len = strlen("section");
37162cfcf62SDimitry Andric std::size_t max_size_len = strlen("size");
37262cfcf62SDimitry Andric std::size_t max_addr_len = strlen("addr");
37362cfcf62SDimitry Andric for (const SectionRef &Section : Obj->sections()) {
37462cfcf62SDimitry Andric if (!considerForSize(Obj, Section))
37562cfcf62SDimitry Andric continue;
37662cfcf62SDimitry Andric uint64_t size = Section.getSize();
37762cfcf62SDimitry Andric total += size;
37862cfcf62SDimitry Andric
37962cfcf62SDimitry Andric Expected<StringRef> name_or_err = Section.getName();
38062cfcf62SDimitry Andric if (!name_or_err) {
38162cfcf62SDimitry Andric error(name_or_err.takeError(), Obj->getFileName());
38262cfcf62SDimitry Andric return;
38362cfcf62SDimitry Andric }
38462cfcf62SDimitry Andric
38562cfcf62SDimitry Andric uint64_t addr = Section.getAddress();
38662cfcf62SDimitry Andric max_name_len = std::max(max_name_len, name_or_err->size());
38762cfcf62SDimitry Andric max_size_len = std::max(max_size_len, getNumLengthAsString(size));
38862cfcf62SDimitry Andric max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
38962cfcf62SDimitry Andric }
39062cfcf62SDimitry Andric
39162cfcf62SDimitry Andric // Add extra padding.
39262cfcf62SDimitry Andric max_name_len += 2;
39362cfcf62SDimitry Andric max_size_len += 2;
39462cfcf62SDimitry Andric max_addr_len += 2;
39562cfcf62SDimitry Andric
39662cfcf62SDimitry Andric // Setup header format.
39762cfcf62SDimitry Andric fmt << "%-" << max_name_len << "s "
39862cfcf62SDimitry Andric << "%" << max_size_len << "s "
39962cfcf62SDimitry Andric << "%" << max_addr_len << "s\n";
40062cfcf62SDimitry Andric
40162cfcf62SDimitry Andric // Print header
40262cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
40362cfcf62SDimitry Andric static_cast<const char *>("size"),
40462cfcf62SDimitry Andric static_cast<const char *>("addr"));
40562cfcf62SDimitry Andric fmtbuf.clear();
40662cfcf62SDimitry Andric
40762cfcf62SDimitry Andric // Setup per section format.
40862cfcf62SDimitry Andric fmt << "%-" << max_name_len << "s "
40962cfcf62SDimitry Andric << "%#" << max_size_len << radix_fmt << " "
41062cfcf62SDimitry Andric << "%#" << max_addr_len << radix_fmt << "\n";
41162cfcf62SDimitry Andric
41262cfcf62SDimitry Andric // Print each section.
41362cfcf62SDimitry Andric for (const SectionRef &Section : Obj->sections()) {
41462cfcf62SDimitry Andric if (!considerForSize(Obj, Section))
41562cfcf62SDimitry Andric continue;
41662cfcf62SDimitry Andric
41762cfcf62SDimitry Andric Expected<StringRef> name_or_err = Section.getName();
41862cfcf62SDimitry Andric if (!name_or_err) {
41962cfcf62SDimitry Andric error(name_or_err.takeError(), Obj->getFileName());
42062cfcf62SDimitry Andric return;
42162cfcf62SDimitry Andric }
42262cfcf62SDimitry Andric
42362cfcf62SDimitry Andric uint64_t size = Section.getSize();
42462cfcf62SDimitry Andric uint64_t addr = Section.getAddress();
42562cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), name_or_err->str().c_str(), size, addr);
42662cfcf62SDimitry Andric }
42762cfcf62SDimitry Andric
42862cfcf62SDimitry Andric if (ELFCommons) {
4295ffd83dbSDimitry Andric if (Expected<uint64_t> CommonSizeOrErr = getCommonSize(Obj)) {
4305ffd83dbSDimitry Andric total += *CommonSizeOrErr;
43162cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
4325ffd83dbSDimitry Andric *CommonSizeOrErr, static_cast<uint64_t>(0));
4335ffd83dbSDimitry Andric } else {
4345ffd83dbSDimitry Andric error(CommonSizeOrErr.takeError(), Obj->getFileName());
4355ffd83dbSDimitry Andric return;
4365ffd83dbSDimitry Andric }
43762cfcf62SDimitry Andric }
43862cfcf62SDimitry Andric
43962cfcf62SDimitry Andric // Print total.
44062cfcf62SDimitry Andric fmtbuf.clear();
44162cfcf62SDimitry Andric fmt << "%-" << max_name_len << "s "
44262cfcf62SDimitry Andric << "%#" << max_size_len << radix_fmt << "\n";
44362cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
44462cfcf62SDimitry Andric total)
44562cfcf62SDimitry Andric << "\n\n";
44662cfcf62SDimitry Andric } else {
44762cfcf62SDimitry Andric // The Berkeley format does not display individual section sizes. It
44862cfcf62SDimitry Andric // displays the cumulative size for each section type.
44962cfcf62SDimitry Andric uint64_t total_text = 0;
45062cfcf62SDimitry Andric uint64_t total_data = 0;
45162cfcf62SDimitry Andric uint64_t total_bss = 0;
45262cfcf62SDimitry Andric
45362cfcf62SDimitry Andric // Make one pass over the section table to calculate sizes.
45462cfcf62SDimitry Andric for (const SectionRef &Section : Obj->sections()) {
45562cfcf62SDimitry Andric uint64_t size = Section.getSize();
45662cfcf62SDimitry Andric bool isText = Section.isBerkeleyText();
45762cfcf62SDimitry Andric bool isData = Section.isBerkeleyData();
45862cfcf62SDimitry Andric bool isBSS = Section.isBSS();
45962cfcf62SDimitry Andric if (isText)
46062cfcf62SDimitry Andric total_text += size;
46162cfcf62SDimitry Andric else if (isData)
46262cfcf62SDimitry Andric total_data += size;
46362cfcf62SDimitry Andric else if (isBSS)
46462cfcf62SDimitry Andric total_bss += size;
46562cfcf62SDimitry Andric }
46662cfcf62SDimitry Andric
4675ffd83dbSDimitry Andric if (ELFCommons) {
4685ffd83dbSDimitry Andric if (Expected<uint64_t> CommonSizeOrErr = getCommonSize(Obj))
4695ffd83dbSDimitry Andric total_bss += *CommonSizeOrErr;
4705ffd83dbSDimitry Andric else {
4715ffd83dbSDimitry Andric error(CommonSizeOrErr.takeError(), Obj->getFileName());
4725ffd83dbSDimitry Andric return;
4735ffd83dbSDimitry Andric }
4745ffd83dbSDimitry Andric }
47562cfcf62SDimitry Andric
47662cfcf62SDimitry Andric total = total_text + total_data + total_bss;
47762cfcf62SDimitry Andric
47862cfcf62SDimitry Andric if (TotalSizes) {
47962cfcf62SDimitry Andric TotalObjectText += total_text;
48062cfcf62SDimitry Andric TotalObjectData += total_data;
48162cfcf62SDimitry Andric TotalObjectBss += total_bss;
48262cfcf62SDimitry Andric TotalObjectTotal += total;
48362cfcf62SDimitry Andric }
48462cfcf62SDimitry Andric
48562cfcf62SDimitry Andric if (!BerkeleyHeaderPrinted) {
48662cfcf62SDimitry Andric outs() << " text\t"
48762cfcf62SDimitry Andric " data\t"
48862cfcf62SDimitry Andric " bss\t"
48962cfcf62SDimitry Andric " "
49062cfcf62SDimitry Andric << (Radix == octal ? "oct" : "dec")
49162cfcf62SDimitry Andric << "\t"
49262cfcf62SDimitry Andric " hex\t"
49362cfcf62SDimitry Andric "filename\n";
49462cfcf62SDimitry Andric BerkeleyHeaderPrinted = true;
49562cfcf62SDimitry Andric }
49662cfcf62SDimitry Andric
49762cfcf62SDimitry Andric // Print result.
49862cfcf62SDimitry Andric fmt << "%#7" << radix_fmt << "\t"
49962cfcf62SDimitry Andric << "%#7" << radix_fmt << "\t"
50062cfcf62SDimitry Andric << "%#7" << radix_fmt << "\t";
50162cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
50262cfcf62SDimitry Andric fmtbuf.clear();
50362cfcf62SDimitry Andric fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t"
50462cfcf62SDimitry Andric << "%7" PRIx64 "\t";
50562cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), total, total);
50662cfcf62SDimitry Andric }
50762cfcf62SDimitry Andric }
50862cfcf62SDimitry Andric
50962cfcf62SDimitry Andric /// Checks to see if the @p O ObjectFile is a Mach-O file and if it is and there
51062cfcf62SDimitry Andric /// is a list of architecture flags specified then check to make sure this
51162cfcf62SDimitry Andric /// Mach-O file is one of those architectures or all architectures was
51262cfcf62SDimitry Andric /// specificed. If not then an error is generated and this routine returns
51362cfcf62SDimitry Andric /// false. Else it returns true.
checkMachOAndArchFlags(ObjectFile * O,StringRef Filename)51462cfcf62SDimitry Andric static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
51562cfcf62SDimitry Andric auto *MachO = dyn_cast<MachOObjectFile>(O);
51662cfcf62SDimitry Andric
51762cfcf62SDimitry Andric if (!MachO || ArchAll || ArchFlags.empty())
51862cfcf62SDimitry Andric return true;
51962cfcf62SDimitry Andric
52062cfcf62SDimitry Andric MachO::mach_header H;
52162cfcf62SDimitry Andric MachO::mach_header_64 H_64;
52262cfcf62SDimitry Andric Triple T;
52362cfcf62SDimitry Andric if (MachO->is64Bit()) {
52462cfcf62SDimitry Andric H_64 = MachO->MachOObjectFile::getHeader64();
52562cfcf62SDimitry Andric T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
52662cfcf62SDimitry Andric } else {
52762cfcf62SDimitry Andric H = MachO->MachOObjectFile::getHeader();
52862cfcf62SDimitry Andric T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
52962cfcf62SDimitry Andric }
530e8d8bef9SDimitry Andric if (!is_contained(ArchFlags, T.getArchName())) {
53162cfcf62SDimitry Andric error("no architecture specified", Filename);
53262cfcf62SDimitry Andric return false;
53362cfcf62SDimitry Andric }
53462cfcf62SDimitry Andric return true;
53562cfcf62SDimitry Andric }
53662cfcf62SDimitry Andric
53762cfcf62SDimitry Andric /// Print the section sizes for @p file. If @p file is an archive, print the
53862cfcf62SDimitry Andric /// section sizes for each archive member.
printFileSectionSizes(StringRef file)53962cfcf62SDimitry Andric static void printFileSectionSizes(StringRef file) {
54062cfcf62SDimitry Andric
54162cfcf62SDimitry Andric // Attempt to open the binary.
54262cfcf62SDimitry Andric Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
54362cfcf62SDimitry Andric if (!BinaryOrErr) {
54462cfcf62SDimitry Andric error(BinaryOrErr.takeError(), file);
54562cfcf62SDimitry Andric return;
54662cfcf62SDimitry Andric }
54762cfcf62SDimitry Andric Binary &Bin = *BinaryOrErr.get().getBinary();
54862cfcf62SDimitry Andric
54962cfcf62SDimitry Andric if (Archive *a = dyn_cast<Archive>(&Bin)) {
55062cfcf62SDimitry Andric // This is an archive. Iterate over each member and display its sizes.
55162cfcf62SDimitry Andric Error Err = Error::success();
55262cfcf62SDimitry Andric for (auto &C : a->children(Err)) {
55362cfcf62SDimitry Andric Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
55462cfcf62SDimitry Andric if (!ChildOrErr) {
55562cfcf62SDimitry Andric if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
55662cfcf62SDimitry Andric error(std::move(E), a->getFileName(), C);
55762cfcf62SDimitry Andric continue;
55862cfcf62SDimitry Andric }
55962cfcf62SDimitry Andric if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
56062cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
56162cfcf62SDimitry Andric if (!checkMachOAndArchFlags(o, file))
56262cfcf62SDimitry Andric return;
56362cfcf62SDimitry Andric if (OutputFormat == sysv)
56462cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
56562cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin)
56662cfcf62SDimitry Andric outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
56762cfcf62SDimitry Andric printObjectSectionSizes(o);
568bdd1243dSDimitry Andric if (!MachO && OutputFormat == darwin)
569bdd1243dSDimitry Andric outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
57062cfcf62SDimitry Andric if (OutputFormat == berkeley) {
57162cfcf62SDimitry Andric if (MachO)
57262cfcf62SDimitry Andric outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
57362cfcf62SDimitry Andric else
57462cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
57562cfcf62SDimitry Andric }
57662cfcf62SDimitry Andric }
57762cfcf62SDimitry Andric }
57862cfcf62SDimitry Andric if (Err)
57962cfcf62SDimitry Andric error(std::move(Err), a->getFileName());
58062cfcf62SDimitry Andric } else if (MachOUniversalBinary *UB =
58162cfcf62SDimitry Andric dyn_cast<MachOUniversalBinary>(&Bin)) {
58262cfcf62SDimitry Andric // If we have a list of architecture flags specified dump only those.
58362cfcf62SDimitry Andric if (!ArchAll && !ArchFlags.empty()) {
58462cfcf62SDimitry Andric // Look for a slice in the universal binary that matches each ArchFlag.
58562cfcf62SDimitry Andric bool ArchFound;
58662cfcf62SDimitry Andric for (unsigned i = 0; i < ArchFlags.size(); ++i) {
58762cfcf62SDimitry Andric ArchFound = false;
58862cfcf62SDimitry Andric for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
58962cfcf62SDimitry Andric E = UB->end_objects();
59062cfcf62SDimitry Andric I != E; ++I) {
59162cfcf62SDimitry Andric if (ArchFlags[i] == I->getArchFlagName()) {
59262cfcf62SDimitry Andric ArchFound = true;
59362cfcf62SDimitry Andric Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
59462cfcf62SDimitry Andric if (UO) {
59562cfcf62SDimitry Andric if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
59662cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
59762cfcf62SDimitry Andric if (OutputFormat == sysv)
59862cfcf62SDimitry Andric outs() << o->getFileName() << " :\n";
59962cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin) {
60062cfcf62SDimitry Andric if (MoreThanOneFile || ArchFlags.size() > 1)
60162cfcf62SDimitry Andric outs() << o->getFileName() << " (for architecture "
60262cfcf62SDimitry Andric << I->getArchFlagName() << "): \n";
60362cfcf62SDimitry Andric }
60462cfcf62SDimitry Andric printObjectSectionSizes(o);
60562cfcf62SDimitry Andric if (OutputFormat == berkeley) {
60662cfcf62SDimitry Andric if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
60762cfcf62SDimitry Andric outs() << o->getFileName() << " (for architecture "
60862cfcf62SDimitry Andric << I->getArchFlagName() << ")";
60962cfcf62SDimitry Andric outs() << "\n";
61062cfcf62SDimitry Andric }
61162cfcf62SDimitry Andric }
61262cfcf62SDimitry Andric } else if (auto E = isNotObjectErrorInvalidFileType(
61362cfcf62SDimitry Andric UO.takeError())) {
61462cfcf62SDimitry Andric error(std::move(E), file, ArchFlags.size() > 1 ?
61562cfcf62SDimitry Andric StringRef(I->getArchFlagName()) : StringRef());
61662cfcf62SDimitry Andric return;
61762cfcf62SDimitry Andric } else if (Expected<std::unique_ptr<Archive>> AOrErr =
61862cfcf62SDimitry Andric I->getAsArchive()) {
61962cfcf62SDimitry Andric std::unique_ptr<Archive> &UA = *AOrErr;
62062cfcf62SDimitry Andric // This is an archive. Iterate over each member and display its
62162cfcf62SDimitry Andric // sizes.
62262cfcf62SDimitry Andric Error Err = Error::success();
62362cfcf62SDimitry Andric for (auto &C : UA->children(Err)) {
62462cfcf62SDimitry Andric Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
62562cfcf62SDimitry Andric if (!ChildOrErr) {
62662cfcf62SDimitry Andric if (auto E = isNotObjectErrorInvalidFileType(
62762cfcf62SDimitry Andric ChildOrErr.takeError()))
62862cfcf62SDimitry Andric error(std::move(E), UA->getFileName(), C,
62962cfcf62SDimitry Andric ArchFlags.size() > 1 ?
63062cfcf62SDimitry Andric StringRef(I->getArchFlagName()) : StringRef());
63162cfcf62SDimitry Andric continue;
63262cfcf62SDimitry Andric }
63362cfcf62SDimitry Andric if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
63462cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
63562cfcf62SDimitry Andric if (OutputFormat == sysv)
63662cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << UA->getFileName()
63762cfcf62SDimitry Andric << "):\n";
63862cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin)
63962cfcf62SDimitry Andric outs() << UA->getFileName() << "(" << o->getFileName()
64062cfcf62SDimitry Andric << ")"
64162cfcf62SDimitry Andric << " (for architecture " << I->getArchFlagName()
64262cfcf62SDimitry Andric << "):\n";
64362cfcf62SDimitry Andric printObjectSectionSizes(o);
64462cfcf62SDimitry Andric if (OutputFormat == berkeley) {
64562cfcf62SDimitry Andric if (MachO) {
64662cfcf62SDimitry Andric outs() << UA->getFileName() << "(" << o->getFileName()
64762cfcf62SDimitry Andric << ")";
64862cfcf62SDimitry Andric if (ArchFlags.size() > 1)
64962cfcf62SDimitry Andric outs() << " (for architecture " << I->getArchFlagName()
65062cfcf62SDimitry Andric << ")";
65162cfcf62SDimitry Andric outs() << "\n";
65262cfcf62SDimitry Andric } else
65362cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << UA->getFileName()
65462cfcf62SDimitry Andric << ")\n";
65562cfcf62SDimitry Andric }
65662cfcf62SDimitry Andric }
65762cfcf62SDimitry Andric }
65862cfcf62SDimitry Andric if (Err)
65962cfcf62SDimitry Andric error(std::move(Err), UA->getFileName());
66062cfcf62SDimitry Andric } else {
66162cfcf62SDimitry Andric consumeError(AOrErr.takeError());
66262cfcf62SDimitry Andric error("mach-o universal file for architecture " +
66362cfcf62SDimitry Andric StringRef(I->getArchFlagName()) +
66462cfcf62SDimitry Andric " is not a mach-o file or an archive file",
66562cfcf62SDimitry Andric file);
66662cfcf62SDimitry Andric }
66762cfcf62SDimitry Andric }
66862cfcf62SDimitry Andric }
66962cfcf62SDimitry Andric if (!ArchFound) {
67062cfcf62SDimitry Andric error("file does not contain architecture " + ArchFlags[i], file);
67162cfcf62SDimitry Andric return;
67262cfcf62SDimitry Andric }
67362cfcf62SDimitry Andric }
67462cfcf62SDimitry Andric return;
67562cfcf62SDimitry Andric }
67662cfcf62SDimitry Andric // No architecture flags were specified so if this contains a slice that
67762cfcf62SDimitry Andric // matches the host architecture dump only that.
67862cfcf62SDimitry Andric if (!ArchAll) {
67962cfcf62SDimitry Andric StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
68062cfcf62SDimitry Andric for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
68162cfcf62SDimitry Andric E = UB->end_objects();
68262cfcf62SDimitry Andric I != E; ++I) {
68362cfcf62SDimitry Andric if (HostArchName == I->getArchFlagName()) {
68462cfcf62SDimitry Andric Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
68562cfcf62SDimitry Andric if (UO) {
68662cfcf62SDimitry Andric if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
68762cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
68862cfcf62SDimitry Andric if (OutputFormat == sysv)
68962cfcf62SDimitry Andric outs() << o->getFileName() << " :\n";
69062cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin) {
69162cfcf62SDimitry Andric if (MoreThanOneFile)
69262cfcf62SDimitry Andric outs() << o->getFileName() << " (for architecture "
69362cfcf62SDimitry Andric << I->getArchFlagName() << "):\n";
69462cfcf62SDimitry Andric }
69562cfcf62SDimitry Andric printObjectSectionSizes(o);
69662cfcf62SDimitry Andric if (OutputFormat == berkeley) {
69762cfcf62SDimitry Andric if (!MachO || MoreThanOneFile)
69862cfcf62SDimitry Andric outs() << o->getFileName() << " (for architecture "
69962cfcf62SDimitry Andric << I->getArchFlagName() << ")";
70062cfcf62SDimitry Andric outs() << "\n";
70162cfcf62SDimitry Andric }
70262cfcf62SDimitry Andric }
70362cfcf62SDimitry Andric } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
70462cfcf62SDimitry Andric error(std::move(E), file);
70562cfcf62SDimitry Andric return;
70662cfcf62SDimitry Andric } else if (Expected<std::unique_ptr<Archive>> AOrErr =
70762cfcf62SDimitry Andric I->getAsArchive()) {
70862cfcf62SDimitry Andric std::unique_ptr<Archive> &UA = *AOrErr;
70962cfcf62SDimitry Andric // This is an archive. Iterate over each member and display its
71062cfcf62SDimitry Andric // sizes.
71162cfcf62SDimitry Andric Error Err = Error::success();
71262cfcf62SDimitry Andric for (auto &C : UA->children(Err)) {
71362cfcf62SDimitry Andric Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
71462cfcf62SDimitry Andric if (!ChildOrErr) {
71562cfcf62SDimitry Andric if (auto E = isNotObjectErrorInvalidFileType(
71662cfcf62SDimitry Andric ChildOrErr.takeError()))
71762cfcf62SDimitry Andric error(std::move(E), UA->getFileName(), C);
71862cfcf62SDimitry Andric continue;
71962cfcf62SDimitry Andric }
72062cfcf62SDimitry Andric if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
72162cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
72262cfcf62SDimitry Andric if (OutputFormat == sysv)
72362cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << UA->getFileName()
72462cfcf62SDimitry Andric << "):\n";
72562cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin)
72662cfcf62SDimitry Andric outs() << UA->getFileName() << "(" << o->getFileName() << ")"
72762cfcf62SDimitry Andric << " (for architecture " << I->getArchFlagName()
72862cfcf62SDimitry Andric << "):\n";
72962cfcf62SDimitry Andric printObjectSectionSizes(o);
73062cfcf62SDimitry Andric if (OutputFormat == berkeley) {
73162cfcf62SDimitry Andric if (MachO)
73262cfcf62SDimitry Andric outs() << UA->getFileName() << "(" << o->getFileName()
73362cfcf62SDimitry Andric << ")\n";
73462cfcf62SDimitry Andric else
73562cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << UA->getFileName()
73662cfcf62SDimitry Andric << ")\n";
73762cfcf62SDimitry Andric }
73862cfcf62SDimitry Andric }
73962cfcf62SDimitry Andric }
74062cfcf62SDimitry Andric if (Err)
74162cfcf62SDimitry Andric error(std::move(Err), UA->getFileName());
74262cfcf62SDimitry Andric } else {
74362cfcf62SDimitry Andric consumeError(AOrErr.takeError());
74462cfcf62SDimitry Andric error("mach-o universal file for architecture " +
74562cfcf62SDimitry Andric StringRef(I->getArchFlagName()) +
74662cfcf62SDimitry Andric " is not a mach-o file or an archive file",
74762cfcf62SDimitry Andric file);
74862cfcf62SDimitry Andric }
74962cfcf62SDimitry Andric return;
75062cfcf62SDimitry Andric }
75162cfcf62SDimitry Andric }
75262cfcf62SDimitry Andric }
75362cfcf62SDimitry Andric // Either all architectures have been specified or none have been specified
75462cfcf62SDimitry Andric // and this does not contain the host architecture so dump all the slices.
75562cfcf62SDimitry Andric bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
75662cfcf62SDimitry Andric for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
75762cfcf62SDimitry Andric E = UB->end_objects();
75862cfcf62SDimitry Andric I != E; ++I) {
75962cfcf62SDimitry Andric Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
76062cfcf62SDimitry Andric if (UO) {
76162cfcf62SDimitry Andric if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
76262cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
76362cfcf62SDimitry Andric if (OutputFormat == sysv)
76462cfcf62SDimitry Andric outs() << o->getFileName() << " :\n";
76562cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin) {
76662cfcf62SDimitry Andric if (MoreThanOneFile || MoreThanOneArch)
76762cfcf62SDimitry Andric outs() << o->getFileName() << " (for architecture "
76862cfcf62SDimitry Andric << I->getArchFlagName() << "):";
76962cfcf62SDimitry Andric outs() << "\n";
77062cfcf62SDimitry Andric }
77162cfcf62SDimitry Andric printObjectSectionSizes(o);
77262cfcf62SDimitry Andric if (OutputFormat == berkeley) {
77362cfcf62SDimitry Andric if (!MachO || MoreThanOneFile || MoreThanOneArch)
77462cfcf62SDimitry Andric outs() << o->getFileName() << " (for architecture "
77562cfcf62SDimitry Andric << I->getArchFlagName() << ")";
77662cfcf62SDimitry Andric outs() << "\n";
77762cfcf62SDimitry Andric }
77862cfcf62SDimitry Andric }
77962cfcf62SDimitry Andric } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
78062cfcf62SDimitry Andric error(std::move(E), file, MoreThanOneArch ?
78162cfcf62SDimitry Andric StringRef(I->getArchFlagName()) : StringRef());
78262cfcf62SDimitry Andric return;
78362cfcf62SDimitry Andric } else if (Expected<std::unique_ptr<Archive>> AOrErr =
78462cfcf62SDimitry Andric I->getAsArchive()) {
78562cfcf62SDimitry Andric std::unique_ptr<Archive> &UA = *AOrErr;
78662cfcf62SDimitry Andric // This is an archive. Iterate over each member and display its sizes.
78762cfcf62SDimitry Andric Error Err = Error::success();
78862cfcf62SDimitry Andric for (auto &C : UA->children(Err)) {
78962cfcf62SDimitry Andric Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
79062cfcf62SDimitry Andric if (!ChildOrErr) {
79162cfcf62SDimitry Andric if (auto E = isNotObjectErrorInvalidFileType(
79262cfcf62SDimitry Andric ChildOrErr.takeError()))
79362cfcf62SDimitry Andric error(std::move(E), UA->getFileName(), C, MoreThanOneArch ?
79462cfcf62SDimitry Andric StringRef(I->getArchFlagName()) : StringRef());
79562cfcf62SDimitry Andric continue;
79662cfcf62SDimitry Andric }
79762cfcf62SDimitry Andric if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
79862cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
79962cfcf62SDimitry Andric if (OutputFormat == sysv)
80062cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << UA->getFileName()
80162cfcf62SDimitry Andric << "):\n";
80262cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin)
80362cfcf62SDimitry Andric outs() << UA->getFileName() << "(" << o->getFileName() << ")"
80462cfcf62SDimitry Andric << " (for architecture " << I->getArchFlagName() << "):\n";
80562cfcf62SDimitry Andric printObjectSectionSizes(o);
80662cfcf62SDimitry Andric if (OutputFormat == berkeley) {
80762cfcf62SDimitry Andric if (MachO)
80862cfcf62SDimitry Andric outs() << UA->getFileName() << "(" << o->getFileName() << ")"
80962cfcf62SDimitry Andric << " (for architecture " << I->getArchFlagName()
81062cfcf62SDimitry Andric << ")\n";
81162cfcf62SDimitry Andric else
81262cfcf62SDimitry Andric outs() << o->getFileName() << " (ex " << UA->getFileName()
81362cfcf62SDimitry Andric << ")\n";
81462cfcf62SDimitry Andric }
81562cfcf62SDimitry Andric }
81662cfcf62SDimitry Andric }
81762cfcf62SDimitry Andric if (Err)
81862cfcf62SDimitry Andric error(std::move(Err), UA->getFileName());
81962cfcf62SDimitry Andric } else {
82062cfcf62SDimitry Andric consumeError(AOrErr.takeError());
82162cfcf62SDimitry Andric error("mach-o universal file for architecture " +
82262cfcf62SDimitry Andric StringRef(I->getArchFlagName()) +
82362cfcf62SDimitry Andric " is not a mach-o file or an archive file",
82462cfcf62SDimitry Andric file);
82562cfcf62SDimitry Andric }
82662cfcf62SDimitry Andric }
82762cfcf62SDimitry Andric } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
82862cfcf62SDimitry Andric if (!checkMachOAndArchFlags(o, file))
82962cfcf62SDimitry Andric return;
83062cfcf62SDimitry Andric MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
83162cfcf62SDimitry Andric if (OutputFormat == sysv)
83262cfcf62SDimitry Andric outs() << o->getFileName() << " :\n";
83362cfcf62SDimitry Andric else if (MachO && OutputFormat == darwin && MoreThanOneFile)
83462cfcf62SDimitry Andric outs() << o->getFileName() << ":\n";
83562cfcf62SDimitry Andric printObjectSectionSizes(o);
836bdd1243dSDimitry Andric if (!MachO && OutputFormat == darwin)
837bdd1243dSDimitry Andric outs() << o->getFileName() << "\n";
83862cfcf62SDimitry Andric if (OutputFormat == berkeley) {
83962cfcf62SDimitry Andric if (!MachO || MoreThanOneFile)
84062cfcf62SDimitry Andric outs() << o->getFileName();
84162cfcf62SDimitry Andric outs() << "\n";
84262cfcf62SDimitry Andric }
84362cfcf62SDimitry Andric } else {
84462cfcf62SDimitry Andric error("unsupported file type", file);
84562cfcf62SDimitry Andric }
84662cfcf62SDimitry Andric }
84762cfcf62SDimitry Andric
printBerkeleyTotals()84862cfcf62SDimitry Andric static void printBerkeleyTotals() {
84962cfcf62SDimitry Andric std::string fmtbuf;
85062cfcf62SDimitry Andric raw_string_ostream fmt(fmtbuf);
85162cfcf62SDimitry Andric const char *radix_fmt = getRadixFmt();
85262cfcf62SDimitry Andric fmt << "%#7" << radix_fmt << "\t"
85362cfcf62SDimitry Andric << "%#7" << radix_fmt << "\t"
85462cfcf62SDimitry Andric << "%#7" << radix_fmt << "\t";
85562cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), TotalObjectText, TotalObjectData,
85662cfcf62SDimitry Andric TotalObjectBss);
85762cfcf62SDimitry Andric fmtbuf.clear();
85862cfcf62SDimitry Andric fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t"
85962cfcf62SDimitry Andric << "%7" PRIx64 "\t";
86062cfcf62SDimitry Andric outs() << format(fmt.str().c_str(), TotalObjectTotal, TotalObjectTotal)
86162cfcf62SDimitry Andric << "(TOTALS)\n";
86262cfcf62SDimitry Andric }
86362cfcf62SDimitry Andric
llvm_size_main(int argc,char ** argv,const llvm::ToolContext &)86406c3fb27SDimitry Andric int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
865fe6060f1SDimitry Andric BumpPtrAllocator A;
866fe6060f1SDimitry Andric StringSaver Saver(A);
867fe6060f1SDimitry Andric SizeOptTable Tbl;
86862cfcf62SDimitry Andric ToolName = argv[0];
869fcaf7f86SDimitry Andric opt::InputArgList Args =
870fcaf7f86SDimitry Andric Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
871fcaf7f86SDimitry Andric error(Msg);
872fcaf7f86SDimitry Andric exit(1);
873fcaf7f86SDimitry Andric });
874fe6060f1SDimitry Andric if (Args.hasArg(OPT_help)) {
875fe6060f1SDimitry Andric Tbl.printHelp(
876fe6060f1SDimitry Andric outs(),
877fe6060f1SDimitry Andric (Twine(ToolName) + " [options] <input object files>").str().c_str(),
878fe6060f1SDimitry Andric "LLVM object size dumper");
879fe6060f1SDimitry Andric // TODO Replace this with OptTable API once it adds extrahelp support.
880fe6060f1SDimitry Andric outs() << "\nPass @FILE as argument to read options from FILE.\n";
881fe6060f1SDimitry Andric return 0;
882fe6060f1SDimitry Andric }
883fe6060f1SDimitry Andric if (Args.hasArg(OPT_version)) {
884fe6060f1SDimitry Andric outs() << ToolName << '\n';
885fe6060f1SDimitry Andric cl::PrintVersionMessage();
886fe6060f1SDimitry Andric return 0;
887fe6060f1SDimitry Andric }
88862cfcf62SDimitry Andric
889fe6060f1SDimitry Andric ELFCommons = Args.hasArg(OPT_common);
890fe6060f1SDimitry Andric DarwinLongFormat = Args.hasArg(OPT_l);
891fe6060f1SDimitry Andric TotalSizes = Args.hasArg(OPT_totals);
892fe6060f1SDimitry Andric StringRef V = Args.getLastArgValue(OPT_format_EQ, "berkeley");
893fe6060f1SDimitry Andric if (V == "berkeley")
894fe6060f1SDimitry Andric OutputFormat = berkeley;
895fe6060f1SDimitry Andric else if (V == "darwin")
896fe6060f1SDimitry Andric OutputFormat = darwin;
897fe6060f1SDimitry Andric else if (V == "sysv")
898fe6060f1SDimitry Andric OutputFormat = sysv;
899fe6060f1SDimitry Andric else
900fe6060f1SDimitry Andric error("--format value should be one of: 'berkeley', 'darwin', 'sysv'");
901fe6060f1SDimitry Andric V = Args.getLastArgValue(OPT_radix_EQ, "10");
902fe6060f1SDimitry Andric if (V == "8")
903fe6060f1SDimitry Andric Radix = RadixTy::octal;
904fe6060f1SDimitry Andric else if (V == "10")
905fe6060f1SDimitry Andric Radix = RadixTy::decimal;
906fe6060f1SDimitry Andric else if (V == "16")
907fe6060f1SDimitry Andric Radix = RadixTy::hexadecimal;
908fe6060f1SDimitry Andric else
909fe6060f1SDimitry Andric error("--radix value should be one of: 8, 10, 16 ");
910fe6060f1SDimitry Andric
911fe6060f1SDimitry Andric for (const auto *A : Args.filtered(OPT_arch_EQ)) {
912fe6060f1SDimitry Andric SmallVector<StringRef, 2> Values;
913fe6060f1SDimitry Andric llvm::SplitString(A->getValue(), Values, ",");
914fe6060f1SDimitry Andric for (StringRef V : Values) {
915fe6060f1SDimitry Andric if (V == "all")
91662cfcf62SDimitry Andric ArchAll = true;
917fe6060f1SDimitry Andric else if (MachOObjectFile::isValidArch(V))
918fe6060f1SDimitry Andric ArchFlags.push_back(V);
919fe6060f1SDimitry Andric else {
92062cfcf62SDimitry Andric outs() << ToolName << ": for the -arch option: Unknown architecture "
921fe6060f1SDimitry Andric << "named '" << V << "'";
92262cfcf62SDimitry Andric return 1;
92362cfcf62SDimitry Andric }
92462cfcf62SDimitry Andric }
92562cfcf62SDimitry Andric }
92662cfcf62SDimitry Andric
927fe6060f1SDimitry Andric InputFilenames = Args.getAllArgValues(OPT_INPUT);
92862cfcf62SDimitry Andric if (InputFilenames.empty())
92962cfcf62SDimitry Andric InputFilenames.push_back("a.out");
93062cfcf62SDimitry Andric
93162cfcf62SDimitry Andric MoreThanOneFile = InputFilenames.size() > 1;
93262cfcf62SDimitry Andric llvm::for_each(InputFilenames, printFileSectionSizes);
93362cfcf62SDimitry Andric if (OutputFormat == berkeley && TotalSizes)
93462cfcf62SDimitry Andric printBerkeleyTotals();
93562cfcf62SDimitry Andric
93662cfcf62SDimitry Andric if (HadError)
93762cfcf62SDimitry Andric return 1;
938bdd1243dSDimitry Andric return 0;
93962cfcf62SDimitry Andric }
940