Commit 33662270 by Rafael Avila de Espindola Committed by Rafael Espindola

lto-plugin.c (message): New variable.

2009-10-16  Rafael Avila de Espindola  <espindola@google.com>

	* lto-plugin.c (message): New variable.
	(check): New function.
	(parse_table_entry, translate, write_resolution,add_output_files,
	exec_lto_wrapper,claim_file_handler, onload): Use check instead of
	assert.
	(cleanup_handler): Use check instead of assert. Remove the arguments
	file if it exists.

From-SVN: r152906
parent 2ced58ea
2009-10-16 Rafael Avila de Espindola <espindola@google.com>
* lto-plugin.c (message): New variable.
(check): New function.
(parse_table_entry, translate, write_resolution,add_output_files,
exec_lto_wrapper,claim_file_handler, onload): Use check instead of
assert.
(cleanup_handler): Use check instead of assert. Remove the arguments
file if it exists.
2009-10-15 Rafael Avila de Espindola <espindola@google.com> 2009-10-15 Rafael Avila de Espindola <espindola@google.com>
* lto-plugin.c (resolution_file): New. * lto-plugin.c (resolution_file): New.
......
...@@ -82,6 +82,7 @@ static ld_plugin_get_symbols get_symbols; ...@@ -82,6 +82,7 @@ static ld_plugin_get_symbols get_symbols;
static ld_plugin_register_cleanup register_cleanup; static ld_plugin_register_cleanup register_cleanup;
static ld_plugin_add_input_file add_input_file; static ld_plugin_add_input_file add_input_file;
static ld_plugin_add_input_library add_input_library; static ld_plugin_add_input_library add_input_library;
static ld_plugin_message message;
static struct plugin_file_info *claimed_files = NULL; static struct plugin_file_info *claimed_files = NULL;
static unsigned int num_claimed_files = 0; static unsigned int num_claimed_files = 0;
...@@ -99,6 +100,23 @@ static bool debug; ...@@ -99,6 +100,23 @@ static bool debug;
static bool nop; static bool nop;
static char *resolution_file = NULL; static char *resolution_file = NULL;
static void
check (bool gate, enum ld_plugin_level level, const char *text)
{
if (gate)
return;
if (message)
message (level, text);
else
{
/* If there is no nicer way to inform the user, fallback to stderr. */
fprintf (stderr, "%s\n", text);
if (level == LDPL_FATAL)
abort ();
}
}
/* Parse an entry of the IL symbol table. The data to be parsed is pointed /* Parse an entry of the IL symbol table. The data to be parsed is pointed
by P and the result is written in ENTRY. The slot number is stored in SLOT. by P and the result is written in ENTRY. The slot number is stored in SLOT.
Returns the address of the next entry. */ Returns the address of the next entry. */
...@@ -142,12 +160,12 @@ parse_table_entry (char *p, struct ld_plugin_symbol *entry, uint32_t *slot) ...@@ -142,12 +160,12 @@ parse_table_entry (char *p, struct ld_plugin_symbol *entry, uint32_t *slot)
entry->comdat_key = strdup (entry->comdat_key); entry->comdat_key = strdup (entry->comdat_key);
t = *p; t = *p;
assert (t <= 4); check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
entry->def = translate_kind[t]; entry->def = translate_kind[t];
p++; p++;
t = *p; t = *p;
assert (t <= 3); check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
entry->visibility = translate_visibility[t]; entry->visibility = translate_visibility[t];
p++; p++;
...@@ -218,9 +236,9 @@ translate (Elf_Data *symtab, struct plugin_symtab *out) ...@@ -218,9 +236,9 @@ translate (Elf_Data *symtab, struct plugin_symtab *out)
{ {
n++; n++;
syms = realloc (syms, n * sizeof (struct ld_plugin_symbol)); syms = realloc (syms, n * sizeof (struct ld_plugin_symbol));
assert (syms); check (syms, LDPL_FATAL, "could not allocate memory");
slots = realloc (slots, n * sizeof (uint32_t)); slots = realloc (slots, n * sizeof (uint32_t));
assert (slots); check (slots, LDPL_FATAL, "could not allocate memory");
data = parse_table_entry (data, &syms[n - 1], &slots[n - 1]); data = parse_table_entry (data, &syms[n - 1], &slots[n - 1]);
} }
...@@ -297,7 +315,7 @@ write_resolution (void) ...@@ -297,7 +315,7 @@ write_resolution (void)
return; return;
f = fopen (resolution_file, "w"); f = fopen (resolution_file, "w");
assert (f); check (f, LDPL_FATAL, "could not open file");
fprintf (f, "%d\n", num_claimed_files); fprintf (f, "%d\n", num_claimed_files);
...@@ -329,7 +347,7 @@ write_resolution (void) ...@@ -329,7 +347,7 @@ write_resolution (void)
static void static void
add_output_files (FILE *f) add_output_files (FILE *f)
{ {
char fname[1000]; /* FIXME: Is this big enough? */ char fname[1000]; /* FIXME: Remove this restriction. */
for (;;) for (;;)
{ {
...@@ -339,7 +357,7 @@ add_output_files (FILE *f) ...@@ -339,7 +357,7 @@ add_output_files (FILE *f)
break; break;
len = strlen (s); len = strlen (s);
assert (s[len - 1] == '\n'); check (s[len - 1] == '\n', LDPL_FATAL, "file name too long");
s[len - 1] = '\0'; s[len - 1] = '\0';
num_output_files++; num_output_files++;
...@@ -367,16 +385,16 @@ exec_lto_wrapper (char *argv[]) ...@@ -367,16 +385,16 @@ exec_lto_wrapper (char *argv[])
/* 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. */
t = asprintf (&at_args, "@%s/arguments", temp_obj_dir_name); t = asprintf (&at_args, "@%s/arguments", temp_obj_dir_name);
assert (t >= 0); check (t >= 0, LDPL_FATAL, "asprintf failed");
args_name = at_args + 1; args_name = at_args + 1;
args = fopen (args_name, "w"); args = fopen (args_name, "w");
assert (args); check (args, LDPL_FATAL, "could not open arguments file");
t = writeargv (&argv[1], args); t = writeargv (&argv[1], args);
assert (t == 0); check (t == 0, LDPL_FATAL, "could not write arguments");
t = fclose (args); t = fclose (args);
assert (t == 0); check (t == 0, LDPL_FATAL, "could not close arguments file");
new_argv[0] = argv[0]; new_argv[0] = argv[0];
new_argv[1] = at_args; new_argv[1] = at_args;
...@@ -392,25 +410,26 @@ exec_lto_wrapper (char *argv[]) ...@@ -392,25 +410,26 @@ exec_lto_wrapper (char *argv[])
pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL); pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
assert (pex != NULL); check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t); errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
assert (errmsg == NULL); check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
assert (t == 0); check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
wrapper_output = pex_read_output (pex, 0); wrapper_output = pex_read_output (pex, 0);
assert (wrapper_output); check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
add_output_files (wrapper_output); add_output_files (wrapper_output);
t = pex_get_status (pex, 1, &status); t = pex_get_status (pex, 1, &status);
assert (t == 1); check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
assert (WIFEXITED (status) && WEXITSTATUS (status) == 0); check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
"lto-wrapper failed");
pex_free (pex); pex_free (pex);
t = unlink (args_name); t = unlink (args_name);
assert (t == 0); check (t == 0, LDPL_FATAL, "could not unlink arguments file");
free (at_args); free (at_args);
} }
...@@ -493,8 +512,12 @@ all_symbols_read_handler (void) ...@@ -493,8 +512,12 @@ all_symbols_read_handler (void)
static enum ld_plugin_status static enum ld_plugin_status
cleanup_handler (void) cleanup_handler (void)
{ {
/* Note: we cannot use LDPL_FATAL in here as that would produce
an infinite loop. */
int t; int t;
unsigned i; unsigned i;
char *arguments;
struct stat buf;
for (i = 0; i < num_claimed_files; i++) for (i = 0; i < num_claimed_files; i++)
{ {
...@@ -502,11 +525,23 @@ cleanup_handler (void) ...@@ -502,11 +525,23 @@ cleanup_handler (void)
if (info->temp) if (info->temp)
{ {
t = unlink (info->name); t = unlink (info->name);
assert (t == 0); check (t == 0, LDPL_ERROR, "could not unlink temporary file");
} }
} }
/* If we are being called from an error handler, it is possible
that the arguments file is still exists. */
t = asprintf (&arguments, "%s/arguments", temp_obj_dir_name);
check (t >= 0, LDPL_ERROR, "asprintf failed");
if (stat(arguments, &buf) == 0)
{
t = unlink (arguments);
check (t == 0, LDPL_ERROR, "could not unlink arguments file");
}
free (arguments);
t = rmdir (temp_obj_dir_name); t = rmdir (temp_obj_dir_name);
assert (t == 0); check (t == 0, LDPL_ERROR, "could not remove temporary directory");
free_2 (); free_2 ();
return LDPS_OK; return LDPS_OK;
...@@ -535,13 +570,13 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) ...@@ -535,13 +570,13 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
char *objname; char *objname;
int t = asprintf (&objname, "%s/obj%d.o", int t = asprintf (&objname, "%s/obj%d.o",
temp_obj_dir_name, objnum); temp_obj_dir_name, objnum);
assert (t >= 0); check (t >= 0, LDPL_FATAL, "asprintf failed");
objnum++; objnum++;
fd = open (objname, O_RDWR | O_CREAT, 0666); fd = open (objname, O_RDWR | O_CREAT, 0666);
assert (fd > 0); check (fd > 0, LDPL_FATAL, "could not open/create temporary file");
offset = lseek (file->fd, file->offset, SEEK_SET); offset = lseek (file->fd, file->offset, SEEK_SET);
assert (offset == file->offset); check (offset == file->offset, LDPL_FATAL, "could not seek");
while (size > 0) while (size > 0)
{ {
ssize_t r, written; ssize_t r, written;
...@@ -549,7 +584,7 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) ...@@ -549,7 +584,7 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
off_t s = sizeof (buf) < size ? sizeof (buf) : size; off_t s = sizeof (buf) < size ? sizeof (buf) : size;
r = read (file->fd, buf, s); r = read (file->fd, buf, s);
written = write (fd, buf, r); written = write (fd, buf, r);
assert (written = r); check (written == r, LDPL_FATAL, "could not write to temporary file");
size -= r; size -= r;
} }
lto_file.name = objname; lto_file.name = objname;
...@@ -579,7 +614,7 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) ...@@ -579,7 +614,7 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
status = add_symbols (file->handle, lto_file.symtab.nsyms, status = add_symbols (file->handle, lto_file.symtab.nsyms,
lto_file.symtab.syms); lto_file.symtab.syms);
assert (status == LDPS_OK); check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
*claimed = 1; *claimed = 1;
num_claimed_files++; num_claimed_files++;
...@@ -594,7 +629,7 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) ...@@ -594,7 +629,7 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
if (file->offset != 0) if (file->offset != 0)
{ {
int t = unlink (lto_file.name); int t = unlink (lto_file.name);
assert (t == 0); check (t == 0, LDPL_FATAL, "could not unlink file");
} }
free (lto_file.name); free (lto_file.name);
...@@ -646,13 +681,16 @@ onload (struct ld_plugin_tv *tv) ...@@ -646,13 +681,16 @@ onload (struct ld_plugin_tv *tv)
char *t; char *t;
unsigned version = elf_version (EV_CURRENT); unsigned version = elf_version (EV_CURRENT);
assert (version != EV_NONE); check (version != EV_NONE, LDPL_FATAL, "invalid ELF version");
p = tv; p = tv;
while (p->tv_tag) while (p->tv_tag)
{ {
switch (p->tv_tag) switch (p->tv_tag)
{ {
case LDPT_MESSAGE:
message = p->tv_u.tv_message;
break;
case LDPT_REGISTER_CLAIM_FILE_HOOK: case LDPT_REGISTER_CLAIM_FILE_HOOK:
register_claim_file = p->tv_u.tv_register_claim_file; register_claim_file = p->tv_u.tv_register_claim_file;
break; break;
...@@ -683,22 +721,25 @@ onload (struct ld_plugin_tv *tv) ...@@ -683,22 +721,25 @@ onload (struct ld_plugin_tv *tv)
p++; p++;
} }
assert (register_claim_file); check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
assert (add_symbols); check (add_symbols, LDPL_FATAL, "add_symbols not found");
status = register_claim_file (claim_file_handler); status = register_claim_file (claim_file_handler);
assert (status == LDPS_OK); check (status == LDPS_OK, LDPL_FATAL,
"could not register the claim_file callback");
if (register_cleanup) if (register_cleanup)
{ {
status = register_cleanup (cleanup_handler); status = register_cleanup (cleanup_handler);
assert (status == LDPS_OK); check (status == LDPS_OK, LDPL_FATAL,
"could not register the cleanup callback");
} }
if (register_all_symbols_read) if (register_all_symbols_read)
{ {
assert (get_symbols); check (get_symbols, LDPL_FATAL, "get_symbols not found");
status = register_all_symbols_read (all_symbols_read_handler); status = register_all_symbols_read (all_symbols_read_handler);
assert (status == LDPS_OK); check (status == LDPS_OK, LDPL_FATAL,
"could not register the all_symbols_read callback");
} }
temp_obj_dir_name = strdup ("tmp_objectsXXXXXX"); temp_obj_dir_name = strdup ("tmp_objectsXXXXXX");
......
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