1 /* go-libmain.c -- the startup function for a Go library.
2 
3    Copyright 2015 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6 
7 #include "config.h"
8 
9 #include <errno.h>
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <unistd.h>
14 
15 #include "runtime.h"
16 #include "go-alloc.h"
17 #include "array.h"
18 #include "arch.h"
19 #include "malloc.h"
20 
21 /* This is used when building a standalone Go library using the Go
22    command's -buildmode=c-archive or -buildmode=c-shared option.  It
23    starts up the Go code as a global constructor but does not take any
24    other action.  The main program is written in some other language
25    and calls exported Go functions as needed.  */
26 
27 static void die (const char *, int);
28 static void initfn (int, char **, char **);
29 static void *gostart (void *);
30 
31 /* Used to pass arguments to the thread that runs the Go startup.  */
32 
33 struct args {
34   int argc;
35   char **argv;
36 };
37 
38 /* We use .init_array so that we can get the command line arguments.
39    This obviously assumes .init_array support; different systems may
40    require other approaches.  */
41 
42 typedef void (*initarrayfn) (int, char **, char **);
43 
44 static initarrayfn initarray[1]
45 __attribute__ ((section (".init_array"), used)) =
46   { initfn };
47 
48 /* This function is called at program startup time.  It starts a new
49    thread to do the actual Go startup, so that program startup is not
50    paused waiting for the Go initialization functions.  Exported cgo
51    functions will wait for initialization to complete if
52    necessary.  */
53 
54 static void
initfn(int argc,char ** argv,char ** env)55 initfn (int argc, char **argv, char** env __attribute__ ((unused)))
56 {
57   int err;
58   pthread_attr_t attr;
59   struct args *a;
60   pthread_t tid;
61 
62   runtime_isarchive = true;
63 
64   runtime_initsig(true);
65 
66   a = (struct args *) malloc (sizeof *a);
67   if (a == NULL)
68     die ("malloc", errno);
69   a->argc = argc;
70   a->argv = argv;
71 
72   err = pthread_attr_init (&attr);
73   if (err != 0)
74     die ("pthread_attr_init", err);
75   err = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
76   if (err != 0)
77     die ("pthread_attr_setdetachstate", err);
78 
79   err = pthread_create (&tid, &attr, gostart, (void *) a);
80   if (err != 0)
81     die ("pthread_create", err);
82 
83   err = pthread_attr_destroy (&attr);
84   if (err != 0)
85     die ("pthread_attr_destroy", err);
86 }
87 
88 /* Start up the Go runtime.  */
89 
90 static void *
gostart(void * arg)91 gostart (void *arg)
92 {
93   struct args *a = (struct args *) arg;
94 
95   if (runtime_isstarted)
96     return NULL;
97   runtime_isstarted = true;
98 
99   runtime_check ();
100   runtime_args (a->argc, (byte **) a->argv);
101   runtime_osinit ();
102   runtime_schedinit ();
103   __go_go (runtime_main, NULL);
104   runtime_mstart (runtime_m ());
105   abort ();
106 }
107 
108 /* If something goes wrong during program startup, crash.  There is no
109    way to report failure and nobody to whom to report it.  */
110 
111 static void
die(const char * fn,int err)112 die (const char *fn, int err)
113 {
114   fprintf (stderr, "%s: %d\n", fn, err);
115   exit (EXIT_FAILURE);
116 }
117