add indirect_symbol to mach-o port.
[deliverable/binutils-gdb.git] / bfd / mach-o.c
index 30b77d8057a9fbb299f7d75724249b4851b4f4ee..c5196631c4a90ea7f0637dd8c7192662c41ada3c 100644 (file)
@@ -533,10 +533,17 @@ bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
 
 bfd_boolean
 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
-                                        asymbol *isymbol ATTRIBUTE_UNUSED,
+                                        asymbol *isymbol,
                                         bfd *obfd ATTRIBUTE_UNUSED,
-                                        asymbol *osymbol ATTRIBUTE_UNUSED)
+                                        asymbol *osymbol)
 {
+  bfd_mach_o_asymbol *os, *is;
+  os = (bfd_mach_o_asymbol *)osymbol;
+  is = (bfd_mach_o_asymbol *)isymbol;
+  os->n_type = is->n_type;
+  os->n_sect = is->n_sect;
+  os->n_desc = is->n_desc;
+  os->symbol.udata.i = is->symbol.udata.i;
   return TRUE;
 }
 
@@ -651,6 +658,8 @@ bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
   return nsyms;
 }
 
+/* Create synthetic symbols for indirect symbols.  */
+
 long
 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
                                  long symcount ATTRIBUTE_UNUSED,
@@ -670,19 +679,23 @@ bfd_mach_o_get_synthetic_symtab (bfd *abfd,
 
   *ret = NULL;
 
+  /* Stop now if no symbols or no indirect symbols.  */
   if (dysymtab == NULL || symtab == NULL || symtab->symbols == NULL)
     return 0;
 
   if (dysymtab->nindirectsyms == 0)
     return 0;
 
+  /* We need to allocate a bfd symbol for every indirect symbol and to
+     allocate the memory for its name.  */
   count = dysymtab->nindirectsyms;
   size = count * sizeof (asymbol) + 1;
 
   for (j = 0; j < count; j++)
     {
       unsigned int isym = dysymtab->indirect_syms[j];
-              
+
+      /* Some indirect symbols are anonymous.  */
       if (isym < symtab->nsyms && symtab->symbols[isym].symbol.name)
         size += strlen (symtab->symbols[isym].symbol.name) + sizeof ("$stub");
     }
@@ -707,6 +720,7 @@ bfd_mach_o_get_synthetic_symtab (bfd *abfd,
         case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
         case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
         case BFD_MACH_O_S_SYMBOL_STUBS:
+          /* Only these sections have indirect symbols.  */
           first = sec->reserved1;
           last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
           addr = sec->addr;
@@ -814,7 +828,7 @@ bfd_mach_o_print_symbol (bfd *abfd,
 
 static void
 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
-                                bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
+                                bfd_mach_o_cpu_subtype msubtype,
                                 enum bfd_architecture *type,
                                 unsigned long *subtype)
 {
@@ -822,8 +836,12 @@ bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
 
   switch (mtype)
     {
-    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;
       *subtype = bfd_mach_i386_i386;
@@ -832,17 +850,52 @@ bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
       *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;
+      switch (msubtype)
+        {
+        case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
+          *subtype = bfd_mach_arm_4T;
+          break;
+        case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
+          *subtype = bfd_mach_arm_4T;  /* Best fit ?  */
+          break;
+        case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
+          *subtype = bfd_mach_arm_5TE;
+          break;
+        case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
+          *subtype = bfd_mach_arm_XScale;
+          break;
+        case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
+          *subtype = bfd_mach_arm_5TE; /* Best fit ?  */
+          break;
+        case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
+        default:
+          break;
+        }
+      break;
+    case BFD_MACH_O_CPU_TYPE_MC88000:
+      *type = bfd_arch_m88k;
+      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;
       *subtype = bfd_mach_ppc;
@@ -1354,22 +1407,6 @@ bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
     {
       bfd_size_type str_index;
       bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
-
-      /* For a bare indirect symbol, the system tools expect that the symbol
-        value will be the string table offset for its referenced counterpart.
-        
-        Normally, indirect syms will not be written this way, but rather as
-        part of the dysymtab command.
-        
-        In either case, correct operation depends on the symbol table being
-        sorted such that the indirect symbols are at the end (since the 
-        string table index is filled in below).  */
-
-      if (IS_MACHO_INDIRECT (s->n_type))
-       /* A pointer to the referenced symbol will be stored in the udata
-          field.  Use that to find the string index.  */
-       s->symbol.value = 
-           ((bfd_mach_o_asymbol *)s->symbol.udata.p)->symbol.udata.i;
      
       if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
        /* An index of 0 always means the empty string.  */
@@ -1377,11 +1414,6 @@ bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
       else
         {
           str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
-          /* Record the string index.  This can be looked up by an indirect sym
-            which retains a pointer to its referenced counterpart, until it is
-            actually output.  */
-         if (IS_MACHO_INDIRECT (s->n_type))
-           s->symbol.udata.i = str_index;
 
           if (str_index == (bfd_size_type) -1)
             goto err;
@@ -1627,28 +1659,24 @@ bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
 }
 
 static unsigned
-bfd_mach_o_primary_symbol_sort_key (unsigned type)
+bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol *s)
 {
-  unsigned mtyp = type & BFD_MACH_O_N_TYPE;
+  unsigned mtyp = s->n_type & BFD_MACH_O_N_TYPE;
 
   /* Just leave debug symbols where they are (pretend they are local, and
      then they will just be sorted on position).  */
-  if (type & BFD_MACH_O_N_STAB)
+  if (s->n_type & BFD_MACH_O_N_STAB)
     return 0;
 
-  /* Sort indirects to last.  */
-  if (mtyp == BFD_MACH_O_N_INDR)
-    return 3;
-
   /* Local (we should never see an undefined local AFAICT).  */
-  if (! (type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT)))
+  if (! (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT)))
     return 0;
 
   /* Common symbols look like undefined externs.  */
   if (mtyp == BFD_MACH_O_N_UNDF)
     return 2;
 
-  /* A defined symbol that's not indirect or extern.  */
+  /* A defined non-local, non-debug symbol.  */
   return 1;
 }
 
@@ -1659,8 +1687,8 @@ bfd_mach_o_cf_symbols (const void *a, const void *b)
   bfd_mach_o_asymbol *sb = *(bfd_mach_o_asymbol **) b;
   unsigned int soa, sob;
 
-  soa = bfd_mach_o_primary_symbol_sort_key (sa->n_type);
-  sob = bfd_mach_o_primary_symbol_sort_key (sb->n_type);
+  soa = bfd_mach_o_primary_symbol_sort_key (sa);
+  sob = bfd_mach_o_primary_symbol_sort_key (sb);
   if (soa < sob)
     return -1;
 
@@ -1674,55 +1702,13 @@ bfd_mach_o_cf_symbols (const void *a, const void *b)
         return -1;
       if (sa->symbol.udata.i > sb->symbol.udata.i)
         return  1;
+
+      /* This is probably an error.  */
       return 0;
     }
 
-  /* Unless it's an indirect the second sort key is name.  */
-  if (soa < 3)
-    return strcmp (sa->symbol.name, sb->symbol.name);
-
-  /* Here be indirect symbols, which have different sort rules.  */
-
-  /* Next sort key for indirect, is the section index.  */
-  if (sa->n_sect < sb->n_sect)
-    return -1;
-
-  if (sa->n_sect > sb->n_sect)
-    return  1;
-
-  /* Last sort key is the order of definition - which should be in line with
-     the value, since a stub size of 0 is meaninglesss.  */
-
-  if (sa->symbol.value < sb->symbol.value)
-    return -1;
-
-  if (sa->symbol.value > sb->symbol.value)
-    return 1;
-
-  /* In the final analysis, this is probably an error ... but leave it alone
-     for now.  */
-  return 0;
-}
-
-/* When this is finished, return the number of non-indirect symbols.  */
-
-static unsigned int
-bfd_mach_o_sort_symbol_table (asymbol **symbols, unsigned int nin)
-{
-  qsort (symbols, (size_t) nin, sizeof (void *), bfd_mach_o_cf_symbols);
-  
-  /* Find the last non-indirect symbol.  
-     There must be at least one non-indirect symbol otherwise there's
-     nothing for the indirect(s) to refer to.  */
-  do
-    { 
-      bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[nin - 1];
-      if (IS_MACHO_INDIRECT (s->n_type))
-       nin--;
-      else
-       break;
-    } while (nin - 1 > 0);
-  return nin;
+  /* The second sort key is name.  */
+  return strcmp (sa->symbol.name, sb->symbol.name);
 }
 
 /* Process the symbols.
@@ -1738,18 +1724,14 @@ bfd_mach_o_sort_symbol_table (asymbol **symbols, unsigned int nin)
                (unsorted)
        ( ii) external defined
                (by name)
-       (iii) external undefined
+       (iii) external undefined/common
                (by name)
        ( iv) common
                (by name)
-       (  v) indirect 
-               (by section)
-                       (by position within section).
-
-   (c) Indirect symbols are moved to the end of the list.  */
+*/
 
 static bfd_boolean
-bfd_mach_o_mangle_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
+bfd_mach_o_mangle_symbols (bfd *abfd)
 {
   unsigned long i;
   asymbol **symbols = bfd_get_outsymbols (abfd);
@@ -1761,11 +1743,15 @@ bfd_mach_o_mangle_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
     {
       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))
+      /* We use this value, which is out-of-range as a symbol index, to signal
+        that the mach-o-specific data are not filled in and need to be created
+        from the bfd values.  It is much preferable for the application to do
+        this, since more meaningful diagnostics can be made that way.  */
+
+      if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
         {
-          /* 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.  */
+          /* No symbol information has been set - therefore determine
+             it from the bfd symbol flags/info.  */
           if (s->symbol.section == bfd_abs_section_ptr)
             s->n_type = BFD_MACH_O_N_ABS;
           else if (s->symbol.section == bfd_und_section_ptr)
@@ -1775,9 +1761,13 @@ bfd_mach_o_mangle_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
                 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
               /* mach-o automatically makes undefined symbols extern.  */
              s->n_type |= BFD_MACH_O_N_EXT;
+             s->symbol.flags |= BSF_GLOBAL;
             }
           else if (s->symbol.section == bfd_com_section_ptr)
-            s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
+           {
+              s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
+              s->symbol.flags |= BSF_GLOBAL;
+            }
           else
             s->n_type = BFD_MACH_O_N_SECT;
           
@@ -1793,54 +1783,18 @@ bfd_mach_o_mangle_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
                && s->symbol.name == NULL))
        s->n_sect = s->symbol.section->target_index;
 
-      /* Unless we're looking at an indirect sym, note the input ordering.
-        We use this to keep local symbols ordered as per the input.  */
-      if (! IS_MACHO_INDIRECT (s->n_type))
-       s->symbol.udata.i = i;
+      /* Number to preserve order for local and debug syms.  */
+      s->symbol.udata.i = i;
     }
 
-  /* Sort the symbols and determine how many will remain in the main symbol
-     table, and how many will be emitted as indirect (assuming that we will
-     be emitting a dysymtab).  Renumber the sorted symbols so that the right
-     index will be found during indirection.  */
-  i = bfd_mach_o_sort_symbol_table (symbols, bfd_get_symcount (abfd));
-  if (bfd_mach_o_should_emit_dysymtab ())
-    {
-      /* Point at the first indirect symbol.  */
-      if (i < bfd_get_symcount (abfd))
-       {
-         mdata->indirect_syms = &symbols[i];
-         mdata->nindirect = bfd_get_symcount (abfd) - i;
-         /* This is, essentially, local to the output section of mach-o,
-            and therefore should be safe.  */
-         abfd->symcount = i;
-       }
+  /* Sort the symbols.  */
+  qsort ((void *) symbols, (size_t) bfd_get_symcount (abfd),
+        sizeof (asymbol *), bfd_mach_o_cf_symbols);
 
-      /* Now setup the counts for each type of symbol.  */
-      for (i = 0; i < bfd_get_symcount (abfd); ++i)
-       {
-         bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
-         s->symbol.udata.i = i;  /* renumber.  */
-         if (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT))
-           break;
-       }
-      mdata->nlocal = i;
-      for (; i < bfd_get_symcount (abfd); ++i)
-       {
-         bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
-         s->symbol.udata.i = i;  /* renumber.  */
-         if ((s->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF)
-           break;
-       }
-      mdata->ndefext = i - mdata->nlocal;
-      mdata->nundefext = bfd_get_symcount (abfd) 
-                        - mdata->ndefext 
-                        - mdata->nlocal;
-      for (; i < bfd_get_symcount (abfd); ++i)
-       {
-         bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
-         s->symbol.udata.i = i;  /* renumber.  */
-       }
+  for (i = 0; i < bfd_get_symcount (abfd); ++i)
+    {
+      bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
+      s->symbol.udata.i = i;  /* renumber.  */
     }
 
   return TRUE;
@@ -2069,6 +2023,7 @@ bfd_mach_o_build_seg_command (const char *segment,
 
   /* TODO: fix this up for non-MH_OBJECT cases.  */
   seg->vmaddr = 0;
+  seg->vmsize = 0;
 
   seg->fileoff = mdata->filelen;
   seg->filesize = 0;
@@ -2094,9 +2049,21 @@ bfd_mach_o_build_seg_command (const char *segment,
 
       bfd_mach_o_append_section_to_segment (seg, sec);
 
-      if (s->size == 0)
-         s->offset = 0;
-      else
+      s->offset = 0;
+      if (s->size > 0)
+       {
+          seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
+         seg->vmsize += s->size;
+        }
+      
+      /* Zerofill sections have zero file size & offset, 
+        and are not written.  */
+      if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL
+          || (s->flags & BFD_MACH_O_SECTION_TYPE_MASK) 
+             == BFD_MACH_O_S_GB_ZEROFILL)
+        continue;
+
+      if (s->size > 0)
        {
           mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
           s->offset = mdata->filelen;
@@ -2108,11 +2075,37 @@ bfd_mach_o_build_seg_command (const char *segment,
     }
 
   seg->filesize = mdata->filelen - seg->fileoff;
-  seg->vmsize = seg->filesize;
 
   return TRUE;
 }
 
+/* Count the number of indirect symbols in the image.
+   Requires that the sections are in their final order.  */
+
+static unsigned int
+bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
+{
+  unsigned int i;
+  unsigned int nisyms = 0;
+
+  for (i = 0; i < mdata->nsects; ++i)
+    {
+      bfd_mach_o_section *sec = mdata->sections[i];
+
+      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:
+           nisyms += bfd_mach_o_section_get_nbr_indirect (abfd, sec);
+           break;
+         default:
+           break;
+       }
+    }
+  return nisyms;
+}
+
 static bfd_boolean
 bfd_mach_o_build_dysymtab_command (bfd *abfd,
                                   bfd_mach_o_data_struct *mdata,
@@ -2133,42 +2126,86 @@ bfd_mach_o_build_dysymtab_command (bfd *abfd,
     }
 
   dsym->ilocalsym = 0;
-  dsym->nlocalsym = mdata->nlocal;
-  dsym->iextdefsym = dsym->nlocalsym;
-  dsym->nextdefsym = mdata->ndefext;
-  dsym->iundefsym = dsym->nextdefsym + dsym->iextdefsym;
-  dsym->nundefsym = mdata->nundefext;
 
-  if (mdata->nindirect > 0)
+  if (bfd_get_symcount (abfd) > 0)
+    {
+      asymbol **symbols = bfd_get_outsymbols (abfd);
+      unsigned long i;
+
+       /* Count the number of each kind of symbol.  */
+      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_EXT | BFD_MACH_O_N_PEXT))
+           break;
+       }
+      dsym->nlocalsym = i;
+      dsym->iextdefsym = i;
+      for (; 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_TYPE) == BFD_MACH_O_N_UNDF)
+           break;
+       }
+      dsym->nextdefsym = i - dsym->nlocalsym;
+      dsym->iundefsym = dsym->nextdefsym + dsym->iextdefsym;
+      dsym->nundefsym = bfd_get_symcount (abfd) 
+                       - dsym->nlocalsym 
+                       - dsym->nextdefsym;
+    }
+  else
     {
-      unsigned i, sect;
+      dsym->nlocalsym = 0;
+      dsym->iextdefsym = 0;
+      dsym->nextdefsym = 0;
+      dsym->iundefsym = 0;
+      dsym->nundefsym = 0;
+    }
+
+  dsym->nindirectsyms = bfd_mach_o_count_indirect_symbols (abfd, mdata);
+  if (dsym->nindirectsyms > 0)
+    {
+      unsigned i;
+      unsigned n;
 
       mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
       dsym->indirectsymoff = mdata->filelen;
-      mdata->filelen += mdata->nindirect * 4;
+      mdata->filelen += dsym->nindirectsyms * 4;
       
-      dsym->indirect_syms = bfd_zalloc (abfd, mdata->nindirect * 4);
+      dsym->indirect_syms = bfd_zalloc (abfd, dsym->nindirectsyms * 4);
       if (dsym->indirect_syms == NULL)
         return FALSE;
-      dsym->nindirectsyms = mdata->nindirect;
-      
-      /* So fill in the indices, and point the section reserved1 fields
-        at the right one.  */
-      sect = (unsigned) -1;
-      for (i = 0; i < mdata->nindirect; ++i)
+                 
+      n = 0;
+      for (i = 0; i < mdata->nsects; ++i)
        {
-         bfd_mach_o_asymbol *s = 
-                       (bfd_mach_o_asymbol *) mdata->indirect_syms[i];
-         /* Lookup the index of the referenced symbol.  */
-         dsym->indirect_syms[i] = 
-               ((bfd_mach_o_asymbol *) s->symbol.udata.p)->symbol.udata.i;
-         if (s->n_sect != sect)
+         bfd_mach_o_section *sec = mdata->sections[i];
+
+         switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
            {
-             /* Mach-o sections are 1-based, but the section table
-                is 0-based.  */
-             bfd_mach_o_section *sc = mdata->sections[s->n_sect-1];
-             sc->reserved1 = i;
-             sect = s->n_sect;
+             case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
+             case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
+             case BFD_MACH_O_S_SYMBOL_STUBS:
+               {
+                 unsigned j, num;
+                 bfd_mach_o_asymbol **isyms = sec->indirect_syms;
+                 
+                 num = bfd_mach_o_section_get_nbr_indirect (abfd, sec);
+                 if (isyms == NULL || num == 0)
+                   break;
+                 /* Record the starting index in the reserved1 field.  */
+                 sec->reserved1 = n;
+                 for (j = 0; j < num; j++, n++)
+                   {
+                     if (isyms[j] == NULL)
+                       dsym->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
+                     else
+                       dsym->indirect_syms[n] = isyms[j]->symbol.udata.i;
+                   }
+               }
+               break;
+             default:
+               break;
            }
        }
     }
@@ -2184,11 +2221,11 @@ bfd_boolean
 bfd_mach_o_build_commands (bfd *abfd)
 {
   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;
-  bfd_mach_o_load_command *cmd;
-  bfd_mach_o_load_command *symtab_cmd;
-  unsigned symcind;
+  unsigned wide = mach_o_wide_p (&mdata->header);
+  int segcmd_idx = -1;
+  int symtab_idx = -1;
+  int dysymtab_idx = -1;
+  unsigned long base_offset = 0;
 
   /* Return now if commands are already present.  */
   if (mdata->header.ncmds)
@@ -2215,34 +2252,51 @@ bfd_mach_o_build_commands (bfd *abfd)
 
   /* Order the symbol table, fill-in/check mach-o specific fields and
      partition out any indirect symbols.  */
-  if (!bfd_mach_o_mangle_symbols (abfd, mdata))
+  if (!bfd_mach_o_mangle_symbols (abfd))
     return FALSE;
 
-  /* It's valid to have a file with only absolute symbols...  */
+  /* Very simple command set (only really applicable to MH_OBJECTs):
+     All the commands are optional - present only when there is suitable data.
+     (i.e. it is valid to have an empty file)
+
+       a command (segment) to contain all the sections,
+       command for the symbol table,
+       a command for the dysymtab.  
+
+     ??? maybe we should assert that this is an MH_OBJECT?  */
+
   if (mdata->nsects > 0)
     {
+      segcmd_idx = 0;
       mdata->header.ncmds = 1;
-      symcind = 1;
     }
-  else
-    symcind = 0;
 
-  /* It's OK to have a file with only section statements.  */
   if (bfd_get_symcount (abfd) > 0)
-    mdata->header.ncmds += 1;
+    {
+      mdata->header.ncmds++;
+      symtab_idx = segcmd_idx + 1; /* 0 if the seg command is absent.  */
+    }
 
-  /* Very simple version (only really applicable to MH_OBJECTs):
-       a command (segment) to contain all the sections,
-       a command for the symbol table
-       a n (optional) command for the dysymtab.  
+  /* FIXME:
+     This is a rather crude test for whether we should build a dysymtab.  */
+  if (bfd_mach_o_should_emit_dysymtab ()
+      && bfd_get_symcount (abfd))
+    {
+      mdata->header.ncmds++;
+      /* If there should be a case where a dysymtab could be emitted without
+        a symtab (seems improbable), this would need amending.  */
+      dysymtab_idx = symtab_idx + 1;
+    }
 
-     ??? maybe we should assert that this is an MH_OBJECT?  */
+  if (wide)
+    base_offset = BFD_MACH_O_HEADER_64_SIZE;
+  else
+    base_offset = BFD_MACH_O_HEADER_SIZE;
 
-  if (bfd_mach_o_should_emit_dysymtab ()
-      && bfd_get_symcount (abfd) > 0)
-    mdata->header.ncmds += 1;
+  /* Well, we must have a header, at least.  */
+  mdata->filelen = base_offset;
 
-  /* A bit weird, but looks like no content;
+  /* A bit unusual, but no content is valid;
      as -n empty.s -o empty.o  */
   if (mdata->header.ncmds == 0)
     return TRUE;
@@ -2252,10 +2306,10 @@ bfd_mach_o_build_commands (bfd *abfd)
   if (mdata->commands == NULL)
     return FALSE;
 
-  if (mdata->nsects > 0)
+  if (segcmd_idx >= 0)
     {  
-      cmd = &mdata->commands[0];
-      seg = &cmd->command.segment;
+      bfd_mach_o_load_command *cmd = &mdata->commands[segcmd_idx];
+      bfd_mach_o_segment_command *seg = &cmd->command.segment;
 
       /* Count the segctions in the special blank segment used for MH_OBJECT.  */
       seg->nsects = bfd_mach_o_count_sections_for_seg (NULL, mdata);
@@ -2263,52 +2317,61 @@ bfd_mach_o_build_commands (bfd *abfd)
        return FALSE;
 
       /* Init segment command.  */
+      cmd->offset = base_offset;
       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;
+      mdata->filelen += cmd->len;
     }
 
-  if (bfd_get_symcount (abfd) > 0)
+  if (symtab_idx >= 0)
     {
       /* Init symtab command.  */
-      symtab_cmd = &mdata->commands[symcind];
+      bfd_mach_o_load_command *cmd = &mdata->commands[symtab_idx];
   
-      symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
-      if (symcind > 0)
-        symtab_cmd->offset = mdata->commands[0].offset 
-                            + mdata->commands[0].len;
-      else
-        symtab_cmd->offset = 0;
-      symtab_cmd->len = 6 * 4;
-      symtab_cmd->type_required = FALSE;
-  
-      mdata->header.sizeofcmds += symtab_cmd->len;
-      mdata->filelen += symtab_cmd->len;
+      cmd->type = BFD_MACH_O_LC_SYMTAB;
+      cmd->offset = base_offset;
+      if (segcmd_idx >= 0)
+        cmd->offset += mdata->commands[segcmd_idx].len;
+
+      cmd->len = sizeof (struct mach_o_symtab_command_external)
+                + BFD_MACH_O_LC_SIZE;
+      cmd->type_required = FALSE;
+      mdata->header.sizeofcmds += cmd->len;
+      mdata->filelen += cmd->len;
     }
 
-  /* If required, setup symtab command.  */
-  if (bfd_mach_o_should_emit_dysymtab ()
-      && bfd_get_symcount (abfd) > 0)
+  /* If required, setup symtab command, see comment above about the quality
+     of this test.  */
+  if (dysymtab_idx >= 0)
     {
-      cmd = &mdata->commands[symcind+1];
+      bfd_mach_o_load_command *cmd = &mdata->commands[dysymtab_idx];
+
       cmd->type = BFD_MACH_O_LC_DYSYMTAB;
-      cmd->offset = symtab_cmd->offset + symtab_cmd->len;
+      if (symtab_idx >= 0)
+        cmd->offset = mdata->commands[symtab_idx].offset 
+                   + mdata->commands[symtab_idx].len;
+      else if (segcmd_idx >= 0)
+        cmd->offset = mdata->commands[segcmd_idx].offset 
+                   + mdata->commands[segcmd_idx].len;
+      else
+       cmd->offset = base_offset;
+
       cmd->type_required = FALSE;
-      cmd->len = 18 * 4 + BFD_MACH_O_LC_SIZE;
+      cmd->len = sizeof (struct mach_o_dysymtab_command_external)
+                + BFD_MACH_O_LC_SIZE;
 
       mdata->header.sizeofcmds += cmd->len;
       mdata->filelen += cmd->len;
@@ -2316,15 +2379,15 @@ bfd_mach_o_build_commands (bfd *abfd)
 
   /* So, now we have sized the commands and the filelen set to that.
      Now we can build the segment command and set the section file offsets.  */
-  if (mdata->nsects > 0
-      && ! bfd_mach_o_build_seg_command (NULL, mdata, seg))
+  if (segcmd_idx >= 0
+      && ! bfd_mach_o_build_seg_command 
+               (NULL, mdata, &mdata->commands[segcmd_idx].command.segment))
     return FALSE;
 
   /* If we're doing a dysymtab, cmd points to its load command.  */
-  if (bfd_mach_o_should_emit_dysymtab ()
-      && bfd_get_symcount (abfd) > 0
+  if (dysymtab_idx >= 0
       && ! bfd_mach_o_build_dysymtab_command (abfd, mdata, 
-                                             &mdata->commands[symcind+1]))
+                                             &mdata->commands[dysymtab_idx]))
     return FALSE;
 
   /* The symtab command is filled in when the symtab is written.  */
@@ -2377,7 +2440,7 @@ bfd_mach_o_make_empty_symbol (bfd *abfd)
   if (new_symbol == NULL)
     return new_symbol;
   new_symbol->the_bfd = abfd;
-  new_symbol->udata.i = 0;
+  new_symbol->udata.i = SYM_MACHO_FIELDS_UNSET;
   return new_symbol;
 }
 
@@ -2705,7 +2768,7 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd,
   s->symbol.name = sym->strtab + stroff;
   s->symbol.value = value;
   s->symbol.flags = 0x0;
-  s->symbol.udata.i = 0;
+  s->symbol.udata.i = i;
   s->n_type = type;
   s->n_sect = section;
   s->n_desc = desc;
@@ -2736,13 +2799,9 @@ bfd_mach_o_read_symtab_symbol (bfd *abfd,
     }
   else
     {
-      if (type & BFD_MACH_O_N_PEXT)
+      if (type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
        s->symbol.flags |= BSF_GLOBAL;
-
-      if (type & BFD_MACH_O_N_EXT)
-       s->symbol.flags |= BSF_GLOBAL;
-
-      if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
+      else
        s->symbol.flags |= BSF_LOCAL;
 
       switch (symtype)
@@ -2998,6 +3057,32 @@ bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
   return 0;
 }
 
+static int
+bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib;
+  struct mach_o_fvmlib_command_external raw;
+  unsigned int nameoff;
+
+  if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return -1;
+
+  nameoff = bfd_h_get_32 (abfd, raw.name);
+  fvm->minor_version = bfd_h_get_32 (abfd, raw.minor_version);
+  fvm->header_addr = bfd_h_get_32 (abfd, raw.header_addr);
+
+  fvm->name_offset = command->offset + nameoff;
+  fvm->name_len = command->len - nameoff;
+  fvm->name_str = bfd_alloc (abfd, fvm->name_len);
+  if (fvm->name_str == NULL)
+    return -1;
+  if (bfd_seek (abfd, fvm->name_offset, SEEK_SET) != 0
+      || bfd_bread (fvm->name_str, fvm->name_len, abfd) != fvm->name_len)
+    return -1;
+  return 0;
+}
+
 static int
 bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
 {
@@ -3411,6 +3496,22 @@ bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command)
   return TRUE;
 }
 
+static bfd_boolean
+bfd_mach_o_read_encryption_info (bfd *abfd, bfd_mach_o_load_command *command)
+{
+  bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info;
+  struct mach_o_encryption_info_command_external raw;
+
+  if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+      || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
+    return FALSE;
+
+  cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff);
+  cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize);
+  cmd->cryptid = bfd_get_32 (abfd, raw.cryptid);
+  return TRUE;
+}
+
 static int
 bfd_mach_o_read_segment (bfd *abfd,
                          bfd_mach_o_load_command *command,
@@ -3556,6 +3657,9 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
       break;
     case BFD_MACH_O_LC_LOADFVMLIB:
     case BFD_MACH_O_LC_IDFVMLIB:
+      if (bfd_mach_o_read_fvmlib (abfd, command) != 0)
+       return -1;
+      break;
     case BFD_MACH_O_LC_IDENT:
     case BFD_MACH_O_LC_FVMFILE:
     case BFD_MACH_O_LC_PREPAGE:
@@ -3587,6 +3691,10 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
       if (bfd_mach_o_read_linkedit (abfd, command) != 0)
        return -1;
       break;
+    case BFD_MACH_O_LC_ENCRYPTION_INFO:
+      if (!bfd_mach_o_read_encryption_info (abfd, command))
+       return -1;
+      break;
     case BFD_MACH_O_LC_DYLD_INFO:
       if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
        return -1;
@@ -3597,7 +3705,7 @@ bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
        return -1;
       break;
     default:
-      (*_bfd_error_handler)(_("%B: unable to read unknown load command 0x%lx"),
+      (*_bfd_error_handler)(_("%B: unknown load command 0x%lx"),
          abfd, (unsigned long) command->type);
       break;
     }
@@ -3894,6 +4002,7 @@ bfd_mach_o_header_p (bfd *abfd,
       if (header.cputype != cputype)
         goto wrong;
     }
+
   if (filetype)
     {
       if (header.filetype != filetype)
@@ -4012,6 +4121,42 @@ bfd_mach_o_archive_p (bfd *abfd)
   return NULL;
 }
 
+/* Set the filename for a fat binary member ABFD, whose bfd architecture is
+   ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY.
+   Set arelt_data and origin fields too.  */
+
+static void
+bfd_mach_o_fat_member_init (bfd *abfd,
+                            enum bfd_architecture arch_type,
+                            unsigned long arch_subtype,
+                            mach_o_fat_archentry *entry)
+{
+  struct areltdata *areltdata;
+  /* Create the member filename. Use ARCH_NAME.  */
+  const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype);
+
+  if (ap)
+    {
+      /* Use the architecture name if known.  */
+      abfd->filename = ap->printable_name;
+    }
+  else
+    {
+      /* Forge a uniq id.  */
+      const size_t namelen = 2 + 8 + 1 + 2 + 8 + 1;
+      char *name = bfd_alloc (abfd, namelen);
+      snprintf (name, namelen, "0x%lx-0x%lx",
+                entry->cputype, entry->cpusubtype);
+      abfd->filename = name;
+    }
+
+  areltdata = bfd_zalloc (abfd, sizeof (struct areltdata));
+  areltdata->parsed_size = entry->size;
+  abfd->arelt_data = areltdata;
+  abfd->iostream = NULL;
+  abfd->origin = entry->offset;
+}
+
 bfd *
 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
 {
@@ -4027,9 +4172,13 @@ bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
 
   /* Find index of previous entry.  */
   if (prev == NULL)
-    i = 0;     /* Start at first one.  */
+    {
+      /* Start at first one.  */
+      i = 0;
+    }
   else
     {
+      /* Find index of PREV.  */
       for (i = 0; i < adata->nfat_arch; i++)
        {
          if (adata->archentries[i].offset == prev->origin)
@@ -4042,8 +4191,10 @@ bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
          bfd_set_error (bfd_error_bad_value);
          return NULL;
        }
-    i++;       /* Get next entry.  */
-  }
+
+      /* Get next entry.  */
+      i++;
+    }
 
   if (i >= adata->nfat_arch)
     {
@@ -4056,24 +4207,42 @@ bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
   if (nbfd == NULL)
     return NULL;
 
-  nbfd->origin = entry->offset;
-
   bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
                                   &arch_type, &arch_subtype);
 
-  /* Create the member filename. Use ARCH_NAME.  */
-  nbfd->filename = bfd_printable_arch_mach (arch_type, arch_subtype);
-  nbfd->iostream = NULL;
+  bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry);
+
   bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
 
   return nbfd;
 }
 
+/* Analogous to stat call.  */
+
+static int
+bfd_mach_o_fat_stat_arch_elt (bfd *abfd, struct stat *buf)
+{
+  if (abfd->arelt_data == NULL)
+    {
+      bfd_set_error (bfd_error_invalid_operation);
+      return -1;
+    }
+
+  buf->st_mtime = 0;
+  buf->st_uid = 0;
+  buf->st_gid = 0;
+  buf->st_mode = 0644;
+  buf->st_size = arelt_size (abfd);
+
+  return 0;
+}
+
 /* 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,
@@ -4113,10 +4282,7 @@ bfd_mach_o_fat_extract (bfd *abfd,
       if (res == NULL)
        return NULL;
 
-      res->origin = e->offset;
-
-      res->filename = bfd_printable_arch_mach (cpu_type, cpu_subtype);
-      res->iostream = NULL;
+      bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e);
 
       if (bfd_check_format (res, format))
        {
@@ -4624,7 +4790,7 @@ bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
 #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_generic_stat_arch_elt          bfd_mach_o_fat_stat_arch_elt
 #define bfd_mach_o_update_armap_timestamp         _bfd_noarchive_update_armap_timestamp
 
 #define TARGET_NAME            mach_o_fat_vec
This page took 0.035725 seconds and 4 git commands to generate.