1 /* Utility functions used by tools like collect2 and lto-wrapper.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
24 #include "diagnostic.h"
28 #include "simple-object.h"
29 #include "lto-section-names.h"
30 #include "collect-utils.h"
32 static char *response_file
;
40 /* Notify user of a non-error. */
42 notice (const char *cmsgid
, ...)
46 va_start (ap
, cmsgid
);
47 vfprintf (stderr
, _(cmsgid
), ap
);
52 fatal_signal (int signum
)
54 signal (signum
, SIG_DFL
);
56 /* Get the same signal again, this time not handled,
57 so its normal effect occurs. */
58 kill (getpid (), signum
);
61 /* Wait for a process to finish, and exit if a nonzero status is found. */
64 collect_wait (const char *prog
, struct pex_obj
*pex
)
68 if (!pex_get_status (pex
, 1, &status
))
69 fatal_error (input_location
, "cannot get program status: %m");
72 if (response_file
&& !save_temps
)
74 unlink (response_file
);
80 if (WIFSIGNALED (status
))
82 int sig
= WTERMSIG (status
);
83 fatal_error (input_location
, "%s terminated with signal %d [%s]%s",
84 prog
, sig
, strsignal (sig
),
85 WCOREDUMP (status
) ? ", core dumped" : "");
88 if (WIFEXITED (status
))
89 return WEXITSTATUS (status
);
95 do_wait (const char *prog
, struct pex_obj
*pex
)
97 int ret
= collect_wait (prog
, pex
);
99 fatal_error (input_location
, "%s returned %d exit status", prog
, ret
);
103 /* Execute a program, and wait for the reply. */
106 collect_execute (const char *prog
, char **argv
, const char *outname
,
107 const char *errname
, int flags
, bool use_atfile
)
112 char *response_arg
= NULL
;
113 char *response_argv
[3];
115 if (use_atfile
&& argv
[0] != NULL
)
117 /* If using @file arguments, create a temporary file and put the
118 contents of argv into it. Then change argv to an array corresponding
119 to a single argument @FILE, where FILE is the temporary filename. */
121 char **current_argv
= argv
+ 1;
122 char *argv0
= argv
[0];
126 /* Note: we assume argv contains at least one element; this is
129 response_file
= make_temp_file ("");
131 f
= fopen (response_file
, "w");
134 fatal_error (input_location
, "could not open response file %s",
137 status
= writeargv (current_argv
, f
);
140 fatal_error (input_location
, "could not write to response file %s",
146 fatal_error (input_location
, "could not close response file %s",
149 response_arg
= concat ("@", response_file
, NULL
);
150 response_argv
[0] = argv0
;
151 response_argv
[1] = response_arg
;
152 response_argv
[2] = NULL
;
154 argv
= response_argv
;
157 if (verbose
|| debug
)
163 fprintf (stderr
, "%s", argv
[0]);
165 notice ("[cannot find %s]", prog
);
167 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
168 fprintf (stderr
, " %s", str
);
170 fprintf (stderr
, "\n");
176 /* If we cannot find a program we need, complain error. Do this here
177 since we might not end up needing something that we could not find. */
180 fatal_error (input_location
, "cannot find %qs", prog
);
182 pex
= pex_init (0, "collect2", NULL
);
184 fatal_error (input_location
, "%<pex_init%> failed: %m");
186 errmsg
= pex_run (pex
, flags
, argv
[0], argv
, outname
,
193 fatal_error (input_location
, "%s: %m", _(errmsg
));
196 fatal_error (input_location
, errmsg
);
205 fork_execute (const char *prog
, char **argv
, bool use_atfile
)
209 pex
= collect_execute (prog
, argv
, NULL
, NULL
,
210 PEX_LAST
| PEX_SEARCH
, use_atfile
);
214 /* Delete tempfiles. */
217 utils_cleanup (bool from_signal
)
219 static bool cleanup_done
= false;
224 /* Setting cleanup_done prevents an infinite loop if one of the
225 calls to maybe_unlink fails. */
228 tool_cleanup (from_signal
);