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, 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #define MAX_ITERATIONS 17
32 /* Obstack allocation and deallocation routines. */
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
36 /* Defined in collect2.c. */
37 extern int vflag, debug;
39 extern char *c_file_name;
40 extern struct obstack temporary_obstack;
41 extern struct obstack permanent_obstack;
42 extern char * temporary_firstobj;
44 /* Defined in the automatically-generated underscore.c. */
45 extern int prepends_underscore;
47 static int tlink_verbose;
49 /* Hash table code. */
51 typedef struct symbol_hash_entry
53 struct hash_entry root;
54 struct file_hash_entry *file;
60 typedef struct file_hash_entry
62 struct hash_entry root;
69 typedef struct demangled_hash_entry
71 struct hash_entry root;
75 static struct hash_table symbol_table;
77 static struct hash_entry *
78 symbol_hash_newfunc (entry, table, string)
79 struct hash_entry *entry;
80 struct hash_table *table;
83 struct symbol_hash_entry *ret = (struct symbol_hash_entry *) entry;
86 ret = ((struct symbol_hash_entry *)
87 hash_allocate (table, sizeof (struct symbol_hash_entry)));
91 ret = ((struct symbol_hash_entry *)
92 hash_newfunc ((struct hash_entry *) ret, table,
93 (hash_table_key) string));
98 return (struct hash_entry *) ret;
101 static struct symbol_hash_entry *
102 symbol_hash_lookup (string, create)
106 return ((struct symbol_hash_entry *)
107 hash_lookup (&symbol_table, (hash_table_key) string,
108 create, &string_copy));
111 static struct hash_table file_table;
113 static struct hash_entry *
114 file_hash_newfunc (entry, table, string)
115 struct hash_entry *entry;
116 struct hash_table *table;
119 struct file_hash_entry *ret = (struct file_hash_entry *) entry;
122 ret = ((struct file_hash_entry *)
123 hash_allocate (table, sizeof (struct file_hash_entry)));
127 ret = ((struct file_hash_entry *)
128 hash_newfunc ((struct hash_entry *) ret, table,
129 (hash_table_key) string));
134 return (struct hash_entry *) ret;
137 static struct file_hash_entry *
138 file_hash_lookup (string)
141 return ((struct file_hash_entry *)
142 hash_lookup (&file_table, (hash_table_key) string, true,
146 static struct hash_table demangled_table;
148 static struct hash_entry *
149 demangled_hash_newfunc (entry, table, string)
150 struct hash_entry *entry;
151 struct hash_table *table;
154 struct demangled_hash_entry *ret = (struct demangled_hash_entry *) entry;
157 ret = ((struct demangled_hash_entry *)
158 hash_allocate (table, sizeof (struct demangled_hash_entry)));
162 ret = ((struct demangled_hash_entry *)
163 hash_newfunc ((struct hash_entry *) ret, table,
164 (hash_table_key) string));
166 return (struct hash_entry *) ret;
169 static struct demangled_hash_entry *
170 demangled_hash_lookup (string, create)
174 return ((struct demangled_hash_entry *)
175 hash_lookup (&demangled_table, (hash_table_key) string,
176 create, &string_copy));
181 struct symbol_stack_entry
184 struct symbol_stack_entry *next;
186 struct obstack symbol_stack_obstack;
187 struct symbol_stack_entry *symbol_stack;
189 struct file_stack_entry
192 struct file_stack_entry *next;
194 struct obstack file_stack_obstack;
195 struct file_stack_entry *file_stack;
201 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
202 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
204 ep->next = symbol_stack;
211 struct symbol_stack_entry *ep = symbol_stack;
216 symbol_stack = ep->next;
217 obstack_free (&symbol_stack_obstack, ep);
225 struct file_stack_entry *ep;
230 ep = (struct file_stack_entry *) obstack_alloc
231 (&file_stack_obstack, sizeof (struct file_stack_entry));
233 ep->next = file_stack;
241 struct file_stack_entry *ep = file_stack;
246 file_stack = ep->next;
247 obstack_free (&file_stack_obstack, ep);
252 /* Other machinery. */
259 hash_table_init (&symbol_table, symbol_hash_newfunc, &string_hash,
261 hash_table_init (&file_table, file_hash_newfunc, &string_hash,
263 hash_table_init (&demangled_table, demangled_hash_newfunc,
264 &string_hash, &string_compare);
265 obstack_begin (&symbol_stack_obstack, 0);
266 obstack_begin (&file_stack_obstack, 0);
268 p = getenv ("TLINK_VERBOSE");
270 tlink_verbose = atoi (p);
282 tlink_execute (prog, argv, redir)
287 collect_execute (prog, argv, redir);
288 return collect_wait (prog);
292 frob_extension (s, ext)
295 char *p = rindex (s, '/');
302 obstack_grow (&temporary_obstack, s, p - s);
303 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
307 obstack_fgets (stream, ob)
312 while ((c = getc (stream)) != EOF && c != '\n')
313 obstack_1grow (ob, c);
314 if (obstack_object_size (ob) == 0)
316 obstack_1grow (ob, '\0');
317 return obstack_finish (ob);
324 return obstack_fgets (stream, &temporary_obstack);
331 return obstack_fgets (stream, &permanent_obstack);
334 /* Real tlink code. */
337 freadsym (stream, f, chosen)
345 char *name = tfgets (stream);
346 sym = symbol_hash_lookup (name, true);
349 if (sym->file == NULL)
353 sym->chosen = chosen;
357 if (sym->chosen && sym->file != f)
359 if (sym->chosen == 1)
360 file_push (sym->file);
365 chosen = sym->chosen;
369 sym->chosen = chosen;
378 FILE *stream = fopen ((char*) f->root.key, "r");
380 if (tlink_verbose >= 2)
381 fprintf (stderr, "collect: reading %s\n",
382 (char*) f->root.key);
384 while (fscanf (stream, "%c ", &c) == 1)
389 f->args = pfgets (stream);
392 f->dir = pfgets (stream);
395 f->main = pfgets (stream);
398 freadsym (stream, f, 2);
401 freadsym (stream, f, 1);
404 freadsym (stream, f, 0);
407 obstack_free (&temporary_obstack, temporary_firstobj);
411 f->args = getenv ("COLLECT_GCC_OPTIONS");
417 maybe_tweak (line, f)
421 symbol *sym = symbol_hash_lookup (line + 2, false);
423 if ((sym->file == f && sym->tweaking)
424 || (sym->file != f && line[0] == 'C'))
441 while ((f = file_pop ()) != NULL)
443 char *line, *command;
444 FILE *stream = fopen ((char*) f->root.key, "r");
445 char *outname = frob_extension ((char*) f->root.key, ".rnw");
446 FILE *output = fopen (outname, "w");
448 while ((line = tfgets (stream)) != NULL)
454 maybe_tweak (line, f);
456 fprintf (output, "%s\n", line);
460 rename (outname, (char*) f->root.key);
462 obstack_grow (&temporary_obstack, "cd ", 3);
463 obstack_grow (&temporary_obstack, f->dir, strlen (f->dir));
464 obstack_grow (&temporary_obstack, "; ", 2);
465 obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name));
466 obstack_1grow (&temporary_obstack, ' ');
467 obstack_grow (&temporary_obstack, f->args, strlen (f->args));
468 obstack_1grow (&temporary_obstack, ' ');
469 command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main));
472 fprintf (stderr, "collect: recompiling %s\n", f->main);
473 if (tlink_verbose >= 3)
474 fprintf (stderr, "%s\n", command);
476 if (system (command) != 0)
481 obstack_free (&temporary_obstack, temporary_firstobj);
487 read_repo_files (object_lst)
490 char **object = object_lst;
492 for (; *object; object++)
494 char *p = frob_extension (*object, ".rpo");
497 if (! file_exists (p))
500 f = file_hash_lookup (p);
505 if (file_stack != NULL && ! recompile_files ())
508 return (symbol_stack != NULL);
512 demangle_new_symbols ()
516 while ((sym = symbol_pop ()) != NULL)
519 char *p = cplus_demangle ((char*) sym->root.key,
520 DMGL_PARAMS | DMGL_ANSI);
525 dem = demangled_hash_lookup (p, true);
526 dem->mangled = (char*) sym->root.key;
531 scan_linker_output (fname)
534 FILE *stream = fopen (fname, "r");
537 while ((line = tfgets (stream)) != NULL)
543 while (*p && ISSPACE ((unsigned char)*p))
549 for (q = p; *q && ! ISSPACE ((unsigned char)*q); ++q)
552 /* Try the first word on the line. */
555 if (*p == '_' && prepends_underscore)
560 sym = symbol_hash_lookup (p, false);
563 /* Try a mangled name in quotes. */
569 /* First try `GNU style'. */
570 p = index (oldq, '`');
572 p++, q = index (p, '\'');
573 /* Then try "double quotes". */
574 else if (p = index (oldq, '"'), p)
575 p++, q = index (p, '"');
580 dem = demangled_hash_lookup (p, false);
582 sym = symbol_hash_lookup (dem->mangled, false);
584 sym = symbol_hash_lookup (p, false);
588 if (sym && sym->tweaked)
593 if (sym && !sym->tweaking)
595 if (tlink_verbose >= 2)
596 fprintf (stderr, "collect: tweaking %s in %s\n",
597 (char*) sym->root.key, (char*) sym->file->root.key);
599 file_push (sym->file);
602 obstack_free (&temporary_obstack, temporary_firstobj);
606 return (file_stack != NULL);
610 do_tlink (ld_argv, object_lst)
611 char **ld_argv, **object_lst;
613 int exit = tlink_execute ("ld", ld_argv, ldout);
621 /* Until collect does a better job of figuring out which are object
622 files, assume that everything on the command line could be. */
623 if (read_repo_files (ld_argv))
624 while (exit && i++ < MAX_ITERATIONS)
626 if (tlink_verbose >= 3)
628 demangle_new_symbols ();
629 if (! scan_linker_output (ldout))
631 if (! recompile_files ())
634 fprintf (stderr, "collect: relinking\n");
635 exit = tlink_execute ("ld", ld_argv, ldout);
643 error ("ld returned %d exit status", exit);