1 /* Scan linker error messages for missing template instantiations and provide
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
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
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
31 #define MAX_ITERATIONS 17
33 /* Obstack allocation and deallocation routines. */
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
37 /* Defined in the automatically-generated underscore.c. */
38 extern int prepends_underscore
;
40 static int tlink_verbose
;
42 /* Hash table boilerplate for working with hash.[ch]. We have hash tables
43 for symbol names, file names, and demangled symbols. */
45 typedef struct symbol_hash_entry
47 struct hash_entry root
;
48 struct file_hash_entry
*file
;
54 typedef struct file_hash_entry
56 struct hash_entry root
;
63 typedef struct demangled_hash_entry
65 struct hash_entry root
;
69 static struct hash_table symbol_table
;
71 static struct hash_entry
* symbol_hash_newfunc
PARAMS ((struct hash_entry
*,
74 static struct symbol_hash_entry
* symbol_hash_lookup
PARAMS ((const char *,
76 static struct hash_entry
* file_hash_newfunc
PARAMS ((struct hash_entry
*,
79 static struct file_hash_entry
* file_hash_lookup
PARAMS ((const char *));
80 static struct hash_entry
* demangled_hash_newfunc
PARAMS ((struct hash_entry
*,
83 static struct demangled_hash_entry
*
84 demangled_hash_lookup
PARAMS ((const char *, int));
85 static void symbol_push
PARAMS ((symbol
*));
86 static symbol
* symbol_pop
PARAMS ((void));
87 static void file_push
PARAMS ((file
*));
88 static file
* file_pop
PARAMS ((void));
89 static void tlink_init
PARAMS ((void));
90 static int tlink_execute
PARAMS ((const char *, char **, const char *));
91 static char * frob_extension
PARAMS ((const char *, const char *));
92 static char * obstack_fgets
PARAMS ((FILE *, struct obstack
*));
93 static char * tfgets
PARAMS ((FILE *));
94 static char * pfgets
PARAMS ((FILE *));
95 static void freadsym
PARAMS ((FILE *, file
*, int));
96 static void read_repo_file
PARAMS ((file
*));
97 static void maybe_tweak
PARAMS ((char *, file
*));
98 static int recompile_files
PARAMS ((void));
99 static int read_repo_files
PARAMS ((char **));
100 static void demangle_new_symbols
PARAMS ((void));
101 static int scan_linker_output
PARAMS ((const char *));
103 /* Create a new entry for the symbol hash table.
104 Passed to hash_table_init. */
106 static struct hash_entry
*
107 symbol_hash_newfunc (entry
, table
, string
)
108 struct hash_entry
*entry
;
109 struct hash_table
*table
;
110 hash_table_key string ATTRIBUTE_UNUSED
;
112 struct symbol_hash_entry
*ret
= (struct symbol_hash_entry
*) entry
;
115 ret
= ((struct symbol_hash_entry
*)
116 hash_allocate (table
, sizeof (struct symbol_hash_entry
)));
124 return (struct hash_entry
*) ret
;
127 /* Look up an entry in the symbol hash table. */
129 static struct symbol_hash_entry
*
130 symbol_hash_lookup (string
, create
)
134 return ((struct symbol_hash_entry
*)
135 hash_lookup (&symbol_table
, (const hash_table_key
) string
,
136 create
, string_copy
));
139 static struct hash_table file_table
;
141 /* Create a new entry for the file hash table.
142 Passed to hash_table_init. */
144 static struct hash_entry
*
145 file_hash_newfunc (entry
, table
, string
)
146 struct hash_entry
*entry
;
147 struct hash_table
*table
;
148 hash_table_key string ATTRIBUTE_UNUSED
;
150 struct file_hash_entry
*ret
= (struct file_hash_entry
*) entry
;
153 ret
= ((struct file_hash_entry
*)
154 hash_allocate (table
, sizeof (struct file_hash_entry
)));
162 return (struct hash_entry
*) ret
;
165 /* Look up an entry in the file hash table. */
167 static struct file_hash_entry
*
168 file_hash_lookup (string
)
171 return ((struct file_hash_entry
*)
172 hash_lookup (&file_table
, (const hash_table_key
) string
, true,
176 static struct hash_table demangled_table
;
178 /* Create a new entry for the demangled name hash table.
179 Passed to hash_table_init. */
181 static struct hash_entry
*
182 demangled_hash_newfunc (entry
, table
, string
)
183 struct hash_entry
*entry
;
184 struct hash_table
*table
;
185 hash_table_key string ATTRIBUTE_UNUSED
;
187 struct demangled_hash_entry
*ret
= (struct demangled_hash_entry
*) entry
;
190 ret
= ((struct demangled_hash_entry
*)
191 hash_allocate (table
, sizeof (struct demangled_hash_entry
)));
196 return (struct hash_entry
*) ret
;
199 /* Look up an entry in the demangled name hash table. */
201 static struct demangled_hash_entry
*
202 demangled_hash_lookup (string
, create
)
206 return ((struct demangled_hash_entry
*)
207 hash_lookup (&demangled_table
, (const hash_table_key
) string
,
208 create
, string_copy
));
213 struct symbol_stack_entry
216 struct symbol_stack_entry
*next
;
218 struct obstack symbol_stack_obstack
;
219 struct symbol_stack_entry
*symbol_stack
;
221 struct file_stack_entry
224 struct file_stack_entry
*next
;
226 struct obstack file_stack_obstack
;
227 struct file_stack_entry
*file_stack
;
233 struct symbol_stack_entry
*ep
= (struct symbol_stack_entry
*) obstack_alloc
234 (&symbol_stack_obstack
, sizeof (struct symbol_stack_entry
));
236 ep
->next
= symbol_stack
;
243 struct symbol_stack_entry
*ep
= symbol_stack
;
248 symbol_stack
= ep
->next
;
249 obstack_free (&symbol_stack_obstack
, ep
);
257 struct file_stack_entry
*ep
;
262 ep
= (struct file_stack_entry
*) obstack_alloc
263 (&file_stack_obstack
, sizeof (struct file_stack_entry
));
265 ep
->next
= file_stack
;
273 struct file_stack_entry
*ep
= file_stack
;
278 file_stack
= ep
->next
;
279 obstack_free (&file_stack_obstack
, ep
);
284 /* Other machinery. */
286 /* Initialize the tlink machinery. Called from do_tlink. */
293 hash_table_init (&symbol_table
, symbol_hash_newfunc
, string_hash
,
295 hash_table_init (&file_table
, file_hash_newfunc
, string_hash
,
297 hash_table_init (&demangled_table
, demangled_hash_newfunc
,
298 string_hash
, string_compare
);
299 obstack_begin (&symbol_stack_obstack
, 0);
300 obstack_begin (&file_stack_obstack
, 0);
302 p
= getenv ("TLINK_VERBOSE");
304 tlink_verbose
= atoi (p
);
316 tlink_execute (prog
, argv
, redir
)
321 collect_execute (prog
, argv
, redir
);
322 return collect_wait (prog
);
326 frob_extension (s
, ext
)
330 const char *p
= strrchr (s
, '/');
333 p
= strrchr (p
, '.');
337 obstack_grow (&temporary_obstack
, s
, p
- s
);
338 return obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
342 obstack_fgets (stream
, ob
)
347 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
348 obstack_1grow (ob
, c
);
349 if (obstack_object_size (ob
) == 0)
351 obstack_1grow (ob
, '\0');
352 return obstack_finish (ob
);
359 return obstack_fgets (stream
, &temporary_obstack
);
366 return obstack_fgets (stream
, &permanent_obstack
);
369 /* Real tlink code. */
371 /* Subroutine of read_repo_file. We are reading the repo file for file F,
372 which is coming in on STREAM, and the symbol that comes next in STREAM
373 is offerred, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
375 XXX "provided" is unimplemented, both here and in the compiler. */
378 freadsym (stream
, f
, chosen
)
386 const char *name
= tfgets (stream
);
387 sym
= symbol_hash_lookup (name
, true);
390 if (sym
->file
== NULL
)
392 /* We didn't have this symbol already, so we choose this file. */
396 sym
->chosen
= chosen
;
400 /* We want this file; cast aside any pretender. */
402 if (sym
->chosen
&& sym
->file
!= f
)
404 if (sym
->chosen
== 1)
405 file_push (sym
->file
);
410 chosen
= sym
->chosen
;
414 sym
->chosen
= chosen
;
418 /* Read in the repo file denoted by F, and record all its information. */
425 FILE *stream
= fopen ((char*) f
->root
.key
, "r");
427 if (tlink_verbose
>= 2)
428 fprintf (stderr
, _("collect: reading %s\n"),
429 (char*) f
->root
.key
);
431 while (fscanf (stream
, "%c ", &c
) == 1)
436 f
->args
= pfgets (stream
);
439 f
->dir
= pfgets (stream
);
442 f
->main
= pfgets (stream
);
445 freadsym (stream
, f
, 2);
448 freadsym (stream
, f
, 1);
451 freadsym (stream
, f
, 0);
454 obstack_free (&temporary_obstack
, temporary_firstobj
);
458 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
463 /* We might want to modify LINE, which is a symbol line from file F. We do
464 this if either we saw an error message referring to the symbol in
465 question, or we have already allocated the symbol to another file and
466 this one wants to emit it as well. */
469 maybe_tweak (line
, f
)
473 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
475 if ((sym
->file
== f
&& sym
->tweaking
)
476 || (sym
->file
!= f
&& line
[0] == 'C'))
488 /* Update the repo files for each of the object files we have adjusted and
491 XXX Should this use collect_execute instead of system? */
498 putenv (xstrdup ("COMPILER_PATH"));
499 putenv (xstrdup ("LIBRARY_PATH"));
501 while ((f
= file_pop ()) != NULL
)
503 char *line
, *command
;
504 FILE *stream
= fopen ((char*) f
->root
.key
, "r");
505 const char *const outname
= frob_extension ((char*) f
->root
.key
, ".rnw");
506 FILE *output
= fopen (outname
, "w");
508 while ((line
= tfgets (stream
)) != NULL
)
514 maybe_tweak (line
, f
);
516 fprintf (output
, "%s\n", line
);
520 rename (outname
, (char*) f
->root
.key
);
522 obstack_grow (&temporary_obstack
, "cd ", 3);
523 obstack_grow (&temporary_obstack
, f
->dir
, strlen (f
->dir
));
524 obstack_grow (&temporary_obstack
, "; ", 2);
525 obstack_grow (&temporary_obstack
, c_file_name
, strlen (c_file_name
));
526 obstack_1grow (&temporary_obstack
, ' ');
527 obstack_grow (&temporary_obstack
, f
->args
, strlen (f
->args
));
528 obstack_1grow (&temporary_obstack
, ' ');
529 command
= obstack_copy0 (&temporary_obstack
, f
->main
, strlen (f
->main
));
532 fprintf (stderr
, _("collect: recompiling %s\n"), f
->main
);
533 if (tlink_verbose
>= 3)
534 fprintf (stderr
, "%s\n", command
);
536 if (system (command
) != 0)
541 obstack_free (&temporary_obstack
, temporary_firstobj
);
546 /* The first phase of processing: determine which object files have
547 .rpo files associated with them, and read in the information. */
550 read_repo_files (object_lst
)
553 char **object
= object_lst
;
555 for (; *object
; object
++)
560 /* Don't bother trying for ld flags. */
561 if (*object
[0] == '-')
564 p
= frob_extension (*object
, ".rpo");
566 if (! file_exists (p
))
569 f
= file_hash_lookup (p
);
574 if (file_stack
!= NULL
&& ! recompile_files ())
577 return (symbol_stack
!= NULL
);
580 /* Add the demangled forms of any new symbols to the hash table. */
583 demangle_new_symbols ()
587 while ((sym
= symbol_pop ()) != NULL
)
590 const char *p
= cplus_demangle ((char*) sym
->root
.key
,
591 DMGL_PARAMS
| DMGL_ANSI
);
596 dem
= demangled_hash_lookup (p
, true);
597 dem
->mangled
= (char*) sym
->root
.key
;
601 /* Step through the output of the linker, in the file named FNAME, and
602 adjust the settings for each symbol encountered. */
605 scan_linker_output (fname
)
608 FILE *stream
= fopen (fname
, "r");
611 while ((line
= tfgets (stream
)) != NULL
)
617 while (*p
&& ISSPACE ((unsigned char)*p
))
623 for (q
= p
; *q
&& ! ISSPACE ((unsigned char)*q
); ++q
)
626 /* Try the first word on the line. */
629 if (*p
== '_' && prepends_underscore
)
634 sym
= symbol_hash_lookup (p
, false);
636 /* Some SVR4 linkers produce messages like
637 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
639 if (! sym
&& ! end
&& strstr (q
+1, "Undefined symbol: "))
641 char *p
= strrchr (q
+1, ' ');
645 if (*p
== '_' && prepends_underscore
)
647 sym
= symbol_hash_lookup (p
, false);
651 /* Try a mangled name in quotes. */
653 const char *oldq
= q
+1;
657 /* First try `GNU style'. */
658 p
= strchr (oldq
, '`');
660 p
++, q
= strchr (p
, '\'');
661 /* Then try "double quotes". */
662 else if (p
= strchr (oldq
, '"'), p
)
663 p
++, q
= strchr (p
, '"');
665 /* Don't let the strstr's below see the demangled name; we
666 might get spurious matches. */
670 /* We need to check for certain error keywords here, or we would
671 mistakenly use GNU ld's "In function `foo':" message. */
672 if (q
&& (strstr (oldq
, "ndefined")
673 || strstr (oldq
, "nresolved")
674 || strstr (oldq
, "nsatisfied")
675 || strstr (oldq
, "ultiple")))
678 dem
= demangled_hash_lookup (p
, false);
680 sym
= symbol_hash_lookup (dem
->mangled
, false);
683 if (*p
== '_' && prepends_underscore
)
685 sym
= symbol_hash_lookup (p
, false);
690 if (sym
&& sym
->tweaked
)
695 if (sym
&& !sym
->tweaking
)
697 if (tlink_verbose
>= 2)
698 fprintf (stderr
, _("collect: tweaking %s in %s\n"),
699 (char*) sym
->root
.key
, (char*) sym
->file
->root
.key
);
701 file_push (sym
->file
);
704 obstack_free (&temporary_obstack
, temporary_firstobj
);
708 return (file_stack
!= NULL
);
711 /* Entry point for tlink. Called from main in collect2.c.
713 Iteratively try to provide definitions for all the unresolved symbols
714 mentioned in the linker error messages.
716 LD_ARGV is an array of arguments for the linker.
717 OBJECT_LST is an array of object files that we may be able to recompile
718 to provide missing definitions. Currently ignored. */
721 do_tlink (ld_argv
, object_lst
)
722 char **ld_argv
, **object_lst ATTRIBUTE_UNUSED
;
724 int exit
= tlink_execute ("ld", ld_argv
, ldout
);
732 /* Until collect does a better job of figuring out which are object
733 files, assume that everything on the command line could be. */
734 if (read_repo_files (ld_argv
))
735 while (exit
&& i
++ < MAX_ITERATIONS
)
737 if (tlink_verbose
>= 3)
739 demangle_new_symbols ();
740 if (! scan_linker_output (ldout
))
742 if (! recompile_files ())
745 fprintf (stderr
, _("collect: relinking\n"));
746 exit
= tlink_execute ("ld", ld_argv
, ldout
);
754 error ("ld returned %d exit status", exit
);