Commit 65ffa1f2 authored by jan.koester's avatar jan.koester
Browse files

ld.so: LD_LIBRARY_PATH, $ORIGIN, missing libraries are an error, dladdr path

The first Rust program linked against Rust dylibs (rustc built by rustc,
stage 3 of the rust package: rustc_binary NEEDs librustc_driver.so and
libstd.so) found neither: the loader had no LD_LIBRARY_PATH at all, and it
opened RUNPATH "$ORIGIN/../lib" literally. A NEEDED library it couldn't
find was skipped silently, so the process died later with "undefined
symbol" on the first function from it.

- LD_LIBRARY_PATH (ignored under AT_SECURE), searched before the
  referrer's RUNPATH/RPATH, as glibc does with DT_RUNPATH
- $ORIGIN / ${ORIGIN} in both: the referrer's directory, the main
  program's from AT_EXECFN
- a NEEDED library of the program that can't be found: "error while
  loading shared libraries: X: cannot open shared object file", exit 127
- dladdr()'s dli_fname is the path the object was loaded from (glibc's
  l_name), not its DT_NEEDED name: rustc locates its sysroot from
  librustc_driver.so's path

Checked with the stage-3 rustc in the RC5 chroot: -vV and --print sysroot
work. ctest 172/172.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent b91bc69e
Loading
Loading
Loading
Loading
+4 −1
Changes for src/linux/dlfcn.cpp: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -194,7 +194,10 @@ extern "C" {
            const unsigned char* end = d->base + hi;
            if (a < start || a >= end) continue;

            info->dli_fname = (d->name && *d->name) ? d->name : __main_program_path();
            /* the path it was loaded from, as glibc's l_name: rustc finds
             * its sysroot from librustc_driver.so's dli_fname */
            info->dli_fname = d->path ? d->path
                            : (d->name && *d->name) ? d->name : __main_program_path();
            info->dli_fbase = d->base;

            if (d->hashtab && d->syms && d->strings) {
+77 −15
Changes for src/linux/ld/dynlink.cpp: 77 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -97,6 +97,12 @@ static dso* g_head = nullptr;
static dso* g_tail = nullptr;
static dso* g_self = nullptr;
static dso* g_main = nullptr;
/* LD_LIBRARY_PATH (null under AT_SECURE) and the main program's directory
 * ($ORIGIN for its RUNPATH), both set in __dls2() */
static const char* g_ld_library_path = nullptr;
static char g_main_origin[PATH_MAX];
/* the library load_object() last failed to find (for the error message) */
static const char* g_load_error_name = nullptr;

static void register_dso(dso* d) {
    d->next = nullptr;
@@ -316,6 +322,46 @@ static int try_open_in_dir(const char* dir, size_t dirlen, const char* name, cha
    return raw_open(full_out, O_RDONLY);
}

/* Each directory of a ':'-separated list, $ORIGIN / ${ORIGIN} replaced by
 * the referrer's directory (empty entries are skipped). */
static int try_open_in_list(const char* list, dso* referrer, const char* name, char* full_out, size_t full_out_sz) {
    const char* origin = nullptr;
    if (referrer == g_main && g_main_origin[0]) origin = g_main_origin;
    else if (referrer && referrer->dir) origin = referrer->dir;
    const char* p = list;
    while (*p) {
        const char* colon = strchr(p, ':');
        size_t len = colon ? (size_t)(colon - p) : strlen(p);
        char dirbuf[PATH_MAX];
        size_t n = 0;
        bool ok = len > 0;
        for (size_t i = 0; ok && i < len; ) {
            size_t skip = 0;
            if (p[i] == '$' && len - i >= 7 && memcmp(p + i, "$ORIGIN", 7) == 0) skip = 7;
            else if (p[i] == '$' && len - i >= 9 && memcmp(p + i, "${ORIGIN}", 9) == 0) skip = 9;
            if (skip) {
                if (!origin) { ok = false; break; }
                size_t ol = strlen(origin);
                if (n + ol >= sizeof(dirbuf)) { ok = false; break; }
                memcpy(dirbuf + n, origin, ol);
                n += ol;
                i += skip;
            } else {
                if (n + 1 >= sizeof(dirbuf)) { ok = false; break; }
                dirbuf[n++] = p[i++];
            }
        }
        if (ok && n > 0) {
            dirbuf[n] = 0;
            int fd = try_open_in_dir(dirbuf, n, name, full_out, full_out_sz);
            if (fd >= 0) return fd;
        }
        p += len;
        if (*p == ':') p++;
    }
    return -1;
}

static int resolve_and_open(const char* name, dso* referrer, char* full_out, size_t full_out_sz) {
    if (strchr(name, '/')) {
        int fd = raw_open(name, O_RDONLY);
@@ -333,21 +379,15 @@ static int resolve_and_open(const char* name, dso* referrer, char* full_out, siz
        if (fd >= 0) return fd;
    }

    if (referrer && referrer->rpath) {
        const char* p = referrer->rpath;
        while (*p) {
            const char* colon = strchr(p, ':');
            size_t len = colon ? (size_t)(colon - p) : strlen(p);
            if (len > 0 && len < 256) {
                char dirbuf[256];
                memcpy(dirbuf, p, len);
                dirbuf[len] = 0;
                int fd = try_open_in_dir(dirbuf, len, name, full_out, full_out_sz);
    /* LD_LIBRARY_PATH, then the referrer's RUNPATH (glibc's order with
     * DT_RUNPATH; DT_RPATH is treated the same) */
    if (g_ld_library_path) {
        int fd = try_open_in_list(g_ld_library_path, referrer, name, full_out, full_out_sz);
        if (fd >= 0) return fd;
    }
            p += len;
            if (*p == ':') p++;
        }
    if (referrer && referrer->rpath) {
        int fd = try_open_in_list(referrer->rpath, referrer, name, full_out, full_out_sz);
        if (fd >= 0) return fd;
    }

    /* musl's default order: locally installed libraries (/usr/local/lib)
@@ -455,7 +495,7 @@ dso* load_object(const char* name, dso* referrer) {

    char path[PATH_MAX];
    int fd = resolve_and_open(name, referrer, path, sizeof(path));
    if (fd < 0) return nullptr;
    if (fd < 0) { g_load_error_name = name; return nullptr; }

    ElfW(Ehdr) eh;
    if (__sysc(__NR_pread64, fd, (long)&eh, sizeof(eh), __SYSCALL_LL_PRW(0)) != (long)sizeof(eh)) {
@@ -494,6 +534,7 @@ dso* load_object(const char* name, dso* referrer) {
    d->map_start = map_start;
    d->map_len = map_len;
    d->name = dup_str(name);
    d->path = dup_str(path);

    char dirbuf[PATH_MAX];
    if (split_dir(path, dirbuf, sizeof(dirbuf)))
@@ -505,6 +546,7 @@ dso* load_object(const char* name, dso* referrer) {
        delete[] phdrs_heap;
        delete[] d->name;
        if (d->dir) delete[] d->dir;
        if (d->path) delete[] d->path;
        delete d;
        return nullptr;
    }
@@ -752,6 +794,7 @@ void unload_object(dso* d) {
    delete[] d->phdrs;
    if (d->name) delete[] d->name;
    if (d->dir) delete[] d->dir;
    if (d->path) delete[] d->path;
    delete d;
}

@@ -821,6 +864,14 @@ extern "C" __attribute__((visibility("hidden"))) _Noreturn void __dls2(unsigned

    __init_auxv(envp);

    if (!getauxval(AT_SECURE)) {
        for (char** e = envp; *e; e++)
            if (strncmp(*e, "LD_LIBRARY_PATH=", 16) == 0) { g_ld_library_path = *e + 16; break; }
    }
    if (const char* exe = (const char*)getauxval(AT_EXECFN))
        if (!split_dir(exe, g_main_origin, sizeof(g_main_origin)))
            strcpy(g_main_origin, ".");

    dso* main_ = &g_main_storage;
    init_main_dso(main_);
    g_main = main_;
@@ -829,8 +880,19 @@ extern "C" __attribute__((visibility("hidden"))) _Noreturn void __dls2(unsigned
    needed_list main_needed;
    decode_dynamic(main_, &main_needed);
    for (size_t i = 0; i < main_needed.count; i++) {
        g_load_error_name = nullptr;
        dso* dep = load_object(main_needed.items[i], main_);
        if (dep && main_->depcount < 16)
        if (!dep) {
            /* like glibc: name it and stop, instead of failing later on
             * the first symbol from it */
            const char* what = g_load_error_name ? g_load_error_name : main_needed.items[i];
            auto put = [](const char* m) { syscall3(__NR_write, 2, (unsigned long)m, strlen(m)); };
            put("systempp ld: error while loading shared libraries: ");
            put(what);
            put(": cannot open shared object file\n");
            syscall1(__NR_exit_group, 127);
        }
        if (main_->depcount < 16)
            main_->deps[main_->depcount++] = dep;
    }

+1 −0
Changes for src/linux/ld/dynlink.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ namespace sys { namespace ld {
struct dso {
    char* name;               /* heap-allocated (operator new[]) */
    char* dir;                /* directory it was loaded from, or nullptr */
    char* path;               /* the file it was loaded from (dladdr's dli_fname), or nullptr */
    const char* rpath;        /* DT_RPATH/DT_RUNPATH string (v1: same handling for both), or nullptr */
    unsigned char* base;      /* load bias */
    void* map_start;          /* first mapped page, for munmap on unload */