blob: bb299884f25ef4d050ade0df4180c3c9202cef3c [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"
Kevin Enderby246a4602014-06-17 17:54:13 +000018#include "llvm/Object/MachO.h"
Kevin Enderby4b8fc282014-06-18 22:04:40 +000019#include "llvm/Object/MachOUniversal.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/Object/ObjectFile.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>
Eugene Zelenkoffec81c2015-11-04 22:32:32 +000033
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000034using namespace llvm;
35using namespace object;
36
Kevin Enderby10769742014-07-01 22:26:31 +000037enum OutputFormatTy { berkeley, sysv, darwin };
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000038static cl::opt<OutputFormatTy>
Kevin Enderby10769742014-07-01 22:26:31 +000039OutputFormat("format", cl::desc("Specify output format"),
40 cl::values(clEnumVal(sysv, "System V format"),
41 clEnumVal(berkeley, "Berkeley format"),
42 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
43 cl::init(berkeley));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000044
Kevin Enderby10769742014-07-01 22:26:31 +000045static cl::opt<OutputFormatTy> OutputFormatShort(
46 cl::desc("Specify output format"),
47 cl::values(clEnumValN(sysv, "A", "System V format"),
48 clEnumValN(berkeley, "B", "Berkeley format"),
49 clEnumValN(darwin, "m", "Darwin -m format"), clEnumValEnd),
50 cl::init(berkeley));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000051
Kevin Enderby246a4602014-06-17 17:54:13 +000052static bool berkeleyHeaderPrinted = false;
53static bool moreThanOneFile = false;
54
Kevin Enderby10769742014-07-01 22:26:31 +000055cl::opt<bool>
56DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
57 "to include addresses and offsets."));
Kevin Enderby246a4602014-06-17 17:54:13 +000058
Kevin Enderbyafef4c92014-07-01 17:19:10 +000059static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000060ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
61 cl::ZeroOrMore);
Kevin Enderbyafef4c92014-07-01 17:19:10 +000062bool ArchAll = false;
63
Kevin Enderby10769742014-07-01 22:26:31 +000064enum RadixTy { octal = 8, decimal = 10, hexadecimal = 16 };
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000065static cl::opt<unsigned int>
Kevin Enderby10769742014-07-01 22:26:31 +000066Radix("-radix", cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
67 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000068
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000069static cl::opt<RadixTy>
Kevin Enderby10769742014-07-01 22:26:31 +000070RadixShort(cl::desc("Print size in radix:"),
71 cl::values(clEnumValN(octal, "o", "Print size in octal"),
72 clEnumValN(decimal, "d", "Print size in decimal"),
73 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
74 clEnumValEnd),
75 cl::init(decimal));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000076
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000077static cl::list<std::string>
Kevin Enderby10769742014-07-01 22:26:31 +000078InputFilenames(cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000079
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000080static std::string ToolName;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000081
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000082/// @brief If ec is not success, print the error and return true.
Rafael Espindola4453e42942014-06-13 03:07:50 +000083static bool error(std::error_code ec) {
Kevin Enderby10769742014-07-01 22:26:31 +000084 if (!ec)
85 return false;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000086
87 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
88 outs().flush();
89 return true;
90}
91
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000092/// @brief Get the length of the string that represents @p num in Radix
93/// including the leading 0x or 0 for hexadecimal and octal respectively.
Andrew Trick7dc278d2011-09-29 01:22:31 +000094static size_t getNumLengthAsString(uint64_t num) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000095 APInt conv(64, num);
96 SmallString<32> result;
Michael J. Spencercc5f8d42011-09-29 00:59:18 +000097 conv.toString(result, Radix, false, true);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +000098 return result.size();
99}
100
Eric Christopher572e03a2015-06-19 01:53:21 +0000101/// @brief Return the printing format for the Radix.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000102static const char *getRadixFmt() {
Kevin Enderby246a4602014-06-17 17:54:13 +0000103 switch (Radix) {
104 case octal:
105 return PRIo64;
106 case decimal:
107 return PRIu64;
108 case hexadecimal:
109 return PRIx64;
110 }
111 return nullptr;
112}
113
114/// @brief Print the size of each Mach-O segment and section in @p MachO.
115///
116/// This is when used when @c OutputFormat is darwin and produces the same
117/// output as darwin's size(1) -m output.
118static void PrintDarwinSectionSizes(MachOObjectFile *MachO) {
119 std::string fmtbuf;
120 raw_string_ostream fmt(fmtbuf);
121 const char *radix_fmt = getRadixFmt();
122 if (Radix == hexadecimal)
123 fmt << "0x";
124 fmt << "%" << radix_fmt;
125
Kevin Enderby246a4602014-06-17 17:54:13 +0000126 uint32_t Filetype = MachO->getHeader().filetype;
Kevin Enderby246a4602014-06-17 17:54:13 +0000127
128 uint64_t total = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000129 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000130 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
131 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
132 outs() << "Segment " << Seg.segname << ": "
133 << format(fmt.str().c_str(), Seg.vmsize);
134 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000135 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
136 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000137 outs() << "\n";
138 total += Seg.vmsize;
139 uint64_t sec_total = 0;
140 for (unsigned J = 0; J < Seg.nsects; ++J) {
141 MachO::section_64 Sec = MachO->getSection64(Load, J);
142 if (Filetype == MachO::MH_OBJECT)
143 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
144 << format("%.16s", &Sec.sectname) << "): ";
145 else
146 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
147 outs() << format(fmt.str().c_str(), Sec.size);
148 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000149 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
150 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000151 outs() << "\n";
152 sec_total += Sec.size;
153 }
154 if (Seg.nsects != 0)
155 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000156 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000157 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
158 outs() << "Segment " << Seg.segname << ": "
159 << format(fmt.str().c_str(), Seg.vmsize);
160 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000161 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr) << " fileoff "
162 << Seg.fileoff << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000163 outs() << "\n";
164 total += Seg.vmsize;
165 uint64_t sec_total = 0;
166 for (unsigned J = 0; J < Seg.nsects; ++J) {
167 MachO::section Sec = MachO->getSection(Load, J);
168 if (Filetype == MachO::MH_OBJECT)
169 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
170 << format("%.16s", &Sec.sectname) << "): ";
171 else
172 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
173 outs() << format(fmt.str().c_str(), Sec.size);
174 if (DarwinLongFormat)
Kevin Enderby10769742014-07-01 22:26:31 +0000175 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr) << " offset "
176 << Sec.offset << ")";
Kevin Enderby246a4602014-06-17 17:54:13 +0000177 outs() << "\n";
178 sec_total += Sec.size;
179 }
180 if (Seg.nsects != 0)
181 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
182 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000183 }
184 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
185}
186
187/// @brief Print the summary sizes of the standard Mach-O segments in @p MachO.
188///
189/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
190/// produces the same output as darwin's size(1) default output.
191static void PrintDarwinSegmentSizes(MachOObjectFile *MachO) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000192 uint64_t total_text = 0;
193 uint64_t total_data = 0;
194 uint64_t total_objc = 0;
195 uint64_t total_others = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000196 for (const auto &Load : MachO->load_commands()) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000197 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
198 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
199 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
200 for (unsigned J = 0; J < Seg.nsects; ++J) {
201 MachO::section_64 Sec = MachO->getSection64(Load, J);
202 StringRef SegmentName = StringRef(Sec.segname);
203 if (SegmentName == "__TEXT")
204 total_text += Sec.size;
205 else if (SegmentName == "__DATA")
206 total_data += Sec.size;
207 else if (SegmentName == "__OBJC")
208 total_objc += Sec.size;
209 else
210 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000211 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000212 } else {
213 StringRef SegmentName = StringRef(Seg.segname);
214 if (SegmentName == "__TEXT")
215 total_text += Seg.vmsize;
216 else if (SegmentName == "__DATA")
217 total_data += Seg.vmsize;
218 else if (SegmentName == "__OBJC")
219 total_objc += Seg.vmsize;
220 else
221 total_others += Seg.vmsize;
222 }
Kevin Enderby10769742014-07-01 22:26:31 +0000223 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000224 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
225 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
226 for (unsigned J = 0; J < Seg.nsects; ++J) {
227 MachO::section Sec = MachO->getSection(Load, J);
228 StringRef SegmentName = StringRef(Sec.segname);
229 if (SegmentName == "__TEXT")
230 total_text += Sec.size;
231 else if (SegmentName == "__DATA")
232 total_data += Sec.size;
233 else if (SegmentName == "__OBJC")
234 total_objc += Sec.size;
235 else
236 total_others += Sec.size;
Kevin Enderby10769742014-07-01 22:26:31 +0000237 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000238 } else {
239 StringRef SegmentName = StringRef(Seg.segname);
240 if (SegmentName == "__TEXT")
241 total_text += Seg.vmsize;
242 else if (SegmentName == "__DATA")
243 total_data += Seg.vmsize;
244 else if (SegmentName == "__OBJC")
245 total_objc += Seg.vmsize;
246 else
247 total_others += Seg.vmsize;
248 }
249 }
Kevin Enderby246a4602014-06-17 17:54:13 +0000250 }
251 uint64_t total = total_text + total_data + total_objc + total_others;
252
253 if (!berkeleyHeaderPrinted) {
254 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
255 berkeleyHeaderPrinted = true;
256 }
257 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
258 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
259 << "\t";
260}
261
Alexey Samsonov48803e52014-03-13 14:37:36 +0000262/// @brief Print the size of each section in @p Obj.
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000263///
264/// The format used is determined by @c OutputFormat and @c Radix.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000265static void PrintObjectSectionSizes(ObjectFile *Obj) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000266 uint64_t total = 0;
267 std::string fmtbuf;
268 raw_string_ostream fmt(fmtbuf);
Kevin Enderby246a4602014-06-17 17:54:13 +0000269 const char *radix_fmt = getRadixFmt();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000270
Kevin Enderby246a4602014-06-17 17:54:13 +0000271 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
272 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
273 // let it fall through to OutputFormat berkeley.
274 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
275 if (OutputFormat == darwin && MachO)
276 PrintDarwinSectionSizes(MachO);
277 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
278 // darwin's default berkeley format for Mach-O files.
279 else if (MachO && OutputFormat == berkeley)
280 PrintDarwinSegmentSizes(MachO);
281 else if (OutputFormat == sysv) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000282 // Run two passes over all sections. The first gets the lengths needed for
283 // formatting the output. The second actually does the output.
284 std::size_t max_name_len = strlen("section");
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000285 std::size_t max_size_len = strlen("size");
286 std::size_t max_addr_len = strlen("addr");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000287 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000288 uint64_t size = Section.getSize();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000289 total += size;
290
291 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000292 if (error(Section.getName(name)))
293 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000294 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000295 max_name_len = std::max(max_name_len, name.size());
Andrew Trick7dc278d2011-09-29 01:22:31 +0000296 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
297 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000298 }
299
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000300 // Add extra padding.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000301 max_name_len += 2;
302 max_size_len += 2;
303 max_addr_len += 2;
304
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000305 // Setup header format.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000306 fmt << "%-" << max_name_len << "s "
307 << "%" << max_size_len << "s "
308 << "%" << max_addr_len << "s\n";
309
310 // Print header
Kevin Enderby10769742014-07-01 22:26:31 +0000311 outs() << format(fmt.str().c_str(), static_cast<const char *>("section"),
312 static_cast<const char *>("size"),
313 static_cast<const char *>("addr"));
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000314 fmtbuf.clear();
315
316 // Setup per section format.
317 fmt << "%-" << max_name_len << "s "
318 << "%#" << max_size_len << radix_fmt << " "
319 << "%#" << max_addr_len << radix_fmt << "\n";
320
321 // Print each section.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000322 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000323 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000324 if (error(Section.getName(name)))
325 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000326 uint64_t size = Section.getSize();
327 uint64_t addr = Section.getAddress();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000328 std::string namestr = name;
329
Alexey Samsonov48803e52014-03-13 14:37:36 +0000330 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000331 }
332
333 // Print total.
334 fmtbuf.clear();
335 fmt << "%-" << max_name_len << "s "
336 << "%#" << max_size_len << radix_fmt << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000337 outs() << format(fmt.str().c_str(), static_cast<const char *>("Total"),
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000338 total);
339 } else {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000340 // The Berkeley format does not display individual section sizes. It
341 // displays the cumulative size for each section type.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000342 uint64_t total_text = 0;
343 uint64_t total_data = 0;
344 uint64_t total_bss = 0;
345
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000346 // Make one pass over the section table to calculate sizes.
Alexey Samsonov48803e52014-03-13 14:37:36 +0000347 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000348 uint64_t size = Section.getSize();
349 bool isText = Section.isText();
350 bool isData = Section.isData();
351 bool isBSS = Section.isBSS();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000352 if (isText)
353 total_text += size;
354 else if (isData)
355 total_data += size;
356 else if (isBSS)
357 total_bss += size;
358 }
359
360 total = total_text + total_data + total_bss;
361
Kevin Enderby246a4602014-06-17 17:54:13 +0000362 if (!berkeleyHeaderPrinted) {
363 outs() << " text data bss "
Kevin Enderby10769742014-07-01 22:26:31 +0000364 << (Radix == octal ? "oct" : "dec") << " hex filename\n";
Kevin Enderby246a4602014-06-17 17:54:13 +0000365 berkeleyHeaderPrinted = true;
366 }
367
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000368 // Print result.
369 fmt << "%#7" << radix_fmt << " "
370 << "%#7" << radix_fmt << " "
371 << "%#7" << radix_fmt << " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000372 outs() << format(fmt.str().c_str(), total_text, total_data, total_bss);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000373 fmtbuf.clear();
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000374 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
375 << "%7" PRIx64 " ";
Kevin Enderby10769742014-07-01 22:26:31 +0000376 outs() << format(fmt.str().c_str(), total, total);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000377 }
378}
379
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000380/// @brief Checks to see if the @p o ObjectFile is a Mach-O file and if it is
381/// and there is a list of architecture flags specified then check to
382/// make sure this Mach-O file is one of those architectures or all
383/// architectures was specificed. If not then an error is generated and
384/// this routine returns false. Else it returns true.
385static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
386 if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) {
387 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
388 bool ArchFound = false;
389 MachO::mach_header H;
390 MachO::mach_header_64 H_64;
391 Triple T;
392 if (MachO->is64Bit()) {
393 H_64 = MachO->MachOObjectFile::getHeader64();
394 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
395 } else {
396 H = MachO->MachOObjectFile::getHeader();
397 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
398 }
399 unsigned i;
Kevin Enderby10769742014-07-01 22:26:31 +0000400 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000401 if (ArchFlags[i] == T.getArchName())
402 ArchFound = true;
403 break;
404 }
405 if (!ArchFound) {
406 errs() << ToolName << ": file: " << file
407 << " does not contain architecture: " << ArchFlags[i] << ".\n";
408 return false;
409 }
410 }
411 return true;
412}
413
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000414/// @brief Print the section sizes for @p file. If @p file is an archive, print
415/// the section sizes for each archive member.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000416static void PrintFileSectionSizes(StringRef file) {
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000417
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000418 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000419 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000420 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000421 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000422 return;
423 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000424 Binary &Bin = *BinaryOrErr.get().getBinary();
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000425
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000426 if (Archive *a = dyn_cast<Archive>(&Bin)) {
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000427 // This is an archive. Iterate over each member and display its sizes.
Rafael Espindola23a97502014-01-21 16:09:45 +0000428 for (object::Archive::child_iterator i = a->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000429 e = a->child_end();
430 i != e; ++i) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000431 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
Rafael Espindolaae460022014-06-16 16:08:36 +0000432 if (std::error_code EC = ChildOrErr.getError()) {
433 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000434 continue;
435 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000436 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000437 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000438 if (!checkMachOAndArchFlags(o, file))
439 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000440 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000441 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
442 else if (MachO && OutputFormat == darwin)
443 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000444 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000445 if (OutputFormat == berkeley) {
446 if (MachO)
447 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
448 else
449 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
450 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000451 }
452 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000453 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000454 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000455 // If we have a list of architecture flags specified dump only those.
456 if (!ArchAll && ArchFlags.size() != 0) {
457 // Look for a slice in the universal binary that matches each ArchFlag.
458 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000459 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000460 ArchFound = false;
461 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
462 E = UB->end_objects();
463 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000464 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000465 ArchFound = true;
466 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000467 if (UO) {
468 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
469 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
470 if (OutputFormat == sysv)
471 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000472 else if (MachO && OutputFormat == darwin) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000473 if (moreThanOneFile || ArchFlags.size() > 1)
474 outs() << o->getFileName() << " (for architecture "
475 << I->getArchTypeName() << "): \n";
476 }
477 PrintObjectSectionSizes(o);
478 if (OutputFormat == berkeley) {
479 if (!MachO || moreThanOneFile || ArchFlags.size() > 1)
480 outs() << o->getFileName() << " (for architecture "
481 << I->getArchTypeName() << ")";
482 outs() << "\n";
483 }
484 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000485 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
486 I->getAsArchive()) {
487 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000488 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000489 // sizes.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000490 for (object::Archive::child_iterator i = UA->child_begin(),
491 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000492 i != e; ++i) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000493 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000494 if (std::error_code EC = ChildOrErr.getError()) {
495 errs() << ToolName << ": " << file << ": " << EC.message()
496 << ".\n";
497 continue;
498 }
499 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
500 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
501 if (OutputFormat == sysv)
502 outs() << o->getFileName() << " (ex " << UA->getFileName()
503 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000504 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000505 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000506 << ")"
507 << " (for architecture " << I->getArchTypeName()
508 << "):\n";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000509 PrintObjectSectionSizes(o);
510 if (OutputFormat == berkeley) {
511 if (MachO) {
512 outs() << UA->getFileName() << "(" << o->getFileName()
513 << ")";
514 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000515 outs() << " (for architecture " << I->getArchTypeName()
516 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000517 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000518 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000519 outs() << o->getFileName() << " (ex " << UA->getFileName()
520 << ")\n";
521 }
522 }
523 }
524 }
525 }
526 }
527 if (!ArchFound) {
528 errs() << ToolName << ": file: " << file
529 << " does not contain architecture" << ArchFlags[i] << ".\n";
530 return;
531 }
532 }
533 return;
534 }
535 // No architecture flags were specified so if this contains a slice that
536 // matches the host architecture dump only that.
537 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000538 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000539 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
540 E = UB->end_objects();
541 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000542 if (HostArchName == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000543 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000544 if (UO) {
545 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
546 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
547 if (OutputFormat == sysv)
548 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000549 else if (MachO && OutputFormat == darwin) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000550 if (moreThanOneFile)
551 outs() << o->getFileName() << " (for architecture "
552 << I->getArchTypeName() << "):\n";
553 }
554 PrintObjectSectionSizes(o);
555 if (OutputFormat == berkeley) {
556 if (!MachO || moreThanOneFile)
557 outs() << o->getFileName() << " (for architecture "
558 << I->getArchTypeName() << ")";
559 outs() << "\n";
560 }
561 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000562 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
563 I->getAsArchive()) {
564 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000565 // This is an archive. Iterate over each member and display its
566 // sizes.
567 for (object::Archive::child_iterator i = UA->child_begin(),
568 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000569 i != e; ++i) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000570 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000571 if (std::error_code EC = ChildOrErr.getError()) {
572 errs() << ToolName << ": " << file << ": " << EC.message()
573 << ".\n";
574 continue;
575 }
576 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
577 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
578 if (OutputFormat == sysv)
579 outs() << o->getFileName() << " (ex " << UA->getFileName()
580 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000581 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000582 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
583 << " (for architecture " << I->getArchTypeName()
584 << "):\n";
585 PrintObjectSectionSizes(o);
586 if (OutputFormat == berkeley) {
587 if (MachO)
588 outs() << UA->getFileName() << "(" << o->getFileName()
589 << ")\n";
590 else
591 outs() << o->getFileName() << " (ex " << UA->getFileName()
592 << ")\n";
593 }
594 }
595 }
596 }
597 return;
598 }
599 }
600 }
601 // Either all architectures have been specified or none have been specified
602 // and this does not contain the host architecture so dump all the slices.
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000603 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
604 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
605 E = UB->end_objects();
606 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000607 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000608 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000609 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000610 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000611 if (OutputFormat == sysv)
612 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000613 else if (MachO && OutputFormat == darwin) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000614 if (moreThanOneFile || moreThanOneArch)
615 outs() << o->getFileName() << " (for architecture "
616 << I->getArchTypeName() << "):";
617 outs() << "\n";
618 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000619 PrintObjectSectionSizes(o);
620 if (OutputFormat == berkeley) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000621 if (!MachO || moreThanOneFile || moreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000622 outs() << o->getFileName() << " (for architecture "
623 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000624 outs() << "\n";
625 }
626 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000627 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
628 I->getAsArchive()) {
629 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000630 // This is an archive. Iterate over each member and display its sizes.
631 for (object::Archive::child_iterator i = UA->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000632 e = UA->child_end();
633 i != e; ++i) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000634 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000635 if (std::error_code EC = ChildOrErr.getError()) {
636 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
637 continue;
638 }
639 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
640 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
641 if (OutputFormat == sysv)
642 outs() << o->getFileName() << " (ex " << UA->getFileName()
643 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000644 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000645 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000646 << " (for architecture " << I->getArchTypeName() << "):\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000647 PrintObjectSectionSizes(o);
648 if (OutputFormat == berkeley) {
649 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000650 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
651 << " (for architecture " << I->getArchTypeName()
652 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000653 else
654 outs() << o->getFileName() << " (ex " << UA->getFileName()
655 << ")\n";
656 }
657 }
658 }
659 }
660 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000661 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000662 if (!checkMachOAndArchFlags(o, file))
663 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000664 if (OutputFormat == sysv)
665 outs() << o->getFileName() << " :\n";
666 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000667 if (OutputFormat == berkeley) {
668 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
669 if (!MachO || moreThanOneFile)
670 outs() << o->getFileName();
671 outs() << "\n";
672 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000673 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000674 errs() << ToolName << ": " << file << ": "
675 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000676 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000677 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000678 if (OutputFormat == sysv)
679 outs() << "\n";
680}
681
682int main(int argc, char **argv) {
683 // Print a stack trace if we signal out.
684 sys::PrintStackTraceOnErrorSignal();
685 PrettyStackTraceProgram X(argc, argv);
686
Kevin Enderby10769742014-07-01 22:26:31 +0000687 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000688 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
689
690 ToolName = argv[0];
691 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000692 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000693 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000694 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000695
Kevin Enderby10769742014-07-01 22:26:31 +0000696 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000697 if (ArchFlags[i] == "all") {
698 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000699 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000700 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000701 outs() << ToolName << ": for the -arch option: Unknown architecture "
702 << "named '" << ArchFlags[i] << "'";
703 return 1;
704 }
705 }
706 }
707
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000708 if (InputFilenames.size() == 0)
709 InputFilenames.push_back("a.out");
710
Kevin Enderby246a4602014-06-17 17:54:13 +0000711 moreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000712 std::for_each(InputFilenames.begin(), InputFilenames.end(),
713 PrintFileSectionSizes);
714
715 return 0;
716}