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