tlink.c 20 KB

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