* Makefile.tpl: Fix stupid pasto.
[official-gcc.git] / gcc / tlink.c
blob6da909c7baa2277f632d0743c7b69995eff2c7f0
1 /* Scan linker error messages for missing template instantiations and provide
2 them.
4 Copyright (C) 1995, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 Contributed by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "intl.h"
29 #include "obstack.h"
30 #include "hashtab.h"
31 #include "demangle.h"
32 #include "collect2.h"
34 #define MAX_ITERATIONS 17
36 /* Defined in the automatically-generated underscore.c. */
37 extern int prepends_underscore;
39 static int tlink_verbose;
41 /* Hash table boilerplate for working with htab_t. We have hash tables
42 for symbol names, file names, and demangled symbols. */
44 typedef struct symbol_hash_entry
46 const char *key;
47 struct file_hash_entry *file;
48 int chosen;
49 int tweaking;
50 int tweaked;
51 } symbol;
53 typedef struct file_hash_entry
55 const char *key;
56 const char *args;
57 const char *dir;
58 const char *main;
59 int tweaking;
60 } file;
62 typedef struct demangled_hash_entry
64 const char *key;
65 const char *mangled;
66 } demangled;
68 /* Hash and comparison functions for these hash tables. */
70 static int hash_string_eq PARAMS ((const void *, const void *));
71 static hashval_t hash_string_hash PARAMS ((const void *));
73 static int
74 hash_string_eq (s1_p, s2_p)
75 const void *s1_p;
76 const void *s2_p;
78 const char *const *s1 = (const char *const *) s1_p;
79 const char *s2 = (const char *) s2_p;
80 return strcmp (*s1, s2) == 0;
83 static hashval_t
84 hash_string_hash (s_p)
85 const void *s_p;
87 const char *const *s = (const char *const *) s_p;
88 return (*htab_hash_string) (*s);
91 static htab_t symbol_table;
93 static struct symbol_hash_entry * symbol_hash_lookup PARAMS ((const char *,
94 int));
95 static struct file_hash_entry * file_hash_lookup PARAMS ((const char *));
96 static struct demangled_hash_entry *
97 demangled_hash_lookup PARAMS ((const char *, int));
98 static void symbol_push PARAMS ((symbol *));
99 static symbol * symbol_pop PARAMS ((void));
100 static void file_push PARAMS ((file *));
101 static file * file_pop PARAMS ((void));
102 static void tlink_init PARAMS ((void));
103 static int tlink_execute PARAMS ((const char *, char **, const char *));
104 static char * frob_extension PARAMS ((const char *, const char *));
105 static char * obstack_fgets PARAMS ((FILE *, struct obstack *));
106 static char * tfgets PARAMS ((FILE *));
107 static char * pfgets PARAMS ((FILE *));
108 static void freadsym PARAMS ((FILE *, file *, int));
109 static void read_repo_file PARAMS ((file *));
110 static void maybe_tweak PARAMS ((char *, file *));
111 static int recompile_files PARAMS ((void));
112 static int read_repo_files PARAMS ((char **));
113 static void demangle_new_symbols PARAMS ((void));
114 static int scan_linker_output PARAMS ((const char *));
116 /* Look up an entry in the symbol hash table. */
118 static struct symbol_hash_entry *
119 symbol_hash_lookup (string, create)
120 const char *string;
121 int create;
123 PTR *e;
124 e = htab_find_slot_with_hash (symbol_table, string,
125 (*htab_hash_string) (string),
126 create ? INSERT : NO_INSERT);
127 if (e == NULL)
128 return NULL;
129 if (*e == NULL)
131 struct symbol_hash_entry *v;
132 *e = v = xcalloc (1, sizeof (*v));
133 v->key = xstrdup (string);
135 return *e;
138 static htab_t file_table;
140 /* Look up an entry in the file hash table. */
142 static struct file_hash_entry *
143 file_hash_lookup (string)
144 const char *string;
146 PTR *e;
147 e = htab_find_slot_with_hash (file_table, string,
148 (*htab_hash_string) (string),
149 INSERT);
150 if (*e == NULL)
152 struct file_hash_entry *v;
153 *e = v = xcalloc (1, sizeof (*v));
154 v->key = xstrdup (string);
156 return *e;
159 static htab_t demangled_table;
161 /* Look up an entry in the demangled name hash table. */
163 static struct demangled_hash_entry *
164 demangled_hash_lookup (string, create)
165 const char *string;
166 int create;
168 PTR *e;
169 e = htab_find_slot_with_hash (demangled_table, string,
170 (*htab_hash_string) (string),
171 create ? INSERT : NO_INSERT);
172 if (e == NULL)
173 return NULL;
174 if (*e == NULL)
176 struct demangled_hash_entry *v;
177 *e = v = xcalloc (1, sizeof (*v));
178 v->key = xstrdup (string);
180 return *e;
183 /* Stack code. */
185 struct symbol_stack_entry
187 symbol *value;
188 struct symbol_stack_entry *next;
190 struct obstack symbol_stack_obstack;
191 struct symbol_stack_entry *symbol_stack;
193 struct file_stack_entry
195 file *value;
196 struct file_stack_entry *next;
198 struct obstack file_stack_obstack;
199 struct file_stack_entry *file_stack;
201 static void
202 symbol_push (p)
203 symbol *p;
205 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
206 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
207 ep->value = p;
208 ep->next = symbol_stack;
209 symbol_stack = ep;
212 static symbol *
213 symbol_pop ()
215 struct symbol_stack_entry *ep = symbol_stack;
216 symbol *p;
217 if (ep == NULL)
218 return NULL;
219 p = ep->value;
220 symbol_stack = ep->next;
221 obstack_free (&symbol_stack_obstack, ep);
222 return p;
225 static void
226 file_push (p)
227 file *p;
229 struct file_stack_entry *ep;
231 if (p->tweaking)
232 return;
234 ep = (struct file_stack_entry *) obstack_alloc
235 (&file_stack_obstack, sizeof (struct file_stack_entry));
236 ep->value = p;
237 ep->next = file_stack;
238 file_stack = ep;
239 p->tweaking = 1;
242 static file *
243 file_pop ()
245 struct file_stack_entry *ep = file_stack;
246 file *p;
247 if (ep == NULL)
248 return NULL;
249 p = ep->value;
250 file_stack = ep->next;
251 obstack_free (&file_stack_obstack, ep);
252 p->tweaking = 0;
253 return p;
256 /* Other machinery. */
258 /* Initialize the tlink machinery. Called from do_tlink. */
260 static void
261 tlink_init ()
263 const char *p;
265 symbol_table = htab_create (500, hash_string_hash, hash_string_eq,
266 NULL);
267 file_table = htab_create (500, hash_string_hash, hash_string_eq,
268 NULL);
269 demangled_table = htab_create (500, hash_string_hash, hash_string_eq,
270 NULL);
272 obstack_begin (&symbol_stack_obstack, 0);
273 obstack_begin (&file_stack_obstack, 0);
275 p = getenv ("TLINK_VERBOSE");
276 if (p)
277 tlink_verbose = atoi (p);
278 else
280 tlink_verbose = 1;
281 if (vflag)
282 tlink_verbose = 2;
283 if (debug)
284 tlink_verbose = 3;
288 static int
289 tlink_execute (prog, argv, redir)
290 const char *prog;
291 char **argv;
292 const char *redir;
294 collect_execute (prog, argv, redir);
295 return collect_wait (prog);
298 static char *
299 frob_extension (s, ext)
300 const char *s;
301 const char *ext;
303 const char *p = strrchr (s, '/');
304 if (! p)
305 p = s;
306 p = strrchr (p, '.');
307 if (! p)
308 p = s + strlen (s);
310 obstack_grow (&temporary_obstack, s, p - s);
311 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
314 static char *
315 obstack_fgets (stream, ob)
316 FILE *stream;
317 struct obstack *ob;
319 int c;
320 while ((c = getc (stream)) != EOF && c != '\n')
321 obstack_1grow (ob, c);
322 if (obstack_object_size (ob) == 0)
323 return NULL;
324 obstack_1grow (ob, '\0');
325 return obstack_finish (ob);
328 static char *
329 tfgets (stream)
330 FILE *stream;
332 return obstack_fgets (stream, &temporary_obstack);
335 static char *
336 pfgets (stream)
337 FILE *stream;
339 return xstrdup (tfgets (stream));
342 /* Real tlink code. */
344 /* Subroutine of read_repo_file. We are reading the repo file for file F,
345 which is coming in on STREAM, and the symbol that comes next in STREAM
346 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
348 XXX "provided" is unimplemented, both here and in the compiler. */
350 static void
351 freadsym (stream, f, chosen)
352 FILE *stream;
353 file *f;
354 int chosen;
356 symbol *sym;
359 const char *name = tfgets (stream);
360 sym = symbol_hash_lookup (name, true);
363 if (sym->file == NULL)
365 /* We didn't have this symbol already, so we choose this file. */
367 symbol_push (sym);
368 sym->file = f;
369 sym->chosen = chosen;
371 else if (chosen)
373 /* We want this file; cast aside any pretender. */
375 if (sym->chosen && sym->file != f)
377 if (sym->chosen == 1)
378 file_push (sym->file);
379 else
381 file_push (f);
382 f = sym->file;
383 chosen = sym->chosen;
386 sym->file = f;
387 sym->chosen = chosen;
391 /* Read in the repo file denoted by F, and record all its information. */
393 static void
394 read_repo_file (f)
395 file *f;
397 char c;
398 FILE *stream = fopen (f->key, "r");
400 if (tlink_verbose >= 2)
401 fprintf (stderr, _("collect: reading %s\n"), f->key);
403 while (fscanf (stream, "%c ", &c) == 1)
405 switch (c)
407 case 'A':
408 f->args = pfgets (stream);
409 break;
410 case 'D':
411 f->dir = pfgets (stream);
412 break;
413 case 'M':
414 f->main = pfgets (stream);
415 break;
416 case 'P':
417 freadsym (stream, f, 2);
418 break;
419 case 'C':
420 freadsym (stream, f, 1);
421 break;
422 case 'O':
423 freadsym (stream, f, 0);
424 break;
426 obstack_free (&temporary_obstack, temporary_firstobj);
428 fclose (stream);
429 if (f->args == NULL)
430 f->args = getenv ("COLLECT_GCC_OPTIONS");
431 if (f->dir == NULL)
432 f->dir = ".";
435 /* We might want to modify LINE, which is a symbol line from file F. We do
436 this if either we saw an error message referring to the symbol in
437 question, or we have already allocated the symbol to another file and
438 this one wants to emit it as well. */
440 static void
441 maybe_tweak (line, f)
442 char *line;
443 file *f;
445 symbol *sym = symbol_hash_lookup (line + 2, false);
447 if ((sym->file == f && sym->tweaking)
448 || (sym->file != f && line[0] == 'C'))
450 sym->tweaking = 0;
451 sym->tweaked = 1;
453 if (line[0] == 'O')
454 line[0] = 'C';
455 else
456 line[0] = 'O';
460 /* Update the repo files for each of the object files we have adjusted and
461 recompile.
463 XXX Should this use collect_execute instead of system? */
465 static int
466 recompile_files ()
468 file *f;
470 putenv (xstrdup ("COMPILER_PATH="));
471 putenv (xstrdup ("LIBRARY_PATH="));
473 while ((f = file_pop ()) != NULL)
475 char *line, *command;
476 FILE *stream = fopen (f->key, "r");
477 const char *const outname = frob_extension (f->key, ".rnw");
478 FILE *output = fopen (outname, "w");
480 while ((line = tfgets (stream)) != NULL)
482 switch (line[0])
484 case 'C':
485 case 'O':
486 maybe_tweak (line, f);
488 fprintf (output, "%s\n", line);
490 fclose (stream);
491 fclose (output);
492 rename (outname, f->key);
494 obstack_grow (&temporary_obstack, "cd ", 3);
495 obstack_grow (&temporary_obstack, f->dir, strlen (f->dir));
496 obstack_grow (&temporary_obstack, "; ", 2);
497 obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name));
498 obstack_1grow (&temporary_obstack, ' ');
499 obstack_grow (&temporary_obstack, f->args, strlen (f->args));
500 obstack_1grow (&temporary_obstack, ' ');
501 command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main));
503 if (tlink_verbose)
504 fprintf (stderr, _("collect: recompiling %s\n"), f->main);
505 if (tlink_verbose >= 3)
506 fprintf (stderr, "%s\n", command);
508 if (system (command) != 0)
509 return 0;
511 read_repo_file (f);
513 obstack_free (&temporary_obstack, temporary_firstobj);
515 return 1;
518 /* The first phase of processing: determine which object files have
519 .rpo files associated with them, and read in the information. */
521 static int
522 read_repo_files (object_lst)
523 char **object_lst;
525 char **object = object_lst;
527 for (; *object; object++)
529 const char *p;
530 file *f;
532 /* Don't bother trying for ld flags. */
533 if (*object[0] == '-')
534 continue;
536 p = frob_extension (*object, ".rpo");
538 if (! file_exists (p))
539 continue;
541 f = file_hash_lookup (p);
543 read_repo_file (f);
546 if (file_stack != NULL && ! recompile_files ())
547 return 0;
549 return (symbol_stack != NULL);
552 /* Add the demangled forms of any new symbols to the hash table. */
554 static void
555 demangle_new_symbols ()
557 symbol *sym;
559 while ((sym = symbol_pop ()) != NULL)
561 demangled *dem;
562 const char *p = cplus_demangle (sym->key, DMGL_PARAMS | DMGL_ANSI);
564 if (! p)
565 continue;
567 dem = demangled_hash_lookup (p, true);
568 dem->mangled = sym->key;
572 /* Step through the output of the linker, in the file named FNAME, and
573 adjust the settings for each symbol encountered. */
575 static int
576 scan_linker_output (fname)
577 const char *fname;
579 FILE *stream = fopen (fname, "r");
580 char *line;
582 while ((line = tfgets (stream)) != NULL)
584 char *p = line, *q;
585 symbol *sym;
586 int end;
588 while (*p && ISSPACE ((unsigned char) *p))
589 ++p;
591 if (! *p)
592 continue;
594 for (q = p; *q && ! ISSPACE ((unsigned char) *q); ++q)
597 /* Try the first word on the line. */
598 if (*p == '.')
599 ++p;
600 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
601 p += strlen (USER_LABEL_PREFIX);
603 end = ! *q;
604 *q = 0;
605 sym = symbol_hash_lookup (p, false);
607 /* Some SVR4 linkers produce messages like
608 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
610 if (! sym && ! end && strstr (q + 1, "Undefined symbol: "))
612 char *p = strrchr (q + 1, ' ');
613 p++;
614 if (*p == '.')
615 p++;
616 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
617 p += strlen (USER_LABEL_PREFIX);
618 sym = symbol_hash_lookup (p, false);
621 if (! sym && ! end)
622 /* Try a mangled name in quotes. */
624 const char *oldq = q + 1;
625 demangled *dem = 0;
626 q = 0;
628 /* First try `GNU style'. */
629 p = strchr (oldq, '`');
630 if (p)
631 p++, q = strchr (p, '\'');
632 /* Then try "double quotes". */
633 else if (p = strchr (oldq, '"'), p)
634 p++, q = strchr (p, '"');
635 else {
636 /* Then try entire line. */
637 q = strchr (oldq, 0);
638 if (q != oldq)
639 p = (char *)oldq;
642 if (p)
644 /* Don't let the strstr's below see the demangled name; we
645 might get spurious matches. */
646 p[-1] = '\0';
648 /* powerpc64-linux references .foo when calling function foo. */
649 if (*p == '.')
650 p++;
653 /* We need to check for certain error keywords here, or we would
654 mistakenly use GNU ld's "In function `foo':" message. */
655 if (q && (strstr (oldq, "ndefined")
656 || strstr (oldq, "nresolved")
657 || strstr (oldq, "nsatisfied")
658 || strstr (oldq, "ultiple")))
660 *q = 0;
661 dem = demangled_hash_lookup (p, false);
662 if (dem)
663 sym = symbol_hash_lookup (dem->mangled, false);
664 else
666 if (!strncmp (p, USER_LABEL_PREFIX,
667 strlen (USER_LABEL_PREFIX)))
668 p += strlen (USER_LABEL_PREFIX);
669 sym = symbol_hash_lookup (p, false);
674 if (sym && sym->tweaked)
676 fclose (stream);
677 return 0;
679 if (sym && !sym->tweaking)
681 if (tlink_verbose >= 2)
682 fprintf (stderr, _("collect: tweaking %s in %s\n"),
683 sym->key, sym->file->key);
684 sym->tweaking = 1;
685 file_push (sym->file);
688 obstack_free (&temporary_obstack, temporary_firstobj);
691 fclose (stream);
692 return (file_stack != NULL);
695 /* Entry point for tlink. Called from main in collect2.c.
697 Iteratively try to provide definitions for all the unresolved symbols
698 mentioned in the linker error messages.
700 LD_ARGV is an array of arguments for the linker.
701 OBJECT_LST is an array of object files that we may be able to recompile
702 to provide missing definitions. Currently ignored. */
704 void
705 do_tlink (ld_argv, object_lst)
706 char **ld_argv, **object_lst ATTRIBUTE_UNUSED;
708 int exit = tlink_execute ("ld", ld_argv, ldout);
710 tlink_init ();
712 if (exit)
714 int i = 0;
716 /* Until collect does a better job of figuring out which are object
717 files, assume that everything on the command line could be. */
718 if (read_repo_files (ld_argv))
719 while (exit && i++ < MAX_ITERATIONS)
721 if (tlink_verbose >= 3)
722 dump_file (ldout);
723 demangle_new_symbols ();
724 if (! scan_linker_output (ldout))
725 break;
726 if (! recompile_files ())
727 break;
728 if (tlink_verbose)
729 fprintf (stderr, _("collect: relinking\n"));
730 exit = tlink_execute ("ld", ld_argv, ldout);
734 dump_file (ldout);
735 unlink (ldout);
736 if (exit)
738 error ("ld returned %d exit status", exit);
739 collect_exit (exit);