void MachOObject::load() { if (isLoaded()) throw std::logic_error("Module already loaded"); if (strcmp(m_file->platform(), ARCH_NAME) != 0) { std::stringstream ss; ss << "This version of Darling dyld cannot load binaries for " << m_file->platform() << "."; throw std::runtime_error(ss.str()); } loadSegments(); transitionState(dyld_image_state_mapped); if (m_slide > 0) rebase(); transitionState(dyld_image_state_rebased); readSymbols(); readExports(); MachOMgr::instance()->add(this, isMainModule()); loadDependencies(); performRelocations(); performBinds(); transitionState(dyld_image_state_bound); transitionState(dyld_image_state_dependents_initialized); // registerEHSection(); if (isMainModule()) fillInProgramVars(); fillInDyldData(); setInitialSegmentProtection(); setupTLS(); MachOMgr::instance()->notifyAdd(this); runInitializers(); transitionState(dyld_image_state_initialized); m_file->closeFd(); if (MachOMgr::instance()->printLibraries()) std::cerr << "dyld: Loaded " << this->path() << std::endl; }
void NativeObject::unload() { MachOMgr::instance()->remove(this); if (!MachOMgr::instance()->isDestroying()) { if (m_path.find('/') != std::string::npos) ::dlclose(m_nativeRef); } transitionState(dyld_image_state_terminated); }
void MachOObject::unload() { if (!isLoaded()) throw std::logic_error("Module not loaded"); MachOMgr::instance()->remove(this); teardownTLS(); runFinalizers(); if (!MachOMgr::instance()->isDestroying()) { unloadSegments(); // unregisterEHSection(); } for (LoadableObject* dep : m_dependencies) dep->delRef(); m_dependencies.clear(); transitionState(dyld_image_state_terminated); }
void NativeObject::load() { int flags = 0; struct link_map* lm; flags |= globalExports() ? RTLD_GLOBAL : RTLD_LOCAL; //flags |= bindAllAtLoad() ? RTLD_NOW : RTLD_LAZY; flags |= RTLD_LAZY; if (MachOMgr::instance()->printLibraries()) std::cerr << "dyld: Loading " << m_path << std::endl; m_nativeRef = ::dlopen(m_path.c_str(), flags); if (!m_nativeRef) { const char* err = ::dlerror(); std::stringstream ss; ss << "Failed to load native library: " << err; throw std::runtime_error(ss.str()); } else { updateName(); if (MachOMgr::instance()->printLibraries()) std::cerr << "dyld: Loaded " << m_path << std::endl; MachOMgr::instance()->add(this); } if (::dlinfo(m_nativeRef, RTLD_DI_LINKMAP, &lm) == 0) m_baseAddress = (void*) lm->l_addr; transitionState(dyld_image_state_mapped); transitionState(dyld_image_state_dependents_mapped); transitionState(dyld_image_state_rebased); transitionState(dyld_image_state_bound); transitionState(dyld_image_state_dependents_initialized); transitionState(dyld_image_state_initialized); }