OLD | NEW |
1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===// | 1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This program is a utility that works like traditional Unix "readelf", | 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. | 11 // except that it can handle any type of object file recognized by lib/Object. |
12 // | 12 // |
13 // It makes use of the generic ObjectFile interface. | 13 // It makes use of the generic ObjectFile interface. |
14 // | 14 // |
15 // Caution: This utility is new, experimental, unsupported, and incomplete. | 15 // Caution: This utility is new, experimental, unsupported, and incomplete. |
16 // | 16 // |
17 //===----------------------------------------------------------------------===// | 17 //===----------------------------------------------------------------------===// |
18 | 18 |
19 #include "llvm/Object/ObjectFile.h" | 19 #include "llvm/Object/ObjectFile.h" |
| 20 #include "llvm/Object/ELF.h" |
20 #include "llvm/Analysis/Verifier.h" | 21 #include "llvm/Analysis/Verifier.h" |
21 #include "llvm/ADT/Triple.h" | 22 #include "llvm/ADT/Triple.h" |
22 #include "llvm/Support/Format.h" | 23 #include "llvm/Support/Format.h" |
23 #include "llvm/Support/CommandLine.h" | 24 #include "llvm/Support/CommandLine.h" |
24 #include "llvm/Support/PrettyStackTrace.h" | 25 #include "llvm/Support/PrettyStackTrace.h" |
25 #include "llvm/Support/Debug.h" | 26 #include "llvm/Support/Debug.h" |
26 #include "llvm/Support/Signals.h" | 27 #include "llvm/Support/Signals.h" |
27 #include "llvm/Support/FormattedStream.h" | 28 #include "llvm/Support/FormattedStream.h" |
28 | 29 |
29 using namespace llvm; | 30 using namespace llvm; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 if (Flags & SymbolRef::SF_FormatSpecific) | 72 if (Flags & SymbolRef::SF_FormatSpecific) |
72 result += "formatspecific,"; | 73 result += "formatspecific,"; |
73 | 74 |
74 // Remove trailing comma | 75 // Remove trailing comma |
75 if (result.size() > 0) { | 76 if (result.size() > 0) { |
76 result.erase(result.size() - 1); | 77 result.erase(result.size() - 1); |
77 } | 78 } |
78 return result; | 79 return result; |
79 } | 80 } |
80 | 81 |
81 void DumpSymbol(const SymbolRef &sym) { | 82 void DumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) { |
82 StringRef Name; | 83 StringRef Name; |
83 SymbolRef::Type Type; | 84 SymbolRef::Type Type; |
84 uint32_t Flags; | 85 uint32_t Flags; |
85 uint64_t Address; | 86 uint64_t Address; |
86 uint64_t Size; | 87 uint64_t Size; |
87 uint64_t FileOffset; | 88 uint64_t FileOffset; |
88 sym.getName(Name); | 89 Sym.getName(Name); |
89 sym.getAddress(Address); | 90 Sym.getAddress(Address); |
90 sym.getSize(Size); | 91 Sym.getSize(Size); |
91 sym.getFileOffset(FileOffset); | 92 Sym.getFileOffset(FileOffset); |
92 sym.getType(Type); | 93 Sym.getType(Type); |
93 sym.getFlags(Flags); | 94 Sym.getFlags(Flags); |
| 95 std::string FullName = Name; |
| 96 |
| 97 // If this is a dynamic symbol from an ELF object, append |
| 98 // the symbol's version to the name. |
| 99 if (IsDynamic && obj->isELF()) { |
| 100 StringRef Version; |
| 101 bool IsDefault; |
| 102 GetELFSymbolVersion(obj, Sym, Version, IsDefault); |
| 103 if (!Version.empty()) { |
| 104 FullName += (IsDefault ? "@@" : "@"); |
| 105 FullName += Version; |
| 106 } |
| 107 } |
94 | 108 |
95 // format() can't handle StringRefs | 109 // format() can't handle StringRefs |
96 outs() << format(" %-32s", Name.str().c_str()) | 110 outs() << format(" %-32s", FullName.c_str()) |
97 << format(" %-4s", GetTypeStr(Type)) | 111 << format(" %-4s", GetTypeStr(Type)) |
98 << format(" %16"PRIx64, Address) | 112 << format(" %16"PRIx64, Address) |
99 << format(" %16"PRIx64, Size) | 113 << format(" %16"PRIx64, Size) |
100 << format(" %16"PRIx64, FileOffset) | 114 << format(" %16"PRIx64, FileOffset) |
101 << " " << GetFlagStr(Flags) | 115 << " " << GetFlagStr(Flags) |
102 << "\n"; | 116 << "\n"; |
103 } | 117 } |
104 | 118 |
105 | 119 |
106 // Iterate through the normal symbols in the ObjectFile | 120 // Iterate through the normal symbols in the ObjectFile |
107 void DumpSymbols(const ObjectFile *obj) { | 121 void DumpSymbols(const ObjectFile *obj) { |
108 error_code ec; | 122 error_code ec; |
109 uint32_t count = 0; | 123 uint32_t count = 0; |
110 outs() << "Symbols:\n"; | 124 outs() << "Symbols:\n"; |
111 symbol_iterator it = obj->begin_symbols(); | 125 symbol_iterator it = obj->begin_symbols(); |
112 symbol_iterator ie = obj->end_symbols(); | 126 symbol_iterator ie = obj->end_symbols(); |
113 while (it != ie) { | 127 while (it != ie) { |
114 DumpSymbol(*it); | 128 DumpSymbol(*it, obj, false); |
115 it.increment(ec); | 129 it.increment(ec); |
116 if (ec) | 130 if (ec) |
117 report_fatal_error("Symbol iteration failed"); | 131 report_fatal_error("Symbol iteration failed"); |
118 ++count; | 132 ++count; |
119 } | 133 } |
120 outs() << " Total: " << count << "\n\n"; | 134 outs() << " Total: " << count << "\n\n"; |
121 } | 135 } |
122 | 136 |
123 // Iterate through the dynamic symbols in the ObjectFile. | 137 // Iterate through the dynamic symbols in the ObjectFile. |
124 void DumpDynamicSymbols(const ObjectFile *obj) { | 138 void DumpDynamicSymbols(const ObjectFile *obj) { |
125 error_code ec; | 139 error_code ec; |
126 uint32_t count = 0; | 140 uint32_t count = 0; |
127 outs() << "Dynamic Symbols:\n"; | 141 outs() << "Dynamic Symbols:\n"; |
128 symbol_iterator it = obj->begin_dynamic_symbols(); | 142 symbol_iterator it = obj->begin_dynamic_symbols(); |
129 symbol_iterator ie = obj->end_dynamic_symbols(); | 143 symbol_iterator ie = obj->end_dynamic_symbols(); |
130 while (it != ie) { | 144 while (it != ie) { |
131 DumpSymbol(*it); | 145 DumpSymbol(*it, obj, true); |
132 it.increment(ec); | 146 it.increment(ec); |
133 if (ec) | 147 if (ec) |
134 report_fatal_error("Symbol iteration failed"); | 148 report_fatal_error("Symbol iteration failed"); |
135 ++count; | 149 ++count; |
136 } | 150 } |
137 outs() << " Total: " << count << "\n\n"; | 151 outs() << " Total: " << count << "\n\n"; |
138 } | 152 } |
139 | 153 |
140 void DumpLibrary(const LibraryRef &lib) { | 154 void DumpLibrary(const LibraryRef &lib) { |
141 StringRef path; | 155 StringRef path; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 errs() << InputFilename << ": Object type not recognized\n"; | 209 errs() << InputFilename << ": Object type not recognized\n"; |
196 } | 210 } |
197 | 211 |
198 DumpHeaders(obj); | 212 DumpHeaders(obj); |
199 DumpSymbols(obj); | 213 DumpSymbols(obj); |
200 DumpDynamicSymbols(obj); | 214 DumpDynamicSymbols(obj); |
201 DumpLibrariesNeeded(obj); | 215 DumpLibrariesNeeded(obj); |
202 return 0; | 216 return 0; |
203 } | 217 } |
204 | 218 |
OLD | NEW |