Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 1 | //===-- llvm-size.cpp - Print the size of each object section ---*- C++ -*-===// |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 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" |
Rafael Espindola | a0ff556 | 2016-02-09 21:39:49 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ELFObjectFile.h" |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 19 | #include "llvm/Object/MachO.h" |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 20 | #include "llvm/Object/MachOUniversal.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 21 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Casting.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/FileSystem.h" |
| 25 | #include "llvm/Support/Format.h" |
| 26 | #include "llvm/Support/ManagedStatic.h" |
| 27 | #include "llvm/Support/MemoryBuffer.h" |
| 28 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Signals.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 33 | #include <system_error> |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 34 | |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | using namespace object; |
| 37 | |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 38 | enum OutputFormatTy { berkeley, sysv, darwin }; |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 39 | static cl::opt<OutputFormatTy> |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 40 | OutputFormat("format", cl::desc("Specify output format"), |
| 41 | cl::values(clEnumVal(sysv, "System V format"), |
| 42 | clEnumVal(berkeley, "Berkeley format"), |
| 43 | clEnumVal(darwin, "Darwin -m format"), clEnumValEnd), |
| 44 | cl::init(berkeley)); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 45 | |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 46 | static cl::opt<OutputFormatTy> OutputFormatShort( |
| 47 | cl::desc("Specify output format"), |
| 48 | cl::values(clEnumValN(sysv, "A", "System V format"), |
| 49 | clEnumValN(berkeley, "B", "Berkeley format"), |
| 50 | clEnumValN(darwin, "m", "Darwin -m format"), clEnumValEnd), |
| 51 | cl::init(berkeley)); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 53 | static bool BerkeleyHeaderPrinted = false; |
| 54 | static bool MoreThanOneFile = false; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 55 | |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 56 | cl::opt<bool> |
| 57 | DarwinLongFormat("l", cl::desc("When format is darwin, use long format " |
| 58 | "to include addresses and offsets.")); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 59 | |
Hemant Kulkarni | 274457e | 2016-03-28 16:48:10 +0000 | [diff] [blame] | 60 | cl::opt<bool> |
| 61 | ELFCommons("common", |
| 62 | cl::desc("Print common symbols in the ELF file. When using " |
| 63 | "Berkely format, this is added to bss."), |
| 64 | cl::init(false)); |
| 65 | |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 66 | static cl::list<std::string> |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 67 | ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"), |
| 68 | cl::ZeroOrMore); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 69 | bool ArchAll = false; |
| 70 | |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 71 | enum RadixTy { octal = 8, decimal = 10, hexadecimal = 16 }; |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 72 | static cl::opt<unsigned int> |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 73 | Radix("-radix", cl::desc("Print size in radix. Only 8, 10, and 16 are valid"), |
| 74 | cl::init(decimal)); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 75 | |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 76 | static cl::opt<RadixTy> |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 77 | RadixShort(cl::desc("Print size in radix:"), |
| 78 | cl::values(clEnumValN(octal, "o", "Print size in octal"), |
| 79 | clEnumValN(decimal, "d", "Print size in decimal"), |
| 80 | clEnumValN(hexadecimal, "x", "Print size in hexadecimal"), |
| 81 | clEnumValEnd), |
| 82 | cl::init(decimal)); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 83 | |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 84 | static cl::list<std::string> |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 85 | InputFilenames(cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 86 | |
Kevin Enderby | 64f7a99 | 2016-05-02 21:41:03 +0000 | [diff] [blame] | 87 | bool HadError = false; |
| 88 | |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 89 | static std::string ToolName; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 90 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 91 | /// If ec is not success, print the error and return true. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 92 | static bool error(std::error_code ec) { |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 93 | if (!ec) |
| 94 | return false; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 95 | |
Kevin Enderby | 64f7a99 | 2016-05-02 21:41:03 +0000 | [diff] [blame] | 96 | HadError = true; |
Davide Italiano | 911eb31 | 2016-01-25 01:24:15 +0000 | [diff] [blame] | 97 | errs() << ToolName << ": error reading file: " << ec.message() << ".\n"; |
| 98 | errs().flush(); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 102 | // This version of error() prints the archive name and member name, for example: |
| 103 | // "libx.a(foo.o)" after the ToolName before the error message. It sets |
| 104 | // HadError but returns allowing the code to move on to other archive members. |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 105 | static void error(llvm::Error E, StringRef FileName, const Archive::Child &C, |
| 106 | StringRef ArchitectureName = StringRef()) { |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 107 | HadError = true; |
| 108 | errs() << ToolName << ": " << FileName; |
| 109 | |
| 110 | ErrorOr<StringRef> NameOrErr = C.getName(); |
| 111 | // TODO: if we have a error getting the name then it would be nice to print |
| 112 | // the index of which archive member this is and or its offset in the |
| 113 | // archive instead of "???" as the name. |
| 114 | if (NameOrErr.getError()) |
| 115 | errs() << "(" << "???" << ")"; |
| 116 | else |
| 117 | errs() << "(" << NameOrErr.get() << ")"; |
| 118 | |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 119 | if (!ArchitectureName.empty()) |
| 120 | errs() << " (for architecture " << ArchitectureName << ") "; |
| 121 | |
| 122 | std::string Buf; |
| 123 | raw_string_ostream OS(Buf); |
| 124 | logAllUnhandledErrors(std::move(E), OS, ""); |
| 125 | OS.flush(); |
| 126 | errs() << " " << Buf << "\n"; |
| 127 | } |
| 128 | |
| 129 | // 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 |
| 130 | // before the error message. It sets HadError but returns allowing the code to |
| 131 | // move on to other architecture slices. |
| 132 | static void error(llvm::Error E, StringRef FileName, |
| 133 | StringRef ArchitectureName = StringRef()) { |
| 134 | HadError = true; |
| 135 | errs() << ToolName << ": " << FileName; |
| 136 | |
| 137 | if (!ArchitectureName.empty()) |
| 138 | errs() << " (for architecture " << ArchitectureName << ") "; |
| 139 | |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 140 | std::string Buf; |
| 141 | raw_string_ostream OS(Buf); |
| 142 | logAllUnhandledErrors(std::move(E), OS, ""); |
| 143 | OS.flush(); |
| 144 | errs() << " " << Buf << "\n"; |
| 145 | } |
| 146 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 147 | /// Get the length of the string that represents @p num in Radix including the |
| 148 | /// leading 0x or 0 for hexadecimal and octal respectively. |
Andrew Trick | 7dc278d | 2011-09-29 01:22:31 +0000 | [diff] [blame] | 149 | static size_t getNumLengthAsString(uint64_t num) { |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 150 | APInt conv(64, num); |
| 151 | SmallString<32> result; |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 152 | conv.toString(result, Radix, false, true); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 153 | return result.size(); |
| 154 | } |
| 155 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 156 | /// Return the printing format for the Radix. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 157 | static const char *getRadixFmt() { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 158 | switch (Radix) { |
| 159 | case octal: |
| 160 | return PRIo64; |
| 161 | case decimal: |
| 162 | return PRIu64; |
| 163 | case hexadecimal: |
| 164 | return PRIx64; |
| 165 | } |
| 166 | return nullptr; |
| 167 | } |
| 168 | |
Rafael Espindola | a0ff556 | 2016-02-09 21:39:49 +0000 | [diff] [blame] | 169 | /// Remove unneeded ELF sections from calculation |
| 170 | static bool considerForSize(ObjectFile *Obj, SectionRef Section) { |
| 171 | if (!Obj->isELF()) |
| 172 | return true; |
| 173 | switch (static_cast<ELFSectionRef>(Section).getType()) { |
| 174 | case ELF::SHT_NULL: |
| 175 | case ELF::SHT_SYMTAB: |
| 176 | case ELF::SHT_STRTAB: |
| 177 | case ELF::SHT_REL: |
| 178 | case ELF::SHT_RELA: |
| 179 | return false; |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | |
Hemant Kulkarni | 274457e | 2016-03-28 16:48:10 +0000 | [diff] [blame] | 184 | /// Total size of all ELF common symbols |
| 185 | static uint64_t getCommonSize(ObjectFile *Obj) { |
| 186 | uint64_t TotalCommons = 0; |
| 187 | for (auto &Sym : Obj->symbols()) |
| 188 | if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common) |
| 189 | TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl()); |
| 190 | return TotalCommons; |
| 191 | } |
| 192 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 193 | /// Print the size of each Mach-O segment and section in @p MachO. |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 194 | /// |
| 195 | /// This is when used when @c OutputFormat is darwin and produces the same |
| 196 | /// output as darwin's size(1) -m output. |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 197 | static void printDarwinSectionSizes(MachOObjectFile *MachO) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 198 | std::string fmtbuf; |
| 199 | raw_string_ostream fmt(fmtbuf); |
| 200 | const char *radix_fmt = getRadixFmt(); |
| 201 | if (Radix == hexadecimal) |
| 202 | fmt << "0x"; |
| 203 | fmt << "%" << radix_fmt; |
| 204 | |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 205 | uint32_t Filetype = MachO->getHeader().filetype; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 206 | |
| 207 | uint64_t total = 0; |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 208 | for (const auto &Load : MachO->load_commands()) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 209 | if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
| 210 | MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load); |
| 211 | outs() << "Segment " << Seg.segname << ": " |
| 212 | << format(fmt.str().c_str(), Seg.vmsize); |
| 213 | if (DarwinLongFormat) |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 214 | outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff " |
| 215 | << Seg.fileoff << ")"; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 216 | outs() << "\n"; |
| 217 | total += Seg.vmsize; |
| 218 | uint64_t sec_total = 0; |
| 219 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 220 | MachO::section_64 Sec = MachO->getSection64(Load, J); |
| 221 | if (Filetype == MachO::MH_OBJECT) |
| 222 | outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", " |
| 223 | << format("%.16s", &Sec.sectname) << "): "; |
| 224 | else |
| 225 | outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": "; |
| 226 | outs() << format(fmt.str().c_str(), Sec.size); |
| 227 | if (DarwinLongFormat) |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 228 | outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset " |
| 229 | << Sec.offset << ")"; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 230 | outs() << "\n"; |
| 231 | sec_total += Sec.size; |
| 232 | } |
| 233 | if (Seg.nsects != 0) |
| 234 | outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 235 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 236 | MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load); |
Kevin Enderby | 2058e9d | 2016-02-09 18:33:15 +0000 | [diff] [blame] | 237 | uint64_t Seg_vmsize = Seg.vmsize; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 238 | outs() << "Segment " << Seg.segname << ": " |
Kevin Enderby | 2058e9d | 2016-02-09 18:33:15 +0000 | [diff] [blame] | 239 | << format(fmt.str().c_str(), Seg_vmsize); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 240 | if (DarwinLongFormat) |
Kevin Enderby | 2058e9d | 2016-02-09 18:33:15 +0000 | [diff] [blame] | 241 | outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff " |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 242 | << Seg.fileoff << ")"; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 243 | outs() << "\n"; |
| 244 | total += Seg.vmsize; |
| 245 | uint64_t sec_total = 0; |
| 246 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 247 | MachO::section Sec = MachO->getSection(Load, J); |
| 248 | if (Filetype == MachO::MH_OBJECT) |
| 249 | outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", " |
| 250 | << format("%.16s", &Sec.sectname) << "): "; |
| 251 | else |
| 252 | outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": "; |
Kevin Enderby | 2058e9d | 2016-02-09 18:33:15 +0000 | [diff] [blame] | 253 | uint64_t Sec_size = Sec.size; |
| 254 | outs() << format(fmt.str().c_str(), Sec_size); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 255 | if (DarwinLongFormat) |
Kevin Enderby | 2058e9d | 2016-02-09 18:33:15 +0000 | [diff] [blame] | 256 | outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset " |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 257 | << Sec.offset << ")"; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 258 | outs() << "\n"; |
| 259 | sec_total += Sec.size; |
| 260 | } |
| 261 | if (Seg.nsects != 0) |
| 262 | outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n"; |
| 263 | } |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 264 | } |
| 265 | outs() << "total " << format(fmt.str().c_str(), total) << "\n"; |
| 266 | } |
| 267 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 268 | /// Print the summary sizes of the standard Mach-O segments in @p MachO. |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 269 | /// |
| 270 | /// This is when used when @c OutputFormat is berkeley with a Mach-O file and |
| 271 | /// produces the same output as darwin's size(1) default output. |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 272 | static void printDarwinSegmentSizes(MachOObjectFile *MachO) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 273 | uint64_t total_text = 0; |
| 274 | uint64_t total_data = 0; |
| 275 | uint64_t total_objc = 0; |
| 276 | uint64_t total_others = 0; |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 277 | for (const auto &Load : MachO->load_commands()) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 278 | if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
| 279 | MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load); |
| 280 | if (MachO->getHeader().filetype == MachO::MH_OBJECT) { |
| 281 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 282 | MachO::section_64 Sec = MachO->getSection64(Load, J); |
| 283 | StringRef SegmentName = StringRef(Sec.segname); |
| 284 | if (SegmentName == "__TEXT") |
| 285 | total_text += Sec.size; |
| 286 | else if (SegmentName == "__DATA") |
| 287 | total_data += Sec.size; |
| 288 | else if (SegmentName == "__OBJC") |
| 289 | total_objc += Sec.size; |
| 290 | else |
| 291 | total_others += Sec.size; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 292 | } |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 293 | } else { |
| 294 | StringRef SegmentName = StringRef(Seg.segname); |
| 295 | if (SegmentName == "__TEXT") |
| 296 | total_text += Seg.vmsize; |
| 297 | else if (SegmentName == "__DATA") |
| 298 | total_data += Seg.vmsize; |
| 299 | else if (SegmentName == "__OBJC") |
| 300 | total_objc += Seg.vmsize; |
| 301 | else |
| 302 | total_others += Seg.vmsize; |
| 303 | } |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 304 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 305 | MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load); |
| 306 | if (MachO->getHeader().filetype == MachO::MH_OBJECT) { |
| 307 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 308 | MachO::section Sec = MachO->getSection(Load, J); |
| 309 | StringRef SegmentName = StringRef(Sec.segname); |
| 310 | if (SegmentName == "__TEXT") |
| 311 | total_text += Sec.size; |
| 312 | else if (SegmentName == "__DATA") |
| 313 | total_data += Sec.size; |
| 314 | else if (SegmentName == "__OBJC") |
| 315 | total_objc += Sec.size; |
| 316 | else |
| 317 | total_others += Sec.size; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 318 | } |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 319 | } else { |
| 320 | StringRef SegmentName = StringRef(Seg.segname); |
| 321 | if (SegmentName == "__TEXT") |
| 322 | total_text += Seg.vmsize; |
| 323 | else if (SegmentName == "__DATA") |
| 324 | total_data += Seg.vmsize; |
| 325 | else if (SegmentName == "__OBJC") |
| 326 | total_objc += Seg.vmsize; |
| 327 | else |
| 328 | total_others += Seg.vmsize; |
| 329 | } |
| 330 | } |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 331 | } |
| 332 | uint64_t total = total_text + total_data + total_objc + total_others; |
| 333 | |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 334 | if (!BerkeleyHeaderPrinted) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 335 | outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n"; |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 336 | BerkeleyHeaderPrinted = true; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 337 | } |
| 338 | outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t" |
| 339 | << total_others << "\t" << total << "\t" << format("%" PRIx64, total) |
| 340 | << "\t"; |
| 341 | } |
| 342 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 343 | /// Print the size of each section in @p Obj. |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 344 | /// |
| 345 | /// The format used is determined by @c OutputFormat and @c Radix. |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 346 | static void printObjectSectionSizes(ObjectFile *Obj) { |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 347 | uint64_t total = 0; |
| 348 | std::string fmtbuf; |
| 349 | raw_string_ostream fmt(fmtbuf); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 350 | const char *radix_fmt = getRadixFmt(); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 351 | |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 352 | // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's |
| 353 | // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object |
| 354 | // let it fall through to OutputFormat berkeley. |
| 355 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj); |
| 356 | if (OutputFormat == darwin && MachO) |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 357 | printDarwinSectionSizes(MachO); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 358 | // If we have a MachOObjectFile and the OutputFormat is berkeley print as |
| 359 | // darwin's default berkeley format for Mach-O files. |
| 360 | else if (MachO && OutputFormat == berkeley) |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 361 | printDarwinSegmentSizes(MachO); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 362 | else if (OutputFormat == sysv) { |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 363 | // Run two passes over all sections. The first gets the lengths needed for |
| 364 | // formatting the output. The second actually does the output. |
| 365 | std::size_t max_name_len = strlen("section"); |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 366 | std::size_t max_size_len = strlen("size"); |
| 367 | std::size_t max_addr_len = strlen("addr"); |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 368 | for (const SectionRef &Section : Obj->sections()) { |
Rafael Espindola | a0ff556 | 2016-02-09 21:39:49 +0000 | [diff] [blame] | 369 | if (!considerForSize(Obj, Section)) |
| 370 | continue; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 371 | uint64_t size = Section.getSize(); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 372 | total += size; |
| 373 | |
| 374 | StringRef name; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 375 | if (error(Section.getName(name))) |
| 376 | return; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 377 | uint64_t addr = Section.getAddress(); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 378 | max_name_len = std::max(max_name_len, name.size()); |
Andrew Trick | 7dc278d | 2011-09-29 01:22:31 +0000 | [diff] [blame] | 379 | max_size_len = std::max(max_size_len, getNumLengthAsString(size)); |
| 380 | max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr)); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 383 | // Add extra padding. |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 384 | max_name_len += 2; |
| 385 | max_size_len += 2; |
| 386 | max_addr_len += 2; |
| 387 | |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 388 | // Setup header format. |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 389 | fmt << "%-" << max_name_len << "s " |
| 390 | << "%" << max_size_len << "s " |
| 391 | << "%" << max_addr_len << "s\n"; |
| 392 | |
| 393 | // Print header |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 394 | outs() << format(fmt.str().c_str(), static_cast<const char *>("section"), |
| 395 | static_cast<const char *>("size"), |
| 396 | static_cast<const char *>("addr")); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 397 | fmtbuf.clear(); |
| 398 | |
| 399 | // Setup per section format. |
| 400 | fmt << "%-" << max_name_len << "s " |
| 401 | << "%#" << max_size_len << radix_fmt << " " |
| 402 | << "%#" << max_addr_len << radix_fmt << "\n"; |
| 403 | |
| 404 | // Print each section. |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 405 | for (const SectionRef &Section : Obj->sections()) { |
Rafael Espindola | a0ff556 | 2016-02-09 21:39:49 +0000 | [diff] [blame] | 406 | if (!considerForSize(Obj, Section)) |
| 407 | continue; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 408 | StringRef name; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 409 | if (error(Section.getName(name))) |
| 410 | return; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 411 | uint64_t size = Section.getSize(); |
| 412 | uint64_t addr = Section.getAddress(); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 413 | std::string namestr = name; |
| 414 | |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 415 | outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Hemant Kulkarni | 274457e | 2016-03-28 16:48:10 +0000 | [diff] [blame] | 418 | if (ELFCommons) { |
| 419 | uint64_t CommonSize = getCommonSize(Obj); |
| 420 | total += CommonSize; |
| 421 | outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(), |
| 422 | CommonSize, static_cast<uint64_t>(0)); |
| 423 | } |
| 424 | |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 425 | // Print total. |
| 426 | fmtbuf.clear(); |
| 427 | fmt << "%-" << max_name_len << "s " |
| 428 | << "%#" << max_size_len << radix_fmt << "\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 429 | outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"), |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 430 | total); |
| 431 | } else { |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 432 | // The Berkeley format does not display individual section sizes. It |
| 433 | // displays the cumulative size for each section type. |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 434 | uint64_t total_text = 0; |
| 435 | uint64_t total_data = 0; |
| 436 | uint64_t total_bss = 0; |
| 437 | |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 438 | // Make one pass over the section table to calculate sizes. |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 439 | for (const SectionRef &Section : Obj->sections()) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 440 | uint64_t size = Section.getSize(); |
| 441 | bool isText = Section.isText(); |
| 442 | bool isData = Section.isData(); |
| 443 | bool isBSS = Section.isBSS(); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 444 | if (isText) |
| 445 | total_text += size; |
| 446 | else if (isData) |
| 447 | total_data += size; |
| 448 | else if (isBSS) |
| 449 | total_bss += size; |
| 450 | } |
| 451 | |
Hemant Kulkarni | 274457e | 2016-03-28 16:48:10 +0000 | [diff] [blame] | 452 | if (ELFCommons) |
| 453 | total_bss += getCommonSize(Obj); |
| 454 | |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 455 | total = total_text + total_data + total_bss; |
| 456 | |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 457 | if (!BerkeleyHeaderPrinted) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 458 | outs() << " text data bss " |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 459 | << (Radix == octal ? "oct" : "dec") << " hex filename\n"; |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 460 | BerkeleyHeaderPrinted = true; |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 463 | // Print result. |
| 464 | fmt << "%#7" << radix_fmt << " " |
| 465 | << "%#7" << radix_fmt << " " |
| 466 | << "%#7" << radix_fmt << " "; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 467 | outs() << format(fmt.str().c_str(), total_text, total_data, total_bss); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 468 | fmtbuf.clear(); |
Benjamin Kramer | f3da529 | 2011-11-05 08:57:40 +0000 | [diff] [blame] | 469 | fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " " |
| 470 | << "%7" PRIx64 " "; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 471 | outs() << format(fmt.str().c_str(), total, total); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 475 | /// Checks to see if the @p o ObjectFile is a Mach-O file and if it is and there |
| 476 | /// is a list of architecture flags specified then check to make sure this |
| 477 | /// Mach-O file is one of those architectures or all architectures was |
| 478 | /// specificed. If not then an error is generated and this routine returns |
| 479 | /// false. Else it returns true. |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 480 | static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) { |
| 481 | if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) { |
| 482 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
| 483 | bool ArchFound = false; |
| 484 | MachO::mach_header H; |
| 485 | MachO::mach_header_64 H_64; |
| 486 | Triple T; |
| 487 | if (MachO->is64Bit()) { |
| 488 | H_64 = MachO->MachOObjectFile::getHeader64(); |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 489 | T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 490 | } else { |
| 491 | H = MachO->MachOObjectFile::getHeader(); |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 492 | T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 493 | } |
| 494 | unsigned i; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 495 | for (i = 0; i < ArchFlags.size(); ++i) { |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 496 | if (ArchFlags[i] == T.getArchName()) |
| 497 | ArchFound = true; |
| 498 | break; |
| 499 | } |
| 500 | if (!ArchFound) { |
| 501 | errs() << ToolName << ": file: " << file |
| 502 | << " does not contain architecture: " << ArchFlags[i] << ".\n"; |
| 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
Rafael Espindola | 49a0e5e | 2016-02-09 21:32:56 +0000 | [diff] [blame] | 509 | /// Print the section sizes for @p file. If @p file is an archive, print the |
| 510 | /// section sizes for each archive member. |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 511 | static void printFileSectionSizes(StringRef file) { |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 512 | |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 513 | // Attempt to open the binary. |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 514 | Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file); |
| 515 | if (!BinaryOrErr) { |
| 516 | error(errorToErrorCode(BinaryOrErr.takeError())); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 517 | return; |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 518 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 519 | Binary &Bin = *BinaryOrErr.get().getBinary(); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 520 | |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 521 | if (Archive *a = dyn_cast<Archive>(&Bin)) { |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 522 | // This is an archive. Iterate over each member and display its sizes. |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 523 | for (object::Archive::child_iterator i = a->child_begin(), |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 524 | e = a->child_end(); |
| 525 | i != e; ++i) { |
Davide Italiano | ff11b90 | 2016-02-13 01:38:16 +0000 | [diff] [blame] | 526 | if (error(i->getError())) |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 527 | exit(1); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 528 | Expected<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary(); |
| 529 | if (!ChildOrErr) { |
| 530 | if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) |
| 531 | error(std::move(E), a->getFileName(), i->get()); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 532 | continue; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 533 | } |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 534 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) { |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 535 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 536 | if (!checkMachOAndArchFlags(o, file)) |
| 537 | return; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 538 | if (OutputFormat == sysv) |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 539 | outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n"; |
| 540 | else if (MachO && OutputFormat == darwin) |
| 541 | outs() << a->getFileName() << "(" << o->getFileName() << "):\n"; |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 542 | printObjectSectionSizes(o); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 543 | if (OutputFormat == berkeley) { |
| 544 | if (MachO) |
| 545 | outs() << a->getFileName() << "(" << o->getFileName() << ")\n"; |
| 546 | else |
| 547 | outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n"; |
| 548 | } |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 551 | } else if (MachOUniversalBinary *UB = |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 552 | dyn_cast<MachOUniversalBinary>(&Bin)) { |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 553 | // If we have a list of architecture flags specified dump only those. |
| 554 | if (!ArchAll && ArchFlags.size() != 0) { |
| 555 | // Look for a slice in the universal binary that matches each ArchFlag. |
| 556 | bool ArchFound; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 557 | for (unsigned i = 0; i < ArchFlags.size(); ++i) { |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 558 | ArchFound = false; |
| 559 | for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), |
| 560 | E = UB->end_objects(); |
| 561 | I != E; ++I) { |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 562 | if (ArchFlags[i] == I->getArchTypeName()) { |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 563 | ArchFound = true; |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 564 | Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile(); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 565 | if (UO) { |
| 566 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) { |
| 567 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
| 568 | if (OutputFormat == sysv) |
| 569 | outs() << o->getFileName() << " :\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 570 | else if (MachO && OutputFormat == darwin) { |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 571 | if (MoreThanOneFile || ArchFlags.size() > 1) |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 572 | outs() << o->getFileName() << " (for architecture " |
| 573 | << I->getArchTypeName() << "): \n"; |
| 574 | } |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 575 | printObjectSectionSizes(o); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 576 | if (OutputFormat == berkeley) { |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 577 | if (!MachO || MoreThanOneFile || ArchFlags.size() > 1) |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 578 | outs() << o->getFileName() << " (for architecture " |
| 579 | << I->getArchTypeName() << ")"; |
| 580 | outs() << "\n"; |
| 581 | } |
| 582 | } |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 583 | } else if (auto E = isNotObjectErrorInvalidFileType( |
| 584 | UO.takeError())) { |
| 585 | error(std::move(E), file, ArchFlags.size() > 1 ? |
| 586 | StringRef(I->getArchTypeName()) : StringRef()); |
| 587 | return; |
Rafael Espindola | 0bfe828 | 2014-12-09 21:05:36 +0000 | [diff] [blame] | 588 | } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = |
| 589 | I->getAsArchive()) { |
| 590 | std::unique_ptr<Archive> &UA = *AOrErr; |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 591 | // This is an archive. Iterate over each member and display its |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 592 | // sizes. |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 593 | for (object::Archive::child_iterator i = UA->child_begin(), |
| 594 | e = UA->child_end(); |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 595 | i != e; ++i) { |
Davide Italiano | ff11b90 | 2016-02-13 01:38:16 +0000 | [diff] [blame] | 596 | if (error(i->getError())) |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 597 | exit(1); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 598 | Expected<std::unique_ptr<Binary>> ChildOrErr = |
| 599 | i->get().getAsBinary(); |
| 600 | if (!ChildOrErr) { |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 601 | if (auto E = isNotObjectErrorInvalidFileType( |
| 602 | ChildOrErr.takeError())) |
| 603 | error(std::move(E), UA->getFileName(), i->get(), |
| 604 | ArchFlags.size() > 1 ? |
| 605 | StringRef(I->getArchTypeName()) : StringRef()); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 606 | continue; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 607 | } |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 608 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) { |
| 609 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
| 610 | if (OutputFormat == sysv) |
| 611 | outs() << o->getFileName() << " (ex " << UA->getFileName() |
| 612 | << "):\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 613 | else if (MachO && OutputFormat == darwin) |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 614 | outs() << UA->getFileName() << "(" << o->getFileName() |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 615 | << ")" |
| 616 | << " (for architecture " << I->getArchTypeName() |
| 617 | << "):\n"; |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 618 | printObjectSectionSizes(o); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 619 | if (OutputFormat == berkeley) { |
| 620 | if (MachO) { |
| 621 | outs() << UA->getFileName() << "(" << o->getFileName() |
| 622 | << ")"; |
| 623 | if (ArchFlags.size() > 1) |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 624 | outs() << " (for architecture " << I->getArchTypeName() |
| 625 | << ")"; |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 626 | outs() << "\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 627 | } else |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 628 | outs() << o->getFileName() << " (ex " << UA->getFileName() |
| 629 | << ")\n"; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | if (!ArchFound) { |
| 637 | errs() << ToolName << ": file: " << file |
| 638 | << " does not contain architecture" << ArchFlags[i] << ".\n"; |
| 639 | return; |
| 640 | } |
| 641 | } |
| 642 | return; |
| 643 | } |
| 644 | // No architecture flags were specified so if this contains a slice that |
| 645 | // matches the host architecture dump only that. |
| 646 | if (!ArchAll) { |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 647 | StringRef HostArchName = MachOObjectFile::getHostArch().getArchName(); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 648 | for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), |
| 649 | E = UB->end_objects(); |
| 650 | I != E; ++I) { |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 651 | if (HostArchName == I->getArchTypeName()) { |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 652 | Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile(); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 653 | if (UO) { |
| 654 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) { |
| 655 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
| 656 | if (OutputFormat == sysv) |
| 657 | outs() << o->getFileName() << " :\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 658 | else if (MachO && OutputFormat == darwin) { |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 659 | if (MoreThanOneFile) |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 660 | outs() << o->getFileName() << " (for architecture " |
| 661 | << I->getArchTypeName() << "):\n"; |
| 662 | } |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 663 | printObjectSectionSizes(o); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 664 | if (OutputFormat == berkeley) { |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 665 | if (!MachO || MoreThanOneFile) |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 666 | outs() << o->getFileName() << " (for architecture " |
| 667 | << I->getArchTypeName() << ")"; |
| 668 | outs() << "\n"; |
| 669 | } |
| 670 | } |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 671 | } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) { |
| 672 | error(std::move(E), file); |
| 673 | return; |
Rafael Espindola | 0bfe828 | 2014-12-09 21:05:36 +0000 | [diff] [blame] | 674 | } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = |
| 675 | I->getAsArchive()) { |
| 676 | std::unique_ptr<Archive> &UA = *AOrErr; |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 677 | // This is an archive. Iterate over each member and display its |
| 678 | // sizes. |
| 679 | for (object::Archive::child_iterator i = UA->child_begin(), |
| 680 | e = UA->child_end(); |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 681 | i != e; ++i) { |
Davide Italiano | ff11b90 | 2016-02-13 01:38:16 +0000 | [diff] [blame] | 682 | if (error(i->getError())) |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 683 | exit(1); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 684 | Expected<std::unique_ptr<Binary>> ChildOrErr = |
| 685 | i->get().getAsBinary(); |
| 686 | if (!ChildOrErr) { |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 687 | if (auto E = isNotObjectErrorInvalidFileType( |
| 688 | ChildOrErr.takeError())) |
| 689 | error(std::move(E), UA->getFileName(), i->get()); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 690 | continue; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 691 | } |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 692 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) { |
| 693 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
| 694 | if (OutputFormat == sysv) |
| 695 | outs() << o->getFileName() << " (ex " << UA->getFileName() |
| 696 | << "):\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 697 | else if (MachO && OutputFormat == darwin) |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 698 | outs() << UA->getFileName() << "(" << o->getFileName() << ")" |
| 699 | << " (for architecture " << I->getArchTypeName() |
| 700 | << "):\n"; |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 701 | printObjectSectionSizes(o); |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 702 | if (OutputFormat == berkeley) { |
| 703 | if (MachO) |
| 704 | outs() << UA->getFileName() << "(" << o->getFileName() |
| 705 | << ")\n"; |
| 706 | else |
| 707 | outs() << o->getFileName() << " (ex " << UA->getFileName() |
| 708 | << ")\n"; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | return; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | // Either all architectures have been specified or none have been specified |
| 718 | // and this does not contain the host architecture so dump all the slices. |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 719 | bool MoreThanOneArch = UB->getNumberOfObjects() > 1; |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 720 | for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), |
| 721 | E = UB->end_objects(); |
| 722 | I != E; ++I) { |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 723 | Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile(); |
Rafael Espindola | 4f7932b | 2014-06-23 20:41:02 +0000 | [diff] [blame] | 724 | if (UO) { |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 725 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) { |
Kevin Enderby | 1983fcf | 2014-06-19 22:03:18 +0000 | [diff] [blame] | 726 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 727 | if (OutputFormat == sysv) |
| 728 | outs() << o->getFileName() << " :\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 729 | else if (MachO && OutputFormat == darwin) { |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 730 | if (MoreThanOneFile || MoreThanOneArch) |
Kevin Enderby | 1983fcf | 2014-06-19 22:03:18 +0000 | [diff] [blame] | 731 | outs() << o->getFileName() << " (for architecture " |
| 732 | << I->getArchTypeName() << "):"; |
| 733 | outs() << "\n"; |
| 734 | } |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 735 | printObjectSectionSizes(o); |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 736 | if (OutputFormat == berkeley) { |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 737 | if (!MachO || MoreThanOneFile || MoreThanOneArch) |
Kevin Enderby | 1983fcf | 2014-06-19 22:03:18 +0000 | [diff] [blame] | 738 | outs() << o->getFileName() << " (for architecture " |
| 739 | << I->getArchTypeName() << ")"; |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 740 | outs() << "\n"; |
| 741 | } |
| 742 | } |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 743 | } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) { |
| 744 | error(std::move(E), file, MoreThanOneArch ? |
| 745 | StringRef(I->getArchTypeName()) : StringRef()); |
| 746 | return; |
Rafael Espindola | 0bfe828 | 2014-12-09 21:05:36 +0000 | [diff] [blame] | 747 | } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = |
| 748 | I->getAsArchive()) { |
| 749 | std::unique_ptr<Archive> &UA = *AOrErr; |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 750 | // This is an archive. Iterate over each member and display its sizes. |
| 751 | for (object::Archive::child_iterator i = UA->child_begin(), |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 752 | e = UA->child_end(); |
| 753 | i != e; ++i) { |
Davide Italiano | ff11b90 | 2016-02-13 01:38:16 +0000 | [diff] [blame] | 754 | if (error(i->getError())) |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 755 | exit(1); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 756 | Expected<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary(); |
| 757 | if (!ChildOrErr) { |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame^] | 758 | if (auto E = isNotObjectErrorInvalidFileType( |
| 759 | ChildOrErr.takeError())) |
| 760 | error(std::move(E), UA->getFileName(), i->get(), MoreThanOneArch ? |
| 761 | StringRef(I->getArchTypeName()) : StringRef()); |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 762 | continue; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 763 | } |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 764 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) { |
| 765 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
| 766 | if (OutputFormat == sysv) |
| 767 | outs() << o->getFileName() << " (ex " << UA->getFileName() |
| 768 | << "):\n"; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 769 | else if (MachO && OutputFormat == darwin) |
Kevin Enderby | 1983fcf | 2014-06-19 22:03:18 +0000 | [diff] [blame] | 770 | outs() << UA->getFileName() << "(" << o->getFileName() << ")" |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 771 | << " (for architecture " << I->getArchTypeName() << "):\n"; |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 772 | printObjectSectionSizes(o); |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 773 | if (OutputFormat == berkeley) { |
| 774 | if (MachO) |
Kevin Enderby | 1983fcf | 2014-06-19 22:03:18 +0000 | [diff] [blame] | 775 | outs() << UA->getFileName() << "(" << o->getFileName() << ")" |
| 776 | << " (for architecture " << I->getArchTypeName() |
| 777 | << ")\n"; |
Kevin Enderby | 4b8fc28 | 2014-06-18 22:04:40 +0000 | [diff] [blame] | 778 | else |
| 779 | outs() << o->getFileName() << " (ex " << UA->getFileName() |
| 780 | << ")\n"; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | } |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 786 | } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) { |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 787 | if (!checkMachOAndArchFlags(o, file)) |
| 788 | return; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 789 | if (OutputFormat == sysv) |
| 790 | outs() << o->getFileName() << " :\n"; |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 791 | printObjectSectionSizes(o); |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 792 | if (OutputFormat == berkeley) { |
| 793 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o); |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 794 | if (!MachO || MoreThanOneFile) |
Kevin Enderby | 246a460 | 2014-06-17 17:54:13 +0000 | [diff] [blame] | 795 | outs() << o->getFileName(); |
| 796 | outs() << "\n"; |
| 797 | } |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 798 | } else { |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 799 | errs() << ToolName << ": " << file << ": " |
| 800 | << "Unrecognized file type.\n"; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 801 | } |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 802 | // System V adds an extra newline at the end of each file. |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 803 | if (OutputFormat == sysv) |
| 804 | outs() << "\n"; |
| 805 | } |
| 806 | |
| 807 | int main(int argc, char **argv) { |
| 808 | // Print a stack trace if we signal out. |
| 809 | sys::PrintStackTraceOnErrorSignal(); |
| 810 | PrettyStackTraceProgram X(argc, argv); |
| 811 | |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 812 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 813 | cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n"); |
| 814 | |
| 815 | ToolName = argv[0]; |
| 816 | if (OutputFormatShort.getNumOccurrences()) |
Chris Bieneman | e71fb5c | 2015-01-22 01:49:59 +0000 | [diff] [blame] | 817 | OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 818 | if (RadixShort.getNumOccurrences()) |
Michael J. Spencer | cc5f8d4 | 2011-09-29 00:59:18 +0000 | [diff] [blame] | 819 | Radix = RadixShort; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 820 | |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 821 | for (unsigned i = 0; i < ArchFlags.size(); ++i) { |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 822 | if (ArchFlags[i] == "all") { |
| 823 | ArchAll = true; |
Kevin Enderby | 1076974 | 2014-07-01 22:26:31 +0000 | [diff] [blame] | 824 | } else { |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 825 | if (!MachOObjectFile::isValidArch(ArchFlags[i])) { |
Kevin Enderby | afef4c9 | 2014-07-01 17:19:10 +0000 | [diff] [blame] | 826 | outs() << ToolName << ": for the -arch option: Unknown architecture " |
| 827 | << "named '" << ArchFlags[i] << "'"; |
| 828 | return 1; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 833 | if (InputFilenames.size() == 0) |
| 834 | InputFilenames.push_back("a.out"); |
| 835 | |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 836 | MoreThanOneFile = InputFilenames.size() > 1; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 837 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
Rafael Espindola | 1dc30a4 | 2016-02-09 21:35:14 +0000 | [diff] [blame] | 838 | printFileSectionSizes); |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 839 | |
Kevin Enderby | 64f7a99 | 2016-05-02 21:41:03 +0000 | [diff] [blame] | 840 | if (HadError) |
| 841 | return 1; |
Michael J. Spencer | c4ad466 | 2011-09-28 20:57:46 +0000 | [diff] [blame] | 842 | } |