1 /* Copyright (C) 1991, 92, 93, 94, 95, 98, 99, 2000 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 look after all the file stuff
42 const char *ldfile_input_filename;
43 boolean ldfile_assumed_script = false;
44 const char *ldfile_output_machine_name = "";
45 unsigned long ldfile_output_machine;
46 enum bfd_architecture ldfile_output_architecture;
47 search_dirs_type *search_head;
53 #if defined (_WIN32) && ! defined (__CYGWIN32__)
60 /* The MPW path char is a colon. */
66 static search_dirs_type **search_tail_ptr = &search_head;
68 typedef struct search_arch
71 struct search_arch *next;
74 static search_arch_type *search_arch_head;
75 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
77 static FILE *try_open PARAMS ((const char *name, const char *exten));
80 ldfile_add_library_path (name, cmdline)
84 search_dirs_type *new;
86 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
89 new->cmdline = cmdline;
90 *search_tail_ptr = new;
91 search_tail_ptr = &new->next;
94 /* Try to open a BFD for a lang_input_statement. */
97 ldfile_try_open_bfd (attempt, entry)
99 lang_input_statement_type *entry;
101 entry->the_bfd = bfd_openr (attempt, entry->target);
103 if (trace_file_tries)
105 if (entry->the_bfd == NULL)
106 info_msg (_("attempt to open %s failed\n"), attempt);
108 info_msg (_("attempt to open %s succeeded\n"), attempt);
111 if (entry->the_bfd == NULL)
113 if (bfd_get_error () == bfd_error_invalid_target)
114 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
118 /* If we are searching for this file, see if the architecture is
119 compatible with the output file. If it isn't, keep searching.
120 If we can't open the file as an object file, stop the search
123 if (entry->search_dirs_flag)
127 if (bfd_check_format (entry->the_bfd, bfd_archive))
128 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
130 check = entry->the_bfd;
132 if (! bfd_check_format (check, bfd_object))
134 if (bfd_arch_get_compatible (check, output_bfd) == NULL)
136 einfo (_("%P: skipping incompatible %s when searching for %s"),
137 attempt, entry->local_sym_name);
138 bfd_close (entry->the_bfd);
139 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))
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");
265 if (trace_file_tries)
268 info_msg (_("cannot find script file %s\n"), name);
270 info_msg (_("opened script file %s\n"), name);
278 sprintf (buff, "%s%s", name, exten);
279 result = fopen (buff, "r");
280 if (trace_file_tries)
283 info_msg (_("cannot find script file %s\n"), buff);
285 info_msg (_("opened script file %s\n"), buff);
292 /* Try to open NAME; if that fails, look for it in any directories
293 specified with -L, without and with EXTEND apppended. */
296 ldfile_find_command_file (name, extend)
300 search_dirs_type *search;
304 /* First try raw name */
305 result = try_open(name,"");
306 if (result == (FILE *)NULL) {
307 /* Try now prefixes */
308 for (search = search_head;
309 search != (search_dirs_type *)NULL;
310 search = search->next) {
311 sprintf(buffer,"%s%s%s", search->name, slash, name);
312 result = try_open(buffer, extend);
320 ldfile_open_command_file (name)
323 FILE *ldlex_input_stack;
324 ldlex_input_stack = ldfile_find_command_file(name, "");
326 if (ldlex_input_stack == (FILE *)NULL) {
327 bfd_set_error (bfd_error_system_call);
328 einfo(_("%P%F: cannot open linker script file %s: %E\n"),name);
330 lex_push_file(ldlex_input_stack, name);
332 ldfile_input_filename = name;
344 gnu960_map_archname( name )
347 struct tabentry { char *cmd_switch; char *arch; };
348 static struct tabentry arch_tab[] = {
352 "KC", "mc", /* Synonym for MC */
355 "SA", "ka", /* Functionally equivalent to KA */
356 "SB", "kb", /* Functionally equivalent to KB */
362 for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
363 if ( !strcmp(name,tp->cmd_switch) ){
368 if ( tp->cmd_switch == NULL ){
369 einfo(_("%P%F: unknown architecture: %s\n"),name);
377 ldfile_add_arch(name)
380 search_arch_type *new =
381 (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
385 if (ldfile_output_machine_name[0] != '\0') {
386 einfo(_("%P%F: target architecture respecified\n"));
389 ldfile_output_machine_name = name;
392 new->next = (search_arch_type*)NULL;
393 new->name = gnu960_map_archname( name );
394 *search_arch_tail_ptr = new;
395 search_arch_tail_ptr = &new->next;
399 #else /* not GNU960 */
403 ldfile_add_arch (in_name)
404 CONST char * in_name;
406 char *name = buystring(in_name);
407 search_arch_type *new =
408 (search_arch_type *) xmalloc (sizeof (search_arch_type));
410 ldfile_output_machine_name = in_name;
413 new->next = (search_arch_type*)NULL;
416 if (isupper ((unsigned char) *name))
417 *name = tolower ((unsigned char) *name);
420 *search_arch_tail_ptr = new;
421 search_arch_tail_ptr = &new->next;
426 /* Set the output architecture */
428 ldfile_set_output_arch (string)
431 const bfd_arch_info_type *arch = bfd_scan_arch(string);
434 ldfile_output_architecture = arch->arch;
435 ldfile_output_machine = arch->mach;
436 ldfile_output_machine_name = arch->printable_name;
439 einfo(_("%P%F: cannot represent machine `%s'\n"), string);