1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of the GNU Binutils.
7 This program 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 3 of the License, or
10 (at your option) any later version.
12 This program 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
25 #include "safe-ctype.h"
35 #include "libiberty.h"
36 #include "filenames.h"
38 const char * ldfile_input_filename
;
39 bfd_boolean ldfile_assumed_script
= FALSE
;
40 const char * ldfile_output_machine_name
= "";
41 unsigned long ldfile_output_machine
;
42 enum bfd_architecture ldfile_output_architecture
;
43 search_dirs_type
* search_head
;
46 static char * slash
= "";
48 #if defined (_WIN32) && ! defined (__CYGWIN32__)
49 static char * slash
= "\\";
51 static char * slash
= "/";
55 typedef struct search_arch
58 struct search_arch
*next
;
61 static search_dirs_type
**search_tail_ptr
= &search_head
;
62 static search_arch_type
*search_arch_head
;
63 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
65 /* Test whether a pathname, after canonicalization, is the same or a
66 sub-directory of the sysroot directory. */
69 is_sysrooted_pathname (const char *name
, bfd_boolean notsame
)
71 char * realname
= ld_canon_sysroot
? lrealpath (name
) : NULL
;
78 len
= strlen (realname
);
80 if (((! notsame
&& len
== ld_canon_sysroot_len
)
81 || (len
>= ld_canon_sysroot_len
82 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
])
83 && (realname
[ld_canon_sysroot_len
] = '\0') == '\0'))
84 && FILENAME_CMP (ld_canon_sysroot
, realname
) == 0)
95 /* Adds NAME to the library search path.
96 Makes a copy of NAME using xmalloc(). */
99 ldfile_add_library_path (const char *name
, bfd_boolean cmdline
)
101 search_dirs_type
*new_dirs
;
103 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
106 new_dirs
= (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
107 new_dirs
->next
= NULL
;
108 new_dirs
->cmdline
= cmdline
;
109 *search_tail_ptr
= new_dirs
;
110 search_tail_ptr
= &new_dirs
->next
;
112 /* If a directory is marked as honoring sysroot, prepend the sysroot path
116 new_dirs
->name
= concat (ld_sysroot
, name
+ 1, (const char *) NULL
);
117 new_dirs
->sysrooted
= TRUE
;
121 new_dirs
->name
= xstrdup (name
);
122 new_dirs
->sysrooted
= is_sysrooted_pathname (name
, FALSE
);
126 /* Try to open a BFD for a lang_input_statement. */
129 ldfile_try_open_bfd (const char *attempt
,
130 lang_input_statement_type
*entry
)
132 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
134 if (trace_file_tries
)
136 if (entry
->the_bfd
== NULL
)
137 info_msg (_("attempt to open %s failed\n"), attempt
);
139 info_msg (_("attempt to open %s succeeded\n"), attempt
);
142 if (entry
->the_bfd
== NULL
)
144 if (bfd_get_error () == bfd_error_invalid_target
)
145 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
149 /* If we are searching for this file, see if the architecture is
150 compatible with the output file. If it isn't, keep searching.
151 If we can't open the file as an object file, stop the search
152 here. If we are statically linking, ensure that we don't link
155 if (entry
->search_dirs_flag
|| !entry
->dynamic
)
159 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
160 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
162 check
= entry
->the_bfd
;
166 if (! bfd_check_format (check
, bfd_object
))
168 if (check
== entry
->the_bfd
169 && entry
->search_dirs_flag
170 && bfd_get_error () == bfd_error_file_not_recognized
171 && ! ldemul_unrecognized_file (entry
))
174 char *arg
, *arg1
, *arg2
, *arg3
;
177 /* Try to interpret the file as a linker script. */
178 ldfile_open_command_file (attempt
);
180 ldfile_assumed_script
= TRUE
;
181 parser_input
= input_selected
;
183 token
= INPUT_SCRIPT
;
189 if ((token
= yylex ()) != '(')
191 if ((token
= yylex ()) != NAME
)
199 if ((token
= yylex ()) != NAME
)
205 if ((token
= yylex ()) != ','
206 || (token
= yylex ()) != NAME
)
217 switch (command_line
.endian
)
223 arg
= arg2
? arg2
: arg1
; break;
225 arg
= arg3
? arg3
: arg1
; break;
227 if (strcmp (arg
, lang_get_output_target ()) != 0)
231 if (arg2
) free (arg2
);
232 if (arg3
) free (arg3
);
236 case VERS_IDENTIFIER
:
241 if (yylval
.bigint
.str
)
242 free (yylval
.bigint
.str
);
248 ldfile_assumed_script
= FALSE
;
253 if (command_line
.warn_search_mismatch
)
254 einfo (_("%P: skipping incompatible %s "
255 "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
, link_info
.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 (link_info
.output_bfd
) == bfd_target_xcoff_flavour
280 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
282 if (command_line
.warn_search_mismatch
)
283 einfo (_("%P: skipping incompatible %s "
284 "when searching for %s\n"),
285 attempt
, entry
->local_sym_name
);
286 bfd_close (entry
->the_bfd
);
287 entry
->the_bfd
= NULL
;
296 /* Search for and open the file specified by ENTRY. If it is an
297 archive, use ARCH, LIB and SUFFIX to modify the file name. */
300 ldfile_open_file_search (const char *arch
,
301 lang_input_statement_type
*entry
,
305 search_dirs_type
*search
;
307 /* If this is not an archive, try to open it in the current
309 if (! entry
->is_archive
)
311 if (entry
->sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
313 char *name
= concat (ld_sysroot
, entry
->filename
,
314 (const char *) NULL
);
315 if (ldfile_try_open_bfd (name
, entry
))
317 entry
->filename
= name
;
322 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
324 entry
->sysrooted
= IS_ABSOLUTE_PATH (entry
->filename
)
325 && is_sysrooted_pathname (entry
->filename
, TRUE
);
329 if (IS_ABSOLUTE_PATH (entry
->filename
))
333 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
337 if (entry
->dynamic
&& ! link_info
.relocatable
)
339 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
341 entry
->sysrooted
= search
->sysrooted
;
346 if (entry
->is_archive
)
347 string
= concat (search
->name
, slash
, lib
, entry
->filename
,
348 arch
, suffix
, (const char *) NULL
);
350 string
= concat (search
->name
, slash
, entry
->filename
,
353 if (ldfile_try_open_bfd (string
, entry
))
355 entry
->filename
= string
;
356 entry
->sysrooted
= search
->sysrooted
;
366 /* Open the input file specified by ENTRY.
367 PR 4437: Do not stop on the first missing file, but
368 continue processing other input files in case there
369 are more errors to report. */
372 ldfile_open_file (lang_input_statement_type
*entry
)
374 if (entry
->the_bfd
!= NULL
)
377 if (! entry
->search_dirs_flag
)
379 if (ldfile_try_open_bfd (entry
->filename
, entry
))
382 if (strcmp (entry
->filename
, entry
->local_sym_name
) != 0)
383 einfo (_("%P: cannot find %s (%s): %E\n"),
384 entry
->filename
, entry
->local_sym_name
);
386 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
388 entry
->missing_file
= TRUE
;
393 search_arch_type
*arch
;
394 bfd_boolean found
= FALSE
;
396 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
397 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
399 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
403 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
407 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
412 /* If we have found the file, we don't need to search directories
415 entry
->search_dirs_flag
= FALSE
;
420 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
421 einfo (_("%P: cannot find %s inside %s\n"),
422 entry
->local_sym_name
, ld_sysroot
);
424 einfo (_("%P: cannot find %s\n"), entry
->local_sym_name
);
425 entry
->missing_file
= TRUE
;
431 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
434 try_open (const char *name
, const char *exten
)
438 result
= fopen (name
, "r");
440 if (trace_file_tries
)
443 info_msg (_("cannot find script file %s\n"), name
);
445 info_msg (_("opened script file %s\n"), name
);
455 buff
= concat (name
, exten
, (const char *) NULL
);
456 result
= fopen (buff
, "r");
458 if (trace_file_tries
)
461 info_msg (_("cannot find script file %s\n"), buff
);
463 info_msg (_("opened script file %s\n"), buff
);
471 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
474 check_for_scripts_dir (char *dir
)
480 buf
= concat (dir
, "/ldscripts", (const char *) NULL
);
481 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
486 /* Return the default directory for finding script files.
487 We look for the "ldscripts" directory in:
489 SCRIPTDIR (passed from Makefile)
490 (adjusted according to the current location of the binary)
491 SCRIPTDIR (passed from Makefile)
492 the dir where this program is (for using it from the build tree). */
495 find_scripts_dir (void)
499 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
502 if (check_for_scripts_dir (dir
))
507 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
510 if (check_for_scripts_dir (dir
))
515 if (check_for_scripts_dir (SCRIPTDIR
))
516 /* We've been installed normally. */
519 /* Look for "ldscripts" in the dir where our binary is. */
520 dir
= make_relative_prefix (program_name
, ".", ".");
523 if (check_for_scripts_dir (dir
))
531 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
532 it in directories specified with -L, then in the default script
533 directory, without and with EXTEND appended. If DEFAULT_ONLY is
534 true, the search is restricted to the default script location. */
537 ldfile_find_command_file (const char *name
, const char *extend
,
538 bfd_boolean default_only
)
540 search_dirs_type
*search
;
543 static search_dirs_type
*script_search
;
547 /* First try raw name. */
548 result
= try_open (name
, "");
555 char *script_dir
= find_scripts_dir ();
558 search_dirs_type
**save_tail_ptr
= search_tail_ptr
;
559 search_tail_ptr
= &script_search
;
560 ldfile_add_library_path (script_dir
, TRUE
);
561 search_tail_ptr
= save_tail_ptr
;
565 /* Temporarily append script_search to the path list so that the
566 paths specified with -L will be searched first. */
567 *search_tail_ptr
= script_search
;
569 /* Try now prefixes. */
570 for (search
= default_only
? script_search
: search_head
;
572 search
= search
->next
)
574 buffer
= concat (search
->name
, slash
, name
, (const char *) NULL
);
575 result
= try_open (buffer
, extend
);
581 /* Restore the original path list. */
582 *search_tail_ptr
= NULL
;
587 /* Open command file NAME. */
590 ldfile_open_command_file_1 (const char *name
, bfd_boolean default_only
)
592 FILE *ldlex_input_stack
;
593 ldlex_input_stack
= ldfile_find_command_file (name
, "", default_only
);
595 if (ldlex_input_stack
== NULL
)
597 bfd_set_error (bfd_error_system_call
);
598 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
601 lex_push_file (ldlex_input_stack
, name
);
603 ldfile_input_filename
= name
;
606 saved_script_handle
= ldlex_input_stack
;
609 /* Open command file NAME in the current directory, -L directories,
610 the default script location, in that order. */
613 ldfile_open_command_file (const char *name
)
615 ldfile_open_command_file_1 (name
, FALSE
);
618 /* Open command file NAME at the default script location. */
621 ldfile_open_default_command_file (const char *name
)
623 ldfile_open_command_file_1 (name
, TRUE
);
627 ldfile_add_arch (const char *in_name
)
629 char *name
= xstrdup (in_name
);
630 search_arch_type
*new_arch
= (search_arch_type
*)
631 xmalloc (sizeof (search_arch_type
));
633 ldfile_output_machine_name
= in_name
;
635 new_arch
->name
= name
;
636 new_arch
->next
= NULL
;
639 *name
= TOLOWER (*name
);
642 *search_arch_tail_ptr
= new_arch
;
643 search_arch_tail_ptr
= &new_arch
->next
;
647 /* Set the output architecture. */
650 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
652 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
656 ldfile_output_architecture
= arch
->arch
;
657 ldfile_output_machine
= arch
->mach
;
658 ldfile_output_machine_name
= arch
->printable_name
;
660 else if (defarch
!= bfd_arch_unknown
)
661 ldfile_output_architecture
= defarch
;
663 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);