llvm.org GIT mirror llvm / 75c4aba
llvm-size: Apply Chris's code review fixes. This doesn't use formated_raw_ostream because it doesn't support the functionality needed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140751 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 8 years ago
1 changed file(s) with 59 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
3030 using namespace llvm;
3131 using namespace object;
3232
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
33 enum OutputFormatTy {berkeley, sysv};
34 static cl::opt
35 OutputFormat("format",
36 cl::desc("Specify output format"),
37 cl::values(clEnumVal(sysv, "System V format"),
38 clEnumVal(berkeley, "Berkeley format"),
39 clEnumValEnd),
40 cl::init(berkeley));
41
42 static cl::opt
43 OutputFormatShort(cl::desc("Specify output format"),
44 cl::values(clEnumValN(sysv, "A", "System V format"),
45 clEnumValN(berkeley, "B", "Berkeley format"),
46 clEnumValEnd),
47 cl::init(berkeley));
48
49 enum RadixTy {octal = 8, decimal = 10, hexadecimal = 16};
50 static cl::opt
51 Radix("-radix",
52 cl::desc("Print size in radix. Only 8, 10, and 16 are valid"),
53 cl::init(decimal));
54
55 static cl::opt
56 RadixShort(cl::desc("Print size in radix:"),
57 cl::values(clEnumValN(octal, "o", "Print size in octal"),
58 clEnumValN(decimal, "d", "Print size in decimal"),
59 clEnumValN(hexadecimal, "x", "Print size in hexadecimal"),
60 clEnumValEnd),
61 cl::init(decimal));
62
63 static cl::list
64 InputFilenames(cl::Positional, cl::desc(""),
65 cl::ZeroOrMore);
66
67 static std::string ToolName;
68
69 /// @brief If ec is not success, print the error and return true.
7170 static bool error(error_code ec) {
7271 if (!ec) return false;
7372
7675 return true;
7776 }
7877
79 static int getNumLengthAsString(uint64_t num) {
78 /// @brief Get the length of the string that represents @p num in Radix
79 /// including the leading 0x or 0 for hexadecimal and octal respectively.
80 static unsigned int getNumLengthAsString(uint64_t num) {
8081 APInt conv(64, num);
8182 SmallString<32> result;
82 conv.toString(result, static_cast(Radix), false, true);
83 conv.toString(result, Radix, false, true);
8384 return result.size();
8485 }
8586
87 /// @brief Print the size of each section in @p o.
88 ///
89 /// The format used is determined by @c OutputFormat and @c Radix.
8690 static void PrintObjectSectionSizes(ObjectFile *o) {
8791 uint64_t total = 0;
8892 std::string fmtbuf;
104108 // Run two passes over all sections. The first gets the lengths needed for
105109 // formatting the output. The second actually does the output.
106110 std::size_t max_name_len = strlen("section");
107 int max_size_len = strlen("size");
108 int max_addr_len = strlen("addr");
111 std::size_t max_size_len = strlen("size");
112 std::size_t max_addr_len = strlen("addr");
109113 error_code ec;
110114 for (ObjectFile::section_iterator i = o->begin_sections(),
111115 e = o->end_sections(); i != e;
126130 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
127131 }
128132
133 // Add extra padding.
129134 max_name_len += 2;
130135 max_size_len += 2;
131136 max_addr_len += 2;
132137
138 // Setup header format.
133139 fmt << "%-" << max_name_len << "s "
134140 << "%" << max_size_len << "s "
135141 << "%" << max_addr_len << "s\n";
175181 static_cast("Total"),
176182 total);
177183 } else {
184 // The Berkeley format does not display individual section sizes. It
185 // displays the cumulative size for each section type.
178186 uint64_t total_text = 0;
179187 uint64_t total_data = 0;
180188 uint64_t total_bss = 0;
181189
190 // Make one pass over the section table to calculate sizes.
182191 error_code ec;
183 // Collect section data.
184192 for (ObjectFile::section_iterator i = o->begin_sections(),
185193 e = o->end_sections(); i != e;
186194 i.increment(ec)) {
222230 }
223231 }
224232
233 /// @brief Print the section sizes for @p file. If @p file is an archive, print
234 /// the section sizes for each archive member.
225235 static void PrintFileSectionSizes(StringRef file) {
226236 // If file is not stdin, check that it exists.
227237 if (file != "-") {
232242 }
233243 }
234244
245 // Attempt to open the binary.
235246 OwningPtr binary;
236247 if (error_code ec = createBinary(file, binary)) {
237248 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
239250 }
240251
241252 if (Archive *a = dyn_cast(binary.get())) {
253 // This is an archive. Iterate over each member and display its sizes.
242254 for (object::Archive::child_iterator i = a->begin_children(),
243 e = a->end_children(); i != e; ++i) {
255 e = a->end_children(); i != e; ++i) {
244256 OwningPtr child;
245257 if (error_code ec = i->getAsBinary(child)) {
246258 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
264276 } else {
265277 errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n";
266278 }
279 // System V adds an extra newline at the end of each file.
267280 if (OutputFormat == sysv)
268281 outs() << "\n";
269282 }
280293 if (OutputFormatShort.getNumOccurrences())
281294 OutputFormat = OutputFormatShort;
282295 if (RadixShort.getNumOccurrences())
283 Radix = int(RadixShort);
296 Radix = RadixShort;
284297
285298 if (InputFilenames.size() == 0)
286299 InputFilenames.push_back("a.out");
287300
288301 if (OutputFormat == berkeley)
289302 outs() << " text data bss "
290 << (Radix == int(octal) ? "oct" : "dec")
303 << (Radix == octal ? "oct" : "dec")
291304 << " hex filename\n";
292305
293306 std::for_each(InputFilenames.begin(), InputFilenames.end(),