+ return TRUE;
+}
+
+/* If H is a symbol that needs a global GOT entry, but has a dynamic
+ symbol table index lower than any we've seen to date, record it for
+ posterity. */
+
+static bfd_boolean
+mips_elf_record_global_got_symbol (h, abfd, info, g)
+ struct elf_link_hash_entry *h;
+ bfd *abfd;
+ struct bfd_link_info *info;
+ struct mips_got_info *g;
+{
+ struct mips_got_entry entry, **loc;
+
+ /* A global symbol in the GOT must also be in the dynamic symbol
+ table. */
+ if (h->dynindx == -1)
+ {
+ switch (ELF_ST_VISIBILITY (h->other))
+ {
+ case STV_INTERNAL:
+ case STV_HIDDEN:
+ _bfd_mips_elf_hide_symbol (info, h, TRUE);
+ break;
+ }
+ if (!bfd_elf32_link_record_dynamic_symbol (info, h))
+ return FALSE;
+ }
+
+ entry.abfd = abfd;
+ entry.symndx = -1;
+ entry.d.h = (struct mips_elf_link_hash_entry *) h;
+
+ loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
+ INSERT);
+
+ /* If we've already marked this entry as needing GOT space, we don't
+ need to do it again. */
+ if (*loc)
+ return TRUE;
+
+ *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
+
+ if (! *loc)
+ return FALSE;
+
+ entry.gotidx = -1;
+ memcpy (*loc, &entry, sizeof entry);
+
+ if (h->got.offset != MINUS_ONE)
+ return TRUE;
+
+ /* By setting this to a value other than -1, we are indicating that
+ there needs to be a GOT entry for H. Avoid using zero, as the
+ generic ELF copy_indirect_symbol tests for <= 0. */
+ h->got.offset = 1;
+
+ return TRUE;
+}
+
+/* Reserve space in G for a GOT entry containing the value of symbol
+ SYMNDX in input bfd ABDF, plus ADDEND. */
+
+static bfd_boolean
+mips_elf_record_local_got_symbol (abfd, symndx, addend, g)
+ bfd *abfd;
+ long symndx;
+ bfd_vma addend;
+ struct mips_got_info *g;
+{
+ struct mips_got_entry entry, **loc;
+
+ entry.abfd = abfd;
+ entry.symndx = symndx;
+ entry.d.addend = addend;
+ loc = (struct mips_got_entry **)
+ htab_find_slot (g->got_entries, &entry, INSERT);
+
+ if (*loc)
+ return TRUE;
+
+ entry.gotidx = g->local_gotno++;
+
+ *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
+
+ if (! *loc)
+ return FALSE;
+
+ memcpy (*loc, &entry, sizeof entry);
+
+ return TRUE;
+}
+\f
+/* Compute the hash value of the bfd in a bfd2got hash entry. */
+
+static hashval_t
+mips_elf_bfd2got_entry_hash (entry_)
+ const PTR entry_;
+{
+ const struct mips_elf_bfd2got_hash *entry
+ = (struct mips_elf_bfd2got_hash *)entry_;
+
+ return entry->bfd->id;
+}
+
+/* Check whether two hash entries have the same bfd. */
+
+static int
+mips_elf_bfd2got_entry_eq (entry1, entry2)
+ const PTR entry1;
+ const PTR entry2;
+{
+ const struct mips_elf_bfd2got_hash *e1
+ = (const struct mips_elf_bfd2got_hash *)entry1;
+ const struct mips_elf_bfd2got_hash *e2
+ = (const struct mips_elf_bfd2got_hash *)entry2;
+
+ return e1->bfd == e2->bfd;
+}
+
+/* In a multi-got link, determine the GOT to be used for IBDF. G must
+ be the master GOT data. */
+
+static struct mips_got_info *
+mips_elf_got_for_ibfd (g, ibfd)
+ struct mips_got_info *g;
+ bfd *ibfd;
+{
+ struct mips_elf_bfd2got_hash e, *p;
+
+ if (! g->bfd2got)
+ return g;
+
+ e.bfd = ibfd;
+ p = (struct mips_elf_bfd2got_hash *) htab_find (g->bfd2got, &e);
+ return p ? p->g : NULL;
+}
+
+/* Create one separate got for each bfd that has entries in the global
+ got, such that we can tell how many local and global entries each
+ bfd requires. */
+
+static int
+mips_elf_make_got_per_bfd (entryp, p)
+ void **entryp;
+ void *p;
+{
+ struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
+ struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
+ htab_t bfd2got = arg->bfd2got;
+ struct mips_got_info *g;
+ struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
+ void **bfdgotp;
+
+ /* Find the got_info for this GOT entry's input bfd. Create one if
+ none exists. */
+ bfdgot_entry.bfd = entry->abfd;
+ bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
+ bfdgot = (struct mips_elf_bfd2got_hash *)*bfdgotp;
+
+ if (bfdgot != NULL)
+ g = bfdgot->g;
+ else
+ {
+ bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
+ (arg->obfd, sizeof (struct mips_elf_bfd2got_hash));
+
+ if (bfdgot == NULL)
+ {
+ arg->obfd = 0;
+ return 0;
+ }
+
+ *bfdgotp = bfdgot;
+
+ bfdgot->bfd = entry->abfd;
+ bfdgot->g = g = (struct mips_got_info *)
+ bfd_alloc (arg->obfd, sizeof (struct mips_got_info));
+ if (g == NULL)
+ {
+ arg->obfd = 0;
+ return 0;
+ }
+
+ g->global_gotsym = NULL;
+ g->global_gotno = 0;
+ g->local_gotno = 0;
+ g->assigned_gotno = -1;
+ g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
+ mips_elf_multi_got_entry_eq,
+ (htab_del) NULL);
+ if (g->got_entries == NULL)
+ {
+ arg->obfd = 0;
+ return 0;
+ }
+
+ g->bfd2got = NULL;
+ g->next = NULL;
+ }
+
+ /* Insert the GOT entry in the bfd's got entry hash table. */
+ entryp = htab_find_slot (g->got_entries, entry, INSERT);
+ if (*entryp != NULL)
+ return 1;
+
+ *entryp = entry;
+
+ if (entry->symndx >= 0 || entry->d.h->forced_local)
+ ++g->local_gotno;
+ else
+ ++g->global_gotno;
+
+ return 1;
+}
+
+/* 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;