llvm.org GIT mirror llvm / 32a12ba
Add llvm-size. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140722 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 8 years ago
5 changed file(s) with 323 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
2727 add_subdirectory(llvm-ranlib)
2828 add_subdirectory(llvm-ar)
2929 add_subdirectory(llvm-nm)
30 add_subdirectory(llvm-size)
3031
3132 add_subdirectory(llvm-ld)
3233 add_subdirectory(llvm-prof)
2525 lli llvm-extract llvm-mc \
2626 bugpoint llvm-bcanalyzer llvm-stub \
2727 llvm-diff macho-dump llvm-objdump \
28 llvm-rtdyld llvm-dwarfdump llvm-cov
28 llvm-rtdyld llvm-dwarfdump llvm-cov \
29 llvm-size
2930
3031 # Let users override the set of tools to build from the command line.
3132 ifdef ONLY_TOOLS
0 set(LLVM_LINK_COMPONENTS object)
1
2 add_llvm_tool(llvm-size
3 llvm-size.cpp
4 )
0 ##===- tools/llvm-size/Makefile ----------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 LEVEL = ../..
9
10 TOOLNAME = llvm-size
11 LINK_COMPONENTS = object
12
13 # This tool has no plugins, optimize startup time.
14 TOOL_NO_EXPORTS = 1
15
16 include $(LEVEL)/Makefile.common
0 //===-- llvm-size.cpp - Print the size of each object section -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This program is a utility that works like traditional Unix "size",
10 // that is, it prints out the size of each section, and the total size of all
11 // sections.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/Object/Archive.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/system_error.h"
28 #include
29 #include
30 using namespace llvm;
31 using namespace object;
32
33 namespace {
34 enum OutputFormatTy {berkeley, sysv};
35 cl::opt
36 OutputFormat("format",
37 cl::desc("Specify output format"),
38 cl::values(clEnumVal(sysv, "System V format"),
39 clEnumVal(berkeley, "Berkeley format"),
40 clEnumValEnd),
41 cl::init(berkeley));
42
43 cl::opt
44 OutputFormatShort(cl::desc("Specify output format"),
45 cl::values(clEnumValN(sysv, "A", "System V format"),
46 clEnumValN(berkeley, "B", "Berkeley format"),
47 clEnumValEnd),
48 cl::init(berkeley));
49
50 enum RadixTy {octal = 8, decimal = 10, hexadecimal = 16};
51 cl::opt
52 Radix("-radix",
53 cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
54 cl::init(decimal));
55
56 cl::opt
57 RadixShort(cl::desc("Print size in radix:"),
58 cl::values(clEnumValN(octal, "o", "Print size in octal"),
59 clEnumValN(decimal, "d", "Print size in decimal"),
60 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
61 clEnumValEnd),
62 cl::init(decimal));
63
64 cl::list
65 InputFilenames(cl::Positional, cl::desc(""),
66 cl::ZeroOrMore);
67
68 std::string ToolName;
69 }
70
71 static bool error(error_code ec) {
72 if (!ec) return false;
73
74 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
75 outs().flush();
76 return true;
77 }
78
79 static int getNumLengthAsString(uint64_t num) {
80 APInt conv(64, num);
81 SmallString<32> result;
82 conv.toString(result, unsigned int(Radix), false, true);
83 return result.size();
84 }
85
86 static void PrintObjectSectionSizes(ObjectFile *o) {
87 uint64_t total = 0;
88 std::string fmtbuf;
89 raw_string_ostream fmt(fmtbuf);
90
91 const char *radix_fmt = 0;
92 switch (Radix) {
93 case octal:
94 radix_fmt = "llo";
95 break;
96 case decimal:
97 radix_fmt = "llu";
98 break;
99 case hexadecimal:
100 radix_fmt = "llx";
101 break;
102 }
103 if (OutputFormat == sysv) {
104 // Run two passes over all sections. The first gets the lengths needed for
105 // formatting the output. The second actually does the output.
106 std::size_t max_name_len = strlen("section");
107 int max_size_len = strlen("size");
108 int max_addr_len = strlen("addr");
109 error_code ec;
110 for (ObjectFile::section_iterator i = o->begin_sections(),
111 e = o->end_sections(); i != e;
112 i.increment(ec)) {
113 if (error(ec))
114 return;
115 uint64_t size = 0;
116 if (error(i->getSize(size)))
117 return;
118 total += size;
119
120 StringRef name;
121 uint64_t addr = 0;
122 if (error(i->getName(name))) return;
123 if (error(i->getAddress(addr))) return;
124 max_name_len = std::max(max_name_len, name.size());
125 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
126 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
127 }
128
129 max_name_len += 2;
130 max_size_len += 2;
131 max_addr_len += 2;
132
133 fmt << "%-" << max_name_len << "s "
134 << "%" << max_size_len << "s "
135 << "%" << max_addr_len << "s\n";
136
137 // Print header
138 outs() << format(fmt.str().c_str(),
139 static_cast("section"),
140 static_cast("size"),
141 static_cast("addr"));
142 fmtbuf.clear();
143
144 // Setup per section format.
145 fmt << "%-" << max_name_len << "s "
146 << "%#" << max_size_len << radix_fmt << " "
147 << "%#" << max_addr_len << radix_fmt << "\n";
148
149 // Print each section.
150 for (ObjectFile::section_iterator i = o->begin_sections(),
151 e = o->end_sections(); i != e;
152 i.increment(ec)) {
153 if (error(ec))
154 return;
155
156 StringRef name;
157 uint64_t size = 0;
158 uint64_t addr = 0;
159 if (error(i->getName(name))) return;
160 if (error(i->getSize(size))) return;
161 if (error(i->getAddress(addr))) return;
162 std::string namestr = name;
163
164 outs() << format(fmt.str().c_str(),
165 namestr.c_str(),
166 size,
167 addr);
168 }
169
170 // Print total.
171 fmtbuf.clear();
172 fmt << "%-" << max_name_len << "s "
173 << "%#" << max_size_len << radix_fmt << "\n";
174 outs() << format(fmt.str().c_str(),
175 static_cast("Total"),
176 total);
177 } else {
178 uint64_t total_text = 0;
179 uint64_t total_data = 0;
180 uint64_t total_bss = 0;
181
182 error_code ec;
183 // Collect section data.
184 for (ObjectFile::section_iterator i = o->begin_sections(),
185 e = o->end_sections(); i != e;
186 i.increment(ec)) {
187 if (error(ec))
188 return;
189
190 uint64_t size = 0;
191 bool isText = false;
192 bool isData = false;
193 bool isBSS = false;
194 if (error(i->getSize(size))) return;
195 if (error(i->isText(isText))) return;
196 if (error(i->isData(isData))) return;
197 if (error(i->isBSS(isBSS))) return;
198 if (isText)
199 total_text += size;
200 else if (isData)
201 total_data += size;
202 else if (isBSS)
203 total_bss += size;
204 }
205
206 total = total_text + total_data + total_bss;
207
208 // Print result.
209 fmt << "%#7" << radix_fmt << " "
210 << "%#7" << radix_fmt << " "
211 << "%#7" << radix_fmt << " ";
212 outs() << format(fmt.str().c_str(),
213 total_text,
214 total_data,
215 total_bss);
216 fmtbuf.clear();
217 fmt << "%7" << (Radix == octal ? "llo" : "llu") << " "
218 << "%7llx ";
219 outs() << format(fmt.str().c_str(),
220 total,
221 total);
222 }
223 }
224
225 static void PrintFileSectionSizes(StringRef file) {
226 // If file is not stdin, check that it exists.
227 if (file != "-") {
228 bool exists;
229 if (sys::fs::exists(file, exists) || !exists) {
230 errs() << ToolName << ": '" << file << "': " << "No such file\n";
231 return;
232 }
233 }
234
235 OwningPtr binary;
236 if (error_code ec = createBinary(file, binary)) {
237 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
238 return;
239 }
240
241 if (Archive *a = dyn_cast(binary.get())) {
242 for (object::Archive::child_iterator i = a->begin_children(),
243 e = a->end_children(); i != e; ++i) {
244 OwningPtr child;
245 if (error_code ec = i->getAsBinary(child)) {
246 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
247 continue;
248 }
249 if (ObjectFile *o = dyn_cast(child.get())) {
250 if (OutputFormat == sysv)
251 outs() << o->getFileName() << " (ex " << a->getFileName()
252 << "):\n";
253 PrintObjectSectionSizes(o);
254 if (OutputFormat == berkeley)
255 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n";
256 }
257 }
258 } else if (ObjectFile *o = dyn_cast(binary.get())) {
259 if (OutputFormat == sysv)
260 outs() << o->getFileName() << " :\n";
261 PrintObjectSectionSizes(o);
262 if (OutputFormat == berkeley)
263 outs() << o->getFileName() << "\n";
264 } else {
265 errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n";
266 }
267 if (OutputFormat == sysv)
268 outs() << "\n";
269 }
270
271 int main(int argc, char **argv) {
272 // Print a stack trace if we signal out.
273 sys::PrintStackTraceOnErrorSignal();
274 PrettyStackTraceProgram X(argc, argv);
275
276 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
277 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n");
278
279 ToolName = argv[0];
280 if (OutputFormatShort.getNumOccurrences())
281 OutputFormat = OutputFormatShort;
282 if (RadixShort.getNumOccurrences())
283 Radix = int(RadixShort);
284
285 if (InputFilenames.size() == 0)
286 InputFilenames.push_back("a.out");
287
288 if (OutputFormat == berkeley)
289 outs() << " text data bss "
290 << (Radix == int(octal) ? "oct" : "dec")
291 << " hex filename\n";
292
293 std::for_each(InputFilenames.begin(), InputFilenames.end(),
294 PrintFileSectionSizes);
295
296 return 0;
297 }