blob: 67c81ec9e7cf7fc6ae0e89904029fbcfa3d778fb [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 };
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000074static cl::opt<unsigned int>
Mehdi Amini70a02b02016-12-23 23:55:08 +000075Radix("radix", cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
Kevin Enderby10769742014-07-01 22:26:31 +000076 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000077
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000078static cl::opt<RadixTy>
Kevin Enderby10769742014-07-01 22:26:31 +000079RadixShort(cl::desc("Print size in radix:"),
80 cl::values(clEnumValN(octal, "o", "Print size in octal"),
81 clEnumValN(decimal, "d", "Print size in decimal"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000082 clEnumValN(hexadecimal, "x", "Print size in hexadecimal")),
Kevin Enderby10769742014-07-01 22:26:31 +000083 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000084
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +000085static cl::opt<bool>
86 TotalSizes("totals",
87 cl::desc("Print totals of all objects - Berkeley format only"),
88 cl::init(false));
89
90static cl::alias TotalSizesShort("t", cl::desc("Short for --totals"),
91 cl::aliasopt(TotalSizes));
92
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000093static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000094InputFilenames(cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000095
Fangrui Song1e2d5cb2018-06-22 22:20:10 +000096static bool HadError = false;
Kevin Enderby64f7a992016-05-02 21:41:03 +000097
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000098static std::string ToolName;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000099
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000100/// If ec is not success, print the error and return true.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000101static bool error(std::error_code ec) {
Kevin Enderby10769742014-07-01 22:26:31 +0000102 if (!ec)
103 return false;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000104
Kevin Enderby64f7a992016-05-02 21:41:03 +0000105 HadError = true;
Davide Italiano911eb312016-01-25 01:24:15 +0000106 errs() << ToolName << ": error reading file: " << ec.message() << ".\n";
107 errs().flush();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000108 return true;
109}
110
Kevin Enderby42398052016-06-28 23:16:13 +0000111static bool error(Twine Message) {
112 HadError = true;
113 errs() << ToolName << ": " << Message << ".\n";
114 errs().flush();
115 return true;
116}
117
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000118// This version of error() prints the archive name and member name, for example:
119// "libx.a(foo.o)" after the ToolName before the error message. It sets
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000120// HadError but returns allowing the code to move on to other archive members.
Kevin Enderby9acb1092016-05-31 20:35:34 +0000121static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
122 StringRef ArchitectureName = StringRef()) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000123 HadError = true;
124 errs() << ToolName << ": " << FileName;
125
Kevin Enderbyf4586032016-07-29 17:44:13 +0000126 Expected<StringRef> NameOrErr = C.getName();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000127 // TODO: if we have a error getting the name then it would be nice to print
128 // the index of which archive member this is and or its offset in the
129 // archive instead of "???" as the name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000130 if (!NameOrErr) {
131 consumeError(NameOrErr.takeError());
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000132 errs() << "(" << "???" << ")";
Kevin Enderbyf4586032016-07-29 17:44:13 +0000133 } else
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000134 errs() << "(" << NameOrErr.get() << ")";
135
Kevin Enderby9acb1092016-05-31 20:35:34 +0000136 if (!ArchitectureName.empty())
137 errs() << " (for architecture " << ArchitectureName << ") ";
138
139 std::string Buf;
140 raw_string_ostream OS(Buf);
141 logAllUnhandledErrors(std::move(E), OS, "");
142 OS.flush();
143 errs() << " " << Buf << "\n";
144}
145
146// 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
147// before the error message. It sets HadError but returns allowing the code to
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000148// move on to other architecture slices.
Kevin Enderby9acb1092016-05-31 20:35:34 +0000149static void error(llvm::Error E, StringRef FileName,
150 StringRef ArchitectureName = StringRef()) {
151 HadError = true;
152 errs() << ToolName << ": " << FileName;
153
154 if (!ArchitectureName.empty())
155 errs() << " (for architecture " << ArchitectureName << ") ";
156
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000157 std::string Buf;
158 raw_string_ostream OS(Buf);
159 logAllUnhandledErrors(std::move(E), OS, "");
160 OS.flush();
161 errs() << " " << Buf << "\n";
162}
163
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000164/// Get the length of the string that represents @p num in Radix including the
165/// leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +0000166static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000167 APInt conv(64, num);
168 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000169 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000170 return result.size();
171}
172
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000173/// Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000174static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000175 switch (Radix) {
176 case octal:
177 return PRIo64;
178 case decimal:
179 return PRIu64;
180 case hexadecimal:
181 return PRIx64;
182 }
183 return nullptr;
184}
185
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000186/// Remove unneeded ELF sections from calculation
187static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
188 if (!Obj->isELF())
189 return true;
190 switch (static_cast<ELFSectionRef>(Section).getType()) {
191 case ELF::SHT_NULL:
192 case ELF::SHT_SYMTAB:
193 case ELF::SHT_STRTAB:
194 case ELF::SHT_REL:
195 case ELF::SHT_RELA:
196 return false;
197 }
198 return true;
199}
200
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000201/// Total size of all ELF common symbols
202static uint64_t getCommonSize(ObjectFile *Obj) {
203 uint64_t TotalCommons = 0;
204 for (auto &Sym : Obj->symbols())
205 if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
206 TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
207 return TotalCommons;
208}
209
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000210/// Print the size of each Mach-O segment and section in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000211///
212/// This is when used when @c OutputFormat is darwin and produces the same
213/// output as darwin's size(1) -m output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000214static void printDarwinSectionSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000215 std::string fmtbuf;
216 raw_string_ostream fmt(fmtbuf);
217 const char *radix_fmt = getRadixFmt();
218 if (Radix == hexadecimal)
219 fmt << "0x";
220 fmt << "%" << radix_fmt;
221
Kevin Enderby246a4602014-06-17 17:54:13 +0000222 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000223
224 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000225 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000226 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
227 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
228 outs() << "Segment " << Seg.segname << ": "
229 << format(fmt.str().c_str(), Seg.vmsize);
230 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000231 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
232 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000233 outs() << "\n";
234 total += Seg.vmsize;
235 uint64_t sec_total = 0;
236 for (unsigned J = 0; J < Seg.nsects; ++J) {
237 MachO::section_64 Sec = MachO->getSection64(Load, J);
238 if (Filetype == MachO::MH_OBJECT)
239 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
240 << format("%.16s", &Sec.sectname) << "): ";
241 else
242 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
243 outs() << format(fmt.str().c_str(), Sec.size);
244 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000245 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
246 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000247 outs() << "\n";
248 sec_total += Sec.size;
249 }
250 if (Seg.nsects != 0)
251 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000252 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000253 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000254 uint64_t Seg_vmsize = Seg.vmsize;
Kevin Enderby246a4602014-06-17 17:54:13 +0000255 outs() << "Segment " << Seg.segname << ": "
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000256 << format(fmt.str().c_str(), Seg_vmsize);
Kevin Enderby246a4602014-06-17 17:54:13 +0000257 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000258 outs() << " (vmaddr 0x" << format("%" PRIx32, Seg.vmaddr) << " fileoff "
Kevin Enderby10769742014-07-01 22:26:31 +0000259 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000260 outs() << "\n";
261 total += Seg.vmsize;
262 uint64_t sec_total = 0;
263 for (unsigned J = 0; J < Seg.nsects; ++J) {
264 MachO::section Sec = MachO->getSection(Load, J);
265 if (Filetype == MachO::MH_OBJECT)
266 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
267 << format("%.16s", &Sec.sectname) << "): ";
268 else
269 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000270 uint64_t Sec_size = Sec.size;
271 outs() << format(fmt.str().c_str(), Sec_size);
Kevin Enderby246a4602014-06-17 17:54:13 +0000272 if (DarwinLongFormat)
Kevin Enderby2058e9d2016-02-09 18:33:15 +0000273 outs() << " (addr 0x" << format("%" PRIx32, Sec.addr) << " offset "
Kevin Enderby10769742014-07-01 22:26:31 +0000274 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000275 outs() << "\n";
276 sec_total += Sec.size;
277 }
278 if (Seg.nsects != 0)
279 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
280 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000281 }
282 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
283}
284
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000285/// Print the summary sizes of the standard Mach-O segments in @p MachO.
Kevin Enderby246a4602014-06-17 17:54:13 +0000286///
287/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
288/// produces the same output as darwin's size(1) default output.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000289static void printDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000290 uint64_t total_text = 0;
291 uint64_t total_data = 0;
292 uint64_t total_objc = 0;
293 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000294 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000295 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
296 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
297 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
298 for (unsigned J = 0; J < Seg.nsects; ++J) {
299 MachO::section_64 Sec = MachO->getSection64(Load, J);
300 StringRef SegmentName = StringRef(Sec.segname);
301 if (SegmentName == "__TEXT")
302 total_text += Sec.size;
303 else if (SegmentName == "__DATA")
304 total_data += Sec.size;
305 else if (SegmentName == "__OBJC")
306 total_objc += Sec.size;
307 else
308 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000309 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000310 } else {
311 StringRef SegmentName = StringRef(Seg.segname);
312 if (SegmentName == "__TEXT")
313 total_text += Seg.vmsize;
314 else if (SegmentName == "__DATA")
315 total_data += Seg.vmsize;
316 else if (SegmentName == "__OBJC")
317 total_objc += Seg.vmsize;
318 else
319 total_others += Seg.vmsize;
320 }
Kevin Enderby10769742014-07-01 22:26:31 +0000321 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000322 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
323 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
324 for (unsigned J = 0; J < Seg.nsects; ++J) {
325 MachO::section Sec = MachO->getSection(Load, J);
326 StringRef SegmentName = StringRef(Sec.segname);
327 if (SegmentName == "__TEXT")
328 total_text += Sec.size;
329 else if (SegmentName == "__DATA")
330 total_data += Sec.size;
331 else if (SegmentName == "__OBJC")
332 total_objc += Sec.size;
333 else
334 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000335 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000336 } else {
337 StringRef SegmentName = StringRef(Seg.segname);
338 if (SegmentName == "__TEXT")
339 total_text += Seg.vmsize;
340 else if (SegmentName == "__DATA")
341 total_data += Seg.vmsize;
342 else if (SegmentName == "__OBJC")
343 total_objc += Seg.vmsize;
344 else
345 total_others += Seg.vmsize;
346 }
347 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000348 }
349 uint64_t total = total_text + total_data + total_objc + total_others;
350
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000351 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000352 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000353 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000354 }
355 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
356 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
357 << "\t";
358}
359
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000360/// Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000361///
362/// The format used is determined by @c OutputFormat and @c Radix.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000363static void printObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000364 uint64_t total = 0;
365 std::string fmtbuf;
366 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000367 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000368
Kevin Enderby246a4602014-06-17 17:54:13 +0000369 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
370 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
371 // let it fall through to OutputFormat berkeley.
372 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
373 if (OutputFormat == darwin && MachO)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000374 printDarwinSectionSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000375 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
376 // darwin's default berkeley format for Mach-O files.
377 else if (MachO && OutputFormat == berkeley)
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000378 printDarwinSegmentSizes(MachO);
Kevin Enderby246a4602014-06-17 17:54:13 +0000379 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000380 // Run two passes over all sections. The first gets the lengths needed for
381 // formatting the output. The second actually does the output.
382 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000383 std::size_t max_size_len = strlen("size");
384 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000385 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000386 if (!considerForSize(Obj, Section))
387 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000388 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000389 total += size;
390
391 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000392 if (error(Section.getName(name)))
393 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000394 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000395 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000396 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
397 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000398 }
399
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000400 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000401 max_name_len += 2;
402 max_size_len += 2;
403 max_addr_len += 2;
404
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000405 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000406 fmt << "%-" << max_name_len << "s "
407 << "%" << max_size_len << "s "
408 << "%" << max_addr_len << "s\n";
409
410 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000411 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
412 static_cast<const char *>("size"),
413 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000414 fmtbuf.clear();
415
416 // Setup per section format.
417 fmt << "%-" << max_name_len << "s "
418 << "%#" << max_size_len << radix_fmt << " "
419 << "%#" << max_addr_len << radix_fmt << "\n";
420
421 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000422 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindolaa0ff5562016-02-09 21:39:49 +0000423 if (!considerForSize(Obj, Section))
424 continue;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000425 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000426 if (error(Section.getName(name)))
427 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000428 uint64_t size = Section.getSize();
429 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000430 std::string namestr = name;
431
Alexey Samsonov48803e52014-03-13 14:37:36 +0000432 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000433 }
434
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000435 if (ELFCommons) {
436 uint64_t CommonSize = getCommonSize(Obj);
437 total += CommonSize;
438 outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
439 CommonSize, static_cast<uint64_t>(0));
440 }
441
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000442 // Print total.
443 fmtbuf.clear();
444 fmt << "%-" << max_name_len << "s "
445 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000446 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000447 total);
448 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000449 // The Berkeley format does not display individual section sizes. It
450 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000451 uint64_t total_text = 0;
452 uint64_t total_data = 0;
453 uint64_t total_bss = 0;
454
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000455 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000456 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000457 uint64_t size = Section.getSize();
458 bool isText = Section.isText();
459 bool isData = Section.isData();
460 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000461 if (isText)
462 total_text += size;
463 else if (isData)
464 total_data += size;
465 else if (isBSS)
466 total_bss += size;
467 }
468
Hemant Kulkarni274457e2016-03-28 16:48:10 +0000469 if (ELFCommons)
470 total_bss += getCommonSize(Obj);
471
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000472 total = total_text + total_data + total_bss;
473
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000474 if (TotalSizes) {
475 TotalObjectText += total_text;
476 TotalObjectData += total_data;
477 TotalObjectBss += total_bss;
478 TotalObjectTotal += total;
479 }
480
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000481 if (!BerkeleyHeaderPrinted) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000482 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000483 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000484 BerkeleyHeaderPrinted = true;
Kevin Enderby246a4602014-06-17 17:54:13 +0000485 }
486
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000487 // Print result.
488 fmt << "%#7" << radix_fmt << " "
489 << "%#7" << radix_fmt << " "
490 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000491 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000492 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000493 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
494 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000495 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000496 }
497}
498
David Majnemer91160d82016-10-31 17:11:31 +0000499/// 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 +0000500/// is a list of architecture flags specified then check to make sure this
501/// Mach-O file is one of those architectures or all architectures was
502/// specificed. If not then an error is generated and this routine returns
503/// false. Else it returns true.
David Majnemer91160d82016-10-31 17:11:31 +0000504static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
505 auto *MachO = dyn_cast<MachOObjectFile>(O);
506
507 if (!MachO || ArchAll || ArchFlags.empty())
508 return true;
509
510 MachO::mach_header H;
511 MachO::mach_header_64 H_64;
512 Triple T;
513 if (MachO->is64Bit()) {
514 H_64 = MachO->MachOObjectFile::getHeader64();
515 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
516 } else {
517 H = MachO->MachOObjectFile::getHeader();
518 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
519 }
520 if (none_of(ArchFlags, [&](const std::string &Name) {
521 return Name == T.getArchName();
522 })) {
523 error(Filename + ": No architecture specified");
524 return false;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000525 }
526 return true;
527}
528
Rafael Espindola49a0e5e2016-02-09 21:32:56 +0000529/// Print the section sizes for @p file. If @p file is an archive, print the
530/// section sizes for each archive member.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000531static void printFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000532
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000533 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000534 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
535 if (!BinaryOrErr) {
Kevin Enderby600fb3f2016-08-05 18:19:40 +0000536 error(BinaryOrErr.takeError(), file);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000537 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000538 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000539 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000540
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000541 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000542 // This is an archive. Iterate over each member and display its sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000543 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000544 for (auto &C : a->children(Err)) {
545 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000546 if (!ChildOrErr) {
547 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000548 error(std::move(E), a->getFileName(), C);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000549 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000550 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000551 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000552 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000553 if (!checkMachOAndArchFlags(o, file))
554 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000555 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000556 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
557 else if (MachO && OutputFormat == darwin)
558 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000559 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000560 if (OutputFormat == berkeley) {
561 if (MachO)
562 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
563 else
564 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
565 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000566 }
567 }
Lang Hamesfc209622016-07-14 02:24:01 +0000568 if (Err)
569 error(std::move(Err), a->getFileName());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000570 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000571 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000572 // If we have a list of architecture flags specified dump only those.
573 if (!ArchAll && ArchFlags.size() != 0) {
574 // Look for a slice in the universal binary that matches each ArchFlag.
575 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000576 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000577 ArchFound = false;
578 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
579 E = UB->end_objects();
580 I != E; ++I) {
Kevin Enderby59343a92016-12-16 22:54:02 +0000581 if (ArchFlags[i] == I->getArchFlagName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000582 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +0000583 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000584 if (UO) {
585 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
586 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
587 if (OutputFormat == sysv)
588 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000589 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000590 if (MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000591 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000592 << I->getArchFlagName() << "): \n";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000593 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000594 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000595 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000596 if (!MachO || MoreThanOneFile || ArchFlags.size() > 1)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000597 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000598 << I->getArchFlagName() << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000599 outs() << "\n";
600 }
601 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000602 } else if (auto E = isNotObjectErrorInvalidFileType(
603 UO.takeError())) {
604 error(std::move(E), file, ArchFlags.size() > 1 ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000605 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000606 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000607 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000608 I->getAsArchive()) {
609 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000610 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000611 // sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000612 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000613 for (auto &C : UA->children(Err)) {
614 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000615 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000616 if (auto E = isNotObjectErrorInvalidFileType(
617 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000618 error(std::move(E), UA->getFileName(), C,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000619 ArchFlags.size() > 1 ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000620 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000621 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000622 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000623 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
624 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
625 if (OutputFormat == sysv)
626 outs() << o->getFileName() << " (ex " << UA->getFileName()
627 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000628 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000629 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000630 << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000631 << " (for architecture " << I->getArchFlagName()
Kevin Enderby10769742014-07-01 22:26:31 +0000632 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000633 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000634 if (OutputFormat == berkeley) {
635 if (MachO) {
636 outs() << UA->getFileName() << "(" << o->getFileName()
637 << ")";
638 if (ArchFlags.size() > 1)
Kevin Enderby59343a92016-12-16 22:54:02 +0000639 outs() << " (for architecture " << I->getArchFlagName()
Kevin Enderby10769742014-07-01 22:26:31 +0000640 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000641 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000642 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000643 outs() << o->getFileName() << " (ex " << UA->getFileName()
644 << ")\n";
645 }
646 }
647 }
Lang Hamesfc209622016-07-14 02:24:01 +0000648 if (Err)
649 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000650 } else {
651 consumeError(AOrErr.takeError());
652 error("Mach-O universal file: " + file + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +0000653 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +0000654 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000655 }
656 }
657 }
658 if (!ArchFound) {
659 errs() << ToolName << ": file: " << file
660 << " does not contain architecture" << ArchFlags[i] << ".\n";
661 return;
662 }
663 }
664 return;
665 }
666 // No architecture flags were specified so if this contains a slice that
667 // matches the host architecture dump only that.
668 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000669 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000670 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
671 E = UB->end_objects();
672 I != E; ++I) {
Kevin Enderby59343a92016-12-16 22:54:02 +0000673 if (HostArchName == I->getArchFlagName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000674 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000675 if (UO) {
676 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
677 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
678 if (OutputFormat == sysv)
679 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000680 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000681 if (MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000682 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000683 << I->getArchFlagName() << "):\n";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000684 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000685 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000686 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000687 if (!MachO || MoreThanOneFile)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000688 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000689 << I->getArchFlagName() << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000690 outs() << "\n";
691 }
692 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000693 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
694 error(std::move(E), file);
695 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000696 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000697 I->getAsArchive()) {
698 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000699 // This is an archive. Iterate over each member and display its
700 // sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000701 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000702 for (auto &C : UA->children(Err)) {
703 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000704 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000705 if (auto E = isNotObjectErrorInvalidFileType(
706 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000707 error(std::move(E), UA->getFileName(), C);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000708 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000709 }
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000710 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
711 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
712 if (OutputFormat == sysv)
713 outs() << o->getFileName() << " (ex " << UA->getFileName()
714 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000715 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000716 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000717 << " (for architecture " << I->getArchFlagName()
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000718 << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000719 printObjectSectionSizes(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000720 if (OutputFormat == berkeley) {
721 if (MachO)
722 outs() << UA->getFileName() << "(" << o->getFileName()
723 << ")\n";
724 else
725 outs() << o->getFileName() << " (ex " << UA->getFileName()
726 << ")\n";
727 }
728 }
729 }
Lang Hamesfc209622016-07-14 02:24:01 +0000730 if (Err)
731 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000732 } else {
733 consumeError(AOrErr.takeError());
734 error("Mach-O universal file: " + file + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +0000735 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +0000736 " is not a Mach-O file or an archive file");
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000737 }
738 return;
739 }
740 }
741 }
742 // Either all architectures have been specified or none have been specified
743 // and this does not contain the host architecture so dump all the slices.
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000744 bool MoreThanOneArch = UB->getNumberOfObjects() > 1;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000745 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
746 E = UB->end_objects();
747 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000748 Expected<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000749 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000750 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000751 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000752 if (OutputFormat == sysv)
753 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000754 else if (MachO && OutputFormat == darwin) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000755 if (MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000756 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000757 << I->getArchFlagName() << "):";
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000758 outs() << "\n";
759 }
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000760 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000761 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000762 if (!MachO || MoreThanOneFile || MoreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000763 outs() << o->getFileName() << " (for architecture "
Kevin Enderby59343a92016-12-16 22:54:02 +0000764 << I->getArchFlagName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000765 outs() << "\n";
766 }
767 }
Kevin Enderby9acb1092016-05-31 20:35:34 +0000768 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
769 error(std::move(E), file, MoreThanOneArch ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000770 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000771 return;
Kevin Enderby42398052016-06-28 23:16:13 +0000772 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000773 I->getAsArchive()) {
774 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000775 // This is an archive. Iterate over each member and display its sizes.
Mehdi Amini41af4302016-11-11 04:28:40 +0000776 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000777 for (auto &C : UA->children(Err)) {
778 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000779 if (!ChildOrErr) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000780 if (auto E = isNotObjectErrorInvalidFileType(
781 ChildOrErr.takeError()))
Lang Hamesfc209622016-07-14 02:24:01 +0000782 error(std::move(E), UA->getFileName(), C, MoreThanOneArch ?
Kevin Enderby59343a92016-12-16 22:54:02 +0000783 StringRef(I->getArchFlagName()) : StringRef());
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000784 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000785 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000786 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
787 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
788 if (OutputFormat == sysv)
789 outs() << o->getFileName() << " (ex " << UA->getFileName()
790 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000791 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000792 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000793 << " (for architecture " << I->getArchFlagName() << "):\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000794 printObjectSectionSizes(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000795 if (OutputFormat == berkeley) {
796 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000797 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby59343a92016-12-16 22:54:02 +0000798 << " (for architecture " << I->getArchFlagName()
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000799 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000800 else
801 outs() << o->getFileName() << " (ex " << UA->getFileName()
802 << ")\n";
803 }
804 }
805 }
Lang Hamesfc209622016-07-14 02:24:01 +0000806 if (Err)
807 error(std::move(Err), UA->getFileName());
Kevin Enderby42398052016-06-28 23:16:13 +0000808 } else {
809 consumeError(AOrErr.takeError());
810 error("Mach-O universal file: " + file + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +0000811 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +0000812 " is not a Mach-O file or an archive file");
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000813 }
814 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000815 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000816 if (!checkMachOAndArchFlags(o, file))
817 return;
Kevin Enderby5997c942016-12-01 19:12:55 +0000818 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000819 if (OutputFormat == sysv)
820 outs() << o->getFileName() << " :\n";
Kevin Enderby5997c942016-12-01 19:12:55 +0000821 else if (MachO && OutputFormat == darwin && MoreThanOneFile)
822 outs() << o->getFileName() << ":\n";
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000823 printObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000824 if (OutputFormat == berkeley) {
Rafael Espindola1dc30a42016-02-09 21:35:14 +0000825 if (!MachO || MoreThanOneFile)
Kevin Enderby246a4602014-06-17 17:54:13 +0000826 outs() << o->getFileName();
827 outs() << "\n";
828 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000829 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000830 errs() << ToolName << ": " << file << ": "
831 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000832 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000833 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000834 if (OutputFormat == sysv)
835 outs() << "\n";
836}
837
Hemant Kulkarni5f4ca2f2016-09-12 17:08:28 +0000838static void printBerkelyTotals() {
839 std::string fmtbuf;
840 raw_string_ostream fmt(fmtbuf);
841 const char *radix_fmt = getRadixFmt();
842 fmt << "%#7" << radix_fmt << " "
843 << "%#7" << radix_fmt << " "
844 << "%#7" << radix_fmt << " ";
845 outs() << format(fmt.str().c_str(), TotalObjectText, TotalObjectData,
846 TotalObjectBss);
847 fmtbuf.clear();
848 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
849 << "%7" PRIx64 " ";
850 outs() << format(fmt.str().c_str(), TotalObjectTotal, TotalObjectTotal)
851 << "(TOTALS)\n";
852}
853
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000854int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000855 InitLLVM X(argc, argv);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000856 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
857
858 ToolName = argv[0];
859 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000860 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000861 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000862 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000863
Kevin Enderby10769742014-07-01 22:26:31 +0000864 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000865 if (ArchFlags[i] == "all") {
866 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000867 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000868 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000869 outs() << ToolName << ": for the -arch option: Unknown architecture "
870 << "named '" << ArchFlags[i] << "'";
871 return 1;
872 }
873 }
874 }
875
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000876 if (InputFilenames.size() == 0)
Dimitry Andrice4f5d012017-12-18 19:46:56 +0000877 InputFilenames.push_back("a.out");
878
879 MoreThanOneFile = InputFilenames.size() > 1;
880 llvm::for_each(InputFilenames, printFileSectionSizes);
881 if (OutputFormat == berkeley && TotalSizes)
882 printBerkelyTotals();
883
Kevin Enderby64f7a992016-05-02 21:41:03 +0000884 if (HadError)
885 return 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000886}