(HAVE_REALPATH): New entry.
[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 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;
91}
92
93/* Try to open a BFD for a lang_input_statement. */
94
b34976b6 95bfd_boolean
252b5132
RH
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);
b34976b6 114 return FALSE;
252b5132 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);
b34976b6
AM
145
146 ldfile_assumed_script = TRUE;
599917b8
JJ
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 }
b34976b6 213 ldfile_assumed_script = FALSE;
599917b8
JJ
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;
b34976b6 222 return FALSE;
599917b8
JJ
223 }
224 }
b34976b6 225 return TRUE;
599917b8 226 }
f1f0d9ab 227
312b768e
NC
228 if ((bfd_arch_get_compatible (check, output_bfd,
229 command_line.accept_unknown_input_arch) == NULL)
230 /* XCOFF archives can have 32 and 64 bit objects. */
f1f0d9ab 231 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
71daf8b4 232 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
f1f0d9ab 233 && bfd_check_format (entry->the_bfd, bfd_archive)))
a9998805 234 {
1c64c4ed 235 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
236 attempt, entry->local_sym_name);
237 bfd_close (entry->the_bfd);
238 entry->the_bfd = NULL;
b34976b6 239 return FALSE;
a9998805 240 }
b90d1146
ILT
241 }
242 }
243
b34976b6 244 return TRUE;
252b5132
RH
245}
246
247/* Search for and open the file specified by ENTRY. If it is an
248 archive, use ARCH, LIB and SUFFIX to modify the file name. */
249
b34976b6 250bfd_boolean
252b5132
RH
251ldfile_open_file_search (arch, entry, lib, suffix)
252 const char *arch;
253 lang_input_statement_type *entry;
254 const char *lib;
255 const char *suffix;
256{
257 search_dirs_type *search;
258
259 /* If this is not an archive, try to open it in the current
260 directory first. */
261 if (! entry->is_archive)
262 {
263 if (ldfile_try_open_bfd (entry->filename, entry))
b34976b6 264 return TRUE;
252b5132
RH
265 }
266
267 for (search = search_head;
89cdebba 268 search != (search_dirs_type *) NULL;
4de2d33d 269 search = search->next)
252b5132
RH
270 {
271 char *string;
272
273 if (entry->dynamic && ! link_info.relocateable)
274 {
275 if (ldemul_open_dynamic_archive (arch, search, entry))
b34976b6 276 return TRUE;
252b5132
RH
277 }
278
279 string = (char *) xmalloc (strlen (search->name)
280 + strlen (slash)
281 + strlen (lib)
282 + strlen (entry->filename)
283 + strlen (arch)
284 + strlen (suffix)
285 + 1);
286
287 if (entry->is_archive)
288 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
289 lib, entry->filename, arch, suffix);
290 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
291#if defined (__MSDOS__) || defined (_WIN32)
4de2d33d 292 || entry->filename[0] == '\\'
3882b010 293 || (ISALPHA (entry->filename[0])
252b5132
RH
294 && entry->filename[1] == ':')
295#endif
296 )
297 strcpy (string, entry->filename);
298 else
299 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
300
301 if (ldfile_try_open_bfd (string, entry))
302 {
b90d1146 303 entry->filename = string;
b34976b6 304 return TRUE;
252b5132
RH
305 }
306
307 free (string);
308 }
309
b34976b6 310 return FALSE;
252b5132
RH
311}
312
313/* Open the input file specified by ENTRY. */
314
315void
316ldfile_open_file (entry)
317 lang_input_statement_type *entry;
318{
319 if (entry->the_bfd != NULL)
320 return;
321
322 if (! entry->search_dirs_flag)
323 {
324 if (ldfile_try_open_bfd (entry->filename, entry))
325 return;
326 if (strcmp (entry->filename, entry->local_sym_name) != 0)
327 einfo (_("%F%P: cannot open %s for %s: %E\n"),
328 entry->filename, entry->local_sym_name);
329 else
1c64c4ed 330 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
252b5132
RH
331 }
332 else
333 {
334 search_arch_type *arch;
b34976b6 335 bfd_boolean found = FALSE;
252b5132
RH
336
337 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
338 for (arch = search_arch_head;
339 arch != (search_arch_type *) NULL;
340 arch = arch->next)
341 {
78f85fd7
L
342 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
343 if (found)
344 break;
252b5132 345#ifdef VMS
78f85fd7
L
346 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
347 if (found)
348 break;
252b5132 349#endif
78f85fd7
L
350 found = ldemul_find_potential_libraries (arch->name, entry);
351 if (found)
352 break;
252b5132 353 }
4de2d33d 354
78f85fd7
L
355 /* If we have found the file, we don't need to search directories
356 again. */
357 if (found)
b34976b6 358 entry->search_dirs_flag = FALSE;
78f85fd7
L
359 else
360 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
361 }
362}
363
364/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
365
366static FILE *
367try_open (name, exten)
368 const char *name;
369 const char *exten;
370{
371 FILE *result;
372 char buff[1000];
373
374 result = fopen (name, "r");
4de2d33d 375
252b5132
RH
376 if (trace_file_tries)
377 {
378 if (result == NULL)
379 info_msg (_("cannot find script file %s\n"), name);
380 else
381 info_msg (_("opened script file %s\n"), name);
382 }
383
384 if (result != NULL)
385 return result;
386
387 if (*exten)
388 {
389 sprintf (buff, "%s%s", name, exten);
390 result = fopen (buff, "r");
4de2d33d 391
252b5132
RH
392 if (trace_file_tries)
393 {
394 if (result == NULL)
395 info_msg (_("cannot find script file %s\n"), buff);
396 else
397 info_msg (_("opened script file %s\n"), buff);
398 }
399 }
400
401 return result;
402}
403
404/* Try to open NAME; if that fails, look for it in any directories
405 specified with -L, without and with EXTEND apppended. */
406
407FILE *
408ldfile_find_command_file (name, extend)
409 const char *name;
410 const char *extend;
411{
412 search_dirs_type *search;
413 FILE *result;
414 char buffer[1000];
415
89cdebba 416 /* First try raw name. */
1c64c4ed 417 result = try_open (name, "");
89cdebba 418 if (result == (FILE *) NULL)
1c64c4ed 419 {
89cdebba 420 /* Try now prefixes. */
1c64c4ed 421 for (search = search_head;
89cdebba 422 search != (search_dirs_type *) NULL;
1c64c4ed
NC
423 search = search->next)
424 {
89cdebba 425 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 426
1c64c4ed
NC
427 result = try_open (buffer, extend);
428 if (result)
429 break;
430 }
252b5132 431 }
4de2d33d 432
252b5132
RH
433 return result;
434}
435
436void
437ldfile_open_command_file (name)
438 const char *name;
439{
440 FILE *ldlex_input_stack;
1c64c4ed 441 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 442
89cdebba 443 if (ldlex_input_stack == (FILE *) NULL)
1c64c4ed
NC
444 {
445 bfd_set_error (bfd_error_system_call);
446 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
447 }
4de2d33d 448
1c64c4ed 449 lex_push_file (ldlex_input_stack, name);
4de2d33d 450
252b5132
RH
451 ldfile_input_filename = name;
452 lineno = 1;
b7a26f91 453
b9a8de1e 454 saved_script_handle = ldlex_input_stack;
252b5132
RH
455}
456
252b5132 457#ifdef GNU960
1c64c4ed
NC
458static char *
459gnu960_map_archname (name)
460 char *name;
252b5132
RH
461{
462 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
463 static struct tabentry arch_tab[] =
464 {
252b5132
RH
465 "", "",
466 "KA", "ka",
467 "KB", "kb",
468 "KC", "mc", /* Synonym for MC */
469 "MC", "mc",
470 "CA", "ca",
471 "SA", "ka", /* Functionally equivalent to KA */
472 "SB", "kb", /* Functionally equivalent to KB */
473 NULL, ""
474 };
475 struct tabentry *tp;
252b5132 476
1c64c4ed
NC
477 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
478 {
479 if (! strcmp (name,tp->cmd_switch))
480 break;
252b5132 481 }
252b5132 482
1c64c4ed 483 if (tp->cmd_switch == NULL)
89cdebba 484 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 485
252b5132
RH
486 return tp->arch;
487}
488
252b5132 489void
1c64c4ed
NC
490ldfile_add_arch (name)
491 char *name;
252b5132
RH
492{
493 search_arch_type *new =
89cdebba 494 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
252b5132 495
1c64c4ed
NC
496 if (*name != '\0')
497 {
498 if (ldfile_output_machine_name[0] != '\0')
499 {
500 einfo (_("%P%F: target architecture respecified\n"));
501 return;
502 }
4de2d33d 503
1c64c4ed 504 ldfile_output_machine_name = name;
252b5132 505 }
252b5132 506
89cdebba 507 new->next = (search_arch_type *) NULL;
1c64c4ed 508 new->name = gnu960_map_archname (name);
252b5132
RH
509 *search_arch_tail_ptr = new;
510 search_arch_tail_ptr = &new->next;
252b5132
RH
511}
512
89cdebba 513#else /* not GNU960 */
252b5132 514
252b5132
RH
515void
516ldfile_add_arch (in_name)
4da711b1 517 const char *in_name;
252b5132 518{
d1b2b2dc 519 char *name = xstrdup (in_name);
252b5132
RH
520 search_arch_type *new =
521 (search_arch_type *) xmalloc (sizeof (search_arch_type));
522
523 ldfile_output_machine_name = in_name;
524
525 new->name = name;
89cdebba 526 new->next = (search_arch_type *) NULL;
252b5132
RH
527 while (*name)
528 {
3882b010 529 *name = TOLOWER (*name);
252b5132
RH
530 name++;
531 }
532 *search_arch_tail_ptr = new;
533 search_arch_tail_ptr = &new->next;
534
535}
536#endif
537
89cdebba
KH
538/* Set the output architecture. */
539
252b5132
RH
540void
541ldfile_set_output_arch (string)
4da711b1 542 const char *string;
252b5132 543{
1c64c4ed
NC
544 const bfd_arch_info_type *arch = bfd_scan_arch (string);
545
546 if (arch)
547 {
548 ldfile_output_architecture = arch->arch;
549 ldfile_output_machine = arch->mach;
550 ldfile_output_machine_name = arch->printable_name;
551 }
552 else
553 {
554 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
555 }
252b5132 556}
This page took 0.172127 seconds and 4 git commands to generate.