X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=bfd%2Fmach-o.c;h=e32b7873cef6b78f2ff994ef550e160a49710a96;hb=df2a5f8de7dd943e0f8773c220fad000e49b78a6;hp=5f10a0a49e1fa77f96bc2a757c4f18dbac4300eb;hpb=b3adc24a0713411ab38a21dc894dd40dbc5c8f4f;p=binutils-gdb.git diff --git a/bfd/mach-o.c b/bfd/mach-o.c index 5f10a0a49e1..e32b7873cef 100644 --- a/bfd/mach-o.c +++ b/bfd/mach-o.c @@ -1,5 +1,5 @@ /* Mach-O support for BFD. - Copyright (C) 1999-2020 Free Software Foundation, Inc. + Copyright (C) 1999-2022 Free Software Foundation, Inc. This file is part of BFD, the Binary File Descriptor library. @@ -36,9 +36,9 @@ #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject #define FILE_ALIGN(off, algn) \ - (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1U << (algn))) + (((off) + ((ufile_ptr) 1 << (algn)) - 1) & ((ufile_ptr) -1 << (algn))) -static bfd_boolean +static bool bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd); unsigned int @@ -52,36 +52,36 @@ bfd_mach_o_version (bfd *abfd) return mdata->header.version; } -bfd_boolean +bool bfd_mach_o_valid (bfd *abfd) { if (abfd == NULL || abfd->xvec == NULL) - return FALSE; + return false; if (abfd->xvec->flavour != bfd_target_mach_o_flavour) - return FALSE; + return false; if (bfd_mach_o_get_data (abfd) == NULL) - return FALSE; - return TRUE; + return false; + return true; } -static INLINE bfd_boolean +static inline bool mach_o_wide_p (bfd_mach_o_header *header) { switch (header->version) { case 1: - return FALSE; + return false; case 2: - return TRUE; + return true; default: BFD_FAIL (); - return FALSE; + return false; } } -static INLINE bfd_boolean +static inline bool bfd_mach_o_wide_p (bfd *abfd) { return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header); @@ -551,7 +551,7 @@ bfd_mach_o_append_command (bfd *abfd, bfd_mach_o_load_command *cmd) /* Copy any private info we understand from the input symbol to the output symbol. */ -bfd_boolean +bool bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED, asymbol *isymbol, bfd *obfd ATTRIBUTE_UNUSED, @@ -566,13 +566,13 @@ bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED, os->n_desc = is->n_desc; os->symbol.udata.i = is->symbol.udata.i; - return TRUE; + return true; } /* Copy any private info we understand from the input section to the output section. */ -bfd_boolean +bool bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection, bfd *obfd, asection *osection) { @@ -581,7 +581,7 @@ bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection, if (ibfd->xvec->flavour != bfd_target_mach_o_flavour || obfd->xvec->flavour != bfd_target_mach_o_flavour) - return TRUE; + return true; BFD_ASSERT (is != NULL && os != NULL); @@ -590,7 +590,7 @@ bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection, os->reserved2 = is->reserved2; os->reserved3 = is->reserved3; - return TRUE; + return true; } static const char * @@ -618,12 +618,10 @@ cputype (unsigned long value) } static const char * -cpusubtype (unsigned long cputype, unsigned long cpusubtype) +cpusubtype (unsigned long cpu_type, unsigned long cpu_subtype, char *buffer) { - static char buffer[128]; - buffer[0] = 0; - switch (cpusubtype & BFD_MACH_O_CPU_SUBTYPE_MASK) + switch (cpu_subtype & BFD_MACH_O_CPU_SUBTYPE_MASK) { case 0: break; @@ -633,13 +631,13 @@ cpusubtype (unsigned long cputype, unsigned long cpusubtype) sprintf (buffer, _("")); break; } - cpusubtype &= ~ BFD_MACH_O_CPU_SUBTYPE_MASK; + cpu_subtype &= ~ BFD_MACH_O_CPU_SUBTYPE_MASK; - switch (cputype) + switch (cpu_type) { case BFD_MACH_O_CPU_TYPE_X86_64: case BFD_MACH_O_CPU_TYPE_I386: - switch (cpusubtype) + switch (cpu_subtype) { case BFD_MACH_O_CPU_SUBTYPE_X86_ALL: return strcat (buffer, " (X86_ALL)"); @@ -649,7 +647,7 @@ cpusubtype (unsigned long cputype, unsigned long cpusubtype) break; case BFD_MACH_O_CPU_TYPE_ARM: - switch (cpusubtype) + switch (cpu_subtype) { case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL: return strcat (buffer, " (ARM_ALL)"); @@ -669,7 +667,7 @@ cpusubtype (unsigned long cputype, unsigned long cpusubtype) break; case BFD_MACH_O_CPU_TYPE_ARM64: - switch (cpusubtype) + switch (cpu_subtype) { case BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL: return strcat (buffer, " (ARM64_ALL)"); @@ -684,37 +682,38 @@ cpusubtype (unsigned long cputype, unsigned long cpusubtype) break; } - if (cpusubtype != 0) + if (cpu_subtype != 0) return strcat (buffer, _(" ()")); return buffer; } -bfd_boolean +bool bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, void *ptr) { FILE * file = (FILE *) ptr; bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); + char buff[128]; fprintf (file, _(" MACH-O header:\n")); fprintf (file, _(" magic: %#lx\n"), (long) mdata->header.magic); fprintf (file, _(" cputype: %#lx (%s)\n"), (long) mdata->header.cputype, cputype (mdata->header.cputype)); fprintf (file, _(" cpusubtype: %#lx%s\n"), (long) mdata->header.cpusubtype, - cpusubtype (mdata->header.cputype, mdata->header.cpusubtype)); + cpusubtype (mdata->header.cputype, mdata->header.cpusubtype, buff)); fprintf (file, _(" filetype: %#lx\n"), (long) mdata->header.filetype); fprintf (file, _(" ncmds: %#lx\n"), (long) mdata->header.ncmds); fprintf (file, _(" sizeocmds: %#lx\n"), (long) mdata->header.sizeofcmds); fprintf (file, _(" flags: %#lx\n"), (long) mdata->header.flags); fprintf (file, _(" version: %x\n"), mdata->header.version); - return TRUE; + return true; } /* Copy any private info we understand from the input bfd to the output bfd. */ -bfd_boolean +bool bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd) { bfd_mach_o_data_struct *imdata; @@ -723,7 +722,7 @@ bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd) if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour) - return TRUE; + return true; BFD_ASSERT (bfd_mach_o_valid (ibfd)); BFD_ASSERT (bfd_mach_o_valid (obfd)); @@ -762,7 +761,7 @@ bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd) /* Command is copied. */ ocmd = bfd_alloc (obfd, sizeof (bfd_mach_o_load_command)); if (ocmd == NULL) - return FALSE; + return false; /* Copy common fields. */ ocmd->type = icmd->type; @@ -850,23 +849,23 @@ bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd) bfd_mach_o_append_command (obfd, ocmd); } - return TRUE; + return true; } /* This allows us to set up to 32 bits of flags (unless we invent some fiendish scheme to subdivide). For now, we'll just set the file flags without error checking - just overwrite. */ -bfd_boolean +bool bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); if (!mdata) - return FALSE; + return false; mdata->header.flags = flags; - return TRUE; + return true; } /* Count the total number of symbols. */ @@ -1240,7 +1239,7 @@ bfd_mach_o_pad_command (bfd *abfd, unsigned int len) return 0; } -static bfd_boolean +static bool bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header) { struct mach_o_header_external raw; @@ -1262,12 +1261,12 @@ bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header) if (bfd_seek (abfd, 0, SEEK_SET) != 0 || bfd_bwrite (&raw, size, abfd) != size) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_thread_command *cmd = &command->command.thread; @@ -1290,15 +1289,15 @@ bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; offset += cmd->flavours[i].size + sizeof (raw); } - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_dylinker_command *cmd = &command->command.dylinker; @@ -1309,19 +1308,19 @@ bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; namelen = strlen (cmd->name_str) + 1; if (bfd_bwrite (cmd->name_str, namelen, abfd) != namelen) - return FALSE; + return false; if (bfd_mach_o_pad_command (abfd, namelen) < 0) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_dylib_command *cmd = &command->command.dylib; @@ -1335,19 +1334,19 @@ bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; namelen = strlen (cmd->name_str) + 1; if (bfd_bwrite (cmd->name_str, namelen, abfd) != namelen) - return FALSE; + return false; if (bfd_mach_o_pad_command (abfd, namelen) < 0) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_main_command *cmd = &command->command.main; @@ -1358,12 +1357,12 @@ bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info; @@ -1382,39 +1381,39 @@ bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; if (cmd->rebase_size != 0) if (bfd_seek (abfd, cmd->rebase_off, SEEK_SET) != 0 || (bfd_bwrite (cmd->rebase_content, cmd->rebase_size, abfd) != cmd->rebase_size)) - return FALSE; + return false; if (cmd->bind_size != 0) if (bfd_seek (abfd, cmd->bind_off, SEEK_SET) != 0 || (bfd_bwrite (cmd->bind_content, cmd->bind_size, abfd) != cmd->bind_size)) - return FALSE; + return false; if (cmd->weak_bind_size != 0) if (bfd_seek (abfd, cmd->weak_bind_off, SEEK_SET) != 0 || (bfd_bwrite (cmd->weak_bind_content, cmd->weak_bind_size, abfd) != cmd->weak_bind_size)) - return FALSE; + return false; if (cmd->lazy_bind_size != 0) if (bfd_seek (abfd, cmd->lazy_bind_off, SEEK_SET) != 0 || (bfd_bwrite (cmd->lazy_bind_content, cmd->lazy_bind_size, abfd) != cmd->lazy_bind_size)) - return FALSE; + return false; if (cmd->export_size != 0) if (bfd_seek (abfd, cmd->export_off, SEEK_SET) != 0 || (bfd_bwrite (cmd->export_content, cmd->export_size, abfd) != cmd->export_size)) - return FALSE; + return false; - return TRUE; + return true; } long @@ -1428,7 +1427,7 @@ bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, return -1; } #endif - return (asect->reloc_count + 1) * sizeof (arelent *); + return (asect->reloc_count + 1L) * sizeof (arelent *); } /* In addition to the need to byte-swap the symbol number, the bit positions @@ -1462,7 +1461,7 @@ bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel, /* Set syms_ptr_ptr and addend of RES. */ -bfd_boolean +bool bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *reloc, arelent *res, asymbol **syms) @@ -1504,7 +1503,7 @@ bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd, { _bfd_error_handler (_("\ malformed mach-o reloc: section index is greater than the number of sections")); - return FALSE; + return false; } /* A section number. */ @@ -1525,7 +1524,7 @@ malformed mach-o reloc: section index is greater than the number of sections")); the PPC swap_reloc_in routine. */ res->sym_ptr_ptr = sym; - return TRUE; + return true; } /* Do most of the work for canonicalize_relocs on RAW: create internal @@ -1536,7 +1535,7 @@ malformed mach-o reloc: section index is greater than the number of sections")); (like arm64) don't follow the generic rules (symnum is a value for the non-scattered relocation ADDEND). */ -bfd_boolean +bool bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd, struct mach_o_reloc_info_external *raw, bfd_mach_o_reloc_info *reloc, @@ -1596,14 +1595,14 @@ bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd, if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, reloc, res, syms)) - return FALSE; + return false; } /* We have set up a reloc with all the information present, so the swapper can modify address, value and addend fields, if necessary, to convey information in the generic BFD reloc that is mach-o specific. */ - return TRUE; + return true; } static int @@ -1614,24 +1613,20 @@ bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos, bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); unsigned long i; struct mach_o_reloc_info_external *native_relocs = NULL; - bfd_size_type native_size; + size_t native_size; /* Allocate and read relocs. */ - native_size = count * BFD_MACH_O_RELENT_SIZE; - - /* PR 17512: file: 09477b57. */ - if (native_size < count) + if (_bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &native_size)) + /* PR 17512: file: 09477b57. */ goto err; - native_relocs = - (struct mach_o_reloc_info_external *) bfd_malloc (native_size); + if (bfd_seek (abfd, filepos, SEEK_SET) != 0) + return -1; + native_relocs = (struct mach_o_reloc_info_external *) + _bfd_malloc_and_read (abfd, native_size, native_size); if (native_relocs == NULL) return -1; - if (bfd_seek (abfd, filepos, SEEK_SET) != 0 - || bfd_bread (native_relocs, native_size, abfd) != native_size) - goto err; - for (i = 0; i < count; i++) { if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i], @@ -1665,9 +1660,11 @@ bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect, if (asect->relocation == NULL) { - if (asect->reloc_count * sizeof (arelent) < asect->reloc_count) + size_t amt; + + if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt)) return -1; - res = bfd_malloc (asect->reloc_count * sizeof (arelent)); + res = bfd_malloc (amt); if (res == NULL) return -1; @@ -1720,12 +1717,30 @@ bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels, if (mdata->dyn_reloc_cache == NULL) { - if ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent) - < (dysymtab->nextrel + dysymtab->nlocrel)) - return -1; + ufile_ptr filesize = bfd_get_file_size (abfd); + size_t amt; - res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel) - * sizeof (arelent)); + if (filesize != 0) + { + if (dysymtab->extreloff > filesize + || dysymtab->nextrel > ((filesize - dysymtab->extreloff) + / BFD_MACH_O_RELENT_SIZE) + || dysymtab->locreloff > filesize + || dysymtab->nlocrel > ((filesize - dysymtab->locreloff) + / BFD_MACH_O_RELENT_SIZE)) + { + bfd_set_error (bfd_error_file_truncated); + return -1; + } + } + if (_bfd_mul_overflow (dysymtab->nextrel + dysymtab->nlocrel, + sizeof (arelent), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return -1; + } + + res = bfd_malloc (amt); if (res == NULL) return -1; @@ -1789,7 +1804,7 @@ bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields, fields[3] = info; } -static bfd_boolean +static bool bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section) { unsigned int i; @@ -1799,13 +1814,13 @@ bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section) sec = section->bfdsection; if (sec->reloc_count == 0) - return TRUE; + return true; if (bed->_bfd_mach_o_swap_reloc_out == NULL) - return TRUE; + return true; if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0) - return FALSE; + return false; /* Convert and write. */ entries = section->bfdsection->orelocation; @@ -1817,7 +1832,7 @@ bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section) /* Convert relocation to an intermediate representation. */ if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo)) - return FALSE; + return false; /* Lower the relocation info. */ if (pinfo->r_scattered) @@ -1842,12 +1857,12 @@ bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section) if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd) != BFD_MACH_O_RELENT_SIZE) - return FALSE; + return false; } - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section) { struct mach_o_section_32_external raw; @@ -1866,12 +1881,12 @@ bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section) if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd) != BFD_MACH_O_SECTION_SIZE) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section) { struct mach_o_section_64_external raw; @@ -1891,12 +1906,12 @@ bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section) if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd) != BFD_MACH_O_SECTION_64_SIZE) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command) { struct mach_o_segment_command_32_external raw; @@ -1907,7 +1922,7 @@ bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command) for (sec = seg->sect_head; sec != NULL; sec = sec->next) if (!bfd_mach_o_write_relocs (abfd, sec)) - return FALSE; + return false; memcpy (raw.segname, seg->segname, 16); bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr); @@ -1921,16 +1936,16 @@ bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; for (sec = seg->sect_head; sec != NULL; sec = sec->next) if (!bfd_mach_o_write_section_32 (abfd, sec)) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command) { struct mach_o_segment_command_64_external raw; @@ -1941,7 +1956,7 @@ bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command) for (sec = seg->sect_head; sec != NULL; sec = sec->next) if (!bfd_mach_o_write_relocs (abfd, sec)) - return FALSE; + return false; memcpy (raw.segname, seg->segname, 16); bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr); @@ -1955,16 +1970,16 @@ bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; for (sec = seg->sect_head; sec != NULL; sec = sec->next) if (!bfd_mach_o_write_section_64 (abfd, sec)) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -1976,17 +1991,17 @@ bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym) /* Write the symbols first. */ if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0) - return FALSE; + return false; strtab = _bfd_stringtab_init (); if (strtab == NULL) - return FALSE; + return false; if (sym->nsyms > 0) /* Although we don't strictly need to do this, for compatibility with Darwin system tools, actually output an empty string for the index 0 entry. */ - _bfd_stringtab_add (strtab, "", TRUE, FALSE); + _bfd_stringtab_add (strtab, "", true, false); for (i = 0; i < sym->nsyms; i++) { @@ -1998,7 +2013,7 @@ bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym) str_index = 0; else { - str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE); + str_index = _bfd_stringtab_add (strtab, s->symbol.name, true, false); if (str_index == (bfd_size_type) -1) goto err; @@ -2046,19 +2061,19 @@ bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym) /* Pad string table. */ padlen = bfd_mach_o_pad4 (abfd, sym->strsize); if (padlen < 0) - return FALSE; + return false; mdata->filelen += padlen; sym->strsize += padlen; - return TRUE; + return true; err: _bfd_stringtab_free (strtab); sym->strsize = 0; - return FALSE; + return false; } -static bfd_boolean +static bool bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_symtab_command *sym = &command->command.symtab; @@ -2074,9 +2089,9 @@ bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command) if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; - return TRUE; + return true; } /* Count the number of indirect symbols in the image. @@ -2108,7 +2123,7 @@ bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata) /* Create the dysymtab. */ -static bfd_boolean +static bool bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -2122,7 +2137,7 @@ bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd) { _bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet" " implemented for dysymtab commands.")); - return FALSE; + return false; } cmd->ilocalsym = 0; @@ -2167,16 +2182,17 @@ bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd) { unsigned i; unsigned n; + size_t amt; mdata->filelen = FILE_ALIGN (mdata->filelen, 2); cmd->indirectsymoff = mdata->filelen; - mdata->filelen += cmd->nindirectsyms * 4; + if (_bfd_mul_overflow (cmd->nindirectsyms, 4, &amt)) + return false; + mdata->filelen += amt; - if (cmd->nindirectsyms * 4 < cmd->nindirectsyms) - return FALSE; - cmd->indirect_syms = bfd_zalloc (abfd, cmd->nindirectsyms * 4); + cmd->indirect_syms = bfd_zalloc (abfd, amt); if (cmd->indirect_syms == NULL) - return FALSE; + return false; n = 0; for (i = 0; i < mdata->nsects; ++i) @@ -2216,13 +2232,13 @@ bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd) } } - return TRUE; + return true; } /* Write a dysymtab command. TODO: Possibly coalesce writes of smaller objects. */ -static bfd_boolean +static bool bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab; @@ -2234,7 +2250,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) unsigned int i; if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->nmodtab; i++) { @@ -2269,7 +2285,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) &w.objc_module_info_size); if (bfd_bwrite ((void *) &w, sizeof (w), abfd) != sizeof (w)) - return FALSE; + return false; } else { @@ -2292,7 +2308,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) &n.objc_module_info_size); if (bfd_bwrite ((void *) &n, sizeof (n), abfd) != sizeof (n)) - return FALSE; + return false; } } } @@ -2302,7 +2318,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) unsigned int i; if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->ntoc; i++) { @@ -2313,7 +2329,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) bfd_h_put_32 (abfd, toc->module_index, &raw.module_index); if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; } } @@ -2322,7 +2338,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) unsigned int i; if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->nindirectsyms; ++i) { @@ -2330,7 +2346,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw); if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; } } @@ -2339,7 +2355,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) unsigned int i; if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->nextrefsyms; i++) { @@ -2363,13 +2379,13 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) bfd_h_put_32 (abfd, v, raw); if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; } } /* The command. */ if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0) - return FALSE; + return false; else { struct mach_o_dysymtab_command_external raw; @@ -2394,10 +2410,10 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) bfd_h_put_32 (abfd, cmd->nlocrel, &raw.nlocrel); if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; } - return TRUE; + return true; } static unsigned @@ -2472,14 +2488,14 @@ bfd_mach_o_cf_symbols (const void *a, const void *b) (by name) */ -static bfd_boolean +static bool bfd_mach_o_mangle_symbols (bfd *abfd) { unsigned long i; asymbol **symbols = bfd_get_outsymbols (abfd); if (symbols == NULL || bfd_get_symcount (abfd) == 0) - return TRUE; + return true; for (i = 0; i < bfd_get_symcount (abfd); i++) { @@ -2542,18 +2558,19 @@ bfd_mach_o_mangle_symbols (bfd *abfd) s->symbol.udata.i = i; /* renumber. */ } - return TRUE; + return true; } /* We build a flat table of sections, which can be re-ordered if necessary. Fill in the section number and other mach-o-specific data. */ -static bfd_boolean +static bool bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata) { asection *sec; unsigned target_index; unsigned nsect; + size_t amt; nsect = bfd_count_sections (abfd); @@ -2561,21 +2578,21 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata) doing. */ if (mdata->nsects == nsect && (mdata->nsects == 0 || mdata->sections != NULL)) - return TRUE; + return true; /* We need to check that this can be done... */ if (nsect > 255) { _bfd_error_handler (_("mach-o: there are too many sections (%u)" " maximum is 255,\n"), nsect); - return FALSE; + return false; } mdata->nsects = nsect; - mdata->sections = bfd_alloc2 (abfd, - mdata->nsects, sizeof (bfd_mach_o_section *)); + amt = mdata->nsects * sizeof (bfd_mach_o_section *); + mdata->sections = bfd_alloc (abfd, amt); if (mdata->sections == NULL) - return FALSE; + return false; /* Create Mach-O sections. Section type, attribute and align should have been set when the @@ -2599,10 +2616,10 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata) sec->target_index = ++target_index; } - return TRUE; + return true; } -bfd_boolean +bool bfd_mach_o_write_contents (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -2613,12 +2630,12 @@ bfd_mach_o_write_contents (bfd *abfd) /* Make the commands, if not already present. */ if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd)) - return FALSE; - abfd->output_has_begun = TRUE; + return false; + abfd->output_has_begun = true; /* Write the header. */ if (!bfd_mach_o_write_header (abfd, &mdata->header)) - return FALSE; + return false; /* First pass: allocate the linkedit segment. */ for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) @@ -2701,12 +2718,12 @@ bfd_mach_o_write_contents (bfd *abfd) /* Build the dysymtab. */ if (dysymtab != NULL) if (!bfd_mach_o_build_dysymtab (abfd, dysymtab)) - return FALSE; + return false; /* Write symtab and strtab. */ if (symtab != NULL) if (!bfd_mach_o_write_symtab_content (abfd, symtab)) - return FALSE; + return false; /* Adjust linkedit size. */ if (linkedit != NULL) @@ -2735,56 +2752,56 @@ bfd_mach_o_write_contents (bfd *abfd) if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0 || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8) - return FALSE; + return false; switch (cmd->type) { case BFD_MACH_O_LC_SEGMENT: if (!bfd_mach_o_write_segment_32 (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_SEGMENT_64: if (!bfd_mach_o_write_segment_64 (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_SYMTAB: if (!bfd_mach_o_write_symtab (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_DYSYMTAB: if (!bfd_mach_o_write_dysymtab (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_THREAD: case BFD_MACH_O_LC_UNIXTHREAD: if (!bfd_mach_o_write_thread (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_LOAD_DYLIB: if (!bfd_mach_o_write_dylib (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_LOAD_DYLINKER: if (!bfd_mach_o_write_dylinker (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_MAIN: if (!bfd_mach_o_write_main (abfd, cmd)) - return FALSE; + return false; break; case BFD_MACH_O_LC_DYLD_INFO: if (!bfd_mach_o_write_dyld_info (abfd, cmd)) - return FALSE; + return false; break; default: _bfd_error_handler (_("unable to write unknown load command %#x"), cmd->type); - return FALSE; + return false; } } - return TRUE; + return true; } static void @@ -2821,7 +2838,7 @@ bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED, s->flags = BFD_MACH_O_S_REGULAR; } -static bfd_boolean +static bool bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -2926,10 +2943,10 @@ bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE; } - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -2965,7 +2982,7 @@ bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) (_("section address (%#" PRIx64 ") " "below start of segment (%#" PRIx64 ")"), (uint64_t) s->addr, (uint64_t) vma); - return FALSE; + return false; } vma = s->addr + s->size; @@ -3021,7 +3038,7 @@ bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) /* Relocs shouldn't appear in non-object files. */ if (s->bfdsection->reloc_count != 0) - return FALSE; + return false; } /* Set maxprot. */ @@ -3036,13 +3053,13 @@ bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) seg->filesize = (seg->filesize + pagemask) & ~pagemask; mdata->filelen = (mdata->filelen + pagemask) & ~pagemask; - return TRUE; + return true; } /* Layout the commands: set commands size and offset, set ncmds and sizeofcmds fields in header. */ -static bfd_boolean +static bool bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata) { unsigned wide = mach_o_wide_p (&mdata->header); @@ -3050,7 +3067,7 @@ bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata) ufile_ptr offset; bfd_mach_o_load_command *cmd; unsigned int align; - bfd_boolean ret = TRUE; + bool ret = true; hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE; align = wide ? 8 - 1 : 4 - 1; @@ -3106,7 +3123,7 @@ bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata) _bfd_error_handler (_("unable to layout unknown load command %#x"), cmd->type); - ret = FALSE; + ret = false; break; } @@ -3132,7 +3149,7 @@ bfd_mach_o_init_segment (bfd_mach_o_data_struct *mdata, /* Init segment command. */ cmd->type = wide ? BFD_MACH_O_LC_SEGMENT_64 : BFD_MACH_O_LC_SEGMENT; - cmd->type_required = FALSE; + cmd->type_required = false; strcpy (seg->segname, segname); seg->nsects = nbr_sect; @@ -3153,7 +3170,7 @@ bfd_mach_o_init_segment (bfd_mach_o_data_struct *mdata, TODO: Other file formats, rebuilding symtab/dysymtab commands for strip and copy functionality. */ -bfd_boolean +bool bfd_mach_o_build_commands (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -3168,7 +3185,7 @@ bfd_mach_o_build_commands (bfd *abfd) /* Return now if already built. */ if (mdata->header.ncmds != 0) - return TRUE; + return true; /* Fill in the file type, if not already set. */ if (mdata->header.filetype == 0) @@ -3186,12 +3203,12 @@ bfd_mach_o_build_commands (bfd *abfd) since that depends on properly numbered sections. */ if (mdata->nsects == 0 || mdata->sections == NULL) if (! bfd_mach_o_mangle_sections (abfd, mdata)) - return FALSE; + return false; /* Order the symbol table, fill-in/check mach-o specific fields and partition out any indirect symbols. */ if (!bfd_mach_o_mangle_symbols (abfd)) - return FALSE; + return false; /* Segment commands. */ if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT) @@ -3257,7 +3274,7 @@ bfd_mach_o_build_commands (bfd *abfd) /* Create commands for segments (and symtabs), prepend them. */ commands = bfd_zalloc (abfd, nbr_commands * sizeof (bfd_mach_o_load_command)); if (commands == NULL) - return FALSE; + return false; for (i = 0; i < nbr_commands - 1; i++) commands[i].next = &commands[i + 1]; commands[nbr_commands - 1].next = mdata->first_command; @@ -3308,7 +3325,7 @@ bfd_mach_o_build_commands (bfd *abfd) bfd_mach_o_load_command *cmd = &commands[symtab_idx]; cmd->type = BFD_MACH_O_LC_SYMTAB; - cmd->type_required = FALSE; + cmd->type_required = false; } /* If required, setup symtab command, see comment above about the quality @@ -3318,7 +3335,7 @@ bfd_mach_o_build_commands (bfd *abfd) bfd_mach_o_load_command *cmd = &commands[dysymtab_idx]; cmd->type = BFD_MACH_O_LC_DYSYMTAB; - cmd->type_required = FALSE; + cmd->type_required = false; } /* Create the main command. */ @@ -3327,7 +3344,7 @@ bfd_mach_o_build_commands (bfd *abfd) bfd_mach_o_load_command *cmd = &commands[main_idx]; cmd->type = BFD_MACH_O_LC_MAIN; - cmd->type_required = TRUE; + cmd->type_required = true; cmd->command.main.entryoff = 0; cmd->command.main.stacksize = 0; @@ -3335,7 +3352,7 @@ bfd_mach_o_build_commands (bfd *abfd) /* Layout commands. */ if (! bfd_mach_o_layout_commands (mdata)) - return FALSE; + 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. */ @@ -3344,7 +3361,7 @@ bfd_mach_o_build_commands (bfd *abfd) for (i = 0; i < nbr_segcmd; i++) if (!bfd_mach_o_build_obj_seg_command (abfd, &commands[i].command.segment)) - return FALSE; + return false; } else { @@ -3356,7 +3373,7 @@ bfd_mach_o_build_commands (bfd *abfd) bfd_mach_o_segment_command *seg = &commands[i].command.segment; if (!bfd_mach_o_build_exec_seg_command (abfd, seg)) - return FALSE; + return false; if (seg->vmaddr + seg->vmsize > maxvma) maxvma = seg->vmaddr + seg->vmsize; @@ -3376,12 +3393,12 @@ bfd_mach_o_build_commands (bfd *abfd) bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr; } - return TRUE; + return true; } /* Set the contents of a section. */ -bfd_boolean +bool bfd_mach_o_set_section_contents (bfd *abfd, asection *section, const void * location, @@ -3393,17 +3410,17 @@ bfd_mach_o_set_section_contents (bfd *abfd, /* Trying to write the first section contents will trigger the creation of the load commands if they are not already present. */ if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd)) - return FALSE; + return false; if (count == 0) - return TRUE; + return true; pos = section->filepos + offset; if (bfd_seek (abfd, pos, SEEK_SET) != 0 || bfd_bwrite (location, count, abfd) != count) - return FALSE; + return false; - return TRUE; + return true; } int @@ -3429,7 +3446,7 @@ bfd_mach_o_make_empty_symbol (bfd *abfd) return new_symbol; } -static bfd_boolean +static bool bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header) { struct mach_o_header_external raw; @@ -3439,7 +3456,7 @@ bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header) /* Just read the magic number. */ if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0 || bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4) - return FALSE; + return false; if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC) { @@ -3472,7 +3489,7 @@ bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header) else { header->byteorder = BFD_ENDIAN_UNKNOWN; - return FALSE; + return false; } /* Once the size of the header is known, read the full header. */ @@ -3481,7 +3498,7 @@ bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header) if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0 || bfd_bread (&raw, size, abfd) != size) - return FALSE; + return false; header->cputype = (*get32) (raw.cputype); header->cpusubtype = (*get32) (raw.cpusubtype); @@ -3495,10 +3512,10 @@ bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header) else header->reserved = 0; - return TRUE; + return true; } -bfd_boolean +bool bfd_mach_o_new_section_hook (bfd *abfd, asection *sec) { bfd_mach_o_section *s; @@ -3512,7 +3529,7 @@ bfd_mach_o_new_section_hook (bfd *abfd, asection *sec) s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s)); if (s == NULL) - return FALSE; + return false; sec->used_by_bfd = s; s->bfdsection = sec; @@ -3636,12 +3653,12 @@ bfd_mach_o_read_section_32 (bfd *abfd, unsigned long prot) 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) + if (section->align >= 31) { _bfd_error_handler - (_("bfd_mach_o_read_section_32: overlarge alignment value: %#lx, " - "using 32 instead"), section->align); - section->align = 32; + (_("bfd_mach_o_read_section_32: overlarge alignment value: %#lx"), + section->align); + section->align = 30; } section->reloff = bfd_h_get_32 (abfd, raw.reloff); section->nreloc = bfd_h_get_32 (abfd, raw.nreloc); @@ -3679,12 +3696,12 @@ bfd_mach_o_read_section_64 (bfd *abfd, unsigned long prot) 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) + if (section->align >= 63) { _bfd_error_handler - (_("bfd_mach_o_read_section_64: overlarge alignment value: %#lx, " - "using 32 instead"), section->align); - section->align = 32; + (_("bfd_mach_o_read_section_64: overlarge alignment value: %#lx"), + section->align); + section->align = 62; } section->reloff = bfd_h_get_32 (abfd, raw.reloff); section->nreloc = bfd_h_get_32 (abfd, raw.nreloc); @@ -3707,7 +3724,7 @@ bfd_mach_o_read_section (bfd *abfd, unsigned long prot, unsigned int wide) return bfd_mach_o_read_section_32 (abfd, prot); } -static bfd_boolean +static bool bfd_mach_o_read_symtab_symbol (bfd *abfd, bfd_mach_o_symtab_command *sym, bfd_mach_o_asymbol *s, @@ -3735,7 +3752,7 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd, /* xgettext:c-format */ (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"), symwidth, symoff); - return FALSE; + return false; } stroff = bfd_h_get_32 (abfd, raw.n_strx); @@ -3755,7 +3772,7 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd, (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"), stroff, sym->strsize); - return FALSE; + return false; } s->symbol.the_bfd = abfd; @@ -3862,10 +3879,10 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd, } } - return TRUE; + return true; } -bfd_boolean +bool bfd_mach_o_read_symtab_strtab (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -3873,11 +3890,11 @@ bfd_mach_o_read_symtab_strtab (bfd *abfd) /* Fail if there is no symtab. */ if (sym == NULL) - return FALSE; + return false; /* Success if already loaded. */ if (sym->strtab) - return TRUE; + return true; if (abfd->flags & BFD_IN_MEMORY) { @@ -3888,7 +3905,7 @@ bfd_mach_o_read_symtab_strtab (bfd *abfd) if ((sym->stroff + sym->strsize) > b->size) { bfd_set_error (bfd_error_file_truncated); - return FALSE; + return false; } sym->strtab = (char *) b->buffer + sym->stroff; } @@ -3896,45 +3913,55 @@ bfd_mach_o_read_symtab_strtab (bfd *abfd) { /* See PR 21840 for a reproducer. */ if ((sym->strsize + 1) == 0) - return FALSE; - sym->strtab = bfd_alloc (abfd, sym->strsize + 1); + return false; + if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0) + return false; + sym->strtab = (char *) _bfd_alloc_and_read (abfd, sym->strsize + 1, + sym->strsize); if (sym->strtab == NULL) - return FALSE; + return false; - if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0 - || bfd_bread (sym->strtab, sym->strsize, abfd) != sym->strsize) - { - /* PR 17512: file: 10888-1609-0.004. */ - bfd_release (abfd, sym->strtab); - sym->strtab = NULL; - bfd_set_error (bfd_error_file_truncated); - return FALSE; - } /* Zero terminate the string table. */ sym->strtab[sym->strsize] = 0; } - return TRUE; + return true; } -bfd_boolean +bool bfd_mach_o_read_symtab_symbols (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); bfd_mach_o_symtab_command *sym = mdata->symtab; unsigned long i; + size_t amt; + ufile_ptr filesize; - if (sym == NULL || sym->symbols) + if (sym == NULL || sym->nsyms == 0 || sym->symbols) /* Return now if there are no symbols or if already loaded. */ - return TRUE; + return true; - sym->symbols = bfd_alloc2 (abfd, sym->nsyms, sizeof (bfd_mach_o_asymbol)); - if (sym->symbols == NULL) + filesize = bfd_get_file_size (abfd); + if (filesize != 0) + { + unsigned int wide = mach_o_wide_p (&mdata->header); + unsigned int symwidth + = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE; + + if (sym->symoff > filesize + || sym->nsyms > (filesize - sym->symoff) / symwidth) + { + bfd_set_error (bfd_error_file_truncated); + sym->nsyms = 0; + return false; + } + } + if (_bfd_mul_overflow (sym->nsyms, sizeof (bfd_mach_o_asymbol), &amt) + || (sym->symbols = bfd_alloc (abfd, amt)) == NULL) { - _bfd_error_handler (_("bfd_mach_o_read_symtab_symbols: " - "unable to allocate memory for symbols")); + bfd_set_error (bfd_error_no_memory); sym->nsyms = 0; - return FALSE; + return false; } if (!bfd_mach_o_read_symtab_strtab (abfd)) @@ -3944,13 +3971,13 @@ bfd_mach_o_read_symtab_symbols (bfd *abfd) if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i)) goto fail; - return TRUE; + return true; fail: bfd_release (abfd, sym->symbols); sym->symbols = NULL; sym->nsyms = 0; - return FALSE; + return false; } static const char * @@ -3990,7 +4017,15 @@ bfd_mach_o_ppc_flavour_string (unsigned int flavour) } } -static bfd_boolean +static unsigned char * +bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos, size_t size) +{ + if (bfd_seek (abfd, filepos, SEEK_SET) != 0) + return NULL; + return _bfd_alloc_and_read (abfd, size, size); +} + +static bool bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_dylinker_command *cmd = &command->command.dylinker; @@ -3998,24 +4033,23 @@ bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command) unsigned int nameoff; unsigned int namelen; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; nameoff = bfd_h_get_32 (abfd, raw.str); + if (nameoff > command->len) + return false; cmd->name_offset = nameoff; namelen = command->len - nameoff; nameoff += command->offset; - cmd->name_str = bfd_alloc (abfd, namelen); - if (cmd->name_str == NULL) - return FALSE; - if (bfd_seek (abfd, nameoff, SEEK_SET) != 0 - || bfd_bread (cmd->name_str, namelen, abfd) != namelen) - return FALSE; - return TRUE; + cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, nameoff, namelen); + return cmd->name_str != NULL; } -static bfd_boolean +static bool bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -4023,7 +4057,10 @@ bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command) struct mach_o_dylib_command_external raw; unsigned int nameoff; unsigned int namelen; + file_ptr pos; + if (command->len < sizeof (raw) + 8) + return false; switch (command->type) { case BFD_MACH_O_LC_LOAD_DYLIB: @@ -4035,29 +4072,27 @@ bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command) break; default: BFD_FAIL (); - return FALSE; + return false; } if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; nameoff = bfd_h_get_32 (abfd, raw.name); + if (nameoff > command->len) + return false; cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp); cmd->current_version = bfd_h_get_32 (abfd, raw.current_version); cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version); cmd->name_offset = command->offset + nameoff; namelen = command->len - nameoff; - cmd->name_str = bfd_alloc (abfd, namelen); - if (cmd->name_str == NULL) - return FALSE; - if (bfd_seek (abfd, mdata->hdr_offset + cmd->name_offset, SEEK_SET) != 0 - || bfd_bread (cmd->name_str, namelen, abfd) != namelen) - return FALSE; - return TRUE; + pos = mdata->hdr_offset + cmd->name_offset; + cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, pos, namelen); + return cmd->name_str != NULL; } -static bfd_boolean +static bool bfd_mach_o_read_prebound_dylib (bfd *abfd, bfd_mach_o_load_command *command) { @@ -4068,20 +4103,20 @@ bfd_mach_o_read_prebound_dylib (bfd *abfd, unsigned int str_len; unsigned char *str; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; nameoff = bfd_h_get_32 (abfd, raw.name); modoff = bfd_h_get_32 (abfd, raw.linked_modules); if (nameoff > command->len || modoff > command->len) - return FALSE; + return false; str_len = command->len - sizeof (raw); - str = bfd_alloc (abfd, str_len); + str = _bfd_alloc_and_read (abfd, str_len, str_len); if (str == NULL) - return FALSE; - if (bfd_bread (str, str_len, abfd) != str_len) - return FALSE; + return false; cmd->name_offset = command->offset + nameoff; cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules); @@ -4089,39 +4124,43 @@ bfd_mach_o_read_prebound_dylib (bfd *abfd, cmd->name_str = (char *)str + nameoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE); cmd->linked_modules = str + modoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_prebind_cksum (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_prebind_cksum_command *cmd = &command->command.prebind_cksum; struct mach_o_prebind_cksum_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->cksum = bfd_get_32 (abfd, raw.cksum); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_twolevel_hints (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_twolevel_hints_command *cmd = &command->command.twolevel_hints; struct mach_o_twolevel_hints_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->offset = bfd_get_32 (abfd, raw.offset); cmd->nhints = bfd_get_32 (abfd, raw.nhints); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib; @@ -4129,25 +4168,25 @@ bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command) unsigned int nameoff; unsigned int namelen; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; nameoff = bfd_h_get_32 (abfd, raw.name); + if (nameoff > command->len) + return false; fvm->minor_version = bfd_h_get_32 (abfd, raw.minor_version); fvm->header_addr = bfd_h_get_32 (abfd, raw.header_addr); fvm->name_offset = command->offset + nameoff; namelen = command->len - nameoff; - fvm->name_str = bfd_alloc (abfd, namelen); - if (fvm->name_str == NULL) - return FALSE; - if (bfd_seek (abfd, fvm->name_offset, SEEK_SET) != 0 - || bfd_bread (fvm->name_str, namelen, abfd) != namelen) - return FALSE; - return TRUE; + fvm->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, fvm->name_offset, + namelen); + return fvm->name_str != NULL; } -static bfd_boolean +static bool bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -4155,6 +4194,8 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command) unsigned int offset; unsigned int nflavours; unsigned int i; + struct mach_o_thread_command_external raw; + size_t amt; BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD) || (command->type == BFD_MACH_O_LC_UNIXTHREAD)); @@ -4162,43 +4203,42 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command) /* Count the number of threads. */ offset = 8; nflavours = 0; - while (offset != command->len) + while (offset + sizeof (raw) <= command->len) { - struct mach_o_thread_command_external raw; - - if (offset >= command->len) - return FALSE; + unsigned int count; if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; - offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4; + count = bfd_h_get_32 (abfd, raw.count); + if (count > (unsigned) -1 / 4 + || command->len - (offset + sizeof (raw)) < count * 4) + return false; + offset += sizeof (raw) + count * 4; nflavours++; } + if (nflavours == 0 || offset != command->len) + return false; /* Allocate threads. */ - cmd->flavours = bfd_alloc2 - (abfd, nflavours, sizeof (bfd_mach_o_thread_flavour)); + if (_bfd_mul_overflow (nflavours, sizeof (bfd_mach_o_thread_flavour), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return false; + } + cmd->flavours = bfd_alloc (abfd, amt); if (cmd->flavours == NULL) - return FALSE; + return false; cmd->nflavours = nflavours; offset = 8; nflavours = 0; while (offset != command->len) { - struct mach_o_thread_command_external raw; - - if (offset >= command->len) - return FALSE; - - if (nflavours >= cmd->nflavours) - return FALSE; - if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour); cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw); @@ -4236,7 +4276,7 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command) snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1; sname = bfd_alloc (abfd, snamelen); if (sname == NULL) - return FALSE; + return false; for (;;) { @@ -4257,11 +4297,12 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command) cmd->section = bfdsec; } - return TRUE; + return true; } -static bfd_boolean -bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) +static bool +bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command, + ufile_ptr filesize) { bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab; bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -4271,8 +4312,10 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) { struct mach_o_dysymtab_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym); cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym); @@ -4299,14 +4342,26 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) unsigned int i; int wide = bfd_mach_o_wide_p (abfd); unsigned int module_len = wide ? 56 : 52; + size_t amt; - cmd->dylib_module = - bfd_alloc2 (abfd, cmd->nmodtab, sizeof (bfd_mach_o_dylib_module)); + if (cmd->modtaboff > filesize + || cmd->nmodtab > (filesize - cmd->modtaboff) / module_len) + { + bfd_set_error (bfd_error_file_truncated); + return false; + } + if (_bfd_mul_overflow (cmd->nmodtab, + sizeof (bfd_mach_o_dylib_module), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return false; + } + cmd->dylib_module = bfd_alloc (abfd, amt); if (cmd->dylib_module == NULL) - return FALSE; + return false; if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->nmodtab; i++) { @@ -4315,7 +4370,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) unsigned char buf[56]; if (bfd_bread ((void *) buf, module_len, abfd) != module_len) - return FALSE; + return false; module->module_name_idx = bfd_h_get_32 (abfd, buf + 0); module->iextdefsym = bfd_h_get_32 (abfd, buf + 4); @@ -4348,22 +4403,34 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) if (cmd->ntoc != 0) { unsigned long i; + size_t amt; + struct mach_o_dylib_table_of_contents_external raw; - cmd->dylib_toc = bfd_alloc2 - (abfd, cmd->ntoc, sizeof (bfd_mach_o_dylib_table_of_content)); + if (cmd->tocoff > filesize + || cmd->ntoc > (filesize - cmd->tocoff) / sizeof (raw)) + { + bfd_set_error (bfd_error_file_truncated); + return false; + } + if (_bfd_mul_overflow (cmd->ntoc, + sizeof (bfd_mach_o_dylib_table_of_content), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return false; + } + cmd->dylib_toc = bfd_alloc (abfd, amt); if (cmd->dylib_toc == NULL) - return FALSE; + return false; if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->ntoc; i++) { - struct mach_o_dylib_table_of_contents_external raw; bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i]; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index); toc->module_index = bfd_h_get_32 (abfd, raw.module_index); @@ -4373,14 +4440,25 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) if (cmd->nindirectsyms != 0) { unsigned int i; + size_t amt; - cmd->indirect_syms = bfd_alloc2 - (abfd, cmd->nindirectsyms, sizeof (unsigned int)); + if (cmd->indirectsymoff > filesize + || cmd->nindirectsyms > (filesize - cmd->indirectsymoff) / 4) + { + bfd_set_error (bfd_error_file_truncated); + return false; + } + if (_bfd_mul_overflow (cmd->nindirectsyms, sizeof (unsigned int), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return false; + } + cmd->indirect_syms = bfd_alloc (abfd, amt); if (cmd->indirect_syms == NULL) - return FALSE; + return false; if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->nindirectsyms; i++) { @@ -4388,7 +4466,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) unsigned int *is = &cmd->indirect_syms[i]; if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; *is = bfd_h_get_32 (abfd, raw); } @@ -4398,14 +4476,26 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) { unsigned long v; unsigned int i; + size_t amt; - cmd->ext_refs = bfd_alloc2 - (abfd, cmd->nextrefsyms, sizeof (bfd_mach_o_dylib_reference)); + if (cmd->extrefsymoff > filesize + || cmd->nextrefsyms > (filesize - cmd->extrefsymoff) / 4) + { + bfd_set_error (bfd_error_file_truncated); + return false; + } + if (_bfd_mul_overflow (cmd->nextrefsyms, + sizeof (bfd_mach_o_dylib_reference), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return false; + } + cmd->ext_refs = bfd_alloc (abfd, amt); if (cmd->ext_refs == NULL) - return FALSE; + return false; if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0) - return FALSE; + return false; for (i = 0; i < cmd->nextrefsyms; i++) { @@ -4413,7 +4503,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i]; if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; /* Fields isym and flags are written as bit-fields, thus we need a specific processing for endianness. */ @@ -4432,14 +4522,15 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) } if (mdata->dysymtab) - return FALSE; + return false; mdata->dysymtab = cmd; - return TRUE; + return true; } -static bfd_boolean -bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command) +static bool +bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command, + ufile_ptr filesize) { bfd_mach_o_symtab_command *symtab = &command->command.symtab; bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -4447,8 +4538,10 @@ bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command) BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB); + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; symtab->symoff = bfd_h_get_32 (abfd, raw.symoff); symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms); @@ -4457,97 +4550,97 @@ bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command) symtab->symbols = NULL; symtab->strtab = NULL; + if (symtab->symoff > filesize + || symtab->nsyms > (filesize - symtab->symoff) / BFD_MACH_O_NLIST_SIZE + || symtab->stroff > filesize + || symtab->strsize > filesize - symtab->stroff) + { + bfd_set_error (bfd_error_file_truncated); + return false; + } + if (symtab->nsyms != 0) abfd->flags |= HAS_SYMS; if (mdata->symtab) - return FALSE; + return false; mdata->symtab = symtab; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_uuid_command *cmd = &command->command.uuid; BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID); + if (command->len < 16 + 8) + return false; if (bfd_bread (cmd->uuid, 16, abfd) != 16) - return FALSE; + return false; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_linkedit_command *cmd = &command->command.linkedit; struct mach_o_linkedit_data_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->dataoff = bfd_get_32 (abfd, raw.dataoff); cmd->datasize = bfd_get_32 (abfd, raw.datasize); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_str_command *cmd = &command->command.str; struct mach_o_str_command_external raw; unsigned long off; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; off = bfd_get_32 (abfd, raw.str); + if (off > command->len) + return false; + cmd->stroff = command->offset + off; cmd->str_len = command->len - off; - cmd->str = bfd_alloc (abfd, cmd->str_len); - if (cmd->str == NULL) - return FALSE; - if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0 - || bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len) - return FALSE; - return TRUE; + cmd->str = (char *) bfd_mach_o_alloc_and_read (abfd, cmd->stroff, + cmd->str_len); + return cmd->str != NULL; } -static unsigned char * -bfd_mach_o_alloc_and_read (bfd *abfd, unsigned int off, unsigned int size) -{ - unsigned char *buf; - - buf = bfd_alloc (abfd, size); - if (buf == NULL) - return NULL; - if (bfd_seek (abfd, off, SEEK_SET) != 0 - || bfd_bread (buf, size, abfd) != size) - return NULL; - return buf; -} - -static bfd_boolean +static bool bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd) { /* Read rebase content. */ if (cmd->rebase_content == NULL && cmd->rebase_size != 0) { - cmd->rebase_content = - bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size); + cmd->rebase_content + = bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size); if (cmd->rebase_content == NULL) - return FALSE; + return false; } /* Read bind content. */ if (cmd->bind_content == NULL && cmd->bind_size != 0) { - cmd->bind_content = - bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size); + cmd->bind_content + = bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size); if (cmd->bind_content == NULL) - return FALSE; + return false; } /* Read weak bind content. */ @@ -4556,7 +4649,7 @@ bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd) cmd->weak_bind_content = bfd_mach_o_alloc_and_read (abfd, cmd->weak_bind_off, cmd->weak_bind_size); if (cmd->weak_bind_content == NULL) - return FALSE; + return false; } /* Read lazy bind content. */ @@ -4565,7 +4658,7 @@ bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd) cmd->lazy_bind_content = bfd_mach_o_alloc_and_read (abfd, cmd->lazy_bind_off, cmd->lazy_bind_size); if (cmd->lazy_bind_content == NULL) - return FALSE; + return false; } /* Read export content. */ @@ -4574,20 +4667,22 @@ bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd) cmd->export_content = bfd_mach_o_alloc_and_read (abfd, cmd->export_off, cmd->export_size); if (cmd->export_content == NULL) - return FALSE; + return false; } - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info; struct mach_o_dyld_info_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off); cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size); @@ -4604,76 +4699,86 @@ bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command) cmd->export_off = bfd_get_32 (abfd, raw.export_off); cmd->export_size = bfd_get_32 (abfd, raw.export_size); cmd->export_content = NULL; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_version_min_command *cmd = &command->command.version_min; struct mach_o_version_min_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->version = bfd_get_32 (abfd, raw.version); cmd->sdk = bfd_get_32 (abfd, raw.sdk); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_encryption_info (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info; struct mach_o_encryption_info_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff); cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize); cmd->cryptid = bfd_get_32 (abfd, raw.cryptid); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_encryption_info_64 (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info; struct mach_o_encryption_info_64_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff); cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize); cmd->cryptid = bfd_get_32 (abfd, raw.cryptid); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_main (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_main_command *cmd = &command->command.main; struct mach_o_entry_point_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->entryoff = bfd_get_64 (abfd, raw.entryoff); cmd->stacksize = bfd_get_64 (abfd, raw.stacksize); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_source_version (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_source_version_command *cmd = &command->command.source_version; struct mach_o_source_version_command_external raw; bfd_uint64_t ver; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; ver = bfd_get_64 (abfd, raw.version); /* Note: we use a serie of shift to avoid shift > 32 (for which gcc @@ -4688,41 +4793,45 @@ bfd_mach_o_read_source_version (bfd *abfd, bfd_mach_o_load_command *command) cmd->b = ver & 0x3ff; ver >>= 10; cmd->a = ver & 0xffffff; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_note (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_note_command *cmd = &command->command.note; struct mach_o_note_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; memcpy (cmd->data_owner, raw.data_owner, 16); cmd->offset = bfd_get_64 (abfd, raw.offset); cmd->size = bfd_get_64 (abfd, raw.size); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_build_version (bfd *abfd, bfd_mach_o_load_command *command) { bfd_mach_o_build_version_command *cmd = &command->command.build_version; struct mach_o_build_version_command_external raw; + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; cmd->platform = bfd_get_32 (abfd, raw.platform); cmd->minos = bfd_get_32 (abfd, raw.minos); cmd->sdk = bfd_get_32 (abfd, raw.sdk); cmd->ntools = bfd_get_32 (abfd, raw.ntools); - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_segment (bfd *abfd, bfd_mach_o_load_command *command, unsigned int wide) @@ -4736,8 +4845,10 @@ bfd_mach_o_read_segment (bfd *abfd, BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64); + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; memcpy (seg->segname, raw.segname, 16); seg->segname[16] = '\0'; @@ -4757,8 +4868,10 @@ bfd_mach_o_read_segment (bfd *abfd, BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT); + if (command->len < sizeof (raw) + 8) + return false; if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw)) - return FALSE; + return false; memcpy (seg->segname, raw.segname, 16); seg->segname[16] = '\0'; @@ -4781,29 +4894,30 @@ bfd_mach_o_read_segment (bfd *abfd, sec = bfd_mach_o_read_section (abfd, seg->initprot, wide); if (sec == NULL) - return FALSE; + return false; bfd_mach_o_append_section_to_segment (seg, bfd_mach_o_get_mach_o_section (sec)); } - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command) { return bfd_mach_o_read_segment (abfd, command, 0); } -static bfd_boolean +static bool bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command) { return bfd_mach_o_read_segment (abfd, command, 1); } -static bfd_boolean -bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command) +static bool +bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command, + ufile_ptr filesize) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); struct mach_o_load_command_external raw; @@ -4812,39 +4926,41 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command) /* Read command type and length. */ if (bfd_seek (abfd, mdata->hdr_offset + command->offset, SEEK_SET) != 0 || bfd_bread (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE) - return FALSE; + return false; cmd = bfd_h_get_32 (abfd, raw.cmd); - command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD; - command->type_required = cmd & BFD_MACH_O_LC_REQ_DYLD ? TRUE : FALSE; + command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD; + command->type_required = (cmd & BFD_MACH_O_LC_REQ_DYLD) != 0; command->len = bfd_h_get_32 (abfd, raw.cmdsize); + if (command->len < 8 || command->len % 4 != 0) + return false; switch (command->type) { case BFD_MACH_O_LC_SEGMENT: if (!bfd_mach_o_read_segment_32 (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_SEGMENT_64: if (!bfd_mach_o_read_segment_64 (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_SYMTAB: - if (!bfd_mach_o_read_symtab (abfd, command)) - return FALSE; + if (!bfd_mach_o_read_symtab (abfd, command, filesize)) + return false; break; case BFD_MACH_O_LC_SYMSEG: break; case BFD_MACH_O_LC_THREAD: case BFD_MACH_O_LC_UNIXTHREAD: if (!bfd_mach_o_read_thread (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_LOAD_DYLINKER: case BFD_MACH_O_LC_ID_DYLINKER: case BFD_MACH_O_LC_DYLD_ENVIRONMENT: if (!bfd_mach_o_read_dylinker (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_LOAD_DYLIB: case BFD_MACH_O_LC_LAZY_LOAD_DYLIB: @@ -4853,16 +4969,16 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command) case BFD_MACH_O_LC_REEXPORT_DYLIB: case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB: if (!bfd_mach_o_read_dylib (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_PREBOUND_DYLIB: if (!bfd_mach_o_read_prebound_dylib (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_LOADFVMLIB: case BFD_MACH_O_LC_IDFVMLIB: if (!bfd_mach_o_read_fvmlib (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_IDENT: case BFD_MACH_O_LC_FVMFILE: @@ -4876,23 +4992,23 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command) case BFD_MACH_O_LC_SUB_CLIENT: case BFD_MACH_O_LC_RPATH: if (!bfd_mach_o_read_str (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_DYSYMTAB: - if (!bfd_mach_o_read_dysymtab (abfd, command)) - return FALSE; + if (!bfd_mach_o_read_dysymtab (abfd, command, filesize)) + return false; break; case BFD_MACH_O_LC_PREBIND_CKSUM: if (!bfd_mach_o_read_prebind_cksum (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_TWOLEVEL_HINTS: if (!bfd_mach_o_read_twolevel_hints (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_UUID: if (!bfd_mach_o_read_uuid (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_CODE_SIGNATURE: case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO: @@ -4900,62 +5016,65 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command) case BFD_MACH_O_LC_DATA_IN_CODE: case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS: case BFD_MACH_O_LC_LINKER_OPTIMIZATION_HINT: + case BFD_MACH_O_LC_DYLD_EXPORTS_TRIE: + case BFD_MACH_O_LC_DYLD_CHAINED_FIXUPS: if (!bfd_mach_o_read_linkedit (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_ENCRYPTION_INFO: if (!bfd_mach_o_read_encryption_info (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_ENCRYPTION_INFO_64: if (!bfd_mach_o_read_encryption_info_64 (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_DYLD_INFO: if (!bfd_mach_o_read_dyld_info (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_VERSION_MIN_MACOSX: case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS: case BFD_MACH_O_LC_VERSION_MIN_WATCHOS: case BFD_MACH_O_LC_VERSION_MIN_TVOS: if (!bfd_mach_o_read_version_min (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_MAIN: if (!bfd_mach_o_read_main (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_SOURCE_VERSION: if (!bfd_mach_o_read_source_version (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_LINKER_OPTIONS: break; case BFD_MACH_O_LC_NOTE: if (!bfd_mach_o_read_note (abfd, command)) - return FALSE; + return false; break; case BFD_MACH_O_LC_BUILD_VERSION: if (!bfd_mach_o_read_build_version (abfd, command)) - return FALSE; + return false; break; default: command->len = 0; _bfd_error_handler (_("%pB: unknown load command %#x"), abfd, command->type); - return FALSE; + return false; } - return TRUE; + return true; } -static void +static bool bfd_mach_o_flatten_sections (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); bfd_mach_o_load_command *cmd; long csect = 0; + size_t amt; /* Count total number of sections. */ mdata->nsects = 0; @@ -4972,8 +5091,14 @@ bfd_mach_o_flatten_sections (bfd *abfd) } /* Allocate sections array. */ - mdata->sections = bfd_alloc2 (abfd, - mdata->nsects, sizeof (bfd_mach_o_section *)); + if (_bfd_mul_overflow (mdata->nsects, sizeof (bfd_mach_o_section *), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return false; + } + mdata->sections = bfd_alloc (abfd, amt); + if (mdata->sections == NULL && mdata->nsects != 0) + return false; /* Fill the array. */ csect = 0; @@ -4992,9 +5117,10 @@ bfd_mach_o_flatten_sections (bfd *abfd) mdata->sections[csect++] = sec; } } + return true; } -static bfd_boolean +static bool bfd_mach_o_scan_start_address (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -5018,13 +5144,13 @@ bfd_mach_o_scan_start_address (bfd *abfd) { abfd->start_address = main_cmd->entryoff + (text_sect->addr - text_sect->offset); - return TRUE; + return true; } } /* An object file has no start address, so do not fail if not found. */ if (thr == NULL) - return TRUE; + return true; /* FIXME: create a subtarget hook ? */ for (i = 0; i < thr->nflavours; i++) @@ -5036,7 +5162,7 @@ bfd_mach_o_scan_start_address (bfd *abfd) if (bfd_seek (abfd, thr->flavours[i].offset + 40, SEEK_SET) != 0 || bfd_bread (buf, 4, abfd) != 4) - return FALSE; + return false; abfd->start_address = bfd_h_get_32 (abfd, buf); } @@ -5047,7 +5173,7 @@ bfd_mach_o_scan_start_address (bfd *abfd) if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0 || bfd_bread (buf, 4, abfd) != 4) - return FALSE; + return false; abfd->start_address = bfd_h_get_32 (abfd, buf); } @@ -5058,7 +5184,7 @@ bfd_mach_o_scan_start_address (bfd *abfd) if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0 || bfd_bread (buf, 8, abfd) != 8) - return FALSE; + return false; abfd->start_address = bfd_h_get_64 (abfd, buf); } @@ -5069,16 +5195,16 @@ bfd_mach_o_scan_start_address (bfd *abfd) if (bfd_seek (abfd, thr->flavours[i].offset + (16 * 8), SEEK_SET) != 0 || bfd_bread (buf, 8, abfd) != 8) - return FALSE; + return false; abfd->start_address = bfd_h_get_64 (abfd, buf); } } - return TRUE; + return true; } -bfd_boolean +bool bfd_mach_o_set_arch_mach (bfd *abfd, enum bfd_architecture arch, unsigned long machine) @@ -5090,19 +5216,19 @@ bfd_mach_o_set_arch_mach (bfd *abfd, if (arch != bed->arch && arch != bfd_arch_unknown && bed->arch != bfd_arch_unknown) - return FALSE; + return false; return bfd_default_set_arch_mach (abfd, arch, machine); } -static bfd_boolean +static bool bfd_mach_o_scan (bfd *abfd, bfd_mach_o_header *header, bfd_mach_o_data_struct *mdata) { unsigned int i; - enum bfd_architecture cputype; - unsigned long cpusubtype; + enum bfd_architecture cpu_type; + unsigned long cpu_subtype; unsigned int hdrsize; hdrsize = mach_o_wide_p (header) ? @@ -5128,28 +5254,44 @@ bfd_mach_o_scan (bfd *abfd, abfd->tdata.mach_o_data = mdata; bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype, - &cputype, &cpusubtype); - if (cputype == bfd_arch_unknown) + &cpu_type, &cpu_subtype); + if (cpu_type == bfd_arch_unknown) { _bfd_error_handler /* xgettext:c-format */ (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"), header->cputype, header->cpusubtype); - return FALSE; + return false; } - bfd_set_arch_mach (abfd, cputype, cpusubtype); + bfd_set_arch_mach (abfd, cpu_type, cpu_subtype); if (header->ncmds != 0) { bfd_mach_o_load_command *cmd; + size_t amt; + ufile_ptr filesize = bfd_get_file_size (abfd); + + if (filesize == 0) + filesize = (ufile_ptr) -1; mdata->first_command = NULL; mdata->last_command = NULL; - cmd = bfd_alloc2 (abfd, header->ncmds, sizeof (bfd_mach_o_load_command)); + if (header->ncmds > (filesize - hdrsize) / BFD_MACH_O_LC_SIZE) + { + bfd_set_error (bfd_error_file_truncated); + return false; + } + if (_bfd_mul_overflow (header->ncmds, + sizeof (bfd_mach_o_load_command), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + return false; + } + cmd = bfd_alloc (abfd, amt); if (cmd == NULL) - return FALSE; + return false; for (i = 0; i < header->ncmds; i++) { @@ -5165,27 +5307,28 @@ bfd_mach_o_scan (bfd *abfd, cur->offset = prev->offset + prev->len; } - if (!bfd_mach_o_read_command (abfd, cur)) - return FALSE; + if (!bfd_mach_o_read_command (abfd, cur, filesize)) + return false; } } /* Sections should be flatten before scanning start address. */ - bfd_mach_o_flatten_sections (abfd); + if (!bfd_mach_o_flatten_sections (abfd)) + return false; if (!bfd_mach_o_scan_start_address (abfd)) - return FALSE; + return false; - return TRUE; + return true; } -bfd_boolean +bool bfd_mach_o_mkobject_init (bfd *abfd) { bfd_mach_o_data_struct *mdata = NULL; mdata = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct)); if (mdata == NULL) - return FALSE; + return false; abfd->tdata.mach_o_data = mdata; mdata->header.magic = 0; @@ -5202,16 +5345,16 @@ bfd_mach_o_mkobject_init (bfd *abfd) mdata->sections = NULL; mdata->dyn_reloc_cache = NULL; - return TRUE; + return true; } -static bfd_boolean +static bool bfd_mach_o_gen_mkobject (bfd *abfd) { bfd_mach_o_data_struct *mdata; if (!bfd_mach_o_mkobject_init (abfd)) - return FALSE; + return false; mdata = bfd_mach_o_get_data (abfd); mdata->header.magic = BFD_MACH_O_MH_MAGIC; @@ -5220,14 +5363,14 @@ bfd_mach_o_gen_mkobject (bfd *abfd) mdata->header.byteorder = abfd->xvec->byteorder; mdata->header.version = 1; - return TRUE; + return true; } -const bfd_target * +bfd_cleanup bfd_mach_o_header_p (bfd *abfd, file_ptr hdr_off, - bfd_mach_o_filetype filetype, - bfd_mach_o_cpu_type cputype) + bfd_mach_o_filetype file_type, + bfd_mach_o_cpu_type cpu_type) { bfd_mach_o_header header; bfd_mach_o_data_struct *mdata; @@ -5254,9 +5397,9 @@ bfd_mach_o_header_p (bfd *abfd, /* Check cputype and filetype. In case of wildcard, do not accept magics that are handled by existing targets. */ - if (cputype) + if (cpu_type) { - if (header.cputype != cputype) + if (header.cputype != cpu_type) goto wrong; } else @@ -5269,9 +5412,9 @@ bfd_mach_o_header_p (bfd *abfd, #endif } - if (filetype) + if (file_type) { - if (header.filetype != filetype) + if (header.filetype != file_type) goto wrong; } else @@ -5294,7 +5437,7 @@ bfd_mach_o_header_p (bfd *abfd, if (!bfd_mach_o_scan (abfd, &header, mdata)) goto wrong; - return abfd->xvec; + return _bfd_no_cleanup; wrong: bfd_set_error (bfd_error_wrong_format); @@ -5303,13 +5446,13 @@ bfd_mach_o_header_p (bfd *abfd, return NULL; } -static const bfd_target * +static bfd_cleanup bfd_mach_o_gen_object_p (bfd *abfd) { return bfd_mach_o_header_p (abfd, 0, 0, 0); } -static const bfd_target * +static bfd_cleanup bfd_mach_o_gen_core_p (bfd *abfd) { return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_MH_CORE, 0); @@ -5359,12 +5502,14 @@ typedef struct mach_o_fat_data_struct mach_o_fat_archentry *archentries; } mach_o_fat_data_struct; -const bfd_target * +bfd_cleanup bfd_mach_o_fat_archive_p (bfd *abfd) { mach_o_fat_data_struct *adata = NULL; struct mach_o_fat_header_external hdr; unsigned long i; + size_t amt; + ufile_ptr filesize; if (bfd_seek (abfd, 0, SEEK_SET) != 0 || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr)) @@ -5384,11 +5529,17 @@ bfd_mach_o_fat_archive_p (bfd *abfd) if (adata->nfat_arch > 30) goto error; - adata->archentries = - bfd_alloc2 (abfd, adata->nfat_arch, sizeof (mach_o_fat_archentry)); + if (_bfd_mul_overflow (adata->nfat_arch, + sizeof (mach_o_fat_archentry), &amt)) + { + bfd_set_error (bfd_error_file_too_big); + goto error; + } + adata->archentries = bfd_alloc (abfd, amt); if (adata->archentries == NULL) goto error; + filesize = bfd_get_file_size (abfd); for (i = 0; i < adata->nfat_arch; i++) { struct mach_o_fat_arch_external arch; @@ -5399,11 +5550,20 @@ bfd_mach_o_fat_archive_p (bfd *abfd) adata->archentries[i].offset = bfd_getb32 (arch.offset); adata->archentries[i].size = bfd_getb32 (arch.size); adata->archentries[i].align = bfd_getb32 (arch.align); + if (filesize != 0 + && (adata->archentries[i].offset > filesize + || (adata->archentries[i].size + > filesize - adata->archentries[i].offset))) + { + bfd_release (abfd, adata); + bfd_set_error (bfd_error_malformed_archive); + return NULL; + } } abfd->tdata.mach_o_fat_data = adata; - return abfd->xvec; + return _bfd_no_cleanup; error: if (adata != NULL) @@ -5416,7 +5576,7 @@ bfd_mach_o_fat_archive_p (bfd *abfd) ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY. Set arelt_data and origin fields too. */ -static bfd_boolean +static bool bfd_mach_o_fat_member_init (bfd *abfd, enum bfd_architecture arch_type, unsigned long arch_subtype, @@ -5425,35 +5585,32 @@ bfd_mach_o_fat_member_init (bfd *abfd, struct areltdata *areltdata; /* Create the member filename. Use ARCH_NAME. */ const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype); - char *filename; + const char *filename; if (ap) { /* Use the architecture name if known. */ - filename = bfd_strdup (ap->printable_name); - if (filename == NULL) - return FALSE; + filename = bfd_set_filename (abfd, ap->printable_name); } else { /* Forge a uniq id. */ - const size_t namelen = 2 + 8 + 1 + 2 + 8 + 1; - filename = bfd_malloc (namelen); - if (filename == NULL) - return FALSE; - snprintf (filename, namelen, "0x%lx-0x%lx", + char buf[2 + 8 + 1 + 2 + 8 + 1]; + snprintf (buf, sizeof (buf), "0x%lx-0x%lx", entry->cputype, entry->cpusubtype); + filename = bfd_set_filename (abfd, buf); } - bfd_set_filename (abfd, filename); + if (!filename) + return false; areltdata = bfd_zmalloc (sizeof (struct areltdata)); if (areltdata == NULL) - return FALSE; + return false; areltdata->parsed_size = entry->size; abfd->arelt_data = areltdata; abfd->iostream = NULL; abfd->origin = entry->offset; - return TRUE; + return true; } bfd * @@ -5598,11 +5755,11 @@ bfd_mach_o_fat_extract (bfd *abfd, return NULL; } -static bfd_boolean +static bool bfd_mach_o_fat_close_and_cleanup (bfd *abfd) { _bfd_unlink_from_archive_parent (abfd); - return TRUE; + return true; } int @@ -5752,6 +5909,8 @@ bfd_mach_o_core_fetch_environment (bfd *abfd, unsigned char *buf = bfd_malloc (1024); unsigned long size = 1024; + if (buf == NULL) + return -1; for (;;) { bfd_size_type nread = 0; @@ -5783,7 +5942,8 @@ bfd_mach_o_core_fetch_environment (bfd *abfd, { unsigned long val; - val = *((unsigned long *) (buf + size - offset)); + val = bfd_get_32(abfd, buf + size - offset); + if (! found_nonnull) { if (val != 0) @@ -5797,6 +5957,8 @@ bfd_mach_o_core_fetch_environment (bfd *abfd, bottom = seg->fileoff + seg->filesize - offset; top = seg->fileoff + seg->filesize - 4; *rbuf = bfd_malloc (top - bottom); + if (*rbuf == NULL) + return -1; *rlen = top - bottom; memcpy (*rbuf, buf + size - *rlen, *rlen); @@ -5844,13 +6006,13 @@ bfd_mach_o_lookup_uuid_command (bfd *abfd) bfd_mach_o_load_command *uuid_cmd = NULL; int ncmd = bfd_mach_o_lookup_command (abfd, BFD_MACH_O_LC_UUID, &uuid_cmd); if (ncmd != 1 || uuid_cmd == NULL) - return FALSE; + return false; return &uuid_cmd->command.uuid; } /* Return true if ABFD is a dSYM file and its UUID matches UUID_CMD. */ -static bfd_boolean +static bool bfd_mach_o_dsym_for_uuid_p (bfd *abfd, const bfd_mach_o_uuid_command *uuid_cmd) { bfd_mach_o_uuid_command *dsym_uuid_cmd; @@ -5859,22 +6021,22 @@ bfd_mach_o_dsym_for_uuid_p (bfd *abfd, const bfd_mach_o_uuid_command *uuid_cmd) BFD_ASSERT (uuid_cmd); if (!bfd_check_format (abfd, bfd_object)) - return FALSE; + return false; if (bfd_get_flavour (abfd) != bfd_target_mach_o_flavour || bfd_mach_o_get_data (abfd) == NULL || bfd_mach_o_get_data (abfd)->header.filetype != BFD_MACH_O_MH_DSYM) - return FALSE; + return false; dsym_uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd); if (dsym_uuid_cmd == NULL) - return FALSE; + return false; if (memcmp (uuid_cmd->uuid, dsym_uuid_cmd->uuid, sizeof (uuid_cmd->uuid)) != 0) - return FALSE; + return false; - return TRUE; + return true; } /* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD. @@ -5923,12 +6085,12 @@ bfd_mach_o_follow_dsym (bfd *abfd) if (abfd->my_archive && !bfd_is_thin_archive (abfd->my_archive)) base_bfd = abfd->my_archive; /* BFD may have been opened from a stream. */ - if (base_bfd->filename == NULL) + if (bfd_get_filename (base_bfd) == NULL) { bfd_set_error (bfd_error_invalid_operation); return NULL; } - base_basename = lbasename (base_bfd->filename); + base_basename = lbasename (bfd_get_filename (base_bfd)); uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd); if (uuid_cmd == NULL) @@ -5938,11 +6100,14 @@ bfd_mach_o_follow_dsym (bfd *abfd) It seems apple's GDB checks all files in the dSYM bundle directory. http://opensource.apple.com/source/gdb/gdb-1708/src/gdb/macosx/macosx-tdep.c */ - dsym_filename = (char *)bfd_malloc (strlen (base_bfd->filename) + dsym_filename = (char *)bfd_malloc (strlen (bfd_get_filename (base_bfd)) + strlen (dsym_subdir) + 1 + strlen (base_basename) + 1); + if (dsym_filename == NULL) + return NULL; + sprintf (dsym_filename, "%s%s/%s", - base_bfd->filename, dsym_subdir, base_basename); + bfd_get_filename (base_bfd), dsym_subdir, base_basename); dsym_bfd = bfd_mach_o_find_dsym (dsym_filename, uuid_cmd, bfd_get_arch_info (abfd)); @@ -5952,7 +6117,7 @@ bfd_mach_o_follow_dsym (bfd *abfd) return dsym_bfd; } -bfd_boolean +bool bfd_mach_o_find_nearest_line (bfd *abfd, asymbol **symbols, asection *section, @@ -5964,7 +6129,7 @@ bfd_mach_o_find_nearest_line (bfd *abfd, { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); if (mdata == NULL) - return FALSE; + return false; switch (mdata->header.filetype) { case BFD_MACH_O_MH_OBJECT: @@ -5985,12 +6150,12 @@ bfd_mach_o_find_nearest_line (bfd *abfd, if (! _bfd_dwarf2_slurp_debug_info (abfd, mdata->dsym_bfd, dwarf_debug_sections, symbols, &mdata->dwarf2_find_line_info, - FALSE)) - return FALSE; + false)) + return false; } break; default: - return FALSE; + return false; } return _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, filename_ptr, functionname_ptr, @@ -5999,7 +6164,7 @@ bfd_mach_o_find_nearest_line (bfd *abfd, &mdata->dwarf2_find_line_info); } -bfd_boolean +bool bfd_mach_o_close_and_cleanup (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -6020,8 +6185,8 @@ bfd_mach_o_close_and_cleanup (bfd *abfd) but it is small, and happens when we are closing down, so it should not matter too much. */ char *dsym_filename = (char *)(fat_bfd - ? fat_bfd->filename - : mdata->dsym_bfd->filename); + ? bfd_get_filename (fat_bfd) + : bfd_get_filename (mdata->dsym_bfd)); #endif bfd_close (mdata->dsym_bfd); mdata->dsym_bfd = NULL; @@ -6036,7 +6201,7 @@ bfd_mach_o_close_and_cleanup (bfd *abfd) return _bfd_generic_close_and_cleanup (abfd); } -bfd_boolean +bool bfd_mach_o_free_cached_info (bfd *abfd) { bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); @@ -6049,7 +6214,7 @@ bfd_mach_o_free_cached_info (bfd *abfd) asect->relocation = NULL; } - return TRUE; + return true; } #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup