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
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 const char * ldfile_input_filename;
44 bfd_boolean ldfile_assumed_script = FALSE;
45 const char * ldfile_output_machine_name = "";
46 unsigned long ldfile_output_machine;
47 enum bfd_architecture ldfile_output_architecture;
48 search_dirs_type * search_head;
51 static char * slash = "";
53 #if defined (_WIN32) && ! defined (__CYGWIN32__)
54 static char * slash = "\\";
56 static char * slash = "/";
60 typedef struct search_arch
63 struct search_arch *next;
66 static search_dirs_type **search_tail_ptr = &search_head;
67 static search_arch_type *search_arch_head;
68 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
70 /* Test whether a pathname, after canonicalization, is the same or a
71 sub-directory of the sysroot directory. */
74 is_sysrooted_pathname (const char *name, bfd_boolean notsame)
76 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
83 len = strlen (realname);
85 if (((! notsame && len == ld_canon_sysroot_len)
86 || (len >= ld_canon_sysroot_len
87 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
88 && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
89 && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
100 /* Adds NAME to the library search path.
101 Makes a copy of NAME using xmalloc(). */
104 ldfile_add_library_path (const char *name, bfd_boolean cmdline)
106 search_dirs_type *new_dirs;
108 if (!cmdline && config.only_cmd_line_lib_dirs)
111 new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
112 new_dirs->next = NULL;
113 new_dirs->cmdline = cmdline;
114 *search_tail_ptr = new_dirs;
115 search_tail_ptr = &new_dirs->next;
117 /* If a directory is marked as honoring sysroot, prepend the sysroot path
121 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
122 new_dirs->sysrooted = TRUE;
126 new_dirs->name = xstrdup (name);
127 new_dirs->sysrooted = is_sysrooted_pathname (name, FALSE);
131 /* Try to open a BFD for a lang_input_statement. */
134 ldfile_try_open_bfd (const char *attempt,
135 lang_input_statement_type *entry)
137 entry->the_bfd = bfd_openr (attempt, entry->target);
139 if (trace_file_tries)
141 if (entry->the_bfd == NULL)
142 info_msg (_("attempt to open %s failed\n"), attempt);
144 info_msg (_("attempt to open %s succeeded\n"), attempt);
147 if (entry->the_bfd == NULL)
149 if (bfd_get_error () == bfd_error_invalid_target)
150 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
154 /* Linker needs to decompress sections. */
155 entry->the_bfd->flags |= BFD_DECOMPRESS;
157 /* If we are searching for this file, see if the architecture is
158 compatible with the output file. If it isn't, keep searching.
159 If we can't open the file as an object file, stop the search
160 here. If we are statically linking, ensure that we don't link
163 In the code below, it's OK to exit early if the check fails,
164 closing the checked BFD and returning FALSE, but if the BFD
165 checks out compatible, do not exit early returning TRUE, or
166 the plugins will not get a chance to claim the file. */
168 if (entry->search_dirs_flag || !entry->dynamic)
172 if (bfd_check_format (entry->the_bfd, bfd_archive))
173 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
175 check = entry->the_bfd;
179 if (! bfd_check_format (check, bfd_object))
181 if (check == entry->the_bfd
182 && entry->search_dirs_flag
183 && bfd_get_error () == bfd_error_file_not_recognized
184 && ! ldemul_unrecognized_file (entry))
187 char *arg, *arg1, *arg2, *arg3;
190 /* Try to interpret the file as a linker script. */
191 ldfile_open_command_file (attempt);
193 ldfile_assumed_script = TRUE;
194 parser_input = input_selected;
196 token = INPUT_SCRIPT;
202 if ((token = yylex ()) != '(')
204 if ((token = yylex ()) != NAME)
212 if ((token = yylex ()) != NAME)
218 if ((token = yylex ()) != ','
219 || (token = yylex ()) != NAME)
230 switch (command_line.endian)
236 arg = arg2 ? arg2 : arg1; break;
238 arg = arg3 ? arg3 : arg1; break;
240 if (strcmp (arg, lang_get_output_target ()) != 0)
244 if (arg2) free (arg2);
245 if (arg3) free (arg3);
249 case VERS_IDENTIFIER:
254 if (yylval.bigint.str)
255 free (yylval.bigint.str);
261 ldfile_assumed_script = FALSE;
266 if (command_line.warn_search_mismatch)
267 einfo (_("%P: skipping incompatible %s "
268 "when searching for %s\n"),
269 attempt, entry->local_sym_name);
270 bfd_close (entry->the_bfd);
271 entry->the_bfd = NULL;
278 if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
280 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
282 bfd_close (entry->the_bfd);
283 entry->the_bfd = NULL;
287 if (entry->search_dirs_flag
288 && !bfd_arch_get_compatible (check, link_info.output_bfd,
289 command_line.accept_unknown_input_arch)
290 /* XCOFF archives can have 32 and 64 bit objects. */
291 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
292 && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour
293 && bfd_check_format (entry->the_bfd, bfd_archive)))
295 if (command_line.warn_search_mismatch)
296 einfo (_("%P: skipping incompatible %s "
297 "when searching for %s\n"),
298 attempt, entry->local_sym_name);
299 bfd_close (entry->the_bfd);
300 entry->the_bfd = NULL;
306 #ifdef ENABLE_PLUGINS
307 /* If plugins are active, they get first chance to claim
308 any successfully-opened input file. We skip archives
309 here; the plugin wants us to offer it the individual
310 members when we enumerate them, not the whole file. We
311 also ignore corefiles, because that's just weird. It is
312 a needed side-effect of calling bfd_check_format with
313 bfd_object that it sets the bfd's arch and mach, which
314 will be needed when and if we want to bfd_create a new
315 one using this one as a template. */
316 if (bfd_check_format (entry->the_bfd, bfd_object)
317 && plugin_active_plugins_p ()
318 && !no_more_claiming)
320 int fd = open (attempt, O_RDONLY | O_BINARY);
323 struct ld_plugin_input_file file;
327 file.filesize = lseek (fd, 0, SEEK_END);
329 plugin_maybe_claim (&file, entry);
332 #endif /* ENABLE_PLUGINS */
334 /* It opened OK, the format checked out, and the plugins have had
335 their chance to claim it, so this is success. */
339 /* Search for and open the file specified by ENTRY. If it is an
340 archive, use ARCH, LIB and SUFFIX to modify the file name. */
343 ldfile_open_file_search (const char *arch,
344 lang_input_statement_type *entry,
348 search_dirs_type *search;
350 /* If this is not an archive, try to open it in the current
352 if (! entry->maybe_archive)
354 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
356 char *name = concat (ld_sysroot, entry->filename,
357 (const char *) NULL);
358 if (ldfile_try_open_bfd (name, entry))
360 entry->filename = name;
365 else if (ldfile_try_open_bfd (entry->filename, entry))
367 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
368 && is_sysrooted_pathname (entry->filename, TRUE);
372 if (IS_ABSOLUTE_PATH (entry->filename))
376 for (search = search_head; search != NULL; search = search->next)
380 if (entry->dynamic && ! link_info.relocatable)
382 if (ldemul_open_dynamic_archive (arch, search, entry))
384 entry->sysrooted = search->sysrooted;
389 if (entry->maybe_archive)
390 string = concat (search->name, slash, lib, entry->filename,
391 arch, suffix, (const char *) NULL);
393 string = concat (search->name, slash, entry->filename,
396 if (ldfile_try_open_bfd (string, entry))
398 entry->filename = string;
399 entry->sysrooted = search->sysrooted;
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. */
415 ldfile_open_file (lang_input_statement_type *entry)
417 if (entry->the_bfd != NULL)
420 if (! entry->search_dirs_flag)
422 if (ldfile_try_open_bfd (entry->filename, entry))
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);
429 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
431 entry->missing_file = TRUE;
436 search_arch_type *arch;
437 bfd_boolean found = FALSE;
439 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
440 for (arch = search_arch_head; arch != NULL; arch = arch->next)
442 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
446 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
450 found = ldemul_find_potential_libraries (arch->name, entry);
455 /* If we have found the file, we don't need to search directories
458 entry->search_dirs_flag = FALSE;
463 && IS_ABSOLUTE_PATH (entry->local_sym_name))
464 einfo (_("%P: cannot find %s inside %s\n"),
465 entry->local_sym_name, ld_sysroot);
467 einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
468 entry->missing_file = TRUE;
474 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
477 try_open (const char *name, const char *exten)
481 result = fopen (name, "r");
483 if (trace_file_tries)
486 info_msg (_("cannot find script file %s\n"), name);
488 info_msg (_("opened script file %s\n"), name);
498 buff = concat (name, exten, (const char *) NULL);
499 result = fopen (buff, "r");
501 if (trace_file_tries)
504 info_msg (_("cannot find script file %s\n"), buff);
506 info_msg (_("opened script file %s\n"), buff);
514 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
517 check_for_scripts_dir (char *dir)
523 buf = concat (dir, "/ldscripts", (const char *) NULL);
524 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
529 /* Return the default directory for finding script files.
530 We look for the "ldscripts" directory in:
532 SCRIPTDIR (passed from Makefile)
533 (adjusted according to the current location of the binary)
534 the dir where this program is (for using it from the build tree). */
537 find_scripts_dir (void)
541 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
544 if (check_for_scripts_dir (dir))
549 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
552 if (check_for_scripts_dir (dir))
557 /* Look for "ldscripts" in the dir where our binary is. */
558 dir = make_relative_prefix (program_name, ".", ".");
561 if (check_for_scripts_dir (dir))
569 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
570 it in directories specified with -L, then in the default script
571 directory, without and with EXTEND appended. If DEFAULT_ONLY is
572 true, the search is restricted to the default script location. */
575 ldfile_find_command_file (const char *name, const char *extend,
576 bfd_boolean default_only)
578 search_dirs_type *search;
581 static search_dirs_type *script_search;
585 /* First try raw name. */
586 result = try_open (name, "");
593 char *script_dir = find_scripts_dir ();
596 search_dirs_type **save_tail_ptr = search_tail_ptr;
597 search_tail_ptr = &script_search;
598 ldfile_add_library_path (script_dir, TRUE);
599 search_tail_ptr = save_tail_ptr;
603 /* Temporarily append script_search to the path list so that the
604 paths specified with -L will be searched first. */
605 *search_tail_ptr = script_search;
607 /* Try now prefixes. */
608 for (search = default_only ? script_search : search_head;
610 search = search->next)
612 buffer = concat (search->name, slash, name, (const char *) NULL);
613 result = try_open (buffer, extend);
619 /* Restore the original path list. */
620 *search_tail_ptr = NULL;
625 /* Open command file NAME. */
628 ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
630 FILE *ldlex_input_stack;
631 ldlex_input_stack = ldfile_find_command_file (name, "", default_only);
633 if (ldlex_input_stack == NULL)
635 bfd_set_error (bfd_error_system_call);
636 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
639 lex_push_file (ldlex_input_stack, name);
641 ldfile_input_filename = name;
644 saved_script_handle = ldlex_input_stack;
647 /* Open command file NAME in the current directory, -L directories,
648 the default script location, in that order. */
651 ldfile_open_command_file (const char *name)
653 ldfile_open_command_file_1 (name, FALSE);
656 /* Open command file NAME at the default script location. */
659 ldfile_open_default_command_file (const char *name)
661 ldfile_open_command_file_1 (name, TRUE);
665 ldfile_add_arch (const char *in_name)
667 char *name = xstrdup (in_name);
668 search_arch_type *new_arch = (search_arch_type *)
669 xmalloc (sizeof (search_arch_type));
671 ldfile_output_machine_name = in_name;
673 new_arch->name = name;
674 new_arch->next = NULL;
677 *name = TOLOWER (*name);
680 *search_arch_tail_ptr = new_arch;
681 search_arch_tail_ptr = &new_arch->next;
685 /* Set the output architecture. */
688 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
690 const bfd_arch_info_type *arch = bfd_scan_arch (string);
694 ldfile_output_architecture = arch->arch;
695 ldfile_output_machine = arch->mach;
696 ldfile_output_machine_name = arch->printable_name;
698 else if (defarch != bfd_arch_unknown)
699 ldfile_output_architecture = defarch;
701 einfo (_("%P%F: cannot represent machine `%s'\n"), string);