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