haskell-cryptography / HsOpenSSL

OpenSSL binding for Haskell
http://hackage.haskell.org/package/HsOpenSSL
Creative Commons Zero v1.0 Universal
22 stars 33 forks source link

Building on MacOS for x86_64 architecture #77

Open bapcyk opened 1 year ago

bapcyk commented 1 year ago

I am trying to use the library on arm-based MacOS which support both architectures (arm64, x86_64), it's so called "M1":

$ sw_vers
ProductName:            macOS
ProductVersion:         13.0.1
BuildVersion:           22A400
$ arch
arm64

and when I compile my application using the library for arm64 architecture - no problem (but it requires sudo ln -sf /opt/homebrew/Cellar/openssl@3/3.0.7 /usr/local/opt/openssl).

But when I try to switch to x86_64, I get an error like:

dlopen(/Xxx/.stack/snapshots/x86_64-osx/b1...50/9.2.5/lib/x86_64-osx-ghc-9.2.5/libHSHsOpenSSL-0.11.7.2-1JX1qBi8YfpGDjk1ra3OXq-ghc9.2.5.dylib, 0x0005): symbol not found in flat namespace '_DSA_free'

I do it by:

stack --resolver lts-20.4 --arch x86_64 build

and installed openssl with brew arch -x86_64 ./brew install openssl@3 from the folder (/usr/local/homebrew) where I installed Homebrew for x86_64.

I tried flags as well, but it does not help.

Not sure, why it happens, maybe it tries to use arm64 library. Any help is appreciated.

bapcyk commented 1 year ago

After some research I realized that errors like:

<command line>: dlopen(/XXXXX/.stack/snapshots/x86_64-osx/b1d675598b9b6c5f516e03f82c45d01becd6003e6128005b2b4acb8628b0f350/9.2.5/lib/x86_64-osx-ghc-9.2.5/libHSHsOpenSSL-0.11.7.2-1JX1qBi8YfpGDjk1ra3OXq-ghc9.2.5.dylib, 0x0005): symbol not found in flat namespace '_DSA_free'

happens during compilation of files containing things related to templates, like $(embedDir ...) and similar and in Lens related things. For example, it happens during compilation of a module like this one:

{-# LANGUAGE FlexibleInstances      #-}
{-# LANGUAGE FunctionalDependencies #-}

module MyUtilities.Lens where

import Control.Lens ( makePrisms, makeFieldsNoPrefix )
import MyUtilities.Network ( Error, NetData )

makePrisms ''Error
makeFieldsNoPrefix ''NetData

It looks like a template Haskell tries to link HsOpenSSL but it does not know folders where are openssl's .dylib files. I added them into a sub-folder of the project and set extra-lib-dirs:

package.yaml:

...
extra-lib-dirs:
  - libs/ssl
...

but it does not help.

PS. openssl libs are in /usr/local/opt/openssl/lib also (as the HsOpenSSL cabal's expects) - I have them in libs/ssl and in /usr/local/opt/openssl/lib - the same libs for x86_64.

Any suggestions?

bapcyk commented 1 year ago

The problem file libHSHsOpenSSL-0.11.7.2-1JX1qBi8YfpGDjk1ra3OXq-ghc9.2.5.dylib (it's in .stack snapshot dir for x86_64) depends on some libraries, but libcrypto is not among them (libcrypto is the library exporting problem symbol _DSA_free). And it imports some symbols, one of them is _DSA_free. No idea is it OK, it looks strange

dyld_info -dependents -imports /Xxxxx/.stack/snapshots/x86_64-osx/b1d675598b9b6c5f516e03f82c45d01becd6003e6128005b2b4acb8628b0f350/9.2.5/lib/x86_64-osx-ghc-9.2.5/libHSHsOpenSSL-0.11.7.2-1JX1qBi8YfpGDjk1ra3OXq-ghc9.2.5.dylib
/Xxxxx/.stack/snapshots/x86_64-osx/b1d675598b9b6c5f516e03f82c45d01becd6003e6128005b2b4acb8628b0f350/9.2.5/lib/x86_64-osx-ghc-9.2.5/libHSHsOpenSSL-0.11.7.2-1JX1qBi8YfpGDjk1ra3OXq-ghc9.2.5.dylib [x86_64]:
    -dependents:
        attributes     load path
                       @rpath/libHSnetwork-3.1.2.7-L5OzboM5UdW3uPk8hB4dqj-ghc9.2.5.dylib
                       @rpath/libHStime-1.11.1.1-ghc9.2.5.dylib
                       @rpath/libHSbytestring-0.11.3.1-ghc9.2.5.dylib
                       @rpath/libHSbase-4.16.4.0-ghc9.2.5.dylib
                       @rpath/libHSghc-bignum-1.2-ghc9.2.5.dylib
                       @rpath/libHSghc-prim-0.8.0-ghc9.2.5.dylib
                       /usr/lib/libSystem.B.dylib
0x0000  0xFFFFFFFE  _DSA_free
0x0001  0xFFFFFFFE  _EVP_CIPHER_CTX_free
0x0002  0xFFFFFFFE  _EVP_MD_CTX_free
0x0003  0xFFFFFFFE  _EVP_PKEY_free

The similar file but in the snapshot for ARM64 platform:

dyld_info -dependents -imports libHSHsOpenSSL-0.11.7.2-1JX1qBi8YfpGDjk1ra3OXq-ghc9.2.5.dylib
libHSHsOpenSSL-0.11.7.2-1JX1qBi8YfpGDjk1ra3OXq-ghc9.2.5.dylib [arm64]:
    -dependents:
        attributes     load path
                       /opt/homebrew/opt/openssl@3/lib/libssl.3.dylib
                       /opt/homebrew/opt/openssl@3/lib/libcrypto.3.dylib
                       @rpath/libHSnetwork-3.1.2.7-L5OzboM5UdW3uPk8hB4dqj-ghc9.2.5.dylib
                       @rpath/libHStime-1.11.1.1-ghc9.2.5.dylib
                       @rpath/libHSbytestring-0.11.3.1-ghc9.2.5.dylib
                       @rpath/libHSbase-4.16.4.0-ghc9.2.5.dylib
                       @rpath/libHSghc-bignum-1.2-ghc9.2.5.dylib
                       @rpath/libHSghc-prim-0.8.0-ghc9.2.5.dylib
                       /usr/lib/libSystem.B.dylib
0x0000  0xFFFFFFFE  _stable_ptr_table
0x0001  0xFFFFFFFE  _stg_CHARLIKE_closure
0x0002  0xFFFFFFFE  _stg_INTLIKE_closure
0x0003  0xFFFFFFFE  _stg_INTLIKE_closure+0xCB1
0x0004  0xFFFFFFFE  _stg_INTLIKE_closure+0x301
0x0005  0xFFFFFFFE  _stg_INTLIKE_closure+0xCB1

imports explicitly libssl and libcrypto and no _DSA_free symbol (and Co) in imports section output.

And the same files examined with otool -l ...

for ARM64:

...
Load command 11
          cmd LC_LOAD_DYLIB
      cmdsize 72
         name /opt/homebrew/opt/openssl@3/lib/libssl.3.dylib (offset 24)
   time stamp 2 Thu Jan  1 02:00:02 1970
      current version 3.0.0
compatibility version 3.0.0
Load command 12
          cmd LC_LOAD_DYLIB
      cmdsize 80
         name /opt/homebrew/opt/openssl@3/lib/libcrypto.3.dylib (offset 24)
   time stamp 2 Thu Jan  1 02:00:02 1970
      current version 3.0.0
compatibility version 3.0.0
Load command 13
          cmd LC_LOAD_DYL...
...

ie, it tries to load ssl, crypto lib, but for x86_64 nothing related to these libraries.