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 code. */
52 typedef struct symbol_hash_entry
54 struct hash_entry root;
55 struct file_hash_entry *file;
61 typedef struct file_hash_entry
63 struct hash_entry root;
70 typedef struct demangled_hash_entry
72 struct hash_entry root;
76 static struct hash_table symbol_table;
78 static struct hash_entry *
79 symbol_hash_newfunc (entry, table, string)
80 struct hash_entry *entry;
81 struct hash_table *table;
84 struct symbol_hash_entry *ret = (struct symbol_hash_entry *) entry;
87 ret = ((struct symbol_hash_entry *)
88 hash_allocate (table, sizeof (struct symbol_hash_entry)));
92 ret = ((struct symbol_hash_entry *)
93 hash_newfunc ((struct hash_entry *) ret, table,
94 (hash_table_key) string));
99 return (struct hash_entry *) ret;
102 static struct symbol_hash_entry *
103 symbol_hash_lookup (string, create)
107 return ((struct symbol_hash_entry *)
108 hash_lookup (&symbol_table, (hash_table_key) string,
109 create, &string_copy));
112 static struct hash_table file_table;
114 static struct hash_entry *
115 file_hash_newfunc (entry, table, string)
116 struct hash_entry *entry;
117 struct hash_table *table;
120 struct file_hash_entry *ret = (struct file_hash_entry *) entry;
123 ret = ((struct file_hash_entry *)
124 hash_allocate (table, sizeof (struct file_hash_entry)));
128 ret = ((struct file_hash_entry *)
129 hash_newfunc ((struct hash_entry *) ret, table,
130 (hash_table_key) string));
135 return (struct hash_entry *) ret;
138 static struct file_hash_entry *
139 file_hash_lookup (string)
142 return ((struct file_hash_entry *)
143 hash_lookup (&file_table, (hash_table_key) string, true,
147 static struct hash_table demangled_table;
149 static struct hash_entry *
150 demangled_hash_newfunc (entry, table, string)
151 struct hash_entry *entry;
152 struct hash_table *table;
155 struct demangled_hash_entry *ret = (struct demangled_hash_entry *) entry;
158 ret = ((struct demangled_hash_entry *)
159 hash_allocate (table, sizeof (struct demangled_hash_entry)));
163 ret = ((struct demangled_hash_entry *)
164 hash_newfunc ((struct hash_entry *) ret, table,
165 (hash_table_key) string));
167 return (struct hash_entry *) ret;
170 static struct demangled_hash_entry *
171 demangled_hash_lookup (string, create)
175 return ((struct demangled_hash_entry *)
176 hash_lookup (&demangled_table, (hash_table_key) string,
177 create, &string_copy));
182 struct symbol_stack_entry
185 struct symbol_stack_entry *next;
187 struct obstack symbol_stack_obstack;
188 struct symbol_stack_entry *symbol_stack;
190 struct file_stack_entry
193 struct file_stack_entry *next;
195 struct obstack file_stack_obstack;
196 struct file_stack_entry *file_stack;
202 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
203 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
205 ep->next = symbol_stack;
212 struct symbol_stack_entry *ep = symbol_stack;
217 symbol_stack = ep->next;
218 obstack_free (&symbol_stack_obstack, ep);
226 struct file_stack_entry *ep;
231 ep = (struct file_stack_entry *) obstack_alloc
232 (&file_stack_obstack, sizeof (struct file_stack_entry));
234 ep->next = file_stack;
242 struct file_stack_entry *ep = file_stack;
247 file_stack = ep->next;
248 obstack_free (&file_stack_obstack, ep);
253 /* Other machinery. */
260 hash_table_init (&symbol_table, symbol_hash_newfunc, &string_hash,
262 hash_table_init (&file_table, file_hash_newfunc, &string_hash,
264 hash_table_init (&demangled_table, demangled_hash_newfunc,
265 &string_hash, &string_compare);
266 obstack_begin (&symbol_stack_obstack, 0);
267 obstack_begin (&file_stack_obstack, 0);
269 p = getenv ("TLINK_VERBOSE");
271 tlink_verbose = atoi (p);
283 tlink_execute (prog, argv, redir)
288 collect_execute (prog, argv, redir);
289 return collect_wait (prog);
293 frob_extension (s, ext)
296 char *p = rindex (s, '/');
303 obstack_grow (&temporary_obstack, s, p - s);
304 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
308 obstack_fgets (stream, ob)
313 while ((c = getc (stream)) != EOF && c != '\n')
314 obstack_1grow (ob, c);
315 if (obstack_object_size (ob) == 0)
317 obstack_1grow (ob, '\0');
318 return obstack_finish (ob);
325 return obstack_fgets (stream, &temporary_obstack);
332 return obstack_fgets (stream, &permanent_obstack);
335 /* Real tlink code. */
338 freadsym (stream, f, chosen)
346 char *name = tfgets (stream);
347 sym = symbol_hash_lookup (name, true);
350 if (sym->file == NULL)
354 sym->chosen = chosen;
358 if (sym->chosen && sym->file != f)
360 if (sym->chosen == 1)
361 file_push (sym->file);
366 chosen = sym->chosen;
370 sym->chosen = chosen;
379 FILE *stream = fopen ((char*) f->root.key, "r");
381 if (tlink_verbose >= 2)
382 fprintf (stderr, "collect: reading %s\n",
383 (char*) f->root.key);
385 while (fscanf (stream, "%c ", &c) == 1)
390 f->args = pfgets (stream);
393 f->dir = pfgets (stream);
396 f->main = pfgets (stream);
399 freadsym (stream, f, 2);
402 freadsym (stream, f, 1);
405 freadsym (stream, f, 0);
408 obstack_free (&temporary_obstack, temporary_firstobj);
412 f->args = getenv ("COLLECT_GCC_OPTIONS");
418 maybe_tweak (line, f)
422 symbol *sym = symbol_hash_lookup (line + 2, false);
424 if ((sym->file == f && sym->tweaking)
425 || (sym->file != f && line[0] == 'C'))
442 while ((f = file_pop ()) != NULL)
444 char *line, *command;
445 FILE *stream = fopen ((char*) f->root.key, "r");
446 char *outname = frob_extension ((char*) f->root.key, ".rnw");
447 FILE *output = fopen (outname, "w");
449 while ((line = tfgets (stream)) != NULL)
455 maybe_tweak (line, f);
457 fprintf (output, "%s\n", line);
461 rename (outname, (char*) f->root.key);
463 obstack_grow (&temporary_obstack, "cd ", 3);
464 obstack_grow (&temporary_obstack, f->dir, strlen (f->dir));
465 obstack_grow (&temporary_obstack, "; ", 2);
466 obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name));
467 obstack_1grow (&temporary_obstack, ' ');
468 obstack_grow (&temporary_obstack, f->args, strlen (f->args));
469 obstack_1grow (&temporary_obstack, ' ');
470 command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main));
473 fprintf (stderr, "collect: recompiling %s\n", f->main);
474 if (tlink_verbose >= 3)
475 fprintf (stderr, "%s\n", command);
477 if (system (command) != 0)
482 obstack_free (&temporary_obstack, temporary_firstobj);
488 read_repo_files (object_lst)
491 char **object = object_lst;
493 for (; *object; object++)
495 char *p = frob_extension (*object, ".rpo");
498 if (! file_exists (p))
501 f = file_hash_lookup (p);
506 if (file_stack != NULL && ! recompile_files ())
509 return (symbol_stack != NULL);
513 demangle_new_symbols ()
517 while ((sym = symbol_pop ()) != NULL)
520 char *p = cplus_demangle ((char*) sym->root.key,
521 DMGL_PARAMS | DMGL_ANSI);
526 dem = demangled_hash_lookup (p, true);
527 dem->mangled = (char*) sym->root.key;
532 scan_linker_output (fname)
535 FILE *stream = fopen (fname, "r");
538 while ((line = tfgets (stream)) != NULL)
544 while (*p && ISSPACE ((unsigned char)*p))
550 for (q = p; *q && ! ISSPACE ((unsigned char)*q); ++q)
553 /* Try the first word on the line. */
556 if (*p == '_' && prepends_underscore)
561 sym = symbol_hash_lookup (p, false);
564 /* Try a mangled name in quotes. */
570 /* First try `GNU style'. */
571 p = index (oldq, '`');
573 p++, q = index (p, '\'');
574 /* Then try "double quotes". */
575 else if (p = index (oldq, '"'), p)
576 p++, q = index (p, '"');
581 dem = demangled_hash_lookup (p, false);
583 sym = symbol_hash_lookup (dem->mangled, false);
585 sym = symbol_hash_lookup (p, false);
589 if (sym && sym->tweaked)
594 if (sym && !sym->tweaking)
596 if (tlink_verbose >= 2)
597 fprintf (stderr, "collect: tweaking %s in %s\n",
598 (char*) sym->root.key, (char*) sym->file->root.key);
600 file_push (sym->file);
603 obstack_free (&temporary_obstack, temporary_firstobj);
607 return (file_stack != NULL);
611 do_tlink (ld_argv, object_lst)
612 char **ld_argv, **object_lst;
614 int exit = tlink_execute ("ld", ld_argv, ldout);
622 /* Until collect does a better job of figuring out which are object
623 files, assume that everything on the command line could be. */
624 if (read_repo_files (ld_argv))
625 while (exit && i++ < MAX_ITERATIONS)
627 if (tlink_verbose >= 3)
629 demangle_new_symbols ();
630 if (! scan_linker_output (ldout))
632 if (! recompile_files ())
635 fprintf (stderr, "collect: relinking\n");
636 exit = tlink_execute ("ld", ld_argv, ldout);
644 error ("ld returned %d exit status", exit);