David Meyer | ee37b6e | 2012-03-02 23:43:51 +0000 | [diff] [blame] | 1 | //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===// |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 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 | // |
David Meyer | ee37b6e | 2012-03-02 23:43:51 +0000 | [diff] [blame] | 10 | // This program is a utility that works like traditional Unix "readelf", |
| 11 | // except that it can handle any type of object file recognized by lib/Object. |
| 12 | // |
| 13 | // It makes use of the generic ObjectFile interface. |
| 14 | // |
| 15 | // Caution: This utility is new, experimental, unsupported, and incomplete. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 18 | |
David Meyer | 97f7787 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Verifier.h" |
| 21 | #include "llvm/Object/ELF.h" |
| 22 | #include "llvm/Object/ObjectFile.h" |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Format.h" |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/PrettyStackTrace.h" |
| 28 | #include "llvm/Support/Signals.h" |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | using namespace llvm::object; |
| 32 | |
| 33 | static cl::opt<std::string> |
| 34 | InputFilename(cl::Positional, cl::desc("<input object>"), cl::init("")); |
| 35 | |
Rafael Espindola | 148ee4f | 2012-12-31 16:05:21 +0000 | [diff] [blame] | 36 | static void dumpSymbolHeader() { |
| 37 | outs() << format(" %-32s", (const char*)"Name") |
| 38 | << format(" %-4s", (const char*)"Type") |
| 39 | << format(" %-16s", (const char*)"Address") |
| 40 | << format(" %-16s", (const char*)"Size") |
| 41 | << format(" %-16s", (const char*)"FileOffset") |
| 42 | << format(" %-26s", (const char*)"Flags") |
| 43 | << "\n"; |
| 44 | } |
| 45 | |
Rafael Espindola | fc73847 | 2012-12-31 16:29:44 +0000 | [diff] [blame^] | 46 | static void dumpSectionHeader() { |
| 47 | outs() << format(" %-24s", (const char*)"Name") |
| 48 | << format(" %-16s", (const char*)"Address") |
| 49 | << format(" %-16s", (const char*)"Size") |
| 50 | << format(" %-8s", (const char*)"Align") |
| 51 | << format(" %-26s", (const char*)"Flags") |
| 52 | << "\n"; |
| 53 | } |
| 54 | |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 55 | static const char *getTypeStr(SymbolRef::Type Type) { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 56 | switch (Type) { |
| 57 | case SymbolRef::ST_Unknown: return "?"; |
| 58 | case SymbolRef::ST_Data: return "DATA"; |
| 59 | case SymbolRef::ST_Debug: return "DBG"; |
| 60 | case SymbolRef::ST_File: return "FILE"; |
| 61 | case SymbolRef::ST_Function: return "FUNC"; |
| 62 | case SymbolRef::ST_Other: return "-"; |
| 63 | } |
| 64 | return "INV"; |
| 65 | } |
| 66 | |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 67 | static std::string getSymbolFlagStr(uint32_t Flags) { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 68 | std::string result; |
| 69 | if (Flags & SymbolRef::SF_Undefined) |
| 70 | result += "undef,"; |
| 71 | if (Flags & SymbolRef::SF_Global) |
| 72 | result += "global,"; |
| 73 | if (Flags & SymbolRef::SF_Weak) |
| 74 | result += "weak,"; |
| 75 | if (Flags & SymbolRef::SF_Absolute) |
| 76 | result += "absolute,"; |
| 77 | if (Flags & SymbolRef::SF_ThreadLocal) |
| 78 | result += "threadlocal,"; |
| 79 | if (Flags & SymbolRef::SF_Common) |
| 80 | result += "common,"; |
| 81 | if (Flags & SymbolRef::SF_FormatSpecific) |
| 82 | result += "formatspecific,"; |
| 83 | |
| 84 | // Remove trailing comma |
| 85 | if (result.size() > 0) { |
| 86 | result.erase(result.size() - 1); |
| 87 | } |
| 88 | return result; |
| 89 | } |
| 90 | |
Rafael Espindola | ad78479 | 2012-12-31 15:45:31 +0000 | [diff] [blame] | 91 | static void checkError(error_code ec, const char *msg) { |
| 92 | if (ec) |
| 93 | report_fatal_error(std::string(msg) + ": " + ec.message()); |
| 94 | } |
| 95 | |
Rafael Espindola | fc73847 | 2012-12-31 16:29:44 +0000 | [diff] [blame^] | 96 | static std::string getSectionFlagStr(const SectionRef &Section) { |
| 97 | const struct { |
| 98 | error_code (SectionRef::*MemF)(bool &) const; |
| 99 | const char *FlagStr, *ErrorStr; |
| 100 | } Work[] = |
| 101 | {{ &SectionRef::isText, "text,", "Section.isText() failed" }, |
| 102 | { &SectionRef::isData, "data,", "Section.isData() failed" }, |
| 103 | { &SectionRef::isBSS, "bss,", "Section.isBSS() failed" }, |
| 104 | { &SectionRef::isRequiredForExecution, "required,", |
| 105 | "Section.isRequiredForExecution() failed" }, |
| 106 | { &SectionRef::isVirtual, "virtual,", "Section.isVirtual() failed" }, |
| 107 | { &SectionRef::isZeroInit, "zeroinit,", "Section.isZeroInit() failed" }, |
| 108 | { &SectionRef::isReadOnlyData, "rodata,", |
| 109 | "Section.isReadOnlyData() failed" }}; |
| 110 | |
| 111 | std::string result; |
| 112 | for (uint32_t I = 0; I < sizeof(Work)/sizeof(*Work); ++I) { |
| 113 | bool B; |
| 114 | checkError((Section.*Work[I].MemF)(B), Work[I].ErrorStr); |
| 115 | if (B) |
| 116 | result += Work[I].FlagStr; |
| 117 | } |
| 118 | |
| 119 | // Remove trailing comma |
| 120 | if (result.size() > 0) { |
| 121 | result.erase(result.size() - 1); |
| 122 | } |
| 123 | return result; |
| 124 | } |
| 125 | |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 126 | static void |
| 127 | dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) { |
Rafael Espindola | 1318e14 | 2012-12-31 15:30:58 +0000 | [diff] [blame] | 128 | StringRef Name; |
| 129 | SymbolRef::Type Type; |
| 130 | uint32_t Flags; |
| 131 | uint64_t Address; |
| 132 | uint64_t Size; |
| 133 | uint64_t FileOffset; |
Rafael Espindola | ad78479 | 2012-12-31 15:45:31 +0000 | [diff] [blame] | 134 | checkError(Sym.getName(Name), "SymbolRef.getName() failed"); |
| 135 | checkError(Sym.getAddress(Address), "SymbolRef.getAddress() failed"); |
| 136 | checkError(Sym.getSize(Size), "SymbolRef.getSize() failed"); |
| 137 | checkError(Sym.getFileOffset(FileOffset), |
| 138 | "SymbolRef.getFileOffset() failed"); |
| 139 | checkError(Sym.getType(Type), "SymbolRef.getType() failed"); |
| 140 | checkError(Sym.getFlags(Flags), "SymbolRef.getFlags() failed"); |
Rafael Espindola | 1318e14 | 2012-12-31 15:30:58 +0000 | [diff] [blame] | 141 | std::string FullName = Name; |
David Meyer | 2d70e26 | 2012-03-09 20:59:52 +0000 | [diff] [blame] | 142 | |
Rafael Espindola | 1318e14 | 2012-12-31 15:30:58 +0000 | [diff] [blame] | 143 | // If this is a dynamic symbol from an ELF object, append |
| 144 | // the symbol's version to the name. |
| 145 | if (IsDynamic && obj->isELF()) { |
| 146 | StringRef Version; |
| 147 | bool IsDefault; |
| 148 | GetELFSymbolVersion(obj, Sym, Version, IsDefault); |
| 149 | if (!Version.empty()) { |
| 150 | FullName += (IsDefault ? "@@" : "@"); |
| 151 | FullName += Version; |
David Meyer | 2d70e26 | 2012-03-09 20:59:52 +0000 | [diff] [blame] | 152 | } |
Rafael Espindola | 1318e14 | 2012-12-31 15:30:58 +0000 | [diff] [blame] | 153 | } |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | 1318e14 | 2012-12-31 15:30:58 +0000 | [diff] [blame] | 155 | // format() can't handle StringRefs |
| 156 | outs() << format(" %-32s", FullName.c_str()) |
| 157 | << format(" %-4s", getTypeStr(Type)) |
| 158 | << format(" %16" PRIx64, Address) |
| 159 | << format(" %16" PRIx64, Size) |
| 160 | << format(" %16" PRIx64, FileOffset) |
| 161 | << " " << getSymbolFlagStr(Flags) |
| 162 | << "\n"; |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 163 | } |
| 164 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 165 | // Iterate through the normal symbols in the ObjectFile |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 166 | static void dumpSymbols(const ObjectFile *obj) { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 167 | error_code ec; |
| 168 | uint32_t count = 0; |
| 169 | outs() << "Symbols:\n"; |
Rafael Espindola | 148ee4f | 2012-12-31 16:05:21 +0000 | [diff] [blame] | 170 | dumpSymbolHeader(); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 171 | symbol_iterator it = obj->begin_symbols(); |
| 172 | symbol_iterator ie = obj->end_symbols(); |
| 173 | while (it != ie) { |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 174 | dumpSymbol(*it, obj, false); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 175 | it.increment(ec); |
| 176 | if (ec) |
| 177 | report_fatal_error("Symbol iteration failed"); |
| 178 | ++count; |
| 179 | } |
| 180 | outs() << " Total: " << count << "\n\n"; |
| 181 | } |
| 182 | |
| 183 | // Iterate through the dynamic symbols in the ObjectFile. |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 184 | static void dumpDynamicSymbols(const ObjectFile *obj) { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 185 | error_code ec; |
| 186 | uint32_t count = 0; |
| 187 | outs() << "Dynamic Symbols:\n"; |
Rafael Espindola | 148ee4f | 2012-12-31 16:05:21 +0000 | [diff] [blame] | 188 | dumpSymbolHeader(); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 189 | symbol_iterator it = obj->begin_dynamic_symbols(); |
| 190 | symbol_iterator ie = obj->end_dynamic_symbols(); |
| 191 | while (it != ie) { |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 192 | dumpSymbol(*it, obj, true); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 193 | it.increment(ec); |
| 194 | if (ec) |
| 195 | report_fatal_error("Symbol iteration failed"); |
| 196 | ++count; |
| 197 | } |
| 198 | outs() << " Total: " << count << "\n\n"; |
| 199 | } |
| 200 | |
Rafael Espindola | fc73847 | 2012-12-31 16:29:44 +0000 | [diff] [blame^] | 201 | static void dumpSection(const SectionRef &Section, const ObjectFile *obj) { |
| 202 | StringRef Name; |
| 203 | checkError(Section.getName(Name), "SectionRef::getName() failed"); |
| 204 | uint64_t Addr, Size, Align; |
| 205 | checkError(Section.getAddress(Addr), "SectionRef::getAddress() failed"); |
| 206 | checkError(Section.getSize(Size), "SectionRef::getSize() failed"); |
| 207 | checkError(Section.getAlignment(Align), "SectionRef::getAlignment() failed"); |
| 208 | outs() << format(" %-24s", std::string(Name).c_str()) |
| 209 | << format(" %16" PRIx64, Addr) |
| 210 | << format(" %16" PRIx64, Size) |
| 211 | << format(" %8" PRIx64, Align) |
| 212 | << " " << getSectionFlagStr(Section) |
| 213 | << "\n"; |
| 214 | } |
| 215 | |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 216 | static void dumpLibrary(const LibraryRef &lib) { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 217 | StringRef path; |
| 218 | lib.getPath(path); |
| 219 | outs() << " " << path << "\n"; |
| 220 | } |
| 221 | |
Rafael Espindola | fc73847 | 2012-12-31 16:29:44 +0000 | [diff] [blame^] | 222 | template<typename Iterator, typename Func> |
| 223 | static void dump(const ObjectFile *obj, Func f, Iterator begin, Iterator end, |
| 224 | const char *errStr) { |
| 225 | error_code ec; |
| 226 | uint32_t count = 0; |
| 227 | Iterator it = begin, ie = end; |
| 228 | while (it != ie) { |
| 229 | f(*it, obj); |
| 230 | it.increment(ec); |
| 231 | if (ec) |
| 232 | report_fatal_error(errStr); |
| 233 | ++count; |
| 234 | } |
| 235 | outs() << " Total: " << count << "\n\n"; |
| 236 | } |
| 237 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 238 | // Iterate through needed libraries |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 239 | static void dumpLibrariesNeeded(const ObjectFile *obj) { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 240 | error_code ec; |
| 241 | uint32_t count = 0; |
| 242 | library_iterator it = obj->begin_libraries_needed(); |
| 243 | library_iterator ie = obj->end_libraries_needed(); |
| 244 | outs() << "Libraries needed:\n"; |
| 245 | while (it != ie) { |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 246 | dumpLibrary(*it); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 247 | it.increment(ec); |
| 248 | if (ec) |
| 249 | report_fatal_error("Needed libraries iteration failed"); |
| 250 | ++count; |
| 251 | } |
| 252 | outs() << " Total: " << count << "\n\n"; |
| 253 | } |
| 254 | |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 255 | static void dumpHeaders(const ObjectFile *obj) { |
David Meyer | 97f7787 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 256 | outs() << "File Format : " << obj->getFileFormatName() << "\n"; |
| 257 | outs() << "Arch : " |
| 258 | << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch()) |
| 259 | << "\n"; |
| 260 | outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n"; |
| 261 | outs() << "Load Name : " << obj->getLoadName() << "\n"; |
| 262 | outs() << "\n"; |
| 263 | } |
| 264 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 265 | int main(int argc, char** argv) { |
| 266 | error_code ec; |
| 267 | sys::PrintStackTraceOnErrorSignal(); |
| 268 | PrettyStackTraceProgram X(argc, argv); |
| 269 | |
| 270 | cl::ParseCommandLineOptions(argc, argv, |
| 271 | "LLVM Object Reader\n"); |
| 272 | |
| 273 | if (InputFilename.empty()) { |
| 274 | errs() << "Please specify an input filename\n"; |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | // Open the object file |
| 279 | OwningPtr<MemoryBuffer> File; |
| 280 | if (MemoryBuffer::getFile(InputFilename, File)) { |
| 281 | errs() << InputFilename << ": Open failed\n"; |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | ObjectFile *obj = ObjectFile::createObjectFile(File.take()); |
| 286 | if (!obj) { |
| 287 | errs() << InputFilename << ": Object type not recognized\n"; |
| 288 | } |
| 289 | |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 290 | dumpHeaders(obj); |
| 291 | dumpSymbols(obj); |
| 292 | dumpDynamicSymbols(obj); |
Rafael Espindola | fc73847 | 2012-12-31 16:29:44 +0000 | [diff] [blame^] | 293 | |
| 294 | outs() << "Sections:\n"; |
| 295 | dumpSectionHeader(); |
| 296 | dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(), |
| 297 | "Section iteration failed"); |
| 298 | |
Rafael Espindola | 663cebc | 2012-12-31 15:27:42 +0000 | [diff] [blame] | 299 | dumpLibrariesNeeded(obj); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |