Skip to content

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Apr 13, 2022

This depends on #2774.

Static functions are used in tinygo.org/x/bluetooth but so far we've mostly just worked around them. This implements proper support for static functions.

@aykevl
Copy link
Member Author

aykevl commented Apr 13, 2022

Error: /Users/runner/work/tinygo/tinygo/testdata/cgo/extra.go:9:78: error: aliases are not supported on darwin
void _Cgo_static_8fa7a76b0410251fc60e_headerfunc_static(void) __attribute__((alias("headerfunc_static")));

grumble

@aykevl
Copy link
Member Author

aykevl commented Apr 14, 2022

I expected this to be necessary for tinygo-org/drivers#401 but apparently it's not.

@deadprogram
Copy link
Member

Now that #2774 has been merged, this PR should now be able to be moved forward.

@aykevl
Copy link
Member Author

aykevl commented May 22, 2022

Unfortunately, not. It doesn't work on MacOS yet. I need to work around that somehow.

@ysoldak
Copy link
Contributor

ysoldak commented Aug 30, 2022

Unfortunately, not. It doesn't work on MacOS yet. I need to work around that somehow.

I've found this workaround for MacOS, can we use it?
https://github.com/besser82/libxcrypt/blob/b8c6d0475f5e1a87a6dfbc9f62b7402b69052049/lib/crypt-port.h#L191-L198

/* Darwin (Mach-O) doesn't support alias attributes or symbol versioning.
   It does, however, support symbol aliasing at the object file level.  */
#ifdef __APPLE__

# define _strong_alias(name, aliasname)         \
  __asm__(".globl _" #aliasname);               \
  __asm__(".set _" #aliasname ", _" #name);     \
  extern __typeof(name) aliasname __THROW

@aykevl aykevl force-pushed the cgo-static branch 2 times, most recently from 2049ce7 to 298ddc6 Compare August 31, 2022 21:36
@aykevl
Copy link
Member Author

aykevl commented Aug 31, 2022

@ysoldak thanks for this suggestion! I was aware of the linker feature but didn't know how to use it from C.

Unfortunately, the linker we use doesn't support aliases:

ld.lld-14: error: TODO: support symbols of type 10
ld.lld-14: error: TODO: support symbols of type 10
ld.lld-14: error: undefined symbol: __Cgo_static_425e352dd21e64a2b260_headerfunc_static
>>> referenced by /tmp/tinygo2273409226/main.o

ld.lld-14: error: undefined symbol: __Cgo_static_f2b9c44701219e7a3dfe_headerfunc_static
>>> referenced by /tmp/tinygo2273409226/main.o
error: failed to link /tmp/tinygo2273409226/main: exit status 1

Perhaps I can try to add support...

@ysoldak
Copy link
Contributor

ysoldak commented Aug 31, 2022

"the linker we use doesn't support aliases", you mean this?

ld.lld-14: error: TODO: support symbols of type 10

@aykevl
Copy link
Member Author

aykevl commented Aug 31, 2022

Yes. "symbol of type 10" refers to aliases.

@aykevl aykevl marked this pull request as ready for review September 1, 2022 10:59
@aykevl
Copy link
Member Author

aykevl commented Sep 1, 2022

I've added a workaround for MacOS, let's see whether it works.

@ysoldak
Copy link
Contributor

ysoldak commented Sep 1, 2022

It works on my Mac at least. macOS 12.5.1
I've tried

GOOS=darwin GOARCH=amd64 tinygo  build  -size short -o test       ./testdata/cgo
   code    data     bss |   flash     ram
  12734     112     145 |   12846     257

and

GOOS=darwin GOARCH=arm64 tinygo  build  -size short -o test       ./testdata/cgo
   code    data     bss |   flash     ram
  12704     120     136 |   12824     256

@aykevl
Copy link
Member Author

aykevl commented Sep 15, 2022

Reminder: this PR is ready for review.

callInst = "return " + callInst
}
aliasDeclaration := fmt.Sprintf(`
#ifdef __APPLE__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😿 oh Apple

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The worst part is that the linker actually does support aliases, it's just the compiler that doesn't.
...and I don't feel like creating a Clang patch and pushing for its inclusion.

Copy link
Member

@deadprogram deadprogram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am really looking forward to being able to compile some libs that have not been easily importable until now.

@deadprogram
Copy link
Member

Merging!!

@deadprogram deadprogram merged commit 5551ec7 into dev Sep 16, 2022
@deadprogram deadprogram deleted the cgo-static branch September 16, 2022 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants