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 const char *getTypeStr(SymbolRef::Type Type) {
38 case SymbolRef::ST_Unknown: return "?";
39 case SymbolRef::ST_Data: return "DATA";
40 case SymbolRef::ST_Debug: return "DBG";
41 case SymbolRef::ST_File: return "FILE";
42 case SymbolRef::ST_Function: return "FUNC";
43 case SymbolRef::ST_Other: return "-";
48 static std::string getSymbolFlagStr(uint32_t Flags) {
50 if (Flags & SymbolRef::SF_Undefined)
52 if (Flags & SymbolRef::SF_Global)
54 if (Flags & SymbolRef::SF_Weak)
56 if (Flags & SymbolRef::SF_Absolute)
57 result += "absolute,";
58 if (Flags & SymbolRef::SF_ThreadLocal)
59 result += "threadlocal,";
60 if (Flags & SymbolRef::SF_Common)
62 if (Flags & SymbolRef::SF_FormatSpecific)
63 result += "formatspecific,";
65 // Remove trailing comma
66 if (result.size() > 0) {
67 result.erase(result.size() - 1);
72 static void checkError(error_code ec, const char *msg) {
74 report_fatal_error(std::string(msg) + ": " + ec.message());
78 dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
85 checkError(Sym.getName(Name), "SymbolRef.getName() failed");
86 checkError(Sym.getAddress(Address), "SymbolRef.getAddress() failed");
87 checkError(Sym.getSize(Size), "SymbolRef.getSize() failed");
88 checkError(Sym.getFileOffset(FileOffset),
89 "SymbolRef.getFileOffset() failed");
90 checkError(Sym.getType(Type), "SymbolRef.getType() failed");
91 checkError(Sym.getFlags(Flags), "SymbolRef.getFlags() failed");
92 std::string FullName = Name;
94 // If this is a dynamic symbol from an ELF object, append
95 // the symbol's version to the name.
96 if (IsDynamic && obj->isELF()) {
99 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
100 if (!Version.empty()) {
101 FullName += (IsDefault ? "@@" : "@");
106 // format() can't handle StringRefs
107 outs() << format(" %-32s", FullName.c_str())
108 << format(" %-4s", getTypeStr(Type))
109 << format(" %16" PRIx64, Address)
110 << format(" %16" PRIx64, Size)
111 << format(" %16" PRIx64, FileOffset)
112 << " " << getSymbolFlagStr(Flags)
116 // Iterate through the normal symbols in the ObjectFile
117 static void dumpSymbols(const ObjectFile *obj) {
120 outs() << "Symbols:\n";
121 symbol_iterator it = obj->begin_symbols();
122 symbol_iterator ie = obj->end_symbols();
124 dumpSymbol(*it, obj, false);
127 report_fatal_error("Symbol iteration failed");
130 outs() << " Total: " << count << "\n\n";
133 // Iterate through the dynamic symbols in the ObjectFile.
134 static void dumpDynamicSymbols(const ObjectFile *obj) {
137 outs() << "Dynamic Symbols:\n";
138 symbol_iterator it = obj->begin_dynamic_symbols();
139 symbol_iterator ie = obj->end_dynamic_symbols();
141 dumpSymbol(*it, obj, true);
144 report_fatal_error("Symbol iteration failed");
147 outs() << " Total: " << count << "\n\n";
150 static void dumpLibrary(const LibraryRef &lib) {
153 outs() << " " << path << "\n";
156 // Iterate through needed libraries
157 static void dumpLibrariesNeeded(const ObjectFile *obj) {
160 library_iterator it = obj->begin_libraries_needed();
161 library_iterator ie = obj->end_libraries_needed();
162 outs() << "Libraries needed:\n";
167 report_fatal_error("Needed libraries iteration failed");
170 outs() << " Total: " << count << "\n\n";
173 static void dumpHeaders(const ObjectFile *obj) {
174 outs() << "File Format : " << obj->getFileFormatName() << "\n";
176 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
178 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
179 outs() << "Load Name : " << obj->getLoadName() << "\n";
183 int main(int argc, char** argv) {
185 sys::PrintStackTraceOnErrorSignal();
186 PrettyStackTraceProgram X(argc, argv);
188 cl::ParseCommandLineOptions(argc, argv,
189 "LLVM Object Reader\n");
191 if (InputFilename.empty()) {
192 errs() << "Please specify an input filename\n";
196 // Open the object file
197 OwningPtr<MemoryBuffer> File;
198 if (MemoryBuffer::getFile(InputFilename, File)) {
199 errs() << InputFilename << ": Open failed\n";
203 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
205 errs() << InputFilename << ": Object type not recognized\n";
210 dumpDynamicSymbols(obj);
211 dumpLibrariesNeeded(obj);