blob: 24c9cd862feb7ec8f96ec979682fcd5c99a693f7 [file] [log] [blame]
Michael J. Spencerc4ad4662011-09-28 20:57:46 +00001//===-- llvm-size.cpp - Print the size of each object section -------------===//
2//
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"
18#include "llvm/Object/ObjectFile.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"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000021#include "llvm/Support/Casting.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/Format.h"
25#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000028#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000030#include <algorithm>
31#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000032#include <system_error>
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"),
41 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
42 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"),
48 clEnumValN(darwin, "m", "Darwin -m format"), clEnumValEnd),
49 cl::init(berkeley));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000050
Kevin Enderby246a4602014-06-17 17:54:13 +000051static bool berkeleyHeaderPrinted = false;
52static bool moreThanOneFile = false;
53
Kevin Enderby10769742014-07-01 22:26:31 +000054cl::opt<bool>
55DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
56 "to include addresses and offsets."));
Kevin Enderby246a4602014-06-17 17:54:13 +000057
Kevin Enderbyafef4c92014-07-01 17:19:10 +000058static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000059ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
60 cl::ZeroOrMore);
Kevin Enderbyafef4c92014-07-01 17:19:10 +000061bool ArchAll = false;
62
Kevin Enderby10769742014-07-01 22:26:31 +000063enum RadixTy { octal = 8, decimal = 10, hexadecimal = 16 };
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000064static cl::opt<unsigned int>
Kevin Enderby10769742014-07-01 22:26:31 +000065Radix("-radix", cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
66 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000067
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000068static cl::opt<RadixTy>
Kevin Enderby10769742014-07-01 22:26:31 +000069RadixShort(cl::desc("Print size in radix:"),
70 cl::values(clEnumValN(octal, "o", "Print size in octal"),
71 clEnumValN(decimal, "d", "Print size in decimal"),
72 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
73 clEnumValEnd),
74 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000075
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000076static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000077InputFilenames(cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000078
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000079static std::string ToolName;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000080
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000081/// @brief If ec is not success, print the error and return true.
Rafael Espindola4453e42942014-06-13 03:07:50 +000082static bool error(std::error_code ec) {
Kevin Enderby10769742014-07-01 22:26:31 +000083 if (!ec)
84 return false;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000085
86 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
87 outs().flush();
88 return true;
89}
90
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000091/// @brief Get the length of the string that represents @p num in Radix
92/// including the leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +000093static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000094 APInt conv(64, num);
95 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000096 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000097 return result.size();
98}
99
Kevin Enderby246a4602014-06-17 17:54:13 +0000100/// @brief Return the the printing format for the Radix.
Kevin Enderby10769742014-07-01 22:26:31 +0000101static const char *getRadixFmt(void) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000102 switch (Radix) {
103 case octal:
104 return PRIo64;
105 case decimal:
106 return PRIu64;
107 case hexadecimal:
108 return PRIx64;
109 }
110 return nullptr;
111}
112
113/// @brief Print the size of each Mach-O segment and section in @p MachO.
114///
115/// This is when used when @c OutputFormat is darwin and produces the same
116/// output as darwin's size(1) -m output.
117static void PrintDarwinSectionSizes(MachOObjectFile *MachO) {
118 std::string fmtbuf;
119 raw_string_ostream fmt(fmtbuf);
120 const char *radix_fmt = getRadixFmt();
121 if (Radix == hexadecimal)
122 fmt << "0x";
123 fmt << "%" << radix_fmt;
124
125 uint32_t LoadCommandCount = MachO->getHeader().ncmds;
126 uint32_t Filetype = MachO->getHeader().filetype;
127 MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
128
129 uint64_t total = 0;
Kevin Enderby10769742014-07-01 22:26:31 +0000130 for (unsigned I = 0;; ++I) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000131 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
132 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
133 outs() << "Segment " << Seg.segname << ": "
134 << format(fmt.str().c_str(), Seg.vmsize);
135 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000136 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
137 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000138 outs() << "\n";
139 total += Seg.vmsize;
140 uint64_t sec_total = 0;
141 for (unsigned J = 0; J < Seg.nsects; ++J) {
142 MachO::section_64 Sec = MachO->getSection64(Load, J);
143 if (Filetype == MachO::MH_OBJECT)
144 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
145 << format("%.16s", &Sec.sectname) << "): ";
146 else
147 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
148 outs() << format(fmt.str().c_str(), Sec.size);
149 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000150 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
151 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000152 outs() << "\n";
153 sec_total += Sec.size;
154 }
155 if (Seg.nsects != 0)
156 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000157 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000158 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
159 outs() << "Segment " << Seg.segname << ": "
160 << format(fmt.str().c_str(), Seg.vmsize);
161 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000162 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
163 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000164 outs() << "\n";
165 total += Seg.vmsize;
166 uint64_t sec_total = 0;
167 for (unsigned J = 0; J < Seg.nsects; ++J) {
168 MachO::section Sec = MachO->getSection(Load, J);
169 if (Filetype == MachO::MH_OBJECT)
170 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
171 << format("%.16s", &Sec.sectname) << "): ";
172 else
173 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
174 outs() << format(fmt.str().c_str(), Sec.size);
175 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000176 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
177 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000178 outs() << "\n";
179 sec_total += Sec.size;
180 }
181 if (Seg.nsects != 0)
182 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
183 }
184 if (I == LoadCommandCount - 1)
185 break;
186 else
187 Load = MachO->getNextLoadCommandInfo(Load);
188 }
189 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
190}
191
192/// @brief Print the summary sizes of the standard Mach-O segments in @p MachO.
193///
194/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
195/// produces the same output as darwin's size(1) default output.
196static void PrintDarwinSegmentSizes(MachOObjectFile *MachO) {
197 uint32_t LoadCommandCount = MachO->getHeader().ncmds;
198 MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
199
200 uint64_t total_text = 0;
201 uint64_t total_data = 0;
202 uint64_t total_objc = 0;
203 uint64_t total_others = 0;
Kevin Enderby10769742014-07-01 22:26:31 +0000204 for (unsigned I = 0;; ++I) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000205 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
206 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
207 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
208 for (unsigned J = 0; J < Seg.nsects; ++J) {
209 MachO::section_64 Sec = MachO->getSection64(Load, J);
210 StringRef SegmentName = StringRef(Sec.segname);
211 if (SegmentName == "__TEXT")
212 total_text += Sec.size;
213 else if (SegmentName == "__DATA")
214 total_data += Sec.size;
215 else if (SegmentName == "__OBJC")
216 total_objc += Sec.size;
217 else
218 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000219 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000220 } else {
221 StringRef SegmentName = StringRef(Seg.segname);
222 if (SegmentName == "__TEXT")
223 total_text += Seg.vmsize;
224 else if (SegmentName == "__DATA")
225 total_data += Seg.vmsize;
226 else if (SegmentName == "__OBJC")
227 total_objc += Seg.vmsize;
228 else
229 total_others += Seg.vmsize;
230 }
Kevin Enderby10769742014-07-01 22:26:31 +0000231 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000232 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
233 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
234 for (unsigned J = 0; J < Seg.nsects; ++J) {
235 MachO::section Sec = MachO->getSection(Load, J);
236 StringRef SegmentName = StringRef(Sec.segname);
237 if (SegmentName == "__TEXT")
238 total_text += Sec.size;
239 else if (SegmentName == "__DATA")
240 total_data += Sec.size;
241 else if (SegmentName == "__OBJC")
242 total_objc += Sec.size;
243 else
244 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000245 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000246 } else {
247 StringRef SegmentName = StringRef(Seg.segname);
248 if (SegmentName == "__TEXT")
249 total_text += Seg.vmsize;
250 else if (SegmentName == "__DATA")
251 total_data += Seg.vmsize;
252 else if (SegmentName == "__OBJC")
253 total_objc += Seg.vmsize;
254 else
255 total_others += Seg.vmsize;
256 }
257 }
258 if (I == LoadCommandCount - 1)
259 break;
260 else
261 Load = MachO->getNextLoadCommandInfo(Load);
262 }
263 uint64_t total = total_text + total_data + total_objc + total_others;
264
265 if (!berkeleyHeaderPrinted) {
266 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
267 berkeleyHeaderPrinted = true;
268 }
269 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
270 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
271 << "\t";
272}
273
Alexey Samsonov48803e52014-03-13 14:37:36 +0000274/// @brief Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000275///
276/// The format used is determined by @c OutputFormat and @c Radix.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000277static void PrintObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000278 uint64_t total = 0;
279 std::string fmtbuf;
280 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000281 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000282
Kevin Enderby246a4602014-06-17 17:54:13 +0000283 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
284 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
285 // let it fall through to OutputFormat berkeley.
286 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
287 if (OutputFormat == darwin && MachO)
288 PrintDarwinSectionSizes(MachO);
289 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
290 // darwin's default berkeley format for Mach-O files.
291 else if (MachO && OutputFormat == berkeley)
292 PrintDarwinSegmentSizes(MachO);
293 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000294 // Run two passes over all sections. The first gets the lengths needed for
295 // formatting the output. The second actually does the output.
296 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000297 std::size_t max_size_len = strlen("size");
298 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000299 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000300 uint64_t size = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000301 if (error(Section.getSize(size)))
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000302 return;
303 total += size;
304
305 StringRef name;
306 uint64_t addr = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000307 if (error(Section.getName(name)))
308 return;
309 if (error(Section.getAddress(addr)))
310 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000311 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000312 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
313 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000314 }
315
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000316 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000317 max_name_len += 2;
318 max_size_len += 2;
319 max_addr_len += 2;
320
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000321 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000322 fmt << "%-" << max_name_len << "s "
323 << "%" << max_size_len << "s "
324 << "%" << max_addr_len << "s\n";
325
326 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000327 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
328 static_cast<const char *>("size"),
329 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000330 fmtbuf.clear();
331
332 // Setup per section format.
333 fmt << "%-" << max_name_len << "s "
334 << "%#" << max_size_len << radix_fmt << " "
335 << "%#" << max_addr_len << radix_fmt << "\n";
336
337 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000338 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000339 StringRef name;
340 uint64_t size = 0;
341 uint64_t addr = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000342 if (error(Section.getName(name)))
343 return;
344 if (error(Section.getSize(size)))
345 return;
346 if (error(Section.getAddress(addr)))
347 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000348 std::string namestr = name;
349
Alexey Samsonov48803e52014-03-13 14:37:36 +0000350 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000351 }
352
353 // Print total.
354 fmtbuf.clear();
355 fmt << "%-" << max_name_len << "s "
356 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000357 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000358 total);
359 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000360 // The Berkeley format does not display individual section sizes. It
361 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000362 uint64_t total_text = 0;
363 uint64_t total_data = 0;
364 uint64_t total_bss = 0;
365
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000366 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000367 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000368 uint64_t size = 0;
369 bool isText = false;
370 bool isData = false;
371 bool isBSS = false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000372 if (error(Section.getSize(size)))
373 return;
374 if (error(Section.isText(isText)))
375 return;
376 if (error(Section.isData(isData)))
377 return;
378 if (error(Section.isBSS(isBSS)))
379 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000380 if (isText)
381 total_text += size;
382 else if (isData)
383 total_data += size;
384 else if (isBSS)
385 total_bss += size;
386 }
387
388 total = total_text + total_data + total_bss;
389
Kevin Enderby246a4602014-06-17 17:54:13 +0000390 if (!berkeleyHeaderPrinted) {
391 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000392 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Kevin Enderby246a4602014-06-17 17:54:13 +0000393 berkeleyHeaderPrinted = true;
394 }
395
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000396 // Print result.
397 fmt << "%#7" << radix_fmt << " "
398 << "%#7" << radix_fmt << " "
399 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000400 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000401 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000402 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
403 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000404 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000405 }
406}
407
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000408/// @brief Checks to see if the @p o ObjectFile is a Mach-O file and if it is
409/// and there is a list of architecture flags specified then check to
410/// make sure this Mach-O file is one of those architectures or all
411/// architectures was specificed. If not then an error is generated and
412/// this routine returns false. Else it returns true.
413static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
414 if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) {
415 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
416 bool ArchFound = false;
417 MachO::mach_header H;
418 MachO::mach_header_64 H_64;
419 Triple T;
420 if (MachO->is64Bit()) {
421 H_64 = MachO->MachOObjectFile::getHeader64();
422 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
423 } else {
424 H = MachO->MachOObjectFile::getHeader();
425 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
426 }
427 unsigned i;
Kevin Enderby10769742014-07-01 22:26:31 +0000428 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000429 if (ArchFlags[i] == T.getArchName())
430 ArchFound = true;
431 break;
432 }
433 if (!ArchFound) {
434 errs() << ToolName << ": file: " << file
435 << " does not contain architecture: " << ArchFlags[i] << ".\n";
436 return false;
437 }
438 }
439 return true;
440}
441
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000442/// @brief Print the section sizes for @p file. If @p file is an archive, print
443/// the section sizes for each archive member.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000444static void PrintFileSectionSizes(StringRef file) {
445 // If file is not stdin, check that it exists.
446 if (file != "-") {
447 bool exists;
448 if (sys::fs::exists(file, exists) || !exists) {
Kevin Enderby10769742014-07-01 22:26:31 +0000449 errs() << ToolName << ": '" << file << "': "
450 << "No such file\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000451 return;
452 }
453 }
454
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000455 // Attempt to open the binary.
Rafael Espindola437b0d52014-07-31 03:12:45 +0000456 ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000457 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000458 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000459 return;
460 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000461 Binary &Bin = *BinaryOrErr.get();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000462
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000463 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000464 // This is an archive. Iterate over each member and display its sizes.
Rafael Espindola23a97502014-01-21 16:09:45 +0000465 for (object::Archive::child_iterator i = a->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000466 e = a->child_end();
467 i != e; ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000468 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
469 if (std::error_code EC = ChildOrErr.getError()) {
470 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000471 continue;
472 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000473 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000474 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000475 if (!checkMachOAndArchFlags(o, file))
476 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000477 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000478 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
479 else if (MachO && OutputFormat == darwin)
480 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000481 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000482 if (OutputFormat == berkeley) {
483 if (MachO)
484 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
485 else
486 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
487 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000488 }
489 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000490 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000491 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000492 // If we have a list of architecture flags specified dump only those.
493 if (!ArchAll && ArchFlags.size() != 0) {
494 // Look for a slice in the universal binary that matches each ArchFlag.
495 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000496 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000497 ArchFound = false;
498 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
499 E = UB->end_objects();
500 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000501 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000502 ArchFound = true;
503 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
504 std::unique_ptr<Archive> UA;
505 if (UO) {
506 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
507 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
508 if (OutputFormat == sysv)
509 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000510 else if (MachO && OutputFormat == darwin) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000511 if (moreThanOneFile || ArchFlags.size() > 1)
512 outs() << o->getFileName() << " (for architecture "
513 << I->getArchTypeName() << "): \n";
514 }
515 PrintObjectSectionSizes(o);
516 if (OutputFormat == berkeley) {
517 if (!MachO || moreThanOneFile || ArchFlags.size() > 1)
518 outs() << o->getFileName() << " (for architecture "
519 << I->getArchTypeName() << ")";
520 outs() << "\n";
521 }
522 }
Kevin Enderby10769742014-07-01 22:26:31 +0000523 } else if (!I->getAsArchive(UA)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000524 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000525 // sizes.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000526 for (object::Archive::child_iterator i = UA->child_begin(),
527 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000528 i != e; ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000529 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
530 if (std::error_code EC = ChildOrErr.getError()) {
531 errs() << ToolName << ": " << file << ": " << EC.message()
532 << ".\n";
533 continue;
534 }
535 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
536 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
537 if (OutputFormat == sysv)
538 outs() << o->getFileName() << " (ex " << UA->getFileName()
539 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000540 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000541 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000542 << ")"
543 << " (for architecture " << I->getArchTypeName()
544 << "):\n";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000545 PrintObjectSectionSizes(o);
546 if (OutputFormat == berkeley) {
547 if (MachO) {
548 outs() << UA->getFileName() << "(" << o->getFileName()
549 << ")";
550 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000551 outs() << " (for architecture " << I->getArchTypeName()
552 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000553 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000554 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000555 outs() << o->getFileName() << " (ex " << UA->getFileName()
556 << ")\n";
557 }
558 }
559 }
560 }
561 }
562 }
563 if (!ArchFound) {
564 errs() << ToolName << ": file: " << file
565 << " does not contain architecture" << ArchFlags[i] << ".\n";
566 return;
567 }
568 }
569 return;
570 }
571 // No architecture flags were specified so if this contains a slice that
572 // matches the host architecture dump only that.
573 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000574 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000575 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
576 E = UB->end_objects();
577 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000578 if (HostArchName == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000579 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
580 std::unique_ptr<Archive> UA;
581 if (UO) {
582 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
583 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
584 if (OutputFormat == sysv)
585 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000586 else if (MachO && OutputFormat == darwin) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000587 if (moreThanOneFile)
588 outs() << o->getFileName() << " (for architecture "
589 << I->getArchTypeName() << "):\n";
590 }
591 PrintObjectSectionSizes(o);
592 if (OutputFormat == berkeley) {
593 if (!MachO || moreThanOneFile)
594 outs() << o->getFileName() << " (for architecture "
595 << I->getArchTypeName() << ")";
596 outs() << "\n";
597 }
598 }
Kevin Enderby10769742014-07-01 22:26:31 +0000599 } else if (!I->getAsArchive(UA)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000600 // This is an archive. Iterate over each member and display its
601 // sizes.
602 for (object::Archive::child_iterator i = UA->child_begin(),
603 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000604 i != e; ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000605 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
606 if (std::error_code EC = ChildOrErr.getError()) {
607 errs() << ToolName << ": " << file << ": " << EC.message()
608 << ".\n";
609 continue;
610 }
611 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
612 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
613 if (OutputFormat == sysv)
614 outs() << o->getFileName() << " (ex " << UA->getFileName()
615 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000616 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000617 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
618 << " (for architecture " << I->getArchTypeName()
619 << "):\n";
620 PrintObjectSectionSizes(o);
621 if (OutputFormat == berkeley) {
622 if (MachO)
623 outs() << UA->getFileName() << "(" << o->getFileName()
624 << ")\n";
625 else
626 outs() << o->getFileName() << " (ex " << UA->getFileName()
627 << ")\n";
628 }
629 }
630 }
631 }
632 return;
633 }
634 }
635 }
636 // Either all architectures have been specified or none have been specified
637 // and this does not contain the host architecture so dump all the slices.
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000638 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
639 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
640 E = UB->end_objects();
641 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000642 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000643 std::unique_ptr<Archive> UA;
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000644 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000645 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000646 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000647 if (OutputFormat == sysv)
648 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000649 else if (MachO && OutputFormat == darwin) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000650 if (moreThanOneFile || moreThanOneArch)
651 outs() << o->getFileName() << " (for architecture "
652 << I->getArchTypeName() << "):";
653 outs() << "\n";
654 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000655 PrintObjectSectionSizes(o);
656 if (OutputFormat == berkeley) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000657 if (!MachO || moreThanOneFile || moreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000658 outs() << o->getFileName() << " (for architecture "
659 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000660 outs() << "\n";
661 }
662 }
Kevin Enderby10769742014-07-01 22:26:31 +0000663 } else if (!I->getAsArchive(UA)) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000664 // This is an archive. Iterate over each member and display its sizes.
665 for (object::Archive::child_iterator i = UA->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000666 e = UA->child_end();
667 i != e; ++i) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000668 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
669 if (std::error_code EC = ChildOrErr.getError()) {
670 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
671 continue;
672 }
673 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
674 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
675 if (OutputFormat == sysv)
676 outs() << o->getFileName() << " (ex " << UA->getFileName()
677 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000678 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000679 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000680 << " (for architecture " << I->getArchTypeName() << "):\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000681 PrintObjectSectionSizes(o);
682 if (OutputFormat == berkeley) {
683 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000684 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
685 << " (for architecture " << I->getArchTypeName()
686 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000687 else
688 outs() << o->getFileName() << " (ex " << UA->getFileName()
689 << ")\n";
690 }
691 }
692 }
693 }
694 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000695 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000696 if (!checkMachOAndArchFlags(o, file))
697 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000698 if (OutputFormat == sysv)
699 outs() << o->getFileName() << " :\n";
700 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000701 if (OutputFormat == berkeley) {
702 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
703 if (!MachO || moreThanOneFile)
704 outs() << o->getFileName();
705 outs() << "\n";
706 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000707 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000708 errs() << ToolName << ": " << file << ": "
709 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000710 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000711 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000712 if (OutputFormat == sysv)
713 outs() << "\n";
714}
715
716int main(int argc, char **argv) {
717 // Print a stack trace if we signal out.
718 sys::PrintStackTraceOnErrorSignal();
719 PrettyStackTraceProgram X(argc, argv);
720
Kevin Enderby10769742014-07-01 22:26:31 +0000721 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000722 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
723
724 ToolName = argv[0];
725 if (OutputFormatShort.getNumOccurrences())
726 OutputFormat = OutputFormatShort;
727 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000728 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000729
Kevin Enderby10769742014-07-01 22:26:31 +0000730 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000731 if (ArchFlags[i] == "all") {
732 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000733 } else {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000734 Triple T = MachOObjectFile::getArch(ArchFlags[i]);
Kevin Enderby10769742014-07-01 22:26:31 +0000735 if (T.getArch() == Triple::UnknownArch) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000736 outs() << ToolName << ": for the -arch option: Unknown architecture "
737 << "named '" << ArchFlags[i] << "'";
738 return 1;
739 }
740 }
741 }
742
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000743 if (InputFilenames.size() == 0)
744 InputFilenames.push_back("a.out");
745
Kevin Enderby246a4602014-06-17 17:54:13 +0000746 moreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000747 std::for_each(InputFilenames.begin(), InputFilenames.end(),
748 PrintFileSectionSizes);
749
750 return 0;
751}