blob: 81d2bfb730b9a07abd3244c087684cbf3fd9752e [file] [log] [blame]
David Meyeree37b6e2012-03-02 23:43:51 +00001//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
David Meyer5c2b4ea2012-03-01 01:36:50 +00002//
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 Meyeree37b6e2012-03-02 23:43:51 +000010// 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 Meyer5c2b4ea2012-03-01 01:36:50 +000018
David Meyer97f77872012-03-01 22:19:54 +000019#include "llvm/ADT/Triple.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000020#include "llvm/Analysis/Verifier.h"
21#include "llvm/Object/ELF.h"
22#include "llvm/Object/ObjectFile.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000023#include "llvm/Support/CommandLine.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000024#include "llvm/Support/Debug.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000025#include "llvm/Support/Format.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000026#include "llvm/Support/FormattedStream.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000027#include "llvm/Support/PrettyStackTrace.h"
28#include "llvm/Support/Signals.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000029
30using namespace llvm;
31using namespace llvm::object;
32
33static cl::opt<std::string>
34InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
35
Rafael Espindola148ee4f2012-12-31 16:05:21 +000036static 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 Espindola663cebc2012-12-31 15:27:42 +000046static const char *getTypeStr(SymbolRef::Type Type) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000047 switch (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 "-";
54 }
55 return "INV";
56}
57
Rafael Espindola663cebc2012-12-31 15:27:42 +000058static std::string getSymbolFlagStr(uint32_t Flags) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000059 std::string result;
60 if (Flags & SymbolRef::SF_Undefined)
61 result += "undef,";
62 if (Flags & SymbolRef::SF_Global)
63 result += "global,";
64 if (Flags & SymbolRef::SF_Weak)
65 result += "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)
71 result += "common,";
72 if (Flags & SymbolRef::SF_FormatSpecific)
73 result += "formatspecific,";
74
75 // Remove trailing comma
76 if (result.size() > 0) {
77 result.erase(result.size() - 1);
78 }
79 return result;
80}
81
Rafael Espindolaad784792012-12-31 15:45:31 +000082static void checkError(error_code ec, const char *msg) {
83 if (ec)
84 report_fatal_error(std::string(msg) + ": " + ec.message());
85}
86
Rafael Espindola663cebc2012-12-31 15:27:42 +000087static void
88dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
Rafael Espindola1318e142012-12-31 15:30:58 +000089 StringRef Name;
90 SymbolRef::Type Type;
91 uint32_t Flags;
92 uint64_t Address;
93 uint64_t Size;
94 uint64_t FileOffset;
Rafael Espindolaad784792012-12-31 15:45:31 +000095 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");
Rafael Espindola1318e142012-12-31 15:30:58 +0000102 std::string FullName = Name;
David Meyer2d70e262012-03-09 20:59:52 +0000103
Rafael Espindola1318e142012-12-31 15:30:58 +0000104 // If this is a dynamic symbol from an ELF object, append
105 // the symbol's version to the name.
106 if (IsDynamic && obj->isELF()) {
107 StringRef Version;
108 bool IsDefault;
109 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
110 if (!Version.empty()) {
111 FullName += (IsDefault ? "@@" : "@");
112 FullName += Version;
David Meyer2d70e262012-03-09 20:59:52 +0000113 }
Rafael Espindola1318e142012-12-31 15:30:58 +0000114 }
David Meyer5c2b4ea2012-03-01 01:36:50 +0000115
Rafael Espindola1318e142012-12-31 15:30:58 +0000116 // 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)
123 << "\n";
David Meyer5c2b4ea2012-03-01 01:36:50 +0000124}
125
David Meyer5c2b4ea2012-03-01 01:36:50 +0000126// Iterate through the normal symbols in the ObjectFile
Rafael Espindola663cebc2012-12-31 15:27:42 +0000127static void dumpSymbols(const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000128 error_code ec;
129 uint32_t count = 0;
130 outs() << "Symbols:\n";
Rafael Espindola148ee4f2012-12-31 16:05:21 +0000131 dumpSymbolHeader();
David Meyer5c2b4ea2012-03-01 01:36:50 +0000132 symbol_iterator it = obj->begin_symbols();
133 symbol_iterator ie = obj->end_symbols();
134 while (it != ie) {
Rafael Espindola663cebc2012-12-31 15:27:42 +0000135 dumpSymbol(*it, obj, false);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000136 it.increment(ec);
137 if (ec)
138 report_fatal_error("Symbol iteration failed");
139 ++count;
140 }
141 outs() << " Total: " << count << "\n\n";
142}
143
144// Iterate through the dynamic symbols in the ObjectFile.
Rafael Espindola663cebc2012-12-31 15:27:42 +0000145static void dumpDynamicSymbols(const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000146 error_code ec;
147 uint32_t count = 0;
148 outs() << "Dynamic Symbols:\n";
Rafael Espindola148ee4f2012-12-31 16:05:21 +0000149 dumpSymbolHeader();
David Meyer5c2b4ea2012-03-01 01:36:50 +0000150 symbol_iterator it = obj->begin_dynamic_symbols();
151 symbol_iterator ie = obj->end_dynamic_symbols();
152 while (it != ie) {
Rafael Espindola663cebc2012-12-31 15:27:42 +0000153 dumpSymbol(*it, obj, true);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000154 it.increment(ec);
155 if (ec)
156 report_fatal_error("Symbol iteration failed");
157 ++count;
158 }
159 outs() << " Total: " << count << "\n\n";
160}
161
Rafael Espindola663cebc2012-12-31 15:27:42 +0000162static void dumpLibrary(const LibraryRef &lib) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000163 StringRef path;
164 lib.getPath(path);
165 outs() << " " << path << "\n";
166}
167
168// Iterate through needed libraries
Rafael Espindola663cebc2012-12-31 15:27:42 +0000169static void dumpLibrariesNeeded(const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000170 error_code ec;
171 uint32_t count = 0;
172 library_iterator it = obj->begin_libraries_needed();
173 library_iterator ie = obj->end_libraries_needed();
174 outs() << "Libraries needed:\n";
175 while (it != ie) {
Rafael Espindola663cebc2012-12-31 15:27:42 +0000176 dumpLibrary(*it);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000177 it.increment(ec);
178 if (ec)
179 report_fatal_error("Needed libraries iteration failed");
180 ++count;
181 }
182 outs() << " Total: " << count << "\n\n";
183}
184
Rafael Espindola663cebc2012-12-31 15:27:42 +0000185static void dumpHeaders(const ObjectFile *obj) {
David Meyer97f77872012-03-01 22:19:54 +0000186 outs() << "File Format : " << obj->getFileFormatName() << "\n";
187 outs() << "Arch : "
188 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
189 << "\n";
190 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
191 outs() << "Load Name : " << obj->getLoadName() << "\n";
192 outs() << "\n";
193}
194
David Meyer5c2b4ea2012-03-01 01:36:50 +0000195int main(int argc, char** argv) {
196 error_code ec;
197 sys::PrintStackTraceOnErrorSignal();
198 PrettyStackTraceProgram X(argc, argv);
199
200 cl::ParseCommandLineOptions(argc, argv,
201 "LLVM Object Reader\n");
202
203 if (InputFilename.empty()) {
204 errs() << "Please specify an input filename\n";
205 return 1;
206 }
207
208 // Open the object file
209 OwningPtr<MemoryBuffer> File;
210 if (MemoryBuffer::getFile(InputFilename, File)) {
211 errs() << InputFilename << ": Open failed\n";
212 return 1;
213 }
214
215 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
216 if (!obj) {
217 errs() << InputFilename << ": Object type not recognized\n";
218 }
219
Rafael Espindola663cebc2012-12-31 15:27:42 +0000220 dumpHeaders(obj);
221 dumpSymbols(obj);
222 dumpDynamicSymbols(obj);
223 dumpLibrariesNeeded(obj);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000224 return 0;
225}
226