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