1 /* go-libmain.c -- the startup function for a Go library.
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. */
14 #include <sys/types.h>
22 #if defined(__sun) && defined(__SVR4)
24 /* Read a file into memory on Solaris, returning an malloc'ed buffer
25 and setting *SIZE to its size. */
28 read_file (const char *fn
, size_t *size
)
35 if (stat (fn
, &st
) < 0)
37 buf
= malloc ((size_t) st
.st_size
);
40 o
= open (fn
, O_RDONLY
);
46 got
= read (o
, buf
, st
.st_size
);
48 if (got
!= st
.st_size
)
58 /* On Solaris we don't get passed argc/argv, but we can fetch it from
62 read_cmdline (int *argc
, char ***argv
)
78 snprintf (fn
, sizeof fn
, "/proc/%ld/cmdline", (long) pid
);
79 argbuf
= read_file (fn
, &argsize
);
83 snprintf (fn
, sizeof fn
, "/proc/%ld/environ", (long) pid
);
84 envbuf
= read_file (fn
, &envsize
);
92 for (p
= argbuf
; p
< argbuf
+ argsize
; p
++)
96 ++i
; // For trailing NULL.
97 for (p
= envbuf
; p
< envbuf
+ envsize
; p
++)
100 ++i
; // For trailing NULL.
102 *argv
= (char **) malloc (i
* sizeof (char *));
113 for (p
= argbuf
; p
< argbuf
+ argsize
; p
++)
124 for (p
= envbuf
; p
< envbuf
+ envsize
; p
++)
135 #endif /* defined(__sun) && defined(__SVR4) */
137 /* This is used when building a standalone Go library using the Go
138 command's -buildmode=c-archive or -buildmode=c-shared option. It
139 starts up the Go code as a global constructor but does not take any
140 other action. The main program is written in some other language
141 and calls exported Go functions as needed. */
143 static void die (const char *, int);
144 /* .init_array section does not exist in AIX XCOFF.
145 -Wl,-binitfini:__go_init option will be required to build go
146 libraries and make sure __go_init is called when the library is
147 loaded. This requires __go_init to be exported. */
149 void __go_init (int, char **, char **);
150 static void *gostart (void *);
152 /* Used to pass arguments to the thread that runs the Go startup. */
160 /* We use .init_array so that we can get the command line arguments.
161 This obviously assumes .init_array support; different systems may
162 require other approaches. */
164 typedef void (*initarrayfn
) (int, char **, char **);
166 static initarrayfn initarray
[1]
167 __attribute__ ((section (".init_array"), used
)) =
171 /* This function is called at program startup time. It starts a new
172 thread to do the actual Go startup, so that program startup is not
173 paused waiting for the Go initialization functions. Exported cgo
174 functions will wait for initialization to complete if
178 __go_init (int argc
, char **argv
, char** env
__attribute__ ((unused
)))
185 #if defined(__sun) && defined(__SVR4)
186 read_cmdline (&argc
, &argv
);
189 runtime_isarchive
= true;
193 runtime_initsig(true);
195 a
= (struct args
*) malloc (sizeof *a
);
197 die ("malloc", errno
);
201 err
= pthread_attr_init (&attr
);
203 die ("pthread_attr_init", err
);
204 err
= pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
206 die ("pthread_attr_setdetachstate", err
);
208 err
= pthread_create (&tid
, &attr
, gostart
, (void *) a
);
210 die ("pthread_create", err
);
212 err
= pthread_attr_destroy (&attr
);
214 die ("pthread_attr_destroy", err
);
217 /* Start up the Go runtime. */
222 struct args
*a
= (struct args
*) arg
;
224 if (runtime_isstarted
)
226 runtime_isstarted
= true;
230 runtime_args (a
->argc
, (byte
**) a
->argv
);
232 runtime_schedinit ();
233 __go_go ((uintptr
)(runtime_main
), NULL
);
234 runtime_mstart (runtime_m ());
238 /* If something goes wrong during program startup, crash. There is no
239 way to report failure and nobody to whom to report it. */
242 die (const char *fn
, int err
)
244 fprintf (stderr
, "%s: %d\n", fn
, err
);