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/StringMap.h"
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/ExecutionEngine/RuntimeDyld.h"
17 #include "llvm/Object/MachOObject.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/Memory.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/system_error.h"
25 using namespace llvm::object;
27 static cl::list<std::string>
28 InputFileList(cl::Positional, cl::ZeroOrMore,
29 cl::desc("<input file>"));
35 static cl::opt<ActionType>
36 Action(cl::desc("Action to perform:"),
38 cl::values(clEnumValN(AC_Execute, "execute",
39 "Load, link, and execute the inputs."),
42 static cl::opt<std::string>
44 cl::desc("Function to call as entry point."),
49 // A trivial memory manager that doesn't do anything fancy, just uses the
50 // support library allocation routines directly.
51 class TrivialMemoryManager : public RTDyldMemoryManager {
53 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
54 SmallVector<sys::MemoryBlock, 16> DataMemory;
56 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
58 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
61 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
62 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
63 uint8_t *FunctionEnd);
66 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
69 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
72 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
75 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
78 uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name,
80 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
83 void TrivialMemoryManager::endFunctionBody(const char *Name,
84 uint8_t *FunctionStart,
85 uint8_t *FunctionEnd) {
86 uintptr_t Size = FunctionEnd - FunctionStart + 1;
87 FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size));
90 static const char *ProgramName;
92 static void Message(const char *Type, const Twine &Msg) {
93 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
96 static int Error(const Twine &Msg) {
97 Message("error", Msg);
103 static int executeInput() {
104 // Instantiate a dynamic linker.
105 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
106 RuntimeDyld Dyld(MemMgr);
108 // If we don't have any input files, read from stdin.
109 if (!InputFileList.size())
110 InputFileList.push_back("-");
111 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
112 // Load the input memory buffer.
113 OwningPtr<MemoryBuffer> InputBuffer;
114 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
116 return Error("unable to read input: '" + ec.message() + "'");
118 // Load the object file into it.
119 if (Dyld.loadObject(InputBuffer.take())) {
120 return Error(Dyld.getErrorString());
124 // Resolve all the relocations we can.
125 Dyld.resolveRelocations();
127 // FIXME: Error out if there are unresolved relocations.
129 // Get the address of the entry point (_main by default).
130 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
131 if (MainAddress == 0)
132 return Error("no definition for '" + EntryPoint + "'");
134 // Invalidate the instruction cache for each loaded function.
135 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
136 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
137 // Make sure the memory is executable.
138 std::string ErrorStr;
139 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
140 if (!sys::Memory::setExecutable(Data, &ErrorStr))
141 return Error("unable to mark function executable: '" + ErrorStr + "'");
144 // Dispatch to _main().
145 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
147 int (*Main)(int, const char**) =
148 (int(*)(int,const char**)) uintptr_t(MainAddress);
149 const char **Argv = new const char*[2];
150 // Use the name of the first input object module as argv[0] for the target.
151 Argv[0] = InputFileList[0].c_str();
153 return Main(1, Argv);
156 int main(int argc, char **argv) {
157 ProgramName = argv[0];
158 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
160 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
164 return executeInput();