1 /* Utility functions used by tools like collect2 and lto-wrapper.
2    Copyright (C) 2009-2021 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
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
9 version.
10 
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
14 for more details.
15 
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/>.  */
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 
32 static char *response_file;
33 
34 bool debug;
35 bool verbose;
36 bool save_temps;
37 const char *dumppfx;
38 
39 
40 /* Notify user of a non-error.  */
41 void
notice(const char * cmsgid,...)42 notice (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 
51 void
fatal_signal(int signum)52 fatal_signal (int signum)
53 {
54   signal (signum, SIG_DFL);
55   utils_cleanup (true);
56   /* Get the same signal again, this time not handled,
57      so its normal effect occurs.  */
58   kill (getpid (), signum);
59 }
60 
61 /* Wait for a process to finish, and exit if a nonzero status is found.  */
62 
63 int
collect_wait(const char * prog,struct pex_obj * pex)64 collect_wait (const char *prog, struct pex_obj *pex)
65 {
66   int status;
67 
68   if (!pex_get_status (pex, 1, &status))
69     fatal_error (input_location, "cannot get program status: %m");
70   pex_free (pex);
71 
72   if (response_file && !save_temps)
73     {
74       unlink (response_file);
75       response_file = NULL;
76     }
77 
78   if (status)
79     {
80       if (WIFSIGNALED (status))
81 	{
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" : "");
86 	}
87 
88       if (WIFEXITED (status))
89 	return WEXITSTATUS (status);
90     }
91   return 0;
92 }
93 
94 void
do_wait(const char * prog,struct pex_obj * pex)95 do_wait (const char *prog, struct pex_obj *pex)
96 {
97   int ret = collect_wait (prog, pex);
98   if (ret != 0)
99     fatal_error (input_location, "%s returned %d exit status", prog, ret);
100 }
101 
102 
103 /* Execute a program, and wait for the reply.  */
104 
105 struct pex_obj *
collect_execute(const char * prog,char ** argv,const char * outname,const char * errname,int flags,bool use_atfile,const char * atsuffix)106 collect_execute (const char *prog, char **argv, const char *outname,
107 		 const char *errname, int flags, bool use_atfile,
108 		 const char *atsuffix)
109 {
110   struct pex_obj *pex;
111   const char *errmsg;
112   int err;
113   char *response_arg = NULL;
114   char *response_argv[3];
115 
116   if (use_atfile && argv[0] != NULL)
117     {
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.  */
121 
122       char **current_argv = argv + 1;
123       char *argv0 = argv[0];
124       int status;
125       FILE *f;
126 
127       /* Note: we assume argv contains at least one element; this is
128          checked above.  */
129 
130       if (!save_temps || !atsuffix || !dumppfx)
131 	response_file = make_temp_file ("");
132       else
133 	response_file = concat (dumppfx, atsuffix, NULL);
134 
135       f = fopen (response_file, "w");
136 
137       if (f == NULL)
138         fatal_error (input_location, "could not open response file %s",
139 		     response_file);
140 
141       status = writeargv (current_argv, f);
142 
143       if (status)
144         fatal_error (input_location, "could not write to response file %s",
145 		     response_file);
146 
147       status = fclose (f);
148 
149       if (EOF == status)
150         fatal_error (input_location, "could not close response file %s",
151 		     response_file);
152 
153       response_arg = concat ("@", response_file, NULL);
154       response_argv[0] = argv0;
155       response_argv[1] = response_arg;
156       response_argv[2] = NULL;
157 
158       argv = response_argv;
159     }
160 
161   if (verbose || debug)
162     {
163       char **p_argv;
164       const char *str;
165 
166       if (argv[0])
167 	fprintf (stderr, "%s", argv[0]);
168       else
169 	notice ("[cannot find %s]", prog);
170 
171       for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
172 	fprintf (stderr, " %s", str);
173 
174       fprintf (stderr, "\n");
175     }
176 
177   fflush (stdout);
178   fflush (stderr);
179 
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.  */
182 
183   if (argv[0] == 0)
184     fatal_error (input_location, "cannot find %qs", prog);
185 
186   pex = pex_init (0, "collect2", NULL);
187   if (pex == NULL)
188     fatal_error (input_location, "%<pex_init%> failed: %m");
189 
190   errmsg = pex_run (pex, flags, argv[0], argv, outname,
191 		    errname, &err);
192   if (errmsg != NULL)
193     {
194       if (err != 0)
195 	{
196 	  errno = err;
197 	  fatal_error (input_location, "%s: %m", _(errmsg));
198 	}
199       else
200 	fatal_error (input_location, errmsg);
201     }
202 
203   free (response_arg);
204 
205   return pex;
206 }
207 
208 void
fork_execute(const char * prog,char ** argv,bool use_atfile,const char * atsuffix)209 fork_execute (const char *prog, char **argv, bool use_atfile,
210 	      const char *atsuffix)
211 {
212   struct pex_obj *pex;
213 
214   pex = collect_execute (prog, argv, NULL, NULL,
215 			 PEX_LAST | PEX_SEARCH, use_atfile, atsuffix);
216   do_wait (prog, pex);
217 }
218 
219 /* Delete tempfiles.  */
220 
221 void
utils_cleanup(bool from_signal)222 utils_cleanup (bool from_signal)
223 {
224   static bool cleanup_done = false;
225 
226   if (cleanup_done)
227     return;
228 
229   /* Setting cleanup_done prevents an infinite loop if one of the
230      calls to maybe_unlink fails. */
231   cleanup_done = true;
232 
233   tool_cleanup (from_signal);
234 }
235