The branch main has been updated by kib:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=21a52f99440c9bec7679f3b0c5c9d888901c3694

commit 21a52f99440c9bec7679f3b0c5c9d888901c3694
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2023-08-09 05:07:05 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2023-08-09 05:07:05 +0000

    libc dlfcn.c: make dl_iterate_phdr() from libc more useful
    
    Apparently there are applications that resolve dl_iterate_phdr from libc
    and try to call the symbol. Our libc only provides stubs for dl* to
    satisfy static linker or statically linked binaries, and is not prepared
    to this situation.
    
    Add a code to dso libc to find real dl_iterate_phdr and redirect the
    call to it.
    
    Reported by:    yuri
    PR:     272992
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
---
 lib/libc/gen/dlfcn.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/lib/libc/gen/dlfcn.c b/lib/libc/gen/dlfcn.c
index 61984e2fe86c..f1ee86ec1934 100644
--- a/lib/libc/gen/dlfcn.c
+++ b/lib/libc/gen/dlfcn.c
@@ -169,7 +169,9 @@ _rtld_thread_init(void *li __unused)
 #ifndef IN_LIBDL
 static pthread_once_t dl_phdr_info_once = PTHREAD_ONCE_INIT;
 static struct dl_phdr_info phdr_info;
+#ifndef PIC
 static mutex_t dl_phdr_info_lock = MUTEX_INITIALIZER;
+#endif
 
 static void
 dl_init_phdr_info(void)
@@ -208,7 +210,16 @@ int
 dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *) 
__unused,
     void *data __unused)
 {
-#ifndef IN_LIBDL
+#if defined IN_LIBDL
+       return (0);
+#elif defined PIC
+       int (*r)(int (*)(struct dl_phdr_info *, size_t, void *), void *);
+
+       r = dlsym(RTLD_DEFAULT, "dl_iterate_phdr");
+       if (r == NULL)
+               return (0);
+       return (r(callback, data));
+#else
        tls_index ti;
        int ret;
 
@@ -223,8 +234,6 @@ dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, 
size_t, void *) __unused,
        ret = callback(&phdr_info, sizeof(phdr_info), data);
        mutex_unlock(&dl_phdr_info_lock);
        return (ret);
-#else
-       return (0);
 #endif
 }
 

Reply via email to