1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 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
;
72 /* Test whether a pathname, after canonicalization, is the same or a
73 sub-directory of the sysroot directory. */
76 is_sysrooted_pathname (const char *name
, bfd_boolean notsame
)
78 char * realname
= ld_canon_sysroot
? lrealpath (name
) : NULL
;
85 len
= strlen (realname
);
87 if (((! notsame
&& len
== ld_canon_sysroot_len
)
88 || (len
>= ld_canon_sysroot_len
89 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
])
90 && (realname
[ld_canon_sysroot_len
] = '\0') == '\0'))
91 && FILENAME_CMP (ld_canon_sysroot
, realname
) == 0)
102 /* Adds NAME to the library search path.
103 Makes a copy of NAME using xmalloc(). */
106 ldfile_add_library_path (const char *name
, bfd_boolean cmdline
)
108 search_dirs_type
*new;
110 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
113 new = xmalloc (sizeof (search_dirs_type
));
115 new->cmdline
= cmdline
;
116 *search_tail_ptr
= new;
117 search_tail_ptr
= &new->next
;
119 /* If a directory is marked as honoring sysroot, prepend the sysroot path
123 new->name
= concat (ld_sysroot
, name
+ 1, NULL
);
124 new->sysrooted
= TRUE
;
128 new->name
= xstrdup (name
);
129 new->sysrooted
= is_sysrooted_pathname (name
, FALSE
);
133 /* Try to open a BFD for a lang_input_statement. */
136 ldfile_try_open_bfd (const char *attempt
,
137 lang_input_statement_type
*entry
)
139 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
141 if (trace_file_tries
)
143 if (entry
->the_bfd
== NULL
)
144 info_msg (_("attempt to open %s failed\n"), attempt
);
146 info_msg (_("attempt to open %s succeeded\n"), attempt
);
149 if (entry
->the_bfd
== NULL
)
151 if (bfd_get_error () == bfd_error_invalid_target
)
152 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
156 /* If we are searching for this file, see if the architecture is
157 compatible with the output file. If it isn't, keep searching.
158 If we can't open the file as an object file, stop the search
161 if (entry
->search_dirs_flag
)
165 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
166 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
168 check
= entry
->the_bfd
;
172 if (! bfd_check_format (check
, bfd_object
))
174 if (check
== entry
->the_bfd
175 && bfd_get_error () == bfd_error_file_not_recognized
176 && ! ldemul_unrecognized_file (entry
))
179 char *arg
, *arg1
, *arg2
, *arg3
;
182 /* Try to interpret the file as a linker script. */
183 ldfile_open_command_file (attempt
);
185 ldfile_assumed_script
= TRUE
;
186 parser_input
= input_selected
;
188 token
= INPUT_SCRIPT
;
194 if ((token
= yylex ()) != '(')
196 if ((token
= yylex ()) != NAME
)
204 if ((token
= yylex ()) != NAME
)
210 if ((token
= yylex ()) != ','
211 || (token
= yylex ()) != NAME
)
222 switch (command_line
.endian
)
228 arg
= arg2
? arg2
: arg1
; break;
230 arg
= arg3
? arg3
: arg1
; break;
232 if (strcmp (arg
, lang_get_output_target ()) != 0)
236 if (arg2
) free (arg2
);
237 if (arg3
) free (arg3
);
241 case VERS_IDENTIFIER
:
246 if (yylval
.bigint
.str
)
247 free (yylval
.bigint
.str
);
253 ldfile_assumed_script
= FALSE
;
258 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
259 attempt
, entry
->local_sym_name
);
260 bfd_close (entry
->the_bfd
);
261 entry
->the_bfd
= NULL
;
268 if ((bfd_arch_get_compatible (check
, output_bfd
,
269 command_line
.accept_unknown_input_arch
) == NULL
)
270 /* XCOFF archives can have 32 and 64 bit objects. */
271 && ! (bfd_get_flavour (check
) == bfd_target_xcoff_flavour
272 && bfd_get_flavour (output_bfd
) == bfd_target_xcoff_flavour
273 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
275 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
276 attempt
, entry
->local_sym_name
);
277 bfd_close (entry
->the_bfd
);
278 entry
->the_bfd
= NULL
;
287 /* Search for and open the file specified by ENTRY. If it is an
288 archive, use ARCH, LIB and SUFFIX to modify the file name. */
291 ldfile_open_file_search (const char *arch
,
292 lang_input_statement_type
*entry
,
296 search_dirs_type
*search
;
298 /* If this is not an archive, try to open it in the current
300 if (! entry
->is_archive
)
302 if (entry
->sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
304 char *name
= concat (ld_sysroot
, entry
->filename
,
305 (const char *) NULL
);
306 if (ldfile_try_open_bfd (name
, entry
))
308 entry
->filename
= name
;
313 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
315 entry
->sysrooted
= IS_ABSOLUTE_PATH (entry
->filename
)
316 && is_sysrooted_pathname (entry
->filename
, TRUE
);
320 if (IS_ABSOLUTE_PATH (entry
->filename
))
324 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
328 if (entry
->dynamic
&& ! link_info
.relocatable
)
330 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
332 entry
->sysrooted
= search
->sysrooted
;
337 string
= xmalloc (strlen (search
->name
)
340 + strlen (entry
->filename
)
345 if (entry
->is_archive
)
346 sprintf (string
, "%s%s%s%s%s%s", search
->name
, slash
,
347 lib
, entry
->filename
, arch
, suffix
);
349 sprintf (string
, "%s%s%s", search
->name
, slash
, entry
->filename
);
351 if (ldfile_try_open_bfd (string
, entry
))
353 entry
->filename
= string
;
354 entry
->sysrooted
= search
->sysrooted
;
364 /* Open the input file specified by ENTRY. */
367 ldfile_open_file (lang_input_statement_type
*entry
)
369 if (entry
->the_bfd
!= NULL
)
372 if (! entry
->search_dirs_flag
)
374 if (ldfile_try_open_bfd (entry
->filename
, entry
))
376 if (strcmp (entry
->filename
, entry
->local_sym_name
) != 0)
377 einfo (_("%F%P: %s (%s): No such file: %E\n"),
378 entry
->filename
, entry
->local_sym_name
);
380 einfo (_("%F%P: %s: No such file: %E\n"), entry
->local_sym_name
);
384 search_arch_type
*arch
;
385 bfd_boolean found
= FALSE
;
387 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
388 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
390 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
394 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
398 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
403 /* If we have found the file, we don't need to search directories
406 entry
->search_dirs_flag
= FALSE
;
407 else if (entry
->sysrooted
409 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
410 einfo (_("%F%P: cannot find %s inside %s\n"),
411 entry
->local_sym_name
, ld_sysroot
);
413 einfo (_("%F%P: cannot find %s\n"), entry
->local_sym_name
);
417 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
420 try_open (const char *name
, const char *exten
)
425 result
= fopen (name
, "r");
427 if (trace_file_tries
)
430 info_msg (_("cannot find script file %s\n"), name
);
432 info_msg (_("opened script file %s\n"), name
);
440 sprintf (buff
, "%s%s", name
, exten
);
441 result
= fopen (buff
, "r");
443 if (trace_file_tries
)
446 info_msg (_("cannot find script file %s\n"), buff
);
448 info_msg (_("opened script file %s\n"), buff
);
455 /* Try to open NAME; if that fails, look for it in any directories
456 specified with -L, without and with EXTEND appended. */
459 ldfile_find_command_file (const char *name
, const char *extend
)
461 search_dirs_type
*search
;
465 /* First try raw name. */
466 result
= try_open (name
, "");
469 /* Try now prefixes. */
470 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
472 sprintf (buffer
, "%s%s%s", search
->name
, slash
, name
);
474 result
= try_open (buffer
, extend
);
484 ldfile_open_command_file (const char *name
)
486 FILE *ldlex_input_stack
;
487 ldlex_input_stack
= ldfile_find_command_file (name
, "");
489 if (ldlex_input_stack
== NULL
)
491 bfd_set_error (bfd_error_system_call
);
492 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
495 lex_push_file (ldlex_input_stack
, name
);
497 ldfile_input_filename
= name
;
500 saved_script_handle
= ldlex_input_stack
;
505 gnu960_map_archname (char *name
)
507 struct tabentry
{ char *cmd_switch
; char *arch
; };
508 static struct tabentry arch_tab
[] =
513 "KC", "mc", /* Synonym for MC */
516 "SA", "ka", /* Functionally equivalent to KA */
517 "SB", "kb", /* Functionally equivalent to KB */
522 for (tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++)
524 if (! strcmp (name
,tp
->cmd_switch
))
528 if (tp
->cmd_switch
== NULL
)
529 einfo (_("%P%F: unknown architecture: %s\n"), name
);
535 ldfile_add_arch (char *name
)
537 search_arch_type
*new = xmalloc (sizeof (search_arch_type
));
541 if (ldfile_output_machine_name
[0] != '\0')
543 einfo (_("%P%F: target architecture respecified\n"));
547 ldfile_output_machine_name
= name
;
551 new->name
= gnu960_map_archname (name
);
552 *search_arch_tail_ptr
= new;
553 search_arch_tail_ptr
= &new->next
;
556 #else /* not GNU960 */
559 ldfile_add_arch (const char *in_name
)
561 char *name
= xstrdup (in_name
);
562 search_arch_type
*new = xmalloc (sizeof (search_arch_type
));
564 ldfile_output_machine_name
= in_name
;
570 *name
= TOLOWER (*name
);
573 *search_arch_tail_ptr
= new;
574 search_arch_tail_ptr
= &new->next
;
579 /* Set the output architecture. */
582 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
584 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
588 ldfile_output_architecture
= arch
->arch
;
589 ldfile_output_machine
= arch
->mach
;
590 ldfile_output_machine_name
= arch
->printable_name
;
592 else if (defarch
!= bfd_arch_unknown
)
593 ldfile_output_architecture
= defarch
;
595 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);