From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 26/27] gdb/testsuite: Factor out dlopen/LoadLibrary shim into lib/gdb-dlfcn.h
Date: Thu, 23 Jul 2026 14:01:17 +0100 [thread overview]
Message-ID: <20260723130118.206735-27-pedro@palves.net> (raw)
In-Reply-To: <20260723130118.206735-1-pedro@palves.net>
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
next prev parent reply other threads:[~2026-07-23 13:07 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 13:00 [PATCH 00/27] Teach the testsuite about the Windows/MSVC target Pedro Alves
2026-07-23 13:00 ` [PATCH 01/27] gdb/testsuite: Don't link with -lm on windows-msvc Pedro Alves
2026-07-23 13:00 ` [PATCH 02/27] gdb/testsuite: Use unprefixed runtest for a native Windows/MSVC build Pedro Alves
2026-07-23 13:00 ` [PATCH 03/27] gdb/testsuite: Find host binutils in a native Windows/MSVC config Pedro Alves
2026-07-23 13:00 ` [PATCH 04/27] gdb/testsuite: Compile with -Wno-deprecated-declarations on windows-msvc Pedro Alves
2026-07-23 13:00 ` [PATCH 05/27] gdb/testsuite: Recognize windows-msvc across lib/gdb.exp procedures Pedro Alves
2026-07-23 13:00 ` [PATCH 06/27] gdb/testsuite: Don't pass -fPIC on PE/COFF targets Pedro Alves
2026-07-23 13:00 ` [PATCH 07/27] gdb/testsuite: Use /IMPLIB on windows-msvc to name import libraries Pedro Alves
2026-07-23 13:00 ` [PATCH 08/27] gdb/testsuite: Export all DLL symbols on windows-msvc via generated .def Pedro Alves
2026-07-23 13:01 ` [PATCH 09/27] gdb/testsuite: Restrict --no-as-needed to ELF targets Pedro Alves
2026-07-23 13:01 ` [PATCH 10/27] gdb.base/set-cwd.exp: Use is_windows_native_target Pedro Alves
2026-07-23 13:01 ` [PATCH 11/27] gdb.base/exitsignal.exp: " Pedro Alves
2026-07-23 13:01 ` [PATCH 12/27] gdb.base/exitsignal.exp: Relax SIGSEGV second-chance pattern Pedro Alves
2026-07-23 13:01 ` [PATCH 13/27] gdb/testsuite: Skip -Ttext-segment on PE Pedro Alves
2026-07-23 13:01 ` [PATCH 14/27] gdb/testsuite: Support text_segment on windows-msvc via /BASE Pedro Alves
2026-07-23 13:01 ` [PATCH 15/27] gdb.base/shreloc.exp: Use gdb_compile text_segment to set image base Pedro Alves
2026-07-23 13:01 ` [PATCH 16/27] gdb/coffread: Don't relocate absolute symbols Pedro Alves
2026-07-23 13:01 ` [PATCH 17/27] gdb.base/shreloc.exp: Test absolute symbols portably Pedro Alves
2026-07-23 13:01 ` [PATCH 18/27] gdb: %p => host_address_to_string, target-section owner token Pedro Alves
2026-07-23 13:01 ` [PATCH 19/27] gdb: %p => host_address_to_string, dump_for_expression Pedro Alves
2026-07-23 13:01 ` [PATCH 20/27] gdb: %p => host_address_to_string, find_symtab_matching_filename Pedro Alves
2026-07-23 13:01 ` [PATCH 21/27] gdb: %p => host_address_to_string, handle_output_debug_string Pedro Alves
2026-07-23 13:01 ` [PATCH 22/27] gdb.base/maint-info-sections.exp: Match exec file name with optional .exe Pedro Alves
2026-07-23 13:01 ` [PATCH 23/27] gdb.base/maint-info-sections.exp: Remove stale Windows DATA xfail Pedro Alves
2026-07-23 13:01 ` [PATCH 24/27] gdb.base/solib-weak.exp: Skip on all PE/COFF targets Pedro Alves
2026-07-23 13:01 ` [PATCH 25/27] gdb.server/wrapper.exp: Skip on all Windows targets Pedro Alves
2026-07-23 13:01 ` Pedro Alves [this message]
2026-07-23 13:01 ` [PATCH 27/27] gdb/testsuite/lib/gdb-dlfcn.h: __WIN32__ => _WIN32 Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260723130118.206735-27-pedro@palves.net \
--to=pedro@palves.net \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox