]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/vms-alpha.c
[gdb/build] Fix missing implicit constructor call with gcc 4.8
[thirdparty/binutils-gdb.git] / bfd / vms-alpha.c
index 8f22a31ae5d4470100e5e5d0302d952e93d590af..5bf32d67803e38830d321a812b37ab9578c6999f 100644 (file)
@@ -1,5 +1,5 @@
 /* vms.c -- BFD back-end for EVAX (openVMS/Alpha) files.
-   Copyright (C) 1996-2018 Free Software Foundation, Inc.
+   Copyright (C) 1996-2020 Free Software Foundation, Inc.
 
    Initial version written by Klaus Kaempf (kkaempf@rmi.de)
    Major rewrite by Adacore.
@@ -34,6 +34,7 @@
 */
 
 #include "sysdep.h"
+#include <limits.h>
 #include "bfd.h"
 #include "bfdlink.h"
 #include "libbfd.h"
@@ -71,6 +72,9 @@
 \f
 
 #define MIN(a,b) ((a) < (b) ? (a) : (b))
+#ifndef CHAR_BIT
+#define CHAR_BIT 8
+#endif
 
 /* The r_type field in a reloc is one of the following values.  */
 #define ALPHA_R_IGNORE         0
@@ -368,14 +372,16 @@ struct vms_private_data_struct *bfd_vms_get_data (bfd *);
 
 static int vms_get_remaining_object_record (bfd *, unsigned int);
 static bfd_boolean _bfd_vms_slurp_object_records (bfd * abfd);
-static void alpha_vms_add_fixup_lp (struct bfd_link_info *, bfd *, bfd *);
-static void alpha_vms_add_fixup_ca (struct bfd_link_info *, bfd *, bfd *);
-static void alpha_vms_add_fixup_qr (struct bfd_link_info *, bfd *, bfd *,
-                                   bfd_vma);
-static void alpha_vms_add_fixup_lr (struct bfd_link_info *, unsigned int,
-                                   bfd_vma);
-static void alpha_vms_add_lw_reloc (struct bfd_link_info *);
-static void alpha_vms_add_qw_reloc (struct bfd_link_info *);
+static bfd_boolean alpha_vms_add_fixup_lp (struct bfd_link_info *,
+                                          bfd *, bfd *);
+static bfd_boolean alpha_vms_add_fixup_ca (struct bfd_link_info *,
+                                          bfd *, bfd *);
+static bfd_boolean alpha_vms_add_fixup_qr (struct bfd_link_info *,
+                                          bfd *, bfd *, bfd_vma);
+static bfd_boolean alpha_vms_add_fixup_lr (struct bfd_link_info *,
+                                          unsigned int, bfd_vma);
+static bfd_boolean alpha_vms_add_lw_reloc (struct bfd_link_info *);
+static bfd_boolean alpha_vms_add_qw_reloc (struct bfd_link_info *);
 
 struct vector_type
 {
@@ -401,17 +407,12 @@ struct vector_type
 
 /* Be sure there is room for a new element.  */
 
-static void vector_grow1 (struct vector_type *vec, size_t elsz);
+static void *vector_grow1 (struct vector_type *vec, size_t elsz);
 
 /* Allocate room for a new element and return its address.  */
 
 #define VEC_APPEND(VEC, TYPE)                                  \
-  (vector_grow1 (&VEC, sizeof (TYPE)), &VEC_EL(VEC, TYPE, (VEC).nbr_el++))
-
-/* Append an element.  */
-
-#define VEC_APPEND_EL(VEC, TYPE, EL)           \
-  (*(VEC_APPEND (VEC, TYPE)) = EL)
+  ((TYPE *) vector_grow1 (&VEC, sizeof (TYPE)))
 
 struct alpha_vms_vma_ref
 {
@@ -503,6 +504,7 @@ _bfd_vms_slurp_eihd (bfd *abfd, unsigned int *eisd_offset,
   vms_debug2 ((4, "EIHD size %d imgtype %d symvva 0x%lx eisd %d eihs %d\n",
               size, imgtype, (unsigned long)symvva,
               *eisd_offset, *eihs_offset));
+  (void) size;
 
   return TRUE;
 }
@@ -529,12 +531,12 @@ _bfd_vms_slurp_eisd (bfd *abfd, unsigned int offset)
       asection *section;
       flagword bfd_flags;
 
-      /* PR 17512: file: 3d9e9fe9.
-        12 is the offset of the eisdsize field from the start of the record (8)
-        plus the size of the eisdsize field (4).  */
-      if (offset >= PRIV (recrd.rec_size) - 12)
+      /* PR 17512: file: 3d9e9fe9.  */
+      if (offset > PRIV (recrd.rec_size)
+         || (PRIV (recrd.rec_size) - offset
+             < offsetof (struct vms_eisd, eisdsize) + 4))
        return FALSE;
-      eisd = (struct vms_eisd *)(PRIV (recrd.rec) + offset);
+      eisd = (struct vms_eisd *) (PRIV (recrd.rec) + offset);
       rec_size = bfd_getl32 (eisd->eisdsize);
       if (rec_size == 0)
        break;
@@ -547,11 +549,10 @@ _bfd_vms_slurp_eisd (bfd *abfd, unsigned int offset)
        }
 
       /* Make sure that there is enough data present in the record.  */
-      /* FIXME: Should we use sizeof (struct vms_eisd) rather than just 32 here ?  */
-      if (rec_size < 32)
+      if (rec_size < offsetof (struct vms_eisd, type) + 1)
        return FALSE;
       /* Make sure that the record is not too big either.  */
-      if (offset + rec_size >= PRIV (recrd.rec_size))
+      if (rec_size > PRIV (recrd.rec_size) - offset)
        return FALSE;
 
       offset += rec_size;
@@ -592,13 +593,16 @@ _bfd_vms_slurp_eisd (bfd *abfd, unsigned int offset)
 
       if (flags & EISD__M_GBL)
        {
-         if (rec_size < offsetof (struct vms_eisd, gblnam))
+         if (rec_size <= offsetof (struct vms_eisd, gblnam))
            return FALSE;
          else if (rec_size < sizeof (struct vms_eisd))
-           name = _bfd_vms_save_counted_string (eisd->gblnam,
+           name = _bfd_vms_save_counted_string (abfd, eisd->gblnam,
                                                 rec_size - offsetof (struct vms_eisd, gblnam));
          else
-           name = _bfd_vms_save_counted_string (eisd->gblnam, EISD__K_GBLNAMLEN);
+           name = _bfd_vms_save_counted_string (abfd, eisd->gblnam,
+                                                EISD__K_GBLNAMLEN);
+         if (name == NULL || name[0] == 0)
+           return FALSE;
          bfd_flags |= SEC_COFF_SHARED_LIBRARY;
          bfd_flags &= ~(SEC_ALLOC | SEC_LOAD);
        }
@@ -610,7 +614,9 @@ _bfd_vms_slurp_eisd (bfd *abfd, unsigned int offset)
        {
          const char *pfx;
 
-         name = (char*) bfd_alloc (abfd, 32);
+         name = (char *) bfd_alloc (abfd, 32);
+         if (name == NULL)
+           return FALSE;
          if (flags & EISD__M_DZRO)
            pfx = "BSS";
          else if (flags & EISD__M_EXE)
@@ -632,7 +638,7 @@ _bfd_vms_slurp_eisd (bfd *abfd, unsigned int offset)
       section->size = size;
       section->vma = vaddr;
 
-      if (!bfd_set_section_flags (abfd, section, bfd_flags))
+      if (!bfd_set_section_flags (section, bfd_flags))
        return FALSE;
     }
 
@@ -687,7 +693,7 @@ _bfd_vms_slurp_eihs (bfd *abfd, unsigned int offset)
       section->size = dstsize;
       section->filepos = VMS_BLOCK_SIZE * (dstvbn - 1);
 
-      if (!bfd_set_section_flags (abfd, section, bfd_flags))
+      if (!bfd_set_section_flags (section, bfd_flags))
        return FALSE;
 
       PRIV (dst_section) = section;
@@ -705,7 +711,7 @@ _bfd_vms_slurp_eihs (bfd *abfd, unsigned int offset)
       section->size = dmtbytes;
       section->filepos = VMS_BLOCK_SIZE * (dmtvbn - 1);
 
-      if (!bfd_set_section_flags (abfd, section, bfd_flags))
+      if (!bfd_set_section_flags (section, bfd_flags))
        return FALSE;
     }
 
@@ -861,7 +867,7 @@ vms_get_remaining_object_record (bfd *abfd, unsigned int read_so_far)
   if (to_read > PRIV (recrd.buf_size))
     {
       PRIV (recrd.buf)
-       = (unsigned char *) bfd_realloc (PRIV (recrd.buf), to_read);
+       = (unsigned char *) bfd_realloc_or_free (PRIV (recrd.buf), to_read);
       if (PRIV (recrd.buf) == NULL)
        return 0;
       PRIV (recrd.buf_size) = to_read;
@@ -924,36 +930,39 @@ _bfd_vms_slurp_ehdr (bfd *abfd)
       PRIV (hdr_data).hdr_l_recsiz = bfd_getl32 (vms_rec + 16);
       if ((vms_rec + 20 + vms_rec[20] + 1) >= end)
        goto fail;
-      PRIV (hdr_data).hdr_t_name   = _bfd_vms_save_counted_string (vms_rec + 20, vms_rec[20]);
+      PRIV (hdr_data).hdr_t_name
+       = _bfd_vms_save_counted_string (abfd, vms_rec + 20, vms_rec[20]);
       ptr = vms_rec + 20 + vms_rec[20] + 1;
       if ((ptr + *ptr + 1) >= end)
        goto fail;
-      PRIV (hdr_data).hdr_t_version =_bfd_vms_save_counted_string (ptr, *ptr);
+      PRIV (hdr_data).hdr_t_version
+       = _bfd_vms_save_counted_string (abfd, ptr, *ptr);
       ptr += *ptr + 1;
       if (ptr + 17 >= end)
        goto fail;
-      PRIV (hdr_data).hdr_t_date = _bfd_vms_save_sized_string (ptr, 17);
+      PRIV (hdr_data).hdr_t_date
+       = _bfd_vms_save_sized_string (abfd, ptr, 17);
       break;
 
     case EMH__C_LNM:
       if (vms_rec + PRIV (recrd.rec_size - 6) > end)
        goto fail;
-      PRIV (hdr_data).hdr_c_lnm =
-       _bfd_vms_save_sized_string (vms_rec, PRIV (recrd.rec_size - 6));
+      PRIV (hdr_data).hdr_c_lnm
+       = _bfd_vms_save_sized_string (abfd, vms_rec, PRIV (recrd.rec_size - 6));
       break;
 
     case EMH__C_SRC:
       if (vms_rec + PRIV (recrd.rec_size - 6) > end)
        goto fail;
-      PRIV (hdr_data).hdr_c_src =
-       _bfd_vms_save_sized_string (vms_rec, PRIV (recrd.rec_size - 6));
+      PRIV (hdr_data).hdr_c_src
+       = _bfd_vms_save_sized_string (abfd, vms_rec, PRIV (recrd.rec_size - 6));
       break;
 
     case EMH__C_TTL:
       if (vms_rec + PRIV (recrd.rec_size - 6) > end)
        goto fail;
-      PRIV (hdr_data).hdr_c_ttl =
-       _bfd_vms_save_sized_string (vms_rec, PRIV (recrd.rec_size - 6));
+      PRIV (hdr_data).hdr_c_ttl
+       = _bfd_vms_save_sized_string (abfd, vms_rec, PRIV (recrd.rec_size - 6));
       break;
 
     case EMH__C_CPR:
@@ -1121,7 +1130,7 @@ add_symbol_entry (bfd *abfd, struct vms_symbol_entry *sym)
       else
        {
          PRIV (max_sym_count) *= 2;
-         PRIV (syms) = bfd_realloc
+         PRIV (syms) = bfd_realloc_or_free
            (PRIV (syms),
             (PRIV (max_sym_count) * sizeof (struct vms_symbol_entry *)));
        }
@@ -1137,12 +1146,20 @@ add_symbol_entry (bfd *abfd, struct vms_symbol_entry *sym)
    Return NULL in case of error.  */
 
 static struct vms_symbol_entry *
-add_symbol (bfd *abfd, const unsigned char *ascic)
+add_symbol (bfd *abfd, const unsigned char *ascic, unsigned int max)
 {
   struct vms_symbol_entry *entry;
-  int len;
+  unsigned int len;
 
   len = *ascic++;
+  max -= 1;
+  if (len > max)
+    {
+      _bfd_error_handler (_("record is too small for symbol name length"));
+      bfd_set_error (bfd_error_bad_value);
+      return NULL;
+    }
+
   entry = (struct vms_symbol_entry *)bfd_zalloc (abfd, sizeof (*entry) + len);
   if (entry == NULL)
     return NULL;
@@ -1164,7 +1181,8 @@ _bfd_vms_slurp_egsd (bfd *abfd)
   int gsd_type;
   unsigned int gsd_size;
   unsigned char *vms_rec;
-  unsigned long base_addr;
+  bfd_vma base_addr;
+  long psindx;
 
   vms_debug2 ((2, "EGSD\n"));
 
@@ -1180,7 +1198,7 @@ _bfd_vms_slurp_egsd (bfd *abfd)
   PRIV (recrd.rec_size) -= 8;
 
   /* Calculate base address for each section.  */
-  base_addr = 0L;
+  base_addr = 0;
 
   while (PRIV (recrd.rec_size) > 4)
     {
@@ -1194,16 +1212,19 @@ _bfd_vms_slurp_egsd (bfd *abfd)
       /* PR 21615: Check for size overflow.  */
       if (PRIV (recrd.rec_size) < gsd_size)
        {
-         _bfd_error_handler (_("corrupt EGSD record: size (%#x) is larger than remaining space (%#x)"),
-                             gsd_size, PRIV (recrd.rec_size));
+         _bfd_error_handler (_("corrupt EGSD record type %d: size (%#x) "
+                               "is larger than remaining space (%#x)"),
+                             gsd_type, gsd_size, PRIV (recrd.rec_size));
          bfd_set_error (bfd_error_bad_value);
          return FALSE;
        }
 
       if (gsd_size < 4)
        {
-         _bfd_error_handler (_("corrupt EGSD record: size (%#x) is too small"),
-                             gsd_size);
+       too_small:
+         _bfd_error_handler (_("corrupt EGSD record type %d: size (%#x) "
+                               "is too small"),
+                             gsd_type, gsd_size);
          bfd_set_error (bfd_error_bad_value);
          return FALSE;
        }
@@ -1213,10 +1234,12 @@ _bfd_vms_slurp_egsd (bfd *abfd)
        case EGSD__C_PSC:
          /* Program section definition.  */
          {
-           struct vms_egps *egps = (struct vms_egps *)vms_rec;
+           struct vms_egps *egps = (struct vms_egps *) vms_rec;
            flagword new_flags, vms_flags;
            asection *section;
 
+           if (offsetof (struct vms_egps, flags) + 2 > gsd_size)
+             goto too_small;
            vms_flags = bfd_getl16 (egps->flags);
 
            if ((vms_flags & EGPS__V_REL) == 0)
@@ -1228,9 +1251,15 @@ _bfd_vms_slurp_egsd (bfd *abfd)
            else
              {
                char *name;
-               unsigned long align_addr;
-
-               name = _bfd_vms_save_counted_string (&egps->namlng, gsd_size - 4);
+               bfd_vma align_addr;
+               size_t left;
+
+               if (offsetof (struct vms_egps, namlng) >= gsd_size)
+                 goto too_small;
+               left = gsd_size - offsetof (struct vms_egps, namlng);
+               name = _bfd_vms_save_counted_string (abfd, &egps->namlng, left);
+               if (name == NULL || name[0] == 0)
+                 return FALSE;
 
                section = bfd_make_section (abfd, name);
                if (!section)
@@ -1238,12 +1267,13 @@ _bfd_vms_slurp_egsd (bfd *abfd)
 
                section->filepos = 0;
                section->size = bfd_getl32 (egps->alloc);
-               section->alignment_power = egps->align;
+               section->alignment_power = egps->align & 31;
 
                vms_section_data (section)->flags = vms_flags;
                vms_section_data (section)->no_flags = 0;
 
-               new_flags = vms_secflag_by_name (evax_section_flags, name,
+               new_flags = vms_secflag_by_name (evax_section_flags,
+                                                section->name,
                                                 section->size > 0);
                if (section->size > 0)
                  new_flags |= SEC_LOAD;
@@ -1261,14 +1291,13 @@ _bfd_vms_slurp_egsd (bfd *abfd)
                    new_flags |= SEC_CODE;
                    new_flags &= ~SEC_DATA;
                  }
-               if (!bfd_set_section_flags (abfd, section, new_flags))
+               if (!bfd_set_section_flags (section, new_flags))
                  return FALSE;
 
                /* Give a non-overlapping vma to non absolute sections.  */
-               align_addr = (1 << section->alignment_power);
-               if ((base_addr % align_addr) != 0)
-                 base_addr += (align_addr - (base_addr % align_addr));
-               section->vma = (bfd_vma)base_addr;
+               align_addr = (bfd_vma) 1 << section->alignment_power;
+               base_addr = (base_addr + align_addr - 1) & -align_addr;
+               section->vma = base_addr;
                base_addr += section->size;
              }
 
@@ -1292,18 +1321,22 @@ _bfd_vms_slurp_egsd (bfd *abfd)
 
        case EGSD__C_SYM:
          {
-           int nameoff;
+           unsigned int nameoff;
            struct vms_symbol_entry *entry;
            struct vms_egsy *egsy = (struct vms_egsy *) vms_rec;
            flagword old_flags;
 
+           if (offsetof (struct vms_egsy, flags) + 2 > gsd_size)
+             goto too_small;
            old_flags = bfd_getl16 (egsy->flags);
            if (old_flags & EGSY__V_DEF)
              nameoff = ESDF__B_NAMLNG;
            else
              nameoff = ESRF__B_NAMLNG;
 
-           entry = add_symbol (abfd, vms_rec + nameoff);
+           if (nameoff >= gsd_size)
+             goto too_small;
+           entry = add_symbol (abfd, vms_rec + nameoff, gsd_size - nameoff);
            if (entry == NULL)
              return FALSE;
 
@@ -1320,8 +1353,7 @@ _bfd_vms_slurp_egsd (bfd *abfd)
 
            if (old_flags & EGSY__V_DEF)
              {
-               struct vms_esdf *esdf = (struct vms_esdf *)vms_rec;
-               long psindx;
+               struct vms_esdf *esdf = (struct vms_esdf *) vms_rec;
 
                entry->value = bfd_getl64 (esdf->value);
                if (PRIV (sections) == NULL)
@@ -1331,7 +1363,9 @@ _bfd_vms_slurp_egsd (bfd *abfd)
                /* PR 21813: Check for an out of range index.  */
                if (psindx < 0 || psindx >= (int) PRIV (section_count))
                  {
-                   _bfd_error_handler (_("corrupt EGSD record: its psindx field is too big (%#lx)"),
+                 bad_psindx:
+                   _bfd_error_handler (_("corrupt EGSD record: its psindx "
+                                         "field is too big (%#lx)"),
                                        psindx);
                    bfd_set_error (bfd_error_bad_value);
                    return FALSE;
@@ -1344,14 +1378,9 @@ _bfd_vms_slurp_egsd (bfd *abfd)
 
                    entry->code_value = bfd_getl64 (esdf->code_address);
                    psindx = bfd_getl32 (esdf->ca_psindx);
-               /* PR 21813: Check for an out of range index.  */
+                   /* PR 21813: Check for an out of range index.  */
                    if (psindx < 0 || psindx >= (int) PRIV (section_count))
-                     {
-                       _bfd_error_handler (_("corrupt EGSD record: its psindx field is too big (%#lx)"),
-                                           psindx);
-                       bfd_set_error (bfd_error_bad_value);
-                       return FALSE;
-                     }
+                     goto bad_psindx;
                    entry->code_section = PRIV (sections)[psindx];
                  }
              }
@@ -1363,11 +1392,13 @@ _bfd_vms_slurp_egsd (bfd *abfd)
            struct vms_symbol_entry *entry;
            struct vms_egst *egst = (struct vms_egst *)vms_rec;
            flagword old_flags;
+           unsigned int nameoff = offsetof (struct vms_egst, namlng);
 
            old_flags = bfd_getl16 (egst->header.flags);
 
-           entry = add_symbol (abfd, &egst->namlng);
-
+           if (nameoff >= gsd_size)
+             goto too_small;
+           entry = add_symbol (abfd, &egst->namlng, gsd_size - nameoff);
            if (entry == NULL)
              return FALSE;
 
@@ -1379,19 +1410,12 @@ _bfd_vms_slurp_egsd (bfd *abfd)
 
            if (old_flags & EGSY__V_REL)
              {
-               long psindx;
-
                if (PRIV (sections) == NULL)
                  return FALSE;
                psindx = bfd_getl32 (egst->psindx);
                /* PR 21813: Check for an out of range index.  */
                if (psindx < 0 || psindx >= (int) PRIV (section_count))
-                 {
-                   _bfd_error_handler (_("corrupt EGSD record: its psindx field is too big (%#lx)"),
-                                       psindx);
-                   bfd_set_error (bfd_error_bad_value);
-                   return FALSE;
-                 }
+                 goto bad_psindx;
                entry->section = PRIV (sections)[psindx];
              }
            else
@@ -1437,7 +1461,7 @@ _bfd_vms_slurp_egsd (bfd *abfd)
 
 /* Push value and section index.  */
 
-static void
+static bfd_boolean
 _bfd_vms_push (bfd *abfd, bfd_vma val, unsigned int reloc)
 {
   vms_debug2 ((4, "<push %08lx (0x%08x) at %d>\n",
@@ -1450,26 +1474,28 @@ _bfd_vms_push (bfd *abfd, bfd_vma val, unsigned int reloc)
     {
       bfd_set_error (bfd_error_bad_value);
       _bfd_error_handler (_("stack overflow (%d) in _bfd_vms_push"), PRIV (stackptr));
-      exit (1);
+      return FALSE;
     }
+  return TRUE;
 }
 
 /* Pop value and section index.  */
 
-static void
+static bfd_boolean
 _bfd_vms_pop (bfd *abfd, bfd_vma *val, unsigned int *rel)
 {
   if (PRIV (stackptr) == 0)
     {
       bfd_set_error (bfd_error_bad_value);
       _bfd_error_handler (_("stack underflow in _bfd_vms_pop"));
-      exit (1);
+      return FALSE;
     }
   PRIV (stackptr)--;
   *val = PRIV (stack[PRIV (stackptr)]).value;
   *rel = PRIV (stack[PRIV (stackptr)]).reloc;
 
   vms_debug2 ((4, "<pop %08lx (0x%08x)>\n", (unsigned long)*val, *rel));
+  return TRUE;
 }
 
 /* Routines to fill sections contents during tir/etir read.  */
@@ -1526,40 +1552,62 @@ image_inc_ptr (bfd *abfd, bfd_vma offset)
 
 /* Save current DST location counter under specified index.  */
 
-static void
+static bfd_boolean
 dst_define_location (bfd *abfd, unsigned int loc)
 {
   vms_debug2 ((4, "dst_define_location (%d)\n", (int)loc));
 
+  if (loc > 1 << 24)
+    {
+      /* 16M entries ought to be plenty.  */
+      bfd_set_error (bfd_error_bad_value);
+      _bfd_error_handler (_("dst_define_location %u too large"), loc);
+      return FALSE;
+    }
+
   /* Grow the ptr offset table if necessary.  */
   if (loc + 1 > PRIV (dst_ptr_offsets_count))
     {
-      PRIV (dst_ptr_offsets) = bfd_realloc (PRIV (dst_ptr_offsets),
-                                          (loc + 1) * sizeof (unsigned int));
+      PRIV (dst_ptr_offsets)
+       = bfd_realloc_or_free (PRIV (dst_ptr_offsets),
+                              (loc + 1) * sizeof (unsigned int));
+      if (PRIV (dst_ptr_offsets) == NULL)
+       return FALSE;
       PRIV (dst_ptr_offsets_count) = loc + 1;
     }
 
   PRIV (dst_ptr_offsets)[loc] = PRIV (image_offset);
+  return TRUE;
 }
 
 /* Restore saved DST location counter from specified index.  */
 
-static void
+static bfd_boolean
 dst_restore_location (bfd *abfd, unsigned int loc)
 {
   vms_debug2 ((4, "dst_restore_location (%d)\n", (int)loc));
 
-  PRIV (image_offset) = PRIV (dst_ptr_offsets)[loc];
+  if (loc < PRIV (dst_ptr_offsets_count))
+    {
+      PRIV (image_offset) = PRIV (dst_ptr_offsets)[loc];
+      return TRUE;
+    }
+  return FALSE;
 }
 
 /* Retrieve saved DST location counter from specified index.  */
 
-static unsigned int
-dst_retrieve_location (bfd *abfd, unsigned int loc)
+static bfd_boolean
+dst_retrieve_location (bfd *abfd, bfd_vma *loc)
 {
-  vms_debug2 ((4, "dst_retrieve_location (%d)\n", (int)loc));
+  vms_debug2 ((4, "dst_retrieve_location (%d)\n", (int) *loc));
 
-  return PRIV (dst_ptr_offsets)[loc];
+  if (*loc < PRIV (dst_ptr_offsets_count))
+    {
+      *loc = PRIV (dst_ptr_offsets)[*loc];
+      return TRUE;
+    }
+  return FALSE;
 }
 
 /* Write multiple bytes to section image.  */
@@ -1567,29 +1615,39 @@ dst_retrieve_location (bfd *abfd, unsigned int loc)
 static bfd_boolean
 image_write (bfd *abfd, unsigned char *ptr, unsigned int size)
 {
+  asection *sec = PRIV (image_section);
+  size_t off = PRIV (image_offset);
+
+  /* Check bounds.  */
+  if (off > sec->size
+      || size > sec->size - off)
+    {
+      bfd_set_error (bfd_error_bad_value);
+      return FALSE;
+    }
+
 #if VMS_DEBUG
   _bfd_vms_debug (8, "image_write from (%p, %d) to (%ld)\n", ptr, size,
-                 (long)PRIV (image_offset));
-  _bfd_hexdump (9, ptr, size, 0);
+                 (long) off));
 #endif
 
   if (PRIV (image_section)->contents != NULL)
+    memcpy (sec->contents + off, ptr, size);
+  else
     {
-      asection *sec = PRIV (image_section);
-      file_ptr off = PRIV (image_offset);
-
-      /* Check bounds.  */
-      if (off > (file_ptr)sec->size
-         || size > (file_ptr)sec->size
-         || off + size > (file_ptr)sec->size)
-       {
-         bfd_set_error (bfd_error_bad_value);
-         return FALSE;
-       }
-
-      memcpy (sec->contents + off, ptr, size);
+      unsigned int i;
+      for (i = 0; i < size; i++)
+       if (ptr[i] != 0)
+         {
+           bfd_set_error (bfd_error_bad_value);
+           return FALSE;
+         }
     }
 
+#if VMS_DEBUG
+  _bfd_hexdump (9, ptr, size, 0);
+#endif
+
   PRIV (image_offset) += size;
   return TRUE;
 }
@@ -1837,10 +1895,10 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
   unsigned char *ptr;
   unsigned int length;
   unsigned char *maxptr;
-  bfd_vma op1;
-  bfd_vma op2;
-  unsigned int rel1;
-  unsigned int rel2;
+  bfd_vma op1 = 0;
+  bfd_vma op2 = 0;
+  unsigned int rel1 = RELC_NONE;
+  unsigned int rel2 = RELC_NONE;
   struct alpha_vms_link_hash_entry *h;
 
   PRIV (recrd.rec) += ETIR__C_HEADER_SIZE;
@@ -1854,24 +1912,29 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
 
   while (ptr < maxptr)
     {
-      int cmd = bfd_getl16 (ptr);
-      int cmd_length = bfd_getl16 (ptr + 2);
+      int cmd, cmd_length;
 
-      ptr += 4;
+      if (ptr + 4 > maxptr)
+       goto corrupt_etir;
+
+      cmd = bfd_getl16 (ptr);
+      cmd_length = bfd_getl16 (ptr + 2);
 
       /* PR 21589 and 21579: Check for a corrupt ETIR record.  */
-      if (cmd_length < 4 || (ptr + cmd_length > maxptr + 4))
+      if (cmd_length < 4 || ptr + cmd_length > maxptr)
        {
        corrupt_etir:
          _bfd_error_handler (_("corrupt ETIR record encountered"));
          bfd_set_error (bfd_error_bad_value);
          return FALSE;
        }
+      ptr += 4;
+      cmd_length -= 4;
 
 #if VMS_DEBUG
       _bfd_vms_debug (4, "etir: %s(%d)\n",
                      _bfd_vms_etir_name (cmd), cmd);
-      _bfd_hexdump (8, ptr, cmd_length - 4, 0);
+      _bfd_hexdump (8, ptr, cmd_length, 0);
 #endif
 
       switch (cmd)
@@ -1881,8 +1944,9 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
 
             stack 32 bit value of symbol (high bits set to 0).  */
        case ETIR__C_STA_GBL:
-         _bfd_vms_get_value (abfd, ptr, maxptr, info, &op1, &h);
-         _bfd_vms_push (abfd, op1, alpha_vms_sym_to_ctxt (h));
+         _bfd_vms_get_value (abfd, ptr, ptr + cmd_length, info, &op1, &h);
+         if (!_bfd_vms_push (abfd, op1, alpha_vms_sym_to_ctxt (h)))
+           return FALSE;
          break;
 
          /* Stack longword
@@ -1890,9 +1954,10 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
 
             stack 32 bit value, sign extend to 64 bit.  */
        case ETIR__C_STA_LW:
-         if (ptr + 4 >= maxptr)
+         if (cmd_length < 4)
            goto corrupt_etir;
-         _bfd_vms_push (abfd, bfd_getl32 (ptr), RELC_NONE);
+         if (!_bfd_vms_push (abfd, bfd_getl32 (ptr), RELC_NONE))
+           return FALSE;
          break;
 
          /* Stack quadword
@@ -1900,9 +1965,10 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
 
             stack 64 bit value of symbol.  */
        case ETIR__C_STA_QW:
-         if (ptr + 8 >= maxptr)
+         if (cmd_length < 8)
            goto corrupt_etir;
-         _bfd_vms_push (abfd, bfd_getl64 (ptr), RELC_NONE);
+         if (!_bfd_vms_push (abfd, bfd_getl64 (ptr), RELC_NONE))
+           return FALSE;
          break;
 
          /* Stack psect base plus quadword offset
@@ -1915,7 +1981,7 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          {
            int psect;
 
-           if (ptr + 12 >= maxptr)
+           if (cmd_length < 12)
              goto corrupt_etir;
            psect = bfd_getl32 (ptr);
            if ((unsigned int) psect >= PRIV (section_count))
@@ -1926,7 +1992,8 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
                return FALSE;
              }
            op1 = bfd_getl64 (ptr + 4);
-           _bfd_vms_push (abfd, op1, psect | RELC_SEC_BASE);
+           if (!_bfd_vms_push (abfd, op1, psect | RELC_SEC_BASE))
+             return FALSE;
          }
          break;
 
@@ -1941,25 +2008,30 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          /* Store byte: pop stack, write byte
             arg: -.  */
        case ETIR__C_STO_B:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 != RELC_NONE)
            goto bad_context;
-         image_write_b (abfd, (unsigned int) op1 & 0xff);
+         if (!image_write_b (abfd, (unsigned int) op1 & 0xff))
+           return FALSE;
          break;
 
          /* Store word: pop stack, write word
             arg: -.  */
        case ETIR__C_STO_W:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 != RELC_NONE)
            goto bad_context;
-         image_write_w (abfd, (unsigned int) op1 & 0xffff);
+         if (!image_write_w (abfd, (unsigned int) op1 & 0xffff))
+           return FALSE;
          break;
 
          /* Store longword: pop stack, write longword
             arg: -.  */
        case ETIR__C_STO_LW:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 & RELC_SEC_BASE)
            {
              op1 = alpha_vms_fix_sec_rel (abfd, info, rel1, op1);
@@ -1967,22 +2039,26 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
            }
          else if (rel1 & RELC_SHR_BASE)
            {
-             alpha_vms_add_fixup_lr (info, rel1 & RELC_MASK, op1);
+             if (!alpha_vms_add_fixup_lr (info, rel1 & RELC_MASK, op1))
+               return FALSE;
              rel1 = RELC_NONE;
            }
          if (rel1 != RELC_NONE)
            {
              if (rel1 != RELC_REL)
                abort ();
-             alpha_vms_add_lw_reloc (info);
+             if (!alpha_vms_add_lw_reloc (info))
+               return FALSE;
            }
-         image_write_l (abfd, op1);
+         if (!image_write_l (abfd, op1))
+           return FALSE;
          break;
 
          /* Store quadword: pop stack, write quadword
             arg: -.  */
        case ETIR__C_STO_QW:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 & RELC_SEC_BASE)
            {
              op1 = alpha_vms_fix_sec_rel (abfd, info, rel1, op1);
@@ -1994,9 +2070,11 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
            {
              if (rel1 != RELC_REL)
                abort ();
-             alpha_vms_add_qw_reloc (info);
+             if (!alpha_vms_add_qw_reloc (info))
+               return FALSE;
            }
-         image_write_q (abfd, op1);
+         if (!image_write_q (abfd, op1))
+           return FALSE;
          break;
 
          /* Store immediate repeated: pop stack for repeat count
@@ -2006,43 +2084,53 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          {
            int size;
 
-           if (ptr + 4 >= maxptr)
+           if (cmd_length < 4)
              goto corrupt_etir;
            size = bfd_getl32 (ptr);
-           _bfd_vms_pop (abfd, &op1, &rel1);
+           if (size > cmd_length - 4)
+             goto corrupt_etir;
+           if (!_bfd_vms_pop (abfd, &op1, &rel1))
+             return FALSE;
            if (rel1 != RELC_NONE)
              goto bad_context;
+           if (size == 0)
+             break;
+           op1 &= 0xffffffff;
            while (op1-- > 0)
-             image_write (abfd, ptr + 4, size);
+             if (!image_write (abfd, ptr + 4, size))
+               return FALSE;
          }
          break;
 
          /* Store global: write symbol value
             arg: cs    global symbol name.  */
        case ETIR__C_STO_GBL:
-         _bfd_vms_get_value (abfd, ptr, maxptr, info, &op1, &h);
+         _bfd_vms_get_value (abfd, ptr, ptr + cmd_length, info, &op1, &h);
          if (h && h->sym)
            {
              if (h->sym->typ == EGSD__C_SYMG)
                {
-                 alpha_vms_add_fixup_qr
-                   (info, abfd, h->sym->owner, h->sym->symbol_vector);
+                 if (!alpha_vms_add_fixup_qr (info, abfd, h->sym->owner,
+                                              h->sym->symbol_vector))
+                   return FALSE;
                  op1 = 0;
                }
              else
                {
                  op1 = alpha_vms_get_sym_value (h->sym->section,
                                                 h->sym->value);
-                 alpha_vms_add_qw_reloc (info);
+                 if (!alpha_vms_add_qw_reloc (info))
+                   return FALSE;
                }
            }
-         image_write_q (abfd, op1);
+         if (!image_write_q (abfd, op1))
+           return FALSE;
          break;
 
          /* Store code address: write address of entry point
             arg: cs    global symbol name (procedure).  */
        case ETIR__C_STO_CA:
-         _bfd_vms_get_value (abfd, ptr, maxptr, info, &op1, &h);
+         _bfd_vms_get_value (abfd, ptr, ptr + cmd_length, info, &op1, &h);
          if (h && h->sym)
            {
              if (h->sym->flags & EGSY__V_NORM)
@@ -2050,14 +2138,16 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
                  /* That's really a procedure.  */
                  if (h->sym->typ == EGSD__C_SYMG)
                    {
-                     alpha_vms_add_fixup_ca (info, abfd, h->sym->owner);
+                     if (!alpha_vms_add_fixup_ca (info, abfd, h->sym->owner))
+                       return FALSE;
                      op1 = h->sym->symbol_vector;
                    }
                  else
                    {
                      op1 = alpha_vms_get_sym_value (h->sym->code_section,
                                                     h->sym->code_value);
-                     alpha_vms_add_qw_reloc (info);
+                     if (!alpha_vms_add_qw_reloc (info))
+                       return FALSE;
                    }
                }
              else
@@ -2066,20 +2156,23 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
                  abort ();
                }
            }
-         image_write_q (abfd, op1);
+         if (!image_write_q (abfd, op1))
+           return FALSE;
          break;
 
          /* Store offset to psect: pop stack, add low 32 bits to base of psect
             arg: none.  */
        case ETIR__C_STO_OFF:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
 
          if (!(rel1 & RELC_SEC_BASE))
            abort ();
 
          op1 = alpha_vms_fix_sec_rel (abfd, info, rel1, op1);
          rel1 = RELC_REL;
-         image_write_q (abfd, op1);
+         if (!image_write_q (abfd, op1))
+           return FALSE;
          break;
 
          /* Store immediate
@@ -2089,10 +2182,11 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          {
            unsigned int size;
 
-           if (ptr + 4 >= maxptr)
+           if (cmd_length < 4)
              goto corrupt_etir;
            size = bfd_getl32 (ptr);
-           image_write (abfd, ptr + 4, size);
+           if (!image_write (abfd, ptr + 4, size))
+             return FALSE;
          }
          break;
 
@@ -2103,11 +2197,12 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
             store global longword: store 32bit value of symbol
             arg: cs    symbol name.  */
        case ETIR__C_STO_GBL_LW:
-         _bfd_vms_get_value (abfd, ptr, maxptr, info, &op1, &h);
+         _bfd_vms_get_value (abfd, ptr, ptr + cmd_length, info, &op1, &h);
 #if 0
          abort ();
 #endif
-         image_write_l (abfd, op1);
+         if (!image_write_l (abfd, op1))
+           return FALSE;
          break;
 
        case ETIR__C_STO_RB:
@@ -2156,12 +2251,15 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
             da signature.  */
 
        case ETIR__C_STC_LP_PSB:
-         _bfd_vms_get_value (abfd, ptr + 4, maxptr, info, &op1, &h);
+         if (cmd_length < 4)
+           goto corrupt_etir;
+         _bfd_vms_get_value (abfd, ptr + 4, ptr + cmd_length, info, &op1, &h);
          if (h && h->sym)
            {
              if (h->sym->typ == EGSD__C_SYMG)
                {
-                 alpha_vms_add_fixup_lp (info, abfd, h->sym->owner);
+                 if (!alpha_vms_add_fixup_lp (info, abfd, h->sym->owner))
+                   return FALSE;
                  op1 = h->sym->symbol_vector;
                  op2 = 0;
                }
@@ -2179,8 +2277,9 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
              op1 = 0;
              op2 = 0;
            }
-         image_write_q (abfd, op1);
-         image_write_q (abfd, op2);
+         if (!image_write_q (abfd, op1)
+             || !image_write_q (abfd, op2))
+           return FALSE;
          break;
 
          /* 205 Store-conditional NOP at address of global
@@ -2243,7 +2342,8 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          /* Det relocation base: pop stack, set image location counter
             arg: none.  */
        case ETIR__C_CTL_SETRB:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (!(rel1 & RELC_SEC_BASE))
            abort ();
          image_set_ptr (abfd, op1, rel1 & RELC_MASK, info);
@@ -2252,7 +2352,7 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          /* Augment relocation base: increment image location counter by offset
             arg: lw    offset value.  */
        case ETIR__C_CTL_AUGRB:
-         if (ptr + 4 >= maxptr)
+         if (cmd_length < 4)
            goto corrupt_etir;
          op1 = bfd_getl32 (ptr);
          image_inc_ptr (abfd, op1);
@@ -2261,46 +2361,65 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          /* Define location: pop index, save location counter under index
             arg: none.  */
        case ETIR__C_CTL_DFLOC:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 != RELC_NONE)
            goto bad_context;
-         dst_define_location (abfd, op1);
+         if (!dst_define_location (abfd, op1))
+           return FALSE;
          break;
 
          /* Set location: pop index, restore location counter from index
             arg: none.  */
        case ETIR__C_CTL_STLOC:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 != RELC_NONE)
            goto bad_context;
-         dst_restore_location (abfd, op1);
+         if (!dst_restore_location (abfd, op1))
+           {
+             bfd_set_error (bfd_error_bad_value);
+             _bfd_error_handler (_("invalid %s"), "ETIR__C_CTL_STLOC");
+             return FALSE;
+           }
          break;
 
          /* Stack defined location: pop index, push location counter from index
             arg: none.  */
        case ETIR__C_CTL_STKDL:
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, dst_retrieve_location (abfd, op1), RELC_NONE);
+         if (!dst_retrieve_location (abfd, &op1))
+           {
+             bfd_set_error (bfd_error_bad_value);
+             _bfd_error_handler (_("invalid %s"), "ETIR__C_CTL_STKDL");
+             return FALSE;
+           }
+         if (!_bfd_vms_push (abfd, op1, RELC_NONE))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_NOP:      /* No-op.  */
          break;
 
        case ETIR__C_OPR_ADD:      /* Add.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 == RELC_NONE && rel2 != RELC_NONE)
            rel1 = rel2;
          else if (rel1 != RELC_NONE && rel2 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, op1 + op2, rel1);
+         if (!_bfd_vms_push (abfd, op1 + op2, rel1))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_SUB:      /* Subtract.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 == RELC_NONE && rel2 != RELC_NONE)
            rel1 = rel2;
          else if ((rel1 & RELC_SEC_BASE) && (rel2 & RELC_SEC_BASE))
@@ -2311,69 +2430,93 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
            }
          else if (rel1 != RELC_NONE && rel2 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, op2 - op1, rel1);
+         if (!_bfd_vms_push (abfd, op2 - op1, rel1))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_MUL:      /* Multiply.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 != RELC_NONE || rel2 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, op1 * op2, RELC_NONE);
+         if (!_bfd_vms_push (abfd, op1 * op2, RELC_NONE))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_DIV:      /* Divide.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 != RELC_NONE || rel2 != RELC_NONE)
            goto bad_context;
-         if (op2 == 0)
-           _bfd_vms_push (abfd, 0, RELC_NONE);
+         if (op1 == 0)
+           {
+             /* Divide by zero is supposed to give a result of zero,
+                and a non-fatal warning message.  */
+             _bfd_error_handler (_("%s divide by zero"), "ETIR__C_OPR_DIV");
+             if (!_bfd_vms_push (abfd, 0, RELC_NONE))
+               return FALSE;
+           }
          else
-           _bfd_vms_push (abfd, op2 / op1, RELC_NONE);
+           {
+             if (!_bfd_vms_push (abfd, op2 / op1, RELC_NONE))
+               return FALSE;
+           }
          break;
 
        case ETIR__C_OPR_AND:      /* Logical AND.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 != RELC_NONE || rel2 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, op1 & op2, RELC_NONE);
+         if (!_bfd_vms_push (abfd, op1 & op2, RELC_NONE))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_IOR:      /* Logical inclusive OR.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 != RELC_NONE || rel2 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, op1 | op2, RELC_NONE);
+         if (!_bfd_vms_push (abfd, op1 | op2, RELC_NONE))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_EOR:      /* Logical exclusive OR.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 != RELC_NONE || rel2 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, op1 ^ op2, RELC_NONE);
+         if (!_bfd_vms_push (abfd, op1 ^ op2, RELC_NONE))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_NEG:      /* Negate.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, -op1, RELC_NONE);
+         if (!_bfd_vms_push (abfd, -op1, RELC_NONE))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_COM:      /* Complement.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (rel1 != RELC_NONE)
            goto bad_context;
-         _bfd_vms_push (abfd, ~op1, RELC_NONE);
+         if (!_bfd_vms_push (abfd, ~op1, RELC_NONE))
+           return FALSE;
          break;
 
        case ETIR__C_OPR_ASH:      /* Arithmetic shift.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
-         _bfd_vms_pop (abfd, &op2, &rel2);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1)
+             || !_bfd_vms_pop (abfd, &op2, &rel2))
+           return FALSE;
          if (rel1 != RELC_NONE || rel2 != RELC_NONE)
            {
            bad_context:
@@ -2381,11 +2524,29 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
                                  _bfd_vms_etir_name (cmd));
              return FALSE;
            }
-         if ((int)op2 < 0)             /* Shift right.  */
-           op1 >>= -(int)op2;
-         else                  /* Shift left.  */
-           op1 <<= (int)op2;
-         _bfd_vms_push (abfd, op1, RELC_NONE); /* FIXME: sym.  */
+         if ((bfd_signed_vma) op2 < 0)
+           {
+             /* Shift right.  */
+             bfd_vma sign;
+             op2 = -op2;
+             if (op2 >= CHAR_BIT * sizeof (op1))
+               op2 = CHAR_BIT * sizeof (op1) - 1;
+             /* op1 = (bfd_signed_vma) op1 >> op2; */
+             sign = op1 & ((bfd_vma) 1 << (CHAR_BIT * sizeof (op1) - 1));
+             op1 >>= op2;
+             sign >>= op2;
+             op1 = (op1 ^ sign) - sign;
+           }
+         else
+           {
+             /* Shift left.  */
+             if (op2 >= CHAR_BIT * sizeof (op1))
+               op1 = 0;
+             else
+               op1 <<= op2;
+           }
+         if (!_bfd_vms_push (abfd, op1, RELC_NONE)) /* FIXME: sym.  */
+           return FALSE;
          break;
 
        case ETIR__C_OPR_INSV:      /* Insert field.   */
@@ -2399,14 +2560,20 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          break;
 
        case ETIR__C_OPR_SEL:      /* Select.  */
-         _bfd_vms_pop (abfd, &op1, &rel1);
+         if (!_bfd_vms_pop (abfd, &op1, &rel1))
+           return FALSE;
          if (op1 & 0x01L)
-           _bfd_vms_pop (abfd, &op1, &rel1);
+           {
+             if (!_bfd_vms_pop (abfd, &op1, &rel1))
+               return FALSE;
+           }
          else
            {
-             _bfd_vms_pop (abfd, &op1, &rel1);
-             _bfd_vms_pop (abfd, &op2, &rel2);
-             _bfd_vms_push (abfd, op1, rel1);
+             if (!_bfd_vms_pop (abfd, &op1, &rel1)
+                 || !_bfd_vms_pop (abfd, &op2, &rel2))
+               return FALSE;
+             if (!_bfd_vms_push (abfd, op1, rel1))
+               return FALSE;
            }
          break;
 
@@ -2416,7 +2583,7 @@ _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
          break;
        }
 
-      ptr += cmd_length - 4;
+      ptr += cmd_length;
     }
 
   return TRUE;
@@ -2441,7 +2608,7 @@ vms_slurp_debug (bfd *abfd)
       section = bfd_make_section (abfd, "$DST$");
       if (!section)
        return FALSE;
-      if (!bfd_set_section_flags (abfd, section, flags))
+      if (!bfd_set_section_flags (section, flags))
        return FALSE;
       PRIV (dst_section) = section;
     }
@@ -2527,7 +2694,7 @@ _bfd_vms_slurp_eeom (bfd *abfd)
 static bfd_boolean
 _bfd_vms_slurp_object_records (bfd * abfd)
 {
-  bfd_boolean err;
+  bfd_boolean ok;
   int type;
 
   do
@@ -2544,27 +2711,27 @@ _bfd_vms_slurp_object_records (bfd * abfd)
       switch (type)
        {
        case EOBJ__C_EMH:
-         err = _bfd_vms_slurp_ehdr (abfd);
+         ok = _bfd_vms_slurp_ehdr (abfd);
          break;
        case EOBJ__C_EEOM:
-         err = _bfd_vms_slurp_eeom (abfd);
+         ok = _bfd_vms_slurp_eeom (abfd);
          break;
        case EOBJ__C_EGSD:
-         err = _bfd_vms_slurp_egsd (abfd);
+         ok = _bfd_vms_slurp_egsd (abfd);
          break;
        case EOBJ__C_ETIR:
-         err = TRUE; /* _bfd_vms_slurp_etir (abfd); */
+         ok = TRUE; /* _bfd_vms_slurp_etir (abfd); */
          break;
        case EOBJ__C_EDBG:
-         err = _bfd_vms_slurp_edbg (abfd);
+         ok = _bfd_vms_slurp_edbg (abfd);
          break;
        case EOBJ__C_ETBT:
-         err = _bfd_vms_slurp_etbt (abfd);
+         ok = _bfd_vms_slurp_etbt (abfd);
          break;
        default:
-         err = FALSE;
+         ok = FALSE;
        }
-      if (!err)
+      if (!ok)
        {
          vms_debug2 ((2, "slurp type %d failed\n", type));
          return FALSE;
@@ -2579,7 +2746,7 @@ _bfd_vms_slurp_object_records (bfd * abfd)
 static bfd_boolean
 vms_initialize (bfd * abfd)
 {
-  bfd_size_type amt;
+  size_t amt;
 
   amt = sizeof (struct vms_private_data_struct);
   abfd->tdata.any = bfd_zalloc (abfd, amt);
@@ -2601,10 +2768,26 @@ vms_initialize (bfd * abfd)
   return FALSE;
 }
 
+/* Free malloc'd memory.  */
+
+static void
+alpha_vms_free_private (bfd *abfd)
+{
+  struct module *module;
+
+  free (PRIV (recrd.buf));
+  free (PRIV (sections));
+  free (PRIV (syms));
+  free (PRIV (dst_ptr_offsets));
+
+  for (module = PRIV (modules); module; module = module->next)
+    free (module->file_table);
+}
+
 /* Check the format for a file being read.
    Return a (bfd_target *) if it's an object file or zero if not.  */
 
-static const struct bfd_target *
+static bfd_cleanup
 alpha_vms_object_p (bfd *abfd)
 {
   void *tdata_save = abfd->tdata.any;
@@ -2615,10 +2798,13 @@ alpha_vms_object_p (bfd *abfd)
 
   /* Allocate alpha-vms specific data.  */
   if (!vms_initialize (abfd))
-    goto error_ret;
+    {
+      abfd->tdata.any = tdata_save;
+      return NULL;
+    }
 
   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
-    goto err_wrong_format;
+    goto error_ret;
 
   /* The first challenge with VMS is to discover the kind of the file.
 
@@ -2637,27 +2823,17 @@ alpha_vms_object_p (bfd *abfd)
      2 bytes size repeated) and 12 bytes for images (4 bytes major id,
      4 bytes minor id, 4 bytes length).  */
   test_len = 12;
-
-  /* Size the main buffer.  */
-  buf = (unsigned char *) bfd_malloc (test_len);
+  buf = _bfd_malloc_and_read (abfd, test_len, test_len);
   if (buf == NULL)
     goto error_ret;
   PRIV (recrd.buf) = buf;
   PRIV (recrd.buf_size) = test_len;
-
-  /* Initialize the record pointer.  */
   PRIV (recrd.rec) = buf;
 
-  if (bfd_bread (buf, test_len, abfd) != test_len)
-    goto err_wrong_format;
-
   /* Is it an image?  */
   if ((bfd_getl32 (buf) == EIHD__K_MAJORID)
       && (bfd_getl32 (buf + 4) == EIHD__K_MINORID))
     {
-      unsigned int to_read;
-      unsigned int read_so_far;
-      unsigned int remaining;
       unsigned int eisd_offset, eihs_offset;
 
       /* Extract the header size.  */
@@ -2667,44 +2843,25 @@ alpha_vms_object_p (bfd *abfd)
       if (PRIV (recrd.rec_size) == 0)
        PRIV (recrd.rec_size) = sizeof (struct vms_eihd);
 
-      if (PRIV (recrd.rec_size) > PRIV (recrd.buf_size))
-       {
-         buf = bfd_realloc_or_free (buf, PRIV (recrd.rec_size));
-
-         if (buf == NULL)
-           {
-             PRIV (recrd.buf) = NULL;
-             goto error_ret;
-           }
-         PRIV (recrd.buf) = buf;
-         PRIV (recrd.buf_size) = PRIV (recrd.rec_size);
-       }
-
       /* PR 21813: Check for a truncated record.  */
-      if (PRIV (recrd.rec_size < test_len))
-       goto error_ret;
-      /* Read the remaining record.  */
-      remaining = PRIV (recrd.rec_size) - test_len;
-      to_read = MIN (VMS_BLOCK_SIZE - test_len, remaining);
-      read_so_far = test_len;
-
-      while (remaining > 0)
-       {
-         if (bfd_bread (buf + read_so_far, to_read, abfd) != to_read)
-           goto err_wrong_format;
+      /* PR 17512: file: 7d7c57c2.  */
+      if (PRIV (recrd.rec_size) < sizeof (struct vms_eihd))
+       goto err_wrong_format;
 
-         read_so_far += to_read;
-         remaining -= to_read;
+      if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
+       goto error_ret;
 
-         to_read = MIN (VMS_BLOCK_SIZE, remaining);
-       }
+      free (PRIV (recrd.buf));
+      PRIV (recrd.buf) = NULL;
+      buf = _bfd_malloc_and_read (abfd, PRIV (recrd.rec_size),
+                                 PRIV (recrd.rec_size));
+      if (buf == NULL)
+       goto error_ret;
 
-      /* Reset the record pointer.  */
+      PRIV (recrd.buf) = buf;
+      PRIV (recrd.buf_size) = PRIV (recrd.rec_size);
       PRIV (recrd.rec) = buf;
 
-      /* PR 17512: file: 7d7c57c2.  */
-      if (PRIV (recrd.rec_size) < sizeof (struct vms_eihd))
-       goto error_ret;
       vms_debug2 ((2, "file type is image\n"));
 
       if (!_bfd_vms_slurp_eihd (abfd, &eisd_offset, &eihs_offset))
@@ -2749,14 +2906,13 @@ alpha_vms_object_p (bfd *abfd)
   if (! bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0))
     goto err_wrong_format;
 
-  return abfd->xvec;
+  return alpha_vms_free_private;
 
  err_wrong_format:
   bfd_set_error (bfd_error_wrong_format);
 
  error_ret:
-  if (PRIV (recrd.buf))
-    free (PRIV (recrd.buf));
+  alpha_vms_free_private (abfd);
   if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
     bfd_release (abfd, abfd->tdata.any);
   abfd->tdata.any = tdata_save;
@@ -2855,22 +3011,36 @@ _bfd_vms_write_eeom (bfd *abfd)
   return TRUE;
 }
 
-static void
+static void *
 vector_grow1 (struct vector_type *vec, size_t elsz)
 {
-  if (vec->nbr_el + 1 < vec->max_el)
-    return;
-
-  if (vec->max_el == 0)
-    {
-      vec->max_el = 16;
-      vec->els = bfd_malloc2 (vec->max_el, elsz);
-    }
-  else
+  if (vec->nbr_el >= vec->max_el)
     {
-      vec->max_el *= 2;
-      vec->els = bfd_realloc2 (vec->els, vec->max_el, elsz);
+      if (vec->max_el == 0)
+       {
+         vec->max_el = 16;
+         vec->els = bfd_malloc (vec->max_el * elsz);
+       }
+      else
+       {
+         size_t amt;
+         if (vec->max_el > -1u / 2)
+           {
+             bfd_set_error (bfd_error_file_too_big);
+             return NULL;
+           }
+         vec->max_el *= 2;
+         if (_bfd_mul_overflow (vec->max_el, elsz, &amt))
+           {
+             bfd_set_error (bfd_error_file_too_big);
+             return NULL;
+           }
+         vec->els = bfd_realloc_or_free (vec->els, amt);
+       }
     }
+  if (vec->els == NULL)
+    return NULL;
+  return (char *) vec->els + elsz * vec->nbr_el++;
 }
 
 /* Bump ABFD file position to next block.  */
@@ -4094,7 +4264,10 @@ _bfd_vms_write_etir (bfd * abfd, int objtype ATTRIBUTE_UNUSED)
            {
              /* Output rest of section.  */
              if (curr_addr > section->size)
-               _bfd_error_handler (_("size error in section %pA"), section);
+               {
+                 _bfd_error_handler (_("size error in section %pA"), section);
+                 return FALSE;
+               }
              size = section->size - curr_addr;
              sto_imm (abfd, section, size, curr_data, curr_addr);
              curr_data += size;
@@ -4167,7 +4340,7 @@ new_module (bfd *abfd)
 
 /* Parse debug info for a module and internalize it.  */
 
-static void
+static bfd_boolean
 parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
              int length)
 {
@@ -4201,7 +4374,7 @@ parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
        {
        case DST__K_MODBEG:
          module->name
-           = _bfd_vms_save_counted_string (ptr + DST_S_B_MODBEG_NAME,
+           = _bfd_vms_save_counted_string (abfd, ptr + DST_S_B_MODBEG_NAME,
                                            maxptr - (ptr + DST_S_B_MODBEG_NAME));
 
          curr_pc = 0;
@@ -4219,7 +4392,7 @@ parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
          funcinfo = (struct funcinfo *)
            bfd_zalloc (abfd, sizeof (struct funcinfo));
          funcinfo->name
-           = _bfd_vms_save_counted_string (ptr + DST_S_B_RTNBEG_NAME,
+           = _bfd_vms_save_counted_string (abfd, ptr + DST_S_B_RTNBEG_NAME,
                                            maxptr - (ptr + DST_S_B_RTNBEG_NAME));
          funcinfo->low = bfd_getl32 (ptr + DST_S_L_RTNBEG_ADDRESS);
          funcinfo->next = module->func_table;
@@ -4270,19 +4443,20 @@ parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
                  {
                    unsigned int fileid
                      = bfd_getl16 (src_ptr + DST_S_W_SRC_DF_FILEID);
-                   char *filename
-                     = _bfd_vms_save_counted_string (src_ptr + DST_S_B_SRC_DF_FILENAME,
-                                                     (ptr + rec_length) -
-                                                     (src_ptr + DST_S_B_SRC_DF_FILENAME)
-                                                     );
+                   char *filename = _bfd_vms_save_counted_string
+                     (abfd,
+                      src_ptr + DST_S_B_SRC_DF_FILENAME,
+                      ptr + rec_length - (src_ptr + DST_S_B_SRC_DF_FILENAME));
 
                    while (fileid >= module->file_table_count)
                      {
                        module->file_table_count *= 2;
                        module->file_table
-                         = bfd_realloc (module->file_table,
-                                        module->file_table_count
-                                          * sizeof (struct fileinfo));
+                         = bfd_realloc_or_free (module->file_table,
+                                                module->file_table_count
+                                                * sizeof (struct fileinfo));
+                       if (module->file_table == NULL)
+                         return FALSE;
                      }
 
                    module->file_table [fileid].name = filename;
@@ -4604,6 +4778,7 @@ parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
      because parsing can be either performed at module creation
      or deferred until debug info is consumed.  */
   SET_MODULE_PARSED (module);
+  return TRUE;
 }
 
 /* Build the list of modules for the specified BFD.  */
@@ -4620,7 +4795,7 @@ build_module_list (bfd *abfd)
         section and build the list of modules.  This is sufficient
         since we can compute the start address and the end address
         of every module from the section contents.  */
-      bfd_size_type size = bfd_get_section_size (dmt);
+      bfd_size_type size = bfd_section_size (dmt);
       unsigned char *ptr, *end;
 
       ptr = (unsigned char *) bfd_alloc (abfd, size);
@@ -4681,7 +4856,8 @@ build_module_list (bfd *abfd)
        return NULL;
 
       module = new_module (abfd);
-      parse_module (abfd, module, PRIV (dst_section)->contents, -1);
+      if (!parse_module (abfd, module, PRIV (dst_section)->contents, -1))
+       return NULL;
       list = module;
     }
 
@@ -4706,17 +4882,19 @@ module_find_nearest_line (bfd *abfd, struct module *module, bfd_vma addr,
     {
       unsigned int size = module->size;
       unsigned int modbeg = PRIV (dst_section)->filepos + module->modbeg;
-      unsigned char *buffer = (unsigned char *) bfd_malloc (module->size);
+      unsigned char *buffer;
 
       if (bfd_seek (abfd, modbeg, SEEK_SET) != 0
-         || bfd_bread (buffer, size, abfd) != size)
+         || (buffer = _bfd_malloc_and_read (abfd, size, size)) == NULL)
        {
          bfd_set_error (bfd_error_no_debug_section);
          return FALSE;
        }
 
-      parse_module (abfd, module, buffer, size);
+      ret = parse_module (abfd, module, buffer, size);
       free (buffer);
+      if (!ret)
+       return ret;
     }
 
   /* Find out the function (if any) that contains the address.  */
@@ -5190,8 +5368,10 @@ alpha_vms_slurp_relocs (bfd *abfd)
                else
                  {
                    vms_sec->reloc_max *= 2;
-                   sec->relocation = bfd_realloc
+                   sec->relocation = bfd_realloc_or_free
                      (sec->relocation, vms_sec->reloc_max * sizeof (arelent));
+                   if (sec->relocation == NULL)
+                     return FALSE;
                  }
              }
            reloc = &sec->relocation[sec->reloc_count];
@@ -6612,7 +6792,7 @@ evax_bfd_print_relocation_records (FILE *file, const unsigned char *rel,
          fprintf (file, _("   bitmap: 0x%08x (count: %u):\n"), val, count);
 
          for (k = 0; k < 32; k++)
-           if (val & (1 << k))
+           if (val & (1u << k))
              {
                if (n == 0)
                  fputs ("   ", file);
@@ -7033,8 +7213,8 @@ evax_bfd_print_dst (struct bfd *abfd, unsigned int dst_size, FILE *file)
       dst_size -= len;
       off += len;
       len -= sizeof (dsth);
-      buf = bfd_malloc (len);
-      if (bfd_bread (buf, len, abfd) != len)
+      buf = _bfd_malloc_and_read (abfd, len, len);
+      if (buf == NULL)
        {
          fprintf (file, _("cannot read DST symbol\n"));
          return;
@@ -7949,14 +8129,12 @@ evax_bfd_print_image (bfd *abfd, FILE *file)
       unsigned int codeadroff;
       unsigned int lpfixoff;
       unsigned int chgprtoff;
+      file_ptr f_off = (file_ptr) (eiaf_vbn - 1) * VMS_BLOCK_SIZE;
 
-      buf = bfd_malloc (eiaf_size);
-
-      if (bfd_seek (abfd, (file_ptr) (eiaf_vbn - 1) * VMS_BLOCK_SIZE, SEEK_SET)
-         || bfd_bread (buf, eiaf_size, abfd) != eiaf_size)
+      if (bfd_seek (abfd, f_off, SEEK_SET) != 0
+         || (buf = _bfd_malloc_and_read (abfd, eiaf_size, eiaf_size)) == NULL)
        {
          fprintf (file, _("cannot read EIHA\n"));
-         free (buf);
          return;
        }
       eiaf = (struct vms_eiaf *)buf;
@@ -8233,41 +8411,49 @@ alpha_vms_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
 
 /* Add a linkage pair fixup at address SECT + OFFSET to SHLIB. */
 
-static void
+static bfd_boolean
 alpha_vms_add_fixup_lp (struct bfd_link_info *info, bfd *src, bfd *shlib)
 {
   struct alpha_vms_shlib_el *sl;
   asection *sect = PRIV2 (src, image_section);
   file_ptr offset = PRIV2 (src, image_offset);
+  bfd_vma *p;
 
   sl = &VEC_EL (alpha_vms_link_hash (info)->shrlibs,
                struct alpha_vms_shlib_el, PRIV2 (shlib, shr_index));
   sl->has_fixups = TRUE;
-  VEC_APPEND_EL (sl->lp, bfd_vma,
-                sect->output_section->vma + sect->output_offset + offset);
+  p = VEC_APPEND (sl->lp, bfd_vma);
+  if (p == NULL)
+    return FALSE;
+  *p = sect->output_section->vma + sect->output_offset + offset;
   sect->output_section->flags |= SEC_RELOC;
+  return TRUE;
 }
 
 /* Add a code address fixup at address SECT + OFFSET to SHLIB. */
 
-static void
+static bfd_boolean
 alpha_vms_add_fixup_ca (struct bfd_link_info *info, bfd *src, bfd *shlib)
 {
   struct alpha_vms_shlib_el *sl;
   asection *sect = PRIV2 (src, image_section);
   file_ptr offset = PRIV2 (src, image_offset);
+  bfd_vma *p;
 
   sl = &VEC_EL (alpha_vms_link_hash (info)->shrlibs,
                struct alpha_vms_shlib_el, PRIV2 (shlib, shr_index));
   sl->has_fixups = TRUE;
-  VEC_APPEND_EL (sl->ca, bfd_vma,
-                sect->output_section->vma + sect->output_offset + offset);
+  p = VEC_APPEND (sl->ca, bfd_vma);
+  if (p == NULL)
+    return FALSE;
+  *p = sect->output_section->vma + sect->output_offset + offset;
   sect->output_section->flags |= SEC_RELOC;
+  return TRUE;
 }
 
 /* Add a quad word relocation fixup at address SECT + OFFSET to SHLIB. */
 
-static void
+static bfd_boolean
 alpha_vms_add_fixup_qr (struct bfd_link_info *info, bfd *src,
                        bfd *shlib, bfd_vma vec)
 {
@@ -8280,30 +8466,35 @@ alpha_vms_add_fixup_qr (struct bfd_link_info *info, bfd *src,
                struct alpha_vms_shlib_el, PRIV2 (shlib, shr_index));
   sl->has_fixups = TRUE;
   r = VEC_APPEND (sl->qr, struct alpha_vms_vma_ref);
+  if (r == NULL)
+    return FALSE;
   r->vma = sect->output_section->vma + sect->output_offset + offset;
   r->ref = vec;
   sect->output_section->flags |= SEC_RELOC;
+  return TRUE;
 }
 
-static void
+static bfd_boolean
 alpha_vms_add_fixup_lr (struct bfd_link_info *info ATTRIBUTE_UNUSED,
                        unsigned int shr ATTRIBUTE_UNUSED,
                        bfd_vma vec ATTRIBUTE_UNUSED)
 {
   /* Not yet supported.  */
-  abort ();
+  return FALSE;
 }
 
 /* Add relocation.  FIXME: Not yet emitted.  */
 
-static void
+static bfd_boolean
 alpha_vms_add_lw_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED)
 {
+  return FALSE;
 }
 
-static void
+static bfd_boolean
 alpha_vms_add_qw_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED)
 {
+  return FALSE;
 }
 
 static struct bfd_hash_entry *
@@ -8333,13 +8524,34 @@ alpha_vms_link_hash_newfunc (struct bfd_hash_entry *entry,
   return (struct bfd_hash_entry *) ret;
 }
 
+static void
+alpha_vms_bfd_link_hash_table_free (bfd *abfd)
+{
+  struct alpha_vms_link_hash_table *t;
+  unsigned i;
+
+  t = (struct alpha_vms_link_hash_table *) abfd->link.hash;
+  for (i = 0; i < VEC_COUNT (t->shrlibs); i++)
+    {
+      struct alpha_vms_shlib_el *shlib;
+
+      shlib = &VEC_EL (t->shrlibs, struct alpha_vms_shlib_el, i);
+      free (&VEC_EL (shlib->ca, bfd_vma, 0));
+      free (&VEC_EL (shlib->lp, bfd_vma, 0));
+      free (&VEC_EL (shlib->qr, struct alpha_vms_vma_ref, 0));
+    }
+  free (&VEC_EL (t->shrlibs, struct alpha_vms_shlib_el, 0));
+
+  _bfd_generic_link_hash_table_free (abfd);
+}
+
 /* Create an Alpha/VMS link hash table.  */
 
 static struct bfd_link_hash_table *
 alpha_vms_bfd_link_hash_table_create (bfd *abfd)
 {
   struct alpha_vms_link_hash_table *ret;
-  bfd_size_type amt = sizeof (struct alpha_vms_link_hash_table);
+  size_t amt = sizeof (struct alpha_vms_link_hash_table);
 
   ret = (struct alpha_vms_link_hash_table *) bfd_malloc (amt);
   if (ret == NULL)
@@ -8354,6 +8566,7 @@ alpha_vms_bfd_link_hash_table_create (bfd *abfd)
 
   VEC_INIT (ret->shrlibs);
   ret->fixup = NULL;
+  ret->root.hash_table_free = alpha_vms_bfd_link_hash_table_free;
 
   return &ret->root;
 }
@@ -8408,6 +8621,8 @@ alpha_vms_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
 
       shlib = VEC_APPEND (alpha_vms_link_hash (info)->shrlibs,
                          struct alpha_vms_shlib_el);
+      if (shlib == NULL)
+       return FALSE;
       shlib->abfd = abfd;
       VEC_INIT (shlib->ca);
       VEC_INIT (shlib->lp);
@@ -8909,8 +9124,8 @@ alpha_vms_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
       return FALSE;
     }
 
-  bfd_get_outsymbols (abfd) = NULL;
-  bfd_get_symcount (abfd) = 0;
+  abfd->outsymbols = NULL;
+  abfd->symcount = 0;
 
   /* Mark all sections which will be included in the output file.  */
   for (o = abfd->sections; o != NULL; o = o->next)
@@ -9280,35 +9495,24 @@ vms_close_and_cleanup (bfd * abfd)
   if (abfd == NULL || abfd->tdata.any == NULL)
     return TRUE;
 
-  if (abfd->format == bfd_archive)
+  if (abfd->format == bfd_object)
     {
-      bfd_release (abfd, abfd->tdata.any);
-      abfd->tdata.any = NULL;
-      return TRUE;
-    }
-
-  if (PRIV (recrd.buf) != NULL)
-    free (PRIV (recrd.buf));
-
-  if (PRIV (sections) != NULL)
-    free (PRIV (sections));
-
-  bfd_release (abfd, abfd->tdata.any);
-  abfd->tdata.any = NULL;
+      alpha_vms_free_private (abfd);
 
 #ifdef VMS
-  if (abfd->direction == write_direction)
-    {
-      /* Last step on VMS is to convert the file to variable record length
-        format.  */
-      if (!bfd_cache_close (abfd))
-       return FALSE;
-      if (!_bfd_vms_convert_to_var_unix_filename (abfd->filename))
-       return FALSE;
-    }
+      if (abfd->direction == write_direction)
+       {
+         /* Last step on VMS is to convert the file to variable record length
+            format.  */
+         if (!bfd_cache_close (abfd))
+           return FALSE;
+         if (!_bfd_vms_convert_to_var_unix_filename (abfd->filename))
+           return FALSE;
+       }
 #endif
+    }
 
-  return TRUE;
+  return _bfd_generic_close_and_cleanup (abfd);
 }
 
 /* Called when a new section is created.  */
@@ -9316,12 +9520,12 @@ vms_close_and_cleanup (bfd * abfd)
 static bfd_boolean
 vms_new_section_hook (bfd * abfd, asection *section)
 {
-  bfd_size_type amt;
+  size_t amt;
 
   vms_debug2 ((1, "vms_new_section_hook (%p, [%u]%s)\n",
               abfd, section->index, section->name));
 
-  if (! bfd_set_section_alignment (abfd, section, 0))
+  if (!bfd_set_section_alignment (section, 0))
     return FALSE;
 
   vms_debug2 ((7, "%u: %s\n", section->index, section->name));
@@ -9409,11 +9613,11 @@ vms_get_symbol_info (bfd * abfd ATTRIBUTE_UNUSED,
   else if (bfd_is_ind_section (sec))
     ret->type = 'I';
   else if ((symbol->flags & BSF_FUNCTION)
-          || (bfd_get_section_flags (abfd, sec) & SEC_CODE))
+          || (bfd_section_flags (sec) & SEC_CODE))
     ret->type = 'T';
-  else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
+  else if (bfd_section_flags (sec) & SEC_DATA)
     ret->type = 'D';
-  else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
+  else if (bfd_section_flags (sec) & SEC_ALLOC)
     ret->type = 'B';
   else
     ret->type = '?';
@@ -9501,6 +9705,7 @@ bfd_vms_get_data (bfd *abfd)
 #define vms_bfd_copy_link_hash_symbol_type \
   _bfd_generic_copy_link_hash_symbol_type
 #define vms_bfd_is_group_section         bfd_generic_is_group_section
+#define vms_bfd_group_name               bfd_generic_group_name
 #define vms_bfd_discard_group            bfd_generic_discard_group
 #define vms_section_already_linked       _bfd_generic_section_already_linked
 #define vms_bfd_define_common_symbol     bfd_generic_define_common_symbol
@@ -9547,6 +9752,7 @@ bfd_vms_get_data (bfd *abfd)
 #define alpha_vms_bfd_lookup_section_flags bfd_generic_lookup_section_flags
 #define alpha_vms_bfd_merge_sections bfd_generic_merge_sections
 #define alpha_vms_bfd_is_group_section bfd_generic_is_group_section
+#define alpha_vms_bfd_group_name bfd_generic_group_name
 #define alpha_vms_bfd_discard_group bfd_generic_discard_group
 #define alpha_vms_section_already_linked \
   _bfd_generic_section_already_linked