]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tlink.c
Update copyright years.
[thirdparty/gcc.git] / gcc / tlink.c
CommitLineData
94ca3aab 1/* Scan linker error messages for missing template instantiations and provide
2 them.
3
f1717362 4 Copyright (C) 1995-2016 Free Software Foundation, Inc.
94ca3aab 5 Contributed by Jason Merrill (jason@cygnus.com).
6
f12b58b3 7This file is part of GCC.
94ca3aab 8
f12b58b3 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
8c4c00c1 11Software Foundation; either version 3, or (at your option) any later
f12b58b3 12version.
94ca3aab 13
f12b58b3 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.
94ca3aab 18
19You should have received a copy of the GNU General Public License
8c4c00c1 20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
94ca3aab 22
94ca3aab 23#include "config.h"
405711de 24#include "system.h"
805e22b2 25#include "coretypes.h"
26#include "tm.h"
6e6e5faa 27#include "intl.h"
1f3233d1 28#include "obstack.h"
94ca3aab 29#include "demangle.h"
1ae13971 30#include "collect2.h"
39ef03bb 31#include "collect-utils.h"
82715bcd 32#include "filenames.h"
11091b4d 33#include "diagnostic-core.h"
94ca3aab 34
a2beb429 35/* TARGET_64BIT may be defined to use driver specific functionality. */
36#undef TARGET_64BIT
37#define TARGET_64BIT TARGET_64BIT_DEFAULT
38
94ca3aab 39#define MAX_ITERATIONS 17
40
94ca3aab 41/* Defined in the automatically-generated underscore.c. */
42extern int prepends_underscore;
43
44static int tlink_verbose;
05a7239a 45
691f496e 46static char *initial_cwd;
94ca3aab 47\f
1f3233d1 48/* Hash table boilerplate for working with htab_t. We have hash tables
56e06438 49 for symbol names, file names, and demangled symbols. */
94ca3aab 50
51typedef struct symbol_hash_entry
52{
1f3233d1 53 const char *key;
94ca3aab 54 struct file_hash_entry *file;
55 int chosen;
56 int tweaking;
57 int tweaked;
58} symbol;
59
60typedef struct file_hash_entry
61{
1f3233d1 62 const char *key;
94ca3aab 63 const char *args;
64 const char *dir;
65 const char *main;
66 int tweaking;
67} file;
68
ee7ee34d 69typedef const char *str;
ee7ee34d 70
94ca3aab 71typedef struct demangled_hash_entry
72{
1f3233d1 73 const char *key;
f1f41a6c 74 vec<str> mangled;
94ca3aab 75} demangled;
76
1f3233d1 77/* Hash and comparison functions for these hash tables. */
78
60b8c5b3 79static int hash_string_eq (const void *, const void *);
80static hashval_t hash_string_hash (const void *);
1f3233d1 81
82static int
60b8c5b3 83hash_string_eq (const void *s1_p, const void *s2_p)
1f3233d1 84{
513b1ebe 85 const char *const *s1 = (const char *const *) s1_p;
86 const char *s2 = (const char *) s2_p;
1f3233d1 87 return strcmp (*s1, s2) == 0;
88}
89
90static hashval_t
60b8c5b3 91hash_string_hash (const void *s_p)
1f3233d1 92{
513b1ebe 93 const char *const *s = (const char *const *) s_p;
1f3233d1 94 return (*htab_hash_string) (*s);
95}
96
97static htab_t symbol_table;
94ca3aab 98
60b8c5b3 99static struct symbol_hash_entry * symbol_hash_lookup (const char *, int);
100static struct file_hash_entry * file_hash_lookup (const char *);
101static struct demangled_hash_entry *demangled_hash_lookup (const char *, int);
102static void symbol_push (symbol *);
103static symbol * symbol_pop (void);
104static void file_push (file *);
105static file * file_pop (void);
60b8c5b3 106static char * frob_extension (const char *, const char *);
107static char * obstack_fgets (FILE *, struct obstack *);
108static char * tfgets (FILE *);
109static char * pfgets (FILE *);
110static void freadsym (FILE *, file *, int);
111static void read_repo_file (file *);
112static void maybe_tweak (char *, file *);
113static int recompile_files (void);
114static int read_repo_files (char **);
115static void demangle_new_symbols (void);
116static int scan_linker_output (const char *);
0809af09 117
56e06438 118/* Look up an entry in the symbol hash table. */
119
94ca3aab 120static struct symbol_hash_entry *
60b8c5b3 121symbol_hash_lookup (const char *string, int create)
94ca3aab 122{
b9a7cc69 123 void **e;
1f3233d1 124 e = htab_find_slot_with_hash (symbol_table, string,
513b1ebe 125 (*htab_hash_string) (string),
1f3233d1 126 create ? INSERT : NO_INSERT);
127 if (e == NULL)
128 return NULL;
129 if (*e == NULL)
94ca3aab 130 {
1f3233d1 131 struct symbol_hash_entry *v;
4c36ffe6 132 *e = v = XCNEW (struct symbol_hash_entry);
1f3233d1 133 v->key = xstrdup (string);
94ca3aab 134 }
25a1c410 135 return (struct symbol_hash_entry *) *e;
94ca3aab 136}
137
1f3233d1 138static htab_t file_table;
139
56e06438 140/* Look up an entry in the file hash table. */
141
94ca3aab 142static struct file_hash_entry *
60b8c5b3 143file_hash_lookup (const char *string)
94ca3aab 144{
b9a7cc69 145 void **e;
1f3233d1 146 e = htab_find_slot_with_hash (file_table, string,
513b1ebe 147 (*htab_hash_string) (string),
1f3233d1 148 INSERT);
149 if (*e == NULL)
94ca3aab 150 {
1f3233d1 151 struct file_hash_entry *v;
4c36ffe6 152 *e = v = XCNEW (struct file_hash_entry);
1f3233d1 153 v->key = xstrdup (string);
94ca3aab 154 }
25a1c410 155 return (struct file_hash_entry *) *e;
94ca3aab 156}
157
1f3233d1 158static htab_t demangled_table;
159
56e06438 160/* Look up an entry in the demangled name hash table. */
161
94ca3aab 162static struct demangled_hash_entry *
60b8c5b3 163demangled_hash_lookup (const char *string, int create)
94ca3aab 164{
b9a7cc69 165 void **e;
1f3233d1 166 e = htab_find_slot_with_hash (demangled_table, string,
513b1ebe 167 (*htab_hash_string) (string),
1f3233d1 168 create ? INSERT : NO_INSERT);
169 if (e == NULL)
170 return NULL;
171 if (*e == NULL)
172 {
173 struct demangled_hash_entry *v;
4c36ffe6 174 *e = v = XCNEW (struct demangled_hash_entry);
1f3233d1 175 v->key = xstrdup (string);
176 }
25a1c410 177 return (struct demangled_hash_entry *) *e;
94ca3aab 178}
179\f
180/* Stack code. */
181
182struct symbol_stack_entry
183{
184 symbol *value;
185 struct symbol_stack_entry *next;
186};
187struct obstack symbol_stack_obstack;
188struct symbol_stack_entry *symbol_stack;
189
190struct file_stack_entry
191{
192 file *value;
193 struct file_stack_entry *next;
194};
195struct obstack file_stack_obstack;
196struct file_stack_entry *file_stack;
197
198static void
60b8c5b3 199symbol_push (symbol *p)
94ca3aab 200{
25a1c410 201 struct symbol_stack_entry *ep
202 = XOBNEW (&symbol_stack_obstack, struct symbol_stack_entry);
94ca3aab 203 ep->value = p;
204 ep->next = symbol_stack;
205 symbol_stack = ep;
206}
207
208static symbol *
60b8c5b3 209symbol_pop (void)
94ca3aab 210{
211 struct symbol_stack_entry *ep = symbol_stack;
212 symbol *p;
213 if (ep == NULL)
214 return NULL;
215 p = ep->value;
216 symbol_stack = ep->next;
217 obstack_free (&symbol_stack_obstack, ep);
218 return p;
219}
220
221static void
60b8c5b3 222file_push (file *p)
94ca3aab 223{
224 struct file_stack_entry *ep;
225
226 if (p->tweaking)
227 return;
228
25a1c410 229 ep = XOBNEW (&file_stack_obstack, struct file_stack_entry);
94ca3aab 230 ep->value = p;
231 ep->next = file_stack;
232 file_stack = ep;
233 p->tweaking = 1;
234}
235
236static file *
60b8c5b3 237file_pop (void)
94ca3aab 238{
239 struct file_stack_entry *ep = file_stack;
240 file *p;
241 if (ep == NULL)
242 return NULL;
243 p = ep->value;
244 file_stack = ep->next;
245 obstack_free (&file_stack_obstack, ep);
246 p->tweaking = 0;
247 return p;
248}
249\f
250/* Other machinery. */
251
56e06438 252/* Initialize the tlink machinery. Called from do_tlink. */
253
94ca3aab 254static void
60b8c5b3 255tlink_init (void)
94ca3aab 256{
e504db4a 257 const char *p;
94ca3aab 258
1f3233d1 259 symbol_table = htab_create (500, hash_string_hash, hash_string_eq,
260 NULL);
261 file_table = htab_create (500, hash_string_hash, hash_string_eq,
262 NULL);
263 demangled_table = htab_create (500, hash_string_hash, hash_string_eq,
264 NULL);
60b8c5b3 265
94ca3aab 266 obstack_begin (&symbol_stack_obstack, 0);
267 obstack_begin (&file_stack_obstack, 0);
268
269 p = getenv ("TLINK_VERBOSE");
270 if (p)
271 tlink_verbose = atoi (p);
272 else
273 {
274 tlink_verbose = 1;
572cae00 275 if (verbose)
94ca3aab 276 tlink_verbose = 2;
277 if (debug)
278 tlink_verbose = 3;
279 }
05a7239a 280
691f496e 281 initial_cwd = getpwd ();
94ca3aab 282}
283
284static int
d6b5203d 285tlink_execute (const char *prog, char **argv, const char *outname,
39ef03bb 286 const char *errname, bool use_atfile)
94ca3aab 287{
6e24b140 288 struct pex_obj *pex;
289
39ef03bb 290 pex = collect_execute (prog, argv, outname, errname,
291 PEX_LAST | PEX_SEARCH, use_atfile);
6e24b140 292 return collect_wait (prog, pex);
40570cc2 293}
94ca3aab 294
295static char *
60b8c5b3 296frob_extension (const char *s, const char *ext)
94ca3aab 297{
82715bcd 298 const char *p;
299
300 p = strrchr (lbasename (s), '.');
94ca3aab 301 if (! p)
302 p = s + strlen (s);
303
304 obstack_grow (&temporary_obstack, s, p - s);
25a1c410 305 return (char *) obstack_copy0 (&temporary_obstack, ext, strlen (ext));
94ca3aab 306}
307
308static char *
60b8c5b3 309obstack_fgets (FILE *stream, struct obstack *ob)
94ca3aab 310{
311 int c;
312 while ((c = getc (stream)) != EOF && c != '\n')
313 obstack_1grow (ob, c);
314 if (obstack_object_size (ob) == 0)
315 return NULL;
316 obstack_1grow (ob, '\0');
4fac984f 317 return XOBFINISH (ob, char *);
94ca3aab 318}
319
320static char *
60b8c5b3 321tfgets (FILE *stream)
94ca3aab 322{
323 return obstack_fgets (stream, &temporary_obstack);
324}
325
326static char *
60b8c5b3 327pfgets (FILE *stream)
94ca3aab 328{
92192583 329 return xstrdup (tfgets (stream));
94ca3aab 330}
331\f
332/* Real tlink code. */
333
56e06438 334/* Subroutine of read_repo_file. We are reading the repo file for file F,
335 which is coming in on STREAM, and the symbol that comes next in STREAM
de132707 336 is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
56e06438 337
338 XXX "provided" is unimplemented, both here and in the compiler. */
339
94ca3aab 340static void
60b8c5b3 341freadsym (FILE *stream, file *f, int chosen)
94ca3aab 342{
343 symbol *sym;
344
345 {
e504db4a 346 const char *name = tfgets (stream);
94ca3aab 347 sym = symbol_hash_lookup (name, true);
348 }
349
350 if (sym->file == NULL)
351 {
56e06438 352 /* We didn't have this symbol already, so we choose this file. */
353
94ca3aab 354 symbol_push (sym);
355 sym->file = f;
356 sym->chosen = chosen;
357 }
358 else if (chosen)
359 {
56e06438 360 /* We want this file; cast aside any pretender. */
361
94ca3aab 362 if (sym->chosen && sym->file != f)
363 {
364 if (sym->chosen == 1)
365 file_push (sym->file);
366 else
367 {
368 file_push (f);
369 f = sym->file;
370 chosen = sym->chosen;
371 }
372 }
373 sym->file = f;
374 sym->chosen = chosen;
375 }
376}
377
56e06438 378/* Read in the repo file denoted by F, and record all its information. */
379
94ca3aab 380static void
60b8c5b3 381read_repo_file (file *f)
94ca3aab 382{
383 char c;
1f3233d1 384 FILE *stream = fopen (f->key, "r");
94ca3aab 385
386 if (tlink_verbose >= 2)
1f3233d1 387 fprintf (stderr, _("collect: reading %s\n"), f->key);
94ca3aab 388
389 while (fscanf (stream, "%c ", &c) == 1)
390 {
391 switch (c)
392 {
393 case 'A':
394 f->args = pfgets (stream);
395 break;
396 case 'D':
397 f->dir = pfgets (stream);
398 break;
399 case 'M':
400 f->main = pfgets (stream);
401 break;
402 case 'P':
403 freadsym (stream, f, 2);
404 break;
405 case 'C':
406 freadsym (stream, f, 1);
407 break;
408 case 'O':
409 freadsym (stream, f, 0);
410 break;
411 }
412 obstack_free (&temporary_obstack, temporary_firstobj);
413 }
414 fclose (stream);
415 if (f->args == NULL)
416 f->args = getenv ("COLLECT_GCC_OPTIONS");
417 if (f->dir == NULL)
418 f->dir = ".";
419}
420
56e06438 421/* We might want to modify LINE, which is a symbol line from file F. We do
422 this if either we saw an error message referring to the symbol in
423 question, or we have already allocated the symbol to another file and
424 this one wants to emit it as well. */
425
94ca3aab 426static void
60b8c5b3 427maybe_tweak (char *line, file *f)
94ca3aab 428{
429 symbol *sym = symbol_hash_lookup (line + 2, false);
430
431 if ((sym->file == f && sym->tweaking)
432 || (sym->file != f && line[0] == 'C'))
433 {
434 sym->tweaking = 0;
435 sym->tweaked = 1;
436
437 if (line[0] == 'O')
ee7ee34d 438 {
439 line[0] = 'C';
440 sym->chosen = 1;
441 }
94ca3aab 442 else
ee7ee34d 443 {
444 line[0] = 'O';
445 sym->chosen = 0;
446 }
94ca3aab 447 }
448}
449
56e06438 450/* Update the repo files for each of the object files we have adjusted and
05a7239a 451 recompile. */
56e06438 452
94ca3aab 453static int
60b8c5b3 454recompile_files (void)
94ca3aab 455{
456 file *f;
457
ce75e2bb 458 putenv (xstrdup ("COMPILER_PATH="));
459 putenv (xstrdup ("LIBRARY_PATH="));
40570cc2 460
94ca3aab 461 while ((f = file_pop ()) != NULL)
462 {
05a7239a 463 char *line;
464 const char *p, *q;
465 char **argv;
466 struct obstack arg_stack;
1f3233d1 467 FILE *stream = fopen (f->key, "r");
468 const char *const outname = frob_extension (f->key, ".rnw");
94ca3aab 469 FILE *output = fopen (outname, "w");
470
471 while ((line = tfgets (stream)) != NULL)
472 {
473 switch (line[0])
474 {
475 case 'C':
476 case 'O':
477 maybe_tweak (line, f);
478 }
479 fprintf (output, "%s\n", line);
480 }
481 fclose (stream);
482 fclose (output);
e39224c2 483 /* On Windows "rename" returns -1 and sets ERRNO to EACCESS if
484 the new file name already exists. Therefore, we explicitly
485 remove the old file first. */
486 if (remove (f->key) == -1)
c05be867 487 fatal_error (input_location, "removing .rpo file: %m");
e39224c2 488 if (rename (outname, f->key) == -1)
c05be867 489 fatal_error (input_location, "renaming .rpo file: %m");
94ca3aab 490
caa6fdce 491 if (!f->args)
492 {
1e5fcbe2 493 error ("repository file '%s' does not contain command-line "
caa6fdce 494 "arguments", f->key);
495 return 0;
496 }
05a7239a 497
498 /* Build a null-terminated argv array suitable for
499 tlink_execute(). Manipulate arguments on the arg_stack while
500 building argv on the temporary_obstack. */
501
502 obstack_init (&arg_stack);
503 obstack_ptr_grow (&temporary_obstack, c_file_name);
504
505 for (p = f->args; *p != '\0'; p = q + 1)
506 {
507 /* Arguments are delimited by single-quotes. Find the
508 opening quote. */
509 p = strchr (p, '\'');
510 if (!p)
511 goto done;
512
513 /* Find the closing quote. */
514 q = strchr (p + 1, '\'');
515 if (!q)
516 goto done;
517
518 obstack_grow (&arg_stack, p + 1, q - (p + 1));
519
520 /* Replace '\'' with '. This is how set_collect_gcc_options
521 encodes a single-quote. */
522 while (q[1] == '\\' && q[2] == '\'' && q[3] == '\'')
523 {
524 const char *r;
525
526 r = strchr (q + 4, '\'');
527 if (!r)
528 goto done;
529
530 obstack_grow (&arg_stack, q + 3, r - (q + 3));
531 q = r;
532 }
533
534 obstack_1grow (&arg_stack, '\0');
535 obstack_ptr_grow (&temporary_obstack, obstack_finish (&arg_stack));
536 }
537 done:
538 obstack_ptr_grow (&temporary_obstack, f->main);
539 obstack_ptr_grow (&temporary_obstack, NULL);
4fac984f 540 argv = XOBFINISH (&temporary_obstack, char **);
94ca3aab 541
542 if (tlink_verbose)
a4db0a1d 543 fprintf (stderr, _("collect: recompiling %s\n"), f->main);
94ca3aab 544
05a7239a 545 if (chdir (f->dir) != 0
39ef03bb 546 || tlink_execute (c_file_name, argv, NULL, NULL, false) != 0
05a7239a 547 || chdir (initial_cwd) != 0)
94ca3aab 548 return 0;
549
550 read_repo_file (f);
551
05a7239a 552 obstack_free (&arg_stack, NULL);
94ca3aab 553 obstack_free (&temporary_obstack, temporary_firstobj);
554 }
555 return 1;
556}
557
56e06438 558/* The first phase of processing: determine which object files have
559 .rpo files associated with them, and read in the information. */
560
94ca3aab 561static int
60b8c5b3 562read_repo_files (char **object_lst)
94ca3aab 563{
564 char **object = object_lst;
565
566 for (; *object; object++)
567 {
d823001a 568 const char *p;
94ca3aab 569 file *f;
570
d823001a 571 /* Don't bother trying for ld flags. */
572 if (*object[0] == '-')
573 continue;
574
575 p = frob_extension (*object, ".rpo");
576
94ca3aab 577 if (! file_exists (p))
578 continue;
579
580 f = file_hash_lookup (p);
581
582 read_repo_file (f);
583 }
584
585 if (file_stack != NULL && ! recompile_files ())
586 return 0;
587
588 return (symbol_stack != NULL);
589}
590
56e06438 591/* Add the demangled forms of any new symbols to the hash table. */
592
94ca3aab 593static void
60b8c5b3 594demangle_new_symbols (void)
94ca3aab 595{
596 symbol *sym;
597
598 while ((sym = symbol_pop ()) != NULL)
599 {
600 demangled *dem;
1f3233d1 601 const char *p = cplus_demangle (sym->key, DMGL_PARAMS | DMGL_ANSI);
94ca3aab 602
603 if (! p)
604 continue;
605
606 dem = demangled_hash_lookup (p, true);
f1f41a6c 607 dem->mangled.safe_push (sym->key);
ee7ee34d 608 }
609}
610
611/* We want to tweak symbol SYM. Return true if all is well, false on
612 error. */
613
614static bool
615start_tweaking (symbol *sym)
616{
617 if (sym && sym->tweaked)
618 {
619 error ("'%s' was assigned to '%s', but was not defined "
620 "during recompilation, or vice versa",
621 sym->key, sym->file->key);
622 return 0;
623 }
624 if (sym && !sym->tweaking)
625 {
626 if (tlink_verbose >= 2)
627 fprintf (stderr, _("collect: tweaking %s in %s\n"),
628 sym->key, sym->file->key);
629 sym->tweaking = 1;
630 file_push (sym->file);
94ca3aab 631 }
ee7ee34d 632 return true;
94ca3aab 633}
634
56e06438 635/* Step through the output of the linker, in the file named FNAME, and
636 adjust the settings for each symbol encountered. */
637
94ca3aab 638static int
60b8c5b3 639scan_linker_output (const char *fname)
94ca3aab 640{
641 FILE *stream = fopen (fname, "r");
642 char *line;
d11c2189 643 int skip_next_in_line = 0;
94ca3aab 644
645 while ((line = tfgets (stream)) != NULL)
646 {
647 char *p = line, *q;
648 symbol *sym;
ee7ee34d 649 demangled *dem = 0;
94ca3aab 650 int end;
f1148292 651 int ok = 0;
ee7ee34d 652 unsigned ix;
653 str s;
f1148292 654
d11c2189 655 /* On darwin9, we might have to skip " in " lines as well. */
656 if (skip_next_in_line
657 && strstr (p, " in "))
cd79e6c7 658 continue;
d11c2189 659 skip_next_in_line = 0;
40570cc2 660
513b1ebe 661 while (*p && ISSPACE ((unsigned char) *p))
94ca3aab 662 ++p;
663
664 if (! *p)
665 continue;
666
513b1ebe 667 for (q = p; *q && ! ISSPACE ((unsigned char) *q); ++q)
94ca3aab 668 ;
669
670 /* Try the first word on the line. */
671 if (*p == '.')
672 ++p;
65e49ff1 673 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
674 p += strlen (USER_LABEL_PREFIX);
94ca3aab 675
676 end = ! *q;
677 *q = 0;
678 sym = symbol_hash_lookup (p, false);
679
7234ad38 680 /* Some SVR4 linkers produce messages like
681 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
682 */
513b1ebe 683 if (! sym && ! end && strstr (q + 1, "Undefined symbol: "))
7234ad38 684 {
513b1ebe 685 char *p = strrchr (q + 1, ' ');
7234ad38 686 p++;
687 if (*p == '.')
688 p++;
65e49ff1 689 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
690 p += strlen (USER_LABEL_PREFIX);
7234ad38 691 sym = symbol_hash_lookup (p, false);
692 }
693
94ca3aab 694 if (! sym && ! end)
bd855dab 695 /* Try a mangled name in quotes. */
94ca3aab 696 {
dea3189b 697 char *oldq = q + 1;
94ca3aab 698 q = 0;
699
d11c2189 700 /* On darwin9, we look for "foo" referenced from:\n\(.* in .*\n\)* */
f1148292 701 if (strcmp (oldq, "referenced from:") == 0)
702 {
703 /* We have to remember that we found a symbol to tweak. */
704 ok = 1;
705
d11c2189 706 /* We actually want to start from the first word on the
707 line. */
f1148292 708 oldq = p;
709
d11c2189 710 /* Since the format is multiline, we have to skip
711 following lines with " in ". */
712 skip_next_in_line = 1;
f1148292 713 }
714
bd855dab 715 /* First try `GNU style'. */
78dbff7c 716 p = strchr (oldq, '`');
bd855dab 717 if (p)
78dbff7c 718 p++, q = strchr (p, '\'');
bd855dab 719 /* Then try "double quotes". */
78dbff7c 720 else if (p = strchr (oldq, '"'), p)
721 p++, q = strchr (p, '"');
168de4c5 722 /* Then try 'single quotes'. */
723 else if (p = strchr (oldq, '\''), p)
724 p++, q = strchr (p, '\'');
9511f117 725 else {
726 /* Then try entire line. */
727 q = strchr (oldq, 0);
728 if (q != oldq)
59fbfc4a 729 p = (char *)oldq;
9511f117 730 }
94ca3aab 731
53bdb86c 732 if (p)
0beb646c 733 {
734 /* Don't let the strstr's below see the demangled name; we
735 might get spurious matches. */
736 p[-1] = '\0';
737
738 /* powerpc64-linux references .foo when calling function foo. */
739 if (*p == '.')
740 p++;
741 }
53bdb86c 742
b51e1cf8 743 /* We need to check for certain error keywords here, or we would
744 mistakenly use GNU ld's "In function `foo':" message. */
f1148292 745 if (q && (ok
746 || strstr (oldq, "ndefined")
13d09b16 747 || strstr (oldq, "nresolved")
036a9f3c 748 || strstr (oldq, "nsatisfied")
b51e1cf8 749 || strstr (oldq, "ultiple")))
94ca3aab 750 {
bd855dab 751 *q = 0;
752 dem = demangled_hash_lookup (p, false);
ee7ee34d 753 if (!dem)
40570cc2 754 {
65e49ff1 755 if (!strncmp (p, USER_LABEL_PREFIX,
756 strlen (USER_LABEL_PREFIX)))
757 p += strlen (USER_LABEL_PREFIX);
01e2144f 758 sym = symbol_hash_lookup (p, false);
759 }
94ca3aab 760 }
94ca3aab 761 }
762
ee7ee34d 763 if (dem)
30c1e454 764 {
ee7ee34d 765 /* We found a demangled name. If this is the name of a
766 constructor or destructor, there can be several mangled names
767 that match it, so choose or unchoose all of them. If some are
768 chosen and some not, leave the later ones that don't match
75de4aa2 769 alone for now; either this will cause the link to succeed, or
ee7ee34d 770 on the next attempt we will switch all of them the other way
771 and that will cause it to succeed. */
772 int chosen = 0;
f1f41a6c 773 int len = dem->mangled.length ();
ee7ee34d 774 ok = true;
f1f41a6c 775 FOR_EACH_VEC_ELT (dem->mangled, ix, s)
ee7ee34d 776 {
777 sym = symbol_hash_lookup (s, false);
778 if (ix == 0)
779 chosen = sym->chosen;
780 else if (sym->chosen != chosen)
781 /* Mismatch. */
782 continue;
783 /* Avoid an error about re-tweaking when we guess wrong in
784 the case of mismatch. */
785 if (len > 1)
786 sym->tweaked = false;
787 ok = start_tweaking (sym);
788 }
94ca3aab 789 }
ee7ee34d 790 else
791 ok = start_tweaking (sym);
40570cc2 792
94ca3aab 793 obstack_free (&temporary_obstack, temporary_firstobj);
ee7ee34d 794
795 if (!ok)
796 {
797 fclose (stream);
798 return 0;
799 }
94ca3aab 800 }
801
30c1e454 802 fclose (stream);
94ca3aab 803 return (file_stack != NULL);
804}
805
56e06438 806/* Entry point for tlink. Called from main in collect2.c.
807
808 Iteratively try to provide definitions for all the unresolved symbols
809 mentioned in the linker error messages.
810
811 LD_ARGV is an array of arguments for the linker.
812 OBJECT_LST is an array of object files that we may be able to recompile
813 to provide missing definitions. Currently ignored. */
814
94ca3aab 815void
60b8c5b3 816do_tlink (char **ld_argv, char **object_lst ATTRIBUTE_UNUSED)
94ca3aab 817{
39ef03bb 818 int ret = tlink_execute ("ld", ld_argv, ldout, lderrout,
819 HAVE_GNU_LD && at_file_supplied);
94ca3aab 820
821 tlink_init ();
822
908e0747 823 if (ret)
94ca3aab 824 {
825 int i = 0;
826
827 /* Until collect does a better job of figuring out which are object
828 files, assume that everything on the command line could be. */
829 if (read_repo_files (ld_argv))
908e0747 830 while (ret && i++ < MAX_ITERATIONS)
94ca3aab 831 {
832 if (tlink_verbose >= 3)
d6b5203d 833 {
b5369b7d 834 dump_ld_file (ldout, stdout);
835 dump_ld_file (lderrout, stderr);
d6b5203d 836 }
94ca3aab 837 demangle_new_symbols ();
d6b5203d 838 if (! scan_linker_output (ldout)
839 && ! scan_linker_output (lderrout))
94ca3aab 840 break;
841 if (! recompile_files ())
842 break;
843 if (tlink_verbose)
a4db0a1d 844 fprintf (stderr, _("collect: relinking\n"));
39ef03bb 845 ret = tlink_execute ("ld", ld_argv, ldout, lderrout,
846 HAVE_GNU_LD && at_file_supplied);
94ca3aab 847 }
848 }
849
b5369b7d 850 dump_ld_file (ldout, stdout);
94ca3aab 851 unlink (ldout);
b5369b7d 852 dump_ld_file (lderrout, stderr);
d6b5203d 853 unlink (lderrout);
908e0747 854 if (ret)
94ca3aab 855 {
908e0747 856 error ("ld returned %d exit status", ret);
857 exit (ret);
94ca3aab 858 }
2b98e22b 859 else
860 {
861 /* We have just successfully produced an output file, so assume that we
862 may unlink it if need be for now on. */
863 may_unlink_output_file = true;
864 }
94ca3aab 865}