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