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