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