* configure.tgt: Add bfin-*rtems*.
[external/binutils.git] / ld / ldcref.c
index bcb787b..4ebb8cd 100644 (file)
@@ -1,32 +1,36 @@
 /* ldcref.c -- output a cross reference table
-   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
-   Free Software Foundation, Inc.
+   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006,
+   2007  Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@cygnus.com>
 
-This file is part of GLD, the Gnu Linker.
+   This file is part of the GNU Binutils.
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* This file holds routines that manage the cross reference table.
    The table is used to generate cross reference reports.  It is also
    used to implement the NOCROSSREFS command in the linker script.  */
 
-#include "bfd.h"
 #include "sysdep.h"
+#include "bfd.h"
 #include "bfdlink.h"
 #include "libiberty.h"
+#include "demangle.h"
+#include "objalloc.h"
 
 #include "ld.h"
 #include "ldmain.h"
@@ -55,7 +59,7 @@ struct cref_ref {
 struct cref_hash_entry {
   struct bfd_hash_entry root;
   /* The demangled name.  */
-  char *demangled;
+  const char *demangled;
   /* References to and definitions of this symbol.  */
   struct cref_ref *refs;
 };
@@ -101,6 +105,16 @@ static bfd_boolean cref_initialized;
 
 static size_t cref_symcount;
 
+/* Used to take a snapshot of the cref hash table when starting to
+   add syms from an as-needed library.  */
+static struct bfd_hash_entry **old_table;
+static unsigned int old_size;
+static unsigned int old_count;
+static void *old_tab;
+static void *alloc_mark;
+static size_t tabsize, entsize, refsize;
+static size_t old_symcount;
+
 /* Create an entry in a cref hash table.  */
 
 static struct bfd_hash_entry *
@@ -149,7 +163,8 @@ add_cref (const char *name,
 
   if (! cref_initialized)
     {
-      if (! bfd_hash_table_init (&cref_table.root, cref_hash_newfunc))
+      if (!bfd_hash_table_init (&cref_table.root, cref_hash_newfunc,
+                               sizeof (struct cref_hash_entry)))
        einfo (_("%X%P: bfd_hash_table_init of cref table failed: %E\n"));
       cref_initialized = TRUE;
     }
@@ -164,7 +179,9 @@ add_cref (const char *name,
 
   if (r == NULL)
     {
-      r = xmalloc (sizeof *r);
+      r = bfd_hash_allocate (&cref_table.root, sizeof *r);
+      if (r == NULL)
+       einfo (_("%X%P: cref alloc failed: %E\n"));
       r->next = h->refs;
       h->refs = r;
       r->abfd = abfd;
@@ -181,6 +198,125 @@ add_cref (const char *name,
     r->def = TRUE;
 }
 
+/* Called before loading an as-needed library to take a snapshot of
+   the cref hash table, and after we have loaded or found that the
+   library was not needed.  */
+
+bfd_boolean
+handle_asneeded_cref (bfd *abfd ATTRIBUTE_UNUSED,
+                     enum notice_asneeded_action act)
+{
+  unsigned int i;
+
+  if (!cref_initialized)
+    return TRUE;
+
+  if (act == notice_as_needed)
+    {
+      char *old_ent, *old_ref;
+
+      for (i = 0; i < cref_table.root.size; i++)
+       {
+         struct bfd_hash_entry *p;
+         struct cref_hash_entry *c;
+         struct cref_ref *r;
+
+         for (p = cref_table.root.table[i]; p != NULL; p = p->next)
+           {
+             entsize += cref_table.root.entsize;
+             c = (struct cref_hash_entry *) p;
+             for (r = c->refs; r != NULL; r = r->next)
+               refsize += sizeof (struct cref_hash_entry);
+           }
+       }
+
+      tabsize = cref_table.root.size * sizeof (struct bfd_hash_entry *);
+      old_tab = xmalloc (tabsize + entsize + refsize);
+
+      alloc_mark = bfd_hash_allocate (&cref_table.root, 1);
+      if (alloc_mark == NULL)
+       return FALSE;
+
+      memcpy (old_tab, cref_table.root.table, tabsize);
+      old_ent = (char *) old_tab + tabsize;
+      old_ref = (char *) old_ent + entsize;
+      old_table = cref_table.root.table;
+      old_size = cref_table.root.size;
+      old_count = cref_table.root.count;
+      old_symcount = cref_symcount;
+
+      for (i = 0; i < cref_table.root.size; i++)
+       {
+         struct bfd_hash_entry *p;
+         struct cref_hash_entry *c;
+         struct cref_ref *r;
+
+         for (p = cref_table.root.table[i]; p != NULL; p = p->next)
+           {
+             memcpy (old_ent, p, cref_table.root.entsize);
+             old_ent = (char *) old_ent + cref_table.root.entsize;
+             c = (struct cref_hash_entry *) p;
+             for (r = c->refs; r != NULL; r = r->next)
+               {
+                 memcpy (old_ref, r, sizeof (struct cref_hash_entry));
+                 old_ref = (char *) old_ref + sizeof (struct cref_hash_entry);
+               }
+           }
+       }
+      return TRUE;
+    }
+
+  if (act == notice_not_needed)
+    {
+      char *old_ent, *old_ref;
+
+      if (old_tab == NULL)
+       {
+         /* The only way old_tab can be NULL is if the cref hash table
+            had not been initialised when notice_as_needed.  */
+         bfd_hash_table_free (&cref_table.root);
+         cref_initialized = FALSE;
+         return TRUE;
+       }
+
+      old_ent = (char *) old_tab + tabsize;
+      old_ref = (char *) old_ent + entsize;
+      cref_table.root.table = old_table;
+      cref_table.root.size = old_size;
+      cref_table.root.count = old_count;
+      memcpy (cref_table.root.table, old_tab, tabsize);
+      cref_symcount = old_symcount;
+
+      for (i = 0; i < cref_table.root.size; i++)
+       {
+         struct bfd_hash_entry *p;
+         struct cref_hash_entry *c;
+         struct cref_ref *r;
+
+         for (p = cref_table.root.table[i]; p != NULL; p = p->next)
+           {
+             memcpy (p, old_ent, cref_table.root.entsize);
+             old_ent = (char *) old_ent + cref_table.root.entsize;
+             c = (struct cref_hash_entry *) p;
+             for (r = c->refs; r != NULL; r = r->next)
+               {
+                 memcpy (r, old_ref, sizeof (struct cref_hash_entry));
+                 old_ref = (char *) old_ref + sizeof (struct cref_hash_entry);
+               }
+           }
+       }
+
+      objalloc_free_block ((struct objalloc *) cref_table.root.memory,
+                          alloc_mark);
+    }
+  else if (act != notice_needed)
+    return FALSE;
+
+  free (old_tab);
+  old_tab = NULL;
+  return TRUE;
+}
+
 /* Copy the addresses of the hash table entries into an array.  This
    is called via cref_hash_traverse.  We also fill in the demangled
    name.  */
@@ -191,7 +327,10 @@ cref_fill_array (struct cref_hash_entry *h, void *data)
   struct cref_hash_entry ***pph = data;
 
   ASSERT (h->demangled == NULL);
-  h->demangled = demangle (h->root.string);
+  h->demangled = bfd_demangle (output_bfd, h->root.string,
+                              DMGL_ANSI | DMGL_PARAMS);
+  if (h->demangled == NULL)
+    h->demangled = h->root.string;
 
   **pph = h;
 
@@ -577,11 +716,11 @@ check_reloc_refs (bfd *abfd, asection *sec, void *iarg)
                                                   | BSF_WEAK)) != 0))
              || (!global
                  && ((*q->sym_ptr_ptr)->flags & (BSF_LOCAL
-                                                 | BSF_SECTION_SYM)) != 0))
+                                                 | BSF_SECTION_SYM)) != 0
+                 && bfd_get_section (*q->sym_ptr_ptr) == info->defsec))
          && (symname != NULL
              ? strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), symname) == 0
-             : (((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
-                && bfd_get_section (*q->sym_ptr_ptr) == info->defsec)))
+             : ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0))
        {
          /* We found a reloc for the symbol.  The symbol is defined
             in OUTSECNAME.  This reloc is from a section which is