1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002
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 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
;
93 /* Try to open a BFD for a lang_input_statement. */
96 ldfile_try_open_bfd (attempt
, entry
)
98 lang_input_statement_type
*entry
;
100 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
102 if (trace_file_tries
)
104 if (entry
->the_bfd
== NULL
)
105 info_msg (_("attempt to open %s failed\n"), attempt
);
107 info_msg (_("attempt to open %s succeeded\n"), attempt
);
110 if (entry
->the_bfd
== NULL
)
112 if (bfd_get_error () == bfd_error_invalid_target
)
113 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
117 /* If we are searching for this file, see if the architecture is
118 compatible with the output file. If it isn't, keep searching.
119 If we can't open the file as an object file, stop the search
122 if (entry
->search_dirs_flag
)
126 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
127 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
129 check
= entry
->the_bfd
;
133 if (! bfd_check_format (check
, bfd_object
))
135 if (check
== entry
->the_bfd
136 && bfd_get_error () == bfd_error_file_not_recognized
137 && ! ldemul_unrecognized_file (entry
))
140 char *arg
, *arg1
, *arg2
, *arg3
;
143 /* Try to interpret the file as a linker script. */
144 ldfile_open_command_file (attempt
);
146 ldfile_assumed_script
= true;
147 parser_input
= input_selected
;
149 token
= INPUT_SCRIPT
;
155 if ((token
= yylex ()) != '(')
157 if ((token
= yylex ()) != NAME
)
165 if ((token
= yylex ()) != NAME
)
171 if ((token
= yylex ()) != ','
172 || (token
= yylex ()) != NAME
)
183 switch (command_line
.endian
)
189 arg
= arg2
? arg2
: arg1
; break;
191 arg
= arg3
? arg3
: arg1
; break;
193 if (strcmp (arg
, lang_get_output_target ()) != 0)
197 if (arg2
) free (arg2
);
198 if (arg3
) free (arg3
);
202 case VERS_IDENTIFIER
:
207 if (yylval
.bigint
.str
)
208 free (yylval
.bigint
.str
);
213 ldfile_assumed_script
= false;
218 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
219 attempt
, entry
->local_sym_name
);
220 bfd_close (entry
->the_bfd
);
221 entry
->the_bfd
= NULL
;
228 if ((bfd_arch_get_compatible (check
, output_bfd
) == NULL
)
229 /* XCOFF archives can have 32 and 64 bit objects */
230 && ! (bfd_get_flavour (check
) == bfd_target_xcoff_flavour
231 && bfd_get_flavour (output_bfd
) == bfd_target_xcoff_flavour
232 && bfd_check_format (entry
->the_bfd
, bfd_archive
)))
234 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
235 attempt
, entry
->local_sym_name
);
236 bfd_close (entry
->the_bfd
);
237 entry
->the_bfd
= NULL
;
246 /* Search for and open the file specified by ENTRY. If it is an
247 archive, use ARCH, LIB and SUFFIX to modify the file name. */
250 ldfile_open_file_search (arch
, entry
, lib
, suffix
)
252 lang_input_statement_type
*entry
;
256 search_dirs_type
*search
;
258 /* If this is not an archive, try to open it in the current
260 if (! entry
->is_archive
)
262 if (ldfile_try_open_bfd (entry
->filename
, entry
))
266 for (search
= search_head
;
267 search
!= (search_dirs_type
*) NULL
;
268 search
= search
->next
)
272 if (entry
->dynamic
&& ! link_info
.relocateable
)
274 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
278 string
= (char *) xmalloc (strlen (search
->name
)
281 + strlen (entry
->filename
)
286 if (entry
->is_archive
)
287 sprintf (string
, "%s%s%s%s%s%s", search
->name
, slash
,
288 lib
, entry
->filename
, arch
, suffix
);
289 else if (entry
->filename
[0] == '/' || entry
->filename
[0] == '.'
290 #if defined (__MSDOS__) || defined (_WIN32)
291 || entry
->filename
[0] == '\\'
292 || (ISALPHA (entry
->filename
[0])
293 && entry
->filename
[1] == ':')
296 strcpy (string
, entry
->filename
);
298 sprintf (string
, "%s%s%s", search
->name
, slash
, entry
->filename
);
300 if (ldfile_try_open_bfd (string
, entry
))
302 entry
->filename
= string
;
312 /* Open the input file specified by ENTRY. */
315 ldfile_open_file (entry
)
316 lang_input_statement_type
*entry
;
318 if (entry
->the_bfd
!= NULL
)
321 if (! entry
->search_dirs_flag
)
323 if (ldfile_try_open_bfd (entry
->filename
, entry
))
325 if (strcmp (entry
->filename
, entry
->local_sym_name
) != 0)
326 einfo (_("%F%P: cannot open %s for %s: %E\n"),
327 entry
->filename
, entry
->local_sym_name
);
329 einfo (_("%F%P: cannot open %s: %E\n"), entry
->local_sym_name
);
333 search_arch_type
*arch
;
334 boolean found
= false;
336 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
337 for (arch
= search_arch_head
;
338 arch
!= (search_arch_type
*) NULL
;
341 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
345 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
349 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
354 /* If we have found the file, we don't need to search directories
357 entry
->search_dirs_flag
= false;
359 einfo (_("%F%P: cannot find %s\n"), entry
->local_sym_name
);
363 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
366 try_open (name
, exten
)
373 result
= fopen (name
, "r");
375 if (trace_file_tries
)
378 info_msg (_("cannot find script file %s\n"), name
);
380 info_msg (_("opened script file %s\n"), name
);
388 sprintf (buff
, "%s%s", name
, exten
);
389 result
= fopen (buff
, "r");
391 if (trace_file_tries
)
394 info_msg (_("cannot find script file %s\n"), buff
);
396 info_msg (_("opened script file %s\n"), buff
);
403 /* Try to open NAME; if that fails, look for it in any directories
404 specified with -L, without and with EXTEND apppended. */
407 ldfile_find_command_file (name
, extend
)
411 search_dirs_type
*search
;
415 /* First try raw name. */
416 result
= try_open (name
, "");
417 if (result
== (FILE *) NULL
)
419 /* Try now prefixes. */
420 for (search
= search_head
;
421 search
!= (search_dirs_type
*) NULL
;
422 search
= search
->next
)
424 sprintf (buffer
, "%s%s%s", search
->name
, slash
, name
);
426 result
= try_open (buffer
, extend
);
436 ldfile_open_command_file (name
)
439 FILE *ldlex_input_stack
;
440 ldlex_input_stack
= ldfile_find_command_file (name
, "");
442 if (ldlex_input_stack
== (FILE *) NULL
)
444 bfd_set_error (bfd_error_system_call
);
445 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
448 lex_push_file (ldlex_input_stack
, name
);
450 ldfile_input_filename
= name
;
453 saved_script_handle
= ldlex_input_stack
;
458 gnu960_map_archname (name
)
461 struct tabentry
{ char *cmd_switch
; char *arch
; };
462 static struct tabentry arch_tab
[] =
467 "KC", "mc", /* Synonym for MC */
470 "SA", "ka", /* Functionally equivalent to KA */
471 "SB", "kb", /* Functionally equivalent to KB */
476 for (tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++)
478 if (! strcmp (name
,tp
->cmd_switch
))
482 if (tp
->cmd_switch
== NULL
)
483 einfo (_("%P%F: unknown architecture: %s\n"), name
);
489 ldfile_add_arch (name
)
492 search_arch_type
*new =
493 (search_arch_type
*) xmalloc ((bfd_size_type
) (sizeof (search_arch_type
)));
497 if (ldfile_output_machine_name
[0] != '\0')
499 einfo (_("%P%F: target architecture respecified\n"));
503 ldfile_output_machine_name
= name
;
506 new->next
= (search_arch_type
*) NULL
;
507 new->name
= gnu960_map_archname (name
);
508 *search_arch_tail_ptr
= new;
509 search_arch_tail_ptr
= &new->next
;
512 #else /* not GNU960 */
515 ldfile_add_arch (in_name
)
518 char *name
= xstrdup (in_name
);
519 search_arch_type
*new =
520 (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
522 ldfile_output_machine_name
= in_name
;
525 new->next
= (search_arch_type
*) NULL
;
528 *name
= TOLOWER (*name
);
531 *search_arch_tail_ptr
= new;
532 search_arch_tail_ptr
= &new->next
;
537 /* Set the output architecture. */
540 ldfile_set_output_arch (string
)
543 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
547 ldfile_output_architecture
= arch
->arch
;
548 ldfile_output_machine
= arch
->mach
;
549 ldfile_output_machine_name
= arch
->printable_name
;
553 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);