Do not error when -E provided (PR pch/78970).
[official-gcc.git] / libgo / runtime / go-libmain.c
blob8e07e90178c0a26b82ff976a3f8deeaf4eda13a7
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. */
7 #include "config.h"
9 #include <errno.h>
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <unistd.h>
15 #include "runtime.h"
16 #include "array.h"
17 #include "arch.h"
18 #include "malloc.h"
20 /* This is used when building a standalone Go library using the Go
21 command's -buildmode=c-archive or -buildmode=c-shared option. It
22 starts up the Go code as a global constructor but does not take any
23 other action. The main program is written in some other language
24 and calls exported Go functions as needed. */
26 static void die (const char *, int);
27 static void initfn (int, char **, char **);
28 static void *gostart (void *);
30 /* Used to pass arguments to the thread that runs the Go startup. */
32 struct args {
33 int argc;
34 char **argv;
37 /* We use .init_array so that we can get the command line arguments.
38 This obviously assumes .init_array support; different systems may
39 require other approaches. */
41 typedef void (*initarrayfn) (int, char **, char **);
43 static initarrayfn initarray[1]
44 __attribute__ ((section (".init_array"), used)) =
45 { initfn };
47 /* This function is called at program startup time. It starts a new
48 thread to do the actual Go startup, so that program startup is not
49 paused waiting for the Go initialization functions. Exported cgo
50 functions will wait for initialization to complete if
51 necessary. */
53 static void
54 initfn (int argc, char **argv, char** env __attribute__ ((unused)))
56 int err;
57 pthread_attr_t attr;
58 struct args *a;
59 pthread_t tid;
61 runtime_isarchive = true;
63 setIsCgo ();
64 runtime_cpuinit ();
65 runtime_initsig(true);
67 a = (struct args *) malloc (sizeof *a);
68 if (a == NULL)
69 die ("malloc", errno);
70 a->argc = argc;
71 a->argv = argv;
73 err = pthread_attr_init (&attr);
74 if (err != 0)
75 die ("pthread_attr_init", err);
76 err = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
77 if (err != 0)
78 die ("pthread_attr_setdetachstate", err);
80 err = pthread_create (&tid, &attr, gostart, (void *) a);
81 if (err != 0)
82 die ("pthread_create", err);
84 err = pthread_attr_destroy (&attr);
85 if (err != 0)
86 die ("pthread_attr_destroy", err);
89 /* Start up the Go runtime. */
91 static void *
92 gostart (void *arg)
94 struct args *a = (struct args *) arg;
96 if (runtime_isstarted)
97 return NULL;
98 runtime_isstarted = true;
100 runtime_check ();
101 runtime_args (a->argc, (byte **) a->argv);
102 runtime_osinit ();
103 runtime_schedinit ();
104 __go_go (runtime_main, NULL);
105 runtime_mstart (runtime_m ());
106 abort ();
109 /* If something goes wrong during program startup, crash. There is no
110 way to report failure and nobody to whom to report it. */
112 static void
113 die (const char *fn, int err)
115 fprintf (stderr, "%s: %d\n", fn, err);
116 exit (EXIT_FAILURE);