blob: 3922cad570b5918697097cdcfae4cd33088a8d9a [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"
Rui Ueyama197194b2018-04-13 18:26:06 +000026#include "llvm/Support/InitLLVM.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000027#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000028#include "llvm/Support/raw_ostream.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000029#include <algorithm>
30#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000031#include <system_error>
Eugene Zelenkoffec81c2015-11-04 22:32:32 +000032
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000033using namespace llvm;
34using namespace object;
35
Kevin Enderby10769742014-07-01 22:26:31 +000036enum OutputFormatTy { berkeley, sysv, darwin };
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000037static cl::opt<OutputFormatTy>
Kevin Enderby10769742014-07-01 22:26:31 +000038OutputFormat("format", cl::desc("Specify output format"),
39 cl::values(clEnumVal(sysv, "System V format"),
40 clEnumVal(berkeley, "Berkeley format"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000041 clEnumVal(darwin, "Darwin -m format")),
Kevin Enderby10769742014-07-01 22:26:31 +000042 cl::init(berkeley));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000043
Kevin Enderby10769742014-07-01 22:26:31 +000044static cl::opt<OutputFormatTy> OutputFormatShort(
45 cl::desc("Specify output format"),
46 cl::values(clEnumValN(sysv, "A", "System V format"),
47 clEnumValN(berkeley, "B", "Berkeley format"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000048 clEnumValN(darwin, "m", "Darwin -m format")),
Kevin Enderby10769742014-07-01 22:26:31 +000049 cl::init(berkeley));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000050
Rafael Espindola1dc30a42016-02-09 21:35:14 +000051static bool BerkeleyHeaderPrinted = false;
52static bool MoreThanOneFile = false;
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +000053static uint64_t TotalObjectText = 0;
54static uint64_t TotalObjectData = 0;
55static uint64_t TotalObjectBss = 0;
56static uint64_t TotalObjectTotal = 0;
Kevin Enderby246a4602014-06-17 17:54:13 +000057
Kevin Enderby10769742014-07-01 22:26:31 +000058cl::opt<bool>
59DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
60 "to include addresses and offsets."));
Kevin Enderby246a4602014-06-17 17:54:13 +000061
Hemant Kulkarni274457e2016-03-28 16:48:10 +000062cl::opt<bool>
63 ELFCommons("common",
64 cl::desc("Print common symbols in the ELF file. When using "
65 "Berkely format, this is added to bss."),
66 cl::init(false));
67
Kevin Enderbyafef4c92014-07-01 17:19:10 +000068static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000069ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
70 cl::ZeroOrMore);
Fangrui Song1e2d5cb2018-06-22 22:20:10 +000071static bool ArchAll = false;
Kevin Enderbyafef4c92014-07-01 17:19:10 +000072
Kevin Enderby10769742014-07-01 22:26:31 +000073enum RadixTy { octal = 8, decimal = 10, hexadecimal = 16 };
James Hendersonb3735ee2018-10-30 11:52:47 +000074static cl::opt<RadixTy> Radix(
75 "radix", cl::desc("Print size in radix"), cl::init(decimal),
76 cl::values(clEnumValN(octal, "8", "Print size in octal"),
77 clEnumValN(decimal, "10", "Print size in decimal"),
78 clEnumValN(hexadecimal, "16", "Print size in hexadecimal")));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000079
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000080static cl::opt<RadixTy>
Kevin Enderby10769742014-07-01 22:26:31 +000081RadixShort(cl::desc("Print size in radix:"),
82 cl::values(clEnumValN(octal, "o", "Print size in octal"),
83 clEnumValN(decimal, "d", "Print size in decimal"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000084 clEnumValN(hexadecimal, "x", "Print size in hexadecimal")),
Kevin Enderby10769742014-07-01 22:26:31 +000085 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000086
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +000087static cl::opt<bool>
88 TotalSizes("totals",
89 cl::desc("Print totals of all objects - Berkeley format only"),
90 cl::init(false));
91
92static cl::alias TotalSizesShort("t", cl::desc("Short for --totals"),
93 cl::aliasopt(TotalSizes));
94
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000095static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000096InputFilenames(cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000097
Fangrui Song1e2d5cb2018-06-22 22:20:10 +000098static bool HadError = false;
Kevin Enderby64f7a992016-05-02 21:41:03 +000099
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000100static std::string ToolName;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000101
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000102/// If ec is not success, print the error and return true.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000103static bool error(std::error_code ec) {
Kevin Enderby10769742014-07-01 22:26:31 +0000104 if (!ec)
105 return false;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000106
Kevin Enderby64f7a992016-05-02 21:41:03 +0000107 HadError = true;
Davide Italiano911eb312016-01-25 01:24:15 +0000108 errs() << ToolName << ": error reading file: " << ec.message() << ".\n";
109 errs().flush();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000110 return true;
111}
112
Kevin Enderby42398052016-06-28 23:16:13 +0000113static bool error(Twine Message) {
114 HadError = true;
115 errs() << ToolName << ": " << Message << ".\n";
116 errs().flush();
117 return true;
118}
119
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000120// This version of error() prints the archive name and member name, for example:
121// "libx.a(foo.o)" after the ToolName before the error message. It sets
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000122// HadError but returns allowing the code to move on to other archive members.
Kevin Enderby9acb1092016-05-31 20:35:34 +0000123static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
124 StringRef ArchitectureName = StringRef()) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000125 HadError = true;
126 errs() << ToolName << ": " << FileName;
127
Kevin Enderbyf4586032016-07-29 17:44:13 +0000128 Expected<StringRef> NameOrErr = C.getName();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000129 // TODO: if we have a error getting the name then it would be nice to print
130 // the index of which archive member this is and or its offset in the
131 // archive instead of "???" as the name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000132 if (!NameOrErr) {
133 consumeError(NameOrErr.takeError());
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000134 errs() << "(" << "???" << ")";
Kevin Enderbyf4586032016-07-29 17:44:13 +0000135 } else
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000136 errs() << "(" << NameOrErr.get() << ")";
137
Kevin Enderby9acb1092016-05-31 20:35:34 +0000138 if (!ArchitectureName.empty())
139 errs() << " (for architecture " << ArchitectureName << ") ";
140
141 std::string Buf;
142 raw_string_ostream OS(Buf);
Jonas Devlieghere45eb84f2018-11-11 01:46:03 +0000143 logAllUnhandledErrors(std::move(E), OS);
Kevin Enderby9acb1092016-05-31 20:35:34 +0000144 OS.flush();
145 errs() << " " << Buf << "\n";
146}
147
148// 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
149// before the error message. It sets HadError but returns allowing the code to
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000150// move on to other architecture slices.
Kevin Enderby9acb1092016-05-31 20:35:34 +0000151static void error(llvm::Error E, StringRef FileName,
152 StringRef ArchitectureName = StringRef()) {
153 HadError = true;
154 errs() << ToolName << ": " << FileName;
155
156 if (!ArchitectureName.empty())
157 errs() << " (for architecture " << ArchitectureName << ") ";
158
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000159 std::string Buf;
160 raw_string_ostream OS(Buf);
Jonas Devlieghere45eb84f2018-11-11 01:46:03 +0000161 logAllUnhandledErrors(std::move(E), OS);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000162 OS.flush();
163 errs() << " " << Buf << "\n";
164}
165
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000166/// Get the length of the string that represents @p num in Radix including the
167/// leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +0000168static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000169 APInt conv(64, num);
170 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000171 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000172 return result.size();
173}
174
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000175/// Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000176static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000177 switch (Radix) {
178 case octal:
179 return PRIo64;
180 case decimal:
181 return PRIu64;
182 case hexadecimal:
183 return PRIx64;
184 }
185 return nullptr;
186}
187
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000188/// Remove unneeded ELF sections from calculation
189static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
190 if (!Obj->isELF())
191 return true;
192 switch (static_cast<ELFSectionRef>(Section).getType()) {
193 case ELF::SHT_NULL:
194 case ELF::SHT_SYMTAB:
195 case ELF::SHT_STRTAB:
196 case ELF::SHT_REL:
197 case ELF::SHT_RELA:
198 return false;
199 }
200 return true;
201}
202
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000203/// Total size of all ELF common symbols
204static uint64_t getCommonSize(ObjectFile *Obj) {
205 uint64_t TotalCommons = 0;
206 for (auto &Sym : Obj->symbols())
207 if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
208 TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
209 return TotalCommons;
210}
211
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000212/// Print the size of each Mach-O segment and section in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000213///
214/// This is when used when @c OutputFormat is darwin and produces the same
215/// output as darwin's size(1) -m output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000216static void printDarwinSectionSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000217 std::string fmtbuf;
218 raw_string_ostream fmt(fmtbuf);
219 const char *radix_fmt = getRadixFmt();
220 if (Radix == hexadecimal)
221 fmt << "0x";
222 fmt << "%" << radix_fmt;
223
Kevin Enderby246a4602014-06-17 17:54:13 +0000224 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000225
226 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000227 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000228 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
229 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
230 outs() << "Segment " << Seg.segname << ": "
231 << format(fmt.str().c_str(), Seg.vmsize);
232 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000233 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
234 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000235 outs() << "\n";
236 total += Seg.vmsize;
237 uint64_t sec_total = 0;
238 for (unsigned J = 0; J < Seg.nsects; ++J) {
239 MachO::section_64 Sec = MachO->getSection64(Load, J);
240 if (Filetype == MachO::MH_OBJECT)
241 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
242 << format("%.16s", &Sec.sectname) << "): ";
243 else
244 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
245 outs() << format(fmt.str().c_str(), Sec.size);
246 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000247 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
248 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000249 outs() << "\n";
250 sec_total += Sec.size;
251 }
252 if (Seg.nsects != 0)
253 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000254 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000255 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000256 uint64_t Seg_vmsize = Seg.vmsize;
Kevin Enderby246a4602014-06-17 17:54:13 +0000257 outs() << "Segment " << Seg.segname << ": "
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000258 << format(fmt.str().c_str(), Seg_vmsize);
Kevin Enderby246a4602014-06-17 17:54:13 +0000259 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000260 outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
Kevin Enderby10769742014-07-01 22:26:31 +0000261 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000262 outs() << "\n";
263 total += Seg.vmsize;
264 uint64_t sec_total = 0;
265 for (unsigned J = 0; J < Seg.nsects; ++J) {
266 MachO::section Sec = MachO->getSection(Load, J);
267 if (Filetype == MachO::MH_OBJECT)
268 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
269 << format("%.16s", &Sec.sectname) << "): ";
270 else
271 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000272 uint64_t Sec_size = Sec.size;
273 outs() << format(fmt.str().c_str(), Sec_size);
Kevin Enderby246a4602014-06-17 17:54:13 +0000274 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000275 outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
Kevin Enderby10769742014-07-01 22:26:31 +0000276 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000277 outs() << "\n";
278 sec_total += Sec.size;
279 }
280 if (Seg.nsects != 0)
281 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
282 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000283 }
284 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
285}
286
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000287/// Print the summary sizes of the standard Mach-O segments in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000288///
289/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
290/// produces the same output as darwin's size(1) default output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000291static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000292 uint64_t total_text = 0;
293 uint64_t total_data = 0;
294 uint64_t total_objc = 0;
295 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000296 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000297 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
298 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
299 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
300 for (unsigned J = 0; J < Seg.nsects; ++J) {
301 MachO::section_64 Sec = MachO->getSection64(Load, J);
302 StringRef SegmentName = StringRef(Sec.segname);
303 if (SegmentName == "__TEXT")
304 total_text += Sec.size;
305 else if (SegmentName == "__DATA")
306 total_data += Sec.size;
307 else if (SegmentName == "__OBJC")
308 total_objc += Sec.size;
309 else
310 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000311 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000312 } else {
313 StringRef SegmentName = StringRef(Seg.segname);
314 if (SegmentName == "__TEXT")
315 total_text += Seg.vmsize;
316 else if (SegmentName == "__DATA")
317 total_data += Seg.vmsize;
318 else if (SegmentName == "__OBJC")
319 total_objc += Seg.vmsize;
320 else
321 total_others += Seg.vmsize;
322 }
Kevin Enderby10769742014-07-01 22:26:31 +0000323 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000324 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
325 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
326 for (unsigned J = 0; J < Seg.nsects; ++J) {
327 MachO::section Sec = MachO->getSection(Load, J);
328 StringRef SegmentName = StringRef(Sec.segname);
329 if (SegmentName == "__TEXT")
330 total_text += Sec.size;
331 else if (SegmentName == "__DATA")
332 total_data += Sec.size;
333 else if (SegmentName == "__OBJC")
334 total_objc += Sec.size;
335 else
336 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000337 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000338 } else {
339 StringRef SegmentName = StringRef(Seg.segname);
340 if (SegmentName == "__TEXT")
341 total_text += Seg.vmsize;
342 else if (SegmentName == "__DATA")
343 total_data += Seg.vmsize;
344 else if (SegmentName == "__OBJC")
345 total_objc += Seg.vmsize;
346 else
347 total_others += Seg.vmsize;
348 }
349 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000350 }
351 uint64_t total = total_text + total_data + total_objc + total_others;
352
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000353 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000354 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000355 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000356 }
357 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
358 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
359 << "\t";
360}
361
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000362/// Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000363///
364/// The format used is determined by @c OutputFormat and @c Radix.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000365static void printObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000366 uint64_t total = 0;
367 std::string fmtbuf;
368 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000369 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000370
Kevin Enderby246a4602014-06-17 17:54:13 +0000371 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
372 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
373 // let it fall through to OutputFormat berkeley.
374 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
375 if (OutputFormat == darwin && MachO)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000376 printDarwinSectionSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000377 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
378 // darwin's default berkeley format for Mach-O files.
379 else if (MachO && OutputFormat == berkeley)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000380 printDarwinSegmentSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000381 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000382 // Run two passes over all sections. The first gets the lengths needed for
383 // formatting the output. The second actually does the output.
384 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000385 std::size_t max_size_len = strlen("size");
386 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000387 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000388 if (!considerForSize(Obj, Section))
389 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000390 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000391 total += size;
392
393 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000394 if (error(Section.getName(name)))
395 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000396 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000397 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000398 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
399 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000400 }
401
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000402 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000403 max_name_len += 2;
404 max_size_len += 2;
405 max_addr_len += 2;
406
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000407 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000408 fmt << "%-" << max_name_len << "s "
409 << "%" << max_size_len << "s "
410 << "%" << max_addr_len << "s\n";
411
412 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000413 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
414 static_cast<const char *>("size"),
415 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000416 fmtbuf.clear();
417
418 // Setup per section format.
419 fmt << "%-" << max_name_len << "s "
420 << "%#" << max_size_len << radix_fmt << " "
421 << "%#" << max_addr_len << radix_fmt << "\n";
422
423 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000424 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000425 if (!considerForSize(Obj, Section))
426 continue;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000427 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000428 if (error(Section.getName(name)))
429 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000430 uint64_t size = Section.getSize();
431 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000432 std::string namestr = name;
433
Alexey Samsonov48803e52014-03-13 14:37:36 +0000434 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000435 }
436
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000437 if (ELFCommons) {
438 uint64_t CommonSize = getCommonSize(Obj);
439 total += CommonSize;
440 outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
441 CommonSize, static_cast<uint64_t>(0));
442 }
443
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000444 // Print total.
445 fmtbuf.clear();
446 fmt << "%-" << max_name_len << "s "
447 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000448 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000449 total);
450 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000451 // The Berkeley format does not display individual section sizes. It
452 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000453 uint64_t total_text = 0;
454 uint64_t total_data = 0;
455 uint64_t total_bss = 0;
456
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000457 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000458 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000459 uint64_t size = Section.getSize();
460 bool isText = Section.isText();
461 bool isData = Section.isData();
462 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000463 if (isText)
464 total_text += size;
465 else if (isData)
466 total_data += size;
467 else if (isBSS)
468 total_bss += size;
469 }
470
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000471 if (ELFCommons)
472 total_bss += getCommonSize(Obj);
473
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000474 total = total_text + total_data + total_bss;
475
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000476 if (TotalSizes) {
477 TotalObjectText += total_text;
478 TotalObjectData += total_data;
479 TotalObjectBss += total_bss;
480 TotalObjectTotal += total;
481 }
482
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000483 if (!BerkeleyHeaderPrinted) {
Jordan Rupprecht8d60f9b2018-09-21 23:48:12 +0000484 outs() << " text\t"
485 " data\t"
486 " bss\t"
487 " "
488 << (Radix == octal ? "oct" : "dec")
489 << "\t"
490 " hex\t"
491 "filename\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000492 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000493 }
494
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000495 // Print result.
Jordan Rupprecht8d60f9b2018-09-21 23:48:12 +0000496 fmt << "%#7" << radix_fmt << "\t"
497 << "%#7" << radix_fmt << "\t"
498 << "%#7" << radix_fmt << "\t";
Kevin Enderby10769742014-07-01 22:26:31 +0000499 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000500 fmtbuf.clear();
Jordan Rupprecht8d60f9b2018-09-21 23:48:12 +0000501 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t"
502 << "%7" PRIx64 "\t";
Kevin Enderby10769742014-07-01 22:26:31 +0000503 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000504 }
505}
506
David Majnemer91160d82016-10-31 17:11:31 +0000507/// Checks to see if the @p O ObjectFile is a Mach-O file and if it is and there
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000508/// is a list of architecture flags specified then check to make sure this
509/// Mach-O file is one of those architectures or all architectures was
510/// specificed. If not then an error is generated and this routine returns
511/// false. Else it returns true.
David Majnemer91160d82016-10-31 17:11:31 +0000512static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
513 auto *MachO = dyn_cast<MachOObjectFile>(O);
514
515 if (!MachO || ArchAll || ArchFlags.empty())
516 return true;
517
518 MachO::mach_header H;
519 MachO::mach_header_64 H_64;
520 Triple T;
521 if (MachO->is64Bit()) {
522 H_64 = MachO->MachOObjectFile::getHeader64();
523 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
524 } else {
525 H = MachO->MachOObjectFile::getHeader();
526 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
527 }
528 if (none_of(ArchFlags, [&](const std::string &Name) {
529 return Name == T.getArchName();
530 })) {
531 error(Filename + ": No architecture specified");
532 return false;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000533 }
534 return true;
535}
536
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000537/// Print the section sizes for @p file. If @p file is an archive, print the
538/// section sizes for each archive member.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000539static void printFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000540
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000541 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000542 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
543 if (!BinaryOrErr) {
Kevin Enderby600fb3f2016-08-05 18:19:40 +0000544 error(BinaryOrErr.takeError(), file);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000545 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000546 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000547 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000548
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000549 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000550 // This is an archive. Iterate over each member and display its sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000551 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000552 for (auto &C : a->children(Err)) {
553 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000554 if (!ChildOrErr) {
555 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000556 error(std::move(E), a->getFileName(), C);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000557 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000558 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000559 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000560 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000561 if (!checkMachOAndArchFlags(o, file))
562 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000563 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000564 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
565 else if (MachO && OutputFormat == darwin)
566 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000567 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000568 if (OutputFormat == berkeley) {
569 if (MachO)
570 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
571 else
572 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
573 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000574 }
575 }
Lang Hamesfc209622016-07-14 02:24:01 +0000576 if (Err)
577 error(std::move(Err), a->getFileName());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000578 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000579 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000580 // If we have a list of architecture flags specified dump only those.
581 if (!ArchAll && ArchFlags.size() != 0) {
582 // Look for a slice in the universal binary that matches each ArchFlag.
583 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000584 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000585 ArchFound = false;
586 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
587 E = UB->end_objects();
588 I != E; ++I) {
Kevin Enderby59343a92016-12-16 22:54:02 +0000589 if (ArchFlags[i] == I->getArchFlagName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000590 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +0000591 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000592 if (UO) {
593 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
594 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
595 if (OutputFormat == sysv)
596 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000597 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000598 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000599 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000600 << I->getArchFlagName() << "): \n";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000601 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000602 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000603 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000604 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000605 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000606 << I->getArchFlagName() << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000607 outs() << "\n";
608 }
609 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000610 } else if (auto E = isNotObjectErrorInvalidFileType(
611 UO.takeError())) {
612 error(std::move(E), file, ArchFlags.size() > 1 ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000613 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000614 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000615 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000616 I->getAsArchive()) {
617 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000618 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000619 // sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000620 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000621 for (auto &C : UA->children(Err)) {
622 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000623 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000624 if (auto E = isNotObjectErrorInvalidFileType(
625 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000626 error(std::move(E), UA->getFileName(), C,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000627 ArchFlags.size() > 1 ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000628 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000629 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000630 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000631 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
632 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
633 if (OutputFormat == sysv)
634 outs() << o->getFileName() << " (ex " << UA->getFileName()
635 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000636 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000637 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000638 << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000639 << " (for architecture " << I->getArchFlagName()
Kevin Enderby10769742014-07-01 22:26:31 +0000640 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000641 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000642 if (OutputFormat == berkeley) {
643 if (MachO) {
644 outs() << UA->getFileName() << "(" << o->getFileName()
645 << ")";
646 if (ArchFlags.size() > 1)
Kevin Enderby59343a92016-12-16 22:54:02 +0000647 outs() << " (for architecture " << I->getArchFlagName()
Kevin Enderby10769742014-07-01 22:26:31 +0000648 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000649 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000650 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000651 outs() << o->getFileName() << " (ex " << UA->getFileName()
652 << ")\n";
653 }
654 }
655 }
Lang Hamesfc209622016-07-14 02:24:01 +0000656 if (Err)
657 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000658 } else {
659 consumeError(AOrErr.takeError());
660 error("Mach-O universal file: " + file + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +0000661 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +0000662 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000663 }
664 }
665 }
666 if (!ArchFound) {
667 errs() << ToolName << ": file: " << file
668 << " does not contain architecture" << ArchFlags[i] << ".\n";
669 return;
670 }
671 }
672 return;
673 }
674 // No architecture flags were specified so if this contains a slice that
675 // matches the host architecture dump only that.
676 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000677 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000678 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
679 E = UB->end_objects();
680 I != E; ++I) {
Kevin Enderby59343a92016-12-16 22:54:02 +0000681 if (HostArchName == I->getArchFlagName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000682 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000683 if (UO) {
684 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
685 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
686 if (OutputFormat == sysv)
687 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000688 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000689 if (MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000690 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000691 << I->getArchFlagName() << "):\n";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000692 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000693 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000694 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000695 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000696 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000697 << I->getArchFlagName() << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000698 outs() << "\n";
699 }
700 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000701 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
702 error(std::move(E), file);
703 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000704 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000705 I->getAsArchive()) {
706 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000707 // This is an archive. Iterate over each member and display its
708 // sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000709 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000710 for (auto &C : UA->children(Err)) {
711 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000712 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000713 if (auto E = isNotObjectErrorInvalidFileType(
714 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000715 error(std::move(E), UA->getFileName(), C);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000716 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000717 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000718 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
719 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
720 if (OutputFormat == sysv)
721 outs() << o->getFileName() << " (ex " << UA->getFileName()
722 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000723 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000724 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000725 << " (for architecture " << I->getArchFlagName()
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000726 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000727 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000728 if (OutputFormat == berkeley) {
729 if (MachO)
730 outs() << UA->getFileName() << "(" << o->getFileName()
731 << ")\n";
732 else
733 outs() << o->getFileName() << " (ex " << UA->getFileName()
734 << ")\n";
735 }
736 }
737 }
Lang Hamesfc209622016-07-14 02:24:01 +0000738 if (Err)
739 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000740 } else {
741 consumeError(AOrErr.takeError());
742 error("Mach-O universal file: " + file + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +0000743 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +0000744 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000745 }
746 return;
747 }
748 }
749 }
750 // Either all architectures have been specified or none have been specified
751 // and this does not contain the host architecture so dump all the slices.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000752 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000753 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
754 E = UB->end_objects();
755 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000756 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000757 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000758 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000759 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000760 if (OutputFormat == sysv)
761 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000762 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000763 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000764 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000765 << I->getArchFlagName() << "):";
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000766 outs() << "\n";
767 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000768 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000769 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000770 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000771 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000772 << I->getArchFlagName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000773 outs() << "\n";
774 }
775 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000776 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
777 error(std::move(E), file, MoreThanOneArch ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000778 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000779 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000780 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000781 I->getAsArchive()) {
782 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000783 // This is an archive. Iterate over each member and display its sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000784 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000785 for (auto &C : UA->children(Err)) {
786 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000787 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000788 if (auto E = isNotObjectErrorInvalidFileType(
789 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000790 error(std::move(E), UA->getFileName(), C, MoreThanOneArch ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000791 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000792 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000793 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000794 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
795 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
796 if (OutputFormat == sysv)
797 outs() << o->getFileName() << " (ex " << UA->getFileName()
798 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000799 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000800 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000801 << " (for architecture " << I->getArchFlagName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000802 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000803 if (OutputFormat == berkeley) {
804 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000805 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000806 << " (for architecture " << I->getArchFlagName()
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000807 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000808 else
809 outs() << o->getFileName() << " (ex " << UA->getFileName()
810 << ")\n";
811 }
812 }
813 }
Lang Hamesfc209622016-07-14 02:24:01 +0000814 if (Err)
815 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000816 } else {
817 consumeError(AOrErr.takeError());
818 error("Mach-O universal file: " + file + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +0000819 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +0000820 " is not a Mach-O file or an archive file");
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000821 }
822 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000823 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000824 if (!checkMachOAndArchFlags(o, file))
825 return;
Kevin Enderby5997c942016-12-01 19:12:55 +0000826 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000827 if (OutputFormat == sysv)
828 outs() << o->getFileName() << " :\n";
Kevin Enderby5997c942016-12-01 19:12:55 +0000829 else if (MachO && OutputFormat == darwin && MoreThanOneFile)
830 outs() << o->getFileName() << ":\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000831 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000832 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000833 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000834 outs() << o->getFileName();
835 outs() << "\n";
836 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000837 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000838 errs() << ToolName << ": " << file << ": "
839 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000840 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000841 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000842 if (OutputFormat == sysv)
843 outs() << "\n";
844}
845
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000846static void printBerkelyTotals() {
847 std::string fmtbuf;
848 raw_string_ostream fmt(fmtbuf);
849 const char *radix_fmt = getRadixFmt();
Jordan Rupprecht8d60f9b2018-09-21 23:48:12 +0000850 fmt << "%#7" << radix_fmt << "\t"
851 << "%#7" << radix_fmt << "\t"
852 << "%#7" << radix_fmt << "\t";
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000853 outs() << format(fmt.str().c_str(), TotalObjectText, TotalObjectData,
854 TotalObjectBss);
855 fmtbuf.clear();
Jordan Rupprecht8d60f9b2018-09-21 23:48:12 +0000856 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t"
857 << "%7" PRIx64 "\t";
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000858 outs() << format(fmt.str().c_str(), TotalObjectTotal, TotalObjectTotal)
859 << "(TOTALS)\n";
860}
861
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000862int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000863 InitLLVM X(argc, argv);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000864 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
865
866 ToolName = argv[0];
867 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000868 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000869 if (RadixShort.getNumOccurrences())
James Hendersonb3735ee2018-10-30 11:52:47 +0000870 Radix = RadixShort.getValue();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000871
Kevin Enderby10769742014-07-01 22:26:31 +0000872 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000873 if (ArchFlags[i] == "all") {
874 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000875 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000876 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000877 outs() << ToolName << ": for the -arch option: Unknown architecture "
878 << "named '" << ArchFlags[i] << "'";
879 return 1;
880 }
881 }
882 }
883
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000884 if (InputFilenames.size() == 0)
Dimitry Andrice4f5d012017-12-18 19:46:56 +0000885 InputFilenames.push_back("a.out");
886
887 MoreThanOneFile = InputFilenames.size() > 1;
888 llvm::for_each(InputFilenames, printFileSectionSizes);
889 if (OutputFormat == berkeley && TotalSizes)
890 printBerkelyTotals();
891
Kevin Enderby64f7a992016-05-02 21:41:03 +0000892 if (HadError)
893 return 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000894}