1 /* Copyright (C) 1991, 92, 93, 94, 95, 98, 1999 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 boolean ldfile_open_file_search
78 PARAMS ((const char *arch, lang_input_statement_type *,
79 const char *lib, const char *suffix));
80 static FILE *try_open PARAMS ((const char *name, const char *exten));
83 ldfile_add_library_path (name, cmdline)
87 search_dirs_type *new;
89 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
92 new->cmdline = cmdline;
93 *search_tail_ptr = new;
94 search_tail_ptr = &new->next;
97 /* Try to open a BFD for a lang_input_statement. */
100 ldfile_try_open_bfd (attempt, entry)
102 lang_input_statement_type *entry;
104 entry->the_bfd = bfd_openr (attempt, entry->target);
106 if (trace_file_tries)
108 if (entry->the_bfd == NULL)
109 info_msg (_("attempt to open %s failed\n"), attempt);
111 info_msg (_("attempt to open %s succeeded\n"), attempt);
114 if (entry->the_bfd != NULL)
118 if (bfd_get_error () == bfd_error_invalid_target)
119 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
124 /* Search for and open the file specified by ENTRY. If it is an
125 archive, use ARCH, LIB and SUFFIX to modify the file name. */
128 ldfile_open_file_search (arch, entry, lib, suffix)
130 lang_input_statement_type *entry;
134 search_dirs_type *search;
136 /* If this is not an archive, try to open it in the current
138 if (! entry->is_archive)
140 if (ldfile_try_open_bfd (entry->filename, entry))
144 for (search = search_head;
145 search != (search_dirs_type *)NULL;
146 search = search->next)
150 if (entry->dynamic && ! link_info.relocateable)
152 if (ldemul_open_dynamic_archive (arch, search, entry))
156 string = (char *) xmalloc (strlen (search->name)
159 + strlen (entry->filename)
164 if (entry->is_archive)
165 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
166 lib, entry->filename, arch, suffix);
167 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
168 #if defined (__MSDOS__) || defined (_WIN32)
169 || entry->filename[0] == '\\'
170 || (isalpha (entry->filename[0])
171 && entry->filename[1] == ':')
174 strcpy (string, entry->filename);
176 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
178 if (ldfile_try_open_bfd (string, entry))
182 if (bfd_check_format (entry->the_bfd, bfd_archive))
184 /* We treat an archive as compatible if it empty
185 or has at least one compatible object. */
186 arfile = bfd_openr_next_archived_file (entry->the_bfd, NULL);
192 && !(bfd_check_format (arfile, bfd_object)
193 && bfd_arch_get_compatible (arfile, output_bfd)))
194 arfile = bfd_openr_next_archived_file (entry->the_bfd, arfile);
196 else if (bfd_arch_get_compatible (entry->the_bfd, output_bfd))
201 entry->filename = string;
205 info_msg (_("%s is for an incompatible architecture -- skipped\n"),
207 bfd_close(entry->the_bfd);
208 entry->the_bfd = NULL;
217 /* Open the input file specified by ENTRY. */
220 ldfile_open_file (entry)
221 lang_input_statement_type *entry;
223 if (entry->the_bfd != NULL)
226 if (! entry->search_dirs_flag)
228 if (ldfile_try_open_bfd (entry->filename, entry))
230 if (strcmp (entry->filename, entry->local_sym_name) != 0)
231 einfo (_("%F%P: cannot open %s for %s: %E\n"),
232 entry->filename, entry->local_sym_name);
234 einfo(_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
238 search_arch_type *arch;
240 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
241 for (arch = search_arch_head;
242 arch != (search_arch_type *) NULL;
245 if (ldfile_open_file_search (arch->name, entry, "lib", ".a"))
248 if (ldfile_open_file_search (arch->name, entry, ":lib", ".a"))
252 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
256 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
259 try_open (name, exten)
266 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");
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) {
309 /* Try now prefixes */
310 for (search = search_head;
311 search != (search_dirs_type *)NULL;
312 search = search->next) {
313 sprintf(buffer,"%s%s%s", search->name, slash, name);
314 result = try_open(buffer, extend);
322 ldfile_open_command_file (name)
325 FILE *ldlex_input_stack;
326 ldlex_input_stack = ldfile_find_command_file(name, "");
328 if (ldlex_input_stack == (FILE *)NULL) {
329 bfd_set_error (bfd_error_system_call);
330 einfo(_("%P%F: cannot open linker script file %s: %E\n"),name);
332 lex_push_file(ldlex_input_stack, name);
334 ldfile_input_filename = name;
346 gnu960_map_archname( name )
349 struct tabentry { char *cmd_switch; char *arch; };
350 static struct tabentry arch_tab[] = {
354 "KC", "mc", /* Synonym for MC */
357 "SA", "ka", /* Functionally equivalent to KA */
358 "SB", "kb", /* Functionally equivalent to KB */
364 for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
365 if ( !strcmp(name,tp->cmd_switch) ){
370 if ( tp->cmd_switch == NULL ){
371 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') {
388 einfo(_("%P%F: target architecture respecified\n"));
391 ldfile_output_machine_name = name;
394 new->next = (search_arch_type*)NULL;
395 new->name = gnu960_map_archname( name );
396 *search_arch_tail_ptr = new;
397 search_arch_tail_ptr = &new->next;
401 #else /* not GNU960 */
405 ldfile_add_arch (in_name)
406 CONST char * 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 */
430 ldfile_set_output_arch (string)
433 const bfd_arch_info_type *arch = bfd_scan_arch(string);
436 ldfile_output_architecture = arch->arch;
437 ldfile_output_machine = arch->mach;
438 ldfile_output_machine_name = arch->printable_name;
441 einfo(_("%P%F: cannot represent machine `%s'\n"), string);