Hello everyone, first time here so please bear with me. 
I got the tipp of asking this rather fringe case question here from 
comments on my /r/golang post. 

In brief I want to create a shared library out of a golang project so that 
it can be dynamically loaded by applications written in another language on 
IBM AIX 7.2.
I am aware that go build -buildmode=c-shared is unsupported on AIX at the 
time of writing, so I am currently trying to work my way around this 
limitation by creating first a static archive first with "go build 
-buildmode=c-archive" and have that compiled with gcc into a shared object 
for dynamic linking.
A quick, probably nicer to read version of this has been posted on this 
reddit post 
<https://www.reddit.com/r/golang/comments/tk65e0/creating_a_shared_c_library_out_of_go_project_to/>
.

To test my endeavour I have created a simple .go library and main.c file:
sharedLibTest.go
package main

import (
    "fmt"
)

import "C"

func main() {
    fmt.Printf("%s\n", "Golang: main was called")
    MyPackage_Init()
    MyPackage_Create()
}

//export MyPackage_Init
func MyPackage_Init() {
    fmt.Printf("%s\n", "Golang: MyPackage_Init was called")
}

//export MyPackage_Create
func MyPackage_Create() {
    fmt.Printf("%s\n", "Golang: MyPackage_Create was called")
}

main.c
#include <stdio.h> #include "sharedLibTest.h" int main() { printf("%s\n", 
"C: main() called"); MyPackage_Init(); MyPackage_Create(); }

On my local linux machine I am able to compile these two files together 
both statically as well as dynamically with a native shared object as well 
as through the detour of a static archive. Please refer to the reddit post 
for the details of that.
To try and compile it on AIX I can create a static archive, have that 
compiled into a shared object with gcc8 and, due to AIX weirdness, have 
that resulting .so file renamed to .a because AIX's linker does not look 
for .so files for reasons best known to IBM.

$ go build -v -buildmode=c-archive -mod vendor -o 
/home/myuser/workspace/go_proj/sharedLibTest/sharedLibTest.a 
/home/myuser/workspace/go_proj/sharedLibTest/sharedLibTest.go
$ cat > symbols.export << EOF > MyPackage_Init > MyPackage_Create > EOF  
$ gcc -fPIC -DPIC -g -O2 -mcpu=power7 -maix64 -shared -lpthread 
-Wl,-bE:symbols.export -o libsharedLibTest.so -Wl,-bnoobjreorder 
./sharedLibTest.a 
  $ mv libsharedLibTest.so libsharedLibTest.a

I can finally link that that with my main.c file
$ gcc -L. -g -O2 -mcpu=power7 -maix64 -Wl,-bnoobjreorder -lsharedLibTest 
-lpthreads main.c  

But when I try to run that I get 
$ LD_LIBRARY_PATH=/home/myuser/workspace/go_proj/sharedLibTest ./a.out
exec(): 0509-036 Cannot load program ./a.out because of the following 
errors: 0509-150 Dependent module 
/home/myuser/workspace/go_proj/sharedLibTest/libsharedLibTest.a(libsharedLibTest.so)
 
could not be loaded. 0509-187 The local-exec model was used for 
thread-local storage, but the module is not the main program. 0509-193 
Examine the .loader section header with the 'dump -Hv' command.  


Now so far I have gathered, that if I can make the go static archive 
compile with whatever the equivalent of gcc's -fPIC option is, that archive 
produces Position Independent Code that can be loaded dynamically, a thing 
that is currently failing, according to my understanding of that loader's 
error message.

Adding -gcflags="-shared" to my go build invocation leads to an error that 
I presume is the base of the issue and why c-shared plainly prints the that 
shared libs are unsupported
$ go build -buildmode=c-archive -gcflags=-shared 
-ldflags=-linkmode=external -mod vendor -o sharedLibTest.a sharedLibTest.go 
# command-line-arguments 
main.main: unknown reloc to .TOC.: 48 (R_ADDRPOWER_PCREL) 
_cgoexp_87081e778e87_MyPackage_Init: unknown reloc to .TOC.: 48 
(R_ADDRPOWER_PCREL) 
_cgoexp_87081e778e87_MyPackage_Create: unknown reloc to .TOC.: 48 
(R_ADDRPOWER_PCREL)

So alas: my question finally is, Is there any way for golang projects to 
one way or another be compiled down into a shared library that can be 
loaded dynamically by other applications? 
I have gotten yet another hint just now from an IBM forum post, that I may 
try gcc-go, however I am wondering if this really hasn't been done yet by 
anyone else.

Best regards,
miecc

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/8fa0f5df-20a6-4a19-aacf-1e9fd06699bdn%40googlegroups.com.

Reply via email to