if (xlat)
{
len = strlen (xlat->bfd_name);
- res = bfd_alloc (abfd, len+1);
+ res = bfd_alloc (abfd, len + 1);
if (res == NULL)
return;
memcpy (res, xlat->bfd_name, len+1);
ody->export_size = idy->export_size;
ody->export_content = idy->export_content;
}
+ /* PR 17512L: file: 730e492d. */
+ else
+ {
+ ody->rebase_size =
+ ody->bind_size =
+ ody->weak_bind_size =
+ ody->lazy_bind_size =
+ ody->export_size = 0;
+ ody->rebase_content =
+ ody->bind_content =
+ ody->weak_bind_content =
+ ody->lazy_bind_content =
+ ody->export_content = NULL;
+ }
}
break;
if (reloc.r_extern)
{
/* PR 17512: file: 8396-1185-0.004. */
- if (bfd_get_symcount (abfd) > 0 && num > bfd_get_symcount (abfd))
+ if (num >= (unsigned) bfd_mach_o_count_symbols (abfd))
sym = bfd_und_section_ptr->symbol_ptr_ptr;
else if (syms == NULL)
sym = bfd_und_section_ptr->symbol_ptr_ptr;
/* Allocate and read relocs. */
native_size = count * BFD_MACH_O_RELENT_SIZE;
+ /* PR 17512: file: 09477b57. */
+ if (native_size < count)
+ return -1;
+
native_relocs =
(struct mach_o_reloc_info_external *) bfd_malloc (native_size);
if (native_relocs == NULL)
if (asect->relocation == NULL)
{
+ if (asect->reloc_count * sizeof (arelent) < asect->reloc_count)
+ return -1;
res = bfd_malloc (asect->reloc_count * sizeof (arelent));
if (res == NULL)
return -1;
if (mdata->dyn_reloc_cache == NULL)
{
+ if ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent)
+ < (dysymtab->nextrel + dysymtab->nlocrel))
+ return -1;
+
res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel)
* sizeof (arelent));
if (res == NULL)
mdata->filelen += sym->strsize;
if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
- return FALSE;
+ goto err;
if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
goto err;
- _bfd_stringtab_free (strtab);
/* Pad string table. */
padlen = bfd_mach_o_pad4 (abfd, sym->strsize);
err:
_bfd_stringtab_free (strtab);
+ sym->strsize = 0;
return FALSE;
}
cmd->indirectsymoff = mdata->filelen;
mdata->filelen += cmd->nindirectsyms * 4;
+ if (cmd->nindirectsyms * 4 < cmd->nindirectsyms)
+ return FALSE;
cmd->indirect_syms = bfd_zalloc (abfd, cmd->nindirectsyms * 4);
if (cmd->indirect_syms == NULL)
return FALSE;
}
mdata->nsects = nsect;
- mdata->sections = bfd_alloc (abfd,
- mdata->nsects * sizeof (bfd_mach_o_section *));
+ mdata->sections = bfd_alloc2 (abfd,
+ mdata->nsects, sizeof (bfd_mach_o_section *));
if (mdata->sections == NULL)
return FALSE;
bfd_mach_o_append_section_to_segment (seg, s);
- BFD_ASSERT (s->addr >= vma);
+ if (s->addr < vma)
+ {
+ (*_bfd_error_handler)
+ (_("section address (%lx) below start of segment (%lx)"),
+ (unsigned long) s->addr, (unsigned long) vma);
+ return FALSE;
+ }
+
vma = s->addr + s->size;
}
/* Layout the commands: set commands size and offset, set ncmds and sizeofcmds
fields in header. */
-static void
+static bfd_boolean
bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata)
{
unsigned wide = mach_o_wide_p (&mdata->header);
ufile_ptr offset;
bfd_mach_o_load_command *cmd;
unsigned int align;
+ bfd_boolean ret = TRUE;
hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
align = wide ? 8 - 1 : 4 - 1;
(*_bfd_error_handler)
(_("unable to layout unknown load command 0x%lx"),
(unsigned long) cmd->type);
+ ret = FALSE;
break;
}
}
mdata->header.sizeofcmds = offset - hdrlen;
mdata->filelen = offset;
+
+ return ret;
}
/* Subroutine of bfd_mach_o_build_commands: set type, name and nsects of a
if (nbr_commands == 0)
{
/* Layout commands (well none...) and set headers command fields. */
- bfd_mach_o_layout_commands (mdata);
- return TRUE;
+ return bfd_mach_o_layout_commands (mdata);
}
/* Create commands for segments (and symtabs), prepend them. */
}
/* Layout commands. */
- bfd_mach_o_layout_commands (mdata);
+ if (! bfd_mach_o_layout_commands (mdata))
+ return FALSE;
/* So, now we have sized the commands and the filelen set to that.
Now we can build the segment command and set the section file offsets. */
section->size = bfd_h_get_32 (abfd, raw.size);
section->offset = bfd_h_get_32 (abfd, raw.offset);
section->align = bfd_h_get_32 (abfd, raw.align);
+ /* PR 17512: file: 0017eb76. */
+ if (section->align > 64)
+ {
+ (*_bfd_error_handler) (_("bfd_mach_o_read_section_32: overlarge alignment value: 0x%x, using 32 instead"),
+ section->align);
+ section->align = 32;
+ }
section->reloff = bfd_h_get_32 (abfd, raw.reloff);
section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
section->flags = bfd_h_get_32 (abfd, raw.flags);
section->size = bfd_h_get_64 (abfd, raw.size);
section->offset = bfd_h_get_32 (abfd, raw.offset);
section->align = bfd_h_get_32 (abfd, raw.align);
+ if (section->align > 64)
+ {
+ (*_bfd_error_handler) (_("bfd_mach_o_read_section_64: overlarge alignment value: 0x%x, using 32 instead"),
+ section->align);
+ section->align = 32;
+ }
section->reloff = bfd_h_get_32 (abfd, raw.reloff);
section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
section->flags = bfd_h_get_32 (abfd, raw.flags);
}
else
{
- sym->strtab = bfd_alloc (abfd, sym->strsize);
+ sym->strtab = bfd_alloc (abfd, sym->strsize + 1);
if (sym->strtab == NULL)
return FALSE;
bfd_set_error (bfd_error_file_truncated);
return FALSE;
}
+ /* Zero terminate the string table. */
+ sym->strtab[sym->strsize] = 0;
}
return TRUE;
/* Return now if there are no symbols or if already loaded. */
return TRUE;
- sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
-
+ sym->symbols = bfd_alloc2 (abfd, sym->nsyms, sizeof (bfd_mach_o_asymbol));
if (sym->symbols == NULL)
{
(*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
+ sym->nsyms = 0;
return FALSE;
}
if (!bfd_mach_o_read_symtab_strtab (abfd))
- {
- bfd_release (abfd, sym->symbols);
- sym->symbols = NULL;
- return FALSE;
- }
+ goto fail;
for (i = 0; i < sym->nsyms; i++)
- {
- if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
- {
- bfd_release (abfd, sym->symbols);
- sym->symbols = NULL;
- return FALSE;
- }
- }
+ if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
+ goto fail;
return TRUE;
+
+ fail:
+ bfd_release (abfd, sym->symbols);
+ sym->symbols = NULL;
+ sym->nsyms = 0;
+ return FALSE;
}
static const char *
}
/* Allocate threads. */
- cmd->flavours = bfd_alloc
- (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
+ cmd->flavours = bfd_alloc2
+ (abfd, nflavours, sizeof (bfd_mach_o_thread_flavour));
if (cmd->flavours == NULL)
return FALSE;
cmd->nflavours = nflavours;
unsigned int module_len = wide ? 56 : 52;
cmd->dylib_module =
- bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
+ bfd_alloc2 (abfd, cmd->nmodtab, sizeof (bfd_mach_o_dylib_module));
if (cmd->dylib_module == NULL)
return FALSE;
if (cmd->ntoc != 0)
{
- unsigned int i;
+ unsigned long i;
- cmd->dylib_toc = bfd_alloc
- (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
+ cmd->dylib_toc = bfd_alloc2
+ (abfd, cmd->ntoc, sizeof (bfd_mach_o_dylib_table_of_content));
if (cmd->dylib_toc == NULL)
return FALSE;
{
unsigned int i;
- cmd->indirect_syms = bfd_alloc
- (abfd, cmd->nindirectsyms * sizeof (unsigned int));
+ cmd->indirect_syms = bfd_alloc2
+ (abfd, cmd->nindirectsyms, sizeof (unsigned int));
if (cmd->indirect_syms == NULL)
return FALSE;
unsigned long v;
unsigned int i;
- cmd->ext_refs = bfd_alloc
- (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
+ cmd->ext_refs = bfd_alloc2
+ (abfd, cmd->nextrefsyms, sizeof (bfd_mach_o_dylib_reference));
if (cmd->ext_refs == NULL)
return FALSE;
return FALSE;
break;
default:
- {
- static bfd_boolean unknown_set = FALSE;
- static unsigned long unknown_command = 0;
-
- /* Prevent reams of error messages when parsing corrupt binaries. */
- if (!unknown_set)
- unknown_set = TRUE;
- else if (command->type == unknown_command)
- break;
- unknown_command = command->type;
-
- (*_bfd_error_handler)(_("%B: unknown load command 0x%lx"),
- abfd, (unsigned long) command->type);
- break;
- }
+ command->len = 0;
+ (*_bfd_error_handler)(_("%B: unknown load command 0x%lx"),
+ abfd, (unsigned long) command->type);
+ return FALSE;
}
return TRUE;
}
/* Allocate sections array. */
- mdata->sections = bfd_alloc (abfd,
- mdata->nsects * sizeof (bfd_mach_o_section *));
+ mdata->sections = bfd_alloc2 (abfd,
+ mdata->nsects, sizeof (bfd_mach_o_section *));
/* Fill the array. */
csect = 0;
mdata->first_command = NULL;
mdata->last_command = NULL;
- cmd = bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
+
+ cmd = bfd_alloc2 (abfd, header->ncmds, sizeof (bfd_mach_o_load_command));
if (cmd == NULL)
return FALSE;
goto error;
adata->archentries =
- bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
+ bfd_alloc2 (abfd, adata->nfat_arch, sizeof (mach_o_fat_archentry));
if (adata->archentries == NULL)
goto error;
}
abfd->tdata.mach_o_fat_data = adata;
+
return abfd->xvec;
error: