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