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 Free Software Foundation, Inc.
5 This file is part of the GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
25 #include "safe-ctype.h"
35 #include "libiberty.h"
36 #include "filenames.h"
38 #include "plugin-api.h"
40 #endif /* ENABLE_PLUGINS */
42 const char * ldfile_input_filename
;
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
)
75 char * realname
= ld_canon_sysroot
? lrealpath (name
) : NULL
;
82 len
= strlen (realname
);
84 if (((! notsame
&& len
== ld_canon_sysroot_len
)
85 || (len
>= ld_canon_sysroot_len
86 && IS_DIR_SEPARATOR (realname
[ld_canon_sysroot_len
])
87 && (realname
[ld_canon_sysroot_len
] = '\0') == '\0'))
88 && FILENAME_CMP (ld_canon_sysroot
, realname
) == 0)
99 /* Adds NAME to the library search path.
100 Makes a copy of NAME using xmalloc(). */
103 ldfile_add_library_path (const char *name
, bfd_boolean cmdline
)
105 search_dirs_type
*new_dirs
;
107 if (!cmdline
&& config
.only_cmd_line_lib_dirs
)
110 new_dirs
= (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
111 new_dirs
->next
= NULL
;
112 new_dirs
->cmdline
= cmdline
;
113 *search_tail_ptr
= new_dirs
;
114 search_tail_ptr
= &new_dirs
->next
;
116 /* If a directory is marked as honoring sysroot, prepend the sysroot path
120 new_dirs
->name
= concat (ld_sysroot
, name
+ 1, (const char *) NULL
);
121 new_dirs
->sysrooted
= TRUE
;
125 new_dirs
->name
= xstrdup (name
);
126 new_dirs
->sysrooted
= is_sysrooted_pathname (name
, FALSE
);
130 /* Try to open a BFD for a lang_input_statement. */
133 ldfile_try_open_bfd (const char *attempt
,
134 lang_input_statement_type
*entry
)
136 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
138 if (trace_file_tries
)
140 if (entry
->the_bfd
== NULL
)
141 info_msg (_("attempt to open %s failed\n"), attempt
);
143 info_msg (_("attempt to open %s succeeded\n"), attempt
);
146 if (entry
->the_bfd
== NULL
)
148 if (bfd_get_error () == bfd_error_invalid_target
)
149 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
153 /* Linker needs to decompress sections. */
154 entry
->the_bfd
->flags
|= BFD_DECOMPRESS
;
156 /* If we are searching for this file, see if the architecture is
157 compatible with the output file. If it isn't, keep searching.
158 If we can't open the file as an object file, stop the search
159 here. If we are statically linking, ensure that we don't link
162 In the code below, it's OK to exit early if the check fails,
163 closing the checked BFD and returning FALSE, but if the BFD
164 checks out compatible, do not exit early returning TRUE, or
165 the plugins will not get a chance to claim the file. */
167 if (entry
->search_dirs_flag
|| !entry
->dynamic
)
171 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
172 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
174 check
= entry
->the_bfd
;
178 if (! bfd_check_format (check
, bfd_object
))
180 if (check
== entry
->the_bfd
181 && entry
->search_dirs_flag
182 && bfd_get_error () == bfd_error_file_not_recognized
183 && ! ldemul_unrecognized_file (entry
))
186 char *arg
, *arg1
, *arg2
, *arg3
;
189 /* Try to interpret the file as a linker script. */
190 ldfile_open_command_file (attempt
);
192 ldfile_assumed_script
= TRUE
;
193 parser_input
= input_selected
;
195 token
= INPUT_SCRIPT
;
201 if ((token
= yylex ()) != '(')
203 if ((token
= yylex ()) != NAME
)
211 if ((token
= yylex ()) != NAME
)
217 if ((token
= yylex ()) != ','
218 || (token
= yylex ()) != NAME
)
229 switch (command_line
.endian
)
235 arg
= arg2
? arg2
: arg1
; break;
237 arg
= arg3
? arg3
: arg1
; break;
239 if (strcmp (arg
, lang_get_output_target ()) != 0)
243 if (arg2
) free (arg2
);
244 if (arg3
) free (arg3
);
248 case VERS_IDENTIFIER
:
253 if (yylval
.bigint
.str
)
254 free (yylval
.bigint
.str
);
260 ldfile_assumed_script
= FALSE
;
265 if (command_line
.warn_search_mismatch
)
266 einfo (_("%P: skipping incompatible %s "
267 "when searching for %s\n"),
268 attempt
, entry
->local_sym_name
);
269 bfd_close (entry
->the_bfd
);
270 entry
->the_bfd
= NULL
;
277 if (!entry
->dynamic
&& (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
279 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
281 bfd_close (entry
->the_bfd
);
282 entry
->the_bfd
= NULL
;
286 if (entry
->search_dirs_flag
287 && !bfd_arch_get_compatible (check
, link_info
.output_bfd
,
288 command_line
.accept_unknown_input_arch
)
289 /* XCOFF archives can have 32 and 64 bit objects. */
290 && ! (bfd_get_flavour (check
) == bfd_target_xcoff_flavour
291 && bfd_get_flavour (link_info
.output_bfd
) == bfd_target_xcoff_flavour
292 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
294 if (command_line
.warn_search_mismatch
)
295 einfo (_("%P: skipping incompatible %s "
296 "when searching for %s\n"),
297 attempt
, entry
->local_sym_name
);
298 bfd_close (entry
->the_bfd
);
299 entry
->the_bfd
= NULL
;
305 #ifdef ENABLE_PLUGINS
306 /* If plugins are active, they get first chance to claim
307 any successfully-opened input file. We skip archives
308 here; the plugin wants us to offer it the individual
309 members when we enumerate them, not the whole file. We
310 also ignore corefiles, because that's just weird. It is
311 a needed side-effect of calling bfd_check_format with
312 bfd_object that it sets the bfd's arch and mach, which
313 will be needed when and if we want to bfd_create a new
314 one using this one as a template. */
315 if (bfd_check_format (entry
->the_bfd
, bfd_object
)
316 && plugin_active_plugins_p ()
317 && !no_more_claiming
)
319 int fd
= open (attempt
, O_RDONLY
| O_BINARY
);
322 struct ld_plugin_input_file file
;
327 file
.filesize
= lseek (fd
, 0, SEEK_END
);
329 /* We create a dummy BFD, initially empty, to house
330 whatever symbols the plugin may want to add. */
331 file
.handle
= plugin_get_ir_dummy_bfd (attempt
, entry
->the_bfd
);
332 if (plugin_call_claim_file (&file
, &claimed
))
333 einfo (_("%P%F: %s: plugin reported error claiming file\n"),
334 plugin_error_plugin ());
335 /* fd belongs to us, not the plugin; but we don't need it. */
339 /* Discard the real file's BFD and substitute the dummy one. */
340 bfd_close (entry
->the_bfd
);
341 entry
->the_bfd
= file
.handle
;
342 entry
->claimed
= TRUE
;
343 bfd_make_readable (entry
->the_bfd
);
347 /* If plugin didn't claim the file, we don't need the dummy
348 bfd. Can't avoid speculatively creating it, alas. */
349 bfd_close_all_done (file
.handle
);
350 entry
->claimed
= FALSE
;
354 #endif /* ENABLE_PLUGINS */
356 /* It opened OK, the format checked out, and the plugins have had
357 their chance to claim it, so this is success. */
361 /* Search for and open the file specified by ENTRY. If it is an
362 archive, use ARCH, LIB and SUFFIX to modify the file name. */
365 ldfile_open_file_search (const char *arch
,
366 lang_input_statement_type
*entry
,
370 search_dirs_type
*search
;
372 /* If this is not an archive, try to open it in the current
374 if (! entry
->maybe_archive
)
376 if (entry
->sysrooted
&& IS_ABSOLUTE_PATH (entry
->filename
))
378 char *name
= concat (ld_sysroot
, entry
->filename
,
379 (const char *) NULL
);
380 if (ldfile_try_open_bfd (name
, entry
))
382 entry
->filename
= name
;
387 else if (ldfile_try_open_bfd (entry
->filename
, entry
))
389 entry
->sysrooted
= IS_ABSOLUTE_PATH (entry
->filename
)
390 && is_sysrooted_pathname (entry
->filename
, TRUE
);
394 if (IS_ABSOLUTE_PATH (entry
->filename
))
398 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
402 if (entry
->dynamic
&& ! link_info
.relocatable
)
404 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
406 entry
->sysrooted
= search
->sysrooted
;
411 if (entry
->maybe_archive
)
412 string
= concat (search
->name
, slash
, lib
, entry
->filename
,
413 arch
, suffix
, (const char *) NULL
);
415 string
= concat (search
->name
, slash
, entry
->filename
,
418 if (ldfile_try_open_bfd (string
, entry
))
420 entry
->filename
= string
;
421 entry
->sysrooted
= search
->sysrooted
;
431 /* Open the input file specified by ENTRY.
432 PR 4437: Do not stop on the first missing file, but
433 continue processing other input files in case there
434 are more errors to report. */
437 ldfile_open_file (lang_input_statement_type
*entry
)
439 if (entry
->the_bfd
!= NULL
)
442 if (! entry
->search_dirs_flag
)
444 if (ldfile_try_open_bfd (entry
->filename
, entry
))
447 if (filename_cmp (entry
->filename
, entry
->local_sym_name
) != 0)
448 einfo (_("%P: cannot find %s (%s): %E\n"),
449 entry
->filename
, entry
->local_sym_name
);
451 einfo (_("%P: cannot find %s: %E\n"), entry
->local_sym_name
);
453 entry
->missing_file
= TRUE
;
458 search_arch_type
*arch
;
459 bfd_boolean found
= FALSE
;
461 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
462 for (arch
= search_arch_head
; arch
!= NULL
; arch
= arch
->next
)
464 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
468 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
472 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
477 /* If we have found the file, we don't need to search directories
480 entry
->search_dirs_flag
= FALSE
;
485 && IS_ABSOLUTE_PATH (entry
->local_sym_name
))
486 einfo (_("%P: cannot find %s inside %s\n"),
487 entry
->local_sym_name
, ld_sysroot
);
489 einfo (_("%P: cannot find %s\n"), entry
->local_sym_name
);
490 entry
->missing_file
= TRUE
;
496 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
499 try_open (const char *name
, const char *exten
)
503 result
= fopen (name
, "r");
505 if (trace_file_tries
)
508 info_msg (_("cannot find script file %s\n"), name
);
510 info_msg (_("opened script file %s\n"), name
);
520 buff
= concat (name
, exten
, (const char *) NULL
);
521 result
= fopen (buff
, "r");
523 if (trace_file_tries
)
526 info_msg (_("cannot find script file %s\n"), buff
);
528 info_msg (_("opened script file %s\n"), buff
);
536 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
539 check_for_scripts_dir (char *dir
)
545 buf
= concat (dir
, "/ldscripts", (const char *) NULL
);
546 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
551 /* Return the default directory for finding script files.
552 We look for the "ldscripts" directory in:
554 SCRIPTDIR (passed from Makefile)
555 (adjusted according to the current location of the binary)
556 the dir where this program is (for using it from the build tree). */
559 find_scripts_dir (void)
563 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
566 if (check_for_scripts_dir (dir
))
571 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
574 if (check_for_scripts_dir (dir
))
579 /* Look for "ldscripts" in the dir where our binary is. */
580 dir
= make_relative_prefix (program_name
, ".", ".");
583 if (check_for_scripts_dir (dir
))
591 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
592 it in directories specified with -L, then in the default script
593 directory, without and with EXTEND appended. If DEFAULT_ONLY is
594 true, the search is restricted to the default script location. */
597 ldfile_find_command_file (const char *name
, const char *extend
,
598 bfd_boolean default_only
)
600 search_dirs_type
*search
;
603 static search_dirs_type
*script_search
;
607 /* First try raw name. */
608 result
= try_open (name
, "");
615 char *script_dir
= find_scripts_dir ();
618 search_dirs_type
**save_tail_ptr
= search_tail_ptr
;
619 search_tail_ptr
= &script_search
;
620 ldfile_add_library_path (script_dir
, TRUE
);
621 search_tail_ptr
= save_tail_ptr
;
625 /* Temporarily append script_search to the path list so that the
626 paths specified with -L will be searched first. */
627 *search_tail_ptr
= script_search
;
629 /* Try now prefixes. */
630 for (search
= default_only
? script_search
: search_head
;
632 search
= search
->next
)
634 buffer
= concat (search
->name
, slash
, name
, (const char *) NULL
);
635 result
= try_open (buffer
, extend
);
641 /* Restore the original path list. */
642 *search_tail_ptr
= NULL
;
647 /* Open command file NAME. */
650 ldfile_open_command_file_1 (const char *name
, bfd_boolean default_only
)
652 FILE *ldlex_input_stack
;
653 ldlex_input_stack
= ldfile_find_command_file (name
, "", default_only
);
655 if (ldlex_input_stack
== NULL
)
657 bfd_set_error (bfd_error_system_call
);
658 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
661 lex_push_file (ldlex_input_stack
, name
);
663 ldfile_input_filename
= name
;
666 saved_script_handle
= ldlex_input_stack
;
669 /* Open command file NAME in the current directory, -L directories,
670 the default script location, in that order. */
673 ldfile_open_command_file (const char *name
)
675 ldfile_open_command_file_1 (name
, FALSE
);
678 /* Open command file NAME at the default script location. */
681 ldfile_open_default_command_file (const char *name
)
683 ldfile_open_command_file_1 (name
, TRUE
);
687 ldfile_add_arch (const char *in_name
)
689 char *name
= xstrdup (in_name
);
690 search_arch_type
*new_arch
= (search_arch_type
*)
691 xmalloc (sizeof (search_arch_type
));
693 ldfile_output_machine_name
= in_name
;
695 new_arch
->name
= name
;
696 new_arch
->next
= NULL
;
699 *name
= TOLOWER (*name
);
702 *search_arch_tail_ptr
= new_arch
;
703 search_arch_tail_ptr
= &new_arch
->next
;
707 /* Set the output architecture. */
710 ldfile_set_output_arch (const char *string
, enum bfd_architecture defarch
)
712 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
716 ldfile_output_architecture
= arch
->arch
;
717 ldfile_output_machine
= arch
->mach
;
718 ldfile_output_machine_name
= arch
->printable_name
;
720 else if (defarch
!= bfd_arch_unknown
)
721 ldfile_output_architecture
= defarch
;
723 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);