From: Nadav Har'El <[email protected]>
Committer: Nadav Har'El <[email protected]>
Branch: master

Fix gccgo-example

Changes to the gccgo runtime library made it necessary to change the main()
starting a go program, and the gccgo-example app now longer works.

However, since libgobegin.a is now compiled as -fPIC (see
https://patchwork.ozlabs.org/patch/464423/), we can take main() directly
from that library, instead of needing to write it on our own, and everything
becomes simpler (and works).

Fixes https://github.com/cloudius-systems/osv/issues/693

Signed-off-by: Nadav Har'El <[email protected]>

---
diff --git a/gccgo-example/Makefile b/gccgo-example/Makefile
--- a/gccgo-example/Makefile
+++ b/gccgo-example/Makefile
@@ -6,7 +6,7 @@ module: hello.so
                >> usr.manifest

 CFLAGS = -g -fPIC
-LDFLAGS = -lgo
+LDFLAGS = -lgo -lgobegin

 %.o: %.go
        gccgo -c $(CFLAGS) -o $@ $<
@@ -18,5 +18,5 @@ clean:
        rm -f hello.so *.o hello usr.manifest

 # Test pie (works on Linux, but not yet on OSv)
-hello: hello.o go-main.o
+hello: hello.o
        $(CC) -pie -o $@ $^ $(LDFLAGS)
diff --git a/gccgo-example/go-main.c b/gccgo-example/go-main.c
--- a/gccgo-example/go-main.c
+++ b/gccgo-example/go-main.c
@@ -1,32 +1,13 @@
 /*
  * When compiling a Go program with gccgo, the main() is normally picked up
- * from libgobegin.a. Unfortunately, this library is compiled without -fPIC
- * so we cannot use it in OSv, so we need to supply our own main() function.
+ * from libgobegin.a. In the past this library was compiled without -fPIC
+ * so we couldn't use it in OSv, but in more recent gccgo this was fixed
+ * (see https://patchwork.ozlabs.org/patch/464423/) and now we can use it.
  *
- * This is a minimal version of gccgo's runtime/go-main.c.
+ * Still, we need to make sure the linker picks up main() from that library.
+ * When compiling an executable, it knows it should. But when compiling a
+ * a shared object, it doesn't know. We need to refer to main() to force it
+ * being included.
  */
-
-#include <stdlib.h>
-#include <stdint.h>
-
-/* The various functions from libgo do all the work */
-extern void runtime_check(void);
-extern void runtime_args(uint32_t, uint8_t**);
-extern void runtime_osinit(void);
-extern void runtime_schedinit(void);
-extern struct G* __go_go(void (*pfn)(void*), void*);
-extern void* runtime_mstart(void*);
-extern void runtime_main(void*);
-extern struct M* runtime_m(void);
-
-int
-main (int argc, char **argv)
-{
-  runtime_check ();
-  runtime_args (argc, (uint8_t**) argv);
-  runtime_osinit ();
-  runtime_schedinit ();
-  __go_go (runtime_main, NULL);
-  runtime_mstart (runtime_m ());
-  abort ();
-}
+extern int main();
+void *force_main_to_be_included = main;

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to