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/Support/Casting.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/system_error.h"
32 using namespace object;
35 enum OutputFormatTy {berkeley, sysv};
36 cl::opt<OutputFormatTy>
37 OutputFormat("format",
38 cl::desc("Specify output format"),
39 cl::values(clEnumVal(sysv, "System V format"),
40 clEnumVal(berkeley, "Berkeley format"),
44 cl::opt<OutputFormatTy>
45 OutputFormatShort(cl::desc("Specify output format"),
46 cl::values(clEnumValN(sysv, "A", "System V format"),
47 clEnumValN(berkeley, "B", "Berkeley format"),
51 enum RadixTy {octal = 8, decimal = 10, hexadecimal = 16};
54 cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
58 RadixShort(cl::desc("Print size in radix:"),
59 cl::values(clEnumValN(octal, "o", "Print size in octal"),
60 clEnumValN(decimal, "d", "Print size in decimal"),
61 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
66 InputFilenames(cl::Positional, cl::desc("<input files>"),
72 static bool error(error_code ec) {
73 if (!ec) return false;
75 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
80 static int getNumLengthAsString(uint64_t num) {
82 SmallString<32> result;
83 conv.toString(result, static_cast<unsigned int>(Radix), false, true);
87 static void PrintObjectSectionSizes(ObjectFile *o) {
90 raw_string_ostream fmt(fmtbuf);
92 const char *radix_fmt = 0;
104 if (OutputFormat == sysv) {
105 // Run two passes over all sections. The first gets the lengths needed for
106 // formatting the output. The second actually does the output.
107 std::size_t max_name_len = strlen("section");
108 int max_size_len = strlen("size");
109 int max_addr_len = strlen("addr");
111 for (ObjectFile::section_iterator i = o->begin_sections(),
112 e = o->end_sections(); i != e;
117 if (error(i->getSize(size)))
123 if (error(i->getName(name))) return;
124 if (error(i->getAddress(addr))) return;
125 max_name_len = std::max(max_name_len, name.size());
126 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
127 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
134 fmt << "%-" << max_name_len << "s "
135 << "%" << max_size_len << "s "
136 << "%" << max_addr_len << "s\n";
139 outs() << format(fmt.str().c_str(),
140 static_cast<const char*>("section"),
141 static_cast<const char*>("size"),
142 static_cast<const char*>("addr"));
145 // Setup per section format.
146 fmt << "%-" << max_name_len << "s "
147 << "%#" << max_size_len << radix_fmt << " "
148 << "%#" << max_addr_len << radix_fmt << "\n";
150 // Print each section.
151 for (ObjectFile::section_iterator i = o->begin_sections(),
152 e = o->end_sections(); i != e;
160 if (error(i->getName(name))) return;
161 if (error(i->getSize(size))) return;
162 if (error(i->getAddress(addr))) return;
163 std::string namestr = name;
165 outs() << format(fmt.str().c_str(),
173 fmt << "%-" << max_name_len << "s "
174 << "%#" << max_size_len << radix_fmt << "\n";
175 outs() << format(fmt.str().c_str(),
176 static_cast<const char*>("Total"),
179 uint64_t total_text = 0;
180 uint64_t total_data = 0;
181 uint64_t total_bss = 0;
184 // Collect section data.
185 for (ObjectFile::section_iterator i = o->begin_sections(),
186 e = o->end_sections(); i != e;
195 if (error(i->getSize(size))) return;
196 if (error(i->isText(isText))) return;
197 if (error(i->isData(isData))) return;
198 if (error(i->isBSS(isBSS))) return;
207 total = total_text + total_data + total_bss;
210 fmt << "%#7" << radix_fmt << " "
211 << "%#7" << radix_fmt << " "
212 << "%#7" << radix_fmt << " ";
213 outs() << format(fmt.str().c_str(),
218 fmt << "%7" << (Radix == octal ? "llo" : "llu") << " "
220 outs() << format(fmt.str().c_str(),
226 static void PrintFileSectionSizes(StringRef file) {
227 // If file is not stdin, check that it exists.
230 if (sys::fs::exists(file, exists) || !exists) {
231 errs() << ToolName << ": '" << file << "': " << "No such file\n";
236 OwningPtr<Binary> binary;
237 if (error_code ec = createBinary(file, binary)) {
238 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
242 if (Archive *a = dyn_cast<Archive>(binary.get())) {
243 for (object::Archive::child_iterator i = a->begin_children(),
244 e = a->end_children(); i != e; ++i) {
245 OwningPtr<Binary> child;
246 if (error_code ec = i->getAsBinary(child)) {
247 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
250 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
251 if (OutputFormat == sysv)
252 outs() << o->getFileName() << " (ex " << a->getFileName()
254 PrintObjectSectionSizes(o);
255 if (OutputFormat == berkeley)
256 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
259 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
260 if (OutputFormat == sysv)
261 outs() << o->getFileName() << " :\n";
262 PrintObjectSectionSizes(o);
263 if (OutputFormat == berkeley)
264 outs() << o->getFileName() << "\n";
266 errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n";
268 if (OutputFormat == sysv)
272 int main(int argc, char **argv) {
273 // Print a stack trace if we signal out.
274 sys::PrintStackTraceOnErrorSignal();
275 PrettyStackTraceProgram X(argc, argv);
277 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
278 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
281 if (OutputFormatShort.getNumOccurrences())
282 OutputFormat = OutputFormatShort;
283 if (RadixShort.getNumOccurrences())
284 Radix = int(RadixShort);
286 if (InputFilenames.size() == 0)
287 InputFilenames.push_back("a.out");
289 if (OutputFormat == berkeley)
290 outs() << " text data bss "
291 << (Radix == int(octal) ? "oct" : "dec")
292 << " hex filename\n";
294 std::for_each(InputFilenames.begin(), InputFilenames.end(),
295 PrintFileSectionSizes);