include/coff
[binutils-gdb.git] / bfd / vms-lib.c
index c959df959672a3f7976f1da95b2c682ee09c43cf..813ea39eddc22ed65ee9caff8de6c6a63e09fabf 100644 (file)
@@ -833,7 +833,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;
@@ -951,11 +951,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)
@@ -969,7 +970,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)
@@ -1071,7 +1072,7 @@ vms_lib_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
             }
           if (buf != NULL)
             {
-              *(unsigned char *)buf = c;
+              *buf = c;
               buf++;
             }
           nbytes--;
@@ -1215,7 +1216,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;
@@ -1550,6 +1551,7 @@ get_idxlen (struct lib_index *idx, bfd_boolean is_elfidx)
 
 /* Write the index.  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.
    Return TRUE on success.  */
 
 static bfd_boolean
@@ -1569,8 +1571,8 @@ vms_write_index (bfd *abfd,
   } blk[MAX_LEVEL];
 
   /* The kbn blocks are used to store long symbol names.  */
-  unsigned int kbn_sz = 0;      /* Number of bytes availble in the kbn block.  */
-  unsigned int kbn_vbn = 0;     /* VBN of the kbn block.  */
+  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)
@@ -1590,7 +1592,7 @@ vms_write_index (bfd *abfd,
   /* 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;
@@ -1606,12 +1608,12 @@ vms_write_index (bfd *abfd,
 
       if (is_elfidx && idx->namlen >= MAX_KEYLEN)
         {
-          /* If the name is too long, write it in the kbn block.  */
+          /* 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 name in the kbn, chunk after chunk.  */
+          /* Write the key in the kbn, chunk after chunk.  */
           do
             {
               if (kbn_sz < sizeof (struct vms_kbn))
@@ -1697,7 +1699,7 @@ 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)++;
@@ -1716,7 +1718,8 @@ vms_write_index (bfd *abfd,
                   memcpy (rblk[j + 1]->keys + blk[j + 1].len,
                           rblk[j]->keys + blk[j].len,
                           blk[j].lastlen);
-                  /* Fix the entry (which in always the first field of an entry.  */
+                  /* Fix the entry (which in always the first field of an
+                    entry.  */
                   rfa = (struct vms_rfa *)(rblk[j + 1]->keys + blk[j + 1].len);
                   bfd_putl32 (blk[j].vbn, rfa->vbn);
                   bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
@@ -1805,23 +1808,23 @@ 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.  */
-          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;
-          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);
-        }
+      /* 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;
+      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 (vms_write_block (abfd, blk[j].vbn, rblk[j]) != TRUE)
@@ -1830,6 +1833,7 @@ vms_write_index (bfd *abfd,
       free (rblk[j]);
     }
 
+  /* Write the last kbn (if any).  */
   if (kbn_vbn != 0)
     {
       if (vms_write_block (abfd, kbn_vbn, kbn_blk) != TRUE)
@@ -2111,6 +2115,7 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
           /* 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)
             {