* configure.tgt: Enable elf64_sparc on Solaris7+/sparc. And make
[binutils.git] / ld / ldfile.c
blob187770e892b73f8aad80d86db6fe5ccbd81634f5
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)
8 any later version.
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
18 02111-1307, USA. */
21 ldfile.c
23 look after all the file stuff
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmisc.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include "ldfile.h"
35 #include "ldmain.h"
36 #include "ldgram.h"
37 #include "ldlex.h"
38 #include "ldemul.h"
40 #include <ctype.h>
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;
49 #ifndef MPW
50 #ifdef VMS
51 char *slash = "";
52 #else
53 #if defined (_WIN32) && ! defined (__CYGWIN32__)
54 char *slash = "\\";
55 #else
56 char *slash = "/";
57 #endif
58 #endif
59 #else /* MPW */
60 /* The MPW path char is a colon. */
61 char *slash = ":";
62 #endif /* MPW */
64 /* LOCAL */
66 static search_dirs_type **search_tail_ptr = &search_head;
68 typedef struct search_arch
70 char *name;
71 struct search_arch *next;
72 } search_arch_type;
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));
79 void
80 ldfile_add_library_path (name, cmdline)
81 const char *name;
82 boolean cmdline;
84 search_dirs_type *new;
86 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
87 new->next = NULL;
88 new->name = name;
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. */
96 boolean
97 ldfile_try_open_bfd (attempt, entry)
98 const char *attempt;
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);
107 else
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);
115 return false;
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
121 here. */
123 if (entry->search_dirs_flag)
125 bfd *check;
127 if (bfd_check_format (entry->the_bfd, bfd_archive))
128 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
129 else
130 check = entry->the_bfd;
132 if (! bfd_check_format (check, bfd_object))
133 return true;
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;
140 return false;
144 return true;
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. */
150 boolean
151 ldfile_open_file_search (arch, entry, lib, suffix)
152 const char *arch;
153 lang_input_statement_type *entry;
154 const char *lib;
155 const char *suffix;
157 search_dirs_type *search;
159 /* If this is not an archive, try to open it in the current
160 directory first. */
161 if (! entry->is_archive)
163 if (ldfile_try_open_bfd (entry->filename, entry))
164 return true;
167 for (search = search_head;
168 search != (search_dirs_type *)NULL;
169 search = search->next)
171 char *string;
173 if (entry->dynamic && ! link_info.relocateable)
175 if (ldemul_open_dynamic_archive (arch, search, entry))
176 return true;
179 string = (char *) xmalloc (strlen (search->name)
180 + strlen (slash)
181 + strlen (lib)
182 + strlen (entry->filename)
183 + strlen (arch)
184 + strlen (suffix)
185 + 1);
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] == ':')
195 #endif
197 strcpy (string, entry->filename);
198 else
199 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
201 if (ldfile_try_open_bfd (string, entry))
203 entry->filename = string;
204 return true;
207 free (string);
210 return false;
213 /* Open the input file specified by ENTRY. */
215 void
216 ldfile_open_file (entry)
217 lang_input_statement_type *entry;
219 if (entry->the_bfd != NULL)
220 return;
222 if (! entry->search_dirs_flag)
224 if (ldfile_try_open_bfd (entry->filename, entry))
225 return;
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);
229 else
230 einfo(_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
232 else
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;
239 arch = arch->next)
241 if (ldfile_open_file_search (arch->name, entry, "lib", ".a"))
242 return;
243 #ifdef VMS
244 if (ldfile_open_file_search (arch->name, entry, ":lib", ".a"))
245 return;
246 #endif
247 if (ldemul_find_potential_libraries (arch->name, entry))
248 return;
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. */
256 static FILE *
257 try_open (name, exten)
258 const char *name;
259 const char *exten;
261 FILE *result;
262 char buff[1000];
264 result = fopen (name, "r");
265 if (trace_file_tries)
267 if (result == NULL)
268 info_msg (_("cannot find script file %s\n"), name);
269 else
270 info_msg (_("opened script file %s\n"), name);
273 if (result != NULL)
274 return result;
276 if (*exten)
278 sprintf (buff, "%s%s", name, exten);
279 result = fopen (buff, "r");
280 if (trace_file_tries)
282 if (result == NULL)
283 info_msg (_("cannot find script file %s\n"), buff);
284 else
285 info_msg (_("opened script file %s\n"), buff);
289 return result;
292 /* Try to open NAME; if that fails, look for it in any directories
293 specified with -L, without and with EXTEND apppended. */
295 FILE *
296 ldfile_find_command_file (name, extend)
297 const char *name;
298 const char *extend;
300 search_dirs_type *search;
301 FILE *result;
302 char buffer[1000];
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);
313 if (result)break;
316 return result;
319 void
320 ldfile_open_command_file (name)
321 const char *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;
333 lineno = 1;
334 had_script = true;
341 #ifdef GNU960
342 static
343 char *
344 gnu960_map_archname( name )
345 char *name;
347 struct tabentry { char *cmd_switch; char *arch; };
348 static struct tabentry arch_tab[] = {
349 "", "",
350 "KA", "ka",
351 "KB", "kb",
352 "KC", "mc", /* Synonym for MC */
353 "MC", "mc",
354 "CA", "ca",
355 "SA", "ka", /* Functionally equivalent to KA */
356 "SB", "kb", /* Functionally equivalent to KB */
357 NULL, ""
359 struct tabentry *tp;
362 for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
363 if ( !strcmp(name,tp->cmd_switch) ){
364 break;
368 if ( tp->cmd_switch == NULL ){
369 einfo(_("%P%F: unknown architecture: %s\n"),name);
371 return tp->arch;
376 void
377 ldfile_add_arch(name)
378 char *name;
380 search_arch_type *new =
381 (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
384 if (*name != '\0') {
385 if (ldfile_output_machine_name[0] != '\0') {
386 einfo(_("%P%F: target architecture respecified\n"));
387 return;
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 */
402 void
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;
412 new->name = name;
413 new->next = (search_arch_type*)NULL;
414 while (*name)
416 if (isupper ((unsigned char) *name))
417 *name = tolower ((unsigned char) *name);
418 name++;
420 *search_arch_tail_ptr = new;
421 search_arch_tail_ptr = &new->next;
424 #endif
426 /* Set the output architecture */
427 void
428 ldfile_set_output_arch (string)
429 CONST char *string;
431 const bfd_arch_info_type *arch = bfd_scan_arch(string);
433 if (arch) {
434 ldfile_output_architecture = arch->arch;
435 ldfile_output_machine = arch->mach;
436 ldfile_output_machine_name = arch->printable_name;
438 else {
439 einfo(_("%P%F: cannot represent machine `%s'\n"), string);