+
+/* Attempt to merge gots of different input bfds. Try to use as much
+ as possible of the primary got, since it doesn't require explicit
+ dynamic relocations, but don't use bfds that would reference global
+ symbols out of the addressable range. Failing the primary got,
+ attempt to merge with the current got, or finish the current got
+ and then make make the new got current. */
+
+static int
+mips_elf_merge_gots (bfd2got_, p)
+ void **bfd2got_;
+ void *p;
+{
+ struct mips_elf_bfd2got_hash *bfd2got
+ = (struct mips_elf_bfd2got_hash *)*bfd2got_;
+ struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
+ unsigned int lcount = bfd2got->g->local_gotno;
+ unsigned int gcount = bfd2got->g->global_gotno;
+ unsigned int maxcnt = arg->max_count;
+
+ /* If we don't have a primary GOT and this is not too big, use it as
+ a starting point for the primary GOT. */
+ if (! arg->primary && lcount + gcount <= maxcnt)
+ {
+ arg->primary = bfd2got->g;
+ arg->primary_count = lcount + gcount;
+ }
+ /* If it looks like we can merge this bfd's entries with those of
+ the primary, merge them. The heuristics is conservative, but we
+ don't have to squeeze it too hard. */
+ else if (arg->primary
+ && (arg->primary_count + lcount + gcount) <= maxcnt)
+ {
+ struct mips_got_info *g = bfd2got->g;
+ int old_lcount = arg->primary->local_gotno;
+ int old_gcount = arg->primary->global_gotno;
+
+ bfd2got->g = arg->primary;
+
+ htab_traverse (g->got_entries,
+ mips_elf_make_got_per_bfd,
+ arg);
+ if (arg->obfd == NULL)
+ return 0;
+
+ htab_delete (g->got_entries);
+ /* We don't have to worry about releasing memory of the actual
+ got entries, since they're all in the master got_entries hash
+ table anyway. */
+
+ BFD_ASSERT (old_lcount + lcount == arg->primary->local_gotno);
+ BFD_ASSERT (old_gcount + gcount >= arg->primary->global_gotno);
+
+ arg->primary_count = arg->primary->local_gotno
+ + arg->primary->global_gotno;
+ }
+ /* If we can merge with the last-created got, do it. */
+ else if (arg->current
+ && arg->current_count + lcount + gcount <= maxcnt)
+ {
+ struct mips_got_info *g = bfd2got->g;
+ int old_lcount = arg->current->local_gotno;
+ int old_gcount = arg->current->global_gotno;
+
+ bfd2got->g = arg->current;
+
+ htab_traverse (g->got_entries,
+ mips_elf_make_got_per_bfd,
+ arg);
+ if (arg->obfd == NULL)
+ return 0;
+
+ htab_delete (g->got_entries);
+
+ BFD_ASSERT (old_lcount + lcount == arg->current->local_gotno);
+ BFD_ASSERT (old_gcount + gcount >= arg->current->global_gotno);
+
+ arg->current_count = arg->current->local_gotno
+ + arg->current->global_gotno;
+ }
+ /* Well, we couldn't merge, so create a new GOT. Don't check if it
+ fits; if it turns out that it doesn't, we'll get relocation
+ overflows anyway. */
+ else
+ {
+ bfd2got->g->next = arg->current;
+ arg->current = bfd2got->g;
+
+ arg->current_count = lcount + gcount;
+ }
+
+ return 1;
+}
+
+/* If passed a NULL mips_got_info in the argument, set the marker used
+ to tell whether a global symbol needs a got entry (in the primary
+ got) to the given VALUE.
+
+ If passed a pointer G to a mips_got_info in the argument (it must
+ not be the primary GOT), compute the offset from the beginning of
+ the (primary) GOT section to the entry in G corresponding to the
+ global symbol. G's assigned_gotno must contain the index of the
+ first available global GOT entry in G. VALUE must contain the size
+ of a GOT entry in bytes. For each global GOT entry that requires a
+ dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
+ marked as not elligible for lazy resolution through a function
+ stub. */
+static int
+mips_elf_set_global_got_offset (entryp, p)
+ void **entryp;
+ void *p;
+{
+ struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
+ struct mips_elf_set_global_got_offset_arg *arg
+ = (struct mips_elf_set_global_got_offset_arg *)p;
+ struct mips_got_info *g = arg->g;
+
+ if (entry->abfd != NULL && entry->symndx == -1
+ && entry->d.h->root.dynindx != -1)
+ {
+ if (g)
+ {
+ BFD_ASSERT (g->global_gotsym == NULL);
+
+ entry->gotidx = arg->value * (long) g->assigned_gotno++;
+ /* We can't do lazy update of GOT entries for
+ non-primary GOTs since the PLT entries don't use the
+ right offsets, so punt at it for now. */
+ entry->d.h->no_fn_stub = TRUE;
+ if (arg->info->shared
+ || (elf_hash_table (arg->info)->dynamic_sections_created
+ && ((entry->d.h->root.elf_link_hash_flags
+ & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
+ && ((entry->d.h->root.elf_link_hash_flags
+ & ELF_LINK_HASH_DEF_REGULAR) == 0)))
+ ++arg->needed_relocs;
+ }
+ else
+ entry->d.h->root.got.offset = arg->value;
+ }
+
+ return 1;
+}
+
+/* Follow indirect and warning hash entries so that each got entry
+ points to the final symbol definition. P must point to a pointer
+ to the hash table we're traversing. Since this traversal may
+ modify the hash table, we set this pointer to NULL to indicate
+ we've made a potentially-destructive change to the hash table, so
+ the traversal must be restarted. */
+static int
+mips_elf_resolve_final_got_entry (entryp, p)
+ void **entryp;
+ void *p;
+{
+ struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
+ htab_t got_entries = *(htab_t *)p;
+
+ if (entry->abfd != NULL && entry->symndx == -1)
+ {
+ struct mips_elf_link_hash_entry *h = entry->d.h;
+
+ while (h->root.root.type == bfd_link_hash_indirect
+ || h->root.root.type == bfd_link_hash_warning)
+ h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
+
+ if (entry->d.h == h)
+ return 1;
+
+ entry->d.h = h;
+
+ /* If we can't find this entry with the new bfd hash, re-insert
+ it, and get the traversal restarted. */
+ if (! htab_find (got_entries, entry))
+ {
+ htab_clear_slot (got_entries, entryp);
+ entryp = htab_find_slot (got_entries, entry, INSERT);
+ if (! *entryp)
+ *entryp = entry;
+ /* Abort the traversal, since the whole table may have
+ moved, and leave it up to the parent to restart the
+ process. */
+ *(htab_t *)p = NULL;
+ return 0;
+ }
+ /* We might want to decrement the global_gotno count, but it's
+ either too early or too late for that at this point. */
+ }
+
+ return 1;
+}
+
+/* Turn indirect got entries in a got_entries table into their final
+ locations. */
+static void
+mips_elf_resolve_final_got_entries (g)
+ struct mips_got_info *g;
+{
+ htab_t got_entries;
+
+ do
+ {
+ got_entries = g->got_entries;
+
+ htab_traverse (got_entries,
+ mips_elf_resolve_final_got_entry,
+ &got_entries);
+ }
+ while (got_entries == NULL);
+}
+
+/* Return the offset of an input bfd IBFD's GOT from the beginning of
+ the primary GOT. */
+static bfd_vma
+mips_elf_adjust_gp (abfd, g, ibfd)
+ bfd *abfd;
+ struct mips_got_info *g;
+ bfd *ibfd;
+{
+ if (g->bfd2got == NULL)
+ return 0;
+
+ g = mips_elf_got_for_ibfd (g, ibfd);
+ if (! g)
+ return 0;
+
+ BFD_ASSERT (g->next);
+
+ g = g->next;
+
+ return (g->local_gotno + g->global_gotno) * MIPS_ELF_GOT_SIZE (abfd);
+}
+
+/* Turn a single GOT that is too big for 16-bit addressing into
+ a sequence of GOTs, each one 16-bit addressable. */
+
+static bfd_boolean
+mips_elf_multi_got (abfd, info, g, got, pages)
+ bfd *abfd;
+ struct bfd_link_info *info;
+ struct mips_got_info *g;
+ asection *got;
+ bfd_size_type pages;
+{
+ struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
+ struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
+ struct mips_got_info *gg;
+ unsigned int assign;
+
+ g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
+ mips_elf_bfd2got_entry_eq,
+ (htab_del) NULL);
+ if (g->bfd2got == NULL)
+ return FALSE;
+
+ got_per_bfd_arg.bfd2got = g->bfd2got;
+ got_per_bfd_arg.obfd = abfd;
+ got_per_bfd_arg.info = info;
+
+ /* Count how many GOT entries each input bfd requires, creating a
+ map from bfd to got info while at that. */
+ mips_elf_resolve_final_got_entries (g);
+ htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
+ if (got_per_bfd_arg.obfd == NULL)
+ return FALSE;
+
+ got_per_bfd_arg.current = NULL;
+ got_per_bfd_arg.primary = NULL;
+ /* Taking out PAGES entries is a worst-case estimate. We could
+ compute the maximum number of pages that each separate input bfd
+ uses, but it's probably not worth it. */
+ got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (abfd)
+ / MIPS_ELF_GOT_SIZE (abfd))
+ - MIPS_RESERVED_GOTNO - pages);
+
+ /* Try to merge the GOTs of input bfds together, as long as they
+ don't seem to exceed the maximum GOT size, choosing one of them
+ to be the primary GOT. */
+ htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
+ if (got_per_bfd_arg.obfd == NULL)
+ return FALSE;
+
+ /* If we find any suitable primary GOT, create an empty one. */
+ if (got_per_bfd_arg.primary == NULL)
+ {
+ g->next = (struct mips_got_info *)
+ bfd_alloc (abfd, sizeof (struct mips_got_info));
+ if (g->next == NULL)
+ return FALSE;
+
+ g->next->global_gotsym = NULL;
+ g->next->global_gotno = 0;
+ g->next->local_gotno = 0;
+ g->next->assigned_gotno = 0;
+ g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
+ mips_elf_multi_got_entry_eq,
+ (htab_del) NULL);
+ if (g->next->got_entries == NULL)
+ return FALSE;
+ g->next->bfd2got = NULL;
+ }
+ else
+ g->next = got_per_bfd_arg.primary;
+ g->next->next = got_per_bfd_arg.current;
+
+ /* GG is now the master GOT, and G is the primary GOT. */
+ gg = g;
+ g = g->next;
+
+ /* Map the output bfd to the primary got. That's what we're going
+ to use for bfds that use GOT16 or GOT_PAGE relocations that we
+ didn't mark in check_relocs, and we want a quick way to find it.
+ We can't just use gg->next because we're going to reverse the
+ list. */
+ {
+ struct mips_elf_bfd2got_hash *bfdgot;
+ void **bfdgotp;
+
+ bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
+ (abfd, sizeof (struct mips_elf_bfd2got_hash));
+
+ if (bfdgot == NULL)
+ return FALSE;
+
+ bfdgot->bfd = abfd;
+ bfdgot->g = g;
+ bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
+
+ BFD_ASSERT (*bfdgotp == NULL);
+ *bfdgotp = bfdgot;
+ }
+
+ /* The IRIX dynamic linker requires every symbol that is referenced
+ in a dynamic relocation to be present in the primary GOT, so
+ arrange for them to appear after those that are actually
+ referenced.
+
+ GNU/Linux could very well do without it, but it would slow down
+ the dynamic linker, since it would have to resolve every dynamic
+ symbol referenced in other GOTs more than once, without help from
+ the cache. Also, knowing that every external symbol has a GOT
+ helps speed up the resolution of local symbols too, so GNU/Linux
+ follows IRIX's practice.
+
+ The number 2 is used by mips_elf_sort_hash_table_f to count
+ global GOT symbols that are unreferenced in the primary GOT, with
+ an initial dynamic index computed from gg->assigned_gotno, where
+ the number of unreferenced global entries in the primary GOT is
+ preserved. */
+ if (1)
+ {
+ gg->assigned_gotno = gg->global_gotno - g->global_gotno;
+ g->global_gotno = gg->global_gotno;
+ set_got_offset_arg.value = 2;
+ }
+ else
+ {
+ /* This could be used for dynamic linkers that don't optimize
+ symbol resolution while applying relocations so as to use
+ primary GOT entries or assuming the symbol is locally-defined.
+ With this code, we assign lower dynamic indices to global
+ symbols that are not referenced in the primary GOT, so that
+ their entries can be omitted. */
+ gg->assigned_gotno = 0;
+ set_got_offset_arg.value = -1;
+ }
+
+ /* Reorder dynamic symbols as described above (which behavior
+ depends on the setting of VALUE). */
+ set_got_offset_arg.g = NULL;
+ htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
+ &set_got_offset_arg);
+ set_got_offset_arg.value = 1;
+ htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
+ &set_got_offset_arg);
+ if (! mips_elf_sort_hash_table (info, 1))
+ return FALSE;
+
+ /* Now go through the GOTs assigning them offset ranges.
+ [assigned_gotno, local_gotno[ will be set to the range of local
+ entries in each GOT. We can then compute the end of a GOT by
+ adding local_gotno to global_gotno. We reverse the list and make
+ it circular since then we'll be able to quickly compute the
+ beginning of a GOT, by computing the end of its predecessor. To
+ avoid special cases for the primary GOT, while still preserving
+ assertions that are valid for both single- and multi-got links,
+ we arrange for the main got struct to have the right number of
+ global entries, but set its local_gotno such that the initial
+ offset of the primary GOT is zero. Remember that the primary GOT
+ will become the last item in the circular linked list, so it
+ points back to the master GOT. */
+ gg->local_gotno = -g->global_gotno;
+ gg->global_gotno = g->global_gotno;
+ assign = 0;
+ gg->next = gg;
+
+ do
+ {
+ struct mips_got_info *gn;
+
+ assign += MIPS_RESERVED_GOTNO;
+ g->assigned_gotno = assign;
+ g->local_gotno += assign + pages;
+ assign = g->local_gotno + g->global_gotno;
+
+ /* Take g out of the direct list, and push it onto the reversed
+ list that gg points to. */
+ gn = g->next;
+ g->next = gg->next;
+ gg->next = g;
+ g = gn;
+ }
+ while (g);
+
+ got->_raw_size = (gg->next->local_gotno
+ + gg->next->global_gotno) * MIPS_ELF_GOT_SIZE (abfd);
+
+ return TRUE;
+}
+