Back to index

cell-binutils  2.17cvs20070401
ldfile.c
Go to the documentation of this file.
00001 /* Linker file opening and searching.
00002    Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
00003    2003, 2004, 2005 Free Software Foundation, Inc.
00004 
00005    This file is part of GLD, the Gnu Linker.
00006 
00007    GLD is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2, or (at your option)
00010    any later version.
00011 
00012    GLD is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with GLD; see the file COPYING.  If not, write to the Free
00019    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00020    02110-1301, USA.  */
00021 
00022 /* ldfile.c:  look after all the file stuff.  */
00023 
00024 #include "bfd.h"
00025 #include "sysdep.h"
00026 #include "bfdlink.h"
00027 #include "safe-ctype.h"
00028 #include "ld.h"
00029 #include "ldmisc.h"
00030 #include "ldexp.h"
00031 #include "ldlang.h"
00032 #include "ldfile.h"
00033 #include "ldmain.h"
00034 #include <ldgram.h>
00035 #include "ldlex.h"
00036 #include "ldemul.h"
00037 #include "libiberty.h"
00038 #include "filenames.h"
00039 
00040 const char * ldfile_input_filename;
00041 bfd_boolean  ldfile_assumed_script = FALSE;
00042 const char * ldfile_output_machine_name = "";
00043 unsigned long ldfile_output_machine;
00044 enum bfd_architecture ldfile_output_architecture;
00045 search_dirs_type * search_head;
00046 
00047 #ifdef VMS
00048 static char * slash = "";
00049 #else
00050 #if defined (_WIN32) && ! defined (__CYGWIN32__)
00051 static char * slash = "\\";
00052 #else
00053 static char * slash = "/";
00054 #endif
00055 #endif
00056 
00057 typedef struct search_arch
00058 {
00059   char *name;
00060   struct search_arch *next;
00061 } search_arch_type;
00062 
00063 static search_dirs_type **search_tail_ptr = &search_head;
00064 static search_arch_type *search_arch_head;
00065 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
00066 
00067 /* Test whether a pathname, after canonicalization, is the same or a
00068    sub-directory of the sysroot directory.  */
00069 
00070 static bfd_boolean
00071 is_sysrooted_pathname (const char *name, bfd_boolean notsame)
00072 {
00073   char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
00074   int len;
00075   bfd_boolean result;
00076 
00077   if (! realname)
00078     return FALSE;
00079 
00080   len = strlen (realname);
00081 
00082   if (((! notsame && len == ld_canon_sysroot_len)
00083        || (len >= ld_canon_sysroot_len
00084           && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
00085           && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
00086       && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
00087     result = TRUE;
00088   else
00089     result = FALSE;
00090 
00091   if (realname)
00092     free (realname);
00093 
00094   return result;
00095 }
00096 
00097 /* Adds NAME to the library search path.
00098    Makes a copy of NAME using xmalloc().  */
00099 
00100 void
00101 ldfile_add_library_path (const char *name, bfd_boolean cmdline)
00102 {
00103   search_dirs_type *new;
00104 
00105   if (!cmdline && config.only_cmd_line_lib_dirs)
00106     return;
00107 
00108   new = xmalloc (sizeof (search_dirs_type));
00109   new->next = NULL;
00110   new->cmdline = cmdline;
00111   *search_tail_ptr = new;
00112   search_tail_ptr = &new->next;
00113 
00114   /* If a directory is marked as honoring sysroot, prepend the sysroot path
00115      now.  */
00116   if (name[0] == '=')
00117     {
00118       new->name = concat (ld_sysroot, name + 1, NULL);
00119       new->sysrooted = TRUE;
00120     }
00121   else
00122     {
00123       new->name = xstrdup (name);
00124       new->sysrooted = is_sysrooted_pathname (name, FALSE);
00125     }
00126 }
00127 
00128 /* Try to open a BFD for a lang_input_statement.  */
00129 
00130 bfd_boolean
00131 ldfile_try_open_bfd (const char *attempt,
00132                    lang_input_statement_type *entry)
00133 {
00134   entry->the_bfd = bfd_openr (attempt, entry->target);
00135 
00136   if (trace_file_tries)
00137     {
00138       if (entry->the_bfd == NULL)
00139        info_msg (_("attempt to open %s failed\n"), attempt);
00140       else
00141        info_msg (_("attempt to open %s succeeded\n"), attempt);
00142     }
00143 
00144   if (entry->the_bfd == NULL)
00145     {
00146       if (bfd_get_error () == bfd_error_invalid_target)
00147        einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
00148       return FALSE;
00149     }
00150 
00151   /* If we are searching for this file, see if the architecture is
00152      compatible with the output file.  If it isn't, keep searching.
00153      If we can't open the file as an object file, stop the search
00154      here.  If we are statically linking, ensure that we don't link
00155      a dynamic object.  */
00156 
00157   if (entry->search_dirs_flag || !entry->dynamic)
00158     {
00159       bfd *check;
00160 
00161       if (bfd_check_format (entry->the_bfd, bfd_archive))
00162        check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
00163       else
00164        check = entry->the_bfd;
00165 
00166       if (check != NULL)
00167        {
00168          if (! bfd_check_format (check, bfd_object))
00169            {
00170              if (check == entry->the_bfd
00171                 && entry->search_dirs_flag
00172                 && bfd_get_error () == bfd_error_file_not_recognized
00173                 && ! ldemul_unrecognized_file (entry))
00174               {
00175                 int token, skip = 0;
00176                 char *arg, *arg1, *arg2, *arg3;
00177                 extern FILE *yyin;
00178 
00179                 /* Try to interpret the file as a linker script.  */
00180                 ldfile_open_command_file (attempt);
00181 
00182                 ldfile_assumed_script = TRUE;
00183                 parser_input = input_selected;
00184                 ldlex_both ();
00185                 token = INPUT_SCRIPT;
00186                 while (token != 0)
00187                   {
00188                     switch (token)
00189                      {
00190                      case OUTPUT_FORMAT:
00191                        if ((token = yylex ()) != '(')
00192                          continue;
00193                        if ((token = yylex ()) != NAME)
00194                          continue;
00195                        arg1 = yylval.name;
00196                        arg2 = NULL;
00197                        arg3 = NULL;
00198                        token = yylex ();
00199                        if (token == ',')
00200                          {
00201                            if ((token = yylex ()) != NAME)
00202                             {
00203                               free (arg1);
00204                               continue;
00205                             }
00206                            arg2 = yylval.name;
00207                            if ((token = yylex ()) != ','
00208                               || (token = yylex ()) != NAME)
00209                             {
00210                               free (arg1);
00211                               free (arg2);
00212                               continue;
00213                             }
00214                            arg3 = yylval.name;
00215                            token = yylex ();
00216                          }
00217                        if (token == ')')
00218                          {
00219                            switch (command_line.endian)
00220                             {
00221                             default:
00222                             case ENDIAN_UNSET:
00223                               arg = arg1; break;
00224                             case ENDIAN_BIG:
00225                               arg = arg2 ? arg2 : arg1; break;
00226                             case ENDIAN_LITTLE:
00227                               arg = arg3 ? arg3 : arg1; break;
00228                             }
00229                            if (strcmp (arg, lang_get_output_target ()) != 0)
00230                             skip = 1;
00231                          }
00232                        free (arg1);
00233                        if (arg2) free (arg2);
00234                        if (arg3) free (arg3);
00235                        break;
00236                      case NAME:
00237                      case LNAME:
00238                      case VERS_IDENTIFIER:
00239                      case VERS_TAG:
00240                        free (yylval.name);
00241                        break;
00242                      case INT:
00243                        if (yylval.bigint.str)
00244                          free (yylval.bigint.str);
00245                        break;
00246                      }
00247                     token = yylex ();
00248                   }
00249                 ldlex_popstate ();
00250                 ldfile_assumed_script = FALSE;
00251                 fclose (yyin);
00252                 yyin = NULL;
00253                 if (skip)
00254                   {
00255                     einfo (_("%P: skipping incompatible %s when searching for %s\n"),
00256                           attempt, entry->local_sym_name);
00257                     bfd_close (entry->the_bfd);
00258                     entry->the_bfd = NULL;
00259                     return FALSE;
00260                   }
00261               }
00262              return TRUE;
00263            }
00264 
00265          if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
00266            {
00267              einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
00268                    attempt);
00269              bfd_close (entry->the_bfd);
00270              entry->the_bfd = NULL;
00271              return FALSE;
00272            }
00273 
00274          if (entry->search_dirs_flag
00275              && !bfd_arch_get_compatible (check, output_bfd,
00276                                       command_line.accept_unknown_input_arch)
00277              /* XCOFF archives can have 32 and 64 bit objects.  */
00278              && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
00279                   && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
00280                   && bfd_check_format (entry->the_bfd, bfd_archive)))
00281            {
00282              einfo (_("%P: skipping incompatible %s when searching for %s\n"),
00283                    attempt, entry->local_sym_name);
00284              bfd_close (entry->the_bfd);
00285              entry->the_bfd = NULL;
00286              return FALSE;
00287            }
00288        }
00289     }
00290 
00291   return TRUE;
00292 }
00293 
00294 /* Search for and open the file specified by ENTRY.  If it is an
00295    archive, use ARCH, LIB and SUFFIX to modify the file name.  */
00296 
00297 bfd_boolean
00298 ldfile_open_file_search (const char *arch,
00299                       lang_input_statement_type *entry,
00300                       const char *lib,
00301                       const char *suffix)
00302 {
00303   search_dirs_type *search;
00304 
00305   /* If this is not an archive, try to open it in the current
00306      directory first.  */
00307   if (! entry->is_archive)
00308     {
00309       if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
00310        {
00311          char *name = concat (ld_sysroot, entry->filename,
00312                             (const char *) NULL);
00313          if (ldfile_try_open_bfd (name, entry))
00314            {
00315              entry->filename = name;
00316              return TRUE;
00317            }
00318          free (name);
00319        }
00320       else if (ldfile_try_open_bfd (entry->filename, entry))
00321        {
00322          entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
00323            && is_sysrooted_pathname (entry->filename, TRUE);
00324          return TRUE;
00325        }
00326 
00327       if (IS_ABSOLUTE_PATH (entry->filename))
00328        return FALSE;
00329     }
00330 
00331   for (search = search_head; search != NULL; search = search->next)
00332     {
00333       char *string;
00334 
00335       if (entry->dynamic && ! link_info.relocatable)
00336        {
00337          if (ldemul_open_dynamic_archive (arch, search, entry))
00338            {
00339              entry->sysrooted = search->sysrooted;
00340              return TRUE;
00341            }
00342        }
00343 
00344       string = xmalloc (strlen (search->name)
00345                      + strlen (slash)
00346                      + strlen (lib)
00347                      + strlen (entry->filename)
00348                      + strlen (arch)
00349                      + strlen (suffix)
00350                      + 1);
00351 
00352       if (entry->is_archive)
00353        sprintf (string, "%s%s%s%s%s%s", search->name, slash,
00354                lib, entry->filename, arch, suffix);
00355       else
00356        sprintf (string, "%s%s%s", search->name, slash, entry->filename);
00357 
00358       if (ldfile_try_open_bfd (string, entry))
00359        {
00360          entry->filename = string;
00361          entry->sysrooted = search->sysrooted;
00362          return TRUE;
00363        }
00364 
00365       free (string);
00366     }
00367 
00368   return FALSE;
00369 }
00370 
00371 /* Open the input file specified by ENTRY.  */
00372 
00373 void
00374 ldfile_open_file (lang_input_statement_type *entry)
00375 {
00376   if (entry->the_bfd != NULL)
00377     return;
00378 
00379   if (! entry->search_dirs_flag)
00380     {
00381       if (ldfile_try_open_bfd (entry->filename, entry))
00382        return;
00383       if (strcmp (entry->filename, entry->local_sym_name) != 0)
00384        einfo (_("%F%P: %s (%s): No such file: %E\n"),
00385               entry->filename, entry->local_sym_name);
00386       else
00387        einfo (_("%F%P: %s: No such file: %E\n"), entry->local_sym_name);
00388     }
00389   else
00390     {
00391       search_arch_type *arch;
00392       bfd_boolean found = FALSE;
00393 
00394       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
00395       for (arch = search_arch_head; arch != NULL; arch = arch->next)
00396        {
00397          found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
00398          if (found)
00399            break;
00400 #ifdef VMS
00401          found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
00402          if (found)
00403            break;
00404 #endif
00405          found = ldemul_find_potential_libraries (arch->name, entry);
00406          if (found)
00407            break;
00408        }
00409 
00410       /* If we have found the file, we don't need to search directories
00411         again.  */
00412       if (found)
00413        entry->search_dirs_flag = FALSE;
00414       else if (entry->sysrooted
00415               && ld_sysroot
00416               && IS_ABSOLUTE_PATH (entry->local_sym_name))
00417        einfo (_("%F%P: cannot find %s inside %s\n"),
00418               entry->local_sym_name, ld_sysroot);
00419       else
00420        einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
00421     }
00422 }
00423 
00424 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
00425 
00426 static FILE *
00427 try_open (const char *name, const char *exten)
00428 {
00429   FILE *result;
00430   char buff[1000];
00431 
00432   result = fopen (name, "r");
00433 
00434   if (trace_file_tries)
00435     {
00436       if (result == NULL)
00437        info_msg (_("cannot find script file %s\n"), name);
00438       else
00439        info_msg (_("opened script file %s\n"), name);
00440     }
00441 
00442   if (result != NULL)
00443     return result;
00444 
00445   if (*exten)
00446     {
00447       sprintf (buff, "%s%s", name, exten);
00448       result = fopen (buff, "r");
00449 
00450       if (trace_file_tries)
00451        {
00452          if (result == NULL)
00453            info_msg (_("cannot find script file %s\n"), buff);
00454          else
00455            info_msg (_("opened script file %s\n"), buff);
00456        }
00457     }
00458 
00459   return result;
00460 }
00461 
00462 /* Try to open NAME; if that fails, look for it in any directories
00463    specified with -L, without and with EXTEND appended.  */
00464 
00465 static FILE *
00466 ldfile_find_command_file (const char *name, const char *extend)
00467 {
00468   search_dirs_type *search;
00469   FILE *result;
00470   char buffer[1000];
00471 
00472   /* First try raw name.  */
00473   result = try_open (name, "");
00474   if (result == NULL)
00475     {
00476       /* Try now prefixes.  */
00477       for (search = search_head; search != NULL; search = search->next)
00478        {
00479          sprintf (buffer, "%s%s%s", search->name, slash, name);
00480 
00481          result = try_open (buffer, extend);
00482          if (result)
00483            break;
00484        }
00485     }
00486 
00487   return result;
00488 }
00489 
00490 void
00491 ldfile_open_command_file (const char *name)
00492 {
00493   FILE *ldlex_input_stack;
00494   ldlex_input_stack = ldfile_find_command_file (name, "");
00495 
00496   if (ldlex_input_stack == NULL)
00497     {
00498       bfd_set_error (bfd_error_system_call);
00499       einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
00500     }
00501 
00502   lex_push_file (ldlex_input_stack, name);
00503 
00504   ldfile_input_filename = name;
00505   lineno = 1;
00506 
00507   saved_script_handle = ldlex_input_stack;
00508 }
00509 
00510 void
00511 ldfile_add_arch (const char *in_name)
00512 {
00513   char *name = xstrdup (in_name);
00514   search_arch_type *new = xmalloc (sizeof (search_arch_type));
00515 
00516   ldfile_output_machine_name = in_name;
00517 
00518   new->name = name;
00519   new->next = NULL;
00520   while (*name)
00521     {
00522       *name = TOLOWER (*name);
00523       name++;
00524     }
00525   *search_arch_tail_ptr = new;
00526   search_arch_tail_ptr = &new->next;
00527 
00528 }
00529 
00530 /* Set the output architecture.  */
00531 
00532 void
00533 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
00534 {
00535   const bfd_arch_info_type *arch = bfd_scan_arch (string);
00536 
00537   if (arch)
00538     {
00539       ldfile_output_architecture = arch->arch;
00540       ldfile_output_machine = arch->mach;
00541       ldfile_output_machine_name = arch->printable_name;
00542     }
00543   else if (defarch != bfd_arch_unknown)
00544     ldfile_output_architecture = defarch;
00545   else
00546     einfo (_("%P%F: cannot represent machine `%s'\n"), string);
00547 }