+/* Write a dysymtab command.
+ TODO: Possibly coalesce writes of smaller objects. */
+
+static bfd_boolean
+bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
+{
+ bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
+
+ BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
+
+ if (cmd->nmodtab != 0)
+ {
+ unsigned int i;
+
+ if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
+ return FALSE;
+
+ for (i = 0; i < cmd->nmodtab; i++)
+ {
+ bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
+ unsigned int iinit;
+ unsigned int ninit;
+
+ iinit = module->iinit & 0xffff;
+ iinit |= ((module->iterm & 0xffff) << 16);
+
+ ninit = module->ninit & 0xffff;
+ ninit |= ((module->nterm & 0xffff) << 16);
+
+ if (bfd_mach_o_wide_p (abfd))
+ {
+ struct mach_o_dylib_module_64_external w;
+
+ bfd_h_put_32 (abfd, module->module_name_idx, &w.module_name);
+ bfd_h_put_32 (abfd, module->iextdefsym, &w.iextdefsym);
+ bfd_h_put_32 (abfd, module->nextdefsym, &w.nextdefsym);
+ bfd_h_put_32 (abfd, module->irefsym, &w.irefsym);
+ bfd_h_put_32 (abfd, module->nrefsym, &w.nrefsym);
+ bfd_h_put_32 (abfd, module->ilocalsym, &w.ilocalsym);
+ bfd_h_put_32 (abfd, module->nlocalsym, &w.nlocalsym);
+ bfd_h_put_32 (abfd, module->iextrel, &w.iextrel);
+ bfd_h_put_32 (abfd, module->nextrel, &w.nextrel);
+ bfd_h_put_32 (abfd, iinit, &w.iinit_iterm);
+ bfd_h_put_32 (abfd, ninit, &w.ninit_nterm);
+ bfd_h_put_64 (abfd, module->objc_module_info_addr,
+ &w.objc_module_info_addr);
+ bfd_h_put_32 (abfd, module->objc_module_info_size,
+ &w.objc_module_info_size);
+
+ if (bfd_bwrite ((void *) &w, sizeof (w), abfd) != sizeof (w))
+ return FALSE;
+ }
+ else
+ {
+ struct mach_o_dylib_module_external n;
+
+ bfd_h_put_32 (abfd, module->module_name_idx, &n.module_name);
+ bfd_h_put_32 (abfd, module->iextdefsym, &n.iextdefsym);
+ bfd_h_put_32 (abfd, module->nextdefsym, &n.nextdefsym);
+ bfd_h_put_32 (abfd, module->irefsym, &n.irefsym);
+ bfd_h_put_32 (abfd, module->nrefsym, &n.nrefsym);
+ bfd_h_put_32 (abfd, module->ilocalsym, &n.ilocalsym);
+ bfd_h_put_32 (abfd, module->nlocalsym, &n.nlocalsym);
+ bfd_h_put_32 (abfd, module->iextrel, &n.iextrel);
+ bfd_h_put_32 (abfd, module->nextrel, &n.nextrel);
+ bfd_h_put_32 (abfd, iinit, &n.iinit_iterm);
+ bfd_h_put_32 (abfd, ninit, &n.ninit_nterm);
+ bfd_h_put_32 (abfd, module->objc_module_info_addr,
+ &n.objc_module_info_addr);
+ bfd_h_put_32 (abfd, module->objc_module_info_size,
+ &n.objc_module_info_size);
+
+ if (bfd_bwrite ((void *) &n, sizeof (n), abfd) != sizeof (n))
+ return FALSE;
+ }
+ }
+ }
+
+ if (cmd->ntoc != 0)
+ {
+ unsigned int i;
+
+ if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
+ return FALSE;
+
+ for (i = 0; i < cmd->ntoc; i++)
+ {
+ struct mach_o_dylib_table_of_contents_external raw;
+ bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
+
+ bfd_h_put_32 (abfd, toc->symbol_index, &raw.symbol_index);
+ bfd_h_put_32 (abfd, toc->module_index, &raw.module_index);
+
+ if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+ }
+ }
+
+ if (cmd->nindirectsyms > 0)
+ {
+ unsigned int i;
+
+ if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
+ return FALSE;
+
+ for (i = 0; i < cmd->nindirectsyms; ++i)
+ {
+ unsigned char raw[4];
+
+ bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw);
+ if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+ }
+ }
+
+ if (cmd->nextrefsyms != 0)
+ {
+ unsigned int i;
+
+ if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
+ return FALSE;
+
+ for (i = 0; i < cmd->nextrefsyms; i++)
+ {
+ unsigned long v;
+ unsigned char raw[4];
+ bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
+
+ /* Fields isym and flags are written as bit-fields, thus we need
+ a specific processing for endianness. */
+
+ if (bfd_big_endian (abfd))
+ {
+ v = ((ref->isym & 0xffffff) << 8);
+ v |= ref->flags & 0xff;
+ }
+ else
+ {
+ v = ref->isym & 0xffffff;
+ v |= ((ref->flags & 0xff) << 24);
+ }
+
+ bfd_h_put_32 (abfd, v, raw);
+ if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+ }
+ }
+
+ /* The command. */
+ if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0)
+ return FALSE;
+ else
+ {
+ struct mach_o_dysymtab_command_external raw;
+
+ bfd_h_put_32 (abfd, cmd->ilocalsym, &raw.ilocalsym);
+ bfd_h_put_32 (abfd, cmd->nlocalsym, &raw.nlocalsym);
+ bfd_h_put_32 (abfd, cmd->iextdefsym, &raw.iextdefsym);
+ bfd_h_put_32 (abfd, cmd->nextdefsym, &raw.nextdefsym);
+ bfd_h_put_32 (abfd, cmd->iundefsym, &raw.iundefsym);
+ bfd_h_put_32 (abfd, cmd->nundefsym, &raw.nundefsym);
+ bfd_h_put_32 (abfd, cmd->tocoff, &raw.tocoff);
+ bfd_h_put_32 (abfd, cmd->ntoc, &raw.ntoc);
+ bfd_h_put_32 (abfd, cmd->modtaboff, &raw.modtaboff);
+ bfd_h_put_32 (abfd, cmd->nmodtab, &raw.nmodtab);
+ bfd_h_put_32 (abfd, cmd->extrefsymoff, &raw.extrefsymoff);
+ bfd_h_put_32 (abfd, cmd->nextrefsyms, &raw.nextrefsyms);
+ bfd_h_put_32 (abfd, cmd->indirectsymoff, &raw.indirectsymoff);
+ bfd_h_put_32 (abfd, cmd->nindirectsyms, &raw.nindirectsyms);
+ bfd_h_put_32 (abfd, cmd->extreloff, &raw.extreloff);
+ bfd_h_put_32 (abfd, cmd->nextrel, &raw.nextrel);
+ bfd_h_put_32 (abfd, cmd->locreloff, &raw.locreloff);
+ bfd_h_put_32 (abfd, cmd->nlocrel, &raw.nlocrel);
+
+ if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static unsigned
+bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol *s)
+{
+ 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 (s->n_type & BFD_MACH_O_N_STAB)
+ return 0;
+
+ /* Local (we should never see an undefined local AFAICT). */
+ 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 non-local, non-debug symbol. */
+ return 1;
+}
+
+static int
+bfd_mach_o_cf_symbols (const void *a, const void *b)
+{
+ bfd_mach_o_asymbol *sa = *(bfd_mach_o_asymbol **) a;
+ bfd_mach_o_asymbol *sb = *(bfd_mach_o_asymbol **) b;
+ unsigned int soa, sob;
+
+ soa = bfd_mach_o_primary_symbol_sort_key (sa);
+ sob = bfd_mach_o_primary_symbol_sort_key (sb);
+ if (soa < sob)
+ return -1;
+
+ if (soa > sob)
+ return 1;
+
+ /* If it's local or stab, just preserve the input order. */
+ if (soa == 0)
+ {
+ if (sa->symbol.udata.i < sb->symbol.udata.i)
+ return -1;
+ if (sa->symbol.udata.i > sb->symbol.udata.i)
+ return 1;
+
+ /* This is probably an error. */
+ return 0;
+ }
+
+ /* The second sort key is name. */
+ return strcmp (sa->symbol.name, sb->symbol.name);
+}
+
+/* Process the symbols.
+
+ This should be OK for single-module files - but it is not likely to work
+ for multi-module shared libraries.
+
+ (a) If the application has not filled in the relevant mach-o fields, make
+ an estimate.
+
+ (b) Order them, like this:
+ ( i) local.
+ (unsorted)
+ ( ii) external defined
+ (by name)
+ (iii) external undefined/common
+ (by name)
+ ( iv) common
+ (by name)
+*/