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