1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 // It makes use of the generic ObjectFile interface.
15 // Caution: This utility is new, experimental, unsupported, and incomplete.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/Object/ELF.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Signals.h"
31 using namespace llvm::object;
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
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")
46 static const char *getTypeStr(SymbolRef::Type Type) {
48 case SymbolRef::ST_Unknown: return "?";
49 case SymbolRef::ST_Data: return "DATA";
50 case SymbolRef::ST_Debug: return "DBG";
51 case SymbolRef::ST_File: return "FILE";
52 case SymbolRef::ST_Function: return "FUNC";
53 case SymbolRef::ST_Other: return "-";
58 static std::string getSymbolFlagStr(uint32_t Flags) {
60 if (Flags & SymbolRef::SF_Undefined)
62 if (Flags & SymbolRef::SF_Global)
64 if (Flags & SymbolRef::SF_Weak)
66 if (Flags & SymbolRef::SF_Absolute)
67 result += "absolute,";
68 if (Flags & SymbolRef::SF_ThreadLocal)
69 result += "threadlocal,";
70 if (Flags & SymbolRef::SF_Common)
72 if (Flags & SymbolRef::SF_FormatSpecific)
73 result += "formatspecific,";
75 // Remove trailing comma
76 if (result.size() > 0) {
77 result.erase(result.size() - 1);
82 static void checkError(error_code ec, const char *msg) {
84 report_fatal_error(std::string(msg) + ": " + ec.message());
88 dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
95 checkError(Sym.getName(Name), "SymbolRef.getName() failed");
96 checkError(Sym.getAddress(Address), "SymbolRef.getAddress() failed");
97 checkError(Sym.getSize(Size), "SymbolRef.getSize() failed");
98 checkError(Sym.getFileOffset(FileOffset),
99 "SymbolRef.getFileOffset() failed");
100 checkError(Sym.getType(Type), "SymbolRef.getType() failed");
101 checkError(Sym.getFlags(Flags), "SymbolRef.getFlags() failed");
102 std::string FullName = Name;
104 // If this is a dynamic symbol from an ELF object, append
105 // the symbol's version to the name.
106 if (IsDynamic && obj->isELF()) {
109 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
110 if (!Version.empty()) {
111 FullName += (IsDefault ? "@@" : "@");
116 // format() can't handle StringRefs
117 outs() << format(" %-32s", FullName.c_str())
118 << format(" %-4s", getTypeStr(Type))
119 << format(" %16" PRIx64, Address)
120 << format(" %16" PRIx64, Size)
121 << format(" %16" PRIx64, FileOffset)
122 << " " << getSymbolFlagStr(Flags)
126 // Iterate through the normal symbols in the ObjectFile
127 static void dumpSymbols(const ObjectFile *obj) {
130 outs() << "Symbols:\n";
132 symbol_iterator it = obj->begin_symbols();
133 symbol_iterator ie = obj->end_symbols();
135 dumpSymbol(*it, obj, false);
138 report_fatal_error("Symbol iteration failed");
141 outs() << " Total: " << count << "\n\n";
144 // Iterate through the dynamic symbols in the ObjectFile.
145 static void dumpDynamicSymbols(const ObjectFile *obj) {
148 outs() << "Dynamic Symbols:\n";
150 symbol_iterator it = obj->begin_dynamic_symbols();
151 symbol_iterator ie = obj->end_dynamic_symbols();
153 dumpSymbol(*it, obj, true);
156 report_fatal_error("Symbol iteration failed");
159 outs() << " Total: " << count << "\n\n";
162 static void dumpLibrary(const LibraryRef &lib) {
165 outs() << " " << path << "\n";
168 // Iterate through needed libraries
169 static void dumpLibrariesNeeded(const ObjectFile *obj) {
172 library_iterator it = obj->begin_libraries_needed();
173 library_iterator ie = obj->end_libraries_needed();
174 outs() << "Libraries needed:\n";
179 report_fatal_error("Needed libraries iteration failed");
182 outs() << " Total: " << count << "\n\n";
185 static void dumpHeaders(const ObjectFile *obj) {
186 outs() << "File Format : " << obj->getFileFormatName() << "\n";
188 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
190 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
191 outs() << "Load Name : " << obj->getLoadName() << "\n";
195 int main(int argc, char** argv) {
197 sys::PrintStackTraceOnErrorSignal();
198 PrettyStackTraceProgram X(argc, argv);
200 cl::ParseCommandLineOptions(argc, argv,
201 "LLVM Object Reader\n");
203 if (InputFilename.empty()) {
204 errs() << "Please specify an input filename\n";
208 // Open the object file
209 OwningPtr<MemoryBuffer> File;
210 if (MemoryBuffer::getFile(InputFilename, File)) {
211 errs() << InputFilename << ": Open failed\n";
215 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
217 errs() << InputFilename << ": Object type not recognized\n";
222 dumpDynamicSymbols(obj);
223 dumpLibrariesNeeded(obj);