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 2009, 2010, 2011 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"
33 #include "filenames.h"
34 #include "diagnostic-core.h"
37 /* TARGET_64BIT may be defined to use driver specific functionality. */
39 #define TARGET_64BIT TARGET_64BIT_DEFAULT
41 #define MAX_ITERATIONS 17
43 /* Defined in the automatically-generated underscore.c. */
44 extern int prepends_underscore
;
46 static int tlink_verbose
;
48 static char *initial_cwd
;
50 /* Hash table boilerplate for working with htab_t. We have hash tables
51 for symbol names, file names, and demangled symbols. */
53 typedef struct symbol_hash_entry
56 struct file_hash_entry
*file
;
62 typedef struct file_hash_entry
71 typedef const char *str
;
73 typedef struct demangled_hash_entry
79 /* Hash and comparison functions for these hash tables. */
81 static int hash_string_eq (const void *, const void *);
82 static hashval_t
hash_string_hash (const void *);
85 hash_string_eq (const void *s1_p
, const void *s2_p
)
87 const char *const *s1
= (const char *const *) s1_p
;
88 const char *s2
= (const char *) s2_p
;
89 return strcmp (*s1
, s2
) == 0;
93 hash_string_hash (const void *s_p
)
95 const char *const *s
= (const char *const *) s_p
;
96 return (*htab_hash_string
) (*s
);
99 static htab_t symbol_table
;
101 static struct symbol_hash_entry
* symbol_hash_lookup (const char *, int);
102 static struct file_hash_entry
* file_hash_lookup (const char *);
103 static struct demangled_hash_entry
*demangled_hash_lookup (const char *, int);
104 static void symbol_push (symbol
*);
105 static symbol
* symbol_pop (void);
106 static void file_push (file
*);
107 static file
* file_pop (void);
108 static void tlink_init (void);
109 static int tlink_execute (const char *, char **, const char *, const char *);
110 static char * frob_extension (const char *, const char *);
111 static char * obstack_fgets (FILE *, struct obstack
*);
112 static char * tfgets (FILE *);
113 static char * pfgets (FILE *);
114 static void freadsym (FILE *, file
*, int);
115 static void read_repo_file (file
*);
116 static void maybe_tweak (char *, file
*);
117 static int recompile_files (void);
118 static int read_repo_files (char **);
119 static void demangle_new_symbols (void);
120 static int scan_linker_output (const char *);
122 /* Look up an entry in the symbol hash table. */
124 static struct symbol_hash_entry
*
125 symbol_hash_lookup (const char *string
, int create
)
128 e
= htab_find_slot_with_hash (symbol_table
, string
,
129 (*htab_hash_string
) (string
),
130 create
? INSERT
: NO_INSERT
);
135 struct symbol_hash_entry
*v
;
136 *e
= v
= XCNEW (struct symbol_hash_entry
);
137 v
->key
= xstrdup (string
);
139 return (struct symbol_hash_entry
*) *e
;
142 static htab_t file_table
;
144 /* Look up an entry in the file hash table. */
146 static struct file_hash_entry
*
147 file_hash_lookup (const char *string
)
150 e
= htab_find_slot_with_hash (file_table
, string
,
151 (*htab_hash_string
) (string
),
155 struct file_hash_entry
*v
;
156 *e
= v
= XCNEW (struct file_hash_entry
);
157 v
->key
= xstrdup (string
);
159 return (struct file_hash_entry
*) *e
;
162 static htab_t demangled_table
;
164 /* Look up an entry in the demangled name hash table. */
166 static struct demangled_hash_entry
*
167 demangled_hash_lookup (const char *string
, int create
)
170 e
= htab_find_slot_with_hash (demangled_table
, string
,
171 (*htab_hash_string
) (string
),
172 create
? INSERT
: NO_INSERT
);
177 struct demangled_hash_entry
*v
;
178 *e
= v
= XCNEW (struct demangled_hash_entry
);
179 v
->key
= xstrdup (string
);
181 return (struct demangled_hash_entry
*) *e
;
186 struct symbol_stack_entry
189 struct symbol_stack_entry
*next
;
191 struct obstack symbol_stack_obstack
;
192 struct symbol_stack_entry
*symbol_stack
;
194 struct file_stack_entry
197 struct file_stack_entry
*next
;
199 struct obstack file_stack_obstack
;
200 struct file_stack_entry
*file_stack
;
203 symbol_push (symbol
*p
)
205 struct symbol_stack_entry
*ep
206 = XOBNEW (&symbol_stack_obstack
, 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
);
228 struct file_stack_entry
*ep
;
233 ep
= XOBNEW (&file_stack_obstack
, struct file_stack_entry
);
235 ep
->next
= file_stack
;
243 struct file_stack_entry
*ep
= file_stack
;
248 file_stack
= ep
->next
;
249 obstack_free (&file_stack_obstack
, ep
);
254 /* Other machinery. */
256 /* Initialize the tlink machinery. Called from do_tlink. */
263 symbol_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
265 file_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
267 demangled_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
270 obstack_begin (&symbol_stack_obstack
, 0);
271 obstack_begin (&file_stack_obstack
, 0);
273 p
= getenv ("TLINK_VERBOSE");
275 tlink_verbose
= atoi (p
);
285 initial_cwd
= getpwd ();
289 tlink_execute (const char *prog
, char **argv
, const char *outname
,
294 pex
= collect_execute (prog
, argv
, outname
, errname
, PEX_LAST
| PEX_SEARCH
);
295 return collect_wait (prog
, pex
);
299 frob_extension (const char *s
, const char *ext
)
303 p
= strrchr (lbasename (s
), '.');
307 obstack_grow (&temporary_obstack
, s
, p
- s
);
308 return (char *) obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
312 obstack_fgets (FILE *stream
, struct obstack
*ob
)
315 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
316 obstack_1grow (ob
, c
);
317 if (obstack_object_size (ob
) == 0)
319 obstack_1grow (ob
, '\0');
320 return XOBFINISH (ob
, char *);
324 tfgets (FILE *stream
)
326 return obstack_fgets (stream
, &temporary_obstack
);
330 pfgets (FILE *stream
)
332 return xstrdup (tfgets (stream
));
335 /* Real tlink code. */
337 /* Subroutine of read_repo_file. We are reading the repo file for file F,
338 which is coming in on STREAM, and the symbol that comes next in STREAM
339 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
341 XXX "provided" is unimplemented, both here and in the compiler. */
344 freadsym (FILE *stream
, file
*f
, int chosen
)
349 const char *name
= tfgets (stream
);
350 sym
= symbol_hash_lookup (name
, true);
353 if (sym
->file
== NULL
)
355 /* We didn't have this symbol already, so we choose this file. */
359 sym
->chosen
= chosen
;
363 /* We want this file; cast aside any pretender. */
365 if (sym
->chosen
&& sym
->file
!= f
)
367 if (sym
->chosen
== 1)
368 file_push (sym
->file
);
373 chosen
= sym
->chosen
;
377 sym
->chosen
= chosen
;
381 /* Read in the repo file denoted by F, and record all its information. */
384 read_repo_file (file
*f
)
387 FILE *stream
= fopen (f
->key
, "r");
389 if (tlink_verbose
>= 2)
390 fprintf (stderr
, _("collect: reading %s\n"), f
->key
);
392 while (fscanf (stream
, "%c ", &c
) == 1)
397 f
->args
= pfgets (stream
);
400 f
->dir
= pfgets (stream
);
403 f
->main
= pfgets (stream
);
406 freadsym (stream
, f
, 2);
409 freadsym (stream
, f
, 1);
412 freadsym (stream
, f
, 0);
415 obstack_free (&temporary_obstack
, temporary_firstobj
);
419 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
424 /* We might want to modify LINE, which is a symbol line from file F. We do
425 this if either we saw an error message referring to the symbol in
426 question, or we have already allocated the symbol to another file and
427 this one wants to emit it as well. */
430 maybe_tweak (char *line
, file
*f
)
432 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
434 if ((sym
->file
== f
&& sym
->tweaking
)
435 || (sym
->file
!= f
&& line
[0] == 'C'))
453 /* Update the repo files for each of the object files we have adjusted and
457 recompile_files (void)
461 putenv (xstrdup ("COMPILER_PATH="));
462 putenv (xstrdup ("LIBRARY_PATH="));
464 while ((f
= file_pop ()) != NULL
)
469 struct obstack arg_stack
;
470 FILE *stream
= fopen (f
->key
, "r");
471 const char *const outname
= frob_extension (f
->key
, ".rnw");
472 FILE *output
= fopen (outname
, "w");
474 while ((line
= tfgets (stream
)) != NULL
)
480 maybe_tweak (line
, f
);
482 fprintf (output
, "%s\n", line
);
486 /* On Windows "rename" returns -1 and sets ERRNO to EACCESS if
487 the new file name already exists. Therefore, we explicitly
488 remove the old file first. */
489 if (remove (f
->key
) == -1)
490 fatal_error ("removing .rpo file: %m");
491 if (rename (outname
, f
->key
) == -1)
492 fatal_error ("renaming .rpo file: %m");
496 error ("repository file '%s' does not contain command-line "
497 "arguments", f
->key
);
501 /* Build a null-terminated argv array suitable for
502 tlink_execute(). Manipulate arguments on the arg_stack while
503 building argv on the temporary_obstack. */
505 obstack_init (&arg_stack
);
506 obstack_ptr_grow (&temporary_obstack
, c_file_name
);
508 for (p
= f
->args
; *p
!= '\0'; p
= q
+ 1)
510 /* Arguments are delimited by single-quotes. Find the
512 p
= strchr (p
, '\'');
516 /* Find the closing quote. */
517 q
= strchr (p
+ 1, '\'');
521 obstack_grow (&arg_stack
, p
+ 1, q
- (p
+ 1));
523 /* Replace '\'' with '. This is how set_collect_gcc_options
524 encodes a single-quote. */
525 while (q
[1] == '\\' && q
[2] == '\'' && q
[3] == '\'')
529 r
= strchr (q
+ 4, '\'');
533 obstack_grow (&arg_stack
, q
+ 3, r
- (q
+ 3));
537 obstack_1grow (&arg_stack
, '\0');
538 obstack_ptr_grow (&temporary_obstack
, obstack_finish (&arg_stack
));
541 obstack_ptr_grow (&temporary_obstack
, f
->main
);
542 obstack_ptr_grow (&temporary_obstack
, NULL
);
543 argv
= XOBFINISH (&temporary_obstack
, char **);
546 fprintf (stderr
, _("collect: recompiling %s\n"), f
->main
);
548 if (chdir (f
->dir
) != 0
549 || tlink_execute (c_file_name
, argv
, NULL
, NULL
) != 0
550 || chdir (initial_cwd
) != 0)
555 obstack_free (&arg_stack
, NULL
);
556 obstack_free (&temporary_obstack
, temporary_firstobj
);
561 /* The first phase of processing: determine which object files have
562 .rpo files associated with them, and read in the information. */
565 read_repo_files (char **object_lst
)
567 char **object
= object_lst
;
569 for (; *object
; object
++)
574 /* Don't bother trying for ld flags. */
575 if (*object
[0] == '-')
578 p
= frob_extension (*object
, ".rpo");
580 if (! file_exists (p
))
583 f
= file_hash_lookup (p
);
588 if (file_stack
!= NULL
&& ! recompile_files ())
591 return (symbol_stack
!= NULL
);
594 /* Add the demangled forms of any new symbols to the hash table. */
597 demangle_new_symbols (void)
601 while ((sym
= symbol_pop ()) != NULL
)
604 const char *p
= cplus_demangle (sym
->key
, DMGL_PARAMS
| DMGL_ANSI
);
609 dem
= demangled_hash_lookup (p
, true);
610 dem
->mangled
.safe_push (sym
->key
);
614 /* We want to tweak symbol SYM. Return true if all is well, false on
618 start_tweaking (symbol
*sym
)
620 if (sym
&& sym
->tweaked
)
622 error ("'%s' was assigned to '%s', but was not defined "
623 "during recompilation, or vice versa",
624 sym
->key
, sym
->file
->key
);
627 if (sym
&& !sym
->tweaking
)
629 if (tlink_verbose
>= 2)
630 fprintf (stderr
, _("collect: tweaking %s in %s\n"),
631 sym
->key
, sym
->file
->key
);
633 file_push (sym
->file
);
638 /* Step through the output of the linker, in the file named FNAME, and
639 adjust the settings for each symbol encountered. */
642 scan_linker_output (const char *fname
)
644 FILE *stream
= fopen (fname
, "r");
646 int skip_next_in_line
= 0;
648 while ((line
= tfgets (stream
)) != NULL
)
658 /* On darwin9, we might have to skip " in " lines as well. */
659 if (skip_next_in_line
660 && strstr (p
, " in "))
662 skip_next_in_line
= 0;
664 while (*p
&& ISSPACE ((unsigned char) *p
))
670 for (q
= p
; *q
&& ! ISSPACE ((unsigned char) *q
); ++q
)
673 /* Try the first word on the line. */
676 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
677 p
+= strlen (USER_LABEL_PREFIX
);
681 sym
= symbol_hash_lookup (p
, false);
683 /* Some SVR4 linkers produce messages like
684 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
686 if (! sym
&& ! end
&& strstr (q
+ 1, "Undefined symbol: "))
688 char *p
= strrchr (q
+ 1, ' ');
692 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
693 p
+= strlen (USER_LABEL_PREFIX
);
694 sym
= symbol_hash_lookup (p
, false);
698 /* Try a mangled name in quotes. */
703 /* On darwin9, we look for "foo" referenced from:\n\(.* in .*\n\)* */
704 if (strcmp (oldq
, "referenced from:") == 0)
706 /* We have to remember that we found a symbol to tweak. */
709 /* We actually want to start from the first word on the
713 /* Since the format is multiline, we have to skip
714 following lines with " in ". */
715 skip_next_in_line
= 1;
718 /* First try `GNU style'. */
719 p
= strchr (oldq
, '`');
721 p
++, q
= strchr (p
, '\'');
722 /* Then try "double quotes". */
723 else if (p
= strchr (oldq
, '"'), p
)
724 p
++, q
= strchr (p
, '"');
725 /* Then try 'single quotes'. */
726 else if (p
= strchr (oldq
, '\''), p
)
727 p
++, q
= strchr (p
, '\'');
729 /* Then try entire line. */
730 q
= strchr (oldq
, 0);
737 /* Don't let the strstr's below see the demangled name; we
738 might get spurious matches. */
741 /* powerpc64-linux references .foo when calling function foo. */
746 /* We need to check for certain error keywords here, or we would
747 mistakenly use GNU ld's "In function `foo':" message. */
749 || strstr (oldq
, "ndefined")
750 || strstr (oldq
, "nresolved")
751 || strstr (oldq
, "nsatisfied")
752 || strstr (oldq
, "ultiple")))
755 dem
= demangled_hash_lookup (p
, false);
758 if (!strncmp (p
, USER_LABEL_PREFIX
,
759 strlen (USER_LABEL_PREFIX
)))
760 p
+= strlen (USER_LABEL_PREFIX
);
761 sym
= symbol_hash_lookup (p
, false);
768 /* We found a demangled name. If this is the name of a
769 constructor or destructor, there can be several mangled names
770 that match it, so choose or unchoose all of them. If some are
771 chosen and some not, leave the later ones that don't match
772 alone for now; either this will cause the link to suceed, or
773 on the next attempt we will switch all of them the other way
774 and that will cause it to succeed. */
776 int len
= dem
->mangled
.length ();
778 FOR_EACH_VEC_ELT (dem
->mangled
, ix
, s
)
780 sym
= symbol_hash_lookup (s
, false);
782 chosen
= sym
->chosen
;
783 else if (sym
->chosen
!= chosen
)
786 /* Avoid an error about re-tweaking when we guess wrong in
787 the case of mismatch. */
789 sym
->tweaked
= false;
790 ok
= start_tweaking (sym
);
794 ok
= start_tweaking (sym
);
796 obstack_free (&temporary_obstack
, temporary_firstobj
);
806 return (file_stack
!= NULL
);
809 /* Entry point for tlink. Called from main in collect2.c.
811 Iteratively try to provide definitions for all the unresolved symbols
812 mentioned in the linker error messages.
814 LD_ARGV is an array of arguments for the linker.
815 OBJECT_LST is an array of object files that we may be able to recompile
816 to provide missing definitions. Currently ignored. */
819 do_tlink (char **ld_argv
, char **object_lst ATTRIBUTE_UNUSED
)
821 int exit
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
);
829 /* Until collect does a better job of figuring out which are object
830 files, assume that everything on the command line could be. */
831 if (read_repo_files (ld_argv
))
832 while (exit
&& i
++ < MAX_ITERATIONS
)
834 if (tlink_verbose
>= 3)
836 dump_ld_file (ldout
, stdout
);
837 dump_ld_file (lderrout
, stderr
);
839 demangle_new_symbols ();
840 if (! scan_linker_output (ldout
)
841 && ! scan_linker_output (lderrout
))
843 if (! recompile_files ())
846 fprintf (stderr
, _("collect: relinking\n"));
847 exit
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
);
851 dump_ld_file (ldout
, stdout
);
853 dump_ld_file (lderrout
, stderr
);
857 error ("ld returned %d exit status", exit
);
862 /* We have just successfully produced an output file, so assume that we
863 may unlink it if need be for now on. */
864 may_unlink_output_file
= true;