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-readobj.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/Analysis/Verifier.h"
26 #include "llvm/Object/ELF.h"
27 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/PrettyStackTrace.h"
33 #include "llvm/Support/Signals.h"
36 using namespace llvm::object;
38 static cl::opt<std::string>
39 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
41 static void dumpSymbolHeader() {
42 outs() << format(" %-32s", (const char *)"Name")
43 << format(" %-4s", (const char *)"Type")
44 << format(" %-4s", (const char *)"Section")
45 << format(" %-16s", (const char *)"Address")
46 << format(" %-16s", (const char *)"Size")
47 << format(" %-16s", (const char *)"FileOffset")
48 << format(" %-26s", (const char *)"Flags") << "\n";
51 static 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")
60 static const char *getTypeStr(SymbolRef::Type 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 "-";
72 static std::string getSymbolFlagStr(uint32_t Flags) {
74 if (Flags & SymbolRef::SF_Undefined)
76 if (Flags & SymbolRef::SF_Global)
78 if (Flags & SymbolRef::SF_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)
86 if (Flags & SymbolRef::SF_FormatSpecific)
87 result += "formatspecific,";
89 // Remove trailing comma
90 if (result.size() > 0) {
91 result.erase(result.size() - 1);
96 static void checkError(error_code ec, const char *msg) {
98 report_fatal_error(std::string(msg) + ": " + ec.message());
101 static std::string getSectionFlagStr(const SectionRef &Section) {
103 error_code (SectionRef::*MemF)(bool &) const;
104 const char *FlagStr, *ErrorStr;
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" }};
117 for (uint32_t I = 0; I < sizeof(Work)/sizeof(*Work); ++I) {
119 checkError((Section.*Work[I].MemF)(B), Work[I].ErrorStr);
121 result += Work[I].FlagStr;
124 // Remove trailing comma
125 if (result.size() > 0) {
126 result.erase(result.size() - 1);
132 dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
134 SymbolRef::Type Type;
139 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");
146 std::string FullName = Name;
148 llvm::object::section_iterator symSection(obj->begin_sections());
149 Sym.getSection(symSection);
150 StringRef sectionName;
152 if (symSection != obj->end_sections())
153 checkError(symSection->getName(sectionName),
154 "SectionRef::getName() failed");
156 // If this is a dynamic symbol from an ELF object, append
157 // the symbol's version to the name.
158 if (IsDynamic && obj->isELF()) {
161 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
162 if (!Version.empty()) {
163 FullName += (IsDefault ? "@@" : "@");
168 // format() can't handle StringRefs
169 outs() << format(" %-32s", FullName.c_str())
170 << format(" %-4s", getTypeStr(Type))
171 << format(" %-32s", std::string(sectionName).c_str())
172 << format(" %16" PRIx64, Address) << format(" %16" PRIx64, Size)
173 << format(" %16" PRIx64, FileOffset) << " "
174 << getSymbolFlagStr(Flags) << "\n";
177 static void dumpStaticSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
178 return dumpSymbol(Sym, obj, false);
181 static void dumpDynamicSymbol(const SymbolRef &Sym, const ObjectFile *obj) {
182 return dumpSymbol(Sym, obj, true);
185 static void dumpSection(const SectionRef &Section, const ObjectFile *obj) {
187 checkError(Section.getName(Name), "SectionRef::getName() failed");
188 uint64_t Addr, Size, Align;
189 checkError(Section.getAddress(Addr), "SectionRef::getAddress() failed");
190 checkError(Section.getSize(Size), "SectionRef::getSize() failed");
191 checkError(Section.getAlignment(Align), "SectionRef::getAlignment() failed");
192 outs() << format(" %-24s", std::string(Name).c_str())
193 << format(" %16" PRIx64, Addr)
194 << format(" %16" PRIx64, Size)
195 << format(" %8" PRIx64, Align)
196 << " " << getSectionFlagStr(Section)
200 static void dumpLibrary(const LibraryRef &lib, const ObjectFile *obj) {
203 outs() << " " << path << "\n";
206 template<typename Iterator, typename Func>
207 static void dump(const ObjectFile *obj, Func f, Iterator begin, Iterator end,
208 const char *errStr) {
211 Iterator it = begin, ie = end;
216 report_fatal_error(errStr);
219 outs() << " Total: " << count << "\n\n";
222 static void dumpHeaders(const ObjectFile *obj) {
223 outs() << "File Format : " << obj->getFileFormatName() << "\n";
225 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
227 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
228 outs() << "Load Name : " << obj->getLoadName() << "\n";
232 int main(int argc, char** argv) {
234 sys::PrintStackTraceOnErrorSignal();
235 PrettyStackTraceProgram X(argc, argv);
237 cl::ParseCommandLineOptions(argc, argv,
238 "LLVM Object Reader\n");
240 if (InputFilename.empty()) {
241 errs() << "Please specify an input filename\n";
245 // Open the object file
246 OwningPtr<MemoryBuffer> File;
247 if (MemoryBuffer::getFile(InputFilename, File)) {
248 errs() << InputFilename << ": Open failed\n";
252 OwningPtr<ObjectFile> o(ObjectFile::createObjectFile(File.take()));
253 ObjectFile *obj = o.get();
255 errs() << InputFilename << ": Object type not recognized\n";
260 outs() << "Symbols:\n";
262 dump(obj, dumpStaticSymbol, obj->begin_symbols(), obj->end_symbols(),
263 "Symbol iteration failed");
265 outs() << "Dynamic Symbols:\n";
267 dump(obj, dumpDynamicSymbol, obj->begin_dynamic_symbols(),
268 obj->end_dynamic_symbols(), "Symbol iteration failed");
270 outs() << "Sections:\n";
272 dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(),
273 "Section iteration failed");
276 if (ErrorOr<void> e = dumpELFDynamicTable(obj, outs()))
279 errs() << "InputFilename" << ": " << error_code(e).message() << "\n";
282 outs() << "Libraries needed:\n";
283 dump(obj, &dumpLibrary, obj->begin_libraries_needed(),
284 obj->end_libraries_needed(), "Needed libraries iteration failed");