1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
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)
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.
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
22 /* ldfile.c: look after all the file stuff. */
27 #include "safe-ctype.h"
37 #include "libiberty.h"
38 #include "filenames.h"
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
;
51 #if defined (_WIN32) && ! defined (__CYGWIN32__)
58 /* The MPW path char is a colon. */
62 typedef struct search_arch
65 struct search_arch
*next
;
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
;
73 PARAMS ((const char *, const char *));
74 static bfd_boolean is_sysrooted_pathname
75 PARAMS ((const char *, bfd_boolean
));
77 /* Test whether a pathname, after canonicalization, is the same or a
78 sub-directory of the sysroot directory. */
81 is_sysrooted_pathname (name
, notsame
)
85 char * realname
= ld_canon_sysroot
? lrealpath (name
) : NULL
;
92 len
= strlen (realname
);
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)
109 /* Adds NAME to the library search path.
110 Makes a copy of NAME using xmalloc(). */
113 ldfile_add_library_path (name
, cmdline
)
117 search_dirs_type
*new;
120 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
123 new = (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
125 new->cmdline
= cmdline
;
126 *search_tail_ptr
= new;
127 search_tail_ptr
= &new->next
;
129 /* If a directory is marked as honoring sysroot, prepend the sysroot path
133 new->name
= concat (ld_sysroot
, name
+ 1, NULL
);
134 new->sysrooted
= TRUE
;
138 new->name
= xstrdup (name
);
139 new->sysrooted
= is_sysrooted_pathname (name
, FALSE
);
143 /* Try to open a BFD for a lang_input_statement. */
146 ldfile_try_open_bfd (attempt
, entry
)
148 lang_input_statement_type
*entry
;
150 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
152 if (trace_file_tries
)
154 if (entry
->the_bfd
== NULL
)
155 info_msg (_("attempt to open %s failed\n"), attempt
);
157 info_msg (_("attempt to open %s succeeded\n"), attempt
);
160 if (entry
->the_bfd
== NULL
)
162 if (bfd_get_error () == bfd_error_invalid_target
)
163 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
167 /* If we are searching for this file, see if the architecture is
168 compatible with the output file. If it isn't, keep searching.
169 If we can't open the file as an object file, stop the search
172 if (entry
->search_dirs_flag
)
176 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
177 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
179 check
= entry
->the_bfd
;
183 if (! bfd_check_format (check
, bfd_object
))
185 if (check
== entry
->the_bfd
186 && bfd_get_error () == bfd_error_file_not_recognized
187 && ! ldemul_unrecognized_file (entry
))
190 char *arg
, *arg1
, *arg2
, *arg3
;
193 /* Try to interpret the file as a linker script. */
194 ldfile_open_command_file (attempt
);
196 ldfile_assumed_script
= TRUE
;
197 parser_input
= input_selected
;
199 token
= INPUT_SCRIPT
;
205 if ((token
= yylex ()) != '(')
207 if ((token
= yylex ()) != NAME
)
215 if ((token
= yylex ()) != NAME
)
221 if ((token
= yylex ()) != ','
222 || (token
= yylex ()) != NAME
)
233 switch (command_line
.endian
)
239 arg
= arg2
? arg2
: arg1
; break;
241 arg
= arg3
? arg3
: arg1
; break;
243 if (strcmp (arg
, lang_get_output_target ()) != 0)
247 if (arg2
) free (arg2
);
248 if (arg3
) free (arg3
);
252 case VERS_IDENTIFIER
:
257 if (yylval
.bigint
.str
)
258 free (yylval
.bigint
.str
);
264 ldfile_assumed_script
= FALSE
;
269 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
270 attempt
, entry
->local_sym_name
);
271 bfd_close (entry
->the_bfd
);
272 entry
->the_bfd
= NULL
;
279 if ((bfd_arch_get_compatible (check
, output_bfd
,
280 command_line
.accept_unknown_input_arch
) == NULL
)
281 /* XCOFF archives can have 32 and 64 bit objects. */
282 && ! (bfd_get_flavour (check
) == bfd_target_xcoff_flavour
283 && bfd_get_flavour (output_bfd
) == bfd_target_xcoff_flavour
284 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
286 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
287 attempt
, entry
->local_sym_name
);
288 bfd_close (entry
->the_bfd
);
289 entry
->the_bfd
= NULL
;
298 /* Search for and open the file specified by ENTRY. If it is an
299 archive, use ARCH, LIB and SUFFIX to modify the file name. */
302 ldfile_open_file_search (arch
, entry
, lib
, suffix
)
304 lang_input_statement_type
*entry
;
308 search_dirs_type
*search
;
310 /* If this is not an archive, try to open it in the current
312 if (! entry
->is_archive
)
314 if (entry
->sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
316 char *name
= concat (ld_sysroot
, entry
->filename
,
317 (const char *) NULL
);
318 if (ldfile_try_open_bfd (name
, entry
))
320 entry
->filename
= name
;
325 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
327 entry
->sysrooted
= IS_ABSOLUTE_PATH (entry
->filename
)
328 && is_sysrooted_pathname (entry
->filename
, TRUE
);
332 if (IS_ABSOLUTE_PATH (entry
->filename
))
336 for (search
= search_head
;
337 search
!= (search_dirs_type
*) NULL
;
338 search
= search
->next
)
342 if (entry
->dynamic
&& ! link_info
.relocateable
)
344 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
346 entry
->sysrooted
= search
->sysrooted
;
351 string
= (char *) xmalloc (strlen (search
->name
)
354 + strlen (entry
->filename
)
359 if (entry
->is_archive
)
360 sprintf (string
, "%s%s%s%s%s%s", search
->name
, slash
,
361 lib
, entry
->filename
, arch
, suffix
);
363 sprintf (string
, "%s%s%s", search
->name
, slash
, entry
->filename
);
365 if (ldfile_try_open_bfd (string
, entry
))
367 entry
->filename
= string
;
368 entry
->sysrooted
= search
->sysrooted
;
378 /* Open the input file specified by ENTRY. */
381 ldfile_open_file (entry
)
382 lang_input_statement_type
*entry
;
384 if (entry
->the_bfd
!= NULL
)
387 if (! entry
->search_dirs_flag
)
389 if (ldfile_try_open_bfd (entry
->filename
, entry
))
391 if (strcmp (entry
->filename
, entry
->local_sym_name
) != 0)
392 einfo (_("%F%P: cannot open %s for %s: %E\n"),
393 entry
->filename
, entry
->local_sym_name
);
395 einfo (_("%F%P: cannot open %s: %E\n"), entry
->local_sym_name
);
399 search_arch_type
*arch
;
400 bfd_boolean found
= FALSE
;
402 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
403 for (arch
= search_arch_head
;
404 arch
!= (search_arch_type
*) NULL
;
407 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
411 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
415 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
420 /* If we have found the file, we don't need to search directories
423 entry
->search_dirs_flag
= FALSE
;
424 else if (entry
->sysrooted
426 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
427 einfo (_("%F%P: cannot find %s inside %s\n"),
428 entry
->local_sym_name
, ld_sysroot
);
430 einfo (_("%F%P: cannot find %s\n"), entry
->local_sym_name
);
434 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
437 try_open (name
, exten
)
444 result
= fopen (name
, "r");
446 if (trace_file_tries
)
449 info_msg (_("cannot find script file %s\n"), name
);
451 info_msg (_("opened script file %s\n"), name
);
459 sprintf (buff
, "%s%s", name
, exten
);
460 result
= fopen (buff
, "r");
462 if (trace_file_tries
)
465 info_msg (_("cannot find script file %s\n"), buff
);
467 info_msg (_("opened script file %s\n"), buff
);
474 /* Try to open NAME; if that fails, look for it in any directories
475 specified with -L, without and with EXTEND appended. */
478 ldfile_find_command_file (name
, extend
)
482 search_dirs_type
*search
;
486 /* First try raw name. */
487 result
= try_open (name
, "");
488 if (result
== (FILE *) NULL
)
490 /* Try now prefixes. */
491 for (search
= search_head
;
492 search
!= (search_dirs_type
*) NULL
;
493 search
= search
->next
)
495 sprintf (buffer
, "%s%s%s", search
->name
, slash
, name
);
497 result
= try_open (buffer
, extend
);
507 ldfile_open_command_file (name
)
510 FILE *ldlex_input_stack
;
511 ldlex_input_stack
= ldfile_find_command_file (name
, "");
513 if (ldlex_input_stack
== (FILE *) NULL
)
515 bfd_set_error (bfd_error_system_call
);
516 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
519 lex_push_file (ldlex_input_stack
, name
);
521 ldfile_input_filename
= name
;
524 saved_script_handle
= ldlex_input_stack
;
529 gnu960_map_archname (name
)
532 struct tabentry
{ char *cmd_switch
; char *arch
; };
533 static struct tabentry arch_tab
[] =
538 "KC", "mc", /* Synonym for MC */
541 "SA", "ka", /* Functionally equivalent to KA */
542 "SB", "kb", /* Functionally equivalent to KB */
547 for (tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++)
549 if (! strcmp (name
,tp
->cmd_switch
))
553 if (tp
->cmd_switch
== NULL
)
554 einfo (_("%P%F: unknown architecture: %s\n"), name
);
560 ldfile_add_arch (name
)
563 search_arch_type
*new =
564 (search_arch_type
*) xmalloc ((bfd_size_type
) (sizeof (search_arch_type
)));
568 if (ldfile_output_machine_name
[0] != '\0')
570 einfo (_("%P%F: target architecture respecified\n"));
574 ldfile_output_machine_name
= name
;
577 new->next
= (search_arch_type
*) NULL
;
578 new->name
= gnu960_map_archname (name
);
579 *search_arch_tail_ptr
= new;
580 search_arch_tail_ptr
= &new->next
;
583 #else /* not GNU960 */
586 ldfile_add_arch (in_name
)
589 char *name
= xstrdup (in_name
);
590 search_arch_type
*new =
591 (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
593 ldfile_output_machine_name
= in_name
;
596 new->next
= (search_arch_type
*) NULL
;
599 *name
= TOLOWER (*name
);
602 *search_arch_tail_ptr
= new;
603 search_arch_tail_ptr
= &new->next
;
608 /* Set the output architecture. */
611 ldfile_set_output_arch (string
)
614 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
618 ldfile_output_architecture
= arch
->arch
;
619 ldfile_output_machine
= arch
->mach
;
620 ldfile_output_machine_name
= arch
->printable_name
;
624 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);