1 //===-- llvm-size.cpp - Print the size of each object section -------------===// 2 // 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" 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/Signals.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include "llvm/Support/system_error.h" 29 #include <algorithm> 30 #include <string> 31 using namespace llvm; 32 using namespace object; 33 34 enum OutputFormatTy {berkeley, sysv}; 35 static cl::opt<OutputFormatTy> 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 static cl::opt<OutputFormatTy> 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 static cl::opt<unsigned int> 52 Radix("-radix", 53 cl::desc("Print size in radix. Only 8, 10, and 16 are valid"), 54 cl::init(decimal)); 55 56 static cl::opt<RadixTy> 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 static cl::list<std::string> 65 InputFilenames(cl::Positional, cl::desc("<input files>"), 66 cl::ZeroOrMore); 67 68 static std::string ToolName; 69 70 /// @brief If ec is not success, print the error and return true. 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 /// @brief Get the length of the string that represents @p num in Radix 80 /// including the leading 0x or 0 for hexadecimal and octal respectively. 81 static size_t getNumLengthAsString(uint64_t num) { 82 APInt conv(64, num); 83 SmallString<32> result; 84 conv.toString(result, Radix, false, true); 85 return result.size(); 86 } 87 88 /// @brief Print the size of each section in @p o. 89 /// 90 /// The format used is determined by @c OutputFormat and @c Radix. 91 static void PrintObjectSectionSizes(ObjectFile *o) { 92 uint64_t total = 0; 93 std::string fmtbuf; 94 raw_string_ostream fmt(fmtbuf); 95 96 const char *radix_fmt = 0; 97 switch (Radix) { 98 case octal: 99 radix_fmt = PRIo64; 100 break; 101 case decimal: 102 radix_fmt = PRIu64; 103 break; 104 case hexadecimal: 105 radix_fmt = PRIx64; 106 break; 107 } 108 if (OutputFormat == sysv) { 109 // Run two passes over all sections. The first gets the lengths needed for 110 // formatting the output. The second actually does the output. 111 std::size_t max_name_len = strlen("section"); 112 std::size_t max_size_len = strlen("size"); 113 std::size_t max_addr_len = strlen("addr"); 114 for (section_iterator i = o->section_begin(), e = o->section_end(); 115 i != e; ++i) { 116 uint64_t size = 0; 117 if (error(i->getSize(size))) 118 return; 119 total += size; 120 121 StringRef name; 122 uint64_t addr = 0; 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)); 128 } 129 130 // Add extra padding. 131 max_name_len += 2; 132 max_size_len += 2; 133 max_addr_len += 2; 134 135 // Setup header format. 136 fmt << "%-" << max_name_len << "s " 137 << "%" << max_size_len << "s " 138 << "%" << max_addr_len << "s\n"; 139 140 // Print header 141 outs() << format(fmt.str().c_str(), 142 static_cast<const char*>("section"), 143 static_cast<const char*>("size"), 144 static_cast<const char*>("addr")); 145 fmtbuf.clear(); 146 147 // Setup per section format. 148 fmt << "%-" << max_name_len << "s " 149 << "%#" << max_size_len << radix_fmt << " " 150 << "%#" << max_addr_len << radix_fmt << "\n"; 151 152 // Print each section. 153 for (section_iterator i = o->section_begin(), e = o->section_end(); 154 i != e; ++i) { 155 StringRef name; 156 uint64_t size = 0; 157 uint64_t addr = 0; 158 if (error(i->getName(name))) return; 159 if (error(i->getSize(size))) return; 160 if (error(i->getAddress(addr))) return; 161 std::string namestr = name; 162 163 outs() << format(fmt.str().c_str(), 164 namestr.c_str(), 165 size, 166 addr); 167 } 168 169 // Print total. 170 fmtbuf.clear(); 171 fmt << "%-" << max_name_len << "s " 172 << "%#" << max_size_len << radix_fmt << "\n"; 173 outs() << format(fmt.str().c_str(), 174 static_cast<const char*>("Total"), 175 total); 176 } else { 177 // The Berkeley format does not display individual section sizes. It 178 // displays the cumulative size for each section type. 179 uint64_t total_text = 0; 180 uint64_t total_data = 0; 181 uint64_t total_bss = 0; 182 183 // Make one pass over the section table to calculate sizes. 184 for (section_iterator i = o->section_begin(), e = o->section_end(); 185 i != e; ++i) { 186 uint64_t size = 0; 187 bool isText = false; 188 bool isData = false; 189 bool isBSS = false; 190 if (error(i->getSize(size))) return; 191 if (error(i->isText(isText))) return; 192 if (error(i->isData(isData))) return; 193 if (error(i->isBSS(isBSS))) return; 194 if (isText) 195 total_text += size; 196 else if (isData) 197 total_data += size; 198 else if (isBSS) 199 total_bss += size; 200 } 201 202 total = total_text + total_data + total_bss; 203 204 // Print result. 205 fmt << "%#7" << radix_fmt << " " 206 << "%#7" << radix_fmt << " " 207 << "%#7" << radix_fmt << " "; 208 outs() << format(fmt.str().c_str(), 209 total_text, 210 total_data, 211 total_bss); 212 fmtbuf.clear(); 213 fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " " 214 << "%7" PRIx64 " "; 215 outs() << format(fmt.str().c_str(), 216 total, 217 total); 218 } 219 } 220 221 /// @brief Print the section sizes for @p file. If @p file is an archive, print 222 /// the section sizes for each archive member. 223 static void PrintFileSectionSizes(StringRef file) { 224 // If file is not stdin, check that it exists. 225 if (file != "-") { 226 bool exists; 227 if (sys::fs::exists(file, exists) || !exists) { 228 errs() << ToolName << ": '" << file << "': " << "No such file\n"; 229 return; 230 } 231 } 232 233 // Attempt to open the binary. 234 ErrorOr<Binary *> BinaryOrErr = createBinary(file); 235 if (error_code EC = BinaryOrErr.getError()) { 236 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n"; 237 return; 238 } 239 std::unique_ptr<Binary> binary(BinaryOrErr.get()); 240 241 if (Archive *a = dyn_cast<Archive>(binary.get())) { 242 // This is an archive. Iterate over each member and display its sizes. 243 for (object::Archive::child_iterator i = a->child_begin(), 244 e = a->child_end(); i != e; ++i) { 245 std::unique_ptr<Binary> child; 246 if (error_code ec = i->getAsBinary(child)) { 247 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n"; 248 continue; 249 } 250 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get())) { 251 if (OutputFormat == sysv) 252 outs() << o->getFileName() << " (ex " << a->getFileName() 253 << "):\n"; 254 PrintObjectSectionSizes(o); 255 if (OutputFormat == berkeley) 256 outs() << o->getFileName() << " (ex " << a->getFileName() << ")\n"; 257 } 258 } 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"; 265 } else { 266 errs() << ToolName << ": " << file << ": " << "Unrecognized file type.\n"; 267 } 268 // System V adds an extra newline at the end of each file. 269 if (OutputFormat == sysv) 270 outs() << "\n"; 271 } 272 273 int main(int argc, char **argv) { 274 // Print a stack trace if we signal out. 275 sys::PrintStackTraceOnErrorSignal(); 276 PrettyStackTraceProgram X(argc, argv); 277 278 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 279 cl::ParseCommandLineOptions(argc, argv, "llvm object size dumper\n"); 280 281 ToolName = argv[0]; 282 if (OutputFormatShort.getNumOccurrences()) 283 OutputFormat = OutputFormatShort; 284 if (RadixShort.getNumOccurrences()) 285 Radix = RadixShort; 286 287 if (InputFilenames.size() == 0) 288 InputFilenames.push_back("a.out"); 289 290 if (OutputFormat == berkeley) 291 outs() << " text data bss " 292 << (Radix == octal ? "oct" : "dec") 293 << " hex filename\n"; 294 295 std::for_each(InputFilenames.begin(), InputFilenames.end(), 296 PrintFileSectionSizes); 297 298 return 0; 299 } 300