+ 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)
+*/
+
+static bfd_boolean
+bfd_mach_o_mangle_symbols (bfd *abfd)
+{
+ unsigned long i;
+ asymbol **symbols = bfd_get_outsymbols (abfd);
+
+ if (symbols == NULL || bfd_get_symcount (abfd) == 0)
+ return TRUE;
+
+ for (i = 0; i < bfd_get_symcount (abfd); i++)
+ {
+ bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
+
+ /* 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)
+ {
+ /* 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)
+ {
+ s->n_type = BFD_MACH_O_N_UNDF;
+ if (s->symbol.flags & BSF_WEAK)
+ 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->symbol.flags |= BSF_GLOBAL;
+ }
+ else
+ s->n_type = BFD_MACH_O_N_SECT;
+
+ if (s->symbol.flags & BSF_GLOBAL)
+ s->n_type |= BFD_MACH_O_N_EXT;
+ }
+
+ /* Put the section index in, where required. */
+ if ((s->symbol.section != bfd_abs_section_ptr
+ && s->symbol.section != bfd_und_section_ptr
+ && s->symbol.section != bfd_com_section_ptr)
+ || ((s->n_type & BFD_MACH_O_N_STAB) != 0
+ && s->symbol.name == NULL))
+ s->n_sect = s->symbol.section->output_section->target_index;
+
+ /* Number to preserve order for local and debug syms. */
+ s->symbol.udata.i = i;
+ }
+
+ /* Sort the symbols. */
+ qsort ((void *) symbols, (size_t) bfd_get_symcount (abfd),
+ sizeof (asymbol *), bfd_mach_o_cf_symbols);
+
+ 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;
+}
+
+/* We build a flat table of sections, which can be re-ordered if necessary.
+ Fill in the section number and other mach-o-specific data. */
+
+static bfd_boolean
+bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
+{
+ asection *sec;
+ unsigned target_index;
+ unsigned nsect;
+
+ nsect = bfd_count_sections (abfd);
+
+ /* Don't do it if it's already set - assume the application knows what it's
+ doing. */
+ if (mdata->nsects == nsect
+ && (mdata->nsects == 0 || mdata->sections != NULL))
+ return TRUE;
+
+ mdata->nsects = nsect;
+ mdata->sections = bfd_alloc (abfd,
+ mdata->nsects * sizeof (bfd_mach_o_section *));
+ if (mdata->sections == NULL)
+ return FALSE;
+
+ /* We need to check that this can be done... */
+ if (nsect > 255)
+ (*_bfd_error_handler) (_("mach-o: there are too many sections (%d)"
+ " maximum is 255,\n"), nsect);
+
+ /* Create Mach-O sections.
+ Section type, attribute and align should have been set when the
+ section was created - either read in or specified. */
+ target_index = 0;
+ for (sec = abfd->sections; sec; sec = sec->next)
+ {
+ unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
+ bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
+
+ mdata->sections[target_index] = msect;
+
+ msect->addr = bfd_get_section_vma (abfd, sec);
+ msect->size = bfd_get_section_size (sec);
+
+ /* Use the largest alignment set, in case it was bumped after the
+ section was created. */
+ msect->align = msect->align > bfd_align ? msect->align : bfd_align;
+
+ msect->offset = 0;
+ sec->target_index = ++target_index;
+ }
+
+ return TRUE;
+}
+
+bfd_boolean
+bfd_mach_o_write_contents (bfd *abfd)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ bfd_mach_o_load_command *cmd;
+ bfd_mach_o_symtab_command *symtab = NULL;
+ bfd_mach_o_dysymtab_command *dysymtab = NULL;
+ bfd_mach_o_segment_command *linkedit = NULL;
+
+ /* Make the commands, if not already present. */
+ if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
+ return FALSE;
+ abfd->output_has_begun = TRUE;
+
+ /* Write the header. */
+ if (!bfd_mach_o_write_header (abfd, &mdata->header))
+ return FALSE;
+
+ /* First pass: allocate the linkedit segment. */
+ for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
+ switch (cmd->type)
+ {
+ case BFD_MACH_O_LC_SEGMENT_64:
+ case BFD_MACH_O_LC_SEGMENT:
+ if (strcmp (cmd->command.segment.segname, "__LINKEDIT") == 0)
+ linkedit = &cmd->command.segment;
+ break;
+ case BFD_MACH_O_LC_SYMTAB:
+ symtab = &cmd->command.symtab;
+ break;
+ case BFD_MACH_O_LC_DYSYMTAB:
+ dysymtab = &cmd->command.dysymtab;
+ break;
+ case BFD_MACH_O_LC_DYLD_INFO:
+ {
+ bfd_mach_o_dyld_info_command *di = &cmd->command.dyld_info;
+
+ if (di->rebase_size != 0)
+ {
+ di->rebase_off = mdata->filelen;
+ mdata->filelen += di->rebase_size;
+ }
+ if (di->bind_size != 0)
+ {
+ di->bind_off = mdata->filelen;
+ mdata->filelen += di->bind_size;
+ }
+ if (di->weak_bind_size != 0)
+ {
+ di->weak_bind_off = mdata->filelen;
+ mdata->filelen += di->weak_bind_size;
+ }
+ if (di->lazy_bind_size != 0)
+ {
+ di->lazy_bind_off = mdata->filelen;
+ mdata->filelen += di->lazy_bind_size;
+ }
+ if (di->export_size != 0)
+ {
+ di->export_off = mdata->filelen;
+ mdata->filelen += di->export_size;
+ }
+ }
+ break;
+ case BFD_MACH_O_LC_LOAD_DYLIB:
+ case BFD_MACH_O_LC_LOAD_DYLINKER:
+ case BFD_MACH_O_LC_MAIN:
+ /* Nothing to do. */
+ break;
+ default:
+ (*_bfd_error_handler)
+ (_("unable to allocate data for load command 0x%lx"),
+ (unsigned long) cmd->type);
+ break;
+ }
+
+ /* Specially handle symtab and dysymtab. */
+
+ /* Pre-allocate the symbol table (but not the string table). The reason
+ is that the dysymtab is after the symbol table but before the string
+ table (required by the native strip tool). */
+ if (symtab != NULL)
+ {
+ unsigned int symlen;
+ unsigned int wide = bfd_mach_o_wide_p (abfd);
+
+ symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
+
+ /* Align for symbols. */
+ mdata->filelen = FILE_ALIGN (mdata->filelen, wide ? 3 : 2);
+ symtab->symoff = mdata->filelen;
+
+ symtab->nsyms = bfd_get_symcount (abfd);
+ mdata->filelen += symtab->nsyms * symlen;
+ }
+
+ /* Build the dysymtab. */
+ if (dysymtab != NULL)
+ if (!bfd_mach_o_build_dysymtab (abfd, dysymtab))
+ return FALSE;
+
+ /* Write symtab and strtab. */
+ if (symtab != NULL)
+ if (!bfd_mach_o_write_symtab_content (abfd, symtab))
+ return FALSE;
+
+ /* Adjust linkedit size. */
+ if (linkedit != NULL)
+ {
+ /* bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; */
+
+ linkedit->vmsize = mdata->filelen - linkedit->fileoff;
+ /* linkedit->vmsize = (linkedit->vmsize + pagemask) & ~pagemask; */
+ linkedit->filesize = mdata->filelen - linkedit->fileoff;
+
+ linkedit->initprot = BFD_MACH_O_PROT_READ;
+ linkedit->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
+ | BFD_MACH_O_PROT_EXECUTE;
+ }
+
+ /* Second pass: write commands. */
+ for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
+ {
+ struct mach_o_load_command_external raw;
+ unsigned long typeflag;
+
+ typeflag = cmd->type | (cmd->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
+
+ bfd_h_put_32 (abfd, typeflag, raw.cmd);
+ bfd_h_put_32 (abfd, cmd->len, raw.cmdsize);
+
+ if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
+ || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
+ return FALSE;
+
+ switch (cmd->type)
+ {
+ case BFD_MACH_O_LC_SEGMENT:
+ if (!bfd_mach_o_write_segment_32 (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_SEGMENT_64:
+ if (!bfd_mach_o_write_segment_64 (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_SYMTAB:
+ if (!bfd_mach_o_write_symtab (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_DYSYMTAB:
+ if (!bfd_mach_o_write_dysymtab (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_THREAD:
+ case BFD_MACH_O_LC_UNIXTHREAD:
+ if (!bfd_mach_o_write_thread (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_LOAD_DYLIB:
+ if (!bfd_mach_o_write_dylib (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_LOAD_DYLINKER:
+ if (!bfd_mach_o_write_dylinker (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_MAIN:
+ if (!bfd_mach_o_write_main (abfd, cmd))
+ return FALSE;
+ break;
+ case BFD_MACH_O_LC_DYLD_INFO:
+ if (!bfd_mach_o_write_dyld_info (abfd, cmd))
+ return FALSE;
+ break;
+ default:
+ (*_bfd_error_handler)
+ (_("unable to write unknown load command 0x%lx"),
+ (unsigned long) cmd->type);
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+static void
+bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
+ bfd_mach_o_section *s)
+{
+ if (seg->sect_head == NULL)
+ seg->sect_head = s;
+ else
+ seg->sect_tail->next = s;
+ seg->sect_tail = s;
+}
+
+/* Create section Mach-O flags from BFD flags. */
+
+static void
+bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
+ asection *sec)
+{
+ flagword bfd_flags;
+ bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
+
+ /* Create default flags. */
+ bfd_flags = bfd_get_section_flags (abfd, sec);
+ if ((bfd_flags & SEC_CODE) == SEC_CODE)
+ s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
+ | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
+ | BFD_MACH_O_S_REGULAR;
+ else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
+ s->flags = BFD_MACH_O_S_ZEROFILL;
+ else if (bfd_flags & SEC_DEBUGGING)
+ s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
+ else
+ s->flags = BFD_MACH_O_S_REGULAR;
+}
+
+static bfd_boolean
+bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ unsigned int i, j;
+
+ seg->vmaddr = 0;
+ seg->fileoff = mdata->filelen;
+ seg->initprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
+ | BFD_MACH_O_PROT_EXECUTE;
+ seg->maxprot = seg->initprot;
+
+ /* Append sections to the segment.
+
+ This is a little tedious, we have to honor the need to account zerofill
+ sections after all the rest. This forces us to do the calculation of
+ total vmsize in three passes so that any alignment increments are
+ properly accounted. */
+ for (i = 0; i < mdata->nsects; ++i)
+ {
+ bfd_mach_o_section *s = mdata->sections[i];
+ asection *sec = s->bfdsection;
+
+ /* Although we account for zerofill section sizes in vm order, they are
+ placed in the file in source sequence. */
+ bfd_mach_o_append_section_to_segment (seg, s);
+ s->offset = 0;
+
+ /* Zerofill sections have zero file size & offset, the only content
+ written to the file is the symbols. */
+ 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;
+
+ /* The Darwin system tools (in MH_OBJECT files, at least) always account
+ sections, even those with zero size. */
+ if (s->size > 0)
+ {
+ seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
+ seg->vmsize += s->size;
+
+ /* MH_OBJECT files have unaligned content. */
+ if (1)
+ {
+ seg->filesize = FILE_ALIGN (seg->filesize, s->align);
+ mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
+ }
+ seg->filesize += s->size;
+
+ /* The system tools write even zero-sized sections with an offset
+ field set to the current file position. */
+ s->offset = mdata->filelen;
+ }
+
+ sec->filepos = s->offset;
+ mdata->filelen += s->size;
+ }