llvm-readobj.cpp revision dc0f8a3fd993f5db67e121b0e2c132ac4d104a24
1//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
2//
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//
10// 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.
13//
14// 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.
17//
18// Output should be specialized for each format where appropriate.
19//
20//===----------------------------------------------------------------------===//
21
22#include "llvm/ADT/Triple.h"
23#include "llvm/Analysis/Verifier.h"
24#include "llvm/Object/ELF.h"
25#include "llvm/Object/ObjectFile.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/Format.h"
29#include "llvm/Support/FormattedStream.h"
30#include "llvm/Support/PrettyStackTrace.h"
31#include "llvm/Support/Signals.h"
32
33using namespace llvm;
34using namespace llvm::object;
35
36static cl::opt<std::string>
37InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
38
39static 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
49static 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
58static const char *getTypeStr(SymbolRef::Type Type) {
59  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
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  OwningPtr<ObjectFile> o(ObjectFile::createObjectFile(File.take()));
244  ObjectFile *obj = o.get();
245  if (!obj) {
246    errs() << InputFilename << ": Object type not recognized\n";
247  }
248
249  dumpHeaders(obj);
250
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");
260
261  outs() << "Sections:\n";
262  dumpSectionHeader();
263  dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(),
264       "Section iteration failed");
265
266  outs() << "Libraries needed:\n";
267  dump(obj, &dumpLibrary, obj->begin_libraries_needed(),
268       obj->end_libraries_needed(), "Needed libraries iteration failed");
269
270  return 0;
271}
272
273