valyala / gozstd

go wrapper for zstd
MIT License
420 stars 60 forks source link

build as a c Library, but faile #30

Open szl-926929 opened 3 years ago

szl-926929 commented 3 years ago

os: centos 7 want to compile into a C library with CGo, but get error: /usr/lib/golang/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /bin/ld: /home/lcsuper/kong-zstd-go/vendor/github.com/valyala/gozstd/libzstd_linux_amd64.a(zdict.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /bin/ld: final link failed: Bad value collect2: error: ld returned 1 exit status

fuyao-w commented 2 years ago

ME TOO

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /bin/ld: /home/xxxx/go/pkg/mod/github.com/valyala/gozstd@v1.11.0/libzstd_linux_amd64.a(zdict.o): relocation R_X86_64_PC32 against symbol stderr@@GLIBC_2.2.5' can not be used when making a PDE object; recompile with -fPIC /bin/ld: /home/xxxx/go/pkg/mod/github.com/valyala/gozstd@v1.11.0/libzstd_linux_amd64.a(fastcover.o): relocation R_X86_64_PC32 against symbolstderr@@GLIBC_2.2.5' can not be used when making a PDE object; recompile with -fPIC /bin/ld: /home/xxxx/go/pkg/mod/github.com/valyala/gozstd@v1.11.0/libzstd_linux_amd64.a(cover.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a PDE object; recompile with -fPIC /bin/ld: final link failed: nonrepresentable section on output collect2: error: ld returned 1 exit status

aexvir commented 2 years ago

also hitting this issue, only reproducible on linux, works fine on darwin, both arm64 and amd64

/usr/bin/ld: /go/pkg/mod/github.com/valyala/gozstd@v1.17.0/libzstd_linux_amd64.a(cover.o): warning: relocation against `stderr@@GLIBC_2.2.5' in read-only section `.text'
/usr/bin/ld: /go/pkg/mod/github.com/valyala/gozstd@v1.17.0/libzstd_linux_amd64.a(zdict.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
clang: error: linker command failed with exit code 1 (use -v to see invocation)

easy to reproduce with just

package main

// #cgo CFLAGS: -fPIC
import "C"
import "github.com/valyala/gozstd"

//export Compress
func Compress(msg *C.char) *C.char {
    input := []byte(C.GoString(msg))
    output := gozstd.Compress(nil, input)
    return C.CString(string(output))
}

func main() {}

then

go build -o zstd.so -buildmode=c-shared .

note: the cgo directive doesn't work, I just added as the error message suggested, but it makes no difference

aexvir commented 2 years ago

alright, issue found, the c archive for linux amd64 wasn't built with -fPIC as the gcc error says as a workaround you can clone this repo, rebuild the .a file for your platform, and go mod edit -replace the dependency on your project with the cloned one

example

git clone https://github.com/valyala/gozstd.git ../gozstd
cd ../gozstd
git checkout v1.17.0 # or whatever release you want # could be parsed from go.mod
MOREFLAGS=-fPIC make clean libzstd.a
cd - # wherever your package is
go mod edit -replace github.com/valyala/gozstd=../gozstd

now the build works like a charm 🙂


now... is this a bug when the .a files where generated and committed to this repo? or is there some intention behind the lack of the -fPIC flag? I'd lie if I'd say that I fully understand how all this works, so I'm not sure what's the best way to address this issue

@valyala sorry for the tag, but it would be really nice to get your input I guess that a pr from me updating the .a file won't be accepted, right? I guess there wouldn't be any way of verifying that it hasn't been tampered with, or it doesn't introduce some issue for other users

fuergaosi233 commented 1 year ago

alright, issue found, the c archive for linux amd64 wasn't built with -fPIC as the gcc error says as a workaround you can clone this repo, rebuild the .a file for your platform, and go mod edit -replace the dependency on your project with the cloned one

example

git clone https://github.com/valyala/gozstd.git ../gozstd
cd ../gozstd
git checkout v1.17.0 # or whatever release you want # could be parsed from go.mod
MOREFLAGS=-fPIC make clean libzstd.a
cd - # wherever your package is
go mod edit -replace github.com/valyala/gozstd=../gozstd

now the build works like a charm 🙂

now... is this a bug when the .a files where generated and committed to this repo? or is there some intention behind the lack of the -fPIC flag? I'd lie if I'd say that I fully understand how all this works, so I'm not sure what's the best way to address this issue

@valyala sorry for the tag, but it would be really nice to get your input I guess that a pr from me updating the .a file won't be accepted, right? I guess there wouldn't be any way of verifying that it hasn't been tampered with, or it doesn't introduce some issue for other users

I got this error when I want to execute this command

Cleaning library completed
CC obj/conf_6d8c1dc6b1841212cc254358d8403b5f/static/debug.o
cc: internal compiler error: Segmentation fault (program as)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-6/README.Bugs> for instructions.
Makefile:210: recipe for target 'obj/conf_6d8c1dc6b1841212cc254358d8403b5f/static/debug.o' failed
make[2]: *** [obj/conf_6d8c1dc6b1841212cc254358d8403b5f/static/debug.o] Error 4
Makefile:107: recipe for target 'libzstd.a' failed
make[1]: *** [libzstd.a] Error 2
make[1]: Leaving directory '/data00/home/gaopengyang/project/github/gozstd/zstd/lib'
Makefile:15: recipe for target 'libzstd_linux_amd64.a' failed
make: *** [libzstd_linux_amd64.a] Error 2

My go version: 1.18 Gcc Version: gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)

fuergaosi233 commented 1 year ago

alright, issue found, the c archive for linux amd64 wasn't built with -fPIC as the gcc error says as a workaround you can clone this repo, rebuild the .a file for your platform, and go mod edit -replace the dependency on your project with the cloned one example

git clone https://github.com/valyala/gozstd.git ../gozstd
cd ../gozstd
git checkout v1.17.0 # or whatever release you want # could be parsed from go.mod
MOREFLAGS=-fPIC make clean libzstd.a
cd - # wherever your package is
go mod edit -replace github.com/valyala/gozstd=../gozstd

now the build works like a charm 🙂 now... is this a bug when the .a files where generated and committed to this repo? or is there some intention behind the lack of the -fPIC flag? I'd lie if I'd say that I fully understand how all this works, so I'm not sure what's the best way to address this issue @valyala sorry for the tag, but it would be really nice to get your input I guess that a pr from me updating the .a file won't be accepted, right? I guess there wouldn't be any way of verifying that it hasn't been tampered with, or it doesn't introduce some issue for other users

I got this error when I want to execute this command

Cleaning library completed
CC obj/conf_6d8c1dc6b1841212cc254358d8403b5f/static/debug.o
cc: internal compiler error: Segmentation fault (program as)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-6/README.Bugs> for instructions.
Makefile:210: recipe for target 'obj/conf_6d8c1dc6b1841212cc254358d8403b5f/static/debug.o' failed
make[2]: *** [obj/conf_6d8c1dc6b1841212cc254358d8403b5f/static/debug.o] Error 4
Makefile:107: recipe for target 'libzstd.a' failed
make[1]: *** [libzstd.a] Error 2
make[1]: Leaving directory '/data00/home/gaopengyang/project/github/gozstd/zstd/lib'
Makefile:15: recipe for target 'libzstd_linux_amd64.a' failed
make: *** [libzstd_linux_amd64.a] Error 2

My go version: 1.18 Gcc Version: gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)

There is a problem with my GCC. I will fix it after reinstalling OS