blob: 8da0ec677cb07ee680afeb103ad005a32a11ff16 [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
Kevin Enderby42398052016-06-28 23:16:13 +0000102static bool error(Twine Message) {
103 HadError = true;
104 errs() << ToolName << ": " << Message << ".\n";
105 errs().flush();
106 return true;
107}
108
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000109// This version of error() prints the archive name and member name, for example:
110// "libx.a(foo.o)" after the ToolName before the error message. It sets
111// HadError but returns allowing the code to move on to other archive members.
Kevin Enderby9acb1092016-05-31 20:35:34 +0000112static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
113 StringRef ArchitectureName = StringRef()) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000114 HadError = true;
115 errs() << ToolName << ": " << FileName;
116
Kevin Enderbyf4586032016-07-29 17:44:13 +0000117 Expected<StringRef> NameOrErr = C.getName();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000118 // TODO: if we have a error getting the name then it would be nice to print
119 // the index of which archive member this is and or its offset in the
120 // archive instead of "???" as the name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000121 if (!NameOrErr) {
122 consumeError(NameOrErr.takeError());
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000123 errs() << "(" << "???" << ")";
Kevin Enderbyf4586032016-07-29 17:44:13 +0000124 } else
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000125 errs() << "(" << NameOrErr.get() << ")";
126
Kevin Enderby9acb1092016-05-31 20:35:34 +0000127 if (!ArchitectureName.empty())
128 errs() << " (for architecture " << ArchitectureName << ") ";
129
130 std::string Buf;
131 raw_string_ostream OS(Buf);
132 logAllUnhandledErrors(std::move(E), OS, "");
133 OS.flush();
134 errs() << " " << Buf << "\n";
135}
136
137// This version of error() prints the file name and which architecture slice it // is from, for example: "foo.o (for architecture i386)" after the ToolName
138// before the error message. It sets HadError but returns allowing the code to
139// move on to other architecture slices.
140static void error(llvm::Error E, StringRef FileName,
141 StringRef ArchitectureName = StringRef()) {
142 HadError = true;
143 errs() << ToolName << ": " << FileName;
144
145 if (!ArchitectureName.empty())
146 errs() << " (for architecture " << ArchitectureName << ") ";
147
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000148 std::string Buf;
149 raw_string_ostream OS(Buf);
150 logAllUnhandledErrors(std::move(E), OS, "");
151 OS.flush();
152 errs() << " " << Buf << "\n";
153}
154
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000155/// Get the length of the string that represents @p num in Radix including the
156/// leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +0000157static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000158 APInt conv(64, num);
159 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000160 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000161 return result.size();
162}
163
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000164/// Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000165static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000166 switch (Radix) {
167 case octal:
168 return PRIo64;
169 case decimal:
170 return PRIu64;
171 case hexadecimal:
172 return PRIx64;
173 }
174 return nullptr;
175}
176
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000177/// Remove unneeded ELF sections from calculation
178static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
179 if (!Obj->isELF())
180 return true;
181 switch (static_cast<ELFSectionRef>(Section).getType()) {
182 case ELF::SHT_NULL:
183 case ELF::SHT_SYMTAB:
184 case ELF::SHT_STRTAB:
185 case ELF::SHT_REL:
186 case ELF::SHT_RELA:
187 return false;
188 }
189 return true;
190}
191
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000192/// Total size of all ELF common symbols
193static uint64_t getCommonSize(ObjectFile *Obj) {
194 uint64_t TotalCommons = 0;
195 for (auto &Sym : Obj->symbols())
196 if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
197 TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
198 return TotalCommons;
199}
200
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000201/// Print the size of each Mach-O segment and section in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000202///
203/// This is when used when @c OutputFormat is darwin and produces the same
204/// output as darwin's size(1) -m output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000205static void printDarwinSectionSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000206 std::string fmtbuf;
207 raw_string_ostream fmt(fmtbuf);
208 const char *radix_fmt = getRadixFmt();
209 if (Radix == hexadecimal)
210 fmt << "0x";
211 fmt << "%" << radix_fmt;
212
Kevin Enderby246a4602014-06-17 17:54:13 +0000213 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000214
215 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000216 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000217 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
218 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
219 outs() << "Segment " << Seg.segname << ": "
220 << format(fmt.str().c_str(), Seg.vmsize);
221 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000222 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
223 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000224 outs() << "\n";
225 total += Seg.vmsize;
226 uint64_t sec_total = 0;
227 for (unsigned J = 0; J < Seg.nsects; ++J) {
228 MachO::section_64 Sec = MachO->getSection64(Load, J);
229 if (Filetype == MachO::MH_OBJECT)
230 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
231 << format("%.16s", &Sec.sectname) << "): ";
232 else
233 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
234 outs() << format(fmt.str().c_str(), Sec.size);
235 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000236 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
237 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000238 outs() << "\n";
239 sec_total += Sec.size;
240 }
241 if (Seg.nsects != 0)
242 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000243 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000244 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000245 uint64_t Seg_vmsize = Seg.vmsize;
Kevin Enderby246a4602014-06-17 17:54:13 +0000246 outs() << "Segment " << Seg.segname << ": "
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000247 << format(fmt.str().c_str(), Seg_vmsize);
Kevin Enderby246a4602014-06-17 17:54:13 +0000248 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000249 outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
Kevin Enderby10769742014-07-01 22:26:31 +0000250 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000251 outs() << "\n";
252 total += Seg.vmsize;
253 uint64_t sec_total = 0;
254 for (unsigned J = 0; J < Seg.nsects; ++J) {
255 MachO::section Sec = MachO->getSection(Load, J);
256 if (Filetype == MachO::MH_OBJECT)
257 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
258 << format("%.16s", &Sec.sectname) << "): ";
259 else
260 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000261 uint64_t Sec_size = Sec.size;
262 outs() << format(fmt.str().c_str(), Sec_size);
Kevin Enderby246a4602014-06-17 17:54:13 +0000263 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000264 outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
Kevin Enderby10769742014-07-01 22:26:31 +0000265 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000266 outs() << "\n";
267 sec_total += Sec.size;
268 }
269 if (Seg.nsects != 0)
270 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
271 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000272 }
273 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
274}
275
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000276/// Print the summary sizes of the standard Mach-O segments in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000277///
278/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
279/// produces the same output as darwin's size(1) default output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000280static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000281 uint64_t total_text = 0;
282 uint64_t total_data = 0;
283 uint64_t total_objc = 0;
284 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000285 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000286 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
287 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
288 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
289 for (unsigned J = 0; J < Seg.nsects; ++J) {
290 MachO::section_64 Sec = MachO->getSection64(Load, J);
291 StringRef SegmentName = StringRef(Sec.segname);
292 if (SegmentName == "__TEXT")
293 total_text += Sec.size;
294 else if (SegmentName == "__DATA")
295 total_data += Sec.size;
296 else if (SegmentName == "__OBJC")
297 total_objc += Sec.size;
298 else
299 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000300 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000301 } else {
302 StringRef SegmentName = StringRef(Seg.segname);
303 if (SegmentName == "__TEXT")
304 total_text += Seg.vmsize;
305 else if (SegmentName == "__DATA")
306 total_data += Seg.vmsize;
307 else if (SegmentName == "__OBJC")
308 total_objc += Seg.vmsize;
309 else
310 total_others += Seg.vmsize;
311 }
Kevin Enderby10769742014-07-01 22:26:31 +0000312 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000313 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
314 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
315 for (unsigned J = 0; J < Seg.nsects; ++J) {
316 MachO::section Sec = MachO->getSection(Load, J);
317 StringRef SegmentName = StringRef(Sec.segname);
318 if (SegmentName == "__TEXT")
319 total_text += Sec.size;
320 else if (SegmentName == "__DATA")
321 total_data += Sec.size;
322 else if (SegmentName == "__OBJC")
323 total_objc += Sec.size;
324 else
325 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000326 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000327 } else {
328 StringRef SegmentName = StringRef(Seg.segname);
329 if (SegmentName == "__TEXT")
330 total_text += Seg.vmsize;
331 else if (SegmentName == "__DATA")
332 total_data += Seg.vmsize;
333 else if (SegmentName == "__OBJC")
334 total_objc += Seg.vmsize;
335 else
336 total_others += Seg.vmsize;
337 }
338 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000339 }
340 uint64_t total = total_text + total_data + total_objc + total_others;
341
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000342 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000343 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000344 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000345 }
346 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
347 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
348 << "\t";
349}
350
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000351/// Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000352///
353/// The format used is determined by @c OutputFormat and @c Radix.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000354static void printObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000355 uint64_t total = 0;
356 std::string fmtbuf;
357 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000358 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000359
Kevin Enderby246a4602014-06-17 17:54:13 +0000360 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
361 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
362 // let it fall through to OutputFormat berkeley.
363 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
364 if (OutputFormat == darwin && MachO)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000365 printDarwinSectionSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000366 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
367 // darwin's default berkeley format for Mach-O files.
368 else if (MachO && OutputFormat == berkeley)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000369 printDarwinSegmentSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000370 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000371 // Run two passes over all sections. The first gets the lengths needed for
372 // formatting the output. The second actually does the output.
373 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000374 std::size_t max_size_len = strlen("size");
375 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000376 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000377 if (!considerForSize(Obj, Section))
378 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000379 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000380 total += size;
381
382 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000383 if (error(Section.getName(name)))
384 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000385 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000386 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000387 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
388 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000389 }
390
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000391 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000392 max_name_len += 2;
393 max_size_len += 2;
394 max_addr_len += 2;
395
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000396 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000397 fmt << "%-" << max_name_len << "s "
398 << "%" << max_size_len << "s "
399 << "%" << max_addr_len << "s\n";
400
401 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000402 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
403 static_cast<const char *>("size"),
404 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000405 fmtbuf.clear();
406
407 // Setup per section format.
408 fmt << "%-" << max_name_len << "s "
409 << "%#" << max_size_len << radix_fmt << " "
410 << "%#" << max_addr_len << radix_fmt << "\n";
411
412 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000413 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000414 if (!considerForSize(Obj, Section))
415 continue;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000416 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000417 if (error(Section.getName(name)))
418 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000419 uint64_t size = Section.getSize();
420 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000421 std::string namestr = name;
422
Alexey Samsonov48803e52014-03-13 14:37:36 +0000423 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000424 }
425
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000426 if (ELFCommons) {
427 uint64_t CommonSize = getCommonSize(Obj);
428 total += CommonSize;
429 outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
430 CommonSize, static_cast<uint64_t>(0));
431 }
432
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000433 // Print total.
434 fmtbuf.clear();
435 fmt << "%-" << max_name_len << "s "
436 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000437 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000438 total);
439 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000440 // The Berkeley format does not display individual section sizes. It
441 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000442 uint64_t total_text = 0;
443 uint64_t total_data = 0;
444 uint64_t total_bss = 0;
445
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000446 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000447 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000448 uint64_t size = Section.getSize();
449 bool isText = Section.isText();
450 bool isData = Section.isData();
451 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000452 if (isText)
453 total_text += size;
454 else if (isData)
455 total_data += size;
456 else if (isBSS)
457 total_bss += size;
458 }
459
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000460 if (ELFCommons)
461 total_bss += getCommonSize(Obj);
462
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000463 total = total_text + total_data + total_bss;
464
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000465 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000466 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000467 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000468 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000469 }
470
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000471 // Print result.
472 fmt << "%#7" << radix_fmt << " "
473 << "%#7" << radix_fmt << " "
474 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000475 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000476 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000477 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
478 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000479 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000480 }
481}
482
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000483/// Checks to see if the @p o ObjectFile is a Mach-O file and if it is and there
484/// is a list of architecture flags specified then check to make sure this
485/// Mach-O file is one of those architectures or all architectures was
486/// specificed. If not then an error is generated and this routine returns
487/// false. Else it returns true.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000488static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
489 if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) {
490 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
491 bool ArchFound = false;
492 MachO::mach_header H;
493 MachO::mach_header_64 H_64;
494 Triple T;
495 if (MachO->is64Bit()) {
496 H_64 = MachO->MachOObjectFile::getHeader64();
Tim Northover9e8eb412016-04-22 23:21:13 +0000497 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000498 } else {
499 H = MachO->MachOObjectFile::getHeader();
Tim Northover9e8eb412016-04-22 23:21:13 +0000500 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000501 }
502 unsigned i;
Kevin Enderby10769742014-07-01 22:26:31 +0000503 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000504 if (ArchFlags[i] == T.getArchName())
505 ArchFound = true;
506 break;
507 }
508 if (!ArchFound) {
509 errs() << ToolName << ": file: " << file
510 << " does not contain architecture: " << ArchFlags[i] << ".\n";
511 return false;
512 }
513 }
514 return true;
515}
516
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000517/// Print the section sizes for @p file. If @p file is an archive, print the
518/// section sizes for each archive member.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000519static void printFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000520
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000521 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000522 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
523 if (!BinaryOrErr) {
Kevin Enderby600fb3f2016-08-05 18:19:40 +0000524 error(BinaryOrErr.takeError(), file);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000525 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000526 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000527 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000528
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000529 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000530 // This is an archive. Iterate over each member and display its sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000531 Error Err;
532 for (auto &C : a->children(Err)) {
533 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000534 if (!ChildOrErr) {
535 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000536 error(std::move(E), a->getFileName(), C);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000537 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000538 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000539 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000540 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000541 if (!checkMachOAndArchFlags(o, file))
542 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000543 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000544 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
545 else if (MachO && OutputFormat == darwin)
546 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000547 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000548 if (OutputFormat == berkeley) {
549 if (MachO)
550 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
551 else
552 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
553 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000554 }
555 }
Lang Hamesfc209622016-07-14 02:24:01 +0000556 if (Err)
557 error(std::move(Err), a->getFileName());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000558 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000559 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000560 // If we have a list of architecture flags specified dump only those.
561 if (!ArchAll && ArchFlags.size() != 0) {
562 // Look for a slice in the universal binary that matches each ArchFlag.
563 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000564 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000565 ArchFound = false;
566 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
567 E = UB->end_objects();
568 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000569 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000570 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +0000571 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000572 if (UO) {
573 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
574 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
575 if (OutputFormat == sysv)
576 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000577 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000578 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000579 outs() << o->getFileName() << " (for architecture "
580 << I->getArchTypeName() << "): \n";
581 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000582 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000583 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000584 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000585 outs() << o->getFileName() << " (for architecture "
586 << I->getArchTypeName() << ")";
587 outs() << "\n";
588 }
589 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000590 } else if (auto E = isNotObjectErrorInvalidFileType(
591 UO.takeError())) {
592 error(std::move(E), file, ArchFlags.size() > 1 ?
593 StringRef(I->getArchTypeName()) : StringRef());
594 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000595 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000596 I->getAsArchive()) {
597 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000598 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000599 // sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000600 Error Err;
601 for (auto &C : UA->children(Err)) {
602 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000603 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000604 if (auto E = isNotObjectErrorInvalidFileType(
605 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000606 error(std::move(E), UA->getFileName(), C,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000607 ArchFlags.size() > 1 ?
608 StringRef(I->getArchTypeName()) : StringRef());
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000609 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000610 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000611 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
612 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
613 if (OutputFormat == sysv)
614 outs() << o->getFileName() << " (ex " << UA->getFileName()
615 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000616 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000617 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000618 << ")"
619 << " (for architecture " << I->getArchTypeName()
620 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000621 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000622 if (OutputFormat == berkeley) {
623 if (MachO) {
624 outs() << UA->getFileName() << "(" << o->getFileName()
625 << ")";
626 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000627 outs() << " (for architecture " << I->getArchTypeName()
628 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000629 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000630 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000631 outs() << o->getFileName() << " (ex " << UA->getFileName()
632 << ")\n";
633 }
634 }
635 }
Lang Hamesfc209622016-07-14 02:24:01 +0000636 if (Err)
637 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000638 } else {
639 consumeError(AOrErr.takeError());
640 error("Mach-O universal file: " + file + " for architecture " +
641 StringRef(I->getArchTypeName()) +
642 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000643 }
644 }
645 }
646 if (!ArchFound) {
647 errs() << ToolName << ": file: " << file
648 << " does not contain architecture" << ArchFlags[i] << ".\n";
649 return;
650 }
651 }
652 return;
653 }
654 // No architecture flags were specified so if this contains a slice that
655 // matches the host architecture dump only that.
656 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000657 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000658 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
659 E = UB->end_objects();
660 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000661 if (HostArchName == I->getArchTypeName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000662 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000663 if (UO) {
664 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
665 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
666 if (OutputFormat == sysv)
667 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000668 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000669 if (MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000670 outs() << o->getFileName() << " (for architecture "
671 << I->getArchTypeName() << "):\n";
672 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000673 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000674 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000675 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000676 outs() << o->getFileName() << " (for architecture "
677 << I->getArchTypeName() << ")";
678 outs() << "\n";
679 }
680 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000681 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
682 error(std::move(E), file);
683 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000684 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000685 I->getAsArchive()) {
686 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000687 // This is an archive. Iterate over each member and display its
688 // sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000689 Error Err;
690 for (auto &C : UA->children(Err)) {
691 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000692 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000693 if (auto E = isNotObjectErrorInvalidFileType(
694 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000695 error(std::move(E), UA->getFileName(), C);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000696 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000697 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000698 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
699 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
700 if (OutputFormat == sysv)
701 outs() << o->getFileName() << " (ex " << UA->getFileName()
702 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000703 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000704 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
705 << " (for architecture " << I->getArchTypeName()
706 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000707 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000708 if (OutputFormat == berkeley) {
709 if (MachO)
710 outs() << UA->getFileName() << "(" << o->getFileName()
711 << ")\n";
712 else
713 outs() << o->getFileName() << " (ex " << UA->getFileName()
714 << ")\n";
715 }
716 }
717 }
Lang Hamesfc209622016-07-14 02:24:01 +0000718 if (Err)
719 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000720 } else {
721 consumeError(AOrErr.takeError());
722 error("Mach-O universal file: " + file + " for architecture " +
723 StringRef(I->getArchTypeName()) +
724 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000725 }
726 return;
727 }
728 }
729 }
730 // Either all architectures have been specified or none have been specified
731 // and this does not contain the host architecture so dump all the slices.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000732 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000733 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
734 E = UB->end_objects();
735 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000736 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000737 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000738 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000739 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000740 if (OutputFormat == sysv)
741 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000742 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000743 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000744 outs() << o->getFileName() << " (for architecture "
745 << I->getArchTypeName() << "):";
746 outs() << "\n";
747 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000748 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000749 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000750 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000751 outs() << o->getFileName() << " (for architecture "
752 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000753 outs() << "\n";
754 }
755 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000756 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
757 error(std::move(E), file, MoreThanOneArch ?
758 StringRef(I->getArchTypeName()) : StringRef());
759 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000760 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000761 I->getAsArchive()) {
762 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000763 // This is an archive. Iterate over each member and display its sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000764 Error Err;
765 for (auto &C : UA->children(Err)) {
766 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000767 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000768 if (auto E = isNotObjectErrorInvalidFileType(
769 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000770 error(std::move(E), UA->getFileName(), C, MoreThanOneArch ?
Kevin Enderby9acb1092016-05-31 20:35:34 +0000771 StringRef(I->getArchTypeName()) : StringRef());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000772 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000773 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000774 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
775 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
776 if (OutputFormat == sysv)
777 outs() << o->getFileName() << " (ex " << UA->getFileName()
778 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000779 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000780 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000781 << " (for architecture " << I->getArchTypeName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000782 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000783 if (OutputFormat == berkeley) {
784 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000785 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
786 << " (for architecture " << I->getArchTypeName()
787 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000788 else
789 outs() << o->getFileName() << " (ex " << UA->getFileName()
790 << ")\n";
791 }
792 }
793 }
Lang Hamesfc209622016-07-14 02:24:01 +0000794 if (Err)
795 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000796 } else {
797 consumeError(AOrErr.takeError());
798 error("Mach-O universal file: " + file + " for architecture " +
799 StringRef(I->getArchTypeName()) +
800 " is not a Mach-O file or an archive file");
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000801 }
802 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000803 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000804 if (!checkMachOAndArchFlags(o, file))
805 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000806 if (OutputFormat == sysv)
807 outs() << o->getFileName() << " :\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000808 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000809 if (OutputFormat == berkeley) {
810 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000811 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000812 outs() << o->getFileName();
813 outs() << "\n";
814 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000815 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000816 errs() << ToolName << ": " << file << ": "
817 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000818 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000819 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000820 if (OutputFormat == sysv)
821 outs() << "\n";
822}
823
824int main(int argc, char **argv) {
825 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000826 sys::PrintStackTraceOnErrorSignal(argv[0]);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000827 PrettyStackTraceProgram X(argc, argv);
828
Kevin Enderby10769742014-07-01 22:26:31 +0000829 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000830 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
831
832 ToolName = argv[0];
833 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000834 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000835 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000836 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000837
Kevin Enderby10769742014-07-01 22:26:31 +0000838 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000839 if (ArchFlags[i] == "all") {
840 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000841 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000842 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000843 outs() << ToolName << ": for the -arch option: Unknown architecture "
844 << "named '" << ArchFlags[i] << "'";
845 return 1;
846 }
847 }
848 }
849
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000850 if (InputFilenames.size() == 0)
851 InputFilenames.push_back("a.out");
852
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000853 MoreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000854 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000855 printFileSectionSizes);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000856
Kevin Enderby64f7a992016-05-02 21:41:03 +0000857 if (HadError)
858 return 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000859}