+/* Relocate and write an ECOFF section into an ECOFF output file. */
+
+static bfd_boolean
+ecoff_indirect_link_order (bfd *output_bfd,
+ struct bfd_link_info *info,
+ asection *output_section,
+ struct bfd_link_order *link_order)
+{
+ asection *input_section;
+ bfd *input_bfd;
+ bfd_byte *contents = NULL;
+ bfd_size_type external_reloc_size;
+ bfd_size_type external_relocs_size;
+ void * external_relocs = NULL;
+
+ BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
+
+ input_section = link_order->u.indirect.section;
+ input_bfd = input_section->owner;
+ if (input_section->size == 0)
+ return TRUE;
+
+ BFD_ASSERT (input_section->output_section == output_section);
+ BFD_ASSERT (input_section->output_offset == link_order->offset);
+ BFD_ASSERT (input_section->size == link_order->size);
+
+ /* Get the section contents. */
+ if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
+ goto error_return;
+
+ /* Get the relocs. If we are relaxing MIPS code, they will already
+ have been read in. Otherwise, we read them in now. */
+ external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
+ external_relocs_size = external_reloc_size * input_section->reloc_count;
+
+ external_relocs = bfd_malloc (external_relocs_size);
+ if (external_relocs == NULL && external_relocs_size != 0)
+ goto error_return;
+
+ if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
+ || (bfd_bread (external_relocs, external_relocs_size, input_bfd)
+ != external_relocs_size))
+ goto error_return;
+
+ /* Relocate the section contents. */
+ if (! ((*ecoff_backend (input_bfd)->relocate_section)
+ (output_bfd, info, input_bfd, input_section, contents,
+ external_relocs)))
+ goto error_return;
+
+ /* Write out the relocated section. */
+ if (! bfd_set_section_contents (output_bfd,
+ output_section,
+ contents,
+ input_section->output_offset,
+ input_section->size))
+ goto error_return;
+
+ /* If we are producing relocatable output, the relocs were
+ modified, and we write them out now. We use the reloc_count
+ field of output_section to keep track of the number of relocs we
+ have output so far. */
+ if (bfd_link_relocatable (info))
+ {
+ file_ptr pos = (output_section->rel_filepos
+ + output_section->reloc_count * external_reloc_size);
+ if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
+ || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd)
+ != external_relocs_size))
+ goto error_return;
+ output_section->reloc_count += input_section->reloc_count;
+ }
+
+ if (contents != NULL)
+ free (contents);
+ if (external_relocs != NULL)
+ free (external_relocs);
+ return TRUE;
+
+ error_return:
+ if (contents != NULL)
+ free (contents);
+ if (external_relocs != NULL)
+ free (external_relocs);
+ return FALSE;
+}
+
+/* Generate a reloc when linking an ECOFF file. This is a reloc
+ requested by the linker, and does come from any input file. This
+ is used to build constructor and destructor tables when linking
+ with -Ur. */
+
+static bfd_boolean
+ecoff_reloc_link_order (bfd *output_bfd,
+ struct bfd_link_info *info,
+ asection *output_section,
+ struct bfd_link_order *link_order)
+{
+ enum bfd_link_order_type type;
+ asection *section;
+ bfd_vma addend;
+ arelent rel;
+ struct internal_reloc in;
+ bfd_size_type external_reloc_size;
+ bfd_byte *rbuf;
+ bfd_boolean ok;
+ file_ptr pos;
+
+ type = link_order->type;
+ section = NULL;
+ addend = link_order->u.reloc.p->addend;
+
+ /* We set up an arelent to pass to the backend adjust_reloc_out
+ routine. */
+ rel.address = link_order->offset;
+
+ rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
+ if (rel.howto == 0)
+ {
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ if (type == bfd_section_reloc_link_order)
+ {
+ section = link_order->u.reloc.p->u.section;
+ rel.sym_ptr_ptr = section->symbol_ptr_ptr;
+ }
+ else
+ {
+ struct bfd_link_hash_entry *h;
+
+ /* Treat a reloc against a defined symbol as though it were
+ actually against the section. */
+ h = bfd_wrapped_link_hash_lookup (output_bfd, info,
+ link_order->u.reloc.p->u.name,
+ FALSE, FALSE, FALSE);
+ if (h != NULL
+ && (h->type == bfd_link_hash_defined
+ || h->type == bfd_link_hash_defweak))
+ {
+ type = bfd_section_reloc_link_order;
+ section = h->u.def.section->output_section;
+ /* It seems that we ought to add the symbol value to the
+ addend here, but in practice it has already been added
+ because it was passed to constructor_callback. */
+ addend += section->vma + h->u.def.section->output_offset;
+ }
+ else
+ {
+ /* We can't set up a reloc against a symbol correctly,
+ because we have no asymbol structure. Currently no
+ adjust_reloc_out routine cares. */
+ rel.sym_ptr_ptr = NULL;
+ }
+ }
+
+ /* All ECOFF relocs are in-place. Put the addend into the object
+ file. */
+
+ BFD_ASSERT (rel.howto->partial_inplace);
+ if (addend != 0)
+ {
+ bfd_size_type size;
+ bfd_reloc_status_type rstat;
+ bfd_byte *buf;
+
+ size = bfd_get_reloc_size (rel.howto);
+ buf = (bfd_byte *) bfd_zmalloc (size);
+ if (buf == NULL && size != 0)
+ return FALSE;
+ rstat = _bfd_relocate_contents (rel.howto, output_bfd,
+ (bfd_vma) addend, buf);
+ switch (rstat)
+ {
+ case bfd_reloc_ok:
+ break;
+ default:
+ case bfd_reloc_outofrange:
+ abort ();
+ case bfd_reloc_overflow:
+ (*info->callbacks->reloc_overflow)
+ (info, NULL,
+ (link_order->type == bfd_section_reloc_link_order
+ ? bfd_section_name (section)
+ : link_order->u.reloc.p->u.name),
+ rel.howto->name, addend, NULL, NULL, (bfd_vma) 0);
+ break;
+ }
+ ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
+ (file_ptr) link_order->offset, size);
+ free (buf);
+ if (! ok)
+ return FALSE;
+ }
+
+ rel.addend = 0;
+
+ /* Move the information into an internal_reloc structure. */
+ in.r_vaddr = rel.address + bfd_section_vma (output_section);
+ in.r_type = rel.howto->type;
+
+ if (type == bfd_symbol_reloc_link_order)
+ {
+ struct ecoff_link_hash_entry *h;
+
+ h = ((struct ecoff_link_hash_entry *)
+ bfd_wrapped_link_hash_lookup (output_bfd, info,
+ link_order->u.reloc.p->u.name,
+ FALSE, FALSE, TRUE));
+ if (h != NULL
+ && h->indx != -1)
+ in.r_symndx = h->indx;
+ else
+ {
+ (*info->callbacks->unattached_reloc)
+ (info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
+ in.r_symndx = 0;
+ }
+ in.r_extern = 1;
+ }
+ else
+ {
+ const char *name;
+ unsigned int i;
+ static struct
+ {
+ const char * name;
+ long r_symndx;
+ }
+ section_symndx [] =
+ {
+ { _TEXT, RELOC_SECTION_TEXT },
+ { _RDATA, RELOC_SECTION_RDATA },
+ { _DATA, RELOC_SECTION_DATA },
+ { _SDATA, RELOC_SECTION_SDATA },
+ { _SBSS, RELOC_SECTION_SBSS },
+ { _BSS, RELOC_SECTION_BSS },
+ { _INIT, RELOC_SECTION_INIT },
+ { _LIT8, RELOC_SECTION_LIT8 },
+ { _LIT4, RELOC_SECTION_LIT4 },
+ { _XDATA, RELOC_SECTION_XDATA },
+ { _PDATA, RELOC_SECTION_PDATA },
+ { _FINI, RELOC_SECTION_FINI },
+ { _LITA, RELOC_SECTION_LITA },
+ { "*ABS*", RELOC_SECTION_ABS },
+ { _RCONST, RELOC_SECTION_RCONST }
+ };
+
+ name = bfd_section_name (section);
+
+ for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
+ if (streq (name, section_symndx[i].name))
+ {
+ in.r_symndx = section_symndx[i].r_symndx;
+ break;
+ }
+
+ if (i == ARRAY_SIZE (section_symndx))
+ abort ();
+
+ in.r_extern = 0;
+ }
+
+ /* Let the BFD backend adjust the reloc. */
+ (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
+
+ /* Get some memory and swap out the reloc. */
+ external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
+ rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
+ if (rbuf == NULL)
+ return FALSE;
+
+ (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
+
+ pos = (output_section->rel_filepos
+ + output_section->reloc_count * external_reloc_size);
+ ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
+ && (bfd_bwrite ((void *) rbuf, external_reloc_size, output_bfd)
+ == external_reloc_size));
+
+ if (ok)
+ ++output_section->reloc_count;
+
+ free (rbuf);
+
+ return ok;
+}
+