1 /* Scan linker error messages for missing template instantiations and provide
4 Copyright (C) 1995, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 Contributed by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
32 #define MAX_ITERATIONS 17
34 /* Defined in the automatically-generated underscore.c. */
35 extern int prepends_underscore;
37 static int tlink_verbose;
39 /* Hash table boilerplate for working with htab_t. We have hash tables
40 for symbol names, file names, and demangled symbols. */
42 typedef struct symbol_hash_entry
45 struct file_hash_entry *file;
51 typedef struct file_hash_entry
60 typedef struct demangled_hash_entry
66 /* Hash and comparison functions for these hash tables. */
68 static int hash_string_eq PARAMS ((const void *, const void *));
69 static hashval_t hash_string_hash PARAMS ((const void *));
72 hash_string_eq (s1_p, s2_p)
76 const char *const *s1 = (const char *const *) s1_p;
77 const char *s2 = (const char *) s2_p;
78 return strcmp (*s1, s2) == 0;
82 hash_string_hash (s_p)
85 const char *const *s = (const char *const *) s_p;
86 return (*htab_hash_string) (*s);
89 static htab_t symbol_table;
91 static struct symbol_hash_entry * symbol_hash_lookup PARAMS ((const char *,
93 static struct file_hash_entry * file_hash_lookup PARAMS ((const char *));
94 static struct demangled_hash_entry *
95 demangled_hash_lookup PARAMS ((const char *, int));
96 static void symbol_push PARAMS ((symbol *));
97 static symbol * symbol_pop PARAMS ((void));
98 static void file_push PARAMS ((file *));
99 static file * file_pop PARAMS ((void));
100 static void tlink_init PARAMS ((void));
101 static int tlink_execute PARAMS ((const char *, char **, const char *));
102 static char * frob_extension PARAMS ((const char *, const char *));
103 static char * obstack_fgets PARAMS ((FILE *, struct obstack *));
104 static char * tfgets PARAMS ((FILE *));
105 static char * pfgets PARAMS ((FILE *));
106 static void freadsym PARAMS ((FILE *, file *, int));
107 static void read_repo_file PARAMS ((file *));
108 static void maybe_tweak PARAMS ((char *, file *));
109 static int recompile_files PARAMS ((void));
110 static int read_repo_files PARAMS ((char **));
111 static void demangle_new_symbols PARAMS ((void));
112 static int scan_linker_output PARAMS ((const char *));
114 /* Look up an entry in the symbol hash table. */
116 static struct symbol_hash_entry *
117 symbol_hash_lookup (string, create)
122 e = htab_find_slot_with_hash (symbol_table, string,
123 (*htab_hash_string) (string),
124 create ? INSERT : NO_INSERT);
129 struct symbol_hash_entry *v;
130 *e = v = xcalloc (1, sizeof (*v));
131 v->key = xstrdup (string);
136 static htab_t file_table;
138 /* Look up an entry in the file hash table. */
140 static struct file_hash_entry *
141 file_hash_lookup (string)
145 e = htab_find_slot_with_hash (file_table, string,
146 (*htab_hash_string) (string),
150 struct file_hash_entry *v;
151 *e = v = xcalloc (1, sizeof (*v));
152 v->key = xstrdup (string);
157 static htab_t demangled_table;
159 /* Look up an entry in the demangled name hash table. */
161 static struct demangled_hash_entry *
162 demangled_hash_lookup (string, create)
167 e = htab_find_slot_with_hash (demangled_table, string,
168 (*htab_hash_string) (string),
169 create ? INSERT : NO_INSERT);
174 struct demangled_hash_entry *v;
175 *e = v = xcalloc (1, sizeof (*v));
176 v->key = xstrdup (string);
183 struct symbol_stack_entry
186 struct symbol_stack_entry *next;
188 struct obstack symbol_stack_obstack;
189 struct symbol_stack_entry *symbol_stack;
191 struct file_stack_entry
194 struct file_stack_entry *next;
196 struct obstack file_stack_obstack;
197 struct file_stack_entry *file_stack;
203 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
204 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
206 ep->next = symbol_stack;
213 struct symbol_stack_entry *ep = symbol_stack;
218 symbol_stack = ep->next;
219 obstack_free (&symbol_stack_obstack, ep);
227 struct file_stack_entry *ep;
232 ep = (struct file_stack_entry *) obstack_alloc
233 (&file_stack_obstack, sizeof (struct file_stack_entry));
235 ep->next = file_stack;
243 struct file_stack_entry *ep = file_stack;
248 file_stack = ep->next;
249 obstack_free (&file_stack_obstack, ep);
254 /* Other machinery. */
256 /* Initialize the tlink machinery. Called from do_tlink. */
263 symbol_table = htab_create (500, hash_string_hash, hash_string_eq,
265 file_table = htab_create (500, hash_string_hash, hash_string_eq,
267 demangled_table = htab_create (500, hash_string_hash, hash_string_eq,
270 obstack_begin (&symbol_stack_obstack, 0);
271 obstack_begin (&file_stack_obstack, 0);
273 p = getenv ("TLINK_VERBOSE");
275 tlink_verbose = atoi (p);
287 tlink_execute (prog, argv, redir)
292 collect_execute (prog, argv, redir);
293 return collect_wait (prog);
297 frob_extension (s, ext)
301 const char *p = strrchr (s, '/');
304 p = strrchr (p, '.');
308 obstack_grow (&temporary_obstack, s, p - s);
309 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
313 obstack_fgets (stream, ob)
318 while ((c = getc (stream)) != EOF && c != '\n')
319 obstack_1grow (ob, c);
320 if (obstack_object_size (ob) == 0)
322 obstack_1grow (ob, '\0');
323 return obstack_finish (ob);
330 return obstack_fgets (stream, &temporary_obstack);
337 return xstrdup (tfgets (stream));
340 /* Real tlink code. */
342 /* Subroutine of read_repo_file. We are reading the repo file for file F,
343 which is coming in on STREAM, and the symbol that comes next in STREAM
344 is offerred, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
346 XXX "provided" is unimplemented, both here and in the compiler. */
349 freadsym (stream, f, chosen)
357 const char *name = tfgets (stream);
358 sym = symbol_hash_lookup (name, true);
361 if (sym->file == NULL)
363 /* We didn't have this symbol already, so we choose this file. */
367 sym->chosen = chosen;
371 /* We want this file; cast aside any pretender. */
373 if (sym->chosen && sym->file != f)
375 if (sym->chosen == 1)
376 file_push (sym->file);
381 chosen = sym->chosen;
385 sym->chosen = chosen;
389 /* Read in the repo file denoted by F, and record all its information. */
396 FILE *stream = fopen (f->key, "r");
398 if (tlink_verbose >= 2)
399 fprintf (stderr, _("collect: reading %s\n"), f->key);
401 while (fscanf (stream, "%c ", &c) == 1)
406 f->args = pfgets (stream);
409 f->dir = pfgets (stream);
412 f->main = pfgets (stream);
415 freadsym (stream, f, 2);
418 freadsym (stream, f, 1);
421 freadsym (stream, f, 0);
424 obstack_free (&temporary_obstack, temporary_firstobj);
428 f->args = getenv ("COLLECT_GCC_OPTIONS");
433 /* We might want to modify LINE, which is a symbol line from file F. We do
434 this if either we saw an error message referring to the symbol in
435 question, or we have already allocated the symbol to another file and
436 this one wants to emit it as well. */
439 maybe_tweak (line, f)
443 symbol *sym = symbol_hash_lookup (line + 2, false);
445 if ((sym->file == f && sym->tweaking)
446 || (sym->file != f && line[0] == 'C'))
458 /* Update the repo files for each of the object files we have adjusted and
461 XXX Should this use collect_execute instead of system? */
468 putenv (xstrdup ("COMPILER_PATH="));
469 putenv (xstrdup ("LIBRARY_PATH="));
471 while ((f = file_pop ()) != NULL)
473 char *line, *command;
474 FILE *stream = fopen (f->key, "r");
475 const char *const outname = frob_extension (f->key, ".rnw");
476 FILE *output = fopen (outname, "w");
478 while ((line = tfgets (stream)) != NULL)
484 maybe_tweak (line, f);
486 fprintf (output, "%s\n", line);
490 rename (outname, f->key);
492 obstack_grow (&temporary_obstack, "cd ", 3);
493 obstack_grow (&temporary_obstack, f->dir, strlen (f->dir));
494 obstack_grow (&temporary_obstack, "; ", 2);
495 obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name));
496 obstack_1grow (&temporary_obstack, ' ');
497 obstack_grow (&temporary_obstack, f->args, strlen (f->args));
498 obstack_1grow (&temporary_obstack, ' ');
499 command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main));
502 fprintf (stderr, _("collect: recompiling %s\n"), f->main);
503 if (tlink_verbose >= 3)
504 fprintf (stderr, "%s\n", command);
506 if (system (command) != 0)
511 obstack_free (&temporary_obstack, temporary_firstobj);
516 /* The first phase of processing: determine which object files have
517 .rpo files associated with them, and read in the information. */
520 read_repo_files (object_lst)
523 char **object = object_lst;
525 for (; *object; object++)
530 /* Don't bother trying for ld flags. */
531 if (*object[0] == '-')
534 p = frob_extension (*object, ".rpo");
536 if (! file_exists (p))
539 f = file_hash_lookup (p);
544 if (file_stack != NULL && ! recompile_files ())
547 return (symbol_stack != NULL);
550 /* Add the demangled forms of any new symbols to the hash table. */
553 demangle_new_symbols ()
557 while ((sym = symbol_pop ()) != NULL)
560 const char *p = cplus_demangle (sym->key, DMGL_PARAMS | DMGL_ANSI);
565 dem = demangled_hash_lookup (p, true);
566 dem->mangled = sym->key;
570 /* Step through the output of the linker, in the file named FNAME, and
571 adjust the settings for each symbol encountered. */
574 scan_linker_output (fname)
577 FILE *stream = fopen (fname, "r");
580 while ((line = tfgets (stream)) != NULL)
586 while (*p && ISSPACE ((unsigned char) *p))
592 for (q = p; *q && ! ISSPACE ((unsigned char) *q); ++q)
595 /* Try the first word on the line. */
598 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
599 p += strlen (USER_LABEL_PREFIX);
603 sym = symbol_hash_lookup (p, false);
605 /* Some SVR4 linkers produce messages like
606 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
608 if (! sym && ! end && strstr (q + 1, "Undefined symbol: "))
610 char *p = strrchr (q + 1, ' ');
614 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
615 p += strlen (USER_LABEL_PREFIX);
616 sym = symbol_hash_lookup (p, false);
620 /* Try a mangled name in quotes. */
622 const char *oldq = q + 1;
626 /* First try `GNU style'. */
627 p = strchr (oldq, '`');
629 p++, q = strchr (p, '\'');
630 /* Then try "double quotes". */
631 else if (p = strchr (oldq, '"'), p)
632 p++, q = strchr (p, '"');
636 /* Don't let the strstr's below see the demangled name; we
637 might get spurious matches. */
640 /* powerpc64-linux references .foo when calling function foo. */
645 /* We need to check for certain error keywords here, or we would
646 mistakenly use GNU ld's "In function `foo':" message. */
647 if (q && (strstr (oldq, "ndefined")
648 || strstr (oldq, "nresolved")
649 || strstr (oldq, "nsatisfied")
650 || strstr (oldq, "ultiple")))
653 dem = demangled_hash_lookup (p, false);
655 sym = symbol_hash_lookup (dem->mangled, false);
658 if (!strncmp (p, USER_LABEL_PREFIX,
659 strlen (USER_LABEL_PREFIX)))
660 p += strlen (USER_LABEL_PREFIX);
661 sym = symbol_hash_lookup (p, false);
666 if (sym && sym->tweaked)
671 if (sym && !sym->tweaking)
673 if (tlink_verbose >= 2)
674 fprintf (stderr, _("collect: tweaking %s in %s\n"),
675 sym->key, sym->file->key);
677 file_push (sym->file);
680 obstack_free (&temporary_obstack, temporary_firstobj);
684 return (file_stack != NULL);
687 /* Entry point for tlink. Called from main in collect2.c.
689 Iteratively try to provide definitions for all the unresolved symbols
690 mentioned in the linker error messages.
692 LD_ARGV is an array of arguments for the linker.
693 OBJECT_LST is an array of object files that we may be able to recompile
694 to provide missing definitions. Currently ignored. */
697 do_tlink (ld_argv, object_lst)
698 char **ld_argv, **object_lst ATTRIBUTE_UNUSED;
700 int exit = tlink_execute ("ld", ld_argv, ldout);
708 /* Until collect does a better job of figuring out which are object
709 files, assume that everything on the command line could be. */
710 if (read_repo_files (ld_argv))
711 while (exit && i++ < MAX_ITERATIONS)
713 if (tlink_verbose >= 3)
715 demangle_new_symbols ();
716 if (! scan_linker_output (ldout))
718 if (! recompile_files ())
721 fprintf (stderr, _("collect: relinking\n"));
722 exit = tlink_execute ("ld", ld_argv, ldout);
730 error ("ld returned %d exit status", exit);