How to execute machine code programatically in golang?

121 views
Skip to first unread message

kant kodali

unread,
Jun 22, 2024, 1:13:17 AM (11 days ago) Jun 22
to golang-dev

Hi All,

I was trying to see how I can execute machine code programmatically in golang. Here is an example I have so far https://go.dev/play/p/5HL1ydr5IUy but this results in a segfault when I finally call the function in the end. Any idea how to fix this? 

Thanks,
Kant

Mark Diener

unread,
Jun 23, 2024, 4:32:27 PM (9 days ago) Jun 23
to golang-dev

Alan Donovan

unread,
Jun 23, 2024, 5:00:10 PM (9 days ago) Jun 23
to kant kodali, golang-dev
A func value is a pointer to a word containing the address of the
code; in other words, it's doubly indirect. (The code generated for
f() is a load from f followed by an indirect CALL.) So, you need to
introduce another level of indirection. Also, the logic for the unsafe
coercion is wrong: it lacks an & operator, so you need a second level
of indirection.

Try this: https://go.dev/play/p/cLYOt3WWbrv
> --
> You received this message because you are subscribed to the Google Groups "golang-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/CA%2BiiNx8oWS0jORDJP2LyD%3Doqykbr1qw%3DUeL6daTzRUJ7O10LpQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages