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/ADT/Triple.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/Object/ELF.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Signals.h"
31 using namespace llvm::object;
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
36 static const char *getTypeStr(SymbolRef::Type Type) {
38 case SymbolRef::ST_Unknown: return "?";
39 case SymbolRef::ST_Data: return "DATA";
40 case SymbolRef::ST_Debug: return "DBG";
41 case SymbolRef::ST_File: return "FILE";
42 case SymbolRef::ST_Function: return "FUNC";
43 case SymbolRef::ST_Other: return "-";
48 static std::string getSymbolFlagStr(uint32_t Flags) {
50 if (Flags & SymbolRef::SF_Undefined)
52 if (Flags & SymbolRef::SF_Global)
54 if (Flags & SymbolRef::SF_Weak)
56 if (Flags & SymbolRef::SF_Absolute)
57 result += "absolute,";
58 if (Flags & SymbolRef::SF_ThreadLocal)
59 result += "threadlocal,";
60 if (Flags & SymbolRef::SF_Common)
62 if (Flags & SymbolRef::SF_FormatSpecific)
63 result += "formatspecific,";
65 // Remove trailing comma
66 if (result.size() > 0) {
67 result.erase(result.size() - 1);
73 dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) {
81 Sym.getAddress(Address);
83 Sym.getFileOffset(FileOffset);
86 std::string FullName = Name;
88 // If this is a dynamic symbol from an ELF object, append
89 // the symbol's version to the name.
90 if (IsDynamic && obj->isELF()) {
93 GetELFSymbolVersion(obj, Sym, Version, IsDefault);
94 if (!Version.empty()) {
95 FullName += (IsDefault ? "@@" : "@");
100 // format() can't handle StringRefs
101 outs() << format(" %-32s", FullName.c_str())
102 << format(" %-4s", getTypeStr(Type))
103 << format(" %16" PRIx64, Address)
104 << format(" %16" PRIx64, Size)
105 << format(" %16" PRIx64, FileOffset)
106 << " " << getSymbolFlagStr(Flags)
110 // Iterate through the normal symbols in the ObjectFile
111 static void dumpSymbols(const ObjectFile *obj) {
114 outs() << "Symbols:\n";
115 symbol_iterator it = obj->begin_symbols();
116 symbol_iterator ie = obj->end_symbols();
118 dumpSymbol(*it, obj, false);
121 report_fatal_error("Symbol iteration failed");
124 outs() << " Total: " << count << "\n\n";
127 // Iterate through the dynamic symbols in the ObjectFile.
128 static void dumpDynamicSymbols(const ObjectFile *obj) {
131 outs() << "Dynamic Symbols:\n";
132 symbol_iterator it = obj->begin_dynamic_symbols();
133 symbol_iterator ie = obj->end_dynamic_symbols();
135 dumpSymbol(*it, obj, true);
138 report_fatal_error("Symbol iteration failed");
141 outs() << " Total: " << count << "\n\n";
144 static void dumpLibrary(const LibraryRef &lib) {
147 outs() << " " << path << "\n";
150 // Iterate through needed libraries
151 static void dumpLibrariesNeeded(const ObjectFile *obj) {
154 library_iterator it = obj->begin_libraries_needed();
155 library_iterator ie = obj->end_libraries_needed();
156 outs() << "Libraries needed:\n";
161 report_fatal_error("Needed libraries iteration failed");
164 outs() << " Total: " << count << "\n\n";
167 static void dumpHeaders(const ObjectFile *obj) {
168 outs() << "File Format : " << obj->getFileFormatName() << "\n";
170 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
172 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
173 outs() << "Load Name : " << obj->getLoadName() << "\n";
177 int main(int argc, char** argv) {
179 sys::PrintStackTraceOnErrorSignal();
180 PrettyStackTraceProgram X(argc, argv);
182 cl::ParseCommandLineOptions(argc, argv,
183 "LLVM Object Reader\n");
185 if (InputFilename.empty()) {
186 errs() << "Please specify an input filename\n";
190 // Open the object file
191 OwningPtr<MemoryBuffer> File;
192 if (MemoryBuffer::getFile(InputFilename, File)) {
193 errs() << InputFilename << ": Open failed\n";
197 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
199 errs() << InputFilename << ": Object type not recognized\n";
204 dumpDynamicSymbols(obj);
205 dumpLibrariesNeeded(obj);