blob: 8f8804a7c45812abddcba761a500177daba7f733 [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 Espindola663cebc2012-12-31 15:27:42 +000036static const char *getTypeStr(SymbolRef::Type Type) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000037 switch (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 "-";
44 }
45 return "INV";
46}
47
Rafael Espindola663cebc2012-12-31 15:27:42 +000048static std::string getSymbolFlagStr(uint32_t Flags) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000049 std::string result;
50 if (Flags & SymbolRef::SF_Undefined)
51 result += "undef,";
52 if (Flags & SymbolRef::SF_Global)
53 result += "global,";
54 if (Flags & SymbolRef::SF_Weak)
55 result += "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)
61 result += "common,";
62 if (Flags & SymbolRef::SF_FormatSpecific)
63 result += "formatspecific,";
64
65 // Remove trailing comma
66 if (result.size() > 0) {
67 result.erase(result.size() - 1);
68 }
69 return result;
70}
71
Rafael Espindolaad784792012-12-31 15:45:31 +000072static void checkError(error_code ec, const char *msg) {
73 if (ec)
74 report_fatal_error(std::string(msg) + ": " + ec.message());
75}
76
Rafael Espindola663cebc2012-12-31 15:27:42 +000077static void
78dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
Rafael Espindola1318e142012-12-31 15:30:58 +000079 StringRef Name;
80 SymbolRef::Type Type;
81 uint32_t Flags;
82 uint64_t Address;
83 uint64_t Size;
84 uint64_t FileOffset;
Rafael Espindolaad784792012-12-31 15:45:31 +000085 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");
Rafael Espindola1318e142012-12-31 15:30:58 +000092 std::string FullName = Name;
David Meyer2d70e262012-03-09 20:59:52 +000093
Rafael Espindola1318e142012-12-31 15:30:58 +000094 // If this is a dynamic symbol from an ELF object, append
95 // the symbol's version to the name.
96 if (IsDynamic && obj->isELF()) {
97 StringRef Version;
98 bool IsDefault;
99 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
100 if (!Version.empty()) {
101 FullName += (IsDefault ? "@@" : "@");
102 FullName += Version;
David Meyer2d70e262012-03-09 20:59:52 +0000103 }
Rafael Espindola1318e142012-12-31 15:30:58 +0000104 }
David Meyer5c2b4ea2012-03-01 01:36:50 +0000105
Rafael Espindola1318e142012-12-31 15:30:58 +0000106 // 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)
113 << "\n";
David Meyer5c2b4ea2012-03-01 01:36:50 +0000114}
115
David Meyer5c2b4ea2012-03-01 01:36:50 +0000116// Iterate through the normal symbols in the ObjectFile
Rafael Espindola663cebc2012-12-31 15:27:42 +0000117static void dumpSymbols(const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000118 error_code ec;
119 uint32_t count = 0;
120 outs() << "Symbols:\n";
121 symbol_iterator it = obj->begin_symbols();
122 symbol_iterator ie = obj->end_symbols();
123 while (it != ie) {
Rafael Espindola663cebc2012-12-31 15:27:42 +0000124 dumpSymbol(*it, obj, false);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000125 it.increment(ec);
126 if (ec)
127 report_fatal_error("Symbol iteration failed");
128 ++count;
129 }
130 outs() << " Total: " << count << "\n\n";
131}
132
133// Iterate through the dynamic symbols in the ObjectFile.
Rafael Espindola663cebc2012-12-31 15:27:42 +0000134static void dumpDynamicSymbols(const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000135 error_code ec;
136 uint32_t count = 0;
137 outs() << "Dynamic Symbols:\n";
138 symbol_iterator it = obj->begin_dynamic_symbols();
139 symbol_iterator ie = obj->end_dynamic_symbols();
140 while (it != ie) {
Rafael Espindola663cebc2012-12-31 15:27:42 +0000141 dumpSymbol(*it, obj, true);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000142 it.increment(ec);
143 if (ec)
144 report_fatal_error("Symbol iteration failed");
145 ++count;
146 }
147 outs() << " Total: " << count << "\n\n";
148}
149
Rafael Espindola663cebc2012-12-31 15:27:42 +0000150static void dumpLibrary(const LibraryRef &lib) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000151 StringRef path;
152 lib.getPath(path);
153 outs() << " " << path << "\n";
154}
155
156// Iterate through needed libraries
Rafael Espindola663cebc2012-12-31 15:27:42 +0000157static void dumpLibrariesNeeded(const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000158 error_code ec;
159 uint32_t count = 0;
160 library_iterator it = obj->begin_libraries_needed();
161 library_iterator ie = obj->end_libraries_needed();
162 outs() << "Libraries needed:\n";
163 while (it != ie) {
Rafael Espindola663cebc2012-12-31 15:27:42 +0000164 dumpLibrary(*it);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000165 it.increment(ec);
166 if (ec)
167 report_fatal_error("Needed libraries iteration failed");
168 ++count;
169 }
170 outs() << " Total: " << count << "\n\n";
171}
172
Rafael Espindola663cebc2012-12-31 15:27:42 +0000173static void dumpHeaders(const ObjectFile *obj) {
David Meyer97f77872012-03-01 22:19:54 +0000174 outs() << "File Format : " << obj->getFileFormatName() << "\n";
175 outs() << "Arch : "
176 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
177 << "\n";
178 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
179 outs() << "Load Name : " << obj->getLoadName() << "\n";
180 outs() << "\n";
181}
182
David Meyer5c2b4ea2012-03-01 01:36:50 +0000183int main(int argc, char** argv) {
184 error_code ec;
185 sys::PrintStackTraceOnErrorSignal();
186 PrettyStackTraceProgram X(argc, argv);
187
188 cl::ParseCommandLineOptions(argc, argv,
189 "LLVM Object Reader\n");
190
191 if (InputFilename.empty()) {
192 errs() << "Please specify an input filename\n";
193 return 1;
194 }
195
196 // Open the object file
197 OwningPtr<MemoryBuffer> File;
198 if (MemoryBuffer::getFile(InputFilename, File)) {
199 errs() << InputFilename << ": Open failed\n";
200 return 1;
201 }
202
203 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
204 if (!obj) {
205 errs() << InputFilename << ": Object type not recognized\n";
206 }
207
Rafael Espindola663cebc2012-12-31 15:27:42 +0000208 dumpHeaders(obj);
209 dumpSymbols(obj);
210 dumpDynamicSymbols(obj);
211 dumpLibrariesNeeded(obj);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000212 return 0;
213}
214