llvm-readobj.cpp revision dd3aa9eab2b40ea1573a5482472bbb89e6f1038d
15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// License. See LICENSE.TXT for details.
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//===----------------------------------------------------------------------===//
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// This is a tool similar to readelf, except it works on multiple object file
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// formats. The main purpose of this tool is to provide detailed output suitable
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// for FileCheck.
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Flags should be similar to readelf where supported, but the output format
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// does not need to be identical. The point is to not make users learn yet
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// another set of flags.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Output should be specialized for each format where appropriate.
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//===----------------------------------------------------------------------===//
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/ADT/Triple.h"
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Analysis/Verifier.h"
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Object/ELF.h"
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Object/ObjectFile.h"
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Support/CommandLine.h"
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Support/Debug.h"
28f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include "llvm/Support/Format.h"
29f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include "llvm/Support/FormattedStream.h"
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Support/PrettyStackTrace.h"
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Support/Signals.h"
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using namespace llvm;
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdochusing namespace llvm::object;
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)static cl::opt<std::string>
37197021e6b966cfb06891637935ef33fff06433d1Ben MurdochInputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)static void dumpSymbolHeader() {
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  outs() << format("  %-32s", (const char*)"Name")
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-4s", (const char*)"Type")
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-16s", (const char*)"Address")
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-16s", (const char*)"Size")
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-16s", (const char*)"FileOffset")
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-26s", (const char*)"Flags")
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << "\n";
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)static void dumpSectionHeader() {
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  outs() << format("  %-24s", (const char*)"Name")
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-16s", (const char*)"Address")
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-16s", (const char*)"Size")
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-8s", (const char*)"Align")
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << format("  %-26s", (const char*)"Flags")
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)         << "\n";
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)static const char *getTypeStr(SymbolRef::Type Type) {
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  switch (Type) {
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case SymbolRef::ST_Unknown: return "?";
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case SymbolRef::ST_Data: return "DATA";
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case SymbolRef::ST_Debug: return "DBG";
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case SymbolRef::ST_File: return "FILE";
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case SymbolRef::ST_Function: return "FUNC";
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case SymbolRef::ST_Other: return "-";
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  }
67  return "INV";
68}
69
70static std::string getSymbolFlagStr(uint32_t Flags) {
71  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
94static void checkError(error_code ec, const char *msg) {
95  if (ec)
96    report_fatal_error(std::string(msg) + ": " + ec.message());
97}
98
99static 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
129static void
130dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
131  StringRef Name;
132  SymbolRef::Type Type;
133  uint32_t Flags;
134  uint64_t Address;
135  uint64_t Size;
136  uint64_t FileOffset;
137  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");
144  std::string FullName = Name;
145
146  // 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;
155    }
156  }
157
158  // 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";
166}
167
168static void dumpStaticSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
169  return dumpSymbol(Sym, obj, false);
170}
171
172static void dumpDynamicSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
173  return dumpSymbol(Sym, obj, true);
174}
175
176static 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
191static void dumpLibrary(const LibraryRef &lib, const ObjectFile *obj) {
192  StringRef path;
193  lib.getPath(path);
194  outs() << "  " << path << "\n";
195}
196
197template<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
213static void dumpHeaders(const ObjectFile *obj) {
214  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
223int 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
243  ObjectFile *obj = ObjectFile::createObjectFile(File.take());
244  if (!obj) {
245    errs() << InputFilename << ": Object type not recognized\n";
246  }
247
248  dumpHeaders(obj);
249
250  outs() << "Symbols:\n";
251  dumpSymbolHeader();
252  dump(obj, dumpStaticSymbol, obj->begin_symbols(), obj->end_symbols(),
253       "Symbol iteration failed");
254
255  outs() << "Dynamic Symbols:\n";
256  dumpSymbolHeader();
257  dump(obj, dumpDynamicSymbol, obj->begin_dynamic_symbols(),
258       obj->end_dynamic_symbols(), "Symbol iteration failed");
259
260  outs() << "Sections:\n";
261  dumpSectionHeader();
262  dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(),
263       "Section iteration failed");
264
265  outs() << "Libraries needed:\n";
266  dump(obj, &dumpLibrary, obj->begin_libraries_needed(),
267       obj->end_libraries_needed(), "Needed libraries iteration failed");
268
269  return 0;
270}
271
272