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