1 /* Scan linker error messages for missing template instantiations and provide
4 Copyright (C) 1995, 1998 Free Software Foundation, Inc.
5 Contributed by Jason Merrill (jason@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
31 #define MAX_ITERATIONS 17
33 /* Obstack allocation and deallocation routines. */
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
37 /* Defined in collect2.c. */
38 extern int vflag
, debug
;
40 extern char *c_file_name
;
41 extern struct obstack temporary_obstack
;
42 extern struct obstack permanent_obstack
;
43 extern char * temporary_firstobj
;
45 /* Defined in the automatically-generated underscore.c. */
46 extern int prepends_underscore
;
48 static int tlink_verbose
;
50 /* Hash table boilerplate for working with hash.[ch]. We have hash tables
51 for symbol names, file names, and demangled symbols. */
53 typedef struct symbol_hash_entry
55 struct hash_entry root
;
56 struct file_hash_entry
*file
;
62 typedef struct file_hash_entry
64 struct hash_entry root
;
71 typedef struct demangled_hash_entry
73 struct hash_entry root
;
77 static struct hash_table symbol_table
;
79 static struct hash_entry
* symbol_hash_newfunc
PARAMS ((struct hash_entry
*,
82 static struct symbol_hash_entry
* symbol_hash_lookup
PARAMS ((const char *,
84 static struct hash_entry
* file_hash_newfunc
PARAMS ((struct hash_entry
*,
87 static struct file_hash_entry
* file_hash_lookup
PARAMS ((const char *));
88 static struct hash_entry
* demangled_hash_newfunc
PARAMS ((struct hash_entry
*,
91 static struct demangled_hash_entry
*
92 demangled_hash_lookup
PARAMS ((const char *, boolean
));
93 static void symbol_push
PARAMS ((symbol
*));
94 static symbol
* symbol_pop
PARAMS ((void));
95 static void file_push
PARAMS ((file
*));
96 static file
* file_pop
PARAMS ((void));
97 static void tlink_init
PARAMS ((void));
98 static int tlink_execute
PARAMS ((char *, char **, char *));
99 static char * frob_extension
PARAMS ((char *, const char *));
100 static char * obstack_fgets
PARAMS ((FILE *, struct obstack
*));
101 static char * tfgets
PARAMS ((FILE *));
102 static char * pfgets
PARAMS ((FILE *));
103 static void freadsym
PARAMS ((FILE *, file
*, int));
104 static void read_repo_file
PARAMS ((file
*));
105 static void maybe_tweak
PARAMS ((char *, file
*));
106 static int recompile_files
PARAMS ((void));
107 static int read_repo_files
PARAMS ((char **));
108 static void demangle_new_symbols
PARAMS ((void));
109 static int scan_linker_output
PARAMS ((const char *));
111 /* Create a new entry for the symbol hash table.
112 Passed to hash_table_init. */
114 static struct hash_entry
*
115 symbol_hash_newfunc (entry
, table
, string
)
116 struct hash_entry
*entry
;
117 struct hash_table
*table
;
118 hash_table_key string
;
120 struct symbol_hash_entry
*ret
= (struct symbol_hash_entry
*) entry
;
123 ret
= ((struct symbol_hash_entry
*)
124 hash_allocate (table
, sizeof (struct symbol_hash_entry
)));
132 return (struct hash_entry
*) ret
;
135 /* Look up an entry in the symbol hash table. */
137 static struct symbol_hash_entry
*
138 symbol_hash_lookup (string
, create
)
142 return ((struct symbol_hash_entry
*)
143 hash_lookup (&symbol_table
, (hash_table_key
) string
,
144 create
, &string_copy
));
147 static struct hash_table file_table
;
149 /* Create a new entry for the file hash table.
150 Passed to hash_table_init. */
152 static struct hash_entry
*
153 file_hash_newfunc (entry
, table
, string
)
154 struct hash_entry
*entry
;
155 struct hash_table
*table
;
156 hash_table_key string
;
158 struct file_hash_entry
*ret
= (struct file_hash_entry
*) entry
;
161 ret
= ((struct file_hash_entry
*)
162 hash_allocate (table
, sizeof (struct file_hash_entry
)));
170 return (struct hash_entry
*) ret
;
173 /* Look up an entry in the file hash table. */
175 static struct file_hash_entry
*
176 file_hash_lookup (string
)
179 return ((struct file_hash_entry
*)
180 hash_lookup (&file_table
, (hash_table_key
) string
, true,
184 static struct hash_table demangled_table
;
186 /* Create a new entry for the demangled name hash table.
187 Passed to hash_table_init. */
189 static struct hash_entry
*
190 demangled_hash_newfunc (entry
, table
, string
)
191 struct hash_entry
*entry
;
192 struct hash_table
*table
;
193 hash_table_key string
;
195 struct demangled_hash_entry
*ret
= (struct demangled_hash_entry
*) entry
;
198 ret
= ((struct demangled_hash_entry
*)
199 hash_allocate (table
, sizeof (struct demangled_hash_entry
)));
204 return (struct hash_entry
*) ret
;
207 /* Look up an entry in the demangled name hash table. */
209 static struct demangled_hash_entry
*
210 demangled_hash_lookup (string
, create
)
214 return ((struct demangled_hash_entry
*)
215 hash_lookup (&demangled_table
, (hash_table_key
) string
,
216 create
, &string_copy
));
221 struct symbol_stack_entry
224 struct symbol_stack_entry
*next
;
226 struct obstack symbol_stack_obstack
;
227 struct symbol_stack_entry
*symbol_stack
;
229 struct file_stack_entry
232 struct file_stack_entry
*next
;
234 struct obstack file_stack_obstack
;
235 struct file_stack_entry
*file_stack
;
241 struct symbol_stack_entry
*ep
= (struct symbol_stack_entry
*) obstack_alloc
242 (&symbol_stack_obstack
, sizeof (struct symbol_stack_entry
));
244 ep
->next
= symbol_stack
;
251 struct symbol_stack_entry
*ep
= symbol_stack
;
256 symbol_stack
= ep
->next
;
257 obstack_free (&symbol_stack_obstack
, ep
);
265 struct file_stack_entry
*ep
;
270 ep
= (struct file_stack_entry
*) obstack_alloc
271 (&file_stack_obstack
, sizeof (struct file_stack_entry
));
273 ep
->next
= file_stack
;
281 struct file_stack_entry
*ep
= file_stack
;
286 file_stack
= ep
->next
;
287 obstack_free (&file_stack_obstack
, ep
);
292 /* Other machinery. */
294 /* Initialize the tlink machinery. Called from do_tlink. */
301 hash_table_init (&symbol_table
, symbol_hash_newfunc
, &string_hash
,
303 hash_table_init (&file_table
, file_hash_newfunc
, &string_hash
,
305 hash_table_init (&demangled_table
, demangled_hash_newfunc
,
306 &string_hash
, &string_compare
);
307 obstack_begin (&symbol_stack_obstack
, 0);
308 obstack_begin (&file_stack_obstack
, 0);
310 p
= getenv ("TLINK_VERBOSE");
312 tlink_verbose
= atoi (p
);
324 tlink_execute (prog
, argv
, redir
)
329 collect_execute (prog
, argv
, redir
);
330 return collect_wait (prog
);
334 frob_extension (s
, ext
)
338 char *p
= rindex (s
, '/');
345 obstack_grow (&temporary_obstack
, s
, p
- s
);
346 return obstack_copy0 (&temporary_obstack
, ext
, strlen (ext
));
350 obstack_fgets (stream
, ob
)
355 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
356 obstack_1grow (ob
, c
);
357 if (obstack_object_size (ob
) == 0)
359 obstack_1grow (ob
, '\0');
360 return obstack_finish (ob
);
367 return obstack_fgets (stream
, &temporary_obstack
);
374 return obstack_fgets (stream
, &permanent_obstack
);
377 /* Real tlink code. */
379 /* Subroutine of read_repo_file. We are reading the repo file for file F,
380 which is coming in on STREAM, and the symbol that comes next in STREAM
381 is offerred, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
383 XXX "provided" is unimplemented, both here and in the compiler. */
386 freadsym (stream
, f
, chosen
)
394 char *name
= tfgets (stream
);
395 sym
= symbol_hash_lookup (name
, true);
398 if (sym
->file
== NULL
)
400 /* We didn't have this symbol already, so we choose this file. */
404 sym
->chosen
= chosen
;
408 /* We want this file; cast aside any pretender. */
410 if (sym
->chosen
&& sym
->file
!= f
)
412 if (sym
->chosen
== 1)
413 file_push (sym
->file
);
418 chosen
= sym
->chosen
;
422 sym
->chosen
= chosen
;
426 /* Read in the repo file denoted by F, and record all its information. */
433 FILE *stream
= fopen ((char*) f
->root
.key
, "r");
435 if (tlink_verbose
>= 2)
436 fprintf (stderr
, "collect: reading %s\n",
437 (char*) f
->root
.key
);
439 while (fscanf (stream
, "%c ", &c
) == 1)
444 f
->args
= pfgets (stream
);
447 f
->dir
= pfgets (stream
);
450 f
->main
= pfgets (stream
);
453 freadsym (stream
, f
, 2);
456 freadsym (stream
, f
, 1);
459 freadsym (stream
, f
, 0);
462 obstack_free (&temporary_obstack
, temporary_firstobj
);
466 f
->args
= getenv ("COLLECT_GCC_OPTIONS");
471 /* We might want to modify LINE, which is a symbol line from file F. We do
472 this if either we saw an error message referring to the symbol in
473 question, or we have already allocated the symbol to another file and
474 this one wants to emit it as well. */
477 maybe_tweak (line
, f
)
481 symbol
*sym
= symbol_hash_lookup (line
+ 2, false);
483 if ((sym
->file
== f
&& sym
->tweaking
)
484 || (sym
->file
!= f
&& line
[0] == 'C'))
496 /* Update the repo files for each of the object files we have adjusted and
499 XXX Should this use collect_execute instead of system? */
506 while ((f
= file_pop ()) != NULL
)
508 char *line
, *command
;
509 FILE *stream
= fopen ((char*) f
->root
.key
, "r");
510 char *outname
= frob_extension ((char*) f
->root
.key
, ".rnw");
511 FILE *output
= fopen (outname
, "w");
513 while ((line
= tfgets (stream
)) != NULL
)
519 maybe_tweak (line
, f
);
521 fprintf (output
, "%s\n", line
);
525 rename (outname
, (char*) f
->root
.key
);
527 obstack_grow (&temporary_obstack
, "cd ", 3);
528 obstack_grow (&temporary_obstack
, f
->dir
, strlen (f
->dir
));
529 obstack_grow (&temporary_obstack
, "; ", 2);
530 obstack_grow (&temporary_obstack
, c_file_name
, strlen (c_file_name
));
531 obstack_1grow (&temporary_obstack
, ' ');
532 obstack_grow (&temporary_obstack
, f
->args
, strlen (f
->args
));
533 obstack_1grow (&temporary_obstack
, ' ');
534 command
= obstack_copy0 (&temporary_obstack
, f
->main
, strlen (f
->main
));
537 fprintf (stderr
, "collect: recompiling %s\n", f
->main
);
538 if (tlink_verbose
>= 3)
539 fprintf (stderr
, "%s\n", command
);
541 if (system (command
) != 0)
546 obstack_free (&temporary_obstack
, temporary_firstobj
);
551 /* The first phase of processing: determine which object files have
552 .rpo files associated with them, and read in the information. */
555 read_repo_files (object_lst
)
558 char **object
= object_lst
;
560 for (; *object
; object
++)
562 char *p
= frob_extension (*object
, ".rpo");
565 if (! file_exists (p
))
568 f
= file_hash_lookup (p
);
573 if (file_stack
!= NULL
&& ! recompile_files ())
576 return (symbol_stack
!= NULL
);
579 /* Add the demangled forms of any new symbols to the hash table. */
582 demangle_new_symbols ()
586 while ((sym
= symbol_pop ()) != NULL
)
589 char *p
= cplus_demangle ((char*) sym
->root
.key
,
590 DMGL_PARAMS
| DMGL_ANSI
);
595 dem
= demangled_hash_lookup (p
, true);
596 dem
->mangled
= (char*) sym
->root
.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 (fname
)
607 FILE *stream
= fopen (fname
, "r");
610 while ((line
= tfgets (stream
)) != NULL
)
616 while (*p
&& ISSPACE ((unsigned char)*p
))
622 for (q
= p
; *q
&& ! ISSPACE ((unsigned char)*q
); ++q
)
625 /* Try the first word on the line. */
628 if (*p
== '_' && prepends_underscore
)
633 sym
= symbol_hash_lookup (p
, false);
636 /* Try a mangled name in quotes. */
642 /* First try `GNU style'. */
643 p
= index (oldq
, '`');
645 p
++, q
= index (p
, '\'');
646 /* Then try "double quotes". */
647 else if (p
= index (oldq
, '"'), p
)
648 p
++, q
= index (p
, '"');
653 dem
= demangled_hash_lookup (p
, false);
655 sym
= symbol_hash_lookup (dem
->mangled
, false);
657 sym
= symbol_hash_lookup (p
, false);
661 if (sym
&& sym
->tweaked
)
666 if (sym
&& !sym
->tweaking
)
668 if (tlink_verbose
>= 2)
669 fprintf (stderr
, "collect: tweaking %s in %s\n",
670 (char*) sym
->root
.key
, (char*) sym
->file
->root
.key
);
672 file_push (sym
->file
);
675 obstack_free (&temporary_obstack
, temporary_firstobj
);
679 return (file_stack
!= NULL
);
682 /* Entry point for tlink. Called from main in collect2.c.
684 Iteratively try to provide definitions for all the unresolved symbols
685 mentioned in the linker error messages.
687 LD_ARGV is an array of arguments for the linker.
688 OBJECT_LST is an array of object files that we may be able to recompile
689 to provide missing definitions. Currently ignored. */
692 do_tlink (ld_argv
, object_lst
)
693 char **ld_argv
, **object_lst
;
695 int exit
= tlink_execute ("ld", ld_argv
, ldout
);
703 /* Until collect does a better job of figuring out which are object
704 files, assume that everything on the command line could be. */
705 if (read_repo_files (ld_argv
))
706 while (exit
&& i
++ < MAX_ITERATIONS
)
708 if (tlink_verbose
>= 3)
710 demangle_new_symbols ();
711 if (! scan_linker_output (ldout
))
713 if (! recompile_files ())
716 fprintf (stderr
, "collect: relinking\n");
717 exit
= tlink_execute ("ld", ld_argv
, ldout
);
725 error ("ld returned %d exit status", exit
);