1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD 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 2, or (at your option)
12 GLD 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 GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* ldfile.c: look after all the file stuff. */
27 #include "safe-ctype.h"
37 #include "libiberty.h"
38 #include "filenames.h"
40 const char * ldfile_input_filename;
41 bfd_boolean ldfile_assumed_script = FALSE;
42 const char * ldfile_output_machine_name = "";
43 unsigned long ldfile_output_machine;
44 enum bfd_architecture ldfile_output_architecture;
45 search_dirs_type * search_head;
51 #if defined (_WIN32) && ! defined (__CYGWIN32__)
58 /* The MPW path char is a colon. */
62 typedef struct search_arch
65 struct search_arch *next;
68 static search_dirs_type **search_tail_ptr = &search_head;
69 static search_arch_type *search_arch_head;
70 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
73 PARAMS ((const char *, const char *));
74 static bfd_boolean is_sysrooted_pathname
75 PARAMS ((const char *, bfd_boolean));
77 /* Test whether a pathname, after canonicalization, is the same or a
78 sub-directory of the sysroot directory. */
81 is_sysrooted_pathname (name, notsame)
85 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
92 len = strlen (realname);
94 if (((! notsame && len == ld_canon_sysroot_len)
95 || (len >= ld_canon_sysroot_len
96 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
97 && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
98 && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
110 ldfile_add_library_path (name, cmdline)
114 search_dirs_type *new;
116 if (!cmdline && config.only_cmd_line_lib_dirs)
119 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
122 new->cmdline = cmdline;
123 *search_tail_ptr = new;
124 search_tail_ptr = &new->next;
126 /* If a directory is marked as honoring sysroot, prepend the sysroot path
128 if (new->name[0] == '=')
130 new->name = concat (ld_sysroot, &new->name[1], NULL);
131 new->sysrooted = TRUE;
134 new->sysrooted = is_sysrooted_pathname (new->name, FALSE);
137 /* Try to open a BFD for a lang_input_statement. */
140 ldfile_try_open_bfd (attempt, entry)
142 lang_input_statement_type *entry;
144 entry->the_bfd = bfd_openr (attempt, entry->target);
146 if (trace_file_tries)
148 if (entry->the_bfd == NULL)
149 info_msg (_("attempt to open %s failed\n"), attempt);
151 info_msg (_("attempt to open %s succeeded\n"), attempt);
154 if (entry->the_bfd == NULL)
156 if (bfd_get_error () == bfd_error_invalid_target)
157 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
161 /* If we are searching for this file, see if the architecture is
162 compatible with the output file. If it isn't, keep searching.
163 If we can't open the file as an object file, stop the search
166 if (entry->search_dirs_flag)
170 if (bfd_check_format (entry->the_bfd, bfd_archive))
171 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
173 check = entry->the_bfd;
177 if (! bfd_check_format (check, bfd_object))
179 if (check == entry->the_bfd
180 && bfd_get_error () == bfd_error_file_not_recognized
181 && ! ldemul_unrecognized_file (entry))
184 char *arg, *arg1, *arg2, *arg3;
187 /* Try to interpret the file as a linker script. */
188 ldfile_open_command_file (attempt);
190 ldfile_assumed_script = TRUE;
191 parser_input = input_selected;
193 token = INPUT_SCRIPT;
199 if ((token = yylex ()) != '(')
201 if ((token = yylex ()) != NAME)
209 if ((token = yylex ()) != NAME)
215 if ((token = yylex ()) != ','
216 || (token = yylex ()) != NAME)
227 switch (command_line.endian)
233 arg = arg2 ? arg2 : arg1; break;
235 arg = arg3 ? arg3 : arg1; break;
237 if (strcmp (arg, lang_get_output_target ()) != 0)
241 if (arg2) free (arg2);
242 if (arg3) free (arg3);
246 case VERS_IDENTIFIER:
251 if (yylval.bigint.str)
252 free (yylval.bigint.str);
258 ldfile_assumed_script = FALSE;
263 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
264 attempt, entry->local_sym_name);
265 bfd_close (entry->the_bfd);
266 entry->the_bfd = NULL;
273 if ((bfd_arch_get_compatible (check, output_bfd,
274 command_line.accept_unknown_input_arch) == NULL)
275 /* XCOFF archives can have 32 and 64 bit objects. */
276 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
277 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
278 && bfd_check_format (entry->the_bfd, bfd_archive)))
280 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
281 attempt, entry->local_sym_name);
282 bfd_close (entry->the_bfd);
283 entry->the_bfd = NULL;
292 /* Search for and open the file specified by ENTRY. If it is an
293 archive, use ARCH, LIB and SUFFIX to modify the file name. */
296 ldfile_open_file_search (arch, entry, lib, suffix)
298 lang_input_statement_type *entry;
302 search_dirs_type *search;
304 /* If this is not an archive, try to open it in the current
306 if (! entry->is_archive)
308 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
310 char *name = concat (ld_sysroot, entry->filename,
311 (const char *) NULL);
312 if (ldfile_try_open_bfd (name, entry))
314 entry->filename = name;
319 else if (ldfile_try_open_bfd (entry->filename, entry))
321 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
322 && is_sysrooted_pathname (entry->filename, TRUE);
326 if (IS_ABSOLUTE_PATH (entry->filename))
330 for (search = search_head;
331 search != (search_dirs_type *) NULL;
332 search = search->next)
336 if (entry->dynamic && ! link_info.relocateable)
338 if (ldemul_open_dynamic_archive (arch, search, entry))
340 entry->sysrooted = search->sysrooted;
345 string = (char *) xmalloc (strlen (search->name)
348 + strlen (entry->filename)
353 if (entry->is_archive)
354 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
355 lib, entry->filename, arch, suffix);
357 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
359 if (ldfile_try_open_bfd (string, entry))
361 entry->filename = string;
362 entry->sysrooted = search->sysrooted;
372 /* Open the input file specified by ENTRY. */
375 ldfile_open_file (entry)
376 lang_input_statement_type *entry;
378 if (entry->the_bfd != NULL)
381 if (! entry->search_dirs_flag)
383 if (ldfile_try_open_bfd (entry->filename, entry))
385 if (strcmp (entry->filename, entry->local_sym_name) != 0)
386 einfo (_("%F%P: cannot open %s for %s: %E\n"),
387 entry->filename, entry->local_sym_name);
389 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
393 search_arch_type *arch;
394 bfd_boolean found = FALSE;
396 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
397 for (arch = search_arch_head;
398 arch != (search_arch_type *) NULL;
401 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
405 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
409 found = ldemul_find_potential_libraries (arch->name, entry);
414 /* If we have found the file, we don't need to search directories
417 entry->search_dirs_flag = FALSE;
418 else if (entry->sysrooted
420 && IS_ABSOLUTE_PATH (entry->local_sym_name))
421 einfo (_("%F%P: cannot find %s inside %s\n"),
422 entry->local_sym_name, ld_sysroot);
424 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
428 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
431 try_open (name, exten)
438 result = fopen (name, "r");
440 if (trace_file_tries)
443 info_msg (_("cannot find script file %s\n"), name);
445 info_msg (_("opened script file %s\n"), name);
453 sprintf (buff, "%s%s", name, exten);
454 result = fopen (buff, "r");
456 if (trace_file_tries)
459 info_msg (_("cannot find script file %s\n"), buff);
461 info_msg (_("opened script file %s\n"), buff);
468 /* Try to open NAME; if that fails, look for it in any directories
469 specified with -L, without and with EXTEND appended. */
472 ldfile_find_command_file (name, extend)
476 search_dirs_type *search;
480 /* First try raw name. */
481 result = try_open (name, "");
482 if (result == (FILE *) NULL)
484 /* Try now prefixes. */
485 for (search = search_head;
486 search != (search_dirs_type *) NULL;
487 search = search->next)
489 sprintf (buffer, "%s%s%s", search->name, slash, name);
491 result = try_open (buffer, extend);
501 ldfile_open_command_file (name)
504 FILE *ldlex_input_stack;
505 ldlex_input_stack = ldfile_find_command_file (name, "");
507 if (ldlex_input_stack == (FILE *) NULL)
509 bfd_set_error (bfd_error_system_call);
510 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
513 lex_push_file (ldlex_input_stack, name);
515 ldfile_input_filename = name;
518 saved_script_handle = ldlex_input_stack;
523 gnu960_map_archname (name)
526 struct tabentry { char *cmd_switch; char *arch; };
527 static struct tabentry arch_tab[] =
532 "KC", "mc", /* Synonym for MC */
535 "SA", "ka", /* Functionally equivalent to KA */
536 "SB", "kb", /* Functionally equivalent to KB */
541 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
543 if (! strcmp (name,tp->cmd_switch))
547 if (tp->cmd_switch == NULL)
548 einfo (_("%P%F: unknown architecture: %s\n"), name);
554 ldfile_add_arch (name)
557 search_arch_type *new =
558 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
562 if (ldfile_output_machine_name[0] != '\0')
564 einfo (_("%P%F: target architecture respecified\n"));
568 ldfile_output_machine_name = name;
571 new->next = (search_arch_type *) NULL;
572 new->name = gnu960_map_archname (name);
573 *search_arch_tail_ptr = new;
574 search_arch_tail_ptr = &new->next;
577 #else /* not GNU960 */
580 ldfile_add_arch (in_name)
583 char *name = xstrdup (in_name);
584 search_arch_type *new =
585 (search_arch_type *) xmalloc (sizeof (search_arch_type));
587 ldfile_output_machine_name = in_name;
590 new->next = (search_arch_type *) NULL;
593 *name = TOLOWER (*name);
596 *search_arch_tail_ptr = new;
597 search_arch_tail_ptr = &new->next;
602 /* Set the output architecture. */
605 ldfile_set_output_arch (string)
608 const bfd_arch_info_type *arch = bfd_scan_arch (string);
612 ldfile_output_architecture = arch->arch;
613 ldfile_output_machine = arch->mach;
614 ldfile_output_machine_name = arch->printable_name;
618 einfo (_("%P%F: cannot represent machine `%s'\n"), string);