From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4BF5F3858422; Fri, 5 May 2023 19:59:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4BF5F3858422 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683316761; bh=T7C7SSEivagdcBC6kgOY8ZE7h+42QyhCuHjhvQ78NXA=; h=From:To:Subject:Date:From; b=lUHVxzW7M4Ccjy3PL8H3gXiN+C9rTUfJaHe6gWAbtYby81FijI2hN1wK/Vwcv/xBG z3ZD+g8rGDEXpMdLAVnao8yRbwzkJv9T/wduUhoKVps+CxVYvaqaYdi1y3G684FsDI r077Z11XoC7ka1FRrUQ62OEEg3R590ANLSnqymV4= From: "fweimer at redhat dot com" To: glibc-bugs@sourceware.org Subject: [Bug dynamic-link/30424] New: Calling dlopen from preinit function inhibits proper libc initialization Date: Fri, 05 May 2023 19:59:20 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: glibc X-Bugzilla-Component: dynamic-link X-Bugzilla-Version: unspecified X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: fweimer at redhat dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://sourceware.org/bugzilla/show_bug.cgi?id=3D30424 Bug ID: 30424 Summary: Calling dlopen from preinit function inhibits proper libc initialization Product: glibc Version: unspecified Status: NEW Severity: normal Priority: P2 Component: dynamic-link Assignee: unassigned at sourceware dot org Reporter: fweimer at redhat dot com Target Milestone: --- Reproducer: #define _GNU_SOURCE #include #include #include #include #include static void preinit (int argc, char **argv, char **envp) { printf ("environ: %p\n", environ); printf ("USER: %s\n", getenv ("USER")); if (argc !=3D 1) dlopen (LIBC_SO, RTLD_NOW); } static void (*const preinit_array []) (int, char **, char **) __attribute__ ((section (".preinit_array"), aligned (sizeof (void *)), used)) =3D { &preinit, }; int main (void) { printf ("environ: %p\n", environ); printf ("USER: %s\n", getenv ("USER")); } Running it without any arguments: environ: (nil) USER: (null) environ: 0x7ffe9b2dcc88 USER: fweimer This is sort-of expected because at preinit time, libc is not fully initial= ized yet because the ELF constructors have not run. Running with an argument triggers the dlopen: environ: (nil) USER: (null) environ: (nil) USER: (null) This is unexpected. The reason is that _dl_open (in ld.so) does not use the original data from the kernel, but receives the values to use from dlopen (= in libc.so). But as libc.so is not fully initialized at preinit time, it cannot initialize itself. The fact that ELF constructors have run is recorded, so = the full initialization is never performed. I think we should keep passing down at least environ from libc.so to ld.so because environ can change legitimately. Nowadays, we have __libc_early_ini= t, we can use it to get rid of ELF constructors and initialize libc.so fully before application code runs. This should resolve the issue because __libc_early_init runs before the preinit functions. --=20 You are receiving this mail because: You are on the CC list for the bug.=