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

Code before:
    *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();
        symbol_found = *value != NULL || err == NULL;
    }
#endif

    int rtld_first_handle = 0;
#ifdef _OS_DARWIN_
    rtld_first_handle = (uintptr_t)handle & 1;
#endif

#ifndef _OS_WINDOWS_
    /*
     * Unlike GetProcAddress, dlsym will search the dependencies of the given
     * library, so we must check where the symbol came from.
     */
    if (symbol_found && !search_deps && handle != jl_RTLD_DEFAULT_handle &&
        !rtld_first_handle) {
        void *symbol_handle = jl_find_dynamic_library_by_addr(*value, 0, 1);
        symbol_found = handle == symbol_handle;
    }
#endif

    if (!symbol_found) {
        if (throw_err) {
#ifdef _OS_WINDOWS_
            char err[256];
            win32_formatmessage(GetLastError(), err, sizeof(err));
#endif
            jl_errorf("could not load symbol \"%s\":\n%s", symbol, err);
        }
        return 0;
    }

    return 1;

Review comment (suggestion):
I'd recommend just tagging the pointer with `| 1` before `dlsym` instead of using jl_find_dynamic_library_by_addr, since handle comparison is potentially annoying (you have to mask with &-4 to remove mode bits)

https://github.com/apple-oss-distributions/dyld/blob/9307719dd8dc9b385daa412b03cfceb897b2b398/dyld/DyldAPIs.cpp#L149-L150
https://github.com/apple-opensource/dyld/blob/e3f88907bebb8421f50f0943595f6874de70ebe0/dyld3/APIs.cpp#L889-L900

Code after:
    /* When !search_deps and the handle isn't special, force RTLD_FIRST. */
    if (!search_deps && handle != RTLD_NEXT && handle != RTLD_DEFAULT &&
        handle != RTLD_SELF && handle != RTLD_MAIN_ONLY)
        handle = (void *)((uintptr_t)handle | 1);
    *value = dlsym(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();
        symbol_found = *value != NULL || err == NULL;
    }
#endif

#if !defined(_OS_DARWIN_) && !defined(_OS_WINDOWS_)
    /*
     * Unlike GetProcAddress, dlsym will search the dependencies of the given
     * library, so we must check where the symbol came from.
     */
    if (symbol_found && !search_deps && handle != jl_RTLD_DEFAULT_handle) {
        void *symbol_handle = jl_find_dynamic_library_by_addr(*value, 0, 1);
        symbol_found = handle == symbol_handle;
    }
#endif

    if (!symbol_found) {
        if (throw_err) {
#ifdef _OS_WINDOWS_
            char err[256];
            win32_formatmessage(GetLastError(), err, sizeof(err));
#endif
            jl_errorf("could not load symbol \"%s\":\n%s", symbol, err);
        }
        return 0;
    }

    return 1;