Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 1 | //===-- 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 Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringMap.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DIContext.h" |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/RuntimeDyldChecker.h" |
| 19 | #include "llvm/MC/MCAsmInfo.h" |
| 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCDisassembler.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstPrinter.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstrInfo.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCRegisterInfo.h" |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 25 | #include "llvm/Object/MachO.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 27 | #include "llvm/Support/DynamicLibrary.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
| 29 | #include "llvm/Support/Memory.h" |
| 30 | #include "llvm/Support/MemoryBuffer.h" |
Alp Toker | 9f67932 | 2013-11-05 09:33:43 +0000 | [diff] [blame] | 31 | #include "llvm/Support/PrettyStackTrace.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Signals.h" |
| 33 | #include "llvm/Support/TargetRegistry.h" |
| 34 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 36 | #include <list> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 37 | #include <system_error> |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 38 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | using namespace llvm::object; |
| 41 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 42 | static cl::list<std::string> |
| 43 | InputFileList(cl::Positional, cl::ZeroOrMore, |
| 44 | cl::desc("<input file>")); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 45 | |
| 46 | enum ActionType { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 47 | AC_Execute, |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 48 | AC_PrintLineInfo, |
| 49 | AC_Verify |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static cl::opt<ActionType> |
| 53 | Action(cl::desc("Action to perform:"), |
| 54 | cl::init(AC_Execute), |
| 55 | cl::values(clEnumValN(AC_Execute, "execute", |
| 56 | "Load, link, and execute the inputs."), |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 57 | clEnumValN(AC_PrintLineInfo, "printline", |
| 58 | "Load, link, and print line information for each function."), |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 59 | clEnumValN(AC_Verify, "verify", |
| 60 | "Load, link and verify the resulting memory image."), |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 61 | clEnumValEnd)); |
| 62 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 63 | static cl::opt<std::string> |
| 64 | EntryPoint("entry", |
| 65 | cl::desc("Function to call as entry point."), |
| 66 | cl::init("_main")); |
| 67 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 68 | static cl::list<std::string> |
| 69 | Dylibs("dylib", |
| 70 | cl::desc("Add library."), |
| 71 | cl::ZeroOrMore); |
| 72 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 73 | static cl::opt<std::string> |
| 74 | TripleName("triple", cl::desc("Target triple for disassembler")); |
| 75 | |
| 76 | static cl::list<std::string> |
| 77 | CheckFiles("check", |
| 78 | cl::desc("File containing RuntimeDyld verifier checks."), |
| 79 | cl::ZeroOrMore); |
| 80 | |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 81 | static cl::opt<uint64_t> |
| 82 | TargetAddrStart("target-addr-start", |
| 83 | cl::desc("For -verify only: start of phony target address " |
| 84 | "range."), |
| 85 | cl::init(4096), // Start at "page 1" - no allocating at "null". |
| 86 | cl::Hidden); |
| 87 | |
| 88 | static cl::opt<uint64_t> |
| 89 | TargetAddrEnd("target-addr-end", |
| 90 | cl::desc("For -verify only: end of phony target address range."), |
| 91 | cl::init(~0ULL), |
| 92 | cl::Hidden); |
| 93 | |
| 94 | static cl::opt<uint64_t> |
| 95 | TargetSectionSep("target-section-sep", |
| 96 | cl::desc("For -verify only: Separation between sections in " |
| 97 | "phony target address space."), |
| 98 | cl::init(0), |
| 99 | cl::Hidden); |
| 100 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 101 | static cl::list<std::string> |
| 102 | SpecificSectionMappings("map-section", |
| 103 | cl::desc("Map a section to a specific address."), |
| 104 | cl::ZeroOrMore); |
| 105 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 106 | /* *** */ |
| 107 | |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 108 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 109 | // support library allocation routines directly. |
| 110 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 111 | public: |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 112 | SmallVector<sys::MemoryBlock, 16> FunctionMemory; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 113 | SmallVector<sys::MemoryBlock, 16> DataMemory; |
| 114 | |
| 115 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 116 | unsigned SectionID, |
| 117 | StringRef SectionName) override; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 118 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 119 | unsigned SectionID, StringRef SectionName, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 120 | bool IsReadOnly) override; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 121 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 122 | void *getPointerToNamedFunction(const std::string &Name, |
| 123 | bool AbortOnFailure = true) override { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 124 | return nullptr; |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 127 | bool finalizeMemory(std::string *ErrMsg) override { return false; } |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 128 | |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 129 | // Invalidate instruction cache for sections with execute permissions. |
| 130 | // Some platforms with separate data cache and instruction cache require |
| 131 | // explicit cache flush, otherwise JIT code manipulations (like resolved |
| 132 | // relocations) will get to the data cache but not to the instruction cache. |
| 133 | virtual void invalidateInstructionCache(); |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 136 | uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, |
| 137 | unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 138 | unsigned SectionID, |
| 139 | StringRef SectionName) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 140 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 141 | FunctionMemory.push_back(MB); |
| 142 | return (uint8_t*)MB.base(); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, |
| 146 | unsigned Alignment, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 147 | unsigned SectionID, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 148 | StringRef SectionName, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 149 | bool IsReadOnly) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 150 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 151 | DataMemory.push_back(MB); |
| 152 | return (uint8_t*)MB.base(); |
| 153 | } |
| 154 | |
| 155 | void TrivialMemoryManager::invalidateInstructionCache() { |
| 156 | for (int i = 0, e = FunctionMemory.size(); i != e; ++i) |
| 157 | sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(), |
| 158 | FunctionMemory[i].size()); |
| 159 | |
| 160 | for (int i = 0, e = DataMemory.size(); i != e; ++i) |
| 161 | sys::Memory::InvalidateInstructionCache(DataMemory[i].base(), |
| 162 | DataMemory[i].size()); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 165 | static const char *ProgramName; |
| 166 | |
| 167 | static void Message(const char *Type, const Twine &Msg) { |
| 168 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 169 | } |
| 170 | |
| 171 | static int Error(const Twine &Msg) { |
| 172 | Message("error", Msg); |
| 173 | return 1; |
| 174 | } |
| 175 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 176 | static void loadDylibs() { |
| 177 | for (const std::string &Dylib : Dylibs) { |
| 178 | if (sys::fs::is_regular_file(Dylib)) { |
| 179 | std::string ErrMsg; |
| 180 | if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg)) |
| 181 | llvm::errs() << "Error loading '" << Dylib << "': " |
| 182 | << ErrMsg << "\n"; |
| 183 | } else |
| 184 | llvm::errs() << "Dylib not found: '" << Dylib << "'.\n"; |
| 185 | } |
| 186 | } |
| 187 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 188 | /* *** */ |
| 189 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 190 | static int printLineInfoForInput() { |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 191 | // Load any dylibs requested on the command line. |
| 192 | loadDylibs(); |
| 193 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 194 | // If we don't have any input files, read from stdin. |
| 195 | if (!InputFileList.size()) |
| 196 | InputFileList.push_back("-"); |
| 197 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 198 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 199 | TrivialMemoryManager MemMgr; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 200 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 201 | |
| 202 | // Load the input memory buffer. |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 203 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 204 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 205 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
| 206 | if (std::error_code EC = InputBuffer.getError()) |
| 207 | return Error("unable to read input: '" + EC.message() + "'"); |
| 208 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 209 | ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj( |
| 210 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 211 | |
| 212 | if (std::error_code EC = MaybeObj.getError()) |
| 213 | return Error("unable to create object file: '" + EC.message() + "'"); |
| 214 | |
| 215 | ObjectFile &Obj = **MaybeObj; |
| 216 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 217 | // Load the object file |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 218 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = |
| 219 | Dyld.loadObject(Obj); |
| 220 | |
| 221 | if (Dyld.hasError()) |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 222 | return Error(Dyld.getErrorString()); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 223 | |
| 224 | // Resolve all the relocations we can. |
| 225 | Dyld.resolveRelocations(); |
| 226 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 227 | OwningBinary<ObjectFile> DebugObj = LoadedObjInfo->getObjectForDebug(Obj); |
| 228 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 229 | std::unique_ptr<DIContext> Context( |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 230 | DIContext::getDWARFContext(*DebugObj.getBinary())); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 231 | |
| 232 | // Use symbol info to iterate functions in the object. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 233 | for (object::symbol_iterator I = DebugObj.getBinary()->symbol_begin(), |
| 234 | E = DebugObj.getBinary()->symbol_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 235 | I != E; ++I) { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 236 | object::SymbolRef::Type SymType; |
| 237 | if (I->getType(SymType)) continue; |
| 238 | if (SymType == object::SymbolRef::ST_Function) { |
| 239 | StringRef Name; |
| 240 | uint64_t Addr; |
| 241 | uint64_t Size; |
| 242 | if (I->getName(Name)) continue; |
| 243 | if (I->getAddress(Addr)) continue; |
| 244 | if (I->getSize(Size)) continue; |
| 245 | |
| 246 | outs() << "Function: " << Name << ", Size = " << Size << "\n"; |
| 247 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 248 | DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size); |
| 249 | DILineInfoTable::iterator Begin = Lines.begin(); |
| 250 | DILineInfoTable::iterator End = Lines.end(); |
| 251 | for (DILineInfoTable::iterator It = Begin; It != End; ++It) { |
| 252 | outs() << " Line info @ " << It->first - Addr << ": " |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 253 | << It->second.FileName << ", line:" << It->second.Line << "\n"; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 254 | } |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 262 | static int executeInput() { |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 263 | // Load any dylibs requested on the command line. |
| 264 | loadDylibs(); |
| 265 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 266 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 267 | TrivialMemoryManager MemMgr; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 268 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 269 | |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 270 | // FIXME: Preserve buffers until resolveRelocations time to work around a bug |
| 271 | // in RuntimeDyldELF. |
| 272 | // This fixme should be fixed ASAP. This is a very brittle workaround. |
| 273 | std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers; |
| 274 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 275 | // If we don't have any input files, read from stdin. |
| 276 | if (!InputFileList.size()) |
| 277 | InputFileList.push_back("-"); |
| 278 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 279 | // Load the input memory buffer. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 280 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 281 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
| 282 | if (std::error_code EC = InputBuffer.getError()) |
| 283 | return Error("unable to read input: '" + EC.message() + "'"); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 284 | ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj( |
| 285 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 286 | |
| 287 | if (std::error_code EC = MaybeObj.getError()) |
| 288 | return Error("unable to create object file: '" + EC.message() + "'"); |
| 289 | |
| 290 | ObjectFile &Obj = **MaybeObj; |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 291 | InputBuffers.push_back(std::move(*InputBuffer)); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 292 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 293 | // Load the object file |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 294 | Dyld.loadObject(Obj); |
| 295 | if (Dyld.hasError()) { |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 296 | return Error(Dyld.getErrorString()); |
| 297 | } |
Jim Grosbach | 40411cc | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 298 | } |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 299 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 300 | // Resolve all the relocations we can. |
| 301 | Dyld.resolveRelocations(); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 302 | // Clear instruction cache before code will be executed. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 303 | MemMgr.invalidateInstructionCache(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 304 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 305 | // FIXME: Error out if there are unresolved relocations. |
| 306 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 307 | // Get the address of the entry point (_main by default). |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 308 | void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 309 | if (!MainAddress) |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 310 | return Error("no definition for '" + EntryPoint + "'"); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 311 | |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 312 | // Invalidate the instruction cache for each loaded function. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 313 | for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) { |
| 314 | sys::MemoryBlock &Data = MemMgr.FunctionMemory[i]; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 315 | // Make sure the memory is executable. |
| 316 | std::string ErrorStr; |
| 317 | sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); |
| 318 | if (!sys::Memory::setExecutable(Data, &ErrorStr)) |
| 319 | return Error("unable to mark function executable: '" + ErrorStr + "'"); |
| 320 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 321 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 322 | // Dispatch to _main(). |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 323 | errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 324 | |
| 325 | int (*Main)(int, const char**) = |
| 326 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 327 | const char **Argv = new const char*[2]; |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 328 | // Use the name of the first input object module as argv[0] for the target. |
| 329 | Argv[0] = InputFileList[0].c_str(); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 330 | Argv[1] = nullptr; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 331 | return Main(1, Argv); |
| 332 | } |
| 333 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 334 | static int checkAllExpressions(RuntimeDyldChecker &Checker) { |
| 335 | for (const auto& CheckerFileName : CheckFiles) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 336 | ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf = |
| 337 | MemoryBuffer::getFileOrSTDIN(CheckerFileName); |
| 338 | if (std::error_code EC = CheckerFileBuf.getError()) |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 339 | return Error("unable to read input '" + CheckerFileName + "': " + |
| 340 | EC.message()); |
| 341 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 342 | if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", |
| 343 | CheckerFileBuf.get().get())) |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 344 | return Error("some checks in '" + CheckerFileName + "' failed"); |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 349 | static std::map<void *, uint64_t> |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 350 | applySpecificSectionMappings(RuntimeDyldChecker &Checker) { |
| 351 | |
| 352 | std::map<void*, uint64_t> SpecificMappings; |
| 353 | |
| 354 | for (StringRef Mapping : SpecificSectionMappings) { |
| 355 | |
| 356 | size_t EqualsIdx = Mapping.find_first_of("="); |
| 357 | StringRef SectionIDStr = Mapping.substr(0, EqualsIdx); |
| 358 | size_t ComaIdx = Mapping.find_first_of(","); |
| 359 | |
| 360 | if (ComaIdx == StringRef::npos) { |
| 361 | errs() << "Invalid section specification '" << Mapping |
| 362 | << "'. Should be '<file name>,<section name>=<addr>'\n"; |
| 363 | exit(1); |
| 364 | } |
| 365 | |
| 366 | StringRef FileName = SectionIDStr.substr(0, ComaIdx); |
| 367 | StringRef SectionName = SectionIDStr.substr(ComaIdx + 1); |
| 368 | |
| 369 | uint64_t OldAddrInt; |
| 370 | std::string ErrorMsg; |
| 371 | std::tie(OldAddrInt, ErrorMsg) = |
| 372 | Checker.getSectionAddr(FileName, SectionName, true); |
| 373 | |
| 374 | if (ErrorMsg != "") { |
| 375 | errs() << ErrorMsg; |
| 376 | exit(1); |
| 377 | } |
| 378 | |
| 379 | void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt)); |
| 380 | |
| 381 | StringRef NewAddrStr = Mapping.substr(EqualsIdx + 1); |
| 382 | uint64_t NewAddr; |
| 383 | |
| 384 | if (NewAddrStr.getAsInteger(0, NewAddr)) { |
| 385 | errs() << "Invalid section address in mapping: " << Mapping << "\n"; |
| 386 | exit(1); |
| 387 | } |
| 388 | |
| 389 | Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr); |
| 390 | SpecificMappings[OldAddr] = NewAddr; |
| 391 | } |
| 392 | |
| 393 | return SpecificMappings; |
| 394 | } |
| 395 | |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 396 | // Scatter sections in all directions! |
| 397 | // Remaps section addresses for -verify mode. The following command line options |
| 398 | // can be used to customize the layout of the memory within the phony target's |
| 399 | // address space: |
| 400 | // -target-addr-start <s> -- Specify where the phony target addres range starts. |
| 401 | // -target-addr-end <e> -- Specify where the phony target address range ends. |
| 402 | // -target-section-sep <d> -- Specify how big a gap should be left between the |
| 403 | // end of one section and the start of the next. |
| 404 | // Defaults to zero. Set to something big |
| 405 | // (e.g. 1 << 32) to stress-test stubs, GOTs, etc. |
| 406 | // |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 407 | static void remapSections(const llvm::Triple &TargetTriple, |
| 408 | const TrivialMemoryManager &MemMgr, |
| 409 | RuntimeDyldChecker &Checker) { |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 410 | |
| 411 | // Set up a work list (section addr/size pairs). |
| 412 | typedef std::list<std::pair<void*, uint64_t>> WorklistT; |
| 413 | WorklistT Worklist; |
| 414 | |
| 415 | for (const auto& CodeSection : MemMgr.FunctionMemory) |
| 416 | Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size())); |
| 417 | for (const auto& DataSection : MemMgr.DataMemory) |
| 418 | Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size())); |
| 419 | |
| 420 | // Apply any section-specific mappings that were requested on the command |
| 421 | // line. |
| 422 | typedef std::map<void*, uint64_t> AppliedMappingsT; |
| 423 | AppliedMappingsT AppliedMappings = applySpecificSectionMappings(Checker); |
| 424 | |
| 425 | // Keep an "already allocated" mapping of section target addresses to sizes. |
| 426 | // Sections whose address mappings aren't specified on the command line will |
| 427 | // allocated around the explicitly mapped sections while maintaining the |
| 428 | // minimum separation. |
| 429 | std::map<uint64_t, uint64_t> AlreadyAllocated; |
| 430 | |
| 431 | // Move the previously applied mappings into the already-allocated map. |
| 432 | for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end(); |
| 433 | I != E;) { |
| 434 | WorklistT::iterator Tmp = I; |
| 435 | ++I; |
| 436 | AppliedMappingsT::iterator AI = AppliedMappings.find(Tmp->first); |
| 437 | |
| 438 | if (AI != AppliedMappings.end()) { |
| 439 | AlreadyAllocated[AI->second] = Tmp->second; |
| 440 | Worklist.erase(Tmp); |
| 441 | } |
| 442 | } |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 443 | |
| 444 | // If the -target-addr-end option wasn't explicitly passed, then set it to a |
| 445 | // sensible default based on the target triple. |
| 446 | if (TargetAddrEnd.getNumOccurrences() == 0) { |
| 447 | if (TargetTriple.isArch16Bit()) |
| 448 | TargetAddrEnd = (1ULL << 16) - 1; |
| 449 | else if (TargetTriple.isArch32Bit()) |
| 450 | TargetAddrEnd = (1ULL << 32) - 1; |
| 451 | // TargetAddrEnd already has a sensible default for 64-bit systems, so |
| 452 | // there's nothing to do in the 64-bit case. |
| 453 | } |
| 454 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 455 | // Process any elements remaining in the worklist. |
| 456 | while (!Worklist.empty()) { |
| 457 | std::pair<void*, uint64_t> CurEntry = Worklist.front(); |
| 458 | Worklist.pop_front(); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 459 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 460 | uint64_t NextSectionAddr = TargetAddrStart; |
| 461 | |
| 462 | for (const auto &Alloc : AlreadyAllocated) |
| 463 | if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first) |
| 464 | break; |
| 465 | else |
| 466 | NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep; |
| 467 | |
| 468 | AlreadyAllocated[NextSectionAddr] = CurEntry.second; |
| 469 | Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Load and link the objects specified on the command line, but do not execute |
| 475 | // anything. Instead, attach a RuntimeDyldChecker instance and call it to |
| 476 | // verify the correctness of the linked memory. |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 477 | static int linkAndVerify() { |
| 478 | |
| 479 | // Check for missing triple. |
| 480 | if (TripleName == "") { |
| 481 | llvm::errs() << "Error: -triple required when running in -verify mode.\n"; |
| 482 | return 1; |
| 483 | } |
| 484 | |
| 485 | // Look up the target and build the disassembler. |
| 486 | Triple TheTriple(Triple::normalize(TripleName)); |
| 487 | std::string ErrorStr; |
| 488 | const Target *TheTarget = |
| 489 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
| 490 | if (!TheTarget) { |
| 491 | llvm::errs() << "Error accessing target '" << TripleName << "': " |
| 492 | << ErrorStr << "\n"; |
| 493 | return 1; |
| 494 | } |
| 495 | TripleName = TheTriple.getTriple(); |
| 496 | |
| 497 | std::unique_ptr<MCSubtargetInfo> STI( |
| 498 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 499 | assert(STI && "Unable to create subtarget info!"); |
| 500 | |
| 501 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 502 | assert(MRI && "Unable to create target register info!"); |
| 503 | |
| 504 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 505 | assert(MAI && "Unable to create target asm info!"); |
| 506 | |
| 507 | MCContext Ctx(MAI.get(), MRI.get(), nullptr); |
| 508 | |
| 509 | std::unique_ptr<MCDisassembler> Disassembler( |
| 510 | TheTarget->createMCDisassembler(*STI, Ctx)); |
| 511 | assert(Disassembler && "Unable to create disassembler!"); |
| 512 | |
| 513 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 514 | |
| 515 | std::unique_ptr<MCInstPrinter> InstPrinter( |
Eric Christopher | f801940 | 2015-03-31 00:10:04 +0000 | [diff] [blame] | 516 | TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI)); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 517 | |
| 518 | // Load any dylibs requested on the command line. |
| 519 | loadDylibs(); |
| 520 | |
| 521 | // Instantiate a dynamic linker. |
| 522 | TrivialMemoryManager MemMgr; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 523 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Lang Hames | 925e51b | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 524 | Dyld.setProcessAllSections(true); |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 525 | RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(), |
| 526 | llvm::dbgs()); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 527 | |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 528 | // FIXME: Preserve buffers until resolveRelocations time to work around a bug |
| 529 | // in RuntimeDyldELF. |
| 530 | // This fixme should be fixed ASAP. This is a very brittle workaround. |
| 531 | std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers; |
| 532 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 533 | // If we don't have any input files, read from stdin. |
| 534 | if (!InputFileList.size()) |
| 535 | InputFileList.push_back("-"); |
| 536 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 537 | // Load the input memory buffer. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 538 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 539 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 540 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 541 | if (std::error_code EC = InputBuffer.getError()) |
| 542 | return Error("unable to read input: '" + EC.message() + "'"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 543 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 544 | ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj( |
| 545 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 546 | |
| 547 | if (std::error_code EC = MaybeObj.getError()) |
| 548 | return Error("unable to create object file: '" + EC.message() + "'"); |
| 549 | |
| 550 | ObjectFile &Obj = **MaybeObj; |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 551 | InputBuffers.push_back(std::move(*InputBuffer)); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 552 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 553 | // Load the object file |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 554 | Dyld.loadObject(Obj); |
| 555 | if (Dyld.hasError()) { |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 556 | return Error(Dyld.getErrorString()); |
| 557 | } |
| 558 | } |
| 559 | |
Lang Hames | 375385f | 2014-07-30 03:12:41 +0000 | [diff] [blame] | 560 | // Re-map the section addresses into the phony target address space. |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 561 | remapSections(TheTriple, MemMgr, Checker); |
Lang Hames | 375385f | 2014-07-30 03:12:41 +0000 | [diff] [blame] | 562 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 563 | // Resolve all the relocations we can. |
| 564 | Dyld.resolveRelocations(); |
| 565 | |
Lang Hames | 925e51b | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 566 | // Register EH frames. |
| 567 | Dyld.registerEHFrames(); |
| 568 | |
Lang Hames | ae17268 | 2014-08-05 20:51:46 +0000 | [diff] [blame] | 569 | int ErrorCode = checkAllExpressions(Checker); |
| 570 | if (Dyld.hasError()) { |
| 571 | errs() << "RTDyld reported an error applying relocations:\n " |
| 572 | << Dyld.getErrorString() << "\n"; |
| 573 | ErrorCode = 1; |
| 574 | } |
| 575 | |
| 576 | return ErrorCode; |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 579 | int main(int argc, char **argv) { |
Alp Toker | 9f67932 | 2013-11-05 09:33:43 +0000 | [diff] [blame] | 580 | sys::PrintStackTraceOnErrorSignal(); |
| 581 | PrettyStackTraceProgram X(argc, argv); |
| 582 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 583 | ProgramName = argv[0]; |
| 584 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 585 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 586 | llvm::InitializeAllTargetInfos(); |
| 587 | llvm::InitializeAllTargetMCs(); |
| 588 | llvm::InitializeAllDisassemblers(); |
| 589 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 590 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 591 | |
| 592 | switch (Action) { |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 593 | case AC_Execute: |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 594 | return executeInput(); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 595 | case AC_PrintLineInfo: |
| 596 | return printLineInfoForInput(); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 597 | case AC_Verify: |
| 598 | return linkAndVerify(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 599 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 600 | } |