blob: ee398e7b4cfcbea811db3f5f89084cb26bacc590 [file] [log] [blame]
Jim Grosbach1cb19a42011-03-18 17:11:39 +00001//===-- 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 Grosbach6e563312011-03-21 22:15:52 +000016#include "llvm/ExecutionEngine/RuntimeDyld.h"
Jim Grosbach1cb19a42011-03-18 17:11:39 +000017#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"
24using namespace llvm;
25using namespace llvm::object;
26
27static cl::opt<std::string>
28InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
29
30enum ActionType {
31 AC_Execute
32};
33
34static cl::opt<ActionType>
35Action(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
Jim Grosbach6b32e7e2011-04-13 15:38:30 +000041static cl::opt<std::string>
42EntryPoint("entry",
43 cl::desc("Function to call as entry point."),
44 cl::init("_main"));
45
Jim Grosbach1cb19a42011-03-18 17:11:39 +000046/* *** */
47
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000048// A trivial memory manager that doesn't do anything fancy, just uses the
49// support library allocation routines directly.
50class TrivialMemoryManager : public RTDyldMemoryManager {
51public:
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000052 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
53
Jim Grosbachc41ab782011-04-06 01:11:05 +000054 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
55 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000056 uint8_t *FunctionEnd);
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000057};
58
Jim Grosbachc41ab782011-04-06 01:11:05 +000059uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name,
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000060 uintptr_t &Size) {
Jim Grosbachc41ab782011-04-06 01:11:05 +000061 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000062}
63
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000064void TrivialMemoryManager::endFunctionBody(const char *Name,
65 uint8_t *FunctionStart,
66 uint8_t *FunctionEnd) {
67 uintptr_t Size = FunctionEnd - FunctionStart + 1;
68 FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size));
69}
70
Jim Grosbach1cb19a42011-03-18 17:11:39 +000071static const char *ProgramName;
72
73static void Message(const char *Type, const Twine &Msg) {
74 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
75}
76
77static int Error(const Twine &Msg) {
78 Message("error", Msg);
79 return 1;
80}
81
82/* *** */
83
Jim Grosbach82c25b42011-03-18 17:24:21 +000084static int executeInput() {
Jim Grosbach1cb19a42011-03-18 17:11:39 +000085 // Load the input memory buffer.
86 OwningPtr<MemoryBuffer> InputBuffer;
87 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
88 return Error("unable to read input: '" + ec.message() + "'");
89
Jim Grosbach6e563312011-03-21 22:15:52 +000090 // Instantiate a dynamic linker.
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000091 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
92 RuntimeDyld Dyld(MemMgr);
Jim Grosbach1cb19a42011-03-18 17:11:39 +000093
Jim Grosbach6e563312011-03-21 22:15:52 +000094 // Load the object file into it.
Jim Grosbachb3eecaf2011-03-22 18:19:42 +000095 if (Dyld.loadObject(InputBuffer.take())) {
96 return Error(Dyld.getErrorString());
97 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000098 // Resolve all the relocations we can.
99 Dyld.resolveRelocations();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000100
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000101 // Get the address of the entry point (_main by default).
102 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
Jim Grosbach6e563312011-03-21 22:15:52 +0000103 if (MainAddress == 0)
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000104 return Error("no definition for '" + EntryPoint + "'");
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000105
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000106 // Invalidate the instruction cache for each loaded function.
107 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
108 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
109 // Make sure the memory is executable.
110 std::string ErrorStr;
111 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
112 if (!sys::Memory::setExecutable(Data, &ErrorStr))
113 return Error("unable to mark function executable: '" + ErrorStr + "'");
114 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000115
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000116
117 // Dispatch to _main().
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000118 errs() << "loaded '_main' at: " << (void*)MainAddress << "\n";
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000119
120 int (*Main)(int, const char**) =
121 (int(*)(int,const char**)) uintptr_t(MainAddress);
122 const char **Argv = new const char*[2];
123 Argv[0] = InputFile.c_str();
124 Argv[1] = 0;
125 return Main(1, Argv);
126}
127
128int main(int argc, char **argv) {
129 ProgramName = argv[0];
130 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
131
132 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
133
134 switch (Action) {
135 default:
136 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000137 return executeInput();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000138 }
139
140 return 0;
141}