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