]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldfile.c
Update version number on mainline
[thirdparty/binutils-gdb.git] / ld / ldfile.c
1 /* Linker file opening and searching.
2 Copyright (C) 1991-2025 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 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;
48
49 #ifdef VMS
50 static char *slash = "";
51 #else
52 #if defined (_WIN32) && !defined (__CYGWIN32__)
53 static char *slash = "\\";
54 #else
55 static char *slash = "/";
56 #endif
57 #endif
58
59 typedef struct search_arch
60 {
61 char *name;
62 struct search_arch *next;
63 } search_arch_type;
64
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;
69
70 typedef struct input_remap
71 {
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. */
75 } input_remap;
76
77 static struct input_remap * input_remaps = NULL;
78
79 void
80 ldfile_add_remap (const char * pattern, const char * renamed)
81 {
82 struct input_remap * new_entry;
83
84 new_entry = xmalloc (sizeof * new_entry);
85 new_entry->pattern = xstrdup (pattern);
86 new_entry->next = NULL;
87
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;
92 else
93 /* FIXME: Should we add sanity checking of the 'renamed' string ? */
94 new_entry->renamed = xstrdup (renamed);
95
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)
100 {
101 input_remaps = new_entry;
102 }
103 else
104 {
105 struct input_remap * i;
106
107 for (i = input_remaps; i->next != NULL; i = i->next)
108 ;
109 i->next = new_entry;
110 }
111 }
112
113 static void
114 ldfile_remap_input_free (void)
115 {
116 while (input_remaps != NULL)
117 {
118 struct input_remap * i = input_remaps;
119
120 input_remaps = i->next;
121 free ((void *) i->pattern);
122 free ((void *) i->renamed);
123 free (i);
124 }
125 }
126
127 bool
128 ldfile_add_remap_file (const char * file)
129 {
130 FILE * f;
131
132 f = fopen (file, FOPEN_RT);
133 if (f == NULL)
134 return false;
135
136 size_t linelen = 256;
137 char * line = xmalloc (linelen);
138
139 do
140 {
141 char * p = line;
142 char * q;
143
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')
148 {
149 line = xrealloc (line, 2 * linelen);
150 p = line + linelen - 1;
151 linelen += linelen;
152 }
153
154 if (q == NULL && p == line)
155 break;
156
157 p = strchr (line, '\n');
158 if (p)
159 *p = '\0';
160
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, '#');
165 if (p)
166 *p = '\0';
167
168 /* Remove leading whitespace. NUL is no whitespace character. */
169 p = line;
170 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
171 ++p;
172
173 /* If the line is blank it is ignored. */
174 if (*p == '\0')
175 continue;
176
177 char * pattern = p;
178
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')
183 ++p;
184
185 if (*p == '\0')
186 {
187 fatal ("%P: malformed remap file entry: %s\n", line);
188 continue;
189 }
190
191 * p++ = '\0';
192
193 /* Skip whitespace again. */
194 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
195 ++p;
196
197 if (*p == '\0')
198 {
199 fatal ("%P: malformed remap file entry: %s\n", line);
200 continue;
201 }
202
203 char * renamed = p;
204
205 /* Advance past the rename entry. */
206 while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
207 && *p != '\r' && *p != '\v')
208 ++p;
209 /* And terminate it. */
210 *p = '\0';
211
212 ldfile_add_remap (pattern, renamed);
213 }
214 while (! feof (f));
215
216 free (line);
217 fclose (f);
218
219 return true;
220 }
221
222 const char *
223 ldfile_possibly_remap_input (const char * filename)
224 {
225 struct input_remap * i;
226
227 if (filename == NULL)
228 return NULL;
229
230 for (i = input_remaps; i != NULL; i = i->next)
231 {
232 if (fnmatch (i->pattern, filename, 0) == 0)
233 {
234 if (verbose)
235 {
236 if (strpbrk ((i->pattern), "?*[") != NULL)
237 {
238 if (i->renamed)
239 info_msg (_("remap input file '%s' to '%s' based upon pattern '%s'\n"),
240 filename, i->renamed, i->pattern);
241 else
242 info_msg (_("remove input file '%s' based upon pattern '%s'\n"),
243 filename, i->pattern);
244 }
245 else
246 {
247 if (i->renamed)
248 info_msg (_("remap input file '%s' to '%s'\n"),
249 filename, i->renamed);
250 else
251 info_msg (_("remove input file '%s'\n"),
252 filename);
253 }
254 }
255
256 return i->renamed;
257 }
258 }
259
260 return filename;
261 }
262
263 void
264 ldfile_print_input_remaps (void)
265 {
266 if (input_remaps == NULL)
267 return;
268
269 minfo (_("\nInput File Remapping\n\n"));
270
271 struct input_remap * i;
272
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>"));
276 }
277
278
279 /* Test whether a pathname, after canonicalization, is the same or a
280 sub-directory of the sysroot directory. */
281
282 static bool
283 is_sysrooted_pathname (const char *name)
284 {
285 char *realname;
286 int len;
287 bool result;
288
289 if (ld_canon_sysroot == NULL)
290 return false;
291
292 realname = lrealpath (name);
293 len = strlen (realname);
294 result = false;
295 if (len > ld_canon_sysroot_len
296 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
297 {
298 realname[ld_canon_sysroot_len] = '\0';
299 result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
300 }
301
302 free (realname);
303 return result;
304 }
305
306 /* Adds NAME to the library search path.
307 Makes a copy of NAME using xmalloc(). */
308
309 void
310 ldfile_add_library_path (const char *name, bool cmdline)
311 {
312 search_dirs_type *new_dirs;
313
314 if (!cmdline && config.only_cmd_line_lib_dirs)
315 return;
316
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;
322
323 /* If a directory is marked as honoring sysroot, prepend the sysroot path
324 now. */
325 if (name[0] == '=')
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);
329 else
330 new_dirs->name = xstrdup (name);
331 }
332
333 static void
334 ldfile_library_path_free (search_dirs_type **root)
335 {
336 search_dirs_type *ent;
337 while ((ent = *root) != NULL)
338 {
339 *root = ent->next;
340 free ((void *) ent->name);
341 free (ent);
342 }
343 }
344
345 /* Try to open a BFD for a lang_input_statement. */
346
347 bool
348 ldfile_try_open_bfd (const char *attempt,
349 lang_input_statement_type *entry)
350 {
351 entry->the_bfd = bfd_openr (attempt, entry->target);
352
353 if (verbose)
354 {
355 if (entry->the_bfd == NULL)
356 info_msg (_("attempt to open %s failed\n"), attempt);
357 else
358 info_msg (_("attempt to open %s succeeded\n"), attempt);
359 }
360
361 if (entry->the_bfd == NULL)
362 {
363 if (bfd_get_error () == bfd_error_invalid_target)
364 fatal (_("%P: invalid BFD target `%s'\n"), entry->target);
365 return false;
366 }
367
368 /* PR 30568: Do not track lto generated temporary object files. */
369 #if BFD_SUPPORTS_PLUGINS
370 if (!entry->flags.lto_output)
371 #endif
372 track_dependency_files (attempt);
373
374 /* Linker needs to decompress sections. */
375 entry->the_bfd->flags |= BFD_DECOMPRESS;
376
377 /* This is a linker input BFD. */
378 entry->the_bfd->is_linker_input = 1;
379
380 #if BFD_SUPPORTS_PLUGINS
381 if (entry->flags.lto_output)
382 entry->the_bfd->lto_output = 1;
383 #endif
384
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
389 a dynamic object.
390
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. */
395
396 if (entry->flags.search_dirs || !entry->flags.dynamic)
397 {
398 bfd *check;
399
400 if (bfd_check_format (entry->the_bfd, bfd_archive))
401 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
402 else
403 check = entry->the_bfd;
404
405 if (check != NULL)
406 {
407 if (!bfd_check_format (check, bfd_object))
408 {
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))
413 {
414 int token, skip = 0;
415 char *arg, *arg1, *arg2, *arg3;
416 extern FILE *yyin;
417
418 /* Try to interpret the file as a linker script. */
419 ldfile_open_command_file (attempt);
420
421 ldfile_assumed_script = true;
422 parser_input = input_selected;
423 ldlex_script ();
424 token = INPUT_SCRIPT;
425 while (token != 0)
426 {
427 switch (token)
428 {
429 case OUTPUT_FORMAT:
430 if ((token = yylex ()) != '(')
431 continue;
432 if ((token = yylex ()) != NAME)
433 continue;
434 arg1 = yylval.name;
435 arg2 = NULL;
436 arg3 = NULL;
437 token = yylex ();
438 if (token == ',')
439 {
440 if ((token = yylex ()) != NAME)
441 continue;
442 arg2 = yylval.name;
443 if ((token = yylex ()) != ','
444 || (token = yylex ()) != NAME)
445 continue;
446 arg3 = yylval.name;
447 token = yylex ();
448 }
449 if (token == ')')
450 {
451 switch (command_line.endian)
452 {
453 default:
454 case ENDIAN_UNSET:
455 arg = arg1; break;
456 case ENDIAN_BIG:
457 arg = arg2 ? arg2 : arg1; break;
458 case ENDIAN_LITTLE:
459 arg = arg3 ? arg3 : arg1; break;
460 }
461 if (strcmp (arg, lang_get_output_target ()) != 0)
462 skip = 1;
463 }
464 break;
465 case NAME:
466 case LNAME:
467 case VERS_IDENTIFIER:
468 case VERS_TAG:
469 case INT:
470 break;
471 }
472 token = yylex ();
473 }
474 ldlex_popstate ();
475 ldfile_assumed_script = false;
476 fclose (yyin);
477 yyin = NULL;
478 if (skip)
479 {
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;
486 return false;
487 }
488 }
489 goto success;
490 }
491
492 if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
493 {
494 fatal (_("%P: attempted static link of dynamic object `%s'\n"),
495 attempt);
496 bfd_close (entry->the_bfd);
497 entry->the_bfd = NULL;
498 return false;
499 }
500
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)))
509 {
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;
516 return false;
517 }
518 }
519 }
520 success:
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
532 && !no_more_claiming
533 && bfd_check_format (entry->the_bfd, bfd_object))
534 plugin_maybe_claim (entry);
535 else
536 cmdline_check_object_only_section (entry->the_bfd, false);
537 #endif /* BFD_SUPPORTS_PLUGINS */
538
539 /* It opened OK, the format checked out, and the plugins have had
540 their chance to claim it, so this is success. */
541 return true;
542 }
543
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. */
546
547 bool
548 ldfile_open_file_search (const char *arch,
549 lang_input_statement_type *entry,
550 const char *lib,
551 const char *suffix)
552 {
553 search_dirs_type *search;
554
555 /* If this is not an archive, try to open it in the current
556 directory first. */
557 if (!entry->flags.maybe_archive)
558 {
559 if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
560 {
561 char *name = concat (ld_sysroot, entry->filename,
562 (const char *) NULL);
563 if (ldfile_try_open_bfd (name, entry))
564 {
565 entry->filename = name;
566 return true;
567 }
568 free (name);
569 }
570 else if (ldfile_try_open_bfd (entry->filename, entry))
571 return true;
572
573 if (IS_ABSOLUTE_PATH (entry->filename))
574 return false;
575 }
576
577 for (search = search_head; search != NULL; search = search->next)
578 {
579 char *string;
580
581 if (entry->flags.dynamic && !bfd_link_relocatable (&link_info))
582 {
583 if (ldemul_open_dynamic_archive (arch, search, entry))
584 return true;
585 }
586
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);
590 else
591 string = concat (search->name, slash, entry->filename,
592 (const char *) 0);
593
594 if (ldfile_try_open_bfd (string, entry))
595 {
596 entry->filename = string;
597 return true;
598 }
599
600 free (string);
601 }
602
603 return false;
604 }
605
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. */
610
611 void
612 ldfile_open_file (lang_input_statement_type *entry)
613 {
614 if (entry->the_bfd != NULL)
615 return;
616
617 if (!entry->flags.search_dirs)
618 {
619 if (ldfile_try_open_bfd (entry->filename, entry))
620 return;
621
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);
625 else
626 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
627
628 entry->flags.missing_file = true;
629 input_flags.missing_file = true;
630 }
631 else
632 {
633 search_arch_type *arch;
634 bool found = false;
635
636 /* If extra_search_path is set, entry->filename is a relative path.
637 Search the directory of the current linker script before searching
638 other paths. */
639 if (entry->extra_search_path)
640 {
641 char *path = concat (entry->extra_search_path, slash, entry->filename,
642 (const char *)0);
643 if (ldfile_try_open_bfd (path, entry))
644 {
645 entry->filename = path;
646 entry->flags.search_dirs = false;
647 return;
648 }
649
650 free (path);
651 }
652
653 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */
654 for (arch = search_arch_head; arch != NULL; arch = arch->next)
655 {
656 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
657 if (found)
658 break;
659 #ifdef VMS
660 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
661 if (found)
662 break;
663 #endif
664 found = ldemul_find_potential_libraries (arch->name, entry);
665 if (found)
666 break;
667 }
668
669 /* If we have found the file, we don't need to search directories
670 again. */
671 if (found)
672 entry->flags.search_dirs = false;
673 else
674 {
675 if (entry->flags.sysrooted
676 && ld_sysroot
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)
682 {
683 char * argv[4];
684 const char * res;
685 int status, err;
686
687 argv[0] = error_handling_script;
688 argv[1] = "missing-lib";
689 argv[2] = (char *) entry->local_sym_name;
690 argv[3] = NULL;
691
692 if (verbose)
693 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
694 argv[0], argv[1], argv[2]);
695
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. */,
700 &status, &err);
701 if (res != NULL)
702 {
703 einfo (_("%P: Failed to run error handling script '%s', reason: "),
704 error_handling_script);
705 /* FIXME: We assume here that errrno == err. */
706 perror (res);
707 }
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);
711 }
712 #endif
713 else
714 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
715
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);
721
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)
725 {
726 if (ldfile_open_file_search (arch->name, entry, "", ".a"))
727 {
728 const char * base = lbasename (entry->filename);
729
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;
734 break;
735 }
736 }
737
738 entry->flags.missing_file = true;
739 input_flags.missing_file = true;
740 }
741 }
742 }
743
744 /* Try to open NAME. */
745
746 static FILE *
747 try_open (const char *name, bool *sysrooted)
748 {
749 FILE *result;
750
751 result = fopen (name, "r");
752
753 if (result != NULL)
754 {
755 *sysrooted = is_sysrooted_pathname (name);
756 track_dependency_files (name);
757 }
758
759 if (verbose)
760 {
761 if (result == NULL)
762 info_msg (_("cannot find script file %s\n"), name);
763 else
764 info_msg (_("opened script file %s\n"), name);
765 }
766
767 return result;
768 }
769
770 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
771
772 static bool
773 check_for_scripts_dir (char *dir)
774 {
775 char *buf;
776 struct stat s;
777 bool res;
778
779 buf = concat (dir, "/ldscripts", (const char *) NULL);
780 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
781 free (buf);
782 return res;
783 }
784
785 /* Return the default directory for finding script files.
786 We look for the "ldscripts" directory in:
787
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). */
791
792 static char *
793 find_scripts_dir (void)
794 {
795 char *dir;
796
797 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
798 if (dir)
799 {
800 if (check_for_scripts_dir (dir))
801 return dir;
802 free (dir);
803 }
804
805 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
806 if (dir)
807 {
808 if (check_for_scripts_dir (dir))
809 return dir;
810 free (dir);
811 }
812
813 /* Look for "ldscripts" in the dir where our binary is. */
814 dir = make_relative_prefix (program_name, ".", ".");
815 if (dir)
816 {
817 if (check_for_scripts_dir (dir))
818 return dir;
819 free (dir);
820 }
821
822 return NULL;
823 }
824
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. */
829
830 static FILE *
831 ldfile_find_command_file (const char *name,
832 bool default_only,
833 bool *sysrooted)
834 {
835 search_dirs_type *search;
836 FILE *result = NULL;
837 char *path;
838
839 if (!default_only)
840 {
841 /* First try raw name. */
842 result = try_open (name, sysrooted);
843 if (result != NULL)
844 return result;
845 }
846
847 if (!script_search)
848 {
849 char *script_dir = find_scripts_dir ();
850 if (script_dir)
851 {
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;
856 free (script_dir);
857 }
858 }
859
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;
863
864 /* Try now prefixes. */
865 for (search = default_only ? script_search : search_head;
866 search != NULL;
867 search = search->next)
868 {
869 path = concat (search->name, slash, name, (const char *) NULL);
870 result = try_open (path, sysrooted);
871 free (path);
872 if (result)
873 break;
874 }
875
876 /* Restore the original path list. */
877 *search_tail_ptr = NULL;
878
879 return result;
880 }
881
882 struct script_name_list *processed_scripts = NULL;
883 /* Open command file NAME. */
884
885 static void
886 ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
887 {
888 FILE *ldlex_input_stack;
889 bool sysrooted;
890 struct script_name_list *script;
891 size_t len;
892
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)
896 {
897 if ((open_how != script_nonT || script->open_how != script_nonT)
898 && strcmp (name, script->name) == 0)
899 {
900 fatal (_("%P: error: linker script file '%s'"
901 " appears multiple times\n"), name);
902 return;
903 }
904 }
905
906 len = strlen (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;
912
913 ldlex_input_stack = ldfile_find_command_file (name,
914 open_how == script_defaultT,
915 &sysrooted);
916 if (ldlex_input_stack == NULL)
917 {
918 bfd_set_error (bfd_error_system_call);
919 fatal (_("%P: cannot open linker script file %s: %E\n"), name);
920 return;
921 }
922
923 lex_push_file (ldlex_input_stack, name, sysrooted);
924
925 lineno = 1;
926
927 saved_script_handle = ldlex_input_stack;
928 }
929
930 static void
931 ldfile_script_free (struct script_name_list **root)
932 {
933 struct script_name_list *ent;
934 while ((ent = *root) != NULL)
935 {
936 *root = ent->next;
937 free (ent);
938 }
939 }
940
941 /* Open command file NAME in the current directory, -L directories,
942 the default script location, in that order. */
943
944 void
945 ldfile_open_command_file (const char *name)
946 {
947 ldfile_open_command_file_1 (name, script_nonT);
948 }
949
950 void
951 ldfile_open_script_file (const char *name)
952 {
953 ldfile_open_command_file_1 (name, script_T);
954 }
955
956 /* Open command file NAME at the default script location. */
957
958 void
959 ldfile_open_default_command_file (const char *name)
960 {
961 ldfile_open_command_file_1 (name, script_defaultT);
962 }
963
964 void
965 ldfile_add_arch (const char *in_name)
966 {
967 char *name = xstrdup (in_name);
968 search_arch_type *new_arch
969 = (search_arch_type *) xmalloc (sizeof (search_arch_type));
970
971 ldfile_output_machine_name = in_name;
972
973 new_arch->name = name;
974 new_arch->next = NULL;
975 while (*name)
976 {
977 *name = TOLOWER (*name);
978 name++;
979 }
980 *search_arch_tail_ptr = new_arch;
981 search_arch_tail_ptr = &new_arch->next;
982
983 }
984
985 static void
986 ldfile_arch_free (search_arch_type **root)
987 {
988 search_arch_type *ent;
989 while ((ent = *root) != NULL)
990 {
991 *root = ent->next;
992 free (ent->name);
993 free (ent);
994 }
995 }
996
997 /* Set the output architecture. */
998
999 void
1000 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
1001 {
1002 const bfd_arch_info_type *arch = bfd_scan_arch (string);
1003
1004 if (arch)
1005 {
1006 ldfile_output_architecture = arch->arch;
1007 ldfile_output_machine = arch->mach;
1008 ldfile_output_machine_name = arch->printable_name;
1009 }
1010 else if (defarch != bfd_arch_unknown)
1011 ldfile_output_architecture = defarch;
1012 else
1013 fatal (_("%P: cannot represent machine `%s'\n"), string);
1014 }
1015
1016 /* Tidy up memory. */
1017
1018 void
1019 ldfile_free (void)
1020 {
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);
1028 }