1 /*===- pso-stub.c - Stub executable to run llvm bitcode files -------------===//
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 //===----------------------------------------------------------------------===*/
15 #include "llvm/Object/ObjectFile.h"
16 #include "llvm/Analysis/Verifier.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Signals.h"
22 #include "llvm/Support/FormattedStream.h"
25 using namespace llvm::object;
27 static cl::opt<std::string>
28 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
30 void DumpSymbolHeader() {
31 outs() << format(" %-32s", (const char*)"Name")
32 << format(" %-4s", (const char*)"Type")
33 << format(" %-16s", (const char*)"Address")
34 << format(" %-16s", (const char*)"Size")
35 << format(" %-16s", (const char*)"FileOffset")
36 << format(" %-26s", (const char*)"Flags")
40 const char *GetTypeStr(SymbolRef::Type Type) {
42 case SymbolRef::ST_Unknown: return "?";
43 case SymbolRef::ST_Data: return "DATA";
44 case SymbolRef::ST_Debug: return "DBG";
45 case SymbolRef::ST_File: return "FILE";
46 case SymbolRef::ST_Function: return "FUNC";
47 case SymbolRef::ST_Other: return "-";
52 std::string GetFlagStr(uint32_t Flags) {
54 if (Flags & SymbolRef::SF_Undefined)
56 if (Flags & SymbolRef::SF_Global)
58 if (Flags & SymbolRef::SF_Weak)
60 if (Flags & SymbolRef::SF_Absolute)
61 result += "absolute,";
62 if (Flags & SymbolRef::SF_ThreadLocal)
63 result += "threadlocal,";
64 if (Flags & SymbolRef::SF_Common)
66 if (Flags & SymbolRef::SF_FormatSpecific)
67 result += "formatspecific,";
69 // Remove trailing comma
70 if (result.size() > 0) {
71 result.erase(result.size() - 1);
76 void DumpSymbol(const SymbolRef &sym) {
84 sym.getAddress(Address);
86 sym.getFileOffset(FileOffset);
90 // format() can't handle StringRefs
91 outs() << format(" %-32s", Name.str().c_str())
92 << format(" %-4s", GetTypeStr(Type))
93 << format(" %16"PRIx64, Address)
94 << format(" %16"PRIx64, Size)
95 << format(" %16"PRIx64, FileOffset)
96 << " " << GetFlagStr(Flags)
101 // Iterate through the normal symbols in the ObjectFile
102 void DumpSymbols(const ObjectFile *obj) {
105 outs() << "Symbols:\n";
106 symbol_iterator it = obj->begin_symbols();
107 symbol_iterator ie = obj->end_symbols();
112 report_fatal_error("Symbol iteration failed");
115 outs() << " Total: " << count << "\n\n";
118 // Iterate through the dynamic symbols in the ObjectFile.
119 void DumpDynamicSymbols(const ObjectFile *obj) {
122 outs() << "Dynamic Symbols:\n";
123 symbol_iterator it = obj->begin_dynamic_symbols();
124 symbol_iterator ie = obj->end_dynamic_symbols();
129 report_fatal_error("Symbol iteration failed");
132 outs() << " Total: " << count << "\n\n";
135 void DumpLibrary(const LibraryRef &lib) {
138 outs() << " " << path << "\n";
141 // Iterate through needed libraries
142 void DumpLibrariesNeeded(const ObjectFile *obj) {
145 library_iterator it = obj->begin_libraries_needed();
146 library_iterator ie = obj->end_libraries_needed();
147 outs() << "Libraries needed:\n";
152 report_fatal_error("Needed libraries iteration failed");
155 outs() << " Total: " << count << "\n\n";
158 int main(int argc, char** argv) {
160 sys::PrintStackTraceOnErrorSignal();
161 PrettyStackTraceProgram X(argc, argv);
163 cl::ParseCommandLineOptions(argc, argv,
164 "LLVM Object Reader\n");
166 if (InputFilename.empty()) {
167 errs() << "Please specify an input filename\n";
171 // Open the object file
172 OwningPtr<MemoryBuffer> File;
173 if (MemoryBuffer::getFile(InputFilename, File)) {
174 errs() << InputFilename << ": Open failed\n";
178 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
180 errs() << InputFilename << ": Object type not recognized\n";
184 DumpDynamicSymbols(obj);
185 DumpLibrariesNeeded(obj);