]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldfile.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / ld / ldfile.c
1 /* Linker file opening and searching.
2 Copyright (C) 1991-2024 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 #include <fnmatch.h>
38 #if BFD_SUPPORTS_PLUGINS
39 #include "plugin-api.h"
40 #include "plugin.h"
41 #endif /* BFD_SUPPORTS_PLUGINS */
42
43 #ifdef VMS
44 static char *slash = "";
45 #else
46 #if defined (_WIN32) && !defined (__CYGWIN32__)
47 static char *slash = "\\";
48 #else
49 static char *slash = "/";
50 #endif
51 #endif
52
53 typedef struct search_arch
54 {
55 char *name;
56 struct search_arch *next;
57 } search_arch_type;
58
59 bool ldfile_assumed_script = false;
60 const char * ldfile_output_machine_name = "";
61 unsigned long ldfile_output_machine;
62 enum bfd_architecture ldfile_output_architecture;
63 search_dirs_type * search_head;
64
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;
68
69 typedef struct input_remap
70 {
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. */
74 } input_remap;
75
76 static struct input_remap * input_remaps = NULL;
77
78 void
79 ldfile_add_remap (const char * pattern, const char * renamed)
80 {
81 struct input_remap * new_entry;
82
83 new_entry = xmalloc (sizeof * new_entry);
84 new_entry->pattern = xstrdup (pattern);
85 new_entry->next = NULL;
86
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;
91 else
92 /* FIXME: Should we add sanity checking of the 'renamed' string ? */
93 new_entry->renamed = xstrdup (renamed);
94
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)
99 {
100 input_remaps = new_entry;
101 }
102 else
103 {
104 struct input_remap * i;
105
106 for (i = input_remaps; i->next != NULL; i = i->next)
107 ;
108 i->next = new_entry;
109 }
110 }
111
112 void
113 ldfile_remap_input_free (void)
114 {
115 while (input_remaps != NULL)
116 {
117 struct input_remap * i = input_remaps;
118
119 input_remaps = i->next;
120 free ((void *) i->pattern);
121 free ((void *) i->renamed);
122 free (i);
123 }
124 }
125
126 bool
127 ldfile_add_remap_file (const char * file)
128 {
129 FILE * f;
130
131 f = fopen (file, FOPEN_RT);
132 if (f == NULL)
133 return false;
134
135 size_t linelen = 256;
136 char * line = xmalloc (linelen);
137
138 do
139 {
140 char * p = line;
141 char * q;
142
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')
147 {
148 line = xrealloc (line, 2 * linelen);
149 p = line + linelen - 1;
150 linelen += linelen;
151 }
152
153 if (q == NULL && p == line)
154 break;
155
156 p = strchr (line, '\n');
157 if (p)
158 *p = '\0';
159
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, '#');
164 if (p)
165 *p = '\0';
166
167 /* Remove leading whitespace. NUL is no whitespace character. */
168 p = line;
169 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
170 ++p;
171
172 /* If the line is blank it is ignored. */
173 if (*p == '\0')
174 continue;
175
176 char * pattern = p;
177
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')
182 ++p;
183
184 if (*p == '\0')
185 {
186 einfo ("%F%P: malformed remap file entry: %s\n", line);
187 continue;
188 }
189
190 * p++ = '\0';
191
192 /* Skip whitespace again. */
193 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
194 ++p;
195
196 if (*p == '\0')
197 {
198 einfo ("%F%P: malformed remap file entry: %s\n", line);
199 continue;
200 }
201
202 char * renamed = p;
203
204 /* Advance past the rename entry. */
205 while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
206 && *p != '\r' && *p != '\v')
207 ++p;
208 /* And terminate it. */
209 *p = '\0';
210
211 ldfile_add_remap (pattern, renamed);
212 }
213 while (! feof (f));
214
215 free (line);
216 fclose (f);
217
218 return true;
219 }
220
221 const char *
222 ldfile_possibly_remap_input (const char * filename)
223 {
224 struct input_remap * i;
225
226 if (filename == NULL)
227 return NULL;
228
229 for (i = input_remaps; i != NULL; i = i->next)
230 {
231 if (fnmatch (i->pattern, filename, 0) == 0)
232 {
233 if (verbose)
234 {
235 if (strpbrk ((i->pattern), "?*[") != NULL)
236 {
237 if (i->renamed)
238 info_msg (_("remap input file '%s' to '%s' based upon pattern '%s'\n"),
239 filename, i->renamed, i->pattern);
240 else
241 info_msg (_("remove input file '%s' based upon pattern '%s'\n"),
242 filename, i->pattern);
243 }
244 else
245 {
246 if (i->renamed)
247 info_msg (_("remap input file '%s' to '%s'\n"),
248 filename, i->renamed);
249 else
250 info_msg (_("remove input file '%s'\n"),
251 filename);
252 }
253 }
254
255 return i->renamed;
256 }
257 }
258
259 return filename;
260 }
261
262 void
263 ldfile_print_input_remaps (void)
264 {
265 if (input_remaps == NULL)
266 return;
267
268 minfo (_("\nInput File Remapping\n\n"));
269
270 struct input_remap * i;
271
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>"));
275 }
276
277
278 /* Test whether a pathname, after canonicalization, is the same or a
279 sub-directory of the sysroot directory. */
280
281 static bool
282 is_sysrooted_pathname (const char *name)
283 {
284 char *realname;
285 int len;
286 bool result;
287
288 if (ld_canon_sysroot == NULL)
289 return false;
290
291 realname = lrealpath (name);
292 len = strlen (realname);
293 result = false;
294 if (len > ld_canon_sysroot_len
295 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
296 {
297 realname[ld_canon_sysroot_len] = '\0';
298 result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
299 }
300
301 free (realname);
302 return result;
303 }
304
305 /* Adds NAME to the library search path.
306 Makes a copy of NAME using xmalloc().
307 Returns a pointer to the newly created search_dirs_type structure
308 or NULL if there was a problem. */
309
310 search_dirs_type *
311 ldfile_add_library_path (const char *name, enum search_dir_source source)
312 {
313 search_dirs_type *new_dirs;
314
315 if (source != search_dir_cmd_line && config.only_cmd_line_lib_dirs)
316 return NULL;
317
318 new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
319 new_dirs->source = source;
320
321 /* If a directory is marked as honoring sysroot, prepend the sysroot path
322 now. */
323 if (name[0] == '=')
324 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
325 else if (startswith (name, "$SYSROOT"))
326 new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
327 else
328 new_dirs->name = xstrdup (name);
329
330 /* Accumulate script and command line sourced
331 search paths at the end of the current list. */
332 #if BFD_SUPPORTS_PLUGINS
333 /* PR 31904: But put plugin sourced paths at the start of the list. */
334 if (source == search_dir_plugin)
335 {
336 new_dirs->next = search_head;
337 search_head = new_dirs;
338 }
339 else
340 #endif
341 {
342 new_dirs->next = NULL;
343 *search_tail_ptr = new_dirs;
344 search_tail_ptr = &new_dirs->next;
345 }
346
347 return new_dirs;
348 }
349
350 /* Try to open a BFD for a lang_input_statement. */
351
352 bool
353 ldfile_try_open_bfd (const char *attempt,
354 lang_input_statement_type *entry)
355 {
356 entry->the_bfd = bfd_openr (attempt, entry->target);
357
358 if (verbose)
359 {
360 if (entry->the_bfd == NULL)
361 info_msg (_("attempt to open %s failed\n"), attempt);
362 else
363 info_msg (_("attempt to open %s succeeded\n"), attempt);
364 }
365
366 if (entry->the_bfd == NULL)
367 {
368 if (bfd_get_error () == bfd_error_invalid_target)
369 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
370 return false;
371 }
372
373 /* PR 30568: Do not track plugin generated object files. */
374 #if BFD_SUPPORTS_PLUGINS
375 if (entry->plugin != NULL)
376 #endif
377 track_dependency_files (attempt);
378
379 /* Linker needs to decompress sections. */
380 entry->the_bfd->flags |= BFD_DECOMPRESS;
381
382 /* This is a linker input BFD. */
383 entry->the_bfd->is_linker_input = 1;
384
385 #if BFD_SUPPORTS_PLUGINS
386 if (entry->plugin != NULL)
387 entry->the_bfd->lto_output = 1;
388 #endif
389
390 /* If we are searching for this file, see if the architecture is
391 compatible with the output file. If it isn't, keep searching.
392 If we can't open the file as an object file, stop the search
393 here. If we are statically linking, ensure that we don't link
394 a dynamic object.
395
396 In the code below, it's OK to exit early if the check fails,
397 closing the checked BFD and returning false, but if the BFD
398 checks out compatible, do not exit early returning true, or
399 the plugins will not get a chance to claim the file. */
400
401 if (entry->flags.search_dirs || !entry->flags.dynamic)
402 {
403 bfd *check;
404
405 if (bfd_check_format (entry->the_bfd, bfd_archive))
406 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
407 else
408 check = entry->the_bfd;
409
410 if (check != NULL)
411 {
412 if (!bfd_check_format (check, bfd_object))
413 {
414 if (check == entry->the_bfd
415 && entry->flags.search_dirs
416 && bfd_get_error () == bfd_error_file_not_recognized
417 && !ldemul_unrecognized_file (entry))
418 {
419 int token, skip = 0;
420 char *arg, *arg1, *arg2, *arg3;
421 extern FILE *yyin;
422
423 /* Try to interpret the file as a linker script. */
424 ldfile_open_command_file (attempt);
425
426 ldfile_assumed_script = true;
427 parser_input = input_selected;
428 ldlex_script ();
429 token = INPUT_SCRIPT;
430 while (token != 0)
431 {
432 switch (token)
433 {
434 case OUTPUT_FORMAT:
435 if ((token = yylex ()) != '(')
436 continue;
437 if ((token = yylex ()) != NAME)
438 continue;
439 arg1 = yylval.name;
440 arg2 = NULL;
441 arg3 = NULL;
442 token = yylex ();
443 if (token == ',')
444 {
445 if ((token = yylex ()) != NAME)
446 {
447 free (arg1);
448 continue;
449 }
450 arg2 = yylval.name;
451 if ((token = yylex ()) != ','
452 || (token = yylex ()) != NAME)
453 {
454 free (arg1);
455 free (arg2);
456 continue;
457 }
458 arg3 = yylval.name;
459 token = yylex ();
460 }
461 if (token == ')')
462 {
463 switch (command_line.endian)
464 {
465 default:
466 case ENDIAN_UNSET:
467 arg = arg1; break;
468 case ENDIAN_BIG:
469 arg = arg2 ? arg2 : arg1; break;
470 case ENDIAN_LITTLE:
471 arg = arg3 ? arg3 : arg1; break;
472 }
473 if (strcmp (arg, lang_get_output_target ()) != 0)
474 skip = 1;
475 }
476 free (arg1);
477 free (arg2);
478 free (arg3);
479 break;
480 case NAME:
481 case LNAME:
482 case VERS_IDENTIFIER:
483 case VERS_TAG:
484 free (yylval.name);
485 break;
486 case INT:
487 free (yylval.bigint.str);
488 break;
489 }
490 token = yylex ();
491 }
492 ldlex_popstate ();
493 ldfile_assumed_script = false;
494 fclose (yyin);
495 yyin = NULL;
496 if (skip)
497 {
498 if (command_line.warn_search_mismatch)
499 einfo (_("%P: skipping incompatible %s "
500 "when searching for %s\n"),
501 attempt, entry->local_sym_name);
502 bfd_close (entry->the_bfd);
503 entry->the_bfd = NULL;
504 return false;
505 }
506 }
507 goto success;
508 }
509
510 if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
511 {
512 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
513 attempt);
514 bfd_close (entry->the_bfd);
515 entry->the_bfd = NULL;
516 return false;
517 }
518
519 if (entry->flags.search_dirs
520 && !bfd_arch_get_compatible (check, link_info.output_bfd,
521 command_line.accept_unknown_input_arch)
522 /* XCOFF archives can have 32 and 64 bit objects. */
523 && !(bfd_get_flavour (check) == bfd_target_xcoff_flavour
524 && (bfd_get_flavour (link_info.output_bfd)
525 == bfd_target_xcoff_flavour)
526 && bfd_check_format (entry->the_bfd, bfd_archive)))
527 {
528 if (command_line.warn_search_mismatch)
529 einfo (_("%P: skipping incompatible %s "
530 "when searching for %s\n"),
531 attempt, entry->local_sym_name);
532 bfd_close (entry->the_bfd);
533 entry->the_bfd = NULL;
534 return false;
535 }
536 }
537 }
538 success:
539 #if BFD_SUPPORTS_PLUGINS
540 /* If plugins are active, they get first chance to claim
541 any successfully-opened input file. We skip archives
542 here; the plugin wants us to offer it the individual
543 members when we enumerate them, not the whole file. We
544 also ignore corefiles, because that's just weird. It is
545 a needed side-effect of calling bfd_check_format with
546 bfd_object that it sets the bfd's arch and mach, which
547 will be needed when and if we want to bfd_create a new
548 one using this one as a template. */
549 if (link_info.lto_plugin_active
550 && !no_more_claiming
551 && bfd_check_format (entry->the_bfd, bfd_object))
552 plugin_maybe_claim (entry);
553 #endif /* BFD_SUPPORTS_PLUGINS */
554
555 /* It opened OK, the format checked out, and the plugins have had
556 their chance to claim it, so this is success. */
557 return true;
558 }
559
560 /* Search for and open the file specified by ENTRY. If it is an
561 archive, use ARCH, LIB and SUFFIX to modify the file name. */
562
563 bool
564 ldfile_open_file_search (const char *arch,
565 lang_input_statement_type *entry,
566 const char *lib,
567 const char *suffix)
568 {
569 search_dirs_type *search;
570
571 /* If this is not an archive, try to open it in the current
572 directory first. */
573 if (!entry->flags.maybe_archive)
574 {
575 if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
576 {
577 char *name = concat (ld_sysroot, entry->filename,
578 (const char *) NULL);
579 if (ldfile_try_open_bfd (name, entry))
580 {
581 entry->filename = name;
582 return true;
583 }
584 free (name);
585 }
586 else if (ldfile_try_open_bfd (entry->filename, entry))
587 return true;
588
589 if (IS_ABSOLUTE_PATH (entry->filename))
590 return false;
591 }
592
593 for (search = search_head; search != NULL; search = search->next)
594 {
595 char *string;
596
597 #if BFD_SUPPORTS_PLUGINS
598 /* PR 31904: Only check a plugin sourced search
599 directory if the file is from the same plugin. */
600 if (search->source == search_dir_plugin
601 && entry->plugin != search->plugin)
602 continue;
603 #endif
604
605 if (entry->flags.dynamic && !bfd_link_relocatable (&link_info))
606 {
607 if (ldemul_open_dynamic_archive (arch, search, entry))
608 return true;
609 }
610
611 if (entry->flags.maybe_archive && !entry->flags.full_name_provided)
612 string = concat (search->name, slash, lib, entry->filename,
613 arch, suffix, (const char *) NULL);
614 else
615 string = concat (search->name, slash, entry->filename,
616 (const char *) 0);
617
618 if (ldfile_try_open_bfd (string, entry))
619 {
620 entry->filename = string;
621 return true;
622 }
623
624 free (string);
625 }
626
627 return false;
628 }
629
630 /* Open the input file specified by ENTRY.
631 PR 4437: Do not stop on the first missing file, but
632 continue processing other input files in case there
633 are more errors to report. */
634
635 void
636 ldfile_open_file (lang_input_statement_type *entry)
637 {
638 if (entry->the_bfd != NULL)
639 return;
640
641 if (!entry->flags.search_dirs)
642 {
643 if (ldfile_try_open_bfd (entry->filename, entry))
644 return;
645
646 if (filename_cmp (entry->filename, entry->local_sym_name) != 0)
647 einfo (_("%P: cannot find %s (%s): %E\n"),
648 entry->filename, entry->local_sym_name);
649 else
650 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
651
652 entry->flags.missing_file = true;
653 input_flags.missing_file = true;
654 }
655 else
656 {
657 search_arch_type *arch;
658 bool found = false;
659
660 /* If extra_search_path is set, entry->filename is a relative path.
661 Search the directory of the current linker script before searching
662 other paths. */
663 if (entry->extra_search_path)
664 {
665 char *path = concat (entry->extra_search_path, slash, entry->filename,
666 (const char *)0);
667 if (ldfile_try_open_bfd (path, entry))
668 {
669 entry->filename = path;
670 entry->flags.search_dirs = false;
671 return;
672 }
673
674 free (path);
675 }
676
677 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */
678 for (arch = search_arch_head; arch != NULL; arch = arch->next)
679 {
680 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
681 if (found)
682 break;
683 #ifdef VMS
684 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
685 if (found)
686 break;
687 #endif
688 found = ldemul_find_potential_libraries (arch->name, entry);
689 if (found)
690 break;
691 }
692
693 /* If we have found the file, we don't need to search directories
694 again. */
695 if (found)
696 entry->flags.search_dirs = false;
697 else
698 {
699 if (entry->flags.sysrooted
700 && ld_sysroot
701 && IS_ABSOLUTE_PATH (entry->local_sym_name))
702 einfo (_("%P: cannot find %s inside %s\n"),
703 entry->local_sym_name, ld_sysroot);
704 #if SUPPORT_ERROR_HANDLING_SCRIPT
705 else if (error_handling_script != NULL)
706 {
707 char * argv[4];
708 const char * res;
709 int status, err;
710
711 argv[0] = error_handling_script;
712 argv[1] = "missing-lib";
713 argv[2] = (char *) entry->local_sym_name;
714 argv[3] = NULL;
715
716 if (verbose)
717 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
718 argv[0], argv[1], argv[2]);
719
720 res = pex_one (PEX_SEARCH, error_handling_script, argv,
721 N_("error handling script"),
722 NULL /* Send stdout to random, temp file. */,
723 NULL /* Write to stderr. */,
724 &status, &err);
725 if (res != NULL)
726 {
727 einfo (_("%P: Failed to run error handling script '%s', reason: "),
728 error_handling_script);
729 /* FIXME: We assume here that errrno == err. */
730 perror (res);
731 }
732 else /* We ignore the return status of the script
733 and always print the error message. */
734 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
735 }
736 #endif
737 else
738 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
739
740 /* PR 25747: Be kind to users who forgot to add the
741 "lib" prefix to their library when it was created. */
742 for (arch = search_arch_head; arch != NULL; arch = arch->next)
743 {
744 if (ldfile_open_file_search (arch->name, entry, "", ".a"))
745 {
746 const char * base = lbasename (entry->filename);
747
748 einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"),
749 entry->filename, base, base);
750 bfd_close (entry->the_bfd);
751 entry->the_bfd = NULL;
752 break;
753 }
754 }
755
756 entry->flags.missing_file = true;
757 input_flags.missing_file = true;
758 }
759 }
760 }
761
762 /* Try to open NAME. */
763
764 static FILE *
765 try_open (const char *name, bool *sysrooted)
766 {
767 FILE *result;
768
769 result = fopen (name, "r");
770
771 if (result != NULL)
772 {
773 *sysrooted = is_sysrooted_pathname (name);
774 track_dependency_files (name);
775 }
776
777 if (verbose)
778 {
779 if (result == NULL)
780 info_msg (_("cannot find script file %s\n"), name);
781 else
782 info_msg (_("opened script file %s\n"), name);
783 }
784
785 return result;
786 }
787
788 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
789
790 static bool
791 check_for_scripts_dir (char *dir)
792 {
793 char *buf;
794 struct stat s;
795 bool res;
796
797 buf = concat (dir, "/ldscripts", (const char *) NULL);
798 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
799 free (buf);
800 return res;
801 }
802
803 /* Return the default directory for finding script files.
804 We look for the "ldscripts" directory in:
805
806 SCRIPTDIR (passed from Makefile)
807 (adjusted according to the current location of the binary)
808 the dir where this program is (for using it from the build tree). */
809
810 static char *
811 find_scripts_dir (void)
812 {
813 char *dir;
814
815 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
816 if (dir)
817 {
818 if (check_for_scripts_dir (dir))
819 return dir;
820 free (dir);
821 }
822
823 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
824 if (dir)
825 {
826 if (check_for_scripts_dir (dir))
827 return dir;
828 free (dir);
829 }
830
831 /* Look for "ldscripts" in the dir where our binary is. */
832 dir = make_relative_prefix (program_name, ".", ".");
833 if (dir)
834 {
835 if (check_for_scripts_dir (dir))
836 return dir;
837 free (dir);
838 }
839
840 return NULL;
841 }
842
843 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
844 it in directories specified with -L, then in the default script
845 directory. If DEFAULT_ONLY is true, the search is restricted to
846 the default script location. */
847
848 static FILE *
849 ldfile_find_command_file (const char *name,
850 bool default_only,
851 bool *sysrooted)
852 {
853 search_dirs_type *search;
854 FILE *result = NULL;
855 char *path;
856 static search_dirs_type *script_search;
857
858 if (!default_only)
859 {
860 /* First try raw name. */
861 result = try_open (name, sysrooted);
862 if (result != NULL)
863 return result;
864 }
865
866 if (!script_search)
867 {
868 char *script_dir = find_scripts_dir ();
869 if (script_dir)
870 {
871 search_dirs_type **save_tail_ptr = search_tail_ptr;
872 search_tail_ptr = &script_search;
873 (void) ldfile_add_library_path (script_dir, search_dir_cmd_line);
874 search_tail_ptr = save_tail_ptr;
875 }
876 }
877
878 /* Temporarily append script_search to the path list so that the
879 paths specified with -L will be searched first. */
880 *search_tail_ptr = script_search;
881
882 /* Try now prefixes. */
883 for (search = default_only ? script_search : search_head;
884 search != NULL;
885 search = search->next)
886 {
887 #if BFD_SUPPORTS_PLUGINS
888 /* Do not search for linker commands in plugin sourced search directories. */
889 if (search->source == search_dir_plugin)
890 continue;
891 #endif
892 path = concat (search->name, slash, name, (const char *) NULL);
893 result = try_open (path, sysrooted);
894 free (path);
895 if (result)
896 break;
897 }
898
899 /* Restore the original path list. */
900 *search_tail_ptr = NULL;
901
902 return result;
903 }
904
905 struct script_name_list *processed_scripts = NULL;
906 /* Open command file NAME. */
907
908 static void
909 ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
910 {
911 FILE *ldlex_input_stack;
912 bool sysrooted;
913 struct script_name_list *script;
914 size_t len;
915
916 /* PR 24576: Catch the case where the user has accidentally included
917 the same linker script twice. */
918 for (script = processed_scripts; script != NULL; script = script->next)
919 {
920 if ((open_how != script_nonT || script->open_how != script_nonT)
921 && strcmp (name, script->name) == 0)
922 {
923 einfo (_("%F%P: error: linker script file '%s'"
924 " appears multiple times\n"), name);
925 return;
926 }
927 }
928
929 /* FIXME: This memory is never freed, but that should not really matter.
930 It will be released when the linker exits, and it is unlikely to ever
931 be more than a few tens of bytes. */
932 len = strlen (name);
933 script = xmalloc (sizeof (*script) + len);
934 script->next = processed_scripts;
935 script->open_how = open_how;
936 memcpy (script->name, name, len + 1);
937 processed_scripts = script;
938
939 ldlex_input_stack = ldfile_find_command_file (name,
940 open_how == script_defaultT,
941 &sysrooted);
942 if (ldlex_input_stack == NULL)
943 {
944 bfd_set_error (bfd_error_system_call);
945 einfo (_("%F%P: cannot open linker script file %s: %E\n"), name);
946 return;
947 }
948
949 lex_push_file (ldlex_input_stack, name, sysrooted);
950
951 lineno = 1;
952
953 saved_script_handle = ldlex_input_stack;
954 }
955
956 /* Open command file NAME in the current directory, -L directories,
957 the default script location, in that order. */
958
959 void
960 ldfile_open_command_file (const char *name)
961 {
962 ldfile_open_command_file_1 (name, script_nonT);
963 }
964
965 void
966 ldfile_open_script_file (const char *name)
967 {
968 ldfile_open_command_file_1 (name, script_T);
969 }
970
971 /* Open command file NAME at the default script location. */
972
973 void
974 ldfile_open_default_command_file (const char *name)
975 {
976 ldfile_open_command_file_1 (name, script_defaultT);
977 }
978
979 void
980 ldfile_add_arch (const char *in_name)
981 {
982 char *name = xstrdup (in_name);
983 search_arch_type *new_arch
984 = (search_arch_type *) xmalloc (sizeof (search_arch_type));
985
986 ldfile_output_machine_name = in_name;
987
988 new_arch->name = name;
989 new_arch->next = NULL;
990 while (*name)
991 {
992 *name = TOLOWER (*name);
993 name++;
994 }
995 *search_arch_tail_ptr = new_arch;
996 search_arch_tail_ptr = &new_arch->next;
997
998 }
999
1000 /* Set the output architecture. */
1001
1002 void
1003 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
1004 {
1005 const bfd_arch_info_type *arch = bfd_scan_arch (string);
1006
1007 if (arch)
1008 {
1009 ldfile_output_architecture = arch->arch;
1010 ldfile_output_machine = arch->mach;
1011 ldfile_output_machine_name = arch->printable_name;
1012 }
1013 else if (defarch != bfd_arch_unknown)
1014 ldfile_output_architecture = defarch;
1015 else
1016 einfo (_("%F%P: cannot represent machine `%s'\n"), string);
1017 }