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