1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
26 #include "safe-ctype.h"
36 #include "libiberty.h"
37 #include "filenames.h"
39 #include "plugin-api.h"
41 #endif /* ENABLE_PLUGINS */
43 bfd_boolean 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;
50 static char * slash = "";
52 #if defined (_WIN32) && ! defined (__CYGWIN32__)
53 static char * slash = "\\";
55 static char * slash = "/";
59 typedef struct search_arch
62 struct search_arch *next;
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;
69 /* Test whether a pathname, after canonicalization, is the same or a
70 sub-directory of the sysroot directory. */
73 is_sysrooted_pathname (const char *name, bfd_boolean notsame)
79 if (ld_canon_sysroot == NULL)
82 realname = lrealpath (name);
83 len = strlen (realname);
85 if (len == ld_canon_sysroot_len)
87 else if (len > ld_canon_sysroot_len
88 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
91 realname[ld_canon_sysroot_len] = '\0';
95 result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
101 /* Adds NAME to the library search path.
102 Makes a copy of NAME using xmalloc(). */
105 ldfile_add_library_path (const char *name, bfd_boolean cmdline)
107 search_dirs_type *new_dirs;
109 if (!cmdline && config.only_cmd_line_lib_dirs)
112 new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
113 new_dirs->next = NULL;
114 new_dirs->cmdline = cmdline;
115 *search_tail_ptr = new_dirs;
116 search_tail_ptr = &new_dirs->next;
118 /* If a directory is marked as honoring sysroot, prepend the sysroot path
122 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
123 new_dirs->sysrooted = TRUE;
127 new_dirs->name = xstrdup (name);
128 new_dirs->sysrooted = is_sysrooted_pathname (name, FALSE);
132 /* Try to open a BFD for a lang_input_statement. */
135 ldfile_try_open_bfd (const char *attempt,
136 lang_input_statement_type *entry)
138 entry->the_bfd = bfd_openr (attempt, entry->target);
140 if (trace_file_tries)
142 if (entry->the_bfd == NULL)
143 info_msg (_("attempt to open %s failed\n"), attempt);
145 info_msg (_("attempt to open %s succeeded\n"), attempt);
148 if (entry->the_bfd == NULL)
150 if (bfd_get_error () == bfd_error_invalid_target)
151 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
155 /* Linker needs to decompress sections. */
156 entry->the_bfd->flags |= BFD_DECOMPRESS;
158 /* If we are searching for this file, see if the architecture is
159 compatible with the output file. If it isn't, keep searching.
160 If we can't open the file as an object file, stop the search
161 here. If we are statically linking, ensure that we don't link
164 In the code below, it's OK to exit early if the check fails,
165 closing the checked BFD and returning FALSE, but if the BFD
166 checks out compatible, do not exit early returning TRUE, or
167 the plugins will not get a chance to claim the file. */
169 if (entry->flags.search_dirs || !entry->flags.dynamic)
173 if (bfd_check_format (entry->the_bfd, bfd_archive))
174 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
176 check = entry->the_bfd;
180 if (! bfd_check_format (check, bfd_object))
182 if (check == entry->the_bfd
183 && entry->flags.search_dirs
184 && bfd_get_error () == bfd_error_file_not_recognized
185 && ! ldemul_unrecognized_file (entry))
188 char *arg, *arg1, *arg2, *arg3;
191 /* Try to interpret the file as a linker script. */
192 ldfile_open_command_file (attempt);
194 ldfile_assumed_script = TRUE;
195 parser_input = input_selected;
197 token = INPUT_SCRIPT;
203 if ((token = yylex ()) != '(')
205 if ((token = yylex ()) != NAME)
213 if ((token = yylex ()) != NAME)
219 if ((token = yylex ()) != ','
220 || (token = yylex ()) != NAME)
231 switch (command_line.endian)
237 arg = arg2 ? arg2 : arg1; break;
239 arg = arg3 ? arg3 : arg1; break;
241 if (strcmp (arg, lang_get_output_target ()) != 0)
245 if (arg2) free (arg2);
246 if (arg3) free (arg3);
250 case VERS_IDENTIFIER:
255 if (yylval.bigint.str)
256 free (yylval.bigint.str);
262 ldfile_assumed_script = FALSE;
267 if (command_line.warn_search_mismatch)
268 einfo (_("%P: skipping incompatible %s "
269 "when searching for %s\n"),
270 attempt, entry->local_sym_name);
271 bfd_close (entry->the_bfd);
272 entry->the_bfd = NULL;
279 if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
281 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
283 bfd_close (entry->the_bfd);
284 entry->the_bfd = NULL;
288 if (entry->flags.search_dirs
289 && !bfd_arch_get_compatible (check, link_info.output_bfd,
290 command_line.accept_unknown_input_arch)
291 /* XCOFF archives can have 32 and 64 bit objects. */
292 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
293 && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour
294 && bfd_check_format (entry->the_bfd, bfd_archive)))
296 if (command_line.warn_search_mismatch)
297 einfo (_("%P: skipping incompatible %s "
298 "when searching for %s\n"),
299 attempt, entry->local_sym_name);
300 bfd_close (entry->the_bfd);
301 entry->the_bfd = NULL;
307 #ifdef ENABLE_PLUGINS
308 /* If plugins are active, they get first chance to claim
309 any successfully-opened input file. We skip archives
310 here; the plugin wants us to offer it the individual
311 members when we enumerate them, not the whole file. We
312 also ignore corefiles, because that's just weird. It is
313 a needed side-effect of calling bfd_check_format with
314 bfd_object that it sets the bfd's arch and mach, which
315 will be needed when and if we want to bfd_create a new
316 one using this one as a template. */
317 if (bfd_check_format (entry->the_bfd, bfd_object)
318 && plugin_active_plugins_p ()
319 && !no_more_claiming)
321 int fd = open (attempt, O_RDONLY | O_BINARY);
324 struct ld_plugin_input_file file;
328 file.filesize = lseek (fd, 0, SEEK_END);
330 plugin_maybe_claim (&file, entry);
333 #endif /* ENABLE_PLUGINS */
335 /* It opened OK, the format checked out, and the plugins have had
336 their chance to claim it, so this is success. */
340 /* Search for and open the file specified by ENTRY. If it is an
341 archive, use ARCH, LIB and SUFFIX to modify the file name. */
344 ldfile_open_file_search (const char *arch,
345 lang_input_statement_type *entry,
349 search_dirs_type *search;
351 /* If this is not an archive, try to open it in the current
353 if (! entry->flags.maybe_archive)
355 if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
357 char *name = concat (ld_sysroot, entry->filename,
358 (const char *) NULL);
359 if (ldfile_try_open_bfd (name, entry))
361 entry->filename = name;
366 else if (ldfile_try_open_bfd (entry->filename, entry))
368 entry->flags.sysrooted
369 = (IS_ABSOLUTE_PATH (entry->filename)
370 && is_sysrooted_pathname (entry->filename, TRUE));
374 if (IS_ABSOLUTE_PATH (entry->filename))
378 for (search = search_head; search != NULL; search = search->next)
382 if (entry->flags.dynamic && ! link_info.relocatable)
384 if (ldemul_open_dynamic_archive (arch, search, entry))
386 entry->flags.sysrooted = search->sysrooted;
391 if (entry->flags.maybe_archive)
392 string = concat (search->name, slash, lib, entry->filename,
393 arch, suffix, (const char *) NULL);
395 string = concat (search->name, slash, entry->filename,
398 if (ldfile_try_open_bfd (string, entry))
400 entry->filename = string;
401 entry->flags.sysrooted = search->sysrooted;
411 /* Open the input file specified by ENTRY.
412 PR 4437: Do not stop on the first missing file, but
413 continue processing other input files in case there
414 are more errors to report. */
417 ldfile_open_file (lang_input_statement_type *entry)
419 if (entry->the_bfd != NULL)
422 if (! entry->flags.search_dirs)
424 if (ldfile_try_open_bfd (entry->filename, entry))
427 if (filename_cmp (entry->filename, entry->local_sym_name) != 0)
428 einfo (_("%P: cannot find %s (%s): %E\n"),
429 entry->filename, entry->local_sym_name);
431 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
433 entry->flags.missing_file = TRUE;
434 input_flags.missing_file = TRUE;
438 search_arch_type *arch;
439 bfd_boolean found = FALSE;
441 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
442 for (arch = search_arch_head; arch != NULL; arch = arch->next)
444 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
448 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
452 found = ldemul_find_potential_libraries (arch->name, entry);
457 /* If we have found the file, we don't need to search directories
460 entry->flags.search_dirs = FALSE;
463 if (entry->flags.sysrooted
465 && IS_ABSOLUTE_PATH (entry->local_sym_name))
466 einfo (_("%P: cannot find %s inside %s\n"),
467 entry->local_sym_name, ld_sysroot);
469 einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
470 entry->flags.missing_file = TRUE;
471 input_flags.missing_file = TRUE;
476 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
479 try_open (const char *name, const char *exten)
483 result = fopen (name, "r");
485 if (trace_file_tries)
488 info_msg (_("cannot find script file %s\n"), name);
490 info_msg (_("opened script file %s\n"), name);
500 buff = concat (name, exten, (const char *) NULL);
501 result = fopen (buff, "r");
503 if (trace_file_tries)
506 info_msg (_("cannot find script file %s\n"), buff);
508 info_msg (_("opened script file %s\n"), buff);
516 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
519 check_for_scripts_dir (char *dir)
525 buf = concat (dir, "/ldscripts", (const char *) NULL);
526 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
531 /* Return the default directory for finding script files.
532 We look for the "ldscripts" directory in:
534 SCRIPTDIR (passed from Makefile)
535 (adjusted according to the current location of the binary)
536 the dir where this program is (for using it from the build tree). */
539 find_scripts_dir (void)
543 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
546 if (check_for_scripts_dir (dir))
551 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
554 if (check_for_scripts_dir (dir))
559 /* Look for "ldscripts" in the dir where our binary is. */
560 dir = make_relative_prefix (program_name, ".", ".");
563 if (check_for_scripts_dir (dir))
571 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
572 it in directories specified with -L, then in the default script
573 directory, without and with EXTEND appended. If DEFAULT_ONLY is
574 true, the search is restricted to the default script location. */
577 ldfile_find_command_file (const char *name, const char *extend,
578 bfd_boolean default_only)
580 search_dirs_type *search;
583 static search_dirs_type *script_search;
587 /* First try raw name. */
588 result = try_open (name, "");
595 char *script_dir = find_scripts_dir ();
598 search_dirs_type **save_tail_ptr = search_tail_ptr;
599 search_tail_ptr = &script_search;
600 ldfile_add_library_path (script_dir, TRUE);
601 search_tail_ptr = save_tail_ptr;
605 /* Temporarily append script_search to the path list so that the
606 paths specified with -L will be searched first. */
607 *search_tail_ptr = script_search;
609 /* Try now prefixes. */
610 for (search = default_only ? script_search : search_head;
612 search = search->next)
614 buffer = concat (search->name, slash, name, (const char *) NULL);
615 result = try_open (buffer, extend);
621 /* Restore the original path list. */
622 *search_tail_ptr = NULL;
627 /* Open command file NAME. */
630 ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
632 FILE *ldlex_input_stack;
633 ldlex_input_stack = ldfile_find_command_file (name, "", default_only);
635 if (ldlex_input_stack == NULL)
637 bfd_set_error (bfd_error_system_call);
638 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
641 lex_push_file (ldlex_input_stack, name);
645 saved_script_handle = ldlex_input_stack;
648 /* Open command file NAME in the current directory, -L directories,
649 the default script location, in that order. */
652 ldfile_open_command_file (const char *name)
654 ldfile_open_command_file_1 (name, FALSE);
657 /* Open command file NAME at the default script location. */
660 ldfile_open_default_command_file (const char *name)
662 ldfile_open_command_file_1 (name, TRUE);
666 ldfile_add_arch (const char *in_name)
668 char *name = xstrdup (in_name);
669 search_arch_type *new_arch = (search_arch_type *)
670 xmalloc (sizeof (search_arch_type));
672 ldfile_output_machine_name = in_name;
674 new_arch->name = name;
675 new_arch->next = NULL;
678 *name = TOLOWER (*name);
681 *search_arch_tail_ptr = new_arch;
682 search_arch_tail_ptr = &new_arch->next;
686 /* Set the output architecture. */
689 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
691 const bfd_arch_info_type *arch = bfd_scan_arch (string);
695 ldfile_output_architecture = arch->arch;
696 ldfile_output_machine = arch->mach;
697 ldfile_output_machine_name = arch->printable_name;
699 else if (defarch != bfd_arch_unknown)
700 ldfile_output_architecture = defarch;
702 einfo (_("%P%F: cannot represent machine `%s'\n"), string);