Copyright update for binutils
[deliverable/binutils-gdb.git] / bfd / vms-lib.c
index bbef19c7f4191a69d617aeb9b48b32df7905f9b9..863b915dfe1696a435ed8c52ff25bdfdffa0972d 100644 (file)
@@ -1,6 +1,6 @@
 /* BFD back-end for VMS archive files.
 
-   Copyright 2010 Free Software Foundation, Inc.
+   Copyright (C) 2010-2016 Free Software Foundation, Inc.
    Written by Tristan Gingold <gingold@adacore.com>, AdaCore.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -25,6 +25,7 @@
 #include "libbfd.h"
 #include "safe-ctype.h"
 #include "bfdver.h"
+#include "libiberty.h"
 #include "vms.h"
 #include "vms/lbr.h"
 #include "vms/dcx.h"
@@ -35,7 +36,8 @@
 #endif
 
 /* Maximum key length (which is also the maximum symbol length in archive).  */
-#define MAX_KEYLEN 129
+#define MAX_KEYLEN 128
+#define MAX_EKEYLEN 1024
 
 /* DCX Submaps.  */
 
@@ -77,6 +79,10 @@ struct lib_tdata
   /* Total size of the mhd (element header).  */
   unsigned int mhd_size;
 
+  /* Creation date.  */
+  unsigned int credat_lo;
+  unsigned int credat_hi;
+
   /* Vector of modules (archive elements), already sorted.  */
   unsigned int nbr_modules;
   struct carsym *modules;
@@ -186,14 +192,13 @@ vms_add_indexes_from_list (bfd *abfd, struct carsym_mem *cs, char *name,
     }
 }
 
-/* Read block VBN from ABFD and store it into BLK.  */
+/* Read block VBN from ABFD and store it into BLK.  Return FALSE in case of error.  */
 
 static bfd_boolean
 vms_read_block (bfd *abfd, unsigned int vbn, void *blk)
 {
   file_ptr off;
 
-  /* Read the index block.  */
   off = (vbn - 1) * VMS_BLOCK_SIZE;
   if (bfd_seek (abfd, off, SEEK_SET) != 0
       || bfd_bread (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
@@ -202,6 +207,21 @@ vms_read_block (bfd *abfd, unsigned int vbn, void *blk)
   return TRUE;
 }
 
+/* Write the content of BLK to block VBN of ABFD.  Return FALSE in case of error.  */
+
+static bfd_boolean
+vms_write_block (bfd *abfd, unsigned int vbn, void *blk)
+{
+  file_ptr off;
+
+  off = (vbn - 1) * VMS_BLOCK_SIZE;
+  if (bfd_seek (abfd, off, SEEK_SET) != 0
+      || bfd_bwrite (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
+    return FALSE;
+
+  return TRUE;
+}
+
 /* Read index block VBN and put the entry in **IDX (which is updated).
    If the entry is indirect, recurse.  */
 
@@ -259,6 +279,9 @@ vms_traverse_index (bfd *abfd, unsigned int vbn, struct carsym_mem *cs)
       if (idx_vbn == 0)
         return FALSE;
 
+      /* Point to the next index entry.  */
+      p = keyname + keylen;
+
       if (idx_off == RFADEF__C_INDEX)
         {
           /* Indirect entry.  Recurse.  */
@@ -350,9 +373,6 @@ vms_traverse_index (bfd *abfd, unsigned int vbn, struct carsym_mem *cs)
                 return FALSE;
             }
         }
-
-      /* Point to the next index entry.  */
-      p = keyname + keylen;
     }
 
   return TRUE;
@@ -423,6 +443,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
 {
   struct vms_lhd lhd;
   unsigned int sanity;
+  unsigned int majorid;
   struct lib_tdata *tdata_hold;
   struct lib_tdata *tdata;
   unsigned int dcxvbn;
@@ -445,13 +466,14 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
       bfd_set_error (bfd_error_wrong_format);
       return NULL;
     }
+  majorid = bfd_getl32 (lhd.majorid);
 
   /* Check archive kind.  */
   switch (kind)
     {
     case vms_lib_alpha:
       if ((lhd.type != LBR__C_TYP_EOBJ && lhd.type != LBR__C_TYP_ESHSTB)
-          || bfd_getl32 (lhd.majorid) != 3
+          || majorid != LBR_MAJORID
           || lhd.nindex != 2)
         {
           bfd_set_error (bfd_error_wrong_format);
@@ -460,7 +482,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
       break;
     case vms_lib_ia64:
       if ((lhd.type != LBR__C_TYP_IOBJ && lhd.type != LBR__C_TYP_ISHSTB)
-          || bfd_getl32 (lhd.majorid) != 6
+          || majorid != LBR_ELFMAJORID
           || lhd.nindex != 2)
         {
           bfd_set_error (bfd_error_wrong_format);
@@ -471,7 +493,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
       if ((lhd.type != LBR__C_TYP_TXT
            && lhd.type != LBR__C_TYP_MLB
            && lhd.type != LBR__C_TYP_HLP)
-          || bfd_getl32 (lhd.majorid) != 3
+          || majorid != LBR_MAJORID
           || lhd.nindex != 1)
         {
           bfd_set_error (bfd_error_wrong_format);
@@ -488,10 +510,12 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
   if (tdata == NULL)
     return NULL;
   abfd->tdata.any = (void *)tdata;
-  tdata->ver = bfd_getl32 (lhd.majorid);
+  tdata->ver = majorid;
   tdata->mhd_size = MHD__C_USRDAT + lhd.mhdusz;
   tdata->type = lhd.type;
   tdata->kind = kind;
+  tdata->credat_lo = bfd_getl32 (lhd.credat + 0);
+  tdata->credat_hi = bfd_getl32 (lhd.credat + 4);
 
   /* Read indexes.  */
   tdata->nbr_modules = bfd_getl32 (lhd.modcnt);
@@ -577,6 +601,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
           off = bfd_getl16 (sbm->next);
           if (off != 0)
             {
+              /* Read the 'next' array.  */
               sbmdesc->next = (unsigned short *)bfd_alloc
                 (abfd, sbm_len * sizeof (unsigned short));
               buf1 = data + off;
@@ -585,6 +610,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
             }
           else
             {
+              /* There is no next array if there is only one submap.  */
               BFD_ASSERT (tdata->nbr_dcxsbm == 1);
               sbmdesc->next = NULL;
             }
@@ -605,7 +631,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
 
  err:
   bfd_release (abfd, tdata);
-  abfd->tdata.any = (void *)tdata_hold;;
+  abfd->tdata.any = (void *)tdata_hold;
   return NULL;
 }
 
@@ -617,6 +643,14 @@ _bfd_vms_lib_alpha_archive_p (bfd *abfd)
   return _bfd_vms_lib_archive_p (abfd, vms_lib_alpha);
 }
 
+/* Standard function for ia64 libraries.  */
+
+const bfd_target *
+_bfd_vms_lib_ia64_archive_p (bfd *abfd)
+{
+  return _bfd_vms_lib_archive_p (abfd, vms_lib_ia64);
+}
+
 /* Standard function for text libraries.  */
 
 static const bfd_target *
@@ -627,8 +661,8 @@ _bfd_vms_lib_txt_archive_p (bfd *abfd)
 
 /* Standard bfd function.  */
 
-bfd_boolean
-_bfd_vms_lib_mkarchive (bfd *abfd)
+static bfd_boolean
+_bfd_vms_lib_mkarchive (bfd *abfd, enum vms_lib_kind kind)
 {
   struct lib_tdata *tdata;
 
@@ -637,9 +671,24 @@ _bfd_vms_lib_mkarchive (bfd *abfd)
     return FALSE;
 
   abfd->tdata.any = (void *)tdata;
-  tdata->ver = 3;
-  tdata->mhd_size = sizeof (struct vms_mhd);
-  tdata->type = LBR__C_TYP_EOBJ;
+  vms_get_time (&tdata->credat_hi, &tdata->credat_lo);
+
+  tdata->kind = kind;
+  switch (kind)
+    {
+    case vms_lib_alpha:
+      tdata->ver = LBR_MAJORID;
+      tdata->mhd_size = offsetof (struct vms_mhd, pad1);
+      tdata->type = LBR__C_TYP_EOBJ;
+      break;
+    case vms_lib_ia64:
+      tdata->ver = LBR_ELFMAJORID;
+      tdata->mhd_size = sizeof (struct vms_mhd);
+      tdata->type = LBR__C_TYP_IOBJ;
+      break;
+    default:
+      abort ();
+    }
 
   tdata->nbr_modules = 0;
   tdata->artdata.symdef_count = 0;
@@ -650,6 +699,18 @@ _bfd_vms_lib_mkarchive (bfd *abfd)
   return TRUE;
 }
 
+bfd_boolean
+_bfd_vms_lib_alpha_mkarchive (bfd *abfd)
+{
+  return _bfd_vms_lib_mkarchive (abfd, vms_lib_alpha);
+}
+
+bfd_boolean
+_bfd_vms_lib_ia64_mkarchive (bfd *abfd)
+{
+  return _bfd_vms_lib_mkarchive (abfd, vms_lib_ia64);
+}
+
 /* Find NAME in the symbol index.  Return the index.  */
 
 symindex
@@ -774,7 +835,7 @@ vms_lib_read_block (struct bfd *abfd)
    function does not handle records nor EOF.  */
 
 static file_ptr
-vms_lib_bread_raw (struct bfd *abfd, void *buf, file_ptr nbytes)
+vms_lib_bread_raw (struct bfd *abfd, unsigned char *buf, file_ptr nbytes)
 {
   struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
   file_ptr res;
@@ -892,11 +953,12 @@ vms_lib_dcx (struct vms_lib_iovec *vec, unsigned char *buf, file_ptr nbytes)
 /* Standard IOVEC function.  */
 
 static file_ptr
-vms_lib_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
+vms_lib_bread (struct bfd *abfd, void *vbuf, file_ptr nbytes)
 {
   struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
   file_ptr res;
   file_ptr chunk;
+  unsigned char *buf = (unsigned char *)vbuf;
 
   /* Do not read past the end.  */
   if (vec->where >= vec->file_len)
@@ -910,7 +972,7 @@ vms_lib_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
           unsigned char blen[2];
 
           /* Read record length.  */
-          if (vms_lib_bread_raw (abfd, &blen, sizeof (blen)) != sizeof (blen))
+          if (vms_lib_bread_raw (abfd, blen, sizeof (blen)) != sizeof (blen))
             return -1;
           vec->rec_len = bfd_getl16 (blen);
           if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
@@ -1012,7 +1074,7 @@ vms_lib_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
             }
           if (buf != NULL)
             {
-              *(unsigned char *)buf = c;
+              *buf = c;
               buf++;
             }
           nbytes--;
@@ -1136,11 +1198,13 @@ vms_lib_bstat (struct bfd *abfd ATTRIBUTE_UNUSED,
 
 static void *
 vms_lib_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
-             void *addr ATTRIBUTE_UNUSED,
-             bfd_size_type len ATTRIBUTE_UNUSED,
-             int prot ATTRIBUTE_UNUSED,
-             int flags ATTRIBUTE_UNUSED,
-             file_ptr offset ATTRIBUTE_UNUSED)
+               void *addr ATTRIBUTE_UNUSED,
+               bfd_size_type len ATTRIBUTE_UNUSED,
+               int prot ATTRIBUTE_UNUSED,
+               int flags ATTRIBUTE_UNUSED,
+               file_ptr offset ATTRIBUTE_UNUSED,
+               void **map_addr ATTRIBUTE_UNUSED,
+               bfd_size_type *map_len ATTRIBUTE_UNUSED)
 {
   return (void *) -1;
 }
@@ -1156,7 +1220,7 @@ static bfd_boolean
 vms_lib_bopen (bfd *el, file_ptr filepos)
 {
   struct vms_lib_iovec *vec;
-  char buf[256];
+  unsigned char buf[256];
   struct vms_mhd *mhd;
   struct lib_tdata *tdata = bfd_libdata (el->my_archive);
   unsigned int len;
@@ -1200,7 +1264,7 @@ vms_lib_bopen (bfd *el, file_ptr filepos)
   /* Check id.  */
   if (mhd->id != MHD__C_MHDID)
     return FALSE;
-  if (len >= sizeof (struct vms_mhd))
+  if (len >= MHD__C_MHDLEN + 1)
     el->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
   el->mtime = vms_rawtime_to_time_t (mhd->datim);
   el->mtime_set = TRUE;
@@ -1233,6 +1297,7 @@ _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx)
   struct lib_tdata *tdata = bfd_libdata (abfd);
   bfd *res;
   file_ptr file_off;
+  char *name;
 
   /* Sanity check.  */
   if (modidx >= tdata->nbr_modules)
@@ -1273,7 +1338,7 @@ _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx)
       res = _bfd_create_empty_archive_element_shell (abfd);
       if (res == NULL)
         return NULL;
-      arelt = bfd_zalloc (res, sizeof (*arelt));
+      arelt = bfd_zmalloc (sizeof (*arelt));
       if (arelt == NULL)
         return NULL;
       res->arelt_data = arelt;
@@ -1294,7 +1359,25 @@ _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx)
       res->origin = file_off + tdata->mhd_size;
     }
 
-  res->filename = tdata->modules[modidx].name;
+  /* Set filename.  */
+  name = tdata->modules[modidx].name;
+  switch (tdata->type)
+    {
+    case LBR__C_TYP_IOBJ:
+    case LBR__C_TYP_EOBJ:
+      /* For object archives, append .obj to mimic standard behaviour.  */
+      {
+       size_t namelen = strlen (name);
+       char *name1 = bfd_alloc (res, namelen + 4 + 1);
+       memcpy (name1, name, namelen);
+       strcpy (name1 + namelen, ".obj");
+       name = name1;
+      }
+      break;
+    default:
+      break;
+    }
+  res->filename = xstrdup (name);
 
   tdata->cache[modidx] = res;
 
@@ -1443,7 +1526,7 @@ _bfd_vms_lib_generic_stat_arch_elt (bfd *abfd, struct stat *st)
 
 /* Internal representation of an index entry.  */
 
-struct vms_index
+struct lib_index
 {
   /* Corresponding archive member.  */
   bfd *abfd;
@@ -1461,10 +1544,10 @@ struct vms_index
 /* Used to sort index entries.  */
 
 static int
-vms_index_cmp (const void *lv, const void *rv)
+lib_index_cmp (const void *lv, const void *rv)
 {
-  const struct vms_index *l = lv;
-  const struct vms_index *r = rv;
+  const struct lib_index *l = lv;
+  const struct lib_index *r = rv;
 
   return strcmp (l->name, r->name);
 }
@@ -1476,33 +1559,62 @@ vms_index_cmp (const void *lv, const void *rv)
 /* Get the size of an index entry.  */
 
 static unsigned int
-get_idxlen (struct vms_index *idx)
+get_idxlen (struct lib_index *idx, bfd_boolean is_elfidx)
 {
-  return 7 + idx->namlen;
+  if (is_elfidx)
+    {
+      /* 9 is the size of struct vms_elfidx without keyname.  */
+      if (idx->namlen > MAX_KEYLEN)
+        return 9 + sizeof (struct vms_kbn);
+      else
+        return 9 + idx->namlen;
+    }
+  else
+    {
+      /* 7 is the size of struct vms_idx without keyname.  */
+      return 7 + idx->namlen;
+    }
 }
 
-/* Write the index.  VBN is the first vbn to be used, and will contain
-   on return the last vbn.
+/* Write the index composed by NBR symbols contained in IDX.
+   VBN is the first vbn to be used, and will contain on return the last vbn.
+   Can be called with ABFD set to NULL just to size the index.
+   If not null, TOPVBN will be assigned to the vbn of the root index tree.
+   IS_ELFIDX is true for elfidx (ie ia64) indexes layout.
    Return TRUE on success.  */
 
 static bfd_boolean
 vms_write_index (bfd *abfd,
-                 struct vms_index *idx, unsigned int nbr, unsigned int *vbn,
-                 unsigned int *topvbn)
+                 struct lib_index *idx, unsigned int nbr, unsigned int *vbn,
+                 unsigned int *topvbn, bfd_boolean is_elfidx)
 {
+  /* The index is organized as a tree.  This function implements a naive
+     algorithm to balance the tree: it fills the leaves, and create a new
+     branch when all upper leaves and branches are full.  We only keep in
+     memory a path to the current leaf.  */
   unsigned int i;
   int j;
   int level;
+  /* Disk blocks for the current path.  */
   struct vms_indexdef *rblk[MAX_LEVEL];
+  /* Info on the current blocks.  */
   struct idxblk
   {
-    unsigned int vbn;
-    unsigned short len;
-    unsigned short lastlen;
+    unsigned int vbn;          /* VBN of the block.  */
+    /* The last entry is identified so that it could be copied to the
+       parent block.  */
+    unsigned short len;                /* Length up to the last entry.  */
+    unsigned short lastlen;    /* Length of the last entry.  */
   } blk[MAX_LEVEL];
 
+  /* The kbn blocks are used to store long symbol names.  */
+  unsigned int kbn_sz = 0;   /* Number of bytes available in the kbn block.  */
+  unsigned int kbn_vbn = 0;  /* VBN of the kbn block.  */
+  unsigned char *kbn_blk = NULL; /* Contents of the kbn block.  */
+
   if (nbr == 0)
     {
+      /* No entries.  Very easy to handle.  */
       if (topvbn != NULL)
         *topvbn = 0;
       return TRUE;
@@ -1511,28 +1623,106 @@ vms_write_index (bfd *abfd,
   if (abfd == NULL)
     {
       /* Sort the index the first time this function is called.  */
-      qsort (idx, nbr, sizeof (struct vms_index), vms_index_cmp);
+      qsort (idx, nbr, sizeof (struct lib_index), lib_index_cmp);
     }
 
   /* Allocate first index block.  */
   level = 1;
   if (abfd != NULL)
-    rblk[0] = bfd_malloc (sizeof (struct vms_indexdef));
+    rblk[0] = bfd_zmalloc (sizeof (struct vms_indexdef));
   blk[0].vbn = (*vbn)++;
   blk[0].len = 0;
   blk[0].lastlen = 0;
 
   for (i = 0; i < nbr; i++, idx++)
     {
-      unsigned int idxlen = get_idxlen (idx);
-      struct vms_idx *en;
+      unsigned int idxlen;
       int flush = 0;
+      unsigned int key_vbn = 0;
+      unsigned int key_off = 0;
+
+      idxlen = get_idxlen (idx, is_elfidx);
+
+      if (is_elfidx && idx->namlen > MAX_KEYLEN)
+        {
+          /* If the key (ie name) is too long, write it in the kbn block.  */
+          unsigned int kl = idx->namlen;
+          unsigned int kl_chunk;
+          const char *key = idx->name;
+
+          /* Write the key in the kbn, chunk after chunk.  */
+          do
+            {
+              if (kbn_sz < sizeof (struct vms_kbn))
+                {
+                  /* Not enough room in the kbn block.  */
+                  if (abfd != NULL)
+                    {
+                      /* Write it to the disk (if there is one).  */
+                      if (kbn_vbn != 0)
+                        {
+                          if (vms_write_block (abfd, kbn_vbn, kbn_blk) != TRUE)
+                            return FALSE;
+                        }
+                      else
+                        {
+                          kbn_blk = bfd_malloc (VMS_BLOCK_SIZE);
+                          if (kbn_blk == NULL)
+                            return FALSE;
+                        }
+                      *(unsigned short *)kbn_blk = 0;
+                    }
+                  /* Allocate a new block for the keys.  */
+                  kbn_vbn = (*vbn)++;
+                  kbn_sz = VMS_BLOCK_SIZE - 2;
+                }
+              /* Size of the chunk written to the current key block.  */
+              if (kl + sizeof (struct vms_kbn) > kbn_sz)
+                kl_chunk = kbn_sz - sizeof (struct vms_kbn);
+              else
+                kl_chunk = kl;
+
+              if (kbn_blk != NULL)
+                {
+                  struct vms_kbn *kbn;
+
+                  kbn = (struct vms_kbn *)(kbn_blk + VMS_BLOCK_SIZE - kbn_sz);
+
+                  if (key_vbn == 0)
+                    {
+                      /* Save the rfa of the first chunk.  */
+                      key_vbn = kbn_vbn;
+                      key_off = VMS_BLOCK_SIZE - kbn_sz;
+                    }
+
+                  bfd_putl16 (kl_chunk, kbn->keylen);
+                  if (kl_chunk == kl)
+                    {
+                      /* No next chunk.  */
+                      bfd_putl32 (0, kbn->rfa.vbn);
+                      bfd_putl16 (0, kbn->rfa.offset);
+                    }
+                  else
+                    {
+                      /* Next chunk will be at the start of the next block.  */
+                      bfd_putl32 (*vbn, kbn->rfa.vbn);
+                      bfd_putl16 (2, kbn->rfa.offset);
+                    }
+                  memcpy ((char *)(kbn + 1), key, kl_chunk);
+                  key += kl_chunk;
+                }
+              kl -= kl_chunk;
+              kl_chunk = (kl_chunk + 1) & ~1;    /* Always align.  */
+              kbn_sz -= kl_chunk + sizeof (struct vms_kbn);
+            }
+          while (kl > 0);
+        }
 
       /* Check if a block might overflow.  In this case we will flush this
          block and all the blocks below it.  */
       for (j = 0; j < level; j++)
         if (blk[j].len + blk[j].lastlen + idxlen > INDEXDEF__BLKSIZ)
-          flush = j + 1;
+         flush = j + 1;
 
       for (j = 0; j < level; j++)
         {
@@ -1548,25 +1738,32 @@ vms_write_index (bfd *abfd,
                   /* Need to create a parent.  */
                   if (abfd != NULL)
                     {
-                      rblk[level] = bfd_malloc (sizeof (struct vms_indexdef));
+                      rblk[level] = bfd_zmalloc (sizeof (struct vms_indexdef));
                       bfd_putl32 (*vbn, rblk[j]->parent);
                     }
                   blk[level].vbn = (*vbn)++;
                   blk[level].len = 0;
-                  blk[level].lastlen = 0;
+                  blk[level].lastlen = blk[j].lastlen;
 
                   level++;
                 }
 
-              /* Update parent block: write the new entry.  */
+              /* Update parent block: write the last entry from the current
+                block.  */
               if (abfd != NULL)
                 {
-                  en = (struct vms_idx *)(rblk[j]->keys + blk[j].len);
-                  memcpy (rblk[j + 1]->keys + blk[j + 1].len, en,
-                          blk[j].lastlen);
-                  en = (struct vms_idx *)(rblk[j + 1]->keys + blk[j + 1].len);
-                  bfd_putl32 (blk[j].vbn, en->rfa.vbn);
-                  bfd_putl16 (RFADEF__C_INDEX, en->rfa.offset);
+                  struct vms_rfa *rfa;
+
+                 /* Pointer to the last entry in parent block.  */
+                 rfa = (struct vms_rfa *)(rblk[j + 1]->keys + blk[j + 1].len);
+
+                  /* Copy the whole entry.  */
+                 BFD_ASSERT (blk[j + 1].lastlen == blk[j].lastlen);
+                  memcpy (rfa, rblk[j]->keys + blk[j].len, blk[j].lastlen);
+                  /* Fix the entry (which in always the first field of an
+                    entry.  */
+                  bfd_putl32 (blk[j].vbn, rfa->vbn);
+                  bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
                 }
 
               if (j + 1 == flush)
@@ -1574,7 +1771,7 @@ vms_write_index (bfd *abfd,
                   /* And allocate it.  Do it only on the block that won't be
                      flushed (so that the parent of the parent can be
                      updated too).  */
-                  blk[j + 1].len += blk[j].lastlen;
+                  blk[j + 1].len += blk[j + 1].lastlen;
                   blk[j + 1].lastlen = 0;
                 }
 
@@ -1582,11 +1779,7 @@ vms_write_index (bfd *abfd,
               if (abfd != NULL)
                 {
                   bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
-                  if (bfd_seek (abfd, (blk[j].vbn - 1) * VMS_BLOCK_SIZE,
-                                SEEK_SET) != 0)
-                    return FALSE;
-                  if (bfd_bwrite (rblk[j], sizeof (struct vms_indexdef), abfd)
-                      != sizeof (struct vms_indexdef))
+                  if (vms_write_block (abfd, blk[j].vbn, rblk[j]) != TRUE)
                     return FALSE;
                 }
 
@@ -1599,25 +1792,59 @@ vms_write_index (bfd *abfd,
           /* Append it to the block.  */
           if (j == 0)
             {
+             /* Keep the previous last entry.  */
               blk[j].len += blk[j].lastlen;
 
               if (abfd != NULL)
                 {
-                  en = (struct vms_idx *)(rblk[j]->keys + blk[j].len);
+                  struct vms_rfa *rfa;
+
+                  rfa = (struct vms_rfa *)(rblk[j]->keys + blk[j].len);
                   bfd_putl32 ((idx->abfd->proxy_origin / VMS_BLOCK_SIZE) + 1,
-                              en->rfa.vbn);
+                              rfa->vbn);
                   bfd_putl16
-                    ((idx->abfd->proxy_origin % VMS_BLOCK_SIZE) + DATA__DATA,
-                     en->rfa.offset);
-                  en->keylen = idx->namlen;
-                  memcpy (en->keyname, idx->name, idx->namlen);
-                }
-            }
+                    ((idx->abfd->proxy_origin % VMS_BLOCK_SIZE)
+                     + (is_elfidx ? 0 : DATA__DATA),
+                     rfa->offset);
 
-          blk[j].lastlen = idxlen;
+                  if (is_elfidx)
+                    {
+                      /* Use elfidx format.  */
+                      struct vms_elfidx *en = (struct vms_elfidx *)rfa;
+
+                      en->flags = 0;
+                      if (key_vbn != 0)
+                        {
+                          /* Long symbol name.  */
+                          struct vms_kbn *k = (struct vms_kbn *)(en->keyname);
+                          bfd_putl16 (sizeof (struct vms_kbn), en->keylen);
+                          bfd_putl16 (idx->namlen, k->keylen);
+                          bfd_putl32 (key_vbn, k->rfa.vbn);
+                          bfd_putl16 (key_off, k->rfa.offset);
+                          en->flags |= ELFIDX__SYMESC;
+                        }
+                      else
+                        {
+                          bfd_putl16 (idx->namlen, en->keylen);
+                          memcpy (en->keyname, idx->name, idx->namlen);
+                        }
+                    }
+                  else
+                    {
+                      /* Use idx format.  */
+                      struct vms_idx *en = (struct vms_idx *)rfa;
+                      en->keylen = idx->namlen;
+                      memcpy (en->keyname, idx->name, idx->namlen);
+                    }
+                }
+           }
+         /* The last added key can now be the last one all blocks in the
+            path.  */
+         blk[j].lastlen = idxlen;
         }
     }
 
+  /* Save VBN of the root.  */
   if (topvbn != NULL)
     *topvbn = blk[level - 1].vbn;
 
@@ -1625,33 +1852,40 @@ vms_write_index (bfd *abfd,
     return TRUE;
 
   /* Flush.  */
-  for (j = 0; j < level; j++)
+  for (j = 1; j < level; j++)
     {
-      if (j > 0)
-        {
-          /* Update parent block: write the new entry.  */
-          struct vms_idx *en;
-          struct vms_idx *par;
-
-          en = (struct vms_idx *)(rblk[j - 1]->keys + blk[j - 1].len);
-          par = (struct vms_idx *)(rblk[j]->keys + blk[j].len);
-          memcpy (par, en, blk[j - 1].lastlen);
-          bfd_putl32 (blk[j - 1].vbn, par->rfa.vbn);
-          bfd_putl16 (RFADEF__C_INDEX, par->rfa.offset);
-        }
+      /* Update parent block: write the new entry.  */
+      unsigned char *en;
+      unsigned char *par;
+      struct vms_rfa *rfa;
+
+      en = rblk[j - 1]->keys + blk[j - 1].len;
+      par = rblk[j]->keys + blk[j].len;
+      BFD_ASSERT (blk[j].lastlen == blk[j - 1].lastlen);
+      memcpy (par, en, blk[j - 1].lastlen);
+      rfa = (struct vms_rfa *)par;
+      bfd_putl32 (blk[j - 1].vbn, rfa->vbn);
+      bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
+    }
 
+  for (j = 0; j < level; j++)
+    {
       /* Write this block on the disk.  */
       bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
-      if (bfd_seek (abfd, (blk[j].vbn - 1) * VMS_BLOCK_SIZE,
-                    SEEK_SET) != 0)
-        return FALSE;
-      if (bfd_bwrite (rblk[j], sizeof (struct vms_indexdef), abfd)
-          != sizeof (struct vms_indexdef))
+      if (vms_write_block (abfd, blk[j].vbn, rblk[j]) != TRUE)
         return FALSE;
 
       free (rblk[j]);
     }
 
+  /* Write the last kbn (if any).  */
+  if (kbn_vbn != 0)
+    {
+      if (vms_write_block (abfd, kbn_vbn, kbn_blk) != TRUE)
+        return FALSE;
+      free (kbn_blk);
+    }
+
   return TRUE;
 }
 
@@ -1696,18 +1930,18 @@ vms_write_data_block (bfd *arch, struct vms_datadef *data, file_ptr *off,
 
 static bfd_boolean
 _bfd_vms_lib_build_map (unsigned int nbr_modules,
-                        struct vms_index *modules,
+                        struct lib_index *modules,
                         unsigned int *res_cnt,
-                        struct vms_index **res)
+                        struct lib_index **res)
 {
   unsigned int i;
   asymbol **syms = NULL;
   long syms_max = 0;
-  struct vms_index *map = NULL;
+  struct lib_index *map = NULL;
   unsigned int map_max = 1024;         /* Fine initial default.  */
   unsigned int map_count = 0;
 
-  map = (struct vms_index *) bfd_malloc (map_max * sizeof (struct vms_index));
+  map = (struct lib_index *) bfd_malloc (map_max * sizeof (struct lib_index));
   if (map == NULL)
     goto error_return;
 
@@ -1754,21 +1988,20 @@ _bfd_vms_lib_build_map (unsigned int nbr_modules,
                    || bfd_is_com_section (sec))
                   && ! bfd_is_und_section (sec))
                 {
-                  struct vms_index *new_map;
+                  struct lib_index *new_map;
 
                   /* This symbol will go into the archive header.  */
                   if (map_count == map_max)
                     {
                       map_max *= 2;
-                      new_map = (struct vms_index *)
-                        bfd_realloc (map, map_max * sizeof (struct vms_index));
+                      new_map = (struct lib_index *)
+                        bfd_realloc (map, map_max * sizeof (struct lib_index));
                       if (new_map == NULL)
                         goto error_return;
                       map = new_map;
                     }
 
                   map[map_count].abfd = current;
-                  /* FIXME: check length.  */
                   map[map_count].namlen = strlen (syms[src_count]->name);
                   map[map_count].name = syms[src_count]->name;
                   map_count++;
@@ -1797,9 +2030,9 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
 {
   bfd *current;
   unsigned int nbr_modules;
-  struct vms_index *modules;
+  struct lib_index *modules;
   unsigned int nbr_symbols;
-  struct vms_index *symbols;
+  struct lib_index *symbols;
   struct lib_tdata *tdata = bfd_libdata (arch);
   unsigned int i;
   file_ptr off;
@@ -1808,6 +2041,8 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
   unsigned int vbn;
   unsigned int mod_idx_vbn;
   unsigned int sym_idx_vbn;
+  bfd_boolean is_elfidx = tdata->kind == vms_lib_ia64;
+  unsigned int max_keylen = is_elfidx ? MAX_EKEYLEN : MAX_KEYLEN;
 
   /* Count the number of modules (and do a first sanity check).  */
   nbr_modules = 0;
@@ -1831,7 +2066,7 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
 
   /* Build the modules list.  */
   BFD_ASSERT (tdata->modules == NULL);
-  modules = bfd_alloc (arch, nbr_modules * sizeof (struct vms_index));
+  modules = bfd_alloc (arch, nbr_modules * sizeof (struct lib_index));
   if (modules == NULL)
     return FALSE;
 
@@ -1839,7 +2074,7 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
        current != NULL;
        current = current->archive_next, i++)
     {
-      int nl;
+      unsigned int nl;
 
       modules[i].abfd = current;
       modules[i].name = vms_get_module_name (current->filename, FALSE);
@@ -1847,12 +2082,12 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
 
       /* FIXME: silently truncate long names ?  */
       nl = strlen (modules[i].name);
-      modules[i].namlen = (nl > MAX_KEYLEN ? MAX_KEYLEN : nl);
+      modules[i].namlen = (nl > max_keylen ? max_keylen : nl);
     }
 
   /* Create the module index.  */
   vbn = 0;
-  if (!vms_write_index (NULL, modules, nbr_modules, &vbn, NULL))
+  if (!vms_write_index (NULL, modules, nbr_modules, &vbn, NULL, is_elfidx))
     return FALSE;
   nbr_mod_iblk = vbn;
 
@@ -1861,7 +2096,7 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
     return FALSE;
 
   vbn = 0;
-  if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL))
+  if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL, is_elfidx))
     return FALSE;
   nbr_sym_iblk = vbn;
 
@@ -1881,8 +2116,15 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
       current = modules[i].abfd;
       current->proxy_origin = off;
 
-      bfd_putl16 (sizeof (struct vms_mhd), blk);
-      mhd = (struct vms_mhd *)(blk + 2);
+      if (is_elfidx)
+        sz = 0;
+      else
+        {
+          /* Write the MHD as a record (ie, size first).  */
+          sz = 2;
+          bfd_putl16 (tdata->mhd_size, blk);
+        }
+      mhd = (struct vms_mhd *)(blk + sz);
       memset (mhd, 0, sizeof (struct vms_mhd));
       mhd->lbrflag = 0;
       mhd->id = MHD__C_MHDID;
@@ -1891,31 +2133,100 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
       bfd_putl32 (modules[i].ref, mhd->refcnt);
       /* FIXME: datim.  */
 
-      sz = (2 + sizeof (struct vms_mhd) + 1) & ~1;
-      if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
-        goto input_err;
+      sz += tdata->mhd_size;
+      sz = (sz + 1) & ~1;
 
+      /* Rewind the member to be put into the archive.  */
       if (bfd_seek (current, 0, SEEK_SET) != 0)
         goto input_err;
 
-      while (1)
-       {
-          sz = bfd_bread (blk, sizeof (blk), current);
-          if (sz == 0)
-            break;
+      /* Copy the member into the archive.  */
+      if (is_elfidx)
+        {
+          unsigned int modsize = 0;
+          bfd_size_type amt;
+          file_ptr off_hdr = off;
+
+          /* Read to complete the first block.  */
+          amt = bfd_bread (blk + sz, VMS_BLOCK_SIZE - sz, current);
+          if (amt == (bfd_size_type)-1)
+            goto input_err;
+          modsize = amt;
+          if (amt < VMS_BLOCK_SIZE - sz)
+            {
+              /* The member size is less than a block.  Pad the block.  */
+              memset (blk + sz + amt, 0, VMS_BLOCK_SIZE - sz - amt);
+            }
+          bfd_putl32 (modsize, mhd->modsize);
+
+          /* Write the first block (which contains an mhd).  */
+          if (bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
+            goto input_err;
+          off += VMS_BLOCK_SIZE;
+
+          if (amt == VMS_BLOCK_SIZE - sz)
+            {
+              /* Copy the remaining.  */
+              char buffer[DEFAULT_BUFFERSIZE];
+
+              while (1)
+                {
+                  amt = bfd_bread (buffer, sizeof (buffer), current);
+                  if (amt == (bfd_size_type)-1)
+                    goto input_err;
+                  if (amt == 0)
+                    break;
+                  modsize += amt;
+                  if (amt != sizeof (buffer))
+                    {
+                      /* Clear the padding.  */
+                      memset (buffer + amt, 0, sizeof (buffer) - amt);
+                      amt = (amt + VMS_BLOCK_SIZE) & ~(VMS_BLOCK_SIZE - 1);
+                    }
+                  if (bfd_bwrite (buffer, amt, arch) != amt)
+                    goto input_err;
+                  off += amt;
+                }
+
+              /* Now that the size is known, write the first block (again).  */
+              bfd_putl32 (modsize, mhd->modsize);
+              if (bfd_seek (arch, off_hdr, SEEK_SET) != 0
+                  || bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
+                goto input_err;
+              if (bfd_seek (arch, off, SEEK_SET) != 0)
+                goto input_err;
+            }
+        }
+      else
+        {
+          /* Write the MHD.  */
           if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
             goto input_err;
+
+          /* Write the member.  */
+          while (1)
+            {
+              sz = bfd_bread (blk, sizeof (blk), current);
+              if (sz == 0)
+                break;
+              if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
+                goto input_err;
+            }
+
+          /* Write the end of module marker.  */
+          if (vms_write_data_block (arch, &data, &off,
+                                    eotdesc, sizeof (eotdesc), 1) < 0)
+            goto input_err;
         }
-      if (vms_write_data_block (arch, &data, &off,
-                                eotdesc, sizeof (eotdesc), 1) < 0)
-        goto input_err;
     }
 
   /* Write the indexes.  */
   vbn = 2;
-  if (vms_write_index (arch, modules, nbr_modules, &vbn, &mod_idx_vbn) != TRUE)
+  if (vms_write_index (arch, modules, nbr_modules, &vbn, &mod_idx_vbn,
+                       is_elfidx) != TRUE)
     return FALSE;
-  if (vms_write_index (arch, symbols, nbr_symbols, &vbn, &sym_idx_vbn) != TRUE)
+  if (vms_write_index (arch, symbols, nbr_symbols, &vbn, &sym_idx_vbn,
+                       is_elfidx) != TRUE)
     return FALSE;
 
   /* Write libary header.  */
@@ -1924,13 +2235,25 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
     struct vms_lhd *lhd = (struct vms_lhd *)blk;
     struct vms_idd *idd = (struct vms_idd *)(blk + sizeof (*lhd));
     unsigned int idd_flags;
+    unsigned int saneid;
 
     memset (blk, 0, sizeof (blk));
 
-    lhd->type = LBR__C_TYP_EOBJ;
+    lhd->type = tdata->type;
     lhd->nindex = 2;
-    bfd_putl32 (LHD_SANEID3, lhd->sanity);
-    bfd_putl16 (3, lhd->majorid);
+    switch (tdata->kind)
+      {
+      case vms_lib_alpha:
+        saneid = LHD_SANEID3;
+        break;
+      case vms_lib_ia64:
+        saneid = LHD_SANEID6;
+        break;
+      default:
+        abort ();
+      }
+    bfd_putl32 (saneid, lhd->sanity);
+    bfd_putl16 (tdata->ver, lhd->majorid);
     bfd_putl16 (0, lhd->minorid);
     snprintf ((char *)lhd->lbrver + 1, sizeof (lhd->lbrver) - 1,
               "GNU ar %u.%u.%u",
@@ -1940,36 +2263,41 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
     lhd->lbrver[sizeof (lhd->lbrver) - 1] = 0;
     lhd->lbrver[0] = strlen ((char *)lhd->lbrver + 1);
 
-    /* FIXME.  */
-    bfd_putl64 (0, lhd->credat);
-    bfd_putl64 (0, lhd->updtim);
+    bfd_putl32 (tdata->credat_lo, lhd->credat + 0);
+    bfd_putl32 (tdata->credat_hi, lhd->credat + 4);
+    vms_raw_get_time (lhd->updtim);
 
-    lhd->mhdusz = sizeof (struct vms_mhd) - MHD__C_USRDAT;
+    lhd->mhdusz = tdata->mhd_size - MHD__C_USRDAT;
 
     bfd_putl32 (nbr_modules + nbr_symbols, lhd->idxcnt);
     bfd_putl32 (nbr_modules, lhd->modcnt);
     bfd_putl32 (nbr_modules, lhd->modhdrs);
 
+    /* Number of blocks for index.  */
+    bfd_putl32 (nbr_mod_iblk + nbr_sym_iblk, lhd->idxblks);
     bfd_putl32 (vbn - 1, lhd->hipreal);
     bfd_putl32 (vbn - 1, lhd->hiprusd);
 
+    /* VBN of the next free block.  */
+    bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextvbn);
+    bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextrfa + 0);
+    bfd_putl16 (0, lhd->nextrfa + 4);
+
     /* First index (modules name).  */
     idd_flags = IDD__FLAGS_ASCII | IDD__FLAGS_VARLENIDX
       | IDD__FLAGS_NOCASECMP | IDD__FLAGS_NOCASENTR;
     bfd_putl16 (idd_flags, idd->flags);
-    bfd_putl16 (MAX_KEYLEN, idd->keylen);
+    bfd_putl16 (max_keylen + 1, idd->keylen);
     bfd_putl16 (mod_idx_vbn, idd->vbn);
     idd++;
 
     /* Second index (symbols name).  */
     bfd_putl16 (idd_flags, idd->flags);
-    bfd_putl16 (MAX_KEYLEN, idd->keylen);
+    bfd_putl16 (max_keylen + 1, idd->keylen);
     bfd_putl16 (sym_idx_vbn, idd->vbn);
     idd++;
 
-    if (bfd_seek (arch, 0, SEEK_SET) != 0)
-      return FALSE;
-    if (bfd_bwrite (blk, sizeof (blk), arch) != sizeof (blk))
+    if (vms_write_block (arch, 1, blk) != TRUE)
       return FALSE;
   }
 
@@ -1983,7 +2311,7 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
 /* Add a target for text library.  This costs almost nothing and is useful to
    read VMS library on the host.  */
 
-const bfd_target vms_lib_txt_vec =
+const bfd_target alpha_vms_lib_txt_vec =
 {
   "vms-libtxt",                        /* Name.  */
   bfd_target_unknown_flavour,
@@ -1994,6 +2322,7 @@ const bfd_target vms_lib_txt_vec =
   0,                           /* symbol_leading_char.  */
   ' ',                         /* ar_pad_char.  */
   15,                          /* ar_max_namelen.  */
+  0,                           /* match priority.  */
   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
   bfd_getl16, bfd_getl_signed_16, bfd_putl16,
@@ -2018,5 +2347,5 @@ const bfd_target vms_lib_txt_vec =
 
   NULL,
 
-  (PTR) 0
+  NULL
 };
This page took 0.037221 seconds and 4 git commands to generate.