Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build as a c Library, but faile #30

Open
szl-926929 opened this issue Dec 8, 2020 · 6 comments
Open

build as a c Library, but faile #30

szl-926929 opened this issue Dec 8, 2020 · 6 comments

Comments

@szl-926929
Copy link

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
Copy link

fuyao-w commented Feb 28, 2022

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/[email protected]/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/[email protected]/libzstd_linux_amd64.a(fastcover.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/[email protected]/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
Copy link

aexvir commented Jun 1, 2022

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/[email protected]/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/[email protected]/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
Copy link

aexvir commented Jun 1, 2022

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
Copy link

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
Copy link

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

@HoChihchou
Copy link

me too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants