This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RFC: Implement --no-add-needed/--add-needed for ld


I am working on the gcc unwind library bug:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14925

A propposed fix requires adding --no-add-needed/--add-needed for ld:

http://gcc.gnu.org/ml/gcc/2004-04/msg01130.html

Here is something I have been working on. Any comments?


H.J.
----
--- binutils/bfd/bfd-in.h.needed	2004-07-06 16:10:22.000000000 -0700
+++ binutils/bfd/bfd-in.h	2004-07-07 23:53:41.676387892 -0700
@@ -628,7 +628,9 @@ struct bfd_link_needed_list
 enum dynamic_lib_link_class {
   DYN_NORMAL = 0,
   DYN_AS_NEEDED = 1,
-  DYN_DT_NEEDED = 2
+  DYN_DT_NEEDED = 2,
+  DYN_NO_ADD_NEEDED = 4,
+  DYN_NO_NEEDED = 8
 };
 
 extern bfd_boolean bfd_elf_record_link_assignment
@@ -646,6 +648,8 @@ extern const char *bfd_elf_get_dt_soname
   (bfd *);
 extern void bfd_elf_set_dyn_lib_class
   (bfd *, int);
+extern int bfd_elf_get_dyn_lib_class
+  (bfd *);
 extern struct bfd_link_needed_list *bfd_elf_get_runpath_list
   (bfd *, struct bfd_link_info *);
 extern bfd_boolean bfd_elf_discard_info
--- binutils/bfd/elf.c.needed	2004-07-06 16:10:22.000000000 -0700
+++ binutils/bfd/elf.c	2004-07-08 00:15:25.164802381 -0700
@@ -1562,6 +1562,18 @@ bfd_elf_set_dt_needed_name (bfd *abfd, c
     elf_dt_name (abfd) = name;
 }
 
+int
+bfd_elf_get_dyn_lib_class (bfd *abfd)
+{
+  int lib_class;
+  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+      && bfd_get_format (abfd) == bfd_object)
+    lib_class = elf_dyn_lib_class (abfd);
+  else
+    lib_class = 0;
+  return lib_class;
+}
+
 void
 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
 {
--- binutils/bfd/elflink.c.needed	2004-07-07 15:53:14.000000000 -0700
+++ binutils/bfd/elflink.c	2004-07-08 00:38:04.197520901 -0700
@@ -3064,8 +3064,13 @@ elf_link_add_object_symbols (bfd *abfd, 
       /* If this dynamic lib was specified on the command line with
 	 --as-needed in effect, then we don't want to add a DT_NEEDED
 	 tag unless the lib is actually used.  Similary for libs brought
-	 in by another lib's DT_NEEDED.  */
-      add_needed = elf_dyn_lib_class (abfd) == DYN_NORMAL;
+	 in by another lib's DT_NEEDED.  When --no-add-needed is used
+	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
+	 any dynamic library in DT_NEEDED tags in the dynamic lib at
+	 all.  */
+      add_needed = (elf_dyn_lib_class (abfd)
+		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
+		       | DYN_NO_NEEDED)) == 0;
 
       s = bfd_get_section_by_name (abfd, ".dynamic");
       if (s != NULL)
@@ -3846,7 +3851,17 @@ elf_link_add_object_symbols (bfd *abfd, 
 
 	      /* A symbol from a library loaded via DT_NEEDED of some
 		 other library is referenced by a regular object.
-		 Add a DT_NEEDED entry for it.  */
+		 Add a DT_NEEDED entry for it.  Issue an error if
+		 --no-add-needed is used.  */
+	      if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
+		{
+		  (*_bfd_error_handler)
+		    (_("%s: invalid DSO for symbol `%s' definition"),
+		     bfd_archive_filename (abfd), name);
+		  bfd_set_error (bfd_error_bad_value);
+		  goto error_free_vers;
+		}
+
 	      add_needed = TRUE;
 	      ret = elf_add_dt_needed_tag (info, soname, add_needed);
 	      if (ret < 0)
@@ -5818,7 +5833,7 @@ elf_link_check_versioned_symbol (struct 
     case bfd_link_hash_undefweak:
       abfd = h->root.u.undef.abfd;
       if ((abfd->flags & DYNAMIC) == 0
-	  || elf_dyn_lib_class (abfd) != DYN_DT_NEEDED)
+	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
 	return FALSE;
       break;
 
--- binutils/ld/emultempl/elf32.em.needed	2004-06-24 08:35:38.000000000 -0700
+++ binutils/ld/emultempl/elf32.em	2004-07-08 00:32:45.139790857 -0700
@@ -96,14 +96,25 @@ cat >>e${EMULATION_NAME}.c <<EOF
 static bfd_boolean
 gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry)
 {
-  if (!entry->as_needed
-      || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
-    return FALSE;
+  int class = 0;
 
   /* Tell the ELF linker that we don't want the output file to have a
      DT_NEEDED entry for this file, unless it is used to resolve
      references in a regular object.  */
-  bfd_elf_set_dyn_lib_class (entry->the_bfd, DYN_AS_NEEDED);
+  if (entry->as_needed)
+    class = DYN_AS_NEEDED;
+
+  /* Tell the ELF linker that we don't want the output file to have a
+     DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
+     this file at all.  */
+  if (!entry->add_needed)
+    class |= DYN_NO_ADD_NEEDED;
+
+  if (!class
+      || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
+    return FALSE;
+
+  bfd_elf_set_dyn_lib_class (entry->the_bfd, class);
 
   /* Continue on with normal load_symbols processing.  */
   return FALSE;
@@ -242,16 +253,24 @@ gld${EMULATION_NAME}_stat_needed (lang_i
 	   global_needed->name, global_needed->by, soname);
 }
 
+struct dt_needed
+{
+  bfd *by;
+  const char *name;
+};
 
 /* This function is called for each possible name for a dynamic object
    named by a DT_NEEDED entry.  The FORCE parameter indicates whether
    to skip the check for a conflicting version.  */
 
 static bfd_boolean
-gld${EMULATION_NAME}_try_needed (const char *name, int force)
+gld${EMULATION_NAME}_try_needed (struct dt_needed *needed,
+				 int force)
 {
   bfd *abfd;
+  const char *name = needed->name;
   const char *soname;
+  int class;
 
   abfd = bfd_openr (name, bfd_get_target (output_bfd));
   if (abfd == NULL)
@@ -364,7 +383,16 @@ cat >>e${EMULATION_NAME}.c <<EOF
   /* Tell the ELF linker that we don't want the output file to have a
      DT_NEEDED entry for this file, unless it is used to resolve
      references in a regular object.  */
-  bfd_elf_set_dyn_lib_class (abfd, DYN_DT_NEEDED);
+  class = DYN_DT_NEEDED;
+
+  /* Tell the ELF linker that we don't want the output file to have a
+     DT_NEEDED entry for this file at all.  */
+  if (needed->by
+      && (bfd_elf_get_dyn_lib_class (needed->by)
+	  & DYN_NO_ADD_NEEDED) != 0)
+    class |= DYN_NO_NEEDED;
+
+  bfd_elf_set_dyn_lib_class (abfd, class);
 
   /* Add this file into the symbol table.  */
   if (! bfd_link_add_symbols (abfd, &link_info))
@@ -377,16 +405,23 @@ cat >>e${EMULATION_NAME}.c <<EOF
 /* Search for a needed file in a path.  */
 
 static bfd_boolean
-gld${EMULATION_NAME}_search_needed (const char *path, const char *name, int force)
+gld${EMULATION_NAME}_search_needed (const char *path,
+				    struct dt_needed *n, int force)
 {
   const char *s;
+  const char *name = n->name;
   size_t len;
+  struct dt_needed needed;
 
   if (name[0] == '/')
-    return gld${EMULATION_NAME}_try_needed (name, force);
+    return gld${EMULATION_NAME}_try_needed (n, force);
 
   if (path == NULL || *path == '\0')
     return FALSE;
+
+  needed.by = n->by;
+  needed.name = n->name;
+
   len = strlen (name);
   while (1)
     {
@@ -407,7 +442,8 @@ gld${EMULATION_NAME}_search_needed (cons
 	}
       strcpy (sset, name);
 
-      if (gld${EMULATION_NAME}_try_needed (filename, force))
+      needed.name = filename;
+      if (gld${EMULATION_NAME}_try_needed (&needed, force))
 	return TRUE;
 
       free (filename);
@@ -474,6 +510,7 @@ gld${EMULATION_NAME}_check_ld_so_conf (c
 {
   static bfd_boolean initialized;
   static char *ld_so_conf;
+  struct dt_needed needed;
 
   if (! initialized)
     {
@@ -548,7 +585,10 @@ gld${EMULATION_NAME}_check_ld_so_conf (c
   if (ld_so_conf == NULL)
     return FALSE;
 
-  return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
+
+  needed.by = NULL;
+  needed.name = name;
+  return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force);
 }
 
 EOF
@@ -631,6 +671,7 @@ gld${EMULATION_NAME}_after_open (void)
   for (l = needed; l != NULL; l = l->next)
     {
       struct bfd_link_needed_list *ll;
+      struct dt_needed n, nn;
       int force;
 
       /* If we've already seen this file, skip it.  */
@@ -647,6 +688,9 @@ gld${EMULATION_NAME}_after_open (void)
       if (global_found)
 	continue;
 
+      n.by = l->by;
+      n.name = l->name;
+      nn.by = l->by;
       if (trace_file_tries)
 	info_msg (_("%s needed by %B\n"), l->name, l->by);
 
@@ -676,13 +720,13 @@ fi
 cat >>e${EMULATION_NAME}.c <<EOF
 
 	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
-						  l->name, force))
+						  &n, force))
 	    break;
 EOF
 if [ "x${USE_LIBPATH}" = xyes ] ; then
 cat >>e${EMULATION_NAME}.c <<EOF
 	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
-						  l->name, force))
+						  &n, force))
 	    break;
 EOF
 fi
@@ -692,12 +736,12 @@ cat >>e${EMULATION_NAME}.c <<EOF
 	      && command_line.rpath == NULL)
 	    {
 	      lib_path = (const char *) getenv ("LD_RUN_PATH");
-	      if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
+	      if (gld${EMULATION_NAME}_search_needed (lib_path, &n,
 						      force))
 		break;
 	    }
 	  lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
-	  if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
+	  if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
 	    break;
 EOF
 fi
@@ -710,7 +754,7 @@ cat >>e${EMULATION_NAME}.c <<EOF
 	      char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name);
 	      found = (rp->by == l->by
 		       && gld${EMULATION_NAME}_search_needed (tmpname,
-							      l->name,
+							      &n,
 							      force));
 	      free (tmpname);
 	    }
@@ -729,7 +773,8 @@ cat >>e${EMULATION_NAME}.c <<EOF
 		continue;
 	      filename = (char *) xmalloc (strlen (search->name) + len + 2);
 	      sprintf (filename, "%s/%s", search->name, l->name);
-	      if (gld${EMULATION_NAME}_try_needed (filename, force))
+	      nn.name = filename;
+	      if (gld${EMULATION_NAME}_try_needed (&nn, force))
 		break;
 	      free (filename);
 	    }
--- binutils/ld/ld.texinfo.needed	2004-07-06 08:36:55.000000000 -0700
+++ binutils/ld/ld.texinfo	2004-07-07 14:42:25.000000000 -0700
@@ -984,6 +984,17 @@ needed. @option{--as-needed} causes DT_N
 for libraries that satisfy some reference from regular objects.
 @option{--no-as-needed} restores the default behaviour.
 
+@kindex --add-needed
+@kindex --no-add-needed
+@item --add-needed
+@itemx --no-add-needed
+This option affects the treatment of dynamic libraries from ELF
+DT_NEEDED tags in dynamic libraries mentioned on the command line after
+the @option{--no-add-needed} option.  Normally, the linker will load
+each dynamic library from DT_NEEDED tags.  @option{--no-add-needed}
+causes those libraries from DT_NEEDED tags to be ignored.
+@option{--add-needed} restores the default behaviour.
+
 @kindex -assert @var{keyword}
 @item -assert @var{keyword}
 This option is ignored for SunOS compatibility.
--- binutils/ld/ldlang.c.needed	2004-07-06 16:10:22.000000000 -0700
+++ binutils/ld/ldlang.c	2004-07-07 22:43:07.000000000 -0700
@@ -454,6 +454,7 @@ new_afile (const char *name,
   p->next = NULL;
   p->symbol_count = 0;
   p->dynamic = config.dynamic_link;
+  p->add_needed = add_needed;
   p->as_needed = as_needed;
   p->whole_archive = whole_archive;
   p->loaded = FALSE;
--- binutils/ld/ldlang.h.needed	2004-05-19 08:24:36.000000000 -0700
+++ binutils/ld/ldlang.h	2004-07-07 15:30:50.000000000 -0700
@@ -266,6 +266,10 @@ typedef struct lang_input_statement_stru
   /* Whether to search for this entry as a dynamic archive.  */
   bfd_boolean dynamic;
 
+  /* Whether dynamic libraries in DT_NEEDED tags in this entry should
+     be loaded.  */
+  bfd_boolean add_needed;
+
   /* Whether this entry should cause a DT_NEEDED tag only when
      satisfying references from regular files, or always.  */
   bfd_boolean as_needed;
--- binutils/ld/ldmain.c.needed	2004-05-25 22:35:42.000000000 -0700
+++ binutils/ld/ldmain.c	2004-07-07 14:46:06.000000000 -0700
@@ -97,6 +97,9 @@ bfd_boolean whole_archive;
    actually satisfies some reference in a regular object.  */
 bfd_boolean as_needed;
 
+/* Nonzero means load DT_NEEDED entries.  */
+bfd_boolean add_needed = TRUE;
+
 /* TRUE if we should demangle symbol names.  */
 bfd_boolean demangling;
 
--- binutils/ld/ldmain.h.needed	2004-03-18 10:11:56.000000000 -0800
+++ binutils/ld/ldmain.h	2004-07-07 14:46:21.000000000 -0700
@@ -33,6 +33,7 @@ extern bfd_boolean trace_file_tries;
 extern bfd_boolean version_printed;
 extern bfd_boolean whole_archive;
 extern bfd_boolean as_needed;
+extern bfd_boolean add_needed;
 extern bfd_boolean demangling;
 extern int g_switch_value;
 extern const char *output_filename;
--- binutils/ld/lexsup.c.needed	2004-05-28 12:50:30.000000000 -0700
+++ binutils/ld/lexsup.c	2004-07-07 15:07:21.000000000 -0700
@@ -113,6 +113,8 @@ enum option_values
   OPTION_SPLIT_BY_RELOC,
   OPTION_SPLIT_BY_FILE ,
   OPTION_WHOLE_ARCHIVE,
+  OPTION_ADD_NEEDED,
+  OPTION_NO_ADD_NEEDED,
   OPTION_AS_NEEDED,
   OPTION_NO_AS_NEEDED,
   OPTION_WRAP,
@@ -486,6 +488,12 @@ static const struct ld_option ld_options
   { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
     '\0', NULL, N_("Include all objects from following archives"),
     TWO_DASHES },
+  { {"add-needed", no_argument, NULL, OPTION_ADD_NEEDED},
+    '\0', NULL, N_("Load DT_NEEDED tags for following dynamic libs"),
+    TWO_DASHES },
+  { {"no-add-needed", no_argument, NULL, OPTION_NO_ADD_NEEDED},
+    '\0', NULL, N_("Do not load DT_NEEDED tags for following dynamic libs"),
+    TWO_DASHES },
   { {"as-needed", no_argument, NULL, OPTION_AS_NEEDED},
     '\0', NULL, N_("Only set DT_NEEDED for following dynamic libs if used"),
     TWO_DASHES },
@@ -1196,6 +1204,12 @@ parse_args (unsigned argc, char **argv)
 	case OPTION_WHOLE_ARCHIVE:
 	  whole_archive = TRUE;
 	  break;
+	case OPTION_ADD_NEEDED:
+	  add_needed = TRUE;
+	  break;
+	case OPTION_NO_ADD_NEEDED:
+	  add_needed = FALSE;
+	  break;
 	case OPTION_AS_NEEDED:
 	  as_needed = TRUE;
 	  break;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]