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"
39 const char *ldfile_input_filename;
40 bfd_boolean ldfile_assumed_script = FALSE;
41 const char *ldfile_output_machine_name = "";
42 unsigned long ldfile_output_machine;
43 enum bfd_architecture ldfile_output_architecture;
44 search_dirs_type *search_head;
50 #if defined (_WIN32) && ! defined (__CYGWIN32__)
57 /* The MPW path char is a colon. */
63 static search_dirs_type **search_tail_ptr = &search_head;
65 typedef struct search_arch {
67 struct search_arch *next;
70 static search_arch_type *search_arch_head;
71 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
73 static FILE *try_open PARAMS ((const char *name, const char *exten));
76 ldfile_add_library_path (name, cmdline)
80 search_dirs_type *new;
82 if (!cmdline && config.only_cmd_line_lib_dirs)
85 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
88 new->cmdline = cmdline;
89 *search_tail_ptr = new;
90 search_tail_ptr = &new->next;
92 /* If a directory is marked as honoring sysroot, prepend the sysroot path
94 if (new->name[0] == '=')
96 new->name = concat (ld_sysroot, &new->name[1], NULL);
97 new->sysrooted = TRUE;
100 new->sysrooted = FALSE;
103 /* Try to open a BFD for a lang_input_statement. */
106 ldfile_try_open_bfd (attempt, entry)
108 lang_input_statement_type *entry;
110 entry->the_bfd = bfd_openr (attempt, entry->target);
112 if (trace_file_tries)
114 if (entry->the_bfd == NULL)
115 info_msg (_("attempt to open %s failed\n"), attempt);
117 info_msg (_("attempt to open %s succeeded\n"), attempt);
120 if (entry->the_bfd == NULL)
122 if (bfd_get_error () == bfd_error_invalid_target)
123 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
127 /* If we are searching for this file, see if the architecture is
128 compatible with the output file. If it isn't, keep searching.
129 If we can't open the file as an object file, stop the search
132 if (entry->search_dirs_flag)
136 if (bfd_check_format (entry->the_bfd, bfd_archive))
137 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
139 check = entry->the_bfd;
143 if (! bfd_check_format (check, bfd_object))
145 if (check == entry->the_bfd
146 && bfd_get_error () == bfd_error_file_not_recognized
147 && ! ldemul_unrecognized_file (entry))
150 char *arg, *arg1, *arg2, *arg3;
153 /* Try to interpret the file as a linker script. */
154 ldfile_open_command_file (attempt);
156 ldfile_assumed_script = TRUE;
157 parser_input = input_selected;
159 token = INPUT_SCRIPT;
165 if ((token = yylex ()) != '(')
167 if ((token = yylex ()) != NAME)
175 if ((token = yylex ()) != NAME)
181 if ((token = yylex ()) != ','
182 || (token = yylex ()) != NAME)
193 switch (command_line.endian)
199 arg = arg2 ? arg2 : arg1; break;
201 arg = arg3 ? arg3 : arg1; break;
203 if (strcmp (arg, lang_get_output_target ()) != 0)
207 if (arg2) free (arg2);
208 if (arg3) free (arg3);
212 case VERS_IDENTIFIER:
217 if (yylval.bigint.str)
218 free (yylval.bigint.str);
224 ldfile_assumed_script = FALSE;
229 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
230 attempt, entry->local_sym_name);
231 bfd_close (entry->the_bfd);
232 entry->the_bfd = NULL;
239 if ((bfd_arch_get_compatible (check, output_bfd,
240 command_line.accept_unknown_input_arch) == NULL)
241 /* XCOFF archives can have 32 and 64 bit objects. */
242 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
243 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
244 && bfd_check_format (entry->the_bfd, bfd_archive)))
246 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
247 attempt, entry->local_sym_name);
248 bfd_close (entry->the_bfd);
249 entry->the_bfd = NULL;
258 /* Search for and open the file specified by ENTRY. If it is an
259 archive, use ARCH, LIB and SUFFIX to modify the file name. */
262 ldfile_open_file_search (arch, entry, lib, suffix)
264 lang_input_statement_type *entry;
268 search_dirs_type *search;
270 /* If this is not an archive, try to open it in the current
272 if (! entry->is_archive)
274 if (entry->sysrooted && entry->filename[0] == '/')
276 char *name = concat (ld_sysroot, entry->filename,
277 (const char *) NULL);
278 if (ldfile_try_open_bfd (name, entry))
280 entry->filename = name;
285 else if (ldfile_try_open_bfd (entry->filename, entry))
287 entry->sysrooted = FALSE;
292 for (search = search_head;
293 search != (search_dirs_type *) NULL;
294 search = search->next)
298 if (entry->dynamic && ! link_info.relocateable)
300 if (ldemul_open_dynamic_archive (arch, search, entry))
302 entry->sysrooted = search->sysrooted;
307 string = (char *) xmalloc (strlen (search->name)
310 + strlen (entry->filename)
315 if (entry->is_archive)
316 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
317 lib, entry->filename, arch, suffix);
318 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
319 #if defined (__MSDOS__) || defined (_WIN32)
320 || entry->filename[0] == '\\'
321 || (ISALPHA (entry->filename[0])
322 && entry->filename[1] == ':')
325 strcpy (string, entry->filename);
327 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
329 if (ldfile_try_open_bfd (string, entry))
331 entry->filename = string;
332 entry->sysrooted = search->sysrooted;
342 /* Open the input file specified by ENTRY. */
345 ldfile_open_file (entry)
346 lang_input_statement_type *entry;
348 if (entry->the_bfd != NULL)
351 if (! entry->search_dirs_flag)
353 if (ldfile_try_open_bfd (entry->filename, entry))
355 if (strcmp (entry->filename, entry->local_sym_name) != 0)
356 einfo (_("%F%P: cannot open %s for %s: %E\n"),
357 entry->filename, entry->local_sym_name);
359 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
363 search_arch_type *arch;
364 bfd_boolean found = FALSE;
366 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
367 for (arch = search_arch_head;
368 arch != (search_arch_type *) NULL;
371 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
375 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
379 found = ldemul_find_potential_libraries (arch->name, entry);
384 /* If we have found the file, we don't need to search directories
387 entry->search_dirs_flag = FALSE;
389 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
393 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
396 try_open (name, exten)
403 result = fopen (name, "r");
405 if (trace_file_tries)
408 info_msg (_("cannot find script file %s\n"), name);
410 info_msg (_("opened script file %s\n"), name);
418 sprintf (buff, "%s%s", name, exten);
419 result = fopen (buff, "r");
421 if (trace_file_tries)
424 info_msg (_("cannot find script file %s\n"), buff);
426 info_msg (_("opened script file %s\n"), buff);
433 /* Try to open NAME; if that fails, look for it in any directories
434 specified with -L, without and with EXTEND appended. */
437 ldfile_find_command_file (name, extend)
441 search_dirs_type *search;
445 /* First try raw name. */
446 result = try_open (name, "");
447 if (result == (FILE *) NULL)
449 /* Try now prefixes. */
450 for (search = search_head;
451 search != (search_dirs_type *) NULL;
452 search = search->next)
454 sprintf (buffer, "%s%s%s", search->name, slash, name);
456 result = try_open (buffer, extend);
466 ldfile_open_command_file (name)
469 FILE *ldlex_input_stack;
470 ldlex_input_stack = ldfile_find_command_file (name, "");
472 if (ldlex_input_stack == (FILE *) NULL)
474 bfd_set_error (bfd_error_system_call);
475 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
478 lex_push_file (ldlex_input_stack, name);
480 ldfile_input_filename = name;
483 saved_script_handle = ldlex_input_stack;
488 gnu960_map_archname (name)
491 struct tabentry { char *cmd_switch; char *arch; };
492 static struct tabentry arch_tab[] =
497 "KC", "mc", /* Synonym for MC */
500 "SA", "ka", /* Functionally equivalent to KA */
501 "SB", "kb", /* Functionally equivalent to KB */
506 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
508 if (! strcmp (name,tp->cmd_switch))
512 if (tp->cmd_switch == NULL)
513 einfo (_("%P%F: unknown architecture: %s\n"), name);
519 ldfile_add_arch (name)
522 search_arch_type *new =
523 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
527 if (ldfile_output_machine_name[0] != '\0')
529 einfo (_("%P%F: target architecture respecified\n"));
533 ldfile_output_machine_name = name;
536 new->next = (search_arch_type *) NULL;
537 new->name = gnu960_map_archname (name);
538 *search_arch_tail_ptr = new;
539 search_arch_tail_ptr = &new->next;
542 #else /* not GNU960 */
545 ldfile_add_arch (in_name)
548 char *name = xstrdup (in_name);
549 search_arch_type *new =
550 (search_arch_type *) xmalloc (sizeof (search_arch_type));
552 ldfile_output_machine_name = in_name;
555 new->next = (search_arch_type *) NULL;
558 *name = TOLOWER (*name);
561 *search_arch_tail_ptr = new;
562 search_arch_tail_ptr = &new->next;
567 /* Set the output architecture. */
570 ldfile_set_output_arch (string)
573 const bfd_arch_info_type *arch = bfd_scan_arch (string);
577 ldfile_output_architecture = arch->arch;
578 ldfile_output_machine = arch->mach;
579 ldfile_output_machine_name = arch->printable_name;
583 einfo (_("%P%F: cannot represent machine `%s'\n"), string);