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
26 #include "coretypes.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
47 struct file_hash_entry
*file
;
53 typedef struct file_hash_entry
62 typedef struct demangled_hash_entry
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 *));
74 hash_string_eq (s1_p
, 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;
84 hash_string_hash (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 *,
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
)
124 e
= htab_find_slot_with_hash (symbol_table
, string
,
125 (*htab_hash_string
) (string
),
126 create
? INSERT
: NO_INSERT
);
131 struct symbol_hash_entry
*v
;
132 *e
= v
= xcalloc (1, sizeof (*v
));
133 v
->key
= xstrdup (string
);
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
)
147 e
= htab_find_slot_with_hash (file_table
, string
,
148 (*htab_hash_string
) (string
),
152 struct file_hash_entry
*v
;
153 *e
= v
= xcalloc (1, sizeof (*v
));
154 v
->key
= xstrdup (string
);
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
)
169 e
= htab_find_slot_with_hash (demangled_table
, string
,
170 (*htab_hash_string
) (string
),
171 create
? INSERT
: NO_INSERT
);
176 struct demangled_hash_entry
*v
;
177 *e
= v
= xcalloc (1, sizeof (*v
));
178 v
->key
= xstrdup (string
);
185 struct symbol_stack_entry
188 struct symbol_stack_entry
*next
;
190 struct obstack symbol_stack_obstack
;
191 struct symbol_stack_entry
*symbol_stack
;
193 struct file_stack_entry
196 struct file_stack_entry
*next
;
198 struct obstack file_stack_obstack
;
199 struct file_stack_entry
*file_stack
;
205 struct symbol_stack_entry
*ep
= (struct symbol_stack_entry
*) obstack_alloc
206 (&symbol_stack_obstack
, sizeof (struct symbol_stack_entry
));
208 ep
->next
= symbol_stack
;
215 struct symbol_stack_entry
*ep
= symbol_stack
;
220 symbol_stack
= ep
->next
;
221 obstack_free (&symbol_stack_obstack
, ep
);
229 struct file_stack_entry
*ep
;
234 ep
= (struct file_stack_entry
*) obstack_alloc
235 (&file_stack_obstack
, sizeof (struct file_stack_entry
));
237 ep
->next
= file_stack
;
245 struct file_stack_entry
*ep
= file_stack
;
250 file_stack
= ep
->next
;
251 obstack_free (&file_stack_obstack
, ep
);
256 /* Other machinery. */
258 /* Initialize the tlink machinery. Called from do_tlink. */
265 symbol_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
267 file_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
269 demangled_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
272 obstack_begin (&symbol_stack_obstack
, 0);
273 obstack_begin (&file_stack_obstack
, 0);
275 p
= getenv ("TLINK_VERBOSE");
277 tlink_verbose
= atoi (p
);
289 tlink_execute (prog
, argv
, redir
)
294 collect_execute (prog
, argv
, redir
);
295 return collect_wait (prog
);
299 frob_extension (s
, ext
)
303 const char *p
= strrchr (s
, '/');
306 p
= strrchr (p
, '.');
310 obstack_grow (&temporary_obstack
, s
, p
- s
);
311 return obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
315 obstack_fgets (stream
, ob
)
320 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
321 obstack_1grow (ob
, c
);
322 if (obstack_object_size (ob
) == 0)
324 obstack_1grow (ob
, '\0');
325 return obstack_finish (ob
);
332 return obstack_fgets (stream
, &temporary_obstack
);
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. */
351 freadsym (stream
, f
, chosen
)
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. */
369 sym
->chosen
= 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
);
383 chosen
= sym
->chosen
;
387 sym
->chosen
= chosen
;
391 /* Read in the repo file denoted by F, and record all its information. */
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)
408 f
->args
= pfgets (stream
);
411 f
->dir
= pfgets (stream
);
414 f
->main
= pfgets (stream
);
417 freadsym (stream
, f
, 2);
420 freadsym (stream
, f
, 1);
423 freadsym (stream
, f
, 0);
426 obstack_free (&temporary_obstack
, temporary_firstobj
);
430 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
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. */
441 maybe_tweak (line
, f
)
445 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
447 if ((sym
->file
== f
&& sym
->tweaking
)
448 || (sym
->file
!= f
&& line
[0] == 'C'))
460 /* Update the repo files for each of the object files we have adjusted and
463 XXX Should this use collect_execute instead of system? */
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
)
486 maybe_tweak (line
, f
);
488 fprintf (output
, "%s\n", line
);
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
));
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)
513 obstack_free (&temporary_obstack
, temporary_firstobj
);
518 /* The first phase of processing: determine which object files have
519 .rpo files associated with them, and read in the information. */
522 read_repo_files (object_lst
)
525 char **object
= object_lst
;
527 for (; *object
; object
++)
532 /* Don't bother trying for ld flags. */
533 if (*object
[0] == '-')
536 p
= frob_extension (*object
, ".rpo");
538 if (! file_exists (p
))
541 f
= file_hash_lookup (p
);
546 if (file_stack
!= NULL
&& ! recompile_files ())
549 return (symbol_stack
!= NULL
);
552 /* Add the demangled forms of any new symbols to the hash table. */
555 demangle_new_symbols ()
559 while ((sym
= symbol_pop ()) != NULL
)
562 const char *p
= cplus_demangle (sym
->key
, DMGL_PARAMS
| DMGL_ANSI
);
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. */
576 scan_linker_output (fname
)
579 FILE *stream
= fopen (fname
, "r");
582 while ((line
= tfgets (stream
)) != NULL
)
588 while (*p
&& ISSPACE ((unsigned char) *p
))
594 for (q
= p
; *q
&& ! ISSPACE ((unsigned char) *q
); ++q
)
597 /* Try the first word on the line. */
600 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
601 p
+= strlen (USER_LABEL_PREFIX
);
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, ' ');
616 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
617 p
+= strlen (USER_LABEL_PREFIX
);
618 sym
= symbol_hash_lookup (p
, false);
622 /* Try a mangled name in quotes. */
624 const char *oldq
= q
+ 1;
628 /* First try `GNU style'. */
629 p
= strchr (oldq
, '`');
631 p
++, q
= strchr (p
, '\'');
632 /* Then try "double quotes". */
633 else if (p
= strchr (oldq
, '"'), p
)
634 p
++, q
= strchr (p
, '"');
636 /* Don't let the strstr's below see the demangled name; we
637 might get spurious matches. */
641 /* We need to check for certain error keywords here, or we would
642 mistakenly use GNU ld's "In function `foo':" message. */
643 if (q
&& (strstr (oldq
, "ndefined")
644 || strstr (oldq
, "nresolved")
645 || strstr (oldq
, "nsatisfied")
646 || strstr (oldq
, "ultiple")))
649 dem
= demangled_hash_lookup (p
, false);
651 sym
= symbol_hash_lookup (dem
->mangled
, false);
654 if (!strncmp (p
, USER_LABEL_PREFIX
,
655 strlen (USER_LABEL_PREFIX
)))
656 p
+= strlen (USER_LABEL_PREFIX
);
657 sym
= symbol_hash_lookup (p
, false);
662 if (sym
&& sym
->tweaked
)
667 if (sym
&& !sym
->tweaking
)
669 if (tlink_verbose
>= 2)
670 fprintf (stderr
, _("collect: tweaking %s in %s\n"),
671 sym
->key
, sym
->file
->key
);
673 file_push (sym
->file
);
676 obstack_free (&temporary_obstack
, temporary_firstobj
);
680 return (file_stack
!= NULL
);
683 /* Entry point for tlink. Called from main in collect2.c.
685 Iteratively try to provide definitions for all the unresolved symbols
686 mentioned in the linker error messages.
688 LD_ARGV is an array of arguments for the linker.
689 OBJECT_LST is an array of object files that we may be able to recompile
690 to provide missing definitions. Currently ignored. */
693 do_tlink (ld_argv
, object_lst
)
694 char **ld_argv
, **object_lst ATTRIBUTE_UNUSED
;
696 int exit
= tlink_execute ("ld", ld_argv
, ldout
);
704 /* Until collect does a better job of figuring out which are object
705 files, assume that everything on the command line could be. */
706 if (read_repo_files (ld_argv
))
707 while (exit
&& i
++ < MAX_ITERATIONS
)
709 if (tlink_verbose
>= 3)
711 demangle_new_symbols ();
712 if (! scan_linker_output (ldout
))
714 if (! recompile_files ())
717 fprintf (stderr
, _("collect: relinking\n"));
718 exit
= tlink_execute ("ld", ld_argv
, ldout
);
726 error ("ld returned %d exit status", exit
);