Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go build with another glibc

I have installed another version of GLIBC and want to compile Golang code against this new GLIBC.

I have tried the following command for dynamic compilation:

go build --ldflags '-linkmode external -L /path/to/another_glibc/

But when I run ldd "go_executable", it still shows linked to default glibc.

Output:

linux-vdso.so.1 =>  (0x00007fff29da7000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f128a93c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f128ad06000)

Expected Output:

linux-vdso.so.1 =>  (0x00007fff45fa7000)
libc.so.6 => /another_glibc/lib/libc.so.6 (0x00007f5cd2067000)
/another_glibc/ld-2.29.so => /lib64/ld-linux-x86-64.so.2 (0x00007f5cd2420000)

What is missing here?

like image 829
bitbyter Avatar asked Apr 01 '19 07:04

bitbyter


People also ask

Can I have multiple glibc versions?

So, in a way, we can have multiple versions of glibc on our machine and have a libglib-2.0.so link to a specific version. The linker will look for the soname field in the shared object and embed it in the resulting binary. The soname field specifies the filename for our target shared library.

Does go need glibc?

Go does not require any C libraries if that's what you're asking. JimB, so Go-devs reimplemented all low-level stuff?

Does Go link to libc?

The Go linker never links directly against the C library. This normally works because the Go build decides that getuid and friends come from the libc.so shared C library.

Does gcc depend on glibc?

Is it possible to compile a program using gcc without depending on glibc? Yes, there are alternative libc versions, such as Musl, uClibc, dietLibc, etc. See their documentation on how to use them. Your problem does not appear to be a GLIBC dependency, but rather a mismatch between your build and your target hosts.


2 Answers

Before doing go build Set

CGO_LDFLAGS

Dynamic:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib"

Static:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib -static"

CGO_LDFLAGS lets you set GCC-like ld flags for Go.

like image 74
bitbyter Avatar answered Sep 20 '22 19:09

bitbyter


bitbyter's answer is not correct for the dynamic case because it requires that the system dynamic linker is compatible with the non-system glibc, which is unlikely. You can set the dynamic linker like this:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib64"
CGO_LDFLAGS="$CGO_LDFLAGS -Xlinker --dynamic-linker="/path/to/another_glibc/lib64/ld-linux-x86-64.so.2"

The dynamic linker name is specific to the architecture, so you have to research its name.

like image 37
Florian Weimer Avatar answered Sep 23 '22 19:09

Florian Weimer