daily update
[binutils.git] / ld / ldfile.c
bloba9a69544d33cd9f8b38c3298d7fac28bb9041db7
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. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "safe-ctype.h"
26 #include "ld.h"
27 #include "ldmisc.h"
28 #include "ldexp.h"
29 #include "ldlang.h"
30 #include "ldfile.h"
31 #include "ldmain.h"
32 #include <ldgram.h>
33 #include "ldlex.h"
34 #include "ldemul.h"
35 #include "libiberty.h"
36 #include "filenames.h"
37 #ifdef ENABLE_PLUGINS
38 #include "plugin-api.h"
39 #include "plugin.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;
49 #ifdef VMS
50 static char * slash = "";
51 #else
52 #if defined (_WIN32) && ! defined (__CYGWIN32__)
53 static char * slash = "\\";
54 #else
55 static char * slash = "/";
56 #endif
57 #endif
59 typedef struct search_arch
61 char *name;
62 struct search_arch *next;
63 } search_arch_type;
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. */
72 static bfd_boolean
73 is_sysrooted_pathname (const char *name, bfd_boolean notsame)
75 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
76 int len;
77 bfd_boolean result;
79 if (! realname)
80 return FALSE;
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)
89 result = TRUE;
90 else
91 result = FALSE;
93 if (realname)
94 free (realname);
96 return result;
99 /* Adds NAME to the library search path.
100 Makes a copy of NAME using xmalloc(). */
102 void
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)
108 return;
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
117 now. */
118 if (name[0] == '=')
120 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
121 new_dirs->sysrooted = TRUE;
123 else
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. */
132 bfd_boolean
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);
142 else
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);
150 return FALSE;
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
160 a dynamic object.
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)
169 bfd *check;
171 if (bfd_check_format (entry->the_bfd, bfd_archive))
172 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
173 else
174 check = entry->the_bfd;
176 if (check != NULL)
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))
185 int token, skip = 0;
186 char *arg, *arg1, *arg2, *arg3;
187 extern FILE *yyin;
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;
194 ldlex_both ();
195 token = INPUT_SCRIPT;
196 while (token != 0)
198 switch (token)
200 case OUTPUT_FORMAT:
201 if ((token = yylex ()) != '(')
202 continue;
203 if ((token = yylex ()) != NAME)
204 continue;
205 arg1 = yylval.name;
206 arg2 = NULL;
207 arg3 = NULL;
208 token = yylex ();
209 if (token == ',')
211 if ((token = yylex ()) != NAME)
213 free (arg1);
214 continue;
216 arg2 = yylval.name;
217 if ((token = yylex ()) != ','
218 || (token = yylex ()) != NAME)
220 free (arg1);
221 free (arg2);
222 continue;
224 arg3 = yylval.name;
225 token = yylex ();
227 if (token == ')')
229 switch (command_line.endian)
231 default:
232 case ENDIAN_UNSET:
233 arg = arg1; break;
234 case ENDIAN_BIG:
235 arg = arg2 ? arg2 : arg1; break;
236 case ENDIAN_LITTLE:
237 arg = arg3 ? arg3 : arg1; break;
239 if (strcmp (arg, lang_get_output_target ()) != 0)
240 skip = 1;
242 free (arg1);
243 if (arg2) free (arg2);
244 if (arg3) free (arg3);
245 break;
246 case NAME:
247 case LNAME:
248 case VERS_IDENTIFIER:
249 case VERS_TAG:
250 free (yylval.name);
251 break;
252 case INT:
253 if (yylval.bigint.str)
254 free (yylval.bigint.str);
255 break;
257 token = yylex ();
259 ldlex_popstate ();
260 ldfile_assumed_script = FALSE;
261 fclose (yyin);
262 yyin = NULL;
263 if (skip)
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;
271 return FALSE;
274 goto success;
277 if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
279 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
280 attempt);
281 bfd_close (entry->the_bfd);
282 entry->the_bfd = NULL;
283 return FALSE;
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;
300 return FALSE;
304 success:
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 ())
318 int fd = open (attempt, O_RDONLY | O_BINARY);
319 if (fd >= 0)
321 struct ld_plugin_input_file file;
322 int claimed = 0;
324 file.name = attempt;
325 file.offset = 0;
326 file.filesize = lseek (fd, 0, SEEK_END);
327 file.fd = fd;
328 /* We create a dummy BFD, initially empty, to house
329 whatever symbols the plugin may want to add. */
330 file.handle = plugin_get_ir_dummy_bfd (attempt, entry->the_bfd);
331 if (plugin_call_claim_file (&file, &claimed))
332 einfo (_("%P%F: %s: plugin reported error claiming file\n"),
333 plugin_error_plugin ());
334 /* fd belongs to us, not the plugin; but we don't need it. */
335 close (fd);
336 if (claimed)
338 /* Discard the real file's BFD and substitute the dummy one. */
339 bfd_close (entry->the_bfd);
340 entry->the_bfd = file.handle;
341 entry->claimed = TRUE;
342 bfd_make_readable (entry->the_bfd);
344 else
346 /* If plugin didn't claim the file, we don't need the dummy
347 bfd. Can't avoid speculatively creating it, alas. */
348 bfd_close_all_done (file.handle);
349 entry->claimed = FALSE;
353 #endif /* ENABLE_PLUGINS */
355 /* It opened OK, the format checked out, and the plugins have had
356 their chance to claim it, so this is success. */
357 return TRUE;
360 /* Search for and open the file specified by ENTRY. If it is an
361 archive, use ARCH, LIB and SUFFIX to modify the file name. */
363 bfd_boolean
364 ldfile_open_file_search (const char *arch,
365 lang_input_statement_type *entry,
366 const char *lib,
367 const char *suffix)
369 search_dirs_type *search;
371 /* If this is not an archive, try to open it in the current
372 directory first. */
373 if (! entry->maybe_archive)
375 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
377 char *name = concat (ld_sysroot, entry->filename,
378 (const char *) NULL);
379 if (ldfile_try_open_bfd (name, entry))
381 entry->filename = name;
382 return TRUE;
384 free (name);
386 else if (ldfile_try_open_bfd (entry->filename, entry))
388 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
389 && is_sysrooted_pathname (entry->filename, TRUE);
390 return TRUE;
393 if (IS_ABSOLUTE_PATH (entry->filename))
394 return FALSE;
397 for (search = search_head; search != NULL; search = search->next)
399 char *string;
401 if (entry->dynamic && ! link_info.relocatable)
403 if (ldemul_open_dynamic_archive (arch, search, entry))
405 entry->sysrooted = search->sysrooted;
406 return TRUE;
410 if (entry->maybe_archive)
411 string = concat (search->name, slash, lib, entry->filename,
412 arch, suffix, (const char *) NULL);
413 else
414 string = concat (search->name, slash, entry->filename,
415 (const char *) 0);
417 if (ldfile_try_open_bfd (string, entry))
419 entry->filename = string;
420 entry->sysrooted = search->sysrooted;
421 return TRUE;
424 free (string);
427 return FALSE;
430 /* Open the input file specified by ENTRY.
431 PR 4437: Do not stop on the first missing file, but
432 continue processing other input files in case there
433 are more errors to report. */
435 void
436 ldfile_open_file (lang_input_statement_type *entry)
438 if (entry->the_bfd != NULL)
439 return;
441 if (! entry->search_dirs_flag)
443 if (ldfile_try_open_bfd (entry->filename, entry))
444 return;
446 if (strcmp (entry->filename, entry->local_sym_name) != 0)
447 einfo (_("%P: cannot find %s (%s): %E\n"),
448 entry->filename, entry->local_sym_name);
449 else
450 einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
452 entry->missing_file = TRUE;
453 missing_file = TRUE;
455 else
457 search_arch_type *arch;
458 bfd_boolean found = FALSE;
460 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
461 for (arch = search_arch_head; arch != NULL; arch = arch->next)
463 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
464 if (found)
465 break;
466 #ifdef VMS
467 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
468 if (found)
469 break;
470 #endif
471 found = ldemul_find_potential_libraries (arch->name, entry);
472 if (found)
473 break;
476 /* If we have found the file, we don't need to search directories
477 again. */
478 if (found)
479 entry->search_dirs_flag = FALSE;
480 else
482 if (entry->sysrooted
483 && ld_sysroot
484 && IS_ABSOLUTE_PATH (entry->local_sym_name))
485 einfo (_("%P: cannot find %s inside %s\n"),
486 entry->local_sym_name, ld_sysroot);
487 else
488 einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
489 entry->missing_file = TRUE;
490 missing_file = TRUE;
495 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
497 static FILE *
498 try_open (const char *name, const char *exten)
500 FILE *result;
502 result = fopen (name, "r");
504 if (trace_file_tries)
506 if (result == NULL)
507 info_msg (_("cannot find script file %s\n"), name);
508 else
509 info_msg (_("opened script file %s\n"), name);
512 if (result != NULL)
513 return result;
515 if (*exten)
517 char *buff;
519 buff = concat (name, exten, (const char *) NULL);
520 result = fopen (buff, "r");
522 if (trace_file_tries)
524 if (result == NULL)
525 info_msg (_("cannot find script file %s\n"), buff);
526 else
527 info_msg (_("opened script file %s\n"), buff);
529 free (buff);
532 return result;
535 /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
537 static bfd_boolean
538 check_for_scripts_dir (char *dir)
540 char *buf;
541 struct stat s;
542 bfd_boolean res;
544 buf = concat (dir, "/ldscripts", (const char *) NULL);
545 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
546 free (buf);
547 return res;
550 /* Return the default directory for finding script files.
551 We look for the "ldscripts" directory in:
553 SCRIPTDIR (passed from Makefile)
554 (adjusted according to the current location of the binary)
555 the dir where this program is (for using it from the build tree). */
557 static char *
558 find_scripts_dir (void)
560 char *dir;
562 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
563 if (dir)
565 if (check_for_scripts_dir (dir))
566 return dir;
567 free (dir);
570 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
571 if (dir)
573 if (check_for_scripts_dir (dir))
574 return dir;
575 free (dir);
578 /* Look for "ldscripts" in the dir where our binary is. */
579 dir = make_relative_prefix (program_name, ".", ".");
580 if (dir)
582 if (check_for_scripts_dir (dir))
583 return dir;
584 free (dir);
587 return NULL;
590 /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
591 it in directories specified with -L, then in the default script
592 directory, without and with EXTEND appended. If DEFAULT_ONLY is
593 true, the search is restricted to the default script location. */
595 static FILE *
596 ldfile_find_command_file (const char *name, const char *extend,
597 bfd_boolean default_only)
599 search_dirs_type *search;
600 FILE *result = NULL;
601 char *buffer;
602 static search_dirs_type *script_search;
604 if (!default_only)
606 /* First try raw name. */
607 result = try_open (name, "");
608 if (result != NULL)
609 return result;
612 if (!script_search)
614 char *script_dir = find_scripts_dir ();
615 if (script_dir)
617 search_dirs_type **save_tail_ptr = search_tail_ptr;
618 search_tail_ptr = &script_search;
619 ldfile_add_library_path (script_dir, TRUE);
620 search_tail_ptr = save_tail_ptr;
624 /* Temporarily append script_search to the path list so that the
625 paths specified with -L will be searched first. */
626 *search_tail_ptr = script_search;
628 /* Try now prefixes. */
629 for (search = default_only ? script_search : search_head;
630 search != NULL;
631 search = search->next)
633 buffer = concat (search->name, slash, name, (const char *) NULL);
634 result = try_open (buffer, extend);
635 free (buffer);
636 if (result)
637 break;
640 /* Restore the original path list. */
641 *search_tail_ptr = NULL;
643 return result;
646 /* Open command file NAME. */
648 static void
649 ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
651 FILE *ldlex_input_stack;
652 ldlex_input_stack = ldfile_find_command_file (name, "", default_only);
654 if (ldlex_input_stack == NULL)
656 bfd_set_error (bfd_error_system_call);
657 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
660 lex_push_file (ldlex_input_stack, name);
662 ldfile_input_filename = name;
663 lineno = 1;
665 saved_script_handle = ldlex_input_stack;
668 /* Open command file NAME in the current directory, -L directories,
669 the default script location, in that order. */
671 void
672 ldfile_open_command_file (const char *name)
674 ldfile_open_command_file_1 (name, FALSE);
677 /* Open command file NAME at the default script location. */
679 void
680 ldfile_open_default_command_file (const char *name)
682 ldfile_open_command_file_1 (name, TRUE);
685 void
686 ldfile_add_arch (const char *in_name)
688 char *name = xstrdup (in_name);
689 search_arch_type *new_arch = (search_arch_type *)
690 xmalloc (sizeof (search_arch_type));
692 ldfile_output_machine_name = in_name;
694 new_arch->name = name;
695 new_arch->next = NULL;
696 while (*name)
698 *name = TOLOWER (*name);
699 name++;
701 *search_arch_tail_ptr = new_arch;
702 search_arch_tail_ptr = &new_arch->next;
706 /* Set the output architecture. */
708 void
709 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
711 const bfd_arch_info_type *arch = bfd_scan_arch (string);
713 if (arch)
715 ldfile_output_architecture = arch->arch;
716 ldfile_output_machine = arch->mach;
717 ldfile_output_machine_name = arch->printable_name;
719 else if (defarch != bfd_arch_unknown)
720 ldfile_output_architecture = defarch;
721 else
722 einfo (_("%P%F: cannot represent machine `%s'\n"), string);