blob: 990582d7f45957e29c732d22efa44e385c1ed9cb [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
Jim Grosbach4f9f41f2011-04-13 15:49:40 +000027static cl::list<std::string>
28InputFileList(cl::Positional, cl::ZeroOrMore,
29 cl::desc("<input file>"));
Jim Grosbach1cb19a42011-03-18 17:11:39 +000030
31enum ActionType {
32 AC_Execute
33};
34
35static cl::opt<ActionType>
36Action(cl::desc("Action to perform:"),
37 cl::init(AC_Execute),
38 cl::values(clEnumValN(AC_Execute, "execute",
39 "Load, link, and execute the inputs."),
40 clEnumValEnd));
41
Jim Grosbach6b32e7e2011-04-13 15:38:30 +000042static cl::opt<std::string>
43EntryPoint("entry",
44 cl::desc("Function to call as entry point."),
45 cl::init("_main"));
46
Jim Grosbach1cb19a42011-03-18 17:11:39 +000047/* *** */
48
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000049// A trivial memory manager that doesn't do anything fancy, just uses the
50// support library allocation routines directly.
51class TrivialMemoryManager : public RTDyldMemoryManager {
52public:
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000053 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
Jim Grosbach61425c02012-01-16 22:26:39 +000054 SmallVector<sys::MemoryBlock, 16> DataMemory;
55
56 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
57 unsigned SectionID);
58 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
59 unsigned SectionID);
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000060
Chandler Carruth3e296712012-03-22 05:44:06 +000061 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
62 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
63 uint8_t *FunctionEnd);
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000064};
65
Jim Grosbach61425c02012-01-16 22:26:39 +000066uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
67 unsigned Alignment,
68 unsigned SectionID) {
69 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
70}
71
72uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
73 unsigned Alignment,
74 unsigned SectionID) {
75 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
76}
77
Chandler Carruth3e296712012-03-22 05:44:06 +000078uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name,
79 uintptr_t &Size) {
80 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
81}
82
83void TrivialMemoryManager::endFunctionBody(const char *Name,
84 uint8_t *FunctionStart,
85 uint8_t *FunctionEnd) {
86 uintptr_t Size = FunctionEnd - FunctionStart + 1;
87 FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size));
88}
89
Jim Grosbach1cb19a42011-03-18 17:11:39 +000090static const char *ProgramName;
91
92static void Message(const char *Type, const Twine &Msg) {
93 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
94}
95
96static int Error(const Twine &Msg) {
97 Message("error", Msg);
98 return 1;
99}
100
101/* *** */
102
Jim Grosbach82c25b42011-03-18 17:24:21 +0000103static int executeInput() {
Jim Grosbach6e563312011-03-21 22:15:52 +0000104 // Instantiate a dynamic linker.
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000105 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
106 RuntimeDyld Dyld(MemMgr);
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000107
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000108 // If we don't have any input files, read from stdin.
109 if (!InputFileList.size())
110 InputFileList.push_back("-");
111 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
112 // Load the input memory buffer.
113 OwningPtr<MemoryBuffer> InputBuffer;
114 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
115 InputBuffer))
116 return Error("unable to read input: '" + ec.message() + "'");
117
118 // Load the object file into it.
119 if (Dyld.loadObject(InputBuffer.take())) {
120 return Error(Dyld.getErrorString());
121 }
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000122 }
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000123
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000124 // Resolve all the relocations we can.
125 Dyld.resolveRelocations();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000126
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000127 // FIXME: Error out if there are unresolved relocations.
128
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000129 // Get the address of the entry point (_main by default).
130 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
Jim Grosbach6e563312011-03-21 22:15:52 +0000131 if (MainAddress == 0)
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000132 return Error("no definition for '" + EntryPoint + "'");
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000133
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000134 // Invalidate the instruction cache for each loaded function.
135 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
136 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
137 // Make sure the memory is executable.
138 std::string ErrorStr;
139 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
140 if (!sys::Memory::setExecutable(Data, &ErrorStr))
141 return Error("unable to mark function executable: '" + ErrorStr + "'");
142 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000143
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000144 // Dispatch to _main().
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000145 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000146
147 int (*Main)(int, const char**) =
148 (int(*)(int,const char**)) uintptr_t(MainAddress);
149 const char **Argv = new const char*[2];
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000150 // Use the name of the first input object module as argv[0] for the target.
151 Argv[0] = InputFileList[0].c_str();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000152 Argv[1] = 0;
153 return Main(1, Argv);
154}
155
156int main(int argc, char **argv) {
157 ProgramName = argv[0];
158 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
159
160 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
161
162 switch (Action) {
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000163 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000164 return executeInput();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000165 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000166}