* Makefile.in (SFILES): Add cp-names.y.
[deliverable/binutils-gdb.git] / bfd / archive.c
index ba6e684ee2348d2820cba170719f089ad0b70fae..79808459b15df27dcbb0948f45884aeebd62eabe 100644 (file)
@@ -1,24 +1,24 @@
 /* BFD back-end for archive files (libraries).
    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003
+   2000, 2001, 2002, 2003, 2004, 2005
    Free Software Foundation, Inc.
    Written by Cygnus Support.  Mostly Gumby Henkel-Wallace's fault.
 
-This file is part of BFD, the Binary File Descriptor library.
+   This file is part of BFD, the Binary File Descriptor library.
 
-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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 /*
 @setfilename archive-info
@@ -133,15 +133,12 @@ DESCRIPTION
 #include "aout/ar.h"
 #include "aout/ranlib.h"
 #include "safe-ctype.h"
+#include "hashtab.h"
 
 #ifndef errno
 extern int errno;
 #endif
 
-#ifdef GNU960
-#define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
-#endif
-
 /* We keep a cache of archive filepointers to archive elements to
    speed up searching the archive by filepos.  We only add an entry to
    the cache when we actually read one.  We also don't sort the cache;
@@ -150,8 +147,7 @@ extern int errno;
    to the front of the contents!  */
 struct ar_cache {
   file_ptr ptr;
-  bfd *arelt;
-  struct ar_cache *next;
+  bfd *arbfd;
 };
 
 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
@@ -221,7 +217,7 @@ bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
   return prev;
 }
 
-/* To be called by backends only */
+/* To be called by backends only */
 
 bfd *
 _bfd_create_empty_archive_element_shell (bfd *obfd)
@@ -251,40 +247,62 @@ bfd_set_archive_head (bfd *output_archive, bfd *new_head)
 bfd *
 _bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
 {
-  struct ar_cache *current;
+  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
+  struct ar_cache m;
+  m.ptr = filepos;
 
-  for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
-       current = current->next)
-    if (current->ptr == filepos)
-      return current->arelt;
+  if (hash_table)
+    {
+      struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
+      if (!entry)
+       return NULL;
+      else
+       return entry->arbfd;
+    }
+  else
+    return NULL;
+}
 
-  return NULL;
+static hashval_t
+hash_file_ptr (const PTR p)
+{
+  return (hashval_t) (((struct ar_cache *) p)->ptr);
 }
 
-/* Kind of stupid to call cons for each one, but we don't do too many */
+/* Returns non-zero if P1 and P2 are equal.  */
+
+static int
+eq_file_ptr (const PTR p1, const PTR p2)
+{
+  struct ar_cache *arc1 = (struct ar_cache *) p1;
+  struct ar_cache *arc2 = (struct ar_cache *) p2;
+  return arc1->ptr == arc2->ptr;
+}
+
+/* Kind of stupid to call cons for each one, but we don't do too many.  */
+
 bfd_boolean
 _bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
 {
-  bfd_size_type amt = sizeof (struct ar_cache);
+  struct ar_cache *cache;
+  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
 
-  struct ar_cache *new_cache = bfd_zalloc (arch_bfd, amt);
-  if (new_cache == NULL)
-    return FALSE;
-
-  new_cache->ptr = filepos;
-  new_cache->arelt = new_elt;
-  new_cache->next = NULL;
-  if (bfd_ardata (arch_bfd)->cache == NULL)
-    bfd_ardata (arch_bfd)->cache = new_cache;
-  else
+  /* If the hash table hasn't been created, create it.  */
+  if (hash_table == NULL)
     {
-      struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
-
-      while (current->next != NULL)
-       current = current->next;
-      current->next = new_cache;
+      hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
+                                     NULL, calloc, free);
+      if (hash_table == NULL)
+       return FALSE;
+      bfd_ardata (arch_bfd)->cache = hash_table;
     }
 
+  /* Insert new_elt into the hash table by filepos.  */
+  cache = bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
+  cache->ptr = filepos;
+  cache->arbfd = new_elt;
+  *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
+
   return TRUE;
 }
 \f
@@ -316,8 +334,7 @@ get_extended_arelt_filename (bfd *arch, const char *name)
    Presumes the file pointer is already in the right place (ie pointing
    to the ar_hdr in the file).   Moves the file pointer; on success it
    should be pointing to the front of the file contents; on failure it
-   could have been moved arbitrarily.
-*/
+   could have been moved arbitrarily.  */
 
 void *
 _bfd_generic_read_ar_hdr (bfd *abfd)
@@ -467,6 +484,12 @@ _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
   struct areltdata *new_areldata;
   bfd *n_nfd;
 
+  if (archive->my_archive)
+    {
+      filepos += archive->origin;
+      archive = archive->my_archive;
+    }
+
   n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos);
   if (n_nfd)
     return n_nfd;
@@ -549,10 +572,12 @@ bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
   else
     {
       unsigned int size = arelt_size (last_file);
+      filestart = last_file->origin + size;
+      if (archive->my_archive)
+       filestart -= archive->origin;
       /* Pad to an even boundary...
         Note that last_file->origin can be odd in the case of
         BSD-4.4-style element with a long odd size.  */
-      filestart = last_file->origin + size;
       filestart += filestart % 2;
     }
 
@@ -573,14 +598,9 @@ bfd_generic_archive_p (bfd *abfd)
       return NULL;
     }
 
-#ifdef GNU960
-  if (strncmp (armag, BFD_GNU960_ARMAG (abfd), SARMAG) != 0)
-    return 0;
-#else
   if (strncmp (armag, ARMAG, SARMAG) != 0 &&
       strncmp (armag, ARMAGB, SARMAG) != 0)
     return 0;
-#endif
 
   tdata_hold = bfd_ardata (abfd);
 
@@ -634,14 +654,6 @@ bfd_generic_archive_p (bfd *abfd)
          if (bfd_check_format (first, bfd_object)
              && first->xvec != abfd->xvec)
            {
-#if 0
-             /* We ought to close `first' here, but we can't, because
-                we have no way to remove it from the archive cache.
-                It's close to impossible to figure out when we can
-                release bfd_ardata.  FIXME.  */
-             bfd_close (first);
-             bfd_release (abfd, bfd_ardata (abfd));
-#endif
              bfd_set_error (bfd_error_wrong_object_format);
              bfd_ardata (abfd) = tdata_hold;
              return NULL;
@@ -670,7 +682,7 @@ bfd_generic_archive_p (bfd *abfd)
 /* The size of the string count.  */
 #define BSD_STRING_COUNT_SIZE 4
 
-/* Returns FALSE on error, TRUE otherwise */
+/* Returns FALSE on error, TRUE otherwise */
 
 static bfd_boolean
 do_slurp_bsd_armap (bfd *abfd)
@@ -776,7 +788,6 @@ do_slurp_coff_armap (bfd *abfd)
   nsymz = bfd_getb32 (int_buf);
   stringsize = parsed_size - (4 * nsymz) - 4;
 
-#if 1
   /* ... except that some archive formats are broken, and it may be our
      fault - the i960 little endian coff sometimes has big and sometimes
      little, because our tools changed.  Here's a horrible hack to clean
@@ -791,7 +802,6 @@ do_slurp_coff_armap (bfd *abfd)
       stringsize = parsed_size - (4 * nsymz) - 4;
       swap = bfd_getl32;
     }
-#endif
 
   /* The coff armap must be read sequentially.  So we construct a
      bsd-style one in core all at once, for simplicity.  */
@@ -901,8 +911,8 @@ bfd_slurp_armap (bfd *abfd)
   return TRUE;
 }
 \f
-/* Returns FALSE on error, TRUE otherwise */
-/* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
+/* Returns FALSE on error, TRUE otherwise */
+/* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
    header is in a slightly different order and the map name is '/'.
    This flavour is used by hp300hpux.  */
 
@@ -932,7 +942,7 @@ bfd_slurp_bsd_armap_f2 (bfd *abfd)
     return FALSE;
 
   if (!strncmp (nextname, "__.SYMDEF       ", 16)
-      || !strncmp (nextname, "__.SYMDEF/      ", 16)) /* old Linux archives */
+      || !strncmp (nextname, "__.SYMDEF/      ", 16)) /* Old Linux archives.  */
     return do_slurp_bsd_armap (abfd);
 
   if (strncmp (nextname, "/               ", 16))
@@ -1084,9 +1094,6 @@ _bfd_slurp_extended_name_table (bfd *abfd)
 
       /* FIXME, we can't release namedata here because it was allocated
         below extended_names on the objalloc...  */
-#if 0
-      bfd_release (abfd, namedata);
-#endif
     }
   return TRUE;
 }
@@ -1296,7 +1303,7 @@ _bfd_construct_extended_name_table (bfd *abfd,
   return TRUE;
 }
 \f
-/** A couple of functions for creating ar_hdrs */
+/* A couple of functions for creating ar_hdrs.  */
 
 #ifdef HPUX_LARGE_AR_IDS
 /* Function to encode large UID/GID values according to HP.  */
@@ -1646,11 +1653,7 @@ _bfd_write_archive_contents (bfd *arch)
 
       if (makemap && ! hasobjects)
        {                       /* Don't bother if we won't make a map!  */
-         if ((bfd_check_format (current, bfd_object))
-#if 0                          /* FIXME -- these are not set correctly */
-             && ((bfd_get_file_flags (current) & HAS_SYMS))
-#endif
-           )
+         if ((bfd_check_format (current, bfd_object)))
            hasobjects = TRUE;
        }
     }
@@ -1661,11 +1664,7 @@ _bfd_write_archive_contents (bfd *arch)
 
   if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
     return FALSE;
-#ifdef GNU960
-  wrote = bfd_bwrite (BFD_GNU960_ARMAG (arch), SARMAG, arch);
-#else
   wrote = bfd_bwrite (ARMAG, SARMAG, arch);
-#endif
   if (wrote != SARMAG)
     return FALSE;
 
@@ -1765,9 +1764,9 @@ _bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
   bfd *current;
   file_ptr elt_no = 0;
   struct orl *map = NULL;
-  unsigned int orl_max = 1024;         /* fine initial default */
+  unsigned int orl_max = 1024;         /* Fine initial default.  */
   unsigned int orl_count = 0;
-  int stridx = 0;              /* string index */
+  int stridx = 0;
   asymbol **syms = NULL;
   long syms_max = 0;
   bfd_boolean ret;
@@ -1916,7 +1915,7 @@ bsd_write_armap (bfd *arch,
   unsigned int mapsize = ranlibsize + stringsize + 8;
   file_ptr firstreal;
   bfd *current = arch->archive_head;
-  bfd *last_elt = current;     /* last element arch seen */
+  bfd *last_elt = current;     /* Last element arch seen.  */
   bfd_byte temp[4];
   unsigned int count;
   struct ar_hdr hdr;
@@ -1960,7 +1959,7 @@ bsd_write_armap (bfd *arch,
              current = current->next;
            }
          while (current != map[count].u.abfd);
-       }                       /* if new archive element */
+       }
 
       last_elt = current;
       H_PUT_32 (arch, map[count].namidx, buf);
@@ -2058,8 +2057,7 @@ _bfd_archive_bsd_update_armap_timestamp (bfd *arch)
    symbol name 0
    symbol name 1
 
-   symbol name n-1
-*/
+   symbol name n-1  */
 
 bfd_boolean
 coff_write_armap (bfd *arch,
@@ -2104,7 +2102,6 @@ coff_write_armap (bfd *arch,
       (((char *) (&hdr))[i]) = ' ';
 
   /* Write the ar header for this item and the number of symbols.  */
-
   if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
       != sizeof (struct ar_hdr))
     return FALSE;
This page took 0.027506 seconds and 4 git commands to generate.