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