Make bfd_byte an int8_t, flagword a uint32_t
[binutils-gdb.git] / bfd / pdp11.c
index 2c9fe0ae56bf9546251339b0604b4e74078dd70c..9edbba92c825dc51e89fd1371c8c5397b48b3a87 100644 (file)
@@ -1,5 +1,5 @@
 /* BFD back-end for PDP-11 a.out binaries.
-   Copyright (C) 2001-2021 Free Software Foundation, Inc.
+   Copyright (C) 2001-2023 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -280,9 +280,9 @@ static bool separate_i_d = false;
 reloc_howto_type howto_table_pdp11[] =
 {
   /* type             rs size bsz  pcrel bitpos ovrf                     sf name     part_inpl readmask  setmask    pcdone */
-HOWTO( 0,             0,  1,  16,  false, 0, complain_overflow_signed,0,"16",  true, 0x0000ffff,0x0000ffff, false),
-HOWTO( 1,             0,  1,  16,  true,  0, complain_overflow_signed,0,"DISP16",      true, 0x0000ffff,0x0000ffff, false),
-HOWTO( 2,             0,  2,  32,  false, 0, complain_overflow_signed,0,"32",  true, 0x0000ffff,0x0000ffff, false),
+HOWTO( 0,             0,  2,  16,  false, 0, complain_overflow_dont,0,"16",    true, 0x0000ffff,0x0000ffff, false),
+HOWTO( 1,             0,  2,  16,  true,  0, complain_overflow_dont,0,"DISP16",        true, 0x0000ffff,0x0000ffff, false),
+HOWTO( 2,             0,  4,  32,  false, 0, complain_overflow_dont,0,"32",    true, 0x0000ffff,0x0000ffff, false),
 };
 
 #define TABLE_SIZE(TABLE)      (sizeof(TABLE)/sizeof(TABLE[0]))
@@ -1333,8 +1333,6 @@ aout_get_external_symbols (bfd *abfd)
 
          if (stringsize >= BYTES_IN_LONG)
            {
-             /* Keep the string count in the buffer for convenience
-                when indexing with e_strx.  */
              amt = stringsize - BYTES_IN_LONG;
              if (bfd_bread (strings + BYTES_IN_LONG, amt, abfd) != amt)
                {
@@ -1344,7 +1342,8 @@ aout_get_external_symbols (bfd *abfd)
            }
        }
       /* Ensure that a zero index yields an empty string.  */
-      memset (strings, 0, BYTES_IN_LONG);
+      if (stringsize >= BYTES_IN_WORD)
+       memset (strings, 0, BYTES_IN_LONG);
 
       /* Ensure that the string buffer is NUL terminated.  */
       strings[stringsize] = 0;
@@ -1862,7 +1861,10 @@ pdp11_aout_swap_reloc_out (bfd *abfd, arelent *g, bfd_byte *natptr)
   if (r_extern)                                                                \
     {                                                                  \
       /* Undefined symbol.  */                                         \
-      cache_ptr->sym_ptr_ptr = symbols + r_index;                      \
+      if (symbols != NULL && r_index < bfd_get_symcount (abfd))                \
+       cache_ptr->sym_ptr_ptr = symbols + r_index;                     \
+      else                                                             \
+       cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;   \
       cache_ptr->addend = ad;                                          \
     }                                                                  \
   else                                                                 \
@@ -2125,13 +2127,7 @@ NAME (aout, canonicalize_reloc) (bfd *abfd,
 long
 NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
 {
-  bfd_size_type count;
-
-  if (bfd_get_format (abfd) != bfd_object)
-    {
-      bfd_set_error (bfd_error_invalid_operation);
-      return -1;
-    }
+  size_t count, raw;
 
   if (asect->flags & SEC_CONSTRUCTOR)
     count = asect->reloc_count;
@@ -2147,11 +2143,21 @@ NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
       return -1;
     }
 
-  if (count >= LONG_MAX / sizeof (arelent *))
+  if (count >= LONG_MAX / sizeof (arelent *)
+      || _bfd_mul_overflow (count, obj_reloc_entry_size (abfd), &raw))
     {
       bfd_set_error (bfd_error_file_too_big);
       return -1;
     }
+  if (!bfd_write_p (abfd))
+    {
+      ufile_ptr filesize = bfd_get_file_size (abfd);
+      if (filesize != 0 && raw > filesize)
+       {
+         bfd_set_error (bfd_error_file_truncated);
+         return -1;
+       }
+    }
   return (count + 1) * sizeof (arelent *);
 }
 
@@ -2533,12 +2539,13 @@ NAME (aout, bfd_free_cached_info) (bfd *abfd)
 {
   asection *o;
 
-  if (bfd_get_format (abfd) != bfd_object)
+  if (bfd_get_format (abfd) != bfd_object
+      || abfd->tdata.aout_data == NULL)
     return true;
 
 #define BFCI_FREE(x) do { free (x); x = NULL; } while (0)
+  BFCI_FREE (adata (abfd).line_buf);
   BFCI_FREE (obj_aout_symbols (abfd));
-
 #ifdef USE_MMAP
   obj_aout_external_syms (abfd) = 0;
   bfd_free_window (&obj_aout_sym_window (abfd));
@@ -3261,7 +3268,7 @@ aout_link_reloc_link_order (struct aout_final_link_info *flaginfo,
     r_baserel = (howto->type & 8) != 0;
     r_jmptable = (howto->type & 16) != 0;
     r_relative = (howto->type & 32) != 0;
-    r_length = howto->size;
+    r_length = bfd_log2 (bfd_get_reloc_size (howto));
 
     PUT_WORD (flaginfo->output_bfd, p->offset, srel.r_address);
     if (bfd_header_big_endian (flaginfo->output_bfd))