Correct spelling of "relocatable".
[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 4
3fe38064 5 This file is part of GLD, the Gnu Linker.
252b5132 6
3fe38064
NC
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.
252b5132 11
3fe38064
NC
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.
252b5132 16
3fe38064
NC
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. */
252b5132 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"
3fe38064 38#include "filenames.h"
252b5132 39
3fe38064
NC
40const char * ldfile_input_filename;
41bfd_boolean ldfile_assumed_script = FALSE;
42const char * ldfile_output_machine_name = "";
252b5132
RH
43unsigned long ldfile_output_machine;
44enum bfd_architecture ldfile_output_architecture;
3fe38064 45search_dirs_type * search_head;
252b5132
RH
46
47#ifndef MPW
48#ifdef VMS
3fe38064 49char * slash = "";
252b5132
RH
50#else
51#if defined (_WIN32) && ! defined (__CYGWIN32__)
3fe38064 52char * slash = "\\";
252b5132 53#else
3fe38064 54char * slash = "/";
252b5132
RH
55#endif
56#endif
57#else /* MPW */
1c64c4ed 58/* The MPW path char is a colon. */
3fe38064 59char * slash = ":";
252b5132
RH
60#endif /* MPW */
61
3fe38064
NC
62typedef struct search_arch
63{
4de2d33d 64 char *name;
252b5132
RH
65 struct search_arch *next;
66} search_arch_type;
67
3fe38064 68static search_dirs_type **search_tail_ptr = &search_head;
252b5132
RH
69static search_arch_type *search_arch_head;
70static search_arch_type **search_arch_tail_ptr = &search_arch_head;
4de2d33d 71
3fe38064
NC
72static FILE *try_open
73 PARAMS ((const char *, const char *));
74static bfd_boolean is_sysrooted_pathname
75 PARAMS ((const char *, bfd_boolean));
76
77/* Test whether a pathname, after canonicalization, is the same or a
78 sub-directory of the sysroot directory. */
79
80static bfd_boolean
81is_sysrooted_pathname (name, notsame)
82 const char *name;
83 bfd_boolean notsame;
84{
85 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
86 int len;
87 bfd_boolean result;
88
89 if (! realname)
90 return FALSE;
91
92 len = strlen (realname);
93
94 if (((! notsame && len == ld_canon_sysroot_len)
95 || (len >= ld_canon_sysroot_len
96 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
97 && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
98 && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
99 result = TRUE;
100 else
101 result = FALSE;
102
103 if (realname)
104 free (realname);
105
106 return result;
107}
252b5132 108
5ed6aba4
NC
109/* Adds NAME to the library search path.
110 Makes a copy of NAME using xmalloc(). */
111
252b5132
RH
112void
113ldfile_add_library_path (name, cmdline)
114 const char *name;
b34976b6 115 bfd_boolean cmdline;
252b5132
RH
116{
117 search_dirs_type *new;
118
361b220e
CD
119 if (!cmdline && config.only_cmd_line_lib_dirs)
120 return;
121
252b5132
RH
122 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
123 new->next = NULL;
252b5132
RH
124 new->cmdline = cmdline;
125 *search_tail_ptr = new;
126 search_tail_ptr = &new->next;
9c8ebd6a
DJ
127
128 /* If a directory is marked as honoring sysroot, prepend the sysroot path
129 now. */
5ed6aba4 130 if (name[0] == '=')
e3f2db7f 131 {
5ed6aba4 132 new->name = concat (ld_sysroot, name + 1, NULL);
e3f2db7f
AO
133 new->sysrooted = TRUE;
134 }
135 else
5ed6aba4
NC
136 {
137 new->name = xstrdup (name);
138 new->sysrooted = is_sysrooted_pathname (name, FALSE);
139 }
252b5132
RH
140}
141
142/* Try to open a BFD for a lang_input_statement. */
143
b34976b6 144bfd_boolean
252b5132
RH
145ldfile_try_open_bfd (attempt, entry)
146 const char *attempt;
147 lang_input_statement_type *entry;
148{
149 entry->the_bfd = bfd_openr (attempt, entry->target);
150
151 if (trace_file_tries)
152 {
153 if (entry->the_bfd == NULL)
154 info_msg (_("attempt to open %s failed\n"), attempt);
155 else
156 info_msg (_("attempt to open %s succeeded\n"), attempt);
157 }
158
b90d1146 159 if (entry->the_bfd == NULL)
252b5132
RH
160 {
161 if (bfd_get_error () == bfd_error_invalid_target)
162 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
b34976b6 163 return FALSE;
252b5132 164 }
b90d1146
ILT
165
166 /* If we are searching for this file, see if the architecture is
167 compatible with the output file. If it isn't, keep searching.
168 If we can't open the file as an object file, stop the search
169 here. */
170
171 if (entry->search_dirs_flag)
172 {
173 bfd *check;
174
175 if (bfd_check_format (entry->the_bfd, bfd_archive))
176 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
177 else
178 check = entry->the_bfd;
179
a9998805 180 if (check != NULL)
b90d1146 181 {
a9998805 182 if (! bfd_check_format (check, bfd_object))
599917b8
JJ
183 {
184 if (check == entry->the_bfd
185 && bfd_get_error () == bfd_error_file_not_recognized
186 && ! ldemul_unrecognized_file (entry))
187 {
188 int token, skip = 0;
189 char *arg, *arg1, *arg2, *arg3;
190 extern FILE *yyin;
191
192 /* Try to interpret the file as a linker script. */
193 ldfile_open_command_file (attempt);
b34976b6
AM
194
195 ldfile_assumed_script = TRUE;
599917b8
JJ
196 parser_input = input_selected;
197 ldlex_both ();
198 token = INPUT_SCRIPT;
199 while (token != 0)
200 {
201 switch (token)
202 {
203 case OUTPUT_FORMAT:
204 if ((token = yylex ()) != '(')
205 continue;
206 if ((token = yylex ()) != NAME)
207 continue;
208 arg1 = yylval.name;
209 arg2 = NULL;
210 arg3 = NULL;
211 token = yylex ();
212 if (token == ',')
213 {
214 if ((token = yylex ()) != NAME)
215 {
216 free (arg1);
217 continue;
218 }
219 arg2 = yylval.name;
220 if ((token = yylex ()) != ','
221 || (token = yylex ()) != NAME)
222 {
223 free (arg1);
224 free (arg2);
225 continue;
226 }
227 arg3 = yylval.name;
228 token = yylex ();
229 }
230 if (token == ')')
231 {
232 switch (command_line.endian)
233 {
234 default:
235 case ENDIAN_UNSET:
236 arg = arg1; break;
237 case ENDIAN_BIG:
238 arg = arg2 ? arg2 : arg1; break;
239 case ENDIAN_LITTLE:
240 arg = arg3 ? arg3 : arg1; break;
241 }
242 if (strcmp (arg, lang_get_output_target ()) != 0)
243 skip = 1;
244 }
245 free (arg1);
246 if (arg2) free (arg2);
247 if (arg3) free (arg3);
248 break;
249 case NAME:
250 case LNAME:
251 case VERS_IDENTIFIER:
252 case VERS_TAG:
253 free (yylval.name);
254 break;
255 case INT:
256 if (yylval.bigint.str)
257 free (yylval.bigint.str);
258 break;
259 }
260 token = yylex ();
261 }
4f39e302 262 ldlex_popstate ();
b34976b6 263 ldfile_assumed_script = FALSE;
599917b8
JJ
264 fclose (yyin);
265 yyin = NULL;
266 if (skip)
267 {
268 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
269 attempt, entry->local_sym_name);
270 bfd_close (entry->the_bfd);
271 entry->the_bfd = NULL;
b34976b6 272 return FALSE;
599917b8
JJ
273 }
274 }
b34976b6 275 return TRUE;
599917b8 276 }
f1f0d9ab 277
312b768e
NC
278 if ((bfd_arch_get_compatible (check, output_bfd,
279 command_line.accept_unknown_input_arch) == NULL)
280 /* XCOFF archives can have 32 and 64 bit objects. */
f1f0d9ab 281 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
71daf8b4 282 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
f1f0d9ab 283 && bfd_check_format (entry->the_bfd, bfd_archive)))
a9998805 284 {
1c64c4ed 285 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
286 attempt, entry->local_sym_name);
287 bfd_close (entry->the_bfd);
288 entry->the_bfd = NULL;
b34976b6 289 return FALSE;
a9998805 290 }
b90d1146
ILT
291 }
292 }
293
b34976b6 294 return TRUE;
252b5132
RH
295}
296
297/* Search for and open the file specified by ENTRY. If it is an
298 archive, use ARCH, LIB and SUFFIX to modify the file name. */
299
b34976b6 300bfd_boolean
252b5132
RH
301ldfile_open_file_search (arch, entry, lib, suffix)
302 const char *arch;
303 lang_input_statement_type *entry;
304 const char *lib;
305 const char *suffix;
306{
307 search_dirs_type *search;
308
309 /* If this is not an archive, try to open it in the current
310 directory first. */
311 if (! entry->is_archive)
312 {
3fe38064 313 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
e3f2db7f
AO
314 {
315 char *name = concat (ld_sysroot, entry->filename,
316 (const char *) NULL);
317 if (ldfile_try_open_bfd (name, entry))
318 {
319 entry->filename = name;
320 return TRUE;
321 }
322 free (name);
323 }
324 else if (ldfile_try_open_bfd (entry->filename, entry))
325 {
3fe38064
NC
326 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
327 && is_sysrooted_pathname (entry->filename, TRUE);
e3f2db7f
AO
328 return TRUE;
329 }
3fe38064
NC
330
331 if (IS_ABSOLUTE_PATH (entry->filename))
332 return FALSE;
252b5132
RH
333 }
334
335 for (search = search_head;
89cdebba 336 search != (search_dirs_type *) NULL;
4de2d33d 337 search = search->next)
252b5132
RH
338 {
339 char *string;
340
1049f94e 341 if (entry->dynamic && ! link_info.relocatable)
252b5132
RH
342 {
343 if (ldemul_open_dynamic_archive (arch, search, entry))
e3f2db7f
AO
344 {
345 entry->sysrooted = search->sysrooted;
346 return TRUE;
347 }
252b5132
RH
348 }
349
350 string = (char *) xmalloc (strlen (search->name)
351 + strlen (slash)
352 + strlen (lib)
353 + strlen (entry->filename)
354 + strlen (arch)
355 + strlen (suffix)
356 + 1);
357
358 if (entry->is_archive)
359 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
360 lib, entry->filename, arch, suffix);
252b5132
RH
361 else
362 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
363
364 if (ldfile_try_open_bfd (string, entry))
365 {
b90d1146 366 entry->filename = string;
e3f2db7f 367 entry->sysrooted = search->sysrooted;
b34976b6 368 return TRUE;
252b5132
RH
369 }
370
371 free (string);
372 }
373
b34976b6 374 return FALSE;
252b5132
RH
375}
376
377/* Open the input file specified by ENTRY. */
378
379void
380ldfile_open_file (entry)
381 lang_input_statement_type *entry;
382{
383 if (entry->the_bfd != NULL)
384 return;
385
386 if (! entry->search_dirs_flag)
387 {
388 if (ldfile_try_open_bfd (entry->filename, entry))
389 return;
390 if (strcmp (entry->filename, entry->local_sym_name) != 0)
391 einfo (_("%F%P: cannot open %s for %s: %E\n"),
392 entry->filename, entry->local_sym_name);
393 else
1c64c4ed 394 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
252b5132
RH
395 }
396 else
397 {
398 search_arch_type *arch;
b34976b6 399 bfd_boolean found = FALSE;
252b5132
RH
400
401 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
402 for (arch = search_arch_head;
403 arch != (search_arch_type *) NULL;
404 arch = arch->next)
405 {
78f85fd7
L
406 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
407 if (found)
408 break;
252b5132 409#ifdef VMS
78f85fd7
L
410 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
411 if (found)
412 break;
252b5132 413#endif
78f85fd7
L
414 found = ldemul_find_potential_libraries (arch->name, entry);
415 if (found)
416 break;
252b5132 417 }
4de2d33d 418
78f85fd7
L
419 /* If we have found the file, we don't need to search directories
420 again. */
421 if (found)
b34976b6 422 entry->search_dirs_flag = FALSE;
3fe38064
NC
423 else if (entry->sysrooted
424 && ld_sysroot
425 && IS_ABSOLUTE_PATH (entry->local_sym_name))
426 einfo (_("%F%P: cannot find %s inside %s\n"),
427 entry->local_sym_name, ld_sysroot);
78f85fd7
L
428 else
429 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
430 }
431}
432
433/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
434
435static FILE *
436try_open (name, exten)
437 const char *name;
438 const char *exten;
439{
440 FILE *result;
441 char buff[1000];
442
443 result = fopen (name, "r");
4de2d33d 444
252b5132
RH
445 if (trace_file_tries)
446 {
447 if (result == NULL)
448 info_msg (_("cannot find script file %s\n"), name);
449 else
450 info_msg (_("opened script file %s\n"), name);
451 }
452
453 if (result != NULL)
454 return result;
455
456 if (*exten)
457 {
458 sprintf (buff, "%s%s", name, exten);
459 result = fopen (buff, "r");
4de2d33d 460
252b5132
RH
461 if (trace_file_tries)
462 {
463 if (result == NULL)
464 info_msg (_("cannot find script file %s\n"), buff);
465 else
466 info_msg (_("opened script file %s\n"), buff);
467 }
468 }
469
470 return result;
471}
472
473/* Try to open NAME; if that fails, look for it in any directories
396a2467 474 specified with -L, without and with EXTEND appended. */
252b5132
RH
475
476FILE *
477ldfile_find_command_file (name, extend)
478 const char *name;
479 const char *extend;
480{
481 search_dirs_type *search;
482 FILE *result;
483 char buffer[1000];
484
89cdebba 485 /* First try raw name. */
1c64c4ed 486 result = try_open (name, "");
89cdebba 487 if (result == (FILE *) NULL)
1c64c4ed 488 {
89cdebba 489 /* Try now prefixes. */
1c64c4ed 490 for (search = search_head;
89cdebba 491 search != (search_dirs_type *) NULL;
1c64c4ed
NC
492 search = search->next)
493 {
89cdebba 494 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 495
1c64c4ed
NC
496 result = try_open (buffer, extend);
497 if (result)
498 break;
499 }
252b5132 500 }
4de2d33d 501
252b5132
RH
502 return result;
503}
504
505void
506ldfile_open_command_file (name)
507 const char *name;
508{
509 FILE *ldlex_input_stack;
1c64c4ed 510 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 511
89cdebba 512 if (ldlex_input_stack == (FILE *) NULL)
1c64c4ed
NC
513 {
514 bfd_set_error (bfd_error_system_call);
515 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
516 }
4de2d33d 517
1c64c4ed 518 lex_push_file (ldlex_input_stack, name);
4de2d33d 519
252b5132
RH
520 ldfile_input_filename = name;
521 lineno = 1;
b7a26f91 522
b9a8de1e 523 saved_script_handle = ldlex_input_stack;
252b5132
RH
524}
525
252b5132 526#ifdef GNU960
1c64c4ed
NC
527static char *
528gnu960_map_archname (name)
529 char *name;
252b5132
RH
530{
531 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
532 static struct tabentry arch_tab[] =
533 {
252b5132
RH
534 "", "",
535 "KA", "ka",
536 "KB", "kb",
537 "KC", "mc", /* Synonym for MC */
538 "MC", "mc",
539 "CA", "ca",
540 "SA", "ka", /* Functionally equivalent to KA */
541 "SB", "kb", /* Functionally equivalent to KB */
542 NULL, ""
543 };
544 struct tabentry *tp;
252b5132 545
1c64c4ed
NC
546 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
547 {
548 if (! strcmp (name,tp->cmd_switch))
549 break;
252b5132 550 }
252b5132 551
1c64c4ed 552 if (tp->cmd_switch == NULL)
89cdebba 553 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 554
252b5132
RH
555 return tp->arch;
556}
557
252b5132 558void
1c64c4ed
NC
559ldfile_add_arch (name)
560 char *name;
252b5132
RH
561{
562 search_arch_type *new =
89cdebba 563 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
252b5132 564
1c64c4ed
NC
565 if (*name != '\0')
566 {
567 if (ldfile_output_machine_name[0] != '\0')
568 {
569 einfo (_("%P%F: target architecture respecified\n"));
570 return;
571 }
4de2d33d 572
1c64c4ed 573 ldfile_output_machine_name = name;
252b5132 574 }
252b5132 575
89cdebba 576 new->next = (search_arch_type *) NULL;
1c64c4ed 577 new->name = gnu960_map_archname (name);
252b5132
RH
578 *search_arch_tail_ptr = new;
579 search_arch_tail_ptr = &new->next;
252b5132
RH
580}
581
89cdebba 582#else /* not GNU960 */
252b5132 583
252b5132
RH
584void
585ldfile_add_arch (in_name)
4da711b1 586 const char *in_name;
252b5132 587{
d1b2b2dc 588 char *name = xstrdup (in_name);
252b5132
RH
589 search_arch_type *new =
590 (search_arch_type *) xmalloc (sizeof (search_arch_type));
591
592 ldfile_output_machine_name = in_name;
593
594 new->name = name;
89cdebba 595 new->next = (search_arch_type *) NULL;
252b5132
RH
596 while (*name)
597 {
3882b010 598 *name = TOLOWER (*name);
252b5132
RH
599 name++;
600 }
601 *search_arch_tail_ptr = new;
602 search_arch_tail_ptr = &new->next;
603
604}
605#endif
606
89cdebba
KH
607/* Set the output architecture. */
608
252b5132
RH
609void
610ldfile_set_output_arch (string)
4da711b1 611 const char *string;
252b5132 612{
1c64c4ed
NC
613 const bfd_arch_info_type *arch = bfd_scan_arch (string);
614
615 if (arch)
616 {
617 ldfile_output_architecture = arch->arch;
618 ldfile_output_machine = arch->mach;
619 ldfile_output_machine_name = arch->printable_name;
620 }
621 else
622 {
623 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
624 }
252b5132 625}
This page took 0.199545 seconds and 4 git commands to generate.