[binutils-gdb] [gdb/build] Use gnulib fnmatch

Tom de Vries vries@sourceware.org
Sat Nov 14 13:44:14 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2e7f60647f1f68b32be625772b885c431fc3b095

commit 2e7f60647f1f68b32be625772b885c431fc3b095
Author: Tom de Vries <tdevries@suse.de>
Date:   Sat Nov 14 14:44:03 2026 +0100

    [gdb/build] Use gnulib fnmatch
    
    I built gdb on aarch64-macos, ran the skip_gfile_matching selftest and got:
    ...
    (gdb) maint selftest -v skip_gfile_matching
      ...
    Pattern (*[[:digit:]]*.h)
    Filename (xx1xx.h)
    Fullname (/tmp/xx1xx.h)
    Regexp (/[^/]*[[:digit:]][^/]*\.h$)
    Matched: No     [Expected: Yes]
      ...
    Failed: 5
    Self test failed: self-test failed at gdb/skip.c:1820
    
    Failures:
      skip_gfile_matching
    
    Ran 1 unit tests, 1 failed
    ...
    
    Disabling this piece of code in do_skip_gfile_p using gdb_filename_fnmatch:
    ...
      /* If basenames don't match then the full pattern cannot match.  The
         gdb_filename_fnmatch already handles case insensitive filesystems, and
         as we're only checking the basenames here, directory separators are
         not a problem.  */
      if (!basenames_may_differ
          && gdb_filename_fnmatch (lbasename (pattern.c_str ()),
                                   lbasename (get_filename ()),
                                   FNM_FILE_NAME | FNM_NOESCAPE) != 0)
        return false;
    ...
    makes the selftest pass.
    
    The failure reduces to this minimal reproducer:
    ...
        const char *pattern = "[[:digit:]]";
        const char *string = "1";
        int flags = FNM_FILE_NAME | FNM_NOESCAPE;
        int res = fnmatch (pattern, string, flags);
        gdb_assert (res == 0);
    ...
    and the root cause is that we use libiberty's fnmatch, which is rather old
    (based on some glibc v1 version).
    
    This happens as follows:
    - because system libc is not glibc, the !__GNU_LIBRARY__ test in
      libiberty/fnmatch.c succeeds and fnmatch.o contains an fnmatch function
    - because system libc is not glibc, gnulib also compiles an fnmatch function
    - because of the inclusion order in INTERNAL_CFLAGS of libiberty vs gnulib,
      the libiberty copy gets used
    
    For contrast, on x86_64-linux:
    - because system libc is glibc, the !__GNU_LIBRARY__ test in
      libiberty/fnmatch.c fails and fnmatch.o is empty
    - because system libc is glibc, gnulib doesn't compile an fnmatch function
    - fnmatch binds to fnmatch from the system glibc
    
    Fix this by changing the inclusion order in INTERNAL_CFLAGS.
    
    More concretely, by:
    - factoring out GNULIB_LIBIBERTY_CFLAGS,
    - using GNULIB_LIBIBERTY_CFLAGS in INTERNAL_CFLAGS instead of INCGNU and
      INCLUDE_CFLAGS, and
    - adding an extensive comment at GNULIB_LIBIBERTY_CFLAGS to explain why
      inclusion order matters.
    
    Likewise in gdbserver and gdbsupport dirs.
    
    Tested on aarch64-macos by rebuilding and running the selftests.
    
    Tested on x86_64-linux.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34877

Diff:
---
 gdb/Makefile.in        | 56 ++++++++++++++++++++++++++++++++++++++++++++++++--
 gdbserver/Makefile.in  |  8 ++++++--
 gdbsupport/Makefile.am | 10 ++++++---
 gdbsupport/Makefile.in | 10 ++++++---
 4 files changed, 74 insertions(+), 10 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 41dbb814787..55c4d012f25 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -157,6 +157,59 @@ CC_LD = $(LIBTOOL) $(SILENT_FLAG) --mode=link $(CXX) $(CXX_DIALECT)
 INCLUDE_DIR = $(srcdir)/../include
 INCLUDE_CFLAGS = -I$(INCLUDE_DIR)
 
+# GDB uses GNU libiberty and Gnulib.  Both are part portability, part utility
+# libraries.  Both are maintained externally, and we have in-repo copies.
+#
+# Libiberty is a "decommissioned GNU package".  OTOH, libiberty is maintained
+# as part of GCC, and so at least the parts that GCC relies on are maintained.
+# For functionality provided by both Gnulib and libiberty, we prefer the
+# Gnulib one because it's actively maintained as a whole.
+#
+# While for Gnulib we have a means to selectively import only the part that we
+# want (gnulib/update-gnulib.sh), libiberty is imported as a whole.
+# Consequently we can end up with conflicting files, making inclusion
+# order the determining factor for which one we use.  So reflecting the
+# preference mentioned above, we include Gnulib before libiberty.
+#
+# As a running example, consider fnmatch.
+#
+# On a macOS system:
+# - libiberty provides:
+#   - include/fnmatch.h
+#   - build/libiberty/fnmatch.o containing a definition for fnmatch
+# - Gnulib provides:
+#   - build/gnulib/import/fnmatch.h, mapping fnmatch to rpl_fnmatch
+#   - build/gnulib/import/fnmatch.o containing a definition for rpl_fnmatch
+# Depending on which fnmatch.h gets picked, GDB gets either the libiberty or
+# the Gnulib version.  Note that in this case at least, linking order doesn't
+# matter.  Also note that even when GDB uses Gnulib's fnmatch, other
+# components may still use libiberty's.
+#
+# For contrast, let's also consider fnmatch on a Linux system with glibc:
+# - libiberty provides:
+#   - include/fnmatch.h
+#   - build/libiberty/fnmatch.o containing no definitions
+# - Gnulib provides nothing fnmatch related
+# In this case, the fnmatch.h from libiberty is used, but at link time glibc's
+# version is used, which is not ideal.
+#
+# So while enforcing inclusion order works, IWBN to have a better solution.
+#
+# Some alternatives avoiding libiberty's fnmatch are:
+# - selective importing of libiberty, like we have for Gnulib.
+# - updating the external libiberty (maintained in GCC) to drop fnmatch.  The
+#   first step for this would be for GCC to stop using libiberty's fnmatch,
+#   which was once proposed as part of an effort to start using Gnulib in
+#   GCC [1], but this effort stalled.
+#   [1] https://gcc.gnu.org/wiki/replacelibibertywithgnulib
+# Both of these would have to take into consideration other in-repo components
+# using libiberty.
+#
+# We could also update the external libiberty to include a more modern copy of
+# fnmatch.  But the question is whether it's a good use of effort to maintain a
+# decommissioned library if a better alternative is already available.
+GNULIB_LIBIBERTY_CFLAGS = $(INCGNU) $(INCLUDE_CFLAGS)
+
 # Where is the "-liberty" library?  Typically in ../libiberty.
 LIBIBERTY = ../libiberty/libiberty.a
 
@@ -636,13 +689,12 @@ INTERNAL_CFLAGS = \
 	$(GDB_CFLAGS) \
 	$(OPCODES_CFLAGS) \
 	$(BFD_CFLAGS) \
-	$(INCLUDE_CFLAGS) \
+	$(GNULIB_LIBIBERTY_CFLAGS) \
 	$(READLINE_CFLAGS) \
 	$(ZLIBINC) \
 	$(ZSTD_CFLAGS) \
 	$(LIBDECNUMBER_CFLAGS) \
 	$(INTL_CFLAGS) \
-	$(INCGNU) \
 	$(INCSUPPORT) \
 	$(LIBBACKTRACE_INC) \
 	$(ENABLE_CFLAGS) \
diff --git a/gdbserver/Makefile.in b/gdbserver/Makefile.in
index aac8742a923..e489af1f410 100644
--- a/gdbserver/Makefile.in
+++ b/gdbserver/Makefile.in
@@ -118,6 +118,11 @@ INCSUPPORT = \
 	-I$(srcdir)/.. \
 	-I..
 
+# See gdb/Makefile.in.
+GNULIB_LIBIBERTY_CFLAGS = \
+	$(INCGNU) \
+	-I$(INCLUDE_DIR)
+
 # All the includes used for CFLAGS and for lint.
 # -I. for config files.
 # -I${srcdir} for our headers.
@@ -132,9 +137,8 @@ INCLUDE_CFLAGS = \
 	-I${srcdir} \
 	-I$(srcdir)/../gdb/regformats \
 	-I$(srcdir)/.. \
-	-I$(INCLUDE_DIR) \
+	$(GNULIB_LIBIBERTY_CFLAGS) \
 	-I$(srcdir)/../gdb \
-	$(INCGNU) \
 	$(INCSUPPORT) \
 	$(INTL_CFLAGS)
 
diff --git a/gdbsupport/Makefile.am b/gdbsupport/Makefile.am
index 01a623b1be0..c895e0ecaca 100644
--- a/gdbsupport/Makefile.am
+++ b/gdbsupport/Makefile.am
@@ -20,16 +20,20 @@
 AUTOMAKE_OPTIONS = no-dist foreign
 ACLOCAL_AMFLAGS = -I . -I ../config
 
+# See gdb/Makefile.in.
+GNULIB_LIBIBERTY_CFLAGS = \
+	-I$(srcdir)/../gnulib/import \
+	-I../gnulib/import \
+	-I$(srcdir)/../include
+
 # Suppress 'configure.ac: error: AM_GNU_GETTEXT used but SUBDIRS not defined'
 # from Automake, as gdbsupport uses AM_GNU_GETTEXT through
 # ZW_GNU_GETTEXT_SISTER_DIR, but doesn't have any translations (currently).
 SUBDIRS =
 
 AM_CPPFLAGS = \
-	-I$(srcdir)/../include \
 	-I$(srcdir)/../gdb \
-	-I$(srcdir)/../gnulib/import \
-	-I../gnulib/import \
+	$(GNULIB_LIBIBERTY_CFLAGS) \
 	-I.. \
 	-I$(srcdir)/.. \
 	$(INCINTL) \
diff --git a/gdbsupport/Makefile.in b/gdbsupport/Makefile.in
index c3be0660336..c05ce5fc494 100644
--- a/gdbsupport/Makefile.in
+++ b/gdbsupport/Makefile.in
@@ -389,15 +389,19 @@ top_srcdir = @top_srcdir@
 AUTOMAKE_OPTIONS = no-dist foreign
 ACLOCAL_AMFLAGS = -I . -I ../config
 
+# See gdb/Makefile.in.
+GNULIB_LIBIBERTY_CFLAGS = \
+	-I$(srcdir)/../gnulib/import \
+	-I../gnulib/import \
+	-I$(srcdir)/../include
+
 # Suppress 'configure.ac: error: AM_GNU_GETTEXT used but SUBDIRS not defined'
 # from Automake, as gdbsupport uses AM_GNU_GETTEXT through
 # ZW_GNU_GETTEXT_SISTER_DIR, but doesn't have any translations (currently).
 SUBDIRS = 
 AM_CPPFLAGS = \
-	-I$(srcdir)/../include \
 	-I$(srcdir)/../gdb \
-	-I$(srcdir)/../gnulib/import \
-	-I../gnulib/import \
+	$(GNULIB_LIBIBERTY_CFLAGS) \
 	-I.. \
 	-I$(srcdir)/.. \
 	$(INCINTL) \


More information about the Gdb-cvs mailing list