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