blob: e3b487c388ffa8d60fe503c0058703d716f06e16 [file] [log] [blame]
Eugene Zelenkoffec81c2015-11-04 22:32:32 +00001//===-- llvm-size.cpp - Print the size of each object section ---*- C++ -*-===//
Michael J. Spencerc4ad4662011-09-28 20:57:46 +00002//
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 Espindolaa0ff5562016-02-09 21:39:49 +000018#include "llvm/Object/ELFObjectFile.h"
Kevin Enderby246a4602014-06-17 17:54:13 +000019#include "llvm/Object/MachO.h"
Kevin Enderby4b8fc282014-06-18 22:04:40 +000020#include "llvm/Object/MachOUniversal.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "llvm/Object/ObjectFile.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000022#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. Spencerc4ad4662011-09-28 20:57:46 +000029#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000031#include <algorithm>
32#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000033#include <system_error>
Eugene Zelenkoffec81c2015-11-04 22:32:32 +000034
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000035using namespace llvm;
36using namespace object;
37
Kevin Enderby10769742014-07-01 22:26:31 +000038enum OutputFormatTy { berkeley, sysv, darwin };
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000039static cl::opt<OutputFormatTy>
Kevin Enderby10769742014-07-01 22:26:31 +000040OutputFormat("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. Spencerc4ad4662011-09-28 20:57:46 +000045
Kevin Enderby10769742014-07-01 22:26:31 +000046static 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. Spencerc4ad4662011-09-28 20:57:46 +000052
Rafael Espindola1dc30a42016-02-09 21:35:14 +000053static bool BerkeleyHeaderPrinted = false;
54static bool MoreThanOneFile = false;
Kevin Enderby246a4602014-06-17 17:54:13 +000055
Kevin Enderby10769742014-07-01 22:26:31 +000056cl::opt<bool>
57DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
58 "to include addresses and offsets."));
Kevin Enderby246a4602014-06-17 17:54:13 +000059
Hemant Kulkarni274457e2016-03-28 16:48:10 +000060cl::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 Enderbyafef4c92014-07-01 17:19:10 +000066static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000067ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
68 cl::ZeroOrMore);
Kevin Enderbyafef4c92014-07-01 17:19:10 +000069bool ArchAll = false;
70
Kevin Enderby10769742014-07-01 22:26:31 +000071enum RadixTy { octal = 8, decimal = 10, hexadecimal = 16 };
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000072static cl::opt<unsigned int>
Kevin Enderby10769742014-07-01 22:26:31 +000073Radix("-radix", cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
74 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000075
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000076static cl::opt<RadixTy>
Kevin Enderby10769742014-07-01 22:26:31 +000077RadixShort(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. Spencerc4ad4662011-09-28 20:57:46 +000083
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000084static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000085InputFilenames(cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000086
Kevin Enderby64f7a992016-05-02 21:41:03 +000087bool HadError = false;
88
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000089static std::string ToolName;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000090
Rafael Espindola49a0e5e2016-02-09 21:32:56 +000091/// If ec is not success, print the error and return true.
Rafael Espindola4453e42942014-06-13 03:07:50 +000092static bool error(std::error_code ec) {
Kevin Enderby10769742014-07-01 22:26:31 +000093 if (!ec)
94 return false;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000095
Kevin Enderby64f7a992016-05-02 21:41:03 +000096 HadError = true;
Davide Italiano911eb312016-01-25 01:24:15 +000097 errs() << ToolName << ": error reading file: " << ec.message() << ".\n";
98 errs().flush();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000099 return true;
100}
101
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000102/// 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 Trick7dc278d2011-09-29 01:22:31 +0000104static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000105 APInt conv(64, num);
106 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000107 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000108 return result.size();
109}
110
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000111/// Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000112static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000113 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 Espindolaa0ff5562016-02-09 21:39:49 +0000124/// Remove unneeded ELF sections from calculation
125static 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 Kulkarni274457e2016-03-28 16:48:10 +0000139/// Total size of all ELF common symbols
140static 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 Espindola49a0e5e2016-02-09 21:32:56 +0000148/// Print the size of each Mach-O segment and section in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000149///
150/// This is when used when @c OutputFormat is darwin and produces the same
151/// output as darwin's size(1) -m output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000152static void printDarwinSectionSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000153 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 Enderby246a4602014-06-17 17:54:13 +0000160 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000161
162 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000163 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000164 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 Enderby10769742014-07-01 22:26:31 +0000169 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
170 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000171 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 Enderby10769742014-07-01 22:26:31 +0000183 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
184 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000185 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 Enderby10769742014-07-01 22:26:31 +0000190 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000191 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000192 uint64_t Seg_vmsize = Seg.vmsize;
Kevin Enderby246a4602014-06-17 17:54:13 +0000193 outs() << "Segment " << Seg.segname << ": "
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000194 << format(fmt.str().c_str(), Seg_vmsize);
Kevin Enderby246a4602014-06-17 17:54:13 +0000195 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000196 outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
Kevin Enderby10769742014-07-01 22:26:31 +0000197 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000198 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 Enderby2058e9d2016-02-09 18:33:15 +0000208 uint64_t Sec_size = Sec.size;
209 outs() << format(fmt.str().c_str(), Sec_size);
Kevin Enderby246a4602014-06-17 17:54:13 +0000210 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000211 outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
Kevin Enderby10769742014-07-01 22:26:31 +0000212 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000213 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 Enderby246a4602014-06-17 17:54:13 +0000219 }
220 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
221}
222
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000223/// Print the summary sizes of the standard Mach-O segments in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000224///
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 Espindola1dc30a42016-02-09 21:35:14 +0000227static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000228 uint64_t total_text = 0;
229 uint64_t total_data = 0;
230 uint64_t total_objc = 0;
231 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000232 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000233 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 Enderby10769742014-07-01 22:26:31 +0000247 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000248 } 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 Enderby10769742014-07-01 22:26:31 +0000259 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000260 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 Enderby10769742014-07-01 22:26:31 +0000273 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000274 } 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 Enderby246a4602014-06-17 17:54:13 +0000286 }
287 uint64_t total = total_text + total_data + total_objc + total_others;
288
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000289 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000290 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000291 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000292 }
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 Espindola49a0e5e2016-02-09 21:32:56 +0000298/// Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000299///
300/// The format used is determined by @c OutputFormat and @c Radix.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000301static void printObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000302 uint64_t total = 0;
303 std::string fmtbuf;
304 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000305 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000306
Kevin Enderby246a4602014-06-17 17:54:13 +0000307 // 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 Espindola1dc30a42016-02-09 21:35:14 +0000312 printDarwinSectionSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000313 // 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 Espindola1dc30a42016-02-09 21:35:14 +0000316 printDarwinSegmentSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000317 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000318 // 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. Spencercc5f8d42011-09-29 00:59:18 +0000321 std::size_t max_size_len = strlen("size");
322 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000323 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000324 if (!considerForSize(Obj, Section))
325 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000326 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000327 total += size;
328
329 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000330 if (error(Section.getName(name)))
331 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000332 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000333 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000334 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
335 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000336 }
337
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000338 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000339 max_name_len += 2;
340 max_size_len += 2;
341 max_addr_len += 2;
342
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000343 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000344 fmt << "%-" << max_name_len << "s "
345 << "%" << max_size_len << "s "
346 << "%" << max_addr_len << "s\n";
347
348 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000349 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. Spencerc4ad4662011-09-28 20:57:46 +0000352 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 Samsonov48803e52014-03-13 14:37:36 +0000360 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000361 if (!considerForSize(Obj, Section))
362 continue;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000363 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000364 if (error(Section.getName(name)))
365 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000366 uint64_t size = Section.getSize();
367 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000368 std::string namestr = name;
369
Alexey Samsonov48803e52014-03-13 14:37:36 +0000370 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000371 }
372
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000373 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. Spencerc4ad4662011-09-28 20:57:46 +0000380 // Print total.
381 fmtbuf.clear();
382 fmt << "%-" << max_name_len << "s "
383 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000384 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000385 total);
386 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000387 // The Berkeley format does not display individual section sizes. It
388 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000389 uint64_t total_text = 0;
390 uint64_t total_data = 0;
391 uint64_t total_bss = 0;
392
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000393 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000394 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000395 uint64_t size = Section.getSize();
396 bool isText = Section.isText();
397 bool isData = Section.isData();
398 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000399 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 Kulkarni274457e2016-03-28 16:48:10 +0000407 if (ELFCommons)
408 total_bss += getCommonSize(Obj);
409
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000410 total = total_text + total_data + total_bss;
411
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000412 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000413 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000414 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000415 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000416 }
417
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000418 // Print result.
419 fmt << "%#7" << radix_fmt << " "
420 << "%#7" << radix_fmt << " "
421 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000422 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000423 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000424 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
425 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000426 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000427 }
428}
429
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000430/// 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 Enderbyafef4c92014-07-01 17:19:10 +0000435static 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 Northover9e8eb412016-04-22 23:21:13 +0000444 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000445 } else {
446 H = MachO->MachOObjectFile::getHeader();
Tim Northover9e8eb412016-04-22 23:21:13 +0000447 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000448 }
449 unsigned i;
Kevin Enderby10769742014-07-01 22:26:31 +0000450 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000451 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 Espindola49a0e5e2016-02-09 21:32:56 +0000464/// Print the section sizes for @p file. If @p file is an archive, print the
465/// section sizes for each archive member.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000466static void printFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000467
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000468 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000469 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
470 if (!BinaryOrErr) {
471 error(errorToErrorCode(BinaryOrErr.takeError()));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000472 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000473 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000474 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000475
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000476 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000477 // This is an archive. Iterate over each member and display its sizes.
Rafael Espindola23a97502014-01-21 16:09:45 +0000478 for (object::Archive::child_iterator i = a->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000479 e = a->child_end();
480 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000481 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000482 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000483 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000484 if (error(ChildOrErr.getError()))
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000485 continue;
Rafael Espindolaae460022014-06-16 16:08:36 +0000486 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000487 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000488 if (!checkMachOAndArchFlags(o, file))
489 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000490 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000491 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
492 else if (MachO && OutputFormat == darwin)
493 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000494 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000495 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. Spencerc4ad4662011-09-28 20:57:46 +0000501 }
502 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000503 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000504 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000505 // 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 Enderby10769742014-07-01 22:26:31 +0000509 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000510 ArchFound = false;
511 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
512 E = UB->end_objects();
513 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000514 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000515 ArchFound = true;
516 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000517 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 Enderby10769742014-07-01 22:26:31 +0000522 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000523 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000524 outs() << o->getFileName() << " (for architecture "
525 << I->getArchTypeName() << "): \n";
526 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000527 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000528 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000529 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000530 outs() << o->getFileName() << " (for architecture "
531 << I->getArchTypeName() << ")";
532 outs() << "\n";
533 }
534 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000535 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
536 I->getAsArchive()) {
537 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000538 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000539 // sizes.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000540 for (object::Archive::child_iterator i = UA->child_begin(),
541 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000542 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000543 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000544 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000545 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000546 if (error(ChildOrErr.getError()))
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000547 continue;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000548 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 Enderby10769742014-07-01 22:26:31 +0000553 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000554 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000555 << ")"
556 << " (for architecture " << I->getArchTypeName()
557 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000558 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000559 if (OutputFormat == berkeley) {
560 if (MachO) {
561 outs() << UA->getFileName() << "(" << o->getFileName()
562 << ")";
563 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000564 outs() << " (for architecture " << I->getArchTypeName()
565 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000566 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000567 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000568 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 Enderby10769742014-07-01 22:26:31 +0000587 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000588 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
589 E = UB->end_objects();
590 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000591 if (HostArchName == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000592 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000593 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 Enderby10769742014-07-01 22:26:31 +0000598 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000599 if (MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000600 outs() << o->getFileName() << " (for architecture "
601 << I->getArchTypeName() << "):\n";
602 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000603 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000604 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000605 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000606 outs() << o->getFileName() << " (for architecture "
607 << I->getArchTypeName() << ")";
608 outs() << "\n";
609 }
610 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000611 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
612 I->getAsArchive()) {
613 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000614 // 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 Enderby10769742014-07-01 22:26:31 +0000618 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000619 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000620 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000621 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000622 if (error(ChildOrErr.getError()))
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000623 continue;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000624 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 Enderby10769742014-07-01 22:26:31 +0000629 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000630 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
631 << " (for architecture " << I->getArchTypeName()
632 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000633 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000634 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 Espindola1dc30a42016-02-09 21:35:14 +0000651 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000652 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
653 E = UB->end_objects();
654 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000655 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000656 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000657 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000658 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000659 if (OutputFormat == sysv)
660 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000661 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000662 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000663 outs() << o->getFileName() << " (for architecture "
664 << I->getArchTypeName() << "):";
665 outs() << "\n";
666 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000667 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000668 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000669 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000670 outs() << o->getFileName() << " (for architecture "
671 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000672 outs() << "\n";
673 }
674 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000675 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
676 I->getAsArchive()) {
677 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000678 // This is an archive. Iterate over each member and display its sizes.
679 for (object::Archive::child_iterator i = UA->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000680 e = UA->child_end();
681 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000682 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000683 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000684 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000685 if (error(ChildOrErr.getError()))
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000686 continue;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000687 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 Enderby10769742014-07-01 22:26:31 +0000692 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000693 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000694 << " (for architecture " << I->getArchTypeName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000695 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000696 if (OutputFormat == berkeley) {
697 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000698 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
699 << " (for architecture " << I->getArchTypeName()
700 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000701 else
702 outs() << o->getFileName() << " (ex " << UA->getFileName()
703 << ")\n";
704 }
705 }
706 }
707 }
708 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000709 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000710 if (!checkMachOAndArchFlags(o, file))
711 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000712 if (OutputFormat == sysv)
713 outs() << o->getFileName() << " :\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000714 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000715 if (OutputFormat == berkeley) {
716 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000717 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000718 outs() << o->getFileName();
719 outs() << "\n";
720 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000721 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000722 errs() << ToolName << ": " << file << ": "
723 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000724 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000725 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000726 if (OutputFormat == sysv)
727 outs() << "\n";
728}
729
730int main(int argc, char **argv) {
731 // Print a stack trace if we signal out.
732 sys::PrintStackTraceOnErrorSignal();
733 PrettyStackTraceProgram X(argc, argv);
734
Kevin Enderby10769742014-07-01 22:26:31 +0000735 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000736 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
737
738 ToolName = argv[0];
739 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000740 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000741 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000742 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000743
Kevin Enderby10769742014-07-01 22:26:31 +0000744 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000745 if (ArchFlags[i] == "all") {
746 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000747 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000748 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000749 outs() << ToolName << ": for the -arch option: Unknown architecture "
750 << "named '" << ArchFlags[i] << "'";
751 return 1;
752 }
753 }
754 }
755
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000756 if (InputFilenames.size() == 0)
757 InputFilenames.push_back("a.out");
758
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000759 MoreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000760 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000761 printFileSectionSizes);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000762
Kevin Enderby64f7a992016-05-02 21:41:03 +0000763 if (HadError)
764 return 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000765}