+ else if (num == 0x00ffffff || num == 0)
+ {
+ /* The 'symnum' in a non-scattered PAIR is 0x00ffffff. But as this
+ is generic code, we don't know wether this is really a PAIR.
+ This value is almost certainly not a valid section number, hence
+ this specific case to avoid an assertion failure.
+ Target specific swap_reloc_in routine should adjust that. */
+ sym = bfd_abs_section_ptr->symbol_ptr_ptr;
+ }
+ else
+ {
+ /* PR 17512: file: 006-2964-0.004. */
+ if (num > mdata->nsects)
+ {
+ _bfd_error_handler (_("\
+malformed mach-o reloc: section index is greater than the number of sections"));
+ return FALSE;
+ }
+
+ /* A section number. */
+ sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
+ /* For a symbol defined in section S, the addend (stored in the
+ binary) contains the address of the section. To comply with
+ bfd convention, subtract the section address.
+ Use the address from the header, so that the user can modify
+ the vma of the section. */
+ res->addend = -mdata->sections[num - 1]->addr;
+ }
+
+ /* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset
+ in the lower 16bits of the address value. So we have to find the
+ 'symbol' from the preceding reloc. We do this even though the
+ section symbol is probably not needed here, because NULL symbol
+ values cause an assert in generic BFD code. This must be done in
+ the PPC swap_reloc_in routine. */
+ res->sym_ptr_ptr = sym;
+
+ return TRUE;
+}
+
+/* Do most of the work for canonicalize_relocs on RAW: create internal
+ representation RELOC and set most fields of RES using symbol table SYMS.
+ Each target still has to set the howto of RES and possibly adjust other
+ fields.
+ Previously the Mach-O hook point was simply swap_in, but some targets
+ (like arm64) don't follow the generic rules (symnum is a value for the
+ non-scattered relocation ADDEND). */
+
+bfd_boolean
+bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd,
+ struct mach_o_reloc_info_external *raw,
+ bfd_mach_o_reloc_info *reloc,
+ arelent *res, asymbol **syms)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ bfd_vma addr;
+
+ addr = bfd_get_32 (abfd, raw->r_address);
+ res->sym_ptr_ptr = NULL;
+ res->addend = 0;
+
+ if (addr & BFD_MACH_O_SR_SCATTERED)
+ {
+ unsigned int j;
+ bfd_vma symnum = bfd_get_32 (abfd, raw->r_symbolnum);
+
+ /* Scattered relocation, can't be extern. */
+ reloc->r_scattered = 1;
+ reloc->r_extern = 0;
+
+ /* Extract section and offset from r_value (symnum). */
+ reloc->r_value = symnum;
+ /* FIXME: This breaks when a symbol in a reloc exactly follows the
+ end of the data for the section (e.g. in a calculation of section
+ data length). At present, the symbol will end up associated with
+ the following section or, if it falls within alignment padding, as
+ null - which will assert later. */
+ for (j = 0; j < mdata->nsects; j++)
+ {
+ bfd_mach_o_section *sect = mdata->sections[j];
+ if (symnum >= sect->addr && symnum < sect->addr + sect->size)
+ {
+ res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
+ res->addend = symnum - sect->addr;
+ break;
+ }
+ }
+
+ /* Extract the info and address fields from r_address. */
+ reloc->r_type = BFD_MACH_O_GET_SR_TYPE (addr);
+ reloc->r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
+ reloc->r_pcrel = addr & BFD_MACH_O_SR_PCREL;
+ reloc->r_address = BFD_MACH_O_GET_SR_TYPE (addr);
+ res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
+ }
+ else
+ {
+ /* Non-scattered relocation. */
+ reloc->r_scattered = 0;
+ reloc->r_address = addr;
+ res->address = addr;
+
+ /* The value and info fields have to be extracted dependent on target
+ endian-ness. */
+ bfd_mach_o_swap_in_non_scattered_reloc (abfd, reloc, raw->r_symbolnum);
+
+ if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, reloc,
+ res, syms))
+ return FALSE;
+ }
+
+ /* We have set up a reloc with all the information present, so the swapper
+ can modify address, value and addend fields, if necessary, to convey
+ information in the generic BFD reloc that is mach-o specific. */
+
+ return TRUE;
+}
+
+static int
+bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
+ unsigned long count,
+ arelent *res, asymbol **syms)
+{
+ bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+ unsigned long i;
+ struct mach_o_reloc_info_external *native_relocs = NULL;
+ size_t native_size;
+
+ /* Allocate and read relocs. */
+ if (_bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &native_size))
+ /* PR 17512: file: 09477b57. */
+ goto err;
+
+ if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
+ return -1;
+ 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: