2011-12-16 Shinichiro Hamaji <shinichiro.hamaji@gmail.com>
[deliverable/binutils-gdb.git] / bfd / mach-o.c
index 1daaf985ff76f154ddfdbad0354c7b97fed1b044..dca86013d6419977e9a4688f80183eb1ebb1f324 100644 (file)
@@ -1,12 +1,13 @@
 /* Mach-O support for BFD.
 /* Mach-O support for BFD.
-   Copyright 1999, 2000, 2001, 2002
+   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+   2009, 2010, 2011
    Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
 
+#include "sysdep.h"
 #include "mach-o.h"
 #include "bfd.h"
 #include "mach-o.h"
 #include "bfd.h"
-#include "sysdep.h"
 #include "libbfd.h"
 #include "libiberty.h"
 #include "libbfd.h"
 #include "libiberty.h"
+#include "aout/stab_gnu.h"
+#include "mach-o/reloc.h"
+#include "mach-o/external.h"
 #include <ctype.h>
 
 #include <ctype.h>
 
-#ifndef BFD_IO_FUNCS
-#define BFD_IO_FUNCS 0
-#endif
-
-#define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
-#define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
-#define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
-#define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
-#define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
-#define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
-#define bfd_mach_o_write_armap _bfd_noarchive_write_armap
-#define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
-#define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
-#define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
-#define        bfd_mach_o_close_and_cleanup _bfd_generic_close_and_cleanup
-#define bfd_mach_o_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
-#define bfd_mach_o_new_section_hook _bfd_generic_new_section_hook
-#define bfd_mach_o_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
-#define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
-#define bfd_mach_o_get_lineno _bfd_nosymbols_get_lineno
-#define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
-#define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
-#define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
-#define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
-#define bfd_mach_o_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
-#define bfd_mach_o_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
-#define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
-#define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
-#define bfd_mach_o_bfd_relax_section bfd_generic_relax_section
-#define bfd_mach_o_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
-#define bfd_mach_o_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
-#define bfd_mach_o_bfd_link_add_symbols _bfd_generic_link_add_symbols
-#define bfd_mach_o_bfd_link_just_syms _bfd_generic_link_just_syms
-#define bfd_mach_o_bfd_final_link _bfd_generic_final_link
-#define bfd_mach_o_bfd_link_split_section _bfd_generic_link_split_section
-#define bfd_mach_o_set_arch_mach bfd_default_set_arch_mach
-#define bfd_mach_o_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
-#define bfd_mach_o_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
-#define bfd_mach_o_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
-#define bfd_mach_o_get_section_contents _bfd_generic_get_section_contents
-#define bfd_mach_o_set_section_contents _bfd_generic_set_section_contents
-#define bfd_mach_o_bfd_gc_sections bfd_generic_gc_sections
-#define bfd_mach_o_bfd_merge_sections bfd_generic_merge_sections
-#define bfd_mach_o_bfd_discard_group bfd_generic_discard_group
-
-static boolean  bfd_mach_o_bfd_copy_private_symbol_data  PARAMS ((bfd *, asymbol *, bfd *, asymbol *));
-static boolean  bfd_mach_o_bfd_copy_private_section_data PARAMS ((bfd *, asection *, bfd *, asection *));
-static boolean  bfd_mach_o_bfd_copy_private_bfd_data PARAMS ((bfd *, bfd *));
-static long     bfd_mach_o_count_symbols             PARAMS ((bfd *));
-static long     bfd_mach_o_get_symtab_upper_bound    PARAMS ((bfd *));
-static long     bfd_mach_o_get_symtab                PARAMS ((bfd *, asymbol **));
-static void     bfd_mach_o_get_symbol_info           PARAMS ((bfd *, asymbol *, symbol_info *));
-static void     bfd_mach_o_print_symbol              PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
-static void     bfd_mach_o_convert_architecture      PARAMS ((bfd_mach_o_cpu_type, bfd_mach_o_cpu_subtype, enum bfd_architecture *, unsigned long *));
-static boolean  bfd_mach_o_write_contents            PARAMS ((bfd *));
-static int      bfd_mach_o_sizeof_headers            PARAMS ((bfd *, boolean));
-static asymbol *   bfd_mach_o_make_empty_symbol      PARAMS ((bfd *));
-static int      bfd_mach_o_write_header              PARAMS ((bfd *, bfd_mach_o_header *));
-static int      bfd_mach_o_read_header               PARAMS ((bfd *, bfd_mach_o_header *));
-static asection *  bfd_mach_o_make_bfd_section       PARAMS ((bfd *, bfd_mach_o_section *));
-static int      bfd_mach_o_scan_read_section         PARAMS ((bfd *, bfd_mach_o_section *, bfd_vma));
-static int      bfd_mach_o_scan_write_section        PARAMS ((bfd *, bfd_mach_o_section *, bfd_vma));
-static int      bfd_mach_o_scan_write_symtab_symbols PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_write_thread         PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_dylinker        PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_dylib           PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_prebound_dylib  PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_thread          PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_write_symtab         PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_dysymtab        PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_symtab          PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_segment         PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_write_segment        PARAMS ((bfd *, bfd_mach_o_load_command *));
-static int      bfd_mach_o_scan_read_command         PARAMS ((bfd *, bfd_mach_o_load_command *));
-static void     bfd_mach_o_flatten_sections          PARAMS ((bfd *));
-static const char * bfd_mach_o_i386_flavour_string   PARAMS ((unsigned int));
-static const char * bfd_mach_o_ppc_flavour_string    PARAMS ((unsigned int));
-
-/* The flags field of a section structure is separated into two parts a section
-   type and section attributes.  The section types are mutually exclusive (it
-   can only have one type) but the section attributes are not (it may have more
-   than one attribute).  */
-
-#define SECTION_TYPE             0x000000ff     /* 256 section types.  */
-#define SECTION_ATTRIBUTES       0xffffff00     /*  24 section attributes.  */
-
-/* Constants for the section attributes part of the flags field of a section
-   structure.  */
-
-#define SECTION_ATTRIBUTES_USR   0xff000000     /* User-settable attributes.  */
-#define S_ATTR_PURE_INSTRUCTIONS 0x80000000     /* Section contains only true machine instructions.  */
-#define SECTION_ATTRIBUTES_SYS   0x00ffff00     /* System setable attributes.  */
-#define S_ATTR_SOME_INSTRUCTIONS 0x00000400     /* Section contains some machine instructions.  */
-#define S_ATTR_EXT_RELOC         0x00000200     /* Section has external relocation entries.  */
-#define S_ATTR_LOC_RELOC         0x00000100     /* Section has local relocation entries.  */
-
-#define N_STAB 0xe0
-#define N_TYPE 0x1e
-#define N_EXT  0x01
-#define N_UNDF 0x0
-#define N_ABS  0x2
-#define N_SECT 0xe
-#define N_INDR 0xa
-
-boolean
-bfd_mach_o_valid (abfd)
-     bfd *abfd;
+#define bfd_mach_o_object_p bfd_mach_o_gen_object_p
+#define bfd_mach_o_core_p bfd_mach_o_gen_core_p
+#define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
+
+#define FILE_ALIGN(off, algn) \
+  (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
+
+unsigned int
+bfd_mach_o_version (bfd *abfd)
+{
+  bfd_mach_o_data_struct *mdata = NULL;
+
+  BFD_ASSERT (bfd_mach_o_valid (abfd));
+  mdata = bfd_mach_o_get_data (abfd);
+
+  return mdata->header.version;
+}
+
+bfd_boolean
+bfd_mach_o_valid (bfd *abfd)
 {
   if (abfd == NULL || abfd->xvec == NULL)
 {
   if (abfd == NULL || abfd->xvec == NULL)
-    return 0;
+    return FALSE;
 
 
-  if (! ((abfd->xvec == &mach_o_be_vec)
-        || (abfd->xvec == &mach_o_le_vec)
-        || (abfd->xvec == &mach_o_fat_vec)))
-    return 0;
+  if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
+    return FALSE;
+
+  if (bfd_mach_o_get_data (abfd) == NULL)
+    return FALSE;
+  return TRUE;
+}
+
+static INLINE bfd_boolean
+mach_o_wide_p (bfd_mach_o_header *header)
+{
+  switch (header->version)
+    {
+    case 1:
+      return FALSE;
+    case 2:
+      return TRUE;
+    default:
+      BFD_FAIL ();
+      return FALSE;
+    }
+}
+
+static INLINE bfd_boolean
+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.  */
+
+/* __TEXT Segment.  */
+static const mach_o_section_name_xlat text_section_names_xlat[] =
+  {
+    {  ".text",                                "__text",       
+       SEC_CODE | SEC_LOAD,                    BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,    0},
+    {  ".const",                               "__const",
+       SEC_READONLY | SEC_DATA | SEC_LOAD,     BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,                 0},
+    {  ".static_const",                        "__static_const",
+       SEC_READONLY | SEC_DATA | SEC_LOAD,     BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,                 0},
+    {  ".cstring",                             "__cstring",
+       SEC_READONLY | SEC_DATA | SEC_LOAD | SEC_MERGE | SEC_STRINGS,
+                                               BFD_MACH_O_S_CSTRING_LITERALS,
+       BFD_MACH_O_S_ATTR_NONE,                 0},
+    {  ".literal4",                            "__literal4",
+       SEC_READONLY | SEC_DATA | SEC_LOAD,     BFD_MACH_O_S_4BYTE_LITERALS,
+       BFD_MACH_O_S_ATTR_NONE,                 2},
+    {  ".literal8",                            "__literal8",
+       SEC_READONLY | SEC_DATA | SEC_LOAD,     BFD_MACH_O_S_8BYTE_LITERALS,
+       BFD_MACH_O_S_ATTR_NONE,                 3},
+    {  ".literal16",                           "__literal16",
+       SEC_READONLY | SEC_DATA | SEC_LOAD,     BFD_MACH_O_S_16BYTE_LITERALS,
+       BFD_MACH_O_S_ATTR_NONE,                 4},
+    {  ".constructor",                         "__constructor",
+       SEC_CODE | SEC_LOAD,                    BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,                 0},
+    {  ".destructor",                          "__destructor",
+       SEC_CODE | SEC_LOAD,                    BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,                 0},
+    {  ".eh_frame",                            "__eh_frame",
+       SEC_READONLY | SEC_LOAD,                BFD_MACH_O_S_COALESCED,
+       BFD_MACH_O_S_ATTR_LIVE_SUPPORT
+       | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
+       | BFD_MACH_O_S_ATTR_NO_TOC,             3},
+    { NULL, NULL, 0, 0, 0, 0}
+  };
+
+/* __DATA Segment.  */
+static const mach_o_section_name_xlat data_section_names_xlat[] =
+  {
+    {  ".data",                        "__data",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,         0},
+    {  ".bss",                         "__bss",
+       SEC_NO_FLAGS,                   BFD_MACH_O_S_ZEROFILL,
+       BFD_MACH_O_S_ATTR_NONE,         0},
+    {  ".const_data",                  "__const",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,         0},
+    {  ".static_data",                 "__static_data",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,         0},
+    {  ".mod_init_func",               "__mod_init_func",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS,
+       BFD_MACH_O_S_ATTR_NONE,         2},
+    {  ".mod_term_func",               "__mod_term_func",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS,
+       BFD_MACH_O_S_ATTR_NONE,         2},
+    {  ".dyld",                        "__dyld",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,         0},
+    {  ".cfstring",                    "__cfstring",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NONE,         2},
+    { NULL, NULL, 0, 0, 0, 0}
+  };
+
+/* __DWARF Segment.  */
+static const mach_o_section_name_xlat dwarf_section_names_xlat[] =
+  {
+    {  ".debug_frame",                 "__debug_frame",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_info",                  "__debug_info",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_abbrev",                "__debug_abbrev",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_aranges",               "__debug_aranges",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_macinfo",               "__debug_macinfo",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_line",                  "__debug_line",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_loc",                   "__debug_loc",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_pubnames",              "__debug_pubnames",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_pubtypes",              "__debug_pubtypes",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_str",                   "__debug_str",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_ranges",                "__debug_ranges",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    {  ".debug_macro",                 "__debug_macro",
+       SEC_DEBUGGING,                  BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_DEBUG,        0},
+    { NULL, NULL, 0, 0, 0, 0}
+  };
+
+/* __OBJC Segment.  */
+static const mach_o_section_name_xlat objc_section_names_xlat[] =
+  {
+    {  ".objc_class",                  "__class",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_meta_class",             "__meta_class",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_cat_cls_meth",           "__cat_cls_meth",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_cat_inst_meth",          "__cat_inst_meth",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_protocol",               "__protocol",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_string_object",          "__string_object",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_cls_meth",               "__cls_meth",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_inst_meth",              "__inst_meth",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_cls_refs",               "__cls_refs",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LITERAL_POINTERS,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_message_refs",           "__message_refs",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LITERAL_POINTERS,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_symbols",                "__symbols",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_category",               "__category",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_class_vars",             "__class_vars",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_instance_vars",          "__instance_vars",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_module_info",            "__module_info",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_selector_strs",          "__selector_strs",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_CSTRING_LITERALS,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_image_info",             "__image_info",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc_selector_fixup",         "__sel_fixup",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    /* Objc V1 */
+    {  ".objc1_class_ext",             "__class_ext",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc1_property_list",         "__property",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    {  ".objc1_protocol_ext",          "__protocol_ext",
+       SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_REGULAR,
+       BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+    { NULL, NULL, 0, 0, 0, 0}
+  };
+
+static const mach_o_segment_name_xlat segsec_names_xlat[] =
+  {
+    { "__TEXT", text_section_names_xlat },
+    { "__DATA", data_section_names_xlat },
+    { "__DWARF", dwarf_section_names_xlat },
+    { "__OBJC", objc_section_names_xlat },
+    { NULL, NULL }
+  };
+
+/* For both cases bfd-name => mach-o name and vice versa, the specific target
+   is checked before the generic.  This allows a target (e.g. ppc for cstring)
+   to override the generic definition with a more specific one.  */
+
+/* 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 *
+bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
+                                      const char *sectname)
+{
+  const struct mach_o_segment_name_xlat *seg;
+  const mach_o_section_name_xlat *sec;
+  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+
+  /* First try any target-specific translations defined...  */
+  if (bed->segsec_names_xlat)
+    for (seg = bed->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,
+                      BFD_MACH_O_SECTNAME_SIZE) == 0)
+           return sec;
+
+  /* ... and then the Mach-O generic ones.  */
+  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,
+                    BFD_MACH_O_SECTNAME_SIZE) == 0)
+          return sec;
+
+  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 
+   Mach-O equivalent.  Otherwise return NULL.  */
+
+const mach_o_section_name_xlat *
+bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name,
+                                     const char **segname)
+{
+  const struct mach_o_segment_name_xlat *seg;
+  const mach_o_section_name_xlat *sec;
+  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+  *segname = NULL;
+
+  if (bfd_name[0] != '.')
+    return NULL;
+
+  /* First try any target-specific translations defined...  */
+  if (bed->segsec_names_xlat)
+    for (seg = bed->segsec_names_xlat; seg->segname; seg++)
+      for (sec = seg->sections; sec->bfd_name; sec++)
+       if (strcmp (bfd_name, sec->bfd_name) == 0)
+         {
+           *segname = seg->segname;
+           return sec;
+         }
+
+  /* ... and then the Mach-O generic ones.  */
+  for (seg = segsec_names_xlat; seg->segname; seg++)
+    for (sec = seg->sections; sec->bfd_name; sec++)
+      if (strcmp (bfd_name, sec->bfd_name) == 0)
+       {
+         *segname = seg->segname;
+         return sec;
+       }
+
+  return NULL;  
+}
+
+/* Convert Mach-O section name to BFD.
+
+   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.
+
+   SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
+   terminated if the name length is exactly 16 bytes - but must be if the name
+   length is less than 16 characters).  */
+
+void
+bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
+                                       const char *secname, const char **name,
+                                       flagword *flags)
+{
+  const mach_o_section_name_xlat *xlat;
+  char *res;
+  unsigned int len;
+  const char *pfx = "";
+
+  *name = NULL;
+  *flags = SEC_NO_FLAGS;
+
+  /* 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)
+    {
+      len = strlen (xlat->bfd_name);
+      res = bfd_alloc (abfd, len+1);
+      if (res == NULL)
+       return;
+      memcpy (res, xlat->bfd_name, len+1);
+      *name = res;
+      *flags = xlat->bfd_flags;
+      return;
+    }
+
+  /* ... else we make up a bfd name from the segment concatenated with the
+     section.  */
+
+  len = 16 + 1 + 16 + 1;
+
+  /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
+     with an underscore.  */
+  if (segname[0] != '_')
+    {
+      static const char seg_pfx[] = "LC_SEGMENT.";
+
+      pfx = seg_pfx;
+      len += sizeof (seg_pfx) - 1;
+    }
+
+  res = bfd_alloc (abfd, len);
+  if (res == NULL)
+    return;
+  snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname);
+  *name = res;
+}
+
+/* Convert a bfd section name to a Mach-O segment + section name.
+
+   If the name is a canonical one for which we have a Darwin match
+   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 
+   "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL.  */
+
+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)
+{
+  const mach_o_section_name_xlat *xlat;
+  const char *name = bfd_get_section_name (abfd, sect);
+  const char *segname;
+  const char *dot;
+  unsigned int len;
+  unsigned int seglen;
+  unsigned int seclen;
+
+  memset (section->segname, 0, BFD_MACH_O_SEGNAME_SIZE + 1);
+  memset (section->sectname, 0, BFD_MACH_O_SECTNAME_SIZE + 1);
+
+  /* See if is a canonical name ... */
+  xlat = bfd_mach_o_section_data_for_bfd_name (abfd, name, &segname);
+  if (xlat)
+    {
+      strcpy (section->segname, segname);
+      strcpy (section->sectname, xlat->mach_o_name);
+      return xlat;
+    }
+
+  /* .. else we convert our constructed one back to Mach-O.
+     Strip LC_SEGMENT. prefix, if present.  */
+  if (strncmp (name, "LC_SEGMENT.", 11) == 0)
+    name += 11;
+
+  /* Find a dot.  */
+  dot = strchr (name, '.');
+  len = strlen (name);
+
+  /* Try to split name into segment and section names.  */
+  if (dot && dot != name)
+    {
+      seglen = dot - name;
+      seclen = len - (dot + 1 - name);
+
+      if (seglen < 16 && seclen < 16)
+        {
+          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
+     into dots.  */
+  if (dot && dot == name)
+    return NULL;
+
+  /* Just duplicate the name into both segment and section.  */
+  if (len > 16)
+    len = 16;
+  memcpy (section->segname, name, len);
+  section->segname[len] = 0;
+  memcpy (section->sectname, name, len);
+  section->sectname[len] = 0;
+  return NULL;
+}
 
 
-  if (abfd->tdata.mach_o_data == NULL)
+/* Return the size of an entry for section SEC.
+   Must be called only for symbol pointer section and symbol stubs
+   sections.  */
+
+unsigned int
+bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
+{
+  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:
+      return bfd_mach_o_wide_p (abfd) ? 8 : 4;
+    case BFD_MACH_O_S_SYMBOL_STUBS:
+      return sec->reserved2;
+    default:
+      BFD_FAIL ();
+      return 0;
+    }
+}
+
+/* Return the number of indirect symbols for a section.
+   Must be called only for symbol pointer section and symbol stubs
+   sections.  */
+
+unsigned int
+bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
+{
+  unsigned int elsz;
+
+  elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
+  if (elsz == 0)
     return 0;
     return 0;
-  return 1;
+  else
+    return sec->size / elsz;
 }
 
 }
 
+
 /* 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.  */
 
-static boolean
-bfd_mach_o_bfd_copy_private_symbol_data (ibfd, isymbol, obfd, osymbol)
-     bfd *ibfd ATTRIBUTE_UNUSED;
-     asymbol *isymbol ATTRIBUTE_UNUSED;
-     bfd *obfd ATTRIBUTE_UNUSED;
-     asymbol *osymbol ATTRIBUTE_UNUSED;
+bfd_boolean
+bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
+                                        asymbol *isymbol ATTRIBUTE_UNUSED,
+                                        bfd *obfd ATTRIBUTE_UNUSED,
+                                        asymbol *osymbol ATTRIBUTE_UNUSED)
 {
 {
-  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.  */
 
-static boolean
-bfd_mach_o_bfd_copy_private_section_data (ibfd, isection, obfd, osection)
-     bfd *ibfd ATTRIBUTE_UNUSED;
-     asection *isection ATTRIBUTE_UNUSED;
-     bfd *obfd ATTRIBUTE_UNUSED;
-     asection *osection ATTRIBUTE_UNUSED;
+bfd_boolean
+bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
+                                         asection *isection,
+                                         bfd *obfd ATTRIBUTE_UNUSED,
+                                         asection *osection)
 {
 {
-  return true;
+  if (osection->used_by_bfd == NULL)
+    osection->used_by_bfd = isection->used_by_bfd;
+  else 
+    if (isection->used_by_bfd != NULL)
+      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;
+
+  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.  */
 
-static boolean
-bfd_mach_o_bfd_copy_private_bfd_data (ibfd, obfd)
-     bfd *ibfd;
-     bfd *obfd;
+bfd_boolean
+bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
 {
 {
+  if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
+      || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
+    return TRUE;
+
   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));
 
-  obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
-  obfd->tdata.mach_o_data->ibfd = ibfd;
-  return true;
+  /* FIXME: copy commands.  */
+
+  return TRUE;
 }
 
 }
 
-static long
-bfd_mach_o_count_symbols (abfd)
-     bfd *abfd;
+/* 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)
 {
 {
-  bfd_mach_o_data_struct *mdata = NULL;
-  long nsyms = 0;
-  unsigned long i;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
 
 
-  BFD_ASSERT (bfd_mach_o_valid (abfd));
-  mdata = abfd->tdata.mach_o_data;
+  if (!mdata)
+    return FALSE;
 
 
-  for (i = 0; i < mdata->header.ncmds; i++)
-    if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
-      {
-       bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
-       nsyms += sym->nsyms;
-      }
-
-  return nsyms;
+  mdata->header.flags = flags;
+  return TRUE;
 }
 
 }
 
+/* Count the total number of symbols.  */
+
 static long
 static long
-bfd_mach_o_get_symtab_upper_bound (abfd)
-     bfd *abfd;
+bfd_mach_o_count_symbols (bfd *abfd)
 {
 {
-  long nsyms = bfd_mach_o_count_symbols (abfd);
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
 
 
-  if (nsyms < 0)
-    return nsyms;
+  if (mdata->symtab == NULL)
+    return 0;
+  return mdata->symtab->nsyms;
+}
+
+long
+bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
+{
+  long nsyms = bfd_mach_o_count_symbols (abfd);
 
   return ((nsyms + 1) * sizeof (asymbol *));
 }
 
 
   return ((nsyms + 1) * sizeof (asymbol *));
 }
 
-static long
-bfd_mach_o_get_symtab (abfd, alocation)
-     bfd *abfd;
-     asymbol **alocation;
+long
+bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
 {
 {
-  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   long nsyms = bfd_mach_o_count_symbols (abfd);
   long nsyms = bfd_mach_o_count_symbols (abfd);
-  asymbol **csym = alocation;
-  unsigned long i, j;
+  bfd_mach_o_symtab_command *sym = mdata->symtab;
+  unsigned long j;
 
   if (nsyms < 0)
     return nsyms;
 
 
   if (nsyms < 0)
     return nsyms;
 
-  for (i = 0; i < mdata->header.ncmds; i++)
+  if (nsyms == 0)
     {
     {
-      if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
-       {
-         bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
+      /* Do not try to read symbols if there are none.  */
+      alocation[0] = NULL;
+      return 0;
+    }
 
 
-         if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
-           {
-             fprintf (stderr, "bfd_mach_o_get_symtab: unable to load symbols for section %lu\n", i);
-             return 0;
-           }
+  if (!bfd_mach_o_read_symtab_symbols (abfd))
+    {
+      (*_bfd_error_handler)
+        (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
+      return 0;
+    }
 
 
-         BFD_ASSERT (sym->symbols != NULL);
+  BFD_ASSERT (sym->symbols != NULL);
 
 
-         for (j = 0; j < sym->nsyms; j++)
-           {
-             BFD_ASSERT (csym < (alocation + nsyms));
-             *csym++ = &sym->symbols[j];
-           }
-       }
-    }
+  for (j = 0; j < sym->nsyms; j++)
+    alocation[j] = &sym->symbols[j].symbol;
 
 
-  *csym++ = NULL;
+  alocation[j] = NULL;
 
   return nsyms;
 }
 
 
   return nsyms;
 }
 
-static void
-bfd_mach_o_get_symbol_info (abfd, symbol, ret)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     asymbol *symbol;
-     symbol_info *ret;
+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)
+{
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
+  bfd_mach_o_symtab_command *symtab = mdata->symtab;
+  asymbol *s;
+  unsigned long count, i, j, n;
+  size_t size;
+  char *names;
+  char *nul_name;
+
+  *ret = NULL;
+
+  if (dysymtab == NULL || symtab == NULL || symtab->symbols == NULL)
+    return 0;
+
+  if (dysymtab->nindirectsyms == 0)
+    return 0;
+
+  count = dysymtab->nindirectsyms;
+  size = count * sizeof (asymbol) + 1;
+
+  for (j = 0; j < count; j++)
+    {
+      unsigned int isym = dysymtab->indirect_syms[j];
+              
+      if (isym < symtab->nsyms && symtab->symbols[isym].symbol.name)
+        size += strlen (symtab->symbols[isym].symbol.name) + sizeof ("$stub");
+    }
+
+  s = *ret = (asymbol *) bfd_malloc (size);
+  if (s == NULL)
+    return -1;
+  names = (char *) (s + count);
+  nul_name = names;
+  *names++ = 0;
+  
+  n = 0;
+  for (i = 0; i < mdata->nsects; i++)
+    {
+      bfd_mach_o_section *sec = mdata->sections[i];
+      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:
+        case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
+        case BFD_MACH_O_S_SYMBOL_STUBS:
+          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);
+          for (j = first; j < last; j++)
+            {
+              unsigned int isym = dysymtab->indirect_syms[j];
+
+              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;
+
+                  s->name = names;
+                  len = strlen (sym);
+                  memcpy (names, sym, len);
+                  names += len;
+                  memcpy (names, "$stub", sizeof ("$stub"));
+                  names += sizeof ("$stub");
+                }
+              else
+                s->name = nul_name;
+
+              addr += entry_size;
+              s++;
+              n++;
+            }
+          break;
+        default:
+          break;
+        }
+    }
+
+  return n;
+}
+
+void
+bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
+                           asymbol *symbol,
+                           symbol_info *ret)
 {
   bfd_symbol_info (symbol, ret);
 }
 
 {
   bfd_symbol_info (symbol, ret);
 }
 
-static void
-bfd_mach_o_print_symbol (abfd, afile, symbol, how)
-     bfd *abfd;
-     PTR afile;
-     asymbol *symbol;
-     bfd_print_symbol_type how;
+void
+bfd_mach_o_print_symbol (bfd *abfd,
+                        void * afile,
+                        asymbol *symbol,
+                        bfd_print_symbol_type how)
 {
   FILE *file = (FILE *) afile;
 {
   FILE *file = (FILE *) afile;
+  const char *name;
+  bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
 
   switch (how)
     {
 
   switch (how)
     {
@@ -285,17 +769,50 @@ bfd_mach_o_print_symbol (abfd, afile, symbol, how)
       fprintf (file, "%s", symbol->name);
       break;
     default:
       fprintf (file, "%s", symbol->name);
       break;
     default:
-      bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
-      fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
+      bfd_print_symbol_vandf (abfd, (void *) file, symbol);
+      if (asym->n_type & BFD_MACH_O_N_STAB)
+       name = bfd_get_stab_name (asym->n_type);
+      else
+       switch (asym->n_type & BFD_MACH_O_N_TYPE)
+         {
+         case BFD_MACH_O_N_UNDF:
+            if (symbol->value == 0)
+              name = "UND";
+            else
+              name = "COM";
+           break;
+         case BFD_MACH_O_N_ABS:
+           name = "ABS";
+           break;
+         case BFD_MACH_O_N_INDR:
+           name = "INDR";
+           break;
+         case BFD_MACH_O_N_PBUD:
+           name = "PBUD";
+           break;
+         case BFD_MACH_O_N_SECT:
+           name = "SECT";
+           break;
+         default:
+           name = "???";
+           break;
+         }
+      if (name == NULL)
+       name = "";
+      fprintf (file, " %02x %-6s %02x %04x",
+               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);
+      fprintf (file, " %s", symbol->name);
     }
 }
 
 static void
     }
 }
 
 static void
-bfd_mach_o_convert_architecture (mtype, msubtype, type, subtype)
-     bfd_mach_o_cpu_type mtype;
-     bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED;
-     enum bfd_architecture *type;
-     unsigned long *subtype;
+bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
+                                bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
+                                enum bfd_architecture *type,
+                                unsigned long *subtype)
 {
   *subtype = bfd_arch_unknown;
 
 {
   *subtype = bfd_arch_unknown;
 
@@ -303,113 +820,715 @@ bfd_mach_o_convert_architecture (mtype, msubtype, type, subtype)
     {
     case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
     case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
     {
     case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
     case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
-    case BFD_MACH_O_CPU_TYPE_I386: *type = bfd_arch_i386; break;
+    case BFD_MACH_O_CPU_TYPE_I386:
+      *type = bfd_arch_i386;
+      *subtype = bfd_mach_i386_i386;
+      break;
+    case BFD_MACH_O_CPU_TYPE_X86_64:
+      *type = bfd_arch_i386;
+      *subtype = bfd_mach_x86_64;
+      break;
     case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
     case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
     case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
     case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
     case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
     case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
     case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
     case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
     case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
     case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
-    case BFD_MACH_O_CPU_TYPE_SPARC: *type = bfd_arch_sparc; 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_I860: *type = bfd_arch_i860; break;
     case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
-    case BFD_MACH_O_CPU_TYPE_POWERPC: *type = bfd_arch_powerpc; break;
-    default: *type = bfd_arch_unknown; break;
+    case BFD_MACH_O_CPU_TYPE_POWERPC:
+      *type = bfd_arch_powerpc;
+      *subtype = bfd_mach_ppc;
+      break;
+    case BFD_MACH_O_CPU_TYPE_POWERPC_64:
+      *type = bfd_arch_powerpc;
+      *subtype = bfd_mach_ppc64;
+      break;
+    default:
+      *type = bfd_arch_unknown;
+      break;
     }
     }
+}
+
+static bfd_boolean
+bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
+{
+  struct mach_o_header_external raw;
+  unsigned int size;
+
+  size = mach_o_wide_p (header) ?
+    BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
+
+  bfd_h_put_32 (abfd, header->magic, raw.magic);
+  bfd_h_put_32 (abfd, header->cputype, raw.cputype);
+  bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
+  bfd_h_put_32 (abfd, header->filetype, raw.filetype);
+  bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
+  bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
+  bfd_h_put_32 (abfd, header->flags, raw.flags);
+
+  if (mach_o_wide_p (header))
+    bfd_h_put_32 (abfd, header->reserved, raw.reserved);
+
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0
+      || bfd_bwrite (&raw, size, abfd) != size)
+    return FALSE;
+
+  return TRUE;
+}
+
+static int
+bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_thread_command *cmd = &command->command.thread;
+  unsigned int i;
+  struct mach_o_thread_command_external raw;
+  unsigned int offset;
 
 
-  switch (*type)
+  BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
+             || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
+
+  offset = 8;
+  for (i = 0; i < cmd->nflavours; i++)
     {
     {
-    case bfd_arch_i386: *subtype = bfd_mach_i386_i386; break;
-    case bfd_arch_sparc: *subtype = bfd_mach_sparc; break;
-    default:
-      *subtype = bfd_arch_unknown;
+      BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
+      BFD_ASSERT (cmd->flavours[i].offset ==
+                  (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_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+       return -1;
+
+      offset += cmd->flavours[i].size + sizeof (raw);
     }
     }
+
+  return 0;
 }
 
 }
 
-static boolean
-bfd_mach_o_write_contents (abfd)
-     bfd *abfd;
+long
+bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
+                                  asection *asect)
 {
 {
+  return (asect->reloc_count + 1) * sizeof (arelent *);
+}
+
+static int
+bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
+                                   struct mach_o_reloc_info_external *raw,
+                                   arelent *res, asymbol **syms)
+{
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+  bfd_mach_o_reloc_info reloc;
+  bfd_vma addr;
+  bfd_vma symnum;
+  asymbol **sym;
+
+  addr = bfd_get_32 (abfd, raw->r_address);
+  symnum = bfd_get_32 (abfd, raw->r_symbolnum);
+  
+  if (addr & BFD_MACH_O_SR_SCATTERED)
+    {
+      unsigned int j;
+
+      /* Scattered relocation.
+         Extract section and offset from r_value.  */
+      res->sym_ptr_ptr = NULL;
+      res->addend = 0;
+      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;
+            }
+        }
+      res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
+      reloc.r_type = BFD_MACH_O_GET_SR_TYPE (addr);
+      reloc.r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
+      reloc.r_pcrel = addr & BFD_MACH_O_SR_PCREL;
+      reloc.r_scattered = 1;
+    }
+  else
+    {
+      unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
+      res->addend = 0;
+      res->address = addr;
+      if (symnum & BFD_MACH_O_R_EXTERN)
+        {
+          sym = syms + num;
+          reloc.r_extern = 1;
+        }
+      else
+        {
+          BFD_ASSERT (num != 0);
+          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
+             bfd conventio, substract the section address.
+             Use the address from the header, so that the user can modify
+             the vma of the section.  */
+          res->addend = -mdata->sections[num - 1]->addr;
+          reloc.r_extern = 0;
+        }
+      res->sym_ptr_ptr = sym;
+      reloc.r_type = BFD_MACH_O_GET_R_TYPE (symnum);
+      reloc.r_length = BFD_MACH_O_GET_R_LENGTH (symnum);
+      reloc.r_pcrel = (symnum & BFD_MACH_O_R_PCREL) ? 1 : 0;
+      reloc.r_scattered = 0;
+    }
+  
+  if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
+    return -1;
+  return 0;
+}
+
+static int
+bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
+                                unsigned long count,
+                                arelent *res, asymbol **syms)
+{
+  unsigned long i;
+  struct mach_o_reloc_info_external *native_relocs;
+  bfd_size_type native_size;
+
+  /* Allocate and read relocs.  */
+  native_size = count * BFD_MACH_O_RELENT_SIZE;
+  native_relocs =
+    (struct mach_o_reloc_info_external *) bfd_malloc (native_size);
+  if (native_relocs == NULL)
+    return -1;
+
+  if (bfd_seek (abfd, filepos, SEEK_SET) != 0
+      || bfd_bread (native_relocs, native_size, abfd) != native_size)
+    goto err;
+
+  for (i = 0; i < count; i++)
+    {
+      if (bfd_mach_o_canonicalize_one_reloc (abfd, &native_relocs[i],
+                                             &res[i], syms) < 0)
+        goto err;
+    }
+  free (native_relocs);
+  return i;
+ err:
+  free (native_relocs);
+  return -1;
+}
+
+long
+bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
+                               arelent **rels, asymbol **syms)
+{
+  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+  unsigned long i;
+  arelent *res;
+
+  if (asect->reloc_count == 0)
+    return 0;
+
+  /* No need to go further if we don't know how to read relocs.  */
+  if (bed->_bfd_mach_o_swap_reloc_in == NULL)
+    return 0;
+
+  if (asect->relocation == NULL)
+    {
+      res = bfd_malloc (asect->reloc_count * sizeof (arelent));
+      if (res == NULL)
+        return -1;
+
+      if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
+                                          asect->reloc_count, res, syms) < 0)
+        {
+          free (res);
+          return -1;
+        }
+      asect->relocation = res;
+    }
+
+  res = asect->relocation;
+  for (i = 0; i < asect->reloc_count; i++)
+    rels[i] = &res[i];
+  rels[i] = NULL;
+
+  return i;
+}
+
+long
+bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
+{
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+
+  if (mdata->dysymtab == NULL)
+    return 1;
+  return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel + 1)
+    * sizeof (arelent *);
+}
+
+long
+bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
+                                       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_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+  unsigned long i;
+  arelent *res;
+
+  if (dysymtab == NULL)
+    return 0;
+  if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
+    return 0;
+
+  /* No need to go further if we don't know how to read relocs.  */
+  if (bed->_bfd_mach_o_swap_reloc_in == NULL)
+    return 0;
+
+  if (mdata->dyn_reloc_cache == NULL)
+    {
+      res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel)
+                        * sizeof (arelent));
+      if (res == NULL)
+        return -1;
+
+      if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
+                                          dysymtab->nextrel, res, syms) < 0)
+        {
+          free (res);
+          return -1;
+        }
+
+      if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
+                                          dysymtab->nlocrel,
+                                          res + dysymtab->nextrel, syms) < 0)
+        {
+          free (res);
+          return -1;
+        }
+
+      mdata->dyn_reloc_cache = res;
+    }
+
+  res = mdata->dyn_reloc_cache;
+  for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
+    rels[i] = &res[i];
+  rels[i] = NULL;
+  return i;
+}
+
+static bfd_boolean
+bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
+{
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   unsigned int i;
   unsigned int i;
-  asection *s;
+  arelent **entries;
+  asection *sec;
+  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+
+  sec = section->bfdsection;
+  if (sec->reloc_count == 0)
+    return TRUE;
+
+  if (bed->_bfd_mach_o_swap_reloc_out == NULL)
+    return TRUE;
+
+  /* Allocate relocation room.  */
+  mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
+  section->nreloc = sec->reloc_count;
+  sec->rel_filepos = mdata->filelen;
+  section->reloff = sec->rel_filepos;
+  mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
+
+  if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
+    return FALSE;
+
+  /* Convert and write.  */
+  entries = section->bfdsection->orelocation;
+  for (i = 0; i < section->nreloc; i++)
+    {
+      arelent *rel = entries[i];
+      struct mach_o_reloc_info_external raw;
+      bfd_mach_o_reloc_info info, *pinfo = &info;
+
+      /* Convert relocation to an intermediate representation.  */
+      if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
+        return FALSE;
+
+      /* 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);
+        }
+      else
+        {
+          unsigned long v;
+
+          bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
+          v = BFD_MACH_O_SET_R_SYMBOLNUM (pinfo->r_value)
+            | (pinfo->r_pcrel ? BFD_MACH_O_R_PCREL : 0)
+            | BFD_MACH_O_SET_R_LENGTH (pinfo->r_length)
+            | (pinfo->r_extern ? BFD_MACH_O_R_EXTERN : 0)
+            | BFD_MACH_O_SET_R_TYPE (pinfo->r_type);
+          bfd_put_32 (abfd, v, raw.r_symbolnum);
+        }
+
+      if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
+          != BFD_MACH_O_RELENT_SIZE)
+        return FALSE;
+    }
+  return TRUE;
+}
+
+static int
+bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
+{
+  struct mach_o_section_32_external raw;
+
+  memcpy (raw.sectname, section->sectname, 16);
+  memcpy (raw.segname, section->segname, 16);
+  bfd_h_put_32 (abfd, section->addr, raw.addr);
+  bfd_h_put_32 (abfd, section->size, raw.size);
+  bfd_h_put_32 (abfd, section->offset, raw.offset);
+  bfd_h_put_32 (abfd, section->align, raw.align);
+  bfd_h_put_32 (abfd, section->reloff, raw.reloff);
+  bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
+  bfd_h_put_32 (abfd, section->flags, raw.flags);
+  bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
+  bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
+
+  if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
+      != BFD_MACH_O_SECTION_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static int
+bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
+{
+  struct mach_o_section_64_external raw;
+
+  memcpy (raw.sectname, section->sectname, 16);
+  memcpy (raw.segname, section->segname, 16);
+  bfd_h_put_64 (abfd, section->addr, raw.addr);
+  bfd_h_put_64 (abfd, section->size, raw.size);
+  bfd_h_put_32 (abfd, section->offset, raw.offset);
+  bfd_h_put_32 (abfd, section->align, raw.align);
+  bfd_h_put_32 (abfd, section->reloff, raw.reloff);
+  bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
+  bfd_h_put_32 (abfd, section->flags, raw.flags);
+  bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
+  bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
+  bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
+
+  if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
+      != BFD_MACH_O_SECTION_64_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static int
+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_segment_command *seg = &command->command.segment;
+  bfd_mach_o_section *sec;
+
+  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
+
+  for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+    if (!bfd_mach_o_write_relocs (abfd, sec))
+      return -1;
+
+  memcpy (raw.segname, seg->segname, 16);
+  bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
+  bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
+  bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
+  bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
+  bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
+  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;
+
+  for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+    if (bfd_mach_o_write_section_32 (abfd, sec))
+      return -1;
+
+  return 0;
+}
+
+static int
+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_segment_command *seg = &command->command.segment;
+  bfd_mach_o_section *sec;
+
+  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
+
+  for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+    if (!bfd_mach_o_write_relocs (abfd, sec))
+      return -1;
+
+  memcpy (raw.segname, seg->segname, 16);
+  bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
+  bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
+  bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
+  bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
+  bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
+  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;
+
+  for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+    if (bfd_mach_o_write_section_64 (abfd, sec))
+      return -1;
+
+  return 0;
+}
+
+static bfd_boolean
+bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  bfd_mach_o_symtab_command *sym = &command->command.symtab;
+  unsigned long i;
+  unsigned int wide = bfd_mach_o_wide_p (abfd);
+  unsigned int symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
+  struct bfd_strtab_hash *strtab;
+  asymbol **symbols = bfd_get_outsymbols (abfd);
+
+  BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
+
+  /* Write the symbols first.  */
+  mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
+  sym->symoff = mdata->filelen;
+  if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
+    return FALSE;
+
+  sym->nsyms = bfd_get_symcount (abfd);
+  mdata->filelen += sym->nsyms * symlen;
+
+  strtab = _bfd_stringtab_init ();
+  if (strtab == NULL)
+    return FALSE;
+
+  if (sym->nsyms > 0)
+    /* Although we don't strictly need to do this, for compatibility with
+       Darwin system tools, actually output an empty string for the index
+       0 entry.  */
+    _bfd_stringtab_add (strtab, "", TRUE, FALSE);
+
+  for (i = 0; i < sym->nsyms; i++)
+    {
+      bfd_size_type str_index;
+      bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
+
+      /* Compute name index.  */
+      /* An index of 0 always means the empty string.  */
+      if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
+        str_index = 0;
+      else
+        {
+          str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
+          if (str_index == (bfd_size_type) -1)
+            goto err;
+        }
+
+      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;
+        }
+      else
+        {
+          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);
+
+          if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+            goto err;
+        }
+    }
+  sym->strsize = _bfd_stringtab_size (strtab);
+  sym->stroff = mdata->filelen;
+  mdata->filelen += sym->strsize;
 
 
-  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+  if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
+    goto err;
+  _bfd_stringtab_free (strtab);
 
 
-  /* Write data sections first in case they overlap header data to be
-     written later.  */
+  /* The command.  */
+  {
+    struct mach_o_symtab_command_external raw;
 
 
-  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
-    ;
+    bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
+    bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
+    bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
+    bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
 
 
-#if 0
-  for (i = 0; i < mdata->header.ncmds; i++)
-    {
-      bfd_mach_o_load_command *cur = &mdata->commands[i];
-      if (cur->type != BFD_MACH_O_LC_SEGMENT)
-       break;
+    if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+        || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+      return FALSE;
+  }
 
 
-      {
-       bfd_mach_o_segment_command *seg = &cur->command.segment;
-       char buf[1024];
-       bfd_vma nbytes = seg->filesize;
-       bfd_vma curoff = seg->fileoff;
+  return TRUE;
 
 
-       while (nbytes > 0)
-         {
-           bfd_vma thisread = nbytes;
+ err:
+  _bfd_stringtab_free (strtab);
+  return FALSE;
+}
 
 
-           if (thisread > 1024)
-             thisread = 1024;
+/* Process the symbols and generate Mach-O specific fields.
+   Number them.  */
 
 
-           bfd_seek (abfd, curoff, SEEK_SET);
-           if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
-             return false;
+static bfd_boolean
+bfd_mach_o_mangle_symbols (bfd *abfd)
+{
+  unsigned long i;
+  asymbol **symbols = bfd_get_outsymbols (abfd);
 
 
-           bfd_seek (abfd, curoff, SEEK_SET);
-           if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
-             return false;
+  for (i = 0; i < bfd_get_symcount (abfd); i++)
+    {
+      bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
+
+      if (s->n_type == BFD_MACH_O_N_UNDF && !(s->symbol.flags & BSF_DEBUGGING))
+        {
+          /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
+             symbols should be N_UNDEF | N_EXT), we suppose the back-end
+             values haven't been set.  */
+          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;
+            }
+          else if (s->symbol.section == bfd_com_section_ptr)
+            s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
+          else
+            s->n_type = BFD_MACH_O_N_SECT;
+          
+          if (s->symbol.flags & BSF_GLOBAL)
+            s->n_type |= BFD_MACH_O_N_EXT;
+        }
+
+      /* Compute section index.  */
+      if (s->symbol.section != bfd_abs_section_ptr
+          && s->symbol.section != bfd_und_section_ptr
+          && s->symbol.section != bfd_com_section_ptr)
+        s->n_sect = s->symbol.section->target_index;
+
+      /* Number symbols.  */
+      s->symbol.udata.i = i;
+    }
+  return TRUE;
+}
 
 
-           nbytes -= thisread;
-           curoff += thisread;
-         }
-      }
-  }
-#endif
+bfd_boolean
+bfd_mach_o_write_contents (bfd *abfd)
+{
+  unsigned int i;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+
+  if (mdata->header.ncmds == 0)
+    if (!bfd_mach_o_build_commands (abfd))
+      return FALSE;
 
   /* Now write header information.  */
 
   /* Now write header information.  */
-  if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
-    return false;
+  if (mdata->header.filetype == 0)
+    {
+      if (abfd->flags & EXEC_P)
+        mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
+      else if (abfd->flags & DYNAMIC)
+        mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
+      else
+        mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
+    }
+  if (!bfd_mach_o_write_header (abfd, &mdata->header))
+    return FALSE;
+
+  /* Assign a number to each symbols.  */
+  if (!bfd_mach_o_mangle_symbols (abfd))
+    return FALSE;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
-      unsigned char buf[8];
+      struct mach_o_load_command_external raw;
       bfd_mach_o_load_command *cur = &mdata->commands[i];
       unsigned long typeflag;
 
       bfd_mach_o_load_command *cur = &mdata->commands[i];
       unsigned long typeflag;
 
-      typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
+      typeflag = cur->type | (cur->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
 
 
-      bfd_h_put_32 (abfd, typeflag, buf);
-      bfd_h_put_32 (abfd, cur->len, buf + 4);
+      bfd_h_put_32 (abfd, typeflag, raw.cmd);
+      bfd_h_put_32 (abfd, cur->len, raw.cmdsize);
 
 
-      bfd_seek (abfd, cur->offset, SEEK_SET);
-      if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
-       return false;
+      if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
+          || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
+       return FALSE;
 
       switch (cur->type)
        {
        case BFD_MACH_O_LC_SEGMENT:
 
       switch (cur->type)
        {
        case BFD_MACH_O_LC_SEGMENT:
-         if (bfd_mach_o_scan_write_segment (abfd, cur) != 0)
-           return false;
+         if (bfd_mach_o_write_segment_32 (abfd, cur) != 0)
+           return FALSE;
+         break;
+       case BFD_MACH_O_LC_SEGMENT_64:
+         if (bfd_mach_o_write_segment_64 (abfd, cur) != 0)
+           return FALSE;
          break;
        case BFD_MACH_O_LC_SYMTAB:
          break;
        case BFD_MACH_O_LC_SYMTAB:
-         if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
-           return false;
+         if (!bfd_mach_o_write_symtab (abfd, cur))
+           return FALSE;
          break;
        case BFD_MACH_O_LC_SYMSEG:
          break;
        case BFD_MACH_O_LC_THREAD:
        case BFD_MACH_O_LC_UNIXTHREAD:
          break;
        case BFD_MACH_O_LC_SYMSEG:
          break;
        case BFD_MACH_O_LC_THREAD:
        case BFD_MACH_O_LC_UNIXTHREAD:
-         if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
-           return false;
+         if (bfd_mach_o_write_thread (abfd, cur) != 0)
+           return FALSE;
          break;
        case BFD_MACH_O_LC_LOADFVMLIB:
        case BFD_MACH_O_LC_IDFVMLIB:
          break;
        case BFD_MACH_O_LC_LOADFVMLIB:
        case BFD_MACH_O_LC_IDFVMLIB:
@@ -420,6 +1539,8 @@ bfd_mach_o_write_contents (abfd)
        case BFD_MACH_O_LC_LOAD_DYLIB:
        case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
        case BFD_MACH_O_LC_ID_DYLIB:
        case BFD_MACH_O_LC_LOAD_DYLIB:
        case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
        case BFD_MACH_O_LC_ID_DYLIB:
+       case BFD_MACH_O_LC_REEXPORT_DYLIB:
+        case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
        case BFD_MACH_O_LC_LOAD_DYLINKER:
        case BFD_MACH_O_LC_ID_DYLINKER:
        case BFD_MACH_O_LC_PREBOUND_DYLIB:
        case BFD_MACH_O_LC_LOAD_DYLINKER:
        case BFD_MACH_O_LC_ID_DYLINKER:
        case BFD_MACH_O_LC_PREBOUND_DYLIB:
@@ -427,376 +1548,645 @@ bfd_mach_o_write_contents (abfd)
        case BFD_MACH_O_LC_SUB_FRAMEWORK:
          break;
        default:
        case BFD_MACH_O_LC_SUB_FRAMEWORK:
          break;
        default:
-         fprintf (stderr,
-                  "unable to write unknown load command 0x%lx\n",
-                  (long) cur->type);
-         return false;
+         (*_bfd_error_handler) (_("unable to write unknown load command 0x%lx"),
+                                (unsigned long) cur->type);
+         return FALSE;
        }
     }
 
        }
     }
 
-  return true;
+  return TRUE;
 }
 
 }
 
-static int
-bfd_mach_o_sizeof_headers (a, b)
-     bfd *a ATTRIBUTE_UNUSED;
-     boolean b ATTRIBUTE_UNUSED;
+static void
+bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
+                                      asection *sec)
 {
 {
-  return 0;
+  bfd_mach_o_section *s = (bfd_mach_o_section *)sec->used_by_bfd;
+  if (seg->sect_head == NULL)
+    seg->sect_head = s;
+  else
+    seg->sect_tail->next = s;
+  seg->sect_tail = s;
 }
 
 }
 
-/* Make an empty symbol.  This is required only because
-   bfd_make_section_anyway wants to create a symbol for the section.  */
+/* Create section Mach-O flags from BFD flags.  */
+
+static void
+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);
+
+  /* Create default flags.  */
+  bfd_flags = bfd_get_section_flags (abfd, sec);
+  if ((bfd_flags & SEC_CODE) == SEC_CODE)
+    s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
+      | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
+      | BFD_MACH_O_S_REGULAR;
+  else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
+    s->flags = BFD_MACH_O_S_ZEROFILL;
+  else if (bfd_flags & SEC_DEBUGGING)
+    s->flags = BFD_MACH_O_S_REGULAR |  BFD_MACH_O_S_ATTR_DEBUG;
+  else
+    s->flags = BFD_MACH_O_S_REGULAR;
+}
 
 
-static asymbol *
-bfd_mach_o_make_empty_symbol (abfd)
-     bfd *abfd;
+/* Build Mach-O load commands from the sections.  */
+
+bfd_boolean
+bfd_mach_o_build_commands (bfd *abfd)
 {
 {
-  asymbol *new;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  unsigned int wide = mach_o_wide_p (&mdata->header);
+  bfd_mach_o_segment_command *seg;
+  asection *sec;
+  bfd_mach_o_load_command *cmd;
+  bfd_mach_o_load_command *symtab_cmd;
+  int target_index;
+
+  /* Return now if commands are already built.  */
+  if (mdata->header.ncmds)
+    return FALSE;
+
+  /* Very simple version: a command (segment) to contain all the sections and
+     a command for the symbol table.  */
+  mdata->header.ncmds = 2;
+  mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
+                               * sizeof (bfd_mach_o_load_command));
+  if (mdata->commands == NULL)
+    return FALSE;
+  cmd = &mdata->commands[0];
+  seg = &cmd->command.segment;
+
+  seg->nsects = bfd_count_sections (abfd);
+
+  /* Set segment command.  */
+  if (wide)
+    {
+      cmd->type = BFD_MACH_O_LC_SEGMENT_64;
+      cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
+      cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
+        + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
+    }
+  else
+    {
+      cmd->type = BFD_MACH_O_LC_SEGMENT;
+      cmd->offset = BFD_MACH_O_HEADER_SIZE;
+      cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
+        + BFD_MACH_O_SECTION_SIZE * seg->nsects;
+    }
+  cmd->type_required = FALSE;
+  mdata->header.sizeofcmds = cmd->len;
+  mdata->filelen = cmd->offset + cmd->len;
+
+  /* Set symtab command.  */
+  symtab_cmd = &mdata->commands[1];
+  
+  symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
+  symtab_cmd->offset = cmd->offset + cmd->len;
+  symtab_cmd->len = 6 * 4;
+  symtab_cmd->type_required = FALSE;
+  
+  mdata->header.sizeofcmds += symtab_cmd->len;
+  mdata->filelen += symtab_cmd->len;
+
+  /* Fill segment command.  */
+  memset (seg->segname, 0, sizeof (seg->segname));
+  seg->vmaddr = 0;
+  seg->fileoff = mdata->filelen;
+  seg->filesize = 0;
+  seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
+    | BFD_MACH_O_PROT_EXECUTE;
+  seg->initprot = seg->maxprot;
+  seg->flags = 0;
+  seg->sect_head = NULL;
+  seg->sect_tail = NULL;
+
+  /* Create Mach-O sections.
+     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)
+    {
+      unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
+      bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
+
+      bfd_mach_o_append_section_to_segment (seg, sec);
+
+      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 
+        section was created.  */
+      msect->align = msect->align > bfd_align ? msect->align : bfd_align;
+
+      if (msect->size != 0)
+        {
+          mdata->filelen = FILE_ALIGN (mdata->filelen, msect->align);
+          msect->offset = mdata->filelen;
+        }
+      else
+        msect->offset = 0;
 
 
-  new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
-  if (new == NULL)
-    return new;
-  new->the_bfd = abfd;
-  return new;
+      sec->filepos = msect->offset;
+      sec->target_index = ++target_index;
+
+      mdata->filelen += msect->size;
+    }
+  seg->filesize = mdata->filelen - seg->fileoff;
+  seg->vmsize = seg->filesize;
+
+  return TRUE;
 }
 
 }
 
-static int
-bfd_mach_o_write_header (abfd, header)
-     bfd *abfd;
-     bfd_mach_o_header *header;
-{
-  unsigned char buf[28];
-
-  bfd_h_put_32 (abfd, header->magic, buf + 0);
-  bfd_h_put_32 (abfd, header->cputype, buf + 4);
-  bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
-  bfd_h_put_32 (abfd, header->filetype, buf + 12);
-  bfd_h_put_32 (abfd, header->ncmds, buf + 16);
-  bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
-  bfd_h_put_32 (abfd, header->flags, buf + 24);
-
-  bfd_seek (abfd, 0, SEEK_SET);
-  if (bfd_bwrite ((PTR) buf, 28, abfd) != 28)
-    return -1;
+/* Set the contents of a section.  */
+
+bfd_boolean
+bfd_mach_o_set_section_contents (bfd *abfd,
+                                asection *section,
+                                const void * location,
+                                file_ptr offset,
+                                bfd_size_type count)
+{
+  file_ptr pos;
+
+  /* This must be done first, because bfd_set_section_contents is
+     going to set output_has_begun to TRUE.  */
+  if (! abfd->output_has_begun && ! bfd_mach_o_build_commands (abfd))
+    return FALSE;
+
+  if (count == 0)
+    return TRUE;
+
+  pos = section->filepos + offset;
+  if (bfd_seek (abfd, pos, SEEK_SET) != 0
+      || bfd_bwrite (location, count, abfd) != count)
+    return FALSE;
+
+  return TRUE;
+}
 
 
+int
+bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
+                          struct bfd_link_info *info ATTRIBUTE_UNUSED)
+{
   return 0;
 }
 
   return 0;
 }
 
-static int
-bfd_mach_o_read_header (abfd, header)
-     bfd *abfd;
-     bfd_mach_o_header *header;
+/* Make an empty symbol.  This is required only because
+   bfd_make_section_anyway wants to create a symbol for the section.  */
+
+asymbol *
+bfd_mach_o_make_empty_symbol (bfd *abfd)
 {
 {
-  unsigned char buf[28];
-  bfd_vma (*get32) PARAMS ((const bfd_byte *)) = NULL;
+  asymbol *new_symbol;
+
+  new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
+  if (new_symbol == NULL)
+    return new_symbol;
+  new_symbol->the_bfd = abfd;
+  new_symbol->udata.i = 0;
+  return new_symbol;
+}
 
 
-  bfd_seek (abfd, 0, SEEK_SET);
+static bfd_boolean
+bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
+{
+  struct mach_o_header_external raw;
+  unsigned int size;
+  bfd_vma (*get32) (const void *) = NULL;
 
 
-  if (bfd_bread ((PTR) buf, 28, abfd) != 28)
-    return -1;
+  /* Just read the magic number.  */
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0
+      || bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4)
+    return FALSE;
 
 
-  if (bfd_getb32 (buf) == 0xfeedface)
+  if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
+    {
+      header->byteorder = BFD_ENDIAN_BIG;
+      header->magic = BFD_MACH_O_MH_MAGIC;
+      header->version = 1;
+      get32 = bfd_getb32;
+    }
+  else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
+    {
+      header->byteorder = BFD_ENDIAN_LITTLE;
+      header->magic = BFD_MACH_O_MH_MAGIC;
+      header->version = 1;
+      get32 = bfd_getl32;
+    }
+  else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
     {
       header->byteorder = BFD_ENDIAN_BIG;
     {
       header->byteorder = BFD_ENDIAN_BIG;
-      header->magic = 0xfeedface;
+      header->magic = BFD_MACH_O_MH_MAGIC_64;
+      header->version = 2;
       get32 = bfd_getb32;
     }
       get32 = bfd_getb32;
     }
-  else if (bfd_getl32 (buf) == 0xfeedface)
+  else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
     {
       header->byteorder = BFD_ENDIAN_LITTLE;
     {
       header->byteorder = BFD_ENDIAN_LITTLE;
-      header->magic = 0xfeedface;
+      header->magic = BFD_MACH_O_MH_MAGIC_64;
+      header->version = 2;
       get32 = bfd_getl32;
     }
   else
     {
       header->byteorder = BFD_ENDIAN_UNKNOWN;
       get32 = bfd_getl32;
     }
   else
     {
       header->byteorder = BFD_ENDIAN_UNKNOWN;
-      return -1;
+      return FALSE;
     }
 
     }
 
-  header->cputype = (*get32) (buf + 4);
-  header->cpusubtype = (*get32) (buf + 8);
-  header->filetype = (*get32) (buf + 12);
-  header->ncmds = (*get32) (buf + 16);
-  header->sizeofcmds = (*get32) (buf + 20);
-  header->flags = (*get32) (buf + 24);
+  /* Once the size of the header is known, read the full header.  */
+  size = mach_o_wide_p (header) ?
+    BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
 
 
-  return 0;
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0
+      || bfd_bread (&raw, size, abfd) != size)
+    return FALSE;
+
+  header->cputype = (*get32) (raw.cputype);
+  header->cpusubtype = (*get32) (raw.cpusubtype);
+  header->filetype = (*get32) (raw.filetype);
+  header->ncmds = (*get32) (raw.ncmds);
+  header->sizeofcmds = (*get32) (raw.sizeofcmds);
+  header->flags = (*get32) (raw.flags);
+
+  if (mach_o_wide_p (header))
+    header->reserved = (*get32) (raw.reserved);
+
+  return TRUE;
 }
 
 }
 
-static asection *
-bfd_mach_o_make_bfd_section (abfd, section)
-     bfd *abfd;
-     bfd_mach_o_section *section;
+bfd_boolean
+bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
 {
 {
-  asection *bfdsec;
-  char *sname;
-  const char *prefix = "LC_SEGMENT";
-  unsigned int snamelen;
+  bfd_mach_o_section *s;
+  unsigned bfdalign = bfd_get_section_alignment (abfd, sec);
+
+  s = bfd_mach_o_get_mach_o_section (sec);
+  if (s == NULL)
+    {
+      flagword bfd_flags;
+      static const mach_o_section_name_xlat * xlat;
 
 
-  snamelen = strlen (prefix) + 1
-    + strlen (section->segname) + 1
-    + strlen (section->sectname) + 1;
+      s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s));
+      if (s == NULL)
+       return FALSE;
+      sec->used_by_bfd = s;
+      s->bfdsection = sec;
+
+      /* Create the Darwin seg/sect name pair from the bfd name.
+        If this is a canonical name for which a specific paiting exists
+        there will also be defined flags, type, attribute and alignment
+        values.  */
+      xlat = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
+      if (xlat != NULL)
+       {
+         s->flags = xlat->macho_sectype | xlat->macho_secattr;
+         s->align = xlat->sectalign > bfdalign ? xlat->sectalign 
+                                               : bfdalign;
+         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);
+       }
+      else
+       /* Create default flags.  */
+       bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
+    }
 
 
-  sname = (char *) bfd_alloc (abfd, snamelen);
-  if (sname == NULL)
-    return NULL;
-  sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
+  return _bfd_generic_new_section_hook (abfd, sec);
+}
 
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
-  if (bfdsec == NULL)
-    return NULL;
+static void
+bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
+                                     unsigned long prot)
+{
+  flagword flags;
+  bfd_mach_o_section *section;
 
 
-  bfdsec->vma = section->addr;
-  bfdsec->lma = section->addr;
-  bfdsec->_raw_size = section->size;
-  bfdsec->filepos = section->offset;
-  bfdsec->alignment_power = section->align;
+  flags = bfd_get_section_flags (abfd, sec);
+  section = bfd_mach_o_get_mach_o_section (sec);
 
 
-  if (section->flags & BFD_MACH_O_S_ZEROFILL)
-    bfdsec->flags = SEC_ALLOC;
+  /* TODO: see if we should use the xlat system for doing this by
+     preference and fall back to this for unknown sections.  */
+
+  if (flags == SEC_NO_FLAGS)
+    {
+      /* Try to guess flags.  */
+      if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
+        flags = SEC_DEBUGGING;
+      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;
+            }
+        }
+    }
   else
   else
-    bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
+    {
+      if ((flags & SEC_DEBUGGING) == 0)
+        flags |= SEC_ALLOC;
+    }
 
 
-  return bfdsec;
+  if (section->offset != 0)
+    flags |= SEC_HAS_CONTENTS;
+  if (section->nreloc != 0)
+    flags |= SEC_RELOC;
+
+  bfd_set_section_flags (abfd, sec, flags);
+
+  sec->vma = section->addr;
+  sec->lma = section->addr;
+  sec->size = section->size;
+  sec->filepos = section->offset;
+  sec->alignment_power = section->align;
+  sec->segment_mark = 0;
+  sec->reloc_count = section->nreloc;
+  sec->rel_filepos = section->reloff;
 }
 
 }
 
-static int
-bfd_mach_o_scan_read_section (abfd, section, offset)
-     bfd *abfd;
-     bfd_mach_o_section *section;
-     bfd_vma offset;
+static asection *
+bfd_mach_o_make_bfd_section (bfd *abfd,
+                             const unsigned char *segname,
+                             const unsigned char *sectname)
 {
 {
-  unsigned char buf[68];
+  const char *sname;
+  flagword flags;
 
 
-  bfd_seek (abfd, offset, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 68, abfd) != 68)
-    return -1;
-
-  memcpy (section->sectname, buf, 16);
-  section->sectname[16] = '\0';
-  memcpy (section->segname, buf + 16, 16);
-  section->segname[16] = '\0';
-  section->addr = bfd_h_get_32 (abfd, buf + 32);
-  section->size = bfd_h_get_32 (abfd, buf + 36);
-  section->offset = bfd_h_get_32 (abfd, buf + 40);
-  section->align = bfd_h_get_32 (abfd, buf + 44);
-  section->reloff = bfd_h_get_32 (abfd, buf + 48);
-  section->nreloc = bfd_h_get_32 (abfd, buf + 52);
-  section->flags = bfd_h_get_32 (abfd, buf + 56);
-  section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
-  section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
-  section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section);
-
-  if (section->bfdsection == NULL)
-    return -1;
+  bfd_mach_o_convert_section_name_to_bfd
+    (abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
+  if (sname == NULL)
+    return NULL;
 
 
-  return 0;
+  return bfd_make_section_anyway_with_flags (abfd, sname, flags);
 }
 
 }
 
-static int
-bfd_mach_o_scan_write_section (abfd, section, offset)
-     bfd *abfd;
-     bfd_mach_o_section *section;
-     bfd_vma offset;
-{
-  unsigned char buf[68];
-
-  memcpy (buf, section->sectname, 16);
-  memcpy (buf + 16, section->segname, 16);
-  bfd_h_put_32 (abfd, section->addr, buf + 32);
-  bfd_h_put_32 (abfd, section->size, buf + 36);
-  bfd_h_put_32 (abfd, section->offset, buf + 40);
-  bfd_h_put_32 (abfd, section->align, buf + 44);
-  bfd_h_put_32 (abfd, section->reloff, buf + 48);
-  bfd_h_put_32 (abfd, section->nreloc, buf + 52);
-  bfd_h_put_32 (abfd, section->flags, buf + 56);
-  /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
-  /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
-
-  bfd_seek (abfd, offset, SEEK_SET);
-  if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
-    return -1;
+static asection *
+bfd_mach_o_read_section_32 (bfd *abfd,
+                            unsigned int offset,
+                            unsigned long prot)
+{
+  struct mach_o_section_32_external raw;
+  asection *sec;
+  bfd_mach_o_section *section;
 
 
-  return 0;
+  if (bfd_seek (abfd, offset, SEEK_SET) != 0
+      || (bfd_bread (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
+          != BFD_MACH_O_SECTION_SIZE))
+    return NULL;
+
+  sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
+  if (sec == NULL)
+    return NULL;
+
+  section = bfd_mach_o_get_mach_o_section (sec);
+  memcpy (section->segname, raw.segname, sizeof (raw.segname));
+  section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
+  memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
+  section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
+  section->addr = bfd_h_get_32 (abfd, raw.addr);
+  section->size = bfd_h_get_32 (abfd, raw.size);
+  section->offset = bfd_h_get_32 (abfd, raw.offset);
+  section->align = bfd_h_get_32 (abfd, raw.align);
+  section->reloff = bfd_h_get_32 (abfd, raw.reloff);
+  section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
+  section->flags = bfd_h_get_32 (abfd, raw.flags);
+  section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
+  section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
+  section->reserved3 = 0;
+
+  bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
+
+  return sec;
 }
 
 }
 
-static int
-bfd_mach_o_scan_write_symtab_symbols (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+static asection *
+bfd_mach_o_read_section_64 (bfd *abfd,
+                            unsigned int offset,
+                            unsigned long prot)
 {
 {
-  bfd_mach_o_symtab_command *sym = &command->command.symtab;
-  asymbol *s = NULL;
-  unsigned long i;
+  struct mach_o_section_64_external raw;
+  asection *sec;
+  bfd_mach_o_section *section;
 
 
-  for (i = 0; i < sym->nsyms; i++)
-    {
-      unsigned char buf[12];
-      bfd_vma symoff = sym->symoff + (i * 12);
-      unsigned char ntype = 0;
-      unsigned char nsect = 0;
-      short ndesc = 0;
-
-      s = &sym->symbols[i];
-
-      /* Don't set this from the symbol information; use stored values.  */
-#if 0
-      if (s->flags & BSF_GLOBAL)
-       ntype |= N_EXT;
-      if (s->flags & BSF_DEBUGGING)
-       ntype |= N_STAB;
-
-      if (s->section == bfd_und_section_ptr)
-       ntype |= N_UNDF;
-      else if (s->section == bfd_abs_section_ptr)
-       ntype |= N_ABS;
-      else
-       ntype |= N_SECT;
-#endif
-
-      /* Instead just set from the stored values.  */
-      ntype = (s->udata.i >> 24) & 0xff;
-      nsect = (s->udata.i >> 16) & 0xff;
-      ndesc = s->udata.i & 0xffff;
-
-      bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
-      bfd_h_put_8 (abfd, ntype, buf + 4);
-      bfd_h_put_8 (abfd, nsect, buf + 5);
-      bfd_h_put_16 (abfd, ndesc, buf + 6);
-      bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
-
-      bfd_seek (abfd, symoff, SEEK_SET);
-      if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
-       {
-         fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
-                  12, (unsigned long) symoff);
-         return -1;
-       }
-    }
+  if (bfd_seek (abfd, offset, SEEK_SET) != 0
+      || (bfd_bread (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
+          != BFD_MACH_O_SECTION_64_SIZE))
+    return NULL;
 
 
-  return 0;
+  sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
+  if (sec == NULL)
+    return NULL;
+
+  section = bfd_mach_o_get_mach_o_section (sec);
+  memcpy (section->segname, raw.segname, sizeof (raw.segname));
+  section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
+  memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
+  section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
+  section->addr = bfd_h_get_64 (abfd, raw.addr);
+  section->size = bfd_h_get_64 (abfd, raw.size);
+  section->offset = bfd_h_get_32 (abfd, raw.offset);
+  section->align = bfd_h_get_32 (abfd, raw.align);
+  section->reloff = bfd_h_get_32 (abfd, raw.reloff);
+  section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
+  section->flags = bfd_h_get_32 (abfd, raw.flags);
+  section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
+  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);
+
+  return sec;
 }
 
 }
 
-int
-bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
-     bfd *abfd;
-     bfd_mach_o_symtab_command *sym;
-     asymbol *s;
-     unsigned long i;
-{
-  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
-  bfd_vma symoff = sym->symoff + (i * 12);
-  unsigned char buf[12];
+static asection *
+bfd_mach_o_read_section (bfd *abfd,
+                         unsigned int offset,
+                         unsigned long prot,
+                         unsigned int wide)
+{
+  if (wide)
+    return bfd_mach_o_read_section_64 (abfd, offset, prot);
+  else
+    return bfd_mach_o_read_section_32 (abfd, offset, prot);
+}
+
+static bfd_boolean
+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_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  unsigned int wide = mach_o_wide_p (&mdata->header);
+  unsigned int symwidth =
+    wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
+  unsigned int symoff = sym->symoff + (i * symwidth);
+  struct mach_o_nlist_64_external raw;
   unsigned char type = -1;
   unsigned char section = -1;
   short desc = -1;
   unsigned char type = -1;
   unsigned char section = -1;
   short desc = -1;
-  unsigned long value = -1;
+  symvalue value = -1;
   unsigned long stroff = -1;
   unsigned int symtype = -1;
 
   BFD_ASSERT (sym->strtab != NULL);
 
   unsigned long stroff = -1;
   unsigned int symtype = -1;
 
   BFD_ASSERT (sym->strtab != NULL);
 
-  bfd_seek (abfd, symoff, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 12, abfd) != 12)
+  if (bfd_seek (abfd, symoff, SEEK_SET) != 0
+      || bfd_bread (&raw, symwidth, abfd) != symwidth)
     {
     {
-      fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
-              12, (unsigned long) symoff);
-      return -1;
+      (*_bfd_error_handler)
+        (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu"),
+         symwidth, (unsigned long) symoff);
+      return FALSE;
     }
 
     }
 
-  stroff = bfd_h_get_32 (abfd, buf);
-  type = bfd_h_get_8 (abfd, buf + 4);
-  symtype = (type & 0x0e);
-  section = bfd_h_get_8 (abfd, buf + 5) - 1;
-  desc = bfd_h_get_16 (abfd, buf + 6);
-  value = bfd_h_get_32 (abfd, buf + 8);
+  stroff = bfd_h_get_32 (abfd, raw.n_strx);
+  type = bfd_h_get_8 (abfd, raw.n_type);
+  symtype = type & BFD_MACH_O_N_TYPE;
+  section = bfd_h_get_8 (abfd, raw.n_sect);
+  desc = bfd_h_get_16 (abfd, raw.n_desc);
+  if (wide)
+    value = bfd_h_get_64 (abfd, raw.n_value);
+  else
+    value = bfd_h_get_32 (abfd, raw.n_value);
 
   if (stroff >= sym->strsize)
     {
 
   if (stroff >= sym->strsize)
     {
-      fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
-              (unsigned long) stroff, (unsigned long) sym->strsize);
-      return -1;
+      (*_bfd_error_handler)
+        (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %lu)"),
+         (unsigned long) stroff,
+         (unsigned long) sym->strsize);
+      return FALSE;
     }
 
     }
 
-  s->the_bfd = abfd;
-  s->name = sym->strtab + stroff;
-  s->value = value;
-  s->udata.i = (type << 24) | (section << 16) | desc;
-  s->flags = 0x0;
+  s->symbol.the_bfd = abfd;
+  s->symbol.name = sym->strtab + stroff;
+  s->symbol.value = value;
+  s->symbol.flags = 0x0;
+  s->symbol.udata.i = 0;
+  s->n_type = type;
+  s->n_sect = section;
+  s->n_desc = desc;
 
   if (type & BFD_MACH_O_N_STAB)
     {
 
   if (type & BFD_MACH_O_N_STAB)
     {
-      s->flags |= BSF_DEBUGGING;
-      s->section = bfd_und_section_ptr;
+      s->symbol.flags |= BSF_DEBUGGING;
+      s->symbol.section = bfd_und_section_ptr;
+      switch (type)
+       {
+       case N_FUN:
+       case N_STSYM:
+       case N_LCSYM:
+       case N_BNSYM:
+       case N_SLINE:
+       case N_ENSYM:
+       case N_ECOMM:
+       case N_ECOML:
+       case N_GSYM:
+         if ((section > 0) && (section <= mdata->nsects))
+           {
+             s->symbol.section = mdata->sections[section - 1]->bfdsection;
+             s->symbol.value =
+                s->symbol.value - mdata->sections[section - 1]->addr;
+           }
+         break;
+       }
     }
   else
     {
       if (type & BFD_MACH_O_N_PEXT)
     }
   else
     {
       if (type & BFD_MACH_O_N_PEXT)
-       {
-         type &= ~BFD_MACH_O_N_PEXT;
-         s->flags |= BSF_GLOBAL;
-       }
+       s->symbol.flags |= BSF_GLOBAL;
 
       if (type & BFD_MACH_O_N_EXT)
 
       if (type & BFD_MACH_O_N_EXT)
-       {
-         type &= ~BFD_MACH_O_N_EXT;
-         s->flags |= BSF_GLOBAL;
-       }
+       s->symbol.flags |= BSF_GLOBAL;
+
+      if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
+       s->symbol.flags |= BSF_LOCAL;
 
       switch (symtype)
        {
        case BFD_MACH_O_N_UNDF:
 
       switch (symtype)
        {
        case BFD_MACH_O_N_UNDF:
-         s->section = bfd_und_section_ptr;
+          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:
          break;
        case BFD_MACH_O_N_PBUD:
-         s->section = bfd_und_section_ptr;
+         s->symbol.section = bfd_und_section_ptr;
          break;
        case BFD_MACH_O_N_ABS:
          break;
        case BFD_MACH_O_N_ABS:
-         s->section = bfd_abs_section_ptr;
+         s->symbol.section = bfd_abs_section_ptr;
          break;
        case BFD_MACH_O_N_SECT:
          if ((section > 0) && (section <= mdata->nsects))
            {
          break;
        case BFD_MACH_O_N_SECT:
          if ((section > 0) && (section <= mdata->nsects))
            {
-             s->section = mdata->sections[section - 1]->bfdsection;
-             s->value = s->value - mdata->sections[section - 1]->addr;
+             s->symbol.section = mdata->sections[section - 1]->bfdsection;
+             s->symbol.value =
+                s->symbol.value - mdata->sections[section - 1]->addr;
            }
          else
            {
              /* Mach-O uses 0 to mean "no section"; not an error.  */
              if (section != 0)
                {
            }
          else
            {
              /* Mach-O uses 0 to mean "no section"; not an error.  */
              if (section != 0)
                {
-                 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
-                          "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
-                          s->name, section, mdata->nsects);
+                 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
+                                          "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined"),
+                                        s->symbol.name, section, mdata->nsects);
                }
                }
-             s->section = bfd_und_section_ptr;
+             s->symbol.section = bfd_und_section_ptr;
            }
          break;
        case BFD_MACH_O_N_INDR:
            }
          break;
        case BFD_MACH_O_N_INDR:
-         fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
-                  "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
-                  s->name);
-         s->section = bfd_und_section_ptr;
+         /* FIXME: we don't follow the BFD convention as this indirect symbol
+            won't be followed by the referenced one.  This looks harmless
+            unless we start using the linker.  */
+         s->symbol.flags |= BSF_INDIRECT;
+         s->symbol.section = bfd_ind_section_ptr;
+         s->symbol.value = 0;
          break;
        default:
          break;
        default:
-         fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
-                  "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
-                  s->name, symtype);
-         s->section = bfd_und_section_ptr;
+         (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
+                                  "symbol \"%s\" specified invalid type field 0x%x: setting to undefined"),
+                                s->symbol.name, symtype);
+         s->symbol.section = bfd_und_section_ptr;
          break;
        }
     }
 
          break;
        }
     }
 
-  return 0;
+  return TRUE;
 }
 
 }
 
-int
-bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
-     bfd *abfd;
-     bfd_mach_o_symtab_command *sym;
+bfd_boolean
+bfd_mach_o_read_symtab_strtab (bfd *abfd)
 {
 {
-  BFD_ASSERT (sym->strtab == NULL);
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  bfd_mach_o_symtab_command *sym = mdata->symtab;
+
+  /* Fail if there is no symtab.  */
+  if (sym == NULL)
+    return FALSE;
+
+  /* Success if already loaded.  */
+  if (sym->strtab)
+    return TRUE;
 
   if (abfd->flags & BFD_IN_MEMORY)
     {
 
   if (abfd->flags & BFD_IN_MEMORY)
     {
@@ -807,268 +2197,167 @@ bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
       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 -1;
+         return FALSE;
        }
        }
-      sym->strtab = b->buffer + sym->stroff;
-      return 0;
+      sym->strtab = (char *) b->buffer + sym->stroff;
     }
     }
-
-  sym->strtab = bfd_alloc (abfd, sym->strsize);
-  if (sym->strtab == NULL)
-    return -1;
-
-  bfd_seek (abfd, sym->stroff, SEEK_SET);
-  if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
+  else
     {
     {
-      fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
-              sym->strsize, sym->stroff);
-      return -1;
+      sym->strtab = bfd_alloc (abfd, sym->strsize);
+      if (sym->strtab == NULL)
+        return FALSE;
+
+      if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
+          || bfd_bread (sym->strtab, sym->strsize, abfd) != sym->strsize)
+        {
+          bfd_set_error (bfd_error_file_truncated);
+          return FALSE;
+        }
     }
 
     }
 
-  return 0;
+  return TRUE;
 }
 
 }
 
-int
-bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
-     bfd *abfd;
-     bfd_mach_o_symtab_command *sym;
+bfd_boolean
+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;
   unsigned long i;
-  int ret;
 
 
-  BFD_ASSERT (sym->symbols == NULL);
-  sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
-
-  if (sym->symbols == NULL)
+  if (sym == NULL || sym->symbols)
     {
     {
-      fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
-      return -1;
+      /* Return now if there are no symbols or if already loaded.  */
+      return TRUE;
     }
 
     }
 
-  ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
-  if (ret != 0)
-    return ret;
+  sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
 
 
-  for (i = 0; i < sym->nsyms; i++)
+  if (sym->symbols == NULL)
     {
     {
-      ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
-      if (ret != 0)
-       return ret;
+      (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
+      return FALSE;
     }
 
     }
 
-  return 0;
-}
-
-int
-bfd_mach_o_scan_read_dysymtab_symbol (abfd, dysym, sym, s, i)
-     bfd *abfd;
-     bfd_mach_o_dysymtab_command *dysym;
-     bfd_mach_o_symtab_command *sym;
-     asymbol *s;
-     unsigned long i;
-{
-  unsigned long isymoff = dysym->indirectsymoff + (i * 4);
-  unsigned long symindex;
-  unsigned char buf[4];
-
-  BFD_ASSERT (i < dysym->nindirectsyms);
+  if (!bfd_mach_o_read_symtab_strtab (abfd))
+    return FALSE;
 
 
-  bfd_seek (abfd, isymoff, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 4, abfd) != 4)
+  for (i = 0; i < sym->nsyms; i++)
     {
     {
-      fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
-              (unsigned long) 4, isymoff);
-      return -1;
+      if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
+       return FALSE;
     }
     }
-  symindex = bfd_h_get_32 (abfd, buf);
 
 
-  return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
+  return TRUE;
 }
 
 static const char *
 }
 
 static const char *
-bfd_mach_o_i386_flavour_string (flavour)
-     unsigned int flavour;
+bfd_mach_o_i386_flavour_string (unsigned int flavour)
 {
   switch ((int) flavour)
     {
 {
   switch ((int) flavour)
     {
-    case BFD_MACH_O_i386_NEW_THREAD_STATE: return "i386_NEW_THREAD_STATE";
-    case BFD_MACH_O_i386_FLOAT_STATE: return "i386_FLOAT_STATE";
-    case BFD_MACH_O_i386_ISA_PORT_MAP_STATE: return "i386_ISA_PORT_MAP_STATE";
-    case BFD_MACH_O_i386_V86_ASSIST_STATE: return "i386_V86_ASSIST_STATE";
-    case BFD_MACH_O_i386_REGS_SEGS_STATE: return "i386_REGS_SEGS_STATE";
-    case BFD_MACH_O_i386_THREAD_SYSCALL_STATE: return "i386_THREAD_SYSCALL_STATE";
-    case BFD_MACH_O_i386_THREAD_STATE_NONE: return "i386_THREAD_STATE_NONE";
-    case BFD_MACH_O_i386_SAVED_STATE: return "i386_SAVED_STATE";
-    case BFD_MACH_O_i386_THREAD_STATE: return "i386_THREAD_STATE";
-    case BFD_MACH_O_i386_THREAD_FPSTATE: return "i386_THREAD_FPSTATE";
-    case BFD_MACH_O_i386_THREAD_EXCEPTSTATE: return "i386_THREAD_EXCEPTSTATE";
-    case BFD_MACH_O_i386_THREAD_CTHREADSTATE: return "i386_THREAD_CTHREADSTATE";
+    case BFD_MACH_O_x86_THREAD_STATE32:    return "x86_THREAD_STATE32";
+    case BFD_MACH_O_x86_FLOAT_STATE32:     return "x86_FLOAT_STATE32";
+    case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
+    case BFD_MACH_O_x86_THREAD_STATE64:    return "x86_THREAD_STATE64";
+    case BFD_MACH_O_x86_FLOAT_STATE64:     return "x86_FLOAT_STATE64";
+    case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
+    case BFD_MACH_O_x86_THREAD_STATE:      return "x86_THREAD_STATE";
+    case BFD_MACH_O_x86_FLOAT_STATE:       return "x86_FLOAT_STATE";
+    case BFD_MACH_O_x86_EXCEPTION_STATE:   return "x86_EXCEPTION_STATE";
+    case BFD_MACH_O_x86_DEBUG_STATE32:     return "x86_DEBUG_STATE32";
+    case BFD_MACH_O_x86_DEBUG_STATE64:     return "x86_DEBUG_STATE64";
+    case BFD_MACH_O_x86_DEBUG_STATE:       return "x86_DEBUG_STATE";
+    case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
     default: return "UNKNOWN";
     }
 }
 
 static const char *
     default: return "UNKNOWN";
     }
 }
 
 static const char *
-bfd_mach_o_ppc_flavour_string (flavour)
-     unsigned int flavour;
+bfd_mach_o_ppc_flavour_string (unsigned int flavour)
 {
   switch ((int) flavour)
     {
 {
   switch ((int) flavour)
     {
-    case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
-    case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
-    case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
-    case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
+    case BFD_MACH_O_PPC_THREAD_STATE:      return "PPC_THREAD_STATE";
+    case BFD_MACH_O_PPC_FLOAT_STATE:       return "PPC_FLOAT_STATE";
+    case BFD_MACH_O_PPC_EXCEPTION_STATE:   return "PPC_EXCEPTION_STATE";
+    case BFD_MACH_O_PPC_VECTOR_STATE:      return "PPC_VECTOR_STATE";
+    case BFD_MACH_O_PPC_THREAD_STATE64:    return "PPC_THREAD_STATE64";
+    case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
     default: return "UNKNOWN";
     }
 }
 
 static int
     default: return "UNKNOWN";
     }
 }
 
 static int
-bfd_mach_o_scan_write_thread (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
-{
-  bfd_mach_o_thread_command *cmd = &command->command.thread;
-  unsigned int i;
-  unsigned char buf[8];
-  bfd_vma offset;
-  unsigned int nflavours;
-
-  BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
-             || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
-
-  offset = 8;
-  nflavours = 0;
-  for (i = 0; i < cmd->nflavours; i++)
-    {
-      BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
-      BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
-
-      bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
-      bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
-
-      bfd_seek (abfd, command->offset + offset, SEEK_SET);
-      if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
-       return -1;
-
-      offset += cmd->flavours[i].size + 8;
-    }
-
-  return 0;
-}
-
-static int
-bfd_mach_o_scan_read_dylinker (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+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_dylinker_command *cmd = &command->command.dylinker;
-  unsigned char buf[4];
+  struct mach_o_str_command_external raw;
   unsigned int nameoff;
   unsigned int nameoff;
-  asection *bfdsec;
-  char *sname;
-  const char *prefix;
 
   BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
              || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
 
 
   BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
              || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
 
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 4, abfd) != 4)
+  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 -1;
 
-  nameoff = bfd_h_get_32 (abfd, buf + 0);
+  nameoff = bfd_h_get_32 (abfd, raw.str);
 
   cmd->name_offset = command->offset + nameoff;
   cmd->name_len = command->len - nameoff;
 
   cmd->name_offset = command->offset + nameoff;
   cmd->name_len = command->len - nameoff;
-
-  if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
-    prefix = "LC_LOAD_DYLINKER";
-  else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
-    prefix = "LC_ID_DYLINKER";
-  else
-    abort ();
-
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
-  if (sname == NULL)
+  cmd->name_str = bfd_alloc (abfd, cmd->name_len);
+  if (cmd->name_str == NULL)
     return -1;
     return -1;
-  strcpy (sname, prefix);
-
-  bfdsec = bfd_make_section_anyway (abfd, sname);
-  if (bfdsec == NULL)
+  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 -1;
-
-  bfdsec->vma = 0;
-  bfdsec->lma = 0;
-  bfdsec->_raw_size = command->len - 8;
-  bfdsec->filepos = command->offset + 8;
-  bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
-
-  cmd->section = bfdsec;
-
   return 0;
 }
 
 static int
   return 0;
 }
 
 static int
-bfd_mach_o_scan_read_dylib (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylib_command *cmd = &command->command.dylib;
 {
   bfd_mach_o_dylib_command *cmd = &command->command.dylib;
-  unsigned char buf[16];
+  struct mach_o_dylib_command_external raw;
   unsigned int nameoff;
   unsigned int nameoff;
-  asection *bfdsec;
-  char *sname;
-  const char *prefix;
 
 
-  BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
-             || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
-             || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
+  switch (command->type)
+    {
+    case BFD_MACH_O_LC_LOAD_DYLIB:
+    case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
+    case BFD_MACH_O_LC_ID_DYLIB:
+    case BFD_MACH_O_LC_REEXPORT_DYLIB:
+    case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
+      break;
+    default:
+      BFD_FAIL ();
+      return -1;
+    }
 
 
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 16, abfd) != 16)
+  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 -1;
 
-  nameoff = bfd_h_get_32 (abfd, buf + 0);
-  cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
-  cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
-  cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
+  nameoff = bfd_h_get_32 (abfd, raw.name);
+  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;
   cmd->name_len = command->len - nameoff;
 
   cmd->name_offset = command->offset + nameoff;
   cmd->name_len = command->len - nameoff;
-
-  if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
-    prefix = "LC_LOAD_DYLIB";
-  else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
-    prefix = "LC_LOAD_WEAK_DYLIB";
-  else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
-    prefix = "LC_ID_DYLIB";
-  else
-    abort ();
-
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
-  if (sname == NULL)
+  cmd->name_str = bfd_alloc (abfd, cmd->name_len);
+  if (cmd->name_str == NULL)
     return -1;
     return -1;
-  strcpy (sname, prefix);
-
-  bfdsec = bfd_make_section_anyway (abfd, sname);
-  if (bfdsec == NULL)
+  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 -1;
-
-  bfdsec->vma = 0;
-  bfdsec->lma = 0;
-  bfdsec->_raw_size = command->len - 8;
-  bfdsec->filepos = command->offset + 8;
-  bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
-
-  cmd->section = bfdsec;
-
   return 0;
 }
 
 static int
   return 0;
 }
 
 static int
-bfd_mach_o_scan_read_prebound_dylib (abfd, command)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     bfd_mach_o_load_command *command ATTRIBUTE_UNUSED;
+bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
+                                bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
 {
   /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
 
 {
   /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
 
@@ -1077,42 +2366,38 @@ bfd_mach_o_scan_read_prebound_dylib (abfd, command)
 }
 
 static int
 }
 
 static int
-bfd_mach_o_scan_read_thread (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
 {
 {
-  bfd_mach_o_data_struct *mdata = NULL;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   bfd_mach_o_thread_command *cmd = &command->command.thread;
   bfd_mach_o_thread_command *cmd = &command->command.thread;
-  unsigned char buf[8];
-  bfd_vma offset;
+  unsigned int offset;
   unsigned int nflavours;
   unsigned int i;
 
   BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
              || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
 
   unsigned int nflavours;
   unsigned int i;
 
   BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
              || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
 
-  BFD_ASSERT (bfd_mach_o_valid (abfd));
-  mdata = abfd->tdata.mach_o_data;
-
+  /* Count the number of threads.  */
   offset = 8;
   nflavours = 0;
   while (offset != command->len)
     {
   offset = 8;
   nflavours = 0;
   while (offset != command->len)
     {
+      struct mach_o_thread_command_external raw;
+
       if (offset >= command->len)
        return -1;
 
       if (offset >= command->len)
        return -1;
 
-      bfd_seek (abfd, command->offset + offset, SEEK_SET);
-
-      if (bfd_bread ((PTR) buf, 8, abfd) != 8)
+      if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
+          || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
        return -1;
 
        return -1;
 
-      offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
+      offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
       nflavours++;
     }
 
       nflavours++;
     }
 
-  cmd->flavours =
-    ((bfd_mach_o_thread_flavour *)
-     bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour)));
+  /* Allocate threads.  */
+  cmd->flavours = bfd_alloc
+    (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
   if (cmd->flavours == NULL)
     return -1;
   cmd->nflavours = nflavours;
   if (cmd->flavours == NULL)
     return -1;
   cmd->nflavours = nflavours;
@@ -1121,21 +2406,22 @@ bfd_mach_o_scan_read_thread (abfd, command)
   nflavours = 0;
   while (offset != command->len)
     {
   nflavours = 0;
   while (offset != command->len)
     {
+      struct mach_o_thread_command_external raw;
+
       if (offset >= command->len)
        return -1;
 
       if (nflavours >= cmd->nflavours)
        return -1;
 
       if (offset >= command->len)
        return -1;
 
       if (nflavours >= cmd->nflavours)
        return -1;
 
-      bfd_seek (abfd, command->offset + offset, SEEK_SET);
-
-      if (bfd_bread ((PTR) buf, 8, abfd) != 8)
+      if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
+          || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
        return -1;
 
        return -1;
 
-      cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
-      cmd->flavours[nflavours].offset = command->offset + offset + 8;
-      cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
-      offset += cmd->flavours[nflavours].size + 8;
+      cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
+      cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
+      cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4;
+      offset += cmd->flavours[nflavours].size + sizeof (raw);
       nflavours++;
     }
 
       nflavours++;
     }
 
@@ -1151,9 +2437,11 @@ bfd_mach_o_scan_read_thread (abfd, command)
       switch (mdata->header.cputype)
        {
        case BFD_MACH_O_CPU_TYPE_POWERPC:
       switch (mdata->header.cputype)
        {
        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);
          break;
        case BFD_MACH_O_CPU_TYPE_I386:
          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);
          break;
        default:
          flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
          break;
        default:
@@ -1162,7 +2450,7 @@ bfd_mach_o_scan_read_thread (abfd, command)
        }
 
       snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
        }
 
       snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
-      sname = (char *) bfd_alloc (abfd, snamelen);
+      sname = bfd_alloc (abfd, snamelen);
       if (sname == NULL)
        return -1;
 
       if (sname == NULL)
        return -1;
 
@@ -1174,322 +2462,463 @@ bfd_mach_o_scan_read_thread (abfd, command)
          j++;
        }
 
          j++;
        }
 
-      bfdsec = bfd_make_section (abfd, sname);
+      bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
+
+      bfdsec->vma = 0;
+      bfdsec->lma = 0;
+      bfdsec->size = cmd->flavours[i].size;
+      bfdsec->filepos = cmd->flavours[i].offset;
+      bfdsec->alignment_power = 0x0;
+
+      cmd->section = bfdsec;
+    }
+
+  return 0;
+}
+
+static int
+bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+
+  BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
+
+  {
+    struct mach_o_dysymtab_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 -1;
+
+    cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
+    cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
+    cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym);
+    cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym);
+    cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym);
+    cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym);
+    cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff);
+    cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc);
+    cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff);
+    cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab);
+    cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff);
+    cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms);
+    cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff);
+    cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms);
+    cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff);
+    cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel);
+    cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff);
+    cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel);
+  }
+
+  if (cmd->nmodtab != 0)
+    {
+      unsigned int i;
+      int wide = bfd_mach_o_wide_p (abfd);
+      unsigned int module_len = wide ? 56 : 52;
+
+      cmd->dylib_module =
+        bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
+      if (cmd->dylib_module == NULL)
+        return -1;
+
+      if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
+        return -1;
+
+      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 -1;
+
+          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 int i;
 
 
-      bfdsec->vma = 0;
-      bfdsec->lma = 0;
-      bfdsec->_raw_size = cmd->flavours[i].size;
-      bfdsec->filepos = cmd->flavours[i].offset;
-      bfdsec->alignment_power = 0x0;
-      bfdsec->flags = SEC_HAS_CONTENTS;
+      cmd->dylib_toc = bfd_alloc
+        (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
+      if (cmd->dylib_toc == NULL)
+        return -1;
 
 
-      cmd->section = bfdsec;
-    }
+      if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
+        return -1;
 
 
-  return 0;
-}
+      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];
 
 
-static int
-bfd_mach_o_scan_write_symtab (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
-{
-  bfd_mach_o_symtab_command *seg = &command->command.symtab;
-  unsigned char buf[16];
+          if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+            return -1;
 
 
-  BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
+          toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
+          toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
+        }
+    }
 
 
-  bfd_h_put_32 (abfd, seg->symoff, buf);
-  bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
-  bfd_h_put_32 (abfd, seg->stroff, buf + 8);
-  bfd_h_put_32 (abfd, seg->strsize, buf + 12);
+  if (cmd->nindirectsyms != 0)
+    {
+      unsigned int i;
 
 
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
-    return -1;
+      cmd->indirect_syms = bfd_alloc
+        (abfd, cmd->nindirectsyms * sizeof (unsigned int));
+      if (cmd->indirect_syms == NULL)
+        return -1;
 
 
-  if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
-    return -1;
+      if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
+        return -1;
 
 
-  return 0;
-}
+      for (i = 0; i < cmd->nindirectsyms; i++)
+        {
+          unsigned char raw[4];
+          unsigned int *is = &cmd->indirect_syms[i];
 
 
-static int
-bfd_mach_o_scan_read_dysymtab (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
-{
-  bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
-  unsigned char buf[72];
+          if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
+            return -1;
 
 
-  BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
+          *is = bfd_h_get_32 (abfd, raw);
+        }
+    }
 
 
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 72, abfd) != 72)
-    return -1;
+  if (cmd->nextrefsyms != 0)
+    {
+      unsigned long v;
+      unsigned int i;
+
+      cmd->ext_refs = bfd_alloc
+        (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
+      if (cmd->ext_refs == NULL)
+        return -1;
+
+      if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
+        return -1;
+
+      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 -1;
+
+          /* 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;
+            }
+        }
+    }
 
 
-  seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
-  seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
-  seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
-  seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
-  seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
-  seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
-  seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
-  seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
-  seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
-  seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
-  seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
-  seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
-  seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
-  seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
-  seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
-  seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
-  seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
-  seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
+  if (mdata->dysymtab)
+    return -1;
+  mdata->dysymtab = cmd;
 
   return 0;
 }
 
 static int
 
   return 0;
 }
 
 static int
-bfd_mach_o_scan_read_symtab (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
 {
-  bfd_mach_o_symtab_command *seg = &command->command.symtab;
-  unsigned char buf[16];
-  asection *bfdsec;
-  char *sname;
-  const char *prefix = "LC_SYMTAB.stabs";
+  bfd_mach_o_symtab_command *symtab = &command->command.symtab;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  struct mach_o_symtab_command_external raw;
 
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
 
 
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
 
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 16, abfd) != 16)
+  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 -1;
 
-  seg->symoff = bfd_h_get_32 (abfd, buf);
-  seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
-  seg->stroff = bfd_h_get_32 (abfd, buf + 8);
-  seg->strsize = bfd_h_get_32 (abfd, buf + 12);
-  seg->symbols = NULL;
-  seg->strtab = NULL;
+  symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
+  symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
+  symtab->stroff = bfd_h_get_32 (abfd, raw.stroff);
+  symtab->strsize = bfd_h_get_32 (abfd, raw.strsize);
+  symtab->symbols = NULL;
+  symtab->strtab = NULL;
 
 
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
-  if (sname == NULL)
-    return -1;
-  strcpy (sname, prefix);
+  if (symtab->nsyms != 0)
+    abfd->flags |= HAS_SYMS;
 
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
-  if (bfdsec == NULL)
+  if (mdata->symtab)
     return -1;
     return -1;
+  mdata->symtab = symtab;
+  return 0;
+}
 
 
-  bfdsec->vma = 0;
-  bfdsec->lma = 0;
-  bfdsec->_raw_size = seg->nsyms * 12;
-  bfdsec->filepos = seg->symoff;
-  bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
+static int
+bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_uuid_command *cmd = &command->command.uuid;
 
 
-  seg->stabs_segment = bfdsec;
+  BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
 
 
-  prefix = "LC_SYMTAB.stabstr";
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
-  if (sname == NULL)
+  if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (cmd->uuid, 16, abfd) != 16)
     return -1;
     return -1;
-  strcpy (sname, prefix);
 
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
-  if (bfdsec == NULL)
-    return -1;
+  return 0;
+}
 
 
-  bfdsec->vma = 0;
-  bfdsec->lma = 0;
-  bfdsec->_raw_size = seg->strsize;
-  bfdsec->filepos = seg->stroff;
-  bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
+static int
+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;
 
 
-  seg->stabstr_segment = bfdsec;
+  if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return -1;
 
 
+  cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
+  cmd->datasize = bfd_get_32 (abfd, raw.datasize);
   return 0;
 }
 
 static int
   return 0;
 }
 
 static int
-bfd_mach_o_scan_read_segment (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
 {
 {
-  unsigned char buf[48];
-  bfd_mach_o_segment_command *seg = &command->command.segment;
-  unsigned long i;
-  asection *bfdsec;
-  char *sname;
-  const char *prefix = "LC_SEGMENT";
-  unsigned int snamelen;
-
-  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
+  bfd_mach_o_str_command *cmd = &command->command.str;
+  struct mach_o_str_command_external raw;
+  unsigned long off;
 
 
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 48, abfd) != 48)
+  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 -1;
 
-  memcpy (seg->segname, buf, 16);
-  seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
-  seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
-  seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
-  seg->filesize = bfd_h_get_32 (abfd, buf +  28);
-  /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
-  /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
-  seg->nsects = bfd_h_get_32 (abfd, buf + 40);
-  seg->flags = bfd_h_get_32 (abfd, buf + 44);
-
-  snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
-  sname = (char *) bfd_alloc (abfd, snamelen);
-  if (sname == NULL)
+  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 -1;
-  sprintf (sname, "%s.%s", prefix, seg->segname);
-
-  bfdsec = bfd_make_section_anyway (abfd, sname);
-  if (bfdsec == NULL)
+  if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
+      || bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len)
     return -1;
     return -1;
+  return 0;
+}
 
 
-  bfdsec->vma = seg->vmaddr;
-  bfdsec->lma = seg->vmaddr;
-  bfdsec->_raw_size = seg->filesize;
-  bfdsec->filepos = seg->fileoff;
-  bfdsec->alignment_power = 0x0;
-  bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
-
-  seg->segment = bfdsec;
-
-  if (seg->nsects != 0)
-    {
-      seg->sections =
-       ((bfd_mach_o_section *)
-        bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section)));
-      if (seg->sections == NULL)
-       return -1;
-
-      for (i = 0; i < seg->nsects; i++)
-       {
-         bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
+static int
+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 (bfd_mach_o_scan_read_section (abfd, &seg->sections[i],
-                                           segoff) != 0)
-           return -1;
-       }
-    }
+  if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return -1;
 
 
+  cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
+  cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
+  cmd->bind_off = bfd_get_32 (abfd, raw.bind_off);
+  cmd->bind_size = bfd_get_32 (abfd, raw.bind_size);
+  cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off);
+  cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size);
+  cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off);
+  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 0;
 }
 
+static bfd_boolean
+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 (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_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;
+}
+
 static int
 static int
-bfd_mach_o_scan_write_segment (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_read_segment (bfd *abfd,
+                         bfd_mach_o_load_command *command,
+                         unsigned int wide)
 {
 {
-  unsigned char buf[48];
   bfd_mach_o_segment_command *seg = &command->command.segment;
   unsigned long i;
 
   bfd_mach_o_segment_command *seg = &command->command.segment;
   unsigned long i;
 
-  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
+  if (wide)
+    {
+      struct mach_o_segment_command_64_external raw;
 
 
-  memcpy (buf, seg->segname, 16);
-  bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
-  bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
-  bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
-  bfd_h_put_32 (abfd, seg->filesize, buf + 28);
-  bfd_h_put_32 (abfd, 0 /* seg->maxprot */, buf + 32);
-  bfd_h_put_32 (abfd, 0 /* seg->initprot */, buf + 36);
-  bfd_h_put_32 (abfd, seg->nsects, buf + 40);
-  bfd_h_put_32 (abfd, seg->flags, buf + 44);
-
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
-    return -1;
+      BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
 
 
-  {
-    char buf[1024];
-    bfd_vma nbytes = seg->filesize;
-    bfd_vma curoff = seg->fileoff;
+      if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+          || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+        return -1;
 
 
-    while (nbytes > 0)
-      {
-       bfd_vma thisread = nbytes;
+      memcpy (seg->segname, raw.segname, 16);
+      seg->segname[16] = '\0';
+
+      seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr);
+      seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize);
+      seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff);
+      seg->filesize = bfd_h_get_64 (abfd, raw.filesize);
+      seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
+      seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
+      seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
+      seg->flags = bfd_h_get_32 (abfd, raw.flags);
+    }
+  else
+    {
+      struct mach_o_segment_command_32_external raw;
 
 
-       if (thisread > 1024)
-         thisread = 1024;
+      BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
 
 
-       bfd_seek (abfd, curoff, SEEK_SET);
-       if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
-         return -1;
+      if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+          || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+        return -1;
 
 
-       bfd_seek (abfd, curoff, SEEK_SET);
-       if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
-         return -1;
+      memcpy (seg->segname, raw.segname, 16);
+      seg->segname[16] = '\0';
 
 
-       nbytes -= thisread;
-       curoff += thisread;
-      }
-  }
+      seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr);
+      seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize);
+      seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff);
+      seg->filesize = bfd_h_get_32 (abfd, raw.filesize);
+      seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
+      seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
+      seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
+      seg->flags = bfd_h_get_32 (abfd, raw.flags);
+    }
+  seg->sect_head = NULL;
+  seg->sect_tail = NULL;
 
   for (i = 0; i < seg->nsects; i++)
     {
 
   for (i = 0; i < seg->nsects; i++)
     {
-      bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
+      bfd_vma segoff;
+      asection *sec;
 
 
-      if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
-       return -1;
+      if (wide)
+        segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
+          + (i * BFD_MACH_O_SECTION_64_SIZE);
+      else
+        segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
+          + (i * BFD_MACH_O_SECTION_SIZE);
+
+      sec = bfd_mach_o_read_section (abfd, segoff, seg->initprot, wide);
+      if (sec == NULL)
+        return -1;
+
+      bfd_mach_o_append_section_to_segment (seg, sec);
     }
 
   return 0;
 }
 
 static int
     }
 
   return 0;
 }
 
 static int
-bfd_mach_o_scan_read_command (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+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
+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
+bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
 {
 {
-  unsigned char buf[8];
+  struct mach_o_load_command_external raw;
+  unsigned int cmd;
 
 
-  bfd_seek (abfd, command->offset, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 8, abfd) != 8)
+  /* 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 -1;
 
-  command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
-  command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
-                           ? 1 : 0);
-  command->len = bfd_h_get_32 (abfd, buf + 4);
+  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->len = bfd_h_get_32 (abfd, raw.cmdsize);
 
   switch (command->type)
     {
     case BFD_MACH_O_LC_SEGMENT:
 
   switch (command->type)
     {
     case BFD_MACH_O_LC_SEGMENT:
-      if (bfd_mach_o_scan_read_segment (abfd, command) != 0)
+      if (bfd_mach_o_read_segment_32 (abfd, command) != 0)
+       return -1;
+      break;
+    case BFD_MACH_O_LC_SEGMENT_64:
+      if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
        return -1;
       break;
     case BFD_MACH_O_LC_SYMTAB:
        return -1;
       break;
     case BFD_MACH_O_LC_SYMTAB:
-      if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
+      if (bfd_mach_o_read_symtab (abfd, command) != 0)
        return -1;
       break;
     case BFD_MACH_O_LC_SYMSEG:
       break;
     case BFD_MACH_O_LC_THREAD:
     case BFD_MACH_O_LC_UNIXTHREAD:
        return -1;
       break;
     case BFD_MACH_O_LC_SYMSEG:
       break;
     case BFD_MACH_O_LC_THREAD:
     case BFD_MACH_O_LC_UNIXTHREAD:
-      if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
+      if (bfd_mach_o_read_thread (abfd, command) != 0)
        return -1;
       break;
     case BFD_MACH_O_LC_LOAD_DYLINKER:
     case BFD_MACH_O_LC_ID_DYLINKER:
        return -1;
       break;
     case BFD_MACH_O_LC_LOAD_DYLINKER:
     case BFD_MACH_O_LC_ID_DYLINKER:
-      if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
+      if (bfd_mach_o_read_dylinker (abfd, command) != 0)
        return -1;
       break;
     case BFD_MACH_O_LC_LOAD_DYLIB:
     case BFD_MACH_O_LC_ID_DYLIB:
     case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
        return -1;
       break;
     case BFD_MACH_O_LC_LOAD_DYLIB:
     case BFD_MACH_O_LC_ID_DYLIB:
     case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
-      if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
+    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;
       break;
     case BFD_MACH_O_LC_PREBOUND_DYLIB:
        return -1;
       break;
     case BFD_MACH_O_LC_PREBOUND_DYLIB:
-      if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
+      if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
        return -1;
       break;
     case BFD_MACH_O_LC_LOADFVMLIB:
        return -1;
       break;
     case BFD_MACH_O_LC_LOADFVMLIB:
@@ -1498,21 +2927,45 @@ bfd_mach_o_scan_read_command (abfd, command)
     case BFD_MACH_O_LC_FVMFILE:
     case BFD_MACH_O_LC_PREPAGE:
     case BFD_MACH_O_LC_ROUTINES:
     case BFD_MACH_O_LC_FVMFILE:
     case BFD_MACH_O_LC_PREPAGE:
     case BFD_MACH_O_LC_ROUTINES:
+    case BFD_MACH_O_LC_ROUTINES_64:
+      break;
     case BFD_MACH_O_LC_SUB_FRAMEWORK:
     case BFD_MACH_O_LC_SUB_FRAMEWORK:
+    case BFD_MACH_O_LC_SUB_UMBRELLA:
+    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;
       break;
     case BFD_MACH_O_LC_DYSYMTAB:
       break;
     case BFD_MACH_O_LC_DYSYMTAB:
-      if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
+      if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
        return -1;
       break;
        return -1;
       break;
-    case BFD_MACH_O_LC_SUB_UMBRELLA:
-    case BFD_MACH_O_LC_SUB_CLIENT:
-    case BFD_MACH_O_LC_SUB_LIBRARY:
     case BFD_MACH_O_LC_TWOLEVEL_HINTS:
     case BFD_MACH_O_LC_PREBIND_CKSUM:
       break;
     case BFD_MACH_O_LC_TWOLEVEL_HINTS:
     case BFD_MACH_O_LC_PREBIND_CKSUM:
       break;
+    case BFD_MACH_O_LC_UUID:
+      if (bfd_mach_o_read_uuid (abfd, command) != 0)
+       return -1;
+      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;
+      break;
+    case BFD_MACH_O_LC_DYLD_INFO:
+      if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
+       return -1;
+      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;
+      break;
     default:
     default:
-      fprintf (stderr, "unable to read unknown load command 0x%lx\n",
-              (unsigned long) command->type);
+      (*_bfd_error_handler)(_("%B: unable to read unknown load command 0x%lx"),
+         abfd, (unsigned long) command->type);
       break;
     }
 
       break;
     }
 
@@ -1520,18 +2973,19 @@ bfd_mach_o_scan_read_command (abfd, command)
 }
 
 static void
 }
 
 static void
-bfd_mach_o_flatten_sections (abfd)
-     bfd *abfd;
+bfd_mach_o_flatten_sections (bfd *abfd)
 {
 {
-  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   long csect = 0;
   long csect = 0;
-  unsigned long i, j;
+  unsigned long i;
 
 
+  /* Count total number of sections.  */
   mdata->nsects = 0;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
   mdata->nsects = 0;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
-      if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
+      if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
+         || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
        {
          bfd_mach_o_segment_command *seg;
 
        {
          bfd_mach_o_segment_command *seg;
 
@@ -1540,60 +2994,60 @@ bfd_mach_o_flatten_sections (abfd)
        }
     }
 
        }
     }
 
+  /* Allocate sections array.  */
   mdata->sections = bfd_alloc (abfd,
                               mdata->nsects * sizeof (bfd_mach_o_section *));
   mdata->sections = bfd_alloc (abfd,
                               mdata->nsects * sizeof (bfd_mach_o_section *));
+
+  /* Fill the array.  */
   csect = 0;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
   csect = 0;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
-      if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
+      if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
+         || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
        {
          bfd_mach_o_segment_command *seg;
        {
          bfd_mach_o_segment_command *seg;
+          bfd_mach_o_section *sec;
 
          seg = &mdata->commands[i].command.segment;
          BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
 
 
          seg = &mdata->commands[i].command.segment;
          BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
 
-         for (j = 0; j < seg->nsects; j++)
-           mdata->sections[csect++] = &seg->sections[j];
+          for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+           mdata->sections[csect++] = sec;
        }
     }
 }
 
        }
     }
 }
 
-int
-bfd_mach_o_scan_start_address (abfd)
-     bfd *abfd;
+static bfd_boolean
+bfd_mach_o_scan_start_address (bfd *abfd)
 {
 {
-  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   bfd_mach_o_thread_command *cmd = NULL;
   unsigned long i;
 
   for (i = 0; i < mdata->header.ncmds; i++)
   bfd_mach_o_thread_command *cmd = NULL;
   unsigned long i;
 
   for (i = 0; i < mdata->header.ncmds; i++)
-    {
-      if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
-         (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
-       {
-         if (cmd == NULL)
-           cmd = &mdata->commands[i].command.thread;
-         else
-           return 0;
-       }
-    }
+    if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
+        (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
+      {
+        cmd = &mdata->commands[i].command.thread;
+        break;
+      }
 
   if (cmd == NULL)
 
   if (cmd == NULL)
-    return 0;
+    return FALSE;
 
 
+  /* FIXME: create a subtarget hook ?  */
   for (i = 0; i < cmd->nflavours; i++)
     {
       if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
          && (cmd->flavours[i].flavour
   for (i = 0; i < cmd->nflavours; i++)
     {
       if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
          && (cmd->flavours[i].flavour
-             == (unsigned long) BFD_MACH_O_i386_THREAD_STATE))
+             == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
        {
          unsigned char buf[4];
 
        {
          unsigned char buf[4];
 
-         bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
-
-         if (bfd_bread (buf, 4, abfd) != 4)
-           return -1;
+         if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
+              || 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);
        }
@@ -1602,96 +3056,139 @@ bfd_mach_o_scan_start_address (abfd)
        {
          unsigned char buf[4];
 
        {
          unsigned char buf[4];
 
-         bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
-
-         if (bfd_bread (buf, 4, abfd) != 4)
-           return -1;
+         if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
+              || 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);
        }
+      else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
+               && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
+        {
+          unsigned char buf[8];
+
+          if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
+              || bfd_bread (buf, 8, abfd) != 8)
+            return FALSE;
+
+          abfd->start_address = bfd_h_get_64 (abfd, buf);
+        }
+      else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
+               && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
+        {
+          unsigned char buf[8];
+
+          if (bfd_seek (abfd, cmd->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);
+        }
     }
 
     }
 
-  return 0;
+  return TRUE;
 }
 
 }
 
-int
-bfd_mach_o_scan (abfd, header, mdata)
-     bfd *abfd;
-     bfd_mach_o_header *header;
-     bfd_mach_o_data_struct *mdata;
+bfd_boolean
+bfd_mach_o_set_arch_mach (bfd *abfd,
+                          enum bfd_architecture arch,
+                          unsigned long machine)
+{
+  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+
+  /* If this isn't the right architecture for this backend, and this
+     isn't the generic backend, fail.  */
+  if (arch != bed->arch
+      && arch != bfd_arch_unknown
+      && bed->arch != bfd_arch_unknown)
+    return FALSE;
+
+  return bfd_default_set_arch_mach (abfd, arch, machine);
+}
+
+static bfd_boolean
+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;
 {
   unsigned int i;
   enum bfd_architecture cputype;
   unsigned long cpusubtype;
+  unsigned int hdrsize;
+
+  hdrsize = mach_o_wide_p (header) ?
+    BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
 
   mdata->header = *header;
 
   mdata->header = *header;
-  mdata->symbols = NULL;
 
 
-  abfd->flags = (abfd->xvec->object_flags
-                | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
+  abfd->flags = abfd->flags & BFD_IN_MEMORY;
+  switch (header->filetype)
+    {
+    case BFD_MACH_O_MH_OBJECT:
+      abfd->flags |= HAS_RELOC;
+      break;
+    case BFD_MACH_O_MH_EXECUTE:
+      abfd->flags |= EXEC_P;
+      break;
+    case BFD_MACH_O_MH_DYLIB:
+    case BFD_MACH_O_MH_BUNDLE:
+      abfd->flags |= DYNAMIC;
+      break;
+    }
+
   abfd->tdata.mach_o_data = mdata;
 
   bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
                                   &cputype, &cpusubtype);
   if (cputype == bfd_arch_unknown)
     {
   abfd->tdata.mach_o_data = mdata;
 
   bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
                                   &cputype, &cpusubtype);
   if (cputype == bfd_arch_unknown)
     {
-      fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
-              header->cputype, header->cpusubtype);
-      return -1;
+      (*_bfd_error_handler)
+        (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
+         header->cputype, header->cpusubtype);
+      return FALSE;
     }
 
   bfd_set_arch_mach (abfd, cputype, cpusubtype);
 
   if (header->ncmds != 0)
     {
     }
 
   bfd_set_arch_mach (abfd, cputype, cpusubtype);
 
   if (header->ncmds != 0)
     {
-      mdata->commands =
-       ((bfd_mach_o_load_command *)
-        bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command)));
+      mdata->commands = bfd_alloc
+        (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
       if (mdata->commands == NULL)
       if (mdata->commands == NULL)
-       return -1;
+       return FALSE;
 
       for (i = 0; i < header->ncmds; i++)
        {
          bfd_mach_o_load_command *cur = &mdata->commands[i];
 
          if (i == 0)
 
       for (i = 0; i < header->ncmds; i++)
        {
          bfd_mach_o_load_command *cur = &mdata->commands[i];
 
          if (i == 0)
-           cur->offset = 28;
+           cur->offset = hdrsize;
          else
            {
              bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
              cur->offset = prev->offset + prev->len;
            }
 
          else
            {
              bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
              cur->offset = prev->offset + prev->len;
            }
 
-         if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
-           return -1;
+         if (bfd_mach_o_read_command (abfd, cur) < 0)
+           return FALSE;
        }
     }
 
   if (bfd_mach_o_scan_start_address (abfd) < 0)
        }
     }
 
   if (bfd_mach_o_scan_start_address (abfd) < 0)
-    {
-#if 0
-      fprintf (stderr, "bfd_mach_o_scan: unable to scan start address: %s\n",
-              bfd_errmsg (bfd_get_error ()));
-      abfd->tdata.mach_o_data = NULL;
-      return -1;
-#endif
-    }
+    return FALSE;
 
   bfd_mach_o_flatten_sections (abfd);
 
   bfd_mach_o_flatten_sections (abfd);
-
-  return 0;
+  return TRUE;
 }
 
 }
 
-boolean
-bfd_mach_o_mkobject (abfd)
-     bfd *abfd;
+bfd_boolean
+bfd_mach_o_mkobject_init (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = NULL;
 
 {
   bfd_mach_o_data_struct *mdata = NULL;
 
-  mdata = ((bfd_mach_o_data_struct *)
-          bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct)));
+  mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
   if (mdata == NULL)
   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;
@@ -1703,80 +3200,49 @@ bfd_mach_o_mkobject (abfd)
   mdata->header.flags = 0;
   mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
   mdata->commands = NULL;
   mdata->header.flags = 0;
   mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
   mdata->commands = NULL;
-  mdata->nsymbols = 0;
-  mdata->symbols = NULL;
   mdata->nsects = 0;
   mdata->sections = NULL;
   mdata->nsects = 0;
   mdata->sections = NULL;
-  mdata->ibfd = NULL;
+  mdata->dyn_reloc_cache = NULL;
 
 
-  return true;
+  return TRUE;
 }
 
 }
 
-const bfd_target *
-bfd_mach_o_object_p (abfd)
-     bfd *abfd;
+static bfd_boolean
+bfd_mach_o_gen_mkobject (bfd *abfd)
 {
 {
-  struct bfd_preserve preserve;
-  bfd_mach_o_header header;
-
-  preserve.marker = NULL;
-  if (bfd_mach_o_read_header (abfd, &header) != 0)
-    goto wrong;
-
-  if (! (header.byteorder == BFD_ENDIAN_BIG
-        || header.byteorder == BFD_ENDIAN_LITTLE))
-    {
-      fprintf (stderr, "unknown header byte-order value 0x%lx\n",
-              (long) header.byteorder);
-      goto wrong;
-    }
-
-  if (! ((header.byteorder == BFD_ENDIAN_BIG
-         && abfd->xvec->byteorder == BFD_ENDIAN_BIG
-         && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
-        || (header.byteorder == BFD_ENDIAN_LITTLE
-            && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
-            && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
-    goto wrong;
-
-  preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
-  if (preserve.marker == NULL
-      || !bfd_preserve_save (abfd, &preserve))
-    goto fail;
-
-  if (bfd_mach_o_scan (abfd, &header,
-                      (bfd_mach_o_data_struct *) preserve.marker) != 0)
-    goto wrong;
+  bfd_mach_o_data_struct *mdata;
 
 
-  bfd_preserve_finish (abfd, &preserve);
-  return abfd->xvec;
+  if (!bfd_mach_o_mkobject_init (abfd))
+    return FALSE;
 
 
- wrong:
-  bfd_set_error (bfd_error_wrong_format);
+  mdata = bfd_mach_o_get_data (abfd);
+  mdata->header.magic = BFD_MACH_O_MH_MAGIC;
+  mdata->header.cputype = 0;
+  mdata->header.cpusubtype = 0;
+  mdata->header.byteorder = abfd->xvec->byteorder;
+  mdata->header.version = 1;
 
 
- fail:
-  if (preserve.marker != NULL)
-    bfd_preserve_restore (abfd, &preserve);
-  return NULL;
+  return TRUE;
 }
 
 const bfd_target *
 }
 
 const bfd_target *
-bfd_mach_o_core_p (abfd)
-     bfd *abfd;
+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;
 
   preserve.marker = NULL;
 {
   struct bfd_preserve preserve;
   bfd_mach_o_header header;
 
   preserve.marker = NULL;
-  if (bfd_mach_o_read_header (abfd, &header) != 0)
+  if (!bfd_mach_o_read_header (abfd, &header))
     goto wrong;
 
   if (! (header.byteorder == BFD_ENDIAN_BIG
         || header.byteorder == BFD_ENDIAN_LITTLE))
     {
     goto wrong;
 
   if (! (header.byteorder == BFD_ENDIAN_BIG
         || header.byteorder == BFD_ENDIAN_LITTLE))
     {
-      fprintf (stderr, "unknown header byte-order value 0x%lx\n",
-              (long) header.byteorder);
-      abort ();
+      (*_bfd_error_handler) (_("unknown header byte-order value 0x%lx"),
+                            (unsigned long) header.byteorder);
+      goto wrong;
     }
 
   if (! ((header.byteorder == BFD_ENDIAN_BIG
     }
 
   if (! ((header.byteorder == BFD_ENDIAN_BIG
@@ -1787,16 +3253,38 @@ bfd_mach_o_core_p (abfd)
             && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
     goto wrong;
 
             && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
     goto wrong;
 
-  if (header.filetype != BFD_MACH_O_MH_CORE)
-    goto wrong;
+  /* Check cputype and filetype.
+     In case of wildcard, do not accept magics that are handled by existing
+     targets.  */
+  if (cputype)
+    {
+      if (header.cputype != cputype)
+        goto wrong;
+    }
+  if (filetype)
+    {
+      if (header.filetype != filetype)
+        goto wrong;
+    }
+  else
+    {
+      switch (header.filetype)
+        {
+        case BFD_MACH_O_MH_CORE:
+          /* Handled by core_p */
+          goto wrong;
+        default:
+          break;
+        }
+    }
 
   preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
   if (preserve.marker == NULL
       || !bfd_preserve_save (abfd, &preserve))
     goto fail;
 
 
   preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
   if (preserve.marker == NULL
       || !bfd_preserve_save (abfd, &preserve))
     goto fail;
 
-  if (bfd_mach_o_scan (abfd, &header,
-                      (bfd_mach_o_data_struct *) preserve.marker) != 0)
+  if (!bfd_mach_o_scan (abfd, &header,
+                        (bfd_mach_o_data_struct *) preserve.marker))
     goto wrong;
 
   bfd_preserve_finish (abfd, &preserve);
     goto wrong;
 
   bfd_preserve_finish (abfd, &preserve);
@@ -1811,6 +3299,18 @@ bfd_mach_o_core_p (abfd)
   return NULL;
 }
 
   return NULL;
 }
 
+static const bfd_target *
+bfd_mach_o_gen_object_p (bfd *abfd)
+{
+  return bfd_mach_o_header_p (abfd, 0, 0);
+}
+
+static const bfd_target *
+bfd_mach_o_gen_core_p (bfd *abfd)
+{
+  return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
+}
+
 typedef struct mach_o_fat_archentry
 {
   unsigned long cputype;
 typedef struct mach_o_fat_archentry
 {
   unsigned long cputype;
@@ -1818,7 +3318,6 @@ typedef struct mach_o_fat_archentry
   unsigned long offset;
   unsigned long size;
   unsigned long align;
   unsigned long offset;
   unsigned long size;
   unsigned long align;
-  bfd *abfd;
 } mach_o_fat_archentry;
 
 typedef struct mach_o_fat_data_struct
 } mach_o_fat_archentry;
 
 typedef struct mach_o_fat_data_struct
@@ -1829,44 +3328,45 @@ typedef struct mach_o_fat_data_struct
 } mach_o_fat_data_struct;
 
 const bfd_target *
 } mach_o_fat_data_struct;
 
 const bfd_target *
-bfd_mach_o_archive_p (abfd)
-     bfd *abfd;
+bfd_mach_o_archive_p (bfd *abfd)
 {
   mach_o_fat_data_struct *adata = NULL;
 {
   mach_o_fat_data_struct *adata = NULL;
-  unsigned char buf[20];
+  struct mach_o_fat_header_external hdr;
   unsigned long i;
 
   unsigned long i;
 
-  bfd_seek (abfd, 0, SEEK_SET);
-  if (bfd_bread ((PTR) buf, 8, abfd) != 8)
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0
+      || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
     goto error;
 
     goto error;
 
-  adata = (mach_o_fat_data_struct *)
-    bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
+  adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
   if (adata == NULL)
     goto error;
 
   if (adata == NULL)
     goto error;
 
-  adata->magic = bfd_getb32 (buf);
-  adata->nfat_arch = bfd_getb32 (buf + 4);
+  adata->magic = bfd_getb32 (hdr.magic);
+  adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
   if (adata->magic != 0xcafebabe)
     goto error;
   if (adata->magic != 0xcafebabe)
     goto error;
+  /* Avoid matching Java bytecode files, which have the same magic number.
+     In the Java bytecode file format this field contains the JVM version,
+     which starts at 43.0.  */
+  if (adata->nfat_arch > 30)
+    goto error;
 
 
-  adata->archentries = (mach_o_fat_archentry *)
+  adata->archentries =
     bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
   if (adata->archentries == NULL)
     goto error;
 
   for (i = 0; i < adata->nfat_arch; i++)
     {
     bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
   if (adata->archentries == NULL)
     goto error;
 
   for (i = 0; i < adata->nfat_arch; i++)
     {
-      bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
-
-      if (bfd_bread ((PTR) buf, 20, abfd) != 20)
+      struct mach_o_fat_arch_external arch;
+      if (bfd_bread (&arch, sizeof (arch), abfd) != sizeof (arch))
        goto error;
        goto error;
-      adata->archentries[i].cputype = bfd_getb32 (buf);
-      adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
-      adata->archentries[i].offset = bfd_getb32 (buf + 8);
-      adata->archentries[i].size = bfd_getb32 (buf + 12);
-      adata->archentries[i].align = bfd_getb32 (buf + 16);
-      adata->archentries[i].abfd = NULL;
+      adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
+      adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
+      adata->archentries[i].offset = bfd_getb32 (arch.offset);
+      adata->archentries[i].size = bfd_getb32 (arch.size);
+      adata->archentries[i].align = bfd_getb32 (arch.align);
     }
 
   abfd->tdata.mach_o_fat_data = adata;
     }
 
   abfd->tdata.mach_o_fat_data = adata;
@@ -1880,13 +3380,14 @@ bfd_mach_o_archive_p (abfd)
 }
 
 bfd *
 }
 
 bfd *
-bfd_mach_o_openr_next_archived_file (archive, prev)
-  bfd *archive;
-  bfd *prev;
+bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
 {
   mach_o_fat_data_struct *adata;
   mach_o_fat_archentry *entry = NULL;
   unsigned long i;
 {
   mach_o_fat_data_struct *adata;
   mach_o_fat_archentry *entry = NULL;
   unsigned long i;
+  bfd *nbfd;
+  enum bfd_architecture arch_type;
+  unsigned long arch_subtype;
 
   adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
   BFD_ASSERT (adata != NULL);
 
   adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
   BFD_ASSERT (adata != NULL);
@@ -1898,7 +3399,7 @@ bfd_mach_o_openr_next_archived_file (archive, prev)
     {
       for (i = 0; i < adata->nfat_arch; i++)
        {
     {
       for (i = 0; i < adata->nfat_arch; i++)
        {
-         if (adata->archentries[i].abfd == prev)
+         if (adata->archentries[i].offset == prev->origin)
            break;
        }
 
            break;
        }
 
@@ -1918,90 +3419,93 @@ bfd_mach_o_openr_next_archived_file (archive, prev)
     }
 
   entry = &adata->archentries[i];
     }
 
   entry = &adata->archentries[i];
-  if (entry->abfd == NULL)
-    {
-      bfd *nbfd = _bfd_new_bfd_contained_in (archive);
-      char *s = NULL;
+  nbfd = _bfd_new_bfd_contained_in (archive);
+  if (nbfd == NULL)
+    return NULL;
 
 
-      if (nbfd == NULL)
-       return NULL;
+  nbfd->origin = entry->offset;
 
 
-      nbfd->origin = entry->offset;
-      s = bfd_malloc (strlen (archive->filename) + 1);
-      if (s == NULL)
-       return NULL;
-      strcpy (s, archive->filename);
-      nbfd->filename = s;
-      nbfd->iostream = NULL;
-      entry->abfd = nbfd;
-    }
+  bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
+                                  &arch_type, &arch_subtype);
 
 
-  return entry->abfd;
+  /* Create the member filename. Use ARCH_NAME.  */
+  nbfd->filename = bfd_printable_arch_mach (arch_type, arch_subtype);
+  nbfd->iostream = NULL;
+  bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
+
+  return nbfd;
 }
 
 }
 
-int
-bfd_mach_o_lookup_section (abfd, section, mcommand, msection)
-     bfd *abfd;
-     asection *section;
-     bfd_mach_o_load_command **mcommand;
-     bfd_mach_o_section **msection;
+/* If ABFD format is FORMAT and architecture is ARCH, return it.
+   If ABFD is a fat image containing a member that corresponds to FORMAT
+   and ARCH, returns it.
+   In other case, returns NULL.
+   This function allows transparent uses of fat images.  */
+bfd *
+bfd_mach_o_fat_extract (bfd *abfd,
+                       bfd_format format,
+                       const bfd_arch_info_type *arch)
 {
 {
-  struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
-  unsigned int i, j, num;
+  bfd *res;
+  mach_o_fat_data_struct *adata;
+  unsigned int i;
 
 
-  bfd_mach_o_load_command *ncmd = NULL;
-  bfd_mach_o_section *nsect = NULL;
+  if (bfd_check_format (abfd, format))
+    {
+      if (bfd_get_arch_info (abfd) == arch)
+       return abfd;
+      return NULL;
+    }
+  if (!bfd_check_format (abfd, bfd_archive)
+      || abfd->xvec != &mach_o_fat_vec)
+    return NULL;
 
 
-  BFD_ASSERT (mcommand != NULL);
-  BFD_ASSERT (msection != NULL);
+  /* This is a Mach-O fat image.  */
+  adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
+  BFD_ASSERT (adata != NULL);
 
 
-  num = 0;
-  for (i = 0; i < md->header.ncmds; i++)
+  for (i = 0; i < adata->nfat_arch; i++)
     {
     {
-      struct bfd_mach_o_load_command *cmd = &md->commands[i];
-      struct bfd_mach_o_segment_command *seg = NULL;
+      struct mach_o_fat_archentry *e = &adata->archentries[i];
+      enum bfd_architecture cpu_type;
+      unsigned long cpu_subtype;
 
 
-      if (cmd->type != BFD_MACH_O_LC_SEGMENT)
+      bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
+                                      &cpu_type, &cpu_subtype);
+      if (cpu_type != arch->arch || cpu_subtype != arch->mach)
        continue;
        continue;
-      seg = &cmd->command.segment;
 
 
-      if (seg->segment == section)
-       {
-         if (num == 0)
-           ncmd = cmd;
-         num++;
-       }
+      /* The architecture is found.  */
+      res = _bfd_new_bfd_contained_in (abfd);
+      if (res == NULL)
+       return NULL;
 
 
-      for (j = 0; j < seg->nsects; j++)
-       {
-         struct bfd_mach_o_section *sect = &seg->sections[j];
+      res->origin = e->offset;
 
 
-         if (sect->bfdsection == section)
-           {
-             if (num == 0)
-               nsect = sect;
-             num++;
-           }
+      res->filename = bfd_printable_arch_mach (cpu_type, cpu_subtype);
+      res->iostream = NULL;
+
+      if (bfd_check_format (res, format))
+       {
+         BFD_ASSERT (bfd_get_arch_info (res) == arch);
+         return res;
        }
        }
+      bfd_close (res);
+      return NULL;
     }
 
     }
 
-  *mcommand = ncmd;
-  *msection = nsect;
-  return num;
+  return NULL;
 }
 
 int
 }
 
 int
-bfd_mach_o_lookup_command (abfd, type, mcommand)
-     bfd *abfd;
-     bfd_mach_o_load_command_type type;
-     bfd_mach_o_load_command **mcommand;
+bfd_mach_o_lookup_command (bfd *abfd,
+                          bfd_mach_o_load_command_type type,
+                          bfd_mach_o_load_command **mcommand)
 {
 {
-  struct mach_o_data_struct *md = NULL;
+  struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
   bfd_mach_o_load_command *ncmd = NULL;
   unsigned int i, num;
 
   bfd_mach_o_load_command *ncmd = NULL;
   unsigned int i, num;
 
-  md = abfd->tdata.mach_o_data;
-
   BFD_ASSERT (md != NULL);
   BFD_ASSERT (mcommand != NULL);
 
   BFD_ASSERT (md != NULL);
   BFD_ASSERT (mcommand != NULL);
 
@@ -2023,8 +3527,7 @@ bfd_mach_o_lookup_command (abfd, type, mcommand)
 }
 
 unsigned long
 }
 
 unsigned long
-bfd_mach_o_stack_addr (type)
-     enum bfd_mach_o_cpu_type type;
+bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
 {
   switch (type)
     {
 {
   switch (type)
     {
@@ -2047,13 +3550,78 @@ bfd_mach_o_stack_addr (type)
     }
 }
 
     }
 }
 
+const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
+{
+  { "regular", BFD_MACH_O_S_REGULAR},
+  { "zerofill", BFD_MACH_O_S_ZEROFILL},
+  { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
+  { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
+  { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
+  { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
+  { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
+  { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
+  { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
+  { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
+  { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
+  { "coalesced", BFD_MACH_O_S_COALESCED},
+  { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
+  { "interposing", BFD_MACH_O_S_INTERPOSING},
+  { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
+  { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
+  { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
+  { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
+  { NULL, 0}
+};
+
+const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
+{
+  { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
+  { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
+  { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
+  { "debug", BFD_MACH_O_S_ATTR_DEBUG },
+  { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
+  { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
+  { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
+  { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
+  { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
+  { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
+  { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
+  { NULL, 0}
+};
+
+/* Get the section type from NAME.  Return 256 if NAME is unknown.  */
+
+unsigned int
+bfd_mach_o_get_section_type_from_name (const char *name)
+{
+  const bfd_mach_o_xlat_name *x;
+
+  for (x = bfd_mach_o_section_type_name; x->name; x++)
+    if (strcmp (x->name, name) == 0)
+      return x->val;
+  /* Maximum section ID = 0xff.  */
+  return 256;
+}
+
+/* Get the section attribute from NAME.  Return -1 if NAME is unknown.  */
+
+unsigned int
+bfd_mach_o_get_section_attribute_from_name (const char *name)
+{
+  const bfd_mach_o_xlat_name *x;
+
+  for (x = bfd_mach_o_section_attribute_name; x->name; x++)
+    if (strcmp (x->name, name) == 0)
+      return x->val;
+  return (unsigned int)-1;
+}
+
 int
 int
-bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
-     bfd *abfd;
-     unsigned char **rbuf;
-     unsigned int *rlen;
+bfd_mach_o_core_fetch_environment (bfd *abfd,
+                                  unsigned char **rbuf,
+                                  unsigned int *rlen)
 {
 {
-  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
   unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
   unsigned int i = 0;
 
   unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
   unsigned int i = 0;
 
@@ -2083,13 +3651,23 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
              if (size > (end - start))
                size = (end - start);
 
              if (size > (end - start))
                size = (end - start);
 
-             buf = bfd_realloc (buf, size);
+             buf = bfd_realloc_or_free (buf, size);
+             if (buf == NULL)
+               return -1;
+
+             if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
+                {
+                  free (buf);
+                  return -1;
+                }
 
 
-             bfd_seek (abfd, end - size, SEEK_SET);
              nread = bfd_bread (buf, size, abfd);
 
              if (nread != size)
              nread = bfd_bread (buf, size, abfd);
 
              if (nread != size)
-               return -1;
+               {
+                 free (buf);
+                 return -1;
+               }
 
              for (offset = 4; offset <= size; offset += 4)
                {
 
              for (offset = 4; offset <= size; offset += 4)
                {
@@ -2112,6 +3690,7 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
                      *rlen = top - bottom;
 
                      memcpy (*rbuf, buf + size - *rlen, *rlen);
                      *rlen = top - bottom;
 
                      memcpy (*rbuf, buf + size - *rlen, *rlen);
+                     free (buf);
                      return 0;
                    }
                }
                      return 0;
                    }
                }
@@ -2121,6 +3700,8 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
 
              size *= 2;
            }
 
              size *= 2;
            }
+
+         free (buf);
        }
     }
 
        }
     }
 
@@ -2128,8 +3709,7 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
 }
 
 char *
 }
 
 char *
-bfd_mach_o_core_file_failing_command (abfd)
-     bfd *abfd;
+bfd_mach_o_core_file_failing_command (bfd *abfd)
 {
   unsigned char *buf = NULL;
   unsigned int len = 0;
 {
   unsigned char *buf = NULL;
   unsigned int len = 0;
@@ -2139,56 +3719,131 @@ bfd_mach_o_core_file_failing_command (abfd)
   if (ret < 0)
     return NULL;
 
   if (ret < 0)
     return NULL;
 
-  return buf;
+  return (char *) buf;
 }
 
 int
 }
 
 int
-bfd_mach_o_core_file_failing_signal (abfd)
-     bfd *abfd ATTRIBUTE_UNUSED;
+bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
 {
   return 0;
 }
 
 {
   return 0;
 }
 
-boolean
-bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
-     bfd *core_bfd ATTRIBUTE_UNUSED;
-     bfd *exec_bfd ATTRIBUTE_UNUSED;
+bfd_boolean
+bfd_mach_o_find_nearest_line (bfd *abfd,
+                             asection *section,
+                             asymbol **symbols,
+                             bfd_vma offset,
+                             const char **filename_ptr,
+                             const char **functionname_ptr,
+                             unsigned int *line_ptr)
+{
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  /* TODO: Handle executables and dylibs by using dSYMs. */
+  if (mdata->header.filetype != BFD_MACH_O_MH_OBJECT)
+    return FALSE;
+  if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
+                                    section, symbols, offset,
+                                    filename_ptr, functionname_ptr,
+                                    line_ptr, 0,
+                                    &mdata->dwarf2_find_line_info))
+    return TRUE;
+  return FALSE;
+}
+
+bfd_boolean
+bfd_mach_o_close_and_cleanup (bfd *abfd)
+{
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  if (bfd_get_format (abfd) == bfd_object && mdata != NULL)
+    {
+      _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
+      bfd_mach_o_free_cached_info (abfd);
+    }
+
+  return _bfd_generic_close_and_cleanup (abfd);
+}
+
+bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 {
 {
-  return true;
+  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+  asection *asect;
+  free (mdata->dyn_reloc_cache);
+  mdata->dyn_reloc_cache = NULL;
+  for (asect = abfd->sections; asect != NULL; asect = asect->next)
+    {
+      free (asect->relocation);
+      asect->relocation = NULL;
+    }
+
+  return TRUE;
 }
 
 }
 
-#define TARGET_NAME mach_o_be_vec
-#define TARGET_STRING "mach-o-be"
-#define TARGET_BIG_ENDIAN 1
-#define TARGET_ARCHIVE 0
+#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
+#define bfd_mach_o_swap_reloc_out NULL
+#define bfd_mach_o_print_thread NULL
+#define bfd_mach_o_tgt_seg_table NULL
+
+#define TARGET_NAME            mach_o_be_vec
+#define TARGET_STRING          "mach-o-be"
+#define TARGET_ARCHITECTURE    bfd_arch_unknown
+#define TARGET_BIG_ENDIAN      1
+#define TARGET_ARCHIVE                 0
+#define TARGET_PRIORITY                1
 #include "mach-o-target.c"
 
 #undef TARGET_NAME
 #undef TARGET_STRING
 #include "mach-o-target.c"
 
 #undef TARGET_NAME
 #undef TARGET_STRING
+#undef TARGET_ARCHITECTURE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
+#undef TARGET_PRIORITY
 
 
-#define TARGET_NAME mach_o_le_vec
-#define TARGET_STRING "mach-o-le"
-#define TARGET_BIG_ENDIAN 0
-#define TARGET_ARCHIVE 0
+#define TARGET_NAME            mach_o_le_vec
+#define TARGET_STRING          "mach-o-le"
+#define TARGET_ARCHITECTURE    bfd_arch_unknown
+#define TARGET_BIG_ENDIAN      0
+#define TARGET_ARCHIVE                 0
+#define TARGET_PRIORITY                1
 
 #include "mach-o-target.c"
 
 #undef TARGET_NAME
 #undef TARGET_STRING
 
 #include "mach-o-target.c"
 
 #undef TARGET_NAME
 #undef TARGET_STRING
+#undef TARGET_ARCHITECTURE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
-
-#define TARGET_NAME mach_o_fat_vec
-#define TARGET_STRING "mach-o-fat"
-#define TARGET_BIG_ENDIAN 1
-#define TARGET_ARCHIVE 1
+#undef TARGET_PRIORITY
+
+/* Not yet handled: creating an archive.  */
+#define bfd_mach_o_mkarchive                      _bfd_noarchive_mkarchive
+
+/* Not used.  */
+#define bfd_mach_o_read_ar_hdr                    _bfd_noarchive_read_ar_hdr
+#define bfd_mach_o_write_ar_hdr                   _bfd_noarchive_write_ar_hdr
+#define bfd_mach_o_slurp_armap                    _bfd_noarchive_slurp_armap
+#define bfd_mach_o_slurp_extended_name_table      _bfd_noarchive_slurp_extended_name_table
+#define bfd_mach_o_construct_extended_name_table  _bfd_noarchive_construct_extended_name_table
+#define bfd_mach_o_truncate_arname                _bfd_noarchive_truncate_arname
+#define bfd_mach_o_write_armap                    _bfd_noarchive_write_armap
+#define bfd_mach_o_get_elt_at_index               _bfd_noarchive_get_elt_at_index
+#define bfd_mach_o_generic_stat_arch_elt          _bfd_noarchive_generic_stat_arch_elt
+#define bfd_mach_o_update_armap_timestamp         _bfd_noarchive_update_armap_timestamp
+
+#define TARGET_NAME            mach_o_fat_vec
+#define TARGET_STRING          "mach-o-fat"
+#define TARGET_ARCHITECTURE    bfd_arch_unknown
+#define TARGET_BIG_ENDIAN      1
+#define TARGET_ARCHIVE                 1
+#define TARGET_PRIORITY                0
 
 #include "mach-o-target.c"
 
 #undef TARGET_NAME
 #undef TARGET_STRING
 
 #include "mach-o-target.c"
 
 #undef TARGET_NAME
 #undef TARGET_STRING
+#undef TARGET_ARCHITECTURE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
+#undef TARGET_PRIORITY
This page took 0.075478 seconds and 4 git commands to generate.