xref: /netbsd-src/external/gpl3/gdb/dist/bfd/elfxx-mips.c (revision e7e3f9039de8be1cf7c0ea24791a16ea85f484dc)
1 /* MIPS-specific support for ELF
2    Copyright (C) 1993-2024 Free Software Foundation, Inc.
3 
4    Most of the information added by Ian Lance Taylor, Cygnus Support,
5    <ian@cygnus.com>.
6    N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7    <mark@codesourcery.com>
8    Traditional MIPS targets support added by Koundinya.K, Dansk Data
9    Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
10 
11    This file is part of BFD, the Binary File Descriptor library.
12 
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 3 of the License, or
16    (at your option) any later version.
17 
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26    MA 02110-1301, USA.  */
27 
28 
29 /* This file handles functionality common to the different MIPS ABI's.  */
30 
31 #include "sysdep.h"
32 #include "bfd.h"
33 #include "libbfd.h"
34 #include "libiberty.h"
35 #include "elf-bfd.h"
36 #include "ecoff-bfd.h"
37 #include "elfxx-mips.h"
38 #include "elf/mips.h"
39 #include "elf-vxworks.h"
40 #include "dwarf2.h"
41 
42 /* Get the ECOFF swapping routines.  */
43 #include "coff/sym.h"
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
47 
48 #include "hashtab.h"
49 
50 /* Types of TLS GOT entry.  */
51 enum mips_got_tls_type {
52   GOT_TLS_NONE,
53   GOT_TLS_GD,
54   GOT_TLS_LDM,
55   GOT_TLS_IE
56 };
57 
58 /* This structure is used to hold information about one GOT entry.
59    There are four types of entry:
60 
61       (1) an absolute address
62 	    requires: abfd == NULL
63 	    fields: d.address
64 
65       (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66 	    requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67 	    fields: abfd, symndx, d.addend, tls_type
68 
69       (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70 	    requires: abfd != NULL, symndx == -1
71 	    fields: d.h, tls_type
72 
73       (4) a TLS LDM slot
74 	    requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75 	    fields: none; there's only one of these per GOT.  */
76 struct mips_got_entry
77 {
78   /* One input bfd that needs the GOT entry.  */
79   bfd *abfd;
80   /* The index of the symbol, as stored in the relocation r_info, if
81      we have a local symbol; -1 otherwise.  */
82   long symndx;
83   union
84   {
85     /* If abfd == NULL, an address that must be stored in the got.  */
86     bfd_vma address;
87     /* If abfd != NULL && symndx != -1, the addend of the relocation
88        that should be added to the symbol value.  */
89     bfd_vma addend;
90     /* If abfd != NULL && symndx == -1, the hash table entry
91        corresponding to a symbol in the GOT.  The symbol's entry
92        is in the local area if h->global_got_area is GGA_NONE,
93        otherwise it is in the global area.  */
94     struct mips_elf_link_hash_entry *h;
95   } d;
96 
97   /* The TLS type of this GOT entry.  An LDM GOT entry will be a local
98      symbol entry with r_symndx == 0.  */
99   unsigned char tls_type;
100 
101   /* True if we have filled in the GOT contents for a TLS entry,
102      and created the associated relocations.  */
103   unsigned char tls_initialized;
104 
105   /* The offset from the beginning of the .got section to the entry
106      corresponding to this symbol+addend.  If it's a global symbol
107      whose offset is yet to be decided, it's going to be -1.  */
108   long gotidx;
109 };
110 
111 /* This structure represents a GOT page reference from an input bfd.
112    Each instance represents a symbol + ADDEND, where the representation
113    of the symbol depends on whether it is local to the input bfd.
114    If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115    Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
116 
117    Page references with SYMNDX >= 0 always become page references
118    in the output.  Page references with SYMNDX < 0 only become page
119    references if the symbol binds locally; in other cases, the page
120    reference decays to a global GOT reference.  */
121 struct mips_got_page_ref
122 {
123   long symndx;
124   union
125   {
126     struct mips_elf_link_hash_entry *h;
127     bfd *abfd;
128   } u;
129   bfd_vma addend;
130 };
131 
132 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133    The structures form a non-overlapping list that is sorted by increasing
134    MIN_ADDEND.  */
135 struct mips_got_page_range
136 {
137   struct mips_got_page_range *next;
138   bfd_signed_vma min_addend;
139   bfd_signed_vma max_addend;
140 };
141 
142 /* This structure describes the range of addends that are applied to page
143    relocations against a given section.  */
144 struct mips_got_page_entry
145 {
146   /* The section that these entries are based on.  */
147   asection *sec;
148   /* The ranges for this page entry.  */
149   struct mips_got_page_range *ranges;
150   /* The maximum number of page entries needed for RANGES.  */
151   bfd_vma num_pages;
152 };
153 
154 /* This structure is used to hold .got information when linking.  */
155 
156 struct mips_got_info
157 {
158   /* The number of global .got entries.  */
159   unsigned int global_gotno;
160   /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
161   unsigned int reloc_only_gotno;
162   /* The number of .got slots used for TLS.  */
163   unsigned int tls_gotno;
164   /* The first unused TLS .got entry.  Used only during
165      mips_elf_initialize_tls_index.  */
166   unsigned int tls_assigned_gotno;
167   /* The number of local .got entries, eventually including page entries.  */
168   unsigned int local_gotno;
169   /* The maximum number of page entries needed.  */
170   unsigned int page_gotno;
171   /* The number of relocations needed for the GOT entries.  */
172   unsigned int relocs;
173   /* The first unused local .got entry.  */
174   unsigned int assigned_low_gotno;
175   /* The last unused local .got entry.  */
176   unsigned int assigned_high_gotno;
177   /* A hash table holding members of the got.  */
178   struct htab *got_entries;
179   /* A hash table holding mips_got_page_ref structures.  */
180   struct htab *got_page_refs;
181   /* A hash table of mips_got_page_entry structures.  */
182   struct htab *got_page_entries;
183   /* In multi-got links, a pointer to the next got (err, rather, most
184      of the time, it points to the previous got).  */
185   struct mips_got_info *next;
186 };
187 
188 /* Structure passed when merging bfds' gots.  */
189 
190 struct mips_elf_got_per_bfd_arg
191 {
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 /* A structure used to pass information to htab_traverse callbacks
216    when laying out the GOT.  */
217 
218 struct mips_elf_traverse_got_arg
219 {
220   struct bfd_link_info *info;
221   struct mips_got_info *g;
222   int value;
223 };
224 
225 struct _mips_elf_section_data
226 {
227   struct bfd_elf_section_data elf;
228   union
229   {
230     bfd_byte *tdata;
231   } u;
232 };
233 
234 #define mips_elf_section_data(sec) \
235   ((struct _mips_elf_section_data *) elf_section_data (sec))
236 
237 #define is_mips_elf(bfd)				\
238   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
239    && elf_tdata (bfd) != NULL				\
240    && elf_object_id (bfd) == MIPS_ELF_DATA)
241 
242 /* The ABI says that every symbol used by dynamic relocations must have
243    a global GOT entry.  Among other things, this provides the dynamic
244    linker with a free, directly-indexed cache.  The GOT can therefore
245    contain symbols that are not referenced by GOT relocations themselves
246    (in other words, it may have symbols that are not referenced by things
247    like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
248 
249    GOT relocations are less likely to overflow if we put the associated
250    GOT entries towards the beginning.  We therefore divide the global
251    GOT entries into two areas: "normal" and "reloc-only".  Entries in
252    the first area can be used for both dynamic relocations and GP-relative
253    accesses, while those in the "reloc-only" area are for dynamic
254    relocations only.
255 
256    These GGA_* ("Global GOT Area") values are organised so that lower
257    values are more general than higher values.  Also, non-GGA_NONE
258    values are ordered by the position of the area in the GOT.  */
259 #define GGA_NORMAL 0
260 #define GGA_RELOC_ONLY 1
261 #define GGA_NONE 2
262 
263 /* Information about a non-PIC interface to a PIC function.  There are
264    two ways of creating these interfaces.  The first is to add:
265 
266 	lui	$25,%hi(func)
267 	addiu	$25,$25,%lo(func)
268 
269    immediately before a PIC function "func".  The second is to add:
270 
271 	lui	$25,%hi(func)
272 	j	func
273 	addiu	$25,$25,%lo(func)
274 
275    to a separate trampoline section.
276 
277    Stubs of the first kind go in a new section immediately before the
278    target function.  Stubs of the second kind go in a single section
279    pointed to by the hash table's "strampoline" field.  */
280 struct mips_elf_la25_stub {
281   /* The generated section that contains this stub.  */
282   asection *stub_section;
283 
284   /* The offset of the stub from the start of STUB_SECTION.  */
285   bfd_vma offset;
286 
287   /* One symbol for the original function.  Its location is available
288      in H->root.root.u.def.  */
289   struct mips_elf_link_hash_entry *h;
290 };
291 
292 /* Macros for populating a mips_elf_la25_stub.  */
293 
294 #define LA25_LUI(VAL) (0x3c190000 | (VAL))	/* lui t9,VAL */
295 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
296 #define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
297 #define LA25_ADDIU(VAL) (0x27390000 | (VAL))	/* addiu t9,t9,VAL */
298 #define LA25_LUI_MICROMIPS(VAL)						\
299   (0x41b90000 | (VAL))				/* lui t9,VAL */
300 #define LA25_J_MICROMIPS(VAL)						\
301   (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))	/* j VAL */
302 #define LA25_ADDIU_MICROMIPS(VAL)					\
303   (0x33390000 | (VAL))				/* addiu t9,t9,VAL */
304 
305 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
306    the dynamic symbols.  */
307 
308 struct mips_elf_hash_sort_data
309 {
310   /* The symbol in the global GOT with the lowest dynamic symbol table
311      index.  */
312   struct elf_link_hash_entry *low;
313   /* The least dynamic symbol table index corresponding to a non-TLS
314      symbol with a GOT entry.  */
315   bfd_size_type min_got_dynindx;
316   /* The greatest dynamic symbol table index corresponding to a symbol
317      with a GOT entry that is not referenced (e.g., a dynamic symbol
318      with dynamic relocations pointing to it from non-primary GOTs).  */
319   bfd_size_type max_unref_got_dynindx;
320   /* The greatest dynamic symbol table index corresponding to a local
321      symbol.  */
322   bfd_size_type max_local_dynindx;
323   /* The greatest dynamic symbol table index corresponding to an external
324      symbol without a GOT entry.  */
325   bfd_size_type max_non_got_dynindx;
326   /* If non-NULL, output BFD for .MIPS.xhash finalization.  */
327   bfd *output_bfd;
328   /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329      real final dynindx.  */
330   bfd_byte *mipsxhash;
331 };
332 
333 /* We make up to two PLT entries if needed, one for standard MIPS code
334    and one for compressed code, either a MIPS16 or microMIPS one.  We
335    keep a separate record of traditional lazy-binding stubs, for easier
336    processing.  */
337 
338 struct plt_entry
339 {
340   /* Traditional SVR4 stub offset, or -1 if none.  */
341   bfd_vma stub_offset;
342 
343   /* Standard PLT entry offset, or -1 if none.  */
344   bfd_vma mips_offset;
345 
346   /* Compressed PLT entry offset, or -1 if none.  */
347   bfd_vma comp_offset;
348 
349   /* The corresponding .got.plt index, or -1 if none.  */
350   bfd_vma gotplt_index;
351 
352   /* Whether we need a standard PLT entry.  */
353   unsigned int need_mips : 1;
354 
355   /* Whether we need a compressed PLT entry.  */
356   unsigned int need_comp : 1;
357 };
358 
359 /* The MIPS ELF linker needs additional information for each symbol in
360    the global hash table.  */
361 
362 struct mips_elf_link_hash_entry
363 {
364   struct elf_link_hash_entry root;
365 
366   /* External symbol information.  */
367   EXTR esym;
368 
369   /* The la25 stub we have created for ths symbol, if any.  */
370   struct mips_elf_la25_stub *la25_stub;
371 
372   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373      this symbol.  */
374   unsigned int possibly_dynamic_relocs;
375 
376   /* If there is a stub that 32 bit functions should use to call this
377      16 bit function, this points to the section containing the stub.  */
378   asection *fn_stub;
379 
380   /* If there is a stub that 16 bit functions should use to call this
381      32 bit function, this points to the section containing the stub.  */
382   asection *call_stub;
383 
384   /* This is like the call_stub field, but it is used if the function
385      being called returns a floating point value.  */
386   asection *call_fp_stub;
387 
388   /* If non-zero, location in .MIPS.xhash to write real final dynindx.  */
389   bfd_vma mipsxhash_loc;
390 
391   /* The highest GGA_* value that satisfies all references to this symbol.  */
392   unsigned int global_got_area : 2;
393 
394   /* True if all GOT relocations against this symbol are for calls.  This is
395      a looser condition than no_fn_stub below, because there may be other
396      non-call non-GOT relocations against the symbol.  */
397   unsigned int got_only_for_calls : 1;
398 
399   /* True if one of the relocations described by possibly_dynamic_relocs
400      is against a readonly section.  */
401   unsigned int readonly_reloc : 1;
402 
403   /* True if there is a relocation against this symbol that must be
404      resolved by the static linker (in other words, if the relocation
405      cannot possibly be made dynamic).  */
406   unsigned int has_static_relocs : 1;
407 
408   /* True if we must not create a .MIPS.stubs entry for this symbol.
409      This is set, for example, if there are relocations related to
410      taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411      See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
412   unsigned int no_fn_stub : 1;
413 
414   /* Whether we need the fn_stub; this is true if this symbol appears
415      in any relocs other than a 16 bit call.  */
416   unsigned int need_fn_stub : 1;
417 
418   /* True if this symbol is referenced by branch relocations from
419      any non-PIC input file.  This is used to determine whether an
420      la25 stub is required.  */
421   unsigned int has_nonpic_branches : 1;
422 
423   /* Does this symbol need a traditional MIPS lazy-binding stub
424      (as opposed to a PLT entry)?  */
425   unsigned int needs_lazy_stub : 1;
426 
427   /* Does this symbol resolve to a PLT entry?  */
428   unsigned int use_plt_entry : 1;
429 };
430 
431 /* MIPS ELF linker hash table.  */
432 
433 struct mips_elf_link_hash_table
434 {
435   struct elf_link_hash_table root;
436 
437   /* The number of .rtproc entries.  */
438   bfd_size_type procedure_count;
439 
440   /* The size of the .compact_rel section (if SGI_COMPAT).  */
441   bfd_size_type compact_rel_size;
442 
443   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444      is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
445   bool use_rld_obj_head;
446 
447   /* The  __rld_map or __rld_obj_head symbol. */
448   struct elf_link_hash_entry *rld_symbol;
449 
450   /* This is set if we see any mips16 stub sections.  */
451   bool mips16_stubs_seen;
452 
453   /* True if we can generate copy relocs and PLTs.  */
454   bool use_plts_and_copy_relocs;
455 
456   /* True if we can only use 32-bit microMIPS instructions.  */
457   bool insn32;
458 
459   /* True if we suppress checks for invalid branches between ISA modes.  */
460   bool ignore_branch_isa;
461 
462   /* True if we are targetting R6 compact branches.  */
463   bool compact_branches;
464 
465   /* True if we already reported the small-data section overflow.  */
466   bool small_data_overflow_reported;
467 
468   /* True if we use the special `__gnu_absolute_zero' symbol.  */
469   bool use_absolute_zero;
470 
471   /* True if we have been configured for a GNU target.  */
472   bool gnu_target;
473 
474   /* Shortcuts to some dynamic sections, or NULL if they are not
475      being used.  */
476   asection *srelplt2;
477   asection *sstubs;
478 
479   /* The master GOT information.  */
480   struct mips_got_info *got_info;
481 
482   /* The global symbol in the GOT with the lowest index in the dynamic
483      symbol table.  */
484   struct elf_link_hash_entry *global_gotsym;
485 
486   /* The size of the PLT header in bytes.  */
487   bfd_vma plt_header_size;
488 
489   /* The size of a standard PLT entry in bytes.  */
490   bfd_vma plt_mips_entry_size;
491 
492   /* The size of a compressed PLT entry in bytes.  */
493   bfd_vma plt_comp_entry_size;
494 
495   /* The offset of the next standard PLT entry to create.  */
496   bfd_vma plt_mips_offset;
497 
498   /* The offset of the next compressed PLT entry to create.  */
499   bfd_vma plt_comp_offset;
500 
501   /* The index of the next .got.plt entry to create.  */
502   bfd_vma plt_got_index;
503 
504   /* The number of functions that need a lazy-binding stub.  */
505   bfd_vma lazy_stub_count;
506 
507   /* The size of a function stub entry in bytes.  */
508   bfd_vma function_stub_size;
509 
510   /* The number of reserved entries at the beginning of the GOT.  */
511   unsigned int reserved_gotno;
512 
513   /* The section used for mips_elf_la25_stub trampolines.
514      See the comment above that structure for details.  */
515   asection *strampoline;
516 
517   /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518      pairs.  */
519   htab_t la25_stubs;
520 
521   /* A function FN (NAME, IS, OS) that creates a new input section
522      called NAME and links it to output section OS.  If IS is nonnull,
523      the new section should go immediately before it, otherwise it
524      should go at the (current) beginning of OS.
525 
526      The function returns the new section on success, otherwise it
527      returns null.  */
528   asection *(*add_stub_section) (const char *, asection *, asection *);
529 
530   /* Is the PLT header compressed?  */
531   unsigned int plt_header_is_comp : 1;
532 };
533 
534 /* Get the MIPS ELF linker hash table from a link_info structure.  */
535 
536 #define mips_elf_hash_table(p) \
537   ((is_elf_hash_table ((p)->hash)					\
538     && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA)		\
539    ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
540 
541 /* A structure used to communicate with htab_traverse callbacks.  */
542 struct mips_htab_traverse_info
543 {
544   /* The usual link-wide information.  */
545   struct bfd_link_info *info;
546   bfd *output_bfd;
547 
548   /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
549   bool error;
550 };
551 
552 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
554    that contains the relocation field and DATA points to the start of
555    INPUT_SECTION.  */
556 
557 struct mips_hi16
558 {
559   struct mips_hi16 *next;
560   bfd_byte *data;
561   asection *input_section;
562   arelent rel;
563 };
564 
565 /* MIPS ELF private object data.  */
566 
567 struct mips_elf_obj_tdata
568 {
569   /* Generic ELF private object data.  */
570   struct elf_obj_tdata root;
571 
572   /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
573   bfd *abi_fp_bfd;
574 
575   /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
576   bfd *abi_msa_bfd;
577 
578   /* The abiflags for this object.  */
579   Elf_Internal_ABIFlags_v0 abiflags;
580   bool abiflags_valid;
581 
582   /* The GOT requirements of input bfds.  */
583   struct mips_got_info *got;
584 
585   /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
586      included directly in this one, but there's no point to wasting
587      the memory just for the infrequently called find_nearest_line.  */
588   struct mips_elf_find_line *find_line_info;
589 
590   /* An array of stub sections indexed by symbol number.  */
591   asection **local_stubs;
592   asection **local_call_stubs;
593 
594   /* The Irix 5 support uses two virtual sections, which represent
595      text/data symbols defined in dynamic objects.  */
596   asymbol *elf_data_symbol;
597   asymbol *elf_text_symbol;
598   asection *elf_data_section;
599   asection *elf_text_section;
600 
601   struct mips_hi16 *mips_hi16_list;
602 };
603 
604 /* Get MIPS ELF private object data from BFD's tdata.  */
605 
606 #define mips_elf_tdata(bfd) \
607   ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
608 
609 #define TLS_RELOC_P(r_type) \
610   (r_type == R_MIPS_TLS_DTPMOD32		\
611    || r_type == R_MIPS_TLS_DTPMOD64		\
612    || r_type == R_MIPS_TLS_DTPREL32		\
613    || r_type == R_MIPS_TLS_DTPREL64		\
614    || r_type == R_MIPS_TLS_GD			\
615    || r_type == R_MIPS_TLS_LDM			\
616    || r_type == R_MIPS_TLS_DTPREL_HI16		\
617    || r_type == R_MIPS_TLS_DTPREL_LO16		\
618    || r_type == R_MIPS_TLS_GOTTPREL		\
619    || r_type == R_MIPS_TLS_TPREL32		\
620    || r_type == R_MIPS_TLS_TPREL64		\
621    || r_type == R_MIPS_TLS_TPREL_HI16		\
622    || r_type == R_MIPS_TLS_TPREL_LO16		\
623    || r_type == R_MIPS16_TLS_GD			\
624    || r_type == R_MIPS16_TLS_LDM		\
625    || r_type == R_MIPS16_TLS_DTPREL_HI16	\
626    || r_type == R_MIPS16_TLS_DTPREL_LO16	\
627    || r_type == R_MIPS16_TLS_GOTTPREL		\
628    || r_type == R_MIPS16_TLS_TPREL_HI16		\
629    || r_type == R_MIPS16_TLS_TPREL_LO16		\
630    || r_type == R_MICROMIPS_TLS_GD		\
631    || r_type == R_MICROMIPS_TLS_LDM		\
632    || r_type == R_MICROMIPS_TLS_DTPREL_HI16	\
633    || r_type == R_MICROMIPS_TLS_DTPREL_LO16	\
634    || r_type == R_MICROMIPS_TLS_GOTTPREL	\
635    || r_type == R_MICROMIPS_TLS_TPREL_HI16	\
636    || r_type == R_MICROMIPS_TLS_TPREL_LO16)
637 
638 /* Structure used to pass information to mips_elf_output_extsym.  */
639 
640 struct extsym_info
641 {
642   bfd *abfd;
643   struct bfd_link_info *info;
644   struct ecoff_debug_info *debug;
645   const struct ecoff_debug_swap *swap;
646   bool failed;
647 };
648 
649 /* The names of the runtime procedure table symbols used on IRIX5.  */
650 
651 static const char * const mips_elf_dynsym_rtproc_names[] =
652 {
653   "_procedure_table",
654   "_procedure_string_table",
655   "_procedure_table_size",
656   NULL
657 };
658 
659 /* These structures are used to generate the .compact_rel section on
660    IRIX5.  */
661 
662 typedef struct
663 {
664   unsigned long id1;		/* Always one?  */
665   unsigned long num;		/* Number of compact relocation entries.  */
666   unsigned long id2;		/* Always two?  */
667   unsigned long offset;		/* The file offset of the first relocation.  */
668   unsigned long reserved0;	/* Zero?  */
669   unsigned long reserved1;	/* Zero?  */
670 } Elf32_compact_rel;
671 
672 typedef struct
673 {
674   bfd_byte id1[4];
675   bfd_byte num[4];
676   bfd_byte id2[4];
677   bfd_byte offset[4];
678   bfd_byte reserved0[4];
679   bfd_byte reserved1[4];
680 } Elf32_External_compact_rel;
681 
682 typedef struct
683 {
684   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
685   unsigned int rtype : 4;	/* Relocation types. See below.  */
686   unsigned int dist2to : 8;
687   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
688   unsigned long konst;		/* KONST field. See below.  */
689   unsigned long vaddr;		/* VADDR to be relocated.  */
690 } Elf32_crinfo;
691 
692 typedef struct
693 {
694   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
695   unsigned int rtype : 4;	/* Relocation types. See below.  */
696   unsigned int dist2to : 8;
697   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
698   unsigned long konst;		/* KONST field. See below.  */
699 } Elf32_crinfo2;
700 
701 typedef struct
702 {
703   bfd_byte info[4];
704   bfd_byte konst[4];
705   bfd_byte vaddr[4];
706 } Elf32_External_crinfo;
707 
708 typedef struct
709 {
710   bfd_byte info[4];
711   bfd_byte konst[4];
712 } Elf32_External_crinfo2;
713 
714 /* These are the constants used to swap the bitfields in a crinfo.  */
715 
716 #define CRINFO_CTYPE (0x1U)
717 #define CRINFO_CTYPE_SH (31)
718 #define CRINFO_RTYPE (0xfU)
719 #define CRINFO_RTYPE_SH (27)
720 #define CRINFO_DIST2TO (0xffU)
721 #define CRINFO_DIST2TO_SH (19)
722 #define CRINFO_RELVADDR (0x7ffffU)
723 #define CRINFO_RELVADDR_SH (0)
724 
725 /* A compact relocation info has long (3 words) or short (2 words)
726    formats.  A short format doesn't have VADDR field and relvaddr
727    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
728 #define CRF_MIPS_LONG			1
729 #define CRF_MIPS_SHORT			0
730 
731 /* There are 4 types of compact relocation at least. The value KONST
732    has different meaning for each type:
733 
734    (type)		(konst)
735    CT_MIPS_REL32	Address in data
736    CT_MIPS_WORD		Address in word (XXX)
737    CT_MIPS_GPHI_LO	GP - vaddr
738    CT_MIPS_JMPAD	Address to jump
739    */
740 
741 #define CRT_MIPS_REL32			0xa
742 #define CRT_MIPS_WORD			0xb
743 #define CRT_MIPS_GPHI_LO		0xc
744 #define CRT_MIPS_JMPAD			0xd
745 
746 #define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
747 #define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
748 #define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
749 #define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
750 
751 /* The structure of the runtime procedure descriptor created by the
752    loader for use by the static exception system.  */
753 
754 typedef struct runtime_pdr {
755 	bfd_vma	adr;		/* Memory address of start of procedure.  */
756 	long	regmask;	/* Save register mask.  */
757 	long	regoffset;	/* Save register offset.  */
758 	long	fregmask;	/* Save floating point register mask.  */
759 	long	fregoffset;	/* Save floating point register offset.  */
760 	long	frameoffset;	/* Frame size.  */
761 	short	framereg;	/* Frame pointer register.  */
762 	short	pcreg;		/* Offset or reg of return pc.  */
763 	long	irpss;		/* Index into the runtime string table.  */
764 	long	reserved;
765 	struct exception_info *exception_info;/* Pointer to exception array.  */
766 } RPDR, *pRPDR;
767 #define cbRPDR sizeof (RPDR)
768 #define rpdNil ((pRPDR) 0)
769 
770 static struct mips_got_entry *mips_elf_create_local_got_entry
771   (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772    struct mips_elf_link_hash_entry *, int);
773 static bool mips_elf_sort_hash_table_f
774   (struct mips_elf_link_hash_entry *, void *);
775 static bfd_vma mips_elf_high
776   (bfd_vma);
777 static bool mips_elf_create_dynamic_relocation
778   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780    bfd_vma *, asection *);
781 static bfd_vma mips_elf_adjust_gp
782   (bfd *, struct mips_got_info *, bfd *);
783 
784 /* This will be used when we sort the dynamic relocation records.  */
785 static bfd *reldyn_sorting_bfd;
786 
787 /* True if ABFD is for CPUs with load interlocking that include
788    non-MIPS1 CPUs and R3900.  */
789 #define LOAD_INTERLOCKS_P(abfd) \
790   (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
791    || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
792 
793 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794    This should be safe for all architectures.  We enable this predicate
795    for RM9000 for now.  */
796 #define JAL_TO_BAL_P(abfd) \
797   ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
798 
799 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800    This should be safe for all architectures.  We enable this predicate for
801    all CPUs.  */
802 #define JALR_TO_BAL_P(abfd) 1
803 
804 /* True if ABFD is for CPUs that are faster if JR is converted to B.
805    This should be safe for all architectures.  We enable this predicate for
806    all CPUs.  */
807 #define JR_TO_B_P(abfd) 1
808 
809 /* True if ABFD is a PIC object.  */
810 #define PIC_OBJECT_P(abfd) \
811   ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
812 
813 /* Nonzero if ABFD is using the O32 ABI.  */
814 #define ABI_O32_P(abfd) \
815   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
816 
817 /* Nonzero if ABFD is using the N32 ABI.  */
818 #define ABI_N32_P(abfd) \
819   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
820 
821 /* Nonzero if ABFD is using the N64 ABI.  */
822 #define ABI_64_P(abfd) \
823   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
824 
825 /* Nonzero if ABFD is using NewABI conventions.  */
826 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
827 
828 /* Nonzero if ABFD has microMIPS code.  */
829 #define MICROMIPS_P(abfd) \
830   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
831 
832 /* Nonzero if ABFD is MIPS R6.  */
833 #define MIPSR6_P(abfd) \
834   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
835     || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
836 
837 /* The IRIX compatibility level we are striving for.  */
838 #define IRIX_COMPAT(abfd) \
839   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
840 
841 /* Whether we are trying to be compatible with IRIX at all.  */
842 #define SGI_COMPAT(abfd) \
843   (IRIX_COMPAT (abfd) != ict_none)
844 
845 /* The name of the options section.  */
846 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
847   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
848 
849 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
851 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
853 
854 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
855 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856   (strcmp (NAME, ".MIPS.abiflags") == 0)
857 
858 /* Whether the section is readonly.  */
859 #define MIPS_ELF_READONLY_SECTION(sec) \
860   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
861    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
862 
863 /* The name of the stub section.  */
864 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
865 
866 /* The size of an external REL relocation.  */
867 #define MIPS_ELF_REL_SIZE(abfd) \
868   (get_elf_backend_data (abfd)->s->sizeof_rel)
869 
870 /* The size of an external RELA relocation.  */
871 #define MIPS_ELF_RELA_SIZE(abfd) \
872   (get_elf_backend_data (abfd)->s->sizeof_rela)
873 
874 /* The size of an external dynamic table entry.  */
875 #define MIPS_ELF_DYN_SIZE(abfd) \
876   (get_elf_backend_data (abfd)->s->sizeof_dyn)
877 
878 /* The size of a GOT entry.  */
879 #define MIPS_ELF_GOT_SIZE(abfd) \
880   (get_elf_backend_data (abfd)->s->arch_size / 8)
881 
882 /* The size of the .rld_map section. */
883 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884   (get_elf_backend_data (abfd)->s->arch_size / 8)
885 
886 /* The size of a symbol-table entry.  */
887 #define MIPS_ELF_SYM_SIZE(abfd) \
888   (get_elf_backend_data (abfd)->s->sizeof_sym)
889 
890 /* The default alignment for sections, as a power of two.  */
891 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
892   (get_elf_backend_data (abfd)->s->log_file_align)
893 
894 /* Get word-sized data.  */
895 #define MIPS_ELF_GET_WORD(abfd, ptr) \
896   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
897 
898 /* Put out word-sized data.  */
899 #define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
900   (ABI_64_P (abfd)				\
901    ? bfd_put_64 (abfd, val, ptr)		\
902    : bfd_put_32 (abfd, val, ptr))
903 
904 /* The opcode for word-sized loads (LW or LD).  */
905 #define MIPS_ELF_LOAD_WORD(abfd) \
906   (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
907 
908 /* Add a dynamic symbol table-entry.  */
909 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
910   _bfd_elf_add_dynamic_entry (info, tag, val)
911 
912 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
913   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
914 
915 /* The name of the dynamic relocation section.  */
916 #define MIPS_ELF_REL_DYN_NAME(INFO) \
917   (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918    ? ".rela.dyn" : ".rel.dyn")
919 
920 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921    from smaller values.  Start with zero, widen, *then* decrement.  */
922 #define MINUS_ONE	(((bfd_vma)0) - 1)
923 #define MINUS_TWO	(((bfd_vma)0) - 2)
924 
925 /* The value to write into got[1] for SVR4 targets, to identify it is
926    a GNU object.  The dynamic linker can then use got[1] to store the
927    module pointer.  */
928 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929   ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
930 
931 /* The offset of $gp from the beginning of the .got section.  */
932 #define ELF_MIPS_GP_OFFSET(INFO) \
933   (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934    ? 0x0 : 0x7ff0)
935 
936 /* The maximum size of the GOT for it to be addressable using 16-bit
937    offsets from $gp.  */
938 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
939 
940 /* Instructions which appear in a stub.  */
941 #define STUB_LW(abfd)							\
942   ((ABI_64_P (abfd)							\
943     ? 0xdf998010				/* ld t9,0x8010(gp) */	\
944     : 0x8f998010))				/* lw t9,0x8010(gp) */
945 #define STUB_MOVE 0x03e07825			/* or t7,ra,zero */
946 #define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
947 #define STUB_JALR 0x0320f809			/* jalr ra,t9 */
948 #define STUB_JALRC 0xf8190000			/* jalrc ra,t9 */
949 #define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
950 #define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
951 #define STUB_LI16S(abfd, VAL)						\
952    ((ABI_64_P (abfd)							\
953     ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
954     : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
955 
956 /* Likewise for the microMIPS ASE.  */
957 #define STUB_LW_MICROMIPS(abfd)						\
958   (ABI_64_P (abfd)							\
959    ? 0xdf3c8010					/* ld t9,0x8010(gp) */	\
960    : 0xff3c8010)				/* lw t9,0x8010(gp) */
961 #define STUB_MOVE_MICROMIPS 0x0dff		/* move t7,ra */
962 #define STUB_MOVE32_MICROMIPS 0x001f7a90	/* or t7,ra,zero */
963 #define STUB_LUI_MICROMIPS(VAL)						\
964    (0x41b80000 + (VAL))				/* lui t8,VAL */
965 #define STUB_JALR_MICROMIPS 0x45d9		/* jalr t9 */
966 #define STUB_JALR32_MICROMIPS 0x03f90f3c	/* jalr ra,t9 */
967 #define STUB_ORI_MICROMIPS(VAL)						\
968   (0x53180000 + (VAL))				/* ori t8,t8,VAL */
969 #define STUB_LI16U_MICROMIPS(VAL)					\
970   (0x53000000 + (VAL))				/* ori t8,zero,VAL unsigned */
971 #define STUB_LI16S_MICROMIPS(abfd, VAL)					\
972    (ABI_64_P (abfd)							\
973     ? 0x5f000000 + (VAL)	/* daddiu t8,zero,VAL sign extended */	\
974     : 0x33000000 + (VAL))	/* addiu t8,zero,VAL sign extended */
975 
976 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
978 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
980 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
982 
983 /* The name of the dynamic interpreter.  This is put in the .interp
984    section.  */
985 
986 #define ELF_DYNAMIC_INTERPRETER(abfd)		\
987    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"	\
988     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"	\
989     : "/usr/lib/libc.so.1")
990 
991 #ifdef BFD64
992 #define MNAME(bfd,pre,pos) \
993   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
994 #define ELF_R_SYM(bfd, i)					\
995   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996 #define ELF_R_TYPE(bfd, i)					\
997   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998 #define ELF_R_INFO(bfd, s, t)					\
999   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000 #else
1001 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1002 #define ELF_R_SYM(bfd, i)					\
1003   (ELF32_R_SYM (i))
1004 #define ELF_R_TYPE(bfd, i)					\
1005   (ELF32_R_TYPE (i))
1006 #define ELF_R_INFO(bfd, s, t)					\
1007   (ELF32_R_INFO (s, t))
1008 #endif
1009 
1010   /* The mips16 compiler uses a couple of special sections to handle
1011      floating point arguments.
1012 
1013      Section names that look like .mips16.fn.FNNAME contain stubs that
1014      copy floating point arguments from the fp regs to the gp regs and
1015      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1016      call should be redirected to the stub instead.  If no 32 bit
1017      function calls FNNAME, the stub should be discarded.  We need to
1018      consider any reference to the function, not just a call, because
1019      if the address of the function is taken we will need the stub,
1020      since the address might be passed to a 32 bit function.
1021 
1022      Section names that look like .mips16.call.FNNAME contain stubs
1023      that copy floating point arguments from the gp regs to the fp
1024      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1025      then any 16 bit function that calls FNNAME should be redirected
1026      to the stub instead.  If FNNAME is not a 32 bit function, the
1027      stub should be discarded.
1028 
1029      .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030      which call FNNAME and then copy the return value from the fp regs
1031      to the gp regs.  These stubs store the return value in $18 while
1032      calling FNNAME; any function which might call one of these stubs
1033      must arrange to save $18 around the call.  (This case is not
1034      needed for 32 bit functions that call 16 bit functions, because
1035      16 bit functions always return floating point values in both
1036      $f0/$f1 and $2/$3.)
1037 
1038      Note that in all cases FNNAME might be defined statically.
1039      Therefore, FNNAME is not used literally.  Instead, the relocation
1040      information will indicate which symbol the section is for.
1041 
1042      We record any stubs that we find in the symbol table.  */
1043 
1044 #define FN_STUB ".mips16.fn."
1045 #define CALL_STUB ".mips16.call."
1046 #define CALL_FP_STUB ".mips16.call.fp."
1047 
1048 #define FN_STUB_P(name) startswith (name, FN_STUB)
1049 #define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050 #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1051 
1052 /* The format of the first PLT entry in an O32 executable.  */
1053 static const bfd_vma mips_o32_exec_plt0_entry[] =
1054 {
1055   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
1056   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
1057   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
1058   0x031cc023,	/* subu $24, $24, $28					*/
1059   0x03e07825,	/* or t7, ra, zero					*/
1060   0x0018c082,	/* srl $24, $24, 2					*/
1061   0x0320f809,	/* jalr $25						*/
1062   0x2718fffe	/* subu $24, $24, 2					*/
1063 };
1064 
1065 /* The format of the first PLT entry in an O32 executable using compact
1066    jumps.  */
1067 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1068 {
1069   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
1070   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
1071   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
1072   0x031cc023,	/* subu $24, $24, $28					*/
1073   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
1074   0x0018c082,	/* srl $24, $24, 2					*/
1075   0x2718fffe,	/* subu $24, $24, 2					*/
1076   0xf8190000	/* jalrc $25						*/
1077 };
1078 
1079 /* The format of the first PLT entry in an N32 executable.  Different
1080    because gp ($28) is not available; we use t2 ($14) instead.  */
1081 static const bfd_vma mips_n32_exec_plt0_entry[] =
1082 {
1083   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
1084   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
1085   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
1086   0x030ec023,	/* subu $24, $24, $14					*/
1087   0x03e07825,	/* or t7, ra, zero					*/
1088   0x0018c082,	/* srl $24, $24, 2					*/
1089   0x0320f809,	/* jalr $25						*/
1090   0x2718fffe	/* subu $24, $24, 2					*/
1091 };
1092 
1093 /* The format of the first PLT entry in an N32 executable using compact
1094    jumps.  Different because gp ($28) is not available; we use t2 ($14)
1095    instead.  */
1096 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1097 {
1098   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
1099   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
1100   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
1101   0x030ec023,	/* subu $24, $24, $14					*/
1102   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
1103   0x0018c082,	/* srl $24, $24, 2					*/
1104   0x2718fffe,	/* subu $24, $24, 2					*/
1105   0xf8190000	/* jalrc $25						*/
1106 };
1107 
1108 /* The format of the first PLT entry in an N64 executable.  Different
1109    from N32 because of the increased size of GOT entries.  */
1110 static const bfd_vma mips_n64_exec_plt0_entry[] =
1111 {
1112   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
1113   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
1114   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
1115   0x030ec023,	/* subu $24, $24, $14					*/
1116   0x03e07825,	/* or t7, ra, zero					*/
1117   0x0018c0c2,	/* srl $24, $24, 3					*/
1118   0x0320f809,	/* jalr $25						*/
1119   0x2718fffe	/* subu $24, $24, 2					*/
1120 };
1121 
1122 /* The format of the first PLT entry in an N64 executable using compact
1123    jumps.  Different from N32 because of the increased size of GOT
1124    entries.  */
1125 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1126 {
1127   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
1128   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
1129   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
1130   0x030ec023,	/* subu $24, $24, $14					*/
1131   0x03e0782d,	/* move $15, $31	# 64-bit move (daddu)		*/
1132   0x0018c0c2,	/* srl $24, $24, 3					*/
1133   0x2718fffe,	/* subu $24, $24, 2					*/
1134   0xf8190000	/* jalrc $25						*/
1135 };
1136 
1137 
1138 /* The format of the microMIPS first PLT entry in an O32 executable.
1139    We rely on v0 ($2) rather than t8 ($24) to contain the address
1140    of the GOTPLT entry handled, so this stub may only be used when
1141    all the subsequent PLT entries are microMIPS code too.
1142 
1143    The trailing NOP is for alignment and correct disassembly only.  */
1144 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1145 {
1146   0x7980, 0x0000,	/* addiupc $3, (&GOTPLT[0]) - .			*/
1147   0xff23, 0x0000,	/* lw $25, 0($3)				*/
1148   0x0535,		/* subu $2, $2, $3				*/
1149   0x2525,		/* srl $2, $2, 2				*/
1150   0x3302, 0xfffe,	/* subu $24, $2, 2				*/
1151   0x0dff,		/* move $15, $31				*/
1152   0x45f9,		/* jalrs $25					*/
1153   0x0f83,		/* move $28, $3					*/
1154   0x0c00		/* nop						*/
1155 };
1156 
1157 /* The format of the microMIPS first PLT entry in an O32 executable
1158    in the insn32 mode.  */
1159 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1160 {
1161   0x41bc, 0x0000,	/* lui $28, %hi(&GOTPLT[0])			*/
1162   0xff3c, 0x0000,	/* lw $25, %lo(&GOTPLT[0])($28)			*/
1163   0x339c, 0x0000,	/* addiu $28, $28, %lo(&GOTPLT[0])		*/
1164   0x0398, 0xc1d0,	/* subu $24, $24, $28				*/
1165   0x001f, 0x7a90,	/* or $15, $31, zero				*/
1166   0x0318, 0x1040,	/* srl $24, $24, 2				*/
1167   0x03f9, 0x0f3c,	/* jalr $25					*/
1168   0x3318, 0xfffe	/* subu $24, $24, 2				*/
1169 };
1170 
1171 /* The format of subsequent standard PLT entries.  */
1172 static const bfd_vma mips_exec_plt_entry[] =
1173 {
1174   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
1175   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
1176   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
1177   0x03200008	/* jr $25					*/
1178 };
1179 
1180 static const bfd_vma mipsr6_exec_plt_entry[] =
1181 {
1182   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
1183   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
1184   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
1185   0x03200009	/* jr $25					*/
1186 };
1187 
1188 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1189 {
1190   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
1191   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
1192   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
1193   0xd8190000	/* jic $25, 0					*/
1194 };
1195 
1196 /* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
1197    and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198    directly addressable.  */
1199 static const bfd_vma mips16_o32_exec_plt_entry[] =
1200 {
1201   0xb203,		/* lw $2, 12($pc)			*/
1202   0x9a60,		/* lw $3, 0($2)				*/
1203   0x651a,		/* move $24, $2				*/
1204   0xeb00,		/* jr $3				*/
1205   0x653b,		/* move $25, $3				*/
1206   0x6500,		/* nop					*/
1207   0x0000, 0x0000	/* .word (.got.plt entry)		*/
1208 };
1209 
1210 /* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
1211    as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
1212 static const bfd_vma micromips_o32_exec_plt_entry[] =
1213 {
1214   0x7900, 0x0000,	/* addiupc $2, (.got.plt entry) - .	*/
1215   0xff22, 0x0000,	/* lw $25, 0($2)			*/
1216   0x4599,		/* jr $25				*/
1217   0x0f02		/* move $24, $2				*/
1218 };
1219 
1220 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
1221 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1222 {
1223   0x41af, 0x0000,	/* lui $15, %hi(.got.plt entry)		*/
1224   0xff2f, 0x0000,	/* lw $25, %lo(.got.plt entry)($15)	*/
1225   0x0019, 0x0f3c,	/* jr $25				*/
1226   0x330f, 0x0000	/* addiu $24, $15, %lo(.got.plt entry)	*/
1227 };
1228 
1229 /* The format of the first PLT entry in a VxWorks executable.  */
1230 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1231 {
1232   0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
1233   0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
1234   0x8f390008,	/* lw t9, 8(t9)					*/
1235   0x00000000,	/* nop						*/
1236   0x03200008,	/* jr t9					*/
1237   0x00000000	/* nop						*/
1238 };
1239 
1240 /* The format of subsequent PLT entries.  */
1241 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1242 {
1243   0x10000000,	/* b .PLT_resolver			*/
1244   0x24180000,	/* li t8, <pltindex>			*/
1245   0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
1246   0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
1247   0x8f390000,	/* lw t9, 0(t9)				*/
1248   0x00000000,	/* nop					*/
1249   0x03200008,	/* jr t9				*/
1250   0x00000000	/* nop					*/
1251 };
1252 
1253 /* The format of the first PLT entry in a VxWorks shared object.  */
1254 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1255 {
1256   0x8f990008,	/* lw t9, 8(gp)		*/
1257   0x00000000,	/* nop			*/
1258   0x03200008,	/* jr t9		*/
1259   0x00000000,	/* nop			*/
1260   0x00000000,	/* nop			*/
1261   0x00000000	/* nop			*/
1262 };
1263 
1264 /* The format of subsequent PLT entries.  */
1265 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1266 {
1267   0x10000000,	/* b .PLT_resolver	*/
1268   0x24180000	/* li t8, <pltindex>	*/
1269 };
1270 
1271 /* microMIPS 32-bit opcode helper installer.  */
1272 
1273 static void
1274 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1275 {
1276   bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1277   bfd_put_16 (abfd,  opcode	   & 0xffff, ptr + 2);
1278 }
1279 
1280 /* microMIPS 32-bit opcode helper retriever.  */
1281 
1282 static bfd_vma
1283 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1284 {
1285   return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1286 }
1287 
1288 /* Look up an entry in a MIPS ELF linker hash table.  */
1289 
1290 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
1291   ((struct mips_elf_link_hash_entry *)					\
1292    elf_link_hash_lookup (&(table)->root, (string), (create),		\
1293 			 (copy), (follow)))
1294 
1295 /* Traverse a MIPS ELF linker hash table.  */
1296 
1297 #define mips_elf_link_hash_traverse(table, func, info)			\
1298   (elf_link_hash_traverse						\
1299    (&(table)->root,							\
1300     (bool (*) (struct elf_link_hash_entry *, void *)) (func),		\
1301     (info)))
1302 
1303 /* Find the base offsets for thread-local storage in this object,
1304    for GD/LD and IE/LE respectively.  */
1305 
1306 #define TP_OFFSET 0x7000
1307 #define DTP_OFFSET 0x8000
1308 
1309 static bfd_vma
1310 dtprel_base (struct bfd_link_info *info)
1311 {
1312   /* If tls_sec is NULL, we should have signalled an error already.  */
1313   if (elf_hash_table (info)->tls_sec == NULL)
1314     return 0;
1315   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1316 }
1317 
1318 static bfd_vma
1319 tprel_base (struct bfd_link_info *info)
1320 {
1321   /* If tls_sec is NULL, we should have signalled an error already.  */
1322   if (elf_hash_table (info)->tls_sec == NULL)
1323     return 0;
1324   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1325 }
1326 
1327 /* Create an entry in a MIPS ELF linker hash table.  */
1328 
1329 static struct bfd_hash_entry *
1330 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331 			    struct bfd_hash_table *table, const char *string)
1332 {
1333   struct mips_elf_link_hash_entry *ret =
1334     (struct mips_elf_link_hash_entry *) entry;
1335 
1336   /* Allocate the structure if it has not already been allocated by a
1337      subclass.  */
1338   if (ret == NULL)
1339     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340   if (ret == NULL)
1341     return (struct bfd_hash_entry *) ret;
1342 
1343   /* Call the allocation method of the superclass.  */
1344   ret = ((struct mips_elf_link_hash_entry *)
1345 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346 				     table, string));
1347   if (ret != NULL)
1348     {
1349       /* Set local fields.  */
1350       memset (&ret->esym, 0, sizeof (EXTR));
1351       /* We use -2 as a marker to indicate that the information has
1352 	 not been set.  -1 means there is no associated ifd.  */
1353       ret->esym.ifd = -2;
1354       ret->la25_stub = 0;
1355       ret->possibly_dynamic_relocs = 0;
1356       ret->fn_stub = NULL;
1357       ret->call_stub = NULL;
1358       ret->call_fp_stub = NULL;
1359       ret->mipsxhash_loc = 0;
1360       ret->global_got_area = GGA_NONE;
1361       ret->got_only_for_calls = true;
1362       ret->readonly_reloc = false;
1363       ret->has_static_relocs = false;
1364       ret->no_fn_stub = false;
1365       ret->need_fn_stub = false;
1366       ret->has_nonpic_branches = false;
1367       ret->needs_lazy_stub = false;
1368       ret->use_plt_entry = false;
1369     }
1370 
1371   return (struct bfd_hash_entry *) ret;
1372 }
1373 
1374 /* Allocate MIPS ELF private object data.  */
1375 
1376 bool
1377 _bfd_mips_elf_mkobject (bfd *abfd)
1378 {
1379   return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380 				  MIPS_ELF_DATA);
1381 }
1382 
1383 /* MIPS ELF uses a special find_nearest_line routine in order the
1384    handle the ECOFF debugging information.  */
1385 
1386 struct mips_elf_find_line
1387 {
1388   struct ecoff_debug_info d;
1389   struct ecoff_find_line i;
1390 };
1391 
1392 bool
1393 _bfd_mips_elf_free_cached_info (bfd *abfd)
1394 {
1395   struct mips_elf_obj_tdata *tdata;
1396 
1397   if ((bfd_get_format (abfd) == bfd_object
1398        || bfd_get_format (abfd) == bfd_core)
1399       && (tdata = mips_elf_tdata (abfd)) != NULL)
1400     {
1401       BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1402       while (tdata->mips_hi16_list != NULL)
1403 	{
1404 	  struct mips_hi16 *hi = tdata->mips_hi16_list;
1405 	  tdata->mips_hi16_list = hi->next;
1406 	  free (hi);
1407 	}
1408       if (tdata->find_line_info != NULL)
1409 	_bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1410     }
1411   return _bfd_elf_free_cached_info (abfd);
1412 }
1413 
1414 bool
1415 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1416 {
1417   if (!sec->used_by_bfd)
1418     {
1419       struct _mips_elf_section_data *sdata;
1420       size_t amt = sizeof (*sdata);
1421 
1422       sdata = bfd_zalloc (abfd, amt);
1423       if (sdata == NULL)
1424 	return false;
1425       sec->used_by_bfd = sdata;
1426     }
1427 
1428   return _bfd_elf_new_section_hook (abfd, sec);
1429 }
1430 
1431 /* Read ECOFF debugging information from a .mdebug section into a
1432    ecoff_debug_info structure.  */
1433 
1434 bool
1435 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1436 			       struct ecoff_debug_info *debug)
1437 {
1438   HDRR *symhdr;
1439   const struct ecoff_debug_swap *swap;
1440   char *ext_hdr;
1441 
1442   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443   memset (debug, 0, sizeof (*debug));
1444 
1445   ext_hdr = bfd_malloc (swap->external_hdr_size);
1446   if (ext_hdr == NULL && swap->external_hdr_size != 0)
1447     goto error_return;
1448 
1449   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1450 				  swap->external_hdr_size))
1451     goto error_return;
1452 
1453   symhdr = &debug->symbolic_header;
1454   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1455   free (ext_hdr);
1456   ext_hdr = NULL;
1457 
1458   /* The symbolic header contains absolute file offsets and sizes to
1459      read.  */
1460 #define READ(ptr, offset, count, size)					\
1461   do									\
1462     {									\
1463       size_t amt;							\
1464       debug->ptr = NULL;						\
1465       if (symhdr->count == 0)						\
1466 	break;								\
1467       if (_bfd_mul_overflow (size, symhdr->count, &amt))		\
1468 	{								\
1469 	  bfd_set_error (bfd_error_file_too_big);			\
1470 	  goto error_return;						\
1471 	}								\
1472       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)		\
1473 	goto error_return;						\
1474       debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);		\
1475       if (debug->ptr == NULL)						\
1476 	goto error_return;						\
1477       ((char *) debug->ptr)[amt] = 0;					\
1478     } while (0)
1479 
1480   READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1481   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1482   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1483   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1484   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1485   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1486   READ (ss, cbSsOffset, issMax, sizeof (char));
1487   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1488   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1489   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1490   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1491 #undef READ
1492 
1493   return true;
1494 
1495  error_return:
1496   free (ext_hdr);
1497   _bfd_ecoff_free_ecoff_debug_info (debug);
1498   return false;
1499 }
1500 
1501 /* Swap RPDR (runtime procedure table entry) for output.  */
1502 
1503 static void
1504 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1505 {
1506   H_PUT_S32 (abfd, in->adr, ex->p_adr);
1507   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1508   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1509   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1510   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1511   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1512 
1513   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1514   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1515 
1516   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1517 }
1518 
1519 /* Create a runtime procedure table from the .mdebug section.  */
1520 
1521 static bool
1522 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1523 				 struct bfd_link_info *info, asection *s,
1524 				 struct ecoff_debug_info *debug)
1525 {
1526   const struct ecoff_debug_swap *swap;
1527   HDRR *hdr = &debug->symbolic_header;
1528   RPDR *rpdr, *rp;
1529   struct rpdr_ext *erp;
1530   void *rtproc;
1531   struct pdr_ext *epdr;
1532   struct sym_ext *esym;
1533   char *ss, **sv;
1534   char *str;
1535   bfd_size_type size;
1536   bfd_size_type count;
1537   unsigned long sindex;
1538   unsigned long i;
1539   PDR pdr;
1540   SYMR sym;
1541   const char *no_name_func = _("static procedure (no name)");
1542 
1543   epdr = NULL;
1544   rpdr = NULL;
1545   esym = NULL;
1546   ss = NULL;
1547   sv = NULL;
1548 
1549   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1550 
1551   sindex = strlen (no_name_func) + 1;
1552   count = hdr->ipdMax;
1553   if (count > 0)
1554     {
1555       size = swap->external_pdr_size;
1556 
1557       epdr = bfd_malloc (size * count);
1558       if (epdr == NULL)
1559 	goto error_return;
1560 
1561       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1562 	goto error_return;
1563 
1564       size = sizeof (RPDR);
1565       rp = rpdr = bfd_malloc (size * count);
1566       if (rpdr == NULL)
1567 	goto error_return;
1568 
1569       size = sizeof (char *);
1570       sv = bfd_malloc (size * count);
1571       if (sv == NULL)
1572 	goto error_return;
1573 
1574       count = hdr->isymMax;
1575       size = swap->external_sym_size;
1576       esym = bfd_malloc (size * count);
1577       if (esym == NULL)
1578 	goto error_return;
1579 
1580       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1581 	goto error_return;
1582 
1583       count = hdr->issMax;
1584       ss = bfd_malloc (count);
1585       if (ss == NULL)
1586 	goto error_return;
1587       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1588 	goto error_return;
1589 
1590       count = hdr->ipdMax;
1591       for (i = 0; i < (unsigned long) count; i++, rp++)
1592 	{
1593 	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1594 	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1595 	  rp->adr = sym.value;
1596 	  rp->regmask = pdr.regmask;
1597 	  rp->regoffset = pdr.regoffset;
1598 	  rp->fregmask = pdr.fregmask;
1599 	  rp->fregoffset = pdr.fregoffset;
1600 	  rp->frameoffset = pdr.frameoffset;
1601 	  rp->framereg = pdr.framereg;
1602 	  rp->pcreg = pdr.pcreg;
1603 	  rp->irpss = sindex;
1604 	  sv[i] = ss + sym.iss;
1605 	  sindex += strlen (sv[i]) + 1;
1606 	}
1607     }
1608 
1609   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1610   size = BFD_ALIGN (size, 16);
1611   rtproc = bfd_alloc (abfd, size);
1612   if (rtproc == NULL)
1613     {
1614       mips_elf_hash_table (info)->procedure_count = 0;
1615       goto error_return;
1616     }
1617 
1618   mips_elf_hash_table (info)->procedure_count = count + 2;
1619 
1620   erp = rtproc;
1621   memset (erp, 0, sizeof (struct rpdr_ext));
1622   erp++;
1623   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1624   strcpy (str, no_name_func);
1625   str += strlen (no_name_func) + 1;
1626   for (i = 0; i < count; i++)
1627     {
1628       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1629       strcpy (str, sv[i]);
1630       str += strlen (sv[i]) + 1;
1631     }
1632   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1633 
1634   /* Set the size and contents of .rtproc section.  */
1635   s->size = size;
1636   s->contents = rtproc;
1637 
1638   /* Skip this section later on (I don't think this currently
1639      matters, but someday it might).  */
1640   s->map_head.link_order = NULL;
1641 
1642   free (epdr);
1643   free (rpdr);
1644   free (esym);
1645   free (ss);
1646   free (sv);
1647   return true;
1648 
1649  error_return:
1650   free (epdr);
1651   free (rpdr);
1652   free (esym);
1653   free (ss);
1654   free (sv);
1655   return false;
1656 }
1657 
1658 /* We're going to create a stub for H.  Create a symbol for the stub's
1659    value and size, to help make the disassembly easier to read.  */
1660 
1661 static bool
1662 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1663 			     struct mips_elf_link_hash_entry *h,
1664 			     const char *prefix, asection *s, bfd_vma value,
1665 			     bfd_vma size)
1666 {
1667   bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1668   struct bfd_link_hash_entry *bh;
1669   struct elf_link_hash_entry *elfh;
1670   char *name;
1671   bool res;
1672 
1673   if (micromips_p)
1674     value |= 1;
1675 
1676   /* Create a new symbol.  */
1677   name = concat (prefix, h->root.root.root.string, NULL);
1678   bh = NULL;
1679   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1680 					  BSF_LOCAL, s, value, NULL,
1681 					  true, false, &bh);
1682   free (name);
1683   if (! res)
1684     return false;
1685 
1686   /* Make it a local function.  */
1687   elfh = (struct elf_link_hash_entry *) bh;
1688   elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1689   elfh->size = size;
1690   elfh->forced_local = 1;
1691   if (micromips_p)
1692     elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1693   return true;
1694 }
1695 
1696 /* We're about to redefine H.  Create a symbol to represent H's
1697    current value and size, to help make the disassembly easier
1698    to read.  */
1699 
1700 static bool
1701 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1702 			       struct mips_elf_link_hash_entry *h,
1703 			       const char *prefix)
1704 {
1705   struct bfd_link_hash_entry *bh;
1706   struct elf_link_hash_entry *elfh;
1707   char *name;
1708   asection *s;
1709   bfd_vma value;
1710   bool res;
1711 
1712   /* Read the symbol's value.  */
1713   BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1714 	      || h->root.root.type == bfd_link_hash_defweak);
1715   s = h->root.root.u.def.section;
1716   value = h->root.root.u.def.value;
1717 
1718   /* Create a new symbol.  */
1719   name = concat (prefix, h->root.root.root.string, NULL);
1720   bh = NULL;
1721   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1722 					  BSF_LOCAL, s, value, NULL,
1723 					  true, false, &bh);
1724   free (name);
1725   if (! res)
1726     return false;
1727 
1728   /* Make it local and copy the other attributes from H.  */
1729   elfh = (struct elf_link_hash_entry *) bh;
1730   elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1731   elfh->other = h->root.other;
1732   elfh->size = h->root.size;
1733   elfh->forced_local = 1;
1734   return true;
1735 }
1736 
1737 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1738    function rather than to a hard-float stub.  */
1739 
1740 static bool
1741 section_allows_mips16_refs_p (asection *section)
1742 {
1743   const char *name;
1744 
1745   name = bfd_section_name (section);
1746   return (FN_STUB_P (name)
1747 	  || CALL_STUB_P (name)
1748 	  || CALL_FP_STUB_P (name)
1749 	  || strcmp (name, ".pdr") == 0);
1750 }
1751 
1752 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1753    stub section of some kind.  Return the R_SYMNDX of the target
1754    function, or 0 if we can't decide which function that is.  */
1755 
1756 static unsigned long
1757 mips16_stub_symndx (const struct elf_backend_data *bed,
1758 		    asection *sec ATTRIBUTE_UNUSED,
1759 		    const Elf_Internal_Rela *relocs,
1760 		    const Elf_Internal_Rela *relend)
1761 {
1762   int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1763   const Elf_Internal_Rela *rel;
1764 
1765   /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1766      one in a compound relocation.  */
1767   for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1768     if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1769       return ELF_R_SYM (sec->owner, rel->r_info);
1770 
1771   /* Otherwise trust the first relocation, whatever its kind.  This is
1772      the traditional behavior.  */
1773   if (relocs < relend)
1774     return ELF_R_SYM (sec->owner, relocs->r_info);
1775 
1776   return 0;
1777 }
1778 
1779 /* Check the mips16 stubs for a particular symbol, and see if we can
1780    discard them.  */
1781 
1782 static void
1783 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1784 			     struct mips_elf_link_hash_entry *h)
1785 {
1786   /* Dynamic symbols must use the standard call interface, in case other
1787      objects try to call them.  */
1788   if (h->fn_stub != NULL
1789       && h->root.dynindx != -1)
1790     {
1791       mips_elf_create_shadow_symbol (info, h, ".mips16.");
1792       h->need_fn_stub = true;
1793     }
1794 
1795   if (h->fn_stub != NULL
1796       && ! h->need_fn_stub)
1797     {
1798       /* We don't need the fn_stub; the only references to this symbol
1799 	 are 16 bit calls.  Clobber the size to 0 to prevent it from
1800 	 being included in the link.  */
1801       h->fn_stub->size = 0;
1802       h->fn_stub->flags &= ~SEC_RELOC;
1803       h->fn_stub->reloc_count = 0;
1804       h->fn_stub->flags |= SEC_EXCLUDE;
1805       h->fn_stub->output_section = bfd_abs_section_ptr;
1806     }
1807 
1808   if (h->call_stub != NULL
1809       && ELF_ST_IS_MIPS16 (h->root.other))
1810     {
1811       /* We don't need the call_stub; this is a 16 bit function, so
1812 	 calls from other 16 bit functions are OK.  Clobber the size
1813 	 to 0 to prevent it from being included in the link.  */
1814       h->call_stub->size = 0;
1815       h->call_stub->flags &= ~SEC_RELOC;
1816       h->call_stub->reloc_count = 0;
1817       h->call_stub->flags |= SEC_EXCLUDE;
1818       h->call_stub->output_section = bfd_abs_section_ptr;
1819     }
1820 
1821   if (h->call_fp_stub != NULL
1822       && ELF_ST_IS_MIPS16 (h->root.other))
1823     {
1824       /* We don't need the call_stub; this is a 16 bit function, so
1825 	 calls from other 16 bit functions are OK.  Clobber the size
1826 	 to 0 to prevent it from being included in the link.  */
1827       h->call_fp_stub->size = 0;
1828       h->call_fp_stub->flags &= ~SEC_RELOC;
1829       h->call_fp_stub->reloc_count = 0;
1830       h->call_fp_stub->flags |= SEC_EXCLUDE;
1831       h->call_fp_stub->output_section = bfd_abs_section_ptr;
1832     }
1833 }
1834 
1835 /* Hashtable callbacks for mips_elf_la25_stubs.  */
1836 
1837 static hashval_t
1838 mips_elf_la25_stub_hash (const void *entry_)
1839 {
1840   const struct mips_elf_la25_stub *entry;
1841 
1842   entry = (struct mips_elf_la25_stub *) entry_;
1843   return entry->h->root.root.u.def.section->id
1844     + entry->h->root.root.u.def.value;
1845 }
1846 
1847 static int
1848 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1849 {
1850   const struct mips_elf_la25_stub *entry1, *entry2;
1851 
1852   entry1 = (struct mips_elf_la25_stub *) entry1_;
1853   entry2 = (struct mips_elf_la25_stub *) entry2_;
1854   return ((entry1->h->root.root.u.def.section
1855 	   == entry2->h->root.root.u.def.section)
1856 	  && (entry1->h->root.root.u.def.value
1857 	      == entry2->h->root.root.u.def.value));
1858 }
1859 
1860 /* Called by the linker to set up the la25 stub-creation code.  FN is
1861    the linker's implementation of add_stub_function.  Return true on
1862    success.  */
1863 
1864 bool
1865 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1866 			  asection *(*fn) (const char *, asection *,
1867 					   asection *))
1868 {
1869   struct mips_elf_link_hash_table *htab;
1870 
1871   htab = mips_elf_hash_table (info);
1872   if (htab == NULL)
1873     return false;
1874 
1875   htab->add_stub_section = fn;
1876   htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1877 				      mips_elf_la25_stub_eq, NULL);
1878   if (htab->la25_stubs == NULL)
1879     return false;
1880 
1881   return true;
1882 }
1883 
1884 /* Return true if H is a locally-defined PIC function, in the sense
1885    that it or its fn_stub might need $25 to be valid on entry.
1886    Note that MIPS16 functions set up $gp using PC-relative instructions,
1887    so they themselves never need $25 to be valid.  Only non-MIPS16
1888    entry points are of interest here.  */
1889 
1890 static bool
1891 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1892 {
1893   return ((h->root.root.type == bfd_link_hash_defined
1894 	   || h->root.root.type == bfd_link_hash_defweak)
1895 	  && h->root.def_regular
1896 	  && !bfd_is_abs_section (h->root.root.u.def.section)
1897 	  && !bfd_is_und_section (h->root.root.u.def.section)
1898 	  && (!ELF_ST_IS_MIPS16 (h->root.other)
1899 	      || (h->fn_stub && h->need_fn_stub))
1900 	  && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1901 	      || ELF_ST_IS_MIPS_PIC (h->root.other)));
1902 }
1903 
1904 /* Set *SEC to the input section that contains the target of STUB.
1905    Return the offset of the target from the start of that section.  */
1906 
1907 static bfd_vma
1908 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1909 			  asection **sec)
1910 {
1911   if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1912     {
1913       BFD_ASSERT (stub->h->need_fn_stub);
1914       *sec = stub->h->fn_stub;
1915       return 0;
1916     }
1917   else
1918     {
1919       *sec = stub->h->root.root.u.def.section;
1920       return stub->h->root.root.u.def.value;
1921     }
1922 }
1923 
1924 /* STUB describes an la25 stub that we have decided to implement
1925    by inserting an LUI/ADDIU pair before the target function.
1926    Create the section and redirect the function symbol to it.  */
1927 
1928 static bool
1929 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1930 			 struct bfd_link_info *info)
1931 {
1932   struct mips_elf_link_hash_table *htab;
1933   char *name;
1934   asection *s, *input_section;
1935   unsigned int align;
1936 
1937   htab = mips_elf_hash_table (info);
1938   if (htab == NULL)
1939     return false;
1940 
1941   /* Create a unique name for the new section.  */
1942   name = bfd_malloc (11 + sizeof (".text.stub."));
1943   if (name == NULL)
1944     return false;
1945   sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1946 
1947   /* Create the section.  */
1948   mips_elf_get_la25_target (stub, &input_section);
1949   s = htab->add_stub_section (name, input_section,
1950 			      input_section->output_section);
1951   if (s == NULL)
1952     return false;
1953 
1954   /* Make sure that any padding goes before the stub.  */
1955   align = input_section->alignment_power;
1956   if (!bfd_set_section_alignment (s, align))
1957     return false;
1958   if (align > 3)
1959     s->size = (1 << align) - 8;
1960 
1961   /* Create a symbol for the stub.  */
1962   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1963   stub->stub_section = s;
1964   stub->offset = s->size;
1965 
1966   /* Allocate room for it.  */
1967   s->size += 8;
1968   return true;
1969 }
1970 
1971 /* STUB describes an la25 stub that we have decided to implement
1972    with a separate trampoline.  Allocate room for it and redirect
1973    the function symbol to it.  */
1974 
1975 static bool
1976 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1977 			      struct bfd_link_info *info)
1978 {
1979   struct mips_elf_link_hash_table *htab;
1980   asection *s;
1981 
1982   htab = mips_elf_hash_table (info);
1983   if (htab == NULL)
1984     return false;
1985 
1986   /* Create a trampoline section, if we haven't already.  */
1987   s = htab->strampoline;
1988   if (s == NULL)
1989     {
1990       asection *input_section = stub->h->root.root.u.def.section;
1991       s = htab->add_stub_section (".text", NULL,
1992 				  input_section->output_section);
1993       if (s == NULL || !bfd_set_section_alignment (s, 4))
1994 	return false;
1995       htab->strampoline = s;
1996     }
1997 
1998   /* Create a symbol for the stub.  */
1999   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
2000   stub->stub_section = s;
2001   stub->offset = s->size;
2002 
2003   /* Allocate room for it.  */
2004   s->size += 16;
2005   return true;
2006 }
2007 
2008 /* H describes a symbol that needs an la25 stub.  Make sure that an
2009    appropriate stub exists and point H at it.  */
2010 
2011 static bool
2012 mips_elf_add_la25_stub (struct bfd_link_info *info,
2013 			struct mips_elf_link_hash_entry *h)
2014 {
2015   struct mips_elf_link_hash_table *htab;
2016   struct mips_elf_la25_stub search, *stub;
2017   bool use_trampoline_p;
2018   asection *s;
2019   bfd_vma value;
2020   void **slot;
2021 
2022   /* Describe the stub we want.  */
2023   search.stub_section = NULL;
2024   search.offset = 0;
2025   search.h = h;
2026 
2027   /* See if we've already created an equivalent stub.  */
2028   htab = mips_elf_hash_table (info);
2029   if (htab == NULL)
2030     return false;
2031 
2032   slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2033   if (slot == NULL)
2034     return false;
2035 
2036   stub = (struct mips_elf_la25_stub *) *slot;
2037   if (stub != NULL)
2038     {
2039       /* We can reuse the existing stub.  */
2040       h->la25_stub = stub;
2041       return true;
2042     }
2043 
2044   /* Create a permanent copy of ENTRY and add it to the hash table.  */
2045   stub = bfd_malloc (sizeof (search));
2046   if (stub == NULL)
2047     return false;
2048   *stub = search;
2049   *slot = stub;
2050 
2051   /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2052      of the section and if we would need no more than 2 nops.  */
2053   value = mips_elf_get_la25_target (stub, &s);
2054   if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2055     value &= ~1;
2056   use_trampoline_p = (value != 0 || s->alignment_power > 4);
2057 
2058   h->la25_stub = stub;
2059   return (use_trampoline_p
2060 	  ? mips_elf_add_la25_trampoline (stub, info)
2061 	  : mips_elf_add_la25_intro (stub, info));
2062 }
2063 
2064 /* A mips_elf_link_hash_traverse callback that is called before sizing
2065    sections.  DATA points to a mips_htab_traverse_info structure.  */
2066 
2067 static bool
2068 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2069 {
2070   struct mips_htab_traverse_info *hti;
2071 
2072   hti = (struct mips_htab_traverse_info *) data;
2073   if (!bfd_link_relocatable (hti->info))
2074     mips_elf_check_mips16_stubs (hti->info, h);
2075 
2076   if (mips_elf_local_pic_function_p (h))
2077     {
2078       /* PR 12845: If H is in a section that has been garbage
2079 	 collected it will have its output section set to *ABS*.  */
2080       if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2081 	return true;
2082 
2083       /* H is a function that might need $25 to be valid on entry.
2084 	 If we're creating a non-PIC relocatable object, mark H as
2085 	 being PIC.  If we're creating a non-relocatable object with
2086 	 non-PIC branches and jumps to H, make sure that H has an la25
2087 	 stub.  */
2088       if (bfd_link_relocatable (hti->info))
2089 	{
2090 	  if (!PIC_OBJECT_P (hti->output_bfd))
2091 	    h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2092 	}
2093       else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2094 	{
2095 	  hti->error = true;
2096 	  return false;
2097 	}
2098     }
2099   return true;
2100 }
2101 
2102 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2103    Most mips16 instructions are 16 bits, but these instructions
2104    are 32 bits.
2105 
2106    The format of these instructions is:
2107 
2108    +--------------+--------------------------------+
2109    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
2110    +--------------+--------------------------------+
2111    |		    Immediate  15:0		   |
2112    +-----------------------------------------------+
2113 
2114    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
2115    Note that the immediate value in the first word is swapped.
2116 
2117    When producing a relocatable object file, R_MIPS16_26 is
2118    handled mostly like R_MIPS_26.  In particular, the addend is
2119    stored as a straight 26-bit value in a 32-bit instruction.
2120    (gas makes life simpler for itself by never adjusting a
2121    R_MIPS16_26 reloc to be against a section, so the addend is
2122    always zero).  However, the 32 bit instruction is stored as 2
2123    16-bit values, rather than a single 32-bit value.  In a
2124    big-endian file, the result is the same; in a little-endian
2125    file, the two 16-bit halves of the 32 bit value are swapped.
2126    This is so that a disassembler can recognize the jal
2127    instruction.
2128 
2129    When doing a final link, R_MIPS16_26 is treated as a 32 bit
2130    instruction stored as two 16-bit values.  The addend A is the
2131    contents of the targ26 field.  The calculation is the same as
2132    R_MIPS_26.  When storing the calculated value, reorder the
2133    immediate value as shown above, and don't forget to store the
2134    value as two 16-bit values.
2135 
2136    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2137    defined as
2138 
2139    big-endian:
2140    +--------+----------------------+
2141    |	    |			   |
2142    |	    |	 targ26-16	   |
2143    |31	  26|25			  0|
2144    +--------+----------------------+
2145 
2146    little-endian:
2147    +----------+------+-------------+
2148    |	      |	     |		   |
2149    |  sub1    |	     |	   sub2	   |
2150    |0	     9|10  15|16	 31|
2151    +----------+--------------------+
2152    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2153    ((sub1 << 16) | sub2)).
2154 
2155    When producing a relocatable object file, the calculation is
2156    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2157    When producing a fully linked file, the calculation is
2158    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2159    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2160 
2161    The table below lists the other MIPS16 instruction relocations.
2162    Each one is calculated in the same way as the non-MIPS16 relocation
2163    given on the right, but using the extended MIPS16 layout of 16-bit
2164    immediate fields:
2165 
2166 	R_MIPS16_GPREL		R_MIPS_GPREL16
2167 	R_MIPS16_GOT16		R_MIPS_GOT16
2168 	R_MIPS16_CALL16		R_MIPS_CALL16
2169 	R_MIPS16_HI16		R_MIPS_HI16
2170 	R_MIPS16_LO16		R_MIPS_LO16
2171 
2172    A typical instruction will have a format like this:
2173 
2174    +--------------+--------------------------------+
2175    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
2176    +--------------+--------------------------------+
2177    |    Major     |   rx   |   ry   |   Imm  4:0   |
2178    +--------------+--------------------------------+
2179 
2180    EXTEND is the five bit value 11110.  Major is the instruction
2181    opcode.
2182 
2183    All we need to do here is shuffle the bits appropriately.
2184    As above, the two 16-bit halves must be swapped on a
2185    little-endian system.
2186 
2187    Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2188    relocatable field is shifted by 1 rather than 2 and the same bit
2189    shuffling is done as with the relocations above.  */
2190 
2191 static inline bool
2192 mips16_reloc_p (int r_type)
2193 {
2194   switch (r_type)
2195     {
2196     case R_MIPS16_26:
2197     case R_MIPS16_GPREL:
2198     case R_MIPS16_GOT16:
2199     case R_MIPS16_CALL16:
2200     case R_MIPS16_HI16:
2201     case R_MIPS16_LO16:
2202     case R_MIPS16_TLS_GD:
2203     case R_MIPS16_TLS_LDM:
2204     case R_MIPS16_TLS_DTPREL_HI16:
2205     case R_MIPS16_TLS_DTPREL_LO16:
2206     case R_MIPS16_TLS_GOTTPREL:
2207     case R_MIPS16_TLS_TPREL_HI16:
2208     case R_MIPS16_TLS_TPREL_LO16:
2209     case R_MIPS16_PC16_S1:
2210       return true;
2211 
2212     default:
2213       return false;
2214     }
2215 }
2216 
2217 /* Check if a microMIPS reloc.  */
2218 
2219 static inline bool
2220 micromips_reloc_p (unsigned int r_type)
2221 {
2222   return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2223 }
2224 
2225 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2226    on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1,
2227    R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
2228    16-bit instructions.  */
2229 
2230 static inline bool
2231 micromips_reloc_shuffle_p (unsigned int r_type)
2232 {
2233   return (micromips_reloc_p (r_type)
2234 	  && r_type != R_MICROMIPS_PC7_S1
2235 	  && r_type != R_MICROMIPS_PC10_S1
2236 	  && r_type != R_MICROMIPS_GPREL7_S2);
2237 }
2238 
2239 static inline bool
2240 got16_reloc_p (int r_type)
2241 {
2242   return (r_type == R_MIPS_GOT16
2243 	  || r_type == R_MIPS16_GOT16
2244 	  || r_type == R_MICROMIPS_GOT16);
2245 }
2246 
2247 static inline bool
2248 call16_reloc_p (int r_type)
2249 {
2250   return (r_type == R_MIPS_CALL16
2251 	  || r_type == R_MIPS16_CALL16
2252 	  || r_type == R_MICROMIPS_CALL16);
2253 }
2254 
2255 static inline bool
2256 got_disp_reloc_p (unsigned int r_type)
2257 {
2258   return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2259 }
2260 
2261 static inline bool
2262 got_page_reloc_p (unsigned int r_type)
2263 {
2264   return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2265 }
2266 
2267 static inline bool
2268 got_lo16_reloc_p (unsigned int r_type)
2269 {
2270   return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2271 }
2272 
2273 static inline bool
2274 call_hi16_reloc_p (unsigned int r_type)
2275 {
2276   return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2277 }
2278 
2279 static inline bool
2280 call_lo16_reloc_p (unsigned int r_type)
2281 {
2282   return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2283 }
2284 
2285 static inline bool
2286 hi16_reloc_p (int r_type)
2287 {
2288   return (r_type == R_MIPS_HI16
2289 	  || r_type == R_MIPS16_HI16
2290 	  || r_type == R_MICROMIPS_HI16
2291 	  || r_type == R_MIPS_PCHI16);
2292 }
2293 
2294 static inline bool
2295 lo16_reloc_p (int r_type)
2296 {
2297   return (r_type == R_MIPS_LO16
2298 	  || r_type == R_MIPS16_LO16
2299 	  || r_type == R_MICROMIPS_LO16
2300 	  || r_type == R_MIPS_PCLO16);
2301 }
2302 
2303 static inline bool
2304 mips16_call_reloc_p (int r_type)
2305 {
2306   return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2307 }
2308 
2309 static inline bool
2310 jal_reloc_p (int r_type)
2311 {
2312   return (r_type == R_MIPS_26
2313 	  || r_type == R_MIPS16_26
2314 	  || r_type == R_MICROMIPS_26_S1);
2315 }
2316 
2317 static inline bool
2318 b_reloc_p (int r_type)
2319 {
2320   return (r_type == R_MIPS_PC26_S2
2321 	  || r_type == R_MIPS_PC21_S2
2322 	  || r_type == R_MIPS_PC16
2323 	  || r_type == R_MIPS_GNU_REL16_S2
2324 	  || r_type == R_MIPS16_PC16_S1
2325 	  || r_type == R_MICROMIPS_PC16_S1
2326 	  || r_type == R_MICROMIPS_PC10_S1
2327 	  || r_type == R_MICROMIPS_PC7_S1);
2328 }
2329 
2330 static inline bool
2331 aligned_pcrel_reloc_p (int r_type)
2332 {
2333   return (r_type == R_MIPS_PC18_S3
2334 	  || r_type == R_MIPS_PC19_S2);
2335 }
2336 
2337 static inline bool
2338 branch_reloc_p (int r_type)
2339 {
2340   return (r_type == R_MIPS_26
2341 	  || r_type == R_MIPS_PC26_S2
2342 	  || r_type == R_MIPS_PC21_S2
2343 	  || r_type == R_MIPS_PC16
2344 	  || r_type == R_MIPS_GNU_REL16_S2);
2345 }
2346 
2347 static inline bool
2348 mips16_branch_reloc_p (int r_type)
2349 {
2350   return (r_type == R_MIPS16_26
2351 	  || r_type == R_MIPS16_PC16_S1);
2352 }
2353 
2354 static inline bool
2355 micromips_branch_reloc_p (int r_type)
2356 {
2357   return (r_type == R_MICROMIPS_26_S1
2358 	  || r_type == R_MICROMIPS_PC16_S1
2359 	  || r_type == R_MICROMIPS_PC10_S1
2360 	  || r_type == R_MICROMIPS_PC7_S1);
2361 }
2362 
2363 static inline bool
2364 tls_gd_reloc_p (unsigned int r_type)
2365 {
2366   return (r_type == R_MIPS_TLS_GD
2367 	  || r_type == R_MIPS16_TLS_GD
2368 	  || r_type == R_MICROMIPS_TLS_GD);
2369 }
2370 
2371 static inline bool
2372 tls_ldm_reloc_p (unsigned int r_type)
2373 {
2374   return (r_type == R_MIPS_TLS_LDM
2375 	  || r_type == R_MIPS16_TLS_LDM
2376 	  || r_type == R_MICROMIPS_TLS_LDM);
2377 }
2378 
2379 static inline bool
2380 tls_gottprel_reloc_p (unsigned int r_type)
2381 {
2382   return (r_type == R_MIPS_TLS_GOTTPREL
2383 	  || r_type == R_MIPS16_TLS_GOTTPREL
2384 	  || r_type == R_MICROMIPS_TLS_GOTTPREL);
2385 }
2386 
2387 static inline bool
2388 needs_shuffle (int r_type)
2389 {
2390   return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2391 }
2392 
2393 void
2394 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2395 			       bool jal_shuffle, bfd_byte *data)
2396 {
2397   bfd_vma first, second, val;
2398 
2399   if (!needs_shuffle (r_type))
2400     return;
2401 
2402   /* Pick up the first and second halfwords of the instruction.  */
2403   first = bfd_get_16 (abfd, data);
2404   second = bfd_get_16 (abfd, data + 2);
2405   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2406     val = first << 16 | second;
2407   else if (r_type != R_MIPS16_26)
2408     val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2409 	   | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2410   else
2411     val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2412 	   | ((first & 0x1f) << 21) | second);
2413   bfd_put_32 (abfd, val, data);
2414 }
2415 
2416 void
2417 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2418 			     bool jal_shuffle, bfd_byte *data)
2419 {
2420   bfd_vma first, second, val;
2421 
2422   if (!needs_shuffle (r_type))
2423     return;
2424 
2425   val = bfd_get_32 (abfd, data);
2426   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2427     {
2428       second = val & 0xffff;
2429       first = val >> 16;
2430     }
2431   else if (r_type != R_MIPS16_26)
2432     {
2433       second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2434       first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2435     }
2436   else
2437     {
2438       second = val & 0xffff;
2439       first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2440 	       | ((val >> 21) & 0x1f);
2441     }
2442   bfd_put_16 (abfd, second, data + 2);
2443   bfd_put_16 (abfd, first, data);
2444 }
2445 
2446 /* Perform reloc offset checking.
2447    We can only use bfd_reloc_offset_in_range, which takes into account
2448    the size of the field being relocated, when section contents will
2449    be accessed because mips object files may use relocations that seem
2450    to access beyond section limits.
2451    gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2452    R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2453    section.  The R_MIPS_SUB applies to the addend for the next reloc
2454    rather than the section contents.
2455 
2456    CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2457    CHECK_INPLACE to only check partial_inplace relocs, and
2458    CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
2459 
2460 bool
2461 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2462 				 arelent *reloc_entry, enum reloc_check check)
2463 {
2464   if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2465     return true;
2466   if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2467     return true;
2468   return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2469 				    input_section, reloc_entry->address);
2470 }
2471 
2472 bfd_reloc_status_type
2473 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2474 			       arelent *reloc_entry, asection *input_section,
2475 			       bool relocatable, void *data, bfd_vma gp)
2476 {
2477   bfd_vma relocation;
2478   bfd_signed_vma val;
2479   bfd_reloc_status_type status;
2480 
2481   if (bfd_is_com_section (symbol->section))
2482     relocation = 0;
2483   else
2484     relocation = symbol->value;
2485 
2486   if (symbol->section->output_section != NULL)
2487     {
2488       relocation += symbol->section->output_section->vma;
2489       relocation += symbol->section->output_offset;
2490     }
2491 
2492   /* Set val to the offset into the section or symbol.  */
2493   val = reloc_entry->addend;
2494 
2495   _bfd_mips_elf_sign_extend (val, 16);
2496 
2497   /* Adjust val for the final section location and GP value.  If we
2498      are producing relocatable output, we don't want to do this for
2499      an external symbol.  */
2500   if (! relocatable
2501       || (symbol->flags & BSF_SECTION_SYM) != 0)
2502     val += relocation - gp;
2503 
2504   if (reloc_entry->howto->partial_inplace)
2505     {
2506       if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2507 				      reloc_entry->address))
2508 	return bfd_reloc_outofrange;
2509 
2510       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2511 				       (bfd_byte *) data
2512 				       + reloc_entry->address);
2513       if (status != bfd_reloc_ok)
2514 	return status;
2515     }
2516   else
2517     reloc_entry->addend = val;
2518 
2519   if (relocatable)
2520     reloc_entry->address += input_section->output_offset;
2521 
2522   return bfd_reloc_ok;
2523 }
2524 
2525 /* A howto special_function for REL *HI16 relocations.  We can only
2526    calculate the correct value once we've seen the partnering
2527    *LO16 relocation, so just save the information for later.
2528 
2529    The ABI requires that the *LO16 immediately follow the *HI16.
2530    However, as a GNU extension, we permit an arbitrary number of
2531    *HI16s to be associated with a single *LO16.  This significantly
2532    simplies the relocation handling in gcc.  */
2533 
2534 bfd_reloc_status_type
2535 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2536 			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2537 			  asection *input_section, bfd *output_bfd,
2538 			  char **error_message ATTRIBUTE_UNUSED)
2539 {
2540   struct mips_hi16 *n;
2541   struct mips_elf_obj_tdata *tdata;
2542 
2543   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2544     return bfd_reloc_outofrange;
2545 
2546   n = bfd_malloc (sizeof *n);
2547   if (n == NULL)
2548     return bfd_reloc_outofrange;
2549 
2550   tdata = mips_elf_tdata (abfd);
2551   n->next = tdata->mips_hi16_list;
2552   n->data = data;
2553   n->input_section = input_section;
2554   n->rel = *reloc_entry;
2555   tdata->mips_hi16_list = n;
2556 
2557   if (output_bfd != NULL)
2558     reloc_entry->address += input_section->output_offset;
2559 
2560   return bfd_reloc_ok;
2561 }
2562 
2563 /* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2564    like any other 16-bit relocation when applied to global symbols, but is
2565    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2566 
2567 bfd_reloc_status_type
2568 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2569 			   void *data, asection *input_section,
2570 			   bfd *output_bfd, char **error_message)
2571 {
2572   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2573       || bfd_is_und_section (bfd_asymbol_section (symbol))
2574       || bfd_is_com_section (bfd_asymbol_section (symbol)))
2575     /* The relocation is against a global symbol.  */
2576     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2577 					input_section, output_bfd,
2578 					error_message);
2579 
2580   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2581 				   input_section, output_bfd, error_message);
2582 }
2583 
2584 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2585    is a straightforward 16 bit inplace relocation, but we must deal with
2586    any partnering high-part relocations as well.  */
2587 
2588 bfd_reloc_status_type
2589 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2590 			  void *data, asection *input_section,
2591 			  bfd *output_bfd, char **error_message)
2592 {
2593   bfd_vma vallo;
2594   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2595   struct mips_elf_obj_tdata *tdata;
2596 
2597   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2598 				  reloc_entry->address))
2599     return bfd_reloc_outofrange;
2600 
2601   _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2602 				 location);
2603   /* The high 16 bits of the addend are stored in the high insn, the
2604      low 16 bits in the low insn, but there is a catch:  You can't
2605      just concatenate the high and low parts.  The high part of the
2606      addend is adjusted for the fact that the low part is sign
2607      extended.  For example, an addend of 0x38000 would have 0x0004 in
2608      the high part and 0x8000 (=0xff..f8000) in the low part.
2609      To extract the actual addend, calculate (a)
2610      ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2611      We will be applying (symbol + addend) & 0xffff to the low insn,
2612      and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2613      high insn (the +0x8000 adjusting for when the applied low part is
2614      negative).  Substituting (a) into (b) and recognising that
2615      (hi & 0xffff) is already in the high insn gives a high part
2616      addend adjustment of (lo & 0xffff) ^ 0x8000.  */
2617   vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2618   _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2619 			       location);
2620 
2621   tdata = mips_elf_tdata (abfd);
2622   while (tdata->mips_hi16_list != NULL)
2623     {
2624       bfd_reloc_status_type ret;
2625       struct mips_hi16 *hi;
2626 
2627       hi = tdata->mips_hi16_list;
2628 
2629       /* R_MIPS*_GOT16 relocations are something of a special case.  We
2630 	 want to install the addend in the same way as for a R_MIPS*_HI16
2631 	 relocation (with a rightshift of 16).  However, since GOT16
2632 	 relocations can also be used with global symbols, their howto
2633 	 has a rightshift of 0.  */
2634       if (hi->rel.howto->type == R_MIPS_GOT16)
2635 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2636       else if (hi->rel.howto->type == R_MIPS16_GOT16)
2637 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2638       else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2639 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2640 
2641       hi->rel.addend += vallo;
2642 
2643       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2644 					 hi->input_section, output_bfd,
2645 					 error_message);
2646       if (ret != bfd_reloc_ok)
2647 	return ret;
2648 
2649       tdata->mips_hi16_list = hi->next;
2650       free (hi);
2651     }
2652 
2653   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2654 				      input_section, output_bfd,
2655 				      error_message);
2656 }
2657 
2658 /* A generic howto special_function.  This calculates and installs the
2659    relocation itself, thus avoiding the oft-discussed problems in
2660    bfd_perform_relocation and bfd_install_relocation.  */
2661 
2662 bfd_reloc_status_type
2663 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2664 			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2665 			     asection *input_section, bfd *output_bfd,
2666 			     char **error_message ATTRIBUTE_UNUSED)
2667 {
2668   bfd_signed_vma val;
2669   bfd_reloc_status_type status;
2670   bool relocatable;
2671 
2672   relocatable = (output_bfd != NULL);
2673 
2674   if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2675 					(relocatable
2676 					 ? check_inplace : check_std)))
2677     return bfd_reloc_outofrange;
2678 
2679   /* Build up the field adjustment in VAL.  */
2680   val = 0;
2681   if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2682       && symbol->section->output_section != NULL)
2683     {
2684       /* Either we're calculating the final field value or we have a
2685 	 relocation against a section symbol.  Add in the section's
2686 	 offset or address.  */
2687       val += symbol->section->output_section->vma;
2688       val += symbol->section->output_offset;
2689     }
2690 
2691   if (!relocatable)
2692     {
2693       /* We're calculating the final field value.  Add in the symbol's value
2694 	 and, if pc-relative, subtract the address of the field itself.  */
2695       val += symbol->value;
2696       if (reloc_entry->howto->pc_relative)
2697 	{
2698 	  val -= input_section->output_section->vma;
2699 	  val -= input_section->output_offset;
2700 	  val -= reloc_entry->address;
2701 	}
2702     }
2703 
2704   /* VAL is now the final adjustment.  If we're keeping this relocation
2705      in the output file, and if the relocation uses a separate addend,
2706      we just need to add VAL to that addend.  Otherwise we need to add
2707      VAL to the relocation field itself.  */
2708   if (relocatable && !reloc_entry->howto->partial_inplace)
2709     reloc_entry->addend += val;
2710   else
2711     {
2712       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2713 
2714       /* Add in the separate addend, if any.  */
2715       val += reloc_entry->addend;
2716 
2717       /* Add VAL to the relocation field.  */
2718       _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2719 				     location);
2720       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2721 				       location);
2722       _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2723 				   location);
2724 
2725       if (status != bfd_reloc_ok)
2726 	return status;
2727     }
2728 
2729   if (relocatable)
2730     reloc_entry->address += input_section->output_offset;
2731 
2732   return bfd_reloc_ok;
2733 }
2734 
2735 /* Swap an entry in a .gptab section.  Note that these routines rely
2736    on the equivalence of the two elements of the union.  */
2737 
2738 static void
2739 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2740 			      Elf32_gptab *in)
2741 {
2742   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2743   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2744 }
2745 
2746 static void
2747 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2748 			       Elf32_External_gptab *ex)
2749 {
2750   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2751   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2752 }
2753 
2754 static void
2755 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2756 				Elf32_External_compact_rel *ex)
2757 {
2758   H_PUT_32 (abfd, in->id1, ex->id1);
2759   H_PUT_32 (abfd, in->num, ex->num);
2760   H_PUT_32 (abfd, in->id2, ex->id2);
2761   H_PUT_32 (abfd, in->offset, ex->offset);
2762   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2763   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2764 }
2765 
2766 static void
2767 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2768 			   Elf32_External_crinfo *ex)
2769 {
2770   unsigned long l;
2771 
2772   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2773        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2774        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2775        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2776   H_PUT_32 (abfd, l, ex->info);
2777   H_PUT_32 (abfd, in->konst, ex->konst);
2778   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2779 }
2780 
2781 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
2782    routines swap this structure in and out.  They are used outside of
2783    BFD, so they are globally visible.  */
2784 
2785 void
2786 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2787 				Elf32_RegInfo *in)
2788 {
2789   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2790   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2791   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2792   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2793   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2794   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2795 }
2796 
2797 void
2798 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2799 				 Elf32_External_RegInfo *ex)
2800 {
2801   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2802   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2803   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2804   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2805   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2806   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2807 }
2808 
2809 /* In the 64 bit ABI, the .MIPS.options section holds register
2810    information in an Elf64_Reginfo structure.  These routines swap
2811    them in and out.  They are globally visible because they are used
2812    outside of BFD.  These routines are here so that gas can call them
2813    without worrying about whether the 64 bit ABI has been included.  */
2814 
2815 void
2816 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2817 				Elf64_Internal_RegInfo *in)
2818 {
2819   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2820   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2821   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2822   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2823   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2824   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2825   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2826 }
2827 
2828 void
2829 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2830 				 Elf64_External_RegInfo *ex)
2831 {
2832   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2833   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2834   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2835   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2836   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2837   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2838   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2839 }
2840 
2841 /* Swap in an options header.  */
2842 
2843 void
2844 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2845 			      Elf_Internal_Options *in)
2846 {
2847   in->kind = H_GET_8 (abfd, ex->kind);
2848   in->size = H_GET_8 (abfd, ex->size);
2849   in->section = H_GET_16 (abfd, ex->section);
2850   in->info = H_GET_32 (abfd, ex->info);
2851 }
2852 
2853 /* Swap out an options header.  */
2854 
2855 void
2856 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2857 			       Elf_External_Options *ex)
2858 {
2859   H_PUT_8 (abfd, in->kind, ex->kind);
2860   H_PUT_8 (abfd, in->size, ex->size);
2861   H_PUT_16 (abfd, in->section, ex->section);
2862   H_PUT_32 (abfd, in->info, ex->info);
2863 }
2864 
2865 /* Swap in an abiflags structure.  */
2866 
2867 void
2868 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2869 				  const Elf_External_ABIFlags_v0 *ex,
2870 				  Elf_Internal_ABIFlags_v0 *in)
2871 {
2872   in->version = H_GET_16 (abfd, ex->version);
2873   in->isa_level = H_GET_8 (abfd, ex->isa_level);
2874   in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2875   in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2876   in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2877   in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2878   in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2879   in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2880   in->ases = H_GET_32 (abfd, ex->ases);
2881   in->flags1 = H_GET_32 (abfd, ex->flags1);
2882   in->flags2 = H_GET_32 (abfd, ex->flags2);
2883 }
2884 
2885 /* Swap out an abiflags structure.  */
2886 
2887 void
2888 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2889 				   const Elf_Internal_ABIFlags_v0 *in,
2890 				   Elf_External_ABIFlags_v0 *ex)
2891 {
2892   H_PUT_16 (abfd, in->version, ex->version);
2893   H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2894   H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2895   H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2896   H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2897   H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2898   H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2899   H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2900   H_PUT_32 (abfd, in->ases, ex->ases);
2901   H_PUT_32 (abfd, in->flags1, ex->flags1);
2902   H_PUT_32 (abfd, in->flags2, ex->flags2);
2903 }
2904 
2905 /* This function is called via qsort() to sort the dynamic relocation
2906    entries by increasing r_symndx value.  */
2907 
2908 static int
2909 sort_dynamic_relocs (const void *arg1, const void *arg2)
2910 {
2911   Elf_Internal_Rela int_reloc1;
2912   Elf_Internal_Rela int_reloc2;
2913   int diff;
2914 
2915   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2916   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2917 
2918   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2919   if (diff != 0)
2920     return diff;
2921 
2922   if (int_reloc1.r_offset < int_reloc2.r_offset)
2923     return -1;
2924   if (int_reloc1.r_offset > int_reloc2.r_offset)
2925     return 1;
2926   return 0;
2927 }
2928 
2929 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2930 
2931 static int
2932 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2933 			const void *arg2 ATTRIBUTE_UNUSED)
2934 {
2935 #ifdef BFD64
2936   Elf_Internal_Rela int_reloc1[3];
2937   Elf_Internal_Rela int_reloc2[3];
2938 
2939   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2940     (reldyn_sorting_bfd, arg1, int_reloc1);
2941   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2942     (reldyn_sorting_bfd, arg2, int_reloc2);
2943 
2944   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2945     return -1;
2946   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2947     return 1;
2948 
2949   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2950     return -1;
2951   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2952     return 1;
2953   return 0;
2954 #else
2955   abort ();
2956 #endif
2957 }
2958 
2959 
2960 /* This routine is used to write out ECOFF debugging external symbol
2961    information.  It is called via mips_elf_link_hash_traverse.  The
2962    ECOFF external symbol information must match the ELF external
2963    symbol information.  Unfortunately, at this point we don't know
2964    whether a symbol is required by reloc information, so the two
2965    tables may wind up being different.  We must sort out the external
2966    symbol information before we can set the final size of the .mdebug
2967    section, and we must set the size of the .mdebug section before we
2968    can relocate any sections, and we can't know which symbols are
2969    required by relocation until we relocate the sections.
2970    Fortunately, it is relatively unlikely that any symbol will be
2971    stripped but required by a reloc.  In particular, it can not happen
2972    when generating a final executable.  */
2973 
2974 static bool
2975 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2976 {
2977   struct extsym_info *einfo = data;
2978   bool strip;
2979   asection *sec, *output_section;
2980 
2981   if (h->root.indx == -2)
2982     strip = false;
2983   else if ((h->root.def_dynamic
2984 	    || h->root.ref_dynamic
2985 	    || h->root.type == bfd_link_hash_new)
2986 	   && !h->root.def_regular
2987 	   && !h->root.ref_regular)
2988     strip = true;
2989   else if (einfo->info->strip == strip_all
2990 	   || (einfo->info->strip == strip_some
2991 	       && bfd_hash_lookup (einfo->info->keep_hash,
2992 				   h->root.root.root.string,
2993 				   false, false) == NULL))
2994     strip = true;
2995   else
2996     strip = false;
2997 
2998   if (strip)
2999     return true;
3000 
3001   if (h->esym.ifd == -2)
3002     {
3003       h->esym.jmptbl = 0;
3004       h->esym.cobol_main = 0;
3005       h->esym.weakext = 0;
3006       h->esym.reserved = 0;
3007       h->esym.ifd = ifdNil;
3008       h->esym.asym.value = 0;
3009       h->esym.asym.st = stGlobal;
3010 
3011       if (h->root.root.type == bfd_link_hash_undefined
3012 	  || h->root.root.type == bfd_link_hash_undefweak)
3013 	{
3014 	  const char *name;
3015 
3016 	  /* Use undefined class.  Also, set class and type for some
3017 	     special symbols.  */
3018 	  name = h->root.root.root.string;
3019 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3020 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3021 	    {
3022 	      h->esym.asym.sc = scData;
3023 	      h->esym.asym.st = stLabel;
3024 	      h->esym.asym.value = 0;
3025 	    }
3026 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3027 	    {
3028 	      h->esym.asym.sc = scAbs;
3029 	      h->esym.asym.st = stLabel;
3030 	      h->esym.asym.value =
3031 		mips_elf_hash_table (einfo->info)->procedure_count;
3032 	    }
3033 	  else
3034 	    h->esym.asym.sc = scUndefined;
3035 	}
3036       else if (h->root.root.type != bfd_link_hash_defined
3037 	  && h->root.root.type != bfd_link_hash_defweak)
3038 	h->esym.asym.sc = scAbs;
3039       else
3040 	{
3041 	  const char *name;
3042 
3043 	  sec = h->root.root.u.def.section;
3044 	  output_section = sec->output_section;
3045 
3046 	  /* When making a shared library and symbol h is the one from
3047 	     the another shared library, OUTPUT_SECTION may be null.  */
3048 	  if (output_section == NULL)
3049 	    h->esym.asym.sc = scUndefined;
3050 	  else
3051 	    {
3052 	      name = bfd_section_name (output_section);
3053 
3054 	      if (strcmp (name, ".text") == 0)
3055 		h->esym.asym.sc = scText;
3056 	      else if (strcmp (name, ".data") == 0)
3057 		h->esym.asym.sc = scData;
3058 	      else if (strcmp (name, ".sdata") == 0)
3059 		h->esym.asym.sc = scSData;
3060 	      else if (strcmp (name, ".rodata") == 0
3061 		       || strcmp (name, ".rdata") == 0)
3062 		h->esym.asym.sc = scRData;
3063 	      else if (strcmp (name, ".bss") == 0)
3064 		h->esym.asym.sc = scBss;
3065 	      else if (strcmp (name, ".sbss") == 0)
3066 		h->esym.asym.sc = scSBss;
3067 	      else if (strcmp (name, ".init") == 0)
3068 		h->esym.asym.sc = scInit;
3069 	      else if (strcmp (name, ".fini") == 0)
3070 		h->esym.asym.sc = scFini;
3071 	      else
3072 		h->esym.asym.sc = scAbs;
3073 	    }
3074 	}
3075 
3076       h->esym.asym.reserved = 0;
3077       h->esym.asym.index = indexNil;
3078     }
3079 
3080   if (h->root.root.type == bfd_link_hash_common)
3081     h->esym.asym.value = h->root.root.u.c.size;
3082   else if (h->root.root.type == bfd_link_hash_defined
3083 	   || h->root.root.type == bfd_link_hash_defweak)
3084     {
3085       if (h->esym.asym.sc == scCommon)
3086 	h->esym.asym.sc = scBss;
3087       else if (h->esym.asym.sc == scSCommon)
3088 	h->esym.asym.sc = scSBss;
3089 
3090       sec = h->root.root.u.def.section;
3091       output_section = sec->output_section;
3092       if (output_section != NULL)
3093 	h->esym.asym.value = (h->root.root.u.def.value
3094 			      + sec->output_offset
3095 			      + output_section->vma);
3096       else
3097 	h->esym.asym.value = 0;
3098     }
3099   else
3100     {
3101       struct mips_elf_link_hash_entry *hd = h;
3102 
3103       while (hd->root.root.type == bfd_link_hash_indirect)
3104 	hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3105 
3106       if (hd->needs_lazy_stub)
3107 	{
3108 	  BFD_ASSERT (hd->root.plt.plist != NULL);
3109 	  BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3110 	  /* Set type and value for a symbol with a function stub.  */
3111 	  h->esym.asym.st = stProc;
3112 	  sec = hd->root.root.u.def.section;
3113 	  if (sec == NULL)
3114 	    h->esym.asym.value = 0;
3115 	  else
3116 	    {
3117 	      output_section = sec->output_section;
3118 	      if (output_section != NULL)
3119 		h->esym.asym.value = (hd->root.plt.plist->stub_offset
3120 				      + sec->output_offset
3121 				      + output_section->vma);
3122 	      else
3123 		h->esym.asym.value = 0;
3124 	    }
3125 	}
3126     }
3127 
3128   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3129 				      h->root.root.root.string,
3130 				      &h->esym))
3131     {
3132       einfo->failed = true;
3133       return false;
3134     }
3135 
3136   return true;
3137 }
3138 
3139 /* A comparison routine used to sort .gptab entries.  */
3140 
3141 static int
3142 gptab_compare (const void *p1, const void *p2)
3143 {
3144   const Elf32_gptab *a1 = p1;
3145   const Elf32_gptab *a2 = p2;
3146 
3147   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3148 }
3149 
3150 /* Functions to manage the got entry hash table.  */
3151 
3152 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3153    hash number.  */
3154 
3155 static inline hashval_t
3156 mips_elf_hash_bfd_vma (bfd_vma addr)
3157 {
3158 #ifdef BFD64
3159   return addr + (addr >> 32);
3160 #else
3161   return addr;
3162 #endif
3163 }
3164 
3165 static hashval_t
3166 mips_elf_got_entry_hash (const void *entry_)
3167 {
3168   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3169 
3170   return (entry->symndx
3171 	  + ((entry->tls_type == GOT_TLS_LDM) << 18)
3172 	  + (entry->tls_type == GOT_TLS_LDM ? 0
3173 	     : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3174 	     : entry->symndx >= 0 ? (entry->abfd->id
3175 				     + mips_elf_hash_bfd_vma (entry->d.addend))
3176 	     : entry->d.h->root.root.root.hash));
3177 }
3178 
3179 static int
3180 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3181 {
3182   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3183   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3184 
3185   return (e1->symndx == e2->symndx
3186 	  && e1->tls_type == e2->tls_type
3187 	  && (e1->tls_type == GOT_TLS_LDM ? true
3188 	      : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3189 	      : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3190 				   && e1->d.addend == e2->d.addend)
3191 	      : e2->abfd && e1->d.h == e2->d.h));
3192 }
3193 
3194 static hashval_t
3195 mips_got_page_ref_hash (const void *ref_)
3196 {
3197   const struct mips_got_page_ref *ref;
3198 
3199   ref = (const struct mips_got_page_ref *) ref_;
3200   return ((ref->symndx >= 0
3201 	   ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3202 	   : ref->u.h->root.root.root.hash)
3203 	  + mips_elf_hash_bfd_vma (ref->addend));
3204 }
3205 
3206 static int
3207 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3208 {
3209   const struct mips_got_page_ref *ref1, *ref2;
3210 
3211   ref1 = (const struct mips_got_page_ref *) ref1_;
3212   ref2 = (const struct mips_got_page_ref *) ref2_;
3213   return (ref1->symndx == ref2->symndx
3214 	  && (ref1->symndx < 0
3215 	      ? ref1->u.h == ref2->u.h
3216 	      : ref1->u.abfd == ref2->u.abfd)
3217 	  && ref1->addend == ref2->addend);
3218 }
3219 
3220 static hashval_t
3221 mips_got_page_entry_hash (const void *entry_)
3222 {
3223   const struct mips_got_page_entry *entry;
3224 
3225   entry = (const struct mips_got_page_entry *) entry_;
3226   return entry->sec->id;
3227 }
3228 
3229 static int
3230 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3231 {
3232   const struct mips_got_page_entry *entry1, *entry2;
3233 
3234   entry1 = (const struct mips_got_page_entry *) entry1_;
3235   entry2 = (const struct mips_got_page_entry *) entry2_;
3236   return entry1->sec == entry2->sec;
3237 }
3238 
3239 /* Create and return a new mips_got_info structure.  */
3240 
3241 static struct mips_got_info *
3242 mips_elf_create_got_info (bfd *abfd)
3243 {
3244   struct mips_got_info *g;
3245 
3246   g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3247   if (g == NULL)
3248     return NULL;
3249 
3250   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3251 				    mips_elf_got_entry_eq, NULL);
3252   if (g->got_entries == NULL)
3253     return NULL;
3254 
3255   g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3256 				      mips_got_page_ref_eq, NULL);
3257   if (g->got_page_refs == NULL)
3258     return NULL;
3259 
3260   return g;
3261 }
3262 
3263 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3264    CREATE_P and if ABFD doesn't already have a GOT.  */
3265 
3266 static struct mips_got_info *
3267 mips_elf_bfd_got (bfd *abfd, bool create_p)
3268 {
3269   struct mips_elf_obj_tdata *tdata;
3270 
3271   if (!is_mips_elf (abfd))
3272     return NULL;
3273 
3274   tdata = mips_elf_tdata (abfd);
3275   if (!tdata->got && create_p)
3276     tdata->got = mips_elf_create_got_info (abfd);
3277   return tdata->got;
3278 }
3279 
3280 /* Record that ABFD should use output GOT G.  */
3281 
3282 static void
3283 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3284 {
3285   struct mips_elf_obj_tdata *tdata;
3286 
3287   BFD_ASSERT (is_mips_elf (abfd));
3288   tdata = mips_elf_tdata (abfd);
3289   if (tdata->got)
3290     {
3291       /* The GOT structure itself and the hash table entries are
3292 	 allocated to a bfd, but the hash tables aren't.  */
3293       htab_delete (tdata->got->got_entries);
3294       htab_delete (tdata->got->got_page_refs);
3295       if (tdata->got->got_page_entries)
3296 	htab_delete (tdata->got->got_page_entries);
3297     }
3298   tdata->got = g;
3299 }
3300 
3301 /* Return the dynamic relocation section.  If it doesn't exist, try to
3302    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
3303    if creation fails.  */
3304 
3305 static asection *
3306 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3307 {
3308   const char *dname;
3309   asection *sreloc;
3310   bfd *dynobj;
3311 
3312   dname = MIPS_ELF_REL_DYN_NAME (info);
3313   dynobj = elf_hash_table (info)->dynobj;
3314   sreloc = bfd_get_linker_section (dynobj, dname);
3315   if (sreloc == NULL && create_p)
3316     {
3317       sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3318 						   (SEC_ALLOC
3319 						    | SEC_LOAD
3320 						    | SEC_HAS_CONTENTS
3321 						    | SEC_IN_MEMORY
3322 						    | SEC_LINKER_CREATED
3323 						    | SEC_READONLY));
3324       if (sreloc == NULL
3325 	  || !bfd_set_section_alignment (sreloc,
3326 					 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3327 	return NULL;
3328     }
3329   return sreloc;
3330 }
3331 
3332 /* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
3333 
3334 static int
3335 mips_elf_reloc_tls_type (unsigned int r_type)
3336 {
3337   if (tls_gd_reloc_p (r_type))
3338     return GOT_TLS_GD;
3339 
3340   if (tls_ldm_reloc_p (r_type))
3341     return GOT_TLS_LDM;
3342 
3343   if (tls_gottprel_reloc_p (r_type))
3344     return GOT_TLS_IE;
3345 
3346   return GOT_TLS_NONE;
3347 }
3348 
3349 /* Return the number of GOT slots needed for GOT TLS type TYPE.  */
3350 
3351 static int
3352 mips_tls_got_entries (unsigned int type)
3353 {
3354   switch (type)
3355     {
3356     case GOT_TLS_GD:
3357     case GOT_TLS_LDM:
3358       return 2;
3359 
3360     case GOT_TLS_IE:
3361       return 1;
3362 
3363     case GOT_TLS_NONE:
3364       return 0;
3365     }
3366   abort ();
3367 }
3368 
3369 /* Count the number of relocations needed for a TLS GOT entry, with
3370    access types from TLS_TYPE, and symbol H (or a local symbol if H
3371    is NULL).  */
3372 
3373 static int
3374 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3375 		     struct elf_link_hash_entry *h)
3376 {
3377   int indx = 0;
3378   bool need_relocs = false;
3379   bool dyn = elf_hash_table (info)->dynamic_sections_created;
3380 
3381   if (h != NULL
3382       && h->dynindx != -1
3383       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3384       && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3385     indx = h->dynindx;
3386 
3387   if ((bfd_link_dll (info) || indx != 0)
3388       && (h == NULL
3389 	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3390 	  || h->root.type != bfd_link_hash_undefweak))
3391     need_relocs = true;
3392 
3393   if (!need_relocs)
3394     return 0;
3395 
3396   switch (tls_type)
3397     {
3398     case GOT_TLS_GD:
3399       return indx != 0 ? 2 : 1;
3400 
3401     case GOT_TLS_IE:
3402       return 1;
3403 
3404     case GOT_TLS_LDM:
3405       return bfd_link_dll (info) ? 1 : 0;
3406 
3407     default:
3408       return 0;
3409     }
3410 }
3411 
3412 /* Add the number of GOT entries and TLS relocations required by ENTRY
3413    to G.  */
3414 
3415 static void
3416 mips_elf_count_got_entry (struct bfd_link_info *info,
3417 			  struct mips_got_info *g,
3418 			  struct mips_got_entry *entry)
3419 {
3420   if (entry->tls_type)
3421     {
3422       g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3423       g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3424 					entry->symndx < 0
3425 					? &entry->d.h->root : NULL);
3426     }
3427   else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3428     g->local_gotno += 1;
3429   else
3430     g->global_gotno += 1;
3431 }
3432 
3433 /* Output a simple dynamic relocation into SRELOC.  */
3434 
3435 static void
3436 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3437 				    asection *sreloc,
3438 				    unsigned long reloc_index,
3439 				    unsigned long indx,
3440 				    int r_type,
3441 				    bfd_vma offset)
3442 {
3443   Elf_Internal_Rela rel[3];
3444 
3445   memset (rel, 0, sizeof (rel));
3446 
3447   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3448   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3449 
3450   if (ABI_64_P (output_bfd))
3451     {
3452       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3453 	(output_bfd, &rel[0],
3454 	 (sreloc->contents
3455 	  + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3456     }
3457   else
3458     bfd_elf32_swap_reloc_out
3459       (output_bfd, &rel[0],
3460        (sreloc->contents
3461 	+ reloc_index * sizeof (Elf32_External_Rel)));
3462 }
3463 
3464 /* Initialize a set of TLS GOT entries for one symbol.  */
3465 
3466 static void
3467 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3468 			       struct mips_got_entry *entry,
3469 			       struct mips_elf_link_hash_entry *h,
3470 			       bfd_vma value)
3471 {
3472   bool dyn = elf_hash_table (info)->dynamic_sections_created;
3473   struct mips_elf_link_hash_table *htab;
3474   int indx;
3475   asection *sreloc, *sgot;
3476   bfd_vma got_offset, got_offset2;
3477   bool need_relocs = false;
3478 
3479   htab = mips_elf_hash_table (info);
3480   if (htab == NULL)
3481     return;
3482 
3483   sgot = htab->root.sgot;
3484 
3485   indx = 0;
3486   if (h != NULL
3487       && h->root.dynindx != -1
3488       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3489       && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3490     indx = h->root.dynindx;
3491 
3492   if (entry->tls_initialized)
3493     return;
3494 
3495   if ((bfd_link_dll (info) || indx != 0)
3496       && (h == NULL
3497 	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3498 	  || h->root.type != bfd_link_hash_undefweak))
3499     need_relocs = true;
3500 
3501   /* MINUS_ONE means the symbol is not defined in this object.  It may not
3502      be defined at all; assume that the value doesn't matter in that
3503      case.  Otherwise complain if we would use the value.  */
3504   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3505 	      || h->root.root.type == bfd_link_hash_undefweak);
3506 
3507   /* Emit necessary relocations.  */
3508   sreloc = mips_elf_rel_dyn_section (info, false);
3509   got_offset = entry->gotidx;
3510 
3511   switch (entry->tls_type)
3512     {
3513     case GOT_TLS_GD:
3514       /* General Dynamic.  */
3515       got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3516 
3517       if (need_relocs)
3518 	{
3519 	  mips_elf_output_dynamic_relocation
3520 	    (abfd, sreloc, sreloc->reloc_count++, indx,
3521 	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3522 	     sgot->output_offset + sgot->output_section->vma + got_offset);
3523 
3524 	  if (indx)
3525 	    mips_elf_output_dynamic_relocation
3526 	      (abfd, sreloc, sreloc->reloc_count++, indx,
3527 	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3528 	       sgot->output_offset + sgot->output_section->vma + got_offset2);
3529 	  else
3530 	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3531 			       sgot->contents + got_offset2);
3532 	}
3533       else
3534 	{
3535 	  MIPS_ELF_PUT_WORD (abfd, 1,
3536 			     sgot->contents + got_offset);
3537 	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3538 			     sgot->contents + got_offset2);
3539 	}
3540       break;
3541 
3542     case GOT_TLS_IE:
3543       /* Initial Exec model.  */
3544       if (need_relocs)
3545 	{
3546 	  if (indx == 0)
3547 	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3548 			       sgot->contents + got_offset);
3549 	  else
3550 	    MIPS_ELF_PUT_WORD (abfd, 0,
3551 			       sgot->contents + got_offset);
3552 
3553 	  mips_elf_output_dynamic_relocation
3554 	    (abfd, sreloc, sreloc->reloc_count++, indx,
3555 	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3556 	     sgot->output_offset + sgot->output_section->vma + got_offset);
3557 	}
3558       else
3559 	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3560 			   sgot->contents + got_offset);
3561       break;
3562 
3563     case GOT_TLS_LDM:
3564       /* The initial offset is zero, and the LD offsets will include the
3565 	 bias by DTP_OFFSET.  */
3566       MIPS_ELF_PUT_WORD (abfd, 0,
3567 			 sgot->contents + got_offset
3568 			 + MIPS_ELF_GOT_SIZE (abfd));
3569 
3570       if (!bfd_link_dll (info))
3571 	MIPS_ELF_PUT_WORD (abfd, 1,
3572 			   sgot->contents + got_offset);
3573       else
3574 	mips_elf_output_dynamic_relocation
3575 	  (abfd, sreloc, sreloc->reloc_count++, indx,
3576 	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3577 	   sgot->output_offset + sgot->output_section->vma + got_offset);
3578       break;
3579 
3580     default:
3581       abort ();
3582     }
3583 
3584   entry->tls_initialized = true;
3585 }
3586 
3587 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3588    for global symbol H.  .got.plt comes before the GOT, so the offset
3589    will be negative.  */
3590 
3591 static bfd_vma
3592 mips_elf_gotplt_index (struct bfd_link_info *info,
3593 		       struct elf_link_hash_entry *h)
3594 {
3595   bfd_vma got_address, got_value;
3596   struct mips_elf_link_hash_table *htab;
3597 
3598   htab = mips_elf_hash_table (info);
3599   BFD_ASSERT (htab != NULL);
3600 
3601   BFD_ASSERT (h->plt.plist != NULL);
3602   BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3603 
3604   /* Calculate the address of the associated .got.plt entry.  */
3605   got_address = (htab->root.sgotplt->output_section->vma
3606 		 + htab->root.sgotplt->output_offset
3607 		 + (h->plt.plist->gotplt_index
3608 		    * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3609 
3610   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
3611   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3612 	       + htab->root.hgot->root.u.def.section->output_offset
3613 	       + htab->root.hgot->root.u.def.value);
3614 
3615   return got_address - got_value;
3616 }
3617 
3618 /* Return the GOT offset for address VALUE.   If there is not yet a GOT
3619    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3620    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3621    offset can be found.  */
3622 
3623 static bfd_vma
3624 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3625 			  bfd_vma value, unsigned long r_symndx,
3626 			  struct mips_elf_link_hash_entry *h, int r_type)
3627 {
3628   struct mips_elf_link_hash_table *htab;
3629   struct mips_got_entry *entry;
3630 
3631   htab = mips_elf_hash_table (info);
3632   BFD_ASSERT (htab != NULL);
3633 
3634   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3635 					   r_symndx, h, r_type);
3636   if (!entry)
3637     return MINUS_ONE;
3638 
3639   if (entry->tls_type)
3640     mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3641   return entry->gotidx;
3642 }
3643 
3644 /* Return the GOT index of global symbol H in the primary GOT.  */
3645 
3646 static bfd_vma
3647 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3648 				   struct elf_link_hash_entry *h)
3649 {
3650   struct mips_elf_link_hash_table *htab;
3651   long global_got_dynindx;
3652   struct mips_got_info *g;
3653   bfd_vma got_index;
3654 
3655   htab = mips_elf_hash_table (info);
3656   BFD_ASSERT (htab != NULL);
3657 
3658   global_got_dynindx = 0;
3659   if (htab->global_gotsym != NULL)
3660     global_got_dynindx = htab->global_gotsym->dynindx;
3661 
3662   /* Once we determine the global GOT entry with the lowest dynamic
3663      symbol table index, we must put all dynamic symbols with greater
3664      indices into the primary GOT.  That makes it easy to calculate the
3665      GOT offset.  */
3666   BFD_ASSERT (h->dynindx >= global_got_dynindx);
3667   g = mips_elf_bfd_got (obfd, false);
3668   got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3669 	       * MIPS_ELF_GOT_SIZE (obfd));
3670   BFD_ASSERT (got_index < htab->root.sgot->size);
3671 
3672   return got_index;
3673 }
3674 
3675 /* Return the GOT index for the global symbol indicated by H, which is
3676    referenced by a relocation of type R_TYPE in IBFD.  */
3677 
3678 static bfd_vma
3679 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3680 			   struct elf_link_hash_entry *h, int r_type)
3681 {
3682   struct mips_elf_link_hash_table *htab;
3683   struct mips_got_info *g;
3684   struct mips_got_entry lookup, *entry;
3685   bfd_vma gotidx;
3686 
3687   htab = mips_elf_hash_table (info);
3688   BFD_ASSERT (htab != NULL);
3689 
3690   g = mips_elf_bfd_got (ibfd, false);
3691   BFD_ASSERT (g);
3692 
3693   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3694   if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3695     return mips_elf_primary_global_got_index (obfd, info, h);
3696 
3697   lookup.abfd = ibfd;
3698   lookup.symndx = -1;
3699   lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3700   entry = htab_find (g->got_entries, &lookup);
3701   BFD_ASSERT (entry);
3702 
3703   gotidx = entry->gotidx;
3704   BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3705 
3706   if (lookup.tls_type)
3707     {
3708       bfd_vma value = MINUS_ONE;
3709 
3710       if ((h->root.type == bfd_link_hash_defined
3711 	   || h->root.type == bfd_link_hash_defweak)
3712 	  && h->root.u.def.section->output_section)
3713 	value = (h->root.u.def.value
3714 		 + h->root.u.def.section->output_offset
3715 		 + h->root.u.def.section->output_section->vma);
3716 
3717       mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3718     }
3719   return gotidx;
3720 }
3721 
3722 /* Find a GOT page entry that points to within 32KB of VALUE.  These
3723    entries are supposed to be placed at small offsets in the GOT, i.e.,
3724    within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3725    entry could be created.  If OFFSETP is nonnull, use it to return the
3726    offset of the GOT entry from VALUE.  */
3727 
3728 static bfd_vma
3729 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3730 		   bfd_vma value, bfd_vma *offsetp)
3731 {
3732   bfd_vma page, got_index;
3733   struct mips_got_entry *entry;
3734 
3735   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3736   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3737 					   NULL, R_MIPS_GOT_PAGE);
3738 
3739   if (!entry)
3740     return MINUS_ONE;
3741 
3742   got_index = entry->gotidx;
3743 
3744   if (offsetp)
3745     *offsetp = value - entry->d.address;
3746 
3747   return got_index;
3748 }
3749 
3750 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3751    EXTERNAL is true if the relocation was originally against a global
3752    symbol that binds locally.  */
3753 
3754 static bfd_vma
3755 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3756 		      bfd_vma value, bool external)
3757 {
3758   struct mips_got_entry *entry;
3759 
3760   /* GOT16 relocations against local symbols are followed by a LO16
3761      relocation; those against global symbols are not.  Thus if the
3762      symbol was originally local, the GOT16 relocation should load the
3763      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3764   if (! external)
3765     value = mips_elf_high (value) << 16;
3766 
3767   /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3768      R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3769      same in all cases.  */
3770   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3771 					   NULL, R_MIPS_GOT16);
3772   if (entry)
3773     return entry->gotidx;
3774   else
3775     return MINUS_ONE;
3776 }
3777 
3778 /* Returns the offset for the entry at the INDEXth position
3779    in the GOT.  */
3780 
3781 static bfd_vma
3782 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3783 				bfd *input_bfd, bfd_vma got_index)
3784 {
3785   struct mips_elf_link_hash_table *htab;
3786   asection *sgot;
3787   bfd_vma gp;
3788 
3789   htab = mips_elf_hash_table (info);
3790   BFD_ASSERT (htab != NULL);
3791 
3792   sgot = htab->root.sgot;
3793   gp = _bfd_get_gp_value (output_bfd)
3794     + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3795 
3796   return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3797 }
3798 
3799 /* Create and return a local GOT entry for VALUE, which was calculated
3800    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3801    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3802    instead.  */
3803 
3804 static struct mips_got_entry *
3805 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3806 				 bfd *ibfd, bfd_vma value,
3807 				 unsigned long r_symndx,
3808 				 struct mips_elf_link_hash_entry *h,
3809 				 int r_type)
3810 {
3811   struct mips_got_entry lookup, *entry;
3812   void **loc;
3813   struct mips_got_info *g;
3814   struct mips_elf_link_hash_table *htab;
3815   bfd_vma gotidx;
3816 
3817   htab = mips_elf_hash_table (info);
3818   BFD_ASSERT (htab != NULL);
3819 
3820   g = mips_elf_bfd_got (ibfd, false);
3821   if (g == NULL)
3822     {
3823       g = mips_elf_bfd_got (abfd, false);
3824       BFD_ASSERT (g != NULL);
3825     }
3826 
3827   /* This function shouldn't be called for symbols that live in the global
3828      area of the GOT.  */
3829   BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3830 
3831   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3832   if (lookup.tls_type)
3833     {
3834       lookup.abfd = ibfd;
3835       if (tls_ldm_reloc_p (r_type))
3836 	{
3837 	  lookup.symndx = 0;
3838 	  lookup.d.addend = 0;
3839 	}
3840       else if (h == NULL)
3841 	{
3842 	  lookup.symndx = r_symndx;
3843 	  lookup.d.addend = 0;
3844 	}
3845       else
3846 	{
3847 	  lookup.symndx = -1;
3848 	  lookup.d.h = h;
3849 	}
3850 
3851       entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3852       BFD_ASSERT (entry);
3853 
3854       gotidx = entry->gotidx;
3855       BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3856 
3857       return entry;
3858     }
3859 
3860   lookup.abfd = NULL;
3861   lookup.symndx = -1;
3862   lookup.d.address = value;
3863   loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3864   if (!loc)
3865     return NULL;
3866 
3867   entry = (struct mips_got_entry *) *loc;
3868   if (entry)
3869     return entry;
3870 
3871   if (g->assigned_low_gotno > g->assigned_high_gotno)
3872     {
3873       /* We didn't allocate enough space in the GOT.  */
3874       _bfd_error_handler
3875 	(_("not enough GOT space for local GOT entries"));
3876       bfd_set_error (bfd_error_bad_value);
3877       return NULL;
3878     }
3879 
3880   entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3881   if (!entry)
3882     return NULL;
3883 
3884   if (got16_reloc_p (r_type)
3885       || call16_reloc_p (r_type)
3886       || got_page_reloc_p (r_type)
3887       || got_disp_reloc_p (r_type))
3888     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3889   else
3890     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3891 
3892   *entry = lookup;
3893   *loc = entry;
3894 
3895   MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3896 
3897   /* These GOT entries need a dynamic relocation on VxWorks.  */
3898   if (htab->root.target_os == is_vxworks)
3899     {
3900       Elf_Internal_Rela outrel;
3901       asection *s;
3902       bfd_byte *rloc;
3903       bfd_vma got_address;
3904 
3905       s = mips_elf_rel_dyn_section (info, false);
3906       got_address = (htab->root.sgot->output_section->vma
3907 		     + htab->root.sgot->output_offset
3908 		     + entry->gotidx);
3909 
3910       rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3911       outrel.r_offset = got_address;
3912       outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3913       outrel.r_addend = value;
3914       bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3915     }
3916 
3917   return entry;
3918 }
3919 
3920 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3921    The number might be exact or a worst-case estimate, depending on how
3922    much information is available to elf_backend_omit_section_dynsym at
3923    the current linking stage.  */
3924 
3925 static bfd_size_type
3926 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3927 {
3928   bfd_size_type count;
3929 
3930   count = 0;
3931   if (bfd_link_pic (info))
3932     {
3933       asection *p;
3934       const struct elf_backend_data *bed;
3935 
3936       bed = get_elf_backend_data (output_bfd);
3937       for (p = output_bfd->sections; p ; p = p->next)
3938 	if ((p->flags & SEC_EXCLUDE) == 0
3939 	    && (p->flags & SEC_ALLOC) != 0
3940 	    && elf_hash_table (info)->dynamic_relocs
3941 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3942 	  ++count;
3943     }
3944   return count;
3945 }
3946 
3947 /* Sort the dynamic symbol table so that symbols that need GOT entries
3948    appear towards the end.  */
3949 
3950 static bool
3951 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3952 {
3953   struct mips_elf_link_hash_table *htab;
3954   struct mips_elf_hash_sort_data hsd;
3955   struct mips_got_info *g;
3956 
3957   htab = mips_elf_hash_table (info);
3958   BFD_ASSERT (htab != NULL);
3959 
3960   if (htab->root.dynsymcount == 0)
3961     return true;
3962 
3963   g = htab->got_info;
3964   if (g == NULL)
3965     return true;
3966 
3967   hsd.low = NULL;
3968   hsd.max_unref_got_dynindx
3969     = hsd.min_got_dynindx
3970     = (htab->root.dynsymcount - g->reloc_only_gotno);
3971   /* Add 1 to local symbol indices to account for the mandatory NULL entry
3972      at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
3973   hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3974   hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3975   hsd.output_bfd = abfd;
3976   if (htab->root.dynobj != NULL
3977       && htab->root.dynamic_sections_created
3978       && info->emit_gnu_hash)
3979     {
3980       asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3981       BFD_ASSERT (s != NULL);
3982       hsd.mipsxhash = s->contents;
3983       BFD_ASSERT (hsd.mipsxhash != NULL);
3984     }
3985   else
3986     hsd.mipsxhash = NULL;
3987   mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3988 
3989   /* There should have been enough room in the symbol table to
3990      accommodate both the GOT and non-GOT symbols.  */
3991   BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3992   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3993   BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3994   BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3995 
3996   /* Now we know which dynamic symbol has the lowest dynamic symbol
3997      table index in the GOT.  */
3998   htab->global_gotsym = hsd.low;
3999 
4000   return true;
4001 }
4002 
4003 /* If H needs a GOT entry, assign it the highest available dynamic
4004    index.  Otherwise, assign it the lowest available dynamic
4005    index.  */
4006 
4007 static bool
4008 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4009 {
4010   struct mips_elf_hash_sort_data *hsd = data;
4011 
4012   /* Symbols without dynamic symbol table entries aren't interesting
4013      at all.  */
4014   if (h->root.dynindx == -1)
4015     return true;
4016 
4017   switch (h->global_got_area)
4018     {
4019     case GGA_NONE:
4020       if (h->root.forced_local)
4021 	h->root.dynindx = hsd->max_local_dynindx++;
4022       else
4023 	h->root.dynindx = hsd->max_non_got_dynindx++;
4024       break;
4025 
4026     case GGA_NORMAL:
4027       h->root.dynindx = --hsd->min_got_dynindx;
4028       hsd->low = (struct elf_link_hash_entry *) h;
4029       break;
4030 
4031     case GGA_RELOC_ONLY:
4032       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4033 	hsd->low = (struct elf_link_hash_entry *) h;
4034       h->root.dynindx = hsd->max_unref_got_dynindx++;
4035       break;
4036     }
4037 
4038   /* Populate the .MIPS.xhash translation table entry with
4039      the symbol dynindx.  */
4040   if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4041     bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4042 		hsd->mipsxhash + h->mipsxhash_loc);
4043 
4044   return true;
4045 }
4046 
4047 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4048    (which is owned by the caller and shouldn't be added to the
4049    hash table directly).  */
4050 
4051 static bool
4052 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4053 			   struct mips_got_entry *lookup)
4054 {
4055   struct mips_elf_link_hash_table *htab;
4056   struct mips_got_entry *entry;
4057   struct mips_got_info *g;
4058   void **loc, **bfd_loc;
4059 
4060   /* Make sure there's a slot for this entry in the master GOT.  */
4061   htab = mips_elf_hash_table (info);
4062   g = htab->got_info;
4063   loc = htab_find_slot (g->got_entries, lookup, INSERT);
4064   if (!loc)
4065     return false;
4066 
4067   /* Populate the entry if it isn't already.  */
4068   entry = (struct mips_got_entry *) *loc;
4069   if (!entry)
4070     {
4071       entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4072       if (!entry)
4073 	return false;
4074 
4075       lookup->tls_initialized = false;
4076       lookup->gotidx = -1;
4077       *entry = *lookup;
4078       *loc = entry;
4079     }
4080 
4081   /* Reuse the same GOT entry for the BFD's GOT.  */
4082   g = mips_elf_bfd_got (abfd, true);
4083   if (!g)
4084     return false;
4085 
4086   bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4087   if (!bfd_loc)
4088     return false;
4089 
4090   if (!*bfd_loc)
4091     *bfd_loc = entry;
4092   return true;
4093 }
4094 
4095 /* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
4096    entry for it.  FOR_CALL is true if the caller is only interested in
4097    using the GOT entry for calls.  */
4098 
4099 static bool
4100 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4101 				   bfd *abfd, struct bfd_link_info *info,
4102 				   bool for_call, int r_type)
4103 {
4104   struct mips_elf_link_hash_table *htab;
4105   struct mips_elf_link_hash_entry *hmips;
4106   struct mips_got_entry entry;
4107   unsigned char tls_type;
4108 
4109   htab = mips_elf_hash_table (info);
4110   BFD_ASSERT (htab != NULL);
4111 
4112   hmips = (struct mips_elf_link_hash_entry *) h;
4113   if (!for_call)
4114     hmips->got_only_for_calls = false;
4115 
4116   /* A global symbol in the GOT must also be in the dynamic symbol
4117      table.  */
4118   if (h->dynindx == -1)
4119     {
4120       switch (ELF_ST_VISIBILITY (h->other))
4121 	{
4122 	case STV_INTERNAL:
4123 	case STV_HIDDEN:
4124 	  _bfd_mips_elf_hide_symbol (info, h, true);
4125 	  break;
4126 	}
4127       if (!bfd_elf_link_record_dynamic_symbol (info, h))
4128 	return false;
4129     }
4130 
4131   tls_type = mips_elf_reloc_tls_type (r_type);
4132   if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4133     hmips->global_got_area = GGA_NORMAL;
4134 
4135   entry.abfd = abfd;
4136   entry.symndx = -1;
4137   entry.d.h = (struct mips_elf_link_hash_entry *) h;
4138   entry.tls_type = tls_type;
4139   return mips_elf_record_got_entry (info, abfd, &entry);
4140 }
4141 
4142 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4143    where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
4144 
4145 static bool
4146 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4147 				  struct bfd_link_info *info, int r_type)
4148 {
4149   struct mips_elf_link_hash_table *htab;
4150   struct mips_got_info *g;
4151   struct mips_got_entry entry;
4152 
4153   htab = mips_elf_hash_table (info);
4154   BFD_ASSERT (htab != NULL);
4155 
4156   g = htab->got_info;
4157   BFD_ASSERT (g != NULL);
4158 
4159   entry.abfd = abfd;
4160   entry.symndx = symndx;
4161   entry.d.addend = addend;
4162   entry.tls_type = mips_elf_reloc_tls_type (r_type);
4163   return mips_elf_record_got_entry (info, abfd, &entry);
4164 }
4165 
4166 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4167    H is the symbol's hash table entry, or null if SYMNDX is local
4168    to ABFD.  */
4169 
4170 static bool
4171 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4172 			      long symndx, struct elf_link_hash_entry *h,
4173 			      bfd_signed_vma addend)
4174 {
4175   struct mips_elf_link_hash_table *htab;
4176   struct mips_got_info *g1, *g2;
4177   struct mips_got_page_ref lookup, *entry;
4178   void **loc, **bfd_loc;
4179 
4180   htab = mips_elf_hash_table (info);
4181   BFD_ASSERT (htab != NULL);
4182 
4183   g1 = htab->got_info;
4184   BFD_ASSERT (g1 != NULL);
4185 
4186   if (h)
4187     {
4188       lookup.symndx = -1;
4189       lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4190     }
4191   else
4192     {
4193       lookup.symndx = symndx;
4194       lookup.u.abfd = abfd;
4195     }
4196   lookup.addend = addend;
4197   loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4198   if (loc == NULL)
4199     return false;
4200 
4201   entry = (struct mips_got_page_ref *) *loc;
4202   if (!entry)
4203     {
4204       entry = bfd_alloc (abfd, sizeof (*entry));
4205       if (!entry)
4206 	return false;
4207 
4208       *entry = lookup;
4209       *loc = entry;
4210     }
4211 
4212   /* Add the same entry to the BFD's GOT.  */
4213   g2 = mips_elf_bfd_got (abfd, true);
4214   if (!g2)
4215     return false;
4216 
4217   bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4218   if (!bfd_loc)
4219     return false;
4220 
4221   if (!*bfd_loc)
4222     *bfd_loc = entry;
4223 
4224   return true;
4225 }
4226 
4227 /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
4228 
4229 static void
4230 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4231 				       unsigned int n)
4232 {
4233   asection *s;
4234   struct mips_elf_link_hash_table *htab;
4235 
4236   htab = mips_elf_hash_table (info);
4237   BFD_ASSERT (htab != NULL);
4238 
4239   s = mips_elf_rel_dyn_section (info, false);
4240   BFD_ASSERT (s != NULL);
4241 
4242   if (htab->root.target_os == is_vxworks)
4243     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4244   else
4245     {
4246       if (s->size == 0)
4247 	{
4248 	  /* Make room for a null element.  */
4249 	  s->size += MIPS_ELF_REL_SIZE (abfd);
4250 	  ++s->reloc_count;
4251 	}
4252       s->size += n * MIPS_ELF_REL_SIZE (abfd);
4253     }
4254 }
4255 
4256 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4257    mips_elf_traverse_got_arg structure.  Count the number of GOT
4258    entries and TLS relocs.  Set DATA->value to true if we need
4259    to resolve indirect or warning symbols and then recreate the GOT.  */
4260 
4261 static int
4262 mips_elf_check_recreate_got (void **entryp, void *data)
4263 {
4264   struct mips_got_entry *entry;
4265   struct mips_elf_traverse_got_arg *arg;
4266 
4267   entry = (struct mips_got_entry *) *entryp;
4268   arg = (struct mips_elf_traverse_got_arg *) data;
4269   if (entry->abfd != NULL && entry->symndx == -1)
4270     {
4271       struct mips_elf_link_hash_entry *h;
4272 
4273       h = entry->d.h;
4274       if (h->root.root.type == bfd_link_hash_indirect
4275 	  || h->root.root.type == bfd_link_hash_warning)
4276 	{
4277 	  arg->value = true;
4278 	  return 0;
4279 	}
4280     }
4281   mips_elf_count_got_entry (arg->info, arg->g, entry);
4282   return 1;
4283 }
4284 
4285 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4286    mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
4287    converting entries for indirect and warning symbols into entries
4288    for the target symbol.  Set DATA->g to null on error.  */
4289 
4290 static int
4291 mips_elf_recreate_got (void **entryp, void *data)
4292 {
4293   struct mips_got_entry new_entry, *entry;
4294   struct mips_elf_traverse_got_arg *arg;
4295   void **slot;
4296 
4297   entry = (struct mips_got_entry *) *entryp;
4298   arg = (struct mips_elf_traverse_got_arg *) data;
4299   if (entry->abfd != NULL
4300       && entry->symndx == -1
4301       && (entry->d.h->root.root.type == bfd_link_hash_indirect
4302 	  || entry->d.h->root.root.type == bfd_link_hash_warning))
4303     {
4304       struct mips_elf_link_hash_entry *h;
4305 
4306       new_entry = *entry;
4307       entry = &new_entry;
4308       h = entry->d.h;
4309       do
4310 	{
4311 	  BFD_ASSERT (h->global_got_area == GGA_NONE);
4312 	  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4313 	}
4314       while (h->root.root.type == bfd_link_hash_indirect
4315 	     || h->root.root.type == bfd_link_hash_warning);
4316       entry->d.h = h;
4317     }
4318   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4319   if (slot == NULL)
4320     {
4321       arg->g = NULL;
4322       return 0;
4323     }
4324   if (*slot == NULL)
4325     {
4326       if (entry == &new_entry)
4327 	{
4328 	  entry = bfd_alloc (entry->abfd, sizeof (*entry));
4329 	  if (!entry)
4330 	    {
4331 	      arg->g = NULL;
4332 	      return 0;
4333 	    }
4334 	  *entry = new_entry;
4335 	}
4336       *slot = entry;
4337       mips_elf_count_got_entry (arg->info, arg->g, entry);
4338     }
4339   return 1;
4340 }
4341 
4342 /* Return the maximum number of GOT page entries required for RANGE.  */
4343 
4344 static bfd_vma
4345 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4346 {
4347   return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4348 }
4349 
4350 /* Record that G requires a page entry that can reach SEC + ADDEND.  */
4351 
4352 static bool
4353 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4354 				asection *sec, bfd_signed_vma addend)
4355 {
4356   struct mips_got_info *g = arg->g;
4357   struct mips_got_page_entry lookup, *entry;
4358   struct mips_got_page_range **range_ptr, *range;
4359   bfd_vma old_pages, new_pages;
4360   void **loc;
4361 
4362   /* Find the mips_got_page_entry hash table entry for this section.  */
4363   lookup.sec = sec;
4364   loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4365   if (loc == NULL)
4366     return false;
4367 
4368   /* Create a mips_got_page_entry if this is the first time we've
4369      seen the section.  */
4370   entry = (struct mips_got_page_entry *) *loc;
4371   if (!entry)
4372     {
4373       entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4374       if (!entry)
4375 	return false;
4376 
4377       entry->sec = sec;
4378       *loc = entry;
4379     }
4380 
4381   /* Skip over ranges whose maximum extent cannot share a page entry
4382      with ADDEND.  */
4383   range_ptr = &entry->ranges;
4384   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4385     range_ptr = &(*range_ptr)->next;
4386 
4387   /* If we scanned to the end of the list, or found a range whose
4388      minimum extent cannot share a page entry with ADDEND, create
4389      a new singleton range.  */
4390   range = *range_ptr;
4391   if (!range || addend < range->min_addend - 0xffff)
4392     {
4393       range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4394       if (!range)
4395 	return false;
4396 
4397       range->next = *range_ptr;
4398       range->min_addend = addend;
4399       range->max_addend = addend;
4400 
4401       *range_ptr = range;
4402       entry->num_pages++;
4403       g->page_gotno++;
4404       return true;
4405     }
4406 
4407   /* Remember how many pages the old range contributed.  */
4408   old_pages = mips_elf_pages_for_range (range);
4409 
4410   /* Update the ranges.  */
4411   if (addend < range->min_addend)
4412     range->min_addend = addend;
4413   else if (addend > range->max_addend)
4414     {
4415       if (range->next && addend >= range->next->min_addend - 0xffff)
4416 	{
4417 	  old_pages += mips_elf_pages_for_range (range->next);
4418 	  range->max_addend = range->next->max_addend;
4419 	  range->next = range->next->next;
4420 	}
4421       else
4422 	range->max_addend = addend;
4423     }
4424 
4425   /* Record any change in the total estimate.  */
4426   new_pages = mips_elf_pages_for_range (range);
4427   if (old_pages != new_pages)
4428     {
4429       entry->num_pages += new_pages - old_pages;
4430       g->page_gotno += new_pages - old_pages;
4431     }
4432 
4433   return true;
4434 }
4435 
4436 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4437    and for which DATA points to a mips_elf_traverse_got_arg.  Work out
4438    whether the page reference described by *REFP needs a GOT page entry,
4439    and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
4440 
4441 static int
4442 mips_elf_resolve_got_page_ref (void **refp, void *data)
4443 {
4444   struct mips_got_page_ref *ref;
4445   struct mips_elf_traverse_got_arg *arg;
4446   struct mips_elf_link_hash_table *htab;
4447   asection *sec;
4448   bfd_vma addend;
4449 
4450   ref = (struct mips_got_page_ref *) *refp;
4451   arg = (struct mips_elf_traverse_got_arg *) data;
4452   htab = mips_elf_hash_table (arg->info);
4453 
4454   if (ref->symndx < 0)
4455     {
4456       struct mips_elf_link_hash_entry *h;
4457 
4458       /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
4459       h = ref->u.h;
4460       if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4461 	return 1;
4462 
4463       /* Ignore undefined symbols; we'll issue an error later if
4464 	 appropriate.  */
4465       if (!((h->root.root.type == bfd_link_hash_defined
4466 	     || h->root.root.type == bfd_link_hash_defweak)
4467 	    && h->root.root.u.def.section))
4468 	return 1;
4469 
4470       sec = h->root.root.u.def.section;
4471       addend = h->root.root.u.def.value + ref->addend;
4472     }
4473   else
4474     {
4475       Elf_Internal_Sym *isym;
4476 
4477       /* Read in the symbol.  */
4478       isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4479 				    ref->symndx);
4480       if (isym == NULL)
4481 	{
4482 	  arg->g = NULL;
4483 	  return 0;
4484 	}
4485 
4486       /* Get the associated input section.  */
4487       sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4488       if (sec == NULL)
4489 	{
4490 	  arg->g = NULL;
4491 	  return 0;
4492 	}
4493 
4494       /* If this is a mergable section, work out the section and offset
4495 	 of the merged data.  For section symbols, the addend specifies
4496 	 of the offset _of_ the first byte in the data, otherwise it
4497 	 specifies the offset _from_ the first byte.  */
4498       if (sec->flags & SEC_MERGE)
4499 	{
4500 	  void *secinfo;
4501 
4502 	  secinfo = elf_section_data (sec)->sec_info;
4503 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4504 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4505 						 isym->st_value + ref->addend);
4506 	  else
4507 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4508 						 isym->st_value) + ref->addend;
4509 	}
4510       else
4511 	addend = isym->st_value + ref->addend;
4512     }
4513   if (!mips_elf_record_got_page_entry (arg, sec, addend))
4514     {
4515       arg->g = NULL;
4516       return 0;
4517     }
4518   return 1;
4519 }
4520 
4521 /* If any entries in G->got_entries are for indirect or warning symbols,
4522    replace them with entries for the target symbol.  Convert g->got_page_refs
4523    into got_page_entry structures and estimate the number of page entries
4524    that they require.  */
4525 
4526 static bool
4527 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4528 				    struct mips_got_info *g)
4529 {
4530   struct mips_elf_traverse_got_arg tga;
4531   struct mips_got_info oldg;
4532 
4533   oldg = *g;
4534 
4535   tga.info = info;
4536   tga.g = g;
4537   tga.value = false;
4538   htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4539   if (tga.value)
4540     {
4541       *g = oldg;
4542       g->got_entries = htab_create (htab_size (oldg.got_entries),
4543 				    mips_elf_got_entry_hash,
4544 				    mips_elf_got_entry_eq, NULL);
4545       if (!g->got_entries)
4546 	return false;
4547 
4548       htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4549       if (!tga.g)
4550 	return false;
4551 
4552       htab_delete (oldg.got_entries);
4553     }
4554 
4555   g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4556 					 mips_got_page_entry_eq, NULL);
4557   if (g->got_page_entries == NULL)
4558     return false;
4559 
4560   tga.info = info;
4561   tga.g = g;
4562   htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4563 
4564   return true;
4565 }
4566 
4567 /* Return true if a GOT entry for H should live in the local rather than
4568    global GOT area.  */
4569 
4570 static bool
4571 mips_use_local_got_p (struct bfd_link_info *info,
4572 		      struct mips_elf_link_hash_entry *h)
4573 {
4574   /* Symbols that aren't in the dynamic symbol table must live in the
4575      local GOT.  This includes symbols that are completely undefined
4576      and which therefore don't bind locally.  We'll report undefined
4577      symbols later if appropriate.  */
4578   if (h->root.dynindx == -1)
4579     return true;
4580 
4581   /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4582      to the local GOT, as they would be implicitly relocated by the
4583      base address by the dynamic loader.  */
4584   if (bfd_is_abs_symbol (&h->root.root))
4585     return false;
4586 
4587   /* Symbols that bind locally can (and in the case of forced-local
4588      symbols, must) live in the local GOT.  */
4589   if (h->got_only_for_calls
4590       ? SYMBOL_CALLS_LOCAL (info, &h->root)
4591       : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4592     return true;
4593 
4594   /* If this is an executable that must provide a definition of the symbol,
4595      either though PLTs or copy relocations, then that address should go in
4596      the local rather than global GOT.  */
4597   if (bfd_link_executable (info) && h->has_static_relocs)
4598     return true;
4599 
4600   return false;
4601 }
4602 
4603 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4604    link_info structure.  Decide whether the hash entry needs an entry in
4605    the global part of the primary GOT, setting global_got_area accordingly.
4606    Count the number of global symbols that are in the primary GOT only
4607    because they have relocations against them (reloc_only_gotno).  */
4608 
4609 static bool
4610 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4611 {
4612   struct bfd_link_info *info;
4613   struct mips_elf_link_hash_table *htab;
4614   struct mips_got_info *g;
4615 
4616   info = (struct bfd_link_info *) data;
4617   htab = mips_elf_hash_table (info);
4618   g = htab->got_info;
4619   if (h->global_got_area != GGA_NONE)
4620     {
4621       /* Make a final decision about whether the symbol belongs in the
4622 	 local or global GOT.  */
4623       if (mips_use_local_got_p (info, h))
4624 	/* The symbol belongs in the local GOT.  We no longer need this
4625 	   entry if it was only used for relocations; those relocations
4626 	   will be against the null or section symbol instead of H.  */
4627 	h->global_got_area = GGA_NONE;
4628       else if (htab->root.target_os == is_vxworks
4629 	       && h->got_only_for_calls
4630 	       && h->root.plt.plist->mips_offset != MINUS_ONE)
4631 	/* On VxWorks, calls can refer directly to the .got.plt entry;
4632 	   they don't need entries in the regular GOT.  .got.plt entries
4633 	   will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
4634 	h->global_got_area = GGA_NONE;
4635       else if (h->global_got_area == GGA_RELOC_ONLY)
4636 	{
4637 	  g->reloc_only_gotno++;
4638 	  g->global_gotno++;
4639 	}
4640     }
4641   return 1;
4642 }
4643 
4644 /* A htab_traverse callback for GOT entries.  Add each one to the GOT
4645    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4646 
4647 static int
4648 mips_elf_add_got_entry (void **entryp, void *data)
4649 {
4650   struct mips_got_entry *entry;
4651   struct mips_elf_traverse_got_arg *arg;
4652   void **slot;
4653 
4654   entry = (struct mips_got_entry *) *entryp;
4655   arg = (struct mips_elf_traverse_got_arg *) data;
4656   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4657   if (!slot)
4658     {
4659       arg->g = NULL;
4660       return 0;
4661     }
4662   if (!*slot)
4663     {
4664       *slot = entry;
4665       mips_elf_count_got_entry (arg->info, arg->g, entry);
4666     }
4667   return 1;
4668 }
4669 
4670 /* A htab_traverse callback for GOT page entries.  Add each one to the GOT
4671    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4672 
4673 static int
4674 mips_elf_add_got_page_entry (void **entryp, void *data)
4675 {
4676   struct mips_got_page_entry *entry;
4677   struct mips_elf_traverse_got_arg *arg;
4678   void **slot;
4679 
4680   entry = (struct mips_got_page_entry *) *entryp;
4681   arg = (struct mips_elf_traverse_got_arg *) data;
4682   slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4683   if (!slot)
4684     {
4685       arg->g = NULL;
4686       return 0;
4687     }
4688   if (!*slot)
4689     {
4690       *slot = entry;
4691       arg->g->page_gotno += entry->num_pages;
4692     }
4693   return 1;
4694 }
4695 
4696 /* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
4697    this would lead to overflow, 1 if they were merged successfully,
4698    and 0 if a merge failed due to lack of memory.  (These values are chosen
4699    so that nonnegative return values can be returned by a htab_traverse
4700    callback.)  */
4701 
4702 static int
4703 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4704 			 struct mips_got_info *to,
4705 			 struct mips_elf_got_per_bfd_arg *arg)
4706 {
4707   struct mips_elf_traverse_got_arg tga;
4708   unsigned int estimate;
4709 
4710   /* Work out how many page entries we would need for the combined GOT.  */
4711   estimate = arg->max_pages;
4712   if (estimate >= from->page_gotno + to->page_gotno)
4713     estimate = from->page_gotno + to->page_gotno;
4714 
4715   /* And conservatively estimate how many local and TLS entries
4716      would be needed.  */
4717   estimate += from->local_gotno + to->local_gotno;
4718   estimate += from->tls_gotno + to->tls_gotno;
4719 
4720   /* If we're merging with the primary got, any TLS relocations will
4721      come after the full set of global entries.  Otherwise estimate those
4722      conservatively as well.  */
4723   if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4724     estimate += arg->global_count;
4725   else
4726     estimate += from->global_gotno + to->global_gotno;
4727 
4728   /* Bail out if the combined GOT might be too big.  */
4729   if (estimate > arg->max_count)
4730     return -1;
4731 
4732   /* Transfer the bfd's got information from FROM to TO.  */
4733   tga.info = arg->info;
4734   tga.g = to;
4735   htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4736   if (!tga.g)
4737     return 0;
4738 
4739   htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4740   if (!tga.g)
4741     return 0;
4742 
4743   mips_elf_replace_bfd_got (abfd, to);
4744   return 1;
4745 }
4746 
4747 /* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
4748    as possible of the primary got, since it doesn't require explicit
4749    dynamic relocations, but don't use bfds that would reference global
4750    symbols out of the addressable range.  Failing the primary got,
4751    attempt to merge with the current got, or finish the current got
4752    and then make make the new got current.  */
4753 
4754 static bool
4755 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4756 		    struct mips_elf_got_per_bfd_arg *arg)
4757 {
4758   unsigned int estimate;
4759   int result;
4760 
4761   if (!mips_elf_resolve_final_got_entries (arg->info, g))
4762     return false;
4763 
4764   /* Work out the number of page, local and TLS entries.  */
4765   estimate = arg->max_pages;
4766   if (estimate > g->page_gotno)
4767     estimate = g->page_gotno;
4768   estimate += g->local_gotno + g->tls_gotno;
4769 
4770   /* We place TLS GOT entries after both locals and globals.  The globals
4771      for the primary GOT may overflow the normal GOT size limit, so be
4772      sure not to merge a GOT which requires TLS with the primary GOT in that
4773      case.  This doesn't affect non-primary GOTs.  */
4774   estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4775 
4776   if (estimate <= arg->max_count)
4777     {
4778       /* If we don't have a primary GOT, use it as
4779 	 a starting point for the primary GOT.  */
4780       if (!arg->primary)
4781 	{
4782 	  arg->primary = g;
4783 	  return true;
4784 	}
4785 
4786       /* Try merging with the primary GOT.  */
4787       result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4788       if (result >= 0)
4789 	return result;
4790     }
4791 
4792   /* If we can merge with the last-created got, do it.  */
4793   if (arg->current)
4794     {
4795       result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4796       if (result >= 0)
4797 	return result;
4798     }
4799 
4800   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4801      fits; if it turns out that it doesn't, we'll get relocation
4802      overflows anyway.  */
4803   g->next = arg->current;
4804   arg->current = g;
4805 
4806   return true;
4807 }
4808 
4809 /* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
4810    to GOTIDX, duplicating the entry if it has already been assigned
4811    an index in a different GOT.  */
4812 
4813 static bool
4814 mips_elf_set_gotidx (void **entryp, long gotidx)
4815 {
4816   struct mips_got_entry *entry;
4817 
4818   entry = (struct mips_got_entry *) *entryp;
4819   if (entry->gotidx > 0)
4820     {
4821       struct mips_got_entry *new_entry;
4822 
4823       new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4824       if (!new_entry)
4825 	return false;
4826 
4827       *new_entry = *entry;
4828       *entryp = new_entry;
4829       entry = new_entry;
4830     }
4831   entry->gotidx = gotidx;
4832   return true;
4833 }
4834 
4835 /* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
4836    mips_elf_traverse_got_arg in which DATA->value is the size of one
4837    GOT entry.  Set DATA->g to null on failure.  */
4838 
4839 static int
4840 mips_elf_initialize_tls_index (void **entryp, void *data)
4841 {
4842   struct mips_got_entry *entry;
4843   struct mips_elf_traverse_got_arg *arg;
4844 
4845   /* We're only interested in TLS symbols.  */
4846   entry = (struct mips_got_entry *) *entryp;
4847   if (entry->tls_type == GOT_TLS_NONE)
4848     return 1;
4849 
4850   arg = (struct mips_elf_traverse_got_arg *) data;
4851   if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4852     {
4853       arg->g = NULL;
4854       return 0;
4855     }
4856 
4857   /* Account for the entries we've just allocated.  */
4858   arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4859   return 1;
4860 }
4861 
4862 /* A htab_traverse callback for GOT entries, where DATA points to a
4863    mips_elf_traverse_got_arg.  Set the global_got_area of each global
4864    symbol to DATA->value.  */
4865 
4866 static int
4867 mips_elf_set_global_got_area (void **entryp, void *data)
4868 {
4869   struct mips_got_entry *entry;
4870   struct mips_elf_traverse_got_arg *arg;
4871 
4872   entry = (struct mips_got_entry *) *entryp;
4873   arg = (struct mips_elf_traverse_got_arg *) data;
4874   if (entry->abfd != NULL
4875       && entry->symndx == -1
4876       && entry->d.h->global_got_area != GGA_NONE)
4877     entry->d.h->global_got_area = arg->value;
4878   return 1;
4879 }
4880 
4881 /* A htab_traverse callback for secondary GOT entries, where DATA points
4882    to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
4883    and record the number of relocations they require.  DATA->value is
4884    the size of one GOT entry.  Set DATA->g to null on failure.  */
4885 
4886 static int
4887 mips_elf_set_global_gotidx (void **entryp, void *data)
4888 {
4889   struct mips_got_entry *entry;
4890   struct mips_elf_traverse_got_arg *arg;
4891 
4892   entry = (struct mips_got_entry *) *entryp;
4893   arg = (struct mips_elf_traverse_got_arg *) data;
4894   if (entry->abfd != NULL
4895       && entry->symndx == -1
4896       && entry->d.h->global_got_area != GGA_NONE)
4897     {
4898       if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4899 	{
4900 	  arg->g = NULL;
4901 	  return 0;
4902 	}
4903       arg->g->assigned_low_gotno += 1;
4904 
4905       if (bfd_link_pic (arg->info)
4906 	  || (elf_hash_table (arg->info)->dynamic_sections_created
4907 	      && entry->d.h->root.def_dynamic
4908 	      && !entry->d.h->root.def_regular))
4909 	arg->g->relocs += 1;
4910     }
4911 
4912   return 1;
4913 }
4914 
4915 /* A htab_traverse callback for GOT entries for which DATA is the
4916    bfd_link_info.  Forbid any global symbols from having traditional
4917    lazy-binding stubs.  */
4918 
4919 static int
4920 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4921 {
4922   struct bfd_link_info *info;
4923   struct mips_elf_link_hash_table *htab;
4924   struct mips_got_entry *entry;
4925 
4926   entry = (struct mips_got_entry *) *entryp;
4927   info = (struct bfd_link_info *) data;
4928   htab = mips_elf_hash_table (info);
4929   BFD_ASSERT (htab != NULL);
4930 
4931   if (entry->abfd != NULL
4932       && entry->symndx == -1
4933       && entry->d.h->needs_lazy_stub)
4934     {
4935       entry->d.h->needs_lazy_stub = false;
4936       htab->lazy_stub_count--;
4937     }
4938 
4939   return 1;
4940 }
4941 
4942 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4943    the primary GOT.  */
4944 static bfd_vma
4945 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4946 {
4947   if (!g->next)
4948     return 0;
4949 
4950   g = mips_elf_bfd_got (ibfd, false);
4951   if (! g)
4952     return 0;
4953 
4954   BFD_ASSERT (g->next);
4955 
4956   g = g->next;
4957 
4958   return (g->local_gotno + g->global_gotno + g->tls_gotno)
4959     * MIPS_ELF_GOT_SIZE (abfd);
4960 }
4961 
4962 /* Turn a single GOT that is too big for 16-bit addressing into
4963    a sequence of GOTs, each one 16-bit addressable.  */
4964 
4965 static bool
4966 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4967 		    asection *got, bfd_size_type pages)
4968 {
4969   struct mips_elf_link_hash_table *htab;
4970   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4971   struct mips_elf_traverse_got_arg tga;
4972   struct mips_got_info *g, *gg;
4973   unsigned int assign, needed_relocs;
4974   bfd *dynobj, *ibfd;
4975 
4976   dynobj = elf_hash_table (info)->dynobj;
4977   htab = mips_elf_hash_table (info);
4978   BFD_ASSERT (htab != NULL);
4979 
4980   g = htab->got_info;
4981 
4982   got_per_bfd_arg.obfd = abfd;
4983   got_per_bfd_arg.info = info;
4984   got_per_bfd_arg.current = NULL;
4985   got_per_bfd_arg.primary = NULL;
4986   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4987 				/ MIPS_ELF_GOT_SIZE (abfd))
4988 			       - htab->reserved_gotno);
4989   got_per_bfd_arg.max_pages = pages;
4990   /* The number of globals that will be included in the primary GOT.
4991      See the calls to mips_elf_set_global_got_area below for more
4992      information.  */
4993   got_per_bfd_arg.global_count = g->global_gotno;
4994 
4995   /* Try to merge the GOTs of input bfds together, as long as they
4996      don't seem to exceed the maximum GOT size, choosing one of them
4997      to be the primary GOT.  */
4998   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4999     {
5000       gg = mips_elf_bfd_got (ibfd, false);
5001       if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5002 	return false;
5003     }
5004 
5005   /* If we do not find any suitable primary GOT, create an empty one.  */
5006   if (got_per_bfd_arg.primary == NULL)
5007     g->next = mips_elf_create_got_info (abfd);
5008   else
5009     g->next = got_per_bfd_arg.primary;
5010   g->next->next = got_per_bfd_arg.current;
5011 
5012   /* GG is now the master GOT, and G is the primary GOT.  */
5013   gg = g;
5014   g = g->next;
5015 
5016   /* Map the output bfd to the primary got.  That's what we're going
5017      to use for bfds that use GOT16 or GOT_PAGE relocations that we
5018      didn't mark in check_relocs, and we want a quick way to find it.
5019      We can't just use gg->next because we're going to reverse the
5020      list.  */
5021   mips_elf_replace_bfd_got (abfd, g);
5022 
5023   /* Every symbol that is referenced in a dynamic relocation must be
5024      present in the primary GOT, so arrange for them to appear after
5025      those that are actually referenced.  */
5026   gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5027   g->global_gotno = gg->global_gotno;
5028 
5029   tga.info = info;
5030   tga.value = GGA_RELOC_ONLY;
5031   htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5032   tga.value = GGA_NORMAL;
5033   htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5034 
5035   /* Now go through the GOTs assigning them offset ranges.
5036      [assigned_low_gotno, local_gotno[ will be set to the range of local
5037      entries in each GOT.  We can then compute the end of a GOT by
5038      adding local_gotno to global_gotno.  We reverse the list and make
5039      it circular since then we'll be able to quickly compute the
5040      beginning of a GOT, by computing the end of its predecessor.  To
5041      avoid special cases for the primary GOT, while still preserving
5042      assertions that are valid for both single- and multi-got links,
5043      we arrange for the main got struct to have the right number of
5044      global entries, but set its local_gotno such that the initial
5045      offset of the primary GOT is zero.  Remember that the primary GOT
5046      will become the last item in the circular linked list, so it
5047      points back to the master GOT.  */
5048   gg->local_gotno = -g->global_gotno;
5049   gg->global_gotno = g->global_gotno;
5050   gg->tls_gotno = 0;
5051   assign = 0;
5052   gg->next = gg;
5053 
5054   do
5055     {
5056       struct mips_got_info *gn;
5057 
5058       assign += htab->reserved_gotno;
5059       g->assigned_low_gotno = assign;
5060       g->local_gotno += assign;
5061       g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5062       g->assigned_high_gotno = g->local_gotno - 1;
5063       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5064 
5065       /* Take g out of the direct list, and push it onto the reversed
5066 	 list that gg points to.  g->next is guaranteed to be nonnull after
5067 	 this operation, as required by mips_elf_initialize_tls_index. */
5068       gn = g->next;
5069       g->next = gg->next;
5070       gg->next = g;
5071 
5072       /* Set up any TLS entries.  We always place the TLS entries after
5073 	 all non-TLS entries.  */
5074       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5075       tga.g = g;
5076       tga.value = MIPS_ELF_GOT_SIZE (abfd);
5077       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5078       if (!tga.g)
5079 	return false;
5080       BFD_ASSERT (g->tls_assigned_gotno == assign);
5081 
5082       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
5083       g = gn;
5084 
5085       /* Forbid global symbols in every non-primary GOT from having
5086 	 lazy-binding stubs.  */
5087       if (g)
5088 	htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5089     }
5090   while (g);
5091 
5092   got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5093 
5094   needed_relocs = 0;
5095   for (g = gg->next; g && g->next != gg; g = g->next)
5096     {
5097       unsigned int save_assign;
5098 
5099       /* Assign offsets to global GOT entries and count how many
5100 	 relocations they need.  */
5101       save_assign = g->assigned_low_gotno;
5102       g->assigned_low_gotno = g->local_gotno;
5103       tga.info = info;
5104       tga.value = MIPS_ELF_GOT_SIZE (abfd);
5105       tga.g = g;
5106       htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5107       if (!tga.g)
5108 	return false;
5109       BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5110       g->assigned_low_gotno = save_assign;
5111 
5112       if (bfd_link_pic (info))
5113 	{
5114 	  g->relocs += g->local_gotno - g->assigned_low_gotno;
5115 	  BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5116 		      + g->next->global_gotno
5117 		      + g->next->tls_gotno
5118 		      + htab->reserved_gotno);
5119 	}
5120       needed_relocs += g->relocs;
5121     }
5122   needed_relocs += g->relocs;
5123 
5124   if (needed_relocs)
5125     mips_elf_allocate_dynamic_relocations (dynobj, info,
5126 					   needed_relocs);
5127 
5128   return true;
5129 }
5130 
5131 
5132 /* Returns the first relocation of type r_type found, beginning with
5133    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
5134 
5135 static const Elf_Internal_Rela *
5136 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5137 			  const Elf_Internal_Rela *relocation,
5138 			  const Elf_Internal_Rela *relend)
5139 {
5140   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5141 
5142   while (relocation < relend)
5143     {
5144       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5145 	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5146 	return relocation;
5147 
5148       ++relocation;
5149     }
5150 
5151   /* We didn't find it.  */
5152   return NULL;
5153 }
5154 
5155 /* Return whether an input relocation is against a local symbol.  */
5156 
5157 static bool
5158 mips_elf_local_relocation_p (bfd *input_bfd,
5159 			     const Elf_Internal_Rela *relocation,
5160 			     asection **local_sections)
5161 {
5162   unsigned long r_symndx;
5163   Elf_Internal_Shdr *symtab_hdr;
5164   size_t extsymoff;
5165 
5166   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5167   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5168   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5169 
5170   if (r_symndx < extsymoff)
5171     return true;
5172   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5173     return true;
5174 
5175   return false;
5176 }
5177 
5178 /* Sign-extend VALUE, which has the indicated number of BITS.  */
5179 
5180 bfd_vma
5181 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5182 {
5183   if (value & ((bfd_vma) 1 << (bits - 1)))
5184     /* VALUE is negative.  */
5185     value |= ((bfd_vma) - 1) << bits;
5186 
5187   return value;
5188 }
5189 
5190 /* Return non-zero if the indicated VALUE has overflowed the maximum
5191    range expressible by a signed number with the indicated number of
5192    BITS.  */
5193 
5194 static bool
5195 mips_elf_overflow_p (bfd_vma value, int bits)
5196 {
5197   bfd_signed_vma svalue = (bfd_signed_vma) value;
5198 
5199   if (svalue > (1 << (bits - 1)) - 1)
5200     /* The value is too big.  */
5201     return true;
5202   else if (svalue < -(1 << (bits - 1)))
5203     /* The value is too small.  */
5204     return true;
5205 
5206   /* All is well.  */
5207   return false;
5208 }
5209 
5210 /* Calculate the %high function.  */
5211 
5212 static bfd_vma
5213 mips_elf_high (bfd_vma value)
5214 {
5215   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5216 }
5217 
5218 /* Calculate the %higher function.  */
5219 
5220 static bfd_vma
5221 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5222 {
5223 #ifdef BFD64
5224   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5225 #else
5226   abort ();
5227   return MINUS_ONE;
5228 #endif
5229 }
5230 
5231 /* Calculate the %highest function.  */
5232 
5233 static bfd_vma
5234 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5235 {
5236 #ifdef BFD64
5237   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5238 #else
5239   abort ();
5240   return MINUS_ONE;
5241 #endif
5242 }
5243 
5244 /* Create the .compact_rel section.  */
5245 
5246 static bool
5247 mips_elf_create_compact_rel_section
5248   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5249 {
5250   flagword flags;
5251   register asection *s;
5252 
5253   if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5254     {
5255       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5256 	       | SEC_READONLY);
5257 
5258       s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5259       if (s == NULL
5260 	  || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5261 	return false;
5262 
5263       s->size = sizeof (Elf32_External_compact_rel);
5264     }
5265 
5266   return true;
5267 }
5268 
5269 /* Create the .got section to hold the global offset table.  */
5270 
5271 static bool
5272 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5273 {
5274   flagword flags;
5275   register asection *s;
5276   struct elf_link_hash_entry *h;
5277   struct bfd_link_hash_entry *bh;
5278   struct mips_elf_link_hash_table *htab;
5279 
5280   htab = mips_elf_hash_table (info);
5281   BFD_ASSERT (htab != NULL);
5282 
5283   /* This function may be called more than once.  */
5284   if (htab->root.sgot)
5285     return true;
5286 
5287   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5288 	   | SEC_LINKER_CREATED);
5289 
5290   /* We have to use an alignment of 2**4 here because this is hardcoded
5291      in the function stub generation and in the linker script.  */
5292   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5293   if (s == NULL
5294       || !bfd_set_section_alignment (s, 4))
5295     return false;
5296   htab->root.sgot = s;
5297 
5298   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5299      linker script because we don't want to define the symbol if we
5300      are not creating a global offset table.  */
5301   bh = NULL;
5302   if (! (_bfd_generic_link_add_one_symbol
5303 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5304 	  0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5305     return false;
5306 
5307   h = (struct elf_link_hash_entry *) bh;
5308   h->non_elf = 0;
5309   h->def_regular = 1;
5310   h->type = STT_OBJECT;
5311   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5312   elf_hash_table (info)->hgot = h;
5313 
5314   if (bfd_link_pic (info)
5315       && ! bfd_elf_link_record_dynamic_symbol (info, h))
5316     return false;
5317 
5318   htab->got_info = mips_elf_create_got_info (abfd);
5319   mips_elf_section_data (s)->elf.this_hdr.sh_flags
5320     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5321 
5322   /* We also need a .got.plt section when generating PLTs.  */
5323   s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5324 					  SEC_ALLOC | SEC_LOAD
5325 					  | SEC_HAS_CONTENTS
5326 					  | SEC_IN_MEMORY
5327 					  | SEC_LINKER_CREATED);
5328   if (s == NULL)
5329     return false;
5330   htab->root.sgotplt = s;
5331 
5332   return true;
5333 }
5334 
5335 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5336    __GOTT_INDEX__ symbols.  These symbols are only special for
5337    shared objects; they are not used in executables.  */
5338 
5339 static bool
5340 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5341 {
5342   return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5343 	  && bfd_link_pic (info)
5344 	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5345 	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5346 }
5347 
5348 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5349    require an la25 stub.  See also mips_elf_local_pic_function_p,
5350    which determines whether the destination function ever requires a
5351    stub.  */
5352 
5353 static bool
5354 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5355 				     bool target_is_16_bit_code_p)
5356 {
5357   /* We specifically ignore branches and jumps from EF_PIC objects,
5358      where the onus is on the compiler or programmer to perform any
5359      necessary initialization of $25.  Sometimes such initialization
5360      is unnecessary; for example, -mno-shared functions do not use
5361      the incoming value of $25, and may therefore be called directly.  */
5362   if (PIC_OBJECT_P (input_bfd))
5363     return false;
5364 
5365   switch (r_type)
5366     {
5367     case R_MIPS_26:
5368     case R_MIPS_PC16:
5369     case R_MIPS_PC21_S2:
5370     case R_MIPS_PC26_S2:
5371     case R_MICROMIPS_26_S1:
5372     case R_MICROMIPS_PC7_S1:
5373     case R_MICROMIPS_PC10_S1:
5374     case R_MICROMIPS_PC16_S1:
5375     case R_MICROMIPS_PC23_S2:
5376       return true;
5377 
5378     case R_MIPS16_26:
5379       return !target_is_16_bit_code_p;
5380 
5381     default:
5382       return false;
5383     }
5384 }
5385 
5386 /* Obtain the field relocated by RELOCATION.  */
5387 
5388 static bfd_vma
5389 mips_elf_obtain_contents (reloc_howto_type *howto,
5390 			  const Elf_Internal_Rela *relocation,
5391 			  bfd *input_bfd, bfd_byte *contents)
5392 {
5393   bfd_vma x = 0;
5394   bfd_byte *location = contents + relocation->r_offset;
5395   unsigned int size = bfd_get_reloc_size (howto);
5396 
5397   /* Obtain the bytes.  */
5398   if (size != 0)
5399     x = bfd_get (8 * size, input_bfd, location);
5400 
5401   return x;
5402 }
5403 
5404 /* Store the field relocated by RELOCATION.  */
5405 
5406 static void
5407 mips_elf_store_contents (reloc_howto_type *howto,
5408 			 const Elf_Internal_Rela *relocation,
5409 			 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5410 {
5411   bfd_byte *location = contents + relocation->r_offset;
5412   unsigned int size = bfd_get_reloc_size (howto);
5413 
5414   /* Put the value into the output.  */
5415   if (size != 0)
5416     bfd_put (8 * size, input_bfd, x, location);
5417 }
5418 
5419 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5420    RELOCATION described by HOWTO, with a move of 0 to the load target
5421    register, returning TRUE if that is successful and FALSE otherwise.
5422    If DOIT is FALSE, then only determine it patching is possible and
5423    return status without actually changing CONTENTS.
5424 */
5425 
5426 static bool
5427 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5428 			   const Elf_Internal_Rela *relocation,
5429 			   reloc_howto_type *howto, bool doit)
5430 {
5431   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5432   bfd_byte *location = contents + relocation->r_offset;
5433   bool nullified = true;
5434   bfd_vma x;
5435 
5436   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5437 
5438   /* Obtain the current value.  */
5439   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5440 
5441   /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5442      while RY is at bits [18:16] of the combined 32-bit instruction word.  */
5443   if (mips16_reloc_p (r_type)
5444       && (((x >> 22) & 0x3ff) == 0x3d3				/* LW */
5445 	  || ((x >> 22) & 0x3ff) == 0x3c7))			/* LD */
5446     x = (0x3cdU << 22) | (x & (7 << 16)) << 3;			/* LI */
5447   else if (micromips_reloc_p (r_type)
5448 	   && ((x >> 26) & 0x37) == 0x37)			/* LW/LD */
5449     x = (0xc << 26) | (x & (0x1f << 21));			/* ADDIU */
5450   else if (((x >> 26) & 0x3f) == 0x23				/* LW */
5451 	   || ((x >> 26) & 0x3f) == 0x37)			/* LD */
5452     x = (0x9 << 26) | (x & (0x1f << 16));			/* ADDIU */
5453   else
5454     nullified = false;
5455 
5456   /* Put the value into the output.  */
5457   if (doit && nullified)
5458     mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5459 
5460   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5461 
5462   return nullified;
5463 }
5464 
5465 /* Calculate the value produced by the RELOCATION (which comes from
5466    the INPUT_BFD).  The ADDEND is the addend to use for this
5467    RELOCATION; RELOCATION->R_ADDEND is ignored.
5468 
5469    The result of the relocation calculation is stored in VALUEP.
5470    On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5471    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5472 
5473    This function returns bfd_reloc_continue if the caller need take no
5474    further action regarding this relocation, bfd_reloc_notsupported if
5475    something goes dramatically wrong, bfd_reloc_overflow if an
5476    overflow occurs, and bfd_reloc_ok to indicate success.  */
5477 
5478 static bfd_reloc_status_type
5479 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5480 			       asection *input_section, bfd_byte *contents,
5481 			       struct bfd_link_info *info,
5482 			       const Elf_Internal_Rela *relocation,
5483 			       bfd_vma addend, reloc_howto_type *howto,
5484 			       Elf_Internal_Sym *local_syms,
5485 			       asection **local_sections, bfd_vma *valuep,
5486 			       const char **namep,
5487 			       bool *cross_mode_jump_p,
5488 			       bool save_addend)
5489 {
5490   /* The eventual value we will return.  */
5491   bfd_vma value;
5492   /* The address of the symbol against which the relocation is
5493      occurring.  */
5494   bfd_vma symbol = 0;
5495   /* The final GP value to be used for the relocatable, executable, or
5496      shared object file being produced.  */
5497   bfd_vma gp;
5498   /* The place (section offset or address) of the storage unit being
5499      relocated.  */
5500   bfd_vma p;
5501   /* The value of GP used to create the relocatable object.  */
5502   bfd_vma gp0;
5503   /* The offset into the global offset table at which the address of
5504      the relocation entry symbol, adjusted by the addend, resides
5505      during execution.  */
5506   bfd_vma g = MINUS_ONE;
5507   /* The section in which the symbol referenced by the relocation is
5508      located.  */
5509   asection *sec = NULL;
5510   struct mips_elf_link_hash_entry *h = NULL;
5511   /* TRUE if the symbol referred to by this relocation is a local
5512      symbol.  */
5513   bool local_p, was_local_p;
5514   /* TRUE if the symbol referred to by this relocation is a section
5515      symbol.  */
5516   bool section_p = false;
5517   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
5518   bool gp_disp_p = false;
5519   /* TRUE if the symbol referred to by this relocation is
5520      "__gnu_local_gp".  */
5521   bool gnu_local_gp_p = false;
5522   Elf_Internal_Shdr *symtab_hdr;
5523   size_t extsymoff;
5524   unsigned long r_symndx;
5525   int r_type;
5526   /* TRUE if overflow occurred during the calculation of the
5527      relocation value.  */
5528   bool overflowed_p;
5529   /* TRUE if this relocation refers to a MIPS16 function.  */
5530   bool target_is_16_bit_code_p = false;
5531   bool target_is_micromips_code_p = false;
5532   struct mips_elf_link_hash_table *htab;
5533   bfd *dynobj;
5534   bool resolved_to_zero;
5535 
5536   dynobj = elf_hash_table (info)->dynobj;
5537   htab = mips_elf_hash_table (info);
5538   BFD_ASSERT (htab != NULL);
5539 
5540   /* Parse the relocation.  */
5541   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5542   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5543   p = (input_section->output_section->vma
5544        + input_section->output_offset
5545        + relocation->r_offset);
5546 
5547   /* Assume that there will be no overflow.  */
5548   overflowed_p = false;
5549 
5550   /* Figure out whether or not the symbol is local, and get the offset
5551      used in the array of hash table entries.  */
5552   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5553   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5554 					 local_sections);
5555   was_local_p = local_p;
5556   if (! elf_bad_symtab (input_bfd))
5557     extsymoff = symtab_hdr->sh_info;
5558   else
5559     {
5560       /* The symbol table does not follow the rule that local symbols
5561 	 must come before globals.  */
5562       extsymoff = 0;
5563     }
5564 
5565   /* Figure out the value of the symbol.  */
5566   if (local_p)
5567     {
5568       bool micromips_p = MICROMIPS_P (abfd);
5569       Elf_Internal_Sym *sym;
5570 
5571       sym = local_syms + r_symndx;
5572       sec = local_sections[r_symndx];
5573 
5574       section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5575 
5576       symbol = sec->output_section->vma + sec->output_offset;
5577       if (!section_p || (sec->flags & SEC_MERGE))
5578 	symbol += sym->st_value;
5579       if ((sec->flags & SEC_MERGE) && section_p)
5580 	{
5581 	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5582 	  addend -= symbol;
5583 	  addend += sec->output_section->vma + sec->output_offset;
5584 	}
5585 
5586       /* MIPS16/microMIPS text labels should be treated as odd.  */
5587       if (ELF_ST_IS_COMPRESSED (sym->st_other))
5588 	++symbol;
5589 
5590       /* Record the name of this symbol, for our caller.  */
5591       *namep = bfd_elf_string_from_elf_section (input_bfd,
5592 						symtab_hdr->sh_link,
5593 						sym->st_name);
5594       if (*namep == NULL || **namep == '\0')
5595 	*namep = bfd_section_name (sec);
5596 
5597       /* For relocations against a section symbol and ones against no
5598 	 symbol (absolute relocations) infer the ISA mode from the addend.  */
5599       if (section_p || r_symndx == STN_UNDEF)
5600 	{
5601 	  target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5602 	  target_is_micromips_code_p = (addend & 1) && micromips_p;
5603 	}
5604       /* For relocations against an absolute symbol infer the ISA mode
5605 	 from the value of the symbol plus addend.  */
5606       else if (bfd_is_abs_section (sec))
5607 	{
5608 	  target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5609 	  target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5610 	}
5611       /* Otherwise just use the regular symbol annotation available.  */
5612       else
5613 	{
5614 	  target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5615 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5616 	}
5617     }
5618   else
5619     {
5620       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
5621 
5622       /* For global symbols we look up the symbol in the hash-table.  */
5623       h = ((struct mips_elf_link_hash_entry *)
5624 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5625       /* Find the real hash-table entry for this symbol.  */
5626       while (h->root.root.type == bfd_link_hash_indirect
5627 	     || h->root.root.type == bfd_link_hash_warning)
5628 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5629 
5630       /* Record the name of this symbol, for our caller.  */
5631       *namep = h->root.root.root.string;
5632 
5633       /* See if this is the special _gp_disp symbol.  Note that such a
5634 	 symbol must always be a global symbol.  */
5635       if (strcmp (*namep, "_gp_disp") == 0
5636 	  && ! NEWABI_P (input_bfd))
5637 	{
5638 	  /* Relocations against _gp_disp are permitted only with
5639 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
5640 	  if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5641 	    return bfd_reloc_notsupported;
5642 
5643 	  gp_disp_p = true;
5644 	}
5645       /* See if this is the special _gp symbol.  Note that such a
5646 	 symbol must always be a global symbol.  */
5647       else if (strcmp (*namep, "__gnu_local_gp") == 0)
5648 	gnu_local_gp_p = true;
5649 
5650 
5651       /* If this symbol is defined, calculate its address.  Note that
5652 	 _gp_disp is a magic symbol, always implicitly defined by the
5653 	 linker, so it's inappropriate to check to see whether or not
5654 	 its defined.  */
5655       else if ((h->root.root.type == bfd_link_hash_defined
5656 		|| h->root.root.type == bfd_link_hash_defweak)
5657 	       && h->root.root.u.def.section)
5658 	{
5659 	  sec = h->root.root.u.def.section;
5660 	  if (sec->output_section)
5661 	    symbol = (h->root.root.u.def.value
5662 		      + sec->output_section->vma
5663 		      + sec->output_offset);
5664 	  else
5665 	    symbol = h->root.root.u.def.value;
5666 	}
5667       else if (h->root.root.type == bfd_link_hash_undefweak)
5668 	/* We allow relocations against undefined weak symbols, giving
5669 	   it the value zero, so that you can undefined weak functions
5670 	   and check to see if they exist by looking at their
5671 	   addresses.  */
5672 	symbol = 0;
5673       else if (info->unresolved_syms_in_objects == RM_IGNORE
5674 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5675 	symbol = 0;
5676       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5677 		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5678 	{
5679 	  /* If this is a dynamic link, we should have created a
5680 	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5681 	     in _bfd_mips_elf_create_dynamic_sections.
5682 	     Otherwise, we should define the symbol with a value of 0.
5683 	     FIXME: It should probably get into the symbol table
5684 	     somehow as well.  */
5685 	  BFD_ASSERT (! bfd_link_pic (info));
5686 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5687 	  symbol = 0;
5688 	}
5689       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5690 	{
5691 	  /* This is an optional symbol - an Irix specific extension to the
5692 	     ELF spec.  Ignore it for now.
5693 	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
5694 	     than simply ignoring them, but we do not handle this for now.
5695 	     For information see the "64-bit ELF Object File Specification"
5696 	     which is available from here:
5697 	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
5698 	  symbol = 0;
5699 	}
5700       else
5701 	{
5702           bool reject_undefined
5703 	    = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5704 		&& !info->warn_unresolved_syms)
5705 	       || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5706 
5707 	  info->callbacks->undefined_symbol
5708 	    (info, h->root.root.root.string, input_bfd,
5709 	     input_section, relocation->r_offset, reject_undefined);
5710 
5711 	  if (reject_undefined)
5712 	    return bfd_reloc_undefined;
5713 
5714 	  symbol = 0;
5715 	}
5716 
5717       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5718       target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5719     }
5720 
5721   /* If this is a reference to a 16-bit function with a stub, we need
5722      to redirect the relocation to the stub unless:
5723 
5724      (a) the relocation is for a MIPS16 JAL;
5725 
5726      (b) the relocation is for a MIPS16 PIC call, and there are no
5727 	 non-MIPS16 uses of the GOT slot; or
5728 
5729      (c) the section allows direct references to MIPS16 functions.  */
5730   if (r_type != R_MIPS16_26
5731       && !bfd_link_relocatable (info)
5732       && ((h != NULL
5733 	   && h->fn_stub != NULL
5734 	   && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5735 	  || (local_p
5736 	      && mips_elf_tdata (input_bfd)->local_stubs != NULL
5737 	      && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5738       && !section_allows_mips16_refs_p (input_section))
5739     {
5740       /* This is a 32- or 64-bit call to a 16-bit function.  We should
5741 	 have already noticed that we were going to need the
5742 	 stub.  */
5743       if (local_p)
5744 	{
5745 	  sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5746 	  value = 0;
5747 	}
5748       else
5749 	{
5750 	  BFD_ASSERT (h->need_fn_stub);
5751 	  if (h->la25_stub)
5752 	    {
5753 	      /* If a LA25 header for the stub itself exists, point to the
5754 		 prepended LUI/ADDIU sequence.  */
5755 	      sec = h->la25_stub->stub_section;
5756 	      value = h->la25_stub->offset;
5757 	    }
5758 	  else
5759 	    {
5760 	      sec = h->fn_stub;
5761 	      value = 0;
5762 	    }
5763 	}
5764 
5765       symbol = sec->output_section->vma + sec->output_offset + value;
5766       /* The target is 16-bit, but the stub isn't.  */
5767       target_is_16_bit_code_p = false;
5768     }
5769   /* If this is a MIPS16 call with a stub, that is made through the PLT or
5770      to a standard MIPS function, we need to redirect the call to the stub.
5771      Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5772      indirect calls should use an indirect stub instead.  */
5773   else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5774 	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5775 	       || (local_p
5776 		   && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5777 		   && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5778 	   && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5779     {
5780       if (local_p)
5781 	sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5782       else
5783 	{
5784 	  /* If both call_stub and call_fp_stub are defined, we can figure
5785 	     out which one to use by checking which one appears in the input
5786 	     file.  */
5787 	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
5788 	    {
5789 	      asection *o;
5790 
5791 	      sec = NULL;
5792 	      for (o = input_bfd->sections; o != NULL; o = o->next)
5793 		{
5794 		  if (CALL_FP_STUB_P (bfd_section_name (o)))
5795 		    {
5796 		      sec = h->call_fp_stub;
5797 		      break;
5798 		    }
5799 		}
5800 	      if (sec == NULL)
5801 		sec = h->call_stub;
5802 	    }
5803 	  else if (h->call_stub != NULL)
5804 	    sec = h->call_stub;
5805 	  else
5806 	    sec = h->call_fp_stub;
5807 	}
5808 
5809       BFD_ASSERT (sec->size > 0);
5810       symbol = sec->output_section->vma + sec->output_offset;
5811     }
5812   /* If this is a direct call to a PIC function, redirect to the
5813      non-PIC stub.  */
5814   else if (h != NULL && h->la25_stub
5815 	   && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5816 						   target_is_16_bit_code_p))
5817     {
5818 	symbol = (h->la25_stub->stub_section->output_section->vma
5819 		  + h->la25_stub->stub_section->output_offset
5820 		  + h->la25_stub->offset);
5821 	if (ELF_ST_IS_MICROMIPS (h->root.other))
5822 	  symbol |= 1;
5823     }
5824   /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5825      entry is used if a standard PLT entry has also been made.  In this
5826      case the symbol will have been set by mips_elf_set_plt_sym_value
5827      to point to the standard PLT entry, so redirect to the compressed
5828      one.  */
5829   else if ((mips16_branch_reloc_p (r_type)
5830 	    || micromips_branch_reloc_p (r_type))
5831 	   && !bfd_link_relocatable (info)
5832 	   && h != NULL
5833 	   && h->use_plt_entry
5834 	   && h->root.plt.plist->comp_offset != MINUS_ONE
5835 	   && h->root.plt.plist->mips_offset != MINUS_ONE)
5836     {
5837       bool micromips_p = MICROMIPS_P (abfd);
5838 
5839       sec = htab->root.splt;
5840       symbol = (sec->output_section->vma
5841 		+ sec->output_offset
5842 		+ htab->plt_header_size
5843 		+ htab->plt_mips_offset
5844 		+ h->root.plt.plist->comp_offset
5845 		+ 1);
5846 
5847       target_is_16_bit_code_p = !micromips_p;
5848       target_is_micromips_code_p = micromips_p;
5849     }
5850 
5851   /* Make sure MIPS16 and microMIPS are not used together.  */
5852   if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5853       || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5854    {
5855       _bfd_error_handler
5856 	(_("MIPS16 and microMIPS functions cannot call each other"));
5857       return bfd_reloc_notsupported;
5858    }
5859 
5860   /* Calls from 16-bit code to 32-bit code and vice versa require the
5861      mode change.  However, we can ignore calls to undefined weak symbols,
5862      which should never be executed at runtime.  This exception is important
5863      because the assembly writer may have "known" that any definition of the
5864      symbol would be 16-bit code, and that direct jumps were therefore
5865      acceptable.  */
5866   *cross_mode_jump_p = (!bfd_link_relocatable (info)
5867 			&& !(h && h->root.root.type == bfd_link_hash_undefweak)
5868 			&& ((mips16_branch_reloc_p (r_type)
5869 			     && !target_is_16_bit_code_p)
5870 			    || (micromips_branch_reloc_p (r_type)
5871 				&& !target_is_micromips_code_p)
5872 			    || ((branch_reloc_p (r_type)
5873 				 || r_type == R_MIPS_JALR)
5874 				&& (target_is_16_bit_code_p
5875 				    || target_is_micromips_code_p))));
5876 
5877   resolved_to_zero = (h != NULL
5878 		      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5879 
5880   switch (r_type)
5881     {
5882     case R_MIPS16_CALL16:
5883     case R_MIPS16_GOT16:
5884     case R_MIPS_CALL16:
5885     case R_MIPS_GOT16:
5886     case R_MIPS_GOT_PAGE:
5887     case R_MIPS_GOT_DISP:
5888     case R_MIPS_GOT_LO16:
5889     case R_MIPS_CALL_LO16:
5890     case R_MICROMIPS_CALL16:
5891     case R_MICROMIPS_GOT16:
5892     case R_MICROMIPS_GOT_PAGE:
5893     case R_MICROMIPS_GOT_DISP:
5894     case R_MICROMIPS_GOT_LO16:
5895     case R_MICROMIPS_CALL_LO16:
5896       if (resolved_to_zero
5897 	  && !bfd_link_relocatable (info)
5898 	  && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5899 					relocation->r_offset)
5900 	  && mips_elf_nullify_got_load (input_bfd, contents,
5901 					relocation, howto, true))
5902 	return bfd_reloc_continue;
5903 
5904       /* Fall through.  */
5905     case R_MIPS_GOT_HI16:
5906     case R_MIPS_CALL_HI16:
5907     case R_MICROMIPS_GOT_HI16:
5908     case R_MICROMIPS_CALL_HI16:
5909       if (resolved_to_zero
5910 	  && htab->use_absolute_zero
5911 	  && bfd_link_pic (info))
5912 	{
5913 	  /* Redirect to the special `__gnu_absolute_zero' symbol.  */
5914 	  h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5915 					 false, false, false);
5916 	  BFD_ASSERT (h != NULL);
5917 	}
5918       break;
5919     }
5920 
5921   local_p = (h == NULL || mips_use_local_got_p (info, h));
5922 
5923   gp0 = _bfd_get_gp_value (input_bfd);
5924   gp = _bfd_get_gp_value (abfd);
5925   if (htab->got_info)
5926     gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5927 
5928   if (gnu_local_gp_p)
5929     symbol = gp;
5930 
5931   /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5932      to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
5933      corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
5934   if (got_page_reloc_p (r_type) && !local_p)
5935     {
5936       r_type = (micromips_reloc_p (r_type)
5937 		? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5938       addend = 0;
5939     }
5940 
5941   /* If we haven't already determined the GOT offset, and we're going
5942      to need it, get it now.  */
5943   switch (r_type)
5944     {
5945     case R_MIPS16_CALL16:
5946     case R_MIPS16_GOT16:
5947     case R_MIPS_CALL16:
5948     case R_MIPS_GOT16:
5949     case R_MIPS_GOT_DISP:
5950     case R_MIPS_GOT_HI16:
5951     case R_MIPS_CALL_HI16:
5952     case R_MIPS_GOT_LO16:
5953     case R_MIPS_CALL_LO16:
5954     case R_MICROMIPS_CALL16:
5955     case R_MICROMIPS_GOT16:
5956     case R_MICROMIPS_GOT_DISP:
5957     case R_MICROMIPS_GOT_HI16:
5958     case R_MICROMIPS_CALL_HI16:
5959     case R_MICROMIPS_GOT_LO16:
5960     case R_MICROMIPS_CALL_LO16:
5961     case R_MIPS_TLS_GD:
5962     case R_MIPS_TLS_GOTTPREL:
5963     case R_MIPS_TLS_LDM:
5964     case R_MIPS16_TLS_GD:
5965     case R_MIPS16_TLS_GOTTPREL:
5966     case R_MIPS16_TLS_LDM:
5967     case R_MICROMIPS_TLS_GD:
5968     case R_MICROMIPS_TLS_GOTTPREL:
5969     case R_MICROMIPS_TLS_LDM:
5970       /* Find the index into the GOT where this value is located.  */
5971       if (tls_ldm_reloc_p (r_type))
5972 	{
5973 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
5974 					0, 0, NULL, r_type);
5975 	  if (g == MINUS_ONE)
5976 	    return bfd_reloc_outofrange;
5977 	}
5978       else if (!local_p)
5979 	{
5980 	  /* On VxWorks, CALL relocations should refer to the .got.plt
5981 	     entry, which is initialized to point at the PLT stub.  */
5982 	  if (htab->root.target_os == is_vxworks
5983 	      && (call_hi16_reloc_p (r_type)
5984 		  || call_lo16_reloc_p (r_type)
5985 		  || call16_reloc_p (r_type)))
5986 	    {
5987 	      BFD_ASSERT (addend == 0);
5988 	      BFD_ASSERT (h->root.needs_plt);
5989 	      g = mips_elf_gotplt_index (info, &h->root);
5990 	    }
5991 	  else
5992 	    {
5993 	      BFD_ASSERT (addend == 0);
5994 	      g = mips_elf_global_got_index (abfd, info, input_bfd,
5995 					     &h->root, r_type);
5996 	      if (!TLS_RELOC_P (r_type)
5997 		  && !elf_hash_table (info)->dynamic_sections_created)
5998 		/* This is a static link.  We must initialize the GOT entry.  */
5999 		MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
6000 	    }
6001 	}
6002       else if (htab->root.target_os != is_vxworks
6003 	       && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6004 	/* The calculation below does not involve "g".  */
6005 	break;
6006       else
6007 	{
6008 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
6009 					symbol + addend, r_symndx, h, r_type);
6010 	  if (g == MINUS_ONE)
6011 	    return bfd_reloc_outofrange;
6012 	}
6013 
6014       /* Convert GOT indices to actual offsets.  */
6015       g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6016       break;
6017     }
6018 
6019   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6020      symbols are resolved by the loader.  Add them to .rela.dyn.  */
6021   if (h != NULL && is_gott_symbol (info, &h->root))
6022     {
6023       Elf_Internal_Rela outrel;
6024       bfd_byte *loc;
6025       asection *s;
6026 
6027       s = mips_elf_rel_dyn_section (info, false);
6028       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6029 
6030       outrel.r_offset = (input_section->output_section->vma
6031 			 + input_section->output_offset
6032 			 + relocation->r_offset);
6033       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6034       outrel.r_addend = addend;
6035       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6036 
6037       /* If we've written this relocation for a readonly section,
6038 	 we need to set DF_TEXTREL again, so that we do not delete the
6039 	 DT_TEXTREL tag.  */
6040       if (MIPS_ELF_READONLY_SECTION (input_section))
6041 	info->flags |= DF_TEXTREL;
6042 
6043       *valuep = 0;
6044       return bfd_reloc_ok;
6045     }
6046 
6047   /* Figure out what kind of relocation is being performed.  */
6048   switch (r_type)
6049     {
6050     case R_MIPS_NONE:
6051       return bfd_reloc_continue;
6052 
6053     case R_MIPS_16:
6054       if (howto->partial_inplace)
6055 	addend = _bfd_mips_elf_sign_extend (addend, 16);
6056       value = symbol + addend;
6057       overflowed_p = mips_elf_overflow_p (value, 16);
6058       break;
6059 
6060     case R_MIPS_32:
6061     case R_MIPS_REL32:
6062     case R_MIPS_64:
6063       if ((bfd_link_pic (info)
6064 	   || (htab->root.dynamic_sections_created
6065 	       && h != NULL
6066 	       && h->root.def_dynamic
6067 	       && !h->root.def_regular
6068 	       && !h->has_static_relocs))
6069 	  && r_symndx != STN_UNDEF
6070 	  && (h == NULL
6071 	      || h->root.root.type != bfd_link_hash_undefweak
6072 	      || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6073 		  && !resolved_to_zero))
6074 	  && (input_section->flags & SEC_ALLOC) != 0)
6075 	{
6076 	  /* If we're creating a shared library, then we can't know
6077 	     where the symbol will end up.  So, we create a relocation
6078 	     record in the output, and leave the job up to the dynamic
6079 	     linker.  We must do the same for executable references to
6080 	     shared library symbols, unless we've decided to use copy
6081 	     relocs or PLTs instead.  */
6082 	  value = addend;
6083 	  if (!mips_elf_create_dynamic_relocation (abfd,
6084 						   info,
6085 						   relocation,
6086 						   h,
6087 						   sec,
6088 						   symbol,
6089 						   &value,
6090 						   input_section))
6091 	    return bfd_reloc_undefined;
6092 	}
6093       else
6094 	{
6095 	  if (r_type != R_MIPS_REL32)
6096 	    value = symbol + addend;
6097 	  else
6098 	    value = addend;
6099 	}
6100       value &= howto->dst_mask;
6101       break;
6102 
6103     case R_MIPS_PC32:
6104       value = symbol + addend - p;
6105       value &= howto->dst_mask;
6106       break;
6107 
6108     case R_MIPS16_26:
6109       /* The calculation for R_MIPS16_26 is just the same as for an
6110 	 R_MIPS_26.  It's only the storage of the relocated field into
6111 	 the output file that's different.  That's handled in
6112 	 mips_elf_perform_relocation.  So, we just fall through to the
6113 	 R_MIPS_26 case here.  */
6114     case R_MIPS_26:
6115     case R_MICROMIPS_26_S1:
6116       {
6117 	unsigned int shift;
6118 
6119 	/* Shift is 2, unusually, for microMIPS JALX.  */
6120 	shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6121 
6122 	if (howto->partial_inplace && !section_p)
6123 	  value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6124 	else
6125 	  value = addend;
6126 	value += symbol;
6127 
6128 	/* Make sure the target of a jump is suitably aligned.  Bit 0 must
6129 	   be the correct ISA mode selector except for weak undefined
6130 	   symbols.  */
6131 	if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6132 	    && (*cross_mode_jump_p
6133 		? (value & 3) != (r_type == R_MIPS_26)
6134 		: (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6135 	  return bfd_reloc_outofrange;
6136 
6137 	value >>= shift;
6138 	if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6139 	  overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6140 	value &= howto->dst_mask;
6141       }
6142       break;
6143 
6144     case R_MIPS_TLS_DTPREL_HI16:
6145     case R_MIPS16_TLS_DTPREL_HI16:
6146     case R_MICROMIPS_TLS_DTPREL_HI16:
6147       value = (mips_elf_high (addend + symbol - dtprel_base (info))
6148 	       & howto->dst_mask);
6149       break;
6150 
6151     case R_MIPS_TLS_DTPREL_LO16:
6152     case R_MIPS_TLS_DTPREL32:
6153     case R_MIPS_TLS_DTPREL64:
6154     case R_MIPS16_TLS_DTPREL_LO16:
6155     case R_MICROMIPS_TLS_DTPREL_LO16:
6156       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6157       break;
6158 
6159     case R_MIPS_TLS_TPREL_HI16:
6160     case R_MIPS16_TLS_TPREL_HI16:
6161     case R_MICROMIPS_TLS_TPREL_HI16:
6162       value = (mips_elf_high (addend + symbol - tprel_base (info))
6163 	       & howto->dst_mask);
6164       break;
6165 
6166     case R_MIPS_TLS_TPREL_LO16:
6167     case R_MIPS_TLS_TPREL32:
6168     case R_MIPS_TLS_TPREL64:
6169     case R_MIPS16_TLS_TPREL_LO16:
6170     case R_MICROMIPS_TLS_TPREL_LO16:
6171       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6172       break;
6173 
6174     case R_MIPS_HI16:
6175     case R_MIPS16_HI16:
6176     case R_MICROMIPS_HI16:
6177       if (!gp_disp_p)
6178 	{
6179 	  value = mips_elf_high (addend + symbol);
6180 	  value &= howto->dst_mask;
6181 	}
6182       else
6183 	{
6184 	  /* For MIPS16 ABI code we generate this sequence
6185 		0: li      $v0,%hi(_gp_disp)
6186 		4: addiupc $v1,%lo(_gp_disp)
6187 		8: sll     $v0,16
6188 	       12: addu    $v0,$v1
6189 	       14: move    $gp,$v0
6190 	     So the offsets of hi and lo relocs are the same, but the
6191 	     base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6192 	     ADDIUPC clears the low two bits of the instruction address,
6193 	     so the base is ($t9 + 4) & ~3.  */
6194 	  if (r_type == R_MIPS16_HI16)
6195 	    value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6196 	  /* The microMIPS .cpload sequence uses the same assembly
6197 	     instructions as the traditional psABI version, but the
6198 	     incoming $t9 has the low bit set.  */
6199 	  else if (r_type == R_MICROMIPS_HI16)
6200 	    value = mips_elf_high (addend + gp - p - 1);
6201 	  else
6202 	    value = mips_elf_high (addend + gp - p);
6203 	}
6204       break;
6205 
6206     case R_MIPS_LO16:
6207     case R_MIPS16_LO16:
6208     case R_MICROMIPS_LO16:
6209     case R_MICROMIPS_HI0_LO16:
6210       if (!gp_disp_p)
6211 	value = (symbol + addend) & howto->dst_mask;
6212       else
6213 	{
6214 	  /* See the comment for R_MIPS16_HI16 above for the reason
6215 	     for this conditional.  */
6216 	  if (r_type == R_MIPS16_LO16)
6217 	    value = addend + gp - (p & ~(bfd_vma) 0x3);
6218 	  else if (r_type == R_MICROMIPS_LO16
6219 		   || r_type == R_MICROMIPS_HI0_LO16)
6220 	    value = addend + gp - p + 3;
6221 	  else
6222 	    value = addend + gp - p + 4;
6223 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6224 	     for overflow.  But, on, say, IRIX5, relocations against
6225 	     _gp_disp are normally generated from the .cpload
6226 	     pseudo-op.  It generates code that normally looks like
6227 	     this:
6228 
6229 	       lui    $gp,%hi(_gp_disp)
6230 	       addiu  $gp,$gp,%lo(_gp_disp)
6231 	       addu   $gp,$gp,$t9
6232 
6233 	     Here $t9 holds the address of the function being called,
6234 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
6235 	     relocation can easily overflow in this situation, but the
6236 	     R_MIPS_HI16 relocation will handle the overflow.
6237 	     Therefore, we consider this a bug in the MIPS ABI, and do
6238 	     not check for overflow here.  */
6239 	}
6240       break;
6241 
6242     case R_MIPS_LITERAL:
6243     case R_MICROMIPS_LITERAL:
6244       /* Because we don't merge literal sections, we can handle this
6245 	 just like R_MIPS_GPREL16.  In the long run, we should merge
6246 	 shared literals, and then we will need to additional work
6247 	 here.  */
6248 
6249       /* Fall through.  */
6250 
6251     case R_MIPS16_GPREL:
6252       /* The R_MIPS16_GPREL performs the same calculation as
6253 	 R_MIPS_GPREL16, but stores the relocated bits in a different
6254 	 order.  We don't need to do anything special here; the
6255 	 differences are handled in mips_elf_perform_relocation.  */
6256     case R_MIPS_GPREL16:
6257     case R_MICROMIPS_GPREL7_S2:
6258     case R_MICROMIPS_GPREL16:
6259       {
6260 	int bits = howto->bitsize + howto->rightshift;
6261 	/* Only sign-extend the addend if it was extracted from the
6262 	   instruction.  If the addend was separate, leave it alone,
6263 	   otherwise we may lose significant bits.  */
6264 	if (howto->partial_inplace)
6265 	  addend = _bfd_mips_elf_sign_extend (addend, bits);
6266 	value = symbol + addend - gp;
6267 	/* If the symbol was local, any earlier relocatable links will
6268 	   have adjusted its addend with the gp offset, so compensate
6269 	   for that now.  Don't do it for symbols forced local in this
6270 	   link, though, since they won't have had the gp offset applied
6271 	   to them before.  */
6272 	if (was_local_p)
6273 	  value += gp0;
6274 	if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6275 	  overflowed_p = mips_elf_overflow_p (value, bits);
6276       }
6277       break;
6278 
6279     case R_MIPS16_GOT16:
6280     case R_MIPS16_CALL16:
6281     case R_MIPS_GOT16:
6282     case R_MIPS_CALL16:
6283     case R_MICROMIPS_GOT16:
6284     case R_MICROMIPS_CALL16:
6285       /* VxWorks does not have separate local and global semantics for
6286 	 R_MIPS*_GOT16; every relocation evaluates to "G".  */
6287       if (htab->root.target_os != is_vxworks && local_p)
6288 	{
6289 	  value = mips_elf_got16_entry (abfd, input_bfd, info,
6290 					symbol + addend, !was_local_p);
6291 	  if (value == MINUS_ONE)
6292 	    return bfd_reloc_outofrange;
6293 	  value
6294 	    = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6295 	  overflowed_p = mips_elf_overflow_p (value, 16);
6296 	  break;
6297 	}
6298 
6299       /* Fall through.  */
6300 
6301     case R_MIPS_TLS_GD:
6302     case R_MIPS_TLS_GOTTPREL:
6303     case R_MIPS_TLS_LDM:
6304     case R_MIPS_GOT_DISP:
6305     case R_MIPS16_TLS_GD:
6306     case R_MIPS16_TLS_GOTTPREL:
6307     case R_MIPS16_TLS_LDM:
6308     case R_MICROMIPS_TLS_GD:
6309     case R_MICROMIPS_TLS_GOTTPREL:
6310     case R_MICROMIPS_TLS_LDM:
6311     case R_MICROMIPS_GOT_DISP:
6312       value = g;
6313       overflowed_p = mips_elf_overflow_p (value, 16);
6314       break;
6315 
6316     case R_MIPS_GPREL32:
6317       value = (addend + symbol + gp0 - gp);
6318       if (!save_addend)
6319 	value &= howto->dst_mask;
6320       break;
6321 
6322     case R_MIPS_PC16:
6323     case R_MIPS_GNU_REL16_S2:
6324       if (howto->partial_inplace)
6325 	addend = _bfd_mips_elf_sign_extend (addend, 18);
6326 
6327       /* No need to exclude weak undefined symbols here as they resolve
6328 	 to 0 and never set `*cross_mode_jump_p', so this alignment check
6329 	 will never trigger for them.  */
6330       if (*cross_mode_jump_p
6331 	  ? ((symbol + addend) & 3) != 1
6332 	  : ((symbol + addend) & 3) != 0)
6333 	return bfd_reloc_outofrange;
6334 
6335       value = symbol + addend - p;
6336       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6337 	overflowed_p = mips_elf_overflow_p (value, 18);
6338       value >>= howto->rightshift;
6339       value &= howto->dst_mask;
6340       break;
6341 
6342     case R_MIPS16_PC16_S1:
6343       if (howto->partial_inplace)
6344 	addend = _bfd_mips_elf_sign_extend (addend, 17);
6345 
6346       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6347 	  && (*cross_mode_jump_p
6348 	      ? ((symbol + addend) & 3) != 0
6349 	      : ((symbol + addend) & 1) == 0))
6350 	return bfd_reloc_outofrange;
6351 
6352       value = symbol + addend - p;
6353       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6354 	overflowed_p = mips_elf_overflow_p (value, 17);
6355       value >>= howto->rightshift;
6356       value &= howto->dst_mask;
6357       break;
6358 
6359     case R_MIPS_PC21_S2:
6360       if (howto->partial_inplace)
6361 	addend = _bfd_mips_elf_sign_extend (addend, 23);
6362 
6363       if ((symbol + addend) & 3)
6364 	return bfd_reloc_outofrange;
6365 
6366       value = symbol + addend - p;
6367       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6368 	overflowed_p = mips_elf_overflow_p (value, 23);
6369       value >>= howto->rightshift;
6370       value &= howto->dst_mask;
6371       break;
6372 
6373     case R_MIPS_PC26_S2:
6374       if (howto->partial_inplace)
6375 	addend = _bfd_mips_elf_sign_extend (addend, 28);
6376 
6377       if ((symbol + addend) & 3)
6378 	return bfd_reloc_outofrange;
6379 
6380       value = symbol + addend - p;
6381       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6382 	overflowed_p = mips_elf_overflow_p (value, 28);
6383       value >>= howto->rightshift;
6384       value &= howto->dst_mask;
6385       break;
6386 
6387     case R_MIPS_PC18_S3:
6388       if (howto->partial_inplace)
6389 	addend = _bfd_mips_elf_sign_extend (addend, 21);
6390 
6391       if ((symbol + addend) & 7)
6392 	return bfd_reloc_outofrange;
6393 
6394       value = symbol + addend - ((p | 7) ^ 7);
6395       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6396 	overflowed_p = mips_elf_overflow_p (value, 21);
6397       value >>= howto->rightshift;
6398       value &= howto->dst_mask;
6399       break;
6400 
6401     case R_MIPS_PC19_S2:
6402       if (howto->partial_inplace)
6403 	addend = _bfd_mips_elf_sign_extend (addend, 21);
6404 
6405       if ((symbol + addend) & 3)
6406 	return bfd_reloc_outofrange;
6407 
6408       value = symbol + addend - p;
6409       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6410 	overflowed_p = mips_elf_overflow_p (value, 21);
6411       value >>= howto->rightshift;
6412       value &= howto->dst_mask;
6413       break;
6414 
6415     case R_MIPS_PCHI16:
6416       value = mips_elf_high (symbol + addend - p);
6417       value &= howto->dst_mask;
6418       break;
6419 
6420     case R_MIPS_PCLO16:
6421       if (howto->partial_inplace)
6422 	addend = _bfd_mips_elf_sign_extend (addend, 16);
6423       value = symbol + addend - p;
6424       value &= howto->dst_mask;
6425       break;
6426 
6427     case R_MICROMIPS_PC7_S1:
6428       if (howto->partial_inplace)
6429 	addend = _bfd_mips_elf_sign_extend (addend, 8);
6430 
6431       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6432 	  && (*cross_mode_jump_p
6433 	      ? ((symbol + addend + 2) & 3) != 0
6434 	      : ((symbol + addend + 2) & 1) == 0))
6435 	return bfd_reloc_outofrange;
6436 
6437       value = symbol + addend - p;
6438       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6439 	overflowed_p = mips_elf_overflow_p (value, 8);
6440       value >>= howto->rightshift;
6441       value &= howto->dst_mask;
6442       break;
6443 
6444     case R_MICROMIPS_PC10_S1:
6445       if (howto->partial_inplace)
6446 	addend = _bfd_mips_elf_sign_extend (addend, 11);
6447 
6448       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6449 	  && (*cross_mode_jump_p
6450 	      ? ((symbol + addend + 2) & 3) != 0
6451 	      : ((symbol + addend + 2) & 1) == 0))
6452 	return bfd_reloc_outofrange;
6453 
6454       value = symbol + addend - p;
6455       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6456 	overflowed_p = mips_elf_overflow_p (value, 11);
6457       value >>= howto->rightshift;
6458       value &= howto->dst_mask;
6459       break;
6460 
6461     case R_MICROMIPS_PC16_S1:
6462       if (howto->partial_inplace)
6463 	addend = _bfd_mips_elf_sign_extend (addend, 17);
6464 
6465       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6466 	  && (*cross_mode_jump_p
6467 	      ? ((symbol + addend) & 3) != 0
6468 	      : ((symbol + addend) & 1) == 0))
6469 	return bfd_reloc_outofrange;
6470 
6471       value = symbol + addend - p;
6472       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6473 	overflowed_p = mips_elf_overflow_p (value, 17);
6474       value >>= howto->rightshift;
6475       value &= howto->dst_mask;
6476       break;
6477 
6478     case R_MICROMIPS_PC23_S2:
6479       if (howto->partial_inplace)
6480 	addend = _bfd_mips_elf_sign_extend (addend, 25);
6481       value = symbol + addend - ((p | 3) ^ 3);
6482       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6483 	overflowed_p = mips_elf_overflow_p (value, 25);
6484       value >>= howto->rightshift;
6485       value &= howto->dst_mask;
6486       break;
6487 
6488     case R_MIPS_GOT_HI16:
6489     case R_MIPS_CALL_HI16:
6490     case R_MICROMIPS_GOT_HI16:
6491     case R_MICROMIPS_CALL_HI16:
6492       /* We're allowed to handle these two relocations identically.
6493 	 The dynamic linker is allowed to handle the CALL relocations
6494 	 differently by creating a lazy evaluation stub.  */
6495       value = g;
6496       value = mips_elf_high (value);
6497       value &= howto->dst_mask;
6498       break;
6499 
6500     case R_MIPS_GOT_LO16:
6501     case R_MIPS_CALL_LO16:
6502     case R_MICROMIPS_GOT_LO16:
6503     case R_MICROMIPS_CALL_LO16:
6504       value = g & howto->dst_mask;
6505       break;
6506 
6507     case R_MIPS_GOT_PAGE:
6508     case R_MICROMIPS_GOT_PAGE:
6509       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6510       if (value == MINUS_ONE)
6511 	return bfd_reloc_outofrange;
6512       value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6513       overflowed_p = mips_elf_overflow_p (value, 16);
6514       break;
6515 
6516     case R_MIPS_GOT_OFST:
6517     case R_MICROMIPS_GOT_OFST:
6518       if (local_p)
6519 	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6520       else
6521 	value = addend;
6522       overflowed_p = mips_elf_overflow_p (value, 16);
6523       break;
6524 
6525     case R_MIPS_SUB:
6526     case R_MICROMIPS_SUB:
6527       value = symbol - addend;
6528       value &= howto->dst_mask;
6529       break;
6530 
6531     case R_MIPS_HIGHER:
6532     case R_MICROMIPS_HIGHER:
6533       value = mips_elf_higher (addend + symbol);
6534       value &= howto->dst_mask;
6535       break;
6536 
6537     case R_MIPS_HIGHEST:
6538     case R_MICROMIPS_HIGHEST:
6539       value = mips_elf_highest (addend + symbol);
6540       value &= howto->dst_mask;
6541       break;
6542 
6543     case R_MIPS_SCN_DISP:
6544     case R_MICROMIPS_SCN_DISP:
6545       value = symbol + addend - sec->output_offset;
6546       value &= howto->dst_mask;
6547       break;
6548 
6549     case R_MIPS_JALR:
6550     case R_MICROMIPS_JALR:
6551       /* This relocation is only a hint.  In some cases, we optimize
6552 	 it into a bal instruction.  But we don't try to optimize
6553 	 when the symbol does not resolve locally.  */
6554       if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6555 	return bfd_reloc_continue;
6556       /* We can't optimize cross-mode jumps either.  */
6557       if (*cross_mode_jump_p)
6558 	return bfd_reloc_continue;
6559       value = symbol + addend;
6560       /* Neither we can non-instruction-aligned targets.  */
6561       if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6562 	return bfd_reloc_continue;
6563       break;
6564 
6565     case R_MIPS_PJUMP:
6566     case R_MIPS_GNU_VTINHERIT:
6567     case R_MIPS_GNU_VTENTRY:
6568       /* We don't do anything with these at present.  */
6569       return bfd_reloc_continue;
6570 
6571     default:
6572       /* An unrecognized relocation type.  */
6573       return bfd_reloc_notsupported;
6574     }
6575 
6576   /* Store the VALUE for our caller.  */
6577   *valuep = value;
6578   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6579 }
6580 
6581 /* It has been determined that the result of the RELOCATION is the
6582    VALUE.  Use HOWTO to place VALUE into the output file at the
6583    appropriate position.  The SECTION is the section to which the
6584    relocation applies.
6585    CROSS_MODE_JUMP_P is true if the relocation field
6586    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6587 
6588    Returns FALSE if anything goes wrong.  */
6589 
6590 static bool
6591 mips_elf_perform_relocation (struct bfd_link_info *info,
6592 			     reloc_howto_type *howto,
6593 			     const Elf_Internal_Rela *relocation,
6594 			     bfd_vma value, bfd *input_bfd,
6595 			     asection *input_section, bfd_byte *contents,
6596 			     bool cross_mode_jump_p)
6597 {
6598   bfd_vma x;
6599   bfd_byte *location;
6600   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6601 
6602   /* Figure out where the relocation is occurring.  */
6603   location = contents + relocation->r_offset;
6604 
6605   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6606 
6607   /* Obtain the current value.  */
6608   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6609 
6610   /* Clear the field we are setting.  */
6611   x &= ~howto->dst_mask;
6612 
6613   /* Set the field.  */
6614   x |= (value & howto->dst_mask);
6615 
6616   /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
6617   if (!cross_mode_jump_p && jal_reloc_p (r_type))
6618     {
6619       bfd_vma opcode = x >> 26;
6620 
6621       if (r_type == R_MIPS16_26 ? opcode == 0x7
6622 	  : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6623 	  : opcode == 0x1d)
6624 	{
6625 	  info->callbacks->einfo
6626 	    (_("%X%H: unsupported JALX to the same ISA mode\n"),
6627 	     input_bfd, input_section, relocation->r_offset);
6628 	  return true;
6629 	}
6630     }
6631   if (cross_mode_jump_p && jal_reloc_p (r_type))
6632     {
6633       bool ok;
6634       bfd_vma opcode = x >> 26;
6635       bfd_vma jalx_opcode;
6636 
6637       /* Check to see if the opcode is already JAL or JALX.  */
6638       if (r_type == R_MIPS16_26)
6639 	{
6640 	  ok = ((opcode == 0x6) || (opcode == 0x7));
6641 	  jalx_opcode = 0x7;
6642 	}
6643       else if (r_type == R_MICROMIPS_26_S1)
6644 	{
6645 	  ok = ((opcode == 0x3d) || (opcode == 0x3c));
6646 	  jalx_opcode = 0x3c;
6647 	}
6648       else
6649 	{
6650 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
6651 	  jalx_opcode = 0x1d;
6652 	}
6653 
6654       /* If the opcode is not JAL or JALX, there's a problem.  We cannot
6655 	 convert J or JALS to JALX.  */
6656       if (!ok)
6657 	{
6658 	  info->callbacks->einfo
6659 	    (_("%X%H: unsupported jump between ISA modes; "
6660 	       "consider recompiling with interlinking enabled\n"),
6661 	     input_bfd, input_section, relocation->r_offset);
6662 	  return true;
6663 	}
6664 
6665       /* Make this the JALX opcode.  */
6666       x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6667     }
6668   else if (cross_mode_jump_p && b_reloc_p (r_type))
6669     {
6670       bool ok = false;
6671       bfd_vma opcode = x >> 16;
6672       bfd_vma jalx_opcode = 0;
6673       bfd_vma sign_bit = 0;
6674       bfd_vma addr;
6675       bfd_vma dest;
6676 
6677       if (r_type == R_MICROMIPS_PC16_S1)
6678 	{
6679 	  ok = opcode == 0x4060;
6680 	  jalx_opcode = 0x3c;
6681 	  sign_bit = 0x10000;
6682 	  value <<= 1;
6683 	}
6684       else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6685 	{
6686 	  ok = opcode == 0x411;
6687 	  jalx_opcode = 0x1d;
6688 	  sign_bit = 0x20000;
6689 	  value <<= 2;
6690 	}
6691 
6692       if (ok && !bfd_link_pic (info))
6693 	{
6694 	  addr = (input_section->output_section->vma
6695 		  + input_section->output_offset
6696 		  + relocation->r_offset
6697 		  + 4);
6698 	  dest = (addr
6699 		  + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6700 
6701 	  if ((addr >> 28) << 28 != (dest >> 28) << 28)
6702 	    {
6703 	      info->callbacks->einfo
6704 		(_("%X%H: cannot convert branch between ISA modes "
6705 		   "to JALX: relocation out of range\n"),
6706 		 input_bfd, input_section, relocation->r_offset);
6707 	      return true;
6708 	    }
6709 
6710 	  /* Make this the JALX opcode.  */
6711 	  x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6712 	}
6713       else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6714 	{
6715 	  info->callbacks->einfo
6716 	    (_("%X%H: unsupported branch between ISA modes\n"),
6717 	     input_bfd, input_section, relocation->r_offset);
6718 	  return true;
6719 	}
6720     }
6721 
6722   /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6723      range.  */
6724   if (!bfd_link_relocatable (info)
6725       && !cross_mode_jump_p
6726       && ((JAL_TO_BAL_P (input_bfd)
6727 	   && r_type == R_MIPS_26
6728 	   && (x >> 26) == 0x3)			/* jal addr */
6729 	  || (JALR_TO_BAL_P (input_bfd)
6730 	      && r_type == R_MIPS_JALR
6731 	      && x == 0x0320f809)		/* jalr t9 */
6732 	  || (JR_TO_B_P (input_bfd)
6733 	      && r_type == R_MIPS_JALR
6734 	      && (x & ~1) == 0x03200008)))	/* jr t9 / jalr zero, t9 */
6735     {
6736       bfd_vma addr;
6737       bfd_vma dest;
6738       bfd_signed_vma off;
6739 
6740       addr = (input_section->output_section->vma
6741 	      + input_section->output_offset
6742 	      + relocation->r_offset
6743 	      + 4);
6744       if (r_type == R_MIPS_26)
6745 	dest = (value << 2) | ((addr >> 28) << 28);
6746       else
6747 	dest = value;
6748       off = dest - addr;
6749       if (off <= 0x1ffff && off >= -0x20000)
6750 	{
6751 	  if ((x & ~1) == 0x03200008)		/* jr t9 / jalr zero, t9 */
6752 	    x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
6753 	  else
6754 	    x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
6755 	}
6756     }
6757 
6758   /* Put the value into the output.  */
6759   mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6760 
6761   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6762 			       location);
6763 
6764   return true;
6765 }
6766 
6767 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
6768    is the original relocation, which is now being transformed into a
6769    dynamic relocation.  The ADDENDP is adjusted if necessary; the
6770    caller should store the result in place of the original addend.  */
6771 
6772 static bool
6773 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6774 				    struct bfd_link_info *info,
6775 				    const Elf_Internal_Rela *rel,
6776 				    struct mips_elf_link_hash_entry *h,
6777 				    asection *sec, bfd_vma symbol,
6778 				    bfd_vma *addendp, asection *input_section)
6779 {
6780   Elf_Internal_Rela outrel[3];
6781   asection *sreloc;
6782   bfd *dynobj;
6783   int r_type;
6784   long indx;
6785   bool defined_p;
6786   struct mips_elf_link_hash_table *htab;
6787 
6788   htab = mips_elf_hash_table (info);
6789   BFD_ASSERT (htab != NULL);
6790 
6791   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6792   dynobj = elf_hash_table (info)->dynobj;
6793   sreloc = mips_elf_rel_dyn_section (info, false);
6794   BFD_ASSERT (sreloc != NULL);
6795   BFD_ASSERT (sreloc->contents != NULL);
6796   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6797 	      < sreloc->size);
6798 
6799   outrel[0].r_offset =
6800     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6801   if (ABI_64_P (output_bfd))
6802     {
6803       outrel[1].r_offset =
6804 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6805       outrel[2].r_offset =
6806 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6807     }
6808 
6809   if (outrel[0].r_offset == MINUS_ONE)
6810     /* The relocation field has been deleted.  */
6811     return true;
6812 
6813   if (outrel[0].r_offset == MINUS_TWO)
6814     {
6815       /* The relocation field has been converted into a relative value of
6816 	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
6817 	 the field to be fully relocated, so add in the symbol's value.  */
6818       *addendp += symbol;
6819       return true;
6820     }
6821 
6822   /* We must now calculate the dynamic symbol table index to use
6823      in the relocation.  */
6824   if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6825     {
6826       BFD_ASSERT (htab->root.target_os == is_vxworks
6827 		  || h->global_got_area != GGA_NONE);
6828       indx = h->root.dynindx;
6829       if (SGI_COMPAT (output_bfd))
6830 	defined_p = h->root.def_regular;
6831       else
6832 	/* ??? glibc's ld.so just adds the final GOT entry to the
6833 	   relocation field.  It therefore treats relocs against
6834 	   defined symbols in the same way as relocs against
6835 	   undefined symbols.  */
6836 	defined_p = false;
6837     }
6838   else
6839     {
6840       if (sec != NULL && bfd_is_abs_section (sec))
6841 	indx = 0;
6842       else if (sec == NULL || sec->owner == NULL)
6843 	{
6844 	  BFD_ASSERT (0);
6845 	  bfd_set_error (bfd_error_bad_value);
6846 	  return false;
6847 	}
6848       else
6849 	{
6850 	  indx = elf_section_data (sec->output_section)->dynindx;
6851 	  if (indx == 0)
6852 	    {
6853 	      asection *osec = htab->root.text_index_section;
6854 	      indx = elf_section_data (osec)->dynindx;
6855 	    }
6856 	  if (indx == 0)
6857 	    abort ();
6858 	}
6859 
6860       /* Instead of generating a relocation using the section
6861 	 symbol, we may as well make it a fully relative
6862 	 relocation.  We want to avoid generating relocations to
6863 	 local symbols because we used to generate them
6864 	 incorrectly, without adding the original symbol value,
6865 	 which is mandated by the ABI for section symbols.  In
6866 	 order to give dynamic loaders and applications time to
6867 	 phase out the incorrect use, we refrain from emitting
6868 	 section-relative relocations.  It's not like they're
6869 	 useful, after all.  This should be a bit more efficient
6870 	 as well.  */
6871       /* ??? Although this behavior is compatible with glibc's ld.so,
6872 	 the ABI says that relocations against STN_UNDEF should have
6873 	 a symbol value of 0.  Irix rld honors this, so relocations
6874 	 against STN_UNDEF have no effect.  */
6875       if (!SGI_COMPAT (output_bfd))
6876 	indx = 0;
6877       defined_p = true;
6878     }
6879 
6880   /* If the relocation was previously an absolute relocation and
6881      this symbol will not be referred to by the relocation, we must
6882      adjust it by the value we give it in the dynamic symbol table.
6883      Otherwise leave the job up to the dynamic linker.  */
6884   if (defined_p && r_type != R_MIPS_REL32)
6885     *addendp += symbol;
6886 
6887   if (htab->root.target_os == is_vxworks)
6888     /* VxWorks uses non-relative relocations for this.  */
6889     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6890   else
6891     /* The relocation is always an REL32 relocation because we don't
6892        know where the shared library will wind up at load-time.  */
6893     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6894 				   R_MIPS_REL32);
6895 
6896   /* For strict adherence to the ABI specification, we should
6897      generate a R_MIPS_64 relocation record by itself before the
6898      _REL32/_64 record as well, such that the addend is read in as
6899      a 64-bit value (REL32 is a 32-bit relocation, after all).
6900      However, since none of the existing ELF64 MIPS dynamic
6901      loaders seems to care, we don't waste space with these
6902      artificial relocations.  If this turns out to not be true,
6903      mips_elf_allocate_dynamic_relocation() should be tweaked so
6904      as to make room for a pair of dynamic relocations per
6905      invocation if ABI_64_P, and here we should generate an
6906      additional relocation record with R_MIPS_64 by itself for a
6907      NULL symbol before this relocation record.  */
6908   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6909 				 ABI_64_P (output_bfd)
6910 				 ? R_MIPS_64
6911 				 : R_MIPS_NONE);
6912   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6913 
6914   /* Adjust the output offset of the relocation to reference the
6915      correct location in the output file.  */
6916   outrel[0].r_offset += (input_section->output_section->vma
6917 			 + input_section->output_offset);
6918   outrel[1].r_offset += (input_section->output_section->vma
6919 			 + input_section->output_offset);
6920   outrel[2].r_offset += (input_section->output_section->vma
6921 			 + input_section->output_offset);
6922 
6923   /* Put the relocation back out.  We have to use the special
6924      relocation outputter in the 64-bit case since the 64-bit
6925      relocation format is non-standard.  */
6926   if (ABI_64_P (output_bfd))
6927     {
6928       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6929 	(output_bfd, &outrel[0],
6930 	 (sreloc->contents
6931 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6932     }
6933   else if (htab->root.target_os == is_vxworks)
6934     {
6935       /* VxWorks uses RELA rather than REL dynamic relocations.  */
6936       outrel[0].r_addend = *addendp;
6937       bfd_elf32_swap_reloca_out
6938 	(output_bfd, &outrel[0],
6939 	 (sreloc->contents
6940 	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6941     }
6942   else
6943     bfd_elf32_swap_reloc_out
6944       (output_bfd, &outrel[0],
6945        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6946 
6947   /* We've now added another relocation.  */
6948   ++sreloc->reloc_count;
6949 
6950   /* Make sure the output section is writable.  The dynamic linker
6951      will be writing to it.  */
6952   elf_section_data (input_section->output_section)->this_hdr.sh_flags
6953     |= SHF_WRITE;
6954 
6955   /* On IRIX5, make an entry of compact relocation info.  */
6956   if (IRIX_COMPAT (output_bfd) == ict_irix5)
6957     {
6958       asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6959       bfd_byte *cr;
6960 
6961       if (scpt)
6962 	{
6963 	  Elf32_crinfo cptrel;
6964 
6965 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6966 	  cptrel.vaddr = (rel->r_offset
6967 			  + input_section->output_section->vma
6968 			  + input_section->output_offset);
6969 	  if (r_type == R_MIPS_REL32)
6970 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6971 	  else
6972 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6973 	  mips_elf_set_cr_dist2to (cptrel, 0);
6974 	  cptrel.konst = *addendp;
6975 
6976 	  cr = (scpt->contents
6977 		+ sizeof (Elf32_External_compact_rel));
6978 	  mips_elf_set_cr_relvaddr (cptrel, 0);
6979 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6980 				     ((Elf32_External_crinfo *) cr
6981 				      + scpt->reloc_count));
6982 	  ++scpt->reloc_count;
6983 	}
6984     }
6985 
6986   /* If we've written this relocation for a readonly section,
6987      we need to set DF_TEXTREL again, so that we do not delete the
6988      DT_TEXTREL tag.  */
6989   if (MIPS_ELF_READONLY_SECTION (input_section))
6990     info->flags |= DF_TEXTREL;
6991 
6992   return true;
6993 }
6994 
6995 /* Return the MACH for a MIPS e_flags value.  */
6996 
6997 unsigned long
6998 _bfd_elf_mips_mach (flagword flags)
6999 {
7000   switch (flags & EF_MIPS_MACH)
7001     {
7002     case EF_MIPS_MACH_3900:
7003       return bfd_mach_mips3900;
7004 
7005     case EF_MIPS_MACH_4010:
7006       return bfd_mach_mips4010;
7007 
7008     case EF_MIPS_MACH_ALLEGREX:
7009       return bfd_mach_mips_allegrex;
7010 
7011     case EF_MIPS_MACH_4100:
7012       return bfd_mach_mips4100;
7013 
7014     case EF_MIPS_MACH_4111:
7015       return bfd_mach_mips4111;
7016 
7017     case EF_MIPS_MACH_4120:
7018       return bfd_mach_mips4120;
7019 
7020     case EF_MIPS_MACH_4650:
7021       return bfd_mach_mips4650;
7022 
7023     case EF_MIPS_MACH_5400:
7024       return bfd_mach_mips5400;
7025 
7026     case EF_MIPS_MACH_5500:
7027       return bfd_mach_mips5500;
7028 
7029     case EF_MIPS_MACH_5900:
7030       return bfd_mach_mips5900;
7031 
7032     case EF_MIPS_MACH_9000:
7033       return bfd_mach_mips9000;
7034 
7035     case EF_MIPS_MACH_SB1:
7036       return bfd_mach_mips_sb1;
7037 
7038     case EF_MIPS_MACH_LS2E:
7039       return bfd_mach_mips_loongson_2e;
7040 
7041     case EF_MIPS_MACH_LS2F:
7042       return bfd_mach_mips_loongson_2f;
7043 
7044     case EF_MIPS_MACH_GS464:
7045       return bfd_mach_mips_gs464;
7046 
7047     case EF_MIPS_MACH_GS464E:
7048       return bfd_mach_mips_gs464e;
7049 
7050     case EF_MIPS_MACH_GS264E:
7051       return bfd_mach_mips_gs264e;
7052 
7053     case EF_MIPS_MACH_OCTEON3:
7054       return bfd_mach_mips_octeon3;
7055 
7056     case EF_MIPS_MACH_OCTEON2:
7057       return bfd_mach_mips_octeon2;
7058 
7059     case EF_MIPS_MACH_OCTEON:
7060       return bfd_mach_mips_octeon;
7061 
7062     case EF_MIPS_MACH_XLR:
7063       return bfd_mach_mips_xlr;
7064 
7065     case EF_MIPS_MACH_IAMR2:
7066       return bfd_mach_mips_interaptiv_mr2;
7067 
7068     default:
7069       switch (flags & EF_MIPS_ARCH)
7070 	{
7071 	default:
7072 	case EF_MIPS_ARCH_1:
7073 	  return bfd_mach_mips3000;
7074 
7075 	case EF_MIPS_ARCH_2:
7076 	  return bfd_mach_mips6000;
7077 
7078 	case EF_MIPS_ARCH_3:
7079 	  return bfd_mach_mips4000;
7080 
7081 	case EF_MIPS_ARCH_4:
7082 	  return bfd_mach_mips8000;
7083 
7084 	case EF_MIPS_ARCH_5:
7085 	  return bfd_mach_mips5;
7086 
7087 	case EF_MIPS_ARCH_32:
7088 	  return bfd_mach_mipsisa32;
7089 
7090 	case EF_MIPS_ARCH_64:
7091 	  return bfd_mach_mipsisa64;
7092 
7093 	case EF_MIPS_ARCH_32R2:
7094 	  return bfd_mach_mipsisa32r2;
7095 
7096 	case EF_MIPS_ARCH_64R2:
7097 	  return bfd_mach_mipsisa64r2;
7098 
7099 	case EF_MIPS_ARCH_32R6:
7100 	  return bfd_mach_mipsisa32r6;
7101 
7102 	case EF_MIPS_ARCH_64R6:
7103 	  return bfd_mach_mipsisa64r6;
7104 	}
7105     }
7106 
7107   return 0;
7108 }
7109 
7110 /* Return printable name for ABI.  */
7111 
7112 static inline char *
7113 elf_mips_abi_name (bfd *abfd)
7114 {
7115   flagword flags;
7116 
7117   flags = elf_elfheader (abfd)->e_flags;
7118   switch (flags & EF_MIPS_ABI)
7119     {
7120     case 0:
7121       if (ABI_N32_P (abfd))
7122 	return "N32";
7123       else if (ABI_64_P (abfd))
7124 	return "64";
7125       else
7126 	return "none";
7127     case EF_MIPS_ABI_O32:
7128       return "O32";
7129     case EF_MIPS_ABI_O64:
7130       return "O64";
7131     case EF_MIPS_ABI_EABI32:
7132       return "EABI32";
7133     case EF_MIPS_ABI_EABI64:
7134       return "EABI64";
7135     default:
7136       return "unknown abi";
7137     }
7138 }
7139 
7140 /* MIPS ELF uses two common sections.  One is the usual one, and the
7141    other is for small objects.  All the small objects are kept
7142    together, and then referenced via the gp pointer, which yields
7143    faster assembler code.  This is what we use for the small common
7144    section.  This approach is copied from ecoff.c.  */
7145 static asection mips_elf_scom_section;
7146 static const asymbol mips_elf_scom_symbol =
7147   GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7148 static asection mips_elf_scom_section =
7149   BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7150 		    ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7151 
7152 /* MIPS ELF also uses an acommon section, which represents an
7153    allocated common symbol which may be overridden by a
7154    definition in a shared library.  */
7155 static asection mips_elf_acom_section;
7156 static const asymbol mips_elf_acom_symbol =
7157   GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7158 static asection mips_elf_acom_section =
7159   BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7160 		    ".acommon", 0, SEC_ALLOC);
7161 
7162 /* This is used for both the 32-bit and the 64-bit ABI.  */
7163 
7164 void
7165 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7166 {
7167   elf_symbol_type *elfsym;
7168 
7169   /* Handle the special MIPS section numbers that a symbol may use.  */
7170   elfsym = (elf_symbol_type *) asym;
7171   switch (elfsym->internal_elf_sym.st_shndx)
7172     {
7173     case SHN_MIPS_ACOMMON:
7174       /* This section is used in a dynamically linked executable file.
7175 	 It is an allocated common section.  The dynamic linker can
7176 	 either resolve these symbols to something in a shared
7177 	 library, or it can just leave them here.  For our purposes,
7178 	 we can consider these symbols to be in a new section.  */
7179       asym->section = &mips_elf_acom_section;
7180       break;
7181 
7182     case SHN_COMMON:
7183       /* Common symbols less than the GP size are automatically
7184 	 treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7185       if (asym->value > elf_gp_size (abfd)
7186 	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7187 	  || IRIX_COMPAT (abfd) == ict_irix6
7188 	  || strcmp (asym->name, "__gnu_lto_slim") == 0)
7189 	break;
7190       /* Fall through.  */
7191     case SHN_MIPS_SCOMMON:
7192       asym->section = &mips_elf_scom_section;
7193       asym->value = elfsym->internal_elf_sym.st_size;
7194       break;
7195 
7196     case SHN_MIPS_SUNDEFINED:
7197       asym->section = bfd_und_section_ptr;
7198       break;
7199 
7200     case SHN_MIPS_TEXT:
7201       {
7202 	asection *section = bfd_get_section_by_name (abfd, ".text");
7203 
7204 	if (section != NULL)
7205 	  {
7206 	    asym->section = section;
7207 	    /* MIPS_TEXT is a bit special, the address is not an offset
7208 	       to the base of the .text section.  So subtract the section
7209 	       base address to make it an offset.  */
7210 	    asym->value -= section->vma;
7211 	  }
7212       }
7213       break;
7214 
7215     case SHN_MIPS_DATA:
7216       {
7217 	asection *section = bfd_get_section_by_name (abfd, ".data");
7218 
7219 	if (section != NULL)
7220 	  {
7221 	    asym->section = section;
7222 	    /* MIPS_DATA is a bit special, the address is not an offset
7223 	       to the base of the .data section.  So subtract the section
7224 	       base address to make it an offset.  */
7225 	    asym->value -= section->vma;
7226 	  }
7227       }
7228       break;
7229     }
7230 
7231   /* If this is an odd-valued function symbol, assume it's a MIPS16
7232      or microMIPS one.  */
7233   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7234       && (asym->value & 1) != 0)
7235     {
7236       asym->value--;
7237       if (MICROMIPS_P (abfd))
7238 	elfsym->internal_elf_sym.st_other
7239 	  = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7240       else
7241 	elfsym->internal_elf_sym.st_other
7242 	  = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7243     }
7244 }
7245 
7246 /* Implement elf_backend_eh_frame_address_size.  This differs from
7247    the default in the way it handles EABI64.
7248 
7249    EABI64 was originally specified as an LP64 ABI, and that is what
7250    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
7251    historically accepted the combination of -mabi=eabi and -mlong32,
7252    and this ILP32 variation has become semi-official over time.
7253    Both forms use elf32 and have pointer-sized FDE addresses.
7254 
7255    If an EABI object was generated by GCC 4.0 or above, it will have
7256    an empty .gcc_compiled_longXX section, where XX is the size of longs
7257    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
7258    have no special marking to distinguish them from LP64 objects.
7259 
7260    We don't want users of the official LP64 ABI to be punished for the
7261    existence of the ILP32 variant, but at the same time, we don't want
7262    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7263    We therefore take the following approach:
7264 
7265       - If ABFD contains a .gcc_compiled_longXX section, use it to
7266 	determine the pointer size.
7267 
7268       - Otherwise check the type of the first relocation.  Assume that
7269 	the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7270 
7271       - Otherwise punt.
7272 
7273    The second check is enough to detect LP64 objects generated by pre-4.0
7274    compilers because, in the kind of output generated by those compilers,
7275    the first relocation will be associated with either a CIE personality
7276    routine or an FDE start address.  Furthermore, the compilers never
7277    used a special (non-pointer) encoding for this ABI.
7278 
7279    Checking the relocation type should also be safe because there is no
7280    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
7281    did so.  */
7282 
7283 unsigned int
7284 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7285 {
7286   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7287     return 8;
7288   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
7289     {
7290       bool long32_p, long64_p;
7291 
7292       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7293       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7294       if (long32_p && long64_p)
7295 	return 0;
7296       if (long32_p)
7297 	return 4;
7298       if (long64_p)
7299 	return 8;
7300 
7301       if (sec->reloc_count > 0)
7302 	{
7303 	  /* Load the relocations for this section.  */
7304 	  Elf_Internal_Rela *internal_relocs =
7305 	    _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
7306 	  if (internal_relocs == NULL)
7307 	    return 0;
7308 
7309 	  unsigned int size = 0;
7310 	  if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
7311 	    size = 8;
7312 
7313 	  if (elf_section_data (sec)->relocs != internal_relocs)
7314 	    free (internal_relocs);
7315 
7316 	  return size;
7317 	}
7318 
7319       return 0;
7320     }
7321   return 4;
7322 }
7323 
7324 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7325    relocations against two unnamed section symbols to resolve to the
7326    same address.  For example, if we have code like:
7327 
7328 	lw	$4,%got_disp(.data)($gp)
7329 	lw	$25,%got_disp(.text)($gp)
7330 	jalr	$25
7331 
7332    then the linker will resolve both relocations to .data and the program
7333    will jump there rather than to .text.
7334 
7335    We can work around this problem by giving names to local section symbols.
7336    This is also what the MIPSpro tools do.  */
7337 
7338 bool
7339 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7340 {
7341   return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7342 }
7343 
7344 /* Work over a section just before writing it out.  This routine is
7345    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
7346    sections that need the SHF_MIPS_GPREL flag by name; there has to be
7347    a better way.  */
7348 
7349 bool
7350 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7351 {
7352   if (hdr->sh_type == SHT_MIPS_REGINFO
7353       && hdr->sh_size > 0)
7354     {
7355       bfd_byte buf[4];
7356 
7357       BFD_ASSERT (hdr->contents == NULL);
7358 
7359       if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7360 	{
7361 	  _bfd_error_handler
7362 	    (_("%pB: incorrect `.reginfo' section size; "
7363 	       "expected %" PRIu64 ", got %" PRIu64),
7364 	     abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7365 	     (uint64_t) hdr->sh_size);
7366 	  bfd_set_error (bfd_error_bad_value);
7367 	  return false;
7368 	}
7369 
7370       if (bfd_seek (abfd,
7371 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7372 		    SEEK_SET) != 0)
7373 	return false;
7374       H_PUT_32 (abfd, elf_gp (abfd), buf);
7375       if (bfd_write (buf, 4, abfd) != 4)
7376 	return false;
7377     }
7378 
7379   if (hdr->sh_type == SHT_MIPS_OPTIONS
7380       && hdr->bfd_section != NULL
7381       && mips_elf_section_data (hdr->bfd_section) != NULL
7382       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7383     {
7384       bfd_byte *contents, *l, *lend;
7385 
7386       /* We stored the section contents in the tdata field in the
7387 	 set_section_contents routine.  We save the section contents
7388 	 so that we don't have to read them again.
7389 	 At this point we know that elf_gp is set, so we can look
7390 	 through the section contents to see if there is an
7391 	 ODK_REGINFO structure.  */
7392 
7393       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7394       l = contents;
7395       lend = contents + hdr->sh_size;
7396       while (l + sizeof (Elf_External_Options) <= lend)
7397 	{
7398 	  Elf_Internal_Options intopt;
7399 
7400 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7401 					&intopt);
7402 	  if (intopt.size < sizeof (Elf_External_Options))
7403 	    {
7404 	      _bfd_error_handler
7405 		/* xgettext:c-format */
7406 		(_("%pB: warning: bad `%s' option size %u smaller than"
7407 		   " its header"),
7408 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7409 	      break;
7410 	    }
7411 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7412 	    {
7413 	      bfd_byte buf[8];
7414 
7415 	      if (bfd_seek (abfd,
7416 			    (hdr->sh_offset
7417 			     + (l - contents)
7418 			     + sizeof (Elf_External_Options)
7419 			     + (sizeof (Elf64_External_RegInfo) - 8)),
7420 			     SEEK_SET) != 0)
7421 		return false;
7422 	      H_PUT_64 (abfd, elf_gp (abfd), buf);
7423 	      if (bfd_write (buf, 8, abfd) != 8)
7424 		return false;
7425 	    }
7426 	  else if (intopt.kind == ODK_REGINFO)
7427 	    {
7428 	      bfd_byte buf[4];
7429 
7430 	      if (bfd_seek (abfd,
7431 			    (hdr->sh_offset
7432 			     + (l - contents)
7433 			     + sizeof (Elf_External_Options)
7434 			     + (sizeof (Elf32_External_RegInfo) - 4)),
7435 			    SEEK_SET) != 0)
7436 		return false;
7437 	      H_PUT_32 (abfd, elf_gp (abfd), buf);
7438 	      if (bfd_write (buf, 4, abfd) != 4)
7439 		return false;
7440 	    }
7441 	  l += intopt.size;
7442 	}
7443     }
7444 
7445   if (hdr->bfd_section != NULL)
7446     {
7447       const char *name = bfd_section_name (hdr->bfd_section);
7448 
7449       /* .sbss is not handled specially here because the GNU/Linux
7450 	 prelinker can convert .sbss from NOBITS to PROGBITS and
7451 	 changing it back to NOBITS breaks the binary.  The entry in
7452 	 _bfd_mips_elf_special_sections will ensure the correct flags
7453 	 are set on .sbss if BFD creates it without reading it from an
7454 	 input file, and without special handling here the flags set
7455 	 on it in an input file will be followed.  */
7456       if (strcmp (name, ".sdata") == 0
7457 	  || strcmp (name, ".lit8") == 0
7458 	  || strcmp (name, ".lit4") == 0)
7459 	hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7460       else if (strcmp (name, ".srdata") == 0)
7461 	hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7462       else if (strcmp (name, ".compact_rel") == 0)
7463 	hdr->sh_flags = 0;
7464       else if (strcmp (name, ".rtproc") == 0)
7465 	{
7466 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7467 	    {
7468 	      unsigned int adjust;
7469 
7470 	      adjust = hdr->sh_size % hdr->sh_addralign;
7471 	      if (adjust != 0)
7472 		hdr->sh_size += hdr->sh_addralign - adjust;
7473 	    }
7474 	}
7475     }
7476 
7477   return true;
7478 }
7479 
7480 /* Handle a MIPS specific section when reading an object file.  This
7481    is called when elfcode.h finds a section with an unknown type.
7482    This routine supports both the 32-bit and 64-bit ELF ABI.  */
7483 
7484 bool
7485 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7486 				 Elf_Internal_Shdr *hdr,
7487 				 const char *name,
7488 				 int shindex)
7489 {
7490   flagword flags = 0;
7491 
7492   /* There ought to be a place to keep ELF backend specific flags, but
7493      at the moment there isn't one.  We just keep track of the
7494      sections by their name, instead.  Fortunately, the ABI gives
7495      suggested names for all the MIPS specific sections, so we will
7496      probably get away with this.  */
7497   switch (hdr->sh_type)
7498     {
7499     case SHT_MIPS_LIBLIST:
7500       if (strcmp (name, ".liblist") != 0)
7501 	return false;
7502       break;
7503     case SHT_MIPS_MSYM:
7504       if (strcmp (name, ".msym") != 0)
7505 	return false;
7506       break;
7507     case SHT_MIPS_CONFLICT:
7508       if (strcmp (name, ".conflict") != 0)
7509 	return false;
7510       break;
7511     case SHT_MIPS_GPTAB:
7512       if (! startswith (name, ".gptab."))
7513 	return false;
7514       break;
7515     case SHT_MIPS_UCODE:
7516       if (strcmp (name, ".ucode") != 0)
7517 	return false;
7518       break;
7519     case SHT_MIPS_DEBUG:
7520       if (strcmp (name, ".mdebug") != 0)
7521 	return false;
7522       flags = SEC_DEBUGGING;
7523       break;
7524     case SHT_MIPS_REGINFO:
7525       if (strcmp (name, ".reginfo") != 0
7526 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7527 	return false;
7528       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7529       break;
7530     case SHT_MIPS_IFACE:
7531       if (strcmp (name, ".MIPS.interfaces") != 0)
7532 	return false;
7533       break;
7534     case SHT_MIPS_CONTENT:
7535       if (! startswith (name, ".MIPS.content"))
7536 	return false;
7537       break;
7538     case SHT_MIPS_OPTIONS:
7539       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7540 	return false;
7541       break;
7542     case SHT_MIPS_ABIFLAGS:
7543       if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7544 	return false;
7545       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7546       break;
7547     case SHT_MIPS_DWARF:
7548       if (! startswith (name, ".debug_")
7549          && ! startswith (name, ".gnu.debuglto_.debug_")
7550          && ! startswith (name, ".zdebug_")
7551          && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7552 	return false;
7553       break;
7554     case SHT_MIPS_SYMBOL_LIB:
7555       if (strcmp (name, ".MIPS.symlib") != 0)
7556 	return false;
7557       break;
7558     case SHT_MIPS_EVENTS:
7559       if (! startswith (name, ".MIPS.events")
7560 	  && ! startswith (name, ".MIPS.post_rel"))
7561 	return false;
7562       break;
7563     case SHT_MIPS_XHASH:
7564       if (strcmp (name, ".MIPS.xhash") != 0)
7565 	return false;
7566     default:
7567       break;
7568     }
7569 
7570   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7571     return false;
7572 
7573   if (hdr->sh_flags & SHF_MIPS_GPREL)
7574     flags |= SEC_SMALL_DATA;
7575 
7576   if (flags)
7577     {
7578       if (!bfd_set_section_flags (hdr->bfd_section,
7579 				  (bfd_section_flags (hdr->bfd_section)
7580 				   | flags)))
7581 	return false;
7582     }
7583 
7584   if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7585     {
7586       Elf_External_ABIFlags_v0 ext;
7587 
7588       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7589 				      &ext, 0, sizeof ext))
7590 	return false;
7591       bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7592 					&mips_elf_tdata (abfd)->abiflags);
7593       if (mips_elf_tdata (abfd)->abiflags.version != 0)
7594 	return false;
7595       mips_elf_tdata (abfd)->abiflags_valid = true;
7596     }
7597 
7598   /* FIXME: We should record sh_info for a .gptab section.  */
7599 
7600   /* For a .reginfo section, set the gp value in the tdata information
7601      from the contents of this section.  We need the gp value while
7602      processing relocs, so we just get it now.  The .reginfo section
7603      is not used in the 64-bit MIPS ELF ABI.  */
7604   if (hdr->sh_type == SHT_MIPS_REGINFO)
7605     {
7606       Elf32_External_RegInfo ext;
7607       Elf32_RegInfo s;
7608 
7609       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7610 				      &ext, 0, sizeof ext))
7611 	return false;
7612       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7613       elf_gp (abfd) = s.ri_gp_value;
7614     }
7615 
7616   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7617      set the gp value based on what we find.  We may see both
7618      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7619      they should agree.  */
7620   if (hdr->sh_type == SHT_MIPS_OPTIONS)
7621     {
7622       bfd_byte *contents, *l, *lend;
7623 
7624       if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7625 	{
7626 	  free (contents);
7627 	  return false;
7628 	}
7629       l = contents;
7630       lend = contents + hdr->sh_size;
7631       while (l + sizeof (Elf_External_Options) <= lend)
7632 	{
7633 	  Elf_Internal_Options intopt;
7634 
7635 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7636 					&intopt);
7637 	  if (intopt.size < sizeof (Elf_External_Options))
7638 	    {
7639 	    bad_opt:
7640 	      _bfd_error_handler
7641 		/* xgettext:c-format */
7642 		(_("%pB: warning: truncated `%s' option"),
7643 		 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7644 	      break;
7645 	    }
7646 	  if (intopt.kind == ODK_REGINFO)
7647 	    {
7648 	      if (ABI_64_P (abfd))
7649 		{
7650 		  Elf64_Internal_RegInfo intreg;
7651 		  size_t needed = (sizeof (Elf_External_Options)
7652 				   + sizeof (Elf64_External_RegInfo));
7653 		  if (intopt.size < needed || (size_t) (lend - l) < needed)
7654 		    goto bad_opt;
7655 		  bfd_mips_elf64_swap_reginfo_in
7656 		    (abfd,
7657 		     ((Elf64_External_RegInfo *)
7658 		      (l + sizeof (Elf_External_Options))),
7659 		     &intreg);
7660 		  elf_gp (abfd) = intreg.ri_gp_value;
7661 		}
7662 	      else
7663 		{
7664 		  Elf32_RegInfo intreg;
7665 		  size_t needed = (sizeof (Elf_External_Options)
7666 				   + sizeof (Elf32_External_RegInfo));
7667 		  if (intopt.size < needed || (size_t) (lend - l) < needed)
7668 		    goto bad_opt;
7669 		  bfd_mips_elf32_swap_reginfo_in
7670 		    (abfd,
7671 		     ((Elf32_External_RegInfo *)
7672 		      (l + sizeof (Elf_External_Options))),
7673 		     &intreg);
7674 		  elf_gp (abfd) = intreg.ri_gp_value;
7675 		}
7676 	    }
7677 	  l += intopt.size;
7678 	}
7679       free (contents);
7680     }
7681 
7682   return true;
7683 }
7684 
7685 /* Set the correct type for a MIPS ELF section.  We do this by the
7686    section name, which is a hack, but ought to work.  This routine is
7687    used by both the 32-bit and the 64-bit ABI.  */
7688 
7689 bool
7690 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7691 {
7692   const char *name = bfd_section_name (sec);
7693 
7694   if (strcmp (name, ".liblist") == 0)
7695     {
7696       hdr->sh_type = SHT_MIPS_LIBLIST;
7697       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7698       /* The sh_link field is set in final_write_processing.  */
7699     }
7700   else if (strcmp (name, ".conflict") == 0)
7701     hdr->sh_type = SHT_MIPS_CONFLICT;
7702   else if (startswith (name, ".gptab."))
7703     {
7704       hdr->sh_type = SHT_MIPS_GPTAB;
7705       hdr->sh_entsize = sizeof (Elf32_External_gptab);
7706       /* The sh_info field is set in final_write_processing.  */
7707     }
7708   else if (strcmp (name, ".ucode") == 0)
7709     hdr->sh_type = SHT_MIPS_UCODE;
7710   else if (strcmp (name, ".mdebug") == 0)
7711     {
7712       hdr->sh_type = SHT_MIPS_DEBUG;
7713       /* In a shared object on IRIX 5.3, the .mdebug section has an
7714 	 entsize of 0.  FIXME: Does this matter?  */
7715       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7716 	hdr->sh_entsize = 0;
7717       else
7718 	hdr->sh_entsize = 1;
7719     }
7720   else if (strcmp (name, ".reginfo") == 0)
7721     {
7722       hdr->sh_type = SHT_MIPS_REGINFO;
7723       /* In a shared object on IRIX 5.3, the .reginfo section has an
7724 	 entsize of 0x18.  FIXME: Does this matter?  */
7725       if (SGI_COMPAT (abfd))
7726 	{
7727 	  if ((abfd->flags & DYNAMIC) != 0)
7728 	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7729 	  else
7730 	    hdr->sh_entsize = 1;
7731 	}
7732       else
7733 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7734     }
7735   else if (SGI_COMPAT (abfd)
7736 	   && (strcmp (name, ".hash") == 0
7737 	       || strcmp (name, ".dynamic") == 0
7738 	       || strcmp (name, ".dynstr") == 0))
7739     {
7740       if (SGI_COMPAT (abfd))
7741 	hdr->sh_entsize = 0;
7742 #if 0
7743       /* This isn't how the IRIX6 linker behaves.  */
7744       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7745 #endif
7746     }
7747   else if (strcmp (name, ".got") == 0
7748 	   || strcmp (name, ".srdata") == 0
7749 	   || strcmp (name, ".sdata") == 0
7750 	   || strcmp (name, ".sbss") == 0
7751 	   || strcmp (name, ".lit4") == 0
7752 	   || strcmp (name, ".lit8") == 0)
7753     hdr->sh_flags |= SHF_MIPS_GPREL;
7754   else if (strcmp (name, ".MIPS.interfaces") == 0)
7755     {
7756       hdr->sh_type = SHT_MIPS_IFACE;
7757       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7758     }
7759   else if (startswith (name, ".MIPS.content"))
7760     {
7761       hdr->sh_type = SHT_MIPS_CONTENT;
7762       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7763       /* The sh_info field is set in final_write_processing.  */
7764     }
7765   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7766     {
7767       hdr->sh_type = SHT_MIPS_OPTIONS;
7768       hdr->sh_entsize = 1;
7769       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7770     }
7771   else if (startswith (name, ".MIPS.abiflags"))
7772     {
7773       hdr->sh_type = SHT_MIPS_ABIFLAGS;
7774       hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7775     }
7776   else if (startswith (name, ".debug_")
7777 	   || startswith (name, ".gnu.debuglto_.debug_")
7778 	   || startswith (name, ".zdebug_")
7779 	   || startswith (name, ".gnu.debuglto_.zdebug_"))
7780     {
7781       hdr->sh_type = SHT_MIPS_DWARF;
7782 
7783       /* Irix facilities such as libexc expect a single .debug_frame
7784 	 per executable, the system ones have NOSTRIP set and the linker
7785 	 doesn't merge sections with different flags so ...  */
7786       if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7787 	hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7788     }
7789   else if (strcmp (name, ".MIPS.symlib") == 0)
7790     {
7791       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7792       /* The sh_link and sh_info fields are set in
7793 	 final_write_processing.  */
7794     }
7795   else if (startswith (name, ".MIPS.events")
7796 	   || startswith (name, ".MIPS.post_rel"))
7797     {
7798       hdr->sh_type = SHT_MIPS_EVENTS;
7799       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7800       /* The sh_link field is set in final_write_processing.  */
7801     }
7802   else if (strcmp (name, ".msym") == 0)
7803     {
7804       hdr->sh_type = SHT_MIPS_MSYM;
7805       hdr->sh_flags |= SHF_ALLOC;
7806       hdr->sh_entsize = 8;
7807     }
7808   else if (strcmp (name, ".MIPS.xhash") == 0)
7809     {
7810       hdr->sh_type = SHT_MIPS_XHASH;
7811       hdr->sh_flags |= SHF_ALLOC;
7812       hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7813     }
7814 
7815   /* The generic elf_fake_sections will set up REL_HDR using the default
7816    kind of relocations.  We used to set up a second header for the
7817    non-default kind of relocations here, but only NewABI would use
7818    these, and the IRIX ld doesn't like resulting empty RELA sections.
7819    Thus we create those header only on demand now.  */
7820 
7821   return true;
7822 }
7823 
7824 /* Given a BFD section, try to locate the corresponding ELF section
7825    index.  This is used by both the 32-bit and the 64-bit ABI.
7826    Actually, it's not clear to me that the 64-bit ABI supports these,
7827    but for non-PIC objects we will certainly want support for at least
7828    the .scommon section.  */
7829 
7830 bool
7831 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7832 					asection *sec, int *retval)
7833 {
7834   if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7835     {
7836       *retval = SHN_MIPS_SCOMMON;
7837       return true;
7838     }
7839   if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7840     {
7841       *retval = SHN_MIPS_ACOMMON;
7842       return true;
7843     }
7844   return false;
7845 }
7846 
7847 /* Hook called by the linker routine which adds symbols from an object
7848    file.  We must handle the special MIPS section numbers here.  */
7849 
7850 bool
7851 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7852 			       Elf_Internal_Sym *sym, const char **namep,
7853 			       flagword *flagsp ATTRIBUTE_UNUSED,
7854 			       asection **secp, bfd_vma *valp)
7855 {
7856   if (SGI_COMPAT (abfd)
7857       && (abfd->flags & DYNAMIC) != 0
7858       && strcmp (*namep, "_rld_new_interface") == 0)
7859     {
7860       /* Skip IRIX5 rld entry name.  */
7861       *namep = NULL;
7862       return true;
7863     }
7864 
7865   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7866      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
7867      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
7868      a magic symbol resolved by the linker, we ignore this bogus definition
7869      of _gp_disp.  New ABI objects do not suffer from this problem so this
7870      is not done for them. */
7871   if (!NEWABI_P(abfd)
7872       && (sym->st_shndx == SHN_ABS)
7873       && (strcmp (*namep, "_gp_disp") == 0))
7874     {
7875       *namep = NULL;
7876       return true;
7877     }
7878 
7879   switch (sym->st_shndx)
7880     {
7881     case SHN_COMMON:
7882       /* Common symbols less than the GP size are automatically
7883 	 treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7884       if (sym->st_size > elf_gp_size (abfd)
7885 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
7886 	  || IRIX_COMPAT (abfd) == ict_irix6
7887 	  || strcmp (*namep, "__gnu_lto_slim") == 0)
7888 	break;
7889       /* Fall through.  */
7890     case SHN_MIPS_SCOMMON:
7891       *secp = bfd_make_section_old_way (abfd, ".scommon");
7892       (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7893       *valp = sym->st_size;
7894       break;
7895 
7896     case SHN_MIPS_TEXT:
7897       /* This section is used in a shared object.  */
7898       if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7899 	{
7900 	  asymbol *elf_text_symbol;
7901 	  asection *elf_text_section;
7902 	  size_t amt = sizeof (asection);
7903 
7904 	  elf_text_section = bfd_zalloc (abfd, amt);
7905 	  if (elf_text_section == NULL)
7906 	    return false;
7907 
7908 	  amt = sizeof (asymbol);
7909 	  elf_text_symbol = bfd_zalloc (abfd, amt);
7910 	  if (elf_text_symbol == NULL)
7911 	    return false;
7912 
7913 	  /* Initialize the section.  */
7914 
7915 	  mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7916 	  mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7917 
7918 	  elf_text_section->symbol = elf_text_symbol;
7919 	  elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7920 
7921 	  elf_text_section->name = ".text";
7922 	  elf_text_section->flags = SEC_NO_FLAGS;
7923 	  elf_text_section->output_section = NULL;
7924 	  elf_text_section->owner = abfd;
7925 	  elf_text_symbol->name = ".text";
7926 	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7927 	  elf_text_symbol->section = elf_text_section;
7928 	}
7929       /* This code used to do *secp = bfd_und_section_ptr if
7930 	 bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7931 	 so I took it out.  */
7932       *secp = mips_elf_tdata (abfd)->elf_text_section;
7933       break;
7934 
7935     case SHN_MIPS_ACOMMON:
7936       /* Fall through. XXX Can we treat this as allocated data?  */
7937     case SHN_MIPS_DATA:
7938       /* This section is used in a shared object.  */
7939       if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7940 	{
7941 	  asymbol *elf_data_symbol;
7942 	  asection *elf_data_section;
7943 	  size_t amt = sizeof (asection);
7944 
7945 	  elf_data_section = bfd_zalloc (abfd, amt);
7946 	  if (elf_data_section == NULL)
7947 	    return false;
7948 
7949 	  amt = sizeof (asymbol);
7950 	  elf_data_symbol = bfd_zalloc (abfd, amt);
7951 	  if (elf_data_symbol == NULL)
7952 	    return false;
7953 
7954 	  /* Initialize the section.  */
7955 
7956 	  mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7957 	  mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7958 
7959 	  elf_data_section->symbol = elf_data_symbol;
7960 	  elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7961 
7962 	  elf_data_section->name = ".data";
7963 	  elf_data_section->flags = SEC_NO_FLAGS;
7964 	  elf_data_section->output_section = NULL;
7965 	  elf_data_section->owner = abfd;
7966 	  elf_data_symbol->name = ".data";
7967 	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7968 	  elf_data_symbol->section = elf_data_section;
7969 	}
7970       /* This code used to do *secp = bfd_und_section_ptr if
7971 	 bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7972 	 so I took it out.  */
7973       *secp = mips_elf_tdata (abfd)->elf_data_section;
7974       break;
7975 
7976     case SHN_MIPS_SUNDEFINED:
7977       *secp = bfd_und_section_ptr;
7978       break;
7979     }
7980 
7981   if (SGI_COMPAT (abfd)
7982       && ! bfd_link_pic (info)
7983       && info->output_bfd->xvec == abfd->xvec
7984       && strcmp (*namep, "__rld_obj_head") == 0)
7985     {
7986       struct elf_link_hash_entry *h;
7987       struct bfd_link_hash_entry *bh;
7988 
7989       /* Mark __rld_obj_head as dynamic.  */
7990       bh = NULL;
7991       if (! (_bfd_generic_link_add_one_symbol
7992 	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7993 	      get_elf_backend_data (abfd)->collect, &bh)))
7994 	return false;
7995 
7996       h = (struct elf_link_hash_entry *) bh;
7997       h->non_elf = 0;
7998       h->def_regular = 1;
7999       h->type = STT_OBJECT;
8000 
8001       if (! bfd_elf_link_record_dynamic_symbol (info, h))
8002 	return false;
8003 
8004       mips_elf_hash_table (info)->use_rld_obj_head = true;
8005       mips_elf_hash_table (info)->rld_symbol = h;
8006     }
8007 
8008   /* If this is a mips16 text symbol, add 1 to the value to make it
8009      odd.  This will cause something like .word SYM to come up with
8010      the right value when it is loaded into the PC.  */
8011   if (ELF_ST_IS_COMPRESSED (sym->st_other))
8012     ++*valp;
8013 
8014   return true;
8015 }
8016 
8017 /* This hook function is called before the linker writes out a global
8018    symbol.  We mark symbols as small common if appropriate.  This is
8019    also where we undo the increment of the value for a mips16 symbol.  */
8020 
8021 int
8022 _bfd_mips_elf_link_output_symbol_hook
8023   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8024    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8025    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8026 {
8027   /* If we see a common symbol, which implies a relocatable link, then
8028      if a symbol was small common in an input file, mark it as small
8029      common in the output file.  */
8030   if (sym->st_shndx == SHN_COMMON
8031       && strcmp (input_sec->name, ".scommon") == 0)
8032     sym->st_shndx = SHN_MIPS_SCOMMON;
8033 
8034   if (ELF_ST_IS_COMPRESSED (sym->st_other))
8035     sym->st_value &= ~1;
8036 
8037   return 1;
8038 }
8039 
8040 /* Functions for the dynamic linker.  */
8041 
8042 /* Create dynamic sections when linking against a dynamic object.  */
8043 
8044 bool
8045 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8046 {
8047   struct elf_link_hash_entry *h;
8048   struct bfd_link_hash_entry *bh;
8049   flagword flags;
8050   register asection *s;
8051   const char * const *namep;
8052   struct mips_elf_link_hash_table *htab;
8053 
8054   htab = mips_elf_hash_table (info);
8055   BFD_ASSERT (htab != NULL);
8056 
8057   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8058 	   | SEC_LINKER_CREATED | SEC_READONLY);
8059 
8060   /* The psABI requires a read-only .dynamic section, but the VxWorks
8061      EABI doesn't.  */
8062   if (htab->root.target_os != is_vxworks)
8063     {
8064       s = bfd_get_linker_section (abfd, ".dynamic");
8065       if (s != NULL)
8066 	{
8067 	  if (!bfd_set_section_flags (s, flags))
8068 	    return false;
8069 	}
8070     }
8071 
8072   /* We need to create .got section.  */
8073   if (!mips_elf_create_got_section (abfd, info))
8074     return false;
8075 
8076   if (! mips_elf_rel_dyn_section (info, true))
8077     return false;
8078 
8079   /* Create .stub section.  */
8080   s = bfd_make_section_anyway_with_flags (abfd,
8081 					  MIPS_ELF_STUB_SECTION_NAME (abfd),
8082 					  flags | SEC_CODE);
8083   if (s == NULL
8084       || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8085     return false;
8086   htab->sstubs = s;
8087 
8088   if (!mips_elf_hash_table (info)->use_rld_obj_head
8089       && bfd_link_executable (info)
8090       && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8091     {
8092       s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8093 					      flags &~ (flagword) SEC_READONLY);
8094       if (s == NULL
8095 	  || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8096 	return false;
8097     }
8098 
8099   /* Create .MIPS.xhash section.  */
8100   if (info->emit_gnu_hash)
8101     s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8102 					    flags | SEC_READONLY);
8103 
8104   /* On IRIX5, we adjust add some additional symbols and change the
8105      alignments of several sections.  There is no ABI documentation
8106      indicating that this is necessary on IRIX6, nor any evidence that
8107      the linker takes such action.  */
8108   if (IRIX_COMPAT (abfd) == ict_irix5)
8109     {
8110       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8111 	{
8112 	  bh = NULL;
8113 	  if (! (_bfd_generic_link_add_one_symbol
8114 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8115 		  NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8116 	    return false;
8117 
8118 	  h = (struct elf_link_hash_entry *) bh;
8119 	  h->mark = 1;
8120 	  h->non_elf = 0;
8121 	  h->def_regular = 1;
8122 	  h->type = STT_SECTION;
8123 
8124 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
8125 	    return false;
8126 	}
8127 
8128       /* We need to create a .compact_rel section.  */
8129       if (SGI_COMPAT (abfd))
8130 	{
8131 	  if (!mips_elf_create_compact_rel_section (abfd, info))
8132 	    return false;
8133 	}
8134 
8135       /* Change alignments of some sections.  */
8136       s = bfd_get_linker_section (abfd, ".hash");
8137       if (s != NULL)
8138 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8139 
8140       s = bfd_get_linker_section (abfd, ".dynsym");
8141       if (s != NULL)
8142 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8143 
8144       s = bfd_get_linker_section (abfd, ".dynstr");
8145       if (s != NULL)
8146 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8147 
8148       /* ??? */
8149       s = bfd_get_section_by_name (abfd, ".reginfo");
8150       if (s != NULL)
8151 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8152 
8153       s = bfd_get_linker_section (abfd, ".dynamic");
8154       if (s != NULL)
8155 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8156     }
8157 
8158   if (bfd_link_executable (info))
8159     {
8160       const char *name;
8161 
8162       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8163       bh = NULL;
8164       if (!(_bfd_generic_link_add_one_symbol
8165 	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8166 	     NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8167 	return false;
8168 
8169       h = (struct elf_link_hash_entry *) bh;
8170       h->non_elf = 0;
8171       h->def_regular = 1;
8172       h->type = STT_SECTION;
8173 
8174       if (! bfd_elf_link_record_dynamic_symbol (info, h))
8175 	return false;
8176 
8177       if (! mips_elf_hash_table (info)->use_rld_obj_head)
8178 	{
8179 	  /* __rld_map is a four byte word located in the .data section
8180 	     and is filled in by the rtld to contain a pointer to
8181 	     the _r_debug structure. Its symbol value will be set in
8182 	     _bfd_mips_elf_finish_dynamic_symbol.  */
8183 	  s = bfd_get_linker_section (abfd, ".rld_map");
8184 	  BFD_ASSERT (s != NULL);
8185 
8186 	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8187 	  bh = NULL;
8188 	  if (!(_bfd_generic_link_add_one_symbol
8189 		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8190 		 get_elf_backend_data (abfd)->collect, &bh)))
8191 	    return false;
8192 
8193 	  h = (struct elf_link_hash_entry *) bh;
8194 	  h->non_elf = 0;
8195 	  h->def_regular = 1;
8196 	  h->type = STT_OBJECT;
8197 
8198 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
8199 	    return false;
8200 	  mips_elf_hash_table (info)->rld_symbol = h;
8201 	}
8202     }
8203 
8204   /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8205      Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
8206   if (!_bfd_elf_create_dynamic_sections (abfd, info))
8207     return false;
8208 
8209   /* Do the usual VxWorks handling.  */
8210   if (htab->root.target_os == is_vxworks
8211       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8212     return false;
8213 
8214   return true;
8215 }
8216 
8217 /* Return true if relocation REL against section SEC is a REL rather than
8218    RELA relocation.  RELOCS is the first relocation in the section and
8219    ABFD is the bfd that contains SEC.  */
8220 
8221 static bool
8222 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8223 			   const Elf_Internal_Rela *relocs,
8224 			   const Elf_Internal_Rela *rel)
8225 {
8226   Elf_Internal_Shdr *rel_hdr;
8227   const struct elf_backend_data *bed;
8228 
8229   /* To determine which flavor of relocation this is, we depend on the
8230      fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
8231   rel_hdr = elf_section_data (sec)->rel.hdr;
8232   if (rel_hdr == NULL)
8233     return false;
8234   bed = get_elf_backend_data (abfd);
8235   return ((size_t) (rel - relocs)
8236 	  < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8237 }
8238 
8239 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8240    HOWTO is the relocation's howto and CONTENTS points to the contents
8241    of the section that REL is against.  */
8242 
8243 static bfd_vma
8244 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8245 			  const Elf_Internal_Rela *rel,
8246 			  reloc_howto_type *howto, bfd_byte *contents)
8247 {
8248   bfd_byte *location;
8249   unsigned int r_type;
8250   bfd_vma addend;
8251   bfd_vma bytes;
8252 
8253   if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8254     return 0;
8255 
8256   r_type = ELF_R_TYPE (abfd, rel->r_info);
8257   location = contents + rel->r_offset;
8258 
8259   /* Get the addend, which is stored in the input file.  */
8260   _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8261   bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8262   _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8263 
8264   addend = bytes & howto->src_mask;
8265 
8266   /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
8267      accordingly.  */
8268   if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8269     addend <<= 1;
8270 
8271   return addend;
8272 }
8273 
8274 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8275    and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
8276    and update *ADDEND with the final addend.  Return true on success
8277    or false if the LO16 could not be found.  RELEND is the exclusive
8278    upper bound on the relocations for REL's section.  */
8279 
8280 static bool
8281 mips_elf_add_lo16_rel_addend (bfd *abfd,
8282 			      asection *sec,
8283 			      const Elf_Internal_Rela *rel,
8284 			      const Elf_Internal_Rela *relend,
8285 			      bfd_byte *contents, bfd_vma *addend)
8286 {
8287   unsigned int r_type, lo16_type;
8288   const Elf_Internal_Rela *lo16_relocation;
8289   reloc_howto_type *lo16_howto;
8290   bfd_vma l;
8291 
8292   r_type = ELF_R_TYPE (abfd, rel->r_info);
8293   if (mips16_reloc_p (r_type))
8294     lo16_type = R_MIPS16_LO16;
8295   else if (micromips_reloc_p (r_type))
8296     lo16_type = R_MICROMIPS_LO16;
8297   else if (r_type == R_MIPS_PCHI16)
8298     lo16_type = R_MIPS_PCLO16;
8299   else
8300     lo16_type = R_MIPS_LO16;
8301 
8302   /* The combined value is the sum of the HI16 addend, left-shifted by
8303      sixteen bits, and the LO16 addend, sign extended.  (Usually, the
8304      code does a `lui' of the HI16 value, and then an `addiu' of the
8305      LO16 value.)
8306 
8307      Scan ahead to find a matching LO16 relocation.
8308 
8309      According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8310      be immediately following.  However, for the IRIX6 ABI, the next
8311      relocation may be a composed relocation consisting of several
8312      relocations for the same address.  In that case, the R_MIPS_LO16
8313      relocation may occur as one of these.  We permit a similar
8314      extension in general, as that is useful for GCC.
8315 
8316      In some cases GCC dead code elimination removes the LO16 but keeps
8317      the corresponding HI16.  This is strictly speaking a violation of
8318      the ABI but not immediately harmful.  */
8319   lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8320   if (lo16_relocation == NULL)
8321     return false;
8322 
8323   /* Obtain the addend kept there.  */
8324   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8325   l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8326 				contents);
8327 
8328   l <<= lo16_howto->rightshift;
8329   l = _bfd_mips_elf_sign_extend (l, 16);
8330 
8331   *addend <<= 16;
8332   *addend += l;
8333   return true;
8334 }
8335 
8336 /* Try to read the contents of section SEC in bfd ABFD.  Return true and
8337    store the contents in *CONTENTS on success.  Assume that *CONTENTS
8338    already holds the contents if it is nonull on entry.  */
8339 
8340 static bool
8341 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8342 {
8343   if (*contents)
8344     return true;
8345 
8346   /* Get cached copy if it exists.  */
8347   if (elf_section_data (sec)->this_hdr.contents != NULL)
8348     {
8349       *contents = elf_section_data (sec)->this_hdr.contents;
8350       return true;
8351     }
8352 
8353   return bfd_malloc_and_get_section (abfd, sec, contents);
8354 }
8355 
8356 /* Make a new PLT record to keep internal data.  */
8357 
8358 static struct plt_entry *
8359 mips_elf_make_plt_record (bfd *abfd)
8360 {
8361   struct plt_entry *entry;
8362 
8363   entry = bfd_zalloc (abfd, sizeof (*entry));
8364   if (entry == NULL)
8365     return NULL;
8366 
8367   entry->stub_offset = MINUS_ONE;
8368   entry->mips_offset = MINUS_ONE;
8369   entry->comp_offset = MINUS_ONE;
8370   entry->gotplt_index = MINUS_ONE;
8371   return entry;
8372 }
8373 
8374 /* Define the special `__gnu_absolute_zero' symbol.  We only need this
8375    for PIC code, as otherwise there is no load-time relocation involved
8376    and local GOT entries whose value is zero at static link time will
8377    retain their value at load time.  */
8378 
8379 static bool
8380 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8381 			       struct mips_elf_link_hash_table *htab,
8382 			       unsigned int r_type)
8383 {
8384   union
8385     {
8386       struct elf_link_hash_entry *eh;
8387       struct bfd_link_hash_entry *bh;
8388     }
8389   hzero;
8390 
8391   BFD_ASSERT (!htab->use_absolute_zero);
8392   BFD_ASSERT (bfd_link_pic (info));
8393 
8394   hzero.bh = NULL;
8395   if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8396 					 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8397 					 NULL, false, false, &hzero.bh))
8398     return false;
8399 
8400   BFD_ASSERT (hzero.bh != NULL);
8401   hzero.eh->size = 0;
8402   hzero.eh->type = STT_NOTYPE;
8403   hzero.eh->other = STV_PROTECTED;
8404   hzero.eh->def_regular = 1;
8405   hzero.eh->non_elf = 0;
8406 
8407   if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8408     return false;
8409 
8410   htab->use_absolute_zero = true;
8411 
8412   return true;
8413 }
8414 
8415 /* Look through the relocs for a section during the first phase, and
8416    allocate space in the global offset table and record the need for
8417    standard MIPS and compressed procedure linkage table entries.  */
8418 
8419 bool
8420 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8421 			    asection *sec, const Elf_Internal_Rela *relocs)
8422 {
8423   const char *name;
8424   bfd *dynobj;
8425   Elf_Internal_Shdr *symtab_hdr;
8426   struct elf_link_hash_entry **sym_hashes;
8427   size_t extsymoff;
8428   const Elf_Internal_Rela *rel;
8429   const Elf_Internal_Rela *rel_end;
8430   asection *sreloc;
8431   const struct elf_backend_data *bed;
8432   struct mips_elf_link_hash_table *htab;
8433   bfd_byte *contents;
8434   bfd_vma addend;
8435   reloc_howto_type *howto;
8436 
8437   if (bfd_link_relocatable (info))
8438     return true;
8439 
8440   htab = mips_elf_hash_table (info);
8441   BFD_ASSERT (htab != NULL);
8442 
8443   dynobj = elf_hash_table (info)->dynobj;
8444   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8445   sym_hashes = elf_sym_hashes (abfd);
8446   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8447 
8448   bed = get_elf_backend_data (abfd);
8449   rel_end = relocs + sec->reloc_count;
8450 
8451   /* Check for the mips16 stub sections.  */
8452 
8453   name = bfd_section_name (sec);
8454   if (FN_STUB_P (name))
8455     {
8456       unsigned long r_symndx;
8457 
8458       /* Look at the relocation information to figure out which symbol
8459 	 this is for.  */
8460 
8461       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8462       if (r_symndx == 0)
8463 	{
8464 	  _bfd_error_handler
8465 	    /* xgettext:c-format */
8466 	    (_("%pB: warning: cannot determine the target function for"
8467 	       " stub section `%s'"),
8468 	     abfd, name);
8469 	  bfd_set_error (bfd_error_bad_value);
8470 	  return false;
8471 	}
8472 
8473       if (r_symndx < extsymoff
8474 	  || sym_hashes[r_symndx - extsymoff] == NULL)
8475 	{
8476 	  asection *o;
8477 
8478 	  /* This stub is for a local symbol.  This stub will only be
8479 	     needed if there is some relocation in this BFD, other
8480 	     than a 16 bit function call, which refers to this symbol.  */
8481 	  for (o = abfd->sections; o != NULL; o = o->next)
8482 	    {
8483 	      Elf_Internal_Rela *sec_relocs;
8484 	      const Elf_Internal_Rela *r, *rend;
8485 
8486 	      /* We can ignore stub sections when looking for relocs.  */
8487 	      if ((o->flags & SEC_RELOC) == 0
8488 		  || o->reloc_count == 0
8489 		  || section_allows_mips16_refs_p (o))
8490 		continue;
8491 
8492 	      sec_relocs
8493 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8494 					     info->keep_memory);
8495 	      if (sec_relocs == NULL)
8496 		return false;
8497 
8498 	      rend = sec_relocs + o->reloc_count;
8499 	      for (r = sec_relocs; r < rend; r++)
8500 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8501 		    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8502 		  break;
8503 
8504 	      if (elf_section_data (o)->relocs != sec_relocs)
8505 		free (sec_relocs);
8506 
8507 	      if (r < rend)
8508 		break;
8509 	    }
8510 
8511 	  if (o == NULL)
8512 	    {
8513 	      /* There is no non-call reloc for this stub, so we do
8514 		 not need it.  Since this function is called before
8515 		 the linker maps input sections to output sections, we
8516 		 can easily discard it by setting the SEC_EXCLUDE
8517 		 flag.  */
8518 	      sec->flags |= SEC_EXCLUDE;
8519 	      return true;
8520 	    }
8521 
8522 	  /* Record this stub in an array of local symbol stubs for
8523 	     this BFD.  */
8524 	  if (mips_elf_tdata (abfd)->local_stubs == NULL)
8525 	    {
8526 	      unsigned long symcount;
8527 	      asection **n;
8528 	      bfd_size_type amt;
8529 
8530 	      if (elf_bad_symtab (abfd))
8531 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8532 	      else
8533 		symcount = symtab_hdr->sh_info;
8534 	      amt = symcount * sizeof (asection *);
8535 	      n = bfd_zalloc (abfd, amt);
8536 	      if (n == NULL)
8537 		return false;
8538 	      mips_elf_tdata (abfd)->local_stubs = n;
8539 	    }
8540 
8541 	  sec->flags |= SEC_KEEP;
8542 	  mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8543 
8544 	  /* We don't need to set mips16_stubs_seen in this case.
8545 	     That flag is used to see whether we need to look through
8546 	     the global symbol table for stubs.  We don't need to set
8547 	     it here, because we just have a local stub.  */
8548 	}
8549       else
8550 	{
8551 	  struct mips_elf_link_hash_entry *h;
8552 
8553 	  h = ((struct mips_elf_link_hash_entry *)
8554 	       sym_hashes[r_symndx - extsymoff]);
8555 
8556 	  while (h->root.root.type == bfd_link_hash_indirect
8557 		 || h->root.root.type == bfd_link_hash_warning)
8558 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8559 
8560 	  /* H is the symbol this stub is for.  */
8561 
8562 	  /* If we already have an appropriate stub for this function, we
8563 	     don't need another one, so we can discard this one.  Since
8564 	     this function is called before the linker maps input sections
8565 	     to output sections, we can easily discard it by setting the
8566 	     SEC_EXCLUDE flag.  */
8567 	  if (h->fn_stub != NULL)
8568 	    {
8569 	      sec->flags |= SEC_EXCLUDE;
8570 	      return true;
8571 	    }
8572 
8573 	  sec->flags |= SEC_KEEP;
8574 	  h->fn_stub = sec;
8575 	  mips_elf_hash_table (info)->mips16_stubs_seen = true;
8576 	}
8577     }
8578   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8579     {
8580       unsigned long r_symndx;
8581       struct mips_elf_link_hash_entry *h;
8582       asection **loc;
8583 
8584       /* Look at the relocation information to figure out which symbol
8585 	 this is for.  */
8586 
8587       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8588       if (r_symndx == 0)
8589 	{
8590 	  _bfd_error_handler
8591 	    /* xgettext:c-format */
8592 	    (_("%pB: warning: cannot determine the target function for"
8593 	       " stub section `%s'"),
8594 	     abfd, name);
8595 	  bfd_set_error (bfd_error_bad_value);
8596 	  return false;
8597 	}
8598 
8599       if (r_symndx < extsymoff
8600 	  || sym_hashes[r_symndx - extsymoff] == NULL)
8601 	{
8602 	  asection *o;
8603 
8604 	  /* This stub is for a local symbol.  This stub will only be
8605 	     needed if there is some relocation (R_MIPS16_26) in this BFD
8606 	     that refers to this symbol.  */
8607 	  for (o = abfd->sections; o != NULL; o = o->next)
8608 	    {
8609 	      Elf_Internal_Rela *sec_relocs;
8610 	      const Elf_Internal_Rela *r, *rend;
8611 
8612 	      /* We can ignore stub sections when looking for relocs.  */
8613 	      if ((o->flags & SEC_RELOC) == 0
8614 		  || o->reloc_count == 0
8615 		  || section_allows_mips16_refs_p (o))
8616 		continue;
8617 
8618 	      sec_relocs
8619 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8620 					     info->keep_memory);
8621 	      if (sec_relocs == NULL)
8622 		return false;
8623 
8624 	      rend = sec_relocs + o->reloc_count;
8625 	      for (r = sec_relocs; r < rend; r++)
8626 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8627 		    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8628 		    break;
8629 
8630 	      if (elf_section_data (o)->relocs != sec_relocs)
8631 		free (sec_relocs);
8632 
8633 	      if (r < rend)
8634 		break;
8635 	    }
8636 
8637 	  if (o == NULL)
8638 	    {
8639 	      /* There is no non-call reloc for this stub, so we do
8640 		 not need it.  Since this function is called before
8641 		 the linker maps input sections to output sections, we
8642 		 can easily discard it by setting the SEC_EXCLUDE
8643 		 flag.  */
8644 	      sec->flags |= SEC_EXCLUDE;
8645 	      return true;
8646 	    }
8647 
8648 	  /* Record this stub in an array of local symbol call_stubs for
8649 	     this BFD.  */
8650 	  if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8651 	    {
8652 	      unsigned long symcount;
8653 	      asection **n;
8654 	      bfd_size_type amt;
8655 
8656 	      if (elf_bad_symtab (abfd))
8657 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8658 	      else
8659 		symcount = symtab_hdr->sh_info;
8660 	      amt = symcount * sizeof (asection *);
8661 	      n = bfd_zalloc (abfd, amt);
8662 	      if (n == NULL)
8663 		return false;
8664 	      mips_elf_tdata (abfd)->local_call_stubs = n;
8665 	    }
8666 
8667 	  sec->flags |= SEC_KEEP;
8668 	  mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8669 
8670 	  /* We don't need to set mips16_stubs_seen in this case.
8671 	     That flag is used to see whether we need to look through
8672 	     the global symbol table for stubs.  We don't need to set
8673 	     it here, because we just have a local stub.  */
8674 	}
8675       else
8676 	{
8677 	  h = ((struct mips_elf_link_hash_entry *)
8678 	       sym_hashes[r_symndx - extsymoff]);
8679 
8680 	  /* H is the symbol this stub is for.  */
8681 
8682 	  if (CALL_FP_STUB_P (name))
8683 	    loc = &h->call_fp_stub;
8684 	  else
8685 	    loc = &h->call_stub;
8686 
8687 	  /* If we already have an appropriate stub for this function, we
8688 	     don't need another one, so we can discard this one.  Since
8689 	     this function is called before the linker maps input sections
8690 	     to output sections, we can easily discard it by setting the
8691 	     SEC_EXCLUDE flag.  */
8692 	  if (*loc != NULL)
8693 	    {
8694 	      sec->flags |= SEC_EXCLUDE;
8695 	      return true;
8696 	    }
8697 
8698 	  sec->flags |= SEC_KEEP;
8699 	  *loc = sec;
8700 	  mips_elf_hash_table (info)->mips16_stubs_seen = true;
8701 	}
8702     }
8703 
8704   sreloc = NULL;
8705   contents = NULL;
8706   for (rel = relocs; rel < rel_end; ++rel)
8707     {
8708       unsigned long r_symndx;
8709       unsigned int r_type;
8710       struct elf_link_hash_entry *h;
8711       bool can_make_dynamic_p;
8712       bool call_reloc_p;
8713       bool constrain_symbol_p;
8714 
8715       r_symndx = ELF_R_SYM (abfd, rel->r_info);
8716       r_type = ELF_R_TYPE (abfd, rel->r_info);
8717 
8718       if (r_symndx < extsymoff)
8719 	h = NULL;
8720       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8721 	{
8722 	  _bfd_error_handler
8723 	    /* xgettext:c-format */
8724 	    (_("%pB: malformed reloc detected for section %s"),
8725 	     abfd, name);
8726 	  bfd_set_error (bfd_error_bad_value);
8727 	  return false;
8728 	}
8729       else
8730 	{
8731 	  h = sym_hashes[r_symndx - extsymoff];
8732 	  if (h != NULL)
8733 	    {
8734 	      while (h->root.type == bfd_link_hash_indirect
8735 		     || h->root.type == bfd_link_hash_warning)
8736 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
8737 	    }
8738 	}
8739 
8740       /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8741 	 relocation into a dynamic one.  */
8742       can_make_dynamic_p = false;
8743 
8744       /* Set CALL_RELOC_P to true if the relocation is for a call,
8745 	 and if pointer equality therefore doesn't matter.  */
8746       call_reloc_p = false;
8747 
8748       /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8749 	 into account when deciding how to define the symbol.  */
8750       constrain_symbol_p = true;
8751 
8752       switch (r_type)
8753 	{
8754 	case R_MIPS_CALL16:
8755 	case R_MIPS_CALL_HI16:
8756 	case R_MIPS_CALL_LO16:
8757 	case R_MIPS16_CALL16:
8758 	case R_MICROMIPS_CALL16:
8759 	case R_MICROMIPS_CALL_HI16:
8760 	case R_MICROMIPS_CALL_LO16:
8761 	  call_reloc_p = true;
8762 	  /* Fall through.  */
8763 
8764 	case R_MIPS_GOT16:
8765 	case R_MIPS_GOT_LO16:
8766 	case R_MIPS_GOT_PAGE:
8767 	case R_MIPS_GOT_DISP:
8768 	case R_MIPS16_GOT16:
8769 	case R_MICROMIPS_GOT16:
8770 	case R_MICROMIPS_GOT_LO16:
8771 	case R_MICROMIPS_GOT_PAGE:
8772 	case R_MICROMIPS_GOT_DISP:
8773 	  /* If we have a symbol that will resolve to zero at static link
8774 	     time and it is used by a GOT relocation applied to code we
8775 	     cannot relax to an immediate zero load, then we will be using
8776 	     the special `__gnu_absolute_zero' symbol whose value is zero
8777 	     at dynamic load time.  We ignore HI16-type GOT relocations at
8778 	     this stage, because their handling will depend entirely on
8779 	     the corresponding LO16-type GOT relocation.  */
8780 	  if (!call_hi16_reloc_p (r_type)
8781 	      && h != NULL
8782 	      && bfd_link_pic (info)
8783 	      && !htab->use_absolute_zero
8784 	      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8785 	    {
8786 	      bool rel_reloc;
8787 
8788 	      if (!mips_elf_get_section_contents (abfd, sec, &contents))
8789 		return false;
8790 
8791 	      rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8792 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8793 	      if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8794 		if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8795 						false))
8796 		  if (!mips_elf_define_absolute_zero (abfd, info, htab,
8797 						      r_type))
8798 		    return false;
8799 	    }
8800 
8801 	  /* Fall through.  */
8802 	case R_MIPS_GOT_HI16:
8803 	case R_MIPS_GOT_OFST:
8804 	case R_MIPS_TLS_GOTTPREL:
8805 	case R_MIPS_TLS_GD:
8806 	case R_MIPS_TLS_LDM:
8807 	case R_MIPS16_TLS_GOTTPREL:
8808 	case R_MIPS16_TLS_GD:
8809 	case R_MIPS16_TLS_LDM:
8810 	case R_MICROMIPS_GOT_HI16:
8811 	case R_MICROMIPS_GOT_OFST:
8812 	case R_MICROMIPS_TLS_GOTTPREL:
8813 	case R_MICROMIPS_TLS_GD:
8814 	case R_MICROMIPS_TLS_LDM:
8815 	  if (dynobj == NULL)
8816 	    elf_hash_table (info)->dynobj = dynobj = abfd;
8817 	  if (!mips_elf_create_got_section (dynobj, info))
8818 	    return false;
8819 	  if (htab->root.target_os == is_vxworks
8820 	      && !bfd_link_pic (info))
8821 	    {
8822 	      _bfd_error_handler
8823 		/* xgettext:c-format */
8824 		(_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8825 		 abfd, (uint64_t) rel->r_offset);
8826 	      bfd_set_error (bfd_error_bad_value);
8827 	      return false;
8828 	    }
8829 	  can_make_dynamic_p = true;
8830 	  break;
8831 
8832 	case R_MIPS_NONE:
8833 	case R_MIPS_JALR:
8834 	case R_MICROMIPS_JALR:
8835 	  /* These relocations have empty fields and are purely there to
8836 	     provide link information.  The symbol value doesn't matter.  */
8837 	  constrain_symbol_p = false;
8838 	  break;
8839 
8840 	case R_MIPS_GPREL16:
8841 	case R_MIPS_GPREL32:
8842 	case R_MIPS16_GPREL:
8843 	case R_MICROMIPS_GPREL16:
8844 	  /* GP-relative relocations always resolve to a definition in a
8845 	     regular input file, ignoring the one-definition rule.  This is
8846 	     important for the GP setup sequence in NewABI code, which
8847 	     always resolves to a local function even if other relocations
8848 	     against the symbol wouldn't.  */
8849 	  constrain_symbol_p = false;
8850 	  break;
8851 
8852 	case R_MIPS_32:
8853 	case R_MIPS_REL32:
8854 	case R_MIPS_64:
8855 	  /* In VxWorks executables, references to external symbols
8856 	     must be handled using copy relocs or PLT entries; it is not
8857 	     possible to convert this relocation into a dynamic one.
8858 
8859 	     For executables that use PLTs and copy-relocs, we have a
8860 	     choice between converting the relocation into a dynamic
8861 	     one or using copy relocations or PLT entries.  It is
8862 	     usually better to do the former, unless the relocation is
8863 	     against a read-only section.  */
8864 	  if ((bfd_link_pic (info)
8865 	       || (h != NULL
8866 		   && htab->root.target_os != is_vxworks
8867 		   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8868 		   && !(!info->nocopyreloc
8869 			&& !PIC_OBJECT_P (abfd)
8870 			&& MIPS_ELF_READONLY_SECTION (sec))))
8871 	      && (sec->flags & SEC_ALLOC) != 0)
8872 	    {
8873 	      can_make_dynamic_p = true;
8874 	      if (dynobj == NULL)
8875 		elf_hash_table (info)->dynobj = dynobj = abfd;
8876 	    }
8877 	  break;
8878 
8879 	case R_MIPS_26:
8880 	case R_MIPS_PC16:
8881 	case R_MIPS_PC21_S2:
8882 	case R_MIPS_PC26_S2:
8883 	case R_MIPS16_26:
8884 	case R_MIPS16_PC16_S1:
8885 	case R_MICROMIPS_26_S1:
8886 	case R_MICROMIPS_PC7_S1:
8887 	case R_MICROMIPS_PC10_S1:
8888 	case R_MICROMIPS_PC16_S1:
8889 	case R_MICROMIPS_PC23_S2:
8890 	  call_reloc_p = true;
8891 	  break;
8892 	}
8893 
8894       if (h)
8895 	{
8896 	  if (constrain_symbol_p)
8897 	    {
8898 	      if (!can_make_dynamic_p)
8899 		((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8900 
8901 	      if (!call_reloc_p)
8902 		h->pointer_equality_needed = 1;
8903 
8904 	      /* We must not create a stub for a symbol that has
8905 		 relocations related to taking the function's address.
8906 		 This doesn't apply to VxWorks, where CALL relocs refer
8907 		 to a .got.plt entry instead of a normal .got entry.  */
8908 	      if (htab->root.target_os != is_vxworks
8909 		  && (!can_make_dynamic_p || !call_reloc_p))
8910 		((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8911 	    }
8912 
8913 	  /* Relocations against the special VxWorks __GOTT_BASE__ and
8914 	     __GOTT_INDEX__ symbols must be left to the loader.  Allocate
8915 	     room for them in .rela.dyn.  */
8916 	  if (is_gott_symbol (info, h))
8917 	    {
8918 	      if (sreloc == NULL)
8919 		{
8920 		  sreloc = mips_elf_rel_dyn_section (info, true);
8921 		  if (sreloc == NULL)
8922 		    return false;
8923 		}
8924 	      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8925 	      if (MIPS_ELF_READONLY_SECTION (sec))
8926 		/* We tell the dynamic linker that there are
8927 		   relocations against the text segment.  */
8928 		info->flags |= DF_TEXTREL;
8929 	    }
8930 	}
8931       else if (call_lo16_reloc_p (r_type)
8932 	       || got_lo16_reloc_p (r_type)
8933 	       || got_disp_reloc_p (r_type)
8934 	       || (got16_reloc_p (r_type)
8935 		   && htab->root.target_os == is_vxworks))
8936 	{
8937 	  /* We may need a local GOT entry for this relocation.  We
8938 	     don't count R_MIPS_GOT_PAGE because we can estimate the
8939 	     maximum number of pages needed by looking at the size of
8940 	     the segment.  Similar comments apply to R_MIPS*_GOT16 and
8941 	     R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8942 	     always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
8943 	     R_MIPS_CALL_HI16 because these are always followed by an
8944 	     R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
8945 	  if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8946 						 rel->r_addend, info, r_type))
8947 	    return false;
8948 	}
8949 
8950       if (h != NULL
8951 	  && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8952 						  ELF_ST_IS_MIPS16 (h->other)))
8953 	((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8954 
8955       switch (r_type)
8956 	{
8957 	case R_MIPS_CALL16:
8958 	case R_MIPS16_CALL16:
8959 	case R_MICROMIPS_CALL16:
8960 	  if (h == NULL)
8961 	    {
8962 	      _bfd_error_handler
8963 		/* xgettext:c-format */
8964 		(_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8965 		 abfd, (uint64_t) rel->r_offset);
8966 	      bfd_set_error (bfd_error_bad_value);
8967 	      return false;
8968 	    }
8969 	  /* Fall through.  */
8970 
8971 	case R_MIPS_CALL_HI16:
8972 	case R_MIPS_CALL_LO16:
8973 	case R_MICROMIPS_CALL_HI16:
8974 	case R_MICROMIPS_CALL_LO16:
8975 	  if (h != NULL)
8976 	    {
8977 	      /* Make sure there is room in the regular GOT to hold the
8978 		 function's address.  We may eliminate it in favour of
8979 		 a .got.plt entry later; see mips_elf_count_got_symbols.  */
8980 	      if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8981 						      r_type))
8982 		return false;
8983 
8984 	      /* We need a stub, not a plt entry for the undefined
8985 		 function.  But we record it as if it needs plt.  See
8986 		 _bfd_elf_adjust_dynamic_symbol.  */
8987 	      h->needs_plt = 1;
8988 	      h->type = STT_FUNC;
8989 	    }
8990 	  break;
8991 
8992 	case R_MIPS_GOT_PAGE:
8993 	case R_MICROMIPS_GOT_PAGE:
8994 	case R_MIPS16_GOT16:
8995 	case R_MIPS_GOT16:
8996 	case R_MIPS_GOT_HI16:
8997 	case R_MIPS_GOT_LO16:
8998 	case R_MICROMIPS_GOT16:
8999 	case R_MICROMIPS_GOT_HI16:
9000 	case R_MICROMIPS_GOT_LO16:
9001 	  if (!h || got_page_reloc_p (r_type))
9002 	    {
9003 	      /* This relocation needs (or may need, if h != NULL) a
9004 		 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
9005 		 know for sure until we know whether the symbol is
9006 		 preemptible.  */
9007 	      if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
9008 		{
9009 		  if (!mips_elf_get_section_contents (abfd, sec, &contents))
9010 		    return false;
9011 		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
9012 		  addend = mips_elf_read_rel_addend (abfd, sec, rel,
9013 						     howto, contents);
9014 		  if (got16_reloc_p (r_type))
9015 		    mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
9016 						  contents, &addend);
9017 		  else
9018 		    addend <<= howto->rightshift;
9019 		}
9020 	      else
9021 		addend = rel->r_addend;
9022 	      if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9023 						 h, addend))
9024 		return false;
9025 
9026 	      if (h)
9027 		{
9028 		  struct mips_elf_link_hash_entry *hmips =
9029 		    (struct mips_elf_link_hash_entry *) h;
9030 
9031 		  /* This symbol is definitely not overridable.  */
9032 		  if (hmips->root.def_regular
9033 		      && ! (bfd_link_pic (info) && ! info->symbolic
9034 			    && ! hmips->root.forced_local))
9035 		    h = NULL;
9036 		}
9037 	    }
9038 	  /* If this is a global, overridable symbol, GOT_PAGE will
9039 	     decay to GOT_DISP, so we'll need a GOT entry for it.  */
9040 	  /* Fall through.  */
9041 
9042 	case R_MIPS_GOT_DISP:
9043 	case R_MICROMIPS_GOT_DISP:
9044 	  if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9045 						       false, r_type))
9046 	    return false;
9047 	  break;
9048 
9049 	case R_MIPS_TLS_GOTTPREL:
9050 	case R_MIPS16_TLS_GOTTPREL:
9051 	case R_MICROMIPS_TLS_GOTTPREL:
9052 	  if (bfd_link_pic (info))
9053 	    info->flags |= DF_STATIC_TLS;
9054 	  /* Fall through */
9055 
9056 	case R_MIPS_TLS_LDM:
9057 	case R_MIPS16_TLS_LDM:
9058 	case R_MICROMIPS_TLS_LDM:
9059 	  if (tls_ldm_reloc_p (r_type))
9060 	    {
9061 	      r_symndx = STN_UNDEF;
9062 	      h = NULL;
9063 	    }
9064 	  /* Fall through */
9065 
9066 	case R_MIPS_TLS_GD:
9067 	case R_MIPS16_TLS_GD:
9068 	case R_MICROMIPS_TLS_GD:
9069 	  /* This symbol requires a global offset table entry, or two
9070 	     for TLS GD relocations.  */
9071 	  if (h != NULL)
9072 	    {
9073 	      if (!mips_elf_record_global_got_symbol (h, abfd, info,
9074 						      false, r_type))
9075 		return false;
9076 	    }
9077 	  else
9078 	    {
9079 	      if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9080 						     rel->r_addend,
9081 						     info, r_type))
9082 		return false;
9083 	    }
9084 	  break;
9085 
9086 	case R_MIPS_32:
9087 	case R_MIPS_REL32:
9088 	case R_MIPS_64:
9089 	  /* In VxWorks executables, references to external symbols
9090 	     are handled using copy relocs or PLT stubs, so there's
9091 	     no need to add a .rela.dyn entry for this relocation.  */
9092 	  if (can_make_dynamic_p)
9093 	    {
9094 	      if (sreloc == NULL)
9095 		{
9096 		  sreloc = mips_elf_rel_dyn_section (info, true);
9097 		  if (sreloc == NULL)
9098 		    return false;
9099 		}
9100 	      if (bfd_link_pic (info) && h == NULL)
9101 		{
9102 		  /* When creating a shared object, we must copy these
9103 		     reloc types into the output file as R_MIPS_REL32
9104 		     relocs.  Make room for this reloc in .rel(a).dyn.  */
9105 		  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9106 		  if (MIPS_ELF_READONLY_SECTION (sec))
9107 		    /* We tell the dynamic linker that there are
9108 		       relocations against the text segment.  */
9109 		    info->flags |= DF_TEXTREL;
9110 		}
9111 	      else
9112 		{
9113 		  struct mips_elf_link_hash_entry *hmips;
9114 
9115 		  /* For a shared object, we must copy this relocation
9116 		     unless the symbol turns out to be undefined and
9117 		     weak with non-default visibility, in which case
9118 		     it will be left as zero.
9119 
9120 		     We could elide R_MIPS_REL32 for locally binding symbols
9121 		     in shared libraries, but do not yet do so.
9122 
9123 		     For an executable, we only need to copy this
9124 		     reloc if the symbol is defined in a dynamic
9125 		     object.  */
9126 		  hmips = (struct mips_elf_link_hash_entry *) h;
9127 		  ++hmips->possibly_dynamic_relocs;
9128 		  if (MIPS_ELF_READONLY_SECTION (sec))
9129 		    /* We need it to tell the dynamic linker if there
9130 		       are relocations against the text segment.  */
9131 		    hmips->readonly_reloc = true;
9132 		}
9133 	    }
9134 
9135 	  if (SGI_COMPAT (abfd))
9136 	    mips_elf_hash_table (info)->compact_rel_size +=
9137 	      sizeof (Elf32_External_crinfo);
9138 	  break;
9139 
9140 	case R_MIPS_26:
9141 	case R_MIPS_GPREL16:
9142 	case R_MIPS_LITERAL:
9143 	case R_MIPS_GPREL32:
9144 	case R_MICROMIPS_26_S1:
9145 	case R_MICROMIPS_GPREL16:
9146 	case R_MICROMIPS_LITERAL:
9147 	case R_MICROMIPS_GPREL7_S2:
9148 	  if (SGI_COMPAT (abfd))
9149 	    mips_elf_hash_table (info)->compact_rel_size +=
9150 	      sizeof (Elf32_External_crinfo);
9151 	  break;
9152 
9153 	  /* This relocation describes the C++ object vtable hierarchy.
9154 	     Reconstruct it for later use during GC.  */
9155 	case R_MIPS_GNU_VTINHERIT:
9156 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9157 	    return false;
9158 	  break;
9159 
9160 	  /* This relocation describes which C++ vtable entries are actually
9161 	     used.  Record for later use during GC.  */
9162 	case R_MIPS_GNU_VTENTRY:
9163 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9164 	    return false;
9165 	  break;
9166 
9167 	default:
9168 	  break;
9169 	}
9170 
9171       /* Record the need for a PLT entry.  At this point we don't know
9172 	 yet if we are going to create a PLT in the first place, but
9173 	 we only record whether the relocation requires a standard MIPS
9174 	 or a compressed code entry anyway.  If we don't make a PLT after
9175 	 all, then we'll just ignore these arrangements.  Likewise if
9176 	 a PLT entry is not created because the symbol is satisfied
9177 	 locally.  */
9178       if (h != NULL
9179 	  && (branch_reloc_p (r_type)
9180 	      || mips16_branch_reloc_p (r_type)
9181 	      || micromips_branch_reloc_p (r_type))
9182 	  && !SYMBOL_CALLS_LOCAL (info, h))
9183 	{
9184 	  if (h->plt.plist == NULL)
9185 	    h->plt.plist = mips_elf_make_plt_record (abfd);
9186 	  if (h->plt.plist == NULL)
9187 	    return false;
9188 
9189 	  if (branch_reloc_p (r_type))
9190 	    h->plt.plist->need_mips = true;
9191 	  else
9192 	    h->plt.plist->need_comp = true;
9193 	}
9194 
9195       /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9196 	 if there is one.  We only need to handle global symbols here;
9197 	 we decide whether to keep or delete stubs for local symbols
9198 	 when processing the stub's relocations.  */
9199       if (h != NULL
9200 	  && !mips16_call_reloc_p (r_type)
9201 	  && !section_allows_mips16_refs_p (sec))
9202 	{
9203 	  struct mips_elf_link_hash_entry *mh;
9204 
9205 	  mh = (struct mips_elf_link_hash_entry *) h;
9206 	  mh->need_fn_stub = true;
9207 	}
9208 
9209       /* Refuse some position-dependent relocations when creating a
9210 	 shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
9211 	 not PIC, but we can create dynamic relocations and the result
9212 	 will be fine.  Also do not refuse R_MIPS_LO16, which can be
9213 	 combined with R_MIPS_GOT16.  */
9214       if (bfd_link_pic (info))
9215 	{
9216 	  switch (r_type)
9217 	    {
9218 	    case R_MIPS_TLS_TPREL_HI16:
9219 	    case R_MIPS16_TLS_TPREL_HI16:
9220 	    case R_MICROMIPS_TLS_TPREL_HI16:
9221 	    case R_MIPS_TLS_TPREL_LO16:
9222 	    case R_MIPS16_TLS_TPREL_LO16:
9223 	    case R_MICROMIPS_TLS_TPREL_LO16:
9224 	      /* These are okay in PIE, but not in a shared library.  */
9225 	      if (bfd_link_executable (info))
9226 		break;
9227 
9228 	      /* FALLTHROUGH */
9229 
9230 	    case R_MIPS16_HI16:
9231 	    case R_MIPS_HI16:
9232 	    case R_MIPS_HIGHER:
9233 	    case R_MIPS_HIGHEST:
9234 	    case R_MICROMIPS_HI16:
9235 	    case R_MICROMIPS_HIGHER:
9236 	    case R_MICROMIPS_HIGHEST:
9237 	      /* Don't refuse a high part relocation if it's against
9238 		 no symbol (e.g. part of a compound relocation).  */
9239 	      if (r_symndx == STN_UNDEF)
9240 		break;
9241 
9242 	      /* Likewise an absolute symbol.  */
9243 	      if (h != NULL && bfd_is_abs_symbol (&h->root))
9244 		break;
9245 
9246 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9247 		 and has a special meaning.  */
9248 	      if (!NEWABI_P (abfd) && h != NULL
9249 		  && strcmp (h->root.root.string, "_gp_disp") == 0)
9250 		break;
9251 
9252 	      /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
9253 	      if (is_gott_symbol (info, h))
9254 		break;
9255 
9256 	      /* FALLTHROUGH */
9257 
9258 	    case R_MIPS16_26:
9259 	    case R_MIPS_26:
9260 	    case R_MICROMIPS_26_S1:
9261 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9262 	      /* An error for unsupported relocations is raised as part
9263 		 of the above search, so we can skip the following.  */
9264 	      if (howto != NULL)
9265 		info->callbacks->einfo
9266 		  /* xgettext:c-format */
9267 		  (_("%X%H: relocation %s against `%s' cannot be used"
9268 		     " when making a shared object; recompile with -fPIC\n"),
9269 		   abfd, sec, rel->r_offset, howto->name,
9270 		   (h) ? h->root.root.string : "a local symbol");
9271 	      break;
9272 	    default:
9273 	      break;
9274 	    }
9275 	}
9276     }
9277 
9278   return true;
9279 }
9280 
9281 /* Allocate space for global sym dynamic relocs.  */
9282 
9283 static bool
9284 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9285 {
9286   struct bfd_link_info *info = inf;
9287   bfd *dynobj;
9288   struct mips_elf_link_hash_entry *hmips;
9289   struct mips_elf_link_hash_table *htab;
9290 
9291   htab = mips_elf_hash_table (info);
9292   BFD_ASSERT (htab != NULL);
9293 
9294   dynobj = elf_hash_table (info)->dynobj;
9295   hmips = (struct mips_elf_link_hash_entry *) h;
9296 
9297   /* VxWorks executables are handled elsewhere; we only need to
9298      allocate relocations in shared objects.  */
9299   if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9300     return true;
9301 
9302   /* Ignore indirect symbols.  All relocations against such symbols
9303      will be redirected to the target symbol.  */
9304   if (h->root.type == bfd_link_hash_indirect)
9305     return true;
9306 
9307   /* If this symbol is defined in a dynamic object, or we are creating
9308      a shared library, we will need to copy any R_MIPS_32 or
9309      R_MIPS_REL32 relocs against it into the output file.  */
9310   if (! bfd_link_relocatable (info)
9311       && hmips->possibly_dynamic_relocs != 0
9312       && (h->root.type == bfd_link_hash_defweak
9313 	  || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9314 	  || bfd_link_pic (info)))
9315     {
9316       bool do_copy = true;
9317 
9318       if (h->root.type == bfd_link_hash_undefweak)
9319 	{
9320 	  /* Do not copy relocations for undefined weak symbols that
9321 	     we are not going to export.  */
9322 	  if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9323 	    do_copy = false;
9324 
9325 	  /* Make sure undefined weak symbols are output as a dynamic
9326 	     symbol in PIEs.  */
9327 	  else if (h->dynindx == -1 && !h->forced_local)
9328 	    {
9329 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
9330 		return false;
9331 	    }
9332 	}
9333 
9334       if (do_copy)
9335 	{
9336 	  /* Even though we don't directly need a GOT entry for this symbol,
9337 	     the SVR4 psABI requires it to have a dynamic symbol table
9338 	     index greater that DT_MIPS_GOTSYM if there are dynamic
9339 	     relocations against it.
9340 
9341 	     VxWorks does not enforce the same mapping between the GOT
9342 	     and the symbol table, so the same requirement does not
9343 	     apply there.  */
9344 	  if (htab->root.target_os != is_vxworks)
9345 	    {
9346 	      if (hmips->global_got_area > GGA_RELOC_ONLY)
9347 		hmips->global_got_area = GGA_RELOC_ONLY;
9348 	      hmips->got_only_for_calls = false;
9349 	    }
9350 
9351 	  mips_elf_allocate_dynamic_relocations
9352 	    (dynobj, info, hmips->possibly_dynamic_relocs);
9353 	  if (hmips->readonly_reloc)
9354 	    /* We tell the dynamic linker that there are relocations
9355 	       against the text segment.  */
9356 	    info->flags |= DF_TEXTREL;
9357 	}
9358     }
9359 
9360   return true;
9361 }
9362 
9363 /* Adjust a symbol defined by a dynamic object and referenced by a
9364    regular object.  The current definition is in some section of the
9365    dynamic object, but we're not including those sections.  We have to
9366    change the definition to something the rest of the link can
9367    understand.  */
9368 
9369 bool
9370 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9371 				     struct elf_link_hash_entry *h)
9372 {
9373   bfd *dynobj;
9374   struct mips_elf_link_hash_entry *hmips;
9375   struct mips_elf_link_hash_table *htab;
9376   asection *s, *srel;
9377 
9378   htab = mips_elf_hash_table (info);
9379   BFD_ASSERT (htab != NULL);
9380 
9381   dynobj = elf_hash_table (info)->dynobj;
9382   hmips = (struct mips_elf_link_hash_entry *) h;
9383 
9384   /* Make sure we know what is going on here.  */
9385   if (dynobj == NULL
9386       || (! h->needs_plt
9387 	  && ! h->is_weakalias
9388 	  && (! h->def_dynamic
9389 	      || ! h->ref_regular
9390 	      || h->def_regular)))
9391     {
9392       if (h->type == STT_GNU_IFUNC)
9393 	_bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9394 			    h->root.root.string);
9395       else
9396 	_bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9397 			    h->root.root.string);
9398       return true;
9399     }
9400 
9401   hmips = (struct mips_elf_link_hash_entry *) h;
9402 
9403   /* If there are call relocations against an externally-defined symbol,
9404      see whether we can create a MIPS lazy-binding stub for it.  We can
9405      only do this if all references to the function are through call
9406      relocations, and in that case, the traditional lazy-binding stubs
9407      are much more efficient than PLT entries.
9408 
9409      Traditional stubs are only available on SVR4 psABI-based systems;
9410      VxWorks always uses PLTs instead.  */
9411   if (htab->root.target_os != is_vxworks
9412       && h->needs_plt
9413       && !hmips->no_fn_stub)
9414     {
9415       if (! elf_hash_table (info)->dynamic_sections_created)
9416 	return true;
9417 
9418       /* If this symbol is not defined in a regular file, then set
9419 	 the symbol to the stub location.  This is required to make
9420 	 function pointers compare as equal between the normal
9421 	 executable and the shared library.  */
9422       if (!h->def_regular
9423 	  && !bfd_is_abs_section (htab->sstubs->output_section))
9424 	{
9425 	  hmips->needs_lazy_stub = true;
9426 	  htab->lazy_stub_count++;
9427 	  return true;
9428 	}
9429     }
9430   /* As above, VxWorks requires PLT entries for externally-defined
9431      functions that are only accessed through call relocations.
9432 
9433      Both VxWorks and non-VxWorks targets also need PLT entries if there
9434      are static-only relocations against an externally-defined function.
9435      This can technically occur for shared libraries if there are
9436      branches to the symbol, although it is unlikely that this will be
9437      used in practice due to the short ranges involved.  It can occur
9438      for any relative or absolute relocation in executables; in that
9439      case, the PLT entry becomes the function's canonical address.  */
9440   else if (((h->needs_plt && !hmips->no_fn_stub)
9441 	    || (h->type == STT_FUNC && hmips->has_static_relocs))
9442 	   && htab->use_plts_and_copy_relocs
9443 	   && !SYMBOL_CALLS_LOCAL (info, h)
9444 	   && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9445 		&& h->root.type == bfd_link_hash_undefweak))
9446     {
9447       bool micromips_p = MICROMIPS_P (info->output_bfd);
9448       bool newabi_p = NEWABI_P (info->output_bfd);
9449 
9450       /* If this is the first symbol to need a PLT entry, then make some
9451 	 basic setup.  Also work out PLT entry sizes.  We'll need them
9452 	 for PLT offset calculations.  */
9453       if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9454 	{
9455 	  BFD_ASSERT (htab->root.sgotplt->size == 0);
9456 	  BFD_ASSERT (htab->plt_got_index == 0);
9457 
9458 	  /* If we're using the PLT additions to the psABI, each PLT
9459 	     entry is 16 bytes and the PLT0 entry is 32 bytes.
9460 	     Encourage better cache usage by aligning.  We do this
9461 	     lazily to avoid pessimizing traditional objects.  */
9462 	  if (htab->root.target_os != is_vxworks
9463 	      && !bfd_set_section_alignment (htab->root.splt, 5))
9464 	    return false;
9465 
9466 	  /* Make sure that .got.plt is word-aligned.  We do this lazily
9467 	     for the same reason as above.  */
9468 	  if (!bfd_set_section_alignment (htab->root.sgotplt,
9469 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9470 	    return false;
9471 
9472 	  /* On non-VxWorks targets, the first two entries in .got.plt
9473 	     are reserved.  */
9474 	  if (htab->root.target_os != is_vxworks)
9475 	    htab->plt_got_index
9476 	      += (get_elf_backend_data (dynobj)->got_header_size
9477 		  / MIPS_ELF_GOT_SIZE (dynobj));
9478 
9479 	  /* On VxWorks, also allocate room for the header's
9480 	     .rela.plt.unloaded entries.  */
9481 	  if (htab->root.target_os == is_vxworks
9482 	      && !bfd_link_pic (info))
9483 	    htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9484 
9485 	  /* Now work out the sizes of individual PLT entries.  */
9486 	  if (htab->root.target_os == is_vxworks
9487 	      && bfd_link_pic (info))
9488 	    htab->plt_mips_entry_size
9489 	      = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9490 	  else if (htab->root.target_os == is_vxworks)
9491 	    htab->plt_mips_entry_size
9492 	      = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9493 	  else if (newabi_p)
9494 	    htab->plt_mips_entry_size
9495 	      = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9496 	  else if (!micromips_p)
9497 	    {
9498 	      htab->plt_mips_entry_size
9499 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
9500 	      htab->plt_comp_entry_size
9501 		= 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9502 	    }
9503 	  else if (htab->insn32)
9504 	    {
9505 	      htab->plt_mips_entry_size
9506 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
9507 	      htab->plt_comp_entry_size
9508 		= 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9509 	    }
9510 	  else
9511 	    {
9512 	      htab->plt_mips_entry_size
9513 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
9514 	      htab->plt_comp_entry_size
9515 		= 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9516 	    }
9517 	}
9518 
9519       if (h->plt.plist == NULL)
9520 	h->plt.plist = mips_elf_make_plt_record (dynobj);
9521       if (h->plt.plist == NULL)
9522 	return false;
9523 
9524       /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9525 	 n32 or n64, so always use a standard entry there.
9526 
9527 	 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9528 	 all MIPS16 calls will go via that stub, and there is no benefit
9529 	 to having a MIPS16 entry.  And in the case of call_stub a
9530 	 standard entry actually has to be used as the stub ends with a J
9531 	 instruction.  */
9532       if (newabi_p
9533 	  || htab->root.target_os == is_vxworks
9534 	  || hmips->call_stub
9535 	  || hmips->call_fp_stub)
9536 	{
9537 	  h->plt.plist->need_mips = true;
9538 	  h->plt.plist->need_comp = false;
9539 	}
9540 
9541       /* Otherwise, if there are no direct calls to the function, we
9542 	 have a free choice of whether to use standard or compressed
9543 	 entries.  Prefer microMIPS entries if the object is known to
9544 	 contain microMIPS code, so that it becomes possible to create
9545 	 pure microMIPS binaries.  Prefer standard entries otherwise,
9546 	 because MIPS16 ones are no smaller and are usually slower.  */
9547       if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9548 	{
9549 	  if (micromips_p)
9550 	    h->plt.plist->need_comp = true;
9551 	  else
9552 	    h->plt.plist->need_mips = true;
9553 	}
9554 
9555       if (h->plt.plist->need_mips)
9556 	{
9557 	  h->plt.plist->mips_offset = htab->plt_mips_offset;
9558 	  htab->plt_mips_offset += htab->plt_mips_entry_size;
9559 	}
9560       if (h->plt.plist->need_comp)
9561 	{
9562 	  h->plt.plist->comp_offset = htab->plt_comp_offset;
9563 	  htab->plt_comp_offset += htab->plt_comp_entry_size;
9564 	}
9565 
9566       /* Reserve the corresponding .got.plt entry now too.  */
9567       h->plt.plist->gotplt_index = htab->plt_got_index++;
9568 
9569       /* If the output file has no definition of the symbol, set the
9570 	 symbol's value to the address of the stub.  */
9571       if (!bfd_link_pic (info) && !h->def_regular)
9572 	hmips->use_plt_entry = true;
9573 
9574       /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
9575       htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9576 				   ? MIPS_ELF_RELA_SIZE (dynobj)
9577 				   : MIPS_ELF_REL_SIZE (dynobj));
9578 
9579       /* Make room for the .rela.plt.unloaded relocations.  */
9580       if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9581 	htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9582 
9583       /* All relocations against this symbol that could have been made
9584 	 dynamic will now refer to the PLT entry instead.  */
9585       hmips->possibly_dynamic_relocs = 0;
9586 
9587       return true;
9588     }
9589 
9590   /* If this is a weak symbol, and there is a real definition, the
9591      processor independent code will have arranged for us to see the
9592      real definition first, and we can just use the same value.  */
9593   if (h->is_weakalias)
9594     {
9595       struct elf_link_hash_entry *def = weakdef (h);
9596       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9597       h->root.u.def.section = def->root.u.def.section;
9598       h->root.u.def.value = def->root.u.def.value;
9599       return true;
9600     }
9601 
9602   /* Otherwise, there is nothing further to do for symbols defined
9603      in regular objects.  */
9604   if (h->def_regular)
9605     return true;
9606 
9607   /* There's also nothing more to do if we'll convert all relocations
9608      against this symbol into dynamic relocations.  */
9609   if (!hmips->has_static_relocs)
9610     return true;
9611 
9612   /* We're now relying on copy relocations.  Complain if we have
9613      some that we can't convert.  */
9614   if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9615     {
9616       _bfd_error_handler (_("non-dynamic relocations refer to "
9617 			    "dynamic symbol %s"),
9618 			  h->root.root.string);
9619       bfd_set_error (bfd_error_bad_value);
9620       return false;
9621     }
9622 
9623   /* We must allocate the symbol in our .dynbss section, which will
9624      become part of the .bss section of the executable.  There will be
9625      an entry for this symbol in the .dynsym section.  The dynamic
9626      object will contain position independent code, so all references
9627      from the dynamic object to this symbol will go through the global
9628      offset table.  The dynamic linker will use the .dynsym entry to
9629      determine the address it must put in the global offset table, so
9630      both the dynamic object and the regular object will refer to the
9631      same memory location for the variable.  */
9632 
9633   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9634     {
9635       s = htab->root.sdynrelro;
9636       srel = htab->root.sreldynrelro;
9637     }
9638   else
9639     {
9640       s = htab->root.sdynbss;
9641       srel = htab->root.srelbss;
9642     }
9643   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9644     {
9645       if (htab->root.target_os == is_vxworks)
9646 	srel->size += sizeof (Elf32_External_Rela);
9647       else
9648 	mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9649       h->needs_copy = 1;
9650     }
9651 
9652   /* All relocations against this symbol that could have been made
9653      dynamic will now refer to the local copy instead.  */
9654   hmips->possibly_dynamic_relocs = 0;
9655 
9656   return _bfd_elf_adjust_dynamic_copy (info, h, s);
9657 }
9658 
9659 /* If the link uses a GOT, lay it out and work out its size.  */
9660 
9661 static bool
9662 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9663 {
9664   bfd *dynobj;
9665   asection *s;
9666   struct mips_got_info *g;
9667   bfd_size_type loadable_size = 0;
9668   bfd_size_type page_gotno;
9669   bfd *ibfd;
9670   struct mips_elf_traverse_got_arg tga;
9671   struct mips_elf_link_hash_table *htab;
9672 
9673   htab = mips_elf_hash_table (info);
9674   BFD_ASSERT (htab != NULL);
9675 
9676   s = htab->root.sgot;
9677   if (s == NULL)
9678     return true;
9679 
9680   dynobj = elf_hash_table (info)->dynobj;
9681   g = htab->got_info;
9682 
9683   /* Allocate room for the reserved entries.  VxWorks always reserves
9684      3 entries; other objects only reserve 2 entries.  */
9685   BFD_ASSERT (g->assigned_low_gotno == 0);
9686   if (htab->root.target_os == is_vxworks)
9687     htab->reserved_gotno = 3;
9688   else
9689     htab->reserved_gotno = 2;
9690   g->local_gotno += htab->reserved_gotno;
9691   g->assigned_low_gotno = htab->reserved_gotno;
9692 
9693   /* Decide which symbols need to go in the global part of the GOT and
9694      count the number of reloc-only GOT symbols.  */
9695   mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9696 
9697   if (!mips_elf_resolve_final_got_entries (info, g))
9698     return false;
9699 
9700   /* Calculate the total loadable size of the output.  That
9701      will give us the maximum number of GOT_PAGE entries
9702      required.  */
9703   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9704     {
9705       asection *subsection;
9706 
9707       for (subsection = ibfd->sections;
9708 	   subsection;
9709 	   subsection = subsection->next)
9710 	{
9711 	  if ((subsection->flags & SEC_ALLOC) == 0)
9712 	    continue;
9713 	  loadable_size += ((subsection->size + 0xf)
9714 			    &~ (bfd_size_type) 0xf);
9715 	}
9716     }
9717 
9718   if (htab->root.target_os == is_vxworks)
9719     /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9720        relocations against local symbols evaluate to "G", and the EABI does
9721        not include R_MIPS_GOT_PAGE.  */
9722     page_gotno = 0;
9723   else
9724     /* Assume there are two loadable segments consisting of contiguous
9725        sections.  Is 5 enough?  */
9726     page_gotno = (loadable_size >> 16) + 5;
9727 
9728   /* Choose the smaller of the two page estimates; both are intended to be
9729      conservative.  */
9730   if (page_gotno > g->page_gotno)
9731     page_gotno = g->page_gotno;
9732 
9733   g->local_gotno += page_gotno;
9734   g->assigned_high_gotno = g->local_gotno - 1;
9735 
9736   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9737   s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9738   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9739 
9740   /* VxWorks does not support multiple GOTs.  It initializes $gp to
9741      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9742      dynamic loader.  */
9743   if (htab->root.target_os != is_vxworks
9744       && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9745     {
9746       if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9747 	return false;
9748     }
9749   else
9750     {
9751       /* Record that all bfds use G.  This also has the effect of freeing
9752 	 the per-bfd GOTs, which we no longer need.  */
9753       for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9754 	if (mips_elf_bfd_got (ibfd, false))
9755 	  mips_elf_replace_bfd_got (ibfd, g);
9756       mips_elf_replace_bfd_got (output_bfd, g);
9757 
9758       /* Set up TLS entries.  */
9759       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9760       tga.info = info;
9761       tga.g = g;
9762       tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9763       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9764       if (!tga.g)
9765 	return false;
9766       BFD_ASSERT (g->tls_assigned_gotno
9767 		  == g->global_gotno + g->local_gotno + g->tls_gotno);
9768 
9769       /* Each VxWorks GOT entry needs an explicit relocation.  */
9770       if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9771 	g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9772 
9773       /* Allocate room for the TLS relocations.  */
9774       if (g->relocs)
9775 	mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9776     }
9777 
9778   return true;
9779 }
9780 
9781 /* Estimate the size of the .MIPS.stubs section.  */
9782 
9783 static void
9784 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9785 {
9786   struct mips_elf_link_hash_table *htab;
9787   bfd_size_type dynsymcount;
9788 
9789   htab = mips_elf_hash_table (info);
9790   BFD_ASSERT (htab != NULL);
9791 
9792   if (htab->lazy_stub_count == 0)
9793     return;
9794 
9795   /* IRIX rld assumes that a function stub isn't at the end of the .text
9796      section, so add a dummy entry to the end.  */
9797   htab->lazy_stub_count++;
9798 
9799   /* Get a worst-case estimate of the number of dynamic symbols needed.
9800      At this point, dynsymcount does not account for section symbols
9801      and count_section_dynsyms may overestimate the number that will
9802      be needed.  */
9803   dynsymcount = (elf_hash_table (info)->dynsymcount
9804 		 + count_section_dynsyms (output_bfd, info));
9805 
9806   /* Determine the size of one stub entry.  There's no disadvantage
9807      from using microMIPS code here, so for the sake of pure-microMIPS
9808      binaries we prefer it whenever there's any microMIPS code in
9809      output produced at all.  This has a benefit of stubs being
9810      shorter by 4 bytes each too, unless in the insn32 mode.  */
9811   if (!MICROMIPS_P (output_bfd))
9812     htab->function_stub_size = (dynsymcount > 0x10000
9813 				? MIPS_FUNCTION_STUB_BIG_SIZE
9814 				: MIPS_FUNCTION_STUB_NORMAL_SIZE);
9815   else if (htab->insn32)
9816     htab->function_stub_size = (dynsymcount > 0x10000
9817 				? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9818 				: MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9819   else
9820     htab->function_stub_size = (dynsymcount > 0x10000
9821 				? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9822 				: MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9823 
9824   htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9825 }
9826 
9827 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9828    mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
9829    stub, allocate an entry in the stubs section.  */
9830 
9831 static bool
9832 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9833 {
9834   struct mips_htab_traverse_info *hti = data;
9835   struct mips_elf_link_hash_table *htab;
9836   struct bfd_link_info *info;
9837   bfd *output_bfd;
9838 
9839   info = hti->info;
9840   output_bfd = hti->output_bfd;
9841   htab = mips_elf_hash_table (info);
9842   BFD_ASSERT (htab != NULL);
9843 
9844   if (h->needs_lazy_stub)
9845     {
9846       bool micromips_p = MICROMIPS_P (output_bfd);
9847       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9848       bfd_vma isa_bit = micromips_p;
9849 
9850       BFD_ASSERT (htab->root.dynobj != NULL);
9851       if (h->root.plt.plist == NULL)
9852 	h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9853       if (h->root.plt.plist == NULL)
9854 	{
9855 	  hti->error = true;
9856 	  return false;
9857 	}
9858       h->root.root.u.def.section = htab->sstubs;
9859       h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9860       h->root.plt.plist->stub_offset = htab->sstubs->size;
9861       h->root.other = other;
9862       htab->sstubs->size += htab->function_stub_size;
9863     }
9864   return true;
9865 }
9866 
9867 /* Allocate offsets in the stubs section to each symbol that needs one.
9868    Set the final size of the .MIPS.stub section.  */
9869 
9870 static bool
9871 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9872 {
9873   bfd *output_bfd = info->output_bfd;
9874   bool micromips_p = MICROMIPS_P (output_bfd);
9875   unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9876   bfd_vma isa_bit = micromips_p;
9877   struct mips_elf_link_hash_table *htab;
9878   struct mips_htab_traverse_info hti;
9879   struct elf_link_hash_entry *h;
9880   bfd *dynobj;
9881 
9882   htab = mips_elf_hash_table (info);
9883   BFD_ASSERT (htab != NULL);
9884 
9885   if (htab->lazy_stub_count == 0)
9886     return true;
9887 
9888   htab->sstubs->size = 0;
9889   hti.info = info;
9890   hti.output_bfd = output_bfd;
9891   hti.error = false;
9892   mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9893   if (hti.error)
9894     return false;
9895   htab->sstubs->size += htab->function_stub_size;
9896   BFD_ASSERT (htab->sstubs->size
9897 	      == htab->lazy_stub_count * htab->function_stub_size);
9898 
9899   dynobj = elf_hash_table (info)->dynobj;
9900   BFD_ASSERT (dynobj != NULL);
9901   h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9902   if (h == NULL)
9903     return false;
9904   h->root.u.def.value = isa_bit;
9905   h->other = other;
9906   h->type = STT_FUNC;
9907 
9908   return true;
9909 }
9910 
9911 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9912    bfd_link_info.  If H uses the address of a PLT entry as the value
9913    of the symbol, then set the entry in the symbol table now.  Prefer
9914    a standard MIPS PLT entry.  */
9915 
9916 static bool
9917 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9918 {
9919   struct bfd_link_info *info = data;
9920   bool micromips_p = MICROMIPS_P (info->output_bfd);
9921   struct mips_elf_link_hash_table *htab;
9922   unsigned int other;
9923   bfd_vma isa_bit;
9924   bfd_vma val;
9925 
9926   htab = mips_elf_hash_table (info);
9927   BFD_ASSERT (htab != NULL);
9928 
9929   if (h->use_plt_entry)
9930     {
9931       BFD_ASSERT (h->root.plt.plist != NULL);
9932       BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9933 		  || h->root.plt.plist->comp_offset != MINUS_ONE);
9934 
9935       val = htab->plt_header_size;
9936       if (h->root.plt.plist->mips_offset != MINUS_ONE)
9937 	{
9938 	  isa_bit = 0;
9939 	  val += h->root.plt.plist->mips_offset;
9940 	  other = 0;
9941 	}
9942       else
9943 	{
9944 	  isa_bit = 1;
9945 	  val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9946 	  other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9947 	}
9948       val += isa_bit;
9949       /* For VxWorks, point at the PLT load stub rather than the lazy
9950 	 resolution stub; this stub will become the canonical function
9951 	 address.  */
9952       if (htab->root.target_os == is_vxworks)
9953 	val += 8;
9954 
9955       h->root.root.u.def.section = htab->root.splt;
9956       h->root.root.u.def.value = val;
9957       h->root.other = other;
9958     }
9959 
9960   return true;
9961 }
9962 
9963 /* Set the sizes of the dynamic sections, some mips non-dynamic sections,
9964    and check for any mips16 stub sections that we can discard.  */
9965 
9966 bool
9967 _bfd_mips_elf_late_size_sections (bfd *output_bfd,
9968 				  struct bfd_link_info *info)
9969 {
9970   bfd *dynobj;
9971   asection *s, *sreldyn;
9972   bool reltext;
9973   struct mips_elf_link_hash_table *htab;
9974   struct mips_htab_traverse_info hti;
9975 
9976   htab = mips_elf_hash_table (info);
9977   BFD_ASSERT (htab != NULL);
9978 
9979   /* The .reginfo section has a fixed size.  */
9980   s = bfd_get_section_by_name (output_bfd, ".reginfo");
9981   if (s != NULL)
9982     {
9983       bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
9984       s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9985     }
9986 
9987   /* The .MIPS.abiflags section has a fixed size.  */
9988   s = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9989   if (s != NULL)
9990     {
9991       bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
9992       s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9993     }
9994 
9995   hti.info = info;
9996   hti.output_bfd = output_bfd;
9997   hti.error = false;
9998   mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
9999   if (hti.error)
10000     return false;
10001 
10002   dynobj = htab->root.dynobj;
10003   if (dynobj == NULL)
10004     return true;
10005 
10006   if (htab->root.dynamic_sections_created)
10007     {
10008       /* Set the contents of the .interp section to the interpreter.  */
10009       if (bfd_link_executable (info) && !info->nointerp)
10010 	{
10011 	  s = bfd_get_linker_section (dynobj, ".interp");
10012 	  BFD_ASSERT (s != NULL);
10013 	  s->size
10014 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10015 	  s->contents
10016 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10017 	}
10018 
10019       /* Figure out the size of the PLT header if we know that we
10020 	 are using it.  For the sake of cache alignment always use
10021 	 a standard header whenever any standard entries are present
10022 	 even if microMIPS entries are present as well.  This also
10023 	 lets the microMIPS header rely on the value of $v0 only set
10024 	 by microMIPS entries, for a small size reduction.
10025 
10026 	 Set symbol table entry values for symbols that use the
10027 	 address of their PLT entry now that we can calculate it.
10028 
10029 	 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10030 	 haven't already in _bfd_elf_create_dynamic_sections.  */
10031       if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10032 	{
10033 	  bool micromips_p = (MICROMIPS_P (output_bfd)
10034 				     && !htab->plt_mips_offset);
10035 	  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10036 	  bfd_vma isa_bit = micromips_p;
10037 	  struct elf_link_hash_entry *h;
10038 	  bfd_vma size;
10039 
10040 	  BFD_ASSERT (htab->use_plts_and_copy_relocs);
10041 	  BFD_ASSERT (htab->root.sgotplt->size == 0);
10042 	  BFD_ASSERT (htab->root.splt->size == 0);
10043 
10044 	  if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10045 	    size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10046 	  else if (htab->root.target_os == is_vxworks)
10047 	    size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10048 	  else if (ABI_64_P (output_bfd))
10049 	    size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10050 	  else if (ABI_N32_P (output_bfd))
10051 	    size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10052 	  else if (!micromips_p)
10053 	    size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10054 	  else if (htab->insn32)
10055 	    size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10056 	  else
10057 	    size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10058 
10059 	  htab->plt_header_is_comp = micromips_p;
10060 	  htab->plt_header_size = size;
10061 	  htab->root.splt->size = (size
10062 				   + htab->plt_mips_offset
10063 				   + htab->plt_comp_offset);
10064 	  htab->root.sgotplt->size = (htab->plt_got_index
10065 				      * MIPS_ELF_GOT_SIZE (dynobj));
10066 
10067 	  mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10068 
10069 	  if (htab->root.hplt == NULL)
10070 	    {
10071 	      h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10072 					       "_PROCEDURE_LINKAGE_TABLE_");
10073 	      htab->root.hplt = h;
10074 	      if (h == NULL)
10075 		return false;
10076 	    }
10077 
10078 	  h = htab->root.hplt;
10079 	  h->root.u.def.value = isa_bit;
10080 	  h->other = other;
10081 	  h->type = STT_FUNC;
10082 	}
10083     }
10084 
10085   /* Allocate space for global sym dynamic relocs.  */
10086   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10087 
10088   mips_elf_estimate_stub_size (output_bfd, info);
10089 
10090   if (!mips_elf_lay_out_got (output_bfd, info))
10091     return false;
10092 
10093   mips_elf_lay_out_lazy_stubs (info);
10094 
10095   /* The check_relocs and adjust_dynamic_symbol entry points have
10096      determined the sizes of the various dynamic sections.  Allocate
10097      memory for them.  */
10098   reltext = false;
10099   for (s = dynobj->sections; s != NULL; s = s->next)
10100     {
10101       const char *name;
10102 
10103       /* It's OK to base decisions on the section name, because none
10104 	 of the dynobj section names depend upon the input files.  */
10105       name = bfd_section_name (s);
10106 
10107       if ((s->flags & SEC_LINKER_CREATED) == 0)
10108 	continue;
10109 
10110       if (startswith (name, ".rel"))
10111 	{
10112 	  if (s->size != 0)
10113 	    {
10114 	      const char *outname;
10115 	      asection *target;
10116 
10117 	      /* If this relocation section applies to a read only
10118 		 section, then we probably need a DT_TEXTREL entry.
10119 		 If the relocation section is .rel(a).dyn, we always
10120 		 assert a DT_TEXTREL entry rather than testing whether
10121 		 there exists a relocation to a read only section or
10122 		 not.  */
10123 	      outname = bfd_section_name (s->output_section);
10124 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
10125 	      if ((target != NULL
10126 		   && (target->flags & SEC_READONLY) != 0
10127 		   && (target->flags & SEC_ALLOC) != 0)
10128 		  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10129 		reltext = true;
10130 
10131 	      /* We use the reloc_count field as a counter if we need
10132 		 to copy relocs into the output file.  */
10133 	      if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10134 		s->reloc_count = 0;
10135 
10136 	      /* If combreloc is enabled, elf_link_sort_relocs() will
10137 		 sort relocations, but in a different way than we do,
10138 		 and before we're done creating relocations.  Also, it
10139 		 will move them around between input sections'
10140 		 relocation's contents, so our sorting would be
10141 		 broken, so don't let it run.  */
10142 	      info->combreloc = 0;
10143 	    }
10144 	}
10145       else if (bfd_link_executable (info)
10146 	       && !htab->use_rld_obj_head
10147 	       && startswith (name, ".rld_map"))
10148 	{
10149 	  /* We add a room for __rld_map.  It will be filled in by the
10150 	     rtld to contain a pointer to the _r_debug structure.  */
10151 	  s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10152 	}
10153       else if (SGI_COMPAT (output_bfd)
10154 	       && startswith (name, ".compact_rel"))
10155 	s->size += htab->compact_rel_size;
10156       else if (s == htab->root.splt)
10157 	{
10158 	  /* If the last PLT entry has a branch delay slot, allocate
10159 	     room for an extra nop to fill the delay slot.  This is
10160 	     for CPUs without load interlocking.  */
10161 	  if (! LOAD_INTERLOCKS_P (output_bfd)
10162 	      && htab->root.target_os != is_vxworks
10163 	      && s->size > 0)
10164 	    s->size += 4;
10165 	}
10166       else if (! startswith (name, ".init")
10167 	       && s != htab->root.sgot
10168 	       && s != htab->root.sgotplt
10169 	       && s != htab->sstubs
10170 	       && s != htab->root.sdynbss
10171 	       && s != htab->root.sdynrelro)
10172 	{
10173 	  /* It's not one of our sections, so don't allocate space.  */
10174 	  continue;
10175 	}
10176 
10177       if (s->size == 0)
10178 	{
10179 	  s->flags |= SEC_EXCLUDE;
10180 	  continue;
10181 	}
10182 
10183       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10184 	continue;
10185 
10186       /* Allocate memory for the section contents.  */
10187       s->contents = bfd_zalloc (dynobj, s->size);
10188       if (s->contents == NULL)
10189 	{
10190 	  bfd_set_error (bfd_error_no_memory);
10191 	  return false;
10192 	}
10193     }
10194 
10195   if (htab->root.dynamic_sections_created)
10196     {
10197       /* Add some entries to the .dynamic section.  We fill in the
10198 	 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10199 	 must add the entries now so that we get the correct size for
10200 	 the .dynamic section.  */
10201 
10202       /* SGI object has the equivalence of DT_DEBUG in the
10203 	 DT_MIPS_RLD_MAP entry.  This must come first because glibc
10204 	 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10205 	 may only look at the first one they see.  */
10206       if (!bfd_link_pic (info)
10207 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10208 	return false;
10209 
10210       if (bfd_link_executable (info)
10211 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10212 	return false;
10213 
10214       /* The DT_DEBUG entry may be filled in by the dynamic linker and
10215 	 used by the debugger.  */
10216       if (bfd_link_executable (info)
10217 	  && !SGI_COMPAT (output_bfd)
10218 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10219 	return false;
10220 
10221       if (reltext
10222 	  && (SGI_COMPAT (output_bfd)
10223 	      || htab->root.target_os == is_vxworks))
10224 	info->flags |= DF_TEXTREL;
10225 
10226       if ((info->flags & DF_TEXTREL) != 0)
10227 	{
10228 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10229 	    return false;
10230 
10231 	  /* Clear the DF_TEXTREL flag.  It will be set again if we
10232 	     write out an actual text relocation; we may not, because
10233 	     at this point we do not know whether e.g. any .eh_frame
10234 	     absolute relocations have been converted to PC-relative.  */
10235 	  info->flags &= ~DF_TEXTREL;
10236 	}
10237 
10238       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10239 	return false;
10240 
10241       sreldyn = mips_elf_rel_dyn_section (info, false);
10242       if (htab->root.target_os == is_vxworks)
10243 	{
10244 	  /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
10245 	     use any of the DT_MIPS_* tags.  */
10246 	  if (sreldyn && sreldyn->size > 0)
10247 	    {
10248 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10249 		return false;
10250 
10251 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10252 		return false;
10253 
10254 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10255 		return false;
10256 	    }
10257 	}
10258       else
10259 	{
10260 	  if (sreldyn && sreldyn->size > 0
10261 	      && !bfd_is_abs_section (sreldyn->output_section))
10262 	    {
10263 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10264 		return false;
10265 
10266 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10267 		return false;
10268 
10269 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10270 		return false;
10271 	    }
10272 
10273 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10274 	    return false;
10275 
10276 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10277 	    return false;
10278 
10279 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10280 	    return false;
10281 
10282 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10283 	    return false;
10284 
10285 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10286 	    return false;
10287 
10288 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10289 	    return false;
10290 
10291 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10292 	    return false;
10293 
10294 	  if (info->emit_gnu_hash
10295 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10296 	    return false;
10297 
10298 	  if (IRIX_COMPAT (dynobj) == ict_irix5
10299 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10300 	    return false;
10301 
10302 	  if (IRIX_COMPAT (dynobj) == ict_irix6
10303 	      && (bfd_get_section_by_name
10304 		  (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10305 	      && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10306 	    return false;
10307 	}
10308       if (htab->root.splt->size > 0)
10309 	{
10310 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10311 	    return false;
10312 
10313 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10314 	    return false;
10315 
10316 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10317 	    return false;
10318 
10319 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10320 	    return false;
10321 	}
10322       if (htab->root.target_os == is_vxworks
10323 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10324 	return false;
10325     }
10326 
10327   return true;
10328 }
10329 
10330 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10331    Adjust its R_ADDEND field so that it is correct for the output file.
10332    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10333    and sections respectively; both use symbol indexes.  */
10334 
10335 static void
10336 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10337 			bfd *input_bfd, Elf_Internal_Sym *local_syms,
10338 			asection **local_sections, Elf_Internal_Rela *rel)
10339 {
10340   unsigned int r_type, r_symndx;
10341   Elf_Internal_Sym *sym;
10342   asection *sec;
10343 
10344   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10345     {
10346       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10347       if (gprel16_reloc_p (r_type)
10348 	  || r_type == R_MIPS_GPREL32
10349 	  || literal_reloc_p (r_type))
10350 	{
10351 	  rel->r_addend += _bfd_get_gp_value (input_bfd);
10352 	  rel->r_addend -= _bfd_get_gp_value (output_bfd);
10353 	}
10354 
10355       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10356       sym = local_syms + r_symndx;
10357 
10358       /* Adjust REL's addend to account for section merging.  */
10359       if (!bfd_link_relocatable (info))
10360 	{
10361 	  sec = local_sections[r_symndx];
10362 	  _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10363 	}
10364 
10365       /* This would normally be done by the rela_normal code in elflink.c.  */
10366       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10367 	rel->r_addend += local_sections[r_symndx]->output_offset;
10368     }
10369 }
10370 
10371 /* Handle relocations against symbols from removed linkonce sections,
10372    or sections discarded by a linker script.  We use this wrapper around
10373    RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10374    on 64-bit ELF targets.  In this case for any relocation handled, which
10375    always be the first in a triplet, the remaining two have to be processed
10376    together with the first, even if they are R_MIPS_NONE.  It is the symbol
10377    index referred by the first reloc that applies to all the three and the
10378    remaining two never refer to an object symbol.  And it is the final
10379    relocation (the last non-null one) that determines the output field of
10380    the whole relocation so retrieve the corresponding howto structure for
10381    the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10382 
10383    Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10384    and therefore requires to be pasted in a loop.  It also defines a block
10385    and does not protect any of its arguments, hence the extra brackets.  */
10386 
10387 static void
10388 mips_reloc_against_discarded_section (bfd *output_bfd,
10389 				      struct bfd_link_info *info,
10390 				      bfd *input_bfd, asection *input_section,
10391 				      Elf_Internal_Rela **rel,
10392 				      const Elf_Internal_Rela **relend,
10393 				      bool rel_reloc,
10394 				      reloc_howto_type *howto,
10395 				      bfd_byte *contents)
10396 {
10397   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10398   int count = bed->s->int_rels_per_ext_rel;
10399   unsigned int r_type;
10400   int i;
10401 
10402   for (i = count - 1; i > 0; i--)
10403     {
10404       r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10405       if (r_type != R_MIPS_NONE)
10406 	{
10407 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10408 	  break;
10409 	}
10410     }
10411   do
10412     {
10413        RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10414 					(*rel), count, (*relend),
10415 					howto, i, contents);
10416     }
10417   while (0);
10418 }
10419 
10420 /* Relocate a MIPS ELF section.  */
10421 
10422 int
10423 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10424 				bfd *input_bfd, asection *input_section,
10425 				bfd_byte *contents, Elf_Internal_Rela *relocs,
10426 				Elf_Internal_Sym *local_syms,
10427 				asection **local_sections)
10428 {
10429   Elf_Internal_Rela *rel;
10430   const Elf_Internal_Rela *relend;
10431   bfd_vma addend = 0;
10432   bool use_saved_addend_p = false;
10433 
10434   relend = relocs + input_section->reloc_count;
10435   for (rel = relocs; rel < relend; ++rel)
10436     {
10437       const char *name;
10438       bfd_vma value = 0;
10439       reloc_howto_type *howto;
10440       bool cross_mode_jump_p = false;
10441       /* TRUE if the relocation is a RELA relocation, rather than a
10442 	 REL relocation.  */
10443       bool rela_relocation_p = true;
10444       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10445       const char *msg;
10446       unsigned long r_symndx;
10447       asection *sec;
10448       Elf_Internal_Shdr *symtab_hdr;
10449       struct elf_link_hash_entry *h;
10450       bool rel_reloc;
10451 
10452       rel_reloc = (NEWABI_P (input_bfd)
10453 		   && mips_elf_rel_relocation_p (input_bfd, input_section,
10454 						 relocs, rel));
10455       /* Find the relocation howto for this relocation.  */
10456       howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10457 
10458       r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10459       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10460       if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10461 	{
10462 	  sec = local_sections[r_symndx];
10463 	  h = NULL;
10464 	}
10465       else
10466 	{
10467 	  unsigned long extsymoff;
10468 
10469 	  extsymoff = 0;
10470 	  if (!elf_bad_symtab (input_bfd))
10471 	    extsymoff = symtab_hdr->sh_info;
10472 	  h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10473 	  while (h->root.type == bfd_link_hash_indirect
10474 		 || h->root.type == bfd_link_hash_warning)
10475 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10476 
10477 	  sec = NULL;
10478 	  if (h->root.type == bfd_link_hash_defined
10479 	      || h->root.type == bfd_link_hash_defweak)
10480 	    sec = h->root.u.def.section;
10481 	}
10482 
10483       if (sec != NULL && discarded_section (sec))
10484 	{
10485 	  mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10486 						input_section, &rel, &relend,
10487 						rel_reloc, howto, contents);
10488 	  continue;
10489 	}
10490 
10491       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10492 	{
10493 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
10494 	     64-bit code, but make sure all their addresses are in the
10495 	     lowermost or uppermost 32-bit section of the 64-bit address
10496 	     space.  Thus, when they use an R_MIPS_64 they mean what is
10497 	     usually meant by R_MIPS_32, with the exception that the
10498 	     stored value is sign-extended to 64 bits.  */
10499 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10500 
10501 	  /* On big-endian systems, we need to lie about the position
10502 	     of the reloc.  */
10503 	  if (bfd_big_endian (input_bfd))
10504 	    rel->r_offset += 4;
10505 	}
10506 
10507       if (!use_saved_addend_p)
10508 	{
10509 	  /* If these relocations were originally of the REL variety,
10510 	     we must pull the addend out of the field that will be
10511 	     relocated.  Otherwise, we simply use the contents of the
10512 	     RELA relocation.  */
10513 	  if (mips_elf_rel_relocation_p (input_bfd, input_section,
10514 					 relocs, rel))
10515 	    {
10516 	      rela_relocation_p = false;
10517 	      addend = mips_elf_read_rel_addend (input_bfd, input_section,
10518 						 rel, howto, contents);
10519 	      if (hi16_reloc_p (r_type)
10520 		  || (got16_reloc_p (r_type)
10521 		      && mips_elf_local_relocation_p (input_bfd, rel,
10522 						      local_sections)))
10523 		{
10524 		  if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10525 						     rel, relend,
10526 						     contents, &addend))
10527 		    {
10528 		      if (h)
10529 			name = h->root.root.string;
10530 		      else
10531 			name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10532 						 local_syms + r_symndx,
10533 						 sec);
10534 		      _bfd_error_handler
10535 			/* xgettext:c-format */
10536 			(_("%pB: can't find matching LO16 reloc against `%s'"
10537 			   " for %s at %#" PRIx64 " in section `%pA'"),
10538 			 input_bfd, name,
10539 			 howto->name, (uint64_t) rel->r_offset, input_section);
10540 		    }
10541 		}
10542 	      else
10543 		addend <<= howto->rightshift;
10544 	    }
10545 	  else
10546 	    addend = rel->r_addend;
10547 	  mips_elf_adjust_addend (output_bfd, info, input_bfd,
10548 				  local_syms, local_sections, rel);
10549 	}
10550 
10551       if (bfd_link_relocatable (info))
10552 	{
10553 	  if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10554 	      && bfd_big_endian (input_bfd))
10555 	    rel->r_offset -= 4;
10556 
10557 	  if (!rela_relocation_p && rel->r_addend)
10558 	    {
10559 	      addend += rel->r_addend;
10560 	      if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10561 		addend = mips_elf_high (addend);
10562 	      else if (r_type == R_MIPS_HIGHER)
10563 		addend = mips_elf_higher (addend);
10564 	      else if (r_type == R_MIPS_HIGHEST)
10565 		addend = mips_elf_highest (addend);
10566 	      else
10567 		addend >>= howto->rightshift;
10568 
10569 	      /* We use the source mask, rather than the destination
10570 		 mask because the place to which we are writing will be
10571 		 source of the addend in the final link.  */
10572 	      addend &= howto->src_mask;
10573 
10574 	      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10575 		/* See the comment above about using R_MIPS_64 in the 32-bit
10576 		   ABI.  Here, we need to update the addend.  It would be
10577 		   possible to get away with just using the R_MIPS_32 reloc
10578 		   but for endianness.  */
10579 		{
10580 		  bfd_vma sign_bits;
10581 		  bfd_vma low_bits;
10582 		  bfd_vma high_bits;
10583 
10584 		  if (addend & ((bfd_vma) 1 << 31))
10585 #ifdef BFD64
10586 		    sign_bits = ((bfd_vma) 1 << 32) - 1;
10587 #else
10588 		    sign_bits = -1;
10589 #endif
10590 		  else
10591 		    sign_bits = 0;
10592 
10593 		  /* If we don't know that we have a 64-bit type,
10594 		     do two separate stores.  */
10595 		  if (bfd_big_endian (input_bfd))
10596 		    {
10597 		      /* Store the sign-bits (which are most significant)
10598 			 first.  */
10599 		      low_bits = sign_bits;
10600 		      high_bits = addend;
10601 		    }
10602 		  else
10603 		    {
10604 		      low_bits = addend;
10605 		      high_bits = sign_bits;
10606 		    }
10607 		  bfd_put_32 (input_bfd, low_bits,
10608 			      contents + rel->r_offset);
10609 		  bfd_put_32 (input_bfd, high_bits,
10610 			      contents + rel->r_offset + 4);
10611 		  continue;
10612 		}
10613 
10614 	      if (! mips_elf_perform_relocation (info, howto, rel, addend,
10615 						 input_bfd, input_section,
10616 						 contents, false))
10617 		return false;
10618 	    }
10619 
10620 	  /* Go on to the next relocation.  */
10621 	  continue;
10622 	}
10623 
10624       /* In the N32 and 64-bit ABIs there may be multiple consecutive
10625 	 relocations for the same offset.  In that case we are
10626 	 supposed to treat the output of each relocation as the addend
10627 	 for the next.  */
10628       if (rel + 1 < relend
10629 	  && rel->r_offset == rel[1].r_offset
10630 	  && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10631 	use_saved_addend_p = true;
10632       else
10633 	use_saved_addend_p = false;
10634 
10635       /* Figure out what value we are supposed to relocate.  */
10636       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10637 					     input_section, contents,
10638 					     info, rel, addend, howto,
10639 					     local_syms, local_sections,
10640 					     &value, &name, &cross_mode_jump_p,
10641 					     use_saved_addend_p))
10642 	{
10643 	case bfd_reloc_continue:
10644 	  /* There's nothing to do.  */
10645 	  continue;
10646 
10647 	case bfd_reloc_undefined:
10648 	  /* mips_elf_calculate_relocation already called the
10649 	     undefined_symbol callback.  There's no real point in
10650 	     trying to perform the relocation at this point, so we
10651 	     just skip ahead to the next relocation.  */
10652 	  continue;
10653 
10654 	case bfd_reloc_notsupported:
10655 	  msg = _("internal error: unsupported relocation error");
10656 	  info->callbacks->warning
10657 	    (info, msg, name, input_bfd, input_section, rel->r_offset);
10658 	  return false;
10659 
10660 	case bfd_reloc_overflow:
10661 	  if (use_saved_addend_p)
10662 	    /* Ignore overflow until we reach the last relocation for
10663 	       a given location.  */
10664 	    ;
10665 	  else
10666 	    {
10667 	      struct mips_elf_link_hash_table *htab;
10668 
10669 	      htab = mips_elf_hash_table (info);
10670 	      BFD_ASSERT (htab != NULL);
10671 	      BFD_ASSERT (name != NULL);
10672 	      if (!htab->small_data_overflow_reported
10673 		  && (gprel16_reloc_p (howto->type)
10674 		      || literal_reloc_p (howto->type)))
10675 		{
10676 		  msg = _("small-data section too large;"
10677 			  " lower small-data size limit (see option -G)");
10678 
10679 		  htab->small_data_overflow_reported = true;
10680 		  (*info->callbacks->einfo) ("%P: %s\n", msg);
10681 		}
10682 	      (*info->callbacks->reloc_overflow)
10683 		(info, NULL, name, howto->name, (bfd_vma) 0,
10684 		 input_bfd, input_section, rel->r_offset);
10685 	    }
10686 	  break;
10687 
10688 	case bfd_reloc_ok:
10689 	  break;
10690 
10691 	case bfd_reloc_outofrange:
10692 	  msg = NULL;
10693 	  if (jal_reloc_p (howto->type))
10694 	    msg = (cross_mode_jump_p
10695 		   ? _("cannot convert a jump to JALX "
10696 		       "for a non-word-aligned address")
10697 		   : (howto->type == R_MIPS16_26
10698 		      ? _("jump to a non-word-aligned address")
10699 		      : _("jump to a non-instruction-aligned address")));
10700 	  else if (b_reloc_p (howto->type))
10701 	    msg = (cross_mode_jump_p
10702 		   ? _("cannot convert a branch to JALX "
10703 		       "for a non-word-aligned address")
10704 		   : _("branch to a non-instruction-aligned address"));
10705 	  else if (aligned_pcrel_reloc_p (howto->type))
10706 	    msg = _("PC-relative load from unaligned address");
10707 	  if (msg)
10708 	    {
10709 	      info->callbacks->einfo
10710 		("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10711 	      break;
10712 	    }
10713 	  /* Fall through.  */
10714 
10715 	default:
10716 	  abort ();
10717 	  break;
10718 	}
10719 
10720       /* If we've got another relocation for the address, keep going
10721 	 until we reach the last one.  */
10722       if (use_saved_addend_p)
10723 	{
10724 	  addend = value;
10725 	  continue;
10726 	}
10727 
10728       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10729 	/* See the comment above about using R_MIPS_64 in the 32-bit
10730 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
10731 	   that calculated the right value.  Now, however, we
10732 	   sign-extend the 32-bit result to 64-bits, and store it as a
10733 	   64-bit value.  We are especially generous here in that we
10734 	   go to extreme lengths to support this usage on systems with
10735 	   only a 32-bit VMA.  */
10736 	{
10737 	  bfd_vma sign_bits;
10738 	  bfd_vma low_bits;
10739 	  bfd_vma high_bits;
10740 
10741 	  if (value & ((bfd_vma) 1 << 31))
10742 #ifdef BFD64
10743 	    sign_bits = ((bfd_vma) 1 << 32) - 1;
10744 #else
10745 	    sign_bits = -1;
10746 #endif
10747 	  else
10748 	    sign_bits = 0;
10749 
10750 	  /* If we don't know that we have a 64-bit type,
10751 	     do two separate stores.  */
10752 	  if (bfd_big_endian (input_bfd))
10753 	    {
10754 	      /* Undo what we did above.  */
10755 	      rel->r_offset -= 4;
10756 	      /* Store the sign-bits (which are most significant)
10757 		 first.  */
10758 	      low_bits = sign_bits;
10759 	      high_bits = value;
10760 	    }
10761 	  else
10762 	    {
10763 	      low_bits = value;
10764 	      high_bits = sign_bits;
10765 	    }
10766 	  bfd_put_32 (input_bfd, low_bits,
10767 		      contents + rel->r_offset);
10768 	  bfd_put_32 (input_bfd, high_bits,
10769 		      contents + rel->r_offset + 4);
10770 	  continue;
10771 	}
10772 
10773       /* Actually perform the relocation.  */
10774       if (! mips_elf_perform_relocation (info, howto, rel, value,
10775 					 input_bfd, input_section,
10776 					 contents, cross_mode_jump_p))
10777 	return false;
10778     }
10779 
10780   return true;
10781 }
10782 
10783 /* A function that iterates over each entry in la25_stubs and fills
10784    in the code for each one.  DATA points to a mips_htab_traverse_info.  */
10785 
10786 static int
10787 mips_elf_create_la25_stub (void **slot, void *data)
10788 {
10789   struct mips_htab_traverse_info *hti;
10790   struct mips_elf_link_hash_table *htab;
10791   struct mips_elf_la25_stub *stub;
10792   asection *s;
10793   bfd_byte *loc;
10794   bfd_vma offset, target, target_high, target_low;
10795   bfd_vma branch_pc;
10796   bfd_signed_vma pcrel_offset = 0;
10797 
10798   stub = (struct mips_elf_la25_stub *) *slot;
10799   hti = (struct mips_htab_traverse_info *) data;
10800   htab = mips_elf_hash_table (hti->info);
10801   BFD_ASSERT (htab != NULL);
10802 
10803   /* Create the section contents, if we haven't already.  */
10804   s = stub->stub_section;
10805   loc = s->contents;
10806   if (loc == NULL)
10807     {
10808       loc = bfd_malloc (s->size);
10809       if (loc == NULL)
10810 	{
10811 	  hti->error = true;
10812 	  return false;
10813 	}
10814       s->contents = loc;
10815     }
10816 
10817   /* Work out where in the section this stub should go.  */
10818   offset = stub->offset;
10819 
10820   /* We add 8 here to account for the LUI/ADDIU instructions
10821      before the branch instruction.  This cannot be moved down to
10822      where pcrel_offset is calculated as 's' is updated in
10823      mips_elf_get_la25_target.  */
10824   branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10825 
10826   /* Work out the target address.  */
10827   target = mips_elf_get_la25_target (stub, &s);
10828   target += s->output_section->vma + s->output_offset;
10829 
10830   target_high = ((target + 0x8000) >> 16) & 0xffff;
10831   target_low = (target & 0xffff);
10832 
10833   /* Calculate the PC of the compact branch instruction (for the case where
10834      compact branches are used for either microMIPSR6 or MIPSR6 with
10835      compact branches.  Add 4-bytes to account for BC using the PC of the
10836      next instruction as the base.  */
10837   pcrel_offset = target - (branch_pc + 4);
10838 
10839   if (stub->stub_section != htab->strampoline)
10840     {
10841       /* This is a simple LUI/ADDIU stub.  Zero out the beginning
10842 	 of the section and write the two instructions at the end.  */
10843       memset (loc, 0, offset);
10844       loc += offset;
10845       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10846 	{
10847 	  bfd_put_micromips_32 (hti->output_bfd,
10848 				LA25_LUI_MICROMIPS (target_high),
10849 				loc);
10850 	  bfd_put_micromips_32 (hti->output_bfd,
10851 				LA25_ADDIU_MICROMIPS (target_low),
10852 				loc + 4);
10853 	}
10854       else
10855 	{
10856 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10857 	  bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10858 	}
10859     }
10860   else
10861     {
10862       /* This is trampoline.  */
10863       loc += offset;
10864       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10865 	{
10866 	  bfd_put_micromips_32 (hti->output_bfd,
10867 				LA25_LUI_MICROMIPS (target_high), loc);
10868 	  bfd_put_micromips_32 (hti->output_bfd,
10869 				LA25_J_MICROMIPS (target), loc + 4);
10870 	  bfd_put_micromips_32 (hti->output_bfd,
10871 				LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10872 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
10873 	}
10874       else
10875 	{
10876 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10877 	  if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10878 	    {
10879 	      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10880 	      bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10881 	    }
10882 	  else
10883 	    {
10884 	      bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10885 	      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10886 	    }
10887 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
10888 	}
10889     }
10890   return true;
10891 }
10892 
10893 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10894    adjust it appropriately now.  */
10895 
10896 static void
10897 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10898 				      const char *name, Elf_Internal_Sym *sym)
10899 {
10900   /* The linker script takes care of providing names and values for
10901      these, but we must place them into the right sections.  */
10902   static const char* const text_section_symbols[] = {
10903     "_ftext",
10904     "_etext",
10905     "__dso_displacement",
10906     "__elf_header",
10907     "__program_header_table",
10908     NULL
10909   };
10910 
10911   static const char* const data_section_symbols[] = {
10912     "_fdata",
10913     "_edata",
10914     "_end",
10915     "_fbss",
10916     NULL
10917   };
10918 
10919   const char* const *p;
10920   int i;
10921 
10922   for (i = 0; i < 2; ++i)
10923     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10924 	 *p;
10925 	 ++p)
10926       if (strcmp (*p, name) == 0)
10927 	{
10928 	  /* All of these symbols are given type STT_SECTION by the
10929 	     IRIX6 linker.  */
10930 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10931 	  sym->st_other = STO_PROTECTED;
10932 
10933 	  /* The IRIX linker puts these symbols in special sections.  */
10934 	  if (i == 0)
10935 	    sym->st_shndx = SHN_MIPS_TEXT;
10936 	  else
10937 	    sym->st_shndx = SHN_MIPS_DATA;
10938 
10939 	  break;
10940 	}
10941 }
10942 
10943 /* Finish up dynamic symbol handling.  We set the contents of various
10944    dynamic sections here.  */
10945 
10946 bool
10947 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10948 				     struct bfd_link_info *info,
10949 				     struct elf_link_hash_entry *h,
10950 				     Elf_Internal_Sym *sym)
10951 {
10952   bfd *dynobj;
10953   asection *sgot;
10954   struct mips_got_info *g, *gg;
10955   const char *name;
10956   int idx;
10957   struct mips_elf_link_hash_table *htab;
10958   struct mips_elf_link_hash_entry *hmips;
10959 
10960   htab = mips_elf_hash_table (info);
10961   BFD_ASSERT (htab != NULL);
10962   dynobj = elf_hash_table (info)->dynobj;
10963   hmips = (struct mips_elf_link_hash_entry *) h;
10964 
10965   BFD_ASSERT (htab->root.target_os != is_vxworks);
10966 
10967   if (h->plt.plist != NULL
10968       && (h->plt.plist->mips_offset != MINUS_ONE
10969 	  || h->plt.plist->comp_offset != MINUS_ONE))
10970     {
10971       /* We've decided to create a PLT entry for this symbol.  */
10972       bfd_byte *loc;
10973       bfd_vma header_address, got_address;
10974       bfd_vma got_address_high, got_address_low, load;
10975       bfd_vma got_index;
10976       bfd_vma isa_bit;
10977 
10978       got_index = h->plt.plist->gotplt_index;
10979 
10980       BFD_ASSERT (htab->use_plts_and_copy_relocs);
10981       BFD_ASSERT (h->dynindx != -1);
10982       BFD_ASSERT (htab->root.splt != NULL);
10983       BFD_ASSERT (got_index != MINUS_ONE);
10984       BFD_ASSERT (!h->def_regular);
10985 
10986       /* Calculate the address of the PLT header.  */
10987       isa_bit = htab->plt_header_is_comp;
10988       header_address = (htab->root.splt->output_section->vma
10989 			+ htab->root.splt->output_offset + isa_bit);
10990 
10991       /* Calculate the address of the .got.plt entry.  */
10992       got_address = (htab->root.sgotplt->output_section->vma
10993 		     + htab->root.sgotplt->output_offset
10994 		     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10995 
10996       got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10997       got_address_low = got_address & 0xffff;
10998 
10999       /* The PLT sequence is not safe for N64 if .got.plt entry's address
11000 	 cannot be loaded in two instructions.  */
11001       if (ABI_64_P (output_bfd)
11002 	  && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11003 	{
11004 	  _bfd_error_handler
11005 	    /* xgettext:c-format */
11006 	    (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11007 	       "supported; consider using `-Ttext-segment=...'"),
11008 	     output_bfd,
11009 	     htab->root.sgotplt->output_section,
11010 	     (int64_t) got_address);
11011 	  bfd_set_error (bfd_error_no_error);
11012 	  return false;
11013 	}
11014 
11015       /* Initially point the .got.plt entry at the PLT header.  */
11016       loc = (htab->root.sgotplt->contents
11017 	     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11018       if (ABI_64_P (output_bfd))
11019 	bfd_put_64 (output_bfd, header_address, loc);
11020       else
11021 	bfd_put_32 (output_bfd, header_address, loc);
11022 
11023       /* Now handle the PLT itself.  First the standard entry (the order
11024 	 does not matter, we just have to pick one).  */
11025       if (h->plt.plist->mips_offset != MINUS_ONE)
11026 	{
11027 	  const bfd_vma *plt_entry;
11028 	  bfd_vma plt_offset;
11029 
11030 	  plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11031 
11032 	  BFD_ASSERT (plt_offset <= htab->root.splt->size);
11033 
11034 	  /* Find out where the .plt entry should go.  */
11035 	  loc = htab->root.splt->contents + plt_offset;
11036 
11037 	  /* Pick the load opcode.  */
11038 	  load = MIPS_ELF_LOAD_WORD (output_bfd);
11039 
11040 	  /* Fill in the PLT entry itself.  */
11041 
11042 	  if (MIPSR6_P (output_bfd))
11043 	    plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11044 					       : mipsr6_exec_plt_entry;
11045 	  else
11046 	    plt_entry = mips_exec_plt_entry;
11047 	  bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11048 	  bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11049 		      loc + 4);
11050 
11051 	  if (! LOAD_INTERLOCKS_P (output_bfd)
11052 	      || (MIPSR6_P (output_bfd) && htab->compact_branches))
11053 	    {
11054 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11055 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11056 	    }
11057 	  else
11058 	    {
11059 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11060 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11061 			  loc + 12);
11062 	    }
11063 	}
11064 
11065       /* Now the compressed entry.  They come after any standard ones.  */
11066       if (h->plt.plist->comp_offset != MINUS_ONE)
11067 	{
11068 	  bfd_vma plt_offset;
11069 
11070 	  plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11071 			+ h->plt.plist->comp_offset);
11072 
11073 	  BFD_ASSERT (plt_offset <= htab->root.splt->size);
11074 
11075 	  /* Find out where the .plt entry should go.  */
11076 	  loc = htab->root.splt->contents + plt_offset;
11077 
11078 	  /* Fill in the PLT entry itself.  */
11079 	  if (!MICROMIPS_P (output_bfd))
11080 	    {
11081 	      const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11082 
11083 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
11084 	      bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11085 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11086 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11087 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11088 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11089 	      bfd_put_32 (output_bfd, got_address, loc + 12);
11090 	    }
11091 	  else if (htab->insn32)
11092 	    {
11093 	      const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11094 
11095 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
11096 	      bfd_put_16 (output_bfd, got_address_high, loc + 2);
11097 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11098 	      bfd_put_16 (output_bfd, got_address_low, loc + 6);
11099 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11100 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11101 	      bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11102 	      bfd_put_16 (output_bfd, got_address_low, loc + 14);
11103 	    }
11104 	  else
11105 	    {
11106 	      const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11107 	      bfd_signed_vma gotpc_offset;
11108 	      bfd_vma loc_address;
11109 
11110 	      BFD_ASSERT (got_address % 4 == 0);
11111 
11112 	      loc_address = (htab->root.splt->output_section->vma
11113 			     + htab->root.splt->output_offset + plt_offset);
11114 	      gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11115 
11116 	      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11117 	      if (gotpc_offset + 0x1000000 >= 0x2000000)
11118 		{
11119 		  _bfd_error_handler
11120 		    /* xgettext:c-format */
11121 		    (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11122 		       "beyond the range of ADDIUPC"),
11123 		     output_bfd,
11124 		     htab->root.sgotplt->output_section,
11125 		     (int64_t) gotpc_offset,
11126 		     htab->root.splt->output_section);
11127 		  bfd_set_error (bfd_error_no_error);
11128 		  return false;
11129 		}
11130 	      bfd_put_16 (output_bfd,
11131 			  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11132 	      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11133 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11134 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11135 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11136 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11137 	    }
11138 	}
11139 
11140       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11141       mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11142 					  got_index - 2, h->dynindx,
11143 					  R_MIPS_JUMP_SLOT, got_address);
11144 
11145       /* We distinguish between PLT entries and lazy-binding stubs by
11146 	 giving the former an st_other value of STO_MIPS_PLT.  Set the
11147 	 flag and leave the value if there are any relocations in the
11148 	 binary where pointer equality matters.  */
11149       sym->st_shndx = SHN_UNDEF;
11150       if (h->pointer_equality_needed)
11151 	sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11152       else
11153 	{
11154 	  sym->st_value = 0;
11155 	  sym->st_other = 0;
11156 	}
11157     }
11158 
11159   if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11160     {
11161       /* We've decided to create a lazy-binding stub.  */
11162       bool micromips_p = MICROMIPS_P (output_bfd);
11163       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11164       bfd_vma stub_size = htab->function_stub_size;
11165       bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11166       bfd_vma isa_bit = micromips_p;
11167       bfd_vma stub_big_size;
11168 
11169       if (!micromips_p)
11170 	stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11171       else if (htab->insn32)
11172 	stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11173       else
11174 	stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11175 
11176       /* This symbol has a stub.  Set it up.  */
11177 
11178       BFD_ASSERT (h->dynindx != -1);
11179 
11180       BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11181 
11182       /* Values up to 2^31 - 1 are allowed.  Larger values would cause
11183 	 sign extension at runtime in the stub, resulting in a negative
11184 	 index value.  */
11185       if (h->dynindx & ~0x7fffffff)
11186 	{
11187 	  _bfd_error_handler
11188 	    (_("%pB: cannot handle more than %d dynamic symbols"),
11189 	     output_bfd, 0x7fffffff);
11190 	  bfd_set_error (bfd_error_bad_value);
11191 	  return false;
11192 	}
11193 
11194       /* Fill the stub.  */
11195       if (micromips_p)
11196 	{
11197 	  idx = 0;
11198 	  bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11199 				stub + idx);
11200 	  idx += 4;
11201 	  if (htab->insn32)
11202 	    {
11203 	      bfd_put_micromips_32 (output_bfd,
11204 				    STUB_MOVE32_MICROMIPS, stub + idx);
11205 	      idx += 4;
11206 	    }
11207 	  else
11208 	    {
11209 	      bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11210 	      idx += 2;
11211 	    }
11212 	  if (stub_size == stub_big_size)
11213 	    {
11214 	      long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11215 
11216 	      bfd_put_micromips_32 (output_bfd,
11217 				    STUB_LUI_MICROMIPS (dynindx_hi),
11218 				    stub + idx);
11219 	      idx += 4;
11220 	    }
11221 	  if (htab->insn32)
11222 	    {
11223 	      bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11224 				    stub + idx);
11225 	      idx += 4;
11226 	    }
11227 	  else
11228 	    {
11229 	      bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11230 	      idx += 2;
11231 	    }
11232 
11233 	  /* If a large stub is not required and sign extension is not a
11234 	     problem, then use legacy code in the stub.  */
11235 	  if (stub_size == stub_big_size)
11236 	    bfd_put_micromips_32 (output_bfd,
11237 				  STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11238 				  stub + idx);
11239 	  else if (h->dynindx & ~0x7fff)
11240 	    bfd_put_micromips_32 (output_bfd,
11241 				  STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11242 				  stub + idx);
11243 	  else
11244 	    bfd_put_micromips_32 (output_bfd,
11245 				  STUB_LI16S_MICROMIPS (output_bfd,
11246 							h->dynindx),
11247 				  stub + idx);
11248 	}
11249       else
11250 	{
11251 	  idx = 0;
11252 	  bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11253 	  idx += 4;
11254 	  bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11255 	  idx += 4;
11256 	  if (stub_size == stub_big_size)
11257 	    {
11258 	      bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11259 			  stub + idx);
11260 	      idx += 4;
11261 	    }
11262 
11263 	  if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11264 	    {
11265 	      bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11266 	      idx += 4;
11267 	    }
11268 
11269 	  /* If a large stub is not required and sign extension is not a
11270 	     problem, then use legacy code in the stub.  */
11271 	  if (stub_size == stub_big_size)
11272 	    bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11273 			stub + idx);
11274 	  else if (h->dynindx & ~0x7fff)
11275 	    bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11276 			stub + idx);
11277 	  else
11278 	    bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11279 			stub + idx);
11280 	  idx += 4;
11281 
11282 	  if (MIPSR6_P (output_bfd) && htab->compact_branches)
11283 	    bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11284 	}
11285 
11286       BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11287       memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11288 	      stub, stub_size);
11289 
11290       /* Mark the symbol as undefined.  stub_offset != -1 occurs
11291 	 only for the referenced symbol.  */
11292       sym->st_shndx = SHN_UNDEF;
11293 
11294       /* The run-time linker uses the st_value field of the symbol
11295 	 to reset the global offset table entry for this external
11296 	 to its stub address when unlinking a shared object.  */
11297       sym->st_value = (htab->sstubs->output_section->vma
11298 		       + htab->sstubs->output_offset
11299 		       + h->plt.plist->stub_offset
11300 		       + isa_bit);
11301       sym->st_other = other;
11302     }
11303 
11304   /* If we have a MIPS16 function with a stub, the dynamic symbol must
11305      refer to the stub, since only the stub uses the standard calling
11306      conventions.  */
11307   if (h->dynindx != -1 && hmips->fn_stub != NULL)
11308     {
11309       BFD_ASSERT (hmips->need_fn_stub);
11310       sym->st_value = (hmips->fn_stub->output_section->vma
11311 		       + hmips->fn_stub->output_offset);
11312       sym->st_size = hmips->fn_stub->size;
11313       sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11314     }
11315 
11316   BFD_ASSERT (h->dynindx != -1
11317 	      || h->forced_local);
11318 
11319   sgot = htab->root.sgot;
11320   g = htab->got_info;
11321   BFD_ASSERT (g != NULL);
11322 
11323   /* Run through the global symbol table, creating GOT entries for all
11324      the symbols that need them.  */
11325   if (hmips->global_got_area != GGA_NONE)
11326     {
11327       bfd_vma offset;
11328       bfd_vma value;
11329 
11330       value = sym->st_value;
11331       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11332       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11333     }
11334 
11335   if (hmips->global_got_area != GGA_NONE && g->next)
11336     {
11337       struct mips_got_entry e, *p;
11338       bfd_vma entry;
11339       bfd_vma offset;
11340 
11341       gg = g;
11342 
11343       e.abfd = output_bfd;
11344       e.symndx = -1;
11345       e.d.h = hmips;
11346       e.tls_type = GOT_TLS_NONE;
11347 
11348       for (g = g->next; g->next != gg; g = g->next)
11349 	{
11350 	  if (g->got_entries
11351 	      && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11352 							   &e)))
11353 	    {
11354 	      offset = p->gotidx;
11355 	      BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11356 	      if (bfd_link_pic (info)
11357 		  || (elf_hash_table (info)->dynamic_sections_created
11358 		      && p->d.h != NULL
11359 		      && p->d.h->root.def_dynamic
11360 		      && !p->d.h->root.def_regular))
11361 		{
11362 		  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
11363 		     the various compatibility problems, it's easier to mock
11364 		     up an R_MIPS_32 or R_MIPS_64 relocation and leave
11365 		     mips_elf_create_dynamic_relocation to calculate the
11366 		     appropriate addend.  */
11367 		  Elf_Internal_Rela rel[3];
11368 
11369 		  memset (rel, 0, sizeof (rel));
11370 		  if (ABI_64_P (output_bfd))
11371 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11372 		  else
11373 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11374 		  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11375 
11376 		  entry = 0;
11377 		  if (! (mips_elf_create_dynamic_relocation
11378 			 (output_bfd, info, rel,
11379 			  e.d.h, NULL, sym->st_value, &entry, sgot)))
11380 		    return false;
11381 		}
11382 	      else
11383 		entry = sym->st_value;
11384 	      MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11385 	    }
11386 	}
11387     }
11388 
11389   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
11390   name = h->root.root.string;
11391   if (h == elf_hash_table (info)->hdynamic
11392       || h == elf_hash_table (info)->hgot)
11393     sym->st_shndx = SHN_ABS;
11394   else if (strcmp (name, "_DYNAMIC_LINK") == 0
11395 	   || strcmp (name, "_DYNAMIC_LINKING") == 0)
11396     {
11397       sym->st_shndx = SHN_ABS;
11398       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11399       sym->st_value = 1;
11400     }
11401   else if (SGI_COMPAT (output_bfd))
11402     {
11403       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11404 	  || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11405 	{
11406 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11407 	  sym->st_other = STO_PROTECTED;
11408 	  sym->st_value = 0;
11409 	  sym->st_shndx = SHN_MIPS_DATA;
11410 	}
11411       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11412 	{
11413 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11414 	  sym->st_other = STO_PROTECTED;
11415 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
11416 	  sym->st_shndx = SHN_ABS;
11417 	}
11418       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11419 	{
11420 	  if (h->type == STT_FUNC)
11421 	    sym->st_shndx = SHN_MIPS_TEXT;
11422 	  else if (h->type == STT_OBJECT)
11423 	    sym->st_shndx = SHN_MIPS_DATA;
11424 	}
11425     }
11426 
11427   /* Emit a copy reloc, if needed.  */
11428   if (h->needs_copy)
11429     {
11430       asection *s;
11431       bfd_vma symval;
11432 
11433       BFD_ASSERT (h->dynindx != -1);
11434       BFD_ASSERT (htab->use_plts_and_copy_relocs);
11435 
11436       s = mips_elf_rel_dyn_section (info, false);
11437       symval = (h->root.u.def.section->output_section->vma
11438 		+ h->root.u.def.section->output_offset
11439 		+ h->root.u.def.value);
11440       mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11441 					  h->dynindx, R_MIPS_COPY, symval);
11442     }
11443 
11444   /* Handle the IRIX6-specific symbols.  */
11445   if (IRIX_COMPAT (output_bfd) == ict_irix6)
11446     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11447 
11448   /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
11449      to treat compressed symbols like any other.  */
11450   if (ELF_ST_IS_MIPS16 (sym->st_other))
11451     {
11452       BFD_ASSERT (sym->st_value & 1);
11453       sym->st_other -= STO_MIPS16;
11454     }
11455   else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11456     {
11457       BFD_ASSERT (sym->st_value & 1);
11458       sym->st_other -= STO_MICROMIPS;
11459     }
11460 
11461   return true;
11462 }
11463 
11464 /* Likewise, for VxWorks.  */
11465 
11466 bool
11467 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11468 					 struct bfd_link_info *info,
11469 					 struct elf_link_hash_entry *h,
11470 					 Elf_Internal_Sym *sym)
11471 {
11472   bfd *dynobj;
11473   asection *sgot;
11474   struct mips_got_info *g;
11475   struct mips_elf_link_hash_table *htab;
11476   struct mips_elf_link_hash_entry *hmips;
11477 
11478   htab = mips_elf_hash_table (info);
11479   BFD_ASSERT (htab != NULL);
11480   dynobj = elf_hash_table (info)->dynobj;
11481   hmips = (struct mips_elf_link_hash_entry *) h;
11482 
11483   if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11484     {
11485       bfd_byte *loc;
11486       bfd_vma plt_address, got_address, got_offset, branch_offset;
11487       Elf_Internal_Rela rel;
11488       static const bfd_vma *plt_entry;
11489       bfd_vma gotplt_index;
11490       bfd_vma plt_offset;
11491 
11492       plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11493       gotplt_index = h->plt.plist->gotplt_index;
11494 
11495       BFD_ASSERT (h->dynindx != -1);
11496       BFD_ASSERT (htab->root.splt != NULL);
11497       BFD_ASSERT (gotplt_index != MINUS_ONE);
11498       BFD_ASSERT (plt_offset <= htab->root.splt->size);
11499 
11500       /* Calculate the address of the .plt entry.  */
11501       plt_address = (htab->root.splt->output_section->vma
11502 		     + htab->root.splt->output_offset
11503 		     + plt_offset);
11504 
11505       /* Calculate the address of the .got.plt entry.  */
11506       got_address = (htab->root.sgotplt->output_section->vma
11507 		     + htab->root.sgotplt->output_offset
11508 		     + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11509 
11510       /* Calculate the offset of the .got.plt entry from
11511 	 _GLOBAL_OFFSET_TABLE_.  */
11512       got_offset = mips_elf_gotplt_index (info, h);
11513 
11514       /* Calculate the offset for the branch at the start of the PLT
11515 	 entry.  The branch jumps to the beginning of .plt.  */
11516       branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11517 
11518       /* Fill in the initial value of the .got.plt entry.  */
11519       bfd_put_32 (output_bfd, plt_address,
11520 		  (htab->root.sgotplt->contents
11521 		   + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11522 
11523       /* Find out where the .plt entry should go.  */
11524       loc = htab->root.splt->contents + plt_offset;
11525 
11526       if (bfd_link_pic (info))
11527 	{
11528 	  plt_entry = mips_vxworks_shared_plt_entry;
11529 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11530 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11531 	}
11532       else
11533 	{
11534 	  bfd_vma got_address_high, got_address_low;
11535 
11536 	  plt_entry = mips_vxworks_exec_plt_entry;
11537 	  got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11538 	  got_address_low = got_address & 0xffff;
11539 
11540 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11541 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11542 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11543 	  bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11544 	  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11545 	  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11546 	  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11547 	  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11548 
11549 	  loc = (htab->srelplt2->contents
11550 		 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11551 
11552 	  /* Emit a relocation for the .got.plt entry.  */
11553 	  rel.r_offset = got_address;
11554 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11555 	  rel.r_addend = plt_offset;
11556 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11557 
11558 	  /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
11559 	  loc += sizeof (Elf32_External_Rela);
11560 	  rel.r_offset = plt_address + 8;
11561 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11562 	  rel.r_addend = got_offset;
11563 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11564 
11565 	  /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
11566 	  loc += sizeof (Elf32_External_Rela);
11567 	  rel.r_offset += 4;
11568 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11569 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11570 	}
11571 
11572       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11573       loc = (htab->root.srelplt->contents
11574 	     + gotplt_index * sizeof (Elf32_External_Rela));
11575       rel.r_offset = got_address;
11576       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11577       rel.r_addend = 0;
11578       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11579 
11580       if (!h->def_regular)
11581 	sym->st_shndx = SHN_UNDEF;
11582     }
11583 
11584   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11585 
11586   sgot = htab->root.sgot;
11587   g = htab->got_info;
11588   BFD_ASSERT (g != NULL);
11589 
11590   /* See if this symbol has an entry in the GOT.  */
11591   if (hmips->global_got_area != GGA_NONE)
11592     {
11593       bfd_vma offset;
11594       Elf_Internal_Rela outrel;
11595       bfd_byte *loc;
11596       asection *s;
11597 
11598       /* Install the symbol value in the GOT.   */
11599       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11600       MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11601 
11602       /* Add a dynamic relocation for it.  */
11603       s = mips_elf_rel_dyn_section (info, false);
11604       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11605       outrel.r_offset = (sgot->output_section->vma
11606 			 + sgot->output_offset
11607 			 + offset);
11608       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11609       outrel.r_addend = 0;
11610       bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11611     }
11612 
11613   /* Emit a copy reloc, if needed.  */
11614   if (h->needs_copy)
11615     {
11616       Elf_Internal_Rela rel;
11617       asection *srel;
11618       bfd_byte *loc;
11619 
11620       BFD_ASSERT (h->dynindx != -1);
11621 
11622       rel.r_offset = (h->root.u.def.section->output_section->vma
11623 		      + h->root.u.def.section->output_offset
11624 		      + h->root.u.def.value);
11625       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11626       rel.r_addend = 0;
11627       if (h->root.u.def.section == htab->root.sdynrelro)
11628 	srel = htab->root.sreldynrelro;
11629       else
11630 	srel = htab->root.srelbss;
11631       loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11632       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11633       ++srel->reloc_count;
11634     }
11635 
11636   /* If this is a mips16/microMIPS symbol, force the value to be even.  */
11637   if (ELF_ST_IS_COMPRESSED (sym->st_other))
11638     sym->st_value &= ~1;
11639 
11640   return true;
11641 }
11642 
11643 /* Write out a plt0 entry to the beginning of .plt.  */
11644 
11645 static bool
11646 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11647 {
11648   bfd_byte *loc;
11649   bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11650   static const bfd_vma *plt_entry;
11651   struct mips_elf_link_hash_table *htab;
11652 
11653   htab = mips_elf_hash_table (info);
11654   BFD_ASSERT (htab != NULL);
11655 
11656   if (ABI_64_P (output_bfd))
11657     plt_entry = (htab->compact_branches
11658 		 ? mipsr6_n64_exec_plt0_entry_compact
11659 		 : mips_n64_exec_plt0_entry);
11660   else if (ABI_N32_P (output_bfd))
11661     plt_entry = (htab->compact_branches
11662 		 ? mipsr6_n32_exec_plt0_entry_compact
11663 		 : mips_n32_exec_plt0_entry);
11664   else if (!htab->plt_header_is_comp)
11665     plt_entry = (htab->compact_branches
11666 		 ? mipsr6_o32_exec_plt0_entry_compact
11667 		 : mips_o32_exec_plt0_entry);
11668   else if (htab->insn32)
11669     plt_entry = micromips_insn32_o32_exec_plt0_entry;
11670   else
11671     plt_entry = micromips_o32_exec_plt0_entry;
11672 
11673   /* Calculate the value of .got.plt.  */
11674   gotplt_value = (htab->root.sgotplt->output_section->vma
11675 		  + htab->root.sgotplt->output_offset);
11676   gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11677   gotplt_value_low = gotplt_value & 0xffff;
11678 
11679   /* The PLT sequence is not safe for N64 if .got.plt's address can
11680      not be loaded in two instructions.  */
11681   if (ABI_64_P (output_bfd)
11682       && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11683     {
11684       _bfd_error_handler
11685 	/* xgettext:c-format */
11686 	(_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11687 	   "supported; consider using `-Ttext-segment=...'"),
11688 	 output_bfd,
11689 	 htab->root.sgotplt->output_section,
11690 	 (int64_t) gotplt_value);
11691       bfd_set_error (bfd_error_no_error);
11692       return false;
11693     }
11694 
11695   /* Install the PLT header.  */
11696   loc = htab->root.splt->contents;
11697   if (plt_entry == micromips_o32_exec_plt0_entry)
11698     {
11699       bfd_vma gotpc_offset;
11700       bfd_vma loc_address;
11701       size_t i;
11702 
11703       BFD_ASSERT (gotplt_value % 4 == 0);
11704 
11705       loc_address = (htab->root.splt->output_section->vma
11706 		     + htab->root.splt->output_offset);
11707       gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11708 
11709       /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11710       if (gotpc_offset + 0x1000000 >= 0x2000000)
11711 	{
11712 	  _bfd_error_handler
11713 	    /* xgettext:c-format */
11714 	    (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11715 	       "beyond the range of ADDIUPC"),
11716 	     output_bfd,
11717 	     htab->root.sgotplt->output_section,
11718 	     (int64_t) gotpc_offset,
11719 	     htab->root.splt->output_section);
11720 	  bfd_set_error (bfd_error_no_error);
11721 	  return false;
11722 	}
11723       bfd_put_16 (output_bfd,
11724 		  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11725       bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11726       for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11727 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11728     }
11729   else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11730     {
11731       size_t i;
11732 
11733       bfd_put_16 (output_bfd, plt_entry[0], loc);
11734       bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11735       bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11736       bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11737       bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11738       bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11739       for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11740 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11741     }
11742   else
11743     {
11744       bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11745       bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11746       bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11747       bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11748       bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11749       bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11750       bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11751       bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11752     }
11753 
11754   return true;
11755 }
11756 
11757 /* Install the PLT header for a VxWorks executable and finalize the
11758    contents of .rela.plt.unloaded.  */
11759 
11760 static void
11761 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11762 {
11763   Elf_Internal_Rela rela;
11764   bfd_byte *loc;
11765   bfd_vma got_value, got_value_high, got_value_low, plt_address;
11766   static const bfd_vma *plt_entry;
11767   struct mips_elf_link_hash_table *htab;
11768 
11769   htab = mips_elf_hash_table (info);
11770   BFD_ASSERT (htab != NULL);
11771 
11772   plt_entry = mips_vxworks_exec_plt0_entry;
11773 
11774   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
11775   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11776 	       + htab->root.hgot->root.u.def.section->output_offset
11777 	       + htab->root.hgot->root.u.def.value);
11778 
11779   got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11780   got_value_low = got_value & 0xffff;
11781 
11782   /* Calculate the address of the PLT header.  */
11783   plt_address = (htab->root.splt->output_section->vma
11784 		 + htab->root.splt->output_offset);
11785 
11786   /* Install the PLT header.  */
11787   loc = htab->root.splt->contents;
11788   bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11789   bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11790   bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11791   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11792   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11793   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11794 
11795   /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
11796   loc = htab->srelplt2->contents;
11797   rela.r_offset = plt_address;
11798   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11799   rela.r_addend = 0;
11800   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11801   loc += sizeof (Elf32_External_Rela);
11802 
11803   /* Output the relocation for the following addiu of
11804      %lo(_GLOBAL_OFFSET_TABLE_).  */
11805   rela.r_offset += 4;
11806   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11807   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11808   loc += sizeof (Elf32_External_Rela);
11809 
11810   /* Fix up the remaining relocations.  They may have the wrong
11811      symbol index for _G_O_T_ or _P_L_T_ depending on the order
11812      in which symbols were output.  */
11813   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11814     {
11815       Elf_Internal_Rela rel;
11816 
11817       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11818       rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11819       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11820       loc += sizeof (Elf32_External_Rela);
11821 
11822       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11823       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11824       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11825       loc += sizeof (Elf32_External_Rela);
11826 
11827       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11828       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11829       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11830       loc += sizeof (Elf32_External_Rela);
11831     }
11832 }
11833 
11834 /* Install the PLT header for a VxWorks shared library.  */
11835 
11836 static void
11837 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11838 {
11839   unsigned int i;
11840   struct mips_elf_link_hash_table *htab;
11841 
11842   htab = mips_elf_hash_table (info);
11843   BFD_ASSERT (htab != NULL);
11844 
11845   /* We just need to copy the entry byte-by-byte.  */
11846   for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11847     bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11848 		htab->root.splt->contents + i * 4);
11849 }
11850 
11851 /* Finish up the dynamic sections.  */
11852 
11853 bool
11854 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11855 				       struct bfd_link_info *info)
11856 {
11857   bfd *dynobj;
11858   asection *sdyn;
11859   asection *sgot;
11860   struct mips_got_info *gg, *g;
11861   struct mips_elf_link_hash_table *htab;
11862 
11863   htab = mips_elf_hash_table (info);
11864   BFD_ASSERT (htab != NULL);
11865 
11866   dynobj = elf_hash_table (info)->dynobj;
11867 
11868   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11869 
11870   sgot = htab->root.sgot;
11871   gg = htab->got_info;
11872 
11873   if (elf_hash_table (info)->dynamic_sections_created)
11874     {
11875       bfd_byte *b;
11876       int dyn_to_skip = 0, dyn_skipped = 0;
11877 
11878       BFD_ASSERT (sdyn != NULL);
11879       BFD_ASSERT (gg != NULL);
11880 
11881       g = mips_elf_bfd_got (output_bfd, false);
11882       BFD_ASSERT (g != NULL);
11883 
11884       for (b = sdyn->contents;
11885 	   b < sdyn->contents + sdyn->size;
11886 	   b += MIPS_ELF_DYN_SIZE (dynobj))
11887 	{
11888 	  Elf_Internal_Dyn dyn;
11889 	  const char *name;
11890 	  size_t elemsize;
11891 	  asection *s;
11892 	  bool swap_out_p;
11893 
11894 	  /* Read in the current dynamic entry.  */
11895 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11896 
11897 	  /* Assume that we're going to modify it and write it out.  */
11898 	  swap_out_p = true;
11899 
11900 	  switch (dyn.d_tag)
11901 	    {
11902 	    case DT_RELENT:
11903 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11904 	      break;
11905 
11906 	    case DT_RELAENT:
11907 	      BFD_ASSERT (htab->root.target_os == is_vxworks);
11908 	      dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11909 	      break;
11910 
11911 	    case DT_STRSZ:
11912 	      /* Rewrite DT_STRSZ.  */
11913 	      dyn.d_un.d_val =
11914 		_bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11915 	      break;
11916 
11917 	    case DT_PLTGOT:
11918 	      s = htab->root.sgot;
11919 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11920 	      break;
11921 
11922 	    case DT_MIPS_PLTGOT:
11923 	      s = htab->root.sgotplt;
11924 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11925 	      break;
11926 
11927 	    case DT_MIPS_RLD_VERSION:
11928 	      dyn.d_un.d_val = 1; /* XXX */
11929 	      break;
11930 
11931 	    case DT_MIPS_FLAGS:
11932 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11933 	      break;
11934 
11935 	    case DT_MIPS_TIME_STAMP:
11936 	      {
11937 		time_t t;
11938 		time (&t);
11939 		dyn.d_un.d_val = t;
11940 	      }
11941 	      break;
11942 
11943 	    case DT_MIPS_ICHECKSUM:
11944 	      /* XXX FIXME: */
11945 	      swap_out_p = false;
11946 	      break;
11947 
11948 	    case DT_MIPS_IVERSION:
11949 	      /* XXX FIXME: */
11950 	      swap_out_p = false;
11951 	      break;
11952 
11953 	    case DT_MIPS_BASE_ADDRESS:
11954 	      s = output_bfd->sections;
11955 	      BFD_ASSERT (s != NULL);
11956 	      dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11957 	      break;
11958 
11959 	    case DT_MIPS_LOCAL_GOTNO:
11960 	      dyn.d_un.d_val = g->local_gotno;
11961 	      break;
11962 
11963 	    case DT_MIPS_UNREFEXTNO:
11964 	      /* The index into the dynamic symbol table which is the
11965 		 entry of the first external symbol that is not
11966 		 referenced within the same object.  */
11967 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11968 	      break;
11969 
11970 	    case DT_MIPS_GOTSYM:
11971 	      if (htab->global_gotsym)
11972 		{
11973 		  dyn.d_un.d_val = htab->global_gotsym->dynindx;
11974 		  break;
11975 		}
11976 	      /* In case if we don't have global got symbols we default
11977 		 to setting DT_MIPS_GOTSYM to the same value as
11978 		 DT_MIPS_SYMTABNO.  */
11979 	      /* Fall through.  */
11980 
11981 	    case DT_MIPS_SYMTABNO:
11982 	      name = ".dynsym";
11983 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11984 	      s = bfd_get_linker_section (dynobj, name);
11985 
11986 	      if (s != NULL)
11987 		dyn.d_un.d_val = s->size / elemsize;
11988 	      else
11989 		dyn.d_un.d_val = 0;
11990 	      break;
11991 
11992 	    case DT_MIPS_HIPAGENO:
11993 	      dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11994 	      break;
11995 
11996 	    case DT_MIPS_RLD_MAP:
11997 	      {
11998 		struct elf_link_hash_entry *h;
11999 		h = mips_elf_hash_table (info)->rld_symbol;
12000 		if (!h)
12001 		  {
12002 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12003 		    swap_out_p = false;
12004 		    break;
12005 		  }
12006 		s = h->root.u.def.section;
12007 
12008 		/* The MIPS_RLD_MAP tag stores the absolute address of the
12009 		   debug pointer.  */
12010 		dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12011 				  + h->root.u.def.value);
12012 	      }
12013 	      break;
12014 
12015 	    case DT_MIPS_RLD_MAP_REL:
12016 	      {
12017 		struct elf_link_hash_entry *h;
12018 		bfd_vma dt_addr, rld_addr;
12019 		h = mips_elf_hash_table (info)->rld_symbol;
12020 		if (!h)
12021 		  {
12022 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12023 		    swap_out_p = false;
12024 		    break;
12025 		  }
12026 		s = h->root.u.def.section;
12027 
12028 		/* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12029 		   pointer, relative to the address of the tag.  */
12030 		dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12031 			   + (b - sdyn->contents));
12032 		rld_addr = (s->output_section->vma + s->output_offset
12033 			    + h->root.u.def.value);
12034 		dyn.d_un.d_ptr = rld_addr - dt_addr;
12035 	      }
12036 	      break;
12037 
12038 	    case DT_MIPS_OPTIONS:
12039 	      s = (bfd_get_section_by_name
12040 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12041 	      dyn.d_un.d_ptr = s->vma;
12042 	      break;
12043 
12044 	    case DT_PLTREL:
12045 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
12046 	      if (htab->root.target_os == is_vxworks)
12047 		dyn.d_un.d_val = DT_RELA;
12048 	      else
12049 		dyn.d_un.d_val = DT_REL;
12050 	      break;
12051 
12052 	    case DT_PLTRELSZ:
12053 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
12054 	      dyn.d_un.d_val = htab->root.srelplt->size;
12055 	      break;
12056 
12057 	    case DT_JMPREL:
12058 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
12059 	      dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12060 				+ htab->root.srelplt->output_offset);
12061 	      break;
12062 
12063 	    case DT_TEXTREL:
12064 	      /* If we didn't need any text relocations after all, delete
12065 		 the dynamic tag.  */
12066 	      if (!(info->flags & DF_TEXTREL))
12067 		{
12068 		  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12069 		  swap_out_p = false;
12070 		}
12071 	      break;
12072 
12073 	    case DT_FLAGS:
12074 	      /* If we didn't need any text relocations after all, clear
12075 		 DF_TEXTREL from DT_FLAGS.  */
12076 	      if (!(info->flags & DF_TEXTREL))
12077 		dyn.d_un.d_val &= ~DF_TEXTREL;
12078 	      else
12079 		swap_out_p = false;
12080 	      break;
12081 
12082 	    case DT_MIPS_XHASH:
12083 	      name = ".MIPS.xhash";
12084 	      s = bfd_get_linker_section (dynobj, name);
12085 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12086 	      break;
12087 
12088 	    default:
12089 	      swap_out_p = false;
12090 	      if (htab->root.target_os == is_vxworks
12091 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12092 		swap_out_p = true;
12093 	      break;
12094 	    }
12095 
12096 	  if (swap_out_p || dyn_skipped)
12097 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12098 	      (dynobj, &dyn, b - dyn_skipped);
12099 
12100 	  if (dyn_to_skip)
12101 	    {
12102 	      dyn_skipped += dyn_to_skip;
12103 	      dyn_to_skip = 0;
12104 	    }
12105 	}
12106 
12107       /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
12108       if (dyn_skipped > 0)
12109 	memset (b - dyn_skipped, 0, dyn_skipped);
12110     }
12111 
12112   if (sgot != NULL && sgot->size > 0
12113       && !bfd_is_abs_section (sgot->output_section))
12114     {
12115       if (htab->root.target_os == is_vxworks)
12116 	{
12117 	  /* The first entry of the global offset table points to the
12118 	     ".dynamic" section.  The second is initialized by the
12119 	     loader and contains the shared library identifier.
12120 	     The third is also initialized by the loader and points
12121 	     to the lazy resolution stub.  */
12122 	  MIPS_ELF_PUT_WORD (output_bfd,
12123 			     sdyn->output_offset + sdyn->output_section->vma,
12124 			     sgot->contents);
12125 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
12126 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12127 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
12128 			     sgot->contents
12129 			     + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12130 	}
12131       else
12132 	{
12133 	  /* The first entry of the global offset table will be filled at
12134 	     runtime. The second entry will be used by some runtime loaders.
12135 	     This isn't the case of IRIX rld.  */
12136 	  MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12137 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12138 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12139 	}
12140 
12141       elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12142 	 = MIPS_ELF_GOT_SIZE (output_bfd);
12143     }
12144 
12145   /* Generate dynamic relocations for the non-primary gots.  */
12146   if (gg != NULL && gg->next)
12147     {
12148       Elf_Internal_Rela rel[3];
12149       bfd_vma addend = 0;
12150 
12151       memset (rel, 0, sizeof (rel));
12152       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12153 
12154       for (g = gg->next; g->next != gg; g = g->next)
12155 	{
12156 	  bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12157 	    + g->next->tls_gotno;
12158 
12159 	  MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12160 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12161 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12162 			     sgot->contents
12163 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12164 
12165 	  if (! bfd_link_pic (info))
12166 	    continue;
12167 
12168 	  for (; got_index < g->local_gotno; got_index++)
12169 	    {
12170 	      if (got_index >= g->assigned_low_gotno
12171 		  && got_index <= g->assigned_high_gotno)
12172 		continue;
12173 
12174 	      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12175 		= got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12176 	      if (!(mips_elf_create_dynamic_relocation
12177 		    (output_bfd, info, rel, NULL,
12178 		     bfd_abs_section_ptr,
12179 		     0, &addend, sgot)))
12180 		return false;
12181 	      BFD_ASSERT (addend == 0);
12182 	    }
12183 	}
12184     }
12185 
12186   /* The generation of dynamic relocations for the non-primary gots
12187      adds more dynamic relocations.  We cannot count them until
12188      here.  */
12189 
12190   if (elf_hash_table (info)->dynamic_sections_created)
12191     {
12192       bfd_byte *b;
12193       bool swap_out_p;
12194 
12195       BFD_ASSERT (sdyn != NULL);
12196 
12197       for (b = sdyn->contents;
12198 	   b < sdyn->contents + sdyn->size;
12199 	   b += MIPS_ELF_DYN_SIZE (dynobj))
12200 	{
12201 	  Elf_Internal_Dyn dyn;
12202 	  asection *s;
12203 
12204 	  /* Read in the current dynamic entry.  */
12205 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12206 
12207 	  /* Assume that we're going to modify it and write it out.  */
12208 	  swap_out_p = true;
12209 
12210 	  switch (dyn.d_tag)
12211 	    {
12212 	    case DT_RELSZ:
12213 	      /* Reduce DT_RELSZ to account for any relocations we
12214 		 decided not to make.  This is for the n64 irix rld,
12215 		 which doesn't seem to apply any relocations if there
12216 		 are trailing null entries.  */
12217 	      s = mips_elf_rel_dyn_section (info, false);
12218 	      dyn.d_un.d_val = (s->reloc_count
12219 				* (ABI_64_P (output_bfd)
12220 				   ? sizeof (Elf64_Mips_External_Rel)
12221 				   : sizeof (Elf32_External_Rel)));
12222 	      /* Adjust the section size too.  Tools like the prelinker
12223 		 can reasonably expect the values to the same.  */
12224 	      BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12225 	      elf_section_data (s->output_section)->this_hdr.sh_size
12226 		= dyn.d_un.d_val;
12227 	      break;
12228 
12229 	    default:
12230 	      swap_out_p = false;
12231 	      break;
12232 	    }
12233 
12234 	  if (swap_out_p)
12235 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12236 	      (dynobj, &dyn, b);
12237 	}
12238     }
12239 
12240   {
12241     asection *s;
12242     Elf32_compact_rel cpt;
12243 
12244     if (SGI_COMPAT (output_bfd))
12245       {
12246 	/* Write .compact_rel section out.  */
12247 	s = bfd_get_linker_section (dynobj, ".compact_rel");
12248 	if (s != NULL)
12249 	  {
12250 	    cpt.id1 = 1;
12251 	    cpt.num = s->reloc_count;
12252 	    cpt.id2 = 2;
12253 	    cpt.offset = (s->output_section->filepos
12254 			  + sizeof (Elf32_External_compact_rel));
12255 	    cpt.reserved0 = 0;
12256 	    cpt.reserved1 = 0;
12257 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12258 					    ((Elf32_External_compact_rel *)
12259 					     s->contents));
12260 
12261 	    /* Clean up a dummy stub function entry in .text.  */
12262 	    if (htab->sstubs != NULL
12263 		&& htab->sstubs->contents != NULL)
12264 	      {
12265 		file_ptr dummy_offset;
12266 
12267 		BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12268 		dummy_offset = htab->sstubs->size - htab->function_stub_size;
12269 		memset (htab->sstubs->contents + dummy_offset, 0,
12270 			htab->function_stub_size);
12271 	      }
12272 	  }
12273       }
12274 
12275     /* The psABI says that the dynamic relocations must be sorted in
12276        increasing order of r_symndx.  The VxWorks EABI doesn't require
12277        this, and because the code below handles REL rather than RELA
12278        relocations, using it for VxWorks would be outright harmful.  */
12279     if (htab->root.target_os != is_vxworks)
12280       {
12281 	s = mips_elf_rel_dyn_section (info, false);
12282 	if (s != NULL
12283 	    && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12284 	  {
12285 	    reldyn_sorting_bfd = output_bfd;
12286 
12287 	    if (ABI_64_P (output_bfd))
12288 	      qsort ((Elf64_External_Rel *) s->contents + 1,
12289 		     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12290 		     sort_dynamic_relocs_64);
12291 	    else
12292 	      qsort ((Elf32_External_Rel *) s->contents + 1,
12293 		     s->reloc_count - 1, sizeof (Elf32_External_Rel),
12294 		     sort_dynamic_relocs);
12295 	  }
12296       }
12297   }
12298 
12299   if (htab->root.splt && htab->root.splt->size > 0)
12300     {
12301       if (htab->root.target_os == is_vxworks)
12302 	{
12303 	  if (bfd_link_pic (info))
12304 	    mips_vxworks_finish_shared_plt (output_bfd, info);
12305 	  else
12306 	    mips_vxworks_finish_exec_plt (output_bfd, info);
12307 	}
12308       else
12309 	{
12310 	  BFD_ASSERT (!bfd_link_pic (info));
12311 	  if (!mips_finish_exec_plt (output_bfd, info))
12312 	    return false;
12313 	}
12314     }
12315   return true;
12316 }
12317 
12318 
12319 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
12320 
12321 static void
12322 mips_set_isa_flags (bfd *abfd)
12323 {
12324   flagword val;
12325 
12326   switch (bfd_get_mach (abfd))
12327     {
12328     default:
12329       if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12330         val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
12331       else
12332         val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
12333       break;
12334 
12335     case bfd_mach_mips3000:
12336       val = EF_MIPS_ARCH_1;
12337       break;
12338 
12339     case bfd_mach_mips3900:
12340       val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
12341       break;
12342 
12343     case bfd_mach_mips6000:
12344       val = EF_MIPS_ARCH_2;
12345       break;
12346 
12347     case bfd_mach_mips4010:
12348       val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
12349       break;
12350 
12351     case bfd_mach_mips_allegrex:
12352       val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
12353       break;
12354 
12355     case bfd_mach_mips4000:
12356     case bfd_mach_mips4300:
12357     case bfd_mach_mips4400:
12358     case bfd_mach_mips4600:
12359       val = EF_MIPS_ARCH_3;
12360       break;
12361 
12362     case bfd_mach_mips4100:
12363       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
12364       break;
12365 
12366     case bfd_mach_mips4111:
12367       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
12368       break;
12369 
12370     case bfd_mach_mips4120:
12371       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
12372       break;
12373 
12374     case bfd_mach_mips4650:
12375       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
12376       break;
12377 
12378     case bfd_mach_mips5400:
12379       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
12380       break;
12381 
12382     case bfd_mach_mips5500:
12383       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
12384       break;
12385 
12386     case bfd_mach_mips5900:
12387       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
12388       break;
12389 
12390     case bfd_mach_mips9000:
12391       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
12392       break;
12393 
12394     case bfd_mach_mips5000:
12395     case bfd_mach_mips7000:
12396     case bfd_mach_mips8000:
12397     case bfd_mach_mips10000:
12398     case bfd_mach_mips12000:
12399     case bfd_mach_mips14000:
12400     case bfd_mach_mips16000:
12401       val = EF_MIPS_ARCH_4;
12402       break;
12403 
12404     case bfd_mach_mips5:
12405       val = EF_MIPS_ARCH_5;
12406       break;
12407 
12408     case bfd_mach_mips_loongson_2e:
12409       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
12410       break;
12411 
12412     case bfd_mach_mips_loongson_2f:
12413       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
12414       break;
12415 
12416     case bfd_mach_mips_sb1:
12417       val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
12418       break;
12419 
12420     case bfd_mach_mips_gs464:
12421       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
12422       break;
12423 
12424     case bfd_mach_mips_gs464e:
12425       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
12426       break;
12427 
12428     case bfd_mach_mips_gs264e:
12429       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
12430       break;
12431 
12432     case bfd_mach_mips_octeon:
12433     case bfd_mach_mips_octeonp:
12434       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
12435       break;
12436 
12437     case bfd_mach_mips_octeon3:
12438       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
12439       break;
12440 
12441     case bfd_mach_mips_xlr:
12442       val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
12443       break;
12444 
12445     case bfd_mach_mips_octeon2:
12446       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
12447       break;
12448 
12449     case bfd_mach_mipsisa32:
12450       val = EF_MIPS_ARCH_32;
12451       break;
12452 
12453     case bfd_mach_mipsisa64:
12454       val = EF_MIPS_ARCH_64;
12455       break;
12456 
12457     case bfd_mach_mipsisa32r2:
12458     case bfd_mach_mipsisa32r3:
12459     case bfd_mach_mipsisa32r5:
12460       val = EF_MIPS_ARCH_32R2;
12461       break;
12462 
12463     case bfd_mach_mips_interaptiv_mr2:
12464       val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
12465       break;
12466 
12467     case bfd_mach_mipsisa64r2:
12468     case bfd_mach_mipsisa64r3:
12469     case bfd_mach_mipsisa64r5:
12470       val = EF_MIPS_ARCH_64R2;
12471       break;
12472 
12473     case bfd_mach_mipsisa32r6:
12474       val = EF_MIPS_ARCH_32R6;
12475       break;
12476 
12477     case bfd_mach_mipsisa64r6:
12478       val = EF_MIPS_ARCH_64R6;
12479       break;
12480     }
12481   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12482   elf_elfheader (abfd)->e_flags |= val;
12483 
12484 }
12485 
12486 
12487 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12488    Don't do so for code sections.  We want to keep ordering of HI16/LO16
12489    as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
12490    relocs to be sorted.  */
12491 
12492 bool
12493 _bfd_mips_elf_sort_relocs_p (asection *sec)
12494 {
12495   return (sec->flags & SEC_CODE) == 0;
12496 }
12497 
12498 
12499 /* The final processing done just before writing out a MIPS ELF object
12500    file.  This gets the MIPS architecture right based on the machine
12501    number.  This is used by both the 32-bit and the 64-bit ABI.  */
12502 
12503 void
12504 _bfd_mips_final_write_processing (bfd *abfd)
12505 {
12506   unsigned int i;
12507   Elf_Internal_Shdr **hdrpp;
12508   const char *name;
12509   asection *sec;
12510 
12511   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12512      is nonzero.  This is for compatibility with old objects, which used
12513      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
12514   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12515     mips_set_isa_flags (abfd);
12516 
12517   /* Set the sh_info field for .gptab sections and other appropriate
12518      info for each special section.  */
12519   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12520        i < elf_numsections (abfd);
12521        i++, hdrpp++)
12522     {
12523       switch ((*hdrpp)->sh_type)
12524 	{
12525 	case SHT_MIPS_MSYM:
12526 	case SHT_MIPS_LIBLIST:
12527 	  sec = bfd_get_section_by_name (abfd, ".dynstr");
12528 	  if (sec != NULL)
12529 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12530 	  break;
12531 
12532 	case SHT_MIPS_GPTAB:
12533 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12534 	  name = bfd_section_name ((*hdrpp)->bfd_section);
12535 	  if (startswith (name, ".gptab."))
12536 	    {
12537 	      sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12538 	      if (sec != NULL)
12539 		(*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12540 	    }
12541 	  break;
12542 
12543 	case SHT_MIPS_CONTENT:
12544 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12545 	  name = bfd_section_name ((*hdrpp)->bfd_section);
12546 	  if (startswith (name, ".MIPS.content"))
12547 	    {
12548 	      sec = bfd_get_section_by_name (abfd,
12549 					     name + sizeof ".MIPS.content" - 1);
12550 	      if (sec != NULL)
12551 		(*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12552 	    }
12553 	  break;
12554 
12555 	case SHT_MIPS_SYMBOL_LIB:
12556 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
12557 	  if (sec != NULL)
12558 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12559 	  sec = bfd_get_section_by_name (abfd, ".liblist");
12560 	  if (sec != NULL)
12561 	    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12562 	  break;
12563 
12564 	case SHT_MIPS_EVENTS:
12565 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12566 	  name = bfd_section_name ((*hdrpp)->bfd_section);
12567 	  if (startswith (name, ".MIPS.events"))
12568 	    sec = bfd_get_section_by_name (abfd,
12569 					   name + sizeof ".MIPS.events" - 1);
12570 	  else if (startswith (name, ".MIPS.post_rel"))
12571 	    sec = bfd_get_section_by_name (abfd,
12572 					   name + sizeof ".MIPS.post_rel" - 1);
12573 	  else
12574 	    sec = NULL;
12575 	  if (sec != NULL)
12576 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12577 	  break;
12578 
12579 	case SHT_MIPS_XHASH:
12580 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
12581 	  if (sec != NULL)
12582 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12583 	}
12584     }
12585 }
12586 
12587 bool
12588 _bfd_mips_elf_final_write_processing (bfd *abfd)
12589 {
12590   _bfd_mips_final_write_processing (abfd);
12591   return _bfd_elf_final_write_processing (abfd);
12592 }
12593 
12594 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12595    segments.  */
12596 
12597 int
12598 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12599 					  struct bfd_link_info *info ATTRIBUTE_UNUSED)
12600 {
12601   asection *s;
12602   int ret = 0;
12603 
12604   /* See if we need a PT_MIPS_REGINFO segment.  */
12605   s = bfd_get_section_by_name (abfd, ".reginfo");
12606   if (s && (s->flags & SEC_LOAD))
12607     ++ret;
12608 
12609   /* See if we need a PT_MIPS_ABIFLAGS segment.  */
12610   if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12611     ++ret;
12612 
12613   /* See if we need a PT_MIPS_OPTIONS segment.  */
12614   if (IRIX_COMPAT (abfd) == ict_irix6
12615       && bfd_get_section_by_name (abfd,
12616 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12617     ++ret;
12618 
12619   /* See if we need a PT_MIPS_RTPROC segment.  */
12620   if (IRIX_COMPAT (abfd) == ict_irix5
12621       && bfd_get_section_by_name (abfd, ".dynamic")
12622       && bfd_get_section_by_name (abfd, ".mdebug"))
12623     ++ret;
12624 
12625   /* Allocate a PT_NULL header in dynamic objects.  See
12626      _bfd_mips_elf_modify_segment_map for details.  */
12627   if (!SGI_COMPAT (abfd)
12628       && bfd_get_section_by_name (abfd, ".dynamic"))
12629     ++ret;
12630 
12631   return ret;
12632 }
12633 
12634 /* Modify the segment map for an IRIX5 executable.  */
12635 
12636 bool
12637 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12638 				  struct bfd_link_info *info)
12639 {
12640   asection *s;
12641   struct elf_segment_map *m, **pm;
12642   size_t amt;
12643 
12644   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12645      segment.  */
12646   s = bfd_get_section_by_name (abfd, ".reginfo");
12647   if (s != NULL && (s->flags & SEC_LOAD) != 0)
12648     {
12649       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12650 	if (m->p_type == PT_MIPS_REGINFO)
12651 	  break;
12652       if (m == NULL)
12653 	{
12654 	  amt = sizeof *m;
12655 	  m = bfd_zalloc (abfd, amt);
12656 	  if (m == NULL)
12657 	    return false;
12658 
12659 	  m->p_type = PT_MIPS_REGINFO;
12660 	  m->count = 1;
12661 	  m->sections[0] = s;
12662 
12663 	  /* We want to put it after the PHDR and INTERP segments.  */
12664 	  pm = &elf_seg_map (abfd);
12665 	  while (*pm != NULL
12666 		 && ((*pm)->p_type == PT_PHDR
12667 		     || (*pm)->p_type == PT_INTERP))
12668 	    pm = &(*pm)->next;
12669 
12670 	  m->next = *pm;
12671 	  *pm = m;
12672 	}
12673     }
12674 
12675   /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12676      segment.  */
12677   s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12678   if (s != NULL && (s->flags & SEC_LOAD) != 0)
12679     {
12680       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12681 	if (m->p_type == PT_MIPS_ABIFLAGS)
12682 	  break;
12683       if (m == NULL)
12684 	{
12685 	  amt = sizeof *m;
12686 	  m = bfd_zalloc (abfd, amt);
12687 	  if (m == NULL)
12688 	    return false;
12689 
12690 	  m->p_type = PT_MIPS_ABIFLAGS;
12691 	  m->count = 1;
12692 	  m->sections[0] = s;
12693 
12694 	  /* We want to put it after the PHDR and INTERP segments.  */
12695 	  pm = &elf_seg_map (abfd);
12696 	  while (*pm != NULL
12697 		 && ((*pm)->p_type == PT_PHDR
12698 		     || (*pm)->p_type == PT_INTERP))
12699 	    pm = &(*pm)->next;
12700 
12701 	  m->next = *pm;
12702 	  *pm = m;
12703 	}
12704     }
12705 
12706   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12707      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
12708      PT_MIPS_OPTIONS segment immediately following the program header
12709      table.  */
12710   if (NEWABI_P (abfd)
12711       /* On non-IRIX6 new abi, we'll have already created a segment
12712 	 for this section, so don't create another.  I'm not sure this
12713 	 is not also the case for IRIX 6, but I can't test it right
12714 	 now.  */
12715       && IRIX_COMPAT (abfd) == ict_irix6)
12716     {
12717       for (s = abfd->sections; s; s = s->next)
12718 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12719 	  break;
12720 
12721       if (s)
12722 	{
12723 	  struct elf_segment_map *options_segment;
12724 
12725 	  pm = &elf_seg_map (abfd);
12726 	  while (*pm != NULL
12727 		 && ((*pm)->p_type == PT_PHDR
12728 		     || (*pm)->p_type == PT_INTERP))
12729 	    pm = &(*pm)->next;
12730 
12731 	  if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12732 	    {
12733 	      amt = sizeof (struct elf_segment_map);
12734 	      options_segment = bfd_zalloc (abfd, amt);
12735 	      options_segment->next = *pm;
12736 	      options_segment->p_type = PT_MIPS_OPTIONS;
12737 	      options_segment->p_flags = PF_R;
12738 	      options_segment->p_flags_valid = true;
12739 	      options_segment->count = 1;
12740 	      options_segment->sections[0] = s;
12741 	      *pm = options_segment;
12742 	    }
12743 	}
12744     }
12745   else
12746     {
12747       if (IRIX_COMPAT (abfd) == ict_irix5)
12748 	{
12749 	  /* If there are .dynamic and .mdebug sections, we make a room
12750 	     for the RTPROC header.  FIXME: Rewrite without section names.  */
12751 	  if (bfd_get_section_by_name (abfd, ".interp") == NULL
12752 	      && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12753 	      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12754 	    {
12755 	      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12756 		if (m->p_type == PT_MIPS_RTPROC)
12757 		  break;
12758 	      if (m == NULL)
12759 		{
12760 		  amt = sizeof *m;
12761 		  m = bfd_zalloc (abfd, amt);
12762 		  if (m == NULL)
12763 		    return false;
12764 
12765 		  m->p_type = PT_MIPS_RTPROC;
12766 
12767 		  s = bfd_get_section_by_name (abfd, ".rtproc");
12768 		  if (s == NULL)
12769 		    {
12770 		      m->count = 0;
12771 		      m->p_flags = 0;
12772 		      m->p_flags_valid = 1;
12773 		    }
12774 		  else
12775 		    {
12776 		      m->count = 1;
12777 		      m->sections[0] = s;
12778 		    }
12779 
12780 		  /* We want to put it after the DYNAMIC segment.  */
12781 		  pm = &elf_seg_map (abfd);
12782 		  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12783 		    pm = &(*pm)->next;
12784 		  if (*pm != NULL)
12785 		    pm = &(*pm)->next;
12786 
12787 		  m->next = *pm;
12788 		  *pm = m;
12789 		}
12790 	    }
12791 	}
12792       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12793 	 .dynstr, .dynsym, and .hash sections, and everything in
12794 	 between.  */
12795       for (pm = &elf_seg_map (abfd); *pm != NULL;
12796 	   pm = &(*pm)->next)
12797 	if ((*pm)->p_type == PT_DYNAMIC)
12798 	  break;
12799       m = *pm;
12800       /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12801 	 glibc's dynamic linker has traditionally derived the number of
12802 	 tags from the p_filesz field, and sometimes allocates stack
12803 	 arrays of that size.  An overly-big PT_DYNAMIC segment can
12804 	 be actively harmful in such cases.  Making PT_DYNAMIC contain
12805 	 other sections can also make life hard for the prelinker,
12806 	 which might move one of the other sections to a different
12807 	 PT_LOAD segment.  */
12808       if (SGI_COMPAT (abfd)
12809 	  && m != NULL
12810 	  && m->count == 1
12811 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
12812 	{
12813 	  static const char *sec_names[] =
12814 	  {
12815 	    ".dynamic", ".dynstr", ".dynsym", ".hash"
12816 	  };
12817 	  bfd_vma low, high;
12818 	  unsigned int i, c;
12819 	  struct elf_segment_map *n;
12820 
12821 	  low = ~(bfd_vma) 0;
12822 	  high = 0;
12823 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12824 	    {
12825 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
12826 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
12827 		{
12828 		  bfd_size_type sz;
12829 
12830 		  if (low > s->vma)
12831 		    low = s->vma;
12832 		  sz = s->size;
12833 		  if (high < s->vma + sz)
12834 		    high = s->vma + sz;
12835 		}
12836 	    }
12837 
12838 	  c = 0;
12839 	  for (s = abfd->sections; s != NULL; s = s->next)
12840 	    if ((s->flags & SEC_LOAD) != 0
12841 		&& s->vma >= low
12842 		&& s->vma + s->size <= high)
12843 	      ++c;
12844 
12845 	  amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12846 	  n = bfd_zalloc (abfd, amt);
12847 	  if (n == NULL)
12848 	    return false;
12849 	  *n = *m;
12850 	  n->count = c;
12851 
12852 	  i = 0;
12853 	  for (s = abfd->sections; s != NULL; s = s->next)
12854 	    {
12855 	      if ((s->flags & SEC_LOAD) != 0
12856 		  && s->vma >= low
12857 		  && s->vma + s->size <= high)
12858 		{
12859 		  n->sections[i] = s;
12860 		  ++i;
12861 		}
12862 	    }
12863 
12864 	  *pm = n;
12865 	}
12866     }
12867 
12868   /* Allocate a spare program header in dynamic objects so that tools
12869      like the prelinker can add an extra PT_LOAD entry.
12870 
12871      If the prelinker needs to make room for a new PT_LOAD entry, its
12872      standard procedure is to move the first (read-only) sections into
12873      the new (writable) segment.  However, the MIPS ABI requires
12874      .dynamic to be in a read-only segment, and the section will often
12875      start within sizeof (ElfNN_Phdr) bytes of the last program header.
12876 
12877      Although the prelinker could in principle move .dynamic to a
12878      writable segment, it seems better to allocate a spare program
12879      header instead, and avoid the need to move any sections.
12880      There is a long tradition of allocating spare dynamic tags,
12881      so allocating a spare program header seems like a natural
12882      extension.
12883 
12884      If INFO is NULL, we may be copying an already prelinked binary
12885      with objcopy or strip, so do not add this header.  */
12886   if (info != NULL
12887       && !SGI_COMPAT (abfd)
12888       && bfd_get_section_by_name (abfd, ".dynamic"))
12889     {
12890       for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12891 	if ((*pm)->p_type == PT_NULL)
12892 	  break;
12893       if (*pm == NULL)
12894 	{
12895 	  m = bfd_zalloc (abfd, sizeof (*m));
12896 	  if (m == NULL)
12897 	    return false;
12898 
12899 	  m->p_type = PT_NULL;
12900 	  *pm = m;
12901 	}
12902     }
12903 
12904   return true;
12905 }
12906 
12907 /* Return the section that should be marked against GC for a given
12908    relocation.  */
12909 
12910 asection *
12911 _bfd_mips_elf_gc_mark_hook (asection *sec,
12912 			    struct bfd_link_info *info,
12913 			    Elf_Internal_Rela *rel,
12914 			    struct elf_link_hash_entry *h,
12915 			    Elf_Internal_Sym *sym)
12916 {
12917   /* ??? Do mips16 stub sections need to be handled special?  */
12918 
12919   if (h != NULL)
12920     switch (ELF_R_TYPE (sec->owner, rel->r_info))
12921       {
12922       case R_MIPS_GNU_VTINHERIT:
12923       case R_MIPS_GNU_VTENTRY:
12924 	return NULL;
12925       }
12926 
12927   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12928 }
12929 
12930 /* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
12931 
12932 bool
12933 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12934 				      elf_gc_mark_hook_fn gc_mark_hook)
12935 {
12936   bfd *sub;
12937 
12938   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12939 
12940   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12941     {
12942       asection *o;
12943 
12944       if (! is_mips_elf (sub))
12945 	continue;
12946 
12947       for (o = sub->sections; o != NULL; o = o->next)
12948 	if (!o->gc_mark
12949 	    && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12950 	  {
12951 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12952 	      return false;
12953 	  }
12954     }
12955 
12956   return true;
12957 }
12958 
12959 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12960    hiding the old indirect symbol.  Process additional relocation
12961    information.  Also called for weakdefs, in which case we just let
12962    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
12963 
12964 void
12965 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12966 				    struct elf_link_hash_entry *dir,
12967 				    struct elf_link_hash_entry *ind)
12968 {
12969   struct mips_elf_link_hash_entry *dirmips, *indmips;
12970 
12971   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12972 
12973   dirmips = (struct mips_elf_link_hash_entry *) dir;
12974   indmips = (struct mips_elf_link_hash_entry *) ind;
12975   /* Any absolute non-dynamic relocations against an indirect or weak
12976      definition will be against the target symbol.  */
12977   if (indmips->has_static_relocs)
12978     dirmips->has_static_relocs = true;
12979 
12980   if (ind->root.type != bfd_link_hash_indirect)
12981     return;
12982 
12983   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12984   if (indmips->readonly_reloc)
12985     dirmips->readonly_reloc = true;
12986   if (indmips->no_fn_stub)
12987     dirmips->no_fn_stub = true;
12988   if (indmips->fn_stub)
12989     {
12990       dirmips->fn_stub = indmips->fn_stub;
12991       indmips->fn_stub = NULL;
12992     }
12993   if (indmips->need_fn_stub)
12994     {
12995       dirmips->need_fn_stub = true;
12996       indmips->need_fn_stub = false;
12997     }
12998   if (indmips->call_stub)
12999     {
13000       dirmips->call_stub = indmips->call_stub;
13001       indmips->call_stub = NULL;
13002     }
13003   if (indmips->call_fp_stub)
13004     {
13005       dirmips->call_fp_stub = indmips->call_fp_stub;
13006       indmips->call_fp_stub = NULL;
13007     }
13008   if (indmips->global_got_area < dirmips->global_got_area)
13009     dirmips->global_got_area = indmips->global_got_area;
13010   if (indmips->global_got_area < GGA_NONE)
13011     indmips->global_got_area = GGA_NONE;
13012   if (indmips->has_nonpic_branches)
13013     dirmips->has_nonpic_branches = true;
13014 }
13015 
13016 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13017    to hide it.  It has to remain global (it will also be protected) so as to
13018    be assigned a global GOT entry, which will then remain unchanged at load
13019    time.  */
13020 
13021 void
13022 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13023 			   struct elf_link_hash_entry *entry,
13024 			   bool force_local)
13025 {
13026   struct mips_elf_link_hash_table *htab;
13027 
13028   htab = mips_elf_hash_table (info);
13029   BFD_ASSERT (htab != NULL);
13030   if (htab->use_absolute_zero
13031       && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13032     return;
13033 
13034   _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13035 }
13036 
13037 #define PDR_SIZE 32
13038 
13039 bool
13040 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13041 			    struct bfd_link_info *info)
13042 {
13043   asection *o;
13044   bool ret = false;
13045   unsigned char *tdata;
13046   size_t i, skip;
13047 
13048   o = bfd_get_section_by_name (abfd, ".pdr");
13049   if (! o)
13050     return false;
13051   if (o->size == 0)
13052     return false;
13053   if (o->size % PDR_SIZE != 0)
13054     return false;
13055   if (o->output_section != NULL
13056       && bfd_is_abs_section (o->output_section))
13057     return false;
13058 
13059   tdata = bfd_zmalloc (o->size / PDR_SIZE);
13060   if (! tdata)
13061     return false;
13062 
13063   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13064 					    info->keep_memory);
13065   if (!cookie->rels)
13066     {
13067       free (tdata);
13068       return false;
13069     }
13070 
13071   cookie->rel = cookie->rels;
13072   cookie->relend = cookie->rels + o->reloc_count;
13073 
13074   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13075     {
13076       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13077 	{
13078 	  tdata[i] = 1;
13079 	  skip ++;
13080 	}
13081     }
13082 
13083   if (skip != 0)
13084     {
13085       mips_elf_section_data (o)->u.tdata = tdata;
13086       if (o->rawsize == 0)
13087 	o->rawsize = o->size;
13088       o->size -= skip * PDR_SIZE;
13089       ret = true;
13090     }
13091   else
13092     free (tdata);
13093 
13094   if (! info->keep_memory)
13095     free (cookie->rels);
13096 
13097   return ret;
13098 }
13099 
13100 bool
13101 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13102 {
13103   if (strcmp (sec->name, ".pdr") == 0)
13104     return true;
13105   return false;
13106 }
13107 
13108 bool
13109 _bfd_mips_elf_write_section (bfd *output_bfd,
13110 			     struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13111 			     asection *sec, bfd_byte *contents)
13112 {
13113   bfd_byte *to, *from, *end;
13114   int i;
13115 
13116   if (strcmp (sec->name, ".pdr") != 0)
13117     return false;
13118 
13119   if (mips_elf_section_data (sec)->u.tdata == NULL)
13120     return false;
13121 
13122   to = contents;
13123   end = contents + sec->size;
13124   for (from = contents, i = 0;
13125        from < end;
13126        from += PDR_SIZE, i++)
13127     {
13128       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13129 	continue;
13130       if (to != from)
13131 	memcpy (to, from, PDR_SIZE);
13132       to += PDR_SIZE;
13133     }
13134   bfd_set_section_contents (output_bfd, sec->output_section, contents,
13135 			    sec->output_offset, sec->size);
13136   return true;
13137 }
13138 
13139 /* microMIPS code retains local labels for linker relaxation.  Omit them
13140    from output by default for clarity.  */
13141 
13142 bool
13143 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13144 {
13145   return _bfd_elf_is_local_label_name (abfd, sym->name);
13146 }
13147 
13148 bool
13149 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13150 				 asection *section, bfd_vma offset,
13151 				 const char **filename_ptr,
13152 				 const char **functionname_ptr,
13153 				 unsigned int *line_ptr,
13154 				 unsigned int *discriminator_ptr)
13155 {
13156   asection *msec;
13157 
13158   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13159 				     filename_ptr, functionname_ptr,
13160 				     line_ptr, discriminator_ptr,
13161 				     dwarf_debug_sections,
13162 				     &elf_tdata (abfd)->dwarf2_find_line_info)
13163       == 1)
13164     return true;
13165 
13166   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13167 				     filename_ptr, functionname_ptr,
13168 				     line_ptr))
13169     {
13170       if (!*functionname_ptr)
13171 	_bfd_elf_find_function (abfd, symbols, section, offset,
13172 				*filename_ptr ? NULL : filename_ptr,
13173 				functionname_ptr);
13174       return true;
13175     }
13176 
13177   msec = bfd_get_section_by_name (abfd, ".mdebug");
13178   if (msec != NULL)
13179     {
13180       flagword origflags;
13181       struct mips_elf_find_line *fi;
13182       const struct ecoff_debug_swap * const swap =
13183 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13184 
13185       /* If we are called during a link, mips_elf_final_link may have
13186 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
13187 	 if appropriate (which it normally will be).  */
13188       origflags = msec->flags;
13189       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13190 	msec->flags |= SEC_HAS_CONTENTS;
13191 
13192       fi = mips_elf_tdata (abfd)->find_line_info;
13193       if (fi == NULL)
13194 	{
13195 	  bfd_size_type external_fdr_size;
13196 	  char *fraw_src;
13197 	  char *fraw_end;
13198 	  struct fdr *fdr_ptr;
13199 	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
13200 
13201 	  fi = bfd_zalloc (abfd, amt);
13202 	  if (fi == NULL)
13203 	    {
13204 	      msec->flags = origflags;
13205 	      return false;
13206 	    }
13207 
13208 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13209 	    {
13210 	      msec->flags = origflags;
13211 	      return false;
13212 	    }
13213 
13214 	  /* Swap in the FDR information.  */
13215 	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13216 	  fi->d.fdr = bfd_alloc (abfd, amt);
13217 	  if (fi->d.fdr == NULL)
13218 	    {
13219 	      _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13220 	      msec->flags = origflags;
13221 	      return false;
13222 	    }
13223 	  external_fdr_size = swap->external_fdr_size;
13224 	  fdr_ptr = fi->d.fdr;
13225 	  fraw_src = (char *) fi->d.external_fdr;
13226 	  fraw_end = (fraw_src
13227 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
13228 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13229 	    (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13230 
13231 	  mips_elf_tdata (abfd)->find_line_info = fi;
13232 	}
13233 
13234       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13235 				  &fi->i, filename_ptr, functionname_ptr,
13236 				  line_ptr))
13237 	{
13238 	  msec->flags = origflags;
13239 	  return true;
13240 	}
13241 
13242       msec->flags = origflags;
13243     }
13244 
13245   /* Fall back on the generic ELF find_nearest_line routine.  */
13246 
13247   return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13248 				     filename_ptr, functionname_ptr,
13249 				     line_ptr, discriminator_ptr);
13250 }
13251 
13252 bool
13253 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13254 				 const char **filename_ptr,
13255 				 const char **functionname_ptr,
13256 				 unsigned int *line_ptr)
13257 {
13258   bool found;
13259   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13260 					 functionname_ptr, line_ptr,
13261 					 & elf_tdata (abfd)->dwarf2_find_line_info);
13262   return found;
13263 }
13264 
13265 
13266 /* When are writing out the .options or .MIPS.options section,
13267    remember the bytes we are writing out, so that we can install the
13268    GP value in the section_processing routine.  */
13269 
13270 bool
13271 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13272 				    const void *location,
13273 				    file_ptr offset, bfd_size_type count)
13274 {
13275   if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13276     {
13277       bfd_byte *c;
13278 
13279       if (elf_section_data (section) == NULL)
13280 	{
13281 	  size_t amt = sizeof (struct bfd_elf_section_data);
13282 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
13283 	  if (elf_section_data (section) == NULL)
13284 	    return false;
13285 	}
13286       c = mips_elf_section_data (section)->u.tdata;
13287       if (c == NULL)
13288 	{
13289 	  c = bfd_zalloc (abfd, section->size);
13290 	  if (c == NULL)
13291 	    return false;
13292 	  mips_elf_section_data (section)->u.tdata = c;
13293 	}
13294 
13295       memcpy (c + offset, location, count);
13296     }
13297 
13298   return _bfd_elf_set_section_contents (abfd, section, location, offset,
13299 					count);
13300 }
13301 
13302 /* This is almost identical to bfd_generic_get_... except that some
13303    MIPS relocations need to be handled specially.  Sigh.  */
13304 
13305 bfd_byte *
13306 _bfd_elf_mips_get_relocated_section_contents
13307   (bfd *abfd,
13308    struct bfd_link_info *link_info,
13309    struct bfd_link_order *link_order,
13310    bfd_byte *data,
13311    bool relocatable,
13312    asymbol **symbols)
13313 {
13314   bfd *input_bfd = link_order->u.indirect.section->owner;
13315   asection *input_section = link_order->u.indirect.section;
13316   long reloc_size;
13317   arelent **reloc_vector;
13318   long reloc_count;
13319 
13320   reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13321   if (reloc_size < 0)
13322     return NULL;
13323 
13324   /* Read in the section.  */
13325   bfd_byte *orig_data = data;
13326   if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13327     return NULL;
13328 
13329   if (data == NULL)
13330     return NULL;
13331 
13332   if (reloc_size == 0)
13333     return data;
13334 
13335   reloc_vector = (arelent **) bfd_malloc (reloc_size);
13336   if (reloc_vector == NULL)
13337     {
13338       struct mips_elf_obj_tdata *tdata;
13339       struct mips_hi16 **hip, *hi;
13340     error_return:
13341       /* If we are going to return an error, remove entries on
13342 	 mips_hi16_list that point into this section's data.  Data
13343 	 will typically be freed on return from this function.  */
13344       tdata = mips_elf_tdata (abfd);
13345       hip = &tdata->mips_hi16_list;
13346       while ((hi = *hip) != NULL)
13347 	{
13348 	  if (hi->input_section == input_section)
13349 	    {
13350 	      *hip = hi->next;
13351 	      free (hi);
13352 	    }
13353 	  else
13354 	    hip = &hi->next;
13355 	}
13356       if (orig_data == NULL)
13357 	free (data);
13358       data = NULL;
13359       goto out;
13360     }
13361 
13362   reloc_count = bfd_canonicalize_reloc (input_bfd,
13363 					input_section,
13364 					reloc_vector,
13365 					symbols);
13366   if (reloc_count < 0)
13367     goto error_return;
13368 
13369   if (reloc_count > 0)
13370     {
13371       arelent **parent;
13372       /* for mips */
13373       int gp_found;
13374       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
13375 
13376       {
13377 	struct bfd_hash_entry *h;
13378 	struct bfd_link_hash_entry *lh;
13379 	/* Skip all this stuff if we aren't mixing formats.  */
13380 	if (abfd && input_bfd
13381 	    && abfd->xvec == input_bfd->xvec)
13382 	  lh = 0;
13383 	else
13384 	  {
13385 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13386 	    lh = (struct bfd_link_hash_entry *) h;
13387 	  }
13388       lookup:
13389 	if (lh)
13390 	  {
13391 	    switch (lh->type)
13392 	      {
13393 	      case bfd_link_hash_undefined:
13394 	      case bfd_link_hash_undefweak:
13395 	      case bfd_link_hash_common:
13396 		gp_found = 0;
13397 		break;
13398 	      case bfd_link_hash_defined:
13399 	      case bfd_link_hash_defweak:
13400 		gp_found = 1;
13401 		gp = lh->u.def.value;
13402 		break;
13403 	      case bfd_link_hash_indirect:
13404 	      case bfd_link_hash_warning:
13405 		lh = lh->u.i.link;
13406 		/* @@FIXME  ignoring warning for now */
13407 		goto lookup;
13408 	      case bfd_link_hash_new:
13409 	      default:
13410 		abort ();
13411 	      }
13412 	  }
13413 	else
13414 	  gp_found = 0;
13415       }
13416       /* end mips */
13417 
13418       for (parent = reloc_vector; *parent != NULL; parent++)
13419 	{
13420 	  char *error_message = NULL;
13421 	  asymbol *symbol;
13422 	  bfd_reloc_status_type r;
13423 
13424 	  symbol = *(*parent)->sym_ptr_ptr;
13425 	  /* PR ld/19628: A specially crafted input file
13426 	     can result in a NULL symbol pointer here.  */
13427 	  if (symbol == NULL)
13428 	    {
13429 	      link_info->callbacks->einfo
13430 		/* xgettext:c-format */
13431 		(_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13432 		 abfd, input_section, (* parent)->address);
13433 	      goto error_return;
13434 	    }
13435 
13436 	  /* Zap reloc field when the symbol is from a discarded
13437 	     section, ignoring any addend.  Do the same when called
13438 	     from bfd_simple_get_relocated_section_contents for
13439 	     undefined symbols in debug sections.  This is to keep
13440 	     debug info reasonably sane, in particular so that
13441 	     DW_FORM_ref_addr to another file's .debug_info isn't
13442 	     confused with an offset into the current file's
13443 	     .debug_info.  */
13444 	  if ((symbol->section != NULL && discarded_section (symbol->section))
13445 	      || (symbol->section == bfd_und_section_ptr
13446 		  && (input_section->flags & SEC_DEBUGGING) != 0
13447 		  && link_info->input_bfds == link_info->output_bfd))
13448 	    {
13449 	      bfd_vma off;
13450 	      static reloc_howto_type none_howto
13451 		= HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13452 			 "unused", false, 0, 0, false);
13453 
13454 	      off = ((*parent)->address
13455 		     * bfd_octets_per_byte (input_bfd, input_section));
13456 	      _bfd_clear_contents ((*parent)->howto, input_bfd,
13457 				   input_section, data, off);
13458 	      (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13459 	      (*parent)->addend = 0;
13460 	      (*parent)->howto = &none_howto;
13461 	      r = bfd_reloc_ok;
13462 	    }
13463 
13464 	  /* Specific to MIPS: Deal with relocation types that require
13465 	     knowing the gp of the output bfd.  */
13466 
13467 	  /* If we've managed to find the gp and have a special
13468 	     function for the relocation then go ahead, else default
13469 	     to the generic handling.  */
13470 	  else if (gp_found
13471 		   && ((*parent)->howto->special_function
13472 		       == _bfd_mips_elf32_gprel16_reloc))
13473 	    r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13474 					       input_section, relocatable,
13475 					       data, gp);
13476 	  else
13477 	    r = bfd_perform_relocation (input_bfd,
13478 					*parent,
13479 					data,
13480 					input_section,
13481 					relocatable ? abfd : NULL,
13482 					&error_message);
13483 
13484 	  if (relocatable)
13485 	    {
13486 	      asection *os = input_section->output_section;
13487 
13488 	      /* A partial link, so keep the relocs.  */
13489 	      os->orelocation[os->reloc_count] = *parent;
13490 	      os->reloc_count++;
13491 	    }
13492 
13493 	  if (r != bfd_reloc_ok)
13494 	    {
13495 	      switch (r)
13496 		{
13497 		case bfd_reloc_undefined:
13498 		  (*link_info->callbacks->undefined_symbol)
13499 		    (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13500 		     input_bfd, input_section, (*parent)->address, true);
13501 		  break;
13502 		case bfd_reloc_dangerous:
13503 		  BFD_ASSERT (error_message != NULL);
13504 		  (*link_info->callbacks->reloc_dangerous)
13505 		    (link_info, error_message,
13506 		     input_bfd, input_section, (*parent)->address);
13507 		  break;
13508 		case bfd_reloc_overflow:
13509 		  (*link_info->callbacks->reloc_overflow)
13510 		    (link_info, NULL,
13511 		     bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13512 		     (*parent)->howto->name, (*parent)->addend,
13513 		     input_bfd, input_section, (*parent)->address);
13514 		  break;
13515 		case bfd_reloc_outofrange:
13516 		  /* PR ld/13730:
13517 		     This error can result when processing some partially
13518 		     complete binaries.  Do not abort, but issue an error
13519 		     message instead.  */
13520 		  link_info->callbacks->einfo
13521 		    /* xgettext:c-format */
13522 		    (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13523 		     abfd, input_section, * parent);
13524 		  goto error_return;
13525 
13526 		case bfd_reloc_notsupported:
13527 		  /* PR ld/17512
13528 		     This error can result when processing a corrupt binary.
13529 		     Do not abort.  Issue an error message instead.  */
13530 		  link_info->callbacks->einfo
13531 		    /* xgettext:c-format */
13532 		    (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13533 		     abfd, input_section, * parent);
13534 		  goto error_return;
13535 
13536 		default:
13537 		  /* PR 17512; file: 90c2a92e.
13538 		     Report unexpected results, without aborting.  */
13539 		  link_info->callbacks->einfo
13540 		    /* xgettext:c-format */
13541 		    (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13542 		     abfd, input_section, * parent, r);
13543 		  break;
13544 		}
13545 
13546 	    }
13547 	}
13548     }
13549 
13550  out:
13551   free (reloc_vector);
13552   return data;
13553 }
13554 
13555 static bool
13556 mips_elf_relax_delete_bytes (bfd *abfd,
13557 			     asection *sec, bfd_vma addr, int count)
13558 {
13559   Elf_Internal_Shdr *symtab_hdr;
13560   unsigned int sec_shndx;
13561   bfd_byte *contents;
13562   Elf_Internal_Rela *irel, *irelend;
13563   Elf_Internal_Sym *isym;
13564   Elf_Internal_Sym *isymend;
13565   struct elf_link_hash_entry **sym_hashes;
13566   struct elf_link_hash_entry **end_hashes;
13567   struct elf_link_hash_entry **start_hashes;
13568   unsigned int symcount;
13569 
13570   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13571   contents = elf_section_data (sec)->this_hdr.contents;
13572 
13573   irel = elf_section_data (sec)->relocs;
13574   irelend = irel + sec->reloc_count;
13575 
13576   /* Actually delete the bytes.  */
13577   memmove (contents + addr, contents + addr + count,
13578 	   (size_t) (sec->size - addr - count));
13579   sec->size -= count;
13580 
13581   /* Adjust all the relocs.  */
13582   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13583     {
13584       /* Get the new reloc address.  */
13585       if (irel->r_offset > addr)
13586 	irel->r_offset -= count;
13587     }
13588 
13589   BFD_ASSERT (addr % 2 == 0);
13590   BFD_ASSERT (count % 2 == 0);
13591 
13592   /* Adjust the local symbols defined in this section.  */
13593   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13594   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13595   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13596     if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13597       isym->st_value -= count;
13598 
13599   /* Now adjust the global symbols defined in this section.  */
13600   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13601 	      - symtab_hdr->sh_info);
13602   sym_hashes = start_hashes = elf_sym_hashes (abfd);
13603   end_hashes = sym_hashes + symcount;
13604 
13605   for (; sym_hashes < end_hashes; sym_hashes++)
13606     {
13607       struct elf_link_hash_entry *sym_hash = *sym_hashes;
13608 
13609       if ((sym_hash->root.type == bfd_link_hash_defined
13610 	   || sym_hash->root.type == bfd_link_hash_defweak)
13611 	  && sym_hash->root.u.def.section == sec)
13612 	{
13613 	  bfd_vma value = sym_hash->root.u.def.value;
13614 
13615 	  if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13616 	    value &= MINUS_TWO;
13617 	  if (value > addr)
13618 	    sym_hash->root.u.def.value -= count;
13619 	}
13620     }
13621 
13622   return true;
13623 }
13624 
13625 
13626 /* Opcodes needed for microMIPS relaxation as found in
13627    opcodes/micromips-opc.c.  */
13628 
13629 struct opcode_descriptor {
13630   unsigned long match;
13631   unsigned long mask;
13632 };
13633 
13634 /* The $ra register aka $31.  */
13635 
13636 #define RA 31
13637 
13638 /* 32-bit instruction format register fields.  */
13639 
13640 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13641 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13642 
13643 /* Check if a 5-bit register index can be abbreviated to 3 bits.  */
13644 
13645 #define OP16_VALID_REG(r) \
13646   ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13647 
13648 
13649 /* 32-bit and 16-bit branches.  */
13650 
13651 static const struct opcode_descriptor b_insns_32[] = {
13652   { /* "b",	"p",		*/ 0x40400000, 0xffff0000 }, /* bgez 0 */
13653   { /* "b",	"p",		*/ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13654   { 0, 0 }  /* End marker for find_match().  */
13655 };
13656 
13657 static const struct opcode_descriptor bc_insn_32 =
13658   { /* "bc(1|2)(ft)", "N,p",	*/ 0x42800000, 0xfec30000 };
13659 
13660 static const struct opcode_descriptor bz_insn_32 =
13661   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 };
13662 
13663 static const struct opcode_descriptor bzal_insn_32 =
13664   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 };
13665 
13666 static const struct opcode_descriptor beq_insn_32 =
13667   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 };
13668 
13669 static const struct opcode_descriptor b_insn_16 =
13670   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 };
13671 
13672 static const struct opcode_descriptor bz_insn_16 =
13673   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 };
13674 
13675 
13676 /* 32-bit and 16-bit branch EQ and NE zero.  */
13677 
13678 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13679    eq and second the ne.  This convention is used when replacing a
13680    32-bit BEQ/BNE with the 16-bit version.  */
13681 
13682 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13683 
13684 static const struct opcode_descriptor bz_rs_insns_32[] = {
13685   { /* "beqz",	"s,p",		*/ 0x94000000, 0xffe00000 },
13686   { /* "bnez",	"s,p",		*/ 0xb4000000, 0xffe00000 },
13687   { 0, 0 }  /* End marker for find_match().  */
13688 };
13689 
13690 static const struct opcode_descriptor bz_rt_insns_32[] = {
13691   { /* "beqz",	"t,p",		*/ 0x94000000, 0xfc01f000 },
13692   { /* "bnez",	"t,p",		*/ 0xb4000000, 0xfc01f000 },
13693   { 0, 0 }  /* End marker for find_match().  */
13694 };
13695 
13696 static const struct opcode_descriptor bzc_insns_32[] = {
13697   { /* "beqzc",	"s,p",		*/ 0x40e00000, 0xffe00000 },
13698   { /* "bnezc",	"s,p",		*/ 0x40a00000, 0xffe00000 },
13699   { 0, 0 }  /* End marker for find_match().  */
13700 };
13701 
13702 static const struct opcode_descriptor bz_insns_16[] = {
13703   { /* "beqz",	"md,mE",	*/ 0x8c00,     0xfc00 },
13704   { /* "bnez",	"md,mE",	*/ 0xac00,     0xfc00 },
13705   { 0, 0 }  /* End marker for find_match().  */
13706 };
13707 
13708 /* Switch between a 5-bit register index and its 3-bit shorthand.  */
13709 
13710 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13711 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13712 
13713 
13714 /* 32-bit instructions with a delay slot.  */
13715 
13716 static const struct opcode_descriptor jal_insn_32_bd16 =
13717   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 };
13718 
13719 static const struct opcode_descriptor jal_insn_32_bd32 =
13720   { /* "jal",	"a",		*/ 0xf4000000, 0xfc000000 };
13721 
13722 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13723   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 };
13724 
13725 static const struct opcode_descriptor j_insn_32 =
13726   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 };
13727 
13728 static const struct opcode_descriptor jalr_insn_32 =
13729   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff };
13730 
13731 /* This table can be compacted, because no opcode replacement is made.  */
13732 
13733 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13734   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 },
13735 
13736   { /* "jalrs[.hb]", "t,s",	*/ 0x00004f3c, 0xfc00efff },
13737   { /* "b(ge|lt)zals", "s,p",	*/ 0x42200000, 0xffa00000 },
13738 
13739   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 },
13740   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 },
13741   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 },
13742   { 0, 0 }  /* End marker for find_match().  */
13743 };
13744 
13745 /* This table can be compacted, because no opcode replacement is made.  */
13746 
13747 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13748   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 },
13749 
13750   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff },
13751   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 },
13752   { 0, 0 }  /* End marker for find_match().  */
13753 };
13754 
13755 
13756 /* 16-bit instructions with a delay slot.  */
13757 
13758 static const struct opcode_descriptor jalr_insn_16_bd16 =
13759   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 };
13760 
13761 static const struct opcode_descriptor jalr_insn_16_bd32 =
13762   { /* "jalr",	"my,mj",	*/ 0x45c0,     0xffe0 };
13763 
13764 static const struct opcode_descriptor jr_insn_16 =
13765   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 };
13766 
13767 #define JR16_REG(opcode) ((opcode) & 0x1f)
13768 
13769 /* This table can be compacted, because no opcode replacement is made.  */
13770 
13771 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13772   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 },
13773 
13774   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 },
13775   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 },
13776   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 },
13777   { 0, 0 }  /* End marker for find_match().  */
13778 };
13779 
13780 
13781 /* LUI instruction.  */
13782 
13783 static const struct opcode_descriptor lui_insn =
13784  { /* "lui",	"s,u",		*/ 0x41a00000, 0xffe00000 };
13785 
13786 
13787 /* ADDIU instruction.  */
13788 
13789 static const struct opcode_descriptor addiu_insn =
13790   { /* "addiu",	"t,r,j",	*/ 0x30000000, 0xfc000000 };
13791 
13792 static const struct opcode_descriptor addiupc_insn =
13793   { /* "addiu",	"mb,$pc,mQ",	*/ 0x78000000, 0xfc000000 };
13794 
13795 #define ADDIUPC_REG_FIELD(r) \
13796   (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13797 
13798 
13799 /* Relaxable instructions in a JAL delay slot: MOVE.  */
13800 
13801 /* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
13802    (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
13803 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13804 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13805 
13806 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13807 #define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
13808 
13809 static const struct opcode_descriptor move_insns_32[] = {
13810   { /* "move",	"d,s",		*/ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
13811   { /* "move",	"d,s",		*/ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13812   { 0, 0 }  /* End marker for find_match().  */
13813 };
13814 
13815 static const struct opcode_descriptor move_insn_16 =
13816   { /* "move",	"mp,mj",	*/ 0x0c00,     0xfc00 };
13817 
13818 
13819 /* NOP instructions.  */
13820 
13821 static const struct opcode_descriptor nop_insn_32 =
13822   { /* "nop",	"",		*/ 0x00000000, 0xffffffff };
13823 
13824 static const struct opcode_descriptor nop_insn_16 =
13825   { /* "nop",	"",		*/ 0x0c00,     0xffff };
13826 
13827 
13828 /* Instruction match support.  */
13829 
13830 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13831 
13832 static int
13833 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13834 {
13835   unsigned long indx;
13836 
13837   for (indx = 0; insn[indx].mask != 0; indx++)
13838     if (MATCH (opcode, insn[indx]))
13839       return indx;
13840 
13841   return -1;
13842 }
13843 
13844 
13845 /* Branch and delay slot decoding support.  */
13846 
13847 /* If PTR points to what *might* be a 16-bit branch or jump, then
13848    return the minimum length of its delay slot, otherwise return 0.
13849    Non-zero results are not definitive as we might be checking against
13850    the second half of another instruction.  */
13851 
13852 static int
13853 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13854 {
13855   unsigned long opcode;
13856   int bdsize;
13857 
13858   opcode = bfd_get_16 (abfd, ptr);
13859   if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13860     /* 16-bit branch/jump with a 32-bit delay slot.  */
13861     bdsize = 4;
13862   else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13863 	   || find_match (opcode, ds_insns_16_bd16) >= 0)
13864     /* 16-bit branch/jump with a 16-bit delay slot.  */
13865     bdsize = 2;
13866   else
13867     /* No delay slot.  */
13868     bdsize = 0;
13869 
13870   return bdsize;
13871 }
13872 
13873 /* If PTR points to what *might* be a 32-bit branch or jump, then
13874    return the minimum length of its delay slot, otherwise return 0.
13875    Non-zero results are not definitive as we might be checking against
13876    the second half of another instruction.  */
13877 
13878 static int
13879 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13880 {
13881   unsigned long opcode;
13882   int bdsize;
13883 
13884   opcode = bfd_get_micromips_32 (abfd, ptr);
13885   if (find_match (opcode, ds_insns_32_bd32) >= 0)
13886     /* 32-bit branch/jump with a 32-bit delay slot.  */
13887     bdsize = 4;
13888   else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13889     /* 32-bit branch/jump with a 16-bit delay slot.  */
13890     bdsize = 2;
13891   else
13892     /* No delay slot.  */
13893     bdsize = 0;
13894 
13895   return bdsize;
13896 }
13897 
13898 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13899    that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
13900 
13901 static bool
13902 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13903 {
13904   unsigned long opcode;
13905 
13906   opcode = bfd_get_16 (abfd, ptr);
13907   if (MATCH (opcode, b_insn_16)
13908 						/* B16  */
13909       || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13910 						/* JR16  */
13911       || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13912 						/* BEQZ16, BNEZ16  */
13913       || (MATCH (opcode, jalr_insn_16_bd32)
13914 						/* JALR16  */
13915 	  && reg != JR16_REG (opcode) && reg != RA))
13916     return true;
13917 
13918   return false;
13919 }
13920 
13921 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13922    then return TRUE, otherwise FALSE.  */
13923 
13924 static bool
13925 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13926 {
13927   unsigned long opcode;
13928 
13929   opcode = bfd_get_micromips_32 (abfd, ptr);
13930   if (MATCH (opcode, j_insn_32)
13931 						/* J  */
13932       || MATCH (opcode, bc_insn_32)
13933 						/* BC1F, BC1T, BC2F, BC2T  */
13934       || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13935 						/* JAL, JALX  */
13936       || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13937 						/* BGEZ, BGTZ, BLEZ, BLTZ  */
13938       || (MATCH (opcode, bzal_insn_32)
13939 						/* BGEZAL, BLTZAL  */
13940 	  && reg != OP32_SREG (opcode) && reg != RA)
13941       || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13942 						/* JALR, JALR.HB, BEQ, BNE  */
13943 	  && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13944     return true;
13945 
13946   return false;
13947 }
13948 
13949 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13950    IRELEND) at OFFSET indicate that there must be a compact branch there,
13951    then return TRUE, otherwise FALSE.  */
13952 
13953 static bool
13954 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13955 		     const Elf_Internal_Rela *internal_relocs,
13956 		     const Elf_Internal_Rela *irelend)
13957 {
13958   const Elf_Internal_Rela *irel;
13959   unsigned long opcode;
13960 
13961   opcode = bfd_get_micromips_32 (abfd, ptr);
13962   if (find_match (opcode, bzc_insns_32) < 0)
13963     return false;
13964 
13965   for (irel = internal_relocs; irel < irelend; irel++)
13966     if (irel->r_offset == offset
13967 	&& ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13968       return true;
13969 
13970   return false;
13971 }
13972 
13973 /* Bitsize checking.  */
13974 #define IS_BITSIZE(val, N)						\
13975   (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))		\
13976     - (1ULL << ((N) - 1))) == (val))
13977 
13978 
13979 bool
13980 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13981 			     struct bfd_link_info *link_info,
13982 			     bool *again)
13983 {
13984   bool insn32 = mips_elf_hash_table (link_info)->insn32;
13985   Elf_Internal_Shdr *symtab_hdr;
13986   Elf_Internal_Rela *internal_relocs;
13987   Elf_Internal_Rela *irel, *irelend;
13988   bfd_byte *contents = NULL;
13989   Elf_Internal_Sym *isymbuf = NULL;
13990 
13991   /* Assume nothing changes.  */
13992   *again = false;
13993 
13994   /* We don't have to do anything for a relocatable link, if
13995      this section does not have relocs, or if this is not a
13996      code section.  */
13997 
13998   if (bfd_link_relocatable (link_info)
13999       || sec->reloc_count == 0
14000       || (sec->flags & SEC_RELOC) == 0
14001       || (sec->flags & SEC_HAS_CONTENTS) == 0
14002       || (sec->flags & SEC_CODE) == 0)
14003     return true;
14004 
14005   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14006 
14007   /* Get a copy of the native relocations.  */
14008   internal_relocs = (_bfd_elf_link_read_relocs
14009 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14010 		      link_info->keep_memory));
14011   if (internal_relocs == NULL)
14012     goto error_return;
14013 
14014   /* Walk through them looking for relaxing opportunities.  */
14015   irelend = internal_relocs + sec->reloc_count;
14016   for (irel = internal_relocs; irel < irelend; irel++)
14017     {
14018       unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14019       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14020       bool target_is_micromips_code_p;
14021       unsigned long opcode;
14022       bfd_vma symval;
14023       bfd_vma pcrval;
14024       bfd_byte *ptr;
14025       int fndopc;
14026 
14027       /* The number of bytes to delete for relaxation and from where
14028 	 to delete these bytes starting at irel->r_offset.  */
14029       int delcnt = 0;
14030       int deloff = 0;
14031 
14032       /* If this isn't something that can be relaxed, then ignore
14033 	 this reloc.  */
14034       if (r_type != R_MICROMIPS_HI16
14035 	  && r_type != R_MICROMIPS_PC16_S1
14036 	  && r_type != R_MICROMIPS_26_S1)
14037 	continue;
14038 
14039       /* Get the section contents if we haven't done so already.  */
14040       if (contents == NULL)
14041 	{
14042 	  /* Get cached copy if it exists.  */
14043 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
14044 	    contents = elf_section_data (sec)->this_hdr.contents;
14045 	  /* Go get them off disk.  */
14046 	  else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14047 	    goto error_return;
14048 	}
14049       ptr = contents + irel->r_offset;
14050 
14051       /* Read this BFD's local symbols if we haven't done so already.  */
14052       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14053 	{
14054 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14055 	  if (isymbuf == NULL)
14056 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14057 					    symtab_hdr->sh_info, 0,
14058 					    NULL, NULL, NULL);
14059 	  if (isymbuf == NULL)
14060 	    goto error_return;
14061 	}
14062 
14063       /* Get the value of the symbol referred to by the reloc.  */
14064       if (r_symndx < symtab_hdr->sh_info)
14065 	{
14066 	  /* A local symbol.  */
14067 	  Elf_Internal_Sym *isym;
14068 	  asection *sym_sec;
14069 
14070 	  isym = isymbuf + r_symndx;
14071 	  if (isym->st_shndx == SHN_UNDEF)
14072 	    sym_sec = bfd_und_section_ptr;
14073 	  else if (isym->st_shndx == SHN_ABS)
14074 	    sym_sec = bfd_abs_section_ptr;
14075 	  else if (isym->st_shndx == SHN_COMMON)
14076 	    sym_sec = bfd_com_section_ptr;
14077 	  else
14078 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14079 	  symval = (isym->st_value
14080 		    + sym_sec->output_section->vma
14081 		    + sym_sec->output_offset);
14082 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14083 	}
14084       else
14085 	{
14086 	  unsigned long indx;
14087 	  struct elf_link_hash_entry *h;
14088 
14089 	  /* An external symbol.  */
14090 	  indx = r_symndx - symtab_hdr->sh_info;
14091 	  h = elf_sym_hashes (abfd)[indx];
14092 	  BFD_ASSERT (h != NULL);
14093 
14094 	  if (h->root.type != bfd_link_hash_defined
14095 	      && h->root.type != bfd_link_hash_defweak)
14096 	    /* This appears to be a reference to an undefined
14097 	       symbol.  Just ignore it -- it will be caught by the
14098 	       regular reloc processing.  */
14099 	    continue;
14100 
14101 	  symval = (h->root.u.def.value
14102 		    + h->root.u.def.section->output_section->vma
14103 		    + h->root.u.def.section->output_offset);
14104 	  target_is_micromips_code_p = (!h->needs_plt
14105 					&& ELF_ST_IS_MICROMIPS (h->other));
14106 	}
14107 
14108 
14109       /* For simplicity of coding, we are going to modify the
14110 	 section contents, the section relocs, and the BFD symbol
14111 	 table.  We must tell the rest of the code not to free up this
14112 	 information.  It would be possible to instead create a table
14113 	 of changes which have to be made, as is done in coff-mips.c;
14114 	 that would be more work, but would require less memory when
14115 	 the linker is run.  */
14116 
14117       /* Only 32-bit instructions relaxed.  */
14118       if (irel->r_offset + 4 > sec->size)
14119 	continue;
14120 
14121       opcode = bfd_get_micromips_32 (abfd, ptr);
14122 
14123       /* This is the pc-relative distance from the instruction the
14124 	 relocation is applied to, to the symbol referred.  */
14125       pcrval = (symval
14126 		- (sec->output_section->vma + sec->output_offset)
14127 		- irel->r_offset);
14128 
14129       /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14130 	 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14131 	 R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
14132 
14133 	   (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14134 
14135 	 where pcrval has first to be adjusted to apply against the LO16
14136 	 location (we make the adjustment later on, when we have figured
14137 	 out the offset).  */
14138       if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14139 	{
14140 	  bool bzc = false;
14141 	  unsigned long nextopc;
14142 	  unsigned long reg;
14143 	  bfd_vma offset;
14144 
14145 	  /* Give up if the previous reloc was a HI16 against this symbol
14146 	     too.  */
14147 	  if (irel > internal_relocs
14148 	      && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14149 	      && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14150 	    continue;
14151 
14152 	  /* Or if the next reloc is not a LO16 against this symbol.  */
14153 	  if (irel + 1 >= irelend
14154 	      || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14155 	      || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14156 	    continue;
14157 
14158 	  /* Or if the second next reloc is a LO16 against this symbol too.  */
14159 	  if (irel + 2 >= irelend
14160 	      && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14161 	      && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14162 	    continue;
14163 
14164 	  /* See if the LUI instruction *might* be in a branch delay slot.
14165 	     We check whether what looks like a 16-bit branch or jump is
14166 	     actually an immediate argument to a compact branch, and let
14167 	     it through if so.  */
14168 	  if (irel->r_offset >= 2
14169 	      && check_br16_dslot (abfd, ptr - 2)
14170 	      && !(irel->r_offset >= 4
14171 		   && (bzc = check_relocated_bzc (abfd,
14172 						  ptr - 4, irel->r_offset - 4,
14173 						  internal_relocs, irelend))))
14174 	    continue;
14175 	  if (irel->r_offset >= 4
14176 	      && !bzc
14177 	      && check_br32_dslot (abfd, ptr - 4))
14178 	    continue;
14179 
14180 	  reg = OP32_SREG (opcode);
14181 
14182 	  /* We only relax adjacent instructions or ones separated with
14183 	     a branch or jump that has a delay slot.  The branch or jump
14184 	     must not fiddle with the register used to hold the address.
14185 	     Subtract 4 for the LUI itself.  */
14186 	  offset = irel[1].r_offset - irel[0].r_offset;
14187 	  switch (offset - 4)
14188 	    {
14189 	    case 0:
14190 	      break;
14191 	    case 2:
14192 	      if (check_br16 (abfd, ptr + 4, reg))
14193 		break;
14194 	      continue;
14195 	    case 4:
14196 	      if (check_br32 (abfd, ptr + 4, reg))
14197 		break;
14198 	      continue;
14199 	    default:
14200 	      continue;
14201 	    }
14202 
14203 	  nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14204 
14205 	  /* Give up unless the same register is used with both
14206 	     relocations.  */
14207 	  if (OP32_SREG (nextopc) != reg)
14208 	    continue;
14209 
14210 	  /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14211 	     and rounding up to take masking of the two LSBs into account.  */
14212 	  pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14213 
14214 	  /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
14215 	  if (IS_BITSIZE (symval, 16))
14216 	    {
14217 	      /* Fix the relocation's type.  */
14218 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14219 
14220 	      /* Instructions using R_MICROMIPS_LO16 have the base or
14221 		 source register in bits 20:16.  This register becomes $0
14222 		 (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
14223 	      nextopc &= ~0x001f0000;
14224 	      bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14225 			  contents + irel[1].r_offset);
14226 	    }
14227 
14228 	  /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14229 	     We add 4 to take LUI deletion into account while checking
14230 	     the PC-relative distance.  */
14231 	  else if (symval % 4 == 0
14232 		   && IS_BITSIZE (pcrval + 4, 25)
14233 		   && MATCH (nextopc, addiu_insn)
14234 		   && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14235 		   && OP16_VALID_REG (OP32_TREG (nextopc)))
14236 	    {
14237 	      /* Fix the relocation's type.  */
14238 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14239 
14240 	      /* Replace ADDIU with the ADDIUPC version.  */
14241 	      nextopc = (addiupc_insn.match
14242 			 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14243 
14244 	      bfd_put_micromips_32 (abfd, nextopc,
14245 				    contents + irel[1].r_offset);
14246 	    }
14247 
14248 	  /* Can't do anything, give up, sigh...  */
14249 	  else
14250 	    continue;
14251 
14252 	  /* Fix the relocation's type.  */
14253 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14254 
14255 	  /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
14256 	  delcnt = 4;
14257 	  deloff = 0;
14258 	}
14259 
14260       /* Compact branch relaxation -- due to the multitude of macros
14261 	 employed by the compiler/assembler, compact branches are not
14262 	 always generated.  Obviously, this can/will be fixed elsewhere,
14263 	 but there is no drawback in double checking it here.  */
14264       else if (r_type == R_MICROMIPS_PC16_S1
14265 	       && irel->r_offset + 5 < sec->size
14266 	       && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14267 		   || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14268 	       && ((!insn32
14269 		    && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14270 					nop_insn_16) ? 2 : 0))
14271 		   || (irel->r_offset + 7 < sec->size
14272 		       && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14273 								 ptr + 4),
14274 					   nop_insn_32) ? 4 : 0))))
14275 	{
14276 	  unsigned long reg;
14277 
14278 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14279 
14280 	  /* Replace BEQZ/BNEZ with the compact version.  */
14281 	  opcode = (bzc_insns_32[fndopc].match
14282 		    | BZC32_REG_FIELD (reg)
14283 		    | (opcode & 0xffff));		/* Addend value.  */
14284 
14285 	  bfd_put_micromips_32 (abfd, opcode, ptr);
14286 
14287 	  /* Delete the delay slot NOP: two or four bytes from
14288 	     irel->offset + 4; delcnt has already been set above.  */
14289 	  deloff = 4;
14290 	}
14291 
14292       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
14293 	 to check the distance from the next instruction, so subtract 2.  */
14294       else if (!insn32
14295 	       && r_type == R_MICROMIPS_PC16_S1
14296 	       && IS_BITSIZE (pcrval - 2, 11)
14297 	       && find_match (opcode, b_insns_32) >= 0)
14298 	{
14299 	  /* Fix the relocation's type.  */
14300 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14301 
14302 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
14303 	  bfd_put_16 (abfd,
14304 		      (b_insn_16.match
14305 		       | (opcode & 0x3ff)),		/* Addend value.  */
14306 		      ptr);
14307 
14308 	  /* Delete 2 bytes from irel->r_offset + 2.  */
14309 	  delcnt = 2;
14310 	  deloff = 2;
14311 	}
14312 
14313       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
14314 	 to check the distance from the next instruction, so subtract 2.  */
14315       else if (!insn32
14316 	       && r_type == R_MICROMIPS_PC16_S1
14317 	       && IS_BITSIZE (pcrval - 2, 8)
14318 	       && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14319 		    && OP16_VALID_REG (OP32_SREG (opcode)))
14320 		   || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14321 		       && OP16_VALID_REG (OP32_TREG (opcode)))))
14322 	{
14323 	  unsigned long reg;
14324 
14325 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14326 
14327 	  /* Fix the relocation's type.  */
14328 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14329 
14330 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
14331 	  bfd_put_16 (abfd,
14332 		      (bz_insns_16[fndopc].match
14333 		       | BZ16_REG_FIELD (reg)
14334 		       | (opcode & 0x7f)),		/* Addend value.  */
14335 		      ptr);
14336 
14337 	  /* Delete 2 bytes from irel->r_offset + 2.  */
14338 	  delcnt = 2;
14339 	  deloff = 2;
14340 	}
14341 
14342       /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
14343       else if (!insn32
14344 	       && r_type == R_MICROMIPS_26_S1
14345 	       && target_is_micromips_code_p
14346 	       && irel->r_offset + 7 < sec->size
14347 	       && MATCH (opcode, jal_insn_32_bd32))
14348 	{
14349 	  unsigned long n32opc;
14350 	  bool relaxed = false;
14351 
14352 	  n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14353 
14354 	  if (MATCH (n32opc, nop_insn_32))
14355 	    {
14356 	      /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
14357 	      bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14358 
14359 	      relaxed = true;
14360 	    }
14361 	  else if (find_match (n32opc, move_insns_32) >= 0)
14362 	    {
14363 	      /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
14364 	      bfd_put_16 (abfd,
14365 			  (move_insn_16.match
14366 			   | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14367 			   | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14368 			  ptr + 4);
14369 
14370 	      relaxed = true;
14371 	    }
14372 	  /* Other 32-bit instructions relaxable to 16-bit
14373 	     instructions will be handled here later.  */
14374 
14375 	  if (relaxed)
14376 	    {
14377 	      /* JAL with 32-bit delay slot that is changed to a JALS
14378 		 with 16-bit delay slot.  */
14379 	      bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14380 
14381 	      /* Delete 2 bytes from irel->r_offset + 6.  */
14382 	      delcnt = 2;
14383 	      deloff = 6;
14384 	    }
14385 	}
14386 
14387       if (delcnt != 0)
14388 	{
14389 	  /* Note that we've changed the relocs, section contents, etc.  */
14390 	  elf_section_data (sec)->relocs = internal_relocs;
14391 	  elf_section_data (sec)->this_hdr.contents = contents;
14392 	  symtab_hdr->contents = (unsigned char *) isymbuf;
14393 
14394 	  /* Delete bytes depending on the delcnt and deloff.  */
14395 	  if (!mips_elf_relax_delete_bytes (abfd, sec,
14396 					    irel->r_offset + deloff, delcnt))
14397 	    goto error_return;
14398 
14399 	  /* That will change things, so we should relax again.
14400 	     Note that this is not required, and it may be slow.  */
14401 	  *again = true;
14402 	}
14403     }
14404 
14405   if (isymbuf != NULL
14406       && symtab_hdr->contents != (unsigned char *) isymbuf)
14407     {
14408       if (! link_info->keep_memory)
14409 	free (isymbuf);
14410       else
14411 	{
14412 	  /* Cache the symbols for elf_link_input_bfd.  */
14413 	  symtab_hdr->contents = (unsigned char *) isymbuf;
14414 	}
14415     }
14416 
14417   if (contents != NULL
14418       && elf_section_data (sec)->this_hdr.contents != contents)
14419     {
14420       if (! link_info->keep_memory)
14421 	free (contents);
14422       else
14423 	{
14424 	  /* Cache the section contents for elf_link_input_bfd.  */
14425 	  elf_section_data (sec)->this_hdr.contents = contents;
14426 	}
14427     }
14428 
14429   if (elf_section_data (sec)->relocs != internal_relocs)
14430     free (internal_relocs);
14431 
14432   return true;
14433 
14434  error_return:
14435   if (symtab_hdr->contents != (unsigned char *) isymbuf)
14436     free (isymbuf);
14437   if (elf_section_data (sec)->this_hdr.contents != contents)
14438     free (contents);
14439   if (elf_section_data (sec)->relocs != internal_relocs)
14440     free (internal_relocs);
14441 
14442   return false;
14443 }
14444 
14445 /* Create a MIPS ELF linker hash table.  */
14446 
14447 struct bfd_link_hash_table *
14448 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14449 {
14450   struct mips_elf_link_hash_table *ret;
14451   size_t amt = sizeof (struct mips_elf_link_hash_table);
14452 
14453   ret = bfd_zmalloc (amt);
14454   if (ret == NULL)
14455     return NULL;
14456 
14457   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14458 				      mips_elf_link_hash_newfunc,
14459 				      sizeof (struct mips_elf_link_hash_entry),
14460 				      MIPS_ELF_DATA))
14461     {
14462       free (ret);
14463       return NULL;
14464     }
14465   ret->root.init_plt_refcount.plist = NULL;
14466   ret->root.init_plt_offset.plist = NULL;
14467 
14468   return &ret->root.root;
14469 }
14470 
14471 /* Likewise, but indicate that the target is VxWorks.  */
14472 
14473 struct bfd_link_hash_table *
14474 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14475 {
14476   struct bfd_link_hash_table *ret;
14477 
14478   ret = _bfd_mips_elf_link_hash_table_create (abfd);
14479   if (ret)
14480     {
14481       struct mips_elf_link_hash_table *htab;
14482 
14483       htab = (struct mips_elf_link_hash_table *) ret;
14484       htab->use_plts_and_copy_relocs = true;
14485     }
14486   return ret;
14487 }
14488 
14489 /* A function that the linker calls if we are allowed to use PLTs
14490    and copy relocs.  */
14491 
14492 void
14493 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14494 {
14495   mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14496 }
14497 
14498 /* A function that the linker calls to select between all or only
14499    32-bit microMIPS instructions, and between making or ignoring
14500    branch relocation checks for invalid transitions between ISA modes.
14501    Also record whether we have been configured for a GNU target.  */
14502 
14503 void
14504 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14505 			    bool ignore_branch_isa,
14506 			    bool gnu_target)
14507 {
14508   mips_elf_hash_table (info)->insn32 = insn32;
14509   mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14510   mips_elf_hash_table (info)->gnu_target = gnu_target;
14511 }
14512 
14513 /* A function that the linker calls to enable use of compact branches in
14514    linker generated code for MIPSR6.  */
14515 
14516 void
14517 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14518 {
14519   mips_elf_hash_table (info)->compact_branches = on;
14520 }
14521 
14522 
14523 /* Structure for saying that BFD machine EXTENSION extends BASE.  */
14524 
14525 struct mips_mach_extension
14526 {
14527   unsigned long extension, base;
14528 };
14529 
14530 /* An array that maps 64-bit architectures to the corresponding 32-bit
14531    architectures.  */
14532 static const struct mips_mach_extension mips_mach_32_64[] =
14533 {
14534   { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14535   { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14536   { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14537   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14538   { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
14539 };
14540 
14541 /* An array describing how BFD machines relate to one another.  The entries
14542    are ordered topologically with MIPS I extensions listed last.  */
14543 
14544 static const struct mips_mach_extension mips_mach_extensions[] =
14545 {
14546   /* MIPS64r2 extensions.  */
14547   { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14548   { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14549   { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14550   { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14551   { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14552   { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14553   { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14554 
14555   /* MIPS64 extensions.  */
14556   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14557   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14558   { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14559 
14560   /* MIPS V extensions.  */
14561   { bfd_mach_mipsisa64, bfd_mach_mips5 },
14562 
14563   /* R10000 extensions.  */
14564   { bfd_mach_mips12000, bfd_mach_mips10000 },
14565   { bfd_mach_mips14000, bfd_mach_mips10000 },
14566   { bfd_mach_mips16000, bfd_mach_mips10000 },
14567 
14568   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
14569      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
14570      better to allow vr5400 and vr5500 code to be merged anyway, since
14571      many libraries will just use the core ISA.  Perhaps we could add
14572      some sort of ASE flag if this ever proves a problem.  */
14573   { bfd_mach_mips5500, bfd_mach_mips5400 },
14574   { bfd_mach_mips5400, bfd_mach_mips5000 },
14575 
14576   /* MIPS IV extensions.  */
14577   { bfd_mach_mips5, bfd_mach_mips8000 },
14578   { bfd_mach_mips10000, bfd_mach_mips8000 },
14579   { bfd_mach_mips5000, bfd_mach_mips8000 },
14580   { bfd_mach_mips7000, bfd_mach_mips8000 },
14581   { bfd_mach_mips9000, bfd_mach_mips8000 },
14582 
14583   /* VR4100 extensions.  */
14584   { bfd_mach_mips4120, bfd_mach_mips4100 },
14585   { bfd_mach_mips4111, bfd_mach_mips4100 },
14586 
14587   /* MIPS III extensions.  */
14588   { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14589   { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14590   { bfd_mach_mips8000, bfd_mach_mips4000 },
14591   { bfd_mach_mips4650, bfd_mach_mips4000 },
14592   { bfd_mach_mips4600, bfd_mach_mips4000 },
14593   { bfd_mach_mips4400, bfd_mach_mips4000 },
14594   { bfd_mach_mips4300, bfd_mach_mips4000 },
14595   { bfd_mach_mips4100, bfd_mach_mips4000 },
14596   { bfd_mach_mips5900, bfd_mach_mips4000 },
14597 
14598   /* MIPS32r3 extensions.  */
14599   { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14600 
14601   /* MIPS32r2 extensions.  */
14602   { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14603 
14604   /* MIPS32 extensions.  */
14605   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14606 
14607   /* MIPS II extensions.  */
14608   { bfd_mach_mips4000, bfd_mach_mips6000 },
14609   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14610   { bfd_mach_mips4010, bfd_mach_mips6000 },
14611   { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14612 
14613   /* MIPS I extensions.  */
14614   { bfd_mach_mips6000, bfd_mach_mips3000 },
14615   { bfd_mach_mips3900, bfd_mach_mips3000 }
14616 };
14617 
14618 /* Return true if bfd machine EXTENSION is the same as BASE, or if
14619    EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
14620 
14621 static bool
14622 mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14623 {
14624   size_t i;
14625 
14626   if (extension == base)
14627     return true;
14628 
14629   for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14630     if (extension == mips_mach_32_64[i].extension)
14631       return base == mips_mach_32_64[i].base;
14632 
14633   return false;
14634 }
14635 
14636 static bool
14637 mips_mach_extends_p (unsigned long base, unsigned long extension)
14638 {
14639   size_t i;
14640 
14641   if (mips_mach_extends_32_64 (base, extension))
14642     return true;
14643 
14644   for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14645     if (extension == mips_mach_extensions[i].extension)
14646       {
14647 	extension = mips_mach_extensions[i].base;
14648 	if (mips_mach_extends_32_64 (base, extension))
14649 	  return true;
14650       }
14651 
14652   return false;
14653 }
14654 
14655 /* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
14656 
14657 static unsigned long
14658 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14659 {
14660   switch (isa_ext)
14661     {
14662     case AFL_EXT_3900:	      return bfd_mach_mips3900;
14663     case AFL_EXT_4010:	      return bfd_mach_mips4010;
14664     case AFL_EXT_4100:	      return bfd_mach_mips4100;
14665     case AFL_EXT_4111:	      return bfd_mach_mips4111;
14666     case AFL_EXT_4120:	      return bfd_mach_mips4120;
14667     case AFL_EXT_4650:	      return bfd_mach_mips4650;
14668     case AFL_EXT_5400:	      return bfd_mach_mips5400;
14669     case AFL_EXT_5500:	      return bfd_mach_mips5500;
14670     case AFL_EXT_5900:	      return bfd_mach_mips5900;
14671     case AFL_EXT_10000:	      return bfd_mach_mips10000;
14672     case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14673     case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14674     case AFL_EXT_SB1:	      return bfd_mach_mips_sb1;
14675     case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
14676     case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
14677     case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
14678     case AFL_EXT_XLR:	      return bfd_mach_mips_xlr;
14679     default:		      return bfd_mach_mips3000;
14680     }
14681 }
14682 
14683 /* Return the .MIPS.abiflags value representing each ISA Extension.  */
14684 
14685 unsigned int
14686 bfd_mips_isa_ext (bfd *abfd)
14687 {
14688   switch (bfd_get_mach (abfd))
14689     {
14690     case bfd_mach_mips3900:	    return AFL_EXT_3900;
14691     case bfd_mach_mips4010:	    return AFL_EXT_4010;
14692     case bfd_mach_mips4100:	    return AFL_EXT_4100;
14693     case bfd_mach_mips4111:	    return AFL_EXT_4111;
14694     case bfd_mach_mips4120:	    return AFL_EXT_4120;
14695     case bfd_mach_mips4650:	    return AFL_EXT_4650;
14696     case bfd_mach_mips5400:	    return AFL_EXT_5400;
14697     case bfd_mach_mips5500:	    return AFL_EXT_5500;
14698     case bfd_mach_mips5900:	    return AFL_EXT_5900;
14699     case bfd_mach_mips10000:	    return AFL_EXT_10000;
14700     case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14701     case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14702     case bfd_mach_mips_sb1:	    return AFL_EXT_SB1;
14703     case bfd_mach_mips_octeon:	    return AFL_EXT_OCTEON;
14704     case bfd_mach_mips_octeonp:	    return AFL_EXT_OCTEONP;
14705     case bfd_mach_mips_octeon3:	    return AFL_EXT_OCTEON3;
14706     case bfd_mach_mips_octeon2:	    return AFL_EXT_OCTEON2;
14707     case bfd_mach_mips_xlr:	    return AFL_EXT_XLR;
14708     case bfd_mach_mips_interaptiv_mr2:
14709       return AFL_EXT_INTERAPTIV_MR2;
14710     default:			    return 0;
14711     }
14712 }
14713 
14714 /* Encode ISA level and revision as a single value.  */
14715 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14716 
14717 /* Decode a single value into level and revision.  */
14718 #define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
14719 #define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
14720 
14721 /* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
14722 
14723 static void
14724 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14725 {
14726   int new_isa = 0;
14727   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14728     {
14729     case EF_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
14730     case EF_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
14731     case EF_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
14732     case EF_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
14733     case EF_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
14734     case EF_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
14735     case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14736     case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14737     case EF_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
14738     case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14739     case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14740     default:
14741       _bfd_error_handler
14742 	/* xgettext:c-format */
14743 	(_("%pB: unknown architecture %s"),
14744 	 abfd, bfd_printable_name (abfd));
14745     }
14746 
14747   if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14748     {
14749       abiflags->isa_level = ISA_LEVEL (new_isa);
14750       abiflags->isa_rev = ISA_REV (new_isa);
14751     }
14752 
14753   /* Update the isa_ext if ABFD describes a further extension.  */
14754   if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14755 			   bfd_get_mach (abfd)))
14756     abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14757 }
14758 
14759 /* Return true if the given ELF header flags describe a 32-bit binary.  */
14760 
14761 static bool
14762 mips_32bit_flags_p (flagword flags)
14763 {
14764   return ((flags & EF_MIPS_32BITMODE) != 0
14765 	  || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
14766 	  || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
14767 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
14768 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
14769 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
14770 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
14771 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
14772 }
14773 
14774 /* Infer the content of the ABI flags based on the elf header.  */
14775 
14776 static void
14777 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14778 {
14779   obj_attribute *in_attr;
14780 
14781   memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14782   update_mips_abiflags_isa (abfd, abiflags);
14783 
14784   if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14785     abiflags->gpr_size = AFL_REG_32;
14786   else
14787     abiflags->gpr_size = AFL_REG_64;
14788 
14789   abiflags->cpr1_size = AFL_REG_NONE;
14790 
14791   in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14792   abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14793 
14794   if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14795       || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14796       || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14797 	  && abiflags->gpr_size == AFL_REG_32))
14798     abiflags->cpr1_size = AFL_REG_32;
14799   else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14800 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14801 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14802     abiflags->cpr1_size = AFL_REG_64;
14803 
14804   abiflags->cpr2_size = AFL_REG_NONE;
14805 
14806   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14807     abiflags->ases |= AFL_ASE_MDMX;
14808   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14809     abiflags->ases |= AFL_ASE_MIPS16;
14810   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14811     abiflags->ases |= AFL_ASE_MICROMIPS;
14812 
14813   if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14814       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14815       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14816       && abiflags->isa_level >= 32
14817       && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14818     abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14819 }
14820 
14821 /* We need to use a special link routine to handle the .reginfo and
14822    the .mdebug sections.  We need to merge all instances of these
14823    sections together, not write them all out sequentially.  */
14824 
14825 bool
14826 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14827 {
14828   asection *o;
14829   struct bfd_link_order *p;
14830   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14831   asection *rtproc_sec, *abiflags_sec;
14832   Elf32_RegInfo reginfo;
14833   struct ecoff_debug_info debug;
14834   struct mips_htab_traverse_info hti;
14835   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14836   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14837   HDRR *symhdr = &debug.symbolic_header;
14838   void *mdebug_handle = NULL;
14839   asection *s;
14840   EXTR esym;
14841   unsigned int i;
14842   bfd_size_type amt;
14843   struct mips_elf_link_hash_table *htab;
14844 
14845   static const char * const secname[] =
14846   {
14847     ".text", ".init", ".fini", ".data",
14848     ".rodata", ".sdata", ".sbss", ".bss"
14849   };
14850   static const int sc[] =
14851   {
14852     scText, scInit, scFini, scData,
14853     scRData, scSData, scSBss, scBss
14854   };
14855 
14856   htab = mips_elf_hash_table (info);
14857   BFD_ASSERT (htab != NULL);
14858 
14859   /* Sort the dynamic symbols so that those with GOT entries come after
14860      those without.  */
14861   if (!mips_elf_sort_hash_table (abfd, info))
14862     return false;
14863 
14864   /* Create any scheduled LA25 stubs.  */
14865   hti.info = info;
14866   hti.output_bfd = abfd;
14867   hti.error = false;
14868   htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14869   if (hti.error)
14870     return false;
14871 
14872   /* Get a value for the GP register.  */
14873   if (elf_gp (abfd) == 0)
14874     {
14875       struct bfd_link_hash_entry *h;
14876 
14877       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14878       if (h != NULL && h->type == bfd_link_hash_defined)
14879 	elf_gp (abfd) = (h->u.def.value
14880 			 + h->u.def.section->output_section->vma
14881 			 + h->u.def.section->output_offset);
14882       else if (htab->root.target_os == is_vxworks
14883 	       && (h = bfd_link_hash_lookup (info->hash,
14884 					     "_GLOBAL_OFFSET_TABLE_",
14885 					     false, false, true))
14886 	       && h->type == bfd_link_hash_defined)
14887 	elf_gp (abfd) = (h->u.def.section->output_section->vma
14888 			 + h->u.def.section->output_offset
14889 			 + h->u.def.value);
14890       else if (bfd_link_relocatable (info))
14891 	{
14892 	  bfd_vma lo = MINUS_ONE;
14893 
14894 	  /* Find the GP-relative section with the lowest offset.  */
14895 	  for (o = abfd->sections; o != NULL; o = o->next)
14896 	    if (o->vma < lo
14897 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14898 	      lo = o->vma;
14899 
14900 	  /* And calculate GP relative to that.  */
14901 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14902 	}
14903       else
14904 	{
14905 	  /* If the relocate_section function needs to do a reloc
14906 	     involving the GP value, it should make a reloc_dangerous
14907 	     callback to warn that GP is not defined.  */
14908 	}
14909     }
14910 
14911   /* Go through the sections and collect the .reginfo and .mdebug
14912      information.  */
14913   abiflags_sec = NULL;
14914   reginfo_sec = NULL;
14915   mdebug_sec = NULL;
14916   gptab_data_sec = NULL;
14917   gptab_bss_sec = NULL;
14918   for (o = abfd->sections; o != NULL; o = o->next)
14919     {
14920       if (strcmp (o->name, ".MIPS.abiflags") == 0)
14921 	{
14922 	  /* We have found the .MIPS.abiflags section in the output file.
14923 	     Look through all the link_orders comprising it and remove them.
14924 	     The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
14925 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
14926 	    {
14927 	      asection *input_section;
14928 
14929 	      if (p->type != bfd_indirect_link_order)
14930 		{
14931 		  if (p->type == bfd_data_link_order)
14932 		    continue;
14933 		  abort ();
14934 		}
14935 
14936 	      input_section = p->u.indirect.section;
14937 
14938 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
14939 		 elf_link_input_bfd ignores this section.  */
14940 	      input_section->flags &= ~SEC_HAS_CONTENTS;
14941 	    }
14942 
14943 	  /* Size has been set in _bfd_mips_elf_late_size_sections.  */
14944 	  BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14945 
14946 	  /* Skip this section later on (I don't think this currently
14947 	     matters, but someday it might).  */
14948 	  o->map_head.link_order = NULL;
14949 
14950 	  abiflags_sec = o;
14951 	}
14952 
14953       if (strcmp (o->name, ".reginfo") == 0)
14954 	{
14955 	  memset (&reginfo, 0, sizeof reginfo);
14956 
14957 	  /* We have found the .reginfo section in the output file.
14958 	     Look through all the link_orders comprising it and merge
14959 	     the information together.  */
14960 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
14961 	    {
14962 	      asection *input_section;
14963 	      bfd *input_bfd;
14964 	      Elf32_External_RegInfo ext;
14965 	      Elf32_RegInfo sub;
14966 	      bfd_size_type sz;
14967 
14968 	      if (p->type != bfd_indirect_link_order)
14969 		{
14970 		  if (p->type == bfd_data_link_order)
14971 		    continue;
14972 		  abort ();
14973 		}
14974 
14975 	      input_section = p->u.indirect.section;
14976 	      input_bfd = input_section->owner;
14977 
14978 	      sz = (input_section->size < sizeof (ext)
14979 		    ? input_section->size : sizeof (ext));
14980 	      memset (&ext, 0, sizeof (ext));
14981 	      if (! bfd_get_section_contents (input_bfd, input_section,
14982 					      &ext, 0, sz))
14983 		return false;
14984 
14985 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14986 
14987 	      reginfo.ri_gprmask |= sub.ri_gprmask;
14988 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14989 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14990 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14991 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14992 
14993 	      /* ri_gp_value is set by the function
14994 		 `_bfd_mips_elf_section_processing' when the section is
14995 		 finally written out.  */
14996 
14997 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
14998 		 elf_link_input_bfd ignores this section.  */
14999 	      input_section->flags &= ~SEC_HAS_CONTENTS;
15000 	    }
15001 
15002 	  /* Size has been set in _bfd_mips_elf_late_size_sections.  */
15003 	  BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
15004 
15005 	  /* Skip this section later on (I don't think this currently
15006 	     matters, but someday it might).  */
15007 	  o->map_head.link_order = NULL;
15008 
15009 	  reginfo_sec = o;
15010 	}
15011 
15012       if (strcmp (o->name, ".mdebug") == 0)
15013 	{
15014 	  struct extsym_info einfo;
15015 	  bfd_vma last;
15016 
15017 	  /* We have found the .mdebug section in the output file.
15018 	     Look through all the link_orders comprising it and merge
15019 	     the information together.  */
15020 	  symhdr->magic = swap->sym_magic;
15021 	  /* FIXME: What should the version stamp be?  */
15022 	  symhdr->vstamp = 0;
15023 	  symhdr->ilineMax = 0;
15024 	  symhdr->cbLine = 0;
15025 	  symhdr->idnMax = 0;
15026 	  symhdr->ipdMax = 0;
15027 	  symhdr->isymMax = 0;
15028 	  symhdr->ioptMax = 0;
15029 	  symhdr->iauxMax = 0;
15030 	  symhdr->issMax = 0;
15031 	  symhdr->issExtMax = 0;
15032 	  symhdr->ifdMax = 0;
15033 	  symhdr->crfd = 0;
15034 	  symhdr->iextMax = 0;
15035 
15036 	  /* We accumulate the debugging information itself in the
15037 	     debug_info structure.  */
15038 	  debug.alloc_syments = false;
15039 	  debug.line = NULL;
15040 	  debug.external_dnr = NULL;
15041 	  debug.external_pdr = NULL;
15042 	  debug.external_sym = NULL;
15043 	  debug.external_opt = NULL;
15044 	  debug.external_aux = NULL;
15045 	  debug.ss = NULL;
15046 	  debug.ssext = debug.ssext_end = NULL;
15047 	  debug.external_fdr = NULL;
15048 	  debug.external_rfd = NULL;
15049 	  debug.external_ext = debug.external_ext_end = NULL;
15050 
15051 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15052 	  if (mdebug_handle == NULL)
15053 	    return false;
15054 
15055 	  esym.jmptbl = 0;
15056 	  esym.cobol_main = 0;
15057 	  esym.weakext = 0;
15058 	  esym.reserved = 0;
15059 	  esym.ifd = ifdNil;
15060 	  esym.asym.iss = issNil;
15061 	  esym.asym.st = stLocal;
15062 	  esym.asym.reserved = 0;
15063 	  esym.asym.index = indexNil;
15064 	  last = 0;
15065 	  for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15066 	    {
15067 	      esym.asym.sc = sc[i];
15068 	      s = bfd_get_section_by_name (abfd, secname[i]);
15069 	      if (s != NULL)
15070 		{
15071 		  esym.asym.value = s->vma;
15072 		  last = s->vma + s->size;
15073 		}
15074 	      else
15075 		esym.asym.value = last;
15076 	      if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15077 						 secname[i], &esym))
15078 		return false;
15079 	    }
15080 
15081 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
15082 	    {
15083 	      asection *input_section;
15084 	      bfd *input_bfd;
15085 	      const struct ecoff_debug_swap *input_swap;
15086 	      struct ecoff_debug_info input_debug;
15087 	      char *eraw_src;
15088 	      char *eraw_end;
15089 
15090 	      if (p->type != bfd_indirect_link_order)
15091 		{
15092 		  if (p->type == bfd_data_link_order)
15093 		    continue;
15094 		  abort ();
15095 		}
15096 
15097 	      input_section = p->u.indirect.section;
15098 	      input_bfd = input_section->owner;
15099 
15100 	      if (!is_mips_elf (input_bfd))
15101 		{
15102 		  /* I don't know what a non MIPS ELF bfd would be
15103 		     doing with a .mdebug section, but I don't really
15104 		     want to deal with it.  */
15105 		  continue;
15106 		}
15107 
15108 	      input_swap = (get_elf_backend_data (input_bfd)
15109 			    ->elf_backend_ecoff_debug_swap);
15110 
15111 	      BFD_ASSERT (p->size == input_section->size);
15112 
15113 	      /* The ECOFF linking code expects that we have already
15114 		 read in the debugging information and set up an
15115 		 ecoff_debug_info structure, so we do that now.  */
15116 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15117 						   &input_debug))
15118 		return false;
15119 
15120 	      if (! (bfd_ecoff_debug_accumulate
15121 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
15122 		      &input_debug, input_swap, info)))
15123 		{
15124 		  _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15125 		  return false;
15126 		}
15127 
15128 	      /* Loop through the external symbols.  For each one with
15129 		 interesting information, try to find the symbol in
15130 		 the linker global hash table and save the information
15131 		 for the output external symbols.  */
15132 	      eraw_src = input_debug.external_ext;
15133 	      eraw_end = (eraw_src
15134 			  + (input_debug.symbolic_header.iextMax
15135 			     * input_swap->external_ext_size));
15136 	      for (;
15137 		   eraw_src < eraw_end;
15138 		   eraw_src += input_swap->external_ext_size)
15139 		{
15140 		  EXTR ext;
15141 		  const char *name;
15142 		  struct mips_elf_link_hash_entry *h;
15143 
15144 		  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15145 		  if (ext.asym.sc == scNil
15146 		      || ext.asym.sc == scUndefined
15147 		      || ext.asym.sc == scSUndefined)
15148 		    continue;
15149 
15150 		  name = input_debug.ssext + ext.asym.iss;
15151 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15152 						 name, false, false, true);
15153 		  if (h == NULL || h->esym.ifd != -2)
15154 		    continue;
15155 
15156 		  if (ext.ifd != -1)
15157 		    {
15158 		      BFD_ASSERT (ext.ifd
15159 				  < input_debug.symbolic_header.ifdMax);
15160 		      ext.ifd = input_debug.ifdmap[ext.ifd];
15161 		    }
15162 
15163 		  h->esym = ext;
15164 		}
15165 
15166 	      /* Free up the information we just read.  */
15167 	      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15168 
15169 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
15170 		 elf_link_input_bfd ignores this section.  */
15171 	      input_section->flags &= ~SEC_HAS_CONTENTS;
15172 	    }
15173 
15174 	  if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15175 	    {
15176 	      /* Create .rtproc section.  */
15177 	      rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15178 	      if (rtproc_sec == NULL)
15179 		{
15180 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15181 				    | SEC_LINKER_CREATED | SEC_READONLY);
15182 
15183 		  rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15184 								   ".rtproc",
15185 								   flags);
15186 		  if (rtproc_sec == NULL
15187 		      || !bfd_set_section_alignment (rtproc_sec, 4))
15188 		    return false;
15189 		}
15190 
15191 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15192 						     info, rtproc_sec,
15193 						     &debug))
15194 		return false;
15195 	    }
15196 
15197 	  /* Build the external symbol information.  */
15198 	  einfo.abfd = abfd;
15199 	  einfo.info = info;
15200 	  einfo.debug = &debug;
15201 	  einfo.swap = swap;
15202 	  einfo.failed = false;
15203 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15204 				       mips_elf_output_extsym, &einfo);
15205 	  if (einfo.failed)
15206 	    return false;
15207 
15208 	  /* Set the size of the .mdebug section.  */
15209 	  o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15210 
15211 	  /* Skip this section later on (I don't think this currently
15212 	     matters, but someday it might).  */
15213 	  o->map_head.link_order = NULL;
15214 
15215 	  mdebug_sec = o;
15216 	}
15217 
15218       if (startswith (o->name, ".gptab."))
15219 	{
15220 	  const char *subname;
15221 	  unsigned int c;
15222 	  Elf32_gptab *tab;
15223 	  Elf32_External_gptab *ext_tab;
15224 	  unsigned int j;
15225 
15226 	  /* The .gptab.sdata and .gptab.sbss sections hold
15227 	     information describing how the small data area would
15228 	     change depending upon the -G switch.  These sections
15229 	     not used in executables files.  */
15230 	  if (! bfd_link_relocatable (info))
15231 	    {
15232 	      for (p = o->map_head.link_order; p != NULL; p = p->next)
15233 		{
15234 		  asection *input_section;
15235 
15236 		  if (p->type != bfd_indirect_link_order)
15237 		    {
15238 		      if (p->type == bfd_data_link_order)
15239 			continue;
15240 		      abort ();
15241 		    }
15242 
15243 		  input_section = p->u.indirect.section;
15244 
15245 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
15246 		     elf_link_input_bfd ignores this section.  */
15247 		  input_section->flags &= ~SEC_HAS_CONTENTS;
15248 		}
15249 
15250 	      /* Skip this section later on (I don't think this
15251 		 currently matters, but someday it might).  */
15252 	      o->map_head.link_order = NULL;
15253 
15254 	      /* Really remove the section.  */
15255 	      bfd_section_list_remove (abfd, o);
15256 	      --abfd->section_count;
15257 
15258 	      continue;
15259 	    }
15260 
15261 	  /* There is one gptab for initialized data, and one for
15262 	     uninitialized data.  */
15263 	  if (strcmp (o->name, ".gptab.sdata") == 0)
15264 	    gptab_data_sec = o;
15265 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
15266 	    gptab_bss_sec = o;
15267 	  else
15268 	    {
15269 	      _bfd_error_handler
15270 		/* xgettext:c-format */
15271 		(_("%pB: illegal section name `%pA'"), abfd, o);
15272 	      bfd_set_error (bfd_error_nonrepresentable_section);
15273 	      return false;
15274 	    }
15275 
15276 	  /* The linker script always combines .gptab.data and
15277 	     .gptab.sdata into .gptab.sdata, and likewise for
15278 	     .gptab.bss and .gptab.sbss.  It is possible that there is
15279 	     no .sdata or .sbss section in the output file, in which
15280 	     case we must change the name of the output section.  */
15281 	  subname = o->name + sizeof ".gptab" - 1;
15282 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
15283 	    {
15284 	      if (o == gptab_data_sec)
15285 		o->name = ".gptab.data";
15286 	      else
15287 		o->name = ".gptab.bss";
15288 	      subname = o->name + sizeof ".gptab" - 1;
15289 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15290 	    }
15291 
15292 	  /* Set up the first entry.  */
15293 	  c = 1;
15294 	  amt = c * sizeof (Elf32_gptab);
15295 	  tab = bfd_malloc (amt);
15296 	  if (tab == NULL)
15297 	    return false;
15298 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15299 	  tab[0].gt_header.gt_unused = 0;
15300 
15301 	  /* Combine the input sections.  */
15302 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
15303 	    {
15304 	      asection *input_section;
15305 	      bfd *input_bfd;
15306 	      bfd_size_type size;
15307 	      unsigned long last;
15308 	      bfd_size_type gpentry;
15309 
15310 	      if (p->type != bfd_indirect_link_order)
15311 		{
15312 		  if (p->type == bfd_data_link_order)
15313 		    continue;
15314 		  abort ();
15315 		}
15316 
15317 	      input_section = p->u.indirect.section;
15318 	      input_bfd = input_section->owner;
15319 
15320 	      /* Combine the gptab entries for this input section one
15321 		 by one.  We know that the input gptab entries are
15322 		 sorted by ascending -G value.  */
15323 	      size = input_section->size;
15324 	      last = 0;
15325 	      for (gpentry = sizeof (Elf32_External_gptab);
15326 		   gpentry < size;
15327 		   gpentry += sizeof (Elf32_External_gptab))
15328 		{
15329 		  Elf32_External_gptab ext_gptab;
15330 		  Elf32_gptab int_gptab;
15331 		  unsigned long val;
15332 		  unsigned long add;
15333 		  bool exact;
15334 		  unsigned int look;
15335 
15336 		  if (! (bfd_get_section_contents
15337 			 (input_bfd, input_section, &ext_gptab, gpentry,
15338 			  sizeof (Elf32_External_gptab))))
15339 		    {
15340 		      free (tab);
15341 		      return false;
15342 		    }
15343 
15344 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15345 						&int_gptab);
15346 		  val = int_gptab.gt_entry.gt_g_value;
15347 		  add = int_gptab.gt_entry.gt_bytes - last;
15348 
15349 		  exact = false;
15350 		  for (look = 1; look < c; look++)
15351 		    {
15352 		      if (tab[look].gt_entry.gt_g_value >= val)
15353 			tab[look].gt_entry.gt_bytes += add;
15354 
15355 		      if (tab[look].gt_entry.gt_g_value == val)
15356 			exact = true;
15357 		    }
15358 
15359 		  if (! exact)
15360 		    {
15361 		      Elf32_gptab *new_tab;
15362 		      unsigned int max;
15363 
15364 		      /* We need a new table entry.  */
15365 		      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15366 		      new_tab = bfd_realloc (tab, amt);
15367 		      if (new_tab == NULL)
15368 			{
15369 			  free (tab);
15370 			  return false;
15371 			}
15372 		      tab = new_tab;
15373 		      tab[c].gt_entry.gt_g_value = val;
15374 		      tab[c].gt_entry.gt_bytes = add;
15375 
15376 		      /* Merge in the size for the next smallest -G
15377 			 value, since that will be implied by this new
15378 			 value.  */
15379 		      max = 0;
15380 		      for (look = 1; look < c; look++)
15381 			{
15382 			  if (tab[look].gt_entry.gt_g_value < val
15383 			      && (max == 0
15384 				  || (tab[look].gt_entry.gt_g_value
15385 				      > tab[max].gt_entry.gt_g_value)))
15386 			    max = look;
15387 			}
15388 		      if (max != 0)
15389 			tab[c].gt_entry.gt_bytes +=
15390 			  tab[max].gt_entry.gt_bytes;
15391 
15392 		      ++c;
15393 		    }
15394 
15395 		  last = int_gptab.gt_entry.gt_bytes;
15396 		}
15397 
15398 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
15399 		 elf_link_input_bfd ignores this section.  */
15400 	      input_section->flags &= ~SEC_HAS_CONTENTS;
15401 	    }
15402 
15403 	  /* The table must be sorted by -G value.  */
15404 	  if (c > 2)
15405 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15406 
15407 	  /* Swap out the table.  */
15408 	  amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15409 	  ext_tab = bfd_alloc (abfd, amt);
15410 	  if (ext_tab == NULL)
15411 	    {
15412 	      free (tab);
15413 	      return false;
15414 	    }
15415 
15416 	  for (j = 0; j < c; j++)
15417 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15418 	  free (tab);
15419 
15420 	  o->size = c * sizeof (Elf32_External_gptab);
15421 	  o->contents = (bfd_byte *) ext_tab;
15422 
15423 	  /* Skip this section later on (I don't think this currently
15424 	     matters, but someday it might).  */
15425 	  o->map_head.link_order = NULL;
15426 	}
15427     }
15428 
15429   /* Invoke the regular ELF backend linker to do all the work.  */
15430   if (!bfd_elf_final_link (abfd, info))
15431     return false;
15432 
15433   /* Now write out the computed sections.  */
15434 
15435   if (abiflags_sec != NULL)
15436     {
15437       Elf_External_ABIFlags_v0 ext;
15438       Elf_Internal_ABIFlags_v0 *abiflags;
15439 
15440       abiflags = &mips_elf_tdata (abfd)->abiflags;
15441 
15442       /* Set up the abiflags if no valid input sections were found.  */
15443       if (!mips_elf_tdata (abfd)->abiflags_valid)
15444 	{
15445 	  infer_mips_abiflags (abfd, abiflags);
15446 	  mips_elf_tdata (abfd)->abiflags_valid = true;
15447 	}
15448       bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15449       if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15450 	return false;
15451     }
15452 
15453   if (reginfo_sec != NULL)
15454     {
15455       Elf32_External_RegInfo ext;
15456 
15457       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15458       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15459 	return false;
15460     }
15461 
15462   if (mdebug_sec != NULL)
15463     {
15464       BFD_ASSERT (abfd->output_has_begun);
15465       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15466 					       swap, info,
15467 					       mdebug_sec->filepos))
15468 	return false;
15469 
15470       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15471     }
15472 
15473   if (gptab_data_sec != NULL)
15474     {
15475       if (! bfd_set_section_contents (abfd, gptab_data_sec,
15476 				      gptab_data_sec->contents,
15477 				      0, gptab_data_sec->size))
15478 	return false;
15479     }
15480 
15481   if (gptab_bss_sec != NULL)
15482     {
15483       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15484 				      gptab_bss_sec->contents,
15485 				      0, gptab_bss_sec->size))
15486 	return false;
15487     }
15488 
15489   if (SGI_COMPAT (abfd))
15490     {
15491       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15492       if (rtproc_sec != NULL)
15493 	{
15494 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
15495 					  rtproc_sec->contents,
15496 					  0, rtproc_sec->size))
15497 	    return false;
15498 	}
15499     }
15500 
15501   return true;
15502 }
15503 
15504 /* Merge object file header flags from IBFD into OBFD.  Raise an error
15505    if there are conflicting settings.  */
15506 
15507 static bool
15508 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15509 {
15510   bfd *obfd = info->output_bfd;
15511   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15512   flagword old_flags;
15513   flagword new_flags;
15514   bool ok;
15515 
15516   new_flags = elf_elfheader (ibfd)->e_flags;
15517   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15518   old_flags = elf_elfheader (obfd)->e_flags;
15519 
15520   /* Check flag compatibility.  */
15521 
15522   new_flags &= ~EF_MIPS_NOREORDER;
15523   old_flags &= ~EF_MIPS_NOREORDER;
15524 
15525   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
15526      doesn't seem to matter.  */
15527   new_flags &= ~EF_MIPS_XGOT;
15528   old_flags &= ~EF_MIPS_XGOT;
15529 
15530   /* MIPSpro generates ucode info in n64 objects.  Again, we should
15531      just be able to ignore this.  */
15532   new_flags &= ~EF_MIPS_UCODE;
15533   old_flags &= ~EF_MIPS_UCODE;
15534 
15535   /* DSOs should only be linked with CPIC code.  */
15536   if ((ibfd->flags & DYNAMIC) != 0)
15537     new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15538 
15539   if (new_flags == old_flags)
15540     return true;
15541 
15542   ok = true;
15543 
15544   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15545       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15546     {
15547       _bfd_error_handler
15548 	(_("%pB: warning: linking abicalls files with non-abicalls files"),
15549 	 ibfd);
15550       ok = true;
15551     }
15552 
15553   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15554     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15555   if (! (new_flags & EF_MIPS_PIC))
15556     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15557 
15558   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15559   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15560 
15561   /* Compare the ISAs.  */
15562   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15563     {
15564       _bfd_error_handler
15565 	(_("%pB: linking 32-bit code with 64-bit code"),
15566 	 ibfd);
15567       ok = false;
15568     }
15569   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15570     {
15571       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
15572       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15573 	{
15574 	  /* Copy the architecture info from IBFD to OBFD.  Also copy
15575 	     the 32-bit flag (if set) so that we continue to recognise
15576 	     OBFD as a 32-bit binary.  */
15577 	  bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15578 	  elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15579 	  elf_elfheader (obfd)->e_flags
15580 	    |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15581 
15582 	  /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
15583 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15584 
15585 	  /* Copy across the ABI flags if OBFD doesn't use them
15586 	     and if that was what caused us to treat IBFD as 32-bit.  */
15587 	  if ((old_flags & EF_MIPS_ABI) == 0
15588 	      && mips_32bit_flags_p (new_flags)
15589 	      && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15590 	    elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15591 	}
15592       else
15593 	{
15594 	  /* The ISAs aren't compatible.  */
15595 	  _bfd_error_handler
15596 	    /* xgettext:c-format */
15597 	    (_("%pB: linking %s module with previous %s modules"),
15598 	     ibfd,
15599 	     bfd_printable_name (ibfd),
15600 	     bfd_printable_name (obfd));
15601 	  ok = false;
15602 	}
15603     }
15604 
15605   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15606   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15607 
15608   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
15609      does set EI_CLASS differently from any 32-bit ABI.  */
15610   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15611       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15612 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15613     {
15614       /* Only error if both are set (to different values).  */
15615       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15616 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15617 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15618 	{
15619 	  _bfd_error_handler
15620 	    /* xgettext:c-format */
15621 	    (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15622 	     ibfd,
15623 	     elf_mips_abi_name (ibfd),
15624 	     elf_mips_abi_name (obfd));
15625 	  ok = false;
15626 	}
15627       new_flags &= ~EF_MIPS_ABI;
15628       old_flags &= ~EF_MIPS_ABI;
15629     }
15630 
15631   /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
15632      and allow arbitrary mixing of the remaining ASEs (retain the union).  */
15633   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15634     {
15635       int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15636       int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15637       int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15638       int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15639       int micro_mis = old_m16 && new_micro;
15640       int m16_mis = old_micro && new_m16;
15641 
15642       if (m16_mis || micro_mis)
15643 	{
15644 	  _bfd_error_handler
15645 	    /* xgettext:c-format */
15646 	    (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15647 	     ibfd,
15648 	     m16_mis ? "MIPS16" : "microMIPS",
15649 	     m16_mis ? "microMIPS" : "MIPS16");
15650 	  ok = false;
15651 	}
15652 
15653       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15654 
15655       new_flags &= ~ EF_MIPS_ARCH_ASE;
15656       old_flags &= ~ EF_MIPS_ARCH_ASE;
15657     }
15658 
15659   /* Compare NaN encodings.  */
15660   if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15661     {
15662       /* xgettext:c-format */
15663       _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15664 			  ibfd,
15665 			  (new_flags & EF_MIPS_NAN2008
15666 			   ? "-mnan=2008" : "-mnan=legacy"),
15667 			  (old_flags & EF_MIPS_NAN2008
15668 			   ? "-mnan=2008" : "-mnan=legacy"));
15669       ok = false;
15670       new_flags &= ~EF_MIPS_NAN2008;
15671       old_flags &= ~EF_MIPS_NAN2008;
15672     }
15673 
15674   /* Compare FP64 state.  */
15675   if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15676     {
15677       /* xgettext:c-format */
15678       _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15679 			  ibfd,
15680 			  (new_flags & EF_MIPS_FP64
15681 			   ? "-mfp64" : "-mfp32"),
15682 			  (old_flags & EF_MIPS_FP64
15683 			   ? "-mfp64" : "-mfp32"));
15684       ok = false;
15685       new_flags &= ~EF_MIPS_FP64;
15686       old_flags &= ~EF_MIPS_FP64;
15687     }
15688 
15689   /* Warn about any other mismatches */
15690   if (new_flags != old_flags)
15691     {
15692       /* xgettext:c-format */
15693       _bfd_error_handler
15694 	(_("%pB: uses different e_flags (%#x) fields than previous modules "
15695 	   "(%#x)"),
15696 	 ibfd, new_flags, old_flags);
15697       ok = false;
15698     }
15699 
15700   return ok;
15701 }
15702 
15703 /* Merge object attributes from IBFD into OBFD.  Raise an error if
15704    there are conflicting attributes.  */
15705 static bool
15706 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15707 {
15708   bfd *obfd = info->output_bfd;
15709   obj_attribute *in_attr;
15710   obj_attribute *out_attr;
15711   bfd *abi_fp_bfd;
15712   bfd *abi_msa_bfd;
15713 
15714   abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15715   in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15716   if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15717     mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15718 
15719   abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15720   if (!abi_msa_bfd
15721       && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15722     mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15723 
15724   if (!elf_known_obj_attributes_proc (obfd)[0].i)
15725     {
15726       /* This is the first object.  Copy the attributes.  */
15727       _bfd_elf_copy_obj_attributes (ibfd, obfd);
15728 
15729       /* Use the Tag_null value to indicate the attributes have been
15730 	 initialized.  */
15731       elf_known_obj_attributes_proc (obfd)[0].i = 1;
15732 
15733       return true;
15734     }
15735 
15736   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15737      non-conflicting ones.  */
15738   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15739   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15740     {
15741       int out_fp, in_fp;
15742 
15743       out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15744       in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15745       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15746       if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15747 	out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15748       else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15749 	       && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15750 		   || in_fp == Val_GNU_MIPS_ABI_FP_64
15751 		   || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15752 	{
15753 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15754 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15755 	}
15756       else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15757 	       && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15758 		   || out_fp == Val_GNU_MIPS_ABI_FP_64
15759 		   || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15760 	/* Keep the current setting.  */;
15761       else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15762 	       && in_fp == Val_GNU_MIPS_ABI_FP_64)
15763 	{
15764 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15765 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15766 	}
15767       else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15768 	       && out_fp == Val_GNU_MIPS_ABI_FP_64)
15769 	/* Keep the current setting.  */;
15770       else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15771 	{
15772 	  const char *out_string, *in_string;
15773 
15774 	  out_string = _bfd_mips_fp_abi_string (out_fp);
15775 	  in_string = _bfd_mips_fp_abi_string (in_fp);
15776 	  /* First warn about cases involving unrecognised ABIs.  */
15777 	  if (!out_string && !in_string)
15778 	    /* xgettext:c-format */
15779 	    _bfd_error_handler
15780 	      (_("warning: %pB uses unknown floating point ABI %d "
15781 		 "(set by %pB), %pB uses unknown floating point ABI %d"),
15782 	       obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15783 	  else if (!out_string)
15784 	    _bfd_error_handler
15785 	      /* xgettext:c-format */
15786 	      (_("warning: %pB uses unknown floating point ABI %d "
15787 		 "(set by %pB), %pB uses %s"),
15788 	       obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15789 	  else if (!in_string)
15790 	    _bfd_error_handler
15791 	      /* xgettext:c-format */
15792 	      (_("warning: %pB uses %s (set by %pB), "
15793 		 "%pB uses unknown floating point ABI %d"),
15794 	       obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15795 	  else
15796 	    {
15797 	      /* If one of the bfds is soft-float, the other must be
15798 		 hard-float.  The exact choice of hard-float ABI isn't
15799 		 really relevant to the error message.  */
15800 	      if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15801 		out_string = "-mhard-float";
15802 	      else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15803 		in_string = "-mhard-float";
15804 	      _bfd_error_handler
15805 		/* xgettext:c-format */
15806 		(_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15807 		 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15808 	    }
15809 	}
15810     }
15811 
15812   /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15813      non-conflicting ones.  */
15814   if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15815     {
15816       out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15817       if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15818 	out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15819       else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15820 	switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15821 	  {
15822 	  case Val_GNU_MIPS_ABI_MSA_128:
15823 	    _bfd_error_handler
15824 	      /* xgettext:c-format */
15825 	      (_("warning: %pB uses %s (set by %pB), "
15826 		 "%pB uses unknown MSA ABI %d"),
15827 	       obfd, "-mmsa", abi_msa_bfd,
15828 	       ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15829 	    break;
15830 
15831 	  default:
15832 	    switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15833 	      {
15834 	      case Val_GNU_MIPS_ABI_MSA_128:
15835 		_bfd_error_handler
15836 		  /* xgettext:c-format */
15837 		  (_("warning: %pB uses unknown MSA ABI %d "
15838 		     "(set by %pB), %pB uses %s"),
15839 		     obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15840 		   abi_msa_bfd, ibfd, "-mmsa");
15841 		  break;
15842 
15843 	      default:
15844 		_bfd_error_handler
15845 		  /* xgettext:c-format */
15846 		  (_("warning: %pB uses unknown MSA ABI %d "
15847 		     "(set by %pB), %pB uses unknown MSA ABI %d"),
15848 		   obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15849 		   abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15850 		break;
15851 	      }
15852 	  }
15853     }
15854 
15855   /* Merge Tag_compatibility attributes and any common GNU ones.  */
15856   return _bfd_elf_merge_object_attributes (ibfd, info);
15857 }
15858 
15859 /* Merge object ABI flags from IBFD into OBFD.  Raise an error if
15860    there are conflicting settings.  */
15861 
15862 static bool
15863 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15864 {
15865   obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15866   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15867   struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15868 
15869   /* Update the output abiflags fp_abi using the computed fp_abi.  */
15870   out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15871 
15872 #define max(a, b) ((a) > (b) ? (a) : (b))
15873   /* Merge abiflags.  */
15874   out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15875 				       in_tdata->abiflags.isa_level);
15876   out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15877 				     in_tdata->abiflags.isa_rev);
15878   out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15879 				      in_tdata->abiflags.gpr_size);
15880   out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15881 				       in_tdata->abiflags.cpr1_size);
15882   out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15883 				       in_tdata->abiflags.cpr2_size);
15884 #undef max
15885   out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15886   out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15887 
15888   return true;
15889 }
15890 
15891 /* Merge backend specific data from an object file to the output
15892    object file when linking.  */
15893 
15894 bool
15895 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15896 {
15897   bfd *obfd = info->output_bfd;
15898   struct mips_elf_obj_tdata *out_tdata;
15899   struct mips_elf_obj_tdata *in_tdata;
15900   bool null_input_bfd = true;
15901   asection *sec;
15902   bool ok;
15903 
15904   /* Check if we have the same endianness.  */
15905   if (! _bfd_generic_verify_endian_match (ibfd, info))
15906     {
15907       _bfd_error_handler
15908 	(_("%pB: endianness incompatible with that of the selected emulation"),
15909 	 ibfd);
15910       return false;
15911     }
15912 
15913   if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15914     return true;
15915 
15916   in_tdata = mips_elf_tdata (ibfd);
15917   out_tdata = mips_elf_tdata (obfd);
15918 
15919   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15920     {
15921       _bfd_error_handler
15922 	(_("%pB: ABI is incompatible with that of the selected emulation"),
15923 	 ibfd);
15924       return false;
15925     }
15926 
15927   /* Check to see if the input BFD actually contains any sections.  If not,
15928      then it has no attributes, and its flags may not have been initialized
15929      either, but it cannot actually cause any incompatibility.  */
15930   /* FIXME: This excludes any input shared library from consideration.  */
15931   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15932     {
15933       /* Ignore synthetic sections and empty .text, .data and .bss sections
15934 	 which are automatically generated by gas.  Also ignore fake
15935 	 (s)common sections, since merely defining a common symbol does
15936 	 not affect compatibility.  */
15937       if ((sec->flags & SEC_IS_COMMON) == 0
15938 	  && strcmp (sec->name, ".reginfo")
15939 	  && strcmp (sec->name, ".mdebug")
15940 	  && (sec->size != 0
15941 	      || (strcmp (sec->name, ".text")
15942 		  && strcmp (sec->name, ".data")
15943 		  && strcmp (sec->name, ".bss"))))
15944 	{
15945 	  null_input_bfd = false;
15946 	  break;
15947 	}
15948     }
15949   if (null_input_bfd)
15950     return true;
15951 
15952   /* Populate abiflags using existing information.  */
15953   if (in_tdata->abiflags_valid)
15954     {
15955       obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15956       Elf_Internal_ABIFlags_v0 in_abiflags;
15957       Elf_Internal_ABIFlags_v0 abiflags;
15958 
15959       /* Set up the FP ABI attribute from the abiflags if it is not already
15960 	 set.  */
15961       if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15962 	in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15963 
15964       infer_mips_abiflags (ibfd, &abiflags);
15965       in_abiflags = in_tdata->abiflags;
15966 
15967       /* It is not possible to infer the correct ISA revision
15968 	 for R3 or R5 so drop down to R2 for the checks.  */
15969       if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15970 	in_abiflags.isa_rev = 2;
15971 
15972       if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15973 	  < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15974 	_bfd_error_handler
15975 	  (_("%pB: warning: inconsistent ISA between e_flags and "
15976 	     ".MIPS.abiflags"), ibfd);
15977       if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15978 	  && in_abiflags.fp_abi != abiflags.fp_abi)
15979 	_bfd_error_handler
15980 	  (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15981 	     ".MIPS.abiflags"), ibfd);
15982       if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15983 	_bfd_error_handler
15984 	  (_("%pB: warning: inconsistent ASEs between e_flags and "
15985 	     ".MIPS.abiflags"), ibfd);
15986       /* The isa_ext is allowed to be an extension of what can be inferred
15987 	 from e_flags.  */
15988       if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15989 				bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15990 	_bfd_error_handler
15991 	  (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15992 	     ".MIPS.abiflags"), ibfd);
15993       if (in_abiflags.flags2 != 0)
15994 	_bfd_error_handler
15995 	  (_("%pB: warning: unexpected flag in the flags2 field of "
15996 	     ".MIPS.abiflags (0x%lx)"), ibfd,
15997 	   in_abiflags.flags2);
15998     }
15999   else
16000     {
16001       infer_mips_abiflags (ibfd, &in_tdata->abiflags);
16002       in_tdata->abiflags_valid = true;
16003     }
16004 
16005   if (!out_tdata->abiflags_valid)
16006     {
16007       /* Copy input abiflags if output abiflags are not already valid.  */
16008       out_tdata->abiflags = in_tdata->abiflags;
16009       out_tdata->abiflags_valid = true;
16010     }
16011 
16012   if (! elf_flags_init (obfd))
16013     {
16014       elf_flags_init (obfd) = true;
16015       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16016       elf_elfheader (obfd)->e_ident[EI_CLASS]
16017 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
16018 
16019       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16020 	  && (bfd_get_arch_info (obfd)->the_default
16021 	      || mips_mach_extends_p (bfd_get_mach (obfd),
16022 				      bfd_get_mach (ibfd))))
16023 	{
16024 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16025 				   bfd_get_mach (ibfd)))
16026 	    return false;
16027 
16028 	  /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
16029 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16030 	}
16031 
16032       ok = true;
16033     }
16034   else
16035     ok = mips_elf_merge_obj_e_flags (ibfd, info);
16036 
16037   ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16038 
16039   ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16040 
16041   if (!ok)
16042     {
16043       bfd_set_error (bfd_error_bad_value);
16044       return false;
16045     }
16046 
16047   return true;
16048 }
16049 
16050 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
16051 
16052 bool
16053 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16054 {
16055   BFD_ASSERT (!elf_flags_init (abfd)
16056 	      || elf_elfheader (abfd)->e_flags == flags);
16057 
16058   elf_elfheader (abfd)->e_flags = flags;
16059   elf_flags_init (abfd) = true;
16060   return true;
16061 }
16062 
16063 char *
16064 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16065 {
16066   switch (dtag)
16067     {
16068     default: return "";
16069     case DT_MIPS_RLD_VERSION:
16070       return "MIPS_RLD_VERSION";
16071     case DT_MIPS_TIME_STAMP:
16072       return "MIPS_TIME_STAMP";
16073     case DT_MIPS_ICHECKSUM:
16074       return "MIPS_ICHECKSUM";
16075     case DT_MIPS_IVERSION:
16076       return "MIPS_IVERSION";
16077     case DT_MIPS_FLAGS:
16078       return "MIPS_FLAGS";
16079     case DT_MIPS_BASE_ADDRESS:
16080       return "MIPS_BASE_ADDRESS";
16081     case DT_MIPS_MSYM:
16082       return "MIPS_MSYM";
16083     case DT_MIPS_CONFLICT:
16084       return "MIPS_CONFLICT";
16085     case DT_MIPS_LIBLIST:
16086       return "MIPS_LIBLIST";
16087     case DT_MIPS_LOCAL_GOTNO:
16088       return "MIPS_LOCAL_GOTNO";
16089     case DT_MIPS_CONFLICTNO:
16090       return "MIPS_CONFLICTNO";
16091     case DT_MIPS_LIBLISTNO:
16092       return "MIPS_LIBLISTNO";
16093     case DT_MIPS_SYMTABNO:
16094       return "MIPS_SYMTABNO";
16095     case DT_MIPS_UNREFEXTNO:
16096       return "MIPS_UNREFEXTNO";
16097     case DT_MIPS_GOTSYM:
16098       return "MIPS_GOTSYM";
16099     case DT_MIPS_HIPAGENO:
16100       return "MIPS_HIPAGENO";
16101     case DT_MIPS_RLD_MAP:
16102       return "MIPS_RLD_MAP";
16103     case DT_MIPS_RLD_MAP_REL:
16104       return "MIPS_RLD_MAP_REL";
16105     case DT_MIPS_DELTA_CLASS:
16106       return "MIPS_DELTA_CLASS";
16107     case DT_MIPS_DELTA_CLASS_NO:
16108       return "MIPS_DELTA_CLASS_NO";
16109     case DT_MIPS_DELTA_INSTANCE:
16110       return "MIPS_DELTA_INSTANCE";
16111     case DT_MIPS_DELTA_INSTANCE_NO:
16112       return "MIPS_DELTA_INSTANCE_NO";
16113     case DT_MIPS_DELTA_RELOC:
16114       return "MIPS_DELTA_RELOC";
16115     case DT_MIPS_DELTA_RELOC_NO:
16116       return "MIPS_DELTA_RELOC_NO";
16117     case DT_MIPS_DELTA_SYM:
16118       return "MIPS_DELTA_SYM";
16119     case DT_MIPS_DELTA_SYM_NO:
16120       return "MIPS_DELTA_SYM_NO";
16121     case DT_MIPS_DELTA_CLASSSYM:
16122       return "MIPS_DELTA_CLASSSYM";
16123     case DT_MIPS_DELTA_CLASSSYM_NO:
16124       return "MIPS_DELTA_CLASSSYM_NO";
16125     case DT_MIPS_CXX_FLAGS:
16126       return "MIPS_CXX_FLAGS";
16127     case DT_MIPS_PIXIE_INIT:
16128       return "MIPS_PIXIE_INIT";
16129     case DT_MIPS_SYMBOL_LIB:
16130       return "MIPS_SYMBOL_LIB";
16131     case DT_MIPS_LOCALPAGE_GOTIDX:
16132       return "MIPS_LOCALPAGE_GOTIDX";
16133     case DT_MIPS_LOCAL_GOTIDX:
16134       return "MIPS_LOCAL_GOTIDX";
16135     case DT_MIPS_HIDDEN_GOTIDX:
16136       return "MIPS_HIDDEN_GOTIDX";
16137     case DT_MIPS_PROTECTED_GOTIDX:
16138       return "MIPS_PROTECTED_GOT_IDX";
16139     case DT_MIPS_OPTIONS:
16140       return "MIPS_OPTIONS";
16141     case DT_MIPS_INTERFACE:
16142       return "MIPS_INTERFACE";
16143     case DT_MIPS_DYNSTR_ALIGN:
16144       return "DT_MIPS_DYNSTR_ALIGN";
16145     case DT_MIPS_INTERFACE_SIZE:
16146       return "DT_MIPS_INTERFACE_SIZE";
16147     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16148       return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16149     case DT_MIPS_PERF_SUFFIX:
16150       return "DT_MIPS_PERF_SUFFIX";
16151     case DT_MIPS_COMPACT_SIZE:
16152       return "DT_MIPS_COMPACT_SIZE";
16153     case DT_MIPS_GP_VALUE:
16154       return "DT_MIPS_GP_VALUE";
16155     case DT_MIPS_AUX_DYNAMIC:
16156       return "DT_MIPS_AUX_DYNAMIC";
16157     case DT_MIPS_PLTGOT:
16158       return "DT_MIPS_PLTGOT";
16159     case DT_MIPS_RWPLT:
16160       return "DT_MIPS_RWPLT";
16161     case DT_MIPS_XHASH:
16162       return "DT_MIPS_XHASH";
16163     }
16164 }
16165 
16166 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16167    not known.  */
16168 
16169 const char *
16170 _bfd_mips_fp_abi_string (int fp)
16171 {
16172   switch (fp)
16173     {
16174       /* These strings aren't translated because they're simply
16175 	 option lists.  */
16176     case Val_GNU_MIPS_ABI_FP_DOUBLE:
16177       return "-mdouble-float";
16178 
16179     case Val_GNU_MIPS_ABI_FP_SINGLE:
16180       return "-msingle-float";
16181 
16182     case Val_GNU_MIPS_ABI_FP_SOFT:
16183       return "-msoft-float";
16184 
16185     case Val_GNU_MIPS_ABI_FP_OLD_64:
16186       return _("-mips32r2 -mfp64 (12 callee-saved)");
16187 
16188     case Val_GNU_MIPS_ABI_FP_XX:
16189       return "-mfpxx";
16190 
16191     case Val_GNU_MIPS_ABI_FP_64:
16192       return "-mgp32 -mfp64";
16193 
16194     case Val_GNU_MIPS_ABI_FP_64A:
16195       return "-mgp32 -mfp64 -mno-odd-spreg";
16196 
16197     default:
16198       return 0;
16199     }
16200 }
16201 
16202 static void
16203 print_mips_ases (FILE *file, unsigned int mask)
16204 {
16205   if (mask & AFL_ASE_DSP)
16206     fputs ("\n\tDSP ASE", file);
16207   if (mask & AFL_ASE_DSPR2)
16208     fputs ("\n\tDSP R2 ASE", file);
16209   if (mask & AFL_ASE_DSPR3)
16210     fputs ("\n\tDSP R3 ASE", file);
16211   if (mask & AFL_ASE_EVA)
16212     fputs ("\n\tEnhanced VA Scheme", file);
16213   if (mask & AFL_ASE_MCU)
16214     fputs ("\n\tMCU (MicroController) ASE", file);
16215   if (mask & AFL_ASE_MDMX)
16216     fputs ("\n\tMDMX ASE", file);
16217   if (mask & AFL_ASE_MIPS3D)
16218     fputs ("\n\tMIPS-3D ASE", file);
16219   if (mask & AFL_ASE_MT)
16220     fputs ("\n\tMT ASE", file);
16221   if (mask & AFL_ASE_SMARTMIPS)
16222     fputs ("\n\tSmartMIPS ASE", file);
16223   if (mask & AFL_ASE_VIRT)
16224     fputs ("\n\tVZ ASE", file);
16225   if (mask & AFL_ASE_MSA)
16226     fputs ("\n\tMSA ASE", file);
16227   if (mask & AFL_ASE_MIPS16)
16228     fputs ("\n\tMIPS16 ASE", file);
16229   if (mask & AFL_ASE_MICROMIPS)
16230     fputs ("\n\tMICROMIPS ASE", file);
16231   if (mask & AFL_ASE_XPA)
16232     fputs ("\n\tXPA ASE", file);
16233   if (mask & AFL_ASE_MIPS16E2)
16234     fputs ("\n\tMIPS16e2 ASE", file);
16235   if (mask & AFL_ASE_CRC)
16236     fputs ("\n\tCRC ASE", file);
16237   if (mask & AFL_ASE_GINV)
16238     fputs ("\n\tGINV ASE", file);
16239   if (mask & AFL_ASE_LOONGSON_MMI)
16240     fputs ("\n\tLoongson MMI ASE", file);
16241   if (mask & AFL_ASE_LOONGSON_CAM)
16242     fputs ("\n\tLoongson CAM ASE", file);
16243   if (mask & AFL_ASE_LOONGSON_EXT)
16244     fputs ("\n\tLoongson EXT ASE", file);
16245   if (mask & AFL_ASE_LOONGSON_EXT2)
16246     fputs ("\n\tLoongson EXT2 ASE", file);
16247   if (mask == 0)
16248     fprintf (file, "\n\t%s", _("None"));
16249   else if ((mask & ~AFL_ASE_MASK) != 0)
16250     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16251 }
16252 
16253 static void
16254 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16255 {
16256   switch (isa_ext)
16257     {
16258     case 0:
16259       fputs (_("None"), file);
16260       break;
16261     case AFL_EXT_XLR:
16262       fputs ("RMI XLR", file);
16263       break;
16264     case AFL_EXT_OCTEON3:
16265       fputs ("Cavium Networks Octeon3", file);
16266       break;
16267     case AFL_EXT_OCTEON2:
16268       fputs ("Cavium Networks Octeon2", file);
16269       break;
16270     case AFL_EXT_OCTEONP:
16271       fputs ("Cavium Networks OcteonP", file);
16272       break;
16273     case AFL_EXT_OCTEON:
16274       fputs ("Cavium Networks Octeon", file);
16275       break;
16276     case AFL_EXT_5900:
16277       fputs ("Toshiba R5900", file);
16278       break;
16279     case AFL_EXT_4650:
16280       fputs ("MIPS R4650", file);
16281       break;
16282     case AFL_EXT_4010:
16283       fputs ("LSI R4010", file);
16284       break;
16285     case AFL_EXT_4100:
16286       fputs ("NEC VR4100", file);
16287       break;
16288     case AFL_EXT_3900:
16289       fputs ("Toshiba R3900", file);
16290       break;
16291     case AFL_EXT_10000:
16292       fputs ("MIPS R10000", file);
16293       break;
16294     case AFL_EXT_SB1:
16295       fputs ("Broadcom SB-1", file);
16296       break;
16297     case AFL_EXT_4111:
16298       fputs ("NEC VR4111/VR4181", file);
16299       break;
16300     case AFL_EXT_4120:
16301       fputs ("NEC VR4120", file);
16302       break;
16303     case AFL_EXT_5400:
16304       fputs ("NEC VR5400", file);
16305       break;
16306     case AFL_EXT_5500:
16307       fputs ("NEC VR5500", file);
16308       break;
16309     case AFL_EXT_LOONGSON_2E:
16310       fputs ("ST Microelectronics Loongson 2E", file);
16311       break;
16312     case AFL_EXT_LOONGSON_2F:
16313       fputs ("ST Microelectronics Loongson 2F", file);
16314       break;
16315     case AFL_EXT_INTERAPTIV_MR2:
16316       fputs ("Imagination interAptiv MR2", file);
16317       break;
16318     default:
16319       fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16320       break;
16321     }
16322 }
16323 
16324 static void
16325 print_mips_fp_abi_value (FILE *file, int val)
16326 {
16327   switch (val)
16328     {
16329     case Val_GNU_MIPS_ABI_FP_ANY:
16330       fprintf (file, _("Hard or soft float\n"));
16331       break;
16332     case Val_GNU_MIPS_ABI_FP_DOUBLE:
16333       fprintf (file, _("Hard float (double precision)\n"));
16334       break;
16335     case Val_GNU_MIPS_ABI_FP_SINGLE:
16336       fprintf (file, _("Hard float (single precision)\n"));
16337       break;
16338     case Val_GNU_MIPS_ABI_FP_SOFT:
16339       fprintf (file, _("Soft float\n"));
16340       break;
16341     case Val_GNU_MIPS_ABI_FP_OLD_64:
16342       fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16343       break;
16344     case Val_GNU_MIPS_ABI_FP_XX:
16345       fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16346       break;
16347     case Val_GNU_MIPS_ABI_FP_64:
16348       fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16349       break;
16350     case Val_GNU_MIPS_ABI_FP_64A:
16351       fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16352       break;
16353     default:
16354       fprintf (file, "??? (%d)\n", val);
16355       break;
16356     }
16357 }
16358 
16359 static int
16360 get_mips_reg_size (int reg_size)
16361 {
16362   return (reg_size == AFL_REG_NONE) ? 0
16363 	 : (reg_size == AFL_REG_32) ? 32
16364 	 : (reg_size == AFL_REG_64) ? 64
16365 	 : (reg_size == AFL_REG_128) ? 128
16366 	 : -1;
16367 }
16368 
16369 bool
16370 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16371 {
16372   FILE *file = ptr;
16373 
16374   BFD_ASSERT (abfd != NULL && ptr != NULL);
16375 
16376   /* Print normal ELF private data.  */
16377   _bfd_elf_print_private_bfd_data (abfd, ptr);
16378 
16379   /* xgettext:c-format */
16380   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16381 
16382   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
16383     fprintf (file, _(" [abi=O32]"));
16384   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
16385     fprintf (file, _(" [abi=O64]"));
16386   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
16387     fprintf (file, _(" [abi=EABI32]"));
16388   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
16389     fprintf (file, _(" [abi=EABI64]"));
16390   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16391     fprintf (file, _(" [abi unknown]"));
16392   else if (ABI_N32_P (abfd))
16393     fprintf (file, _(" [abi=N32]"));
16394   else if (ABI_64_P (abfd))
16395     fprintf (file, _(" [abi=64]"));
16396   else
16397     fprintf (file, _(" [no abi set]"));
16398 
16399   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
16400     fprintf (file, " [mips1]");
16401   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
16402     fprintf (file, " [mips2]");
16403   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
16404     fprintf (file, " [mips3]");
16405   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
16406     fprintf (file, " [mips4]");
16407   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
16408     fprintf (file, " [mips5]");
16409   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
16410     fprintf (file, " [mips32]");
16411   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
16412     fprintf (file, " [mips64]");
16413   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
16414     fprintf (file, " [mips32r2]");
16415   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
16416     fprintf (file, " [mips64r2]");
16417   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
16418     fprintf (file, " [mips32r6]");
16419   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
16420     fprintf (file, " [mips64r6]");
16421   else
16422     fprintf (file, _(" [unknown ISA]"));
16423 
16424   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16425     fprintf (file, " [mdmx]");
16426 
16427   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16428     fprintf (file, " [mips16]");
16429 
16430   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16431     fprintf (file, " [micromips]");
16432 
16433   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16434     fprintf (file, " [nan2008]");
16435 
16436   if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16437     fprintf (file, " [old fp64]");
16438 
16439   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16440     fprintf (file, " [32bitmode]");
16441   else
16442     fprintf (file, _(" [not 32bitmode]"));
16443 
16444   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16445     fprintf (file, " [noreorder]");
16446 
16447   if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16448     fprintf (file, " [PIC]");
16449 
16450   if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16451     fprintf (file, " [CPIC]");
16452 
16453   if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16454     fprintf (file, " [XGOT]");
16455 
16456   if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16457     fprintf (file, " [UCODE]");
16458 
16459   fputc ('\n', file);
16460 
16461   if (mips_elf_tdata (abfd)->abiflags_valid)
16462     {
16463       Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16464       fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16465       fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16466       if (abiflags->isa_rev > 1)
16467 	fprintf (file, "r%d", abiflags->isa_rev);
16468       fprintf (file, "\nGPR size: %d",
16469 	       get_mips_reg_size (abiflags->gpr_size));
16470       fprintf (file, "\nCPR1 size: %d",
16471 	       get_mips_reg_size (abiflags->cpr1_size));
16472       fprintf (file, "\nCPR2 size: %d",
16473 	       get_mips_reg_size (abiflags->cpr2_size));
16474       fputs ("\nFP ABI: ", file);
16475       print_mips_fp_abi_value (file, abiflags->fp_abi);
16476       fputs ("ISA Extension: ", file);
16477       print_mips_isa_ext (file, abiflags->isa_ext);
16478       fputs ("\nASEs:", file);
16479       print_mips_ases (file, abiflags->ases);
16480       fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16481       fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16482       fputc ('\n', file);
16483     }
16484 
16485   return true;
16486 }
16487 
16488 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16489 {
16490   { STRING_COMMA_LEN (".lit4"),	  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16491   { STRING_COMMA_LEN (".lit8"),	  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16492   { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16493   { STRING_COMMA_LEN (".sbss"),	 -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16494   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16495   { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
16496   { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
16497   { NULL,		      0,  0, 0,		     0 }
16498 };
16499 
16500 /* Merge non visibility st_other attributes.  Ensure that the
16501    STO_OPTIONAL flag is copied into h->other, even if this is not a
16502    definiton of the symbol.  */
16503 void
16504 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16505 				      unsigned int st_other,
16506 				      bool definition,
16507 				      bool dynamic ATTRIBUTE_UNUSED)
16508 {
16509   if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16510     {
16511       unsigned char other;
16512 
16513       other = (definition ? st_other : h->other);
16514       other &= ~ELF_ST_VISIBILITY (-1);
16515       h->other = other | ELF_ST_VISIBILITY (h->other);
16516     }
16517 
16518   if (!definition
16519       && ELF_MIPS_IS_OPTIONAL (st_other))
16520     h->other |= STO_OPTIONAL;
16521 }
16522 
16523 /* Decide whether an undefined symbol is special and can be ignored.
16524    This is the case for OPTIONAL symbols on IRIX.  */
16525 bool
16526 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16527 {
16528   return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16529 }
16530 
16531 bool
16532 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16533 {
16534   return (sym->st_shndx == SHN_COMMON
16535 	  || sym->st_shndx == SHN_MIPS_ACOMMON
16536 	  || sym->st_shndx == SHN_MIPS_SCOMMON);
16537 }
16538 
16539 /* Return address for Ith PLT stub in section PLT, for relocation REL
16540    or (bfd_vma) -1 if it should not be included.  */
16541 
16542 bfd_vma
16543 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16544 			   const arelent *rel ATTRIBUTE_UNUSED)
16545 {
16546   return (plt->vma
16547 	  + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16548 	  + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16549 }
16550 
16551 /* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
16552    and microMIPS PLT slots we may have a many-to-one mapping between .plt
16553    and .got.plt and also the slots may be of a different size each we walk
16554    the PLT manually fetching instructions and matching them against known
16555    patterns.  To make things easier standard MIPS slots, if any, always come
16556    first.  As we don't create proper ELF symbols we use the UDATA.I member
16557    of ASYMBOL to carry ISA annotation.  The encoding used is the same as
16558    with the ST_OTHER member of the ELF symbol.  */
16559 
16560 long
16561 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16562 				    long symcount ATTRIBUTE_UNUSED,
16563 				    asymbol **syms ATTRIBUTE_UNUSED,
16564 				    long dynsymcount, asymbol **dynsyms,
16565 				    asymbol **ret)
16566 {
16567   static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16568   static const char microsuffix[] = "@micromipsplt";
16569   static const char m16suffix[] = "@mips16plt";
16570   static const char mipssuffix[] = "@plt";
16571 
16572   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16573   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16574   bool micromips_p = MICROMIPS_P (abfd);
16575   Elf_Internal_Shdr *hdr;
16576   bfd_byte *plt_data;
16577   bfd_vma plt_offset;
16578   unsigned int other;
16579   bfd_vma entry_size;
16580   bfd_vma plt0_size;
16581   asection *relplt;
16582   bfd_vma opcode;
16583   asection *plt;
16584   asymbol *send;
16585   size_t size;
16586   char *names;
16587   long counti;
16588   arelent *p;
16589   asymbol *s;
16590   char *nend;
16591   long count;
16592   long pi;
16593   long i;
16594   long n;
16595 
16596   *ret = NULL;
16597 
16598   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16599     return 0;
16600 
16601   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16602   if (relplt == NULL)
16603     return 0;
16604 
16605   hdr = &elf_section_data (relplt)->this_hdr;
16606   if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16607     return 0;
16608 
16609   plt = bfd_get_section_by_name (abfd, ".plt");
16610   if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16611     return 0;
16612 
16613   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16614   if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16615     return -1;
16616   p = relplt->relocation;
16617 
16618   /* Calculating the exact amount of space required for symbols would
16619      require two passes over the PLT, so just pessimise assuming two
16620      PLT slots per relocation.  */
16621   count = NUM_SHDR_ENTRIES (hdr);
16622   counti = count * bed->s->int_rels_per_ext_rel;
16623   size = 2 * count * sizeof (asymbol);
16624   size += count * (sizeof (mipssuffix) +
16625 		   (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16626   for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16627     size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16628 
16629   /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
16630   size += sizeof (asymbol) + sizeof (pltname);
16631 
16632   if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16633     return -1;
16634 
16635   if (plt->size < 16)
16636     return -1;
16637 
16638   s = *ret = bfd_malloc (size);
16639   if (s == NULL)
16640     return -1;
16641   send = s + 2 * count + 1;
16642 
16643   names = (char *) send;
16644   nend = (char *) s + size;
16645   n = 0;
16646 
16647   opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16648   if (opcode == 0x3302fffe)
16649     {
16650       if (!micromips_p)
16651 	return -1;
16652       plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16653       other = STO_MICROMIPS;
16654     }
16655   else if (opcode == 0x0398c1d0)
16656     {
16657       if (!micromips_p)
16658 	return -1;
16659       plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16660       other = STO_MICROMIPS;
16661     }
16662   else
16663     {
16664       plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16665       other = 0;
16666     }
16667 
16668   s->the_bfd = abfd;
16669   s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16670   s->section = plt;
16671   s->value = 0;
16672   s->name = names;
16673   s->udata.i = other;
16674   memcpy (names, pltname, sizeof (pltname));
16675   names += sizeof (pltname);
16676   ++s, ++n;
16677 
16678   pi = 0;
16679   for (plt_offset = plt0_size;
16680        plt_offset + 8 <= plt->size && s < send;
16681        plt_offset += entry_size)
16682     {
16683       bfd_vma gotplt_addr;
16684       const char *suffix;
16685       bfd_vma gotplt_hi;
16686       bfd_vma gotplt_lo;
16687       size_t suffixlen;
16688 
16689       opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16690 
16691       /* Check if the second word matches the expected MIPS16 instruction.  */
16692       if (opcode == 0x651aeb00)
16693 	{
16694 	  if (micromips_p)
16695 	    return -1;
16696 	  /* Truncated table???  */
16697 	  if (plt_offset + 16 > plt->size)
16698 	    break;
16699 	  gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16700 	  entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16701 	  suffixlen = sizeof (m16suffix);
16702 	  suffix = m16suffix;
16703 	  other = STO_MIPS16;
16704 	}
16705       /* Likewise the expected microMIPS instruction (no insn32 mode).  */
16706       else if (opcode == 0xff220000)
16707 	{
16708 	  if (!micromips_p)
16709 	    return -1;
16710 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16711 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16712 	  gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16713 	  gotplt_lo <<= 2;
16714 	  gotplt_addr = gotplt_hi + gotplt_lo;
16715 	  gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16716 	  entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16717 	  suffixlen = sizeof (microsuffix);
16718 	  suffix = microsuffix;
16719 	  other = STO_MICROMIPS;
16720 	}
16721       /* Likewise the expected microMIPS instruction (insn32 mode).  */
16722       else if ((opcode & 0xffff0000) == 0xff2f0000)
16723 	{
16724 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16725 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16726 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16727 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16728 	  gotplt_addr = gotplt_hi + gotplt_lo;
16729 	  entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16730 	  suffixlen = sizeof (microsuffix);
16731 	  suffix = microsuffix;
16732 	  other = STO_MICROMIPS;
16733 	}
16734       /* Otherwise assume standard MIPS code.  */
16735       else
16736 	{
16737 	  gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16738 	  gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16739 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16740 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16741 	  gotplt_addr = gotplt_hi + gotplt_lo;
16742 	  entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16743 	  suffixlen = sizeof (mipssuffix);
16744 	  suffix = mipssuffix;
16745 	  other = 0;
16746 	}
16747       /* Truncated table???  */
16748       if (plt_offset + entry_size > plt->size)
16749 	break;
16750 
16751       for (i = 0;
16752 	   i < count && p[pi].address != gotplt_addr;
16753 	   i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16754 
16755       if (i < count)
16756 	{
16757 	  size_t namelen;
16758 	  size_t len;
16759 
16760 	  *s = **p[pi].sym_ptr_ptr;
16761 	  /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
16762 	     we are defining a symbol, ensure one of them is set.  */
16763 	  if ((s->flags & BSF_LOCAL) == 0)
16764 	    s->flags |= BSF_GLOBAL;
16765 	  s->flags |= BSF_SYNTHETIC;
16766 	  s->section = plt;
16767 	  s->value = plt_offset;
16768 	  s->name = names;
16769 	  s->udata.i = other;
16770 
16771 	  len = strlen ((*p[pi].sym_ptr_ptr)->name);
16772 	  namelen = len + suffixlen;
16773 	  if (names + namelen > nend)
16774 	    break;
16775 
16776 	  memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16777 	  names += len;
16778 	  memcpy (names, suffix, suffixlen);
16779 	  names += suffixlen;
16780 
16781 	  ++s, ++n;
16782 	  pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16783 	}
16784     }
16785 
16786   free (plt_data);
16787 
16788   return n;
16789 }
16790 
16791 /* Return the ABI flags associated with ABFD if available.  */
16792 
16793 Elf_Internal_ABIFlags_v0 *
16794 bfd_mips_elf_get_abiflags (bfd *abfd)
16795 {
16796   struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16797 
16798   return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16799 }
16800 
16801 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16802    field.  Taken from `libc-abis.h' generated at GNU libc build time.
16803    Using a MIPS_ prefix as other libc targets use different values.  */
16804 enum
16805 {
16806   MIPS_LIBC_ABI_DEFAULT = 0,
16807   MIPS_LIBC_ABI_MIPS_PLT,
16808   MIPS_LIBC_ABI_UNIQUE,
16809   MIPS_LIBC_ABI_MIPS_O32_FP64,
16810   MIPS_LIBC_ABI_ABSOLUTE,
16811   MIPS_LIBC_ABI_XHASH,
16812   MIPS_LIBC_ABI_MAX
16813 };
16814 
16815 bool
16816 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16817 {
16818   struct mips_elf_link_hash_table *htab = NULL;
16819   Elf_Internal_Ehdr *i_ehdrp;
16820 
16821   if (!_bfd_elf_init_file_header (abfd, link_info))
16822     return false;
16823 
16824   i_ehdrp = elf_elfheader (abfd);
16825   if (link_info)
16826     {
16827       htab = mips_elf_hash_table (link_info);
16828       BFD_ASSERT (htab != NULL);
16829     }
16830 
16831   if (htab != NULL
16832       && htab->use_plts_and_copy_relocs
16833       && htab->root.target_os != is_vxworks)
16834     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16835 
16836   if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16837       || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16838     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16839 
16840   /* Mark that we need support for absolute symbols in the dynamic loader.  */
16841   if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16842     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16843 
16844   /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16845      if it is the only hash section that will be created.  */
16846   if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16847     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16848   return true;
16849 }
16850 
16851 int
16852 _bfd_mips_elf_compact_eh_encoding
16853   (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16854 {
16855   return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16856 }
16857 
16858 /* Return the opcode for can't unwind.  */
16859 
16860 int
16861 _bfd_mips_elf_cant_unwind_opcode
16862   (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16863 {
16864   return COMPACT_EH_CANT_UNWIND_OPCODE;
16865 }
16866 
16867 /* Record a position XLAT_LOC in the xlat translation table, associated with
16868    the hash entry H.  The entry in the translation table will later be
16869    populated with the real symbol dynindx.  */
16870 
16871 void
16872 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16873 				   bfd_vma xlat_loc)
16874 {
16875   struct mips_elf_link_hash_entry *hmips;
16876 
16877   hmips = (struct mips_elf_link_hash_entry *) h;
16878   hmips->mipsxhash_loc = xlat_loc;
16879 }
16880