Fix type of .persistent.bss section
[deliverable/binutils-gdb.git] / bfd / mach-o.c
index 9fe6326a299bc86d4ff4e86824b90a78e60959a3..60121c4ef4798024bb097dffc573a8db3ccec8ce 100644 (file)
@@ -1,5 +1,5 @@
 /* Mach-O support for BFD.
 /* Mach-O support for BFD.
-   Copyright (C) 1999-2017 Free Software Foundation, Inc.
+   Copyright (C) 1999-2021 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
 
    This file is part of BFD, the Binary File Descriptor library.
 
    MA 02110-1301, USA.  */
 
 #include "sysdep.h"
    MA 02110-1301, USA.  */
 
 #include "sysdep.h"
-#include "mach-o.h"
+#include <limits.h>
 #include "bfd.h"
 #include "libbfd.h"
 #include "libiberty.h"
 #include "bfd.h"
 #include "libbfd.h"
 #include "libiberty.h"
+#include "mach-o.h"
 #include "aout/stab_gnu.h"
 #include "mach-o/reloc.h"
 #include "mach-o/external.h"
 #include "aout/stab_gnu.h"
 #include "mach-o/reloc.h"
 #include "mach-o/external.h"
@@ -37,7 +38,7 @@
 #define FILE_ALIGN(off, algn) \
   (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1U << (algn)))
 
 #define FILE_ALIGN(off, algn) \
   (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1U << (algn)))
 
-static bfd_boolean
+static bool
 bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd);
 
 unsigned int
 bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd);
 
 unsigned int
@@ -51,36 +52,36 @@ bfd_mach_o_version (bfd *abfd)
   return mdata->header.version;
 }
 
   return mdata->header.version;
 }
 
-bfd_boolean
+bool
 bfd_mach_o_valid (bfd *abfd)
 {
   if (abfd == NULL || abfd->xvec == NULL)
 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)
 
   if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
-    return FALSE;
+    return false;
 
   if (bfd_mach_o_get_data (abfd) == NULL)
 
   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:
 mach_o_wide_p (bfd_mach_o_header *header)
 {
   switch (header->version)
     {
     case 1:
-      return FALSE;
+      return false;
     case 2:
     case 2:
-      return TRUE;
+      return true;
     default:
       BFD_FAIL ();
     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);
 bfd_mach_o_wide_p (bfd *abfd)
 {
   return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
@@ -315,9 +316,9 @@ bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
   for (seg = segsec_names_xlat; seg->segname; seg++)
     if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
       for (sec = seg->sections; sec->mach_o_name; sec++)
   for (seg = segsec_names_xlat; seg->segname; seg++)
     if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
       for (sec = seg->sections; sec->mach_o_name; sec++)
-        if (strncmp (sec->mach_o_name, sectname,
+       if (strncmp (sec->mach_o_name, sectname,
                     BFD_MACH_O_SECTNAME_SIZE) == 0)
                     BFD_MACH_O_SECTNAME_SIZE) == 0)
-          return sec;
+         return sec;
 
   return NULL;
 }
 
   return NULL;
 }
@@ -432,11 +433,11 @@ bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
 
 static const mach_o_section_name_xlat *
 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
 
 static const mach_o_section_name_xlat *
 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
-                                           asection *sect,
-                                           bfd_mach_o_section *section)
+                                          asection *sect,
+                                          bfd_mach_o_section *section)
 {
   const mach_o_section_name_xlat *xlat;
 {
   const mach_o_section_name_xlat *xlat;
-  const char *name = bfd_get_section_name (abfd, sect);
+  const char *name = bfd_section_name (sect);
   const char *segname;
   const char *dot;
   unsigned int len;
   const char *segname;
   const char *dot;
   unsigned int len;
@@ -472,13 +473,13 @@ bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
 
       if (seglen <= BFD_MACH_O_SEGNAME_SIZE
          && seclen <= BFD_MACH_O_SECTNAME_SIZE)
 
       if (seglen <= BFD_MACH_O_SEGNAME_SIZE
          && seclen <= BFD_MACH_O_SECTNAME_SIZE)
-        {
-          memcpy (section->segname, name, seglen);
-          section->segname[seglen] = 0;
-          memcpy (section->sectname, dot + 1, seclen);
-          section->sectname[seclen] = 0;
-          return NULL;
-        }
+       {
+         memcpy (section->segname, name, seglen);
+         section->segname[seglen] = 0;
+         memcpy (section->sectname, dot + 1, seclen);
+         section->sectname[seclen] = 0;
+         return NULL;
+       }
     }
 
   /* The segment and section names are both missing - don't make them
     }
 
   /* The segment and section names are both missing - don't make them
@@ -550,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.  */
 
 /* 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,
 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
                                         asymbol *isymbol,
                                         bfd *obfd ATTRIBUTE_UNUSED,
@@ -565,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;
 
   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.  */
 
 }
 
 /* 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)
 {
 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection,
                                          bfd *obfd, asection *osection)
 {
@@ -580,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)
 
   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);
 
 
   BFD_ASSERT (is != NULL && os != NULL);
 
@@ -589,13 +590,130 @@ bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection,
   os->reserved2 = is->reserved2;
   os->reserved3 = is->reserved3;
 
   os->reserved2 = is->reserved2;
   os->reserved3 = is->reserved3;
 
-  return TRUE;
+  return true;
+}
+
+static const char *
+cputype (unsigned long value)
+{
+  switch (value)
+    {
+    case BFD_MACH_O_CPU_TYPE_VAX: return "VAX";
+    case BFD_MACH_O_CPU_TYPE_MC680x0: return "MC68k";
+    case BFD_MACH_O_CPU_TYPE_I386: return "I386";
+    case BFD_MACH_O_CPU_TYPE_MIPS: return "MIPS";
+    case BFD_MACH_O_CPU_TYPE_MC98000: return "MC98k";
+    case BFD_MACH_O_CPU_TYPE_HPPA: return "HPPA";
+    case BFD_MACH_O_CPU_TYPE_ARM: return "ARM";
+    case BFD_MACH_O_CPU_TYPE_MC88000: return "MC88K";
+    case BFD_MACH_O_CPU_TYPE_SPARC: return "SPARC";
+    case BFD_MACH_O_CPU_TYPE_I860: return "I860";
+    case BFD_MACH_O_CPU_TYPE_ALPHA: return "ALPHA";
+    case BFD_MACH_O_CPU_TYPE_POWERPC: return "PPC";
+    case BFD_MACH_O_CPU_TYPE_POWERPC_64: return "PPC64";
+    case BFD_MACH_O_CPU_TYPE_X86_64: return "X86_64";
+    case BFD_MACH_O_CPU_TYPE_ARM64: return "ARM64";
+    default: return _("<unknown>");
+    }
+}
+
+static const char *
+cpusubtype (unsigned long cpu_type, unsigned long cpu_subtype, char *buffer)
+{
+  buffer[0] = 0;
+  switch (cpu_subtype & BFD_MACH_O_CPU_SUBTYPE_MASK)
+    {
+    case 0:
+      break;
+    case BFD_MACH_O_CPU_SUBTYPE_LIB64:
+      sprintf (buffer, " (LIB64)"); break;
+    default:
+      sprintf (buffer, _("<unknown mask flags>")); break;
+    }
+
+  cpu_subtype &= ~ BFD_MACH_O_CPU_SUBTYPE_MASK;
+
+  switch (cpu_type)
+    {
+    case BFD_MACH_O_CPU_TYPE_X86_64:
+    case BFD_MACH_O_CPU_TYPE_I386:
+      switch (cpu_subtype)
+       {
+       case BFD_MACH_O_CPU_SUBTYPE_X86_ALL:
+         return strcat (buffer, " (X86_ALL)");
+       default:
+         break;
+       }
+      break;
+
+    case BFD_MACH_O_CPU_TYPE_ARM:
+      switch (cpu_subtype)
+       {
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
+         return strcat (buffer, " (ARM_ALL)");
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
+         return strcat (buffer, " (ARM_V4T)");
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
+         return strcat (buffer, " (ARM_V6)");
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
+         return strcat (buffer, " (ARM_V5TEJ)");
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
+         return strcat (buffer, " (ARM_XSCALE)");
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
+         return strcat (buffer, " (ARM_V7)");
+       default:
+         break;
+       }
+      break;
+
+    case BFD_MACH_O_CPU_TYPE_ARM64:
+      switch (cpu_subtype)
+       {
+       case BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL:
+         return strcat (buffer, " (ARM64_ALL)");
+       case BFD_MACH_O_CPU_SUBTYPE_ARM64_V8:
+         return strcat (buffer, " (ARM64_V8)");
+       default:
+         break;
+       }
+      break;
+
+    default:
+      break;
+    }
+
+  if (cpu_subtype != 0)
+    return strcat (buffer, _(" (<unknown>)"));
+
+  return buffer;
+}
+
+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, 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;
 }
 
 /* Copy any private info we understand from the input bfd
    to the output bfd.  */
 
 }
 
 /* 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;
 bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd)
 {
   bfd_mach_o_data_struct *imdata;
@@ -604,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)
 
   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));
 
   BFD_ASSERT (bfd_mach_o_valid (ibfd));
   BFD_ASSERT (bfd_mach_o_valid (obfd));
@@ -615,6 +733,21 @@ bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd)
   /* Copy header flags.  */
   omdata->header.flags = imdata->header.flags;
 
   /* Copy header flags.  */
   omdata->header.flags = imdata->header.flags;
 
+  /* PR 23299.  Copy the cputype.  */
+  if (imdata->header.cputype != omdata->header.cputype)
+    {
+      if (omdata->header.cputype == 0)
+       omdata->header.cputype = imdata->header.cputype;
+      else if (imdata->header.cputype != 0)
+       /* Urg - what has happened ?  */
+       _bfd_error_handler (_("incompatible cputypes in mach-o files: %ld vs %ld"),
+                           (long) imdata->header.cputype,
+                           (long) omdata->header.cputype);
+    }
+
+  /* Copy the cpusubtype.  */
+  omdata->header.cpusubtype = imdata->header.cpusubtype;
+
   /* Copy commands.  */
   for (icmd = imdata->first_command; icmd != NULL; icmd = icmd->next)
     {
   /* Copy commands.  */
   for (icmd = imdata->first_command; icmd != NULL; icmd = icmd->next)
     {
@@ -628,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)
          /* 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;
 
          /* Copy common fields.  */
          ocmd->type = icmd->type;
@@ -716,23 +849,23 @@ bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd)
       bfd_mach_o_append_command (obfd, ocmd);
     }
 
       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.  */
 
 }
 
 /* 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)
 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;
 
   mdata->header.flags = flags;
-  return TRUE;
+  return true;
 }
 
 /* Count the total number of symbols.  */
 }
 
 /* Count the total number of symbols.  */
@@ -776,7 +909,7 @@ bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
   if (!bfd_mach_o_read_symtab_symbols (abfd))
     {
       _bfd_error_handler
   if (!bfd_mach_o_read_symtab_symbols (abfd))
     {
       _bfd_error_handler
-        (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
+       (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
       return 0;
     }
 
       return 0;
     }
 
@@ -794,11 +927,11 @@ bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
 
 long
 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
 
 long
 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
-                                 long symcount ATTRIBUTE_UNUSED,
-                                 asymbol **syms ATTRIBUTE_UNUSED,
-                                 long dynsymcount ATTRIBUTE_UNUSED,
-                                 asymbol **dynsyms ATTRIBUTE_UNUSED,
-                                 asymbol **ret)
+                                long symcount ATTRIBUTE_UNUSED,
+                                asymbol **syms ATTRIBUTE_UNUSED,
+                                long dynsymcount ATTRIBUTE_UNUSED,
+                                asymbol **dynsyms ATTRIBUTE_UNUSED,
+                                asymbol **ret)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
@@ -853,63 +986,63 @@ bfd_mach_o_get_synthetic_symtab (bfd *abfd,
       bfd_vma entry_size;
 
       switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
       bfd_vma entry_size;
 
       switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
-        {
-        case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
-        case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
-        case BFD_MACH_O_S_SYMBOL_STUBS:
-          /* Only these sections have indirect symbols.  */
-          first = sec->reserved1;
-          last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
-          addr = sec->addr;
-          entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
+       {
+       case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
+       case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
+       case BFD_MACH_O_S_SYMBOL_STUBS:
+         /* Only these sections have indirect symbols.  */
+         first = sec->reserved1;
+         last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
+         addr = sec->addr;
+         entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
 
          /* PR 17512: file: 08e15eec.  */
          if (first >= count || last >= count || first > last)
            goto fail;
 
 
          /* PR 17512: file: 08e15eec.  */
          if (first >= count || last >= count || first > last)
            goto fail;
 
-          for (j = first; j < last; j++)
-            {
-              unsigned int isym = dysymtab->indirect_syms[j];
+         for (j = first; j < last; j++)
+           {
+             unsigned int isym = dysymtab->indirect_syms[j];
 
              /* PR 17512: file: 04d64d9b.  */
              if (((char *) s) + sizeof (* s) > s_end)
                goto fail;
 
 
              /* PR 17512: file: 04d64d9b.  */
              if (((char *) s) + sizeof (* s) > s_end)
                goto fail;
 
-              s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
-              s->section = sec->bfdsection;
-              s->value = addr - sec->addr;
-              s->udata.p = NULL;
+             s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
+             s->section = sec->bfdsection;
+             s->value = addr - sec->addr;
+             s->udata.p = NULL;
 
 
-              if (isym < symtab->nsyms
-                  && symtab->symbols[isym].symbol.name)
-                {
-                  const char *sym = symtab->symbols[isym].symbol.name;
-                  size_t len;
+             if (isym < symtab->nsyms
+                 && symtab->symbols[isym].symbol.name)
+               {
+                 const char *sym = symtab->symbols[isym].symbol.name;
+                 size_t len;
 
 
-                  s->name = names;
-                  len = strlen (sym);
+                 s->name = names;
+                 len = strlen (sym);
                  /* PR 17512: file: 47dfd4d2.  */
                  if (names + len >= s_end)
                    goto fail;
                  /* PR 17512: file: 47dfd4d2.  */
                  if (names + len >= s_end)
                    goto fail;
-                  memcpy (names, sym, len);
-                  names += len;
+                 memcpy (names, sym, len);
+                 names += len;
                  /* PR 17512: file: 18f340a4.  */
                  if (names + sizeof (stub) >= s_end)
                    goto fail;
                  /* PR 17512: file: 18f340a4.  */
                  if (names + sizeof (stub) >= s_end)
                    goto fail;
-                  memcpy (names, stub, sizeof (stub));
-                  names += sizeof (stub);
-                }
-              else
-                s->name = nul_name;
-
-              addr += entry_size;
-              s++;
-              n++;
-            }
-          break;
-        default:
-          break;
-        }
+                 memcpy (names, stub, sizeof (stub));
+                 names += sizeof (stub);
+               }
+             else
+               s->name = nul_name;
+
+             addr += entry_size;
+             s++;
+             n++;
+           }
+         break;
+       default:
+         break;
+       }
     }
 
   return n;
     }
 
   return n;
@@ -951,10 +1084,10 @@ bfd_mach_o_print_symbol (bfd *abfd,
        switch (asym->n_type & BFD_MACH_O_N_TYPE)
          {
          case BFD_MACH_O_N_UNDF:
        switch (asym->n_type & BFD_MACH_O_N_TYPE)
          {
          case BFD_MACH_O_N_UNDF:
-            if (symbol->value == 0)
-              name = "UND";
-            else
-              name = "COM";
+           if (symbol->value == 0)
+             name = "UND";
+           else
+             name = "COM";
            break;
          case BFD_MACH_O_N_ABS:
            name = "ABS";
            break;
          case BFD_MACH_O_N_ABS:
            name = "ABS";
@@ -975,7 +1108,7 @@ bfd_mach_o_print_symbol (bfd *abfd,
       if (name == NULL)
        name = "";
       fprintf (file, " %02x %-6s %02x %04x",
       if (name == NULL)
        name = "";
       fprintf (file, " %02x %-6s %02x %04x",
-               asym->n_type, name, asym->n_sect, asym->n_desc);
+              asym->n_type, name, asym->n_sect, asym->n_desc);
       if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
          && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
        fprintf (file, " [%s]", symbol->section->name);
       if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
          && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
        fprintf (file, " [%s]", symbol->section->name);
@@ -1019,37 +1152,31 @@ bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
     case BFD_MACH_O_CPU_TYPE_ARM:
       *type = bfd_arch_arm;
       switch (msubtype)
     case BFD_MACH_O_CPU_TYPE_ARM:
       *type = bfd_arch_arm;
       switch (msubtype)
-        {
-        case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
-          *subtype = bfd_mach_arm_4T;
-          break;
-        case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
-          *subtype = bfd_mach_arm_4T;  /* Best fit ?  */
-          break;
-        case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
-          *subtype = bfd_mach_arm_5TE;
-          break;
-        case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
-          *subtype = bfd_mach_arm_XScale;
-          break;
-        case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
-          *subtype = bfd_mach_arm_5TE; /* Best fit ?  */
-          break;
-        case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
-        default:
-          break;
-        }
-      break;
-    case BFD_MACH_O_CPU_TYPE_MC88000:
-      *type = bfd_arch_m88k;
+       {
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
+         *subtype = bfd_mach_arm_4T;
+         break;
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
+         *subtype = bfd_mach_arm_4T;   /* Best fit ?  */
+         break;
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
+         *subtype = bfd_mach_arm_5TE;
+         break;
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
+         *subtype = bfd_mach_arm_XScale;
+         break;
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
+         *subtype = bfd_mach_arm_5TE;  /* Best fit ?  */
+         break;
+       case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
+       default:
+         break;
+       }
       break;
     case BFD_MACH_O_CPU_TYPE_SPARC:
       *type = bfd_arch_sparc;
       *subtype = bfd_mach_sparc;
       break;
       break;
     case BFD_MACH_O_CPU_TYPE_SPARC:
       *type = bfd_arch_sparc;
       *subtype = bfd_mach_sparc;
       break;
-    case BFD_MACH_O_CPU_TYPE_I860:
-      *type = bfd_arch_i860;
-      break;
     case BFD_MACH_O_CPU_TYPE_ALPHA:
       *type = bfd_arch_alpha;
       break;
     case BFD_MACH_O_CPU_TYPE_ALPHA:
       *type = bfd_arch_alpha;
       break;
@@ -1112,7 +1239,7 @@ bfd_mach_o_pad_command (bfd *abfd, unsigned int len)
     return 0;
 }
 
     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;
 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
 {
   struct mach_o_header_external raw;
@@ -1134,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)
 
   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;
 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_thread_command *cmd = &command->command.thread;
@@ -1155,22 +1282,22 @@ bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
     {
       BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
       BFD_ASSERT (cmd->flavours[i].offset ==
     {
       BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
       BFD_ASSERT (cmd->flavours[i].offset ==
-                  (command->offset + offset + BFD_MACH_O_LC_SIZE));
+                 (command->offset + offset + BFD_MACH_O_LC_SIZE));
 
       bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
       bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
 
       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
 
       bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
       bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
 
       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
-          || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
-       return FALSE;
+         || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+       return false;
 
       offset += cmd->flavours[i].size + sizeof (raw);
     }
 
 
       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;
 bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
@@ -1181,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))
 
   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)
 
   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)
 
   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;
 bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylib_command *cmd = &command->command.dylib;
@@ -1207,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))
 
   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)
 
   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)
 
   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;
 bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_main_command *cmd = &command->command.main;
@@ -1230,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))
 
   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;
 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;
@@ -1254,46 +1381,53 @@ 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))
 
   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))
 
   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))
 
   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))
 
   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))
 
   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))
 
   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
 bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
 }
 
 long
 bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
-                                  asection *asect)
+                                 asection *asect)
 {
 {
-  return (asect->reloc_count + 1) * sizeof (arelent *);
+#if SIZEOF_LONG == SIZEOF_INT
+   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
+    {
+      bfd_set_error (bfd_error_file_too_big);
+      return -1;
+    }
+#endif
+ return (asect->reloc_count + 1) * sizeof (arelent *);
 }
 
 /* In addition to the need to byte-swap the symbol number, the bit positions
 }
 
 /* In addition to the need to byte-swap the symbol number, the bit positions
@@ -1301,7 +1435,7 @@ bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
 
 void
 bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
 
 void
 bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
-                                      unsigned char *fields)
+                                       unsigned char *fields)
 {
   unsigned char info = fields[3];
 
 {
   unsigned char info = fields[3];
 
@@ -1327,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.  */
 
 
 /* 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)
 bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
                                             bfd_mach_o_reloc_info *reloc,
                                             arelent *res, asymbol **syms)
@@ -1366,7 +1500,11 @@ bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
     {
       /* PR 17512: file: 006-2964-0.004.  */
       if (num > mdata->nsects)
     {
       /* PR 17512: file: 006-2964-0.004.  */
       if (num > mdata->nsects)
-       return FALSE;
+       {
+         _bfd_error_handler (_("\
+malformed mach-o reloc: section index is greater than the number of sections"));
+         return false;
+       }
 
       /* A section number.  */
       sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
 
       /* A section number.  */
       sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
@@ -1374,7 +1512,7 @@ bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
         binary) contains the address of the section.  To comply with
         bfd convention, subtract the section address.
         Use the address from the header, so that the user can modify
         binary) contains the address of the section.  To comply with
         bfd convention, subtract the section address.
         Use the address from the header, so that the user can modify
-             the vma of the section.  */
+            the vma of the section.  */
       res->addend = -mdata->sections[num - 1]->addr;
     }
 
       res->addend = -mdata->sections[num - 1]->addr;
     }
 
@@ -1386,7 +1524,7 @@ bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
      the PPC swap_reloc_in routine.  */
   res->sym_ptr_ptr = sym;
 
      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
 }
 
 /* Do most of the work for canonicalize_relocs on RAW: create internal
@@ -1397,7 +1535,7 @@ bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
    (like arm64) don't follow the generic rules (symnum is a value for the
    non-scattered relocation ADDEND).  */
 
    (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,
 bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd,
                                       struct mach_o_reloc_info_external *raw,
                                       bfd_mach_o_reloc_info *reloc,
@@ -1427,15 +1565,15 @@ bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd,
         the following section or, if it falls within alignment padding, as
         null - which will assert later.  */
       for (j = 0; j < mdata->nsects; j++)
         the following section or, if it falls within alignment padding, as
         null - which will assert later.  */
       for (j = 0; j < mdata->nsects; j++)
-        {
-          bfd_mach_o_section *sect = mdata->sections[j];
-          if (symnum >= sect->addr && symnum < sect->addr + sect->size)
-            {
-              res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
-              res->addend = symnum - sect->addr;
-              break;
-            }
-        }
+       {
+         bfd_mach_o_section *sect = mdata->sections[j];
+         if (symnum >= sect->addr && symnum < sect->addr + sect->size)
+           {
+             res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
+             res->addend = symnum - sect->addr;
+             break;
+           }
+       }
 
       /* Extract the info and address fields from r_address.  */
       reloc->r_type = BFD_MACH_O_GET_SR_TYPE (addr);
 
       /* Extract the info and address fields from r_address.  */
       reloc->r_type = BFD_MACH_O_GET_SR_TYPE (addr);
@@ -1452,63 +1590,62 @@ bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd,
       res->address = addr;
 
       /* The value and info fields have to be extracted dependent on target
       res->address = addr;
 
       /* The value and info fields have to be extracted dependent on target
-         endian-ness.  */
+        endian-ness.  */
       bfd_mach_o_swap_in_non_scattered_reloc (abfd, reloc, raw->r_symbolnum);
 
       if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, reloc,
                                                        res, syms))
       bfd_mach_o_swap_in_non_scattered_reloc (abfd, reloc, raw->r_symbolnum);
 
       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.  */
 
     }
 
   /* 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
 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
 }
 
 static int
 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
-                                unsigned long count,
-                                arelent *res, asymbol **syms)
+                               unsigned long count,
+                               arelent *res, asymbol **syms)
 {
   bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
   unsigned long i;
 {
   bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
   unsigned long i;
-  struct mach_o_reloc_info_external *native_relocs;
-  bfd_size_type native_size;
+  struct mach_o_reloc_info_external *native_relocs = NULL;
+  size_t native_size;
 
   /* Allocate and read relocs.  */
 
   /* Allocate and read relocs.  */
-  native_size = count * BFD_MACH_O_RELENT_SIZE;
+  if (_bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &native_size))
+    /* PR 17512: file: 09477b57.  */
+    goto err;
 
 
-  /* PR 17512: file: 09477b57.  */
-  if (native_size < count)
+  if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
     return -1;
     return -1;
-
-  native_relocs =
-    (struct mach_o_reloc_info_external *) bfd_malloc (native_size);
+  native_relocs = (struct mach_o_reloc_info_external *)
+    _bfd_malloc_and_read (abfd, native_size, native_size);
   if (native_relocs == NULL)
     return -1;
 
   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],
                                                      &res[i], syms, res))
   for (i = 0; i < count; i++)
     {
       if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i],
                                                      &res[i], syms, res))
-        goto err;
+       goto err;
     }
   free (native_relocs);
   return i;
     }
   free (native_relocs);
   return i;
+
  err:
   free (native_relocs);
  err:
   free (native_relocs);
+  if (bfd_get_error () == bfd_error_no_error)
+    bfd_set_error (bfd_error_invalid_operation);
   return -1;
 }
 
 long
 bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
   return -1;
 }
 
 long
 bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
-                               arelent **rels, asymbol **syms)
+                              arelent **rels, asymbol **syms)
 {
   bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
   unsigned long i;
 {
   bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
   unsigned long i;
@@ -1523,18 +1660,20 @@ bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
 
   if (asect->relocation == NULL)
     {
 
   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;
        return -1;
-      res = bfd_malloc (asect->reloc_count * sizeof (arelent));
+      res = bfd_malloc (amt);
       if (res == NULL)
       if (res == NULL)
-        return -1;
+       return -1;
 
       if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
 
       if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
-                                          asect->reloc_count, res, syms) < 0)
-        {
-          free (res);
-          return -1;
-        }
+                                         asect->reloc_count, res, syms) < 0)
+       {
+         free (res);
+         return -1;
+       }
       asect->relocation = res;
     }
 
       asect->relocation = res;
     }
 
@@ -1559,7 +1698,7 @@ bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
 
 long
 bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
 
 long
 bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
-                                       struct bfd_symbol **syms)
+                                      struct bfd_symbol **syms)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
@@ -1578,29 +1717,47 @@ bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
 
   if (mdata->dyn_reloc_cache == NULL)
     {
 
   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)
       if (res == NULL)
-        return -1;
+       return -1;
 
       if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
 
       if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
-                                          dysymtab->nextrel, res, syms) < 0)
-        {
-          free (res);
-          return -1;
-        }
+                                         dysymtab->nextrel, res, syms) < 0)
+       {
+         free (res);
+         return -1;
+       }
 
       if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
 
       if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
-                                          dysymtab->nlocrel,
-                                          res + dysymtab->nextrel, syms) < 0)
-        {
-          free (res);
-          return -1;
-        }
+                                         dysymtab->nlocrel,
+                                         res + dysymtab->nextrel, syms) < 0)
+       {
+         free (res);
+         return -1;
+       }
 
       mdata->dyn_reloc_cache = res;
     }
 
       mdata->dyn_reloc_cache = res;
     }
@@ -1617,7 +1774,7 @@ bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
 
 static void
 bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
 
 static void
 bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
-                                      bfd_mach_o_reloc_info *rel)
+                                        bfd_mach_o_reloc_info *rel)
 {
   unsigned char info = 0;
 
 {
   unsigned char info = 0;
 
@@ -1647,7 +1804,7 @@ bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
   fields[3] = info;
 }
 
   fields[3] = info;
 }
 
-static bfd_boolean
+static bool
 bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
 {
   unsigned int i;
 bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
 {
   unsigned int i;
@@ -1657,13 +1814,13 @@ bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
 
   sec = section->bfdsection;
   if (sec->reloc_count == 0)
 
   sec = section->bfdsection;
   if (sec->reloc_count == 0)
-    return TRUE;
+    return true;
 
   if (bed->_bfd_mach_o_swap_reloc_out == NULL)
 
   if (bed->_bfd_mach_o_swap_reloc_out == NULL)
-    return TRUE;
+    return true;
 
   if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
 
   if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
-    return FALSE;
+    return false;
 
   /* Convert and write.  */
   entries = section->bfdsection->orelocation;
 
   /* Convert and write.  */
   entries = section->bfdsection->orelocation;
@@ -1675,37 +1832,37 @@ 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))
 
       /* 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)
 
       /* Lower the relocation info.  */
       if (pinfo->r_scattered)
-        {
-          unsigned long v;
-
-          v = BFD_MACH_O_SR_SCATTERED
-            | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
-            | BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length)
-            | BFD_MACH_O_SET_SR_TYPE (pinfo->r_type)
-            | BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address);
-          /* Note: scattered relocs have field in reverse order...  */
-          bfd_put_32 (abfd, v, raw.r_address);
-          bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
-        }
+       {
+         unsigned long v;
+
+         v = BFD_MACH_O_SR_SCATTERED
+           | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
+           | BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length)
+           | BFD_MACH_O_SET_SR_TYPE (pinfo->r_type)
+           | BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address);
+         /* Note: scattered relocs have field in reverse order...  */
+         bfd_put_32 (abfd, v, raw.r_address);
+         bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
+       }
       else
       else
-        {
-          bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
-          bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum,
+       {
+         bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
+         bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum,
                                                   pinfo);
                                                   pinfo);
-        }
+       }
 
       if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
 
       if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
-          != BFD_MACH_O_RELENT_SIZE)
-        return FALSE;
+         != BFD_MACH_O_RELENT_SIZE)
+       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;
 bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
 {
   struct mach_o_section_32_external raw;
@@ -1724,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)
 
   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;
 bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
 {
   struct mach_o_section_64_external raw;
@@ -1749,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)
 
   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;
 bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
 {
   struct mach_o_segment_command_32_external raw;
@@ -1765,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))
 
   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);
 
   memcpy (raw.segname, seg->segname, 16);
   bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
@@ -1779,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))
 
   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))
 
   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;
 bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
 {
   struct mach_o_segment_command_64_external raw;
@@ -1799,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))
 
   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);
 
   memcpy (raw.segname, seg->segname, 16);
   bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
@@ -1813,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))
 
   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))
 
   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);
 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);
@@ -1834,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)
 
   /* Write the symbols first.  */
   if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
-    return FALSE;
+    return false;
 
   strtab = _bfd_stringtab_init ();
   if (strtab == NULL)
 
   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.  */
 
   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++)
     {
 
   for (i = 0; i < sym->nsyms; i++)
     {
@@ -1853,43 +2010,43 @@ bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym)
 
       if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
        /* An index of 0 always means the empty string.  */
 
       if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
        /* An index of 0 always means the empty string.  */
-        str_index = 0;
+       str_index = 0;
       else
       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;
-        }
+         if (str_index == (bfd_size_type) -1)
+           goto err;
+       }
 
       if (wide)
 
       if (wide)
-        {
-          struct mach_o_nlist_64_external raw;
-
-          bfd_h_put_32 (abfd, str_index, raw.n_strx);
-          bfd_h_put_8 (abfd, s->n_type, raw.n_type);
-          bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
-          bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
-          bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
-                        raw.n_value);
-
-          if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
-            goto err;
-        }
+       {
+         struct mach_o_nlist_64_external raw;
+
+         bfd_h_put_32 (abfd, str_index, raw.n_strx);
+         bfd_h_put_8 (abfd, s->n_type, raw.n_type);
+         bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
+         bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
+         bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
+                       raw.n_value);
+
+         if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+           goto err;
+       }
       else
       else
-        {
-          struct mach_o_nlist_external raw;
+       {
+         struct mach_o_nlist_external raw;
 
 
-          bfd_h_put_32 (abfd, str_index, raw.n_strx);
-          bfd_h_put_8 (abfd, s->n_type, raw.n_type);
-          bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
-          bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
-          bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
-                        raw.n_value);
+         bfd_h_put_32 (abfd, str_index, raw.n_strx);
+         bfd_h_put_8 (abfd, s->n_type, raw.n_type);
+         bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
+         bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
+         bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
+                       raw.n_value);
 
 
-          if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
-            goto err;
-        }
+         if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+           goto err;
+       }
     }
   sym->strsize = _bfd_stringtab_size (strtab);
   sym->stroff = mdata->filelen;
     }
   sym->strsize = _bfd_stringtab_size (strtab);
   sym->stroff = mdata->filelen;
@@ -1904,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)
   /* Pad string table.  */
   padlen = bfd_mach_o_pad4 (abfd, sym->strsize);
   if (padlen < 0)
-    return FALSE;
+    return false;
   mdata->filelen += padlen;
   sym->strsize += padlen;
 
   mdata->filelen += padlen;
   sym->strsize += padlen;
 
-  return TRUE;
+  return true;
 
  err:
   _bfd_stringtab_free (strtab);
   sym->strsize = 0;
 
  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;
 bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_symtab_command *sym = &command->command.symtab;
@@ -1932,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))
 
   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.
 }
 
 /* Count the number of indirect symbols in the image.
@@ -1966,7 +2123,7 @@ bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
 
 /* Create the dysymtab.  */
 
 
 /* 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);
 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);
@@ -1980,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."));
     {
       _bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet"
                            " implemented for dysymtab commands."));
-      return FALSE;
+      return false;
     }
 
   cmd->ilocalsym = 0;
     }
 
   cmd->ilocalsym = 0;
@@ -2025,16 +2182,17 @@ bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd)
     {
       unsigned i;
       unsigned n;
     {
       unsigned i;
       unsigned n;
+      size_t amt;
 
       mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
       cmd->indirectsymoff = mdata->filelen;
 
       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)
       if (cmd->indirect_syms == NULL)
-        return FALSE;
+       return false;
 
       n = 0;
       for (i = 0; i < mdata->nsects; ++i)
 
       n = 0;
       for (i = 0; i < mdata->nsects; ++i)
@@ -2058,13 +2216,13 @@ bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd)
                  for (j = 0; j < num; j++, n++)
                    {
                      if (isyms[j] == NULL)
                  for (j = 0; j < num; j++, n++)
                    {
                      if (isyms[j] == NULL)
-                       cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
+                       cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
                      else if (isyms[j]->symbol.section == bfd_abs_section_ptr
                               && ! (isyms[j]->n_type & BFD_MACH_O_N_EXT))
                      else if (isyms[j]->symbol.section == bfd_abs_section_ptr
                               && ! (isyms[j]->n_type & BFD_MACH_O_N_EXT))
-                       cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL
+                       cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL
                                                 | BFD_MACH_O_INDIRECT_SYM_ABS;
                      else
                                                 | BFD_MACH_O_INDIRECT_SYM_ABS;
                      else
-                       cmd->indirect_syms[n] = isyms[j]->symbol.udata.i;
+                       cmd->indirect_syms[n] = isyms[j]->symbol.udata.i;
                    }
                }
                break;
                    }
                }
                break;
@@ -2074,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.  */
 
 }
 
 /* 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;
 bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
@@ -2092,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)
       unsigned int i;
 
       if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
-       return FALSE;
+       return false;
 
       for (i = 0; i < cmd->nmodtab; i++)
        {
 
       for (i = 0; i < cmd->nmodtab; i++)
        {
@@ -2127,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))
                            &w.objc_module_info_size);
 
              if (bfd_bwrite ((void *) &w, sizeof (w), abfd) != sizeof (w))
-               return FALSE;
+               return false;
            }
          else
            {
            }
          else
            {
@@ -2150,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))
                            &n.objc_module_info_size);
 
              if (bfd_bwrite ((void *) &n, sizeof (n), abfd) != sizeof (n))
-               return FALSE;
+               return false;
            }
        }
     }
            }
        }
     }
@@ -2160,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)
       unsigned int i;
 
       if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
-       return FALSE;
+       return false;
 
       for (i = 0; i < cmd->ntoc; i++)
        {
 
       for (i = 0; i < cmd->ntoc; i++)
        {
@@ -2171,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))
          bfd_h_put_32 (abfd, toc->module_index, &raw.module_index);
 
          if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
-           return FALSE;
+           return false;
        }
     }
 
        }
     }
 
@@ -2180,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)
       unsigned int i;
 
       if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
-       return FALSE;
+       return false;
 
       for (i = 0; i < cmd->nindirectsyms; ++i)
        {
 
       for (i = 0; i < cmd->nindirectsyms; ++i)
        {
@@ -2188,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))
 
          bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw);
          if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
-           return FALSE;
+           return false;
        }
     }
 
        }
     }
 
@@ -2197,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)
       unsigned int i;
 
       if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
-       return FALSE;
+       return false;
 
       for (i = 0; i < cmd->nextrefsyms; i++)
        {
 
       for (i = 0; i < cmd->nextrefsyms; i++)
        {
@@ -2221,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))
 
          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)
        }
     }
 
   /* 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;
   else
     {
       struct mach_o_dysymtab_command_external raw;
@@ -2252,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))
       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
 }
 
 static unsigned
@@ -2299,9 +2457,9 @@ bfd_mach_o_cf_symbols (const void *a, const void *b)
   if (soa == 0)
     {
       if (sa->symbol.udata.i < sb->symbol.udata.i)
   if (soa == 0)
     {
       if (sa->symbol.udata.i < sb->symbol.udata.i)
-        return -1;
+       return -1;
       if (sa->symbol.udata.i > sb->symbol.udata.i)
       if (sa->symbol.udata.i > sb->symbol.udata.i)
-        return  1;
+       return  1;
 
       /* This is probably an error.  */
       return 0;
 
       /* This is probably an error.  */
       return 0;
@@ -2330,14 +2488,14 @@ bfd_mach_o_cf_symbols (const void *a, const void *b)
                (by name)
 */
 
                (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)
 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++)
     {
 
   for (i = 0; i < bfd_get_symcount (abfd); i++)
     {
@@ -2349,38 +2507,41 @@ bfd_mach_o_mangle_symbols (bfd *abfd)
         this, since more meaningful diagnostics can be made that way.  */
 
       if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
         this, since more meaningful diagnostics can be made that way.  */
 
       if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
-        {
-          /* No symbol information has been set - therefore determine
-             it from the bfd symbol flags/info.  */
-          if (s->symbol.section == bfd_abs_section_ptr)
-            s->n_type = BFD_MACH_O_N_ABS;
-          else if (s->symbol.section == bfd_und_section_ptr)
-            {
-              s->n_type = BFD_MACH_O_N_UNDF;
-              if (s->symbol.flags & BSF_WEAK)
-                s->n_desc |= BFD_MACH_O_N_WEAK_REF;
-              /* mach-o automatically makes undefined symbols extern.  */
+       {
+         /* No symbol information has been set - therefore determine
+            it from the bfd symbol flags/info.  */
+         if (s->symbol.section == bfd_abs_section_ptr)
+           s->n_type = BFD_MACH_O_N_ABS;
+         else if (s->symbol.section == bfd_und_section_ptr)
+           {
+             s->n_type = BFD_MACH_O_N_UNDF;
+             if (s->symbol.flags & BSF_WEAK)
+               s->n_desc |= BFD_MACH_O_N_WEAK_REF;
+             /* mach-o automatically makes undefined symbols extern.  */
              s->n_type |= BFD_MACH_O_N_EXT;
              s->symbol.flags |= BSF_GLOBAL;
              s->n_type |= BFD_MACH_O_N_EXT;
              s->symbol.flags |= BSF_GLOBAL;
-            }
-          else if (s->symbol.section == bfd_com_section_ptr)
+           }
+         else if (s->symbol.section == bfd_com_section_ptr)
            {
            {
-              s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
-              s->symbol.flags |= BSF_GLOBAL;
-            }
-          else
-            s->n_type = BFD_MACH_O_N_SECT;
+             s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
+             s->symbol.flags |= BSF_GLOBAL;
+           }
+         else
+           s->n_type = BFD_MACH_O_N_SECT;
+       }
 
 
-          if (s->symbol.flags & BSF_GLOBAL)
-            s->n_type |= BFD_MACH_O_N_EXT;
-        }
+      /* Update external symbol bit in case objcopy changed it.  */
+      if (s->symbol.flags & BSF_GLOBAL)
+       s->n_type |= BFD_MACH_O_N_EXT;
+      else
+       s->n_type &= ~BFD_MACH_O_N_EXT;
 
       /* Put the section index in, where required.  */
       if ((s->symbol.section != bfd_abs_section_ptr
 
       /* Put the section index in, where required.  */
       if ((s->symbol.section != bfd_abs_section_ptr
-          && s->symbol.section != bfd_und_section_ptr
-          && s->symbol.section != bfd_com_section_ptr)
-          || ((s->n_type & BFD_MACH_O_N_STAB) != 0
-               && s->symbol.name == NULL))
+         && s->symbol.section != bfd_und_section_ptr
+         && s->symbol.section != bfd_com_section_ptr)
+         || ((s->n_type & BFD_MACH_O_N_STAB) != 0
+              && s->symbol.name == NULL))
        s->n_sect = s->symbol.section->output_section->target_index;
 
       /* Number to preserve order for local and debug syms.  */
        s->n_sect = s->symbol.section->output_section->target_index;
 
       /* Number to preserve order for local and debug syms.  */
@@ -2397,18 +2558,19 @@ bfd_mach_o_mangle_symbols (bfd *abfd)
       s->symbol.udata.i = i;  /* renumber.  */
     }
 
       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.  */
 
 }
 
 /* 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;
 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);
 
 
   nsect = bfd_count_sections (abfd);
 
@@ -2416,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))
      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);
 
   /* 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->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)
   if (mdata->sections == NULL)
-    return FALSE;
+    return false;
 
   /* Create Mach-O sections.
      Section type, attribute and align should have been set when the
 
   /* Create Mach-O sections.
      Section type, attribute and align should have been set when the
@@ -2438,13 +2600,13 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
   target_index = 0;
   for (sec = abfd->sections; sec; sec = sec->next)
     {
   target_index = 0;
   for (sec = abfd->sections; sec; sec = sec->next)
     {
-      unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
+      unsigned bfd_align = bfd_section_alignment (sec);
       bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
 
       mdata->sections[target_index] = msect;
 
       bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
 
       mdata->sections[target_index] = msect;
 
-      msect->addr = bfd_get_section_vma (abfd, sec);
-      msect->size = bfd_get_section_size (sec);
+      msect->addr = bfd_section_vma (sec);
+      msect->size = bfd_section_size (sec);
 
       /* Use the largest alignment set, in case it was bumped after the
         section was created.  */
 
       /* Use the largest alignment set, in case it was bumped after the
         section was created.  */
@@ -2454,10 +2616,10 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
       sec->target_index = ++target_index;
     }
 
       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);
 bfd_mach_o_write_contents (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
@@ -2468,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))
 
   /* 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))
 
   /* 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)
 
   /* First pass: allocate the linkedit segment.  */
   for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
@@ -2556,12 +2718,12 @@ bfd_mach_o_write_contents (bfd *abfd)
   /* Build the dysymtab.  */
   if (dysymtab != NULL)
     if (!bfd_mach_o_build_dysymtab (abfd, dysymtab))
   /* 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))
 
   /* 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)
 
   /* Adjust linkedit size.  */
   if (linkedit != NULL)
@@ -2589,62 +2751,62 @@ bfd_mach_o_write_contents (bfd *abfd)
       bfd_h_put_32 (abfd, cmd->len, raw.cmdsize);
 
       if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
       bfd_h_put_32 (abfd, cmd->len, raw.cmdsize);
 
       if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
-          || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
-       return FALSE;
+         || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
+       return false;
 
       switch (cmd->type)
        {
        case BFD_MACH_O_LC_SEGMENT:
          if (!bfd_mach_o_write_segment_32 (abfd, cmd))
 
       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))
          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))
          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))
          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))
          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))
          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))
          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))
          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))
          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);
          break;
        default:
          _bfd_error_handler
            (_("unable to write unknown load command %#x"),
             cmd->type);
-         return FALSE;
+         return false;
        }
     }
 
        }
     }
 
-  return TRUE;
+  return true;
 }
 
 static void
 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
 }
 
 static void
 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
-                                      bfd_mach_o_section *s)
+                                     bfd_mach_o_section *s)
 {
   if (seg->sect_head == NULL)
     seg->sect_head = s;
 {
   if (seg->sect_head == NULL)
     seg->sect_head = s;
@@ -2663,7 +2825,7 @@ bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
   bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
 
   /* Create default flags.  */
   bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
 
   /* Create default flags.  */
-  bfd_flags = bfd_get_section_flags (abfd, sec);
+  bfd_flags = bfd_section_flags (sec);
   if ((bfd_flags & SEC_CODE) == SEC_CODE)
     s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
       | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
   if ((bfd_flags & SEC_CODE) == SEC_CODE)
     s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
       | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
@@ -2676,7 +2838,7 @@ bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
     s->flags = BFD_MACH_O_S_REGULAR;
 }
 
     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);
 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);
@@ -2707,9 +2869,9 @@ bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
       /* Zerofill sections have zero file size & offset, the only content
         written to the file is the symbols.  */
       if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL
       /* Zerofill sections have zero file size & offset, the only content
         written to the file is the symbols.  */
       if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL
-          || ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
+         || ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
              == BFD_MACH_O_S_GB_ZEROFILL))
              == BFD_MACH_O_S_GB_ZEROFILL))
-        continue;
+       continue;
 
       /* The Darwin system tools (in MH_OBJECT files, at least) always account
         sections, even those with zero size.  */
 
       /* The Darwin system tools (in MH_OBJECT files, at least) always account
         sections, even those with zero size.  */
@@ -2722,13 +2884,13 @@ bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
          if (1)
            {
              seg->filesize = FILE_ALIGN (seg->filesize, s->align);
          if (1)
            {
              seg->filesize = FILE_ALIGN (seg->filesize, s->align);
-              mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
-            }
+             mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
+           }
          seg->filesize += s->size;
 
          /* The system tools write even zero-sized sections with an offset
             field set to the current file position.  */
          seg->filesize += s->size;
 
          /* The system tools write even zero-sized sections with an offset
             field set to the current file position.  */
-          s->offset = mdata->filelen;
+         s->offset = mdata->filelen;
        }
 
       sec->filepos = s->offset;
        }
 
       sec->filepos = s->offset;
@@ -2771,20 +2933,20 @@ bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
 
       ms->nreloc = sec->reloc_count;
       if (ms->nreloc == 0)
 
       ms->nreloc = sec->reloc_count;
       if (ms->nreloc == 0)
-        {
+       {
          /* Clear nreloc and reloff if there is no relocs.  */
          ms->reloff = 0;
          continue;
          /* Clear nreloc and reloff if there is no relocs.  */
          ms->reloff = 0;
          continue;
-        }
+       }
       sec->rel_filepos = mdata->filelen;
       ms->reloff = sec->rel_filepos;
       mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
     }
 
       sec->rel_filepos = mdata->filelen;
       ms->reloff = sec->rel_filepos;
       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);
 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);
@@ -2817,9 +2979,10 @@ bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
        {
          _bfd_error_handler
            /* xgettext:c-format */
        {
          _bfd_error_handler
            /* xgettext:c-format */
-           (_("section address (%#Lx) below start of segment (%#Lx)"),
-              s->addr, vma);
-         return FALSE;
+           (_("section address (%#" PRIx64 ") "
+              "below start of segment (%#" PRIx64 ")"),
+              (uint64_t) s->addr, (uint64_t) vma);
+         return false;
        }
 
       vma = s->addr + s->size;
        }
 
       vma = s->addr + s->size;
@@ -2837,7 +3000,7 @@ bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
   for (s = seg->sect_head; s != NULL; s = s->next)
     {
       asection *sec = s->bfdsection;
   for (s = seg->sect_head; s != NULL; s = s->next)
     {
       asection *sec = s->bfdsection;
-      flagword flags = bfd_get_section_flags (abfd, sec);
+      flagword flags = bfd_section_flags (sec);
 
       /* Adjust segment size.  */
       seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
 
       /* Adjust segment size.  */
       seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
@@ -2847,7 +3010,7 @@ bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
       seg->filesize = FILE_ALIGN (seg->filesize, s->align);
 
       if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_ZEROFILL
       seg->filesize = FILE_ALIGN (seg->filesize, s->align);
 
       if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_ZEROFILL
-          && ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
+         && ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
              != BFD_MACH_O_S_GB_ZEROFILL))
        {
          mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
              != BFD_MACH_O_S_GB_ZEROFILL))
        {
          mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
@@ -2875,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)
 
       /* Relocs shouldn't appear in non-object files.  */
       if (s->bfdsection->reloc_count != 0)
-       return FALSE;
+       return false;
     }
 
   /* Set maxprot.  */
     }
 
   /* Set maxprot.  */
@@ -2890,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;
 
   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.  */
 
 }
 
 /* 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);
 bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata)
 {
   unsigned wide = mach_o_wide_p (&mdata->header);
@@ -2904,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;
   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;
 
   hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
   align = wide ? 8 - 1 : 4 - 1;
@@ -2960,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);
          _bfd_error_handler
            (_("unable to layout unknown load command %#x"),
             cmd->type);
-         ret = FALSE;
+         ret = false;
          break;
        }
 
          break;
        }
 
@@ -2986,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;
 
   /* 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;
 
   strcpy (seg->segname, segname);
   seg->nsects = nbr_sect;
@@ -3007,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.  */
 
    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);
 bfd_mach_o_build_commands (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
@@ -3022,17 +3185,17 @@ bfd_mach_o_build_commands (bfd *abfd)
 
   /* Return now if already built.  */
   if (mdata->header.ncmds != 0)
 
   /* 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)
     {
       if (abfd->flags & EXEC_P)
 
   /* Fill in the file type, if not already set.  */
   if (mdata->header.filetype == 0)
     {
       if (abfd->flags & EXEC_P)
-        mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
+       mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
       else if (abfd->flags & DYNAMIC)
       else if (abfd->flags & DYNAMIC)
-        mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
+       mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
       else
       else
-        mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
+       mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
     }
 
   /* If hasn't already been done, flatten sections list, and sort
     }
 
   /* If hasn't already been done, flatten sections list, and sort
@@ -3040,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))
      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))
 
   /* 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)
 
   /* Segment commands.  */
   if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
@@ -3111,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)
   /* 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;
   for (i = 0; i < nbr_commands - 1; i++)
     commands[i].next = &commands[i + 1];
   commands[nbr_commands - 1].next = mdata->first_command;
@@ -3162,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;
       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
     }
 
   /* If required, setup symtab command, see comment above about the quality
@@ -3172,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;
       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.  */
     }
 
   /* Create the main command.  */
@@ -3181,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;
       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;
 
       cmd->command.main.entryoff = 0;
       cmd->command.main.stacksize = 0;
@@ -3189,7 +3352,7 @@ bfd_mach_o_build_commands (bfd *abfd)
 
   /* Layout commands.  */
   if (! bfd_mach_o_layout_commands (mdata))
 
   /* 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.  */
 
   /* 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.  */
@@ -3198,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))
       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
     {
     }
   else
     {
@@ -3210,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))
          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;
 
          if (seg->vmaddr + seg->vmsize > maxvma)
            maxvma = seg->vmaddr + seg->vmsize;
@@ -3230,12 +3393,12 @@ bfd_mach_o_build_commands (bfd *abfd)
          bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr;
     }
 
          bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr;
     }
 
-  return TRUE;
+  return true;
 }
 
 /* Set the contents of a section.  */
 
 }
 
 /* Set the contents of a section.  */
 
-bfd_boolean
+bool
 bfd_mach_o_set_section_contents (bfd *abfd,
                                 asection *section,
                                 const void * location,
 bfd_mach_o_set_section_contents (bfd *abfd,
                                 asection *section,
                                 const void * location,
@@ -3247,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))
   /* 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)
 
   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)
 
   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
 }
 
 int
@@ -3283,7 +3446,7 @@ bfd_mach_o_make_empty_symbol (bfd *abfd)
   return new_symbol;
 }
 
   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;
 bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header)
 {
   struct mach_o_header_external raw;
@@ -3293,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)
   /* 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)
     {
 
   if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
     {
@@ -3326,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;
   else
     {
       header->byteorder = BFD_ENDIAN_UNKNOWN;
-      return FALSE;
+      return false;
     }
 
   /* Once the size of the header is known, read the full header.  */
     }
 
   /* Once the size of the header is known, read the full header.  */
@@ -3335,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)
 
   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);
 
   header->cputype = (*get32) (raw.cputype);
   header->cpusubtype = (*get32) (raw.cpusubtype);
@@ -3349,14 +3512,14 @@ bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header)
   else
     header->reserved = 0;
 
   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;
 bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
 {
   bfd_mach_o_section *s;
-  unsigned bfdalign = bfd_get_section_alignment (abfd, sec);
+  unsigned bfdalign = bfd_section_alignment (sec);
 
   s = bfd_mach_o_get_mach_o_section (sec);
   if (s == NULL)
 
   s = bfd_mach_o_get_mach_o_section (sec);
   if (s == NULL)
@@ -3366,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)
 
       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;
 
       sec->used_by_bfd = s;
       s->bfdsection = sec;
 
@@ -3380,10 +3543,10 @@ bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
          s->flags = xlat->macho_sectype | xlat->macho_secattr;
          s->align = xlat->sectalign > bfdalign ? xlat->sectalign
                                                : bfdalign;
          s->flags = xlat->macho_sectype | xlat->macho_secattr;
          s->align = xlat->sectalign > bfdalign ? xlat->sectalign
                                                : bfdalign;
-         (void) bfd_set_section_alignment (abfd, sec, s->align);
-         bfd_flags = bfd_get_section_flags (abfd, sec);
+         bfd_set_section_alignment (sec, s->align);
+         bfd_flags = bfd_section_flags (sec);
          if (bfd_flags == SEC_NO_FLAGS)
          if (bfd_flags == SEC_NO_FLAGS)
-           bfd_set_section_flags (abfd, sec, xlat->bfd_flags);
+           bfd_set_section_flags (sec, xlat->bfd_flags);
        }
       else
        /* Create default flags.  */
        }
       else
        /* Create default flags.  */
@@ -3394,13 +3557,12 @@ bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
 }
 
 static void
 }
 
 static void
-bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
-                                     unsigned long prot)
+bfd_mach_o_init_section_from_mach_o (asection *sec, unsigned long prot)
 {
   flagword flags;
   bfd_mach_o_section *section;
 
 {
   flagword flags;
   bfd_mach_o_section *section;
 
-  flags = bfd_get_section_flags (abfd, sec);
+  flags = bfd_section_flags (sec);
   section = bfd_mach_o_get_mach_o_section (sec);
 
   /* TODO: see if we should use the xlat system for doing this by
   section = bfd_mach_o_get_mach_o_section (sec);
 
   /* TODO: see if we should use the xlat system for doing this by
@@ -3410,27 +3572,27 @@ bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
     {
       /* Try to guess flags.  */
       if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
     {
       /* Try to guess flags.  */
       if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
-        flags = SEC_DEBUGGING;
+       flags = SEC_DEBUGGING;
       else
       else
-        {
-          flags = SEC_ALLOC;
-          if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
-              != BFD_MACH_O_S_ZEROFILL)
-            {
-              flags |= SEC_LOAD;
-              if (prot & BFD_MACH_O_PROT_EXECUTE)
-                flags |= SEC_CODE;
-              if (prot & BFD_MACH_O_PROT_WRITE)
-                flags |= SEC_DATA;
-              else if (prot & BFD_MACH_O_PROT_READ)
-                flags |= SEC_READONLY;
-            }
-        }
+       {
+         flags = SEC_ALLOC;
+         if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
+             != BFD_MACH_O_S_ZEROFILL)
+           {
+             flags |= SEC_LOAD;
+             if (prot & BFD_MACH_O_PROT_EXECUTE)
+               flags |= SEC_CODE;
+             if (prot & BFD_MACH_O_PROT_WRITE)
+               flags |= SEC_DATA;
+             else if (prot & BFD_MACH_O_PROT_READ)
+               flags |= SEC_READONLY;
+           }
+       }
     }
   else
     {
       if ((flags & SEC_DEBUGGING) == 0)
     }
   else
     {
       if ((flags & SEC_DEBUGGING) == 0)
-        flags |= SEC_ALLOC;
+       flags |= SEC_ALLOC;
     }
 
   if (section->offset != 0)
     }
 
   if (section->offset != 0)
@@ -3438,7 +3600,7 @@ bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
   if (section->nreloc != 0)
     flags |= SEC_RELOC;
 
   if (section->nreloc != 0)
     flags |= SEC_RELOC;
 
-  bfd_set_section_flags (abfd, sec, flags);
+  bfd_set_section_flags (sec, flags);
 
   sec->vma = section->addr;
   sec->lma = section->addr;
 
   sec->vma = section->addr;
   sec->lma = section->addr;
@@ -3452,8 +3614,8 @@ bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
 
 static asection *
 bfd_mach_o_make_bfd_section (bfd *abfd,
 
 static asection *
 bfd_mach_o_make_bfd_section (bfd *abfd,
-                             const unsigned char *segname,
-                             const unsigned char *sectname)
+                            const unsigned char *segname,
+                            const unsigned char *sectname)
 {
   const char *sname;
   flagword flags;
 {
   const char *sname;
   flagword flags;
@@ -3505,7 +3667,7 @@ bfd_mach_o_read_section_32 (bfd *abfd, unsigned long prot)
   section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
   section->reserved3 = 0;
 
   section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
   section->reserved3 = 0;
 
-  bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
+  bfd_mach_o_init_section_from_mach_o (sec, prot);
 
   return sec;
 }
 
   return sec;
 }
@@ -3548,7 +3710,7 @@ bfd_mach_o_read_section_64 (bfd *abfd, unsigned long prot)
   section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
   section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
 
   section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
   section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
 
-  bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
+  bfd_mach_o_init_section_from_mach_o (sec, prot);
 
   return sec;
 }
 
   return sec;
 }
@@ -3562,11 +3724,11 @@ bfd_mach_o_read_section (bfd *abfd, unsigned long prot, unsigned int wide)
     return bfd_mach_o_read_section_32 (abfd, prot);
 }
 
     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_read_symtab_symbol (bfd *abfd,
-                               bfd_mach_o_symtab_command *sym,
-                               bfd_mach_o_asymbol *s,
-                               unsigned long i)
+                              bfd_mach_o_symtab_command *sym,
+                              bfd_mach_o_asymbol *s,
+                              unsigned long i)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   unsigned int wide = mach_o_wide_p (&mdata->header);
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   unsigned int wide = mach_o_wide_p (&mdata->header);
@@ -3588,9 +3750,9 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd,
     {
       _bfd_error_handler
        /* xgettext:c-format */
     {
       _bfd_error_handler
        /* xgettext:c-format */
-        (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"),
-         symwidth, symoff);
-      return FALSE;
+       (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"),
+        symwidth, symoff);
+      return false;
     }
 
   stroff = bfd_h_get_32 (abfd, raw.n_strx);
     }
 
   stroff = bfd_h_get_32 (abfd, raw.n_strx);
@@ -3607,10 +3769,10 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd,
     {
       _bfd_error_handler
        /* xgettext:c-format */
     {
       _bfd_error_handler
        /* xgettext:c-format */
-        (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"),
-         stroff,
-         sym->strsize);
-      return FALSE;
+       (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"),
+        stroff,
+        sym->strsize);
+      return false;
     }
 
   s->symbol.the_bfd = abfd;
     }
 
   s->symbol.the_bfd = abfd;
@@ -3641,7 +3803,7 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd,
            {
              s->symbol.section = mdata->sections[section - 1]->bfdsection;
              s->symbol.value =
            {
              s->symbol.section = mdata->sections[section - 1]->bfdsection;
              s->symbol.value =
-                s->symbol.value - mdata->sections[section - 1]->addr;
+               s->symbol.value - mdata->sections[section - 1]->addr;
            }
          break;
        }
            }
          break;
        }
@@ -3656,19 +3818,19 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd,
       switch (symtype)
        {
        case BFD_MACH_O_N_UNDF:
       switch (symtype)
        {
        case BFD_MACH_O_N_UNDF:
-          if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
-              && s->symbol.value != 0)
-            {
-              /* A common symbol.  */
-              s->symbol.section = bfd_com_section_ptr;
-              s->symbol.flags = BSF_NO_FLAGS;
-            }
-          else
-            {
-              s->symbol.section = bfd_und_section_ptr;
-              if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
-                s->symbol.flags |= BSF_WEAK;
-            }
+         if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
+             && s->symbol.value != 0)
+           {
+             /* A common symbol.  */
+             s->symbol.section = bfd_com_section_ptr;
+             s->symbol.flags = BSF_NO_FLAGS;
+           }
+         else
+           {
+             s->symbol.section = bfd_und_section_ptr;
+             if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
+               s->symbol.flags |= BSF_WEAK;
+           }
          break;
        case BFD_MACH_O_N_PBUD:
          s->symbol.section = bfd_und_section_ptr;
          break;
        case BFD_MACH_O_N_PBUD:
          s->symbol.section = bfd_und_section_ptr;
@@ -3681,7 +3843,7 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd,
            {
              s->symbol.section = mdata->sections[section - 1]->bfdsection;
              s->symbol.value =
            {
              s->symbol.section = mdata->sections[section - 1]->bfdsection;
              s->symbol.value =
-                s->symbol.value - mdata->sections[section - 1]->addr;
+               s->symbol.value - mdata->sections[section - 1]->addr;
            }
          else
            {
            }
          else
            {
@@ -3717,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);
 bfd_mach_o_read_symtab_strtab (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
@@ -3728,11 +3890,11 @@ bfd_mach_o_read_symtab_strtab (bfd *abfd)
 
   /* Fail if there is no symtab.  */
   if (sym == NULL)
 
   /* Fail if there is no symtab.  */
   if (sym == NULL)
-    return FALSE;
+    return false;
 
   /* Success if already loaded.  */
   if (sym->strtab)
 
   /* Success if already loaded.  */
   if (sym->strtab)
-    return TRUE;
+    return true;
 
   if (abfd->flags & BFD_IN_MEMORY)
     {
 
   if (abfd->flags & BFD_IN_MEMORY)
     {
@@ -3743,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);
       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;
     }
        }
       sym->strtab = (char *) b->buffer + sym->stroff;
     }
@@ -3751,45 +3913,55 @@ bfd_mach_o_read_symtab_strtab (bfd *abfd)
     {
       /* See PR 21840 for a reproducer.  */
       if ((sym->strsize + 1) == 0)
     {
       /* 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)
       if (sym->strtab == NULL)
-        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;
-        }
+       return false;
+
       /* Zero terminate the string table.  */
       sym->strtab[sym->strsize] = 0;
     }
 
       /* 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;
 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 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)
     {
     {
-      _bfd_error_handler (_("bfd_mach_o_read_symtab_symbols: "
-                           "unable to allocate memory for symbols"));
+      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_set_error (bfd_error_no_memory);
       sym->nsyms = 0;
       sym->nsyms = 0;
-      return FALSE;
+      return false;
     }
 
   if (!bfd_mach_o_read_symtab_strtab (abfd))
     }
 
   if (!bfd_mach_o_read_symtab_strtab (abfd))
@@ -3799,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;
 
     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;
 
  fail:
   bfd_release (abfd, sym->symbols);
   sym->symbols = NULL;
   sym->nsyms = 0;
-  return FALSE;
+  return false;
 }
 
 static const char *
 }
 
 static const char *
@@ -3845,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;
 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
@@ -3853,24 +4033,23 @@ bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
   unsigned int nameoff;
   unsigned int namelen;
 
   unsigned int nameoff;
   unsigned int namelen;
 
+  if (command->len < sizeof (raw) + 8)
+    return false;
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return FALSE;
+    return false;
 
   nameoff = bfd_h_get_32 (abfd, raw.str);
 
   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_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);
 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);
@@ -3878,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;
   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:
   switch (command->type)
     {
     case BFD_MACH_O_LC_LOAD_DYLIB:
@@ -3890,31 +4072,29 @@ bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
       break;
     default:
       BFD_FAIL ();
       break;
     default:
       BFD_FAIL ();
-      return FALSE;
+      return false;
     }
 
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
     }
 
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return FALSE;
+    return false;
 
   nameoff = bfd_h_get_32 (abfd, raw.name);
 
   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->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_read_prebound_dylib (bfd *abfd,
-                                bfd_mach_o_load_command *command)
+                               bfd_mach_o_load_command *command)
 {
   bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib;
   struct mach_o_prebound_dylib_command_external raw;
 {
   bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib;
   struct mach_o_prebound_dylib_command_external raw;
@@ -3923,20 +4103,20 @@ bfd_mach_o_read_prebound_dylib (bfd *abfd,
   unsigned int str_len;
   unsigned char *str;
 
   unsigned int str_len;
   unsigned char *str;
 
+  if (command->len < sizeof (raw) + 8)
+    return false;
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
   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)
 
   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_len = command->len - sizeof (raw);
-  str = bfd_alloc (abfd, str_len);
+  str = _bfd_alloc_and_read (abfd, str_len, str_len);
   if (str == NULL)
   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);
 
   cmd->name_offset = command->offset + nameoff;
   cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules);
@@ -3944,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);
 
   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;
 
 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))
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return FALSE;
+    return false;
 
   cmd->cksum = bfd_get_32 (abfd, raw.cksum);
 
   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;
 
 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))
   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);
 
   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;
 bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib;
@@ -3984,25 +4168,25 @@ bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command)
   unsigned int nameoff;
   unsigned int namelen;
 
   unsigned int nameoff;
   unsigned int namelen;
 
+  if (command->len < sizeof (raw) + 8)
+    return false;
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return FALSE;
+    return false;
 
   nameoff = bfd_h_get_32 (abfd, raw.name);
 
   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->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);
 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);
@@ -4010,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;
   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));
 
   BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
              || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
@@ -4017,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;
   /* 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
 
       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
-          || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-       return FALSE;
-
-      offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
+         || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+       return false;
+
+      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++;
     }
       nflavours++;
     }
+  if (nflavours == 0 || offset != command->len)
+    return false;
 
   /* Allocate threads.  */
 
   /* 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)
   if (cmd->flavours == NULL)
-    return FALSE;
+    return false;
   cmd->nflavours = nflavours;
 
   offset = 8;
   nflavours = 0;
   while (offset != command->len)
     {
   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
       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
-          || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-       return FALSE;
+         || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+       return false;
 
       cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
       cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
 
       cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
       cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
@@ -4091,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)
       snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
       sname = bfd_alloc (abfd, snamelen);
       if (sname == NULL)
-       return FALSE;
+       return false;
 
       for (;;)
        {
 
       for (;;)
        {
@@ -4112,11 +4297,12 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
       cmd->section = bfdsec;
     }
 
       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);
 {
   bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
@@ -4126,8 +4312,10 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
   {
     struct mach_o_dysymtab_command_external raw;
 
   {
     struct mach_o_dysymtab_command_external raw;
 
+    if (command->len < sizeof (raw) + 8)
+      return false;
     if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
     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);
 
     cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
     cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
@@ -4154,147 +4342,195 @@ 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;
       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)
       if (cmd->dylib_module == NULL)
-        return FALSE;
+       return false;
 
       if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
 
       if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
-        return FALSE;
+       return false;
 
       for (i = 0; i < cmd->nmodtab; i++)
 
       for (i = 0; i < cmd->nmodtab; i++)
-        {
-          bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
-          unsigned long v;
-          unsigned char buf[56];
-
-          if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
-            return FALSE;
-
-          module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
-          module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
-          module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
-          module->irefsym = bfd_h_get_32 (abfd, buf + 12);
-          module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
-          module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
-          module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
-          module->iextrel = bfd_h_get_32 (abfd, buf + 28);
-          module->nextrel = bfd_h_get_32 (abfd, buf + 32);
-          v = bfd_h_get_32 (abfd, buf +36);
-          module->iinit = v & 0xffff;
-          module->iterm = (v >> 16) & 0xffff;
-          v = bfd_h_get_32 (abfd, buf + 40);
-          module->ninit = v & 0xffff;
-          module->nterm = (v >> 16) & 0xffff;
-          if (wide)
-            {
-              module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
-              module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
-            }
-          else
-            {
-              module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
-              module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
-            }
-        }
+       {
+         bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
+         unsigned long v;
+         unsigned char buf[56];
+
+         if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
+           return false;
+
+         module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
+         module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
+         module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
+         module->irefsym = bfd_h_get_32 (abfd, buf + 12);
+         module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
+         module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
+         module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
+         module->iextrel = bfd_h_get_32 (abfd, buf + 28);
+         module->nextrel = bfd_h_get_32 (abfd, buf + 32);
+         v = bfd_h_get_32 (abfd, buf +36);
+         module->iinit = v & 0xffff;
+         module->iterm = (v >> 16) & 0xffff;
+         v = bfd_h_get_32 (abfd, buf + 40);
+         module->ninit = v & 0xffff;
+         module->nterm = (v >> 16) & 0xffff;
+         if (wide)
+           {
+             module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
+             module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
+           }
+         else
+           {
+             module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
+             module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
+           }
+       }
     }
 
   if (cmd->ntoc != 0)
     {
       unsigned long i;
     }
 
   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)
       if (cmd->dylib_toc == NULL)
-        return FALSE;
+       return false;
 
       if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
 
       if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
-        return FALSE;
+       return false;
 
       for (i = 0; i < cmd->ntoc; i++)
 
       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];
+       {
+         bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
 
 
-          if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-            return FALSE;
+         if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+           return false;
 
 
-          toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
-          toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
-        }
+         toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
+         toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
+       }
     }
 
   if (cmd->nindirectsyms != 0)
     {
       unsigned int i;
     }
 
   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)
       if (cmd->indirect_syms == NULL)
-        return FALSE;
+       return false;
 
       if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
 
       if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
-        return FALSE;
+       return false;
 
       for (i = 0; i < cmd->nindirectsyms; i++)
 
       for (i = 0; i < cmd->nindirectsyms; i++)
-        {
-          unsigned char raw[4];
-          unsigned int *is = &cmd->indirect_syms[i];
+       {
+         unsigned char raw[4];
+         unsigned int *is = &cmd->indirect_syms[i];
 
 
-          if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
-            return FALSE;
+         if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
+           return false;
 
 
-          *is = bfd_h_get_32 (abfd, raw);
-        }
+         *is = bfd_h_get_32 (abfd, raw);
+       }
     }
 
   if (cmd->nextrefsyms != 0)
     {
       unsigned long v;
       unsigned int i;
     }
 
   if (cmd->nextrefsyms != 0)
     {
       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)
       if (cmd->ext_refs == NULL)
-        return FALSE;
+       return false;
 
       if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
 
       if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
-        return FALSE;
+       return false;
 
       for (i = 0; i < cmd->nextrefsyms; i++)
 
       for (i = 0; i < cmd->nextrefsyms; i++)
-        {
-          unsigned char raw[4];
-          bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
-
-          if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
-            return FALSE;
-
-          /* Fields isym and flags are written as bit-fields, thus we need
-             a specific processing for endianness.  */
-          v = bfd_h_get_32 (abfd, raw);
-          if (bfd_big_endian (abfd))
-            {
-              ref->isym = (v >> 8) & 0xffffff;
-              ref->flags = v & 0xff;
-            }
-          else
-            {
-              ref->isym = v & 0xffffff;
-              ref->flags = (v >> 24) & 0xff;
-            }
-        }
+       {
+         unsigned char raw[4];
+         bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
+
+         if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
+           return false;
+
+         /* Fields isym and flags are written as bit-fields, thus we need
+            a specific processing for endianness.  */
+         v = bfd_h_get_32 (abfd, raw);
+         if (bfd_big_endian (abfd))
+           {
+             ref->isym = (v >> 8) & 0xffffff;
+             ref->flags = v & 0xff;
+           }
+         else
+           {
+             ref->isym = v & 0xffffff;
+             ref->flags = (v >> 24) & 0xff;
+           }
+       }
     }
 
   if (mdata->dysymtab)
     }
 
   if (mdata->dysymtab)
-    return FALSE;
+    return false;
   mdata->dysymtab = cmd;
 
   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);
 {
   bfd_mach_o_symtab_command *symtab = &command->command.symtab;
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
@@ -4302,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);
 
 
   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))
   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);
 
   symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
   symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
@@ -4312,97 +4550,97 @@ bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
   symtab->symbols = NULL;
   symtab->strtab = NULL;
 
   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)
   if (symtab->nsyms != 0)
     abfd->flags |= HAS_SYMS;
 
   if (mdata->symtab)
-    return FALSE;
+    return false;
   mdata->symtab = symtab;
   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);
 
 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)
   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;
 
 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))
   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);
 
   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;
 
 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))
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return FALSE;
+    return false;
 
   off = bfd_get_32 (abfd, raw.str);
 
   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->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)
     {
 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)
       if (cmd->rebase_content == NULL)
-       return FALSE;
+       return false;
     }
 
   /* Read bind content.  */
   if (cmd->bind_content == NULL && cmd->bind_size != 0)
     {
     }
 
   /* 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)
       if (cmd->bind_content == NULL)
-       return FALSE;
+       return false;
     }
 
   /* Read weak bind content.  */
     }
 
   /* Read weak bind content.  */
@@ -4411,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)
       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.  */
     }
 
   /* Read lazy bind content.  */
@@ -4420,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)
       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.  */
     }
 
   /* Read export content.  */
@@ -4429,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)
       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;
 
 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))
   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);
 
   cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
   cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
@@ -4459,80 +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;
   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;
 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;
-  unsigned int ver;
 
 
+  if (command->len < sizeof (raw) + 8)
+    return false;
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
   if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return FALSE;
+    return false;
 
 
-  ver = bfd_get_32 (abfd, raw.version);
-  cmd->rel = ver >> 16;
-  cmd->maj = ver >> 8;
-  cmd->min = ver;
-  cmd->reserved = bfd_get_32 (abfd, raw.reserved);
-  return TRUE;
+  cmd->version = bfd_get_32 (abfd, raw.version);
+  cmd->sdk = bfd_get_32 (abfd, raw.sdk);
+  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;
 
 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))
   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);
 
   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;
 
 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))
   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);
 
   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;
 
 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))
   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);
 
   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;
 
 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))
   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
 
   ver = bfd_get_64 (abfd, raw.version);
   /* Note: we use a serie of shift to avoid shift > 32 (for which gcc
@@ -4547,13 +4793,48 @@ bfd_mach_o_read_source_version (bfd *abfd, bfd_mach_o_load_command *command)
   cmd->b = ver & 0x3ff;
   ver >>= 10;
   cmd->a = ver & 0xffffff;
   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;
+
+  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;
+}
+
+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;
+
+  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;
+}
+
+static bool
 bfd_mach_o_read_segment (bfd *abfd,
 bfd_mach_o_read_segment (bfd *abfd,
-                         bfd_mach_o_load_command *command,
-                         unsigned int wide)
+                        bfd_mach_o_load_command *command,
+                        unsigned int wide)
 {
   bfd_mach_o_segment_command *seg = &command->command.segment;
   unsigned long i;
 {
   bfd_mach_o_segment_command *seg = &command->command.segment;
   unsigned long i;
@@ -4564,8 +4845,10 @@ bfd_mach_o_read_segment (bfd *abfd,
 
       BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
 
 
       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))
       if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-        return FALSE;
+       return false;
 
       memcpy (seg->segname, raw.segname, 16);
       seg->segname[16] = '\0';
 
       memcpy (seg->segname, raw.segname, 16);
       seg->segname[16] = '\0';
@@ -4585,8 +4868,10 @@ bfd_mach_o_read_segment (bfd *abfd,
 
       BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
 
 
       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))
       if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-        return FALSE;
+       return false;
 
       memcpy (seg->segname, raw.segname, 16);
       seg->segname[16] = '\0';
 
       memcpy (seg->segname, raw.segname, 16);
       seg->segname[16] = '\0';
@@ -4609,29 +4894,30 @@ bfd_mach_o_read_segment (bfd *abfd,
 
       sec = bfd_mach_o_read_section (abfd, seg->initprot, wide);
       if (sec == NULL)
 
       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));
     }
 
 
       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);
 }
 
 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);
 }
 
 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;
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   struct mach_o_load_command_external raw;
@@ -4640,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)
   /* 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);
 
   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);
   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))
 
   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))
       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:
       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))
       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))
       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:
       break;
     case BFD_MACH_O_LC_LOAD_DYLIB:
     case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
@@ -4681,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))
     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))
       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))
       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:
       break;
     case BFD_MACH_O_LC_IDENT:
     case BFD_MACH_O_LC_FVMFILE:
@@ -4704,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))
     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:
       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))
       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))
       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))
       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:
       break;
     case BFD_MACH_O_LC_CODE_SIGNATURE:
     case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
@@ -4728,51 +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_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))
       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))
       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))
       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))
       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:
       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))
       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))
       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))
       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;
+      break;
+    case BFD_MACH_O_LC_BUILD_VERSION:
+      if (!bfd_mach_o_read_build_version (abfd, command))
+       return false;
       break;
     default:
       command->len = 0;
       break;
     default:
       command->len = 0;
-      _bfd_error_handler (_("%B: unknown load command %#x"),
+      _bfd_error_handler (_("%pB: unknown load command %#x"),
                          abfd, command->type);
                          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;
 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;
 
   /* Count total number of sections.  */
   mdata->nsects = 0;
@@ -4789,8 +5091,14 @@ bfd_mach_o_flatten_sections (bfd *abfd)
     }
 
   /* Allocate sections array.  */
     }
 
   /* 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;
 
   /* Fill the array.  */
   csect = 0;
@@ -4801,17 +5109,18 @@ bfd_mach_o_flatten_sections (bfd *abfd)
          || cmd->type == BFD_MACH_O_LC_SEGMENT_64)
        {
          bfd_mach_o_segment_command *seg = &cmd->command.segment;
          || cmd->type == BFD_MACH_O_LC_SEGMENT_64)
        {
          bfd_mach_o_segment_command *seg = &cmd->command.segment;
-          bfd_mach_o_section *sec;
+         bfd_mach_o_section *sec;
 
          BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
 
 
          BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
 
-          for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+         for (sec = seg->sect_head; sec != NULL; sec = sec->next)
            mdata->sections[csect++] = sec;
        }
     }
            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);
 bfd_mach_o_scan_start_address (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
@@ -4823,8 +5132,8 @@ bfd_mach_o_scan_start_address (bfd *abfd)
     if (cmd->type == BFD_MACH_O_LC_THREAD
        || cmd->type == BFD_MACH_O_LC_UNIXTHREAD)
       {
     if (cmd->type == BFD_MACH_O_LC_THREAD
        || cmd->type == BFD_MACH_O_LC_UNIXTHREAD)
       {
-        thr = &cmd->command.thread;
-        break;
+       thr = &cmd->command.thread;
+       break;
       }
     else if (cmd->type == BFD_MACH_O_LC_MAIN && mdata->nsects > 1)
       {
       }
     else if (cmd->type == BFD_MACH_O_LC_MAIN && mdata->nsects > 1)
       {
@@ -4835,13 +5144,13 @@ bfd_mach_o_scan_start_address (bfd *abfd)
          {
            abfd->start_address = main_cmd->entryoff
              + (text_sect->addr - text_sect->offset);
          {
            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)
          }
       }
 
   /* 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++)
 
   /* FIXME: create a subtarget hook ?  */
   for (i = 0; i < thr->nflavours; i++)
@@ -4852,8 +5161,8 @@ bfd_mach_o_scan_start_address (bfd *abfd)
          unsigned char buf[4];
 
          if (bfd_seek (abfd, thr->flavours[i].offset + 40, SEEK_SET) != 0
          unsigned char buf[4];
 
          if (bfd_seek (abfd, thr->flavours[i].offset + 40, SEEK_SET) != 0
-              || bfd_bread (buf, 4, abfd) != 4)
-           return FALSE;
+             || bfd_bread (buf, 4, abfd) != 4)
+           return false;
 
          abfd->start_address = bfd_h_get_32 (abfd, buf);
        }
 
          abfd->start_address = bfd_h_get_32 (abfd, buf);
        }
@@ -4863,42 +5172,42 @@ bfd_mach_o_scan_start_address (bfd *abfd)
          unsigned char buf[4];
 
          if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
          unsigned char buf[4];
 
          if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
-              || bfd_bread (buf, 4, abfd) != 4)
-           return FALSE;
+             || bfd_bread (buf, 4, abfd) != 4)
+           return false;
 
          abfd->start_address = bfd_h_get_32 (abfd, buf);
        }
       else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
 
          abfd->start_address = bfd_h_get_32 (abfd, buf);
        }
       else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
-               && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
-        {
-          unsigned char buf[8];
+              && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
+       {
+         unsigned char buf[8];
 
 
-          if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
-              || bfd_bread (buf, 8, abfd) != 8)
-            return FALSE;
+         if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
+             || bfd_bread (buf, 8, abfd) != 8)
+           return false;
 
 
-          abfd->start_address = bfd_h_get_64 (abfd, buf);
-        }
+         abfd->start_address = bfd_h_get_64 (abfd, buf);
+       }
       else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
       else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
-               && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
-        {
-          unsigned char buf[8];
+              && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
+       {
+         unsigned char buf[8];
 
 
-          if (bfd_seek (abfd, thr->flavours[i].offset + (16 * 8), SEEK_SET) != 0
-              || bfd_bread (buf, 8, abfd) != 8)
-            return FALSE;
+         if (bfd_seek (abfd, thr->flavours[i].offset + (16 * 8), SEEK_SET) != 0
+             || bfd_bread (buf, 8, abfd) != 8)
+           return false;
 
 
-          abfd->start_address = bfd_h_get_64 (abfd, buf);
-        }
+         abfd->start_address = bfd_h_get_64 (abfd, buf);
+       }
     }
 
     }
 
-  return TRUE;
+  return true;
 }
 
 }
 
-bfd_boolean
+bool
 bfd_mach_o_set_arch_mach (bfd *abfd,
 bfd_mach_o_set_arch_mach (bfd *abfd,
-                          enum bfd_architecture arch,
-                          unsigned long machine)
+                         enum bfd_architecture arch,
+                         unsigned long machine)
 {
   bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
 
 {
   bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
 
@@ -4907,19 +5216,19 @@ bfd_mach_o_set_arch_mach (bfd *abfd,
   if (arch != bed->arch
       && arch != bfd_arch_unknown
       && bed->arch != bfd_arch_unknown)
   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);
 }
 
 
   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;
 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) ?
   unsigned int hdrsize;
 
   hdrsize = mach_o_wide_p (header) ?
@@ -4945,28 +5254,44 @@ bfd_mach_o_scan (bfd *abfd,
   abfd->tdata.mach_o_data = mdata;
 
   bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
   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_error_handler
        /* xgettext:c-format */
-        (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
-         header->cputype, header->cpusubtype);
-      return FALSE;
+       (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
+        header->cputype, header->cpusubtype);
+      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;
 
   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;
 
 
       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)
       if (cmd == NULL)
-       return FALSE;
+       return false;
 
       for (i = 0; i < header->ncmds; i++)
        {
 
       for (i = 0; i < header->ncmds; i++)
        {
@@ -4982,27 +5307,28 @@ bfd_mach_o_scan (bfd *abfd,
              cur->offset = prev->offset + prev->len;
            }
 
              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.  */
        }
     }
 
   /* 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))
   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)
 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;
   abfd->tdata.mach_o_data = mdata;
 
   mdata->header.magic = 0;
@@ -5019,16 +5345,16 @@ bfd_mach_o_mkobject_init (bfd *abfd)
   mdata->sections = NULL;
   mdata->dyn_reloc_cache = NULL;
 
   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))
 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;
 
   mdata = bfd_mach_o_get_data (abfd);
   mdata->header.magic = BFD_MACH_O_MH_MAGIC;
@@ -5037,14 +5363,14 @@ bfd_mach_o_gen_mkobject (bfd *abfd)
   mdata->header.byteorder = abfd->xvec->byteorder;
   mdata->header.version = 1;
 
   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_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;
 {
   bfd_mach_o_header header;
   bfd_mach_o_data_struct *mdata;
@@ -5071,10 +5397,10 @@ 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.  */
   /* 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)
-        goto wrong;
+      if (header.cputype != cpu_type)
+       goto wrong;
     }
   else
     {
     }
   else
     {
@@ -5086,21 +5412,21 @@ bfd_mach_o_header_p (bfd *abfd,
 #endif
     }
 
 #endif
     }
 
-  if (filetype)
+  if (file_type)
     {
     {
-      if (header.filetype != filetype)
-        goto wrong;
+      if (header.filetype != file_type)
+       goto wrong;
     }
   else
     {
       switch (header.filetype)
     }
   else
     {
       switch (header.filetype)
-        {
-        case BFD_MACH_O_MH_CORE:
-          /* Handled by core_p */
-          goto wrong;
-        default:
-          break;
-        }
+       {
+       case BFD_MACH_O_MH_CORE:
+         /* Handled by core_p */
+         goto wrong;
+       default:
+         break;
+       }
     }
 
   mdata = (bfd_mach_o_data_struct *) bfd_zalloc (abfd, sizeof (*mdata));
     }
 
   mdata = (bfd_mach_o_data_struct *) bfd_zalloc (abfd, sizeof (*mdata));
@@ -5111,7 +5437,7 @@ bfd_mach_o_header_p (bfd *abfd,
   if (!bfd_mach_o_scan (abfd, &header, mdata))
     goto wrong;
 
   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);
 
  wrong:
   bfd_set_error (bfd_error_wrong_format);
@@ -5120,13 +5446,13 @@ bfd_mach_o_header_p (bfd *abfd,
   return NULL;
 }
 
   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);
 }
 
 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);
 bfd_mach_o_gen_core_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_MH_CORE, 0);
@@ -5176,12 +5502,13 @@ typedef struct mach_o_fat_data_struct
   mach_o_fat_archentry *archentries;
 } 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;
 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;
 
   if (bfd_seek (abfd, 0, SEEK_SET) != 0
       || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
 
   if (bfd_seek (abfd, 0, SEEK_SET) != 0
       || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
@@ -5201,8 +5528,13 @@ bfd_mach_o_fat_archive_p (bfd *abfd)
   if (adata->nfat_arch > 30)
     goto error;
 
   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;
 
   if (adata->archentries == NULL)
     goto error;
 
@@ -5220,7 +5552,7 @@ bfd_mach_o_fat_archive_p (bfd *abfd)
 
   abfd->tdata.mach_o_fat_data = adata;
 
 
   abfd->tdata.mach_o_fat_data = adata;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  error:
   if (adata != NULL)
 
  error:
   if (adata != NULL)
@@ -5233,36 +5565,41 @@ 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.  */
 
    ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY.
    Set arelt_data and origin fields too.  */
 
-static void
+static bool
 bfd_mach_o_fat_member_init (bfd *abfd,
 bfd_mach_o_fat_member_init (bfd *abfd,
-                            enum bfd_architecture arch_type,
-                            unsigned long arch_subtype,
-                            mach_o_fat_archentry *entry)
+                           enum bfd_architecture arch_type,
+                           unsigned long arch_subtype,
+                           mach_o_fat_archentry *entry)
 {
   struct areltdata *areltdata;
   /* Create the member filename. Use ARCH_NAME.  */
   const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype);
 {
   struct areltdata *areltdata;
   /* Create the member filename. Use ARCH_NAME.  */
   const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype);
+  const char *filename;
 
   if (ap)
     {
       /* Use the architecture name if known.  */
 
   if (ap)
     {
       /* Use the architecture name if known.  */
-      abfd->filename = xstrdup (ap->printable_name);
+      filename = bfd_set_filename (abfd, ap->printable_name);
     }
   else
     {
       /* Forge a uniq id.  */
     }
   else
     {
       /* Forge a uniq id.  */
-      const size_t namelen = 2 + 8 + 1 + 2 + 8 + 1;
-      char *name = xmalloc (namelen);
-      snprintf (name, namelen, "0x%lx-0x%lx",
-                entry->cputype, entry->cpusubtype);
-      abfd->filename = name;
+      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);
     }
     }
+  if (!filename)
+    return false;
 
   areltdata = bfd_zmalloc (sizeof (struct areltdata));
 
   areltdata = bfd_zmalloc (sizeof (struct areltdata));
+  if (areltdata == NULL)
+    return false;
   areltdata->parsed_size = entry->size;
   abfd->arelt_data = areltdata;
   abfd->iostream = NULL;
   abfd->origin = entry->offset;
   areltdata->parsed_size = entry->size;
   abfd->arelt_data = areltdata;
   abfd->iostream = NULL;
   abfd->origin = entry->offset;
+  return true;
 }
 
 bfd *
 }
 
 bfd *
@@ -5318,7 +5655,11 @@ bfd_mach_o_fat_openr_next_archived_file (bfd *archive, bfd *prev)
   bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
                                   &arch_type, &arch_subtype);
 
   bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
                                   &arch_type, &arch_subtype);
 
-  bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry);
+  if (!bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry))
+    {
+      bfd_close (nbfd);
+      return NULL;
+    }
 
   bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
 
 
   bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
 
@@ -5390,9 +5731,8 @@ bfd_mach_o_fat_extract (bfd *abfd,
       if (res == NULL)
        return NULL;
 
       if (res == NULL)
        return NULL;
 
-      bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e);
-
-      if (bfd_check_format (res, format))
+      if (bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e)
+         && bfd_check_format (res, format))
        {
          BFD_ASSERT (bfd_get_arch_info (res) == arch);
          return res;
        {
          BFD_ASSERT (bfd_get_arch_info (res) == arch);
          return res;
@@ -5404,6 +5744,13 @@ bfd_mach_o_fat_extract (bfd *abfd,
   return NULL;
 }
 
   return NULL;
 }
 
+static bool
+bfd_mach_o_fat_close_and_cleanup (bfd *abfd)
+{
+  _bfd_unlink_from_archive_parent (abfd);
+  return true;
+}
+
 int
 bfd_mach_o_lookup_command (bfd *abfd,
                           bfd_mach_o_load_command_type type,
 int
 bfd_mach_o_lookup_command (bfd *abfd,
                           bfd_mach_o_load_command_type type,
@@ -5437,16 +5784,12 @@ bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
     {
     case BFD_MACH_O_CPU_TYPE_MC680x0:
       return 0x04000000;
     {
     case BFD_MACH_O_CPU_TYPE_MC680x0:
       return 0x04000000;
-    case BFD_MACH_O_CPU_TYPE_MC88000:
-      return 0xffffe000;
     case BFD_MACH_O_CPU_TYPE_POWERPC:
       return 0xc0000000;
     case BFD_MACH_O_CPU_TYPE_I386:
       return 0xc0000000;
     case BFD_MACH_O_CPU_TYPE_SPARC:
       return 0xf0000000;
     case BFD_MACH_O_CPU_TYPE_POWERPC:
       return 0xc0000000;
     case BFD_MACH_O_CPU_TYPE_I386:
       return 0xc0000000;
     case BFD_MACH_O_CPU_TYPE_SPARC:
       return 0xf0000000;
-    case BFD_MACH_O_CPU_TYPE_I860:
-      return 0;
     case BFD_MACH_O_CPU_TYPE_HPPA:
       return 0xc0000000 - 0x04000000;
     default:
     case BFD_MACH_O_CPU_TYPE_HPPA:
       return 0xc0000000 - 0x04000000;
     default:
@@ -5555,6 +5898,8 @@ bfd_mach_o_core_fetch_environment (bfd *abfd,
          unsigned char *buf = bfd_malloc (1024);
          unsigned long size = 1024;
 
          unsigned char *buf = bfd_malloc (1024);
          unsigned long size = 1024;
 
+         if (buf == NULL)
+           return -1;
          for (;;)
            {
              bfd_size_type nread = 0;
          for (;;)
            {
              bfd_size_type nread = 0;
@@ -5569,10 +5914,10 @@ bfd_mach_o_core_fetch_environment (bfd *abfd,
                return -1;
 
              if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
                return -1;
 
              if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
-                {
-                  free (buf);
-                  return -1;
-                }
+               {
+                 free (buf);
+                 return -1;
+               }
 
              nread = bfd_bread (buf, size, abfd);
 
 
              nread = bfd_bread (buf, size, abfd);
 
@@ -5600,6 +5945,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);
                      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);
                      *rlen = top - bottom;
 
                      memcpy (*rbuf, buf + size - *rlen, *rlen);
@@ -5647,13 +5994,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)
   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. */
 
   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;
 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;
@@ -5662,22 +6009,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))
   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)
 
   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)
 
   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,
 
   if (memcmp (uuid_cmd->uuid, dsym_uuid_cmd->uuid,
-              sizeof (uuid_cmd->uuid)) != 0)
-    return FALSE;
+             sizeof (uuid_cmd->uuid)) != 0)
+    return false;
 
 
-  return TRUE;
+  return true;
 }
 
 /* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD.
 }
 
 /* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD.
@@ -5686,8 +6033,8 @@ bfd_mach_o_dsym_for_uuid_p (bfd *abfd, const bfd_mach_o_uuid_command *uuid_cmd)
 
 static bfd *
 bfd_mach_o_find_dsym (const char *dsym_filename,
 
 static bfd *
 bfd_mach_o_find_dsym (const char *dsym_filename,
-                      const bfd_mach_o_uuid_command *uuid_cmd,
-                      const bfd_arch_info_type *arch)
+                     const bfd_mach_o_uuid_command *uuid_cmd,
+                     const bfd_arch_info_type *arch)
 {
   bfd *base_dsym_bfd, *dsym_bfd;
 
 {
   bfd *base_dsym_bfd, *dsym_bfd;
 
@@ -5726,12 +6073,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 (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;
     }
     {
       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)
 
   uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd);
   if (uuid_cmd == NULL)
@@ -5741,21 +6088,24 @@ 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
   */
      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)
-                                       + strlen (dsym_subdir) + 1
-                                       + strlen (base_basename) + 1);
+  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",
   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,
 
   dsym_bfd = bfd_mach_o_find_dsym (dsym_filename, uuid_cmd,
-                                   bfd_get_arch_info (abfd));
+                                  bfd_get_arch_info (abfd));
   if (dsym_bfd == NULL)
     free (dsym_filename);
 
   return dsym_bfd;
 }
 
   if (dsym_bfd == NULL)
     free (dsym_filename);
 
   return dsym_bfd;
 }
 
-bfd_boolean
+bool
 bfd_mach_o_find_nearest_line (bfd *abfd,
                              asymbol **symbols,
                              asection *section,
 bfd_mach_o_find_nearest_line (bfd *abfd,
                              asymbol **symbols,
                              asection *section,
@@ -5767,7 +6117,7 @@ bfd_mach_o_find_nearest_line (bfd *abfd,
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   if (mdata == NULL)
 {
   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:
   switch (mdata->header.filetype)
     {
     case BFD_MACH_O_MH_OBJECT:
@@ -5777,32 +6127,32 @@ bfd_mach_o_find_nearest_line (bfd *abfd,
     case BFD_MACH_O_MH_BUNDLE:
     case BFD_MACH_O_MH_KEXT_BUNDLE:
       if (mdata->dwarf2_find_line_info == NULL)
     case BFD_MACH_O_MH_BUNDLE:
     case BFD_MACH_O_MH_KEXT_BUNDLE:
       if (mdata->dwarf2_find_line_info == NULL)
-        {
-          mdata->dsym_bfd = bfd_mach_o_follow_dsym (abfd);
-          /* When we couldn't find dSYM for this binary, we look for
-             the debug information in the binary itself. In this way,
-             we won't try finding separated dSYM again because
-             mdata->dwarf2_find_line_info will be filled. */
-          if (! mdata->dsym_bfd)
-            break;
-          if (! _bfd_dwarf2_slurp_debug_info (abfd, mdata->dsym_bfd,
-                                              dwarf_debug_sections, symbols,
-                                              &mdata->dwarf2_find_line_info,
-                                             FALSE))
-            return FALSE;
-        }
+       {
+         mdata->dsym_bfd = bfd_mach_o_follow_dsym (abfd);
+         /* When we couldn't find dSYM for this binary, we look for
+            the debug information in the binary itself. In this way,
+            we won't try finding separated dSYM again because
+            mdata->dwarf2_find_line_info will be filled. */
+         if (! mdata->dsym_bfd)
+           break;
+         if (! _bfd_dwarf2_slurp_debug_info (abfd, mdata->dsym_bfd,
+                                             dwarf_debug_sections, symbols,
+                                             &mdata->dwarf2_find_line_info,
+                                             false))
+           return false;
+       }
       break;
     default:
       break;
     default:
-      return FALSE;
+      return false;
     }
   return _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
                                        filename_ptr, functionname_ptr,
                                        line_ptr, discriminator_ptr,
     }
   return _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
                                        filename_ptr, functionname_ptr,
                                        line_ptr, discriminator_ptr,
-                                       dwarf_debug_sections, 0,
+                                       dwarf_debug_sections,
                                        &mdata->dwarf2_find_line_info);
 }
 
                                        &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);
 bfd_mach_o_close_and_cleanup (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
@@ -5811,8 +6161,8 @@ bfd_mach_o_close_and_cleanup (bfd *abfd)
       _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
       bfd_mach_o_free_cached_info (abfd);
       if (mdata->dsym_bfd != NULL)
       _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
       bfd_mach_o_free_cached_info (abfd);
       if (mdata->dsym_bfd != NULL)
-        {
-          bfd *fat_bfd = mdata->dsym_bfd->my_archive;
+       {
+         bfd *fat_bfd = mdata->dsym_bfd->my_archive;
 #if 0
          /* FIXME: PR 19435: This calculation to find the memory allocated by
             bfd_mach_o_follow_dsym for the filename does not always end up
 #if 0
          /* FIXME: PR 19435: This calculation to find the memory allocated by
             bfd_mach_o_follow_dsym for the filename does not always end up
@@ -5822,24 +6172,25 @@ bfd_mach_o_close_and_cleanup (bfd *abfd)
             commented out.  This does mean that there will be a memory leak,
             but it is small, and happens when we are closing down, so it
             should not matter too much.  */
             commented out.  This does mean that there will be a memory leak,
             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);
+         char *dsym_filename = (char *)(fat_bfd
+                                        ? bfd_get_filename (fat_bfd)
+                                        : bfd_get_filename (mdata->dsym_bfd));
 #endif
 #endif
-          bfd_close (mdata->dsym_bfd);
-          mdata->dsym_bfd = NULL;
-          if (fat_bfd)
-            bfd_close (fat_bfd);
+         bfd_close (mdata->dsym_bfd);
+         mdata->dsym_bfd = NULL;
+         if (fat_bfd)
+           bfd_close (fat_bfd);
 #if 0
 #if 0
-          free (dsym_filename);
+         free (dsym_filename);
 #endif
 #endif
-        }
+       }
     }
 
   return _bfd_generic_close_and_cleanup (abfd);
 }
 
     }
 
   return _bfd_generic_close_and_cleanup (abfd);
 }
 
-bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
+bool
+bfd_mach_o_free_cached_info (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   asection *asect;
 {
   bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   asection *asect;
@@ -5851,7 +6202,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
       asect->relocation = NULL;
     }
 
       asect->relocation = NULL;
     }
 
-  return TRUE;
+  return true;
 }
 
 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
 }
 
 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
@@ -5863,12 +6214,12 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #define bfd_mach_o_tgt_seg_table NULL
 #define bfd_mach_o_section_type_valid_for_tgt NULL
 
 #define bfd_mach_o_tgt_seg_table NULL
 #define bfd_mach_o_section_type_valid_for_tgt NULL
 
-#define TARGET_NAME            mach_o_be_vec
-#define TARGET_STRING          "mach-o-be"
+#define TARGET_NAME            mach_o_be_vec
+#define TARGET_STRING          "mach-o-be"
 #define TARGET_ARCHITECTURE    bfd_arch_unknown
 #define TARGET_PAGESIZE                1
 #define TARGET_ARCHITECTURE    bfd_arch_unknown
 #define TARGET_PAGESIZE                1
-#define TARGET_BIG_ENDIAN      1
-#define TARGET_ARCHIVE                 0
+#define TARGET_BIG_ENDIAN      1
+#define TARGET_ARCHIVE         0
 #define TARGET_PRIORITY                1
 #include "mach-o-target.c"
 
 #define TARGET_PRIORITY                1
 #include "mach-o-target.c"
 
@@ -5880,12 +6231,12 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #undef TARGET_ARCHIVE
 #undef TARGET_PRIORITY
 
 #undef TARGET_ARCHIVE
 #undef TARGET_PRIORITY
 
-#define TARGET_NAME            mach_o_le_vec
-#define TARGET_STRING          "mach-o-le"
+#define TARGET_NAME            mach_o_le_vec
+#define TARGET_STRING          "mach-o-le"
 #define TARGET_ARCHITECTURE    bfd_arch_unknown
 #define TARGET_PAGESIZE                1
 #define TARGET_ARCHITECTURE    bfd_arch_unknown
 #define TARGET_PAGESIZE                1
-#define TARGET_BIG_ENDIAN      0
-#define TARGET_ARCHIVE                 0
+#define TARGET_BIG_ENDIAN      0
+#define TARGET_ARCHIVE         0
 #define TARGET_PRIORITY                1
 
 #include "mach-o-target.c"
 #define TARGET_PRIORITY                1
 
 #include "mach-o-target.c"
@@ -5899,21 +6250,21 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #undef TARGET_PRIORITY
 
 /* Not yet handled: creating an archive.  */
 #undef TARGET_PRIORITY
 
 /* Not yet handled: creating an archive.  */
-#define bfd_mach_o_mkarchive                      _bfd_noarchive_mkarchive
+#define bfd_mach_o_mkarchive                     _bfd_noarchive_mkarchive
 
 
-#define bfd_mach_o_close_and_cleanup             bfd_true
+#define bfd_mach_o_close_and_cleanup             bfd_mach_o_fat_close_and_cleanup
 
 /* Not used.  */
 
 /* Not used.  */
-#define bfd_mach_o_generic_stat_arch_elt          bfd_mach_o_fat_stat_arch_elt
+#define bfd_mach_o_generic_stat_arch_elt         bfd_mach_o_fat_stat_arch_elt
 #define bfd_mach_o_openr_next_archived_file      bfd_mach_o_fat_openr_next_archived_file
 #define bfd_mach_o_archive_p   bfd_mach_o_fat_archive_p
 
 #define bfd_mach_o_openr_next_archived_file      bfd_mach_o_fat_openr_next_archived_file
 #define bfd_mach_o_archive_p   bfd_mach_o_fat_archive_p
 
-#define TARGET_NAME            mach_o_fat_vec
-#define TARGET_STRING          "mach-o-fat"
+#define TARGET_NAME            mach_o_fat_vec
+#define TARGET_STRING          "mach-o-fat"
 #define TARGET_ARCHITECTURE    bfd_arch_unknown
 #define TARGET_PAGESIZE                1
 #define TARGET_ARCHITECTURE    bfd_arch_unknown
 #define TARGET_PAGESIZE                1
-#define TARGET_BIG_ENDIAN      1
-#define TARGET_ARCHIVE                 1
+#define TARGET_BIG_ENDIAN      1
+#define TARGET_ARCHIVE         1
 #define TARGET_PRIORITY                0
 
 #include "mach-o-target.c"
 #define TARGET_PRIORITY                0
 
 #include "mach-o-target.c"
This page took 0.09306 seconds and 4 git commands to generate.