1 /* Scan linker error messages for missing template instantiations and provide
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2003
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 2, 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 COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include "coretypes.h"
35 #define MAX_ITERATIONS 17
37 /* Defined in the automatically-generated underscore.c. */
38 extern int prepends_underscore
;
40 static int tlink_verbose
;
42 /* Hash table boilerplate for working with htab_t. We have hash tables
43 for symbol names, file names, and demangled symbols. */
45 typedef struct symbol_hash_entry
48 struct file_hash_entry
*file
;
54 typedef struct file_hash_entry
63 typedef struct demangled_hash_entry
69 /* Hash and comparison functions for these hash tables. */
71 static int hash_string_eq (const void *, const void *);
72 static hashval_t
hash_string_hash (const void *);
75 hash_string_eq (const void *s1_p
, const void *s2_p
)
77 const char *const *s1
= (const char *const *) s1_p
;
78 const char *s2
= (const char *) s2_p
;
79 return strcmp (*s1
, s2
) == 0;
83 hash_string_hash (const void *s_p
)
85 const char *const *s
= (const char *const *) s_p
;
86 return (*htab_hash_string
) (*s
);
89 static htab_t symbol_table
;
91 static struct symbol_hash_entry
* symbol_hash_lookup (const char *, int);
92 static struct file_hash_entry
* file_hash_lookup (const char *);
93 static struct demangled_hash_entry
*demangled_hash_lookup (const char *, int);
94 static void symbol_push (symbol
*);
95 static symbol
* symbol_pop (void);
96 static void file_push (file
*);
97 static file
* file_pop (void);
98 static void tlink_init (void);
99 static int tlink_execute (const char *, char **, const char *);
100 static char * frob_extension (const char *, const char *);
101 static char * obstack_fgets (FILE *, struct obstack
*);
102 static char * tfgets (FILE *);
103 static char * pfgets (FILE *);
104 static void freadsym (FILE *, file
*, int);
105 static void read_repo_file (file
*);
106 static void maybe_tweak (char *, file
*);
107 static int recompile_files (void);
108 static int read_repo_files (char **);
109 static void demangle_new_symbols (void);
110 static int scan_linker_output (const char *);
112 /* Look up an entry in the symbol hash table. */
114 static struct symbol_hash_entry
*
115 symbol_hash_lookup (const char *string
, int create
)
118 e
= htab_find_slot_with_hash (symbol_table
, string
,
119 (*htab_hash_string
) (string
),
120 create
? INSERT
: NO_INSERT
);
125 struct symbol_hash_entry
*v
;
126 *e
= v
= xcalloc (1, sizeof (*v
));
127 v
->key
= xstrdup (string
);
132 static htab_t file_table
;
134 /* Look up an entry in the file hash table. */
136 static struct file_hash_entry
*
137 file_hash_lookup (const char *string
)
140 e
= htab_find_slot_with_hash (file_table
, string
,
141 (*htab_hash_string
) (string
),
145 struct file_hash_entry
*v
;
146 *e
= v
= xcalloc (1, sizeof (*v
));
147 v
->key
= xstrdup (string
);
152 static htab_t demangled_table
;
154 /* Look up an entry in the demangled name hash table. */
156 static struct demangled_hash_entry
*
157 demangled_hash_lookup (const char *string
, int create
)
160 e
= htab_find_slot_with_hash (demangled_table
, string
,
161 (*htab_hash_string
) (string
),
162 create
? INSERT
: NO_INSERT
);
167 struct demangled_hash_entry
*v
;
168 *e
= v
= xcalloc (1, sizeof (*v
));
169 v
->key
= xstrdup (string
);
176 struct symbol_stack_entry
179 struct symbol_stack_entry
*next
;
181 struct obstack symbol_stack_obstack
;
182 struct symbol_stack_entry
*symbol_stack
;
184 struct file_stack_entry
187 struct file_stack_entry
*next
;
189 struct obstack file_stack_obstack
;
190 struct file_stack_entry
*file_stack
;
193 symbol_push (symbol
*p
)
195 struct symbol_stack_entry
*ep
= obstack_alloc
196 (&symbol_stack_obstack
, sizeof (struct symbol_stack_entry
));
198 ep
->next
= symbol_stack
;
205 struct symbol_stack_entry
*ep
= symbol_stack
;
210 symbol_stack
= ep
->next
;
211 obstack_free (&symbol_stack_obstack
, ep
);
218 struct file_stack_entry
*ep
;
224 (&file_stack_obstack
, sizeof (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
);
278 tlink_execute (const char *prog
, char **argv
, const char *redir
)
280 collect_execute (prog
, argv
, redir
);
281 return collect_wait (prog
);
285 frob_extension (const char *s
, const char *ext
)
287 const char *p
= strrchr (s
, '/');
290 p
= strrchr (p
, '.');
294 obstack_grow (&temporary_obstack
, s
, p
- s
);
295 return obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
299 obstack_fgets (FILE *stream
, struct obstack
*ob
)
302 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
303 obstack_1grow (ob
, c
);
304 if (obstack_object_size (ob
) == 0)
306 obstack_1grow (ob
, '\0');
307 return obstack_finish (ob
);
311 tfgets (FILE *stream
)
313 return obstack_fgets (stream
, &temporary_obstack
);
317 pfgets (FILE *stream
)
319 return xstrdup (tfgets (stream
));
322 /* Real tlink code. */
324 /* Subroutine of read_repo_file. We are reading the repo file for file F,
325 which is coming in on STREAM, and the symbol that comes next in STREAM
326 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
328 XXX "provided" is unimplemented, both here and in the compiler. */
331 freadsym (FILE *stream
, file
*f
, int chosen
)
336 const char *name
= tfgets (stream
);
337 sym
= symbol_hash_lookup (name
, true);
340 if (sym
->file
== NULL
)
342 /* We didn't have this symbol already, so we choose this file. */
346 sym
->chosen
= chosen
;
350 /* We want this file; cast aside any pretender. */
352 if (sym
->chosen
&& sym
->file
!= f
)
354 if (sym
->chosen
== 1)
355 file_push (sym
->file
);
360 chosen
= sym
->chosen
;
364 sym
->chosen
= chosen
;
368 /* Read in the repo file denoted by F, and record all its information. */
371 read_repo_file (file
*f
)
374 FILE *stream
= fopen (f
->key
, "r");
376 if (tlink_verbose
>= 2)
377 fprintf (stderr
, _("collect: reading %s\n"), f
->key
);
379 while (fscanf (stream
, "%c ", &c
) == 1)
384 f
->args
= pfgets (stream
);
387 f
->dir
= pfgets (stream
);
390 f
->main
= pfgets (stream
);
393 freadsym (stream
, f
, 2);
396 freadsym (stream
, f
, 1);
399 freadsym (stream
, f
, 0);
402 obstack_free (&temporary_obstack
, temporary_firstobj
);
406 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
411 /* We might want to modify LINE, which is a symbol line from file F. We do
412 this if either we saw an error message referring to the symbol in
413 question, or we have already allocated the symbol to another file and
414 this one wants to emit it as well. */
417 maybe_tweak (char *line
, file
*f
)
419 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
421 if ((sym
->file
== f
&& sym
->tweaking
)
422 || (sym
->file
!= f
&& line
[0] == 'C'))
434 /* Update the repo files for each of the object files we have adjusted and
437 XXX Should this use collect_execute instead of system? */
440 recompile_files (void)
444 putenv (xstrdup ("COMPILER_PATH="));
445 putenv (xstrdup ("LIBRARY_PATH="));
447 while ((f
= file_pop ()) != NULL
)
449 char *line
, *command
;
450 FILE *stream
= fopen (f
->key
, "r");
451 const char *const outname
= frob_extension (f
->key
, ".rnw");
452 FILE *output
= fopen (outname
, "w");
454 while ((line
= tfgets (stream
)) != NULL
)
460 maybe_tweak (line
, f
);
462 fprintf (output
, "%s\n", line
);
466 rename (outname
, f
->key
);
468 obstack_grow (&temporary_obstack
, "cd ", 3);
469 obstack_grow (&temporary_obstack
, f
->dir
, strlen (f
->dir
));
470 obstack_grow (&temporary_obstack
, "; ", 2);
471 obstack_grow (&temporary_obstack
, c_file_name
, strlen (c_file_name
));
472 obstack_1grow (&temporary_obstack
, ' ');
473 obstack_grow (&temporary_obstack
, f
->args
, strlen (f
->args
));
474 obstack_1grow (&temporary_obstack
, ' ');
475 command
= obstack_copy0 (&temporary_obstack
, f
->main
, strlen (f
->main
));
478 fprintf (stderr
, _("collect: recompiling %s\n"), f
->main
);
479 if (tlink_verbose
>= 3)
480 fprintf (stderr
, "%s\n", command
);
482 if (system (command
) != 0)
487 obstack_free (&temporary_obstack
, temporary_firstobj
);
492 /* The first phase of processing: determine which object files have
493 .rpo files associated with them, and read in the information. */
496 read_repo_files (char **object_lst
)
498 char **object
= object_lst
;
500 for (; *object
; object
++)
505 /* Don't bother trying for ld flags. */
506 if (*object
[0] == '-')
509 p
= frob_extension (*object
, ".rpo");
511 if (! file_exists (p
))
514 f
= file_hash_lookup (p
);
519 if (file_stack
!= NULL
&& ! recompile_files ())
522 return (symbol_stack
!= NULL
);
525 /* Add the demangled forms of any new symbols to the hash table. */
528 demangle_new_symbols (void)
532 while ((sym
= symbol_pop ()) != NULL
)
535 const char *p
= cplus_demangle (sym
->key
, DMGL_PARAMS
| DMGL_ANSI
);
540 dem
= demangled_hash_lookup (p
, true);
541 dem
->mangled
= sym
->key
;
545 /* Step through the output of the linker, in the file named FNAME, and
546 adjust the settings for each symbol encountered. */
549 scan_linker_output (const char *fname
)
551 FILE *stream
= fopen (fname
, "r");
554 while ((line
= tfgets (stream
)) != NULL
)
560 while (*p
&& ISSPACE ((unsigned char) *p
))
566 for (q
= p
; *q
&& ! ISSPACE ((unsigned char) *q
); ++q
)
569 /* Try the first word on the line. */
572 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
573 p
+= strlen (USER_LABEL_PREFIX
);
577 sym
= symbol_hash_lookup (p
, false);
579 /* Some SVR4 linkers produce messages like
580 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
582 if (! sym
&& ! end
&& strstr (q
+ 1, "Undefined symbol: "))
584 char *p
= strrchr (q
+ 1, ' ');
588 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
589 p
+= strlen (USER_LABEL_PREFIX
);
590 sym
= symbol_hash_lookup (p
, false);
594 /* Try a mangled name in quotes. */
596 const char *oldq
= q
+ 1;
600 /* First try `GNU style'. */
601 p
= strchr (oldq
, '`');
603 p
++, q
= strchr (p
, '\'');
604 /* Then try "double quotes". */
605 else if (p
= strchr (oldq
, '"'), p
)
606 p
++, q
= strchr (p
, '"');
608 /* Then try entire line. */
609 q
= strchr (oldq
, 0);
616 /* Don't let the strstr's below see the demangled name; we
617 might get spurious matches. */
620 /* powerpc64-linux references .foo when calling function foo. */
625 /* We need to check for certain error keywords here, or we would
626 mistakenly use GNU ld's "In function `foo':" message. */
627 if (q
&& (strstr (oldq
, "ndefined")
628 || strstr (oldq
, "nresolved")
629 || strstr (oldq
, "nsatisfied")
630 || strstr (oldq
, "ultiple")))
633 dem
= demangled_hash_lookup (p
, false);
635 sym
= symbol_hash_lookup (dem
->mangled
, false);
638 if (!strncmp (p
, USER_LABEL_PREFIX
,
639 strlen (USER_LABEL_PREFIX
)))
640 p
+= strlen (USER_LABEL_PREFIX
);
641 sym
= symbol_hash_lookup (p
, false);
646 if (sym
&& sym
->tweaked
)
651 if (sym
&& !sym
->tweaking
)
653 if (tlink_verbose
>= 2)
654 fprintf (stderr
, _("collect: tweaking %s in %s\n"),
655 sym
->key
, sym
->file
->key
);
657 file_push (sym
->file
);
660 obstack_free (&temporary_obstack
, temporary_firstobj
);
664 return (file_stack
!= NULL
);
667 /* Entry point for tlink. Called from main in collect2.c.
669 Iteratively try to provide definitions for all the unresolved symbols
670 mentioned in the linker error messages.
672 LD_ARGV is an array of arguments for the linker.
673 OBJECT_LST is an array of object files that we may be able to recompile
674 to provide missing definitions. Currently ignored. */
677 do_tlink (char **ld_argv
, char **object_lst ATTRIBUTE_UNUSED
)
679 int exit
= tlink_execute ("ld", ld_argv
, ldout
);
687 /* Until collect does a better job of figuring out which are object
688 files, assume that everything on the command line could be. */
689 if (read_repo_files (ld_argv
))
690 while (exit
&& i
++ < MAX_ITERATIONS
)
692 if (tlink_verbose
>= 3)
694 demangle_new_symbols ();
695 if (! scan_linker_output (ldout
))
697 if (! recompile_files ())
700 fprintf (stderr
, _("collect: relinking\n"));
701 exit
= tlink_execute ("ld", ld_argv
, ldout
);
709 error ("ld returned %d exit status", exit
);