Disable OpenBSD Clang's return protection for PE modules: the added
instrumentation is not suitable for Wine's Windows code and breaks startup.

Include netinet/tcp_timer.h when checking BSD network headers on OpenBSD,
so missing prerequisite declarations do not make configure reject them.

Build the OpenBSD loader as PIE.

Adapt the resolver probe to OpenBSD's headers and C_IN constant. Do not
require ns_initparse(), which is unavailable there, to detect res_query().

Suppress the out-of-tree gettext notice for the port's separate build.
This removes the configure diagnostic; it does not change po build rules.

Detect OpenBSD's SDL2 soname without the libSDL2-2.0 naming restriction
used on other systems.

Find _Unwind_Find_FDE in libc++abi when it is not available without an
extra library, and pass UNWIND_LIBS to ntdll's Unix library build.

Include sys/socket.h and netinet/tcp_timer.h in the tcpstat member probe
so the OpenBSD header prerequisites are satisfied.

Enable winesndio.drv

Index: configure.ac
--- configure.ac.orig
+++ configure.ac
@@ -56,6 +56,7 @@ AC_ARG_WITH(pthread,   AS_HELP_STRING([--without-pthre
 AC_ARG_WITH(pulse,     AS_HELP_STRING([--without-pulse],[do not use PulseAudio sound support]))
 AC_ARG_WITH(sane,      AS_HELP_STRING([--without-sane],[do not use SANE (scanner support)]))
 AC_ARG_WITH(sdl,       AS_HELP_STRING([--without-sdl],[do not use SDL]))
+AC_ARG_WITH(sndio,     AS_HELP_STRING([--without-sndio],[do not use sndio sound support]))
 AC_ARG_WITH(udev,      AS_HELP_STRING([--without-udev],[do not use udev (plug and play support)]))
 AC_ARG_WITH(usb,       AS_HELP_STRING([--without-usb],[do not use the libusb library]))
 AC_ARG_WITH(v4l2,      AS_HELP_STRING([--without-v4l2],[do not use v4l2 (video capture)]))
@@ -617,6 +618,23 @@ Please install mingw-w64, clang/llvm-dlltool/lld, or l
     WINE_TRY_PE_CFLAGS([-ffunction-sections])
     WINE_TRY_PE_CFLAGS([-ffp-exception-behavior=maytrap])
 
+    dnl OpenBSD's clang adds return protection, and Wine's own modules will fail on startup if return protection is added
+    WINE_TRY_PE_CFLAGS([-fno-ret-protector])
+
+    dnl OpenBSD requires a define hint for the include/ folder so that it knows to use %fs+offset instead of %gs+0x30
+    case $host_os in
+        openbsd*)
+            AS_VAR_APPEND([${wine_arch}_EXTRACFLAGS],[" -D__WINE_OPENBSD_GS_REPLACE"])
+        ;;
+    esac
+
+    dnl In OpenBSD we don't want to work with %gs, so we can force all TLS code to be emulated using -femulated-tls
+    case $host_os in
+        openbsd*)
+            WINE_TRY_PE_CFLAGS([-femulated-tls])
+        ;;
+    esac
+
     dnl clang had broken -fms-hotpatch support before version 18 (https://github.com/llvm/llvm-project/pull/77245)
     WINE_TRY_PE_CFLAGS([-fms-hotpatch -DMIN_CLANG_VERSION=18],
                        [AS_VAR_APPEND([${wine_arch}_EXTRACFLAGS],[" -fms-hotpatch"])
@@ -890,6 +908,9 @@ AC_CHECK_HEADERS([netinet/udp_var.h netinet/icmp_var.h
      #endif
      #ifdef HAVE_NETINET_TCP_H
      # include <netinet/tcp.h>
+     #endif
+     #ifdef __OpenBSD__
+     # include <netinet/tcp_timer.h>
      #endif])
 
 AC_CHECK_HEADERS([linux/ipx.h linux/irda.h linux/rtnetlink.h],,,
@@ -1113,6 +1134,10 @@ case $host_os in
                 WINE_TRY_CFLAGS([-Wl,--no-rosegment],[AS_VAR_APPEND([WINEPRELOADER_LDFLAGS],[" -Wl,--no-rosegment"])])]) ;;
         esac
         ;;
+      openbsd*)
+        AS_VAR_APPEND([WINELOADER_LDFLAGS],[" -pie"])
+        dnl We can only support x64 arch for OpenBSD
+        ;;
       *)
         case $HOST_ARCH in
           i386|arm)
@@ -1667,7 +1692,18 @@ then
          AC_LINK_IFELSE([AC_LANG_PROGRAM([[#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
-#include <resolv.h>]],[[if (!(_res.options & RES_INIT)) res_init(); res_query("foo",ns_c_in,0,0,0); ns_initparse(0,0,0)]])],
+#ifdef HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif
+#include <resolv.h>
+#ifdef __OpenBSD__
+#define ns_c_in C_IN
+#endif]],[[if (!(_res.options & RES_INIT)) res_init();
+res_query("foo",ns_c_in,0,0,0);
+#ifndef __OpenBSD__
+ns_initparse(0,0,0);
+#endif
+]])],
            [ac_cv_have_resolv=${lib:-"none required"}],[ac_cv_have_resolv="not found"])
          test "x$ac_cv_have_resolv" = "xnot found" || break
        done
@@ -1732,8 +1768,6 @@ then
     fi
     WINE_NOTICE_WITH(gettextpo,[test "x$GETTEXTPO_LIBS" = "x"],
       [GetText ${notice_platform}development files not found (or too old), po files can't be rebuilt.])
-    WINE_NOTICE_WITH(gettextpo,[test "$srcdir" != .],
-      [Rebuilding po files is not supported for out of tree builds.])
 fi
 
 dnl **** Check for PulseAudio ****
@@ -1746,6 +1780,16 @@ WINE_NOTICE_WITH(pulse, [test "x$ac_cv_func_pa_stream_
         [libpulse ${notice_platform}development files not found or too old, Pulse won't be supported.],
         [enable_winepulse_drv])
 
+dnl **** Check for sndio ****
+if test "x$with_sndio" != "xno"
+then
+    AC_CHECK_HEADER([sndio.h],
+        [AC_CHECK_LIB(sndio,sio_open,[AC_SUBST(SNDIO_LIBS,"-lsndio")])])
+fi
+WINE_NOTICE_WITH(sndio,[test "x$ac_cv_lib_sndio_sio_open" != xyes],
+        [libsndio ${notice_platform}development files not found, sndio won't be supported.],      
+        [enable_winesndio_drv])
+
 dnl **** Check for FFmpeg ****
 if test "x$with_ffmpeg" != "xno";
 then
@@ -1837,7 +1881,9 @@ if test "x$with_sdl" != "xno"
 then
     WINE_PACKAGE_FLAGS(SDL2,[sdl2],[-lSDL2],,,
         [AC_CHECK_HEADERS([SDL.h],
-            [WINE_CHECK_SONAME(SDL2,SDL_Init,,,,[[libSDL2-2.0*]])])])
+            [AS_CASE([$host_os],
+                [openbsd*], [WINE_CHECK_SONAME(SDL2,SDL_Init)],
+                [WINE_CHECK_SONAME(SDL2,SDL_Init,,,,[[libSDL2-2.0*]])])])])
 fi
 WINE_NOTICE_WITH(sdl,[test "x$ac_cv_lib_soname_SDL2" = "x"],
                  [libSDL2 ${notice_platform}development files not found, SDL2 won't be supported.])
@@ -1926,10 +1972,14 @@ fi
 WINE_NOTICE_WITH(netapi,[test "x$ac_cv_lib_soname_netapi" = "x"],
                  [libnetapi not found, Samba NetAPI won't be supported.])
 
+dnl **** Check for _Unwind_Find_FDE (in libc++abi on OpenBSD) ****
+AC_CHECK_FUNC(_Unwind_Find_FDE,,
+            [AC_CHECK_LIB(c++abi,_Unwind_Find_FDE,[AC_SUBST(UNWIND_LIBS,"-lc++abi")],,[$PTHREAD_LIBS])])
 
+
 dnl **** Check for any sound system ****
-if test "x$enable_winealsa_drv$enable_winecoreaudio_drv$enable_winepulse_drv$enable_wineoss_drv$enable_wineandroid_drv" = xnonononono -a \
-        "x$with_alsa$with_coreaudio$with_oss$with_pulse" != xnononono
+if test "x$enable_winealsa_drv$enable_winecoreaudio_drv$enable_winepulse_drv$enable_wineoss_drv$enable_wineandroid_drv$enable_winesndio_drv" = xnononononono -a \
+        "x$with_alsa$with_coreaudio$with_oss$with_pulse$with_sndio" != xnonononono
 then
     WINE_WARNING([No sound system was found. Windows applications will be silent.])
 fi
@@ -2304,6 +2354,7 @@ AC_CHECK_MEMBERS([struct icmp6stat.icp6s_error],,,
 dnl Check for struct tcpstat
 AC_CHECK_MEMBERS([struct tcpstat.tcps_connattempt],,,
 [#include <sys/types.h>
+#include <sys/socket.h>
 #ifdef HAVE_SYS_SOCKETVAR_H
 #include <sys/socketvar.h>
 #endif
@@ -2313,6 +2364,9 @@ AC_CHECK_MEMBERS([struct tcpstat.tcps_connattempt],,,
 #ifdef HAVE_NETINET_TCP_H
 #include <netinet/tcp.h>
 #endif
+#ifdef __OpenBSD__
+#include <netinet/tcp_timer.h>
+#endif
 #ifdef HAVE_NETINET_TCP_VAR_H
 #include <netinet/tcp_var.h>
 #endif])
@@ -3351,6 +3405,7 @@ WINE_CONFIG_MAKEFILE(dlls/wineoss.drv)
 WINE_CONFIG_MAKEFILE(dlls/wineps.drv)
 WINE_CONFIG_MAKEFILE(dlls/wineps16.drv16)
 WINE_CONFIG_MAKEFILE(dlls/winepulse.drv)
+WINE_CONFIG_MAKEFILE(dlls/winesndio.drv)
 WINE_CONFIG_MAKEFILE(dlls/wineusb.sys)
 WINE_CONFIG_MAKEFILE(dlls/winevulkan)
 WINE_CONFIG_MAKEFILE(dlls/winewayland.drv)
