go libraries and GC

115 views
Skip to first unread message

Brendan Miller

unread,
Nov 11, 2009, 8:40:08 PM11/11/09
to golang-nuts
So, is it actually possible to generate a .so, and use another
language like python to load those libraries? i.e. are go .so's ELF?
Can I load go functions from my C or python program using dlopen and
dlsym and call them? What about passing strings and maps across
language boundaries?

The reason people use C and C++ for writing libraries on linux is
because every language can load functions from an ELF so can call
those functions.

What about interactions with Go's GC?

Am I going to have to write stub code? This seems like it would be
painful because languages like Java already require JNI stubs in C,
which would then have to dlsym, dlload my go stub... which would call
the actual function.

What about dealing with C strings? You prohibit pointer arithmetic, so
this seems like it would be problematic. What about working with C
data structures that require pointer arithmetic?

Brendan

Adam Langley

unread,
Nov 11, 2009, 8:43:56 PM11/11/09
to Brendan Miller, golang-nuts
On Wed, Nov 11, 2009 at 5:40 PM, Brendan Miller <catp...@catphive.net> wrote:
> So, is it actually possible to generate a .so, and use another
> language like python to load those libraries? i.e. are go .so's ELF?

The .a's are ELF files. However, the calling conventions are very
different so one cannot simply link with C code and have things work.

> Can I load go functions from my C or python program using dlopen and
> dlsym and call them? What about passing strings and maps across
> language boundaries?

No, for the above reasons.

> The reason people use C and C++ for writing libraries on linux is
> because every language can load functions from an ELF so can call
> those functions.

Go can call into C code using the FFI. See misc/cgo/gmp/gmp.go in the
source distribution.

> What about interactions with Go's GC?

When calling C functions you should keep hold of any objects that the
C functions may use in order to stop the GC from collecting them.

> Am I going to have to write stub code? This seems like it would be
> painful because languages like Java already require JNI stubs in C,
> which would then have to dlsym, dlload my go stub... which would call
> the actual function.

You should see the GMP example.

AGL

Steve Jenson

unread,
Nov 11, 2009, 10:55:53 PM11/11/09
to Adam Langley, Brendan Miller, golang-nuts
On Wed, Nov 11, 2009 at 5:43 PM, Adam Langley <a...@golang.org> wrote:
>
> On Wed, Nov 11, 2009 at 5:40 PM, Brendan Miller <catp...@catphive.net> wrote:
>> So, is it actually possible to generate a .so, and use another
>> language like python to load those libraries? i.e. are go .so's ELF?
>
> The .a's are ELF files. However, the calling conventions are very
> different so one cannot simply link with C code and have things work.

Hey Adam,

Can you point to any documentation for the calling conventions? I'd
love to try and write a Ruby extension in Go.

Thanks,
Steve

Ian Lance Taylor

unread,
Nov 11, 2009, 11:55:45 PM11/11/09
to ste...@pobox.com, Adam Langley, Brendan Miller, golang-nuts
Steve Jenson <ste...@gmail.com> writes:

> Can you point to any documentation for the calling conventions? I'd
> love to try and write a Ruby extension in Go.

The calling conventions for 6g/8g are simply to push all the
parameters on the stack. But I'm not sure you can write an extension
in Go using 6g/8g because they won't generate a .so. You could do it
using gccgo, though.

Ian
Reply all
Reply to author
Forward
0 new messages