1 /* Scan linker error messages for missing template instantiations and provide
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007, 2008
5 Free Software Foundation, Inc.
6 Contributed by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
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 static char initial_cwd
[MAXPATHLEN
+ 1];
43 /* Hash table boilerplate for working with htab_t. We have hash tables
44 for symbol names, file names, and demangled symbols. */
46 typedef struct symbol_hash_entry
49 struct file_hash_entry
*file
;
55 typedef struct file_hash_entry
64 typedef struct demangled_hash_entry
70 /* Hash and comparison functions for these hash tables. */
72 static int hash_string_eq (const void *, const void *);
73 static hashval_t
hash_string_hash (const void *);
76 hash_string_eq (const void *s1_p
, 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;
84 hash_string_hash (const void *s_p
)
86 const char *const *s
= (const char *const *) s_p
;
87 return (*htab_hash_string
) (*s
);
90 static htab_t symbol_table
;
92 static struct symbol_hash_entry
* symbol_hash_lookup (const char *, int);
93 static struct file_hash_entry
* file_hash_lookup (const char *);
94 static struct demangled_hash_entry
*demangled_hash_lookup (const char *, int);
95 static void symbol_push (symbol
*);
96 static symbol
* symbol_pop (void);
97 static void file_push (file
*);
98 static file
* file_pop (void);
99 static void tlink_init (void);
100 static int tlink_execute (const char *, char **, const char *, const char *);
101 static char * frob_extension (const char *, const char *);
102 static char * obstack_fgets (FILE *, struct obstack
*);
103 static char * tfgets (FILE *);
104 static char * pfgets (FILE *);
105 static void freadsym (FILE *, file
*, int);
106 static void read_repo_file (file
*);
107 static void maybe_tweak (char *, file
*);
108 static int recompile_files (void);
109 static int read_repo_files (char **);
110 static void demangle_new_symbols (void);
111 static int scan_linker_output (const char *);
113 /* Look up an entry in the symbol hash table. */
115 static struct symbol_hash_entry
*
116 symbol_hash_lookup (const char *string
, int create
)
119 e
= htab_find_slot_with_hash (symbol_table
, string
,
120 (*htab_hash_string
) (string
),
121 create
? INSERT
: NO_INSERT
);
126 struct symbol_hash_entry
*v
;
127 *e
= v
= XCNEW (struct symbol_hash_entry
);
128 v
->key
= xstrdup (string
);
130 return (struct symbol_hash_entry
*) *e
;
133 static htab_t file_table
;
135 /* Look up an entry in the file hash table. */
137 static struct file_hash_entry
*
138 file_hash_lookup (const char *string
)
141 e
= htab_find_slot_with_hash (file_table
, string
,
142 (*htab_hash_string
) (string
),
146 struct file_hash_entry
*v
;
147 *e
= v
= XCNEW (struct file_hash_entry
);
148 v
->key
= xstrdup (string
);
150 return (struct file_hash_entry
*) *e
;
153 static htab_t demangled_table
;
155 /* Look up an entry in the demangled name hash table. */
157 static struct demangled_hash_entry
*
158 demangled_hash_lookup (const char *string
, int create
)
161 e
= htab_find_slot_with_hash (demangled_table
, string
,
162 (*htab_hash_string
) (string
),
163 create
? INSERT
: NO_INSERT
);
168 struct demangled_hash_entry
*v
;
169 *e
= v
= XCNEW (struct demangled_hash_entry
);
170 v
->key
= xstrdup (string
);
172 return (struct demangled_hash_entry
*) *e
;
177 struct symbol_stack_entry
180 struct symbol_stack_entry
*next
;
182 struct obstack symbol_stack_obstack
;
183 struct symbol_stack_entry
*symbol_stack
;
185 struct file_stack_entry
188 struct file_stack_entry
*next
;
190 struct obstack file_stack_obstack
;
191 struct file_stack_entry
*file_stack
;
194 symbol_push (symbol
*p
)
196 struct symbol_stack_entry
*ep
197 = XOBNEW (&symbol_stack_obstack
, struct symbol_stack_entry
);
199 ep
->next
= symbol_stack
;
206 struct symbol_stack_entry
*ep
= symbol_stack
;
211 symbol_stack
= ep
->next
;
212 obstack_free (&symbol_stack_obstack
, ep
);
219 struct file_stack_entry
*ep
;
224 ep
= XOBNEW (&file_stack_obstack
, struct file_stack_entry
);
226 ep
->next
= file_stack
;
234 struct file_stack_entry
*ep
= file_stack
;
239 file_stack
= ep
->next
;
240 obstack_free (&file_stack_obstack
, ep
);
245 /* Other machinery. */
247 /* Initialize the tlink machinery. Called from do_tlink. */
254 symbol_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
256 file_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
258 demangled_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
261 obstack_begin (&symbol_stack_obstack
, 0);
262 obstack_begin (&file_stack_obstack
, 0);
264 p
= getenv ("TLINK_VERBOSE");
266 tlink_verbose
= atoi (p
);
276 getcwd (initial_cwd
, sizeof (initial_cwd
));
280 tlink_execute (const char *prog
, char **argv
, const char *outname
,
285 pex
= collect_execute (prog
, argv
, outname
, errname
);
286 return collect_wait (prog
, pex
);
290 frob_extension (const char *s
, const char *ext
)
292 const char *p
= strrchr (s
, '/');
295 p
= strrchr (p
, '.');
299 obstack_grow (&temporary_obstack
, s
, p
- s
);
300 return (char *) obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
304 obstack_fgets (FILE *stream
, struct obstack
*ob
)
307 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
308 obstack_1grow (ob
, c
);
309 if (obstack_object_size (ob
) == 0)
311 obstack_1grow (ob
, '\0');
312 return XOBFINISH (ob
, char *);
316 tfgets (FILE *stream
)
318 return obstack_fgets (stream
, &temporary_obstack
);
322 pfgets (FILE *stream
)
324 return xstrdup (tfgets (stream
));
327 /* Real tlink code. */
329 /* Subroutine of read_repo_file. We are reading the repo file for file F,
330 which is coming in on STREAM, and the symbol that comes next in STREAM
331 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
333 XXX "provided" is unimplemented, both here and in the compiler. */
336 freadsym (FILE *stream
, file
*f
, int chosen
)
341 const char *name
= tfgets (stream
);
342 sym
= symbol_hash_lookup (name
, true);
345 if (sym
->file
== NULL
)
347 /* We didn't have this symbol already, so we choose this file. */
351 sym
->chosen
= chosen
;
355 /* We want this file; cast aside any pretender. */
357 if (sym
->chosen
&& sym
->file
!= f
)
359 if (sym
->chosen
== 1)
360 file_push (sym
->file
);
365 chosen
= sym
->chosen
;
369 sym
->chosen
= chosen
;
373 /* Read in the repo file denoted by F, and record all its information. */
376 read_repo_file (file
*f
)
379 FILE *stream
= fopen (f
->key
, "r");
381 if (tlink_verbose
>= 2)
382 fprintf (stderr
, _("collect: reading %s\n"), f
->key
);
384 while (fscanf (stream
, "%c ", &c
) == 1)
389 f
->args
= pfgets (stream
);
392 f
->dir
= pfgets (stream
);
395 f
->main
= pfgets (stream
);
398 freadsym (stream
, f
, 2);
401 freadsym (stream
, f
, 1);
404 freadsym (stream
, f
, 0);
407 obstack_free (&temporary_obstack
, temporary_firstobj
);
411 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
416 /* We might want to modify LINE, which is a symbol line from file F. We do
417 this if either we saw an error message referring to the symbol in
418 question, or we have already allocated the symbol to another file and
419 this one wants to emit it as well. */
422 maybe_tweak (char *line
, file
*f
)
424 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
426 if ((sym
->file
== f
&& sym
->tweaking
)
427 || (sym
->file
!= f
&& line
[0] == 'C'))
439 /* Update the repo files for each of the object files we have adjusted and
443 recompile_files (void)
447 putenv (xstrdup ("COMPILER_PATH="));
448 putenv (xstrdup ("LIBRARY_PATH="));
450 while ((f
= file_pop ()) != NULL
)
455 struct obstack arg_stack
;
456 FILE *stream
= fopen (f
->key
, "r");
457 const char *const outname
= frob_extension (f
->key
, ".rnw");
458 FILE *output
= fopen (outname
, "w");
460 while ((line
= tfgets (stream
)) != NULL
)
466 maybe_tweak (line
, f
);
468 fprintf (output
, "%s\n", line
);
472 /* On Windows "rename" returns -1 and sets ERRNO to EACCESS if
473 the new file name already exists. Therefore, we explicitly
474 remove the old file first. */
475 if (remove (f
->key
) == -1)
476 fatal_perror ("removing .rpo file");
477 if (rename (outname
, f
->key
) == -1)
478 fatal_perror ("renaming .rpo file");
482 error ("repository file '%s' does not contain command-line "
483 "arguments", f
->key
);
487 /* Build a null-terminated argv array suitable for
488 tlink_execute(). Manipulate arguments on the arg_stack while
489 building argv on the temporary_obstack. */
491 obstack_init (&arg_stack
);
492 obstack_ptr_grow (&temporary_obstack
, c_file_name
);
494 for (p
= f
->args
; *p
!= '\0'; p
= q
+ 1)
496 /* Arguments are delimited by single-quotes. Find the
498 p
= strchr (p
, '\'');
502 /* Find the closing quote. */
503 q
= strchr (p
+ 1, '\'');
507 obstack_grow (&arg_stack
, p
+ 1, q
- (p
+ 1));
509 /* Replace '\'' with '. This is how set_collect_gcc_options
510 encodes a single-quote. */
511 while (q
[1] == '\\' && q
[2] == '\'' && q
[3] == '\'')
515 r
= strchr (q
+ 4, '\'');
519 obstack_grow (&arg_stack
, q
+ 3, r
- (q
+ 3));
523 obstack_1grow (&arg_stack
, '\0');
524 obstack_ptr_grow (&temporary_obstack
, obstack_finish (&arg_stack
));
527 obstack_ptr_grow (&temporary_obstack
, f
->main
);
528 obstack_ptr_grow (&temporary_obstack
, NULL
);
529 argv
= XOBFINISH (&temporary_obstack
, char **);
532 fprintf (stderr
, _("collect: recompiling %s\n"), f
->main
);
534 if (chdir (f
->dir
) != 0
535 || tlink_execute (c_file_name
, argv
, NULL
, NULL
) != 0
536 || chdir (initial_cwd
) != 0)
541 obstack_free (&arg_stack
, NULL
);
542 obstack_free (&temporary_obstack
, temporary_firstobj
);
547 /* The first phase of processing: determine which object files have
548 .rpo files associated with them, and read in the information. */
551 read_repo_files (char **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 (void)
587 while ((sym
= symbol_pop ()) != NULL
)
590 const char *p
= cplus_demangle (sym
->key
, DMGL_PARAMS
| DMGL_ANSI
);
595 dem
= demangled_hash_lookup (p
, true);
596 dem
->mangled
= sym
->key
;
600 /* Step through the output of the linker, in the file named FNAME, and
601 adjust the settings for each symbol encountered. */
604 scan_linker_output (const char *fname
)
606 FILE *stream
= fopen (fname
, "r");
608 int skip_next_in_line
= 0;
610 while ((line
= tfgets (stream
)) != NULL
)
617 /* On darwin9, we might have to skip " in " lines as well. */
618 if (skip_next_in_line
619 && strstr (p
, " in "))
621 skip_next_in_line
= 0;
623 while (*p
&& ISSPACE ((unsigned char) *p
))
629 for (q
= p
; *q
&& ! ISSPACE ((unsigned char) *q
); ++q
)
632 /* Try the first word on the line. */
635 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
636 p
+= strlen (USER_LABEL_PREFIX
);
640 sym
= symbol_hash_lookup (p
, false);
642 /* Some SVR4 linkers produce messages like
643 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
645 if (! sym
&& ! end
&& strstr (q
+ 1, "Undefined symbol: "))
647 char *p
= strrchr (q
+ 1, ' ');
651 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
652 p
+= strlen (USER_LABEL_PREFIX
);
653 sym
= symbol_hash_lookup (p
, false);
657 /* Try a mangled name in quotes. */
663 /* On darwin9, we look for "foo" referenced from:\n\(.* in .*\n\)* */
664 if (strcmp (oldq
, "referenced from:") == 0)
666 /* We have to remember that we found a symbol to tweak. */
669 /* We actually want to start from the first word on the
673 /* Since the format is multiline, we have to skip
674 following lines with " in ". */
675 skip_next_in_line
= 1;
678 /* First try `GNU style'. */
679 p
= strchr (oldq
, '`');
681 p
++, q
= strchr (p
, '\'');
682 /* Then try "double quotes". */
683 else if (p
= strchr (oldq
, '"'), p
)
684 p
++, q
= strchr (p
, '"');
685 /* Then try 'single quotes'. */
686 else if (p
= strchr (oldq
, '\''), p
)
687 p
++, q
= strchr (p
, '\'');
689 /* Then try entire line. */
690 q
= strchr (oldq
, 0);
697 /* Don't let the strstr's below see the demangled name; we
698 might get spurious matches. */
701 /* powerpc64-linux references .foo when calling function foo. */
706 /* We need to check for certain error keywords here, or we would
707 mistakenly use GNU ld's "In function `foo':" message. */
709 || strstr (oldq
, "ndefined")
710 || strstr (oldq
, "nresolved")
711 || strstr (oldq
, "nsatisfied")
712 || strstr (oldq
, "ultiple")))
715 dem
= demangled_hash_lookup (p
, false);
717 sym
= symbol_hash_lookup (dem
->mangled
, false);
720 if (!strncmp (p
, USER_LABEL_PREFIX
,
721 strlen (USER_LABEL_PREFIX
)))
722 p
+= strlen (USER_LABEL_PREFIX
);
723 sym
= symbol_hash_lookup (p
, false);
728 if (sym
&& sym
->tweaked
)
730 error ("'%s' was assigned to '%s', but was not defined "
731 "during recompilation, or vice versa",
732 sym
->key
, sym
->file
->key
);
736 if (sym
&& !sym
->tweaking
)
738 if (tlink_verbose
>= 2)
739 fprintf (stderr
, _("collect: tweaking %s in %s\n"),
740 sym
->key
, sym
->file
->key
);
742 file_push (sym
->file
);
745 obstack_free (&temporary_obstack
, temporary_firstobj
);
749 return (file_stack
!= NULL
);
752 /* Entry point for tlink. Called from main in collect2.c.
754 Iteratively try to provide definitions for all the unresolved symbols
755 mentioned in the linker error messages.
757 LD_ARGV is an array of arguments for the linker.
758 OBJECT_LST is an array of object files that we may be able to recompile
759 to provide missing definitions. Currently ignored. */
762 do_tlink (char **ld_argv
, char **object_lst ATTRIBUTE_UNUSED
)
764 int exit
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
);
772 /* Until collect does a better job of figuring out which are object
773 files, assume that everything on the command line could be. */
774 if (read_repo_files (ld_argv
))
775 while (exit
&& i
++ < MAX_ITERATIONS
)
777 if (tlink_verbose
>= 3)
779 dump_file (ldout
, stdout
);
780 dump_file (lderrout
, stderr
);
782 demangle_new_symbols ();
783 if (! scan_linker_output (ldout
)
784 && ! scan_linker_output (lderrout
))
786 if (! recompile_files ())
789 fprintf (stderr
, _("collect: relinking\n"));
790 exit
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
);
794 dump_file (ldout
, stdout
);
796 dump_file (lderrout
, stderr
);
800 error ("ld returned %d exit status", exit
);