* decl.c (grokdeclarator): Remove const and volatile from type after
[official-gcc.git] / gcc / tlink.c
blob06207dda135c30a6a8197ced9e7b15b6d3c956ec
1 /* Scan linker error messages for missing template instantiations and provide
2 them.
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)
12 any later version.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "hash.h"
26 #include "demangle.h"
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;
38 extern char *ldout;
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;
55 int chosen;
56 int tweaking;
57 int tweaked;
58 } symbol;
60 typedef struct file_hash_entry
62 struct hash_entry root;
63 const char *args;
64 const char *dir;
65 const char *main;
66 int tweaking;
67 } file;
69 typedef struct demangled_hash_entry
71 struct hash_entry root;
72 const char *mangled;
73 } demangled;
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;
81 const char *string;
83 struct symbol_hash_entry *ret = (struct symbol_hash_entry *) entry;
84 if (ret == NULL)
86 ret = ((struct symbol_hash_entry *)
87 hash_allocate (table, sizeof (struct symbol_hash_entry)));
88 if (ret == NULL)
89 return NULL;
91 ret = ((struct symbol_hash_entry *)
92 hash_newfunc ((struct hash_entry *) ret, table, string));
93 ret->file = NULL;
94 ret->chosen = 0;
95 ret->tweaking = 0;
96 ret->tweaked = 0;
97 return (struct hash_entry *) ret;
100 static struct symbol_hash_entry *
101 symbol_hash_lookup (string, create)
102 const char *string;
103 boolean 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;
115 const char *string;
117 struct file_hash_entry *ret = (struct file_hash_entry *) entry;
118 if (ret == NULL)
120 ret = ((struct file_hash_entry *)
121 hash_allocate (table, sizeof (struct file_hash_entry)));
122 if (ret == NULL)
123 return NULL;
125 ret = ((struct file_hash_entry *)
126 hash_newfunc ((struct hash_entry *) ret, table, string));
127 ret->args = NULL;
128 ret->dir = NULL;
129 ret->main = NULL;
130 ret->tweaking = 0;
131 return (struct hash_entry *) ret;
134 static struct file_hash_entry *
135 file_hash_lookup (string)
136 const char *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;
148 const char *string;
150 struct demangled_hash_entry *ret = (struct demangled_hash_entry *) entry;
151 if (ret == NULL)
153 ret = ((struct demangled_hash_entry *)
154 hash_allocate (table, sizeof (struct demangled_hash_entry)));
155 if (ret == NULL)
156 return NULL;
158 ret = ((struct demangled_hash_entry *)
159 hash_newfunc ((struct hash_entry *) ret, table, string));
160 ret->mangled = NULL;
161 return (struct hash_entry *) ret;
164 static struct demangled_hash_entry *
165 demangled_hash_lookup (string, create)
166 const char *string;
167 boolean create;
169 return ((struct demangled_hash_entry *)
170 hash_lookup (&demangled_table, string, create, true));
173 /* Stack code. */
175 struct symbol_stack_entry
177 symbol *value;
178 struct symbol_stack_entry *next;
180 struct obstack symbol_stack_obstack;
181 struct symbol_stack_entry *symbol_stack;
183 struct file_stack_entry
185 file *value;
186 struct file_stack_entry *next;
188 struct obstack file_stack_obstack;
189 struct file_stack_entry *file_stack;
191 static void
192 symbol_push (p)
193 symbol *p;
195 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
196 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
197 ep->value = p;
198 ep->next = symbol_stack;
199 symbol_stack = ep;
202 static symbol *
203 symbol_pop ()
205 struct symbol_stack_entry *ep = symbol_stack;
206 symbol *p;
207 if (ep == NULL)
208 return NULL;
209 p = ep->value;
210 symbol_stack = ep->next;
211 obstack_free (&symbol_stack_obstack, ep);
212 return p;
215 static void
216 file_push (p)
217 file *p;
219 struct file_stack_entry *ep;
221 if (p->tweaking)
222 return;
224 ep = (struct file_stack_entry *) obstack_alloc
225 (&file_stack_obstack, sizeof (struct file_stack_entry));
226 ep->value = p;
227 ep->next = file_stack;
228 file_stack = ep;
229 p->tweaking = 1;
232 static file *
233 file_pop ()
235 struct file_stack_entry *ep = file_stack;
236 file *p;
237 if (ep == NULL)
238 return NULL;
239 p = ep->value;
240 file_stack = ep->next;
241 obstack_free (&file_stack_obstack, ep);
242 p->tweaking = 0;
243 return p;
246 /* Other machinery. */
248 static void
249 tlink_init ()
251 char *p;
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");
260 if (p)
261 tlink_verbose = atoi (p);
262 else
264 tlink_verbose = 1;
265 if (vflag)
266 tlink_verbose = 2;
267 if (debug)
268 tlink_verbose = 3;
272 static int
273 tlink_execute (prog, argv, redir)
274 char *prog;
275 char **argv;
276 char *redir;
278 collect_execute (prog, argv, redir);
279 return collect_wait (prog);
282 static char *
283 frob_extension (s, ext)
284 char *s, *ext;
286 char *p = rindex (s, '/');
287 if (! p)
288 p = s;
289 p = rindex (p, '.');
290 if (! p)
291 p = s + strlen (s);
293 obstack_grow (&temporary_obstack, s, p - s);
294 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
297 static char *
298 obstack_fgets (stream, ob)
299 FILE *stream;
300 struct obstack *ob;
302 int c;
303 while ((c = getc (stream)) != EOF && c != '\n')
304 obstack_1grow (ob, c);
305 if (obstack_object_size (ob) == 0)
306 return NULL;
307 obstack_1grow (ob, '\0');
308 return obstack_finish (ob);
311 static char *
312 tfgets (stream)
313 FILE *stream;
315 return obstack_fgets (stream, &temporary_obstack);
318 static char *
319 pfgets (stream)
320 FILE *stream;
322 return obstack_fgets (stream, &permanent_obstack);
325 /* Real tlink code. */
327 static void
328 freadsym (stream, f, chosen)
329 FILE *stream;
330 file *f;
331 int chosen;
333 symbol *sym;
336 char *name = tfgets (stream);
337 sym = symbol_hash_lookup (name, true);
340 if (sym->file == NULL)
342 symbol_push (sym);
343 sym->file = f;
344 sym->chosen = chosen;
346 else if (chosen)
348 if (sym->chosen && sym->file != f)
350 if (sym->chosen == 1)
351 file_push (sym->file);
352 else
354 file_push (f);
355 f = sym->file;
356 chosen = sym->chosen;
359 sym->file = f;
360 sym->chosen = chosen;
364 static void
365 read_repo_file (f)
366 file *f;
368 char c;
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)
376 switch (c)
378 case 'A':
379 f->args = pfgets (stream);
380 break;
381 case 'D':
382 f->dir = pfgets (stream);
383 break;
384 case 'M':
385 f->main = pfgets (stream);
386 break;
387 case 'P':
388 freadsym (stream, f, 2);
389 break;
390 case 'C':
391 freadsym (stream, f, 1);
392 break;
393 case 'O':
394 freadsym (stream, f, 0);
395 break;
397 obstack_free (&temporary_obstack, temporary_firstobj);
399 fclose (stream);
400 if (f->args == NULL)
401 f->args = getenv ("COLLECT_GCC_OPTIONS");
402 if (f->dir == NULL)
403 f->dir = ".";
406 static void
407 maybe_tweak (line, f)
408 char *line;
409 file *f;
411 symbol *sym = symbol_hash_lookup (line + 2, false);
413 if ((sym->file == f && sym->tweaking)
414 || (sym->file != f && line[0] == 'C'))
416 sym->tweaking = 0;
417 sym->tweaked = 1;
419 if (line[0] == 'O')
420 line[0] = 'C';
421 else
422 line[0] = 'O';
426 static int
427 recompile_files ()
429 file *f;
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)
440 switch (line[0])
442 case 'C':
443 case 'O':
444 maybe_tweak (line, f);
446 fprintf (output, "%s\n", line);
448 fclose (stream);
449 fclose (output);
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));
461 if (tlink_verbose)
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)
467 return 0;
469 read_repo_file (f);
471 obstack_free (&temporary_obstack, temporary_firstobj);
473 return 1;
476 static int
477 read_repo_files (object_lst)
478 char **object_lst;
480 char **object = object_lst;
482 for (; *object; object++)
484 char *p = frob_extension (*object, ".rpo");
485 file *f;
487 if (! file_exists (p))
488 continue;
490 f = file_hash_lookup (p);
492 read_repo_file (f);
495 if (file_stack != NULL && ! recompile_files ())
496 return 0;
498 return (symbol_stack != NULL);
501 static void
502 demangle_new_symbols ()
504 symbol *sym;
506 while ((sym = symbol_pop ()) != NULL)
508 demangled *dem;
509 char *p = cplus_demangle (sym->root.string, DMGL_PARAMS | DMGL_ANSI);
511 if (! p)
512 continue;
514 dem = demangled_hash_lookup (p, true);
515 dem->mangled = sym->root.string;
519 static int
520 scan_linker_output (fname)
521 char *fname;
523 FILE *stream = fopen (fname, "r");
524 char *line;
526 while ((line = tfgets (stream)) != NULL)
528 char *p = line, *q;
529 symbol *sym;
530 int end;
532 while (*p && isspace (*p))
533 ++p;
535 if (! *p)
536 continue;
538 for (q = p; *q && ! isspace (*q); ++q)
541 /* Try the first word on the line. */
542 if (*p == '.')
543 ++p;
544 if (*p == '_' && prepends_underscore)
545 ++p;
547 end = ! *q;
548 *q = 0;
549 sym = symbol_hash_lookup (p, false);
551 if (! sym && ! end)
552 /* Try a mangled name in `quotes'. */
554 demangled *dem = 0;
555 p = index (q+1, '`');
556 q = 0;
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;
564 *p = 0;
565 if (!strcmp (beg, MUL) || !strcmp (beg, UND))
566 p++, q = index (p, '\'');
568 if (q)
569 *q = 0, dem = demangled_hash_lookup (p, false);
570 if (dem)
571 sym = symbol_hash_lookup (dem->mangled, false);
574 if (sym && sym->tweaked)
576 fclose (stream);
577 return 0;
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);
584 sym->tweaking = 1;
585 file_push (sym->file);
588 obstack_free (&temporary_obstack, temporary_firstobj);
591 fclose (stream);
592 return (file_stack != NULL);
595 void
596 do_tlink (ld_argv, object_lst)
597 char **ld_argv, **object_lst;
599 int exit = tlink_execute ("ld", ld_argv, ldout);
601 tlink_init ();
603 if (exit)
605 int i = 0;
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)
613 dump_file (ldout);
614 demangle_new_symbols ();
615 if (! scan_linker_output (ldout))
616 break;
617 if (! recompile_files ())
618 break;
619 if (tlink_verbose)
620 fprintf (stderr, "collect: relinking\n");
621 exit = tlink_execute ("ld", ld_argv, ldout);
625 dump_file (ldout);
626 unlink (ldout);
627 if (exit)
629 error ("ld returned %d exit status", exit);
630 collect_exit (exit);