1 //===-- llvm-size.cpp - Print the size of each object section -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Object/MachOUniversal.h"
21 #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"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/raw_ostream.h"
32 #include <system_error>
34 using namespace object;
36 enum OutputFormatTy {berkeley, sysv, darwin};
37 static cl::opt<OutputFormatTy>
38 OutputFormat("format",
39 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),
45 static cl::opt<OutputFormatTy>
46 OutputFormatShort(cl::desc("Specify output format"),
47 cl::values(clEnumValN(sysv, "A", "System V format"),
48 clEnumValN(berkeley, "B", "Berkeley format"),
52 static bool berkeleyHeaderPrinted = false;
53 static bool moreThanOneFile = false;
55 cl::opt<bool> DarwinLongFormat("l",
56 cl::desc("When format is darwin, use long format "
57 "to include addresses and offsets."));
59 enum RadixTy {octal = 8, decimal = 10, hexadecimal = 16};
60 static cl::opt<unsigned int>
62 cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
65 static cl::opt<RadixTy>
66 RadixShort(cl::desc("Print size in radix:"),
67 cl::values(clEnumValN(octal, "o", "Print size in octal"),
68 clEnumValN(decimal, "d", "Print size in decimal"),
69 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
73 static cl::list<std::string>
74 InputFilenames(cl::Positional, cl::desc("<input files>"),
77 static std::string ToolName;
79 /// @brief If ec is not success, print the error and return true.
80 static bool error(std::error_code ec) {
81 if (!ec) return false;
83 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
88 /// @brief Get the length of the string that represents @p num in Radix
89 /// including the leading 0x or 0 for hexadecimal and octal respectively.
90 static size_t getNumLengthAsString(uint64_t num) {
92 SmallString<32> result;
93 conv.toString(result, Radix, false, true);
97 /// @brief Return the the printing format for the Radix.
98 static const char * getRadixFmt(void) {
110 /// @brief Print the size of each Mach-O segment and section in @p MachO.
112 /// This is when used when @c OutputFormat is darwin and produces the same
113 /// output as darwin's size(1) -m output.
114 static void PrintDarwinSectionSizes(MachOObjectFile *MachO) {
116 raw_string_ostream fmt(fmtbuf);
117 const char *radix_fmt = getRadixFmt();
118 if (Radix == hexadecimal)
120 fmt << "%" << radix_fmt;
122 uint32_t LoadCommandCount = MachO->getHeader().ncmds;
123 uint32_t Filetype = MachO->getHeader().filetype;
124 MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
127 for (unsigned I = 0; ; ++I) {
128 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
129 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
130 outs() << "Segment " << Seg.segname << ": "
131 << format(fmt.str().c_str(), Seg.vmsize);
132 if (DarwinLongFormat)
133 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr)
134 << " fileoff " << Seg.fileoff << ")";
137 uint64_t sec_total = 0;
138 for (unsigned J = 0; J < Seg.nsects; ++J) {
139 MachO::section_64 Sec = MachO->getSection64(Load, J);
140 if (Filetype == MachO::MH_OBJECT)
141 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
142 << format("%.16s", &Sec.sectname) << "): ";
144 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
145 outs() << format(fmt.str().c_str(), Sec.size);
146 if (DarwinLongFormat)
147 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr)
148 << " offset " << Sec.offset << ")";
150 sec_total += Sec.size;
153 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
155 else if (Load.C.cmd == MachO::LC_SEGMENT) {
156 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
157 outs() << "Segment " << Seg.segname << ": "
158 << format(fmt.str().c_str(), Seg.vmsize);
159 if (DarwinLongFormat)
160 outs() << " (vmaddr 0x" << format("%" PRIx64, Seg.vmaddr)
161 << " fileoff " << Seg.fileoff << ")";
164 uint64_t sec_total = 0;
165 for (unsigned J = 0; J < Seg.nsects; ++J) {
166 MachO::section Sec = MachO->getSection(Load, J);
167 if (Filetype == MachO::MH_OBJECT)
168 outs() << "\tSection (" << format("%.16s", &Sec.segname) << ", "
169 << format("%.16s", &Sec.sectname) << "): ";
171 outs() << "\tSection " << format("%.16s", &Sec.sectname) << ": ";
172 outs() << format(fmt.str().c_str(), Sec.size);
173 if (DarwinLongFormat)
174 outs() << " (addr 0x" << format("%" PRIx64, Sec.addr)
175 << " offset " << Sec.offset << ")";
177 sec_total += Sec.size;
180 outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
182 if (I == LoadCommandCount - 1)
185 Load = MachO->getNextLoadCommandInfo(Load);
187 outs() << "total " << format(fmt.str().c_str(), total) << "\n";
190 /// @brief Print the summary sizes of the standard Mach-O segments in @p MachO.
192 /// This is when used when @c OutputFormat is berkeley with a Mach-O file and
193 /// produces the same output as darwin's size(1) default output.
194 static void PrintDarwinSegmentSizes(MachOObjectFile *MachO) {
195 uint32_t LoadCommandCount = MachO->getHeader().ncmds;
196 MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
198 uint64_t total_text = 0;
199 uint64_t total_data = 0;
200 uint64_t total_objc = 0;
201 uint64_t total_others = 0;
202 for (unsigned I = 0; ; ++I) {
203 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
204 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
205 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
206 for (unsigned J = 0; J < Seg.nsects; ++J) {
207 MachO::section_64 Sec = MachO->getSection64(Load, J);
208 StringRef SegmentName = StringRef(Sec.segname);
209 if (SegmentName == "__TEXT")
210 total_text += Sec.size;
211 else if (SegmentName == "__DATA")
212 total_data += Sec.size;
213 else if (SegmentName == "__OBJC")
214 total_objc += Sec.size;
216 total_others += Sec.size;
219 StringRef SegmentName = StringRef(Seg.segname);
220 if (SegmentName == "__TEXT")
221 total_text += Seg.vmsize;
222 else if (SegmentName == "__DATA")
223 total_data += Seg.vmsize;
224 else if (SegmentName == "__OBJC")
225 total_objc += Seg.vmsize;
227 total_others += Seg.vmsize;
230 else if (Load.C.cmd == MachO::LC_SEGMENT) {
231 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Load);
232 if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
233 for (unsigned J = 0; J < Seg.nsects; ++J) {
234 MachO::section Sec = MachO->getSection(Load, J);
235 StringRef SegmentName = StringRef(Sec.segname);
236 if (SegmentName == "__TEXT")
237 total_text += Sec.size;
238 else if (SegmentName == "__DATA")
239 total_data += Sec.size;
240 else if (SegmentName == "__OBJC")
241 total_objc += Sec.size;
243 total_others += Sec.size;
246 StringRef SegmentName = StringRef(Seg.segname);
247 if (SegmentName == "__TEXT")
248 total_text += Seg.vmsize;
249 else if (SegmentName == "__DATA")
250 total_data += Seg.vmsize;
251 else if (SegmentName == "__OBJC")
252 total_objc += Seg.vmsize;
254 total_others += Seg.vmsize;
257 if (I == LoadCommandCount - 1)
260 Load = MachO->getNextLoadCommandInfo(Load);
262 uint64_t total = total_text + total_data + total_objc + total_others;
264 if (!berkeleyHeaderPrinted) {
265 outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n";
266 berkeleyHeaderPrinted = true;
268 outs() << total_text << "\t" << total_data << "\t" << total_objc << "\t"
269 << total_others << "\t" << total << "\t" << format("%" PRIx64, total)
273 /// @brief Print the size of each section in @p Obj.
275 /// The format used is determined by @c OutputFormat and @c Radix.
276 static void PrintObjectSectionSizes(ObjectFile *Obj) {
279 raw_string_ostream fmt(fmtbuf);
280 const char *radix_fmt = getRadixFmt();
282 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
283 // size(1) -m output, else if OutputFormat is darwin and not a Mach-O object
284 // let it fall through to OutputFormat berkeley.
285 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
286 if (OutputFormat == darwin && MachO)
287 PrintDarwinSectionSizes(MachO);
288 // If we have a MachOObjectFile and the OutputFormat is berkeley print as
289 // darwin's default berkeley format for Mach-O files.
290 else if (MachO && OutputFormat == berkeley)
291 PrintDarwinSegmentSizes(MachO);
292 else if (OutputFormat == sysv) {
293 // Run two passes over all sections. The first gets the lengths needed for
294 // formatting the output. The second actually does the output.
295 std::size_t max_name_len = strlen("section");
296 std::size_t max_size_len = strlen("size");
297 std::size_t max_addr_len = strlen("addr");
298 for (const SectionRef &Section : Obj->sections()) {
300 if (error(Section.getSize(size)))
306 if (error(Section.getName(name)))
308 if (error(Section.getAddress(addr)))
310 max_name_len = std::max(max_name_len, name.size());
311 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
312 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
315 // Add extra padding.
320 // Setup header format.
321 fmt << "%-" << max_name_len << "s "
322 << "%" << max_size_len << "s "
323 << "%" << max_addr_len << "s\n";
326 outs() << format(fmt.str().c_str(),
327 static_cast<const char*>("section"),
328 static_cast<const char*>("size"),
329 static_cast<const char*>("addr"));
332 // Setup per section format.
333 fmt << "%-" << max_name_len << "s "
334 << "%#" << max_size_len << radix_fmt << " "
335 << "%#" << max_addr_len << radix_fmt << "\n";
337 // Print each section.
338 for (const SectionRef &Section : Obj->sections()) {
342 if (error(Section.getName(name)))
344 if (error(Section.getSize(size)))
346 if (error(Section.getAddress(addr)))
348 std::string namestr = name;
350 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
355 fmt << "%-" << max_name_len << "s "
356 << "%#" << max_size_len << radix_fmt << "\n";
357 outs() << format(fmt.str().c_str(),
358 static_cast<const char*>("Total"),
361 // The Berkeley format does not display individual section sizes. It
362 // displays the cumulative size for each section type.
363 uint64_t total_text = 0;
364 uint64_t total_data = 0;
365 uint64_t total_bss = 0;
367 // Make one pass over the section table to calculate sizes.
368 for (const SectionRef &Section : Obj->sections()) {
373 if (error(Section.getSize(size)))
375 if (error(Section.isText(isText)))
377 if (error(Section.isData(isData)))
379 if (error(Section.isBSS(isBSS)))
389 total = total_text + total_data + total_bss;
391 if (!berkeleyHeaderPrinted) {
392 outs() << " text data bss "
393 << (Radix == octal ? "oct" : "dec")
394 << " hex filename\n";
395 berkeleyHeaderPrinted = true;
399 fmt << "%#7" << radix_fmt << " "
400 << "%#7" << radix_fmt << " "
401 << "%#7" << radix_fmt << " ";
402 outs() << format(fmt.str().c_str(),
407 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
409 outs() << format(fmt.str().c_str(),
415 /// @brief Print the section sizes for @p file. If @p file is an archive, print
416 /// the section sizes for each archive member.
417 static void PrintFileSectionSizes(StringRef file) {
418 // If file is not stdin, check that it exists.
421 if (sys::fs::exists(file, exists) || !exists) {
422 errs() << ToolName << ": '" << file << "': " << "No such file\n";
427 // Attempt to open the binary.
428 ErrorOr<Binary *> BinaryOrErr = createBinary(file);
429 if (std::error_code EC = BinaryOrErr.getError()) {
430 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
433 std::unique_ptr<Binary> binary(BinaryOrErr.get());
435 if (Archive *a = dyn_cast<Archive>(binary.get())) {
436 // This is an archive. Iterate over each member and display its sizes.
437 for (object::Archive::child_iterator i = a->child_begin(),
438 e = a->child_end(); i != e; ++i) {
439 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
440 if (std::error_code EC = ChildOrErr.getError()) {
441 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
444 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
445 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
446 if (OutputFormat == sysv)
447 outs() << o->getFileName() << " (ex " << a->getFileName()
449 else if(MachO && OutputFormat == darwin)
450 outs() << a->getFileName() << "(" << o->getFileName() << "):\n";
451 PrintObjectSectionSizes(o);
452 if (OutputFormat == berkeley) {
454 outs() << a->getFileName() << "(" << o->getFileName() << ")\n";
456 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
460 } else if (MachOUniversalBinary *UB =
461 dyn_cast<MachOUniversalBinary>(binary.get())) {
462 // This is a Mach-O universal binary. Iterate over each object and
463 // display its sizes.
464 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
465 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
466 E = UB->end_objects();
468 std::unique_ptr<ObjectFile> UO;
469 std::unique_ptr<Archive> UA;
470 if (!I->getAsObjectFile(UO)) {
471 if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
472 if (OutputFormat == sysv)
473 outs() << o->getFileName() << " :\n";
474 PrintObjectSectionSizes(o);
475 if (OutputFormat == berkeley) {
476 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
477 if (!MachO || moreThanOneFile || moreThanOneArch)
478 outs() << o->getFileName();
483 else if (!I->getAsArchive(UA)) {
484 // This is an archive. Iterate over each member and display its sizes.
485 for (object::Archive::child_iterator i = UA->child_begin(),
486 e = UA->child_end(); i != e; ++i) {
487 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
488 if (std::error_code EC = ChildOrErr.getError()) {
489 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
492 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) {
493 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
494 if (OutputFormat == sysv)
495 outs() << o->getFileName() << " (ex " << UA->getFileName()
497 else if(MachO && OutputFormat == darwin)
498 outs() << UA->getFileName() << "(" << o->getFileName() << "):\n";
499 PrintObjectSectionSizes(o);
500 if (OutputFormat == berkeley) {
502 outs() << UA->getFileName() << "(" << o->getFileName() << ")\n";
504 outs() << o->getFileName() << " (ex " << UA->getFileName()
511 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
512 if (OutputFormat == sysv)
513 outs() << o->getFileName() << " :\n";
514 PrintObjectSectionSizes(o);
515 if (OutputFormat == berkeley) {
516 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
517 if (!MachO || moreThanOneFile)
518 outs() << o->getFileName();
522 errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n";
524 // System V adds an extra newline at the end of each file.
525 if (OutputFormat == sysv)
529 int main(int argc, char **argv) {
530 // Print a stack trace if we signal out.
531 sys::PrintStackTraceOnErrorSignal();
532 PrettyStackTraceProgram X(argc, argv);
534 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
535 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
538 if (OutputFormatShort.getNumOccurrences())
539 OutputFormat = OutputFormatShort;
540 if (RadixShort.getNumOccurrences())
543 if (InputFilenames.size() == 0)
544 InputFilenames.push_back("a.out");
546 moreThanOneFile = InputFilenames.size() > 1;
547 std::for_each(InputFilenames.begin(), InputFilenames.end(),
548 PrintFileSectionSizes);