blob: 069cc621f615726962861655a2eca4ddb4a9bdde [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 Enderby7a969422015-11-05 19:24:56 +0000431 if (i->getError()) {
432 errs() << ToolName << ": " << file << ": " << i->getError().message()
433 << ".\n";
434 exit(1);
435 }
436 auto &c = i->get();
437 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = c.getAsBinary();
Rafael Espindolaae460022014-06-16 16:08:36 +0000438 if (std::error_code EC = ChildOrErr.getError()) {
439 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000440 continue;
441 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000442 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
Kevin Enderby246a4602014-06-17 17:54:13 +0000443 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000444 if (!checkMachOAndArchFlags(o, file))
445 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000446 if (OutputFormat == sysv)
Kevin Enderby10769742014-07-01 22:26:31 +0000447 outs() << o->getFileName() << " (ex " << a->getFileName() << "):\n";
448 else if (MachO && OutputFormat == darwin)
449 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000450 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000451 if (OutputFormat == berkeley) {
452 if (MachO)
453 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
454 else
455 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
456 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000457 }
458 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000459 } else if (MachOUniversalBinary *UB =
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000460 dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000461 // If we have a list of architecture flags specified dump only those.
462 if (!ArchAll && ArchFlags.size() != 0) {
463 // Look for a slice in the universal binary that matches each ArchFlag.
464 bool ArchFound;
Kevin Enderby10769742014-07-01 22:26:31 +0000465 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000466 ArchFound = false;
467 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
468 E = UB->end_objects();
469 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000470 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000471 ArchFound = true;
472 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000473 if (UO) {
474 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
475 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
476 if (OutputFormat == sysv)
477 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000478 else if (MachO && OutputFormat == darwin) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000479 if (moreThanOneFile || ArchFlags.size() > 1)
480 outs() << o->getFileName() << " (for architecture "
481 << I->getArchTypeName() << "): \n";
482 }
483 PrintObjectSectionSizes(o);
484 if (OutputFormat == berkeley) {
485 if (!MachO || moreThanOneFile || ArchFlags.size() > 1)
486 outs() << o->getFileName() << " (for architecture "
487 << I->getArchTypeName() << ")";
488 outs() << "\n";
489 }
490 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000491 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
492 I->getAsArchive()) {
493 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000494 // This is an archive. Iterate over each member and display its
Kevin Enderby10769742014-07-01 22:26:31 +0000495 // sizes.
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000496 for (object::Archive::child_iterator i = UA->child_begin(),
497 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000498 i != e; ++i) {
Kevin Enderby7a969422015-11-05 19:24:56 +0000499 if (std::error_code EC = i->getError()) {
500 errs() << ToolName << ": " << file << ": " << EC.message()
501 << ".\n";
502 exit(1);
503 }
504 auto &c = i->get();
505 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = c.getAsBinary();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000506 if (std::error_code EC = ChildOrErr.getError()) {
507 errs() << ToolName << ": " << file << ": " << EC.message()
508 << ".\n";
509 continue;
510 }
511 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
512 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
513 if (OutputFormat == sysv)
514 outs() << o->getFileName() << " (ex " << UA->getFileName()
515 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000516 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000517 outs() << UA->getFileName() << "(" << o->getFileName()
Kevin Enderby10769742014-07-01 22:26:31 +0000518 << ")"
519 << " (for architecture " << I->getArchTypeName()
520 << "):\n";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000521 PrintObjectSectionSizes(o);
522 if (OutputFormat == berkeley) {
523 if (MachO) {
524 outs() << UA->getFileName() << "(" << o->getFileName()
525 << ")";
526 if (ArchFlags.size() > 1)
Kevin Enderby10769742014-07-01 22:26:31 +0000527 outs() << " (for architecture " << I->getArchTypeName()
528 << ")";
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000529 outs() << "\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000530 } else
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000531 outs() << o->getFileName() << " (ex " << UA->getFileName()
532 << ")\n";
533 }
534 }
535 }
536 }
537 }
538 }
539 if (!ArchFound) {
540 errs() << ToolName << ": file: " << file
541 << " does not contain architecture" << ArchFlags[i] << ".\n";
542 return;
543 }
544 }
545 return;
546 }
547 // No architecture flags were specified so if this contains a slice that
548 // matches the host architecture dump only that.
549 if (!ArchAll) {
Kevin Enderby10769742014-07-01 22:26:31 +0000550 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000551 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
552 E = UB->end_objects();
553 I != E; ++I) {
Kevin Enderby10769742014-07-01 22:26:31 +0000554 if (HostArchName == I->getArchTypeName()) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000555 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000556 if (UO) {
557 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
558 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
559 if (OutputFormat == sysv)
560 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000561 else if (MachO && OutputFormat == darwin) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000562 if (moreThanOneFile)
563 outs() << o->getFileName() << " (for architecture "
564 << I->getArchTypeName() << "):\n";
565 }
566 PrintObjectSectionSizes(o);
567 if (OutputFormat == berkeley) {
568 if (!MachO || moreThanOneFile)
569 outs() << o->getFileName() << " (for architecture "
570 << I->getArchTypeName() << ")";
571 outs() << "\n";
572 }
573 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000574 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
575 I->getAsArchive()) {
576 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000577 // This is an archive. Iterate over each member and display its
578 // sizes.
579 for (object::Archive::child_iterator i = UA->child_begin(),
580 e = UA->child_end();
Kevin Enderby10769742014-07-01 22:26:31 +0000581 i != e; ++i) {
Kevin Enderby7a969422015-11-05 19:24:56 +0000582 if (std::error_code EC = i->getError()) {
583 errs() << ToolName << ": " << file << ": " << EC.message()
584 << ".\n";
585 exit(1);
586 }
587 auto &c = i->get();
588 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = c.getAsBinary();
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000589 if (std::error_code EC = ChildOrErr.getError()) {
590 errs() << ToolName << ": " << file << ": " << EC.message()
591 << ".\n";
592 continue;
593 }
594 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
595 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
596 if (OutputFormat == sysv)
597 outs() << o->getFileName() << " (ex " << UA->getFileName()
598 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000599 else if (MachO && OutputFormat == darwin)
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000600 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
601 << " (for architecture " << I->getArchTypeName()
602 << "):\n";
603 PrintObjectSectionSizes(o);
604 if (OutputFormat == berkeley) {
605 if (MachO)
606 outs() << UA->getFileName() << "(" << o->getFileName()
607 << ")\n";
608 else
609 outs() << o->getFileName() << " (ex " << UA->getFileName()
610 << ")\n";
611 }
612 }
613 }
614 }
615 return;
616 }
617 }
618 }
619 // Either all architectures have been specified or none have been specified
620 // and this does not contain the host architecture so dump all the slices.
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000621 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
622 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
623 E = UB->end_objects();
624 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000625 ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000626 if (UO) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000627 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000628 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000629 if (OutputFormat == sysv)
630 outs() << o->getFileName() << " :\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000631 else if (MachO && OutputFormat == darwin) {
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000632 if (moreThanOneFile || moreThanOneArch)
633 outs() << o->getFileName() << " (for architecture "
634 << I->getArchTypeName() << "):";
635 outs() << "\n";
636 }
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000637 PrintObjectSectionSizes(o);
638 if (OutputFormat == berkeley) {
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000639 if (!MachO || moreThanOneFile || moreThanOneArch)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000640 outs() << o->getFileName() << " (for architecture "
641 << I->getArchTypeName() << ")";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000642 outs() << "\n";
643 }
644 }
Rafael Espindola0bfe8282014-12-09 21:05:36 +0000645 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
646 I->getAsArchive()) {
647 std::unique_ptr<Archive> &UA = *AOrErr;
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000648 // This is an archive. Iterate over each member and display its sizes.
649 for (object::Archive::child_iterator i = UA->child_begin(),
Kevin Enderby10769742014-07-01 22:26:31 +0000650 e = UA->child_end();
651 i != e; ++i) {
Kevin Enderby7a969422015-11-05 19:24:56 +0000652 if (std::error_code EC = i->getError()) {
653 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
654 exit(1);
655 }
656 auto &c = i->get();
657 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = c.getAsBinary();
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000658 if (std::error_code EC = ChildOrErr.getError()) {
659 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
660 continue;
661 }
662 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
663 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
664 if (OutputFormat == sysv)
665 outs() << o->getFileName() << " (ex " << UA->getFileName()
666 << "):\n";
Kevin Enderby10769742014-07-01 22:26:31 +0000667 else if (MachO && OutputFormat == darwin)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000668 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
Kevin Enderby10769742014-07-01 22:26:31 +0000669 << " (for architecture " << I->getArchTypeName() << "):\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000670 PrintObjectSectionSizes(o);
671 if (OutputFormat == berkeley) {
672 if (MachO)
Kevin Enderby1983fcf2014-06-19 22:03:18 +0000673 outs() << UA->getFileName() << "(" << o->getFileName() << ")"
674 << " (for architecture " << I->getArchTypeName()
675 << ")\n";
Kevin Enderby4b8fc282014-06-18 22:04:40 +0000676 else
677 outs() << o->getFileName() << " (ex " << UA->getFileName()
678 << ")\n";
679 }
680 }
681 }
682 }
683 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000684 } else if (ObjectFile *o = dyn_cast<ObjectFile>(&Bin)) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000685 if (!checkMachOAndArchFlags(o, file))
686 return;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000687 if (OutputFormat == sysv)
688 outs() << o->getFileName() << " :\n";
689 PrintObjectSectionSizes(o);
Kevin Enderby246a4602014-06-17 17:54:13 +0000690 if (OutputFormat == berkeley) {
691 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
692 if (!MachO || moreThanOneFile)
693 outs() << o->getFileName();
694 outs() << "\n";
695 }
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000696 } else {
Kevin Enderby10769742014-07-01 22:26:31 +0000697 errs() << ToolName << ": " << file << ": "
698 << "Unrecognized file type.\n";
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000699 }
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000700 // System V adds an extra newline at the end of each file.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000701 if (OutputFormat == sysv)
702 outs() << "\n";
703}
704
705int main(int argc, char **argv) {
706 // Print a stack trace if we signal out.
707 sys::PrintStackTraceOnErrorSignal();
708 PrettyStackTraceProgram X(argc, argv);
709
Kevin Enderby10769742014-07-01 22:26:31 +0000710 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000711 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
712
713 ToolName = argv[0];
714 if (OutputFormatShort.getNumOccurrences())
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000715 OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000716 if (RadixShort.getNumOccurrences())
Michael J. Spencercc5f8d42011-09-29 00:59:18 +0000717 Radix = RadixShort;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000718
Kevin Enderby10769742014-07-01 22:26:31 +0000719 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000720 if (ArchFlags[i] == "all") {
721 ArchAll = true;
Kevin Enderby10769742014-07-01 22:26:31 +0000722 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +0000723 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
Kevin Enderbyafef4c92014-07-01 17:19:10 +0000724 outs() << ToolName << ": for the -arch option: Unknown architecture "
725 << "named '" << ArchFlags[i] << "'";
726 return 1;
727 }
728 }
729 }
730
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000731 if (InputFilenames.size() == 0)
732 InputFilenames.push_back("a.out");
733
Kevin Enderby246a4602014-06-17 17:54:13 +0000734 moreThanOneFile = InputFilenames.size() > 1;
Michael J. Spencerc4ad4662011-09-28 20:57:46 +0000735 std::for_each(InputFilenames.begin(), InputFilenames.end(),
736 PrintFileSectionSizes);
737
738 return 0;
739}