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 /* Create a new entry for the symbol hash table.
80 Passed to hash_table_init. */
82 static struct hash_entry *
83 symbol_hash_newfunc (entry, table, string)
84 struct hash_entry *entry;
85 struct hash_table *table;
88 struct symbol_hash_entry *ret = (struct symbol_hash_entry *) entry;
91 ret = ((struct symbol_hash_entry *)
92 hash_allocate (table, sizeof (struct symbol_hash_entry)));
96 ret = ((struct symbol_hash_entry *)
97 hash_newfunc ((struct hash_entry *) ret, table,
98 (hash_table_key) string));
103 return (struct hash_entry *) ret;
106 /* Look up an entry in the symbol hash table. */
108 static struct symbol_hash_entry *
109 symbol_hash_lookup (string, create)
113 return ((struct symbol_hash_entry *)
114 hash_lookup (&symbol_table, (hash_table_key) string,
115 create, &string_copy));
118 static struct hash_table file_table;
120 /* Create a new entry for the file hash table.
121 Passed to hash_table_init. */
123 static struct hash_entry *
124 file_hash_newfunc (entry, table, string)
125 struct hash_entry *entry;
126 struct hash_table *table;
129 struct file_hash_entry *ret = (struct file_hash_entry *) entry;
132 ret = ((struct file_hash_entry *)
133 hash_allocate (table, sizeof (struct file_hash_entry)));
137 ret = ((struct file_hash_entry *)
138 hash_newfunc ((struct hash_entry *) ret, table,
139 (hash_table_key) string));
144 return (struct hash_entry *) ret;
147 /* Look up an entry in the file hash table. */
149 static struct file_hash_entry *
150 file_hash_lookup (string)
153 return ((struct file_hash_entry *)
154 hash_lookup (&file_table, (hash_table_key) string, true,
158 static struct hash_table demangled_table;
160 /* Create a new entry for the demangled name hash table.
161 Passed to hash_table_init. */
163 static struct hash_entry *
164 demangled_hash_newfunc (entry, table, string)
165 struct hash_entry *entry;
166 struct hash_table *table;
169 struct demangled_hash_entry *ret = (struct demangled_hash_entry *) entry;
172 ret = ((struct demangled_hash_entry *)
173 hash_allocate (table, sizeof (struct demangled_hash_entry)));
177 ret = ((struct demangled_hash_entry *)
178 hash_newfunc ((struct hash_entry *) ret, table,
179 (hash_table_key) string));
181 return (struct hash_entry *) ret;
184 /* Look up an entry in the demangled name hash table. */
186 static struct demangled_hash_entry *
187 demangled_hash_lookup (string, create)
191 return ((struct demangled_hash_entry *)
192 hash_lookup (&demangled_table, (hash_table_key) string,
193 create, &string_copy));
198 struct symbol_stack_entry
201 struct symbol_stack_entry *next;
203 struct obstack symbol_stack_obstack;
204 struct symbol_stack_entry *symbol_stack;
206 struct file_stack_entry
209 struct file_stack_entry *next;
211 struct obstack file_stack_obstack;
212 struct file_stack_entry *file_stack;
218 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
219 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
221 ep->next = symbol_stack;
228 struct symbol_stack_entry *ep = symbol_stack;
233 symbol_stack = ep->next;
234 obstack_free (&symbol_stack_obstack, ep);
242 struct file_stack_entry *ep;
247 ep = (struct file_stack_entry *) obstack_alloc
248 (&file_stack_obstack, sizeof (struct file_stack_entry));
250 ep->next = file_stack;
258 struct file_stack_entry *ep = file_stack;
263 file_stack = ep->next;
264 obstack_free (&file_stack_obstack, ep);
269 /* Other machinery. */
271 /* Initialize the tlink machinery. Called from do_tlink. */
278 hash_table_init (&symbol_table, symbol_hash_newfunc, &string_hash,
280 hash_table_init (&file_table, file_hash_newfunc, &string_hash,
282 hash_table_init (&demangled_table, demangled_hash_newfunc,
283 &string_hash, &string_compare);
284 obstack_begin (&symbol_stack_obstack, 0);
285 obstack_begin (&file_stack_obstack, 0);
287 p = getenv ("TLINK_VERBOSE");
289 tlink_verbose = atoi (p);
301 tlink_execute (prog, argv, redir)
306 collect_execute (prog, argv, redir);
307 return collect_wait (prog);
311 frob_extension (s, ext)
314 char *p = rindex (s, '/');
321 obstack_grow (&temporary_obstack, s, p - s);
322 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
326 obstack_fgets (stream, ob)
331 while ((c = getc (stream)) != EOF && c != '\n')
332 obstack_1grow (ob, c);
333 if (obstack_object_size (ob) == 0)
335 obstack_1grow (ob, '\0');
336 return obstack_finish (ob);
343 return obstack_fgets (stream, &temporary_obstack);
350 return obstack_fgets (stream, &permanent_obstack);
353 /* Real tlink code. */
355 /* Subroutine of read_repo_file. We are reading the repo file for file F,
356 which is coming in on STREAM, and the symbol that comes next in STREAM
357 is offerred, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
359 XXX "provided" is unimplemented, both here and in the compiler. */
362 freadsym (stream, f, chosen)
370 char *name = tfgets (stream);
371 sym = symbol_hash_lookup (name, true);
374 if (sym->file == NULL)
376 /* We didn't have this symbol already, so we choose this file. */
380 sym->chosen = chosen;
384 /* We want this file; cast aside any pretender. */
386 if (sym->chosen && sym->file != f)
388 if (sym->chosen == 1)
389 file_push (sym->file);
394 chosen = sym->chosen;
398 sym->chosen = chosen;
402 /* Read in the repo file denoted by F, and record all its information. */
409 FILE *stream = fopen ((char*) f->root.key, "r");
411 if (tlink_verbose >= 2)
412 fprintf (stderr, "collect: reading %s\n",
413 (char*) f->root.key);
415 while (fscanf (stream, "%c ", &c) == 1)
420 f->args = pfgets (stream);
423 f->dir = pfgets (stream);
426 f->main = pfgets (stream);
429 freadsym (stream, f, 2);
432 freadsym (stream, f, 1);
435 freadsym (stream, f, 0);
438 obstack_free (&temporary_obstack, temporary_firstobj);
442 f->args = getenv ("COLLECT_GCC_OPTIONS");
447 /* We might want to modify LINE, which is a symbol line from file F. We do
448 this if either we saw an error message referring to the symbol in
449 question, or we have already allocated the symbol to another file and
450 this one wants to emit it as well. */
453 maybe_tweak (line, f)
457 symbol *sym = symbol_hash_lookup (line + 2, false);
459 if ((sym->file == f && sym->tweaking)
460 || (sym->file != f && line[0] == 'C'))
472 /* Update the repo files for each of the object files we have adjusted and
475 XXX Should this use collect_execute instead of system? */
482 while ((f = file_pop ()) != NULL)
484 char *line, *command;
485 FILE *stream = fopen ((char*) f->root.key, "r");
486 char *outname = frob_extension ((char*) f->root.key, ".rnw");
487 FILE *output = fopen (outname, "w");
489 while ((line = tfgets (stream)) != NULL)
495 maybe_tweak (line, f);
497 fprintf (output, "%s\n", line);
501 rename (outname, (char*) f->root.key);
503 obstack_grow (&temporary_obstack, "cd ", 3);
504 obstack_grow (&temporary_obstack, f->dir, strlen (f->dir));
505 obstack_grow (&temporary_obstack, "; ", 2);
506 obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name));
507 obstack_1grow (&temporary_obstack, ' ');
508 obstack_grow (&temporary_obstack, f->args, strlen (f->args));
509 obstack_1grow (&temporary_obstack, ' ');
510 command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main));
513 fprintf (stderr, "collect: recompiling %s\n", f->main);
514 if (tlink_verbose >= 3)
515 fprintf (stderr, "%s\n", command);
517 if (system (command) != 0)
522 obstack_free (&temporary_obstack, temporary_firstobj);
527 /* The first phase of processing: determine which object files have
528 .rpo files associated with them, and read in the information. */
531 read_repo_files (object_lst)
534 char **object = object_lst;
536 for (; *object; object++)
538 char *p = frob_extension (*object, ".rpo");
541 if (! file_exists (p))
544 f = file_hash_lookup (p);
549 if (file_stack != NULL && ! recompile_files ())
552 return (symbol_stack != NULL);
555 /* Add the demangled forms of any new symbols to the hash table. */
558 demangle_new_symbols ()
562 while ((sym = symbol_pop ()) != NULL)
565 char *p = cplus_demangle ((char*) sym->root.key,
566 DMGL_PARAMS | DMGL_ANSI);
571 dem = demangled_hash_lookup (p, true);
572 dem->mangled = (char*) sym->root.key;
576 /* Step through the output of the linker, in the file named FNAME, and
577 adjust the settings for each symbol encountered. */
580 scan_linker_output (fname)
583 FILE *stream = fopen (fname, "r");
586 while ((line = tfgets (stream)) != NULL)
592 while (*p && ISSPACE ((unsigned char)*p))
598 for (q = p; *q && ! ISSPACE ((unsigned char)*q); ++q)
601 /* Try the first word on the line. */
604 if (*p == '_' && prepends_underscore)
609 sym = symbol_hash_lookup (p, false);
612 /* Try a mangled name in quotes. */
618 /* First try `GNU style'. */
619 p = index (oldq, '`');
621 p++, q = index (p, '\'');
622 /* Then try "double quotes". */
623 else if (p = index (oldq, '"'), p)
624 p++, q = index (p, '"');
629 dem = demangled_hash_lookup (p, false);
631 sym = symbol_hash_lookup (dem->mangled, false);
633 sym = symbol_hash_lookup (p, false);
637 if (sym && sym->tweaked)
642 if (sym && !sym->tweaking)
644 if (tlink_verbose >= 2)
645 fprintf (stderr, "collect: tweaking %s in %s\n",
646 (char*) sym->root.key, (char*) sym->file->root.key);
648 file_push (sym->file);
651 obstack_free (&temporary_obstack, temporary_firstobj);
655 return (file_stack != NULL);
658 /* Entry point for tlink. Called from main in collect2.c.
660 Iteratively try to provide definitions for all the unresolved symbols
661 mentioned in the linker error messages.
663 LD_ARGV is an array of arguments for the linker.
664 OBJECT_LST is an array of object files that we may be able to recompile
665 to provide missing definitions. Currently ignored. */
668 do_tlink (ld_argv, object_lst)
669 char **ld_argv, **object_lst;
671 int exit = tlink_execute ("ld", ld_argv, ldout);
679 /* Until collect does a better job of figuring out which are object
680 files, assume that everything on the command line could be. */
681 if (read_repo_files (ld_argv))
682 while (exit && i++ < MAX_ITERATIONS)
684 if (tlink_verbose >= 3)
686 demangle_new_symbols ();
687 if (! scan_linker_output (ldout))
689 if (! recompile_files ())
692 fprintf (stderr, "collect: relinking\n");
693 exit = tlink_execute ("ld", ld_argv, ldout);
701 error ("ld returned %d exit status", exit);