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/ADT/Triple.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/PrettyStackTrace.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Signals.h"
23 #include "llvm/Support/FormattedStream.h"
26 using namespace llvm::object;
28 static cl::opt<std::string>
29 InputFilename(cl::Positional, cl::desc("<input object>"), cl::init(""));
31 void DumpSymbolHeader() {
32 outs() << format(" %-32s", (const char*)"Name")
33 << format(" %-4s", (const char*)"Type")
34 << format(" %-16s", (const char*)"Address")
35 << format(" %-16s", (const char*)"Size")
36 << format(" %-16s", (const char*)"FileOffset")
37 << format(" %-26s", (const char*)"Flags")
41 const char *GetTypeStr(SymbolRef::Type Type) {
43 case SymbolRef::ST_Unknown: return "?";
44 case SymbolRef::ST_Data: return "DATA";
45 case SymbolRef::ST_Debug: return "DBG";
46 case SymbolRef::ST_File: return "FILE";
47 case SymbolRef::ST_Function: return "FUNC";
48 case SymbolRef::ST_Other: return "-";
53 std::string GetFlagStr(uint32_t Flags) {
55 if (Flags & SymbolRef::SF_Undefined)
57 if (Flags & SymbolRef::SF_Global)
59 if (Flags & SymbolRef::SF_Weak)
61 if (Flags & SymbolRef::SF_Absolute)
62 result += "absolute,";
63 if (Flags & SymbolRef::SF_ThreadLocal)
64 result += "threadlocal,";
65 if (Flags & SymbolRef::SF_Common)
67 if (Flags & SymbolRef::SF_FormatSpecific)
68 result += "formatspecific,";
70 // Remove trailing comma
71 if (result.size() > 0) {
72 result.erase(result.size() - 1);
77 void DumpSymbol(const SymbolRef &sym) {
85 sym.getAddress(Address);
87 sym.getFileOffset(FileOffset);
91 // format() can't handle StringRefs
92 outs() << format(" %-32s", Name.str().c_str())
93 << format(" %-4s", GetTypeStr(Type))
94 << format(" %16"PRIx64, Address)
95 << format(" %16"PRIx64, Size)
96 << format(" %16"PRIx64, FileOffset)
97 << " " << GetFlagStr(Flags)
102 // Iterate through the normal symbols in the ObjectFile
103 void DumpSymbols(const ObjectFile *obj) {
106 outs() << "Symbols:\n";
107 symbol_iterator it = obj->begin_symbols();
108 symbol_iterator ie = obj->end_symbols();
113 report_fatal_error("Symbol iteration failed");
116 outs() << " Total: " << count << "\n\n";
119 // Iterate through the dynamic symbols in the ObjectFile.
120 void DumpDynamicSymbols(const ObjectFile *obj) {
123 outs() << "Dynamic Symbols:\n";
124 symbol_iterator it = obj->begin_dynamic_symbols();
125 symbol_iterator ie = obj->end_dynamic_symbols();
130 report_fatal_error("Symbol iteration failed");
133 outs() << " Total: " << count << "\n\n";
136 void DumpLibrary(const LibraryRef &lib) {
139 outs() << " " << path << "\n";
142 // Iterate through needed libraries
143 void DumpLibrariesNeeded(const ObjectFile *obj) {
146 library_iterator it = obj->begin_libraries_needed();
147 library_iterator ie = obj->end_libraries_needed();
148 outs() << "Libraries needed:\n";
153 report_fatal_error("Needed libraries iteration failed");
156 outs() << " Total: " << count << "\n\n";
159 void DumpHeaders(const ObjectFile *obj) {
160 outs() << "File Format : " << obj->getFileFormatName() << "\n";
162 << Triple::getArchTypeName((llvm::Triple::ArchType)obj->getArch())
164 outs() << "Address Size: " << (8*obj->getBytesInAddress()) << " bits\n";
165 outs() << "Load Name : " << obj->getLoadName() << "\n";
169 int main(int argc, char** argv) {
171 sys::PrintStackTraceOnErrorSignal();
172 PrettyStackTraceProgram X(argc, argv);
174 cl::ParseCommandLineOptions(argc, argv,
175 "LLVM Object Reader\n");
177 if (InputFilename.empty()) {
178 errs() << "Please specify an input filename\n";
182 // Open the object file
183 OwningPtr<MemoryBuffer> File;
184 if (MemoryBuffer::getFile(InputFilename, File)) {
185 errs() << InputFilename << ": Open failed\n";
189 ObjectFile *obj = ObjectFile::createObjectFile(File.take());
191 errs() << InputFilename << ": Object type not recognized\n";
196 DumpDynamicSymbols(obj);
197 DumpLibrariesNeeded(obj);