cwindowsopensslcygwin

Program Fails to Run After Adding OpenSSL Code (SSL_library_init)


I compile for WIndows10, using gcc for Cygwin64 openssl is installed through cygwin's installer Add SSL_library_init();

Before and after the code and compile and run results compared:

test.c

#include <openssl/ssl.h>
#include <stdio.h>

void main()
{
    printf("%s","test");
    // SSL_library_init();

}

test2.c

#include <openssl/ssl.h>
#include <stdio.h>

void main()
{
    printf("%s","test");
    SSL_library_init();

}

run test.c:

PS C:\code_c> gcc -v -o test .\test.c -lssl -lcrypto
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0/configure --srcdir=/mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/mingw64-x86_64-gcc --htmldir=/usr/share/doc/mingw64-x86_64-gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-w64-mingw32 --without-libiconv-prefix --without-libintl-prefix --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --with-build-sysroot=/usr/x86_64-w64-mingw32/sys-root --disable-multilib --disable-win32-registry --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-fully-dynamic-string --enable-graphite --enable-libgomp --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-version-specific-runtime-libs --enable-libgomp --enable-libada --with-dwarf2 --with-gcc-major-version-only --with-gnu-ld --with-gnu-as --with-tune=generic --with-cloog-include=/usr/include/cloog-isl --with-system-zlib --enable-threads=posix --libexecdir=/usr/lib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.4.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'test.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test-'
 /usr/lib/gcc/x86_64-w64-mingw32/12/cc1.exe -quiet -v -D_REENTRANT .\test.c -quiet -dumpdir test- -dumpbase .\test.c -dumpbase-ext .c -mtune=generic -march=x86-64 -version -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccSpZwNu.s
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/x86_64-w64-mingw32/sys-root/usr/local/include"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-w64-mingw32/12/include
 /usr/lib/gcc/x86_64-w64-mingw32/12/include-fixed
 /usr/x86_64-w64-mingw32/sys-root/mingw/include
End of search list.
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

Compiler executable checksum: c58d452fc50d2e54c4f03a2b00330126
 /usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/as.exe -v -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccC6I3Xt.o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccSpZwNu.s
GNU assembler version 2.43.1 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.43.1
COMPILER_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'test.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test.'
 /usr/lib/gcc/x86_64-w64-mingw32/12/collect2.exe -plugin /usr/lib/gcc/x86_64-w64-mingw32/12/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe -plugin-opt=-fresolution=/cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccdL68fZ.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 --sysroot=/usr/x86_64-w64-mingw32/sys-root -m i386pep -Bdynamic -o test.exe /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/12 -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccC6I3Xt.o -lssl -lcrypto -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/default-manifest.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtend.o
COLLECT_GCC_OPTIONS='-v' '-o' 'test.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test.'
PS C:\code_c> ./test.exe
test
PS C:\code_c>

run test2.c

PS C:\code_c> gcc -v -o test2.exe .\test2.c -lssl -lcrypto
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0/configure --srcdir=/mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/mingw64-x86_64-gcc --htmldir=/usr/share/doc/mingw64-x86_64-gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-w64-mingw32 --without-libiconv-prefix --without-libintl-prefix --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --with-build-sysroot=/usr/x86_64-w64-mingw32/sys-root --disable-multilib --disable-win32-registry --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-fully-dynamic-string --enable-graphite --enable-libgomp --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-version-specific-runtime-libs --enable-libgomp --enable-libada --with-dwarf2 --with-gcc-major-version-only --with-gnu-ld --with-gnu-as --with-tune=generic --with-cloog-include=/usr/include/cloog-isl --with-system-zlib --enable-threads=posix --libexecdir=/usr/lib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.4.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2-'
 /usr/lib/gcc/x86_64-w64-mingw32/12/cc1.exe -quiet -v -D_REENTRANT .\test2.c -quiet -dumpdir test2- -dumpbase .\test2.c -dumpbase-ext .c -mtune=generic -march=x86-64 -version -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cccgzwcS.s
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/x86_64-w64-mingw32/sys-root/usr/local/include"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-w64-mingw32/12/include
 /usr/lib/gcc/x86_64-w64-mingw32/12/include-fixed
 /usr/x86_64-w64-mingw32/sys-root/mingw/include
End of search list.
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

Compiler executable checksum: c58d452fc50d2e54c4f03a2b00330126
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2-'
 /usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/as.exe -v -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cc7nbWjK.o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cccgzwcS.s
GNU assembler version 2.43.1 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.43.1
COMPILER_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2.'
 /usr/lib/gcc/x86_64-w64-mingw32/12/collect2.exe -plugin /usr/lib/gcc/x86_64-w64-mingw32/12/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe -plugin-opt=-fresolution=/cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccmPwAbT.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 --sysroot=/usr/x86_64-w64-mingw32/sys-root -m i386pep -Bdynamic -o test2.exe /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/12 -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cc7nbWjK.o -lssl -lcrypto -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/default-manifest.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtend.o
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2.'
PS C:\code_c> .\test2.exe
PS C:\code_c>

Why? Compilation looks okay

I run the same command in kali of WSL test2 is able to print out the test as normal(Not cross-compile)


Solution

  • Noticed you're using MinGW's GCC, so I installed the same OpenSSL flavor package (mingw64-x86_64-openssl). I had to do it anyway, as Cygwin's "standard" OpenSSL doesn't have the development stuff (header files, libs).

    main00.c

    #include <stdio.h>
    #include <openssl/ssl.h>
    
    
    int main()
    {
        printf("%s\n", OPENSSL_VERSION_TEXT);
        SSL_library_init();
        printf("\nDone.\n\n");
        return 0;
    }
    

    Output (Cygwin's MinTTY):

    [cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackExchange/StackOverflow/q079241224]> ~/sopr.sh
    ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
    
    [064bit prompt]> ls
    main00.c
    [064bit prompt]>
    [064bit prompt]> # Not relying on any environment variables (working with full paths), so I know for sure what I'm calling and from where
    [064bit prompt]> x86_64-w64-mingw32-gcc -o main00_pc064.exe main00.c -I/usr/x86_64-w64-mingw32/sys-root/mingw/include -L /usr/x86_64-w64-mingw32/sys-root/mingw/lib64 -lssl
    [064bit prompt]>
    [064bit prompt]> ls
    main00.c  main00_pc064.exe
    [064bit prompt]>
    [064bit prompt]> ./main00_pc064.exe
    [064bit prompt]> # Same behavior as yours
    [064bit prompt]> echo $?
    127
    [064bit prompt]>
    [064bit prompt]> ldd ./main00_pc064.exe
            ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffc58ff0000)
            KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffc57ba0000)
            KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffc56940000)
            msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffc58230000)
            libssl-3-x64.dll => not found
    [064bit prompt]> # Look at last line (dependency)
    [064bit prompt]>
    [064bit prompt]> export _PATH=${PATH}
    [064bit prompt]> export PATH=${PATH}:/usr/x86_64-w64-mingw32/sys-root/mingw/bin
    [064bit prompt]> ldd ./main00_pc064.exe
            ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffc58ff0000)
            KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffc57ba0000)
            KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffc56940000)
            msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffc58230000)
            libssl-3-x64.dll => /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libssl-3-x64.dll (0x7ffc23ad0000)
            libcrypto-3-x64.dll => /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libcrypto-3-x64.dll (0x7ffc1cd40000)
            ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ffc57ae0000)
            sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ffc587e0000)
            RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ffc57010000)
            bcrypt.dll => /cygdrive/c/WINDOWS/System32/bcrypt.dll (0x7ffc56790000)
            USER32.dll => /cygdrive/c/WINDOWS/System32/USER32.dll (0x7ffc58340000)
            win32u.dll => /cygdrive/c/WINDOWS/System32/win32u.dll (0x7ffc567c0000)
            GDI32.dll => /cygdrive/c/WINDOWS/System32/GDI32.dll (0x7ffc58700000)
            gdi32full.dll => /cygdrive/c/WINDOWS/System32/gdi32full.dll (0x7ffc56ea0000)
            msvcp_win.dll => /cygdrive/c/WINDOWS/System32/msvcp_win.dll (0x7ffc567f0000)
            ucrtbase.dll => /cygdrive/c/WINDOWS/System32/ucrtbase.dll (0x7ffc56690000)
            WS2_32.dll => /cygdrive/c/WINDOWS/System32/WS2_32.dll (0x7ffc582d0000)
            zlib1.dll => /usr/x86_64-w64-mingw32/sys-root/mingw/bin/zlib1.dll (0x7ffc38c20000)
    [064bit prompt]>
    [064bit prompt]> # Run again
    [064bit prompt]> ./main00_pc064.exe
    OpenSSL 3.0.15 3 Sep 2024
    
    Done.
    

    So that was it, apparently Cygwin is dumb enough to silently crash and not let the user know that the loader couldn't find an executable's dependency (could it be because it's built for plain Win (doesn't depend on cygwin1.dll)?).

    Since it's built by MinGW, it should run outside Cygwin. Here's the output from a Cmd terminal:

    [cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q079241224]> sopr.bat
    ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
    
    [prompt]>
    [prompt]> main00_pc064.exe
    
    [prompt]> :: This time a dialog popped up stating: "The code execution cannot proceed because libssl-3-x64.dll was not found. Reinstalling the program may fix this problem."
    [prompt]> set _PATH=%PATH%
    
    [prompt]> set PATH=%PATH%;c:\Install\pc064\Cygwin\Cygwin\Version\usr\x86_64-w64-mingw32\sys-root\mingw\bin
    
    [prompt]>
    [prompt]> main00_pc064.exe
    OpenSSL 3.0.15 3 Sep 2024
    
    Done.
    

    Regarding your 1st program, it runs because it doesn't link to OpenSSL libraries, as it doesn't use anything from them (it would still compile if you didn't specify them).

    Might be interesting to read: