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 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_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 308 void 309 ldfile_add_library_path (const char *name, bool cmdline) 310 { 311 search_dirs_type *new_dirs; 312 313 if (!cmdline && config.only_cmd_line_lib_dirs) 314 return; 315 316 new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type)); 317 new_dirs->next = NULL; 318 new_dirs->cmdline = cmdline; 319 *search_tail_ptr = new_dirs; 320 search_tail_ptr = &new_dirs->next; 321 322 /* If a directory is marked as honoring sysroot, prepend the sysroot path 323 now. */ 324 if (name[0] == '=') 325 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL); 326 else if (startswith (name, "$SYSROOT")) 327 new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL); 328 else 329 new_dirs->name = xstrdup (name); 330 } 331 332 /* Try to open a BFD for a lang_input_statement. */ 333 334 bool 335 ldfile_try_open_bfd (const char *attempt, 336 lang_input_statement_type *entry) 337 { 338 entry->the_bfd = bfd_openr (attempt, entry->target); 339 340 if (verbose) 341 { 342 if (entry->the_bfd == NULL) 343 info_msg (_("attempt to open %s failed\n"), attempt); 344 else 345 info_msg (_("attempt to open %s succeeded\n"), attempt); 346 } 347 348 if (entry->the_bfd == NULL) 349 { 350 if (bfd_get_error () == bfd_error_invalid_target) 351 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target); 352 return false; 353 } 354 355 /* PR 30568: Do not track lto generated temporary object files. */ 356 #if BFD_SUPPORTS_PLUGINS 357 if (!entry->flags.lto_output) 358 #endif 359 track_dependency_files (attempt); 360 361 /* Linker needs to decompress sections. */ 362 entry->the_bfd->flags |= BFD_DECOMPRESS; 363 364 /* This is a linker input BFD. */ 365 entry->the_bfd->is_linker_input = 1; 366 367 #if BFD_SUPPORTS_PLUGINS 368 if (entry->flags.lto_output) 369 entry->the_bfd->lto_output = 1; 370 #endif 371 372 /* If we are searching for this file, see if the architecture is 373 compatible with the output file. If it isn't, keep searching. 374 If we can't open the file as an object file, stop the search 375 here. If we are statically linking, ensure that we don't link 376 a dynamic object. 377 378 In the code below, it's OK to exit early if the check fails, 379 closing the checked BFD and returning false, but if the BFD 380 checks out compatible, do not exit early returning true, or 381 the plugins will not get a chance to claim the file. */ 382 383 if (entry->flags.search_dirs || !entry->flags.dynamic) 384 { 385 bfd *check; 386 387 if (bfd_check_format (entry->the_bfd, bfd_archive)) 388 check = bfd_openr_next_archived_file (entry->the_bfd, NULL); 389 else 390 check = entry->the_bfd; 391 392 if (check != NULL) 393 { 394 if (!bfd_check_format (check, bfd_object)) 395 { 396 if (check == entry->the_bfd 397 && entry->flags.search_dirs 398 && bfd_get_error () == bfd_error_file_not_recognized 399 && !ldemul_unrecognized_file (entry)) 400 { 401 int token, skip = 0; 402 char *arg, *arg1, *arg2, *arg3; 403 extern FILE *yyin; 404 405 /* Try to interpret the file as a linker script. */ 406 ldfile_open_command_file (attempt); 407 408 ldfile_assumed_script = true; 409 parser_input = input_selected; 410 ldlex_script (); 411 token = INPUT_SCRIPT; 412 while (token != 0) 413 { 414 switch (token) 415 { 416 case OUTPUT_FORMAT: 417 if ((token = yylex ()) != '(') 418 continue; 419 if ((token = yylex ()) != NAME) 420 continue; 421 arg1 = yylval.name; 422 arg2 = NULL; 423 arg3 = NULL; 424 token = yylex (); 425 if (token == ',') 426 { 427 if ((token = yylex ()) != NAME) 428 { 429 free (arg1); 430 continue; 431 } 432 arg2 = yylval.name; 433 if ((token = yylex ()) != ',' 434 || (token = yylex ()) != NAME) 435 { 436 free (arg1); 437 free (arg2); 438 continue; 439 } 440 arg3 = yylval.name; 441 token = yylex (); 442 } 443 if (token == ')') 444 { 445 switch (command_line.endian) 446 { 447 default: 448 case ENDIAN_UNSET: 449 arg = arg1; break; 450 case ENDIAN_BIG: 451 arg = arg2 ? arg2 : arg1; break; 452 case ENDIAN_LITTLE: 453 arg = arg3 ? arg3 : arg1; break; 454 } 455 if (strcmp (arg, lang_get_output_target ()) != 0) 456 skip = 1; 457 } 458 free (arg1); 459 free (arg2); 460 free (arg3); 461 break; 462 case NAME: 463 case LNAME: 464 case VERS_IDENTIFIER: 465 case VERS_TAG: 466 free (yylval.name); 467 break; 468 case INT: 469 free (yylval.bigint.str); 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 einfo (_("%F%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 #endif /* BFD_SUPPORTS_PLUGINS */ 536 537 /* It opened OK, the format checked out, and the plugins have had 538 their chance to claim it, so this is success. */ 539 return true; 540 } 541 542 /* Search for and open the file specified by ENTRY. If it is an 543 archive, use ARCH, LIB and SUFFIX to modify the file name. */ 544 545 bool 546 ldfile_open_file_search (const char *arch, 547 lang_input_statement_type *entry, 548 const char *lib, 549 const char *suffix) 550 { 551 search_dirs_type *search; 552 553 /* If this is not an archive, try to open it in the current 554 directory first. */ 555 if (!entry->flags.maybe_archive) 556 { 557 if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename)) 558 { 559 char *name = concat (ld_sysroot, entry->filename, 560 (const char *) NULL); 561 if (ldfile_try_open_bfd (name, entry)) 562 { 563 entry->filename = name; 564 return true; 565 } 566 free (name); 567 } 568 else if (ldfile_try_open_bfd (entry->filename, entry)) 569 return true; 570 571 if (IS_ABSOLUTE_PATH (entry->filename)) 572 return false; 573 } 574 575 for (search = search_head; search != NULL; search = search->next) 576 { 577 char *string; 578 579 if (entry->flags.dynamic && !bfd_link_relocatable (&link_info)) 580 { 581 if (ldemul_open_dynamic_archive (arch, search, entry)) 582 return true; 583 } 584 585 if (entry->flags.maybe_archive && !entry->flags.full_name_provided) 586 string = concat (search->name, slash, lib, entry->filename, 587 arch, suffix, (const char *) NULL); 588 else 589 string = concat (search->name, slash, entry->filename, 590 (const char *) 0); 591 592 if (ldfile_try_open_bfd (string, entry)) 593 { 594 entry->filename = string; 595 return true; 596 } 597 598 free (string); 599 } 600 601 return false; 602 } 603 604 /* Open the input file specified by ENTRY. 605 PR 4437: Do not stop on the first missing file, but 606 continue processing other input files in case there 607 are more errors to report. */ 608 609 void 610 ldfile_open_file (lang_input_statement_type *entry) 611 { 612 if (entry->the_bfd != NULL) 613 return; 614 615 if (!entry->flags.search_dirs) 616 { 617 if (ldfile_try_open_bfd (entry->filename, entry)) 618 return; 619 620 if (filename_cmp (entry->filename, entry->local_sym_name) != 0) 621 einfo (_("%P: cannot find %s (%s): %E\n"), 622 entry->filename, entry->local_sym_name); 623 else 624 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); 625 626 entry->flags.missing_file = true; 627 input_flags.missing_file = true; 628 } 629 else 630 { 631 search_arch_type *arch; 632 bool found = false; 633 634 /* If extra_search_path is set, entry->filename is a relative path. 635 Search the directory of the current linker script before searching 636 other paths. */ 637 if (entry->extra_search_path) 638 { 639 char *path = concat (entry->extra_search_path, slash, entry->filename, 640 (const char *)0); 641 if (ldfile_try_open_bfd (path, entry)) 642 { 643 entry->filename = path; 644 entry->flags.search_dirs = false; 645 return; 646 } 647 648 free (path); 649 } 650 651 /* Try to open <filename><suffix> or lib<filename><suffix>.a. */ 652 for (arch = search_arch_head; arch != NULL; arch = arch->next) 653 { 654 found = ldfile_open_file_search (arch->name, entry, "lib", ".a"); 655 if (found) 656 break; 657 #ifdef VMS 658 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a"); 659 if (found) 660 break; 661 #endif 662 found = ldemul_find_potential_libraries (arch->name, entry); 663 if (found) 664 break; 665 } 666 667 /* If we have found the file, we don't need to search directories 668 again. */ 669 if (found) 670 entry->flags.search_dirs = false; 671 else 672 { 673 if (entry->flags.sysrooted 674 && ld_sysroot 675 && IS_ABSOLUTE_PATH (entry->local_sym_name)) 676 einfo (_("%P: cannot find %s inside %s\n"), 677 entry->local_sym_name, ld_sysroot); 678 #if SUPPORT_ERROR_HANDLING_SCRIPT 679 else if (error_handling_script != NULL) 680 { 681 char * argv[4]; 682 const char * res; 683 int status, err; 684 685 argv[0] = error_handling_script; 686 argv[1] = "missing-lib"; 687 argv[2] = (char *) entry->local_sym_name; 688 argv[3] = NULL; 689 690 if (verbose) 691 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"), 692 argv[0], argv[1], argv[2]); 693 694 res = pex_one (PEX_SEARCH, error_handling_script, argv, 695 N_("error handling script"), 696 NULL /* Send stdout to random, temp file. */, 697 NULL /* Write to stderr. */, 698 &status, &err); 699 if (res != NULL) 700 { 701 einfo (_("%P: Failed to run error handling script '%s', reason: "), 702 error_handling_script); 703 /* FIXME: We assume here that errrno == err. */ 704 perror (res); 705 } 706 else /* We ignore the return status of the script 707 and always print the error message. */ 708 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); 709 } 710 #endif 711 else 712 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); 713 714 /* PR 25747: Be kind to users who forgot to add the 715 "lib" prefix to their library when it was created. */ 716 for (arch = search_arch_head; arch != NULL; arch = arch->next) 717 { 718 if (ldfile_open_file_search (arch->name, entry, "", ".a")) 719 { 720 const char * base = lbasename (entry->filename); 721 722 einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"), 723 entry->filename, base, base); 724 bfd_close (entry->the_bfd); 725 entry->the_bfd = NULL; 726 break; 727 } 728 } 729 730 entry->flags.missing_file = true; 731 input_flags.missing_file = true; 732 } 733 } 734 } 735 736 /* Try to open NAME. */ 737 738 static FILE * 739 try_open (const char *name, bool *sysrooted) 740 { 741 FILE *result; 742 743 result = fopen (name, "r"); 744 745 if (result != NULL) 746 { 747 *sysrooted = is_sysrooted_pathname (name); 748 track_dependency_files (name); 749 } 750 751 if (verbose) 752 { 753 if (result == NULL) 754 info_msg (_("cannot find script file %s\n"), name); 755 else 756 info_msg (_("opened script file %s\n"), name); 757 } 758 759 return result; 760 } 761 762 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */ 763 764 static bool 765 check_for_scripts_dir (char *dir) 766 { 767 char *buf; 768 struct stat s; 769 bool res; 770 771 buf = concat (dir, "/ldscripts", (const char *) NULL); 772 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); 773 free (buf); 774 return res; 775 } 776 777 /* Return the default directory for finding script files. 778 We look for the "ldscripts" directory in: 779 780 SCRIPTDIR (passed from Makefile) 781 (adjusted according to the current location of the binary) 782 the dir where this program is (for using it from the build tree). */ 783 784 static char * 785 find_scripts_dir (void) 786 { 787 char *dir; 788 789 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); 790 if (dir) 791 { 792 if (check_for_scripts_dir (dir)) 793 return dir; 794 free (dir); 795 } 796 797 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); 798 if (dir) 799 { 800 if (check_for_scripts_dir (dir)) 801 return dir; 802 free (dir); 803 } 804 805 /* Look for "ldscripts" in the dir where our binary is. */ 806 dir = make_relative_prefix (program_name, ".", "."); 807 if (dir) 808 { 809 if (check_for_scripts_dir (dir)) 810 return dir; 811 free (dir); 812 } 813 814 return NULL; 815 } 816 817 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for 818 it in directories specified with -L, then in the default script 819 directory. If DEFAULT_ONLY is true, the search is restricted to 820 the default script location. */ 821 822 static FILE * 823 ldfile_find_command_file (const char *name, 824 bool default_only, 825 bool *sysrooted) 826 { 827 search_dirs_type *search; 828 FILE *result = NULL; 829 char *path; 830 static search_dirs_type *script_search; 831 832 if (!default_only) 833 { 834 /* First try raw name. */ 835 result = try_open (name, sysrooted); 836 if (result != NULL) 837 return result; 838 } 839 840 if (!script_search) 841 { 842 char *script_dir = find_scripts_dir (); 843 if (script_dir) 844 { 845 search_dirs_type **save_tail_ptr = search_tail_ptr; 846 search_tail_ptr = &script_search; 847 ldfile_add_library_path (script_dir, true); 848 search_tail_ptr = save_tail_ptr; 849 } 850 } 851 852 /* Temporarily append script_search to the path list so that the 853 paths specified with -L will be searched first. */ 854 *search_tail_ptr = script_search; 855 856 /* Try now prefixes. */ 857 for (search = default_only ? script_search : search_head; 858 search != NULL; 859 search = search->next) 860 { 861 path = concat (search->name, slash, name, (const char *) NULL); 862 result = try_open (path, sysrooted); 863 free (path); 864 if (result) 865 break; 866 } 867 868 /* Restore the original path list. */ 869 *search_tail_ptr = NULL; 870 871 return result; 872 } 873 874 enum script_open_style { 875 script_nonT, 876 script_T, 877 script_defaultT 878 }; 879 880 struct script_name_list 881 { 882 struct script_name_list *next; 883 enum script_open_style open_how; 884 char name[1]; 885 }; 886 887 /* Open command file NAME. */ 888 889 static void 890 ldfile_open_command_file_1 (const char *name, enum script_open_style open_how) 891 { 892 FILE *ldlex_input_stack; 893 bool sysrooted; 894 static struct script_name_list *processed_scripts = NULL; 895 struct script_name_list *script; 896 size_t len; 897 898 /* PR 24576: Catch the case where the user has accidentally included 899 the same linker script twice. */ 900 for (script = processed_scripts; script != NULL; script = script->next) 901 { 902 if ((open_how != script_nonT || script->open_how != script_nonT) 903 && strcmp (name, script->name) == 0) 904 { 905 einfo (_("%F%P: error: linker script file '%s'" 906 " appears multiple times\n"), name); 907 return; 908 } 909 } 910 911 /* FIXME: This memory is never freed, but that should not really matter. 912 It will be released when the linker exits, and it is unlikely to ever 913 be more than a few tens of bytes. */ 914 len = strlen (name); 915 script = xmalloc (sizeof (*script) + len); 916 script->next = processed_scripts; 917 script->open_how = open_how; 918 memcpy (script->name, name, len + 1); 919 processed_scripts = script; 920 921 ldlex_input_stack = ldfile_find_command_file (name, 922 open_how == script_defaultT, 923 &sysrooted); 924 if (ldlex_input_stack == NULL) 925 { 926 bfd_set_error (bfd_error_system_call); 927 einfo (_("%F%P: cannot open linker script file %s: %E\n"), name); 928 return; 929 } 930 931 lex_push_file (ldlex_input_stack, name, sysrooted); 932 933 lineno = 1; 934 935 saved_script_handle = ldlex_input_stack; 936 } 937 938 /* Open command file NAME in the current directory, -L directories, 939 the default script location, in that order. */ 940 941 void 942 ldfile_open_command_file (const char *name) 943 { 944 ldfile_open_command_file_1 (name, script_nonT); 945 } 946 947 void 948 ldfile_open_script_file (const char *name) 949 { 950 ldfile_open_command_file_1 (name, script_T); 951 } 952 953 /* Open command file NAME at the default script location. */ 954 955 void 956 ldfile_open_default_command_file (const char *name) 957 { 958 ldfile_open_command_file_1 (name, script_defaultT); 959 } 960 961 void 962 ldfile_add_arch (const char *in_name) 963 { 964 char *name = xstrdup (in_name); 965 search_arch_type *new_arch 966 = (search_arch_type *) xmalloc (sizeof (search_arch_type)); 967 968 ldfile_output_machine_name = in_name; 969 970 new_arch->name = name; 971 new_arch->next = NULL; 972 while (*name) 973 { 974 *name = TOLOWER (*name); 975 name++; 976 } 977 *search_arch_tail_ptr = new_arch; 978 search_arch_tail_ptr = &new_arch->next; 979 980 } 981 982 /* Set the output architecture. */ 983 984 void 985 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch) 986 { 987 const bfd_arch_info_type *arch = bfd_scan_arch (string); 988 989 if (arch) 990 { 991 ldfile_output_architecture = arch->arch; 992 ldfile_output_machine = arch->mach; 993 ldfile_output_machine_name = arch->printable_name; 994 } 995 else if (defarch != bfd_arch_unknown) 996 ldfile_output_architecture = defarch; 997 else 998 einfo (_("%F%P: cannot represent machine `%s'\n"), string); 999 } 1000