Also this probably should have been posted on golang-dev I think.I've deleted the original post)I've and replied to this in the cross-post in golang-dev. Sorry for the inconvenience.- elias
Eventually, I’d like to write shared libraries in go, in particular I want my go code callable from the android dalvik virtual machine. So, after a long and intense staring contest with the tools and the go runtime, I have a proof of concept running under linux/x64. I know that your contribution guide mentions that I should write my design before coding, but in this case I chose to do the experiments myself first to get a feel of the go code and to figure out if it was even possible for me to implement.
btw, according to contribution guide, this thread does belong to golang-nuts.
- elias--
What happens when you run two go based .so files?
If they are both making threads thinking they are the only go runtime it could lead to some interesting chaos.
I haven't tried to address that issue, but I suspect that with proper hiding of the shared library symbols, two go shared libraries could happily coexist, with two seperate runtimes, blissfully unaware of each other. Not the most efficient solution, but certainly simpler that trying to let two go libs share a single, global go runtime.
On Sun, Nov 4, 2012 at 1:01 AM, Elias Naur <elias...@gmail.com> wrote:I haven't tried to address that issue, but I suspect that with proper hiding of the shared library symbols, two go shared libraries could happily coexist, with two seperate runtimes, blissfully unaware of each other. Not the most efficient solution, but certainly simpler that trying to let two go libs share a single, global go runtime.consider sole reference to memory allocated from one runtime passed toGo code running under the other runtime, the first runtime will considerthe memory garbage, but it's used in fact.
This is part of the problem why Go doesn't produce dynamic librarynow.Another complexity is, Go never guarantee stable ABI, with static linking,this doesn't matter; but with dynamic linking, we will need some kind ofversioning system to prevent from loading ABI-incompatible libraries.(yes, i know ELF has symbol versioning and soname mechanism, butwhat about Mach-O and PE/COFF?)
On Sat, Nov 3, 2012 at 6:49 PM, minux <minu...@gmail.com> wrote:On Sun, Nov 4, 2012 at 1:01 AM, Elias Naur <elias...@gmail.com> wrote:I haven't tried to address that issue, but I suspect that with proper hiding of the shared library symbols, two go shared libraries could happily coexist, with two seperate runtimes, blissfully unaware of each other. Not the most efficient solution, but certainly simpler that trying to let two go libs share a single, global go runtime.consider sole reference to memory allocated from one runtime passed toGo code running under the other runtime, the first runtime will considerthe memory garbage, but it's used in fact.How is that different from the problem of passing references to C code through cgo? AFAIK, the proper way to do that is to hold a reference to the data on the go side while it is accessed from C.
This is part of the problem why Go doesn't produce dynamic librarynow.Another complexity is, Go never guarantee stable ABI, with static linking,this doesn't matter; but with dynamic linking, we will need some kind ofversioning system to prevent from loading ABI-incompatible libraries.(yes, i know ELF has symbol versioning and soname mechanism, butwhat about Mach-O and PE/COFF?)Do you mean in the one-global-runtime case? If two go libraries are completely seperate with seperate runtimes, schedulers etv. you shouldn't need ABI stability other than what the C interface gives you.
This is the reason we're discussing this at all :) I'm perfectly happy with statically linked stand-alone executables for my go programs, and not particularly interested in the space savings in disk space or runtime memory. My focus is the cases where a shared library is an absolute requirement. A great example is the android dalvik java runtime which _requires_ external native code to be in the form of a shared library. But anytime you'd like to write go plugins of any sort you'll very likely need dynamic library support in some form.
How multiple plugins written in go coexist is definitely an issue, but I'm willing to go with "unsupported" in the short run, "works, but inefficient in disk and memory space" in the longer run and some kind of "runtime sharing of code/runtime/memory" for the longest run.But for now, I'd just like to get the basics working - position independent code, runtime changes, compiler flags etc which is exactly what my CLs address. As far as I see, you can't avoid that work, however you want to support go shared libraries in the future.- elias
--
Android is an especially annoying use case, since go already runs on android as stand alone executables, they're just no distributable as standard .apk files through Play and the like. Only way to do that is to run an embedded go program from java and communicate with it with some kind of IPC.However, there are still many other cases where you have a main program extensible with plugins, and that is where shared libraries is the most common way to do that. Another useful case is where you have a "friendly" program that is either written entirely in Go or simply wants Go as a "scripting language" not unlike what Python is today. In that case, I could imagine shared library support would be be useful.- elias
> With the two CLs applied, building the runtime with PIC support is done with>Bikeshedding, since I'm completely ignorant of all the low-level
> GO_FLAGS=-lib ./make.bash
nitty-gritty details required to make this work ... but I do know how
to use GCC to build shared libraries from C, and the relevant option
there is -shared. So why not -shared instead of -lib?
Greg
Have you tried it on arm yet?
- a missing -lib to -shared conversion in cmd/go/testflags.go
- Removed unused typedef from cmd/6l/l.h
- moved rel_ro assignment into -U flag conditional in cmd/ld/data.c
- synced to tip and fixed, all tests again pass with or without GO_FLAGS=-shared
Elias did you mail this patch off? I cant seem to add comments to it.
What happens when you run two go based .so files?
If they are both making threads thinking they are the only go runtime it could lead to some interesting chaos.
Also this probably should have been posted on golang-dev I think.