Is it possible to link object files generated from the Go toolchain using clang linker (lld) ?

678 views
Skip to first unread message

Thor Odinson

unread,
Sep 7, 2021, 12:25:33 PM9/7/21
to golang-nuts
I've built an executable file using Go compiler, but the target environments loader expects the program sections to be in specific order and fails to execute.

As a result, I'm trying to compile the Go source using 'go tool compile' to generate object files, which I want to link using the clang linker option instead of 'go tool link'.

Tried building a archive/shared library using go buildmode option, but this includes lot of Go runtime in the built library. If I have to link this library along with other libraries using clang toolchain, this would not resolve the symbols as the statically built library (built using buildmode) has glibc dependency.

"
/usr/lib/llvm-5.0/bin/ld.lld: error: undefined symbol: stderr
>>> referenced by gcc_libinit.c:29
>>> 000006.o:(x_cgo_sys_thread_create) in archive ./sdlgotest.a
"

Is there a way to build Go runtime without glibc symbols, so that I can use the archive without relying on gcc toolchain ?
 

Ian Lance Taylor

unread,
Sep 7, 2021, 3:49:50 PM9/7/21
to Thor Odinson, golang-nuts
I'm not aware of any way to do that.

The Go compiler generates a Go-specific object file format, which the
clang linker does not understand. The normal way to handle this is to
use -buildmode=c-archve, which produces an object that the clang
linker does understand; however, as you noted, that assumes that the
Go code will be linked with C code and more generally with the C
library, which I guess you don't want to do.

I'm a bit surprised that the ordinary "go build" output doesn't work
for you, as the order of sections is the default one. What section
ordering do you need?

Ian

Thor Odinson

unread,
Sep 8, 2021, 7:19:26 AM9/8/21
to golang-nuts
Ian,

I'm bit confused with this,  

>  The normal way to handle this is to
> use -buildmode=c-archve, which produces an object that the clang
> linker does understand; however, as you noted, that assumes that the
> Go code will be linked with C code and more generally with the C
> library, which I guess you don't want to do.

Command used to build archive:
CC=clang  go build -buildmode=c-archive -o main.a main.go

I have tried with buildmode=c-archive and linking with C library only using clang toolchain.

Command used:
clang test-go.c main.a -lpthread

But, I'm getting undefined symbols from gcc_*:

ld: error: undefined symbol: stderr
>>> referenced by gcc_libinit.c:29
>>>               000006.o:(x_cgo_sys_thread_create) in archive main.a
>>> referenced by gcc_fatalf.c:17
>>>               000005.o:(fatalf) in archive main.a
>>> referenced by gcc_util.c:18
>>>               000012.o:(x_cgo_thread_start) in archive main.a

ld: error: undefined symbol: __errno_location
>>> referenced by gcc_linux_amd64.c:43
>>>               000007.o:(x_cgo_init) in archive main.a
>>> referenced by gcc_mmap.c:23
>>>               000008.o:(x_cgo_mmap) in archive main.a
>>> referenced by gcc_sigaction.c:62
>>>               000010.o:(x_cgo_sigaction) in archive main.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Is it possible to compile the Go runtime without glibc dependency so I can use the archive with clang toolchain without relying on gcc toolchain/glibc ?

Ian Lance Taylor

unread,
Sep 8, 2021, 7:53:45 PM9/8/21
to Thor Odinson, golang-nuts
Using -buildmode=c-archive implies a dependency on glibc.

Does it help if you use the -pthread option with clang? You should
use that option in any case.

Ian




> On Wednesday, 8 September 2021 at 01:19:50 UTC+5:30 Ian Lance Taylor wrote:
>>
>> On Tue, Sep 7, 2021 at 9:25 AM Thor Odinson
>> <thorodins...@gmail.com> wrote:
>> >
>> > I've built an executable file using Go compiler, but the target environments loader expects the program sections to be in specific order and fails to execute.
>> >
>> > As a result, I'm trying to compile the Go source using 'go tool compile' to generate object files, which I want to link using the clang linker option instead of 'go tool link'.
>> >
>> > Tried building a archive/shared library using go buildmode option, but this includes lot of Go runtime in the built library. If I have to link this library along with other libraries using clang toolchain, this would not resolve the symbols as the statically built library (built using buildmode) has glibc dependency.
>> >
>> > "
>> > /usr/lib/llvm-5.0/bin/ld.lld: error: undefined symbol: stderr
>> > >>> referenced by gcc_libinit.c:29
>> > >>> 000006.o:(x_cgo_sys_thread_create) in archive ./sdlgotest.a
>> > "
>> >
>> > Is there a way to build Go runtime without glibc symbols, so that I can use the archive without relying on gcc toolchain ?
>>
>> I'm not aware of any way to do that.
>>
>> The Go compiler generates a Go-specific object file format, which the
>> clang linker does not understand. The normal way to handle this is to
>> use -buildmode=c-archve, which produces an object that the clang
>> linker does understand; however, as you noted, that assumes that the
>> Go code will be linked with C code and more generally with the C
>> library, which I guess you don't want to do.
>>
>> I'm a bit surprised that the ordinary "go build" output doesn't work
>> for you, as the order of sections is the default one. What section
>> ordering do you need?
>>
>> Ian
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/984ec245-11a3-41b9-a362-a899d75d5e74n%40googlegroups.com.

Thor Odinson

unread,
Sep 9, 2021, 12:34:58 AM9/9/21
to golang-nuts
Ian,

Thanks for the clarification.
To confirm, setting go env CC=clang CXX=clang++, won't be effective for -buildmode option.
Reply all
Reply to author
Forward
0 new messages