| 1 | //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MCJIT.h" |
| 10 | #include "llvm/ADT/STLExtras.h" |
| 11 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 12 | #include "llvm/ExecutionEngine/JITEventListener.h" |
| 13 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 14 | #include "llvm/ExecutionEngine/ObjectCache.h" |
| 15 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | #include "llvm/IR/Function.h" |
| 19 | #include "llvm/IR/LegacyPassManager.h" |
| 20 | #include "llvm/IR/Mangler.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/Object/Archive.h" |
| 23 | #include "llvm/Object/ObjectFile.h" |
| 24 | #include "llvm/Support/DynamicLibrary.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include "llvm/Support/SmallVectorMemoryBuffer.h" |
| 28 | #include <mutex> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | static struct RegisterJIT { |
| 35 | RegisterJIT() { MCJIT::Register(); } |
| 36 | } JITRegistrator; |
| 37 | |
| 38 | } |
| 39 | |
| 40 | extern "C" void LLVMLinkInMCJIT() { |
| 41 | } |
| 42 | |
| 43 | ExecutionEngine * |
| 44 | MCJIT::createJIT(std::unique_ptr<Module> M, std::string *ErrorStr, |
| 45 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
| 46 | std::shared_ptr<LegacyJITSymbolResolver> Resolver, |
| 47 | std::unique_ptr<TargetMachine> TM) { |
| 48 | // Try to register the program as a source of symbols to resolve against. |
| 49 | // |
| 50 | // FIXME: Don't do this here. |
| 51 | sys::DynamicLibrary::LoadLibraryPermanently(Filename: nullptr, ErrMsg: nullptr); |
| 52 | |
| 53 | if (!MemMgr || !Resolver) { |
| 54 | auto RTDyldMM = std::make_shared<SectionMemoryManager>(); |
| 55 | if (!MemMgr) |
| 56 | MemMgr = RTDyldMM; |
| 57 | if (!Resolver) |
| 58 | Resolver = RTDyldMM; |
| 59 | } |
| 60 | |
| 61 | return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr), |
| 62 | std::move(Resolver)); |
| 63 | } |
| 64 | |
| 65 | MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM, |
| 66 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
| 67 | std::shared_ptr<LegacyJITSymbolResolver> Resolver) |
| 68 | : ExecutionEngine(DataLayout(TM->getTargetTriple().computeDataLayout( |
| 69 | ABIName: TM->Options.MCOptions.getABIName())), |
| 70 | std::move(M)), |
| 71 | TM(std::move(TM)), Ctx(nullptr), MemMgr(std::move(MemMgr)), |
| 72 | Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver), |
| 73 | ObjCache(nullptr) { |
| 74 | // FIXME: We are managing our modules, so we do not want the base class |
| 75 | // ExecutionEngine to manage them as well. To avoid double destruction |
| 76 | // of the first (and only) module added in ExecutionEngine constructor |
| 77 | // we remove it from EE and will destruct it ourselves. |
| 78 | // |
| 79 | // It may make sense to move our module manager (based on SmallStPtr) back |
| 80 | // into EE if the JIT and Interpreter can live with it. |
| 81 | // If so, additional functions: addModule, removeModule, FindFunctionNamed, |
| 82 | // runStaticConstructorsDestructors could be moved back to EE as well. |
| 83 | // |
| 84 | std::unique_ptr<Module> First = std::move(Modules[0]); |
| 85 | Modules.clear(); |
| 86 | |
| 87 | if (First->getDataLayout().isDefault()) |
| 88 | First->setDataLayout(getDataLayout()); |
| 89 | |
| 90 | OwnedModules.addModule(M: std::move(First)); |
| 91 | RegisterJITEventListener(L: JITEventListener::createGDBRegistrationListener()); |
| 92 | } |
| 93 | |
| 94 | MCJIT::~MCJIT() { |
| 95 | std::lock_guard<sys::Mutex> locked(lock); |
| 96 | |
| 97 | Dyld.deregisterEHFrames(); |
| 98 | |
| 99 | for (auto &Obj : LoadedObjects) |
| 100 | if (Obj) |
| 101 | notifyFreeingObject(Obj: *Obj); |
| 102 | |
| 103 | Archives.clear(); |
| 104 | } |
| 105 | |
| 106 | void MCJIT::addModule(std::unique_ptr<Module> M) { |
| 107 | std::lock_guard<sys::Mutex> locked(lock); |
| 108 | |
| 109 | if (M->getDataLayout().isDefault()) |
| 110 | M->setDataLayout(getDataLayout()); |
| 111 | |
| 112 | OwnedModules.addModule(M: std::move(M)); |
| 113 | } |
| 114 | |
| 115 | bool MCJIT::removeModule(Module *M) { |
| 116 | std::lock_guard<sys::Mutex> locked(lock); |
| 117 | return OwnedModules.removeModule(M); |
| 118 | } |
| 119 | |
| 120 | void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) { |
| 121 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(O: *Obj); |
| 122 | if (Dyld.hasError()) |
| 123 | report_fatal_error(reason: Dyld.getErrorString()); |
| 124 | |
| 125 | notifyObjectLoaded(Obj: *Obj, L: *L); |
| 126 | |
| 127 | LoadedObjects.push_back(Elt: std::move(Obj)); |
| 128 | } |
| 129 | |
| 130 | void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) { |
| 131 | std::unique_ptr<object::ObjectFile> ObjFile; |
| 132 | std::unique_ptr<MemoryBuffer> MemBuf; |
| 133 | std::tie(args&: ObjFile, args&: MemBuf) = Obj.takeBinary(); |
| 134 | addObjectFile(Obj: std::move(ObjFile)); |
| 135 | Buffers.push_back(Elt: std::move(MemBuf)); |
| 136 | } |
| 137 | |
| 138 | void MCJIT::addArchive(object::OwningBinary<object::Archive> A) { |
| 139 | Archives.push_back(Elt: std::move(A)); |
| 140 | } |
| 141 | |
| 142 | void MCJIT::setObjectCache(ObjectCache* NewCache) { |
| 143 | std::lock_guard<sys::Mutex> locked(lock); |
| 144 | ObjCache = NewCache; |
| 145 | } |
| 146 | |
| 147 | std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) { |
| 148 | assert(M && "Can not emit a null module" ); |
| 149 | |
| 150 | std::lock_guard<sys::Mutex> locked(lock); |
| 151 | |
| 152 | // Materialize all globals in the module if they have not been |
| 153 | // materialized already. |
| 154 | cantFail(Err: M->materializeAll()); |
| 155 | |
| 156 | // This must be a module which has already been added but not loaded to this |
| 157 | // MCJIT instance, since these conditions are tested by our caller, |
| 158 | // generateCodeForModule. |
| 159 | |
| 160 | legacy::PassManager PM; |
| 161 | |
| 162 | // The RuntimeDyld will take ownership of this shortly |
| 163 | SmallVector<char, 4096> ObjBufferSV; |
| 164 | raw_svector_ostream ObjStream(ObjBufferSV); |
| 165 | |
| 166 | // Turn the machine code intermediate representation into bytes in memory |
| 167 | // that may be executed. |
| 168 | if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules())) |
| 169 | report_fatal_error(reason: "Target does not support MC emission!" ); |
| 170 | |
| 171 | // Initialize passes. |
| 172 | PM.run(M&: *M); |
| 173 | // Flush the output buffer to get the generated code into memory |
| 174 | |
| 175 | auto CompiledObjBuffer = std::make_unique<SmallVectorMemoryBuffer>( |
| 176 | args: std::move(ObjBufferSV), /*RequiresNullTerminator=*/args: false); |
| 177 | |
| 178 | // If we have an object cache, tell it about the new object. |
| 179 | // Note that we're using the compiled image, not the loaded image (as below). |
| 180 | if (ObjCache) { |
| 181 | // MemoryBuffer is a thin wrapper around the actual memory, so it's OK |
| 182 | // to create a temporary object here and delete it after the call. |
| 183 | MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef(); |
| 184 | ObjCache->notifyObjectCompiled(M, Obj: MB); |
| 185 | } |
| 186 | |
| 187 | return CompiledObjBuffer; |
| 188 | } |
| 189 | |
| 190 | void MCJIT::generateCodeForModule(Module *M) { |
| 191 | // Get a thread lock to make sure we aren't trying to load multiple times |
| 192 | std::lock_guard<sys::Mutex> locked(lock); |
| 193 | |
| 194 | // This must be a module which has already been added to this MCJIT instance. |
| 195 | assert(OwnedModules.ownsModule(M) && |
| 196 | "MCJIT::generateCodeForModule: Unknown module." ); |
| 197 | |
| 198 | // Re-compilation is not supported |
| 199 | if (OwnedModules.hasModuleBeenLoaded(M)) |
| 200 | return; |
| 201 | |
| 202 | std::unique_ptr<MemoryBuffer> ObjectToLoad; |
| 203 | // Try to load the pre-compiled object from cache if possible |
| 204 | if (ObjCache) |
| 205 | ObjectToLoad = ObjCache->getObject(M); |
| 206 | |
| 207 | assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch" ); |
| 208 | |
| 209 | // If the cache did not contain a suitable object, compile the object |
| 210 | if (!ObjectToLoad) { |
| 211 | ObjectToLoad = emitObject(M); |
| 212 | assert(ObjectToLoad && "Compilation did not produce an object." ); |
| 213 | } |
| 214 | |
| 215 | // Load the object into the dynamic linker. |
| 216 | // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). |
| 217 | Expected<std::unique_ptr<object::ObjectFile>> LoadedObject = |
| 218 | object::ObjectFile::createObjectFile(Object: ObjectToLoad->getMemBufferRef()); |
| 219 | if (!LoadedObject) { |
| 220 | std::string Buf; |
| 221 | raw_string_ostream OS(Buf); |
| 222 | logAllUnhandledErrors(E: LoadedObject.takeError(), OS); |
| 223 | report_fatal_error(reason: Twine(Buf)); |
| 224 | } |
| 225 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = |
| 226 | Dyld.loadObject(O: *LoadedObject.get()); |
| 227 | |
| 228 | if (Dyld.hasError()) |
| 229 | report_fatal_error(reason: Dyld.getErrorString()); |
| 230 | |
| 231 | notifyObjectLoaded(Obj: *LoadedObject.get(), L: *L); |
| 232 | |
| 233 | Buffers.push_back(Elt: std::move(ObjectToLoad)); |
| 234 | LoadedObjects.push_back(Elt: std::move(*LoadedObject)); |
| 235 | |
| 236 | OwnedModules.markModuleAsLoaded(M); |
| 237 | } |
| 238 | |
| 239 | void MCJIT::finalizeLoadedModules() { |
| 240 | std::lock_guard<sys::Mutex> locked(lock); |
| 241 | |
| 242 | // Resolve any outstanding relocations. |
| 243 | Dyld.resolveRelocations(); |
| 244 | |
| 245 | // Check for Dyld error. |
| 246 | if (Dyld.hasError()) |
| 247 | ErrMsg = Dyld.getErrorString().str(); |
| 248 | |
| 249 | OwnedModules.markAllLoadedModulesAsFinalized(); |
| 250 | |
| 251 | // Register EH frame data for any module we own which has been loaded |
| 252 | Dyld.registerEHFrames(); |
| 253 | |
| 254 | // Set page permissions. |
| 255 | MemMgr->finalizeMemory(); |
| 256 | } |
| 257 | |
| 258 | // FIXME: Rename this. |
| 259 | void MCJIT::finalizeObject() { |
| 260 | std::lock_guard<sys::Mutex> locked(lock); |
| 261 | |
| 262 | // Generate code for module is going to move objects out of the 'added' list, |
| 263 | // so we need to copy that out before using it: |
| 264 | SmallVector<Module *, 16> ModsToAdd(OwnedModules.added()); |
| 265 | |
| 266 | for (auto *M : ModsToAdd) |
| 267 | generateCodeForModule(M); |
| 268 | |
| 269 | finalizeLoadedModules(); |
| 270 | } |
| 271 | |
| 272 | void MCJIT::finalizeModule(Module *M) { |
| 273 | std::lock_guard<sys::Mutex> locked(lock); |
| 274 | |
| 275 | // This must be a module which has already been added to this MCJIT instance. |
| 276 | assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module." ); |
| 277 | |
| 278 | // If the module hasn't been compiled, just do that. |
| 279 | if (!OwnedModules.hasModuleBeenLoaded(M)) |
| 280 | generateCodeForModule(M); |
| 281 | |
| 282 | finalizeLoadedModules(); |
| 283 | } |
| 284 | |
| 285 | JITSymbol MCJIT::findExistingSymbol(const std::string &Name) { |
| 286 | if (void *Addr = getPointerToGlobalIfAvailable(S: Name)) |
| 287 | return JITSymbol(static_cast<uint64_t>( |
| 288 | reinterpret_cast<uintptr_t>(Addr)), |
| 289 | JITSymbolFlags::Exported); |
| 290 | |
| 291 | return Dyld.getSymbol(Name); |
| 292 | } |
| 293 | |
| 294 | Module *MCJIT::findModuleForSymbol(const std::string &Name, |
| 295 | bool CheckFunctionsOnly) { |
| 296 | StringRef DemangledName = Name; |
| 297 | if (DemangledName[0] == getDataLayout().getGlobalPrefix()) |
| 298 | DemangledName = DemangledName.substr(Start: 1); |
| 299 | |
| 300 | std::lock_guard<sys::Mutex> locked(lock); |
| 301 | |
| 302 | // If it hasn't already been generated, see if it's in one of our modules. |
| 303 | for (ModulePtrSet::iterator I = OwnedModules.begin_added(), |
| 304 | E = OwnedModules.end_added(); |
| 305 | I != E; ++I) { |
| 306 | Module *M = *I; |
| 307 | Function *F = M->getFunction(Name: DemangledName); |
| 308 | if (F && !F->isDeclaration()) |
| 309 | return M; |
| 310 | if (!CheckFunctionsOnly) { |
| 311 | GlobalVariable *G = M->getGlobalVariable(Name: DemangledName); |
| 312 | if (G && !G->isDeclaration()) |
| 313 | return M; |
| 314 | // FIXME: Do we need to worry about global aliases? |
| 315 | } |
| 316 | } |
| 317 | // We didn't find the symbol in any of our modules. |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | uint64_t MCJIT::getSymbolAddress(const std::string &Name, |
| 322 | bool CheckFunctionsOnly) { |
| 323 | std::string MangledName; |
| 324 | { |
| 325 | raw_string_ostream MangledNameStream(MangledName); |
| 326 | Mangler::getNameWithPrefix(OS&: MangledNameStream, GVName: Name, DL: getDataLayout()); |
| 327 | } |
| 328 | if (auto Sym = findSymbol(Name: MangledName, CheckFunctionsOnly)) { |
| 329 | if (auto AddrOrErr = Sym.getAddress()) |
| 330 | return *AddrOrErr; |
| 331 | else |
| 332 | report_fatal_error(Err: AddrOrErr.takeError()); |
| 333 | } else if (auto Err = Sym.takeError()) |
| 334 | report_fatal_error(Err: Sym.takeError()); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | JITSymbol MCJIT::findSymbol(const std::string &Name, |
| 339 | bool CheckFunctionsOnly) { |
| 340 | std::lock_guard<sys::Mutex> locked(lock); |
| 341 | |
| 342 | // First, check to see if we already have this symbol. |
| 343 | if (auto Sym = findExistingSymbol(Name)) |
| 344 | return Sym; |
| 345 | |
| 346 | for (object::OwningBinary<object::Archive> &OB : Archives) { |
| 347 | object::Archive *A = OB.getBinary(); |
| 348 | // Look for our symbols in each Archive |
| 349 | auto OptionalChildOrErr = A->findSym(name: Name); |
| 350 | if (!OptionalChildOrErr) |
| 351 | report_fatal_error(Err: OptionalChildOrErr.takeError()); |
| 352 | auto &OptionalChild = *OptionalChildOrErr; |
| 353 | if (OptionalChild) { |
| 354 | // FIXME: Support nested archives? |
| 355 | Expected<std::unique_ptr<object::Binary>> ChildBinOrErr = |
| 356 | OptionalChild->getAsBinary(); |
| 357 | if (!ChildBinOrErr) { |
| 358 | // TODO: Actually report errors helpfully. |
| 359 | consumeError(Err: ChildBinOrErr.takeError()); |
| 360 | continue; |
| 361 | } |
| 362 | std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); |
| 363 | if (ChildBin->isObject()) { |
| 364 | std::unique_ptr<object::ObjectFile> OF( |
| 365 | static_cast<object::ObjectFile *>(ChildBin.release())); |
| 366 | // This causes the object file to be loaded. |
| 367 | addObjectFile(Obj: std::move(OF)); |
| 368 | // The address should be here now. |
| 369 | if (auto Sym = findExistingSymbol(Name)) |
| 370 | return Sym; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // If it hasn't already been generated, see if it's in one of our modules. |
| 376 | Module *M = findModuleForSymbol(Name, CheckFunctionsOnly); |
| 377 | if (M) { |
| 378 | generateCodeForModule(M); |
| 379 | |
| 380 | // Check the RuntimeDyld table again, it should be there now. |
| 381 | return findExistingSymbol(Name); |
| 382 | } |
| 383 | |
| 384 | // If a LazyFunctionCreator is installed, use it to get/create the function. |
| 385 | // FIXME: Should we instead have a LazySymbolCreator callback? |
| 386 | if (LazyFunctionCreator) { |
| 387 | auto Addr = static_cast<uint64_t>( |
| 388 | reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name))); |
| 389 | return JITSymbol(Addr, JITSymbolFlags::Exported); |
| 390 | } |
| 391 | |
| 392 | return nullptr; |
| 393 | } |
| 394 | |
| 395 | uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { |
| 396 | std::lock_guard<sys::Mutex> locked(lock); |
| 397 | uint64_t Result = getSymbolAddress(Name, CheckFunctionsOnly: false); |
| 398 | if (Result != 0) |
| 399 | finalizeLoadedModules(); |
| 400 | return Result; |
| 401 | } |
| 402 | |
| 403 | uint64_t MCJIT::getFunctionAddress(const std::string &Name) { |
| 404 | std::lock_guard<sys::Mutex> locked(lock); |
| 405 | uint64_t Result = getSymbolAddress(Name, CheckFunctionsOnly: true); |
| 406 | if (Result != 0) |
| 407 | finalizeLoadedModules(); |
| 408 | return Result; |
| 409 | } |
| 410 | |
| 411 | // Deprecated. Use getFunctionAddress instead. |
| 412 | void *MCJIT::getPointerToFunction(Function *F) { |
| 413 | std::lock_guard<sys::Mutex> locked(lock); |
| 414 | |
| 415 | Mangler Mang; |
| 416 | SmallString<128> Name; |
| 417 | TM->getNameWithPrefix(Name, GV: F, Mang); |
| 418 | |
| 419 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 420 | bool AbortOnFailure = !F->hasExternalWeakLinkage(); |
| 421 | void *Addr = getPointerToNamedFunction(Name, AbortOnFailure); |
| 422 | updateGlobalMapping(GV: F, Addr); |
| 423 | return Addr; |
| 424 | } |
| 425 | |
| 426 | Module *M = F->getParent(); |
| 427 | bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M); |
| 428 | |
| 429 | // Make sure the relevant module has been compiled and loaded. |
| 430 | if (HasBeenAddedButNotLoaded) |
| 431 | generateCodeForModule(M); |
| 432 | else if (!OwnedModules.hasModuleBeenLoaded(M)) { |
| 433 | // If this function doesn't belong to one of our modules, we're done. |
| 434 | // FIXME: Asking for the pointer to a function that hasn't been registered, |
| 435 | // and isn't a declaration (which is handled above) should probably |
| 436 | // be an assertion. |
| 437 | return nullptr; |
| 438 | } |
| 439 | |
| 440 | // FIXME: Should the Dyld be retaining module information? Probably not. |
| 441 | // |
| 442 | // This is the accessor for the target address, so make sure to check the |
| 443 | // load address of the symbol, not the local address. |
| 444 | return (void*)Dyld.getSymbol(Name).getAddress(); |
| 445 | } |
| 446 | |
| 447 | void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( |
| 448 | bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { |
| 449 | for (; I != E; ++I) { |
| 450 | ExecutionEngine::runStaticConstructorsDestructors(module&: **I, isDtors); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void MCJIT::runStaticConstructorsDestructors(bool isDtors) { |
| 455 | // Execute global ctors/dtors for each module in the program. |
| 456 | runStaticConstructorsDestructorsInModulePtrSet( |
| 457 | isDtors, I: OwnedModules.begin_added(), E: OwnedModules.end_added()); |
| 458 | runStaticConstructorsDestructorsInModulePtrSet( |
| 459 | isDtors, I: OwnedModules.begin_loaded(), E: OwnedModules.end_loaded()); |
| 460 | runStaticConstructorsDestructorsInModulePtrSet( |
| 461 | isDtors, I: OwnedModules.begin_finalized(), E: OwnedModules.end_finalized()); |
| 462 | } |
| 463 | |
| 464 | Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName, |
| 465 | ModulePtrSet::iterator I, |
| 466 | ModulePtrSet::iterator E) { |
| 467 | for (; I != E; ++I) { |
| 468 | Function *F = (*I)->getFunction(Name: FnName); |
| 469 | if (F && !F->isDeclaration()) |
| 470 | return F; |
| 471 | } |
| 472 | return nullptr; |
| 473 | } |
| 474 | |
| 475 | GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name, |
| 476 | bool AllowInternal, |
| 477 | ModulePtrSet::iterator I, |
| 478 | ModulePtrSet::iterator E) { |
| 479 | for (; I != E; ++I) { |
| 480 | GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal); |
| 481 | if (GV && !GV->isDeclaration()) |
| 482 | return GV; |
| 483 | } |
| 484 | return nullptr; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | Function *MCJIT::FindFunctionNamed(StringRef FnName) { |
| 489 | Function *F = FindFunctionNamedInModulePtrSet( |
| 490 | FnName, I: OwnedModules.begin_added(), E: OwnedModules.end_added()); |
| 491 | if (!F) |
| 492 | F = FindFunctionNamedInModulePtrSet(FnName, I: OwnedModules.begin_loaded(), |
| 493 | E: OwnedModules.end_loaded()); |
| 494 | if (!F) |
| 495 | F = FindFunctionNamedInModulePtrSet(FnName, I: OwnedModules.begin_finalized(), |
| 496 | E: OwnedModules.end_finalized()); |
| 497 | return F; |
| 498 | } |
| 499 | |
| 500 | GlobalVariable *MCJIT::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) { |
| 501 | GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet( |
| 502 | Name, AllowInternal, I: OwnedModules.begin_added(), E: OwnedModules.end_added()); |
| 503 | if (!GV) |
| 504 | GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, I: OwnedModules.begin_loaded(), |
| 505 | E: OwnedModules.end_loaded()); |
| 506 | if (!GV) |
| 507 | GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, I: OwnedModules.begin_finalized(), |
| 508 | E: OwnedModules.end_finalized()); |
| 509 | return GV; |
| 510 | } |
| 511 | |
| 512 | GenericValue MCJIT::runFunction(Function *F, ArrayRef<GenericValue> ArgValues) { |
| 513 | assert(F && "Function *F was null at entry to run()" ); |
| 514 | |
| 515 | void *FPtr = getPointerToFunction(F); |
| 516 | finalizeModule(M: F->getParent()); |
| 517 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction" ); |
| 518 | FunctionType *FTy = F->getFunctionType(); |
| 519 | Type *RetTy = FTy->getReturnType(); |
| 520 | |
| 521 | assert((FTy->getNumParams() == ArgValues.size() || |
| 522 | (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
| 523 | "Wrong number of arguments passed into function!" ); |
| 524 | assert(FTy->getNumParams() == ArgValues.size() && |
| 525 | "This doesn't support passing arguments through varargs (yet)!" ); |
| 526 | |
| 527 | // Handle some common cases first. These cases correspond to common `main' |
| 528 | // prototypes. |
| 529 | if (RetTy->isIntegerTy(BitWidth: 32) || RetTy->isVoidTy()) { |
| 530 | switch (ArgValues.size()) { |
| 531 | case 3: |
| 532 | if (FTy->getParamType(i: 0)->isIntegerTy(BitWidth: 32) && |
| 533 | FTy->getParamType(i: 1)->isPointerTy() && |
| 534 | FTy->getParamType(i: 2)->isPointerTy()) { |
| 535 | int (*PF)(int, char **, const char **) = |
| 536 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
| 537 | |
| 538 | // Call the function. |
| 539 | GenericValue rv; |
| 540 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 541 | (char **)GVTOP(GV: ArgValues[1]), |
| 542 | (const char **)GVTOP(GV: ArgValues[2]))); |
| 543 | return rv; |
| 544 | } |
| 545 | break; |
| 546 | case 2: |
| 547 | if (FTy->getParamType(i: 0)->isIntegerTy(BitWidth: 32) && |
| 548 | FTy->getParamType(i: 1)->isPointerTy()) { |
| 549 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
| 550 | |
| 551 | // Call the function. |
| 552 | GenericValue rv; |
| 553 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 554 | (char **)GVTOP(GV: ArgValues[1]))); |
| 555 | return rv; |
| 556 | } |
| 557 | break; |
| 558 | case 1: |
| 559 | if (FTy->getNumParams() == 1 && |
| 560 | FTy->getParamType(i: 0)->isIntegerTy(BitWidth: 32)) { |
| 561 | GenericValue rv; |
| 562 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
| 563 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
| 564 | return rv; |
| 565 | } |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // Handle cases where no arguments are passed first. |
| 571 | if (ArgValues.empty()) { |
| 572 | GenericValue rv; |
| 573 | switch (RetTy->getTypeID()) { |
| 574 | default: llvm_unreachable("Unknown return type for function call!" ); |
| 575 | case Type::IntegerTyID: { |
| 576 | unsigned BitWidth = cast<IntegerType>(Val: RetTy)->getBitWidth(); |
| 577 | if (BitWidth == 1) |
| 578 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
| 579 | else if (BitWidth <= 8) |
| 580 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
| 581 | else if (BitWidth <= 16) |
| 582 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
| 583 | else if (BitWidth <= 32) |
| 584 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
| 585 | else if (BitWidth <= 64) |
| 586 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
| 587 | else |
| 588 | llvm_unreachable("Integer types > 64 bits not supported" ); |
| 589 | return rv; |
| 590 | } |
| 591 | case Type::VoidTyID: |
| 592 | rv.IntVal = APInt(32, ((int (*)())(intptr_t)FPtr)(), true); |
| 593 | return rv; |
| 594 | case Type::FloatTyID: |
| 595 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
| 596 | return rv; |
| 597 | case Type::DoubleTyID: |
| 598 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
| 599 | return rv; |
| 600 | case Type::X86_FP80TyID: |
| 601 | case Type::FP128TyID: |
| 602 | case Type::PPC_FP128TyID: |
| 603 | llvm_unreachable("long double not supported yet" ); |
| 604 | case Type::PointerTyID: |
| 605 | return PTOGV(P: ((void*(*)())(intptr_t)FPtr)()); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | report_fatal_error(reason: "MCJIT::runFunction does not support full-featured " |
| 610 | "argument passing. Please use " |
| 611 | "ExecutionEngine::getFunctionAddress and cast the result " |
| 612 | "to the desired function pointer type." ); |
| 613 | } |
| 614 | |
| 615 | void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) { |
| 616 | if (!isSymbolSearchingDisabled()) { |
| 617 | if (auto Sym = Resolver.findSymbol(Name: std::string(Name))) { |
| 618 | if (auto AddrOrErr = Sym.getAddress()) |
| 619 | return reinterpret_cast<void*>( |
| 620 | static_cast<uintptr_t>(*AddrOrErr)); |
| 621 | } else if (auto Err = Sym.takeError()) |
| 622 | report_fatal_error(Err: std::move(Err)); |
| 623 | } |
| 624 | |
| 625 | /// If a LazyFunctionCreator is installed, use it to get/create the function. |
| 626 | if (LazyFunctionCreator) |
| 627 | if (void *RP = LazyFunctionCreator(std::string(Name))) |
| 628 | return RP; |
| 629 | |
| 630 | if (AbortOnFailure) { |
| 631 | report_fatal_error(reason: "Program used external function '" +Name+ |
| 632 | "' which could not be resolved!" ); |
| 633 | } |
| 634 | return nullptr; |
| 635 | } |
| 636 | |
| 637 | void MCJIT::RegisterJITEventListener(JITEventListener *L) { |
| 638 | if (!L) |
| 639 | return; |
| 640 | std::lock_guard<sys::Mutex> locked(lock); |
| 641 | EventListeners.push_back(x: L); |
| 642 | } |
| 643 | |
| 644 | void MCJIT::UnregisterJITEventListener(JITEventListener *L) { |
| 645 | if (!L) |
| 646 | return; |
| 647 | std::lock_guard<sys::Mutex> locked(lock); |
| 648 | auto I = find(Range: reverse(C&: EventListeners), Val: L); |
| 649 | if (I != EventListeners.rend()) { |
| 650 | std::swap(a&: *I, b&: EventListeners.back()); |
| 651 | EventListeners.pop_back(); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | void MCJIT::notifyObjectLoaded(const object::ObjectFile &Obj, |
| 656 | const RuntimeDyld::LoadedObjectInfo &L) { |
| 657 | uint64_t Key = |
| 658 | static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data())); |
| 659 | std::lock_guard<sys::Mutex> locked(lock); |
| 660 | MemMgr->notifyObjectLoaded(EE: this, Obj); |
| 661 | for (JITEventListener *EL : EventListeners) |
| 662 | EL->notifyObjectLoaded(K: Key, Obj, L); |
| 663 | } |
| 664 | |
| 665 | void MCJIT::notifyFreeingObject(const object::ObjectFile &Obj) { |
| 666 | uint64_t Key = |
| 667 | static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data())); |
| 668 | std::lock_guard<sys::Mutex> locked(lock); |
| 669 | for (JITEventListener *L : EventListeners) |
| 670 | L->notifyFreeingObject(K: Key); |
| 671 | } |
| 672 | |
| 673 | JITSymbol |
| 674 | LinkingSymbolResolver::findSymbol(const std::string &Name) { |
| 675 | auto Result = ParentEngine.findSymbol(Name, CheckFunctionsOnly: false); |
| 676 | if (Result) |
| 677 | return Result; |
| 678 | if (ParentEngine.isSymbolSearchingDisabled()) |
| 679 | return nullptr; |
| 680 | return ClientResolver->findSymbol(Name); |
| 681 | } |
| 682 | |
| 683 | void LinkingSymbolResolver::anchor() {} |
| 684 | |