84795e1f68c192cb6cf79008d004b1683d08ad79
[external/binutils.git] / ld / ldfile.c
1 /* Linker file opening and searching.
2    Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 /* ldfile.c:  look after all the file stuff.  */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "bfdlink.h"
27 #include "safe-ctype.h"
28 #include "ld.h"
29 #include "ldmisc.h"
30 #include "ldexp.h"
31 #include "ldlang.h"
32 #include "ldfile.h"
33 #include "ldmain.h"
34 #include <ldgram.h>
35 #include "ldlex.h"
36 #include "ldemul.h"
37 #include "libiberty.h"
38
39 const char *ldfile_input_filename;
40 bfd_boolean ldfile_assumed_script = FALSE;
41 const char *ldfile_output_machine_name = "";
42 unsigned long ldfile_output_machine;
43 enum bfd_architecture ldfile_output_architecture;
44 search_dirs_type *search_head;
45
46 #ifndef MPW
47 #ifdef VMS
48 char *slash = "";
49 #else
50 #if defined (_WIN32) && ! defined (__CYGWIN32__)
51 char *slash = "\\";
52 #else
53 char *slash = "/";
54 #endif
55 #endif
56 #else /* MPW */
57 /* The MPW path char is a colon.  */
58 char *slash = ":";
59 #endif /* MPW */
60
61 /* LOCAL */
62
63 static search_dirs_type **search_tail_ptr = &search_head;
64
65 typedef struct search_arch {
66   char *name;
67   struct search_arch *next;
68 } search_arch_type;
69
70 static search_arch_type *search_arch_head;
71 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
72
73 static FILE *try_open PARAMS ((const char *name, const char *exten));
74
75 void
76 ldfile_add_library_path (name, cmdline)
77      const char *name;
78      bfd_boolean cmdline;
79 {
80   search_dirs_type *new;
81
82   if (!cmdline && config.only_cmd_line_lib_dirs)
83     return;
84
85   new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
86   new->next = NULL;
87   new->name = name;
88   new->cmdline = cmdline;
89   *search_tail_ptr = new;
90   search_tail_ptr = &new->next;
91
92   /* If a directory is marked as honoring sysroot, prepend the sysroot path
93      now.  */
94   if (new->name[0] == '=')
95     {
96       new->name = concat (ld_sysroot, &new->name[1], NULL);
97       new->sysrooted = TRUE;
98     }
99   else
100     new->sysrooted = FALSE;
101 }
102
103 /* Try to open a BFD for a lang_input_statement.  */
104
105 bfd_boolean
106 ldfile_try_open_bfd (attempt, entry)
107      const char *attempt;
108      lang_input_statement_type *entry;
109 {
110   entry->the_bfd = bfd_openr (attempt, entry->target);
111
112   if (trace_file_tries)
113     {
114       if (entry->the_bfd == NULL)
115         info_msg (_("attempt to open %s failed\n"), attempt);
116       else
117         info_msg (_("attempt to open %s succeeded\n"), attempt);
118     }
119
120   if (entry->the_bfd == NULL)
121     {
122       if (bfd_get_error () == bfd_error_invalid_target)
123         einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
124       return FALSE;
125     }
126
127   /* If we are searching for this file, see if the architecture is
128      compatible with the output file.  If it isn't, keep searching.
129      If we can't open the file as an object file, stop the search
130      here.  */
131
132   if (entry->search_dirs_flag)
133     {
134       bfd *check;
135
136       if (bfd_check_format (entry->the_bfd, bfd_archive))
137         check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
138       else
139         check = entry->the_bfd;
140
141       if (check != NULL)
142         {
143           if (! bfd_check_format (check, bfd_object))
144             {
145               if (check == entry->the_bfd
146                   && bfd_get_error () == bfd_error_file_not_recognized
147                   && ! ldemul_unrecognized_file (entry))
148                 {
149                   int token, skip = 0;
150                   char *arg, *arg1, *arg2, *arg3;
151                   extern FILE *yyin;
152
153                   /* Try to interpret the file as a linker script.  */
154                   ldfile_open_command_file (attempt);
155
156                   ldfile_assumed_script = TRUE;
157                   parser_input = input_selected;
158                   ldlex_both ();
159                   token = INPUT_SCRIPT;
160                   while (token != 0)
161                     {
162                       switch (token)
163                         {
164                         case OUTPUT_FORMAT:
165                           if ((token = yylex ()) != '(')
166                             continue;
167                           if ((token = yylex ()) != NAME)
168                             continue;
169                           arg1 = yylval.name;
170                           arg2 = NULL;
171                           arg3 = NULL;
172                           token = yylex ();
173                           if (token == ',')
174                             {
175                               if ((token = yylex ()) != NAME)
176                                 {
177                                   free (arg1);
178                                   continue;
179                                 }
180                               arg2 = yylval.name;
181                               if ((token = yylex ()) != ','
182                                   || (token = yylex ()) != NAME)
183                                 {
184                                   free (arg1);
185                                   free (arg2);
186                                   continue;
187                                 }
188                               arg3 = yylval.name;
189                               token = yylex ();
190                             }
191                           if (token == ')')
192                             {
193                               switch (command_line.endian)
194                                 {
195                                 default:
196                                 case ENDIAN_UNSET:
197                                   arg = arg1; break;
198                                 case ENDIAN_BIG:
199                                   arg = arg2 ? arg2 : arg1; break;
200                                 case ENDIAN_LITTLE:
201                                   arg = arg3 ? arg3 : arg1; break;
202                                 }
203                               if (strcmp (arg, lang_get_output_target ()) != 0)
204                                 skip = 1;
205                             }
206                           free (arg1);
207                           if (arg2) free (arg2);
208                           if (arg3) free (arg3);
209                           break;
210                         case NAME:
211                         case LNAME:
212                         case VERS_IDENTIFIER:
213                         case VERS_TAG:
214                           free (yylval.name);
215                           break;
216                         case INT:
217                           if (yylval.bigint.str)
218                             free (yylval.bigint.str);
219                           break;
220                         }
221                       token = yylex ();
222                     }
223                   ldfile_assumed_script = FALSE;
224                   fclose (yyin);
225                   yyin = NULL;
226                   if (skip)
227                     {
228                       einfo (_("%P: skipping incompatible %s when searching for %s\n"),
229                              attempt, entry->local_sym_name);
230                       bfd_close (entry->the_bfd);
231                       entry->the_bfd = NULL;
232                       return FALSE;
233                     }
234                 }
235               return TRUE;
236             }
237
238           if ((bfd_arch_get_compatible (check, output_bfd,
239                                         command_line.accept_unknown_input_arch) == NULL)
240               /* XCOFF archives can have 32 and 64 bit objects.  */
241               && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
242                     && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
243                     && bfd_check_format (entry->the_bfd, bfd_archive)))
244             {
245               einfo (_("%P: skipping incompatible %s when searching for %s\n"),
246                      attempt, entry->local_sym_name);
247               bfd_close (entry->the_bfd);
248               entry->the_bfd = NULL;
249               return FALSE;
250             }
251         }
252     }
253
254   return TRUE;
255 }
256
257 /* Search for and open the file specified by ENTRY.  If it is an
258    archive, use ARCH, LIB and SUFFIX to modify the file name.  */
259
260 bfd_boolean
261 ldfile_open_file_search (arch, entry, lib, suffix)
262      const char *arch;
263      lang_input_statement_type *entry;
264      const char *lib;
265      const char *suffix;
266 {
267   search_dirs_type *search;
268
269   /* If this is not an archive, try to open it in the current
270      directory first.  */
271   if (! entry->is_archive)
272     {
273       if (entry->sysrooted && entry->filename[0] == '/')
274         {
275           char *name = concat (ld_sysroot, entry->filename,
276                                (const char *) NULL);
277           if (ldfile_try_open_bfd (name, entry))
278             {
279               entry->filename = name;
280               return TRUE;
281             }
282           free (name);
283         }
284       else if (ldfile_try_open_bfd (entry->filename, entry))
285         {
286           entry->sysrooted = FALSE;
287           return TRUE;
288         }
289     }
290
291   for (search = search_head;
292        search != (search_dirs_type *) NULL;
293        search = search->next)
294     {
295       char *string;
296
297       if (entry->dynamic && ! link_info.relocateable)
298         {
299           if (ldemul_open_dynamic_archive (arch, search, entry))
300             {
301               entry->sysrooted = search->sysrooted;
302               return TRUE;
303             }
304         }
305
306       string = (char *) xmalloc (strlen (search->name)
307                                  + strlen (slash)
308                                  + strlen (lib)
309                                  + strlen (entry->filename)
310                                  + strlen (arch)
311                                  + strlen (suffix)
312                                  + 1);
313
314       if (entry->is_archive)
315         sprintf (string, "%s%s%s%s%s%s", search->name, slash,
316                  lib, entry->filename, arch, suffix);
317       else if (entry->filename[0] == '/' || entry->filename[0] == '.'
318 #if defined (__MSDOS__) || defined (_WIN32)
319                || entry->filename[0] == '\\'
320                || (ISALPHA (entry->filename[0])
321                    && entry->filename[1] == ':')
322 #endif
323           )
324         strcpy (string, entry->filename);
325       else
326         sprintf (string, "%s%s%s", search->name, slash, entry->filename);
327
328       if (ldfile_try_open_bfd (string, entry))
329         {
330           entry->filename = string;
331           entry->sysrooted = search->sysrooted;
332           return TRUE;
333         }
334
335       free (string);
336     }
337
338   return FALSE;
339 }
340
341 /* Open the input file specified by ENTRY.  */
342
343 void
344 ldfile_open_file (entry)
345      lang_input_statement_type *entry;
346 {
347   if (entry->the_bfd != NULL)
348     return;
349
350   if (! entry->search_dirs_flag)
351     {
352       if (ldfile_try_open_bfd (entry->filename, entry))
353         return;
354       if (strcmp (entry->filename, entry->local_sym_name) != 0)
355         einfo (_("%F%P: cannot open %s for %s: %E\n"),
356                entry->filename, entry->local_sym_name);
357       else
358         einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
359     }
360   else
361     {
362       search_arch_type *arch;
363       bfd_boolean found = FALSE;
364
365       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
366       for (arch = search_arch_head;
367            arch != (search_arch_type *) NULL;
368            arch = arch->next)
369         {
370           found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
371           if (found)
372             break;
373 #ifdef VMS
374           found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
375           if (found)
376             break;
377 #endif
378           found = ldemul_find_potential_libraries (arch->name, entry);
379           if (found)
380             break;
381         }
382
383       /* If we have found the file, we don't need to search directories
384          again.  */
385       if (found)
386         entry->search_dirs_flag = FALSE;
387       else
388         einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
389     }
390 }
391
392 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
393
394 static FILE *
395 try_open (name, exten)
396      const char *name;
397      const char *exten;
398 {
399   FILE *result;
400   char buff[1000];
401
402   result = fopen (name, "r");
403
404   if (trace_file_tries)
405     {
406       if (result == NULL)
407         info_msg (_("cannot find script file %s\n"), name);
408       else
409         info_msg (_("opened script file %s\n"), name);
410     }
411
412   if (result != NULL)
413     return result;
414
415   if (*exten)
416     {
417       sprintf (buff, "%s%s", name, exten);
418       result = fopen (buff, "r");
419
420       if (trace_file_tries)
421         {
422           if (result == NULL)
423             info_msg (_("cannot find script file %s\n"), buff);
424           else
425             info_msg (_("opened script file %s\n"), buff);
426         }
427     }
428
429   return result;
430 }
431
432 /* Try to open NAME; if that fails, look for it in any directories
433    specified with -L, without and with EXTEND appended.  */
434
435 FILE *
436 ldfile_find_command_file (name, extend)
437      const char *name;
438      const char *extend;
439 {
440   search_dirs_type *search;
441   FILE *result;
442   char buffer[1000];
443
444   /* First try raw name.  */
445   result = try_open (name, "");
446   if (result == (FILE *) NULL)
447     {
448       /* Try now prefixes.  */
449       for (search = search_head;
450            search != (search_dirs_type *) NULL;
451            search = search->next)
452         {
453           sprintf (buffer, "%s%s%s", search->name, slash, name);
454
455           result = try_open (buffer, extend);
456           if (result)
457             break;
458         }
459     }
460
461   return result;
462 }
463
464 void
465 ldfile_open_command_file (name)
466      const char *name;
467 {
468   FILE *ldlex_input_stack;
469   ldlex_input_stack = ldfile_find_command_file (name, "");
470
471   if (ldlex_input_stack == (FILE *) NULL)
472     {
473       bfd_set_error (bfd_error_system_call);
474       einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
475     }
476
477   lex_push_file (ldlex_input_stack, name);
478
479   ldfile_input_filename = name;
480   lineno = 1;
481
482   saved_script_handle = ldlex_input_stack;
483 }
484
485 #ifdef GNU960
486 static char *
487 gnu960_map_archname (name)
488      char *name;
489 {
490   struct tabentry { char *cmd_switch; char *arch; };
491   static struct tabentry arch_tab[] =
492   {
493         "",   "",
494         "KA", "ka",
495         "KB", "kb",
496         "KC", "mc",     /* Synonym for MC */
497         "MC", "mc",
498         "CA", "ca",
499         "SA", "ka",     /* Functionally equivalent to KA */
500         "SB", "kb",     /* Functionally equivalent to KB */
501         NULL, ""
502   };
503   struct tabentry *tp;
504
505   for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
506     {
507       if (! strcmp (name,tp->cmd_switch))
508         break;
509     }
510
511   if (tp->cmd_switch == NULL)
512     einfo (_("%P%F: unknown architecture: %s\n"), name);
513
514   return tp->arch;
515 }
516
517 void
518 ldfile_add_arch (name)
519      char *name;
520 {
521   search_arch_type *new =
522     (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
523
524   if (*name != '\0')
525     {
526       if (ldfile_output_machine_name[0] != '\0')
527         {
528           einfo (_("%P%F: target architecture respecified\n"));
529           return;
530         }
531
532       ldfile_output_machine_name = name;
533     }
534
535   new->next = (search_arch_type *) NULL;
536   new->name = gnu960_map_archname (name);
537   *search_arch_tail_ptr = new;
538   search_arch_tail_ptr = &new->next;
539 }
540
541 #else /* not GNU960 */
542
543 void
544 ldfile_add_arch (in_name)
545      const char *in_name;
546 {
547   char *name = xstrdup (in_name);
548   search_arch_type *new =
549     (search_arch_type *) xmalloc (sizeof (search_arch_type));
550
551   ldfile_output_machine_name = in_name;
552
553   new->name = name;
554   new->next = (search_arch_type *) NULL;
555   while (*name)
556     {
557       *name = TOLOWER (*name);
558       name++;
559     }
560   *search_arch_tail_ptr = new;
561   search_arch_tail_ptr = &new->next;
562
563 }
564 #endif
565
566 /* Set the output architecture.  */
567
568 void
569 ldfile_set_output_arch (string)
570      const char *string;
571 {
572   const bfd_arch_info_type *arch = bfd_scan_arch (string);
573
574   if (arch)
575     {
576       ldfile_output_architecture = arch->arch;
577       ldfile_output_machine = arch->mach;
578       ldfile_output_machine_name = arch->printable_name;
579     }
580   else
581     {
582       einfo (_("%P%F: cannot represent machine `%s'\n"), string);
583     }
584 }