1 /* Linker file opening and searching.
2 Copyright (C) 1991-2016 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "safe-ctype.h"
34 #include "libiberty.h"
35 #include "filenames.h"
37 #include "plugin-api.h"
39 #endif /* ENABLE_PLUGINS */
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
;
48 static char *slash
= "";
50 #if defined (_WIN32) && !defined (__CYGWIN32__)
51 static char *slash
= "\\";
53 static char *slash
= "/";
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
)
77 if (ld_canon_sysroot
== NULL
)
80 realname
= lrealpath (name
);
81 len
= strlen (realname
);
83 if (len
> ld_canon_sysroot_len
84 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
]))
86 realname
[ld_canon_sysroot_len
] = '\0';
87 result
= FILENAME_CMP (ld_canon_sysroot
, realname
) == 0;
94 /* Adds NAME to the library search path.
95 Makes a copy of NAME using xmalloc(). */
98 ldfile_add_library_path (const char *name
, bfd_boolean cmdline
)
100 search_dirs_type
*new_dirs
;
102 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
105 new_dirs
= (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
106 new_dirs
->next
= NULL
;
107 new_dirs
->cmdline
= cmdline
;
108 *search_tail_ptr
= new_dirs
;
109 search_tail_ptr
= &new_dirs
->next
;
111 /* If a directory is marked as honoring sysroot, prepend the sysroot path
114 new_dirs
->name
= concat (ld_sysroot
, name
+ 1, (const char *) NULL
);
116 new_dirs
->name
= xstrdup (name
);
119 /* Try to open a BFD for a lang_input_statement. */
122 ldfile_try_open_bfd (const char *attempt
,
123 lang_input_statement_type
*entry
)
125 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
129 if (entry
->the_bfd
== NULL
)
130 info_msg (_("attempt to open %s failed\n"), attempt
);
132 info_msg (_("attempt to open %s succeeded\n"), attempt
);
135 if (entry
->the_bfd
== NULL
)
137 if (bfd_get_error () == bfd_error_invalid_target
)
138 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
142 /* Linker needs to decompress sections. */
143 entry
->the_bfd
->flags
|= BFD_DECOMPRESS
;
145 /* This is a linker input BFD. */
146 entry
->the_bfd
->is_linker_input
= 1;
148 #ifdef ENABLE_PLUGINS
149 if (entry
->flags
.lto_output
)
150 entry
->the_bfd
->lto_output
= 1;
153 /* If we are searching for this file, see if the architecture is
154 compatible with the output file. If it isn't, keep searching.
155 If we can't open the file as an object file, stop the search
156 here. If we are statically linking, ensure that we don't link
159 In the code below, it's OK to exit early if the check fails,
160 closing the checked BFD and returning FALSE, but if the BFD
161 checks out compatible, do not exit early returning TRUE, or
162 the plugins will not get a chance to claim the file. */
164 if (entry
->flags
.search_dirs
|| !entry
->flags
.dynamic
)
168 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
169 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
171 check
= entry
->the_bfd
;
175 if (!bfd_check_format (check
, bfd_object
))
177 if (check
== entry
->the_bfd
178 && entry
->flags
.search_dirs
179 && bfd_get_error () == bfd_error_file_not_recognized
180 && !ldemul_unrecognized_file (entry
))
183 char *arg
, *arg1
, *arg2
, *arg3
;
186 /* Try to interpret the file as a linker script. */
187 ldfile_open_command_file (attempt
);
189 ldfile_assumed_script
= TRUE
;
190 parser_input
= input_selected
;
192 token
= INPUT_SCRIPT
;
198 if ((token
= yylex ()) != '(')
200 if ((token
= yylex ()) != NAME
)
208 if ((token
= yylex ()) != NAME
)
214 if ((token
= yylex ()) != ','
215 || (token
= yylex ()) != NAME
)
226 switch (command_line
.endian
)
232 arg
= arg2
? arg2
: arg1
; break;
234 arg
= arg3
? arg3
: arg1
; break;
236 if (strcmp (arg
, lang_get_output_target ()) != 0)
240 if (arg2
) free (arg2
);
241 if (arg3
) free (arg3
);
245 case VERS_IDENTIFIER
:
250 if (yylval
.bigint
.str
)
251 free (yylval
.bigint
.str
);
257 ldfile_assumed_script
= FALSE
;
262 if (command_line
.warn_search_mismatch
)
263 einfo (_("%P: skipping incompatible %s "
264 "when searching for %s\n"),
265 attempt
, entry
->local_sym_name
);
266 bfd_close (entry
->the_bfd
);
267 entry
->the_bfd
= NULL
;
274 if (!entry
->flags
.dynamic
&& (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
276 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
278 bfd_close (entry
->the_bfd
);
279 entry
->the_bfd
= NULL
;
283 if (entry
->flags
.search_dirs
284 && !bfd_arch_get_compatible (check
, link_info
.output_bfd
,
285 command_line
.accept_unknown_input_arch
)
286 /* XCOFF archives can have 32 and 64 bit objects. */
287 && !(bfd_get_flavour (check
) == bfd_target_xcoff_flavour
288 && (bfd_get_flavour (link_info
.output_bfd
)
289 == bfd_target_xcoff_flavour
)
290 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
292 if (command_line
.warn_search_mismatch
)
293 einfo (_("%P: skipping incompatible %s "
294 "when searching for %s\n"),
295 attempt
, entry
->local_sym_name
);
296 bfd_close (entry
->the_bfd
);
297 entry
->the_bfd
= NULL
;
303 #ifdef ENABLE_PLUGINS
304 /* If plugins are active, they get first chance to claim
305 any successfully-opened input file. We skip archives
306 here; the plugin wants us to offer it the individual
307 members when we enumerate them, not the whole file. We
308 also ignore corefiles, because that's just weird. It is
309 a needed side-effect of calling bfd_check_format with
310 bfd_object that it sets the bfd's arch and mach, which
311 will be needed when and if we want to bfd_create a new
312 one using this one as a template. */
313 if (link_info
.lto_plugin_active
315 && bfd_check_format (entry
->the_bfd
, bfd_object
))
316 plugin_maybe_claim (entry
);
317 #endif /* ENABLE_PLUGINS */
319 /* It opened OK, the format checked out, and the plugins have had
320 their chance to claim it, so this is success. */
324 /* Search for and open the file specified by ENTRY. If it is an
325 archive, use ARCH, LIB and SUFFIX to modify the file name. */
328 ldfile_open_file_search (const char *arch
,
329 lang_input_statement_type
*entry
,
333 search_dirs_type
*search
;
335 /* If this is not an archive, try to open it in the current
337 if (!entry
->flags
.maybe_archive
)
339 if (entry
->flags
.sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
341 char *name
= concat (ld_sysroot
, entry
->filename
,
342 (const char *) NULL
);
343 if (ldfile_try_open_bfd (name
, entry
))
345 entry
->filename
= name
;
350 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
353 if (IS_ABSOLUTE_PATH (entry
->filename
))
357 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
361 if (entry
->flags
.dynamic
&& !bfd_link_relocatable (&link_info
))
363 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
367 if (entry
->flags
.maybe_archive
&& !entry
->flags
.full_name_provided
)
368 string
= concat (search
->name
, slash
, lib
, entry
->filename
,
369 arch
, suffix
, (const char *) NULL
);
371 string
= concat (search
->name
, slash
, entry
->filename
,
374 if (ldfile_try_open_bfd (string
, entry
))
376 entry
->filename
= string
;
386 /* Open the input file specified by ENTRY.
387 PR 4437: Do not stop on the first missing file, but
388 continue processing other input files in case there
389 are more errors to report. */
392 ldfile_open_file (lang_input_statement_type
*entry
)
394 if (entry
->the_bfd
!= NULL
)
397 if (!entry
->flags
.search_dirs
)
399 if (ldfile_try_open_bfd (entry
->filename
, entry
))
402 if (filename_cmp (entry
->filename
, entry
->local_sym_name
) != 0)
403 einfo (_("%P: cannot find %s (%s): %E\n"),
404 entry
->filename
, entry
->local_sym_name
);
406 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
408 entry
->flags
.missing_file
= TRUE
;
409 input_flags
.missing_file
= TRUE
;
413 search_arch_type
*arch
;
414 bfd_boolean found
= FALSE
;
416 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
417 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
419 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
423 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
427 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
432 /* If we have found the file, we don't need to search directories
435 entry
->flags
.search_dirs
= FALSE
;
438 if (entry
->flags
.sysrooted
440 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
441 einfo (_("%P: cannot find %s inside %s\n"),
442 entry
->local_sym_name
, ld_sysroot
);
444 einfo (_("%P: cannot find %s\n"), entry
->local_sym_name
);
445 entry
->flags
.missing_file
= TRUE
;
446 input_flags
.missing_file
= TRUE
;
451 /* Try to open NAME. */
454 try_open (const char *name
, bfd_boolean
*sysrooted
)
458 result
= fopen (name
, "r");
461 *sysrooted
= is_sysrooted_pathname (name
);
466 info_msg (_("cannot find script file %s\n"), name
);
468 info_msg (_("opened script file %s\n"), name
);
474 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
477 check_for_scripts_dir (char *dir
)
483 buf
= concat (dir
, "/ldscripts", (const char *) NULL
);
484 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
489 /* Return the default directory for finding script files.
490 We look for the "ldscripts" directory in:
492 SCRIPTDIR (passed from Makefile)
493 (adjusted according to the current location of the binary)
494 the dir where this program is (for using it from the build tree). */
497 find_scripts_dir (void)
501 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
504 if (check_for_scripts_dir (dir
))
509 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
512 if (check_for_scripts_dir (dir
))
517 /* Look for "ldscripts" in the dir where our binary is. */
518 dir
= make_relative_prefix (program_name
, ".", ".");
521 if (check_for_scripts_dir (dir
))
529 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
530 it in directories specified with -L, then in the default script
531 directory. If DEFAULT_ONLY is true, the search is restricted to
532 the default script location. */
535 ldfile_find_command_file (const char *name
,
536 bfd_boolean default_only
,
537 bfd_boolean
*sysrooted
)
539 search_dirs_type
*search
;
542 static search_dirs_type
*script_search
;
546 /* First try raw name. */
547 result
= try_open (name
, sysrooted
);
554 char *script_dir
= find_scripts_dir ();
557 search_dirs_type
**save_tail_ptr
= search_tail_ptr
;
558 search_tail_ptr
= &script_search
;
559 ldfile_add_library_path (script_dir
, TRUE
);
560 search_tail_ptr
= save_tail_ptr
;
564 /* Temporarily append script_search to the path list so that the
565 paths specified with -L will be searched first. */
566 *search_tail_ptr
= script_search
;
568 /* Try now prefixes. */
569 for (search
= default_only
? script_search
: search_head
;
571 search
= search
->next
)
573 path
= concat (search
->name
, slash
, name
, (const char *) NULL
);
574 result
= try_open (path
, sysrooted
);
580 /* Restore the original path list. */
581 *search_tail_ptr
= NULL
;
586 /* Open command file NAME. */
589 ldfile_open_command_file_1 (const char *name
, bfd_boolean default_only
)
591 FILE *ldlex_input_stack
;
592 bfd_boolean sysrooted
;
594 ldlex_input_stack
= ldfile_find_command_file (name
, default_only
, &sysrooted
);
596 if (ldlex_input_stack
== NULL
)
598 bfd_set_error (bfd_error_system_call
);
599 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
603 lex_push_file (ldlex_input_stack
, name
, sysrooted
);
607 saved_script_handle
= ldlex_input_stack
;
610 /* Open command file NAME in the current directory, -L directories,
611 the default script location, in that order. */
614 ldfile_open_command_file (const char *name
)
616 ldfile_open_command_file_1 (name
, FALSE
);
619 /* Open command file NAME at the default script location. */
622 ldfile_open_default_command_file (const char *name
)
624 ldfile_open_command_file_1 (name
, TRUE
);
628 ldfile_add_arch (const char *in_name
)
630 char *name
= xstrdup (in_name
);
631 search_arch_type
*new_arch
632 = (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
634 ldfile_output_machine_name
= in_name
;
636 new_arch
->name
= name
;
637 new_arch
->next
= NULL
;
640 *name
= TOLOWER (*name
);
643 *search_arch_tail_ptr
= new_arch
;
644 search_arch_tail_ptr
= &new_arch
->next
;
648 /* Set the output architecture. */
651 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
653 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
657 ldfile_output_architecture
= arch
->arch
;
658 ldfile_output_machine
= arch
->mach
;
659 ldfile_output_machine_name
= arch
->printable_name
;
661 else if (defarch
!= bfd_arch_unknown
)
662 ldfile_output_architecture
= defarch
;
664 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);