From: Bruno Haible Date: Tue, 9 Dec 2025 13:58:00 +0000 (+0100) Subject: relocatable-lib: Fix and simplify previous commit. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34951d0cb0fbfcc86fa8e37bc9ff5a2e03662f60;p=thirdparty%2Fgnulib.git relocatable-lib: Fix and simplify previous commit. * m4/relocatable-lib.m4 (gl_RELOCATABLE_LIBRARY_BODY): Invoke gl_LIBDL. Define HAVE_DLADDR_IN_LIBC. * modules/relocatable-lib (configure.ac): Revert last change. * modules/relocatable-lib-lgpl (Files): Add m4/libdl.m4. (configure.ac): Revert last change. * lib/relocatable.c (_GL_USE_PROCFS, _GL_USE_WIN32, _GL_USE_DLADDR): Remove macros. (ENABLE_COSTLY_RELOCATABLE): Update comments. (find_shared_library_fullname): Use dladdr as first alternative. Test HAVE_DLADDR_IN_LIBC instead of _GL_USE_PROCFS. Define as macro. (get_shared_library_fullname): Update comments. Test find_shared_library_fullname instead of _GL_USE_WIN32. --- diff --git a/ChangeLog b/ChangeLog index cf3ddd6df8..e745af71cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2025-12-09 Bruno Haible + + relocatable-lib: Fix and simplify previous commit. + * m4/relocatable-lib.m4 (gl_RELOCATABLE_LIBRARY_BODY): Invoke gl_LIBDL. + Define HAVE_DLADDR_IN_LIBC. + * modules/relocatable-lib (configure.ac): Revert last change. + * modules/relocatable-lib-lgpl (Files): Add m4/libdl.m4. + (configure.ac): Revert last change. + * lib/relocatable.c (_GL_USE_PROCFS, _GL_USE_WIN32, _GL_USE_DLADDR): + Remove macros. + (ENABLE_COSTLY_RELOCATABLE): Update comments. + (find_shared_library_fullname): Use dladdr as first alternative. Test + HAVE_DLADDR_IN_LIBC instead of _GL_USE_PROCFS. Define as macro. + (get_shared_library_fullname): Update comments. Test + find_shared_library_fullname instead of _GL_USE_WIN32. + 2025-12-09 Reuben Thomas relocatable-lib: Find shared library filename via dladdr. diff --git a/lib/relocatable.c b/lib/relocatable.c index a3d741aff2..5eb7e8b716 100644 --- a/lib/relocatable.c +++ b/lib/relocatable.c @@ -55,6 +55,10 @@ # define strncmp strnicmp #endif +#if HAVE_DLADDR_IN_LIBC +# include +#endif + #if DEPENDS_ON_LIBCHARSET # include #endif @@ -65,21 +69,6 @@ # include #endif -/* We have special code for two types of system: non-Cygwin Windows, and - Linux where dladdr is in a separate library (uClibc and glibc < 2.34). - Need glibc >= 2, for getline(). - - Otherwise, use dladdr. -*/ -#if defined __linux__ && (defined __UCLIBC__ || ((__GLIBC__ >= 2) && (__GLIBC_MINOR__ < 34))) -# define _GL_USE_PROCFS 1 -#elif (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ -# define _GL_USE_WIN32 1 -#elif _GL_DLADDR_IN_LIBC -# define _GL_USE_DLADDR 1 -# include -#endif - #if defined _WIN32 && !defined __CYGWIN__ /* Don't assume that UNICODE is not defined. */ # undef GetModuleFileName @@ -116,9 +105,8 @@ /* Whether to enable the more costly support for relocatable libraries. It allows libraries to be have been installed with a different original - prefix than the program. But it is quite costly, especially on Cygwin - platforms, see below. Therefore we enable it by default only on native - Windows platforms. */ + prefix than the program. But it is quite costly, see below. Therefore + we enable it by default only on native Windows platforms. */ #ifndef ENABLE_COSTLY_RELOCATABLE # if defined _WIN32 && !defined __CYGWIN__ # define ENABLE_COSTLY_RELOCATABLE 1 @@ -333,9 +321,10 @@ static char *shared_library_fullname; #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows only. - On Cygwin, it is better to use dladdr, than to use native Windows - API and cygwin_conv_to_posix_path, because it supports longer file - names (see ). */ + On Cygwin, it is better to use either dladdr() or the Cygwin provided /proc + interface, than to use native Windows API and cygwin_conv_to_posix_path, + because it supports longer file names + (see ). */ /* Determine the full pathname of the shared library when it is loaded. @@ -416,7 +405,17 @@ _DLL_InitTerm (unsigned long hModule, unsigned long ulFlag) static void find_shared_library_fullname () { -#if _GL_USE_PROCFS +#if HAVE_DLADDR_IN_LIBC + /* glibc >= 2.34, musl, macOS, FreeBSD, NetBSD, OpenBSD, Solaris, Cygwin, Minix */ + /* We can use dladdr() without introducing extra link dependencies. */ + Dl_info info; + /* It is OK to use a 'static' function — that does not appear in the + dynamic symbol table of any ELF object — as argument of dladdr() here, + because we don't access the fields info.dli_sname and info.dli_saddr. */ + int ret = dladdr (find_shared_library_fullname, &info); + if (ret != 0 && info.dli_fname != NULL) + shared_library_fullname = strdup (info.dli_fname); +#elif (defined __linux__ && (__GLIBC__ >= 2 || defined __UCLIBC__)) || defined __CYGWIN__ /* Linux has /proc/self/maps. glibc 2 and uClibc have the getline() function. But it is costly: ca. 0.3 ms. */ @@ -461,24 +460,22 @@ find_shared_library_fullname () } fclose (fp); } -#elif _GL_USE_DLADDR - Dl_info info; - int ret = dladdr (find_shared_library_fullname, &info); - if (ret != 0 && info.dli_fname != NULL) - shared_library_fullname = strdup (info.dli_fname); #endif } +# define find_shared_library_fullname find_shared_library_fullname + #endif /* Native Windows / EMX / Unix */ /* Return the full pathname of the current shared library. Return NULL if unknown. - Guaranteed to work only on Linux, EMX, Cygwin, native Windows, and - systems with dladdr in libc. */ + Guaranteed to work only on + glibc >= 2.34, Linux, macOS, FreeBSD, NetBSD, OpenBSD, Solaris, Cygwin, + Minix, native Windows, EMX. */ static char * get_shared_library_fullname () { -#if !_GL_USE_WIN32 +#if defined find_shared_library_fullname static bool tried_find_shared_library_fullname; if (!tried_find_shared_library_fullname) { diff --git a/m4/libdl.m4 b/m4/libdl.m4 index 839b053af5..6c87f727bc 100644 --- a/m4/libdl.m4 +++ b/m4/libdl.m4 @@ -1,5 +1,5 @@ # libdl.m4 -# serial 2 +# serial 3 dnl Copyright (C) 2024-2025 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -15,7 +15,7 @@ AC_DEFUN([gl_LIBDL], dnl dlopen, dlsym are dnl - in libc on glibc >= 2.34, musl libc, macOS, FreeBSD, NetBSD, OpenBSD, dnl AIX, Solaris, Cygwin, Haiku, - dnl - in a separate libdl on glibc < 2.34, Android, uClibc. + dnl - in a separate libdl on glibc < 2.34, uClibc, Android. AC_CACHE_CHECK([for library needed for dlopen and dlsym], [gl_cv_lib_dl], [AC_LINK_IFELSE( diff --git a/m4/relocatable-lib.m4 b/m4/relocatable-lib.m4 index 5efed5218d..222fb68494 100644 --- a/m4/relocatable-lib.m4 +++ b/m4/relocatable-lib.m4 @@ -1,5 +1,5 @@ # relocatable-lib.m4 -# serial 8 +# serial 9 dnl Copyright (C) 2003, 2005-2007, 2009-2025 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -31,6 +31,14 @@ AC_DEFUN([gl_RELOCATABLE_LIBRARY_BODY], [Define to 1 if the package shall run at any location in the file system.]) fi + dnl Determine whether dladdr() exists in libc. + gl_LIBDL + if test -z "$LIBDL"; then + AC_CHECK_FUNC([dladdr], + [AC_DEFINE([HAVE_DLADDR_IN_LIBC], [1], + [Define to 1 if dladdr exists and is defined in libc.]) + ]) + fi ]) dnl Support for relocatable packages for which it is a nop. diff --git a/modules/relocatable-lib b/modules/relocatable-lib index d2f513861b..64be8dbf49 100644 --- a/modules/relocatable-lib +++ b/modules/relocatable-lib @@ -7,8 +7,8 @@ doc/relocatable.texi lib/relocatable.h lib/relocatable.c lib/relocatable.valgrind -m4/libdl.m4 m4/relocatable-lib.m4 +m4/libdl.m4 m4/build-to-host.m4 Depends-on: @@ -20,10 +20,6 @@ gl_RELOCATABLE_LIBRARY if test $RELOCATABLE = yes; then AC_LIBOBJ([relocatable]) fi -gl_LIBDL -if test -z "$LIBDL"; then - AC_DEFINE([_GL_DLADDR_IN_LIBC], [1], [Define to 1 if dladdr is in libc.]) -fi Makefile.am: diff --git a/modules/relocatable-lib-lgpl b/modules/relocatable-lib-lgpl index 4fe84cea85..c0cdafade2 100644 --- a/modules/relocatable-lib-lgpl +++ b/modules/relocatable-lib-lgpl @@ -8,6 +8,7 @@ lib/relocatable.h lib/relocatable.c lib/relocatable.valgrind m4/relocatable-lib.m4 +m4/libdl.m4 m4/build-to-host.m4 Depends-on: @@ -18,10 +19,6 @@ gl_RELOCATABLE_LIBRARY if test $RELOCATABLE = yes; then AC_LIBOBJ([relocatable]) fi -gl_LIBDL -if test -z "$LIBDL"; then - AC_DEFINE([_GL_DLADDR_IN_LIBC], [1], [Define to 1 if dladdr is in libc.]) -fi Makefile.am: DEFS += -DNO_XMALLOC