1 /* Linker file opening and searching.
2 Copyright (C) 1991-2020 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. */
25 #include "safe-ctype.h"
35 #include "libiberty.h"
36 #include "filenames.h"
37 #if BFD_SUPPORTS_PLUGINS
38 #include "plugin-api.h"
40 #endif /* BFD_SUPPORTS_PLUGINS */
42 bfd_boolean ldfile_assumed_script
= FALSE
;
43 const char *ldfile_output_machine_name
= "";
44 unsigned long ldfile_output_machine
;
45 enum bfd_architecture ldfile_output_architecture
;
46 search_dirs_type
*search_head
;
49 static char *slash
= "";
51 #if defined (_WIN32) && !defined (__CYGWIN32__)
52 static char *slash
= "\\";
54 static char *slash
= "/";
58 typedef struct search_arch
61 struct search_arch
*next
;
64 static search_dirs_type
**search_tail_ptr
= &search_head
;
65 static search_arch_type
*search_arch_head
;
66 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
68 /* Test whether a pathname, after canonicalization, is the same or a
69 sub-directory of the sysroot directory. */
72 is_sysrooted_pathname (const char *name
)
78 if (ld_canon_sysroot
== NULL
)
81 realname
= lrealpath (name
);
82 len
= strlen (realname
);
84 if (len
> ld_canon_sysroot_len
85 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
]))
87 realname
[ld_canon_sysroot_len
] = '\0';
88 result
= 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
115 new_dirs
->name
= concat (ld_sysroot
, name
+ 1, (const char *) NULL
);
116 else if (CONST_STRNEQ (name
, "$SYSROOT"))
117 new_dirs
->name
= concat (ld_sysroot
, name
+ strlen ("$SYSROOT"), (const char *) NULL
);
119 new_dirs
->name
= xstrdup (name
);
122 /* Try to open a BFD for a lang_input_statement. */
125 ldfile_try_open_bfd (const char *attempt
,
126 lang_input_statement_type
*entry
)
128 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
132 if (entry
->the_bfd
== NULL
)
133 info_msg (_("attempt to open %s failed\n"), attempt
);
135 info_msg (_("attempt to open %s succeeded\n"), attempt
);
138 if (entry
->the_bfd
== NULL
)
140 if (bfd_get_error () == bfd_error_invalid_target
)
141 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
145 track_dependency_files (attempt
);
147 /* Linker needs to decompress sections. */
148 entry
->the_bfd
->flags
|= BFD_DECOMPRESS
;
150 /* This is a linker input BFD. */
151 entry
->the_bfd
->is_linker_input
= 1;
153 #if BFD_SUPPORTS_PLUGINS
154 if (entry
->flags
.lto_output
)
155 entry
->the_bfd
->lto_output
= 1;
158 /* If we are searching for this file, see if the architecture is
159 compatible with the output file. If it isn't, keep searching.
160 If we can't open the file as an object file, stop the search
161 here. If we are statically linking, ensure that we don't link
164 In the code below, it's OK to exit early if the check fails,
165 closing the checked BFD and returning FALSE, but if the BFD
166 checks out compatible, do not exit early returning TRUE, or
167 the plugins will not get a chance to claim the file. */
169 if (entry
->flags
.search_dirs
|| !entry
->flags
.dynamic
)
173 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
174 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
176 check
= entry
->the_bfd
;
180 if (!bfd_check_format (check
, bfd_object
))
182 if (check
== entry
->the_bfd
183 && entry
->flags
.search_dirs
184 && bfd_get_error () == bfd_error_file_not_recognized
185 && !ldemul_unrecognized_file (entry
))
188 char *arg
, *arg1
, *arg2
, *arg3
;
191 /* Try to interpret the file as a linker script. */
192 ldfile_open_command_file (attempt
);
194 ldfile_assumed_script
= TRUE
;
195 parser_input
= input_selected
;
197 token
= INPUT_SCRIPT
;
203 if ((token
= yylex ()) != '(')
205 if ((token
= yylex ()) != NAME
)
213 if ((token
= yylex ()) != NAME
)
219 if ((token
= yylex ()) != ','
220 || (token
= yylex ()) != NAME
)
231 switch (command_line
.endian
)
237 arg
= arg2
? arg2
: arg1
; break;
239 arg
= arg3
? arg3
: arg1
; break;
241 if (strcmp (arg
, lang_get_output_target ()) != 0)
250 case VERS_IDENTIFIER
:
255 free (yylval
.bigint
.str
);
261 ldfile_assumed_script
= FALSE
;
266 if (command_line
.warn_search_mismatch
)
267 einfo (_("%P: skipping incompatible %s "
268 "when searching for %s\n"),
269 attempt
, entry
->local_sym_name
);
270 bfd_close (entry
->the_bfd
);
271 entry
->the_bfd
= NULL
;
278 if (!entry
->flags
.dynamic
&& (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
280 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
282 bfd_close (entry
->the_bfd
);
283 entry
->the_bfd
= NULL
;
287 if (entry
->flags
.search_dirs
288 && !bfd_arch_get_compatible (check
, link_info
.output_bfd
,
289 command_line
.accept_unknown_input_arch
)
290 /* XCOFF archives can have 32 and 64 bit objects. */
291 && !(bfd_get_flavour (check
) == bfd_target_xcoff_flavour
292 && (bfd_get_flavour (link_info
.output_bfd
)
293 == bfd_target_xcoff_flavour
)
294 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
296 if (command_line
.warn_search_mismatch
)
297 einfo (_("%P: skipping incompatible %s "
298 "when searching for %s\n"),
299 attempt
, entry
->local_sym_name
);
300 bfd_close (entry
->the_bfd
);
301 entry
->the_bfd
= NULL
;
307 #if BFD_SUPPORTS_PLUGINS
308 /* If plugins are active, they get first chance to claim
309 any successfully-opened input file. We skip archives
310 here; the plugin wants us to offer it the individual
311 members when we enumerate them, not the whole file. We
312 also ignore corefiles, because that's just weird. It is
313 a needed side-effect of calling bfd_check_format with
314 bfd_object that it sets the bfd's arch and mach, which
315 will be needed when and if we want to bfd_create a new
316 one using this one as a template. */
317 if (link_info
.lto_plugin_active
319 && bfd_check_format (entry
->the_bfd
, bfd_object
))
320 plugin_maybe_claim (entry
);
321 #endif /* BFD_SUPPORTS_PLUGINS */
323 /* It opened OK, the format checked out, and the plugins have had
324 their chance to claim it, so this is success. */
328 /* Search for and open the file specified by ENTRY. If it is an
329 archive, use ARCH, LIB and SUFFIX to modify the file name. */
332 ldfile_open_file_search (const char *arch
,
333 lang_input_statement_type
*entry
,
337 search_dirs_type
*search
;
339 /* If this is not an archive, try to open it in the current
341 if (!entry
->flags
.maybe_archive
)
343 if (entry
->flags
.sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
345 char *name
= concat (ld_sysroot
, entry
->filename
,
346 (const char *) NULL
);
347 if (ldfile_try_open_bfd (name
, entry
))
349 entry
->filename
= name
;
354 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
357 if (IS_ABSOLUTE_PATH (entry
->filename
))
361 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
365 if (entry
->flags
.dynamic
&& !bfd_link_relocatable (&link_info
))
367 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
371 if (entry
->flags
.maybe_archive
&& !entry
->flags
.full_name_provided
)
372 string
= concat (search
->name
, slash
, lib
, entry
->filename
,
373 arch
, suffix
, (const char *) NULL
);
375 string
= concat (search
->name
, slash
, entry
->filename
,
378 if (ldfile_try_open_bfd (string
, entry
))
380 entry
->filename
= string
;
390 /* Open the input file specified by ENTRY.
391 PR 4437: Do not stop on the first missing file, but
392 continue processing other input files in case there
393 are more errors to report. */
396 ldfile_open_file (lang_input_statement_type
*entry
)
398 if (entry
->the_bfd
!= NULL
)
401 if (!entry
->flags
.search_dirs
)
403 if (ldfile_try_open_bfd (entry
->filename
, entry
))
406 if (filename_cmp (entry
->filename
, entry
->local_sym_name
) != 0)
407 einfo (_("%P: cannot find %s (%s): %E\n"),
408 entry
->filename
, entry
->local_sym_name
);
410 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
412 entry
->flags
.missing_file
= TRUE
;
413 input_flags
.missing_file
= TRUE
;
417 search_arch_type
*arch
;
418 bfd_boolean found
= FALSE
;
420 /* If extra_search_path is set, entry->filename is a relative path.
421 Search the directory of the current linker script before searching
423 if (entry
->extra_search_path
)
425 char *path
= concat (entry
->extra_search_path
, slash
, entry
->filename
,
427 if (ldfile_try_open_bfd (path
, entry
))
429 entry
->filename
= path
;
430 entry
->flags
.search_dirs
= FALSE
;
437 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */
438 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
440 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
444 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
448 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
453 /* If we have found the file, we don't need to search directories
456 entry
->flags
.search_dirs
= FALSE
;
459 if (entry
->flags
.sysrooted
461 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
462 einfo (_("%P: cannot find %s inside %s\n"),
463 entry
->local_sym_name
, ld_sysroot
);
465 einfo (_("%P: cannot find %s\n"), entry
->local_sym_name
);
467 /* PR 25747: Be kind to users who forgot to add the
468 "lib" prefix to their library when it was created. */
469 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
471 if (ldfile_open_file_search (arch
->name
, entry
, "", ".a"))
473 const char * base
= lbasename (entry
->filename
);
475 einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"),
476 entry
->filename
, base
, base
);
477 bfd_close (entry
->the_bfd
);
478 entry
->the_bfd
= NULL
;
482 entry
->flags
.missing_file
= TRUE
;
483 input_flags
.missing_file
= TRUE
;
488 /* Try to open NAME. */
491 try_open (const char *name
, bfd_boolean
*sysrooted
)
495 result
= fopen (name
, "r");
498 *sysrooted
= is_sysrooted_pathname (name
);
503 info_msg (_("cannot find script file %s\n"), name
);
505 info_msg (_("opened script file %s\n"), name
);
511 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
514 check_for_scripts_dir (char *dir
)
520 buf
= concat (dir
, "/ldscripts", (const char *) NULL
);
521 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
526 /* Return the default directory for finding script files.
527 We look for the "ldscripts" directory in:
529 SCRIPTDIR (passed from Makefile)
530 (adjusted according to the current location of the binary)
531 the dir where this program is (for using it from the build tree). */
534 find_scripts_dir (void)
538 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
541 if (check_for_scripts_dir (dir
))
546 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
549 if (check_for_scripts_dir (dir
))
554 /* Look for "ldscripts" in the dir where our binary is. */
555 dir
= make_relative_prefix (program_name
, ".", ".");
558 if (check_for_scripts_dir (dir
))
566 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
567 it in directories specified with -L, then in the default script
568 directory. If DEFAULT_ONLY is true, the search is restricted to
569 the default script location. */
572 ldfile_find_command_file (const char *name
,
573 bfd_boolean default_only
,
574 bfd_boolean
*sysrooted
)
576 search_dirs_type
*search
;
579 static search_dirs_type
*script_search
;
583 /* First try raw name. */
584 result
= try_open (name
, sysrooted
);
591 char *script_dir
= find_scripts_dir ();
594 search_dirs_type
**save_tail_ptr
= search_tail_ptr
;
595 search_tail_ptr
= &script_search
;
596 ldfile_add_library_path (script_dir
, TRUE
);
597 search_tail_ptr
= save_tail_ptr
;
601 /* Temporarily append script_search to the path list so that the
602 paths specified with -L will be searched first. */
603 *search_tail_ptr
= script_search
;
605 /* Try now prefixes. */
606 for (search
= default_only
? script_search
: search_head
;
608 search
= search
->next
)
610 path
= concat (search
->name
, slash
, name
, (const char *) NULL
);
611 result
= try_open (path
, sysrooted
);
617 /* Restore the original path list. */
618 *search_tail_ptr
= NULL
;
623 enum script_open_style
{
629 struct script_name_list
631 struct script_name_list
*next
;
632 enum script_open_style open_how
;
636 /* Open command file NAME. */
639 ldfile_open_command_file_1 (const char *name
, enum script_open_style open_how
)
641 FILE *ldlex_input_stack
;
642 bfd_boolean sysrooted
;
643 static struct script_name_list
*processed_scripts
= NULL
;
644 struct script_name_list
*script
;
647 /* PR 24576: Catch the case where the user has accidentally included
648 the same linker script twice. */
649 for (script
= processed_scripts
; script
!= NULL
; script
= script
->next
)
651 if ((open_how
!= script_nonT
|| script
->open_how
!= script_nonT
)
652 && strcmp (name
, script
->name
) == 0)
654 einfo (_("%F%P: error: linker script file '%s'"
655 " appears multiple times\n"), name
);
660 /* FIXME: This memory is never freed, but that should not really matter.
661 It will be released when the linker exits, and it is unlikely to ever
662 be more than a few tens of bytes. */
664 script
= xmalloc (sizeof (*script
) + len
);
665 script
->next
= processed_scripts
;
666 script
->open_how
= open_how
;
667 memcpy (script
->name
, name
, len
+ 1);
668 processed_scripts
= script
;
670 ldlex_input_stack
= ldfile_find_command_file (name
,
671 open_how
== script_defaultT
,
673 if (ldlex_input_stack
== NULL
)
675 bfd_set_error (bfd_error_system_call
);
676 einfo (_("%F%P: cannot open linker script file %s: %E\n"), name
);
680 track_dependency_files (name
);
682 lex_push_file (ldlex_input_stack
, name
, sysrooted
);
686 saved_script_handle
= ldlex_input_stack
;
689 /* Open command file NAME in the current directory, -L directories,
690 the default script location, in that order. */
693 ldfile_open_command_file (const char *name
)
695 ldfile_open_command_file_1 (name
, script_nonT
);
699 ldfile_open_script_file (const char *name
)
701 ldfile_open_command_file_1 (name
, script_T
);
704 /* Open command file NAME at the default script location. */
707 ldfile_open_default_command_file (const char *name
)
709 ldfile_open_command_file_1 (name
, script_defaultT
);
713 ldfile_add_arch (const char *in_name
)
715 char *name
= xstrdup (in_name
);
716 search_arch_type
*new_arch
717 = (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
719 ldfile_output_machine_name
= in_name
;
721 new_arch
->name
= name
;
722 new_arch
->next
= NULL
;
725 *name
= TOLOWER (*name
);
728 *search_arch_tail_ptr
= new_arch
;
729 search_arch_tail_ptr
= &new_arch
->next
;
733 /* Set the output architecture. */
736 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
738 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
742 ldfile_output_architecture
= arch
->arch
;
743 ldfile_output_machine
= arch
->mach
;
744 ldfile_output_machine_name
= arch
->printable_name
;
746 else if (defarch
!= bfd_arch_unknown
)
747 ldfile_output_architecture
= defarch
;
749 einfo (_("%F%P: cannot represent machine `%s'\n"), string
);