2002-11-30 Nathanael Nerode <neroden@gcc.gnu.org>
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805 1/* Linker file opening and searching.
b7a26f91 2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002
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"
df2a7313 34#include <ldgram.h>
252b5132
RH
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
361b220e
CD
82 if (!cmdline && config.only_cmd_line_lib_dirs)
83 return;
84
252b5132
RH
85 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
86 new->next = NULL;
87 new->name = name;
88 new->cmdline = cmdline;
89 *search_tail_ptr = new;
90 search_tail_ptr = &new->next;
91}
92
93/* Try to open a BFD for a lang_input_statement. */
94
95boolean
96ldfile_try_open_bfd (attempt, entry)
97 const char *attempt;
98 lang_input_statement_type *entry;
99{
100 entry->the_bfd = bfd_openr (attempt, entry->target);
101
102 if (trace_file_tries)
103 {
104 if (entry->the_bfd == NULL)
105 info_msg (_("attempt to open %s failed\n"), attempt);
106 else
107 info_msg (_("attempt to open %s succeeded\n"), attempt);
108 }
109
b90d1146 110 if (entry->the_bfd == NULL)
252b5132
RH
111 {
112 if (bfd_get_error () == bfd_error_invalid_target)
113 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
114 return false;
115 }
b90d1146
ILT
116
117 /* If we are searching for this file, see if the architecture is
118 compatible with the output file. If it isn't, keep searching.
119 If we can't open the file as an object file, stop the search
120 here. */
121
122 if (entry->search_dirs_flag)
123 {
124 bfd *check;
125
126 if (bfd_check_format (entry->the_bfd, bfd_archive))
127 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
128 else
129 check = entry->the_bfd;
130
a9998805 131 if (check != NULL)
b90d1146 132 {
a9998805 133 if (! bfd_check_format (check, bfd_object))
599917b8
JJ
134 {
135 if (check == entry->the_bfd
136 && bfd_get_error () == bfd_error_file_not_recognized
137 && ! ldemul_unrecognized_file (entry))
138 {
139 int token, skip = 0;
140 char *arg, *arg1, *arg2, *arg3;
141 extern FILE *yyin;
142
143 /* Try to interpret the file as a linker script. */
144 ldfile_open_command_file (attempt);
145
146 ldfile_assumed_script = true;
147 parser_input = input_selected;
148 ldlex_both ();
149 token = INPUT_SCRIPT;
150 while (token != 0)
151 {
152 switch (token)
153 {
154 case OUTPUT_FORMAT:
155 if ((token = yylex ()) != '(')
156 continue;
157 if ((token = yylex ()) != NAME)
158 continue;
159 arg1 = yylval.name;
160 arg2 = NULL;
161 arg3 = NULL;
162 token = yylex ();
163 if (token == ',')
164 {
165 if ((token = yylex ()) != NAME)
166 {
167 free (arg1);
168 continue;
169 }
170 arg2 = yylval.name;
171 if ((token = yylex ()) != ','
172 || (token = yylex ()) != NAME)
173 {
174 free (arg1);
175 free (arg2);
176 continue;
177 }
178 arg3 = yylval.name;
179 token = yylex ();
180 }
181 if (token == ')')
182 {
183 switch (command_line.endian)
184 {
185 default:
186 case ENDIAN_UNSET:
187 arg = arg1; break;
188 case ENDIAN_BIG:
189 arg = arg2 ? arg2 : arg1; break;
190 case ENDIAN_LITTLE:
191 arg = arg3 ? arg3 : arg1; break;
192 }
193 if (strcmp (arg, lang_get_output_target ()) != 0)
194 skip = 1;
195 }
196 free (arg1);
197 if (arg2) free (arg2);
198 if (arg3) free (arg3);
199 break;
200 case NAME:
201 case LNAME:
202 case VERS_IDENTIFIER:
203 case VERS_TAG:
204 free (yylval.name);
205 break;
206 case INT:
207 if (yylval.bigint.str)
208 free (yylval.bigint.str);
209 break;
210 }
211 token = yylex ();
212 }
213 ldfile_assumed_script = false;
214 fclose (yyin);
215 yyin = NULL;
216 if (skip)
217 {
218 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
219 attempt, entry->local_sym_name);
220 bfd_close (entry->the_bfd);
221 entry->the_bfd = NULL;
222 return false;
223 }
224 }
225 return true;
226 }
f1f0d9ab
TR
227
228 if ((bfd_arch_get_compatible (check, output_bfd) == NULL)
229 /* XCOFF archives can have 32 and 64 bit objects */
230 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
71daf8b4 231 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
f1f0d9ab 232 && bfd_check_format (entry->the_bfd, bfd_archive)))
a9998805 233 {
1c64c4ed 234 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
235 attempt, entry->local_sym_name);
236 bfd_close (entry->the_bfd);
237 entry->the_bfd = NULL;
238 return false;
239 }
b90d1146
ILT
240 }
241 }
242
243 return true;
252b5132
RH
244}
245
246/* Search for and open the file specified by ENTRY. If it is an
247 archive, use ARCH, LIB and SUFFIX to modify the file name. */
248
344a211f 249boolean
252b5132
RH
250ldfile_open_file_search (arch, entry, lib, suffix)
251 const char *arch;
252 lang_input_statement_type *entry;
253 const char *lib;
254 const char *suffix;
255{
256 search_dirs_type *search;
257
258 /* If this is not an archive, try to open it in the current
259 directory first. */
260 if (! entry->is_archive)
261 {
262 if (ldfile_try_open_bfd (entry->filename, entry))
263 return true;
264 }
265
266 for (search = search_head;
89cdebba 267 search != (search_dirs_type *) NULL;
4de2d33d 268 search = search->next)
252b5132
RH
269 {
270 char *string;
271
272 if (entry->dynamic && ! link_info.relocateable)
273 {
274 if (ldemul_open_dynamic_archive (arch, search, entry))
275 return true;
276 }
277
278 string = (char *) xmalloc (strlen (search->name)
279 + strlen (slash)
280 + strlen (lib)
281 + strlen (entry->filename)
282 + strlen (arch)
283 + strlen (suffix)
284 + 1);
285
286 if (entry->is_archive)
287 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
288 lib, entry->filename, arch, suffix);
289 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
290#if defined (__MSDOS__) || defined (_WIN32)
4de2d33d 291 || entry->filename[0] == '\\'
3882b010 292 || (ISALPHA (entry->filename[0])
252b5132
RH
293 && entry->filename[1] == ':')
294#endif
295 )
296 strcpy (string, entry->filename);
297 else
298 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
299
300 if (ldfile_try_open_bfd (string, entry))
301 {
b90d1146
ILT
302 entry->filename = string;
303 return true;
252b5132
RH
304 }
305
306 free (string);
307 }
308
309 return false;
310}
311
312/* Open the input file specified by ENTRY. */
313
314void
315ldfile_open_file (entry)
316 lang_input_statement_type *entry;
317{
318 if (entry->the_bfd != NULL)
319 return;
320
321 if (! entry->search_dirs_flag)
322 {
323 if (ldfile_try_open_bfd (entry->filename, entry))
324 return;
325 if (strcmp (entry->filename, entry->local_sym_name) != 0)
326 einfo (_("%F%P: cannot open %s for %s: %E\n"),
327 entry->filename, entry->local_sym_name);
328 else
1c64c4ed 329 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
252b5132
RH
330 }
331 else
332 {
333 search_arch_type *arch;
78f85fd7 334 boolean found = false;
252b5132
RH
335
336 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
337 for (arch = search_arch_head;
338 arch != (search_arch_type *) NULL;
339 arch = arch->next)
340 {
78f85fd7
L
341 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
342 if (found)
343 break;
252b5132 344#ifdef VMS
78f85fd7
L
345 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
346 if (found)
347 break;
252b5132 348#endif
78f85fd7
L
349 found = ldemul_find_potential_libraries (arch->name, entry);
350 if (found)
351 break;
252b5132 352 }
4de2d33d 353
78f85fd7
L
354 /* If we have found the file, we don't need to search directories
355 again. */
356 if (found)
357 entry->search_dirs_flag = false;
358 else
359 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
360 }
361}
362
363/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
364
365static FILE *
366try_open (name, exten)
367 const char *name;
368 const char *exten;
369{
370 FILE *result;
371 char buff[1000];
372
373 result = fopen (name, "r");
4de2d33d 374
252b5132
RH
375 if (trace_file_tries)
376 {
377 if (result == NULL)
378 info_msg (_("cannot find script file %s\n"), name);
379 else
380 info_msg (_("opened script file %s\n"), name);
381 }
382
383 if (result != NULL)
384 return result;
385
386 if (*exten)
387 {
388 sprintf (buff, "%s%s", name, exten);
389 result = fopen (buff, "r");
4de2d33d 390
252b5132
RH
391 if (trace_file_tries)
392 {
393 if (result == NULL)
394 info_msg (_("cannot find script file %s\n"), buff);
395 else
396 info_msg (_("opened script file %s\n"), buff);
397 }
398 }
399
400 return result;
401}
402
403/* Try to open NAME; if that fails, look for it in any directories
404 specified with -L, without and with EXTEND apppended. */
405
406FILE *
407ldfile_find_command_file (name, extend)
408 const char *name;
409 const char *extend;
410{
411 search_dirs_type *search;
412 FILE *result;
413 char buffer[1000];
414
89cdebba 415 /* First try raw name. */
1c64c4ed 416 result = try_open (name, "");
89cdebba 417 if (result == (FILE *) NULL)
1c64c4ed 418 {
89cdebba 419 /* Try now prefixes. */
1c64c4ed 420 for (search = search_head;
89cdebba 421 search != (search_dirs_type *) NULL;
1c64c4ed
NC
422 search = search->next)
423 {
89cdebba 424 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 425
1c64c4ed
NC
426 result = try_open (buffer, extend);
427 if (result)
428 break;
429 }
252b5132 430 }
4de2d33d 431
252b5132
RH
432 return result;
433}
434
435void
436ldfile_open_command_file (name)
437 const char *name;
438{
439 FILE *ldlex_input_stack;
1c64c4ed 440 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 441
89cdebba 442 if (ldlex_input_stack == (FILE *) NULL)
1c64c4ed
NC
443 {
444 bfd_set_error (bfd_error_system_call);
445 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
446 }
4de2d33d 447
1c64c4ed 448 lex_push_file (ldlex_input_stack, name);
4de2d33d 449
252b5132
RH
450 ldfile_input_filename = name;
451 lineno = 1;
b7a26f91 452
b9a8de1e 453 saved_script_handle = ldlex_input_stack;
252b5132
RH
454}
455
252b5132 456#ifdef GNU960
1c64c4ed
NC
457static char *
458gnu960_map_archname (name)
459 char *name;
252b5132
RH
460{
461 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
462 static struct tabentry arch_tab[] =
463 {
252b5132
RH
464 "", "",
465 "KA", "ka",
466 "KB", "kb",
467 "KC", "mc", /* Synonym for MC */
468 "MC", "mc",
469 "CA", "ca",
470 "SA", "ka", /* Functionally equivalent to KA */
471 "SB", "kb", /* Functionally equivalent to KB */
472 NULL, ""
473 };
474 struct tabentry *tp;
252b5132 475
1c64c4ed
NC
476 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
477 {
478 if (! strcmp (name,tp->cmd_switch))
479 break;
252b5132 480 }
252b5132 481
1c64c4ed 482 if (tp->cmd_switch == NULL)
89cdebba 483 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 484
252b5132
RH
485 return tp->arch;
486}
487
252b5132 488void
1c64c4ed
NC
489ldfile_add_arch (name)
490 char *name;
252b5132
RH
491{
492 search_arch_type *new =
89cdebba 493 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
252b5132 494
1c64c4ed
NC
495 if (*name != '\0')
496 {
497 if (ldfile_output_machine_name[0] != '\0')
498 {
499 einfo (_("%P%F: target architecture respecified\n"));
500 return;
501 }
4de2d33d 502
1c64c4ed 503 ldfile_output_machine_name = name;
252b5132 504 }
252b5132 505
89cdebba 506 new->next = (search_arch_type *) NULL;
1c64c4ed 507 new->name = gnu960_map_archname (name);
252b5132
RH
508 *search_arch_tail_ptr = new;
509 search_arch_tail_ptr = &new->next;
252b5132
RH
510}
511
89cdebba 512#else /* not GNU960 */
252b5132 513
252b5132
RH
514void
515ldfile_add_arch (in_name)
4da711b1 516 const char *in_name;
252b5132 517{
d1b2b2dc 518 char *name = xstrdup (in_name);
252b5132
RH
519 search_arch_type *new =
520 (search_arch_type *) xmalloc (sizeof (search_arch_type));
521
522 ldfile_output_machine_name = in_name;
523
524 new->name = name;
89cdebba 525 new->next = (search_arch_type *) NULL;
252b5132
RH
526 while (*name)
527 {
3882b010 528 *name = TOLOWER (*name);
252b5132
RH
529 name++;
530 }
531 *search_arch_tail_ptr = new;
532 search_arch_tail_ptr = &new->next;
533
534}
535#endif
536
89cdebba
KH
537/* Set the output architecture. */
538
252b5132
RH
539void
540ldfile_set_output_arch (string)
4da711b1 541 const char *string;
252b5132 542{
1c64c4ed
NC
543 const bfd_arch_info_type *arch = bfd_scan_arch (string);
544
545 if (arch)
546 {
547 ldfile_output_architecture = arch->arch;
548 ldfile_output_machine = arch->mach;
549 ldfile_output_machine_name = arch->printable_name;
550 }
551 else
552 {
553 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
554 }
252b5132 555}
This page took 0.161724 seconds and 4 git commands to generate.