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. */
28 #define MAX_ITERATIONS 17
30 /* Obstack allocation and deallocation routines. */
31 #define obstack_chunk_alloc xmalloc
32 #define obstack_chunk_free free
34 extern char * xmalloc
PARAMS((unsigned));
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
, string
));
97 return (struct hash_entry
*) ret
;
100 static struct symbol_hash_entry
*
101 symbol_hash_lookup (string
, create
)
105 return ((struct symbol_hash_entry
*)
106 hash_lookup (&symbol_table
, string
, create
, true));
109 static struct hash_table file_table
;
111 static struct hash_entry
*
112 file_hash_newfunc (entry
, table
, string
)
113 struct hash_entry
*entry
;
114 struct hash_table
*table
;
117 struct file_hash_entry
*ret
= (struct file_hash_entry
*) entry
;
120 ret
= ((struct file_hash_entry
*)
121 hash_allocate (table
, sizeof (struct file_hash_entry
)));
125 ret
= ((struct file_hash_entry
*)
126 hash_newfunc ((struct hash_entry
*) ret
, table
, string
));
131 return (struct hash_entry
*) ret
;
134 static struct file_hash_entry
*
135 file_hash_lookup (string
)
138 return ((struct file_hash_entry
*)
139 hash_lookup (&file_table
, string
, true, true));
142 static struct hash_table demangled_table
;
144 static struct hash_entry
*
145 demangled_hash_newfunc (entry
, table
, string
)
146 struct hash_entry
*entry
;
147 struct hash_table
*table
;
150 struct demangled_hash_entry
*ret
= (struct demangled_hash_entry
*) entry
;
153 ret
= ((struct demangled_hash_entry
*)
154 hash_allocate (table
, sizeof (struct demangled_hash_entry
)));
158 ret
= ((struct demangled_hash_entry
*)
159 hash_newfunc ((struct hash_entry
*) ret
, table
, string
));
161 return (struct hash_entry
*) ret
;
164 static struct demangled_hash_entry
*
165 demangled_hash_lookup (string
, create
)
169 return ((struct demangled_hash_entry
*)
170 hash_lookup (&demangled_table
, string
, create
, true));
175 struct symbol_stack_entry
178 struct symbol_stack_entry
*next
;
180 struct obstack symbol_stack_obstack
;
181 struct symbol_stack_entry
*symbol_stack
;
183 struct file_stack_entry
186 struct file_stack_entry
*next
;
188 struct obstack file_stack_obstack
;
189 struct file_stack_entry
*file_stack
;
195 struct symbol_stack_entry
*ep
= (struct symbol_stack_entry
*) obstack_alloc
196 (&symbol_stack_obstack
, sizeof (struct symbol_stack_entry
));
198 ep
->next
= symbol_stack
;
205 struct symbol_stack_entry
*ep
= symbol_stack
;
210 symbol_stack
= ep
->next
;
211 obstack_free (&symbol_stack_obstack
, ep
);
219 struct file_stack_entry
*ep
;
224 ep
= (struct file_stack_entry
*) obstack_alloc
225 (&file_stack_obstack
, sizeof (struct file_stack_entry
));
227 ep
->next
= file_stack
;
235 struct file_stack_entry
*ep
= file_stack
;
240 file_stack
= ep
->next
;
241 obstack_free (&file_stack_obstack
, ep
);
246 /* Other machinery. */
253 hash_table_init (&symbol_table
, symbol_hash_newfunc
);
254 hash_table_init (&file_table
, file_hash_newfunc
);
255 hash_table_init (&demangled_table
, demangled_hash_newfunc
);
256 obstack_begin (&symbol_stack_obstack
, 0);
257 obstack_begin (&file_stack_obstack
, 0);
259 p
= getenv ("TLINK_VERBOSE");
261 tlink_verbose
= atoi (p
);
273 tlink_execute (prog
, argv
, redir
)
278 collect_execute (prog
, argv
, redir
);
279 return collect_wait (prog
);
283 frob_extension (s
, ext
)
286 char *p
= rindex (s
, '/');
293 obstack_grow (&temporary_obstack
, s
, p
- s
);
294 return obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
298 obstack_fgets (stream
, ob
)
303 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
304 obstack_1grow (ob
, c
);
305 if (obstack_object_size (ob
) == 0)
307 obstack_1grow (ob
, '\0');
308 return obstack_finish (ob
);
315 return obstack_fgets (stream
, &temporary_obstack
);
322 return obstack_fgets (stream
, &permanent_obstack
);
325 /* Real tlink code. */
328 freadsym (stream
, f
, chosen
)
336 char *name
= tfgets (stream
);
337 sym
= symbol_hash_lookup (name
, true);
340 if (sym
->file
== NULL
)
344 sym
->chosen
= chosen
;
348 if (sym
->chosen
&& sym
->file
!= f
)
350 if (sym
->chosen
== 1)
351 file_push (sym
->file
);
356 chosen
= sym
->chosen
;
360 sym
->chosen
= chosen
;
369 FILE *stream
= fopen (f
->root
.string
, "r");
371 if (tlink_verbose
>= 2)
372 fprintf (stderr
, "collect: reading %s\n", f
->root
.string
);
374 while (fscanf (stream
, "%c ", &c
) == 1)
379 f
->args
= pfgets (stream
);
382 f
->dir
= pfgets (stream
);
385 f
->main
= pfgets (stream
);
388 freadsym (stream
, f
, 2);
391 freadsym (stream
, f
, 1);
394 freadsym (stream
, f
, 0);
397 obstack_free (&temporary_obstack
, temporary_firstobj
);
401 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
407 maybe_tweak (line
, f
)
411 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
413 if ((sym
->file
== f
&& sym
->tweaking
)
414 || (sym
->file
!= f
&& line
[0] == 'C'))
431 while ((f
= file_pop ()) != NULL
)
433 char *line
, *command
;
434 FILE *stream
= fopen (f
->root
.string
, "r");
435 char *outname
= frob_extension (f
->root
.string
, ".rnw");
436 FILE *output
= fopen (outname
, "w");
438 while ((line
= tfgets (stream
)) != NULL
)
444 maybe_tweak (line
, f
);
446 fprintf (output
, "%s\n", line
);
450 rename (outname
, f
->root
.string
);
452 obstack_grow (&temporary_obstack
, "cd ", 3);
453 obstack_grow (&temporary_obstack
, f
->dir
, strlen (f
->dir
));
454 obstack_grow (&temporary_obstack
, "; ", 2);
455 obstack_grow (&temporary_obstack
, c_file_name
, strlen (c_file_name
));
456 obstack_1grow (&temporary_obstack
, ' ');
457 obstack_grow (&temporary_obstack
, f
->args
, strlen (f
->args
));
458 obstack_1grow (&temporary_obstack
, ' ');
459 command
= obstack_copy0 (&temporary_obstack
, f
->main
, strlen (f
->main
));
462 fprintf (stderr
, "collect: recompiling %s\n", f
->main
);
463 if (tlink_verbose
>= 3)
464 fprintf (stderr
, "%s\n", command
);
466 if (system (command
) != 0)
471 obstack_free (&temporary_obstack
, temporary_firstobj
);
477 read_repo_files (object_lst
)
480 char **object
= object_lst
;
482 for (; *object
; object
++)
484 char *p
= frob_extension (*object
, ".rpo");
487 if (! file_exists (p
))
490 f
= file_hash_lookup (p
);
495 if (file_stack
!= NULL
&& ! recompile_files ())
498 return (symbol_stack
!= NULL
);
502 demangle_new_symbols ()
506 while ((sym
= symbol_pop ()) != NULL
)
509 char *p
= cplus_demangle (sym
->root
.string
, DMGL_PARAMS
| DMGL_ANSI
);
514 dem
= demangled_hash_lookup (p
, true);
515 dem
->mangled
= sym
->root
.string
;
520 scan_linker_output (fname
)
523 FILE *stream
= fopen (fname
, "r");
526 while ((line
= tfgets (stream
)) != NULL
)
532 while (*p
&& isspace (*p
))
538 for (q
= p
; *q
&& ! isspace (*q
); ++q
)
541 /* Try the first word on the line. */
544 if (*p
== '_' && prepends_underscore
)
549 sym
= symbol_hash_lookup (p
, false);
552 /* Try a mangled name in `quotes'. */
555 p
= index (q
+1, '`');
558 #define MUL "multiple definition of "
559 #define UND "undefined reference to "
561 if (p
&& (p
- line
> sizeof (MUL
)))
563 char *beg
= p
- sizeof (MUL
) + 1;
565 if (!strcmp (beg
, MUL
) || !strcmp (beg
, UND
))
566 p
++, q
= index (p
, '\'');
569 *q
= 0, dem
= demangled_hash_lookup (p
, false);
571 sym
= symbol_hash_lookup (dem
->mangled
, false);
574 if (sym
&& sym
->tweaked
)
579 if (sym
&& !sym
->tweaking
)
581 if (tlink_verbose
>= 2)
582 fprintf (stderr
, "collect: tweaking %s in %s\n",
583 sym
->root
.string
, sym
->file
->root
.string
);
585 file_push (sym
->file
);
588 obstack_free (&temporary_obstack
, temporary_firstobj
);
592 return (file_stack
!= NULL
);
596 do_tlink (ld_argv
, object_lst
)
597 char **ld_argv
, **object_lst
;
599 int exit
= tlink_execute ("ld", ld_argv
, ldout
);
607 /* Until collect does a better job of figuring out which are object
608 files, assume that everything on the command line could be. */
609 if (read_repo_files (ld_argv
))
610 while (exit
&& i
++ < MAX_ITERATIONS
)
612 if (tlink_verbose
>= 3)
614 demangle_new_symbols ();
615 if (! scan_linker_output (ldout
))
617 if (! recompile_files ())
620 fprintf (stderr
, "collect: relinking\n");
621 exit
= tlink_execute ("ld", ld_argv
, ldout
);
629 error ("ld returned %d exit status", exit
);