1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
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 is a testing tool for use with the MC-JIT LLVM components.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/DebugInfo/DIContext.h"
17 #include "llvm/ExecutionEngine/ObjectBuffer.h"
18 #include "llvm/ExecutionEngine/ObjectImage.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/Memory.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/system_error.h"
28 using namespace llvm::object;
30 static cl::list<std::string>
31 InputFileList(cl::Positional, cl::ZeroOrMore,
32 cl::desc("<input file>"));
39 static cl::opt<ActionType>
40 Action(cl::desc("Action to perform:"),
42 cl::values(clEnumValN(AC_Execute, "execute",
43 "Load, link, and execute the inputs."),
44 clEnumValN(AC_PrintLineInfo, "printline",
45 "Load, link, and print line information for each function."),
48 static cl::opt<std::string>
50 cl::desc("Function to call as entry point."),
55 // A trivial memory manager that doesn't do anything fancy, just uses the
56 // support library allocation routines directly.
57 class TrivialMemoryManager : public RTDyldMemoryManager {
59 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
60 SmallVector<sys::MemoryBlock, 16> DataMemory;
62 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
64 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
65 unsigned SectionID, bool IsReadOnly);
67 virtual void *getPointerToNamedFunction(const std::string &Name,
68 bool AbortOnFailure = true) {
72 bool finalizeMemory(std::string *ErrMsg) { return false; }
74 // Invalidate instruction cache for sections with execute permissions.
75 // Some platforms with separate data cache and instruction cache require
76 // explicit cache flush, otherwise JIT code manipulations (like resolved
77 // relocations) will get to the data cache but not to the instruction cache.
78 virtual void invalidateInstructionCache();
81 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
84 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
85 FunctionMemory.push_back(MB);
86 return (uint8_t*)MB.base();
89 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
93 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
94 DataMemory.push_back(MB);
95 return (uint8_t*)MB.base();
98 void TrivialMemoryManager::invalidateInstructionCache() {
99 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
100 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
101 FunctionMemory[i].size());
103 for (int i = 0, e = DataMemory.size(); i != e; ++i)
104 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
105 DataMemory[i].size());
108 static const char *ProgramName;
110 static void Message(const char *Type, const Twine &Msg) {
111 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
114 static int Error(const Twine &Msg) {
115 Message("error", Msg);
121 static int printLineInfoForInput() {
122 // If we don't have any input files, read from stdin.
123 if (!InputFileList.size())
124 InputFileList.push_back("-");
125 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
126 // Instantiate a dynamic linker.
127 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
128 RuntimeDyld Dyld(MemMgr);
130 // Load the input memory buffer.
131 OwningPtr<MemoryBuffer> InputBuffer;
132 OwningPtr<ObjectImage> LoadedObject;
133 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
135 return Error("unable to read input: '" + ec.message() + "'");
137 // Load the object file
138 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
140 return Error(Dyld.getErrorString());
143 // Resolve all the relocations we can.
144 Dyld.resolveRelocations();
146 OwningPtr<DIContext> Context(DIContext::getDWARFContext(LoadedObject->getObjectFile()));
148 // Use symbol info to iterate functions in the object.
150 for (object::symbol_iterator I = LoadedObject->begin_symbols(),
151 E = LoadedObject->end_symbols();
154 object::SymbolRef::Type SymType;
155 if (I->getType(SymType)) continue;
156 if (SymType == object::SymbolRef::ST_Function) {
160 if (I->getName(Name)) continue;
161 if (I->getAddress(Addr)) continue;
162 if (I->getSize(Size)) continue;
164 outs() << "Function: " << Name << ", Size = " << Size << "\n";
166 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
167 DILineInfoTable::iterator Begin = Lines.begin();
168 DILineInfoTable::iterator End = Lines.end();
169 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
170 outs() << " Line info @ " << It->first - Addr << ": "
171 << It->second.getFileName()
172 << ", line:" << It->second.getLine() << "\n";
181 static int executeInput() {
182 // Instantiate a dynamic linker.
183 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
184 RuntimeDyld Dyld(MemMgr);
186 // If we don't have any input files, read from stdin.
187 if (!InputFileList.size())
188 InputFileList.push_back("-");
189 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
190 // Load the input memory buffer.
191 OwningPtr<MemoryBuffer> InputBuffer;
192 OwningPtr<ObjectImage> LoadedObject;
193 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
195 return Error("unable to read input: '" + ec.message() + "'");
197 // Load the object file
198 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
200 return Error(Dyld.getErrorString());
204 // Resolve all the relocations we can.
205 Dyld.resolveRelocations();
206 // Clear instruction cache before code will be executed.
207 MemMgr->invalidateInstructionCache();
209 // FIXME: Error out if there are unresolved relocations.
211 // Get the address of the entry point (_main by default).
212 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
213 if (MainAddress == 0)
214 return Error("no definition for '" + EntryPoint + "'");
216 // Invalidate the instruction cache for each loaded function.
217 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
218 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
219 // Make sure the memory is executable.
220 std::string ErrorStr;
221 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
222 if (!sys::Memory::setExecutable(Data, &ErrorStr))
223 return Error("unable to mark function executable: '" + ErrorStr + "'");
226 // Dispatch to _main().
227 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
229 int (*Main)(int, const char**) =
230 (int(*)(int,const char**)) uintptr_t(MainAddress);
231 const char **Argv = new const char*[2];
232 // Use the name of the first input object module as argv[0] for the target.
233 Argv[0] = InputFileList[0].c_str();
235 return Main(1, Argv);
238 int main(int argc, char **argv) {
239 ProgramName = argv[0];
240 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
242 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
246 return executeInput();
247 case AC_PrintLineInfo:
248 return printLineInfoForInput();