1 /* Linker file opening and searching.
2 Copyright (C) 1991-2025 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 #if BFD_SUPPORTS_PLUGINS
39 #include "plugin-api.h"
41 #endif /* BFD_SUPPORTS_PLUGINS */
43 bool ldfile_assumed_script
= false;
44 const char *ldfile_output_machine_name
= "";
45 unsigned long ldfile_output_machine
;
46 enum bfd_architecture ldfile_output_architecture
;
47 search_dirs_type
*search_head
;
50 static char *slash
= "";
52 #if defined (_WIN32) && !defined (__CYGWIN32__)
53 static char *slash
= "\\";
55 static char *slash
= "/";
59 typedef struct search_arch
62 struct search_arch
*next
;
65 static search_dirs_type
**search_tail_ptr
= &search_head
;
66 static search_arch_type
*search_arch_head
;
67 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
69 typedef struct input_remap
71 const char * pattern
; /* Pattern to match input files. */
72 const char * renamed
; /* Filename to use if the pattern matches. */
73 struct input_remap
* next
; /* Link in a chain of these structures. */
76 static struct input_remap
* input_remaps
= NULL
;
79 ldfile_add_remap (const char * pattern
, const char * renamed
)
81 struct input_remap
* new_entry
;
83 new_entry
= xmalloc (sizeof * new_entry
);
84 new_entry
->pattern
= xstrdup (pattern
);
85 new_entry
->next
= NULL
;
87 /* Look for special filenames that mean that the input file should be ignored. */
88 if (strcmp (renamed
, "/dev/null") == 0
89 || strcmp (renamed
, "NUL") == 0)
90 new_entry
->renamed
= NULL
;
92 /* FIXME: Should we add sanity checking of the 'renamed' string ? */
93 new_entry
->renamed
= xstrdup (renamed
);
95 /* It would be easier to add this new node at the start of the chain,
96 but users expect that remapping will occur in the order in which
97 they occur on the command line, and in the remapping files. */
98 if (input_remaps
== NULL
)
100 input_remaps
= new_entry
;
104 struct input_remap
* i
;
106 for (i
= input_remaps
; i
->next
!= NULL
; i
= i
->next
)
113 ldfile_remap_input_free (void)
115 while (input_remaps
!= NULL
)
117 struct input_remap
* i
= input_remaps
;
119 input_remaps
= i
->next
;
120 free ((void *) i
->pattern
);
121 free ((void *) i
->renamed
);
127 ldfile_add_remap_file (const char * file
)
131 f
= fopen (file
, FOPEN_RT
);
135 size_t linelen
= 256;
136 char * line
= xmalloc (linelen
);
143 /* Normally this would use getline(3), but we need to be portable. */
144 while ((q
= fgets (p
, linelen
- (p
- line
), f
)) != NULL
145 && strlen (q
) == linelen
- (p
- line
) - 1
146 && line
[linelen
- 2] != '\n')
148 line
= xrealloc (line
, 2 * linelen
);
149 p
= line
+ linelen
- 1;
153 if (q
== NULL
&& p
== line
)
156 p
= strchr (line
, '\n');
160 /* Because the file format does not know any form of quoting we
161 can search forward for the next '#' character and if found
162 make it terminating the line. */
163 p
= strchr (line
, '#');
167 /* Remove leading whitespace. NUL is no whitespace character. */
169 while (*p
== ' ' || *p
== '\f' || *p
== '\r' || *p
== '\t' || *p
== '\v')
172 /* If the line is blank it is ignored. */
178 /* Advance past the pattern. We accept whitespace or '=' as an
179 end-of-pattern marker. */
180 while (*p
&& *p
!= '=' && *p
!= ' ' && *p
!= '\t' && *p
!= '\f'
181 && *p
!= '\r' && *p
!= '\v')
186 einfo ("%F%P: malformed remap file entry: %s\n", line
);
192 /* Skip whitespace again. */
193 while (*p
== ' ' || *p
== '\f' || *p
== '\r' || *p
== '\t' || *p
== '\v')
198 einfo ("%F%P: malformed remap file entry: %s\n", line
);
204 /* Advance past the rename entry. */
205 while (*p
&& *p
!= '=' && *p
!= ' ' && *p
!= '\t' && *p
!= '\f'
206 && *p
!= '\r' && *p
!= '\v')
208 /* And terminate it. */
211 ldfile_add_remap (pattern
, renamed
);
222 ldfile_possibly_remap_input (const char * filename
)
224 struct input_remap
* i
;
226 if (filename
== NULL
)
229 for (i
= input_remaps
; i
!= NULL
; i
= i
->next
)
231 if (fnmatch (i
->pattern
, filename
, 0) == 0)
235 if (strpbrk ((i
->pattern
), "?*[") != NULL
)
238 info_msg (_("remap input file '%s' to '%s' based upon pattern '%s'\n"),
239 filename
, i
->renamed
, i
->pattern
);
241 info_msg (_("remove input file '%s' based upon pattern '%s'\n"),
242 filename
, i
->pattern
);
247 info_msg (_("remap input file '%s' to '%s'\n"),
248 filename
, i
->renamed
);
250 info_msg (_("remove input file '%s'\n"),
263 ldfile_print_input_remaps (void)
265 if (input_remaps
== NULL
)
268 minfo (_("\nInput File Remapping\n\n"));
270 struct input_remap
* i
;
272 for (i
= input_remaps
; i
!= NULL
; i
= i
->next
)
273 minfo (_(" Pattern: %s\tMaps To: %s\n"), i
->pattern
,
274 i
->renamed
? i
->renamed
: _("<discard>"));
278 /* Test whether a pathname, after canonicalization, is the same or a
279 sub-directory of the sysroot directory. */
282 is_sysrooted_pathname (const char *name
)
288 if (ld_canon_sysroot
== NULL
)
291 realname
= lrealpath (name
);
292 len
= strlen (realname
);
294 if (len
> ld_canon_sysroot_len
295 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
]))
297 realname
[ld_canon_sysroot_len
] = '\0';
298 result
= FILENAME_CMP (ld_canon_sysroot
, realname
) == 0;
305 /* Adds NAME to the library search path.
306 Makes a copy of NAME using xmalloc(). */
309 ldfile_add_library_path (const char *name
, bool cmdline
)
311 search_dirs_type
*new_dirs
;
313 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
316 new_dirs
= (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
317 new_dirs
->next
= NULL
;
318 new_dirs
->cmdline
= cmdline
;
319 *search_tail_ptr
= new_dirs
;
320 search_tail_ptr
= &new_dirs
->next
;
322 /* If a directory is marked as honoring sysroot, prepend the sysroot path
325 new_dirs
->name
= concat (ld_sysroot
, name
+ 1, (const char *) NULL
);
326 else if (startswith (name
, "$SYSROOT"))
327 new_dirs
->name
= concat (ld_sysroot
, name
+ strlen ("$SYSROOT"), (const char *) NULL
);
329 new_dirs
->name
= xstrdup (name
);
332 /* Try to open a BFD for a lang_input_statement. */
335 ldfile_try_open_bfd (const char *attempt
,
336 lang_input_statement_type
*entry
)
338 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
342 if (entry
->the_bfd
== NULL
)
343 info_msg (_("attempt to open %s failed\n"), attempt
);
345 info_msg (_("attempt to open %s succeeded\n"), attempt
);
348 if (entry
->the_bfd
== NULL
)
350 if (bfd_get_error () == bfd_error_invalid_target
)
351 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
355 /* PR 30568: Do not track lto generated temporary object files. */
356 #if BFD_SUPPORTS_PLUGINS
357 if (!entry
->flags
.lto_output
)
359 track_dependency_files (attempt
);
361 /* Linker needs to decompress sections. */
362 entry
->the_bfd
->flags
|= BFD_DECOMPRESS
;
364 /* This is a linker input BFD. */
365 entry
->the_bfd
->is_linker_input
= 1;
367 #if BFD_SUPPORTS_PLUGINS
368 if (entry
->flags
.lto_output
)
369 entry
->the_bfd
->lto_output
= 1;
372 /* If we are searching for this file, see if the architecture is
373 compatible with the output file. If it isn't, keep searching.
374 If we can't open the file as an object file, stop the search
375 here. If we are statically linking, ensure that we don't link
378 In the code below, it's OK to exit early if the check fails,
379 closing the checked BFD and returning false, but if the BFD
380 checks out compatible, do not exit early returning true, or
381 the plugins will not get a chance to claim the file. */
383 if (entry
->flags
.search_dirs
|| !entry
->flags
.dynamic
)
387 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
388 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
390 check
= entry
->the_bfd
;
394 if (!bfd_check_format (check
, bfd_object
))
396 if (check
== entry
->the_bfd
397 && entry
->flags
.search_dirs
398 && bfd_get_error () == bfd_error_file_not_recognized
399 && !ldemul_unrecognized_file (entry
))
402 char *arg
, *arg1
, *arg2
, *arg3
;
405 /* Try to interpret the file as a linker script. */
406 ldfile_open_command_file (attempt
);
408 ldfile_assumed_script
= true;
409 parser_input
= input_selected
;
411 token
= INPUT_SCRIPT
;
417 if ((token
= yylex ()) != '(')
419 if ((token
= yylex ()) != NAME
)
427 if ((token
= yylex ()) != NAME
)
433 if ((token
= yylex ()) != ','
434 || (token
= yylex ()) != NAME
)
445 switch (command_line
.endian
)
451 arg
= arg2
? arg2
: arg1
; break;
453 arg
= arg3
? arg3
: arg1
; break;
455 if (strcmp (arg
, lang_get_output_target ()) != 0)
464 case VERS_IDENTIFIER
:
469 free (yylval
.bigint
.str
);
475 ldfile_assumed_script
= false;
480 if (command_line
.warn_search_mismatch
)
481 einfo (_("%P: skipping incompatible %s "
482 "when searching for %s\n"),
483 attempt
, entry
->local_sym_name
);
484 bfd_close (entry
->the_bfd
);
485 entry
->the_bfd
= NULL
;
492 if (!entry
->flags
.dynamic
&& (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
494 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
496 bfd_close (entry
->the_bfd
);
497 entry
->the_bfd
= NULL
;
501 if (entry
->flags
.search_dirs
502 && !bfd_arch_get_compatible (check
, link_info
.output_bfd
,
503 command_line
.accept_unknown_input_arch
)
504 /* XCOFF archives can have 32 and 64 bit objects. */
505 && !(bfd_get_flavour (check
) == bfd_target_xcoff_flavour
506 && (bfd_get_flavour (link_info
.output_bfd
)
507 == bfd_target_xcoff_flavour
)
508 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
510 if (command_line
.warn_search_mismatch
)
511 einfo (_("%P: skipping incompatible %s "
512 "when searching for %s\n"),
513 attempt
, entry
->local_sym_name
);
514 bfd_close (entry
->the_bfd
);
515 entry
->the_bfd
= NULL
;
521 #if BFD_SUPPORTS_PLUGINS
522 /* If plugins are active, they get first chance to claim
523 any successfully-opened input file. We skip archives
524 here; the plugin wants us to offer it the individual
525 members when we enumerate them, not the whole file. We
526 also ignore corefiles, because that's just weird. It is
527 a needed side-effect of calling bfd_check_format with
528 bfd_object that it sets the bfd's arch and mach, which
529 will be needed when and if we want to bfd_create a new
530 one using this one as a template. */
531 if (link_info
.lto_plugin_active
533 && bfd_check_format (entry
->the_bfd
, bfd_object
))
534 plugin_maybe_claim (entry
);
535 #endif /* BFD_SUPPORTS_PLUGINS */
537 /* It opened OK, the format checked out, and the plugins have had
538 their chance to claim it, so this is success. */
542 /* Search for and open the file specified by ENTRY. If it is an
543 archive, use ARCH, LIB and SUFFIX to modify the file name. */
546 ldfile_open_file_search (const char *arch
,
547 lang_input_statement_type
*entry
,
551 search_dirs_type
*search
;
553 /* If this is not an archive, try to open it in the current
555 if (!entry
->flags
.maybe_archive
)
557 if (entry
->flags
.sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
559 char *name
= concat (ld_sysroot
, entry
->filename
,
560 (const char *) NULL
);
561 if (ldfile_try_open_bfd (name
, entry
))
563 entry
->filename
= name
;
568 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
571 if (IS_ABSOLUTE_PATH (entry
->filename
))
575 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
579 if (entry
->flags
.dynamic
&& !bfd_link_relocatable (&link_info
))
581 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
585 if (entry
->flags
.maybe_archive
&& !entry
->flags
.full_name_provided
)
586 string
= concat (search
->name
, slash
, lib
, entry
->filename
,
587 arch
, suffix
, (const char *) NULL
);
589 string
= concat (search
->name
, slash
, entry
->filename
,
592 if (ldfile_try_open_bfd (string
, entry
))
594 entry
->filename
= string
;
604 /* Open the input file specified by ENTRY.
605 PR 4437: Do not stop on the first missing file, but
606 continue processing other input files in case there
607 are more errors to report. */
610 ldfile_open_file (lang_input_statement_type
*entry
)
612 if (entry
->the_bfd
!= NULL
)
615 if (!entry
->flags
.search_dirs
)
617 if (ldfile_try_open_bfd (entry
->filename
, entry
))
620 if (filename_cmp (entry
->filename
, entry
->local_sym_name
) != 0)
621 einfo (_("%P: cannot find %s (%s): %E\n"),
622 entry
->filename
, entry
->local_sym_name
);
624 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
626 entry
->flags
.missing_file
= true;
627 input_flags
.missing_file
= true;
631 search_arch_type
*arch
;
634 /* If extra_search_path is set, entry->filename is a relative path.
635 Search the directory of the current linker script before searching
637 if (entry
->extra_search_path
)
639 char *path
= concat (entry
->extra_search_path
, slash
, entry
->filename
,
641 if (ldfile_try_open_bfd (path
, entry
))
643 entry
->filename
= path
;
644 entry
->flags
.search_dirs
= false;
651 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */
652 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
654 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
658 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
662 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
667 /* If we have found the file, we don't need to search directories
670 entry
->flags
.search_dirs
= false;
673 if (entry
->flags
.sysrooted
675 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
676 einfo (_("%P: cannot find %s inside %s\n"),
677 entry
->local_sym_name
, ld_sysroot
);
678 #if SUPPORT_ERROR_HANDLING_SCRIPT
679 else if (error_handling_script
!= NULL
)
685 argv
[0] = error_handling_script
;
686 argv
[1] = "missing-lib";
687 argv
[2] = (char *) entry
->local_sym_name
;
691 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
692 argv
[0], argv
[1], argv
[2]);
694 res
= pex_one (PEX_SEARCH
, error_handling_script
, argv
,
695 N_("error handling script"),
696 NULL
/* Send stdout to random, temp file. */,
697 NULL
/* Write to stderr. */,
701 einfo (_("%P: Failed to run error handling script '%s', reason: "),
702 error_handling_script
);
703 /* FIXME: We assume here that errrno == err. */
706 else /* We ignore the return status of the script
707 and always print the error message. */
708 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
712 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
714 /* Be kind to users who are creating static executables, but
715 have forgotten to install the necessary static libraries. */
716 if (entry
->flags
.dynamic
== false && startswith (entry
->local_sym_name
, "-l"))
717 einfo (_("%P: have you installed the static version of the %s library ?\n"),
718 entry
->local_sym_name
+ 2);
720 /* PR 25747: Be kind to users who forgot to add the
721 "lib" prefix to their library when it was created. */
722 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
724 if (ldfile_open_file_search (arch
->name
, entry
, "", ".a"))
726 const char * base
= lbasename (entry
->filename
);
728 einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"),
729 entry
->filename
, base
, base
);
730 bfd_close (entry
->the_bfd
);
731 entry
->the_bfd
= NULL
;
736 entry
->flags
.missing_file
= true;
737 input_flags
.missing_file
= true;
742 /* Try to open NAME. */
745 try_open (const char *name
, bool *sysrooted
)
749 result
= fopen (name
, "r");
753 *sysrooted
= is_sysrooted_pathname (name
);
754 track_dependency_files (name
);
760 info_msg (_("cannot find script file %s\n"), name
);
762 info_msg (_("opened script file %s\n"), name
);
768 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
771 check_for_scripts_dir (char *dir
)
777 buf
= concat (dir
, "/ldscripts", (const char *) NULL
);
778 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
783 /* Return the default directory for finding script files.
784 We look for the "ldscripts" directory in:
786 SCRIPTDIR (passed from Makefile)
787 (adjusted according to the current location of the binary)
788 the dir where this program is (for using it from the build tree). */
791 find_scripts_dir (void)
795 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
798 if (check_for_scripts_dir (dir
))
803 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
806 if (check_for_scripts_dir (dir
))
811 /* Look for "ldscripts" in the dir where our binary is. */
812 dir
= make_relative_prefix (program_name
, ".", ".");
815 if (check_for_scripts_dir (dir
))
823 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
824 it in directories specified with -L, then in the default script
825 directory. If DEFAULT_ONLY is true, the search is restricted to
826 the default script location. */
829 ldfile_find_command_file (const char *name
,
833 search_dirs_type
*search
;
836 static search_dirs_type
*script_search
;
840 /* First try raw name. */
841 result
= try_open (name
, sysrooted
);
848 char *script_dir
= find_scripts_dir ();
851 search_dirs_type
**save_tail_ptr
= search_tail_ptr
;
852 search_tail_ptr
= &script_search
;
853 ldfile_add_library_path (script_dir
, true);
854 search_tail_ptr
= save_tail_ptr
;
858 /* Temporarily append script_search to the path list so that the
859 paths specified with -L will be searched first. */
860 *search_tail_ptr
= script_search
;
862 /* Try now prefixes. */
863 for (search
= default_only
? script_search
: search_head
;
865 search
= search
->next
)
867 path
= concat (search
->name
, slash
, name
, (const char *) NULL
);
868 result
= try_open (path
, sysrooted
);
874 /* Restore the original path list. */
875 *search_tail_ptr
= NULL
;
880 struct script_name_list
*processed_scripts
= NULL
;
881 /* Open command file NAME. */
884 ldfile_open_command_file_1 (const char *name
, enum script_open_style open_how
)
886 FILE *ldlex_input_stack
;
888 struct script_name_list
*script
;
891 /* PR 24576: Catch the case where the user has accidentally included
892 the same linker script twice. */
893 for (script
= processed_scripts
; script
!= NULL
; script
= script
->next
)
895 if ((open_how
!= script_nonT
|| script
->open_how
!= script_nonT
)
896 && strcmp (name
, script
->name
) == 0)
898 einfo (_("%F%P: error: linker script file '%s'"
899 " appears multiple times\n"), name
);
904 /* FIXME: This memory is never freed, but that should not really matter.
905 It will be released when the linker exits, and it is unlikely to ever
906 be more than a few tens of bytes. */
908 script
= xmalloc (sizeof (*script
) + len
);
909 script
->next
= processed_scripts
;
910 script
->open_how
= open_how
;
911 memcpy (script
->name
, name
, len
+ 1);
912 processed_scripts
= script
;
914 ldlex_input_stack
= ldfile_find_command_file (name
,
915 open_how
== script_defaultT
,
917 if (ldlex_input_stack
== NULL
)
919 bfd_set_error (bfd_error_system_call
);
920 einfo (_("%F%P: cannot open linker script file %s: %E\n"), name
);
924 lex_push_file (ldlex_input_stack
, name
, sysrooted
);
928 saved_script_handle
= ldlex_input_stack
;
931 /* Open command file NAME in the current directory, -L directories,
932 the default script location, in that order. */
935 ldfile_open_command_file (const char *name
)
937 ldfile_open_command_file_1 (name
, script_nonT
);
941 ldfile_open_script_file (const char *name
)
943 ldfile_open_command_file_1 (name
, script_T
);
946 /* Open command file NAME at the default script location. */
949 ldfile_open_default_command_file (const char *name
)
951 ldfile_open_command_file_1 (name
, script_defaultT
);
955 ldfile_add_arch (const char *in_name
)
957 char *name
= xstrdup (in_name
);
958 search_arch_type
*new_arch
959 = (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
961 ldfile_output_machine_name
= in_name
;
963 new_arch
->name
= name
;
964 new_arch
->next
= NULL
;
967 *name
= TOLOWER (*name
);
970 *search_arch_tail_ptr
= new_arch
;
971 search_arch_tail_ptr
= &new_arch
->next
;
975 /* Set the output architecture. */
978 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
980 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
984 ldfile_output_architecture
= arch
->arch
;
985 ldfile_output_machine
= arch
->mach
;
986 ldfile_output_machine_name
= arch
->printable_name
;
988 else if (defarch
!= bfd_arch_unknown
)
989 ldfile_output_architecture
= defarch
;
991 einfo (_("%F%P: cannot represent machine `%s'\n"), string
);