blob: c5966ead4b6c5c38e7aa9e60dcb5881f9301e01a [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
117 ErrorOr<StringRef> NameOrErr = C.getName();
118 // 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.
121 if (NameOrErr.getError())
122 errs() << "(" << "???" << ")";
123 else
124 errs() << "(" << NameOrErr.get() << ")";
125
Kevin Enderby9acb1092016-05-31 20:35:34 +0000126 if (!ArchitectureName.empty())
127 errs() << " (for architecture " << ArchitectureName << ") ";
128
129 std::string Buf;
130 raw_string_ostream OS(Buf);
131 logAllUnhandledErrors(std::move(E), OS, "");
132 OS.flush();
133 errs() << " " << Buf << "\n";
134}
135
136// 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
137// before the error message. It sets HadError but returns allowing the code to
138// move on to other architecture slices.
139static void error(llvm::Error E, StringRef FileName,
140 StringRef ArchitectureName = StringRef()) {
141 HadError = true;
142 errs() << ToolName << ": " << FileName;
143
144 if (!ArchitectureName.empty())
145 errs() << " (for architecture " << ArchitectureName << ") ";
146
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000147 std::string Buf;
148 raw_string_ostream OS(Buf);
149 logAllUnhandledErrors(std::move(E), OS, "");
150 OS.flush();
151 errs() << " " << Buf << "\n";
152}
153
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000154/// Get the length of the string that represents @p num in Radix including the
155/// leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +0000156static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000157 APInt conv(64, num);
158 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000159 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000160 return result.size();
161}
162
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000163/// Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000164static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000165 switch (Radix) {
166 case octal:
167 return PRIo64;
168 case decimal:
169 return PRIu64;
170 case hexadecimal:
171 return PRIx64;
172 }
173 return nullptr;
174}
175
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000176/// Remove unneeded ELF sections from calculation
177static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
178 if (!Obj->isELF())
179 return true;
180 switch (static_cast<ELFSectionRef>(Section).getType()) {
181 case ELF::SHT_NULL:
182 case ELF::SHT_SYMTAB:
183 case ELF::SHT_STRTAB:
184 case ELF::SHT_REL:
185 case ELF::SHT_RELA:
186 return false;
187 }
188 return true;
189}
190
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000191/// Total size of all ELF common symbols
192static uint64_t getCommonSize(ObjectFile *Obj) {
193 uint64_t TotalCommons = 0;
194 for (auto &Sym : Obj->symbols())
195 if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
196 TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
197 return TotalCommons;
198}
199
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000200/// Print the size of each Mach-O segment and section in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000201///
202/// This is when used when @c OutputFormat is darwin and produces the same
203/// output as darwin's size(1) -m output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000204static void printDarwinSectionSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000205 std::string fmtbuf;
206 raw_string_ostream fmt(fmtbuf);
207 const char *radix_fmt = getRadixFmt();
208 if (Radix == hexadecimal)
209 fmt << "0x";
210 fmt << "%" << radix_fmt;
211
Kevin Enderby246a4602014-06-17 17:54:13 +0000212 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000213
214 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000215 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000216 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
217 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
218 outs() << "Segment " << Seg.segname << ": "
219 << format(fmt.str().c_str(), Seg.vmsize);
220 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000221 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
222 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000223 outs() << "\n";
224 total += Seg.vmsize;
225 uint64_t sec_total = 0;
226 for (unsigned J = 0; J < Seg.nsects; ++J) {
227 MachO::section_64 Sec = MachO->getSection64(Load, J);
228 if (Filetype == MachO::MH_OBJECT)
229 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
230 << format("%.16s", &Sec.sectname) << "): ";
231 else
232 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
233 outs() << format(fmt.str().c_str(), Sec.size);
234 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000235 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
236 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000237 outs() << "\n";
238 sec_total += Sec.size;
239 }
240 if (Seg.nsects != 0)
241 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000242 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000243 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000244 uint64_t Seg_vmsize = Seg.vmsize;
Kevin Enderby246a4602014-06-17 17:54:13 +0000245 outs() << "Segment " << Seg.segname << ": "
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000246 << format(fmt.str().c_str(), Seg_vmsize);
Kevin Enderby246a4602014-06-17 17:54:13 +0000247 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000248 outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
Kevin Enderby10769742014-07-01 22:26:31 +0000249 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000250 outs() << "\n";
251 total += Seg.vmsize;
252 uint64_t sec_total = 0;
253 for (unsigned J = 0; J < Seg.nsects; ++J) {
254 MachO::section Sec = MachO->getSection(Load, J);
255 if (Filetype == MachO::MH_OBJECT)
256 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
257 << format("%.16s", &Sec.sectname) << "): ";
258 else
259 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000260 uint64_t Sec_size = Sec.size;
261 outs() << format(fmt.str().c_str(), Sec_size);
Kevin Enderby246a4602014-06-17 17:54:13 +0000262 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000263 outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
Kevin Enderby10769742014-07-01 22:26:31 +0000264 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000265 outs() << "\n";
266 sec_total += Sec.size;
267 }
268 if (Seg.nsects != 0)
269 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
270 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000271 }
272 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
273}
274
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000275/// Print the summary sizes of the standard Mach-O segments in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000276///
277/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
278/// produces the same output as darwin's size(1) default output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000279static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000280 uint64_t total_text = 0;
281 uint64_t total_data = 0;
282 uint64_t total_objc = 0;
283 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000284 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000285 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
286 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
287 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
288 for (unsigned J = 0; J < Seg.nsects; ++J) {
289 MachO::section_64 Sec = MachO->getSection64(Load, J);
290 StringRef SegmentName = StringRef(Sec.segname);
291 if (SegmentName == "__TEXT")
292 total_text += Sec.size;
293 else if (SegmentName == "__DATA")
294 total_data += Sec.size;
295 else if (SegmentName == "__OBJC")
296 total_objc += Sec.size;
297 else
298 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000299 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000300 } else {
301 StringRef SegmentName = StringRef(Seg.segname);
302 if (SegmentName == "__TEXT")
303 total_text += Seg.vmsize;
304 else if (SegmentName == "__DATA")
305 total_data += Seg.vmsize;
306 else if (SegmentName == "__OBJC")
307 total_objc += Seg.vmsize;
308 else
309 total_others += Seg.vmsize;
310 }
Kevin Enderby10769742014-07-01 22:26:31 +0000311 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000312 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
313 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
314 for (unsigned J = 0; J < Seg.nsects; ++J) {
315 MachO::section Sec = MachO->getSection(Load, J);
316 StringRef SegmentName = StringRef(Sec.segname);
317 if (SegmentName == "__TEXT")
318 total_text += Sec.size;
319 else if (SegmentName == "__DATA")
320 total_data += Sec.size;
321 else if (SegmentName == "__OBJC")
322 total_objc += Sec.size;
323 else
324 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000325 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000326 } else {
327 StringRef SegmentName = StringRef(Seg.segname);
328 if (SegmentName == "__TEXT")
329 total_text += Seg.vmsize;
330 else if (SegmentName == "__DATA")
331 total_data += Seg.vmsize;
332 else if (SegmentName == "__OBJC")
333 total_objc += Seg.vmsize;
334 else
335 total_others += Seg.vmsize;
336 }
337 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000338 }
339 uint64_t total = total_text + total_data + total_objc + total_others;
340
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000341 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000342 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000343 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000344 }
345 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
346 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
347 << "\t";
348}
349
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000350/// Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000351///
352/// The format used is determined by @c OutputFormat and @c Radix.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000353static void printObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000354 uint64_t total = 0;
355 std::string fmtbuf;
356 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000357 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000358
Kevin Enderby246a4602014-06-17 17:54:13 +0000359 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
360 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
361 // let it fall through to OutputFormat berkeley.
362 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
363 if (OutputFormat == darwin && MachO)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000364 printDarwinSectionSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000365 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
366 // darwin's default berkeley format for Mach-O files.
367 else if (MachO && OutputFormat == berkeley)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000368 printDarwinSegmentSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000369 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000370 // Run two passes over all sections. The first gets the lengths needed for
371 // formatting the output. The second actually does the output.
372 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000373 std::size_t max_size_len = strlen("size");
374 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000375 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000376 if (!considerForSize(Obj, Section))
377 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000378 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000379 total += size;
380
381 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000382 if (error(Section.getName(name)))
383 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000384 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000385 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000386 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
387 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000388 }
389
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000390 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000391 max_name_len += 2;
392 max_size_len += 2;
393 max_addr_len += 2;
394
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000395 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000396 fmt << "%-" << max_name_len << "s "
397 << "%" << max_size_len << "s "
398 << "%" << max_addr_len << "s\n";
399
400 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000401 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
402 static_cast<const char *>("size"),
403 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000404 fmtbuf.clear();
405
406 // Setup per section format.
407 fmt << "%-" << max_name_len << "s "
408 << "%#" << max_size_len << radix_fmt << " "
409 << "%#" << max_addr_len << radix_fmt << "\n";
410
411 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000412 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000413 if (!considerForSize(Obj, Section))
414 continue;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000415 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000416 if (error(Section.getName(name)))
417 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000418 uint64_t size = Section.getSize();
419 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000420 std::string namestr = name;
421
Alexey Samsonov48803e52014-03-13 14:37:36 +0000422 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000423 }
424
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000425 if (ELFCommons) {
426 uint64_t CommonSize = getCommonSize(Obj);
427 total += CommonSize;
428 outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
429 CommonSize, static_cast<uint64_t>(0));
430 }
431
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000432 // Print total.
433 fmtbuf.clear();
434 fmt << "%-" << max_name_len << "s "
435 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000436 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000437 total);
438 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000439 // The Berkeley format does not display individual section sizes. It
440 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000441 uint64_t total_text = 0;
442 uint64_t total_data = 0;
443 uint64_t total_bss = 0;
444
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000445 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000446 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000447 uint64_t size = Section.getSize();
448 bool isText = Section.isText();
449 bool isData = Section.isData();
450 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000451 if (isText)
452 total_text += size;
453 else if (isData)
454 total_data += size;
455 else if (isBSS)
456 total_bss += size;
457 }
458
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000459 if (ELFCommons)
460 total_bss += getCommonSize(Obj);
461
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000462 total = total_text + total_data + total_bss;
463
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000464 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000465 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000466 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000467 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000468 }
469
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000470 // Print result.
471 fmt << "%#7" << radix_fmt << " "
472 << "%#7" << radix_fmt << " "
473 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000474 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000475 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000476 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
477 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000478 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000479 }
480}
481
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000482/// Checks to see if the @p o ObjectFile is a Mach-O file and if it is and there
483/// is a list of architecture flags specified then check to make sure this
484/// Mach-O file is one of those architectures or all architectures was
485/// specificed. If not then an error is generated and this routine returns
486/// false. Else it returns true.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000487static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
488 if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) {
489 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
490 bool ArchFound = false;
491 MachO::mach_header H;
492 MachO::mach_header_64 H_64;
493 Triple T;
494 if (MachO->is64Bit()) {
495 H_64 = MachO->MachOObjectFile::getHeader64();
Tim Northover9e8eb412016-04-22 23:21:13 +0000496 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000497 } else {
498 H = MachO->MachOObjectFile::getHeader();
Tim Northover9e8eb412016-04-22 23:21:13 +0000499 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000500 }
501 unsigned i;
Kevin Enderby10769742014-07-01 22:26:31 +0000502 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000503 if (ArchFlags[i] == T.getArchName())
504 ArchFound = true;
505 break;
506 }
507 if (!ArchFound) {
508 errs() << ToolName << ": file: " << file
509 << " does not contain architecture: " << ArchFlags[i] << ".\n";
510 return false;
511 }
512 }
513 return true;
514}
515
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000516/// Print the section sizes for @p file. If @p file is an archive, print the
517/// section sizes for each archive member.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000518static void printFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000519
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000520 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000521 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
522 if (!BinaryOrErr) {
523 error(errorToErrorCode(BinaryOrErr.takeError()));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000524 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000525 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000526 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000527
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000528 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000529 // This is an archive. Iterate over each member and display its sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000530 Error Err;
531 for (auto &C : a->children(Err)) {
532 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000533 if (!ChildOrErr) {
534 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000535 error(std::move(E), a->getFileName(), C);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000536 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000537 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000538 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000539 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000540 if (!checkMachOAndArchFlags(o, file))
541 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000542 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000543 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
544 else if (MachO && OutputFormat == darwin)
545 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000546 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000547 if (OutputFormat == berkeley) {
548 if (MachO)
549 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
550 else
551 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
552 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000553 }
554 }
Lang Hamesfc209622016-07-14 02:24:01 +0000555 if (Err)
556 error(std::move(Err), a->getFileName());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000557 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000558 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000559 // If we have a list of architecture flags specified dump only those.
560 if (!ArchAll && ArchFlags.size() != 0) {
561 // Look for a slice in the universal binary that matches each ArchFlag.
562 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000563 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000564 ArchFound = false;
565 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
566 E = UB->end_objects();
567 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000568 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000569 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +0000570 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000571 if (UO) {
572 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
573 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
574 if (OutputFormat == sysv)
575 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000576 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000577 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000578 outs() << o->getFileName() << " (for architecture "
579 << I->getArchTypeName() << "): \n";
580 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000581 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000582 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000583 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000584 outs() << o->getFileName() << " (for architecture "
585 << I->getArchTypeName() << ")";
586 outs() << "\n";
587 }
588 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000589 } else if (auto E = isNotObjectErrorInvalidFileType(
590 UO.takeError())) {
591 error(std::move(E), file, ArchFlags.size() > 1 ?
592 StringRef(I->getArchTypeName()) : StringRef());
593 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000594 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000595 I->getAsArchive()) {
596 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000597 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000598 // sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000599 Error Err;
600 for (auto &C : UA->children(Err)) {
601 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000602 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000603 if (auto E = isNotObjectErrorInvalidFileType(
604 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000605 error(std::move(E), UA->getFileName(), C,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000606 ArchFlags.size() > 1 ?
607 StringRef(I->getArchTypeName()) : StringRef());
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000608 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000609 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000610 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
611 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
612 if (OutputFormat == sysv)
613 outs() << o->getFileName() << " (ex " << UA->getFileName()
614 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000615 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000616 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000617 << ")"
618 << " (for architecture " << I->getArchTypeName()
619 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000620 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000621 if (OutputFormat == berkeley) {
622 if (MachO) {
623 outs() << UA->getFileName() << "(" << o->getFileName()
624 << ")";
625 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000626 outs() << " (for architecture " << I->getArchTypeName()
627 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000628 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000629 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000630 outs() << o->getFileName() << " (ex " << UA->getFileName()
631 << ")\n";
632 }
633 }
634 }
Lang Hamesfc209622016-07-14 02:24:01 +0000635 if (Err)
636 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000637 } else {
638 consumeError(AOrErr.takeError());
639 error("Mach-O universal file: " + file + " for architecture " +
640 StringRef(I->getArchTypeName()) +
641 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000642 }
643 }
644 }
645 if (!ArchFound) {
646 errs() << ToolName << ": file: " << file
647 << " does not contain architecture" << ArchFlags[i] << ".\n";
648 return;
649 }
650 }
651 return;
652 }
653 // No architecture flags were specified so if this contains a slice that
654 // matches the host architecture dump only that.
655 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000656 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000657 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
658 E = UB->end_objects();
659 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000660 if (HostArchName == I->getArchTypeName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000661 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000662 if (UO) {
663 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
664 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
665 if (OutputFormat == sysv)
666 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000667 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000668 if (MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000669 outs() << o->getFileName() << " (for architecture "
670 << I->getArchTypeName() << "):\n";
671 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000672 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000673 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000674 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000675 outs() << o->getFileName() << " (for architecture "
676 << I->getArchTypeName() << ")";
677 outs() << "\n";
678 }
679 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000680 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
681 error(std::move(E), file);
682 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000683 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000684 I->getAsArchive()) {
685 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000686 // This is an archive. Iterate over each member and display its
687 // sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000688 Error Err;
689 for (auto &C : UA->children(Err)) {
690 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000691 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000692 if (auto E = isNotObjectErrorInvalidFileType(
693 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000694 error(std::move(E), UA->getFileName(), C);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000695 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000696 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000697 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
698 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
699 if (OutputFormat == sysv)
700 outs() << o->getFileName() << " (ex " << UA->getFileName()
701 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000702 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000703 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
704 << " (for architecture " << I->getArchTypeName()
705 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000706 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000707 if (OutputFormat == berkeley) {
708 if (MachO)
709 outs() << UA->getFileName() << "(" << o->getFileName()
710 << ")\n";
711 else
712 outs() << o->getFileName() << " (ex " << UA->getFileName()
713 << ")\n";
714 }
715 }
716 }
Lang Hamesfc209622016-07-14 02:24:01 +0000717 if (Err)
718 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000719 } else {
720 consumeError(AOrErr.takeError());
721 error("Mach-O universal file: " + file + " for architecture " +
722 StringRef(I->getArchTypeName()) +
723 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000724 }
725 return;
726 }
727 }
728 }
729 // Either all architectures have been specified or none have been specified
730 // and this does not contain the host architecture so dump all the slices.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000731 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000732 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
733 E = UB->end_objects();
734 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000735 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000736 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000737 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000738 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000739 if (OutputFormat == sysv)
740 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000741 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000742 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000743 outs() << o->getFileName() << " (for architecture "
744 << I->getArchTypeName() << "):";
745 outs() << "\n";
746 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000747 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000748 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000749 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000750 outs() << o->getFileName() << " (for architecture "
751 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000752 outs() << "\n";
753 }
754 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000755 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
756 error(std::move(E), file, MoreThanOneArch ?
757 StringRef(I->getArchTypeName()) : StringRef());
758 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000759 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000760 I->getAsArchive()) {
761 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000762 // This is an archive. Iterate over each member and display its sizes.
Lang Hamesfc209622016-07-14 02:24:01 +0000763 Error Err;
764 for (auto &C : UA->children(Err)) {
765 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000766 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000767 if (auto E = isNotObjectErrorInvalidFileType(
768 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000769 error(std::move(E), UA->getFileName(), C, MoreThanOneArch ?
Kevin Enderby9acb1092016-05-31 20:35:34 +0000770 StringRef(I->getArchTypeName()) : StringRef());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000771 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000772 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000773 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
774 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
775 if (OutputFormat == sysv)
776 outs() << o->getFileName() << " (ex " << UA->getFileName()
777 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000778 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000779 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000780 << " (for architecture " << I->getArchTypeName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000781 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000782 if (OutputFormat == berkeley) {
783 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000784 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
785 << " (for architecture " << I->getArchTypeName()
786 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000787 else
788 outs() << o->getFileName() << " (ex " << UA->getFileName()
789 << ")\n";
790 }
791 }
792 }
Lang Hamesfc209622016-07-14 02:24:01 +0000793 if (Err)
794 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000795 } else {
796 consumeError(AOrErr.takeError());
797 error("Mach-O universal file: " + file + " for architecture " +
798 StringRef(I->getArchTypeName()) +
799 " is not a Mach-O file or an archive file");
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000800 }
801 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000802 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000803 if (!checkMachOAndArchFlags(o, file))
804 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000805 if (OutputFormat == sysv)
806 outs() << o->getFileName() << " :\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000807 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000808 if (OutputFormat == berkeley) {
809 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000810 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000811 outs() << o->getFileName();
812 outs() << "\n";
813 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000814 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000815 errs() << ToolName << ": " << file << ": "
816 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000817 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000818 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000819 if (OutputFormat == sysv)
820 outs() << "\n";
821}
822
823int main(int argc, char **argv) {
824 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000825 sys::PrintStackTraceOnErrorSignal(argv[0]);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000826 PrettyStackTraceProgram X(argc, argv);
827
Kevin Enderby10769742014-07-01 22:26:31 +0000828 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000829 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
830
831 ToolName = argv[0];
832 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000833 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000834 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000835 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000836
Kevin Enderby10769742014-07-01 22:26:31 +0000837 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000838 if (ArchFlags[i] == "all") {
839 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000840 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000841 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000842 outs() << ToolName << ": for the -arch option: Unknown architecture "
843 << "named '" << ArchFlags[i] << "'";
844 return 1;
845 }
846 }
847 }
848
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000849 if (InputFilenames.size() == 0)
850 InputFilenames.push_back("a.out");
851
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000852 MoreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000853 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000854 printFileSectionSizes);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000855
Kevin Enderby64f7a992016-05-02 21:41:03 +0000856 if (HadError)
857 return 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000858}