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/Object/ELF.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/FormattedStream.h"
31 using namespace llvm::object;
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
36 void DumpSymbolHeader() {
37 outs() << format(" %-32s", (const char*)"Name")
38 << format(" %-4s", (const char*)"Type")
39 << format(" %-16s", (const char*)"Address")
40 << format(" %-16s", (const char*)"Size")
41 << format(" %-16s", (const char*)"FileOffset")
42 << format(" %-26s", (const char*)"Flags")
46 const char *GetTypeStr(SymbolRef::Type Type) {
48 case SymbolRef::ST_Unknown: return "?";
49 case SymbolRef::ST_Data: return "DATA";
50 case SymbolRef::ST_Debug: return "DBG";
51 case SymbolRef::ST_File: return "FILE";
52 case SymbolRef::ST_Function: return "FUNC";
53 case SymbolRef::ST_Other: return "-";
58 std::string GetFlagStr(uint32_t Flags) {
60 if (Flags & SymbolRef::SF_Undefined)
62 if (Flags & SymbolRef::SF_Global)
64 if (Flags & SymbolRef::SF_Weak)
66 if (Flags & SymbolRef::SF_Absolute)
67 result += "absolute,";
68 if (Flags & SymbolRef::SF_ThreadLocal)
69 result += "threadlocal,";
70 if (Flags & SymbolRef::SF_Common)
72 if (Flags & SymbolRef::SF_FormatSpecific)
73 result += "formatspecific,";
75 // Remove trailing comma
76 if (result.size() > 0) {
77 result.erase(result.size() - 1);
82 void DumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
90 Sym.getAddress(Address);
92 Sym.getFileOffset(FileOffset);
95 std::string FullName = Name;
97 // If this is a dynamic symbol from an ELF object, append
98 // the symbol's version to the name.
99 if (IsDynamic && obj->isELF()) {
102 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
103 if (!Version.empty()) {
104 FullName += (IsDefault ? "@@" : "@");
109 // format() can't handle StringRefs
110 outs() << format(" %-32s", FullName.c_str())
111 << format(" %-4s", GetTypeStr(Type))
112 << format(" %16" PRIx64, Address)
113 << format(" %16" PRIx64, Size)
114 << format(" %16" PRIx64, FileOffset)
115 << " " << GetFlagStr(Flags)
120 // Iterate through the normal symbols in the ObjectFile
121 void DumpSymbols(const ObjectFile *obj) {
124 outs() << "Symbols:\n";
125 symbol_iterator it = obj->begin_symbols();
126 symbol_iterator ie = obj->end_symbols();
128 DumpSymbol(*it, obj, false);
131 report_fatal_error("Symbol iteration failed");
134 outs() << " Total: " << count << "\n\n";
137 // Iterate through the dynamic symbols in the ObjectFile.
138 void DumpDynamicSymbols(const ObjectFile *obj) {
141 outs() << "Dynamic Symbols:\n";
142 symbol_iterator it = obj->begin_dynamic_symbols();
143 symbol_iterator ie = obj->end_dynamic_symbols();
145 DumpSymbol(*it, obj, true);
148 report_fatal_error("Symbol iteration failed");
151 outs() << " Total: " << count << "\n\n";
154 void DumpLibrary(const LibraryRef &lib) {
157 outs() << " " << path << "\n";
160 // Iterate through needed libraries
161 void DumpLibrariesNeeded(const ObjectFile *obj) {
164 library_iterator it = obj->begin_libraries_needed();
165 library_iterator ie = obj->end_libraries_needed();
166 outs() << "Libraries needed:\n";
171 report_fatal_error("Needed libraries iteration failed");
174 outs() << " Total: " << count << "\n\n";
177 void DumpHeaders(const ObjectFile *obj) {
178 outs() << "File Format : " << obj->getFileFormatName() << "\n";
180 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
182 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
183 outs() << "Load Name : " << obj->getLoadName() << "\n";
187 int main(int argc, char** argv) {
189 sys::PrintStackTraceOnErrorSignal();
190 PrettyStackTraceProgram X(argc, argv);
192 cl::ParseCommandLineOptions(argc, argv,
193 "LLVM Object Reader\n");
195 if (InputFilename.empty()) {
196 errs() << "Please specify an input filename\n";
200 // Open the object file
201 OwningPtr<MemoryBuffer> File;
202 if (MemoryBuffer::getFile(InputFilename, File)) {
203 errs() << InputFilename << ": Open failed\n";
207 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
209 errs() << InputFilename << ": Object type not recognized\n";
214 DumpDynamicSymbols(obj);
215 DumpLibrariesNeeded(obj);