blob: 83e75d3039ea5d049410c34e3fcd4e8c456a22b0 [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.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000466 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
467 if (!BinaryOrErr) {
468 error(errorToErrorCode(BinaryOrErr.takeError()));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000469 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000470 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000471 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000472
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000473 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000474 // This is an archive. Iterate over each member and display its sizes.
Rafael Espindola23a97502014-01-21 16:09:45 +0000475 for (object::Archive::child_iterator i = a->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000476 e = a->child_end();
477 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000478 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000479 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000480 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000481 if (error(ChildOrErr.getError()))
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000482 continue;
Rafael Espindolaae460022014-06-16 16:08:36 +0000483 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000484 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000485 if (!checkMachOAndArchFlags(o, file))
486 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000487 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000488 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
489 else if (MachO && OutputFormat == darwin)
490 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000491 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000492 if (OutputFormat == berkeley) {
493 if (MachO)
494 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
495 else
496 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
497 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000498 }
499 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000500 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000501 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000502 // If we have a list of architecture flags specified dump only those.
503 if (!ArchAll && ArchFlags.size() != 0) {
504 // Look for a slice in the universal binary that matches each ArchFlag.
505 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000506 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000507 ArchFound = false;
508 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
509 E = UB->end_objects();
510 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000511 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000512 ArchFound = true;
513 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000514 if (UO) {
515 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
516 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
517 if (OutputFormat == sysv)
518 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000519 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000520 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000521 outs() << o->getFileName() << " (for architecture "
522 << I->getArchTypeName() << "): \n";
523 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000524 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000525 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000526 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000527 outs() << o->getFileName() << " (for architecture "
528 << I->getArchTypeName() << ")";
529 outs() << "\n";
530 }
531 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000532 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
533 I->getAsArchive()) {
534 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000535 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000536 // sizes.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000537 for (object::Archive::child_iterator i = UA->child_begin(),
538 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000539 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000540 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000541 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000542 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000543 if (error(ChildOrErr.getError()))
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000544 continue;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000545 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
546 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
547 if (OutputFormat == sysv)
548 outs() << o->getFileName() << " (ex " << UA->getFileName()
549 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000550 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000551 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000552 << ")"
553 << " (for architecture " << I->getArchTypeName()
554 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000555 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000556 if (OutputFormat == berkeley) {
557 if (MachO) {
558 outs() << UA->getFileName() << "(" << o->getFileName()
559 << ")";
560 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000561 outs() << " (for architecture " << I->getArchTypeName()
562 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000563 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000564 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000565 outs() << o->getFileName() << " (ex " << UA->getFileName()
566 << ")\n";
567 }
568 }
569 }
570 }
571 }
572 }
573 if (!ArchFound) {
574 errs() << ToolName << ": file: " << file
575 << " does not contain architecture" << ArchFlags[i] << ".\n";
576 return;
577 }
578 }
579 return;
580 }
581 // No architecture flags were specified so if this contains a slice that
582 // matches the host architecture dump only that.
583 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000584 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000585 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
586 E = UB->end_objects();
587 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000588 if (HostArchName == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000589 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000590 if (UO) {
591 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
592 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
593 if (OutputFormat == sysv)
594 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000595 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000596 if (MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000597 outs() << o->getFileName() << " (for architecture "
598 << I->getArchTypeName() << "):\n";
599 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000600 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000601 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000602 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000603 outs() << o->getFileName() << " (for architecture "
604 << I->getArchTypeName() << ")";
605 outs() << "\n";
606 }
607 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000608 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
609 I->getAsArchive()) {
610 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000611 // This is an archive. Iterate over each member and display its
612 // sizes.
613 for (object::Archive::child_iterator i = UA->child_begin(),
614 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000615 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000616 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000617 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000618 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000619 if (error(ChildOrErr.getError()))
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000620 continue;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000621 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
622 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
623 if (OutputFormat == sysv)
624 outs() << o->getFileName() << " (ex " << UA->getFileName()
625 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000626 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000627 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
628 << " (for architecture " << I->getArchTypeName()
629 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000630 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000631 if (OutputFormat == berkeley) {
632 if (MachO)
633 outs() << UA->getFileName() << "(" << o->getFileName()
634 << ")\n";
635 else
636 outs() << o->getFileName() << " (ex " << UA->getFileName()
637 << ")\n";
638 }
639 }
640 }
641 }
642 return;
643 }
644 }
645 }
646 // Either all architectures have been specified or none have been specified
647 // and this does not contain the host architecture so dump all the slices.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000648 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000649 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
650 E = UB->end_objects();
651 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000652 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000653 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000654 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000655 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000656 if (OutputFormat == sysv)
657 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000658 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000659 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000660 outs() << o->getFileName() << " (for architecture "
661 << I->getArchTypeName() << "):";
662 outs() << "\n";
663 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000664 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000665 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000666 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000667 outs() << o->getFileName() << " (for architecture "
668 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000669 outs() << "\n";
670 }
671 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000672 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
673 I->getAsArchive()) {
674 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000675 // This is an archive. Iterate over each member and display its sizes.
676 for (object::Archive::child_iterator i = UA->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000677 e = UA->child_end();
678 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000679 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000680 exit(1);
Davide Italianoef34c992016-02-13 01:52:47 +0000681 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
Davide Italianoff11b902016-02-13 01:38:16 +0000682 if (error(ChildOrErr.getError()))
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000683 continue;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000684 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
685 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
686 if (OutputFormat == sysv)
687 outs() << o->getFileName() << " (ex " << UA->getFileName()
688 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000689 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000690 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000691 << " (for architecture " << I->getArchTypeName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000692 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000693 if (OutputFormat == berkeley) {
694 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000695 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
696 << " (for architecture " << I->getArchTypeName()
697 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000698 else
699 outs() << o->getFileName() << " (ex " << UA->getFileName()
700 << ")\n";
701 }
702 }
703 }
704 }
705 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000706 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000707 if (!checkMachOAndArchFlags(o, file))
708 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000709 if (OutputFormat == sysv)
710 outs() << o->getFileName() << " :\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000711 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000712 if (OutputFormat == berkeley) {
713 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000714 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000715 outs() << o->getFileName();
716 outs() << "\n";
717 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000718 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000719 errs() << ToolName << ": " << file << ": "
720 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000721 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000722 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000723 if (OutputFormat == sysv)
724 outs() << "\n";
725}
726
727int main(int argc, char **argv) {
728 // Print a stack trace if we signal out.
729 sys::PrintStackTraceOnErrorSignal();
730 PrettyStackTraceProgram X(argc, argv);
731
Kevin Enderby10769742014-07-01 22:26:31 +0000732 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000733 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
734
735 ToolName = argv[0];
736 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000737 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000738 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000739 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000740
Kevin Enderby10769742014-07-01 22:26:31 +0000741 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000742 if (ArchFlags[i] == "all") {
743 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000744 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000745 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000746 outs() << ToolName << ": for the -arch option: Unknown architecture "
747 << "named '" << ArchFlags[i] << "'";
748 return 1;
749 }
750 }
751 }
752
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000753 if (InputFilenames.size() == 0)
754 InputFilenames.push_back("a.out");
755
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000756 MoreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000757 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000758 printFileSectionSizes);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000759
760 return 0;
761}