blob: b93ac640b855788ad27da061ef626a1aa8c7378b [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,
63 unsigned SectionID);
64 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Andrew Kaylor53608a32012-11-15 23:50:01 +000065 unsigned SectionID, bool IsReadOnly);
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000066
Danil Malyshev30b9e322012-03-28 21:46:36 +000067 virtual void *getPointerToNamedFunction(const std::string &Name,
68 bool AbortOnFailure = true) {
69 return 0;
70 }
71
David Tweedabb38fe2013-05-17 10:01:46 +000072 bool finalizeMemory(std::string *ErrMsg) { return false; }
Andrew Kaylor53608a32012-11-15 23:50:01 +000073
Danil Malyshev068c65b2012-05-16 18:50:11 +000074 // Invalidate instruction cache for sections with execute permissions.
75 // Some platforms with separate data cache and instruction cache require
76 // explicit cache flush, otherwise JIT code manipulations (like resolved
77 // relocations) will get to the data cache but not to the instruction cache.
78 virtual void invalidateInstructionCache();
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000079};
80
Jim Grosbach61425c02012-01-16 22:26:39 +000081uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
82 unsigned Alignment,
83 unsigned SectionID) {
Danil Malyshev068c65b2012-05-16 18:50:11 +000084 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
85 FunctionMemory.push_back(MB);
86 return (uint8_t*)MB.base();
Jim Grosbach61425c02012-01-16 22:26:39 +000087}
88
89uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
90 unsigned Alignment,
Andrew Kaylor53608a32012-11-15 23:50:01 +000091 unsigned SectionID,
92 bool IsReadOnly) {
Danil Malyshev068c65b2012-05-16 18:50:11 +000093 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
94 DataMemory.push_back(MB);
95 return (uint8_t*)MB.base();
96}
97
98void TrivialMemoryManager::invalidateInstructionCache() {
99 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
100 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
101 FunctionMemory[i].size());
102
103 for (int i = 0, e = DataMemory.size(); i != e; ++i)
104 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
105 DataMemory[i].size());
Jim Grosbach61425c02012-01-16 22:26:39 +0000106}
107
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000108static const char *ProgramName;
109
110static void Message(const char *Type, const Twine &Msg) {
111 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
112}
113
114static int Error(const Twine &Msg) {
115 Message("error", Msg);
116 return 1;
117}
118
119/* *** */
120
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000121static int printLineInfoForInput() {
122 // If we don't have any input files, read from stdin.
123 if (!InputFileList.size())
124 InputFileList.push_back("-");
125 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
126 // Instantiate a dynamic linker.
Benjamin Kramera757e932013-08-03 22:16:31 +0000127 TrivialMemoryManager MemMgr;
128 RuntimeDyld Dyld(&MemMgr);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000129
130 // Load the input memory buffer.
131 OwningPtr<MemoryBuffer> InputBuffer;
132 OwningPtr<ObjectImage> LoadedObject;
133 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
134 InputBuffer))
135 return Error("unable to read input: '" + ec.message() + "'");
136
137 // Load the object file
138 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
139 if (!LoadedObject) {
140 return Error(Dyld.getErrorString());
141 }
142
143 // Resolve all the relocations we can.
144 Dyld.resolveRelocations();
145
146 OwningPtr<DIContext> Context(DIContext::getDWARFContext(LoadedObject->getObjectFile()));
147
148 // Use symbol info to iterate functions in the object.
149 error_code ec;
150 for (object::symbol_iterator I = LoadedObject->begin_symbols(),
151 E = LoadedObject->end_symbols();
152 I != E && !ec;
153 I.increment(ec)) {
154 object::SymbolRef::Type SymType;
155 if (I->getType(SymType)) continue;
156 if (SymType == object::SymbolRef::ST_Function) {
157 StringRef Name;
158 uint64_t Addr;
159 uint64_t Size;
160 if (I->getName(Name)) continue;
161 if (I->getAddress(Addr)) continue;
162 if (I->getSize(Size)) continue;
163
164 outs() << "Function: " << Name << ", Size = " << Size << "\n";
165
Andrew Kaylore27a7872013-01-26 00:28:05 +0000166 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
167 DILineInfoTable::iterator Begin = Lines.begin();
168 DILineInfoTable::iterator End = Lines.end();
169 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
170 outs() << " Line info @ " << It->first - Addr << ": "
171 << It->second.getFileName()
172 << ", line:" << It->second.getLine() << "\n";
173 }
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000174 }
175 }
176 }
177
178 return 0;
179}
180
Jim Grosbach82c25b42011-03-18 17:24:21 +0000181static int executeInput() {
Jim Grosbach6e563312011-03-21 22:15:52 +0000182 // Instantiate a dynamic linker.
Benjamin Kramera757e932013-08-03 22:16:31 +0000183 TrivialMemoryManager MemMgr;
184 RuntimeDyld Dyld(&MemMgr);
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000185
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000186 // If we don't have any input files, read from stdin.
187 if (!InputFileList.size())
188 InputFileList.push_back("-");
189 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
190 // Load the input memory buffer.
191 OwningPtr<MemoryBuffer> InputBuffer;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000192 OwningPtr<ObjectImage> LoadedObject;
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000193 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
194 InputBuffer))
195 return Error("unable to read input: '" + ec.message() + "'");
196
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000197 // Load the object file
198 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
199 if (!LoadedObject) {
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000200 return Error(Dyld.getErrorString());
201 }
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000202 }
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000203
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000204 // Resolve all the relocations we can.
205 Dyld.resolveRelocations();
Danil Malyshev068c65b2012-05-16 18:50:11 +0000206 // Clear instruction cache before code will be executed.
207 MemMgr->invalidateInstructionCache();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000208
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000209 // FIXME: Error out if there are unresolved relocations.
210
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000211 // Get the address of the entry point (_main by default).
212 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
Jim Grosbach6e563312011-03-21 22:15:52 +0000213 if (MainAddress == 0)
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000214 return Error("no definition for '" + EntryPoint + "'");
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000215
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000216 // Invalidate the instruction cache for each loaded function.
217 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
218 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
219 // Make sure the memory is executable.
220 std::string ErrorStr;
221 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
222 if (!sys::Memory::setExecutable(Data, &ErrorStr))
223 return Error("unable to mark function executable: '" + ErrorStr + "'");
224 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000225
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000226 // Dispatch to _main().
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000227 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000228
229 int (*Main)(int, const char**) =
230 (int(*)(int,const char**)) uintptr_t(MainAddress);
231 const char **Argv = new const char*[2];
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000232 // Use the name of the first input object module as argv[0] for the target.
233 Argv[0] = InputFileList[0].c_str();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000234 Argv[1] = 0;
235 return Main(1, Argv);
236}
237
238int main(int argc, char **argv) {
239 ProgramName = argv[0];
240 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
241
242 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
243
244 switch (Action) {
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000245 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000246 return executeInput();
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000247 case AC_PrintLineInfo:
248 return printLineInfoForInput();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000249 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000250}