* gas/i386/opcode.d: Tweak lgdt for 2001-07-18 change.
[binutils.git] / ld / ldfile.c
blob0e4d0ec3a2e4d027531a95736316b77710a9a8fe
1 /* Linker file opening and searching.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001
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)
10 any later version.
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
20 02111-1307, USA. */
22 /* ldfile.c: look after all the file stuff. */
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "bfdlink.h"
27 #include "ld.h"
28 #include "ldmisc.h"
29 #include "ldexp.h"
30 #include "ldlang.h"
31 #include "ldfile.h"
32 #include "ldmain.h"
33 #include "ldgram.h"
34 #include "ldlex.h"
35 #include "ldemul.h"
36 #include "libiberty.h"
38 #include <ctype.h>
40 const char *ldfile_input_filename;
41 boolean ldfile_assumed_script = false;
42 const char *ldfile_output_machine_name = "";
43 unsigned long ldfile_output_machine;
44 enum bfd_architecture ldfile_output_architecture;
45 search_dirs_type *search_head;
47 #ifndef MPW
48 #ifdef VMS
49 char *slash = "";
50 #else
51 #if defined (_WIN32) && ! defined (__CYGWIN32__)
52 char *slash = "\\";
53 #else
54 char *slash = "/";
55 #endif
56 #endif
57 #else /* MPW */
58 /* The MPW path char is a colon. */
59 char *slash = ":";
60 #endif /* MPW */
62 /* LOCAL */
64 static search_dirs_type **search_tail_ptr = &search_head;
66 typedef struct search_arch {
67 char *name;
68 struct search_arch *next;
69 } search_arch_type;
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));
76 void
77 ldfile_add_library_path (name, cmdline)
78 const char *name;
79 boolean cmdline;
81 search_dirs_type *new;
83 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
84 new->next = NULL;
85 new->name = name;
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. */
93 boolean
94 ldfile_try_open_bfd (attempt, entry)
95 const char *attempt;
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);
104 else
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);
112 return false;
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
118 here. */
120 if (entry->search_dirs_flag)
122 bfd *check;
124 if (bfd_check_format (entry->the_bfd, bfd_archive))
125 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
126 else
127 check = entry->the_bfd;
129 if (check != NULL)
131 if (! bfd_check_format (check, bfd_object))
132 return true;
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;
139 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;
235 boolean found = false;
237 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
238 for (arch = search_arch_head;
239 arch != (search_arch_type *) NULL;
240 arch = arch->next)
242 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
243 if (found)
244 break;
245 #ifdef VMS
246 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
247 if (found)
248 break;
249 #endif
250 found = ldemul_find_potential_libraries (arch->name, entry);
251 if (found)
252 break;
255 /* If we have found the file, we don't need to search directories
256 again. */
257 if (found)
258 entry->search_dirs_flag = false;
259 else
260 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
264 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
266 static FILE *
267 try_open (name, exten)
268 const char *name;
269 const char *exten;
271 FILE *result;
272 char buff[1000];
274 result = fopen (name, "r");
276 if (trace_file_tries)
278 if (result == NULL)
279 info_msg (_("cannot find script file %s\n"), name);
280 else
281 info_msg (_("opened script file %s\n"), name);
284 if (result != NULL)
285 return result;
287 if (*exten)
289 sprintf (buff, "%s%s", name, exten);
290 result = fopen (buff, "r");
292 if (trace_file_tries)
294 if (result == NULL)
295 info_msg (_("cannot find script file %s\n"), buff);
296 else
297 info_msg (_("opened script file %s\n"), buff);
301 return result;
304 /* Try to open NAME; if that fails, look for it in any directories
305 specified with -L, without and with EXTEND apppended. */
307 FILE *
308 ldfile_find_command_file (name, extend)
309 const char *name;
310 const char *extend;
312 search_dirs_type *search;
313 FILE *result;
314 char buffer[1000];
316 /* First try raw name. */
317 result = try_open (name, "");
318 if (result == (FILE *) NULL)
320 /* Try now prefixes. */
321 for (search = search_head;
322 search != (search_dirs_type *) NULL;
323 search = search->next)
325 sprintf (buffer, "%s%s%s", search->name, slash, name);
327 result = try_open (buffer, extend);
328 if (result)
329 break;
333 return result;
336 void
337 ldfile_open_command_file (name)
338 const char *name;
340 FILE *ldlex_input_stack;
341 ldlex_input_stack = ldfile_find_command_file (name, "");
343 if (ldlex_input_stack == (FILE *) NULL)
345 bfd_set_error (bfd_error_system_call);
346 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
349 lex_push_file (ldlex_input_stack, name);
351 ldfile_input_filename = name;
352 lineno = 1;
353 had_script = true;
356 #ifdef GNU960
357 static char *
358 gnu960_map_archname (name)
359 char *name;
361 struct tabentry { char *cmd_switch; char *arch; };
362 static struct tabentry arch_tab[] =
364 "", "",
365 "KA", "ka",
366 "KB", "kb",
367 "KC", "mc", /* Synonym for MC */
368 "MC", "mc",
369 "CA", "ca",
370 "SA", "ka", /* Functionally equivalent to KA */
371 "SB", "kb", /* Functionally equivalent to KB */
372 NULL, ""
374 struct tabentry *tp;
376 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
378 if (! strcmp (name,tp->cmd_switch))
379 break;
382 if (tp->cmd_switch == NULL)
383 einfo (_("%P%F: unknown architecture: %s\n"), name);
385 return tp->arch;
388 void
389 ldfile_add_arch (name)
390 char *name;
392 search_arch_type *new =
393 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
395 if (*name != '\0')
397 if (ldfile_output_machine_name[0] != '\0')
399 einfo (_("%P%F: target architecture respecified\n"));
400 return;
403 ldfile_output_machine_name = name;
406 new->next = (search_arch_type *) NULL;
407 new->name = gnu960_map_archname (name);
408 *search_arch_tail_ptr = new;
409 search_arch_tail_ptr = &new->next;
412 #else /* not GNU960 */
414 void
415 ldfile_add_arch (in_name)
416 CONST char *in_name;
418 char *name = xstrdup (in_name);
419 search_arch_type *new =
420 (search_arch_type *) xmalloc (sizeof (search_arch_type));
422 ldfile_output_machine_name = in_name;
424 new->name = name;
425 new->next = (search_arch_type *) NULL;
426 while (*name)
428 if (isupper ((unsigned char) *name))
429 *name = tolower ((unsigned char) *name);
430 name++;
432 *search_arch_tail_ptr = new;
433 search_arch_tail_ptr = &new->next;
436 #endif
438 /* Set the output architecture. */
440 void
441 ldfile_set_output_arch (string)
442 CONST char *string;
444 const bfd_arch_info_type *arch = bfd_scan_arch (string);
446 if (arch)
448 ldfile_output_architecture = arch->arch;
449 ldfile_output_machine = arch->mach;
450 ldfile_output_machine_name = arch->printable_name;
452 else
454 einfo (_("%P%F: cannot represent machine `%s'\n"), string);