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_dirs_type
*script_search
;
67 static search_arch_type
*search_arch_head
;
68 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
70 typedef struct input_remap
72 const char * pattern
; /* Pattern to match input files. */
73 const char * renamed
; /* Filename to use if the pattern matches. */
74 struct input_remap
* next
; /* Link in a chain of these structures. */
77 static struct input_remap
* input_remaps
= NULL
;
80 ldfile_add_remap (const char * pattern
, const char * renamed
)
82 struct input_remap
* new_entry
;
84 new_entry
= xmalloc (sizeof * new_entry
);
85 new_entry
->pattern
= xstrdup (pattern
);
86 new_entry
->next
= NULL
;
88 /* Look for special filenames that mean that the input file should be ignored. */
89 if (strcmp (renamed
, "/dev/null") == 0
90 || strcmp (renamed
, "NUL") == 0)
91 new_entry
->renamed
= NULL
;
93 /* FIXME: Should we add sanity checking of the 'renamed' string ? */
94 new_entry
->renamed
= xstrdup (renamed
);
96 /* It would be easier to add this new node at the start of the chain,
97 but users expect that remapping will occur in the order in which
98 they occur on the command line, and in the remapping files. */
99 if (input_remaps
== NULL
)
101 input_remaps
= new_entry
;
105 struct input_remap
* i
;
107 for (i
= input_remaps
; i
->next
!= NULL
; i
= i
->next
)
114 ldfile_remap_input_free (void)
116 while (input_remaps
!= NULL
)
118 struct input_remap
* i
= input_remaps
;
120 input_remaps
= i
->next
;
121 free ((void *) i
->pattern
);
122 free ((void *) i
->renamed
);
128 ldfile_add_remap_file (const char * file
)
132 f
= fopen (file
, FOPEN_RT
);
136 size_t linelen
= 256;
137 char * line
= xmalloc (linelen
);
144 /* Normally this would use getline(3), but we need to be portable. */
145 while ((q
= fgets (p
, linelen
- (p
- line
), f
)) != NULL
146 && strlen (q
) == linelen
- (p
- line
) - 1
147 && line
[linelen
- 2] != '\n')
149 line
= xrealloc (line
, 2 * linelen
);
150 p
= line
+ linelen
- 1;
154 if (q
== NULL
&& p
== line
)
157 p
= strchr (line
, '\n');
161 /* Because the file format does not know any form of quoting we
162 can search forward for the next '#' character and if found
163 make it terminating the line. */
164 p
= strchr (line
, '#');
168 /* Remove leading whitespace. NUL is no whitespace character. */
170 while (*p
== ' ' || *p
== '\f' || *p
== '\r' || *p
== '\t' || *p
== '\v')
173 /* If the line is blank it is ignored. */
179 /* Advance past the pattern. We accept whitespace or '=' as an
180 end-of-pattern marker. */
181 while (*p
&& *p
!= '=' && *p
!= ' ' && *p
!= '\t' && *p
!= '\f'
182 && *p
!= '\r' && *p
!= '\v')
187 fatal ("%P: malformed remap file entry: %s\n", line
);
193 /* Skip whitespace again. */
194 while (*p
== ' ' || *p
== '\f' || *p
== '\r' || *p
== '\t' || *p
== '\v')
199 fatal ("%P: malformed remap file entry: %s\n", line
);
205 /* Advance past the rename entry. */
206 while (*p
&& *p
!= '=' && *p
!= ' ' && *p
!= '\t' && *p
!= '\f'
207 && *p
!= '\r' && *p
!= '\v')
209 /* And terminate it. */
212 ldfile_add_remap (pattern
, renamed
);
223 ldfile_possibly_remap_input (const char * filename
)
225 struct input_remap
* i
;
227 if (filename
== NULL
)
230 for (i
= input_remaps
; i
!= NULL
; i
= i
->next
)
232 if (fnmatch (i
->pattern
, filename
, 0) == 0)
236 if (strpbrk ((i
->pattern
), "?*[") != NULL
)
239 info_msg (_("remap input file '%s' to '%s' based upon pattern '%s'\n"),
240 filename
, i
->renamed
, i
->pattern
);
242 info_msg (_("remove input file '%s' based upon pattern '%s'\n"),
243 filename
, i
->pattern
);
248 info_msg (_("remap input file '%s' to '%s'\n"),
249 filename
, i
->renamed
);
251 info_msg (_("remove input file '%s'\n"),
264 ldfile_print_input_remaps (void)
266 if (input_remaps
== NULL
)
269 minfo (_("\nInput File Remapping\n\n"));
271 struct input_remap
* i
;
273 for (i
= input_remaps
; i
!= NULL
; i
= i
->next
)
274 minfo (_(" Pattern: %s\tMaps To: %s\n"), i
->pattern
,
275 i
->renamed
? i
->renamed
: _("<discard>"));
279 /* Test whether a pathname, after canonicalization, is the same or a
280 sub-directory of the sysroot directory. */
283 is_sysrooted_pathname (const char *name
)
289 if (ld_canon_sysroot
== NULL
)
292 realname
= lrealpath (name
);
293 len
= strlen (realname
);
295 if (len
> ld_canon_sysroot_len
296 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
]))
298 realname
[ld_canon_sysroot_len
] = '\0';
299 result
= FILENAME_CMP (ld_canon_sysroot
, realname
) == 0;
306 /* Adds NAME to the library search path.
307 Makes a copy of NAME using xmalloc(). */
310 ldfile_add_library_path (const char *name
, bool cmdline
)
312 search_dirs_type
*new_dirs
;
314 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
317 new_dirs
= (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
318 new_dirs
->next
= NULL
;
319 new_dirs
->cmdline
= cmdline
;
320 *search_tail_ptr
= new_dirs
;
321 search_tail_ptr
= &new_dirs
->next
;
323 /* If a directory is marked as honoring sysroot, prepend the sysroot path
326 new_dirs
->name
= concat (ld_sysroot
, name
+ 1, (const char *) NULL
);
327 else if (startswith (name
, "$SYSROOT"))
328 new_dirs
->name
= concat (ld_sysroot
, name
+ strlen ("$SYSROOT"), (const char *) NULL
);
330 new_dirs
->name
= xstrdup (name
);
334 ldfile_library_path_free (search_dirs_type
**root
)
336 search_dirs_type
*ent
;
337 while ((ent
= *root
) != NULL
)
340 free ((void *) ent
->name
);
345 /* Try to open a BFD for a lang_input_statement. */
348 ldfile_try_open_bfd (const char *attempt
,
349 lang_input_statement_type
*entry
)
351 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
355 if (entry
->the_bfd
== NULL
)
356 info_msg (_("attempt to open %s failed\n"), attempt
);
358 info_msg (_("attempt to open %s succeeded\n"), attempt
);
361 if (entry
->the_bfd
== NULL
)
363 if (bfd_get_error () == bfd_error_invalid_target
)
364 fatal (_("%P: invalid BFD target `%s'\n"), entry
->target
);
368 /* PR 30568: Do not track lto generated temporary object files. */
369 #if BFD_SUPPORTS_PLUGINS
370 if (!entry
->flags
.lto_output
)
372 track_dependency_files (attempt
);
374 /* Linker needs to decompress sections. */
375 entry
->the_bfd
->flags
|= BFD_DECOMPRESS
;
377 /* This is a linker input BFD. */
378 entry
->the_bfd
->is_linker_input
= 1;
380 #if BFD_SUPPORTS_PLUGINS
381 if (entry
->flags
.lto_output
)
382 entry
->the_bfd
->lto_output
= 1;
385 /* If we are searching for this file, see if the architecture is
386 compatible with the output file. If it isn't, keep searching.
387 If we can't open the file as an object file, stop the search
388 here. If we are statically linking, ensure that we don't link
391 In the code below, it's OK to exit early if the check fails,
392 closing the checked BFD and returning false, but if the BFD
393 checks out compatible, do not exit early returning true, or
394 the plugins will not get a chance to claim the file. */
396 if (entry
->flags
.search_dirs
|| !entry
->flags
.dynamic
)
400 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
401 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
403 check
= entry
->the_bfd
;
407 if (!bfd_check_format (check
, bfd_object
))
409 if (check
== entry
->the_bfd
410 && entry
->flags
.search_dirs
411 && bfd_get_error () == bfd_error_file_not_recognized
412 && !ldemul_unrecognized_file (entry
))
415 char *arg
, *arg1
, *arg2
, *arg3
;
418 /* Try to interpret the file as a linker script. */
419 ldfile_open_command_file (attempt
);
421 ldfile_assumed_script
= true;
422 parser_input
= input_selected
;
424 token
= INPUT_SCRIPT
;
430 if ((token
= yylex ()) != '(')
432 if ((token
= yylex ()) != NAME
)
440 if ((token
= yylex ()) != NAME
)
443 if ((token
= yylex ()) != ','
444 || (token
= yylex ()) != NAME
)
451 switch (command_line
.endian
)
457 arg
= arg2
? arg2
: arg1
; break;
459 arg
= arg3
? arg3
: arg1
; break;
461 if (strcmp (arg
, lang_get_output_target ()) != 0)
467 case VERS_IDENTIFIER
:
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 fatal (_("%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
);
536 cmdline_check_object_only_section (entry
->the_bfd
, false);
537 #endif /* BFD_SUPPORTS_PLUGINS */
539 /* It opened OK, the format checked out, and the plugins have had
540 their chance to claim it, so this is success. */
544 /* Search for and open the file specified by ENTRY. If it is an
545 archive, use ARCH, LIB and SUFFIX to modify the file name. */
548 ldfile_open_file_search (const char *arch
,
549 lang_input_statement_type
*entry
,
553 search_dirs_type
*search
;
555 /* If this is not an archive, try to open it in the current
557 if (!entry
->flags
.maybe_archive
)
559 if (entry
->flags
.sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
561 char *name
= concat (ld_sysroot
, entry
->filename
,
562 (const char *) NULL
);
563 if (ldfile_try_open_bfd (name
, entry
))
565 entry
->filename
= name
;
570 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
573 if (IS_ABSOLUTE_PATH (entry
->filename
))
577 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
581 if (entry
->flags
.dynamic
&& !bfd_link_relocatable (&link_info
))
583 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
587 if (entry
->flags
.maybe_archive
&& !entry
->flags
.full_name_provided
)
588 string
= concat (search
->name
, slash
, lib
, entry
->filename
,
589 arch
, suffix
, (const char *) NULL
);
591 string
= concat (search
->name
, slash
, entry
->filename
,
594 if (ldfile_try_open_bfd (string
, entry
))
596 entry
->filename
= string
;
606 /* Open the input file specified by ENTRY.
607 PR 4437: Do not stop on the first missing file, but
608 continue processing other input files in case there
609 are more errors to report. */
612 ldfile_open_file (lang_input_statement_type
*entry
)
614 if (entry
->the_bfd
!= NULL
)
617 if (!entry
->flags
.search_dirs
)
619 if (ldfile_try_open_bfd (entry
->filename
, entry
))
622 if (filename_cmp (entry
->filename
, entry
->local_sym_name
) != 0)
623 einfo (_("%P: cannot find %s (%s): %E\n"),
624 entry
->filename
, entry
->local_sym_name
);
626 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
628 entry
->flags
.missing_file
= true;
629 input_flags
.missing_file
= true;
633 search_arch_type
*arch
;
636 /* If extra_search_path is set, entry->filename is a relative path.
637 Search the directory of the current linker script before searching
639 if (entry
->extra_search_path
)
641 char *path
= concat (entry
->extra_search_path
, slash
, entry
->filename
,
643 if (ldfile_try_open_bfd (path
, entry
))
645 entry
->filename
= path
;
646 entry
->flags
.search_dirs
= false;
653 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */
654 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
656 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
660 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
664 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
669 /* If we have found the file, we don't need to search directories
672 entry
->flags
.search_dirs
= false;
675 if (entry
->flags
.sysrooted
677 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
678 einfo (_("%P: cannot find %s inside %s\n"),
679 entry
->local_sym_name
, ld_sysroot
);
680 #if SUPPORT_ERROR_HANDLING_SCRIPT
681 else if (error_handling_script
!= NULL
)
687 argv
[0] = error_handling_script
;
688 argv
[1] = "missing-lib";
689 argv
[2] = (char *) entry
->local_sym_name
;
693 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
694 argv
[0], argv
[1], argv
[2]);
696 res
= pex_one (PEX_SEARCH
, error_handling_script
, argv
,
697 N_("error handling script"),
698 NULL
/* Send stdout to random, temp file. */,
699 NULL
/* Write to stderr. */,
703 einfo (_("%P: Failed to run error handling script '%s', reason: "),
704 error_handling_script
);
705 /* FIXME: We assume here that errrno == err. */
708 else /* We ignore the return status of the script
709 and always print the error message. */
710 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
714 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
716 /* Be kind to users who are creating static executables, but
717 have forgotten to install the necessary static libraries. */
718 if (entry
->flags
.dynamic
== false && startswith (entry
->local_sym_name
, "-l"))
719 einfo (_("%P: have you installed the static version of the %s library ?\n"),
720 entry
->local_sym_name
+ 2);
722 /* PR 25747: Be kind to users who forgot to add the
723 "lib" prefix to their library when it was created. */
724 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
726 if (ldfile_open_file_search (arch
->name
, entry
, "", ".a"))
728 const char * base
= lbasename (entry
->filename
);
730 einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"),
731 entry
->filename
, base
, base
);
732 bfd_close (entry
->the_bfd
);
733 entry
->the_bfd
= NULL
;
738 entry
->flags
.missing_file
= true;
739 input_flags
.missing_file
= true;
744 /* Try to open NAME. */
747 try_open (const char *name
, bool *sysrooted
)
751 result
= fopen (name
, "r");
755 *sysrooted
= is_sysrooted_pathname (name
);
756 track_dependency_files (name
);
762 info_msg (_("cannot find script file %s\n"), name
);
764 info_msg (_("opened script file %s\n"), name
);
770 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
773 check_for_scripts_dir (char *dir
)
779 buf
= concat (dir
, "/ldscripts", (const char *) NULL
);
780 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
785 /* Return the default directory for finding script files.
786 We look for the "ldscripts" directory in:
788 SCRIPTDIR (passed from Makefile)
789 (adjusted according to the current location of the binary)
790 the dir where this program is (for using it from the build tree). */
793 find_scripts_dir (void)
797 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
800 if (check_for_scripts_dir (dir
))
805 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
808 if (check_for_scripts_dir (dir
))
813 /* Look for "ldscripts" in the dir where our binary is. */
814 dir
= make_relative_prefix (program_name
, ".", ".");
817 if (check_for_scripts_dir (dir
))
825 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
826 it in directories specified with -L, then in the default script
827 directory. If DEFAULT_ONLY is true, the search is restricted to
828 the default script location. */
831 ldfile_find_command_file (const char *name
,
835 search_dirs_type
*search
;
841 /* First try raw name. */
842 result
= try_open (name
, sysrooted
);
849 char *script_dir
= find_scripts_dir ();
852 search_dirs_type
**save_tail_ptr
= search_tail_ptr
;
853 search_tail_ptr
= &script_search
;
854 ldfile_add_library_path (script_dir
, true);
855 search_tail_ptr
= save_tail_ptr
;
860 /* Temporarily append script_search to the path list so that the
861 paths specified with -L will be searched first. */
862 *search_tail_ptr
= script_search
;
864 /* Try now prefixes. */
865 for (search
= default_only
? script_search
: search_head
;
867 search
= search
->next
)
869 path
= concat (search
->name
, slash
, name
, (const char *) NULL
);
870 result
= try_open (path
, sysrooted
);
876 /* Restore the original path list. */
877 *search_tail_ptr
= NULL
;
882 struct script_name_list
*processed_scripts
= NULL
;
883 /* Open command file NAME. */
886 ldfile_open_command_file_1 (const char *name
, enum script_open_style open_how
)
888 FILE *ldlex_input_stack
;
890 struct script_name_list
*script
;
893 /* PR 24576: Catch the case where the user has accidentally included
894 the same linker script twice. */
895 for (script
= processed_scripts
; script
!= NULL
; script
= script
->next
)
897 if ((open_how
!= script_nonT
|| script
->open_how
!= script_nonT
)
898 && strcmp (name
, script
->name
) == 0)
900 fatal (_("%P: error: linker script file '%s'"
901 " appears multiple times\n"), name
);
907 script
= xmalloc (sizeof (*script
) + len
);
908 script
->next
= processed_scripts
;
909 script
->open_how
= open_how
;
910 memcpy (script
->name
, name
, len
+ 1);
911 processed_scripts
= script
;
913 ldlex_input_stack
= ldfile_find_command_file (name
,
914 open_how
== script_defaultT
,
916 if (ldlex_input_stack
== NULL
)
918 bfd_set_error (bfd_error_system_call
);
919 fatal (_("%P: cannot open linker script file %s: %E\n"), name
);
923 lex_push_file (ldlex_input_stack
, name
, sysrooted
);
927 saved_script_handle
= ldlex_input_stack
;
931 ldfile_script_free (struct script_name_list
**root
)
933 struct script_name_list
*ent
;
934 while ((ent
= *root
) != NULL
)
941 /* Open command file NAME in the current directory, -L directories,
942 the default script location, in that order. */
945 ldfile_open_command_file (const char *name
)
947 ldfile_open_command_file_1 (name
, script_nonT
);
951 ldfile_open_script_file (const char *name
)
953 ldfile_open_command_file_1 (name
, script_T
);
956 /* Open command file NAME at the default script location. */
959 ldfile_open_default_command_file (const char *name
)
961 ldfile_open_command_file_1 (name
, script_defaultT
);
965 ldfile_add_arch (const char *in_name
)
967 char *name
= xstrdup (in_name
);
968 search_arch_type
*new_arch
969 = (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
971 ldfile_output_machine_name
= in_name
;
973 new_arch
->name
= name
;
974 new_arch
->next
= NULL
;
977 *name
= TOLOWER (*name
);
980 *search_arch_tail_ptr
= new_arch
;
981 search_arch_tail_ptr
= &new_arch
->next
;
986 ldfile_arch_free (search_arch_type
**root
)
988 search_arch_type
*ent
;
989 while ((ent
= *root
) != NULL
)
997 /* Set the output architecture. */
1000 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
1002 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
1006 ldfile_output_architecture
= arch
->arch
;
1007 ldfile_output_machine
= arch
->mach
;
1008 ldfile_output_machine_name
= arch
->printable_name
;
1010 else if (defarch
!= bfd_arch_unknown
)
1011 ldfile_output_architecture
= defarch
;
1013 fatal (_("%P: cannot represent machine `%s'\n"), string
);
1016 /* Tidy up memory. */
1021 ldfile_remap_input_free ();
1022 ldfile_library_path_free (&script_search
);
1023 search_tail_ptr
= &search_head
;
1024 ldfile_library_path_free (&search_head
);
1025 search_arch_tail_ptr
= &search_arch_head
;
1026 ldfile_arch_free (&search_arch_head
);
1027 ldfile_script_free (&processed_scripts
);