Fix spelling mistakes.
[binutils.git] / ld / ldfile.c
blobc4b89120eeed9860e40a07e22007e218d1da652d
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. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "safe-ctype.h"
27 #include "ld.h"
28 #include "ldmisc.h"
29 #include "ldexp.h"
30 #include "ldlang.h"
31 #include "ldfile.h"
32 #include "ldmain.h"
33 #include <ldgram.h>
34 #include "ldlex.h"
35 #include "ldemul.h"
36 #include "libiberty.h"
37 #include "filenames.h"
38 #ifdef ENABLE_PLUGINS
39 #include "plugin-api.h"
40 #include "plugin.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;
50 #ifdef VMS
51 static char * slash = "";
52 #else
53 #if defined (_WIN32) && ! defined (__CYGWIN32__)
54 static char * slash = "\\";
55 #else
56 static char * slash = "/";
57 #endif
58 #endif
60 typedef struct search_arch
62 char *name;
63 struct search_arch *next;
64 } search_arch_type;
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. */
73 static bfd_boolean
74 is_sysrooted_pathname (const char *name, bfd_boolean notsame)
76 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
77 int len;
78 bfd_boolean result;
80 if (! realname)
81 return FALSE;
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)
90 result = TRUE;
91 else
92 result = FALSE;
94 if (realname)
95 free (realname);
97 return result;
100 /* Adds NAME to the library search path.
101 Makes a copy of NAME using xmalloc(). */
103 void
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)
109 return;
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
118 now. */
119 if (name[0] == '=')
121 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
122 new_dirs->sysrooted = TRUE;
124 else
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. */
133 bfd_boolean
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);
143 else
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);
151 return FALSE;
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
161 a dynamic object.
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)
170 bfd *check;
172 if (bfd_check_format (entry->the_bfd, bfd_archive))
173 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
174 else
175 check = entry->the_bfd;
177 if (check != NULL)
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))
186 int token, skip = 0;
187 char *arg, *arg1, *arg2, *arg3;
188 extern FILE *yyin;
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;
195 ldlex_both ();
196 token = INPUT_SCRIPT;
197 while (token != 0)
199 switch (token)
201 case OUTPUT_FORMAT:
202 if ((token = yylex ()) != '(')
203 continue;
204 if ((token = yylex ()) != NAME)
205 continue;
206 arg1 = yylval.name;
207 arg2 = NULL;
208 arg3 = NULL;
209 token = yylex ();
210 if (token == ',')
212 if ((token = yylex ()) != NAME)
214 free (arg1);
215 continue;
217 arg2 = yylval.name;
218 if ((token = yylex ()) != ','
219 || (token = yylex ()) != NAME)
221 free (arg1);
222 free (arg2);
223 continue;
225 arg3 = yylval.name;
226 token = yylex ();
228 if (token == ')')
230 switch (command_line.endian)
232 default:
233 case ENDIAN_UNSET:
234 arg = arg1; break;
235 case ENDIAN_BIG:
236 arg = arg2 ? arg2 : arg1; break;
237 case ENDIAN_LITTLE:
238 arg = arg3 ? arg3 : arg1; break;
240 if (strcmp (arg, lang_get_output_target ()) != 0)
241 skip = 1;
243 free (arg1);
244 if (arg2) free (arg2);
245 if (arg3) free (arg3);
246 break;
247 case NAME:
248 case LNAME:
249 case VERS_IDENTIFIER:
250 case VERS_TAG:
251 free (yylval.name);
252 break;
253 case INT:
254 if (yylval.bigint.str)
255 free (yylval.bigint.str);
256 break;
258 token = yylex ();
260 ldlex_popstate ();
261 ldfile_assumed_script = FALSE;
262 fclose (yyin);
263 yyin = NULL;
264 if (skip)
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;
272 return FALSE;
275 goto success;
278 if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
280 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
281 attempt);
282 bfd_close (entry->the_bfd);
283 entry->the_bfd = NULL;
284 return FALSE;
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;
301 return FALSE;
305 success:
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);
321 if (fd >= 0)
323 struct ld_plugin_input_file file;
325 file.name = attempt;
326 file.offset = 0;
327 file.filesize = lseek (fd, 0, SEEK_END);
328 file.fd = fd;
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. */
336 return TRUE;
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. */
342 bfd_boolean
343 ldfile_open_file_search (const char *arch,
344 lang_input_statement_type *entry,
345 const char *lib,
346 const char *suffix)
348 search_dirs_type *search;
350 /* If this is not an archive, try to open it in the current
351 directory first. */
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;
361 return TRUE;
363 free (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);
369 return TRUE;
372 if (IS_ABSOLUTE_PATH (entry->filename))
373 return FALSE;
376 for (search = search_head; search != NULL; search = search->next)
378 char *string;
380 if (entry->dynamic && ! link_info.relocatable)
382 if (ldemul_open_dynamic_archive (arch, search, entry))
384 entry->sysrooted = search->sysrooted;
385 return TRUE;
389 if (entry->maybe_archive)
390 string = concat (search->name, slash, lib, entry->filename,
391 arch, suffix, (const char *) NULL);
392 else
393 string = concat (search->name, slash, entry->filename,
394 (const char *) 0);
396 if (ldfile_try_open_bfd (string, entry))
398 entry->filename = string;
399 entry->sysrooted = search->sysrooted;
400 return TRUE;
403 free (string);
406 return FALSE;
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. */
414 void
415 ldfile_open_file (lang_input_statement_type *entry)
417 if (entry->the_bfd != NULL)
418 return;
420 if (! entry->search_dirs_flag)
422 if (ldfile_try_open_bfd (entry->filename, entry))
423 return;
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);
431 entry->missing_file = TRUE;
432 missing_file = TRUE;
434 else
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");
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;
455 /* If we have found the file, we don't need to search directories
456 again. */
457 if (found)
458 entry->search_dirs_flag = FALSE;
459 else
461 if (entry->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->missing_file = TRUE;
469 missing_file = TRUE;
474 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
476 static FILE *
477 try_open (const char *name, const char *exten)
479 FILE *result;
481 result = fopen (name, "r");
483 if (trace_file_tries)
485 if (result == NULL)
486 info_msg (_("cannot find script file %s\n"), name);
487 else
488 info_msg (_("opened script file %s\n"), name);
491 if (result != NULL)
492 return result;
494 if (*exten)
496 char *buff;
498 buff = concat (name, exten, (const char *) NULL);
499 result = fopen (buff, "r");
501 if (trace_file_tries)
503 if (result == NULL)
504 info_msg (_("cannot find script file %s\n"), buff);
505 else
506 info_msg (_("opened script file %s\n"), buff);
508 free (buff);
511 return result;
514 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
516 static bfd_boolean
517 check_for_scripts_dir (char *dir)
519 char *buf;
520 struct stat s;
521 bfd_boolean res;
523 buf = concat (dir, "/ldscripts", (const char *) NULL);
524 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
525 free (buf);
526 return res;
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). */
536 static char *
537 find_scripts_dir (void)
539 char *dir;
541 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
542 if (dir)
544 if (check_for_scripts_dir (dir))
545 return dir;
546 free (dir);
549 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
550 if (dir)
552 if (check_for_scripts_dir (dir))
553 return dir;
554 free (dir);
557 /* Look for "ldscripts" in the dir where our binary is. */
558 dir = make_relative_prefix (program_name, ".", ".");
559 if (dir)
561 if (check_for_scripts_dir (dir))
562 return dir;
563 free (dir);
566 return NULL;
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. */
574 static FILE *
575 ldfile_find_command_file (const char *name, const char *extend,
576 bfd_boolean default_only)
578 search_dirs_type *search;
579 FILE *result = NULL;
580 char *buffer;
581 static search_dirs_type *script_search;
583 if (!default_only)
585 /* First try raw name. */
586 result = try_open (name, "");
587 if (result != NULL)
588 return result;
591 if (!script_search)
593 char *script_dir = find_scripts_dir ();
594 if (script_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;
609 search != NULL;
610 search = search->next)
612 buffer = concat (search->name, slash, name, (const char *) NULL);
613 result = try_open (buffer, extend);
614 free (buffer);
615 if (result)
616 break;
619 /* Restore the original path list. */
620 *search_tail_ptr = NULL;
622 return result;
625 /* Open command file NAME. */
627 static void
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;
642 lineno = 1;
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. */
650 void
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. */
658 void
659 ldfile_open_default_command_file (const char *name)
661 ldfile_open_command_file_1 (name, TRUE);
664 void
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;
675 while (*name)
677 *name = TOLOWER (*name);
678 name++;
680 *search_arch_tail_ptr = new_arch;
681 search_arch_tail_ptr = &new_arch->next;
685 /* Set the output architecture. */
687 void
688 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
690 const bfd_arch_info_type *arch = bfd_scan_arch (string);
692 if (arch)
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;
700 else
701 einfo (_("%P%F: cannot represent machine `%s'\n"), string);