+/* 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;
+ }
+ }
+
+ if (done_something)
+ (*htab->layout_sections_again) ();
+
+ /* Set up for second pass over toc sections to recalculate elf_gp
+ on input sections. */
+ htab->toc_bfd = NULL;
+ htab->toc_first_sec = NULL;
+ htab->second_toc_pass = TRUE;
+ return done_something;
+}
+
+/* Called after second pass of multitoc partitioning. */