New "find" command.
[deliverable/binutils-gdb.git] / bfd / mach-o.c
index 4c4736ada0d14acf989e0d14c03697d66a972021..bde89b04034cc78110a70aa277b2c126f9e3c053 100644 (file)
@@ -1,12 +1,12 @@
 /* Mach-O support for BFD.
-   Copyright 1999, 2000, 2001, 2002
+   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
    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,
 
    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 "sysdep.h"
 #include "libbfd.h"
 #include "libiberty.h"
 #include <ctype.h>
 #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_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_bfd_is_target_special_symbol       ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
+#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_find_inliner_info                  _bfd_nosymbols_find_inliner_info
+#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_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 bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data
-  PARAMS ((bfd *, asymbol *, bfd *, asymbol *));
-static bfd_boolean bfd_mach_o_bfd_copy_private_section_data
-  PARAMS ((bfd *, asection *, bfd *, asection *));
-static bfd_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 bfd_boolean bfd_mach_o_write_contents
-  PARAMS ((bfd *));
-static int bfd_mach_o_sizeof_headers
-  PARAMS ((bfd *, 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));
+#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_is_group_section               bfd_generic_is_group_section
+#define bfd_mach_o_bfd_discard_group                  bfd_generic_discard_group
+#define bfd_mach_o_section_already_linked             _bfd_generic_section_already_linked
+#define bfd_mach_o_bfd_copy_private_header_data       _bfd_generic_bfd_copy_private_header_data
+#define bfd_mach_o_core_file_matches_executable_p     generic_core_file_matches_executable_p
+
 
 /* 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
@@ -163,8 +103,7 @@ static const char * bfd_mach_o_ppc_flavour_string
 #define N_INDR 0xa
 
 bfd_boolean
-bfd_mach_o_valid (abfd)
-     bfd *abfd;
+bfd_mach_o_valid (bfd *abfd)
 {
   if (abfd == NULL || abfd->xvec == NULL)
     return 0;
@@ -183,11 +122,10 @@ bfd_mach_o_valid (abfd)
    to the output symbol.  */
 
 static bfd_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_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;
 }
@@ -196,11 +134,10 @@ bfd_mach_o_bfd_copy_private_symbol_data (ibfd, isymbol, obfd, osymbol)
    to the output section.  */
 
 static bfd_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_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
+                                         asection *isection ATTRIBUTE_UNUSED,
+                                         bfd *obfd ATTRIBUTE_UNUSED,
+                                         asection *osection ATTRIBUTE_UNUSED)
 {
   return TRUE;
 }
@@ -209,9 +146,7 @@ bfd_mach_o_bfd_copy_private_section_data (ibfd, isection, obfd, osection)
    to the output bfd.  */
 
 static bfd_boolean
-bfd_mach_o_bfd_copy_private_bfd_data (ibfd, obfd)
-     bfd *ibfd;
-     bfd *obfd;
+bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
 {
   BFD_ASSERT (bfd_mach_o_valid (ibfd));
   BFD_ASSERT (bfd_mach_o_valid (obfd));
@@ -222,8 +157,7 @@ bfd_mach_o_bfd_copy_private_bfd_data (ibfd, obfd)
 }
 
 static long
-bfd_mach_o_count_symbols (abfd)
-     bfd *abfd;
+bfd_mach_o_count_symbols (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = NULL;
   long nsyms = 0;
@@ -243,8 +177,7 @@ bfd_mach_o_count_symbols (abfd)
 }
 
 static long
-bfd_mach_o_get_symtab_upper_bound (abfd)
-     bfd *abfd;
+bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
 {
   long nsyms = bfd_mach_o_count_symbols (abfd);
 
@@ -255,9 +188,7 @@ bfd_mach_o_get_symtab_upper_bound (abfd)
 }
 
 static long
-bfd_mach_o_get_symtab (abfd, alocation)
-     bfd *abfd;
-     asymbol **alocation;
+bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
 {
   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
   long nsyms = bfd_mach_o_count_symbols (abfd);
@@ -275,7 +206,7 @@ bfd_mach_o_get_symtab (abfd, alocation)
 
          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);
+             fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
              return 0;
            }
 
@@ -295,20 +226,18 @@ bfd_mach_o_get_symtab (abfd, alocation)
 }
 
 static void
-bfd_mach_o_get_symbol_info (abfd, symbol, ret)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     asymbol *symbol;
-     symbol_info *ret;
+bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
+                           asymbol *symbol,
+                           symbol_info *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;
+bfd_mach_o_print_symbol (bfd *abfd,
+                        PTR afile,
+                        asymbol *symbol,
+                        bfd_print_symbol_type how)
 {
   FILE *file = (FILE *) afile;
 
@@ -324,11 +253,10 @@ bfd_mach_o_print_symbol (abfd, afile, symbol, how)
 }
 
 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;
 
@@ -358,55 +286,221 @@ bfd_mach_o_convert_architecture (mtype, msubtype, type, subtype)
     }
 }
 
-static bfd_boolean
-bfd_mach_o_write_contents (abfd)
-     bfd *abfd;
+static int
+bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
 {
-  unsigned int i;
-  asection *s;
+  unsigned char buf[28];
 
-  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+  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);
 
-  /* Write data sections first in case they overlap header data to be
-     written later.  */
+  bfd_seek (abfd, 0, SEEK_SET);
+  if (bfd_bwrite ((PTR) buf, 28, abfd) != 28)
+    return -1;
 
-  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
-    ;
+  return 0;
+}
 
-#if 0
-  for (i = 0; i < mdata->header.ncmds; i++)
+static int
+bfd_mach_o_scan_write_thread (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_mach_o_load_command *cur = &mdata->commands[i];
-      if (cur->type != BFD_MACH_O_LC_SEGMENT)
-       break;
+      BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
+      BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
 
-      {
-       bfd_mach_o_segment_command *seg = &cur->command.segment;
-       char buf[1024];
-       bfd_vma nbytes = seg->filesize;
-       bfd_vma curoff = seg->fileoff;
+      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_write_section (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;
+
+  return 0;
+}
+
+static int
+bfd_mach_o_scan_write_segment (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  unsigned char buf[48];
+  bfd_mach_o_segment_command *seg = &command->command.segment;
+  unsigned long i;
+
+  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
+
+  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;
+
+  {
+    char buf[1024];
+    bfd_vma nbytes = seg->filesize;
+    bfd_vma curoff = seg->fileoff;
 
-       while (nbytes > 0)
-         {
-           bfd_vma thisread = nbytes;
+    while (nbytes > 0)
+      {
+       bfd_vma thisread = nbytes;
 
-           if (thisread > 1024)
-             thisread = 1024;
+       if (thisread > 1024)
+         thisread = 1024;
 
-           bfd_seek (abfd, curoff, SEEK_SET);
-           if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
-             return FALSE;
+       bfd_seek (abfd, curoff, SEEK_SET);
+       if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
+         return -1;
 
-           bfd_seek (abfd, curoff, SEEK_SET);
-           if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
-             return FALSE;
+       bfd_seek (abfd, curoff, SEEK_SET);
+       if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
+         return -1;
 
-           nbytes -= thisread;
-           curoff += thisread;
-         }
+       nbytes -= thisread;
+       curoff += thisread;
       }
   }
-#endif
+
+  for (i = 0; i < seg->nsects; i++)
+    {
+      bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
+
+      if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
+       return -1;
+    }
+
+  return 0;
+}
+
+static int
+bfd_mach_o_scan_write_symtab_symbols (bfd *abfd,
+                                     bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_symtab_command *sym = &command->command.symtab;
+  asymbol *s = NULL;
+  unsigned long i;
+
+  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];
+
+      /* 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;
+       }
+    }
+
+  return 0;
+}
+
+static int
+bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_symtab_command *seg = &command->command.symtab;
+  unsigned char buf[16];
+
+  BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
+
+  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);
+
+  bfd_seek (abfd, command->offset + 8, SEEK_SET);
+  if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
+    return -1;
+
+  if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
+    return -1;
+
+  return 0;
+}
+
+static bfd_boolean
+bfd_mach_o_write_contents (bfd *abfd)
+{
+  unsigned int i;
+  asection *s;
+
+  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+
+  /* Write data sections first in case they overlap header data to be
+     written later.  */
+
+  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
+    ;
 
   /* Now write header information.  */
   if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
@@ -471,9 +565,8 @@ bfd_mach_o_write_contents (abfd)
 }
 
 static int
-bfd_mach_o_sizeof_headers (a, b)
-     bfd *a ATTRIBUTE_UNUSED;
-     bfd_boolean b ATTRIBUTE_UNUSED;
+bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
+                          struct bfd_link_info *info ATTRIBUTE_UNUSED)
 {
   return 0;
 }
@@ -482,12 +575,11 @@ bfd_mach_o_sizeof_headers (a, b)
    bfd_make_section_anyway wants to create a symbol for the section.  */
 
 static asymbol *
-bfd_mach_o_make_empty_symbol (abfd)
-     bfd *abfd;
+bfd_mach_o_make_empty_symbol (bfd *abfd)
 {
   asymbol *new;
 
-  new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
+  new = bfd_zalloc (abfd, sizeof (* new));
   if (new == NULL)
     return new;
   new->the_bfd = abfd;
@@ -495,34 +587,10 @@ bfd_mach_o_make_empty_symbol (abfd)
 }
 
 static int
-bfd_mach_o_write_header (abfd, header)
-     bfd *abfd;
-     bfd_mach_o_header *header;
+bfd_mach_o_read_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;
-
-  return 0;
-}
-
-static int
-bfd_mach_o_read_header (abfd, header)
-     bfd *abfd;
-     bfd_mach_o_header *header;
-{
-  unsigned char buf[28];
-  bfd_vma (*get32) PARAMS ((const bfd_byte *)) = NULL;
+  bfd_vma (*get32) (const void *) = NULL;
 
   bfd_seek (abfd, 0, SEEK_SET);
 
@@ -558,47 +626,43 @@ bfd_mach_o_read_header (abfd, header)
 }
 
 static asection *
-bfd_mach_o_make_bfd_section (abfd, section)
-     bfd *abfd;
-     bfd_mach_o_section *section;
+bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section)
 {
   asection *bfdsec;
   char *sname;
   const char *prefix = "LC_SEGMENT";
   unsigned int snamelen;
+  flagword flags;
 
   snamelen = strlen (prefix) + 1
     + strlen (section->segname) + 1
     + strlen (section->sectname) + 1;
 
-  sname = (char *) bfd_alloc (abfd, snamelen);
+  sname = bfd_alloc (abfd, snamelen);
   if (sname == NULL)
     return NULL;
   sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
+  flags = SEC_ALLOC;
+  if ((section->flags & SECTION_TYPE) != BFD_MACH_O_S_ZEROFILL)
+    flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
+  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
   if (bfdsec == NULL)
     return NULL;
 
   bfdsec->vma = section->addr;
   bfdsec->lma = section->addr;
-  bfdsec->_raw_size = section->size;
+  bfdsec->size = section->size;
   bfdsec->filepos = section->offset;
   bfdsec->alignment_power = section->align;
 
-  if (section->flags & BFD_MACH_O_S_ZEROFILL)
-    bfdsec->flags = SEC_ALLOC;
-  else
-    bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
-
   return bfdsec;
 }
 
 static int
-bfd_mach_o_scan_read_section (abfd, section, offset)
-     bfd *abfd;
-     bfd_mach_o_section *section;
-     bfd_vma offset;
+bfd_mach_o_scan_read_section (bfd *abfd,
+                             bfd_mach_o_section *section,
+                             bfd_vma offset)
 {
   unsigned char buf[68];
 
@@ -627,96 +691,11 @@ bfd_mach_o_scan_read_section (abfd, section, offset)
   return 0;
 }
 
-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;
-
-  return 0;
-}
-
-static int
-bfd_mach_o_scan_write_symtab_symbols (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
-{
-  bfd_mach_o_symtab_command *sym = &command->command.symtab;
-  asymbol *s = NULL;
-  unsigned long i;
-
-  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;
-       }
-    }
-
-  return 0;
-}
-
 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_scan_read_symtab_symbol (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);
@@ -825,9 +804,8 @@ bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
 }
 
 int
-bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
-     bfd *abfd;
-     bfd_mach_o_symtab_command *sym;
+bfd_mach_o_scan_read_symtab_strtab (bfd *abfd,
+                                   bfd_mach_o_symtab_command *sym)
 {
   BFD_ASSERT (sym->strtab == NULL);
 
@@ -842,7 +820,7 @@ bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
          bfd_set_error (bfd_error_file_truncated);
          return -1;
        }
-      sym->strtab = b->buffer + sym->stroff;
+      sym->strtab = (char *) b->buffer + sym->stroff;
       return 0;
     }
 
@@ -862,9 +840,8 @@ bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
 }
 
 int
-bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
-     bfd *abfd;
-     bfd_mach_o_symtab_command *sym;
+bfd_mach_o_scan_read_symtab_symbols (bfd *abfd,
+                                    bfd_mach_o_symtab_command *sym)
 {
   unsigned long i;
   int ret;
@@ -893,12 +870,11 @@ bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
 }
 
 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;
+bfd_mach_o_scan_read_dysymtab_symbol (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;
@@ -919,8 +895,7 @@ bfd_mach_o_scan_read_dysymtab_symbol (abfd, dysym, sym, s, i)
 }
 
 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)
     {
@@ -941,8 +916,7 @@ bfd_mach_o_i386_flavour_string (flavour)
 }
 
 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)
     {
@@ -955,43 +929,8 @@ bfd_mach_o_ppc_flavour_string (flavour)
 }
 
 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_scan_read_dylinker (bfd *abfd,
+                              bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
   unsigned char buf[4];
@@ -1019,21 +958,20 @@ bfd_mach_o_scan_read_dylinker (abfd, command)
   else
     abort ();
 
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
+  sname = bfd_alloc (abfd, strlen (prefix) + 1);
   if (sname == NULL)
     return -1;
   strcpy (sname, prefix);
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
+  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
   if (bfdsec == NULL)
     return -1;
 
   bfdsec->vma = 0;
   bfdsec->lma = 0;
-  bfdsec->_raw_size = command->len - 8;
+  bfdsec->size = command->len - 8;
   bfdsec->filepos = command->offset + 8;
   bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
 
   cmd->section = bfdsec;
 
@@ -1041,9 +979,7 @@ bfd_mach_o_scan_read_dylinker (abfd, command)
 }
 
 static int
-bfd_mach_o_scan_read_dylib (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_scan_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dylib_command *cmd = &command->command.dylib;
   unsigned char buf[16];
@@ -1077,21 +1013,20 @@ bfd_mach_o_scan_read_dylib (abfd, command)
   else
     abort ();
 
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
+  sname = bfd_alloc (abfd, strlen (prefix) + 1);
   if (sname == NULL)
     return -1;
   strcpy (sname, prefix);
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
+  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
   if (bfdsec == NULL)
     return -1;
 
   bfdsec->vma = 0;
   bfdsec->lma = 0;
-  bfdsec->_raw_size = command->len - 8;
+  bfdsec->size = command->len - 8;
   bfdsec->filepos = command->offset + 8;
   bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
 
   cmd->section = bfdsec;
 
@@ -1099,9 +1034,8 @@ bfd_mach_o_scan_read_dylib (abfd, command)
 }
 
 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_scan_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; */
 
@@ -1110,9 +1044,7 @@ bfd_mach_o_scan_read_prebound_dylib (abfd, command)
 }
 
 static int
-bfd_mach_o_scan_read_thread (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_data_struct *mdata = NULL;
   bfd_mach_o_thread_command *cmd = &command->command.thread;
@@ -1143,9 +1075,7 @@ bfd_mach_o_scan_read_thread (abfd, command)
       nflavours++;
     }
 
-  cmd->flavours =
-    ((bfd_mach_o_thread_flavour *)
-     bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour)));
+  cmd->flavours = bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
   if (cmd->flavours == NULL)
     return -1;
   cmd->nflavours = nflavours;
@@ -1195,7 +1125,7 @@ bfd_mach_o_scan_read_thread (abfd, command)
        }
 
       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;
 
@@ -1207,14 +1137,13 @@ bfd_mach_o_scan_read_thread (abfd, command)
          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->_raw_size = cmd->flavours[i].size;
+      bfdsec->size = cmd->flavours[i].size;
       bfdsec->filepos = cmd->flavours[i].offset;
       bfdsec->alignment_power = 0x0;
-      bfdsec->flags = SEC_HAS_CONTENTS;
 
       cmd->section = bfdsec;
     }
@@ -1223,34 +1152,7 @@ bfd_mach_o_scan_read_thread (abfd, command)
 }
 
 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];
-
-  BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
-
-  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);
-
-  bfd_seek (abfd, command->offset + 8, SEEK_SET);
-  if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
-    return -1;
-
-  if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
-    return -1;
-
-  return 0;
-}
-
-static int
-bfd_mach_o_scan_read_dysymtab (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_scan_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
   unsigned char buf[72];
@@ -1284,9 +1186,7 @@ bfd_mach_o_scan_read_dysymtab (abfd, command)
 }
 
 static int
-bfd_mach_o_scan_read_symtab (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_scan_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
   bfd_mach_o_symtab_command *seg = &command->command.symtab;
   unsigned char buf[16];
@@ -1307,40 +1207,38 @@ bfd_mach_o_scan_read_symtab (abfd, command)
   seg->symbols = NULL;
   seg->strtab = NULL;
 
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
+  sname = bfd_alloc (abfd, strlen (prefix) + 1);
   if (sname == NULL)
     return -1;
   strcpy (sname, prefix);
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
+  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
   if (bfdsec == NULL)
     return -1;
 
   bfdsec->vma = 0;
   bfdsec->lma = 0;
-  bfdsec->_raw_size = seg->nsyms * 12;
+  bfdsec->size = seg->nsyms * 12;
   bfdsec->filepos = seg->symoff;
   bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
 
   seg->stabs_segment = bfdsec;
 
   prefix = "LC_SYMTAB.stabstr";
-  sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
+  sname = bfd_alloc (abfd, strlen (prefix) + 1);
   if (sname == NULL)
     return -1;
   strcpy (sname, prefix);
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
+  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
   if (bfdsec == NULL)
     return -1;
 
   bfdsec->vma = 0;
   bfdsec->lma = 0;
-  bfdsec->_raw_size = seg->strsize;
+  bfdsec->size = seg->strsize;
   bfdsec->filepos = seg->stroff;
   bfdsec->alignment_power = 0;
-  bfdsec->flags = SEC_HAS_CONTENTS;
 
   seg->stabstr_segment = bfdsec;
 
@@ -1348,9 +1246,7 @@ bfd_mach_o_scan_read_symtab (abfd, command)
 }
 
 static int
-bfd_mach_o_scan_read_segment (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_scan_read_segment (bfd *abfd, bfd_mach_o_load_command *command)
 {
   unsigned char buf[48];
   bfd_mach_o_segment_command *seg = &command->command.segment;
@@ -1359,6 +1255,7 @@ bfd_mach_o_scan_read_segment (abfd, command)
   char *sname;
   const char *prefix = "LC_SEGMENT";
   unsigned int snamelen;
+  flagword flags;
 
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
 
@@ -1377,29 +1274,27 @@ bfd_mach_o_scan_read_segment (abfd, command)
   seg->flags = bfd_h_get_32 (abfd, buf + 44);
 
   snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
-  sname = (char *) bfd_alloc (abfd, snamelen);
+  sname = bfd_alloc (abfd, snamelen);
   if (sname == NULL)
     return -1;
   sprintf (sname, "%s.%s", prefix, seg->segname);
 
-  bfdsec = bfd_make_section_anyway (abfd, sname);
+  flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
+  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
   if (bfdsec == NULL)
     return -1;
 
   bfdsec->vma = seg->vmaddr;
   bfdsec->lma = seg->vmaddr;
-  bfdsec->_raw_size = seg->filesize;
+  bfdsec->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)));
+      seg->sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
       if (seg->sections == NULL)
        return -1;
 
@@ -1417,70 +1312,7 @@ bfd_mach_o_scan_read_segment (abfd, command)
 }
 
 static int
-bfd_mach_o_scan_write_segment (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
-{
-  unsigned char buf[48];
-  bfd_mach_o_segment_command *seg = &command->command.segment;
-  unsigned long i;
-
-  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
-
-  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;
-
-  {
-    char buf[1024];
-    bfd_vma nbytes = seg->filesize;
-    bfd_vma curoff = seg->fileoff;
-
-    while (nbytes > 0)
-      {
-       bfd_vma thisread = nbytes;
-
-       if (thisread > 1024)
-         thisread = 1024;
-
-       bfd_seek (abfd, curoff, SEEK_SET);
-       if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
-         return -1;
-
-       bfd_seek (abfd, curoff, SEEK_SET);
-       if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
-         return -1;
-
-       nbytes -= thisread;
-       curoff += thisread;
-      }
-  }
-
-  for (i = 0; i < seg->nsects; i++)
-    {
-      bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
-
-      if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
-       return -1;
-    }
-
-  return 0;
-}
-
-static int
-bfd_mach_o_scan_read_command (abfd, command)
-     bfd *abfd;
-     bfd_mach_o_load_command *command;
+bfd_mach_o_scan_read_command (bfd *abfd, bfd_mach_o_load_command *command)
 {
   unsigned char buf[8];
 
@@ -1553,8 +1385,7 @@ bfd_mach_o_scan_read_command (abfd, command)
 }
 
 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;
   long csect = 0;
@@ -1593,8 +1424,7 @@ bfd_mach_o_flatten_sections (abfd)
 }
 
 int
-bfd_mach_o_scan_start_address (abfd)
-     bfd *abfd;
+bfd_mach_o_scan_start_address (bfd *abfd)
 {
   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
   bfd_mach_o_thread_command *cmd = NULL;
@@ -1648,10 +1478,9 @@ bfd_mach_o_scan_start_address (abfd)
 }
 
 int
-bfd_mach_o_scan (abfd, header, mdata)
-     bfd *abfd;
-     bfd_mach_o_header *header;
-     bfd_mach_o_data_struct *mdata;
+bfd_mach_o_scan (bfd *abfd,
+                bfd_mach_o_header *header,
+                bfd_mach_o_data_struct *mdata)
 {
   unsigned int i;
   enum bfd_architecture cputype;
@@ -1677,9 +1506,7 @@ bfd_mach_o_scan (abfd, header, mdata)
 
   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)
        return -1;
 
@@ -1701,28 +1528,18 @@ bfd_mach_o_scan (abfd, header, mdata)
     }
 
   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 -1;
 
   bfd_mach_o_flatten_sections (abfd);
-
   return 0;
 }
 
 bfd_boolean
-bfd_mach_o_mkobject (abfd)
-     bfd *abfd;
+bfd_mach_o_mkobject (bfd *abfd)
 {
   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)
     return FALSE;
   abfd->tdata.mach_o_data = mdata;
@@ -1746,8 +1563,7 @@ bfd_mach_o_mkobject (abfd)
 }
 
 const bfd_target *
-bfd_mach_o_object_p (abfd)
-     bfd *abfd;
+bfd_mach_o_object_p (bfd *abfd)
 {
   struct bfd_preserve preserve;
   bfd_mach_o_header header;
@@ -1794,8 +1610,7 @@ bfd_mach_o_object_p (abfd)
 }
 
 const bfd_target *
-bfd_mach_o_core_p (abfd)
-     bfd *abfd;
+bfd_mach_o_core_p (bfd *abfd)
 {
   struct bfd_preserve preserve;
   bfd_mach_o_header header;
@@ -1862,8 +1677,7 @@ typedef struct mach_o_fat_data_struct
 } 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;
   unsigned char buf[20];
@@ -1873,8 +1687,7 @@ bfd_mach_o_archive_p (abfd)
   if (bfd_bread ((PTR) buf, 8, abfd) != 8)
     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;
 
@@ -1883,7 +1696,7 @@ bfd_mach_o_archive_p (abfd)
   if (adata->magic != 0xcafebabe)
     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;
@@ -1913,9 +1726,7 @@ bfd_mach_o_archive_p (abfd)
 }
 
 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;
@@ -1973,11 +1784,10 @@ bfd_mach_o_openr_next_archived_file (archive, prev)
 }
 
 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;
+bfd_mach_o_lookup_section (bfd *abfd,
+                          asection *section,
+                          bfd_mach_o_load_command **mcommand,
+                          bfd_mach_o_section **msection)
 {
   struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
   unsigned int i, j, num;
@@ -2024,10 +1834,9 @@ bfd_mach_o_lookup_section (abfd, section, mcommand, msection)
 }
 
 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;
   bfd_mach_o_load_command *ncmd = NULL;
@@ -2056,8 +1865,7 @@ bfd_mach_o_lookup_command (abfd, type, mcommand)
 }
 
 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)
     {
@@ -2081,10 +1889,9 @@ bfd_mach_o_stack_addr (type)
 }
 
 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;
   unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
@@ -2116,13 +1923,18 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
              if (size > (end - start))
                size = (end - start);
 
-             buf = bfd_realloc (buf, size);
-
+             buf = bfd_realloc_or_free (buf, size);
+             if (buf == NULL)
+               return -1;
+             
              bfd_seek (abfd, end - size, SEEK_SET);
              nread = bfd_bread (buf, size, abfd);
 
              if (nread != size)
-               return -1;
+               {
+                 free (buf);
+                 return -1;
+               }
 
              for (offset = 4; offset <= size; offset += 4)
                {
@@ -2145,6 +1957,7 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
                      *rlen = top - bottom;
 
                      memcpy (*rbuf, buf + size - *rlen, *rlen);
+                     free (buf);
                      return 0;
                    }
                }
@@ -2154,6 +1967,8 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
 
              size *= 2;
            }
+
+         free (buf);
        }
     }
 
@@ -2161,8 +1976,7 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
 }
 
 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;
@@ -2172,28 +1986,19 @@ bfd_mach_o_core_file_failing_command (abfd)
   if (ret < 0)
     return NULL;
 
-  return buf;
+  return (char *) buf;
 }
 
 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;
 }
 
-bfd_boolean
-bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
-     bfd *core_bfd ATTRIBUTE_UNUSED;
-     bfd *exec_bfd ATTRIBUTE_UNUSED;
-{
-  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 TARGET_NAME            mach_o_be_vec
+#define TARGET_STRING          "mach-o-be"
+#define TARGET_BIG_ENDIAN      1
+#define TARGET_ARCHIVE                 0
 
 #include "mach-o-target.c"
 
@@ -2202,10 +2007,10 @@ bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
 
-#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_BIG_ENDIAN      0
+#define TARGET_ARCHIVE                 0
 
 #include "mach-o-target.c"
 
@@ -2214,10 +2019,10 @@ bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
 #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
+#define TARGET_NAME            mach_o_fat_vec
+#define TARGET_STRING          "mach-o-fat"
+#define TARGET_BIG_ENDIAN      1
+#define TARGET_ARCHIVE                 1
 
 #include "mach-o-target.c"
 
This page took 0.0443 seconds and 4 git commands to generate.