1 /* Utility functions used by tools like collect2 and lto-wrapper.
2 Copyright (C) 2009-2021 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
,
108 const char *atsuffix
)
113 char *response_arg
= NULL
;
114 char *response_argv
[3];
116 if (use_atfile
&& argv
[0] != NULL
)
118 /* If using @file arguments, create a temporary file and put the
119 contents of argv into it. Then change argv to an array corresponding
120 to a single argument @FILE, where FILE is the temporary filename. */
122 char **current_argv
= argv
+ 1;
123 char *argv0
= argv
[0];
127 /* Note: we assume argv contains at least one element; this is
130 if (!save_temps
|| !atsuffix
|| !dumppfx
)
131 response_file
= make_temp_file ("");
133 response_file
= concat (dumppfx
, atsuffix
, NULL
);
135 f
= fopen (response_file
, "w");
138 fatal_error (input_location
, "could not open response file %s",
141 status
= writeargv (current_argv
, f
);
144 fatal_error (input_location
, "could not write to response file %s",
150 fatal_error (input_location
, "could not close response file %s",
153 response_arg
= concat ("@", response_file
, NULL
);
154 response_argv
[0] = argv0
;
155 response_argv
[1] = response_arg
;
156 response_argv
[2] = NULL
;
158 argv
= response_argv
;
161 if (verbose
|| debug
)
167 fprintf (stderr
, "%s", argv
[0]);
169 notice ("[cannot find %s]", prog
);
171 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
172 fprintf (stderr
, " %s", str
);
174 fprintf (stderr
, "\n");
180 /* If we cannot find a program we need, complain error. Do this here
181 since we might not end up needing something that we could not find. */
184 fatal_error (input_location
, "cannot find %qs", prog
);
186 pex
= pex_init (0, "collect2", NULL
);
188 fatal_error (input_location
, "%<pex_init%> failed: %m");
190 errmsg
= pex_run (pex
, flags
, argv
[0], argv
, outname
,
197 fatal_error (input_location
, "%s: %m", _(errmsg
));
200 fatal_error (input_location
, errmsg
);
209 fork_execute (const char *prog
, char **argv
, bool use_atfile
,
210 const char *atsuffix
)
214 pex
= collect_execute (prog
, argv
, NULL
, NULL
,
215 PEX_LAST
| PEX_SEARCH
, use_atfile
, atsuffix
);
219 /* Delete tempfiles. */
222 utils_cleanup (bool from_signal
)
224 static bool cleanup_done
= false;
229 /* Setting cleanup_done prevents an infinite loop if one of the
230 calls to maybe_unlink fails. */
233 tool_cleanup (from_signal
);