blob: faec7c3cc53901de22ff50bfbe94f7088c58bda4 [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
41/* *** */
42
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000043// A trivial memory manager that doesn't do anything fancy, just uses the
44// support library allocation routines directly.
45class TrivialMemoryManager : public RTDyldMemoryManager {
46public:
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000047 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
48
Jim Grosbachc41ab782011-04-06 01:11:05 +000049 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
50 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000051 uint8_t *FunctionEnd);
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000052};
53
Jim Grosbachc41ab782011-04-06 01:11:05 +000054uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name,
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000055 uintptr_t &Size) {
Jim Grosbachc41ab782011-04-06 01:11:05 +000056 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000057}
58
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000059void TrivialMemoryManager::endFunctionBody(const char *Name,
60 uint8_t *FunctionStart,
61 uint8_t *FunctionEnd) {
62 uintptr_t Size = FunctionEnd - FunctionStart + 1;
63 FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size));
64}
65
Jim Grosbach1cb19a42011-03-18 17:11:39 +000066static const char *ProgramName;
67
68static void Message(const char *Type, const Twine &Msg) {
69 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
70}
71
72static int Error(const Twine &Msg) {
73 Message("error", Msg);
74 return 1;
75}
76
77/* *** */
78
Jim Grosbach82c25b42011-03-18 17:24:21 +000079static int executeInput() {
Jim Grosbach1cb19a42011-03-18 17:11:39 +000080 // Load the input memory buffer.
81 OwningPtr<MemoryBuffer> InputBuffer;
82 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
83 return Error("unable to read input: '" + ec.message() + "'");
84
Jim Grosbach6e563312011-03-21 22:15:52 +000085 // Instantiate a dynamic linker.
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000086 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
87 RuntimeDyld Dyld(MemMgr);
Jim Grosbach1cb19a42011-03-18 17:11:39 +000088
Jim Grosbach6e563312011-03-21 22:15:52 +000089 // Load the object file into it.
Jim Grosbachb3eecaf2011-03-22 18:19:42 +000090 if (Dyld.loadObject(InputBuffer.take())) {
91 return Error(Dyld.getErrorString());
92 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +000093
94 // Get the address of "_main".
Jim Grosbachb0271052011-04-08 17:31:24 +000095 void *MainAddress = Dyld.getSymbolAddress("_main");
Jim Grosbach6e563312011-03-21 22:15:52 +000096 if (MainAddress == 0)
Jim Grosbach1cb19a42011-03-18 17:11:39 +000097 return Error("no definition for '_main'");
98
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000099 // Invalidate the instruction cache for each loaded function.
100 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
101 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
102 // Make sure the memory is executable.
103 std::string ErrorStr;
104 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
105 if (!sys::Memory::setExecutable(Data, &ErrorStr))
106 return Error("unable to mark function executable: '" + ErrorStr + "'");
107 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000108
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000109
110 // Dispatch to _main().
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000111 errs() << "loaded '_main' at: " << (void*)MainAddress << "\n";
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000112
113 int (*Main)(int, const char**) =
114 (int(*)(int,const char**)) uintptr_t(MainAddress);
115 const char **Argv = new const char*[2];
116 Argv[0] = InputFile.c_str();
117 Argv[1] = 0;
118 return Main(1, Argv);
119}
120
121int main(int argc, char **argv) {
122 ProgramName = argv[0];
123 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
124
125 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
126
127 switch (Action) {
128 default:
129 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000130 return executeInput();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000131 }
132
133 return 0;
134}