* reloc.c: Add PPC and PPC64 TLS relocs.
[deliverable/binutils-gdb.git] / bfd / mach-o.c
index 62432203d032a596cd18efb06e5f12240b49cd6d..4c4736ada0d14acf989e0d14c03697d66a972021 100644 (file)
@@ -15,7 +15,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software 
+   along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "mach-o.h"
 #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));
+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));
 
 /* 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
@@ -129,7 +162,7 @@ static const char * bfd_mach_o_ppc_flavour_string    PARAMS ((unsigned int));
 #define N_SECT 0xe
 #define N_INDR 0xa
 
-boolean
+bfd_boolean
 bfd_mach_o_valid (abfd)
      bfd *abfd;
 {
@@ -149,33 +182,33 @@ bfd_mach_o_valid (abfd)
 /* Copy any private info we understand from the input symbol
    to the output symbol.  */
 
-static boolean
+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;
 {
-  return true;
+  return TRUE;
 }
 
 /* Copy any private info we understand from the input section
    to the output section.  */
 
-static boolean
+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;
 {
-  return true;
+  return TRUE;
 }
 
 /* Copy any private info we understand from the input bfd
    to the output bfd.  */
 
-static boolean
+static bfd_boolean
 bfd_mach_o_bfd_copy_private_bfd_data (ibfd, obfd)
      bfd *ibfd;
      bfd *obfd;
@@ -185,7 +218,7 @@ bfd_mach_o_bfd_copy_private_bfd_data (ibfd, obfd)
 
   obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
   obfd->tdata.mach_o_data->ibfd = ibfd;
-  return true;
+  return TRUE;
 }
 
 static long
@@ -195,7 +228,7 @@ bfd_mach_o_count_symbols (abfd)
   bfd_mach_o_data_struct *mdata = NULL;
   long nsyms = 0;
   unsigned long i;
-  
+
   BFD_ASSERT (bfd_mach_o_valid (abfd));
   mdata = abfd->tdata.mach_o_data;
 
@@ -257,7 +290,7 @@ bfd_mach_o_get_symtab (abfd, alocation)
     }
 
   *csym++ = NULL;
-  
+
   return nsyms;
 }
 
@@ -324,8 +357,8 @@ bfd_mach_o_convert_architecture (mtype, msubtype, type, subtype)
       *subtype = bfd_arch_unknown;
     }
 }
-  
-static boolean
+
+static bfd_boolean
 bfd_mach_o_write_contents (abfd)
      bfd *abfd;
 {
@@ -362,11 +395,11 @@ bfd_mach_o_write_contents (abfd)
 
            bfd_seek (abfd, curoff, SEEK_SET);
            if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
-             return false;
+             return FALSE;
 
            bfd_seek (abfd, curoff, SEEK_SET);
            if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
-             return false;
+             return FALSE;
 
            nbytes -= thisread;
            curoff += thisread;
@@ -377,7 +410,7 @@ bfd_mach_o_write_contents (abfd)
 
   /* Now write header information.  */
   if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
-    return false;
+    return FALSE;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
@@ -392,24 +425,24 @@ bfd_mach_o_write_contents (abfd)
 
       bfd_seek (abfd, cur->offset, SEEK_SET);
       if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
-       return false;
+       return FALSE;
 
       switch (cur->type)
        {
        case BFD_MACH_O_LC_SEGMENT:
          if (bfd_mach_o_scan_write_segment (abfd, cur) != 0)
-           return false;
+           return FALSE;
          break;
        case BFD_MACH_O_LC_SYMTAB:
          if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
-           return false;
+           return FALSE;
          break;
        case BFD_MACH_O_LC_SYMSEG:
          break;
        case BFD_MACH_O_LC_THREAD:
        case BFD_MACH_O_LC_UNIXTHREAD:
          if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
-           return false;
+           return FALSE;
          break;
        case BFD_MACH_O_LC_LOADFVMLIB:
        case BFD_MACH_O_LC_IDFVMLIB:
@@ -430,17 +463,17 @@ bfd_mach_o_write_contents (abfd)
          fprintf (stderr,
                   "unable to write unknown load command 0x%lx\n",
                   (long) cur->type);
-         return false;
+         return FALSE;
        }
     }
 
-  return true;
+  return TRUE;
 }
 
 static int
 bfd_mach_o_sizeof_headers (a, b)
      bfd *a ATTRIBUTE_UNUSED;
-     boolean b ATTRIBUTE_UNUSED;
+     bfd_boolean b ATTRIBUTE_UNUSED;
 {
   return 0;
 }
@@ -475,7 +508,7 @@ bfd_mach_o_write_header (abfd, header)
   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;
@@ -504,7 +537,7 @@ bfd_mach_o_read_header (abfd, header)
     }
   else if (bfd_getl32 (buf) == 0xfeedface)
     {
-      header->byteorder = BFD_ENDIAN_LITTLE; 
+      header->byteorder = BFD_ENDIAN_LITTLE;
       header->magic = 0xfeedface;
       get32 = bfd_getl32;
     }
@@ -513,7 +546,7 @@ bfd_mach_o_read_header (abfd, header)
       header->byteorder = BFD_ENDIAN_UNKNOWN;
       return -1;
     }
-      
+
   header->cputype = (*get32) (buf + 4);
   header->cpusubtype = (*get32) (buf + 8);
   header->filetype = (*get32) (buf + 12);
@@ -546,7 +579,7 @@ bfd_mach_o_make_bfd_section (abfd, section)
   bfdsec = bfd_make_section_anyway (abfd, sname);
   if (bfdsec == NULL)
     return NULL;
-  
+
   bfdsec->vma = section->addr;
   bfdsec->lma = section->addr;
   bfdsec->_raw_size = section->size;
@@ -572,7 +605,7 @@ bfd_mach_o_scan_read_section (abfd, section, offset)
   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);
@@ -617,7 +650,7 @@ bfd_mach_o_scan_write_section (abfd, section, offset)
   bfd_seek (abfd, offset, SEEK_SET);
   if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
     return -1;
-  
+
   return 0;
 }
 
@@ -641,7 +674,7 @@ bfd_mach_o_scan_write_symtab_symbols (abfd, command)
       s = &sym->symbols[i];
 
       /* Don't set this from the symbol information; use stored values.  */
-#if 0 
+#if 0
       if (s->flags & BSF_GLOBAL)
        ntype |= N_EXT;
       if (s->flags & BSF_DEBUGGING)
@@ -654,7 +687,7 @@ bfd_mach_o_scan_write_symtab_symbols (abfd, command)
       else
        ntype |= N_SECT;
 #endif
-      
+
       /* Instead just set from the stored values.  */
       ntype = (s->udata.i >> 24) & 0xff;
       nsect = (s->udata.i >> 16) & 0xff;
@@ -669,9 +702,9 @@ bfd_mach_o_scan_write_symtab_symbols (abfd, command)
       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", 
+         fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
                   12, (unsigned long) symoff);
-         return -1; 
+         return -1;
        }
     }
 
@@ -691,7 +724,7 @@ bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
   unsigned char type = -1;
   unsigned char section = -1;
   short desc = -1;
-  long value = -1;
+  unsigned long value = -1;
   unsigned long stroff = -1;
   unsigned int symtype = -1;
 
@@ -716,7 +749,7 @@ bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
     {
       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; 
+      return -1;
     }
 
   s->the_bfd = abfd;
@@ -767,7 +800,7 @@ bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
              if (section != 0)
                {
                  fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
-                          "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n", 
+                          "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
                           s->name, section, mdata->nsects);
                }
              s->section = bfd_und_section_ptr;
@@ -775,13 +808,13 @@ bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
          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", 
+                  "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
                   s->name);
          s->section = bfd_und_section_ptr;
          break;
        default:
          fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
-                  "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n", 
+                  "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
                   s->name, symtype);
          s->section = bfd_und_section_ptr;
          break;
@@ -822,13 +855,13 @@ bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
     {
       fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
               sym->strsize, sym->stroff);
-      return -1; 
+      return -1;
     }
 
   return 0;
 }
 
-int 
+int
 bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
      bfd *abfd;
      bfd_mach_o_symtab_command *sym;
@@ -842,9 +875,9 @@ bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
   if (sym->symbols == NULL)
     {
       fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
-      return -1; 
+      return -1;
     }
-  
+
   ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
   if (ret != 0)
     return ret;
@@ -855,7 +888,7 @@ bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
       if (ret != 0)
        return ret;
     }
-  
+
   return 0;
 }
 
@@ -872,7 +905,7 @@ bfd_mach_o_scan_read_dysymtab_symbol (abfd, dysym, sym, s, i)
   unsigned char buf[4];
 
   BFD_ASSERT (i < dysym->nindirectsyms);
-  
+
   bfd_seek (abfd, isymoff, SEEK_SET);
   if (bfd_bread ((PTR) buf, 4, abfd) != 4)
     {
@@ -881,7 +914,7 @@ bfd_mach_o_scan_read_dysymtab_symbol (abfd, dysym, sym, s, i)
       return -1;
     }
   symindex = bfd_h_get_32 (abfd, buf);
-  
+
   return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
 }
 
@@ -994,7 +1027,7 @@ bfd_mach_o_scan_read_dylinker (abfd, command)
   bfdsec = bfd_make_section_anyway (abfd, sname);
   if (bfdsec == NULL)
     return -1;
-  
+
   bfdsec->vma = 0;
   bfdsec->lma = 0;
   bfdsec->_raw_size = command->len - 8;
@@ -1052,7 +1085,7 @@ bfd_mach_o_scan_read_dylib (abfd, command)
   bfdsec = bfd_make_section_anyway (abfd, sname);
   if (bfdsec == NULL)
     return -1;
-  
+
   bfdsec->vma = 0;
   bfdsec->lma = 0;
   bfdsec->_raw_size = command->len - 8;
@@ -1093,7 +1126,7 @@ bfd_mach_o_scan_read_thread (abfd, command)
 
   BFD_ASSERT (bfd_mach_o_valid (abfd));
   mdata = abfd->tdata.mach_o_data;
-  
+
   offset = 8;
   nflavours = 0;
   while (offset != command->len)
@@ -1146,8 +1179,8 @@ bfd_mach_o_scan_read_thread (abfd, command)
       char *sname;
       const char *flavourstr;
       const char *prefix = "LC_THREAD";
-      unsigned int i = 0;
-    
+      unsigned int j = 0;
+
       switch (mdata->header.cputype)
        {
        case BFD_MACH_O_CPU_TYPE_POWERPC:
@@ -1160,7 +1193,7 @@ bfd_mach_o_scan_read_thread (abfd, command)
          flavourstr = "UNKNOWN_ARCHITECTURE";
          break;
        }
-    
+
       snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
       sname = (char *) bfd_alloc (abfd, snamelen);
       if (sname == NULL)
@@ -1168,15 +1201,14 @@ bfd_mach_o_scan_read_thread (abfd, command)
 
       for (;;)
        {
-         sprintf (sname, "%s.%s.%u", prefix, flavourstr, i);
-         if (bfd_get_section_by_name (abfd, sname) == NULL) {
+         sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
+         if (bfd_get_section_by_name (abfd, sname) == NULL)
            break;
-         }
-         i++;
+         j++;
        }
 
       bfdsec = bfd_make_section (abfd, sname);
-  
+
       bfdsec->vma = 0;
       bfdsec->lma = 0;
       bfdsec->_raw_size = cmd->flavours[i].size;
@@ -1190,7 +1222,7 @@ bfd_mach_o_scan_read_thread (abfd, command)
   return 0;
 }
 
-static int 
+static int
 bfd_mach_o_scan_write_symtab (abfd, command)
      bfd *abfd;
      bfd_mach_o_load_command *command;
@@ -1208,14 +1240,14 @@ bfd_mach_o_scan_write_symtab (abfd, command)
   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 
+static int
 bfd_mach_o_scan_read_dysymtab (abfd, command)
      bfd *abfd;
      bfd_mach_o_load_command *command;
@@ -1251,7 +1283,7 @@ bfd_mach_o_scan_read_dysymtab (abfd, command)
   return 0;
 }
 
-static int 
+static int
 bfd_mach_o_scan_read_symtab (abfd, command)
      bfd *abfd;
      bfd_mach_o_load_command *command;
@@ -1267,7 +1299,7 @@ bfd_mach_o_scan_read_symtab (abfd, command)
   bfd_seek (abfd, command->offset + 8, SEEK_SET);
   if (bfd_bread ((PTR) buf, 16, abfd) != 16)
     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);
@@ -1283,7 +1315,7 @@ bfd_mach_o_scan_read_symtab (abfd, command)
   bfdsec = bfd_make_section_anyway (abfd, sname);
   if (bfdsec == NULL)
     return -1;
-  
+
   bfdsec->vma = 0;
   bfdsec->lma = 0;
   bfdsec->_raw_size = seg->nsyms * 12;
@@ -1302,7 +1334,7 @@ bfd_mach_o_scan_read_symtab (abfd, command)
   bfdsec = bfd_make_section_anyway (abfd, sname);
   if (bfdsec == NULL)
     return -1;
-  
+
   bfdsec->vma = 0;
   bfdsec->lma = 0;
   bfdsec->_raw_size = seg->strsize;
@@ -1327,7 +1359,7 @@ bfd_mach_o_scan_read_segment (abfd, command)
   char *sname;
   const char *prefix = "LC_SEGMENT";
   unsigned int snamelen;
+
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
 
   bfd_seek (abfd, command->offset + 8, SEEK_SET);
@@ -1365,19 +1397,20 @@ bfd_mach_o_scan_read_segment (abfd, command)
 
   if (seg->nsects != 0)
     {
-      seg->sections = 
+      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);
 
-         if (bfd_mach_o_scan_read_section (abfd, &seg->sections[i], segoff) != 0)
+         if (bfd_mach_o_scan_read_section (abfd, &seg->sections[i],
+                                           segoff) != 0)
            return -1;
-       }    
+       }
     }
 
   return 0;
@@ -1391,7 +1424,7 @@ bfd_mach_o_scan_write_segment (abfd, 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);
@@ -1412,7 +1445,7 @@ bfd_mach_o_scan_write_segment (abfd, command)
     char buf[1024];
     bfd_vma nbytes = seg->filesize;
     bfd_vma curoff = seg->fileoff;
-    
+
     while (nbytes > 0)
       {
        bfd_vma thisread = nbytes;
@@ -1456,7 +1489,8 @@ bfd_mach_o_scan_read_command (abfd, command)
     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->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);
 
   switch (command->type)
@@ -1525,27 +1559,31 @@ bfd_mach_o_flatten_sections (abfd)
   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
   long csect = 0;
   unsigned long i, j;
-  
+
   mdata->nsects = 0;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
       if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
        {
-         bfd_mach_o_segment_command *seg = &mdata->commands[i].command.segment;
+         bfd_mach_o_segment_command *seg;
+
+         seg = &mdata->commands[i].command.segment;
          mdata->nsects += seg->nsects;
        }
     }
 
-  mdata->sections = bfd_alloc (abfd, mdata->nsects * sizeof (bfd_mach_o_section *));
+  mdata->sections = bfd_alloc (abfd,
+                              mdata->nsects * sizeof (bfd_mach_o_section *));
   csect = 0;
 
   for (i = 0; i < mdata->header.ncmds; i++)
     {
       if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
        {
-         bfd_mach_o_segment_command *seg = &mdata->commands[i].command.segment;
+         bfd_mach_o_segment_command *seg;
 
+         seg = &mdata->commands[i].command.segment;
          BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
 
          for (j = 0; j < seg->nsects; j++)
@@ -1579,8 +1617,9 @@ bfd_mach_o_scan_start_address (abfd)
 
   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))
+      if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
+         && (cmd->flavours[i].flavour
+             == (unsigned long) BFD_MACH_O_i386_THREAD_STATE))
        {
          unsigned char buf[4];
 
@@ -1591,7 +1630,7 @@ bfd_mach_o_scan_start_address (abfd)
 
          abfd->start_address = bfd_h_get_32 (abfd, buf);
        }
-      else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC) 
+      else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
               && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
        {
          unsigned char buf[4];
@@ -1609,27 +1648,24 @@ bfd_mach_o_scan_start_address (abfd)
 }
 
 int
-bfd_mach_o_scan (abfd, header)
+bfd_mach_o_scan (abfd, header, mdata)
      bfd *abfd;
      bfd_mach_o_header *header;
+     bfd_mach_o_data_struct *mdata;
 {
   unsigned int i;
-  bfd_mach_o_data_struct *mdata = NULL;
   enum bfd_architecture cputype;
   unsigned long cpusubtype;
 
-  mdata = ((bfd_mach_o_data_struct *) 
-          bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct)));
-  if (mdata == NULL)
-    return -1;
-
   mdata->header = *header;
   mdata->symbols = NULL;
 
-  abfd->flags = abfd->xvec->object_flags | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS));  
+  abfd->flags = (abfd->xvec->object_flags
+                | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
   abfd->tdata.mach_o_data = mdata;
 
-  bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype, &cputype, &cpusubtype);
+  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",
@@ -1638,15 +1674,15 @@ bfd_mach_o_scan (abfd, header)
     }
 
   bfd_set_arch_mach (abfd, cputype, cpusubtype);
-  
+
   if (header->ncmds != 0)
     {
-      mdata->commands = 
+      mdata->commands =
        ((bfd_mach_o_load_command *)
         bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command)));
       if (mdata->commands == NULL)
        return -1;
-    
+
       for (i = 0; i < header->ncmds; i++)
        {
          bfd_mach_o_load_command *cur = &mdata->commands[i];
@@ -1661,7 +1697,7 @@ bfd_mach_o_scan (abfd, header)
 
          if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
            return -1;
-       } 
+       }
     }
 
   if (bfd_mach_o_scan_start_address (abfd) < 0)
@@ -1679,16 +1715,16 @@ bfd_mach_o_scan (abfd, header)
   return 0;
 }
 
-boolean
+bfd_boolean
 bfd_mach_o_mkobject (abfd)
      bfd *abfd;
 {
   bfd_mach_o_data_struct *mdata = NULL;
 
-  mdata = ((bfd_mach_o_data_struct *) 
+  mdata = ((bfd_mach_o_data_struct *)
           bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct)));
   if (mdata == NULL)
-    return false;
+    return FALSE;
   abfd->tdata.mach_o_data = mdata;
 
   mdata->header.magic = 0;
@@ -1706,91 +1742,106 @@ bfd_mach_o_mkobject (abfd)
   mdata->sections = NULL;
   mdata->ibfd = NULL;
 
-  return true;
+  return TRUE;
 }
 
 const bfd_target *
 bfd_mach_o_object_p (abfd)
      bfd *abfd;
 {
+  struct bfd_preserve preserve;
   bfd_mach_o_header header;
 
+  preserve.marker = NULL;
   if (bfd_mach_o_read_header (abfd, &header) != 0)
-    {
-      bfd_set_error (bfd_error_wrong_format);
-      return NULL;
-    }
+    goto wrong;
 
-  if (! ((header.byteorder == BFD_ENDIAN_BIG)
-        || (header.byteorder == BFD_ENDIAN_LITTLE)))
+  if (! (header.byteorder == BFD_ENDIAN_BIG
+        || header.byteorder == BFD_ENDIAN_LITTLE))
     {
-      fprintf (stderr, "unknown header byte-order value 0x%lx\n", (long) header.byteorder);
-      bfd_set_error (bfd_error_wrong_format);
-      return NULL;
+      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))))
-    {
-      bfd_set_error (bfd_error_wrong_format);
-      return NULL;
-    }
+  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;
 
-  abfd->tdata.mach_o_data = NULL;
+  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) != 0)
-    {
-      abfd->tdata.mach_o_data = NULL;
-      bfd_set_error (bfd_error_wrong_format);
-      return NULL;
-    }
-  
+  if (bfd_mach_o_scan (abfd, &header,
+                      (bfd_mach_o_data_struct *) preserve.marker) != 0)
+    goto wrong;
+
+  bfd_preserve_finish (abfd, &preserve);
   return abfd->xvec;
+
+ wrong:
+  bfd_set_error (bfd_error_wrong_format);
+
+ fail:
+  if (preserve.marker != NULL)
+    bfd_preserve_restore (abfd, &preserve);
+  return NULL;
 }
 
 const bfd_target *
 bfd_mach_o_core_p (abfd)
      bfd *abfd;
 {
+  struct bfd_preserve preserve;
   bfd_mach_o_header header;
 
-  bfd_set_error (bfd_error_wrong_format);
-
+  preserve.marker = NULL;
   if (bfd_mach_o_read_header (abfd, &header) != 0)
-    return NULL;
+    goto wrong;
 
-  if (! ((header.byteorder == BFD_ENDIAN_BIG)
-        || (header.byteorder == BFD_ENDIAN_LITTLE)))
+  if (! (header.byteorder == BFD_ENDIAN_BIG
+        || header.byteorder == BFD_ENDIAN_LITTLE))
     {
-      fprintf (stderr, "unknown header byte-order value 0x%lx\n", (long) header.byteorder);
+      fprintf (stderr, "unknown header byte-order value 0x%lx\n",
+              (long) header.byteorder);
       abort ();
     }
 
-  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))))
-    return NULL;
+  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;
 
   if (header.filetype != BFD_MACH_O_MH_CORE)
-    return NULL;
+    goto wrong;
 
-  abfd->tdata.mach_o_data = NULL;
-  if (bfd_mach_o_scan (abfd, &header) != 0)
-    {
-      abfd->tdata.mach_o_data = NULL;
-      return NULL;
-    }
-  
+  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_preserve_finish (abfd, &preserve);
   return abfd->xvec;
+
+ wrong:
+  bfd_set_error (bfd_error_wrong_format);
+
+ fail:
+  if (preserve.marker != NULL)
+    bfd_preserve_restore (abfd, &preserve);
+  return NULL;
 }
 
 typedef struct mach_o_fat_archentry
@@ -1814,35 +1865,35 @@ const bfd_target *
 bfd_mach_o_archive_p (abfd)
      bfd *abfd;
 {
-  mach_o_fat_data_struct *adata;
+  mach_o_fat_data_struct *adata = NULL;
   unsigned char buf[20];
   unsigned long i;
 
   bfd_seek (abfd, 0, SEEK_SET);
   if (bfd_bread ((PTR) buf, 8, abfd) != 8)
-    return NULL;
+    goto error;
 
-  adata = (mach_o_fat_data_struct *) 
+  adata = (mach_o_fat_data_struct *)
     bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
   if (adata == NULL)
-    return NULL;
-  
+    goto error;
+
   adata->magic = bfd_getb32 (buf);
   adata->nfat_arch = bfd_getb32 (buf + 4);
   if (adata->magic != 0xcafebabe)
-    return NULL;
+    goto error;
 
-  adata->archentries = (mach_o_fat_archentry *) 
+  adata->archentries = (mach_o_fat_archentry *)
     bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
   if (adata->archentries == NULL)
-    return 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)
-       return NULL;
+       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);
@@ -1853,6 +1904,12 @@ bfd_mach_o_archive_p (abfd)
 
   abfd->tdata.mach_o_fat_data = adata;
   return abfd->xvec;
+
+ error:
+  if (adata != NULL)
+    bfd_release (abfd, adata);
+  bfd_set_error (bfd_error_wrong_format);
+  return NULL;
 }
 
 bfd *
@@ -1860,10 +1917,11 @@ bfd_mach_o_openr_next_archived_file (archive, prev)
   bfd *archive;
   bfd *prev;
 {
-  mach_o_fat_data_struct *adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
+  mach_o_fat_data_struct *adata;
   mach_o_fat_archentry *entry = NULL;
   unsigned long i;
 
+  adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
   BFD_ASSERT (adata != NULL);
 
   /* Find index of previous entry.  */
@@ -1881,11 +1939,11 @@ bfd_mach_o_openr_next_archived_file (archive, prev)
        {
          /* Not found.  */
          bfd_set_error (bfd_error_bad_value);
-         return NULL; 
+         return NULL;
        }
     i++;       /* Get next entry.  */
   }
-  
+
   if (i >= adata->nfat_arch)
     {
       bfd_set_error (bfd_error_no_more_archived_files);
@@ -1914,9 +1972,12 @@ bfd_mach_o_openr_next_archived_file (archive, prev)
   return entry->abfd;
 }
 
-int bfd_mach_o_lookup_section 
-  (bfd *abfd, asection *section, 
-   bfd_mach_o_load_command **mcommand, bfd_mach_o_section **msection)
+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;
 {
   struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
   unsigned int i, j, num;
@@ -1936,7 +1997,7 @@ int bfd_mach_o_lookup_section
       if (cmd->type != BFD_MACH_O_LC_SEGMENT)
        continue;
       seg = &cmd->command.segment;
-    
+
       if (seg->segment == section)
        {
          if (num == 0)
@@ -1956,16 +2017,17 @@ int bfd_mach_o_lookup_section
            }
        }
     }
-  
+
   *mcommand = ncmd;
   *msection = nsect;
   return num;
 }
 
 int
-bfd_mach_o_lookup_command 
-  (bfd *abfd, bfd_mach_o_load_command_type type,
-   bfd_mach_o_load_command **mcommand)
+bfd_mach_o_lookup_command (abfd, type, mcommand)
+     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;
@@ -2012,7 +2074,7 @@ bfd_mach_o_stack_addr (type)
     case BFD_MACH_O_CPU_TYPE_I860:
       return 0;
     case BFD_MACH_O_CPU_TYPE_HPPA:
-      return (0xc0000000-0x04000000);
+      return 0xc0000000 - 0x04000000;
     default:
       return 0;
     }
@@ -2058,14 +2120,15 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
 
              bfd_seek (abfd, end - size, SEEK_SET);
              nread = bfd_bread (buf, size, abfd);
-       
+
              if (nread != size)
                return -1;
-      
+
              for (offset = 4; offset <= size; offset += 4)
                {
-                 unsigned long val =  *((unsigned long *) (buf + size - offset));
+                 unsigned long val;
 
+                 val = *((unsigned long *) (buf + size - offset));
                  if (! found_nonnull)
                    {
                      if (val != 0)
@@ -2073,9 +2136,11 @@ bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
                    }
                  else if (val == 0x0)
                    {
-                     unsigned long bottom = seg->fileoff + seg->filesize - offset;
-                     unsigned long top = seg->fileoff + seg->filesize - 4;
+                     unsigned long bottom;
+                     unsigned long top;
 
+                     bottom = seg->fileoff + seg->filesize - offset;
+                     top = seg->fileoff + seg->filesize - 4;
                      *rbuf = bfd_malloc (top - bottom);
                      *rlen = top - bottom;
 
@@ -2117,12 +2182,12 @@ bfd_mach_o_core_file_failing_signal (abfd)
   return 0;
 }
 
-boolean
+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;
+  return TRUE;
 }
 
 #define TARGET_NAME mach_o_be_vec
@@ -2160,4 +2225,3 @@ bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
 #undef TARGET_STRING
 #undef TARGET_BIG_ENDIAN
 #undef TARGET_ARCHIVE
-
This page took 0.038067 seconds and 4 git commands to generate.