]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tlink.c
Fix typo.
[thirdparty/gcc.git] / gcc / tlink.c
CommitLineData
aa32d841
JL
1/* Scan linker error messages for missing template instantiations and provide
2 them.
3
d6edb99e 4 Copyright (C) 1995, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
aa32d841
JL
5 Contributed by Jason Merrill (jason@cygnus.com).
6
1322177d 7This file is part of GCC.
aa32d841 8
1322177d
LB
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 2, or (at your option) any later
12version.
aa32d841 13
1322177d
LB
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
aa32d841
JL
18
19You should have received a copy of the GNU General Public License
1322177d
LB
20along with GCC; see the file COPYING. If not, write to the Free
21Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2202111-1307, USA. */
aa32d841 23
aa32d841 24#include "config.h"
670ee920 25#include "system.h"
4977bab6
ZW
26#include "coretypes.h"
27#include "tm.h"
993c790e 28#include "intl.h"
e2500fed
GK
29#include "obstack.h"
30#include "hashtab.h"
aa32d841 31#include "demangle.h"
2edfd4ee 32#include "collect2.h"
aa32d841
JL
33
34#define MAX_ITERATIONS 17
35
aa32d841
JL
36/* Defined in the automatically-generated underscore.c. */
37extern int prepends_underscore;
38
39static int tlink_verbose;
40\f
e2500fed 41/* Hash table boilerplate for working with htab_t. We have hash tables
eb686064 42 for symbol names, file names, and demangled symbols. */
aa32d841
JL
43
44typedef struct symbol_hash_entry
45{
e2500fed 46 const char *key;
aa32d841
JL
47 struct file_hash_entry *file;
48 int chosen;
49 int tweaking;
50 int tweaked;
51} symbol;
52
53typedef struct file_hash_entry
54{
e2500fed 55 const char *key;
aa32d841
JL
56 const char *args;
57 const char *dir;
58 const char *main;
59 int tweaking;
60} file;
61
62typedef struct demangled_hash_entry
63{
e2500fed 64 const char *key;
aa32d841
JL
65 const char *mangled;
66} demangled;
67
e2500fed
GK
68/* Hash and comparison functions for these hash tables. */
69
70static int hash_string_eq PARAMS ((const void *, const void *));
71static hashval_t hash_string_hash PARAMS ((const void *));
72
73static int
74hash_string_eq (s1_p, s2_p)
75 const void *s1_p;
76 const void *s2_p;
77{
17fa74a2
KH
78 const char *const *s1 = (const char *const *) s1_p;
79 const char *s2 = (const char *) s2_p;
e2500fed
GK
80 return strcmp (*s1, s2) == 0;
81}
82
83static hashval_t
84hash_string_hash (s_p)
85 const void *s_p;
86{
17fa74a2 87 const char *const *s = (const char *const *) s_p;
e2500fed
GK
88 return (*htab_hash_string) (*s);
89}
90
91static htab_t symbol_table;
aa32d841 92
54ea1de9 93static struct symbol_hash_entry * symbol_hash_lookup PARAMS ((const char *,
37a58036 94 int));
54ea1de9 95static struct file_hash_entry * file_hash_lookup PARAMS ((const char *));
54ea1de9 96static struct demangled_hash_entry *
37a58036 97 demangled_hash_lookup PARAMS ((const char *, int));
54ea1de9
KG
98static void symbol_push PARAMS ((symbol *));
99static symbol * symbol_pop PARAMS ((void));
100static void file_push PARAMS ((file *));
101static file * file_pop PARAMS ((void));
102static void tlink_init PARAMS ((void));
d4058195
KG
103static int tlink_execute PARAMS ((const char *, char **, const char *));
104static char * frob_extension PARAMS ((const char *, const char *));
54ea1de9
KG
105static char * obstack_fgets PARAMS ((FILE *, struct obstack *));
106static char * tfgets PARAMS ((FILE *));
107static char * pfgets PARAMS ((FILE *));
108static void freadsym PARAMS ((FILE *, file *, int));
109static void read_repo_file PARAMS ((file *));
110static void maybe_tweak PARAMS ((char *, file *));
111static int recompile_files PARAMS ((void));
112static int read_repo_files PARAMS ((char **));
113static void demangle_new_symbols PARAMS ((void));
114static int scan_linker_output PARAMS ((const char *));
115
eb686064
JM
116/* Look up an entry in the symbol hash table. */
117
aa32d841
JL
118static struct symbol_hash_entry *
119symbol_hash_lookup (string, create)
120 const char *string;
37a58036 121 int create;
aa32d841 122{
e2500fed
GK
123 PTR *e;
124 e = htab_find_slot_with_hash (symbol_table, string,
17fa74a2 125 (*htab_hash_string) (string),
e2500fed
GK
126 create ? INSERT : NO_INSERT);
127 if (e == NULL)
128 return NULL;
129 if (*e == NULL)
aa32d841 130 {
e2500fed
GK
131 struct symbol_hash_entry *v;
132 *e = v = xcalloc (1, sizeof (*v));
133 v->key = xstrdup (string);
aa32d841 134 }
e2500fed 135 return *e;
aa32d841
JL
136}
137
e2500fed
GK
138static htab_t file_table;
139
eb686064
JM
140/* Look up an entry in the file hash table. */
141
aa32d841
JL
142static struct file_hash_entry *
143file_hash_lookup (string)
144 const char *string;
145{
e2500fed
GK
146 PTR *e;
147 e = htab_find_slot_with_hash (file_table, string,
17fa74a2 148 (*htab_hash_string) (string),
e2500fed
GK
149 INSERT);
150 if (*e == NULL)
aa32d841 151 {
e2500fed
GK
152 struct file_hash_entry *v;
153 *e = v = xcalloc (1, sizeof (*v));
154 v->key = xstrdup (string);
aa32d841 155 }
e2500fed 156 return *e;
aa32d841
JL
157}
158
e2500fed
GK
159static htab_t demangled_table;
160
eb686064
JM
161/* Look up an entry in the demangled name hash table. */
162
aa32d841
JL
163static struct demangled_hash_entry *
164demangled_hash_lookup (string, create)
165 const char *string;
37a58036 166 int create;
aa32d841 167{
e2500fed
GK
168 PTR *e;
169 e = htab_find_slot_with_hash (demangled_table, string,
17fa74a2 170 (*htab_hash_string) (string),
e2500fed
GK
171 create ? INSERT : NO_INSERT);
172 if (e == NULL)
173 return NULL;
174 if (*e == NULL)
175 {
176 struct demangled_hash_entry *v;
177 *e = v = xcalloc (1, sizeof (*v));
178 v->key = xstrdup (string);
179 }
180 return *e;
aa32d841
JL
181}
182\f
183/* Stack code. */
184
185struct symbol_stack_entry
186{
187 symbol *value;
188 struct symbol_stack_entry *next;
189};
190struct obstack symbol_stack_obstack;
191struct symbol_stack_entry *symbol_stack;
192
193struct file_stack_entry
194{
195 file *value;
196 struct file_stack_entry *next;
197};
198struct obstack file_stack_obstack;
199struct file_stack_entry *file_stack;
200
201static void
202symbol_push (p)
203 symbol *p;
204{
205 struct symbol_stack_entry *ep = (struct symbol_stack_entry *) obstack_alloc
206 (&symbol_stack_obstack, sizeof (struct symbol_stack_entry));
207 ep->value = p;
208 ep->next = symbol_stack;
209 symbol_stack = ep;
210}
211
212static symbol *
213symbol_pop ()
214{
215 struct symbol_stack_entry *ep = symbol_stack;
216 symbol *p;
217 if (ep == NULL)
218 return NULL;
219 p = ep->value;
220 symbol_stack = ep->next;
221 obstack_free (&symbol_stack_obstack, ep);
222 return p;
223}
224
225static void
226file_push (p)
227 file *p;
228{
229 struct file_stack_entry *ep;
230
231 if (p->tweaking)
232 return;
233
234 ep = (struct file_stack_entry *) obstack_alloc
235 (&file_stack_obstack, sizeof (struct file_stack_entry));
236 ep->value = p;
237 ep->next = file_stack;
238 file_stack = ep;
239 p->tweaking = 1;
240}
241
242static file *
243file_pop ()
244{
245 struct file_stack_entry *ep = file_stack;
246 file *p;
247 if (ep == NULL)
248 return NULL;
249 p = ep->value;
250 file_stack = ep->next;
251 obstack_free (&file_stack_obstack, ep);
252 p->tweaking = 0;
253 return p;
254}
255\f
256/* Other machinery. */
257
eb686064
JM
258/* Initialize the tlink machinery. Called from do_tlink. */
259
aa32d841
JL
260static void
261tlink_init ()
262{
d4058195 263 const char *p;
aa32d841 264
e2500fed
GK
265 symbol_table = htab_create (500, hash_string_hash, hash_string_eq,
266 NULL);
267 file_table = htab_create (500, hash_string_hash, hash_string_eq,
268 NULL);
269 demangled_table = htab_create (500, hash_string_hash, hash_string_eq,
270 NULL);
271
aa32d841
JL
272 obstack_begin (&symbol_stack_obstack, 0);
273 obstack_begin (&file_stack_obstack, 0);
274
275 p = getenv ("TLINK_VERBOSE");
276 if (p)
277 tlink_verbose = atoi (p);
278 else
279 {
280 tlink_verbose = 1;
281 if (vflag)
282 tlink_verbose = 2;
283 if (debug)
284 tlink_verbose = 3;
285 }
286}
287
288static int
289tlink_execute (prog, argv, redir)
d4058195 290 const char *prog;
aa32d841 291 char **argv;
d4058195 292 const char *redir;
aa32d841
JL
293{
294 collect_execute (prog, argv, redir);
295 return collect_wait (prog);
d92b4486 296}
aa32d841
JL
297
298static char *
299frob_extension (s, ext)
d4058195 300 const char *s;
54ea1de9 301 const char *ext;
aa32d841 302{
9473c522 303 const char *p = strrchr (s, '/');
aa32d841
JL
304 if (! p)
305 p = s;
9473c522 306 p = strrchr (p, '.');
aa32d841
JL
307 if (! p)
308 p = s + strlen (s);
309
310 obstack_grow (&temporary_obstack, s, p - s);
311 return obstack_copy0 (&temporary_obstack, ext, strlen (ext));
312}
313
314static char *
315obstack_fgets (stream, ob)
316 FILE *stream;
317 struct obstack *ob;
318{
319 int c;
320 while ((c = getc (stream)) != EOF && c != '\n')
321 obstack_1grow (ob, c);
322 if (obstack_object_size (ob) == 0)
323 return NULL;
324 obstack_1grow (ob, '\0');
325 return obstack_finish (ob);
326}
327
328static char *
329tfgets (stream)
330 FILE *stream;
331{
332 return obstack_fgets (stream, &temporary_obstack);
333}
334
335static char *
336pfgets (stream)
337 FILE *stream;
338{
6d9f628e 339 return xstrdup (tfgets (stream));
aa32d841
JL
340}
341\f
342/* Real tlink code. */
343
eb686064
JM
344/* Subroutine of read_repo_file. We are reading the repo file for file F,
345 which is coming in on STREAM, and the symbol that comes next in STREAM
14b493d6 346 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
eb686064
JM
347
348 XXX "provided" is unimplemented, both here and in the compiler. */
349
aa32d841
JL
350static void
351freadsym (stream, f, chosen)
352 FILE *stream;
353 file *f;
354 int chosen;
355{
356 symbol *sym;
357
358 {
d4058195 359 const char *name = tfgets (stream);
aa32d841
JL
360 sym = symbol_hash_lookup (name, true);
361 }
362
363 if (sym->file == NULL)
364 {
eb686064
JM
365 /* We didn't have this symbol already, so we choose this file. */
366
aa32d841
JL
367 symbol_push (sym);
368 sym->file = f;
369 sym->chosen = chosen;
370 }
371 else if (chosen)
372 {
eb686064
JM
373 /* We want this file; cast aside any pretender. */
374
aa32d841
JL
375 if (sym->chosen && sym->file != f)
376 {
377 if (sym->chosen == 1)
378 file_push (sym->file);
379 else
380 {
381 file_push (f);
382 f = sym->file;
383 chosen = sym->chosen;
384 }
385 }
386 sym->file = f;
387 sym->chosen = chosen;
388 }
389}
390
eb686064
JM
391/* Read in the repo file denoted by F, and record all its information. */
392
aa32d841
JL
393static void
394read_repo_file (f)
395 file *f;
396{
397 char c;
e2500fed 398 FILE *stream = fopen (f->key, "r");
aa32d841
JL
399
400 if (tlink_verbose >= 2)
e2500fed 401 fprintf (stderr, _("collect: reading %s\n"), f->key);
aa32d841
JL
402
403 while (fscanf (stream, "%c ", &c) == 1)
404 {
405 switch (c)
406 {
407 case 'A':
408 f->args = pfgets (stream);
409 break;
410 case 'D':
411 f->dir = pfgets (stream);
412 break;
413 case 'M':
414 f->main = pfgets (stream);
415 break;
416 case 'P':
417 freadsym (stream, f, 2);
418 break;
419 case 'C':
420 freadsym (stream, f, 1);
421 break;
422 case 'O':
423 freadsym (stream, f, 0);
424 break;
425 }
426 obstack_free (&temporary_obstack, temporary_firstobj);
427 }
428 fclose (stream);
429 if (f->args == NULL)
430 f->args = getenv ("COLLECT_GCC_OPTIONS");
431 if (f->dir == NULL)
432 f->dir = ".";
433}
434
eb686064
JM
435/* We might want to modify LINE, which is a symbol line from file F. We do
436 this if either we saw an error message referring to the symbol in
437 question, or we have already allocated the symbol to another file and
438 this one wants to emit it as well. */
439
aa32d841
JL
440static void
441maybe_tweak (line, f)
442 char *line;
443 file *f;
444{
445 symbol *sym = symbol_hash_lookup (line + 2, false);
446
447 if ((sym->file == f && sym->tweaking)
448 || (sym->file != f && line[0] == 'C'))
449 {
450 sym->tweaking = 0;
451 sym->tweaked = 1;
452
453 if (line[0] == 'O')
454 line[0] = 'C';
455 else
456 line[0] = 'O';
457 }
458}
459
eb686064
JM
460/* Update the repo files for each of the object files we have adjusted and
461 recompile.
462
463 XXX Should this use collect_execute instead of system? */
464
aa32d841
JL
465static int
466recompile_files ()
467{
468 file *f;
469
e4d72771
KJ
470 putenv (xstrdup ("COMPILER_PATH="));
471 putenv (xstrdup ("LIBRARY_PATH="));
d92b4486 472
aa32d841
JL
473 while ((f = file_pop ()) != NULL)
474 {
475 char *line, *command;
e2500fed
GK
476 FILE *stream = fopen (f->key, "r");
477 const char *const outname = frob_extension (f->key, ".rnw");
aa32d841
JL
478 FILE *output = fopen (outname, "w");
479
480 while ((line = tfgets (stream)) != NULL)
481 {
482 switch (line[0])
483 {
484 case 'C':
485 case 'O':
486 maybe_tweak (line, f);
487 }
488 fprintf (output, "%s\n", line);
489 }
490 fclose (stream);
491 fclose (output);
e2500fed 492 rename (outname, f->key);
aa32d841
JL
493
494 obstack_grow (&temporary_obstack, "cd ", 3);
495 obstack_grow (&temporary_obstack, f->dir, strlen (f->dir));
496 obstack_grow (&temporary_obstack, "; ", 2);
497 obstack_grow (&temporary_obstack, c_file_name, strlen (c_file_name));
498 obstack_1grow (&temporary_obstack, ' ');
499 obstack_grow (&temporary_obstack, f->args, strlen (f->args));
500 obstack_1grow (&temporary_obstack, ' ');
501 command = obstack_copy0 (&temporary_obstack, f->main, strlen (f->main));
502
503 if (tlink_verbose)
5e4adfba 504 fprintf (stderr, _("collect: recompiling %s\n"), f->main);
aa32d841
JL
505 if (tlink_verbose >= 3)
506 fprintf (stderr, "%s\n", command);
507
508 if (system (command) != 0)
509 return 0;
510
511 read_repo_file (f);
512
513 obstack_free (&temporary_obstack, temporary_firstobj);
514 }
515 return 1;
516}
517
eb686064
JM
518/* The first phase of processing: determine which object files have
519 .rpo files associated with them, and read in the information. */
520
aa32d841
JL
521static int
522read_repo_files (object_lst)
523 char **object_lst;
524{
525 char **object = object_lst;
526
527 for (; *object; object++)
528 {
2c561874 529 const char *p;
aa32d841
JL
530 file *f;
531
2c561874
JM
532 /* Don't bother trying for ld flags. */
533 if (*object[0] == '-')
534 continue;
535
536 p = frob_extension (*object, ".rpo");
537
aa32d841
JL
538 if (! file_exists (p))
539 continue;
540
541 f = file_hash_lookup (p);
542
543 read_repo_file (f);
544 }
545
546 if (file_stack != NULL && ! recompile_files ())
547 return 0;
548
549 return (symbol_stack != NULL);
550}
551
eb686064
JM
552/* Add the demangled forms of any new symbols to the hash table. */
553
aa32d841
JL
554static void
555demangle_new_symbols ()
556{
557 symbol *sym;
558
559 while ((sym = symbol_pop ()) != NULL)
560 {
561 demangled *dem;
e2500fed 562 const char *p = cplus_demangle (sym->key, DMGL_PARAMS | DMGL_ANSI);
aa32d841
JL
563
564 if (! p)
565 continue;
566
567 dem = demangled_hash_lookup (p, true);
e2500fed 568 dem->mangled = sym->key;
aa32d841
JL
569 }
570}
571
eb686064
JM
572/* Step through the output of the linker, in the file named FNAME, and
573 adjust the settings for each symbol encountered. */
574
aa32d841
JL
575static int
576scan_linker_output (fname)
54ea1de9 577 const char *fname;
aa32d841
JL
578{
579 FILE *stream = fopen (fname, "r");
580 char *line;
581
582 while ((line = tfgets (stream)) != NULL)
583 {
584 char *p = line, *q;
585 symbol *sym;
586 int end;
d92b4486 587
17fa74a2 588 while (*p && ISSPACE ((unsigned char) *p))
aa32d841
JL
589 ++p;
590
591 if (! *p)
592 continue;
593
17fa74a2 594 for (q = p; *q && ! ISSPACE ((unsigned char) *q); ++q)
aa32d841
JL
595 ;
596
597 /* Try the first word on the line. */
598 if (*p == '.')
599 ++p;
3fd9d606
ZW
600 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
601 p += strlen (USER_LABEL_PREFIX);
aa32d841
JL
602
603 end = ! *q;
604 *q = 0;
605 sym = symbol_hash_lookup (p, false);
606
644c7c4f
GK
607 /* Some SVR4 linkers produce messages like
608 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
609 */
17fa74a2 610 if (! sym && ! end && strstr (q + 1, "Undefined symbol: "))
644c7c4f 611 {
17fa74a2 612 char *p = strrchr (q + 1, ' ');
644c7c4f
GK
613 p++;
614 if (*p == '.')
615 p++;
3fd9d606
ZW
616 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
617 p += strlen (USER_LABEL_PREFIX);
644c7c4f
GK
618 sym = symbol_hash_lookup (p, false);
619 }
620
aa32d841 621 if (! sym && ! end)
b4558b57 622 /* Try a mangled name in quotes. */
aa32d841 623 {
17fa74a2 624 const char *oldq = q + 1;
aa32d841 625 demangled *dem = 0;
aa32d841
JL
626 q = 0;
627
b4558b57 628 /* First try `GNU style'. */
9473c522 629 p = strchr (oldq, '`');
b4558b57 630 if (p)
9473c522 631 p++, q = strchr (p, '\'');
b4558b57 632 /* Then try "double quotes". */
9473c522
JM
633 else if (p = strchr (oldq, '"'), p)
634 p++, q = strchr (p, '"');
973a43f6
MS
635 else {
636 /* Then try entire line. */
637 q = strchr (oldq, 0);
638 if (q != oldq)
a7050d49 639 p = (char *)oldq;
973a43f6 640 }
aa32d841 641
6ff7fb95 642 if (p)
8ace9f18
AM
643 {
644 /* Don't let the strstr's below see the demangled name; we
645 might get spurious matches. */
646 p[-1] = '\0';
647
648 /* powerpc64-linux references .foo when calling function foo. */
649 if (*p == '.')
650 p++;
651 }
6ff7fb95 652
57be4e89
JM
653 /* We need to check for certain error keywords here, or we would
654 mistakenly use GNU ld's "In function `foo':" message. */
655 if (q && (strstr (oldq, "ndefined")
3f94eee6 656 || strstr (oldq, "nresolved")
eb20f668 657 || strstr (oldq, "nsatisfied")
57be4e89 658 || strstr (oldq, "ultiple")))
aa32d841 659 {
b4558b57
JM
660 *q = 0;
661 dem = demangled_hash_lookup (p, false);
662 if (dem)
663 sym = symbol_hash_lookup (dem->mangled, false);
664 else
d92b4486 665 {
3fd9d606
ZW
666 if (!strncmp (p, USER_LABEL_PREFIX,
667 strlen (USER_LABEL_PREFIX)))
668 p += strlen (USER_LABEL_PREFIX);
9c5b50b3
JM
669 sym = symbol_hash_lookup (p, false);
670 }
aa32d841 671 }
aa32d841
JL
672 }
673
674 if (sym && sym->tweaked)
7b6ffd11
KI
675 {
676 fclose (stream);
677 return 0;
678 }
aa32d841
JL
679 if (sym && !sym->tweaking)
680 {
681 if (tlink_verbose >= 2)
5e4adfba 682 fprintf (stderr, _("collect: tweaking %s in %s\n"),
e2500fed 683 sym->key, sym->file->key);
aa32d841
JL
684 sym->tweaking = 1;
685 file_push (sym->file);
686 }
d92b4486 687
aa32d841
JL
688 obstack_free (&temporary_obstack, temporary_firstobj);
689 }
690
7b6ffd11 691 fclose (stream);
aa32d841
JL
692 return (file_stack != NULL);
693}
694
eb686064
JM
695/* Entry point for tlink. Called from main in collect2.c.
696
697 Iteratively try to provide definitions for all the unresolved symbols
698 mentioned in the linker error messages.
699
700 LD_ARGV is an array of arguments for the linker.
701 OBJECT_LST is an array of object files that we may be able to recompile
702 to provide missing definitions. Currently ignored. */
703
aa32d841
JL
704void
705do_tlink (ld_argv, object_lst)
d4058195 706 char **ld_argv, **object_lst ATTRIBUTE_UNUSED;
aa32d841
JL
707{
708 int exit = tlink_execute ("ld", ld_argv, ldout);
709
710 tlink_init ();
711
712 if (exit)
713 {
714 int i = 0;
715
716 /* Until collect does a better job of figuring out which are object
717 files, assume that everything on the command line could be. */
718 if (read_repo_files (ld_argv))
719 while (exit && i++ < MAX_ITERATIONS)
720 {
721 if (tlink_verbose >= 3)
722 dump_file (ldout);
723 demangle_new_symbols ();
724 if (! scan_linker_output (ldout))
725 break;
726 if (! recompile_files ())
727 break;
728 if (tlink_verbose)
5e4adfba 729 fprintf (stderr, _("collect: relinking\n"));
aa32d841
JL
730 exit = tlink_execute ("ld", ld_argv, ldout);
731 }
732 }
733
734 dump_file (ldout);
735 unlink (ldout);
736 if (exit)
737 {
738 error ("ld returned %d exit status", exit);
739 collect_exit (exit);
740 }
741}