1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001
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. */
36 #include "libiberty.h"
40 const char *ldfile_input_filename
;
41 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. */
64 static search_dirs_type
**search_tail_ptr
= &search_head
;
66 typedef struct search_arch
{
68 struct search_arch
*next
;
71 static search_arch_type
*search_arch_head
;
72 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
74 static FILE *try_open
PARAMS ((const char *name
, const char *exten
));
77 ldfile_add_library_path (name
, cmdline
)
81 search_dirs_type
*new;
83 new = (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
86 new->cmdline
= cmdline
;
87 *search_tail_ptr
= new;
88 search_tail_ptr
= &new->next
;
91 /* Try to open a BFD for a lang_input_statement. */
94 ldfile_try_open_bfd (attempt
, entry
)
96 lang_input_statement_type
*entry
;
98 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
100 if (trace_file_tries
)
102 if (entry
->the_bfd
== NULL
)
103 info_msg (_("attempt to open %s failed\n"), attempt
);
105 info_msg (_("attempt to open %s succeeded\n"), attempt
);
108 if (entry
->the_bfd
== NULL
)
110 if (bfd_get_error () == bfd_error_invalid_target
)
111 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
115 /* If we are searching for this file, see if the architecture is
116 compatible with the output file. If it isn't, keep searching.
117 If we can't open the file as an object file, stop the search
120 if (entry
->search_dirs_flag
)
124 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
125 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
127 check
= entry
->the_bfd
;
131 if (! bfd_check_format (check
, bfd_object
))
133 if (bfd_arch_get_compatible (check
, output_bfd
) == NULL
)
135 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
136 attempt
, entry
->local_sym_name
);
137 bfd_close (entry
->the_bfd
);
138 entry
->the_bfd
= NULL
;
147 /* Search for and open the file specified by ENTRY. If it is an
148 archive, use ARCH, LIB and SUFFIX to modify the file name. */
151 ldfile_open_file_search (arch
, entry
, lib
, suffix
)
153 lang_input_statement_type
*entry
;
157 search_dirs_type
*search
;
159 /* If this is not an archive, try to open it in the current
161 if (! entry
->is_archive
)
163 if (ldfile_try_open_bfd (entry
->filename
, entry
))
167 for (search
= search_head
;
168 search
!= (search_dirs_type
*) NULL
;
169 search
= search
->next
)
173 if (entry
->dynamic
&& ! link_info
.relocateable
)
175 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
179 string
= (char *) xmalloc (strlen (search
->name
)
182 + strlen (entry
->filename
)
187 if (entry
->is_archive
)
188 sprintf (string
, "%s%s%s%s%s%s", search
->name
, slash
,
189 lib
, entry
->filename
, arch
, suffix
);
190 else if (entry
->filename
[0] == '/' || entry
->filename
[0] == '.'
191 #if defined (__MSDOS__) || defined (_WIN32)
192 || entry
->filename
[0] == '\\'
193 || (isalpha (entry
->filename
[0])
194 && entry
->filename
[1] == ':')
197 strcpy (string
, entry
->filename
);
199 sprintf (string
, "%s%s%s", search
->name
, slash
, entry
->filename
);
201 if (ldfile_try_open_bfd (string
, entry
))
203 entry
->filename
= string
;
213 /* Open the input file specified by ENTRY. */
216 ldfile_open_file (entry
)
217 lang_input_statement_type
*entry
;
219 if (entry
->the_bfd
!= NULL
)
222 if (! entry
->search_dirs_flag
)
224 if (ldfile_try_open_bfd (entry
->filename
, entry
))
226 if (strcmp (entry
->filename
, entry
->local_sym_name
) != 0)
227 einfo (_("%F%P: cannot open %s for %s: %E\n"),
228 entry
->filename
, entry
->local_sym_name
);
230 einfo (_("%F%P: cannot open %s: %E\n"), entry
->local_sym_name
);
234 search_arch_type
*arch
;
235 boolean found
= false;
237 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
238 for (arch
= search_arch_head
;
239 arch
!= (search_arch_type
*) NULL
;
242 found
= ldfile_open_file_search (arch
->name
, entry
, "lib", ".a");
246 found
= ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a");
250 found
= ldemul_find_potential_libraries (arch
->name
, entry
);
255 /* If we have found the file, we don't need to search directories
258 entry
->search_dirs_flag
= false;
260 einfo (_("%F%P: cannot find %s\n"), entry
->local_sym_name
);
264 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
267 try_open (name
, exten
)
274 result
= fopen (name
, "r");
276 if (trace_file_tries
)
279 info_msg (_("cannot find script file %s\n"), name
);
281 info_msg (_("opened script file %s\n"), name
);
289 sprintf (buff
, "%s%s", name
, exten
);
290 result
= fopen (buff
, "r");
292 if (trace_file_tries
)
295 info_msg (_("cannot find script file %s\n"), buff
);
297 info_msg (_("opened script file %s\n"), buff
);
304 /* Try to open NAME; if that fails, look for it in any directories
305 specified with -L, without and with EXTEND apppended. */
308 ldfile_find_command_file (name
, extend
)
312 search_dirs_type
*search
;
316 /* First try raw name. */
317 result
= try_open (name
, "");
318 if (result
== (FILE *) NULL
)
320 /* Try now prefixes. */
321 for (search
= search_head
;
322 search
!= (search_dirs_type
*) NULL
;
323 search
= search
->next
)
325 sprintf (buffer
, "%s%s%s", search
->name
, slash
, name
);
327 result
= try_open (buffer
, extend
);
337 ldfile_open_command_file (name
)
340 FILE *ldlex_input_stack
;
341 ldlex_input_stack
= ldfile_find_command_file (name
, "");
343 if (ldlex_input_stack
== (FILE *) NULL
)
345 bfd_set_error (bfd_error_system_call
);
346 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
349 lex_push_file (ldlex_input_stack
, name
);
351 ldfile_input_filename
= name
;
358 gnu960_map_archname (name
)
361 struct tabentry
{ char *cmd_switch
; char *arch
; };
362 static struct tabentry arch_tab
[] =
367 "KC", "mc", /* Synonym for MC */
370 "SA", "ka", /* Functionally equivalent to KA */
371 "SB", "kb", /* Functionally equivalent to KB */
376 for (tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++)
378 if (! strcmp (name
,tp
->cmd_switch
))
382 if (tp
->cmd_switch
== NULL
)
383 einfo (_("%P%F: unknown architecture: %s\n"), name
);
389 ldfile_add_arch (name
)
392 search_arch_type
*new =
393 (search_arch_type
*) xmalloc ((bfd_size_type
) (sizeof (search_arch_type
)));
397 if (ldfile_output_machine_name
[0] != '\0')
399 einfo (_("%P%F: target architecture respecified\n"));
403 ldfile_output_machine_name
= name
;
406 new->next
= (search_arch_type
*) NULL
;
407 new->name
= gnu960_map_archname (name
);
408 *search_arch_tail_ptr
= new;
409 search_arch_tail_ptr
= &new->next
;
412 #else /* not GNU960 */
415 ldfile_add_arch (in_name
)
418 char *name
= xstrdup (in_name
);
419 search_arch_type
*new =
420 (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
422 ldfile_output_machine_name
= in_name
;
425 new->next
= (search_arch_type
*) NULL
;
428 if (isupper ((unsigned char) *name
))
429 *name
= tolower ((unsigned char) *name
);
432 *search_arch_tail_ptr
= new;
433 search_arch_tail_ptr
= &new->next
;
438 /* Set the output architecture. */
441 ldfile_set_output_arch (string
)
444 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
448 ldfile_output_architecture
= arch
->arch
;
449 ldfile_output_machine
= arch
->mach
;
450 ldfile_output_machine_name
= arch
->printable_name
;
454 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);