1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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.
18 // Output should be specialized for each format where appropriate.
20 //===----------------------------------------------------------------------===//
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"
34 using namespace llvm::object;
36 static cl::opt<std::string>
37 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
39 static 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")
49 static 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")
58 static const char *getTypeStr(SymbolRef::Type 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 "-";
70 static std::string getSymbolFlagStr(uint32_t Flags) {
72 if (Flags & SymbolRef::SF_Undefined)
74 if (Flags & SymbolRef::SF_Global)
76 if (Flags & SymbolRef::SF_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)
84 if (Flags & SymbolRef::SF_FormatSpecific)
85 result += "formatspecific,";
87 // Remove trailing comma
88 if (result.size() > 0) {
89 result.erase(result.size() - 1);
94 static void checkError(error_code ec, const char *msg) {
96 report_fatal_error(std::string(msg) + ": " + ec.message());
99 static std::string getSectionFlagStr(const SectionRef &Section) {
101 error_code (SectionRef::*MemF)(bool &) const;
102 const char *FlagStr, *ErrorStr;
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" }};
115 for (uint32_t I = 0; I < sizeof(Work)/sizeof(*Work); ++I) {
117 checkError((Section.*Work[I].MemF)(B), Work[I].ErrorStr);
119 result += Work[I].FlagStr;
122 // Remove trailing comma
123 if (result.size() > 0) {
124 result.erase(result.size() - 1);
130 dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
132 SymbolRef::Type Type;
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;
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()) {
151 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
152 if (!Version.empty()) {
153 FullName += (IsDefault ? "@@" : "@");
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)
168 static void dumpStaticSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
169 return dumpSymbol(Sym, obj, false);
172 static void dumpDynamicSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
173 return dumpSymbol(Sym, obj, true);
176 static void dumpSection(const SectionRef &Section, const ObjectFile *obj) {
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)
191 static void dumpLibrary(const LibraryRef &lib, const ObjectFile *obj) {
194 outs() << " " << path << "\n";
197 template<typename Iterator, typename Func>
198 static void dump(const ObjectFile *obj, Func f, Iterator begin, Iterator end,
199 const char *errStr) {
202 Iterator it = begin, ie = end;
207 report_fatal_error(errStr);
210 outs() << " Total: " << count << "\n\n";
213 static void dumpHeaders(const ObjectFile *obj) {
214 outs() << "File Format : " << obj->getFileFormatName() << "\n";
216 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
218 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
219 outs() << "Load Name : " << obj->getLoadName() << "\n";
223 int main(int argc, char** argv) {
225 sys::PrintStackTraceOnErrorSignal();
226 PrettyStackTraceProgram X(argc, argv);
228 cl::ParseCommandLineOptions(argc, argv,
229 "LLVM Object Reader\n");
231 if (InputFilename.empty()) {
232 errs() << "Please specify an input filename\n";
236 // Open the object file
237 OwningPtr<MemoryBuffer> File;
238 if (MemoryBuffer::getFile(InputFilename, File)) {
239 errs() << InputFilename << ": Open failed\n";
243 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
245 errs() << InputFilename << ": Object type not recognized\n";
250 outs() << "Symbols:\n";
252 dump(obj, dumpStaticSymbol, obj->begin_symbols(), obj->end_symbols(),
253 "Symbol iteration failed");
255 outs() << "Dynamic Symbols:\n";
257 dump(obj, dumpDynamicSymbol, obj->begin_dynamic_symbols(),
258 obj->end_dynamic_symbols(), "Symbol iteration failed");
260 outs() << "Sections:\n";
262 dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(),
263 "Section iteration failed");
265 outs() << "Libraries needed:\n";
266 dump(obj, &dumpLibrary, obj->begin_libraries_needed(),
267 obj->end_libraries_needed(), "Needed libraries iteration failed");