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