* emultempl/elf32.em (gld${EMULATION_NAME}_check_ld_so_conf): New
[deliverable/binutils-gdb.git] / ld / emultempl / elf32.em
index b3cb74d63a7feee58f121d1486eddfa145612bc2..71e34b5abc0039ccbcb17c49f8fe6ffa81c98f67 100644 (file)
@@ -4,7 +4,7 @@ cat >e${EMULATION_NAME}.c <<EOF
 /* This file is is generated by a shell script.  DO NOT EDIT! */
 
 /* 32 bit ELF emulation code for ${EMULATION_NAME}
-   Copyright (C) 1991, 1993, 1994, 1995 Free Software Foundation, Inc.
+   Copyright (C) 1991, 93, 94, 95, 1996 Free Software Foundation, Inc.
    Written by Steve Chamberlain <sac@cygnus.com>
    ELF support by Ian Lance Taylor <ian@cygnus.com>
 
@@ -34,7 +34,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include "bfdlink.h"
 
 #include "ld.h"
-#include "config.h"
 #include "ldmain.h"
 #include "ldemul.h"
 #include "ldfile.h"
@@ -45,10 +44,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
 static boolean gld${EMULATION_NAME}_open_dynamic_archive
-  PARAMS ((const char *, lang_input_statement_type *));
+  PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
 static void gld${EMULATION_NAME}_check_needed
   PARAMS ((lang_input_statement_type *));
+static void gld${EMULATION_NAME}_stat_needed
+  PARAMS ((lang_input_statement_type *));
 static boolean gld${EMULATION_NAME}_search_needed
   PARAMS ((const char *, const char *));
 static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *));
@@ -65,7 +66,7 @@ static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
 static void
 gld${EMULATION_NAME}_before_parse()
 {
-  ldfile_output_architecture = bfd_arch_${ARCH};
+  ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
   config.dynamic_link = ${DYNAMIC_LINK-true};
 }
 
@@ -73,16 +74,33 @@ gld${EMULATION_NAME}_before_parse()
    dynamic libraries have an extension of .so.  */
 
 static boolean
-gld${EMULATION_NAME}_open_dynamic_archive (arch, entry)
+gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
      const char *arch;
+     search_dirs_type *search;
      lang_input_statement_type *entry;
 {
   const char *filename;
+  char *string;
+
+  if (! entry->is_archive)
+    return false;
 
   filename = entry->filename;
 
-  if (! ldfile_open_file_search (arch, entry, "lib", ".so"))
-    return false;
+  string = (char *) xmalloc (strlen (search->name)
+                            + strlen (filename)
+                            + strlen (arch)
+                            + sizeof "/lib.so");
+
+  sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
+
+  if (! ldfile_try_open_bfd (string, entry))
+    {
+      free (string);
+      return false;
+    }
+
+  entry->filename = string;
 
   /* We have found a dynamic object to include in the link.  The ELF
      backend linker will create a DT_NEEDED entry in the .dynamic
@@ -113,10 +131,98 @@ gld${EMULATION_NAME}_open_dynamic_archive (arch, entry)
   return true;
 }
 
+EOF
+if [ "x${host}" = "x${target}" ] ; then
+cat >>e${EMULATION_NAME}.c <<EOF
+
+/* For a native linker, check the file /etc/ld.so.conf for directories
+   in which we may find shared libraries.  /etc/ld.so.conf is really
+   only meaningful on Linux, but we check it on other systems anyhow.  */
+
+static boolean gld${EMULATION_NAME}_check_ld_so_conf PARAMS ((const char *));
+
+static boolean
+gld${EMULATION_NAME}_check_ld_so_conf (name)
+     const char *name;
+{
+  static boolean initialized;
+  static char *ld_so_conf;
+
+  if (! initialized)
+    {
+      FILE *f;
+
+      f = fopen ("/etc/ld.so.conf", FOPEN_RT);
+      if (f != NULL)
+       {
+         char *b;
+         size_t len, alloc;
+         int c;
+
+         len = 0;
+         alloc = 100;
+         b = (char *) xmalloc (alloc);
+
+         while ((c = getc (f)) != EOF)
+           {
+             if (len + 1 >= alloc)
+               {
+                 alloc *= 2;
+                 b = (char *) xrealloc (b, alloc);
+               }
+             if (c != ':'
+                 && c != ' '
+                 && c != '\t'
+                 && c != '\n'
+                 && c != ',')
+               {
+                 b[len] = c;
+                 ++len;
+               }
+             else
+               {
+                 if (len > 0 && b[len - 1] != ':')
+                   {
+                     b[len] = ':';
+                     ++len;
+                   }
+               }
+           }
+
+         if (len > 0 && b[len - 1] == ':')
+           --len;
+
+         if (len > 0)
+           b[len] = '\0';
+         else
+           {
+             free (b);
+             b = NULL;
+           }
+
+         fclose (f);
+
+         ld_so_conf = b;
+       }
+
+      initialized = true;
+    }
+
+  if (ld_so_conf == NULL)
+    return false;
+
+  return gld${EMULATION_NAME}_search_needed (ld_so_conf, name);
+}
+
+EOF
+fi
+cat >>e${EMULATION_NAME}.c <<EOF
+
 /* These variables are required to pass information back and forth
-   between after_open and check_needed.  */
+   between after_open and check_needed and stat_needed.  */
 
-static struct bfd_elf_link_needed_list *global_needed;
+static struct bfd_link_needed_list *global_needed;
+static struct stat global_stat;
 static boolean global_found;
 
 /* This is called after all the input files have been opened.  */
@@ -124,7 +230,11 @@ static boolean global_found;
 static void
 gld${EMULATION_NAME}_after_open ()
 {
-  struct bfd_elf_link_needed_list *needed, *l;
+  struct bfd_link_needed_list *needed, *l;
+
+  /* We only need to worry about this when doing a final link.  */
+  if (link_info.relocateable || link_info.shared)
+    return;
 
   /* Get the list of files which appear in DT_NEEDED entries in
      dynamic objects included in the link (often there will be none).
@@ -138,7 +248,7 @@ gld${EMULATION_NAME}_after_open ()
   needed = bfd_elf_get_needed_list (output_bfd, &link_info);
   for (l = needed; l != NULL; l = l->next)
     {
-      struct bfd_elf_link_needed_list *ll;
+      struct bfd_link_needed_list *ll;
       const char *lib_path;
       size_t len;
       search_dirs_type *search;
@@ -159,15 +269,31 @@ gld${EMULATION_NAME}_after_open ()
 
       /* We need to find this file and include the symbol table.  We
         want to search for the file in the same way that the dynamic
-        linker will search.  That means that we want to use rpath,
-        then the environment variable LD_LIBRARY_PATH, then the
-        linker script LIB_SEARCH_DIRS.  We do not search using the -L
-        arguments.  */
+        linker will search.  That means that we want to use
+        rpath_link, rpath, then the environment variable
+        LD_LIBRARY_PATH (native only), then the linker script
+        LIB_SEARCH_DIRS.  We do not search using the -L arguments.  */
+      if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
+                                             l->name))
+       continue;
       if (gld${EMULATION_NAME}_search_needed (command_line.rpath, l->name))
        continue;
+      if (command_line.rpath_link == NULL
+         && command_line.rpath == NULL)
+       {
+         lib_path = (const char *) getenv ("LD_RUN_PATH");
+         if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
+           continue;
+       }
+EOF
+if [ "x${host}" = "x${target}" ] ; then
+cat >>e${EMULATION_NAME}.c <<EOF
       lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
       if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
        continue;
+EOF
+fi
+cat >>e${EMULATION_NAME}.c <<EOF
       len = strlen (l->name);
       for (search = search_head; search != NULL; search = search->next)
        {
@@ -183,6 +309,14 @@ gld${EMULATION_NAME}_after_open ()
        }
       if (search != NULL)
        continue;
+EOF
+if [ "x${host}" = "x${target}" ] ; then
+cat >>e${EMULATION_NAME}.c <<EOF
+      if (gld${EMULATION_NAME}_check_ld_so_conf (l->name))
+       continue;
+EOF
+fi
+cat >>e${EMULATION_NAME}.c <<EOF
 
       einfo ("%P: warning: %s, needed by %B, not found\n",
             l->name, l->by);
@@ -259,6 +393,25 @@ gld${EMULATION_NAME}_try_needed (name)
 
   /* We've found a dynamic object matching the DT_NEEDED entry.  */
 
+  /* We have already checked that there is no other input file of the
+     same name.  We must now check again that we are not including the
+     same file twice.  We need to do this because on many systems
+     libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
+     reference libc.so.1.  If we have already included libc.so, we
+     don't want to include libc.so.1 if they are the same file, and we
+     can only check that using stat.  */
+
+  if (bfd_stat (abfd, &global_stat) != 0)
+    einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
+  global_found = false;
+  lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
+  if (global_found)
+    {
+      /* Return true to indicate that we found the file, even though
+         we aren't going to do anything with it.  */
+      return true;
+    }
+
   /* Tell the ELF backend that don't want the output file to have a
      DT_NEEDED entry for this file.  */
   bfd_elf_set_dt_needed_name (abfd, "");
@@ -270,15 +423,110 @@ gld${EMULATION_NAME}_try_needed (name)
   return true;
 }
 
-/* See if an input file matches a DT_NEEDED entry.  */
+/* See if an input file matches a DT_NEEDED entry by name.  */
 
 static void
 gld${EMULATION_NAME}_check_needed (s)
      lang_input_statement_type *s;
 {
+  if (global_found)
+    return;
+
   if (s->filename != NULL
       && strcmp (s->filename, global_needed->name) == 0)
-    global_found = true;
+    {
+      global_found = true;
+      return;
+    }
+
+  if (s->the_bfd != NULL)
+    {
+      const char *soname;
+
+      soname = bfd_elf_get_dt_soname (s->the_bfd);
+      if (soname != NULL
+         && strcmp (soname, global_needed->name) == 0)
+       {
+         global_found = true;
+         return;
+       }
+    }
+         
+  if (s->search_dirs_flag
+      && s->filename != NULL
+      && strchr (global_needed->name, '/') == NULL)
+    {
+      const char *f;
+
+      f = strrchr (s->filename, '/');
+      if (f != NULL
+         && strcmp (f + 1, global_needed->name) == 0)
+       {
+         global_found = true;
+         return;
+       }
+    }
+}
+
+/* See if an input file matches a DT_NEEDED entry by running stat on
+   the file.  */
+
+static void
+gld${EMULATION_NAME}_stat_needed (s)
+     lang_input_statement_type *s;
+{
+  struct stat st;
+  const char *suffix;
+  const char *soname;
+  const char *f;
+
+  if (global_found)
+    return;
+  if (s->the_bfd == NULL)
+    return;
+
+  if (bfd_stat (s->the_bfd, &st) != 0)
+    {
+      einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
+      return;
+    }
+
+  if (st.st_dev == global_stat.st_dev
+      && st.st_ino == global_stat.st_ino)
+    {
+      global_found = true;
+      return;
+    }
+
+  /* We issue a warning if it looks like we are including two
+     different versions of the same shared library.  For example,
+     there may be a problem if -lc picks up libc.so.6 but some other
+     shared library has a DT_NEEDED entry of libc.so.5.  This is a
+     hueristic test, and it will only work if the name looks like
+     NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
+     If we really want to issue warnings about mixing version numbers
+     of shared libraries, we need to find a better way.  */
+
+  if (strchr (global_needed->name, '/') != NULL)
+    return;
+  suffix = strstr (global_needed->name, ".so.");
+  if (suffix == NULL)
+    return;
+  suffix += sizeof ".so." - 1;
+
+  soname = bfd_elf_get_dt_soname (s->the_bfd);
+  if (soname == NULL)
+    soname = s->filename;
+
+  f = strrchr (soname, '/');
+  if (f != NULL)
+    ++f;
+  else
+    f = soname;
+
+  if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
+    einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
+          global_needed->name, global_needed->by, f);
 }
 
 /* This is called after the sections have been attached to output
@@ -287,6 +535,7 @@ gld${EMULATION_NAME}_check_needed (s)
 static void
 gld${EMULATION_NAME}_before_allocation ()
 {
+  const char *rpath;
   asection *sinterp;
 
   /* If we are going to make any variable assignments, we need to let
@@ -296,9 +545,12 @@ gld${EMULATION_NAME}_before_allocation ()
 
   /* Let the ELF backend work out the sizes of any sections required
      by dynamic linking.  */
+  rpath = command_line.rpath;
+  if (rpath == NULL)
+    rpath = (const char *) getenv ("LD_RUN_PATH");
   if (! bfd_elf32_size_dynamic_sections (output_bfd,
                                         command_line.soname,
-                                        command_line.rpath,
+                                        rpath,
                                         command_line.export_dynamic,
                                         &link_info,
                                         &sinterp))
@@ -338,7 +590,10 @@ gld${EMULATION_NAME}_before_allocation ()
          einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
                 is->the_bfd);
        msg[sz] = '\0';
-       ret = link_info.callbacks->warning (&link_info, msg);
+       ret = link_info.callbacks->warning (&link_info, msg,
+                                           (const char *) NULL,
+                                           is->the_bfd, (asection *) NULL,
+                                           (bfd_vma) 0);
        ASSERT (ret);
        free (msg);
 
@@ -436,7 +691,7 @@ gld${EMULATION_NAME}_find_exp_assignment (exp)
       break;
 
     case etree_trinary:
-      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
+      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
       break;
@@ -491,6 +746,17 @@ gld${EMULATION_NAME}_place_orphan (file, s)
 
   secname = bfd_get_section_name (s->owner, s);
 
+  /* If this is a final link, then always put .gnu.warning.SYMBOL
+     sections into the .text section to get them out of the way.  */
+  if (! link_info.shared
+      && ! link_info.relocateable
+      && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
+      && hold_text != NULL)
+    {
+      wild_doit (&hold_text->children, s, hold_text, file);
+      return true;
+    }
+
   /* Decide which segment the section should go in based on the
      section name and section flags.  */
   place = NULL;
This page took 0.027952 seconds and 4 git commands to generate.