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

Code before:
#ifdef _OS_WINDOWS_
        err = GetLastError();
        break; // LoadLibrary already tested the rest
#else
        // bail out and show the error if file actually exists
        if (jl_stat(path, (char*)&stbuf) == 0)
            break;
#endif
    }

notfound:
    if (throw_err) {
#ifdef _OS_WINDOWS_
        char reason[256];
        win32_formatmessage(err, reason, sizeof(reason));
#else
        const char *reason = dlerror();
#endif
        jl_errorf("could not load library \"%s\"\n%s", modname, reason);
    }
    handle = NULL;

    return handle;
}

JL_DLLEXPORT int jl_dlsym(void *handle, const char *symbol, void ** value, int throw_err, int no_deps) JL_NOTSAFEPOINT
{
    int symbol_found = 0;

    /* First, get the symbol value */
#ifdef _OS_WINDOWS_
    *value = GetProcAddress((HMODULE) handle, symbol);
#else
    *value = dlsym(handle, symbol);
#endif

    /* Next, check for errors. On Windows, a NULL pointer means the symbol was
     * not found. On everything else, we can have NULL symbols, so we check for
     * non-NULL returns from dlerror(). Since POSIX doesn't require `dlerror`
     * to be implemented safely, FreeBSD doesn't (unlike everyone else, who
     * realized decades ago that threads are here to stay), so we avoid calling
     * `dlerror` unless we need to get the error message.
     * https://github.com/freebsd/freebsd-src/blob/12db51d20823a5e3b9e5f8a2ea73156fe1cbfc28/libexec/rtld-elf/rtld.c#L198
     */
    symbol_found = *value != NULL;
#ifndef _OS_WINDOWS_
    const char *err = "";
    if (!symbol_found) {
        dlerror(); /* Reset error status. */
        *value = dlsym(handle, symbol);
        err = dlerror();

Review comment (suggestion):
It could be nice from a readability perspective to not have the argument represent a negation, as the double negative in `!no_deps` is (subjectively) less intuitive. That would also make it consistent with the `throw_err` argument, which isn't e.g. `dont_throw_err`. Anyway, just my drive-by $0.02, now back into the shadows I go...

Code after:
        ios_putc(0, &path);
        handle = jl_dlopen(path.buf, flags);
        if (handle && !(flags & JL_RTLD_NOLOAD))
            jl_timing_puts(JL_TIMING_DEFAULT_BLOCK, jl_pathname_for_handle(handle));
        if (handle)
            goto success;
#ifdef _OS_WINDOWS_
        err = GetLastError();
        break; // LoadLibrary already tested the rest
#else
        // bail out and show the error if file actually exists
        if (jl_stat(path.buf, (char*)&stbuf) == 0)
            break;
#endif
    }

notfound:
    if (throw_err) {
#ifdef _OS_WINDOWS_
        char reason[256];
        win32_formatmessage(err, reason, sizeof(reason));
#else
        const char *reason = dlerror();
#endif
        ios_close(&relocated);
        ios_close(&path);
        jl_errorf("could not load library \"%s\"\n%s", modname, reason);
    }
    handle = NULL;

success:
    ios_close(&relocated);
    ios_close(&path);
    return handle;
}

/*
 * When search_deps is 1, act like dlsym and search both the library for the
 * handle and all its dependencies.  Use this option only when compatibility
 * with dlsym(3) is required, thought this behaviour is not possible on Windows.
 *
 * At time of writing, only Base.dlsym() uses search_deps = 1.
 */
JL_DLLEXPORT int jl_dlsym(void *handle, const char *symbol, void ** value, int throw_err, int search_deps) JL_NOTSAFEPOINT
{
    int symbol_found = 0;

    /* First, get the symbol value */
#if defined(_OS_WINDOWS_)
    *value = GetProcAddress((HMODULE) handle, symbol);
#elif defined(_OS_DARWIN_)