Locale changes from Bruno Haible <haible@clisp.cons.org>.
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805 1/* Linker file opening and searching.
a2b64bed 2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001
a9998805 3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of GLD, the Gnu Linker.
6
7GLD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GLD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GLD; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
21
1c64c4ed 22/* ldfile.c: look after all the file stuff. */
252b5132
RH
23
24#include "bfd.h"
25#include "sysdep.h"
26#include "bfdlink.h"
3882b010 27#include "safe-ctype.h"
252b5132
RH
28#include "ld.h"
29#include "ldmisc.h"
30#include "ldexp.h"
31#include "ldlang.h"
32#include "ldfile.h"
33#include "ldmain.h"
34#include "ldgram.h"
35#include "ldlex.h"
36#include "ldemul.h"
d1b2b2dc 37#include "libiberty.h"
252b5132 38
252b5132
RH
39const char *ldfile_input_filename;
40boolean ldfile_assumed_script = false;
41const char *ldfile_output_machine_name = "";
42unsigned long ldfile_output_machine;
43enum bfd_architecture ldfile_output_architecture;
44search_dirs_type *search_head;
45
46#ifndef MPW
47#ifdef VMS
48char *slash = "";
49#else
50#if defined (_WIN32) && ! defined (__CYGWIN32__)
51char *slash = "\\";
52#else
53char *slash = "/";
54#endif
55#endif
56#else /* MPW */
1c64c4ed 57/* The MPW path char is a colon. */
252b5132
RH
58char *slash = ":";
59#endif /* MPW */
60
61/* LOCAL */
62
63static search_dirs_type **search_tail_ptr = &search_head;
64
89cdebba 65typedef struct search_arch {
4de2d33d 66 char *name;
252b5132
RH
67 struct search_arch *next;
68} search_arch_type;
69
70static search_arch_type *search_arch_head;
71static search_arch_type **search_arch_tail_ptr = &search_arch_head;
4de2d33d 72
252b5132
RH
73static FILE *try_open PARAMS ((const char *name, const char *exten));
74
75void
76ldfile_add_library_path (name, cmdline)
77 const char *name;
78 boolean cmdline;
79{
80 search_dirs_type *new;
81
82 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
83 new->next = NULL;
84 new->name = name;
85 new->cmdline = cmdline;
86 *search_tail_ptr = new;
87 search_tail_ptr = &new->next;
88}
89
90/* Try to open a BFD for a lang_input_statement. */
91
92boolean
93ldfile_try_open_bfd (attempt, entry)
94 const char *attempt;
95 lang_input_statement_type *entry;
96{
97 entry->the_bfd = bfd_openr (attempt, entry->target);
98
99 if (trace_file_tries)
100 {
101 if (entry->the_bfd == NULL)
102 info_msg (_("attempt to open %s failed\n"), attempt);
103 else
104 info_msg (_("attempt to open %s succeeded\n"), attempt);
105 }
106
b90d1146 107 if (entry->the_bfd == NULL)
252b5132
RH
108 {
109 if (bfd_get_error () == bfd_error_invalid_target)
110 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
111 return false;
112 }
b90d1146
ILT
113
114 /* If we are searching for this file, see if the architecture is
115 compatible with the output file. If it isn't, keep searching.
116 If we can't open the file as an object file, stop the search
117 here. */
118
119 if (entry->search_dirs_flag)
120 {
121 bfd *check;
122
123 if (bfd_check_format (entry->the_bfd, bfd_archive))
124 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
125 else
126 check = entry->the_bfd;
127
a9998805 128 if (check != NULL)
b90d1146 129 {
a9998805
ILT
130 if (! bfd_check_format (check, bfd_object))
131 return true;
132 if (bfd_arch_get_compatible (check, output_bfd) == NULL)
133 {
1c64c4ed 134 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
135 attempt, entry->local_sym_name);
136 bfd_close (entry->the_bfd);
137 entry->the_bfd = NULL;
138 return false;
139 }
b90d1146
ILT
140 }
141 }
142
143 return true;
252b5132
RH
144}
145
146/* Search for and open the file specified by ENTRY. If it is an
147 archive, use ARCH, LIB and SUFFIX to modify the file name. */
148
344a211f 149boolean
252b5132
RH
150ldfile_open_file_search (arch, entry, lib, suffix)
151 const char *arch;
152 lang_input_statement_type *entry;
153 const char *lib;
154 const char *suffix;
155{
156 search_dirs_type *search;
157
158 /* If this is not an archive, try to open it in the current
159 directory first. */
160 if (! entry->is_archive)
161 {
162 if (ldfile_try_open_bfd (entry->filename, entry))
163 return true;
164 }
165
166 for (search = search_head;
89cdebba 167 search != (search_dirs_type *) NULL;
4de2d33d 168 search = search->next)
252b5132
RH
169 {
170 char *string;
171
172 if (entry->dynamic && ! link_info.relocateable)
173 {
174 if (ldemul_open_dynamic_archive (arch, search, entry))
175 return true;
176 }
177
178 string = (char *) xmalloc (strlen (search->name)
179 + strlen (slash)
180 + strlen (lib)
181 + strlen (entry->filename)
182 + strlen (arch)
183 + strlen (suffix)
184 + 1);
185
186 if (entry->is_archive)
187 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
188 lib, entry->filename, arch, suffix);
189 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
190#if defined (__MSDOS__) || defined (_WIN32)
4de2d33d 191 || entry->filename[0] == '\\'
3882b010 192 || (ISALPHA (entry->filename[0])
252b5132
RH
193 && entry->filename[1] == ':')
194#endif
195 )
196 strcpy (string, entry->filename);
197 else
198 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
199
200 if (ldfile_try_open_bfd (string, entry))
201 {
b90d1146
ILT
202 entry->filename = string;
203 return true;
252b5132
RH
204 }
205
206 free (string);
207 }
208
209 return false;
210}
211
212/* Open the input file specified by ENTRY. */
213
214void
215ldfile_open_file (entry)
216 lang_input_statement_type *entry;
217{
218 if (entry->the_bfd != NULL)
219 return;
220
221 if (! entry->search_dirs_flag)
222 {
223 if (ldfile_try_open_bfd (entry->filename, entry))
224 return;
225 if (strcmp (entry->filename, entry->local_sym_name) != 0)
226 einfo (_("%F%P: cannot open %s for %s: %E\n"),
227 entry->filename, entry->local_sym_name);
228 else
1c64c4ed 229 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
252b5132
RH
230 }
231 else
232 {
233 search_arch_type *arch;
78f85fd7 234 boolean found = false;
252b5132
RH
235
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)
240 {
78f85fd7
L
241 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
242 if (found)
243 break;
252b5132 244#ifdef VMS
78f85fd7
L
245 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
246 if (found)
247 break;
252b5132 248#endif
78f85fd7
L
249 found = ldemul_find_potential_libraries (arch->name, entry);
250 if (found)
251 break;
252b5132 252 }
4de2d33d 253
78f85fd7
L
254 /* If we have found the file, we don't need to search directories
255 again. */
256 if (found)
257 entry->search_dirs_flag = false;
258 else
259 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
260 }
261}
262
263/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
264
265static FILE *
266try_open (name, exten)
267 const char *name;
268 const char *exten;
269{
270 FILE *result;
271 char buff[1000];
272
273 result = fopen (name, "r");
4de2d33d 274
252b5132
RH
275 if (trace_file_tries)
276 {
277 if (result == NULL)
278 info_msg (_("cannot find script file %s\n"), name);
279 else
280 info_msg (_("opened script file %s\n"), name);
281 }
282
283 if (result != NULL)
284 return result;
285
286 if (*exten)
287 {
288 sprintf (buff, "%s%s", name, exten);
289 result = fopen (buff, "r");
4de2d33d 290
252b5132
RH
291 if (trace_file_tries)
292 {
293 if (result == NULL)
294 info_msg (_("cannot find script file %s\n"), buff);
295 else
296 info_msg (_("opened script file %s\n"), buff);
297 }
298 }
299
300 return result;
301}
302
303/* Try to open NAME; if that fails, look for it in any directories
304 specified with -L, without and with EXTEND apppended. */
305
306FILE *
307ldfile_find_command_file (name, extend)
308 const char *name;
309 const char *extend;
310{
311 search_dirs_type *search;
312 FILE *result;
313 char buffer[1000];
314
89cdebba 315 /* First try raw name. */
1c64c4ed 316 result = try_open (name, "");
89cdebba 317 if (result == (FILE *) NULL)
1c64c4ed 318 {
89cdebba 319 /* Try now prefixes. */
1c64c4ed 320 for (search = search_head;
89cdebba 321 search != (search_dirs_type *) NULL;
1c64c4ed
NC
322 search = search->next)
323 {
89cdebba 324 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 325
1c64c4ed
NC
326 result = try_open (buffer, extend);
327 if (result)
328 break;
329 }
252b5132 330 }
4de2d33d 331
252b5132
RH
332 return result;
333}
334
335void
336ldfile_open_command_file (name)
337 const char *name;
338{
339 FILE *ldlex_input_stack;
1c64c4ed 340 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 341
89cdebba 342 if (ldlex_input_stack == (FILE *) NULL)
1c64c4ed
NC
343 {
344 bfd_set_error (bfd_error_system_call);
345 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
346 }
4de2d33d 347
1c64c4ed 348 lex_push_file (ldlex_input_stack, name);
4de2d33d 349
252b5132
RH
350 ldfile_input_filename = name;
351 lineno = 1;
b9a8de1e
NC
352
353 saved_script_handle = ldlex_input_stack;
252b5132
RH
354}
355
252b5132 356#ifdef GNU960
1c64c4ed
NC
357static char *
358gnu960_map_archname (name)
359 char *name;
252b5132
RH
360{
361 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
362 static struct tabentry arch_tab[] =
363 {
252b5132
RH
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, ""
373 };
374 struct tabentry *tp;
252b5132 375
1c64c4ed
NC
376 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
377 {
378 if (! strcmp (name,tp->cmd_switch))
379 break;
252b5132 380 }
252b5132 381
1c64c4ed 382 if (tp->cmd_switch == NULL)
89cdebba 383 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 384
252b5132
RH
385 return tp->arch;
386}
387
252b5132 388void
1c64c4ed
NC
389ldfile_add_arch (name)
390 char *name;
252b5132
RH
391{
392 search_arch_type *new =
89cdebba 393 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
252b5132 394
1c64c4ed
NC
395 if (*name != '\0')
396 {
397 if (ldfile_output_machine_name[0] != '\0')
398 {
399 einfo (_("%P%F: target architecture respecified\n"));
400 return;
401 }
4de2d33d 402
1c64c4ed 403 ldfile_output_machine_name = name;
252b5132 404 }
252b5132 405
89cdebba 406 new->next = (search_arch_type *) NULL;
1c64c4ed 407 new->name = gnu960_map_archname (name);
252b5132
RH
408 *search_arch_tail_ptr = new;
409 search_arch_tail_ptr = &new->next;
252b5132
RH
410}
411
89cdebba 412#else /* not GNU960 */
252b5132 413
252b5132
RH
414void
415ldfile_add_arch (in_name)
89cdebba 416 CONST char *in_name;
252b5132 417{
d1b2b2dc 418 char *name = xstrdup (in_name);
252b5132
RH
419 search_arch_type *new =
420 (search_arch_type *) xmalloc (sizeof (search_arch_type));
421
422 ldfile_output_machine_name = in_name;
423
424 new->name = name;
89cdebba 425 new->next = (search_arch_type *) NULL;
252b5132
RH
426 while (*name)
427 {
3882b010 428 *name = TOLOWER (*name);
252b5132
RH
429 name++;
430 }
431 *search_arch_tail_ptr = new;
432 search_arch_tail_ptr = &new->next;
433
434}
435#endif
436
89cdebba
KH
437/* Set the output architecture. */
438
252b5132
RH
439void
440ldfile_set_output_arch (string)
441 CONST char *string;
442{
1c64c4ed
NC
443 const bfd_arch_info_type *arch = bfd_scan_arch (string);
444
445 if (arch)
446 {
447 ldfile_output_architecture = arch->arch;
448 ldfile_output_machine = arch->mach;
449 ldfile_output_machine_name = arch->printable_name;
450 }
451 else
452 {
453 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
454 }
252b5132 455}
This page took 0.132662 seconds and 4 git commands to generate.