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