Commit d8aa2e43 authored by jan.koester's avatar jan.koester
Browse files

speed

parent 081a0a11
Loading
Loading
Loading
Loading
+7 −3
Changes for build-chroot.sh: 7 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -531,7 +531,9 @@ fetch_git() {
            git -C "$dest" remote set-url origin "$url"
        [[ -n $ref ]] && git -C "$dest" rev-parse -q --verify "refs/tags/$ref" >/dev/null && return
        msg "updating $url${ref:+ ($ref)}"
        run fetch git -C "$dest" fetch --depth 1 origin ${ref:+"$ref"}
        # no ref: the remote's HEAD -- a single-branch clone would otherwise
        # keep fetching its old branch, which may no longer exist
        run fetch git -C "$dest" fetch --depth 1 origin "${ref:-HEAD}"
        run fetch git -C "$dest" -c advice.detachedHead=false checkout -q --detach FETCH_HEAD
        return
    fi
@@ -1411,7 +1413,7 @@ stage_sources() {
# which this chroot doesn't have (its rpm dependencies are in /usr).
PACKAGES="${PACKAGES:-zlib zstd popt lua file systempp libressl libelf rpm fresh makeplus libbpf bpftool ninja cmake python3 llvm-project
    ca-certificates ncurses libjson-c libbrotli libcmdplus libuuidp libnetplus libhtmlpp libconfplus libhttppp libparitypp
    libauthdb-client libmediadb-client libsmtppp libimappp mammut liblzma libxml2 libarchive glib2 curl libsolv librepo fmt toml11 libsmartcols dnf5 createrepo_c 
    libauthdb-client libmediadb-client libsmtppp libimappp mammut libdbpp liblzma libxml2 libarchive glib2 curl libsolv librepo fmt toml11 libsmartcols dnf5 createrepo_c 
    fenriros-release proxyplus openssh dosfstools e2fsprogs gptfdisk syslite sharedd adconnect storaged iscsid upsd firewalld tinyxml2 snmpdd ntpplus containerd webctl limine perl rsync
    nasm libwebp x264 x265 libaom ffmpeg
    fenriros-installer dwarves mrustc rust kernel systrace}"
@@ -1550,7 +1552,9 @@ step_repo() {
# ---------------------------------------------------------------------------
# fenstore: FenStore and libdbpp, which it needs to build, as RPMs outside
# RPMS -- repo, iso and docker take everything from there. libdbpp goes into
# the chroot (build dependency), FenStore itself doesn't. FENSTORE_PG=1 adds
# the chroot (build dependency), FenStore itself doesn't. libdbpp is in
# PACKAGES as well: libdbpp-backends (sqlite.so, mammut.so) has to be in the
# repo, or an installed fenstore fails with "Failed to dlopen". FENSTORE_PG=1 adds
# PostgreSQL first, for libdbpp's pgsql backend.
step_fenstore() {
    local pkg log dir=/build/extra-rpms
+6 −0
Changes for include/dlfcn.h: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -35,6 +35,12 @@
#define RTLD_NOLOAD   0x00004
#define RTLD_NODELETE 0x01000

/* dlsym() pseudo-handles (glibc's values): the global search order, and
 * the objects after the caller's in it (wrapping a function another
 * object defines, e.g. from an LD_PRELOAD library). */
#define RTLD_DEFAULT ((void *) 0)
#define RTLD_NEXT    ((void *) -1l)

/* Real gap found compiling LLVM: was missing entirely (real gap:
 * llvm/lib/Support/Unix/Signals.inc's real PrintStackTrace() fallback
 * path, used when llvm-symbolizer isn't available, calls dladdr() over
+8 −1
Changes for src/linux/dlfcn.cpp: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -47,8 +47,15 @@ extern "C" {

#ifdef SYSTEMPP_HAVE_LDSO

    void* dlsym(void* handle, const char* name) {
    __attribute__((noinline)) void* dlsym(void* handle, const char* name) {
        sys::ld::dso* owner = nullptr;
        if (handle == RTLD_NEXT) {
            sys::ld::dso* caller = sys::ld::dso_containing(__builtin_return_address(0));
            ElfW(Sym)* s = caller ? sys::ld::find_sym_after(caller, name, &owner) : nullptr;
            if (!s) { error = (char*)"dlsym: symbol not found"; return nullptr; }
            error = nullptr;
            return owner->base + s->st_value;
        }
        ElfW(Sym)* s = handle
            ? sys::ld::find_in_dso((sys::ld::dso*)handle, name, sys::ld::sysv_hash(name))
            : sys::ld::global_find_sym(name, &owner);
+116 −11
Changes for src/linux/ld/dynlink.cpp: 116 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -163,26 +163,71 @@ ElfW(Sym)* find_in_dso(dso* d, const char* name, uint32_t hash) {
 * once main copy-relocates such a symbol, libc.so's OWN GLOB_DAT slots
 * referencing it must resolve to main's copy too, not libc.so's own
 * storage, or the two would silently desync. */
ElfW(Sym)* global_find_sym(const char* name, dso** owner_out) {
    uint32_t h = sysv_hash(name);
    if (g_main) {
        ElfW(Sym)* s = find_in_dso(g_main, name, h);
        if (s) { if (owner_out) *owner_out = g_main; return s; }
/* LD_PRELOAD objects: searched right after main and before everything
 * else, libc.so included -- that is what lets them interpose libc
 * functions (glibc's order). */
static dso* g_preload[16];
static size_t g_npreload = 0;

static bool is_preload(const dso* d) {
    for (size_t i = 0; i < g_npreload; i++)
        if (g_preload[i] == d) return true;
    return false;
}

/* The global search order: main, the LD_PRELOAD objects, then the load
 * list (self, needed objects, dlopen()ed ones). Calls f(d) until it
 * returns true; returns the object it stopped at. */
template <class F> static dso* for_each_in_search_order(F f) {
    if (g_main && f(g_main)) return g_main;
    for (size_t i = 0; i < g_npreload; i++)
        if (f(g_preload[i])) return g_preload[i];
    for (dso* d = g_head; d; d = d->next) {
        if (d == g_main) continue;
        ElfW(Sym)* s = find_in_dso(d, name, h);
        if (s) { if (owner_out) *owner_out = d; return s; }
        if (d == g_main || is_preload(d)) continue;
        if (f(d)) return d;
    }
    return nullptr;
}

ElfW(Sym)* global_find_sym(const char* name, dso** owner_out) {
    uint32_t h = sysv_hash(name);
    ElfW(Sym)* found = nullptr;
    dso* owner = for_each_in_search_order([&](dso* d) { return (found = find_in_dso(d, name, h)) != nullptr; });
    if (found && owner_out) *owner_out = owner;
    return found;
}

/* Symbol lookup for R_X86_64_COPY only: it must find the ORIGINAL
 * definition to copy from (typically in libc.so), never `skip` (the
 * object currently being relocated) -- `skip` only has a placeholder
 * definition (the BSS slot the copy is being written into), not real
 * data, so global_find_sym()'s normal main-first preference would just
 * find that placeholder and copy garbage into itself. */
ElfW(Sym)* find_sym_after(dso* after, const char* name, dso** owner_out) {
    uint32_t h = sysv_hash(name);
    bool seen = false;
    ElfW(Sym)* found = nullptr;
    dso* owner = for_each_in_search_order([&](dso* d) {
        if (!seen) { seen = (d == after); return false; }
        return (found = find_in_dso(d, name, h)) != nullptr;
    });
    if (found && owner_out) *owner_out = owner;
    return found;
}

dso* dso_containing(const void* addr) {
    const unsigned char* a = (const unsigned char*)addr;
    for (dso* d = g_head; d; d = d->next) {
        for (size_t i = 0; i < d->phnum; i++) {
            const ElfW(Phdr)* ph = &d->phdrs[i];
            if (ph->p_type != PT_LOAD) continue;
            const unsigned char* lo = d->base + ph->p_vaddr;
            if (a >= lo && a < lo + ph->p_memsz) return d;
        }
    }
    return nullptr;
}

static ElfW(Sym)* find_copy_source(const char* name, dso* skip, dso** owner_out) {
    uint32_t h = sysv_hash(name);
    for (dso* d = g_head; d; d = d->next) {
@@ -742,6 +787,10 @@ bool relocate(dso* d) {

/* ---- init/fini ---- */

/* Objects in initialization order, newest first (dso::init_prev): exit()
 * finalizes them in exactly the reverse of the order they were set up. */
static dso* g_init_top = nullptr;

void run_init(dso* d) {
    if (!d || d->inited) return;
    d->inited = true;
@@ -751,16 +800,35 @@ void run_init(dso* d) {
    if (d->init_array)
        for (size_t i = 0; i < d->init_arraysz; i++)
            if (d->init_array[i]) d->init_array[i]();
    d->init_prev = g_init_top;
    g_init_top = d;
}

void run_fini(dso* d) {
    if (!d) return;
    if (!d || d->finid) return;
    d->finid = true;
    if (d->fini_array)
        for (size_t i = d->fini_arraysz; i-- > 0;)
            if (d->fini_array[i]) d->fini_array[i]();
    if (d->fini_func) d->fini_func();
}

void run_fini_all() {
    /* Before this, exit() already ran every __cxa_atexit handler (C++
     * static destructors) process-wide. What was left were the objects'
     * own .fini_array/DT_FINI entries -- __attribute__((destructor)) and
     * the like -- which only dlclose() ever ran. */
    while (dso* d = g_init_top) {
        g_init_top = d->init_prev;
        run_fini(d);
    }
}

static void forget_init(dso* d) {
    for (dso** p = &g_init_top; *p; p = &(*p)->init_prev)
        if (*p == d) { *p = d->init_prev; return; }
}

void unload_object(dso* d) {
    /* self and main are static storage, never heap-allocated (see
     * init_main_dso()/__dls2 -- self must be relocated before operator new
@@ -775,6 +843,7 @@ void unload_object(dso* d) {
    if (d->map_start)
        __systempp_finalize_range(d->map_start, (const char*)d->map_start + d->map_len);
    run_fini(d);
    forget_init(d);
    for (size_t i = 0; i < d->depcount; i++)
        unload_object(d->deps[i]);

@@ -864,9 +933,13 @@ extern "C" __attribute__((visibility("hidden"))) _Noreturn void __dls2(unsigned

    __init_auxv(envp);

    const char* ld_preload = nullptr;
    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; }
        /* both ignored for setuid/setgid programs, like glibc */
        for (char** e = envp; *e; e++) {
            if (strncmp(*e, "LD_LIBRARY_PATH=", 16) == 0) g_ld_library_path = *e + 16;
            else if (strncmp(*e, "LD_PRELOAD=", 11) == 0) ld_preload = *e + 11;
        }
    }
    if (const char* exe = (const char*)getauxval(AT_EXECFN))
        if (!split_dir(exe, g_main_origin, sizeof(g_main_origin)))
@@ -879,6 +952,35 @@ extern "C" __attribute__((visibility("hidden"))) _Noreturn void __dls2(unsigned

    needed_list main_needed;
    decode_dynamic(main_, &main_needed);

    /* LD_PRELOAD: names separated by spaces or colons, loaded before
     * main's own dependencies; one that can't be loaded is reported and
     * skipped (glibc does the same). */
    if (ld_preload) {
        const char* p = ld_preload;
        while (*p && g_npreload < sizeof(g_preload) / sizeof(g_preload[0])) {
            while (*p == ' ' || *p == ':') p++;
            const char* start = p;
            while (*p && *p != ' ' && *p != ':') p++;
            size_t len = (size_t)(p - start);
            if (!len) continue;
            char name[512];
            if (len >= sizeof(name)) continue;
            memcpy(name, start, len);
            name[len] = 0;
            g_load_error_name = nullptr;
            dso* pre = load_object(name, main_);
            if (!pre) {
                auto put = [](const char* m) { syscall3(__NR_write, 2, (unsigned long)m, strlen(m)); };
                put("systempp ld: object '");
                put(name);
                put("' from LD_PRELOAD cannot be preloaded: ignored\n");
                continue;
            }
            if (!is_preload(pre)) g_preload[g_npreload++] = pre;
        }
    }

    for (size_t i = 0; i < main_needed.count; i++) {
        g_load_error_name = nullptr;
        dso* dep = load_object(main_needed.items[i], main_);
@@ -918,6 +1020,9 @@ extern "C" __attribute__((visibility("hidden"))) _Noreturn void __dls2(unsigned

    install_main_thread_tls(nullptr);

    /* glibc order: dependencies (incl. the preloaded objects') first,
     * main last */
    for (size_t i = 0; i < g_npreload; i++) run_init(g_preload[i]);
    run_init(main_);

    void* entry = (void*)getauxval(AT_ENTRY);
+15 −2
Changes for src/linux/ld/dynlink.h: 15 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -66,6 +66,8 @@ struct dso {

    unsigned refcount;
    bool inited;
    bool finid;          /* DT_FINI_ARRAY/DT_FINI ran (run_fini is idempotent) */
    dso* init_prev;      /* the object initialized before this one (see run_init) */

    dso* next;
};
@@ -96,10 +98,14 @@ bool relocate(dso* d);
 * dependencies, exactly once (idempotent via d->inited). */
void run_init(dso* d);

/* Runs DT_FINI_ARRAY/DT_FINI for d only (no dependency recursion -- matches
 * v1's dlclose()-only use of fini, see plan). */
/* Runs DT_FINI_ARRAY/DT_FINI for d only (no dependency recursion), at most
 * once. */
void run_fini(dso* d);

/* exit(): DT_FINI_ARRAY/DT_FINI of every object still loaded, in reverse
 * initialization order (glibc's _dl_fini order). */
void run_fini_all();

/* Drops a reference; once it hits zero, runs fini and unmaps. */
void unload_object(dso* d);

@@ -110,6 +116,13 @@ ElfW(Sym)* find_in_dso(dso* d, const char* name, uint32_t hash);
 * load order), returning the first defined match. */
ElfW(Sym)* global_find_sym(const char* name, dso** owner_out);

/* RTLD_NEXT: the first definition in an object after `after` in the global
 * search order (main, LD_PRELOAD objects, then load order). */
ElfW(Sym)* find_sym_after(dso* after, const char* name, dso** owner_out);

/* The loaded object whose mapping contains addr (main included), or nullptr. */
dso* dso_containing(const void* addr);

dso* list_head();

} }
Loading