mach-o: Define copy_private_header instead of copy_private_bfd.
[deliverable/binutils-gdb.git] / bfd / mach-o.c
index 1f9fc17608539692303a4c1c4c181d047355b730..4de1528597a85934d506598072f22164fafa3ce3 100644 (file)
@@ -1,7 +1,5 @@
 /* Mach-O support for BFD.
-   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
-   2009, 2010, 2011, 2012
-   Free Software Foundation, Inc.
+   Copyright (C) 1999-2014 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -84,7 +82,7 @@ bfd_mach_o_wide_p (bfd *abfd)
 {
   return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
 }
-      
+
 /* Tables to translate well known Mach-O segment/section names to bfd
    names.  Use of canonical names (such as .text or .debug_frame) is required
    by gdb.  */
@@ -92,7 +90,7 @@ bfd_mach_o_wide_p (bfd *abfd)
 /* __TEXT Segment.  */
 static const mach_o_section_name_xlat text_section_names_xlat[] =
   {
-    {  ".text",                                "__text",       
+    {  ".text",                                "__text",
        SEC_CODE | SEC_LOAD,                    BFD_MACH_O_S_REGULAR,
        BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,    0},
     {  ".const",                               "__const",
@@ -197,6 +195,9 @@ static const mach_o_section_name_xlat dwarf_section_names_xlat[] =
     {  ".debug_macro",                 "__debug_macro",
        SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
        BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_gdb_scripts",           "__debug_gdb_scri",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
     { NULL, NULL, 0, 0, 0, 0}
   };
 
@@ -287,7 +288,7 @@ static const char dsym_subdir[] = ".dSYM/Contents/Resources/DWARF";
 
 /* Fetch the translation from a Mach-O section designation (segment, section)
    as a bfd short name, if one exists.  Otherwise return NULL.
-   
+
    Allow the segment and section names to be unterminated 16 byte arrays.  */
 
 const mach_o_section_name_xlat *
@@ -315,11 +316,11 @@ bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
                     BFD_MACH_O_SECTNAME_SIZE) == 0)
           return sec;
 
-  return NULL;  
+  return NULL;
 }
 
 /* If the bfd_name for this section is a 'canonical' form for which we
-   know the Mach-O data, return the segment name and the data for the 
+   know the Mach-O data, return the segment name and the data for the
    Mach-O equivalent.  Otherwise return NULL.  */
 
 const mach_o_section_name_xlat *
@@ -353,12 +354,12 @@ bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name,
          return sec;
        }
 
-  return NULL;  
+  return NULL;
 }
 
 /* Convert Mach-O section name to BFD.
 
-   Try to use standard/canonical names, for which we have tables including 
+   Try to use standard/canonical names, for which we have tables including
    default flag settings - which are returned.  Otherwise forge a new name
    in the form "<segmentname>.<sectionname>" this will be prefixed with
    LC_SEGMENT. if the segment name does not begin with an underscore.
@@ -380,7 +381,7 @@ bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
   *name = NULL;
   *flags = SEC_NO_FLAGS;
 
-  /* First search for a canonical name...  
+  /* First search for a canonical name...
      xlat will be non-null if there is an entry for segname, secname.  */
   xlat = bfd_mach_o_section_data_for_mach_sect (abfd, segname, secname);
   if (xlat)
@@ -423,7 +424,7 @@ bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
    return the translation table - which contains defaults for flags,
    type, attribute and default alignment data.
 
-   Otherwise, expand the bfd_name (assumed to be in the form 
+   Otherwise, expand the bfd_name (assumed to be in the form
    "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL.  */
 
 static const mach_o_section_name_xlat *
@@ -466,7 +467,8 @@ bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
       seglen = dot - name;
       seclen = len - (dot + 1 - name);
 
-      if (seglen < 16 && seclen < 16)
+      if (seglen <= BFD_MACH_O_SEGNAME_SIZE
+         && seclen <= BFD_MACH_O_SECTNAME_SIZE)
         {
           memcpy (section->segname, name, seglen);
           section->segname[seglen] = 0;
@@ -558,11 +560,11 @@ bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
 {
   if (osection->used_by_bfd == NULL)
     osection->used_by_bfd = isection->used_by_bfd;
-  else 
+  else
     if (isection->used_by_bfd != NULL)
-      memcpy (osection->used_by_bfd, isection->used_by_bfd, 
+      memcpy (osection->used_by_bfd, isection->used_by_bfd,
              sizeof (bfd_mach_o_section));
+
   if (osection->used_by_bfd != NULL)
     ((bfd_mach_o_section *)osection->used_by_bfd)->bfdsection = osection;
 
@@ -573,7 +575,7 @@ bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
    to the output bfd.  */
 
 bfd_boolean
-bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
+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)
@@ -590,7 +592,7 @@ bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
 /* 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
 bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags)
 {
@@ -706,7 +708,7 @@ bfd_mach_o_get_synthetic_symtab (bfd *abfd,
   names = (char *) (s + count);
   nul_name = names;
   *names++ = 0;
-  
+
   n = 0;
   for (i = 0; i < mdata->nsects; i++)
     {
@@ -714,7 +716,7 @@ bfd_mach_o_get_synthetic_symtab (bfd *abfd,
       unsigned int first, last;
       bfd_vma addr;
       bfd_vma entry_size;
-      
+
       switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
         {
         case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
@@ -733,7 +735,7 @@ bfd_mach_o_get_synthetic_symtab (bfd *abfd,
               s->section = sec->bfdsection;
               s->value = addr - sec->addr;
               s->udata.p = NULL;
-              
+
               if (isym < symtab->nsyms
                   && symtab->symbols[isym].symbol.name)
                 {
@@ -904,6 +906,10 @@ bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
       *type = bfd_arch_powerpc;
       *subtype = bfd_mach_ppc64;
       break;
+    case BFD_MACH_O_CPU_TYPE_ARM64:
+      *type = bfd_arch_aarch64;
+      *subtype = bfd_mach_aarch64;
+      break;
     default:
       *type = bfd_arch_unknown;
       break;
@@ -937,7 +943,7 @@ bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
   return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_thread_command *cmd = &command->command.thread;
@@ -960,12 +966,12 @@ bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
 
       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
           || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
-       return -1;
+       return FALSE;
 
       offset += cmd->flavours[i].size + sizeof (raw);
     }
 
-  return 0;
+  return TRUE;
 }
 
 long
@@ -989,7 +995,7 @@ bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
       rel->r_value = (fields[0] << 16) | (fields[1] << 8) | fields[2];
       rel->r_type = (info >> BFD_MACH_O_BE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
       rel->r_pcrel = (info & BFD_MACH_O_BE_PCREL) ? 1 : 0;
-      rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT) 
+      rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT)
                      & BFD_MACH_O_LENGTH_MASK;
       rel->r_extern = (info & BFD_MACH_O_BE_EXTERN) ? 1 : 0;
     }
@@ -998,7 +1004,7 @@ bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
       rel->r_value = (fields[2] << 16) | (fields[1] << 8) | fields[0];
       rel->r_type = (info >> BFD_MACH_O_LE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
       rel->r_pcrel = (info & BFD_MACH_O_LE_PCREL) ? 1 : 0;
-      rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT) 
+      rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT)
                      & BFD_MACH_O_LENGTH_MASK;
       rel->r_extern = (info & BFD_MACH_O_LE_EXTERN) ? 1 : 0;
     }
@@ -1018,7 +1024,7 @@ bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
   addr = bfd_get_32 (abfd, raw->r_address);
   res->sym_ptr_ptr = NULL;
   res->addend = 0;
-  
+
   if (addr & BFD_MACH_O_SR_SCATTERED)
     {
       unsigned int j;
@@ -1056,22 +1062,34 @@ bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
   else
     {
       unsigned int num;
-      
+
       /* Non-scattered relocation.  */
       reloc.r_scattered = 0;
-      
+
       /* The value and info fields have to be extracted dependent on target
          endian-ness.  */
       bfd_mach_o_swap_in_non_scattered_reloc (abfd, &reloc, raw->r_symbolnum);
       num = reloc.r_value;
 
       if (reloc.r_extern)
-          sym = syms + num;
-      else if (reloc.r_scattered
-              || (reloc.r_type != BFD_MACH_O_GENERIC_RELOC_PAIR))
+       {
+         /* An external symbol number.  */
+         sym = syms + num;
+       }
+      else if (num == 0x00ffffff || num == 0)
+       {
+         /* The 'symnum' in a non-scattered PAIR is 0x00ffffff.  But as this
+            is generic code, we don't know wether this is really a PAIR.
+            This value is almost certainly not a valid section number, hence
+            this specific case to avoid an assertion failure.
+            Target specific swap_reloc_in routine should adjust that.  */
+         sym = bfd_abs_section_ptr->symbol_ptr_ptr;
+       }
+      else
         {
-          BFD_ASSERT (num != 0);
+         /* A section number.  */
           BFD_ASSERT (num <= mdata->nsects);
+
           sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
           /* For a symbol defined in section S, the addend (stored in the
              binary) contains the address of the section.  To comply with
@@ -1080,26 +1098,23 @@ bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
              the vma of the section.  */
           res->addend = -mdata->sections[num - 1]->addr;
         }
-      else /* ... The 'symnum' in a non-scattered PAIR will be 0x00ffffff.  */
-        {
-          /* Pairs for PPC LO/HI/HA are not scattered, but contain the offset
-             in the lower 16bits of the address value.  So we have to find the
-             'symbol' from the preceding reloc.  We do this even thoough the
-             section symbol is probably not needed here, because NULL symbol
-             values cause an assert in generic BFD code.  */
-          sym = (res - 1)->sym_ptr_ptr;
-        }
+      /* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset
+        in the lower 16bits of the address value.  So we have to find the
+        'symbol' from the preceding reloc.  We do this even though the
+        section symbol is probably not needed here, because NULL symbol
+        values cause an assert in generic BFD code.  This must be done in
+        the PPC swap_reloc_in routine.  */
       res->sym_ptr_ptr = sym;
-      
+
       /* The 'address' is just r_address.
          ??? maybe this should be masked with  0xffffff for safety.  */
       res->address = addr;
       reloc.r_address = addr;
     }
-  
-  /* 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.  */
 
   if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
     return -1;
@@ -1311,9 +1326,9 @@ bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
 
           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);
+            | 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);
@@ -1332,7 +1347,7 @@ bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
   return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
 {
   struct mach_o_section_32_external raw;
@@ -1351,12 +1366,12 @@ bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
 
   if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
       != BFD_MACH_O_SECTION_SIZE)
-    return -1;
+    return FALSE;
 
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
 {
   struct mach_o_section_64_external raw;
@@ -1376,12 +1391,12 @@ bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
 
   if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
       != BFD_MACH_O_SECTION_64_SIZE)
-    return -1;
+    return FALSE;
 
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
 {
   struct mach_o_segment_command_32_external raw;
@@ -1392,7 +1407,7 @@ bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
 
   for (sec = seg->sect_head; sec != NULL; sec = sec->next)
     if (!bfd_mach_o_write_relocs (abfd, sec))
-      return -1;
+      return FALSE;
 
   memcpy (raw.segname, seg->segname, 16);
   bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
@@ -1403,19 +1418,19 @@ bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
   bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
   bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
   bfd_h_put_32 (abfd, seg->flags, raw.flags);
-  
+
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   for (sec = seg->sect_head; sec != NULL; sec = sec->next)
-    if (bfd_mach_o_write_section_32 (abfd, sec))
-      return -1;
+    if (!bfd_mach_o_write_section_32 (abfd, sec))
+      return FALSE;
 
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
 {
   struct mach_o_segment_command_64_external raw;
@@ -1426,7 +1441,7 @@ bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
 
   for (sec = seg->sect_head; sec != NULL; sec = sec->next)
     if (!bfd_mach_o_write_relocs (abfd, sec))
-      return -1;
+      return FALSE;
 
   memcpy (raw.segname, seg->segname, 16);
   bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
@@ -1440,13 +1455,13 @@ bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   for (sec = seg->sect_head; sec != NULL; sec = sec->next)
-    if (bfd_mach_o_write_section_64 (abfd, sec))
-      return -1;
+    if (!bfd_mach_o_write_section_64 (abfd, sec))
+      return FALSE;
 
-  return 0;
+  return TRUE;
 }
 
 static bfd_boolean
@@ -1463,7 +1478,7 @@ bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
 
   /* Write the symbols first.  */
-  mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
+  mdata->filelen = FILE_ALIGN (mdata->filelen, wide ? 3 : 2);
   sym->symoff = mdata->filelen;
   if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
     return FALSE;
@@ -1485,7 +1500,7 @@ bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
     {
       bfd_size_type str_index;
       bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
-     
+
       if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
        /* An index of 0 always means the empty string.  */
         str_index = 0;
@@ -1652,7 +1667,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
            return FALSE;
        }
     }
-  
+
   if (cmd->nindirectsyms > 0)
     {
       unsigned int i;
@@ -1667,7 +1682,7 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
          bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw);
          if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
            return FALSE;
-       }    
+       }
     }
 
   if (cmd->nextrefsyms != 0)
@@ -1848,7 +1863,7 @@ bfd_mach_o_mangle_symbols (bfd *abfd)
             }
           else
             s->n_type = BFD_MACH_O_N_SECT;
-          
+
           if (s->symbol.flags & BSF_GLOBAL)
             s->n_type |= BFD_MACH_O_N_EXT;
         }
@@ -1859,7 +1874,7 @@ bfd_mach_o_mangle_symbols (bfd *abfd)
           && 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->target_index;
+       s->n_sect = s->symbol.section->output_section->target_index;
 
       /* Number to preserve order for local and debug syms.  */
       s->symbol.udata.i = i;
@@ -1889,7 +1904,7 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
   unsigned nsect;
 
   nsect = bfd_count_sections (abfd);
-  
+
   /* Don't do it if it's already set - assume the application knows what it's
      doing.  */
   if (mdata->nsects == nsect
@@ -1897,7 +1912,7 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
     return TRUE;
 
   mdata->nsects = nsect;
-  mdata->sections = bfd_alloc (abfd, 
+  mdata->sections = bfd_alloc (abfd,
                               mdata->nsects * sizeof (bfd_mach_o_section *));
   if (mdata->sections == NULL)
     return FALSE;
@@ -1908,7 +1923,7 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
                             " maximum is 255,\n"), nsect);
 
   /* Create Mach-O sections.
-     Section type, attribute and align should have been set when the 
+     Section type, attribute and align should have been set when the
      section was created - either read in or specified.  */
   target_index = 0;
   for (sec = abfd->sections; sec; sec = sec->next)
@@ -1921,7 +1936,7 @@ bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
       msect->addr = bfd_get_section_vma (abfd, sec);
       msect->size = bfd_get_section_size (sec);
 
-      /* Use the largest alignment set, in case it was bumped after the 
+      /* Use the largest alignment set, in case it was bumped after the
         section was created.  */
       msect->align = msect->align > bfd_align ? msect->align : bfd_align;
 
@@ -1964,11 +1979,11 @@ bfd_mach_o_write_contents (bfd *abfd)
       switch (cur->type)
        {
        case BFD_MACH_O_LC_SEGMENT:
-         if (bfd_mach_o_write_segment_32 (abfd, cur) != 0)
+         if (!bfd_mach_o_write_segment_32 (abfd, cur))
            return FALSE;
          break;
        case BFD_MACH_O_LC_SEGMENT_64:
-         if (bfd_mach_o_write_segment_64 (abfd, cur) != 0)
+         if (!bfd_mach_o_write_segment_64 (abfd, cur))
            return FALSE;
          break;
        case BFD_MACH_O_LC_SYMTAB:
@@ -1983,7 +1998,7 @@ bfd_mach_o_write_contents (bfd *abfd)
          break;
        case BFD_MACH_O_LC_THREAD:
        case BFD_MACH_O_LC_UNIXTHREAD:
-         if (bfd_mach_o_write_thread (abfd, cur) != 0)
+         if (!bfd_mach_o_write_thread (abfd, cur))
            return FALSE;
          break;
        case BFD_MACH_O_LC_LOADFVMLIB:
@@ -2003,8 +2018,9 @@ bfd_mach_o_write_contents (bfd *abfd)
        case BFD_MACH_O_LC_SUB_FRAMEWORK:
          break;
        default:
-         (*_bfd_error_handler) (_("unable to write unknown load command 0x%lx"),
-                                (unsigned long) cur->type);
+         (*_bfd_error_handler)
+           (_("unable to write unknown load command 0x%lx"),
+            (unsigned long) cur->type);
          return FALSE;
        }
     }
@@ -2027,7 +2043,8 @@ bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
 /* Create section Mach-O flags from BFD flags.  */
 
 static void
-bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
+bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
+                                      asection *sec)
 {
   flagword bfd_flags;
   bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
@@ -2049,15 +2066,15 @@ bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED, asection *sec
 /* Count the number of sections in the list for the segment named.
 
    The special case of NULL or "" for the segment name is valid for
-   an MH_OBJECT file and means 'all sections available'.  
-   
-   Requires that the sections table in mdata be filled in.  
+   an MH_OBJECT file and means 'all sections available'.
+
+   Requires that the sections table in mdata be filled in.
 
    Returns the number of sections (0 is valid).
    Any number > 255 signals an invalid section count, although we will,
    perhaps, allow the file to be written (in line with Darwin tools up
-   to XCode 4). 
-   
+   to XCode 4).
+
    A section count of (unsigned long) -1 signals a definite error.  */
 
 static unsigned long
@@ -2112,11 +2129,11 @@ bfd_mach_o_build_seg_command (const char *segment,
   seg->sect_head = NULL;
   seg->sect_tail = NULL;
 
-  /*  Append sections to the segment.  
+  /*  Append sections to the segment.
 
       This is a little tedious, we have to honor the need to account zerofill
       sections after all the rest.  This forces us to do the calculation of
-      total vmsize in three passes so that any alignment increments are 
+      total vmsize in three passes so that any alignment increments are
       properly accounted.  */
 
   for (i = 0; i < mdata->nsects; ++i)
@@ -2126,7 +2143,7 @@ bfd_mach_o_build_seg_command (const char *segment,
 
       /* If we're not making an MH_OBJECT, check whether this section is from
         our segment, and skip if not.  Otherwise, just add all sections.  */
-      if (! is_mho 
+      if (! is_mho
          && strncmp (segment, s->segname, BFD_MACH_O_SEGNAME_SIZE) != 0)
        continue;
 
@@ -2134,11 +2151,11 @@ bfd_mach_o_build_seg_command (const char *segment,
         placed in the file in source sequence.  */
       bfd_mach_o_append_section_to_segment (seg, sec);
       s->offset = 0;
-      
-      /* Zerofill sections have zero file size & offset, 
+
+      /* Zerofill sections have zero file size & offset,
         and are not written.  */
       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)
         continue;
 
@@ -2150,6 +2167,7 @@ bfd_mach_o_build_seg_command (const char *segment,
          seg->filesize = FILE_ALIGN (seg->filesize, s->align);
          seg->filesize += s->size;
 
+         /* Note: follow alignment even for object file.  */
           mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
           s->offset = mdata->filelen;
         }
@@ -2158,7 +2176,13 @@ bfd_mach_o_build_seg_command (const char *segment,
       mdata->filelen += s->size;
     }
 
-  /* Now pass through again, for zerofill, only now we just update the vmsize.  */
+  /* Be sure the file offset of the segment is the file offset of its first
+     section (may have changed due to alignment).  */
+  if (seg->sect_head != NULL)
+    seg->fileoff = seg->sect_head->offset;
+
+  /* Now pass through again, for zerofill, only now we just update the
+     vmsize.  */
   for (i = 0; i < mdata->nsects; ++i)
     {
       bfd_mach_o_section *s = mdata->sections[i];
@@ -2166,9 +2190,9 @@ bfd_mach_o_build_seg_command (const char *segment,
       if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_ZEROFILL)
         continue;
 
-      if (! is_mho 
-         && strncmp (segment, s->segname, BFD_MACH_O_SEGNAME_SIZE) != 0)
-       continue;
+      if (! is_mho
+         && strncmp (segment, s->segname, BFD_MACH_O_SEGNAME_SIZE) != 0)
+       continue;
 
       if (s->size > 0)
        {
@@ -2181,11 +2205,11 @@ bfd_mach_o_build_seg_command (const char *segment,
   for (i = 0; i < mdata->nsects; ++i)
     {
       bfd_mach_o_section *s = mdata->sections[i];
+
       if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_GB_ZEROFILL)
         continue;
 
-      if (! is_mho 
+      if (! is_mho
          && strncmp (segment, s->segname, BFD_MACH_O_SEGNAME_SIZE) != 0)
        continue;
 
@@ -2197,13 +2221,13 @@ bfd_mach_o_build_seg_command (const char *segment,
     }
 
   /* Allocate space for the relocations.  */
-  mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
+  mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
 
   for (i = 0; i < mdata->nsects; ++i)
     {
       bfd_mach_o_section *ms = mdata->sections[i];
       asection *sec = ms->bfdsection;
-        
+
       if ((ms->nreloc = sec->reloc_count) == 0)
         {
          ms->reloff = 0;
@@ -2287,8 +2311,8 @@ bfd_mach_o_build_dysymtab_command (bfd *abfd,
        }
       dsym->nextdefsym = i - dsym->nlocalsym;
       dsym->iundefsym = dsym->nextdefsym + dsym->iextdefsym;
-      dsym->nundefsym = bfd_get_symcount (abfd) 
-                       - dsym->nlocalsym 
+      dsym->nundefsym = bfd_get_symcount (abfd)
+                       - dsym->nlocalsym
                        - dsym->nextdefsym;
     }
   else
@@ -2309,11 +2333,11 @@ bfd_mach_o_build_dysymtab_command (bfd *abfd,
       mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
       dsym->indirectsymoff = mdata->filelen;
       mdata->filelen += dsym->nindirectsyms * 4;
-      
+
       dsym->indirect_syms = bfd_zalloc (abfd, dsym->nindirectsyms * 4);
       if (dsym->indirect_syms == NULL)
         return FALSE;
-                 
+
       n = 0;
       for (i = 0; i < mdata->nsects; ++i)
        {
@@ -2327,7 +2351,7 @@ bfd_mach_o_build_dysymtab_command (bfd *abfd,
                {
                  unsigned j, num;
                  bfd_mach_o_asymbol **isyms = sec->indirect_syms;
-                 
+
                  num = bfd_mach_o_section_get_nbr_indirect (abfd, sec);
                  if (isyms == NULL || num == 0)
                    break;
@@ -2403,7 +2427,7 @@ bfd_mach_o_build_commands (bfd *abfd)
 
        a command (segment) to contain all the sections,
        command for the symbol table,
-       a command for the dysymtab.  
+       a command for the dysymtab.
 
      ??? maybe we should assert that this is an MH_OBJECT?  */
 
@@ -2449,11 +2473,12 @@ bfd_mach_o_build_commands (bfd *abfd)
     return FALSE;
 
   if (segcmd_idx >= 0)
-    {  
+    {
       bfd_mach_o_load_command *cmd = &mdata->commands[segcmd_idx];
       bfd_mach_o_segment_command *seg = &cmd->command.segment;
 
-      /* Count the segctions in the special blank segment used for MH_OBJECT.  */
+      /* Count the segctions in the special blank segment used
+        for MH_OBJECT.  */
       seg->nsects = bfd_mach_o_count_sections_for_seg (NULL, mdata);
       if (seg->nsects == (unsigned long) -1)
        return FALSE;
@@ -2482,7 +2507,7 @@ bfd_mach_o_build_commands (bfd *abfd)
     {
       /* Init symtab command.  */
       bfd_mach_o_load_command *cmd = &mdata->commands[symtab_idx];
-  
+
       cmd->type = BFD_MACH_O_LC_SYMTAB;
       cmd->offset = base_offset;
       if (segcmd_idx >= 0)
@@ -2503,10 +2528,10 @@ bfd_mach_o_build_commands (bfd *abfd)
 
       cmd->type = BFD_MACH_O_LC_DYSYMTAB;
       if (symtab_idx >= 0)
-        cmd->offset = mdata->commands[symtab_idx].offset 
+        cmd->offset = mdata->commands[symtab_idx].offset
                    + mdata->commands[symtab_idx].len;
       else if (segcmd_idx >= 0)
-        cmd->offset = mdata->commands[segcmd_idx].offset 
+        cmd->offset = mdata->commands[segcmd_idx].offset
                    + mdata->commands[segcmd_idx].len;
       else
        cmd->offset = base_offset;
@@ -2522,13 +2547,13 @@ bfd_mach_o_build_commands (bfd *abfd)
   /* 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.  */
   if (segcmd_idx >= 0
-      && ! bfd_mach_o_build_seg_command 
+      && ! bfd_mach_o_build_seg_command
                (NULL, mdata, &mdata->commands[segcmd_idx].command.segment))
     return FALSE;
 
   /* If we're doing a dysymtab, cmd points to its load command.  */
   if (dysymtab_idx >= 0
-      && ! bfd_mach_o_build_dysymtab_command (abfd, mdata, 
+      && ! bfd_mach_o_build_dysymtab_command (abfd, mdata,
                                              &mdata->commands[dysymtab_idx]))
     return FALSE;
 
@@ -2681,9 +2706,9 @@ bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
       if (xlat != NULL)
        {
          s->flags = xlat->macho_sectype | xlat->macho_secattr;
-         s->align = xlat->sectalign > bfdalign ? xlat->sectalign 
+         s->align = xlat->sectalign > bfdalign ? xlat->sectalign
                                                : bfdalign;
-         bfd_set_section_alignment (abfd, sec, s->align);
+         (void) bfd_set_section_alignment (abfd, sec, s->align);
          bfd_flags = bfd_get_section_flags (abfd, sec);
          if (bfd_flags == SEC_NO_FLAGS)
            bfd_set_section_flags (abfd, sec, xlat->bfd_flags);
@@ -3124,43 +3149,44 @@ bfd_mach_o_ppc_flavour_string (unsigned int flavour)
     }
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
   struct mach_o_str_command_external raw;
   unsigned int nameoff;
-
-  BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
-             || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
+  unsigned int namelen;
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   nameoff = bfd_h_get_32 (abfd, raw.str);
 
-  cmd->name_offset = command->offset + nameoff;
-  cmd->name_len = command->len - nameoff;
-  cmd->name_str = bfd_alloc (abfd, cmd->name_len);
+  cmd->name_offset = nameoff;
+  namelen = command->len - nameoff;
+  nameoff += command->offset;
+  cmd->name_str = bfd_alloc (abfd, namelen);
   if (cmd->name_str == NULL)
-    return -1;
-  if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
-      || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
-    return -1;
-  return 0;
+    return FALSE;
+  if (bfd_seek (abfd, nameoff, SEEK_SET) != 0
+      || bfd_bread (cmd->name_str, namelen, abfd) != namelen)
+    return FALSE;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylib_command *cmd = &command->command.dylib;
   struct mach_o_dylib_command_external raw;
   unsigned int nameoff;
+  unsigned int namelen;
 
   switch (command->type)
     {
     case BFD_MACH_O_LC_LOAD_DYLIB:
+    case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
     case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
     case BFD_MACH_O_LC_ID_DYLIB:
     case BFD_MACH_O_LC_REEXPORT_DYLIB:
@@ -3168,12 +3194,12 @@ bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
       break;
     default:
       BFD_FAIL ();
-      return -1;
+      return FALSE;
     }
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   nameoff = bfd_h_get_32 (abfd, raw.name);
   cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp);
@@ -3181,53 +3207,111 @@ bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
   cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version);
 
   cmd->name_offset = command->offset + nameoff;
-  cmd->name_len = command->len - nameoff;
-  cmd->name_str = bfd_alloc (abfd, cmd->name_len);
+  namelen = command->len - nameoff;
+  cmd->name_str = bfd_alloc (abfd, namelen);
   if (cmd->name_str == NULL)
-    return -1;
+    return FALSE;
   if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
-      || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
-    return -1;
-  return 0;
+      || bfd_bread (cmd->name_str, namelen, abfd) != namelen)
+    return FALSE;
+  return TRUE;
 }
 
-static int
-bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
-                                bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
+static bfd_boolean
+bfd_mach_o_read_prebound_dylib (bfd *abfd,
+                                bfd_mach_o_load_command *command)
 {
-  /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
+  bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib;
+  struct mach_o_prebound_dylib_command_external raw;
+  unsigned int nameoff;
+  unsigned int modoff;
+  unsigned int str_len;
+  unsigned char *str;
 
-  BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
-  return 0;
+  if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return FALSE;
+
+  nameoff = bfd_h_get_32 (abfd, raw.name);
+  modoff = bfd_h_get_32 (abfd, raw.linked_modules);
+  if (nameoff > command->len || modoff > command->len)
+    return FALSE;
+
+  str_len = command->len - sizeof (raw);
+  str = bfd_alloc (abfd, str_len);
+  if (str == NULL)
+    return FALSE;
+  if (bfd_bread (str, str_len, abfd) != str_len)
+    return FALSE;
+
+  cmd->name_offset = command->offset + nameoff;
+  cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules);
+  cmd->linked_modules_offset = command->offset + modoff;
+
+  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;
 }
 
-static int
+static bfd_boolean
+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 (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return FALSE;
+
+  cmd->cksum = bfd_get_32 (abfd, raw.cksum);
+  return TRUE;
+}
+
+static bfd_boolean
+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 (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return FALSE;
+
+  cmd->offset = bfd_get_32 (abfd, raw.offset);
+  cmd->nhints = bfd_get_32 (abfd, raw.nhints);
+  return TRUE;
+}
+
+static bfd_boolean
 bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib;
   struct mach_o_fvmlib_command_external raw;
   unsigned int nameoff;
+  unsigned int namelen;
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   nameoff = bfd_h_get_32 (abfd, raw.name);
   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;
-  fvm->name_len = command->len - nameoff;
-  fvm->name_str = bfd_alloc (abfd, fvm->name_len);
+  namelen = command->len - nameoff;
+  fvm->name_str = bfd_alloc (abfd, namelen);
   if (fvm->name_str == NULL)
-    return -1;
+    return FALSE;
   if (bfd_seek (abfd, fvm->name_offset, SEEK_SET) != 0
-      || bfd_bread (fvm->name_str, fvm->name_len, abfd) != fvm->name_len)
-    return -1;
-  return 0;
+      || bfd_bread (fvm->name_str, namelen, abfd) != namelen)
+    return FALSE;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 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);
@@ -3247,11 +3331,11 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
       struct mach_o_thread_command_external raw;
 
       if (offset >= command->len)
-       return -1;
+       return FALSE;
 
       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
           || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-       return -1;
+       return FALSE;
 
       offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
       nflavours++;
@@ -3261,7 +3345,7 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
   cmd->flavours = bfd_alloc
     (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
   if (cmd->flavours == NULL)
-    return -1;
+    return FALSE;
   cmd->nflavours = nflavours;
 
   offset = 8;
@@ -3271,14 +3355,14 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
       struct mach_o_thread_command_external raw;
 
       if (offset >= command->len)
-       return -1;
+       return FALSE;
 
       if (nflavours >= cmd->nflavours)
-       return -1;
+       return FALSE;
 
       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
           || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-       return -1;
+       return FALSE;
 
       cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
       cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
@@ -3300,11 +3384,13 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
        {
        case BFD_MACH_O_CPU_TYPE_POWERPC:
        case BFD_MACH_O_CPU_TYPE_POWERPC_64:
-         flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
+         flavourstr =
+           bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
          break;
        case BFD_MACH_O_CPU_TYPE_I386:
        case BFD_MACH_O_CPU_TYPE_X86_64:
-         flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
+         flavourstr =
+           bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
          break;
        default:
          flavourstr = "UNKNOWN_ARCHITECTURE";
@@ -3314,7 +3400,7 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
       snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
       sname = bfd_alloc (abfd, snamelen);
       if (sname == NULL)
-       return -1;
+       return FALSE;
 
       for (;;)
        {
@@ -3335,10 +3421,10 @@ bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
       cmd->section = bfdsec;
     }
 
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
@@ -3351,7 +3437,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
 
     if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
         || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-      return -1;
+      return FALSE;
 
     cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
     cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
@@ -3382,10 +3468,10 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
       cmd->dylib_module =
         bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
       if (cmd->dylib_module == NULL)
-        return -1;
+        return FALSE;
 
       if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
-        return -1;
+        return FALSE;
 
       for (i = 0; i < cmd->nmodtab; i++)
         {
@@ -3394,7 +3480,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
           unsigned char buf[56];
 
           if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
-            return -1;
+            return FALSE;
 
           module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
           module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
@@ -3431,10 +3517,10 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
       cmd->dylib_toc = bfd_alloc
         (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
       if (cmd->dylib_toc == NULL)
-        return -1;
+        return FALSE;
 
       if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
-        return -1;
+        return FALSE;
 
       for (i = 0; i < cmd->ntoc; i++)
         {
@@ -3442,7 +3528,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
           bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
 
           if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-            return -1;
+            return FALSE;
 
           toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
           toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
@@ -3456,10 +3542,10 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
       cmd->indirect_syms = bfd_alloc
         (abfd, cmd->nindirectsyms * sizeof (unsigned int));
       if (cmd->indirect_syms == NULL)
-        return -1;
+        return FALSE;
 
       if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
-        return -1;
+        return FALSE;
 
       for (i = 0; i < cmd->nindirectsyms; i++)
         {
@@ -3467,7 +3553,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
           unsigned int *is = &cmd->indirect_syms[i];
 
           if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
-            return -1;
+            return FALSE;
 
           *is = bfd_h_get_32 (abfd, raw);
         }
@@ -3481,10 +3567,10 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
       cmd->ext_refs = bfd_alloc
         (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
       if (cmd->ext_refs == NULL)
-        return -1;
+        return FALSE;
 
       if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
-        return -1;
+        return FALSE;
 
       for (i = 0; i < cmd->nextrefsyms; i++)
         {
@@ -3492,7 +3578,7 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
           bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
 
           if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
-            return -1;
+            return FALSE;
 
           /* Fields isym and flags are written as bit-fields, thus we need
              a specific processing for endianness.  */
@@ -3511,13 +3597,13 @@ bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
     }
 
   if (mdata->dysymtab)
-    return -1;
+    return FALSE;
   mdata->dysymtab = cmd;
 
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_symtab_command *symtab = &command->command.symtab;
@@ -3528,7 +3614,7 @@ bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
   symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
@@ -3541,12 +3627,12 @@ bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
     abfd->flags |= HAS_SYMS;
 
   if (mdata->symtab)
-    return -1;
+    return FALSE;
   mdata->symtab = symtab;
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_uuid_command *cmd = &command->command.uuid;
@@ -3555,12 +3641,12 @@ bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bread (cmd->uuid, 16, abfd) != 16)
-    return -1;
+    return FALSE;
 
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
@@ -3568,14 +3654,14 @@ bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
   cmd->datasize = bfd_get_32 (abfd, raw.datasize);
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_str_command *cmd = &command->command.str;
@@ -3584,21 +3670,21 @@ bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
 
   if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
       || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   off = bfd_get_32 (abfd, raw.str);
   cmd->stroff = command->offset + off;
   cmd->str_len = command->len - off;
   cmd->str = bfd_alloc (abfd, cmd->str_len);
   if (cmd->str == NULL)
-    return -1;
+    return FALSE;
   if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
       || bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len)
-    return -1;
-  return 0;
+    return FALSE;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 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;
@@ -3606,7 +3692,7 @@ bfd_mach_o_read_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_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-    return -1;
+    return FALSE;
 
   cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
   cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
@@ -3618,7 +3704,7 @@ bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
   cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size);
   cmd->export_off = bfd_get_32 (abfd, raw.export_off);
   cmd->export_size = bfd_get_32 (abfd, raw.export_size);
-  return 0;
+  return TRUE;
 }
 
 static bfd_boolean
@@ -3656,7 +3742,49 @@ bfd_mach_o_read_encryption_info (bfd *abfd, bfd_mach_o_load_command *command)
   return TRUE;
 }
 
-static int
+static bfd_boolean
+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 (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return FALSE;
+
+  cmd->entryoff = bfd_get_64 (abfd, raw.entryoff);
+  cmd->stacksize = bfd_get_64 (abfd, raw.stacksize);
+  return TRUE;
+}
+
+static bfd_boolean
+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 (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return FALSE;
+
+  ver = bfd_get_64 (abfd, raw.version);
+  /* Note: we use a serie of shift to avoid shift > 32 (for which gcc
+     generates warnings) in case of the host doesn't support 64 bit
+     integers.  */
+  cmd->e = ver & 0x3ff;
+  ver >>= 10;
+  cmd->d = ver & 0x3ff;
+  ver >>= 10;
+  cmd->c = ver & 0x3ff;
+  ver >>= 10;
+  cmd->b = ver & 0x3ff;
+  ver >>= 10;
+  cmd->a = ver & 0xffffff;
+  return TRUE;
+}
+
+static bfd_boolean
 bfd_mach_o_read_segment (bfd *abfd,
                          bfd_mach_o_load_command *command,
                          unsigned int wide)
@@ -3672,7 +3800,7 @@ bfd_mach_o_read_segment (bfd *abfd,
 
       if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
           || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-        return -1;
+        return FALSE;
 
       memcpy (seg->segname, raw.segname, 16);
       seg->segname[16] = '\0';
@@ -3694,7 +3822,7 @@ bfd_mach_o_read_segment (bfd *abfd,
 
       if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
           || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
-        return -1;
+        return FALSE;
 
       memcpy (seg->segname, raw.segname, 16);
       seg->segname[16] = '\0';
@@ -3725,27 +3853,27 @@ bfd_mach_o_read_segment (bfd *abfd,
 
       sec = bfd_mach_o_read_section (abfd, segoff, seg->initprot, wide);
       if (sec == NULL)
-        return -1;
+        return FALSE;
 
       bfd_mach_o_append_section_to_segment (seg, sec);
     }
 
-  return 0;
+  return TRUE;
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
 {
   return bfd_mach_o_read_segment (abfd, command, 0);
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
 {
   return bfd_mach_o_read_segment (abfd, command, 1);
 }
 
-static int
+static bfd_boolean
 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
 {
   struct mach_o_load_command_external raw;
@@ -3754,7 +3882,7 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
   /* Read command type and length.  */
   if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
       || bfd_bread (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE)
-    return -1;
+    return FALSE;
 
   cmd = bfd_h_get_32 (abfd, raw.cmd);
   command->type =  cmd & ~BFD_MACH_O_LC_REQ_DYLD;
@@ -3764,45 +3892,47 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
   switch (command->type)
     {
     case BFD_MACH_O_LC_SEGMENT:
-      if (bfd_mach_o_read_segment_32 (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_segment_32 (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_SEGMENT_64:
-      if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_segment_64 (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_SYMTAB:
-      if (bfd_mach_o_read_symtab (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_symtab (abfd, command))
+       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) != 0)
-       return -1;
+      if (!bfd_mach_o_read_thread (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_LOAD_DYLINKER:
     case BFD_MACH_O_LC_ID_DYLINKER:
-      if (bfd_mach_o_read_dylinker (abfd, command) != 0)
-       return -1;
+    case BFD_MACH_O_LC_DYLD_ENVIRONMENT:
+      if (!bfd_mach_o_read_dylinker (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_LOAD_DYLIB:
+    case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
     case BFD_MACH_O_LC_ID_DYLIB:
     case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
     case BFD_MACH_O_LC_REEXPORT_DYLIB:
     case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
-      if (bfd_mach_o_read_dylib (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_dylib (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_PREBOUND_DYLIB:
-      if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_prebound_dylib (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_LOADFVMLIB:
     case BFD_MACH_O_LC_IDFVMLIB:
-      if (bfd_mach_o_read_fvmlib (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_fvmlib (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_IDENT:
     case BFD_MACH_O_LC_FVMFILE:
@@ -3815,38 +3945,53 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
     case BFD_MACH_O_LC_SUB_LIBRARY:
     case BFD_MACH_O_LC_SUB_CLIENT:
     case BFD_MACH_O_LC_RPATH:
-      if (bfd_mach_o_read_str (abfd, command) != 0)
-        return -1;
+      if (!bfd_mach_o_read_str (abfd, command))
+        return FALSE;
       break;
     case BFD_MACH_O_LC_DYSYMTAB:
-      if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_dysymtab (abfd, command))
+       return FALSE;
       break;
-    case BFD_MACH_O_LC_TWOLEVEL_HINTS:
     case BFD_MACH_O_LC_PREBIND_CKSUM:
+      if (!bfd_mach_o_read_prebind_cksum (abfd, command))
+       return FALSE;
+      break;
+    case BFD_MACH_O_LC_TWOLEVEL_HINTS:
+      if (!bfd_mach_o_read_twolevel_hints (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_UUID:
-      if (bfd_mach_o_read_uuid (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_uuid (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_CODE_SIGNATURE:
     case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
     case BFD_MACH_O_LC_FUNCTION_STARTS:
-      if (bfd_mach_o_read_linkedit (abfd, command) != 0)
-       return -1;
+    case BFD_MACH_O_LC_DATA_IN_CODE:
+    case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS:
+      if (!bfd_mach_o_read_linkedit (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_ENCRYPTION_INFO:
       if (!bfd_mach_o_read_encryption_info (abfd, command))
-       return -1;
+       return FALSE;
       break;
     case BFD_MACH_O_LC_DYLD_INFO:
-      if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
-       return -1;
+      if (!bfd_mach_o_read_dyld_info (abfd, command))
+       return FALSE;
       break;
     case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
     case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
       if (!bfd_mach_o_read_version_min (abfd, command))
-       return -1;
+       return FALSE;
+      break;
+    case BFD_MACH_O_LC_MAIN:
+      if (!bfd_mach_o_read_main (abfd, command))
+       return FALSE;
+      break;
+    case BFD_MACH_O_LC_SOURCE_VERSION:
+      if (!bfd_mach_o_read_source_version (abfd, command))
+       return FALSE;
       break;
     default:
       (*_bfd_error_handler)(_("%B: unknown load command 0x%lx"),
@@ -3854,7 +3999,7 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
       break;
     }
 
-  return 0;
+  return TRUE;
 }
 
 static void
@@ -3917,9 +4062,22 @@ bfd_mach_o_scan_start_address (bfd *abfd)
         cmd = &mdata->commands[i].command.thread;
         break;
       }
+    else if (mdata->commands[i].type == BFD_MACH_O_LC_MAIN
+            && mdata->nsects > 1)
+      {
+       bfd_mach_o_main_command *main_cmd = &mdata->commands[i].command.main;
+       bfd_mach_o_section *text_sect = mdata->sections[0];
+       if (text_sect)
+         {
+           abfd->start_address = main_cmd->entryoff
+             + (text_sect->addr - text_sect->offset);
+           return TRUE;
+         }
+      }
 
+  /* An object file has no start address, so do not fail if not found.  */
   if (cmd == NULL)
-    return FALSE;
+    return TRUE;
 
   /* FIXME: create a subtarget hook ?  */
   for (i = 0; i < cmd->nflavours; i++)
@@ -4054,15 +4212,16 @@ bfd_mach_o_scan (bfd *abfd,
              cur->offset = prev->offset + prev->len;
            }
 
-         if (bfd_mach_o_read_command (abfd, cur) < 0)
+         if (!bfd_mach_o_read_command (abfd, cur))
            return FALSE;
        }
     }
 
-  if (bfd_mach_o_scan_start_address (abfd) < 0)
+  /* Sections should be flatten before scanning start address.  */
+  bfd_mach_o_flatten_sections (abfd);
+  if (!bfd_mach_o_scan_start_address (abfd))
     return FALSE;
 
-  bfd_mach_o_flatten_sections (abfd);
   return TRUE;
 }
 
@@ -4071,7 +4230,7 @@ bfd_mach_o_mkobject_init (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = NULL;
 
-  mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
+  mdata = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
   if (mdata == NULL)
     return FALSE;
   abfd->tdata.mach_o_data = mdata;
@@ -4115,10 +4274,9 @@ bfd_mach_o_header_p (bfd *abfd,
                      bfd_mach_o_filetype filetype,
                      bfd_mach_o_cpu_type cputype)
 {
-  struct bfd_preserve preserve;
   bfd_mach_o_header header;
+  bfd_mach_o_data_struct *mdata;
 
-  preserve.marker = NULL;
   if (!bfd_mach_o_read_header (abfd, &header))
     goto wrong;
 
@@ -4146,6 +4304,15 @@ bfd_mach_o_header_p (bfd *abfd,
       if (header.cputype != cputype)
         goto wrong;
     }
+  else
+    {
+#ifndef BFD64
+      /* Do not recognize 64 architectures if not configured for 64bit targets.
+        This could happen only for generic targets.  */
+      if (mach_o_wide_p (&header))
+        goto wrong;
+#endif
+    }
 
   if (filetype)
     {
@@ -4164,24 +4331,19 @@ bfd_mach_o_header_p (bfd *abfd,
         }
     }
 
-  preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
-  if (preserve.marker == NULL
-      || !bfd_preserve_save (abfd, &preserve))
+  mdata = (bfd_mach_o_data_struct *) bfd_zalloc (abfd, sizeof (*mdata));
+  if (mdata == NULL)
     goto fail;
 
-  if (!bfd_mach_o_scan (abfd, &header,
-                        (bfd_mach_o_data_struct *) preserve.marker))
+  if (!bfd_mach_o_scan (abfd, &header, mdata))
     goto wrong;
 
-  bfd_preserve_finish (abfd, &preserve);
   return abfd->xvec;
 
  wrong:
   bfd_set_error (bfd_error_wrong_format);
 
  fail:
-  if (preserve.marker != NULL)
-    bfd_preserve_restore (abfd, &preserve);
   return NULL;
 }
 
@@ -4197,6 +4359,35 @@ bfd_mach_o_gen_core_p (bfd *abfd)
   return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
 }
 
+/* Return the base address of ABFD, ie the address at which the image is
+   mapped.  The possible initial pagezero is ignored.  */
+
+bfd_vma
+bfd_mach_o_get_base_address (bfd *abfd)
+{
+  bfd_mach_o_data_struct *mdata;
+  unsigned int i;
+
+  /* Check for Mach-O.  */
+  if (!bfd_mach_o_valid (abfd))
+    return 0;
+  mdata = bfd_mach_o_get_data (abfd);
+
+  for (i = 0; i < mdata->header.ncmds; i++)
+    {
+      bfd_mach_o_load_command *cmd = &mdata->commands[i];
+      if ((cmd->type == BFD_MACH_O_LC_SEGMENT
+          || cmd->type == BFD_MACH_O_LC_SEGMENT_64))
+       {
+         struct bfd_mach_o_segment_command *segcmd = &cmd->command.segment;
+
+         if (segcmd->initprot != 0)
+           return segcmd->vmaddr;
+       }
+    }
+  return 0;
+}
+
 typedef struct mach_o_fat_archentry
 {
   unsigned long cputype;
@@ -4282,19 +4473,19 @@ bfd_mach_o_fat_member_init (bfd *abfd,
   if (ap)
     {
       /* Use the architecture name if known.  */
-      abfd->filename = ap->printable_name;
+      abfd->filename = xstrdup (ap->printable_name);
     }
   else
     {
       /* Forge a uniq id.  */
       const size_t namelen = 2 + 8 + 1 + 2 + 8 + 1;
-      char *name = bfd_alloc (abfd, namelen);
+      char *name = xmalloc (namelen);
       snprintf (name, namelen, "0x%lx-0x%lx",
                 entry->cputype, entry->cpusubtype);
       abfd->filename = name;
     }
 
-  areltdata = bfd_zalloc (abfd, sizeof (struct areltdata));
+  areltdata = bfd_zmalloc (sizeof (struct areltdata));
   areltdata->parsed_size = entry->size;
   abfd->arelt_data = areltdata;
   abfd->iostream = NULL;
@@ -4666,7 +4857,7 @@ bfd_mach_o_core_file_failing_command (bfd *abfd)
 {
   unsigned char *buf = NULL;
   unsigned int len = 0;
-  int ret = -1;
+  int ret;
 
   ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
   if (ret < 0)
@@ -4836,7 +5027,7 @@ bfd_mach_o_find_nearest_line (bfd *abfd,
   if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
                                     section, symbols, offset,
                                     filename_ptr, functionname_ptr,
-                                    line_ptr, 0,
+                                    line_ptr, NULL, 0,
                                     &mdata->dwarf2_find_line_info))
     return TRUE;
   return FALSE;
@@ -4864,6 +5055,9 @@ bfd_mach_o_close_and_cleanup (bfd *abfd)
         }
     }
 
+  if (bfd_get_format (abfd) == bfd_archive
+      && abfd->xvec == &mach_o_fat_vec)
+    return TRUE;
   return _bfd_generic_close_and_cleanup (abfd);
 }
 
@@ -4882,7 +5076,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
   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
 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
 
 #define bfd_mach_o_swap_reloc_in NULL
@@ -4894,6 +5088,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #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_BIG_ENDIAN      1
 #define TARGET_ARCHIVE                 0
 #define TARGET_PRIORITY                1
@@ -4902,6 +5097,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #undef TARGET_NAME
 #undef TARGET_STRING
 #undef TARGET_ARCHITECTURE
+#undef TARGET_PAGESIZE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
 #undef TARGET_PRIORITY
@@ -4909,6 +5105,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #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_BIG_ENDIAN      0
 #define TARGET_ARCHIVE                 0
 #define TARGET_PRIORITY                1
@@ -4918,6 +5115,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #undef TARGET_NAME
 #undef TARGET_STRING
 #undef TARGET_ARCHITECTURE
+#undef TARGET_PAGESIZE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
 #undef TARGET_PRIORITY
@@ -4940,6 +5138,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #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_BIG_ENDIAN      1
 #define TARGET_ARCHIVE                 1
 #define TARGET_PRIORITY                0
@@ -4949,6 +5148,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #undef TARGET_NAME
 #undef TARGET_STRING
 #undef TARGET_ARCHITECTURE
+#undef TARGET_PAGESIZE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
 #undef TARGET_PRIORITY
This page took 0.048608 seconds and 4 git commands to generate.