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
;
50 #if defined (_WIN32) && ! defined (__CYGWIN32__)
57 typedef struct search_arch
60 struct search_arch
*next
;
63 static search_dirs_type
**search_tail_ptr
= &search_head
;
64 static search_arch_type
*search_arch_head
;
65 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
67 /* Test whether a pathname, after canonicalization, is the same or a
68 sub-directory of the sysroot directory. */
71 is_sysrooted_pathname (const char *name
, bfd_boolean notsame
)
73 char * realname
= ld_canon_sysroot
? lrealpath (name
) : NULL
;
80 len
= strlen (realname
);
82 if (((! notsame
&& len
== ld_canon_sysroot_len
)
83 || (len
>= ld_canon_sysroot_len
84 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
])
85 && (realname
[ld_canon_sysroot_len
] = '\0') == '\0'))
86 && FILENAME_CMP (ld_canon_sysroot
, realname
) == 0)
97 /* Adds NAME to the library search path.
98 Makes a copy of NAME using xmalloc(). */
101 ldfile_add_library_path (const char *name
, bfd_boolean cmdline
)
103 search_dirs_type
*new;
105 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
108 new = xmalloc (sizeof (search_dirs_type
));
110 new->cmdline
= cmdline
;
111 *search_tail_ptr
= new;
112 search_tail_ptr
= &new->next
;
114 /* If a directory is marked as honoring sysroot, prepend the sysroot path
118 new->name
= concat (ld_sysroot
, name
+ 1, NULL
);
119 new->sysrooted
= TRUE
;
123 new->name
= xstrdup (name
);
124 new->sysrooted
= is_sysrooted_pathname (name
, FALSE
);
128 /* Try to open a BFD for a lang_input_statement. */
131 ldfile_try_open_bfd (const char *attempt
,
132 lang_input_statement_type
*entry
)
134 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
136 if (trace_file_tries
)
138 if (entry
->the_bfd
== NULL
)
139 info_msg (_("attempt to open %s failed\n"), attempt
);
141 info_msg (_("attempt to open %s succeeded\n"), attempt
);
144 if (entry
->the_bfd
== NULL
)
146 if (bfd_get_error () == bfd_error_invalid_target
)
147 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
151 /* If we are searching for this file, see if the architecture is
152 compatible with the output file. If it isn't, keep searching.
153 If we can't open the file as an object file, stop the search
154 here. If we are statically linking, ensure that we don't link
157 if (entry
->search_dirs_flag
|| !entry
->dynamic
)
161 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
162 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
164 check
= entry
->the_bfd
;
168 if (! bfd_check_format (check
, bfd_object
))
170 if (check
== entry
->the_bfd
171 && entry
->search_dirs_flag
172 && bfd_get_error () == bfd_error_file_not_recognized
173 && ! ldemul_unrecognized_file (entry
))
176 char *arg
, *arg1
, *arg2
, *arg3
;
179 /* Try to interpret the file as a linker script. */
180 ldfile_open_command_file (attempt
);
182 ldfile_assumed_script
= TRUE
;
183 parser_input
= input_selected
;
185 token
= INPUT_SCRIPT
;
191 if ((token
= yylex ()) != '(')
193 if ((token
= yylex ()) != NAME
)
201 if ((token
= yylex ()) != NAME
)
207 if ((token
= yylex ()) != ','
208 || (token
= yylex ()) != NAME
)
219 switch (command_line
.endian
)
225 arg
= arg2
? arg2
: arg1
; break;
227 arg
= arg3
? arg3
: arg1
; break;
229 if (strcmp (arg
, lang_get_output_target ()) != 0)
233 if (arg2
) free (arg2
);
234 if (arg3
) free (arg3
);
238 case VERS_IDENTIFIER
:
243 if (yylval
.bigint
.str
)
244 free (yylval
.bigint
.str
);
250 ldfile_assumed_script
= FALSE
;
255 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
256 attempt
, entry
->local_sym_name
);
257 bfd_close (entry
->the_bfd
);
258 entry
->the_bfd
= NULL
;
265 if (!entry
->dynamic
&& (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
267 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
269 bfd_close (entry
->the_bfd
);
270 entry
->the_bfd
= NULL
;
274 if (entry
->search_dirs_flag
275 && !bfd_arch_get_compatible (check
, output_bfd
,
276 command_line
.accept_unknown_input_arch
)
277 /* XCOFF archives can have 32 and 64 bit objects. */
278 && ! (bfd_get_flavour (check
) == bfd_target_xcoff_flavour
279 && bfd_get_flavour (output_bfd
) == bfd_target_xcoff_flavour
280 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
282 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
283 attempt
, entry
->local_sym_name
);
284 bfd_close (entry
->the_bfd
);
285 entry
->the_bfd
= NULL
;
294 /* Search for and open the file specified by ENTRY. If it is an
295 archive, use ARCH, LIB and SUFFIX to modify the file name. */
298 ldfile_open_file_search (const char *arch
,
299 lang_input_statement_type
*entry
,
303 search_dirs_type
*search
;
305 /* If this is not an archive, try to open it in the current
307 if (! entry
->is_archive
)
309 if (entry
->sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
311 char *name
= concat (ld_sysroot
, entry
->filename
,
312 (const char *) NULL
);
313 if (ldfile_try_open_bfd (name
, entry
))
315 entry
->filename
= name
;
320 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
322 entry
->sysrooted
= IS_ABSOLUTE_PATH (entry
->filename
)
323 && is_sysrooted_pathname (entry
->filename
, TRUE
);
327 if (IS_ABSOLUTE_PATH (entry
->filename
))
331 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
335 if (entry
->dynamic
&& ! link_info
.relocatable
)
337 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
339 entry
->sysrooted
= search
->sysrooted
;
344 string
= xmalloc (strlen (search
->name
)
347 + strlen (entry
->filename
)
352 if (entry
->is_archive
)
353 sprintf (string
, "%s%s%s%s%s%s", search
->name
, slash
,
354 lib
, entry
->filename
, arch
, suffix
);
356 sprintf (string
, "%s%s%s", search
->name
, slash
, entry
->filename
);
358 if (ldfile_try_open_bfd (string
, entry
))
360 entry
->filename
= string
;
361 entry
->sysrooted
= search
->sysrooted
;
371 /* Open the input file specified by ENTRY. */
374 ldfile_open_file (lang_input_statement_type
*entry
)
376 if (entry
->the_bfd
!= NULL
)
379 if (! entry
->search_dirs_flag
)
381 if (ldfile_try_open_bfd (entry
->filename
, entry
))
383 if (strcmp (entry
->filename
, entry
->local_sym_name
) != 0)
384 einfo (_("%F%P: %s (%s): No such file: %E\n"),
385 entry
->filename
, entry
->local_sym_name
);
387 einfo (_("%F%P: %s: No such file: %E\n"), entry
->local_sym_name
);
391 search_arch_type
*arch
;
392 bfd_boolean found
= FALSE
;
394 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
395 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
397 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
401 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
405 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
410 /* If we have found the file, we don't need to search directories
413 entry
->search_dirs_flag
= FALSE
;
414 else if (entry
->sysrooted
416 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
417 einfo (_("%F%P: cannot find %s inside %s\n"),
418 entry
->local_sym_name
, ld_sysroot
);
420 einfo (_("%F%P: cannot find %s\n"), entry
->local_sym_name
);
424 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
427 try_open (const char *name
, const char *exten
)
432 result
= fopen (name
, "r");
434 if (trace_file_tries
)
437 info_msg (_("cannot find script file %s\n"), name
);
439 info_msg (_("opened script file %s\n"), name
);
447 sprintf (buff
, "%s%s", name
, exten
);
448 result
= fopen (buff
, "r");
450 if (trace_file_tries
)
453 info_msg (_("cannot find script file %s\n"), buff
);
455 info_msg (_("opened script file %s\n"), buff
);
462 /* Try to open NAME; if that fails, look for it in any directories
463 specified with -L, without and with EXTEND appended. */
466 ldfile_find_command_file (const char *name
, const char *extend
)
468 search_dirs_type
*search
;
472 /* First try raw name. */
473 result
= try_open (name
, "");
476 /* Try now prefixes. */
477 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
479 sprintf (buffer
, "%s%s%s", search
->name
, slash
, name
);
481 result
= try_open (buffer
, extend
);
491 ldfile_open_command_file (const char *name
)
493 FILE *ldlex_input_stack
;
494 ldlex_input_stack
= ldfile_find_command_file (name
, "");
496 if (ldlex_input_stack
== NULL
)
498 bfd_set_error (bfd_error_system_call
);
499 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
502 lex_push_file (ldlex_input_stack
, name
);
504 ldfile_input_filename
= name
;
507 saved_script_handle
= ldlex_input_stack
;
512 gnu960_map_archname (char *name
)
514 struct tabentry
{ char *cmd_switch
; char *arch
; };
515 static struct tabentry arch_tab
[] =
520 "KC", "mc", /* Synonym for MC */
523 "SA", "ka", /* Functionally equivalent to KA */
524 "SB", "kb", /* Functionally equivalent to KB */
529 for (tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++)
531 if (! strcmp (name
,tp
->cmd_switch
))
535 if (tp
->cmd_switch
== NULL
)
536 einfo (_("%P%F: unknown architecture: %s\n"), name
);
542 ldfile_add_arch (char *name
)
544 search_arch_type
*new = xmalloc (sizeof (search_arch_type
));
548 if (ldfile_output_machine_name
[0] != '\0')
550 einfo (_("%P%F: target architecture respecified\n"));
554 ldfile_output_machine_name
= name
;
558 new->name
= gnu960_map_archname (name
);
559 *search_arch_tail_ptr
= new;
560 search_arch_tail_ptr
= &new->next
;
563 #else /* not GNU960 */
566 ldfile_add_arch (const char *in_name
)
568 char *name
= xstrdup (in_name
);
569 search_arch_type
*new = xmalloc (sizeof (search_arch_type
));
571 ldfile_output_machine_name
= in_name
;
577 *name
= TOLOWER (*name
);
580 *search_arch_tail_ptr
= new;
581 search_arch_tail_ptr
= &new->next
;
586 /* Set the output architecture. */
589 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
591 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
595 ldfile_output_architecture
= arch
->arch
;
596 ldfile_output_machine
= arch
->mach
;
597 ldfile_output_machine_name
= arch
->printable_name
;
599 else if (defarch
!= bfd_arch_unknown
)
600 ldfile_output_architecture
= defarch
;
602 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);