+ /* 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)
+*/
+
+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;
+ }
+
+ /* Update external symbol bit in case objcopy changed it. */
+ if (s->symbol.flags & BSF_GLOBAL)
+ s->n_type |= BFD_MACH_O_N_EXT;
+ else
+ 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;
+ size_t amt;
+
+ 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;
+
+ /* We need to check that this can be done... */
+ if (nsect > 255)
+ {
+ _bfd_error_handler (_("mach-o: there are too many sections (%u)"
+ " maximum is 255,\n"), nsect);
+ return FALSE;
+ }
+
+ mdata->nsects = nsect;
+ amt = mdata->nsects * sizeof (bfd_mach_o_section *);
+ mdata->sections = bfd_alloc (abfd, amt);
+ if (mdata->sections == NULL)
+ return FALSE;
+
+ /* 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_section_alignment (sec);
+ bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
+
+ mdata->sections[target_index] = msect;
+
+ msect->addr = bfd_section_vma (sec);
+ msect->size = bfd_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 %#x"),
+ 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 %#x"),
+ 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_section_flags (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;
+ }
+
+ /* Now pass through again, for zerofill, only now we just update the
+ vmsize, and then for zerofill_GB. */
+ for (j = 0; j < 2; j++)
+ {
+ unsigned int stype;
+
+ if (j == 0)
+ stype = BFD_MACH_O_S_ZEROFILL;
+ else
+ stype = BFD_MACH_O_S_GB_ZEROFILL;
+
+ for (i = 0; i < mdata->nsects; ++i)
+ {
+ bfd_mach_o_section *s = mdata->sections[i];
+
+ if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != stype)
+ continue;
+
+ if (s->size > 0)
+ {
+ seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
+ seg->vmsize += s->size;
+ }
+ }
+ }
+
+ /* Allocate space for the relocations. */
+ mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
+
+ for (i = 0; i < mdata->nsects; ++i)
+ {
+ bfd_mach_o_section *ms = mdata->sections[i];
+ asection *sec = ms->bfdsection;
+
+ ms->nreloc = sec->reloc_count;
+ if (ms->nreloc == 0)
+ {
+ /* Clear nreloc and reloff if there is no relocs. */
+ ms->reloff = 0;
+ continue;
+ }
+ sec->rel_filepos = mdata->filelen;
+ ms->reloff = sec->rel_filepos;
+ mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
+ }
+
+ return TRUE;
+}
+
+static bfd_boolean
+bfd_mach_o_build_exec_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;
+ bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1;
+ bfd_vma vma;
+ bfd_mach_o_section *s;
+
+ seg->vmsize = 0;
+
+ seg->fileoff = mdata->filelen;
+ seg->maxprot = 0;
+ seg->initprot = 0;
+ seg->flags = 0;
+
+ /* Append sections to the segment. We assume they are properly ordered
+ by vma (but we check that). */
+ vma = 0;
+ for (i = 0; i < mdata->nsects; ++i)
+ {
+ s = mdata->sections[i];
+
+ /* Consider only sections for this segment. */
+ if (strcmp (seg->segname, s->segname) != 0)
+ continue;
+
+ bfd_mach_o_append_section_to_segment (seg, s);
+
+ if (s->addr < vma)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("section address (%#" PRIx64 ") "
+ "below start of segment (%#" PRIx64 ")"),
+ (uint64_t) s->addr, (uint64_t) vma);
+ return FALSE;
+ }
+
+ vma = s->addr + s->size;
+ }
+
+ /* Set segment file offset: make it page aligned. */
+ vma = seg->sect_head->addr;
+ seg->vmaddr = vma & ~pagemask;
+ if ((mdata->filelen & pagemask) > (vma & pagemask))
+ mdata->filelen += pagemask + 1;
+ seg->fileoff = mdata->filelen & ~pagemask;
+ mdata->filelen = seg->fileoff + (vma & pagemask);
+
+ /* Set section file offset. */
+ for (s = seg->sect_head; s != NULL; s = s->next)
+ {
+ asection *sec = s->bfdsection;
+ flagword flags = bfd_section_flags (sec);
+
+ /* Adjust segment size. */
+ seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
+ seg->vmsize += s->size;
+
+ /* File offset and length. */
+ seg->filesize = FILE_ALIGN (seg->filesize, s->align);
+
+ 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))
+ {
+ mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
+
+ s->offset = mdata->filelen;
+ s->bfdsection->filepos = s->offset;
+
+ seg->filesize += s->size;
+ mdata->filelen += s->size;
+ }
+ else
+ {
+ s->offset = 0;
+ s->bfdsection->filepos = 0;
+ }
+
+ /* Set protection. */
+ if (flags & SEC_LOAD)
+ {
+ if (flags & SEC_CODE)
+ seg->initprot |= BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_EXECUTE;
+ if ((flags & (SEC_DATA | SEC_READONLY)) == SEC_DATA)
+ seg->initprot |= BFD_MACH_O_PROT_WRITE | BFD_MACH_O_PROT_READ;
+ }
+
+ /* Relocs shouldn't appear in non-object files. */
+ if (s->bfdsection->reloc_count != 0)
+ return FALSE;
+ }
+
+ /* Set maxprot. */
+ if (seg->initprot != 0)
+ seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
+ | BFD_MACH_O_PROT_EXECUTE;
+ else
+ seg->maxprot = 0;
+
+ /* Round segment size (and file size). */
+ seg->vmsize = (seg->vmsize + pagemask) & ~pagemask;
+ seg->filesize = (seg->filesize + pagemask) & ~pagemask;
+ mdata->filelen = (mdata->filelen + pagemask) & ~pagemask;
+
+ return TRUE;
+}
+
+/* Layout the commands: set commands size and offset, set ncmds and sizeofcmds
+ fields in header. */
+
+static bfd_boolean
+bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata)
+{
+ unsigned wide = mach_o_wide_p (&mdata->header);
+ unsigned int hdrlen;
+ ufile_ptr offset;
+ bfd_mach_o_load_command *cmd;
+ unsigned int align;
+ bfd_boolean ret = TRUE;
+
+ hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
+ align = wide ? 8 - 1 : 4 - 1;
+ offset = hdrlen;
+ mdata->header.ncmds = 0;
+
+ for (cmd = mdata->first_command; cmd; cmd = cmd->next)
+ {
+ mdata->header.ncmds++;
+ cmd->offset = offset;
+
+ switch (cmd->type)
+ {
+ case BFD_MACH_O_LC_SEGMENT_64:
+ cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
+ + BFD_MACH_O_SECTION_64_SIZE * cmd->command.segment.nsects;
+ break;
+ case BFD_MACH_O_LC_SEGMENT:
+ cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
+ + BFD_MACH_O_SECTION_SIZE * cmd->command.segment.nsects;
+ break;
+ case BFD_MACH_O_LC_SYMTAB:
+ cmd->len = sizeof (struct mach_o_symtab_command_external)
+ + BFD_MACH_O_LC_SIZE;
+ break;
+ case BFD_MACH_O_LC_DYSYMTAB:
+ cmd->len = sizeof (struct mach_o_dysymtab_command_external)
+ + BFD_MACH_O_LC_SIZE;
+ break;
+ case BFD_MACH_O_LC_LOAD_DYLIB:
+ cmd->len = sizeof (struct mach_o_dylib_command_external)
+ + BFD_MACH_O_LC_SIZE;
+ cmd->command.dylib.name_offset = cmd->len;
+ cmd->len += strlen (cmd->command.dylib.name_str);
+ cmd->len = (cmd->len + align) & ~align;
+ break;
+ case BFD_MACH_O_LC_LOAD_DYLINKER:
+ cmd->len = sizeof (struct mach_o_str_command_external)
+ + BFD_MACH_O_LC_SIZE;
+ cmd->command.dylinker.name_offset = cmd->len;
+ cmd->len += strlen (cmd->command.dylinker.name_str);
+ cmd->len = (cmd->len + align) & ~align;
+ break;
+ case BFD_MACH_O_LC_MAIN:
+ cmd->len = sizeof (struct mach_o_entry_point_command_external)
+ + BFD_MACH_O_LC_SIZE;
+ break;
+ case BFD_MACH_O_LC_DYLD_INFO:
+ cmd->len = sizeof (struct mach_o_dyld_info_command_external)
+ + BFD_MACH_O_LC_SIZE;
+ break;
+ default:
+ _bfd_error_handler
+ (_("unable to layout unknown load command %#x"),
+ cmd->type);
+ ret = FALSE;
+ break;
+ }
+
+ BFD_ASSERT (cmd->len % (align + 1) == 0);
+ offset += cmd->len;
+ }
+ mdata->header.sizeofcmds = offset - hdrlen;
+ mdata->filelen = offset;
+
+ return ret;
+}
+
+/* Subroutine of bfd_mach_o_build_commands: set type, name and nsects of a
+ segment. */
+
+static void
+bfd_mach_o_init_segment (bfd_mach_o_data_struct *mdata,
+ bfd_mach_o_load_command *cmd,
+ const char *segname, unsigned int nbr_sect)
+{
+ bfd_mach_o_segment_command *seg = &cmd->command.segment;
+ unsigned wide = mach_o_wide_p (&mdata->header);
+
+ /* Init segment command. */
+ cmd->type = wide ? BFD_MACH_O_LC_SEGMENT_64 : BFD_MACH_O_LC_SEGMENT;
+ cmd->type_required = FALSE;
+
+ strcpy (seg->segname, segname);
+ seg->nsects = nbr_sect;
+
+ seg->vmaddr = 0;
+ seg->vmsize = 0;
+
+ seg->fileoff = 0;
+ seg->filesize = 0;
+ seg->maxprot = 0;
+ seg->initprot = 0;
+ seg->flags = 0;
+ seg->sect_head = NULL;
+ seg->sect_tail = NULL;
+}
+
+/* Build Mach-O load commands (currently assuming an MH_OBJECT file).
+ TODO: Other file formats, rebuilding symtab/dysymtab commands for strip
+ and copy functionality. */
+
+bfd_boolean
+bfd_mach_o_build_commands (bfd *abfd)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ unsigned wide = mach_o_wide_p (&mdata->header);
+ unsigned int nbr_segcmd = 0;
+ bfd_mach_o_load_command *commands;
+ unsigned int nbr_commands;
+ int symtab_idx = -1;
+ int dysymtab_idx = -1;
+ int main_idx = -1;
+ unsigned int i;
+
+ /* Return now if already built. */
+ if (mdata->header.ncmds != 0)
+ return TRUE;
+
+ /* Fill in the file type, if not already set. */
+ if (mdata->header.filetype == 0)
+ {
+ if (abfd->flags & EXEC_P)
+ mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
+ else if (abfd->flags & DYNAMIC)
+ mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
+ else
+ mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
+ }
+
+ /* If hasn't already been done, flatten sections list, and sort
+ if/when required. Must be done before the symbol table is adjusted,
+ since that depends on properly numbered sections. */
+ if (mdata->nsects == 0 || mdata->sections == NULL)
+ if (! bfd_mach_o_mangle_sections (abfd, mdata))
+ return FALSE;
+
+ /* Order the symbol table, fill-in/check mach-o specific fields and
+ partition out any indirect symbols. */
+ if (!bfd_mach_o_mangle_symbols (abfd))
+ return FALSE;
+
+ /* Segment commands. */
+ if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
+ {
+ /* Only one segment for all the sections. But the segment is
+ optional if there is no sections. */
+ nbr_segcmd = (mdata->nsects > 0) ? 1 : 0;
+ }
+ else
+ {
+ bfd_mach_o_section *prev_sect = NULL;
+
+ /* One pagezero segment and one linkedit segment. */
+ nbr_segcmd = 2;
+
+ /* Create one segment for associated segment name in sections.
+ Assume that sections with the same segment name are consecutive. */
+ for (i = 0; i < mdata->nsects; i++)
+ {
+ bfd_mach_o_section *this_sect = mdata->sections[i];
+
+ if (prev_sect == NULL
+ || strcmp (prev_sect->segname, this_sect->segname) != 0)
+ {
+ nbr_segcmd++;
+ prev_sect = this_sect;
+ }
+ }
+ }
+
+ nbr_commands = nbr_segcmd;
+
+ /* One command for the symbol table (only if there are symbols. */
+ if (bfd_get_symcount (abfd) > 0)
+ symtab_idx = nbr_commands++;
+
+ /* 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))
+ {
+ /* If there should be a case where a dysymtab could be emitted without
+ a symtab (seems improbable), this would need amending. */
+ dysymtab_idx = nbr_commands++;
+ }
+
+ /* Add an entry point command. */
+ if (mdata->header.filetype == BFD_MACH_O_MH_EXECUTE
+ && bfd_get_start_address (abfd) != 0)
+ main_idx = nbr_commands++;
+
+ /* Well, we must have a header, at least. */
+ mdata->filelen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
+
+ /* A bit unusual, but no content is valid;
+ as -n empty.s -o empty.o */
+ if (nbr_commands == 0)
+ {
+ /* Layout commands (well none...) and set headers command fields. */
+ return bfd_mach_o_layout_commands (mdata);
+ }
+
+ /* Create commands for segments (and symtabs), prepend them. */
+ commands = bfd_zalloc (abfd, nbr_commands * sizeof (bfd_mach_o_load_command));
+ if (commands == NULL)
+ return FALSE;
+ for (i = 0; i < nbr_commands - 1; i++)
+ commands[i].next = &commands[i + 1];
+ commands[nbr_commands - 1].next = mdata->first_command;
+ if (mdata->first_command == NULL)
+ mdata->last_command = &commands[nbr_commands - 1];
+ mdata->first_command = &commands[0];
+
+ if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT && nbr_segcmd != 0)
+ {
+ /* For object file, there is only one segment. */
+ bfd_mach_o_init_segment (mdata, &commands[0], "", mdata->nsects);
+ }
+ else if (nbr_segcmd != 0)
+ {
+ bfd_mach_o_load_command *cmd;
+
+ BFD_ASSERT (nbr_segcmd >= 2);
+
+ /* The pagezero. */
+ cmd = &commands[0];
+ bfd_mach_o_init_segment (mdata, cmd, "__PAGEZERO", 0);
+
+ /* Segments from sections. */
+ cmd++;
+ for (i = 0; i < mdata->nsects;)
+ {
+ const char *segname = mdata->sections[i]->segname;
+ unsigned int nbr_sect = 1;
+
+ /* Count number of sections for this segment. */
+ for (i++; i < mdata->nsects; i++)
+ if (strcmp (mdata->sections[i]->segname, segname) == 0)
+ nbr_sect++;
+ else
+ break;
+
+ bfd_mach_o_init_segment (mdata, cmd, segname, nbr_sect);
+ cmd++;
+ }
+
+ /* The linkedit. */
+ bfd_mach_o_init_segment (mdata, cmd, "__LINKEDIT", 0);
+ }
+
+ if (symtab_idx >= 0)
+ {
+ /* Init symtab command. */
+ bfd_mach_o_load_command *cmd = &commands[symtab_idx];