llvm-readobj.cpp revision 87b47ccc5afcdffb1fa7f04b27fca926ec7fb344
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details.
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This program is a utility that works like traditional Unix "readelf",
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// except that it can handle any type of object file recognized by lib/Object.
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// It makes use of the generic ObjectFile interface.
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Caution: This utility is new, experimental, unsupported, and incomplete.
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/Triple.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Analysis/Verifier.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Object/ELF.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Object/ObjectFile.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/CommandLine.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Debug.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Format.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/FormattedStream.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/PrettyStackTrace.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Signals.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using namespace llvm;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using namespace llvm::object;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static cl::opt<std::string>
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void dumpSymbolHeader() {
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  outs() << format("  %-32s", (const char*)"Name")
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-4s", (const char*)"Type")
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-16s", (const char*)"Address")
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-16s", (const char*)"Size")
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-16s", (const char*)"FileOffset")
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-26s", (const char*)"Flags")
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << "\n";
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void dumpSectionHeader() {
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  outs() << format("  %-24s", (const char*)"Name")
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-16s", (const char*)"Address")
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-16s", (const char*)"Size")
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-8s", (const char*)"Align")
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << format("  %-26s", (const char*)"Flags")
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         << "\n";
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const char *getTypeStr(SymbolRef::Type Type) {
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (Type) {
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case SymbolRef::ST_Unknown: return "?";
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case SymbolRef::ST_Data: return "DATA";
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case SymbolRef::ST_Debug: return "DBG";
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case SymbolRef::ST_File: return "FILE";
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case SymbolRef::ST_Function: return "FUNC";
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case SymbolRef::ST_Other: return "-";
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return "INV";
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
66
67static std::string getSymbolFlagStr(uint32_t Flags) {
68  std::string result;
69  if (Flags & SymbolRef::SF_Undefined)
70    result += "undef,";
71  if (Flags & SymbolRef::SF_Global)
72    result += "global,";
73  if (Flags & SymbolRef::SF_Weak)
74    result += "weak,";
75  if (Flags & SymbolRef::SF_Absolute)
76    result += "absolute,";
77  if (Flags & SymbolRef::SF_ThreadLocal)
78    result += "threadlocal,";
79  if (Flags & SymbolRef::SF_Common)
80    result += "common,";
81  if (Flags & SymbolRef::SF_FormatSpecific)
82    result += "formatspecific,";
83
84  // Remove trailing comma
85  if (result.size() > 0) {
86    result.erase(result.size() - 1);
87  }
88  return result;
89}
90
91static void checkError(error_code ec, const char *msg) {
92  if (ec)
93    report_fatal_error(std::string(msg) + ": " + ec.message());
94}
95
96static std::string getSectionFlagStr(const SectionRef &Section) {
97  const struct {
98    error_code (SectionRef::*MemF)(bool &) const;
99    const char *FlagStr, *ErrorStr;
100  } Work[] =
101      {{ &SectionRef::isText, "text,", "Section.isText() failed" },
102       { &SectionRef::isData, "data,", "Section.isData() failed" },
103       { &SectionRef::isBSS, "bss,", "Section.isBSS() failed"  },
104       { &SectionRef::isRequiredForExecution, "required,",
105         "Section.isRequiredForExecution() failed" },
106       { &SectionRef::isVirtual, "virtual,", "Section.isVirtual() failed" },
107       { &SectionRef::isZeroInit, "zeroinit,", "Section.isZeroInit() failed" },
108       { &SectionRef::isReadOnlyData, "rodata,",
109         "Section.isReadOnlyData() failed" }};
110
111  std::string result;
112  for (uint32_t I = 0; I < sizeof(Work)/sizeof(*Work); ++I) {
113    bool B;
114    checkError((Section.*Work[I].MemF)(B), Work[I].ErrorStr);
115    if (B)
116      result += Work[I].FlagStr;
117  }
118
119  // Remove trailing comma
120  if (result.size() > 0) {
121    result.erase(result.size() - 1);
122  }
123  return result;
124}
125
126static void
127dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
128  StringRef Name;
129  SymbolRef::Type Type;
130  uint32_t Flags;
131  uint64_t Address;
132  uint64_t Size;
133  uint64_t FileOffset;
134  checkError(Sym.getName(Name), "SymbolRef.getName() failed");
135  checkError(Sym.getAddress(Address), "SymbolRef.getAddress() failed");
136  checkError(Sym.getSize(Size), "SymbolRef.getSize() failed");
137  checkError(Sym.getFileOffset(FileOffset),
138             "SymbolRef.getFileOffset() failed");
139  checkError(Sym.getType(Type), "SymbolRef.getType() failed");
140  checkError(Sym.getFlags(Flags), "SymbolRef.getFlags() failed");
141  std::string FullName = Name;
142
143  // If this is a dynamic symbol from an ELF object, append
144  // the symbol's version to the name.
145  if (IsDynamic && obj->isELF()) {
146    StringRef Version;
147    bool IsDefault;
148    GetELFSymbolVersion(obj, Sym, Version, IsDefault);
149    if (!Version.empty()) {
150      FullName += (IsDefault ? "@@" : "@");
151      FullName += Version;
152    }
153  }
154
155  // format() can't handle StringRefs
156  outs() << format("  %-32s", FullName.c_str())
157         << format("  %-4s", getTypeStr(Type))
158         << format("  %16" PRIx64, Address)
159         << format("  %16" PRIx64, Size)
160         << format("  %16" PRIx64, FileOffset)
161         << "  " << getSymbolFlagStr(Flags)
162         << "\n";
163}
164
165static void dumpStaticSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
166  return dumpSymbol(Sym, obj, false);
167}
168
169static void dumpDynamicSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
170  return dumpSymbol(Sym, obj, true);
171}
172
173static void dumpSection(const SectionRef &Section, const ObjectFile *obj) {
174  StringRef Name;
175  checkError(Section.getName(Name), "SectionRef::getName() failed");
176  uint64_t Addr, Size, Align;
177  checkError(Section.getAddress(Addr), "SectionRef::getAddress() failed");
178  checkError(Section.getSize(Size), "SectionRef::getSize() failed");
179  checkError(Section.getAlignment(Align), "SectionRef::getAlignment() failed");
180  outs() << format("  %-24s", std::string(Name).c_str())
181         << format("  %16" PRIx64, Addr)
182         << format("  %16" PRIx64, Size)
183         << format("  %8" PRIx64, Align)
184         << "  " << getSectionFlagStr(Section)
185         << "\n";
186}
187
188static void dumpLibrary(const LibraryRef &lib, const ObjectFile *obj) {
189  StringRef path;
190  lib.getPath(path);
191  outs() << "  " << path << "\n";
192}
193
194template<typename Iterator, typename Func>
195static void dump(const ObjectFile *obj, Func f, Iterator begin, Iterator end,
196                 const char *errStr) {
197  error_code ec;
198  uint32_t count = 0;
199  Iterator it = begin, ie = end;
200  while (it != ie) {
201    f(*it, obj);
202    it.increment(ec);
203    if (ec)
204      report_fatal_error(errStr);
205    ++count;
206  }
207  outs() << "  Total: " << count << "\n\n";
208}
209
210static void dumpHeaders(const ObjectFile *obj) {
211  outs() << "File Format : " << obj->getFileFormatName() << "\n";
212  outs() << "Arch        : "
213         << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
214         << "\n";
215  outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
216  outs() << "Load Name   : " << obj->getLoadName() << "\n";
217  outs() << "\n";
218}
219
220int main(int argc, char** argv) {
221  error_code ec;
222  sys::PrintStackTraceOnErrorSignal();
223  PrettyStackTraceProgram X(argc, argv);
224
225  cl::ParseCommandLineOptions(argc, argv,
226                              "LLVM Object Reader\n");
227
228  if (InputFilename.empty()) {
229    errs() << "Please specify an input filename\n";
230    return 1;
231  }
232
233  // Open the object file
234  OwningPtr<MemoryBuffer> File;
235  if (MemoryBuffer::getFile(InputFilename, File)) {
236    errs() << InputFilename << ": Open failed\n";
237    return 1;
238  }
239
240  ObjectFile *obj = ObjectFile::createObjectFile(File.take());
241  if (!obj) {
242    errs() << InputFilename << ": Object type not recognized\n";
243  }
244
245  dumpHeaders(obj);
246
247  outs() << "Symbols:\n";
248  dumpSymbolHeader();
249  dump(obj, dumpStaticSymbol, obj->begin_symbols(), obj->end_symbols(),
250       "Symbol iteration failed");
251
252  outs() << "Dynamic Symbols:\n";
253  dumpSymbolHeader();
254  dump(obj, dumpDynamicSymbol, obj->begin_dynamic_symbols(),
255       obj->end_dynamic_symbols(), "Symbol iteration failed");
256
257  outs() << "Sections:\n";
258  dumpSectionHeader();
259  dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(),
260       "Section iteration failed");
261
262  outs() << "Libraries needed:\n";
263  dump(obj, &dumpLibrary, obj->begin_libraries_needed(),
264       obj->end_libraries_needed(), "Needed libraries iteration failed");
265
266  return 0;
267}
268
269