Search code examples
gostatic-linkinggccgo

gccgo -static vs -static-libgo


What is the difference between -static and -static-libgo for gccgo? The documentation doesn't seem to really shed light on what is going on:

  • Use the -static-libgo option to link statically against the compiled packages.
  • Use the -static option to do a fully static link (the default for the gc compiler).

Is -static-libgo only static linking libgo.a only? While -static is full glibc library?


Solution

  • Check the dynamic linkage in the generated ELFs:

    gc builds statically:

    $ go build hello.go
    $ readelf -d hello
    There is no dynamic section in this file.
    

    gccgo links dynamically against libgo, libc etc. by default:

    $ go build -compiler gccgo hello.go
    $ readelf -d hello
    Dynamic section at offset 0x36e0 contains 29 entries:
      Tag        Type                         Name/Value
     0x0000000000000001 (NEEDED)             Shared library: [libgo.so.5]
     0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
     0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
     0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
     0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
    

    Bake libgo inside the executable, but still link dynamically to libc and friends:

    $ go build -compiler gccgo -gccgoflags '-static-libgo' hello.go
    $ readelf -d hello
    Dynamic section at offset 0x128068 contains 28 entries:
      Tag        Type                         Name/Value
     0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
     0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
     0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
     0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2] 
    

    Link everything statically:

    $ go build -compiler gccgo -gccgoflags '-static' hello.go
    $ readelf -d hello
    There is no dynamic section in this file.