tlink.c 18.6 KB
Newer Older
1 2 3
/* Scan linker error messages for missing template instantiations and provide
   them.

4 5
   Copyright (C) 1995, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007, 2008,
   2009, 2010 Free Software Foundation, Inc.
6 7
   Contributed by Jason Merrill (jason@cygnus.com).

8
This file is part of GCC.
9

10 11
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
12
Software Foundation; either version 3, or (at your option) any later
13
version.
14

15 16 17 18
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
19 20

You should have received a copy of the GNU General Public License
21 22
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
23 24

#include "config.h"
25
#include "system.h"
26 27
#include "coretypes.h"
#include "tm.h"
28
#include "intl.h"
29 30
#include "obstack.h"
#include "hashtab.h"
31
#include "demangle.h"
32
#include "collect2.h"
33

34 35 36 37
/* TARGET_64BIT may be defined to use driver specific functionality. */
#undef TARGET_64BIT
#define TARGET_64BIT TARGET_64BIT_DEFAULT

38 39 40 41 42 43
#define MAX_ITERATIONS 17

/* Defined in the automatically-generated underscore.c.  */
extern int prepends_underscore;

static int tlink_verbose;
44

45
static char *initial_cwd;
46

47
/* Hash table boilerplate for working with htab_t.  We have hash tables
Jason Merrill committed
48
   for symbol names, file names, and demangled symbols.  */
49 50 51

typedef struct symbol_hash_entry
{
52
  const char *key;
53 54 55 56 57 58 59 60
  struct file_hash_entry *file;
  int chosen;
  int tweaking;
  int tweaked;
} symbol;

typedef struct file_hash_entry
{
61
  const char *key;
62 63 64 65 66 67 68 69
  const char *args;
  const char *dir;
  const char *main;
  int tweaking;
} file;

typedef struct demangled_hash_entry
{
70
  const char *key;
71 72 73
  const char *mangled;
} demangled;

74 75
/* Hash and comparison functions for these hash tables.  */

76 77
static int hash_string_eq (const void *, const void *);
static hashval_t hash_string_hash (const void *);
78 79

static int
80
hash_string_eq (const void *s1_p, const void *s2_p)
81
{
Kazu Hirata committed
82 83
  const char *const *s1 = (const char *const *) s1_p;
  const char *s2 = (const char *) s2_p;
84 85 86 87
  return strcmp (*s1, s2) == 0;
}

static hashval_t
88
hash_string_hash (const void *s_p)
89
{
Kazu Hirata committed
90
  const char *const *s = (const char *const *) s_p;
91 92 93 94
  return (*htab_hash_string) (*s);
}

static htab_t symbol_table;
95

96 97 98 99 100 101 102 103
static struct symbol_hash_entry * symbol_hash_lookup (const char *, int);
static struct file_hash_entry * file_hash_lookup (const char *);
static struct demangled_hash_entry *demangled_hash_lookup (const char *, int);
static void symbol_push (symbol *);
static symbol * symbol_pop (void);
static void file_push (file *);
static file * file_pop (void);
static void tlink_init (void);
104
static int tlink_execute (const char *, char **, const char *, const char *);
105 106 107 108 109 110 111 112 113 114 115
static char * frob_extension (const char *, const char *);
static char * obstack_fgets (FILE *, struct obstack *);
static char * tfgets (FILE *);
static char * pfgets (FILE *);
static void freadsym (FILE *, file *, int);
static void read_repo_file (file *);
static void maybe_tweak (char *, file *);
static int recompile_files (void);
static int read_repo_files (char **);
static void demangle_new_symbols (void);
static int scan_linker_output (const char *);
Kaveh R. Ghazi committed
116

Jason Merrill committed
117 118
/* Look up an entry in the symbol hash table.  */

119
static struct symbol_hash_entry *
120
symbol_hash_lookup (const char *string, int create)
121
{
122
  void **e;
123
  e = htab_find_slot_with_hash (symbol_table, string,
Kazu Hirata committed
124
				(*htab_hash_string) (string),
125 126 127 128
				create ? INSERT : NO_INSERT);
  if (e == NULL)
    return NULL;
  if (*e == NULL)
129
    {
130
      struct symbol_hash_entry *v;
131
      *e = v = XCNEW (struct symbol_hash_entry);
132
      v->key = xstrdup (string);
133
    }
134
  return (struct symbol_hash_entry *) *e;
135 136
}

137 138
static htab_t file_table;

Jason Merrill committed
139 140
/* Look up an entry in the file hash table.  */

141
static struct file_hash_entry *
142
file_hash_lookup (const char *string)
143
{
144
  void **e;
145
  e = htab_find_slot_with_hash (file_table, string,
Kazu Hirata committed
146
				(*htab_hash_string) (string),
147 148
				INSERT);
  if (*e == NULL)
149
    {
150
      struct file_hash_entry *v;
151
      *e = v = XCNEW (struct file_hash_entry);
152
      v->key = xstrdup (string);
153
    }
154
  return (struct file_hash_entry *) *e;
155 156
}

157 158
static htab_t demangled_table;

Jason Merrill committed
159 160
/* Look up an entry in the demangled name hash table.  */

161
static struct demangled_hash_entry *
162
demangled_hash_lookup (const char *string, int create)
163
{
164
  void **e;
165
  e = htab_find_slot_with_hash (demangled_table, string,
Kazu Hirata committed
166
				(*htab_hash_string) (string),
167 168 169 170 171 172
				create ? INSERT : NO_INSERT);
  if (e == NULL)
    return NULL;
  if (*e == NULL)
    {
      struct demangled_hash_entry *v;
173
      *e = v = XCNEW (struct demangled_hash_entry);
174 175
      v->key = xstrdup (string);
    }
176
  return (struct demangled_hash_entry *) *e;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
}

/* Stack code.  */

struct symbol_stack_entry
{
  symbol *value;
  struct symbol_stack_entry *next;
};
struct obstack symbol_stack_obstack;
struct symbol_stack_entry *symbol_stack;

struct file_stack_entry
{
  file *value;
  struct file_stack_entry *next;
};
struct obstack file_stack_obstack;
struct file_stack_entry *file_stack;

static void
198
symbol_push (symbol *p)
199
{
200 201
  struct symbol_stack_entry *ep
    = XOBNEW (&symbol_stack_obstack, struct symbol_stack_entry);
202 203 204 205 206 207
  ep->value = p;
  ep->next = symbol_stack;
  symbol_stack = ep;
}

static symbol *
208
symbol_pop (void)
209 210 211 212 213 214 215 216 217 218 219 220
{
  struct symbol_stack_entry *ep = symbol_stack;
  symbol *p;
  if (ep == NULL)
    return NULL;
  p = ep->value;
  symbol_stack = ep->next;
  obstack_free (&symbol_stack_obstack, ep);
  return p;
}

static void
221
file_push (file *p)
222 223 224 225 226 227
{
  struct file_stack_entry *ep;

  if (p->tweaking)
    return;

228
  ep = XOBNEW (&file_stack_obstack, struct file_stack_entry);
229 230 231 232 233 234 235
  ep->value = p;
  ep->next = file_stack;
  file_stack = ep;
  p->tweaking = 1;
}

static file *
236
file_pop (void)
237 238 239 240 241 242 243 244 245 246 247 248 249 250
{
  struct file_stack_entry *ep = file_stack;
  file *p;
  if (ep == NULL)
    return NULL;
  p = ep->value;
  file_stack = ep->next;
  obstack_free (&file_stack_obstack, ep);
  p->tweaking = 0;
  return p;
}

/* Other machinery.  */

Jason Merrill committed
251 252
/* Initialize the tlink machinery.  Called from do_tlink.  */

253
static void
254
tlink_init (void)
255
{
256
  const char *p;
257

258 259 260 261 262 263
  symbol_table = htab_create (500, hash_string_hash, hash_string_eq,
			      NULL);
  file_table = htab_create (500, hash_string_hash, hash_string_eq,
			    NULL);
  demangled_table = htab_create (500, hash_string_hash, hash_string_eq,
				 NULL);
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278
  obstack_begin (&symbol_stack_obstack, 0);
  obstack_begin (&file_stack_obstack, 0);

  p = getenv ("TLINK_VERBOSE");
  if (p)
    tlink_verbose = atoi (p);
  else
    {
      tlink_verbose = 1;
      if (vflag)
	tlink_verbose = 2;
      if (debug)
	tlink_verbose = 3;
    }
279

280
  initial_cwd = getpwd ();
281 282 283
}

static int
284 285
tlink_execute (const char *prog, char **argv, const char *outname,
	       const char *errname)
286
{
287 288
  struct pex_obj *pex;

289
  pex = collect_execute (prog, argv, outname, errname, PEX_LAST | PEX_SEARCH);
290
  return collect_wait (prog, pex);
Kazu Hirata committed
291
}
292 293

static char *
294
frob_extension (const char *s, const char *ext)
295
{
296
  const char *p = strrchr (s, '/');
297 298
  if (! p)
    p = s;
299
  p = strrchr (p, '.');
300 301 302 303
  if (! p)
    p = s + strlen (s);

  obstack_grow (&temporary_obstack, s, p - s);
304
  return (char *) obstack_copy0 (&temporary_obstack, ext, strlen (ext));
305 306 307
}

static char *
308
obstack_fgets (FILE *stream, struct obstack *ob)
309 310 311 312 313 314 315
{
  int c;
  while ((c = getc (stream)) != EOF && c != '\n')
    obstack_1grow (ob, c);
  if (obstack_object_size (ob) == 0)
    return NULL;
  obstack_1grow (ob, '\0');
316
  return XOBFINISH (ob, char *);
317 318 319
}

static char *
320
tfgets (FILE *stream)
321 322 323 324 325
{
  return obstack_fgets (stream, &temporary_obstack);
}

static char *
326
pfgets (FILE *stream)
327
{
Geoffrey Keating committed
328
  return xstrdup (tfgets (stream));
329 330 331 332
}

/* Real tlink code.  */

Jason Merrill committed
333 334
/* Subroutine of read_repo_file.  We are reading the repo file for file F,
   which is coming in on STREAM, and the symbol that comes next in STREAM
335
   is offered, chosen or provided if CHOSEN is 0, 1 or 2, respectively.
Jason Merrill committed
336 337 338

   XXX "provided" is unimplemented, both here and in the compiler.  */

339
static void
340
freadsym (FILE *stream, file *f, int chosen)
341 342 343 344
{
  symbol *sym;

  {
345
    const char *name = tfgets (stream);
346 347 348 349 350
    sym = symbol_hash_lookup (name, true);
  }

  if (sym->file == NULL)
    {
Jason Merrill committed
351 352
      /* We didn't have this symbol already, so we choose this file.  */

353 354 355 356 357 358
      symbol_push (sym);
      sym->file = f;
      sym->chosen = chosen;
    }
  else if (chosen)
    {
Jason Merrill committed
359 360
      /* We want this file; cast aside any pretender.  */

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
      if (sym->chosen && sym->file != f)
	{
	  if (sym->chosen == 1)
	    file_push (sym->file);
	  else
	    {
	      file_push (f);
	      f = sym->file;
	      chosen = sym->chosen;
	    }
	}
      sym->file = f;
      sym->chosen = chosen;
    }
}

Jason Merrill committed
377 378
/* Read in the repo file denoted by F, and record all its information.  */

379
static void
380
read_repo_file (file *f)
381 382
{
  char c;
383
  FILE *stream = fopen (f->key, "r");
384 385

  if (tlink_verbose >= 2)
386
    fprintf (stderr, _("collect: reading %s\n"), f->key);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

  while (fscanf (stream, "%c ", &c) == 1)
    {
      switch (c)
	{
	case 'A':
	  f->args = pfgets (stream);
	  break;
	case 'D':
	  f->dir = pfgets (stream);
	  break;
	case 'M':
	  f->main = pfgets (stream);
	  break;
	case 'P':
	  freadsym (stream, f, 2);
	  break;
	case 'C':
	  freadsym (stream, f, 1);
	  break;
	case 'O':
	  freadsym (stream, f, 0);
	  break;
	}
      obstack_free (&temporary_obstack, temporary_firstobj);
    }
  fclose (stream);
  if (f->args == NULL)
    f->args = getenv ("COLLECT_GCC_OPTIONS");
  if (f->dir == NULL)
    f->dir = ".";
}

Jason Merrill committed
420 421 422 423 424
/* We might want to modify LINE, which is a symbol line from file F.  We do
   this if either we saw an error message referring to the symbol in
   question, or we have already allocated the symbol to another file and
   this one wants to emit it as well.  */

425
static void
426
maybe_tweak (char *line, file *f)
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
{
  symbol *sym = symbol_hash_lookup (line + 2, false);

  if ((sym->file == f && sym->tweaking)
      || (sym->file != f && line[0] == 'C'))
    {
      sym->tweaking = 0;
      sym->tweaked = 1;

      if (line[0] == 'O')
	line[0] = 'C';
      else
	line[0] = 'O';
    }
}

Jason Merrill committed
443
/* Update the repo files for each of the object files we have adjusted and
444
   recompile.  */
Jason Merrill committed
445

446
static int
447
recompile_files (void)
448 449 450
{
  file *f;

Kean Johnston committed
451 452
  putenv (xstrdup ("COMPILER_PATH="));
  putenv (xstrdup ("LIBRARY_PATH="));
Kazu Hirata committed
453

454 455
  while ((f = file_pop ()) != NULL)
    {
456 457 458 459
      char *line;
      const char *p, *q;
      char **argv;
      struct obstack arg_stack;
460 461
      FILE *stream = fopen (f->key, "r");
      const char *const outname = frob_extension (f->key, ".rnw");
462 463 464 465 466 467 468 469 470 471 472 473 474 475
      FILE *output = fopen (outname, "w");

      while ((line = tfgets (stream)) != NULL)
	{
	  switch (line[0])
	    {
	    case 'C':
	    case 'O':
	      maybe_tweak (line, f);
	    }
	  fprintf (output, "%s\n", line);
	}
      fclose (stream);
      fclose (output);
476 477 478 479 480 481 482
      /* On Windows "rename" returns -1 and sets ERRNO to EACCESS if
	 the new file name already exists.  Therefore, we explicitly
	 remove the old file first.  */
      if (remove (f->key) == -1)
	fatal_perror ("removing .rpo file");
      if (rename (outname, f->key) == -1)
	fatal_perror ("renaming .rpo file");
483

484 485
      if (!f->args)
	{
486
	  error ("repository file '%s' does not contain command-line "
487 488 489
		 "arguments", f->key);
	  return 0;
	}
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

      /* Build a null-terminated argv array suitable for
	 tlink_execute().  Manipulate arguments on the arg_stack while
	 building argv on the temporary_obstack.  */

      obstack_init (&arg_stack);
      obstack_ptr_grow (&temporary_obstack, c_file_name);

      for (p = f->args; *p != '\0'; p = q + 1)
	{
	  /* Arguments are delimited by single-quotes.  Find the
	     opening quote.  */
	  p = strchr (p, '\'');
	  if (!p)
	    goto done;

	  /* Find the closing quote.  */
	  q = strchr (p + 1, '\'');
	  if (!q)
	    goto done;

	  obstack_grow (&arg_stack, p + 1, q - (p + 1));

	  /* Replace '\'' with '.  This is how set_collect_gcc_options
	     encodes a single-quote.  */
	  while (q[1] == '\\' && q[2] == '\'' && q[3] == '\'')
	    {
	      const char *r;

	      r = strchr (q + 4, '\'');
	      if (!r)
		goto done;

	      obstack_grow (&arg_stack, q + 3, r - (q + 3));
	      q = r;
	    }

	  obstack_1grow (&arg_stack, '\0');
	  obstack_ptr_grow (&temporary_obstack, obstack_finish (&arg_stack));
	}
    done:
      obstack_ptr_grow (&temporary_obstack, f->main);
      obstack_ptr_grow (&temporary_obstack, NULL);
533
      argv = XOBFINISH (&temporary_obstack, char **);
534 535

      if (tlink_verbose)
536
	fprintf (stderr, _("collect: recompiling %s\n"), f->main);
537

538
      if (chdir (f->dir) != 0
539
	  || tlink_execute (c_file_name, argv, NULL, NULL) != 0
540
	  || chdir (initial_cwd) != 0)
541 542 543 544
	return 0;

      read_repo_file (f);

545
      obstack_free (&arg_stack, NULL);
546 547 548 549 550
      obstack_free (&temporary_obstack, temporary_firstobj);
    }
  return 1;
}

Jason Merrill committed
551 552 553
/* The first phase of processing: determine which object files have
   .rpo files associated with them, and read in the information.  */

554
static int
555
read_repo_files (char **object_lst)
556 557 558 559 560
{
  char **object = object_lst;

  for (; *object; object++)
    {
561
      const char *p;
562 563
      file *f;

564 565 566 567 568 569
      /* Don't bother trying for ld flags.  */
      if (*object[0] == '-')
	continue;

      p = frob_extension (*object, ".rpo");

570 571 572 573 574 575 576 577 578 579 580 581 582 583
      if (! file_exists (p))
	continue;

      f = file_hash_lookup (p);

      read_repo_file (f);
    }

  if (file_stack != NULL && ! recompile_files ())
    return 0;

  return (symbol_stack != NULL);
}

Jason Merrill committed
584 585
/* Add the demangled forms of any new symbols to the hash table.  */

586
static void
587
demangle_new_symbols (void)
588 589 590 591 592 593
{
  symbol *sym;

  while ((sym = symbol_pop ()) != NULL)
    {
      demangled *dem;
594
      const char *p = cplus_demangle (sym->key, DMGL_PARAMS | DMGL_ANSI);
595 596 597 598 599

      if (! p)
	continue;

      dem = demangled_hash_lookup (p, true);
600
      dem->mangled = sym->key;
601 602 603
    }
}

Jason Merrill committed
604 605 606
/* Step through the output of the linker, in the file named FNAME, and
   adjust the settings for each symbol encountered.  */

607
static int
608
scan_linker_output (const char *fname)
609 610 611
{
  FILE *stream = fopen (fname, "r");
  char *line;
612
  int skip_next_in_line = 0;
613 614 615 616 617 618

  while ((line = tfgets (stream)) != NULL)
    {
      char *p = line, *q;
      symbol *sym;
      int end;
619 620
      int ok = 0;

621 622 623
      /* On darwin9, we might have to skip " in " lines as well.  */
      if (skip_next_in_line
	  && strstr (p, " in "))
Mike Stump committed
624
	  continue;
625
      skip_next_in_line = 0;
Kazu Hirata committed
626

Kazu Hirata committed
627
      while (*p && ISSPACE ((unsigned char) *p))
628 629 630 631 632
	++p;

      if (! *p)
	continue;

Kazu Hirata committed
633
      for (q = p; *q && ! ISSPACE ((unsigned char) *q); ++q)
634 635 636 637 638
	;

      /* Try the first word on the line.  */
      if (*p == '.')
	++p;
639 640
      if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
	p += strlen (USER_LABEL_PREFIX);
641 642 643 644 645

      end = ! *q;
      *q = 0;
      sym = symbol_hash_lookup (p, false);

646 647 648
      /* Some SVR4 linkers produce messages like
	 ld: 0711-317 ERROR: Undefined symbol: .g__t3foo1Zi
	 */
Kazu Hirata committed
649
      if (! sym && ! end && strstr (q + 1, "Undefined symbol: "))
650
	{
Kazu Hirata committed
651
	  char *p = strrchr (q + 1, ' ');
652 653 654
	  p++;
	  if (*p == '.')
	    p++;
655 656
	  if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
	    p += strlen (USER_LABEL_PREFIX);
657 658 659
	  sym = symbol_hash_lookup (p, false);
	}

660
      if (! sym && ! end)
661
	/* Try a mangled name in quotes.  */
662
	{
663
	  char *oldq = q + 1;
664 665 666
	  demangled *dem = 0;
	  q = 0;

667
	  /* On darwin9, we look for "foo" referenced from:\n\(.* in .*\n\)*  */
668 669 670 671 672
	  if (strcmp (oldq, "referenced from:") == 0)
	    {
	      /* We have to remember that we found a symbol to tweak.  */
	      ok = 1;

673 674
	      /* We actually want to start from the first word on the
		 line.  */
675 676
	      oldq = p;

677 678 679
	      /* Since the format is multiline, we have to skip
		 following lines with " in ".  */
	      skip_next_in_line = 1;
680 681
	    }

682
	  /* First try `GNU style'.  */
683
	  p = strchr (oldq, '`');
684
	  if (p)
685
	    p++, q = strchr (p, '\'');
686
	  /* Then try "double quotes".  */
687 688
	  else if (p = strchr (oldq, '"'), p)
	    p++, q = strchr (p, '"');
689 690 691
	  /* Then try 'single quotes'.  */
	  else if (p = strchr (oldq, '\''), p)
	    p++, q = strchr (p, '\'');
692 693 694 695
	  else {
	    /* Then try entire line.  */
	    q = strchr (oldq, 0);
	    if (q != oldq)
Mike Stump committed
696
	      p = (char *)oldq;
697
	  }
698

699
	  if (p)
700 701 702 703 704 705 706 707 708
	    {
	      /* Don't let the strstr's below see the demangled name; we
		 might get spurious matches.  */
	      p[-1] = '\0';

	      /* powerpc64-linux references .foo when calling function foo.  */
	      if (*p == '.')
		p++;
	    }
709

710 711
	  /* We need to check for certain error keywords here, or we would
	     mistakenly use GNU ld's "In function `foo':" message.  */
712 713
	  if (q && (ok
		    || strstr (oldq, "ndefined")
Jason Merrill committed
714
		    || strstr (oldq, "nresolved")
715
		    || strstr (oldq, "nsatisfied")
716
		    || strstr (oldq, "ultiple")))
717
	    {
718 719 720 721 722
	      *q = 0;
	      dem = demangled_hash_lookup (p, false);
	      if (dem)
		sym = symbol_hash_lookup (dem->mangled, false);
	      else
Kazu Hirata committed
723
		{
724 725 726
		  if (!strncmp (p, USER_LABEL_PREFIX,
				strlen (USER_LABEL_PREFIX)))
		    p += strlen (USER_LABEL_PREFIX);
727 728
		  sym = symbol_hash_lookup (p, false);
		}
729 730 731 732
	    }
	}

      if (sym && sym->tweaked)
733
	{
734
	  error ("'%s' was assigned to '%s', but was not defined "
H.J. Lu committed
735
		 "during recompilation, or vice versa",
736
		 sym->key, sym->file->key);
737 738 739
	  fclose (stream);
	  return 0;
	}
740 741 742
      if (sym && !sym->tweaking)
	{
	  if (tlink_verbose >= 2)
743
	    fprintf (stderr, _("collect: tweaking %s in %s\n"),
744
		     sym->key, sym->file->key);
745 746 747
	  sym->tweaking = 1;
	  file_push (sym->file);
	}
Kazu Hirata committed
748

749 750 751
      obstack_free (&temporary_obstack, temporary_firstobj);
    }

752
  fclose (stream);
753 754 755
  return (file_stack != NULL);
}

Jason Merrill committed
756 757 758 759 760 761 762 763 764
/* Entry point for tlink.  Called from main in collect2.c.

   Iteratively try to provide definitions for all the unresolved symbols
   mentioned in the linker error messages.

   LD_ARGV is an array of arguments for the linker.
   OBJECT_LST is an array of object files that we may be able to recompile
     to provide missing definitions.  Currently ignored.  */

765
void
766
do_tlink (char **ld_argv, char **object_lst ATTRIBUTE_UNUSED)
767
{
768
  int exit = tlink_execute ("ld", ld_argv, ldout, lderrout);
769 770 771 772 773 774 775 776 777 778 779 780 781

  tlink_init ();

  if (exit)
    {
      int i = 0;

      /* Until collect does a better job of figuring out which are object
	 files, assume that everything on the command line could be.  */
      if (read_repo_files (ld_argv))
	while (exit && i++ < MAX_ITERATIONS)
	  {
	    if (tlink_verbose >= 3)
782 783 784 785
	      {
		dump_file (ldout, stdout);
		dump_file (lderrout, stderr);
	      }
786
	    demangle_new_symbols ();
787 788
	    if (! scan_linker_output (ldout)
		&& ! scan_linker_output (lderrout))
789 790 791 792
	      break;
	    if (! recompile_files ())
	      break;
	    if (tlink_verbose)
793
	      fprintf (stderr, _("collect: relinking\n"));
794
	    exit = tlink_execute ("ld", ld_argv, ldout, lderrout);
795 796 797
	  }
    }

798
  dump_file (ldout, stdout);
799
  unlink (ldout);
800 801
  dump_file (lderrout, stderr);
  unlink (lderrout);
802 803 804 805 806 807
  if (exit)
    {
      error ("ld returned %d exit status", exit);
      collect_exit (exit);
    }
}