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