blob: c5c285431a4d5ec285cf26147fcc9d142bb82e2a [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
Jim Grosbach1cb19a42011-03-18 17:11:39 +000014#include "llvm/ADT/OwningPtr.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000015#include "llvm/ADT/StringMap.h"
Andrew Kayloree7c0d22013-01-25 22:50:58 +000016#include "llvm/DebugInfo/DIContext.h"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000017#include "llvm/ExecutionEngine/ObjectBuffer.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000018#include "llvm/ExecutionEngine/ObjectImage.h"
19#include "llvm/ExecutionEngine/RuntimeDyld.h"
Rafael Espindola2173e182013-04-26 20:07:33 +000020#include "llvm/Object/MachO.h"
Jim Grosbach1cb19a42011-03-18 17:11:39 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/Memory.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Support/system_error.h"
27using namespace llvm;
28using namespace llvm::object;
29
Jim Grosbach4f9f41f2011-04-13 15:49:40 +000030static cl::list<std::string>
31InputFileList(cl::Positional, cl::ZeroOrMore,
32 cl::desc("<input file>"));
Jim Grosbach1cb19a42011-03-18 17:11:39 +000033
34enum ActionType {
Andrew Kayloree7c0d22013-01-25 22:50:58 +000035 AC_Execute,
36 AC_PrintLineInfo
Jim Grosbach1cb19a42011-03-18 17:11:39 +000037};
38
39static cl::opt<ActionType>
40Action(cl::desc("Action to perform:"),
41 cl::init(AC_Execute),
42 cl::values(clEnumValN(AC_Execute, "execute",
43 "Load, link, and execute the inputs."),
Andrew Kayloree7c0d22013-01-25 22:50:58 +000044 clEnumValN(AC_PrintLineInfo, "printline",
45 "Load, link, and print line information for each function."),
Jim Grosbach1cb19a42011-03-18 17:11:39 +000046 clEnumValEnd));
47
Jim Grosbach6b32e7e2011-04-13 15:38:30 +000048static cl::opt<std::string>
49EntryPoint("entry",
50 cl::desc("Function to call as entry point."),
51 cl::init("_main"));
52
Jim Grosbach1cb19a42011-03-18 17:11:39 +000053/* *** */
54
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000055// A trivial memory manager that doesn't do anything fancy, just uses the
56// support library allocation routines directly.
57class TrivialMemoryManager : public RTDyldMemoryManager {
58public:
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000059 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
Jim Grosbach61425c02012-01-16 22:26:39 +000060 SmallVector<sys::MemoryBlock, 16> DataMemory;
61
62 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000063 unsigned SectionID, StringRef SectionName);
Jim Grosbach61425c02012-01-16 22:26:39 +000064 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000065 unsigned SectionID, StringRef SectionName,
66 bool IsReadOnly);
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000067
Danil Malyshev30b9e322012-03-28 21:46:36 +000068 virtual void *getPointerToNamedFunction(const std::string &Name,
69 bool AbortOnFailure = true) {
70 return 0;
71 }
72
David Tweedabb38fe2013-05-17 10:01:46 +000073 bool finalizeMemory(std::string *ErrMsg) { return false; }
Andrew Kaylor53608a32012-11-15 23:50:01 +000074
Danil Malyshev068c65b2012-05-16 18:50:11 +000075 // Invalidate instruction cache for sections with execute permissions.
76 // Some platforms with separate data cache and instruction cache require
77 // explicit cache flush, otherwise JIT code manipulations (like resolved
78 // relocations) will get to the data cache but not to the instruction cache.
79 virtual void invalidateInstructionCache();
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000080};
81
Jim Grosbach61425c02012-01-16 22:26:39 +000082uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
83 unsigned Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000084 unsigned SectionID,
85 StringRef SectionName) {
Danil Malyshev068c65b2012-05-16 18:50:11 +000086 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
87 FunctionMemory.push_back(MB);
88 return (uint8_t*)MB.base();
Jim Grosbach61425c02012-01-16 22:26:39 +000089}
90
91uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
92 unsigned Alignment,
Andrew Kaylor53608a32012-11-15 23:50:01 +000093 unsigned SectionID,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000094 StringRef SectionName,
Andrew Kaylor53608a32012-11-15 23:50:01 +000095 bool IsReadOnly) {
Danil Malyshev068c65b2012-05-16 18:50:11 +000096 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
97 DataMemory.push_back(MB);
98 return (uint8_t*)MB.base();
99}
100
101void TrivialMemoryManager::invalidateInstructionCache() {
102 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
103 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
104 FunctionMemory[i].size());
105
106 for (int i = 0, e = DataMemory.size(); i != e; ++i)
107 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
108 DataMemory[i].size());
Jim Grosbach61425c02012-01-16 22:26:39 +0000109}
110
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000111static const char *ProgramName;
112
113static void Message(const char *Type, const Twine &Msg) {
114 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
115}
116
117static int Error(const Twine &Msg) {
118 Message("error", Msg);
119 return 1;
120}
121
122/* *** */
123
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000124static int printLineInfoForInput() {
125 // If we don't have any input files, read from stdin.
126 if (!InputFileList.size())
127 InputFileList.push_back("-");
128 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
129 // Instantiate a dynamic linker.
Benjamin Kramera757e932013-08-03 22:16:31 +0000130 TrivialMemoryManager MemMgr;
131 RuntimeDyld Dyld(&MemMgr);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000132
133 // Load the input memory buffer.
134 OwningPtr<MemoryBuffer> InputBuffer;
135 OwningPtr<ObjectImage> LoadedObject;
136 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
137 InputBuffer))
138 return Error("unable to read input: '" + ec.message() + "'");
139
140 // Load the object file
141 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
142 if (!LoadedObject) {
143 return Error(Dyld.getErrorString());
144 }
145
146 // Resolve all the relocations we can.
147 Dyld.resolveRelocations();
148
149 OwningPtr<DIContext> Context(DIContext::getDWARFContext(LoadedObject->getObjectFile()));
150
151 // Use symbol info to iterate functions in the object.
152 error_code ec;
153 for (object::symbol_iterator I = LoadedObject->begin_symbols(),
154 E = LoadedObject->end_symbols();
155 I != E && !ec;
156 I.increment(ec)) {
157 object::SymbolRef::Type SymType;
158 if (I->getType(SymType)) continue;
159 if (SymType == object::SymbolRef::ST_Function) {
160 StringRef Name;
161 uint64_t Addr;
162 uint64_t Size;
163 if (I->getName(Name)) continue;
164 if (I->getAddress(Addr)) continue;
165 if (I->getSize(Size)) continue;
166
167 outs() << "Function: " << Name << ", Size = " << Size << "\n";
168
Andrew Kaylore27a7872013-01-26 00:28:05 +0000169 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
170 DILineInfoTable::iterator Begin = Lines.begin();
171 DILineInfoTable::iterator End = Lines.end();
172 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
173 outs() << " Line info @ " << It->first - Addr << ": "
174 << It->second.getFileName()
175 << ", line:" << It->second.getLine() << "\n";
176 }
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000177 }
178 }
179 }
180
181 return 0;
182}
183
Jim Grosbach82c25b42011-03-18 17:24:21 +0000184static int executeInput() {
Jim Grosbach6e563312011-03-21 22:15:52 +0000185 // Instantiate a dynamic linker.
Benjamin Kramera757e932013-08-03 22:16:31 +0000186 TrivialMemoryManager MemMgr;
187 RuntimeDyld Dyld(&MemMgr);
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000188
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000189 // If we don't have any input files, read from stdin.
190 if (!InputFileList.size())
191 InputFileList.push_back("-");
192 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
193 // Load the input memory buffer.
194 OwningPtr<MemoryBuffer> InputBuffer;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000195 OwningPtr<ObjectImage> LoadedObject;
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000196 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
197 InputBuffer))
198 return Error("unable to read input: '" + ec.message() + "'");
199
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000200 // Load the object file
201 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
202 if (!LoadedObject) {
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000203 return Error(Dyld.getErrorString());
204 }
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000205 }
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000206
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000207 // Resolve all the relocations we can.
208 Dyld.resolveRelocations();
Danil Malyshev068c65b2012-05-16 18:50:11 +0000209 // Clear instruction cache before code will be executed.
Benjamin Kramerbd194ce2013-08-03 22:18:45 +0000210 MemMgr.invalidateInstructionCache();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000211
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000212 // FIXME: Error out if there are unresolved relocations.
213
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000214 // Get the address of the entry point (_main by default).
215 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
Jim Grosbach6e563312011-03-21 22:15:52 +0000216 if (MainAddress == 0)
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000217 return Error("no definition for '" + EntryPoint + "'");
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000218
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000219 // Invalidate the instruction cache for each loaded function.
Benjamin Kramerbd194ce2013-08-03 22:18:45 +0000220 for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
221 sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000222 // Make sure the memory is executable.
223 std::string ErrorStr;
224 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
225 if (!sys::Memory::setExecutable(Data, &ErrorStr))
226 return Error("unable to mark function executable: '" + ErrorStr + "'");
227 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000228
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000229 // Dispatch to _main().
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000230 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000231
232 int (*Main)(int, const char**) =
233 (int(*)(int,const char**)) uintptr_t(MainAddress);
234 const char **Argv = new const char*[2];
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000235 // Use the name of the first input object module as argv[0] for the target.
236 Argv[0] = InputFileList[0].c_str();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000237 Argv[1] = 0;
238 return Main(1, Argv);
239}
240
241int main(int argc, char **argv) {
242 ProgramName = argv[0];
243 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
244
245 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
246
247 switch (Action) {
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000248 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000249 return executeInput();
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000250 case AC_PrintLineInfo:
251 return printLineInfoForInput();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000252 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000253}