+ dynobj = elf_hash_table (info)->dynobj;
+
+ g = mips_elf_got_info (dynobj, NULL);
+
+ hsd.low = NULL;
+ hsd.max_unref_got_dynindx =
+ hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
+ /* In the multi-got case, assigned_gotno of the master got_info
+ indicate the number of entries that aren't referenced in the
+ primary GOT, but that must have entries because there are
+ dynamic relocations that reference it. Since they aren't
+ referenced, we move them to the end of the GOT, so that they
+ don't prevent other entries that are referenced from getting
+ too large offsets. */
+ - (g->next ? g->assigned_gotno : 0);
+ hsd.max_non_got_dynindx = max_local;
+ mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
+ elf_hash_table (info)),
+ mips_elf_sort_hash_table_f,
+ &hsd);
+
+ /* There should have been enough room in the symbol table to
+ accommodate both the GOT and non-GOT symbols. */
+ BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
+ BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
+ <= elf_hash_table (info)->dynsymcount);
+
+ /* Now we know which dynamic symbol has the lowest dynamic symbol
+ table index in the GOT. */
+ g->global_gotsym = hsd.low;
+
+ return TRUE;
+}
+
+/* If H needs a GOT entry, assign it the highest available dynamic
+ index. Otherwise, assign it the lowest available dynamic
+ index. */
+
+static bfd_boolean
+mips_elf_sort_hash_table_f (h, data)
+ struct mips_elf_link_hash_entry *h;
+ PTR data;
+{
+ struct mips_elf_hash_sort_data *hsd
+ = (struct mips_elf_hash_sort_data *) data;
+
+ if (h->root.root.type == bfd_link_hash_warning)
+ h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
+
+ /* Symbols without dynamic symbol table entries aren't interesting
+ at all. */
+ if (h->root.dynindx == -1)
+ return TRUE;
+
+ /* Global symbols that need GOT entries that are not explicitly
+ referenced are marked with got offset 2. Those that are
+ referenced get a 1, and those that don't need GOT entries get
+ -1. */
+ if (h->root.got.offset == 2)
+ {
+ if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
+ hsd->low = (struct elf_link_hash_entry *) h;
+ h->root.dynindx = hsd->max_unref_got_dynindx++;
+ }
+ else if (h->root.got.offset != 1)
+ h->root.dynindx = hsd->max_non_got_dynindx++;
+ else
+ {
+ h->root.dynindx = --hsd->min_got_dynindx;
+ hsd->low = (struct elf_link_hash_entry *) h;
+ }
+
+ 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 eligible 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++;
+ 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;
+}
+
+/* Mark any global symbols referenced in the GOT we are iterating over
+ as inelligible for lazy resolution stubs. */
+static int
+mips_elf_set_no_stub (entryp, p)
+ void **entryp;
+ void *p ATTRIBUTE_UNUSED;
+{
+ struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
+
+ if (entry->abfd != NULL
+ && entry->symndx == -1
+ && entry->d.h->root.dynindx != -1)
+ entry->d.h->no_fn_stub = TRUE;
+
+ 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);
+}