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;
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 *);
}
\f
long
coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
{
- if (bfd_get_format (abfd) != bfd_object)
+ size_t count, raw;
+
+ count = asect->reloc_count;
+ if (count >= LONG_MAX / sizeof (arelent *)
+ || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw))
{
- bfd_set_error (bfd_error_invalid_operation);
+ bfd_set_error (bfd_error_file_too_big);
return -1;
}
-#if SIZEOF_LONG == SIZEOF_INT
- if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
+ if (!bfd_write_p (abfd))
{
- bfd_set_error (bfd_error_file_too_big);
- return -1;
+ ufile_ptr filesize = bfd_get_file_size (abfd);
+ if (filesize != 0 && raw > filesize)
+ {
+ bfd_set_error (bfd_error_file_truncated);
+ return -1;
+ }
}
-#endif
- return (asect->reloc_count + 1L) * sizeof (arelent *);
+ return (count + 1) * sizeof (arelent *);
}
asymbol *
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 *))
+ size_t count, raw;
+
+ count = sec->reloc_count;
+ if (count >= LONG_MAX / 2 / sizeof (arelent *)
+ || _bfd_mul_overflow (count, sizeof (Elf64_External_Rela), &raw))
{
bfd_set_error (bfd_error_file_too_big);
return -1;
}
-#endif
- return (sec->reloc_count * 2L + 1) * sizeof (arelent *);
+ 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 * 2 + 1) * sizeof (arelent *);
}
static long
}
long
-bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
- asection *asect)
+bfd_mach_o_get_reloc_upper_bound (bfd *abfd, asection *asect)
{
-#if SIZEOF_LONG == SIZEOF_INT
- if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
+ size_t count, raw;
+
+ count = asect->reloc_count;
+ if (count >= LONG_MAX / sizeof (arelent *)
+ || _bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &raw))
{
bfd_set_error (bfd_error_file_too_big);
return -1;
}
-#endif
- return (asect->reloc_count + 1L) * sizeof (arelent *);
+ 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 *);
}
/* In addition to the need to byte-swap the symbol number, the bit positions
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;
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 *);
}