PR24704, Internal error building skiboot for powerpc64-linux-gnu
[external/binutils.git] / bfd / elf64-sparc.c
index bd29be3..50b051a 100644 (file)
@@ -19,6 +19,7 @@
    MA 02110-1301, USA.  */
 
 #include "sysdep.h"
+#include <limits.h>
 #include "bfd.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
 static long
 elf64_sparc_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
 {
+#if SIZEOF_LONG == SIZEOF_INT
+  if (sec->reloc_count >= LONG_MAX / 2 / sizeof (arelent *))
+    {
+      bfd_set_error (bfd_error_file_too_big);
+      return -1;
+    }
+#endif
   return (sec->reloc_count * 2 + 1) * sizeof (arelent *);
 }
 
 static long
 elf64_sparc_get_dynamic_reloc_upper_bound (bfd *abfd)
 {
-  return _bfd_elf_get_dynamic_reloc_upper_bound (abfd) * 2;
+  long ret = _bfd_elf_get_dynamic_reloc_upper_bound (abfd);
+  if (ret > LONG_MAX / 2)
+    {
+      bfd_set_error (bfd_error_file_too_big);
+      ret = -1;
+    }
+  else if (ret > 0)
+    ret *= 2;
+  return ret;
 }
 
 /* Read  relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
@@ -100,17 +116,17 @@ elf64_sparc_slurp_one_reloc_table (bfd *abfd, asection *asect,
       if (ELF64_R_SYM (rela.r_info) == STN_UNDEF)
        relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
       else if (/* PR 17512: file: 996185f8.  */
-               (!dynamic && ELF64_R_SYM(rela.r_info) > bfd_get_symcount(abfd))
-               || (dynamic
-                   && ELF64_R_SYM(rela.r_info) > bfd_get_dynamic_symcount(abfd)))
-        {
-          _bfd_error_handler
+              ELF64_R_SYM (rela.r_info) > (dynamic
+                                           ? bfd_get_dynamic_symcount (abfd)
+                                           : bfd_get_symcount (abfd)))
+       {
+         _bfd_error_handler
            /* xgettext:c-format */
            (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
             abfd, asect, i, (long) ELF64_R_SYM (rela.r_info));
          bfd_set_error (bfd_error_bad_value);
          relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
-        }
+       }
       else
        {
          asymbol **ps, *s;