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 program is a utility that works like traditional Unix "readelf",
11 // except that it can handle any type of object file recognized by lib/Object.
13 // It makes use of the generic ObjectFile interface.
15 // Caution: This utility is new, experimental, unsupported, and incomplete.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/FormattedStream.h"
30 using namespace llvm::object;
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
35 void DumpSymbolHeader() {
36 outs() << format(" %-32s", (const char*)"Name")
37 << format(" %-4s", (const char*)"Type")
38 << format(" %-16s", (const char*)"Address")
39 << format(" %-16s", (const char*)"Size")
40 << format(" %-16s", (const char*)"FileOffset")
41 << format(" %-26s", (const char*)"Flags")
45 const char *GetTypeStr(SymbolRef::Type Type) {
47 case SymbolRef::ST_Unknown: return "?";
48 case SymbolRef::ST_Data: return "DATA";
49 case SymbolRef::ST_Debug: return "DBG";
50 case SymbolRef::ST_File: return "FILE";
51 case SymbolRef::ST_Function: return "FUNC";
52 case SymbolRef::ST_Other: return "-";
57 std::string GetFlagStr(uint32_t Flags) {
59 if (Flags & SymbolRef::SF_Undefined)
61 if (Flags & SymbolRef::SF_Global)
63 if (Flags & SymbolRef::SF_Weak)
65 if (Flags & SymbolRef::SF_Absolute)
66 result += "absolute,";
67 if (Flags & SymbolRef::SF_ThreadLocal)
68 result += "threadlocal,";
69 if (Flags & SymbolRef::SF_Common)
71 if (Flags & SymbolRef::SF_FormatSpecific)
72 result += "formatspecific,";
74 // Remove trailing comma
75 if (result.size() > 0) {
76 result.erase(result.size() - 1);
81 void DumpSymbol(const SymbolRef &sym) {
89 sym.getAddress(Address);
91 sym.getFileOffset(FileOffset);
95 // format() can't handle StringRefs
96 outs() << format(" %-32s", Name.str().c_str())
97 << format(" %-4s", GetTypeStr(Type))
98 << format(" %16"PRIx64, Address)
99 << format(" %16"PRIx64, Size)
100 << format(" %16"PRIx64, FileOffset)
101 << " " << GetFlagStr(Flags)
106 // Iterate through the normal symbols in the ObjectFile
107 void DumpSymbols(const ObjectFile *obj) {
110 outs() << "Symbols:\n";
111 symbol_iterator it = obj->begin_symbols();
112 symbol_iterator ie = obj->end_symbols();
117 report_fatal_error("Symbol iteration failed");
120 outs() << " Total: " << count << "\n\n";
123 // Iterate through the dynamic symbols in the ObjectFile.
124 void DumpDynamicSymbols(const ObjectFile *obj) {
127 outs() << "Dynamic Symbols:\n";
128 symbol_iterator it = obj->begin_dynamic_symbols();
129 symbol_iterator ie = obj->end_dynamic_symbols();
134 report_fatal_error("Symbol iteration failed");
137 outs() << " Total: " << count << "\n\n";
140 void DumpLibrary(const LibraryRef &lib) {
143 outs() << " " << path << "\n";
146 // Iterate through needed libraries
147 void DumpLibrariesNeeded(const ObjectFile *obj) {
150 library_iterator it = obj->begin_libraries_needed();
151 library_iterator ie = obj->end_libraries_needed();
152 outs() << "Libraries needed:\n";
157 report_fatal_error("Needed libraries iteration failed");
160 outs() << " Total: " << count << "\n\n";
163 void DumpHeaders(const ObjectFile *obj) {
164 outs() << "File Format : " << obj->getFileFormatName() << "\n";
166 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
168 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
169 outs() << "Load Name : " << obj->getLoadName() << "\n";
173 int main(int argc, char** argv) {
175 sys::PrintStackTraceOnErrorSignal();
176 PrettyStackTraceProgram X(argc, argv);
178 cl::ParseCommandLineOptions(argc, argv,
179 "LLVM Object Reader\n");
181 if (InputFilename.empty()) {
182 errs() << "Please specify an input filename\n";
186 // Open the object file
187 OwningPtr<MemoryBuffer> File;
188 if (MemoryBuffer::getFile(InputFilename, File)) {
189 errs() << InputFilename << ": Open failed\n";
193 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
195 errs() << InputFilename << ": Object type not recognized\n";
200 DumpDynamicSymbols(obj);
201 DumpLibrariesNeeded(obj);