]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldfile.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / ldfile.c
1 /* Linker file opening and searching.
2 Copyright (C) 1991-2021 Free Software Foundation, Inc.
3
4 This file is part of the GNU Binutils.
5
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.
10
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.
15
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. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "ctf-api.h"
25 #include "safe-ctype.h"
26 #include "ld.h"
27 #include "ldmisc.h"
28 #include "ldexp.h"
29 #include "ldlang.h"
30 #include "ldfile.h"
31 #include "ldmain.h"
32 #include <ldgram.h>
33 #include "ldlex.h"
34 #include "ldemul.h"
35 #include "libiberty.h"
36 #include "filenames.h"
37 #if BFD_SUPPORTS_PLUGINS
38 #include "plugin-api.h"
39 #include "plugin.h"
40 #endif /* BFD_SUPPORTS_PLUGINS */
41
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;
47
48 #ifdef VMS
49 static char *slash = "";
50 #else
51 #if defined (_WIN32) && !defined (__CYGWIN32__)
52 static char *slash = "\\";
53 #else
54 static char *slash = "/";
55 #endif
56 #endif
57
58 typedef struct search_arch
59 {
60 char *name;
61 struct search_arch *next;
62 } search_arch_type;
63
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;
67
68 /* Test whether a pathname, after canonicalization, is the same or a
69 sub-directory of the sysroot directory. */
70
71 static bfd_boolean
72 is_sysrooted_pathname (const char *name)
73 {
74 char *realname;
75 int len;
76 bfd_boolean result;
77
78 if (ld_canon_sysroot == NULL)
79 return FALSE;
80
81 realname = lrealpath (name);
82 len = strlen (realname);
83 result = FALSE;
84 if (len > ld_canon_sysroot_len
85 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
86 {
87 realname[ld_canon_sysroot_len] = '\0';
88 result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
89 }
90
91 free (realname);
92 return result;
93 }
94
95 /* Adds NAME to the library search path.
96 Makes a copy of NAME using xmalloc(). */
97
98 void
99 ldfile_add_library_path (const char *name, bfd_boolean cmdline)
100 {
101 search_dirs_type *new_dirs;
102
103 if (!cmdline && config.only_cmd_line_lib_dirs)
104 return;
105
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;
111
112 /* If a directory is marked as honoring sysroot, prepend the sysroot path
113 now. */
114 if (name[0] == '=')
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);
118 else
119 new_dirs->name = xstrdup (name);
120 }
121
122 /* Try to open a BFD for a lang_input_statement. */
123
124 bfd_boolean
125 ldfile_try_open_bfd (const char *attempt,
126 lang_input_statement_type *entry)
127 {
128 entry->the_bfd = bfd_openr (attempt, entry->target);
129
130 if (verbose)
131 {
132 if (entry->the_bfd == NULL)
133 info_msg (_("attempt to open %s failed\n"), attempt);
134 else
135 info_msg (_("attempt to open %s succeeded\n"), attempt);
136 }
137
138 if (entry->the_bfd == NULL)
139 {
140 if (bfd_get_error () == bfd_error_invalid_target)
141 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
142 return FALSE;
143 }
144
145 track_dependency_files (attempt);
146
147 /* Linker needs to decompress sections. */
148 entry->the_bfd->flags |= BFD_DECOMPRESS;
149
150 /* This is a linker input BFD. */
151 entry->the_bfd->is_linker_input = 1;
152
153 #if BFD_SUPPORTS_PLUGINS
154 if (entry->flags.lto_output)
155 entry->the_bfd->lto_output = 1;
156 #endif
157
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
162 a dynamic object.
163
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. */
168
169 if (entry->flags.search_dirs || !entry->flags.dynamic)
170 {
171 bfd *check;
172
173 if (bfd_check_format (entry->the_bfd, bfd_archive))
174 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
175 else
176 check = entry->the_bfd;
177
178 if (check != NULL)
179 {
180 if (!bfd_check_format (check, bfd_object))
181 {
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))
186 {
187 int token, skip = 0;
188 char *arg, *arg1, *arg2, *arg3;
189 extern FILE *yyin;
190
191 /* Try to interpret the file as a linker script. */
192 ldfile_open_command_file (attempt);
193
194 ldfile_assumed_script = TRUE;
195 parser_input = input_selected;
196 ldlex_both ();
197 token = INPUT_SCRIPT;
198 while (token != 0)
199 {
200 switch (token)
201 {
202 case OUTPUT_FORMAT:
203 if ((token = yylex ()) != '(')
204 continue;
205 if ((token = yylex ()) != NAME)
206 continue;
207 arg1 = yylval.name;
208 arg2 = NULL;
209 arg3 = NULL;
210 token = yylex ();
211 if (token == ',')
212 {
213 if ((token = yylex ()) != NAME)
214 {
215 free (arg1);
216 continue;
217 }
218 arg2 = yylval.name;
219 if ((token = yylex ()) != ','
220 || (token = yylex ()) != NAME)
221 {
222 free (arg1);
223 free (arg2);
224 continue;
225 }
226 arg3 = yylval.name;
227 token = yylex ();
228 }
229 if (token == ')')
230 {
231 switch (command_line.endian)
232 {
233 default:
234 case ENDIAN_UNSET:
235 arg = arg1; break;
236 case ENDIAN_BIG:
237 arg = arg2 ? arg2 : arg1; break;
238 case ENDIAN_LITTLE:
239 arg = arg3 ? arg3 : arg1; break;
240 }
241 if (strcmp (arg, lang_get_output_target ()) != 0)
242 skip = 1;
243 }
244 free (arg1);
245 free (arg2);
246 free (arg3);
247 break;
248 case NAME:
249 case LNAME:
250 case VERS_IDENTIFIER:
251 case VERS_TAG:
252 free (yylval.name);
253 break;
254 case INT:
255 free (yylval.bigint.str);
256 break;
257 }
258 token = yylex ();
259 }
260 ldlex_popstate ();
261 ldfile_assumed_script = FALSE;
262 fclose (yyin);
263 yyin = NULL;
264 if (skip)
265 {
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;
272 return FALSE;
273 }
274 }
275 goto success;
276 }
277
278 if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
279 {
280 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
281 attempt);
282 bfd_close (entry->the_bfd);
283 entry->the_bfd = NULL;
284 return FALSE;
285 }
286
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)))
295 {
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;
302 return FALSE;
303 }
304 }
305 }
306 success:
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
318 && !no_more_claiming
319 && bfd_check_format (entry->the_bfd, bfd_object))
320 plugin_maybe_claim (entry);
321 #endif /* BFD_SUPPORTS_PLUGINS */
322
323 /* It opened OK, the format checked out, and the plugins have had
324 their chance to claim it, so this is success. */
325 return TRUE;
326 }
327
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. */
330
331 bfd_boolean
332 ldfile_open_file_search (const char *arch,
333 lang_input_statement_type *entry,
334 const char *lib,
335 const char *suffix)
336 {
337 search_dirs_type *search;
338
339 /* If this is not an archive, try to open it in the current
340 directory first. */
341 if (!entry->flags.maybe_archive)
342 {
343 if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
344 {
345 char *name = concat (ld_sysroot, entry->filename,
346 (const char *) NULL);
347 if (ldfile_try_open_bfd (name, entry))
348 {
349 entry->filename = name;
350 return TRUE;
351 }
352 free (name);
353 }
354 else if (ldfile_try_open_bfd (entry->filename, entry))
355 return TRUE;
356
357 if (IS_ABSOLUTE_PATH (entry->filename))
358 return FALSE;
359 }
360
361 for (search = search_head; search != NULL; search = search->next)
362 {
363 char *string;
364
365 if (entry->flags.dynamic && !bfd_link_relocatable (&link_info))
366 {
367 if (ldemul_open_dynamic_archive (arch, search, entry))
368 return TRUE;
369 }
370
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);
374 else
375 string = concat (search->name, slash, entry->filename,
376 (const char *) 0);
377
378 if (ldfile_try_open_bfd (string, entry))
379 {
380 entry->filename = string;
381 return TRUE;
382 }
383
384 free (string);
385 }
386
387 return FALSE;
388 }
389
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. */
394
395 void
396 ldfile_open_file (lang_input_statement_type *entry)
397 {
398 if (entry->the_bfd != NULL)
399 return;
400
401 if (!entry->flags.search_dirs)
402 {
403 if (ldfile_try_open_bfd (entry->filename, entry))
404 return;
405
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);
409 else
410 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
411
412 entry->flags.missing_file = TRUE;
413 input_flags.missing_file = TRUE;
414 }
415 else
416 {
417 search_arch_type *arch;
418 bfd_boolean found = FALSE;
419
420 /* If extra_search_path is set, entry->filename is a relative path.
421 Search the directory of the current linker script before searching
422 other paths. */
423 if (entry->extra_search_path)
424 {
425 char *path = concat (entry->extra_search_path, slash, entry->filename,
426 (const char *)0);
427 if (ldfile_try_open_bfd (path, entry))
428 {
429 entry->filename = path;
430 entry->flags.search_dirs = FALSE;
431 return;
432 }
433
434 free (path);
435 }
436
437 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */
438 for (arch = search_arch_head; arch != NULL; arch = arch->next)
439 {
440 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
441 if (found)
442 break;
443 #ifdef VMS
444 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
445 if (found)
446 break;
447 #endif
448 found = ldemul_find_potential_libraries (arch->name, entry);
449 if (found)
450 break;
451 }
452
453 /* If we have found the file, we don't need to search directories
454 again. */
455 if (found)
456 entry->flags.search_dirs = FALSE;
457 else
458 {
459 if (entry->flags.sysrooted
460 && ld_sysroot
461 && IS_ABSOLUTE_PATH (entry->local_sym_name))
462 einfo (_("%P: cannot find %s inside %s\n"),
463 entry->local_sym_name, ld_sysroot);
464 #if SUPPORT_ERROR_HANDLING_SCRIPT
465 else if (error_handling_script != NULL)
466 {
467 char * argv[4];
468 const char * res;
469 int status, err;
470
471 argv[0] = error_handling_script;
472 argv[1] = "missing-lib";
473 argv[2] = (char *) entry->local_sym_name;
474 argv[3] = NULL;
475
476 if (verbose)
477 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
478 argv[0], argv[1], argv[2]);
479
480 res = pex_one (PEX_SEARCH, error_handling_script, argv,
481 N_("error handling script"),
482 NULL /* Send stdout to random, temp file. */,
483 NULL /* Write to stderr. */,
484 &status, &err);
485 if (res != NULL)
486 {
487 einfo (_("%P: Failed to run error handling script '%s', reason: "),
488 error_handling_script);
489 /* FIXME: We assume here that errrno == err. */
490 perror (res);
491 }
492 else /* We ignore the return status of the script
493 and always print the error message. */
494 einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
495 }
496 #endif
497 else
498 einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
499
500 /* PR 25747: Be kind to users who forgot to add the
501 "lib" prefix to their library when it was created. */
502 for (arch = search_arch_head; arch != NULL; arch = arch->next)
503 {
504 if (ldfile_open_file_search (arch->name, entry, "", ".a"))
505 {
506 const char * base = lbasename (entry->filename);
507
508 einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"),
509 entry->filename, base, base);
510 bfd_close (entry->the_bfd);
511 entry->the_bfd = NULL;
512 break;
513 }
514 }
515
516 entry->flags.missing_file = TRUE;
517 input_flags.missing_file = TRUE;
518 }
519 }
520 }
521
522 /* Try to open NAME. */
523
524 static FILE *
525 try_open (const char *name, bfd_boolean *sysrooted)
526 {
527 FILE *result;
528
529 result = fopen (name, "r");
530
531 if (result != NULL)
532 *sysrooted = is_sysrooted_pathname (name);
533
534 if (verbose)
535 {
536 if (result == NULL)
537 info_msg (_("cannot find script file %s\n"), name);
538 else
539 info_msg (_("opened script file %s\n"), name);
540 }
541
542 return result;
543 }
544
545 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
546
547 static bfd_boolean
548 check_for_scripts_dir (char *dir)
549 {
550 char *buf;
551 struct stat s;
552 bfd_boolean res;
553
554 buf = concat (dir, "/ldscripts", (const char *) NULL);
555 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
556 free (buf);
557 return res;
558 }
559
560 /* Return the default directory for finding script files.
561 We look for the "ldscripts" directory in:
562
563 SCRIPTDIR (passed from Makefile)
564 (adjusted according to the current location of the binary)
565 the dir where this program is (for using it from the build tree). */
566
567 static char *
568 find_scripts_dir (void)
569 {
570 char *dir;
571
572 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
573 if (dir)
574 {
575 if (check_for_scripts_dir (dir))
576 return dir;
577 free (dir);
578 }
579
580 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
581 if (dir)
582 {
583 if (check_for_scripts_dir (dir))
584 return dir;
585 free (dir);
586 }
587
588 /* Look for "ldscripts" in the dir where our binary is. */
589 dir = make_relative_prefix (program_name, ".", ".");
590 if (dir)
591 {
592 if (check_for_scripts_dir (dir))
593 return dir;
594 free (dir);
595 }
596
597 return NULL;
598 }
599
600 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
601 it in directories specified with -L, then in the default script
602 directory. If DEFAULT_ONLY is true, the search is restricted to
603 the default script location. */
604
605 static FILE *
606 ldfile_find_command_file (const char *name,
607 bfd_boolean default_only,
608 bfd_boolean *sysrooted)
609 {
610 search_dirs_type *search;
611 FILE *result = NULL;
612 char *path;
613 static search_dirs_type *script_search;
614
615 if (!default_only)
616 {
617 /* First try raw name. */
618 result = try_open (name, sysrooted);
619 if (result != NULL)
620 return result;
621 }
622
623 if (!script_search)
624 {
625 char *script_dir = find_scripts_dir ();
626 if (script_dir)
627 {
628 search_dirs_type **save_tail_ptr = search_tail_ptr;
629 search_tail_ptr = &script_search;
630 ldfile_add_library_path (script_dir, TRUE);
631 search_tail_ptr = save_tail_ptr;
632 }
633 }
634
635 /* Temporarily append script_search to the path list so that the
636 paths specified with -L will be searched first. */
637 *search_tail_ptr = script_search;
638
639 /* Try now prefixes. */
640 for (search = default_only ? script_search : search_head;
641 search != NULL;
642 search = search->next)
643 {
644 path = concat (search->name, slash, name, (const char *) NULL);
645 result = try_open (path, sysrooted);
646 free (path);
647 if (result)
648 break;
649 }
650
651 /* Restore the original path list. */
652 *search_tail_ptr = NULL;
653
654 return result;
655 }
656
657 enum script_open_style {
658 script_nonT,
659 script_T,
660 script_defaultT
661 };
662
663 struct script_name_list
664 {
665 struct script_name_list *next;
666 enum script_open_style open_how;
667 char name[1];
668 };
669
670 /* Open command file NAME. */
671
672 static void
673 ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
674 {
675 FILE *ldlex_input_stack;
676 bfd_boolean sysrooted;
677 static struct script_name_list *processed_scripts = NULL;
678 struct script_name_list *script;
679 size_t len;
680
681 /* PR 24576: Catch the case where the user has accidentally included
682 the same linker script twice. */
683 for (script = processed_scripts; script != NULL; script = script->next)
684 {
685 if ((open_how != script_nonT || script->open_how != script_nonT)
686 && strcmp (name, script->name) == 0)
687 {
688 einfo (_("%F%P: error: linker script file '%s'"
689 " appears multiple times\n"), name);
690 return;
691 }
692 }
693
694 /* FIXME: This memory is never freed, but that should not really matter.
695 It will be released when the linker exits, and it is unlikely to ever
696 be more than a few tens of bytes. */
697 len = strlen (name);
698 script = xmalloc (sizeof (*script) + len);
699 script->next = processed_scripts;
700 script->open_how = open_how;
701 memcpy (script->name, name, len + 1);
702 processed_scripts = script;
703
704 ldlex_input_stack = ldfile_find_command_file (name,
705 open_how == script_defaultT,
706 &sysrooted);
707 if (ldlex_input_stack == NULL)
708 {
709 bfd_set_error (bfd_error_system_call);
710 einfo (_("%F%P: cannot open linker script file %s: %E\n"), name);
711 return;
712 }
713
714 track_dependency_files (name);
715
716 lex_push_file (ldlex_input_stack, name, sysrooted);
717
718 lineno = 1;
719
720 saved_script_handle = ldlex_input_stack;
721 }
722
723 /* Open command file NAME in the current directory, -L directories,
724 the default script location, in that order. */
725
726 void
727 ldfile_open_command_file (const char *name)
728 {
729 ldfile_open_command_file_1 (name, script_nonT);
730 }
731
732 void
733 ldfile_open_script_file (const char *name)
734 {
735 ldfile_open_command_file_1 (name, script_T);
736 }
737
738 /* Open command file NAME at the default script location. */
739
740 void
741 ldfile_open_default_command_file (const char *name)
742 {
743 ldfile_open_command_file_1 (name, script_defaultT);
744 }
745
746 void
747 ldfile_add_arch (const char *in_name)
748 {
749 char *name = xstrdup (in_name);
750 search_arch_type *new_arch
751 = (search_arch_type *) xmalloc (sizeof (search_arch_type));
752
753 ldfile_output_machine_name = in_name;
754
755 new_arch->name = name;
756 new_arch->next = NULL;
757 while (*name)
758 {
759 *name = TOLOWER (*name);
760 name++;
761 }
762 *search_arch_tail_ptr = new_arch;
763 search_arch_tail_ptr = &new_arch->next;
764
765 }
766
767 /* Set the output architecture. */
768
769 void
770 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
771 {
772 const bfd_arch_info_type *arch = bfd_scan_arch (string);
773
774 if (arch)
775 {
776 ldfile_output_architecture = arch->arch;
777 ldfile_output_machine = arch->mach;
778 ldfile_output_machine_name = arch->printable_name;
779 }
780 else if (defarch != bfd_arch_unknown)
781 ldfile_output_architecture = defarch;
782 else
783 einfo (_("%F%P: cannot represent machine `%s'\n"), string);
784 }