Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 1 | //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is a testing tool for use with the MC-JIT LLVM components. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/StringMap.h" |
| 15 | #include "llvm/ADT/OwningPtr.h" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 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" |
| 24 | using namespace llvm; |
| 25 | using namespace llvm::object; |
| 26 | |
| 27 | static cl::opt<std::string> |
| 28 | InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 29 | |
| 30 | enum ActionType { |
| 31 | AC_Execute |
| 32 | }; |
| 33 | |
| 34 | static cl::opt<ActionType> |
| 35 | Action(cl::desc("Action to perform:"), |
| 36 | cl::init(AC_Execute), |
| 37 | cl::values(clEnumValN(AC_Execute, "execute", |
| 38 | "Load, link, and execute the inputs."), |
| 39 | clEnumValEnd)); |
| 40 | |
| 41 | /* *** */ |
| 42 | |
| 43 | static const char *ProgramName; |
| 44 | |
| 45 | static void Message(const char *Type, const Twine &Msg) { |
| 46 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 47 | } |
| 48 | |
| 49 | static int Error(const Twine &Msg) { |
| 50 | Message("error", Msg); |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | /* *** */ |
| 55 | |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 56 | static int executeInput() { |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 57 | // Load the input memory buffer. |
| 58 | OwningPtr<MemoryBuffer> InputBuffer; |
| 59 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer)) |
| 60 | return Error("unable to read input: '" + ec.message() + "'"); |
| 61 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 62 | // Instantiate a dynamic linker. |
| 63 | RuntimeDyld Dyld; |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 64 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 65 | // Load the object file into it. |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame^] | 66 | if (Dyld.loadObject(InputBuffer.take())) { |
| 67 | return Error(Dyld.getErrorString()); |
| 68 | } |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 69 | |
| 70 | // Get the address of "_main". |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 71 | void *MainAddress = Dyld.getSymbolAddress("_main"); |
| 72 | if (MainAddress == 0) |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 73 | return Error("no definition for '_main'"); |
| 74 | |
| 75 | // Invalidate the instruction cache. |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 76 | sys::MemoryBlock Data = Dyld.getMemoryBlock(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 77 | sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); |
| 78 | |
| 79 | // Make sure the memory is executable. |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 80 | std::string ErrorStr; |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 81 | if (!sys::Memory::setExecutable(Data, &ErrorStr)) |
| 82 | return Error("unable to mark function executable: '" + ErrorStr + "'"); |
| 83 | |
| 84 | // Dispatch to _main(). |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 85 | errs() << "loaded '_main' at: " << MainAddress << "\n"; |
| 86 | |
| 87 | int (*Main)(int, const char**) = |
| 88 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 89 | const char **Argv = new const char*[2]; |
| 90 | Argv[0] = InputFile.c_str(); |
| 91 | Argv[1] = 0; |
| 92 | return Main(1, Argv); |
| 93 | } |
| 94 | |
| 95 | int main(int argc, char **argv) { |
| 96 | ProgramName = argv[0]; |
| 97 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 98 | |
| 99 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 100 | |
| 101 | switch (Action) { |
| 102 | default: |
| 103 | case AC_Execute: |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 104 | return executeInput(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |