Make bfd_byte an int8_t, flagword a uint32_t
[binutils-gdb.git] / bfd / coffcode.h
index 9b8798c86313f389d2e1afe75c72bf1a8b384d5f..594f3e0457b0bd2759fa23de0ba0bc8143081bd9 100644 (file)
@@ -1,5 +1,5 @@
 /* Support for the generic parts of most COFF variants, for BFD.
-   Copyright (C) 1990-2021 Free Software Foundation, Inc.
+   Copyright (C) 1990-2023 Free Software Foundation, Inc.
    Written by Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -134,9 +134,10 @@ SUBSUBSECTION
        in output BFDs at runtime; if it is false, as it will be by default
        when generating an executable image, long section names are truncated;
        if true, the long section names extension is employed.  The hook
-       points to a function that allows the value of the flag to be altered
-       at runtime, on formats that support long section names at all; on
-       other formats it points to a stub that returns an error indication.
+       points to a function that allows the value of a copy of the flag
+       in coff object tdata to be altered at runtime, on formats that
+       support long section names at all; on other formats it points
+       to a stub that returns an error indication.
 
        With input BFDs, the flag is set according to whether any long section
        names are detected while reading the section headers.  For a completely
@@ -293,27 +294,30 @@ CODE_FRAGMENT
 .typedef struct coff_ptr_struct
 .{
 .  {* Remembers the offset from the first symbol in the file for
-.     this symbol. Generated by coff_renumber_symbols.  *}
+.     this symbol.  Generated by coff_renumber_symbols.  *}
 .  unsigned int offset;
 .
-.  {* Should the value of this symbol be renumbered.  Used for
-.     XCOFF C_BSTAT symbols.  Set by coff_slurp_symbol_table.  *}
-.  unsigned int fix_value : 1;
+.  {* Selects between the elements of the union below.  *}
+.  unsigned int is_sym : 1;
 .
-.  {* Should the tag field of this symbol be renumbered.
-.     Created by coff_pointerize_aux.  *}
+.  {* Selects between the elements of the x_sym.x_tagndx union.  If set,
+.     p is valid and the field will be renumbered.  *}
 .  unsigned int fix_tag : 1;
 .
-.  {* Should the endidx field of this symbol be renumbered.
-.     Created by coff_pointerize_aux.  *}
+.  {* Selects between the elements of the x_sym.x_fcnary.x_fcn.x_endndx
+.     union.  If set, p is valid and the field will be renumbered.  *}
 .  unsigned int fix_end : 1;
 .
-.  {* Should the x_csect.x_scnlen field be renumbered.
-.     Created by coff_pointerize_aux.  *}
+.  {* Selects between the elements of the x_csect.x_scnlen union.  If set,
+.     p is valid and the field will be renumbered.  *}
 .  unsigned int fix_scnlen : 1;
 .
-.  {* Fix up an XCOFF C_BINCL/C_EINCL symbol.  The value is the
-.     index into the line number entries.  Set by coff_slurp_symbol_table.  *}
+.  {* If set, u.syment.n_value contains a pointer to a symbol.  The final
+.     value will be the offset field.  Used for XCOFF C_BSTAT symbols.  *}
+.  unsigned int fix_value : 1;
+.
+.  {* If set, u.syment.n_value is an index into the line number entries.
+.     Used for XCOFF C_BINCL/C_EINCL symbols.  *}
 .  unsigned int fix_line : 1;
 .
 .  {* The container for the symbol structure as read and translated
@@ -324,8 +328,9 @@ CODE_FRAGMENT
 .    struct internal_syment syment;
 .  } u;
 .
-. {* Selector for the union above.  *}
-. bool is_sym;
+. {* An extra pointer which can used by format based on COFF (like XCOFF)
+.    to provide extra information to their backend.  *}
+. void *extrap;
 .} combined_entry_type;
 .
 .
@@ -458,15 +463,14 @@ static bool ticoff1_bad_format_hook
 static bool
 bfd_coff_set_long_section_names_allowed (bfd *abfd, int enable)
 {
-  coff_backend_info (abfd)->_bfd_coff_long_section_names = enable;
+  bfd_coff_long_section_names (abfd) = enable;
   return true;
 }
 #else /* !defined (COFF_LONG_SECTION_NAMES) */
 static bool
-bfd_coff_set_long_section_names_disallowed (bfd *abfd, int enable)
+bfd_coff_set_long_section_names_disallowed (bfd *abfd ATTRIBUTE_UNUSED,
+                                           int enable ATTRIBUTE_UNUSED)
 {
-  (void) abfd;
-  (void) enable;
   return false;
 }
 #endif /* defined (COFF_LONG_SECTION_NAMES) */
@@ -1491,9 +1495,9 @@ Special entry points for gdb to swap in coff symbol table parts:
 .    (bfd *, FILE *, combined_entry_type *, combined_entry_type *,
 .     combined_entry_type *, unsigned int);
 .
-.  void (*_bfd_coff_reloc16_extra_cases)
+.  bool (*_bfd_coff_reloc16_extra_cases)
 .    (bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
-.     bfd_byte *, unsigned int *, unsigned int *);
+.     bfd_byte *, size_t *, size_t *);
 .
 .  int (*_bfd_coff_reloc16_estimate)
 .    (bfd *, asection *, arelent *, unsigned int,
@@ -1537,7 +1541,7 @@ Special entry points for gdb to swap in coff symbol table parts:
 .} bfd_coff_backend_data;
 .
 .#define coff_backend_info(abfd) \
-.  ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
+.  ((const bfd_coff_backend_data *) (abfd)->xvec->backend_data)
 .
 .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
 .  ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
@@ -1580,7 +1584,7 @@ Special entry points for gdb to swap in coff symbol table parts:
 .#define bfd_coff_long_filenames(abfd) \
 .  (coff_backend_info (abfd)->_bfd_coff_long_filenames)
 .#define bfd_coff_long_section_names(abfd) \
-.  (coff_backend_info (abfd)->_bfd_coff_long_section_names)
+.  (coff_data (abfd)->long_section_names)
 .#define bfd_coff_set_long_section_names(abfd, enable) \
 .  ((coff_backend_info (abfd)->_bfd_coff_set_long_section_names) (abfd, enable))
 .#define bfd_coff_default_section_alignment_power(abfd) \
@@ -1948,9 +1952,15 @@ coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
       if (bfd_bread (& dst, relsz, abfd) != relsz)
        return;
 
-      coff_swap_reloc_in (abfd, &dst, &n);
+      bfd_coff_swap_reloc_in (abfd, &dst, &n);
       if (bfd_seek (abfd, oldpos, 0) != 0)
        return;
+      if (n.r_vaddr < 0x10000)
+       {
+         _bfd_error_handler (_("%pB: overflow reloc count too small"), abfd);
+         bfd_set_error (bfd_error_bad_value);
+         return;
+       }
       section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
       section->rel_filepos += relsz;
     }
@@ -2013,7 +2023,7 @@ coff_set_alignment_hook (bfd * abfd, asection * section, void * scnhdr)
       if (bfd_bread (& dst, relsz, abfd) != relsz)
        return;
 
-      coff_swap_reloc_in (abfd, &dst, &n);
+      bfd_coff_swap_reloc_in (abfd, &dst, &n);
       if (bfd_seek (abfd, oldpos, 0) != 0)
        return;
       section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
@@ -2057,6 +2067,9 @@ coff_mkobject (bfd * abfd)
   coff->relocbase = 0;
   coff->local_toc_sym_map = 0;
 
+  bfd_coff_long_section_names (abfd)
+    = coff_backend_info (abfd)->_bfd_coff_long_section_names;
+
 /*  make_abs_section(abfd);*/
 
   return true;
@@ -2215,6 +2228,18 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
        }
       break;
 #endif
+#ifdef AARCH64MAGIC
+    case AARCH64MAGIC:
+      arch = bfd_arch_aarch64;
+      machine = internal_f->f_flags & F_AARCH64_ARCHITECTURE_MASK;
+      break;
+#endif
+#ifdef LOONGARCH64MAGIC
+    case LOONGARCH64MAGIC:
+      arch = bfd_arch_loongarch;
+      machine = internal_f->f_flags & F_LOONGARCH64_ARCHITECTURE_MASK;
+      break;
+#endif
 #ifdef Z80MAGIC
     case Z80MAGIC:
       arch = bfd_arch_z80;
@@ -2437,10 +2462,11 @@ coff_pointerize_aux_hook (bfd *abfd ATTRIBUTE_UNUSED,
       && indaux + 1 == symbol->u.syment.n_numaux)
     {
       BFD_ASSERT (! aux->is_sym);
-      if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD)
+      if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD
+         && aux->u.auxent.x_csect.x_scnlen.u64 < obj_raw_syment_count (abfd))
        {
          aux->u.auxent.x_csect.x_scnlen.p =
-           table_base + aux->u.auxent.x_csect.x_scnlen.l;
+           table_base + aux->u.auxent.x_csect.x_scnlen.u64;
          aux->fix_scnlen = 1;
        }
 
@@ -2479,21 +2505,21 @@ coff_print_aux (bfd *abfd ATTRIBUTE_UNUSED,
       if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) != XTY_LD)
        {
          BFD_ASSERT (! aux->fix_scnlen);
-         fprintf (file, "val %5" BFD_VMA_FMT "d",
-                  aux->u.auxent.x_csect.x_scnlen.l);
+         fprintf (file, "val %5" PRIu64,
+                  aux->u.auxent.x_csect.x_scnlen.u64);
        }
       else
        {
          fprintf (file, "indx ");
          if (! aux->fix_scnlen)
-           fprintf (file, "%4" BFD_VMA_FMT "d",
-                    aux->u.auxent.x_csect.x_scnlen.l);
+           fprintf (file, "%4" PRIu64,
+                    aux->u.auxent.x_csect.x_scnlen.u64);
          else
            fprintf (file, "%4ld",
                     (long) (aux->u.auxent.x_csect.x_scnlen.p - table_base));
        }
       fprintf (file,
-              " prmhsh %ld snhsh %u typ %d algn %d clss %u stb %ld snstb %u",
+              " prmhsh %u snhsh %u typ %d algn %d clss %u stb %u snstb %u",
               aux->u.auxent.x_csect.x_parmhash,
               (unsigned int) aux->u.auxent.x_csect.x_snhash,
               SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp),
@@ -2674,9 +2700,11 @@ coff_write_relocs (bfd * abfd, int first_undef)
 
 #ifdef SELECT_RELOC
          /* Work out reloc type from what is required.  */
-         SELECT_RELOC (n, q->howto);
+         if (q->howto)
+           SELECT_RELOC (n, q->howto);
 #else
-         n.r_type = q->howto->type;
+         if (q->howto)
+           n.r_type = q->howto->type;
 #endif
          coff_swap_reloc_out (abfd, &n, &dst);
 
@@ -2771,6 +2799,18 @@ coff_set_flags (bfd * abfd,
       return true;
 #endif
 
+#ifdef AARCH64MAGIC
+    case bfd_arch_aarch64:
+      * magicp = AARCH64MAGIC;
+      return true;
+#endif
+
+#ifdef LOONGARCH64MAGIC
+    case bfd_arch_loongarch:
+      * magicp = LOONGARCH64MAGIC;
+      return true;
+#endif
+
 #ifdef ARMMAGIC
     case bfd_arch_arm:
 #ifdef ARM_WINCE
@@ -2940,7 +2980,7 @@ coff_compute_section_file_positions (bfd * abfd)
 #endif
 
 #ifdef COFF_IMAGE_WITH_PE
-  int page_size;
+  unsigned int page_size;
 
   if (coff_data (abfd)->link_info
       || (pe_data (abfd) && pe_data (abfd)->pe_opthdr.FileAlignment))
@@ -2951,22 +2991,12 @@ coff_compute_section_file_positions (bfd * abfd)
         This repairs 'ld -r' for arm-wince-pe target.  */
       if (page_size == 0)
        page_size = 1;
-
-      /* PR 17512: file: 0ac816d3.  */
-      if (page_size < 0)
-       {
-         bfd_set_error (bfd_error_file_too_big);
-         _bfd_error_handler
-           /* xgettext:c-format */
-           (_("%pB: page size is too large (0x%x)"), abfd, page_size);
-         return false;
-       }
     }
   else
     page_size = PE_DEF_FILE_ALIGNMENT;
 #else
 #ifdef COFF_PAGE_SIZE
-  int page_size = COFF_PAGE_SIZE;
+  unsigned int page_size = COFF_PAGE_SIZE;
 #endif
 #endif
 
@@ -3048,9 +3078,10 @@ coff_compute_section_file_positions (bfd * abfd)
     bfd_size_type amt;
 
 #ifdef COFF_PAGE_SIZE
-    /* Clear D_PAGED if section alignment is smaller than
-       COFF_PAGE_SIZE.  */
-   if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE)
+    /* Clear D_PAGED if section / file alignment aren't suitable for
+       paging at COFF_PAGE_SIZE granularity.  */
+   if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE
+       || page_size < COFF_PAGE_SIZE)
      abfd->flags &= ~D_PAGED;
 #endif
 
@@ -3171,7 +3202,11 @@ coff_compute_section_file_positions (bfd * abfd)
             padding the previous section up if necessary.  */
          old_sofar = sofar;
 
-         sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
+#ifdef COFF_IMAGE_WITH_PE
+         sofar = BFD_ALIGN (sofar, page_size);
+#else
+         sofar = BFD_ALIGN (sofar, (bfd_vma) 1 << current->alignment_power);
+#endif
 
 #ifdef RS6000COFF_C
          /* Make sure the file offset and the vma of .text/.data are at the
@@ -3240,14 +3275,18 @@ coff_compute_section_file_positions (bfd * abfd)
 
          old_size = current->size;
          current->size = BFD_ALIGN (current->size,
-                                    1 << current->alignment_power);
+                                    (bfd_vma) 1 << current->alignment_power);
          align_adjust = current->size != old_size;
          sofar += current->size - old_size;
        }
       else
        {
          old_sofar = sofar;
-         sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
+#ifdef COFF_IMAGE_WITH_PE
+         sofar = BFD_ALIGN (sofar, page_size);
+#else
+         sofar = BFD_ALIGN (sofar, (bfd_vma) 1 << current->alignment_power);
+#endif
          align_adjust = sofar != old_sofar;
          current->size += sofar - old_sofar;
        }
@@ -3292,7 +3331,8 @@ coff_compute_section_file_positions (bfd * abfd)
   /* Make sure the relocations are aligned.  We don't need to make
      sure that this byte exists, because it will only matter if there
      really are relocs.  */
-  sofar = BFD_ALIGN (sofar, 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
+  sofar = BFD_ALIGN (sofar,
+                    (bfd_vma) 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
 
   obj_relocbase (abfd) = sofar;
   abfd->output_has_begun = true;
@@ -3821,7 +3861,7 @@ coff_write_object_contents (bfd * abfd)
       bfd_byte *b = bfd_zmalloc (fill_size);
       if (b)
        {
-         bfd_bwrite ((PTR)b, fill_size, abfd);
+         bfd_bwrite (b, fill_size, abfd);
          free (b);
        }
     }
@@ -3866,7 +3906,7 @@ coff_write_object_contents (bfd * abfd)
     internal_f.f_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
 #endif
 
-#ifndef COFF_WITH_pex64
+#if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
 #ifdef COFF_WITH_PE
   internal_f.f_flags |= IMAGE_FILE_32BIT_MACHINE;
 #else
@@ -3915,6 +3955,16 @@ coff_write_object_contents (bfd * abfd)
     internal_a.magic = ZMAGIC;
 #endif
 
+#if defined(AARCH64)
+#define __A_MAGIC_SET__
+    internal_a.magic = ZMAGIC;
+#endif
+
+#if defined(LOONGARCH64)
+#define __A_MAGIC_SET__
+    internal_a.magic = ZMAGIC;
+#endif
+
 #if defined MCORE_PE
 #define __A_MAGIC_SET__
     internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
@@ -3967,8 +4017,13 @@ coff_write_object_contents (bfd * abfd)
 #endif
   }
 
+#ifdef RS6000COFF_C
+  /* XCOFF 32bit needs this to have new behaviour for n_type field.  */
+  internal_a.vstamp = 2;
+#else
   /* FIXME: Does anybody ever set this to another value?  */
   internal_a.vstamp = 0;
+#endif
 
   /* Now should write relocs, strings, syms.  */
   obj_sym_filepos (abfd) = sym_base;
@@ -4043,7 +4098,7 @@ coff_write_object_contents (bfd * abfd)
       bfd_vma toc;
       asection *loader_sec;
 
-      internal_a.vstamp = 1;
+      internal_a.vstamp = 2;
 
       internal_a.o_snentry = xcoff_data (abfd)->snentry;
       if (internal_a.o_snentry == 0)
@@ -4251,10 +4306,13 @@ coff_set_section_contents (bfd * abfd,
 
        rec = (bfd_byte *) location;
        recend = rec + count;
-       while (rec < recend)
+       while (recend - rec >= 4)
          {
+           size_t len = bfd_get_32 (abfd, rec);
+           if (len == 0 || len > (size_t) (recend - rec) / 4)
+             break;
+           rec += len * 4;
            ++section->lma;
-           rec += bfd_get_32 (abfd, rec) * 4;
          }
 
        BFD_ASSERT (rec == recend);
@@ -4288,7 +4346,7 @@ buy_and_read (bfd *abfd, file_ptr where,
     }
   if (bfd_seek (abfd, where, SEEK_SET) != 0)
     return NULL;
-  return _bfd_alloc_and_read (abfd, amt, amt);
+  return _bfd_malloc_and_read (abfd, amt, amt);
 }
 
 /*
@@ -4352,31 +4410,26 @@ coff_slurp_line_table (bfd *abfd, asection *asect)
 
   BFD_ASSERT (asect->lineno == NULL);
 
-  if (asect->lineno_count > asect->size)
+  native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos,
+                                          asect->lineno_count,
+                                          bfd_coff_linesz (abfd));
+  if (native_lineno == NULL)
     {
       _bfd_error_handler
-       (_("%pB: warning: line number count (%#lx) exceeds section size (%#lx)"),
-        abfd, (unsigned long) asect->lineno_count, (unsigned long) asect->size);
+       (_("%pB: warning: line number table read failed"), abfd);
       return false;
     }
 
   if (_bfd_mul_overflow (asect->lineno_count + 1, sizeof (alent), &amt))
     {
       bfd_set_error (bfd_error_file_too_big);
+      free (native_lineno);
       return false;
     }
   lineno_cache = (alent *) bfd_alloc (abfd, amt);
   if (lineno_cache == NULL)
-    return false;
-
-  native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos,
-                                          asect->lineno_count,
-                                          bfd_coff_linesz (abfd));
-  if (native_lineno == NULL)
     {
-      _bfd_error_handler
-       (_("%pB: warning: line number table read failed"), abfd);
-      bfd_release (abfd, lineno_cache);
+      free (native_lineno);
       return false;
     }
 
@@ -4469,7 +4522,7 @@ coff_slurp_line_table (bfd *abfd, asection *asect)
 
   asect->lineno_count = cache_ptr - lineno_cache;
   memset (cache_ptr, 0, sizeof (*cache_ptr));
-  bfd_release (abfd, native_lineno);
+  free (native_lineno);
 
   /* On some systems (eg AIX5.3) the lineno table may not be sorted.  */
   if (!ordered)
@@ -4594,7 +4647,7 @@ coff_slurp_symbol_table (bfd * abfd)
          BFD_ASSERT (src->is_sym);
          dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
          /* We use the native name field to point to the cached field.  */
-         src->u.syment._n._n_n._n_zeroes = (bfd_hostptr_t) dst;
+         src->u.syment._n._n_n._n_zeroes = (uintptr_t) dst;
          dst->symbol.section = coff_section_from_bfd_index (abfd,
                                                     src->u.syment.n_scnum);
          dst->symbol.flags = 0;
@@ -4729,6 +4782,9 @@ coff_slurp_symbol_table (bfd * abfd)
                dst->symbol.value = src->u.syment.n_value;
              break;
 
+           case C_FILE:        /* File name.  */
+             dst->symbol.flags = BSF_FILE;
+             /* Fall through.  */
            case C_MOS:         /* Member of structure.  */
            case C_EOS:         /* End of structure.  */
            case C_REGPARM:     /* Register parameter.  */
@@ -4742,11 +4798,6 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_MOE:         /* Member of enumeration.  */
            case C_MOU:         /* Member of union.  */
            case C_UNTAG:       /* Union tag.  */
-             dst->symbol.flags = BSF_DEBUGGING;
-             dst->symbol.value = (src->u.syment.n_value);
-             break;
-
-           case C_FILE:        /* File name.  */
            case C_STRTAG:      /* Structure tag.  */
 #ifdef RS6000COFF_C
            case C_GSYM:
@@ -4764,7 +4815,7 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_FUN:
            case C_ESTAT:
 #endif
-             dst->symbol.flags = BSF_DEBUGGING;
+             dst->symbol.flags |= BSF_DEBUGGING;
              dst->symbol.value = (src->u.syment.n_value);
              break;
 
@@ -4801,13 +4852,18 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_BSTAT:
              dst->symbol.flags = BSF_DEBUGGING;
 
-             /* The value is actually a symbol index.  Save a pointer
-                to the symbol instead of the index.  FIXME: This
-                should use a union.  */
-             src->u.syment.n_value =
-               (long) (intptr_t) (native_symbols + src->u.syment.n_value);
-             dst->symbol.value = src->u.syment.n_value;
-             src->fix_value = 1;
+             if (src->u.syment.n_value >= obj_raw_syment_count (abfd))
+               dst->symbol.value = 0;
+             else
+               {
+                 /* The value is actually a symbol index.  Save a pointer
+                    to the symbol instead of the index.  FIXME: This
+                    should use a union.  */
+                 src->u.syment.n_value
+                   = (uintptr_t) (native_symbols + src->u.syment.n_value);
+                 dst->symbol.value = src->u.syment.n_value;
+                 src->fix_value = 1;
+               }
              break;
 #endif
 
@@ -4977,11 +5033,11 @@ coff_classify_symbol (bfd *abfd,
         breaks gas generated objects.  */
       if (syment->n_value == 0)
        {
-         asection *sec;
-         char * name;
+         const asection *sec;
+         const char *name;
          char buf[SYMNMLEN + 1];
 
-         name = _bfd_coff_internal_syment_name (abfd, syment, buf)
+         name = _bfd_coff_internal_syment_name (abfd, syment, buf);
          sec = coff_section_from_bfd_index (abfd, syment->n_scnum);
          if (sec != NULL && name != NULL
              && (strcmp (bfd_section_name (sec), name) == 0))
@@ -5042,6 +5098,14 @@ SUBSUBSECTION
        structure, in a back end specific way. For instance, the 386
        uses the @code{r_type} to directly produce an index
        into a howto table vector.
+
+       o Note that @code{arelent.addend} for COFF is often not what
+       most people understand as a relocation addend, but rather an
+       adjustment to the relocation addend stored in section contents
+       of relocatable object files.  The value found in section
+       contents may also be confusing, depending on both symbol value
+       and addend somewhat similar to the field value for a
+       final-linked object.  See @code{CALC_ADDEND}.
 */
 
 #ifndef CALC_ADDEND
@@ -5069,7 +5133,7 @@ SUBSUBSECTION
 static bool
 coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
 {
-  RELOC *native_relocs;
+  bfd_byte *native_relocs;
   arelent *reloc_cache;
   arelent *cache_ptr;
   unsigned int idx;
@@ -5084,31 +5148,37 @@ coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
   if (!coff_slurp_symbol_table (abfd))
     return false;
 
-  native_relocs = (RELOC *) buy_and_read (abfd, asect->rel_filepos,
-                                         asect->reloc_count,
-                                         bfd_coff_relsz (abfd));
+  native_relocs = (bfd_byte *) buy_and_read (abfd, asect->rel_filepos,
+                                            asect->reloc_count,
+                                            bfd_coff_relsz (abfd));
+  if (native_relocs == NULL)
+    return false;
+
   if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
     {
       bfd_set_error (bfd_error_file_too_big);
       return false;
     }
   reloc_cache = (arelent *) bfd_alloc (abfd, amt);
-  if (reloc_cache == NULL || native_relocs == NULL)
-    return false;
+  if (reloc_cache == NULL)
+    {
+      free (native_relocs);
+      return false;
+    }
 
   for (idx = 0; idx < asect->reloc_count; idx++)
     {
       struct internal_reloc dst;
-      struct external_reloc *src;
+      void *src;
 #ifndef RELOC_PROCESSING
       asymbol *ptr;
 #endif
 
       cache_ptr = reloc_cache + idx;
-      src = native_relocs + idx;
+      src = native_relocs + idx * (size_t) bfd_coff_relsz (abfd);
 
       dst.r_offset = 0;
-      coff_swap_reloc_in (abfd, src, &dst);
+      bfd_coff_swap_reloc_in (abfd, src, &dst);
 
 #ifdef RELOC_PROCESSING
       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
@@ -5164,10 +5234,12 @@ coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
            (_("%pB: illegal relocation type %d at address %#" PRIx64),
             abfd, dst.r_type, (uint64_t) dst.r_vaddr);
          bfd_set_error (bfd_error_bad_value);
+         free (native_relocs);
          return false;
        }
     }
 
+  free (native_relocs);
   asect->relocation = reloc_cache;
   return true;
 }
@@ -5265,18 +5337,16 @@ dummy_reloc16_estimate (bfd *abfd ATTRIBUTE_UNUSED,
 
 #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
 
-/* This works even if abort is not declared in any header file.  */
-
-static void
+static bool
 dummy_reloc16_extra_cases (bfd *abfd ATTRIBUTE_UNUSED,
                           struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
                           struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
                           arelent *reloc ATTRIBUTE_UNUSED,
                           bfd_byte *data ATTRIBUTE_UNUSED,
-                          unsigned int *src_ptr ATTRIBUTE_UNUSED,
-                          unsigned int *dst_ptr ATTRIBUTE_UNUSED)
+                          size_t *src_ptr ATTRIBUTE_UNUSED,
+                          size_t *dst_ptr ATTRIBUTE_UNUSED)
 {
-  abort ();
+  return false;
 }
 #endif
 
@@ -5390,7 +5460,7 @@ coff_final_link_postscript (bfd * abfd ATTRIBUTE_UNUSED,
 #define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
 #endif
 
-static bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
+static const bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
 {
   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
   coff_SWAP_aux_out, coff_SWAP_sym_out,
@@ -5431,11 +5501,11 @@ static bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
 #ifdef TICOFF
 /* COFF0 differs in file/section header size and relocation entry size.  */
 
-static bfd_coff_backend_data ticoff0_swap_table =
+static const bfd_coff_backend_data ticoff0_swap_table =
 {
   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
   coff_SWAP_aux_out, coff_SWAP_sym_out,
-  coff_SWAP_lineno_out, coff_SWAP_reloc_out,
+  coff_SWAP_lineno_out, coff_swap_reloc_v0_out,
   coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
   coff_SWAP_scnhdr_out,
   FILHSZ_V0, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ_V0, LINESZ, FILNMLEN,
@@ -5458,7 +5528,7 @@ static bfd_coff_backend_data ticoff0_swap_table =
 #endif
   32768,
   coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
-  coff_SWAP_reloc_in, ticoff0_bad_format_hook, coff_set_arch_mach_hook,
+  coff_swap_reloc_v0_in, ticoff0_bad_format_hook, coff_set_arch_mach_hook,
   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
@@ -5473,7 +5543,7 @@ static bfd_coff_backend_data ticoff0_swap_table =
 #ifdef TICOFF
 /* COFF1 differs in section header size.  */
 
-static bfd_coff_backend_data ticoff1_swap_table =
+static const bfd_coff_backend_data ticoff1_swap_table =
 {
   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
   coff_SWAP_aux_out, coff_SWAP_sym_out,
@@ -5654,11 +5724,11 @@ coff_bigobj_swap_aux_in (bfd *abfd,
       if (numaux > 1)
        {
          if (indx == 0)
-           memcpy (in->x_file.x_fname, ext->File.Name,
+           memcpy (in->x_file.x_n.x_fname, ext->File.Name,
                    numaux * sizeof (AUXENT_BIGOBJ));
        }
       else
-       memcpy (in->x_file.x_fname, ext->File.Name, sizeof (ext->File.Name));
+       memcpy (in->x_file.x_n.x_fname, ext->File.Name, sizeof (ext->File.Name));
       break;
 
     case C_STAT:
@@ -5680,7 +5750,7 @@ coff_bigobj_swap_aux_in (bfd *abfd,
       break;
 
     default:
-      in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->Sym.WeakDefaultSymIndex);
+      in->x_sym.x_tagndx.u32 = H_GET_32 (abfd, ext->Sym.WeakDefaultSymIndex);
       /* Characteristics is ignored.  */
       break;
     }
@@ -5703,7 +5773,7 @@ coff_bigobj_swap_aux_out (bfd * abfd,
   switch (in_class)
     {
     case C_FILE:
-      memcpy (ext->File.Name, in->x_file.x_fname, sizeof (ext->File.Name));
+      memcpy (ext->File.Name, in->x_file.x_n.x_fname, sizeof (ext->File.Name));
 
       return AUXESZ;
 
@@ -5728,13 +5798,13 @@ coff_bigobj_swap_aux_out (bfd * abfd,
       break;
     }
 
-  H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->Sym.WeakDefaultSymIndex);
+  H_PUT_32 (abfd, in->x_sym.x_tagndx.u32, ext->Sym.WeakDefaultSymIndex);
   H_PUT_32 (abfd, 1, ext->Sym.WeakSearchType);
 
   return AUXESZ;
 }
 
-static bfd_coff_backend_data bigobj_swap_table =
+static const bfd_coff_backend_data bigobj_swap_table =
 {
   coff_bigobj_swap_aux_in, coff_bigobj_swap_sym_in, coff_SWAP_lineno_in,
   coff_bigobj_swap_aux_out, coff_bigobj_swap_sym_out,