RuntimeDyld should use the memory manager API.
[oota-llvm.git] / tools / llvm-rtdyld / llvm-rtdyld.cpp
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"
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"
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 // A trivial memory manager that doesn't do anything fancy, just uses the
44 // support library allocation routines directly.
45 class TrivialMemoryManager : public RTDyldMemoryManager {
46 public:
47   uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
48   void endFunctionBody(const char *Name, uint8_t *FunctionStart,
49                        uint8_t *FunctionEnd) {}
50 };
51
52 uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name,
53                                                  uintptr_t &Size) {
54   return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
55 }
56
57 static const char *ProgramName;
58
59 static void Message(const char *Type, const Twine &Msg) {
60   errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
61 }
62
63 static int Error(const Twine &Msg) {
64   Message("error", Msg);
65   return 1;
66 }
67
68 /* *** */
69
70 static int executeInput() {
71   // Load the input memory buffer.
72   OwningPtr<MemoryBuffer> InputBuffer;
73   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
74     return Error("unable to read input: '" + ec.message() + "'");
75
76   // Instantiate a dynamic linker.
77   RuntimeDyld Dyld(new TrivialMemoryManager);
78
79   // Load the object file into it.
80   if (Dyld.loadObject(InputBuffer.take())) {
81     return Error(Dyld.getErrorString());
82   }
83
84   // Get the address of "_main".
85   uint64_t MainAddress = Dyld.getSymbolAddress("_main");
86   if (MainAddress == 0)
87     return Error("no definition for '_main'");
88
89   // Invalidate the instruction cache.
90   sys::MemoryBlock Data = Dyld.getMemoryBlock();
91   sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
92
93   // Make sure the memory is executable.
94   std::string ErrorStr;
95   if (!sys::Memory::setExecutable(Data, &ErrorStr))
96     return Error("unable to mark function executable: '" + ErrorStr + "'");
97
98   // Dispatch to _main().
99   errs() << "loaded '_main' at: " << (void*)MainAddress << "\n";
100
101   int (*Main)(int, const char**) =
102     (int(*)(int,const char**)) uintptr_t(MainAddress);
103   const char **Argv = new const char*[2];
104   Argv[0] = InputFile.c_str();
105   Argv[1] = 0;
106   return Main(1, Argv);
107 }
108
109 int main(int argc, char **argv) {
110   ProgramName = argv[0];
111   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
112
113   cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
114
115   switch (Action) {
116   default:
117   case AC_Execute:
118     return executeInput();
119   }
120
121   return 0;
122 }