News for hackers. Stuff that matters.
Re:Things I Learned Writing a JIT in Go by mitchellh on Wednesday April 30, 2014 @08:31PM (#7677436)
I don't know the full extent of the issue, but the issue right now is two ways. I don't know which they're tackling, or maybe both:

* Go can't dynamically link to C libraries (.so). It can, however, dynamically link to DLLs, which is interesting.

* C can't dynamically link things built with Go

If I were to guess, they're trying to tackle the first problem.

Re:Things I Learned Writing a JIT in Go by misframer on Wednesday April 30, 2014 @08:43PM (#7677474)
Cgo can link to dynamic (.so) and static (.a) libraries.
Re:Things I Learned Writing a JIT in Go by mitchellh on Wednesday April 30, 2014 @08:49PM (#7677493)
I know, I said "Go" can't. Sorry if that isn't clear.
Re:Things I Learned Writing a JIT in Go by e12e on Wednesday April 30, 2014 @09:39PM (#7677699)
I was recently surprised by this, when I tried to run a simple go web server in a chroot -- Apparently go defaults to dynamically linking to the host c libraries for domain lookups (partly this has to do with it being hard to do this with static code on OS X apparently).

The upshot is that while "go build" might be expected to create a static binary (that should be easy to run in a chroot) -- in practice it won't unless you manually rebuild your go toolchain, passing in a parameter to avoid cgo/linking to c libraries for name lookup.

Re:Things I Learned Writing a JIT in Go by f2f on Wednesday April 30, 2014 @10:42PM (#7677926)
if you build with CGO_ENABLED=0 then you'll have fully static go binaries on osx (but TLS won't work)
Re:Things I Learned Writing a JIT in Go by e12e on Wednesday April 30, 2014 @11:56PM (#7678145)
It's my understanding that you have to do this on Linux as well, and it isn't enough to just build your project with CGO_ENABLED=0, you'll also have to rebuild the go-toolchain with CGO_ENABLED=0 for it to work. But perhaps this has changed in recent versions of go?

Eg:

    cd /tmp
    git clone [email protected]:apg/wipes.git wipes.git
    cd wipes.git/
    go get github.com/gorilla/websocket
    go get github.com/gorilla/websocket
    go build
    ldd wipes.git
    # > libc.so.6 among others
    export CGO_ENABLED=0
    go build  # makes no difference, produces identical binary
Afaik, in this case it's 'net/http' that pulls in a dependency on cgo (again, unless we rebuild the whole go toolchain with cgo_enabled=0).

Not that this is terrible as such, but it's a gotcha if you want to run the resulting binary in a chroot, or on a host with a different version of libc...

Re:Things I Learned Writing a JIT in Go by stock_toaster on Thursday May 01, 2014 @02:27AM (#7678555)

  > Afaik, in this case it's 'net/http' that pulls in a 
  > dependency on cgo (again, unless we rebuild the whole
  > go toolchain with cgo_enabled=0).
You can also build with '-tags netgo'[1], and use go for name resolution, but still use cgo for when else you may need it.

[1]: http://dominik.honnef.co/go-tip/2013-09-07/#netgo

Re:Things I Learned Writing a JIT in Go by e12e on Thursday May 01, 2014 @03:54AM (#7678723)
Indeed:

    go install -a -tags netgo std
    go build -tags netgo
    ldd wipes.git
        not a dynamic executable
Thanks for the tip :)
Re:Things I Learned Writing a JIT in Go by f2f on Thursday May 01, 2014 @01:15AM (#7678354)
> But perhaps this has changed in recent versions of go?

it hasn't. i meant 'if you build go with CGO_ENABLED=0' but was unclear.

Re:Things I Learned Writing a JIT in Go by ominous_prime on Wednesday April 30, 2014 @10:57PM (#7677963)
TLS will work, you just can't pull the host CA bundle (osx keeps it in the keychain), so certificate verification won't work without proving your own.
Re:Things I Learned Writing a JIT in Go by georgemcbay on Wednesday April 30, 2014 @11:01PM (#7677980)
By that criteria Go doesn't have any support for DLLs either at the core language level, it just happens to export some Win32 API functions (LoadLibrary, GetProcAddress, etc) via goc (like cgo but specific to the Go bootstrapping build process) in the win32 version of the syscall package that allow you to do your own DLL late binding.
Re:Things I Learned Writing a JIT in Go by 4ad on Thursday May 01, 2014 @06:26AM (#7679041)
Goc is nothing like cgo. Goc is a simple preprocessor that allows writing C code with Go function declarations, and it will transform said code into C code compiled by 6c using some rules that are somewhat hard for people to do by hand and not screw up. For all practical purposes, goc is not required, people can write the C code by hand, it bears no effect on linking and on the ability to use code in external objects. Also, it's seldom used, most of the code is C code, not touched by goc.

The Go linker does know how to link with external objects and use their symbols, and it does this without requiring the external object to be present at link time. It can do this, because the most basic relocations don't require any knowledge about the object. This was previously supported only on PE-COFF, as it was previously used by the Windows target, but now I have extended it to ELF too, and it's used on the Solaris target. In principle, it works on every other ELF platform too (Linux, *BSD).

Note that in all of these cases ABI translation needs to be done, and the internal machinery in the runtime that does this is in motion, just like with cgo. There's no difference in the runtime performance envelope, the only difference is that it doesn't require a target toolchain, like cgo does.

Re:Things I Learned Writing a JIT in Go by 4ad on Thursday May 01, 2014 @06:36AM (#7679066)
> Go can't dynamically link to C libraries (.so). It can, however, dynamically link to DLLs, which is interesting.

Please see my comment here: https://news.ycombinator.com/item?id=7679041

In short, The Go linker can link with ELF shared objects just fine, just as it can link with PE-COFF shared objects, but it supports only the most basic of relocations in both cases (and this is a property of the design, as we can't expect the presence of the shared object because that would break cross-compilation), and it's not what you probably want anyway.