golang building statically linked error: loadinternal: cannot find runtime/cgo

511 views
Skip to first unread message

Alexey Kuznetsov

unread,
Nov 19, 2022, 9:46:31 AM11/19/22
to golang-nuts
Hello!

I'm trying to build statically linked binary and it works with 'build' command but failed with 'install' command. Here is an example:

go build -ldflags "-linkmode external -extldflags -static" -a swap.go

# file swap
swap: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=396a1f51971f4e0d44bfb489a84792efc1782e03, for GNU/Linux 3.2.0, with debug_info, not stripped

# ldd swap
    not a dynamic executable

But following failed:

#  go install -ldflags "-linkmode external -extldflags -static" gitlab.com/axet/sw...@swap-0.0.4
# gitlab.com/axet/swap
loadinternal: cannot find runtime/cgo

Following compiles but not as static:

# go install -ldflags "-extldflags -static" gitlab.com/axet/sw...@swap-0.0.4

# file ~/.go/bin/swap
/home/axet/.go/bin/swap: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=8DAAfOPcNpPhyRmJINiQ/RJu44gsWAPnCPTRsFMPn/vAto5mmPDQDM4aX1HuBe/7MY6v2L5JqkYe2MJN8uw, with debug_info, not stripped

# ldd ~/.go/bin/swap
    not a dynamic executable


Bug or feature?

Ian Lance Taylor

unread,
Nov 19, 2022, 9:49:50 AM11/19/22
to Alexey Kuznetsov, golang-nuts
They both look static to me.  What is the difference?

Ian

Alexey Kuznetsov

unread,
Nov 19, 2022, 10:04:28 AM11/19/22
to Ian Lance Taylor, golang-nuts
'file' command used to say "dynamically linked" if I'm not using '-linkmode external' flag. I tried many times, I didn't pay attention, now it's working...I guess the only question left with "loadinternal: cannot find runtime/cgo"

-- AK

Alexey Kuznetsov

unread,
Nov 19, 2022, 10:06:38 AM11/19/22
to Ian Lance Taylor, golang-nuts
Correction. When I'm using -buildmode pie it producing weired file static / dynamic:

#  go install -ldflags "-extldflags -static" -buildmode pie gitlab.com/axet/sw...@swap-0.0.4

# file .go/bin/swap
.go/bin/swap: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=IdU-e_SYkwhiX9RUPRZ1/FCjd89you8IzAl7nDax_/7ww2AvzWZX8UIdMsxhe8/o5djfvZtYWIn7ldfOiOl, with debug_info, not stripped

# ldd .go/bin/swap
statically linked


-- AK

Ian Lance Taylor

unread,
Nov 19, 2022, 1:38:29 PM11/19/22
to Alexey Kuznetsov, golang-nuts
On Sat, Nov 19, 2022 at 7:06 AM Alexey Kuznetsov
<kuznetso...@gmail.com> wrote:
>
> Correction. When I'm using -buildmode pie it producing weired file static / dynamic:
>
> # go install -ldflags "-extldflags -static" -buildmode pie gitlab.com/axet/sw...@swap-0.0.4
>
> # file .go/bin/swap
> .go/bin/swap: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=IdU-e_SYkwhiX9RUPRZ1/FCjd89you8IzAl7nDax_/7ww2AvzWZX8UIdMsxhe8/o5djfvZtYWIn7ldfOiOl, with debug_info, not stripped
>
> # ldd .go/bin/swap
> statically linked

Those are telling you two different things.

The file command is telling you that the executable requires the
dynamic linker. That is always true for PIE: the dynamic linker
relocates the executable at run time.

The ldd command is telling you that the executable is not linked
against any dynamic libraries. It needs the dynamic linker, but no
dynamic libraries.

Ian
Reply all
Reply to author
Forward
0 new messages