On Sunday, May 10, 2020 at 12:33:08 AM UTC+2, Ian Lance Taylor wrote:
>
> On Sat, May 9, 2020 at 12:56 PM Gert <gert....@gmail.com <javascript:>> 
> wrote: 
> > 
> > This is the closest I get, I am out of ideas. Don't know why it runs but 
> doesn't update the string. 
>
> I don't know what you are really trying to do, but I want to be clear 
> that this code is not supported.  The supported way to call from Go to 
> C is to use cgo.  See https://golang.org/cmd/cgo and 
> https://blog.golang.org/cgo. 
>
> One big problem I see in your code is that you are assuming that Go 
> and C use the same ABI.  That is, that the arguments passed to the Go 
> function will be seen as the arguments passed to the C function.  That 
> is not the case.  When using the gc compiler, Go and C pass arguments 
> in different locations. 
>
> Ian 
>
>
The attempt was based on this aprouch 
https://go-review.googlesource.com/c/go/+/227037/17/src/crypto/x509/internal/macOS/corefoundation.go
 
<https://go-review.googlesource.com/c/go/+/227037/17/src/crypto/x509/internal/macOS/corefoundation.go#112>
 but 
trying to apply it to the most simple example I can think. Filippo Valsorda 
believes its possible to link sqlite into go without using cgo. I made the 
assumption a trampoline assembler jump is the only way to do that?


> > go run . 
> > test [116 101 115 116 0 0 0 0 0 0] 4 0 0 
> > 
> > #include <stdio.h> 
> > #include <string.h> 
> > 
> > int hello(char *s) { 
> >  char c[80]; 
> >  strcpy (c, s); 
> >  sprintf(s, "hello %s", c); 
> >  printf("------\n"); 
> >  return 0; 
> > } 
> > 
> > #include "textflag.h" 
> > 
> > TEXT ·hello_trampoline(SB),NOSPLIT,$0-0 
> >  JMP hello_c(SB) 
> > 
> > package main 
> > 
> > import ( 
> >  "fmt" 
> >  "unsafe" 
> > 
> >  "golang.org/x/sys/unix" 
> > ) 
> > 
> > //go:linkname hello_t hello_c 
> > //go:cgo_import_dynamic hello_c hello "./c/hello.dylib" 
> > 
> > //go:nosplit 
> > func hello_t(s unsafe.Pointer) (uintptr, uintptr, unix.Errno) { 
> >  return unix.Syscall(funcPC(hello_trampoline), uintptr(s), 0, 0) 
> > } 
> > 
> > func hello_trampoline() 
> > 
> > //go:nosplit 
> > func funcPC(f func()) uintptr { 
> >  return **(**uintptr)(unsafe.Pointer(&f)) 
> > } 
> > 
> > func main() { 
> >  b := make([]byte, 10, 10) 
> >  copy(b, []byte("test")) 
> >  r1, r2, err := hello_t(unsafe.Pointer(&b[0])) 
> >  fmt.Printf("%s %v %d %d %d\n", b, b, r1, r2, err) 
> > } 
> > 
> > -- 
> > 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 golan...@googlegroups.com <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/68003925-1a44-4b7c-8cec-21849784bee5%40googlegroups.com.
>  
>
>

-- 
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/7b86244d-44e1-48c7-b651-d7b53627749b%40googlegroups.com.

Reply via email to