]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/collect-utils.c
Fix PR driver/79181 (and others), not deleting some /tmp/cc* files for LTO.
[thirdparty/gcc.git] / gcc / collect-utils.c
CommitLineData
a185856a 1/* Utility functions used by tools like collect2 and lto-wrapper.
99dee823 2 Copyright (C) 2009-2021 Free Software Foundation, Inc.
a185856a
BS
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "intl.h"
24#include "diagnostic.h"
25#include "obstack.h"
26#include "opts.h"
27#include "options.h"
28#include "simple-object.h"
29#include "lto-section-names.h"
30#include "collect-utils.h"
31
32static char *response_file;
33
34bool debug;
35bool verbose;
608508a6 36bool save_temps;
efc16503 37const char *dumppfx;
a185856a 38
a185856a
BS
39
40/* Notify user of a non-error. */
41void
42notice (const char *cmsgid, ...)
43{
44 va_list ap;
45
46 va_start (ap, cmsgid);
47 vfprintf (stderr, _(cmsgid), ap);
48 va_end (ap);
49}
50
51void
52fatal_signal (int signum)
53{
54 signal (signum, SIG_DFL);
5f0ad6a5 55 utils_cleanup (true);
a185856a
BS
56 /* Get the same signal again, this time not handled,
57 so its normal effect occurs. */
58 kill (getpid (), signum);
59}
2dc6782a
AP
60
61/* Setup the signal handlers for the utils. */
62void
63setup_signals (void)
64{
65#ifdef SIGQUIT
66 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
67 signal (SIGQUIT, fatal_signal);
68#endif
69 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
70 signal (SIGINT, fatal_signal);
71#ifdef SIGALRM
72 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
73 signal (SIGALRM, fatal_signal);
74#endif
75#ifdef SIGHUP
76 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
77 signal (SIGHUP, fatal_signal);
78#endif
79 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
80 signal (SIGSEGV, fatal_signal);
81 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
82 signal (SIGTERM, fatal_signal);
83#ifdef SIGPIPE
84 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
85 signal (SIGPIPE, fatal_signal);
86#endif
87#ifdef SIGBUS
88 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
89 signal (SIGBUS, fatal_signal);
90#endif
91#ifdef SIGCHLD
92 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
93 receive the signal. A different setting is inheritable */
94 signal (SIGCHLD, SIG_DFL);
95#endif
96}
5f0ad6a5
BS
97\f
98/* Wait for a process to finish, and exit if a nonzero status is found. */
a185856a
BS
99
100int
101collect_wait (const char *prog, struct pex_obj *pex)
102{
103 int status;
104
105 if (!pex_get_status (pex, 1, &status))
a9c697b8 106 fatal_error (input_location, "cannot get program status: %m");
a185856a
BS
107 pex_free (pex);
108
8289f048
TG
109 if (response_file && !save_temps)
110 {
111 unlink (response_file);
112 response_file = NULL;
113 }
114
a185856a
BS
115 if (status)
116 {
117 if (WIFSIGNALED (status))
118 {
119 int sig = WTERMSIG (status);
40fecdd6 120 fatal_error (input_location, "%s terminated with signal %d [%s]%s",
5f0ad6a5
BS
121 prog, sig, strsignal (sig),
122 WCOREDUMP (status) ? ", core dumped" : "");
a185856a
BS
123 }
124
125 if (WIFEXITED (status))
5f0ad6a5 126 return WEXITSTATUS (status);
a185856a 127 }
a185856a
BS
128 return 0;
129}
130
131void
132do_wait (const char *prog, struct pex_obj *pex)
133{
134 int ret = collect_wait (prog, pex);
135 if (ret != 0)
40fecdd6 136 fatal_error (input_location, "%s returned %d exit status", prog, ret);
a185856a
BS
137}
138
5f0ad6a5
BS
139\f
140/* Execute a program, and wait for the reply. */
a185856a 141
5f0ad6a5
BS
142struct pex_obj *
143collect_execute (const char *prog, char **argv, const char *outname,
b3032d1b
TB
144 const char *errname, int flags, bool use_atfile,
145 const char *atsuffix)
a185856a 146{
5f0ad6a5
BS
147 struct pex_obj *pex;
148 const char *errmsg;
149 int err;
150 char *response_arg = NULL;
151 char *response_argv[3];
152
153 if (use_atfile && argv[0] != NULL)
a185856a 154 {
5f0ad6a5
BS
155 /* If using @file arguments, create a temporary file and put the
156 contents of argv into it. Then change argv to an array corresponding
157 to a single argument @FILE, where FILE is the temporary filename. */
158
159 char **current_argv = argv + 1;
160 char *argv0 = argv[0];
161 int status;
162 FILE *f;
163
164 /* Note: we assume argv contains at least one element; this is
165 checked above. */
166
e9f8a554 167 if (!save_temps || !atsuffix || !dumppfx)
b3032d1b
TB
168 response_file = make_temp_file ("");
169 else
170 response_file = concat (dumppfx, atsuffix, NULL);
5f0ad6a5
BS
171
172 f = fopen (response_file, "w");
173
174 if (f == NULL)
40fecdd6
JM
175 fatal_error (input_location, "could not open response file %s",
176 response_file);
5f0ad6a5
BS
177
178 status = writeargv (current_argv, f);
179
180 if (status)
40fecdd6
JM
181 fatal_error (input_location, "could not write to response file %s",
182 response_file);
5f0ad6a5
BS
183
184 status = fclose (f);
185
186 if (EOF == status)
40fecdd6
JM
187 fatal_error (input_location, "could not close response file %s",
188 response_file);
5f0ad6a5
BS
189
190 response_arg = concat ("@", response_file, NULL);
191 response_argv[0] = argv0;
192 response_argv[1] = response_arg;
193 response_argv[2] = NULL;
194
195 argv = response_argv;
a185856a 196 }
a185856a 197
5f0ad6a5
BS
198 if (verbose || debug)
199 {
200 char **p_argv;
201 const char *str;
202
203 if (argv[0])
204 fprintf (stderr, "%s", argv[0]);
205 else
206 notice ("[cannot find %s]", prog);
207
208 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
209 fprintf (stderr, " %s", str);
210
211 fprintf (stderr, "\n");
212 }
213
214 fflush (stdout);
215 fflush (stderr);
216
217 /* If we cannot find a program we need, complain error. Do this here
218 since we might not end up needing something that we could not find. */
219
220 if (argv[0] == 0)
904f3daa 221 fatal_error (input_location, "cannot find %qs", prog);
5f0ad6a5
BS
222
223 pex = pex_init (0, "collect2", NULL);
224 if (pex == NULL)
a9c697b8 225 fatal_error (input_location, "%<pex_init%> failed: %m");
5f0ad6a5
BS
226
227 errmsg = pex_run (pex, flags, argv[0], argv, outname,
228 errname, &err);
229 if (errmsg != NULL)
230 {
231 if (err != 0)
232 {
233 errno = err;
40fecdd6 234 fatal_error (input_location, "%s: %m", _(errmsg));
5f0ad6a5
BS
235 }
236 else
40fecdd6 237 fatal_error (input_location, errmsg);
5f0ad6a5 238 }
a185856a 239
5f0ad6a5
BS
240 free (response_arg);
241
242 return pex;
243}
a185856a
BS
244
245void
b3032d1b
TB
246fork_execute (const char *prog, char **argv, bool use_atfile,
247 const char *atsuffix)
a185856a
BS
248{
249 struct pex_obj *pex;
a185856a 250
5f0ad6a5 251 pex = collect_execute (prog, argv, NULL, NULL,
b3032d1b 252 PEX_LAST | PEX_SEARCH, use_atfile, atsuffix);
5f0ad6a5
BS
253 do_wait (prog, pex);
254}
a185856a 255
5f0ad6a5 256/* Delete tempfiles. */
a185856a 257
5f0ad6a5
BS
258void
259utils_cleanup (bool from_signal)
260{
261 static bool cleanup_done = false;
a185856a 262
5f0ad6a5
BS
263 if (cleanup_done)
264 return;
a185856a 265
5f0ad6a5
BS
266 /* Setting cleanup_done prevents an infinite loop if one of the
267 calls to maybe_unlink fails. */
268 cleanup_done = true;
a185856a 269
5f0ad6a5 270 tool_cleanup (from_signal);
a185856a 271}