PATCH: Better handle discarded definition (take 2)

H. J. Lu hjl@lucon.org
Mon Jun 2 15:01:00 GMT 2003


The current handling of discarded definitions is not very informative.
It is very easy to tell what the problem really is. This patch tries
to improve it.


H.J.
-------------- next part --------------
bfd/

2003-05-30  H.J. Lu <hongjiu.lu@intel.com>

	* elflink.h (elf_link_input_bfd): Call linker error_handler
	for discarded definitions.

include/

2003-05-30  H.J. Lu <hongjiu.lu@intel.com>

	* bfdlink.h (LD_DEFINITION_IN_DISCARDED_SECTION): New.

ld/

2003-06-02  H.J. Lu <hongjiu.lu@intel.com>

	* ldmisc.c: Include "bfdlink.h".
	(error_handler): Handle LD_DEFINITION_IN_DISCARDED_SECTION
	and -LD_DEFINITION_IN_DISCARDED_SECTION.

ld/testsuite/

2003-06-02  H.J. Lu <hongjiu.lu@intel.com>

	* ld-discard/extern.d: Updated.
	* ld-discard/start.d: Likewise.
	* ld-discard/static.d: Likewise.

--- binutils/bfd/elflink.h.discard	2003-06-02 07:27:25.000000000 -0700
+++ binutils/bfd/elflink.h	2003-06-02 07:36:35.000000000 -0700
@@ -5003,13 +5003,12 @@ elf_link_input_bfd (finfo, input_bfd)
 			      memset (rel, 0, sizeof (*rel));
 			    }
 			  else
-			    {
-			      if (! ((*finfo->info->callbacks->undefined_symbol)
-				     (finfo->info, h->root.root.string,
-				      input_bfd, o, rel->r_offset,
-				      TRUE)))
-				return FALSE;
-			    }
+			    finfo->info->callbacks->error_handler
+			      (LD_DEFINITION_IN_DISCARDED_SECTION,
+			       _("%T: discarded in section `%s' from %s\n"),
+			       h->root.root.string,
+			       h->root.u.def.section->name,
+			       bfd_archive_filename (h->root.u.def.section->owner));
 			}
 		    }
 		  else
@@ -5028,26 +5027,21 @@ elf_link_input_bfd (finfo, input_bfd)
 			    }
 			  else
 			    {
-			      bfd_boolean ok;
-			      const char *msg
-				= _("local symbols in discarded section %s");
-			      bfd_size_type amt
-				= strlen (sec->name) + strlen (msg) - 1;
-			      char *buf = (char *) bfd_malloc (amt);
-
-			      if (buf != NULL)
-				sprintf (buf, msg, sec->name);
-			      else
-				buf = (char *) sec->name;
-			      ok = (*finfo->info->callbacks
-				    ->undefined_symbol) (finfo->info, buf,
-							 input_bfd, o,
-							 rel->r_offset,
-							 TRUE);
-			      if (buf != sec->name)
+			      static int count;
+			      int ok;
+			      char *buf;
+
+			      ok = asprintf (&buf, "local symbol %d",
+					     count++);
+			      if (ok == -1)
+				buf = (char *) "local symbol";
+			      finfo->info->callbacks->error_handler
+				(LD_DEFINITION_IN_DISCARDED_SECTION,
+				 _("%T: discarded in section `%s' from %s\n"),
+				 buf, sec->name,
+				 bfd_archive_filename (input_bfd));
+			      if (ok != -1)
 				free (buf);
-			      if (!ok)
-				return FALSE;
 			    }
 			}
 		    }
--- binutils/include/bfdlink.h.discard	2003-06-02 07:27:26.000000000 -0700
+++ binutils/include/bfdlink.h	2003-06-02 07:36:35.000000000 -0700
@@ -649,4 +649,7 @@ struct bfd_elf_version_tree
   int used;
 };
 
+/* Identifiers of linker error messages.  */
+#define LD_DEFINITION_IN_DISCARDED_SECTION	1
+
 #endif
--- binutils/ld/ldmisc.c.discard	2003-04-24 14:19:08.000000000 -0700
+++ binutils/ld/ldmisc.c	2003-06-02 07:40:49.000000000 -0700
@@ -22,6 +22,7 @@
    02111-1307, USA.  */
 
 #include "bfd.h"
+#include "bfdlink.h"
 #include "sysdep.h"
 #include "libiberty.h"
 #include "demangle.h"
@@ -508,12 +509,61 @@ ld_abort (file, line, fn)
 }
 
 bfd_boolean
-error_handler VPARAMS ((int id ATTRIBUTE_UNUSED, const char *fmt, ...))
+error_handler (int id, const char *fmt, ...)
 {
-  VA_OPEN (arg, fmt);
-  VA_FIXEDARG (arg, const char *, fmt);
+  va_list arg;
+
+  va_start (arg, fmt);
+
+  switch (id)
+    {
+    default:
+      break;
 
+    case -LD_DEFINITION_IN_DISCARDED_SECTION:
+    case LD_DEFINITION_IN_DISCARDED_SECTION:
+      {
+	static struct bfd_hash_table *hash;
+	static int fatal = 1;
+	const char *name;
+	va_list saved;
+
+	if (id == -LD_DEFINITION_IN_DISCARDED_SECTION)
+	  {
+	    fatal = va_arg (arg, int);
+	    goto out;
+	  }
+
+#ifdef __va_copy
+	__va_copy (saved, arg);
+#elif va_copy
+	va_copy (saved, arg);
+#else
+	saved = arg;
+#endif
+	name = va_arg (saved, const char *);
+	/* Only warn once about a particular undefined symbol.  */
+	if (hash == NULL)
+	  {
+	    hash = ((struct bfd_hash_table *)
+		    xmalloc (sizeof (struct bfd_hash_table)));
+	    if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
+	      einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
+	  }
+
+	if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
+	  goto out;
+
+	if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
+	  einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
+
+	if (fatal)
+	  config.make_executable = FALSE;
+      }
+      break;
+    }
   vfinfo (stderr, fmt, arg);
-  VA_CLOSE (arg);
+out:
+  va_end (arg);
   return TRUE;
 }
--- binutils/ld/testsuite/ld-discard/extern.d.discard	2002-10-12 08:43:29.000000000 -0700
+++ binutils/ld/testsuite/ld-discard/extern.d	2003-06-02 07:54:17.000000000 -0700
@@ -1,3 +1,3 @@
 #source: extern.s
 #ld: -T discard.ld
-#error: undefined reference to `(data|local symbols in discarded section \.data\.exit)'
+#error: data: discarded in section `\.data\.exit' from tmpdir/dump0.o
--- binutils/ld/testsuite/ld-discard/start.d.discard	2002-10-12 08:43:29.000000000 -0700
+++ binutils/ld/testsuite/ld-discard/start.d	2003-06-02 07:55:25.000000000 -0700
@@ -1,4 +1,4 @@
 #source: start.s
 #source: exit.s
 #ld: -T discard.ld
-#error: undefined reference to `data'
+#error: data: discarded in section `\.data\.exit' from tmpdir/dump1.o
--- binutils/ld/testsuite/ld-discard/static.d.discard	2002-10-12 08:43:29.000000000 -0700
+++ binutils/ld/testsuite/ld-discard/static.d	2003-06-02 07:55:49.000000000 -0700
@@ -1,3 +1,3 @@
 #source: static.s
 #ld: -T discard.ld
-#error: undefined reference to `local symbols in discarded section \.data\.exit'
+#error: local symbol 0: discarded in section `\.data\.exit' from tmpdir/dump0.o


More information about the Binutils mailing list