aboutsummaryrefslogtreecommitdiff
path: root/gcc/collect2.c
diff options
context:
space:
mode:
authorBernd Edlinger <bernd.edlinger@hotmail.de>2020-12-13 08:24:57 +0100
committerBernd Edlinger <bernd.edlinger@hotmail.de>2021-01-04 10:03:19 +0100
commite9f8a554efe497dd46b8953e580d65e5c023e50c (patch)
tree88aae5b2daa92d3a6ceaff7addbd960b656992eb /gcc/collect2.c
parentc48514bea610d9eaae783fec9d513a690723b0f0 (diff)
downloadgcc-e9f8a554efe497dd46b8953e580d65e5c023e50c.zip
gcc-e9f8a554efe497dd46b8953e580d65e5c023e50c.tar.gz
gcc-e9f8a554efe497dd46b8953e580d65e5c023e50c.tar.bz2
Fix -save-temp leaking lto files in /tmp
When linking with -flto and -save-temps, various temporary files are created in /tmp. The same happens when invoking the driver with @file parameter, and using -L or -I options. gcc: 2021-01-04 Bernd Edlinger <bernd.edlinger@hotmail.de> * collect-utils.c (collect_execute): Check dumppfx. * collect2.c (maybe_run_lto_and_relink, do_link): Pass atsuffix to collect_execute. (do_link): Add new parameter atsuffix. (main): Handle -dumpdir option. Skip one argument for -o, -isystem and -B options. * gcc.c (make_at_file): New helper function. (close_at_file): Use it. gcc/testsuite: 2021-01-04 Bernd Edlinger <bernd.edlinger@hotmail.de> * gcc.misc-tests/outputs.exp: Adjust testcase.
Diffstat (limited to 'gcc/collect2.c')
-rw-r--r--gcc/collect2.c43
1 files changed, 27 insertions, 16 deletions
diff --git a/gcc/collect2.c b/gcc/collect2.c
index 3a43a5a..3f097f9 100644
--- a/gcc/collect2.c
+++ b/gcc/collect2.c
@@ -644,7 +644,7 @@ maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
/* Run the LTO back end. */
pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH,
- at_file_supplied, NULL);
+ at_file_supplied, "lto_args");
{
int c;
FILE *stream;
@@ -728,7 +728,7 @@ maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
/* Run the linker again, this time replacing the object files
optimized by the LTO with the temporary file generated by the LTO. */
fork_execute ("ld", out_lto_ld_argv, HAVE_GNU_LD && at_file_supplied,
- NULL);
+ "ld_args");
/* We assume that temp files were created, and therefore we need to take
that into account (maybe run dsymutil). */
post_ld_pass (/*temp_file*/true);
@@ -740,7 +740,8 @@ maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
{
/* Our caller is relying on us to do the link
even though there is no LTO back end work to be done. */
- fork_execute ("ld", lto_ld_argv, HAVE_GNU_LD && at_file_supplied, NULL);
+ fork_execute ("ld", lto_ld_argv, HAVE_GNU_LD && at_file_supplied,
+ "ld_args");
/* No LTO objects were found, so no new temp file. */
post_ld_pass (/*temp_file*/false);
}
@@ -751,13 +752,13 @@ maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
LD_ARGV is an array of arguments for the linker. */
static void
-do_link (char **ld_argv)
+do_link (char **ld_argv, const char *atsuffix)
{
struct pex_obj *pex;
const char *prog = "ld";
pex = collect_execute (prog, ld_argv, NULL, NULL,
PEX_LAST | PEX_SEARCH,
- HAVE_GNU_LD && at_file_supplied, NULL);
+ HAVE_GNU_LD && at_file_supplied, atsuffix);
int ret = collect_wait (prog, pex);
if (ret)
{
@@ -973,10 +974,10 @@ main (int argc, char **argv)
{
/* Parse the output filename if it's given so that we can make
meaningful temp filenames. */
- if (argv[i][2] == '\0')
- output_file = argv[i+1];
- else
+ if (argv[i][2] != '\0')
output_file = &argv[i][2];
+ else if (argv[i+1] != NULL)
+ output_file = argv[++i];
}
#ifdef COLLECT_EXPORT_LIST
@@ -1022,6 +1023,12 @@ main (int argc, char **argv)
else if (strncmp (q, "-save-temps", 11) == 0)
/* FIXME: Honour =obj. */
save_temps = true;
+ else if (strcmp (q, "-dumpdir") == 0)
+ dumppfx = xstrdup (extract_string (&p));
+ else if (strcmp (q, "-o") == 0
+ || strcmp (q, "-B") == 0
+ || strcmp (q, "-isystem") == 0)
+ (void) extract_string (&p);
}
obstack_free (&temporary_obstack, temporary_firstobj);
@@ -1241,6 +1248,10 @@ main (int argc, char **argv)
*c_ptr++ = xstrdup (q);
}
}
+ else if (strcmp (q, "-o") == 0
+ || strcmp (q, "-dumpdir") == 0
+ || strcmp (q, "-isystem") == 0)
+ (void) extract_string (&p);
#ifdef COLLECT_EXPORT_LIST
/* Detect any invocation with -fvisibility. */
if (strncmp (q, "-fvisibility", 12) == 0)
@@ -1415,10 +1426,10 @@ main (int argc, char **argv)
#endif
case 'o':
- if (arg[2] == '\0')
- output_file = *ld1++ = *ld2++ = *++argv;
- else
+ if (arg[2] != '\0')
output_file = &arg[2];
+ else if (argv[1])
+ output_file = *ld1++ = *ld2++ = *++argv;
break;
case 'r':
@@ -1647,7 +1658,7 @@ main (int argc, char **argv)
functions from precise cross reference insertions by the compiler. */
if (early_exit || ld1_filter != SCAN_NOTHING)
- do_link (ld1_argv);
+ do_link (ld1_argv, "ld1_args");
if (early_exit)
{
@@ -1708,7 +1719,7 @@ main (int argc, char **argv)
/* Do link without additional code generation now if we didn't
do it earlier for scanning purposes. */
if (ld1_filter == SCAN_NOTHING)
- do_link (ld1_argv);
+ do_link (ld1_argv, "ld1_args");
if (lto_mode)
maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
@@ -1806,10 +1817,10 @@ main (int argc, char **argv)
/* Assemble the constructor and destructor tables.
Link the tables in with the rest of the program. */
- fork_execute ("gcc", c_argv, at_file_supplied, NULL);
+ fork_execute ("gcc", c_argv, at_file_supplied, "gcc_args");
#ifdef COLLECT_EXPORT_LIST
/* On AIX we must call link because of possible templates resolution. */
- do_link (ld2_argv);
+ do_link (ld2_argv, "ld2_args");
if (lto_mode)
maybe_run_lto_and_relink (ld2_argv, object_lst, object, false);
@@ -1819,7 +1830,7 @@ main (int argc, char **argv)
maybe_run_lto_and_relink (ld2_argv, object_lst, object, true);
else
{
- fork_execute ("ld", ld2_argv, HAVE_GNU_LD && at_file_supplied, NULL);
+ fork_execute ("ld", ld2_argv, HAVE_GNU_LD && at_file_supplied, "ld_args");
post_ld_pass (/*temp_file*/false);
}