blob: 7b5bd0388d882d58424c4aa754ea0096a18c89b8 [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"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000017#include "llvm/ExecutionEngine/ObjectImage.h"
18#include "llvm/ExecutionEngine/ObjectBuffer.h"
Jim Grosbach1cb19a42011-03-18 17:11:39 +000019#include "llvm/Object/MachOObject.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/Memory.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/Support/system_error.h"
26using namespace llvm;
27using namespace llvm::object;
28
Jim Grosbach4f9f41f2011-04-13 15:49:40 +000029static cl::list<std::string>
30InputFileList(cl::Positional, cl::ZeroOrMore,
31 cl::desc("<input file>"));
Jim Grosbach1cb19a42011-03-18 17:11:39 +000032
33enum ActionType {
34 AC_Execute
35};
36
37static cl::opt<ActionType>
38Action(cl::desc("Action to perform:"),
39 cl::init(AC_Execute),
40 cl::values(clEnumValN(AC_Execute, "execute",
41 "Load, link, and execute the inputs."),
42 clEnumValEnd));
43
Jim Grosbach6b32e7e2011-04-13 15:38:30 +000044static cl::opt<std::string>
45EntryPoint("entry",
46 cl::desc("Function to call as entry point."),
47 cl::init("_main"));
48
Jim Grosbach1cb19a42011-03-18 17:11:39 +000049/* *** */
50
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000051// A trivial memory manager that doesn't do anything fancy, just uses the
52// support library allocation routines directly.
53class TrivialMemoryManager : public RTDyldMemoryManager {
54public:
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000055 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
Jim Grosbach61425c02012-01-16 22:26:39 +000056 SmallVector<sys::MemoryBlock, 16> DataMemory;
57
58 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
59 unsigned SectionID);
60 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
61 unsigned SectionID);
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000062
Danil Malyshev30b9e322012-03-28 21:46:36 +000063 virtual void *getPointerToNamedFunction(const std::string &Name,
64 bool AbortOnFailure = true) {
65 return 0;
66 }
67
Danil Malyshev068c65b2012-05-16 18:50:11 +000068 // Invalidate instruction cache for sections with execute permissions.
69 // Some platforms with separate data cache and instruction cache require
70 // explicit cache flush, otherwise JIT code manipulations (like resolved
71 // relocations) will get to the data cache but not to the instruction cache.
72 virtual void invalidateInstructionCache();
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000073};
74
Jim Grosbach61425c02012-01-16 22:26:39 +000075uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
76 unsigned Alignment,
77 unsigned SectionID) {
Danil Malyshev068c65b2012-05-16 18:50:11 +000078 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
79 FunctionMemory.push_back(MB);
80 return (uint8_t*)MB.base();
Jim Grosbach61425c02012-01-16 22:26:39 +000081}
82
83uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
84 unsigned Alignment,
85 unsigned SectionID) {
Danil Malyshev068c65b2012-05-16 18:50:11 +000086 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
87 DataMemory.push_back(MB);
88 return (uint8_t*)MB.base();
89}
90
91void TrivialMemoryManager::invalidateInstructionCache() {
92 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
93 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
94 FunctionMemory[i].size());
95
96 for (int i = 0, e = DataMemory.size(); i != e; ++i)
97 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
98 DataMemory[i].size());
Jim Grosbach61425c02012-01-16 22:26:39 +000099}
100
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000101static const char *ProgramName;
102
103static void Message(const char *Type, const Twine &Msg) {
104 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
105}
106
107static int Error(const Twine &Msg) {
108 Message("error", Msg);
109 return 1;
110}
111
112/* *** */
113
Jim Grosbach82c25b42011-03-18 17:24:21 +0000114static int executeInput() {
Jim Grosbach6e563312011-03-21 22:15:52 +0000115 // Instantiate a dynamic linker.
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000116 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
117 RuntimeDyld Dyld(MemMgr);
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000118
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000119 // If we don't have any input files, read from stdin.
120 if (!InputFileList.size())
121 InputFileList.push_back("-");
122 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
123 // Load the input memory buffer.
124 OwningPtr<MemoryBuffer> InputBuffer;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000125 OwningPtr<ObjectImage> LoadedObject;
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000126 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
127 InputBuffer))
128 return Error("unable to read input: '" + ec.message() + "'");
129
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000130 // Load the object file
131 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
132 if (!LoadedObject) {
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000133 return Error(Dyld.getErrorString());
134 }
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000135 }
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000136
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000137 // Resolve all the relocations we can.
138 Dyld.resolveRelocations();
Danil Malyshev068c65b2012-05-16 18:50:11 +0000139 // Clear instruction cache before code will be executed.
140 MemMgr->invalidateInstructionCache();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000141
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000142 // FIXME: Error out if there are unresolved relocations.
143
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000144 // Get the address of the entry point (_main by default).
145 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
Jim Grosbach6e563312011-03-21 22:15:52 +0000146 if (MainAddress == 0)
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000147 return Error("no definition for '" + EntryPoint + "'");
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000148
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000149 // Invalidate the instruction cache for each loaded function.
150 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
151 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
152 // Make sure the memory is executable.
153 std::string ErrorStr;
154 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
155 if (!sys::Memory::setExecutable(Data, &ErrorStr))
156 return Error("unable to mark function executable: '" + ErrorStr + "'");
157 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000158
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000159 // Dispatch to _main().
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000160 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000161
162 int (*Main)(int, const char**) =
163 (int(*)(int,const char**)) uintptr_t(MainAddress);
164 const char **Argv = new const char*[2];
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000165 // Use the name of the first input object module as argv[0] for the target.
166 Argv[0] = InputFileList[0].c_str();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000167 Argv[1] = 0;
168 return Main(1, Argv);
169}
170
171int main(int argc, char **argv) {
172 ProgramName = argv[0];
173 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
174
175 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
176
177 switch (Action) {
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000178 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000179 return executeInput();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000180 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000181}