1 /* Linker file opening and searching.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
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.
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.
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. */
24 #include "safe-ctype.h"
34 #include "libiberty.h"
35 #include "filenames.h"
37 #include "plugin-api.h"
39 #endif /* ENABLE_PLUGINS */
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;
48 static char * slash = "";
50 #if defined (_WIN32) && ! defined (__CYGWIN32__)
51 static char * slash = "\\";
53 static char * slash = "/";
57 typedef struct search_arch
60 struct search_arch *next;
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;
67 /* Test whether a pathname, after canonicalization, is the same or a
68 sub-directory of the sysroot directory. */
71 is_sysrooted_pathname (const char *name)
77 if (ld_canon_sysroot == NULL)
80 realname = lrealpath (name);
81 len = strlen (realname);
83 if (len > ld_canon_sysroot_len
84 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
86 realname[ld_canon_sysroot_len] = '\0';
87 result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
94 /* Adds NAME to the library search path.
95 Makes a copy of NAME using xmalloc(). */
98 ldfile_add_library_path (const char *name, bfd_boolean cmdline)
100 search_dirs_type *new_dirs;
102 if (!cmdline && config.only_cmd_line_lib_dirs)
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;
111 /* If a directory is marked as honoring sysroot, prepend the sysroot path
114 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
116 new_dirs->name = xstrdup (name);
119 /* Try to open a BFD for a lang_input_statement. */
122 ldfile_try_open_bfd (const char *attempt,
123 lang_input_statement_type *entry)
125 entry->the_bfd = bfd_openr (attempt, entry->target);
129 if (entry->the_bfd == NULL)
130 info_msg (_("attempt to open %s failed\n"), attempt);
132 info_msg (_("attempt to open %s succeeded\n"), attempt);
135 if (entry->the_bfd == NULL)
137 if (bfd_get_error () == bfd_error_invalid_target)
138 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
142 /* Linker needs to decompress sections. */
143 entry->the_bfd->flags |= BFD_DECOMPRESS;
145 #ifdef ENABLE_PLUGINS
146 if (entry->flags.lto_output)
147 entry->the_bfd->lto_output = 1;
150 /* If we are searching for this file, see if the architecture is
151 compatible with the output file. If it isn't, keep searching.
152 If we can't open the file as an object file, stop the search
153 here. If we are statically linking, ensure that we don't link
156 In the code below, it's OK to exit early if the check fails,
157 closing the checked BFD and returning FALSE, but if the BFD
158 checks out compatible, do not exit early returning TRUE, or
159 the plugins will not get a chance to claim the file. */
161 if (entry->flags.search_dirs || !entry->flags.dynamic)
165 if (bfd_check_format (entry->the_bfd, bfd_archive))
166 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
168 check = entry->the_bfd;
172 if (! bfd_check_format (check, bfd_object))
174 if (check == entry->the_bfd
175 && entry->flags.search_dirs
176 && bfd_get_error () == bfd_error_file_not_recognized
177 && ! ldemul_unrecognized_file (entry))
180 char *arg, *arg1, *arg2, *arg3;
183 /* Try to interpret the file as a linker script. */
184 ldfile_open_command_file (attempt);
186 ldfile_assumed_script = TRUE;
187 parser_input = input_selected;
189 token = INPUT_SCRIPT;
195 if ((token = yylex ()) != '(')
197 if ((token = yylex ()) != NAME)
205 if ((token = yylex ()) != NAME)
211 if ((token = yylex ()) != ','
212 || (token = yylex ()) != NAME)
223 switch (command_line.endian)
229 arg = arg2 ? arg2 : arg1; break;
231 arg = arg3 ? arg3 : arg1; break;
233 if (strcmp (arg, lang_get_output_target ()) != 0)
237 if (arg2) free (arg2);
238 if (arg3) free (arg3);
242 case VERS_IDENTIFIER:
247 if (yylval.bigint.str)
248 free (yylval.bigint.str);
254 ldfile_assumed_script = FALSE;
259 if (command_line.warn_search_mismatch)
260 einfo (_("%P: skipping incompatible %s "
261 "when searching for %s\n"),
262 attempt, entry->local_sym_name);
263 bfd_close (entry->the_bfd);
264 entry->the_bfd = NULL;
271 if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
273 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
275 bfd_close (entry->the_bfd);
276 entry->the_bfd = NULL;
280 if (entry->flags.search_dirs
281 && !bfd_arch_get_compatible (check, link_info.output_bfd,
282 command_line.accept_unknown_input_arch)
283 /* XCOFF archives can have 32 and 64 bit objects. */
284 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
285 && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour
286 && bfd_check_format (entry->the_bfd, bfd_archive)))
288 if (command_line.warn_search_mismatch)
289 einfo (_("%P: skipping incompatible %s "
290 "when searching for %s\n"),
291 attempt, entry->local_sym_name);
292 bfd_close (entry->the_bfd);
293 entry->the_bfd = NULL;
299 #ifdef ENABLE_PLUGINS
300 /* If plugins are active, they get first chance to claim
301 any successfully-opened input file. We skip archives
302 here; the plugin wants us to offer it the individual
303 members when we enumerate them, not the whole file. We
304 also ignore corefiles, because that's just weird. It is
305 a needed side-effect of calling bfd_check_format with
306 bfd_object that it sets the bfd's arch and mach, which
307 will be needed when and if we want to bfd_create a new
308 one using this one as a template. */
309 if (link_info.lto_plugin_active
311 && bfd_check_format (entry->the_bfd, bfd_object))
312 plugin_maybe_claim (entry);
313 #endif /* ENABLE_PLUGINS */
315 /* It opened OK, the format checked out, and the plugins have had
316 their chance to claim it, so this is success. */
320 /* Search for and open the file specified by ENTRY. If it is an
321 archive, use ARCH, LIB and SUFFIX to modify the file name. */
324 ldfile_open_file_search (const char *arch,
325 lang_input_statement_type *entry,
329 search_dirs_type *search;
331 /* If this is not an archive, try to open it in the current
333 if (! entry->flags.maybe_archive)
335 if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
337 char *name = concat (ld_sysroot, entry->filename,
338 (const char *) NULL);
339 if (ldfile_try_open_bfd (name, entry))
341 entry->filename = name;
346 else if (ldfile_try_open_bfd (entry->filename, entry))
349 if (IS_ABSOLUTE_PATH (entry->filename))
353 for (search = search_head; search != NULL; search = search->next)
357 if (entry->flags.dynamic && ! link_info.relocatable)
359 if (ldemul_open_dynamic_archive (arch, search, entry))
363 if (entry->flags.maybe_archive && !entry->flags.full_name_provided)
364 string = concat (search->name, slash, lib, entry->filename,
365 arch, suffix, (const char *) NULL);
367 string = concat (search->name, slash, entry->filename,
370 if (ldfile_try_open_bfd (string, entry))
372 entry->filename = string;
382 /* Open the input file specified by ENTRY.
383 PR 4437: Do not stop on the first missing file, but
384 continue processing other input files in case there
385 are more errors to report. */
388 ldfile_open_file (lang_input_statement_type *entry)
390 if (entry->the_bfd != NULL)
393 if (! entry->flags.search_dirs)
395 if (ldfile_try_open_bfd (entry->filename, entry))
398 if (filename_cmp (entry->filename, entry->local_sym_name) != 0)
399 einfo (_("%P: cannot find %s (%s): %E\n"),
400 entry->filename, entry->local_sym_name);
402 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
404 entry->flags.missing_file = TRUE;
405 input_flags.missing_file = TRUE;
409 search_arch_type *arch;
410 bfd_boolean found = FALSE;
412 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
413 for (arch = search_arch_head; arch != NULL; arch = arch->next)
415 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
419 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
423 found = ldemul_find_potential_libraries (arch->name, entry);
428 /* If we have found the file, we don't need to search directories
431 entry->flags.search_dirs = FALSE;
434 if (entry->flags.sysrooted
436 && IS_ABSOLUTE_PATH (entry->local_sym_name))
437 einfo (_("%P: cannot find %s inside %s\n"),
438 entry->local_sym_name, ld_sysroot);
440 einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
441 entry->flags.missing_file = TRUE;
442 input_flags.missing_file = TRUE;
447 /* Try to open NAME. */
450 try_open (const char *name, bfd_boolean *sysrooted)
454 result = fopen (name, "r");
457 *sysrooted = is_sysrooted_pathname (name);
462 info_msg (_("cannot find script file %s\n"), name);
464 info_msg (_("opened script file %s\n"), name);
470 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
473 check_for_scripts_dir (char *dir)
479 buf = concat (dir, "/ldscripts", (const char *) NULL);
480 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
485 /* Return the default directory for finding script files.
486 We look for the "ldscripts" directory in:
488 SCRIPTDIR (passed from Makefile)
489 (adjusted according to the current location of the binary)
490 the dir where this program is (for using it from the build tree). */
493 find_scripts_dir (void)
497 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
500 if (check_for_scripts_dir (dir))
505 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
508 if (check_for_scripts_dir (dir))
513 /* Look for "ldscripts" in the dir where our binary is. */
514 dir = make_relative_prefix (program_name, ".", ".");
517 if (check_for_scripts_dir (dir))
525 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
526 it in directories specified with -L, then in the default script
527 directory. If DEFAULT_ONLY is true, the search is restricted to
528 the default script location. */
531 ldfile_find_command_file (const char *name,
532 bfd_boolean default_only,
533 bfd_boolean *sysrooted)
535 search_dirs_type *search;
538 static search_dirs_type *script_search;
542 /* First try raw name. */
543 result = try_open (name, sysrooted);
550 char *script_dir = find_scripts_dir ();
553 search_dirs_type **save_tail_ptr = search_tail_ptr;
554 search_tail_ptr = &script_search;
555 ldfile_add_library_path (script_dir, TRUE);
556 search_tail_ptr = save_tail_ptr;
560 /* Temporarily append script_search to the path list so that the
561 paths specified with -L will be searched first. */
562 *search_tail_ptr = script_search;
564 /* Try now prefixes. */
565 for (search = default_only ? script_search : search_head;
567 search = search->next)
569 path = concat (search->name, slash, name, (const char *) NULL);
570 result = try_open (path, sysrooted);
576 /* Restore the original path list. */
577 *search_tail_ptr = NULL;
582 /* Open command file NAME. */
585 ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
587 FILE *ldlex_input_stack;
588 bfd_boolean sysrooted;
590 ldlex_input_stack = ldfile_find_command_file (name, default_only, &sysrooted);
592 if (ldlex_input_stack == NULL)
594 bfd_set_error (bfd_error_system_call);
595 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
599 lex_push_file (ldlex_input_stack, name, sysrooted);
603 saved_script_handle = ldlex_input_stack;
606 /* Open command file NAME in the current directory, -L directories,
607 the default script location, in that order. */
610 ldfile_open_command_file (const char *name)
612 ldfile_open_command_file_1 (name, FALSE);
615 /* Open command file NAME at the default script location. */
618 ldfile_open_default_command_file (const char *name)
620 ldfile_open_command_file_1 (name, TRUE);
624 ldfile_add_arch (const char *in_name)
626 char *name = xstrdup (in_name);
627 search_arch_type *new_arch = (search_arch_type *)
628 xmalloc (sizeof (search_arch_type));
630 ldfile_output_machine_name = in_name;
632 new_arch->name = name;
633 new_arch->next = NULL;
636 *name = TOLOWER (*name);
639 *search_arch_tail_ptr = new_arch;
640 search_arch_tail_ptr = &new_arch->next;
644 /* Set the output architecture. */
647 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
649 const bfd_arch_info_type *arch = bfd_scan_arch (string);
653 ldfile_output_architecture = arch->arch;
654 ldfile_output_machine = arch->mach;
655 ldfile_output_machine_name = arch->printable_name;
657 else if (defarch != bfd_arch_unknown)
658 ldfile_output_architecture = defarch;
660 einfo (_("%P%F: cannot represent machine `%s'\n"), string);