登录
首页 >  Golang >  Go问答

在 Alpine 上运行 libstd.so 时出现错误

来源:stackoverflow

时间:2024-04-26 20:18:37 220浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《在 Alpine 上运行 libstd.so 时出现错误》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

我正在尝试使用共享库构建 go 可执行文件。在使用 gnu libc 的 ubuntu 中,它可以工作。但是,当我尝试在使用 musl libc 的 alpine(docker 映像 golang:alpine 或 1.14.1-alpine3.11)上使用相同的过程时,生成的 libstd.so 被破坏。之后,如果我尝试编译可执行文件,编译就会失败。

这是程序:

$ docker run -it golang:alpine sh
/go # apk add --update alpine-sdk
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/apkindex.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/apkindex.tar.gz
(1/38) installing fakeroot (1.24-r0)
(2/38) installing sudo (1.8.31-r0)
(3/38) installing libcap (2.27-r0)
(4/38) installing pax-utils (1.2.4-r0)
(5/38) installing openssl (1.1.1d-r3)
(6/38) installing libattr (2.4.48-r0)
(7/38) installing attr (2.4.48-r0)
(8/38) installing libacl (2.2.53-r0)
(9/38) installing tar (1.32-r1)
(10/38) installing pkgconf (1.6.3-r0)
(11/38) installing patch (2.7.6-r6)
(12/38) installing libgcc (9.2.0-r4)
(13/38) installing libstdc++ (9.2.0-r4)
(14/38) installing lzip (1.21-r0)
(15/38) installing nghttp2-libs (1.40.0-r0)
(16/38) installing libcurl (7.67.0-r0)
(17/38) installing curl (7.67.0-r0)
(18/38) installing abuild (3.5.0-r0)
executing abuild-3.5.0-r0.pre-install
(19/38) installing binutils (2.33.1-r0)
(20/38) installing libmagic (5.37-r1)
(21/38) installing file (5.37-r1)
(22/38) installing gmp (6.1.2-r1)
(23/38) installing isl (0.18-r0)
(24/38) installing libgomp (9.2.0-r4)
(25/38) installing libatomic (9.2.0-r4)
(26/38) installing mpfr4 (4.0.2-r1)
(27/38) installing mpc1 (1.1.0-r1)
(28/38) installing gcc (9.2.0-r4)
(29/38) installing musl-dev (1.1.24-r2)
(30/38) installing libc-dev (0.7.2-r0)
(31/38) installing g++ (9.2.0-r4)
(32/38) installing make (4.2.1-r2)
(33/38) installing fortify-headers (1.1-r0)
(34/38) installing build-base (0.5-r1)
(35/38) installing expat (2.2.9-r1)
(36/38) installing pcre2 (10.34-r1)
(37/38) installing git (2.24.1-r0)
(38/38) installing alpine-sdk (1.0-r0)
executing busybox-1.31.1-r9.trigger
ok: 196 mib in 53 packages
/go # go install -a -buildmode=shared -linkshared std
/go # ldd /usr/local/go/pkg/linux_amd64_dynlink/libstd.so 
    /lib/ld-musl-x86_64.so.1 (0x7f689af41000)
    libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f689af41000)
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_malloc: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_realloc: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_free: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: main.main: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_stack_end: symbol not found
/go # go version
go version go1.14.1 linux/amd64
/go # go env
go111module=""
goarch="amd64"
gobin=""
gocache="/root/.cache/go-build"
goenv="/root/.config/go/env"
goexe=""
goflags=""
gohostarch="amd64"
gohostos="linux"
goinsecure=""
gonoproxy=""
gonosumdb=""
goos="linux"
gopath="/go"
goprivate=""
goproxy="https://proxy.golang.org,direct"
goroot="/usr/local/go"
gosumdb="sum.golang.org"
gotmpdir=""
gotooldir="/usr/local/go/pkg/tool/linux_amd64"
gccgo="gccgo"
ar="ar"
cc="gcc"
cxx="g++"
cgo_enabled="1"
gomod=""
cgo_cflags="-g -o2"
cgo_cppflags=""
cgo_cxxflags="-g -o2"
cgo_fflags="-g -o2"
cgo_ldflags="-g -o2"
pkg_config="pkg-config"
gogccflags="-fpic -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build202809585=/tmp/go-build -gno-record-gcc-switches"
/go #

当我使用相同的 golang 版本执行相同的过程,但其他发行版具有 gnu libc 时,它会按预期工作:

$ docker run -it golang:buster sh
# go install -a -buildmode=shared -linkshared std
# ldd /usr/local/go/pkg/linux_amd64_dynlink/libstd.so
    linux-vdso.so.1 (0x00007ffe54bea000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f12f79a3000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f12f7982000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f12f77c1000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f12fa0d2000)
# go version
go version go1.14.1 linux/amd64
# go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build609530787=/tmp/go-build -gno-record-gcc-switches"
#

我是否遗漏了一些细节? golang 或 alpine 有 bug 吗?


解决方案


go 运行时似乎有一些显式的 gnu libc 依赖项,如链接错误所示:

error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_malloc: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_realloc: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_free: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: main.main: symbol not found
error relocating /usr/local/go/pkg/linux_amd64_dynlink/libstd.so: __libc_stack_end: symbol not found

轻量级 glibc 兼容性(安装 libc6-compat 软件包)在这种情况下可能不起作用,因为它主要添加存根,并且仍然缺少一些 glibc 功能。

尝试找到有问题的 go 模块,在 go install -a -buildmode=shared -linkshared std 之后在 go 构建缓存中选取(对象文件位于 .cache/go-build 下,使用 grep -r libc 查找 glibc 依赖项),一个大的目标文件脱颖而出。使用 nm 列出对象符号表明它是消耗所需符号的对象:

U __libc_free
        U __libc_malloc
        U __libc_realloc
        U __libc_stack_end

该对象结果是 race.go

google 搜索显示 alpine libc 兼容性确实是一个已知的竞赛问题,记录如下:
https://github.com/golang/go/issues/14481

幸运的是,该票证提到了几种建议的解决方法,例如从源代码构建编译器-rt。

理论要掌握,实操不能落!以上关于《在 Alpine 上运行 libstd.so 时出现错误》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>