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