blob: 0a43775ef27044b29b077dc65cfe5d692590ff9b [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//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000010// This is a tool similar to readelf, except it works on multiple object file
11// formats. The main purpose of this tool is to provide detailed output suitable
12// for FileCheck.
David Meyeree37b6e2012-03-02 23:43:51 +000013//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000014// Flags should be similar to readelf where supported, but the output format
15// does not need to be identical. The point is to not make users learn yet
16// another set of flags.
David Meyeree37b6e2012-03-02 23:43:51 +000017//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000018// Output should be specialized for each format where appropriate.
David Meyeree37b6e2012-03-02 23:43:51 +000019//
20//===----------------------------------------------------------------------===//
David Meyer5c2b4ea2012-03-01 01:36:50 +000021
David Meyer97f77872012-03-01 22:19:54 +000022#include "llvm/ADT/Triple.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000023#include "llvm/Analysis/Verifier.h"
24#include "llvm/Object/ELF.h"
25#include "llvm/Object/ObjectFile.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000026#include "llvm/Support/CommandLine.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000027#include "llvm/Support/Debug.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000028#include "llvm/Support/Format.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000029#include "llvm/Support/FormattedStream.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000030#include "llvm/Support/PrettyStackTrace.h"
31#include "llvm/Support/Signals.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000032
33using namespace llvm;
34using namespace llvm::object;
35
36static cl::opt<std::string>
37InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
38
Rafael Espindola148ee4f2012-12-31 16:05:21 +000039static void dumpSymbolHeader() {
40 outs() << format(" %-32s", (const char*)"Name")
41 << format(" %-4s", (const char*)"Type")
42 << format(" %-16s", (const char*)"Address")
43 << format(" %-16s", (const char*)"Size")
44 << format(" %-16s", (const char*)"FileOffset")
45 << format(" %-26s", (const char*)"Flags")
46 << "\n";
47}
48
Rafael Espindolafc738472012-12-31 16:29:44 +000049static void dumpSectionHeader() {
50 outs() << format(" %-24s", (const char*)"Name")
51 << format(" %-16s", (const char*)"Address")
52 << format(" %-16s", (const char*)"Size")
53 << format(" %-8s", (const char*)"Align")
54 << format(" %-26s", (const char*)"Flags")
55 << "\n";
56}
57
Rafael Espindola663cebc2012-12-31 15:27:42 +000058static const char *getTypeStr(SymbolRef::Type Type) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000059 switch (Type) {
60 case SymbolRef::ST_Unknown: return "?";
61 case SymbolRef::ST_Data: return "DATA";
62 case SymbolRef::ST_Debug: return "DBG";
63 case SymbolRef::ST_File: return "FILE";
64 case SymbolRef::ST_Function: return "FUNC";
65 case SymbolRef::ST_Other: return "-";
66 }
67 return "INV";
68}
69
Rafael Espindola663cebc2012-12-31 15:27:42 +000070static std::string getSymbolFlagStr(uint32_t Flags) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000071 std::string result;
72 if (Flags & SymbolRef::SF_Undefined)
73 result += "undef,";
74 if (Flags & SymbolRef::SF_Global)
75 result += "global,";
76 if (Flags & SymbolRef::SF_Weak)
77 result += "weak,";
78 if (Flags & SymbolRef::SF_Absolute)
79 result += "absolute,";
80 if (Flags & SymbolRef::SF_ThreadLocal)
81 result += "threadlocal,";
82 if (Flags & SymbolRef::SF_Common)
83 result += "common,";
84 if (Flags & SymbolRef::SF_FormatSpecific)
85 result += "formatspecific,";
86
87 // Remove trailing comma
88 if (result.size() > 0) {
89 result.erase(result.size() - 1);
90 }
91 return result;
92}
93
Rafael Espindolaad784792012-12-31 15:45:31 +000094static void checkError(error_code ec, const char *msg) {
95 if (ec)
96 report_fatal_error(std::string(msg) + ": " + ec.message());
97}
98
Rafael Espindolafc738472012-12-31 16:29:44 +000099static std::string getSectionFlagStr(const SectionRef &Section) {
100 const struct {
101 error_code (SectionRef::*MemF)(bool &) const;
102 const char *FlagStr, *ErrorStr;
103 } Work[] =
104 {{ &SectionRef::isText, "text,", "Section.isText() failed" },
105 { &SectionRef::isData, "data,", "Section.isData() failed" },
106 { &SectionRef::isBSS, "bss,", "Section.isBSS() failed" },
107 { &SectionRef::isRequiredForExecution, "required,",
108 "Section.isRequiredForExecution() failed" },
109 { &SectionRef::isVirtual, "virtual,", "Section.isVirtual() failed" },
110 { &SectionRef::isZeroInit, "zeroinit,", "Section.isZeroInit() failed" },
111 { &SectionRef::isReadOnlyData, "rodata,",
112 "Section.isReadOnlyData() failed" }};
113
114 std::string result;
115 for (uint32_t I = 0; I < sizeof(Work)/sizeof(*Work); ++I) {
116 bool B;
117 checkError((Section.*Work[I].MemF)(B), Work[I].ErrorStr);
118 if (B)
119 result += Work[I].FlagStr;
120 }
121
122 // Remove trailing comma
123 if (result.size() > 0) {
124 result.erase(result.size() - 1);
125 }
126 return result;
127}
128
Rafael Espindola663cebc2012-12-31 15:27:42 +0000129static void
130dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
Rafael Espindola1318e142012-12-31 15:30:58 +0000131 StringRef Name;
132 SymbolRef::Type Type;
133 uint32_t Flags;
134 uint64_t Address;
135 uint64_t Size;
136 uint64_t FileOffset;
Rafael Espindolaad784792012-12-31 15:45:31 +0000137 checkError(Sym.getName(Name), "SymbolRef.getName() failed");
138 checkError(Sym.getAddress(Address), "SymbolRef.getAddress() failed");
139 checkError(Sym.getSize(Size), "SymbolRef.getSize() failed");
140 checkError(Sym.getFileOffset(FileOffset),
141 "SymbolRef.getFileOffset() failed");
142 checkError(Sym.getType(Type), "SymbolRef.getType() failed");
143 checkError(Sym.getFlags(Flags), "SymbolRef.getFlags() failed");
Rafael Espindola1318e142012-12-31 15:30:58 +0000144 std::string FullName = Name;
David Meyer2d70e262012-03-09 20:59:52 +0000145
Rafael Espindola1318e142012-12-31 15:30:58 +0000146 // If this is a dynamic symbol from an ELF object, append
147 // the symbol's version to the name.
148 if (IsDynamic && obj->isELF()) {
149 StringRef Version;
150 bool IsDefault;
151 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
152 if (!Version.empty()) {
153 FullName += (IsDefault ? "@@" : "@");
154 FullName += Version;
David Meyer2d70e262012-03-09 20:59:52 +0000155 }
Rafael Espindola1318e142012-12-31 15:30:58 +0000156 }
David Meyer5c2b4ea2012-03-01 01:36:50 +0000157
Rafael Espindola1318e142012-12-31 15:30:58 +0000158 // format() can't handle StringRefs
159 outs() << format(" %-32s", FullName.c_str())
160 << format(" %-4s", getTypeStr(Type))
161 << format(" %16" PRIx64, Address)
162 << format(" %16" PRIx64, Size)
163 << format(" %16" PRIx64, FileOffset)
164 << " " << getSymbolFlagStr(Flags)
165 << "\n";
David Meyer5c2b4ea2012-03-01 01:36:50 +0000166}
167
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000168static void dumpStaticSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
169 return dumpSymbol(Sym, obj, false);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000170}
171
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000172static void dumpDynamicSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
173 return dumpSymbol(Sym, obj, true);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000174}
175
Rafael Espindolafc738472012-12-31 16:29:44 +0000176static void dumpSection(const SectionRef &Section, const ObjectFile *obj) {
177 StringRef Name;
178 checkError(Section.getName(Name), "SectionRef::getName() failed");
179 uint64_t Addr, Size, Align;
180 checkError(Section.getAddress(Addr), "SectionRef::getAddress() failed");
181 checkError(Section.getSize(Size), "SectionRef::getSize() failed");
182 checkError(Section.getAlignment(Align), "SectionRef::getAlignment() failed");
183 outs() << format(" %-24s", std::string(Name).c_str())
184 << format(" %16" PRIx64, Addr)
185 << format(" %16" PRIx64, Size)
186 << format(" %8" PRIx64, Align)
187 << " " << getSectionFlagStr(Section)
188 << "\n";
189}
190
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000191static void dumpLibrary(const LibraryRef &lib, const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000192 StringRef path;
193 lib.getPath(path);
194 outs() << " " << path << "\n";
195}
196
Rafael Espindolafc738472012-12-31 16:29:44 +0000197template<typename Iterator, typename Func>
198static void dump(const ObjectFile *obj, Func f, Iterator begin, Iterator end,
199 const char *errStr) {
200 error_code ec;
201 uint32_t count = 0;
202 Iterator it = begin, ie = end;
203 while (it != ie) {
204 f(*it, obj);
205 it.increment(ec);
206 if (ec)
207 report_fatal_error(errStr);
208 ++count;
209 }
210 outs() << " Total: " << count << "\n\n";
211}
212
Rafael Espindola663cebc2012-12-31 15:27:42 +0000213static void dumpHeaders(const ObjectFile *obj) {
David Meyer97f77872012-03-01 22:19:54 +0000214 outs() << "File Format : " << obj->getFileFormatName() << "\n";
215 outs() << "Arch : "
216 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
217 << "\n";
218 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
219 outs() << "Load Name : " << obj->getLoadName() << "\n";
220 outs() << "\n";
221}
222
David Meyer5c2b4ea2012-03-01 01:36:50 +0000223int main(int argc, char** argv) {
224 error_code ec;
225 sys::PrintStackTraceOnErrorSignal();
226 PrettyStackTraceProgram X(argc, argv);
227
228 cl::ParseCommandLineOptions(argc, argv,
229 "LLVM Object Reader\n");
230
231 if (InputFilename.empty()) {
232 errs() << "Please specify an input filename\n";
233 return 1;
234 }
235
236 // Open the object file
237 OwningPtr<MemoryBuffer> File;
238 if (MemoryBuffer::getFile(InputFilename, File)) {
239 errs() << InputFilename << ": Open failed\n";
240 return 1;
241 }
242
Michael J. Spencerdc0f8a32013-02-08 01:05:48 +0000243 OwningPtr<ObjectFile> o(ObjectFile::createObjectFile(File.take()));
244 ObjectFile *obj = o.get();
David Meyer5c2b4ea2012-03-01 01:36:50 +0000245 if (!obj) {
246 errs() << InputFilename << ": Object type not recognized\n";
247 }
248
Rafael Espindola663cebc2012-12-31 15:27:42 +0000249 dumpHeaders(obj);
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000250
251 outs() << "Symbols:\n";
252 dumpSymbolHeader();
253 dump(obj, dumpStaticSymbol, obj->begin_symbols(), obj->end_symbols(),
254 "Symbol iteration failed");
255
256 outs() << "Dynamic Symbols:\n";
257 dumpSymbolHeader();
258 dump(obj, dumpDynamicSymbol, obj->begin_dynamic_symbols(),
259 obj->end_dynamic_symbols(), "Symbol iteration failed");
Rafael Espindolafc738472012-12-31 16:29:44 +0000260
261 outs() << "Sections:\n";
262 dumpSectionHeader();
263 dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(),
264 "Section iteration failed");
265
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000266 outs() << "Libraries needed:\n";
267 dump(obj, &dumpLibrary, obj->begin_libraries_needed(),
268 obj->end_libraries_needed(), "Needed libraries iteration failed");
269
David Meyer5c2b4ea2012-03-01 01:36:50 +0000270 return 0;
271}
272