- if (bfd_get_file_size (abfd) > 0
- && i_ehdrp->e_phnum > bfd_get_file_size (abfd))
- goto got_no_match;
- amt = (bfd_size_type) i_ehdrp->e_phnum * sizeof (*i_phdr);
- elf_tdata (abfd)->phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
+ filesize = bfd_get_file_size (abfd);
+ if (filesize != 0
+ && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
+ goto got_wrong_format_error;
+ if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
+ goto got_wrong_format_error;
+ elf_tdata (abfd)->phdr
+ = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);