创建共享库时,不能使用Haskell Stack静态二进制重定位R_X86_64_32相对于__TMC_END__ [英] Haskell Stack Static Binary relocation R_X86_64_32 against `__TMC_END__' can not be used when making a shared object

查看:406
本文介绍了创建共享库时,不能使用Haskell Stack静态二进制重定位R_X86_64_32相对于__TMC_END__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的stack.yaml中使用以下内容尝试通过堆栈编译静态二进制文件时:

When using the following in my stack.yaml to (attempt) to compile a static binary through Stack:

ghc-options:
    "*": -static -optc-static -optl-static -optl-pthread -fPIC

我收到此错误:

usr/bin/ld: /usr/lib/gcc/x86_64-amazon-linux/4.8.3/crtbeginT.o: relocation R_X86_64_32 against `__TMC_END__' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-amazon-linux/4.8.3/crtbeginT.o: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)

我正在使用启用了docker的堆栈和自定义的Amazon Linux映像来构建用于AWS Lambda的静态二进制文件.

I'm using stack with docker enabled and a customised Amazon Linux image for building a static binary for use with AWS Lambda.

我以前没有使用静态二进制编译的经验,因此对于此链接器错误我有些困惑.有什么想法吗?

I have no prior experience with static binary compilation, so I'm a bit stumped on this linker error. Any ideas please?

以下是使用-v的gcc参数:

Here's the gcc args using -v:

/usr/bin/gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE '-Wl,--hash-size=31' -Wl,--reduce-memory-overheads -Wl,--no-as-needed -Wl,-rpath<snipped LOADS> -lHSghc<SNIPPED LOADS> -lpq -lz -lrt -lutil -ldl -lgmp

关于问题的根本原因,

推荐答案

@Reid Barton是正确的.真正的问题是堆栈默认情况下正在构建一个共享二进制文件.通过传递选项-static或ghc-options:-optl-static,我们仅分别寻址ghc层和ld层.

@Reid Barton is right, regarding the root cause of the problem. The real issue here is that stack is building a shared binary by default. By passing the options -static or ghc-options: -optl-static we are only addressing respectively the ghc layer and ld layer.

stack
 |_ cabal
      |_ ghc
          |_ gcc
              |_ ld (linker)

解决此问题的正确方法是,在试验构建过程中,告诉cabal我们想要一个静态二进制文件.

The correct way to solve the issue is to tell cabal that we want a static binary, as it pilots the build.

sausade在reddit上提供了有关此操作的答案:您只需在可执行部分的.cabal文件中添加以下内容:

The answer on how to do that was provided by sausade on reddit: you just have to add the following to your .cabal file in the executable section:

directive:

`ld-options: -static`

这将向阴谋集团暗示我们想要一个静态二进制文件,因此它不会将-shared传递给ghc.

This will hint to cabal that we want a static binary and as so it will not pass -shared to ghc.

因此,一旦您在Cabal文件中添加了此指令,就只需运行:

So once you have added this directive in your cabal file, you just have to run:

stack install --ghc-options="-fPIC"

瞧瞧!

P.S:如果您的系统上没有静态库(例如Archlinux),则ld将退回到动态链接

P.S: If you don't have static libraries on your system (like with Archlinux) ld will fallback to dynamic linking

上一个答案:

这是一个古老的gcc问题,没有人愿意解决. https://bugs.launchpad.net/ubuntu/+source /gcc-4.4/+bug/640734

That's an old gcc issue that no one care to fix. https://bugs.launchpad.net/ubuntu/+source/gcc-4.4/+bug/640734

对于您的原始问题,这里有两个有用的链接向您展示如何实现:

For your original problem here are two useful links to show you how to do it:

  • https://gist.github.com/erebe/9611b2c8f1d26cc91cc3fb33bbae0eb9
  • https://www.fpcomplete.com/blog/2016/10/static-compilation-with-stack

这篇关于创建共享库时,不能使用Haskell Stack静态二进制重定位R_X86_64_32相对于__TMC_END__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆