Updated sources to avoid using the identifier name "new", which is a
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805 1/* Linker file opening and searching.
5e2f1575 2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
c38b10fa 3 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
252b5132 4
f96b4a7b 5 This file is part of the GNU Binutils.
252b5132 6
f96b4a7b 7 This program is free software; you can redistribute it and/or modify
3fe38064 8 it under the terms of the GNU General Public License as published by
f96b4a7b
NC
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
252b5132 11
f96b4a7b 12 This program is distributed in the hope that it will be useful,
3fe38064
NC
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 17 You should have received a copy of the GNU General Public License
f96b4a7b
NC
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
252b5132 24#include "bfdlink.h"
3882b010 25#include "safe-ctype.h"
252b5132
RH
26#include "ld.h"
27#include "ldmisc.h"
28#include "ldexp.h"
29#include "ldlang.h"
30#include "ldfile.h"
31#include "ldmain.h"
df2a7313 32#include <ldgram.h>
252b5132
RH
33#include "ldlex.h"
34#include "ldemul.h"
d1b2b2dc 35#include "libiberty.h"
3fe38064 36#include "filenames.h"
252b5132 37
3fe38064
NC
38const char * ldfile_input_filename;
39bfd_boolean ldfile_assumed_script = FALSE;
40const char * ldfile_output_machine_name = "";
252b5132
RH
41unsigned long ldfile_output_machine;
42enum bfd_architecture ldfile_output_architecture;
3fe38064 43search_dirs_type * search_head;
252b5132 44
252b5132 45#ifdef VMS
279e75dc 46static char * slash = "";
252b5132
RH
47#else
48#if defined (_WIN32) && ! defined (__CYGWIN32__)
279e75dc 49static char * slash = "\\";
252b5132 50#else
279e75dc 51static char * slash = "/";
252b5132
RH
52#endif
53#endif
252b5132 54
3fe38064
NC
55typedef struct search_arch
56{
4de2d33d 57 char *name;
252b5132
RH
58 struct search_arch *next;
59} search_arch_type;
60
3fe38064 61static search_dirs_type **search_tail_ptr = &search_head;
252b5132
RH
62static search_arch_type *search_arch_head;
63static search_arch_type **search_arch_tail_ptr = &search_arch_head;
4de2d33d 64
3fe38064
NC
65/* Test whether a pathname, after canonicalization, is the same or a
66 sub-directory of the sysroot directory. */
67
68static bfd_boolean
1579bae1 69is_sysrooted_pathname (const char *name, bfd_boolean notsame)
3fe38064
NC
70{
71 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
72 int len;
73 bfd_boolean result;
74
75 if (! realname)
76 return FALSE;
5e2f1575 77
3fe38064
NC
78 len = strlen (realname);
79
80 if (((! notsame && len == ld_canon_sysroot_len)
81 || (len >= ld_canon_sysroot_len
82 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
83 && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
84 && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
85 result = TRUE;
86 else
87 result = FALSE;
88
89 if (realname)
90 free (realname);
91
92 return result;
93}
252b5132 94
5ed6aba4
NC
95/* Adds NAME to the library search path.
96 Makes a copy of NAME using xmalloc(). */
97
252b5132 98void
1579bae1 99ldfile_add_library_path (const char *name, bfd_boolean cmdline)
252b5132 100{
d3ce72d0 101 search_dirs_type *new_dirs;
252b5132 102
361b220e
CD
103 if (!cmdline && config.only_cmd_line_lib_dirs)
104 return;
105
d3ce72d0
NC
106 new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
107 new_dirs->next = NULL;
108 new_dirs->cmdline = cmdline;
109 *search_tail_ptr = new_dirs;
110 search_tail_ptr = &new_dirs->next;
9c8ebd6a
DJ
111
112 /* If a directory is marked as honoring sysroot, prepend the sysroot path
113 now. */
5ed6aba4 114 if (name[0] == '=')
e3f2db7f 115 {
d3ce72d0
NC
116 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
117 new_dirs->sysrooted = TRUE;
e3f2db7f
AO
118 }
119 else
5ed6aba4 120 {
d3ce72d0
NC
121 new_dirs->name = xstrdup (name);
122 new_dirs->sysrooted = is_sysrooted_pathname (name, FALSE);
5ed6aba4 123 }
252b5132
RH
124}
125
126/* Try to open a BFD for a lang_input_statement. */
127
b34976b6 128bfd_boolean
1579bae1
AM
129ldfile_try_open_bfd (const char *attempt,
130 lang_input_statement_type *entry)
252b5132
RH
131{
132 entry->the_bfd = bfd_openr (attempt, entry->target);
133
134 if (trace_file_tries)
135 {
136 if (entry->the_bfd == NULL)
137 info_msg (_("attempt to open %s failed\n"), attempt);
138 else
139 info_msg (_("attempt to open %s succeeded\n"), attempt);
140 }
141
b90d1146 142 if (entry->the_bfd == NULL)
252b5132
RH
143 {
144 if (bfd_get_error () == bfd_error_invalid_target)
145 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
b34976b6 146 return FALSE;
252b5132 147 }
b90d1146
ILT
148
149 /* If we are searching for this file, see if the architecture is
150 compatible with the output file. If it isn't, keep searching.
151 If we can't open the file as an object file, stop the search
6c0c5b1e
AM
152 here. If we are statically linking, ensure that we don't link
153 a dynamic object. */
b90d1146 154
6c0c5b1e 155 if (entry->search_dirs_flag || !entry->dynamic)
b90d1146
ILT
156 {
157 bfd *check;
158
159 if (bfd_check_format (entry->the_bfd, bfd_archive))
160 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
161 else
162 check = entry->the_bfd;
163
a9998805 164 if (check != NULL)
b90d1146 165 {
a9998805 166 if (! bfd_check_format (check, bfd_object))
599917b8
JJ
167 {
168 if (check == entry->the_bfd
6c0c5b1e 169 && entry->search_dirs_flag
599917b8
JJ
170 && bfd_get_error () == bfd_error_file_not_recognized
171 && ! ldemul_unrecognized_file (entry))
172 {
173 int token, skip = 0;
174 char *arg, *arg1, *arg2, *arg3;
175 extern FILE *yyin;
176
177 /* Try to interpret the file as a linker script. */
178 ldfile_open_command_file (attempt);
b34976b6
AM
179
180 ldfile_assumed_script = TRUE;
599917b8
JJ
181 parser_input = input_selected;
182 ldlex_both ();
183 token = INPUT_SCRIPT;
184 while (token != 0)
185 {
186 switch (token)
187 {
188 case OUTPUT_FORMAT:
189 if ((token = yylex ()) != '(')
190 continue;
191 if ((token = yylex ()) != NAME)
192 continue;
193 arg1 = yylval.name;
194 arg2 = NULL;
195 arg3 = NULL;
196 token = yylex ();
197 if (token == ',')
198 {
199 if ((token = yylex ()) != NAME)
200 {
201 free (arg1);
202 continue;
203 }
204 arg2 = yylval.name;
205 if ((token = yylex ()) != ','
206 || (token = yylex ()) != NAME)
207 {
208 free (arg1);
209 free (arg2);
210 continue;
211 }
212 arg3 = yylval.name;
213 token = yylex ();
214 }
215 if (token == ')')
216 {
217 switch (command_line.endian)
218 {
219 default:
220 case ENDIAN_UNSET:
221 arg = arg1; break;
222 case ENDIAN_BIG:
223 arg = arg2 ? arg2 : arg1; break;
224 case ENDIAN_LITTLE:
225 arg = arg3 ? arg3 : arg1; break;
226 }
227 if (strcmp (arg, lang_get_output_target ()) != 0)
228 skip = 1;
229 }
230 free (arg1);
231 if (arg2) free (arg2);
232 if (arg3) free (arg3);
233 break;
234 case NAME:
235 case LNAME:
236 case VERS_IDENTIFIER:
237 case VERS_TAG:
238 free (yylval.name);
239 break;
240 case INT:
241 if (yylval.bigint.str)
242 free (yylval.bigint.str);
243 break;
5e2f1575 244 }
599917b8
JJ
245 token = yylex ();
246 }
4f39e302 247 ldlex_popstate ();
b34976b6 248 ldfile_assumed_script = FALSE;
599917b8
JJ
249 fclose (yyin);
250 yyin = NULL;
251 if (skip)
252 {
fe7929ce
AM
253 if (command_line.warn_search_mismatch)
254 einfo (_("%P: skipping incompatible %s "
255 "when searching for %s\n"),
256 attempt, entry->local_sym_name);
599917b8
JJ
257 bfd_close (entry->the_bfd);
258 entry->the_bfd = NULL;
b34976b6 259 return FALSE;
599917b8
JJ
260 }
261 }
b34976b6 262 return TRUE;
599917b8 263 }
f1f0d9ab 264
6c0c5b1e
AM
265 if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
266 {
267 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
268 attempt);
269 bfd_close (entry->the_bfd);
270 entry->the_bfd = NULL;
271 return FALSE;
272 }
273
274 if (entry->search_dirs_flag
f13a99db 275 && !bfd_arch_get_compatible (check, link_info.output_bfd,
6c0c5b1e 276 command_line.accept_unknown_input_arch)
312b768e 277 /* XCOFF archives can have 32 and 64 bit objects. */
f1f0d9ab 278 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
f13a99db 279 && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour
f1f0d9ab 280 && bfd_check_format (entry->the_bfd, bfd_archive)))
a9998805 281 {
fe7929ce
AM
282 if (command_line.warn_search_mismatch)
283 einfo (_("%P: skipping incompatible %s "
284 "when searching for %s\n"),
285 attempt, entry->local_sym_name);
a9998805
ILT
286 bfd_close (entry->the_bfd);
287 entry->the_bfd = NULL;
b34976b6 288 return FALSE;
a9998805 289 }
b90d1146
ILT
290 }
291 }
292
b34976b6 293 return TRUE;
252b5132
RH
294}
295
296/* Search for and open the file specified by ENTRY. If it is an
297 archive, use ARCH, LIB and SUFFIX to modify the file name. */
298
b34976b6 299bfd_boolean
1579bae1
AM
300ldfile_open_file_search (const char *arch,
301 lang_input_statement_type *entry,
302 const char *lib,
303 const char *suffix)
252b5132
RH
304{
305 search_dirs_type *search;
306
307 /* If this is not an archive, try to open it in the current
308 directory first. */
309 if (! entry->is_archive)
310 {
3fe38064 311 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
e3f2db7f
AO
312 {
313 char *name = concat (ld_sysroot, entry->filename,
314 (const char *) NULL);
315 if (ldfile_try_open_bfd (name, entry))
316 {
317 entry->filename = name;
318 return TRUE;
319 }
320 free (name);
321 }
322 else if (ldfile_try_open_bfd (entry->filename, entry))
323 {
3fe38064
NC
324 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
325 && is_sysrooted_pathname (entry->filename, TRUE);
e3f2db7f
AO
326 return TRUE;
327 }
3fe38064
NC
328
329 if (IS_ABSOLUTE_PATH (entry->filename))
330 return FALSE;
252b5132
RH
331 }
332
1579bae1 333 for (search = search_head; search != NULL; search = search->next)
252b5132
RH
334 {
335 char *string;
336
1049f94e 337 if (entry->dynamic && ! link_info.relocatable)
252b5132
RH
338 {
339 if (ldemul_open_dynamic_archive (arch, search, entry))
e3f2db7f
AO
340 {
341 entry->sysrooted = search->sysrooted;
342 return TRUE;
343 }
252b5132
RH
344 }
345
252b5132 346 if (entry->is_archive)
a26cc967
AM
347 string = concat (search->name, slash, lib, entry->filename,
348 arch, suffix, (const char *) NULL);
252b5132 349 else
a26cc967
AM
350 string = concat (search->name, slash, entry->filename,
351 (const char *) 0);
252b5132
RH
352
353 if (ldfile_try_open_bfd (string, entry))
354 {
b90d1146 355 entry->filename = string;
e3f2db7f 356 entry->sysrooted = search->sysrooted;
b34976b6 357 return TRUE;
252b5132
RH
358 }
359
360 free (string);
361 }
362
b34976b6 363 return FALSE;
252b5132
RH
364}
365
366/* Open the input file specified by ENTRY. */
367
368void
1579bae1 369ldfile_open_file (lang_input_statement_type *entry)
252b5132
RH
370{
371 if (entry->the_bfd != NULL)
372 return;
373
374 if (! entry->search_dirs_flag)
375 {
376 if (ldfile_try_open_bfd (entry->filename, entry))
377 return;
378 if (strcmp (entry->filename, entry->local_sym_name) != 0)
f24ddbdd 379 einfo (_("%F%P: %s (%s): No such file: %E\n"),
252b5132
RH
380 entry->filename, entry->local_sym_name);
381 else
f24ddbdd 382 einfo (_("%F%P: %s: No such file: %E\n"), entry->local_sym_name);
252b5132
RH
383 }
384 else
385 {
386 search_arch_type *arch;
b34976b6 387 bfd_boolean found = FALSE;
252b5132
RH
388
389 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
1579bae1 390 for (arch = search_arch_head; arch != NULL; arch = arch->next)
252b5132 391 {
78f85fd7
L
392 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
393 if (found)
394 break;
252b5132 395#ifdef VMS
78f85fd7
L
396 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
397 if (found)
398 break;
252b5132 399#endif
78f85fd7
L
400 found = ldemul_find_potential_libraries (arch->name, entry);
401 if (found)
402 break;
252b5132 403 }
4de2d33d 404
78f85fd7
L
405 /* If we have found the file, we don't need to search directories
406 again. */
407 if (found)
b34976b6 408 entry->search_dirs_flag = FALSE;
3fe38064
NC
409 else if (entry->sysrooted
410 && ld_sysroot
411 && IS_ABSOLUTE_PATH (entry->local_sym_name))
412 einfo (_("%F%P: cannot find %s inside %s\n"),
413 entry->local_sym_name, ld_sysroot);
78f85fd7
L
414 else
415 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
416 }
417}
418
419/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
420
421static FILE *
1579bae1 422try_open (const char *name, const char *exten)
252b5132
RH
423{
424 FILE *result;
252b5132
RH
425
426 result = fopen (name, "r");
4de2d33d 427
252b5132
RH
428 if (trace_file_tries)
429 {
430 if (result == NULL)
431 info_msg (_("cannot find script file %s\n"), name);
432 else
433 info_msg (_("opened script file %s\n"), name);
434 }
435
436 if (result != NULL)
437 return result;
438
439 if (*exten)
440 {
a26cc967
AM
441 char *buff;
442
443 buff = concat (name, exten, (const char *) NULL);
252b5132 444 result = fopen (buff, "r");
4de2d33d 445
252b5132
RH
446 if (trace_file_tries)
447 {
448 if (result == NULL)
449 info_msg (_("cannot find script file %s\n"), buff);
450 else
451 info_msg (_("opened script file %s\n"), buff);
452 }
a26cc967 453 free (buff);
252b5132
RH
454 }
455
456 return result;
457}
458
a8caa245
AM
459/* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
460
461static bfd_boolean
462check_for_scripts_dir (char *dir)
463{
464 char *buf;
465 struct stat s;
466 bfd_boolean res;
467
468 buf = concat (dir, "/ldscripts", (const char *) NULL);
469 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
470 free (buf);
471 return res;
472}
473
474/* Return the default directory for finding script files.
475 We look for the "ldscripts" directory in:
476
477 SCRIPTDIR (passed from Makefile)
478 (adjusted according to the current location of the binary)
479 SCRIPTDIR (passed from Makefile)
c38b10fa 480 the dir where this program is (for using it from the build tree). */
a8caa245
AM
481
482static char *
483find_scripts_dir (void)
484{
c38b10fa 485 char *dir;
a8caa245
AM
486
487 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
488 if (dir)
489 {
490 if (check_for_scripts_dir (dir))
491 return dir;
492 free (dir);
493 }
494
495 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
496 if (dir)
497 {
498 if (check_for_scripts_dir (dir))
499 return dir;
500 free (dir);
501 }
502
503 if (check_for_scripts_dir (SCRIPTDIR))
504 /* We've been installed normally. */
505 return SCRIPTDIR;
506
507 /* Look for "ldscripts" in the dir where our binary is. */
c38b10fa
AM
508 dir = make_relative_prefix (program_name, ".", ".");
509 if (dir)
510 {
511 if (check_for_scripts_dir (dir))
512 return dir;
513 free (dir);
514 }
a8caa245 515
a8caa245
AM
516 return NULL;
517}
518
8c3e16f4
L
519/* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
520 it in directories specified with -L, then in the default script
521 directory, without and with EXTEND appended. If DEFAULT_ONLY is
522 true, the search is restricted to the default script location. */
252b5132 523
279e75dc 524static FILE *
7d24f02c
KH
525ldfile_find_command_file (const char *name, const char *extend,
526 bfd_boolean default_only)
252b5132
RH
527{
528 search_dirs_type *search;
32252ac1 529 FILE *result = NULL;
a8caa245
AM
530 char *buffer;
531 static search_dirs_type *script_search;
252b5132 532
8c3e16f4
L
533 if (!default_only)
534 {
535 /* First try raw name. */
536 result = try_open (name, "");
537 if (result != NULL)
538 return result;
539 }
a8caa245
AM
540
541 if (!script_search)
1c64c4ed 542 {
a8caa245
AM
543 char *script_dir = find_scripts_dir ();
544 if (script_dir)
1c64c4ed 545 {
a8caa245
AM
546 search_dirs_type **save_tail_ptr = search_tail_ptr;
547 search_tail_ptr = &script_search;
548 ldfile_add_library_path (script_dir, TRUE);
549 search_tail_ptr = save_tail_ptr;
1c64c4ed 550 }
a8caa245
AM
551 }
552
7d24f02c
KH
553 /* Temporarily append script_search to the path list so that the
554 paths specified with -L will be searched first. */
555 *search_tail_ptr = script_search;
556
a8caa245 557 /* Try now prefixes. */
7d24f02c
KH
558 for (search = default_only ? script_search : search_head;
559 search != NULL;
560 search = search->next)
a8caa245 561 {
a8caa245
AM
562 buffer = concat (search->name, slash, name, (const char *) NULL);
563 result = try_open (buffer, extend);
564 free (buffer);
565 if (result)
566 break;
252b5132 567 }
4de2d33d 568
7d24f02c
KH
569 /* Restore the original path list. */
570 *search_tail_ptr = NULL;
571
252b5132
RH
572 return result;
573}
574
7d24f02c
KH
575/* Open command file NAME. */
576
577static void
578ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
252b5132
RH
579{
580 FILE *ldlex_input_stack;
7d24f02c 581 ldlex_input_stack = ldfile_find_command_file (name, "", default_only);
252b5132 582
1579bae1 583 if (ldlex_input_stack == NULL)
1c64c4ed
NC
584 {
585 bfd_set_error (bfd_error_system_call);
586 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
587 }
4de2d33d 588
1c64c4ed 589 lex_push_file (ldlex_input_stack, name);
4de2d33d 590
252b5132
RH
591 ldfile_input_filename = name;
592 lineno = 1;
b7a26f91 593
b9a8de1e 594 saved_script_handle = ldlex_input_stack;
252b5132
RH
595}
596
7d24f02c
KH
597/* Open command file NAME in the current directory, -L directories,
598 the default script location, in that order. */
599
600void
601ldfile_open_command_file (const char *name)
602{
603 ldfile_open_command_file_1 (name, FALSE);
604}
605
606/* Open command file NAME at the default script location. */
607
608void
609ldfile_open_default_command_file (const char *name)
610{
611 ldfile_open_command_file_1 (name, TRUE);
612}
613
252b5132 614void
1579bae1 615ldfile_add_arch (const char *in_name)
252b5132 616{
d1b2b2dc 617 char *name = xstrdup (in_name);
d3ce72d0
NC
618 search_arch_type *new_arch = (search_arch_type *)
619 xmalloc (sizeof (search_arch_type));
252b5132
RH
620
621 ldfile_output_machine_name = in_name;
622
d3ce72d0
NC
623 new_arch->name = name;
624 new_arch->next = NULL;
252b5132
RH
625 while (*name)
626 {
3882b010 627 *name = TOLOWER (*name);
252b5132
RH
628 name++;
629 }
d3ce72d0
NC
630 *search_arch_tail_ptr = new_arch;
631 search_arch_tail_ptr = &new_arch->next;
252b5132
RH
632
633}
252b5132 634
89cdebba
KH
635/* Set the output architecture. */
636
252b5132 637void
5e2f1575 638ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
252b5132 639{
1c64c4ed
NC
640 const bfd_arch_info_type *arch = bfd_scan_arch (string);
641
642 if (arch)
643 {
644 ldfile_output_architecture = arch->arch;
645 ldfile_output_machine = arch->mach;
646 ldfile_output_machine_name = arch->printable_name;
647 }
5e2f1575
AM
648 else if (defarch != bfd_arch_unknown)
649 ldfile_output_architecture = defarch;
1c64c4ed 650 else
5e2f1575 651 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
252b5132 652}
This page took 0.446055 seconds and 4 git commands to generate.