+ bfd_mach_o_main_command *cmd = &command->command.main;
+ struct mach_o_entry_point_command_external raw;
+
+ bfd_h_put_64 (abfd, cmd->entryoff, raw.entryoff);
+ bfd_h_put_64 (abfd, cmd->stacksize, raw.stacksize);
+
+ 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;
+}
+
+static bfd_boolean
+bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
+{
+ bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
+ struct mach_o_dyld_info_command_external raw;
+
+ bfd_h_put_32 (abfd, cmd->rebase_off, raw.rebase_off);
+ bfd_h_put_32 (abfd, cmd->rebase_size, raw.rebase_size);
+ bfd_h_put_32 (abfd, cmd->bind_off, raw.bind_off);
+ bfd_h_put_32 (abfd, cmd->bind_size, raw.bind_size);
+ bfd_h_put_32 (abfd, cmd->weak_bind_off, raw.weak_bind_off);
+ bfd_h_put_32 (abfd, cmd->weak_bind_size, raw.weak_bind_size);
+ bfd_h_put_32 (abfd, cmd->lazy_bind_off, raw.lazy_bind_off);
+ bfd_h_put_32 (abfd, cmd->lazy_bind_size, raw.lazy_bind_size);
+ bfd_h_put_32 (abfd, cmd->export_off, raw.export_off);
+ bfd_h_put_32 (abfd, cmd->export_size, raw.export_size);
+
+ if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
+ || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
+ return FALSE;
+
+ if (cmd->rebase_size != 0)
+ if (bfd_seek (abfd, cmd->rebase_off, SEEK_SET) != 0
+ || (bfd_bwrite (cmd->rebase_content, cmd->rebase_size, abfd) !=
+ cmd->rebase_size))
+ return FALSE;
+
+ if (cmd->bind_size != 0)
+ if (bfd_seek (abfd, cmd->bind_off, SEEK_SET) != 0
+ || (bfd_bwrite (cmd->bind_content, cmd->bind_size, abfd) !=
+ cmd->bind_size))
+ return FALSE;
+
+ if (cmd->weak_bind_size != 0)
+ if (bfd_seek (abfd, cmd->weak_bind_off, SEEK_SET) != 0
+ || (bfd_bwrite (cmd->weak_bind_content, cmd->weak_bind_size, abfd) !=
+ cmd->weak_bind_size))
+ return FALSE;
+
+ if (cmd->lazy_bind_size != 0)
+ if (bfd_seek (abfd, cmd->lazy_bind_off, SEEK_SET) != 0
+ || (bfd_bwrite (cmd->lazy_bind_content, cmd->lazy_bind_size, abfd) !=
+ cmd->lazy_bind_size))
+ return FALSE;
+
+ if (cmd->export_size != 0)
+ if (bfd_seek (abfd, cmd->export_off, SEEK_SET) != 0
+ || (bfd_bwrite (cmd->export_content, cmd->export_size, abfd) !=
+ cmd->export_size))
+ return FALSE;
+
+ return TRUE;
+}
+
+long
+bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
+ asection *asect)
+{
+#if SIZEOF_LONG == SIZEOF_INT
+ if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
+ {
+ bfd_set_error (bfd_error_file_too_big);
+ return -1;
+ }
+#endif
+ return (asect->reloc_count + 1) * sizeof (arelent *);
+}
+
+/* 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. */
+
+void
+bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
+ unsigned char *fields)
+{
+ unsigned char info = fields[3];
+
+ if (bfd_big_endian (abfd))
+ {
+ rel->r_value = (fields[0] << 16) | (fields[1] << 8) | fields[2];
+ rel->r_type = (info >> BFD_MACH_O_BE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
+ rel->r_pcrel = (info & BFD_MACH_O_BE_PCREL) ? 1 : 0;
+ rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT)
+ & BFD_MACH_O_LENGTH_MASK;
+ rel->r_extern = (info & BFD_MACH_O_BE_EXTERN) ? 1 : 0;
+ }
+ else
+ {
+ rel->r_value = (fields[2] << 16) | (fields[1] << 8) | fields[0];
+ rel->r_type = (info >> BFD_MACH_O_LE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
+ rel->r_pcrel = (info & BFD_MACH_O_LE_PCREL) ? 1 : 0;
+ rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT)
+ & BFD_MACH_O_LENGTH_MASK;
+ rel->r_extern = (info & BFD_MACH_O_LE_EXTERN) ? 1 : 0;
+ }
+}
+
+/* Set syms_ptr_ptr and addend of RES. */
+
+bfd_boolean
+bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
+ bfd_mach_o_reloc_info *reloc,
+ arelent *res, asymbol **syms)
+{
+ bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
+ unsigned int num;
+ asymbol **sym;
+
+ /* Non-scattered relocation. */
+ reloc->r_scattered = 0;
+ res->addend = 0;
+
+ num = reloc->r_value;
+
+ if (reloc->r_extern)
+ {
+ /* PR 17512: file: 8396-1185-0.004. */
+ if (num >= (unsigned) bfd_mach_o_count_symbols (abfd))
+ sym = bfd_und_section_ptr->symbol_ptr_ptr;
+ else if (syms == NULL)
+ sym = bfd_und_section_ptr->symbol_ptr_ptr;
+ else
+ /* An external symbol number. */
+ sym = syms + num;
+ }
+ 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;
+ bfd_size_type native_size;
+
+ /* Allocate and read relocs. */
+ native_size = count * BFD_MACH_O_RELENT_SIZE;
+
+ /* PR 17512: file: 09477b57. */
+ if (native_size < count)
+ goto err;
+
+ native_relocs =
+ (struct mach_o_reloc_info_external *) bfd_malloc (native_size);
+ if (native_relocs == NULL)
+ return -1;
+
+ if (bfd_seek (abfd, filepos, SEEK_SET) != 0
+ || bfd_bread (native_relocs, native_size, abfd) != native_size)
+ goto err;
+
+ 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;