+ /* Keep track of the first .toc or .got section for this input bfd. */
+ if (htab->toc_bfd != isec->owner)
+ {
+ htab->toc_bfd = isec->owner;
+ htab->toc_first_sec = isec;
+ }
+
+ addr = isec->output_offset + isec->output_section->vma;
+ off = addr - htab->toc_curr;
+ if (off + isec->size > 0x10000)
+ {
+ addr = (htab->toc_first_sec->output_offset
+ + htab->toc_first_sec->output_section->vma);
+ htab->toc_curr = addr;
+ }
+
+ /* toc_curr is the base address of this toc group. Set elf_gp
+ for the input section to be the offset relative to the
+ output toc base plus 0x8000. Making the input elf_gp an
+ offset allows us to move the toc as a whole without
+ recalculating input elf_gp. */
+ off = htab->toc_curr - elf_gp (isec->output_section->owner);
+ off += TOC_BASE_OFF;
+
+ /* Die if someone uses a linker script that doesn't keep input
+ file .toc and .got together. */
+ if (elf_gp (isec->owner) != 0
+ && elf_gp (isec->owner) != off)
+ return FALSE;
+
+ elf_gp (isec->owner) = off;
+ return TRUE;
+ }
+
+ /* During the second pass toc_first_sec points to the start of
+ a toc group, and toc_curr is used to track the old elf_gp.
+ We use toc_bfd to ensure we only look at each bfd once. */
+ if (htab->toc_bfd == isec->owner)
+ return TRUE;
+ htab->toc_bfd = isec->owner;
+
+ if (htab->toc_first_sec == NULL
+ || htab->toc_curr != elf_gp (isec->owner))
+ {
+ htab->toc_curr = elf_gp (isec->owner);
+ htab->toc_first_sec = isec;
+ }
+ addr = (htab->toc_first_sec->output_offset
+ + htab->toc_first_sec->output_section->vma);
+ off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
+ elf_gp (isec->owner) = off;
+
+ return TRUE;
+}
+
+/* This function removes unneeded got entries (those with got.offset == -1)
+ and merges entries in the same toc group. */
+
+static void
+merge_got_entries (struct got_entry **pent)
+{
+ struct got_entry *ent, *ent2;
+
+ while ((ent = *pent) != NULL)
+ {
+ if (!ent->is_indirect)
+ {
+ if (ent->got.offset == (bfd_vma) -1)
+ {
+ *pent = ent->next;
+ continue;
+ }
+ for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
+ if (!ent2->is_indirect
+ && ent2->got.offset != (bfd_vma) -1
+ && ent2->addend == ent->addend
+ && ent2->tls_type == ent->tls_type
+ && elf_gp (ent2->owner) == elf_gp (ent->owner))
+ {
+ ent2->is_indirect = TRUE;
+ ent2->got.ent = ent;
+ }
+ }
+ pent = &ent->next;
+ }
+}
+
+/* Called via elf_link_hash_traverse to merge GOT entries for global
+ symbol H. */
+
+static bfd_boolean
+merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
+{
+ if (h->root.type == bfd_link_hash_indirect)
+ return TRUE;
+
+ if (h->root.type == bfd_link_hash_warning)
+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+ merge_got_entries (&h->got.glist);
+
+ return TRUE;
+}
+
+/* Called via elf_link_hash_traverse to allocate GOT entries for global
+ symbol H. */
+
+static bfd_boolean
+reallocate_got (struct elf_link_hash_entry *h, void *inf)
+{
+ struct got_entry *gent;
+
+ if (h->root.type == bfd_link_hash_indirect)
+ return TRUE;
+
+ if (h->root.type == bfd_link_hash_warning)
+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+ for (gent = h->got.glist; gent != NULL; gent = gent->next)
+ if (!gent->is_indirect)
+ allocate_got (h, (struct bfd_link_info *) inf, gent);
+ return TRUE;
+}
+
+/* Called on the first multitoc pass after the last call to
+ ppc64_elf_next_toc_section. This function removes duplicate GOT
+ entries. */
+
+bfd_boolean
+ppc64_elf_layout_multitoc (struct bfd_link_info *info)
+{
+ struct ppc_link_hash_table *htab = ppc_hash_table (info);
+ struct bfd *ibfd, *ibfd2;
+ bfd_boolean done_something;
+
+ htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
+
+ /* Merge local got entries within a toc group. */
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+ {
+ struct got_entry **lgot_ents;
+ struct got_entry **end_lgot_ents;
+ Elf_Internal_Shdr *symtab_hdr;
+ bfd_size_type locsymcount;
+
+ if (!is_ppc64_elf (ibfd))
+ continue;
+
+ lgot_ents = elf_local_got_ents (ibfd);
+ if (!lgot_ents)
+ continue;
+
+ symtab_hdr = &elf_symtab_hdr (ibfd);
+ locsymcount = symtab_hdr->sh_info;
+ end_lgot_ents = lgot_ents + locsymcount;
+
+ for (; lgot_ents < end_lgot_ents; ++lgot_ents)
+ merge_got_entries (lgot_ents);
+ }
+
+ /* And the same for global sym got entries. */
+ elf_link_hash_traverse (&htab->elf, merge_global_got, info);
+
+ /* And tlsld_got. */
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+ {
+ struct got_entry *ent, *ent2;
+
+ if (!is_ppc64_elf (ibfd))
+ continue;
+
+ ent = ppc64_tlsld_got (ibfd);
+ if (!ent->is_indirect
+ && ent->got.offset != (bfd_vma) -1)
+ {
+ for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
+ {
+ if (!is_ppc64_elf (ibfd2))
+ continue;
+
+ ent2 = ppc64_tlsld_got (ibfd2);
+ if (!ent2->is_indirect
+ && ent2->got.offset != (bfd_vma) -1
+ && elf_gp (ibfd2) == elf_gp (ibfd))
+ {
+ ent2->is_indirect = TRUE;
+ ent2->got.ent = ent;
+ }
+ }
+ }
+ }
+
+ /* Zap sizes of got sections. */
+ htab->reliplt->rawsize = htab->reliplt->size;
+ htab->reliplt->size -= htab->got_reli_size;
+ htab->got_reli_size = 0;
+
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+ {
+ asection *got, *relgot;
+
+ if (!is_ppc64_elf (ibfd))
+ continue;
+
+ got = ppc64_elf_tdata (ibfd)->got;
+ if (got != NULL)
+ {
+ got->rawsize = got->size;
+ got->size = 0;
+ relgot = ppc64_elf_tdata (ibfd)->relgot;
+ relgot->rawsize = relgot->size;
+ relgot->size = 0;
+ }
+ }
+
+ /* Now reallocate the got, local syms first. We don't need to
+ allocate section contents again since we never increase size. */
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+ {
+ struct got_entry **lgot_ents;
+ struct got_entry **end_lgot_ents;
+ struct plt_entry **local_plt;
+ struct plt_entry **end_local_plt;
+ char *lgot_masks;
+ bfd_size_type locsymcount;
+ Elf_Internal_Shdr *symtab_hdr;
+ asection *s, *srel;
+
+ if (!is_ppc64_elf (ibfd))
+ continue;
+
+ lgot_ents = elf_local_got_ents (ibfd);
+ if (!lgot_ents)
+ continue;
+
+ symtab_hdr = &elf_symtab_hdr (ibfd);
+ locsymcount = symtab_hdr->sh_info;
+ end_lgot_ents = lgot_ents + locsymcount;
+ local_plt = (struct plt_entry **) end_lgot_ents;
+ end_local_plt = local_plt + locsymcount;
+ lgot_masks = (char *) end_local_plt;
+ s = ppc64_elf_tdata (ibfd)->got;
+ srel = ppc64_elf_tdata (ibfd)->relgot;
+ for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
+ {
+ struct got_entry *ent;
+
+ for (ent = *lgot_ents; ent != NULL; ent = ent->next)
+ if (!ent->is_indirect)
+ {
+ unsigned int num = 1;
+ ent->got.offset = s->size;
+ if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
+ num = 2;
+ s->size += num * 8;
+ if (info->shared)
+ srel->size += num * sizeof (Elf64_External_Rela);
+ else if ((*lgot_masks & PLT_IFUNC) != 0)
+ {
+ htab->reliplt->size
+ += num * sizeof (Elf64_External_Rela);
+ htab->got_reli_size
+ += num * sizeof (Elf64_External_Rela);
+ }
+ }
+ }
+ }
+
+ elf_link_hash_traverse (&htab->elf, reallocate_got, info);
+
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+ {
+ struct got_entry *ent;
+
+ if (!is_ppc64_elf (ibfd))
+ continue;
+
+ ent = ppc64_tlsld_got (ibfd);
+ if (!ent->is_indirect
+ && ent->got.offset != (bfd_vma) -1)
+ {
+ asection *s = ppc64_elf_tdata (ibfd)->got;
+ ent->got.offset = s->size;
+ s->size += 16;
+ if (info->shared)
+ {
+ asection *srel = ppc64_elf_tdata (ibfd)->relgot;
+ srel->size += sizeof (Elf64_External_Rela);
+ }
+ }
+ }
+
+ done_something = htab->reliplt->rawsize != htab->reliplt->size;
+ if (!done_something)
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+ {
+ asection *got;
+
+ if (!is_ppc64_elf (ibfd))
+ continue;
+
+ got = ppc64_elf_tdata (ibfd)->got;
+ if (got != NULL)
+ {
+ done_something = got->rawsize != got->size;
+ if (done_something)
+ break;
+ }
+ }