使用Yesod和Darcs库时如何解决重复符号错误?



由于链接器问题,似乎不可能将Yesod与Darcs库一起使用。我追踪了这个问题,需要熟悉 Darcs 内部的人提供提示来解决这个问题。

在Yesod应用程序中使用darcs库时,出现以下错误:

GHCi runtime linker: fatal error: I found a duplicate definition for symbol
sha256_init
whilst processing object file
/home/sebfisch/.cabal/lib/darcs-2.9.5/ghc-7.4.2/libHSdarcs-2.9.5.a
This could be caused by:
* Loading two different object files which export the same symbol
* Specifying the same object file twice on the GHCi command line
* An incorrect `package.conf' entry, causing some object to be
loaded twice.
GHCi cannot safely continue in this situation.  Exiting now.  Sorry.

它似乎是由 darcs 和加密哈希库公开相同的符号引起的,因为搜索相应的对象文件显示:

# for file in `find ~/.cabal/lib/ -name "*.a"`; do (readelf -s $file | grep -i sha256_init) && (echo $file; echo); done
293: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND sha256_init
17: 0000000000000690    94 FUNC    GLOBAL DEFAULT    1 sha256_init
~/.cabal/lib/cryptohash-0.7.5/ghc-7.4.2/libHScryptohash-0.7.5.a
10: 0000000000000290    45 FUNC    GLOBAL DEFAULT    1 sha256_init
~/.cabal/lib/darcs-2.8.2/ghc-7.4.2/libHSdarcs-2.8.2.a

我编写了一个测试程序来确认 darcs 和 cryptohash 库是冲突的:

import           Crypt.SHA256          (sha256sum)
import           Crypto.Hash.SHA256    (hash)
import           Data.ByteString       (empty)
import qualified Data.ByteString.Char8 as BS
main :: IO ()
main = do
BS.putStrLn $ hash empty    -- cryptohash
putStrLn $ sha256sum empty  -- darcs

它无法编译并出现类似的错误:

/home/sebfisch/.cabal/lib/cryptohash-0.7.5/ghc-7.4.2/libHScryptohash-0.7.5.a(sha256.o): In function `sha256_update': sha256.c:(.text+0x4b0): multiple definition of `sha256_update'
/home/sebfisch/.cabal/lib/darcs-2.8.2/ghc-7.4.2/libHSdarcs-2.8.2.a(sha2.o):sha2.c:(.text+0xf90): first defined here
/home/sebfisch/.cabal/lib/cryptohash-0.7.5/ghc-7.4.2/libHScryptohash-0.7.5.a(sha256.o): In function `sha224_update': sha256.c:(.text+0x640): multiple definition of `sha224_update'
/home/sebfisch/.cabal/lib/darcs-2.8.2/ghc-7.4.2/libHSdarcs-2.8.2.a(sha2.o):sha2.c:(.text+0xbb0): first defined here
/home/sebfisch/.cabal/lib/cryptohash-0.7.5/ghc-7.4.2/libHScryptohash-0.7.5.a(sha256.o): In function `sha256_init': sha256.c:(.text+0x690): multiple definition of `sha256_init'
/home/sebfisch/.cabal/lib/darcs-2.8.2/ghc-7.4.2/libHSdarcs-2.8.2.a(sha2.o):sha2.c (.text+0x290): first defined here
/home/sebfisch/.cabal/lib/cryptohash-0.7.5/ghc-7.4.2/libHScryptohash-0.7.5.a(sha256.o): In function `sha224_init': sha256.c:(.text+0x6f0): multiple definition of `sha224_init'
/home/sebfisch/.cabal/lib/darcs-2.8.2/ghc-7.4.2/libHSdarcs-2.8.2.a(sha2.o):sha2.c (.text+0x620): first defined here
collect2: ld returned 1 exit status

cryptohash 库是 yesod-static 所必需的,在编写 Yesod 应用程序时不容易避免。如何在同一应用程序中使用 Yesod 和 Darcs(作为库)?

从一个库中删除重复的符号会有帮助吗?两个软件包都通过 FFI 访问哈希函数,但使用不同的文件。

来自darcs/Crypt.SHA256:

foreign import ccall unsafe "sha2.h sha256" c_sha256
:: Ptr CChar -> CSize -> Ptr Word8 -> IO ()

来自cryptohash/Crypto.Hash.SHA256:

foreign import ccall unsafe "sha256.h sha256_init"
c_sha256_init :: Ptr Ctx -> IO ()
foreign import ccall "sha256.h sha256_update"
c_sha256_update :: Ptr Ctx -> CString -> Word32 -> IO ()
foreign import ccall unsafe "sha256.h sha256_finalize"
c_sha256_finalize :: Ptr Ctx -> CString -> IO ()

另一个想法是重写 Darcs 以不使用自己的哈希函数。如何重新实现 Darcs 的SHA256模块以使用加密哈希?我的测试程序的main函数中的两个语句没有给出相同的输出(通过注释掉另一个语句进行测试),因此在 Darcs 中使用 cryptohash 似乎并不完全简单。

darcs 哈希输出只是加密哈希输出的 base16 编码版本。看起来base16字节是弥合这一差距的一种方法。我试过了,Crypt.SHA256变得像:

module Crypt.SHA256 ( sha256sum ) where
import Crypto.Hash.SHA256 ( hash )
import Data.ByteString ( ByteString )
import Data.ByteString.Base16 ( encode )
import Data.ByteString.Char8 ( unpack )
sha256sum :: ByteString -> String
sha256sum = unpack . encode . hash

事实上,散列存储包也有sha2.c的副本,并通过重命名符号来修复问题。因此,darcs 2.8 最简单的快速修复方法是从散列存储中复制 sha2.h 和 sha2.c,将两个文件中的hashed_storage_替换为darcs_,并将 darcs 中 src/Crypt/SHA256.hs 中的 FFI 导入更改为:

foreign import ccall unsafe "sha2.h darcs_sha256" c_sha256
:: Ptr CChar -> CSize -> Ptr Word8 -> IO ()

如果对您有所帮助,我很乐意发布带有此更改的 darcs 2.8.3。对于 2.10,我将切换到使用上面的加密哈希,因为我认为没有任何理由继续使用本地 C 版本,并且通常在 darcss 中,我们正试图摆脱通用代码的私有实现。

编辑:我最初认为哈希存储会有同样的问题,但我错了(回想起来,如果不是因为重命名,很明显它会与 darcs 本身发生冲突)。

最新更新