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 DEF_VEC_ALLOC_P(str
,heap
);
75 typedef struct demangled_hash_entry
78 VEC(str
,heap
) *mangled
;
81 /* Hash and comparison functions for these hash tables. */
83 static int hash_string_eq (const void *, const void *);
84 static hashval_t
hash_string_hash (const void *);
87 hash_string_eq (const void *s1_p
, const void *s2_p
)
89 const char *const *s1
= (const char *const *) s1_p
;
90 const char *s2
= (const char *) s2_p
;
91 return strcmp (*s1
, s2
) == 0;
95 hash_string_hash (const void *s_p
)
97 const char *const *s
= (const char *const *) s_p
;
98 return (*htab_hash_string
) (*s
);
101 static htab_t symbol_table
;
103 static struct symbol_hash_entry
* symbol_hash_lookup (const char *, int);
104 static struct file_hash_entry
* file_hash_lookup (const char *);
105 static struct demangled_hash_entry
*demangled_hash_lookup (const char *, int);
106 static void symbol_push (symbol
*);
107 static symbol
* symbol_pop (void);
108 static void file_push (file
*);
109 static file
* file_pop (void);
110 static void tlink_init (void);
111 static int tlink_execute (const char *, char **, const char *, const char *);
112 static char * frob_extension (const char *, const char *);
113 static char * obstack_fgets (FILE *, struct obstack
*);
114 static char * tfgets (FILE *);
115 static char * pfgets (FILE *);
116 static void freadsym (FILE *, file
*, int);
117 static void read_repo_file (file
*);
118 static void maybe_tweak (char *, file
*);
119 static int recompile_files (void);
120 static int read_repo_files (char **);
121 static void demangle_new_symbols (void);
122 static int scan_linker_output (const char *);
124 /* Look up an entry in the symbol hash table. */
126 static struct symbol_hash_entry
*
127 symbol_hash_lookup (const char *string
, int create
)
130 e
= htab_find_slot_with_hash (symbol_table
, string
,
131 (*htab_hash_string
) (string
),
132 create
? INSERT
: NO_INSERT
);
137 struct symbol_hash_entry
*v
;
138 *e
= v
= XCNEW (struct symbol_hash_entry
);
139 v
->key
= xstrdup (string
);
141 return (struct symbol_hash_entry
*) *e
;
144 static htab_t file_table
;
146 /* Look up an entry in the file hash table. */
148 static struct file_hash_entry
*
149 file_hash_lookup (const char *string
)
152 e
= htab_find_slot_with_hash (file_table
, string
,
153 (*htab_hash_string
) (string
),
157 struct file_hash_entry
*v
;
158 *e
= v
= XCNEW (struct file_hash_entry
);
159 v
->key
= xstrdup (string
);
161 return (struct file_hash_entry
*) *e
;
164 static htab_t demangled_table
;
166 /* Look up an entry in the demangled name hash table. */
168 static struct demangled_hash_entry
*
169 demangled_hash_lookup (const char *string
, int create
)
172 e
= htab_find_slot_with_hash (demangled_table
, string
,
173 (*htab_hash_string
) (string
),
174 create
? INSERT
: NO_INSERT
);
179 struct demangled_hash_entry
*v
;
180 *e
= v
= XCNEW (struct demangled_hash_entry
);
181 v
->key
= xstrdup (string
);
183 return (struct demangled_hash_entry
*) *e
;
188 struct symbol_stack_entry
191 struct symbol_stack_entry
*next
;
193 struct obstack symbol_stack_obstack
;
194 struct symbol_stack_entry
*symbol_stack
;
196 struct file_stack_entry
199 struct file_stack_entry
*next
;
201 struct obstack file_stack_obstack
;
202 struct file_stack_entry
*file_stack
;
205 symbol_push (symbol
*p
)
207 struct symbol_stack_entry
*ep
208 = XOBNEW (&symbol_stack_obstack
, struct symbol_stack_entry
);
210 ep
->next
= symbol_stack
;
217 struct symbol_stack_entry
*ep
= symbol_stack
;
222 symbol_stack
= ep
->next
;
223 obstack_free (&symbol_stack_obstack
, ep
);
230 struct file_stack_entry
*ep
;
235 ep
= XOBNEW (&file_stack_obstack
, 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
);
287 initial_cwd
= getpwd ();
291 tlink_execute (const char *prog
, char **argv
, const char *outname
,
296 pex
= collect_execute (prog
, argv
, outname
, errname
, PEX_LAST
| PEX_SEARCH
);
297 return collect_wait (prog
, pex
);
301 frob_extension (const char *s
, const char *ext
)
305 p
= strrchr (lbasename (s
), '.');
309 obstack_grow (&temporary_obstack
, s
, p
- s
);
310 return (char *) obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
314 obstack_fgets (FILE *stream
, struct obstack
*ob
)
317 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
318 obstack_1grow (ob
, c
);
319 if (obstack_object_size (ob
) == 0)
321 obstack_1grow (ob
, '\0');
322 return XOBFINISH (ob
, char *);
326 tfgets (FILE *stream
)
328 return obstack_fgets (stream
, &temporary_obstack
);
332 pfgets (FILE *stream
)
334 return xstrdup (tfgets (stream
));
337 /* Real tlink code. */
339 /* Subroutine of read_repo_file. We are reading the repo file for file F,
340 which is coming in on STREAM, and the symbol that comes next in STREAM
341 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
343 XXX "provided" is unimplemented, both here and in the compiler. */
346 freadsym (FILE *stream
, file
*f
, int chosen
)
351 const char *name
= tfgets (stream
);
352 sym
= symbol_hash_lookup (name
, true);
355 if (sym
->file
== NULL
)
357 /* We didn't have this symbol already, so we choose this file. */
361 sym
->chosen
= chosen
;
365 /* We want this file; cast aside any pretender. */
367 if (sym
->chosen
&& sym
->file
!= f
)
369 if (sym
->chosen
== 1)
370 file_push (sym
->file
);
375 chosen
= sym
->chosen
;
379 sym
->chosen
= chosen
;
383 /* Read in the repo file denoted by F, and record all its information. */
386 read_repo_file (file
*f
)
389 FILE *stream
= fopen (f
->key
, "r");
391 if (tlink_verbose
>= 2)
392 fprintf (stderr
, _("collect: reading %s\n"), f
->key
);
394 while (fscanf (stream
, "%c ", &c
) == 1)
399 f
->args
= pfgets (stream
);
402 f
->dir
= pfgets (stream
);
405 f
->main
= pfgets (stream
);
408 freadsym (stream
, f
, 2);
411 freadsym (stream
, f
, 1);
414 freadsym (stream
, f
, 0);
417 obstack_free (&temporary_obstack
, temporary_firstobj
);
421 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
426 /* We might want to modify LINE, which is a symbol line from file F. We do
427 this if either we saw an error message referring to the symbol in
428 question, or we have already allocated the symbol to another file and
429 this one wants to emit it as well. */
432 maybe_tweak (char *line
, file
*f
)
434 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
436 if ((sym
->file
== f
&& sym
->tweaking
)
437 || (sym
->file
!= f
&& line
[0] == 'C'))
455 /* Update the repo files for each of the object files we have adjusted and
459 recompile_files (void)
463 putenv (xstrdup ("COMPILER_PATH="));
464 putenv (xstrdup ("LIBRARY_PATH="));
466 while ((f
= file_pop ()) != NULL
)
471 struct obstack arg_stack
;
472 FILE *stream
= fopen (f
->key
, "r");
473 const char *const outname
= frob_extension (f
->key
, ".rnw");
474 FILE *output
= fopen (outname
, "w");
476 while ((line
= tfgets (stream
)) != NULL
)
482 maybe_tweak (line
, f
);
484 fprintf (output
, "%s\n", line
);
488 /* On Windows "rename" returns -1 and sets ERRNO to EACCESS if
489 the new file name already exists. Therefore, we explicitly
490 remove the old file first. */
491 if (remove (f
->key
) == -1)
492 fatal_error ("removing .rpo file: %m");
493 if (rename (outname
, f
->key
) == -1)
494 fatal_error ("renaming .rpo file: %m");
498 error ("repository file '%s' does not contain command-line "
499 "arguments", f
->key
);
503 /* Build a null-terminated argv array suitable for
504 tlink_execute(). Manipulate arguments on the arg_stack while
505 building argv on the temporary_obstack. */
507 obstack_init (&arg_stack
);
508 obstack_ptr_grow (&temporary_obstack
, c_file_name
);
510 for (p
= f
->args
; *p
!= '\0'; p
= q
+ 1)
512 /* Arguments are delimited by single-quotes. Find the
514 p
= strchr (p
, '\'');
518 /* Find the closing quote. */
519 q
= strchr (p
+ 1, '\'');
523 obstack_grow (&arg_stack
, p
+ 1, q
- (p
+ 1));
525 /* Replace '\'' with '. This is how set_collect_gcc_options
526 encodes a single-quote. */
527 while (q
[1] == '\\' && q
[2] == '\'' && q
[3] == '\'')
531 r
= strchr (q
+ 4, '\'');
535 obstack_grow (&arg_stack
, q
+ 3, r
- (q
+ 3));
539 obstack_1grow (&arg_stack
, '\0');
540 obstack_ptr_grow (&temporary_obstack
, obstack_finish (&arg_stack
));
543 obstack_ptr_grow (&temporary_obstack
, f
->main
);
544 obstack_ptr_grow (&temporary_obstack
, NULL
);
545 argv
= XOBFINISH (&temporary_obstack
, char **);
548 fprintf (stderr
, _("collect: recompiling %s\n"), f
->main
);
550 if (chdir (f
->dir
) != 0
551 || tlink_execute (c_file_name
, argv
, NULL
, NULL
) != 0
552 || chdir (initial_cwd
) != 0)
557 obstack_free (&arg_stack
, NULL
);
558 obstack_free (&temporary_obstack
, temporary_firstobj
);
563 /* The first phase of processing: determine which object files have
564 .rpo files associated with them, and read in the information. */
567 read_repo_files (char **object_lst
)
569 char **object
= object_lst
;
571 for (; *object
; object
++)
576 /* Don't bother trying for ld flags. */
577 if (*object
[0] == '-')
580 p
= frob_extension (*object
, ".rpo");
582 if (! file_exists (p
))
585 f
= file_hash_lookup (p
);
590 if (file_stack
!= NULL
&& ! recompile_files ())
593 return (symbol_stack
!= NULL
);
596 /* Add the demangled forms of any new symbols to the hash table. */
599 demangle_new_symbols (void)
603 while ((sym
= symbol_pop ()) != NULL
)
606 const char *p
= cplus_demangle (sym
->key
, DMGL_PARAMS
| DMGL_ANSI
);
611 dem
= demangled_hash_lookup (p
, true);
612 VEC_safe_push (str
, heap
, dem
->mangled
, sym
->key
);
616 /* We want to tweak symbol SYM. Return true if all is well, false on
620 start_tweaking (symbol
*sym
)
622 if (sym
&& sym
->tweaked
)
624 error ("'%s' was assigned to '%s', but was not defined "
625 "during recompilation, or vice versa",
626 sym
->key
, sym
->file
->key
);
629 if (sym
&& !sym
->tweaking
)
631 if (tlink_verbose
>= 2)
632 fprintf (stderr
, _("collect: tweaking %s in %s\n"),
633 sym
->key
, sym
->file
->key
);
635 file_push (sym
->file
);
640 /* Step through the output of the linker, in the file named FNAME, and
641 adjust the settings for each symbol encountered. */
644 scan_linker_output (const char *fname
)
646 FILE *stream
= fopen (fname
, "r");
648 int skip_next_in_line
= 0;
650 while ((line
= tfgets (stream
)) != NULL
)
660 /* On darwin9, we might have to skip " in " lines as well. */
661 if (skip_next_in_line
662 && strstr (p
, " in "))
664 skip_next_in_line
= 0;
666 while (*p
&& ISSPACE ((unsigned char) *p
))
672 for (q
= p
; *q
&& ! ISSPACE ((unsigned char) *q
); ++q
)
675 /* Try the first word on the line. */
678 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
679 p
+= strlen (USER_LABEL_PREFIX
);
683 sym
= symbol_hash_lookup (p
, false);
685 /* Some SVR4 linkers produce messages like
686 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
688 if (! sym
&& ! end
&& strstr (q
+ 1, "Undefined symbol: "))
690 char *p
= strrchr (q
+ 1, ' ');
694 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
695 p
+= strlen (USER_LABEL_PREFIX
);
696 sym
= symbol_hash_lookup (p
, false);
700 /* Try a mangled name in quotes. */
705 /* On darwin9, we look for "foo" referenced from:\n\(.* in .*\n\)* */
706 if (strcmp (oldq
, "referenced from:") == 0)
708 /* We have to remember that we found a symbol to tweak. */
711 /* We actually want to start from the first word on the
715 /* Since the format is multiline, we have to skip
716 following lines with " in ". */
717 skip_next_in_line
= 1;
720 /* First try `GNU style'. */
721 p
= strchr (oldq
, '`');
723 p
++, q
= strchr (p
, '\'');
724 /* Then try "double quotes". */
725 else if (p
= strchr (oldq
, '"'), p
)
726 p
++, q
= strchr (p
, '"');
727 /* Then try 'single quotes'. */
728 else if (p
= strchr (oldq
, '\''), p
)
729 p
++, q
= strchr (p
, '\'');
731 /* Then try entire line. */
732 q
= strchr (oldq
, 0);
739 /* Don't let the strstr's below see the demangled name; we
740 might get spurious matches. */
743 /* powerpc64-linux references .foo when calling function foo. */
748 /* We need to check for certain error keywords here, or we would
749 mistakenly use GNU ld's "In function `foo':" message. */
751 || strstr (oldq
, "ndefined")
752 || strstr (oldq
, "nresolved")
753 || strstr (oldq
, "nsatisfied")
754 || strstr (oldq
, "ultiple")))
757 dem
= demangled_hash_lookup (p
, false);
760 if (!strncmp (p
, USER_LABEL_PREFIX
,
761 strlen (USER_LABEL_PREFIX
)))
762 p
+= strlen (USER_LABEL_PREFIX
);
763 sym
= symbol_hash_lookup (p
, false);
770 /* We found a demangled name. If this is the name of a
771 constructor or destructor, there can be several mangled names
772 that match it, so choose or unchoose all of them. If some are
773 chosen and some not, leave the later ones that don't match
774 alone for now; either this will cause the link to suceed, or
775 on the next attempt we will switch all of them the other way
776 and that will cause it to succeed. */
778 int len
= VEC_length (str
, dem
->mangled
);
780 FOR_EACH_VEC_ELT (str
, dem
->mangled
, ix
, s
)
782 sym
= symbol_hash_lookup (s
, false);
784 chosen
= sym
->chosen
;
785 else if (sym
->chosen
!= chosen
)
788 /* Avoid an error about re-tweaking when we guess wrong in
789 the case of mismatch. */
791 sym
->tweaked
= false;
792 ok
= start_tweaking (sym
);
796 ok
= start_tweaking (sym
);
798 obstack_free (&temporary_obstack
, temporary_firstobj
);
808 return (file_stack
!= NULL
);
811 /* Entry point for tlink. Called from main in collect2.c.
813 Iteratively try to provide definitions for all the unresolved symbols
814 mentioned in the linker error messages.
816 LD_ARGV is an array of arguments for the linker.
817 OBJECT_LST is an array of object files that we may be able to recompile
818 to provide missing definitions. Currently ignored. */
821 do_tlink (char **ld_argv
, char **object_lst ATTRIBUTE_UNUSED
)
823 int exit
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
);
831 /* Until collect does a better job of figuring out which are object
832 files, assume that everything on the command line could be. */
833 if (read_repo_files (ld_argv
))
834 while (exit
&& i
++ < MAX_ITERATIONS
)
836 if (tlink_verbose
>= 3)
838 dump_ld_file (ldout
, stdout
);
839 dump_ld_file (lderrout
, stderr
);
841 demangle_new_symbols ();
842 if (! scan_linker_output (ldout
)
843 && ! scan_linker_output (lderrout
))
845 if (! recompile_files ())
848 fprintf (stderr
, _("collect: relinking\n"));
849 exit
= tlink_execute ("ld", ld_argv
, ldout
, lderrout
);
853 dump_ld_file (ldout
, stdout
);
855 dump_ld_file (lderrout
, stderr
);
859 error ("ld returned %d exit status", exit
);
864 /* We have just successfully produced an output file, so assume that we
865 may unlink it if need be for now on. */
866 may_unlink_output_file
= true;