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