1 /* Scan linker error messages for missing template instantiations and provide
4 Copyright (C) 1995-2015 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 3, 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 COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
31 #include "collect-utils.h"
32 #include "filenames.h"
33 #include "diagnostic-core.h"
35 /* TARGET_64BIT may be defined to use driver specific functionality. */
37 #define TARGET_64BIT TARGET_64BIT_DEFAULT
39 #define MAX_ITERATIONS 17
41 /* Defined in the automatically-generated underscore.c. */
42 extern int prepends_underscore
;
44 static int tlink_verbose
;
46 static char *initial_cwd
;
48 /* Hash table boilerplate for working with htab_t. We have hash tables
49 for symbol names, file names, and demangled symbols. */
51 typedef struct symbol_hash_entry
54 struct file_hash_entry
*file
;
60 typedef struct file_hash_entry
69 typedef const char *str
;
71 typedef struct demangled_hash_entry
77 /* Hash and comparison functions for these hash tables. */
79 static int hash_string_eq (const void *, const void *);
80 static hashval_t
hash_string_hash (const void *);
83 hash_string_eq (const void *s1_p
, const void *s2_p
)
85 const char *const *s1
= (const char *const *) s1_p
;
86 const char *s2
= (const char *) s2_p
;
87 return strcmp (*s1
, s2
) == 0;
91 hash_string_hash (const void *s_p
)
93 const char *const *s
= (const char *const *) s_p
;
94 return (*htab_hash_string
) (*s
);
97 static htab_t symbol_table
;
99 static struct symbol_hash_entry
* symbol_hash_lookup (const char *, int);
100 static struct file_hash_entry
* file_hash_lookup (const char *);
101 static struct demangled_hash_entry
*demangled_hash_lookup (const char *, int);
102 static void symbol_push (symbol
*);
103 static symbol
* symbol_pop (void);
104 static void file_push (file
*);
105 static file
* file_pop (void);
106 static char * frob_extension (const char *, const char *);
107 static char * obstack_fgets (FILE *, struct obstack
*);
108 static char * tfgets (FILE *);
109 static char * pfgets (FILE *);
110 static void freadsym (FILE *, file
*, int);
111 static void read_repo_file (file
*);
112 static void maybe_tweak (char *, file
*);
113 static int recompile_files (void);
114 static int read_repo_files (char **);
115 static void demangle_new_symbols (void);
116 static int scan_linker_output (const char *);
118 /* Look up an entry in the symbol hash table. */
120 static struct symbol_hash_entry
*
121 symbol_hash_lookup (const char *string
, int 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
= XCNEW (struct symbol_hash_entry
);
133 v
->key
= xstrdup (string
);
135 return (struct symbol_hash_entry
*) *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 (const char *string
)
146 e
= htab_find_slot_with_hash (file_table
, string
,
147 (*htab_hash_string
) (string
),
151 struct file_hash_entry
*v
;
152 *e
= v
= XCNEW (struct file_hash_entry
);
153 v
->key
= xstrdup (string
);
155 return (struct file_hash_entry
*) *e
;
158 static htab_t demangled_table
;
160 /* Look up an entry in the demangled name hash table. */
162 static struct demangled_hash_entry
*
163 demangled_hash_lookup (const char *string
, int create
)
166 e
= htab_find_slot_with_hash (demangled_table
, string
,
167 (*htab_hash_string
) (string
),
168 create
? INSERT
: NO_INSERT
);
173 struct demangled_hash_entry
*v
;
174 *e
= v
= XCNEW (struct demangled_hash_entry
);
175 v
->key
= xstrdup (string
);
177 return (struct demangled_hash_entry
*) *e
;
182 struct symbol_stack_entry
185 struct symbol_stack_entry
*next
;
187 struct obstack symbol_stack_obstack
;
188 struct symbol_stack_entry
*symbol_stack
;
190 struct file_stack_entry
193 struct file_stack_entry
*next
;
195 struct obstack file_stack_obstack
;
196 struct file_stack_entry
*file_stack
;
199 symbol_push (symbol
*p
)
201 struct symbol_stack_entry
*ep
202 = XOBNEW (&symbol_stack_obstack
, struct symbol_stack_entry
);
204 ep
->next
= symbol_stack
;
211 struct symbol_stack_entry
*ep
= symbol_stack
;
216 symbol_stack
= ep
->next
;
217 obstack_free (&symbol_stack_obstack
, ep
);
224 struct file_stack_entry
*ep
;
229 ep
= XOBNEW (&file_stack_obstack
, struct file_stack_entry
);
231 ep
->next
= file_stack
;
239 struct file_stack_entry
*ep
= file_stack
;
244 file_stack
= ep
->next
;
245 obstack_free (&file_stack_obstack
, ep
);
250 /* Other machinery. */
252 /* Initialize the tlink machinery. Called from do_tlink. */
259 symbol_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
261 file_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
263 demangled_table
= htab_create (500, hash_string_hash
, hash_string_eq
,
266 obstack_begin (&symbol_stack_obstack
, 0);
267 obstack_begin (&file_stack_obstack
, 0);
269 p
= getenv ("TLINK_VERBOSE");
271 tlink_verbose
= atoi (p
);
281 initial_cwd
= getpwd ();
285 tlink_execute (const char *prog
, char **argv
, const char *outname
,
286 const char *errname
, bool use_atfile
)
290 pex
= collect_execute (prog
, argv
, outname
, errname
,
291 PEX_LAST
| PEX_SEARCH
, use_atfile
);
292 return collect_wait (prog
, pex
);
296 frob_extension (const char *s
, const char *ext
)
300 p
= strrchr (lbasename (s
), '.');
304 obstack_grow (&temporary_obstack
, s
, p
- s
);
305 return (char *) obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
309 obstack_fgets (FILE *stream
, struct obstack
*ob
)
312 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
313 obstack_1grow (ob
, c
);
314 if (obstack_object_size (ob
) == 0)
316 obstack_1grow (ob
, '\0');
317 return XOBFINISH (ob
, char *);
321 tfgets (FILE *stream
)
323 return obstack_fgets (stream
, &temporary_obstack
);
327 pfgets (FILE *stream
)
329 return xstrdup (tfgets (stream
));
332 /* Real tlink code. */
334 /* Subroutine of read_repo_file. We are reading the repo file for file F,
335 which is coming in on STREAM, and the symbol that comes next in STREAM
336 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
338 XXX "provided" is unimplemented, both here and in the compiler. */
341 freadsym (FILE *stream
, file
*f
, int chosen
)
346 const char *name
= tfgets (stream
);
347 sym
= symbol_hash_lookup (name
, true);
350 if (sym
->file
== NULL
)
352 /* We didn't have this symbol already, so we choose this file. */
356 sym
->chosen
= chosen
;
360 /* We want this file; cast aside any pretender. */
362 if (sym
->chosen
&& sym
->file
!= f
)
364 if (sym
->chosen
== 1)
365 file_push (sym
->file
);
370 chosen
= sym
->chosen
;
374 sym
->chosen
= chosen
;
378 /* Read in the repo file denoted by F, and record all its information. */
381 read_repo_file (file
*f
)
384 FILE *stream
= fopen (f
->key
, "r");
386 if (tlink_verbose
>= 2)
387 fprintf (stderr
, _("collect: reading %s\n"), f
->key
);
389 while (fscanf (stream
, "%c ", &c
) == 1)
394 f
->args
= pfgets (stream
);
397 f
->dir
= pfgets (stream
);
400 f
->main
= pfgets (stream
);
403 freadsym (stream
, f
, 2);
406 freadsym (stream
, f
, 1);
409 freadsym (stream
, f
, 0);
412 obstack_free (&temporary_obstack
, temporary_firstobj
);
416 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
421 /* We might want to modify LINE, which is a symbol line from file F. We do
422 this if either we saw an error message referring to the symbol in
423 question, or we have already allocated the symbol to another file and
424 this one wants to emit it as well. */
427 maybe_tweak (char *line
, file
*f
)
429 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
431 if ((sym
->file
== f
&& sym
->tweaking
)
432 || (sym
->file
!= f
&& line
[0] == 'C'))
450 /* Update the repo files for each of the object files we have adjusted and
454 recompile_files (void)
458 putenv (xstrdup ("COMPILER_PATH="));
459 putenv (xstrdup ("LIBRARY_PATH="));
461 while ((f
= file_pop ()) != NULL
)
466 struct obstack arg_stack
;
467 FILE *stream
= fopen (f
->key
, "r");
468 const char *const outname
= frob_extension (f
->key
, ".rnw");
469 FILE *output
= fopen (outname
, "w");
471 while ((line
= tfgets (stream
)) != NULL
)
477 maybe_tweak (line
, f
);
479 fprintf (output
, "%s\n", line
);
483 /* On Windows "rename" returns -1 and sets ERRNO to EACCESS if
484 the new file name already exists. Therefore, we explicitly
485 remove the old file first. */
486 if (remove (f
->key
) == -1)
487 fatal_error (input_location
, "removing .rpo file: %m");
488 if (rename (outname
, f
->key
) == -1)
489 fatal_error (input_location
, "renaming .rpo file: %m");
493 error ("repository file '%s' does not contain command-line "
494 "arguments", f
->key
);
498 /* Build a null-terminated argv array suitable for
499 tlink_execute(). Manipulate arguments on the arg_stack while
500 building argv on the temporary_obstack. */
502 obstack_init (&arg_stack
);
503 obstack_ptr_grow (&temporary_obstack
, c_file_name
);
505 for (p
= f
->args
; *p
!= '\0'; p
= q
+ 1)
507 /* Arguments are delimited by single-quotes. Find the
509 p
= strchr (p
, '\'');
513 /* Find the closing quote. */
514 q
= strchr (p
+ 1, '\'');
518 obstack_grow (&arg_stack
, p
+ 1, q
- (p
+ 1));
520 /* Replace '\'' with '. This is how set_collect_gcc_options
521 encodes a single-quote. */
522 while (q
[1] == '\\' && q
[2] == '\'' && q
[3] == '\'')
526 r
= strchr (q
+ 4, '\'');
530 obstack_grow (&arg_stack
, q
+ 3, r
- (q
+ 3));
534 obstack_1grow (&arg_stack
, '\0');
535 obstack_ptr_grow (&temporary_obstack
, obstack_finish (&arg_stack
));
538 obstack_ptr_grow (&temporary_obstack
, f
->main
);
539 obstack_ptr_grow (&temporary_obstack
, NULL
);
540 argv
= XOBFINISH (&temporary_obstack
, char **);
543 fprintf (stderr
, _("collect: recompiling %s\n"), f
->main
);
545 if (chdir (f
->dir
) != 0
546 || tlink_execute (c_file_name
, argv
, NULL
, NULL
, false) != 0
547 || chdir (initial_cwd
) != 0)
552 obstack_free (&arg_stack
, NULL
);
553 obstack_free (&temporary_obstack
, temporary_firstobj
);
558 /* The first phase of processing: determine which object files have
559 .rpo files associated with them, and read in the information. */
562 read_repo_files (char **object_lst
)
564 char **object
= object_lst
;
566 for (; *object
; object
++)
571 /* Don't bother trying for ld flags. */
572 if (*object
[0] == '-')
575 p
= frob_extension (*object
, ".rpo");
577 if (! file_exists (p
))
580 f
= file_hash_lookup (p
);
585 if (file_stack
!= NULL
&& ! recompile_files ())
588 return (symbol_stack
!= NULL
);
591 /* Add the demangled forms of any new symbols to the hash table. */
594 demangle_new_symbols (void)
598 while ((sym
= symbol_pop ()) != NULL
)
601 const char *p
= cplus_demangle (sym
->key
, DMGL_PARAMS
| DMGL_ANSI
);
606 dem
= demangled_hash_lookup (p
, true);
607 dem
->mangled
.safe_push (sym
->key
);
611 /* We want to tweak symbol SYM. Return true if all is well, false on
615 start_tweaking (symbol
*sym
)
617 if (sym
&& sym
->tweaked
)
619 error ("'%s' was assigned to '%s', but was not defined "
620 "during recompilation, or vice versa",
621 sym
->key
, sym
->file
->key
);
624 if (sym
&& !sym
->tweaking
)
626 if (tlink_verbose
>= 2)
627 fprintf (stderr
, _("collect: tweaking %s in %s\n"),
628 sym
->key
, sym
->file
->key
);
630 file_push (sym
->file
);
635 /* Step through the output of the linker, in the file named FNAME, and
636 adjust the settings for each symbol encountered. */
639 scan_linker_output (const char *fname
)
641 FILE *stream
= fopen (fname
, "r");
643 int skip_next_in_line
= 0;
645 while ((line
= tfgets (stream
)) != NULL
)
655 /* On darwin9, we might have to skip " in " lines as well. */
656 if (skip_next_in_line
657 && strstr (p
, " in "))
659 skip_next_in_line
= 0;
661 while (*p
&& ISSPACE ((unsigned char) *p
))
667 for (q
= p
; *q
&& ! ISSPACE ((unsigned char) *q
); ++q
)
670 /* Try the first word on the line. */
673 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
674 p
+= strlen (USER_LABEL_PREFIX
);
678 sym
= symbol_hash_lookup (p
, false);
680 /* Some SVR4 linkers produce messages like
681 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
683 if (! sym
&& ! end
&& strstr (q
+ 1, "Undefined symbol: "))
685 char *p
= strrchr (q
+ 1, ' ');
689 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
690 p
+= strlen (USER_LABEL_PREFIX
);
691 sym
= symbol_hash_lookup (p
, false);
695 /* Try a mangled name in quotes. */
700 /* On darwin9, we look for "foo" referenced from:\n\(.* in .*\n\)* */
701 if (strcmp (oldq
, "referenced from:") == 0)
703 /* We have to remember that we found a symbol to tweak. */
706 /* We actually want to start from the first word on the
710 /* Since the format is multiline, we have to skip
711 following lines with " in ". */
712 skip_next_in_line
= 1;
715 /* First try `GNU style'. */
716 p
= strchr (oldq
, '`');
718 p
++, q
= strchr (p
, '\'');
719 /* Then try "double quotes". */
720 else if (p
= strchr (oldq
, '"'), p
)
721 p
++, q
= strchr (p
, '"');
722 /* Then try 'single quotes'. */
723 else if (p
= strchr (oldq
, '\''), p
)
724 p
++, q
= strchr (p
, '\'');
726 /* Then try entire line. */
727 q
= strchr (oldq
, 0);
734 /* Don't let the strstr's below see the demangled name; we
735 might get spurious matches. */
738 /* powerpc64-linux references .foo when calling function foo. */
743 /* We need to check for certain error keywords here, or we would
744 mistakenly use GNU ld's "In function `foo':" message. */
746 || strstr (oldq
, "ndefined")
747 || strstr (oldq
, "nresolved")
748 || strstr (oldq
, "nsatisfied")
749 || strstr (oldq
, "ultiple")))
752 dem
= demangled_hash_lookup (p
, false);
755 if (!strncmp (p
, USER_LABEL_PREFIX
,
756 strlen (USER_LABEL_PREFIX
)))
757 p
+= strlen (USER_LABEL_PREFIX
);
758 sym
= symbol_hash_lookup (p
, false);
765 /* We found a demangled name. If this is the name of a
766 constructor or destructor, there can be several mangled names
767 that match it, so choose or unchoose all of them. If some are
768 chosen and some not, leave the later ones that don't match
769 alone for now; either this will cause the link to succeed, or
770 on the next attempt we will switch all of them the other way
771 and that will cause it to succeed. */
773 int len
= dem
->mangled
.length ();
775 FOR_EACH_VEC_ELT (dem
->mangled
, ix
, s
)
777 sym
= symbol_hash_lookup (s
, false);
779 chosen
= sym
->chosen
;
780 else if (sym
->chosen
!= chosen
)
783 /* Avoid an error about re-tweaking when we guess wrong in
784 the case of mismatch. */
786 sym
->tweaked
= false;
787 ok
= start_tweaking (sym
);
791 ok
= start_tweaking (sym
);
793 obstack_free (&temporary_obstack
, temporary_firstobj
);
803 return (file_stack
!= NULL
);
806 /* Entry point for tlink. Called from main in collect2.c.
808 Iteratively try to provide definitions for all the unresolved symbols
809 mentioned in the linker error messages.
811 LD_ARGV is an array of arguments for the linker.
812 OBJECT_LST is an array of object files that we may be able to recompile
813 to provide missing definitions. Currently ignored. */
816 do_tlink (char **ld_argv
, char **object_lst ATTRIBUTE_UNUSED
)
818 int ret
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
,
819 HAVE_GNU_LD
&& at_file_supplied
);
827 /* Until collect does a better job of figuring out which are object
828 files, assume that everything on the command line could be. */
829 if (read_repo_files (ld_argv
))
830 while (ret
&& i
++ < MAX_ITERATIONS
)
832 if (tlink_verbose
>= 3)
834 dump_ld_file (ldout
, stdout
);
835 dump_ld_file (lderrout
, stderr
);
837 demangle_new_symbols ();
838 if (! scan_linker_output (ldout
)
839 && ! scan_linker_output (lderrout
))
841 if (! recompile_files ())
844 fprintf (stderr
, _("collect: relinking\n"));
845 ret
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
,
846 HAVE_GNU_LD
&& at_file_supplied
);
850 dump_ld_file (ldout
, stdout
);
852 dump_ld_file (lderrout
, stderr
);
856 error ("ld returned %d exit status", ret
);
861 /* We have just successfully produced an output file, so assume that we
862 may unlink it if need be for now on. */
863 may_unlink_output_file
= true;