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
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
;
236 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
237 for (arch
= search_arch_head
;
238 arch
!= (search_arch_type
*) NULL
;
241 if (ldfile_open_file_search (arch
->name
, entry
, "lib", ".a"))
244 if (ldfile_open_file_search (arch
->name
, entry
, ":lib", ".a"))
247 if (ldemul_find_potential_libraries (arch
->name
, entry
))
251 einfo (_("%F%P: cannot find %s\n"), entry
->local_sym_name
);
255 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
258 try_open (name
, exten
)
265 result
= fopen (name
, "r");
267 if (trace_file_tries
)
270 info_msg (_("cannot find script file %s\n"), name
);
272 info_msg (_("opened script file %s\n"), name
);
280 sprintf (buff
, "%s%s", name
, exten
);
281 result
= fopen (buff
, "r");
283 if (trace_file_tries
)
286 info_msg (_("cannot find script file %s\n"), buff
);
288 info_msg (_("opened script file %s\n"), buff
);
295 /* Try to open NAME; if that fails, look for it in any directories
296 specified with -L, without and with EXTEND apppended. */
299 ldfile_find_command_file (name
, extend
)
303 search_dirs_type
*search
;
307 /* First try raw name */
308 result
= try_open (name
, "");
309 if (result
== (FILE *)NULL
)
311 /* Try now prefixes */
312 for (search
= search_head
;
313 search
!= (search_dirs_type
*)NULL
;
314 search
= search
->next
)
316 sprintf (buffer
,"%s%s%s", search
->name
, slash
, name
);
318 result
= try_open (buffer
, extend
);
328 ldfile_open_command_file (name
)
331 FILE *ldlex_input_stack
;
332 ldlex_input_stack
= ldfile_find_command_file (name
, "");
334 if (ldlex_input_stack
== (FILE *)NULL
)
336 bfd_set_error (bfd_error_system_call
);
337 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name
);
340 lex_push_file (ldlex_input_stack
, name
);
342 ldfile_input_filename
= name
;
349 gnu960_map_archname (name
)
352 struct tabentry
{ char *cmd_switch
; char *arch
; };
353 static struct tabentry arch_tab
[] =
358 "KC", "mc", /* Synonym for MC */
361 "SA", "ka", /* Functionally equivalent to KA */
362 "SB", "kb", /* Functionally equivalent to KB */
368 for (tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++)
370 if (! strcmp (name
,tp
->cmd_switch
))
374 if (tp
->cmd_switch
== NULL
)
375 einfo (_("%P%F: unknown architecture: %s\n"),name
);
381 ldfile_add_arch (name
)
384 search_arch_type
*new =
385 (search_arch_type
*)xmalloc ((bfd_size_type
)(sizeof (search_arch_type
)));
390 if (ldfile_output_machine_name
[0] != '\0')
392 einfo (_("%P%F: target architecture respecified\n"));
396 ldfile_output_machine_name
= name
;
399 new->next
= (search_arch_type
*)NULL
;
400 new->name
= gnu960_map_archname (name
);
401 *search_arch_tail_ptr
= new;
402 search_arch_tail_ptr
= &new->next
;
405 #else /* not GNU960 */
409 ldfile_add_arch (in_name
)
410 CONST
char * in_name
;
412 char *name
= buystring (in_name
);
413 search_arch_type
*new =
414 (search_arch_type
*) xmalloc (sizeof (search_arch_type
));
416 ldfile_output_machine_name
= in_name
;
419 new->next
= (search_arch_type
*)NULL
;
422 if (isupper ((unsigned char) *name
))
423 *name
= tolower ((unsigned char) *name
);
426 *search_arch_tail_ptr
= new;
427 search_arch_tail_ptr
= &new->next
;
432 /* Set the output architecture */
434 ldfile_set_output_arch (string
)
437 const bfd_arch_info_type
*arch
= bfd_scan_arch (string
);
441 ldfile_output_architecture
= arch
->arch
;
442 ldfile_output_machine
= arch
->mach
;
443 ldfile_output_machine_name
= arch
->printable_name
;
447 einfo (_("%P%F: cannot represent machine `%s'\n"), string
);