blob: a670944235626735c47524498136cf6afa57af81 [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 Grosbach5acfa9f2011-03-29 21:03:05 +000016#include "llvm/ExecutionEngine/JITMemoryManager.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000017#include "llvm/ExecutionEngine/RuntimeDyld.h"
Jim Grosbach1cb19a42011-03-18 17:11:39 +000018#include "llvm/Object/MachOObject.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/ManagedStatic.h"
21#include "llvm/Support/Memory.h"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Support/system_error.h"
25using namespace llvm;
26using namespace llvm::object;
27
28static cl::opt<std::string>
29InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
30
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
42/* *** */
43
44static const char *ProgramName;
45
46static void Message(const char *Type, const Twine &Msg) {
47 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
48}
49
50static int Error(const Twine &Msg) {
51 Message("error", Msg);
52 return 1;
53}
54
55/* *** */
56
Jim Grosbach82c25b42011-03-18 17:24:21 +000057static int executeInput() {
Jim Grosbach1cb19a42011-03-18 17:11:39 +000058 // Load the input memory buffer.
59 OwningPtr<MemoryBuffer> InputBuffer;
60 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
61 return Error("unable to read input: '" + ec.message() + "'");
62
Jim Grosbach6e563312011-03-21 22:15:52 +000063 // Instantiate a dynamic linker.
Jim Grosbach5acfa9f2011-03-29 21:03:05 +000064 RuntimeDyld Dyld(JITMemoryManager::CreateDefaultMemManager());
Jim Grosbach1cb19a42011-03-18 17:11:39 +000065
Jim Grosbach6e563312011-03-21 22:15:52 +000066 // Load the object file into it.
Jim Grosbachb3eecaf2011-03-22 18:19:42 +000067 if (Dyld.loadObject(InputBuffer.take())) {
68 return Error(Dyld.getErrorString());
69 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +000070
71 // Get the address of "_main".
Jim Grosbach6e563312011-03-21 22:15:52 +000072 void *MainAddress = Dyld.getSymbolAddress("_main");
73 if (MainAddress == 0)
Jim Grosbach1cb19a42011-03-18 17:11:39 +000074 return Error("no definition for '_main'");
75
76 // Invalidate the instruction cache.
Jim Grosbach6e563312011-03-21 22:15:52 +000077 sys::MemoryBlock Data = Dyld.getMemoryBlock();
Jim Grosbach1cb19a42011-03-18 17:11:39 +000078 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
79
80 // Make sure the memory is executable.
Jim Grosbach6e563312011-03-21 22:15:52 +000081 std::string ErrorStr;
Jim Grosbach1cb19a42011-03-18 17:11:39 +000082 if (!sys::Memory::setExecutable(Data, &ErrorStr))
83 return Error("unable to mark function executable: '" + ErrorStr + "'");
84
85 // Dispatch to _main().
Jim Grosbach1cb19a42011-03-18 17:11:39 +000086 errs() << "loaded '_main' at: " << MainAddress << "\n";
87
88 int (*Main)(int, const char**) =
89 (int(*)(int,const char**)) uintptr_t(MainAddress);
90 const char **Argv = new const char*[2];
91 Argv[0] = InputFile.c_str();
92 Argv[1] = 0;
93 return Main(1, Argv);
94}
95
96int main(int argc, char **argv) {
97 ProgramName = argv[0];
98 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99
100 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
101
102 switch (Action) {
103 default:
104 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000105 return executeInput();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000106 }
107
108 return 0;
109}