blob: 8f0917fc91a580900b55c8ba7e35a444cf6e2aa5 [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
Michael J. Spencerd326d052013-02-20 02:37:12 +000022#include "llvm-readobj.h"
23
David Meyer97f77872012-03-01 22:19:54 +000024#include "llvm/ADT/Triple.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000025#include "llvm/Analysis/Verifier.h"
26#include "llvm/Object/ELF.h"
27#include "llvm/Object/ObjectFile.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000028#include "llvm/Support/CommandLine.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000029#include "llvm/Support/Debug.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000030#include "llvm/Support/Format.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000031#include "llvm/Support/FormattedStream.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000032#include "llvm/Support/PrettyStackTrace.h"
33#include "llvm/Support/Signals.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000034
35using namespace llvm;
36using namespace llvm::object;
37
38static cl::opt<std::string>
39InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
40
Rafael Espindola148ee4f2012-12-31 16:05:21 +000041static void dumpSymbolHeader() {
42 outs() << format(" %-32s", (const char*)"Name")
43 << format(" %-4s", (const char*)"Type")
44 << format(" %-16s", (const char*)"Address")
45 << format(" %-16s", (const char*)"Size")
46 << format(" %-16s", (const char*)"FileOffset")
47 << format(" %-26s", (const char*)"Flags")
48 << "\n";
49}
50
Rafael Espindolafc738472012-12-31 16:29:44 +000051static void dumpSectionHeader() {
52 outs() << format(" %-24s", (const char*)"Name")
53 << format(" %-16s", (const char*)"Address")
54 << format(" %-16s", (const char*)"Size")
55 << format(" %-8s", (const char*)"Align")
56 << format(" %-26s", (const char*)"Flags")
57 << "\n";
58}
59
Rafael Espindola663cebc2012-12-31 15:27:42 +000060static const char *getTypeStr(SymbolRef::Type Type) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000061 switch (Type) {
62 case SymbolRef::ST_Unknown: return "?";
63 case SymbolRef::ST_Data: return "DATA";
64 case SymbolRef::ST_Debug: return "DBG";
65 case SymbolRef::ST_File: return "FILE";
66 case SymbolRef::ST_Function: return "FUNC";
67 case SymbolRef::ST_Other: return "-";
68 }
69 return "INV";
70}
71
Rafael Espindola663cebc2012-12-31 15:27:42 +000072static std::string getSymbolFlagStr(uint32_t Flags) {
David Meyer5c2b4ea2012-03-01 01:36:50 +000073 std::string result;
74 if (Flags & SymbolRef::SF_Undefined)
75 result += "undef,";
76 if (Flags & SymbolRef::SF_Global)
77 result += "global,";
78 if (Flags & SymbolRef::SF_Weak)
79 result += "weak,";
80 if (Flags & SymbolRef::SF_Absolute)
81 result += "absolute,";
82 if (Flags & SymbolRef::SF_ThreadLocal)
83 result += "threadlocal,";
84 if (Flags & SymbolRef::SF_Common)
85 result += "common,";
86 if (Flags & SymbolRef::SF_FormatSpecific)
87 result += "formatspecific,";
88
89 // Remove trailing comma
90 if (result.size() > 0) {
91 result.erase(result.size() - 1);
92 }
93 return result;
94}
95
Rafael Espindolaad784792012-12-31 15:45:31 +000096static void checkError(error_code ec, const char *msg) {
97 if (ec)
98 report_fatal_error(std::string(msg) + ": " + ec.message());
99}
100
Rafael Espindolafc738472012-12-31 16:29:44 +0000101static std::string getSectionFlagStr(const SectionRef &Section) {
102 const struct {
103 error_code (SectionRef::*MemF)(bool &) const;
104 const char *FlagStr, *ErrorStr;
105 } Work[] =
106 {{ &SectionRef::isText, "text,", "Section.isText() failed" },
107 { &SectionRef::isData, "data,", "Section.isData() failed" },
108 { &SectionRef::isBSS, "bss,", "Section.isBSS() failed" },
109 { &SectionRef::isRequiredForExecution, "required,",
110 "Section.isRequiredForExecution() failed" },
111 { &SectionRef::isVirtual, "virtual,", "Section.isVirtual() failed" },
112 { &SectionRef::isZeroInit, "zeroinit,", "Section.isZeroInit() failed" },
113 { &SectionRef::isReadOnlyData, "rodata,",
114 "Section.isReadOnlyData() failed" }};
115
116 std::string result;
117 for (uint32_t I = 0; I < sizeof(Work)/sizeof(*Work); ++I) {
118 bool B;
119 checkError((Section.*Work[I].MemF)(B), Work[I].ErrorStr);
120 if (B)
121 result += Work[I].FlagStr;
122 }
123
124 // Remove trailing comma
125 if (result.size() > 0) {
126 result.erase(result.size() - 1);
127 }
128 return result;
129}
130
Rafael Espindola663cebc2012-12-31 15:27:42 +0000131static void
132dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
Rafael Espindola1318e142012-12-31 15:30:58 +0000133 StringRef Name;
134 SymbolRef::Type Type;
135 uint32_t Flags;
136 uint64_t Address;
137 uint64_t Size;
138 uint64_t FileOffset;
Rafael Espindolaad784792012-12-31 15:45:31 +0000139 checkError(Sym.getName(Name), "SymbolRef.getName() failed");
140 checkError(Sym.getAddress(Address), "SymbolRef.getAddress() failed");
141 checkError(Sym.getSize(Size), "SymbolRef.getSize() failed");
142 checkError(Sym.getFileOffset(FileOffset),
143 "SymbolRef.getFileOffset() failed");
144 checkError(Sym.getType(Type), "SymbolRef.getType() failed");
145 checkError(Sym.getFlags(Flags), "SymbolRef.getFlags() failed");
Rafael Espindola1318e142012-12-31 15:30:58 +0000146 std::string FullName = Name;
David Meyer2d70e262012-03-09 20:59:52 +0000147
Rafael Espindola1318e142012-12-31 15:30:58 +0000148 // If this is a dynamic symbol from an ELF object, append
149 // the symbol's version to the name.
150 if (IsDynamic && obj->isELF()) {
151 StringRef Version;
152 bool IsDefault;
153 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
154 if (!Version.empty()) {
155 FullName += (IsDefault ? "@@" : "@");
156 FullName += Version;
David Meyer2d70e262012-03-09 20:59:52 +0000157 }
Rafael Espindola1318e142012-12-31 15:30:58 +0000158 }
David Meyer5c2b4ea2012-03-01 01:36:50 +0000159
Rafael Espindola1318e142012-12-31 15:30:58 +0000160 // format() can't handle StringRefs
161 outs() << format(" %-32s", FullName.c_str())
162 << format(" %-4s", getTypeStr(Type))
163 << format(" %16" PRIx64, Address)
164 << format(" %16" PRIx64, Size)
165 << format(" %16" PRIx64, FileOffset)
166 << " " << getSymbolFlagStr(Flags)
167 << "\n";
David Meyer5c2b4ea2012-03-01 01:36:50 +0000168}
169
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000170static void dumpStaticSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
171 return dumpSymbol(Sym, obj, false);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000172}
173
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000174static void dumpDynamicSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
175 return dumpSymbol(Sym, obj, true);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000176}
177
Rafael Espindolafc738472012-12-31 16:29:44 +0000178static void dumpSection(const SectionRef &Section, const ObjectFile *obj) {
179 StringRef Name;
180 checkError(Section.getName(Name), "SectionRef::getName() failed");
181 uint64_t Addr, Size, Align;
182 checkError(Section.getAddress(Addr), "SectionRef::getAddress() failed");
183 checkError(Section.getSize(Size), "SectionRef::getSize() failed");
184 checkError(Section.getAlignment(Align), "SectionRef::getAlignment() failed");
185 outs() << format(" %-24s", std::string(Name).c_str())
186 << format(" %16" PRIx64, Addr)
187 << format(" %16" PRIx64, Size)
188 << format(" %8" PRIx64, Align)
189 << " " << getSectionFlagStr(Section)
190 << "\n";
191}
192
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000193static void dumpLibrary(const LibraryRef &lib, const ObjectFile *obj) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000194 StringRef path;
195 lib.getPath(path);
196 outs() << " " << path << "\n";
197}
198
Rafael Espindolafc738472012-12-31 16:29:44 +0000199template<typename Iterator, typename Func>
200static void dump(const ObjectFile *obj, Func f, Iterator begin, Iterator end,
201 const char *errStr) {
202 error_code ec;
203 uint32_t count = 0;
204 Iterator it = begin, ie = end;
205 while (it != ie) {
206 f(*it, obj);
207 it.increment(ec);
208 if (ec)
209 report_fatal_error(errStr);
210 ++count;
211 }
212 outs() << " Total: " << count << "\n\n";
213}
214
Rafael Espindola663cebc2012-12-31 15:27:42 +0000215static void dumpHeaders(const ObjectFile *obj) {
David Meyer97f77872012-03-01 22:19:54 +0000216 outs() << "File Format : " << obj->getFileFormatName() << "\n";
217 outs() << "Arch : "
218 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
219 << "\n";
220 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
221 outs() << "Load Name : " << obj->getLoadName() << "\n";
222 outs() << "\n";
223}
224
David Meyer5c2b4ea2012-03-01 01:36:50 +0000225int main(int argc, char** argv) {
226 error_code ec;
227 sys::PrintStackTraceOnErrorSignal();
228 PrettyStackTraceProgram X(argc, argv);
229
230 cl::ParseCommandLineOptions(argc, argv,
231 "LLVM Object Reader\n");
232
233 if (InputFilename.empty()) {
234 errs() << "Please specify an input filename\n";
235 return 1;
236 }
237
238 // Open the object file
239 OwningPtr<MemoryBuffer> File;
240 if (MemoryBuffer::getFile(InputFilename, File)) {
241 errs() << InputFilename << ": Open failed\n";
242 return 1;
243 }
244
Michael J. Spencerdc0f8a32013-02-08 01:05:48 +0000245 OwningPtr<ObjectFile> o(ObjectFile::createObjectFile(File.take()));
246 ObjectFile *obj = o.get();
David Meyer5c2b4ea2012-03-01 01:36:50 +0000247 if (!obj) {
248 errs() << InputFilename << ": Object type not recognized\n";
249 }
250
Rafael Espindola663cebc2012-12-31 15:27:42 +0000251 dumpHeaders(obj);
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000252
253 outs() << "Symbols:\n";
254 dumpSymbolHeader();
255 dump(obj, dumpStaticSymbol, obj->begin_symbols(), obj->end_symbols(),
256 "Symbol iteration failed");
257
258 outs() << "Dynamic Symbols:\n";
259 dumpSymbolHeader();
260 dump(obj, dumpDynamicSymbol, obj->begin_dynamic_symbols(),
261 obj->end_dynamic_symbols(), "Symbol iteration failed");
Rafael Espindolafc738472012-12-31 16:29:44 +0000262
263 outs() << "Sections:\n";
264 dumpSectionHeader();
265 dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(),
266 "Section iteration failed");
267
Michael J. Spencerd326d052013-02-20 02:37:12 +0000268 if (obj->isELF()) {
269 if (ErrorOr<void> e = dumpELFDynamicTable(obj, outs()))
270 ;
271 else
272 errs() << "InputFilename" << ": " << error_code(e).message() << "\n";
273 }
274
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000275 outs() << "Libraries needed:\n";
276 dump(obj, &dumpLibrary, obj->begin_libraries_needed(),
277 obj->end_libraries_needed(), "Needed libraries iteration failed");
278
David Meyer5c2b4ea2012-03-01 01:36:50 +0000279 return 0;
280}
281