1 /* Scan linker error messages for missing template instantiations and provide
4 Copyright (C) 1995, 1998 Free Software Foundation, Inc.
5 Contributed by Jason Merrill (jason@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
31 #define MAX_ITERATIONS 17
33 /* Obstack allocation and deallocation routines. */
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
37 /* Defined in collect2.c. */
38 extern int vflag, debug;
40 extern char *c_file_name;
41 extern struct obstack temporary_obstack;
42 extern struct obstack permanent_obstack;
43 extern char * temporary_firstobj;
45 /* Defined in the automatically-generated underscore.c. */
46 extern int prepends_underscore;
48 static int tlink_verbose;
50 /* Hash table boilerplate for working with hash.[ch]. We have hash tables
51 for symbol names, file names, and demangled symbols. */
53 typedef struct symbol_hash_entry
55 struct hash_entry root;
56 struct file_hash_entry *file;
62 typedef struct file_hash_entry
64 struct hash_entry root;
71 typedef struct demangled_hash_entry
73 struct hash_entry root;
77 static struct hash_table symbol_table;
79 static struct hash_entry * symbol_hash_newfunc PARAMS ((struct hash_entry *,
82 static struct symbol_hash_entry * symbol_hash_lookup PARAMS ((const char *,
84 static struct hash_entry * file_hash_newfunc PARAMS ((struct hash_entry *,
87 static struct file_hash_entry * file_hash_lookup PARAMS ((const char *));
88 static struct hash_entry * demangled_hash_newfunc PARAMS ((struct hash_entry *,
91 static struct demangled_hash_entry *
92 demangled_hash_lookup PARAMS ((const char *, boolean));
93 static void symbol_push PARAMS ((symbol *));
94 static symbol * symbol_pop PARAMS ((void));
95 static void file_push PARAMS ((file *));
96 static file * file_pop PARAMS ((void));
97 static void tlink_init PARAMS ((void));
98 static int tlink_execute PARAMS ((char *, char **, char *));
99 static char * frob_extension PARAMS ((char *, const char *));
100 static char * obstack_fgets PARAMS ((FILE *, struct obstack *));
101 static char * tfgets PARAMS ((FILE *));
102 static char * pfgets PARAMS ((FILE *));
103 static void freadsym PARAMS ((FILE *, file *, int));
104 static void read_repo_file PARAMS ((file *));
105 static void maybe_tweak PARAMS ((char *, file *));
106 static int recompile_files PARAMS ((void));
107 static int read_repo_files PARAMS ((char **));
108 static void demangle_new_symbols PARAMS ((void));
109 static int scan_linker_output PARAMS ((const char *));
111 /* Create a new entry for the symbol hash table.
112 Passed to hash_table_init. */
114 static struct hash_entry *
115 symbol_hash_newfunc (entry, table, string)
116 struct hash_entry *entry;
117 struct hash_table *table;
118 hash_table_key string;
120 struct symbol_hash_entry *ret = (struct symbol_hash_entry *) entry;
123 ret = ((struct symbol_hash_entry *)
124 hash_allocate (table, sizeof (struct symbol_hash_entry)));
128 ret = ((struct symbol_hash_entry *)
129 hash_newfunc ((struct hash_entry *) ret, table, string));
134 return (struct hash_entry *) ret;
137 /* Look up an entry in the symbol hash table. */
139 static struct symbol_hash_entry *
140 symbol_hash_lookup (string, create)
144 return ((struct symbol_hash_entry *)
145 hash_lookup (&symbol_table, (hash_table_key) string,
146 create, &string_copy));
149 static struct hash_table file_table;
151 /* Create a new entry for the file hash table.
152 Passed to hash_table_init. */
154 static struct hash_entry *
155 file_hash_newfunc (entry, table, string)
156 struct hash_entry *entry;
157 struct hash_table *table;
158 hash_table_key string;
160 struct file_hash_entry *ret = (struct file_hash_entry *) entry;
163 ret = ((struct file_hash_entry *)
164 hash_allocate (table, sizeof (struct file_hash_entry)));
168 ret = ((struct file_hash_entry *)
169 hash_newfunc ((struct hash_entry *) ret, table, string));
174 return (struct hash_entry *) ret;
177 /* Look up an entry in the file hash table. */
179 static struct file_hash_entry *
180 file_hash_lookup (string)
183 return ((struct file_hash_entry *)
184 hash_lookup (&file_table, (hash_table_key) string, true,
188 static struct hash_table demangled_table;
190 /* Create a new entry for the demangled name hash table.
191 Passed to hash_table_init. */
193 static struct hash_entry *
194 demangled_hash_newfunc (entry, table, string)
195 struct hash_entry *entry;
196 struct hash_table *table;
197 hash_table_key string;
199 struct demangled_hash_entry *ret = (struct demangled_hash_entry *) entry;
202 ret = ((struct demangled_hash_entry *)
203 hash_allocate (table, sizeof (struct demangled_hash_entry)));
207 ret = ((struct demangled_hash_entry *)
208 hash_newfunc ((struct hash_entry *) ret, table, string));
210 return (struct hash_entry *) ret;
213 /* Look up an entry in the demangled name hash table. */
215 static struct demangled_hash_entry *
216 demangled_hash_lookup (string, create)
220 return ((struct demangled_hash_entry *)
221 hash_lookup (&demangled_table, (hash_table_key) string,
222 create, &string_copy));
227 struct symbol_stack_entry
230 struct symbol_stack_entry *next;
232 struct obstack symbol_stack_obstack;
233 struct symbol_stack_entry *symbol_stack;
235 struct file_stack_entry
238 struct file_stack_entry *next;
240 struct obstack file_stack_obstack;
241 struct file_stack_entry *file_stack;
247 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
248 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
250 ep->next = symbol_stack;
257 struct symbol_stack_entry *ep = symbol_stack;
262 symbol_stack = ep->next;
263 obstack_free (&symbol_stack_obstack, ep);
271 struct file_stack_entry *ep;
276 ep = (struct file_stack_entry *) obstack_alloc
277 (&file_stack_obstack, sizeof (struct file_stack_entry));
279 ep->next = file_stack;
287 struct file_stack_entry *ep = file_stack;
292 file_stack = ep->next;
293 obstack_free (&file_stack_obstack, ep);
298 /* Other machinery. */
300 /* Initialize the tlink machinery. Called from do_tlink. */
307 hash_table_init (&symbol_table, symbol_hash_newfunc, &string_hash,
309 hash_table_init (&file_table, file_hash_newfunc, &string_hash,
311 hash_table_init (&demangled_table, demangled_hash_newfunc,
312 &string_hash, &string_compare);
313 obstack_begin (&symbol_stack_obstack, 0);
314 obstack_begin (&file_stack_obstack, 0);
316 p = getenv ("TLINK_VERBOSE");
318 tlink_verbose = atoi (p);
330 tlink_execute (prog, argv, redir)
335 collect_execute (prog, argv, redir);
336 return collect_wait (prog);
340 frob_extension (s, ext)
344 char *p = rindex (s, '/');
351 obstack_grow (&temporary_obstack, s, p - s);
352 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
356 obstack_fgets (stream, ob)
361 while ((c = getc (stream)) != EOF && c != '\n')
362 obstack_1grow (ob, c);
363 if (obstack_object_size (ob) == 0)
365 obstack_1grow (ob, '\0');
366 return obstack_finish (ob);
373 return obstack_fgets (stream, &temporary_obstack);
380 return obstack_fgets (stream, &permanent_obstack);
383 /* Real tlink code. */
385 /* Subroutine of read_repo_file. We are reading the repo file for file F,
386 which is coming in on STREAM, and the symbol that comes next in STREAM
387 is offerred, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
389 XXX "provided" is unimplemented, both here and in the compiler. */
392 freadsym (stream, f, chosen)
400 char *name = tfgets (stream);
401 sym = symbol_hash_lookup (name, true);
404 if (sym->file == NULL)
406 /* We didn't have this symbol already, so we choose this file. */
410 sym->chosen = chosen;
414 /* We want this file; cast aside any pretender. */
416 if (sym->chosen && sym->file != f)
418 if (sym->chosen == 1)
419 file_push (sym->file);
424 chosen = sym->chosen;
428 sym->chosen = chosen;
432 /* Read in the repo file denoted by F, and record all its information. */
439 FILE *stream = fopen ((char*) f->root.key, "r");
441 if (tlink_verbose >= 2)
442 fprintf (stderr, "collect: reading %s\n",
443 (char*) f->root.key);
445 while (fscanf (stream, "%c ", &c) == 1)
450 f->args = pfgets (stream);
453 f->dir = pfgets (stream);
456 f->main = pfgets (stream);
459 freadsym (stream, f, 2);
462 freadsym (stream, f, 1);
465 freadsym (stream, f, 0);
468 obstack_free (&temporary_obstack, temporary_firstobj);
472 f->args = getenv ("COLLECT_GCC_OPTIONS");
477 /* We might want to modify LINE, which is a symbol line from file F. We do
478 this if either we saw an error message referring to the symbol in
479 question, or we have already allocated the symbol to another file and
480 this one wants to emit it as well. */
483 maybe_tweak (line, f)
487 symbol *sym = symbol_hash_lookup (line + 2, false);
489 if ((sym->file == f && sym->tweaking)
490 || (sym->file != f && line[0] == 'C'))
502 /* Update the repo files for each of the object files we have adjusted and
505 XXX Should this use collect_execute instead of system? */
512 while ((f = file_pop ()) != NULL)
514 char *line, *command;
515 FILE *stream = fopen ((char*) f->root.key, "r");
516 char *outname = frob_extension ((char*) f->root.key, ".rnw");
517 FILE *output = fopen (outname, "w");
519 while ((line = tfgets (stream)) != NULL)
525 maybe_tweak (line, f);
527 fprintf (output, "%s\n", line);
531 rename (outname, (char*) f->root.key);
533 obstack_grow (&temporary_obstack, "cd ", 3);
534 obstack_grow (&temporary_obstack, f->dir, strlen (f->dir));
535 obstack_grow (&temporary_obstack, "; ", 2);
536 obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name));
537 obstack_1grow (&temporary_obstack, ' ');
538 obstack_grow (&temporary_obstack, f->args, strlen (f->args));
539 obstack_1grow (&temporary_obstack, ' ');
540 command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main));
543 fprintf (stderr, "collect: recompiling %s\n", f->main);
544 if (tlink_verbose >= 3)
545 fprintf (stderr, "%s\n", command);
547 if (system (command) != 0)
552 obstack_free (&temporary_obstack, temporary_firstobj);
557 /* The first phase of processing: determine which object files have
558 .rpo files associated with them, and read in the information. */
561 read_repo_files (object_lst)
564 char **object = object_lst;
566 for (; *object; object++)
568 char *p = frob_extension (*object, ".rpo");
571 if (! file_exists (p))
574 f = file_hash_lookup (p);
579 if (file_stack != NULL && ! recompile_files ())
582 return (symbol_stack != NULL);
585 /* Add the demangled forms of any new symbols to the hash table. */
588 demangle_new_symbols ()
592 while ((sym = symbol_pop ()) != NULL)
595 char *p = cplus_demangle ((char*) sym->root.key,
596 DMGL_PARAMS | DMGL_ANSI);
601 dem = demangled_hash_lookup (p, true);
602 dem->mangled = (char*) sym->root.key;
606 /* Step through the output of the linker, in the file named FNAME, and
607 adjust the settings for each symbol encountered. */
610 scan_linker_output (fname)
613 FILE *stream = fopen (fname, "r");
616 while ((line = tfgets (stream)) != NULL)
622 while (*p && ISSPACE ((unsigned char)*p))
628 for (q = p; *q && ! ISSPACE ((unsigned char)*q); ++q)
631 /* Try the first word on the line. */
634 if (*p == '_' && prepends_underscore)
639 sym = symbol_hash_lookup (p, false);
642 /* Try a mangled name in quotes. */
648 /* First try `GNU style'. */
649 p = index (oldq, '`');
651 p++, q = index (p, '\'');
652 /* Then try "double quotes". */
653 else if (p = index (oldq, '"'), p)
654 p++, q = index (p, '"');
659 dem = demangled_hash_lookup (p, false);
661 sym = symbol_hash_lookup (dem->mangled, false);
663 sym = symbol_hash_lookup (p, false);
667 if (sym && sym->tweaked)
672 if (sym && !sym->tweaking)
674 if (tlink_verbose >= 2)
675 fprintf (stderr, "collect: tweaking %s in %s\n",
676 (char*) sym->root.key, (char*) sym->file->root.key);
678 file_push (sym->file);
681 obstack_free (&temporary_obstack, temporary_firstobj);
685 return (file_stack != NULL);
688 /* Entry point for tlink. Called from main in collect2.c.
690 Iteratively try to provide definitions for all the unresolved symbols
691 mentioned in the linker error messages.
693 LD_ARGV is an array of arguments for the linker.
694 OBJECT_LST is an array of object files that we may be able to recompile
695 to provide missing definitions. Currently ignored. */
698 do_tlink (ld_argv, object_lst)
699 char **ld_argv, **object_lst;
701 int exit = tlink_execute ("ld", ld_argv, ldout);
709 /* Until collect does a better job of figuring out which are object
710 files, assume that everything on the command line could be. */
711 if (read_repo_files (ld_argv))
712 while (exit && i++ < MAX_ITERATIONS)
714 if (tlink_verbose >= 3)
716 demangle_new_symbols ();
717 if (! scan_linker_output (ldout))
719 if (! recompile_files ())
722 fprintf (stderr, "collect: relinking\n");
723 exit = tlink_execute ("ld", ld_argv, ldout);
731 error ("ld returned %d exit status", exit);