--- openssl-3.6.0/crypto/build.info.orig	2025-10-07 08:55:19.738440900 +0200
+++ openssl-3.6.0/crypto/build.info	2025-10-07 08:56:41.554349700 +0200
@@ -107,7 +107,7 @@
         comp_methods.c cversion.c info.c cpt_err.c ebcdic.c uid.c o_time.c \
         o_dir.c o_fopen.c getenv.c o_init.c init.c trace.c provider.c \
         provider_child.c punycode.c passphrase.c sleep.c \
-        quic_vlint.c time.c defaults.c ssl_err.c
+        quic_vlint.c time.c defaults.c ssl_err.c pathtools.c
 SOURCE[../providers/libfips.a]=$UTIL_COMMON
 
 SOURCE[../libcrypto]=$UPLINKSRC
diff --git a/crypto/defaults.c b/crypto/defaults.c
index 727781b..1d8cc05 100644
--- a/crypto/defaults.c
+++ b/crypto/defaults.c
@@ -12,6 +12,7 @@
 #include "internal/thread_once.h"
 #include "internal/cryptlib.h"
 #include "internal/e_os.h"
+#include "pathtools.h"
 
 #if defined(_WIN32) && defined(OSSL_WINCTX)
 
@@ -139,6 +140,41 @@ DEFINE_RUN_ONCE_STATIC(do_defaults_setup)
 
     return 1;
 }
+#elif defined(__MINGW32__) && defined(OPENSSLBIN)
+/**
+ * @brief The directory where OpenSSL is installed.
+ */
+static char *openssldir = NULL;
+
+/**
+ * @brief The directory where OpenSSL engines are located.
+ */
+static char *enginesdir = NULL;
+
+/**
+ * @brief The directory where OpenSSL modules are located.
+ */
+static char *modulesdir = NULL;
+
+static void do_defaults_cleanup_msys2(void) {
+    free(openssldir);
+    free(enginesdir);
+    free(modulesdir);
+}
+
+static CRYPTO_ONCE defaults_setup_msys2_init = CRYPTO_ONCE_STATIC_INIT;
+
+/**
+ * @brief Function to relocate openssldir/modulesdir/enginesdir
+ */
+DEFINE_RUN_ONCE_STATIC(do_defaults_setup_msys2)
+{
+    openssldir = single_path_relocation_lib(OPENSSLBIN, OPENSSLDIR);
+    enginesdir = single_path_relocation_lib(OPENSSLBIN, ENGINESDIR);
+    modulesdir = single_path_relocation_lib(OPENSSLBIN, MODULESDIR);
+    OPENSSL_atexit(do_defaults_cleanup_msys2);
+    return 1;
+}
 #endif /* defined(_WIN32) && defined(OSSL_WINCTX) */
 
 /**
@@ -152,6 +188,10 @@ const char *ossl_get_openssldir(void)
     if (!RUN_ONCE(&defaults_setup_init, do_defaults_setup))
         return NULL;
     return (const char *)openssldirptr;
+#elif defined(__MINGW32__) && defined(OPENSSLBIN)
+    if (!RUN_ONCE(&defaults_setup_msys2_init, do_defaults_setup_msys2))
+        return NULL;
+    return (const char *)openssldir;
 # else
     return OPENSSLDIR;
 #endif
@@ -168,6 +208,10 @@ const char *ossl_get_enginesdir(void)
     if (!RUN_ONCE(&defaults_setup_init, do_defaults_setup))
         return NULL;
     return (const char *)enginesdirptr;
+#elif defined(__MINGW32__) && defined(OPENSSLBIN)
+    if (!RUN_ONCE(&defaults_setup_msys2_init, do_defaults_setup_msys2))
+        return NULL;
+    return (const char *)enginesdir;
 #else
     return ENGINESDIR;
 #endif
@@ -184,6 +228,10 @@ const char *ossl_get_modulesdir(void)
     if (!RUN_ONCE(&defaults_setup_init, do_defaults_setup))
         return NULL;
     return (const char *)modulesdirptr;
+#elif defined(__MINGW32__) && defined(OPENSSLBIN)
+    if (!RUN_ONCE(&defaults_setup_msys2_init, do_defaults_setup_msys2))
+        return NULL;
+    return (const char *)modulesdir;
 #else
     return MODULESDIR;
 #endif
