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"
38 #include "plugin-api.h"
40 #endif /* ENABLE_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 /* Linker needs to decompress sections. */
146 entry
->the_bfd
->flags
|= BFD_DECOMPRESS
;
148 /* This is a linker input BFD. */
149 entry
->the_bfd
->is_linker_input
= 1;
151 #ifdef ENABLE_PLUGINS
152 if (entry
->flags
.lto_output
)
153 entry
->the_bfd
->lto_output
= 1;
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
159 here. If we are statically linking, ensure that we don't link
162 In the code below, it's OK to exit early if the check fails,
163 closing the checked BFD and returning FALSE, but if the BFD
164 checks out compatible, do not exit early returning TRUE, or
165 the plugins will not get a chance to claim the file. */
167 if (entry
->flags
.search_dirs
|| !entry
->flags
.dynamic
)
171 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
172 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
174 check
= entry
->the_bfd
;
178 if (!bfd_check_format (check
, bfd_object
))
180 if (check
== entry
->the_bfd
181 && entry
->flags
.search_dirs
182 && bfd_get_error () == bfd_error_file_not_recognized
183 && !ldemul_unrecognized_file (entry
))
186 char *arg
, *arg1
, *arg2
, *arg3
;
189 /* Try to interpret the file as a linker script. */
190 ldfile_open_command_file (attempt
);
192 ldfile_assumed_script
= TRUE
;
193 parser_input
= input_selected
;
195 token
= INPUT_SCRIPT
;
201 if ((token
= yylex ()) != '(')
203 if ((token
= yylex ()) != NAME
)
211 if ((token
= yylex ()) != NAME
)
217 if ((token
= yylex ()) != ','
218 || (token
= yylex ()) != NAME
)
229 switch (command_line
.endian
)
235 arg
= arg2
? arg2
: arg1
; break;
237 arg
= arg3
? arg3
: arg1
; break;
239 if (strcmp (arg
, lang_get_output_target ()) != 0)
248 case VERS_IDENTIFIER
:
253 free (yylval
.bigint
.str
);
259 ldfile_assumed_script
= FALSE
;
264 if (command_line
.warn_search_mismatch
)
265 einfo (_("%P: skipping incompatible %s "
266 "when searching for %s\n"),
267 attempt
, entry
->local_sym_name
);
268 bfd_close (entry
->the_bfd
);
269 entry
->the_bfd
= NULL
;
276 if (!entry
->flags
.dynamic
&& (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
278 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
280 bfd_close (entry
->the_bfd
);
281 entry
->the_bfd
= NULL
;
285 if (entry
->flags
.search_dirs
286 && !bfd_arch_get_compatible (check
, link_info
.output_bfd
,
287 command_line
.accept_unknown_input_arch
)
288 /* XCOFF archives can have 32 and 64 bit objects. */
289 && !(bfd_get_flavour (check
) == bfd_target_xcoff_flavour
290 && (bfd_get_flavour (link_info
.output_bfd
)
291 == bfd_target_xcoff_flavour
)
292 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
294 if (command_line
.warn_search_mismatch
)
295 einfo (_("%P: skipping incompatible %s "
296 "when searching for %s\n"),
297 attempt
, entry
->local_sym_name
);
298 bfd_close (entry
->the_bfd
);
299 entry
->the_bfd
= NULL
;
305 #ifdef ENABLE_PLUGINS
306 /* If plugins are active, they get first chance to claim
307 any successfully-opened input file. We skip archives
308 here; the plugin wants us to offer it the individual
309 members when we enumerate them, not the whole file. We
310 also ignore corefiles, because that's just weird. It is
311 a needed side-effect of calling bfd_check_format with
312 bfd_object that it sets the bfd's arch and mach, which
313 will be needed when and if we want to bfd_create a new
314 one using this one as a template. */
315 if (link_info
.lto_plugin_active
317 && bfd_check_format (entry
->the_bfd
, bfd_object
))
318 plugin_maybe_claim (entry
);
319 #endif /* ENABLE_PLUGINS */
321 /* It opened OK, the format checked out, and the plugins have had
322 their chance to claim it, so this is success. */
326 /* Search for and open the file specified by ENTRY. If it is an
327 archive, use ARCH, LIB and SUFFIX to modify the file name. */
330 ldfile_open_file_search (const char *arch
,
331 lang_input_statement_type
*entry
,
335 search_dirs_type
*search
;
337 /* If this is not an archive, try to open it in the current
339 if (!entry
->flags
.maybe_archive
)
341 if (entry
->flags
.sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
343 char *name
= concat (ld_sysroot
, entry
->filename
,
344 (const char *) NULL
);
345 if (ldfile_try_open_bfd (name
, entry
))
347 entry
->filename
= name
;
352 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
355 if (IS_ABSOLUTE_PATH (entry
->filename
))
359 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
363 if (entry
->flags
.dynamic
&& !bfd_link_relocatable (&link_info
))
365 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
369 if (entry
->flags
.maybe_archive
&& !entry
->flags
.full_name_provided
)
370 string
= concat (search
->name
, slash
, lib
, entry
->filename
,
371 arch
, suffix
, (const char *) NULL
);
373 string
= concat (search
->name
, slash
, entry
->filename
,
376 if (ldfile_try_open_bfd (string
, entry
))
378 entry
->filename
= string
;
388 /* Open the input file specified by ENTRY.
389 PR 4437: Do not stop on the first missing file, but
390 continue processing other input files in case there
391 are more errors to report. */
394 ldfile_open_file (lang_input_statement_type
*entry
)
396 if (entry
->the_bfd
!= NULL
)
399 if (!entry
->flags
.search_dirs
)
401 if (ldfile_try_open_bfd (entry
->filename
, entry
))
404 if (filename_cmp (entry
->filename
, entry
->local_sym_name
) != 0)
405 einfo (_("%P: cannot find %s (%s): %E\n"),
406 entry
->filename
, entry
->local_sym_name
);
408 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
410 entry
->flags
.missing_file
= TRUE
;
411 input_flags
.missing_file
= TRUE
;
415 search_arch_type
*arch
;
416 bfd_boolean found
= FALSE
;
418 /* If extra_search_path is set, entry->filename is a relative path.
419 Search the directory of the current linker script before searching
421 if (entry
->extra_search_path
)
423 char *path
= concat (entry
->extra_search_path
, slash
, entry
->filename
,
425 if (ldfile_try_open_bfd (path
, entry
))
427 entry
->filename
= path
;
428 entry
->flags
.search_dirs
= FALSE
;
435 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */
436 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
438 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
442 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
446 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
451 /* If we have found the file, we don't need to search directories
454 entry
->flags
.search_dirs
= FALSE
;
457 if (entry
->flags
.sysrooted
459 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
460 einfo (_("%P: cannot find %s inside %s\n"),
461 entry
->local_sym_name
, ld_sysroot
);
463 einfo (_("%P: cannot find %s\n"), entry
->local_sym_name
);
465 /* PR 25747: Be kind to users who forgot to add the
466 "lib" prefix to their library when it was created. */
467 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
469 if (ldfile_open_file_search (arch
->name
, entry
, "", ".a"))
471 const char * base
= lbasename (entry
->filename
);
473 einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"),
474 entry
->filename
, base
, base
);
475 bfd_close (entry
->the_bfd
);
476 entry
->the_bfd
= NULL
;
480 entry
->flags
.missing_file
= TRUE
;
481 input_flags
.missing_file
= TRUE
;
486 /* Try to open NAME. */
489 try_open (const char *name
, bfd_boolean
*sysrooted
)
493 result
= fopen (name
, "r");
496 *sysrooted
= is_sysrooted_pathname (name
);
501 info_msg (_("cannot find script file %s\n"), name
);
503 info_msg (_("opened script file %s\n"), name
);
509 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
512 check_for_scripts_dir (char *dir
)
518 buf
= concat (dir
, "/ldscripts", (const char *) NULL
);
519 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
524 /* Return the default directory for finding script files.
525 We look for the "ldscripts" directory in:
527 SCRIPTDIR (passed from Makefile)
528 (adjusted according to the current location of the binary)
529 the dir where this program is (for using it from the build tree). */
532 find_scripts_dir (void)
536 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
539 if (check_for_scripts_dir (dir
))
544 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
547 if (check_for_scripts_dir (dir
))
552 /* Look for "ldscripts" in the dir where our binary is. */
553 dir
= make_relative_prefix (program_name
, ".", ".");
556 if (check_for_scripts_dir (dir
))
564 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
565 it in directories specified with -L, then in the default script
566 directory. If DEFAULT_ONLY is true, the search is restricted to
567 the default script location. */
570 ldfile_find_command_file (const char *name
,
571 bfd_boolean default_only
,
572 bfd_boolean
*sysrooted
)
574 search_dirs_type
*search
;
577 static search_dirs_type
*script_search
;
581 /* First try raw name. */
582 result
= try_open (name
, sysrooted
);
589 char *script_dir
= find_scripts_dir ();
592 search_dirs_type
**save_tail_ptr
= search_tail_ptr
;
593 search_tail_ptr
= &script_search
;
594 ldfile_add_library_path (script_dir
, TRUE
);
595 search_tail_ptr
= save_tail_ptr
;
599 /* Temporarily append script_search to the path list so that the
600 paths specified with -L will be searched first. */
601 *search_tail_ptr
= script_search
;
603 /* Try now prefixes. */
604 for (search
= default_only
? script_search
: search_head
;
606 search
= search
->next
)
608 path
= concat (search
->name
, slash
, name
, (const char *) NULL
);
609 result
= try_open (path
, sysrooted
);
615 /* Restore the original path list. */
616 *search_tail_ptr
= NULL
;
621 enum script_open_style
{
627 struct script_name_list
629 struct script_name_list
*next
;
630 enum script_open_style open_how
;
634 /* Open command file NAME. */
637 ldfile_open_command_file_1 (const char *name
, enum script_open_style open_how
)
639 FILE *ldlex_input_stack
;
640 bfd_boolean sysrooted
;
641 static struct script_name_list
*processed_scripts
= NULL
;
642 struct script_name_list
*script
;
645 /* PR 24576: Catch the case where the user has accidentally included
646 the same linker script twice. */
647 for (script
= processed_scripts
; script
!= NULL
; script
= script
->next
)
649 if ((open_how
!= script_nonT
|| script
->open_how
!= script_nonT
)
650 && strcmp (name
, script
->name
) == 0)
652 einfo (_("%F%P: error: linker script file '%s'"
653 " appears multiple times\n"), name
);
658 /* FIXME: This memory is never freed, but that should not really matter.
659 It will be released when the linker exits, and it is unlikely to ever
660 be more than a few tens of bytes. */
662 script
= xmalloc (sizeof (*script
) + len
);
663 script
->next
= processed_scripts
;
664 script
->open_how
= open_how
;
665 memcpy (script
->name
, name
, len
+ 1);
666 processed_scripts
= script
;
668 ldlex_input_stack
= ldfile_find_command_file (name
,
669 open_how
== script_defaultT
,
671 if (ldlex_input_stack
== NULL
)
673 bfd_set_error (bfd_error_system_call
);
674 einfo (_("%F%P: cannot open linker script file %s: %E\n"), name
);
678 lex_push_file (ldlex_input_stack
, name
, sysrooted
);
682 saved_script_handle
= ldlex_input_stack
;
685 /* Open command file NAME in the current directory, -L directories,
686 the default script location, in that order. */
689 ldfile_open_command_file (const char *name
)
691 ldfile_open_command_file_1 (name
, script_nonT
);
695 ldfile_open_script_file (const char *name
)
697 ldfile_open_command_file_1 (name
, script_T
);
700 /* Open command file NAME at the default script location. */
703 ldfile_open_default_command_file (const char *name
)
705 ldfile_open_command_file_1 (name
, script_defaultT
);
709 ldfile_add_arch (const char *in_name
)
711 char *name
= xstrdup (in_name
);
712 search_arch_type
*new_arch
713 = (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
715 ldfile_output_machine_name
= in_name
;
717 new_arch
->name
= name
;
718 new_arch
->next
= NULL
;
721 *name
= TOLOWER (*name
);
724 *search_arch_tail_ptr
= new_arch
;
725 search_arch_tail_ptr
= &new_arch
->next
;
729 /* Set the output architecture. */
732 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
734 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
738 ldfile_output_architecture
= arch
->arch
;
739 ldfile_output_machine
= arch
->mach
;
740 ldfile_output_machine_name
= arch
->printable_name
;
742 else if (defarch
!= bfd_arch_unknown
)
743 ldfile_output_architecture
= defarch
;
745 einfo (_("%F%P: cannot represent machine `%s'\n"), string
);