xref: /openbsd-src/gnu/usr.bin/binutils-2.17/ld/ldfile.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Linker file opening and searching.
2*3d8817e4Smiod    Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
3*3d8817e4Smiod    2003, 2004, 2005 Free Software Foundation, Inc.
4*3d8817e4Smiod 
5*3d8817e4Smiod    This file is part of GLD, the Gnu Linker.
6*3d8817e4Smiod 
7*3d8817e4Smiod    GLD is free software; you can redistribute it and/or modify
8*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod    the Free Software Foundation; either version 2, or (at your option)
10*3d8817e4Smiod    any later version.
11*3d8817e4Smiod 
12*3d8817e4Smiod    GLD is distributed in the hope that it will be useful,
13*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*3d8817e4Smiod    GNU General Public License for more details.
16*3d8817e4Smiod 
17*3d8817e4Smiod    You should have received a copy of the GNU General Public License
18*3d8817e4Smiod    along with GLD; see the file COPYING.  If not, write to the Free
19*3d8817e4Smiod    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20*3d8817e4Smiod    02110-1301, USA.  */
21*3d8817e4Smiod 
22*3d8817e4Smiod /* ldfile.c:  look after all the file stuff.  */
23*3d8817e4Smiod 
24*3d8817e4Smiod #include "bfd.h"
25*3d8817e4Smiod #include "sysdep.h"
26*3d8817e4Smiod #include "bfdlink.h"
27*3d8817e4Smiod #include "safe-ctype.h"
28*3d8817e4Smiod #include "ld.h"
29*3d8817e4Smiod #include "ldmisc.h"
30*3d8817e4Smiod #include "ldexp.h"
31*3d8817e4Smiod #include "ldlang.h"
32*3d8817e4Smiod #include "ldfile.h"
33*3d8817e4Smiod #include "ldmain.h"
34*3d8817e4Smiod #include <ldgram.h>
35*3d8817e4Smiod #include "ldlex.h"
36*3d8817e4Smiod #include "ldemul.h"
37*3d8817e4Smiod #include "libiberty.h"
38*3d8817e4Smiod #include "filenames.h"
39*3d8817e4Smiod 
40*3d8817e4Smiod const char * ldfile_input_filename;
41*3d8817e4Smiod bfd_boolean  ldfile_assumed_script = FALSE;
42*3d8817e4Smiod const char * ldfile_output_machine_name = "";
43*3d8817e4Smiod unsigned long ldfile_output_machine;
44*3d8817e4Smiod enum bfd_architecture ldfile_output_architecture;
45*3d8817e4Smiod search_dirs_type * search_head;
46*3d8817e4Smiod 
47*3d8817e4Smiod #ifdef VMS
48*3d8817e4Smiod static char * slash = "";
49*3d8817e4Smiod #else
50*3d8817e4Smiod #if defined (_WIN32) && ! defined (__CYGWIN32__)
51*3d8817e4Smiod static char * slash = "\\";
52*3d8817e4Smiod #else
53*3d8817e4Smiod static char * slash = "/";
54*3d8817e4Smiod #endif
55*3d8817e4Smiod #endif
56*3d8817e4Smiod 
57*3d8817e4Smiod typedef struct search_arch
58*3d8817e4Smiod {
59*3d8817e4Smiod   char *name;
60*3d8817e4Smiod   struct search_arch *next;
61*3d8817e4Smiod } search_arch_type;
62*3d8817e4Smiod 
63*3d8817e4Smiod static search_dirs_type **search_tail_ptr = &search_head;
64*3d8817e4Smiod static search_arch_type *search_arch_head;
65*3d8817e4Smiod static search_arch_type **search_arch_tail_ptr = &search_arch_head;
66*3d8817e4Smiod 
67*3d8817e4Smiod /* Test whether a pathname, after canonicalization, is the same or a
68*3d8817e4Smiod    sub-directory of the sysroot directory.  */
69*3d8817e4Smiod 
70*3d8817e4Smiod static bfd_boolean
is_sysrooted_pathname(const char * name,bfd_boolean notsame)71*3d8817e4Smiod is_sysrooted_pathname (const char *name, bfd_boolean notsame)
72*3d8817e4Smiod {
73*3d8817e4Smiod   char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
74*3d8817e4Smiod   int len;
75*3d8817e4Smiod   bfd_boolean result;
76*3d8817e4Smiod 
77*3d8817e4Smiod   if (! realname)
78*3d8817e4Smiod     return FALSE;
79*3d8817e4Smiod 
80*3d8817e4Smiod   len = strlen (realname);
81*3d8817e4Smiod 
82*3d8817e4Smiod   if (((! notsame && len == ld_canon_sysroot_len)
83*3d8817e4Smiod        || (len >= ld_canon_sysroot_len
84*3d8817e4Smiod 	   && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
85*3d8817e4Smiod 	   && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
86*3d8817e4Smiod       && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
87*3d8817e4Smiod     result = TRUE;
88*3d8817e4Smiod   else
89*3d8817e4Smiod     result = FALSE;
90*3d8817e4Smiod 
91*3d8817e4Smiod   if (realname)
92*3d8817e4Smiod     free (realname);
93*3d8817e4Smiod 
94*3d8817e4Smiod   return result;
95*3d8817e4Smiod }
96*3d8817e4Smiod 
97*3d8817e4Smiod /* Adds NAME to the library search path.
98*3d8817e4Smiod    Makes a copy of NAME using xmalloc().  */
99*3d8817e4Smiod 
100*3d8817e4Smiod void
ldfile_add_library_path(const char * name,bfd_boolean cmdline)101*3d8817e4Smiod ldfile_add_library_path (const char *name, bfd_boolean cmdline)
102*3d8817e4Smiod {
103*3d8817e4Smiod   search_dirs_type *new;
104*3d8817e4Smiod 
105*3d8817e4Smiod   if (!cmdline && config.only_cmd_line_lib_dirs)
106*3d8817e4Smiod     return;
107*3d8817e4Smiod 
108*3d8817e4Smiod   new = xmalloc (sizeof (search_dirs_type));
109*3d8817e4Smiod   new->next = NULL;
110*3d8817e4Smiod   new->cmdline = cmdline;
111*3d8817e4Smiod   *search_tail_ptr = new;
112*3d8817e4Smiod   search_tail_ptr = &new->next;
113*3d8817e4Smiod 
114*3d8817e4Smiod   /* If a directory is marked as honoring sysroot, prepend the sysroot path
115*3d8817e4Smiod      now.  */
116*3d8817e4Smiod   if (name[0] == '=')
117*3d8817e4Smiod     {
118*3d8817e4Smiod       new->name = concat (ld_sysroot, name + 1, NULL);
119*3d8817e4Smiod       new->sysrooted = TRUE;
120*3d8817e4Smiod     }
121*3d8817e4Smiod   else
122*3d8817e4Smiod     {
123*3d8817e4Smiod       new->name = xstrdup (name);
124*3d8817e4Smiod       new->sysrooted = is_sysrooted_pathname (name, FALSE);
125*3d8817e4Smiod     }
126*3d8817e4Smiod }
127*3d8817e4Smiod 
128*3d8817e4Smiod /* Try to open a BFD for a lang_input_statement.  */
129*3d8817e4Smiod 
130*3d8817e4Smiod bfd_boolean
ldfile_try_open_bfd(const char * attempt,lang_input_statement_type * entry)131*3d8817e4Smiod ldfile_try_open_bfd (const char *attempt,
132*3d8817e4Smiod 		     lang_input_statement_type *entry)
133*3d8817e4Smiod {
134*3d8817e4Smiod   entry->the_bfd = bfd_openr (attempt, entry->target);
135*3d8817e4Smiod 
136*3d8817e4Smiod   if (trace_file_tries)
137*3d8817e4Smiod     {
138*3d8817e4Smiod       if (entry->the_bfd == NULL)
139*3d8817e4Smiod 	info_msg (_("attempt to open %s failed\n"), attempt);
140*3d8817e4Smiod       else
141*3d8817e4Smiod 	info_msg (_("attempt to open %s succeeded\n"), attempt);
142*3d8817e4Smiod     }
143*3d8817e4Smiod 
144*3d8817e4Smiod   if (entry->the_bfd == NULL)
145*3d8817e4Smiod     {
146*3d8817e4Smiod       if (bfd_get_error () == bfd_error_invalid_target)
147*3d8817e4Smiod 	einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
148*3d8817e4Smiod       return FALSE;
149*3d8817e4Smiod     }
150*3d8817e4Smiod 
151*3d8817e4Smiod   /* If we are searching for this file, see if the architecture is
152*3d8817e4Smiod      compatible with the output file.  If it isn't, keep searching.
153*3d8817e4Smiod      If we can't open the file as an object file, stop the search
154*3d8817e4Smiod      here.  If we are statically linking, ensure that we don't link
155*3d8817e4Smiod      a dynamic object.  */
156*3d8817e4Smiod 
157*3d8817e4Smiod   if (entry->search_dirs_flag || !entry->dynamic)
158*3d8817e4Smiod     {
159*3d8817e4Smiod       bfd *check;
160*3d8817e4Smiod 
161*3d8817e4Smiod       if (bfd_check_format (entry->the_bfd, bfd_archive))
162*3d8817e4Smiod 	check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
163*3d8817e4Smiod       else
164*3d8817e4Smiod 	check = entry->the_bfd;
165*3d8817e4Smiod 
166*3d8817e4Smiod       if (check != NULL)
167*3d8817e4Smiod 	{
168*3d8817e4Smiod 	  if (! bfd_check_format (check, bfd_object))
169*3d8817e4Smiod 	    {
170*3d8817e4Smiod 	      if (check == entry->the_bfd
171*3d8817e4Smiod 		  && entry->search_dirs_flag
172*3d8817e4Smiod 		  && bfd_get_error () == bfd_error_file_not_recognized
173*3d8817e4Smiod 		  && ! ldemul_unrecognized_file (entry))
174*3d8817e4Smiod 		{
175*3d8817e4Smiod 		  int token, skip = 0;
176*3d8817e4Smiod 		  char *arg, *arg1, *arg2, *arg3;
177*3d8817e4Smiod 		  extern FILE *yyin;
178*3d8817e4Smiod 
179*3d8817e4Smiod 		  /* Try to interpret the file as a linker script.  */
180*3d8817e4Smiod 		  ldfile_open_command_file (attempt);
181*3d8817e4Smiod 
182*3d8817e4Smiod 		  ldfile_assumed_script = TRUE;
183*3d8817e4Smiod 		  parser_input = input_selected;
184*3d8817e4Smiod 		  ldlex_both ();
185*3d8817e4Smiod 		  token = INPUT_SCRIPT;
186*3d8817e4Smiod 		  while (token != 0)
187*3d8817e4Smiod 		    {
188*3d8817e4Smiod 		      switch (token)
189*3d8817e4Smiod 			{
190*3d8817e4Smiod 			case OUTPUT_FORMAT:
191*3d8817e4Smiod 			  if ((token = yylex ()) != '(')
192*3d8817e4Smiod 			    continue;
193*3d8817e4Smiod 			  if ((token = yylex ()) != NAME)
194*3d8817e4Smiod 			    continue;
195*3d8817e4Smiod 			  arg1 = yylval.name;
196*3d8817e4Smiod 			  arg2 = NULL;
197*3d8817e4Smiod 			  arg3 = NULL;
198*3d8817e4Smiod 			  token = yylex ();
199*3d8817e4Smiod 			  if (token == ',')
200*3d8817e4Smiod 			    {
201*3d8817e4Smiod 			      if ((token = yylex ()) != NAME)
202*3d8817e4Smiod 				{
203*3d8817e4Smiod 				  free (arg1);
204*3d8817e4Smiod 				  continue;
205*3d8817e4Smiod 				}
206*3d8817e4Smiod 			      arg2 = yylval.name;
207*3d8817e4Smiod 			      if ((token = yylex ()) != ','
208*3d8817e4Smiod 				  || (token = yylex ()) != NAME)
209*3d8817e4Smiod 				{
210*3d8817e4Smiod 				  free (arg1);
211*3d8817e4Smiod 				  free (arg2);
212*3d8817e4Smiod 				  continue;
213*3d8817e4Smiod 				}
214*3d8817e4Smiod 			      arg3 = yylval.name;
215*3d8817e4Smiod 			      token = yylex ();
216*3d8817e4Smiod 			    }
217*3d8817e4Smiod 			  if (token == ')')
218*3d8817e4Smiod 			    {
219*3d8817e4Smiod 			      switch (command_line.endian)
220*3d8817e4Smiod 				{
221*3d8817e4Smiod 				default:
222*3d8817e4Smiod 				case ENDIAN_UNSET:
223*3d8817e4Smiod 				  arg = arg1; break;
224*3d8817e4Smiod 				case ENDIAN_BIG:
225*3d8817e4Smiod 				  arg = arg2 ? arg2 : arg1; break;
226*3d8817e4Smiod 				case ENDIAN_LITTLE:
227*3d8817e4Smiod 				  arg = arg3 ? arg3 : arg1; break;
228*3d8817e4Smiod 				}
229*3d8817e4Smiod 			      if (strcmp (arg, lang_get_output_target ()) != 0)
230*3d8817e4Smiod 				skip = 1;
231*3d8817e4Smiod 			    }
232*3d8817e4Smiod 			  free (arg1);
233*3d8817e4Smiod 			  if (arg2) free (arg2);
234*3d8817e4Smiod 			  if (arg3) free (arg3);
235*3d8817e4Smiod 			  break;
236*3d8817e4Smiod 			case NAME:
237*3d8817e4Smiod 			case LNAME:
238*3d8817e4Smiod 			case VERS_IDENTIFIER:
239*3d8817e4Smiod 			case VERS_TAG:
240*3d8817e4Smiod 			  free (yylval.name);
241*3d8817e4Smiod 			  break;
242*3d8817e4Smiod 			case INT:
243*3d8817e4Smiod 			  if (yylval.bigint.str)
244*3d8817e4Smiod 			    free (yylval.bigint.str);
245*3d8817e4Smiod 			  break;
246*3d8817e4Smiod 			}
247*3d8817e4Smiod 		      token = yylex ();
248*3d8817e4Smiod 		    }
249*3d8817e4Smiod 		  ldlex_popstate ();
250*3d8817e4Smiod 		  ldfile_assumed_script = FALSE;
251*3d8817e4Smiod 		  fclose (yyin);
252*3d8817e4Smiod 		  yyin = NULL;
253*3d8817e4Smiod 		  if (skip)
254*3d8817e4Smiod 		    {
255*3d8817e4Smiod 		      einfo (_("%P: skipping incompatible %s when searching for %s\n"),
256*3d8817e4Smiod 			     attempt, entry->local_sym_name);
257*3d8817e4Smiod 		      bfd_close (entry->the_bfd);
258*3d8817e4Smiod 		      entry->the_bfd = NULL;
259*3d8817e4Smiod 		      return FALSE;
260*3d8817e4Smiod 		    }
261*3d8817e4Smiod 		}
262*3d8817e4Smiod 	      return TRUE;
263*3d8817e4Smiod 	    }
264*3d8817e4Smiod 
265*3d8817e4Smiod 	  if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
266*3d8817e4Smiod 	    {
267*3d8817e4Smiod 	      einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
268*3d8817e4Smiod 		     attempt);
269*3d8817e4Smiod 	      bfd_close (entry->the_bfd);
270*3d8817e4Smiod 	      entry->the_bfd = NULL;
271*3d8817e4Smiod 	      return FALSE;
272*3d8817e4Smiod 	    }
273*3d8817e4Smiod 
274*3d8817e4Smiod 	  if (entry->search_dirs_flag
275*3d8817e4Smiod 	      && !bfd_arch_get_compatible (check, output_bfd,
276*3d8817e4Smiod 					   command_line.accept_unknown_input_arch)
277*3d8817e4Smiod 	      /* XCOFF archives can have 32 and 64 bit objects.  */
278*3d8817e4Smiod 	      && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
279*3d8817e4Smiod 		    && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
280*3d8817e4Smiod 		    && bfd_check_format (entry->the_bfd, bfd_archive)))
281*3d8817e4Smiod 	    {
282*3d8817e4Smiod 	      einfo (_("%P: skipping incompatible %s when searching for %s\n"),
283*3d8817e4Smiod 		     attempt, entry->local_sym_name);
284*3d8817e4Smiod 	      bfd_close (entry->the_bfd);
285*3d8817e4Smiod 	      entry->the_bfd = NULL;
286*3d8817e4Smiod 	      return FALSE;
287*3d8817e4Smiod 	    }
288*3d8817e4Smiod 	}
289*3d8817e4Smiod     }
290*3d8817e4Smiod 
291*3d8817e4Smiod   return TRUE;
292*3d8817e4Smiod }
293*3d8817e4Smiod 
294*3d8817e4Smiod /* Search for and open the file specified by ENTRY.  If it is an
295*3d8817e4Smiod    archive, use ARCH, LIB and SUFFIX to modify the file name.  */
296*3d8817e4Smiod 
297*3d8817e4Smiod bfd_boolean
ldfile_open_file_search(const char * arch,lang_input_statement_type * entry,const char * lib,const char * suffix)298*3d8817e4Smiod ldfile_open_file_search (const char *arch,
299*3d8817e4Smiod 			 lang_input_statement_type *entry,
300*3d8817e4Smiod 			 const char *lib,
301*3d8817e4Smiod 			 const char *suffix)
302*3d8817e4Smiod {
303*3d8817e4Smiod   search_dirs_type *search;
304*3d8817e4Smiod 
305*3d8817e4Smiod   /* If this is not an archive, try to open it in the current
306*3d8817e4Smiod      directory first.  */
307*3d8817e4Smiod   if (! entry->is_archive)
308*3d8817e4Smiod     {
309*3d8817e4Smiod       if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
310*3d8817e4Smiod 	{
311*3d8817e4Smiod 	  char *name = concat (ld_sysroot, entry->filename,
312*3d8817e4Smiod 			       (const char *) NULL);
313*3d8817e4Smiod 	  if (ldfile_try_open_bfd (name, entry))
314*3d8817e4Smiod 	    {
315*3d8817e4Smiod 	      entry->filename = name;
316*3d8817e4Smiod 	      return TRUE;
317*3d8817e4Smiod 	    }
318*3d8817e4Smiod 	  free (name);
319*3d8817e4Smiod 	}
320*3d8817e4Smiod       else if (ldfile_try_open_bfd (entry->filename, entry))
321*3d8817e4Smiod 	{
322*3d8817e4Smiod 	  entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
323*3d8817e4Smiod 	    && is_sysrooted_pathname (entry->filename, TRUE);
324*3d8817e4Smiod 	  return TRUE;
325*3d8817e4Smiod 	}
326*3d8817e4Smiod 
327*3d8817e4Smiod       if (IS_ABSOLUTE_PATH (entry->filename))
328*3d8817e4Smiod 	return FALSE;
329*3d8817e4Smiod     }
330*3d8817e4Smiod 
331*3d8817e4Smiod   for (search = search_head; search != NULL; search = search->next)
332*3d8817e4Smiod     {
333*3d8817e4Smiod       char *string;
334*3d8817e4Smiod 
335*3d8817e4Smiod       if (entry->dynamic && ! link_info.relocatable)
336*3d8817e4Smiod 	{
337*3d8817e4Smiod 	  if (ldemul_open_dynamic_archive (arch, search, entry))
338*3d8817e4Smiod 	    {
339*3d8817e4Smiod 	      entry->sysrooted = search->sysrooted;
340*3d8817e4Smiod 	      return TRUE;
341*3d8817e4Smiod 	    }
342*3d8817e4Smiod 	}
343*3d8817e4Smiod 
344*3d8817e4Smiod       string = xmalloc (strlen (search->name)
345*3d8817e4Smiod 			+ strlen (slash)
346*3d8817e4Smiod 			+ strlen (lib)
347*3d8817e4Smiod 			+ strlen (entry->filename)
348*3d8817e4Smiod 			+ strlen (arch)
349*3d8817e4Smiod 			+ strlen (suffix)
350*3d8817e4Smiod 			+ 1);
351*3d8817e4Smiod 
352*3d8817e4Smiod       if (entry->is_archive)
353*3d8817e4Smiod 	sprintf (string, "%s%s%s%s%s%s", search->name, slash,
354*3d8817e4Smiod 		 lib, entry->filename, arch, suffix);
355*3d8817e4Smiod       else
356*3d8817e4Smiod 	sprintf (string, "%s%s%s", search->name, slash, entry->filename);
357*3d8817e4Smiod 
358*3d8817e4Smiod       if (ldfile_try_open_bfd (string, entry))
359*3d8817e4Smiod 	{
360*3d8817e4Smiod 	  entry->filename = string;
361*3d8817e4Smiod 	  entry->sysrooted = search->sysrooted;
362*3d8817e4Smiod 	  return TRUE;
363*3d8817e4Smiod 	}
364*3d8817e4Smiod 
365*3d8817e4Smiod       free (string);
366*3d8817e4Smiod     }
367*3d8817e4Smiod 
368*3d8817e4Smiod   return FALSE;
369*3d8817e4Smiod }
370*3d8817e4Smiod 
371*3d8817e4Smiod /* Open the input file specified by ENTRY.  */
372*3d8817e4Smiod 
373*3d8817e4Smiod void
ldfile_open_file(lang_input_statement_type * entry)374*3d8817e4Smiod ldfile_open_file (lang_input_statement_type *entry)
375*3d8817e4Smiod {
376*3d8817e4Smiod   if (entry->the_bfd != NULL)
377*3d8817e4Smiod     return;
378*3d8817e4Smiod 
379*3d8817e4Smiod   if (! entry->search_dirs_flag)
380*3d8817e4Smiod     {
381*3d8817e4Smiod       if (ldfile_try_open_bfd (entry->filename, entry))
382*3d8817e4Smiod 	return;
383*3d8817e4Smiod       if (strcmp (entry->filename, entry->local_sym_name) != 0)
384*3d8817e4Smiod 	einfo (_("%F%P: %s (%s): No such file: %E\n"),
385*3d8817e4Smiod 	       entry->filename, entry->local_sym_name);
386*3d8817e4Smiod       else
387*3d8817e4Smiod 	einfo (_("%F%P: %s: No such file: %E\n"), entry->local_sym_name);
388*3d8817e4Smiod     }
389*3d8817e4Smiod   else
390*3d8817e4Smiod     {
391*3d8817e4Smiod       search_arch_type *arch;
392*3d8817e4Smiod       bfd_boolean found = FALSE;
393*3d8817e4Smiod 
394*3d8817e4Smiod       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
395*3d8817e4Smiod       for (arch = search_arch_head; arch != NULL; arch = arch->next)
396*3d8817e4Smiod 	{
397*3d8817e4Smiod 	  found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
398*3d8817e4Smiod 	  if (found)
399*3d8817e4Smiod 	    break;
400*3d8817e4Smiod #ifdef VMS
401*3d8817e4Smiod 	  found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
402*3d8817e4Smiod 	  if (found)
403*3d8817e4Smiod 	    break;
404*3d8817e4Smiod #endif
405*3d8817e4Smiod 	  found = ldemul_find_potential_libraries (arch->name, entry);
406*3d8817e4Smiod 	  if (found)
407*3d8817e4Smiod 	    break;
408*3d8817e4Smiod 	}
409*3d8817e4Smiod 
410*3d8817e4Smiod       /* If we have found the file, we don't need to search directories
411*3d8817e4Smiod 	 again.  */
412*3d8817e4Smiod       if (found)
413*3d8817e4Smiod 	entry->search_dirs_flag = FALSE;
414*3d8817e4Smiod       else if (entry->sysrooted
415*3d8817e4Smiod 	       && ld_sysroot
416*3d8817e4Smiod 	       && IS_ABSOLUTE_PATH (entry->local_sym_name))
417*3d8817e4Smiod 	einfo (_("%F%P: cannot find %s inside %s\n"),
418*3d8817e4Smiod 	       entry->local_sym_name, ld_sysroot);
419*3d8817e4Smiod       else
420*3d8817e4Smiod 	einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
421*3d8817e4Smiod     }
422*3d8817e4Smiod }
423*3d8817e4Smiod 
424*3d8817e4Smiod /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
425*3d8817e4Smiod 
426*3d8817e4Smiod static FILE *
try_open(const char * name,const char * exten)427*3d8817e4Smiod try_open (const char *name, const char *exten)
428*3d8817e4Smiod {
429*3d8817e4Smiod   FILE *result;
430*3d8817e4Smiod   char buff[1000];
431*3d8817e4Smiod 
432*3d8817e4Smiod   result = fopen (name, "r");
433*3d8817e4Smiod 
434*3d8817e4Smiod   if (trace_file_tries)
435*3d8817e4Smiod     {
436*3d8817e4Smiod       if (result == NULL)
437*3d8817e4Smiod 	info_msg (_("cannot find script file %s\n"), name);
438*3d8817e4Smiod       else
439*3d8817e4Smiod 	info_msg (_("opened script file %s\n"), name);
440*3d8817e4Smiod     }
441*3d8817e4Smiod 
442*3d8817e4Smiod   if (result != NULL)
443*3d8817e4Smiod     return result;
444*3d8817e4Smiod 
445*3d8817e4Smiod   if (*exten)
446*3d8817e4Smiod     {
447*3d8817e4Smiod       sprintf (buff, "%s%s", name, exten);
448*3d8817e4Smiod       result = fopen (buff, "r");
449*3d8817e4Smiod 
450*3d8817e4Smiod       if (trace_file_tries)
451*3d8817e4Smiod 	{
452*3d8817e4Smiod 	  if (result == NULL)
453*3d8817e4Smiod 	    info_msg (_("cannot find script file %s\n"), buff);
454*3d8817e4Smiod 	  else
455*3d8817e4Smiod 	    info_msg (_("opened script file %s\n"), buff);
456*3d8817e4Smiod 	}
457*3d8817e4Smiod     }
458*3d8817e4Smiod 
459*3d8817e4Smiod   return result;
460*3d8817e4Smiod }
461*3d8817e4Smiod 
462*3d8817e4Smiod /* Try to open NAME; if that fails, look for it in any directories
463*3d8817e4Smiod    specified with -L, without and with EXTEND appended.  */
464*3d8817e4Smiod 
465*3d8817e4Smiod static FILE *
ldfile_find_command_file(const char * name,const char * extend)466*3d8817e4Smiod ldfile_find_command_file (const char *name, const char *extend)
467*3d8817e4Smiod {
468*3d8817e4Smiod   search_dirs_type *search;
469*3d8817e4Smiod   FILE *result;
470*3d8817e4Smiod   char buffer[1000];
471*3d8817e4Smiod 
472*3d8817e4Smiod   /* First try raw name.  */
473*3d8817e4Smiod   result = try_open (name, "");
474*3d8817e4Smiod   if (result == NULL)
475*3d8817e4Smiod     {
476*3d8817e4Smiod       /* Try now prefixes.  */
477*3d8817e4Smiod       for (search = search_head; search != NULL; search = search->next)
478*3d8817e4Smiod 	{
479*3d8817e4Smiod 	  sprintf (buffer, "%s%s%s", search->name, slash, name);
480*3d8817e4Smiod 
481*3d8817e4Smiod 	  result = try_open (buffer, extend);
482*3d8817e4Smiod 	  if (result)
483*3d8817e4Smiod 	    break;
484*3d8817e4Smiod 	}
485*3d8817e4Smiod     }
486*3d8817e4Smiod 
487*3d8817e4Smiod   return result;
488*3d8817e4Smiod }
489*3d8817e4Smiod 
490*3d8817e4Smiod void
ldfile_open_command_file(const char * name)491*3d8817e4Smiod ldfile_open_command_file (const char *name)
492*3d8817e4Smiod {
493*3d8817e4Smiod   FILE *ldlex_input_stack;
494*3d8817e4Smiod   ldlex_input_stack = ldfile_find_command_file (name, "");
495*3d8817e4Smiod 
496*3d8817e4Smiod   if (ldlex_input_stack == NULL)
497*3d8817e4Smiod     {
498*3d8817e4Smiod       bfd_set_error (bfd_error_system_call);
499*3d8817e4Smiod       einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
500*3d8817e4Smiod     }
501*3d8817e4Smiod 
502*3d8817e4Smiod   lex_push_file (ldlex_input_stack, name);
503*3d8817e4Smiod 
504*3d8817e4Smiod   ldfile_input_filename = name;
505*3d8817e4Smiod   lineno = 1;
506*3d8817e4Smiod 
507*3d8817e4Smiod   saved_script_handle = ldlex_input_stack;
508*3d8817e4Smiod }
509*3d8817e4Smiod 
510*3d8817e4Smiod void
ldfile_add_arch(const char * in_name)511*3d8817e4Smiod ldfile_add_arch (const char *in_name)
512*3d8817e4Smiod {
513*3d8817e4Smiod   char *name = xstrdup (in_name);
514*3d8817e4Smiod   search_arch_type *new = xmalloc (sizeof (search_arch_type));
515*3d8817e4Smiod 
516*3d8817e4Smiod   ldfile_output_machine_name = in_name;
517*3d8817e4Smiod 
518*3d8817e4Smiod   new->name = name;
519*3d8817e4Smiod   new->next = NULL;
520*3d8817e4Smiod   while (*name)
521*3d8817e4Smiod     {
522*3d8817e4Smiod       *name = TOLOWER (*name);
523*3d8817e4Smiod       name++;
524*3d8817e4Smiod     }
525*3d8817e4Smiod   *search_arch_tail_ptr = new;
526*3d8817e4Smiod   search_arch_tail_ptr = &new->next;
527*3d8817e4Smiod 
528*3d8817e4Smiod }
529*3d8817e4Smiod 
530*3d8817e4Smiod /* Set the output architecture.  */
531*3d8817e4Smiod 
532*3d8817e4Smiod void
ldfile_set_output_arch(const char * string,enum bfd_architecture defarch)533*3d8817e4Smiod ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
534*3d8817e4Smiod {
535*3d8817e4Smiod   const bfd_arch_info_type *arch = bfd_scan_arch (string);
536*3d8817e4Smiod 
537*3d8817e4Smiod   if (arch)
538*3d8817e4Smiod     {
539*3d8817e4Smiod       ldfile_output_architecture = arch->arch;
540*3d8817e4Smiod       ldfile_output_machine = arch->mach;
541*3d8817e4Smiod       ldfile_output_machine_name = arch->printable_name;
542*3d8817e4Smiod     }
543*3d8817e4Smiod   else if (defarch != bfd_arch_unknown)
544*3d8817e4Smiod     ldfile_output_architecture = defarch;
545*3d8817e4Smiod   else
546*3d8817e4Smiod     einfo (_("%P%F: cannot represent machine `%s'\n"), string);
547*3d8817e4Smiod }
548