This may resolve https://wimlib.net/forums/viewtopic.php?t=669. The
reported crash happened in sha1_blocks_x86_avx_bmi2() on the first
instruction that writes to the stack with vmovqda. That suggests the
stack was misaligned. If this indeed fixes the bug, then it has
actually always existed but it got exposed by new code in v1.14.0.
#include <time.h>
#ifdef BUILDING_WIMLIB
#include <time.h>
#ifdef BUILDING_WIMLIB
+ /*
+ * On i386, gcc assumes that the stack is 16-byte aligned at function entry.
+ * However, some compilers (e.g. MSVC) and programming languages (e.g. Delphi)
+ * only guarantee 4-byte alignment when calling functions. This is mainly an
+ * issue on Windows, but it can occur on Linux too. Work around this ABI
+ * incompatibility by realigning the stack pointer when entering the library.
+ * This prevents crashes in SSE/AVX code.
+ */
+# if defined(__GNUC__) && defined(__i386__)
+# define WIMLIB_ALIGN_STACK __attribute__((force_align_arg_pointer))
+# else
+# define WIMLIB_ALIGN_STACK
+# endif
-# define WIMLIBAPI __declspec(dllexport)
+# define WIMLIBAPI __declspec(dllexport) WIMLIB_ALIGN_STACK
-# define WIMLIBAPI __attribute__((visibility("default")))
+# define WIMLIBAPI __attribute__((visibility("default"))) WIMLIB_ALIGN_STACK
# endif
#else
# define WIMLIBAPI
# endif
#else
# define WIMLIBAPI