Commit c101cff8 by Iain Sandoe Committed by Iain Sandoe

lto-plugin - support -save-temps, -v, --version.

This patch makes the lto-plugin follow the same approach
to save-temps as collect2. 

-save-temps causes the temp file to be named meaningfully,
and for the relevant input files to be saved in CWD.

-v, —version causes the save actions to be output to stderr.

one can get this to happen by just putting -save-temps, -v on
the regular link line or (for compatibility with the way the
 -debug flag works) by appending -plugin-opt=-save-temps, etc.

lto-plugin/

2019-05-15  Iain Sandoe  <iain@sandoe.co.uk>

	* lto-plugin.c (exec_lto_wrapper): Make the wrapper
	arguments filename more user-friendly.
	(file_exists, maybe_unlink): New.
	(cleanup_handler): Use maybe unlink to handle the
	case when temps should be saved.
	(process_option): Look for -v, —-version, -save-temps.
	(onload): Record the linker output file name.
	Check for -v, —-version, -save-temps in the GCC collect
	options environment.

From-SVN: r271202
parent debe1ba0
2019-05-15 Iain Sandoe <iain@sandoe.co.uk>
* lto-plugin.c (exec_lto_wrapper): Make the wrapper
arguments filename more user-friendly.
(file_exists, maybe_unlink): New.
(cleanup_handler): Use maybe unlink to handle the
case when temps should be saved.
(process_option): Look for -v, —-version, -save-temps.
(onload): Record the linker output file name.
Check for -v, —-version, -save-temps in the GCC collect
options environment.
2019-02-26 Martin Liska <mliska@suse.cz> 2019-02-26 Martin Liska <mliska@suse.cz>
* lto-symtab.c: Remove. * lto-symtab.c: Remove.
......
...@@ -41,6 +41,7 @@ along with this program; see the file COPYING3. If not see ...@@ -41,6 +41,7 @@ along with this program; see the file COPYING3. If not see
#if HAVE_STDINT_H #if HAVE_STDINT_H
#include <stdint.h> #include <stdint.h>
#endif #endif
#include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
...@@ -184,12 +185,15 @@ static int lto_wrapper_num_args; ...@@ -184,12 +185,15 @@ static int lto_wrapper_num_args;
static char **pass_through_items = NULL; static char **pass_through_items = NULL;
static unsigned int num_pass_through_items; static unsigned int num_pass_through_items;
static char debug; static bool debug;
static bool save_temps;
static bool verbose;
static char nop; static char nop;
static char *resolution_file = NULL; static char *resolution_file = NULL;
static enum ld_plugin_output_file_type linker_output; static enum ld_plugin_output_file_type linker_output;
static int linker_output_set; static int linker_output_set;
static int linker_output_known; static int linker_output_known;
static const char *link_output_name = NULL;
/* The version of gold being used, or -1 if not gold. The number is /* The version of gold being used, or -1 if not gold. The number is
MAJOR * 100 + MINOR. */ MAJOR * 100 + MINOR. */
...@@ -560,8 +564,17 @@ exec_lto_wrapper (char *argv[]) ...@@ -560,8 +564,17 @@ exec_lto_wrapper (char *argv[])
struct pex_obj *pex; struct pex_obj *pex;
const char *errmsg; const char *errmsg;
/* Write argv to a file to avoid a command line that is too long. */ /* Write argv to a file to avoid a command line that is too long
arguments_file_name = make_temp_file (""); Save the file locally on save-temps. */
if (save_temps && link_output_name)
{
arguments_file_name = (char *) xmalloc (strlen (link_output_name)
+ sizeof (".lto_wrapper_args") + 1);
strcpy (arguments_file_name, link_output_name);
strcat (arguments_file_name, ".lto_wrapper_args");
}
else
arguments_file_name = make_temp_file (".lto_wrapper_args");
check (arguments_file_name, LDPL_FATAL, check (arguments_file_name, LDPL_FATAL,
"Failed to generate a temorary file name"); "Failed to generate a temorary file name");
...@@ -579,15 +592,21 @@ exec_lto_wrapper (char *argv[]) ...@@ -579,15 +592,21 @@ exec_lto_wrapper (char *argv[])
for (i = 1; argv[i]; i++) for (i = 1; argv[i]; i++)
{ {
char *a = argv[i]; char *a = argv[i];
/* Check the input argument list for a verbose marker too. */
if (a[0] == '-' && a[1] == 'v' && a[2] == '\0') if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
{ {
for (i = 0; argv[i]; i++) verbose = true;
fprintf (stderr, "%s ", argv[i]);
fprintf (stderr, "\n");
break; break;
} }
} }
if (verbose)
{
for (i = 0; argv[i]; i++)
fprintf (stderr, "%s ", argv[i]);
fprintf (stderr, "\n");
}
new_argv[0] = argv[0]; new_argv[0] = argv[0];
new_argv[1] = at_args; new_argv[1] = at_args;
new_argv[2] = NULL; new_argv[2] = NULL;
...@@ -599,7 +618,6 @@ exec_lto_wrapper (char *argv[]) ...@@ -599,7 +618,6 @@ exec_lto_wrapper (char *argv[])
fprintf (stderr, "\n"); fprintf (stderr, "\n");
} }
pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL); pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper"); check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
...@@ -759,6 +777,29 @@ all_symbols_read_handler (void) ...@@ -759,6 +777,29 @@ all_symbols_read_handler (void)
return LDPS_OK; return LDPS_OK;
} }
/* Helper, as used in collect2. */
static int
file_exists (const char *name)
{
return access (name, R_OK) == 0;
}
/* Unlink FILE unless we have save-temps set.
Note that we're saving files if verbose output is set. */
static void
maybe_unlink (const char *file)
{
if (save_temps && file_exists (file))
{
if (verbose)
fprintf (stderr, "[Leaving %s]\n", file);
return;
}
unlink_if_ordinary (file);
}
/* Remove temporary files at the end of the link. */ /* Remove temporary files at the end of the link. */
static enum ld_plugin_status static enum ld_plugin_status
...@@ -771,16 +812,10 @@ cleanup_handler (void) ...@@ -771,16 +812,10 @@ cleanup_handler (void)
return LDPS_OK; return LDPS_OK;
if (arguments_file_name) if (arguments_file_name)
{ maybe_unlink (arguments_file_name);
t = unlink (arguments_file_name);
check (t == 0, LDPL_FATAL, "could not unlink arguments file");
}
for (i = 0; i < num_output_files; i++) for (i = 0; i < num_output_files; i++)
{ maybe_unlink (output_files[i]);
t = unlink (output_files[i]);
check (t == 0, LDPL_FATAL, "could not unlink output file");
}
free_2 (); free_2 ();
return LDPS_OK; return LDPS_OK;
...@@ -1143,7 +1178,12 @@ process_option (const char *option) ...@@ -1143,7 +1178,12 @@ process_option (const char *option)
if (strcmp (option, "-linker-output-known") == 0) if (strcmp (option, "-linker-output-known") == 0)
linker_output_known = 1; linker_output_known = 1;
if (strcmp (option, "-debug") == 0) if (strcmp (option, "-debug") == 0)
debug = 1; debug = true;
else if ((strcmp (option, "-v") == 0)
|| (strcmp (option, "--verbose") == 0))
verbose = true;
else if (strcmp (option, "-save-temps") == 0)
save_temps = true;
else if (strcmp (option, "-nop") == 0) else if (strcmp (option, "-nop") == 0)
nop = 1; nop = 1;
else if (!strncmp (option, "-pass-through=", strlen("-pass-through="))) else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
...@@ -1180,6 +1220,8 @@ process_option (const char *option) ...@@ -1180,6 +1220,8 @@ process_option (const char *option)
if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0) if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
resolution_file = opt + sizeof ("-fresolution=") - 1; resolution_file = opt + sizeof ("-fresolution=") - 1;
} }
save_temps = save_temps || debug;
verbose = verbose || debug;
} }
/* Called by gold after loading the plugin. TV is the transfer vector. */ /* Called by gold after loading the plugin. TV is the transfer vector. */
...@@ -1232,6 +1274,10 @@ onload (struct ld_plugin_tv *tv) ...@@ -1232,6 +1274,10 @@ onload (struct ld_plugin_tv *tv)
linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val; linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val;
linker_output_set = 1; linker_output_set = 1;
break; break;
case LDPT_OUTPUT_NAME:
/* We only use this to make user-friendly temp file names. */
link_output_name = p->tv_u.tv_string;
break;
default: default:
break; break;
} }
...@@ -1259,12 +1305,21 @@ onload (struct ld_plugin_tv *tv) ...@@ -1259,12 +1305,21 @@ onload (struct ld_plugin_tv *tv)
"could not register the all_symbols_read callback"); "could not register the all_symbols_read callback");
} }
/* Support -fno-use-linker-plugin by failing to load the plugin
for the case where it is auto-loaded by BFD. */
char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS"); char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
if (collect_gcc_options if (collect_gcc_options)
&& strstr (collect_gcc_options, "'-fno-use-linker-plugin'")) {
return LDPS_ERR; /* Support -fno-use-linker-plugin by failing to load the plugin
for the case where it is auto-loaded by BFD. */
if (strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
return LDPS_ERR;
if ( strstr (collect_gcc_options, "'-save-temps'"))
save_temps = true;
if (strstr (collect_gcc_options, "'-v'")
|| strstr (collect_gcc_options, "'--verbose'"))
verbose = true;
}
return LDPS_OK; return LDPS_OK;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment