xref: /netbsd-src/external/gpl3/binutils/dist/bfd/elfxx-mips.c (revision b45fa494daa2ba02187711d31a4144faf0993066)
1 /* MIPS-specific support for ELF
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 
5    Most of the information added by Ian Lance Taylor, Cygnus Support,
6    <ian@cygnus.com>.
7    N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8    <mark@codesourcery.com>
9    Traditional MIPS targets support added by Koundinya.K, Dansk Data
10    Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11 
12    This file is part of BFD, the Binary File Descriptor library.
13 
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 3 of the License, or
17    (at your option) any later version.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
27    MA 02110-1301, USA.  */
28 
29 
30 /* This file handles functionality common to the different MIPS ABI's.  */
31 
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "libbfd.h"
35 #include "libiberty.h"
36 #include "elf-bfd.h"
37 #include "elfxx-mips.h"
38 #include "elf/mips.h"
39 #include "elf-vxworks.h"
40 
41 /* Get the ECOFF swapping routines.  */
42 #include "coff/sym.h"
43 #include "coff/symconst.h"
44 #include "coff/ecoff.h"
45 #include "coff/mips.h"
46 
47 #include "hashtab.h"
48 
49 /* This structure is used to hold information about one GOT entry.
50    There are three types of entry:
51 
52       (1) absolute addresses
53 	    (abfd == NULL)
54       (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 	    (abfd != NULL, symndx >= 0)
56       (3) global and forced-local symbols
57 	    (abfd != NULL, symndx == -1)
58 
59    Type (3) entries are treated differently for different types of GOT.
60    In the "master" GOT -- i.e.  the one that describes every GOT
61    reference needed in the link -- the mips_got_entry is keyed on both
62    the symbol and the input bfd that references it.  If it turns out
63    that we need multiple GOTs, we can then use this information to
64    create separate GOTs for each input bfd.
65 
66    However, we want each of these separate GOTs to have at most one
67    entry for a given symbol, so their type (3) entries are keyed only
68    on the symbol.  The input bfd given by the "abfd" field is somewhat
69    arbitrary in this case.
70 
71    This means that when there are multiple GOTs, each GOT has a unique
72    mips_got_entry for every symbol within it.  We can therefore use the
73    mips_got_entry fields (tls_type and gotidx) to track the symbol's
74    GOT index.
75 
76    However, if it turns out that we need only a single GOT, we continue
77    to use the master GOT to describe it.  There may therefore be several
78    mips_got_entries for the same symbol, each with a different input bfd.
79    We want to make sure that each symbol gets a unique GOT entry, so when
80    there's a single GOT, we use the symbol's hash entry, not the
81    mips_got_entry fields, to track a symbol's GOT index.  */
82 struct mips_got_entry
83 {
84   /* The input bfd in which the symbol is defined.  */
85   bfd *abfd;
86   /* The index of the symbol, as stored in the relocation r_info, if
87      we have a local symbol; -1 otherwise.  */
88   long symndx;
89   union
90   {
91     /* If abfd == NULL, an address that must be stored in the got.  */
92     bfd_vma address;
93     /* If abfd != NULL && symndx != -1, the addend of the relocation
94        that should be added to the symbol value.  */
95     bfd_vma addend;
96     /* If abfd != NULL && symndx == -1, the hash table entry
97        corresponding to a global symbol in the got (or, local, if
98        h->forced_local).  */
99     struct mips_elf_link_hash_entry *h;
100   } d;
101 
102   /* The TLS types included in this GOT entry (specifically, GD and
103      IE).  The GD and IE flags can be added as we encounter new
104      relocations.  LDM can also be set; it will always be alone, not
105      combined with any GD or IE flags.  An LDM GOT entry will be
106      a local symbol entry with r_symndx == 0.  */
107   unsigned char tls_type;
108 
109   /* The offset from the beginning of the .got section to the entry
110      corresponding to this symbol+addend.  If it's a global symbol
111      whose offset is yet to be decided, it's going to be -1.  */
112   long gotidx;
113 };
114 
115 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
116    The structures form a non-overlapping list that is sorted by increasing
117    MIN_ADDEND.  */
118 struct mips_got_page_range
119 {
120   struct mips_got_page_range *next;
121   bfd_signed_vma min_addend;
122   bfd_signed_vma max_addend;
123 };
124 
125 /* This structure describes the range of addends that are applied to page
126    relocations against a given symbol.  */
127 struct mips_got_page_entry
128 {
129   /* The input bfd in which the symbol is defined.  */
130   bfd *abfd;
131   /* The index of the symbol, as stored in the relocation r_info.  */
132   long symndx;
133   /* The ranges for this page entry.  */
134   struct mips_got_page_range *ranges;
135   /* The maximum number of page entries needed for RANGES.  */
136   bfd_vma num_pages;
137 };
138 
139 /* This structure is used to hold .got information when linking.  */
140 
141 struct mips_got_info
142 {
143   /* The global symbol in the GOT with the lowest index in the dynamic
144      symbol table.  */
145   struct elf_link_hash_entry *global_gotsym;
146   /* The number of global .got entries.  */
147   unsigned int global_gotno;
148   /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
149   unsigned int reloc_only_gotno;
150   /* The number of .got slots used for TLS.  */
151   unsigned int tls_gotno;
152   /* The first unused TLS .got entry.  Used only during
153      mips_elf_initialize_tls_index.  */
154   unsigned int tls_assigned_gotno;
155   /* The number of local .got entries, eventually including page entries.  */
156   unsigned int local_gotno;
157   /* The maximum number of page entries needed.  */
158   unsigned int page_gotno;
159   /* The number of local .got entries we have used.  */
160   unsigned int assigned_gotno;
161   /* A hash table holding members of the got.  */
162   struct htab *got_entries;
163   /* A hash table of mips_got_page_entry structures.  */
164   struct htab *got_page_entries;
165   /* A hash table mapping input bfds to other mips_got_info.  NULL
166      unless multi-got was necessary.  */
167   struct htab *bfd2got;
168   /* In multi-got links, a pointer to the next got (err, rather, most
169      of the time, it points to the previous got).  */
170   struct mips_got_info *next;
171   /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
172      for none, or MINUS_TWO for not yet assigned.  This is needed
173      because a single-GOT link may have multiple hash table entries
174      for the LDM.  It does not get initialized in multi-GOT mode.  */
175   bfd_vma tls_ldm_offset;
176 };
177 
178 /* Map an input bfd to a got in a multi-got link.  */
179 
180 struct mips_elf_bfd2got_hash {
181   bfd *bfd;
182   struct mips_got_info *g;
183 };
184 
185 /* Structure passed when traversing the bfd2got hash table, used to
186    create and merge bfd's gots.  */
187 
188 struct mips_elf_got_per_bfd_arg
189 {
190   /* A hashtable that maps bfds to gots.  */
191   htab_t bfd2got;
192   /* The output bfd.  */
193   bfd *obfd;
194   /* The link information.  */
195   struct bfd_link_info *info;
196   /* A pointer to the primary got, i.e., the one that's going to get
197      the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198      DT_MIPS_GOTSYM.  */
199   struct mips_got_info *primary;
200   /* A non-primary got we're trying to merge with other input bfd's
201      gots.  */
202   struct mips_got_info *current;
203   /* The maximum number of got entries that can be addressed with a
204      16-bit offset.  */
205   unsigned int max_count;
206   /* The maximum number of page entries needed by each got.  */
207   unsigned int max_pages;
208   /* The total number of global entries which will live in the
209      primary got and be automatically relocated.  This includes
210      those not referenced by the primary GOT but included in
211      the "master" GOT.  */
212   unsigned int global_count;
213 };
214 
215 /* Another structure used to pass arguments for got entries traversal.  */
216 
217 struct mips_elf_set_global_got_offset_arg
218 {
219   struct mips_got_info *g;
220   int value;
221   unsigned int needed_relocs;
222   struct bfd_link_info *info;
223 };
224 
225 /* A structure used to count TLS relocations or GOT entries, for GOT
226    entry or ELF symbol table traversal.  */
227 
228 struct mips_elf_count_tls_arg
229 {
230   struct bfd_link_info *info;
231   unsigned int needed;
232 };
233 
234 struct _mips_elf_section_data
235 {
236   struct bfd_elf_section_data elf;
237   union
238   {
239     bfd_byte *tdata;
240   } u;
241 };
242 
243 #define mips_elf_section_data(sec) \
244   ((struct _mips_elf_section_data *) elf_section_data (sec))
245 
246 #define is_mips_elf(bfd)				\
247   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
248    && elf_tdata (bfd) != NULL				\
249    && elf_object_id (bfd) == MIPS_ELF_TDATA)
250 
251 /* The ABI says that every symbol used by dynamic relocations must have
252    a global GOT entry.  Among other things, this provides the dynamic
253    linker with a free, directly-indexed cache.  The GOT can therefore
254    contain symbols that are not referenced by GOT relocations themselves
255    (in other words, it may have symbols that are not referenced by things
256    like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
257 
258    GOT relocations are less likely to overflow if we put the associated
259    GOT entries towards the beginning.  We therefore divide the global
260    GOT entries into two areas: "normal" and "reloc-only".  Entries in
261    the first area can be used for both dynamic relocations and GP-relative
262    accesses, while those in the "reloc-only" area are for dynamic
263    relocations only.
264 
265    These GGA_* ("Global GOT Area") values are organised so that lower
266    values are more general than higher values.  Also, non-GGA_NONE
267    values are ordered by the position of the area in the GOT.  */
268 #define GGA_NORMAL 0
269 #define GGA_RELOC_ONLY 1
270 #define GGA_NONE 2
271 
272 /* Information about a non-PIC interface to a PIC function.  There are
273    two ways of creating these interfaces.  The first is to add:
274 
275 	lui	$25,%hi(func)
276 	addiu	$25,$25,%lo(func)
277 
278    immediately before a PIC function "func".  The second is to add:
279 
280 	lui	$25,%hi(func)
281 	j	func
282 	addiu	$25,$25,%lo(func)
283 
284    to a separate trampoline section.
285 
286    Stubs of the first kind go in a new section immediately before the
287    target function.  Stubs of the second kind go in a single section
288    pointed to by the hash table's "strampoline" field.  */
289 struct mips_elf_la25_stub {
290   /* The generated section that contains this stub.  */
291   asection *stub_section;
292 
293   /* The offset of the stub from the start of STUB_SECTION.  */
294   bfd_vma offset;
295 
296   /* One symbol for the original function.  Its location is available
297      in H->root.root.u.def.  */
298   struct mips_elf_link_hash_entry *h;
299 };
300 
301 /* Macros for populating a mips_elf_la25_stub.  */
302 
303 #define LA25_LUI(VAL) (0x3c190000 | (VAL))	/* lui t9,VAL */
304 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
305 #define LA25_ADDIU(VAL) (0x27390000 | (VAL))	/* addiu t9,t9,VAL */
306 
307 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
308    the dynamic symbols.  */
309 
310 struct mips_elf_hash_sort_data
311 {
312   /* The symbol in the global GOT with the lowest dynamic symbol table
313      index.  */
314   struct elf_link_hash_entry *low;
315   /* The least dynamic symbol table index corresponding to a non-TLS
316      symbol with a GOT entry.  */
317   long min_got_dynindx;
318   /* The greatest dynamic symbol table index corresponding to a symbol
319      with a GOT entry that is not referenced (e.g., a dynamic symbol
320      with dynamic relocations pointing to it from non-primary GOTs).  */
321   long max_unref_got_dynindx;
322   /* The greatest dynamic symbol table index not corresponding to a
323      symbol without a GOT entry.  */
324   long max_non_got_dynindx;
325 };
326 
327 /* The MIPS ELF linker needs additional information for each symbol in
328    the global hash table.  */
329 
330 struct mips_elf_link_hash_entry
331 {
332   struct elf_link_hash_entry root;
333 
334   /* External symbol information.  */
335   EXTR esym;
336 
337   /* The la25 stub we have created for ths symbol, if any.  */
338   struct mips_elf_la25_stub *la25_stub;
339 
340   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
341      this symbol.  */
342   unsigned int possibly_dynamic_relocs;
343 
344   /* If there is a stub that 32 bit functions should use to call this
345      16 bit function, this points to the section containing the stub.  */
346   asection *fn_stub;
347 
348   /* If there is a stub that 16 bit functions should use to call this
349      32 bit function, this points to the section containing the stub.  */
350   asection *call_stub;
351 
352   /* This is like the call_stub field, but it is used if the function
353      being called returns a floating point value.  */
354   asection *call_fp_stub;
355 
356 #define GOT_NORMAL	0
357 #define GOT_TLS_GD	1
358 #define GOT_TLS_LDM	2
359 #define GOT_TLS_IE	4
360 #define GOT_TLS_OFFSET_DONE    0x40
361 #define GOT_TLS_DONE    0x80
362   unsigned char tls_type;
363 
364   /* This is only used in single-GOT mode; in multi-GOT mode there
365      is one mips_got_entry per GOT entry, so the offset is stored
366      there.  In single-GOT mode there may be many mips_got_entry
367      structures all referring to the same GOT slot.  It might be
368      possible to use root.got.offset instead, but that field is
369      overloaded already.  */
370   bfd_vma tls_got_offset;
371 
372   /* The highest GGA_* value that satisfies all references to this symbol.  */
373   unsigned int global_got_area : 2;
374 
375   /* True if one of the relocations described by possibly_dynamic_relocs
376      is against a readonly section.  */
377   unsigned int readonly_reloc : 1;
378 
379   /* True if there is a relocation against this symbol that must be
380      resolved by the static linker (in other words, if the relocation
381      cannot possibly be made dynamic).  */
382   unsigned int has_static_relocs : 1;
383 
384   /* True if we must not create a .MIPS.stubs entry for this symbol.
385      This is set, for example, if there are relocations related to
386      taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
387      See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
388   unsigned int no_fn_stub : 1;
389 
390   /* Whether we need the fn_stub; this is true if this symbol appears
391      in any relocs other than a 16 bit call.  */
392   unsigned int need_fn_stub : 1;
393 
394   /* True if this symbol is referenced by branch relocations from
395      any non-PIC input file.  This is used to determine whether an
396      la25 stub is required.  */
397   unsigned int has_nonpic_branches : 1;
398 
399   /* Does this symbol need a traditional MIPS lazy-binding stub
400      (as opposed to a PLT entry)?  */
401   unsigned int needs_lazy_stub : 1;
402 };
403 
404 /* MIPS ELF linker hash table.  */
405 
406 struct mips_elf_link_hash_table
407 {
408   struct elf_link_hash_table root;
409 #if 0
410   /* We no longer use this.  */
411   /* String section indices for the dynamic section symbols.  */
412   bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
413 #endif
414 
415   /* The number of .rtproc entries.  */
416   bfd_size_type procedure_count;
417 
418   /* The size of the .compact_rel section (if SGI_COMPAT).  */
419   bfd_size_type compact_rel_size;
420 
421   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
422      entry is set to the address of __rld_obj_head as in IRIX5.  */
423   bfd_boolean use_rld_obj_head;
424 
425   /* This is the value of the __rld_map or __rld_obj_head symbol.  */
426   bfd_vma rld_value;
427 
428   /* This is set if we see any mips16 stub sections.  */
429   bfd_boolean mips16_stubs_seen;
430 
431   /* True if we can generate copy relocs and PLTs.  */
432   bfd_boolean use_plts_and_copy_relocs;
433 
434   /* True if we're generating code for VxWorks.  */
435   bfd_boolean is_vxworks;
436 
437   /* True if we already reported the small-data section overflow.  */
438   bfd_boolean small_data_overflow_reported;
439 
440   /* Shortcuts to some dynamic sections, or NULL if they are not
441      being used.  */
442   asection *srelbss;
443   asection *sdynbss;
444   asection *srelplt;
445   asection *srelplt2;
446   asection *sgotplt;
447   asection *splt;
448   asection *sstubs;
449   asection *sgot;
450 
451   /* The master GOT information.  */
452   struct mips_got_info *got_info;
453 
454   /* The size of the PLT header in bytes.  */
455   bfd_vma plt_header_size;
456 
457   /* The size of a PLT entry in bytes.  */
458   bfd_vma plt_entry_size;
459 
460   /* The number of functions that need a lazy-binding stub.  */
461   bfd_vma lazy_stub_count;
462 
463   /* The size of a function stub entry in bytes.  */
464   bfd_vma function_stub_size;
465 
466   /* The number of reserved entries at the beginning of the GOT.  */
467   unsigned int reserved_gotno;
468 
469   /* The section used for mips_elf_la25_stub trampolines.
470      See the comment above that structure for details.  */
471   asection *strampoline;
472 
473   /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
474      pairs.  */
475   htab_t la25_stubs;
476 
477   /* A function FN (NAME, IS, OS) that creates a new input section
478      called NAME and links it to output section OS.  If IS is nonnull,
479      the new section should go immediately before it, otherwise it
480      should go at the (current) beginning of OS.
481 
482      The function returns the new section on success, otherwise it
483      returns null.  */
484   asection *(*add_stub_section) (const char *, asection *, asection *);
485 };
486 
487 /* A structure used to communicate with htab_traverse callbacks.  */
488 struct mips_htab_traverse_info {
489   /* The usual link-wide information.  */
490   struct bfd_link_info *info;
491   bfd *output_bfd;
492 
493   /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
494   bfd_boolean error;
495 };
496 
497 #define TLS_RELOC_P(r_type) \
498   (r_type == R_MIPS_TLS_DTPMOD32		\
499    || r_type == R_MIPS_TLS_DTPMOD64		\
500    || r_type == R_MIPS_TLS_DTPREL32		\
501    || r_type == R_MIPS_TLS_DTPREL64		\
502    || r_type == R_MIPS_TLS_GD			\
503    || r_type == R_MIPS_TLS_LDM			\
504    || r_type == R_MIPS_TLS_DTPREL_HI16		\
505    || r_type == R_MIPS_TLS_DTPREL_LO16		\
506    || r_type == R_MIPS_TLS_GOTTPREL		\
507    || r_type == R_MIPS_TLS_TPREL32		\
508    || r_type == R_MIPS_TLS_TPREL64		\
509    || r_type == R_MIPS_TLS_TPREL_HI16		\
510    || r_type == R_MIPS_TLS_TPREL_LO16)
511 
512 /* Structure used to pass information to mips_elf_output_extsym.  */
513 
514 struct extsym_info
515 {
516   bfd *abfd;
517   struct bfd_link_info *info;
518   struct ecoff_debug_info *debug;
519   const struct ecoff_debug_swap *swap;
520   bfd_boolean failed;
521 };
522 
523 /* The names of the runtime procedure table symbols used on IRIX5.  */
524 
525 static const char * const mips_elf_dynsym_rtproc_names[] =
526 {
527   "_procedure_table",
528   "_procedure_string_table",
529   "_procedure_table_size",
530   NULL
531 };
532 
533 /* These structures are used to generate the .compact_rel section on
534    IRIX5.  */
535 
536 typedef struct
537 {
538   unsigned long id1;		/* Always one?  */
539   unsigned long num;		/* Number of compact relocation entries.  */
540   unsigned long id2;		/* Always two?  */
541   unsigned long offset;		/* The file offset of the first relocation.  */
542   unsigned long reserved0;	/* Zero?  */
543   unsigned long reserved1;	/* Zero?  */
544 } Elf32_compact_rel;
545 
546 typedef struct
547 {
548   bfd_byte id1[4];
549   bfd_byte num[4];
550   bfd_byte id2[4];
551   bfd_byte offset[4];
552   bfd_byte reserved0[4];
553   bfd_byte reserved1[4];
554 } Elf32_External_compact_rel;
555 
556 typedef struct
557 {
558   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
559   unsigned int rtype : 4;	/* Relocation types. See below.  */
560   unsigned int dist2to : 8;
561   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
562   unsigned long konst;		/* KONST field. See below.  */
563   unsigned long vaddr;		/* VADDR to be relocated.  */
564 } Elf32_crinfo;
565 
566 typedef struct
567 {
568   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
569   unsigned int rtype : 4;	/* Relocation types. See below.  */
570   unsigned int dist2to : 8;
571   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
572   unsigned long konst;		/* KONST field. See below.  */
573 } Elf32_crinfo2;
574 
575 typedef struct
576 {
577   bfd_byte info[4];
578   bfd_byte konst[4];
579   bfd_byte vaddr[4];
580 } Elf32_External_crinfo;
581 
582 typedef struct
583 {
584   bfd_byte info[4];
585   bfd_byte konst[4];
586 } Elf32_External_crinfo2;
587 
588 /* These are the constants used to swap the bitfields in a crinfo.  */
589 
590 #define CRINFO_CTYPE (0x1)
591 #define CRINFO_CTYPE_SH (31)
592 #define CRINFO_RTYPE (0xf)
593 #define CRINFO_RTYPE_SH (27)
594 #define CRINFO_DIST2TO (0xff)
595 #define CRINFO_DIST2TO_SH (19)
596 #define CRINFO_RELVADDR (0x7ffff)
597 #define CRINFO_RELVADDR_SH (0)
598 
599 /* A compact relocation info has long (3 words) or short (2 words)
600    formats.  A short format doesn't have VADDR field and relvaddr
601    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
602 #define CRF_MIPS_LONG			1
603 #define CRF_MIPS_SHORT			0
604 
605 /* There are 4 types of compact relocation at least. The value KONST
606    has different meaning for each type:
607 
608    (type)		(konst)
609    CT_MIPS_REL32	Address in data
610    CT_MIPS_WORD		Address in word (XXX)
611    CT_MIPS_GPHI_LO	GP - vaddr
612    CT_MIPS_JMPAD	Address to jump
613    */
614 
615 #define CRT_MIPS_REL32			0xa
616 #define CRT_MIPS_WORD			0xb
617 #define CRT_MIPS_GPHI_LO		0xc
618 #define CRT_MIPS_JMPAD			0xd
619 
620 #define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
621 #define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
622 #define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
623 #define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
624 
625 /* The structure of the runtime procedure descriptor created by the
626    loader for use by the static exception system.  */
627 
628 typedef struct runtime_pdr {
629 	bfd_vma	adr;		/* Memory address of start of procedure.  */
630 	long	regmask;	/* Save register mask.  */
631 	long	regoffset;	/* Save register offset.  */
632 	long	fregmask;	/* Save floating point register mask.  */
633 	long	fregoffset;	/* Save floating point register offset.  */
634 	long	frameoffset;	/* Frame size.  */
635 	short	framereg;	/* Frame pointer register.  */
636 	short	pcreg;		/* Offset or reg of return pc.  */
637 	long	irpss;		/* Index into the runtime string table.  */
638 	long	reserved;
639 	struct exception_info *exception_info;/* Pointer to exception array.  */
640 } RPDR, *pRPDR;
641 #define cbRPDR sizeof (RPDR)
642 #define rpdNil ((pRPDR) 0)
643 
644 static struct mips_got_entry *mips_elf_create_local_got_entry
645   (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
646    struct mips_elf_link_hash_entry *, int);
647 static bfd_boolean mips_elf_sort_hash_table_f
648   (struct mips_elf_link_hash_entry *, void *);
649 static bfd_vma mips_elf_high
650   (bfd_vma);
651 static bfd_boolean mips_elf_create_dynamic_relocation
652   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
653    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
654    bfd_vma *, asection *);
655 static hashval_t mips_elf_got_entry_hash
656   (const void *);
657 static bfd_vma mips_elf_adjust_gp
658   (bfd *, struct mips_got_info *, bfd *);
659 static struct mips_got_info *mips_elf_got_for_ibfd
660   (struct mips_got_info *, bfd *);
661 
662 /* This will be used when we sort the dynamic relocation records.  */
663 static bfd *reldyn_sorting_bfd;
664 
665 /* True if ABFD is a PIC object.  */
666 #define PIC_OBJECT_P(abfd) \
667   ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
668 
669 /* Nonzero if ABFD is using the N32 ABI.  */
670 #define ABI_N32_P(abfd) \
671   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
672 
673 /* Nonzero if ABFD is using the N64 ABI.  */
674 #define ABI_64_P(abfd) \
675   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
676 
677 /* Nonzero if ABFD is using NewABI conventions.  */
678 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
679 
680 /* The IRIX compatibility level we are striving for.  */
681 #define IRIX_COMPAT(abfd) \
682   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
683 
684 /* Whether we are trying to be compatible with IRIX at all.  */
685 #define SGI_COMPAT(abfd) \
686   (IRIX_COMPAT (abfd) != ict_none)
687 
688 /* The name of the options section.  */
689 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
690   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
691 
692 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
693    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
694 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
695   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
696 
697 /* Whether the section is readonly.  */
698 #define MIPS_ELF_READONLY_SECTION(sec) \
699   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
700    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
701 
702 /* The name of the stub section.  */
703 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
704 
705 /* The size of an external REL relocation.  */
706 #define MIPS_ELF_REL_SIZE(abfd) \
707   (get_elf_backend_data (abfd)->s->sizeof_rel)
708 
709 /* The size of an external RELA relocation.  */
710 #define MIPS_ELF_RELA_SIZE(abfd) \
711   (get_elf_backend_data (abfd)->s->sizeof_rela)
712 
713 /* The size of an external dynamic table entry.  */
714 #define MIPS_ELF_DYN_SIZE(abfd) \
715   (get_elf_backend_data (abfd)->s->sizeof_dyn)
716 
717 /* The size of a GOT entry.  */
718 #define MIPS_ELF_GOT_SIZE(abfd) \
719   (get_elf_backend_data (abfd)->s->arch_size / 8)
720 
721 /* The size of a symbol-table entry.  */
722 #define MIPS_ELF_SYM_SIZE(abfd) \
723   (get_elf_backend_data (abfd)->s->sizeof_sym)
724 
725 /* The default alignment for sections, as a power of two.  */
726 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
727   (get_elf_backend_data (abfd)->s->log_file_align)
728 
729 /* Get word-sized data.  */
730 #define MIPS_ELF_GET_WORD(abfd, ptr) \
731   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
732 
733 /* Put out word-sized data.  */
734 #define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
735   (ABI_64_P (abfd) 				\
736    ? bfd_put_64 (abfd, val, ptr) 		\
737    : bfd_put_32 (abfd, val, ptr))
738 
739 /* The opcode for word-sized loads (LW or LD).  */
740 #define MIPS_ELF_LOAD_WORD(abfd) \
741   (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
742 
743 /* Add a dynamic symbol table-entry.  */
744 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
745   _bfd_elf_add_dynamic_entry (info, tag, val)
746 
747 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
748   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
749 
750 /* Determine whether the internal relocation of index REL_IDX is REL
751    (zero) or RELA (non-zero).  The assumption is that, if there are
752    two relocation sections for this section, one of them is REL and
753    the other is RELA.  If the index of the relocation we're testing is
754    in range for the first relocation section, check that the external
755    relocation size is that for RELA.  It is also assumed that, if
756    rel_idx is not in range for the first section, and this first
757    section contains REL relocs, then the relocation is in the second
758    section, that is RELA.  */
759 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx)				\
760   ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr)			\
761     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel		\
762     > (bfd_vma)(rel_idx))						\
763    == (elf_section_data (sec)->rel_hdr.sh_entsize			\
764        == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela)		\
765 	   : sizeof (Elf32_External_Rela))))
766 
767 /* The name of the dynamic relocation section.  */
768 #define MIPS_ELF_REL_DYN_NAME(INFO) \
769   (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
770 
771 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
772    from smaller values.  Start with zero, widen, *then* decrement.  */
773 #define MINUS_ONE	(((bfd_vma)0) - 1)
774 #define MINUS_TWO	(((bfd_vma)0) - 2)
775 
776 /* The value to write into got[1] for SVR4 targets, to identify it is
777    a GNU object.  The dynamic linker can then use got[1] to store the
778    module pointer.  */
779 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
780   ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
781 
782 /* The offset of $gp from the beginning of the .got section.  */
783 #define ELF_MIPS_GP_OFFSET(INFO) \
784   (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
785 
786 /* The maximum size of the GOT for it to be addressable using 16-bit
787    offsets from $gp.  */
788 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
789 
790 /* Instructions which appear in a stub.  */
791 #define STUB_LW(abfd)							\
792   ((ABI_64_P (abfd)							\
793     ? 0xdf998010				/* ld t9,0x8010(gp) */	\
794     : 0x8f998010))              		/* lw t9,0x8010(gp) */
795 #define STUB_MOVE(abfd)							\
796    ((ABI_64_P (abfd)							\
797      ? 0x03e0782d				/* daddu t7,ra */	\
798      : 0x03e07821))				/* addu t7,ra */
799 #define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
800 #define STUB_JALR 0x0320f809			/* jalr t9,ra */
801 #define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
802 #define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
803 #define STUB_LI16S(abfd, VAL)						\
804    ((ABI_64_P (abfd)							\
805     ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
806     : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
807 
808 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
809 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
810 
811 /* The name of the dynamic interpreter.  This is put in the .interp
812    section.  */
813 
814 #define ELF_DYNAMIC_INTERPRETER(abfd) 		\
815    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" 	\
816     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" 	\
817     : "/usr/lib/libc.so.1")
818 
819 #ifdef BFD64
820 #define MNAME(bfd,pre,pos) \
821   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
822 #define ELF_R_SYM(bfd, i)					\
823   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
824 #define ELF_R_TYPE(bfd, i)					\
825   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
826 #define ELF_R_INFO(bfd, s, t)					\
827   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
828 #else
829 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
830 #define ELF_R_SYM(bfd, i)					\
831   (ELF32_R_SYM (i))
832 #define ELF_R_TYPE(bfd, i)					\
833   (ELF32_R_TYPE (i))
834 #define ELF_R_INFO(bfd, s, t)					\
835   (ELF32_R_INFO (s, t))
836 #endif
837 
838   /* The mips16 compiler uses a couple of special sections to handle
839      floating point arguments.
840 
841      Section names that look like .mips16.fn.FNNAME contain stubs that
842      copy floating point arguments from the fp regs to the gp regs and
843      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
844      call should be redirected to the stub instead.  If no 32 bit
845      function calls FNNAME, the stub should be discarded.  We need to
846      consider any reference to the function, not just a call, because
847      if the address of the function is taken we will need the stub,
848      since the address might be passed to a 32 bit function.
849 
850      Section names that look like .mips16.call.FNNAME contain stubs
851      that copy floating point arguments from the gp regs to the fp
852      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
853      then any 16 bit function that calls FNNAME should be redirected
854      to the stub instead.  If FNNAME is not a 32 bit function, the
855      stub should be discarded.
856 
857      .mips16.call.fp.FNNAME sections are similar, but contain stubs
858      which call FNNAME and then copy the return value from the fp regs
859      to the gp regs.  These stubs store the return value in $18 while
860      calling FNNAME; any function which might call one of these stubs
861      must arrange to save $18 around the call.  (This case is not
862      needed for 32 bit functions that call 16 bit functions, because
863      16 bit functions always return floating point values in both
864      $f0/$f1 and $2/$3.)
865 
866      Note that in all cases FNNAME might be defined statically.
867      Therefore, FNNAME is not used literally.  Instead, the relocation
868      information will indicate which symbol the section is for.
869 
870      We record any stubs that we find in the symbol table.  */
871 
872 #define FN_STUB ".mips16.fn."
873 #define CALL_STUB ".mips16.call."
874 #define CALL_FP_STUB ".mips16.call.fp."
875 
876 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
877 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
878 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
879 
880 /* The format of the first PLT entry in an O32 executable.  */
881 static const bfd_vma mips_o32_exec_plt0_entry[] = {
882   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
883   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
884   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
885   0x031cc023,	/* subu $24, $24, $28					*/
886   0x03e07821,	/* move $15, $31					*/
887   0x0018c082,	/* srl $24, $24, 2					*/
888   0x0320f809,	/* jalr $25						*/
889   0x2718fffe	/* subu $24, $24, 2					*/
890 };
891 
892 /* The format of the first PLT entry in an N32 executable.  Different
893    because gp ($28) is not available; we use t2 ($14) instead.  */
894 static const bfd_vma mips_n32_exec_plt0_entry[] = {
895   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
896   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
897   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
898   0x030ec023,	/* subu $24, $24, $14					*/
899   0x03e07821,	/* move $15, $31					*/
900   0x0018c082,	/* srl $24, $24, 2					*/
901   0x0320f809,	/* jalr $25						*/
902   0x2718fffe	/* subu $24, $24, 2					*/
903 };
904 
905 /* The format of the first PLT entry in an N64 executable.  Different
906    from N32 because of the increased size of GOT entries.  */
907 static const bfd_vma mips_n64_exec_plt0_entry[] = {
908   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
909   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
910   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
911   0x030ec023,	/* subu $24, $24, $14					*/
912   0x03e07821,	/* move $15, $31					*/
913   0x0018c0c2,	/* srl $24, $24, 3					*/
914   0x0320f809,	/* jalr $25						*/
915   0x2718fffe	/* subu $24, $24, 2					*/
916 };
917 
918 /* The format of subsequent PLT entries.  */
919 static const bfd_vma mips_exec_plt_entry[] = {
920   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
921   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
922   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
923   0x03200008	/* jr $25					*/
924 };
925 
926 /* The format of the first PLT entry in a VxWorks executable.  */
927 static const bfd_vma mips_vxworks_exec_plt0_entry[] = {
928   0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
929   0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
930   0x8f390008,	/* lw t9, 8(t9)					*/
931   0x00000000,	/* nop						*/
932   0x03200008,	/* jr t9					*/
933   0x00000000	/* nop						*/
934 };
935 
936 /* The format of subsequent PLT entries.  */
937 static const bfd_vma mips_vxworks_exec_plt_entry[] = {
938   0x10000000,	/* b .PLT_resolver			*/
939   0x24180000,	/* li t8, <pltindex>			*/
940   0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
941   0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
942   0x8f390000,	/* lw t9, 0(t9)				*/
943   0x00000000,	/* nop					*/
944   0x03200008,	/* jr t9				*/
945   0x00000000	/* nop					*/
946 };
947 
948 /* The format of the first PLT entry in a VxWorks shared object.  */
949 static const bfd_vma mips_vxworks_shared_plt0_entry[] = {
950   0x8f990008,	/* lw t9, 8(gp)		*/
951   0x00000000,	/* nop			*/
952   0x03200008,	/* jr t9		*/
953   0x00000000,	/* nop			*/
954   0x00000000,	/* nop			*/
955   0x00000000	/* nop			*/
956 };
957 
958 /* The format of subsequent PLT entries.  */
959 static const bfd_vma mips_vxworks_shared_plt_entry[] = {
960   0x10000000,	/* b .PLT_resolver	*/
961   0x24180000	/* li t8, <pltindex>	*/
962 };
963 
964 /* Look up an entry in a MIPS ELF linker hash table.  */
965 
966 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
967   ((struct mips_elf_link_hash_entry *)					\
968    elf_link_hash_lookup (&(table)->root, (string), (create),		\
969 			 (copy), (follow)))
970 
971 /* Traverse a MIPS ELF linker hash table.  */
972 
973 #define mips_elf_link_hash_traverse(table, func, info)			\
974   (elf_link_hash_traverse						\
975    (&(table)->root,							\
976     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
977     (info)))
978 
979 /* Get the MIPS ELF linker hash table from a link_info structure.  */
980 
981 #define mips_elf_hash_table(p) \
982   ((struct mips_elf_link_hash_table *) ((p)->hash))
983 
984 /* Find the base offsets for thread-local storage in this object,
985    for GD/LD and IE/LE respectively.  */
986 
987 #define TP_OFFSET 0x7000
988 #define DTP_OFFSET 0x8000
989 
990 static bfd_vma
991 dtprel_base (struct bfd_link_info *info)
992 {
993   /* If tls_sec is NULL, we should have signalled an error already.  */
994   if (elf_hash_table (info)->tls_sec == NULL)
995     return 0;
996   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
997 }
998 
999 static bfd_vma
1000 tprel_base (struct bfd_link_info *info)
1001 {
1002   /* If tls_sec is NULL, we should have signalled an error already.  */
1003   if (elf_hash_table (info)->tls_sec == NULL)
1004     return 0;
1005   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1006 }
1007 
1008 /* Create an entry in a MIPS ELF linker hash table.  */
1009 
1010 static struct bfd_hash_entry *
1011 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1012 			    struct bfd_hash_table *table, const char *string)
1013 {
1014   struct mips_elf_link_hash_entry *ret =
1015     (struct mips_elf_link_hash_entry *) entry;
1016 
1017   /* Allocate the structure if it has not already been allocated by a
1018      subclass.  */
1019   if (ret == NULL)
1020     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1021   if (ret == NULL)
1022     return (struct bfd_hash_entry *) ret;
1023 
1024   /* Call the allocation method of the superclass.  */
1025   ret = ((struct mips_elf_link_hash_entry *)
1026 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1027 				     table, string));
1028   if (ret != NULL)
1029     {
1030       /* Set local fields.  */
1031       memset (&ret->esym, 0, sizeof (EXTR));
1032       /* We use -2 as a marker to indicate that the information has
1033 	 not been set.  -1 means there is no associated ifd.  */
1034       ret->esym.ifd = -2;
1035       ret->la25_stub = 0;
1036       ret->possibly_dynamic_relocs = 0;
1037       ret->fn_stub = NULL;
1038       ret->call_stub = NULL;
1039       ret->call_fp_stub = NULL;
1040       ret->tls_type = GOT_NORMAL;
1041       ret->global_got_area = GGA_NONE;
1042       ret->readonly_reloc = FALSE;
1043       ret->has_static_relocs = FALSE;
1044       ret->no_fn_stub = FALSE;
1045       ret->need_fn_stub = FALSE;
1046       ret->has_nonpic_branches = FALSE;
1047       ret->needs_lazy_stub = FALSE;
1048     }
1049 
1050   return (struct bfd_hash_entry *) ret;
1051 }
1052 
1053 bfd_boolean
1054 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1055 {
1056   if (!sec->used_by_bfd)
1057     {
1058       struct _mips_elf_section_data *sdata;
1059       bfd_size_type amt = sizeof (*sdata);
1060 
1061       sdata = bfd_zalloc (abfd, amt);
1062       if (sdata == NULL)
1063 	return FALSE;
1064       sec->used_by_bfd = sdata;
1065     }
1066 
1067   return _bfd_elf_new_section_hook (abfd, sec);
1068 }
1069 
1070 /* Read ECOFF debugging information from a .mdebug section into a
1071    ecoff_debug_info structure.  */
1072 
1073 bfd_boolean
1074 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1075 			       struct ecoff_debug_info *debug)
1076 {
1077   HDRR *symhdr;
1078   const struct ecoff_debug_swap *swap;
1079   char *ext_hdr;
1080 
1081   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1082   memset (debug, 0, sizeof (*debug));
1083 
1084   ext_hdr = bfd_malloc (swap->external_hdr_size);
1085   if (ext_hdr == NULL && swap->external_hdr_size != 0)
1086     goto error_return;
1087 
1088   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1089 				  swap->external_hdr_size))
1090     goto error_return;
1091 
1092   symhdr = &debug->symbolic_header;
1093   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1094 
1095   /* The symbolic header contains absolute file offsets and sizes to
1096      read.  */
1097 #define READ(ptr, offset, count, size, type)				\
1098   if (symhdr->count == 0)						\
1099     debug->ptr = NULL;							\
1100   else									\
1101     {									\
1102       bfd_size_type amt = (bfd_size_type) size * symhdr->count;		\
1103       debug->ptr = bfd_malloc (amt);					\
1104       if (debug->ptr == NULL)						\
1105 	goto error_return;						\
1106       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0		\
1107 	  || bfd_bread (debug->ptr, amt, abfd) != amt)			\
1108 	goto error_return;						\
1109     }
1110 
1111   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1112   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1113   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1114   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1115   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1116   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1117 	union aux_ext *);
1118   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1119   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1120   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1121   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1122   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1123 #undef READ
1124 
1125   debug->fdr = NULL;
1126 
1127   return TRUE;
1128 
1129  error_return:
1130   if (ext_hdr != NULL)
1131     free (ext_hdr);
1132   if (debug->line != NULL)
1133     free (debug->line);
1134   if (debug->external_dnr != NULL)
1135     free (debug->external_dnr);
1136   if (debug->external_pdr != NULL)
1137     free (debug->external_pdr);
1138   if (debug->external_sym != NULL)
1139     free (debug->external_sym);
1140   if (debug->external_opt != NULL)
1141     free (debug->external_opt);
1142   if (debug->external_aux != NULL)
1143     free (debug->external_aux);
1144   if (debug->ss != NULL)
1145     free (debug->ss);
1146   if (debug->ssext != NULL)
1147     free (debug->ssext);
1148   if (debug->external_fdr != NULL)
1149     free (debug->external_fdr);
1150   if (debug->external_rfd != NULL)
1151     free (debug->external_rfd);
1152   if (debug->external_ext != NULL)
1153     free (debug->external_ext);
1154   return FALSE;
1155 }
1156 
1157 /* Swap RPDR (runtime procedure table entry) for output.  */
1158 
1159 static void
1160 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1161 {
1162   H_PUT_S32 (abfd, in->adr, ex->p_adr);
1163   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1164   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1165   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1166   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1167   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1168 
1169   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1170   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1171 
1172   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1173 }
1174 
1175 /* Create a runtime procedure table from the .mdebug section.  */
1176 
1177 static bfd_boolean
1178 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1179 				 struct bfd_link_info *info, asection *s,
1180 				 struct ecoff_debug_info *debug)
1181 {
1182   const struct ecoff_debug_swap *swap;
1183   HDRR *hdr = &debug->symbolic_header;
1184   RPDR *rpdr, *rp;
1185   struct rpdr_ext *erp;
1186   void *rtproc;
1187   struct pdr_ext *epdr;
1188   struct sym_ext *esym;
1189   char *ss, **sv;
1190   char *str;
1191   bfd_size_type size;
1192   bfd_size_type count;
1193   unsigned long sindex;
1194   unsigned long i;
1195   PDR pdr;
1196   SYMR sym;
1197   const char *no_name_func = _("static procedure (no name)");
1198 
1199   epdr = NULL;
1200   rpdr = NULL;
1201   esym = NULL;
1202   ss = NULL;
1203   sv = NULL;
1204 
1205   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1206 
1207   sindex = strlen (no_name_func) + 1;
1208   count = hdr->ipdMax;
1209   if (count > 0)
1210     {
1211       size = swap->external_pdr_size;
1212 
1213       epdr = bfd_malloc (size * count);
1214       if (epdr == NULL)
1215 	goto error_return;
1216 
1217       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1218 	goto error_return;
1219 
1220       size = sizeof (RPDR);
1221       rp = rpdr = bfd_malloc (size * count);
1222       if (rpdr == NULL)
1223 	goto error_return;
1224 
1225       size = sizeof (char *);
1226       sv = bfd_malloc (size * count);
1227       if (sv == NULL)
1228 	goto error_return;
1229 
1230       count = hdr->isymMax;
1231       size = swap->external_sym_size;
1232       esym = bfd_malloc (size * count);
1233       if (esym == NULL)
1234 	goto error_return;
1235 
1236       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1237 	goto error_return;
1238 
1239       count = hdr->issMax;
1240       ss = bfd_malloc (count);
1241       if (ss == NULL)
1242 	goto error_return;
1243       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1244 	goto error_return;
1245 
1246       count = hdr->ipdMax;
1247       for (i = 0; i < (unsigned long) count; i++, rp++)
1248 	{
1249 	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1250 	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1251 	  rp->adr = sym.value;
1252 	  rp->regmask = pdr.regmask;
1253 	  rp->regoffset = pdr.regoffset;
1254 	  rp->fregmask = pdr.fregmask;
1255 	  rp->fregoffset = pdr.fregoffset;
1256 	  rp->frameoffset = pdr.frameoffset;
1257 	  rp->framereg = pdr.framereg;
1258 	  rp->pcreg = pdr.pcreg;
1259 	  rp->irpss = sindex;
1260 	  sv[i] = ss + sym.iss;
1261 	  sindex += strlen (sv[i]) + 1;
1262 	}
1263     }
1264 
1265   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1266   size = BFD_ALIGN (size, 16);
1267   rtproc = bfd_alloc (abfd, size);
1268   if (rtproc == NULL)
1269     {
1270       mips_elf_hash_table (info)->procedure_count = 0;
1271       goto error_return;
1272     }
1273 
1274   mips_elf_hash_table (info)->procedure_count = count + 2;
1275 
1276   erp = rtproc;
1277   memset (erp, 0, sizeof (struct rpdr_ext));
1278   erp++;
1279   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1280   strcpy (str, no_name_func);
1281   str += strlen (no_name_func) + 1;
1282   for (i = 0; i < count; i++)
1283     {
1284       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1285       strcpy (str, sv[i]);
1286       str += strlen (sv[i]) + 1;
1287     }
1288   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1289 
1290   /* Set the size and contents of .rtproc section.  */
1291   s->size = size;
1292   s->contents = rtproc;
1293 
1294   /* Skip this section later on (I don't think this currently
1295      matters, but someday it might).  */
1296   s->map_head.link_order = NULL;
1297 
1298   if (epdr != NULL)
1299     free (epdr);
1300   if (rpdr != NULL)
1301     free (rpdr);
1302   if (esym != NULL)
1303     free (esym);
1304   if (ss != NULL)
1305     free (ss);
1306   if (sv != NULL)
1307     free (sv);
1308 
1309   return TRUE;
1310 
1311  error_return:
1312   if (epdr != NULL)
1313     free (epdr);
1314   if (rpdr != NULL)
1315     free (rpdr);
1316   if (esym != NULL)
1317     free (esym);
1318   if (ss != NULL)
1319     free (ss);
1320   if (sv != NULL)
1321     free (sv);
1322   return FALSE;
1323 }
1324 
1325 /* We're going to create a stub for H.  Create a symbol for the stub's
1326    value and size, to help make the disassembly easier to read.  */
1327 
1328 static bfd_boolean
1329 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1330 			     struct mips_elf_link_hash_entry *h,
1331 			     const char *prefix, asection *s, bfd_vma value,
1332 			     bfd_vma size)
1333 {
1334   struct bfd_link_hash_entry *bh;
1335   struct elf_link_hash_entry *elfh;
1336   const char *name;
1337 
1338   /* Create a new symbol.  */
1339   name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1340   bh = NULL;
1341   if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1342 					 BSF_LOCAL, s, value, NULL,
1343 					 TRUE, FALSE, &bh))
1344     return FALSE;
1345 
1346   /* Make it a local function.  */
1347   elfh = (struct elf_link_hash_entry *) bh;
1348   elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1349   elfh->size = size;
1350   elfh->forced_local = 1;
1351   return TRUE;
1352 }
1353 
1354 /* We're about to redefine H.  Create a symbol to represent H's
1355    current value and size, to help make the disassembly easier
1356    to read.  */
1357 
1358 static bfd_boolean
1359 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1360 			       struct mips_elf_link_hash_entry *h,
1361 			       const char *prefix)
1362 {
1363   struct bfd_link_hash_entry *bh;
1364   struct elf_link_hash_entry *elfh;
1365   const char *name;
1366   asection *s;
1367   bfd_vma value;
1368 
1369   /* Read the symbol's value.  */
1370   BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1371 	      || h->root.root.type == bfd_link_hash_defweak);
1372   s = h->root.root.u.def.section;
1373   value = h->root.root.u.def.value;
1374 
1375   /* Create a new symbol.  */
1376   name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1377   bh = NULL;
1378   if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1379 					 BSF_LOCAL, s, value, NULL,
1380 					 TRUE, FALSE, &bh))
1381     return FALSE;
1382 
1383   /* Make it local and copy the other attributes from H.  */
1384   elfh = (struct elf_link_hash_entry *) bh;
1385   elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1386   elfh->other = h->root.other;
1387   elfh->size = h->root.size;
1388   elfh->forced_local = 1;
1389   return TRUE;
1390 }
1391 
1392 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1393    function rather than to a hard-float stub.  */
1394 
1395 static bfd_boolean
1396 section_allows_mips16_refs_p (asection *section)
1397 {
1398   const char *name;
1399 
1400   name = bfd_get_section_name (section->owner, section);
1401   return (FN_STUB_P (name)
1402 	  || CALL_STUB_P (name)
1403 	  || CALL_FP_STUB_P (name)
1404 	  || strcmp (name, ".pdr") == 0);
1405 }
1406 
1407 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1408    stub section of some kind.  Return the R_SYMNDX of the target
1409    function, or 0 if we can't decide which function that is.  */
1410 
1411 static unsigned long
1412 mips16_stub_symndx (asection *sec, const Elf_Internal_Rela *relocs,
1413 		    const Elf_Internal_Rela *relend)
1414 {
1415   const Elf_Internal_Rela *rel;
1416 
1417   /* Trust the first R_MIPS_NONE relocation, if any.  */
1418   for (rel = relocs; rel < relend; rel++)
1419     if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1420       return ELF_R_SYM (sec->owner, rel->r_info);
1421 
1422   /* Otherwise trust the first relocation, whatever its kind.  This is
1423      the traditional behavior.  */
1424   if (relocs < relend)
1425     return ELF_R_SYM (sec->owner, relocs->r_info);
1426 
1427   return 0;
1428 }
1429 
1430 /* Check the mips16 stubs for a particular symbol, and see if we can
1431    discard them.  */
1432 
1433 static void
1434 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1435 			     struct mips_elf_link_hash_entry *h)
1436 {
1437   /* Dynamic symbols must use the standard call interface, in case other
1438      objects try to call them.  */
1439   if (h->fn_stub != NULL
1440       && h->root.dynindx != -1)
1441     {
1442       mips_elf_create_shadow_symbol (info, h, ".mips16.");
1443       h->need_fn_stub = TRUE;
1444     }
1445 
1446   if (h->fn_stub != NULL
1447       && ! h->need_fn_stub)
1448     {
1449       /* We don't need the fn_stub; the only references to this symbol
1450          are 16 bit calls.  Clobber the size to 0 to prevent it from
1451          being included in the link.  */
1452       h->fn_stub->size = 0;
1453       h->fn_stub->flags &= ~SEC_RELOC;
1454       h->fn_stub->reloc_count = 0;
1455       h->fn_stub->flags |= SEC_EXCLUDE;
1456     }
1457 
1458   if (h->call_stub != NULL
1459       && ELF_ST_IS_MIPS16 (h->root.other))
1460     {
1461       /* We don't need the call_stub; this is a 16 bit function, so
1462          calls from other 16 bit functions are OK.  Clobber the size
1463          to 0 to prevent it from being included in the link.  */
1464       h->call_stub->size = 0;
1465       h->call_stub->flags &= ~SEC_RELOC;
1466       h->call_stub->reloc_count = 0;
1467       h->call_stub->flags |= SEC_EXCLUDE;
1468     }
1469 
1470   if (h->call_fp_stub != NULL
1471       && ELF_ST_IS_MIPS16 (h->root.other))
1472     {
1473       /* We don't need the call_stub; this is a 16 bit function, so
1474          calls from other 16 bit functions are OK.  Clobber the size
1475          to 0 to prevent it from being included in the link.  */
1476       h->call_fp_stub->size = 0;
1477       h->call_fp_stub->flags &= ~SEC_RELOC;
1478       h->call_fp_stub->reloc_count = 0;
1479       h->call_fp_stub->flags |= SEC_EXCLUDE;
1480     }
1481 }
1482 
1483 /* Hashtable callbacks for mips_elf_la25_stubs.  */
1484 
1485 static hashval_t
1486 mips_elf_la25_stub_hash (const void *entry_)
1487 {
1488   const struct mips_elf_la25_stub *entry;
1489 
1490   entry = (struct mips_elf_la25_stub *) entry_;
1491   return entry->h->root.root.u.def.section->id
1492     + entry->h->root.root.u.def.value;
1493 }
1494 
1495 static int
1496 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1497 {
1498   const struct mips_elf_la25_stub *entry1, *entry2;
1499 
1500   entry1 = (struct mips_elf_la25_stub *) entry1_;
1501   entry2 = (struct mips_elf_la25_stub *) entry2_;
1502   return ((entry1->h->root.root.u.def.section
1503 	   == entry2->h->root.root.u.def.section)
1504 	  && (entry1->h->root.root.u.def.value
1505 	      == entry2->h->root.root.u.def.value));
1506 }
1507 
1508 /* Called by the linker to set up the la25 stub-creation code.  FN is
1509    the linker's implementation of add_stub_function.  Return true on
1510    success.  */
1511 
1512 bfd_boolean
1513 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1514 			  asection *(*fn) (const char *, asection *,
1515 					   asection *))
1516 {
1517   struct mips_elf_link_hash_table *htab;
1518 
1519   htab = mips_elf_hash_table (info);
1520   htab->add_stub_section = fn;
1521   htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1522 				      mips_elf_la25_stub_eq, NULL);
1523   if (htab->la25_stubs == NULL)
1524     return FALSE;
1525 
1526   return TRUE;
1527 }
1528 
1529 /* Return true if H is a locally-defined PIC function, in the sense
1530    that it might need $25 to be valid on entry.  Note that MIPS16
1531    functions never need $25 to be valid on entry; they set up $gp
1532    using PC-relative instructions instead.  */
1533 
1534 static bfd_boolean
1535 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1536 {
1537   return ((h->root.root.type == bfd_link_hash_defined
1538 	   || h->root.root.type == bfd_link_hash_defweak)
1539 	  && h->root.def_regular
1540 	  && !bfd_is_abs_section (h->root.root.u.def.section)
1541 	  && !ELF_ST_IS_MIPS16 (h->root.other)
1542 	  && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1543 	      || ELF_ST_IS_MIPS_PIC (h->root.other)));
1544 }
1545 
1546 /* STUB describes an la25 stub that we have decided to implement
1547    by inserting an LUI/ADDIU pair before the target function.
1548    Create the section and redirect the function symbol to it.  */
1549 
1550 static bfd_boolean
1551 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1552 			 struct bfd_link_info *info)
1553 {
1554   struct mips_elf_link_hash_table *htab;
1555   char *name;
1556   asection *s, *input_section;
1557   unsigned int align;
1558 
1559   htab = mips_elf_hash_table (info);
1560 
1561   /* Create a unique name for the new section.  */
1562   name = bfd_malloc (11 + sizeof (".text.stub."));
1563   if (name == NULL)
1564     return FALSE;
1565   sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1566 
1567   /* Create the section.  */
1568   input_section = stub->h->root.root.u.def.section;
1569   s = htab->add_stub_section (name, input_section,
1570 			      input_section->output_section);
1571   if (s == NULL)
1572     return FALSE;
1573 
1574   /* Make sure that any padding goes before the stub.  */
1575   align = input_section->alignment_power;
1576   if (!bfd_set_section_alignment (s->owner, s, align))
1577     return FALSE;
1578   if (align > 3)
1579     s->size = (1 << align) - 8;
1580 
1581   /* Create a symbol for the stub.  */
1582   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1583   stub->stub_section = s;
1584   stub->offset = s->size;
1585 
1586   /* Allocate room for it.  */
1587   s->size += 8;
1588   return TRUE;
1589 }
1590 
1591 /* STUB describes an la25 stub that we have decided to implement
1592    with a separate trampoline.  Allocate room for it and redirect
1593    the function symbol to it.  */
1594 
1595 static bfd_boolean
1596 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1597 			      struct bfd_link_info *info)
1598 {
1599   struct mips_elf_link_hash_table *htab;
1600   asection *s;
1601 
1602   htab = mips_elf_hash_table (info);
1603 
1604   /* Create a trampoline section, if we haven't already.  */
1605   s = htab->strampoline;
1606   if (s == NULL)
1607     {
1608       asection *input_section = stub->h->root.root.u.def.section;
1609       s = htab->add_stub_section (".text", NULL,
1610 				  input_section->output_section);
1611       if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1612 	return FALSE;
1613       htab->strampoline = s;
1614     }
1615 
1616   /* Create a symbol for the stub.  */
1617   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1618   stub->stub_section = s;
1619   stub->offset = s->size;
1620 
1621   /* Allocate room for it.  */
1622   s->size += 16;
1623   return TRUE;
1624 }
1625 
1626 /* H describes a symbol that needs an la25 stub.  Make sure that an
1627    appropriate stub exists and point H at it.  */
1628 
1629 static bfd_boolean
1630 mips_elf_add_la25_stub (struct bfd_link_info *info,
1631 			struct mips_elf_link_hash_entry *h)
1632 {
1633   struct mips_elf_link_hash_table *htab;
1634   struct mips_elf_la25_stub search, *stub;
1635   bfd_boolean use_trampoline_p;
1636   asection *s;
1637   bfd_vma value;
1638   void **slot;
1639 
1640   /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1641      of the section and if we would need no more than 2 nops.  */
1642   s = h->root.root.u.def.section;
1643   value = h->root.root.u.def.value;
1644   use_trampoline_p = (value != 0 || s->alignment_power > 4);
1645 
1646   /* Describe the stub we want.  */
1647   search.stub_section = NULL;
1648   search.offset = 0;
1649   search.h = h;
1650 
1651   /* See if we've already created an equivalent stub.  */
1652   htab = mips_elf_hash_table (info);
1653   slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1654   if (slot == NULL)
1655     return FALSE;
1656 
1657   stub = (struct mips_elf_la25_stub *) *slot;
1658   if (stub != NULL)
1659     {
1660       /* We can reuse the existing stub.  */
1661       h->la25_stub = stub;
1662       return TRUE;
1663     }
1664 
1665   /* Create a permanent copy of ENTRY and add it to the hash table.  */
1666   stub = bfd_malloc (sizeof (search));
1667   if (stub == NULL)
1668     return FALSE;
1669   *stub = search;
1670   *slot = stub;
1671 
1672   h->la25_stub = stub;
1673   return (use_trampoline_p
1674 	  ? mips_elf_add_la25_trampoline (stub, info)
1675 	  : mips_elf_add_la25_intro (stub, info));
1676 }
1677 
1678 /* A mips_elf_link_hash_traverse callback that is called before sizing
1679    sections.  DATA points to a mips_htab_traverse_info structure.  */
1680 
1681 static bfd_boolean
1682 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1683 {
1684   struct mips_htab_traverse_info *hti;
1685 
1686   hti = (struct mips_htab_traverse_info *) data;
1687   if (h->root.root.type == bfd_link_hash_warning)
1688     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1689 
1690   if (!hti->info->relocatable)
1691     mips_elf_check_mips16_stubs (hti->info, h);
1692 
1693   if (mips_elf_local_pic_function_p (h))
1694     {
1695       /* H is a function that might need $25 to be valid on entry.
1696 	 If we're creating a non-PIC relocatable object, mark H as
1697 	 being PIC.  If we're creating a non-relocatable object with
1698 	 non-PIC branches and jumps to H, make sure that H has an la25
1699 	 stub.  */
1700       if (hti->info->relocatable)
1701 	{
1702 	  if (!PIC_OBJECT_P (hti->output_bfd))
1703 	    h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1704 	}
1705       else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1706 	{
1707 	  hti->error = TRUE;
1708 	  return FALSE;
1709 	}
1710     }
1711   return TRUE;
1712 }
1713 
1714 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1715    Most mips16 instructions are 16 bits, but these instructions
1716    are 32 bits.
1717 
1718    The format of these instructions is:
1719 
1720    +--------------+--------------------------------+
1721    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
1722    +--------------+--------------------------------+
1723    |                Immediate  15:0                |
1724    +-----------------------------------------------+
1725 
1726    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
1727    Note that the immediate value in the first word is swapped.
1728 
1729    When producing a relocatable object file, R_MIPS16_26 is
1730    handled mostly like R_MIPS_26.  In particular, the addend is
1731    stored as a straight 26-bit value in a 32-bit instruction.
1732    (gas makes life simpler for itself by never adjusting a
1733    R_MIPS16_26 reloc to be against a section, so the addend is
1734    always zero).  However, the 32 bit instruction is stored as 2
1735    16-bit values, rather than a single 32-bit value.  In a
1736    big-endian file, the result is the same; in a little-endian
1737    file, the two 16-bit halves of the 32 bit value are swapped.
1738    This is so that a disassembler can recognize the jal
1739    instruction.
1740 
1741    When doing a final link, R_MIPS16_26 is treated as a 32 bit
1742    instruction stored as two 16-bit values.  The addend A is the
1743    contents of the targ26 field.  The calculation is the same as
1744    R_MIPS_26.  When storing the calculated value, reorder the
1745    immediate value as shown above, and don't forget to store the
1746    value as two 16-bit values.
1747 
1748    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1749    defined as
1750 
1751    big-endian:
1752    +--------+----------------------+
1753    |        |                      |
1754    |        |    targ26-16         |
1755    |31    26|25                   0|
1756    +--------+----------------------+
1757 
1758    little-endian:
1759    +----------+------+-------------+
1760    |          |      |             |
1761    |  sub1    |      |     sub2    |
1762    |0        9|10  15|16         31|
1763    +----------+--------------------+
1764    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1765    ((sub1 << 16) | sub2)).
1766 
1767    When producing a relocatable object file, the calculation is
1768    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1769    When producing a fully linked file, the calculation is
1770    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1771    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1772 
1773    The table below lists the other MIPS16 instruction relocations.
1774    Each one is calculated in the same way as the non-MIPS16 relocation
1775    given on the right, but using the extended MIPS16 layout of 16-bit
1776    immediate fields:
1777 
1778 	R_MIPS16_GPREL		R_MIPS_GPREL16
1779 	R_MIPS16_GOT16		R_MIPS_GOT16
1780 	R_MIPS16_CALL16		R_MIPS_CALL16
1781 	R_MIPS16_HI16		R_MIPS_HI16
1782 	R_MIPS16_LO16		R_MIPS_LO16
1783 
1784    A typical instruction will have a format like this:
1785 
1786    +--------------+--------------------------------+
1787    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
1788    +--------------+--------------------------------+
1789    |    Major     |   rx   |   ry   |   Imm  4:0   |
1790    +--------------+--------------------------------+
1791 
1792    EXTEND is the five bit value 11110.  Major is the instruction
1793    opcode.
1794 
1795    All we need to do here is shuffle the bits appropriately.
1796    As above, the two 16-bit halves must be swapped on a
1797    little-endian system.  */
1798 
1799 static inline bfd_boolean
1800 mips16_reloc_p (int r_type)
1801 {
1802   switch (r_type)
1803     {
1804     case R_MIPS16_26:
1805     case R_MIPS16_GPREL:
1806     case R_MIPS16_GOT16:
1807     case R_MIPS16_CALL16:
1808     case R_MIPS16_HI16:
1809     case R_MIPS16_LO16:
1810       return TRUE;
1811 
1812     default:
1813       return FALSE;
1814     }
1815 }
1816 
1817 static inline bfd_boolean
1818 got16_reloc_p (int r_type)
1819 {
1820   return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1821 }
1822 
1823 static inline bfd_boolean
1824 call16_reloc_p (int r_type)
1825 {
1826   return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1827 }
1828 
1829 static inline bfd_boolean
1830 hi16_reloc_p (int r_type)
1831 {
1832   return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1833 }
1834 
1835 static inline bfd_boolean
1836 lo16_reloc_p (int r_type)
1837 {
1838   return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1839 }
1840 
1841 static inline bfd_boolean
1842 mips16_call_reloc_p (int r_type)
1843 {
1844   return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1845 }
1846 
1847 void
1848 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1849 				 bfd_boolean jal_shuffle, bfd_byte *data)
1850 {
1851   bfd_vma extend, insn, val;
1852 
1853   if (!mips16_reloc_p (r_type))
1854     return;
1855 
1856   /* Pick up the mips16 extend instruction and the real instruction.  */
1857   extend = bfd_get_16 (abfd, data);
1858   insn = bfd_get_16 (abfd, data + 2);
1859   if (r_type == R_MIPS16_26)
1860     {
1861       if (jal_shuffle)
1862 	val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1863 	      | ((extend & 0x1f) << 21) | insn;
1864       else
1865 	val = extend << 16 | insn;
1866     }
1867   else
1868     val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1869 	  | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1870   bfd_put_32 (abfd, val, data);
1871 }
1872 
1873 void
1874 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1875 			       bfd_boolean jal_shuffle, bfd_byte *data)
1876 {
1877   bfd_vma extend, insn, val;
1878 
1879   if (!mips16_reloc_p (r_type))
1880     return;
1881 
1882   val = bfd_get_32 (abfd, data);
1883   if (r_type == R_MIPS16_26)
1884     {
1885       if (jal_shuffle)
1886 	{
1887 	  insn = val & 0xffff;
1888 	  extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1889 		   | ((val >> 21) & 0x1f);
1890 	}
1891       else
1892 	{
1893 	  insn = val & 0xffff;
1894 	  extend = val >> 16;
1895 	}
1896     }
1897   else
1898     {
1899       insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1900       extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1901     }
1902   bfd_put_16 (abfd, insn, data + 2);
1903   bfd_put_16 (abfd, extend, data);
1904 }
1905 
1906 bfd_reloc_status_type
1907 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1908 			       arelent *reloc_entry, asection *input_section,
1909 			       bfd_boolean relocatable, void *data, bfd_vma gp)
1910 {
1911   bfd_vma relocation;
1912   bfd_signed_vma val;
1913   bfd_reloc_status_type status;
1914 
1915   if (bfd_is_com_section (symbol->section))
1916     relocation = 0;
1917   else
1918     relocation = symbol->value;
1919 
1920   relocation += symbol->section->output_section->vma;
1921   relocation += symbol->section->output_offset;
1922 
1923   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1924     return bfd_reloc_outofrange;
1925 
1926   /* Set val to the offset into the section or symbol.  */
1927   val = reloc_entry->addend;
1928 
1929   _bfd_mips_elf_sign_extend (val, 16);
1930 
1931   /* Adjust val for the final section location and GP value.  If we
1932      are producing relocatable output, we don't want to do this for
1933      an external symbol.  */
1934   if (! relocatable
1935       || (symbol->flags & BSF_SECTION_SYM) != 0)
1936     val += relocation - gp;
1937 
1938   if (reloc_entry->howto->partial_inplace)
1939     {
1940       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1941 				       (bfd_byte *) data
1942 				       + reloc_entry->address);
1943       if (status != bfd_reloc_ok)
1944 	return status;
1945     }
1946   else
1947     reloc_entry->addend = val;
1948 
1949   if (relocatable)
1950     reloc_entry->address += input_section->output_offset;
1951 
1952   return bfd_reloc_ok;
1953 }
1954 
1955 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1956    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
1957    that contains the relocation field and DATA points to the start of
1958    INPUT_SECTION.  */
1959 
1960 struct mips_hi16
1961 {
1962   struct mips_hi16 *next;
1963   bfd_byte *data;
1964   asection *input_section;
1965   arelent rel;
1966 };
1967 
1968 /* FIXME: This should not be a static variable.  */
1969 
1970 static struct mips_hi16 *mips_hi16_list;
1971 
1972 /* A howto special_function for REL *HI16 relocations.  We can only
1973    calculate the correct value once we've seen the partnering
1974    *LO16 relocation, so just save the information for later.
1975 
1976    The ABI requires that the *LO16 immediately follow the *HI16.
1977    However, as a GNU extension, we permit an arbitrary number of
1978    *HI16s to be associated with a single *LO16.  This significantly
1979    simplies the relocation handling in gcc.  */
1980 
1981 bfd_reloc_status_type
1982 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1983 			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1984 			  asection *input_section, bfd *output_bfd,
1985 			  char **error_message ATTRIBUTE_UNUSED)
1986 {
1987   struct mips_hi16 *n;
1988 
1989   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1990     return bfd_reloc_outofrange;
1991 
1992   n = bfd_malloc (sizeof *n);
1993   if (n == NULL)
1994     return bfd_reloc_outofrange;
1995 
1996   n->next = mips_hi16_list;
1997   n->data = data;
1998   n->input_section = input_section;
1999   n->rel = *reloc_entry;
2000   mips_hi16_list = n;
2001 
2002   if (output_bfd != NULL)
2003     reloc_entry->address += input_section->output_offset;
2004 
2005   return bfd_reloc_ok;
2006 }
2007 
2008 /* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2009    like any other 16-bit relocation when applied to global symbols, but is
2010    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2011 
2012 bfd_reloc_status_type
2013 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2014 			   void *data, asection *input_section,
2015 			   bfd *output_bfd, char **error_message)
2016 {
2017   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2018       || bfd_is_und_section (bfd_get_section (symbol))
2019       || bfd_is_com_section (bfd_get_section (symbol)))
2020     /* The relocation is against a global symbol.  */
2021     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2022 					input_section, output_bfd,
2023 					error_message);
2024 
2025   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2026 				   input_section, output_bfd, error_message);
2027 }
2028 
2029 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2030    is a straightforward 16 bit inplace relocation, but we must deal with
2031    any partnering high-part relocations as well.  */
2032 
2033 bfd_reloc_status_type
2034 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2035 			  void *data, asection *input_section,
2036 			  bfd *output_bfd, char **error_message)
2037 {
2038   bfd_vma vallo;
2039   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2040 
2041   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2042     return bfd_reloc_outofrange;
2043 
2044   _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2045 				   location);
2046   vallo = bfd_get_32 (abfd, location);
2047   _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2048 				 location);
2049 
2050   while (mips_hi16_list != NULL)
2051     {
2052       bfd_reloc_status_type ret;
2053       struct mips_hi16 *hi;
2054 
2055       hi = mips_hi16_list;
2056 
2057       /* R_MIPS*_GOT16 relocations are something of a special case.  We
2058 	 want to install the addend in the same way as for a R_MIPS*_HI16
2059 	 relocation (with a rightshift of 16).  However, since GOT16
2060 	 relocations can also be used with global symbols, their howto
2061 	 has a rightshift of 0.  */
2062       if (hi->rel.howto->type == R_MIPS_GOT16)
2063 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2064       else if (hi->rel.howto->type == R_MIPS16_GOT16)
2065 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2066 
2067       /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
2068 	 carry or borrow will induce a change of +1 or -1 in the high part.  */
2069       hi->rel.addend += (vallo + 0x8000) & 0xffff;
2070 
2071       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2072 					 hi->input_section, output_bfd,
2073 					 error_message);
2074       if (ret != bfd_reloc_ok)
2075 	return ret;
2076 
2077       mips_hi16_list = hi->next;
2078       free (hi);
2079     }
2080 
2081   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2082 				      input_section, output_bfd,
2083 				      error_message);
2084 }
2085 
2086 /* A generic howto special_function.  This calculates and installs the
2087    relocation itself, thus avoiding the oft-discussed problems in
2088    bfd_perform_relocation and bfd_install_relocation.  */
2089 
2090 bfd_reloc_status_type
2091 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2092 			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2093 			     asection *input_section, bfd *output_bfd,
2094 			     char **error_message ATTRIBUTE_UNUSED)
2095 {
2096   bfd_signed_vma val;
2097   bfd_reloc_status_type status;
2098   bfd_boolean relocatable;
2099 
2100   relocatable = (output_bfd != NULL);
2101 
2102   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2103     return bfd_reloc_outofrange;
2104 
2105   /* Build up the field adjustment in VAL.  */
2106   val = 0;
2107   if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2108     {
2109       /* Either we're calculating the final field value or we have a
2110 	 relocation against a section symbol.  Add in the section's
2111 	 offset or address.  */
2112       val += symbol->section->output_section->vma;
2113       val += symbol->section->output_offset;
2114     }
2115 
2116   if (!relocatable)
2117     {
2118       /* We're calculating the final field value.  Add in the symbol's value
2119 	 and, if pc-relative, subtract the address of the field itself.  */
2120       val += symbol->value;
2121       if (reloc_entry->howto->pc_relative)
2122 	{
2123 	  val -= input_section->output_section->vma;
2124 	  val -= input_section->output_offset;
2125 	  val -= reloc_entry->address;
2126 	}
2127     }
2128 
2129   /* VAL is now the final adjustment.  If we're keeping this relocation
2130      in the output file, and if the relocation uses a separate addend,
2131      we just need to add VAL to that addend.  Otherwise we need to add
2132      VAL to the relocation field itself.  */
2133   if (relocatable && !reloc_entry->howto->partial_inplace)
2134     reloc_entry->addend += val;
2135   else
2136     {
2137       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2138 
2139       /* Add in the separate addend, if any.  */
2140       val += reloc_entry->addend;
2141 
2142       /* Add VAL to the relocation field.  */
2143       _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2144 				       location);
2145       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2146 				       location);
2147       _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2148 				     location);
2149 
2150       if (status != bfd_reloc_ok)
2151 	return status;
2152     }
2153 
2154   if (relocatable)
2155     reloc_entry->address += input_section->output_offset;
2156 
2157   return bfd_reloc_ok;
2158 }
2159 
2160 /* Swap an entry in a .gptab section.  Note that these routines rely
2161    on the equivalence of the two elements of the union.  */
2162 
2163 static void
2164 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2165 			      Elf32_gptab *in)
2166 {
2167   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2168   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2169 }
2170 
2171 static void
2172 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2173 			       Elf32_External_gptab *ex)
2174 {
2175   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2176   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2177 }
2178 
2179 static void
2180 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2181 				Elf32_External_compact_rel *ex)
2182 {
2183   H_PUT_32 (abfd, in->id1, ex->id1);
2184   H_PUT_32 (abfd, in->num, ex->num);
2185   H_PUT_32 (abfd, in->id2, ex->id2);
2186   H_PUT_32 (abfd, in->offset, ex->offset);
2187   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2188   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2189 }
2190 
2191 static void
2192 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2193 			   Elf32_External_crinfo *ex)
2194 {
2195   unsigned long l;
2196 
2197   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2198        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2199        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2200        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2201   H_PUT_32 (abfd, l, ex->info);
2202   H_PUT_32 (abfd, in->konst, ex->konst);
2203   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2204 }
2205 
2206 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
2207    routines swap this structure in and out.  They are used outside of
2208    BFD, so they are globally visible.  */
2209 
2210 void
2211 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2212 				Elf32_RegInfo *in)
2213 {
2214   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2215   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2216   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2217   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2218   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2219   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2220 }
2221 
2222 void
2223 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2224 				 Elf32_External_RegInfo *ex)
2225 {
2226   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2227   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2228   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2229   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2230   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2231   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2232 }
2233 
2234 /* In the 64 bit ABI, the .MIPS.options section holds register
2235    information in an Elf64_Reginfo structure.  These routines swap
2236    them in and out.  They are globally visible because they are used
2237    outside of BFD.  These routines are here so that gas can call them
2238    without worrying about whether the 64 bit ABI has been included.  */
2239 
2240 void
2241 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2242 				Elf64_Internal_RegInfo *in)
2243 {
2244   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2245   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2246   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2247   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2248   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2249   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2250   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2251 }
2252 
2253 void
2254 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2255 				 Elf64_External_RegInfo *ex)
2256 {
2257   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2258   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2259   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2260   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2261   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2262   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2263   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2264 }
2265 
2266 /* Swap in an options header.  */
2267 
2268 void
2269 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2270 			      Elf_Internal_Options *in)
2271 {
2272   in->kind = H_GET_8 (abfd, ex->kind);
2273   in->size = H_GET_8 (abfd, ex->size);
2274   in->section = H_GET_16 (abfd, ex->section);
2275   in->info = H_GET_32 (abfd, ex->info);
2276 }
2277 
2278 /* Swap out an options header.  */
2279 
2280 void
2281 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2282 			       Elf_External_Options *ex)
2283 {
2284   H_PUT_8 (abfd, in->kind, ex->kind);
2285   H_PUT_8 (abfd, in->size, ex->size);
2286   H_PUT_16 (abfd, in->section, ex->section);
2287   H_PUT_32 (abfd, in->info, ex->info);
2288 }
2289 
2290 /* This function is called via qsort() to sort the dynamic relocation
2291    entries by increasing r_symndx value.  */
2292 
2293 static int
2294 sort_dynamic_relocs (const void *arg1, const void *arg2)
2295 {
2296   Elf_Internal_Rela int_reloc1;
2297   Elf_Internal_Rela int_reloc2;
2298   int diff;
2299 
2300   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2301   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2302 
2303   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2304   if (diff != 0)
2305     return diff;
2306 
2307   if (int_reloc1.r_offset < int_reloc2.r_offset)
2308     return -1;
2309   if (int_reloc1.r_offset > int_reloc2.r_offset)
2310     return 1;
2311   return 0;
2312 }
2313 
2314 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2315 
2316 static int
2317 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2318 			const void *arg2 ATTRIBUTE_UNUSED)
2319 {
2320 #ifdef BFD64
2321   Elf_Internal_Rela int_reloc1[3];
2322   Elf_Internal_Rela int_reloc2[3];
2323 
2324   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2325     (reldyn_sorting_bfd, arg1, int_reloc1);
2326   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2327     (reldyn_sorting_bfd, arg2, int_reloc2);
2328 
2329   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2330     return -1;
2331   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2332     return 1;
2333 
2334   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2335     return -1;
2336   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2337     return 1;
2338   return 0;
2339 #else
2340   abort ();
2341 #endif
2342 }
2343 
2344 
2345 /* This routine is used to write out ECOFF debugging external symbol
2346    information.  It is called via mips_elf_link_hash_traverse.  The
2347    ECOFF external symbol information must match the ELF external
2348    symbol information.  Unfortunately, at this point we don't know
2349    whether a symbol is required by reloc information, so the two
2350    tables may wind up being different.  We must sort out the external
2351    symbol information before we can set the final size of the .mdebug
2352    section, and we must set the size of the .mdebug section before we
2353    can relocate any sections, and we can't know which symbols are
2354    required by relocation until we relocate the sections.
2355    Fortunately, it is relatively unlikely that any symbol will be
2356    stripped but required by a reloc.  In particular, it can not happen
2357    when generating a final executable.  */
2358 
2359 static bfd_boolean
2360 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2361 {
2362   struct extsym_info *einfo = data;
2363   bfd_boolean strip;
2364   asection *sec, *output_section;
2365 
2366   if (h->root.root.type == bfd_link_hash_warning)
2367     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2368 
2369   if (h->root.indx == -2)
2370     strip = FALSE;
2371   else if ((h->root.def_dynamic
2372 	    || h->root.ref_dynamic
2373 	    || h->root.type == bfd_link_hash_new)
2374 	   && !h->root.def_regular
2375 	   && !h->root.ref_regular)
2376     strip = TRUE;
2377   else if (einfo->info->strip == strip_all
2378 	   || (einfo->info->strip == strip_some
2379 	       && bfd_hash_lookup (einfo->info->keep_hash,
2380 				   h->root.root.root.string,
2381 				   FALSE, FALSE) == NULL))
2382     strip = TRUE;
2383   else
2384     strip = FALSE;
2385 
2386   if (strip)
2387     return TRUE;
2388 
2389   if (h->esym.ifd == -2)
2390     {
2391       h->esym.jmptbl = 0;
2392       h->esym.cobol_main = 0;
2393       h->esym.weakext = 0;
2394       h->esym.reserved = 0;
2395       h->esym.ifd = ifdNil;
2396       h->esym.asym.value = 0;
2397       h->esym.asym.st = stGlobal;
2398 
2399       if (h->root.root.type == bfd_link_hash_undefined
2400 	  || h->root.root.type == bfd_link_hash_undefweak)
2401 	{
2402 	  const char *name;
2403 
2404 	  /* Use undefined class.  Also, set class and type for some
2405              special symbols.  */
2406 	  name = h->root.root.root.string;
2407 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2408 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2409 	    {
2410 	      h->esym.asym.sc = scData;
2411 	      h->esym.asym.st = stLabel;
2412 	      h->esym.asym.value = 0;
2413 	    }
2414 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2415 	    {
2416 	      h->esym.asym.sc = scAbs;
2417 	      h->esym.asym.st = stLabel;
2418 	      h->esym.asym.value =
2419 		mips_elf_hash_table (einfo->info)->procedure_count;
2420 	    }
2421 	  else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2422 	    {
2423 	      h->esym.asym.sc = scAbs;
2424 	      h->esym.asym.st = stLabel;
2425 	      h->esym.asym.value = elf_gp (einfo->abfd);
2426 	    }
2427 	  else
2428 	    h->esym.asym.sc = scUndefined;
2429 	}
2430       else if (h->root.root.type != bfd_link_hash_defined
2431 	  && h->root.root.type != bfd_link_hash_defweak)
2432 	h->esym.asym.sc = scAbs;
2433       else
2434 	{
2435 	  const char *name;
2436 
2437 	  sec = h->root.root.u.def.section;
2438 	  output_section = sec->output_section;
2439 
2440 	  /* When making a shared library and symbol h is the one from
2441 	     the another shared library, OUTPUT_SECTION may be null.  */
2442 	  if (output_section == NULL)
2443 	    h->esym.asym.sc = scUndefined;
2444 	  else
2445 	    {
2446 	      name = bfd_section_name (output_section->owner, output_section);
2447 
2448 	      if (strcmp (name, ".text") == 0)
2449 		h->esym.asym.sc = scText;
2450 	      else if (strcmp (name, ".data") == 0)
2451 		h->esym.asym.sc = scData;
2452 	      else if (strcmp (name, ".sdata") == 0)
2453 		h->esym.asym.sc = scSData;
2454 	      else if (strcmp (name, ".rodata") == 0
2455 		       || strcmp (name, ".rdata") == 0)
2456 		h->esym.asym.sc = scRData;
2457 	      else if (strcmp (name, ".bss") == 0)
2458 		h->esym.asym.sc = scBss;
2459 	      else if (strcmp (name, ".sbss") == 0)
2460 		h->esym.asym.sc = scSBss;
2461 	      else if (strcmp (name, ".init") == 0)
2462 		h->esym.asym.sc = scInit;
2463 	      else if (strcmp (name, ".fini") == 0)
2464 		h->esym.asym.sc = scFini;
2465 	      else
2466 		h->esym.asym.sc = scAbs;
2467 	    }
2468 	}
2469 
2470       h->esym.asym.reserved = 0;
2471       h->esym.asym.index = indexNil;
2472     }
2473 
2474   if (h->root.root.type == bfd_link_hash_common)
2475     h->esym.asym.value = h->root.root.u.c.size;
2476   else if (h->root.root.type == bfd_link_hash_defined
2477 	   || h->root.root.type == bfd_link_hash_defweak)
2478     {
2479       if (h->esym.asym.sc == scCommon)
2480 	h->esym.asym.sc = scBss;
2481       else if (h->esym.asym.sc == scSCommon)
2482 	h->esym.asym.sc = scSBss;
2483 
2484       sec = h->root.root.u.def.section;
2485       output_section = sec->output_section;
2486       if (output_section != NULL)
2487 	h->esym.asym.value = (h->root.root.u.def.value
2488 			      + sec->output_offset
2489 			      + output_section->vma);
2490       else
2491 	h->esym.asym.value = 0;
2492     }
2493   else
2494     {
2495       struct mips_elf_link_hash_entry *hd = h;
2496 
2497       while (hd->root.root.type == bfd_link_hash_indirect)
2498 	hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2499 
2500       if (hd->needs_lazy_stub)
2501 	{
2502 	  /* Set type and value for a symbol with a function stub.  */
2503 	  h->esym.asym.st = stProc;
2504 	  sec = hd->root.root.u.def.section;
2505 	  if (sec == NULL)
2506 	    h->esym.asym.value = 0;
2507 	  else
2508 	    {
2509 	      output_section = sec->output_section;
2510 	      if (output_section != NULL)
2511 		h->esym.asym.value = (hd->root.plt.offset
2512 				      + sec->output_offset
2513 				      + output_section->vma);
2514 	      else
2515 		h->esym.asym.value = 0;
2516 	    }
2517 	}
2518     }
2519 
2520   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2521 				      h->root.root.root.string,
2522 				      &h->esym))
2523     {
2524       einfo->failed = TRUE;
2525       return FALSE;
2526     }
2527 
2528   return TRUE;
2529 }
2530 
2531 /* A comparison routine used to sort .gptab entries.  */
2532 
2533 static int
2534 gptab_compare (const void *p1, const void *p2)
2535 {
2536   const Elf32_gptab *a1 = p1;
2537   const Elf32_gptab *a2 = p2;
2538 
2539   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2540 }
2541 
2542 /* Functions to manage the got entry hash table.  */
2543 
2544 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2545    hash number.  */
2546 
2547 static INLINE hashval_t
2548 mips_elf_hash_bfd_vma (bfd_vma addr)
2549 {
2550 #ifdef BFD64
2551   return addr + (addr >> 32);
2552 #else
2553   return addr;
2554 #endif
2555 }
2556 
2557 /* got_entries only match if they're identical, except for gotidx, so
2558    use all fields to compute the hash, and compare the appropriate
2559    union members.  */
2560 
2561 static hashval_t
2562 mips_elf_got_entry_hash (const void *entry_)
2563 {
2564   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2565 
2566   return entry->symndx
2567     + ((entry->tls_type & GOT_TLS_LDM) << 17)
2568     + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2569        : entry->abfd->id
2570          + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2571 	    : entry->d.h->root.root.root.hash));
2572 }
2573 
2574 static int
2575 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2576 {
2577   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2578   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2579 
2580   /* An LDM entry can only match another LDM entry.  */
2581   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2582     return 0;
2583 
2584   return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2585     && (! e1->abfd ? e1->d.address == e2->d.address
2586 	: e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2587 	: e1->d.h == e2->d.h);
2588 }
2589 
2590 /* multi_got_entries are still a match in the case of global objects,
2591    even if the input bfd in which they're referenced differs, so the
2592    hash computation and compare functions are adjusted
2593    accordingly.  */
2594 
2595 static hashval_t
2596 mips_elf_multi_got_entry_hash (const void *entry_)
2597 {
2598   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2599 
2600   return entry->symndx
2601     + (! entry->abfd
2602        ? mips_elf_hash_bfd_vma (entry->d.address)
2603        : entry->symndx >= 0
2604        ? ((entry->tls_type & GOT_TLS_LDM)
2605 	  ? (GOT_TLS_LDM << 17)
2606 	  : (entry->abfd->id
2607 	     + mips_elf_hash_bfd_vma (entry->d.addend)))
2608        : entry->d.h->root.root.root.hash);
2609 }
2610 
2611 static int
2612 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2613 {
2614   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2615   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2616 
2617   /* Any two LDM entries match.  */
2618   if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2619     return 1;
2620 
2621   /* Nothing else matches an LDM entry.  */
2622   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2623     return 0;
2624 
2625   return e1->symndx == e2->symndx
2626     && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2627 	: e1->abfd == NULL || e2->abfd == NULL
2628 	? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2629 	: e1->d.h == e2->d.h);
2630 }
2631 
2632 static hashval_t
2633 mips_got_page_entry_hash (const void *entry_)
2634 {
2635   const struct mips_got_page_entry *entry;
2636 
2637   entry = (const struct mips_got_page_entry *) entry_;
2638   return entry->abfd->id + entry->symndx;
2639 }
2640 
2641 static int
2642 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2643 {
2644   const struct mips_got_page_entry *entry1, *entry2;
2645 
2646   entry1 = (const struct mips_got_page_entry *) entry1_;
2647   entry2 = (const struct mips_got_page_entry *) entry2_;
2648   return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2649 }
2650 
2651 /* Return the dynamic relocation section.  If it doesn't exist, try to
2652    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
2653    if creation fails.  */
2654 
2655 static asection *
2656 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2657 {
2658   const char *dname;
2659   asection *sreloc;
2660   bfd *dynobj;
2661 
2662   dname = MIPS_ELF_REL_DYN_NAME (info);
2663   dynobj = elf_hash_table (info)->dynobj;
2664   sreloc = bfd_get_section_by_name (dynobj, dname);
2665   if (sreloc == NULL && create_p)
2666     {
2667       sreloc = bfd_make_section_with_flags (dynobj, dname,
2668 					    (SEC_ALLOC
2669 					     | SEC_LOAD
2670 					     | SEC_HAS_CONTENTS
2671 					     | SEC_IN_MEMORY
2672 					     | SEC_LINKER_CREATED
2673 					     | SEC_READONLY));
2674       if (sreloc == NULL
2675 	  || ! bfd_set_section_alignment (dynobj, sreloc,
2676 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2677 	return NULL;
2678     }
2679   return sreloc;
2680 }
2681 
2682 /* Count the number of relocations needed for a TLS GOT entry, with
2683    access types from TLS_TYPE, and symbol H (or a local symbol if H
2684    is NULL).  */
2685 
2686 static int
2687 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2688 		     struct elf_link_hash_entry *h)
2689 {
2690   int indx = 0;
2691   int ret = 0;
2692   bfd_boolean need_relocs = FALSE;
2693   bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2694 
2695   if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2696       && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2697     indx = h->dynindx;
2698 
2699   if ((info->shared || indx != 0)
2700       && (h == NULL
2701 	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2702 	  || h->root.type != bfd_link_hash_undefweak))
2703     need_relocs = TRUE;
2704 
2705   if (!need_relocs)
2706     return FALSE;
2707 
2708   if (tls_type & GOT_TLS_GD)
2709     {
2710       ret++;
2711       if (indx != 0)
2712 	ret++;
2713     }
2714 
2715   if (tls_type & GOT_TLS_IE)
2716     ret++;
2717 
2718   if ((tls_type & GOT_TLS_LDM) && info->shared)
2719     ret++;
2720 
2721   return ret;
2722 }
2723 
2724 /* Count the number of TLS relocations required for the GOT entry in
2725    ARG1, if it describes a local symbol.  */
2726 
2727 static int
2728 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2729 {
2730   struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2731   struct mips_elf_count_tls_arg *arg = arg2;
2732 
2733   if (entry->abfd != NULL && entry->symndx != -1)
2734     arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2735 
2736   return 1;
2737 }
2738 
2739 /* Count the number of TLS GOT entries required for the global (or
2740    forced-local) symbol in ARG1.  */
2741 
2742 static int
2743 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2744 {
2745   struct mips_elf_link_hash_entry *hm
2746     = (struct mips_elf_link_hash_entry *) arg1;
2747   struct mips_elf_count_tls_arg *arg = arg2;
2748 
2749   if (hm->tls_type & GOT_TLS_GD)
2750     arg->needed += 2;
2751   if (hm->tls_type & GOT_TLS_IE)
2752     arg->needed += 1;
2753 
2754   return 1;
2755 }
2756 
2757 /* Count the number of TLS relocations required for the global (or
2758    forced-local) symbol in ARG1.  */
2759 
2760 static int
2761 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2762 {
2763   struct mips_elf_link_hash_entry *hm
2764     = (struct mips_elf_link_hash_entry *) arg1;
2765   struct mips_elf_count_tls_arg *arg = arg2;
2766 
2767   arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2768 
2769   return 1;
2770 }
2771 
2772 /* Output a simple dynamic relocation into SRELOC.  */
2773 
2774 static void
2775 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2776 				    asection *sreloc,
2777 				    unsigned long reloc_index,
2778 				    unsigned long indx,
2779 				    int r_type,
2780 				    bfd_vma offset)
2781 {
2782   Elf_Internal_Rela rel[3];
2783 
2784   memset (rel, 0, sizeof (rel));
2785 
2786   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2787   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2788 
2789   if (ABI_64_P (output_bfd))
2790     {
2791       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2792 	(output_bfd, &rel[0],
2793 	 (sreloc->contents
2794 	  + reloc_index * sizeof (Elf64_Mips_External_Rel)));
2795     }
2796   else
2797     bfd_elf32_swap_reloc_out
2798       (output_bfd, &rel[0],
2799        (sreloc->contents
2800 	+ reloc_index * sizeof (Elf32_External_Rel)));
2801 }
2802 
2803 /* Initialize a set of TLS GOT entries for one symbol.  */
2804 
2805 static void
2806 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2807 			       unsigned char *tls_type_p,
2808 			       struct bfd_link_info *info,
2809 			       struct mips_elf_link_hash_entry *h,
2810 			       bfd_vma value)
2811 {
2812   struct mips_elf_link_hash_table *htab;
2813   int indx;
2814   asection *sreloc, *sgot;
2815   bfd_vma offset, offset2;
2816   bfd_boolean need_relocs = FALSE;
2817 
2818   htab = mips_elf_hash_table (info);
2819   sgot = htab->sgot;
2820 
2821   indx = 0;
2822   if (h != NULL)
2823     {
2824       bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2825 
2826       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2827 	  && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2828 	indx = h->root.dynindx;
2829     }
2830 
2831   if (*tls_type_p & GOT_TLS_DONE)
2832     return;
2833 
2834   if ((info->shared || indx != 0)
2835       && (h == NULL
2836 	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2837 	  || h->root.type != bfd_link_hash_undefweak))
2838     need_relocs = TRUE;
2839 
2840   /* MINUS_ONE means the symbol is not defined in this object.  It may not
2841      be defined at all; assume that the value doesn't matter in that
2842      case.  Otherwise complain if we would use the value.  */
2843   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2844 	      || h->root.root.type == bfd_link_hash_undefweak);
2845 
2846   /* Emit necessary relocations.  */
2847   sreloc = mips_elf_rel_dyn_section (info, FALSE);
2848 
2849   /* General Dynamic.  */
2850   if (*tls_type_p & GOT_TLS_GD)
2851     {
2852       offset = got_offset;
2853       offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2854 
2855       if (need_relocs)
2856 	{
2857 	  mips_elf_output_dynamic_relocation
2858 	    (abfd, sreloc, sreloc->reloc_count++, indx,
2859 	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2860 	     sgot->output_offset + sgot->output_section->vma + offset);
2861 
2862 	  if (indx)
2863 	    mips_elf_output_dynamic_relocation
2864 	      (abfd, sreloc, sreloc->reloc_count++, indx,
2865 	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2866 	       sgot->output_offset + sgot->output_section->vma + offset2);
2867 	  else
2868 	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2869 			       sgot->contents + offset2);
2870 	}
2871       else
2872 	{
2873 	  MIPS_ELF_PUT_WORD (abfd, 1,
2874 			     sgot->contents + offset);
2875 	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2876 			     sgot->contents + offset2);
2877 	}
2878 
2879       got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2880     }
2881 
2882   /* Initial Exec model.  */
2883   if (*tls_type_p & GOT_TLS_IE)
2884     {
2885       offset = got_offset;
2886 
2887       if (need_relocs)
2888 	{
2889 	  if (indx == 0)
2890 	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2891 			       sgot->contents + offset);
2892 	  else
2893 	    MIPS_ELF_PUT_WORD (abfd, 0,
2894 			       sgot->contents + offset);
2895 
2896 	  mips_elf_output_dynamic_relocation
2897 	    (abfd, sreloc, sreloc->reloc_count++, indx,
2898 	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2899 	     sgot->output_offset + sgot->output_section->vma + offset);
2900 	}
2901       else
2902 	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2903 			   sgot->contents + offset);
2904     }
2905 
2906   if (*tls_type_p & GOT_TLS_LDM)
2907     {
2908       /* The initial offset is zero, and the LD offsets will include the
2909 	 bias by DTP_OFFSET.  */
2910       MIPS_ELF_PUT_WORD (abfd, 0,
2911 			 sgot->contents + got_offset
2912 			 + MIPS_ELF_GOT_SIZE (abfd));
2913 
2914       if (!info->shared)
2915 	MIPS_ELF_PUT_WORD (abfd, 1,
2916 			   sgot->contents + got_offset);
2917       else
2918 	mips_elf_output_dynamic_relocation
2919 	  (abfd, sreloc, sreloc->reloc_count++, indx,
2920 	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2921 	   sgot->output_offset + sgot->output_section->vma + got_offset);
2922     }
2923 
2924   *tls_type_p |= GOT_TLS_DONE;
2925 }
2926 
2927 /* Return the GOT index to use for a relocation of type R_TYPE against
2928    a symbol accessed using TLS_TYPE models.  The GOT entries for this
2929    symbol in this GOT start at GOT_INDEX.  This function initializes the
2930    GOT entries and corresponding relocations.  */
2931 
2932 static bfd_vma
2933 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2934 		    int r_type, struct bfd_link_info *info,
2935 		    struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2936 {
2937   BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2938 	      || r_type == R_MIPS_TLS_LDM);
2939 
2940   mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2941 
2942   if (r_type == R_MIPS_TLS_GOTTPREL)
2943     {
2944       BFD_ASSERT (*tls_type & GOT_TLS_IE);
2945       if (*tls_type & GOT_TLS_GD)
2946 	return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2947       else
2948 	return got_index;
2949     }
2950 
2951   if (r_type == R_MIPS_TLS_GD)
2952     {
2953       BFD_ASSERT (*tls_type & GOT_TLS_GD);
2954       return got_index;
2955     }
2956 
2957   if (r_type == R_MIPS_TLS_LDM)
2958     {
2959       BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2960       return got_index;
2961     }
2962 
2963   return got_index;
2964 }
2965 
2966 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2967    for global symbol H.  .got.plt comes before the GOT, so the offset
2968    will be negative.  */
2969 
2970 static bfd_vma
2971 mips_elf_gotplt_index (struct bfd_link_info *info,
2972 		       struct elf_link_hash_entry *h)
2973 {
2974   bfd_vma plt_index, got_address, got_value;
2975   struct mips_elf_link_hash_table *htab;
2976 
2977   htab = mips_elf_hash_table (info);
2978   BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2979 
2980   /* This function only works for VxWorks, because a non-VxWorks .got.plt
2981      section starts with reserved entries.  */
2982   BFD_ASSERT (htab->is_vxworks);
2983 
2984   /* Calculate the index of the symbol's PLT entry.  */
2985   plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
2986 
2987   /* Calculate the address of the associated .got.plt entry.  */
2988   got_address = (htab->sgotplt->output_section->vma
2989 		 + htab->sgotplt->output_offset
2990 		 + plt_index * 4);
2991 
2992   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
2993   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
2994 	       + htab->root.hgot->root.u.def.section->output_offset
2995 	       + htab->root.hgot->root.u.def.value);
2996 
2997   return got_address - got_value;
2998 }
2999 
3000 /* Return the GOT offset for address VALUE.   If there is not yet a GOT
3001    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3002    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3003    offset can be found.  */
3004 
3005 static bfd_vma
3006 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3007 			  bfd_vma value, unsigned long r_symndx,
3008 			  struct mips_elf_link_hash_entry *h, int r_type)
3009 {
3010   struct mips_elf_link_hash_table *htab;
3011   struct mips_got_entry *entry;
3012 
3013   htab = mips_elf_hash_table (info);
3014   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3015 					   r_symndx, h, r_type);
3016   if (!entry)
3017     return MINUS_ONE;
3018 
3019   if (TLS_RELOC_P (r_type))
3020     {
3021       if (entry->symndx == -1 && htab->got_info->next == NULL)
3022 	/* A type (3) entry in the single-GOT case.  We use the symbol's
3023 	   hash table entry to track the index.  */
3024 	return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3025 				   r_type, info, h, value);
3026       else
3027 	return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3028 				   r_type, info, h, value);
3029     }
3030   else
3031     return entry->gotidx;
3032 }
3033 
3034 /* Returns the GOT index for the global symbol indicated by H.  */
3035 
3036 static bfd_vma
3037 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3038 			   int r_type, struct bfd_link_info *info)
3039 {
3040   struct mips_elf_link_hash_table *htab;
3041   bfd_vma index;
3042   struct mips_got_info *g, *gg;
3043   long global_got_dynindx = 0;
3044 
3045   htab = mips_elf_hash_table (info);
3046   gg = g = htab->got_info;
3047   if (g->bfd2got && ibfd)
3048     {
3049       struct mips_got_entry e, *p;
3050 
3051       BFD_ASSERT (h->dynindx >= 0);
3052 
3053       g = mips_elf_got_for_ibfd (g, ibfd);
3054       if (g->next != gg || TLS_RELOC_P (r_type))
3055 	{
3056 	  e.abfd = ibfd;
3057 	  e.symndx = -1;
3058 	  e.d.h = (struct mips_elf_link_hash_entry *)h;
3059 	  e.tls_type = 0;
3060 
3061 	  p = htab_find (g->got_entries, &e);
3062 
3063 	  BFD_ASSERT (p->gotidx > 0);
3064 
3065 	  if (TLS_RELOC_P (r_type))
3066 	    {
3067 	      bfd_vma value = MINUS_ONE;
3068 	      if ((h->root.type == bfd_link_hash_defined
3069 		   || h->root.type == bfd_link_hash_defweak)
3070 		  && h->root.u.def.section->output_section)
3071 		value = (h->root.u.def.value
3072 			 + h->root.u.def.section->output_offset
3073 			 + h->root.u.def.section->output_section->vma);
3074 
3075 	      return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3076 					 info, e.d.h, value);
3077 	    }
3078 	  else
3079 	    return p->gotidx;
3080 	}
3081     }
3082 
3083   if (gg->global_gotsym != NULL)
3084     global_got_dynindx = gg->global_gotsym->dynindx;
3085 
3086   if (TLS_RELOC_P (r_type))
3087     {
3088       struct mips_elf_link_hash_entry *hm
3089 	= (struct mips_elf_link_hash_entry *) h;
3090       bfd_vma value = MINUS_ONE;
3091 
3092       if ((h->root.type == bfd_link_hash_defined
3093 	   || h->root.type == bfd_link_hash_defweak)
3094 	  && h->root.u.def.section->output_section)
3095 	value = (h->root.u.def.value
3096 		 + h->root.u.def.section->output_offset
3097 		 + h->root.u.def.section->output_section->vma);
3098 
3099       index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3100 				  r_type, info, hm, value);
3101     }
3102   else
3103     {
3104       /* Once we determine the global GOT entry with the lowest dynamic
3105 	 symbol table index, we must put all dynamic symbols with greater
3106 	 indices into the GOT.  That makes it easy to calculate the GOT
3107 	 offset.  */
3108       BFD_ASSERT (h->dynindx >= global_got_dynindx);
3109       index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3110 	       * MIPS_ELF_GOT_SIZE (abfd));
3111     }
3112   BFD_ASSERT (index < htab->sgot->size);
3113 
3114   return index;
3115 }
3116 
3117 /* Find a GOT page entry that points to within 32KB of VALUE.  These
3118    entries are supposed to be placed at small offsets in the GOT, i.e.,
3119    within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3120    entry could be created.  If OFFSETP is nonnull, use it to return the
3121    offset of the GOT entry from VALUE.  */
3122 
3123 static bfd_vma
3124 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3125 		   bfd_vma value, bfd_vma *offsetp)
3126 {
3127   bfd_vma page, index;
3128   struct mips_got_entry *entry;
3129 
3130   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3131   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3132 					   NULL, R_MIPS_GOT_PAGE);
3133 
3134   if (!entry)
3135     return MINUS_ONE;
3136 
3137   index = entry->gotidx;
3138 
3139   if (offsetp)
3140     *offsetp = value - entry->d.address;
3141 
3142   return index;
3143 }
3144 
3145 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3146    EXTERNAL is true if the relocation was against a global symbol
3147    that has been forced local.  */
3148 
3149 static bfd_vma
3150 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3151 		      bfd_vma value, bfd_boolean external)
3152 {
3153   struct mips_got_entry *entry;
3154 
3155   /* GOT16 relocations against local symbols are followed by a LO16
3156      relocation; those against global symbols are not.  Thus if the
3157      symbol was originally local, the GOT16 relocation should load the
3158      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3159   if (! external)
3160     value = mips_elf_high (value) << 16;
3161 
3162   /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3163      R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3164      same in all cases.  */
3165   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3166 					   NULL, R_MIPS_GOT16);
3167   if (entry)
3168     return entry->gotidx;
3169   else
3170     return MINUS_ONE;
3171 }
3172 
3173 /* Returns the offset for the entry at the INDEXth position
3174    in the GOT.  */
3175 
3176 static bfd_vma
3177 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3178 				bfd *input_bfd, bfd_vma index)
3179 {
3180   struct mips_elf_link_hash_table *htab;
3181   asection *sgot;
3182   bfd_vma gp;
3183 
3184   htab = mips_elf_hash_table (info);
3185   sgot = htab->sgot;
3186   gp = _bfd_get_gp_value (output_bfd)
3187     + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3188 
3189   return sgot->output_section->vma + sgot->output_offset + index - gp;
3190 }
3191 
3192 /* Create and return a local GOT entry for VALUE, which was calculated
3193    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3194    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3195    instead.  */
3196 
3197 static struct mips_got_entry *
3198 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3199 				 bfd *ibfd, bfd_vma value,
3200 				 unsigned long r_symndx,
3201 				 struct mips_elf_link_hash_entry *h,
3202 				 int r_type)
3203 {
3204   struct mips_got_entry entry, **loc;
3205   struct mips_got_info *g;
3206   struct mips_elf_link_hash_table *htab;
3207 
3208   htab = mips_elf_hash_table (info);
3209 
3210   entry.abfd = NULL;
3211   entry.symndx = -1;
3212   entry.d.address = value;
3213   entry.tls_type = 0;
3214 
3215   g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
3216   if (g == NULL)
3217     {
3218       g = mips_elf_got_for_ibfd (htab->got_info, abfd);
3219       BFD_ASSERT (g != NULL);
3220     }
3221 
3222   /* We might have a symbol, H, if it has been forced local.  Use the
3223      global entry then.  It doesn't matter whether an entry is local
3224      or global for TLS, since the dynamic linker does not
3225      automatically relocate TLS GOT entries.  */
3226   BFD_ASSERT (h == NULL || h->root.forced_local);
3227   if (TLS_RELOC_P (r_type))
3228     {
3229       struct mips_got_entry *p;
3230 
3231       entry.abfd = ibfd;
3232       if (r_type == R_MIPS_TLS_LDM)
3233 	{
3234 	  entry.tls_type = GOT_TLS_LDM;
3235 	  entry.symndx = 0;
3236 	  entry.d.addend = 0;
3237 	}
3238       else if (h == NULL)
3239 	{
3240 	  entry.symndx = r_symndx;
3241 	  entry.d.addend = 0;
3242 	}
3243       else
3244 	entry.d.h = h;
3245 
3246       p = (struct mips_got_entry *)
3247 	htab_find (g->got_entries, &entry);
3248 
3249       BFD_ASSERT (p);
3250       return p;
3251     }
3252 
3253   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3254 						   INSERT);
3255   if (*loc)
3256     return *loc;
3257 
3258   entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3259   entry.tls_type = 0;
3260 
3261   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3262 
3263   if (! *loc)
3264     return NULL;
3265 
3266   memcpy (*loc, &entry, sizeof entry);
3267 
3268   if (g->assigned_gotno > g->local_gotno)
3269     {
3270       (*loc)->gotidx = -1;
3271       /* We didn't allocate enough space in the GOT.  */
3272       (*_bfd_error_handler)
3273 	(_("not enough GOT space for local GOT entries"));
3274       bfd_set_error (bfd_error_bad_value);
3275       return NULL;
3276     }
3277 
3278   MIPS_ELF_PUT_WORD (abfd, value,
3279 		     (htab->sgot->contents + entry.gotidx));
3280 
3281   /* These GOT entries need a dynamic relocation on VxWorks.  */
3282   if (htab->is_vxworks)
3283     {
3284       Elf_Internal_Rela outrel;
3285       asection *s;
3286       bfd_byte *loc;
3287       bfd_vma got_address;
3288 
3289       s = mips_elf_rel_dyn_section (info, FALSE);
3290       got_address = (htab->sgot->output_section->vma
3291 		     + htab->sgot->output_offset
3292 		     + entry.gotidx);
3293 
3294       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3295       outrel.r_offset = got_address;
3296       outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3297       outrel.r_addend = value;
3298       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
3299     }
3300 
3301   return *loc;
3302 }
3303 
3304 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3305    The number might be exact or a worst-case estimate, depending on how
3306    much information is available to elf_backend_omit_section_dynsym at
3307    the current linking stage.  */
3308 
3309 static bfd_size_type
3310 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3311 {
3312   bfd_size_type count;
3313 
3314   count = 0;
3315   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3316     {
3317       asection *p;
3318       const struct elf_backend_data *bed;
3319 
3320       bed = get_elf_backend_data (output_bfd);
3321       for (p = output_bfd->sections; p ; p = p->next)
3322 	if ((p->flags & SEC_EXCLUDE) == 0
3323 	    && (p->flags & SEC_ALLOC) != 0
3324 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3325 	  ++count;
3326     }
3327   return count;
3328 }
3329 
3330 /* Sort the dynamic symbol table so that symbols that need GOT entries
3331    appear towards the end.  */
3332 
3333 static bfd_boolean
3334 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3335 {
3336   struct mips_elf_link_hash_table *htab;
3337   struct mips_elf_hash_sort_data hsd;
3338   struct mips_got_info *g;
3339 
3340   if (elf_hash_table (info)->dynsymcount == 0)
3341     return TRUE;
3342 
3343   htab = mips_elf_hash_table (info);
3344   g = htab->got_info;
3345   if (g == NULL)
3346     return TRUE;
3347 
3348   hsd.low = NULL;
3349   hsd.max_unref_got_dynindx
3350     = hsd.min_got_dynindx
3351     = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3352   hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3353   mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3354 				elf_hash_table (info)),
3355 			       mips_elf_sort_hash_table_f,
3356 			       &hsd);
3357 
3358   /* There should have been enough room in the symbol table to
3359      accommodate both the GOT and non-GOT symbols.  */
3360   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3361   BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3362 	      == elf_hash_table (info)->dynsymcount);
3363   BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3364 	      == g->global_gotno);
3365 
3366   /* Now we know which dynamic symbol has the lowest dynamic symbol
3367      table index in the GOT.  */
3368   g->global_gotsym = hsd.low;
3369 
3370   return TRUE;
3371 }
3372 
3373 /* If H needs a GOT entry, assign it the highest available dynamic
3374    index.  Otherwise, assign it the lowest available dynamic
3375    index.  */
3376 
3377 static bfd_boolean
3378 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3379 {
3380   struct mips_elf_hash_sort_data *hsd = data;
3381 
3382   if (h->root.root.type == bfd_link_hash_warning)
3383     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3384 
3385   /* Symbols without dynamic symbol table entries aren't interesting
3386      at all.  */
3387   if (h->root.dynindx == -1)
3388     return TRUE;
3389 
3390   switch (h->global_got_area)
3391     {
3392     case GGA_NONE:
3393       h->root.dynindx = hsd->max_non_got_dynindx++;
3394       break;
3395 
3396     case GGA_NORMAL:
3397       BFD_ASSERT (h->tls_type == GOT_NORMAL);
3398 
3399       h->root.dynindx = --hsd->min_got_dynindx;
3400       hsd->low = (struct elf_link_hash_entry *) h;
3401       break;
3402 
3403     case GGA_RELOC_ONLY:
3404       BFD_ASSERT (h->tls_type == GOT_NORMAL);
3405 
3406       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3407 	hsd->low = (struct elf_link_hash_entry *) h;
3408       h->root.dynindx = hsd->max_unref_got_dynindx++;
3409       break;
3410     }
3411 
3412   return TRUE;
3413 }
3414 
3415 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3416    symbol table index lower than any we've seen to date, record it for
3417    posterity.  */
3418 
3419 static bfd_boolean
3420 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3421 				   bfd *abfd, struct bfd_link_info *info,
3422 				   unsigned char tls_flag)
3423 {
3424   struct mips_elf_link_hash_table *htab;
3425   struct mips_elf_link_hash_entry *hmips;
3426   struct mips_got_entry entry, **loc;
3427   struct mips_got_info *g;
3428 
3429   htab = mips_elf_hash_table (info);
3430   hmips = (struct mips_elf_link_hash_entry *) h;
3431 
3432   /* A global symbol in the GOT must also be in the dynamic symbol
3433      table.  */
3434   if (h->dynindx == -1)
3435     {
3436       switch (ELF_ST_VISIBILITY (h->other))
3437 	{
3438 	case STV_INTERNAL:
3439 	case STV_HIDDEN:
3440 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3441 	  break;
3442 	}
3443       if (!bfd_elf_link_record_dynamic_symbol (info, h))
3444 	return FALSE;
3445     }
3446 
3447   /* Make sure we have a GOT to put this entry into.  */
3448   g = htab->got_info;
3449   BFD_ASSERT (g != NULL);
3450 
3451   entry.abfd = abfd;
3452   entry.symndx = -1;
3453   entry.d.h = (struct mips_elf_link_hash_entry *) h;
3454   entry.tls_type = 0;
3455 
3456   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3457 						   INSERT);
3458 
3459   /* If we've already marked this entry as needing GOT space, we don't
3460      need to do it again.  */
3461   if (*loc)
3462     {
3463       (*loc)->tls_type |= tls_flag;
3464       return TRUE;
3465     }
3466 
3467   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3468 
3469   if (! *loc)
3470     return FALSE;
3471 
3472   entry.gotidx = -1;
3473   entry.tls_type = tls_flag;
3474 
3475   memcpy (*loc, &entry, sizeof entry);
3476 
3477   if (tls_flag == 0)
3478     hmips->global_got_area = GGA_NORMAL;
3479 
3480   return TRUE;
3481 }
3482 
3483 /* Reserve space in G for a GOT entry containing the value of symbol
3484    SYMNDX in input bfd ABDF, plus ADDEND.  */
3485 
3486 static bfd_boolean
3487 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3488 				  struct bfd_link_info *info,
3489 				  unsigned char tls_flag)
3490 {
3491   struct mips_elf_link_hash_table *htab;
3492   struct mips_got_info *g;
3493   struct mips_got_entry entry, **loc;
3494 
3495   htab = mips_elf_hash_table (info);
3496   g = htab->got_info;
3497   BFD_ASSERT (g != NULL);
3498 
3499   entry.abfd = abfd;
3500   entry.symndx = symndx;
3501   entry.d.addend = addend;
3502   entry.tls_type = tls_flag;
3503   loc = (struct mips_got_entry **)
3504     htab_find_slot (g->got_entries, &entry, INSERT);
3505 
3506   if (*loc)
3507     {
3508       if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3509 	{
3510 	  g->tls_gotno += 2;
3511 	  (*loc)->tls_type |= tls_flag;
3512 	}
3513       else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3514 	{
3515 	  g->tls_gotno += 1;
3516 	  (*loc)->tls_type |= tls_flag;
3517 	}
3518       return TRUE;
3519     }
3520 
3521   if (tls_flag != 0)
3522     {
3523       entry.gotidx = -1;
3524       entry.tls_type = tls_flag;
3525       if (tls_flag == GOT_TLS_IE)
3526 	g->tls_gotno += 1;
3527       else if (tls_flag == GOT_TLS_GD)
3528 	g->tls_gotno += 2;
3529       else if (g->tls_ldm_offset == MINUS_ONE)
3530 	{
3531 	  g->tls_ldm_offset = MINUS_TWO;
3532 	  g->tls_gotno += 2;
3533 	}
3534     }
3535   else
3536     {
3537       entry.gotidx = g->local_gotno++;
3538       entry.tls_type = 0;
3539     }
3540 
3541   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3542 
3543   if (! *loc)
3544     return FALSE;
3545 
3546   memcpy (*loc, &entry, sizeof entry);
3547 
3548   return TRUE;
3549 }
3550 
3551 /* Return the maximum number of GOT page entries required for RANGE.  */
3552 
3553 static bfd_vma
3554 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3555 {
3556   return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3557 }
3558 
3559 /* Record that ABFD has a page relocation against symbol SYMNDX and
3560    that ADDEND is the addend for that relocation.
3561 
3562    This function creates an upper bound on the number of GOT slots
3563    required; no attempt is made to combine references to non-overridable
3564    global symbols across multiple input files.  */
3565 
3566 static bfd_boolean
3567 mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3568 				long symndx, bfd_signed_vma addend)
3569 {
3570   struct mips_elf_link_hash_table *htab;
3571   struct mips_got_info *g;
3572   struct mips_got_page_entry lookup, *entry;
3573   struct mips_got_page_range **range_ptr, *range;
3574   bfd_vma old_pages, new_pages;
3575   void **loc;
3576 
3577   htab = mips_elf_hash_table (info);
3578   g = htab->got_info;
3579   BFD_ASSERT (g != NULL);
3580 
3581   /* Find the mips_got_page_entry hash table entry for this symbol.  */
3582   lookup.abfd = abfd;
3583   lookup.symndx = symndx;
3584   loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3585   if (loc == NULL)
3586     return FALSE;
3587 
3588   /* Create a mips_got_page_entry if this is the first time we've
3589      seen the symbol.  */
3590   entry = (struct mips_got_page_entry *) *loc;
3591   if (!entry)
3592     {
3593       entry = bfd_alloc (abfd, sizeof (*entry));
3594       if (!entry)
3595 	return FALSE;
3596 
3597       entry->abfd = abfd;
3598       entry->symndx = symndx;
3599       entry->ranges = NULL;
3600       entry->num_pages = 0;
3601       *loc = entry;
3602     }
3603 
3604   /* Skip over ranges whose maximum extent cannot share a page entry
3605      with ADDEND.  */
3606   range_ptr = &entry->ranges;
3607   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3608     range_ptr = &(*range_ptr)->next;
3609 
3610   /* If we scanned to the end of the list, or found a range whose
3611      minimum extent cannot share a page entry with ADDEND, create
3612      a new singleton range.  */
3613   range = *range_ptr;
3614   if (!range || addend < range->min_addend - 0xffff)
3615     {
3616       range = bfd_alloc (abfd, sizeof (*range));
3617       if (!range)
3618 	return FALSE;
3619 
3620       range->next = *range_ptr;
3621       range->min_addend = addend;
3622       range->max_addend = addend;
3623 
3624       *range_ptr = range;
3625       entry->num_pages++;
3626       g->page_gotno++;
3627       return TRUE;
3628     }
3629 
3630   /* Remember how many pages the old range contributed.  */
3631   old_pages = mips_elf_pages_for_range (range);
3632 
3633   /* Update the ranges.  */
3634   if (addend < range->min_addend)
3635     range->min_addend = addend;
3636   else if (addend > range->max_addend)
3637     {
3638       if (range->next && addend >= range->next->min_addend - 0xffff)
3639 	{
3640 	  old_pages += mips_elf_pages_for_range (range->next);
3641 	  range->max_addend = range->next->max_addend;
3642 	  range->next = range->next->next;
3643 	}
3644       else
3645 	range->max_addend = addend;
3646     }
3647 
3648   /* Record any change in the total estimate.  */
3649   new_pages = mips_elf_pages_for_range (range);
3650   if (old_pages != new_pages)
3651     {
3652       entry->num_pages += new_pages - old_pages;
3653       g->page_gotno += new_pages - old_pages;
3654     }
3655 
3656   return TRUE;
3657 }
3658 
3659 /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
3660 
3661 static void
3662 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3663 				       unsigned int n)
3664 {
3665   asection *s;
3666   struct mips_elf_link_hash_table *htab;
3667 
3668   htab = mips_elf_hash_table (info);
3669   s = mips_elf_rel_dyn_section (info, FALSE);
3670   BFD_ASSERT (s != NULL);
3671 
3672   if (htab->is_vxworks)
3673     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3674   else
3675     {
3676       if (s->size == 0)
3677 	{
3678 	  /* Make room for a null element.  */
3679 	  s->size += MIPS_ELF_REL_SIZE (abfd);
3680 	  ++s->reloc_count;
3681 	}
3682       s->size += n * MIPS_ELF_REL_SIZE (abfd);
3683     }
3684 }
3685 
3686 /* A htab_traverse callback for GOT entries.  Set boolean *DATA to true
3687    if the GOT entry is for an indirect or warning symbol.  */
3688 
3689 static int
3690 mips_elf_check_recreate_got (void **entryp, void *data)
3691 {
3692   struct mips_got_entry *entry;
3693   bfd_boolean *must_recreate;
3694 
3695   entry = (struct mips_got_entry *) *entryp;
3696   must_recreate = (bfd_boolean *) data;
3697   if (entry->abfd != NULL && entry->symndx == -1)
3698     {
3699       struct mips_elf_link_hash_entry *h;
3700 
3701       h = entry->d.h;
3702       if (h->root.root.type == bfd_link_hash_indirect
3703 	  || h->root.root.type == bfd_link_hash_warning)
3704 	{
3705 	  *must_recreate = TRUE;
3706 	  return 0;
3707 	}
3708     }
3709   return 1;
3710 }
3711 
3712 /* A htab_traverse callback for GOT entries.  Add all entries to
3713    hash table *DATA, converting entries for indirect and warning
3714    symbols into entries for the target symbol.  Set *DATA to null
3715    on error.  */
3716 
3717 static int
3718 mips_elf_recreate_got (void **entryp, void *data)
3719 {
3720   htab_t *new_got;
3721   struct mips_got_entry *entry;
3722   void **slot;
3723 
3724   new_got = (htab_t *) data;
3725   entry = (struct mips_got_entry *) *entryp;
3726   if (entry->abfd != NULL && entry->symndx == -1)
3727     {
3728       struct mips_elf_link_hash_entry *h;
3729 
3730       h = entry->d.h;
3731       while (h->root.root.type == bfd_link_hash_indirect
3732 	     || h->root.root.type == bfd_link_hash_warning)
3733 	{
3734 	  BFD_ASSERT (h->global_got_area == GGA_NONE);
3735 	  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3736 	}
3737       entry->d.h = h;
3738     }
3739   slot = htab_find_slot (*new_got, entry, INSERT);
3740   if (slot == NULL)
3741     {
3742       *new_got = NULL;
3743       return 0;
3744     }
3745   if (*slot == NULL)
3746     *slot = entry;
3747   else
3748     free (entry);
3749   return 1;
3750 }
3751 
3752 /* If any entries in G->got_entries are for indirect or warning symbols,
3753    replace them with entries for the target symbol.  */
3754 
3755 static bfd_boolean
3756 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3757 {
3758   bfd_boolean must_recreate;
3759   htab_t new_got;
3760 
3761   must_recreate = FALSE;
3762   htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3763   if (must_recreate)
3764     {
3765       new_got = htab_create (htab_size (g->got_entries),
3766 			     mips_elf_got_entry_hash,
3767 			     mips_elf_got_entry_eq, NULL);
3768       htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3769       if (new_got == NULL)
3770 	return FALSE;
3771 
3772       /* Each entry in g->got_entries has either been copied to new_got
3773 	 or freed.  Now delete the hash table itself.  */
3774       htab_delete (g->got_entries);
3775       g->got_entries = new_got;
3776     }
3777   return TRUE;
3778 }
3779 
3780 /* A mips_elf_link_hash_traverse callback for which DATA points
3781    to a mips_got_info.  Count the number of type (3) entries.  */
3782 
3783 static int
3784 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
3785 {
3786   struct mips_got_info *g;
3787 
3788   g = (struct mips_got_info *) data;
3789   if (h->global_got_area != GGA_NONE)
3790     {
3791       if (h->root.forced_local || h->root.dynindx == -1)
3792 	{
3793 	  /* We no longer need this entry if it was only used for
3794 	     relocations; those relocations will be against the
3795 	     null or section symbol instead of H.  */
3796 	  if (h->global_got_area != GGA_RELOC_ONLY)
3797 	    g->local_gotno++;
3798 	  h->global_got_area = GGA_NONE;
3799 	}
3800       else
3801 	{
3802 	  g->global_gotno++;
3803 	  if (h->global_got_area == GGA_RELOC_ONLY)
3804 	    g->reloc_only_gotno++;
3805 	}
3806     }
3807   return 1;
3808 }
3809 
3810 /* Compute the hash value of the bfd in a bfd2got hash entry.  */
3811 
3812 static hashval_t
3813 mips_elf_bfd2got_entry_hash (const void *entry_)
3814 {
3815   const struct mips_elf_bfd2got_hash *entry
3816     = (struct mips_elf_bfd2got_hash *)entry_;
3817 
3818   return entry->bfd->id;
3819 }
3820 
3821 /* Check whether two hash entries have the same bfd.  */
3822 
3823 static int
3824 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3825 {
3826   const struct mips_elf_bfd2got_hash *e1
3827     = (const struct mips_elf_bfd2got_hash *)entry1;
3828   const struct mips_elf_bfd2got_hash *e2
3829     = (const struct mips_elf_bfd2got_hash *)entry2;
3830 
3831   return e1->bfd == e2->bfd;
3832 }
3833 
3834 /* In a multi-got link, determine the GOT to be used for IBFD.  G must
3835    be the master GOT data.  */
3836 
3837 static struct mips_got_info *
3838 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3839 {
3840   struct mips_elf_bfd2got_hash e, *p;
3841 
3842   if (! g->bfd2got)
3843     return g;
3844 
3845   e.bfd = ibfd;
3846   p = htab_find (g->bfd2got, &e);
3847   return p ? p->g : NULL;
3848 }
3849 
3850 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3851    Return NULL if an error occured.  */
3852 
3853 static struct mips_got_info *
3854 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3855 			  bfd *input_bfd)
3856 {
3857   struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3858   struct mips_got_info *g;
3859   void **bfdgotp;
3860 
3861   bfdgot_entry.bfd = input_bfd;
3862   bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3863   bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3864 
3865   if (bfdgot == NULL)
3866     {
3867       bfdgot = ((struct mips_elf_bfd2got_hash *)
3868 		bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3869       if (bfdgot == NULL)
3870 	return NULL;
3871 
3872       *bfdgotp = bfdgot;
3873 
3874       g = ((struct mips_got_info *)
3875 	   bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3876       if (g == NULL)
3877 	return NULL;
3878 
3879       bfdgot->bfd = input_bfd;
3880       bfdgot->g = g;
3881 
3882       g->global_gotsym = NULL;
3883       g->global_gotno = 0;
3884       g->reloc_only_gotno = 0;
3885       g->local_gotno = 0;
3886       g->page_gotno = 0;
3887       g->assigned_gotno = -1;
3888       g->tls_gotno = 0;
3889       g->tls_assigned_gotno = 0;
3890       g->tls_ldm_offset = MINUS_ONE;
3891       g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3892 					mips_elf_multi_got_entry_eq, NULL);
3893       if (g->got_entries == NULL)
3894 	return NULL;
3895 
3896       g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3897 					     mips_got_page_entry_eq, NULL);
3898       if (g->got_page_entries == NULL)
3899 	return NULL;
3900 
3901       g->bfd2got = NULL;
3902       g->next = NULL;
3903     }
3904 
3905   return bfdgot->g;
3906 }
3907 
3908 /* A htab_traverse callback for the entries in the master got.
3909    Create one separate got for each bfd that has entries in the global
3910    got, such that we can tell how many local and global entries each
3911    bfd requires.  */
3912 
3913 static int
3914 mips_elf_make_got_per_bfd (void **entryp, void *p)
3915 {
3916   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3917   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3918   struct mips_got_info *g;
3919 
3920   g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3921   if (g == NULL)
3922     {
3923       arg->obfd = NULL;
3924       return 0;
3925     }
3926 
3927   /* Insert the GOT entry in the bfd's got entry hash table.  */
3928   entryp = htab_find_slot (g->got_entries, entry, INSERT);
3929   if (*entryp != NULL)
3930     return 1;
3931 
3932   *entryp = entry;
3933 
3934   if (entry->tls_type)
3935     {
3936       if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3937 	g->tls_gotno += 2;
3938       if (entry->tls_type & GOT_TLS_IE)
3939 	g->tls_gotno += 1;
3940     }
3941   else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
3942     ++g->local_gotno;
3943   else
3944     ++g->global_gotno;
3945 
3946   return 1;
3947 }
3948 
3949 /* A htab_traverse callback for the page entries in the master got.
3950    Associate each page entry with the bfd's got.  */
3951 
3952 static int
3953 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3954 {
3955   struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3956   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3957   struct mips_got_info *g;
3958 
3959   g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3960   if (g == NULL)
3961     {
3962       arg->obfd = NULL;
3963       return 0;
3964     }
3965 
3966   /* Insert the GOT entry in the bfd's got entry hash table.  */
3967   entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
3968   if (*entryp != NULL)
3969     return 1;
3970 
3971   *entryp = entry;
3972   g->page_gotno += entry->num_pages;
3973   return 1;
3974 }
3975 
3976 /* Consider merging the got described by BFD2GOT with TO, using the
3977    information given by ARG.  Return -1 if this would lead to overflow,
3978    1 if they were merged successfully, and 0 if a merge failed due to
3979    lack of memory.  (These values are chosen so that nonnegative return
3980    values can be returned by a htab_traverse callback.)  */
3981 
3982 static int
3983 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
3984 			 struct mips_got_info *to,
3985 			 struct mips_elf_got_per_bfd_arg *arg)
3986 {
3987   struct mips_got_info *from = bfd2got->g;
3988   unsigned int estimate;
3989 
3990   /* Work out how many page entries we would need for the combined GOT.  */
3991   estimate = arg->max_pages;
3992   if (estimate >= from->page_gotno + to->page_gotno)
3993     estimate = from->page_gotno + to->page_gotno;
3994 
3995   /* And conservatively estimate how many local, global and TLS entries
3996      would be needed.  */
3997   estimate += (from->local_gotno
3998 	       + from->global_gotno
3999 	       + from->tls_gotno
4000 	       + to->local_gotno
4001 	       + to->global_gotno
4002 	       + to->tls_gotno);
4003 
4004   /* Bail out if the combined GOT might be too big.  */
4005   if (estimate > arg->max_count)
4006     return -1;
4007 
4008   /* Commit to the merge.  Record that TO is now the bfd for this got.  */
4009   bfd2got->g = to;
4010 
4011   /* Transfer the bfd's got information from FROM to TO.  */
4012   htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4013   if (arg->obfd == NULL)
4014     return 0;
4015 
4016   htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4017   if (arg->obfd == NULL)
4018     return 0;
4019 
4020   /* We don't have to worry about releasing memory of the actual
4021      got entries, since they're all in the master got_entries hash
4022      table anyway.  */
4023   htab_delete (from->got_entries);
4024   htab_delete (from->got_page_entries);
4025   return 1;
4026 }
4027 
4028 /* Attempt to merge gots of different input bfds.  Try to use as much
4029    as possible of the primary got, since it doesn't require explicit
4030    dynamic relocations, but don't use bfds that would reference global
4031    symbols out of the addressable range.  Failing the primary got,
4032    attempt to merge with the current got, or finish the current got
4033    and then make make the new got current.  */
4034 
4035 static int
4036 mips_elf_merge_gots (void **bfd2got_, void *p)
4037 {
4038   struct mips_elf_bfd2got_hash *bfd2got
4039     = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4040   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4041   struct mips_got_info *g;
4042   unsigned int estimate;
4043   int result;
4044 
4045   g = bfd2got->g;
4046 
4047   /* Work out the number of page, local and TLS entries.  */
4048   estimate = arg->max_pages;
4049   if (estimate > g->page_gotno)
4050     estimate = g->page_gotno;
4051   estimate += g->local_gotno + g->tls_gotno;
4052 
4053   /* We place TLS GOT entries after both locals and globals.  The globals
4054      for the primary GOT may overflow the normal GOT size limit, so be
4055      sure not to merge a GOT which requires TLS with the primary GOT in that
4056      case.  This doesn't affect non-primary GOTs.  */
4057   estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4058 
4059   if (estimate <= arg->max_count)
4060     {
4061       /* If we don't have a primary GOT, use it as
4062 	 a starting point for the primary GOT.  */
4063       if (!arg->primary)
4064 	{
4065 	  arg->primary = bfd2got->g;
4066 	  return 1;
4067 	}
4068 
4069       /* Try merging with the primary GOT.  */
4070       result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4071       if (result >= 0)
4072 	return result;
4073     }
4074 
4075   /* If we can merge with the last-created got, do it.  */
4076   if (arg->current)
4077     {
4078       result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4079       if (result >= 0)
4080 	return result;
4081     }
4082 
4083   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4084      fits; if it turns out that it doesn't, we'll get relocation
4085      overflows anyway.  */
4086   g->next = arg->current;
4087   arg->current = g;
4088 
4089   return 1;
4090 }
4091 
4092 /* Set the TLS GOT index for the GOT entry in ENTRYP.  ENTRYP's NEXT field
4093    is null iff there is just a single GOT.  */
4094 
4095 static int
4096 mips_elf_initialize_tls_index (void **entryp, void *p)
4097 {
4098   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4099   struct mips_got_info *g = p;
4100   bfd_vma next_index;
4101   unsigned char tls_type;
4102 
4103   /* We're only interested in TLS symbols.  */
4104   if (entry->tls_type == 0)
4105     return 1;
4106 
4107   next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4108 
4109   if (entry->symndx == -1 && g->next == NULL)
4110     {
4111       /* A type (3) got entry in the single-GOT case.  We use the symbol's
4112 	 hash table entry to track its index.  */
4113       if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4114 	return 1;
4115       entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4116       entry->d.h->tls_got_offset = next_index;
4117       tls_type = entry->d.h->tls_type;
4118     }
4119   else
4120     {
4121       if (entry->tls_type & GOT_TLS_LDM)
4122 	{
4123 	  /* There are separate mips_got_entry objects for each input bfd
4124 	     that requires an LDM entry.  Make sure that all LDM entries in
4125 	     a GOT resolve to the same index.  */
4126 	  if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4127 	    {
4128 	      entry->gotidx = g->tls_ldm_offset;
4129 	      return 1;
4130 	    }
4131 	  g->tls_ldm_offset = next_index;
4132 	}
4133       entry->gotidx = next_index;
4134       tls_type = entry->tls_type;
4135     }
4136 
4137   /* Account for the entries we've just allocated.  */
4138   if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4139     g->tls_assigned_gotno += 2;
4140   if (tls_type & GOT_TLS_IE)
4141     g->tls_assigned_gotno += 1;
4142 
4143   return 1;
4144 }
4145 
4146 /* If passed a NULL mips_got_info in the argument, set the marker used
4147    to tell whether a global symbol needs a got entry (in the primary
4148    got) to the given VALUE.
4149 
4150    If passed a pointer G to a mips_got_info in the argument (it must
4151    not be the primary GOT), compute the offset from the beginning of
4152    the (primary) GOT section to the entry in G corresponding to the
4153    global symbol.  G's assigned_gotno must contain the index of the
4154    first available global GOT entry in G.  VALUE must contain the size
4155    of a GOT entry in bytes.  For each global GOT entry that requires a
4156    dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4157    marked as not eligible for lazy resolution through a function
4158    stub.  */
4159 static int
4160 mips_elf_set_global_got_offset (void **entryp, void *p)
4161 {
4162   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4163   struct mips_elf_set_global_got_offset_arg *arg
4164     = (struct mips_elf_set_global_got_offset_arg *)p;
4165   struct mips_got_info *g = arg->g;
4166 
4167   if (g && entry->tls_type != GOT_NORMAL)
4168     arg->needed_relocs +=
4169       mips_tls_got_relocs (arg->info, entry->tls_type,
4170 			   entry->symndx == -1 ? &entry->d.h->root : NULL);
4171 
4172   if (entry->abfd != NULL
4173       && entry->symndx == -1
4174       && entry->d.h->global_got_area != GGA_NONE)
4175     {
4176       if (g)
4177 	{
4178 	  BFD_ASSERT (g->global_gotsym == NULL);
4179 
4180 	  entry->gotidx = arg->value * (long) g->assigned_gotno++;
4181 	  if (arg->info->shared
4182 	      || (elf_hash_table (arg->info)->dynamic_sections_created
4183 		  && entry->d.h->root.def_dynamic
4184 		  && !entry->d.h->root.def_regular))
4185 	    ++arg->needed_relocs;
4186 	}
4187       else
4188 	entry->d.h->global_got_area = arg->value;
4189     }
4190 
4191   return 1;
4192 }
4193 
4194 /* A htab_traverse callback for GOT entries for which DATA is the
4195    bfd_link_info.  Forbid any global symbols from having traditional
4196    lazy-binding stubs.  */
4197 
4198 static int
4199 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4200 {
4201   struct bfd_link_info *info;
4202   struct mips_elf_link_hash_table *htab;
4203   struct mips_got_entry *entry;
4204 
4205   entry = (struct mips_got_entry *) *entryp;
4206   info = (struct bfd_link_info *) data;
4207   htab = mips_elf_hash_table (info);
4208   if (entry->abfd != NULL
4209       && entry->symndx == -1
4210       && entry->d.h->needs_lazy_stub)
4211     {
4212       entry->d.h->needs_lazy_stub = FALSE;
4213       htab->lazy_stub_count--;
4214     }
4215 
4216   return 1;
4217 }
4218 
4219 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4220    the primary GOT.  */
4221 static bfd_vma
4222 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4223 {
4224   if (g->bfd2got == NULL)
4225     return 0;
4226 
4227   g = mips_elf_got_for_ibfd (g, ibfd);
4228   if (! g)
4229     return 0;
4230 
4231   BFD_ASSERT (g->next);
4232 
4233   g = g->next;
4234 
4235   return (g->local_gotno + g->global_gotno + g->tls_gotno)
4236     * MIPS_ELF_GOT_SIZE (abfd);
4237 }
4238 
4239 /* Turn a single GOT that is too big for 16-bit addressing into
4240    a sequence of GOTs, each one 16-bit addressable.  */
4241 
4242 static bfd_boolean
4243 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4244 		    asection *got, bfd_size_type pages)
4245 {
4246   struct mips_elf_link_hash_table *htab;
4247   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4248   struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
4249   struct mips_got_info *g, *gg;
4250   unsigned int assign, needed_relocs;
4251   bfd *dynobj;
4252 
4253   dynobj = elf_hash_table (info)->dynobj;
4254   htab = mips_elf_hash_table (info);
4255   g = htab->got_info;
4256   g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
4257 				mips_elf_bfd2got_entry_eq, NULL);
4258   if (g->bfd2got == NULL)
4259     return FALSE;
4260 
4261   got_per_bfd_arg.bfd2got = g->bfd2got;
4262   got_per_bfd_arg.obfd = abfd;
4263   got_per_bfd_arg.info = info;
4264 
4265   /* Count how many GOT entries each input bfd requires, creating a
4266      map from bfd to got info while at that.  */
4267   htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4268   if (got_per_bfd_arg.obfd == NULL)
4269     return FALSE;
4270 
4271   /* Also count how many page entries each input bfd requires.  */
4272   htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4273 		 &got_per_bfd_arg);
4274   if (got_per_bfd_arg.obfd == NULL)
4275     return FALSE;
4276 
4277   got_per_bfd_arg.current = NULL;
4278   got_per_bfd_arg.primary = NULL;
4279   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4280 				/ MIPS_ELF_GOT_SIZE (abfd))
4281 			       - htab->reserved_gotno);
4282   got_per_bfd_arg.max_pages = pages;
4283   /* The number of globals that will be included in the primary GOT.
4284      See the calls to mips_elf_set_global_got_offset below for more
4285      information.  */
4286   got_per_bfd_arg.global_count = g->global_gotno;
4287 
4288   /* Try to merge the GOTs of input bfds together, as long as they
4289      don't seem to exceed the maximum GOT size, choosing one of them
4290      to be the primary GOT.  */
4291   htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4292   if (got_per_bfd_arg.obfd == NULL)
4293     return FALSE;
4294 
4295   /* If we do not find any suitable primary GOT, create an empty one.  */
4296   if (got_per_bfd_arg.primary == NULL)
4297     {
4298       g->next = (struct mips_got_info *)
4299 	bfd_alloc (abfd, sizeof (struct mips_got_info));
4300       if (g->next == NULL)
4301 	return FALSE;
4302 
4303       g->next->global_gotsym = NULL;
4304       g->next->global_gotno = 0;
4305       g->next->reloc_only_gotno = 0;
4306       g->next->local_gotno = 0;
4307       g->next->page_gotno = 0;
4308       g->next->tls_gotno = 0;
4309       g->next->assigned_gotno = 0;
4310       g->next->tls_assigned_gotno = 0;
4311       g->next->tls_ldm_offset = MINUS_ONE;
4312       g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4313 					      mips_elf_multi_got_entry_eq,
4314 					      NULL);
4315       if (g->next->got_entries == NULL)
4316 	return FALSE;
4317       g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4318 						   mips_got_page_entry_eq,
4319 						   NULL);
4320       if (g->next->got_page_entries == NULL)
4321 	return FALSE;
4322       g->next->bfd2got = NULL;
4323     }
4324   else
4325     g->next = got_per_bfd_arg.primary;
4326   g->next->next = got_per_bfd_arg.current;
4327 
4328   /* GG is now the master GOT, and G is the primary GOT.  */
4329   gg = g;
4330   g = g->next;
4331 
4332   /* Map the output bfd to the primary got.  That's what we're going
4333      to use for bfds that use GOT16 or GOT_PAGE relocations that we
4334      didn't mark in check_relocs, and we want a quick way to find it.
4335      We can't just use gg->next because we're going to reverse the
4336      list.  */
4337   {
4338     struct mips_elf_bfd2got_hash *bfdgot;
4339     void **bfdgotp;
4340 
4341     bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4342       (abfd, sizeof (struct mips_elf_bfd2got_hash));
4343 
4344     if (bfdgot == NULL)
4345       return FALSE;
4346 
4347     bfdgot->bfd = abfd;
4348     bfdgot->g = g;
4349     bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4350 
4351     BFD_ASSERT (*bfdgotp == NULL);
4352     *bfdgotp = bfdgot;
4353   }
4354 
4355   /* Every symbol that is referenced in a dynamic relocation must be
4356      present in the primary GOT, so arrange for them to appear after
4357      those that are actually referenced.  */
4358   gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4359   g->global_gotno = gg->global_gotno;
4360 
4361   set_got_offset_arg.g = NULL;
4362   set_got_offset_arg.value = GGA_RELOC_ONLY;
4363   htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4364 		 &set_got_offset_arg);
4365   set_got_offset_arg.value = GGA_NORMAL;
4366   htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4367 		 &set_got_offset_arg);
4368 
4369   /* Now go through the GOTs assigning them offset ranges.
4370      [assigned_gotno, local_gotno[ will be set to the range of local
4371      entries in each GOT.  We can then compute the end of a GOT by
4372      adding local_gotno to global_gotno.  We reverse the list and make
4373      it circular since then we'll be able to quickly compute the
4374      beginning of a GOT, by computing the end of its predecessor.  To
4375      avoid special cases for the primary GOT, while still preserving
4376      assertions that are valid for both single- and multi-got links,
4377      we arrange for the main got struct to have the right number of
4378      global entries, but set its local_gotno such that the initial
4379      offset of the primary GOT is zero.  Remember that the primary GOT
4380      will become the last item in the circular linked list, so it
4381      points back to the master GOT.  */
4382   gg->local_gotno = -g->global_gotno;
4383   gg->global_gotno = g->global_gotno;
4384   gg->tls_gotno = 0;
4385   assign = 0;
4386   gg->next = gg;
4387 
4388   do
4389     {
4390       struct mips_got_info *gn;
4391 
4392       assign += htab->reserved_gotno;
4393       g->assigned_gotno = assign;
4394       g->local_gotno += assign;
4395       g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4396       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4397 
4398       /* Take g out of the direct list, and push it onto the reversed
4399 	 list that gg points to.  g->next is guaranteed to be nonnull after
4400 	 this operation, as required by mips_elf_initialize_tls_index. */
4401       gn = g->next;
4402       g->next = gg->next;
4403       gg->next = g;
4404 
4405       /* Set up any TLS entries.  We always place the TLS entries after
4406 	 all non-TLS entries.  */
4407       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4408       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
4409 
4410       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
4411       g = gn;
4412 
4413       /* Forbid global symbols in every non-primary GOT from having
4414 	 lazy-binding stubs.  */
4415       if (g)
4416 	htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4417     }
4418   while (g);
4419 
4420   got->size = (gg->next->local_gotno
4421 	       + gg->next->global_gotno
4422 	       + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4423 
4424   needed_relocs = 0;
4425   set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4426   set_got_offset_arg.info = info;
4427   for (g = gg->next; g && g->next != gg; g = g->next)
4428     {
4429       unsigned int save_assign;
4430 
4431       /* Assign offsets to global GOT entries.  */
4432       save_assign = g->assigned_gotno;
4433       g->assigned_gotno = g->local_gotno;
4434       set_got_offset_arg.g = g;
4435       set_got_offset_arg.needed_relocs = 0;
4436       htab_traverse (g->got_entries,
4437 		     mips_elf_set_global_got_offset,
4438 		     &set_got_offset_arg);
4439       needed_relocs += set_got_offset_arg.needed_relocs;
4440       BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4441 
4442       g->assigned_gotno = save_assign;
4443       if (info->shared)
4444 	{
4445 	  needed_relocs += g->local_gotno - g->assigned_gotno;
4446 	  BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4447 		      + g->next->global_gotno
4448 		      + g->next->tls_gotno
4449 		      + htab->reserved_gotno);
4450 	}
4451     }
4452 
4453   if (needed_relocs)
4454     mips_elf_allocate_dynamic_relocations (dynobj, info,
4455 					   needed_relocs);
4456 
4457   return TRUE;
4458 }
4459 
4460 
4461 /* Returns the first relocation of type r_type found, beginning with
4462    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
4463 
4464 static const Elf_Internal_Rela *
4465 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4466 			  const Elf_Internal_Rela *relocation,
4467 			  const Elf_Internal_Rela *relend)
4468 {
4469   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4470 
4471   while (relocation < relend)
4472     {
4473       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4474 	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4475 	return relocation;
4476 
4477       ++relocation;
4478     }
4479 
4480   /* We didn't find it.  */
4481   return NULL;
4482 }
4483 
4484 /* Return whether a relocation is against a local symbol.  */
4485 
4486 static bfd_boolean
4487 mips_elf_local_relocation_p (bfd *input_bfd,
4488 			     const Elf_Internal_Rela *relocation,
4489 			     asection **local_sections,
4490 			     bfd_boolean check_forced)
4491 {
4492   unsigned long r_symndx;
4493   Elf_Internal_Shdr *symtab_hdr;
4494   struct mips_elf_link_hash_entry *h;
4495   size_t extsymoff;
4496 
4497   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4498   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4499   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4500 
4501   if (r_symndx < extsymoff)
4502     return TRUE;
4503   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4504     return TRUE;
4505 
4506   if (check_forced)
4507     {
4508       /* Look up the hash table to check whether the symbol
4509  	 was forced local.  */
4510       h = (struct mips_elf_link_hash_entry *)
4511 	elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4512       /* Find the real hash-table entry for this symbol.  */
4513       while (h->root.root.type == bfd_link_hash_indirect
4514  	     || h->root.root.type == bfd_link_hash_warning)
4515 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4516       if (h->root.forced_local)
4517 	return TRUE;
4518     }
4519 
4520   return FALSE;
4521 }
4522 
4523 /* Sign-extend VALUE, which has the indicated number of BITS.  */
4524 
4525 bfd_vma
4526 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4527 {
4528   if (value & ((bfd_vma) 1 << (bits - 1)))
4529     /* VALUE is negative.  */
4530     value |= ((bfd_vma) - 1) << bits;
4531 
4532   return value;
4533 }
4534 
4535 /* Return non-zero if the indicated VALUE has overflowed the maximum
4536    range expressible by a signed number with the indicated number of
4537    BITS.  */
4538 
4539 static bfd_boolean
4540 mips_elf_overflow_p (bfd_vma value, int bits)
4541 {
4542   bfd_signed_vma svalue = (bfd_signed_vma) value;
4543 
4544   if (svalue > (1 << (bits - 1)) - 1)
4545     /* The value is too big.  */
4546     return TRUE;
4547   else if (svalue < -(1 << (bits - 1)))
4548     /* The value is too small.  */
4549     return TRUE;
4550 
4551   /* All is well.  */
4552   return FALSE;
4553 }
4554 
4555 /* Calculate the %high function.  */
4556 
4557 static bfd_vma
4558 mips_elf_high (bfd_vma value)
4559 {
4560   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4561 }
4562 
4563 /* Calculate the %higher function.  */
4564 
4565 static bfd_vma
4566 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4567 {
4568 #ifdef BFD64
4569   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4570 #else
4571   abort ();
4572   return MINUS_ONE;
4573 #endif
4574 }
4575 
4576 /* Calculate the %highest function.  */
4577 
4578 static bfd_vma
4579 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4580 {
4581 #ifdef BFD64
4582   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4583 #else
4584   abort ();
4585   return MINUS_ONE;
4586 #endif
4587 }
4588 
4589 /* Create the .compact_rel section.  */
4590 
4591 static bfd_boolean
4592 mips_elf_create_compact_rel_section
4593   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4594 {
4595   flagword flags;
4596   register asection *s;
4597 
4598   if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4599     {
4600       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4601 	       | SEC_READONLY);
4602 
4603       s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4604       if (s == NULL
4605 	  || ! bfd_set_section_alignment (abfd, s,
4606 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4607 	return FALSE;
4608 
4609       s->size = sizeof (Elf32_External_compact_rel);
4610     }
4611 
4612   return TRUE;
4613 }
4614 
4615 /* Create the .got section to hold the global offset table.  */
4616 
4617 static bfd_boolean
4618 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4619 {
4620   flagword flags;
4621   register asection *s;
4622   struct elf_link_hash_entry *h;
4623   struct bfd_link_hash_entry *bh;
4624   struct mips_got_info *g;
4625   bfd_size_type amt;
4626   struct mips_elf_link_hash_table *htab;
4627 
4628   htab = mips_elf_hash_table (info);
4629 
4630   /* This function may be called more than once.  */
4631   if (htab->sgot)
4632     return TRUE;
4633 
4634   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4635 	   | SEC_LINKER_CREATED);
4636 
4637   /* We have to use an alignment of 2**4 here because this is hardcoded
4638      in the function stub generation and in the linker script.  */
4639   s = bfd_make_section_with_flags (abfd, ".got", flags);
4640   if (s == NULL
4641       || ! bfd_set_section_alignment (abfd, s, 4))
4642     return FALSE;
4643   htab->sgot = s;
4644 
4645   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
4646      linker script because we don't want to define the symbol if we
4647      are not creating a global offset table.  */
4648   bh = NULL;
4649   if (! (_bfd_generic_link_add_one_symbol
4650 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4651 	  0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4652     return FALSE;
4653 
4654   h = (struct elf_link_hash_entry *) bh;
4655   h->non_elf = 0;
4656   h->def_regular = 1;
4657   h->type = STT_OBJECT;
4658   elf_hash_table (info)->hgot = h;
4659 
4660   if (info->shared
4661       && ! bfd_elf_link_record_dynamic_symbol (info, h))
4662     return FALSE;
4663 
4664   amt = sizeof (struct mips_got_info);
4665   g = bfd_alloc (abfd, amt);
4666   if (g == NULL)
4667     return FALSE;
4668   g->global_gotsym = NULL;
4669   g->global_gotno = 0;
4670   g->reloc_only_gotno = 0;
4671   g->tls_gotno = 0;
4672   g->local_gotno = 0;
4673   g->page_gotno = 0;
4674   g->assigned_gotno = 0;
4675   g->bfd2got = NULL;
4676   g->next = NULL;
4677   g->tls_ldm_offset = MINUS_ONE;
4678   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4679 				    mips_elf_got_entry_eq, NULL);
4680   if (g->got_entries == NULL)
4681     return FALSE;
4682   g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4683 					 mips_got_page_entry_eq, NULL);
4684   if (g->got_page_entries == NULL)
4685     return FALSE;
4686   htab->got_info = g;
4687   mips_elf_section_data (s)->elf.this_hdr.sh_flags
4688     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4689 
4690   /* We also need a .got.plt section when generating PLTs.  */
4691   s = bfd_make_section_with_flags (abfd, ".got.plt",
4692 				   SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4693 				   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4694   if (s == NULL)
4695     return FALSE;
4696   htab->sgotplt = s;
4697 
4698   return TRUE;
4699 }
4700 
4701 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4702    __GOTT_INDEX__ symbols.  These symbols are only special for
4703    shared objects; they are not used in executables.  */
4704 
4705 static bfd_boolean
4706 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4707 {
4708   return (mips_elf_hash_table (info)->is_vxworks
4709 	  && info->shared
4710 	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4711 	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4712 }
4713 
4714 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4715    require an la25 stub.  See also mips_elf_local_pic_function_p,
4716    which determines whether the destination function ever requires a
4717    stub.  */
4718 
4719 static bfd_boolean
4720 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4721 {
4722   /* We specifically ignore branches and jumps from EF_PIC objects,
4723      where the onus is on the compiler or programmer to perform any
4724      necessary initialization of $25.  Sometimes such initialization
4725      is unnecessary; for example, -mno-shared functions do not use
4726      the incoming value of $25, and may therefore be called directly.  */
4727   if (PIC_OBJECT_P (input_bfd))
4728     return FALSE;
4729 
4730   switch (r_type)
4731     {
4732     case R_MIPS_26:
4733     case R_MIPS_PC16:
4734     case R_MIPS16_26:
4735       return TRUE;
4736 
4737     default:
4738       return FALSE;
4739     }
4740 }
4741 
4742 /* Calculate the value produced by the RELOCATION (which comes from
4743    the INPUT_BFD).  The ADDEND is the addend to use for this
4744    RELOCATION; RELOCATION->R_ADDEND is ignored.
4745 
4746    The result of the relocation calculation is stored in VALUEP.
4747    REQUIRE_JALXP indicates whether or not the opcode used with this
4748    relocation must be JALX.
4749 
4750    This function returns bfd_reloc_continue if the caller need take no
4751    further action regarding this relocation, bfd_reloc_notsupported if
4752    something goes dramatically wrong, bfd_reloc_overflow if an
4753    overflow occurs, and bfd_reloc_ok to indicate success.  */
4754 
4755 static bfd_reloc_status_type
4756 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4757 			       asection *input_section,
4758 			       struct bfd_link_info *info,
4759 			       const Elf_Internal_Rela *relocation,
4760 			       bfd_vma addend, reloc_howto_type *howto,
4761 			       Elf_Internal_Sym *local_syms,
4762 			       asection **local_sections, bfd_vma *valuep,
4763 			       const char **namep, bfd_boolean *require_jalxp,
4764 			       bfd_boolean save_addend)
4765 {
4766   /* The eventual value we will return.  */
4767   bfd_vma value;
4768   /* The address of the symbol against which the relocation is
4769      occurring.  */
4770   bfd_vma symbol = 0;
4771   /* The final GP value to be used for the relocatable, executable, or
4772      shared object file being produced.  */
4773   bfd_vma gp;
4774   /* The place (section offset or address) of the storage unit being
4775      relocated.  */
4776   bfd_vma p;
4777   /* The value of GP used to create the relocatable object.  */
4778   bfd_vma gp0;
4779   /* The offset into the global offset table at which the address of
4780      the relocation entry symbol, adjusted by the addend, resides
4781      during execution.  */
4782   bfd_vma g = MINUS_ONE;
4783   /* The section in which the symbol referenced by the relocation is
4784      located.  */
4785   asection *sec = NULL;
4786   struct mips_elf_link_hash_entry *h = NULL;
4787   /* TRUE if the symbol referred to by this relocation is a local
4788      symbol.  */
4789   bfd_boolean local_p, was_local_p;
4790   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
4791   bfd_boolean gp_disp_p = FALSE;
4792   /* TRUE if the symbol referred to by this relocation is
4793      "__gnu_local_gp".  */
4794   bfd_boolean gnu_local_gp_p = FALSE;
4795   Elf_Internal_Shdr *symtab_hdr;
4796   size_t extsymoff;
4797   unsigned long r_symndx;
4798   int r_type;
4799   /* TRUE if overflow occurred during the calculation of the
4800      relocation value.  */
4801   bfd_boolean overflowed_p;
4802   /* TRUE if this relocation refers to a MIPS16 function.  */
4803   bfd_boolean target_is_16_bit_code_p = FALSE;
4804   struct mips_elf_link_hash_table *htab;
4805   bfd *dynobj;
4806 
4807   dynobj = elf_hash_table (info)->dynobj;
4808   htab = mips_elf_hash_table (info);
4809 
4810   /* Parse the relocation.  */
4811   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4812   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4813   p = (input_section->output_section->vma
4814        + input_section->output_offset
4815        + relocation->r_offset);
4816 
4817   /* Assume that there will be no overflow.  */
4818   overflowed_p = FALSE;
4819 
4820   /* Figure out whether or not the symbol is local, and get the offset
4821      used in the array of hash table entries.  */
4822   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4823   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4824 					 local_sections, FALSE);
4825   was_local_p = local_p;
4826   if (! elf_bad_symtab (input_bfd))
4827     extsymoff = symtab_hdr->sh_info;
4828   else
4829     {
4830       /* The symbol table does not follow the rule that local symbols
4831 	 must come before globals.  */
4832       extsymoff = 0;
4833     }
4834 
4835   /* Figure out the value of the symbol.  */
4836   if (local_p)
4837     {
4838       Elf_Internal_Sym *sym;
4839 
4840       sym = local_syms + r_symndx;
4841       sec = local_sections[r_symndx];
4842 
4843       symbol = sec->output_section->vma + sec->output_offset;
4844       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4845 	  || (sec->flags & SEC_MERGE))
4846 	symbol += sym->st_value;
4847       if ((sec->flags & SEC_MERGE)
4848 	  && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4849 	{
4850 	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4851 	  addend -= symbol;
4852 	  addend += sec->output_section->vma + sec->output_offset;
4853 	}
4854 
4855       /* MIPS16 text labels should be treated as odd.  */
4856       if (ELF_ST_IS_MIPS16 (sym->st_other))
4857 	++symbol;
4858 
4859       /* Record the name of this symbol, for our caller.  */
4860       *namep = bfd_elf_string_from_elf_section (input_bfd,
4861 						symtab_hdr->sh_link,
4862 						sym->st_name);
4863       if (*namep == '\0')
4864 	*namep = bfd_section_name (input_bfd, sec);
4865 
4866       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4867     }
4868   else
4869     {
4870       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
4871 
4872       /* For global symbols we look up the symbol in the hash-table.  */
4873       h = ((struct mips_elf_link_hash_entry *)
4874 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4875       /* Find the real hash-table entry for this symbol.  */
4876       while (h->root.root.type == bfd_link_hash_indirect
4877 	     || h->root.root.type == bfd_link_hash_warning)
4878 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4879 
4880       /* Record the name of this symbol, for our caller.  */
4881       *namep = h->root.root.root.string;
4882 
4883       /* See if this is the special _gp_disp symbol.  Note that such a
4884 	 symbol must always be a global symbol.  */
4885       if (strcmp (*namep, "_gp_disp") == 0
4886 	  && ! NEWABI_P (input_bfd))
4887 	{
4888 	  /* Relocations against _gp_disp are permitted only with
4889 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
4890 	  if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4891 	    return bfd_reloc_notsupported;
4892 
4893 	  gp_disp_p = TRUE;
4894 	}
4895       /* See if this is the special _gp symbol.  Note that such a
4896 	 symbol must always be a global symbol.  */
4897       else if (strcmp (*namep, "__gnu_local_gp") == 0)
4898 	gnu_local_gp_p = TRUE;
4899 
4900 
4901       /* If this symbol is defined, calculate its address.  Note that
4902 	 _gp_disp is a magic symbol, always implicitly defined by the
4903 	 linker, so it's inappropriate to check to see whether or not
4904 	 its defined.  */
4905       else if ((h->root.root.type == bfd_link_hash_defined
4906 		|| h->root.root.type == bfd_link_hash_defweak)
4907 	       && h->root.root.u.def.section)
4908 	{
4909 	  sec = h->root.root.u.def.section;
4910 	  if (sec->output_section)
4911 	    symbol = (h->root.root.u.def.value
4912 		      + sec->output_section->vma
4913 		      + sec->output_offset);
4914 	  else
4915 	    symbol = h->root.root.u.def.value;
4916 	}
4917       else if (h->root.root.type == bfd_link_hash_undefweak)
4918 	/* We allow relocations against undefined weak symbols, giving
4919 	   it the value zero, so that you can undefined weak functions
4920 	   and check to see if they exist by looking at their
4921 	   addresses.  */
4922 	symbol = 0;
4923       else if (info->unresolved_syms_in_objects == RM_IGNORE
4924 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4925 	symbol = 0;
4926       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4927 		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
4928 	{
4929 	  /* If this is a dynamic link, we should have created a
4930 	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4931 	     in in _bfd_mips_elf_create_dynamic_sections.
4932 	     Otherwise, we should define the symbol with a value of 0.
4933 	     FIXME: It should probably get into the symbol table
4934 	     somehow as well.  */
4935 	  BFD_ASSERT (! info->shared);
4936 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4937 	  symbol = 0;
4938 	}
4939       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4940 	{
4941 	  /* This is an optional symbol - an Irix specific extension to the
4942 	     ELF spec.  Ignore it for now.
4943 	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
4944 	     than simply ignoring them, but we do not handle this for now.
4945 	     For information see the "64-bit ELF Object File Specification"
4946 	     which is available from here:
4947 	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
4948 	  symbol = 0;
4949 	}
4950       else
4951 	{
4952 	  if (! ((*info->callbacks->undefined_symbol)
4953 		 (info, h->root.root.root.string, input_bfd,
4954 		  input_section, relocation->r_offset,
4955 		  (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4956 		   || ELF_ST_VISIBILITY (h->root.other))))
4957 	    return bfd_reloc_undefined;
4958 	  symbol = 0;
4959 	}
4960 
4961       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
4962     }
4963 
4964   /* If this is a reference to a 16-bit function with a stub, we need
4965      to redirect the relocation to the stub unless:
4966 
4967      (a) the relocation is for a MIPS16 JAL;
4968 
4969      (b) the relocation is for a MIPS16 PIC call, and there are no
4970 	 non-MIPS16 uses of the GOT slot; or
4971 
4972      (c) the section allows direct references to MIPS16 functions.  */
4973   if (r_type != R_MIPS16_26
4974       && !info->relocatable
4975       && ((h != NULL
4976 	   && h->fn_stub != NULL
4977 	   && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
4978 	  || (local_p
4979 	      && elf_tdata (input_bfd)->local_stubs != NULL
4980 	      && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4981       && !section_allows_mips16_refs_p (input_section))
4982     {
4983       /* This is a 32- or 64-bit call to a 16-bit function.  We should
4984 	 have already noticed that we were going to need the
4985 	 stub.  */
4986       if (local_p)
4987 	sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
4988       else
4989 	{
4990 	  BFD_ASSERT (h->need_fn_stub);
4991 	  sec = h->fn_stub;
4992 	}
4993 
4994       symbol = sec->output_section->vma + sec->output_offset;
4995       /* The target is 16-bit, but the stub isn't.  */
4996       target_is_16_bit_code_p = FALSE;
4997     }
4998   /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
4999      need to redirect the call to the stub.  Note that we specifically
5000      exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5001      use an indirect stub instead.  */
5002   else if (r_type == R_MIPS16_26 && !info->relocatable
5003 	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5004 	       || (local_p
5005 		   && elf_tdata (input_bfd)->local_call_stubs != NULL
5006 		   && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5007 	   && !target_is_16_bit_code_p)
5008     {
5009       if (local_p)
5010 	sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5011       else
5012 	{
5013 	  /* If both call_stub and call_fp_stub are defined, we can figure
5014 	     out which one to use by checking which one appears in the input
5015 	     file.  */
5016 	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
5017 	    {
5018 	      asection *o;
5019 
5020 	      sec = NULL;
5021 	      for (o = input_bfd->sections; o != NULL; o = o->next)
5022 		{
5023 		  if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5024 		    {
5025 		      sec = h->call_fp_stub;
5026 		      break;
5027 		    }
5028 		}
5029 	      if (sec == NULL)
5030 		sec = h->call_stub;
5031 	    }
5032 	  else if (h->call_stub != NULL)
5033 	    sec = h->call_stub;
5034 	  else
5035 	    sec = h->call_fp_stub;
5036   	}
5037 
5038       BFD_ASSERT (sec->size > 0);
5039       symbol = sec->output_section->vma + sec->output_offset;
5040     }
5041   /* If this is a direct call to a PIC function, redirect to the
5042      non-PIC stub.  */
5043   else if (h != NULL && h->la25_stub
5044 	   && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5045     symbol = (h->la25_stub->stub_section->output_section->vma
5046 	      + h->la25_stub->stub_section->output_offset
5047 	      + h->la25_stub->offset);
5048 
5049   /* Calls from 16-bit code to 32-bit code and vice versa require the
5050      special jalx instruction.  */
5051   *require_jalxp = (!info->relocatable
5052                     && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
5053                         || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
5054 
5055   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5056 					 local_sections, TRUE);
5057 
5058   gp0 = _bfd_get_gp_value (input_bfd);
5059   gp = _bfd_get_gp_value (abfd);
5060   if (htab->got_info)
5061     gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5062 
5063   if (gnu_local_gp_p)
5064     symbol = gp;
5065 
5066   /* If we haven't already determined the GOT offset, oand we're going
5067      to need it, get it now.  */
5068   switch (r_type)
5069     {
5070     case R_MIPS_GOT_PAGE:
5071     case R_MIPS_GOT_OFST:
5072       /* We need to decay to GOT_DISP/addend if the symbol doesn't
5073 	 bind locally.  */
5074       local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
5075       if (local_p || r_type == R_MIPS_GOT_OFST)
5076 	break;
5077       /* Fall through.  */
5078 
5079     case R_MIPS16_CALL16:
5080     case R_MIPS16_GOT16:
5081     case R_MIPS_CALL16:
5082     case R_MIPS_GOT16:
5083     case R_MIPS_GOT_DISP:
5084     case R_MIPS_GOT_HI16:
5085     case R_MIPS_CALL_HI16:
5086     case R_MIPS_GOT_LO16:
5087     case R_MIPS_CALL_LO16:
5088     case R_MIPS_TLS_GD:
5089     case R_MIPS_TLS_GOTTPREL:
5090     case R_MIPS_TLS_LDM:
5091       /* Find the index into the GOT where this value is located.  */
5092       if (r_type == R_MIPS_TLS_LDM)
5093 	{
5094 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
5095 					0, 0, NULL, r_type);
5096 	  if (g == MINUS_ONE)
5097 	    return bfd_reloc_outofrange;
5098 	}
5099       else if (!local_p)
5100 	{
5101 	  /* On VxWorks, CALL relocations should refer to the .got.plt
5102 	     entry, which is initialized to point at the PLT stub.  */
5103 	  if (htab->is_vxworks
5104 	      && (r_type == R_MIPS_CALL_HI16
5105 		  || r_type == R_MIPS_CALL_LO16
5106 		  || call16_reloc_p (r_type)))
5107 	    {
5108 	      BFD_ASSERT (addend == 0);
5109 	      BFD_ASSERT (h->root.needs_plt);
5110 	      g = mips_elf_gotplt_index (info, &h->root);
5111 	    }
5112 	  else
5113 	    {
5114 	      /* GOT_PAGE may take a non-zero addend, that is ignored in a
5115 		 GOT_PAGE relocation that decays to GOT_DISP because the
5116 		 symbol turns out to be global.  The addend is then added
5117 		 as GOT_OFST.  */
5118 	      BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
5119 	      g = mips_elf_global_got_index (dynobj, input_bfd,
5120 					     &h->root, r_type, info);
5121 	      if (h->tls_type == GOT_NORMAL
5122 		  && (! elf_hash_table(info)->dynamic_sections_created
5123 		      || (info->shared
5124 			  && (info->symbolic || h->root.forced_local)
5125 			  && h->root.def_regular)))
5126 		/* This is a static link or a -Bsymbolic link.  The
5127 		   symbol is defined locally, or was forced to be local.
5128 		   We must initialize this entry in the GOT.  */
5129 		MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5130 	    }
5131 	}
5132       else if (!htab->is_vxworks
5133 	       && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5134 	/* The calculation below does not involve "g".  */
5135 	break;
5136       else
5137 	{
5138 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
5139 					symbol + addend, r_symndx, h, r_type);
5140 	  if (g == MINUS_ONE)
5141 	    return bfd_reloc_outofrange;
5142 	}
5143 
5144       /* Convert GOT indices to actual offsets.  */
5145       g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5146       break;
5147     }
5148 
5149   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5150      symbols are resolved by the loader.  Add them to .rela.dyn.  */
5151   if (h != NULL && is_gott_symbol (info, &h->root))
5152     {
5153       Elf_Internal_Rela outrel;
5154       bfd_byte *loc;
5155       asection *s;
5156 
5157       s = mips_elf_rel_dyn_section (info, FALSE);
5158       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5159 
5160       outrel.r_offset = (input_section->output_section->vma
5161 			 + input_section->output_offset
5162 			 + relocation->r_offset);
5163       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5164       outrel.r_addend = addend;
5165       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5166 
5167       /* If we've written this relocation for a readonly section,
5168 	 we need to set DF_TEXTREL again, so that we do not delete the
5169 	 DT_TEXTREL tag.  */
5170       if (MIPS_ELF_READONLY_SECTION (input_section))
5171 	info->flags |= DF_TEXTREL;
5172 
5173       *valuep = 0;
5174       return bfd_reloc_ok;
5175     }
5176 
5177   /* Figure out what kind of relocation is being performed.  */
5178   switch (r_type)
5179     {
5180     case R_MIPS_NONE:
5181       return bfd_reloc_continue;
5182 
5183     case R_MIPS_16:
5184       value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5185       overflowed_p = mips_elf_overflow_p (value, 16);
5186       break;
5187 
5188     case R_MIPS_32:
5189     case R_MIPS_REL32:
5190     case R_MIPS_64:
5191       if ((info->shared
5192 	   || (htab->root.dynamic_sections_created
5193 	       && h != NULL
5194 	       && h->root.def_dynamic
5195 	       && !h->root.def_regular
5196 	       && !h->has_static_relocs))
5197 	  && r_symndx != 0
5198 	  && (h == NULL
5199 	      || h->root.root.type != bfd_link_hash_undefweak
5200 	      || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5201 	  && (input_section->flags & SEC_ALLOC) != 0)
5202 	{
5203 	  /* If we're creating a shared library, then we can't know
5204 	     where the symbol will end up.  So, we create a relocation
5205 	     record in the output, and leave the job up to the dynamic
5206 	     linker.  We must do the same for executable references to
5207 	     shared library symbols, unless we've decided to use copy
5208 	     relocs or PLTs instead.  */
5209 	  value = addend;
5210 	  if (!mips_elf_create_dynamic_relocation (abfd,
5211 						   info,
5212 						   relocation,
5213 						   h,
5214 						   sec,
5215 						   symbol,
5216 						   &value,
5217 						   input_section))
5218 	    return bfd_reloc_undefined;
5219 	}
5220       else
5221 	{
5222 	  if (r_type != R_MIPS_REL32)
5223 	    value = symbol + addend;
5224 	  else
5225 	    value = addend;
5226 	}
5227       value &= howto->dst_mask;
5228       break;
5229 
5230     case R_MIPS_PC32:
5231       value = symbol + addend - p;
5232       value &= howto->dst_mask;
5233       break;
5234 
5235     case R_MIPS16_26:
5236       /* The calculation for R_MIPS16_26 is just the same as for an
5237 	 R_MIPS_26.  It's only the storage of the relocated field into
5238 	 the output file that's different.  That's handled in
5239 	 mips_elf_perform_relocation.  So, we just fall through to the
5240 	 R_MIPS_26 case here.  */
5241     case R_MIPS_26:
5242       if (local_p)
5243 	value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
5244       else
5245 	{
5246 	  value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
5247 	  if (h->root.root.type != bfd_link_hash_undefweak)
5248 	    overflowed_p = (value >> 26) != ((p + 4) >> 28);
5249 	}
5250       value &= howto->dst_mask;
5251       break;
5252 
5253     case R_MIPS_TLS_DTPREL_HI16:
5254       value = (mips_elf_high (addend + symbol - dtprel_base (info))
5255 	       & howto->dst_mask);
5256       break;
5257 
5258     case R_MIPS_TLS_DTPREL_LO16:
5259     case R_MIPS_TLS_DTPREL32:
5260     case R_MIPS_TLS_DTPREL64:
5261       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5262       break;
5263 
5264     case R_MIPS_TLS_TPREL_HI16:
5265       value = (mips_elf_high (addend + symbol - tprel_base (info))
5266 	       & howto->dst_mask);
5267       break;
5268 
5269     case R_MIPS_TLS_TPREL_LO16:
5270       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5271       break;
5272 
5273     case R_MIPS_HI16:
5274     case R_MIPS16_HI16:
5275       if (!gp_disp_p)
5276 	{
5277 	  value = mips_elf_high (addend + symbol);
5278 	  value &= howto->dst_mask;
5279 	}
5280       else
5281 	{
5282 	  /* For MIPS16 ABI code we generate this sequence
5283 	        0: li      $v0,%hi(_gp_disp)
5284 	        4: addiupc $v1,%lo(_gp_disp)
5285 	        8: sll     $v0,16
5286 	       12: addu    $v0,$v1
5287 	       14: move    $gp,$v0
5288 	     So the offsets of hi and lo relocs are the same, but the
5289 	     $pc is four higher than $t9 would be, so reduce
5290 	     both reloc addends by 4. */
5291 	  if (r_type == R_MIPS16_HI16)
5292 	    value = mips_elf_high (addend + gp - p - 4);
5293 	  else
5294 	    value = mips_elf_high (addend + gp - p);
5295 	  overflowed_p = mips_elf_overflow_p (value, 16);
5296 	}
5297       break;
5298 
5299     case R_MIPS_LO16:
5300     case R_MIPS16_LO16:
5301       if (!gp_disp_p)
5302 	value = (symbol + addend) & howto->dst_mask;
5303       else
5304 	{
5305 	  /* See the comment for R_MIPS16_HI16 above for the reason
5306 	     for this conditional.  */
5307 	  if (r_type == R_MIPS16_LO16)
5308 	    value = addend + gp - p;
5309 	  else
5310 	    value = addend + gp - p + 4;
5311 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5312 	     for overflow.  But, on, say, IRIX5, relocations against
5313 	     _gp_disp are normally generated from the .cpload
5314 	     pseudo-op.  It generates code that normally looks like
5315 	     this:
5316 
5317 	       lui    $gp,%hi(_gp_disp)
5318 	       addiu  $gp,$gp,%lo(_gp_disp)
5319 	       addu   $gp,$gp,$t9
5320 
5321 	     Here $t9 holds the address of the function being called,
5322 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
5323 	     relocation can easily overflow in this situation, but the
5324 	     R_MIPS_HI16 relocation will handle the overflow.
5325 	     Therefore, we consider this a bug in the MIPS ABI, and do
5326 	     not check for overflow here.  */
5327 	}
5328       break;
5329 
5330     case R_MIPS_LITERAL:
5331       /* Because we don't merge literal sections, we can handle this
5332 	 just like R_MIPS_GPREL16.  In the long run, we should merge
5333 	 shared literals, and then we will need to additional work
5334 	 here.  */
5335 
5336       /* Fall through.  */
5337 
5338     case R_MIPS16_GPREL:
5339       /* The R_MIPS16_GPREL performs the same calculation as
5340 	 R_MIPS_GPREL16, but stores the relocated bits in a different
5341 	 order.  We don't need to do anything special here; the
5342 	 differences are handled in mips_elf_perform_relocation.  */
5343     case R_MIPS_GPREL16:
5344       /* Only sign-extend the addend if it was extracted from the
5345 	 instruction.  If the addend was separate, leave it alone,
5346 	 otherwise we may lose significant bits.  */
5347       if (howto->partial_inplace)
5348 	addend = _bfd_mips_elf_sign_extend (addend, 16);
5349       value = symbol + addend - gp;
5350       /* If the symbol was local, any earlier relocatable links will
5351 	 have adjusted its addend with the gp offset, so compensate
5352 	 for that now.  Don't do it for symbols forced local in this
5353 	 link, though, since they won't have had the gp offset applied
5354 	 to them before.  */
5355       if (was_local_p)
5356 	value += gp0;
5357       overflowed_p = mips_elf_overflow_p (value, 16);
5358       break;
5359 
5360     case R_MIPS16_GOT16:
5361     case R_MIPS16_CALL16:
5362     case R_MIPS_GOT16:
5363     case R_MIPS_CALL16:
5364       /* VxWorks does not have separate local and global semantics for
5365 	 R_MIPS*_GOT16; every relocation evaluates to "G".  */
5366       if (!htab->is_vxworks && local_p)
5367 	{
5368 	  bfd_boolean forced;
5369 
5370 	  forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
5371 						  local_sections, FALSE);
5372 	  value = mips_elf_got16_entry (abfd, input_bfd, info,
5373 					symbol + addend, forced);
5374 	  if (value == MINUS_ONE)
5375 	    return bfd_reloc_outofrange;
5376 	  value
5377 	    = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5378 	  overflowed_p = mips_elf_overflow_p (value, 16);
5379 	  break;
5380 	}
5381 
5382       /* Fall through.  */
5383 
5384     case R_MIPS_TLS_GD:
5385     case R_MIPS_TLS_GOTTPREL:
5386     case R_MIPS_TLS_LDM:
5387     case R_MIPS_GOT_DISP:
5388     got_disp:
5389       value = g;
5390       overflowed_p = mips_elf_overflow_p (value, 16);
5391       break;
5392 
5393     case R_MIPS_GPREL32:
5394       value = (addend + symbol + gp0 - gp);
5395       if (!save_addend)
5396 	value &= howto->dst_mask;
5397       break;
5398 
5399     case R_MIPS_PC16:
5400     case R_MIPS_GNU_REL16_S2:
5401       value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5402       overflowed_p = mips_elf_overflow_p (value, 18);
5403       value >>= howto->rightshift;
5404       value &= howto->dst_mask;
5405       break;
5406 
5407     case R_MIPS_GOT_HI16:
5408     case R_MIPS_CALL_HI16:
5409       /* We're allowed to handle these two relocations identically.
5410 	 The dynamic linker is allowed to handle the CALL relocations
5411 	 differently by creating a lazy evaluation stub.  */
5412       value = g;
5413       value = mips_elf_high (value);
5414       value &= howto->dst_mask;
5415       break;
5416 
5417     case R_MIPS_GOT_LO16:
5418     case R_MIPS_CALL_LO16:
5419       value = g & howto->dst_mask;
5420       break;
5421 
5422     case R_MIPS_GOT_PAGE:
5423       /* GOT_PAGE relocations that reference non-local symbols decay
5424 	 to GOT_DISP.  The corresponding GOT_OFST relocation decays to
5425 	 0.  */
5426       if (! local_p)
5427 	goto got_disp;
5428       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5429       if (value == MINUS_ONE)
5430 	return bfd_reloc_outofrange;
5431       value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5432       overflowed_p = mips_elf_overflow_p (value, 16);
5433       break;
5434 
5435     case R_MIPS_GOT_OFST:
5436       if (local_p)
5437 	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5438       else
5439 	value = addend;
5440       overflowed_p = mips_elf_overflow_p (value, 16);
5441       break;
5442 
5443     case R_MIPS_SUB:
5444       value = symbol - addend;
5445       value &= howto->dst_mask;
5446       break;
5447 
5448     case R_MIPS_HIGHER:
5449       value = mips_elf_higher (addend + symbol);
5450       value &= howto->dst_mask;
5451       break;
5452 
5453     case R_MIPS_HIGHEST:
5454       value = mips_elf_highest (addend + symbol);
5455       value &= howto->dst_mask;
5456       break;
5457 
5458     case R_MIPS_SCN_DISP:
5459       value = symbol + addend - sec->output_offset;
5460       value &= howto->dst_mask;
5461       break;
5462 
5463     case R_MIPS_JALR:
5464       /* This relocation is only a hint.  In some cases, we optimize
5465 	 it into a bal instruction.  But we don't try to optimize
5466 	 branches to the PLT; that will wind up wasting time.  */
5467       if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
5468 	return bfd_reloc_continue;
5469       value = symbol + addend;
5470       break;
5471 
5472     case R_MIPS_PJUMP:
5473     case R_MIPS_GNU_VTINHERIT:
5474     case R_MIPS_GNU_VTENTRY:
5475       /* We don't do anything with these at present.  */
5476       return bfd_reloc_continue;
5477 
5478     default:
5479       /* An unrecognized relocation type.  */
5480       return bfd_reloc_notsupported;
5481     }
5482 
5483   /* Store the VALUE for our caller.  */
5484   *valuep = value;
5485   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5486 }
5487 
5488 /* Obtain the field relocated by RELOCATION.  */
5489 
5490 static bfd_vma
5491 mips_elf_obtain_contents (reloc_howto_type *howto,
5492 			  const Elf_Internal_Rela *relocation,
5493 			  bfd *input_bfd, bfd_byte *contents)
5494 {
5495   bfd_vma x;
5496   bfd_byte *location = contents + relocation->r_offset;
5497 
5498   /* Obtain the bytes.  */
5499   x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5500 
5501   return x;
5502 }
5503 
5504 /* It has been determined that the result of the RELOCATION is the
5505    VALUE.  Use HOWTO to place VALUE into the output file at the
5506    appropriate position.  The SECTION is the section to which the
5507    relocation applies.  If REQUIRE_JALX is TRUE, then the opcode used
5508    for the relocation must be either JAL or JALX, and it is
5509    unconditionally converted to JALX.
5510 
5511    Returns FALSE if anything goes wrong.  */
5512 
5513 static bfd_boolean
5514 mips_elf_perform_relocation (struct bfd_link_info *info,
5515 			     reloc_howto_type *howto,
5516 			     const Elf_Internal_Rela *relocation,
5517 			     bfd_vma value, bfd *input_bfd,
5518 			     asection *input_section, bfd_byte *contents,
5519 			     bfd_boolean require_jalx)
5520 {
5521   bfd_vma x;
5522   bfd_byte *location;
5523   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5524 
5525   /* Figure out where the relocation is occurring.  */
5526   location = contents + relocation->r_offset;
5527 
5528   _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5529 
5530   /* Obtain the current value.  */
5531   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5532 
5533   /* Clear the field we are setting.  */
5534   x &= ~howto->dst_mask;
5535 
5536   /* Set the field.  */
5537   x |= (value & howto->dst_mask);
5538 
5539   /* If required, turn JAL into JALX.  */
5540   if (require_jalx)
5541     {
5542       bfd_boolean ok;
5543       bfd_vma opcode = x >> 26;
5544       bfd_vma jalx_opcode;
5545 
5546       /* Check to see if the opcode is already JAL or JALX.  */
5547       if (r_type == R_MIPS16_26)
5548 	{
5549 	  ok = ((opcode == 0x6) || (opcode == 0x7));
5550 	  jalx_opcode = 0x7;
5551 	}
5552       else
5553 	{
5554 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
5555 	  jalx_opcode = 0x1d;
5556 	}
5557 
5558       /* If the opcode is not JAL or JALX, there's a problem.  */
5559       if (!ok)
5560 	{
5561 	  (*_bfd_error_handler)
5562 	    (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5563 	     input_bfd,
5564 	     input_section,
5565 	     (unsigned long) relocation->r_offset);
5566 	  bfd_set_error (bfd_error_bad_value);
5567 	  return FALSE;
5568 	}
5569 
5570       /* Make this the JALX opcode.  */
5571       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5572     }
5573 
5574   /* On the RM9000, bal is faster than jal, because bal uses branch
5575      prediction hardware.  If we are linking for the RM9000, and we
5576      see jal, and bal fits, use it instead.  Note that this
5577      transformation should be safe for all architectures.  */
5578   if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
5579       && !info->relocatable
5580       && !require_jalx
5581       && ((r_type == R_MIPS_26 && (x >> 26) == 0x3)	    /* jal addr */
5582 	  || (r_type == R_MIPS_JALR && x == 0x0320f809)))   /* jalr t9 */
5583     {
5584       bfd_vma addr;
5585       bfd_vma dest;
5586       bfd_signed_vma off;
5587 
5588       addr = (input_section->output_section->vma
5589 	      + input_section->output_offset
5590 	      + relocation->r_offset
5591 	      + 4);
5592       if (r_type == R_MIPS_26)
5593 	dest = (value << 2) | ((addr >> 28) << 28);
5594       else
5595 	dest = value;
5596       off = dest - addr;
5597       if (off <= 0x1ffff && off >= -0x20000)
5598 	x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
5599     }
5600 
5601   /* Put the value into the output.  */
5602   bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5603 
5604   _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5605 				location);
5606 
5607   return TRUE;
5608 }
5609 
5610 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
5611    is the original relocation, which is now being transformed into a
5612    dynamic relocation.  The ADDENDP is adjusted if necessary; the
5613    caller should store the result in place of the original addend.  */
5614 
5615 static bfd_boolean
5616 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5617 				    struct bfd_link_info *info,
5618 				    const Elf_Internal_Rela *rel,
5619 				    struct mips_elf_link_hash_entry *h,
5620 				    asection *sec, bfd_vma symbol,
5621 				    bfd_vma *addendp, asection *input_section)
5622 {
5623   Elf_Internal_Rela outrel[3];
5624   asection *sreloc;
5625   bfd *dynobj;
5626   int r_type;
5627   long indx;
5628   bfd_boolean defined_p;
5629   struct mips_elf_link_hash_table *htab;
5630 
5631   htab = mips_elf_hash_table (info);
5632   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5633   dynobj = elf_hash_table (info)->dynobj;
5634   sreloc = mips_elf_rel_dyn_section (info, FALSE);
5635   BFD_ASSERT (sreloc != NULL);
5636   BFD_ASSERT (sreloc->contents != NULL);
5637   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5638 	      < sreloc->size);
5639 
5640   outrel[0].r_offset =
5641     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5642   if (ABI_64_P (output_bfd))
5643     {
5644       outrel[1].r_offset =
5645 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5646       outrel[2].r_offset =
5647 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5648     }
5649 
5650   if (outrel[0].r_offset == MINUS_ONE)
5651     /* The relocation field has been deleted.  */
5652     return TRUE;
5653 
5654   if (outrel[0].r_offset == MINUS_TWO)
5655     {
5656       /* The relocation field has been converted into a relative value of
5657 	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
5658 	 the field to be fully relocated, so add in the symbol's value.  */
5659       *addendp += symbol;
5660       return TRUE;
5661     }
5662 
5663   /* We must now calculate the dynamic symbol table index to use
5664      in the relocation.  */
5665   if (h != NULL
5666       && (!h->root.def_regular
5667 	  || (info->shared && !info->symbolic && !h->root.forced_local)))
5668     {
5669       indx = h->root.dynindx;
5670       if (SGI_COMPAT (output_bfd))
5671 	defined_p = h->root.def_regular;
5672       else
5673 	/* ??? glibc's ld.so just adds the final GOT entry to the
5674 	   relocation field.  It therefore treats relocs against
5675 	   defined symbols in the same way as relocs against
5676 	   undefined symbols.  */
5677 	defined_p = FALSE;
5678     }
5679   else
5680     {
5681       if (sec != NULL && bfd_is_abs_section (sec))
5682 	indx = 0;
5683       else if (sec == NULL || sec->owner == NULL)
5684 	{
5685 	  bfd_set_error (bfd_error_bad_value);
5686 	  return FALSE;
5687 	}
5688       else
5689 	{
5690 	  indx = elf_section_data (sec->output_section)->dynindx;
5691 	  if (indx == 0)
5692 	    {
5693 	      asection *osec = htab->root.text_index_section;
5694 	      indx = elf_section_data (osec)->dynindx;
5695 	    }
5696 	  if (indx == 0)
5697 	    abort ();
5698 	}
5699 
5700       /* Instead of generating a relocation using the section
5701 	 symbol, we may as well make it a fully relative
5702 	 relocation.  We want to avoid generating relocations to
5703 	 local symbols because we used to generate them
5704 	 incorrectly, without adding the original symbol value,
5705 	 which is mandated by the ABI for section symbols.  In
5706 	 order to give dynamic loaders and applications time to
5707 	 phase out the incorrect use, we refrain from emitting
5708 	 section-relative relocations.  It's not like they're
5709 	 useful, after all.  This should be a bit more efficient
5710 	 as well.  */
5711       /* ??? Although this behavior is compatible with glibc's ld.so,
5712 	 the ABI says that relocations against STN_UNDEF should have
5713 	 a symbol value of 0.  Irix rld honors this, so relocations
5714 	 against STN_UNDEF have no effect.  */
5715       if (!SGI_COMPAT (output_bfd))
5716 	indx = 0;
5717       defined_p = TRUE;
5718     }
5719 
5720   /* If the relocation was previously an absolute relocation and
5721      this symbol will not be referred to by the relocation, we must
5722      adjust it by the value we give it in the dynamic symbol table.
5723      Otherwise leave the job up to the dynamic linker.  */
5724   if (defined_p && r_type != R_MIPS_REL32)
5725     *addendp += symbol;
5726 
5727   if (htab->is_vxworks)
5728     /* VxWorks uses non-relative relocations for this.  */
5729     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5730   else
5731     /* The relocation is always an REL32 relocation because we don't
5732        know where the shared library will wind up at load-time.  */
5733     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5734 				   R_MIPS_REL32);
5735 
5736   /* For strict adherence to the ABI specification, we should
5737      generate a R_MIPS_64 relocation record by itself before the
5738      _REL32/_64 record as well, such that the addend is read in as
5739      a 64-bit value (REL32 is a 32-bit relocation, after all).
5740      However, since none of the existing ELF64 MIPS dynamic
5741      loaders seems to care, we don't waste space with these
5742      artificial relocations.  If this turns out to not be true,
5743      mips_elf_allocate_dynamic_relocation() should be tweaked so
5744      as to make room for a pair of dynamic relocations per
5745      invocation if ABI_64_P, and here we should generate an
5746      additional relocation record with R_MIPS_64 by itself for a
5747      NULL symbol before this relocation record.  */
5748   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5749 				 ABI_64_P (output_bfd)
5750 				 ? R_MIPS_64
5751 				 : R_MIPS_NONE);
5752   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5753 
5754   /* Adjust the output offset of the relocation to reference the
5755      correct location in the output file.  */
5756   outrel[0].r_offset += (input_section->output_section->vma
5757 			 + input_section->output_offset);
5758   outrel[1].r_offset += (input_section->output_section->vma
5759 			 + input_section->output_offset);
5760   outrel[2].r_offset += (input_section->output_section->vma
5761 			 + input_section->output_offset);
5762 
5763   /* Put the relocation back out.  We have to use the special
5764      relocation outputter in the 64-bit case since the 64-bit
5765      relocation format is non-standard.  */
5766   if (ABI_64_P (output_bfd))
5767     {
5768       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5769 	(output_bfd, &outrel[0],
5770 	 (sreloc->contents
5771 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5772     }
5773   else if (htab->is_vxworks)
5774     {
5775       /* VxWorks uses RELA rather than REL dynamic relocations.  */
5776       outrel[0].r_addend = *addendp;
5777       bfd_elf32_swap_reloca_out
5778 	(output_bfd, &outrel[0],
5779 	 (sreloc->contents
5780 	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5781     }
5782   else
5783     bfd_elf32_swap_reloc_out
5784       (output_bfd, &outrel[0],
5785        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5786 
5787   /* We've now added another relocation.  */
5788   ++sreloc->reloc_count;
5789 
5790   /* Make sure the output section is writable.  The dynamic linker
5791      will be writing to it.  */
5792   elf_section_data (input_section->output_section)->this_hdr.sh_flags
5793     |= SHF_WRITE;
5794 
5795   /* On IRIX5, make an entry of compact relocation info.  */
5796   if (IRIX_COMPAT (output_bfd) == ict_irix5)
5797     {
5798       asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5799       bfd_byte *cr;
5800 
5801       if (scpt)
5802 	{
5803 	  Elf32_crinfo cptrel;
5804 
5805 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5806 	  cptrel.vaddr = (rel->r_offset
5807 			  + input_section->output_section->vma
5808 			  + input_section->output_offset);
5809 	  if (r_type == R_MIPS_REL32)
5810 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5811 	  else
5812 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5813 	  mips_elf_set_cr_dist2to (cptrel, 0);
5814 	  cptrel.konst = *addendp;
5815 
5816 	  cr = (scpt->contents
5817 		+ sizeof (Elf32_External_compact_rel));
5818 	  mips_elf_set_cr_relvaddr (cptrel, 0);
5819 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5820 				     ((Elf32_External_crinfo *) cr
5821 				      + scpt->reloc_count));
5822 	  ++scpt->reloc_count;
5823 	}
5824     }
5825 
5826   /* If we've written this relocation for a readonly section,
5827      we need to set DF_TEXTREL again, so that we do not delete the
5828      DT_TEXTREL tag.  */
5829   if (MIPS_ELF_READONLY_SECTION (input_section))
5830     info->flags |= DF_TEXTREL;
5831 
5832   return TRUE;
5833 }
5834 
5835 /* Return the MACH for a MIPS e_flags value.  */
5836 
5837 unsigned long
5838 _bfd_elf_mips_mach (flagword flags)
5839 {
5840   switch (flags & EF_MIPS_MACH)
5841     {
5842     case E_MIPS_MACH_3900:
5843       return bfd_mach_mips3900;
5844 
5845     case E_MIPS_MACH_4010:
5846       return bfd_mach_mips4010;
5847 
5848     case E_MIPS_MACH_4100:
5849       return bfd_mach_mips4100;
5850 
5851     case E_MIPS_MACH_4111:
5852       return bfd_mach_mips4111;
5853 
5854     case E_MIPS_MACH_4120:
5855       return bfd_mach_mips4120;
5856 
5857     case E_MIPS_MACH_4650:
5858       return bfd_mach_mips4650;
5859 
5860     case E_MIPS_MACH_5400:
5861       return bfd_mach_mips5400;
5862 
5863     case E_MIPS_MACH_5500:
5864       return bfd_mach_mips5500;
5865 
5866     case E_MIPS_MACH_9000:
5867       return bfd_mach_mips9000;
5868 
5869     case E_MIPS_MACH_SB1:
5870       return bfd_mach_mips_sb1;
5871 
5872     case E_MIPS_MACH_LS2E:
5873       return bfd_mach_mips_loongson_2e;
5874 
5875     case E_MIPS_MACH_LS2F:
5876       return bfd_mach_mips_loongson_2f;
5877 
5878     case E_MIPS_MACH_OCTEON:
5879       return bfd_mach_mips_octeon;
5880 
5881     case E_MIPS_MACH_XLR:
5882       return bfd_mach_mips_xlr;
5883 
5884     default:
5885       switch (flags & EF_MIPS_ARCH)
5886 	{
5887 	default:
5888 	case E_MIPS_ARCH_1:
5889 	  return bfd_mach_mips3000;
5890 
5891 	case E_MIPS_ARCH_2:
5892 	  return bfd_mach_mips6000;
5893 
5894 	case E_MIPS_ARCH_3:
5895 	  return bfd_mach_mips4000;
5896 
5897 	case E_MIPS_ARCH_4:
5898 	  return bfd_mach_mips8000;
5899 
5900 	case E_MIPS_ARCH_5:
5901 	  return bfd_mach_mips5;
5902 
5903 	case E_MIPS_ARCH_32:
5904 	  return bfd_mach_mipsisa32;
5905 
5906 	case E_MIPS_ARCH_64:
5907 	  return bfd_mach_mipsisa64;
5908 
5909 	case E_MIPS_ARCH_32R2:
5910 	  return bfd_mach_mipsisa32r2;
5911 
5912 	case E_MIPS_ARCH_64R2:
5913 	  return bfd_mach_mipsisa64r2;
5914 	}
5915     }
5916 
5917   return 0;
5918 }
5919 
5920 /* Return printable name for ABI.  */
5921 
5922 static INLINE char *
5923 elf_mips_abi_name (bfd *abfd)
5924 {
5925   flagword flags;
5926 
5927   flags = elf_elfheader (abfd)->e_flags;
5928   switch (flags & EF_MIPS_ABI)
5929     {
5930     case 0:
5931       if (ABI_N32_P (abfd))
5932 	return "N32";
5933       else if (ABI_64_P (abfd))
5934 	return "64";
5935       else
5936 	return "none";
5937     case E_MIPS_ABI_O32:
5938       return "O32";
5939     case E_MIPS_ABI_O64:
5940       return "O64";
5941     case E_MIPS_ABI_EABI32:
5942       return "EABI32";
5943     case E_MIPS_ABI_EABI64:
5944       return "EABI64";
5945     default:
5946       return "unknown abi";
5947     }
5948 }
5949 
5950 /* MIPS ELF uses two common sections.  One is the usual one, and the
5951    other is for small objects.  All the small objects are kept
5952    together, and then referenced via the gp pointer, which yields
5953    faster assembler code.  This is what we use for the small common
5954    section.  This approach is copied from ecoff.c.  */
5955 static asection mips_elf_scom_section;
5956 static asymbol mips_elf_scom_symbol;
5957 static asymbol *mips_elf_scom_symbol_ptr;
5958 
5959 /* MIPS ELF also uses an acommon section, which represents an
5960    allocated common symbol which may be overridden by a
5961    definition in a shared library.  */
5962 static asection mips_elf_acom_section;
5963 static asymbol mips_elf_acom_symbol;
5964 static asymbol *mips_elf_acom_symbol_ptr;
5965 
5966 /* This is used for both the 32-bit and the 64-bit ABI.  */
5967 
5968 void
5969 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
5970 {
5971   elf_symbol_type *elfsym;
5972 
5973   /* Handle the special MIPS section numbers that a symbol may use.  */
5974   elfsym = (elf_symbol_type *) asym;
5975   switch (elfsym->internal_elf_sym.st_shndx)
5976     {
5977     case SHN_MIPS_ACOMMON:
5978       /* This section is used in a dynamically linked executable file.
5979 	 It is an allocated common section.  The dynamic linker can
5980 	 either resolve these symbols to something in a shared
5981 	 library, or it can just leave them here.  For our purposes,
5982 	 we can consider these symbols to be in a new section.  */
5983       if (mips_elf_acom_section.name == NULL)
5984 	{
5985 	  /* Initialize the acommon section.  */
5986 	  mips_elf_acom_section.name = ".acommon";
5987 	  mips_elf_acom_section.flags = SEC_ALLOC;
5988 	  mips_elf_acom_section.output_section = &mips_elf_acom_section;
5989 	  mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
5990 	  mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
5991 	  mips_elf_acom_symbol.name = ".acommon";
5992 	  mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
5993 	  mips_elf_acom_symbol.section = &mips_elf_acom_section;
5994 	  mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
5995 	}
5996       asym->section = &mips_elf_acom_section;
5997       break;
5998 
5999     case SHN_COMMON:
6000       /* Common symbols less than the GP size are automatically
6001 	 treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
6002       if (asym->value > elf_gp_size (abfd)
6003 	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6004 	  || IRIX_COMPAT (abfd) == ict_irix6)
6005 	break;
6006       /* Fall through.  */
6007     case SHN_MIPS_SCOMMON:
6008       if (mips_elf_scom_section.name == NULL)
6009 	{
6010 	  /* Initialize the small common section.  */
6011 	  mips_elf_scom_section.name = ".scommon";
6012 	  mips_elf_scom_section.flags = SEC_IS_COMMON;
6013 	  mips_elf_scom_section.output_section = &mips_elf_scom_section;
6014 	  mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6015 	  mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6016 	  mips_elf_scom_symbol.name = ".scommon";
6017 	  mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6018 	  mips_elf_scom_symbol.section = &mips_elf_scom_section;
6019 	  mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6020 	}
6021       asym->section = &mips_elf_scom_section;
6022       asym->value = elfsym->internal_elf_sym.st_size;
6023       break;
6024 
6025     case SHN_MIPS_SUNDEFINED:
6026       asym->section = bfd_und_section_ptr;
6027       break;
6028 
6029     case SHN_MIPS_TEXT:
6030       {
6031 	asection *section = bfd_get_section_by_name (abfd, ".text");
6032 
6033 	BFD_ASSERT (SGI_COMPAT (abfd));
6034 	if (section != NULL)
6035 	  {
6036 	    asym->section = section;
6037 	    /* MIPS_TEXT is a bit special, the address is not an offset
6038 	       to the base of the .text section.  So substract the section
6039 	       base address to make it an offset.  */
6040 	    asym->value -= section->vma;
6041 	  }
6042       }
6043       break;
6044 
6045     case SHN_MIPS_DATA:
6046       {
6047 	asection *section = bfd_get_section_by_name (abfd, ".data");
6048 
6049 	BFD_ASSERT (SGI_COMPAT (abfd));
6050 	if (section != NULL)
6051 	  {
6052 	    asym->section = section;
6053 	    /* MIPS_DATA is a bit special, the address is not an offset
6054 	       to the base of the .data section.  So substract the section
6055 	       base address to make it an offset.  */
6056 	    asym->value -= section->vma;
6057 	  }
6058       }
6059       break;
6060     }
6061 
6062   /* If this is an odd-valued function symbol, assume it's a MIPS16 one.  */
6063   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6064       && (asym->value & 1) != 0)
6065     {
6066       asym->value--;
6067       elfsym->internal_elf_sym.st_other
6068 	= ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6069     }
6070 }
6071 
6072 /* Implement elf_backend_eh_frame_address_size.  This differs from
6073    the default in the way it handles EABI64.
6074 
6075    EABI64 was originally specified as an LP64 ABI, and that is what
6076    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
6077    historically accepted the combination of -mabi=eabi and -mlong32,
6078    and this ILP32 variation has become semi-official over time.
6079    Both forms use elf32 and have pointer-sized FDE addresses.
6080 
6081    If an EABI object was generated by GCC 4.0 or above, it will have
6082    an empty .gcc_compiled_longXX section, where XX is the size of longs
6083    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
6084    have no special marking to distinguish them from LP64 objects.
6085 
6086    We don't want users of the official LP64 ABI to be punished for the
6087    existence of the ILP32 variant, but at the same time, we don't want
6088    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6089    We therefore take the following approach:
6090 
6091       - If ABFD contains a .gcc_compiled_longXX section, use it to
6092         determine the pointer size.
6093 
6094       - Otherwise check the type of the first relocation.  Assume that
6095         the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6096 
6097       - Otherwise punt.
6098 
6099    The second check is enough to detect LP64 objects generated by pre-4.0
6100    compilers because, in the kind of output generated by those compilers,
6101    the first relocation will be associated with either a CIE personality
6102    routine or an FDE start address.  Furthermore, the compilers never
6103    used a special (non-pointer) encoding for this ABI.
6104 
6105    Checking the relocation type should also be safe because there is no
6106    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
6107    did so.  */
6108 
6109 unsigned int
6110 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6111 {
6112   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6113     return 8;
6114   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6115     {
6116       bfd_boolean long32_p, long64_p;
6117 
6118       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6119       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6120       if (long32_p && long64_p)
6121 	return 0;
6122       if (long32_p)
6123 	return 4;
6124       if (long64_p)
6125 	return 8;
6126 
6127       if (sec->reloc_count > 0
6128 	  && elf_section_data (sec)->relocs != NULL
6129 	  && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6130 	      == R_MIPS_64))
6131 	return 8;
6132 
6133       return 0;
6134     }
6135   return 4;
6136 }
6137 
6138 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6139    relocations against two unnamed section symbols to resolve to the
6140    same address.  For example, if we have code like:
6141 
6142 	lw	$4,%got_disp(.data)($gp)
6143 	lw	$25,%got_disp(.text)($gp)
6144 	jalr	$25
6145 
6146    then the linker will resolve both relocations to .data and the program
6147    will jump there rather than to .text.
6148 
6149    We can work around this problem by giving names to local section symbols.
6150    This is also what the MIPSpro tools do.  */
6151 
6152 bfd_boolean
6153 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6154 {
6155   return SGI_COMPAT (abfd);
6156 }
6157 
6158 /* Work over a section just before writing it out.  This routine is
6159    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
6160    sections that need the SHF_MIPS_GPREL flag by name; there has to be
6161    a better way.  */
6162 
6163 bfd_boolean
6164 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6165 {
6166   if (hdr->sh_type == SHT_MIPS_REGINFO
6167       && hdr->sh_size > 0)
6168     {
6169       bfd_byte buf[4];
6170 
6171       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6172       BFD_ASSERT (hdr->contents == NULL);
6173 
6174       if (bfd_seek (abfd,
6175 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6176 		    SEEK_SET) != 0)
6177 	return FALSE;
6178       H_PUT_32 (abfd, elf_gp (abfd), buf);
6179       if (bfd_bwrite (buf, 4, abfd) != 4)
6180 	return FALSE;
6181     }
6182 
6183   if (hdr->sh_type == SHT_MIPS_OPTIONS
6184       && hdr->bfd_section != NULL
6185       && mips_elf_section_data (hdr->bfd_section) != NULL
6186       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6187     {
6188       bfd_byte *contents, *l, *lend;
6189 
6190       /* We stored the section contents in the tdata field in the
6191 	 set_section_contents routine.  We save the section contents
6192 	 so that we don't have to read them again.
6193 	 At this point we know that elf_gp is set, so we can look
6194 	 through the section contents to see if there is an
6195 	 ODK_REGINFO structure.  */
6196 
6197       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6198       l = contents;
6199       lend = contents + hdr->sh_size;
6200       while (l + sizeof (Elf_External_Options) <= lend)
6201 	{
6202 	  Elf_Internal_Options intopt;
6203 
6204 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6205 					&intopt);
6206 	  if (intopt.size < sizeof (Elf_External_Options))
6207 	    {
6208 	      (*_bfd_error_handler)
6209 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
6210 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6211 	      break;
6212 	    }
6213 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6214 	    {
6215 	      bfd_byte buf[8];
6216 
6217 	      if (bfd_seek (abfd,
6218 			    (hdr->sh_offset
6219 			     + (l - contents)
6220 			     + sizeof (Elf_External_Options)
6221 			     + (sizeof (Elf64_External_RegInfo) - 8)),
6222 			     SEEK_SET) != 0)
6223 		return FALSE;
6224 	      H_PUT_64 (abfd, elf_gp (abfd), buf);
6225 	      if (bfd_bwrite (buf, 8, abfd) != 8)
6226 		return FALSE;
6227 	    }
6228 	  else if (intopt.kind == ODK_REGINFO)
6229 	    {
6230 	      bfd_byte buf[4];
6231 
6232 	      if (bfd_seek (abfd,
6233 			    (hdr->sh_offset
6234 			     + (l - contents)
6235 			     + sizeof (Elf_External_Options)
6236 			     + (sizeof (Elf32_External_RegInfo) - 4)),
6237 			    SEEK_SET) != 0)
6238 		return FALSE;
6239 	      H_PUT_32 (abfd, elf_gp (abfd), buf);
6240 	      if (bfd_bwrite (buf, 4, abfd) != 4)
6241 		return FALSE;
6242 	    }
6243 	  l += intopt.size;
6244 	}
6245     }
6246 
6247   if (hdr->bfd_section != NULL)
6248     {
6249       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6250 
6251       if (strcmp (name, ".sdata") == 0
6252 	  || strcmp (name, ".lit8") == 0
6253 	  || strcmp (name, ".lit4") == 0)
6254 	{
6255 	  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6256 	  hdr->sh_type = SHT_PROGBITS;
6257 	}
6258       else if (strcmp (name, ".sbss") == 0)
6259 	{
6260 	  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6261 	  hdr->sh_type = SHT_NOBITS;
6262 	}
6263       else if (strcmp (name, ".srdata") == 0)
6264 	{
6265 	  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6266 	  hdr->sh_type = SHT_PROGBITS;
6267 	}
6268       else if (strcmp (name, ".compact_rel") == 0)
6269 	{
6270 	  hdr->sh_flags = 0;
6271 	  hdr->sh_type = SHT_PROGBITS;
6272 	}
6273       else if (strcmp (name, ".rtproc") == 0)
6274 	{
6275 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6276 	    {
6277 	      unsigned int adjust;
6278 
6279 	      adjust = hdr->sh_size % hdr->sh_addralign;
6280 	      if (adjust != 0)
6281 		hdr->sh_size += hdr->sh_addralign - adjust;
6282 	    }
6283 	}
6284     }
6285 
6286   return TRUE;
6287 }
6288 
6289 /* Handle a MIPS specific section when reading an object file.  This
6290    is called when elfcode.h finds a section with an unknown type.
6291    This routine supports both the 32-bit and 64-bit ELF ABI.
6292 
6293    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6294    how to.  */
6295 
6296 bfd_boolean
6297 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6298 				 Elf_Internal_Shdr *hdr,
6299 				 const char *name,
6300 				 int shindex)
6301 {
6302   flagword flags = 0;
6303 
6304   /* There ought to be a place to keep ELF backend specific flags, but
6305      at the moment there isn't one.  We just keep track of the
6306      sections by their name, instead.  Fortunately, the ABI gives
6307      suggested names for all the MIPS specific sections, so we will
6308      probably get away with this.  */
6309   switch (hdr->sh_type)
6310     {
6311     case SHT_MIPS_LIBLIST:
6312       if (strcmp (name, ".liblist") != 0)
6313 	return FALSE;
6314       break;
6315     case SHT_MIPS_MSYM:
6316       if (strcmp (name, ".msym") != 0)
6317 	return FALSE;
6318       break;
6319     case SHT_MIPS_CONFLICT:
6320       if (strcmp (name, ".conflict") != 0)
6321 	return FALSE;
6322       break;
6323     case SHT_MIPS_GPTAB:
6324       if (! CONST_STRNEQ (name, ".gptab."))
6325 	return FALSE;
6326       break;
6327     case SHT_MIPS_UCODE:
6328       if (strcmp (name, ".ucode") != 0)
6329 	return FALSE;
6330       break;
6331     case SHT_MIPS_DEBUG:
6332       if (strcmp (name, ".mdebug") != 0)
6333 	return FALSE;
6334       flags = SEC_DEBUGGING;
6335       break;
6336     case SHT_MIPS_REGINFO:
6337       if (strcmp (name, ".reginfo") != 0
6338 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6339 	return FALSE;
6340       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6341       break;
6342     case SHT_MIPS_IFACE:
6343       if (strcmp (name, ".MIPS.interfaces") != 0)
6344 	return FALSE;
6345       break;
6346     case SHT_MIPS_CONTENT:
6347       if (! CONST_STRNEQ (name, ".MIPS.content"))
6348 	return FALSE;
6349       break;
6350     case SHT_MIPS_OPTIONS:
6351       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6352 	return FALSE;
6353       break;
6354     case SHT_MIPS_DWARF:
6355       if (! CONST_STRNEQ (name, ".debug_")
6356           && ! CONST_STRNEQ (name, ".zdebug_"))
6357 	return FALSE;
6358       break;
6359     case SHT_MIPS_SYMBOL_LIB:
6360       if (strcmp (name, ".MIPS.symlib") != 0)
6361 	return FALSE;
6362       break;
6363     case SHT_MIPS_EVENTS:
6364       if (! CONST_STRNEQ (name, ".MIPS.events")
6365 	  && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6366 	return FALSE;
6367       break;
6368     default:
6369       break;
6370     }
6371 
6372   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6373     return FALSE;
6374 
6375   if (flags)
6376     {
6377       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6378 				   (bfd_get_section_flags (abfd,
6379 							   hdr->bfd_section)
6380 				    | flags)))
6381 	return FALSE;
6382     }
6383 
6384   /* FIXME: We should record sh_info for a .gptab section.  */
6385 
6386   /* For a .reginfo section, set the gp value in the tdata information
6387      from the contents of this section.  We need the gp value while
6388      processing relocs, so we just get it now.  The .reginfo section
6389      is not used in the 64-bit MIPS ELF ABI.  */
6390   if (hdr->sh_type == SHT_MIPS_REGINFO)
6391     {
6392       Elf32_External_RegInfo ext;
6393       Elf32_RegInfo s;
6394 
6395       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6396 				      &ext, 0, sizeof ext))
6397 	return FALSE;
6398       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6399       elf_gp (abfd) = s.ri_gp_value;
6400     }
6401 
6402   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6403      set the gp value based on what we find.  We may see both
6404      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6405      they should agree.  */
6406   if (hdr->sh_type == SHT_MIPS_OPTIONS)
6407     {
6408       bfd_byte *contents, *l, *lend;
6409 
6410       contents = bfd_malloc (hdr->sh_size);
6411       if (contents == NULL)
6412 	return FALSE;
6413       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6414 				      0, hdr->sh_size))
6415 	{
6416 	  free (contents);
6417 	  return FALSE;
6418 	}
6419       l = contents;
6420       lend = contents + hdr->sh_size;
6421       while (l + sizeof (Elf_External_Options) <= lend)
6422 	{
6423 	  Elf_Internal_Options intopt;
6424 
6425 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6426 					&intopt);
6427 	  if (intopt.size < sizeof (Elf_External_Options))
6428 	    {
6429 	      (*_bfd_error_handler)
6430 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
6431 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6432 	      break;
6433 	    }
6434 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6435 	    {
6436 	      Elf64_Internal_RegInfo intreg;
6437 
6438 	      bfd_mips_elf64_swap_reginfo_in
6439 		(abfd,
6440 		 ((Elf64_External_RegInfo *)
6441 		  (l + sizeof (Elf_External_Options))),
6442 		 &intreg);
6443 	      elf_gp (abfd) = intreg.ri_gp_value;
6444 	    }
6445 	  else if (intopt.kind == ODK_REGINFO)
6446 	    {
6447 	      Elf32_RegInfo intreg;
6448 
6449 	      bfd_mips_elf32_swap_reginfo_in
6450 		(abfd,
6451 		 ((Elf32_External_RegInfo *)
6452 		  (l + sizeof (Elf_External_Options))),
6453 		 &intreg);
6454 	      elf_gp (abfd) = intreg.ri_gp_value;
6455 	    }
6456 	  l += intopt.size;
6457 	}
6458       free (contents);
6459     }
6460 
6461   return TRUE;
6462 }
6463 
6464 /* Set the correct type for a MIPS ELF section.  We do this by the
6465    section name, which is a hack, but ought to work.  This routine is
6466    used by both the 32-bit and the 64-bit ABI.  */
6467 
6468 bfd_boolean
6469 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6470 {
6471   const char *name = bfd_get_section_name (abfd, sec);
6472 
6473   if (strcmp (name, ".liblist") == 0)
6474     {
6475       hdr->sh_type = SHT_MIPS_LIBLIST;
6476       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6477       /* The sh_link field is set in final_write_processing.  */
6478     }
6479   else if (strcmp (name, ".conflict") == 0)
6480     hdr->sh_type = SHT_MIPS_CONFLICT;
6481   else if (CONST_STRNEQ (name, ".gptab."))
6482     {
6483       hdr->sh_type = SHT_MIPS_GPTAB;
6484       hdr->sh_entsize = sizeof (Elf32_External_gptab);
6485       /* The sh_info field is set in final_write_processing.  */
6486     }
6487   else if (strcmp (name, ".ucode") == 0)
6488     hdr->sh_type = SHT_MIPS_UCODE;
6489   else if (strcmp (name, ".mdebug") == 0)
6490     {
6491       hdr->sh_type = SHT_MIPS_DEBUG;
6492       /* In a shared object on IRIX 5.3, the .mdebug section has an
6493          entsize of 0.  FIXME: Does this matter?  */
6494       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6495 	hdr->sh_entsize = 0;
6496       else
6497 	hdr->sh_entsize = 1;
6498     }
6499   else if (strcmp (name, ".reginfo") == 0)
6500     {
6501       hdr->sh_type = SHT_MIPS_REGINFO;
6502       /* In a shared object on IRIX 5.3, the .reginfo section has an
6503          entsize of 0x18.  FIXME: Does this matter?  */
6504       if (SGI_COMPAT (abfd))
6505 	{
6506 	  if ((abfd->flags & DYNAMIC) != 0)
6507 	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6508 	  else
6509 	    hdr->sh_entsize = 1;
6510 	}
6511       else
6512 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6513     }
6514   else if (SGI_COMPAT (abfd)
6515 	   && (strcmp (name, ".hash") == 0
6516 	       || strcmp (name, ".dynamic") == 0
6517 	       || strcmp (name, ".dynstr") == 0))
6518     {
6519       if (SGI_COMPAT (abfd))
6520 	hdr->sh_entsize = 0;
6521 #if 0
6522       /* This isn't how the IRIX6 linker behaves.  */
6523       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6524 #endif
6525     }
6526   else if (strcmp (name, ".got") == 0
6527 	   || strcmp (name, ".srdata") == 0
6528 	   || strcmp (name, ".sdata") == 0
6529 	   || strcmp (name, ".sbss") == 0
6530 	   || strcmp (name, ".lit4") == 0
6531 	   || strcmp (name, ".lit8") == 0)
6532     hdr->sh_flags |= SHF_MIPS_GPREL;
6533   else if (strcmp (name, ".MIPS.interfaces") == 0)
6534     {
6535       hdr->sh_type = SHT_MIPS_IFACE;
6536       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6537     }
6538   else if (CONST_STRNEQ (name, ".MIPS.content"))
6539     {
6540       hdr->sh_type = SHT_MIPS_CONTENT;
6541       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6542       /* The sh_info field is set in final_write_processing.  */
6543     }
6544   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6545     {
6546       hdr->sh_type = SHT_MIPS_OPTIONS;
6547       hdr->sh_entsize = 1;
6548       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6549     }
6550   else if (CONST_STRNEQ (name, ".debug_")
6551            || CONST_STRNEQ (name, ".zdebug_"))
6552     {
6553       hdr->sh_type = SHT_MIPS_DWARF;
6554 
6555       /* Irix facilities such as libexc expect a single .debug_frame
6556 	 per executable, the system ones have NOSTRIP set and the linker
6557 	 doesn't merge sections with different flags so ...  */
6558       if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6559 	hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6560     }
6561   else if (strcmp (name, ".MIPS.symlib") == 0)
6562     {
6563       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6564       /* The sh_link and sh_info fields are set in
6565          final_write_processing.  */
6566     }
6567   else if (CONST_STRNEQ (name, ".MIPS.events")
6568 	   || CONST_STRNEQ (name, ".MIPS.post_rel"))
6569     {
6570       hdr->sh_type = SHT_MIPS_EVENTS;
6571       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6572       /* The sh_link field is set in final_write_processing.  */
6573     }
6574   else if (strcmp (name, ".msym") == 0)
6575     {
6576       hdr->sh_type = SHT_MIPS_MSYM;
6577       hdr->sh_flags |= SHF_ALLOC;
6578       hdr->sh_entsize = 8;
6579     }
6580 
6581   /* The generic elf_fake_sections will set up REL_HDR using the default
6582    kind of relocations.  We used to set up a second header for the
6583    non-default kind of relocations here, but only NewABI would use
6584    these, and the IRIX ld doesn't like resulting empty RELA sections.
6585    Thus we create those header only on demand now.  */
6586 
6587   return TRUE;
6588 }
6589 
6590 /* Given a BFD section, try to locate the corresponding ELF section
6591    index.  This is used by both the 32-bit and the 64-bit ABI.
6592    Actually, it's not clear to me that the 64-bit ABI supports these,
6593    but for non-PIC objects we will certainly want support for at least
6594    the .scommon section.  */
6595 
6596 bfd_boolean
6597 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6598 					asection *sec, int *retval)
6599 {
6600   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6601     {
6602       *retval = SHN_MIPS_SCOMMON;
6603       return TRUE;
6604     }
6605   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6606     {
6607       *retval = SHN_MIPS_ACOMMON;
6608       return TRUE;
6609     }
6610   return FALSE;
6611 }
6612 
6613 /* Hook called by the linker routine which adds symbols from an object
6614    file.  We must handle the special MIPS section numbers here.  */
6615 
6616 bfd_boolean
6617 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6618 			       Elf_Internal_Sym *sym, const char **namep,
6619 			       flagword *flagsp ATTRIBUTE_UNUSED,
6620 			       asection **secp, bfd_vma *valp)
6621 {
6622   if (SGI_COMPAT (abfd)
6623       && (abfd->flags & DYNAMIC) != 0
6624       && strcmp (*namep, "_rld_new_interface") == 0)
6625     {
6626       /* Skip IRIX5 rld entry name.  */
6627       *namep = NULL;
6628       return TRUE;
6629     }
6630 
6631   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6632      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
6633      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
6634      a magic symbol resolved by the linker, we ignore this bogus definition
6635      of _gp_disp.  New ABI objects do not suffer from this problem so this
6636      is not done for them. */
6637   if (!NEWABI_P(abfd)
6638       && (sym->st_shndx == SHN_ABS)
6639       && (strcmp (*namep, "_gp_disp") == 0))
6640     {
6641       *namep = NULL;
6642       return TRUE;
6643     }
6644 
6645   switch (sym->st_shndx)
6646     {
6647     case SHN_COMMON:
6648       /* Common symbols less than the GP size are automatically
6649 	 treated as SHN_MIPS_SCOMMON symbols.  */
6650       if (sym->st_size > elf_gp_size (abfd)
6651 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
6652 	  || IRIX_COMPAT (abfd) == ict_irix6)
6653 	break;
6654       /* Fall through.  */
6655     case SHN_MIPS_SCOMMON:
6656       *secp = bfd_make_section_old_way (abfd, ".scommon");
6657       (*secp)->flags |= SEC_IS_COMMON;
6658       *valp = sym->st_size;
6659       break;
6660 
6661     case SHN_MIPS_TEXT:
6662       /* This section is used in a shared object.  */
6663       if (elf_tdata (abfd)->elf_text_section == NULL)
6664 	{
6665 	  asymbol *elf_text_symbol;
6666 	  asection *elf_text_section;
6667 	  bfd_size_type amt = sizeof (asection);
6668 
6669 	  elf_text_section = bfd_zalloc (abfd, amt);
6670 	  if (elf_text_section == NULL)
6671 	    return FALSE;
6672 
6673 	  amt = sizeof (asymbol);
6674 	  elf_text_symbol = bfd_zalloc (abfd, amt);
6675 	  if (elf_text_symbol == NULL)
6676 	    return FALSE;
6677 
6678 	  /* Initialize the section.  */
6679 
6680 	  elf_tdata (abfd)->elf_text_section = elf_text_section;
6681 	  elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6682 
6683 	  elf_text_section->symbol = elf_text_symbol;
6684 	  elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6685 
6686 	  elf_text_section->name = ".text";
6687 	  elf_text_section->flags = SEC_NO_FLAGS;
6688 	  elf_text_section->output_section = NULL;
6689 	  elf_text_section->owner = abfd;
6690 	  elf_text_symbol->name = ".text";
6691 	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6692 	  elf_text_symbol->section = elf_text_section;
6693 	}
6694       /* This code used to do *secp = bfd_und_section_ptr if
6695          info->shared.  I don't know why, and that doesn't make sense,
6696          so I took it out.  */
6697       *secp = elf_tdata (abfd)->elf_text_section;
6698       break;
6699 
6700     case SHN_MIPS_ACOMMON:
6701       /* Fall through. XXX Can we treat this as allocated data?  */
6702     case SHN_MIPS_DATA:
6703       /* This section is used in a shared object.  */
6704       if (elf_tdata (abfd)->elf_data_section == NULL)
6705 	{
6706 	  asymbol *elf_data_symbol;
6707 	  asection *elf_data_section;
6708 	  bfd_size_type amt = sizeof (asection);
6709 
6710 	  elf_data_section = bfd_zalloc (abfd, amt);
6711 	  if (elf_data_section == NULL)
6712 	    return FALSE;
6713 
6714 	  amt = sizeof (asymbol);
6715 	  elf_data_symbol = bfd_zalloc (abfd, amt);
6716 	  if (elf_data_symbol == NULL)
6717 	    return FALSE;
6718 
6719 	  /* Initialize the section.  */
6720 
6721 	  elf_tdata (abfd)->elf_data_section = elf_data_section;
6722 	  elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6723 
6724 	  elf_data_section->symbol = elf_data_symbol;
6725 	  elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6726 
6727 	  elf_data_section->name = ".data";
6728 	  elf_data_section->flags = SEC_NO_FLAGS;
6729 	  elf_data_section->output_section = NULL;
6730 	  elf_data_section->owner = abfd;
6731 	  elf_data_symbol->name = ".data";
6732 	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6733 	  elf_data_symbol->section = elf_data_section;
6734 	}
6735       /* This code used to do *secp = bfd_und_section_ptr if
6736          info->shared.  I don't know why, and that doesn't make sense,
6737          so I took it out.  */
6738       *secp = elf_tdata (abfd)->elf_data_section;
6739       break;
6740 
6741     case SHN_MIPS_SUNDEFINED:
6742       *secp = bfd_und_section_ptr;
6743       break;
6744     }
6745 
6746   if (SGI_COMPAT (abfd)
6747       && ! info->shared
6748       && info->output_bfd->xvec == abfd->xvec
6749       && strcmp (*namep, "__rld_obj_head") == 0)
6750     {
6751       struct elf_link_hash_entry *h;
6752       struct bfd_link_hash_entry *bh;
6753 
6754       /* Mark __rld_obj_head as dynamic.  */
6755       bh = NULL;
6756       if (! (_bfd_generic_link_add_one_symbol
6757 	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6758 	      get_elf_backend_data (abfd)->collect, &bh)))
6759 	return FALSE;
6760 
6761       h = (struct elf_link_hash_entry *) bh;
6762       h->non_elf = 0;
6763       h->def_regular = 1;
6764       h->type = STT_OBJECT;
6765 
6766       if (! bfd_elf_link_record_dynamic_symbol (info, h))
6767 	return FALSE;
6768 
6769       mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6770     }
6771 
6772   /* If this is a mips16 text symbol, add 1 to the value to make it
6773      odd.  This will cause something like .word SYM to come up with
6774      the right value when it is loaded into the PC.  */
6775   if (ELF_ST_IS_MIPS16 (sym->st_other))
6776     ++*valp;
6777 
6778   return TRUE;
6779 }
6780 
6781 /* This hook function is called before the linker writes out a global
6782    symbol.  We mark symbols as small common if appropriate.  This is
6783    also where we undo the increment of the value for a mips16 symbol.  */
6784 
6785 bfd_boolean
6786 _bfd_mips_elf_link_output_symbol_hook
6787   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6788    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6789    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6790 {
6791   /* If we see a common symbol, which implies a relocatable link, then
6792      if a symbol was small common in an input file, mark it as small
6793      common in the output file.  */
6794   if (sym->st_shndx == SHN_COMMON
6795       && strcmp (input_sec->name, ".scommon") == 0)
6796     sym->st_shndx = SHN_MIPS_SCOMMON;
6797 
6798   if (ELF_ST_IS_MIPS16 (sym->st_other))
6799     sym->st_value &= ~1;
6800 
6801   return TRUE;
6802 }
6803 
6804 /* Functions for the dynamic linker.  */
6805 
6806 /* Create dynamic sections when linking against a dynamic object.  */
6807 
6808 bfd_boolean
6809 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6810 {
6811   struct elf_link_hash_entry *h;
6812   struct bfd_link_hash_entry *bh;
6813   flagword flags;
6814   register asection *s;
6815   const char * const *namep;
6816   struct mips_elf_link_hash_table *htab;
6817 
6818   htab = mips_elf_hash_table (info);
6819   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6820 	   | SEC_LINKER_CREATED | SEC_READONLY);
6821 
6822   /* The psABI requires a read-only .dynamic section, but the VxWorks
6823      EABI doesn't.  */
6824   if (!htab->is_vxworks)
6825     {
6826       s = bfd_get_section_by_name (abfd, ".dynamic");
6827       if (s != NULL)
6828 	{
6829 	  if (! bfd_set_section_flags (abfd, s, flags))
6830 	    return FALSE;
6831 	}
6832     }
6833 
6834   /* We need to create .got section.  */
6835   if (!mips_elf_create_got_section (abfd, info))
6836     return FALSE;
6837 
6838   if (! mips_elf_rel_dyn_section (info, TRUE))
6839     return FALSE;
6840 
6841   /* Create .stub section.  */
6842   s = bfd_make_section_with_flags (abfd,
6843 				   MIPS_ELF_STUB_SECTION_NAME (abfd),
6844 				   flags | SEC_CODE);
6845   if (s == NULL
6846       || ! bfd_set_section_alignment (abfd, s,
6847 				      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6848     return FALSE;
6849   htab->sstubs = s;
6850 
6851   if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6852       && !info->shared
6853       && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6854     {
6855       s = bfd_make_section_with_flags (abfd, ".rld_map",
6856 				       flags &~ (flagword) SEC_READONLY);
6857       if (s == NULL
6858 	  || ! bfd_set_section_alignment (abfd, s,
6859 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6860 	return FALSE;
6861     }
6862 
6863   /* On IRIX5, we adjust add some additional symbols and change the
6864      alignments of several sections.  There is no ABI documentation
6865      indicating that this is necessary on IRIX6, nor any evidence that
6866      the linker takes such action.  */
6867   if (IRIX_COMPAT (abfd) == ict_irix5)
6868     {
6869       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6870 	{
6871 	  bh = NULL;
6872 	  if (! (_bfd_generic_link_add_one_symbol
6873 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6874 		  NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6875 	    return FALSE;
6876 
6877 	  h = (struct elf_link_hash_entry *) bh;
6878 	  h->non_elf = 0;
6879 	  h->def_regular = 1;
6880 	  h->type = STT_SECTION;
6881 
6882 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
6883 	    return FALSE;
6884 	}
6885 
6886       /* We need to create a .compact_rel section.  */
6887       if (SGI_COMPAT (abfd))
6888 	{
6889 	  if (!mips_elf_create_compact_rel_section (abfd, info))
6890 	    return FALSE;
6891 	}
6892 
6893       /* Change alignments of some sections.  */
6894       s = bfd_get_section_by_name (abfd, ".hash");
6895       if (s != NULL)
6896 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6897       s = bfd_get_section_by_name (abfd, ".dynsym");
6898       if (s != NULL)
6899 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6900       s = bfd_get_section_by_name (abfd, ".dynstr");
6901       if (s != NULL)
6902 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6903       s = bfd_get_section_by_name (abfd, ".reginfo");
6904       if (s != NULL)
6905 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6906       s = bfd_get_section_by_name (abfd, ".dynamic");
6907       if (s != NULL)
6908 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6909     }
6910 
6911   if (!info->shared)
6912     {
6913       const char *name;
6914 
6915       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6916       bh = NULL;
6917       if (!(_bfd_generic_link_add_one_symbol
6918 	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6919 	     NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6920 	return FALSE;
6921 
6922       h = (struct elf_link_hash_entry *) bh;
6923       h->non_elf = 0;
6924       h->def_regular = 1;
6925       h->type = STT_SECTION;
6926 
6927       if (! bfd_elf_link_record_dynamic_symbol (info, h))
6928 	return FALSE;
6929 
6930       if (! mips_elf_hash_table (info)->use_rld_obj_head)
6931 	{
6932 	  /* __rld_map is a four byte word located in the .data section
6933 	     and is filled in by the rtld to contain a pointer to
6934 	     the _r_debug structure. Its symbol value will be set in
6935 	     _bfd_mips_elf_finish_dynamic_symbol.  */
6936 	  s = bfd_get_section_by_name (abfd, ".rld_map");
6937 	  BFD_ASSERT (s != NULL);
6938 
6939 	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6940 	  bh = NULL;
6941 	  if (!(_bfd_generic_link_add_one_symbol
6942 		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6943 		 get_elf_backend_data (abfd)->collect, &bh)))
6944 	    return FALSE;
6945 
6946 	  h = (struct elf_link_hash_entry *) bh;
6947 	  h->non_elf = 0;
6948 	  h->def_regular = 1;
6949 	  h->type = STT_OBJECT;
6950 
6951 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
6952 	    return FALSE;
6953 	}
6954     }
6955 
6956   /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
6957      Also create the _PROCEDURE_LINKAGE_TABLE symbol.  */
6958   if (!_bfd_elf_create_dynamic_sections (abfd, info))
6959     return FALSE;
6960 
6961   /* Cache the sections created above.  */
6962   htab->splt = bfd_get_section_by_name (abfd, ".plt");
6963   htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
6964   if (htab->is_vxworks)
6965     {
6966       htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6967       htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
6968     }
6969   else
6970     htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
6971   if (!htab->sdynbss
6972       || (htab->is_vxworks && !htab->srelbss && !info->shared)
6973       || !htab->srelplt
6974       || !htab->splt)
6975     abort ();
6976 
6977   if (htab->is_vxworks)
6978     {
6979       /* Do the usual VxWorks handling.  */
6980       if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
6981 	return FALSE;
6982 
6983       /* Work out the PLT sizes.  */
6984       if (info->shared)
6985 	{
6986 	  htab->plt_header_size
6987 	    = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
6988 	  htab->plt_entry_size
6989 	    = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
6990 	}
6991       else
6992 	{
6993 	  htab->plt_header_size
6994 	    = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
6995 	  htab->plt_entry_size
6996 	    = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
6997 	}
6998     }
6999   else if (!info->shared)
7000     {
7001       /* All variants of the plt0 entry are the same size.  */
7002       htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7003       htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7004     }
7005 
7006   return TRUE;
7007 }
7008 
7009 /* Return true if relocation REL against section SEC is a REL rather than
7010    RELA relocation.  RELOCS is the first relocation in the section and
7011    ABFD is the bfd that contains SEC.  */
7012 
7013 static bfd_boolean
7014 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7015 			   const Elf_Internal_Rela *relocs,
7016 			   const Elf_Internal_Rela *rel)
7017 {
7018   Elf_Internal_Shdr *rel_hdr;
7019   const struct elf_backend_data *bed;
7020 
7021   /* To determine which flavor or relocation this is, we depend on the
7022      fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2.  */
7023   rel_hdr = &elf_section_data (sec)->rel_hdr;
7024   bed = get_elf_backend_data (abfd);
7025   if ((size_t) (rel - relocs)
7026       >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7027     rel_hdr = elf_section_data (sec)->rel_hdr2;
7028   return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7029 }
7030 
7031 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7032    HOWTO is the relocation's howto and CONTENTS points to the contents
7033    of the section that REL is against.  */
7034 
7035 static bfd_vma
7036 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7037 			  reloc_howto_type *howto, bfd_byte *contents)
7038 {
7039   bfd_byte *location;
7040   unsigned int r_type;
7041   bfd_vma addend;
7042 
7043   r_type = ELF_R_TYPE (abfd, rel->r_info);
7044   location = contents + rel->r_offset;
7045 
7046   /* Get the addend, which is stored in the input file.  */
7047   _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7048   addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7049   _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7050 
7051   return addend & howto->src_mask;
7052 }
7053 
7054 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7055    and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
7056    and update *ADDEND with the final addend.  Return true on success
7057    or false if the LO16 could not be found.  RELEND is the exclusive
7058    upper bound on the relocations for REL's section.  */
7059 
7060 static bfd_boolean
7061 mips_elf_add_lo16_rel_addend (bfd *abfd,
7062 			      const Elf_Internal_Rela *rel,
7063 			      const Elf_Internal_Rela *relend,
7064 			      bfd_byte *contents, bfd_vma *addend)
7065 {
7066   unsigned int r_type, lo16_type;
7067   const Elf_Internal_Rela *lo16_relocation;
7068   reloc_howto_type *lo16_howto;
7069   bfd_vma l;
7070 
7071   r_type = ELF_R_TYPE (abfd, rel->r_info);
7072   if (mips16_reloc_p (r_type))
7073     lo16_type = R_MIPS16_LO16;
7074   else
7075     lo16_type = R_MIPS_LO16;
7076 
7077   /* The combined value is the sum of the HI16 addend, left-shifted by
7078      sixteen bits, and the LO16 addend, sign extended.  (Usually, the
7079      code does a `lui' of the HI16 value, and then an `addiu' of the
7080      LO16 value.)
7081 
7082      Scan ahead to find a matching LO16 relocation.
7083 
7084      According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7085      be immediately following.  However, for the IRIX6 ABI, the next
7086      relocation may be a composed relocation consisting of several
7087      relocations for the same address.  In that case, the R_MIPS_LO16
7088      relocation may occur as one of these.  We permit a similar
7089      extension in general, as that is useful for GCC.
7090 
7091      In some cases GCC dead code elimination removes the LO16 but keeps
7092      the corresponding HI16.  This is strictly speaking a violation of
7093      the ABI but not immediately harmful.  */
7094   lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7095   if (lo16_relocation == NULL)
7096     return FALSE;
7097 
7098   /* Obtain the addend kept there.  */
7099   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7100   l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7101 
7102   l <<= lo16_howto->rightshift;
7103   l = _bfd_mips_elf_sign_extend (l, 16);
7104 
7105   *addend <<= 16;
7106   *addend += l;
7107   return TRUE;
7108 }
7109 
7110 /* Try to read the contents of section SEC in bfd ABFD.  Return true and
7111    store the contents in *CONTENTS on success.  Assume that *CONTENTS
7112    already holds the contents if it is nonull on entry.  */
7113 
7114 static bfd_boolean
7115 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7116 {
7117   if (*contents)
7118     return TRUE;
7119 
7120   /* Get cached copy if it exists.  */
7121   if (elf_section_data (sec)->this_hdr.contents != NULL)
7122     {
7123       *contents = elf_section_data (sec)->this_hdr.contents;
7124       return TRUE;
7125     }
7126 
7127   return bfd_malloc_and_get_section (abfd, sec, contents);
7128 }
7129 
7130 /* Look through the relocs for a section during the first phase, and
7131    allocate space in the global offset table.  */
7132 
7133 bfd_boolean
7134 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7135 			    asection *sec, const Elf_Internal_Rela *relocs)
7136 {
7137   const char *name;
7138   bfd *dynobj;
7139   Elf_Internal_Shdr *symtab_hdr;
7140   struct elf_link_hash_entry **sym_hashes;
7141   size_t extsymoff;
7142   const Elf_Internal_Rela *rel;
7143   const Elf_Internal_Rela *rel_end;
7144   asection *sreloc;
7145   const struct elf_backend_data *bed;
7146   struct mips_elf_link_hash_table *htab;
7147   bfd_byte *contents;
7148   bfd_vma addend;
7149   reloc_howto_type *howto;
7150 
7151   if (info->relocatable)
7152     return TRUE;
7153 
7154   htab = mips_elf_hash_table (info);
7155   dynobj = elf_hash_table (info)->dynobj;
7156   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7157   sym_hashes = elf_sym_hashes (abfd);
7158   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7159 
7160   bed = get_elf_backend_data (abfd);
7161   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7162 
7163   /* Check for the mips16 stub sections.  */
7164 
7165   name = bfd_get_section_name (abfd, sec);
7166   if (FN_STUB_P (name))
7167     {
7168       unsigned long r_symndx;
7169 
7170       /* Look at the relocation information to figure out which symbol
7171          this is for.  */
7172 
7173       r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7174       if (r_symndx == 0)
7175 	{
7176 	  (*_bfd_error_handler)
7177 	    (_("%B: Warning: cannot determine the target function for"
7178 	       " stub section `%s'"),
7179 	     abfd, name);
7180 	  bfd_set_error (bfd_error_bad_value);
7181 	  return FALSE;
7182 	}
7183 
7184       if (r_symndx < extsymoff
7185 	  || sym_hashes[r_symndx - extsymoff] == NULL)
7186 	{
7187 	  asection *o;
7188 
7189 	  /* This stub is for a local symbol.  This stub will only be
7190              needed if there is some relocation in this BFD, other
7191              than a 16 bit function call, which refers to this symbol.  */
7192 	  for (o = abfd->sections; o != NULL; o = o->next)
7193 	    {
7194 	      Elf_Internal_Rela *sec_relocs;
7195 	      const Elf_Internal_Rela *r, *rend;
7196 
7197 	      /* We can ignore stub sections when looking for relocs.  */
7198 	      if ((o->flags & SEC_RELOC) == 0
7199 		  || o->reloc_count == 0
7200 		  || section_allows_mips16_refs_p (o))
7201 		continue;
7202 
7203 	      sec_relocs
7204 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7205 					     info->keep_memory);
7206 	      if (sec_relocs == NULL)
7207 		return FALSE;
7208 
7209 	      rend = sec_relocs + o->reloc_count;
7210 	      for (r = sec_relocs; r < rend; r++)
7211 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7212 		    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7213 		  break;
7214 
7215 	      if (elf_section_data (o)->relocs != sec_relocs)
7216 		free (sec_relocs);
7217 
7218 	      if (r < rend)
7219 		break;
7220 	    }
7221 
7222 	  if (o == NULL)
7223 	    {
7224 	      /* There is no non-call reloc for this stub, so we do
7225                  not need it.  Since this function is called before
7226                  the linker maps input sections to output sections, we
7227                  can easily discard it by setting the SEC_EXCLUDE
7228                  flag.  */
7229 	      sec->flags |= SEC_EXCLUDE;
7230 	      return TRUE;
7231 	    }
7232 
7233 	  /* Record this stub in an array of local symbol stubs for
7234              this BFD.  */
7235 	  if (elf_tdata (abfd)->local_stubs == NULL)
7236 	    {
7237 	      unsigned long symcount;
7238 	      asection **n;
7239 	      bfd_size_type amt;
7240 
7241 	      if (elf_bad_symtab (abfd))
7242 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7243 	      else
7244 		symcount = symtab_hdr->sh_info;
7245 	      amt = symcount * sizeof (asection *);
7246 	      n = bfd_zalloc (abfd, amt);
7247 	      if (n == NULL)
7248 		return FALSE;
7249 	      elf_tdata (abfd)->local_stubs = n;
7250 	    }
7251 
7252 	  sec->flags |= SEC_KEEP;
7253 	  elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7254 
7255 	  /* We don't need to set mips16_stubs_seen in this case.
7256              That flag is used to see whether we need to look through
7257              the global symbol table for stubs.  We don't need to set
7258              it here, because we just have a local stub.  */
7259 	}
7260       else
7261 	{
7262 	  struct mips_elf_link_hash_entry *h;
7263 
7264 	  h = ((struct mips_elf_link_hash_entry *)
7265 	       sym_hashes[r_symndx - extsymoff]);
7266 
7267 	  while (h->root.root.type == bfd_link_hash_indirect
7268 		 || h->root.root.type == bfd_link_hash_warning)
7269 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7270 
7271 	  /* H is the symbol this stub is for.  */
7272 
7273 	  /* If we already have an appropriate stub for this function, we
7274 	     don't need another one, so we can discard this one.  Since
7275 	     this function is called before the linker maps input sections
7276 	     to output sections, we can easily discard it by setting the
7277 	     SEC_EXCLUDE flag.  */
7278 	  if (h->fn_stub != NULL)
7279 	    {
7280 	      sec->flags |= SEC_EXCLUDE;
7281 	      return TRUE;
7282 	    }
7283 
7284 	  sec->flags |= SEC_KEEP;
7285 	  h->fn_stub = sec;
7286 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7287 	}
7288     }
7289   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7290     {
7291       unsigned long r_symndx;
7292       struct mips_elf_link_hash_entry *h;
7293       asection **loc;
7294 
7295       /* Look at the relocation information to figure out which symbol
7296          this is for.  */
7297 
7298       r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7299       if (r_symndx == 0)
7300 	{
7301 	  (*_bfd_error_handler)
7302 	    (_("%B: Warning: cannot determine the target function for"
7303 	       " stub section `%s'"),
7304 	     abfd, name);
7305 	  bfd_set_error (bfd_error_bad_value);
7306 	  return FALSE;
7307 	}
7308 
7309       if (r_symndx < extsymoff
7310 	  || sym_hashes[r_symndx - extsymoff] == NULL)
7311 	{
7312 	  asection *o;
7313 
7314 	  /* This stub is for a local symbol.  This stub will only be
7315              needed if there is some relocation (R_MIPS16_26) in this BFD
7316              that refers to this symbol.  */
7317 	  for (o = abfd->sections; o != NULL; o = o->next)
7318 	    {
7319 	      Elf_Internal_Rela *sec_relocs;
7320 	      const Elf_Internal_Rela *r, *rend;
7321 
7322 	      /* We can ignore stub sections when looking for relocs.  */
7323 	      if ((o->flags & SEC_RELOC) == 0
7324 		  || o->reloc_count == 0
7325 		  || section_allows_mips16_refs_p (o))
7326 		continue;
7327 
7328 	      sec_relocs
7329 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7330 					     info->keep_memory);
7331 	      if (sec_relocs == NULL)
7332 		return FALSE;
7333 
7334 	      rend = sec_relocs + o->reloc_count;
7335 	      for (r = sec_relocs; r < rend; r++)
7336 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7337 		    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7338 		    break;
7339 
7340 	      if (elf_section_data (o)->relocs != sec_relocs)
7341 		free (sec_relocs);
7342 
7343 	      if (r < rend)
7344 		break;
7345 	    }
7346 
7347 	  if (o == NULL)
7348 	    {
7349 	      /* There is no non-call reloc for this stub, so we do
7350                  not need it.  Since this function is called before
7351                  the linker maps input sections to output sections, we
7352                  can easily discard it by setting the SEC_EXCLUDE
7353                  flag.  */
7354 	      sec->flags |= SEC_EXCLUDE;
7355 	      return TRUE;
7356 	    }
7357 
7358 	  /* Record this stub in an array of local symbol call_stubs for
7359              this BFD.  */
7360 	  if (elf_tdata (abfd)->local_call_stubs == NULL)
7361 	    {
7362 	      unsigned long symcount;
7363 	      asection **n;
7364 	      bfd_size_type amt;
7365 
7366 	      if (elf_bad_symtab (abfd))
7367 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7368 	      else
7369 		symcount = symtab_hdr->sh_info;
7370 	      amt = symcount * sizeof (asection *);
7371 	      n = bfd_zalloc (abfd, amt);
7372 	      if (n == NULL)
7373 		return FALSE;
7374 	      elf_tdata (abfd)->local_call_stubs = n;
7375 	    }
7376 
7377 	  sec->flags |= SEC_KEEP;
7378 	  elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7379 
7380 	  /* We don't need to set mips16_stubs_seen in this case.
7381              That flag is used to see whether we need to look through
7382              the global symbol table for stubs.  We don't need to set
7383              it here, because we just have a local stub.  */
7384 	}
7385       else
7386 	{
7387 	  h = ((struct mips_elf_link_hash_entry *)
7388 	       sym_hashes[r_symndx - extsymoff]);
7389 
7390 	  /* H is the symbol this stub is for.  */
7391 
7392 	  if (CALL_FP_STUB_P (name))
7393 	    loc = &h->call_fp_stub;
7394 	  else
7395 	    loc = &h->call_stub;
7396 
7397 	  /* If we already have an appropriate stub for this function, we
7398 	     don't need another one, so we can discard this one.  Since
7399 	     this function is called before the linker maps input sections
7400 	     to output sections, we can easily discard it by setting the
7401 	     SEC_EXCLUDE flag.  */
7402 	  if (*loc != NULL)
7403 	    {
7404 	      sec->flags |= SEC_EXCLUDE;
7405 	      return TRUE;
7406 	    }
7407 
7408 	  sec->flags |= SEC_KEEP;
7409 	  *loc = sec;
7410 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7411 	}
7412     }
7413 
7414   sreloc = NULL;
7415   contents = NULL;
7416   for (rel = relocs; rel < rel_end; ++rel)
7417     {
7418       unsigned long r_symndx;
7419       unsigned int r_type;
7420       struct elf_link_hash_entry *h;
7421       bfd_boolean can_make_dynamic_p;
7422 
7423       r_symndx = ELF_R_SYM (abfd, rel->r_info);
7424       r_type = ELF_R_TYPE (abfd, rel->r_info);
7425 
7426       if (r_symndx < extsymoff)
7427 	h = NULL;
7428       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7429 	{
7430 	  (*_bfd_error_handler)
7431 	    (_("%B: Malformed reloc detected for section %s"),
7432 	     abfd, name);
7433 	  bfd_set_error (bfd_error_bad_value);
7434 	  return FALSE;
7435 	}
7436       else
7437 	{
7438 	  h = sym_hashes[r_symndx - extsymoff];
7439 	  while (h != NULL
7440 		 && (h->root.type == bfd_link_hash_indirect
7441 		     || h->root.type == bfd_link_hash_warning))
7442 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
7443 	}
7444 
7445       /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7446 	 relocation into a dynamic one.  */
7447       can_make_dynamic_p = FALSE;
7448       switch (r_type)
7449 	{
7450 	case R_MIPS16_GOT16:
7451 	case R_MIPS16_CALL16:
7452 	case R_MIPS_GOT16:
7453 	case R_MIPS_CALL16:
7454 	case R_MIPS_CALL_HI16:
7455 	case R_MIPS_CALL_LO16:
7456 	case R_MIPS_GOT_HI16:
7457 	case R_MIPS_GOT_LO16:
7458 	case R_MIPS_GOT_PAGE:
7459 	case R_MIPS_GOT_OFST:
7460 	case R_MIPS_GOT_DISP:
7461 	case R_MIPS_TLS_GOTTPREL:
7462 	case R_MIPS_TLS_GD:
7463 	case R_MIPS_TLS_LDM:
7464 	  if (dynobj == NULL)
7465 	    elf_hash_table (info)->dynobj = dynobj = abfd;
7466 	  if (!mips_elf_create_got_section (dynobj, info))
7467 	    return FALSE;
7468 	  if (htab->is_vxworks && !info->shared)
7469 	    {
7470 	      (*_bfd_error_handler)
7471 		(_("%B: GOT reloc at 0x%lx not expected in executables"),
7472 		 abfd, (unsigned long) rel->r_offset);
7473 	      bfd_set_error (bfd_error_bad_value);
7474 	      return FALSE;
7475 	    }
7476 	  break;
7477 
7478 	case R_MIPS_32:
7479 	case R_MIPS_REL32:
7480 	case R_MIPS_64:
7481 	  /* In VxWorks executables, references to external symbols
7482 	     must be handled using copy relocs or PLT entries; it is not
7483 	     possible to convert this relocation into a dynamic one.
7484 
7485 	     For executables that use PLTs and copy-relocs, we have a
7486 	     choice between converting the relocation into a dynamic
7487 	     one or using copy relocations or PLT entries.  It is
7488 	     usually better to do the former, unless the relocation is
7489 	     against a read-only section.  */
7490 	  if ((info->shared
7491 	       || (h != NULL
7492 		   && !htab->is_vxworks
7493 		   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7494 		   && !(!info->nocopyreloc
7495 			&& !PIC_OBJECT_P (abfd)
7496 			&& MIPS_ELF_READONLY_SECTION (sec))))
7497 	      && (sec->flags & SEC_ALLOC) != 0)
7498 	    {
7499 	      can_make_dynamic_p = TRUE;
7500 	      if (dynobj == NULL)
7501 		elf_hash_table (info)->dynobj = dynobj = abfd;
7502 	      break;
7503 	    }
7504 	  /* Fall through.  */
7505 
7506 	default:
7507 	  /* Most static relocations require pointer equality, except
7508 	     for branches.  */
7509 	  if (h)
7510 	    h->pointer_equality_needed = TRUE;
7511 	  /* Fall through.  */
7512 
7513 	case R_MIPS_26:
7514 	case R_MIPS_PC16:
7515 	case R_MIPS16_26:
7516 	  if (h)
7517 	    ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7518 	  break;
7519 	}
7520 
7521       if (h)
7522 	{
7523 	  /* Relocations against the special VxWorks __GOTT_BASE__ and
7524 	     __GOTT_INDEX__ symbols must be left to the loader.  Allocate
7525 	     room for them in .rela.dyn.  */
7526 	  if (is_gott_symbol (info, h))
7527 	    {
7528 	      if (sreloc == NULL)
7529 		{
7530 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
7531 		  if (sreloc == NULL)
7532 		    return FALSE;
7533 		}
7534 	      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7535 	      if (MIPS_ELF_READONLY_SECTION (sec))
7536 		/* We tell the dynamic linker that there are
7537 		   relocations against the text segment.  */
7538 		info->flags |= DF_TEXTREL;
7539 	    }
7540 	}
7541       else if (r_type == R_MIPS_CALL_LO16
7542 	       || r_type == R_MIPS_GOT_LO16
7543 	       || r_type == R_MIPS_GOT_DISP
7544 	       || (got16_reloc_p (r_type) && htab->is_vxworks))
7545 	{
7546 	  /* We may need a local GOT entry for this relocation.  We
7547 	     don't count R_MIPS_GOT_PAGE because we can estimate the
7548 	     maximum number of pages needed by looking at the size of
7549 	     the segment.  Similar comments apply to R_MIPS*_GOT16 and
7550 	     R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7551 	     always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
7552 	     R_MIPS_CALL_HI16 because these are always followed by an
7553 	     R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
7554 	  if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7555 						 rel->r_addend, info, 0))
7556 	    return FALSE;
7557 	}
7558 
7559       if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7560 	((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7561 
7562       switch (r_type)
7563 	{
7564 	case R_MIPS_CALL16:
7565 	case R_MIPS16_CALL16:
7566 	  if (h == NULL)
7567 	    {
7568 	      (*_bfd_error_handler)
7569 		(_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7570 		 abfd, (unsigned long) rel->r_offset);
7571 	      bfd_set_error (bfd_error_bad_value);
7572 	      return FALSE;
7573 	    }
7574 	  /* Fall through.  */
7575 
7576 	case R_MIPS_CALL_HI16:
7577 	case R_MIPS_CALL_LO16:
7578 	  if (h != NULL)
7579 	    {
7580 	      /* VxWorks call relocations point the function's .got.plt
7581 		 entry, which will be allocated by adjust_dynamic_symbol.
7582 		 Otherwise, this symbol requires a global GOT entry.  */
7583 	      if ((!htab->is_vxworks || h->forced_local)
7584 		  && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7585 		return FALSE;
7586 
7587 	      /* We need a stub, not a plt entry for the undefined
7588 		 function.  But we record it as if it needs plt.  See
7589 		 _bfd_elf_adjust_dynamic_symbol.  */
7590 	      h->needs_plt = 1;
7591 	      h->type = STT_FUNC;
7592 	    }
7593 	  break;
7594 
7595 	case R_MIPS_GOT_PAGE:
7596 	  /* If this is a global, overridable symbol, GOT_PAGE will
7597 	     decay to GOT_DISP, so we'll need a GOT entry for it.  */
7598 	  if (h)
7599 	    {
7600 	      struct mips_elf_link_hash_entry *hmips =
7601 		(struct mips_elf_link_hash_entry *) h;
7602 
7603 	      /* This symbol is definitely not overridable.  */
7604 	      if (hmips->root.def_regular
7605 		  && ! (info->shared && ! info->symbolic
7606 			&& ! hmips->root.forced_local))
7607 		h = NULL;
7608 	    }
7609 	  /* Fall through.  */
7610 
7611 	case R_MIPS16_GOT16:
7612 	case R_MIPS_GOT16:
7613 	case R_MIPS_GOT_HI16:
7614 	case R_MIPS_GOT_LO16:
7615 	  if (!h || r_type == R_MIPS_GOT_PAGE)
7616 	    {
7617 	      /* This relocation needs (or may need, if h != NULL) a
7618 		 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
7619 		 know for sure until we know whether the symbol is
7620 		 preemptible.  */
7621 	      if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7622 		{
7623 		  if (!mips_elf_get_section_contents (abfd, sec, &contents))
7624 		    return FALSE;
7625 		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7626 		  addend = mips_elf_read_rel_addend (abfd, rel,
7627 						     howto, contents);
7628 		  if (r_type == R_MIPS_GOT16)
7629 		    mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7630 						  contents, &addend);
7631 		  else
7632 		    addend <<= howto->rightshift;
7633 		}
7634 	      else
7635 		addend = rel->r_addend;
7636 	      if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7637 						   addend))
7638 		return FALSE;
7639 	      break;
7640 	    }
7641 	  /* Fall through.  */
7642 
7643 	case R_MIPS_GOT_DISP:
7644 	  if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7645 	    return FALSE;
7646 	  break;
7647 
7648 	case R_MIPS_TLS_GOTTPREL:
7649 	  if (info->shared)
7650 	    info->flags |= DF_STATIC_TLS;
7651 	  /* Fall through */
7652 
7653 	case R_MIPS_TLS_LDM:
7654 	  if (r_type == R_MIPS_TLS_LDM)
7655 	    {
7656 	      r_symndx = 0;
7657 	      h = NULL;
7658 	    }
7659 	  /* Fall through */
7660 
7661 	case R_MIPS_TLS_GD:
7662 	  /* This symbol requires a global offset table entry, or two
7663 	     for TLS GD relocations.  */
7664 	  {
7665 	    unsigned char flag = (r_type == R_MIPS_TLS_GD
7666 				  ? GOT_TLS_GD
7667 				  : r_type == R_MIPS_TLS_LDM
7668 				  ? GOT_TLS_LDM
7669 				  : GOT_TLS_IE);
7670 	    if (h != NULL)
7671 	      {
7672 		struct mips_elf_link_hash_entry *hmips =
7673 		  (struct mips_elf_link_hash_entry *) h;
7674 		hmips->tls_type |= flag;
7675 
7676 		if (h && !mips_elf_record_global_got_symbol (h, abfd,
7677 							     info, flag))
7678 		  return FALSE;
7679 	      }
7680 	    else
7681 	      {
7682 		BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7683 
7684 		if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7685 						       rel->r_addend,
7686 						       info, flag))
7687 		  return FALSE;
7688 	      }
7689 	  }
7690 	  break;
7691 
7692 	case R_MIPS_32:
7693 	case R_MIPS_REL32:
7694 	case R_MIPS_64:
7695 	  /* In VxWorks executables, references to external symbols
7696 	     are handled using copy relocs or PLT stubs, so there's
7697 	     no need to add a .rela.dyn entry for this relocation.  */
7698 	  if (can_make_dynamic_p)
7699 	    {
7700 	      if (sreloc == NULL)
7701 		{
7702 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
7703 		  if (sreloc == NULL)
7704 		    return FALSE;
7705 		}
7706 	      if (info->shared && h == NULL)
7707 		{
7708 		  /* When creating a shared object, we must copy these
7709 		     reloc types into the output file as R_MIPS_REL32
7710 		     relocs.  Make room for this reloc in .rel(a).dyn.  */
7711 		  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7712 		  /* In the N32 and 64-bit ABIs there may be multiple
7713 		     consecutive relocations for the same offset.  If we have
7714 		     a R_MIPS_GPREL32 followed by a R_MIPS_64 then that
7715 		     relocation is complete and needs no futher adjustment.  */
7716 		  if ((rel == relocs
7717 		      || rel[-1].r_offset != rel->r_offset
7718 		      || r_type != R_MIPS_64
7719 		      || ELF_R_TYPE(abfd, rel[-1].r_info) != R_MIPS_GPREL32)
7720 		      && MIPS_ELF_READONLY_SECTION (sec))
7721 		    {
7722 		      /* We tell the dynamic linker that there are
7723 		         relocations against the text segment.  */
7724 		      info->flags |= DF_TEXTREL;
7725 		      info->callbacks->warning
7726 			(info,
7727 			 _("relocation emitted against readonly section"),
7728 			 NULL, abfd, sec, rel->r_offset);
7729 		    }
7730 		}
7731 	      else
7732 		{
7733 		  struct mips_elf_link_hash_entry *hmips;
7734 
7735 		  /* For a shared object, we must copy this relocation
7736 		     unless the symbol turns out to be undefined and
7737 		     weak with non-default visibility, in which case
7738 		     it will be left as zero.
7739 
7740 		     We could elide R_MIPS_REL32 for locally binding symbols
7741 		     in shared libraries, but do not yet do so.
7742 
7743 		     For an executable, we only need to copy this
7744 		     reloc if the symbol is defined in a dynamic
7745 		     object.  */
7746 		  hmips = (struct mips_elf_link_hash_entry *) h;
7747 		  ++hmips->possibly_dynamic_relocs;
7748 		  if (MIPS_ELF_READONLY_SECTION (sec))
7749 		    /* We need it to tell the dynamic linker if there
7750 		       are relocations against the text segment.  */
7751 		    hmips->readonly_reloc = TRUE;
7752 		}
7753 	    }
7754 
7755 	  if (SGI_COMPAT (abfd))
7756 	    mips_elf_hash_table (info)->compact_rel_size +=
7757 	      sizeof (Elf32_External_crinfo);
7758 	  break;
7759 
7760 	case R_MIPS_26:
7761 	case R_MIPS_GPREL16:
7762 	case R_MIPS_LITERAL:
7763 	case R_MIPS_GPREL32:
7764 	  if (SGI_COMPAT (abfd))
7765 	    mips_elf_hash_table (info)->compact_rel_size +=
7766 	      sizeof (Elf32_External_crinfo);
7767 	  break;
7768 
7769 	  /* This relocation describes the C++ object vtable hierarchy.
7770 	     Reconstruct it for later use during GC.  */
7771 	case R_MIPS_GNU_VTINHERIT:
7772 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7773 	    return FALSE;
7774 	  break;
7775 
7776 	  /* This relocation describes which C++ vtable entries are actually
7777 	     used.  Record for later use during GC.  */
7778 	case R_MIPS_GNU_VTENTRY:
7779 	  BFD_ASSERT (h != NULL);
7780 	  if (h != NULL
7781 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7782 	    return FALSE;
7783 	  break;
7784 
7785 	default:
7786 	  break;
7787 	}
7788 
7789       /* We must not create a stub for a symbol that has relocations
7790 	 related to taking the function's address.  This doesn't apply to
7791 	 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7792 	 a normal .got entry.  */
7793       if (!htab->is_vxworks && h != NULL)
7794 	switch (r_type)
7795 	  {
7796 	  default:
7797 	    ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7798 	    break;
7799 	  case R_MIPS16_CALL16:
7800 	  case R_MIPS_CALL16:
7801 	  case R_MIPS_CALL_HI16:
7802 	  case R_MIPS_CALL_LO16:
7803 	  case R_MIPS_JALR:
7804 	    break;
7805 	  }
7806 
7807       /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7808 	 if there is one.  We only need to handle global symbols here;
7809 	 we decide whether to keep or delete stubs for local symbols
7810 	 when processing the stub's relocations.  */
7811       if (h != NULL
7812 	  && !mips16_call_reloc_p (r_type)
7813 	  && !section_allows_mips16_refs_p (sec))
7814 	{
7815 	  struct mips_elf_link_hash_entry *mh;
7816 
7817 	  mh = (struct mips_elf_link_hash_entry *) h;
7818 	  mh->need_fn_stub = TRUE;
7819 	}
7820 
7821       /* Refuse some position-dependent relocations when creating a
7822 	 shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
7823 	 not PIC, but we can create dynamic relocations and the result
7824 	 will be fine.  Also do not refuse R_MIPS_LO16, which can be
7825 	 combined with R_MIPS_GOT16.  */
7826       if (info->shared)
7827 	{
7828 	  switch (r_type)
7829 	    {
7830 	    case R_MIPS16_HI16:
7831 	    case R_MIPS_HI16:
7832 	    case R_MIPS_HIGHER:
7833 	    case R_MIPS_HIGHEST:
7834 	      /* Don't refuse a high part relocation if it's against
7835 		 no symbol (e.g. part of a compound relocation).  */
7836 	      if (r_symndx == 0)
7837 		break;
7838 
7839 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7840 		 and has a special meaning.  */
7841 	      if (!NEWABI_P (abfd) && h != NULL
7842 		  && strcmp (h->root.root.string, "_gp_disp") == 0)
7843 		break;
7844 
7845 	      /* FALLTHROUGH */
7846 
7847 	    case R_MIPS16_26:
7848 	    case R_MIPS_26:
7849 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7850 	      (*_bfd_error_handler)
7851 		(_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7852 		 abfd, howto->name,
7853 		 (h) ? h->root.root.string : "a local symbol");
7854 	      bfd_set_error (bfd_error_bad_value);
7855 	      return FALSE;
7856 	    default:
7857 	      break;
7858 	    }
7859 	}
7860     }
7861 
7862   return TRUE;
7863 }
7864 
7865 bfd_boolean
7866 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7867 			 struct bfd_link_info *link_info,
7868 			 bfd_boolean *again)
7869 {
7870   Elf_Internal_Rela *internal_relocs;
7871   Elf_Internal_Rela *irel, *irelend;
7872   Elf_Internal_Shdr *symtab_hdr;
7873   bfd_byte *contents = NULL;
7874   size_t extsymoff;
7875   bfd_boolean changed_contents = FALSE;
7876   bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7877   Elf_Internal_Sym *isymbuf = NULL;
7878 
7879   /* We are not currently changing any sizes, so only one pass.  */
7880   *again = FALSE;
7881 
7882   if (link_info->relocatable)
7883     return TRUE;
7884 
7885   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7886 					       link_info->keep_memory);
7887   if (internal_relocs == NULL)
7888     return TRUE;
7889 
7890   irelend = internal_relocs + sec->reloc_count
7891     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7892   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7893   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7894 
7895   for (irel = internal_relocs; irel < irelend; irel++)
7896     {
7897       bfd_vma symval;
7898       bfd_signed_vma sym_offset;
7899       unsigned int r_type;
7900       unsigned long r_symndx;
7901       asection *sym_sec;
7902       unsigned long instruction;
7903 
7904       /* Turn jalr into bgezal, and jr into beq, if they're marked
7905 	 with a JALR relocation, that indicate where they jump to.
7906 	 This saves some pipeline bubbles.  */
7907       r_type = ELF_R_TYPE (abfd, irel->r_info);
7908       if (r_type != R_MIPS_JALR)
7909 	continue;
7910 
7911       r_symndx = ELF_R_SYM (abfd, irel->r_info);
7912       /* Compute the address of the jump target.  */
7913       if (r_symndx >= extsymoff)
7914 	{
7915 	  struct mips_elf_link_hash_entry *h
7916 	    = ((struct mips_elf_link_hash_entry *)
7917 	       elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7918 
7919 	  while (h->root.root.type == bfd_link_hash_indirect
7920 		 || h->root.root.type == bfd_link_hash_warning)
7921 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7922 
7923 	  /* If a symbol is undefined, or if it may be overridden,
7924 	     skip it.  */
7925 	  if (! ((h->root.root.type == bfd_link_hash_defined
7926 		  || h->root.root.type == bfd_link_hash_defweak)
7927 		 && h->root.root.u.def.section)
7928 	      || (link_info->shared && ! link_info->symbolic
7929 		  && !h->root.forced_local))
7930 	    continue;
7931 
7932 	  sym_sec = h->root.root.u.def.section;
7933 	  if (sym_sec->output_section)
7934 	    symval = (h->root.root.u.def.value
7935 		      + sym_sec->output_section->vma
7936 		      + sym_sec->output_offset);
7937 	  else
7938 	    symval = h->root.root.u.def.value;
7939 	}
7940       else
7941 	{
7942 	  Elf_Internal_Sym *isym;
7943 
7944 	  /* Read this BFD's symbols if we haven't done so already.  */
7945 	  if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7946 	    {
7947 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7948 	      if (isymbuf == NULL)
7949 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7950 						symtab_hdr->sh_info, 0,
7951 						NULL, NULL, NULL);
7952 	      if (isymbuf == NULL)
7953 		goto relax_return;
7954 	    }
7955 
7956 	  isym = isymbuf + r_symndx;
7957 	  if (isym->st_shndx == SHN_UNDEF)
7958 	    continue;
7959 	  else if (isym->st_shndx == SHN_ABS)
7960 	    sym_sec = bfd_abs_section_ptr;
7961 	  else if (isym->st_shndx == SHN_COMMON)
7962 	    sym_sec = bfd_com_section_ptr;
7963 	  else
7964 	    sym_sec
7965 	      = bfd_section_from_elf_index (abfd, isym->st_shndx);
7966 	  symval = isym->st_value
7967 	    + sym_sec->output_section->vma
7968 	    + sym_sec->output_offset;
7969 	}
7970 
7971       /* Compute branch offset, from delay slot of the jump to the
7972 	 branch target.  */
7973       sym_offset = (symval + irel->r_addend)
7974 	- (sec_start + irel->r_offset + 4);
7975 
7976       /* Branch offset must be properly aligned.  */
7977       if ((sym_offset & 3) != 0)
7978 	continue;
7979 
7980       sym_offset >>= 2;
7981 
7982       /* Check that it's in range.  */
7983       if (sym_offset < -0x8000 || sym_offset >= 0x8000)
7984 	continue;
7985 
7986       /* Get the section contents if we haven't done so already.  */
7987       if (!mips_elf_get_section_contents (abfd, sec, &contents))
7988 	goto relax_return;
7989 
7990       instruction = bfd_get_32 (abfd, contents + irel->r_offset);
7991 
7992       /* If it was jalr <reg>, turn it into bgezal $zero, <target>.  */
7993       if ((instruction & 0xfc1fffff) == 0x0000f809)
7994 	instruction = 0x04110000;
7995       /* If it was jr <reg>, turn it into b <target>.  */
7996       else if ((instruction & 0xfc1fffff) == 0x00000008)
7997 	instruction = 0x10000000;
7998       else
7999 	continue;
8000 
8001       instruction |= (sym_offset & 0xffff);
8002       bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8003       changed_contents = TRUE;
8004     }
8005 
8006   if (contents != NULL
8007       && elf_section_data (sec)->this_hdr.contents != contents)
8008     {
8009       if (!changed_contents && !link_info->keep_memory)
8010         free (contents);
8011       else
8012         {
8013           /* Cache the section contents for elf_link_input_bfd.  */
8014           elf_section_data (sec)->this_hdr.contents = contents;
8015         }
8016     }
8017   return TRUE;
8018 
8019  relax_return:
8020   if (contents != NULL
8021       && elf_section_data (sec)->this_hdr.contents != contents)
8022     free (contents);
8023   return FALSE;
8024 }
8025 
8026 /* Allocate space for global sym dynamic relocs.  */
8027 
8028 static bfd_boolean
8029 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8030 {
8031   struct bfd_link_info *info = inf;
8032   bfd *dynobj;
8033   struct mips_elf_link_hash_entry *hmips;
8034   struct mips_elf_link_hash_table *htab;
8035 
8036   htab = mips_elf_hash_table (info);
8037   dynobj = elf_hash_table (info)->dynobj;
8038   hmips = (struct mips_elf_link_hash_entry *) h;
8039 
8040   /* VxWorks executables are handled elsewhere; we only need to
8041      allocate relocations in shared objects.  */
8042   if (htab->is_vxworks && !info->shared)
8043     return TRUE;
8044 
8045   /* Ignore indirect and warning symbols.  All relocations against
8046      such symbols will be redirected to the target symbol.  */
8047   if (h->root.type == bfd_link_hash_indirect
8048       || h->root.type == bfd_link_hash_warning)
8049     return TRUE;
8050 
8051   /* If this symbol is defined in a dynamic object, or we are creating
8052      a shared library, we will need to copy any R_MIPS_32 or
8053      R_MIPS_REL32 relocs against it into the output file.  */
8054   if (! info->relocatable
8055       && hmips->possibly_dynamic_relocs != 0
8056       && (h->root.type == bfd_link_hash_defweak
8057 	  || !h->def_regular
8058 	  || info->shared))
8059     {
8060       bfd_boolean do_copy = TRUE;
8061 
8062       if (h->root.type == bfd_link_hash_undefweak)
8063 	{
8064 	  /* Do not copy relocations for undefined weak symbols with
8065 	     non-default visibility.  */
8066 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8067 	    do_copy = FALSE;
8068 
8069 	  /* Make sure undefined weak symbols are output as a dynamic
8070 	     symbol in PIEs.  */
8071 	  else if (h->dynindx == -1 && !h->forced_local)
8072 	    {
8073 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8074 		return FALSE;
8075 	    }
8076 	}
8077 
8078       if (do_copy)
8079 	{
8080 	  /* Even though we don't directly need a GOT entry for this symbol,
8081 	     a symbol must have a dynamic symbol table index greater that
8082 	     DT_MIPS_GOTSYM if there are dynamic relocations against it.  */
8083 	  if (hmips->global_got_area > GGA_RELOC_ONLY)
8084 	    hmips->global_got_area = GGA_RELOC_ONLY;
8085 
8086 	  mips_elf_allocate_dynamic_relocations
8087 	    (dynobj, info, hmips->possibly_dynamic_relocs);
8088 	  if (hmips->readonly_reloc)
8089 	    /* We tell the dynamic linker that there are relocations
8090 	       against the text segment.  */
8091 	    info->flags |= DF_TEXTREL;
8092 	}
8093     }
8094 
8095   return TRUE;
8096 }
8097 
8098 /* Adjust a symbol defined by a dynamic object and referenced by a
8099    regular object.  The current definition is in some section of the
8100    dynamic object, but we're not including those sections.  We have to
8101    change the definition to something the rest of the link can
8102    understand.  */
8103 
8104 bfd_boolean
8105 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8106 				     struct elf_link_hash_entry *h)
8107 {
8108   bfd *dynobj;
8109   struct mips_elf_link_hash_entry *hmips;
8110   struct mips_elf_link_hash_table *htab;
8111 
8112   htab = mips_elf_hash_table (info);
8113   dynobj = elf_hash_table (info)->dynobj;
8114   hmips = (struct mips_elf_link_hash_entry *) h;
8115 
8116   /* Make sure we know what is going on here.  */
8117   BFD_ASSERT (dynobj != NULL
8118 	      && (h->needs_plt
8119 		  || h->u.weakdef != NULL
8120 		  || (h->def_dynamic
8121 		      && h->ref_regular
8122 		      && !h->def_regular)));
8123 
8124   hmips = (struct mips_elf_link_hash_entry *) h;
8125 
8126   /* If there are call relocations against an externally-defined symbol,
8127      see whether we can create a MIPS lazy-binding stub for it.  We can
8128      only do this if all references to the function are through call
8129      relocations, and in that case, the traditional lazy-binding stubs
8130      are much more efficient than PLT entries.
8131 
8132      Traditional stubs are only available on SVR4 psABI-based systems;
8133      VxWorks always uses PLTs instead.  */
8134   if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8135     {
8136       if (! elf_hash_table (info)->dynamic_sections_created)
8137 	return TRUE;
8138 
8139       /* If this symbol is not defined in a regular file, then set
8140 	 the symbol to the stub location.  This is required to make
8141 	 function pointers compare as equal between the normal
8142 	 executable and the shared library.  */
8143       if (!h->def_regular)
8144 	{
8145 	  hmips->needs_lazy_stub = TRUE;
8146 	  htab->lazy_stub_count++;
8147 	  return TRUE;
8148 	}
8149     }
8150   /* As above, VxWorks requires PLT entries for externally-defined
8151      functions that are only accessed through call relocations.
8152 
8153      Both VxWorks and non-VxWorks targets also need PLT entries if there
8154      are static-only relocations against an externally-defined function.
8155      This can technically occur for shared libraries if there are
8156      branches to the symbol, although it is unlikely that this will be
8157      used in practice due to the short ranges involved.  It can occur
8158      for any relative or absolute relocation in executables; in that
8159      case, the PLT entry becomes the function's canonical address.  */
8160   else if (((h->needs_plt && !hmips->no_fn_stub)
8161 	    || (h->type == STT_FUNC && hmips->has_static_relocs))
8162 	   && htab->use_plts_and_copy_relocs
8163 	   && !SYMBOL_CALLS_LOCAL (info, h)
8164 	   && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8165 		&& h->root.type == bfd_link_hash_undefweak))
8166     {
8167       /* If this is the first symbol to need a PLT entry, allocate room
8168 	 for the header.  */
8169       if (htab->splt->size == 0)
8170 	{
8171 	  BFD_ASSERT (htab->sgotplt->size == 0);
8172 
8173 	  /* If we're using the PLT additions to the psABI, each PLT
8174 	     entry is 16 bytes and the PLT0 entry is 32 bytes.
8175 	     Encourage better cache usage by aligning.  We do this
8176 	     lazily to avoid pessimizing traditional objects.  */
8177 	  if (!htab->is_vxworks
8178 	      && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8179 	    return FALSE;
8180 
8181 	  /* Make sure that .got.plt is word-aligned.  We do this lazily
8182 	     for the same reason as above.  */
8183 	  if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8184 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8185 	    return FALSE;
8186 
8187 	  htab->splt->size += htab->plt_header_size;
8188 
8189 	  /* On non-VxWorks targets, the first two entries in .got.plt
8190 	     are reserved.  */
8191 	  if (!htab->is_vxworks)
8192 	    htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
8193 
8194 	  /* On VxWorks, also allocate room for the header's
8195 	     .rela.plt.unloaded entries.  */
8196 	  if (htab->is_vxworks && !info->shared)
8197 	    htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8198 	}
8199 
8200       /* Assign the next .plt entry to this symbol.  */
8201       h->plt.offset = htab->splt->size;
8202       htab->splt->size += htab->plt_entry_size;
8203 
8204       /* If the output file has no definition of the symbol, set the
8205 	 symbol's value to the address of the stub.  */
8206       if (!info->shared && !h->def_regular)
8207 	{
8208 	  h->root.u.def.section = htab->splt;
8209 	  h->root.u.def.value = h->plt.offset;
8210 	  /* For VxWorks, point at the PLT load stub rather than the
8211 	     lazy resolution stub; this stub will become the canonical
8212 	     function address.  */
8213 	  if (htab->is_vxworks)
8214 	    h->root.u.def.value += 8;
8215 	}
8216 
8217       /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8218 	 relocation.  */
8219       htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8220       htab->srelplt->size += (htab->is_vxworks
8221 			      ? MIPS_ELF_RELA_SIZE (dynobj)
8222 			      : MIPS_ELF_REL_SIZE (dynobj));
8223 
8224       /* Make room for the .rela.plt.unloaded relocations.  */
8225       if (htab->is_vxworks && !info->shared)
8226 	htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8227 
8228       /* All relocations against this symbol that could have been made
8229 	 dynamic will now refer to the PLT entry instead.  */
8230       hmips->possibly_dynamic_relocs = 0;
8231 
8232       return TRUE;
8233     }
8234 
8235   /* If this is a weak symbol, and there is a real definition, the
8236      processor independent code will have arranged for us to see the
8237      real definition first, and we can just use the same value.  */
8238   if (h->u.weakdef != NULL)
8239     {
8240       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8241 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
8242       h->root.u.def.section = h->u.weakdef->root.u.def.section;
8243       h->root.u.def.value = h->u.weakdef->root.u.def.value;
8244       return TRUE;
8245     }
8246 
8247   /* Otherwise, there is nothing further to do for symbols defined
8248      in regular objects.  */
8249   if (h->def_regular)
8250     return TRUE;
8251 
8252   /* There's also nothing more to do if we'll convert all relocations
8253      against this symbol into dynamic relocations.  */
8254   if (!hmips->has_static_relocs)
8255     return TRUE;
8256 
8257   /* We're now relying on copy relocations.  Complain if we have
8258      some that we can't convert.  */
8259   if (!htab->use_plts_and_copy_relocs || info->shared)
8260     {
8261       (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8262 			       "dynamic symbol %s"),
8263 			     h->root.root.string);
8264       bfd_set_error (bfd_error_bad_value);
8265       return FALSE;
8266     }
8267 
8268   /* We must allocate the symbol in our .dynbss section, which will
8269      become part of the .bss section of the executable.  There will be
8270      an entry for this symbol in the .dynsym section.  The dynamic
8271      object will contain position independent code, so all references
8272      from the dynamic object to this symbol will go through the global
8273      offset table.  The dynamic linker will use the .dynsym entry to
8274      determine the address it must put in the global offset table, so
8275      both the dynamic object and the regular object will refer to the
8276      same memory location for the variable.  */
8277 
8278   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8279     {
8280       if (htab->is_vxworks)
8281 	htab->srelbss->size += sizeof (Elf32_External_Rela);
8282       else
8283 	mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8284       h->needs_copy = 1;
8285     }
8286 
8287   /* All relocations against this symbol that could have been made
8288      dynamic will now refer to the local copy instead.  */
8289   hmips->possibly_dynamic_relocs = 0;
8290 
8291   return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8292 }
8293 
8294 /* This function is called after all the input files have been read,
8295    and the input sections have been assigned to output sections.  We
8296    check for any mips16 stub sections that we can discard.  */
8297 
8298 bfd_boolean
8299 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
8300 				    struct bfd_link_info *info)
8301 {
8302   asection *ri;
8303   struct mips_elf_link_hash_table *htab;
8304   struct mips_htab_traverse_info hti;
8305 
8306   htab = mips_elf_hash_table (info);
8307 
8308   /* The .reginfo section has a fixed size.  */
8309   ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8310   if (ri != NULL)
8311     bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8312 
8313   hti.info = info;
8314   hti.output_bfd = output_bfd;
8315   hti.error = FALSE;
8316   mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8317 			       mips_elf_check_symbols, &hti);
8318   if (hti.error)
8319     return FALSE;
8320 
8321   return TRUE;
8322 }
8323 
8324 /* If the link uses a GOT, lay it out and work out its size.  */
8325 
8326 static bfd_boolean
8327 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8328 {
8329   bfd *dynobj;
8330   asection *s;
8331   struct mips_got_info *g;
8332   bfd_size_type loadable_size = 0;
8333   bfd_size_type page_gotno;
8334   bfd *sub;
8335   struct mips_elf_count_tls_arg count_tls_arg;
8336   struct mips_elf_link_hash_table *htab;
8337 
8338   htab = mips_elf_hash_table (info);
8339   s = htab->sgot;
8340   if (s == NULL)
8341     return TRUE;
8342 
8343   dynobj = elf_hash_table (info)->dynobj;
8344   g = htab->got_info;
8345 
8346   /* Allocate room for the reserved entries.  VxWorks always reserves
8347      3 entries; other objects only reserve 2 entries.  */
8348   BFD_ASSERT (g->assigned_gotno == 0);
8349   if (htab->is_vxworks)
8350     htab->reserved_gotno = 3;
8351   else
8352     htab->reserved_gotno = 2;
8353   g->local_gotno += htab->reserved_gotno;
8354   g->assigned_gotno = htab->reserved_gotno;
8355 
8356   /* Replace entries for indirect and warning symbols with entries for
8357      the target symbol.  */
8358   if (!mips_elf_resolve_final_got_entries (g))
8359     return FALSE;
8360 
8361   /* Count the number of GOT symbols.  */
8362   mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
8363 
8364   /* Calculate the total loadable size of the output.  That
8365      will give us the maximum number of GOT_PAGE entries
8366      required.  */
8367   for (sub = info->input_bfds; sub; sub = sub->link_next)
8368     {
8369       asection *subsection;
8370 
8371       for (subsection = sub->sections;
8372 	   subsection;
8373 	   subsection = subsection->next)
8374 	{
8375 	  if ((subsection->flags & SEC_ALLOC) == 0)
8376 	    continue;
8377 	  loadable_size += ((subsection->size + 0xf)
8378 			    &~ (bfd_size_type) 0xf);
8379 	}
8380     }
8381 
8382   if (htab->is_vxworks)
8383     /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8384        relocations against local symbols evaluate to "G", and the EABI does
8385        not include R_MIPS_GOT_PAGE.  */
8386     page_gotno = 0;
8387   else
8388     /* Assume there are two loadable segments consisting of contiguous
8389        sections.  Is 5 enough?  */
8390     page_gotno = (loadable_size >> 16) + 5;
8391 
8392   /* Choose the smaller of the two estimates; both are intended to be
8393      conservative.  */
8394   if (page_gotno > g->page_gotno)
8395     page_gotno = g->page_gotno;
8396 
8397   g->local_gotno += page_gotno;
8398   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8399   s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8400 
8401   /* We need to calculate tls_gotno for global symbols at this point
8402      instead of building it up earlier, to avoid doublecounting
8403      entries for one global symbol from multiple input files.  */
8404   count_tls_arg.info = info;
8405   count_tls_arg.needed = 0;
8406   elf_link_hash_traverse (elf_hash_table (info),
8407 			  mips_elf_count_global_tls_entries,
8408 			  &count_tls_arg);
8409   g->tls_gotno += count_tls_arg.needed;
8410   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8411 
8412   /* VxWorks does not support multiple GOTs.  It initializes $gp to
8413      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8414      dynamic loader.  */
8415   if (htab->is_vxworks)
8416     {
8417       /* VxWorks executables do not need a GOT.  */
8418       if (info->shared)
8419 	{
8420 	  /* Each VxWorks GOT entry needs an explicit relocation.  */
8421 	  unsigned int count;
8422 
8423 	  count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
8424 	  if (count)
8425 	    mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8426 	}
8427     }
8428   else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8429     {
8430       if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8431 	return FALSE;
8432     }
8433   else
8434     {
8435       struct mips_elf_count_tls_arg arg;
8436 
8437       /* Set up TLS entries.  */
8438       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8439       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
8440 
8441       /* Allocate room for the TLS relocations.  */
8442       arg.info = info;
8443       arg.needed = 0;
8444       htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8445       elf_link_hash_traverse (elf_hash_table (info),
8446 			      mips_elf_count_global_tls_relocs,
8447 			      &arg);
8448       if (arg.needed)
8449 	mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
8450     }
8451 
8452   return TRUE;
8453 }
8454 
8455 /* Estimate the size of the .MIPS.stubs section.  */
8456 
8457 static void
8458 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8459 {
8460   struct mips_elf_link_hash_table *htab;
8461   bfd_size_type dynsymcount;
8462 
8463   htab = mips_elf_hash_table (info);
8464   if (htab->lazy_stub_count == 0)
8465     return;
8466 
8467   /* IRIX rld assumes that a function stub isn't at the end of the .text
8468      section, so add a dummy entry to the end.  */
8469   htab->lazy_stub_count++;
8470 
8471   /* Get a worst-case estimate of the number of dynamic symbols needed.
8472      At this point, dynsymcount does not account for section symbols
8473      and count_section_dynsyms may overestimate the number that will
8474      be needed.  */
8475   dynsymcount = (elf_hash_table (info)->dynsymcount
8476 		 + count_section_dynsyms (output_bfd, info));
8477 
8478   /* Determine the size of one stub entry.  */
8479   htab->function_stub_size = (dynsymcount > 0x10000
8480 			      ? MIPS_FUNCTION_STUB_BIG_SIZE
8481 			      : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8482 
8483   htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8484 }
8485 
8486 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8487    MIPS hash table.  If H needs a traditional MIPS lazy-binding stub,
8488    allocate an entry in the stubs section.  */
8489 
8490 static bfd_boolean
8491 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8492 {
8493   struct mips_elf_link_hash_table *htab;
8494 
8495   htab = (struct mips_elf_link_hash_table *) data;
8496   if (h->needs_lazy_stub)
8497     {
8498       h->root.root.u.def.section = htab->sstubs;
8499       h->root.root.u.def.value = htab->sstubs->size;
8500       h->root.plt.offset = htab->sstubs->size;
8501       htab->sstubs->size += htab->function_stub_size;
8502     }
8503   return TRUE;
8504 }
8505 
8506 /* Allocate offsets in the stubs section to each symbol that needs one.
8507    Set the final size of the .MIPS.stub section.  */
8508 
8509 static void
8510 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8511 {
8512   struct mips_elf_link_hash_table *htab;
8513 
8514   htab = mips_elf_hash_table (info);
8515   if (htab->lazy_stub_count == 0)
8516     return;
8517 
8518   htab->sstubs->size = 0;
8519   mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8520 			       mips_elf_allocate_lazy_stub, htab);
8521   htab->sstubs->size += htab->function_stub_size;
8522   BFD_ASSERT (htab->sstubs->size
8523 	      == htab->lazy_stub_count * htab->function_stub_size);
8524 }
8525 
8526 /* Set the sizes of the dynamic sections.  */
8527 
8528 bfd_boolean
8529 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8530 				     struct bfd_link_info *info)
8531 {
8532   bfd *dynobj;
8533   asection *s, *sreldyn;
8534   bfd_boolean reltext;
8535   struct mips_elf_link_hash_table *htab;
8536 
8537   htab = mips_elf_hash_table (info);
8538   dynobj = elf_hash_table (info)->dynobj;
8539   BFD_ASSERT (dynobj != NULL);
8540 
8541   if (elf_hash_table (info)->dynamic_sections_created)
8542     {
8543       /* Set the contents of the .interp section to the interpreter.  */
8544       if (info->executable)
8545 	{
8546 	  s = bfd_get_section_by_name (dynobj, ".interp");
8547 	  BFD_ASSERT (s != NULL);
8548 	  s->size
8549 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8550 	  s->contents
8551 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8552 	}
8553 
8554       /* Create a symbol for the PLT, if we know that we are using it.  */
8555       if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8556 	{
8557 	  struct elf_link_hash_entry *h;
8558 
8559 	  BFD_ASSERT (htab->use_plts_and_copy_relocs);
8560 
8561 	  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8562 					   "_PROCEDURE_LINKAGE_TABLE_");
8563 	  htab->root.hplt = h;
8564 	  if (h == NULL)
8565 	    return FALSE;
8566 	  h->type = STT_FUNC;
8567 	}
8568     }
8569 
8570   /* Allocate space for global sym dynamic relocs.  */
8571   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8572 
8573   mips_elf_estimate_stub_size (output_bfd, info);
8574 
8575   if (!mips_elf_lay_out_got (output_bfd, info))
8576     return FALSE;
8577 
8578   mips_elf_lay_out_lazy_stubs (info);
8579 
8580   /* The check_relocs and adjust_dynamic_symbol entry points have
8581      determined the sizes of the various dynamic sections.  Allocate
8582      memory for them.  */
8583   reltext = FALSE;
8584   for (s = dynobj->sections; s != NULL; s = s->next)
8585     {
8586       const char *name;
8587 
8588       /* It's OK to base decisions on the section name, because none
8589 	 of the dynobj section names depend upon the input files.  */
8590       name = bfd_get_section_name (dynobj, s);
8591 
8592       if ((s->flags & SEC_LINKER_CREATED) == 0)
8593 	continue;
8594 
8595       if (CONST_STRNEQ (name, ".rel"))
8596 	{
8597 	  if (s->size != 0)
8598 	    {
8599 	      const char *outname;
8600 	      asection *target;
8601 
8602 	      /* If this relocation section applies to a read only
8603                  section, then we probably need a DT_TEXTREL entry.
8604                  If the relocation section is .rel(a).dyn, we always
8605                  assert a DT_TEXTREL entry rather than testing whether
8606                  there exists a relocation to a read only section or
8607                  not.  */
8608 	      outname = bfd_get_section_name (output_bfd,
8609 					      s->output_section);
8610 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
8611 	      if ((target != NULL
8612 		   && (target->flags & SEC_READONLY) != 0
8613 		   && (target->flags & SEC_ALLOC) != 0)
8614 		  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8615 		reltext = TRUE;
8616 
8617 	      /* We use the reloc_count field as a counter if we need
8618 		 to copy relocs into the output file.  */
8619 	      if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8620 		s->reloc_count = 0;
8621 
8622 	      /* If combreloc is enabled, elf_link_sort_relocs() will
8623 		 sort relocations, but in a different way than we do,
8624 		 and before we're done creating relocations.  Also, it
8625 		 will move them around between input sections'
8626 		 relocation's contents, so our sorting would be
8627 		 broken, so don't let it run.  */
8628 	      info->combreloc = 0;
8629 	    }
8630 	}
8631       else if (! info->shared
8632 	       && ! mips_elf_hash_table (info)->use_rld_obj_head
8633 	       && CONST_STRNEQ (name, ".rld_map"))
8634 	{
8635 	  /* We add a room for __rld_map.  It will be filled in by the
8636 	     rtld to contain a pointer to the _r_debug structure.  */
8637 	  s->size += 4;
8638 	}
8639       else if (SGI_COMPAT (output_bfd)
8640 	       && CONST_STRNEQ (name, ".compact_rel"))
8641 	s->size += mips_elf_hash_table (info)->compact_rel_size;
8642       else if (s == htab->splt)
8643 	{
8644 	  /* If the last PLT entry has a branch delay slot, allocate
8645 	     room for an extra nop to fill the delay slot.  */
8646 	  if (!htab->is_vxworks && s->size > 0)
8647 	    s->size += 4;
8648 	}
8649       else if (! CONST_STRNEQ (name, ".init")
8650 	       && s != htab->sgot
8651 	       && s != htab->sgotplt
8652 	       && s != htab->sstubs
8653 	       && s != htab->sdynbss)
8654 	{
8655 	  /* It's not one of our sections, so don't allocate space.  */
8656 	  continue;
8657 	}
8658 
8659       if (s->size == 0)
8660 	{
8661 	  s->flags |= SEC_EXCLUDE;
8662 	  continue;
8663 	}
8664 
8665       if ((s->flags & SEC_HAS_CONTENTS) == 0)
8666 	continue;
8667 
8668       /* Allocate memory for the section contents.  */
8669       s->contents = bfd_zalloc (dynobj, s->size);
8670       if (s->contents == NULL)
8671 	{
8672 	  bfd_set_error (bfd_error_no_memory);
8673 	  return FALSE;
8674 	}
8675     }
8676 
8677   if (elf_hash_table (info)->dynamic_sections_created)
8678     {
8679       /* Add some entries to the .dynamic section.  We fill in the
8680 	 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8681 	 must add the entries now so that we get the correct size for
8682 	 the .dynamic section.  */
8683 
8684       /* SGI object has the equivalence of DT_DEBUG in the
8685 	 DT_MIPS_RLD_MAP entry.  This must come first because glibc
8686 	 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8687 	 looks at the first one it sees.  */
8688       if (!info->shared
8689 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8690 	return FALSE;
8691 
8692       /* The DT_DEBUG entry may be filled in by the dynamic linker and
8693 	 used by the debugger.  */
8694       if (info->executable
8695 	  && !SGI_COMPAT (output_bfd)
8696 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8697 	return FALSE;
8698 
8699       if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8700 	info->flags |= DF_TEXTREL;
8701 
8702       if ((info->flags & DF_TEXTREL) != 0)
8703 	{
8704 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8705 	    return FALSE;
8706 
8707 	  /* Clear the DF_TEXTREL flag.  It will be set again if we
8708 	     write out an actual text relocation; we may not, because
8709 	     at this point we do not know whether e.g. any .eh_frame
8710 	     absolute relocations have been converted to PC-relative.  */
8711 	  info->flags &= ~DF_TEXTREL;
8712 	}
8713 
8714       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8715 	return FALSE;
8716 
8717       sreldyn = mips_elf_rel_dyn_section (info, FALSE);
8718       if (htab->is_vxworks)
8719 	{
8720 	  /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
8721 	     use any of the DT_MIPS_* tags.  */
8722 	  if (sreldyn && sreldyn->size > 0)
8723 	    {
8724 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8725 		return FALSE;
8726 
8727 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8728 		return FALSE;
8729 
8730 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8731 		return FALSE;
8732 	    }
8733 	}
8734       else
8735 	{
8736 	  if (sreldyn && sreldyn->size > 0)
8737 	    {
8738 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8739 		return FALSE;
8740 
8741 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8742 		return FALSE;
8743 
8744 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8745 		return FALSE;
8746 	    }
8747 
8748 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8749 	    return FALSE;
8750 
8751 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8752 	    return FALSE;
8753 
8754 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8755 	    return FALSE;
8756 
8757 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8758 	    return FALSE;
8759 
8760 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8761 	    return FALSE;
8762 
8763 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8764 	    return FALSE;
8765 
8766 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8767 	    return FALSE;
8768 
8769 	  if (IRIX_COMPAT (dynobj) == ict_irix5
8770 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8771 	    return FALSE;
8772 
8773 	  if (IRIX_COMPAT (dynobj) == ict_irix6
8774 	      && (bfd_get_section_by_name
8775 		  (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8776 	      && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8777 	    return FALSE;
8778 	}
8779       if (htab->splt->size > 0)
8780 	{
8781 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8782 	    return FALSE;
8783 
8784 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8785 	    return FALSE;
8786 
8787 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8788 	    return FALSE;
8789 
8790 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8791 	    return FALSE;
8792 	}
8793       if (htab->is_vxworks
8794 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8795 	return FALSE;
8796     }
8797 
8798   return TRUE;
8799 }
8800 
8801 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8802    Adjust its R_ADDEND field so that it is correct for the output file.
8803    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8804    and sections respectively; both use symbol indexes.  */
8805 
8806 static void
8807 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8808 			bfd *input_bfd, Elf_Internal_Sym *local_syms,
8809 			asection **local_sections, Elf_Internal_Rela *rel)
8810 {
8811   unsigned int r_type, r_symndx;
8812   Elf_Internal_Sym *sym;
8813   asection *sec;
8814 
8815   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8816     {
8817       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8818       if (r_type == R_MIPS16_GPREL
8819 	  || r_type == R_MIPS_GPREL16
8820 	  || r_type == R_MIPS_GPREL32
8821 	  || r_type == R_MIPS_LITERAL)
8822 	{
8823 	  rel->r_addend += _bfd_get_gp_value (input_bfd);
8824 	  rel->r_addend -= _bfd_get_gp_value (output_bfd);
8825 	}
8826 
8827       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8828       sym = local_syms + r_symndx;
8829 
8830       /* Adjust REL's addend to account for section merging.  */
8831       if (!info->relocatable)
8832 	{
8833 	  sec = local_sections[r_symndx];
8834 	  _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8835 	}
8836 
8837       /* This would normally be done by the rela_normal code in elflink.c.  */
8838       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8839 	rel->r_addend += local_sections[r_symndx]->output_offset;
8840     }
8841 }
8842 
8843 /* Relocate a MIPS ELF section.  */
8844 
8845 bfd_boolean
8846 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8847 				bfd *input_bfd, asection *input_section,
8848 				bfd_byte *contents, Elf_Internal_Rela *relocs,
8849 				Elf_Internal_Sym *local_syms,
8850 				asection **local_sections)
8851 {
8852   Elf_Internal_Rela *rel;
8853   const Elf_Internal_Rela *relend;
8854   bfd_vma addend = 0;
8855   bfd_boolean use_saved_addend_p = FALSE;
8856   const struct elf_backend_data *bed;
8857 
8858   bed = get_elf_backend_data (output_bfd);
8859   relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8860   for (rel = relocs; rel < relend; ++rel)
8861     {
8862       const char *name;
8863       bfd_vma value = 0;
8864       reloc_howto_type *howto;
8865       bfd_boolean require_jalx;
8866       /* TRUE if the relocation is a RELA relocation, rather than a
8867          REL relocation.  */
8868       bfd_boolean rela_relocation_p = TRUE;
8869       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8870       const char *msg;
8871       unsigned long r_symndx;
8872       asection *sec;
8873       Elf_Internal_Shdr *symtab_hdr;
8874       struct elf_link_hash_entry *h;
8875 
8876       /* Find the relocation howto for this relocation.  */
8877       howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8878 				       NEWABI_P (input_bfd)
8879 				       && (MIPS_RELOC_RELA_P
8880 					   (input_bfd, input_section,
8881 					    rel - relocs)));
8882 
8883       r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
8884       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8885       if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8886 	{
8887 	  sec = local_sections[r_symndx];
8888 	  h = NULL;
8889 	}
8890       else
8891 	{
8892 	  unsigned long extsymoff;
8893 
8894 	  extsymoff = 0;
8895 	  if (!elf_bad_symtab (input_bfd))
8896 	    extsymoff = symtab_hdr->sh_info;
8897 	  h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8898 	  while (h->root.type == bfd_link_hash_indirect
8899 		 || h->root.type == bfd_link_hash_warning)
8900 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8901 
8902 	  sec = NULL;
8903 	  if (h->root.type == bfd_link_hash_defined
8904 	      || h->root.type == bfd_link_hash_defweak)
8905 	    sec = h->root.u.def.section;
8906 	}
8907 
8908       if (sec != NULL && elf_discarded_section (sec))
8909 	{
8910 	  /* For relocs against symbols from removed linkonce sections,
8911 	     or sections discarded by a linker script, we just want the
8912 	     section contents zeroed.  Avoid any special processing.  */
8913 	  _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8914 	  rel->r_info = 0;
8915 	  rel->r_addend = 0;
8916 	  continue;
8917 	}
8918 
8919       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
8920 	{
8921 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
8922 	     64-bit code, but make sure all their addresses are in the
8923 	     lowermost or uppermost 32-bit section of the 64-bit address
8924 	     space.  Thus, when they use an R_MIPS_64 they mean what is
8925 	     usually meant by R_MIPS_32, with the exception that the
8926 	     stored value is sign-extended to 64 bits.  */
8927 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
8928 
8929 	  /* On big-endian systems, we need to lie about the position
8930 	     of the reloc.  */
8931 	  if (bfd_big_endian (input_bfd))
8932 	    rel->r_offset += 4;
8933 	}
8934 
8935       if (!use_saved_addend_p)
8936 	{
8937 	  /* If these relocations were originally of the REL variety,
8938 	     we must pull the addend out of the field that will be
8939 	     relocated.  Otherwise, we simply use the contents of the
8940 	     RELA relocation.  */
8941 	  if (mips_elf_rel_relocation_p (input_bfd, input_section,
8942 					 relocs, rel))
8943 	    {
8944 	      rela_relocation_p = FALSE;
8945 	      addend = mips_elf_read_rel_addend (input_bfd, rel,
8946 						 howto, contents);
8947 	      if (hi16_reloc_p (r_type)
8948 		  || (got16_reloc_p (r_type)
8949 		      && mips_elf_local_relocation_p (input_bfd, rel,
8950 						      local_sections, FALSE)))
8951 		{
8952 		  if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8953 						     contents, &addend))
8954 		    {
8955 		      const char *name;
8956 
8957 		      if (h)
8958 			name = h->root.root.string;
8959 		      else
8960 			name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8961 						 local_syms + r_symndx,
8962 						 sec);
8963 		      (*_bfd_error_handler)
8964 			(_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8965 			 input_bfd, input_section, name, howto->name,
8966 			 rel->r_offset);
8967 		    }
8968 		}
8969 	      else
8970 		addend <<= howto->rightshift;
8971 	    }
8972 	  else
8973 	    addend = rel->r_addend;
8974 	  mips_elf_adjust_addend (output_bfd, info, input_bfd,
8975 				  local_syms, local_sections, rel);
8976 	}
8977 
8978       if (info->relocatable)
8979 	{
8980 	  if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
8981 	      && bfd_big_endian (input_bfd))
8982 	    rel->r_offset -= 4;
8983 
8984 	  if (!rela_relocation_p && rel->r_addend)
8985 	    {
8986 	      addend += rel->r_addend;
8987 	      if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
8988 		addend = mips_elf_high (addend);
8989 	      else if (r_type == R_MIPS_HIGHER)
8990 		addend = mips_elf_higher (addend);
8991 	      else if (r_type == R_MIPS_HIGHEST)
8992 		addend = mips_elf_highest (addend);
8993 	      else
8994 		addend >>= howto->rightshift;
8995 
8996 	      /* We use the source mask, rather than the destination
8997 		 mask because the place to which we are writing will be
8998 		 source of the addend in the final link.  */
8999 	      addend &= howto->src_mask;
9000 
9001 	      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9002 		/* See the comment above about using R_MIPS_64 in the 32-bit
9003 		   ABI.  Here, we need to update the addend.  It would be
9004 		   possible to get away with just using the R_MIPS_32 reloc
9005 		   but for endianness.  */
9006 		{
9007 		  bfd_vma sign_bits;
9008 		  bfd_vma low_bits;
9009 		  bfd_vma high_bits;
9010 
9011 		  if (addend & ((bfd_vma) 1 << 31))
9012 #ifdef BFD64
9013 		    sign_bits = ((bfd_vma) 1 << 32) - 1;
9014 #else
9015 		    sign_bits = -1;
9016 #endif
9017 		  else
9018 		    sign_bits = 0;
9019 
9020 		  /* If we don't know that we have a 64-bit type,
9021 		     do two separate stores.  */
9022 		  if (bfd_big_endian (input_bfd))
9023 		    {
9024 		      /* Store the sign-bits (which are most significant)
9025 			 first.  */
9026 		      low_bits = sign_bits;
9027 		      high_bits = addend;
9028 		    }
9029 		  else
9030 		    {
9031 		      low_bits = addend;
9032 		      high_bits = sign_bits;
9033 		    }
9034 		  bfd_put_32 (input_bfd, low_bits,
9035 			      contents + rel->r_offset);
9036 		  bfd_put_32 (input_bfd, high_bits,
9037 			      contents + rel->r_offset + 4);
9038 		  continue;
9039 		}
9040 
9041 	      if (! mips_elf_perform_relocation (info, howto, rel, addend,
9042 						 input_bfd, input_section,
9043 						 contents, FALSE))
9044 		return FALSE;
9045 	    }
9046 
9047 	  /* Go on to the next relocation.  */
9048 	  continue;
9049 	}
9050 
9051       /* In the N32 and 64-bit ABIs there may be multiple consecutive
9052 	 relocations for the same offset.  In that case we are
9053 	 supposed to treat the output of each relocation as the addend
9054 	 for the next.  */
9055       if (rel + 1 < relend
9056 	  && rel->r_offset == rel[1].r_offset
9057 	  && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9058 	use_saved_addend_p = TRUE;
9059       else
9060 	use_saved_addend_p = FALSE;
9061 
9062       /* Figure out what value we are supposed to relocate.  */
9063       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9064 					     input_section, info, rel,
9065 					     addend, howto, local_syms,
9066 					     local_sections, &value,
9067 					     &name, &require_jalx,
9068 					     use_saved_addend_p))
9069 	{
9070 	case bfd_reloc_continue:
9071 	  /* There's nothing to do.  */
9072 	  continue;
9073 
9074 	case bfd_reloc_undefined:
9075 	  /* mips_elf_calculate_relocation already called the
9076 	     undefined_symbol callback.  There's no real point in
9077 	     trying to perform the relocation at this point, so we
9078 	     just skip ahead to the next relocation.  */
9079 	  continue;
9080 
9081 	case bfd_reloc_notsupported:
9082 	  msg = _("internal error: unsupported relocation error");
9083 	  info->callbacks->warning
9084 	    (info, msg, name, input_bfd, input_section, rel->r_offset);
9085 	  return FALSE;
9086 
9087 	case bfd_reloc_overflow:
9088 	  if (use_saved_addend_p)
9089 	    /* Ignore overflow until we reach the last relocation for
9090 	       a given location.  */
9091 	    ;
9092 	  else
9093 	    {
9094 	      struct mips_elf_link_hash_table *htab;
9095 
9096 	      htab = mips_elf_hash_table (info);
9097 	      BFD_ASSERT (name != NULL);
9098 	      if (!htab->small_data_overflow_reported
9099 		  && (howto->type == R_MIPS_GPREL16
9100 		      || howto->type == R_MIPS_LITERAL))
9101 		{
9102 		  const char *msg =
9103 		    _("small-data section exceeds 64KB;"
9104 		      " lower small-data size limit (see option -G)");
9105 
9106 		  htab->small_data_overflow_reported = TRUE;
9107 		  (*info->callbacks->einfo) ("%P: %s\n", msg);
9108 		}
9109 	      if (! ((*info->callbacks->reloc_overflow)
9110 		     (info, NULL, name, howto->name, (bfd_vma) 0,
9111 		      input_bfd, input_section, rel->r_offset)))
9112 		return FALSE;
9113 	    }
9114 	  break;
9115 
9116 	case bfd_reloc_ok:
9117 	  break;
9118 
9119 	default:
9120 	  abort ();
9121 	  break;
9122 	}
9123 
9124       /* If we've got another relocation for the address, keep going
9125 	 until we reach the last one.  */
9126       if (use_saved_addend_p)
9127 	{
9128 	  addend = value;
9129 	  continue;
9130 	}
9131 
9132       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9133 	/* See the comment above about using R_MIPS_64 in the 32-bit
9134 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
9135 	   that calculated the right value.  Now, however, we
9136 	   sign-extend the 32-bit result to 64-bits, and store it as a
9137 	   64-bit value.  We are especially generous here in that we
9138 	   go to extreme lengths to support this usage on systems with
9139 	   only a 32-bit VMA.  */
9140 	{
9141 	  bfd_vma sign_bits;
9142 	  bfd_vma low_bits;
9143 	  bfd_vma high_bits;
9144 
9145 	  if (value & ((bfd_vma) 1 << 31))
9146 #ifdef BFD64
9147 	    sign_bits = ((bfd_vma) 1 << 32) - 1;
9148 #else
9149 	    sign_bits = -1;
9150 #endif
9151 	  else
9152 	    sign_bits = 0;
9153 
9154 	  /* If we don't know that we have a 64-bit type,
9155 	     do two separate stores.  */
9156 	  if (bfd_big_endian (input_bfd))
9157 	    {
9158 	      /* Undo what we did above.  */
9159 	      rel->r_offset -= 4;
9160 	      /* Store the sign-bits (which are most significant)
9161 		 first.  */
9162 	      low_bits = sign_bits;
9163 	      high_bits = value;
9164 	    }
9165 	  else
9166 	    {
9167 	      low_bits = value;
9168 	      high_bits = sign_bits;
9169 	    }
9170 	  bfd_put_32 (input_bfd, low_bits,
9171 		      contents + rel->r_offset);
9172 	  bfd_put_32 (input_bfd, high_bits,
9173 		      contents + rel->r_offset + 4);
9174 	  continue;
9175 	}
9176 
9177       /* Actually perform the relocation.  */
9178       if (! mips_elf_perform_relocation (info, howto, rel, value,
9179 					 input_bfd, input_section,
9180 					 contents, require_jalx))
9181 	return FALSE;
9182     }
9183 
9184   return TRUE;
9185 }
9186 
9187 /* A function that iterates over each entry in la25_stubs and fills
9188    in the code for each one.  DATA points to a mips_htab_traverse_info.  */
9189 
9190 static int
9191 mips_elf_create_la25_stub (void **slot, void *data)
9192 {
9193   struct mips_htab_traverse_info *hti;
9194   struct mips_elf_link_hash_table *htab;
9195   struct mips_elf_la25_stub *stub;
9196   asection *s;
9197   bfd_byte *loc;
9198   bfd_vma offset, target, target_high, target_low;
9199 
9200   stub = (struct mips_elf_la25_stub *) *slot;
9201   hti = (struct mips_htab_traverse_info *) data;
9202   htab = mips_elf_hash_table (hti->info);
9203 
9204   /* Create the section contents, if we haven't already.  */
9205   s = stub->stub_section;
9206   loc = s->contents;
9207   if (loc == NULL)
9208     {
9209       loc = bfd_malloc (s->size);
9210       if (loc == NULL)
9211 	{
9212 	  hti->error = TRUE;
9213 	  return FALSE;
9214 	}
9215       s->contents = loc;
9216     }
9217 
9218   /* Work out where in the section this stub should go.  */
9219   offset = stub->offset;
9220 
9221   /* Work out the target address.  */
9222   target = (stub->h->root.root.u.def.section->output_section->vma
9223 	    + stub->h->root.root.u.def.section->output_offset
9224 	    + stub->h->root.root.u.def.value);
9225   target_high = ((target + 0x8000) >> 16) & 0xffff;
9226   target_low = (target & 0xffff);
9227 
9228   if (stub->stub_section != htab->strampoline)
9229     {
9230       /* This is a simple LUI/ADIDU stub.  Zero out the beginning
9231 	 of the section and write the two instructions at the end.  */
9232       memset (loc, 0, offset);
9233       loc += offset;
9234       bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9235       bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9236     }
9237   else
9238     {
9239       /* This is trampoline.  */
9240       loc += offset;
9241       bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9242       bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9243       bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9244       bfd_put_32 (hti->output_bfd, 0, loc + 12);
9245     }
9246   return TRUE;
9247 }
9248 
9249 /* If NAME is one of the special IRIX6 symbols defined by the linker,
9250    adjust it appropriately now.  */
9251 
9252 static void
9253 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9254 				      const char *name, Elf_Internal_Sym *sym)
9255 {
9256   /* The linker script takes care of providing names and values for
9257      these, but we must place them into the right sections.  */
9258   static const char* const text_section_symbols[] = {
9259     "_ftext",
9260     "_etext",
9261     "__dso_displacement",
9262     "__elf_header",
9263     "__program_header_table",
9264     NULL
9265   };
9266 
9267   static const char* const data_section_symbols[] = {
9268     "_fdata",
9269     "_edata",
9270     "_end",
9271     "_fbss",
9272     NULL
9273   };
9274 
9275   const char* const *p;
9276   int i;
9277 
9278   for (i = 0; i < 2; ++i)
9279     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9280 	 *p;
9281 	 ++p)
9282       if (strcmp (*p, name) == 0)
9283 	{
9284 	  /* All of these symbols are given type STT_SECTION by the
9285 	     IRIX6 linker.  */
9286 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9287 	  sym->st_other = STO_PROTECTED;
9288 
9289 	  /* The IRIX linker puts these symbols in special sections.  */
9290 	  if (i == 0)
9291 	    sym->st_shndx = SHN_MIPS_TEXT;
9292 	  else
9293 	    sym->st_shndx = SHN_MIPS_DATA;
9294 
9295 	  break;
9296 	}
9297 }
9298 
9299 /* Finish up dynamic symbol handling.  We set the contents of various
9300    dynamic sections here.  */
9301 
9302 bfd_boolean
9303 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9304 				     struct bfd_link_info *info,
9305 				     struct elf_link_hash_entry *h,
9306 				     Elf_Internal_Sym *sym)
9307 {
9308   bfd *dynobj;
9309   asection *sgot;
9310   struct mips_got_info *g, *gg;
9311   const char *name;
9312   int idx;
9313   struct mips_elf_link_hash_table *htab;
9314   struct mips_elf_link_hash_entry *hmips;
9315 
9316   htab = mips_elf_hash_table (info);
9317   dynobj = elf_hash_table (info)->dynobj;
9318   hmips = (struct mips_elf_link_hash_entry *) h;
9319 
9320   BFD_ASSERT (!htab->is_vxworks);
9321 
9322   if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9323     {
9324       /* We've decided to create a PLT entry for this symbol.  */
9325       bfd_byte *loc;
9326       bfd_vma header_address, plt_index, got_address;
9327       bfd_vma got_address_high, got_address_low, load;
9328       const bfd_vma *plt_entry;
9329 
9330       BFD_ASSERT (htab->use_plts_and_copy_relocs);
9331       BFD_ASSERT (h->dynindx != -1);
9332       BFD_ASSERT (htab->splt != NULL);
9333       BFD_ASSERT (h->plt.offset <= htab->splt->size);
9334       BFD_ASSERT (!h->def_regular);
9335 
9336       /* Calculate the address of the PLT header.  */
9337       header_address = (htab->splt->output_section->vma
9338 			+ htab->splt->output_offset);
9339 
9340       /* Calculate the index of the entry.  */
9341       plt_index = ((h->plt.offset - htab->plt_header_size)
9342 		   / htab->plt_entry_size);
9343 
9344       /* Calculate the address of the .got.plt entry.  */
9345       got_address = (htab->sgotplt->output_section->vma
9346 		     + htab->sgotplt->output_offset
9347 		     + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9348       got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9349       got_address_low = got_address & 0xffff;
9350 
9351       /* Initially point the .got.plt entry at the PLT header.  */
9352       loc = (htab->sgotplt->contents
9353 	     + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9354       if (ABI_64_P (output_bfd))
9355 	bfd_put_64 (output_bfd, header_address, loc);
9356       else
9357 	bfd_put_32 (output_bfd, header_address, loc);
9358 
9359       /* Find out where the .plt entry should go.  */
9360       loc = htab->splt->contents + h->plt.offset;
9361 
9362       /* Pick the load opcode.  */
9363       load = MIPS_ELF_LOAD_WORD (output_bfd);
9364 
9365       /* Fill in the PLT entry itself.  */
9366       plt_entry = mips_exec_plt_entry;
9367       bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9368       bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9369       bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9370       bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9371 
9372       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
9373       mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9374 					  plt_index, h->dynindx,
9375 					  R_MIPS_JUMP_SLOT, got_address);
9376 
9377       /* We distinguish between PLT entries and lazy-binding stubs by
9378 	 giving the former an st_other value of STO_MIPS_PLT.  Set the
9379 	 flag and leave the value if there are any relocations in the
9380 	 binary where pointer equality matters.  */
9381       sym->st_shndx = SHN_UNDEF;
9382       if (h->pointer_equality_needed)
9383 	sym->st_other = STO_MIPS_PLT;
9384       else
9385 	sym->st_value = 0;
9386     }
9387   else if (h->plt.offset != MINUS_ONE)
9388     {
9389       /* We've decided to create a lazy-binding stub.  */
9390       bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9391 
9392       /* This symbol has a stub.  Set it up.  */
9393 
9394       BFD_ASSERT (h->dynindx != -1);
9395 
9396       BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9397                   || (h->dynindx <= 0xffff));
9398 
9399       /* Values up to 2^31 - 1 are allowed.  Larger values would cause
9400 	 sign extension at runtime in the stub, resulting in a negative
9401 	 index value.  */
9402       if (h->dynindx & ~0x7fffffff)
9403 	return FALSE;
9404 
9405       /* Fill the stub.  */
9406       idx = 0;
9407       bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9408       idx += 4;
9409       bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9410       idx += 4;
9411       if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9412         {
9413           bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9414                       stub + idx);
9415           idx += 4;
9416         }
9417       bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9418       idx += 4;
9419 
9420       /* If a large stub is not required and sign extension is not a
9421          problem, then use legacy code in the stub.  */
9422       if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9423 	bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9424       else if (h->dynindx & ~0x7fff)
9425         bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9426       else
9427         bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9428 		    stub + idx);
9429 
9430       BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9431       memcpy (htab->sstubs->contents + h->plt.offset,
9432 	      stub, htab->function_stub_size);
9433 
9434       /* Mark the symbol as undefined.  plt.offset != -1 occurs
9435 	 only for the referenced symbol.  */
9436       sym->st_shndx = SHN_UNDEF;
9437 
9438       /* The run-time linker uses the st_value field of the symbol
9439 	 to reset the global offset table entry for this external
9440 	 to its stub address when unlinking a shared object.  */
9441       sym->st_value = (htab->sstubs->output_section->vma
9442 		       + htab->sstubs->output_offset
9443 		       + h->plt.offset);
9444     }
9445 
9446   /* If we have a MIPS16 function with a stub, the dynamic symbol must
9447      refer to the stub, since only the stub uses the standard calling
9448      conventions.  */
9449   if (h->dynindx != -1 && hmips->fn_stub != NULL)
9450     {
9451       BFD_ASSERT (hmips->need_fn_stub);
9452       sym->st_value = (hmips->fn_stub->output_section->vma
9453 		       + hmips->fn_stub->output_offset);
9454       sym->st_size = hmips->fn_stub->size;
9455       sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9456     }
9457 
9458   BFD_ASSERT (h->dynindx != -1
9459 	      || h->forced_local);
9460 
9461   sgot = htab->sgot;
9462   g = htab->got_info;
9463   BFD_ASSERT (g != NULL);
9464 
9465   /* Run through the global symbol table, creating GOT entries for all
9466      the symbols that need them.  */
9467   if (g->global_gotsym != NULL
9468       && h->dynindx >= g->global_gotsym->dynindx)
9469     {
9470       bfd_vma offset;
9471       bfd_vma value;
9472 
9473       value = sym->st_value;
9474       offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9475 					  R_MIPS_GOT16, info);
9476       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9477     }
9478 
9479   if (g->next && h->dynindx != -1 && h->type != STT_TLS)
9480     {
9481       struct mips_got_entry e, *p;
9482       bfd_vma entry;
9483       bfd_vma offset;
9484 
9485       gg = g;
9486 
9487       e.abfd = output_bfd;
9488       e.symndx = -1;
9489       e.d.h = hmips;
9490       e.tls_type = 0;
9491 
9492       for (g = g->next; g->next != gg; g = g->next)
9493 	{
9494 	  if (g->got_entries
9495 	      && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9496 							   &e)))
9497 	    {
9498 	      offset = p->gotidx;
9499 	      if (info->shared
9500 		  || (elf_hash_table (info)->dynamic_sections_created
9501 		      && p->d.h != NULL
9502 		      && p->d.h->root.def_dynamic
9503 		      && !p->d.h->root.def_regular))
9504 		{
9505 		  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
9506 		     the various compatibility problems, it's easier to mock
9507 		     up an R_MIPS_32 or R_MIPS_64 relocation and leave
9508 		     mips_elf_create_dynamic_relocation to calculate the
9509 		     appropriate addend.  */
9510 		  Elf_Internal_Rela rel[3];
9511 
9512 		  memset (rel, 0, sizeof (rel));
9513 		  if (ABI_64_P (output_bfd))
9514 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9515 		  else
9516 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9517 		  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9518 
9519 		  entry = 0;
9520 		  if (! (mips_elf_create_dynamic_relocation
9521 			 (output_bfd, info, rel,
9522 			  e.d.h, NULL, sym->st_value, &entry, sgot)))
9523 		    return FALSE;
9524 		}
9525 	      else
9526 		entry = sym->st_value;
9527 	      MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
9528 	    }
9529 	}
9530     }
9531 
9532   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
9533   name = h->root.root.string;
9534   if (strcmp (name, "_DYNAMIC") == 0
9535       || h == elf_hash_table (info)->hgot)
9536     sym->st_shndx = SHN_ABS;
9537   else if (strcmp (name, "_DYNAMIC_LINK") == 0
9538 	   || strcmp (name, "_DYNAMIC_LINKING") == 0)
9539     {
9540       sym->st_shndx = SHN_ABS;
9541       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9542       sym->st_value = 1;
9543     }
9544   else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
9545     {
9546       sym->st_shndx = SHN_ABS;
9547       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9548       sym->st_value = elf_gp (output_bfd);
9549     }
9550   else if (SGI_COMPAT (output_bfd))
9551     {
9552       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9553 	  || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9554 	{
9555 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9556 	  sym->st_other = STO_PROTECTED;
9557 	  sym->st_value = 0;
9558 	  sym->st_shndx = SHN_MIPS_DATA;
9559 	}
9560       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9561 	{
9562 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9563 	  sym->st_other = STO_PROTECTED;
9564 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
9565 	  sym->st_shndx = SHN_ABS;
9566 	}
9567       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9568 	{
9569 	  if (h->type == STT_FUNC)
9570 	    sym->st_shndx = SHN_MIPS_TEXT;
9571 	  else if (h->type == STT_OBJECT)
9572 	    sym->st_shndx = SHN_MIPS_DATA;
9573 	}
9574     }
9575 
9576   /* Emit a copy reloc, if needed.  */
9577   if (h->needs_copy)
9578     {
9579       asection *s;
9580       bfd_vma symval;
9581 
9582       BFD_ASSERT (h->dynindx != -1);
9583       BFD_ASSERT (htab->use_plts_and_copy_relocs);
9584 
9585       s = mips_elf_rel_dyn_section (info, FALSE);
9586       symval = (h->root.u.def.section->output_section->vma
9587 		+ h->root.u.def.section->output_offset
9588 		+ h->root.u.def.value);
9589       mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9590 					  h->dynindx, R_MIPS_COPY, symval);
9591     }
9592 
9593   /* Handle the IRIX6-specific symbols.  */
9594   if (IRIX_COMPAT (output_bfd) == ict_irix6)
9595     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9596 
9597   if (! info->shared)
9598     {
9599       if (! mips_elf_hash_table (info)->use_rld_obj_head
9600 	  && (strcmp (name, "__rld_map") == 0
9601 	      || strcmp (name, "__RLD_MAP") == 0))
9602 	{
9603 	  asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9604 	  BFD_ASSERT (s != NULL);
9605 	  sym->st_value = s->output_section->vma + s->output_offset;
9606 	  bfd_put_32 (output_bfd, 0, s->contents);
9607 	  if (mips_elf_hash_table (info)->rld_value == 0)
9608 	    mips_elf_hash_table (info)->rld_value = sym->st_value;
9609 	}
9610       else if (mips_elf_hash_table (info)->use_rld_obj_head
9611 	       && strcmp (name, "__rld_obj_head") == 0)
9612 	{
9613 	  /* IRIX6 does not use a .rld_map section.  */
9614 	  if (IRIX_COMPAT (output_bfd) == ict_irix5
9615               || IRIX_COMPAT (output_bfd) == ict_none)
9616 	    BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9617 			!= NULL);
9618 	  mips_elf_hash_table (info)->rld_value = sym->st_value;
9619 	}
9620     }
9621 
9622   /* Keep dynamic MIPS16 symbols odd.  This allows the dynamic linker to
9623      treat MIPS16 symbols like any other.  */
9624   if (ELF_ST_IS_MIPS16 (sym->st_other))
9625     {
9626       BFD_ASSERT (sym->st_value & 1);
9627       sym->st_other -= STO_MIPS16;
9628     }
9629 
9630   return TRUE;
9631 }
9632 
9633 /* Likewise, for VxWorks.  */
9634 
9635 bfd_boolean
9636 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9637 					 struct bfd_link_info *info,
9638 					 struct elf_link_hash_entry *h,
9639 					 Elf_Internal_Sym *sym)
9640 {
9641   bfd *dynobj;
9642   asection *sgot;
9643   struct mips_got_info *g;
9644   struct mips_elf_link_hash_table *htab;
9645 
9646   htab = mips_elf_hash_table (info);
9647   dynobj = elf_hash_table (info)->dynobj;
9648 
9649   if (h->plt.offset != (bfd_vma) -1)
9650     {
9651       bfd_byte *loc;
9652       bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9653       Elf_Internal_Rela rel;
9654       static const bfd_vma *plt_entry;
9655 
9656       BFD_ASSERT (h->dynindx != -1);
9657       BFD_ASSERT (htab->splt != NULL);
9658       BFD_ASSERT (h->plt.offset <= htab->splt->size);
9659 
9660       /* Calculate the address of the .plt entry.  */
9661       plt_address = (htab->splt->output_section->vma
9662 		     + htab->splt->output_offset
9663 		     + h->plt.offset);
9664 
9665       /* Calculate the index of the entry.  */
9666       plt_index = ((h->plt.offset - htab->plt_header_size)
9667 		   / htab->plt_entry_size);
9668 
9669       /* Calculate the address of the .got.plt entry.  */
9670       got_address = (htab->sgotplt->output_section->vma
9671 		     + htab->sgotplt->output_offset
9672 		     + plt_index * 4);
9673 
9674       /* Calculate the offset of the .got.plt entry from
9675 	 _GLOBAL_OFFSET_TABLE_.  */
9676       got_offset = mips_elf_gotplt_index (info, h);
9677 
9678       /* Calculate the offset for the branch at the start of the PLT
9679 	 entry.  The branch jumps to the beginning of .plt.  */
9680       branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9681 
9682       /* Fill in the initial value of the .got.plt entry.  */
9683       bfd_put_32 (output_bfd, plt_address,
9684 		  htab->sgotplt->contents + plt_index * 4);
9685 
9686       /* Find out where the .plt entry should go.  */
9687       loc = htab->splt->contents + h->plt.offset;
9688 
9689       if (info->shared)
9690 	{
9691 	  plt_entry = mips_vxworks_shared_plt_entry;
9692 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9693 	  bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9694 	}
9695       else
9696 	{
9697 	  bfd_vma got_address_high, got_address_low;
9698 
9699 	  plt_entry = mips_vxworks_exec_plt_entry;
9700 	  got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9701 	  got_address_low = got_address & 0xffff;
9702 
9703 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9704 	  bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9705 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9706 	  bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9707 	  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9708 	  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9709 	  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9710 	  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9711 
9712 	  loc = (htab->srelplt2->contents
9713 		 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9714 
9715 	  /* Emit a relocation for the .got.plt entry.  */
9716 	  rel.r_offset = got_address;
9717 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9718 	  rel.r_addend = h->plt.offset;
9719 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9720 
9721 	  /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
9722 	  loc += sizeof (Elf32_External_Rela);
9723 	  rel.r_offset = plt_address + 8;
9724 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9725 	  rel.r_addend = got_offset;
9726 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9727 
9728 	  /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
9729 	  loc += sizeof (Elf32_External_Rela);
9730 	  rel.r_offset += 4;
9731 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9732 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9733 	}
9734 
9735       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
9736       loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9737       rel.r_offset = got_address;
9738       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9739       rel.r_addend = 0;
9740       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9741 
9742       if (!h->def_regular)
9743 	sym->st_shndx = SHN_UNDEF;
9744     }
9745 
9746   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9747 
9748   sgot = htab->sgot;
9749   g = htab->got_info;
9750   BFD_ASSERT (g != NULL);
9751 
9752   /* See if this symbol has an entry in the GOT.  */
9753   if (g->global_gotsym != NULL
9754       && h->dynindx >= g->global_gotsym->dynindx)
9755     {
9756       bfd_vma offset;
9757       Elf_Internal_Rela outrel;
9758       bfd_byte *loc;
9759       asection *s;
9760 
9761       /* Install the symbol value in the GOT.   */
9762       offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9763 					  R_MIPS_GOT16, info);
9764       MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9765 
9766       /* Add a dynamic relocation for it.  */
9767       s = mips_elf_rel_dyn_section (info, FALSE);
9768       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9769       outrel.r_offset = (sgot->output_section->vma
9770 			 + sgot->output_offset
9771 			 + offset);
9772       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9773       outrel.r_addend = 0;
9774       bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9775     }
9776 
9777   /* Emit a copy reloc, if needed.  */
9778   if (h->needs_copy)
9779     {
9780       Elf_Internal_Rela rel;
9781 
9782       BFD_ASSERT (h->dynindx != -1);
9783 
9784       rel.r_offset = (h->root.u.def.section->output_section->vma
9785 		      + h->root.u.def.section->output_offset
9786 		      + h->root.u.def.value);
9787       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9788       rel.r_addend = 0;
9789       bfd_elf32_swap_reloca_out (output_bfd, &rel,
9790 				 htab->srelbss->contents
9791 				 + (htab->srelbss->reloc_count
9792 				    * sizeof (Elf32_External_Rela)));
9793       ++htab->srelbss->reloc_count;
9794     }
9795 
9796   /* If this is a mips16 symbol, force the value to be even.  */
9797   if (ELF_ST_IS_MIPS16 (sym->st_other))
9798     sym->st_value &= ~1;
9799 
9800   return TRUE;
9801 }
9802 
9803 /* Write out a plt0 entry to the beginning of .plt.  */
9804 
9805 static void
9806 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9807 {
9808   bfd_byte *loc;
9809   bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9810   static const bfd_vma *plt_entry;
9811   struct mips_elf_link_hash_table *htab;
9812 
9813   htab = mips_elf_hash_table (info);
9814   if (ABI_64_P (output_bfd))
9815     plt_entry = mips_n64_exec_plt0_entry;
9816   else if (ABI_N32_P (output_bfd))
9817     plt_entry = mips_n32_exec_plt0_entry;
9818   else
9819     plt_entry = mips_o32_exec_plt0_entry;
9820 
9821   /* Calculate the value of .got.plt.  */
9822   gotplt_value = (htab->sgotplt->output_section->vma
9823 		  + htab->sgotplt->output_offset);
9824   gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9825   gotplt_value_low = gotplt_value & 0xffff;
9826 
9827   /* The PLT sequence is not safe for N64 if .got.plt's address can
9828      not be loaded in two instructions.  */
9829   BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9830 	      || ~(gotplt_value | 0x7fffffff) == 0);
9831 
9832   /* Install the PLT header.  */
9833   loc = htab->splt->contents;
9834   bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9835   bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9836   bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9837   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9838   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9839   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9840   bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9841   bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9842 }
9843 
9844 /* Install the PLT header for a VxWorks executable and finalize the
9845    contents of .rela.plt.unloaded.  */
9846 
9847 static void
9848 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9849 {
9850   Elf_Internal_Rela rela;
9851   bfd_byte *loc;
9852   bfd_vma got_value, got_value_high, got_value_low, plt_address;
9853   static const bfd_vma *plt_entry;
9854   struct mips_elf_link_hash_table *htab;
9855 
9856   htab = mips_elf_hash_table (info);
9857   plt_entry = mips_vxworks_exec_plt0_entry;
9858 
9859   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
9860   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9861 	       + htab->root.hgot->root.u.def.section->output_offset
9862 	       + htab->root.hgot->root.u.def.value);
9863 
9864   got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9865   got_value_low = got_value & 0xffff;
9866 
9867   /* Calculate the address of the PLT header.  */
9868   plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9869 
9870   /* Install the PLT header.  */
9871   loc = htab->splt->contents;
9872   bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9873   bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9874   bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9875   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9876   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9877   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9878 
9879   /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
9880   loc = htab->srelplt2->contents;
9881   rela.r_offset = plt_address;
9882   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9883   rela.r_addend = 0;
9884   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9885   loc += sizeof (Elf32_External_Rela);
9886 
9887   /* Output the relocation for the following addiu of
9888      %lo(_GLOBAL_OFFSET_TABLE_).  */
9889   rela.r_offset += 4;
9890   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9891   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9892   loc += sizeof (Elf32_External_Rela);
9893 
9894   /* Fix up the remaining relocations.  They may have the wrong
9895      symbol index for _G_O_T_ or _P_L_T_ depending on the order
9896      in which symbols were output.  */
9897   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9898     {
9899       Elf_Internal_Rela rel;
9900 
9901       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9902       rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9903       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9904       loc += sizeof (Elf32_External_Rela);
9905 
9906       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9907       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9908       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9909       loc += sizeof (Elf32_External_Rela);
9910 
9911       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9912       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9913       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9914       loc += sizeof (Elf32_External_Rela);
9915     }
9916 }
9917 
9918 /* Install the PLT header for a VxWorks shared library.  */
9919 
9920 static void
9921 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9922 {
9923   unsigned int i;
9924   struct mips_elf_link_hash_table *htab;
9925 
9926   htab = mips_elf_hash_table (info);
9927 
9928   /* We just need to copy the entry byte-by-byte.  */
9929   for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9930     bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9931 		htab->splt->contents + i * 4);
9932 }
9933 
9934 /* Finish up the dynamic sections.  */
9935 
9936 bfd_boolean
9937 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9938 				       struct bfd_link_info *info)
9939 {
9940   bfd *dynobj;
9941   asection *sdyn;
9942   asection *sgot;
9943   struct mips_got_info *gg, *g;
9944   struct mips_elf_link_hash_table *htab;
9945 
9946   htab = mips_elf_hash_table (info);
9947   dynobj = elf_hash_table (info)->dynobj;
9948 
9949   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9950 
9951   sgot = htab->sgot;
9952   gg = htab->got_info;
9953 
9954   if (elf_hash_table (info)->dynamic_sections_created)
9955     {
9956       bfd_byte *b;
9957       int dyn_to_skip = 0, dyn_skipped = 0;
9958 
9959       BFD_ASSERT (sdyn != NULL);
9960       BFD_ASSERT (gg != NULL);
9961 
9962       g = mips_elf_got_for_ibfd (gg, output_bfd);
9963       BFD_ASSERT (g != NULL);
9964 
9965       for (b = sdyn->contents;
9966 	   b < sdyn->contents + sdyn->size;
9967 	   b += MIPS_ELF_DYN_SIZE (dynobj))
9968 	{
9969 	  Elf_Internal_Dyn dyn;
9970 	  const char *name;
9971 	  size_t elemsize;
9972 	  asection *s;
9973 	  bfd_boolean swap_out_p;
9974 
9975 	  /* Read in the current dynamic entry.  */
9976 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9977 
9978 	  /* Assume that we're going to modify it and write it out.  */
9979 	  swap_out_p = TRUE;
9980 
9981 	  switch (dyn.d_tag)
9982 	    {
9983 	    case DT_RELENT:
9984 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
9985 	      break;
9986 
9987 	    case DT_RELAENT:
9988 	      BFD_ASSERT (htab->is_vxworks);
9989 	      dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
9990 	      break;
9991 
9992 	    case DT_STRSZ:
9993 	      /* Rewrite DT_STRSZ.  */
9994 	      dyn.d_un.d_val =
9995 		_bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
9996 	      break;
9997 
9998 	    case DT_PLTGOT:
9999 	      s = htab->sgot;
10000 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10001 	      break;
10002 
10003 	    case DT_MIPS_PLTGOT:
10004 	      s = htab->sgotplt;
10005 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10006 	      break;
10007 
10008 	    case DT_MIPS_RLD_VERSION:
10009 	      dyn.d_un.d_val = 1; /* XXX */
10010 	      break;
10011 
10012 	    case DT_MIPS_FLAGS:
10013 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10014 	      break;
10015 
10016 	    case DT_MIPS_TIME_STAMP:
10017 	      {
10018 		time_t t;
10019 		time (&t);
10020 		dyn.d_un.d_val = t;
10021 	      }
10022 	      break;
10023 
10024 	    case DT_MIPS_ICHECKSUM:
10025 	      /* XXX FIXME: */
10026 	      swap_out_p = FALSE;
10027 	      break;
10028 
10029 	    case DT_MIPS_IVERSION:
10030 	      /* XXX FIXME: */
10031 	      swap_out_p = FALSE;
10032 	      break;
10033 
10034 	    case DT_MIPS_BASE_ADDRESS:
10035 	      s = output_bfd->sections;
10036 	      BFD_ASSERT (s != NULL);
10037 	      dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10038 	      break;
10039 
10040 	    case DT_MIPS_LOCAL_GOTNO:
10041 	      dyn.d_un.d_val = g->local_gotno;
10042 	      break;
10043 
10044 	    case DT_MIPS_UNREFEXTNO:
10045 	      /* The index into the dynamic symbol table which is the
10046 		 entry of the first external symbol that is not
10047 		 referenced within the same object.  */
10048 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10049 	      break;
10050 
10051 	    case DT_MIPS_GOTSYM:
10052 	      if (gg->global_gotsym)
10053 		{
10054 		  dyn.d_un.d_val = gg->global_gotsym->dynindx;
10055 		  break;
10056 		}
10057 	      /* In case if we don't have global got symbols we default
10058 		 to setting DT_MIPS_GOTSYM to the same value as
10059 		 DT_MIPS_SYMTABNO, so we just fall through.  */
10060 
10061 	    case DT_MIPS_SYMTABNO:
10062 	      name = ".dynsym";
10063 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10064 	      s = bfd_get_section_by_name (output_bfd, name);
10065 	      BFD_ASSERT (s != NULL);
10066 
10067 	      dyn.d_un.d_val = s->size / elemsize;
10068 	      break;
10069 
10070 	    case DT_MIPS_HIPAGENO:
10071 	      dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10072 	      break;
10073 
10074 	    case DT_MIPS_RLD_MAP:
10075 	      dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10076 	      break;
10077 
10078 	    case DT_MIPS_OPTIONS:
10079 	      s = (bfd_get_section_by_name
10080 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10081 	      dyn.d_un.d_ptr = s->vma;
10082 	      break;
10083 
10084 	    case DT_RELASZ:
10085 	      BFD_ASSERT (htab->is_vxworks);
10086 	      /* The count does not include the JUMP_SLOT relocations.  */
10087 	      if (htab->srelplt)
10088 		dyn.d_un.d_val -= htab->srelplt->size;
10089 	      break;
10090 
10091 	    case DT_PLTREL:
10092 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
10093 	      if (htab->is_vxworks)
10094 		dyn.d_un.d_val = DT_RELA;
10095 	      else
10096 		dyn.d_un.d_val = DT_REL;
10097 	      break;
10098 
10099 	    case DT_PLTRELSZ:
10100 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
10101 	      dyn.d_un.d_val = htab->srelplt->size;
10102 	      break;
10103 
10104 	    case DT_JMPREL:
10105 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
10106 	      dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10107 				+ htab->srelplt->output_offset);
10108 	      break;
10109 
10110 	    case DT_TEXTREL:
10111 	      /* If we didn't need any text relocations after all, delete
10112 		 the dynamic tag.  */
10113 	      if (!(info->flags & DF_TEXTREL))
10114 		{
10115 		  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10116 		  swap_out_p = FALSE;
10117 		}
10118 	      break;
10119 
10120 	    case DT_FLAGS:
10121 	      /* If we didn't need any text relocations after all, clear
10122 		 DF_TEXTREL from DT_FLAGS.  */
10123 	      if (!(info->flags & DF_TEXTREL))
10124 		dyn.d_un.d_val &= ~DF_TEXTREL;
10125 	      else
10126 		swap_out_p = FALSE;
10127 	      break;
10128 
10129 	    default:
10130 	      swap_out_p = FALSE;
10131 	      if (htab->is_vxworks
10132 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10133 		swap_out_p = TRUE;
10134 	      break;
10135 	    }
10136 
10137 	  if (swap_out_p || dyn_skipped)
10138 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10139 	      (dynobj, &dyn, b - dyn_skipped);
10140 
10141 	  if (dyn_to_skip)
10142 	    {
10143 	      dyn_skipped += dyn_to_skip;
10144 	      dyn_to_skip = 0;
10145 	    }
10146 	}
10147 
10148       /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
10149       if (dyn_skipped > 0)
10150 	memset (b - dyn_skipped, 0, dyn_skipped);
10151     }
10152 
10153   if (sgot != NULL && sgot->size > 0
10154       && !bfd_is_abs_section (sgot->output_section))
10155     {
10156       if (htab->is_vxworks)
10157 	{
10158 	  /* The first entry of the global offset table points to the
10159 	     ".dynamic" section.  The second is initialized by the
10160 	     loader and contains the shared library identifier.
10161 	     The third is also initialized by the loader and points
10162 	     to the lazy resolution stub.  */
10163 	  MIPS_ELF_PUT_WORD (output_bfd,
10164 			     sdyn->output_offset + sdyn->output_section->vma,
10165 			     sgot->contents);
10166 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
10167 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10168 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
10169 			     sgot->contents
10170 			     + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10171 	}
10172       else
10173 	{
10174 	  /* The first entry of the global offset table will be filled at
10175 	     runtime. The second entry will be used by some runtime loaders.
10176 	     This isn't the case of IRIX rld.  */
10177 	  MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10178 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10179 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10180 	}
10181 
10182       elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10183 	 = MIPS_ELF_GOT_SIZE (output_bfd);
10184     }
10185 
10186   /* Generate dynamic relocations for the non-primary gots.  */
10187   if (gg != NULL && gg->next)
10188     {
10189       Elf_Internal_Rela rel[3];
10190       bfd_vma addend = 0;
10191 
10192       memset (rel, 0, sizeof (rel));
10193       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10194 
10195       for (g = gg->next; g->next != gg; g = g->next)
10196 	{
10197 	  bfd_vma index = g->next->local_gotno + g->next->global_gotno
10198 	    + g->next->tls_gotno;
10199 
10200 	  MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10201 			     + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10202 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10203 			     sgot->contents
10204 			     + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10205 
10206 	  if (! info->shared)
10207 	    continue;
10208 
10209 	  while (index < g->assigned_gotno)
10210 	    {
10211 	      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10212 		= index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10213 	      if (!(mips_elf_create_dynamic_relocation
10214 		    (output_bfd, info, rel, NULL,
10215 		     bfd_abs_section_ptr,
10216 		     0, &addend, sgot)))
10217 		return FALSE;
10218 	      BFD_ASSERT (addend == 0);
10219 	    }
10220 	}
10221     }
10222 
10223   /* The generation of dynamic relocations for the non-primary gots
10224      adds more dynamic relocations.  We cannot count them until
10225      here.  */
10226 
10227   if (elf_hash_table (info)->dynamic_sections_created)
10228     {
10229       bfd_byte *b;
10230       bfd_boolean swap_out_p;
10231 
10232       BFD_ASSERT (sdyn != NULL);
10233 
10234       for (b = sdyn->contents;
10235 	   b < sdyn->contents + sdyn->size;
10236 	   b += MIPS_ELF_DYN_SIZE (dynobj))
10237 	{
10238 	  Elf_Internal_Dyn dyn;
10239 	  asection *s;
10240 
10241 	  /* Read in the current dynamic entry.  */
10242 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10243 
10244 	  /* Assume that we're going to modify it and write it out.  */
10245 	  swap_out_p = TRUE;
10246 
10247 	  switch (dyn.d_tag)
10248 	    {
10249 	    case DT_RELSZ:
10250 	      /* Reduce DT_RELSZ to account for any relocations we
10251 		 decided not to make.  This is for the n64 irix rld,
10252 		 which doesn't seem to apply any relocations if there
10253 		 are trailing null entries.  */
10254 	      s = mips_elf_rel_dyn_section (info, FALSE);
10255 	      dyn.d_un.d_val = (s->reloc_count
10256 				* (ABI_64_P (output_bfd)
10257 				   ? sizeof (Elf64_Mips_External_Rel)
10258 				   : sizeof (Elf32_External_Rel)));
10259 	      /* Adjust the section size too.  Tools like the prelinker
10260 		 can reasonably expect the values to the same.  */
10261 	      elf_section_data (s->output_section)->this_hdr.sh_size
10262 		= dyn.d_un.d_val;
10263 	      break;
10264 
10265 	    default:
10266 	      swap_out_p = FALSE;
10267 	      break;
10268 	    }
10269 
10270 	  if (swap_out_p)
10271 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10272 	      (dynobj, &dyn, b);
10273 	}
10274     }
10275 
10276   {
10277     asection *s;
10278     Elf32_compact_rel cpt;
10279 
10280     if (SGI_COMPAT (output_bfd))
10281       {
10282 	/* Write .compact_rel section out.  */
10283 	s = bfd_get_section_by_name (dynobj, ".compact_rel");
10284 	if (s != NULL)
10285 	  {
10286 	    cpt.id1 = 1;
10287 	    cpt.num = s->reloc_count;
10288 	    cpt.id2 = 2;
10289 	    cpt.offset = (s->output_section->filepos
10290 			  + sizeof (Elf32_External_compact_rel));
10291 	    cpt.reserved0 = 0;
10292 	    cpt.reserved1 = 0;
10293 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10294 					    ((Elf32_External_compact_rel *)
10295 					     s->contents));
10296 
10297 	    /* Clean up a dummy stub function entry in .text.  */
10298 	    if (htab->sstubs != NULL)
10299 	      {
10300 		file_ptr dummy_offset;
10301 
10302 		BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10303 		dummy_offset = htab->sstubs->size - htab->function_stub_size;
10304 		memset (htab->sstubs->contents + dummy_offset, 0,
10305 			htab->function_stub_size);
10306 	      }
10307 	  }
10308       }
10309 
10310     /* The psABI says that the dynamic relocations must be sorted in
10311        increasing order of r_symndx.  The VxWorks EABI doesn't require
10312        this, and because the code below handles REL rather than RELA
10313        relocations, using it for VxWorks would be outright harmful.  */
10314     if (!htab->is_vxworks)
10315       {
10316 	s = mips_elf_rel_dyn_section (info, FALSE);
10317 	if (s != NULL
10318 	    && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10319 	  {
10320 	    reldyn_sorting_bfd = output_bfd;
10321 
10322 	    if (ABI_64_P (output_bfd))
10323 	      qsort ((Elf64_External_Rel *) s->contents + 1,
10324 		     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10325 		     sort_dynamic_relocs_64);
10326 	    else
10327 	      qsort ((Elf32_External_Rel *) s->contents + 1,
10328 		     s->reloc_count - 1, sizeof (Elf32_External_Rel),
10329 		     sort_dynamic_relocs);
10330 	  }
10331       }
10332   }
10333 
10334   if (htab->splt && htab->splt->size > 0)
10335     {
10336       if (htab->is_vxworks)
10337 	{
10338 	  if (info->shared)
10339 	    mips_vxworks_finish_shared_plt (output_bfd, info);
10340 	  else
10341 	    mips_vxworks_finish_exec_plt (output_bfd, info);
10342 	}
10343       else
10344 	{
10345 	  BFD_ASSERT (!info->shared);
10346 	  mips_finish_exec_plt (output_bfd, info);
10347 	}
10348     }
10349   return TRUE;
10350 }
10351 
10352 
10353 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
10354 
10355 static void
10356 mips_set_isa_flags (bfd *abfd)
10357 {
10358   flagword val;
10359 
10360   switch (bfd_get_mach (abfd))
10361     {
10362     default:
10363     case bfd_mach_mips3000:
10364       val = E_MIPS_ARCH_1;
10365       break;
10366 
10367     case bfd_mach_mips3900:
10368       val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10369       break;
10370 
10371     case bfd_mach_mips6000:
10372       val = E_MIPS_ARCH_2;
10373       break;
10374 
10375     case bfd_mach_mips4000:
10376     case bfd_mach_mips4300:
10377     case bfd_mach_mips4400:
10378     case bfd_mach_mips4600:
10379       val = E_MIPS_ARCH_3;
10380       break;
10381 
10382     case bfd_mach_mips4010:
10383       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10384       break;
10385 
10386     case bfd_mach_mips4100:
10387       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10388       break;
10389 
10390     case bfd_mach_mips4111:
10391       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10392       break;
10393 
10394     case bfd_mach_mips4120:
10395       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10396       break;
10397 
10398     case bfd_mach_mips4650:
10399       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10400       break;
10401 
10402     case bfd_mach_mips5400:
10403       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10404       break;
10405 
10406     case bfd_mach_mips5500:
10407       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10408       break;
10409 
10410     case bfd_mach_mips9000:
10411       val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10412       break;
10413 
10414     case bfd_mach_mips5000:
10415     case bfd_mach_mips7000:
10416     case bfd_mach_mips8000:
10417     case bfd_mach_mips10000:
10418     case bfd_mach_mips12000:
10419       val = E_MIPS_ARCH_4;
10420       break;
10421 
10422     case bfd_mach_mips5:
10423       val = E_MIPS_ARCH_5;
10424       break;
10425 
10426     case bfd_mach_mips_loongson_2e:
10427       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10428       break;
10429 
10430     case bfd_mach_mips_loongson_2f:
10431       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10432       break;
10433 
10434     case bfd_mach_mips_sb1:
10435       val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10436       break;
10437 
10438     case bfd_mach_mips_octeon:
10439       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10440       break;
10441 
10442     case bfd_mach_mips_xlr:
10443       val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10444       break;
10445 
10446     case bfd_mach_mipsisa32:
10447       val = E_MIPS_ARCH_32;
10448       break;
10449 
10450     case bfd_mach_mipsisa64:
10451       val = E_MIPS_ARCH_64;
10452       break;
10453 
10454     case bfd_mach_mipsisa32r2:
10455       val = E_MIPS_ARCH_32R2;
10456       break;
10457 
10458     case bfd_mach_mipsisa64r2:
10459       val = E_MIPS_ARCH_64R2;
10460       break;
10461     }
10462   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10463   elf_elfheader (abfd)->e_flags |= val;
10464 
10465 }
10466 
10467 
10468 /* The final processing done just before writing out a MIPS ELF object
10469    file.  This gets the MIPS architecture right based on the machine
10470    number.  This is used by both the 32-bit and the 64-bit ABI.  */
10471 
10472 void
10473 _bfd_mips_elf_final_write_processing (bfd *abfd,
10474 				      bfd_boolean linker ATTRIBUTE_UNUSED)
10475 {
10476   unsigned int i;
10477   Elf_Internal_Shdr **hdrpp;
10478   const char *name;
10479   asection *sec;
10480 
10481   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10482      is nonzero.  This is for compatibility with old objects, which used
10483      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
10484   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10485     mips_set_isa_flags (abfd);
10486 
10487   /* Set the sh_info field for .gptab sections and other appropriate
10488      info for each special section.  */
10489   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10490        i < elf_numsections (abfd);
10491        i++, hdrpp++)
10492     {
10493       switch ((*hdrpp)->sh_type)
10494 	{
10495 	case SHT_MIPS_MSYM:
10496 	case SHT_MIPS_LIBLIST:
10497 	  sec = bfd_get_section_by_name (abfd, ".dynstr");
10498 	  if (sec != NULL)
10499 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10500 	  break;
10501 
10502 	case SHT_MIPS_GPTAB:
10503 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10504 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10505 	  BFD_ASSERT (name != NULL
10506 		      && CONST_STRNEQ (name, ".gptab."));
10507 	  sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10508 	  BFD_ASSERT (sec != NULL);
10509 	  (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10510 	  break;
10511 
10512 	case SHT_MIPS_CONTENT:
10513 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10514 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10515 	  BFD_ASSERT (name != NULL
10516 		      && CONST_STRNEQ (name, ".MIPS.content"));
10517 	  sec = bfd_get_section_by_name (abfd,
10518 					 name + sizeof ".MIPS.content" - 1);
10519 	  BFD_ASSERT (sec != NULL);
10520 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10521 	  break;
10522 
10523 	case SHT_MIPS_SYMBOL_LIB:
10524 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
10525 	  if (sec != NULL)
10526 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10527 	  sec = bfd_get_section_by_name (abfd, ".liblist");
10528 	  if (sec != NULL)
10529 	    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10530 	  break;
10531 
10532 	case SHT_MIPS_EVENTS:
10533 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10534 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10535 	  BFD_ASSERT (name != NULL);
10536 	  if (CONST_STRNEQ (name, ".MIPS.events"))
10537 	    sec = bfd_get_section_by_name (abfd,
10538 					   name + sizeof ".MIPS.events" - 1);
10539 	  else
10540 	    {
10541 	      BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
10542 	      sec = bfd_get_section_by_name (abfd,
10543 					     (name
10544 					      + sizeof ".MIPS.post_rel" - 1));
10545 	    }
10546 	  BFD_ASSERT (sec != NULL);
10547 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10548 	  break;
10549 
10550 	}
10551     }
10552 }
10553 
10554 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
10555    segments.  */
10556 
10557 int
10558 _bfd_mips_elf_additional_program_headers (bfd *abfd,
10559 					  struct bfd_link_info *info ATTRIBUTE_UNUSED)
10560 {
10561   asection *s;
10562   int ret = 0;
10563 
10564   /* See if we need a PT_MIPS_REGINFO segment.  */
10565   s = bfd_get_section_by_name (abfd, ".reginfo");
10566   if (s && (s->flags & SEC_LOAD))
10567     ++ret;
10568 
10569   /* See if we need a PT_MIPS_OPTIONS segment.  */
10570   if (IRIX_COMPAT (abfd) == ict_irix6
10571       && bfd_get_section_by_name (abfd,
10572 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10573     ++ret;
10574 
10575   /* See if we need a PT_MIPS_RTPROC segment.  */
10576   if (IRIX_COMPAT (abfd) == ict_irix5
10577       && bfd_get_section_by_name (abfd, ".dynamic")
10578       && bfd_get_section_by_name (abfd, ".mdebug"))
10579     ++ret;
10580 
10581   /* Allocate a PT_NULL header in dynamic objects.  See
10582      _bfd_mips_elf_modify_segment_map for details.  */
10583   if (!SGI_COMPAT (abfd)
10584       && bfd_get_section_by_name (abfd, ".dynamic"))
10585     ++ret;
10586 
10587   return ret;
10588 }
10589 
10590 /* Modify the segment map for an IRIX5 executable.  */
10591 
10592 bfd_boolean
10593 _bfd_mips_elf_modify_segment_map (bfd *abfd,
10594 				  struct bfd_link_info *info)
10595 {
10596   asection *s;
10597   struct elf_segment_map *m, **pm;
10598   bfd_size_type amt;
10599 
10600   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10601      segment.  */
10602   s = bfd_get_section_by_name (abfd, ".reginfo");
10603   if (s != NULL && (s->flags & SEC_LOAD) != 0)
10604     {
10605       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10606 	if (m->p_type == PT_MIPS_REGINFO)
10607 	  break;
10608       if (m == NULL)
10609 	{
10610 	  amt = sizeof *m;
10611 	  m = bfd_zalloc (abfd, amt);
10612 	  if (m == NULL)
10613 	    return FALSE;
10614 
10615 	  m->p_type = PT_MIPS_REGINFO;
10616 	  m->count = 1;
10617 	  m->sections[0] = s;
10618 
10619 	  /* We want to put it after the PHDR and INTERP segments.  */
10620 	  pm = &elf_tdata (abfd)->segment_map;
10621 	  while (*pm != NULL
10622 		 && ((*pm)->p_type == PT_PHDR
10623 		     || (*pm)->p_type == PT_INTERP))
10624 	    pm = &(*pm)->next;
10625 
10626 	  m->next = *pm;
10627 	  *pm = m;
10628 	}
10629     }
10630 
10631   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10632      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
10633      PT_MIPS_OPTIONS segment immediately following the program header
10634      table.  */
10635   if (NEWABI_P (abfd)
10636       /* On non-IRIX6 new abi, we'll have already created a segment
10637 	 for this section, so don't create another.  I'm not sure this
10638 	 is not also the case for IRIX 6, but I can't test it right
10639 	 now.  */
10640       && IRIX_COMPAT (abfd) == ict_irix6)
10641     {
10642       for (s = abfd->sections; s; s = s->next)
10643 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10644 	  break;
10645 
10646       if (s)
10647 	{
10648 	  struct elf_segment_map *options_segment;
10649 
10650 	  pm = &elf_tdata (abfd)->segment_map;
10651 	  while (*pm != NULL
10652 		 && ((*pm)->p_type == PT_PHDR
10653 		     || (*pm)->p_type == PT_INTERP))
10654 	    pm = &(*pm)->next;
10655 
10656 	  if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10657 	    {
10658 	      amt = sizeof (struct elf_segment_map);
10659 	      options_segment = bfd_zalloc (abfd, amt);
10660 	      options_segment->next = *pm;
10661 	      options_segment->p_type = PT_MIPS_OPTIONS;
10662 	      options_segment->p_flags = PF_R;
10663 	      options_segment->p_flags_valid = TRUE;
10664 	      options_segment->count = 1;
10665 	      options_segment->sections[0] = s;
10666 	      *pm = options_segment;
10667 	    }
10668 	}
10669     }
10670   else
10671     {
10672       if (IRIX_COMPAT (abfd) == ict_irix5)
10673 	{
10674 	  /* If there are .dynamic and .mdebug sections, we make a room
10675 	     for the RTPROC header.  FIXME: Rewrite without section names.  */
10676 	  if (bfd_get_section_by_name (abfd, ".interp") == NULL
10677 	      && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10678 	      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10679 	    {
10680 	      for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10681 		if (m->p_type == PT_MIPS_RTPROC)
10682 		  break;
10683 	      if (m == NULL)
10684 		{
10685 		  amt = sizeof *m;
10686 		  m = bfd_zalloc (abfd, amt);
10687 		  if (m == NULL)
10688 		    return FALSE;
10689 
10690 		  m->p_type = PT_MIPS_RTPROC;
10691 
10692 		  s = bfd_get_section_by_name (abfd, ".rtproc");
10693 		  if (s == NULL)
10694 		    {
10695 		      m->count = 0;
10696 		      m->p_flags = 0;
10697 		      m->p_flags_valid = 1;
10698 		    }
10699 		  else
10700 		    {
10701 		      m->count = 1;
10702 		      m->sections[0] = s;
10703 		    }
10704 
10705 		  /* We want to put it after the DYNAMIC segment.  */
10706 		  pm = &elf_tdata (abfd)->segment_map;
10707 		  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10708 		    pm = &(*pm)->next;
10709 		  if (*pm != NULL)
10710 		    pm = &(*pm)->next;
10711 
10712 		  m->next = *pm;
10713 		  *pm = m;
10714 		}
10715 	    }
10716 	}
10717       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10718 	 .dynstr, .dynsym, and .hash sections, and everything in
10719 	 between.  */
10720       for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10721 	   pm = &(*pm)->next)
10722 	if ((*pm)->p_type == PT_DYNAMIC)
10723 	  break;
10724       m = *pm;
10725       if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10726 	{
10727 	  /* For a normal mips executable the permissions for the PT_DYNAMIC
10728 	     segment are read, write and execute. We do that here since
10729 	     the code in elf.c sets only the read permission. This matters
10730 	     sometimes for the dynamic linker.  */
10731 	  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10732 	    {
10733 	      m->p_flags = PF_R | PF_W | PF_X;
10734 	      m->p_flags_valid = 1;
10735 	    }
10736 	}
10737       /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10738 	 glibc's dynamic linker has traditionally derived the number of
10739 	 tags from the p_filesz field, and sometimes allocates stack
10740 	 arrays of that size.  An overly-big PT_DYNAMIC segment can
10741 	 be actively harmful in such cases.  Making PT_DYNAMIC contain
10742 	 other sections can also make life hard for the prelinker,
10743 	 which might move one of the other sections to a different
10744 	 PT_LOAD segment.  */
10745       if (SGI_COMPAT (abfd)
10746 	  && m != NULL
10747 	  && m->count == 1
10748 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
10749 	{
10750 	  static const char *sec_names[] =
10751 	  {
10752 	    ".dynamic", ".dynstr", ".dynsym", ".hash"
10753 	  };
10754 	  bfd_vma low, high;
10755 	  unsigned int i, c;
10756 	  struct elf_segment_map *n;
10757 
10758 	  low = ~(bfd_vma) 0;
10759 	  high = 0;
10760 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10761 	    {
10762 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
10763 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
10764 		{
10765 		  bfd_size_type sz;
10766 
10767 		  if (low > s->vma)
10768 		    low = s->vma;
10769 		  sz = s->size;
10770 		  if (high < s->vma + sz)
10771 		    high = s->vma + sz;
10772 		}
10773 	    }
10774 
10775 	  c = 0;
10776 	  for (s = abfd->sections; s != NULL; s = s->next)
10777 	    if ((s->flags & SEC_LOAD) != 0
10778 		&& s->vma >= low
10779 		&& s->vma + s->size <= high)
10780 	      ++c;
10781 
10782 	  amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10783 	  n = bfd_zalloc (abfd, amt);
10784 	  if (n == NULL)
10785 	    return FALSE;
10786 	  *n = *m;
10787 	  n->count = c;
10788 
10789 	  i = 0;
10790 	  for (s = abfd->sections; s != NULL; s = s->next)
10791 	    {
10792 	      if ((s->flags & SEC_LOAD) != 0
10793 		  && s->vma >= low
10794 		  && s->vma + s->size <= high)
10795 		{
10796 		  n->sections[i] = s;
10797 		  ++i;
10798 		}
10799 	    }
10800 
10801 	  *pm = n;
10802 	}
10803     }
10804 
10805   /* Allocate a spare program header in dynamic objects so that tools
10806      like the prelinker can add an extra PT_LOAD entry.
10807 
10808      If the prelinker needs to make room for a new PT_LOAD entry, its
10809      standard procedure is to move the first (read-only) sections into
10810      the new (writable) segment.  However, the MIPS ABI requires
10811      .dynamic to be in a read-only segment, and the section will often
10812      start within sizeof (ElfNN_Phdr) bytes of the last program header.
10813 
10814      Although the prelinker could in principle move .dynamic to a
10815      writable segment, it seems better to allocate a spare program
10816      header instead, and avoid the need to move any sections.
10817      There is a long tradition of allocating spare dynamic tags,
10818      so allocating a spare program header seems like a natural
10819      extension.
10820 
10821      If INFO is NULL, we may be copying an already prelinked binary
10822      with objcopy or strip, so do not add this header.  */
10823   if (info != NULL
10824       && !SGI_COMPAT (abfd)
10825       && bfd_get_section_by_name (abfd, ".dynamic"))
10826     {
10827       for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10828 	if ((*pm)->p_type == PT_NULL)
10829 	  break;
10830       if (*pm == NULL)
10831 	{
10832 	  m = bfd_zalloc (abfd, sizeof (*m));
10833 	  if (m == NULL)
10834 	    return FALSE;
10835 
10836 	  m->p_type = PT_NULL;
10837 	  *pm = m;
10838 	}
10839     }
10840 
10841   return TRUE;
10842 }
10843 
10844 /* Return the section that should be marked against GC for a given
10845    relocation.  */
10846 
10847 asection *
10848 _bfd_mips_elf_gc_mark_hook (asection *sec,
10849 			    struct bfd_link_info *info,
10850 			    Elf_Internal_Rela *rel,
10851 			    struct elf_link_hash_entry *h,
10852 			    Elf_Internal_Sym *sym)
10853 {
10854   /* ??? Do mips16 stub sections need to be handled special?  */
10855 
10856   if (h != NULL)
10857     switch (ELF_R_TYPE (sec->owner, rel->r_info))
10858       {
10859       case R_MIPS_GNU_VTINHERIT:
10860       case R_MIPS_GNU_VTENTRY:
10861 	return NULL;
10862       }
10863 
10864   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10865 }
10866 
10867 /* Update the got entry reference counts for the section being removed.  */
10868 
10869 bfd_boolean
10870 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10871 			     struct bfd_link_info *info ATTRIBUTE_UNUSED,
10872 			     asection *sec ATTRIBUTE_UNUSED,
10873 			     const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
10874 {
10875 #if 0
10876   Elf_Internal_Shdr *symtab_hdr;
10877   struct elf_link_hash_entry **sym_hashes;
10878   bfd_signed_vma *local_got_refcounts;
10879   const Elf_Internal_Rela *rel, *relend;
10880   unsigned long r_symndx;
10881   struct elf_link_hash_entry *h;
10882 
10883   if (info->relocatable)
10884     return TRUE;
10885 
10886   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10887   sym_hashes = elf_sym_hashes (abfd);
10888   local_got_refcounts = elf_local_got_refcounts (abfd);
10889 
10890   relend = relocs + sec->reloc_count;
10891   for (rel = relocs; rel < relend; rel++)
10892     switch (ELF_R_TYPE (abfd, rel->r_info))
10893       {
10894       case R_MIPS16_GOT16:
10895       case R_MIPS16_CALL16:
10896       case R_MIPS_GOT16:
10897       case R_MIPS_CALL16:
10898       case R_MIPS_CALL_HI16:
10899       case R_MIPS_CALL_LO16:
10900       case R_MIPS_GOT_HI16:
10901       case R_MIPS_GOT_LO16:
10902       case R_MIPS_GOT_DISP:
10903       case R_MIPS_GOT_PAGE:
10904       case R_MIPS_GOT_OFST:
10905 	/* ??? It would seem that the existing MIPS code does no sort
10906 	   of reference counting or whatnot on its GOT and PLT entries,
10907 	   so it is not possible to garbage collect them at this time.  */
10908 	break;
10909 
10910       default:
10911 	break;
10912       }
10913 #endif
10914 
10915   return TRUE;
10916 }
10917 
10918 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10919    hiding the old indirect symbol.  Process additional relocation
10920    information.  Also called for weakdefs, in which case we just let
10921    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
10922 
10923 void
10924 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
10925 				    struct elf_link_hash_entry *dir,
10926 				    struct elf_link_hash_entry *ind)
10927 {
10928   struct mips_elf_link_hash_entry *dirmips, *indmips;
10929 
10930   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
10931 
10932   dirmips = (struct mips_elf_link_hash_entry *) dir;
10933   indmips = (struct mips_elf_link_hash_entry *) ind;
10934   /* Any absolute non-dynamic relocations against an indirect or weak
10935      definition will be against the target symbol.  */
10936   if (indmips->has_static_relocs)
10937     dirmips->has_static_relocs = TRUE;
10938 
10939   if (ind->root.type != bfd_link_hash_indirect)
10940     return;
10941 
10942   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10943   if (indmips->readonly_reloc)
10944     dirmips->readonly_reloc = TRUE;
10945   if (indmips->no_fn_stub)
10946     dirmips->no_fn_stub = TRUE;
10947   if (indmips->fn_stub)
10948     {
10949       dirmips->fn_stub = indmips->fn_stub;
10950       indmips->fn_stub = NULL;
10951     }
10952   if (indmips->need_fn_stub)
10953     {
10954       dirmips->need_fn_stub = TRUE;
10955       indmips->need_fn_stub = FALSE;
10956     }
10957   if (indmips->call_stub)
10958     {
10959       dirmips->call_stub = indmips->call_stub;
10960       indmips->call_stub = NULL;
10961     }
10962   if (indmips->call_fp_stub)
10963     {
10964       dirmips->call_fp_stub = indmips->call_fp_stub;
10965       indmips->call_fp_stub = NULL;
10966     }
10967   if (indmips->global_got_area < dirmips->global_got_area)
10968     dirmips->global_got_area = indmips->global_got_area;
10969   if (indmips->global_got_area < GGA_NONE)
10970     indmips->global_got_area = GGA_NONE;
10971   if (indmips->has_nonpic_branches)
10972     dirmips->has_nonpic_branches = TRUE;
10973 
10974   if (dirmips->tls_type == 0)
10975     dirmips->tls_type = indmips->tls_type;
10976 }
10977 
10978 #define PDR_SIZE 32
10979 
10980 bfd_boolean
10981 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
10982 			    struct bfd_link_info *info)
10983 {
10984   asection *o;
10985   bfd_boolean ret = FALSE;
10986   unsigned char *tdata;
10987   size_t i, skip;
10988 
10989   o = bfd_get_section_by_name (abfd, ".pdr");
10990   if (! o)
10991     return FALSE;
10992   if (o->size == 0)
10993     return FALSE;
10994   if (o->size % PDR_SIZE != 0)
10995     return FALSE;
10996   if (o->output_section != NULL
10997       && bfd_is_abs_section (o->output_section))
10998     return FALSE;
10999 
11000   tdata = bfd_zmalloc (o->size / PDR_SIZE);
11001   if (! tdata)
11002     return FALSE;
11003 
11004   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11005 					    info->keep_memory);
11006   if (!cookie->rels)
11007     {
11008       free (tdata);
11009       return FALSE;
11010     }
11011 
11012   cookie->rel = cookie->rels;
11013   cookie->relend = cookie->rels + o->reloc_count;
11014 
11015   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11016     {
11017       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11018 	{
11019 	  tdata[i] = 1;
11020 	  skip ++;
11021 	}
11022     }
11023 
11024   if (skip != 0)
11025     {
11026       mips_elf_section_data (o)->u.tdata = tdata;
11027       o->size -= skip * PDR_SIZE;
11028       ret = TRUE;
11029     }
11030   else
11031     free (tdata);
11032 
11033   if (! info->keep_memory)
11034     free (cookie->rels);
11035 
11036   return ret;
11037 }
11038 
11039 bfd_boolean
11040 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11041 {
11042   if (strcmp (sec->name, ".pdr") == 0)
11043     return TRUE;
11044   return FALSE;
11045 }
11046 
11047 bfd_boolean
11048 _bfd_mips_elf_write_section (bfd *output_bfd,
11049 			     struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11050                              asection *sec, bfd_byte *contents)
11051 {
11052   bfd_byte *to, *from, *end;
11053   int i;
11054 
11055   if (strcmp (sec->name, ".pdr") != 0)
11056     return FALSE;
11057 
11058   if (mips_elf_section_data (sec)->u.tdata == NULL)
11059     return FALSE;
11060 
11061   to = contents;
11062   end = contents + sec->size;
11063   for (from = contents, i = 0;
11064        from < end;
11065        from += PDR_SIZE, i++)
11066     {
11067       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11068 	continue;
11069       if (to != from)
11070 	memcpy (to, from, PDR_SIZE);
11071       to += PDR_SIZE;
11072     }
11073   bfd_set_section_contents (output_bfd, sec->output_section, contents,
11074 			    sec->output_offset, sec->size);
11075   return TRUE;
11076 }
11077 
11078 /* MIPS ELF uses a special find_nearest_line routine in order the
11079    handle the ECOFF debugging information.  */
11080 
11081 struct mips_elf_find_line
11082 {
11083   struct ecoff_debug_info d;
11084   struct ecoff_find_line i;
11085 };
11086 
11087 bfd_boolean
11088 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11089 				 asymbol **symbols, bfd_vma offset,
11090 				 const char **filename_ptr,
11091 				 const char **functionname_ptr,
11092 				 unsigned int *line_ptr)
11093 {
11094   asection *msec;
11095 
11096   if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11097 				     filename_ptr, functionname_ptr,
11098 				     line_ptr))
11099     return TRUE;
11100 
11101   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11102 				     filename_ptr, functionname_ptr,
11103 				     line_ptr, ABI_64_P (abfd) ? 8 : 0,
11104 				     &elf_tdata (abfd)->dwarf2_find_line_info))
11105     return TRUE;
11106 
11107   msec = bfd_get_section_by_name (abfd, ".mdebug");
11108   if (msec != NULL)
11109     {
11110       flagword origflags;
11111       struct mips_elf_find_line *fi;
11112       const struct ecoff_debug_swap * const swap =
11113 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11114 
11115       /* If we are called during a link, mips_elf_final_link may have
11116 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
11117 	 if appropriate (which it normally will be).  */
11118       origflags = msec->flags;
11119       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11120 	msec->flags |= SEC_HAS_CONTENTS;
11121 
11122       fi = elf_tdata (abfd)->find_line_info;
11123       if (fi == NULL)
11124 	{
11125 	  bfd_size_type external_fdr_size;
11126 	  char *fraw_src;
11127 	  char *fraw_end;
11128 	  struct fdr *fdr_ptr;
11129 	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
11130 
11131 	  fi = bfd_zalloc (abfd, amt);
11132 	  if (fi == NULL)
11133 	    {
11134 	      msec->flags = origflags;
11135 	      return FALSE;
11136 	    }
11137 
11138 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11139 	    {
11140 	      msec->flags = origflags;
11141 	      return FALSE;
11142 	    }
11143 
11144 	  /* Swap in the FDR information.  */
11145 	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11146 	  fi->d.fdr = bfd_alloc (abfd, amt);
11147 	  if (fi->d.fdr == NULL)
11148 	    {
11149 	      msec->flags = origflags;
11150 	      return FALSE;
11151 	    }
11152 	  external_fdr_size = swap->external_fdr_size;
11153 	  fdr_ptr = fi->d.fdr;
11154 	  fraw_src = (char *) fi->d.external_fdr;
11155 	  fraw_end = (fraw_src
11156 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
11157 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11158 	    (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11159 
11160 	  elf_tdata (abfd)->find_line_info = fi;
11161 
11162 	  /* Note that we don't bother to ever free this information.
11163              find_nearest_line is either called all the time, as in
11164              objdump -l, so the information should be saved, or it is
11165              rarely called, as in ld error messages, so the memory
11166              wasted is unimportant.  Still, it would probably be a
11167              good idea for free_cached_info to throw it away.  */
11168 	}
11169 
11170       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11171 				  &fi->i, filename_ptr, functionname_ptr,
11172 				  line_ptr))
11173 	{
11174 	  msec->flags = origflags;
11175 	  return TRUE;
11176 	}
11177 
11178       msec->flags = origflags;
11179     }
11180 
11181   /* Fall back on the generic ELF find_nearest_line routine.  */
11182 
11183   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11184 				     filename_ptr, functionname_ptr,
11185 				     line_ptr);
11186 }
11187 
11188 bfd_boolean
11189 _bfd_mips_elf_find_inliner_info (bfd *abfd,
11190 				 const char **filename_ptr,
11191 				 const char **functionname_ptr,
11192 				 unsigned int *line_ptr)
11193 {
11194   bfd_boolean found;
11195   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11196 					 functionname_ptr, line_ptr,
11197 					 & elf_tdata (abfd)->dwarf2_find_line_info);
11198   return found;
11199 }
11200 
11201 
11202 /* When are writing out the .options or .MIPS.options section,
11203    remember the bytes we are writing out, so that we can install the
11204    GP value in the section_processing routine.  */
11205 
11206 bfd_boolean
11207 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11208 				    const void *location,
11209 				    file_ptr offset, bfd_size_type count)
11210 {
11211   if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11212     {
11213       bfd_byte *c;
11214 
11215       if (elf_section_data (section) == NULL)
11216 	{
11217 	  bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11218 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
11219 	  if (elf_section_data (section) == NULL)
11220 	    return FALSE;
11221 	}
11222       c = mips_elf_section_data (section)->u.tdata;
11223       if (c == NULL)
11224 	{
11225 	  c = bfd_zalloc (abfd, section->size);
11226 	  if (c == NULL)
11227 	    return FALSE;
11228 	  mips_elf_section_data (section)->u.tdata = c;
11229 	}
11230 
11231       memcpy (c + offset, location, count);
11232     }
11233 
11234   return _bfd_elf_set_section_contents (abfd, section, location, offset,
11235 					count);
11236 }
11237 
11238 /* This is almost identical to bfd_generic_get_... except that some
11239    MIPS relocations need to be handled specially.  Sigh.  */
11240 
11241 bfd_byte *
11242 _bfd_elf_mips_get_relocated_section_contents
11243   (bfd *abfd,
11244    struct bfd_link_info *link_info,
11245    struct bfd_link_order *link_order,
11246    bfd_byte *data,
11247    bfd_boolean relocatable,
11248    asymbol **symbols)
11249 {
11250   /* Get enough memory to hold the stuff */
11251   bfd *input_bfd = link_order->u.indirect.section->owner;
11252   asection *input_section = link_order->u.indirect.section;
11253   bfd_size_type sz;
11254 
11255   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11256   arelent **reloc_vector = NULL;
11257   long reloc_count;
11258 
11259   if (reloc_size < 0)
11260     goto error_return;
11261 
11262   reloc_vector = bfd_malloc (reloc_size);
11263   if (reloc_vector == NULL && reloc_size != 0)
11264     goto error_return;
11265 
11266   /* read in the section */
11267   sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11268   if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11269     goto error_return;
11270 
11271   reloc_count = bfd_canonicalize_reloc (input_bfd,
11272 					input_section,
11273 					reloc_vector,
11274 					symbols);
11275   if (reloc_count < 0)
11276     goto error_return;
11277 
11278   if (reloc_count > 0)
11279     {
11280       arelent **parent;
11281       /* for mips */
11282       int gp_found;
11283       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
11284 
11285       {
11286 	struct bfd_hash_entry *h;
11287 	struct bfd_link_hash_entry *lh;
11288 	/* Skip all this stuff if we aren't mixing formats.  */
11289 	if (abfd && input_bfd
11290 	    && abfd->xvec == input_bfd->xvec)
11291 	  lh = 0;
11292 	else
11293 	  {
11294 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11295 	    lh = (struct bfd_link_hash_entry *) h;
11296 	  }
11297       lookup:
11298 	if (lh)
11299 	  {
11300 	    switch (lh->type)
11301 	      {
11302 	      case bfd_link_hash_undefined:
11303 	      case bfd_link_hash_undefweak:
11304 	      case bfd_link_hash_common:
11305 		gp_found = 0;
11306 		break;
11307 	      case bfd_link_hash_defined:
11308 	      case bfd_link_hash_defweak:
11309 		gp_found = 1;
11310 		gp = lh->u.def.value;
11311 		break;
11312 	      case bfd_link_hash_indirect:
11313 	      case bfd_link_hash_warning:
11314 		lh = lh->u.i.link;
11315 		/* @@FIXME  ignoring warning for now */
11316 		goto lookup;
11317 	      case bfd_link_hash_new:
11318 	      default:
11319 		abort ();
11320 	      }
11321 	  }
11322 	else
11323 	  gp_found = 0;
11324       }
11325       /* end mips */
11326       for (parent = reloc_vector; *parent != NULL; parent++)
11327 	{
11328 	  char *error_message = NULL;
11329 	  bfd_reloc_status_type r;
11330 
11331 	  /* Specific to MIPS: Deal with relocation types that require
11332 	     knowing the gp of the output bfd.  */
11333 	  asymbol *sym = *(*parent)->sym_ptr_ptr;
11334 
11335 	  /* If we've managed to find the gp and have a special
11336 	     function for the relocation then go ahead, else default
11337 	     to the generic handling.  */
11338 	  if (gp_found
11339 	      && (*parent)->howto->special_function
11340 	      == _bfd_mips_elf32_gprel16_reloc)
11341 	    r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11342 					       input_section, relocatable,
11343 					       data, gp);
11344 	  else
11345 	    r = bfd_perform_relocation (input_bfd, *parent, data,
11346 					input_section,
11347 					relocatable ? abfd : NULL,
11348 					&error_message);
11349 
11350 	  if (relocatable)
11351 	    {
11352 	      asection *os = input_section->output_section;
11353 
11354 	      /* A partial link, so keep the relocs */
11355 	      os->orelocation[os->reloc_count] = *parent;
11356 	      os->reloc_count++;
11357 	    }
11358 
11359 	  if (r != bfd_reloc_ok)
11360 	    {
11361 	      switch (r)
11362 		{
11363 		case bfd_reloc_undefined:
11364 		  if (!((*link_info->callbacks->undefined_symbol)
11365 			(link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11366 			 input_bfd, input_section, (*parent)->address, TRUE)))
11367 		    goto error_return;
11368 		  break;
11369 		case bfd_reloc_dangerous:
11370 		  BFD_ASSERT (error_message != NULL);
11371 		  if (!((*link_info->callbacks->reloc_dangerous)
11372 			(link_info, error_message, input_bfd, input_section,
11373 			 (*parent)->address)))
11374 		    goto error_return;
11375 		  break;
11376 		case bfd_reloc_overflow:
11377 		  if (!((*link_info->callbacks->reloc_overflow)
11378 			(link_info, NULL,
11379 			 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11380 			 (*parent)->howto->name, (*parent)->addend,
11381 			 input_bfd, input_section, (*parent)->address)))
11382 		    goto error_return;
11383 		  break;
11384 		case bfd_reloc_outofrange:
11385 		default:
11386 		  abort ();
11387 		  break;
11388 		}
11389 
11390 	    }
11391 	}
11392     }
11393   if (reloc_vector != NULL)
11394     free (reloc_vector);
11395   return data;
11396 
11397 error_return:
11398   if (reloc_vector != NULL)
11399     free (reloc_vector);
11400   return NULL;
11401 }
11402 
11403 /* Allocate ABFD's target-dependent data.  */
11404 
11405 bfd_boolean
11406 _bfd_mips_elf_mkobject (bfd *abfd)
11407 {
11408   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
11409 				  MIPS_ELF_TDATA);
11410 }
11411 
11412 /* Create a MIPS ELF linker hash table.  */
11413 
11414 struct bfd_link_hash_table *
11415 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
11416 {
11417   struct mips_elf_link_hash_table *ret;
11418   bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11419 
11420   ret = bfd_malloc (amt);
11421   if (ret == NULL)
11422     return NULL;
11423 
11424   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11425 				      mips_elf_link_hash_newfunc,
11426 				      sizeof (struct mips_elf_link_hash_entry)))
11427     {
11428       free (ret);
11429       return NULL;
11430     }
11431 
11432 #if 0
11433   /* We no longer use this.  */
11434   for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11435     ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11436 #endif
11437   ret->procedure_count = 0;
11438   ret->compact_rel_size = 0;
11439   ret->use_rld_obj_head = FALSE;
11440   ret->rld_value = 0;
11441   ret->mips16_stubs_seen = FALSE;
11442   ret->use_plts_and_copy_relocs = FALSE;
11443   ret->is_vxworks = FALSE;
11444   ret->small_data_overflow_reported = FALSE;
11445   ret->srelbss = NULL;
11446   ret->sdynbss = NULL;
11447   ret->srelplt = NULL;
11448   ret->srelplt2 = NULL;
11449   ret->sgotplt = NULL;
11450   ret->splt = NULL;
11451   ret->sstubs = NULL;
11452   ret->sgot = NULL;
11453   ret->got_info = NULL;
11454   ret->plt_header_size = 0;
11455   ret->plt_entry_size = 0;
11456   ret->lazy_stub_count = 0;
11457   ret->function_stub_size = 0;
11458   ret->strampoline = NULL;
11459   ret->la25_stubs = NULL;
11460   ret->add_stub_section = NULL;
11461 
11462   return &ret->root.root;
11463 }
11464 
11465 /* Likewise, but indicate that the target is VxWorks.  */
11466 
11467 struct bfd_link_hash_table *
11468 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11469 {
11470   struct bfd_link_hash_table *ret;
11471 
11472   ret = _bfd_mips_elf_link_hash_table_create (abfd);
11473   if (ret)
11474     {
11475       struct mips_elf_link_hash_table *htab;
11476 
11477       htab = (struct mips_elf_link_hash_table *) ret;
11478       htab->use_plts_and_copy_relocs = TRUE;
11479       htab->is_vxworks = TRUE;
11480     }
11481   return ret;
11482 }
11483 
11484 /* A function that the linker calls if we are allowed to use PLTs
11485    and copy relocs.  */
11486 
11487 void
11488 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11489 {
11490   mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11491 }
11492 
11493 /* We need to use a special link routine to handle the .reginfo and
11494    the .mdebug sections.  We need to merge all instances of these
11495    sections together, not write them all out sequentially.  */
11496 
11497 bfd_boolean
11498 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11499 {
11500   asection *o;
11501   struct bfd_link_order *p;
11502   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11503   asection *rtproc_sec;
11504   Elf32_RegInfo reginfo;
11505   struct ecoff_debug_info debug;
11506   struct mips_htab_traverse_info hti;
11507   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11508   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
11509   HDRR *symhdr = &debug.symbolic_header;
11510   void *mdebug_handle = NULL;
11511   asection *s;
11512   EXTR esym;
11513   unsigned int i;
11514   bfd_size_type amt;
11515   struct mips_elf_link_hash_table *htab;
11516 
11517   static const char * const secname[] =
11518   {
11519     ".text", ".init", ".fini", ".data",
11520     ".rodata", ".sdata", ".sbss", ".bss"
11521   };
11522   static const int sc[] =
11523   {
11524     scText, scInit, scFini, scData,
11525     scRData, scSData, scSBss, scBss
11526   };
11527 
11528   /* Sort the dynamic symbols so that those with GOT entries come after
11529      those without.  */
11530   htab = mips_elf_hash_table (info);
11531   if (!mips_elf_sort_hash_table (abfd, info))
11532     return FALSE;
11533 
11534   /* Create any scheduled LA25 stubs.  */
11535   hti.info = info;
11536   hti.output_bfd = abfd;
11537   hti.error = FALSE;
11538   htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11539   if (hti.error)
11540     return FALSE;
11541 
11542   /* Get a value for the GP register.  */
11543   if (elf_gp (abfd) == 0)
11544     {
11545       struct bfd_link_hash_entry *h;
11546 
11547       h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
11548       if (h != NULL && h->type == bfd_link_hash_defined)
11549 	elf_gp (abfd) = (h->u.def.value
11550 			 + h->u.def.section->output_section->vma
11551 			 + h->u.def.section->output_offset);
11552       else if (htab->is_vxworks
11553 	       && (h = bfd_link_hash_lookup (info->hash,
11554 					     "_GLOBAL_OFFSET_TABLE_",
11555 					     FALSE, FALSE, TRUE))
11556 	       && h->type == bfd_link_hash_defined)
11557 	elf_gp (abfd) = (h->u.def.section->output_section->vma
11558 			 + h->u.def.section->output_offset
11559 			 + h->u.def.value);
11560       else if (info->relocatable)
11561 	{
11562 	  bfd_vma lo = MINUS_ONE;
11563 
11564 	  /* Find the GP-relative section with the lowest offset.  */
11565 	  for (o = abfd->sections; o != NULL; o = o->next)
11566 	    if (o->vma < lo
11567 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11568 	      lo = o->vma;
11569 
11570 	  /* And calculate GP relative to that.  */
11571 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
11572 	}
11573       else
11574 	{
11575 	  /* If the relocate_section function needs to do a reloc
11576 	     involving the GP value, it should make a reloc_dangerous
11577 	     callback to warn that GP is not defined.  */
11578 	}
11579     }
11580 
11581   /* Go through the sections and collect the .reginfo and .mdebug
11582      information.  */
11583   reginfo_sec = NULL;
11584   mdebug_sec = NULL;
11585   gptab_data_sec = NULL;
11586   gptab_bss_sec = NULL;
11587   for (o = abfd->sections; o != NULL; o = o->next)
11588     {
11589       if (strcmp (o->name, ".reginfo") == 0)
11590 	{
11591 	  memset (&reginfo, 0, sizeof reginfo);
11592 
11593 	  /* We have found the .reginfo section in the output file.
11594 	     Look through all the link_orders comprising it and merge
11595 	     the information together.  */
11596 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
11597 	    {
11598 	      asection *input_section;
11599 	      bfd *input_bfd;
11600 	      Elf32_External_RegInfo ext;
11601 	      Elf32_RegInfo sub;
11602 
11603 	      if (p->type != bfd_indirect_link_order)
11604 		{
11605 		  if (p->type == bfd_data_link_order)
11606 		    continue;
11607 		  abort ();
11608 		}
11609 
11610 	      input_section = p->u.indirect.section;
11611 	      input_bfd = input_section->owner;
11612 
11613 	      if (! bfd_get_section_contents (input_bfd, input_section,
11614 					      &ext, 0, sizeof ext))
11615 		return FALSE;
11616 
11617 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11618 
11619 	      reginfo.ri_gprmask |= sub.ri_gprmask;
11620 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11621 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11622 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11623 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11624 
11625 	      /* ri_gp_value is set by the function
11626 		 mips_elf32_section_processing when the section is
11627 		 finally written out.  */
11628 
11629 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
11630 		 elf_link_input_bfd ignores this section.  */
11631 	      input_section->flags &= ~SEC_HAS_CONTENTS;
11632 	    }
11633 
11634 	  /* Size has been set in _bfd_mips_elf_always_size_sections.  */
11635 	  BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
11636 
11637 	  /* Skip this section later on (I don't think this currently
11638 	     matters, but someday it might).  */
11639 	  o->map_head.link_order = NULL;
11640 
11641 	  reginfo_sec = o;
11642 	}
11643 
11644       if (strcmp (o->name, ".mdebug") == 0)
11645 	{
11646 	  struct extsym_info einfo;
11647 	  bfd_vma last;
11648 
11649 	  /* We have found the .mdebug section in the output file.
11650 	     Look through all the link_orders comprising it and merge
11651 	     the information together.  */
11652 	  symhdr->magic = swap->sym_magic;
11653 	  /* FIXME: What should the version stamp be?  */
11654 	  symhdr->vstamp = 0;
11655 	  symhdr->ilineMax = 0;
11656 	  symhdr->cbLine = 0;
11657 	  symhdr->idnMax = 0;
11658 	  symhdr->ipdMax = 0;
11659 	  symhdr->isymMax = 0;
11660 	  symhdr->ioptMax = 0;
11661 	  symhdr->iauxMax = 0;
11662 	  symhdr->issMax = 0;
11663 	  symhdr->issExtMax = 0;
11664 	  symhdr->ifdMax = 0;
11665 	  symhdr->crfd = 0;
11666 	  symhdr->iextMax = 0;
11667 
11668 	  /* We accumulate the debugging information itself in the
11669 	     debug_info structure.  */
11670 	  debug.line = NULL;
11671 	  debug.external_dnr = NULL;
11672 	  debug.external_pdr = NULL;
11673 	  debug.external_sym = NULL;
11674 	  debug.external_opt = NULL;
11675 	  debug.external_aux = NULL;
11676 	  debug.ss = NULL;
11677 	  debug.ssext = debug.ssext_end = NULL;
11678 	  debug.external_fdr = NULL;
11679 	  debug.external_rfd = NULL;
11680 	  debug.external_ext = debug.external_ext_end = NULL;
11681 
11682 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
11683 	  if (mdebug_handle == NULL)
11684 	    return FALSE;
11685 
11686 	  esym.jmptbl = 0;
11687 	  esym.cobol_main = 0;
11688 	  esym.weakext = 0;
11689 	  esym.reserved = 0;
11690 	  esym.ifd = ifdNil;
11691 	  esym.asym.iss = issNil;
11692 	  esym.asym.st = stLocal;
11693 	  esym.asym.reserved = 0;
11694 	  esym.asym.index = indexNil;
11695 	  last = 0;
11696 	  for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11697 	    {
11698 	      esym.asym.sc = sc[i];
11699 	      s = bfd_get_section_by_name (abfd, secname[i]);
11700 	      if (s != NULL)
11701 		{
11702 		  esym.asym.value = s->vma;
11703 		  last = s->vma + s->size;
11704 		}
11705 	      else
11706 		esym.asym.value = last;
11707 	      if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11708 						 secname[i], &esym))
11709 		return FALSE;
11710 	    }
11711 
11712 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
11713 	    {
11714 	      asection *input_section;
11715 	      bfd *input_bfd;
11716 	      const struct ecoff_debug_swap *input_swap;
11717 	      struct ecoff_debug_info input_debug;
11718 	      char *eraw_src;
11719 	      char *eraw_end;
11720 
11721 	      if (p->type != bfd_indirect_link_order)
11722 		{
11723 		  if (p->type == bfd_data_link_order)
11724 		    continue;
11725 		  abort ();
11726 		}
11727 
11728 	      input_section = p->u.indirect.section;
11729 	      input_bfd = input_section->owner;
11730 
11731 	      if (!is_mips_elf (input_bfd))
11732 		{
11733 		  /* I don't know what a non MIPS ELF bfd would be
11734 		     doing with a .mdebug section, but I don't really
11735 		     want to deal with it.  */
11736 		  continue;
11737 		}
11738 
11739 	      input_swap = (get_elf_backend_data (input_bfd)
11740 			    ->elf_backend_ecoff_debug_swap);
11741 
11742 	      BFD_ASSERT (p->size == input_section->size);
11743 
11744 	      /* The ECOFF linking code expects that we have already
11745 		 read in the debugging information and set up an
11746 		 ecoff_debug_info structure, so we do that now.  */
11747 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11748 						   &input_debug))
11749 		return FALSE;
11750 
11751 	      if (! (bfd_ecoff_debug_accumulate
11752 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
11753 		      &input_debug, input_swap, info)))
11754 		return FALSE;
11755 
11756 	      /* Loop through the external symbols.  For each one with
11757 		 interesting information, try to find the symbol in
11758 		 the linker global hash table and save the information
11759 		 for the output external symbols.  */
11760 	      eraw_src = input_debug.external_ext;
11761 	      eraw_end = (eraw_src
11762 			  + (input_debug.symbolic_header.iextMax
11763 			     * input_swap->external_ext_size));
11764 	      for (;
11765 		   eraw_src < eraw_end;
11766 		   eraw_src += input_swap->external_ext_size)
11767 		{
11768 		  EXTR ext;
11769 		  const char *name;
11770 		  struct mips_elf_link_hash_entry *h;
11771 
11772 		  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11773 		  if (ext.asym.sc == scNil
11774 		      || ext.asym.sc == scUndefined
11775 		      || ext.asym.sc == scSUndefined)
11776 		    continue;
11777 
11778 		  name = input_debug.ssext + ext.asym.iss;
11779 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11780 						 name, FALSE, FALSE, TRUE);
11781 		  if (h == NULL || h->esym.ifd != -2)
11782 		    continue;
11783 
11784 		  if (ext.ifd != -1)
11785 		    {
11786 		      BFD_ASSERT (ext.ifd
11787 				  < input_debug.symbolic_header.ifdMax);
11788 		      ext.ifd = input_debug.ifdmap[ext.ifd];
11789 		    }
11790 
11791 		  h->esym = ext;
11792 		}
11793 
11794 	      /* Free up the information we just read.  */
11795 	      free (input_debug.line);
11796 	      free (input_debug.external_dnr);
11797 	      free (input_debug.external_pdr);
11798 	      free (input_debug.external_sym);
11799 	      free (input_debug.external_opt);
11800 	      free (input_debug.external_aux);
11801 	      free (input_debug.ss);
11802 	      free (input_debug.ssext);
11803 	      free (input_debug.external_fdr);
11804 	      free (input_debug.external_rfd);
11805 	      free (input_debug.external_ext);
11806 
11807 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
11808 		 elf_link_input_bfd ignores this section.  */
11809 	      input_section->flags &= ~SEC_HAS_CONTENTS;
11810 	    }
11811 
11812 	  if (SGI_COMPAT (abfd) && info->shared)
11813 	    {
11814 	      /* Create .rtproc section.  */
11815 	      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11816 	      if (rtproc_sec == NULL)
11817 		{
11818 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11819 				    | SEC_LINKER_CREATED | SEC_READONLY);
11820 
11821 		  rtproc_sec = bfd_make_section_with_flags (abfd,
11822 							    ".rtproc",
11823 							    flags);
11824 		  if (rtproc_sec == NULL
11825 		      || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11826 		    return FALSE;
11827 		}
11828 
11829 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11830 						     info, rtproc_sec,
11831 						     &debug))
11832 		return FALSE;
11833 	    }
11834 
11835 	  /* Build the external symbol information.  */
11836 	  einfo.abfd = abfd;
11837 	  einfo.info = info;
11838 	  einfo.debug = &debug;
11839 	  einfo.swap = swap;
11840 	  einfo.failed = FALSE;
11841 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11842 				       mips_elf_output_extsym, &einfo);
11843 	  if (einfo.failed)
11844 	    return FALSE;
11845 
11846 	  /* Set the size of the .mdebug section.  */
11847 	  o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11848 
11849 	  /* Skip this section later on (I don't think this currently
11850 	     matters, but someday it might).  */
11851 	  o->map_head.link_order = NULL;
11852 
11853 	  mdebug_sec = o;
11854 	}
11855 
11856       if (CONST_STRNEQ (o->name, ".gptab."))
11857 	{
11858 	  const char *subname;
11859 	  unsigned int c;
11860 	  Elf32_gptab *tab;
11861 	  Elf32_External_gptab *ext_tab;
11862 	  unsigned int j;
11863 
11864 	  /* The .gptab.sdata and .gptab.sbss sections hold
11865 	     information describing how the small data area would
11866 	     change depending upon the -G switch.  These sections
11867 	     not used in executables files.  */
11868 	  if (! info->relocatable)
11869 	    {
11870 	      for (p = o->map_head.link_order; p != NULL; p = p->next)
11871 		{
11872 		  asection *input_section;
11873 
11874 		  if (p->type != bfd_indirect_link_order)
11875 		    {
11876 		      if (p->type == bfd_data_link_order)
11877 			continue;
11878 		      abort ();
11879 		    }
11880 
11881 		  input_section = p->u.indirect.section;
11882 
11883 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
11884 		     elf_link_input_bfd ignores this section.  */
11885 		  input_section->flags &= ~SEC_HAS_CONTENTS;
11886 		}
11887 
11888 	      /* Skip this section later on (I don't think this
11889 		 currently matters, but someday it might).  */
11890 	      o->map_head.link_order = NULL;
11891 
11892 	      /* Really remove the section.  */
11893 	      bfd_section_list_remove (abfd, o);
11894 	      --abfd->section_count;
11895 
11896 	      continue;
11897 	    }
11898 
11899 	  /* There is one gptab for initialized data, and one for
11900 	     uninitialized data.  */
11901 	  if (strcmp (o->name, ".gptab.sdata") == 0)
11902 	    gptab_data_sec = o;
11903 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
11904 	    gptab_bss_sec = o;
11905 	  else
11906 	    {
11907 	      (*_bfd_error_handler)
11908 		(_("%s: illegal section name `%s'"),
11909 		 bfd_get_filename (abfd), o->name);
11910 	      bfd_set_error (bfd_error_nonrepresentable_section);
11911 	      return FALSE;
11912 	    }
11913 
11914 	  /* The linker script always combines .gptab.data and
11915 	     .gptab.sdata into .gptab.sdata, and likewise for
11916 	     .gptab.bss and .gptab.sbss.  It is possible that there is
11917 	     no .sdata or .sbss section in the output file, in which
11918 	     case we must change the name of the output section.  */
11919 	  subname = o->name + sizeof ".gptab" - 1;
11920 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
11921 	    {
11922 	      if (o == gptab_data_sec)
11923 		o->name = ".gptab.data";
11924 	      else
11925 		o->name = ".gptab.bss";
11926 	      subname = o->name + sizeof ".gptab" - 1;
11927 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11928 	    }
11929 
11930 	  /* Set up the first entry.  */
11931 	  c = 1;
11932 	  amt = c * sizeof (Elf32_gptab);
11933 	  tab = bfd_malloc (amt);
11934 	  if (tab == NULL)
11935 	    return FALSE;
11936 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11937 	  tab[0].gt_header.gt_unused = 0;
11938 
11939 	  /* Combine the input sections.  */
11940 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
11941 	    {
11942 	      asection *input_section;
11943 	      bfd *input_bfd;
11944 	      bfd_size_type size;
11945 	      unsigned long last;
11946 	      bfd_size_type gpentry;
11947 
11948 	      if (p->type != bfd_indirect_link_order)
11949 		{
11950 		  if (p->type == bfd_data_link_order)
11951 		    continue;
11952 		  abort ();
11953 		}
11954 
11955 	      input_section = p->u.indirect.section;
11956 	      input_bfd = input_section->owner;
11957 
11958 	      /* Combine the gptab entries for this input section one
11959 		 by one.  We know that the input gptab entries are
11960 		 sorted by ascending -G value.  */
11961 	      size = input_section->size;
11962 	      last = 0;
11963 	      for (gpentry = sizeof (Elf32_External_gptab);
11964 		   gpentry < size;
11965 		   gpentry += sizeof (Elf32_External_gptab))
11966 		{
11967 		  Elf32_External_gptab ext_gptab;
11968 		  Elf32_gptab int_gptab;
11969 		  unsigned long val;
11970 		  unsigned long add;
11971 		  bfd_boolean exact;
11972 		  unsigned int look;
11973 
11974 		  if (! (bfd_get_section_contents
11975 			 (input_bfd, input_section, &ext_gptab, gpentry,
11976 			  sizeof (Elf32_External_gptab))))
11977 		    {
11978 		      free (tab);
11979 		      return FALSE;
11980 		    }
11981 
11982 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
11983 						&int_gptab);
11984 		  val = int_gptab.gt_entry.gt_g_value;
11985 		  add = int_gptab.gt_entry.gt_bytes - last;
11986 
11987 		  exact = FALSE;
11988 		  for (look = 1; look < c; look++)
11989 		    {
11990 		      if (tab[look].gt_entry.gt_g_value >= val)
11991 			tab[look].gt_entry.gt_bytes += add;
11992 
11993 		      if (tab[look].gt_entry.gt_g_value == val)
11994 			exact = TRUE;
11995 		    }
11996 
11997 		  if (! exact)
11998 		    {
11999 		      Elf32_gptab *new_tab;
12000 		      unsigned int max;
12001 
12002 		      /* We need a new table entry.  */
12003 		      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
12004 		      new_tab = bfd_realloc (tab, amt);
12005 		      if (new_tab == NULL)
12006 			{
12007 			  free (tab);
12008 			  return FALSE;
12009 			}
12010 		      tab = new_tab;
12011 		      tab[c].gt_entry.gt_g_value = val;
12012 		      tab[c].gt_entry.gt_bytes = add;
12013 
12014 		      /* Merge in the size for the next smallest -G
12015 			 value, since that will be implied by this new
12016 			 value.  */
12017 		      max = 0;
12018 		      for (look = 1; look < c; look++)
12019 			{
12020 			  if (tab[look].gt_entry.gt_g_value < val
12021 			      && (max == 0
12022 				  || (tab[look].gt_entry.gt_g_value
12023 				      > tab[max].gt_entry.gt_g_value)))
12024 			    max = look;
12025 			}
12026 		      if (max != 0)
12027 			tab[c].gt_entry.gt_bytes +=
12028 			  tab[max].gt_entry.gt_bytes;
12029 
12030 		      ++c;
12031 		    }
12032 
12033 		  last = int_gptab.gt_entry.gt_bytes;
12034 		}
12035 
12036 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
12037 		 elf_link_input_bfd ignores this section.  */
12038 	      input_section->flags &= ~SEC_HAS_CONTENTS;
12039 	    }
12040 
12041 	  /* The table must be sorted by -G value.  */
12042 	  if (c > 2)
12043 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12044 
12045 	  /* Swap out the table.  */
12046 	  amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
12047 	  ext_tab = bfd_alloc (abfd, amt);
12048 	  if (ext_tab == NULL)
12049 	    {
12050 	      free (tab);
12051 	      return FALSE;
12052 	    }
12053 
12054 	  for (j = 0; j < c; j++)
12055 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12056 	  free (tab);
12057 
12058 	  o->size = c * sizeof (Elf32_External_gptab);
12059 	  o->contents = (bfd_byte *) ext_tab;
12060 
12061 	  /* Skip this section later on (I don't think this currently
12062 	     matters, but someday it might).  */
12063 	  o->map_head.link_order = NULL;
12064 	}
12065     }
12066 
12067   /* Invoke the regular ELF backend linker to do all the work.  */
12068   if (!bfd_elf_final_link (abfd, info))
12069     return FALSE;
12070 
12071   /* Now write out the computed sections.  */
12072 
12073   if (reginfo_sec != NULL)
12074     {
12075       Elf32_External_RegInfo ext;
12076 
12077       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
12078       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
12079 	return FALSE;
12080     }
12081 
12082   if (mdebug_sec != NULL)
12083     {
12084       BFD_ASSERT (abfd->output_has_begun);
12085       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12086 					       swap, info,
12087 					       mdebug_sec->filepos))
12088 	return FALSE;
12089 
12090       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12091     }
12092 
12093   if (gptab_data_sec != NULL)
12094     {
12095       if (! bfd_set_section_contents (abfd, gptab_data_sec,
12096 				      gptab_data_sec->contents,
12097 				      0, gptab_data_sec->size))
12098 	return FALSE;
12099     }
12100 
12101   if (gptab_bss_sec != NULL)
12102     {
12103       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12104 				      gptab_bss_sec->contents,
12105 				      0, gptab_bss_sec->size))
12106 	return FALSE;
12107     }
12108 
12109   if (SGI_COMPAT (abfd))
12110     {
12111       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12112       if (rtproc_sec != NULL)
12113 	{
12114 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
12115 					  rtproc_sec->contents,
12116 					  0, rtproc_sec->size))
12117 	    return FALSE;
12118 	}
12119     }
12120 
12121   return TRUE;
12122 }
12123 
12124 /* Structure for saying that BFD machine EXTENSION extends BASE.  */
12125 
12126 struct mips_mach_extension {
12127   unsigned long extension, base;
12128 };
12129 
12130 
12131 /* An array describing how BFD machines relate to one another.  The entries
12132    are ordered topologically with MIPS I extensions listed last.  */
12133 
12134 static const struct mips_mach_extension mips_mach_extensions[] = {
12135   /* MIPS64r2 extensions.  */
12136   { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12137 
12138   /* MIPS64 extensions.  */
12139   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
12140   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
12141   { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
12142 
12143   /* MIPS V extensions.  */
12144   { bfd_mach_mipsisa64, bfd_mach_mips5 },
12145 
12146   /* R10000 extensions.  */
12147   { bfd_mach_mips12000, bfd_mach_mips10000 },
12148 
12149   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
12150      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
12151      better to allow vr5400 and vr5500 code to be merged anyway, since
12152      many libraries will just use the core ISA.  Perhaps we could add
12153      some sort of ASE flag if this ever proves a problem.  */
12154   { bfd_mach_mips5500, bfd_mach_mips5400 },
12155   { bfd_mach_mips5400, bfd_mach_mips5000 },
12156 
12157   /* MIPS IV extensions.  */
12158   { bfd_mach_mips5, bfd_mach_mips8000 },
12159   { bfd_mach_mips10000, bfd_mach_mips8000 },
12160   { bfd_mach_mips5000, bfd_mach_mips8000 },
12161   { bfd_mach_mips7000, bfd_mach_mips8000 },
12162   { bfd_mach_mips9000, bfd_mach_mips8000 },
12163 
12164   /* VR4100 extensions.  */
12165   { bfd_mach_mips4120, bfd_mach_mips4100 },
12166   { bfd_mach_mips4111, bfd_mach_mips4100 },
12167 
12168   /* MIPS III extensions.  */
12169   { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12170   { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
12171   { bfd_mach_mips8000, bfd_mach_mips4000 },
12172   { bfd_mach_mips4650, bfd_mach_mips4000 },
12173   { bfd_mach_mips4600, bfd_mach_mips4000 },
12174   { bfd_mach_mips4400, bfd_mach_mips4000 },
12175   { bfd_mach_mips4300, bfd_mach_mips4000 },
12176   { bfd_mach_mips4100, bfd_mach_mips4000 },
12177   { bfd_mach_mips4010, bfd_mach_mips4000 },
12178 
12179   /* MIPS32 extensions.  */
12180   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12181 
12182   /* MIPS II extensions.  */
12183   { bfd_mach_mips4000, bfd_mach_mips6000 },
12184   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12185 
12186   /* MIPS I extensions.  */
12187   { bfd_mach_mips6000, bfd_mach_mips3000 },
12188   { bfd_mach_mips3900, bfd_mach_mips3000 }
12189 };
12190 
12191 
12192 /* Return true if bfd machine EXTENSION is an extension of machine BASE.  */
12193 
12194 static bfd_boolean
12195 mips_mach_extends_p (unsigned long base, unsigned long extension)
12196 {
12197   size_t i;
12198 
12199   if (extension == base)
12200     return TRUE;
12201 
12202   if (base == bfd_mach_mipsisa32
12203       && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12204     return TRUE;
12205 
12206   if (base == bfd_mach_mipsisa32r2
12207       && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12208     return TRUE;
12209 
12210   for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
12211     if (extension == mips_mach_extensions[i].extension)
12212       {
12213 	extension = mips_mach_extensions[i].base;
12214 	if (extension == base)
12215 	  return TRUE;
12216       }
12217 
12218   return FALSE;
12219 }
12220 
12221 
12222 /* Return true if the given ELF header flags describe a 32-bit binary.  */
12223 
12224 static bfd_boolean
12225 mips_32bit_flags_p (flagword flags)
12226 {
12227   return ((flags & EF_MIPS_32BITMODE) != 0
12228 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12229 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12230 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12231 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12232 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12233 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
12234 }
12235 
12236 
12237 /* Merge object attributes from IBFD into OBFD.  Raise an error if
12238    there are conflicting attributes.  */
12239 static bfd_boolean
12240 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12241 {
12242   obj_attribute *in_attr;
12243   obj_attribute *out_attr;
12244 
12245   if (!elf_known_obj_attributes_proc (obfd)[0].i)
12246     {
12247       /* This is the first object.  Copy the attributes.  */
12248       _bfd_elf_copy_obj_attributes (ibfd, obfd);
12249 
12250       /* Use the Tag_null value to indicate the attributes have been
12251 	 initialized.  */
12252       elf_known_obj_attributes_proc (obfd)[0].i = 1;
12253 
12254       return TRUE;
12255     }
12256 
12257   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12258      non-conflicting ones.  */
12259   in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12260   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12261   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12262     {
12263       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12264       if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12265 	out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12266       else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12267 	;
12268       else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12269 	_bfd_error_handler
12270 	  (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12271 	   in_attr[Tag_GNU_MIPS_ABI_FP].i);
12272       else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12273 	_bfd_error_handler
12274 	  (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12275 	   out_attr[Tag_GNU_MIPS_ABI_FP].i);
12276       else
12277 	switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12278 	  {
12279 	  case 1:
12280 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12281 	      {
12282 	      case 2:
12283 		_bfd_error_handler
12284 		  (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12285 		   obfd, ibfd);
12286 		break;
12287 
12288 	      case 3:
12289 		_bfd_error_handler
12290 		  (_("Warning: %B uses hard float, %B uses soft float"),
12291 		   obfd, ibfd);
12292 		break;
12293 
12294 	      case 4:
12295 		_bfd_error_handler
12296 		  (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12297 		   obfd, ibfd);
12298 		break;
12299 
12300 	      default:
12301 		abort ();
12302 	      }
12303 	    break;
12304 
12305 	  case 2:
12306 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12307 	      {
12308 	      case 1:
12309 		_bfd_error_handler
12310 		  (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12311 		   ibfd, obfd);
12312 		break;
12313 
12314 	      case 3:
12315 		_bfd_error_handler
12316 		  (_("Warning: %B uses hard float, %B uses soft float"),
12317 		   obfd, ibfd);
12318 		break;
12319 
12320 	      case 4:
12321 		_bfd_error_handler
12322 		  (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12323 		   obfd, ibfd);
12324 		break;
12325 
12326 	      default:
12327 		abort ();
12328 	      }
12329 	    break;
12330 
12331 	  case 3:
12332 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12333 	      {
12334 	      case 1:
12335 	      case 2:
12336 	      case 4:
12337 		_bfd_error_handler
12338 		  (_("Warning: %B uses hard float, %B uses soft float"),
12339 		   ibfd, obfd);
12340 		break;
12341 
12342 	      default:
12343 		abort ();
12344 	      }
12345 	    break;
12346 
12347 	  case 4:
12348 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12349 	      {
12350 	      case 1:
12351 		_bfd_error_handler
12352 		  (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12353 		   ibfd, obfd);
12354 		break;
12355 
12356 	      case 2:
12357 		_bfd_error_handler
12358 		  (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12359 		   ibfd, obfd);
12360 		break;
12361 
12362 	      case 3:
12363 		_bfd_error_handler
12364 		  (_("Warning: %B uses hard float, %B uses soft float"),
12365 		   obfd, ibfd);
12366 		break;
12367 
12368 	      default:
12369 		abort ();
12370 	      }
12371 	    break;
12372 
12373 	  default:
12374 	    abort ();
12375 	  }
12376     }
12377 
12378   /* Merge Tag_compatibility attributes and any common GNU ones.  */
12379   _bfd_elf_merge_object_attributes (ibfd, obfd);
12380 
12381   return TRUE;
12382 }
12383 
12384 /* Merge backend specific data from an object file to the output
12385    object file when linking.  */
12386 
12387 bfd_boolean
12388 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
12389 {
12390   flagword old_flags;
12391   flagword new_flags;
12392   bfd_boolean ok;
12393   bfd_boolean null_input_bfd = TRUE;
12394   asection *sec;
12395 
12396   /* Check if we have the same endianess */
12397   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
12398     {
12399       (*_bfd_error_handler)
12400 	(_("%B: endianness incompatible with that of the selected emulation"),
12401 	 ibfd);
12402       return FALSE;
12403     }
12404 
12405   if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
12406     return TRUE;
12407 
12408   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12409     {
12410       (*_bfd_error_handler)
12411 	(_("%B: ABI is incompatible with that of the selected emulation"),
12412 	 ibfd);
12413       return FALSE;
12414     }
12415 
12416   if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12417     return FALSE;
12418 
12419   new_flags = elf_elfheader (ibfd)->e_flags;
12420   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12421   old_flags = elf_elfheader (obfd)->e_flags;
12422 
12423   if (! elf_flags_init (obfd))
12424     {
12425       elf_flags_init (obfd) = TRUE;
12426       elf_elfheader (obfd)->e_flags = new_flags;
12427       elf_elfheader (obfd)->e_ident[EI_CLASS]
12428 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
12429 
12430       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
12431 	  && (bfd_get_arch_info (obfd)->the_default
12432 	      || mips_mach_extends_p (bfd_get_mach (obfd),
12433 				      bfd_get_mach (ibfd))))
12434 	{
12435 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12436 				   bfd_get_mach (ibfd)))
12437 	    return FALSE;
12438 	}
12439 
12440       return TRUE;
12441     }
12442 
12443   /* Check flag compatibility.  */
12444 
12445   new_flags &= ~EF_MIPS_NOREORDER;
12446   old_flags &= ~EF_MIPS_NOREORDER;
12447 
12448   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
12449      doesn't seem to matter.  */
12450   new_flags &= ~EF_MIPS_XGOT;
12451   old_flags &= ~EF_MIPS_XGOT;
12452 
12453   /* MIPSpro generates ucode info in n64 objects.  Again, we should
12454      just be able to ignore this.  */
12455   new_flags &= ~EF_MIPS_UCODE;
12456   old_flags &= ~EF_MIPS_UCODE;
12457 
12458   /* DSOs should only be linked with CPIC code.  */
12459   if ((ibfd->flags & DYNAMIC) != 0)
12460     new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
12461 
12462   if (new_flags == old_flags)
12463     return TRUE;
12464 
12465   /* Check to see if the input BFD actually contains any sections.
12466      If not, its flags may not have been initialised either, but it cannot
12467      actually cause any incompatibility.  */
12468   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12469     {
12470       /* Ignore synthetic sections and empty .text, .data and .bss sections
12471 	  which are automatically generated by gas.  */
12472       if (strcmp (sec->name, ".reginfo")
12473 	  && strcmp (sec->name, ".mdebug")
12474 	  && (sec->size != 0
12475 	      || (strcmp (sec->name, ".text")
12476 		  && strcmp (sec->name, ".data")
12477 		  && strcmp (sec->name, ".bss"))))
12478 	{
12479 	  null_input_bfd = FALSE;
12480 	  break;
12481 	}
12482     }
12483   if (null_input_bfd)
12484     return TRUE;
12485 
12486   ok = TRUE;
12487 
12488   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12489       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
12490     {
12491       (*_bfd_error_handler)
12492 	(_("%B: warning: linking abicalls files with non-abicalls files"),
12493 	 ibfd);
12494       ok = TRUE;
12495     }
12496 
12497   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12498     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12499   if (! (new_flags & EF_MIPS_PIC))
12500     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12501 
12502   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12503   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12504 
12505   /* Compare the ISAs.  */
12506   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
12507     {
12508       (*_bfd_error_handler)
12509 	(_("%B: linking 32-bit code with 64-bit code"),
12510 	 ibfd);
12511       ok = FALSE;
12512     }
12513   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12514     {
12515       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
12516       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
12517 	{
12518 	  /* Copy the architecture info from IBFD to OBFD.  Also copy
12519 	     the 32-bit flag (if set) so that we continue to recognise
12520 	     OBFD as a 32-bit binary.  */
12521 	  bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12522 	  elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12523 	  elf_elfheader (obfd)->e_flags
12524 	    |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12525 
12526 	  /* Copy across the ABI flags if OBFD doesn't use them
12527 	     and if that was what caused us to treat IBFD as 32-bit.  */
12528 	  if ((old_flags & EF_MIPS_ABI) == 0
12529 	      && mips_32bit_flags_p (new_flags)
12530 	      && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12531 	    elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
12532 	}
12533       else
12534 	{
12535 	  /* The ISAs aren't compatible.  */
12536 	  (*_bfd_error_handler)
12537 	    (_("%B: linking %s module with previous %s modules"),
12538 	     ibfd,
12539 	     bfd_printable_name (ibfd),
12540 	     bfd_printable_name (obfd));
12541 	  ok = FALSE;
12542 	}
12543     }
12544 
12545   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12546   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12547 
12548   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
12549      does set EI_CLASS differently from any 32-bit ABI.  */
12550   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12551       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12552 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12553     {
12554       /* Only error if both are set (to different values).  */
12555       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12556 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12557 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12558 	{
12559 	  (*_bfd_error_handler)
12560 	    (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12561 	     ibfd,
12562 	     elf_mips_abi_name (ibfd),
12563 	     elf_mips_abi_name (obfd));
12564 	  ok = FALSE;
12565 	}
12566       new_flags &= ~EF_MIPS_ABI;
12567       old_flags &= ~EF_MIPS_ABI;
12568     }
12569 
12570   /* For now, allow arbitrary mixing of ASEs (retain the union).  */
12571   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12572     {
12573       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12574 
12575       new_flags &= ~ EF_MIPS_ARCH_ASE;
12576       old_flags &= ~ EF_MIPS_ARCH_ASE;
12577     }
12578 
12579   /* Warn about any other mismatches */
12580   if (new_flags != old_flags)
12581     {
12582       (*_bfd_error_handler)
12583 	(_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12584 	 ibfd, (unsigned long) new_flags,
12585 	 (unsigned long) old_flags);
12586       ok = FALSE;
12587     }
12588 
12589   if (! ok)
12590     {
12591       bfd_set_error (bfd_error_bad_value);
12592       return FALSE;
12593     }
12594 
12595   return TRUE;
12596 }
12597 
12598 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
12599 
12600 bfd_boolean
12601 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
12602 {
12603   BFD_ASSERT (!elf_flags_init (abfd)
12604 	      || elf_elfheader (abfd)->e_flags == flags);
12605 
12606   elf_elfheader (abfd)->e_flags = flags;
12607   elf_flags_init (abfd) = TRUE;
12608   return TRUE;
12609 }
12610 
12611 char *
12612 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12613 {
12614   switch (dtag)
12615     {
12616     default: return "";
12617     case DT_MIPS_RLD_VERSION:
12618       return "MIPS_RLD_VERSION";
12619     case DT_MIPS_TIME_STAMP:
12620       return "MIPS_TIME_STAMP";
12621     case DT_MIPS_ICHECKSUM:
12622       return "MIPS_ICHECKSUM";
12623     case DT_MIPS_IVERSION:
12624       return "MIPS_IVERSION";
12625     case DT_MIPS_FLAGS:
12626       return "MIPS_FLAGS";
12627     case DT_MIPS_BASE_ADDRESS:
12628       return "MIPS_BASE_ADDRESS";
12629     case DT_MIPS_MSYM:
12630       return "MIPS_MSYM";
12631     case DT_MIPS_CONFLICT:
12632       return "MIPS_CONFLICT";
12633     case DT_MIPS_LIBLIST:
12634       return "MIPS_LIBLIST";
12635     case DT_MIPS_LOCAL_GOTNO:
12636       return "MIPS_LOCAL_GOTNO";
12637     case DT_MIPS_CONFLICTNO:
12638       return "MIPS_CONFLICTNO";
12639     case DT_MIPS_LIBLISTNO:
12640       return "MIPS_LIBLISTNO";
12641     case DT_MIPS_SYMTABNO:
12642       return "MIPS_SYMTABNO";
12643     case DT_MIPS_UNREFEXTNO:
12644       return "MIPS_UNREFEXTNO";
12645     case DT_MIPS_GOTSYM:
12646       return "MIPS_GOTSYM";
12647     case DT_MIPS_HIPAGENO:
12648       return "MIPS_HIPAGENO";
12649     case DT_MIPS_RLD_MAP:
12650       return "MIPS_RLD_MAP";
12651     case DT_MIPS_DELTA_CLASS:
12652       return "MIPS_DELTA_CLASS";
12653     case DT_MIPS_DELTA_CLASS_NO:
12654       return "MIPS_DELTA_CLASS_NO";
12655     case DT_MIPS_DELTA_INSTANCE:
12656       return "MIPS_DELTA_INSTANCE";
12657     case DT_MIPS_DELTA_INSTANCE_NO:
12658       return "MIPS_DELTA_INSTANCE_NO";
12659     case DT_MIPS_DELTA_RELOC:
12660       return "MIPS_DELTA_RELOC";
12661     case DT_MIPS_DELTA_RELOC_NO:
12662       return "MIPS_DELTA_RELOC_NO";
12663     case DT_MIPS_DELTA_SYM:
12664       return "MIPS_DELTA_SYM";
12665     case DT_MIPS_DELTA_SYM_NO:
12666       return "MIPS_DELTA_SYM_NO";
12667     case DT_MIPS_DELTA_CLASSSYM:
12668       return "MIPS_DELTA_CLASSSYM";
12669     case DT_MIPS_DELTA_CLASSSYM_NO:
12670       return "MIPS_DELTA_CLASSSYM_NO";
12671     case DT_MIPS_CXX_FLAGS:
12672       return "MIPS_CXX_FLAGS";
12673     case DT_MIPS_PIXIE_INIT:
12674       return "MIPS_PIXIE_INIT";
12675     case DT_MIPS_SYMBOL_LIB:
12676       return "MIPS_SYMBOL_LIB";
12677     case DT_MIPS_LOCALPAGE_GOTIDX:
12678       return "MIPS_LOCALPAGE_GOTIDX";
12679     case DT_MIPS_LOCAL_GOTIDX:
12680       return "MIPS_LOCAL_GOTIDX";
12681     case DT_MIPS_HIDDEN_GOTIDX:
12682       return "MIPS_HIDDEN_GOTIDX";
12683     case DT_MIPS_PROTECTED_GOTIDX:
12684       return "MIPS_PROTECTED_GOT_IDX";
12685     case DT_MIPS_OPTIONS:
12686       return "MIPS_OPTIONS";
12687     case DT_MIPS_INTERFACE:
12688       return "MIPS_INTERFACE";
12689     case DT_MIPS_DYNSTR_ALIGN:
12690       return "DT_MIPS_DYNSTR_ALIGN";
12691     case DT_MIPS_INTERFACE_SIZE:
12692       return "DT_MIPS_INTERFACE_SIZE";
12693     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12694       return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12695     case DT_MIPS_PERF_SUFFIX:
12696       return "DT_MIPS_PERF_SUFFIX";
12697     case DT_MIPS_COMPACT_SIZE:
12698       return "DT_MIPS_COMPACT_SIZE";
12699     case DT_MIPS_GP_VALUE:
12700       return "DT_MIPS_GP_VALUE";
12701     case DT_MIPS_AUX_DYNAMIC:
12702       return "DT_MIPS_AUX_DYNAMIC";
12703     case DT_MIPS_PLTGOT:
12704       return "DT_MIPS_PLTGOT";
12705     case DT_MIPS_RWPLT:
12706       return "DT_MIPS_RWPLT";
12707     }
12708 }
12709 
12710 bfd_boolean
12711 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
12712 {
12713   FILE *file = ptr;
12714 
12715   BFD_ASSERT (abfd != NULL && ptr != NULL);
12716 
12717   /* Print normal ELF private data.  */
12718   _bfd_elf_print_private_bfd_data (abfd, ptr);
12719 
12720   /* xgettext:c-format */
12721   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12722 
12723   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12724     fprintf (file, _(" [abi=O32]"));
12725   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12726     fprintf (file, _(" [abi=O64]"));
12727   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12728     fprintf (file, _(" [abi=EABI32]"));
12729   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12730     fprintf (file, _(" [abi=EABI64]"));
12731   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12732     fprintf (file, _(" [abi unknown]"));
12733   else if (ABI_N32_P (abfd))
12734     fprintf (file, _(" [abi=N32]"));
12735   else if (ABI_64_P (abfd))
12736     fprintf (file, _(" [abi=64]"));
12737   else
12738     fprintf (file, _(" [no abi set]"));
12739 
12740   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12741     fprintf (file, " [mips1]");
12742   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12743     fprintf (file, " [mips2]");
12744   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12745     fprintf (file, " [mips3]");
12746   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12747     fprintf (file, " [mips4]");
12748   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12749     fprintf (file, " [mips5]");
12750   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12751     fprintf (file, " [mips32]");
12752   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12753     fprintf (file, " [mips64]");
12754   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12755     fprintf (file, " [mips32r2]");
12756   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12757     fprintf (file, " [mips64r2]");
12758   else
12759     fprintf (file, _(" [unknown ISA]"));
12760 
12761   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12762     fprintf (file, " [mdmx]");
12763 
12764   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12765     fprintf (file, " [mips16]");
12766 
12767   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12768     fprintf (file, " [32bitmode]");
12769   else
12770     fprintf (file, _(" [not 32bitmode]"));
12771 
12772   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12773     fprintf (file, " [noreorder]");
12774 
12775   if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12776     fprintf (file, " [PIC]");
12777 
12778   if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12779     fprintf (file, " [CPIC]");
12780 
12781   if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12782     fprintf (file, " [XGOT]");
12783 
12784   if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12785     fprintf (file, " [UCODE]");
12786 
12787   fputc ('\n', file);
12788 
12789   return TRUE;
12790 }
12791 
12792 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12793 {
12794   { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12795   { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12796   { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12797   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12798   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12799   { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
12800   { NULL,                     0,  0, 0,              0 }
12801 };
12802 
12803 /* Merge non visibility st_other attributes.  Ensure that the
12804    STO_OPTIONAL flag is copied into h->other, even if this is not a
12805    definiton of the symbol.  */
12806 void
12807 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12808 				      const Elf_Internal_Sym *isym,
12809 				      bfd_boolean definition,
12810 				      bfd_boolean dynamic ATTRIBUTE_UNUSED)
12811 {
12812   if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12813     {
12814       unsigned char other;
12815 
12816       other = (definition ? isym->st_other : h->other);
12817       other &= ~ELF_ST_VISIBILITY (-1);
12818       h->other = other | ELF_ST_VISIBILITY (h->other);
12819     }
12820 
12821   if (!definition
12822       && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12823     h->other |= STO_OPTIONAL;
12824 }
12825 
12826 /* Decide whether an undefined symbol is special and can be ignored.
12827    This is the case for OPTIONAL symbols on IRIX.  */
12828 bfd_boolean
12829 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12830 {
12831   return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12832 }
12833 
12834 bfd_boolean
12835 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12836 {
12837   return (sym->st_shndx == SHN_COMMON
12838 	  || sym->st_shndx == SHN_MIPS_ACOMMON
12839 	  || sym->st_shndx == SHN_MIPS_SCOMMON);
12840 }
12841 
12842 /* Return address for Ith PLT stub in section PLT, for relocation REL
12843    or (bfd_vma) -1 if it should not be included.  */
12844 
12845 bfd_vma
12846 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12847 			   const arelent *rel ATTRIBUTE_UNUSED)
12848 {
12849   return (plt->vma
12850 	  + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12851 	  + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12852 }
12853 
12854 void
12855 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12856 {
12857   struct mips_elf_link_hash_table *htab;
12858   Elf_Internal_Ehdr *i_ehdrp;
12859 
12860   i_ehdrp = elf_elfheader (abfd);
12861   if (link_info)
12862     {
12863       htab = mips_elf_hash_table (link_info);
12864       if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12865 	i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12866     }
12867 }
12868