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