blob: ac43653d58aa4f8cea9b839abd8153d79fbb42dc [file] [log] [blame]
Jim Grosbach0072cdb2011-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
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000014#include "llvm/ADT/StringMap.h"
Andrew Kaylord55d7012013-01-25 22:50:58 +000015#include "llvm/DebugInfo/DIContext.h"
Andrew Kayloradc70562012-10-02 21:18:39 +000016#include "llvm/ExecutionEngine/ObjectBuffer.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000017#include "llvm/ExecutionEngine/ObjectImage.h"
18#include "llvm/ExecutionEngine/RuntimeDyld.h"
Rafael Espindola6e040c02013-04-26 20:07:33 +000019#include "llvm/Object/MachO.h"
Jim Grosbach0072cdb2011-03-18 17:11:39 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/Memory.h"
23#include "llvm/Support/MemoryBuffer.h"
Alp Toker9f679322013-11-05 09:33:43 +000024#include "llvm/Support/PrettyStackTrace.h"
25#include "llvm/Support/Signals.h"
Jim Grosbach0072cdb2011-03-18 17:11:39 +000026#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/system_error.h"
28using namespace llvm;
29using namespace llvm::object;
30
Jim Grosbach7cb41d72011-04-13 15:49:40 +000031static cl::list<std::string>
32InputFileList(cl::Positional, cl::ZeroOrMore,
33 cl::desc("<input file>"));
Jim Grosbach0072cdb2011-03-18 17:11:39 +000034
35enum ActionType {
Andrew Kaylord55d7012013-01-25 22:50:58 +000036 AC_Execute,
37 AC_PrintLineInfo
Jim Grosbach0072cdb2011-03-18 17:11:39 +000038};
39
40static cl::opt<ActionType>
41Action(cl::desc("Action to perform:"),
42 cl::init(AC_Execute),
43 cl::values(clEnumValN(AC_Execute, "execute",
44 "Load, link, and execute the inputs."),
Andrew Kaylord55d7012013-01-25 22:50:58 +000045 clEnumValN(AC_PrintLineInfo, "printline",
46 "Load, link, and print line information for each function."),
Jim Grosbach0072cdb2011-03-18 17:11:39 +000047 clEnumValEnd));
48
Jim Grosbachd35159a2011-04-13 15:38:30 +000049static cl::opt<std::string>
50EntryPoint("entry",
51 cl::desc("Function to call as entry point."),
52 cl::init("_main"));
53
Jim Grosbach0072cdb2011-03-18 17:11:39 +000054/* *** */
55
Jim Grosbach2dcef0502011-04-04 23:04:39 +000056// A trivial memory manager that doesn't do anything fancy, just uses the
57// support library allocation routines directly.
58class TrivialMemoryManager : public RTDyldMemoryManager {
59public:
Jim Grosbach3ed03f12011-04-12 00:23:32 +000060 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
Jim Grosbacheff0a402012-01-16 22:26:39 +000061 SmallVector<sys::MemoryBlock, 16> DataMemory;
62
63 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
Craig Toppere56917c2014-03-08 08:27:28 +000064 unsigned SectionID,
65 StringRef SectionName) override;
Jim Grosbacheff0a402012-01-16 22:26:39 +000066 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000067 unsigned SectionID, StringRef SectionName,
Craig Toppere56917c2014-03-08 08:27:28 +000068 bool IsReadOnly) override;
Jim Grosbach3ed03f12011-04-12 00:23:32 +000069
Craig Toppere56917c2014-03-08 08:27:28 +000070 void *getPointerToNamedFunction(const std::string &Name,
71 bool AbortOnFailure = true) override {
Danil Malyshevbfee5422012-03-28 21:46:36 +000072 return 0;
73 }
74
Craig Toppere56917c2014-03-08 08:27:28 +000075 bool finalizeMemory(std::string *ErrMsg) override { return false; }
Andrew Kaylora342cb92012-11-15 23:50:01 +000076
Danil Malyshev8c17fbd2012-05-16 18:50:11 +000077 // Invalidate instruction cache for sections with execute permissions.
78 // Some platforms with separate data cache and instruction cache require
79 // explicit cache flush, otherwise JIT code manipulations (like resolved
80 // relocations) will get to the data cache but not to the instruction cache.
81 virtual void invalidateInstructionCache();
Jim Grosbach2dcef0502011-04-04 23:04:39 +000082};
83
Jim Grosbacheff0a402012-01-16 22:26:39 +000084uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
85 unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000086 unsigned SectionID,
87 StringRef SectionName) {
Danil Malyshev8c17fbd2012-05-16 18:50:11 +000088 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
89 FunctionMemory.push_back(MB);
90 return (uint8_t*)MB.base();
Jim Grosbacheff0a402012-01-16 22:26:39 +000091}
92
93uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
94 unsigned Alignment,
Andrew Kaylora342cb92012-11-15 23:50:01 +000095 unsigned SectionID,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000096 StringRef SectionName,
Andrew Kaylora342cb92012-11-15 23:50:01 +000097 bool IsReadOnly) {
Danil Malyshev8c17fbd2012-05-16 18:50:11 +000098 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
99 DataMemory.push_back(MB);
100 return (uint8_t*)MB.base();
101}
102
103void TrivialMemoryManager::invalidateInstructionCache() {
104 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
105 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
106 FunctionMemory[i].size());
107
108 for (int i = 0, e = DataMemory.size(); i != e; ++i)
109 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
110 DataMemory[i].size());
Jim Grosbacheff0a402012-01-16 22:26:39 +0000111}
112
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000113static const char *ProgramName;
114
115static void Message(const char *Type, const Twine &Msg) {
116 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
117}
118
119static int Error(const Twine &Msg) {
120 Message("error", Msg);
121 return 1;
122}
123
124/* *** */
125
Andrew Kaylord55d7012013-01-25 22:50:58 +0000126static int printLineInfoForInput() {
127 // If we don't have any input files, read from stdin.
128 if (!InputFileList.size())
129 InputFileList.push_back("-");
130 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
131 // Instantiate a dynamic linker.
Benjamin Kramer9ce77082013-08-03 22:16:31 +0000132 TrivialMemoryManager MemMgr;
133 RuntimeDyld Dyld(&MemMgr);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000134
135 // Load the input memory buffer.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000136 std::unique_ptr<MemoryBuffer> InputBuffer;
137 std::unique_ptr<ObjectImage> LoadedObject;
Andrew Kaylord55d7012013-01-25 22:50:58 +0000138 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
139 InputBuffer))
140 return Error("unable to read input: '" + ec.message() + "'");
141
142 // Load the object file
Ahmed Charles96c9d952014-03-05 10:19:29 +0000143 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release())));
Andrew Kaylord55d7012013-01-25 22:50:58 +0000144 if (!LoadedObject) {
145 return Error(Dyld.getErrorString());
146 }
147
148 // Resolve all the relocations we can.
149 Dyld.resolveRelocations();
150
Ahmed Charles56440fd2014-03-06 05:51:42 +0000151 std::unique_ptr<DIContext> Context(
152 DIContext::getDWARFContext(LoadedObject->getObjectFile()));
Andrew Kaylord55d7012013-01-25 22:50:58 +0000153
154 // Use symbol info to iterate functions in the object.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000155 for (object::symbol_iterator I = LoadedObject->begin_symbols(),
156 E = LoadedObject->end_symbols();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000157 I != E; ++I) {
Andrew Kaylord55d7012013-01-25 22:50:58 +0000158 object::SymbolRef::Type SymType;
159 if (I->getType(SymType)) continue;
160 if (SymType == object::SymbolRef::ST_Function) {
161 StringRef Name;
162 uint64_t Addr;
163 uint64_t Size;
164 if (I->getName(Name)) continue;
165 if (I->getAddress(Addr)) continue;
166 if (I->getSize(Size)) continue;
167
168 outs() << "Function: " << Name << ", Size = " << Size << "\n";
169
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000170 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
171 DILineInfoTable::iterator Begin = Lines.begin();
172 DILineInfoTable::iterator End = Lines.end();
173 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
174 outs() << " Line info @ " << It->first - Addr << ": "
175 << It->second.getFileName()
176 << ", line:" << It->second.getLine() << "\n";
177 }
Andrew Kaylord55d7012013-01-25 22:50:58 +0000178 }
179 }
180 }
181
182 return 0;
183}
184
Jim Grosbach4d5284b2011-03-18 17:24:21 +0000185static int executeInput() {
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000186 // Instantiate a dynamic linker.
Benjamin Kramer9ce77082013-08-03 22:16:31 +0000187 TrivialMemoryManager MemMgr;
188 RuntimeDyld Dyld(&MemMgr);
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000189
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000190 // If we don't have any input files, read from stdin.
191 if (!InputFileList.size())
192 InputFileList.push_back("-");
193 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
194 // Load the input memory buffer.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000195 std::unique_ptr<MemoryBuffer> InputBuffer;
196 std::unique_ptr<ObjectImage> LoadedObject;
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000197 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
198 InputBuffer))
199 return Error("unable to read input: '" + ec.message() + "'");
200
Andrew Kayloradc70562012-10-02 21:18:39 +0000201 // Load the object file
Ahmed Charles96c9d952014-03-05 10:19:29 +0000202 LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release())));
Andrew Kayloradc70562012-10-02 21:18:39 +0000203 if (!LoadedObject) {
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000204 return Error(Dyld.getErrorString());
205 }
Jim Grosbach40411cc2011-03-22 18:19:42 +0000206 }
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000207
Jim Grosbach733d3052011-04-12 21:20:41 +0000208 // Resolve all the relocations we can.
209 Dyld.resolveRelocations();
Danil Malyshev8c17fbd2012-05-16 18:50:11 +0000210 // Clear instruction cache before code will be executed.
Benjamin Kramer5d62ad22013-08-03 22:18:45 +0000211 MemMgr.invalidateInstructionCache();
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000212
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000213 // FIXME: Error out if there are unresolved relocations.
214
Jim Grosbachd35159a2011-04-13 15:38:30 +0000215 // Get the address of the entry point (_main by default).
216 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000217 if (MainAddress == 0)
Jim Grosbachd35159a2011-04-13 15:38:30 +0000218 return Error("no definition for '" + EntryPoint + "'");
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000219
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000220 // Invalidate the instruction cache for each loaded function.
Benjamin Kramer5d62ad22013-08-03 22:18:45 +0000221 for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
222 sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000223 // Make sure the memory is executable.
224 std::string ErrorStr;
225 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
226 if (!sys::Memory::setExecutable(Data, &ErrorStr))
227 return Error("unable to mark function executable: '" + ErrorStr + "'");
228 }
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000229
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000230 // Dispatch to _main().
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000231 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000232
233 int (*Main)(int, const char**) =
234 (int(*)(int,const char**)) uintptr_t(MainAddress);
235 const char **Argv = new const char*[2];
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000236 // Use the name of the first input object module as argv[0] for the target.
237 Argv[0] = InputFileList[0].c_str();
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000238 Argv[1] = 0;
239 return Main(1, Argv);
240}
241
242int main(int argc, char **argv) {
Alp Toker9f679322013-11-05 09:33:43 +0000243 sys::PrintStackTraceOnErrorSignal();
244 PrettyStackTraceProgram X(argc, argv);
245
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000246 ProgramName = argv[0];
247 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
248
249 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
250
251 switch (Action) {
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000252 case AC_Execute:
Jim Grosbach4d5284b2011-03-18 17:24:21 +0000253 return executeInput();
Andrew Kaylord55d7012013-01-25 22:50:58 +0000254 case AC_PrintLineInfo:
255 return printLineInfoForInput();
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000256 }
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000257}