1 /* Linker file opening and searching.
2 Copyright (C) 1991, 92, 93, 94, 95, 98, 99, 2000
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. */
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 new = (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
85 new->cmdline
= cmdline
;
86 *search_tail_ptr
= new;
87 search_tail_ptr
= &new->next
;
90 /* Try to open a BFD for a lang_input_statement. */
93 ldfile_try_open_bfd (attempt
, entry
)
95 lang_input_statement_type
*entry
;
97 entry
->the_bfd
= bfd_openr (attempt
, entry
->target
);
101 if (entry
->the_bfd
== NULL
)
102 info_msg (_("attempt to open %s failed\n"), attempt
);
104 info_msg (_("attempt to open %s succeeded\n"), attempt
);
107 if (entry
->the_bfd
== NULL
)
109 if (bfd_get_error () == bfd_error_invalid_target
)
110 einfo (_("%F%P: invalid BFD target `%s'\n"), entry
->target
);
114 /* If we are searching for this file, see if the architecture is
115 compatible with the output file. If it isn't, keep searching.
116 If we can't open the file as an object file, stop the search
119 if (entry
->search_dirs_flag
)
123 if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
124 check
= bfd_openr_next_archived_file (entry
->the_bfd
, NULL
);
126 check
= entry
->the_bfd
;
130 if (! bfd_check_format (check
, bfd_object
))
132 if (bfd_arch_get_compatible (check
, output_bfd
) == NULL
)
134 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
135 attempt
, entry
->local_sym_name
);
136 bfd_close (entry
->the_bfd
);
137 entry
->the_bfd
= NULL
;
146 /* Search for and open the file specified by ENTRY. If it is an
147 archive, use ARCH, LIB and SUFFIX to modify the file name. */
150 ldfile_open_file_search (arch
, entry
, lib
, suffix
)
152 lang_input_statement_type
*entry
;
156 search_dirs_type
*search
;
158 /* If this is not an archive, try to open it in the current
160 if (! entry
->is_archive
)
162 if (ldfile_try_open_bfd (entry
->filename
, entry
))
166 for (search
= search_head
;
167 search
!= (search_dirs_type
*) NULL
;
168 search
= search
->next
)
172 if (entry
->dynamic
&& ! link_info
.relocateable
)
174 if (ldemul_open_dynamic_archive (arch
, search
, entry
))
178 string
= (char *) xmalloc (strlen (search
->name
)
181 + strlen (entry
->filename
)
186 if (entry
->is_archive
)
187 sprintf (string
, "%s%s%s%s%s%s", search
->name
, slash
,
188 lib
, entry
->filename
, arch
, suffix
);
189 else if (entry
->filename
[0] == '/' || entry
->filename
[0] == '.'
190 #if defined (__MSDOS__) || defined (_WIN32)
191 || entry
->filename
[0] == '\\'
192 || (isalpha (entry
->filename
[0])
193 && entry
->filename
[1] == ':')
196 strcpy (string
, entry
->filename
);
198 sprintf (string
, "%s%s%s", search
->name
, slash
, entry
->filename
);
200 if (ldfile_try_open_bfd (string
, entry
))
202 entry
->filename
= string
;
212 /* Open the input file specified by ENTRY. */
215 ldfile_open_file (entry
)
216 lang_input_statement_type
*entry
;
218 if (entry
->the_bfd
!= NULL
)
221 if (! entry
->search_dirs_flag
)
223 if (ldfile_try_open_bfd (entry
->filename
, entry
))
225 if (strcmp (entry
->filename
, entry
->local_sym_name
) != 0)
226 einfo (_("%F%P: cannot open %s for %s: %E\n"),
227 entry
->filename
, entry
->local_sym_name
);
229 einfo (_("%F%P: cannot open %s: %E\n"), entry
->local_sym_name
);
233 search_arch_type
*arch
;
235 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
236 for (arch
= search_arch_head
;
237 arch
!= (search_arch_type
*) NULL
;
240 if (ldfile_open_file_search (arch
->name
, entry
, "lib", ".a"))
243 if (ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a"))
246 if (ldemul_find_potential_libraries (arch
->name
, entry
))
250 einfo (_("%F%P: cannot find %s\n"), entry
->local_sym_name
);
254 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
257 try_open (name
, exten
)
264 result
= fopen (name
, "r");
266 if (trace_file_tries
)
269 info_msg (_("cannot find script file %s\n"), name
);
271 info_msg (_("opened script file %s\n"), name
);
279 sprintf (buff
, "%s%s", name
, exten
);
280 result
= fopen (buff
, "r");
282 if (trace_file_tries
)
285 info_msg (_("cannot find script file %s\n"), buff
);
287 info_msg (_("opened script file %s\n"), buff
);
294 /* Try to open NAME; if that fails, look for it in any directories
295 specified with -L, without and with EXTEND apppended. */
298 ldfile_find_command_file (name
, extend
)
302 search_dirs_type
*search
;
306 /* First try raw name. */
307 result
= try_open (name
, "");
308 if (result
== (FILE *) NULL
)
310 /* Try now prefixes. */
311 for (search
= search_head
;
312 search
!= (search_dirs_type
*) NULL
;
313 search
= search
->next
)
315 sprintf (buffer
, "%s%s%s", search
->name
, slash
, name
);
317 result
= try_open (buffer
, extend
);
327 ldfile_open_command_file (name
)
330 FILE *ldlex_input_stack
;
331 ldlex_input_stack
= ldfile_find_command_file (name
, "");
333 if (ldlex_input_stack
== (FILE *) NULL
)
335 bfd_set_error (bfd_error_system_call
);
336 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
339 lex_push_file (ldlex_input_stack
, name
);
341 ldfile_input_filename
= name
;
348 gnu960_map_archname (name
)
351 struct tabentry
{ char *cmd_switch
; char *arch
; };
352 static struct tabentry arch_tab
[] =
357 "KC", "mc", /* Synonym for MC */
360 "SA", "ka", /* Functionally equivalent to KA */
361 "SB", "kb", /* Functionally equivalent to KB */
366 for (tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++)
368 if (! strcmp (name
,tp
->cmd_switch
))
372 if (tp
->cmd_switch
== NULL
)
373 einfo (_("%P%F: unknown architecture: %s\n"), name
);
379 ldfile_add_arch (name
)
382 search_arch_type
*new =
383 (search_arch_type
*) xmalloc ((bfd_size_type
) (sizeof (search_arch_type
)));
387 if (ldfile_output_machine_name
[0] != '\0')
389 einfo (_("%P%F: target architecture respecified\n"));
393 ldfile_output_machine_name
= name
;
396 new->next
= (search_arch_type
*) NULL
;
397 new->name
= gnu960_map_archname (name
);
398 *search_arch_tail_ptr
= new;
399 search_arch_tail_ptr
= &new->next
;
402 #else /* not GNU960 */
405 ldfile_add_arch (in_name
)
408 char *name
= buystring (in_name
);
409 search_arch_type
*new =
410 (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
412 ldfile_output_machine_name
= in_name
;
415 new->next
= (search_arch_type
*) NULL
;
418 if (isupper ((unsigned char) *name
))
419 *name
= tolower ((unsigned char) *name
);
422 *search_arch_tail_ptr
= new;
423 search_arch_tail_ptr
= &new->next
;
428 /* Set the output architecture. */
431 ldfile_set_output_arch (string
)
434 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
438 ldfile_output_architecture
= arch
->arch
;
439 ldfile_output_machine
= arch
->mach
;
440 ldfile_output_machine_name
= arch
->printable_name
;
444 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);