blob: 48370136e8ab96225a24bb5b2f2e78bb5924172d [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 Enderbyac9e1552016-05-17 17:10:12 +0000102// This version of error() prints the archive name and member name, for example:
103// "libx.a(foo.o)" after the ToolName before the error message. It sets
104// HadError but returns allowing the code to move on to other archive members.
Kevin Enderby9acb1092016-05-31 20:35:34 +0000105static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
106 StringRef ArchitectureName = StringRef()) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000107 HadError = true;
108 errs() << ToolName << ": " << FileName;
109
110 ErrorOr<StringRef> NameOrErr = C.getName();
111 // TODO: if we have a error getting the name then it would be nice to print
112 // the index of which archive member this is and or its offset in the
113 // archive instead of "???" as the name.
114 if (NameOrErr.getError())
115 errs() << "(" << "???" << ")";
116 else
117 errs() << "(" << NameOrErr.get() << ")";
118
Kevin Enderby9acb1092016-05-31 20:35:34 +0000119 if (!ArchitectureName.empty())
120 errs() << " (for architecture " << ArchitectureName << ") ";
121
122 std::string Buf;
123 raw_string_ostream OS(Buf);
124 logAllUnhandledErrors(std::move(E), OS, "");
125 OS.flush();
126 errs() << " " << Buf << "\n";
127}
128
129// 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
130// before the error message. It sets HadError but returns allowing the code to
131// move on to other architecture slices.
132static void error(llvm::Error E, StringRef FileName,
133 StringRef ArchitectureName = StringRef()) {
134 HadError = true;
135 errs() << ToolName << ": " << FileName;
136
137 if (!ArchitectureName.empty())
138 errs() << " (for architecture " << ArchitectureName << ") ";
139
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000140 std::string Buf;
141 raw_string_ostream OS(Buf);
142 logAllUnhandledErrors(std::move(E), OS, "");
143 OS.flush();
144 errs() << " " << Buf << "\n";
145}
146
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000147/// Get the length of the string that represents @p num in Radix including the
148/// leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +0000149static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000150 APInt conv(64, num);
151 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000152 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000153 return result.size();
154}
155
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000156/// Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000157static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000158 switch (Radix) {
159 case octal:
160 return PRIo64;
161 case decimal:
162 return PRIu64;
163 case hexadecimal:
164 return PRIx64;
165 }
166 return nullptr;
167}
168
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000169/// Remove unneeded ELF sections from calculation
170static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
171 if (!Obj->isELF())
172 return true;
173 switch (static_cast<ELFSectionRef>(Section).getType()) {
174 case ELF::SHT_NULL:
175 case ELF::SHT_SYMTAB:
176 case ELF::SHT_STRTAB:
177 case ELF::SHT_REL:
178 case ELF::SHT_RELA:
179 return false;
180 }
181 return true;
182}
183
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000184/// Total size of all ELF common symbols
185static uint64_t getCommonSize(ObjectFile *Obj) {
186 uint64_t TotalCommons = 0;
187 for (auto &Sym : Obj->symbols())
188 if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
189 TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
190 return TotalCommons;
191}
192
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000193/// Print the size of each Mach-O segment and section in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000194///
195/// This is when used when @c OutputFormat is darwin and produces the same
196/// output as darwin's size(1) -m output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000197static void printDarwinSectionSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000198 std::string fmtbuf;
199 raw_string_ostream fmt(fmtbuf);
200 const char *radix_fmt = getRadixFmt();
201 if (Radix == hexadecimal)
202 fmt << "0x";
203 fmt << "%" << radix_fmt;
204
Kevin Enderby246a4602014-06-17 17:54:13 +0000205 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000206
207 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000208 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000209 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
210 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
211 outs() << "Segment " << Seg.segname << ": "
212 << format(fmt.str().c_str(), Seg.vmsize);
213 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000214 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
215 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000216 outs() << "\n";
217 total += Seg.vmsize;
218 uint64_t sec_total = 0;
219 for (unsigned J = 0; J < Seg.nsects; ++J) {
220 MachO::section_64 Sec = MachO->getSection64(Load, J);
221 if (Filetype == MachO::MH_OBJECT)
222 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
223 << format("%.16s", &Sec.sectname) << "): ";
224 else
225 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
226 outs() << format(fmt.str().c_str(), Sec.size);
227 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000228 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
229 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000230 outs() << "\n";
231 sec_total += Sec.size;
232 }
233 if (Seg.nsects != 0)
234 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000235 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000236 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000237 uint64_t Seg_vmsize = Seg.vmsize;
Kevin Enderby246a4602014-06-17 17:54:13 +0000238 outs() << "Segment " << Seg.segname << ": "
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000239 << format(fmt.str().c_str(), Seg_vmsize);
Kevin Enderby246a4602014-06-17 17:54:13 +0000240 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000241 outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
Kevin Enderby10769742014-07-01 22:26:31 +0000242 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000243 outs() << "\n";
244 total += Seg.vmsize;
245 uint64_t sec_total = 0;
246 for (unsigned J = 0; J < Seg.nsects; ++J) {
247 MachO::section Sec = MachO->getSection(Load, J);
248 if (Filetype == MachO::MH_OBJECT)
249 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
250 << format("%.16s", &Sec.sectname) << "): ";
251 else
252 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000253 uint64_t Sec_size = Sec.size;
254 outs() << format(fmt.str().c_str(), Sec_size);
Kevin Enderby246a4602014-06-17 17:54:13 +0000255 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000256 outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
Kevin Enderby10769742014-07-01 22:26:31 +0000257 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000258 outs() << "\n";
259 sec_total += Sec.size;
260 }
261 if (Seg.nsects != 0)
262 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
263 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000264 }
265 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
266}
267
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000268/// Print the summary sizes of the standard Mach-O segments in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000269///
270/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
271/// produces the same output as darwin's size(1) default output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000272static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000273 uint64_t total_text = 0;
274 uint64_t total_data = 0;
275 uint64_t total_objc = 0;
276 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000277 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000278 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
279 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
280 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
281 for (unsigned J = 0; J < Seg.nsects; ++J) {
282 MachO::section_64 Sec = MachO->getSection64(Load, J);
283 StringRef SegmentName = StringRef(Sec.segname);
284 if (SegmentName == "__TEXT")
285 total_text += Sec.size;
286 else if (SegmentName == "__DATA")
287 total_data += Sec.size;
288 else if (SegmentName == "__OBJC")
289 total_objc += Sec.size;
290 else
291 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000292 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000293 } else {
294 StringRef SegmentName = StringRef(Seg.segname);
295 if (SegmentName == "__TEXT")
296 total_text += Seg.vmsize;
297 else if (SegmentName == "__DATA")
298 total_data += Seg.vmsize;
299 else if (SegmentName == "__OBJC")
300 total_objc += Seg.vmsize;
301 else
302 total_others += Seg.vmsize;
303 }
Kevin Enderby10769742014-07-01 22:26:31 +0000304 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000305 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
306 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
307 for (unsigned J = 0; J < Seg.nsects; ++J) {
308 MachO::section Sec = MachO->getSection(Load, J);
309 StringRef SegmentName = StringRef(Sec.segname);
310 if (SegmentName == "__TEXT")
311 total_text += Sec.size;
312 else if (SegmentName == "__DATA")
313 total_data += Sec.size;
314 else if (SegmentName == "__OBJC")
315 total_objc += Sec.size;
316 else
317 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000318 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000319 } else {
320 StringRef SegmentName = StringRef(Seg.segname);
321 if (SegmentName == "__TEXT")
322 total_text += Seg.vmsize;
323 else if (SegmentName == "__DATA")
324 total_data += Seg.vmsize;
325 else if (SegmentName == "__OBJC")
326 total_objc += Seg.vmsize;
327 else
328 total_others += Seg.vmsize;
329 }
330 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000331 }
332 uint64_t total = total_text + total_data + total_objc + total_others;
333
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000334 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000335 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000336 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000337 }
338 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
339 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
340 << "\t";
341}
342
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000343/// Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000344///
345/// The format used is determined by @c OutputFormat and @c Radix.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000346static void printObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000347 uint64_t total = 0;
348 std::string fmtbuf;
349 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000350 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000351
Kevin Enderby246a4602014-06-17 17:54:13 +0000352 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
353 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
354 // let it fall through to OutputFormat berkeley.
355 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
356 if (OutputFormat == darwin && MachO)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000357 printDarwinSectionSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000358 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
359 // darwin's default berkeley format for Mach-O files.
360 else if (MachO && OutputFormat == berkeley)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000361 printDarwinSegmentSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000362 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000363 // Run two passes over all sections. The first gets the lengths needed for
364 // formatting the output. The second actually does the output.
365 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000366 std::size_t max_size_len = strlen("size");
367 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000368 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000369 if (!considerForSize(Obj, Section))
370 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000371 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000372 total += size;
373
374 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000375 if (error(Section.getName(name)))
376 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000377 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000378 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000379 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
380 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000381 }
382
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000383 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000384 max_name_len += 2;
385 max_size_len += 2;
386 max_addr_len += 2;
387
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000388 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000389 fmt << "%-" << max_name_len << "s "
390 << "%" << max_size_len << "s "
391 << "%" << max_addr_len << "s\n";
392
393 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000394 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
395 static_cast<const char *>("size"),
396 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000397 fmtbuf.clear();
398
399 // Setup per section format.
400 fmt << "%-" << max_name_len << "s "
401 << "%#" << max_size_len << radix_fmt << " "
402 << "%#" << max_addr_len << radix_fmt << "\n";
403
404 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000405 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000406 if (!considerForSize(Obj, Section))
407 continue;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000408 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000409 if (error(Section.getName(name)))
410 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000411 uint64_t size = Section.getSize();
412 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000413 std::string namestr = name;
414
Alexey Samsonov48803e52014-03-13 14:37:36 +0000415 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000416 }
417
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000418 if (ELFCommons) {
419 uint64_t CommonSize = getCommonSize(Obj);
420 total += CommonSize;
421 outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
422 CommonSize, static_cast<uint64_t>(0));
423 }
424
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000425 // Print total.
426 fmtbuf.clear();
427 fmt << "%-" << max_name_len << "s "
428 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000429 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000430 total);
431 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000432 // The Berkeley format does not display individual section sizes. It
433 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000434 uint64_t total_text = 0;
435 uint64_t total_data = 0;
436 uint64_t total_bss = 0;
437
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000438 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000439 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000440 uint64_t size = Section.getSize();
441 bool isText = Section.isText();
442 bool isData = Section.isData();
443 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000444 if (isText)
445 total_text += size;
446 else if (isData)
447 total_data += size;
448 else if (isBSS)
449 total_bss += size;
450 }
451
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000452 if (ELFCommons)
453 total_bss += getCommonSize(Obj);
454
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000455 total = total_text + total_data + total_bss;
456
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000457 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000458 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000459 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000460 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000461 }
462
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000463 // Print result.
464 fmt << "%#7" << radix_fmt << " "
465 << "%#7" << radix_fmt << " "
466 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000467 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000468 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000469 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
470 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000471 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000472 }
473}
474
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000475/// Checks to see if the @p o ObjectFile is a Mach-O file and if it is and there
476/// is a list of architecture flags specified then check to make sure this
477/// Mach-O file is one of those architectures or all architectures was
478/// specificed. If not then an error is generated and this routine returns
479/// false. Else it returns true.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000480static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
481 if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) {
482 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
483 bool ArchFound = false;
484 MachO::mach_header H;
485 MachO::mach_header_64 H_64;
486 Triple T;
487 if (MachO->is64Bit()) {
488 H_64 = MachO->MachOObjectFile::getHeader64();
Tim Northover9e8eb412016-04-22 23:21:13 +0000489 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000490 } else {
491 H = MachO->MachOObjectFile::getHeader();
Tim Northover9e8eb412016-04-22 23:21:13 +0000492 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000493 }
494 unsigned i;
Kevin Enderby10769742014-07-01 22:26:31 +0000495 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000496 if (ArchFlags[i] == T.getArchName())
497 ArchFound = true;
498 break;
499 }
500 if (!ArchFound) {
501 errs() << ToolName << ": file: " << file
502 << " does not contain architecture: " << ArchFlags[i] << ".\n";
503 return false;
504 }
505 }
506 return true;
507}
508
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000509/// Print the section sizes for @p file. If @p file is an archive, print the
510/// section sizes for each archive member.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000511static void printFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000512
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000513 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000514 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
515 if (!BinaryOrErr) {
516 error(errorToErrorCode(BinaryOrErr.takeError()));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000517 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000518 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000519 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000520
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000521 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000522 // This is an archive. Iterate over each member and display its sizes.
Rafael Espindola23a97502014-01-21 16:09:45 +0000523 for (object::Archive::child_iterator i = a->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000524 e = a->child_end();
525 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000526 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000527 exit(1);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000528 Expected<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
529 if (!ChildOrErr) {
530 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
531 error(std::move(E), a->getFileName(), i->get());
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000532 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000533 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000534 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000535 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000536 if (!checkMachOAndArchFlags(o, file))
537 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000538 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000539 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
540 else if (MachO && OutputFormat == darwin)
541 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000542 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000543 if (OutputFormat == berkeley) {
544 if (MachO)
545 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
546 else
547 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
548 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000549 }
550 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000551 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000552 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000553 // If we have a list of architecture flags specified dump only those.
554 if (!ArchAll && ArchFlags.size() != 0) {
555 // Look for a slice in the universal binary that matches each ArchFlag.
556 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000557 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000558 ArchFound = false;
559 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
560 E = UB->end_objects();
561 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000562 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000563 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +0000564 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000565 if (UO) {
566 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
567 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
568 if (OutputFormat == sysv)
569 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000570 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000571 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000572 outs() << o->getFileName() << " (for architecture "
573 << I->getArchTypeName() << "): \n";
574 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000575 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000576 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000577 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000578 outs() << o->getFileName() << " (for architecture "
579 << I->getArchTypeName() << ")";
580 outs() << "\n";
581 }
582 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000583 } else if (auto E = isNotObjectErrorInvalidFileType(
584 UO.takeError())) {
585 error(std::move(E), file, ArchFlags.size() > 1 ?
586 StringRef(I->getArchTypeName()) : StringRef());
587 return;
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000588 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
589 I->getAsArchive()) {
590 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000591 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000592 // sizes.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000593 for (object::Archive::child_iterator i = UA->child_begin(),
594 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000595 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000596 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000597 exit(1);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000598 Expected<std::unique_ptr<Binary>> ChildOrErr =
599 i->get().getAsBinary();
600 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000601 if (auto E = isNotObjectErrorInvalidFileType(
602 ChildOrErr.takeError()))
603 error(std::move(E), UA->getFileName(), i->get(),
604 ArchFlags.size() > 1 ?
605 StringRef(I->getArchTypeName()) : StringRef());
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000606 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000607 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000608 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
609 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
610 if (OutputFormat == sysv)
611 outs() << o->getFileName() << " (ex " << UA->getFileName()
612 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000613 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000614 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000615 << ")"
616 << " (for architecture " << I->getArchTypeName()
617 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000618 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000619 if (OutputFormat == berkeley) {
620 if (MachO) {
621 outs() << UA->getFileName() << "(" << o->getFileName()
622 << ")";
623 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000624 outs() << " (for architecture " << I->getArchTypeName()
625 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000626 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000627 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000628 outs() << o->getFileName() << " (ex " << UA->getFileName()
629 << ")\n";
630 }
631 }
632 }
633 }
634 }
635 }
636 if (!ArchFound) {
637 errs() << ToolName << ": file: " << file
638 << " does not contain architecture" << ArchFlags[i] << ".\n";
639 return;
640 }
641 }
642 return;
643 }
644 // No architecture flags were specified so if this contains a slice that
645 // matches the host architecture dump only that.
646 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000647 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000648 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
649 E = UB->end_objects();
650 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000651 if (HostArchName == I->getArchTypeName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000652 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000653 if (UO) {
654 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
655 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
656 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)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000660 outs() << o->getFileName() << " (for architecture "
661 << I->getArchTypeName() << "):\n";
662 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000663 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000664 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000665 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000666 outs() << o->getFileName() << " (for architecture "
667 << I->getArchTypeName() << ")";
668 outs() << "\n";
669 }
670 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000671 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
672 error(std::move(E), file);
673 return;
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000674 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
675 I->getAsArchive()) {
676 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000677 // This is an archive. Iterate over each member and display its
678 // sizes.
679 for (object::Archive::child_iterator i = UA->child_begin(),
680 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000681 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000682 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000683 exit(1);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000684 Expected<std::unique_ptr<Binary>> ChildOrErr =
685 i->get().getAsBinary();
686 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000687 if (auto E = isNotObjectErrorInvalidFileType(
688 ChildOrErr.takeError()))
689 error(std::move(E), UA->getFileName(), i->get());
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000690 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000691 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000692 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
693 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
694 if (OutputFormat == sysv)
695 outs() << o->getFileName() << " (ex " << UA->getFileName()
696 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000697 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000698 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
699 << " (for architecture " << I->getArchTypeName()
700 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000701 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000702 if (OutputFormat == berkeley) {
703 if (MachO)
704 outs() << UA->getFileName() << "(" << o->getFileName()
705 << ")\n";
706 else
707 outs() << o->getFileName() << " (ex " << UA->getFileName()
708 << ")\n";
709 }
710 }
711 }
712 }
713 return;
714 }
715 }
716 }
717 // Either all architectures have been specified or none have been specified
718 // and this does not contain the host architecture so dump all the slices.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000719 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000720 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
721 E = UB->end_objects();
722 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000723 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000724 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000725 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000726 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000727 if (OutputFormat == sysv)
728 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000729 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000730 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000731 outs() << o->getFileName() << " (for architecture "
732 << I->getArchTypeName() << "):";
733 outs() << "\n";
734 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000735 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000736 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000737 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000738 outs() << o->getFileName() << " (for architecture "
739 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000740 outs() << "\n";
741 }
742 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000743 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
744 error(std::move(E), file, MoreThanOneArch ?
745 StringRef(I->getArchTypeName()) : StringRef());
746 return;
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000747 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
748 I->getAsArchive()) {
749 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000750 // This is an archive. Iterate over each member and display its sizes.
751 for (object::Archive::child_iterator i = UA->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000752 e = UA->child_end();
753 i != e; ++i) {
Davide Italianoff11b902016-02-13 01:38:16 +0000754 if (error(i->getError()))
Kevin Enderby7a969422015-11-05 19:24:56 +0000755 exit(1);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000756 Expected<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary();
757 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000758 if (auto E = isNotObjectErrorInvalidFileType(
759 ChildOrErr.takeError()))
760 error(std::move(E), UA->getFileName(), i->get(), MoreThanOneArch ?
761 StringRef(I->getArchTypeName()) : StringRef());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000762 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000763 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000764 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
765 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
766 if (OutputFormat == sysv)
767 outs() << o->getFileName() << " (ex " << UA->getFileName()
768 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000769 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000770 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000771 << " (for architecture " << I->getArchTypeName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000772 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000773 if (OutputFormat == berkeley) {
774 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000775 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
776 << " (for architecture " << I->getArchTypeName()
777 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000778 else
779 outs() << o->getFileName() << " (ex " << UA->getFileName()
780 << ")\n";
781 }
782 }
783 }
784 }
785 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000786 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000787 if (!checkMachOAndArchFlags(o, file))
788 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000789 if (OutputFormat == sysv)
790 outs() << o->getFileName() << " :\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000791 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000792 if (OutputFormat == berkeley) {
793 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000794 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000795 outs() << o->getFileName();
796 outs() << "\n";
797 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000798 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000799 errs() << ToolName << ": " << file << ": "
800 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000801 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000802 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000803 if (OutputFormat == sysv)
804 outs() << "\n";
805}
806
807int main(int argc, char **argv) {
808 // Print a stack trace if we signal out.
809 sys::PrintStackTraceOnErrorSignal();
810 PrettyStackTraceProgram X(argc, argv);
811
Kevin Enderby10769742014-07-01 22:26:31 +0000812 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000813 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
814
815 ToolName = argv[0];
816 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000817 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000818 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000819 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000820
Kevin Enderby10769742014-07-01 22:26:31 +0000821 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000822 if (ArchFlags[i] == "all") {
823 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000824 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000825 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000826 outs() << ToolName << ": for the -arch option: Unknown architecture "
827 << "named '" << ArchFlags[i] << "'";
828 return 1;
829 }
830 }
831 }
832
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000833 if (InputFilenames.size() == 0)
834 InputFilenames.push_back("a.out");
835
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000836 MoreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000837 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000838 printFileSectionSizes);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000839
Kevin Enderby64f7a992016-05-02 21:41:03 +0000840 if (HadError)
841 return 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000842}