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