correct ft32 reloc range test
[deliverable/binutils-gdb.git] / bfd / ieee.c
index 73b3f98d477a1c4ac20eda66ac6eba80240a7b93..7285ee5b552f8559d6b706263a8537d8b63ac63f 100644 (file)
@@ -1,5 +1,5 @@
 /* BFD back-end for ieee-695 objects.
-   Copyright (C) 1990-2016 Free Software Foundation, Inc.
+   Copyright (C) 1990-2018 Free Software Foundation, Inc.
 
    Written by Steve Chamberlain of Cygnus Support.
 
@@ -154,9 +154,9 @@ ieee_write_id (bfd *abfd, const char *id)
     }
   else
     {
-      (*_bfd_error_handler)
-       (_("%s: string too long (%d chars, max 65535)"),
-        bfd_get_filename (abfd), length);
+      _bfd_error_handler
+       /* xgettext:c-format */
+       (_("%pB: string too long (%ld chars, max 65535)"), abfd, (long) length);
       bfd_set_error (bfd_error_invalid_operation);
       return FALSE;
     }
@@ -169,8 +169,8 @@ ieee_write_id (bfd *abfd, const char *id)
 /* Functions for reading from ieee files in the strange way that the
    standard requires.  */
 
-#define this_byte(ieee)           *((ieee)->input_p)
-#define this_byte_and_next(ieee) (*((ieee)->input_p++))
+#define this_byte(ieee)                  *((ieee)->input_p)
+#define this_byte_and_next(ieee) ((ieee)->input_p < (ieee)->end_p ? *((ieee)->input_p++) : 0)
 
 static bfd_boolean
 next_byte (common_header_type * ieee)
@@ -220,6 +220,15 @@ read_id (common_header_type *ieee)
       length = (length * 256) + this_byte_and_next (ieee);
     }
 
+  /* PR 21612: Check for an invalid length.  */
+  if (ieee->input_p + length >= ieee->end_p)
+    {
+      _bfd_error_handler (_("IEEE parser: string length: %#lx longer than buffer: %#lx"),
+                         (long) length, (long) (ieee->end_p - ieee->input_p));
+      bfd_set_error (bfd_error_invalid_operation);
+      return NULL;
+    }
+
   /* Buy memory and read string.  */
   string = bfd_alloc (ieee->abfd, (bfd_size_type) length + 1);
   if (!string)
@@ -288,10 +297,10 @@ ieee_write_expression (bfd *abfd,
            }
          else
            {
-             (*_bfd_error_handler)
-               (_("%s: unrecognized symbol `%s' flags 0x%x"),
-                bfd_get_filename (abfd), bfd_asymbol_name (symbol),
-                symbol->flags);
+             _bfd_error_handler
+               /* xgettext:c-format */
+               (_("%pB: unrecognized symbol `%s' flags 0x%x"),
+                abfd, bfd_asymbol_name (symbol), symbol->flags);
              bfd_set_error (bfd_error_invalid_operation);
              return FALSE;
            }
@@ -699,12 +708,12 @@ ieee_seek (ieee_data_type * ieee, file_ptr offset)
   if (offset < 0 || (bfd_size_type) offset >= ieee->h.total_amt)
     {
       ieee->h.input_p = ieee->h.first_byte + ieee->h.total_amt;
-      ieee->h.last_byte = ieee->h.input_p;
+      ieee->h.end_p = ieee->h.last_byte = ieee->h.input_p;
       return FALSE;
     }
 
   ieee->h.input_p = ieee->h.first_byte + offset;
-  ieee->h.last_byte = (ieee->h.first_byte + ieee_part_after (ieee, offset));
+  ieee->h.end_p = ieee->h.last_byte = (ieee->h.first_byte + ieee_part_after (ieee, offset));
   return TRUE;
 }
 
@@ -823,8 +832,9 @@ ieee_slurp_external_symbols (bfd *abfd)
                    parse_int (&ieee->h, &value);
                    break;
                  default:
-                   (*_bfd_error_handler)
-                     (_("%B: unimplemented ATI record %u for symbol %u"),
+                   _bfd_error_handler
+                     /* xgettext:c-format */
+                     (_("%pB: unimplemented ATI record %u for symbol %u"),
                       abfd, symbol_attribute_def, symbol_name_index);
                    bfd_set_error (bfd_error_bad_value);
                    return FALSE;
@@ -847,9 +857,10 @@ ieee_slurp_external_symbols (bfd *abfd)
                parse_int (&ieee->h, &value);
                if (value != 0x3f)
                  {
-                   (*_bfd_error_handler)
-                     (_("%B: unexpected ATN type %d in external part"),
-                        abfd, (int) value);
+                   _bfd_error_handler
+                     /* xgettext:c-format */
+                     (_("%pB: unexpected ATN type %" PRId64 " in external part"),
+                        abfd, (int64_t) value);
                    bfd_set_error (bfd_error_bad_value);
                    return FALSE;
                  }
@@ -869,8 +880,8 @@ ieee_slurp_external_symbols (bfd *abfd)
                        break;
 
                      default:
-                       (*_bfd_error_handler)
-                         (_("%B: unexpected type after ATN"), abfd);
+                       _bfd_error_handler
+                         (_("%pB: unexpected type after ATN"), abfd);
                        bfd_set_error (bfd_error_bad_value);
                        return FALSE;
                      }
@@ -902,9 +913,9 @@ ieee_slurp_external_symbols (bfd *abfd)
              return FALSE;
 
            /* Fully linked IEEE-695 files tend to give every symbol
-               an absolute value.  Try to convert that back into a
-               section relative value.  FIXME: This won't always to
-               the right thing.  */
+              an absolute value.  Try to convert that back into a
+              section relative value.  FIXME: This won't always to
+              the right thing.  */
            if (bfd_is_abs_section (symbol->symbol.section)
                && (abfd->flags & HAS_RELOC) == 0)
              {
@@ -1235,6 +1246,8 @@ ieee_slurp_sections (bfd *abfd)
 
                /* Read section name, use it if non empty.  */
                name = read_id (&ieee->h);
+               if (name == NULL)
+                 return FALSE;
                if (name[0])
                  section->name = name;
 
@@ -1353,7 +1366,7 @@ ieee_archive_p (bfd *abfd)
 {
   char *library;
   unsigned int i;
-  unsigned char buffer[512];
+  static unsigned char buffer[512];
   file_ptr buffer_offset = 0;
   ieee_ar_data_type *save = abfd->tdata.ieee_ar_data;
   ieee_ar_data_type *ieee;
@@ -1373,6 +1386,8 @@ ieee_archive_p (bfd *abfd)
 
   ieee->h.first_byte = buffer;
   ieee->h.input_p = buffer;
+  ieee->h.total_amt = sizeof (buffer);
+  ieee->h.end_p = buffer + sizeof (buffer);
 
   ieee->h.abfd = abfd;
 
@@ -1382,6 +1397,8 @@ ieee_archive_p (bfd *abfd)
   (void) next_byte (&(ieee->h));
 
   library = read_id (&(ieee->h));
+  if (library == NULL)
+    goto got_wrong_format_error;
   if (strcmp (library, "LIBRARY") != 0)
     goto got_wrong_format_error;
 
@@ -1440,6 +1457,8 @@ ieee_archive_p (bfd *abfd)
          bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd);
          ieee->h.first_byte = buffer;
          ieee->h.input_p = buffer;
+         ieee->h.total_amt = sizeof (buffer);
+         ieee->h.end_p = buffer + sizeof (buffer);
        }
     }
 
@@ -1463,6 +1482,8 @@ ieee_archive_p (bfd *abfd)
       bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd);
       ieee->h.first_byte = buffer;
       ieee->h.input_p = buffer;
+      ieee->h.total_amt = sizeof (buffer);
+      ieee->h.end_p = buffer + sizeof (buffer);
 
       (void) next_byte (&(ieee->h));   /* Drop F8.  */
       if (! next_byte (&(ieee->h)))    /* Drop 14.  */
@@ -1871,7 +1892,7 @@ ieee_object_p (bfd *abfd)
   char *processor;
   unsigned int part;
   ieee_data_type *ieee;
-  unsigned char buffer[300];
+  static unsigned char buffer[300];
   ieee_data_type *save = IEEE_DATA (abfd);
   bfd_size_type amt;
 
@@ -1888,6 +1909,8 @@ ieee_object_p (bfd *abfd)
 
   ieee->h.input_p = buffer;
   ieee->h.total_amt = sizeof (buffer);
+  ieee->h.end_p = buffer + sizeof (buffer);
+
   if (this_byte_and_next (&(ieee->h)) != Module_Beginning)
     goto got_wrong_format;
 
@@ -1903,9 +1926,13 @@ ieee_object_p (bfd *abfd)
   ieee->section_table_size = 0;
 
   processor = ieee->mb.processor = read_id (&(ieee->h));
+  if (processor == NULL)
+    goto got_wrong_format;
   if (strcmp (processor, "LIBRARY") == 0)
     goto got_wrong_format;
   ieee->mb.module_name = read_id (&(ieee->h));
+  if (ieee->mb.module_name == NULL)
+    goto got_wrong_format;
   if (abfd->filename == (const char *) NULL)
     abfd->filename = xstrdup (ieee->mb.module_name);
 
@@ -1940,7 +1967,7 @@ ieee_object_p (bfd *abfd)
              case '4':
                if (processor[4] == '9')    /* 68349 */
                  strcpy (family, "68030"); /* CPU030 */
-               else                        /* 68340, 68341 */
+               else                        /* 68340, 68341 */
                  strcpy (family, "68332"); /* CPU32 and CPU32+ */
                break;
 
@@ -1949,7 +1976,7 @@ ieee_object_p (bfd *abfd)
              }
          }
        else if (TOUPPER (processor[3]) == 'F')  /* 68F333 */
-         strcpy (family, "68332");                /* CPU32 */
+         strcpy (family, "68332");                /* CPU32 */
        else if ((TOUPPER (processor[3]) == 'C') /* Embedded controllers.  */
                 && ((TOUPPER (processor[2]) == 'E')
                     || (TOUPPER (processor[2]) == 'H')
@@ -2597,10 +2624,13 @@ write_int (int value)
        {
        case 4:
          OUT (value >> 24);
+         /* Fall through.  */
        case 3:
          OUT (value >> 16);
+         /* Fall through.  */
        case 2:
          OUT (value >> 8);
+         /* Fall through.  */
        case 1:
          OUT (value);
        }
@@ -2753,15 +2783,19 @@ drop_int (struct output_buffer_struct *buf)
        case 0x84:
          ch = THIS ();
          NEXT ();
+         /* Fall through.  */
        case 0x83:
          ch = THIS ();
          NEXT ();
+         /* Fall through.  */
        case 0x82:
          ch = THIS ();
          NEXT ();
+         /* Fall through.  */
        case 0x81:
          ch = THIS ();
          NEXT ();
+         /* Fall through.  */
        case 0x80:
          break;
        }
@@ -2791,18 +2825,22 @@ copy_int (void)
          ch = THIS ();
          NEXT ();
          OUT (ch);
+         /* Fall through.  */
        case 0x83:
          ch = THIS ();
          NEXT ();
          OUT (ch);
+         /* Fall through.  */
        case 0x82:
          ch = THIS ();
          NEXT ();
          OUT (ch);
+         /* Fall through.  */
        case 0x81:
          ch = THIS ();
          NEXT ();
          OUT (ch);
+         /* Fall through.  */
        case 0x80:
          break;
        }
@@ -2833,12 +2871,15 @@ copy_till_end (void)
        case 0x84:
          OUT (THIS ());
          NEXT ();
+         /* Fall through.  */
        case 0x83:
          OUT (THIS ());
          NEXT ();
+         /* Fall through.  */
        case 0x82:
          OUT (THIS ());
          NEXT ();
+         /* Fall through.  */
        case 0x81:
          OUT (THIS ());
          NEXT ();
@@ -3268,7 +3309,7 @@ ieee_write_data_part (bfd *abfd)
   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
     {
       /* Skip sections that have no loadable contents (.bss,
-         debugging, etc.)  */
+        debugging, etc.)  */
       if ((s->flags & SEC_LOAD) == 0)
        continue;
 
@@ -3331,7 +3372,7 @@ ieee_set_section_contents (bfd *abfd,
            return FALSE;
        }
       /* bfd_set_section_contents has already checked that everything
-         is within range.  */
+        is within range.  */
       memcpy (section->contents + offset, location, (size_t) count);
       return TRUE;
     }
@@ -3450,7 +3491,7 @@ ieee_write_external_part (bfd *abfd)
          else
            {
              /* This can happen - when there are gaps in the symbols read
-                from an input ieee file.  */
+                from an input ieee file.  */
            }
        }
     }
@@ -3769,7 +3810,7 @@ ieee_openr_next_archived_file (bfd *arch, bfd *prev)
 }
 
 #define ieee_find_nearest_line _bfd_nosymbols_find_nearest_line
-#define ieee_find_line         _bfd_nosymbols_find_line
+#define ieee_find_line        _bfd_nosymbols_find_line
 #define ieee_find_inliner_info _bfd_nosymbols_find_inliner_info
 
 static int
@@ -3812,26 +3853,20 @@ ieee_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
 #define        ieee_close_and_cleanup _bfd_generic_close_and_cleanup
 #define ieee_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
 
-#define ieee_slurp_armap bfd_true
-#define ieee_slurp_extended_name_table bfd_true
+#define ieee_slurp_armap _bfd_bool_bfd_true
+#define ieee_slurp_extended_name_table _bfd_bool_bfd_true
 #define ieee_construct_extended_name_table \
-  ((bfd_boolean (*) \
-    (bfd *, char **, bfd_size_type *, const char **)) \
-   bfd_true)
+  _bfd_noarchive_construct_extended_name_table
 #define ieee_truncate_arname bfd_dont_truncate_arname
-#define ieee_write_armap \
-  ((bfd_boolean (*) \
-    (bfd *, unsigned int, struct orl *, unsigned int, int)) \
-   bfd_true)
-#define ieee_read_ar_hdr bfd_nullvoidptr
-#define ieee_write_ar_hdr ((bfd_boolean (*) (bfd *, bfd *)) bfd_false)
-#define ieee_update_armap_timestamp bfd_true
+#define ieee_write_armap _bfd_noarchive_write_armap
+#define ieee_read_ar_hdr _bfd_ptr_bfd_null_error
+#define ieee_write_ar_hdr _bfd_noarchive_write_ar_hdr
+#define ieee_update_armap_timestamp _bfd_bool_bfd_true
 #define ieee_get_elt_at_index _bfd_generic_get_elt_at_index
 
 #define ieee_get_symbol_version_string \
   _bfd_nosymbols_get_symbol_version_string
-#define ieee_bfd_is_target_special_symbol  \
-  ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
+#define ieee_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
 #define ieee_bfd_is_local_label_name bfd_generic_is_local_label_name
 #define ieee_get_lineno _bfd_nosymbols_get_lineno
 #define ieee_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
@@ -3856,6 +3891,7 @@ ieee_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
 #define ieee_section_already_linked \
   _bfd_generic_section_already_linked
 #define ieee_bfd_define_common_symbol bfd_generic_define_common_symbol
+#define ieee_bfd_define_start_stop bfd_generic_define_start_stop
 #define ieee_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
 #define ieee_bfd_link_add_symbols _bfd_generic_link_add_symbols
 #define ieee_bfd_link_just_syms _bfd_generic_link_just_syms
@@ -3864,6 +3900,7 @@ ieee_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
 #define ieee_bfd_final_link _bfd_generic_final_link
 #define ieee_bfd_link_split_section  _bfd_generic_link_split_section
 #define ieee_bfd_link_check_relocs   _bfd_generic_link_check_relocs
+#define ieee_set_reloc              _bfd_generic_set_reloc
 
 const bfd_target ieee_vec =
 {
@@ -3893,16 +3930,16 @@ const bfd_target ieee_vec =
    _bfd_dummy_target,
   },
   {
-    bfd_false,
+    _bfd_bool_bfd_false_error,
     ieee_mkobject,
     _bfd_generic_mkarchive,
-    bfd_false
+    _bfd_bool_bfd_false_error
   },
   {
-    bfd_false,
+    _bfd_bool_bfd_false_error,
     ieee_write_object_contents,
     _bfd_write_archive_contents,
-    bfd_false,
+    _bfd_bool_bfd_false_error,
   },
 
   /* ieee_close_and_cleanup, ieee_bfd_free_cached_info, ieee_new_section_hook,
This page took 0.029281 seconds and 4 git commands to generate.