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/ExecutionEngine/ObjectImage.h"
18 #include "llvm/ExecutionEngine/ObjectBuffer.h"
19 #include "llvm/Object/MachOObject.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/Memory.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
27 using namespace llvm::object;
29 static cl::list<std::string>
30 InputFileList(cl::Positional, cl::ZeroOrMore,
31 cl::desc("<input file>"));
37 static cl::opt<ActionType>
38 Action(cl::desc("Action to perform:"),
40 cl::values(clEnumValN(AC_Execute, "execute",
41 "Load, link, and execute the inputs."),
44 static cl::opt<std::string>
46 cl::desc("Function to call as entry point."),
51 // A trivial memory manager that doesn't do anything fancy, just uses the
52 // support library allocation routines directly.
53 class TrivialMemoryManager : public RTDyldMemoryManager {
55 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
56 SmallVector<sys::MemoryBlock, 16> DataMemory;
58 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
60 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
63 virtual void *getPointerToNamedFunction(const std::string &Name,
64 bool AbortOnFailure = true) {
68 // Invalidate instruction cache for sections with execute permissions.
69 // Some platforms with separate data cache and instruction cache require
70 // explicit cache flush, otherwise JIT code manipulations (like resolved
71 // relocations) will get to the data cache but not to the instruction cache.
72 virtual void invalidateInstructionCache();
75 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
78 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
79 FunctionMemory.push_back(MB);
80 return (uint8_t*)MB.base();
83 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
86 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
87 DataMemory.push_back(MB);
88 return (uint8_t*)MB.base();
91 void TrivialMemoryManager::invalidateInstructionCache() {
92 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
93 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
94 FunctionMemory[i].size());
96 for (int i = 0, e = DataMemory.size(); i != e; ++i)
97 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
98 DataMemory[i].size());
101 static const char *ProgramName;
103 static void Message(const char *Type, const Twine &Msg) {
104 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
107 static int Error(const Twine &Msg) {
108 Message("error", Msg);
114 static int executeInput() {
115 // Instantiate a dynamic linker.
116 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
117 RuntimeDyld Dyld(MemMgr);
119 // If we don't have any input files, read from stdin.
120 if (!InputFileList.size())
121 InputFileList.push_back("-");
122 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
123 // Load the input memory buffer.
124 OwningPtr<MemoryBuffer> InputBuffer;
125 OwningPtr<ObjectImage> LoadedObject;
126 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
128 return Error("unable to read input: '" + ec.message() + "'");
130 // Load the object file
131 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
133 return Error(Dyld.getErrorString());
137 // Resolve all the relocations we can.
138 Dyld.resolveRelocations();
139 // Clear instruction cache before code will be executed.
140 MemMgr->invalidateInstructionCache();
142 // FIXME: Error out if there are unresolved relocations.
144 // Get the address of the entry point (_main by default).
145 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
146 if (MainAddress == 0)
147 return Error("no definition for '" + EntryPoint + "'");
149 // Invalidate the instruction cache for each loaded function.
150 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
151 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
152 // Make sure the memory is executable.
153 std::string ErrorStr;
154 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
155 if (!sys::Memory::setExecutable(Data, &ErrorStr))
156 return Error("unable to mark function executable: '" + ErrorStr + "'");
159 // Dispatch to _main().
160 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
162 int (*Main)(int, const char**) =
163 (int(*)(int,const char**)) uintptr_t(MainAddress);
164 const char **Argv = new const char*[2];
165 // Use the name of the first input object module as argv[0] for the target.
166 Argv[0] = InputFileList[0].c_str();
168 return Main(1, Argv);
171 int main(int argc, char **argv) {
172 ProgramName = argv[0];
173 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
175 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
179 return executeInput();