+ native_relocs = (struct mach_o_reloc_info_external *)
+ _bfd_malloc_and_read (abfd, native_size, native_size);
+ if (native_relocs == NULL)
+ return -1;
+
+ for (i = 0; i < count; i++)
+ {
+ if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i],
+ &res[i], syms, res))
+ goto err;
+ }
+ free (native_relocs);
+ return i;
+
+ err:
+ free (native_relocs);
+ if (bfd_get_error () == bfd_error_no_error)
+ bfd_set_error (bfd_error_invalid_operation);
+ return -1;
+}
+
+long
+bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
+ arelent **rels, asymbol **syms)
+{
+ bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+ unsigned long i;
+ arelent *res;
+
+ if (asect->reloc_count == 0)
+ return 0;
+
+ /* No need to go further if we don't know how to read relocs. */
+ if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
+ return 0;
+
+ if (asect->relocation == NULL)
+ {
+ size_t amt;
+
+ if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
+ return -1;
+ res = bfd_malloc (amt);
+ if (res == NULL)
+ return -1;
+
+ if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
+ asect->reloc_count, res, syms) < 0)
+ {
+ free (res);
+ return -1;
+ }
+ asect->relocation = res;
+ }
+
+ res = asect->relocation;
+ for (i = 0; i < asect->reloc_count; i++)
+ rels[i] = &res[i];
+ rels[i] = NULL;
+
+ return i;
+}
+
+long
+bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+
+ if (mdata->dysymtab == NULL)
+ return 1;
+ return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel + 1)
+ * sizeof (arelent *);
+}
+
+long
+bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
+ struct bfd_symbol **syms)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
+ bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+ unsigned long i;
+ arelent *res;
+
+ if (dysymtab == NULL)
+ return 0;
+ if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
+ return 0;
+
+ /* No need to go further if we don't know how to read relocs. */
+ if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
+ return 0;
+
+ if (mdata->dyn_reloc_cache == NULL)
+ {
+ ufile_ptr filesize = bfd_get_file_size (abfd);
+ size_t amt;
+
+ if (filesize != 0)
+ {
+ if (dysymtab->extreloff > filesize
+ || dysymtab->nextrel > ((filesize - dysymtab->extreloff)
+ / BFD_MACH_O_RELENT_SIZE)
+ || dysymtab->locreloff > filesize
+ || dysymtab->nlocrel > ((filesize - dysymtab->locreloff)
+ / BFD_MACH_O_RELENT_SIZE))
+ {
+ bfd_set_error (bfd_error_file_truncated);
+ return -1;
+ }
+ }
+ if (_bfd_mul_overflow (dysymtab->nextrel + dysymtab->nlocrel,
+ sizeof (arelent), &amt))
+ {
+ bfd_set_error (bfd_error_file_too_big);
+ return -1;
+ }
+
+ res = bfd_malloc (amt);
+ if (res == NULL)
+ return -1;
+
+ if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
+ dysymtab->nextrel, res, syms) < 0)
+ {
+ free (res);
+ return -1;
+ }
+
+ if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
+ dysymtab->nlocrel,
+ res + dysymtab->nextrel, syms) < 0)
+ {
+ free (res);
+ return -1;
+ }
+
+ mdata->dyn_reloc_cache = res;
+ }
+
+ res = mdata->dyn_reloc_cache;
+ for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
+ rels[i] = &res[i];
+ rels[i] = NULL;
+ return i;
+}
+
+/* In addition to the need to byte-swap the symbol number, the bit positions
+ of the fields in the relocation information vary per target endian-ness. */
+
+static void
+bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
+ bfd_mach_o_reloc_info *rel)
+{
+ unsigned char info = 0;
+
+ BFD_ASSERT (rel->r_type <= 15);
+ BFD_ASSERT (rel->r_length <= 3);
+
+ if (bfd_big_endian (abfd))
+ {
+ fields[0] = (rel->r_value >> 16) & 0xff;
+ fields[1] = (rel->r_value >> 8) & 0xff;
+ fields[2] = rel->r_value & 0xff;
+ info |= rel->r_type << BFD_MACH_O_BE_TYPE_SHIFT;
+ info |= rel->r_pcrel ? BFD_MACH_O_BE_PCREL : 0;
+ info |= rel->r_length << BFD_MACH_O_BE_LENGTH_SHIFT;
+ info |= rel->r_extern ? BFD_MACH_O_BE_EXTERN : 0;
+ }
+ else
+ {
+ fields[2] = (rel->r_value >> 16) & 0xff;
+ fields[1] = (rel->r_value >> 8) & 0xff;
+ fields[0] = rel->r_value & 0xff;
+ info |= rel->r_type << BFD_MACH_O_LE_TYPE_SHIFT;
+ info |= rel->r_pcrel ? BFD_MACH_O_LE_PCREL : 0;
+ info |= rel->r_length << BFD_MACH_O_LE_LENGTH_SHIFT;
+ info |= rel->r_extern ? BFD_MACH_O_LE_EXTERN : 0;
+ }
+ fields[3] = info;
+}
+
+static bfd_boolean
+bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
+{
+ unsigned int i;
+ arelent **entries;
+ asection *sec;
+ bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+
+ sec = section->bfdsection;
+ if (sec->reloc_count == 0)
+ return TRUE;
+
+ if (bed->_bfd_mach_o_swap_reloc_out == NULL)
+ return TRUE;
+
+ if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
+ return FALSE;
+
+ /* Convert and write. */
+ entries = section->bfdsection->orelocation;
+ for (i = 0; i < section->nreloc; i++)
+ {
+ arelent *rel = entries[i];
+ struct mach_o_reloc_info_external raw;
+ bfd_mach_o_reloc_info info, *pinfo = &info;
+
+ /* Convert relocation to an intermediate representation. */
+ if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
+ return FALSE;
+
+ /* Lower the relocation info. */
+ if (pinfo->r_scattered)
+ {
+ unsigned long v;
+
+ v = BFD_MACH_O_SR_SCATTERED
+ | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
+ | BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length)
+ | BFD_MACH_O_SET_SR_TYPE (pinfo->r_type)
+ | BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address);
+ /* Note: scattered relocs have field in reverse order... */
+ bfd_put_32 (abfd, v, raw.r_address);
+ bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
+ }
+ else
+ {
+ bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
+ bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum,
+ pinfo);
+ }
+
+ if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
+ != BFD_MACH_O_RELENT_SIZE)
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static bfd_boolean
+bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
+{
+ struct mach_o_section_32_external raw;
+
+ memcpy (raw.sectname, section->sectname, 16);
+ memcpy (raw.segname, section->segname, 16);
+ bfd_h_put_32 (abfd, section->addr, raw.addr);
+ bfd_h_put_32 (abfd, section->size, raw.size);
+ bfd_h_put_32 (abfd, section->offset, raw.offset);
+ bfd_h_put_32 (abfd, section->align, raw.align);
+ bfd_h_put_32 (abfd, section->reloff, raw.reloff);
+ bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
+ bfd_h_put_32 (abfd, section->flags, raw.flags);
+ bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
+ bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
+
+ if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
+ != BFD_MACH_O_SECTION_SIZE)
+ return FALSE;
+
+ return TRUE;
+}
+
+static bfd_boolean
+bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
+{
+ struct mach_o_section_64_external raw;
+
+ memcpy (raw.sectname, section->sectname, 16);
+ memcpy (raw.segname, section->segname, 16);
+ bfd_h_put_64 (abfd, section->addr, raw.addr);
+ bfd_h_put_64 (abfd, section->size, raw.size);
+ bfd_h_put_32 (abfd, section->offset, raw.offset);
+ bfd_h_put_32 (abfd, section->align, raw.align);
+ bfd_h_put_32 (abfd, section->reloff, raw.reloff);
+ bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
+ bfd_h_put_32 (abfd, section->flags, raw.flags);
+ bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
+ bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
+ bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
+
+ if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
+ != BFD_MACH_O_SECTION_64_SIZE)
+ return FALSE;
+
+ return TRUE;
+}
+
+static bfd_boolean
+bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
+{
+ struct mach_o_segment_command_32_external raw;
+ bfd_mach_o_segment_command *seg = &command->command.segment;
+ bfd_mach_o_section *sec;
+
+ BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
+
+ for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+ if (!bfd_mach_o_write_relocs (abfd, sec))
+ return FALSE;
+
+ memcpy (raw.segname, seg->segname, 16);
+ bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
+ bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
+ bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
+ bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
+ bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
+ bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
+ bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
+ bfd_h_put_32 (abfd, seg->flags, raw.flags);
+
+ if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+ || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+
+ for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+ if (!bfd_mach_o_write_section_32 (abfd, sec))
+ return FALSE;
+
+ return TRUE;
+}
+
+static bfd_boolean
+bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
+{
+ struct mach_o_segment_command_64_external raw;
+ bfd_mach_o_segment_command *seg = &command->command.segment;
+ bfd_mach_o_section *sec;
+
+ BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
+
+ for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+ if (!bfd_mach_o_write_relocs (abfd, sec))
+ return FALSE;
+
+ memcpy (raw.segname, seg->segname, 16);
+ bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
+ bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
+ bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
+ bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
+ bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
+ bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
+ bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
+ bfd_h_put_32 (abfd, seg->flags, raw.flags);
+
+ if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+ || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+
+ for (sec = seg->sect_head; sec != NULL; sec = sec->next)
+ if (!bfd_mach_o_write_section_64 (abfd, sec))
+ return FALSE;
+
+ return TRUE;
+}
+
+static bfd_boolean
+bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ unsigned long i;
+ unsigned int wide = bfd_mach_o_wide_p (abfd);
+ struct bfd_strtab_hash *strtab;
+ asymbol **symbols = bfd_get_outsymbols (abfd);
+ int padlen;
+
+ /* Write the symbols first. */
+ if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
+ return FALSE;
+
+ strtab = _bfd_stringtab_init ();
+ if (strtab == NULL)
+ return FALSE;
+
+ if (sym->nsyms > 0)
+ /* Although we don't strictly need to do this, for compatibility with
+ Darwin system tools, actually output an empty string for the index
+ 0 entry. */
+ _bfd_stringtab_add (strtab, "", TRUE, FALSE);
+
+ for (i = 0; i < sym->nsyms; i++)
+ {
+ bfd_size_type str_index;
+ bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
+
+ if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
+ /* An index of 0 always means the empty string. */
+ str_index = 0;
+ else
+ {
+ str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
+
+ if (str_index == (bfd_size_type) -1)
+ goto err;
+ }
+
+ if (wide)
+ {
+ struct mach_o_nlist_64_external raw;
+
+ bfd_h_put_32 (abfd, str_index, raw.n_strx);
+ bfd_h_put_8 (abfd, s->n_type, raw.n_type);
+ bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
+ bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
+ bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
+ raw.n_value);
+
+ if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ goto err;
+ }
+ else
+ {
+ struct mach_o_nlist_external raw;
+
+ bfd_h_put_32 (abfd, str_index, raw.n_strx);
+ bfd_h_put_8 (abfd, s->n_type, raw.n_type);
+ bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
+ bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
+ bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
+ raw.n_value);
+
+ if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ goto err;
+ }
+ }
+ sym->strsize = _bfd_stringtab_size (strtab);
+ sym->stroff = mdata->filelen;
+ mdata->filelen += sym->strsize;
+
+ if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
+ goto err;
+
+ if (!_bfd_stringtab_emit (abfd, strtab))
+ goto err;
+
+ /* Pad string table. */
+ padlen = bfd_mach_o_pad4 (abfd, sym->strsize);
+ if (padlen < 0)
+ return FALSE;
+ mdata->filelen += padlen;
+ sym->strsize += padlen;
+
+ return TRUE;
+
+ err:
+ _bfd_stringtab_free (strtab);
+ sym->strsize = 0;
+ return FALSE;
+}
+
+static bfd_boolean
+bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
+{
+ bfd_mach_o_symtab_command *sym = &command->command.symtab;
+ struct mach_o_symtab_command_external raw;
+
+ BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
+
+ /* The command. */
+ bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
+ bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
+ bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
+ bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
+
+ if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+ || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+
+ 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;
+}
+
+/* Create the dysymtab. */
+
+static bfd_boolean
+bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+
+ /* TODO:
+ We are not going to try and fill these in yet and, moreover, we are
+ going to bail if they are already set. */
+ if (cmd->nmodtab != 0
+ || cmd->ntoc != 0
+ || cmd->nextrefsyms != 0)
+ {
+ _bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet"
+ " implemented for dysymtab commands."));
+ return FALSE;
+ }
+
+ cmd->ilocalsym = 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;
+ }
+ cmd->nlocalsym = i;
+ cmd->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;
+ }
+ cmd->nextdefsym = i - cmd->nlocalsym;
+ cmd->iundefsym = cmd->nextdefsym + cmd->iextdefsym;
+ cmd->nundefsym = bfd_get_symcount (abfd)
+ - cmd->nlocalsym
+ - cmd->nextdefsym;
+ }
+ else
+ {
+ cmd->nlocalsym = 0;
+ cmd->iextdefsym = 0;
+ cmd->nextdefsym = 0;
+ cmd->iundefsym = 0;
+ cmd->nundefsym = 0;
+ }
+
+ cmd->nindirectsyms = bfd_mach_o_count_indirect_symbols (abfd, mdata);
+ if (cmd->nindirectsyms > 0)
+ {
+ unsigned i;
+ unsigned n;
+ size_t amt;
+
+ mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
+ cmd->indirectsymoff = mdata->filelen;
+ if (_bfd_mul_overflow (cmd->nindirectsyms, 4, &amt))
+ return FALSE;
+ mdata->filelen += amt;
+
+ cmd->indirect_syms = bfd_zalloc (abfd, amt);
+ if (cmd->indirect_syms == NULL)
+ return FALSE;
+
+ n = 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:
+ {
+ 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)
+ cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
+ else if (isyms[j]->symbol.section == bfd_abs_section_ptr
+ && ! (isyms[j]->n_type & BFD_MACH_O_N_EXT))
+ cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL
+ | BFD_MACH_O_INDIRECT_SYM_ABS;
+ else
+ cmd->indirect_syms[n] = isyms[j]->symbol.udata.i;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ return TRUE;
+}
+
+/* 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)
+*/
+
+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];
+
+ cmd->type = BFD_MACH_O_LC_SYMTAB;
+ cmd->type_required = FALSE;
+ }
+
+ /* If required, setup symtab command, see comment above about the quality
+ of this test. */
+ if (dysymtab_idx >= 0)
+ {
+ bfd_mach_o_load_command *cmd = &commands[dysymtab_idx];
+
+ cmd->type = BFD_MACH_O_LC_DYSYMTAB;
+ cmd->type_required = FALSE;
+ }
+
+ /* Create the main command. */
+ if (main_idx >= 0)
+ {
+ bfd_mach_o_load_command *cmd = &commands[main_idx];
+
+ cmd->type = BFD_MACH_O_LC_MAIN;
+ cmd->type_required = TRUE;
+
+ cmd->command.main.entryoff = 0;
+ cmd->command.main.stacksize = 0;
+ }
+
+ /* Layout commands. */
+ if (! bfd_mach_o_layout_commands (mdata))
+ return FALSE;
+
+ /* 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->header.filetype == BFD_MACH_O_MH_OBJECT)
+ {
+ for (i = 0; i < nbr_segcmd; i++)
+ if (!bfd_mach_o_build_obj_seg_command
+ (abfd, &commands[i].command.segment))
+ return FALSE;
+ }
+ else
+ {
+ bfd_vma maxvma = 0;
+
+ /* Skip pagezero and linkedit segments. */
+ for (i = 1; i < nbr_segcmd - 1; i++)
+ {
+ bfd_mach_o_segment_command *seg = &commands[i].command.segment;
+
+ if (!bfd_mach_o_build_exec_seg_command (abfd, seg))
+ return FALSE;
+
+ if (seg->vmaddr + seg->vmsize > maxvma)
+ maxvma = seg->vmaddr + seg->vmsize;
+ }
+
+ /* Set the size of __PAGEZERO. */
+ commands[0].command.segment.vmsize =
+ commands[1].command.segment.vmaddr;
+
+ /* Set the vma and fileoff of __LINKEDIT. */
+ commands[nbr_segcmd - 1].command.segment.vmaddr = maxvma;
+ commands[nbr_segcmd - 1].command.segment.fileoff = mdata->filelen;
+
+ /* Set entry point (once segments have been laid out). */
+ if (main_idx >= 0)
+ commands[main_idx].command.main.entryoff =
+ bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr;
+ }
+
+ return TRUE;
+}
+
+/* Set the contents of a section. */
+
+bfd_boolean
+bfd_mach_o_set_section_contents (bfd *abfd,
+ asection *section,
+ const void * location,
+ file_ptr offset,
+ bfd_size_type count)
+{
+ file_ptr pos;
+
+ /* Trying to write the first section contents will trigger the creation of
+ the load commands if they are not already present. */
+ if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
+ return FALSE;
+
+ if (count == 0)
+ return TRUE;
+
+ pos = section->filepos + offset;
+ if (bfd_seek (abfd, pos, SEEK_SET) != 0
+ || bfd_bwrite (location, count, abfd) != count)
+ return FALSE;
+
+ return TRUE;
+}
+
+int
+bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
+ struct bfd_link_info *info ATTRIBUTE_UNUSED)
+{
+ return 0;
+}
+
+/* Make an empty symbol. This is required only because
+ bfd_make_section_anyway wants to create a symbol for the section. */
+
+asymbol *
+bfd_mach_o_make_empty_symbol (bfd *abfd)
+{
+ asymbol *new_symbol;
+
+ new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
+ if (new_symbol == NULL)
+ return new_symbol;
+ new_symbol->the_bfd = abfd;
+ new_symbol->udata.i = SYM_MACHO_FIELDS_UNSET;
+ return new_symbol;
+}
+
+static bfd_boolean
+bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header)
+{
+ struct mach_o_header_external raw;
+ unsigned int size;
+ bfd_vma (*get32) (const void *) = NULL;
+
+ /* Just read the magic number. */
+ if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0
+ || bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4)
+ return FALSE;
+
+ if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
+ {
+ header->byteorder = BFD_ENDIAN_BIG;
+ header->magic = BFD_MACH_O_MH_MAGIC;
+ header->version = 1;
+ get32 = bfd_getb32;
+ }
+ else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
+ {
+ header->byteorder = BFD_ENDIAN_LITTLE;
+ header->magic = BFD_MACH_O_MH_MAGIC;
+ header->version = 1;
+ get32 = bfd_getl32;
+ }
+ else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
+ {
+ header->byteorder = BFD_ENDIAN_BIG;
+ header->magic = BFD_MACH_O_MH_MAGIC_64;
+ header->version = 2;
+ get32 = bfd_getb32;
+ }
+ else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
+ {
+ header->byteorder = BFD_ENDIAN_LITTLE;
+ header->magic = BFD_MACH_O_MH_MAGIC_64;
+ header->version = 2;
+ get32 = bfd_getl32;
+ }
+ else
+ {
+ header->byteorder = BFD_ENDIAN_UNKNOWN;
+ return FALSE;
+ }
+
+ /* Once the size of the header is known, read the full header. */
+ size = mach_o_wide_p (header) ?
+ BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
+
+ if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0
+ || bfd_bread (&raw, size, abfd) != size)
+ return FALSE;
+
+ header->cputype = (*get32) (raw.cputype);
+ header->cpusubtype = (*get32) (raw.cpusubtype);
+ header->filetype = (*get32) (raw.filetype);
+ header->ncmds = (*get32) (raw.ncmds);
+ header->sizeofcmds = (*get32) (raw.sizeofcmds);
+ header->flags = (*get32) (raw.flags);
+
+ if (mach_o_wide_p (header))
+ header->reserved = (*get32) (raw.reserved);
+ else
+ header->reserved = 0;
+
+ return TRUE;
+}
+
+bfd_boolean
+bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
+{
+ bfd_mach_o_section *s;
+ unsigned bfdalign = bfd_section_alignment (sec);
+
+ s = bfd_mach_o_get_mach_o_section (sec);
+ if (s == NULL)
+ {
+ flagword bfd_flags;
+ static const mach_o_section_name_xlat * xlat;
+
+ s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s));
+ if (s == NULL)
+ return FALSE;
+ sec->used_by_bfd = s;
+ s->bfdsection = sec;
+
+ /* Create the Darwin seg/sect name pair from the bfd name.
+ If this is a canonical name for which a specific paiting exists
+ there will also be defined flags, type, attribute and alignment
+ values. */
+ xlat = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
+ if (xlat != NULL)
+ {
+ s->flags = xlat->macho_sectype | xlat->macho_secattr;
+ s->align = xlat->sectalign > bfdalign ? xlat->sectalign
+ : bfdalign;
+ bfd_set_section_alignment (sec, s->align);
+ bfd_flags = bfd_section_flags (sec);
+ if (bfd_flags == SEC_NO_FLAGS)
+ bfd_set_section_flags (sec, xlat->bfd_flags);
+ }
+ else
+ /* Create default flags. */
+ bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
+ }
+
+ return _bfd_generic_new_section_hook (abfd, sec);
+}
+
+static void
+bfd_mach_o_init_section_from_mach_o (asection *sec, unsigned long prot)
+{
+ flagword flags;
+ bfd_mach_o_section *section;
+
+ flags = bfd_section_flags (sec);
+ section = bfd_mach_o_get_mach_o_section (sec);
+
+ /* TODO: see if we should use the xlat system for doing this by
+ preference and fall back to this for unknown sections. */
+
+ if (flags == SEC_NO_FLAGS)
+ {
+ /* Try to guess flags. */
+ if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
+ flags = SEC_DEBUGGING;
+ else
+ {
+ flags = SEC_ALLOC;
+ if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
+ != BFD_MACH_O_S_ZEROFILL)
+ {
+ flags |= SEC_LOAD;
+ if (prot & BFD_MACH_O_PROT_EXECUTE)
+ flags |= SEC_CODE;
+ if (prot & BFD_MACH_O_PROT_WRITE)
+ flags |= SEC_DATA;
+ else if (prot & BFD_MACH_O_PROT_READ)
+ flags |= SEC_READONLY;
+ }
+ }
+ }
+ else
+ {
+ if ((flags & SEC_DEBUGGING) == 0)
+ flags |= SEC_ALLOC;
+ }
+
+ if (section->offset != 0)
+ flags |= SEC_HAS_CONTENTS;
+ if (section->nreloc != 0)
+ flags |= SEC_RELOC;
+
+ bfd_set_section_flags (sec, flags);
+
+ sec->vma = section->addr;
+ sec->lma = section->addr;
+ sec->size = section->size;
+ sec->filepos = section->offset;
+ sec->alignment_power = section->align;
+ sec->segment_mark = 0;
+ sec->reloc_count = section->nreloc;
+ sec->rel_filepos = section->reloff;
+}
+
+static asection *
+bfd_mach_o_make_bfd_section (bfd *abfd,
+ const unsigned char *segname,
+ const unsigned char *sectname)
+{
+ const char *sname;
+ flagword flags;
+
+ bfd_mach_o_convert_section_name_to_bfd
+ (abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
+ if (sname == NULL)
+ return NULL;
+
+ return bfd_make_section_anyway_with_flags (abfd, sname, flags);
+}
+
+static asection *
+bfd_mach_o_read_section_32 (bfd *abfd, unsigned long prot)
+{
+ struct mach_o_section_32_external raw;
+ asection *sec;
+ bfd_mach_o_section *section;
+
+ if (bfd_bread (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
+ != BFD_MACH_O_SECTION_SIZE)
+ return NULL;
+
+ sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
+ if (sec == NULL)
+ return NULL;
+
+ section = bfd_mach_o_get_mach_o_section (sec);
+ memcpy (section->segname, raw.segname, sizeof (raw.segname));
+ section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
+ memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
+ section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
+ section->addr = bfd_h_get_32 (abfd, raw.addr);
+ section->size = bfd_h_get_32 (abfd, raw.size);
+ section->offset = bfd_h_get_32 (abfd, raw.offset);
+ section->align = bfd_h_get_32 (abfd, raw.align);
+ /* PR 17512: file: 0017eb76. */
+ if (section->align > 64)
+ {
+ _bfd_error_handler
+ (_("bfd_mach_o_read_section_32: overlarge alignment value: %#lx, "
+ "using 32 instead"), section->align);
+ section->align = 32;
+ }
+ section->reloff = bfd_h_get_32 (abfd, raw.reloff);
+ section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
+ section->flags = bfd_h_get_32 (abfd, raw.flags);
+ section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
+ section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
+ section->reserved3 = 0;
+
+ bfd_mach_o_init_section_from_mach_o (sec, prot);
+
+ return sec;
+}
+
+static asection *
+bfd_mach_o_read_section_64 (bfd *abfd, unsigned long prot)
+{
+ struct mach_o_section_64_external raw;
+ asection *sec;
+ bfd_mach_o_section *section;
+
+ if (bfd_bread (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
+ != BFD_MACH_O_SECTION_64_SIZE)
+ return NULL;
+
+ sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
+ if (sec == NULL)
+ return NULL;
+
+ section = bfd_mach_o_get_mach_o_section (sec);
+ memcpy (section->segname, raw.segname, sizeof (raw.segname));
+ section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
+ memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
+ section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
+ section->addr = bfd_h_get_64 (abfd, raw.addr);
+ section->size = bfd_h_get_64 (abfd, raw.size);
+ section->offset = bfd_h_get_32 (abfd, raw.offset);
+ section->align = bfd_h_get_32 (abfd, raw.align);
+ if (section->align > 64)
+ {
+ _bfd_error_handler
+ (_("bfd_mach_o_read_section_64: overlarge alignment value: %#lx, "
+ "using 32 instead"), section->align);
+ section->align = 32;
+ }
+ section->reloff = bfd_h_get_32 (abfd, raw.reloff);
+ section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
+ section->flags = bfd_h_get_32 (abfd, raw.flags);
+ section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
+ section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
+ section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
+
+ bfd_mach_o_init_section_from_mach_o (sec, prot);
+
+ return sec;
+}
+
+static asection *
+bfd_mach_o_read_section (bfd *abfd, unsigned long prot, unsigned int wide)
+{
+ if (wide)
+ return bfd_mach_o_read_section_64 (abfd, prot);
+ else
+ return bfd_mach_o_read_section_32 (abfd, prot);
+}
+
+static bfd_boolean
+bfd_mach_o_read_symtab_symbol (bfd *abfd,
+ bfd_mach_o_symtab_command *sym,
+ bfd_mach_o_asymbol *s,
+ unsigned long i)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ unsigned int wide = mach_o_wide_p (&mdata->header);
+ unsigned int symwidth =
+ wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
+ unsigned int symoff = sym->symoff + (i * symwidth);
+ struct mach_o_nlist_64_external raw;
+ unsigned char type = -1;
+ unsigned char section = -1;
+ short desc = -1;
+ symvalue value = -1;
+ unsigned long stroff = -1;
+ unsigned int symtype = -1;
+
+ BFD_ASSERT (sym->strtab != NULL);
+
+ if (bfd_seek (abfd, symoff, SEEK_SET) != 0
+ || bfd_bread (&raw, symwidth, abfd) != symwidth)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"),
+ symwidth, symoff);
+ return FALSE;
+ }
+
+ stroff = bfd_h_get_32 (abfd, raw.n_strx);
+ type = bfd_h_get_8 (abfd, raw.n_type);
+ symtype = type & BFD_MACH_O_N_TYPE;
+ section = bfd_h_get_8 (abfd, raw.n_sect);
+ desc = bfd_h_get_16 (abfd, raw.n_desc);
+ if (wide)
+ value = bfd_h_get_64 (abfd, raw.n_value);
+ else
+ value = bfd_h_get_32 (abfd, raw.n_value);
+
+ if (stroff >= sym->strsize)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"),
+ stroff,
+ sym->strsize);
+ return FALSE;
+ }
+
+ s->symbol.the_bfd = abfd;
+ s->symbol.name = sym->strtab + stroff;
+ s->symbol.value = value;
+ s->symbol.flags = 0x0;
+ s->symbol.udata.i = i;
+ s->n_type = type;
+ s->n_sect = section;
+ s->n_desc = desc;
+
+ if (type & BFD_MACH_O_N_STAB)
+ {
+ s->symbol.flags |= BSF_DEBUGGING;
+ s->symbol.section = bfd_und_section_ptr;
+ switch (type)
+ {
+ case N_FUN:
+ case N_STSYM:
+ case N_LCSYM:
+ case N_BNSYM:
+ case N_SLINE:
+ case N_ENSYM:
+ case N_ECOMM:
+ case N_ECOML:
+ case N_GSYM:
+ if ((section > 0) && (section <= mdata->nsects))
+ {
+ s->symbol.section = mdata->sections[section - 1]->bfdsection;
+ s->symbol.value =
+ s->symbol.value - mdata->sections[section - 1]->addr;
+ }
+ break;
+ }
+ }
+ else
+ {
+ if (type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
+ s->symbol.flags |= BSF_GLOBAL;
+ else
+ s->symbol.flags |= BSF_LOCAL;
+
+ switch (symtype)
+ {
+ case BFD_MACH_O_N_UNDF:
+ if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
+ && s->symbol.value != 0)
+ {
+ /* A common symbol. */
+ s->symbol.section = bfd_com_section_ptr;
+ s->symbol.flags = BSF_NO_FLAGS;
+ }
+ else
+ {
+ s->symbol.section = bfd_und_section_ptr;
+ if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
+ s->symbol.flags |= BSF_WEAK;
+ }
+ break;
+ case BFD_MACH_O_N_PBUD:
+ s->symbol.section = bfd_und_section_ptr;
+ break;
+ case BFD_MACH_O_N_ABS:
+ s->symbol.section = bfd_abs_section_ptr;
+ break;
+ case BFD_MACH_O_N_SECT:
+ if ((section > 0) && (section <= mdata->nsects))
+ {
+ s->symbol.section = mdata->sections[section - 1]->bfdsection;
+ s->symbol.value =
+ s->symbol.value - mdata->sections[section - 1]->addr;
+ }
+ else
+ {
+ /* Mach-O uses 0 to mean "no section"; not an error. */
+ if (section != 0)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("bfd_mach_o_read_symtab_symbol: "
+ "symbol \"%s\" specified invalid section %d (max %lu): "
+ "setting to undefined"),
+ s->symbol.name, section, mdata->nsects);
+ }
+ s->symbol.section = bfd_und_section_ptr;
+ }
+ break;
+ case BFD_MACH_O_N_INDR:
+ /* FIXME: we don't follow the BFD convention as this indirect symbol
+ won't be followed by the referenced one. This looks harmless
+ unless we start using the linker. */
+ s->symbol.flags |= BSF_INDIRECT;
+ s->symbol.section = bfd_ind_section_ptr;
+ s->symbol.value = 0;
+ break;
+ default:
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("bfd_mach_o_read_symtab_symbol: "
+ "symbol \"%s\" specified invalid type field 0x%x: "
+ "setting to undefined"), s->symbol.name, symtype);
+ s->symbol.section = bfd_und_section_ptr;
+ break;
+ }
+ }
+
+ return TRUE;
+}
+
+bfd_boolean
+bfd_mach_o_read_symtab_strtab (bfd *abfd)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ bfd_mach_o_symtab_command *sym = mdata->symtab;
+
+ /* Fail if there is no symtab. */
+ if (sym == NULL)
+ return FALSE;
+
+ /* Success if already loaded. */
+ if (sym->strtab)
+ return TRUE;
+
+ if (abfd->flags & BFD_IN_MEMORY)
+ {
+ struct bfd_in_memory *b;
+
+ b = (struct bfd_in_memory *) abfd->iostream;
+
+ if ((sym->stroff + sym->strsize) > b->size)
+ {
+ bfd_set_error (bfd_error_file_truncated);
+ return FALSE;
+ }
+ sym->strtab = (char *) b->buffer + sym->stroff;
+ }
+ else
+ {
+ /* See PR 21840 for a reproducer. */
+ if ((sym->strsize + 1) == 0)
+ return FALSE;
+ if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
+ return FALSE;
+ sym->strtab = (char *) _bfd_alloc_and_read (abfd, sym->strsize + 1,
+ sym->strsize);
+ if (sym->strtab == NULL)
+ return FALSE;
+
+ /* Zero terminate the string table. */
+ sym->strtab[sym->strsize] = 0;
+ }
+
+ return TRUE;
+}
+
+bfd_boolean
+bfd_mach_o_read_symtab_symbols (bfd *abfd)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ bfd_mach_o_symtab_command *sym = mdata->symtab;
+ unsigned long i;
+ size_t amt;
+ ufile_ptr filesize;
+
+ if (sym == NULL || sym->nsyms == 0 || sym->symbols)
+ /* Return now if there are no symbols or if already loaded. */
+ return TRUE;
+
+ filesize = bfd_get_file_size (abfd);
+ if (filesize != 0)
+ {
+ unsigned int wide = mach_o_wide_p (&mdata->header);
+ unsigned int symwidth
+ = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
+
+ if (sym->symoff > filesize
+ || sym->nsyms > (filesize - sym->symoff) / symwidth)
+ {
+ bfd_set_error (bfd_error_file_truncated);
+ sym->nsyms = 0;
+ return FALSE;
+ }
+ }
+ if (_bfd_mul_overflow (sym->nsyms, sizeof (bfd_mach_o_asymbol), &amt)
+ || (sym->symbols = bfd_alloc (abfd, amt)) == NULL)
+ {
+ bfd_set_error (bfd_error_no_memory);
+ sym->nsyms = 0;
+ return FALSE;
+ }
+
+ if (!bfd_mach_o_read_symtab_strtab (abfd))
+ goto fail;
+
+ for (i = 0; i < sym->nsyms; i++)
+ if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
+ goto fail;
+
+ return TRUE;
+
+ fail:
+ bfd_release (abfd, sym->symbols);
+ sym->symbols = NULL;
+ sym->nsyms = 0;
+ return FALSE;
+}
+
+static const char *
+bfd_mach_o_i386_flavour_string (unsigned int flavour)
+{
+ switch ((int) flavour)
+ {
+ case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
+ case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
+ case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
+ case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
+ case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
+ case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
+ case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
+ case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
+ case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
+ case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
+ case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
+ case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
+ case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
+ default: return "UNKNOWN";
+ }
+}
+
+static const char *
+bfd_mach_o_ppc_flavour_string (unsigned int flavour)
+{
+ switch ((int) flavour)
+ {
+ case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
+ case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
+ case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
+ case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
+ case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
+ case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
+ default: return "UNKNOWN";
+ }
+}
+
+static unsigned char *
+bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos, size_t size)
+{
+ if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
+ return NULL;
+ return _bfd_alloc_and_read (abfd, size, size);
+}
+
+static bfd_boolean
+bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
+{
+ bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
+ struct mach_o_str_command_external raw;
+ unsigned int nameoff;
+ unsigned int namelen;