External/internal linker and cgo packages

71 views
Skip to first unread message

Vincent Blanchon

unread,
Apr 29, 2020, 1:49:26 AM4/29/20
to golang-nuts
Hello,

I'm building a simple program that has a dependency to github.com/DataDog/zstd, a wrapper of a C code.
So by default, Go will use the external linker. When debugging with, I can see

host link: "clang" "-m64" "-Wl,-headerpad,1144" "-Wl,-no_pie" "-Wl,-pagezero_size,4000000" "-o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-build122622791/b001/exe/a.out" "-Qunused-arguments" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/go.o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000000.o" [...] "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000053.o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000054.o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000055.o" "-g" "-O2" "-g" "-O2" "-g" "-O2" "-lpthread" "-g" "-O2" "-framework" "CoreFoundation" "-framework" "Security" "-no-pie"

It links 55 files, from 000000.o to 000055.oIf I had more custom debug, I can see than those files are mostly duplication:

000004.o is /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d github.com/DataDog/zstd /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d(_x005.o)
000005.o is /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d github.com/DataDog/zstd /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d(_x006.o)

I can see 40 times this zstd package among the 55 files. Is it expected? What is the difference between them?

Also, what would happen to the non known cgo packages (not net, os/user or runtime/cgo) if cgo is not enabled? How the internal linker will manage this cgo package?

Thank you

Ian Lance Taylor

unread,
Apr 29, 2020, 11:52:03 PM4/29/20
to Vincent Blanchon, golang-nuts
I may be missing something, but that doesn't look like duplication to
me. It looks like different files from an archive. The first one is
_x005.o and the second one is _x006.o.

You will get files like these for each .c file in a package that uses cgo.

Ian

Vincent Blanchon

unread,
Apr 29, 2020, 11:58:16 PM4/29/20
to golang-nuts
Hello Ian,

Thank you again for your reply!
I thought Go would compile all the C files in one .go, that makes sense.

By the way, how Go would deal with those files if cgo is not enabled - meaning the internal linker will do the job?
How the internal linker can deal with that?

Ian Lance Taylor

unread,
Apr 30, 2020, 12:00:39 AM4/30/20
to Vincent Blanchon, golang-nuts
On Wed, Apr 29, 2020 at 8:58 PM Vincent Blanchon
<blanchon...@gmail.com> wrote:
>
> Thank you again for your reply!
> I thought Go would compile all the C files in one .go, that makes sense.
>
> By the way, how Go would deal with those files if cgo is not enabled - meaning the internal linker will do the job?
> How the internal linker can deal with that?

If you are using the internal linker, then you aren't using cgo, so
the linker won't see all those C files.

Ian


> Le jeudi 30 avril 2020 07:52:03 UTC+4, Ian Lance Taylor a écrit :
>>
>> On Tue, Apr 28, 2020 at 10:49 PM Vincent Blanchon
>> <blancho...@gmail.com> wrote:
>> >
>> > I'm building a simple program that has a dependency to github.com/DataDog/zstd, a wrapper of a C code.
>> > So by default, Go will use the external linker. When debugging with, I can see
>> >
>> > host link: "clang" "-m64" "-Wl,-headerpad,1144" "-Wl,-no_pie" "-Wl,-pagezero_size,4000000" "-o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-build122622791/b001/exe/a.out" "-Qunused-arguments" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/go.o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000000.o" [...] "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000053.o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000054.o" "/var/folders/bb/w1lgkjdx47qd7695hb5r400h0000gn/T/go-link-023224426/000055.o" "-g" "-O2" "-g" "-O2" "-g" "-O2" "-lpthread" "-g" "-O2" "-framework" "CoreFoundation" "-framework" "Security" "-no-pie"
>> >
>> > It links 55 files, from 000000.o to 000055.oIf I had more custom debug, I can see than those files are mostly duplication:
>> >
>> > 000004.o is /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d github.com/DataDog/zstd /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d(_x005.o)
>> > 000005.o is /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d github.com/DataDog/zstd /path/to/go-build/67/678a3a394ba4feae32a2ddb829ea65c1cf24be64248dcbcc3dae709b8e3b826d-d(_x006.o)
>> >
>> > I can see 40 times this zstd package among the 55 files. Is it expected? What is the difference between them?
>> >
>> > Also, what would happen to the non known cgo packages (not net, os/user or runtime/cgo) if cgo is not enabled? How the internal linker will manage this cgo package?
>>
>> I may be missing something, but that doesn't look like duplication to
>> me. It looks like different files from an archive. The first one is
>> _x005.o and the second one is _x006.o.
>>
>> You will get files like these for each .c file in a package that uses cgo.
>>
>> 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/38760d5d-b8e3-4a42-b087-8b81ab4ebd87%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages