Repository: JuliaLang/julia
File: src/dlload.c (C)

Code before:
        return -1;
    }
    return dlclose(handle);
#endif
}

void *jl_find_dynamic_library_by_addr(void *symbol, int throw_err, int close) JL_NOTSAFEPOINT
{
    void *handle;
#ifdef _OS_WINDOWS_
    if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                            (LPCWSTR)symbol,
                            (HMODULE*)&handle)) {
        if (throw_err)
            jl_error("could not load base module");
        return NULL;
    }
#else
    Dl_info info;
    if (!dladdr(symbol, &info) || !info.dli_fname) {
        if (throw_err)
            jl_error("could not load base module");
        return NULL;
    }
    handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
#if defined(_OS_FREEBSD_)
    // FreeBSD will not give you a handle for the executable if you dlopen() it
    // with RTLD_NOLOAD, so check jl_exe_handle.
    if (handle == NULL && dlerror() == NULL) {
        handle = jl_exe_handle;
    }
#elif !defined(__APPLE__)
    if (handle == RTLD_DEFAULT && (RTLD_DEFAULT != NULL || dlerror() == NULL)) {
        // We loaded the executable but got RTLD_DEFAULT back, ask for a real handle instead
        handle = dlopen("", RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
    }
#endif
    if (handle != NULL && close)
        dlclose(handle); // Undo ref count increment from `dlopen`
#endif
    return handle;
}

JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags, int throw_err)
{
    ios_t path, relocated;
    int i;
#ifdef _OS_WINDOWS_
    int err;
#endif
    uv_stat_t stbuf;

Review comment (suggestion):
```suggestion
    dlerror();
    handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
```

Code after:
        return -1;
    }
    return dlclose(handle);
#endif
}

void *jl_find_dynamic_library_by_addr(void *symbol, int throw_err, int close) JL_NOTSAFEPOINT
{
    void *handle;
#ifdef _OS_WINDOWS_
    if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                            (LPCWSTR)symbol,
                            (HMODULE*)&handle)) {
        if (throw_err)
            jl_error("could not load base module");
        return NULL;
    }
#else
    Dl_info info;
    if (!dladdr(symbol, &info) || !info.dli_fname) {
        if (throw_err)
            jl_error("could not load base module");
        return NULL;
    }
    dlerror();
    handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
#if defined(_OS_FREEBSD_)
    // FreeBSD will not give you a handle for the executable if you dlopen() it
    // with RTLD_NOLOAD, so check jl_exe_handle.
    if (handle == NULL && dlerror() == NULL) {
        handle = jl_exe_handle;
    }
#elif !defined(__APPLE__)
    if (handle == RTLD_DEFAULT && (RTLD_DEFAULT != NULL || dlerror() == NULL)) {
        // We loaded the executable but got RTLD_DEFAULT back, ask for a real handle instead
        handle = dlopen("", RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
    }
#endif
    if (handle != NULL && close)
        dlclose(handle); // Undo ref count increment from `dlopen`
#endif
    return handle;
}

JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags, int throw_err)
{
    ios_t path, relocated;
    int i;
#ifdef _OS_WINDOWS_
    int err;
#endif