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. */
45 #define MAX_ITERATIONS 17
47 /* Obstack allocation and deallocation routines. */
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
51 extern char * xmalloc
PARAMS((unsigned));
52 #ifdef NEED_DECLARATION_FREE
55 #ifdef NEED_DECLARATION_GETENV
56 extern char * getenv ();
59 /* Defined in collect2.c. */
60 extern int vflag
, debug
;
62 extern char *c_file_name
;
63 extern struct obstack temporary_obstack
;
64 extern struct obstack permanent_obstack
;
65 extern char * temporary_firstobj
;
67 /* Defined in the automatically-generated underscore.c. */
68 extern int prepends_underscore
;
70 static int tlink_verbose
;
72 /* Hash table code. */
74 typedef struct symbol_hash_entry
76 struct hash_entry root
;
77 struct file_hash_entry
*file
;
83 typedef struct file_hash_entry
85 struct hash_entry root
;
92 typedef struct demangled_hash_entry
94 struct hash_entry root
;
98 static struct hash_table symbol_table
;
100 static struct hash_entry
*
101 symbol_hash_newfunc (entry
, table
, string
)
102 struct hash_entry
*entry
;
103 struct hash_table
*table
;
106 struct symbol_hash_entry
*ret
= (struct symbol_hash_entry
*) entry
;
109 ret
= ((struct symbol_hash_entry
*)
110 hash_allocate (table
, sizeof (struct symbol_hash_entry
)));
114 ret
= ((struct symbol_hash_entry
*)
115 hash_newfunc ((struct hash_entry
*) ret
, table
, string
));
120 return (struct hash_entry
*) ret
;
123 static struct symbol_hash_entry
*
124 symbol_hash_lookup (string
, create
)
128 return ((struct symbol_hash_entry
*)
129 hash_lookup (&symbol_table
, string
, create
, true));
132 static struct hash_table file_table
;
134 static struct hash_entry
*
135 file_hash_newfunc (entry
, table
, string
)
136 struct hash_entry
*entry
;
137 struct hash_table
*table
;
140 struct file_hash_entry
*ret
= (struct file_hash_entry
*) entry
;
143 ret
= ((struct file_hash_entry
*)
144 hash_allocate (table
, sizeof (struct file_hash_entry
)));
148 ret
= ((struct file_hash_entry
*)
149 hash_newfunc ((struct hash_entry
*) ret
, table
, string
));
154 return (struct hash_entry
*) ret
;
157 static struct file_hash_entry
*
158 file_hash_lookup (string
)
161 return ((struct file_hash_entry
*)
162 hash_lookup (&file_table
, string
, true, true));
165 static struct hash_table demangled_table
;
167 static struct hash_entry
*
168 demangled_hash_newfunc (entry
, table
, string
)
169 struct hash_entry
*entry
;
170 struct hash_table
*table
;
173 struct demangled_hash_entry
*ret
= (struct demangled_hash_entry
*) entry
;
176 ret
= ((struct demangled_hash_entry
*)
177 hash_allocate (table
, sizeof (struct demangled_hash_entry
)));
181 ret
= ((struct demangled_hash_entry
*)
182 hash_newfunc ((struct hash_entry
*) ret
, table
, string
));
184 return (struct hash_entry
*) ret
;
187 static struct demangled_hash_entry
*
188 demangled_hash_lookup (string
, create
)
192 return ((struct demangled_hash_entry
*)
193 hash_lookup (&demangled_table
, string
, create
, true));
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. */
276 hash_table_init (&symbol_table
, symbol_hash_newfunc
);
277 hash_table_init (&file_table
, file_hash_newfunc
);
278 hash_table_init (&demangled_table
, demangled_hash_newfunc
);
279 obstack_begin (&symbol_stack_obstack
, 0);
280 obstack_begin (&file_stack_obstack
, 0);
282 p
= getenv ("TLINK_VERBOSE");
284 tlink_verbose
= atoi (p
);
296 tlink_execute (prog
, argv
, redir
)
301 collect_execute (prog
, argv
, redir
);
302 return collect_wait (prog
);
306 frob_extension (s
, ext
)
309 char *p
= (char *) rindex (s
, '/');
312 p
= (char *) rindex (p
, '.');
316 obstack_grow (&temporary_obstack
, s
, p
- s
);
317 return obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
321 obstack_fgets (stream
, ob
)
326 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
327 obstack_1grow (ob
, c
);
328 if (obstack_object_size (ob
) == 0)
330 obstack_1grow (ob
, '\0');
331 return obstack_finish (ob
);
338 return obstack_fgets (stream
, &temporary_obstack
);
345 return obstack_fgets (stream
, &permanent_obstack
);
348 /* Real tlink code. */
351 freadsym (stream
, f
, chosen
)
359 char *name
= tfgets (stream
);
360 sym
= symbol_hash_lookup (name
, true);
363 if (sym
->file
== NULL
)
367 sym
->chosen
= chosen
;
371 if (sym
->chosen
&& sym
->file
!= f
)
373 if (sym
->chosen
== 1)
374 file_push (sym
->file
);
379 chosen
= sym
->chosen
;
383 sym
->chosen
= chosen
;
392 FILE *stream
= fopen (f
->root
.string
, "r");
394 if (tlink_verbose
>= 2)
395 fprintf (stderr
, "collect: reading %s\n", f
->root
.string
);
397 while (fscanf (stream
, "%c ", &c
) == 1)
402 f
->args
= pfgets (stream
);
405 f
->dir
= pfgets (stream
);
408 f
->main
= pfgets (stream
);
411 freadsym (stream
, f
, 2);
414 freadsym (stream
, f
, 1);
417 freadsym (stream
, f
, 0);
420 obstack_free (&temporary_obstack
, temporary_firstobj
);
424 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
430 maybe_tweak (line
, f
)
434 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
436 if ((sym
->file
== f
&& sym
->tweaking
)
437 || (sym
->file
!= f
&& line
[0] == 'C'))
454 while ((f
= file_pop ()) != NULL
)
456 char *line
, *command
;
457 FILE *stream
= fopen (f
->root
.string
, "r");
458 char *outname
= frob_extension (f
->root
.string
, ".rnw");
459 FILE *output
= fopen (outname
, "w");
461 while ((line
= tfgets (stream
)) != NULL
)
467 maybe_tweak (line
, f
);
469 fprintf (output
, "%s\n", line
);
473 rename (outname
, f
->root
.string
);
475 obstack_grow (&temporary_obstack
, "cd ", 3);
476 obstack_grow (&temporary_obstack
, f
->dir
, strlen (f
->dir
));
477 obstack_grow (&temporary_obstack
, "; ", 2);
478 obstack_grow (&temporary_obstack
, c_file_name
, strlen (c_file_name
));
479 obstack_1grow (&temporary_obstack
, ' ');
480 obstack_grow (&temporary_obstack
, f
->args
, strlen (f
->args
));
481 obstack_1grow (&temporary_obstack
, ' ');
482 command
= obstack_copy0 (&temporary_obstack
, f
->main
, strlen (f
->main
));
485 fprintf (stderr
, "collect: recompiling %s\n", f
->main
);
486 if (tlink_verbose
>= 3)
487 fprintf (stderr
, "%s\n", command
);
489 if (system (command
) != 0)
494 obstack_free (&temporary_obstack
, temporary_firstobj
);
500 read_repo_files (object_lst
)
503 char **object
= object_lst
;
505 for (; *object
; object
++)
507 char *p
= frob_extension (*object
, ".rpo");
510 if (! file_exists (p
))
513 f
= file_hash_lookup (p
);
518 if (file_stack
!= NULL
&& ! recompile_files ())
521 return (symbol_stack
!= NULL
);
525 demangle_new_symbols ()
529 while ((sym
= symbol_pop ()) != NULL
)
532 char *p
= cplus_demangle (sym
->root
.string
, DMGL_PARAMS
| DMGL_ANSI
);
537 dem
= demangled_hash_lookup (p
, true);
538 dem
->mangled
= sym
->root
.string
;
543 scan_linker_output (fname
)
546 FILE *stream
= fopen (fname
, "r");
549 while ((line
= tfgets (stream
)) != NULL
)
555 while (*p
&& isspace (*p
))
561 for (q
= p
; *q
&& ! isspace (*q
); ++q
)
564 /* Try the first word on the line. */
567 if (*p
== '_' && prepends_underscore
)
572 sym
= symbol_hash_lookup (p
, false);
575 /* Try a mangled name in `quotes'. */
578 p
= (char *) index (q
+1, '`');
581 #define MUL "multiple definition of "
582 #define UND "undefined reference to "
584 if (p
&& (p
- line
> sizeof (MUL
)))
586 char *beg
= p
- sizeof (MUL
) + 1;
588 if (!strcmp (beg
, MUL
) || !strcmp (beg
, UND
))
589 p
++, q
= (char *) index (p
, '\'');
592 *q
= 0, dem
= demangled_hash_lookup (p
, false);
594 sym
= symbol_hash_lookup (dem
->mangled
, false);
597 if (sym
&& sym
->tweaked
)
602 if (sym
&& !sym
->tweaking
)
604 if (tlink_verbose
>= 2)
605 fprintf (stderr
, "collect: tweaking %s in %s\n",
606 sym
->root
.string
, sym
->file
->root
.string
);
608 file_push (sym
->file
);
611 obstack_free (&temporary_obstack
, temporary_firstobj
);
615 return (file_stack
!= NULL
);
619 do_tlink (ld_argv
, object_lst
)
620 char **ld_argv
, **object_lst
;
622 int exit
= tlink_execute ("ld", ld_argv
, ldout
);
630 /* Until collect does a better job of figuring out which are object
631 files, assume that everything on the command line could be. */
632 if (read_repo_files (ld_argv
))
633 while (exit
&& i
++ < MAX_ITERATIONS
)
635 if (tlink_verbose
>= 3)
637 demangle_new_symbols ();
638 if (! scan_linker_output (ldout
))
640 if (! recompile_files ())
643 fprintf (stderr
, "collect: relinking\n");
644 exit
= tlink_execute ("ld", ld_argv
, ldout
);
652 error ("ld returned %d exit status", exit
);