1 /* Linker file opening and searching. 2 Copyright (C) 1991-2014 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 "safe-ctype.h" 25 #include "ld.h" 26 #include "ldmisc.h" 27 #include "ldexp.h" 28 #include "ldlang.h" 29 #include "ldfile.h" 30 #include "ldmain.h" 31 #include <ldgram.h> 32 #include "ldlex.h" 33 #include "ldemul.h" 34 #include "libiberty.h" 35 #include "filenames.h" 36 #ifdef ENABLE_PLUGINS 37 #include "plugin-api.h" 38 #include "plugin.h" 39 #endif /* ENABLE_PLUGINS */ 40 41 bfd_boolean ldfile_assumed_script = FALSE; 42 const char * ldfile_output_machine_name = ""; 43 unsigned long ldfile_output_machine; 44 enum bfd_architecture ldfile_output_architecture; 45 search_dirs_type * search_head; 46 47 #ifdef VMS 48 static char * slash = ""; 49 #else 50 #if defined (_WIN32) && ! defined (__CYGWIN32__) 51 static char * slash = "\\"; 52 #else 53 static char * slash = "/"; 54 #endif 55 #endif 56 57 typedef struct search_arch 58 { 59 char *name; 60 struct search_arch *next; 61 } search_arch_type; 62 63 static search_dirs_type **search_tail_ptr = &search_head; 64 static search_arch_type *search_arch_head; 65 static search_arch_type **search_arch_tail_ptr = &search_arch_head; 66 67 /* Test whether a pathname, after canonicalization, is the same or a 68 sub-directory of the sysroot directory. */ 69 70 static bfd_boolean 71 is_sysrooted_pathname (const char *name) 72 { 73 char *realname; 74 int len; 75 bfd_boolean result; 76 77 if (ld_canon_sysroot == NULL) 78 return FALSE; 79 80 realname = lrealpath (name); 81 len = strlen (realname); 82 result = FALSE; 83 if (len > ld_canon_sysroot_len 84 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])) 85 { 86 realname[ld_canon_sysroot_len] = '\0'; 87 result = FILENAME_CMP (ld_canon_sysroot, realname) == 0; 88 } 89 90 free (realname); 91 return result; 92 } 93 94 /* Adds NAME to the library search path. 95 Makes a copy of NAME using xmalloc(). */ 96 97 void 98 ldfile_add_library_path (const char *name, bfd_boolean cmdline) 99 { 100 search_dirs_type *new_dirs; 101 102 if (!cmdline && config.only_cmd_line_lib_dirs) 103 return; 104 105 new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type)); 106 new_dirs->next = NULL; 107 new_dirs->cmdline = cmdline; 108 *search_tail_ptr = new_dirs; 109 search_tail_ptr = &new_dirs->next; 110 111 /* If a directory is marked as honoring sysroot, prepend the sysroot path 112 now. */ 113 if (name[0] == '=') 114 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL); 115 else 116 new_dirs->name = xstrdup (name); 117 118 if (command_line.warn_poison_system_directories 119 && (!strncmp (name, "/lib", 4) 120 /* TODO: This check is disabled for now due to a bunch of packages that 121 * use libtool and relink with -L/usr/lib paths (albeit after the right 122 * sysroot path). Once those are fixed we can enable. 123 * We also need to adjust it so it only rejects one or two levels deep. 124 * Gcc's internal paths also live below /usr/lib. 125 * http://crbug.com/488360 */ 126 /* || !strncmp (name, "/usr/lib", 8) */ 127 || !strncmp (name, "/usr/local/lib", 14) 128 || !strncmp (name, "/usr/X11R6/lib", 14))) 129 { 130 if (command_line.error_poison_system_directories) 131 einfo (_("%X%P: error: library search path \"%s\" is unsafe for " 132 "cross-compilation\n"), name); 133 else 134 einfo (_("%P: warning: library search path \"%s\" is unsafe for " 135 "cross-compilation\n"), name); 136 } 137 } 138 139 /* Try to open a BFD for a lang_input_statement. */ 140 141 bfd_boolean 142 ldfile_try_open_bfd (const char *attempt, 143 lang_input_statement_type *entry) 144 { 145 entry->the_bfd = bfd_openr (attempt, entry->target); 146 147 if (verbose) 148 { 149 if (entry->the_bfd == NULL) 150 info_msg (_("attempt to open %s failed\n"), attempt); 151 else 152 info_msg (_("attempt to open %s succeeded\n"), attempt); 153 } 154 155 if (entry->the_bfd == NULL) 156 { 157 if (bfd_get_error () == bfd_error_invalid_target) 158 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target); 159 return FALSE; 160 } 161 162 /* Linker needs to decompress sections. */ 163 entry->the_bfd->flags |= BFD_DECOMPRESS; 164 165 /* If we are searching for this file, see if the architecture is 166 compatible with the output file. If it isn't, keep searching. 167 If we can't open the file as an object file, stop the search 168 here. If we are statically linking, ensure that we don't link 169 a dynamic object. 170 171 In the code below, it's OK to exit early if the check fails, 172 closing the checked BFD and returning FALSE, but if the BFD 173 checks out compatible, do not exit early returning TRUE, or 174 the plugins will not get a chance to claim the file. */ 175 176 if (entry->flags.search_dirs || !entry->flags.dynamic) 177 { 178 bfd *check; 179 180 if (bfd_check_format (entry->the_bfd, bfd_archive)) 181 check = bfd_openr_next_archived_file (entry->the_bfd, NULL); 182 else 183 check = entry->the_bfd; 184 185 if (check != NULL) 186 { 187 if (! bfd_check_format (check, bfd_object)) 188 { 189 if (check == entry->the_bfd 190 && entry->flags.search_dirs 191 && bfd_get_error () == bfd_error_file_not_recognized 192 && ! ldemul_unrecognized_file (entry)) 193 { 194 int token, skip = 0; 195 char *arg, *arg1, *arg2, *arg3; 196 extern FILE *yyin; 197 198 /* Try to interpret the file as a linker script. */ 199 ldfile_open_command_file (attempt); 200 201 ldfile_assumed_script = TRUE; 202 parser_input = input_selected; 203 ldlex_both (); 204 token = INPUT_SCRIPT; 205 while (token != 0) 206 { 207 switch (token) 208 { 209 case OUTPUT_FORMAT: 210 if ((token = yylex ()) != '(') 211 continue; 212 if ((token = yylex ()) != NAME) 213 continue; 214 arg1 = yylval.name; 215 arg2 = NULL; 216 arg3 = NULL; 217 token = yylex (); 218 if (token == ',') 219 { 220 if ((token = yylex ()) != NAME) 221 { 222 free (arg1); 223 continue; 224 } 225 arg2 = yylval.name; 226 if ((token = yylex ()) != ',' 227 || (token = yylex ()) != NAME) 228 { 229 free (arg1); 230 free (arg2); 231 continue; 232 } 233 arg3 = yylval.name; 234 token = yylex (); 235 } 236 if (token == ')') 237 { 238 switch (command_line.endian) 239 { 240 default: 241 case ENDIAN_UNSET: 242 arg = arg1; break; 243 case ENDIAN_BIG: 244 arg = arg2 ? arg2 : arg1; break; 245 case ENDIAN_LITTLE: 246 arg = arg3 ? arg3 : arg1; break; 247 } 248 if (strcmp (arg, lang_get_output_target ()) != 0) 249 skip = 1; 250 } 251 free (arg1); 252 if (arg2) free (arg2); 253 if (arg3) free (arg3); 254 break; 255 case NAME: 256 case LNAME: 257 case VERS_IDENTIFIER: 258 case VERS_TAG: 259 free (yylval.name); 260 break; 261 case INT: 262 if (yylval.bigint.str) 263 free (yylval.bigint.str); 264 break; 265 } 266 token = yylex (); 267 } 268 ldlex_popstate (); 269 ldfile_assumed_script = FALSE; 270 fclose (yyin); 271 yyin = NULL; 272 if (skip) 273 { 274 if (command_line.warn_search_mismatch) 275 einfo (_("%P: skipping incompatible %s " 276 "when searching for %s\n"), 277 attempt, entry->local_sym_name); 278 bfd_close (entry->the_bfd); 279 entry->the_bfd = NULL; 280 return FALSE; 281 } 282 } 283 goto success; 284 } 285 286 if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0) 287 { 288 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"), 289 attempt); 290 bfd_close (entry->the_bfd); 291 entry->the_bfd = NULL; 292 return FALSE; 293 } 294 295 if (entry->flags.search_dirs 296 && !bfd_arch_get_compatible (check, link_info.output_bfd, 297 command_line.accept_unknown_input_arch) 298 /* XCOFF archives can have 32 and 64 bit objects. */ 299 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour 300 && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour 301 && bfd_check_format (entry->the_bfd, bfd_archive))) 302 { 303 if (command_line.warn_search_mismatch) 304 einfo (_("%P: skipping incompatible %s " 305 "when searching for %s\n"), 306 attempt, entry->local_sym_name); 307 bfd_close (entry->the_bfd); 308 entry->the_bfd = NULL; 309 return FALSE; 310 } 311 } 312 } 313 success: 314 #ifdef ENABLE_PLUGINS 315 /* If plugins are active, they get first chance to claim 316 any successfully-opened input file. We skip archives 317 here; the plugin wants us to offer it the individual 318 members when we enumerate them, not the whole file. We 319 also ignore corefiles, because that's just weird. It is 320 a needed side-effect of calling bfd_check_format with 321 bfd_object that it sets the bfd's arch and mach, which 322 will be needed when and if we want to bfd_create a new 323 one using this one as a template. */ 324 if (bfd_check_format (entry->the_bfd, bfd_object) 325 && plugin_active_plugins_p () 326 && !no_more_claiming) 327 { 328 int fd = open (attempt, O_RDONLY | O_BINARY); 329 if (fd >= 0) 330 { 331 struct ld_plugin_input_file file; 332 333 file.name = attempt; 334 file.offset = 0; 335 file.filesize = lseek (fd, 0, SEEK_END); 336 file.fd = fd; 337 plugin_maybe_claim (&file, entry); 338 } 339 } 340 #endif /* ENABLE_PLUGINS */ 341 342 /* It opened OK, the format checked out, and the plugins have had 343 their chance to claim it, so this is success. */ 344 return TRUE; 345 } 346 347 /* Search for and open the file specified by ENTRY. If it is an 348 archive, use ARCH, LIB and SUFFIX to modify the file name. */ 349 350 bfd_boolean 351 ldfile_open_file_search (const char *arch, 352 lang_input_statement_type *entry, 353 const char *lib, 354 const char *suffix) 355 { 356 search_dirs_type *search; 357 358 /* If this is not an archive, try to open it in the current 359 directory first. */ 360 if (! entry->flags.maybe_archive) 361 { 362 if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename)) 363 { 364 char *name = concat (ld_sysroot, entry->filename, 365 (const char *) NULL); 366 if (ldfile_try_open_bfd (name, entry)) 367 { 368 entry->filename = name; 369 return TRUE; 370 } 371 free (name); 372 } 373 else if (ldfile_try_open_bfd (entry->filename, entry)) 374 return TRUE; 375 376 if (IS_ABSOLUTE_PATH (entry->filename)) 377 return FALSE; 378 } 379 380 for (search = search_head; search != NULL; search = search->next) 381 { 382 char *string; 383 384 if (entry->flags.dynamic && ! link_info.relocatable) 385 { 386 if (ldemul_open_dynamic_archive (arch, search, entry)) 387 return TRUE; 388 } 389 390 if (entry->flags.maybe_archive && !entry->flags.full_name_provided) 391 string = concat (search->name, slash, lib, entry->filename, 392 arch, suffix, (const char *) NULL); 393 else 394 string = concat (search->name, slash, entry->filename, 395 (const char *) 0); 396 397 if (ldfile_try_open_bfd (string, entry)) 398 { 399 entry->filename = string; 400 return TRUE; 401 } 402 403 free (string); 404 } 405 406 return FALSE; 407 } 408 409 /* Open the input file specified by ENTRY. 410 PR 4437: Do not stop on the first missing file, but 411 continue processing other input files in case there 412 are more errors to report. */ 413 414 void 415 ldfile_open_file (lang_input_statement_type *entry) 416 { 417 if (entry->the_bfd != NULL) 418 return; 419 420 if (! entry->flags.search_dirs) 421 { 422 if (ldfile_try_open_bfd (entry->filename, entry)) 423 return; 424 425 if (filename_cmp (entry->filename, entry->local_sym_name) != 0) 426 einfo (_("%P: cannot find %s (%s): %E\n"), 427 entry->filename, entry->local_sym_name); 428 else 429 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); 430 431 entry->flags.missing_file = TRUE; 432 input_flags.missing_file = TRUE; 433 } 434 else 435 { 436 search_arch_type *arch; 437 bfd_boolean found = FALSE; 438 439 /* Try to open <filename><suffix> or lib<filename><suffix>.a */ 440 for (arch = search_arch_head; arch != NULL; arch = arch->next) 441 { 442 found = ldfile_open_file_search (arch->name, entry, "lib", ".a"); 443 if (found) 444 break; 445 #ifdef VMS 446 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a"); 447 if (found) 448 break; 449 #endif 450 found = ldemul_find_potential_libraries (arch->name, entry); 451 if (found) 452 break; 453 } 454 455 /* If we have found the file, we don't need to search directories 456 again. */ 457 if (found) 458 entry->flags.search_dirs = FALSE; 459 else 460 { 461 if (entry->flags.sysrooted 462 && ld_sysroot 463 && IS_ABSOLUTE_PATH (entry->local_sym_name)) 464 einfo (_("%P: cannot find %s inside %s\n"), 465 entry->local_sym_name, ld_sysroot); 466 else 467 einfo (_("%P: cannot find %s\n"), entry->local_sym_name); 468 entry->flags.missing_file = TRUE; 469 input_flags.missing_file = TRUE; 470 } 471 } 472 } 473 474 /* Try to open NAME. */ 475 476 static FILE * 477 try_open (const char *name, bfd_boolean *sysrooted) 478 { 479 FILE *result; 480 481 result = fopen (name, "r"); 482 483 if (result != NULL) 484 *sysrooted = is_sysrooted_pathname (name); 485 486 if (verbose) 487 { 488 if (result == NULL) 489 info_msg (_("cannot find script file %s\n"), name); 490 else 491 info_msg (_("opened script file %s\n"), name); 492 } 493 494 return result; 495 } 496 497 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */ 498 499 static bfd_boolean 500 check_for_scripts_dir (char *dir) 501 { 502 char *buf; 503 struct stat s; 504 bfd_boolean res; 505 506 buf = concat (dir, "/ldscripts", (const char *) NULL); 507 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); 508 free (buf); 509 return res; 510 } 511 512 /* Return the default directory for finding script files. 513 We look for the "ldscripts" directory in: 514 515 SCRIPTDIR (passed from Makefile) 516 (adjusted according to the current location of the binary) 517 the dir where this program is (for using it from the build tree). */ 518 519 static char * 520 find_scripts_dir (void) 521 { 522 char *dir; 523 524 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); 525 if (dir) 526 { 527 if (check_for_scripts_dir (dir)) 528 return dir; 529 free (dir); 530 } 531 532 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); 533 if (dir) 534 { 535 if (check_for_scripts_dir (dir)) 536 return dir; 537 free (dir); 538 } 539 540 /* Look for "ldscripts" in the dir where our binary is. */ 541 dir = make_relative_prefix (program_name, ".", "."); 542 if (dir) 543 { 544 if (check_for_scripts_dir (dir)) 545 return dir; 546 free (dir); 547 } 548 549 return NULL; 550 } 551 552 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for 553 it in directories specified with -L, then in the default script 554 directory. If DEFAULT_ONLY is true, the search is restricted to 555 the default script location. */ 556 557 static FILE * 558 ldfile_find_command_file (const char *name, 559 bfd_boolean default_only, 560 bfd_boolean *sysrooted) 561 { 562 search_dirs_type *search; 563 FILE *result = NULL; 564 char *path; 565 static search_dirs_type *script_search; 566 567 if (!default_only) 568 { 569 /* First try raw name. */ 570 result = try_open (name, sysrooted); 571 if (result != NULL) 572 return result; 573 } 574 575 if (!script_search) 576 { 577 char *script_dir = find_scripts_dir (); 578 if (script_dir) 579 { 580 search_dirs_type **save_tail_ptr = search_tail_ptr; 581 search_tail_ptr = &script_search; 582 ldfile_add_library_path (script_dir, TRUE); 583 search_tail_ptr = save_tail_ptr; 584 } 585 } 586 587 /* Temporarily append script_search to the path list so that the 588 paths specified with -L will be searched first. */ 589 *search_tail_ptr = script_search; 590 591 /* Try now prefixes. */ 592 for (search = default_only ? script_search : search_head; 593 search != NULL; 594 search = search->next) 595 { 596 path = concat (search->name, slash, name, (const char *) NULL); 597 result = try_open (path, sysrooted); 598 free (path); 599 if (result) 600 break; 601 } 602 603 /* Restore the original path list. */ 604 *search_tail_ptr = NULL; 605 606 return result; 607 } 608 609 /* Open command file NAME. */ 610 611 static void 612 ldfile_open_command_file_1 (const char *name, bfd_boolean default_only) 613 { 614 FILE *ldlex_input_stack; 615 bfd_boolean sysrooted; 616 617 ldlex_input_stack = ldfile_find_command_file (name, default_only, &sysrooted); 618 619 if (ldlex_input_stack == NULL) 620 { 621 bfd_set_error (bfd_error_system_call); 622 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name); 623 return; 624 } 625 626 lex_push_file (ldlex_input_stack, name, sysrooted); 627 628 lineno = 1; 629 630 saved_script_handle = ldlex_input_stack; 631 } 632 633 /* Open command file NAME in the current directory, -L directories, 634 the default script location, in that order. */ 635 636 void 637 ldfile_open_command_file (const char *name) 638 { 639 ldfile_open_command_file_1 (name, FALSE); 640 } 641 642 /* Open command file NAME at the default script location. */ 643 644 void 645 ldfile_open_default_command_file (const char *name) 646 { 647 ldfile_open_command_file_1 (name, TRUE); 648 } 649 650 void 651 ldfile_add_arch (const char *in_name) 652 { 653 char *name = xstrdup (in_name); 654 search_arch_type *new_arch = (search_arch_type *) 655 xmalloc (sizeof (search_arch_type)); 656 657 ldfile_output_machine_name = in_name; 658 659 new_arch->name = name; 660 new_arch->next = NULL; 661 while (*name) 662 { 663 *name = TOLOWER (*name); 664 name++; 665 } 666 *search_arch_tail_ptr = new_arch; 667 search_arch_tail_ptr = &new_arch->next; 668 669 } 670 671 /* Set the output architecture. */ 672 673 void 674 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch) 675 { 676 const bfd_arch_info_type *arch = bfd_scan_arch (string); 677 678 if (arch) 679 { 680 ldfile_output_architecture = arch->arch; 681 ldfile_output_machine = arch->mach; 682 ldfile_output_machine_name = arch->printable_name; 683 } 684 else if (defarch != bfd_arch_unknown) 685 ldfile_output_architecture = defarch; 686 else 687 einfo (_("%P%F: cannot represent machine `%s'\n"), string); 688 } 689