[PATCH 26/27] gdb/testsuite: Factor out dlopen/LoadLibrary shim into lib/gdb-dlfcn.h

Pedro Alves <[email protected]> Thu, 23 Jul 2026 14:01:17 +0100
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
Several testcases that dlopen a shared library at runtime have a set
of similar preprocessor shims that map the POSIX
dlopen/dlsym/dlclose/dlerror interfaces onto the equivalent Win32
functions on Windows.

This commit moves the shims into a new testsuite/lib/gdb-dlfcn.h, and
includes it from each testcase.  gdb_compile already adds
testsuite/lib/ to the include path, so no .exp changes are needed.

The header provides a small static gdb_dlerror() for the Windows side,
formatting GetLastError() the way watchpoint-solib.c and unload.c used
to do inline, so those two lose their in-body __WIN32__ blocks too and
just call dlerror() unconditionally.

TEXT() is dropped: it only matters for the WinCE wide-char build
(_WIN32_WCE), and we dropped WinCE support long ago.

This will make the following patch trivial.

Change-Id: I7618808a3c766ae5570358f6e0bfb9ceabf4f4c0
---
 gdb/testsuite/gdb.base/bp-disabled-by-cond.c  | 13 +----
 gdb/testsuite/gdb.base/catch-load.c           |  8 +--
 .../gdb.base/corefile-buildid-shlib.c         | 10 +---
 gdb/testsuite/gdb.base/jit-unload.c           |  9 +---
 gdb/testsuite/gdb.base/shlib-unload.c         | 13 +----
 gdb/testsuite/gdb.base/solib-disc.c           | 10 +---
 gdb/testsuite/gdb.base/unload.c               | 21 +-------
 gdb/testsuite/gdb.base/watchpoint-solib.c     | 17 +------
 gdb/testsuite/gdb.mi/mi-catch-load.c          |  8 +--
 gdb/testsuite/gdb.mi/mi-dprintf-modified.c    | 13 +----
 gdb/testsuite/gdb.perf/solib.c                |  9 +---
 gdb/testsuite/gdb.python/py-event-load.c      |  8 +--
 gdb/testsuite/lib/gdb-dlfcn.h                 | 51 +++++++++++++++++++
 13 files changed, 63 insertions(+), 127 deletions(-)
 create mode 100644 gdb/testsuite/lib/gdb-dlfcn.h

diff --git a/gdb/testsuite/gdb.base/bp-disabled-by-cond.c b/gdb/testsuite/gdb.base/bp-disabled-by-cond.c
index 0d147b34410..f09f90b43a6 100644
--- a/gdb/testsuite/gdb.base/bp-disabled-by-cond.c
+++ b/gdb/testsuite/gdb.base/bp-disabled-by-cond.c
@@ -18,18 +18,7 @@
 #include <assert.h>
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#ifdef _WIN32_WCE
-# define dlsym(handle, func) GetProcAddress (handle, TEXT (func))
-#else
-# define dlsym(handle, func) GetProcAddress (handle, func)
-#endif
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 void
 breakpt ()
diff --git a/gdb/testsuite/gdb.base/catch-load.c b/gdb/testsuite/gdb.base/catch-load.c
index bb53fddc557..e9f8f63362d 100644
--- a/gdb/testsuite/gdb.base/catch-load.c
+++ b/gdb/testsuite/gdb.base/catch-load.c
@@ -17,13 +17,7 @@
 
 #include <stdio.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 /* This is updated by the .exp file.  */
 char *libname = "catch-load-so.so";
diff --git a/gdb/testsuite/gdb.base/corefile-buildid-shlib.c b/gdb/testsuite/gdb.base/corefile-buildid-shlib.c
index 480494d98f7..dca51bc6749 100644
--- a/gdb/testsuite/gdb.base/corefile-buildid-shlib.c
+++ b/gdb/testsuite/gdb.base/corefile-buildid-shlib.c
@@ -19,15 +19,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (name)
-#define dlsym(handle, func) GetProcAddress (handle, func)
-#define dlclose(handle) FreeLibrary (handle)
-#define dlerror() "an error occurred"
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 const char *the_shlib = SHLIB_NAME;
 
diff --git a/gdb/testsuite/gdb.base/jit-unload.c b/gdb/testsuite/gdb.base/jit-unload.c
index 058fb54fd71..e10ba221314 100644
--- a/gdb/testsuite/gdb.base/jit-unload.c
+++ b/gdb/testsuite/gdb.base/jit-unload.c
@@ -20,14 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#define dlclose(handle) FreeLibrary (handle)
-#define dlerror() "an error occurred"
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 
 int
diff --git a/gdb/testsuite/gdb.base/shlib-unload.c b/gdb/testsuite/gdb.base/shlib-unload.c
index e2c2051a19f..6332945b690 100644
--- a/gdb/testsuite/gdb.base/shlib-unload.c
+++ b/gdb/testsuite/gdb.base/shlib-unload.c
@@ -17,18 +17,7 @@
 
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#ifdef _WIN32_WCE
-# define dlsym(handle, func) GetProcAddress (handle, TEXT (func))
-#else
-# define dlsym(handle, func) GetProcAddress (handle, func)
-#endif
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 #include <assert.h>
 
diff --git a/gdb/testsuite/gdb.base/solib-disc.c b/gdb/testsuite/gdb.base/solib-disc.c
index 9230ab13c67..365e352f579 100644
--- a/gdb/testsuite/gdb.base/solib-disc.c
+++ b/gdb/testsuite/gdb.base/solib-disc.c
@@ -18,15 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (name)
-#define dlsym(handle, func) GetProcAddress (handle, func)
-#define dlclose(handle) FreeLibrary (handle)
-#define dlerror() "an error occurred"
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 int main()
 {
diff --git a/gdb/testsuite/gdb.base/unload.c b/gdb/testsuite/gdb.base/unload.c
index 7c365aef9ef..98451a43a27 100644
--- a/gdb/testsuite/gdb.base/unload.c
+++ b/gdb/testsuite/gdb.base/unload.c
@@ -18,18 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#ifdef _WIN32_WCE
-# define dlsym(handle, func) GetProcAddress (handle, TEXT (func))
-#else
-# define dlsym(handle, func) GetProcAddress (handle, func)
-#endif
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 int k = 0;
 
@@ -52,11 +41,7 @@ int main()
 
   if (!unloadshr)
     {
-#ifdef __WIN32__
-      fprintf (stderr, "error %d occurred\n", GetLastError ());
-#else
       fprintf (stderr, "%s\n", dlerror ());
-#endif
       exit (1);
     }
 
@@ -79,11 +64,7 @@ int main()
 
   if (!unloadshr)
     {
-#ifdef __WIN32__
-      fprintf (stderr, "error %d occurred\n", GetLastError ());
-#else
       fprintf (stderr, "%s\n", dlerror ());
-#endif
       exit (1);
     }
 
diff --git a/gdb/testsuite/gdb.base/watchpoint-solib.c b/gdb/testsuite/gdb.base/watchpoint-solib.c
index 79a9e91c642..0e4732f8325 100644
--- a/gdb/testsuite/gdb.base/watchpoint-solib.c
+++ b/gdb/testsuite/gdb.base/watchpoint-solib.c
@@ -18,18 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#ifdef _WIN32_WCE
-# define dlsym(handle, func) GetProcAddress (handle, TEXT (func))
-#else
-# define dlsym(handle, func) GetProcAddress (handle, func)
-#endif
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 
 void open_shlib ()
@@ -41,11 +30,7 @@ void open_shlib ()
 
   if (!handle)
     {
-#ifdef __WIN32__
-      fprintf (stderr, "error %d occurred\n", GetLastError ());
-#else
       fprintf (stderr, "%s\n", dlerror ());
-#endif
       exit (1);
     }
 
diff --git a/gdb/testsuite/gdb.mi/mi-catch-load.c b/gdb/testsuite/gdb.mi/mi-catch-load.c
index 0808d9fa759..bad7f79a37b 100644
--- a/gdb/testsuite/gdb.mi/mi-catch-load.c
+++ b/gdb/testsuite/gdb.mi/mi-catch-load.c
@@ -17,13 +17,7 @@
 
 #include <stdio.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 char *libname = "mi-catch-load-so.so";
 
diff --git a/gdb/testsuite/gdb.mi/mi-dprintf-modified.c b/gdb/testsuite/gdb.mi/mi-dprintf-modified.c
index 03b88712bfc..af8ca6c5940 100644
--- a/gdb/testsuite/gdb.mi/mi-dprintf-modified.c
+++ b/gdb/testsuite/gdb.mi/mi-dprintf-modified.c
@@ -17,18 +17,7 @@
 
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#ifdef _WIN32_WCE
-# define dlsym(handle, func) GetProcAddress (handle, TEXT (func))
-#else
-# define dlsym(handle, func) GetProcAddress (handle, func)
-#endif
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 #include <assert.h>
 
diff --git a/gdb/testsuite/gdb.perf/solib.c b/gdb/testsuite/gdb.perf/solib.c
index 372330b50a7..c4be9e9b46e 100644
--- a/gdb/testsuite/gdb.perf/solib.c
+++ b/gdb/testsuite/gdb.perf/solib.c
@@ -18,14 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-# define dlsym(handle, func) GetProcAddress (handle, func)
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 static void **handles;
 
diff --git a/gdb/testsuite/gdb.python/py-event-load.c b/gdb/testsuite/gdb.python/py-event-load.c
index 1ab5bf832e9..e4887e1a056 100644
--- a/gdb/testsuite/gdb.python/py-event-load.c
+++ b/gdb/testsuite/gdb.python/py-event-load.c
@@ -17,13 +17,7 @@
 
 #include <unistd.h>
 
-#ifdef __WIN32__
-#include <windows.h>
-#define dlopen(name, mode) LoadLibrary (TEXT (name))
-#define dlclose(handle) FreeLibrary (handle)
-#else
-#include <dlfcn.h>
-#endif
+#include "gdb-dlfcn.h"
 
 /* This is updated by the .exp file.  */
 char *libname = "py-events-shlib.so";
diff --git a/gdb/testsuite/lib/gdb-dlfcn.h b/gdb/testsuite/lib/gdb-dlfcn.h
new file mode 100644
index 00000000000..02cd6141b2c
--- /dev/null
+++ b/gdb/testsuite/lib/gdb-dlfcn.h
@@ -0,0 +1,51 @@
+/* This file is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Provide the POSIX dlopen/dlsym/dlclose/dlerror interface for test
+   programs that load a shared library at runtime, mapping it to the
+   Win32 loader API on Windows.  */
+
+#ifndef GDB_DLFCN_H
+#define GDB_DLFCN_H
+
+#ifdef __WIN32__
+
+#include <windows.h>
+#include <stdio.h>
+
+#define dlopen(name, mode) LoadLibrary (name)
+#define dlsym(handle, func) GetProcAddress (handle, func)
+#define dlclose(handle) FreeLibrary (handle)
+#define dlerror() gdb_dlerror ()
+
+static char _gdb_dlerror_buf[64];
+
+static const char *
+gdb_dlerror (void)
+{
+  snprintf (_gdb_dlerror_buf, sizeof (_gdb_dlerror_buf),
+	    "error %lu occurred", (unsigned long) GetLastError ());
+  return _gdb_dlerror_buf;
+}
+
+#else /* POSIX systems */
+
+#include <dlfcn.h>
+
+#endif
+
+#endif /* GDB_DLFCN_H */
-- 
2.54.0