blob: f386a244885baa96eacce7be660fc108c2fd5248 [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
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000087static std::string ToolName;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000088
Rafael Espindola49a0e5e2016-02-09 21:32:56 +000089/// If ec is not success, print the error and return true.
Rafael Espindola4453e42942014-06-13 03:07:50 +000090static bool error(std::error_code ec) {
Kevin Enderby10769742014-07-01 22:26:31 +000091 if (!ec)
92 return false;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000093
Davide Italiano911eb312016-01-25 01:24:15 +000094 errs() << ToolName << ": error reading file: " << ec.message() << ".\n";
95 errs().flush();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000096 return true;
97}
98
Rafael Espindola49a0e5e2016-02-09 21:32:56 +000099/// Get the length of the string that represents @p num in Radix including the
100/// leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +0000101static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000102 APInt conv(64, num);
103 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000104 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000105 return result.size();
106}
107
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000108/// Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000109static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000110 switch (Radix) {
111 case octal:
112 return PRIo64;
113 case decimal:
114 return PRIu64;
115 case hexadecimal:
116 return PRIx64;
117 }
118 return nullptr;
119}
120
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000121/// Remove unneeded ELF sections from calculation
122static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
123 if (!Obj->isELF())
124 return true;
125 switch (static_cast<ELFSectionRef>(Section).getType()) {
126 case ELF::SHT_NULL:
127 case ELF::SHT_SYMTAB:
128 case ELF::SHT_STRTAB:
129 case ELF::SHT_REL:
130 case ELF::SHT_RELA:
131 return false;
132 }
133 return true;
134}
135
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000136/// Total size of all ELF common symbols
137static uint64_t getCommonSize(ObjectFile *Obj) {
138 uint64_t TotalCommons = 0;
139 for (auto &Sym : Obj->symbols())
140 if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
141 TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
142 return TotalCommons;
143}
144
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000145/// Print the size of each Mach-O segment and section in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000146///
147/// This is when used when @c OutputFormat is darwin and produces the same
148/// output as darwin's size(1) -m output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000149static void printDarwinSectionSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000150 std::string fmtbuf;
151 raw_string_ostream fmt(fmtbuf);
152 const char *radix_fmt = getRadixFmt();
153 if (Radix == hexadecimal)
154 fmt << "0x";
155 fmt << "%" << radix_fmt;
156
Kevin Enderby246a4602014-06-17 17:54:13 +0000157 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000158
159 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000160 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000161 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
162 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
163 outs() << "Segment " << Seg.segname << ": "
164 << format(fmt.str().c_str(), Seg.vmsize);
165 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000166 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
167 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000168 outs() << "\n";
169 total += Seg.vmsize;
170 uint64_t sec_total = 0;
171 for (unsigned J = 0; J < Seg.nsects; ++J) {
172 MachO::section_64 Sec = MachO->getSection64(Load, J);
173 if (Filetype == MachO::MH_OBJECT)
174 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
175 << format("%.16s", &Sec.sectname) << "): ";
176 else
177 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
178 outs() << format(fmt.str().c_str(), Sec.size);
179 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000180 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
181 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000182 outs() << "\n";
183 sec_total += Sec.size;
184 }
185 if (Seg.nsects != 0)
186 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000187 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000188 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000189 uint64_t Seg_vmsize = Seg.vmsize;
Kevin Enderby246a4602014-06-17 17:54:13 +0000190 outs() << "Segment " << Seg.segname << ": "
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000191 << format(fmt.str().c_str(), Seg_vmsize);
Kevin Enderby246a4602014-06-17 17:54:13 +0000192 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000193 outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
Kevin Enderby10769742014-07-01 22:26:31 +0000194 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000195 outs() << "\n";
196 total += Seg.vmsize;
197 uint64_t sec_total = 0;
198 for (unsigned J = 0; J < Seg.nsects; ++J) {
199 MachO::section Sec = MachO->getSection(Load, J);
200 if (Filetype == MachO::MH_OBJECT)
201 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
202 << format("%.16s", &Sec.sectname) << "): ";
203 else
204 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000205 uint64_t Sec_size = Sec.size;
206 outs() << format(fmt.str().c_str(), Sec_size);
Kevin Enderby246a4602014-06-17 17:54:13 +0000207 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000208 outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
Kevin Enderby10769742014-07-01 22:26:31 +0000209 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000210 outs() << "\n";
211 sec_total += Sec.size;
212 }
213 if (Seg.nsects != 0)
214 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
215 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000216 }
217 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
218}
219
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000220/// Print the summary sizes of the standard Mach-O segments in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000221///
222/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
223/// produces the same output as darwin's size(1) default output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000224static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000225 uint64_t total_text = 0;
226 uint64_t total_data = 0;
227 uint64_t total_objc = 0;
228 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000229 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000230 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
231 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
232 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
233 for (unsigned J = 0; J < Seg.nsects; ++J) {
234 MachO::section_64 Sec = MachO->getSection64(Load, J);
235 StringRef SegmentName = StringRef(Sec.segname);
236 if (SegmentName == "__TEXT")
237 total_text += Sec.size;
238 else if (SegmentName == "__DATA")
239 total_data += Sec.size;
240 else if (SegmentName == "__OBJC")
241 total_objc += Sec.size;
242 else
243 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000244 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000245 } else {
246 StringRef SegmentName = StringRef(Seg.segname);
247 if (SegmentName == "__TEXT")
248 total_text += Seg.vmsize;
249 else if (SegmentName == "__DATA")
250 total_data += Seg.vmsize;
251 else if (SegmentName == "__OBJC")
252 total_objc += Seg.vmsize;
253 else
254 total_others += Seg.vmsize;
255 }
Kevin Enderby10769742014-07-01 22:26:31 +0000256 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000257 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
258 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
259 for (unsigned J = 0; J < Seg.nsects; ++J) {
260 MachO::section Sec = MachO->getSection(Load, J);
261 StringRef SegmentName = StringRef(Sec.segname);
262 if (SegmentName == "__TEXT")
263 total_text += Sec.size;
264 else if (SegmentName == "__DATA")
265 total_data += Sec.size;
266 else if (SegmentName == "__OBJC")
267 total_objc += Sec.size;
268 else
269 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000270 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000271 } else {
272 StringRef SegmentName = StringRef(Seg.segname);
273 if (SegmentName == "__TEXT")
274 total_text += Seg.vmsize;
275 else if (SegmentName == "__DATA")
276 total_data += Seg.vmsize;
277 else if (SegmentName == "__OBJC")
278 total_objc += Seg.vmsize;
279 else
280 total_others += Seg.vmsize;
281 }
282 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000283 }
284 uint64_t total = total_text + total_data + total_objc + total_others;
285
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000286 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000287 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000288 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000289 }
290 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
291 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
292 << "\t";
293}
294
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000295/// Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000296///
297/// The format used is determined by @c OutputFormat and @c Radix.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000298static void printObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000299 uint64_t total = 0;
300 std::string fmtbuf;
301 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000302 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000303
Kevin Enderby246a4602014-06-17 17:54:13 +0000304 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
305 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
306 // let it fall through to OutputFormat berkeley.
307 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
308 if (OutputFormat == darwin && MachO)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000309 printDarwinSectionSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000310 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
311 // darwin's default berkeley format for Mach-O files.
312 else if (MachO && OutputFormat == berkeley)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000313 printDarwinSegmentSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000314 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000315 // Run two passes over all sections. The first gets the lengths needed for
316 // formatting the output. The second actually does the output.
317 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000318 std::size_t max_size_len = strlen("size");
319 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000320 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000321 if (!considerForSize(Obj, Section))
322 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000323 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000324 total += size;
325
326 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000327 if (error(Section.getName(name)))
328 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000329 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000330 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000331 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
332 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000333 }
334
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000335 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000336 max_name_len += 2;
337 max_size_len += 2;
338 max_addr_len += 2;
339
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000340 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000341 fmt << "%-" << max_name_len << "s "
342 << "%" << max_size_len << "s "
343 << "%" << max_addr_len << "s\n";
344
345 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000346 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
347 static_cast<const char *>("size"),
348 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000349 fmtbuf.clear();
350
351 // Setup per section format.
352 fmt << "%-" << max_name_len << "s "
353 << "%#" << max_size_len << radix_fmt << " "
354 << "%#" << max_addr_len << radix_fmt << "\n";
355
356 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000357 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000358 if (!considerForSize(Obj, Section))
359 continue;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000360 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000361 if (error(Section.getName(name)))
362 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000363 uint64_t size = Section.getSize();
364 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000365 std::string namestr = name;
366
Alexey Samsonov48803e52014-03-13 14:37:36 +0000367 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000368 }
369
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000370 if (ELFCommons) {
371 uint64_t CommonSize = getCommonSize(Obj);
372 total += CommonSize;
373 outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
374 CommonSize, static_cast<uint64_t>(0));
375 }
376
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000377 // Print total.
378 fmtbuf.clear();
379 fmt << "%-" << max_name_len << "s "
380 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000381 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000382 total);
383 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000384 // The Berkeley format does not display individual section sizes. It
385 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000386 uint64_t total_text = 0;
387 uint64_t total_data = 0;
388 uint64_t total_bss = 0;
389
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000390 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000391 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000392 uint64_t size = Section.getSize();
393 bool isText = Section.isText();
394 bool isData = Section.isData();
395 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000396 if (isText)
397 total_text += size;
398 else if (isData)
399 total_data += size;
400 else if (isBSS)
401 total_bss += size;
402 }
403
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000404 if (ELFCommons)
405 total_bss += getCommonSize(Obj);
406
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000407 total = total_text + total_data + total_bss;
408
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000409 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000410 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000411 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000412 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000413 }
414
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000415 // Print result.
416 fmt << "%#7" << radix_fmt << " "
417 << "%#7" << radix_fmt << " "
418 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000419 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000420 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000421 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
422 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000423 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000424 }
425}
426
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000427/// Checks to see if the @p o ObjectFile is a Mach-O file and if it is and there
428/// is a list of architecture flags specified then check to make sure this
429/// Mach-O file is one of those architectures or all architectures was
430/// specificed. If not then an error is generated and this routine returns
431/// false. Else it returns true.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000432static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
433 if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) {
434 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
435 bool ArchFound = false;
436 MachO::mach_header H;
437 MachO::mach_header_64 H_64;
438 Triple T;
439 if (MachO->is64Bit()) {
440 H_64 = MachO->MachOObjectFile::getHeader64();
441 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
442 } else {
443 H = MachO->MachOObjectFile::getHeader();
444 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
445 }
446 unsigned i;
Kevin Enderby10769742014-07-01 22:26:31 +0000447 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000448 if (ArchFlags[i] == T.getArchName())
449 ArchFound = true;
450 break;
451 }
452 if (!ArchFound) {
453 errs() << ToolName << ": file: " << file
454 << " does not contain architecture: " << ArchFlags[i] << ".\n";
455 return false;
456 }
457 }
458 return true;
459}
460
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000461/// Print the section sizes for @p file. If @p file is an archive, print the
462/// section sizes for each archive member.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000463static void printFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000464
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000465 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000466 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Davide Italianoff11b902016-02-13 01:38:16 +0000467 if (error(BinaryOrErr.getError()))
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000468 return;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000469 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000470
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000471 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000472 // This is an archive. Iterate over each member and display its sizes.
Rafael Espindola23a97502014-01-21 16:09:45 +0000473 for (object::Archive::child_iterator i = a->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000474 e = a->child_end();
475 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000476 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000477 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000478 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000479 if (error(ChildOrErr.getError()))
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000480 continue;
Rafael Espindolaae460022014-06-16 16:08:36 +0000481 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000482 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000483 if (!checkMachOAndArchFlags(o, file))
484 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000485 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000486 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
487 else if (MachO && OutputFormat == darwin)
488 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000489 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000490 if (OutputFormat == berkeley) {
491 if (MachO)
492 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
493 else
494 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
495 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000496 }
497 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000498 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000499 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000500 // If we have a list of architecture flags specified dump only those.
501 if (!ArchAll && ArchFlags.size() != 0) {
502 // Look for a slice in the universal binary that matches each ArchFlag.
503 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000504 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000505 ArchFound = false;
506 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
507 E = UB->end_objects();
508 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000509 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000510 ArchFound = true;
511 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000512 if (UO) {
513 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
514 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
515 if (OutputFormat == sysv)
516 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000517 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000518 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000519 outs() << o->getFileName() << " (for architecture "
520 << I->getArchTypeName() << "): \n";
521 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000522 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000523 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000524 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000525 outs() << o->getFileName() << " (for architecture "
526 << I->getArchTypeName() << ")";
527 outs() << "\n";
528 }
529 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000530 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
531 I->getAsArchive()) {
532 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000533 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000534 // sizes.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000535 for (object::Archive::child_iterator i = UA->child_begin(),
536 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000537 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000538 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000539 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000540 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000541 if (error(ChildOrErr.getError()))
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000542 continue;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000543 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
544 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
545 if (OutputFormat == sysv)
546 outs() << o->getFileName() << " (ex " << UA->getFileName()
547 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000548 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000549 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000550 << ")"
551 << " (for architecture " << I->getArchTypeName()
552 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000553 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000554 if (OutputFormat == berkeley) {
555 if (MachO) {
556 outs() << UA->getFileName() << "(" << o->getFileName()
557 << ")";
558 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000559 outs() << " (for architecture " << I->getArchTypeName()
560 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000561 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000562 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000563 outs() << o->getFileName() << " (ex " << UA->getFileName()
564 << ")\n";
565 }
566 }
567 }
568 }
569 }
570 }
571 if (!ArchFound) {
572 errs() << ToolName << ": file: " << file
573 << " does not contain architecture" << ArchFlags[i] << ".\n";
574 return;
575 }
576 }
577 return;
578 }
579 // No architecture flags were specified so if this contains a slice that
580 // matches the host architecture dump only that.
581 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000582 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000583 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
584 E = UB->end_objects();
585 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000586 if (HostArchName == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000587 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000588 if (UO) {
589 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
590 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
591 if (OutputFormat == sysv)
592 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000593 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000594 if (MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000595 outs() << o->getFileName() << " (for architecture "
596 << I->getArchTypeName() << "):\n";
597 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000598 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000599 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000600 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000601 outs() << o->getFileName() << " (for architecture "
602 << I->getArchTypeName() << ")";
603 outs() << "\n";
604 }
605 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000606 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
607 I->getAsArchive()) {
608 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000609 // This is an archive. Iterate over each member and display its
610 // sizes.
611 for (object::Archive::child_iterator i = UA->child_begin(),
612 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000613 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000614 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000615 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000616 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000617 if (error(ChildOrErr.getError()))
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000618 continue;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000619 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
620 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
621 if (OutputFormat == sysv)
622 outs() << o->getFileName() << " (ex " << UA->getFileName()
623 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000624 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000625 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
626 << " (for architecture " << I->getArchTypeName()
627 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000628 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000629 if (OutputFormat == berkeley) {
630 if (MachO)
631 outs() << UA->getFileName() << "(" << o->getFileName()
632 << ")\n";
633 else
634 outs() << o->getFileName() << " (ex " << UA->getFileName()
635 << ")\n";
636 }
637 }
638 }
639 }
640 return;
641 }
642 }
643 }
644 // Either all architectures have been specified or none have been specified
645 // and this does not contain the host architecture so dump all the slices.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000646 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000647 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
648 E = UB->end_objects();
649 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000650 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000651 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000652 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000653 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000654 if (OutputFormat == sysv)
655 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000656 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000657 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000658 outs() << o->getFileName() << " (for architecture "
659 << I->getArchTypeName() << "):";
660 outs() << "\n";
661 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000662 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000663 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000664 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000665 outs() << o->getFileName() << " (for architecture "
666 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000667 outs() << "\n";
668 }
669 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000670 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
671 I->getAsArchive()) {
672 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000673 // This is an archive. Iterate over each member and display its sizes.
674 for (object::Archive::child_iterator i = UA->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000675 e = UA->child_end();
676 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000677 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000678 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000679 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000680 if (error(ChildOrErr.getError()))
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000681 continue;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000682 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
683 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
684 if (OutputFormat == sysv)
685 outs() << o->getFileName() << " (ex " << UA->getFileName()
686 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000687 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000688 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000689 << " (for architecture " << I->getArchTypeName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000690 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000691 if (OutputFormat == berkeley) {
692 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000693 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
694 << " (for architecture " << I->getArchTypeName()
695 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000696 else
697 outs() << o->getFileName() << " (ex " << UA->getFileName()
698 << ")\n";
699 }
700 }
701 }
702 }
703 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000704 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000705 if (!checkMachOAndArchFlags(o, file))
706 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000707 if (OutputFormat == sysv)
708 outs() << o->getFileName() << " :\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000709 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000710 if (OutputFormat == berkeley) {
711 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000712 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000713 outs() << o->getFileName();
714 outs() << "\n";
715 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000716 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000717 errs() << ToolName << ": " << file << ": "
718 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000719 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000720 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000721 if (OutputFormat == sysv)
722 outs() << "\n";
723}
724
725int main(int argc, char **argv) {
726 // Print a stack trace if we signal out.
727 sys::PrintStackTraceOnErrorSignal();
728 PrettyStackTraceProgram X(argc, argv);
729
Kevin Enderby10769742014-07-01 22:26:31 +0000730 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000731 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
732
733 ToolName = argv[0];
734 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000735 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000736 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000737 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000738
Kevin Enderby10769742014-07-01 22:26:31 +0000739 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000740 if (ArchFlags[i] == "all") {
741 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000742 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000743 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000744 outs() << ToolName << ": for the -arch option: Unknown architecture "
745 << "named '" << ArchFlags[i] << "'";
746 return 1;
747 }
748 }
749 }
750
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000751 if (InputFilenames.size() == 0)
752 InputFilenames.push_back("a.out");
753
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000754 MoreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000755 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000756 printFileSectionSizes);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000757
758 return 0;
759}