[MIPS] Add Loongson 3A2000/3A3000 proccessor support.
[external/binutils.git] / gold / mips.cc
1 // mips.cc -- mips target support for gold.
2
3 // Copyright (C) 2011-2018 Free Software Foundation, Inc.
4 // Written by Sasa Stankovic <sasa.stankovic@imgtec.com>
5 //        and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
6 // This file contains borrowed and adapted code from bfd/elfxx-mips.c.
7
8 // This file is part of gold.
9
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
24
25 #include "gold.h"
26
27 #include <algorithm>
28 #include <set>
29 #include <sstream>
30 #include "demangle.h"
31
32 #include "elfcpp.h"
33 #include "parameters.h"
34 #include "reloc.h"
35 #include "mips.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "layout.h"
39 #include "output.h"
40 #include "copy-relocs.h"
41 #include "target.h"
42 #include "target-reloc.h"
43 #include "target-select.h"
44 #include "tls.h"
45 #include "errors.h"
46 #include "gc.h"
47 #include "attributes.h"
48 #include "nacl.h"
49
50 namespace
51 {
52 using namespace gold;
53
54 template<int size, bool big_endian>
55 class Mips_output_data_plt;
56
57 template<int size, bool big_endian>
58 class Mips_output_data_got;
59
60 template<int size, bool big_endian>
61 class Target_mips;
62
63 template<int size, bool big_endian>
64 class Mips_output_section_reginfo;
65
66 template<int size, bool big_endian>
67 class Mips_output_section_options;
68
69 template<int size, bool big_endian>
70 class Mips_output_data_la25_stub;
71
72 template<int size, bool big_endian>
73 class Mips_output_data_mips_stubs;
74
75 template<int size>
76 class Mips_symbol;
77
78 template<int size, bool big_endian>
79 class Mips_got_info;
80
81 template<int size, bool big_endian>
82 class Mips_relobj;
83
84 class Mips16_stub_section_base;
85
86 template<int size, bool big_endian>
87 class Mips16_stub_section;
88
89 // The ABI says that every symbol used by dynamic relocations must have
90 // a global GOT entry.  Among other things, this provides the dynamic
91 // linker with a free, directly-indexed cache.  The GOT can therefore
92 // contain symbols that are not referenced by GOT relocations themselves
93 // (in other words, it may have symbols that are not referenced by things
94 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
95
96 // GOT relocations are less likely to overflow if we put the associated
97 // GOT entries towards the beginning.  We therefore divide the global
98 // GOT entries into two areas: "normal" and "reloc-only".  Entries in
99 // the first area can be used for both dynamic relocations and GP-relative
100 // accesses, while those in the "reloc-only" area are for dynamic
101 // relocations only.
102
103 // These GGA_* ("Global GOT Area") values are organised so that lower
104 // values are more general than higher values.  Also, non-GGA_NONE
105 // values are ordered by the position of the area in the GOT.
106
107 enum Global_got_area
108 {
109   GGA_NORMAL = 0,
110   GGA_RELOC_ONLY = 1,
111   GGA_NONE = 2
112 };
113
114 // The types of GOT entries needed for this platform.
115 // These values are exposed to the ABI in an incremental link.
116 // Do not renumber existing values without changing the version
117 // number of the .gnu_incremental_inputs section.
118 enum Got_type
119 {
120   GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
121   GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
122   GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
123
124   // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
125   GOT_TYPE_STANDARD_MULTIGOT = 3,
126   GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
127   GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
128 };
129
130 // TLS type of GOT entry.
131 enum Got_tls_type
132 {
133   GOT_TLS_NONE = 0,
134   GOT_TLS_GD = 1,
135   GOT_TLS_LDM = 2,
136   GOT_TLS_IE = 4
137 };
138
139 // Values found in the r_ssym field of a relocation entry.
140 enum Special_relocation_symbol
141 {
142   RSS_UNDEF = 0,    // None - value is zero.
143   RSS_GP = 1,       // Value of GP.
144   RSS_GP0 = 2,      // Value of GP in object being relocated.
145   RSS_LOC = 3       // Address of location being relocated.
146 };
147
148 // Whether the section is readonly.
149 static inline bool
150 is_readonly_section(Output_section* output_section)
151 {
152   elfcpp::Elf_Xword section_flags = output_section->flags();
153   elfcpp::Elf_Word section_type = output_section->type();
154
155   if (section_type == elfcpp::SHT_NOBITS)
156     return false;
157
158   if (section_flags & elfcpp::SHF_WRITE)
159     return false;
160
161   return true;
162 }
163
164 // Return TRUE if a relocation of type R_TYPE from OBJECT might
165 // require an la25 stub.  See also local_pic_function, which determines
166 // whether the destination function ever requires a stub.
167 template<int size, bool big_endian>
168 static inline bool
169 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
170                            unsigned int r_type, bool target_is_16_bit_code)
171 {
172   // We specifically ignore branches and jumps from EF_PIC objects,
173   // where the onus is on the compiler or programmer to perform any
174   // necessary initialization of $25.  Sometimes such initialization
175   // is unnecessary; for example, -mno-shared functions do not use
176   // the incoming value of $25, and may therefore be called directly.
177   if (object->is_pic())
178     return false;
179
180   switch (r_type)
181     {
182     case elfcpp::R_MIPS_26:
183     case elfcpp::R_MIPS_PC16:
184     case elfcpp::R_MIPS_PC21_S2:
185     case elfcpp::R_MIPS_PC26_S2:
186     case elfcpp::R_MICROMIPS_26_S1:
187     case elfcpp::R_MICROMIPS_PC7_S1:
188     case elfcpp::R_MICROMIPS_PC10_S1:
189     case elfcpp::R_MICROMIPS_PC16_S1:
190     case elfcpp::R_MICROMIPS_PC23_S2:
191       return true;
192
193     case elfcpp::R_MIPS16_26:
194       return !target_is_16_bit_code;
195
196     default:
197       return false;
198     }
199 }
200
201 // Return true if SYM is a locally-defined PIC function, in the sense
202 // that it or its fn_stub might need $25 to be valid on entry.
203 // Note that MIPS16 functions set up $gp using PC-relative instructions,
204 // so they themselves never need $25 to be valid.  Only non-MIPS16
205 // entry points are of interest here.
206 template<int size, bool big_endian>
207 static inline bool
208 local_pic_function(Mips_symbol<size>* sym)
209 {
210   bool def_regular = (sym->source() == Symbol::FROM_OBJECT
211                       && !sym->object()->is_dynamic()
212                       && !sym->is_undefined());
213
214   if (sym->is_defined() && def_regular)
215     {
216       Mips_relobj<size, big_endian>* object =
217         static_cast<Mips_relobj<size, big_endian>*>(sym->object());
218
219       if ((object->is_pic() || sym->is_pic())
220           && (!sym->is_mips16()
221               || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
222         return true;
223     }
224   return false;
225 }
226
227 static inline bool
228 hi16_reloc(int r_type)
229 {
230   return (r_type == elfcpp::R_MIPS_HI16
231           || r_type == elfcpp::R_MIPS16_HI16
232           || r_type == elfcpp::R_MICROMIPS_HI16
233           || r_type == elfcpp::R_MIPS_PCHI16);
234 }
235
236 static inline bool
237 lo16_reloc(int r_type)
238 {
239   return (r_type == elfcpp::R_MIPS_LO16
240           || r_type == elfcpp::R_MIPS16_LO16
241           || r_type == elfcpp::R_MICROMIPS_LO16
242           || r_type == elfcpp::R_MIPS_PCLO16);
243 }
244
245 static inline bool
246 got16_reloc(unsigned int r_type)
247 {
248   return (r_type == elfcpp::R_MIPS_GOT16
249           || r_type == elfcpp::R_MIPS16_GOT16
250           || r_type == elfcpp::R_MICROMIPS_GOT16);
251 }
252
253 static inline bool
254 call_lo16_reloc(unsigned int r_type)
255 {
256   return (r_type == elfcpp::R_MIPS_CALL_LO16
257           || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
258 }
259
260 static inline bool
261 got_lo16_reloc(unsigned int r_type)
262 {
263   return (r_type == elfcpp::R_MIPS_GOT_LO16
264           || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
265 }
266
267 static inline bool
268 eh_reloc(unsigned int r_type)
269 {
270   return (r_type == elfcpp::R_MIPS_EH);
271 }
272
273 static inline bool
274 got_disp_reloc(unsigned int r_type)
275 {
276   return (r_type == elfcpp::R_MIPS_GOT_DISP
277           || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
278 }
279
280 static inline bool
281 got_page_reloc(unsigned int r_type)
282 {
283   return (r_type == elfcpp::R_MIPS_GOT_PAGE
284           || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
285 }
286
287 static inline bool
288 tls_gd_reloc(unsigned int r_type)
289 {
290   return (r_type == elfcpp::R_MIPS_TLS_GD
291           || r_type == elfcpp::R_MIPS16_TLS_GD
292           || r_type == elfcpp::R_MICROMIPS_TLS_GD);
293 }
294
295 static inline bool
296 tls_gottprel_reloc(unsigned int r_type)
297 {
298   return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
299           || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
300           || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
301 }
302
303 static inline bool
304 tls_ldm_reloc(unsigned int r_type)
305 {
306   return (r_type == elfcpp::R_MIPS_TLS_LDM
307           || r_type == elfcpp::R_MIPS16_TLS_LDM
308           || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
309 }
310
311 static inline bool
312 mips16_call_reloc(unsigned int r_type)
313 {
314   return (r_type == elfcpp::R_MIPS16_26
315           || r_type == elfcpp::R_MIPS16_CALL16);
316 }
317
318 static inline bool
319 jal_reloc(unsigned int r_type)
320 {
321   return (r_type == elfcpp::R_MIPS_26
322           || r_type == elfcpp::R_MIPS16_26
323           || r_type == elfcpp::R_MICROMIPS_26_S1);
324 }
325
326 static inline bool
327 micromips_branch_reloc(unsigned int r_type)
328 {
329   return (r_type == elfcpp::R_MICROMIPS_26_S1
330           || r_type == elfcpp::R_MICROMIPS_PC16_S1
331           || r_type == elfcpp::R_MICROMIPS_PC10_S1
332           || r_type == elfcpp::R_MICROMIPS_PC7_S1);
333 }
334
335 // Check if R_TYPE is a MIPS16 reloc.
336 static inline bool
337 mips16_reloc(unsigned int r_type)
338 {
339   switch (r_type)
340     {
341     case elfcpp::R_MIPS16_26:
342     case elfcpp::R_MIPS16_GPREL:
343     case elfcpp::R_MIPS16_GOT16:
344     case elfcpp::R_MIPS16_CALL16:
345     case elfcpp::R_MIPS16_HI16:
346     case elfcpp::R_MIPS16_LO16:
347     case elfcpp::R_MIPS16_TLS_GD:
348     case elfcpp::R_MIPS16_TLS_LDM:
349     case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
350     case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
351     case elfcpp::R_MIPS16_TLS_GOTTPREL:
352     case elfcpp::R_MIPS16_TLS_TPREL_HI16:
353     case elfcpp::R_MIPS16_TLS_TPREL_LO16:
354       return true;
355
356     default:
357       return false;
358     }
359 }
360
361 // Check if R_TYPE is a microMIPS reloc.
362 static inline bool
363 micromips_reloc(unsigned int r_type)
364 {
365   switch (r_type)
366     {
367     case elfcpp::R_MICROMIPS_26_S1:
368     case elfcpp::R_MICROMIPS_HI16:
369     case elfcpp::R_MICROMIPS_LO16:
370     case elfcpp::R_MICROMIPS_GPREL16:
371     case elfcpp::R_MICROMIPS_LITERAL:
372     case elfcpp::R_MICROMIPS_GOT16:
373     case elfcpp::R_MICROMIPS_PC7_S1:
374     case elfcpp::R_MICROMIPS_PC10_S1:
375     case elfcpp::R_MICROMIPS_PC16_S1:
376     case elfcpp::R_MICROMIPS_CALL16:
377     case elfcpp::R_MICROMIPS_GOT_DISP:
378     case elfcpp::R_MICROMIPS_GOT_PAGE:
379     case elfcpp::R_MICROMIPS_GOT_OFST:
380     case elfcpp::R_MICROMIPS_GOT_HI16:
381     case elfcpp::R_MICROMIPS_GOT_LO16:
382     case elfcpp::R_MICROMIPS_SUB:
383     case elfcpp::R_MICROMIPS_HIGHER:
384     case elfcpp::R_MICROMIPS_HIGHEST:
385     case elfcpp::R_MICROMIPS_CALL_HI16:
386     case elfcpp::R_MICROMIPS_CALL_LO16:
387     case elfcpp::R_MICROMIPS_SCN_DISP:
388     case elfcpp::R_MICROMIPS_JALR:
389     case elfcpp::R_MICROMIPS_HI0_LO16:
390     case elfcpp::R_MICROMIPS_TLS_GD:
391     case elfcpp::R_MICROMIPS_TLS_LDM:
392     case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
393     case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
394     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
395     case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
396     case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
397     case elfcpp::R_MICROMIPS_GPREL7_S2:
398     case elfcpp::R_MICROMIPS_PC23_S2:
399       return true;
400
401     default:
402       return false;
403     }
404 }
405
406 static inline bool
407 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
408 {
409   switch (high_reloc)
410     {
411     case elfcpp::R_MIPS_HI16:
412     case elfcpp::R_MIPS_GOT16:
413       return lo16_reloc == elfcpp::R_MIPS_LO16;
414     case elfcpp::R_MIPS_PCHI16:
415       return lo16_reloc == elfcpp::R_MIPS_PCLO16;
416     case elfcpp::R_MIPS16_HI16:
417     case elfcpp::R_MIPS16_GOT16:
418       return lo16_reloc == elfcpp::R_MIPS16_LO16;
419     case elfcpp::R_MICROMIPS_HI16:
420     case elfcpp::R_MICROMIPS_GOT16:
421       return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
422     default:
423       return false;
424     }
425 }
426
427 // This class is used to hold information about one GOT entry.
428 // There are three types of entry:
429 //
430 //    (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
431 //          (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
432 //    (2) a SYMBOL address, where SYMBOL is not local to an input object
433 //          (sym != NULL, symndx == -1)
434 //    (3) a TLS LDM slot (there's only one of these per GOT.)
435 //          (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
436
437 template<int size, bool big_endian>
438 class Mips_got_entry
439 {
440   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
441
442  public:
443   Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
444                  Mips_address addend, unsigned char tls_type,
445                  unsigned int shndx, bool is_section_symbol)
446     : addend_(addend), symndx_(symndx), tls_type_(tls_type),
447       is_section_symbol_(is_section_symbol), shndx_(shndx)
448   { this->d.object = object; }
449
450   Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
451     : addend_(0), symndx_(-1U), tls_type_(tls_type),
452       is_section_symbol_(false), shndx_(-1U)
453   { this->d.sym = sym; }
454
455   // Return whether this entry is for a local symbol.
456   bool
457   is_for_local_symbol() const
458   { return this->symndx_ != -1U; }
459
460   // Return whether this entry is for a global symbol.
461   bool
462   is_for_global_symbol() const
463   { return this->symndx_ == -1U; }
464
465   // Return the hash of this entry.
466   size_t
467   hash() const
468   {
469     if (this->tls_type_ == GOT_TLS_LDM)
470       return this->symndx_ + (1 << 18);
471
472     size_t name_hash_value = gold::string_hash<char>(
473         (this->symndx_ != -1U)
474          ? this->d.object->name().c_str()
475          : this->d.sym->name());
476     size_t addend = this->addend_;
477     return name_hash_value ^ this->symndx_ ^ (addend << 16);
478   }
479
480   // Return whether this entry is equal to OTHER.
481   bool
482   equals(Mips_got_entry<size, big_endian>* other) const
483   {
484     if (this->symndx_ != other->symndx_
485         || this->tls_type_ != other->tls_type_)
486       return false;
487
488     if (this->tls_type_ == GOT_TLS_LDM)
489       return true;
490
491     return (((this->symndx_ != -1U)
492               ? (this->d.object == other->d.object)
493               : (this->d.sym == other->d.sym))
494             && (this->addend_ == other->addend_));
495   }
496
497   // Return input object that needs this GOT entry.
498   Mips_relobj<size, big_endian>*
499   object() const
500   {
501     gold_assert(this->symndx_ != -1U);
502     return this->d.object;
503   }
504
505   // Return local symbol index for local GOT entries.
506   unsigned int
507   symndx() const
508   {
509     gold_assert(this->symndx_ != -1U);
510     return this->symndx_;
511   }
512
513   // Return the relocation addend for local GOT entries.
514   Mips_address
515   addend() const
516   { return this->addend_; }
517
518   // Return global symbol for global GOT entries.
519   Mips_symbol<size>*
520   sym() const
521   {
522     gold_assert(this->symndx_ == -1U);
523     return this->d.sym;
524   }
525
526   // Return whether this is a TLS GOT entry.
527   bool
528   is_tls_entry() const
529   { return this->tls_type_ != GOT_TLS_NONE; }
530
531   // Return TLS type of this GOT entry.
532   unsigned char
533   tls_type() const
534   { return this->tls_type_; }
535
536   // Return section index of the local symbol for local GOT entries.
537   unsigned int
538   shndx() const
539   { return this->shndx_; }
540
541   // Return whether this is a STT_SECTION symbol.
542   bool
543   is_section_symbol() const
544   { return this->is_section_symbol_; }
545
546  private:
547   // The addend.
548   Mips_address addend_;
549
550   // The index of the symbol if we have a local symbol; -1 otherwise.
551   unsigned int symndx_;
552
553   union
554   {
555     // The input object for local symbols that needs the GOT entry.
556     Mips_relobj<size, big_endian>* object;
557     // If symndx == -1, the global symbol corresponding to this GOT entry.  The
558     // symbol's entry is in the local area if mips_sym->global_got_area is
559     // GGA_NONE, otherwise it is in the global area.
560     Mips_symbol<size>* sym;
561   } d;
562
563   // The TLS type of this GOT entry.  An LDM GOT entry will be a local
564   // symbol entry with r_symndx == 0.
565   unsigned char tls_type_;
566
567   // Whether this is a STT_SECTION symbol.
568   bool is_section_symbol_;
569
570   // For local GOT entries, section index of the local symbol.
571   unsigned int shndx_;
572 };
573
574 // Hash for Mips_got_entry.
575
576 template<int size, bool big_endian>
577 class Mips_got_entry_hash
578 {
579  public:
580   size_t
581   operator()(Mips_got_entry<size, big_endian>* entry) const
582   { return entry->hash(); }
583 };
584
585 // Equality for Mips_got_entry.
586
587 template<int size, bool big_endian>
588 class Mips_got_entry_eq
589 {
590  public:
591   bool
592   operator()(Mips_got_entry<size, big_endian>* e1,
593              Mips_got_entry<size, big_endian>* e2) const
594   { return e1->equals(e2); }
595 };
596
597 // Hash for Mips_symbol.
598
599 template<int size>
600 class Mips_symbol_hash
601 {
602  public:
603   size_t
604   operator()(Mips_symbol<size>* sym) const
605   { return sym->hash(); }
606 };
607
608 // Got_page_range.  This class describes a range of addends: [MIN_ADDEND,
609 // MAX_ADDEND].  The instances form a non-overlapping list that is sorted by
610 // increasing MIN_ADDEND.
611
612 struct Got_page_range
613 {
614   Got_page_range()
615     : next(NULL), min_addend(0), max_addend(0)
616   { }
617
618   Got_page_range* next;
619   int min_addend;
620   int max_addend;
621
622   // Return the maximum number of GOT page entries required.
623   int
624   get_max_pages()
625   { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
626 };
627
628 // Got_page_entry.  This class describes the range of addends that are applied
629 // to page relocations against a given symbol.
630
631 struct Got_page_entry
632 {
633   Got_page_entry()
634     : object(NULL), symndx(-1U), ranges(NULL)
635   { }
636
637   Got_page_entry(Object* object_, unsigned int symndx_)
638     : object(object_), symndx(symndx_), ranges(NULL)
639   { }
640
641   // The input object that needs the GOT page entry.
642   Object* object;
643   // The index of the symbol, as stored in the relocation r_info.
644   unsigned int symndx;
645   // The ranges for this page entry.
646   Got_page_range* ranges;
647 };
648
649 // Hash for Got_page_entry.
650
651 struct Got_page_entry_hash
652 {
653   size_t
654   operator()(Got_page_entry* entry) const
655   { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
656 };
657
658 // Equality for Got_page_entry.
659
660 struct Got_page_entry_eq
661 {
662   bool
663   operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
664   {
665     return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
666   }
667 };
668
669 // This class is used to hold .got information when linking.
670
671 template<int size, bool big_endian>
672 class Mips_got_info
673 {
674   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
675   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
676     Reloc_section;
677   typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
678
679   // Unordered set of GOT entries.
680   typedef Unordered_set<Mips_got_entry<size, big_endian>*,
681       Mips_got_entry_hash<size, big_endian>,
682       Mips_got_entry_eq<size, big_endian> > Got_entry_set;
683
684   // Unordered set of GOT page entries.
685   typedef Unordered_set<Got_page_entry*,
686       Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
687
688   // Unordered set of global GOT entries.
689   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
690       Global_got_entry_set;
691
692  public:
693   Mips_got_info()
694     : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
695       tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
696       got_entries_(), got_page_entries_(), got_page_offset_start_(0),
697       got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
698       offset_(0)
699   { }
700
701   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
702   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
703   void
704   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
705                           unsigned int symndx, Mips_address addend,
706                           unsigned int r_type, unsigned int shndx,
707                           bool is_section_symbol);
708
709   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
710   // in OBJECT.  FOR_CALL is true if the caller is only interested in
711   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
712   // relocation.
713   void
714   record_global_got_symbol(Mips_symbol<size>* mips_sym,
715                            Mips_relobj<size, big_endian>* object,
716                            unsigned int r_type, bool dyn_reloc, bool for_call);
717
718   // Add ENTRY to master GOT and to OBJECT's GOT.
719   void
720   record_got_entry(Mips_got_entry<size, big_endian>* entry,
721                    Mips_relobj<size, big_endian>* object);
722
723   // Record that OBJECT has a page relocation against symbol SYMNDX and
724   // that ADDEND is the addend for that relocation.
725   void
726   record_got_page_entry(Mips_relobj<size, big_endian>* object,
727                         unsigned int symndx, int addend);
728
729   // Create all entries that should be in the local part of the GOT.
730   void
731   add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
732
733   // Create GOT page entries.
734   void
735   add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
736
737   // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
738   void
739   add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
740                      unsigned int non_reloc_only_global_gotno);
741
742   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
743   void
744   add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
745
746   // Create TLS GOT entries.
747   void
748   add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
749
750   // Decide whether the symbol needs an entry in the global part of the primary
751   // GOT, setting global_got_area accordingly.  Count the number of global
752   // symbols that are in the primary GOT only because they have dynamic
753   // relocations R_MIPS_REL32 against them (reloc_only_gotno).
754   void
755   count_got_symbols(Symbol_table* symtab);
756
757   // Return the offset of GOT page entry for VALUE.
758   unsigned int
759   get_got_page_offset(Mips_address value,
760                       Mips_output_data_got<size, big_endian>* got);
761
762   // Count the number of GOT entries required.
763   void
764   count_got_entries();
765
766   // Count the number of GOT entries required by ENTRY.  Accumulate the result.
767   void
768   count_got_entry(Mips_got_entry<size, big_endian>* entry);
769
770   // Add FROM's GOT entries.
771   void
772   add_got_entries(Mips_got_info<size, big_endian>* from);
773
774   // Add FROM's GOT page entries.
775   void
776   add_got_page_count(Mips_got_info<size, big_endian>* from);
777
778   // Return GOT size.
779   unsigned int
780   got_size() const
781   { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
782              + this->tls_gotno_) * size/8);
783   }
784
785   // Return the number of local GOT entries.
786   unsigned int
787   local_gotno() const
788   { return this->local_gotno_; }
789
790   // Return the maximum number of page GOT entries needed.
791   unsigned int
792   page_gotno() const
793   { return this->page_gotno_; }
794
795   // Return the number of global GOT entries.
796   unsigned int
797   global_gotno() const
798   { return this->global_gotno_; }
799
800   // Set the number of global GOT entries.
801   void
802   set_global_gotno(unsigned int global_gotno)
803   { this->global_gotno_ = global_gotno; }
804
805   // Return the number of GGA_RELOC_ONLY global GOT entries.
806   unsigned int
807   reloc_only_gotno() const
808   { return this->reloc_only_gotno_; }
809
810   // Return the number of TLS GOT entries.
811   unsigned int
812   tls_gotno() const
813   { return this->tls_gotno_; }
814
815   // Return the GOT type for this GOT.  Used for multi-GOT links only.
816   unsigned int
817   multigot_got_type(unsigned int got_type) const
818   {
819     switch (got_type)
820       {
821       case GOT_TYPE_STANDARD:
822         return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
823       case GOT_TYPE_TLS_OFFSET:
824         return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
825       case GOT_TYPE_TLS_PAIR:
826         return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
827       default:
828         gold_unreachable();
829       }
830   }
831
832   // Remove lazy-binding stubs for global symbols in this GOT.
833   void
834   remove_lazy_stubs(Target_mips<size, big_endian>* target);
835
836   // Return offset of this GOT from the start of .got section.
837   unsigned int
838   offset() const
839   { return this->offset_; }
840
841   // Set offset of this GOT from the start of .got section.
842   void
843   set_offset(unsigned int offset)
844   { this->offset_ = offset; }
845
846   // Set index of this GOT in multi-GOT links.
847   void
848   set_index(unsigned int index)
849   { this->index_ = index; }
850
851   // Return next GOT in multi-GOT links.
852   Mips_got_info<size, big_endian>*
853   next() const
854   { return this->next_; }
855
856   // Set next GOT in multi-GOT links.
857   void
858   set_next(Mips_got_info<size, big_endian>* next)
859   { this->next_ = next; }
860
861   // Return the offset of TLS LDM entry for this GOT.
862   unsigned int
863   tls_ldm_offset() const
864   { return this->tls_ldm_offset_; }
865
866   // Set the offset of TLS LDM entry for this GOT.
867   void
868   set_tls_ldm_offset(unsigned int tls_ldm_offset)
869   { this->tls_ldm_offset_ = tls_ldm_offset; }
870
871   Global_got_entry_set&
872   global_got_symbols()
873   { return this->global_got_symbols_; }
874
875   // Return the GOT_TLS_* type required by relocation type R_TYPE.
876   static int
877   mips_elf_reloc_tls_type(unsigned int r_type)
878   {
879     if (tls_gd_reloc(r_type))
880       return GOT_TLS_GD;
881
882     if (tls_ldm_reloc(r_type))
883       return GOT_TLS_LDM;
884
885     if (tls_gottprel_reloc(r_type))
886       return GOT_TLS_IE;
887
888     return GOT_TLS_NONE;
889   }
890
891   // Return the number of GOT slots needed for GOT TLS type TYPE.
892   static int
893   mips_tls_got_entries(unsigned int type)
894   {
895     switch (type)
896       {
897       case GOT_TLS_GD:
898       case GOT_TLS_LDM:
899         return 2;
900
901       case GOT_TLS_IE:
902         return 1;
903
904       case GOT_TLS_NONE:
905         return 0;
906
907       default:
908         gold_unreachable();
909       }
910   }
911
912  private:
913   // The number of local GOT entries.
914   unsigned int local_gotno_;
915   // The maximum number of page GOT entries needed.
916   unsigned int page_gotno_;
917   // The number of global GOT entries.
918   unsigned int global_gotno_;
919   // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
920   unsigned int reloc_only_gotno_;
921   // The number of TLS GOT entries.
922   unsigned int tls_gotno_;
923   // The offset of TLS LDM entry for this GOT.
924   unsigned int tls_ldm_offset_;
925   // All symbols that have global GOT entry.
926   Global_got_entry_set global_got_symbols_;
927   // A hash table holding GOT entries.
928   Got_entry_set got_entries_;
929   // A hash table of GOT page entries (only used in master GOT).
930   Got_page_entry_set got_page_entries_;
931   // The offset of first GOT page entry for this GOT.
932   unsigned int got_page_offset_start_;
933   // The offset of next available GOT page entry for this GOT.
934   unsigned int got_page_offset_next_;
935   // A hash table that maps GOT page entry value to the GOT offset where
936   // the entry is located.
937   Got_page_offsets got_page_offsets_;
938   // In multi-GOT links, a pointer to the next GOT.
939   Mips_got_info<size, big_endian>* next_;
940   // Index of this GOT in multi-GOT links.
941   unsigned int index_;
942   // The offset of this GOT in multi-GOT links.
943   unsigned int offset_;
944 };
945
946 // This is a helper class used during relocation scan.  It records GOT16 addend.
947
948 template<int size, bool big_endian>
949 struct got16_addend
950 {
951   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
952
953   got16_addend(const Sized_relobj_file<size, big_endian>* _object,
954                unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
955                Mips_address _addend)
956     : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
957       addend(_addend)
958   { }
959
960   const Sized_relobj_file<size, big_endian>* object;
961   unsigned int shndx;
962   unsigned int r_type;
963   unsigned int r_sym;
964   Mips_address addend;
965 };
966
967 // .MIPS.abiflags section content
968
969 template<bool big_endian>
970 struct Mips_abiflags
971 {
972   typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
973   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
974   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
975
976   Mips_abiflags()
977     : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
978       cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
979   { }
980
981   // Version of flags structure.
982   Valtype16 version;
983   // The level of the ISA: 1-5, 32, 64.
984   Valtype8 isa_level;
985   // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
986   Valtype8 isa_rev;
987   // The size of general purpose registers.
988   Valtype8 gpr_size;
989   // The size of co-processor 1 registers.
990   Valtype8 cpr1_size;
991   // The size of co-processor 2 registers.
992   Valtype8 cpr2_size;
993   // The floating-point ABI.
994   Valtype8 fp_abi;
995   // Processor-specific extension.
996   Valtype32 isa_ext;
997   // Mask of ASEs used.
998   Valtype32 ases;
999   // Mask of general flags.
1000   Valtype32 flags1;
1001   Valtype32 flags2;
1002 };
1003
1004 // Mips_symbol class.  Holds additional symbol information needed for Mips.
1005
1006 template<int size>
1007 class Mips_symbol : public Sized_symbol<size>
1008 {
1009  public:
1010   Mips_symbol()
1011     : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1012       has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1013       pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1014       global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1015       needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1016       comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1017       mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1018   { }
1019
1020   // Return whether this is a MIPS16 symbol.
1021   bool
1022   is_mips16() const
1023   {
1024     // (st_other & STO_MIPS16) == STO_MIPS16
1025     return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1026             == elfcpp::STO_MIPS16 >> 2);
1027   }
1028
1029   // Return whether this is a microMIPS symbol.
1030   bool
1031   is_micromips() const
1032   {
1033     // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1034     return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1035             == elfcpp::STO_MICROMIPS >> 2);
1036   }
1037
1038   // Return whether the symbol needs MIPS16 fn_stub.
1039   bool
1040   need_fn_stub() const
1041   { return this->need_fn_stub_; }
1042
1043   // Set that the symbol needs MIPS16 fn_stub.
1044   void
1045   set_need_fn_stub()
1046   { this->need_fn_stub_ = true; }
1047
1048   // Return whether this symbol is referenced by branch relocations from
1049   // any non-PIC input file.
1050   bool
1051   has_nonpic_branches() const
1052   { return this->has_nonpic_branches_; }
1053
1054   // Set that this symbol is referenced by branch relocations from
1055   // any non-PIC input file.
1056   void
1057   set_has_nonpic_branches()
1058   { this->has_nonpic_branches_ = true; }
1059
1060   // Return the offset of the la25 stub for this symbol from the start of the
1061   // la25 stub section.
1062   unsigned int
1063   la25_stub_offset() const
1064   { return this->la25_stub_offset_; }
1065
1066   // Set the offset of the la25 stub for this symbol from the start of the
1067   // la25 stub section.
1068   void
1069   set_la25_stub_offset(unsigned int offset)
1070   { this->la25_stub_offset_ = offset; }
1071
1072   // Return whether the symbol has la25 stub.  This is true if this symbol is
1073   // for a PIC function, and there are non-PIC branches and jumps to it.
1074   bool
1075   has_la25_stub() const
1076   { return this->la25_stub_offset_ != -1U; }
1077
1078   // Return whether there is a relocation against this symbol that must be
1079   // resolved by the static linker (that is, the relocation cannot possibly
1080   // be made dynamic).
1081   bool
1082   has_static_relocs() const
1083   { return this->has_static_relocs_; }
1084
1085   // Set that there is a relocation against this symbol that must be resolved
1086   // by the static linker (that is, the relocation cannot possibly be made
1087   // dynamic).
1088   void
1089   set_has_static_relocs()
1090   { this->has_static_relocs_ = true; }
1091
1092   // Return whether we must not create a lazy-binding stub for this symbol.
1093   bool
1094   no_lazy_stub() const
1095   { return this->no_lazy_stub_; }
1096
1097   // Set that we must not create a lazy-binding stub for this symbol.
1098   void
1099   set_no_lazy_stub()
1100   { this->no_lazy_stub_ = true; }
1101
1102   // Return the offset of the lazy-binding stub for this symbol from the start
1103   // of .MIPS.stubs section.
1104   unsigned int
1105   lazy_stub_offset() const
1106   { return this->lazy_stub_offset_; }
1107
1108   // Set the offset of the lazy-binding stub for this symbol from the start
1109   // of .MIPS.stubs section.
1110   void
1111   set_lazy_stub_offset(unsigned int offset)
1112   { this->lazy_stub_offset_ = offset; }
1113
1114   // Return whether there are any relocations for this symbol where
1115   // pointer equality matters.
1116   bool
1117   pointer_equality_needed() const
1118   { return this->pointer_equality_needed_; }
1119
1120   // Set that there are relocations for this symbol where pointer equality
1121   // matters.
1122   void
1123   set_pointer_equality_needed()
1124   { this->pointer_equality_needed_ = true; }
1125
1126   // Return global GOT area where this symbol in located.
1127   Global_got_area
1128   global_got_area() const
1129   { return this->global_got_area_; }
1130
1131   // Set global GOT area where this symbol in located.
1132   void
1133   set_global_got_area(Global_got_area global_got_area)
1134   { this->global_got_area_ = global_got_area; }
1135
1136   // Return the global GOT offset for this symbol.  For multi-GOT links, this
1137   // returns the offset from the start of .got section to the first GOT entry
1138   // for the symbol.  Note that in multi-GOT links the symbol can have entry
1139   // in more than one GOT.
1140   unsigned int
1141   global_gotoffset() const
1142   { return this->global_gotoffset_; }
1143
1144   // Set the global GOT offset for this symbol.  Note that in multi-GOT links
1145   // the symbol can have entry in more than one GOT.  This method will set
1146   // the offset only if it is less than current offset.
1147   void
1148   set_global_gotoffset(unsigned int offset)
1149   {
1150     if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1151       this->global_gotoffset_ = offset;
1152   }
1153
1154   // Return whether all GOT relocations for this symbol are for calls.
1155   bool
1156   got_only_for_calls() const
1157   { return this->got_only_for_calls_; }
1158
1159   // Set that there is a GOT relocation for this symbol that is not for call.
1160   void
1161   set_got_not_only_for_calls()
1162   { this->got_only_for_calls_ = false; }
1163
1164   // Return whether this is a PIC symbol.
1165   bool
1166   is_pic() const
1167   {
1168     // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1169     return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1170             == (elfcpp::STO_MIPS_PIC >> 2));
1171   }
1172
1173   // Set the flag in st_other field that marks this symbol as PIC.
1174   void
1175   set_pic()
1176   {
1177     if (this->is_mips16())
1178       // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1179       this->set_nonvis((this->nonvis()
1180                         & ~((elfcpp::STO_MIPS16 >> 2)
1181                             | (elfcpp::STO_MIPS_FLAGS >> 2)))
1182                        | (elfcpp::STO_MIPS_PIC >> 2));
1183     else
1184       // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1185       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1186                        | (elfcpp::STO_MIPS_PIC >> 2));
1187   }
1188
1189   // Set the flag in st_other field that marks this symbol as PLT.
1190   void
1191   set_mips_plt()
1192   {
1193     if (this->is_mips16())
1194       // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1195       this->set_nonvis((this->nonvis()
1196                         & ((elfcpp::STO_MIPS16 >> 2)
1197                            | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1198                        | (elfcpp::STO_MIPS_PLT >> 2));
1199
1200     else
1201       // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1202       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1203                        | (elfcpp::STO_MIPS_PLT >> 2));
1204   }
1205
1206   // Downcast a base pointer to a Mips_symbol pointer.
1207   static Mips_symbol<size>*
1208   as_mips_sym(Symbol* sym)
1209   { return static_cast<Mips_symbol<size>*>(sym); }
1210
1211   // Downcast a base pointer to a Mips_symbol pointer.
1212   static const Mips_symbol<size>*
1213   as_mips_sym(const Symbol* sym)
1214   { return static_cast<const Mips_symbol<size>*>(sym); }
1215
1216   // Return whether the symbol has lazy-binding stub.
1217   bool
1218   has_lazy_stub() const
1219   { return this->has_lazy_stub_; }
1220
1221   // Set whether the symbol has lazy-binding stub.
1222   void
1223   set_has_lazy_stub(bool has_lazy_stub)
1224   { this->has_lazy_stub_ = has_lazy_stub; }
1225
1226   // Return whether the symbol needs a standard PLT entry.
1227   bool
1228   needs_mips_plt() const
1229   { return this->needs_mips_plt_; }
1230
1231   // Set whether the symbol needs a standard PLT entry.
1232   void
1233   set_needs_mips_plt(bool needs_mips_plt)
1234   { this->needs_mips_plt_ = needs_mips_plt; }
1235
1236   // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1237   // entry.
1238   bool
1239   needs_comp_plt() const
1240   { return this->needs_comp_plt_; }
1241
1242   // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1243   void
1244   set_needs_comp_plt(bool needs_comp_plt)
1245   { this->needs_comp_plt_ = needs_comp_plt; }
1246
1247   // Return standard PLT entry offset, or -1 if none.
1248   unsigned int
1249   mips_plt_offset() const
1250   { return this->mips_plt_offset_; }
1251
1252   // Set standard PLT entry offset.
1253   void
1254   set_mips_plt_offset(unsigned int mips_plt_offset)
1255   { this->mips_plt_offset_ = mips_plt_offset; }
1256
1257   // Return whether the symbol has standard PLT entry.
1258   bool
1259   has_mips_plt_offset() const
1260   { return this->mips_plt_offset_ != -1U; }
1261
1262   // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1263   unsigned int
1264   comp_plt_offset() const
1265   { return this->comp_plt_offset_; }
1266
1267   // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1268   void
1269   set_comp_plt_offset(unsigned int comp_plt_offset)
1270   { this->comp_plt_offset_ = comp_plt_offset; }
1271
1272   // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1273   bool
1274   has_comp_plt_offset() const
1275   { return this->comp_plt_offset_ != -1U; }
1276
1277   // Return MIPS16 fn stub for a symbol.
1278   template<bool big_endian>
1279   Mips16_stub_section<size, big_endian>*
1280   get_mips16_fn_stub() const
1281   {
1282     return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1283   }
1284
1285   // Set MIPS16 fn stub for a symbol.
1286   void
1287   set_mips16_fn_stub(Mips16_stub_section_base* stub)
1288   { this->mips16_fn_stub_ = stub; }
1289
1290   // Return whether symbol has MIPS16 fn stub.
1291   bool
1292   has_mips16_fn_stub() const
1293   { return this->mips16_fn_stub_ != NULL; }
1294
1295   // Return MIPS16 call stub for a symbol.
1296   template<bool big_endian>
1297   Mips16_stub_section<size, big_endian>*
1298   get_mips16_call_stub() const
1299   {
1300     return static_cast<Mips16_stub_section<size, big_endian>*>(
1301       mips16_call_stub_);
1302   }
1303
1304   // Set MIPS16 call stub for a symbol.
1305   void
1306   set_mips16_call_stub(Mips16_stub_section_base* stub)
1307   { this->mips16_call_stub_ = stub; }
1308
1309   // Return whether symbol has MIPS16 call stub.
1310   bool
1311   has_mips16_call_stub() const
1312   { return this->mips16_call_stub_ != NULL; }
1313
1314   // Return MIPS16 call_fp stub for a symbol.
1315   template<bool big_endian>
1316   Mips16_stub_section<size, big_endian>*
1317   get_mips16_call_fp_stub() const
1318   {
1319     return static_cast<Mips16_stub_section<size, big_endian>*>(
1320       mips16_call_fp_stub_);
1321   }
1322
1323   // Set MIPS16 call_fp stub for a symbol.
1324   void
1325   set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1326   { this->mips16_call_fp_stub_ = stub; }
1327
1328   // Return whether symbol has MIPS16 call_fp stub.
1329   bool
1330   has_mips16_call_fp_stub() const
1331   { return this->mips16_call_fp_stub_ != NULL; }
1332
1333   bool
1334   get_applied_secondary_got_fixup() const
1335   { return applied_secondary_got_fixup_; }
1336
1337   void
1338   set_applied_secondary_got_fixup()
1339   { this->applied_secondary_got_fixup_ = true; }
1340
1341   // Return the hash of this symbol.
1342   size_t
1343   hash() const
1344   {
1345     return gold::string_hash<char>(this->name());
1346   }
1347
1348  private:
1349   // Whether the symbol needs MIPS16 fn_stub.  This is true if this symbol
1350   // appears in any relocs other than a 16 bit call.
1351   bool need_fn_stub_;
1352
1353   // True if this symbol is referenced by branch relocations from
1354   // any non-PIC input file.  This is used to determine whether an
1355   // la25 stub is required.
1356   bool has_nonpic_branches_;
1357
1358   // The offset of the la25 stub for this symbol from the start of the
1359   // la25 stub section.
1360   unsigned int la25_stub_offset_;
1361
1362   // True if there is a relocation against this symbol that must be
1363   // resolved by the static linker (that is, the relocation cannot
1364   // possibly be made dynamic).
1365   bool has_static_relocs_;
1366
1367   // Whether we must not create a lazy-binding stub for this symbol.
1368   // This is true if the symbol has relocations related to taking the
1369   // function's address.
1370   bool no_lazy_stub_;
1371
1372   // The offset of the lazy-binding stub for this symbol from the start of
1373   // .MIPS.stubs section.
1374   unsigned int lazy_stub_offset_;
1375
1376   // True if there are any relocations for this symbol where pointer equality
1377   // matters.
1378   bool pointer_equality_needed_;
1379
1380   // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1381   // in the global part of the GOT.
1382   Global_got_area global_got_area_;
1383
1384   // The global GOT offset for this symbol.  For multi-GOT links, this is offset
1385   // from the start of .got section to the first GOT entry for the symbol.
1386   // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1387   unsigned int global_gotoffset_;
1388
1389   // Whether all GOT relocations for this symbol are for calls.
1390   bool got_only_for_calls_;
1391   // Whether the symbol has lazy-binding stub.
1392   bool has_lazy_stub_;
1393   // Whether the symbol needs a standard PLT entry.
1394   bool needs_mips_plt_;
1395   // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1396   bool needs_comp_plt_;
1397   // Standard PLT entry offset, or -1 if none.
1398   unsigned int mips_plt_offset_;
1399   // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1400   unsigned int comp_plt_offset_;
1401   // MIPS16 fn stub for a symbol.
1402   Mips16_stub_section_base* mips16_fn_stub_;
1403   // MIPS16 call stub for a symbol.
1404   Mips16_stub_section_base* mips16_call_stub_;
1405   // MIPS16 call_fp stub for a symbol.
1406   Mips16_stub_section_base* mips16_call_fp_stub_;
1407
1408   bool applied_secondary_got_fixup_;
1409 };
1410
1411 // Mips16_stub_section class.
1412
1413 // The mips16 compiler uses a couple of special sections to handle
1414 // floating point arguments.
1415
1416 // Section names that look like .mips16.fn.FNNAME contain stubs that
1417 // copy floating point arguments from the fp regs to the gp regs and
1418 // then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1419 // call should be redirected to the stub instead.  If no 32 bit
1420 // function calls FNNAME, the stub should be discarded.  We need to
1421 // consider any reference to the function, not just a call, because
1422 // if the address of the function is taken we will need the stub,
1423 // since the address might be passed to a 32 bit function.
1424
1425 // Section names that look like .mips16.call.FNNAME contain stubs
1426 // that copy floating point arguments from the gp regs to the fp
1427 // regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1428 // then any 16 bit function that calls FNNAME should be redirected
1429 // to the stub instead.  If FNNAME is not a 32 bit function, the
1430 // stub should be discarded.
1431
1432 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1433 // which call FNNAME and then copy the return value from the fp regs
1434 // to the gp regs.  These stubs store the return address in $18 while
1435 // calling FNNAME; any function which might call one of these stubs
1436 // must arrange to save $18 around the call.  (This case is not
1437 // needed for 32 bit functions that call 16 bit functions, because
1438 // 16 bit functions always return floating point values in both
1439 // $f0/$f1 and $2/$3.)
1440
1441 // Note that in all cases FNNAME might be defined statically.
1442 // Therefore, FNNAME is not used literally.  Instead, the relocation
1443 // information will indicate which symbol the section is for.
1444
1445 // We record any stubs that we find in the symbol table.
1446
1447 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1448
1449 class Mips16_stub_section_base { };
1450
1451 template<int size, bool big_endian>
1452 class Mips16_stub_section : public Mips16_stub_section_base
1453 {
1454   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1455
1456  public:
1457   Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1458     : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1459       found_r_mips_none_(false)
1460   {
1461     gold_assert(object->is_mips16_fn_stub_section(shndx)
1462                 || object->is_mips16_call_stub_section(shndx)
1463                 || object->is_mips16_call_fp_stub_section(shndx));
1464   }
1465
1466   // Return the object of this stub section.
1467   Mips_relobj<size, big_endian>*
1468   object() const
1469   { return this->object_; }
1470
1471   // Return the size of a section.
1472   uint64_t
1473   section_size() const
1474   { return this->object_->section_size(this->shndx_); }
1475
1476   // Return section index of this stub section.
1477   unsigned int
1478   shndx() const
1479   { return this->shndx_; }
1480
1481   // Return symbol index, if stub is for a local function.
1482   unsigned int
1483   r_sym() const
1484   { return this->r_sym_; }
1485
1486   // Return symbol, if stub is for a global function.
1487   Mips_symbol<size>*
1488   gsym() const
1489   { return this->gsym_; }
1490
1491   // Return whether stub is for a local function.
1492   bool
1493   is_for_local_function() const
1494   { return this->gsym_ == NULL; }
1495
1496   // This method is called when a new relocation R_TYPE for local symbol R_SYM
1497   // is found in the stub section.  Try to find stub target.
1498   void
1499   new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1500   {
1501     // To find target symbol for this stub, trust the first R_MIPS_NONE
1502     // relocation, if any.  Otherwise trust the first relocation, whatever
1503     // its kind.
1504     if (this->found_r_mips_none_)
1505       return;
1506     if (r_type == elfcpp::R_MIPS_NONE)
1507       {
1508         this->r_sym_ = r_sym;
1509         this->gsym_ = NULL;
1510         this->found_r_mips_none_ = true;
1511       }
1512     else if (!is_target_found())
1513       this->r_sym_ = r_sym;
1514   }
1515
1516   // This method is called when a new relocation R_TYPE for global symbol GSYM
1517   // is found in the stub section.  Try to find stub target.
1518   void
1519   new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1520   {
1521     // To find target symbol for this stub, trust the first R_MIPS_NONE
1522     // relocation, if any.  Otherwise trust the first relocation, whatever
1523     // its kind.
1524     if (this->found_r_mips_none_)
1525       return;
1526     if (r_type == elfcpp::R_MIPS_NONE)
1527       {
1528         this->gsym_ = gsym;
1529         this->r_sym_ = 0;
1530         this->found_r_mips_none_ = true;
1531       }
1532     else if (!is_target_found())
1533       this->gsym_ = gsym;
1534   }
1535
1536   // Return whether we found the stub target.
1537   bool
1538   is_target_found() const
1539   { return this->r_sym_ != 0 || this->gsym_ != NULL;  }
1540
1541   // Return whether this is a fn stub.
1542   bool
1543   is_fn_stub() const
1544   { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1545
1546   // Return whether this is a call stub.
1547   bool
1548   is_call_stub() const
1549   { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1550
1551   // Return whether this is a call_fp stub.
1552   bool
1553   is_call_fp_stub() const
1554   { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1555
1556   // Return the output address.
1557   Mips_address
1558   output_address() const
1559   {
1560     return (this->object_->output_section(this->shndx_)->address()
1561             + this->object_->output_section_offset(this->shndx_));
1562   }
1563
1564  private:
1565   // The object of this stub section.
1566   Mips_relobj<size, big_endian>* object_;
1567   // The section index of this stub section.
1568   unsigned int shndx_;
1569   // The symbol index, if stub is for a local function.
1570   unsigned int r_sym_;
1571   // The symbol, if stub is for a global function.
1572   Mips_symbol<size>* gsym_;
1573   // True if we found R_MIPS_NONE relocation in this stub.
1574   bool found_r_mips_none_;
1575 };
1576
1577 // Mips_relobj class.
1578
1579 template<int size, bool big_endian>
1580 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1581 {
1582   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1583   typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1584     Mips16_stubs_int_map;
1585   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1586
1587  public:
1588   Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1589               const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1590     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1591       processor_specific_flags_(0), local_symbol_is_mips16_(),
1592       local_symbol_is_micromips_(), mips16_stub_sections_(),
1593       local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1594       local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1595       merge_processor_specific_data_(true), got_info_(NULL),
1596       section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1597       section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
1598       attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
1599       cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1600   {
1601     this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1602     this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1603   }
1604
1605   ~Mips_relobj()
1606   { delete this->attributes_section_data_; }
1607
1608   // Downcast a base pointer to a Mips_relobj pointer.  This is
1609   // not type-safe but we only use Mips_relobj not the base class.
1610   static Mips_relobj<size, big_endian>*
1611   as_mips_relobj(Relobj* relobj)
1612   { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1613
1614   // Downcast a base pointer to a Mips_relobj pointer.  This is
1615   // not type-safe but we only use Mips_relobj not the base class.
1616   static const Mips_relobj<size, big_endian>*
1617   as_mips_relobj(const Relobj* relobj)
1618   { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1619
1620   // Processor-specific flags in ELF file header.  This is valid only after
1621   // reading symbols.
1622   elfcpp::Elf_Word
1623   processor_specific_flags() const
1624   { return this->processor_specific_flags_; }
1625
1626   // Whether a local symbol is MIPS16 symbol.  R_SYM is the symbol table
1627   // index.  This is only valid after do_count_local_symbol is called.
1628   bool
1629   local_symbol_is_mips16(unsigned int r_sym) const
1630   {
1631     gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1632     return this->local_symbol_is_mips16_[r_sym];
1633   }
1634
1635   // Whether a local symbol is microMIPS symbol.  R_SYM is the symbol table
1636   // index.  This is only valid after do_count_local_symbol is called.
1637   bool
1638   local_symbol_is_micromips(unsigned int r_sym) const
1639   {
1640     gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1641     return this->local_symbol_is_micromips_[r_sym];
1642   }
1643
1644   // Get or create MIPS16 stub section.
1645   Mips16_stub_section<size, big_endian>*
1646   get_mips16_stub_section(unsigned int shndx)
1647   {
1648     typename Mips16_stubs_int_map::const_iterator it =
1649       this->mips16_stub_sections_.find(shndx);
1650     if (it != this->mips16_stub_sections_.end())
1651       return (*it).second;
1652
1653     Mips16_stub_section<size, big_endian>* stub_section =
1654       new Mips16_stub_section<size, big_endian>(this, shndx);
1655     this->mips16_stub_sections_.insert(
1656       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1657         stub_section->shndx(), stub_section));
1658     return stub_section;
1659   }
1660
1661   // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1662   // object doesn't have fn stub for R_SYM.
1663   Mips16_stub_section<size, big_endian>*
1664   get_local_mips16_fn_stub(unsigned int r_sym) const
1665   {
1666     typename Mips16_stubs_int_map::const_iterator it =
1667       this->local_mips16_fn_stubs_.find(r_sym);
1668     if (it != this->local_mips16_fn_stubs_.end())
1669       return (*it).second;
1670     return NULL;
1671   }
1672
1673   // Record that this object has MIPS16 fn stub for local symbol.  This method
1674   // is only called if we decided not to discard the stub.
1675   void
1676   add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1677   {
1678     gold_assert(stub->is_for_local_function());
1679     unsigned int r_sym = stub->r_sym();
1680     this->local_mips16_fn_stubs_.insert(
1681       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1682         r_sym, stub));
1683   }
1684
1685   // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1686   // object doesn't have call stub for R_SYM.
1687   Mips16_stub_section<size, big_endian>*
1688   get_local_mips16_call_stub(unsigned int r_sym) const
1689   {
1690     typename Mips16_stubs_int_map::const_iterator it =
1691       this->local_mips16_call_stubs_.find(r_sym);
1692     if (it != this->local_mips16_call_stubs_.end())
1693       return (*it).second;
1694     return NULL;
1695   }
1696
1697   // Record that this object has MIPS16 call stub for local symbol.  This method
1698   // is only called if we decided not to discard the stub.
1699   void
1700   add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1701   {
1702     gold_assert(stub->is_for_local_function());
1703     unsigned int r_sym = stub->r_sym();
1704     this->local_mips16_call_stubs_.insert(
1705       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1706         r_sym, stub));
1707   }
1708
1709   // Record that we found "non 16-bit" call relocation against local symbol
1710   // SYMNDX.  This reloc would need to refer to a MIPS16 fn stub, if there
1711   // is one.
1712   void
1713   add_local_non_16bit_call(unsigned int symndx)
1714   { this->local_non_16bit_calls_.insert(symndx); }
1715
1716   // Return true if there is any "non 16-bit" call relocation against local
1717   // symbol SYMNDX in this object.
1718   bool
1719   has_local_non_16bit_call_relocs(unsigned int symndx)
1720   {
1721     return (this->local_non_16bit_calls_.find(symndx)
1722             != this->local_non_16bit_calls_.end());
1723   }
1724
1725   // Record that we found 16-bit call relocation R_MIPS16_26 against local
1726   // symbol SYMNDX.  Local MIPS16 call or call_fp stubs will only be needed
1727   // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1728   void
1729   add_local_16bit_call(unsigned int symndx)
1730   { this->local_16bit_calls_.insert(symndx); }
1731
1732   // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1733   // symbol SYMNDX in this object.
1734   bool
1735   has_local_16bit_call_relocs(unsigned int symndx)
1736   {
1737     return (this->local_16bit_calls_.find(symndx)
1738             != this->local_16bit_calls_.end());
1739   }
1740
1741   // Get gp value that was used to create this object.
1742   Mips_address
1743   gp_value() const
1744   { return this->gp_; }
1745
1746   // Return whether the object is a PIC object.
1747   bool
1748   is_pic() const
1749   { return this->is_pic_; }
1750
1751   // Return whether the object uses N32 ABI.
1752   bool
1753   is_n32() const
1754   { return this->is_n32_; }
1755
1756   // Return whether the object uses N64 ABI.
1757   bool
1758   is_n64() const
1759   { return size == 64; }
1760
1761   // Return whether the object uses NewABI conventions.
1762   bool
1763   is_newabi() const
1764   { return this->is_n32() || this->is_n64(); }
1765
1766   // Return Mips_got_info for this object.
1767   Mips_got_info<size, big_endian>*
1768   get_got_info() const
1769   { return this->got_info_; }
1770
1771   // Return Mips_got_info for this object.  Create new info if it doesn't exist.
1772   Mips_got_info<size, big_endian>*
1773   get_or_create_got_info()
1774   {
1775     if (!this->got_info_)
1776       this->got_info_ = new Mips_got_info<size, big_endian>();
1777     return this->got_info_;
1778   }
1779
1780   // Set Mips_got_info for this object.
1781   void
1782   set_got_info(Mips_got_info<size, big_endian>* got_info)
1783   { this->got_info_ = got_info; }
1784
1785   // Whether a section SHDNX is a MIPS16 stub section.  This is only valid
1786   // after do_read_symbols is called.
1787   bool
1788   is_mips16_stub_section(unsigned int shndx)
1789   {
1790     return (is_mips16_fn_stub_section(shndx)
1791             || is_mips16_call_stub_section(shndx)
1792             || is_mips16_call_fp_stub_section(shndx));
1793   }
1794
1795   // Return TRUE if relocations in section SHNDX can refer directly to a
1796   // MIPS16 function rather than to a hard-float stub.  This is only valid
1797   // after do_read_symbols is called.
1798   bool
1799   section_allows_mips16_refs(unsigned int shndx)
1800   {
1801     return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1802   }
1803
1804   // Whether a section SHDNX is a MIPS16 fn stub section.  This is only valid
1805   // after do_read_symbols is called.
1806   bool
1807   is_mips16_fn_stub_section(unsigned int shndx)
1808   {
1809     gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1810     return this->section_is_mips16_fn_stub_[shndx];
1811   }
1812
1813   // Whether a section SHDNX is a MIPS16 call stub section.  This is only valid
1814   // after do_read_symbols is called.
1815   bool
1816   is_mips16_call_stub_section(unsigned int shndx)
1817   {
1818     gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1819     return this->section_is_mips16_call_stub_[shndx];
1820   }
1821
1822   // Whether a section SHDNX is a MIPS16 call_fp stub section.  This is only
1823   // valid after do_read_symbols is called.
1824   bool
1825   is_mips16_call_fp_stub_section(unsigned int shndx)
1826   {
1827     gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1828     return this->section_is_mips16_call_fp_stub_[shndx];
1829   }
1830
1831   // Discard MIPS16 stub secions that are not needed.
1832   void
1833   discard_mips16_stub_sections(Symbol_table* symtab);
1834
1835   // Return whether there is a .reginfo section.
1836   bool
1837   has_reginfo_section() const
1838   { return this->has_reginfo_section_; }
1839
1840   // Return whether we want to merge processor-specific data.
1841   bool
1842   merge_processor_specific_data() const
1843   { return this->merge_processor_specific_data_; }
1844
1845   // Return gprmask from the .reginfo section of this object.
1846   Valtype
1847   gprmask() const
1848   { return this->gprmask_; }
1849
1850   // Return cprmask1 from the .reginfo section of this object.
1851   Valtype
1852   cprmask1() const
1853   { return this->cprmask1_; }
1854
1855   // Return cprmask2 from the .reginfo section of this object.
1856   Valtype
1857   cprmask2() const
1858   { return this->cprmask2_; }
1859
1860   // Return cprmask3 from the .reginfo section of this object.
1861   Valtype
1862   cprmask3() const
1863   { return this->cprmask3_; }
1864
1865   // Return cprmask4 from the .reginfo section of this object.
1866   Valtype
1867   cprmask4() const
1868   { return this->cprmask4_; }
1869
1870   // This is the contents of the .MIPS.abiflags section if there is one.
1871   Mips_abiflags<big_endian>*
1872   abiflags()
1873   { return this->abiflags_; }
1874
1875   // This is the contents of the .gnu.attribute section if there is one.
1876   const Attributes_section_data*
1877   attributes_section_data() const
1878   { return this->attributes_section_data_; }
1879
1880  protected:
1881   // Count the local symbols.
1882   void
1883   do_count_local_symbols(Stringpool_template<char>*,
1884                          Stringpool_template<char>*);
1885
1886   // Read the symbol information.
1887   void
1888   do_read_symbols(Read_symbols_data* sd);
1889
1890  private:
1891   // The name of the options section.
1892   const char* mips_elf_options_section_name()
1893   { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1894
1895   // processor-specific flags in ELF file header.
1896   elfcpp::Elf_Word processor_specific_flags_;
1897
1898   // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1899   // This is only valid after do_count_local_symbol is called.
1900   std::vector<bool> local_symbol_is_mips16_;
1901
1902   // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1903   // This is only valid after do_count_local_symbol is called.
1904   std::vector<bool> local_symbol_is_micromips_;
1905
1906   // Map from section index to the MIPS16 stub for that section.  This contains
1907   // all stubs found in this object.
1908   Mips16_stubs_int_map mips16_stub_sections_;
1909
1910   // Local symbols that have "non 16-bit" call relocation.  This relocation
1911   // would need to refer to a MIPS16 fn stub, if there is one.
1912   std::set<unsigned int> local_non_16bit_calls_;
1913
1914   // Local symbols that have 16-bit call relocation R_MIPS16_26.  Local MIPS16
1915   // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1916   // relocation that refers to the stub symbol.
1917   std::set<unsigned int> local_16bit_calls_;
1918
1919   // Map from local symbol index to the MIPS16 fn stub for that symbol.
1920   // This contains only the stubs that we decided not to discard.
1921   Mips16_stubs_int_map local_mips16_fn_stubs_;
1922
1923   // Map from local symbol index to the MIPS16 call stub for that symbol.
1924   // This contains only the stubs that we decided not to discard.
1925   Mips16_stubs_int_map local_mips16_call_stubs_;
1926
1927   // gp value that was used to create this object.
1928   Mips_address gp_;
1929   // Whether the object is a PIC object.
1930   bool is_pic_ : 1;
1931   // Whether the object uses N32 ABI.
1932   bool is_n32_ : 1;
1933   // Whether the object contains a .reginfo section.
1934   bool has_reginfo_section_ : 1;
1935   // Whether we merge processor-specific data of this object to output.
1936   bool merge_processor_specific_data_ : 1;
1937   // The Mips_got_info for this object.
1938   Mips_got_info<size, big_endian>* got_info_;
1939
1940   // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1941   // This is only valid after do_read_symbols is called.
1942   std::vector<bool> section_is_mips16_fn_stub_;
1943
1944   // Bit vector to tell if a section is a MIPS16 call stub section or not.
1945   // This is only valid after do_read_symbols is called.
1946   std::vector<bool> section_is_mips16_call_stub_;
1947
1948   // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1949   // This is only valid after do_read_symbols is called.
1950   std::vector<bool> section_is_mips16_call_fp_stub_;
1951
1952   // .pdr section index.
1953   unsigned int pdr_shndx_;
1954
1955   // Object attributes if there is a .gnu.attributes section or NULL.
1956   Attributes_section_data* attributes_section_data_;
1957
1958   // Object abiflags if there is a .MIPS.abiflags section or NULL.
1959   Mips_abiflags<big_endian>* abiflags_;
1960
1961   // gprmask from the .reginfo section of this object.
1962   Valtype gprmask_;
1963   // cprmask1 from the .reginfo section of this object.
1964   Valtype cprmask1_;
1965   // cprmask2 from the .reginfo section of this object.
1966   Valtype cprmask2_;
1967   // cprmask3 from the .reginfo section of this object.
1968   Valtype cprmask3_;
1969   // cprmask4 from the .reginfo section of this object.
1970   Valtype cprmask4_;
1971 };
1972
1973 // Mips_output_data_got class.
1974
1975 template<int size, bool big_endian>
1976 class Mips_output_data_got : public Output_data_got<size, big_endian>
1977 {
1978   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1979   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1980     Reloc_section;
1981   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1982
1983  public:
1984   Mips_output_data_got(Target_mips<size, big_endian>* target,
1985       Symbol_table* symtab, Layout* layout)
1986     : Output_data_got<size, big_endian>(), target_(target),
1987       symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1988       first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1989       secondary_got_relocs_()
1990   {
1991     this->master_got_info_ = new Mips_got_info<size, big_endian>();
1992     this->set_addralign(16);
1993   }
1994
1995   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1996   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1997   void
1998   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1999                           unsigned int symndx, Mips_address addend,
2000                           unsigned int r_type, unsigned int shndx,
2001                           bool is_section_symbol)
2002   {
2003     this->master_got_info_->record_local_got_symbol(object, symndx, addend,
2004                                                     r_type, shndx,
2005                                                     is_section_symbol);
2006   }
2007
2008   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
2009   // in OBJECT.  FOR_CALL is true if the caller is only interested in
2010   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
2011   // relocation.
2012   void
2013   record_global_got_symbol(Mips_symbol<size>* mips_sym,
2014                            Mips_relobj<size, big_endian>* object,
2015                            unsigned int r_type, bool dyn_reloc, bool for_call)
2016   {
2017     this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2018                                                      dyn_reloc, for_call);
2019   }
2020
2021   // Record that OBJECT has a page relocation against symbol SYMNDX and
2022   // that ADDEND is the addend for that relocation.
2023   void
2024   record_got_page_entry(Mips_relobj<size, big_endian>* object,
2025                         unsigned int symndx, int addend)
2026   { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2027
2028   // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
2029   // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2030   // applied in a static link.
2031   void
2032   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2033                    Mips_symbol<size>* gsym)
2034   { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2035
2036   // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
2037   // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
2038   // relocation that needs to be applied in a static link.
2039   void
2040   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2041                    Sized_relobj_file<size, big_endian>* relobj,
2042                    unsigned int index)
2043   {
2044     this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2045                                                 index));
2046   }
2047
2048   // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2049   // secondary GOT at OFFSET.
2050   void
2051   add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2052                           Mips_symbol<size>* gsym)
2053   {
2054     this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2055                                                        r_type, gsym));
2056   }
2057
2058   // Update GOT entry at OFFSET with VALUE.
2059   void
2060   update_got_entry(unsigned int offset, Mips_address value)
2061   {
2062     elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2063   }
2064
2065   // Return the number of entries in local part of the GOT.  This includes
2066   // local entries, page entries and 2 reserved entries.
2067   unsigned int
2068   get_local_gotno() const
2069   {
2070     if (!this->multi_got())
2071       {
2072         return (2 + this->master_got_info_->local_gotno()
2073                 + this->master_got_info_->page_gotno());
2074       }
2075     else
2076       return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2077   }
2078
2079   // Return dynamic symbol table index of the first symbol with global GOT
2080   // entry.
2081   unsigned int
2082   first_global_got_dynsym_index() const
2083   { return this->first_global_got_dynsym_index_; }
2084
2085   // Set dynamic symbol table index of the first symbol with global GOT entry.
2086   void
2087   set_first_global_got_dynsym_index(unsigned int index)
2088   { this->first_global_got_dynsym_index_ = index; }
2089
2090   // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
2091   // larger than 64K, create multi-GOT.
2092   void
2093   lay_out_got(Layout* layout, Symbol_table* symtab,
2094               const Input_objects* input_objects);
2095
2096   // Create multi-GOT.  For every GOT, add local, global and TLS entries.
2097   void
2098   lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2099
2100   // Attempt to merge GOTs of different input objects.
2101   void
2102   merge_gots(const Input_objects* input_objects);
2103
2104   // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
2105   // this would lead to overflow, true if they were merged successfully.
2106   bool
2107   merge_got_with(Mips_got_info<size, big_endian>* from,
2108                  Mips_relobj<size, big_endian>* object,
2109                  Mips_got_info<size, big_endian>* to);
2110
2111   // Return the offset of GOT page entry for VALUE.  For multi-GOT links,
2112   // use OBJECT's GOT.
2113   unsigned int
2114   get_got_page_offset(Mips_address value,
2115                       const Mips_relobj<size, big_endian>* object)
2116   {
2117     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2118                                           ? this->master_got_info_
2119                                           : object->get_got_info());
2120     gold_assert(g != NULL);
2121     return g->get_got_page_offset(value, this);
2122   }
2123
2124   // Return the GOT offset of type GOT_TYPE of the global symbol
2125   // GSYM.  For multi-GOT links, use OBJECT's GOT.
2126   unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2127                           Mips_relobj<size, big_endian>* object) const
2128   {
2129     if (!this->multi_got())
2130       return gsym->got_offset(got_type);
2131     else
2132       {
2133         Mips_got_info<size, big_endian>* g = object->get_got_info();
2134         gold_assert(g != NULL);
2135         return gsym->got_offset(g->multigot_got_type(got_type));
2136       }
2137   }
2138
2139   // Return the GOT offset of type GOT_TYPE of the local symbol
2140   // SYMNDX.
2141   unsigned int
2142   got_offset(unsigned int symndx, unsigned int got_type,
2143              Sized_relobj_file<size, big_endian>* object,
2144              uint64_t addend) const
2145   { return object->local_got_offset(symndx, got_type, addend); }
2146
2147   // Return the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2148   unsigned int
2149   tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2150   {
2151     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2152                                           ? this->master_got_info_
2153                                           : object->get_got_info());
2154     gold_assert(g != NULL);
2155     return g->tls_ldm_offset();
2156   }
2157
2158   // Set the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2159   void
2160   set_tls_ldm_offset(unsigned int tls_ldm_offset,
2161                      Mips_relobj<size, big_endian>* object)
2162   {
2163     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2164                                           ? this->master_got_info_
2165                                           : object->get_got_info());
2166     gold_assert(g != NULL);
2167     g->set_tls_ldm_offset(tls_ldm_offset);
2168   }
2169
2170   // Return true for multi-GOT links.
2171   bool
2172   multi_got() const
2173   { return this->primary_got_ != NULL; }
2174
2175   // Return the offset of OBJECT's GOT from the start of .got section.
2176   unsigned int
2177   get_got_offset(const Mips_relobj<size, big_endian>* object)
2178   {
2179     if (!this->multi_got())
2180       return 0;
2181     else
2182       {
2183         Mips_got_info<size, big_endian>* g = object->get_got_info();
2184         return g != NULL ? g->offset() : 0;
2185       }
2186   }
2187
2188   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2189   void
2190   add_reloc_only_entries()
2191   { this->master_got_info_->add_reloc_only_entries(this); }
2192
2193   // Return offset of the primary GOT's entry for global symbol.
2194   unsigned int
2195   get_primary_got_offset(const Mips_symbol<size>* sym) const
2196   {
2197     gold_assert(sym->global_got_area() != GGA_NONE);
2198     return (this->get_local_gotno() + sym->dynsym_index()
2199             - this->first_global_got_dynsym_index()) * size/8;
2200   }
2201
2202   // For the entry at offset GOT_OFFSET, return its offset from the gp.
2203   // Input argument GOT_OFFSET is always global offset from the start of
2204   // .got section, for both single and multi-GOT links.
2205   // For single GOT links, this returns GOT_OFFSET - 0x7FF0.  For multi-GOT
2206   // links, the return value is object_got_offset - 0x7FF0, where
2207   // object_got_offset is offset in the OBJECT's GOT.
2208   int
2209   gp_offset(unsigned int got_offset,
2210             const Mips_relobj<size, big_endian>* object) const
2211   {
2212     return (this->address() + got_offset
2213             - this->target_->adjusted_gp_value(object));
2214   }
2215
2216  protected:
2217   // Write out the GOT table.
2218   void
2219   do_write(Output_file*);
2220
2221  private:
2222
2223   // This class represent dynamic relocations that need to be applied by
2224   // gold because we are using TLS relocations in a static link.
2225   class Static_reloc
2226   {
2227    public:
2228     Static_reloc(unsigned int got_offset, unsigned int r_type,
2229                  Mips_symbol<size>* gsym)
2230       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2231     { this->u_.global.symbol = gsym; }
2232
2233     Static_reloc(unsigned int got_offset, unsigned int r_type,
2234           Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2235       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2236     {
2237       this->u_.local.relobj = relobj;
2238       this->u_.local.index = index;
2239     }
2240
2241     // Return the GOT offset.
2242     unsigned int
2243     got_offset() const
2244     { return this->got_offset_; }
2245
2246     // Relocation type.
2247     unsigned int
2248     r_type() const
2249     { return this->r_type_; }
2250
2251     // Whether the symbol is global or not.
2252     bool
2253     symbol_is_global() const
2254     { return this->symbol_is_global_; }
2255
2256     // For a relocation against a global symbol, the global symbol.
2257     Mips_symbol<size>*
2258     symbol() const
2259     {
2260       gold_assert(this->symbol_is_global_);
2261       return this->u_.global.symbol;
2262     }
2263
2264     // For a relocation against a local symbol, the defining object.
2265     Sized_relobj_file<size, big_endian>*
2266     relobj() const
2267     {
2268       gold_assert(!this->symbol_is_global_);
2269       return this->u_.local.relobj;
2270     }
2271
2272     // For a relocation against a local symbol, the local symbol index.
2273     unsigned int
2274     index() const
2275     {
2276       gold_assert(!this->symbol_is_global_);
2277       return this->u_.local.index;
2278     }
2279
2280    private:
2281     // GOT offset of the entry to which this relocation is applied.
2282     unsigned int got_offset_;
2283     // Type of relocation.
2284     unsigned int r_type_;
2285     // Whether this relocation is against a global symbol.
2286     bool symbol_is_global_;
2287     // A global or local symbol.
2288     union
2289     {
2290       struct
2291       {
2292         // For a global symbol, the symbol itself.
2293         Mips_symbol<size>* symbol;
2294       } global;
2295       struct
2296       {
2297         // For a local symbol, the object defining object.
2298         Sized_relobj_file<size, big_endian>* relobj;
2299         // For a local symbol, the symbol index.
2300         unsigned int index;
2301       } local;
2302     } u_;
2303   };
2304
2305   // The target.
2306   Target_mips<size, big_endian>* target_;
2307   // The symbol table.
2308   Symbol_table* symbol_table_;
2309   // The layout.
2310   Layout* layout_;
2311   // Static relocs to be applied to the GOT.
2312   std::vector<Static_reloc> static_relocs_;
2313   // .got section view.
2314   unsigned char* got_view_;
2315   // The dynamic symbol table index of the first symbol with global GOT entry.
2316   unsigned int first_global_got_dynsym_index_;
2317   // The master GOT information.
2318   Mips_got_info<size, big_endian>* master_got_info_;
2319   // The  primary GOT information.
2320   Mips_got_info<size, big_endian>* primary_got_;
2321   // Secondary GOT fixups.
2322   std::vector<Static_reloc> secondary_got_relocs_;
2323 };
2324
2325 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2326 // two ways of creating these interfaces.  The first is to add:
2327 //
2328 //      lui     $25,%hi(func)
2329 //      j       func
2330 //      addiu   $25,$25,%lo(func)
2331 //
2332 // to a separate trampoline section.  The second is to add:
2333 //
2334 //      lui     $25,%hi(func)
2335 //      addiu   $25,$25,%lo(func)
2336 //
2337 // immediately before a PIC function "func", but only if a function is at the
2338 // beginning of the section, and the section is not too heavily aligned (i.e we
2339 // would need to add no more than 2 nops before the stub.)
2340 //
2341 // We only create stubs of the first type.
2342
2343 template<int size, bool big_endian>
2344 class Mips_output_data_la25_stub : public Output_section_data
2345 {
2346   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2347
2348  public:
2349   Mips_output_data_la25_stub()
2350   : Output_section_data(size == 32 ? 4 : 8), symbols_()
2351   { }
2352
2353   // Create LA25 stub for a symbol.
2354   void
2355   create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2356                    Mips_symbol<size>* gsym);
2357
2358   // Return output address of a stub.
2359   Mips_address
2360   stub_address(const Mips_symbol<size>* sym) const
2361   {
2362     gold_assert(sym->has_la25_stub());
2363     return this->address() + sym->la25_stub_offset();
2364   }
2365
2366  protected:
2367   void
2368   do_adjust_output_section(Output_section* os)
2369   { os->set_entsize(0); }
2370
2371  private:
2372   // Template for standard LA25 stub.
2373   static const uint32_t la25_stub_entry[];
2374   // Template for microMIPS LA25 stub.
2375   static const uint32_t la25_stub_micromips_entry[];
2376
2377   // Set the final size.
2378   void
2379   set_final_data_size()
2380   { this->set_data_size(this->symbols_.size() * 16); }
2381
2382   // Create a symbol for SYM stub's value and size, to help make the
2383   // disassembly easier to read.
2384   void
2385   create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2386                      Target_mips<size, big_endian>* target, uint64_t symsize);
2387
2388   // Write to a map file.
2389   void
2390   do_print_to_mapfile(Mapfile* mapfile) const
2391   { mapfile->print_output_data(this, _(".LA25.stubs")); }
2392
2393   // Write out the LA25 stub section.
2394   void
2395   do_write(Output_file*);
2396
2397   // Symbols that have LA25 stubs.
2398   std::vector<Mips_symbol<size>*> symbols_;
2399 };
2400
2401 // MIPS-specific relocation writer.
2402
2403 template<int sh_type, bool dynamic, int size, bool big_endian>
2404 struct Mips_output_reloc_writer;
2405
2406 template<int sh_type, bool dynamic, bool big_endian>
2407 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2408 {
2409   typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2410   typedef std::vector<Output_reloc_type> Relocs;
2411
2412   static void
2413   write(typename Relocs::const_iterator p, unsigned char* pov)
2414   { p->write(pov); }
2415 };
2416
2417 template<int sh_type, bool dynamic, bool big_endian>
2418 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2419 {
2420   typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2421   typedef std::vector<Output_reloc_type> Relocs;
2422
2423   static void
2424   write(typename Relocs::const_iterator p, unsigned char* pov)
2425   {
2426     elfcpp::Mips64_rel_write<big_endian> orel(pov);
2427     orel.put_r_offset(p->get_address());
2428     orel.put_r_sym(p->get_symbol_index());
2429     orel.put_r_ssym(RSS_UNDEF);
2430     orel.put_r_type(p->type());
2431     if (p->type() == elfcpp::R_MIPS_REL32)
2432       orel.put_r_type2(elfcpp::R_MIPS_64);
2433     else
2434       orel.put_r_type2(elfcpp::R_MIPS_NONE);
2435     orel.put_r_type3(elfcpp::R_MIPS_NONE);
2436   }
2437 };
2438
2439 template<int sh_type, bool dynamic, int size, bool big_endian>
2440 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2441                                                         size, big_endian>
2442 {
2443  public:
2444   Mips_output_data_reloc(bool sort_relocs)
2445     : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2446   { }
2447
2448  protected:
2449   // Write out the data.
2450   void
2451   do_write(Output_file* of)
2452   {
2453     typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2454         big_endian> Writer;
2455     this->template do_write_generic<Writer>(of);
2456   }
2457 };
2458
2459
2460 // A class to handle the PLT data.
2461
2462 template<int size, bool big_endian>
2463 class Mips_output_data_plt : public Output_section_data
2464 {
2465   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2466   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2467                                  size, big_endian> Reloc_section;
2468
2469  public:
2470   // Create the PLT section.  The ordinary .got section is an argument,
2471   // since we need to refer to the start.
2472   Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2473                        Target_mips<size, big_endian>* target)
2474     : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2475       plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2476       target_(target)
2477   {
2478     this->rel_ = new Reloc_section(false);
2479     layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2480                                     elfcpp::SHF_ALLOC, this->rel_,
2481                                     ORDER_DYNAMIC_PLT_RELOCS, false);
2482   }
2483
2484   // Add an entry to the PLT for a symbol referenced by r_type relocation.
2485   void
2486   add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2487
2488   // Return the .rel.plt section data.
2489   Reloc_section*
2490   rel_plt() const
2491   { return this->rel_; }
2492
2493   // Return the number of PLT entries.
2494   unsigned int
2495   entry_count() const
2496   { return this->symbols_.size(); }
2497
2498   // Return the offset of the first non-reserved PLT entry.
2499   unsigned int
2500   first_plt_entry_offset() const
2501   { return sizeof(plt0_entry_o32); }
2502
2503   // Return the size of a PLT entry.
2504   unsigned int
2505   plt_entry_size() const
2506   { return sizeof(plt_entry); }
2507
2508   // Set final PLT offsets.  For each symbol, determine whether standard or
2509   // compressed (MIPS16 or microMIPS) PLT entry is used.
2510   void
2511   set_plt_offsets();
2512
2513   // Return the offset of the first standard PLT entry.
2514   unsigned int
2515   first_mips_plt_offset() const
2516   { return this->plt_header_size_; }
2517
2518   // Return the offset of the first compressed PLT entry.
2519   unsigned int
2520   first_comp_plt_offset() const
2521   { return this->plt_header_size_ + this->plt_mips_offset_; }
2522
2523   // Return whether there are any standard PLT entries.
2524   bool
2525   has_standard_entries() const
2526   { return this->plt_mips_offset_ > 0; }
2527
2528   // Return the output address of standard PLT entry.
2529   Mips_address
2530   mips_entry_address(const Mips_symbol<size>* sym) const
2531   {
2532     gold_assert (sym->has_mips_plt_offset());
2533     return (this->address() + this->first_mips_plt_offset()
2534             + sym->mips_plt_offset());
2535   }
2536
2537   // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2538   Mips_address
2539   comp_entry_address(const Mips_symbol<size>* sym) const
2540   {
2541     gold_assert (sym->has_comp_plt_offset());
2542     return (this->address() + this->first_comp_plt_offset()
2543             + sym->comp_plt_offset());
2544   }
2545
2546  protected:
2547   void
2548   do_adjust_output_section(Output_section* os)
2549   { os->set_entsize(0); }
2550
2551   // Write to a map file.
2552   void
2553   do_print_to_mapfile(Mapfile* mapfile) const
2554   { mapfile->print_output_data(this, _(".plt")); }
2555
2556  private:
2557   // Template for the first PLT entry.
2558   static const uint32_t plt0_entry_o32[];
2559   static const uint32_t plt0_entry_n32[];
2560   static const uint32_t plt0_entry_n64[];
2561   static const uint32_t plt0_entry_micromips_o32[];
2562   static const uint32_t plt0_entry_micromips32_o32[];
2563
2564   // Template for subsequent PLT entries.
2565   static const uint32_t plt_entry[];
2566   static const uint32_t plt_entry_r6[];
2567   static const uint32_t plt_entry_mips16_o32[];
2568   static const uint32_t plt_entry_micromips_o32[];
2569   static const uint32_t plt_entry_micromips32_o32[];
2570
2571   // Set the final size.
2572   void
2573   set_final_data_size()
2574   {
2575     this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2576                         + this->plt_comp_offset_);
2577   }
2578
2579   // Write out the PLT data.
2580   void
2581   do_write(Output_file*);
2582
2583   // Return whether the plt header contains microMIPS code.  For the sake of
2584   // cache alignment always use a standard header whenever any standard entries
2585   // are present even if microMIPS entries are present as well.  This also lets
2586   // the microMIPS header rely on the value of $v0 only set by microMIPS
2587   // entries, for a small size reduction.
2588   bool
2589   is_plt_header_compressed() const
2590   {
2591     gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2592     return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2593   }
2594
2595   // Return the size of the PLT header.
2596   unsigned int
2597   get_plt_header_size() const
2598   {
2599     if (this->target_->is_output_n64())
2600       return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2601     else if (this->target_->is_output_n32())
2602       return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2603     else if (!this->is_plt_header_compressed())
2604       return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2605     else if (this->target_->use_32bit_micromips_instructions())
2606       return (2 * sizeof(plt0_entry_micromips32_o32)
2607               / sizeof(plt0_entry_micromips32_o32[0]));
2608     else
2609       return (2 * sizeof(plt0_entry_micromips_o32)
2610               / sizeof(plt0_entry_micromips_o32[0]));
2611   }
2612
2613   // Return the PLT header entry.
2614   const uint32_t*
2615   get_plt_header_entry() const
2616   {
2617     if (this->target_->is_output_n64())
2618       return plt0_entry_n64;
2619     else if (this->target_->is_output_n32())
2620       return plt0_entry_n32;
2621     else if (!this->is_plt_header_compressed())
2622       return plt0_entry_o32;
2623     else if (this->target_->use_32bit_micromips_instructions())
2624       return plt0_entry_micromips32_o32;
2625     else
2626       return plt0_entry_micromips_o32;
2627   }
2628
2629   // Return the size of the standard PLT entry.
2630   unsigned int
2631   standard_plt_entry_size() const
2632   { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2633
2634   // Return the size of the compressed PLT entry.
2635   unsigned int
2636   compressed_plt_entry_size() const
2637   {
2638     gold_assert(!this->target_->is_output_newabi());
2639
2640     if (!this->target_->is_output_micromips())
2641       return (2 * sizeof(plt_entry_mips16_o32)
2642               / sizeof(plt_entry_mips16_o32[0]));
2643     else if (this->target_->use_32bit_micromips_instructions())
2644       return (2 * sizeof(plt_entry_micromips32_o32)
2645               / sizeof(plt_entry_micromips32_o32[0]));
2646     else
2647       return (2 * sizeof(plt_entry_micromips_o32)
2648               / sizeof(plt_entry_micromips_o32[0]));
2649   }
2650
2651   // The reloc section.
2652   Reloc_section* rel_;
2653   // The .got.plt section.
2654   Output_data_space* got_plt_;
2655   // Symbols that have PLT entry.
2656   std::vector<Mips_symbol<size>*> symbols_;
2657   // The offset of the next standard PLT entry to create.
2658   unsigned int plt_mips_offset_;
2659   // The offset of the next compressed PLT entry to create.
2660   unsigned int plt_comp_offset_;
2661   // The size of the PLT header in bytes.
2662   unsigned int plt_header_size_;
2663   // The target.
2664   Target_mips<size, big_endian>* target_;
2665 };
2666
2667 // A class to handle the .MIPS.stubs data.
2668
2669 template<int size, bool big_endian>
2670 class Mips_output_data_mips_stubs : public Output_section_data
2671 {
2672   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2673
2674   // Unordered set of .MIPS.stubs entries.
2675   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2676       Mips_stubs_entry_set;
2677
2678  public:
2679    Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2680      : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2681        stub_offsets_are_set_(false), target_(target)
2682    { }
2683
2684   // Create entry for a symbol.
2685   void
2686   make_entry(Mips_symbol<size>*);
2687
2688   // Remove entry for a symbol.
2689   void
2690   remove_entry(Mips_symbol<size>* gsym);
2691
2692   // Set stub offsets for symbols.  This method expects that the number of
2693   // entries in dynamic symbol table is set.
2694   void
2695   set_lazy_stub_offsets();
2696
2697   void
2698   set_needs_dynsym_value();
2699
2700    // Set the number of entries in dynamic symbol table.
2701   void
2702   set_dynsym_count(unsigned int dynsym_count)
2703   { this->dynsym_count_ = dynsym_count; }
2704
2705   // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2706   // count is greater than 0x10000.  If the dynamic symbol count is less than
2707   // 0x10000, the stub will be 4 bytes smaller.
2708   // There's no disadvantage from using microMIPS code here, so for the sake of
2709   // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2710   // output produced at all.  This has a benefit of stubs being shorter by
2711   // 4 bytes each too, unless in the insn32 mode.
2712   unsigned int
2713   stub_max_size() const
2714   {
2715     if (!this->target_->is_output_micromips()
2716         || this->target_->use_32bit_micromips_instructions())
2717       return 20;
2718     else
2719       return 16;
2720   }
2721
2722   // Return the size of the stub.  This method expects that the final dynsym
2723   // count is set.
2724   unsigned int
2725   stub_size() const
2726   {
2727     gold_assert(this->dynsym_count_ != -1U);
2728     if (this->dynsym_count_ > 0x10000)
2729       return this->stub_max_size();
2730     else
2731       return this->stub_max_size() - 4;
2732   }
2733
2734   // Return output address of a stub.
2735   Mips_address
2736   stub_address(const Mips_symbol<size>* sym) const
2737   {
2738     gold_assert(sym->has_lazy_stub());
2739     return this->address() + sym->lazy_stub_offset();
2740   }
2741
2742  protected:
2743   void
2744   do_adjust_output_section(Output_section* os)
2745   { os->set_entsize(0); }
2746
2747   // Write to a map file.
2748   void
2749   do_print_to_mapfile(Mapfile* mapfile) const
2750   { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2751
2752  private:
2753   static const uint32_t lazy_stub_normal_1[];
2754   static const uint32_t lazy_stub_normal_1_n64[];
2755   static const uint32_t lazy_stub_normal_2[];
2756   static const uint32_t lazy_stub_normal_2_n64[];
2757   static const uint32_t lazy_stub_big[];
2758   static const uint32_t lazy_stub_big_n64[];
2759
2760   static const uint32_t lazy_stub_micromips_normal_1[];
2761   static const uint32_t lazy_stub_micromips_normal_1_n64[];
2762   static const uint32_t lazy_stub_micromips_normal_2[];
2763   static const uint32_t lazy_stub_micromips_normal_2_n64[];
2764   static const uint32_t lazy_stub_micromips_big[];
2765   static const uint32_t lazy_stub_micromips_big_n64[];
2766
2767   static const uint32_t lazy_stub_micromips32_normal_1[];
2768   static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2769   static const uint32_t lazy_stub_micromips32_normal_2[];
2770   static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2771   static const uint32_t lazy_stub_micromips32_big[];
2772   static const uint32_t lazy_stub_micromips32_big_n64[];
2773
2774   // Set the final size.
2775   void
2776   set_final_data_size()
2777   { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2778
2779   // Write out the .MIPS.stubs data.
2780   void
2781   do_write(Output_file*);
2782
2783   // .MIPS.stubs symbols
2784   Mips_stubs_entry_set symbols_;
2785   // Number of entries in dynamic symbol table.
2786   unsigned int dynsym_count_;
2787   // Whether the stub offsets are set.
2788   bool stub_offsets_are_set_;
2789   // The target.
2790   Target_mips<size, big_endian>* target_;
2791 };
2792
2793 // This class handles Mips .reginfo output section.
2794
2795 template<int size, bool big_endian>
2796 class Mips_output_section_reginfo : public Output_section_data
2797 {
2798   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2799
2800  public:
2801   Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2802                               Valtype gprmask, Valtype cprmask1,
2803                               Valtype cprmask2, Valtype cprmask3,
2804                               Valtype cprmask4)
2805     : Output_section_data(24, 4, true), target_(target),
2806       gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2807       cprmask3_(cprmask3), cprmask4_(cprmask4)
2808   { }
2809
2810  protected:
2811   // Write to a map file.
2812   void
2813   do_print_to_mapfile(Mapfile* mapfile) const
2814   { mapfile->print_output_data(this, _(".reginfo")); }
2815
2816   // Write out reginfo section.
2817   void
2818   do_write(Output_file* of);
2819
2820  private:
2821   Target_mips<size, big_endian>* target_;
2822
2823   // gprmask of the output .reginfo section.
2824   Valtype gprmask_;
2825   // cprmask1 of the output .reginfo section.
2826   Valtype cprmask1_;
2827   // cprmask2 of the output .reginfo section.
2828   Valtype cprmask2_;
2829   // cprmask3 of the output .reginfo section.
2830   Valtype cprmask3_;
2831   // cprmask4 of the output .reginfo section.
2832   Valtype cprmask4_;
2833 };
2834
2835 // This class handles .MIPS.options output section.
2836
2837 template<int size, bool big_endian>
2838 class Mips_output_section_options : public Output_section
2839 {
2840  public:
2841   Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
2842                               elfcpp::Elf_Xword flags,
2843                               Target_mips<size, big_endian>* target)
2844     : Output_section(name, type, flags), target_(target)
2845   {
2846     // After the input sections are written, we only need to update
2847     // ri_gp_value field of ODK_REGINFO entries.
2848     this->set_after_input_sections();
2849   }
2850
2851  protected:
2852   // Write out option section.
2853   void
2854   do_write(Output_file* of);
2855
2856  private:
2857   Target_mips<size, big_endian>* target_;
2858 };
2859
2860 // This class handles .MIPS.abiflags output section.
2861
2862 template<int size, bool big_endian>
2863 class Mips_output_section_abiflags : public Output_section_data
2864 {
2865  public:
2866   Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2867     : Output_section_data(24, 8, true), abiflags_(abiflags)
2868   { }
2869
2870  protected:
2871   // Write to a map file.
2872   void
2873   do_print_to_mapfile(Mapfile* mapfile) const
2874   { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2875
2876   void
2877   do_write(Output_file* of);
2878
2879  private:
2880   const Mips_abiflags<big_endian>& abiflags_;
2881 };
2882
2883 // The MIPS target has relocation types which default handling of relocatable
2884 // relocation cannot process.  So we have to extend the default code.
2885
2886 template<bool big_endian, typename Classify_reloc>
2887 class Mips_scan_relocatable_relocs :
2888   public Default_scan_relocatable_relocs<Classify_reloc>
2889 {
2890  public:
2891   // Return the strategy to use for a local symbol which is a section
2892   // symbol, given the relocation type.
2893   inline Relocatable_relocs::Reloc_strategy
2894   local_section_strategy(unsigned int r_type, Relobj* object)
2895   {
2896     if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2897       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2898     else
2899       {
2900         switch (r_type)
2901           {
2902           case elfcpp::R_MIPS_26:
2903             return Relocatable_relocs::RELOC_SPECIAL;
2904
2905           default:
2906             return Default_scan_relocatable_relocs<Classify_reloc>::
2907                 local_section_strategy(r_type, object);
2908           }
2909       }
2910   }
2911 };
2912
2913 // Mips_copy_relocs class.  The only difference from the base class is the
2914 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2915 // Mips cannot convert all relocation types to dynamic relocs.  If a reloc
2916 // cannot be made dynamic, a COPY reloc is emitted.
2917
2918 template<int sh_type, int size, bool big_endian>
2919 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2920 {
2921  public:
2922   Mips_copy_relocs()
2923     : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2924   { }
2925
2926   // Emit any saved relocations which turn out to be needed.  This is
2927   // called after all the relocs have been scanned.
2928   void
2929   emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2930             Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2931
2932  private:
2933   typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2934     Copy_reloc_entry;
2935
2936   // Emit this reloc if appropriate.  This is called after we have
2937   // scanned all the relocations, so we know whether we emitted a
2938   // COPY relocation for SYM_.
2939   void
2940   emit_entry(Copy_reloc_entry& entry,
2941              Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2942              Symbol_table* symtab, Layout* layout,
2943              Target_mips<size, big_endian>* target);
2944 };
2945
2946
2947 // Return true if the symbol SYM should be considered to resolve local
2948 // to the current module, and false otherwise.  The logic is taken from
2949 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2950 static bool
2951 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2952                   bool local_protected)
2953 {
2954   // If it's a local sym, of course we resolve locally.
2955   if (sym == NULL)
2956     return true;
2957
2958   // STV_HIDDEN or STV_INTERNAL ones must be local.
2959   if (sym->visibility() == elfcpp::STV_HIDDEN
2960       || sym->visibility() == elfcpp::STV_INTERNAL)
2961     return true;
2962
2963   // If we don't have a definition in a regular file, then we can't
2964   // resolve locally.  The sym is either undefined or dynamic.
2965   if (sym->is_from_dynobj() || sym->is_undefined())
2966     return false;
2967
2968   // Forced local symbols resolve locally.
2969   if (sym->is_forced_local())
2970     return true;
2971
2972   // As do non-dynamic symbols.
2973   if (!has_dynsym_entry)
2974     return true;
2975
2976   // At this point, we know the symbol is defined and dynamic.  In an
2977   // executable it must resolve locally, likewise when building symbolic
2978   // shared libraries.
2979   if (parameters->options().output_is_executable()
2980       || parameters->options().Bsymbolic())
2981     return true;
2982
2983   // Now deal with defined dynamic symbols in shared libraries.  Ones
2984   // with default visibility might not resolve locally.
2985   if (sym->visibility() == elfcpp::STV_DEFAULT)
2986     return false;
2987
2988   // STV_PROTECTED non-function symbols are local.
2989   if (sym->type() != elfcpp::STT_FUNC)
2990     return true;
2991
2992   // Function pointer equality tests may require that STV_PROTECTED
2993   // symbols be treated as dynamic symbols.  If the address of a
2994   // function not defined in an executable is set to that function's
2995   // plt entry in the executable, then the address of the function in
2996   // a shared library must also be the plt entry in the executable.
2997   return local_protected;
2998 }
2999
3000 // Return TRUE if references to this symbol always reference the symbol in this
3001 // object.
3002 static bool
3003 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3004 {
3005   return symbol_refs_local(sym, has_dynsym_entry, false);
3006 }
3007
3008 // Return TRUE if calls to this symbol always call the version in this object.
3009 static bool
3010 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3011 {
3012   return symbol_refs_local(sym, has_dynsym_entry, true);
3013 }
3014
3015 // Compare GOT offsets of two symbols.
3016
3017 template<int size, bool big_endian>
3018 static bool
3019 got_offset_compare(Symbol* sym1, Symbol* sym2)
3020 {
3021   Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
3022   Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
3023   unsigned int area1 = mips_sym1->global_got_area();
3024   unsigned int area2 = mips_sym2->global_got_area();
3025   gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
3026
3027   // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3028   if (area1 != area2)
3029     return area1 < area2;
3030
3031   return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
3032 }
3033
3034 // This method divides dynamic symbols into symbols that have GOT entry, and
3035 // symbols that don't have GOT entry.  It also sorts symbols with the GOT entry.
3036 // Mips ABI requires that symbols with the GOT entry must be at the end of
3037 // dynamic symbol table, and the order in dynamic symbol table must match the
3038 // order in GOT.
3039
3040 template<int size, bool big_endian>
3041 static void
3042 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3043                     std::vector<Symbol*>* non_got_symbols,
3044                     std::vector<Symbol*>* got_symbols)
3045 {
3046   for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3047        p != dyn_symbols->end();
3048        ++p)
3049     {
3050       Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3051       if (mips_sym->global_got_area() == GGA_NORMAL
3052           || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3053         got_symbols->push_back(mips_sym);
3054       else
3055         non_got_symbols->push_back(mips_sym);
3056     }
3057
3058   std::sort(got_symbols->begin(), got_symbols->end(),
3059             got_offset_compare<size, big_endian>);
3060 }
3061
3062 // Functor class for processing the global symbol table.
3063
3064 template<int size, bool big_endian>
3065 class Symbol_visitor_check_symbols
3066 {
3067  public:
3068   Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3069     Layout* layout, Symbol_table* symtab)
3070     : target_(target), layout_(layout), symtab_(symtab)
3071   { }
3072
3073   void
3074   operator()(Sized_symbol<size>* sym)
3075   {
3076     Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3077     if (local_pic_function<size, big_endian>(mips_sym))
3078       {
3079         // SYM is a function that might need $25 to be valid on entry.
3080         // If we're creating a non-PIC relocatable object, mark SYM as
3081         // being PIC.  If we're creating a non-relocatable object with
3082         // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3083         // stub.
3084         if (parameters->options().relocatable())
3085           {
3086             if (!parameters->options().output_is_position_independent())
3087               mips_sym->set_pic();
3088           }
3089         else if (mips_sym->has_nonpic_branches())
3090           {
3091             this->target_->la25_stub_section(layout_)
3092                 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3093           }
3094       }
3095   }
3096
3097  private:
3098   Target_mips<size, big_endian>* target_;
3099   Layout* layout_;
3100   Symbol_table* symtab_;
3101 };
3102
3103 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3104 // and endianness. The relocation format for MIPS-64 is non-standard.
3105
3106 template<int sh_type, int size, bool big_endian>
3107 struct Mips_reloc_types;
3108
3109 template<bool big_endian>
3110 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3111 {
3112   typedef typename elfcpp::Rel<32, big_endian> Reloc;
3113   typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3114
3115   static typename elfcpp::Elf_types<32>::Elf_Swxword
3116   get_r_addend(const Reloc*)
3117   { return 0; }
3118
3119   static inline void
3120   set_reloc_addend(Reloc_write*,
3121                    typename elfcpp::Elf_types<32>::Elf_Swxword)
3122   { gold_unreachable(); }
3123 };
3124
3125 template<bool big_endian>
3126 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3127 {
3128   typedef typename elfcpp::Rela<32, big_endian> Reloc;
3129   typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3130
3131   static typename elfcpp::Elf_types<32>::Elf_Swxword
3132   get_r_addend(const Reloc* reloc)
3133   { return reloc->get_r_addend(); }
3134
3135   static inline void
3136   set_reloc_addend(Reloc_write* p,
3137                    typename elfcpp::Elf_types<32>::Elf_Swxword val)
3138   { p->put_r_addend(val); }
3139 };
3140
3141 template<bool big_endian>
3142 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3143 {
3144   typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3145   typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3146
3147   static typename elfcpp::Elf_types<64>::Elf_Swxword
3148   get_r_addend(const Reloc*)
3149   { return 0; }
3150
3151   static inline void
3152   set_reloc_addend(Reloc_write*,
3153                    typename elfcpp::Elf_types<64>::Elf_Swxword)
3154   { gold_unreachable(); }
3155 };
3156
3157 template<bool big_endian>
3158 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3159 {
3160   typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3161   typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3162
3163   static typename elfcpp::Elf_types<64>::Elf_Swxword
3164   get_r_addend(const Reloc* reloc)
3165   { return reloc->get_r_addend(); }
3166
3167   static inline void
3168   set_reloc_addend(Reloc_write* p,
3169                    typename elfcpp::Elf_types<64>::Elf_Swxword val)
3170   { p->put_r_addend(val); }
3171 };
3172
3173 // Forward declaration.
3174 static unsigned int
3175 mips_get_size_for_reloc(unsigned int, Relobj*);
3176
3177 // A class for inquiring about properties of a relocation,
3178 // used while scanning relocs during a relocatable link and
3179 // garbage collection.
3180
3181 template<int sh_type_, int size, bool big_endian>
3182 class Mips_classify_reloc;
3183
3184 template<int sh_type_, bool big_endian>
3185 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3186     public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3187 {
3188  public:
3189   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3190       Reltype;
3191   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3192       Reltype_write;
3193
3194   // Return the symbol referred to by the relocation.
3195   static inline unsigned int
3196   get_r_sym(const Reltype* reloc)
3197   { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3198
3199   // Return the type of the relocation.
3200   static inline unsigned int
3201   get_r_type(const Reltype* reloc)
3202   { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3203
3204   static inline unsigned int
3205   get_r_type2(const Reltype*)
3206   { return 0; }
3207
3208   static inline unsigned int
3209   get_r_type3(const Reltype*)
3210   { return 0; }
3211
3212   static inline unsigned int
3213   get_r_ssym(const Reltype*)
3214   { return 0; }
3215
3216   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3217   static inline unsigned int
3218   get_r_addend(const Reltype* reloc)
3219   {
3220     if (sh_type_ == elfcpp::SHT_REL)
3221       return 0;
3222     return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3223   }
3224
3225   // Write the r_info field to a new reloc, using the r_info field from
3226   // the original reloc, replacing the r_sym field with R_SYM.
3227   static inline void
3228   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3229   {
3230     unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3231     new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3232   }
3233
3234   // Write the r_addend field to a new reloc.
3235   static inline void
3236   put_r_addend(Reltype_write* to,
3237                typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3238   { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3239
3240   // Return the size of the addend of the relocation (only used for SHT_REL).
3241   static unsigned int
3242   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3243   { return mips_get_size_for_reloc(r_type, obj); }
3244 };
3245
3246 template<int sh_type_, bool big_endian>
3247 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3248     public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3249 {
3250  public:
3251   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3252       Reltype;
3253   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3254       Reltype_write;
3255
3256   // Return the symbol referred to by the relocation.
3257   static inline unsigned int
3258   get_r_sym(const Reltype* reloc)
3259   { return reloc->get_r_sym(); }
3260
3261   // Return the r_type of the relocation.
3262   static inline unsigned int
3263   get_r_type(const Reltype* reloc)
3264   { return reloc->get_r_type(); }
3265
3266   // Return the r_type2 of the relocation.
3267   static inline unsigned int
3268   get_r_type2(const Reltype* reloc)
3269   { return reloc->get_r_type2(); }
3270
3271   // Return the r_type3 of the relocation.
3272   static inline unsigned int
3273   get_r_type3(const Reltype* reloc)
3274   { return reloc->get_r_type3(); }
3275
3276   // Return the special symbol of the relocation.
3277   static inline unsigned int
3278   get_r_ssym(const Reltype* reloc)
3279   { return reloc->get_r_ssym(); }
3280
3281   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3282   static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3283   get_r_addend(const Reltype* reloc)
3284   {
3285     if (sh_type_ == elfcpp::SHT_REL)
3286       return 0;
3287     return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3288   }
3289
3290   // Write the r_info field to a new reloc, using the r_info field from
3291   // the original reloc, replacing the r_sym field with R_SYM.
3292   static inline void
3293   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3294   {
3295     new_reloc->put_r_sym(r_sym);
3296     new_reloc->put_r_ssym(reloc->get_r_ssym());
3297     new_reloc->put_r_type3(reloc->get_r_type3());
3298     new_reloc->put_r_type2(reloc->get_r_type2());
3299     new_reloc->put_r_type(reloc->get_r_type());
3300   }
3301
3302   // Write the r_addend field to a new reloc.
3303   static inline void
3304   put_r_addend(Reltype_write* to,
3305                typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3306   { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3307
3308   // Return the size of the addend of the relocation (only used for SHT_REL).
3309   static unsigned int
3310   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3311   { return mips_get_size_for_reloc(r_type, obj); }
3312 };
3313
3314 template<int size, bool big_endian>
3315 class Target_mips : public Sized_target<size, big_endian>
3316 {
3317   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3318   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3319     Reloc_section;
3320   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3321   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3322   typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3323       Reltype;
3324   typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3325       Relatype;
3326
3327  public:
3328   Target_mips(const Target::Target_info* info = &mips_info)
3329     : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3330       got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3331       dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3332       mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3333       mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
3334       entry_symbol_is_compressed_(false), insn32_(false)
3335   {
3336     this->add_machine_extensions();
3337   }
3338
3339   // The offset of $gp from the beginning of the .got section.
3340   static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3341
3342   // The maximum size of the GOT for it to be addressable using 16-bit
3343   // offsets from $gp.
3344   static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3345
3346   // Make a new symbol table entry for the Mips target.
3347   Sized_symbol<size>*
3348   make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3349   { return new Mips_symbol<size>(); }
3350
3351   // Process the relocations to determine unreferenced sections for
3352   // garbage collection.
3353   void
3354   gc_process_relocs(Symbol_table* symtab,
3355                     Layout* layout,
3356                     Sized_relobj_file<size, big_endian>* object,
3357                     unsigned int data_shndx,
3358                     unsigned int sh_type,
3359                     const unsigned char* prelocs,
3360                     size_t reloc_count,
3361                     Output_section* output_section,
3362                     bool needs_special_offset_handling,
3363                     size_t local_symbol_count,
3364                     const unsigned char* plocal_symbols);
3365
3366   // Scan the relocations to look for symbol adjustments.
3367   void
3368   scan_relocs(Symbol_table* symtab,
3369               Layout* layout,
3370               Sized_relobj_file<size, big_endian>* object,
3371               unsigned int data_shndx,
3372               unsigned int sh_type,
3373               const unsigned char* prelocs,
3374               size_t reloc_count,
3375               Output_section* output_section,
3376               bool needs_special_offset_handling,
3377               size_t local_symbol_count,
3378               const unsigned char* plocal_symbols);
3379
3380   // Finalize the sections.
3381   void
3382   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3383
3384   // Relocate a section.
3385   void
3386   relocate_section(const Relocate_info<size, big_endian>*,
3387                    unsigned int sh_type,
3388                    const unsigned char* prelocs,
3389                    size_t reloc_count,
3390                    Output_section* output_section,
3391                    bool needs_special_offset_handling,
3392                    unsigned char* view,
3393                    Mips_address view_address,
3394                    section_size_type view_size,
3395                    const Reloc_symbol_changes*);
3396
3397   // Scan the relocs during a relocatable link.
3398   void
3399   scan_relocatable_relocs(Symbol_table* symtab,
3400                           Layout* layout,
3401                           Sized_relobj_file<size, big_endian>* object,
3402                           unsigned int data_shndx,
3403                           unsigned int sh_type,
3404                           const unsigned char* prelocs,
3405                           size_t reloc_count,
3406                           Output_section* output_section,
3407                           bool needs_special_offset_handling,
3408                           size_t local_symbol_count,
3409                           const unsigned char* plocal_symbols,
3410                           Relocatable_relocs*);
3411
3412   // Scan the relocs for --emit-relocs.
3413   void
3414   emit_relocs_scan(Symbol_table* symtab,
3415                    Layout* layout,
3416                    Sized_relobj_file<size, big_endian>* object,
3417                    unsigned int data_shndx,
3418                    unsigned int sh_type,
3419                    const unsigned char* prelocs,
3420                    size_t reloc_count,
3421                    Output_section* output_section,
3422                    bool needs_special_offset_handling,
3423                    size_t local_symbol_count,
3424                    const unsigned char* plocal_syms,
3425                    Relocatable_relocs* rr);
3426
3427   // Emit relocations for a section.
3428   void
3429   relocate_relocs(const Relocate_info<size, big_endian>*,
3430                   unsigned int sh_type,
3431                   const unsigned char* prelocs,
3432                   size_t reloc_count,
3433                   Output_section* output_section,
3434                   typename elfcpp::Elf_types<size>::Elf_Off
3435                     offset_in_output_section,
3436                   unsigned char* view,
3437                   Mips_address view_address,
3438                   section_size_type view_size,
3439                   unsigned char* reloc_view,
3440                   section_size_type reloc_view_size);
3441
3442   // Perform target-specific processing in a relocatable link.  This is
3443   // only used if we use the relocation strategy RELOC_SPECIAL.
3444   void
3445   relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3446                                unsigned int sh_type,
3447                                const unsigned char* preloc_in,
3448                                size_t relnum,
3449                                Output_section* output_section,
3450                                typename elfcpp::Elf_types<size>::Elf_Off
3451                                  offset_in_output_section,
3452                                unsigned char* view,
3453                                Mips_address view_address,
3454                                section_size_type view_size,
3455                                unsigned char* preloc_out);
3456
3457   // Return whether SYM is defined by the ABI.
3458   bool
3459   do_is_defined_by_abi(const Symbol* sym) const
3460   {
3461     return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3462             || (strcmp(sym->name(), "_gp_disp") == 0)
3463             || (strcmp(sym->name(), "___tls_get_addr") == 0));
3464   }
3465
3466   // Return the number of entries in the GOT.
3467   unsigned int
3468   got_entry_count() const
3469   {
3470     if (!this->has_got_section())
3471       return 0;
3472     return this->got_size() / (size/8);
3473   }
3474
3475   // Return the number of entries in the PLT.
3476   unsigned int
3477   plt_entry_count() const
3478   {
3479     if (this->plt_ == NULL)
3480       return 0;
3481     return this->plt_->entry_count();
3482   }
3483
3484   // Return the offset of the first non-reserved PLT entry.
3485   unsigned int
3486   first_plt_entry_offset() const
3487   { return this->plt_->first_plt_entry_offset(); }
3488
3489   // Return the size of each PLT entry.
3490   unsigned int
3491   plt_entry_size() const
3492   { return this->plt_->plt_entry_size(); }
3493
3494   // Get the GOT section, creating it if necessary.
3495   Mips_output_data_got<size, big_endian>*
3496   got_section(Symbol_table*, Layout*);
3497
3498   // Get the GOT section.
3499   Mips_output_data_got<size, big_endian>*
3500   got_section() const
3501   {
3502     gold_assert(this->got_ != NULL);
3503     return this->got_;
3504   }
3505
3506   // Get the .MIPS.stubs section, creating it if necessary.
3507   Mips_output_data_mips_stubs<size, big_endian>*
3508   mips_stubs_section(Layout* layout);
3509
3510   // Get the .MIPS.stubs section.
3511   Mips_output_data_mips_stubs<size, big_endian>*
3512   mips_stubs_section() const
3513   {
3514     gold_assert(this->mips_stubs_ != NULL);
3515     return this->mips_stubs_;
3516   }
3517
3518   // Get the LA25 stub section, creating it if necessary.
3519   Mips_output_data_la25_stub<size, big_endian>*
3520   la25_stub_section(Layout*);
3521
3522   // Get the LA25 stub section.
3523   Mips_output_data_la25_stub<size, big_endian>*
3524   la25_stub_section()
3525   {
3526     gold_assert(this->la25_stub_ != NULL);
3527     return this->la25_stub_;
3528   }
3529
3530   // Get gp value.  It has the value of .got + 0x7FF0.
3531   Mips_address
3532   gp_value() const
3533   {
3534     if (this->gp_ != NULL)
3535       return this->gp_->value();
3536     return 0;
3537   }
3538
3539   // Get gp value.  It has the value of .got + 0x7FF0.  Adjust it for
3540   // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3541   Mips_address
3542   adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3543   {
3544     if (this->gp_ == NULL)
3545       return 0;
3546
3547     bool multi_got = false;
3548     if (this->has_got_section())
3549       multi_got = this->got_section()->multi_got();
3550     if (!multi_got)
3551       return this->gp_->value();
3552     else
3553       return this->gp_->value() + this->got_section()->get_got_offset(object);
3554   }
3555
3556   // Get the dynamic reloc section, creating it if necessary.
3557   Reloc_section*
3558   rel_dyn_section(Layout*);
3559
3560   bool
3561   do_has_custom_set_dynsym_indexes() const
3562   { return true; }
3563
3564   // Don't emit input .reginfo/.MIPS.abiflags sections to
3565   // output .reginfo/.MIPS.abiflags.
3566   bool
3567   do_should_include_section(elfcpp::Elf_Word sh_type) const
3568   {
3569     return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3570              && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3571   }
3572
3573   // Set the dynamic symbol indexes.  INDEX is the index of the first
3574   // global dynamic symbol.  Pointers to the symbols are stored into the
3575   // vector SYMS.  The names are added to DYNPOOL.  This returns an
3576   // updated dynamic symbol index.
3577   unsigned int
3578   do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3579                         std::vector<Symbol*>* syms, Stringpool* dynpool,
3580                         Versions* versions, Symbol_table* symtab) const;
3581
3582   // Remove .MIPS.stubs entry for a symbol.
3583   void
3584   remove_lazy_stub_entry(Mips_symbol<size>* sym)
3585   {
3586     if (this->mips_stubs_ != NULL)
3587       this->mips_stubs_->remove_entry(sym);
3588   }
3589
3590   // The value to write into got[1] for SVR4 targets, to identify it is
3591   // a GNU object.  The dynamic linker can then use got[1] to store the
3592   // module pointer.
3593   uint64_t
3594   mips_elf_gnu_got1_mask()
3595   {
3596     if (this->is_output_n64())
3597       return (uint64_t)1 << 63;
3598     else
3599       return 1 << 31;
3600   }
3601
3602   // Whether the output has microMIPS code.  This is valid only after
3603   // merge_obj_e_flags() is called.
3604   bool
3605   is_output_micromips() const
3606   {
3607     gold_assert(this->are_processor_specific_flags_set());
3608     return elfcpp::is_micromips(this->processor_specific_flags());
3609   }
3610
3611   // Whether the output uses N32 ABI.  This is valid only after
3612   // merge_obj_e_flags() is called.
3613   bool
3614   is_output_n32() const
3615   {
3616     gold_assert(this->are_processor_specific_flags_set());
3617     return elfcpp::abi_n32(this->processor_specific_flags());
3618   }
3619
3620   // Whether the output uses R6 ISA.  This is valid only after
3621   // merge_obj_e_flags() is called.
3622   bool
3623   is_output_r6() const
3624   {
3625     gold_assert(this->are_processor_specific_flags_set());
3626     return elfcpp::r6_isa(this->processor_specific_flags());
3627   }
3628
3629   // Whether the output uses N64 ABI.
3630   bool
3631   is_output_n64() const
3632   { return size == 64; }
3633
3634   // Whether the output uses NEWABI.  This is valid only after
3635   // merge_obj_e_flags() is called.
3636   bool
3637   is_output_newabi() const
3638   { return this->is_output_n32() || this->is_output_n64(); }
3639
3640   // Whether we can only use 32-bit microMIPS instructions.
3641   bool
3642   use_32bit_micromips_instructions() const
3643   { return this->insn32_; }
3644
3645   // Return the r_sym field from a relocation.
3646   unsigned int
3647   get_r_sym(const unsigned char* preloc) const
3648   {
3649     // Since REL and RELA relocs share the same structure through
3650     // the r_info field, we can just use REL here.
3651     Reltype rel(preloc);
3652     return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3653         get_r_sym(&rel);
3654   }
3655
3656  protected:
3657   // Return the value to use for a dynamic symbol which requires special
3658   // treatment.  This is how we support equality comparisons of function
3659   // pointers across shared library boundaries, as described in the
3660   // processor specific ABI supplement.
3661   uint64_t
3662   do_dynsym_value(const Symbol* gsym) const;
3663
3664   // Make an ELF object.
3665   Object*
3666   do_make_elf_object(const std::string&, Input_file*, off_t,
3667                      const elfcpp::Ehdr<size, big_endian>& ehdr);
3668
3669   Object*
3670   do_make_elf_object(const std::string&, Input_file*, off_t,
3671                      const elfcpp::Ehdr<size, !big_endian>&)
3672   { gold_unreachable(); }
3673
3674   // Make an output section.
3675   Output_section*
3676   do_make_output_section(const char* name, elfcpp::Elf_Word type,
3677                          elfcpp::Elf_Xword flags)
3678     {
3679       if (type == elfcpp::SHT_MIPS_OPTIONS)
3680         return new Mips_output_section_options<size, big_endian>(name, type,
3681                                                                  flags, this);
3682       else
3683         return new Output_section(name, type, flags);
3684     }
3685
3686   // Adjust ELF file header.
3687   void
3688   do_adjust_elf_header(unsigned char* view, int len);
3689
3690   // Get the custom dynamic tag value.
3691   unsigned int
3692   do_dynamic_tag_custom_value(elfcpp::DT) const;
3693
3694   // Adjust the value written to the dynamic symbol table.
3695   virtual void
3696   do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3697   {
3698     elfcpp::Sym<size, big_endian> isym(view);
3699     elfcpp::Sym_write<size, big_endian> osym(view);
3700     const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3701
3702     // Keep dynamic compressed symbols odd.  This allows the dynamic linker
3703     // to treat compressed symbols like any other.
3704     Mips_address value = isym.get_st_value();
3705     if (mips_sym->is_mips16() && value != 0)
3706       {
3707         if (!mips_sym->has_mips16_fn_stub())
3708           value |= 1;
3709         else
3710           {
3711             // If we have a MIPS16 function with a stub, the dynamic symbol
3712             // must refer to the stub, since only the stub uses the standard
3713             // calling conventions.  Stub contains MIPS32 code, so don't add +1
3714             // in this case.
3715
3716             // There is a code which does this in the method
3717             // Target_mips::do_dynsym_value, but that code will only be
3718             // executed if the symbol is from dynobj.
3719             // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3720             // table.
3721
3722             Mips16_stub_section<size, big_endian>* fn_stub =
3723               mips_sym->template get_mips16_fn_stub<big_endian>();
3724             value = fn_stub->output_address();
3725             osym.put_st_size(fn_stub->section_size());
3726           }
3727
3728         osym.put_st_value(value);
3729         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3730                           mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3731       }
3732     else if ((mips_sym->is_micromips()
3733               // Stubs are always microMIPS if there is any microMIPS code in
3734               // the output.
3735               || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3736              && value != 0)
3737       {
3738         osym.put_st_value(value | 1);
3739         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3740                           mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3741       }
3742   }
3743
3744  private:
3745   // The class which scans relocations.
3746   class Scan
3747   {
3748    public:
3749     Scan()
3750     { }
3751
3752     static inline int
3753     get_reference_flags(unsigned int r_type);
3754
3755     inline void
3756     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3757           Sized_relobj_file<size, big_endian>* object,
3758           unsigned int data_shndx,
3759           Output_section* output_section,
3760           const Reltype& reloc, unsigned int r_type,
3761           const elfcpp::Sym<size, big_endian>& lsym,
3762           bool is_discarded);
3763
3764     inline void
3765     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3766           Sized_relobj_file<size, big_endian>* object,
3767           unsigned int data_shndx,
3768           Output_section* output_section,
3769           const Relatype& reloc, unsigned int r_type,
3770           const elfcpp::Sym<size, big_endian>& lsym,
3771           bool is_discarded);
3772
3773     inline void
3774     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3775           Sized_relobj_file<size, big_endian>* object,
3776           unsigned int data_shndx,
3777           Output_section* output_section,
3778           const Relatype* rela,
3779           const Reltype* rel,
3780           unsigned int rel_type,
3781           unsigned int r_type,
3782           const elfcpp::Sym<size, big_endian>& lsym,
3783           bool is_discarded);
3784
3785     inline void
3786     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3787            Sized_relobj_file<size, big_endian>* object,
3788            unsigned int data_shndx,
3789            Output_section* output_section,
3790            const Reltype& reloc, unsigned int r_type,
3791            Symbol* gsym);
3792
3793     inline void
3794     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3795            Sized_relobj_file<size, big_endian>* object,
3796            unsigned int data_shndx,
3797            Output_section* output_section,
3798            const Relatype& reloc, unsigned int r_type,
3799            Symbol* gsym);
3800
3801     inline void
3802     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3803            Sized_relobj_file<size, big_endian>* object,
3804            unsigned int data_shndx,
3805            Output_section* output_section,
3806            const Relatype* rela,
3807            const Reltype* rel,
3808            unsigned int rel_type,
3809            unsigned int r_type,
3810            Symbol* gsym);
3811
3812     inline bool
3813     local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3814                                         Target_mips*,
3815                                         Sized_relobj_file<size, big_endian>*,
3816                                         unsigned int,
3817                                         Output_section*,
3818                                         const Reltype&,
3819                                         unsigned int,
3820                                         const elfcpp::Sym<size, big_endian>&)
3821     { return false; }
3822
3823     inline bool
3824     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3825                                          Target_mips*,
3826                                          Sized_relobj_file<size, big_endian>*,
3827                                          unsigned int,
3828                                          Output_section*,
3829                                          const Reltype&,
3830                                          unsigned int, Symbol*)
3831     { return false; }
3832
3833     inline bool
3834     local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3835                                         Target_mips*,
3836                                         Sized_relobj_file<size, big_endian>*,
3837                                         unsigned int,
3838                                         Output_section*,
3839                                         const Relatype&,
3840                                         unsigned int,
3841                                         const elfcpp::Sym<size, big_endian>&)
3842     { return false; }
3843
3844     inline bool
3845     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3846                                          Target_mips*,
3847                                          Sized_relobj_file<size, big_endian>*,
3848                                          unsigned int,
3849                                          Output_section*,
3850                                          const Relatype&,
3851                                          unsigned int, Symbol*)
3852     { return false; }
3853    private:
3854     static void
3855     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3856                             unsigned int r_type);
3857
3858     static void
3859     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3860                              unsigned int r_type, Symbol*);
3861   };
3862
3863   // The class which implements relocation.
3864   class Relocate
3865   {
3866    public:
3867     Relocate()
3868       : calculated_value_(0), calculate_only_(false)
3869     { }
3870
3871     ~Relocate()
3872     { }
3873
3874     // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3875     inline bool
3876     should_apply_static_reloc(const Mips_symbol<size>* gsym,
3877                               unsigned int r_type,
3878                               Output_section* output_section,
3879                               Target_mips* target);
3880
3881     // Do a relocation.  Return false if the caller should not issue
3882     // any warnings about this relocation.
3883     inline bool
3884     relocate(const Relocate_info<size, big_endian>*, unsigned int,
3885              Target_mips*, Output_section*, size_t, const unsigned char*,
3886              const Sized_symbol<size>*, const Symbol_value<size>*,
3887              unsigned char*, Mips_address, section_size_type);
3888
3889    private:
3890     // Result of the relocation.
3891     Valtype calculated_value_;
3892     // Whether we have to calculate relocation instead of applying it.
3893     bool calculate_only_;
3894   };
3895
3896   // This POD class holds the dynamic relocations that should be emitted instead
3897   // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations.  We will emit these
3898   // relocations if it turns out that the symbol does not have static
3899   // relocations.
3900   class Dyn_reloc
3901   {
3902    public:
3903     Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3904               Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3905               Output_section* output_section, Mips_address r_offset)
3906       : sym_(sym), r_type_(r_type), relobj_(relobj),
3907         shndx_(shndx), output_section_(output_section),
3908         r_offset_(r_offset)
3909     { }
3910
3911     // Emit this reloc if appropriate.  This is called after we have
3912     // scanned all the relocations, so we know whether the symbol has
3913     // static relocations.
3914     void
3915     emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3916          Symbol_table* symtab)
3917     {
3918       if (!this->sym_->has_static_relocs())
3919         {
3920           got->record_global_got_symbol(this->sym_, this->relobj_,
3921                                         this->r_type_, true, false);
3922           if (!symbol_references_local(this->sym_,
3923                                 this->sym_->should_add_dynsym_entry(symtab)))
3924             rel_dyn->add_global(this->sym_, this->r_type_,
3925                                 this->output_section_, this->relobj_,
3926                                 this->shndx_, this->r_offset_);
3927           else
3928             rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3929                                           this->output_section_, this->relobj_,
3930                                           this->shndx_, this->r_offset_);
3931         }
3932     }
3933
3934    private:
3935     Mips_symbol<size>* sym_;
3936     unsigned int r_type_;
3937     Mips_relobj<size, big_endian>* relobj_;
3938     unsigned int shndx_;
3939     Output_section* output_section_;
3940     Mips_address r_offset_;
3941   };
3942
3943   // Adjust TLS relocation type based on the options and whether this
3944   // is a local symbol.
3945   static tls::Tls_optimization
3946   optimize_tls_reloc(bool is_final, int r_type);
3947
3948   // Return whether there is a GOT section.
3949   bool
3950   has_got_section() const
3951   { return this->got_ != NULL; }
3952
3953   // Check whether the given ELF header flags describe a 32-bit binary.
3954   bool
3955   mips_32bit_flags(elfcpp::Elf_Word);
3956
3957   enum Mips_mach {
3958     mach_mips3000             = 3000,
3959     mach_mips3900             = 3900,
3960     mach_mips4000             = 4000,
3961     mach_mips4010             = 4010,
3962     mach_mips4100             = 4100,
3963     mach_mips4111             = 4111,
3964     mach_mips4120             = 4120,
3965     mach_mips4300             = 4300,
3966     mach_mips4400             = 4400,
3967     mach_mips4600             = 4600,
3968     mach_mips4650             = 4650,
3969     mach_mips5000             = 5000,
3970     mach_mips5400             = 5400,
3971     mach_mips5500             = 5500,
3972     mach_mips5900             = 5900,
3973     mach_mips6000             = 6000,
3974     mach_mips7000             = 7000,
3975     mach_mips8000             = 8000,
3976     mach_mips9000             = 9000,
3977     mach_mips10000            = 10000,
3978     mach_mips12000            = 12000,
3979     mach_mips14000            = 14000,
3980     mach_mips16000            = 16000,
3981     mach_mips16               = 16,
3982     mach_mips5                = 5,
3983     mach_mips_loongson_2e     = 3001,
3984     mach_mips_loongson_2f     = 3002,
3985     mach_mips_gs464           = 3003,
3986     mach_mips_gs464e          = 3004,
3987     mach_mips_sb1             = 12310201, // octal 'SB', 01
3988     mach_mips_octeon          = 6501,
3989     mach_mips_octeonp         = 6601,
3990     mach_mips_octeon2         = 6502,
3991     mach_mips_octeon3         = 6503,
3992     mach_mips_xlr             = 887682,   // decimal 'XLR'
3993     mach_mipsisa32            = 32,
3994     mach_mipsisa32r2          = 33,
3995     mach_mipsisa32r3          = 34,
3996     mach_mipsisa32r5          = 36,
3997     mach_mipsisa32r6          = 37,
3998     mach_mipsisa64            = 64,
3999     mach_mipsisa64r2          = 65,
4000     mach_mipsisa64r3          = 66,
4001     mach_mipsisa64r5          = 68,
4002     mach_mipsisa64r6          = 69,
4003     mach_mips_micromips       = 96
4004   };
4005
4006   // Return the MACH for a MIPS e_flags value.
4007   unsigned int
4008   elf_mips_mach(elfcpp::Elf_Word);
4009
4010   // Return the MACH for each .MIPS.abiflags ISA Extension.
4011   unsigned int
4012   mips_isa_ext_mach(unsigned int);
4013
4014   // Return the .MIPS.abiflags value representing each ISA Extension.
4015   unsigned int
4016   mips_isa_ext(unsigned int);
4017
4018   // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4019   void
4020   update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4021                       Mips_abiflags<big_endian>*);
4022
4023   // Infer the content of the ABI flags based on the elf header.
4024   void
4025   infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4026
4027   // Create abiflags from elf header or from .MIPS.abiflags section.
4028   void
4029   create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4030
4031   // Return the meaning of fp_abi, or "unknown" if not known.
4032   const char*
4033   fp_abi_string(int);
4034
4035   // Select fp_abi.
4036   int
4037   select_fp_abi(const std::string&, int, int);
4038
4039   // Merge attributes from input object.
4040   void
4041   merge_obj_attributes(const std::string&, const Attributes_section_data*);
4042
4043   // Merge abiflags from input object.
4044   void
4045   merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4046
4047   // Check whether machine EXTENSION is an extension of machine BASE.
4048   bool
4049   mips_mach_extends(unsigned int, unsigned int);
4050
4051   // Merge file header flags from input object.
4052   void
4053   merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4054
4055   // Encode ISA level and revision as a single value.
4056   int
4057   level_rev(unsigned char isa_level, unsigned char isa_rev) const
4058   { return (isa_level << 3) | isa_rev; }
4059
4060   // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4061   static inline bool
4062   jal_to_bal()
4063   { return false; }
4064
4065   // True if we are linking for CPUs that are faster if JALR is converted to
4066   // BAL.  This should be safe for all architectures.  We enable this predicate
4067   // for all CPUs.
4068   static inline bool
4069   jalr_to_bal()
4070   { return true; }
4071
4072   // True if we are linking for CPUs that are faster if JR is converted to B.
4073   // This should be safe for all architectures.  We enable this predicate for
4074   // all CPUs.
4075   static inline bool
4076   jr_to_b()
4077   { return true; }
4078
4079   // Return the size of the GOT section.
4080   section_size_type
4081   got_size() const
4082   {
4083     gold_assert(this->got_ != NULL);
4084     return this->got_->data_size();
4085   }
4086
4087   // Create a PLT entry for a global symbol referenced by r_type relocation.
4088   void
4089   make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4090                  unsigned int r_type);
4091
4092   // Get the PLT section.
4093   Mips_output_data_plt<size, big_endian>*
4094   plt_section() const
4095   {
4096     gold_assert(this->plt_ != NULL);
4097     return this->plt_;
4098   }
4099
4100   // Get the GOT PLT section.
4101   const Mips_output_data_plt<size, big_endian>*
4102   got_plt_section() const
4103   {
4104     gold_assert(this->got_plt_ != NULL);
4105     return this->got_plt_;
4106   }
4107
4108   // Copy a relocation against a global symbol.
4109   void
4110   copy_reloc(Symbol_table* symtab, Layout* layout,
4111              Sized_relobj_file<size, big_endian>* object,
4112              unsigned int shndx, Output_section* output_section,
4113              Symbol* sym, unsigned int r_type, Mips_address r_offset)
4114   {
4115     this->copy_relocs_.copy_reloc(symtab, layout,
4116                                   symtab->get_sized_symbol<size>(sym),
4117                                   object, shndx, output_section,
4118                                   r_type, r_offset, 0,
4119                                   this->rel_dyn_section(layout));
4120   }
4121
4122   void
4123   dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4124                 Mips_relobj<size, big_endian>* relobj,
4125                 unsigned int shndx, Output_section* output_section,
4126                 Mips_address r_offset)
4127   {
4128     this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4129                                           output_section, r_offset));
4130   }
4131
4132   // Calculate value of _gp symbol.
4133   void
4134   set_gp(Layout*, Symbol_table*);
4135
4136   const char*
4137   elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4138   const char*
4139   elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4140
4141   // Adds entries that describe how machines relate to one another.  The entries
4142   // are ordered topologically with MIPS I extensions listed last.  First
4143   // element is extension, second element is base.
4144   void
4145   add_machine_extensions()
4146   {
4147     // MIPS64r2 extensions.
4148     this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4149     this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4150     this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4151     this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4152     this->add_extension(mach_mips_gs464e, mach_mips_gs464);
4153     this->add_extension(mach_mips_gs464, mach_mipsisa64r2);
4154
4155     // MIPS64 extensions.
4156     this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4157     this->add_extension(mach_mips_sb1, mach_mipsisa64);
4158     this->add_extension(mach_mips_xlr, mach_mipsisa64);
4159
4160     // MIPS V extensions.
4161     this->add_extension(mach_mipsisa64, mach_mips5);
4162
4163     // R10000 extensions.
4164     this->add_extension(mach_mips12000, mach_mips10000);
4165     this->add_extension(mach_mips14000, mach_mips10000);
4166     this->add_extension(mach_mips16000, mach_mips10000);
4167
4168     // R5000 extensions.  Note: the vr5500 ISA is an extension of the core
4169     // vr5400 ISA, but doesn't include the multimedia stuff.  It seems
4170     // better to allow vr5400 and vr5500 code to be merged anyway, since
4171     // many libraries will just use the core ISA.  Perhaps we could add
4172     // some sort of ASE flag if this ever proves a problem.
4173     this->add_extension(mach_mips5500, mach_mips5400);
4174     this->add_extension(mach_mips5400, mach_mips5000);
4175
4176     // MIPS IV extensions.
4177     this->add_extension(mach_mips5, mach_mips8000);
4178     this->add_extension(mach_mips10000, mach_mips8000);
4179     this->add_extension(mach_mips5000, mach_mips8000);
4180     this->add_extension(mach_mips7000, mach_mips8000);
4181     this->add_extension(mach_mips9000, mach_mips8000);
4182
4183     // VR4100 extensions.
4184     this->add_extension(mach_mips4120, mach_mips4100);
4185     this->add_extension(mach_mips4111, mach_mips4100);
4186
4187     // MIPS III extensions.
4188     this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4189     this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4190     this->add_extension(mach_mips8000, mach_mips4000);
4191     this->add_extension(mach_mips4650, mach_mips4000);
4192     this->add_extension(mach_mips4600, mach_mips4000);
4193     this->add_extension(mach_mips4400, mach_mips4000);
4194     this->add_extension(mach_mips4300, mach_mips4000);
4195     this->add_extension(mach_mips4100, mach_mips4000);
4196     this->add_extension(mach_mips4010, mach_mips4000);
4197     this->add_extension(mach_mips5900, mach_mips4000);
4198
4199     // MIPS32 extensions.
4200     this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4201
4202     // MIPS II extensions.
4203     this->add_extension(mach_mips4000, mach_mips6000);
4204     this->add_extension(mach_mipsisa32, mach_mips6000);
4205
4206     // MIPS I extensions.
4207     this->add_extension(mach_mips6000, mach_mips3000);
4208     this->add_extension(mach_mips3900, mach_mips3000);
4209   }
4210
4211   // Add value to MIPS extenstions.
4212   void
4213   add_extension(unsigned int base, unsigned int extension)
4214   {
4215     std::pair<unsigned int, unsigned int> ext(base, extension);
4216     this->mips_mach_extensions_.push_back(ext);
4217   }
4218
4219   // Return the number of entries in the .dynsym section.
4220   unsigned int get_dt_mips_symtabno() const
4221   {
4222     return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4223                            / elfcpp::Elf_sizes<size>::sym_size));
4224     // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4225   }
4226
4227   // Information about this specific target which we pass to the
4228   // general Target structure.
4229   static const Target::Target_info mips_info;
4230   // The GOT section.
4231   Mips_output_data_got<size, big_endian>* got_;
4232   // gp symbol.  It has the value of .got + 0x7FF0.
4233   Sized_symbol<size>* gp_;
4234   // The PLT section.
4235   Mips_output_data_plt<size, big_endian>* plt_;
4236   // The GOT PLT section.
4237   Output_data_space* got_plt_;
4238   // The dynamic reloc section.
4239   Reloc_section* rel_dyn_;
4240   // The .rld_map section.
4241   Output_data_zero_fill* rld_map_;
4242   // Relocs saved to avoid a COPY reloc.
4243   Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4244
4245   // A list of dyn relocs to be saved.
4246   std::vector<Dyn_reloc> dyn_relocs_;
4247
4248   // The LA25 stub section.
4249   Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4250   // Architecture extensions.
4251   std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4252   // .MIPS.stubs
4253   Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4254
4255   // Attributes section data in output.
4256   Attributes_section_data* attributes_section_data_;
4257   // .MIPS.abiflags section data in output.
4258   Mips_abiflags<big_endian>* abiflags_;
4259
4260   unsigned int mach_;
4261   Layout* layout_;
4262
4263   typename std::list<got16_addend<size, big_endian> > got16_addends_;
4264
4265   // Whether there is an input .MIPS.abiflags section.
4266   bool has_abiflags_section_;
4267
4268   // Whether the entry symbol is mips16 or micromips.
4269   bool entry_symbol_is_compressed_;
4270
4271   // Whether we can use only 32-bit microMIPS instructions.
4272   // TODO(sasa): This should be a linker option.
4273   bool insn32_;
4274 };
4275
4276 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4277 // It records high part of the relocation pair.
4278
4279 template<int size, bool big_endian>
4280 struct reloc_high
4281 {
4282   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4283
4284   reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4285              const Symbol_value<size>* _psymval, Mips_address _addend,
4286              unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4287              Mips_address _address = 0, bool _gp_disp = false)
4288     : view(_view), object(_object), psymval(_psymval), addend(_addend),
4289       r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4290       address(_address), gp_disp(_gp_disp)
4291   { }
4292
4293   unsigned char* view;
4294   const Mips_relobj<size, big_endian>* object;
4295   const Symbol_value<size>* psymval;
4296   Mips_address addend;
4297   unsigned int r_type;
4298   unsigned int r_sym;
4299   bool extract_addend;
4300   Mips_address address;
4301   bool gp_disp;
4302 };
4303
4304 template<int size, bool big_endian>
4305 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4306 {
4307   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4308   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4309   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4310   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4311   typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4312
4313  public:
4314   typedef enum
4315   {
4316     STATUS_OKAY,            // No error during relocation.
4317     STATUS_OVERFLOW,        // Relocation overflow.
4318     STATUS_BAD_RELOC,       // Relocation cannot be applied.
4319     STATUS_PCREL_UNALIGNED  // Unaligned PC-relative relocation.
4320   } Status;
4321
4322  private:
4323   typedef Relocate_functions<size, big_endian> Base;
4324   typedef Mips_relocate_functions<size, big_endian> This;
4325
4326   static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4327   static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4328   static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
4329
4330   template<int valsize>
4331   static inline typename This::Status
4332   check_overflow(Valtype value)
4333   {
4334     if (size == 32)
4335       return (Bits<valsize>::has_overflow32(value)
4336               ? This::STATUS_OVERFLOW
4337               : This::STATUS_OKAY);
4338
4339     return (Bits<valsize>::has_overflow(value)
4340             ? This::STATUS_OVERFLOW
4341             : This::STATUS_OKAY);
4342   }
4343
4344   static inline bool
4345   should_shuffle_micromips_reloc(unsigned int r_type)
4346   {
4347     return (micromips_reloc(r_type)
4348             && r_type != elfcpp::R_MICROMIPS_PC7_S1
4349             && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4350   }
4351
4352  public:
4353   //   R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4354   //   Most mips16 instructions are 16 bits, but these instructions
4355   //   are 32 bits.
4356   //
4357   //   The format of these instructions is:
4358   //
4359   //   +--------------+--------------------------------+
4360   //   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
4361   //   +--------------+--------------------------------+
4362   //   |                Immediate  15:0                |
4363   //   +-----------------------------------------------+
4364   //
4365   //   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
4366   //   Note that the immediate value in the first word is swapped.
4367   //
4368   //   When producing a relocatable object file, R_MIPS16_26 is
4369   //   handled mostly like R_MIPS_26.  In particular, the addend is
4370   //   stored as a straight 26-bit value in a 32-bit instruction.
4371   //   (gas makes life simpler for itself by never adjusting a
4372   //   R_MIPS16_26 reloc to be against a section, so the addend is
4373   //   always zero).  However, the 32 bit instruction is stored as 2
4374   //   16-bit values, rather than a single 32-bit value.  In a
4375   //   big-endian file, the result is the same; in a little-endian
4376   //   file, the two 16-bit halves of the 32 bit value are swapped.
4377   //   This is so that a disassembler can recognize the jal
4378   //   instruction.
4379   //
4380   //   When doing a final link, R_MIPS16_26 is treated as a 32 bit
4381   //   instruction stored as two 16-bit values.  The addend A is the
4382   //   contents of the targ26 field.  The calculation is the same as
4383   //   R_MIPS_26.  When storing the calculated value, reorder the
4384   //   immediate value as shown above, and don't forget to store the
4385   //   value as two 16-bit values.
4386   //
4387   //   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4388   //   defined as
4389   //
4390   //   big-endian:
4391   //   +--------+----------------------+
4392   //   |        |                      |
4393   //   |        |    targ26-16         |
4394   //   |31    26|25                   0|
4395   //   +--------+----------------------+
4396   //
4397   //   little-endian:
4398   //   +----------+------+-------------+
4399   //   |          |      |             |
4400   //   |  sub1    |      |     sub2    |
4401   //   |0        9|10  15|16         31|
4402   //   +----------+--------------------+
4403   //   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4404   //   ((sub1 << 16) | sub2)).
4405   //
4406   //   When producing a relocatable object file, the calculation is
4407   //   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4408   //   When producing a fully linked file, the calculation is
4409   //   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4410   //   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4411   //
4412   //   The table below lists the other MIPS16 instruction relocations.
4413   //   Each one is calculated in the same way as the non-MIPS16 relocation
4414   //   given on the right, but using the extended MIPS16 layout of 16-bit
4415   //   immediate fields:
4416   //
4417   //      R_MIPS16_GPREL          R_MIPS_GPREL16
4418   //      R_MIPS16_GOT16          R_MIPS_GOT16
4419   //      R_MIPS16_CALL16         R_MIPS_CALL16
4420   //      R_MIPS16_HI16           R_MIPS_HI16
4421   //      R_MIPS16_LO16           R_MIPS_LO16
4422   //
4423   //   A typical instruction will have a format like this:
4424   //
4425   //   +--------------+--------------------------------+
4426   //   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
4427   //   +--------------+--------------------------------+
4428   //   |    Major     |   rx   |   ry   |   Imm  4:0   |
4429   //   +--------------+--------------------------------+
4430   //
4431   //   EXTEND is the five bit value 11110.  Major is the instruction
4432   //   opcode.
4433   //
4434   //   All we need to do here is shuffle the bits appropriately.
4435   //   As above, the two 16-bit halves must be swapped on a
4436   //   little-endian system.
4437
4438   // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4439   // on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1
4440   // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4441
4442   static void
4443   mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4444                        bool jal_shuffle)
4445   {
4446     if (!mips16_reloc(r_type)
4447         && !should_shuffle_micromips_reloc(r_type))
4448       return;
4449
4450     // Pick up the first and second halfwords of the instruction.
4451     Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4452     Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4453     Valtype32 val;
4454
4455     if (micromips_reloc(r_type)
4456         || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4457       val = first << 16 | second;
4458     else if (r_type != elfcpp::R_MIPS16_26)
4459       val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4460              | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4461     else
4462       val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4463              | ((first & 0x1f) << 21) | second);
4464
4465     elfcpp::Swap<32, big_endian>::writeval(view, val);
4466   }
4467
4468   static void
4469   mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4470   {
4471     if (!mips16_reloc(r_type)
4472         && !should_shuffle_micromips_reloc(r_type))
4473       return;
4474
4475     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4476     Valtype16 first, second;
4477
4478     if (micromips_reloc(r_type)
4479         || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4480       {
4481         second = val & 0xffff;
4482         first = val >> 16;
4483       }
4484     else if (r_type != elfcpp::R_MIPS16_26)
4485       {
4486         second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4487         first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4488       }
4489     else
4490       {
4491         second = val & 0xffff;
4492         first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4493                  | ((val >> 21) & 0x1f);
4494       }
4495
4496     elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4497     elfcpp::Swap<16, big_endian>::writeval(view, first);
4498   }
4499
4500   // R_MIPS_16: S + sign-extend(A)
4501   static inline typename This::Status
4502   rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4503         const Symbol_value<size>* psymval, Mips_address addend_a,
4504         bool extract_addend, bool calculate_only, Valtype* calculated_value)
4505   {
4506     Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4507     Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4508
4509     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4510                                      : addend_a);
4511
4512     Valtype x = psymval->value(object, addend);
4513     val = Bits<16>::bit_select32(val, x, 0xffffU);
4514
4515     if (calculate_only)
4516       {
4517         *calculated_value = x;
4518         return This::STATUS_OKAY;
4519       }
4520     else
4521       elfcpp::Swap<16, big_endian>::writeval(wv, val);
4522
4523     return check_overflow<16>(x);
4524   }
4525
4526   // R_MIPS_32: S + A
4527   static inline typename This::Status
4528   rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4529         const Symbol_value<size>* psymval, Mips_address addend_a,
4530         bool extract_addend, bool calculate_only, Valtype* calculated_value)
4531   {
4532     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4533     Valtype addend = (extract_addend
4534                         ? elfcpp::Swap<32, big_endian>::readval(wv)
4535                         : addend_a);
4536     Valtype x = psymval->value(object, addend);
4537
4538     if (calculate_only)
4539       *calculated_value = x;
4540     else
4541       elfcpp::Swap<32, big_endian>::writeval(wv, x);
4542
4543     return This::STATUS_OKAY;
4544   }
4545
4546   // R_MIPS_JALR, R_MICROMIPS_JALR
4547   static inline typename This::Status
4548   reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4549           const Symbol_value<size>* psymval, Mips_address address,
4550           Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4551           unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4552           bool calculate_only, Valtype* calculated_value)
4553   {
4554     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4555     Valtype addend = extract_addend ? 0 : addend_a;
4556     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4557
4558     // Try converting J(AL)R to B(AL), if the target is in range.
4559     if (r_type == elfcpp::R_MIPS_JALR
4560         && !cross_mode_jump
4561         && ((jalr_to_bal && val == 0x0320f809)    // jalr t9
4562             || (jr_to_b && val == 0x03200008)))   // jr t9
4563       {
4564         int offset = psymval->value(object, addend) - (address + 4);
4565         if (!Bits<18>::has_overflow32(offset))
4566           {
4567             if (val == 0x03200008)   // jr t9
4568               val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff);  // b addr
4569             else
4570               val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4571           }
4572       }
4573
4574     if (calculate_only)
4575       *calculated_value = val;
4576     else
4577       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4578
4579     return This::STATUS_OKAY;
4580   }
4581
4582   // R_MIPS_PC32: S + A - P
4583   static inline typename This::Status
4584   relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4585           const Symbol_value<size>* psymval, Mips_address address,
4586           Mips_address addend_a, bool extract_addend, bool calculate_only,
4587           Valtype* calculated_value)
4588   {
4589     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4590     Valtype addend = (extract_addend
4591                         ? elfcpp::Swap<32, big_endian>::readval(wv)
4592                         : addend_a);
4593     Valtype x = psymval->value(object, addend) - address;
4594
4595     if (calculate_only)
4596        *calculated_value = x;
4597     else
4598       elfcpp::Swap<32, big_endian>::writeval(wv, x);
4599
4600     return This::STATUS_OKAY;
4601   }
4602
4603   // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4604   static inline typename This::Status
4605   rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4606         const Symbol_value<size>* psymval, Mips_address address,
4607         bool local, Mips_address addend_a, bool extract_addend,
4608         const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4609         bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4610   {
4611     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4612     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4613
4614     Valtype addend;
4615     if (extract_addend)
4616       {
4617         if (r_type == elfcpp::R_MICROMIPS_26_S1)
4618           addend = (val & 0x03ffffff) << 1;
4619         else
4620           addend = (val & 0x03ffffff) << 2;
4621       }
4622     else
4623       addend = addend_a;
4624
4625     // Make sure the target of JALX is word-aligned.  Bit 0 must be
4626     // the correct ISA mode selector and bit 1 must be 0.
4627     if (!calculate_only && cross_mode_jump
4628         && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4629       {
4630         gold_warning(_("JALX to a non-word-aligned address"));
4631         return This::STATUS_BAD_RELOC;
4632       }
4633
4634     // Shift is 2, unusually, for microMIPS JALX.
4635     unsigned int shift =
4636         (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4637
4638     Valtype x;
4639     if (local)
4640       x = addend | ((address + 4) & (0xfc000000 << shift));
4641     else
4642       {
4643         if (shift == 1)
4644           x = Bits<27>::sign_extend32(addend);
4645         else
4646           x = Bits<28>::sign_extend32(addend);
4647       }
4648     x = psymval->value(object, x) >> shift;
4649
4650     if (!calculate_only && !local && !gsym->is_weak_undefined()
4651         && ((x >> 26) != ((address + 4) >> (26 + shift))))
4652       return This::STATUS_OVERFLOW;
4653
4654     val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4655
4656     // If required, turn JAL into JALX.
4657     if (cross_mode_jump)
4658       {
4659         bool ok;
4660         Valtype32 opcode = val >> 26;
4661         Valtype32 jalx_opcode;
4662
4663         // Check to see if the opcode is already JAL or JALX.
4664         if (r_type == elfcpp::R_MIPS16_26)
4665           {
4666             ok = (opcode == 0x6) || (opcode == 0x7);
4667             jalx_opcode = 0x7;
4668           }
4669         else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4670           {
4671             ok = (opcode == 0x3d) || (opcode == 0x3c);
4672             jalx_opcode = 0x3c;
4673           }
4674         else
4675           {
4676             ok = (opcode == 0x3) || (opcode == 0x1d);
4677             jalx_opcode = 0x1d;
4678           }
4679
4680         // If the opcode is not JAL or JALX, there's a problem.  We cannot
4681         // convert J or JALS to JALX.
4682         if (!calculate_only && !ok)
4683           {
4684             gold_error(_("Unsupported jump between ISA modes; consider "
4685                          "recompiling with interlinking enabled."));
4686             return This::STATUS_BAD_RELOC;
4687           }
4688
4689         // Make this the JALX opcode.
4690         val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4691       }
4692
4693     // Try converting JAL to BAL, if the target is in range.
4694     if (!parameters->options().relocatable()
4695         && !cross_mode_jump
4696         && ((jal_to_bal
4697             && r_type == elfcpp::R_MIPS_26
4698             && (val >> 26) == 0x3)))    // jal addr
4699       {
4700         Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4701         int offset = dest - (address + 4);
4702         if (!Bits<18>::has_overflow32(offset))
4703           {
4704             if (val == 0x03200008)   // jr t9
4705               val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff);  // b addr
4706             else
4707               val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4708           }
4709       }
4710
4711     if (calculate_only)
4712       *calculated_value = val;
4713     else
4714       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4715
4716     return This::STATUS_OKAY;
4717   }
4718
4719   // R_MIPS_PC16
4720   static inline typename This::Status
4721   relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4722           const Symbol_value<size>* psymval, Mips_address address,
4723           Mips_address addend_a, bool extract_addend, bool calculate_only,
4724           Valtype* calculated_value)
4725   {
4726     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4727     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4728
4729     Valtype addend = (extract_addend
4730                       ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4731                       : addend_a);
4732
4733     Valtype x = psymval->value(object, addend) - address;
4734     val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4735
4736     if (calculate_only)
4737       {
4738         *calculated_value = x >> 2;
4739         return This::STATUS_OKAY;
4740       }
4741     else
4742       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4743
4744     if (psymval->value(object, addend) & 3)
4745       return This::STATUS_PCREL_UNALIGNED;
4746
4747     return check_overflow<18>(x);
4748   }
4749
4750   // R_MIPS_PC21_S2
4751   static inline typename This::Status
4752   relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4753           const Symbol_value<size>* psymval, Mips_address address,
4754           Mips_address addend_a, bool extract_addend, bool calculate_only,
4755           Valtype* calculated_value)
4756   {
4757     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4758     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4759
4760     Valtype addend = (extract_addend
4761                       ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4762                       : addend_a);
4763
4764     Valtype x = psymval->value(object, addend) - address;
4765     val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4766
4767     if (calculate_only)
4768       {
4769         *calculated_value = x >> 2;
4770         return This::STATUS_OKAY;
4771       }
4772     else
4773       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4774
4775     if (psymval->value(object, addend) & 3)
4776       return This::STATUS_PCREL_UNALIGNED;
4777
4778     return check_overflow<23>(x);
4779   }
4780
4781   // R_MIPS_PC26_S2
4782   static inline typename This::Status
4783   relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4784           const Symbol_value<size>* psymval, Mips_address address,
4785           Mips_address addend_a, bool extract_addend, bool calculate_only,
4786           Valtype* calculated_value)
4787   {
4788     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4789     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4790
4791     Valtype addend = (extract_addend
4792                       ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4793                       : addend_a);
4794
4795     Valtype x = psymval->value(object, addend) - address;
4796     val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4797
4798     if (calculate_only)
4799       {
4800         *calculated_value = x >> 2;
4801         return This::STATUS_OKAY;
4802       }
4803     else
4804       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4805
4806     if (psymval->value(object, addend) & 3)
4807       return This::STATUS_PCREL_UNALIGNED;
4808
4809     return check_overflow<28>(x);
4810   }
4811
4812   // R_MIPS_PC18_S3
4813   static inline typename This::Status
4814   relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4815           const Symbol_value<size>* psymval, Mips_address address,
4816           Mips_address addend_a, bool extract_addend, bool calculate_only,
4817           Valtype* calculated_value)
4818   {
4819     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4820     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4821
4822     Valtype addend = (extract_addend
4823                       ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4824                       : addend_a);
4825
4826     Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4827     val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4828
4829     if (calculate_only)
4830       {
4831         *calculated_value = x >> 3;
4832         return This::STATUS_OKAY;
4833       }
4834     else
4835       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4836
4837     if (psymval->value(object, addend) & 7)
4838       return This::STATUS_PCREL_UNALIGNED;
4839
4840     return check_overflow<21>(x);
4841   }
4842
4843   // R_MIPS_PC19_S2
4844   static inline typename This::Status
4845   relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4846           const Symbol_value<size>* psymval, Mips_address address,
4847           Mips_address addend_a, bool extract_addend, bool calculate_only,
4848           Valtype* calculated_value)
4849   {
4850     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4851     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4852
4853     Valtype addend = (extract_addend
4854                       ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4855                       : addend_a);
4856
4857     Valtype x = psymval->value(object, addend) - address;
4858     val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4859
4860     if (calculate_only)
4861       {
4862         *calculated_value = x >> 2;
4863         return This::STATUS_OKAY;
4864       }
4865     else
4866       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4867
4868     if (psymval->value(object, addend) & 3)
4869       return This::STATUS_PCREL_UNALIGNED;
4870
4871     return check_overflow<21>(x);
4872   }
4873
4874   // R_MIPS_PCHI16
4875   static inline typename This::Status
4876   relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4877             const Symbol_value<size>* psymval, Mips_address addend,
4878             Mips_address address, unsigned int r_sym, bool extract_addend)
4879   {
4880     // Record the relocation.  It will be resolved when we find pclo16 part.
4881     pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4882                             addend, 0, r_sym, extract_addend, address));
4883     return This::STATUS_OKAY;
4884   }
4885
4886   // R_MIPS_PCHI16
4887   static inline typename This::Status
4888   do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4889              const Symbol_value<size>* psymval, Mips_address addend_hi,
4890              Mips_address address, bool extract_addend, Valtype32 addend_lo,
4891              bool calculate_only, Valtype* calculated_value)
4892   {
4893     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4894     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4895
4896     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4897                                        : addend_hi);
4898
4899     Valtype value = psymval->value(object, addend) - address;
4900     Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4901     val = Bits<32>::bit_select32(val, x, 0xffff);
4902
4903     if (calculate_only)
4904       *calculated_value = x;
4905     else
4906       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4907
4908     return This::STATUS_OKAY;
4909   }
4910
4911   // R_MIPS_PCLO16
4912   static inline typename This::Status
4913   relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4914             const Symbol_value<size>* psymval, Mips_address addend_a,
4915             bool extract_addend, Mips_address address, unsigned int r_sym,
4916             unsigned int rel_type, bool calculate_only,
4917             Valtype* calculated_value)
4918   {
4919     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4920     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4921
4922     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4923                                      : addend_a);
4924
4925     if (rel_type == elfcpp::SHT_REL)
4926       {
4927         // Resolve pending R_MIPS_PCHI16 relocations.
4928         typename std::list<reloc_high<size, big_endian> >::iterator it =
4929             pchi16_relocs.begin();
4930         while (it != pchi16_relocs.end())
4931           {
4932             reloc_high<size, big_endian> pchi16 = *it;
4933             if (pchi16.r_sym == r_sym)
4934               {
4935                 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4936                              pchi16.addend, pchi16.address,
4937                              pchi16.extract_addend, addend, calculate_only,
4938                              calculated_value);
4939                 it = pchi16_relocs.erase(it);
4940               }
4941             else
4942               ++it;
4943           }
4944       }
4945
4946     // Resolve R_MIPS_PCLO16 relocation.
4947     Valtype x = psymval->value(object, addend) - address;
4948     val = Bits<32>::bit_select32(val, x, 0xffff);
4949
4950     if (calculate_only)
4951       *calculated_value = x;
4952     else
4953       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4954
4955     return This::STATUS_OKAY;
4956   }
4957
4958   // R_MICROMIPS_PC7_S1
4959   static inline typename This::Status
4960   relmicromips_pc7_s1(unsigned char* view,
4961                       const Mips_relobj<size, big_endian>* object,
4962                       const Symbol_value<size>* psymval, Mips_address address,
4963                       Mips_address addend_a, bool extract_addend,
4964                       bool calculate_only, Valtype* calculated_value)
4965   {
4966     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4967     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4968
4969     Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4970                                     : addend_a;
4971
4972     Valtype x = psymval->value(object, addend) - address;
4973     val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4974
4975     if (calculate_only)
4976       {
4977         *calculated_value = x >> 1;
4978         return This::STATUS_OKAY;
4979       }
4980     else
4981       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4982
4983     return check_overflow<8>(x);
4984   }
4985
4986   // R_MICROMIPS_PC10_S1
4987   static inline typename This::Status
4988   relmicromips_pc10_s1(unsigned char* view,
4989                        const Mips_relobj<size, big_endian>* object,
4990                        const Symbol_value<size>* psymval, Mips_address address,
4991                        Mips_address addend_a, bool extract_addend,
4992                        bool calculate_only, Valtype* calculated_value)
4993   {
4994     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4995     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4996
4997     Valtype addend = (extract_addend
4998                       ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4999                       : addend_a);
5000
5001     Valtype x = psymval->value(object, addend) - address;
5002     val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
5003
5004     if (calculate_only)
5005       {
5006         *calculated_value = x >> 1;
5007         return This::STATUS_OKAY;
5008       }
5009     else
5010       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5011
5012     return check_overflow<11>(x);
5013   }
5014
5015   // R_MICROMIPS_PC16_S1
5016   static inline typename This::Status
5017   relmicromips_pc16_s1(unsigned char* view,
5018                        const Mips_relobj<size, big_endian>* object,
5019                        const Symbol_value<size>* psymval, Mips_address address,
5020                        Mips_address addend_a, bool extract_addend,
5021                        bool calculate_only, Valtype* calculated_value)
5022   {
5023     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5024     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5025
5026     Valtype addend = (extract_addend
5027                       ? Bits<17>::sign_extend32((val & 0xffff) << 1)
5028                       : addend_a);
5029
5030     Valtype x = psymval->value(object, addend) - address;
5031     val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
5032
5033     if (calculate_only)
5034       {
5035         *calculated_value = x >> 1;
5036         return This::STATUS_OKAY;
5037       }
5038     else
5039       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5040
5041     return check_overflow<17>(x);
5042   }
5043
5044   // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5045   static inline typename This::Status
5046   relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5047           const Symbol_value<size>* psymval, Mips_address addend,
5048           Mips_address address, bool gp_disp, unsigned int r_type,
5049           unsigned int r_sym, bool extract_addend)
5050   {
5051     // Record the relocation.  It will be resolved when we find lo16 part.
5052     hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5053                           addend, r_type, r_sym, extract_addend, address,
5054                           gp_disp));
5055     return This::STATUS_OKAY;
5056   }
5057
5058   // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5059   static inline typename This::Status
5060   do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5061              const Symbol_value<size>* psymval, Mips_address addend_hi,
5062              Mips_address address, bool is_gp_disp, unsigned int r_type,
5063              bool extract_addend, Valtype32 addend_lo,
5064              Target_mips<size, big_endian>* target, bool calculate_only,
5065              Valtype* calculated_value)
5066   {
5067     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5068     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5069
5070     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5071                                        : addend_hi);
5072
5073     Valtype32 value;
5074     if (!is_gp_disp)
5075       value = psymval->value(object, addend);
5076     else
5077       {
5078         // For MIPS16 ABI code we generate this sequence
5079         //    0: li      $v0,%hi(_gp_disp)
5080         //    4: addiupc $v1,%lo(_gp_disp)
5081         //    8: sll     $v0,16
5082         //   12: addu    $v0,$v1
5083         //   14: move    $gp,$v0
5084         // So the offsets of hi and lo relocs are the same, but the
5085         // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5086         // ADDIUPC clears the low two bits of the instruction address,
5087         // so the base is ($t9 + 4) & ~3.
5088         Valtype32 gp_disp;
5089         if (r_type == elfcpp::R_MIPS16_HI16)
5090           gp_disp = (target->adjusted_gp_value(object)
5091                      - ((address + 4) & ~0x3));
5092         // The microMIPS .cpload sequence uses the same assembly
5093         // instructions as the traditional psABI version, but the
5094         // incoming $t9 has the low bit set.
5095         else if (r_type == elfcpp::R_MICROMIPS_HI16)
5096           gp_disp = target->adjusted_gp_value(object) - address - 1;
5097         else
5098           gp_disp = target->adjusted_gp_value(object) - address;
5099         value = gp_disp + addend;
5100       }
5101     Valtype x = ((value + 0x8000) >> 16) & 0xffff;
5102     val = Bits<32>::bit_select32(val, x, 0xffff);
5103
5104     if (calculate_only)
5105       {
5106         *calculated_value = x;
5107         return This::STATUS_OKAY;
5108       }
5109     else
5110       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5111
5112     return (is_gp_disp ? check_overflow<16>(x)
5113                        : This::STATUS_OKAY);
5114   }
5115
5116   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5117   static inline typename This::Status
5118   relgot16_local(unsigned char* view,
5119                  const Mips_relobj<size, big_endian>* object,
5120                  const Symbol_value<size>* psymval, Mips_address addend_a,
5121                  bool extract_addend, unsigned int r_type, unsigned int r_sym)
5122   {
5123     // Record the relocation.  It will be resolved when we find lo16 part.
5124     got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5125                            addend_a, r_type, r_sym, extract_addend));
5126     return This::STATUS_OKAY;
5127   }
5128
5129   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5130   static inline typename This::Status
5131   do_relgot16_local(unsigned char* view,
5132                     const Mips_relobj<size, big_endian>* object,
5133                     const Symbol_value<size>* psymval, Mips_address addend_hi,
5134                     bool extract_addend, Valtype32 addend_lo,
5135                     Target_mips<size, big_endian>* target, bool calculate_only,
5136                     Valtype* calculated_value)
5137   {
5138     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5139     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5140
5141     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5142                                        : addend_hi);
5143
5144     // Find GOT page entry.
5145     Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5146                           & 0xffff;
5147     value <<= 16;
5148     unsigned int got_offset =
5149       target->got_section()->get_got_page_offset(value, object);
5150
5151     // Resolve the relocation.
5152     Valtype x = target->got_section()->gp_offset(got_offset, object);
5153     val = Bits<32>::bit_select32(val, x, 0xffff);
5154
5155     if (calculate_only)
5156       {
5157         *calculated_value = x;
5158         return This::STATUS_OKAY;
5159       }
5160     else
5161       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5162
5163     return check_overflow<16>(x);
5164   }
5165
5166   // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5167   static inline typename This::Status
5168   rello16(Target_mips<size, big_endian>* target, unsigned char* view,
5169           const Mips_relobj<size, big_endian>* object,
5170           const Symbol_value<size>* psymval, Mips_address addend_a,
5171           bool extract_addend, Mips_address address, bool is_gp_disp,
5172           unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
5173           bool calculate_only, Valtype* calculated_value)
5174   {
5175     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5176     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5177
5178     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5179                                      : addend_a);
5180
5181     if (rel_type == elfcpp::SHT_REL)
5182       {
5183         typename This::Status reloc_status = This::STATUS_OKAY;
5184         // Resolve pending R_MIPS_HI16 relocations.
5185         typename std::list<reloc_high<size, big_endian> >::iterator it =
5186           hi16_relocs.begin();
5187         while (it != hi16_relocs.end())
5188           {
5189             reloc_high<size, big_endian> hi16 = *it;
5190             if (hi16.r_sym == r_sym
5191                 && is_matching_lo16_reloc(hi16.r_type, r_type))
5192               {
5193                 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
5194                 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
5195                                        hi16.addend, hi16.address, hi16.gp_disp,
5196                                        hi16.r_type, hi16.extract_addend, addend,
5197                                        target, calculate_only, calculated_value);
5198                 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
5199                 if (reloc_status == This::STATUS_OVERFLOW)
5200                   return This::STATUS_OVERFLOW;
5201                 it = hi16_relocs.erase(it);
5202               }
5203             else
5204               ++it;
5205           }
5206
5207         // Resolve pending local R_MIPS_GOT16 relocations.
5208         typename std::list<reloc_high<size, big_endian> >::iterator it2 =
5209           got16_relocs.begin();
5210         while (it2 != got16_relocs.end())
5211           {
5212             reloc_high<size, big_endian> got16 = *it2;
5213             if (got16.r_sym == r_sym
5214                 && is_matching_lo16_reloc(got16.r_type, r_type))
5215               {
5216                 mips_reloc_unshuffle(got16.view, got16.r_type, false);
5217
5218                 reloc_status = do_relgot16_local(got16.view, got16.object,
5219                                      got16.psymval, got16.addend,
5220                                      got16.extract_addend, addend, target,
5221                                      calculate_only, calculated_value);
5222
5223                 mips_reloc_shuffle(got16.view, got16.r_type, false);
5224                 if (reloc_status == This::STATUS_OVERFLOW)
5225                   return This::STATUS_OVERFLOW;
5226                 it2 = got16_relocs.erase(it2);
5227               }
5228             else
5229               ++it2;
5230           }
5231       }
5232
5233     // Resolve R_MIPS_LO16 relocation.
5234     Valtype x;
5235     if (!is_gp_disp)
5236       x = psymval->value(object, addend);
5237     else
5238       {
5239         // See the comment for R_MIPS16_HI16 above for the reason
5240         // for this conditional.
5241         Valtype32 gp_disp;
5242         if (r_type == elfcpp::R_MIPS16_LO16)
5243           gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
5244         else if (r_type == elfcpp::R_MICROMIPS_LO16
5245                  || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
5246           gp_disp = target->adjusted_gp_value(object) - address + 3;
5247         else
5248           gp_disp = target->adjusted_gp_value(object) - address + 4;
5249         // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5250         // for overflow.  Relocations against _gp_disp are normally
5251         // generated from the .cpload pseudo-op.  It generates code
5252         // that normally looks like this:
5253
5254         //   lui    $gp,%hi(_gp_disp)
5255         //   addiu  $gp,$gp,%lo(_gp_disp)
5256         //   addu   $gp,$gp,$t9
5257
5258         // Here $t9 holds the address of the function being called,
5259         // as required by the MIPS ELF ABI.  The R_MIPS_LO16
5260         // relocation can easily overflow in this situation, but the
5261         // R_MIPS_HI16 relocation will handle the overflow.
5262         // Therefore, we consider this a bug in the MIPS ABI, and do
5263         // not check for overflow here.
5264         x = gp_disp + addend;
5265       }
5266     val = Bits<32>::bit_select32(val, x, 0xffff);
5267
5268     if (calculate_only)
5269       *calculated_value = x;
5270     else
5271       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5272
5273     return This::STATUS_OKAY;
5274   }
5275
5276   // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5277   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5278   // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5279   // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5280   // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5281   // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5282   static inline typename This::Status
5283   relgot(unsigned char* view, int gp_offset, bool calculate_only,
5284          Valtype* calculated_value)
5285   {
5286     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5287     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5288     Valtype x = gp_offset;
5289     val = Bits<32>::bit_select32(val, x, 0xffff);
5290
5291     if (calculate_only)
5292       {
5293         *calculated_value = x;
5294         return This::STATUS_OKAY;
5295       }
5296     else
5297       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5298
5299     return check_overflow<16>(x);
5300   }
5301
5302   // R_MIPS_EH
5303   static inline typename This::Status
5304   releh(unsigned char* view, int gp_offset, bool calculate_only,
5305         Valtype* calculated_value)
5306   {
5307     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5308     Valtype x = gp_offset;
5309
5310     if (calculate_only)
5311       {
5312         *calculated_value = x;
5313         return This::STATUS_OKAY;
5314       }
5315     else
5316       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5317
5318     return check_overflow<32>(x);
5319   }
5320
5321   // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5322   static inline typename This::Status
5323   relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5324              const Mips_relobj<size, big_endian>* object,
5325              const Symbol_value<size>* psymval, Mips_address addend_a,
5326              bool extract_addend, bool calculate_only,
5327              Valtype* calculated_value)
5328   {
5329     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5330     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5331     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5332
5333     // Find a GOT page entry that points to within 32KB of symbol + addend.
5334     Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5335     unsigned int  got_offset =
5336       target->got_section()->get_got_page_offset(value, object);
5337
5338     Valtype x = target->got_section()->gp_offset(got_offset, object);
5339     val = Bits<32>::bit_select32(val, x, 0xffff);
5340
5341     if (calculate_only)
5342       {
5343         *calculated_value = x;
5344         return This::STATUS_OKAY;
5345       }
5346     else
5347       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5348
5349     return check_overflow<16>(x);
5350   }
5351
5352   // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5353   static inline typename This::Status
5354   relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5355              const Mips_relobj<size, big_endian>* object,
5356              const Symbol_value<size>* psymval, Mips_address addend_a,
5357              bool extract_addend, bool local, bool calculate_only,
5358              Valtype* calculated_value)
5359   {
5360     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5361     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5362     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5363
5364     // For a local symbol, find a GOT page entry that points to within 32KB of
5365     // symbol + addend.  Relocation value is the offset of the GOT page entry's
5366     // value from symbol + addend.
5367     // For a global symbol, relocation value is addend.
5368     Valtype x;
5369     if (local)
5370       {
5371         // Find GOT page entry.
5372         Mips_address value = ((psymval->value(object, addend) + 0x8000)
5373                               & ~0xffff);
5374         target->got_section()->get_got_page_offset(value, object);
5375
5376         x = psymval->value(object, addend) - value;
5377       }
5378     else
5379       x = addend;
5380     val = Bits<32>::bit_select32(val, x, 0xffff);
5381
5382     if (calculate_only)
5383       {
5384         *calculated_value = x;
5385         return This::STATUS_OKAY;
5386       }
5387     else
5388       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5389
5390     return check_overflow<16>(x);
5391   }
5392
5393   // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5394   // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5395   static inline typename This::Status
5396   relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5397               Valtype* calculated_value)
5398   {
5399     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5400     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5401     Valtype x = gp_offset;
5402     x = ((x + 0x8000) >> 16) & 0xffff;
5403     val = Bits<32>::bit_select32(val, x, 0xffff);
5404
5405     if (calculate_only)
5406       *calculated_value = x;
5407     else
5408       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5409
5410     return This::STATUS_OKAY;
5411   }
5412
5413   // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5414   // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5415   static inline typename This::Status
5416   relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5417               Valtype* calculated_value)
5418   {
5419     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5420     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5421     Valtype x = gp_offset;
5422     val = Bits<32>::bit_select32(val, x, 0xffff);
5423
5424     if (calculate_only)
5425       *calculated_value = x;
5426     else
5427       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5428
5429     return This::STATUS_OKAY;
5430   }
5431
5432   // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5433   // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5434   static inline typename This::Status
5435   relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5436            const Symbol_value<size>* psymval, Mips_address gp,
5437            Mips_address addend_a, bool extract_addend, bool local,
5438            unsigned int r_type, bool calculate_only,
5439            Valtype* calculated_value)
5440   {
5441     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5442     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5443
5444     Valtype addend;
5445     if (extract_addend)
5446       {
5447         if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5448           addend = (val & 0x7f) << 2;
5449         else
5450           addend = val & 0xffff;
5451         // Only sign-extend the addend if it was extracted from the
5452         // instruction.  If the addend was separate, leave it alone,
5453         // otherwise we may lose significant bits.
5454         addend = Bits<16>::sign_extend32(addend);
5455       }
5456     else
5457       addend = addend_a;
5458
5459     Valtype x = psymval->value(object, addend) - gp;
5460
5461     // If the symbol was local, any earlier relocatable links will
5462     // have adjusted its addend with the gp offset, so compensate
5463     // for that now.  Don't do it for symbols forced local in this
5464     // link, though, since they won't have had the gp offset applied
5465     // to them before.
5466     if (local)
5467       x += object->gp_value();
5468
5469     if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5470       val = Bits<32>::bit_select32(val, x, 0x7f);
5471     else
5472       val = Bits<32>::bit_select32(val, x, 0xffff);
5473
5474     if (calculate_only)
5475       {
5476         *calculated_value = x;
5477         return This::STATUS_OKAY;
5478       }
5479     else
5480       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5481
5482     if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5483       {
5484         gold_error(_("small-data section exceeds 64KB; lower small-data size "
5485                      "limit (see option -G)"));
5486         return This::STATUS_OVERFLOW;
5487       }
5488     return This::STATUS_OKAY;
5489   }
5490
5491   // R_MIPS_GPREL32
5492   static inline typename This::Status
5493   relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5494              const Symbol_value<size>* psymval, Mips_address gp,
5495              Mips_address addend_a, bool extract_addend, bool calculate_only,
5496              Valtype* calculated_value)
5497   {
5498     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5499     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5500     Valtype addend = extract_addend ? val : addend_a;
5501
5502     // R_MIPS_GPREL32 relocations are defined for local symbols only.
5503     Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5504
5505     if (calculate_only)
5506       *calculated_value = x;
5507     else
5508       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5509
5510     return This::STATUS_OKAY;
5511  }
5512
5513   // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5514   // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5515   // R_MICROMIPS_TLS_DTPREL_HI16
5516   static inline typename This::Status
5517   tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5518              const Symbol_value<size>* psymval, Valtype32 tp_offset,
5519              Mips_address addend_a, bool extract_addend, bool calculate_only,
5520              Valtype* calculated_value)
5521   {
5522     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5523     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5524     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5525
5526     // tls symbol values are relative to tls_segment()->vaddr()
5527     Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5528     val = Bits<32>::bit_select32(val, x, 0xffff);
5529
5530     if (calculate_only)
5531       *calculated_value = x;
5532     else
5533       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5534
5535     return This::STATUS_OKAY;
5536   }
5537
5538   // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5539   // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5540   // R_MICROMIPS_TLS_DTPREL_LO16,
5541   static inline typename This::Status
5542   tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5543              const Symbol_value<size>* psymval, Valtype32 tp_offset,
5544              Mips_address addend_a, bool extract_addend, bool calculate_only,
5545              Valtype* calculated_value)
5546   {
5547     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5548     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5549     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5550
5551     // tls symbol values are relative to tls_segment()->vaddr()
5552     Valtype x = psymval->value(object, addend) - tp_offset;
5553     val = Bits<32>::bit_select32(val, x, 0xffff);
5554
5555     if (calculate_only)
5556       *calculated_value = x;
5557     else
5558       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5559
5560     return This::STATUS_OKAY;
5561   }
5562
5563   // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5564   // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5565   static inline typename This::Status
5566   tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5567            const Symbol_value<size>* psymval, Valtype32 tp_offset,
5568            Mips_address addend_a, bool extract_addend, bool calculate_only,
5569            Valtype* calculated_value)
5570   {
5571     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5572     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5573     Valtype addend = extract_addend ? val : addend_a;
5574
5575     // tls symbol values are relative to tls_segment()->vaddr()
5576     Valtype x = psymval->value(object, addend) - tp_offset;
5577
5578     if (calculate_only)
5579       *calculated_value = x;
5580     else
5581       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5582
5583     return This::STATUS_OKAY;
5584   }
5585
5586   // R_MIPS_SUB, R_MICROMIPS_SUB
5587   static inline typename This::Status
5588   relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5589          const Symbol_value<size>* psymval, Mips_address addend_a,
5590          bool extract_addend, bool calculate_only, Valtype* calculated_value)
5591   {
5592     Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5593     Valtype64 addend = (extract_addend
5594                         ? elfcpp::Swap<64, big_endian>::readval(wv)
5595                         : addend_a);
5596
5597     Valtype64 x = psymval->value(object, -addend);
5598     if (calculate_only)
5599       *calculated_value = x;
5600     else
5601       elfcpp::Swap<64, big_endian>::writeval(wv, x);
5602
5603     return This::STATUS_OKAY;
5604   }
5605
5606   // R_MIPS_64: S + A
5607   static inline typename This::Status
5608   rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5609         const Symbol_value<size>* psymval, Mips_address addend_a,
5610         bool extract_addend, bool calculate_only, Valtype* calculated_value,
5611         bool apply_addend_only)
5612   {
5613     Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5614     Valtype64 addend = (extract_addend
5615                         ? elfcpp::Swap<64, big_endian>::readval(wv)
5616                         : addend_a);
5617
5618     Valtype64 x = psymval->value(object, addend);
5619     if (calculate_only)
5620       *calculated_value = x;
5621     else
5622       {
5623         if (apply_addend_only)
5624           x = addend;
5625         elfcpp::Swap<64, big_endian>::writeval(wv, x);
5626       }
5627
5628     return This::STATUS_OKAY;
5629   }
5630
5631   // R_MIPS_HIGHER, R_MICROMIPS_HIGHER
5632   static inline typename This::Status
5633   relhigher(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5634             const Symbol_value<size>* psymval, Mips_address addend_a,
5635             bool extract_addend, bool calculate_only, Valtype* calculated_value)
5636   {
5637     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5638     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5639     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5640                                      : addend_a);
5641
5642     Valtype x = psymval->value(object, addend);
5643     x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
5644     val = Bits<32>::bit_select32(val, x, 0xffff);
5645
5646     if (calculate_only)
5647       *calculated_value = x;
5648     else
5649       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5650
5651     return This::STATUS_OKAY;
5652   }
5653
5654   // R_MIPS_HIGHEST, R_MICROMIPS_HIGHEST
5655   static inline typename This::Status
5656   relhighest(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5657              const Symbol_value<size>* psymval, Mips_address addend_a,
5658              bool extract_addend, bool calculate_only,
5659              Valtype* calculated_value)
5660   {
5661     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5662     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5663     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5664                                      : addend_a);
5665
5666     Valtype x = psymval->value(object, addend);
5667     x = ((x + (uint64_t) 0x800080008000llu) >> 48) & 0xffff;
5668     val = Bits<32>::bit_select32(val, x, 0xffff);
5669
5670     if (calculate_only)
5671       *calculated_value = x;
5672     else
5673       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5674
5675     return This::STATUS_OKAY;
5676   }
5677 };
5678
5679 template<int size, bool big_endian>
5680 typename std::list<reloc_high<size, big_endian> >
5681     Mips_relocate_functions<size, big_endian>::hi16_relocs;
5682
5683 template<int size, bool big_endian>
5684 typename std::list<reloc_high<size, big_endian> >
5685     Mips_relocate_functions<size, big_endian>::got16_relocs;
5686
5687 template<int size, bool big_endian>
5688 typename std::list<reloc_high<size, big_endian> >
5689     Mips_relocate_functions<size, big_endian>::pchi16_relocs;
5690
5691 // Mips_got_info methods.
5692
5693 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5694 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5695
5696 template<int size, bool big_endian>
5697 void
5698 Mips_got_info<size, big_endian>::record_local_got_symbol(
5699     Mips_relobj<size, big_endian>* object, unsigned int symndx,
5700     Mips_address addend, unsigned int r_type, unsigned int shndx,
5701     bool is_section_symbol)
5702 {
5703   Mips_got_entry<size, big_endian>* entry =
5704     new Mips_got_entry<size, big_endian>(object, symndx, addend,
5705                                          mips_elf_reloc_tls_type(r_type),
5706                                          shndx, is_section_symbol);
5707   this->record_got_entry(entry, object);
5708 }
5709
5710 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5711 // in OBJECT.  FOR_CALL is true if the caller is only interested in
5712 // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
5713 // relocation.
5714
5715 template<int size, bool big_endian>
5716 void
5717 Mips_got_info<size, big_endian>::record_global_got_symbol(
5718     Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5719     unsigned int r_type, bool dyn_reloc, bool for_call)
5720 {
5721   if (!for_call)
5722     mips_sym->set_got_not_only_for_calls();
5723
5724   // A global symbol in the GOT must also be in the dynamic symbol table.
5725   if (!mips_sym->needs_dynsym_entry() && !mips_sym->is_forced_local())
5726     {
5727       switch (mips_sym->visibility())
5728         {
5729         case elfcpp::STV_INTERNAL:
5730         case elfcpp::STV_HIDDEN:
5731           mips_sym->set_is_forced_local();
5732           break;
5733         default:
5734           mips_sym->set_needs_dynsym_entry();
5735           break;
5736         }
5737     }
5738
5739   unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5740   if (tls_type == GOT_TLS_NONE)
5741     this->global_got_symbols_.insert(mips_sym);
5742
5743   if (dyn_reloc)
5744     {
5745       if (mips_sym->global_got_area() == GGA_NONE)
5746         mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5747       return;
5748     }
5749
5750   Mips_got_entry<size, big_endian>* entry =
5751     new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5752
5753   this->record_got_entry(entry, object);
5754 }
5755
5756 // Add ENTRY to master GOT and to OBJECT's GOT.
5757
5758 template<int size, bool big_endian>
5759 void
5760 Mips_got_info<size, big_endian>::record_got_entry(
5761     Mips_got_entry<size, big_endian>* entry,
5762     Mips_relobj<size, big_endian>* object)
5763 {
5764   this->got_entries_.insert(entry);
5765
5766   // Create the GOT entry for the OBJECT's GOT.
5767   Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5768   Mips_got_entry<size, big_endian>* entry2 =
5769     new Mips_got_entry<size, big_endian>(*entry);
5770
5771   g->got_entries_.insert(entry2);
5772 }
5773
5774 // Record that OBJECT has a page relocation against symbol SYMNDX and
5775 // that ADDEND is the addend for that relocation.
5776 // This function creates an upper bound on the number of GOT slots
5777 // required; no attempt is made to combine references to non-overridable
5778 // global symbols across multiple input files.
5779
5780 template<int size, bool big_endian>
5781 void
5782 Mips_got_info<size, big_endian>::record_got_page_entry(
5783     Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5784 {
5785   struct Got_page_range **range_ptr, *range;
5786   int old_pages, new_pages;
5787
5788   // Find the Got_page_entry for this symbol.
5789   Got_page_entry* entry = new Got_page_entry(object, symndx);
5790   typename Got_page_entry_set::iterator it =
5791     this->got_page_entries_.find(entry);
5792   if (it != this->got_page_entries_.end())
5793     entry = *it;
5794   else
5795     this->got_page_entries_.insert(entry);
5796
5797   // Get the object's GOT, but we don't need to insert an entry here.
5798   Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5799
5800   // Skip over ranges whose maximum extent cannot share a page entry
5801   // with ADDEND.
5802   range_ptr = &entry->ranges;
5803   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5804     range_ptr = &(*range_ptr)->next;
5805
5806   // If we scanned to the end of the list, or found a range whose
5807   // minimum extent cannot share a page entry with ADDEND, create
5808   // a new singleton range.
5809   range = *range_ptr;
5810   if (!range || addend < range->min_addend - 0xffff)
5811     {
5812       range = new Got_page_range();
5813       range->next = *range_ptr;
5814       range->min_addend = addend;
5815       range->max_addend = addend;
5816
5817       *range_ptr = range;
5818       ++this->page_gotno_;
5819       ++g2->page_gotno_;
5820       return;
5821     }
5822
5823   // Remember how many pages the old range contributed.
5824   old_pages = range->get_max_pages();
5825
5826   // Update the ranges.
5827   if (addend < range->min_addend)
5828     range->min_addend = addend;
5829   else if (addend > range->max_addend)
5830     {
5831       if (range->next && addend >= range->next->min_addend - 0xffff)
5832         {
5833           old_pages += range->next->get_max_pages();
5834           range->max_addend = range->next->max_addend;
5835           range->next = range->next->next;
5836         }
5837       else
5838         range->max_addend = addend;
5839     }
5840
5841   // Record any change in the total estimate.
5842   new_pages = range->get_max_pages();
5843   if (old_pages != new_pages)
5844     {
5845       this->page_gotno_ += new_pages - old_pages;
5846       g2->page_gotno_ += new_pages - old_pages;
5847     }
5848 }
5849
5850 // Create all entries that should be in the local part of the GOT.
5851
5852 template<int size, bool big_endian>
5853 void
5854 Mips_got_info<size, big_endian>::add_local_entries(
5855     Target_mips<size, big_endian>* target, Layout* layout)
5856 {
5857   Mips_output_data_got<size, big_endian>* got = target->got_section();
5858   // First two GOT entries are reserved.  The first entry will be filled at
5859   // runtime.  The second entry will be used by some runtime loaders.
5860   got->add_constant(0);
5861   got->add_constant(target->mips_elf_gnu_got1_mask());
5862
5863   for (typename Got_entry_set::iterator
5864        p = this->got_entries_.begin();
5865        p != this->got_entries_.end();
5866        ++p)
5867     {
5868       Mips_got_entry<size, big_endian>* entry = *p;
5869       if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5870         {
5871           got->add_local(entry->object(), entry->symndx(),
5872                          GOT_TYPE_STANDARD, entry->addend());
5873           unsigned int got_offset = entry->object()->local_got_offset(
5874               entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5875           if (got->multi_got() && this->index_ > 0
5876               && parameters->options().output_is_position_independent())
5877           {
5878             if (!entry->is_section_symbol())
5879               target->rel_dyn_section(layout)->add_local(entry->object(),
5880                   entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5881             else
5882               target->rel_dyn_section(layout)->add_symbolless_local_addend(
5883                   entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5884                   got, got_offset);
5885           }
5886         }
5887     }
5888
5889   this->add_page_entries(target, layout);
5890
5891   // Add global entries that should be in the local area.
5892   for (typename Got_entry_set::iterator
5893        p = this->got_entries_.begin();
5894        p != this->got_entries_.end();
5895        ++p)
5896     {
5897       Mips_got_entry<size, big_endian>* entry = *p;
5898       if (!entry->is_for_global_symbol())
5899         continue;
5900
5901       Mips_symbol<size>* mips_sym = entry->sym();
5902       if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5903         {
5904           unsigned int got_type;
5905           if (!got->multi_got())
5906             got_type = GOT_TYPE_STANDARD;
5907           else
5908             got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5909           if (got->add_global(mips_sym, got_type))
5910             {
5911               mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5912               if (got->multi_got() && this->index_ > 0
5913                   && parameters->options().output_is_position_independent())
5914                 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5915                     mips_sym, elfcpp::R_MIPS_REL32, got,
5916                     mips_sym->got_offset(got_type));
5917             }
5918         }
5919     }
5920 }
5921
5922 // Create GOT page entries.
5923
5924 template<int size, bool big_endian>
5925 void
5926 Mips_got_info<size, big_endian>::add_page_entries(
5927     Target_mips<size, big_endian>* target, Layout* layout)
5928 {
5929   if (this->page_gotno_ == 0)
5930     return;
5931
5932   Mips_output_data_got<size, big_endian>* got = target->got_section();
5933   this->got_page_offset_start_ = got->add_constant(0);
5934   if (got->multi_got() && this->index_ > 0
5935       && parameters->options().output_is_position_independent())
5936     target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5937                                                   this->got_page_offset_start_);
5938   int num_entries = this->page_gotno_;
5939   unsigned int prev_offset = this->got_page_offset_start_;
5940   while (--num_entries > 0)
5941     {
5942       unsigned int next_offset = got->add_constant(0);
5943       if (got->multi_got() && this->index_ > 0
5944           && parameters->options().output_is_position_independent())
5945         target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5946                                                       next_offset);
5947       gold_assert(next_offset == prev_offset + size/8);
5948       prev_offset = next_offset;
5949     }
5950   this->got_page_offset_next_ = this->got_page_offset_start_;
5951 }
5952
5953 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5954
5955 template<int size, bool big_endian>
5956 void
5957 Mips_got_info<size, big_endian>::add_global_entries(
5958     Target_mips<size, big_endian>* target, Layout* layout,
5959     unsigned int non_reloc_only_global_gotno)
5960 {
5961   Mips_output_data_got<size, big_endian>* got = target->got_section();
5962   // Add GGA_NORMAL entries.
5963   unsigned int count = 0;
5964   for (typename Got_entry_set::iterator
5965        p = this->got_entries_.begin();
5966        p != this->got_entries_.end();
5967        ++p)
5968     {
5969       Mips_got_entry<size, big_endian>* entry = *p;
5970       if (!entry->is_for_global_symbol())
5971         continue;
5972
5973       Mips_symbol<size>* mips_sym = entry->sym();
5974       if (mips_sym->global_got_area() != GGA_NORMAL)
5975         continue;
5976
5977       unsigned int got_type;
5978       if (!got->multi_got())
5979         got_type = GOT_TYPE_STANDARD;
5980       else
5981         // In multi-GOT links, global symbol can be in both primary and
5982         // secondary GOT(s).  By creating custom GOT type
5983         // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5984         // is added to secondary GOT(s).
5985         got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5986       if (!got->add_global(mips_sym, got_type))
5987         continue;
5988
5989       mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5990       if (got->multi_got() && this->index_ == 0)
5991         count++;
5992       if (got->multi_got() && this->index_ > 0)
5993         {
5994           if (parameters->options().output_is_position_independent()
5995               || (!parameters->doing_static_link()
5996                   && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
5997             {
5998               target->rel_dyn_section(layout)->add_global(
5999                   mips_sym, elfcpp::R_MIPS_REL32, got,
6000                   mips_sym->got_offset(got_type));
6001               got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
6002                                            elfcpp::R_MIPS_REL32, mips_sym);
6003             }
6004         }
6005     }
6006
6007   if (!got->multi_got() || this->index_ == 0)
6008     {
6009       if (got->multi_got())
6010         {
6011           // We need to allocate space in the primary GOT for GGA_NORMAL entries
6012           // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
6013           // entries correspond to dynamic symbol indexes.
6014           while (count < non_reloc_only_global_gotno)
6015             {
6016               got->add_constant(0);
6017               ++count;
6018             }
6019         }
6020
6021       // Add GGA_RELOC_ONLY entries.
6022       got->add_reloc_only_entries();
6023     }
6024 }
6025
6026 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6027
6028 template<int size, bool big_endian>
6029 void
6030 Mips_got_info<size, big_endian>::add_reloc_only_entries(
6031     Mips_output_data_got<size, big_endian>* got)
6032 {
6033   for (typename Global_got_entry_set::iterator
6034        p = this->global_got_symbols_.begin();
6035        p != this->global_got_symbols_.end();
6036        ++p)
6037     {
6038       Mips_symbol<size>* mips_sym = *p;
6039       if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6040         {
6041           unsigned int got_type;
6042           if (!got->multi_got())
6043             got_type = GOT_TYPE_STANDARD;
6044           else
6045             got_type = GOT_TYPE_STANDARD_MULTIGOT;
6046           if (got->add_global(mips_sym, got_type))
6047             mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6048         }
6049     }
6050 }
6051
6052 // Create TLS GOT entries.
6053
6054 template<int size, bool big_endian>
6055 void
6056 Mips_got_info<size, big_endian>::add_tls_entries(
6057     Target_mips<size, big_endian>* target, Layout* layout)
6058 {
6059   Mips_output_data_got<size, big_endian>* got = target->got_section();
6060   // Add local tls entries.
6061   for (typename Got_entry_set::iterator
6062        p = this->got_entries_.begin();
6063        p != this->got_entries_.end();
6064        ++p)
6065     {
6066       Mips_got_entry<size, big_endian>* entry = *p;
6067       if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6068         continue;
6069
6070       if (entry->tls_type() == GOT_TLS_GD)
6071         {
6072           unsigned int got_type = GOT_TYPE_TLS_PAIR;
6073           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6074                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6075           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6076                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6077
6078           if (!parameters->doing_static_link())
6079             {
6080               got->add_local_pair_with_rel(entry->object(), entry->symndx(),
6081                                            entry->shndx(), got_type,
6082                                            target->rel_dyn_section(layout),
6083                                            r_type1, entry->addend());
6084               unsigned int got_offset =
6085                 entry->object()->local_got_offset(entry->symndx(), got_type,
6086                                                   entry->addend());
6087               got->add_static_reloc(got_offset + size/8, r_type2,
6088                                     entry->object(), entry->symndx());
6089             }
6090           else
6091             {
6092               // We are doing a static link.  Mark it as belong to module 1,
6093               // the executable.
6094               unsigned int got_offset = got->add_constant(1);
6095               entry->object()->set_local_got_offset(entry->symndx(), got_type,
6096                                                     got_offset,
6097                                                     entry->addend());
6098               got->add_constant(0);
6099               got->add_static_reloc(got_offset + size/8, r_type2,
6100                                     entry->object(), entry->symndx());
6101             }
6102         }
6103       else if (entry->tls_type() == GOT_TLS_IE)
6104         {
6105           unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6106           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6107                                             : elfcpp::R_MIPS_TLS_TPREL64);
6108           if (!parameters->doing_static_link())
6109             got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
6110                                     target->rel_dyn_section(layout), r_type,
6111                                     entry->addend());
6112           else
6113             {
6114               got->add_local(entry->object(), entry->symndx(), got_type,
6115                              entry->addend());
6116               unsigned int got_offset =
6117                   entry->object()->local_got_offset(entry->symndx(), got_type,
6118                                                     entry->addend());
6119               got->add_static_reloc(got_offset, r_type, entry->object(),
6120                                     entry->symndx());
6121             }
6122         }
6123       else if (entry->tls_type() == GOT_TLS_LDM)
6124         {
6125           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6126                                             : elfcpp::R_MIPS_TLS_DTPMOD64);
6127           unsigned int got_offset;
6128           if (!parameters->doing_static_link())
6129             {
6130               got_offset = got->add_constant(0);
6131               target->rel_dyn_section(layout)->add_local(
6132                   entry->object(), 0, r_type, got, got_offset);
6133             }
6134           else
6135             // We are doing a static link.  Just mark it as belong to module 1,
6136             // the executable.
6137             got_offset = got->add_constant(1);
6138
6139           got->add_constant(0);
6140           got->set_tls_ldm_offset(got_offset, entry->object());
6141         }
6142       else
6143         gold_unreachable();
6144     }
6145
6146   // Add global tls entries.
6147   for (typename Got_entry_set::iterator
6148        p = this->got_entries_.begin();
6149        p != this->got_entries_.end();
6150        ++p)
6151     {
6152       Mips_got_entry<size, big_endian>* entry = *p;
6153       if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6154         continue;
6155
6156       Mips_symbol<size>* mips_sym = entry->sym();
6157       if (entry->tls_type() == GOT_TLS_GD)
6158         {
6159           unsigned int got_type;
6160           if (!got->multi_got())
6161             got_type = GOT_TYPE_TLS_PAIR;
6162           else
6163             got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6164           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6165                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6166           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6167                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6168           if (!parameters->doing_static_link())
6169             got->add_global_pair_with_rel(mips_sym, got_type,
6170                              target->rel_dyn_section(layout), r_type1, r_type2);
6171           else
6172             {
6173               // Add a GOT pair for for R_MIPS_TLS_GD.  The creates a pair of
6174               // GOT entries.  The first one is initialized to be 1, which is the
6175               // module index for the main executable and the second one 0.  A
6176               // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6177               // the second GOT entry and will be applied by gold.
6178               unsigned int got_offset = got->add_constant(1);
6179               mips_sym->set_got_offset(got_type, got_offset);
6180               got->add_constant(0);
6181               got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6182             }
6183         }
6184       else if (entry->tls_type() == GOT_TLS_IE)
6185         {
6186           unsigned int got_type;
6187           if (!got->multi_got())
6188             got_type = GOT_TYPE_TLS_OFFSET;
6189           else
6190             got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
6191           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6192                                             : elfcpp::R_MIPS_TLS_TPREL64);
6193           if (!parameters->doing_static_link())
6194             got->add_global_with_rel(mips_sym, got_type,
6195                                      target->rel_dyn_section(layout), r_type);
6196           else
6197             {
6198               got->add_global(mips_sym, got_type);
6199               unsigned int got_offset = mips_sym->got_offset(got_type);
6200               got->add_static_reloc(got_offset, r_type, mips_sym);
6201             }
6202         }
6203       else
6204         gold_unreachable();
6205     }
6206 }
6207
6208 // Decide whether the symbol needs an entry in the global part of the primary
6209 // GOT, setting global_got_area accordingly.  Count the number of global
6210 // symbols that are in the primary GOT only because they have dynamic
6211 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6212
6213 template<int size, bool big_endian>
6214 void
6215 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6216 {
6217   for (typename Global_got_entry_set::iterator
6218        p = this->global_got_symbols_.begin();
6219        p != this->global_got_symbols_.end();
6220        ++p)
6221     {
6222       Mips_symbol<size>* sym = *p;
6223       // Make a final decision about whether the symbol belongs in the
6224       // local or global GOT.  Symbols that bind locally can (and in the
6225       // case of forced-local symbols, must) live in the local GOT.
6226       // Those that are aren't in the dynamic symbol table must also
6227       // live in the local GOT.
6228
6229       if (!sym->should_add_dynsym_entry(symtab)
6230           || (sym->got_only_for_calls()
6231               ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6232               : symbol_references_local(sym,
6233                                         sym->should_add_dynsym_entry(symtab))))
6234         // The symbol belongs in the local GOT.  We no longer need this
6235         // entry if it was only used for relocations; those relocations
6236         // will be against the null or section symbol instead.
6237         sym->set_global_got_area(GGA_NONE);
6238       else if (sym->global_got_area() == GGA_RELOC_ONLY)
6239         {
6240           ++this->reloc_only_gotno_;
6241           ++this->global_gotno_ ;
6242         }
6243     }
6244 }
6245
6246 // Return the offset of GOT page entry for VALUE.  Initialize the entry with
6247 // VALUE if it is not initialized.
6248
6249 template<int size, bool big_endian>
6250 unsigned int
6251 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6252     Mips_output_data_got<size, big_endian>* got)
6253 {
6254   typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6255   if (it != this->got_page_offsets_.end())
6256     return it->second;
6257
6258   gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6259               + (size/8) * this->page_gotno_);
6260
6261   unsigned int got_offset = this->got_page_offset_next_;
6262   this->got_page_offsets_[value] = got_offset;
6263   this->got_page_offset_next_ += size/8;
6264   got->update_got_entry(got_offset, value);
6265   return got_offset;
6266 }
6267
6268 // Remove lazy-binding stubs for global symbols in this GOT.
6269
6270 template<int size, bool big_endian>
6271 void
6272 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6273     Target_mips<size, big_endian>* target)
6274 {
6275   for (typename Got_entry_set::iterator
6276        p = this->got_entries_.begin();
6277        p != this->got_entries_.end();
6278        ++p)
6279     {
6280       Mips_got_entry<size, big_endian>* entry = *p;
6281       if (entry->is_for_global_symbol())
6282         target->remove_lazy_stub_entry(entry->sym());
6283     }
6284 }
6285
6286 // Count the number of GOT entries required.
6287
6288 template<int size, bool big_endian>
6289 void
6290 Mips_got_info<size, big_endian>::count_got_entries()
6291 {
6292   for (typename Got_entry_set::iterator
6293        p = this->got_entries_.begin();
6294        p != this->got_entries_.end();
6295        ++p)
6296     {
6297       this->count_got_entry(*p);
6298     }
6299 }
6300
6301 // Count the number of GOT entries required by ENTRY.  Accumulate the result.
6302
6303 template<int size, bool big_endian>
6304 void
6305 Mips_got_info<size, big_endian>::count_got_entry(
6306     Mips_got_entry<size, big_endian>* entry)
6307 {
6308   if (entry->is_tls_entry())
6309     this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6310   else if (entry->is_for_local_symbol()
6311            || entry->sym()->global_got_area() == GGA_NONE)
6312     ++this->local_gotno_;
6313   else
6314     ++this->global_gotno_;
6315 }
6316
6317 // Add FROM's GOT entries.
6318
6319 template<int size, bool big_endian>
6320 void
6321 Mips_got_info<size, big_endian>::add_got_entries(
6322     Mips_got_info<size, big_endian>* from)
6323 {
6324   for (typename Got_entry_set::iterator
6325        p = from->got_entries_.begin();
6326        p != from->got_entries_.end();
6327        ++p)
6328     {
6329       Mips_got_entry<size, big_endian>* entry = *p;
6330       if (this->got_entries_.find(entry) == this->got_entries_.end())
6331         {
6332           Mips_got_entry<size, big_endian>* entry2 =
6333             new Mips_got_entry<size, big_endian>(*entry);
6334           this->got_entries_.insert(entry2);
6335           this->count_got_entry(entry);
6336         }
6337     }
6338 }
6339
6340 // Add FROM's GOT page entries.
6341
6342 template<int size, bool big_endian>
6343 void
6344 Mips_got_info<size, big_endian>::add_got_page_count(
6345     Mips_got_info<size, big_endian>* from)
6346 {
6347   this->page_gotno_ += from->page_gotno_;
6348 }
6349
6350 // Mips_output_data_got methods.
6351
6352 // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
6353 // larger than 64K, create multi-GOT.
6354
6355 template<int size, bool big_endian>
6356 void
6357 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6358     Symbol_table* symtab, const Input_objects* input_objects)
6359 {
6360   // Decide which symbols need to go in the global part of the GOT and
6361   // count the number of reloc-only GOT symbols.
6362   this->master_got_info_->count_got_symbols(symtab);
6363
6364   // Count the number of GOT entries.
6365   this->master_got_info_->count_got_entries();
6366
6367   unsigned int got_size = this->master_got_info_->got_size();
6368   if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6369     this->lay_out_multi_got(layout, input_objects);
6370   else
6371     {
6372       // Record that all objects use single GOT.
6373       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6374            p != input_objects->relobj_end();
6375            ++p)
6376         {
6377           Mips_relobj<size, big_endian>* object =
6378             Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6379           if (object->get_got_info() != NULL)
6380             object->set_got_info(this->master_got_info_);
6381         }
6382
6383       this->master_got_info_->add_local_entries(this->target_, layout);
6384       this->master_got_info_->add_global_entries(this->target_, layout,
6385                                                  /*not used*/-1U);
6386       this->master_got_info_->add_tls_entries(this->target_, layout);
6387     }
6388 }
6389
6390 // Create multi-GOT.  For every GOT, add local, global and TLS entries.
6391
6392 template<int size, bool big_endian>
6393 void
6394 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6395     const Input_objects* input_objects)
6396 {
6397   // Try to merge the GOTs of input objects together, as long as they
6398   // don't seem to exceed the maximum GOT size, choosing one of them
6399   // to be the primary GOT.
6400   this->merge_gots(input_objects);
6401
6402   // Every symbol that is referenced in a dynamic relocation must be
6403   // present in the primary GOT.
6404   this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6405
6406   // Add GOT entries.
6407   unsigned int i = 0;
6408   unsigned int offset = 0;
6409   Mips_got_info<size, big_endian>* g = this->primary_got_;
6410   do
6411     {
6412       g->set_index(i);
6413       g->set_offset(offset);
6414
6415       g->add_local_entries(this->target_, layout);
6416       if (i == 0)
6417         g->add_global_entries(this->target_, layout,
6418                               (this->master_got_info_->global_gotno()
6419                                - this->master_got_info_->reloc_only_gotno()));
6420       else
6421         g->add_global_entries(this->target_, layout, /*not used*/-1U);
6422       g->add_tls_entries(this->target_, layout);
6423
6424       // Forbid global symbols in every non-primary GOT from having
6425       // lazy-binding stubs.
6426       if (i > 0)
6427         g->remove_lazy_stubs(this->target_);
6428
6429       ++i;
6430       offset += g->got_size();
6431       g = g->next();
6432     }
6433   while (g);
6434 }
6435
6436 // Attempt to merge GOTs of different input objects.  Try to use as much as
6437 // possible of the primary GOT, since it doesn't require explicit dynamic
6438 // relocations, but don't use objects that would reference global symbols
6439 // out of the addressable range.  Failing the primary GOT, attempt to merge
6440 // with the current GOT, or finish the current GOT and then make make the new
6441 // GOT current.
6442
6443 template<int size, bool big_endian>
6444 void
6445 Mips_output_data_got<size, big_endian>::merge_gots(
6446     const Input_objects* input_objects)
6447 {
6448   gold_assert(this->primary_got_ == NULL);
6449   Mips_got_info<size, big_endian>* current = NULL;
6450
6451   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6452        p != input_objects->relobj_end();
6453        ++p)
6454     {
6455       Mips_relobj<size, big_endian>* object =
6456         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6457
6458       Mips_got_info<size, big_endian>* g = object->get_got_info();
6459       if (g == NULL)
6460         continue;
6461
6462       g->count_got_entries();
6463
6464       // Work out the number of page, local and TLS entries.
6465       unsigned int estimate = this->master_got_info_->page_gotno();
6466       if (estimate > g->page_gotno())
6467         estimate = g->page_gotno();
6468       estimate += g->local_gotno() + g->tls_gotno();
6469
6470       // We place TLS GOT entries after both locals and globals.  The globals
6471       // for the primary GOT may overflow the normal GOT size limit, so be
6472       // sure not to merge a GOT which requires TLS with the primary GOT in that
6473       // case.  This doesn't affect non-primary GOTs.
6474       estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6475                                       : g->global_gotno());
6476
6477       unsigned int max_count =
6478         Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6479       if (estimate <= max_count)
6480         {
6481           // If we don't have a primary GOT, use it as
6482           // a starting point for the primary GOT.
6483           if (!this->primary_got_)
6484             {
6485               this->primary_got_ = g;
6486               continue;
6487             }
6488
6489           // Try merging with the primary GOT.
6490           if (this->merge_got_with(g, object, this->primary_got_))
6491             continue;
6492         }
6493
6494       // If we can merge with the last-created GOT, do it.
6495       if (current && this->merge_got_with(g, object, current))
6496         continue;
6497
6498       // Well, we couldn't merge, so create a new GOT.  Don't check if it
6499       // fits; if it turns out that it doesn't, we'll get relocation
6500       // overflows anyway.
6501       g->set_next(current);
6502       current = g;
6503     }
6504
6505   // If we do not find any suitable primary GOT, create an empty one.
6506   if (this->primary_got_ == NULL)
6507     this->primary_got_ = new Mips_got_info<size, big_endian>();
6508
6509   // Link primary GOT with secondary GOTs.
6510   this->primary_got_->set_next(current);
6511 }
6512
6513 // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
6514 // this would lead to overflow, true if they were merged successfully.
6515
6516 template<int size, bool big_endian>
6517 bool
6518 Mips_output_data_got<size, big_endian>::merge_got_with(
6519     Mips_got_info<size, big_endian>* from,
6520     Mips_relobj<size, big_endian>* object,
6521     Mips_got_info<size, big_endian>* to)
6522 {
6523   // Work out how many page entries we would need for the combined GOT.
6524   unsigned int estimate = this->master_got_info_->page_gotno();
6525   if (estimate >= from->page_gotno() + to->page_gotno())
6526     estimate = from->page_gotno() + to->page_gotno();
6527
6528   // Conservatively estimate how many local and TLS entries would be needed.
6529   estimate += from->local_gotno() + to->local_gotno();
6530   estimate += from->tls_gotno() + to->tls_gotno();
6531
6532   // If we're merging with the primary got, any TLS relocations will
6533   // come after the full set of global entries.  Otherwise estimate those
6534   // conservatively as well.
6535   if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6536     estimate += this->master_got_info_->global_gotno();
6537   else
6538     estimate += from->global_gotno() + to->global_gotno();
6539
6540   // Bail out if the combined GOT might be too big.
6541   unsigned int max_count =
6542     Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6543   if (estimate > max_count)
6544     return false;
6545
6546   // Transfer the object's GOT information from FROM to TO.
6547   to->add_got_entries(from);
6548   to->add_got_page_count(from);
6549
6550   // Record that OBJECT should use output GOT TO.
6551   object->set_got_info(to);
6552
6553   return true;
6554 }
6555
6556 // Write out the GOT.
6557
6558 template<int size, bool big_endian>
6559 void
6560 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6561 {
6562   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6563       Mips_stubs_entry_set;
6564
6565   // Call parent to write out GOT.
6566   Output_data_got<size, big_endian>::do_write(of);
6567
6568   const off_t offset = this->offset();
6569   const section_size_type oview_size =
6570     convert_to_section_size_type(this->data_size());
6571   unsigned char* const oview = of->get_output_view(offset, oview_size);
6572
6573   // Needed for fixing values of .got section.
6574   this->got_view_ = oview;
6575
6576   // Write lazy stub addresses.
6577   for (typename Mips_stubs_entry_set::iterator
6578        p = this->master_got_info_->global_got_symbols().begin();
6579        p != this->master_got_info_->global_got_symbols().end();
6580        ++p)
6581     {
6582       Mips_symbol<size>* mips_sym = *p;
6583       if (mips_sym->has_lazy_stub())
6584         {
6585           Valtype* wv = reinterpret_cast<Valtype*>(
6586             oview + this->get_primary_got_offset(mips_sym));
6587           Valtype value =
6588             this->target_->mips_stubs_section()->stub_address(mips_sym);
6589           elfcpp::Swap<size, big_endian>::writeval(wv, value);
6590         }
6591     }
6592
6593   // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6594   for (typename Mips_stubs_entry_set::iterator
6595        p = this->master_got_info_->global_got_symbols().begin();
6596        p != this->master_got_info_->global_got_symbols().end();
6597        ++p)
6598     {
6599       Mips_symbol<size>* mips_sym = *p;
6600       if (!this->multi_got()
6601           && (mips_sym->is_mips16() || mips_sym->is_micromips())
6602           && mips_sym->global_got_area() == GGA_NONE
6603           && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6604         {
6605           Valtype* wv = reinterpret_cast<Valtype*>(
6606             oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6607           Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6608           if (value != 0)
6609             {
6610               value |= 1;
6611               elfcpp::Swap<size, big_endian>::writeval(wv, value);
6612             }
6613         }
6614     }
6615
6616   if (!this->secondary_got_relocs_.empty())
6617     {
6618       // Fixup for the secondary GOT R_MIPS_REL32 relocs.  For global
6619       // secondary GOT entries with non-zero initial value copy the value
6620       // to the corresponding primary GOT entry, and set the secondary GOT
6621       // entry to zero.
6622       // TODO(sasa): This is workaround.  It needs to be investigated further.
6623
6624       for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6625         {
6626           Static_reloc& reloc(this->secondary_got_relocs_[i]);
6627           if (reloc.symbol_is_global())
6628             {
6629               Mips_symbol<size>* gsym = reloc.symbol();
6630               gold_assert(gsym != NULL);
6631
6632               unsigned got_offset = reloc.got_offset();
6633               gold_assert(got_offset < oview_size);
6634
6635               // Find primary GOT entry.
6636               Valtype* wv_prim = reinterpret_cast<Valtype*>(
6637                 oview + this->get_primary_got_offset(gsym));
6638
6639               // Find secondary GOT entry.
6640               Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6641
6642               Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6643               if (value != 0)
6644                 {
6645                   elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6646                   elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6647                   gsym->set_applied_secondary_got_fixup();
6648                 }
6649             }
6650         }
6651
6652       of->write_output_view(offset, oview_size, oview);
6653     }
6654
6655   // We are done if there is no fix up.
6656   if (this->static_relocs_.empty())
6657     return;
6658
6659   Output_segment* tls_segment = this->layout_->tls_segment();
6660   gold_assert(tls_segment != NULL);
6661
6662   for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6663     {
6664       Static_reloc& reloc(this->static_relocs_[i]);
6665
6666       Mips_address value;
6667       if (!reloc.symbol_is_global())
6668         {
6669           Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6670           const Symbol_value<size>* psymval =
6671             object->local_symbol(reloc.index());
6672
6673           // We are doing static linking.  Issue an error and skip this
6674           // relocation if the symbol is undefined or in a discarded_section.
6675           bool is_ordinary;
6676           unsigned int shndx = psymval->input_shndx(&is_ordinary);
6677           if ((shndx == elfcpp::SHN_UNDEF)
6678               || (is_ordinary
6679                   && shndx != elfcpp::SHN_UNDEF
6680                   && !object->is_section_included(shndx)
6681                   && !this->symbol_table_->is_section_folded(object, shndx)))
6682             {
6683               gold_error(_("undefined or discarded local symbol %u from "
6684                            " object %s in GOT"),
6685                          reloc.index(), reloc.relobj()->name().c_str());
6686               continue;
6687             }
6688
6689           value = psymval->value(object, 0);
6690         }
6691       else
6692         {
6693           const Mips_symbol<size>* gsym = reloc.symbol();
6694           gold_assert(gsym != NULL);
6695
6696           // We are doing static linking.  Issue an error and skip this
6697           // relocation if the symbol is undefined or in a discarded_section
6698           // unless it is a weakly_undefined symbol.
6699           if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6700               && !gsym->is_weak_undefined())
6701             {
6702               gold_error(_("undefined or discarded symbol %s in GOT"),
6703                          gsym->name());
6704               continue;
6705             }
6706
6707           if (!gsym->is_weak_undefined())
6708             value = gsym->value();
6709           else
6710             value = 0;
6711         }
6712
6713       unsigned got_offset = reloc.got_offset();
6714       gold_assert(got_offset < oview_size);
6715
6716       Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6717       Valtype x;
6718
6719       switch (reloc.r_type())
6720         {
6721         case elfcpp::R_MIPS_TLS_DTPMOD32:
6722         case elfcpp::R_MIPS_TLS_DTPMOD64:
6723           x = value;
6724           break;
6725         case elfcpp::R_MIPS_TLS_DTPREL32:
6726         case elfcpp::R_MIPS_TLS_DTPREL64:
6727           x = value - elfcpp::DTP_OFFSET;
6728           break;
6729         case elfcpp::R_MIPS_TLS_TPREL32:
6730         case elfcpp::R_MIPS_TLS_TPREL64:
6731           x = value - elfcpp::TP_OFFSET;
6732           break;
6733         default:
6734           gold_unreachable();
6735           break;
6736         }
6737
6738       elfcpp::Swap<size, big_endian>::writeval(wv, x);
6739     }
6740
6741   of->write_output_view(offset, oview_size, oview);
6742 }
6743
6744 // Mips_relobj methods.
6745
6746 // Count the local symbols.  The Mips backend needs to know if a symbol
6747 // is a MIPS16 or microMIPS function or not.  For global symbols, it is easy
6748 // because the Symbol object keeps the ELF symbol type and st_other field.
6749 // For local symbol it is harder because we cannot access this information.
6750 // So we override the do_count_local_symbol in parent and scan local symbols to
6751 // mark MIPS16 and microMIPS functions.  This is not the most efficient way but
6752 // I do not want to slow down other ports by calling a per symbol target hook
6753 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6754
6755 template<int size, bool big_endian>
6756 void
6757 Mips_relobj<size, big_endian>::do_count_local_symbols(
6758     Stringpool_template<char>* pool,
6759     Stringpool_template<char>* dynpool)
6760 {
6761   // Ask parent to count the local symbols.
6762   Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6763   const unsigned int loccount = this->local_symbol_count();
6764   if (loccount == 0)
6765     return;
6766
6767   // Initialize the mips16 and micromips function bit-vector.
6768   this->local_symbol_is_mips16_.resize(loccount, false);
6769   this->local_symbol_is_micromips_.resize(loccount, false);
6770
6771   // Read the symbol table section header.
6772   const unsigned int symtab_shndx = this->symtab_shndx();
6773   elfcpp::Shdr<size, big_endian>
6774     symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6775   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6776
6777   // Read the local symbols.
6778   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6779   gold_assert(loccount == symtabshdr.get_sh_info());
6780   off_t locsize = loccount * sym_size;
6781   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6782                                               locsize, true, true);
6783
6784   // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6785
6786   // Skip the first dummy symbol.
6787   psyms += sym_size;
6788   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6789     {
6790       elfcpp::Sym<size, big_endian> sym(psyms);
6791       unsigned char st_other = sym.get_st_other();
6792       this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6793       this->local_symbol_is_micromips_[i] =
6794         elfcpp::elf_st_is_micromips(st_other);
6795     }
6796 }
6797
6798 // Read the symbol information.
6799
6800 template<int size, bool big_endian>
6801 void
6802 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6803 {
6804   // Call parent class to read symbol information.
6805   this->base_read_symbols(sd);
6806
6807   // If this input file is a binary file, it has no processor
6808   // specific data.
6809   Input_file::Format format = this->input_file()->format();
6810   if (format != Input_file::FORMAT_ELF)
6811     {
6812       gold_assert(format == Input_file::FORMAT_BINARY);
6813       this->merge_processor_specific_data_ = false;
6814       return;
6815     }
6816
6817   // Read processor-specific flags in ELF file header.
6818   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6819                                             elfcpp::Elf_sizes<size>::ehdr_size,
6820                                             true, false);
6821   elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6822   this->processor_specific_flags_ = ehdr.get_e_flags();
6823
6824   // Get the section names.
6825   const unsigned char* pnamesu = sd->section_names->data();
6826   const char* pnames = reinterpret_cast<const char*>(pnamesu);
6827
6828   // Initialize the mips16 stub section bit-vectors.
6829   this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6830   this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6831   this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6832
6833   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6834   const unsigned char* pshdrs = sd->section_headers->data();
6835   const unsigned char* ps = pshdrs + shdr_size;
6836   bool must_merge_processor_specific_data = false;
6837   for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6838     {
6839       elfcpp::Shdr<size, big_endian> shdr(ps);
6840
6841       // Sometimes an object has no contents except the section name string
6842       // table and an empty symbol table with the undefined symbol.  We
6843       // don't want to merge processor-specific data from such an object.
6844       if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6845         {
6846           // Symbol table is not empty.
6847           const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
6848             elfcpp::Elf_sizes<size>::sym_size;
6849           if (shdr.get_sh_size() > sym_size)
6850             must_merge_processor_specific_data = true;
6851         }
6852       else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6853         // If this is neither an empty symbol table nor a string table,
6854         // be conservative.
6855         must_merge_processor_specific_data = true;
6856
6857       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6858         {
6859           this->has_reginfo_section_ = true;
6860           // Read the gp value that was used to create this object.  We need the
6861           // gp value while processing relocs.  The .reginfo section is not used
6862           // in the 64-bit MIPS ELF ABI.
6863           section_offset_type section_offset = shdr.get_sh_offset();
6864           section_size_type section_size =
6865             convert_to_section_size_type(shdr.get_sh_size());
6866           const unsigned char* view =
6867              this->get_view(section_offset, section_size, true, false);
6868
6869           this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6870
6871           // Read the rest of .reginfo.
6872           this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6873           this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6874           this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6875           this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6876           this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6877         }
6878
6879       if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6880         {
6881           gold_assert(this->attributes_section_data_ == NULL);
6882           section_offset_type section_offset = shdr.get_sh_offset();
6883           section_size_type section_size =
6884             convert_to_section_size_type(shdr.get_sh_size());
6885           const unsigned char* view =
6886             this->get_view(section_offset, section_size, true, false);
6887           this->attributes_section_data_ =
6888             new Attributes_section_data(view, section_size);
6889         }
6890
6891       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6892         {
6893           gold_assert(this->abiflags_ == NULL);
6894           section_offset_type section_offset = shdr.get_sh_offset();
6895           section_size_type section_size =
6896             convert_to_section_size_type(shdr.get_sh_size());
6897           const unsigned char* view =
6898             this->get_view(section_offset, section_size, true, false);
6899           this->abiflags_ = new Mips_abiflags<big_endian>();
6900
6901           this->abiflags_->version =
6902             elfcpp::Swap<16, big_endian>::readval(view);
6903           if (this->abiflags_->version != 0)
6904             {
6905               gold_error(_("%s: .MIPS.abiflags section has "
6906                            "unsupported version %u"),
6907                          this->name().c_str(),
6908                          this->abiflags_->version);
6909               break;
6910             }
6911           this->abiflags_->isa_level =
6912             elfcpp::Swap<8, big_endian>::readval(view + 2);
6913           this->abiflags_->isa_rev =
6914             elfcpp::Swap<8, big_endian>::readval(view + 3);
6915           this->abiflags_->gpr_size =
6916             elfcpp::Swap<8, big_endian>::readval(view + 4);
6917           this->abiflags_->cpr1_size =
6918             elfcpp::Swap<8, big_endian>::readval(view + 5);
6919           this->abiflags_->cpr2_size =
6920             elfcpp::Swap<8, big_endian>::readval(view + 6);
6921           this->abiflags_->fp_abi =
6922             elfcpp::Swap<8, big_endian>::readval(view + 7);
6923           this->abiflags_->isa_ext =
6924             elfcpp::Swap<32, big_endian>::readval(view + 8);
6925           this->abiflags_->ases =
6926             elfcpp::Swap<32, big_endian>::readval(view + 12);
6927           this->abiflags_->flags1 =
6928             elfcpp::Swap<32, big_endian>::readval(view + 16);
6929           this->abiflags_->flags2 =
6930             elfcpp::Swap<32, big_endian>::readval(view + 20);
6931         }
6932
6933       // In the 64-bit ABI, .MIPS.options section holds register information.
6934       // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6935       // starts with this header:
6936       //
6937       // typedef struct
6938       // {
6939       //   // Type of option.
6940       //   unsigned char kind[1];
6941       //   // Size of option descriptor, including header.
6942       //   unsigned char size[1];
6943       //   // Section index of affected section, or 0 for global option.
6944       //   unsigned char section[2];
6945       //   // Information specific to this kind of option.
6946       //   unsigned char info[4];
6947       // };
6948       //
6949       // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6950       // the gp value based on what we find.  We may see both SHT_MIPS_REGINFO
6951       // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6952
6953       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6954         {
6955           section_offset_type section_offset = shdr.get_sh_offset();
6956           section_size_type section_size =
6957             convert_to_section_size_type(shdr.get_sh_size());
6958           const unsigned char* view =
6959              this->get_view(section_offset, section_size, true, false);
6960           const unsigned char* end = view + section_size;
6961
6962           while (view + 8 <= end)
6963             {
6964               unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6965               unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6966               if (sz < 8)
6967                 {
6968                   gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6969                                "than its header"),
6970                              this->name().c_str(),
6971                              this->mips_elf_options_section_name(), sz);
6972                   break;
6973                 }
6974
6975               if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6976                 {
6977                   // In the 64 bit ABI, an ODK_REGINFO option is the following
6978                   // structure.  The info field of the options header is not
6979                   // used.
6980                   //
6981                   // typedef struct
6982                   // {
6983                   //   // Mask of general purpose registers used.
6984                   //   unsigned char ri_gprmask[4];
6985                   //   // Padding.
6986                   //   unsigned char ri_pad[4];
6987                   //   // Mask of co-processor registers used.
6988                   //   unsigned char ri_cprmask[4][4];
6989                   //   // GP register value for this object file.
6990                   //   unsigned char ri_gp_value[8];
6991                   // };
6992
6993                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6994                                                                       + 32);
6995                 }
6996               else if (kind == elfcpp::ODK_REGINFO)
6997                 {
6998                   // In the 32 bit ABI, an ODK_REGINFO option is the following
6999                   // structure.  The info field of the options header is not
7000                   // used.  The same structure is used in .reginfo section.
7001                   //
7002                   // typedef struct
7003                   // {
7004                   //   unsigned char ri_gprmask[4];
7005                   //   unsigned char ri_cprmask[4][4];
7006                   //   unsigned char ri_gp_value[4];
7007                   // };
7008
7009                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7010                                                                       + 28);
7011                 }
7012               view += sz;
7013             }
7014         }
7015
7016       const char* name = pnames + shdr.get_sh_name();
7017       this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
7018       this->section_is_mips16_call_stub_[i] =
7019         is_prefix_of(".mips16.call.", name);
7020       this->section_is_mips16_call_fp_stub_[i] =
7021         is_prefix_of(".mips16.call.fp.", name);
7022
7023       if (strcmp(name, ".pdr") == 0)
7024         {
7025           gold_assert(this->pdr_shndx_ == -1U);
7026           this->pdr_shndx_ = i;
7027         }
7028     }
7029
7030   // This is rare.
7031   if (!must_merge_processor_specific_data)
7032     this->merge_processor_specific_data_ = false;
7033 }
7034
7035 // Discard MIPS16 stub secions that are not needed.
7036
7037 template<int size, bool big_endian>
7038 void
7039 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7040 {
7041   for (typename Mips16_stubs_int_map::const_iterator
7042        it = this->mips16_stub_sections_.begin();
7043        it != this->mips16_stub_sections_.end(); ++it)
7044     {
7045       Mips16_stub_section<size, big_endian>* stub_section = it->second;
7046       if (!stub_section->is_target_found())
7047         {
7048           gold_error(_("no relocation found in mips16 stub section '%s'"),
7049                      stub_section->object()
7050                        ->section_name(stub_section->shndx()).c_str());
7051         }
7052
7053       bool discard = false;
7054       if (stub_section->is_for_local_function())
7055         {
7056           if (stub_section->is_fn_stub())
7057             {
7058               // This stub is for a local symbol.  This stub will only
7059               // be needed if there is some relocation in this object,
7060               // other than a 16 bit function call, which refers to this
7061               // symbol.
7062               if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7063                 discard = true;
7064               else
7065                 this->add_local_mips16_fn_stub(stub_section);
7066             }
7067           else
7068             {
7069               // This stub is for a local symbol.  This stub will only
7070               // be needed if there is some relocation (R_MIPS16_26) in
7071               // this object that refers to this symbol.
7072               gold_assert(stub_section->is_call_stub()
7073                           || stub_section->is_call_fp_stub());
7074               if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
7075                 discard = true;
7076               else
7077                 this->add_local_mips16_call_stub(stub_section);
7078             }
7079         }
7080       else
7081         {
7082           Mips_symbol<size>* gsym = stub_section->gsym();
7083           if (stub_section->is_fn_stub())
7084             {
7085               if (gsym->has_mips16_fn_stub())
7086                 // We already have a stub for this function.
7087                 discard = true;
7088               else
7089                 {
7090                   gsym->set_mips16_fn_stub(stub_section);
7091                   if (gsym->should_add_dynsym_entry(symtab))
7092                     {
7093                       // If we have a MIPS16 function with a stub, the
7094                       // dynamic symbol must refer to the stub, since only
7095                       // the stub uses the standard calling conventions.
7096                       gsym->set_need_fn_stub();
7097                       if (gsym->is_from_dynobj())
7098                         gsym->set_needs_dynsym_value();
7099                     }
7100                 }
7101               if (!gsym->need_fn_stub())
7102                 discard = true;
7103             }
7104           else if (stub_section->is_call_stub())
7105             {
7106               if (gsym->is_mips16())
7107                 // We don't need the call_stub; this is a 16 bit
7108                 // function, so calls from other 16 bit functions are
7109                 // OK.
7110                 discard = true;
7111               else if (gsym->has_mips16_call_stub())
7112                 // We already have a stub for this function.
7113                 discard = true;
7114               else
7115                 gsym->set_mips16_call_stub(stub_section);
7116             }
7117           else
7118             {
7119               gold_assert(stub_section->is_call_fp_stub());
7120               if (gsym->is_mips16())
7121                 // We don't need the call_stub; this is a 16 bit
7122                 // function, so calls from other 16 bit functions are
7123                 // OK.
7124                 discard = true;
7125               else if (gsym->has_mips16_call_fp_stub())
7126                 // We already have a stub for this function.
7127                 discard = true;
7128               else
7129                 gsym->set_mips16_call_fp_stub(stub_section);
7130             }
7131         }
7132       if (discard)
7133         this->set_output_section(stub_section->shndx(), NULL);
7134    }
7135 }
7136
7137 // Mips_output_data_la25_stub methods.
7138
7139 // Template for standard LA25 stub.
7140 template<int size, bool big_endian>
7141 const uint32_t
7142 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7143 {
7144   0x3c190000,           // lui $25,%hi(func)
7145   0x08000000,           // j func
7146   0x27390000,           // add $25,$25,%lo(func)
7147   0x00000000            // nop
7148 };
7149
7150 // Template for microMIPS LA25 stub.
7151 template<int size, bool big_endian>
7152 const uint32_t
7153 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7154 {
7155   0x41b9, 0x0000,       // lui t9,%hi(func)
7156   0xd400, 0x0000,       // j func
7157   0x3339, 0x0000,       // addiu t9,t9,%lo(func)
7158   0x0000, 0x0000        // nop
7159 };
7160
7161 // Create la25 stub for a symbol.
7162
7163 template<int size, bool big_endian>
7164 void
7165 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7166     Symbol_table* symtab, Target_mips<size, big_endian>* target,
7167     Mips_symbol<size>* gsym)
7168 {
7169   if (!gsym->has_la25_stub())
7170     {
7171       gsym->set_la25_stub_offset(this->symbols_.size() * 16);
7172       this->symbols_.push_back(gsym);
7173       this->create_stub_symbol(gsym, symtab, target, 16);
7174     }
7175 }
7176
7177 // Create a symbol for SYM stub's value and size, to help make the disassembly
7178 // easier to read.
7179
7180 template<int size, bool big_endian>
7181 void
7182 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7183     Mips_symbol<size>* sym, Symbol_table* symtab,
7184     Target_mips<size, big_endian>* target, uint64_t symsize)
7185 {
7186   std::string name(".pic.");
7187   name += sym->name();
7188
7189   unsigned int offset = sym->la25_stub_offset();
7190   if (sym->is_micromips())
7191     offset |= 1;
7192
7193   // Make it a local function.
7194   Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7195                                       Symbol_table::PREDEFINED,
7196                                       target->la25_stub_section(),
7197                                       offset, symsize, elfcpp::STT_FUNC,
7198                                       elfcpp::STB_LOCAL,
7199                                       elfcpp::STV_DEFAULT, 0,
7200                                       false, false);
7201   new_sym->set_is_forced_local();
7202 }
7203
7204 // Write out la25 stubs.  This uses the hand-coded instructions above,
7205 // and adjusts them as needed.
7206
7207 template<int size, bool big_endian>
7208 void
7209 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7210 {
7211   const off_t offset = this->offset();
7212   const section_size_type oview_size =
7213     convert_to_section_size_type(this->data_size());
7214   unsigned char* const oview = of->get_output_view(offset, oview_size);
7215
7216   for (typename std::vector<Mips_symbol<size>*>::iterator
7217        p = this->symbols_.begin();
7218        p != this->symbols_.end();
7219        ++p)
7220     {
7221       Mips_symbol<size>* sym = *p;
7222       unsigned char* pov = oview + sym->la25_stub_offset();
7223
7224       Mips_address target = sym->value();
7225       if (!sym->is_micromips())
7226         {
7227           elfcpp::Swap<32, big_endian>::writeval(pov,
7228               la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7229           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7230               la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7231           elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7232               la25_stub_entry[2] | (target & 0xffff));
7233           elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7234         }
7235       else
7236         {
7237           target |= 1;
7238           // First stub instruction.  Paste high 16-bits of the target.
7239           elfcpp::Swap<16, big_endian>::writeval(pov,
7240                                                  la25_stub_micromips_entry[0]);
7241           elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7242               ((target + 0x8000) >> 16) & 0xffff);
7243           // Second stub instruction.  Paste low 26-bits of the target, shifted
7244           // right by 1.
7245           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7246               la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7247           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7248               la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7249           // Third stub instruction.  Paste low 16-bits of the target.
7250           elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7251                                                  la25_stub_micromips_entry[4]);
7252           elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7253           // Fourth stub instruction.
7254           elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7255                                                  la25_stub_micromips_entry[6]);
7256           elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7257                                                  la25_stub_micromips_entry[7]);
7258         }
7259     }
7260
7261   of->write_output_view(offset, oview_size, oview);
7262 }
7263
7264 // Mips_output_data_plt methods.
7265
7266 // The format of the first PLT entry in an O32 executable.
7267 template<int size, bool big_endian>
7268 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7269 {
7270   0x3c1c0000,         // lui $28, %hi(&GOTPLT[0])
7271   0x8f990000,         // lw $25, %lo(&GOTPLT[0])($28)
7272   0x279c0000,         // addiu $28, $28, %lo(&GOTPLT[0])
7273   0x031cc023,         // subu $24, $24, $28
7274   0x03e07825,         // or $15, $31, zero
7275   0x0018c082,         // srl $24, $24, 2
7276   0x0320f809,         // jalr $25
7277   0x2718fffe          // subu $24, $24, 2
7278 };
7279
7280 // The format of the first PLT entry in an N32 executable.  Different
7281 // because gp ($28) is not available; we use t2 ($14) instead.
7282 template<int size, bool big_endian>
7283 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7284 {
7285   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7286   0x8dd90000,         // lw $25, %lo(&GOTPLT[0])($14)
7287   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7288   0x030ec023,         // subu $24, $24, $14
7289   0x03e07825,         // or $15, $31, zero
7290   0x0018c082,         // srl $24, $24, 2
7291   0x0320f809,         // jalr $25
7292   0x2718fffe          // subu $24, $24, 2
7293 };
7294
7295 // The format of the first PLT entry in an N64 executable.  Different
7296 // from N32 because of the increased size of GOT entries.
7297 template<int size, bool big_endian>
7298 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7299 {
7300   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7301   0xddd90000,         // ld $25, %lo(&GOTPLT[0])($14)
7302   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7303   0x030ec023,         // subu $24, $24, $14
7304   0x03e07825,         // or $15, $31, zero
7305   0x0018c0c2,         // srl $24, $24, 3
7306   0x0320f809,         // jalr $25
7307   0x2718fffe          // subu $24, $24, 2
7308 };
7309
7310 // The format of the microMIPS first PLT entry in an O32 executable.
7311 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7312 // of the GOTPLT entry handled, so this stub may only be used when
7313 // all the subsequent PLT entries are microMIPS code too.
7314 //
7315 // The trailing NOP is for alignment and correct disassembly only.
7316 template<int size, bool big_endian>
7317 const uint32_t Mips_output_data_plt<size, big_endian>::
7318 plt0_entry_micromips_o32[] =
7319 {
7320   0x7980, 0x0000,      // addiupc $3, (&GOTPLT[0]) - .
7321   0xff23, 0x0000,      // lw $25, 0($3)
7322   0x0535,              // subu $2, $2, $3
7323   0x2525,              // srl $2, $2, 2
7324   0x3302, 0xfffe,      // subu $24, $2, 2
7325   0x0dff,              // move $15, $31
7326   0x45f9,              // jalrs $25
7327   0x0f83,              // move $28, $3
7328   0x0c00               // nop
7329 };
7330
7331 // The format of the microMIPS first PLT entry in an O32 executable
7332 // in the insn32 mode.
7333 template<int size, bool big_endian>
7334 const uint32_t Mips_output_data_plt<size, big_endian>::
7335 plt0_entry_micromips32_o32[] =
7336 {
7337   0x41bc, 0x0000,      // lui $28, %hi(&GOTPLT[0])
7338   0xff3c, 0x0000,      // lw $25, %lo(&GOTPLT[0])($28)
7339   0x339c, 0x0000,      // addiu $28, $28, %lo(&GOTPLT[0])
7340   0x0398, 0xc1d0,      // subu $24, $24, $28
7341   0x001f, 0x7a90,      // or $15, $31, zero
7342   0x0318, 0x1040,      // srl $24, $24, 2
7343   0x03f9, 0x0f3c,      // jalr $25
7344   0x3318, 0xfffe       // subu $24, $24, 2
7345 };
7346
7347 // The format of subsequent standard entries in the PLT.
7348 template<int size, bool big_endian>
7349 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7350 {
7351   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7352   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7353   0x03200008,           // jr $25
7354   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7355 };
7356
7357 // The format of subsequent R6 PLT entries.
7358 template<int size, bool big_endian>
7359 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7360 {
7361   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7362   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7363   0x03200009,           // jr $25
7364   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7365 };
7366
7367 // The format of subsequent MIPS16 o32 PLT entries.  We use v1 ($3) as a
7368 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7369 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7370 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7371 // target function address in register v0.
7372 template<int size, bool big_endian>
7373 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7374 {
7375   0xb303,              // lw $3, 12($pc)
7376   0x651b,              // move $24, $3
7377   0x9b60,              // lw $3, 0($3)
7378   0xeb00,              // jr $3
7379   0x653b,              // move $25, $3
7380   0x6500,              // nop
7381   0x0000, 0x0000       // .word (.got.plt entry)
7382 };
7383
7384 // The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
7385 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7386 template<int size, bool big_endian>
7387 const uint32_t Mips_output_data_plt<size, big_endian>::
7388 plt_entry_micromips_o32[] =
7389 {
7390   0x7900, 0x0000,      // addiupc $2, (.got.plt entry) - .
7391   0xff22, 0x0000,      // lw $25, 0($2)
7392   0x4599,              // jr $25
7393   0x0f02               // move $24, $2
7394 };
7395
7396 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7397 template<int size, bool big_endian>
7398 const uint32_t Mips_output_data_plt<size, big_endian>::
7399 plt_entry_micromips32_o32[] =
7400 {
7401   0x41af, 0x0000,      // lui $15, %hi(.got.plt entry)
7402   0xff2f, 0x0000,      // lw $25, %lo(.got.plt entry)($15)
7403   0x0019, 0x0f3c,      // jr $25
7404   0x330f, 0x0000       // addiu $24, $15, %lo(.got.plt entry)
7405 };
7406
7407 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7408
7409 template<int size, bool big_endian>
7410 void
7411 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7412                                                   unsigned int r_type)
7413 {
7414   gold_assert(!gsym->has_plt_offset());
7415
7416   // Final PLT offset for a symbol will be set in method set_plt_offsets().
7417   gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7418                        + sizeof(plt0_entry_o32));
7419   this->symbols_.push_back(gsym);
7420
7421   // Record whether the relocation requires a standard MIPS
7422   // or a compressed code entry.
7423   if (jal_reloc(r_type))
7424    {
7425      if (r_type == elfcpp::R_MIPS_26)
7426        gsym->set_needs_mips_plt(true);
7427      else
7428        gsym->set_needs_comp_plt(true);
7429    }
7430
7431   section_offset_type got_offset = this->got_plt_->current_data_size();
7432
7433   // Every PLT entry needs a GOT entry which points back to the PLT
7434   // entry (this will be changed by the dynamic linker, normally
7435   // lazily when the function is called).
7436   this->got_plt_->set_current_data_size(got_offset + size/8);
7437
7438   gsym->set_needs_dynsym_entry();
7439   this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7440                          got_offset);
7441 }
7442
7443 // Set final PLT offsets.  For each symbol, determine whether standard or
7444 // compressed (MIPS16 or microMIPS) PLT entry is used.
7445
7446 template<int size, bool big_endian>
7447 void
7448 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7449 {
7450   // The sizes of individual PLT entries.
7451   unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7452   unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7453                                       ? this->compressed_plt_entry_size() : 0);
7454
7455   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7456        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7457     {
7458       Mips_symbol<size>* mips_sym = *p;
7459
7460       // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7461       // so always use a standard entry there.
7462       //
7463       // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7464       // all MIPS16 calls will go via that stub, and there is no benefit
7465       // to having a MIPS16 entry.  And in the case of call_stub a
7466       // standard entry actually has to be used as the stub ends with a J
7467       // instruction.
7468       if (this->target_->is_output_newabi()
7469           || mips_sym->has_mips16_call_stub()
7470           || mips_sym->has_mips16_call_fp_stub())
7471         {
7472           mips_sym->set_needs_mips_plt(true);
7473           mips_sym->set_needs_comp_plt(false);
7474         }
7475
7476       // Otherwise, if there are no direct calls to the function, we
7477       // have a free choice of whether to use standard or compressed
7478       // entries.  Prefer microMIPS entries if the object is known to
7479       // contain microMIPS code, so that it becomes possible to create
7480       // pure microMIPS binaries.  Prefer standard entries otherwise,
7481       // because MIPS16 ones are no smaller and are usually slower.
7482       if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7483         {
7484           if (this->target_->is_output_micromips())
7485             mips_sym->set_needs_comp_plt(true);
7486           else
7487             mips_sym->set_needs_mips_plt(true);
7488         }
7489
7490       if (mips_sym->needs_mips_plt())
7491         {
7492           mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7493           this->plt_mips_offset_ += plt_mips_entry_size;
7494         }
7495       if (mips_sym->needs_comp_plt())
7496         {
7497           mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7498           this->plt_comp_offset_ += plt_comp_entry_size;
7499         }
7500     }
7501
7502     // Figure out the size of the PLT header if we know that we are using it.
7503     if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7504       this->plt_header_size_ = this->get_plt_header_size();
7505 }
7506
7507 // Write out the PLT.  This uses the hand-coded instructions above,
7508 // and adjusts them as needed.
7509
7510 template<int size, bool big_endian>
7511 void
7512 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7513 {
7514   const off_t offset = this->offset();
7515   const section_size_type oview_size =
7516     convert_to_section_size_type(this->data_size());
7517   unsigned char* const oview = of->get_output_view(offset, oview_size);
7518
7519   const off_t gotplt_file_offset = this->got_plt_->offset();
7520   const section_size_type gotplt_size =
7521     convert_to_section_size_type(this->got_plt_->data_size());
7522   unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7523                                                          gotplt_size);
7524   unsigned char* pov = oview;
7525
7526   Mips_address plt_address = this->address();
7527
7528   // Calculate the address of .got.plt.
7529   Mips_address gotplt_addr = this->got_plt_->address();
7530   Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7531   Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7532
7533   // The PLT sequence is not safe for N64 if .got.plt's address can
7534   // not be loaded in two instructions.
7535   gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7536               || ~(gotplt_addr | 0x7fffffff) == 0);
7537
7538   // Write the PLT header.
7539   const uint32_t* plt0_entry = this->get_plt_header_entry();
7540   if (plt0_entry == plt0_entry_micromips_o32)
7541     {
7542       // Write microMIPS PLT header.
7543       gold_assert(gotplt_addr % 4 == 0);
7544
7545       Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7546
7547       // ADDIUPC has a span of +/-16MB, check we're in range.
7548       if (gotpc_offset + 0x1000000 >= 0x2000000)
7549        {
7550          gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7551                     "ADDIUPC"), (long)gotpc_offset);
7552          return;
7553        }
7554
7555       elfcpp::Swap<16, big_endian>::writeval(pov,
7556                  plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7557       elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7558                                              (gotpc_offset >> 2) & 0xffff);
7559       pov += 4;
7560       for (unsigned int i = 2;
7561            i < (sizeof(plt0_entry_micromips_o32)
7562                 / sizeof(plt0_entry_micromips_o32[0]));
7563            i++)
7564         {
7565           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7566           pov += 2;
7567         }
7568     }
7569   else if (plt0_entry == plt0_entry_micromips32_o32)
7570     {
7571       // Write microMIPS PLT header in insn32 mode.
7572       elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7573       elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7574       elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7575       elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7576       elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7577       elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7578       pov += 12;
7579       for (unsigned int i = 6;
7580            i < (sizeof(plt0_entry_micromips32_o32)
7581                 / sizeof(plt0_entry_micromips32_o32[0]));
7582            i++)
7583         {
7584           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7585           pov += 2;
7586         }
7587     }
7588   else
7589     {
7590       // Write standard PLT header.
7591       elfcpp::Swap<32, big_endian>::writeval(pov,
7592                                              plt0_entry[0] | gotplt_addr_high);
7593       elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7594                                              plt0_entry[1] | gotplt_addr_low);
7595       elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7596                                              plt0_entry[2] | gotplt_addr_low);
7597       pov += 12;
7598       for (int i = 3; i < 8; i++)
7599         {
7600           elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7601           pov += 4;
7602         }
7603     }
7604
7605
7606   unsigned char* gotplt_pov = gotplt_view;
7607   unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7608
7609   // The first two entries in .got.plt are reserved.
7610   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7611   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7612
7613   unsigned int gotplt_offset = 2 * got_entry_size;
7614   gotplt_pov += 2 * got_entry_size;
7615
7616   // Calculate the address of the PLT header.
7617   Mips_address header_address = (plt_address
7618                                  + (this->is_plt_header_compressed() ? 1 : 0));
7619
7620   // Initialize compressed PLT area view.
7621   unsigned char* pov2 = pov + this->plt_mips_offset_;
7622
7623   // Write the PLT entries.
7624   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7625        p = this->symbols_.begin();
7626        p != this->symbols_.end();
7627        ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7628     {
7629       Mips_symbol<size>* mips_sym = *p;
7630
7631       // Calculate the address of the .got.plt entry.
7632       uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7633       uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7634                                        & 0xffff);
7635       uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7636
7637       // Initially point the .got.plt entry at the PLT header.
7638       if (this->target_->is_output_n64())
7639         elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7640       else
7641         elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7642
7643       // Now handle the PLT itself.  First the standard entry.
7644       if (mips_sym->has_mips_plt_offset())
7645         {
7646           // Pick the load opcode (LW or LD).
7647           uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7648                                                          : 0x8c000000;
7649
7650           const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7651                                                                 : plt_entry;
7652
7653           // Fill in the PLT entry itself.
7654           elfcpp::Swap<32, big_endian>::writeval(pov,
7655               entry[0] | gotplt_entry_addr_hi);
7656           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7657               entry[1] | gotplt_entry_addr_lo | load);
7658           elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
7659           elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7660               entry[3] | gotplt_entry_addr_lo);
7661           pov += 16;
7662         }
7663
7664       // Now the compressed entry.  They come after any standard ones.
7665       if (mips_sym->has_comp_plt_offset())
7666         {
7667           if (!this->target_->is_output_micromips())
7668             {
7669               // Write MIPS16 PLT entry.
7670               const uint32_t* plt_entry = plt_entry_mips16_o32;
7671
7672               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7673               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7674               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7675               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7676               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7677               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7678               elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7679                                                      gotplt_entry_addr);
7680               pov2 += 16;
7681             }
7682           else if (this->target_->use_32bit_micromips_instructions())
7683             {
7684               // Write microMIPS PLT entry in insn32 mode.
7685               const uint32_t* plt_entry = plt_entry_micromips32_o32;
7686
7687               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7688               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7689                                                      gotplt_entry_addr_hi);
7690               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7691               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7692                                                      gotplt_entry_addr_lo);
7693               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7694               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7695               elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7696               elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7697                                                      gotplt_entry_addr_lo);
7698               pov2 += 16;
7699             }
7700           else
7701             {
7702               // Write microMIPS PLT entry.
7703               const uint32_t* plt_entry = plt_entry_micromips_o32;
7704
7705               gold_assert(gotplt_entry_addr % 4 == 0);
7706
7707               Mips_address loc_address = plt_address + pov2 - oview;
7708               int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7709
7710               // ADDIUPC has a span of +/-16MB, check we're in range.
7711               if (gotpc_offset + 0x1000000 >= 0x2000000)
7712                 {
7713                   gold_error(_(".got.plt offset of %ld from .plt beyond the "
7714                              "range of ADDIUPC"), (long)gotpc_offset);
7715                   return;
7716                 }
7717
7718               elfcpp::Swap<16, big_endian>::writeval(pov2,
7719                           plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7720               elfcpp::Swap<16, big_endian>::writeval(
7721                   pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7722               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7723               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7724               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7725               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7726               pov2 += 12;
7727             }
7728         }
7729     }
7730
7731   // Check the number of bytes written for standard entries.
7732   gold_assert(static_cast<section_size_type>(
7733       pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7734   // Check the number of bytes written for compressed entries.
7735   gold_assert((static_cast<section_size_type>(pov2 - pov)
7736                == this->plt_comp_offset_));
7737   // Check the total number of bytes written.
7738   gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7739
7740   gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7741               == gotplt_size);
7742
7743   of->write_output_view(offset, oview_size, oview);
7744   of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7745 }
7746
7747 // Mips_output_data_mips_stubs methods.
7748
7749 // The format of the lazy binding stub when dynamic symbol count is less than
7750 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7751 template<int size, bool big_endian>
7752 const uint32_t
7753 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7754 {
7755   0x8f998010,         // lw t9,0x8010(gp)
7756   0x03e07825,         // or t7,ra,zero
7757   0x0320f809,         // jalr t9,ra
7758   0x24180000          // addiu t8,zero,DYN_INDEX sign extended
7759 };
7760
7761 // The format of the lazy binding stub when dynamic symbol count is less than
7762 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7763 template<int size, bool big_endian>
7764 const uint32_t
7765 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7766 {
7767   0xdf998010,         // ld t9,0x8010(gp)
7768   0x03e07825,         // or t7,ra,zero
7769   0x0320f809,         // jalr t9,ra
7770   0x64180000          // daddiu t8,zero,DYN_INDEX sign extended
7771 };
7772
7773 // The format of the lazy binding stub when dynamic symbol count is less than
7774 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7775 template<int size, bool big_endian>
7776 const uint32_t
7777 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7778 {
7779   0x8f998010,         // lw t9,0x8010(gp)
7780   0x03e07825,         // or t7,ra,zero
7781   0x0320f809,         // jalr t9,ra
7782   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7783 };
7784
7785 // The format of the lazy binding stub when dynamic symbol count is less than
7786 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7787 template<int size, bool big_endian>
7788 const uint32_t
7789 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7790 {
7791   0xdf998010,         // ld t9,0x8010(gp)
7792   0x03e07825,         // or t7,ra,zero
7793   0x0320f809,         // jalr t9,ra
7794   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7795 };
7796
7797 // The format of the lazy binding stub when dynamic symbol count is greater than
7798 // 64K, and ABI is not N64.
7799 template<int size, bool big_endian>
7800 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7801 {
7802   0x8f998010,         // lw t9,0x8010(gp)
7803   0x03e07825,         // or t7,ra,zero
7804   0x3c180000,         // lui t8,DYN_INDEX
7805   0x0320f809,         // jalr t9,ra
7806   0x37180000          // ori t8,t8,DYN_INDEX
7807 };
7808
7809 // The format of the lazy binding stub when dynamic symbol count is greater than
7810 // 64K, and ABI is N64.
7811 template<int size, bool big_endian>
7812 const uint32_t
7813 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7814 {
7815   0xdf998010,         // ld t9,0x8010(gp)
7816   0x03e07825,         // or t7,ra,zero
7817   0x3c180000,         // lui t8,DYN_INDEX
7818   0x0320f809,         // jalr t9,ra
7819   0x37180000          // ori t8,t8,DYN_INDEX
7820 };
7821
7822 // microMIPS stubs.
7823
7824 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7825 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7826 template<int size, bool big_endian>
7827 const uint32_t
7828 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7829 {
7830   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7831   0x0dff,             // move t7,ra
7832   0x45d9,             // jalr t9
7833   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7834 };
7835
7836 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7837 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7838 template<int size, bool big_endian>
7839 const uint32_t
7840 Mips_output_data_mips_stubs<size, big_endian>::
7841 lazy_stub_micromips_normal_1_n64[] =
7842 {
7843   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7844   0x0dff,             // move t7,ra
7845   0x45d9,             // jalr t9
7846   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7847 };
7848
7849 // The format of the microMIPS lazy binding stub when dynamic symbol
7850 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7851 // and ABI is not N64.
7852 template<int size, bool big_endian>
7853 const uint32_t
7854 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7855 {
7856   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7857   0x0dff,             // move t7,ra
7858   0x45d9,             // jalr t9
7859   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7860 };
7861
7862 // The format of the microMIPS lazy binding stub when dynamic symbol
7863 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7864 // and ABI is N64.
7865 template<int size, bool big_endian>
7866 const uint32_t
7867 Mips_output_data_mips_stubs<size, big_endian>::
7868 lazy_stub_micromips_normal_2_n64[] =
7869 {
7870   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7871   0x0dff,             // move t7,ra
7872   0x45d9,             // jalr t9
7873   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7874 };
7875
7876 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7877 // greater than 64K, and ABI is not N64.
7878 template<int size, bool big_endian>
7879 const uint32_t
7880 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7881 {
7882   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7883   0x0dff,             // move t7,ra
7884   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7885   0x45d9,             // jalr t9
7886   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7887 };
7888
7889 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7890 // greater than 64K, and ABI is N64.
7891 template<int size, bool big_endian>
7892 const uint32_t
7893 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7894 {
7895   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7896   0x0dff,             // move t7,ra
7897   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7898   0x45d9,             // jalr t9
7899   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7900 };
7901
7902 // 32-bit microMIPS stubs.
7903
7904 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7905 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7906 // can use only 32-bit instructions.
7907 template<int size, bool big_endian>
7908 const uint32_t
7909 Mips_output_data_mips_stubs<size, big_endian>::
7910 lazy_stub_micromips32_normal_1[] =
7911 {
7912   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7913   0x001f, 0x7a90,     // or t7,ra,zero
7914   0x03f9, 0x0f3c,     // jalr ra,t9
7915   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7916 };
7917
7918 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7919 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7920 // use only 32-bit instructions.
7921 template<int size, bool big_endian>
7922 const uint32_t
7923 Mips_output_data_mips_stubs<size, big_endian>::
7924 lazy_stub_micromips32_normal_1_n64[] =
7925 {
7926   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7927   0x001f, 0x7a90,     // or t7,ra,zero
7928   0x03f9, 0x0f3c,     // jalr ra,t9
7929   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7930 };
7931
7932 // The format of the microMIPS lazy binding stub when dynamic symbol
7933 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7934 // ABI is not N64, and we can use only 32-bit instructions.
7935 template<int size, bool big_endian>
7936 const uint32_t
7937 Mips_output_data_mips_stubs<size, big_endian>::
7938 lazy_stub_micromips32_normal_2[] =
7939 {
7940   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7941   0x001f, 0x7a90,     // or t7,ra,zero
7942   0x03f9, 0x0f3c,     // jalr ra,t9
7943   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7944 };
7945
7946 // The format of the microMIPS lazy binding stub when dynamic symbol
7947 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7948 // ABI is N64, and we can use only 32-bit instructions.
7949 template<int size, bool big_endian>
7950 const uint32_t
7951 Mips_output_data_mips_stubs<size, big_endian>::
7952 lazy_stub_micromips32_normal_2_n64[] =
7953 {
7954   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7955   0x001f, 0x7a90,     // or t7,ra,zero
7956   0x03f9, 0x0f3c,     // jalr ra,t9
7957   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7958 };
7959
7960 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7961 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7962 template<int size, bool big_endian>
7963 const uint32_t
7964 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7965 {
7966   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7967   0x001f, 0x7a90,     // or t7,ra,zero
7968   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7969   0x03f9, 0x0f3c,     // jalr ra,t9
7970   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7971 };
7972
7973 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7974 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7975 template<int size, bool big_endian>
7976 const uint32_t
7977 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
7978 {
7979   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7980   0x001f, 0x7a90,     // or t7,ra,zero
7981   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7982   0x03f9, 0x0f3c,     // jalr ra,t9
7983   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7984 };
7985
7986 // Create entry for a symbol.
7987
7988 template<int size, bool big_endian>
7989 void
7990 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7991     Mips_symbol<size>* gsym)
7992 {
7993   if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7994     {
7995       this->symbols_.insert(gsym);
7996       gsym->set_has_lazy_stub(true);
7997     }
7998 }
7999
8000 // Remove entry for a symbol.
8001
8002 template<int size, bool big_endian>
8003 void
8004 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8005     Mips_symbol<size>* gsym)
8006 {
8007   if (gsym->has_lazy_stub())
8008     {
8009       this->symbols_.erase(gsym);
8010       gsym->set_has_lazy_stub(false);
8011     }
8012 }
8013
8014 // Set stub offsets for symbols.  This method expects that the number of
8015 // entries in dynamic symbol table is set.
8016
8017 template<int size, bool big_endian>
8018 void
8019 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8020 {
8021   gold_assert(this->dynsym_count_ != -1U);
8022
8023   if (this->stub_offsets_are_set_)
8024     return;
8025
8026   unsigned int stub_size = this->stub_size();
8027   unsigned int offset = 0;
8028   for (typename Mips_stubs_entry_set::const_iterator
8029        p = this->symbols_.begin();
8030        p != this->symbols_.end();
8031        ++p, offset += stub_size)
8032     {
8033       Mips_symbol<size>* mips_sym = *p;
8034       mips_sym->set_lazy_stub_offset(offset);
8035     }
8036   this->stub_offsets_are_set_ = true;
8037 }
8038
8039 template<int size, bool big_endian>
8040 void
8041 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8042 {
8043   for (typename Mips_stubs_entry_set::const_iterator
8044        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8045     {
8046       Mips_symbol<size>* sym = *p;
8047       if (sym->is_from_dynobj())
8048         sym->set_needs_dynsym_value();
8049     }
8050 }
8051
8052 // Write out the .MIPS.stubs.  This uses the hand-coded instructions and
8053 // adjusts them as needed.
8054
8055 template<int size, bool big_endian>
8056 void
8057 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
8058 {
8059   const off_t offset = this->offset();
8060   const section_size_type oview_size =
8061     convert_to_section_size_type(this->data_size());
8062   unsigned char* const oview = of->get_output_view(offset, oview_size);
8063
8064   bool big_stub = this->dynsym_count_ > 0x10000;
8065
8066   unsigned char* pov = oview;
8067   for (typename Mips_stubs_entry_set::const_iterator
8068        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8069     {
8070       Mips_symbol<size>* sym = *p;
8071       const uint32_t* lazy_stub;
8072       bool n64 = this->target_->is_output_n64();
8073
8074       if (!this->target_->is_output_micromips())
8075         {
8076           // Write standard (non-microMIPS) stub.
8077           if (!big_stub)
8078             {
8079               if (sym->dynsym_index() & ~0x7fff)
8080                 // Dynsym index is between 32K and 64K.
8081                 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
8082               else
8083                 // Dynsym index is less than 32K.
8084                 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8085             }
8086           else
8087             lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8088
8089           unsigned int i = 0;
8090           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8091           elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8092           pov += 8;
8093
8094           i += 2;
8095           if (big_stub)
8096             {
8097               // LUI instruction of the big stub.  Paste high 16 bits of the
8098               // dynsym index.
8099               elfcpp::Swap<32, big_endian>::writeval(pov,
8100                   lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
8101               pov += 4;
8102               i += 1;
8103             }
8104           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8105           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8106           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8107               lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8108           pov += 8;
8109         }
8110       else if (this->target_->use_32bit_micromips_instructions())
8111         {
8112           // Write microMIPS stub in insn32 mode.
8113           if (!big_stub)
8114             {
8115               if (sym->dynsym_index() & ~0x7fff)
8116                 // Dynsym index is between 32K and 64K.
8117                 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8118                                 : lazy_stub_micromips32_normal_2;
8119               else
8120                 // Dynsym index is less than 32K.
8121                 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8122                                 : lazy_stub_micromips32_normal_1;
8123             }
8124           else
8125             lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8126                             : lazy_stub_micromips32_big;
8127
8128           unsigned int i = 0;
8129           // First stub instruction.  We emit 32-bit microMIPS instructions by
8130           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8131           // the instruction where the opcode is must always come first, for
8132           // both little and big endian.
8133           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8134           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8135           // Second stub instruction.
8136           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8137           elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8138           pov += 8;
8139           i += 4;
8140           if (big_stub)
8141             {
8142               // LUI instruction of the big stub.  Paste high 16 bits of the
8143               // dynsym index.
8144               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8145               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8146                   (sym->dynsym_index() >> 16) & 0x7fff);
8147               pov += 4;
8148               i += 2;
8149             }
8150           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8151           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8152           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8153           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8154           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8155               sym->dynsym_index() & 0xffff);
8156           pov += 8;
8157         }
8158       else
8159         {
8160           // Write microMIPS stub.
8161           if (!big_stub)
8162             {
8163               if (sym->dynsym_index() & ~0x7fff)
8164                 // Dynsym index is between 32K and 64K.
8165                 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8166                                 : lazy_stub_micromips_normal_2;
8167               else
8168                 // Dynsym index is less than 32K.
8169                 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8170                                 : lazy_stub_micromips_normal_1;
8171             }
8172           else
8173             lazy_stub = n64 ? lazy_stub_micromips_big_n64
8174                             : lazy_stub_micromips_big;
8175
8176           unsigned int i = 0;
8177           // First stub instruction.  We emit 32-bit microMIPS instructions by
8178           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8179           // the instruction where the opcode is must always come first, for
8180           // both little and big endian.
8181           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8182           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8183           // Second stub instruction.
8184           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8185           pov += 6;
8186           i += 3;
8187           if (big_stub)
8188             {
8189               // LUI instruction of the big stub.  Paste high 16 bits of the
8190               // dynsym index.
8191               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8192               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8193                   (sym->dynsym_index() >> 16) & 0x7fff);
8194               pov += 4;
8195               i += 2;
8196             }
8197           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8198           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8199           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8200           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8201               sym->dynsym_index() & 0xffff);
8202           pov += 6;
8203         }
8204     }
8205
8206   // We always allocate 20 bytes for every stub, because final dynsym count is
8207   // not known in method do_finalize_sections.  There are 4 unused bytes per
8208   // stub if final dynsym count is less than 0x10000.
8209   unsigned int used = pov - oview;
8210   unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8211   gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8212
8213   // Fill the unused space with zeroes.
8214   // TODO(sasa): Can we strip unused bytes during the relaxation?
8215   if (unused > 0)
8216     memset(pov, 0, unused);
8217
8218   of->write_output_view(offset, oview_size, oview);
8219 }
8220
8221 // Mips_output_section_reginfo methods.
8222
8223 template<int size, bool big_endian>
8224 void
8225 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8226 {
8227   off_t offset = this->offset();
8228   off_t data_size = this->data_size();
8229
8230   unsigned char* view = of->get_output_view(offset, data_size);
8231   elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8232   elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8233   elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8234   elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8235   elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8236   // Write the gp value.
8237   elfcpp::Swap<size, big_endian>::writeval(view + 20,
8238                                            this->target_->gp_value());
8239
8240   of->write_output_view(offset, data_size, view);
8241 }
8242
8243 // Mips_output_section_options methods.
8244
8245 template<int size, bool big_endian>
8246 void
8247 Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
8248 {
8249   off_t offset = this->offset();
8250   const section_size_type oview_size =
8251     convert_to_section_size_type(this->data_size());
8252   unsigned char* view = of->get_output_view(offset, oview_size);
8253   const unsigned char* end = view + oview_size;
8254
8255   while (view + 8 <= end)
8256     {
8257       unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8258       unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8259       if (sz < 8)
8260         {
8261           gold_error(_("Warning: bad `%s' option size %u smaller "
8262                        "than its header in output section"),
8263                      this->name(), sz);
8264           break;
8265         }
8266
8267       // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
8268       if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
8269         elfcpp::Swap<size, big_endian>::writeval(view + 32,
8270                                                  this->target_->gp_value());
8271       else if (kind == elfcpp::ODK_REGINFO)
8272         elfcpp::Swap<size, big_endian>::writeval(view + 28,
8273                                                  this->target_->gp_value());
8274
8275       view += sz;
8276     }
8277
8278   of->write_output_view(offset, oview_size, view);
8279 }
8280
8281 // Mips_output_section_abiflags methods.
8282
8283 template<int size, bool big_endian>
8284 void
8285 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8286 {
8287   off_t offset = this->offset();
8288   off_t data_size = this->data_size();
8289
8290   unsigned char* view = of->get_output_view(offset, data_size);
8291   elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8292   elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8293   elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8294   elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8295   elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8296   elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8297   elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8298   elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8299   elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8300   elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8301   elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8302
8303   of->write_output_view(offset, data_size, view);
8304 }
8305
8306 // Mips_copy_relocs methods.
8307
8308 // Emit any saved relocs.
8309
8310 template<int sh_type, int size, bool big_endian>
8311 void
8312 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8313     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8314     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8315 {
8316   for (typename Copy_relocs<sh_type, size, big_endian>::
8317        Copy_reloc_entries::iterator p = this->entries_.begin();
8318        p != this->entries_.end();
8319        ++p)
8320     emit_entry(*p, reloc_section, symtab, layout, target);
8321
8322   // We no longer need the saved information.
8323   this->entries_.clear();
8324 }
8325
8326 // Emit the reloc if appropriate.
8327
8328 template<int sh_type, int size, bool big_endian>
8329 void
8330 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8331     Copy_reloc_entry& entry,
8332     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8333     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8334 {
8335   // If the symbol is no longer defined in a dynamic object, then we
8336   // emitted a COPY relocation, and we do not want to emit this
8337   // dynamic relocation.
8338   if (!entry.sym_->is_from_dynobj())
8339     return;
8340
8341   bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8342                            || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8343                            || entry.reloc_type_ == elfcpp::R_MIPS_64);
8344
8345   Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8346   if (can_make_dynamic && !sym->has_static_relocs())
8347     {
8348       Mips_relobj<size, big_endian>* object =
8349         Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8350       target->got_section(symtab, layout)->record_global_got_symbol(
8351                           sym, object, entry.reloc_type_, true, false);
8352       if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8353         target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8354             entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8355       else
8356         target->rel_dyn_section(layout)->add_symbolless_global_addend(
8357             sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8358             entry.shndx_, entry.address_);
8359     }
8360   else
8361     this->make_copy_reloc(symtab, layout,
8362                           static_cast<Sized_symbol<size>*>(entry.sym_),
8363                           entry.relobj_,
8364                           reloc_section);
8365 }
8366
8367 // Target_mips methods.
8368
8369 // Return the value to use for a dynamic symbol which requires special
8370 // treatment.  This is how we support equality comparisons of function
8371 // pointers across shared library boundaries, as described in the
8372 // processor specific ABI supplement.
8373
8374 template<int size, bool big_endian>
8375 uint64_t
8376 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8377 {
8378   uint64_t value = 0;
8379   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8380
8381   if (!mips_sym->has_lazy_stub())
8382     {
8383       if (mips_sym->has_plt_offset())
8384         {
8385           // We distinguish between PLT entries and lazy-binding stubs by
8386           // giving the former an st_other value of STO_MIPS_PLT.  Set the
8387           // value to the stub address if there are any relocations in the
8388           // binary where pointer equality matters.
8389           if (mips_sym->pointer_equality_needed())
8390             {
8391               // Prefer a standard MIPS PLT entry.
8392               if (mips_sym->has_mips_plt_offset())
8393                 value = this->plt_section()->mips_entry_address(mips_sym);
8394               else
8395                 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8396             }
8397           else
8398             value = 0;
8399         }
8400     }
8401   else
8402     {
8403       // First, set stub offsets for symbols.  This method expects that the
8404       // number of entries in dynamic symbol table is set.
8405       this->mips_stubs_section()->set_lazy_stub_offsets();
8406
8407       // The run-time linker uses the st_value field of the symbol
8408       // to reset the global offset table entry for this external
8409       // to its stub address when unlinking a shared object.
8410       value = this->mips_stubs_section()->stub_address(mips_sym);
8411     }
8412
8413   if (mips_sym->has_mips16_fn_stub())
8414     {
8415       // If we have a MIPS16 function with a stub, the dynamic symbol must
8416       // refer to the stub, since only the stub uses the standard calling
8417       // conventions.
8418       value = mips_sym->template
8419               get_mips16_fn_stub<big_endian>()->output_address();
8420     }
8421
8422   return value;
8423 }
8424
8425 // Get the dynamic reloc section, creating it if necessary.  It's always
8426 // .rel.dyn, even for MIPS64.
8427
8428 template<int size, bool big_endian>
8429 typename Target_mips<size, big_endian>::Reloc_section*
8430 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8431 {
8432   if (this->rel_dyn_ == NULL)
8433     {
8434       gold_assert(layout != NULL);
8435       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8436       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8437                                       elfcpp::SHF_ALLOC, this->rel_dyn_,
8438                                       ORDER_DYNAMIC_RELOCS, false);
8439
8440       // First entry in .rel.dyn has to be null.
8441       // This is hack - we define dummy output data and set its address to 0,
8442       // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8443       // This ensures that the entry is null.
8444       Output_data* od = new Output_data_zero_fill(0, 0);
8445       od->set_address(0);
8446       this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8447     }
8448   return this->rel_dyn_;
8449 }
8450
8451 // Get the GOT section, creating it if necessary.
8452
8453 template<int size, bool big_endian>
8454 Mips_output_data_got<size, big_endian>*
8455 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8456                                            Layout* layout)
8457 {
8458   if (this->got_ == NULL)
8459     {
8460       gold_assert(symtab != NULL && layout != NULL);
8461
8462       this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8463                                                               layout);
8464       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8465                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8466                                       elfcpp::SHF_MIPS_GPREL),
8467                                       this->got_, ORDER_DATA, false);
8468
8469       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8470       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8471                                     Symbol_table::PREDEFINED,
8472                                     this->got_,
8473                                     0, 0, elfcpp::STT_OBJECT,
8474                                     elfcpp::STB_GLOBAL,
8475                                     elfcpp::STV_HIDDEN, 0,
8476                                     false, false);
8477     }
8478
8479   return this->got_;
8480 }
8481
8482 // Calculate value of _gp symbol.
8483
8484 template<int size, bool big_endian>
8485 void
8486 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8487 {
8488   gold_assert(this->gp_ == NULL);
8489
8490   Sized_symbol<size>* gp =
8491     static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8492
8493   // Set _gp symbol if the linker script hasn't created it.
8494   if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
8495     {
8496       // If there is no .got section, gp should be based on .sdata.
8497       Output_data* gp_section = (this->got_ != NULL
8498                                  ? this->got_->output_section()
8499                                  : layout->find_output_section(".sdata"));
8500
8501       if (gp_section != NULL)
8502         gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8503                                           "_gp", NULL, Symbol_table::PREDEFINED,
8504                                           gp_section, MIPS_GP_OFFSET, 0,
8505                                           elfcpp::STT_NOTYPE,
8506                                           elfcpp::STB_LOCAL,
8507                                           elfcpp::STV_DEFAULT,
8508                                           0, false, false));
8509     }
8510
8511   this->gp_ = gp;
8512 }
8513
8514 // Set the dynamic symbol indexes.  INDEX is the index of the first
8515 // global dynamic symbol.  Pointers to the symbols are stored into the
8516 // vector SYMS.  The names are added to DYNPOOL.  This returns an
8517 // updated dynamic symbol index.
8518
8519 template<int size, bool big_endian>
8520 unsigned int
8521 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8522     std::vector<Symbol*>* dyn_symbols, unsigned int index,
8523     std::vector<Symbol*>* syms, Stringpool* dynpool,
8524     Versions* versions, Symbol_table* symtab) const
8525 {
8526   std::vector<Symbol*> non_got_symbols;
8527   std::vector<Symbol*> got_symbols;
8528
8529   reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8530                                         &got_symbols);
8531
8532   for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8533        p != non_got_symbols.end();
8534        ++p)
8535     {
8536       Symbol* sym = *p;
8537
8538       // Note that SYM may already have a dynamic symbol index, since
8539       // some symbols appear more than once in the symbol table, with
8540       // and without a version.
8541
8542       if (!sym->has_dynsym_index())
8543         {
8544           sym->set_dynsym_index(index);
8545           ++index;
8546           syms->push_back(sym);
8547           dynpool->add(sym->name(), false, NULL);
8548
8549           // Record any version information.
8550           if (sym->version() != NULL)
8551             versions->record_version(symtab, dynpool, sym);
8552
8553           // If the symbol is defined in a dynamic object and is
8554           // referenced in a regular object, then mark the dynamic
8555           // object as needed.  This is used to implement --as-needed.
8556           if (sym->is_from_dynobj() && sym->in_reg())
8557             sym->object()->set_is_needed();
8558         }
8559     }
8560
8561   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8562        p != got_symbols.end();
8563        ++p)
8564     {
8565       Symbol* sym = *p;
8566       if (!sym->has_dynsym_index())
8567         {
8568           // Record any version information.
8569           if (sym->version() != NULL)
8570             versions->record_version(symtab, dynpool, sym);
8571         }
8572     }
8573
8574   index = versions->finalize(symtab, index, syms);
8575
8576   int got_sym_count = 0;
8577   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8578        p != got_symbols.end();
8579        ++p)
8580     {
8581       Symbol* sym = *p;
8582
8583       if (!sym->has_dynsym_index())
8584         {
8585           ++got_sym_count;
8586           sym->set_dynsym_index(index);
8587           ++index;
8588           syms->push_back(sym);
8589           dynpool->add(sym->name(), false, NULL);
8590
8591           // If the symbol is defined in a dynamic object and is
8592           // referenced in a regular object, then mark the dynamic
8593           // object as needed.  This is used to implement --as-needed.
8594           if (sym->is_from_dynobj() && sym->in_reg())
8595             sym->object()->set_is_needed();
8596         }
8597     }
8598
8599   // Set index of the first symbol that has .got entry.
8600   this->got_->set_first_global_got_dynsym_index(
8601     got_sym_count > 0 ? index - got_sym_count : -1U);
8602
8603   if (this->mips_stubs_ != NULL)
8604     this->mips_stubs_->set_dynsym_count(index);
8605
8606   return index;
8607 }
8608
8609 // Create a PLT entry for a global symbol referenced by r_type relocation.
8610
8611 template<int size, bool big_endian>
8612 void
8613 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8614                                               Layout* layout,
8615                                               Mips_symbol<size>* gsym,
8616                                               unsigned int r_type)
8617 {
8618   if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8619     return;
8620
8621   if (this->plt_ == NULL)
8622     {
8623       // Create the GOT section first.
8624       this->got_section(symtab, layout);
8625
8626       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8627       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8628                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8629                                       this->got_plt_, ORDER_DATA, false);
8630
8631       // The first two entries are reserved.
8632       this->got_plt_->set_current_data_size(2 * size/8);
8633
8634       this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8635                                                               this->got_plt_,
8636                                                               this);
8637       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8638                                       (elfcpp::SHF_ALLOC
8639                                        | elfcpp::SHF_EXECINSTR),
8640                                       this->plt_, ORDER_PLT, false);
8641
8642       // Make the sh_info field of .rel.plt point to .plt.
8643       Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
8644       rel_plt_os->set_info_section(this->plt_->output_section());
8645     }
8646
8647   this->plt_->add_entry(gsym, r_type);
8648 }
8649
8650
8651 // Get the .MIPS.stubs section, creating it if necessary.
8652
8653 template<int size, bool big_endian>
8654 Mips_output_data_mips_stubs<size, big_endian>*
8655 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8656 {
8657   if (this->mips_stubs_ == NULL)
8658     {
8659       this->mips_stubs_ =
8660         new Mips_output_data_mips_stubs<size, big_endian>(this);
8661       layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8662                                       (elfcpp::SHF_ALLOC
8663                                        | elfcpp::SHF_EXECINSTR),
8664                                       this->mips_stubs_, ORDER_PLT, false);
8665     }
8666   return this->mips_stubs_;
8667 }
8668
8669 // Get the LA25 stub section, creating it if necessary.
8670
8671 template<int size, bool big_endian>
8672 Mips_output_data_la25_stub<size, big_endian>*
8673 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8674 {
8675   if (this->la25_stub_ == NULL)
8676     {
8677       this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8678       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8679                                       (elfcpp::SHF_ALLOC
8680                                        | elfcpp::SHF_EXECINSTR),
8681                                       this->la25_stub_, ORDER_TEXT, false);
8682     }
8683   return this->la25_stub_;
8684 }
8685
8686 // Process the relocations to determine unreferenced sections for
8687 // garbage collection.
8688
8689 template<int size, bool big_endian>
8690 void
8691 Target_mips<size, big_endian>::gc_process_relocs(
8692                         Symbol_table* symtab,
8693                         Layout* layout,
8694                         Sized_relobj_file<size, big_endian>* object,
8695                         unsigned int data_shndx,
8696                         unsigned int sh_type,
8697                         const unsigned char* prelocs,
8698                         size_t reloc_count,
8699                         Output_section* output_section,
8700                         bool needs_special_offset_handling,
8701                         size_t local_symbol_count,
8702                         const unsigned char* plocal_symbols)
8703 {
8704   typedef Target_mips<size, big_endian> Mips;
8705
8706   if (sh_type == elfcpp::SHT_REL)
8707     {
8708       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8709           Classify_reloc;
8710
8711       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8712         symtab,
8713         layout,
8714         this,
8715         object,
8716         data_shndx,
8717         prelocs,
8718         reloc_count,
8719         output_section,
8720         needs_special_offset_handling,
8721         local_symbol_count,
8722         plocal_symbols);
8723     }
8724   else if (sh_type == elfcpp::SHT_RELA)
8725     {
8726       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8727           Classify_reloc;
8728
8729       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8730         symtab,
8731         layout,
8732         this,
8733         object,
8734         data_shndx,
8735         prelocs,
8736         reloc_count,
8737         output_section,
8738         needs_special_offset_handling,
8739         local_symbol_count,
8740         plocal_symbols);
8741     }
8742   else
8743     gold_unreachable();
8744 }
8745
8746 // Scan relocations for a section.
8747
8748 template<int size, bool big_endian>
8749 void
8750 Target_mips<size, big_endian>::scan_relocs(
8751                         Symbol_table* symtab,
8752                         Layout* layout,
8753                         Sized_relobj_file<size, big_endian>* object,
8754                         unsigned int data_shndx,
8755                         unsigned int sh_type,
8756                         const unsigned char* prelocs,
8757                         size_t reloc_count,
8758                         Output_section* output_section,
8759                         bool needs_special_offset_handling,
8760                         size_t local_symbol_count,
8761                         const unsigned char* plocal_symbols)
8762 {
8763   typedef Target_mips<size, big_endian> Mips;
8764
8765   if (sh_type == elfcpp::SHT_REL)
8766     {
8767       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8768           Classify_reloc;
8769
8770       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8771         symtab,
8772         layout,
8773         this,
8774         object,
8775         data_shndx,
8776         prelocs,
8777         reloc_count,
8778         output_section,
8779         needs_special_offset_handling,
8780         local_symbol_count,
8781         plocal_symbols);
8782     }
8783   else if (sh_type == elfcpp::SHT_RELA)
8784     {
8785       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8786           Classify_reloc;
8787
8788       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8789         symtab,
8790         layout,
8791         this,
8792         object,
8793         data_shndx,
8794         prelocs,
8795         reloc_count,
8796         output_section,
8797         needs_special_offset_handling,
8798         local_symbol_count,
8799         plocal_symbols);
8800     }
8801 }
8802
8803 template<int size, bool big_endian>
8804 bool
8805 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8806 {
8807   return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8808           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8809           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8810           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8811           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8812           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8813           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
8814           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
8815 }
8816
8817 // Return the MACH for a MIPS e_flags value.
8818 template<int size, bool big_endian>
8819 unsigned int
8820 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8821 {
8822   switch (flags & elfcpp::EF_MIPS_MACH)
8823     {
8824     case elfcpp::E_MIPS_MACH_3900:
8825       return mach_mips3900;
8826
8827     case elfcpp::E_MIPS_MACH_4010:
8828       return mach_mips4010;
8829
8830     case elfcpp::E_MIPS_MACH_4100:
8831       return mach_mips4100;
8832
8833     case elfcpp::E_MIPS_MACH_4111:
8834       return mach_mips4111;
8835
8836     case elfcpp::E_MIPS_MACH_4120:
8837       return mach_mips4120;
8838
8839     case elfcpp::E_MIPS_MACH_4650:
8840       return mach_mips4650;
8841
8842     case elfcpp::E_MIPS_MACH_5400:
8843       return mach_mips5400;
8844
8845     case elfcpp::E_MIPS_MACH_5500:
8846       return mach_mips5500;
8847
8848     case elfcpp::E_MIPS_MACH_5900:
8849       return mach_mips5900;
8850
8851     case elfcpp::E_MIPS_MACH_9000:
8852       return mach_mips9000;
8853
8854     case elfcpp::E_MIPS_MACH_SB1:
8855       return mach_mips_sb1;
8856
8857     case elfcpp::E_MIPS_MACH_LS2E:
8858       return mach_mips_loongson_2e;
8859
8860     case elfcpp::E_MIPS_MACH_LS2F:
8861       return mach_mips_loongson_2f;
8862
8863     case elfcpp::E_MIPS_MACH_GS464:
8864       return mach_mips_gs464;
8865
8866     case elfcpp::E_MIPS_MACH_GS464E:
8867       return mach_mips_gs464e;
8868
8869     case elfcpp::E_MIPS_MACH_OCTEON3:
8870       return mach_mips_octeon3;
8871
8872     case elfcpp::E_MIPS_MACH_OCTEON2:
8873       return mach_mips_octeon2;
8874
8875     case elfcpp::E_MIPS_MACH_OCTEON:
8876       return mach_mips_octeon;
8877
8878     case elfcpp::E_MIPS_MACH_XLR:
8879       return mach_mips_xlr;
8880
8881     default:
8882       switch (flags & elfcpp::EF_MIPS_ARCH)
8883         {
8884         default:
8885         case elfcpp::E_MIPS_ARCH_1:
8886           return mach_mips3000;
8887
8888         case elfcpp::E_MIPS_ARCH_2:
8889           return mach_mips6000;
8890
8891         case elfcpp::E_MIPS_ARCH_3:
8892           return mach_mips4000;
8893
8894         case elfcpp::E_MIPS_ARCH_4:
8895           return mach_mips8000;
8896
8897         case elfcpp::E_MIPS_ARCH_5:
8898           return mach_mips5;
8899
8900         case elfcpp::E_MIPS_ARCH_32:
8901           return mach_mipsisa32;
8902
8903         case elfcpp::E_MIPS_ARCH_64:
8904           return mach_mipsisa64;
8905
8906         case elfcpp::E_MIPS_ARCH_32R2:
8907           return mach_mipsisa32r2;
8908
8909         case elfcpp::E_MIPS_ARCH_32R6:
8910           return mach_mipsisa32r6;
8911
8912         case elfcpp::E_MIPS_ARCH_64R2:
8913           return mach_mipsisa64r2;
8914
8915         case elfcpp::E_MIPS_ARCH_64R6:
8916           return mach_mipsisa64r6;
8917         }
8918     }
8919
8920   return 0;
8921 }
8922
8923 // Return the MACH for each .MIPS.abiflags ISA Extension.
8924
8925 template<int size, bool big_endian>
8926 unsigned int
8927 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8928 {
8929   switch (isa_ext)
8930     {
8931     case elfcpp::AFL_EXT_3900:
8932       return mach_mips3900;
8933
8934     case elfcpp::AFL_EXT_4010:
8935       return mach_mips4010;
8936
8937     case elfcpp::AFL_EXT_4100:
8938       return mach_mips4100;
8939
8940     case elfcpp::AFL_EXT_4111:
8941       return mach_mips4111;
8942
8943     case elfcpp::AFL_EXT_4120:
8944       return mach_mips4120;
8945
8946     case elfcpp::AFL_EXT_4650:
8947       return mach_mips4650;
8948
8949     case elfcpp::AFL_EXT_5400:
8950       return mach_mips5400;
8951
8952     case elfcpp::AFL_EXT_5500:
8953       return mach_mips5500;
8954
8955     case elfcpp::AFL_EXT_5900:
8956       return mach_mips5900;
8957
8958     case elfcpp::AFL_EXT_10000:
8959       return mach_mips10000;
8960
8961     case elfcpp::AFL_EXT_LOONGSON_2E:
8962       return mach_mips_loongson_2e;
8963
8964     case elfcpp::AFL_EXT_LOONGSON_2F:
8965       return mach_mips_loongson_2f;
8966
8967     case elfcpp::AFL_EXT_SB1:
8968       return mach_mips_sb1;
8969
8970     case elfcpp::AFL_EXT_OCTEON:
8971       return mach_mips_octeon;
8972
8973     case elfcpp::AFL_EXT_OCTEONP:
8974       return mach_mips_octeonp;
8975
8976     case elfcpp::AFL_EXT_OCTEON2:
8977       return mach_mips_octeon2;
8978
8979     case elfcpp::AFL_EXT_XLR:
8980       return mach_mips_xlr;
8981
8982     default:
8983       return mach_mips3000;
8984     }
8985 }
8986
8987 // Return the .MIPS.abiflags value representing each ISA Extension.
8988
8989 template<int size, bool big_endian>
8990 unsigned int
8991 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
8992 {
8993   switch (mips_mach)
8994     {
8995     case mach_mips3900:
8996       return elfcpp::AFL_EXT_3900;
8997
8998     case mach_mips4010:
8999       return elfcpp::AFL_EXT_4010;
9000
9001     case mach_mips4100:
9002       return elfcpp::AFL_EXT_4100;
9003
9004     case mach_mips4111:
9005       return elfcpp::AFL_EXT_4111;
9006
9007     case mach_mips4120:
9008       return elfcpp::AFL_EXT_4120;
9009
9010     case mach_mips4650:
9011       return elfcpp::AFL_EXT_4650;
9012
9013     case mach_mips5400:
9014       return elfcpp::AFL_EXT_5400;
9015
9016     case mach_mips5500:
9017       return elfcpp::AFL_EXT_5500;
9018
9019     case mach_mips5900:
9020       return elfcpp::AFL_EXT_5900;
9021
9022     case mach_mips10000:
9023       return elfcpp::AFL_EXT_10000;
9024
9025     case mach_mips_loongson_2e:
9026       return elfcpp::AFL_EXT_LOONGSON_2E;
9027
9028     case mach_mips_loongson_2f:
9029       return elfcpp::AFL_EXT_LOONGSON_2F;
9030
9031     case mach_mips_sb1:
9032       return elfcpp::AFL_EXT_SB1;
9033
9034     case mach_mips_octeon:
9035       return elfcpp::AFL_EXT_OCTEON;
9036
9037     case mach_mips_octeonp:
9038       return elfcpp::AFL_EXT_OCTEONP;
9039
9040     case mach_mips_octeon3:
9041       return elfcpp::AFL_EXT_OCTEON3;
9042
9043     case mach_mips_octeon2:
9044       return elfcpp::AFL_EXT_OCTEON2;
9045
9046     case mach_mips_xlr:
9047       return elfcpp::AFL_EXT_XLR;
9048
9049     default:
9050       return 0;
9051     }
9052 }
9053
9054 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
9055
9056 template<int size, bool big_endian>
9057 void
9058 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9059     elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9060 {
9061   int new_isa = 0;
9062   switch (e_flags & elfcpp::EF_MIPS_ARCH)
9063     {
9064     case elfcpp::E_MIPS_ARCH_1:
9065       new_isa = this->level_rev(1, 0);
9066       break;
9067     case elfcpp::E_MIPS_ARCH_2:
9068       new_isa = this->level_rev(2, 0);
9069       break;
9070     case elfcpp::E_MIPS_ARCH_3:
9071       new_isa = this->level_rev(3, 0);
9072       break;
9073     case elfcpp::E_MIPS_ARCH_4:
9074       new_isa = this->level_rev(4, 0);
9075       break;
9076     case elfcpp::E_MIPS_ARCH_5:
9077       new_isa = this->level_rev(5, 0);
9078       break;
9079     case elfcpp::E_MIPS_ARCH_32:
9080       new_isa = this->level_rev(32, 1);
9081       break;
9082     case elfcpp::E_MIPS_ARCH_32R2:
9083       new_isa = this->level_rev(32, 2);
9084       break;
9085     case elfcpp::E_MIPS_ARCH_32R6:
9086       new_isa = this->level_rev(32, 6);
9087       break;
9088     case elfcpp::E_MIPS_ARCH_64:
9089       new_isa = this->level_rev(64, 1);
9090       break;
9091     case elfcpp::E_MIPS_ARCH_64R2:
9092       new_isa = this->level_rev(64, 2);
9093       break;
9094     case elfcpp::E_MIPS_ARCH_64R6:
9095       new_isa = this->level_rev(64, 6);
9096       break;
9097     default:
9098       gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9099                  this->elf_mips_mach_name(e_flags));
9100     }
9101
9102   if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9103     {
9104       // Decode a single value into level and revision.
9105       abiflags->isa_level = new_isa >> 3;
9106       abiflags->isa_rev = new_isa & 0x7;
9107     }
9108
9109   // Update the isa_ext if needed.
9110   if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9111       this->elf_mips_mach(e_flags)))
9112     abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9113 }
9114
9115 // Infer the content of the ABI flags based on the elf header.
9116
9117 template<int size, bool big_endian>
9118 void
9119 Target_mips<size, big_endian>::infer_abiflags(
9120     Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9121 {
9122   const Attributes_section_data* pasd = relobj->attributes_section_data();
9123   int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9124   elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9125
9126   this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9127   if (pasd != NULL)
9128     {
9129       // Read fp_abi from the .gnu.attribute section.
9130       const Object_attribute* attr =
9131         pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9132       attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9133     }
9134
9135   abiflags->fp_abi = attr_fp_abi;
9136   abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9137   abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9138   abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9139                                                        : elfcpp::AFL_REG_64;
9140
9141   if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9142       || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9143       || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9144       && abiflags->gpr_size == elfcpp::AFL_REG_32))
9145     abiflags->cpr1_size = elfcpp::AFL_REG_32;
9146   else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9147            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9148            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9149     abiflags->cpr1_size = elfcpp::AFL_REG_64;
9150
9151   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9152     abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9153   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9154     abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9155   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9156     abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9157
9158   if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9159       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9160       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9161       && abiflags->isa_level >= 32
9162       && abiflags->ases != elfcpp::AFL_ASE_LOONGSON_EXT)
9163     abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9164 }
9165
9166 // Create abiflags from elf header or from .MIPS.abiflags section.
9167
9168 template<int size, bool big_endian>
9169 void
9170 Target_mips<size, big_endian>::create_abiflags(
9171     Mips_relobj<size, big_endian>* relobj,
9172     Mips_abiflags<big_endian>* abiflags)
9173 {
9174   Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9175   Mips_abiflags<big_endian> header_abiflags;
9176
9177   this->infer_abiflags(relobj, &header_abiflags);
9178
9179   if (sec_abiflags == NULL)
9180     {
9181       // If there is no input .MIPS.abiflags section, use abiflags created
9182       // from elf header.
9183       *abiflags = header_abiflags;
9184       return;
9185     }
9186
9187   this->has_abiflags_section_ = true;
9188
9189   // It is not possible to infer the correct ISA revision for R3 or R5
9190   // so drop down to R2 for the checks.
9191   unsigned char isa_rev = sec_abiflags->isa_rev;
9192   if (isa_rev == 3 || isa_rev == 5)
9193     isa_rev = 2;
9194
9195   // Check compatibility between abiflags created from elf header
9196   // and abiflags from .MIPS.abiflags section in this object file.
9197   if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9198       < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9199     gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9200                  relobj->name().c_str());
9201   if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9202       && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9203     gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9204                    ".MIPS.abiflags"), relobj->name().c_str());
9205   if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9206     gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9207                  relobj->name().c_str());
9208   // The isa_ext is allowed to be an extension of what can be inferred
9209   // from e_flags.
9210   if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9211                                this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9212     gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9213                    ".MIPS.abiflags"), relobj->name().c_str());
9214   if (sec_abiflags->flags2 != 0)
9215     gold_warning(_("%s: Unexpected flag in the flags2 field of "
9216                    ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9217                                              sec_abiflags->flags2);
9218   // Use abiflags from .MIPS.abiflags section.
9219   *abiflags = *sec_abiflags;
9220 }
9221
9222 // Return the meaning of fp_abi, or "unknown" if not known.
9223
9224 template<int size, bool big_endian>
9225 const char*
9226 Target_mips<size, big_endian>::fp_abi_string(int fp)
9227 {
9228   switch (fp)
9229     {
9230     case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9231       return "-mdouble-float";
9232     case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9233       return "-msingle-float";
9234     case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9235       return "-msoft-float";
9236     case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9237       return _("-mips32r2 -mfp64 (12 callee-saved)");
9238     case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9239       return "-mfpxx";
9240     case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9241       return "-mgp32 -mfp64";
9242     case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9243       return "-mgp32 -mfp64 -mno-odd-spreg";
9244     default:
9245       return "unknown";
9246     }
9247 }
9248
9249 // Select fp_abi.
9250
9251 template<int size, bool big_endian>
9252 int
9253 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9254                                              int out_fp)
9255 {
9256   if (in_fp == out_fp)
9257     return out_fp;
9258
9259   if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9260     return in_fp;
9261   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9262            && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9263                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9264                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9265     return in_fp;
9266   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9267            && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9268                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9269                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9270     return out_fp; // Keep the current setting.
9271   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9272            && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9273     return in_fp;
9274   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9275            && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9276     return out_fp; // Keep the current setting.
9277   else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9278     gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9279                  fp_abi_string(in_fp), fp_abi_string(out_fp));
9280   return out_fp;
9281 }
9282
9283 // Merge attributes from input object.
9284
9285 template<int size, bool big_endian>
9286 void
9287 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9288     const Attributes_section_data* pasd)
9289 {
9290   // Return if there is no attributes section data.
9291   if (pasd == NULL)
9292     return;
9293
9294   // If output has no object attributes, just copy.
9295   if (this->attributes_section_data_ == NULL)
9296     {
9297       this->attributes_section_data_ = new Attributes_section_data(*pasd);
9298       return;
9299     }
9300
9301   Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9302       Object_attribute::OBJ_ATTR_GNU);
9303
9304   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9305   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9306
9307   // Merge Tag_compatibility attributes and any common GNU ones.
9308   this->attributes_section_data_->merge(name.c_str(), pasd);
9309 }
9310
9311 // Merge abiflags from input object.
9312
9313 template<int size, bool big_endian>
9314 void
9315 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9316     Mips_abiflags<big_endian>* in_abiflags)
9317 {
9318   // If output has no abiflags, just copy.
9319   if (this->abiflags_ == NULL)
9320   {
9321     this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9322     return;
9323   }
9324
9325   this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9326                                                 this->abiflags_->fp_abi);
9327
9328   // Merge abiflags.
9329   this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9330                                         in_abiflags->isa_level);
9331   this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9332                                       in_abiflags->isa_rev);
9333   this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9334                                        in_abiflags->gpr_size);
9335   this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9336                                         in_abiflags->cpr1_size);
9337   this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9338                                         in_abiflags->cpr2_size);
9339   this->abiflags_->ases |= in_abiflags->ases;
9340   this->abiflags_->flags1 |= in_abiflags->flags1;
9341 }
9342
9343 // Check whether machine EXTENSION is an extension of machine BASE.
9344 template<int size, bool big_endian>
9345 bool
9346 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9347                                                  unsigned int extension)
9348 {
9349   if (extension == base)
9350     return true;
9351
9352   if ((base == mach_mipsisa32)
9353       && this->mips_mach_extends(mach_mipsisa64, extension))
9354     return true;
9355
9356   if ((base == mach_mipsisa32r2)
9357       && this->mips_mach_extends(mach_mipsisa64r2, extension))
9358     return true;
9359
9360   for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9361     if (extension == this->mips_mach_extensions_[i].first)
9362       {
9363         extension = this->mips_mach_extensions_[i].second;
9364         if (extension == base)
9365           return true;
9366       }
9367
9368   return false;
9369 }
9370
9371 // Merge file header flags from input object.
9372
9373 template<int size, bool big_endian>
9374 void
9375 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9376                                                  elfcpp::Elf_Word in_flags)
9377 {
9378   // If flags are not set yet, just copy them.
9379   if (!this->are_processor_specific_flags_set())
9380     {
9381       this->set_processor_specific_flags(in_flags);
9382       this->mach_ = this->elf_mips_mach(in_flags);
9383       return;
9384     }
9385
9386   elfcpp::Elf_Word new_flags = in_flags;
9387   elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9388   elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9389   merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9390
9391   // Check flag compatibility.
9392   new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9393   old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9394
9395   // Some IRIX 6 BSD-compatibility objects have this bit set.  It
9396   // doesn't seem to matter.
9397   new_flags &= ~elfcpp::EF_MIPS_XGOT;
9398   old_flags &= ~elfcpp::EF_MIPS_XGOT;
9399
9400   // MIPSpro generates ucode info in n64 objects.  Again, we should
9401   // just be able to ignore this.
9402   new_flags &= ~elfcpp::EF_MIPS_UCODE;
9403   old_flags &= ~elfcpp::EF_MIPS_UCODE;
9404
9405   if (new_flags == old_flags)
9406     {
9407       this->set_processor_specific_flags(merged_flags);
9408       return;
9409     }
9410
9411   if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9412       != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9413     gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9414                  name.c_str());
9415
9416   if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9417     merged_flags |= elfcpp::EF_MIPS_CPIC;
9418   if (!(new_flags & elfcpp::EF_MIPS_PIC))
9419     merged_flags &= ~elfcpp::EF_MIPS_PIC;
9420
9421   new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9422   old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9423
9424   // Compare the ISAs.
9425   if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9426     gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9427   else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9428     {
9429       // Output ISA isn't the same as, or an extension of, input ISA.
9430       if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9431         {
9432           // Copy the architecture info from input object to output.  Also copy
9433           // the 32-bit flag (if set) so that we continue to recognise
9434           // output as a 32-bit binary.
9435           this->mach_ = this->elf_mips_mach(in_flags);
9436           merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9437           merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9438                            | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9439
9440           // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9441           this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9442
9443           // Copy across the ABI flags if output doesn't use them
9444           // and if that was what caused us to treat input object as 32-bit.
9445           if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9446               && this->mips_32bit_flags(new_flags)
9447               && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9448             merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9449         }
9450       else
9451         // The ISAs aren't compatible.
9452         gold_error(_("%s: linking %s module with previous %s modules"),
9453                    name.c_str(), this->elf_mips_mach_name(in_flags),
9454                    this->elf_mips_mach_name(merged_flags));
9455     }
9456
9457   new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9458                 | elfcpp::EF_MIPS_32BITMODE));
9459   old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9460                 | elfcpp::EF_MIPS_32BITMODE));
9461
9462   // Compare ABIs.
9463   if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9464     {
9465       // Only error if both are set (to different values).
9466       if ((new_flags & elfcpp::EF_MIPS_ABI)
9467            && (old_flags & elfcpp::EF_MIPS_ABI))
9468         gold_error(_("%s: ABI mismatch: linking %s module with "
9469                      "previous %s modules"), name.c_str(),
9470                    this->elf_mips_abi_name(in_flags),
9471                    this->elf_mips_abi_name(merged_flags));
9472
9473       new_flags &= ~elfcpp::EF_MIPS_ABI;
9474       old_flags &= ~elfcpp::EF_MIPS_ABI;
9475     }
9476
9477   // Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
9478   // and allow arbitrary mixing of the remaining ASEs (retain the union).
9479   if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9480       != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9481     {
9482       int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9483       int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9484       int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9485       int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9486       int micro_mis = old_m16 && new_micro;
9487       int m16_mis = old_micro && new_m16;
9488
9489       if (m16_mis || micro_mis)
9490         gold_error(_("%s: ASE mismatch: linking %s module with "
9491                      "previous %s modules"), name.c_str(),
9492                    m16_mis ? "MIPS16" : "microMIPS",
9493                    m16_mis ? "microMIPS" : "MIPS16");
9494
9495       merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9496
9497       new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9498       old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9499     }
9500
9501   // Compare NaN encodings.
9502   if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9503     {
9504       gold_error(_("%s: linking %s module with previous %s modules"),
9505                  name.c_str(),
9506                  (new_flags & elfcpp::EF_MIPS_NAN2008
9507                   ? "-mnan=2008" : "-mnan=legacy"),
9508                  (old_flags & elfcpp::EF_MIPS_NAN2008
9509                   ? "-mnan=2008" : "-mnan=legacy"));
9510
9511       new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9512       old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9513     }
9514
9515   // Compare FP64 state.
9516   if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9517     {
9518       gold_error(_("%s: linking %s module with previous %s modules"),
9519                  name.c_str(),
9520                  (new_flags & elfcpp::EF_MIPS_FP64
9521                   ? "-mfp64" : "-mfp32"),
9522                  (old_flags & elfcpp::EF_MIPS_FP64
9523                   ? "-mfp64" : "-mfp32"));
9524
9525       new_flags &= ~elfcpp::EF_MIPS_FP64;
9526       old_flags &= ~elfcpp::EF_MIPS_FP64;
9527     }
9528
9529   // Warn about any other mismatches.
9530   if (new_flags != old_flags)
9531     gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9532                  "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9533
9534   this->set_processor_specific_flags(merged_flags);
9535 }
9536
9537 // Adjust ELF file header.
9538
9539 template<int size, bool big_endian>
9540 void
9541 Target_mips<size, big_endian>::do_adjust_elf_header(
9542     unsigned char* view,
9543     int len)
9544 {
9545   gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9546
9547   elfcpp::Ehdr<size, big_endian> ehdr(view);
9548   unsigned char e_ident[elfcpp::EI_NIDENT];
9549   elfcpp::Elf_Word flags = this->processor_specific_flags();
9550   memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9551
9552   unsigned char ei_abiversion = 0;
9553   elfcpp::Elf_Half type = ehdr.get_e_type();
9554   if (type == elfcpp::ET_EXEC
9555       && parameters->options().copyreloc()
9556       && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9557           == elfcpp::EF_MIPS_CPIC)
9558     ei_abiversion = 1;
9559
9560   if (this->abiflags_ != NULL
9561       && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9562           || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9563     ei_abiversion = 3;
9564
9565   e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9566   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9567   oehdr.put_e_ident(e_ident);
9568
9569   if (this->entry_symbol_is_compressed_)
9570     oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9571 }
9572
9573 // do_make_elf_object to override the same function in the base class.
9574 // We need to use a target-specific sub-class of
9575 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9576 // Hence we need to have our own ELF object creation.
9577
9578 template<int size, bool big_endian>
9579 Object*
9580 Target_mips<size, big_endian>::do_make_elf_object(
9581     const std::string& name,
9582     Input_file* input_file,
9583     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9584 {
9585   int et = ehdr.get_e_type();
9586   // ET_EXEC files are valid input for --just-symbols/-R,
9587   // and we treat them as relocatable objects.
9588   if (et == elfcpp::ET_REL
9589       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9590     {
9591       Mips_relobj<size, big_endian>* obj =
9592         new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9593       obj->setup();
9594       return obj;
9595     }
9596   else if (et == elfcpp::ET_DYN)
9597     {
9598       // TODO(sasa): Should we create Mips_dynobj?
9599       return Target::do_make_elf_object(name, input_file, offset, ehdr);
9600     }
9601   else
9602     {
9603       gold_error(_("%s: unsupported ELF file type %d"),
9604                  name.c_str(), et);
9605       return NULL;
9606     }
9607 }
9608
9609 // Finalize the sections.
9610
9611 template <int size, bool big_endian>
9612 void
9613 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9614                                         const Input_objects* input_objects,
9615                                         Symbol_table* symtab)
9616 {
9617   const bool relocatable = parameters->options().relocatable();
9618
9619   // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9620   // DT_FINI have correct values.
9621   Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9622       symtab->lookup(parameters->options().init()));
9623   if (init != NULL && (init->is_mips16() || init->is_micromips()))
9624     init->set_value(init->value() | 1);
9625   Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9626       symtab->lookup(parameters->options().fini()));
9627   if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9628     fini->set_value(fini->value() | 1);
9629
9630   // Check whether the entry symbol is mips16 or micromips.  This is needed to
9631   // adjust entry address in ELF header.
9632   Mips_symbol<size>* entry =
9633     static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9634   this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9635                                        || entry->is_micromips()));
9636
9637   if (!parameters->doing_static_link()
9638       && (strcmp(parameters->options().hash_style(), "gnu") == 0
9639           || strcmp(parameters->options().hash_style(), "both") == 0))
9640     {
9641       // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9642       // ways.  .gnu.hash needs symbols to be grouped by hash code whereas the
9643       // MIPS ABI requires a mapping between the GOT and the symbol table.
9644       gold_error(".gnu.hash is incompatible with the MIPS ABI");
9645     }
9646
9647   // Check whether the final section that was scanned has HI16 or GOT16
9648   // relocations without the corresponding LO16 part.
9649   if (this->got16_addends_.size() > 0)
9650       gold_error("Can't find matching LO16 reloc");
9651
9652   Valtype gprmask = 0;
9653   Valtype cprmask1 = 0;
9654   Valtype cprmask2 = 0;
9655   Valtype cprmask3 = 0;
9656   Valtype cprmask4 = 0;
9657   bool has_reginfo_section = false;
9658
9659   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9660        p != input_objects->relobj_end();
9661        ++p)
9662     {
9663       Mips_relobj<size, big_endian>* relobj =
9664         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9665
9666       // Check for any mips16 stub sections that we can discard.
9667       if (!relocatable)
9668         relobj->discard_mips16_stub_sections(symtab);
9669
9670       if (!relobj->merge_processor_specific_data())
9671         continue;
9672
9673       // Merge .reginfo contents of input objects.
9674       if (relobj->has_reginfo_section())
9675         {
9676           has_reginfo_section = true;
9677           gprmask |= relobj->gprmask();
9678           cprmask1 |= relobj->cprmask1();
9679           cprmask2 |= relobj->cprmask2();
9680           cprmask3 |= relobj->cprmask3();
9681           cprmask4 |= relobj->cprmask4();
9682         }
9683
9684       // Merge processor specific flags.
9685       Mips_abiflags<big_endian> in_abiflags;
9686
9687       this->create_abiflags(relobj, &in_abiflags);
9688       this->merge_obj_e_flags(relobj->name(),
9689                               relobj->processor_specific_flags());
9690       this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9691       this->merge_obj_attributes(relobj->name(),
9692                                  relobj->attributes_section_data());
9693     }
9694
9695   // Create a .gnu.attributes section if we have merged any attributes
9696   // from inputs.
9697   if (this->attributes_section_data_ != NULL)
9698     {
9699       Output_attributes_section_data* attributes_section =
9700         new Output_attributes_section_data(*this->attributes_section_data_);
9701       layout->add_output_section_data(".gnu.attributes",
9702                                       elfcpp::SHT_GNU_ATTRIBUTES, 0,
9703                                       attributes_section, ORDER_INVALID, false);
9704     }
9705
9706   // Create .MIPS.abiflags output section if there is an input section.
9707   if (this->has_abiflags_section_)
9708     {
9709       Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9710         new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9711
9712       Output_section* os =
9713         layout->add_output_section_data(".MIPS.abiflags",
9714                                         elfcpp::SHT_MIPS_ABIFLAGS,
9715                                         elfcpp::SHF_ALLOC,
9716                                         abiflags_section, ORDER_INVALID, false);
9717
9718       if (!relocatable && os != NULL)
9719         {
9720           Output_segment* abiflags_segment =
9721             layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9722           abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9723         }
9724     }
9725
9726   if (has_reginfo_section && !parameters->options().gc_sections())
9727     {
9728       // Create .reginfo output section.
9729       Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9730         new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9731                                                           cprmask1, cprmask2,
9732                                                           cprmask3, cprmask4);
9733
9734       Output_section* os =
9735         layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9736                                         elfcpp::SHF_ALLOC, reginfo_section,
9737                                         ORDER_INVALID, false);
9738
9739       if (!relocatable && os != NULL)
9740         {
9741           Output_segment* reginfo_segment =
9742             layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9743                                         elfcpp::PF_R);
9744           reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9745         }
9746     }
9747
9748   if (this->plt_ != NULL)
9749     {
9750       // Set final PLT offsets for symbols.
9751       this->plt_section()->set_plt_offsets();
9752
9753       // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9754       // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9755       // there are no standard PLT entries present.
9756       unsigned char nonvis = 0;
9757       if (this->is_output_micromips()
9758           && !this->plt_section()->has_standard_entries())
9759         nonvis = elfcpp::STO_MICROMIPS >> 2;
9760       symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9761                                     Symbol_table::PREDEFINED,
9762                                     this->plt_,
9763                                     0, 0, elfcpp::STT_FUNC,
9764                                     elfcpp::STB_LOCAL,
9765                                     elfcpp::STV_DEFAULT, nonvis,
9766                                     false, false);
9767     }
9768
9769   if (this->mips_stubs_ != NULL)
9770     {
9771       // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9772       unsigned char nonvis = 0;
9773       if (this->is_output_micromips())
9774         nonvis = elfcpp::STO_MICROMIPS >> 2;
9775       symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9776                                     Symbol_table::PREDEFINED,
9777                                     this->mips_stubs_,
9778                                     0, 0, elfcpp::STT_FUNC,
9779                                     elfcpp::STB_LOCAL,
9780                                     elfcpp::STV_DEFAULT, nonvis,
9781                                     false, false);
9782     }
9783
9784   if (!relocatable && !parameters->doing_static_link())
9785     // In case there is no .got section, create one.
9786     this->got_section(symtab, layout);
9787
9788   // Emit any relocs we saved in an attempt to avoid generating COPY
9789   // relocs.
9790   if (this->copy_relocs_.any_saved_relocs())
9791     this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9792                                  this);
9793
9794   // Set _gp value.
9795   this->set_gp(layout, symtab);
9796
9797   // Emit dynamic relocs.
9798   for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9799        p != this->dyn_relocs_.end();
9800        ++p)
9801     p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9802
9803   if (this->has_got_section())
9804     this->got_section()->lay_out_got(layout, symtab, input_objects);
9805
9806   if (this->mips_stubs_ != NULL)
9807     this->mips_stubs_->set_needs_dynsym_value();
9808
9809   // Check for functions that might need $25 to be valid on entry.
9810   // TODO(sasa): Can we do this without iterating over all symbols?
9811   typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9812   symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9813                                                                symtab));
9814
9815   // Add NULL segment.
9816   if (!relocatable)
9817     layout->make_output_segment(elfcpp::PT_NULL, 0);
9818
9819   // Fill in some more dynamic tags.
9820   // TODO(sasa): Add more dynamic tags.
9821   const Reloc_section* rel_plt = (this->plt_ == NULL
9822                                   ? NULL : this->plt_->rel_plt());
9823   layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9824                                   this->rel_dyn_, true, false);
9825
9826   Output_data_dynamic* const odyn = layout->dynamic_data();
9827   if (odyn != NULL
9828       && !relocatable
9829       && !parameters->doing_static_link())
9830   {
9831     unsigned int d_val;
9832     // This element holds a 32-bit version id for the Runtime
9833     // Linker Interface.  This will start at integer value 1.
9834     d_val = 0x01;
9835     odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9836
9837     // Dynamic flags
9838     d_val = elfcpp::RHF_NOTPOT;
9839     odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9840
9841     // Save layout for using when emitting custom dynamic tags.
9842     this->layout_ = layout;
9843
9844     // This member holds the base address of the segment.
9845     odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9846
9847     // This member holds the number of entries in the .dynsym section.
9848     odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9849
9850     // This member holds the index of the first dynamic symbol
9851     // table entry that corresponds to an entry in the global offset table.
9852     odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9853
9854     // This member holds the number of local GOT entries.
9855     odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9856                        this->got_->get_local_gotno());
9857
9858     if (this->plt_ != NULL)
9859       // DT_MIPS_PLTGOT dynamic tag
9860       odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9861
9862     if (!parameters->options().shared())
9863       {
9864         this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9865
9866         layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9867                                         (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9868                                         this->rld_map_, ORDER_INVALID, false);
9869
9870         // __RLD_MAP will be filled in by the runtime loader to contain
9871         // a pointer to the _r_debug structure.
9872         Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9873                                             Symbol_table::PREDEFINED,
9874                                             this->rld_map_,
9875                                             0, 0, elfcpp::STT_OBJECT,
9876                                             elfcpp::STB_GLOBAL,
9877                                             elfcpp::STV_DEFAULT, 0,
9878                                             false, false);
9879
9880         if (!rld_map->is_forced_local())
9881           rld_map->set_needs_dynsym_entry();
9882
9883         if (!parameters->options().pie())
9884           // This member holds the absolute address of the debug pointer.
9885           odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9886         else
9887           // This member holds the offset to the debug pointer,
9888           // relative to the address of the tag.
9889           odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9890       }
9891   }
9892 }
9893
9894 // Get the custom dynamic tag value.
9895 template<int size, bool big_endian>
9896 unsigned int
9897 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9898 {
9899   switch (tag)
9900     {
9901     case elfcpp::DT_MIPS_BASE_ADDRESS:
9902       {
9903         // The base address of the segment.
9904         // At this point, the segment list has been sorted into final order,
9905         // so just return vaddr of the first readable PT_LOAD segment.
9906         Output_segment* seg =
9907           this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9908         gold_assert(seg != NULL);
9909         return seg->vaddr();
9910       }
9911
9912     case elfcpp::DT_MIPS_SYMTABNO:
9913       // The number of entries in the .dynsym section.
9914       return this->get_dt_mips_symtabno();
9915
9916     case elfcpp::DT_MIPS_GOTSYM:
9917       {
9918         // The index of the first dynamic symbol table entry that corresponds
9919         // to an entry in the GOT.
9920         if (this->got_->first_global_got_dynsym_index() != -1U)
9921           return this->got_->first_global_got_dynsym_index();
9922         else
9923           // In case if we don't have global GOT symbols we default to setting
9924           // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9925           return this->get_dt_mips_symtabno();
9926       }
9927
9928     case elfcpp::DT_MIPS_RLD_MAP_REL:
9929       {
9930         // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9931         // relative to the address of the tag.
9932         Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9933         unsigned int entry_offset =
9934           odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9935         gold_assert(entry_offset != -1U);
9936         return this->rld_map_->address() - (odyn->address() + entry_offset);
9937       }
9938     default:
9939       gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9940     }
9941
9942   return (unsigned int)-1;
9943 }
9944
9945 // Relocate section data.
9946
9947 template<int size, bool big_endian>
9948 void
9949 Target_mips<size, big_endian>::relocate_section(
9950                         const Relocate_info<size, big_endian>* relinfo,
9951                         unsigned int sh_type,
9952                         const unsigned char* prelocs,
9953                         size_t reloc_count,
9954                         Output_section* output_section,
9955                         bool needs_special_offset_handling,
9956                         unsigned char* view,
9957                         Mips_address address,
9958                         section_size_type view_size,
9959                         const Reloc_symbol_changes* reloc_symbol_changes)
9960 {
9961   typedef Target_mips<size, big_endian> Mips;
9962   typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9963
9964   if (sh_type == elfcpp::SHT_REL)
9965     {
9966       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9967           Classify_reloc;
9968
9969       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9970                              gold::Default_comdat_behavior, Classify_reloc>(
9971         relinfo,
9972         this,
9973         prelocs,
9974         reloc_count,
9975         output_section,
9976         needs_special_offset_handling,
9977         view,
9978         address,
9979         view_size,
9980         reloc_symbol_changes);
9981     }
9982   else if (sh_type == elfcpp::SHT_RELA)
9983     {
9984       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9985           Classify_reloc;
9986
9987       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9988                              gold::Default_comdat_behavior, Classify_reloc>(
9989         relinfo,
9990         this,
9991         prelocs,
9992         reloc_count,
9993         output_section,
9994         needs_special_offset_handling,
9995         view,
9996         address,
9997         view_size,
9998         reloc_symbol_changes);
9999     }
10000 }
10001
10002 // Return the size of a relocation while scanning during a relocatable
10003 // link.
10004
10005 unsigned int
10006 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
10007 {
10008   switch (r_type)
10009     {
10010     case elfcpp::R_MIPS_NONE:
10011     case elfcpp::R_MIPS_TLS_DTPMOD64:
10012     case elfcpp::R_MIPS_TLS_DTPREL64:
10013     case elfcpp::R_MIPS_TLS_TPREL64:
10014       return 0;
10015
10016     case elfcpp::R_MIPS_32:
10017     case elfcpp::R_MIPS_TLS_DTPMOD32:
10018     case elfcpp::R_MIPS_TLS_DTPREL32:
10019     case elfcpp::R_MIPS_TLS_TPREL32:
10020     case elfcpp::R_MIPS_REL32:
10021     case elfcpp::R_MIPS_PC32:
10022     case elfcpp::R_MIPS_GPREL32:
10023     case elfcpp::R_MIPS_JALR:
10024     case elfcpp::R_MIPS_EH:
10025       return 4;
10026
10027     case elfcpp::R_MIPS_16:
10028     case elfcpp::R_MIPS_HI16:
10029     case elfcpp::R_MIPS_LO16:
10030     case elfcpp::R_MIPS_HIGHER:
10031     case elfcpp::R_MIPS_HIGHEST:
10032     case elfcpp::R_MIPS_GPREL16:
10033     case elfcpp::R_MIPS16_HI16:
10034     case elfcpp::R_MIPS16_LO16:
10035     case elfcpp::R_MIPS_PC16:
10036     case elfcpp::R_MIPS_PCHI16:
10037     case elfcpp::R_MIPS_PCLO16:
10038     case elfcpp::R_MIPS_GOT16:
10039     case elfcpp::R_MIPS16_GOT16:
10040     case elfcpp::R_MIPS_CALL16:
10041     case elfcpp::R_MIPS16_CALL16:
10042     case elfcpp::R_MIPS_GOT_HI16:
10043     case elfcpp::R_MIPS_CALL_HI16:
10044     case elfcpp::R_MIPS_GOT_LO16:
10045     case elfcpp::R_MIPS_CALL_LO16:
10046     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10047     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10048     case elfcpp::R_MIPS_TLS_TPREL_HI16:
10049     case elfcpp::R_MIPS_TLS_TPREL_LO16:
10050     case elfcpp::R_MIPS16_GPREL:
10051     case elfcpp::R_MIPS_GOT_DISP:
10052     case elfcpp::R_MIPS_LITERAL:
10053     case elfcpp::R_MIPS_GOT_PAGE:
10054     case elfcpp::R_MIPS_GOT_OFST:
10055     case elfcpp::R_MIPS_TLS_GD:
10056     case elfcpp::R_MIPS_TLS_LDM:
10057     case elfcpp::R_MIPS_TLS_GOTTPREL:
10058       return 2;
10059
10060     // These relocations are not byte sized
10061     case elfcpp::R_MIPS_26:
10062     case elfcpp::R_MIPS16_26:
10063     case elfcpp::R_MIPS_PC21_S2:
10064     case elfcpp::R_MIPS_PC26_S2:
10065     case elfcpp::R_MIPS_PC18_S3:
10066     case elfcpp::R_MIPS_PC19_S2:
10067       return 4;
10068
10069     case elfcpp::R_MIPS_COPY:
10070     case elfcpp::R_MIPS_JUMP_SLOT:
10071       object->error(_("unexpected reloc %u in object file"), r_type);
10072       return 0;
10073
10074     default:
10075       object->error(_("unsupported reloc %u in object file"), r_type);
10076       return 0;
10077   }
10078 }
10079
10080 // Scan the relocs during a relocatable link.
10081
10082 template<int size, bool big_endian>
10083 void
10084 Target_mips<size, big_endian>::scan_relocatable_relocs(
10085                         Symbol_table* symtab,
10086                         Layout* layout,
10087                         Sized_relobj_file<size, big_endian>* object,
10088                         unsigned int data_shndx,
10089                         unsigned int sh_type,
10090                         const unsigned char* prelocs,
10091                         size_t reloc_count,
10092                         Output_section* output_section,
10093                         bool needs_special_offset_handling,
10094                         size_t local_symbol_count,
10095                         const unsigned char* plocal_symbols,
10096                         Relocatable_relocs* rr)
10097 {
10098   if (sh_type == elfcpp::SHT_REL)
10099     {
10100       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10101           Classify_reloc;
10102       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10103           Scan_relocatable_relocs;
10104
10105       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10106         symtab,
10107         layout,
10108         object,
10109         data_shndx,
10110         prelocs,
10111         reloc_count,
10112         output_section,
10113         needs_special_offset_handling,
10114         local_symbol_count,
10115         plocal_symbols,
10116         rr);
10117     }
10118   else if (sh_type == elfcpp::SHT_RELA)
10119     {
10120       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10121           Classify_reloc;
10122       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10123           Scan_relocatable_relocs;
10124
10125       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10126         symtab,
10127         layout,
10128         object,
10129         data_shndx,
10130         prelocs,
10131         reloc_count,
10132         output_section,
10133         needs_special_offset_handling,
10134         local_symbol_count,
10135         plocal_symbols,
10136         rr);
10137     }
10138   else
10139     gold_unreachable();
10140 }
10141
10142 // Scan the relocs for --emit-relocs.
10143
10144 template<int size, bool big_endian>
10145 void
10146 Target_mips<size, big_endian>::emit_relocs_scan(
10147     Symbol_table* symtab,
10148     Layout* layout,
10149     Sized_relobj_file<size, big_endian>* object,
10150     unsigned int data_shndx,
10151     unsigned int sh_type,
10152     const unsigned char* prelocs,
10153     size_t reloc_count,
10154     Output_section* output_section,
10155     bool needs_special_offset_handling,
10156     size_t local_symbol_count,
10157     const unsigned char* plocal_syms,
10158     Relocatable_relocs* rr)
10159 {
10160   if (sh_type == elfcpp::SHT_REL)
10161     {
10162       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10163           Classify_reloc;
10164       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10165           Emit_relocs_strategy;
10166
10167       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10168         symtab,
10169         layout,
10170         object,
10171         data_shndx,
10172         prelocs,
10173         reloc_count,
10174         output_section,
10175         needs_special_offset_handling,
10176         local_symbol_count,
10177         plocal_syms,
10178         rr);
10179     }
10180   else if (sh_type == elfcpp::SHT_RELA)
10181     {
10182       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10183           Classify_reloc;
10184       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10185           Emit_relocs_strategy;
10186
10187       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10188         symtab,
10189         layout,
10190         object,
10191         data_shndx,
10192         prelocs,
10193         reloc_count,
10194         output_section,
10195         needs_special_offset_handling,
10196         local_symbol_count,
10197         plocal_syms,
10198         rr);
10199     }
10200   else
10201     gold_unreachable();
10202 }
10203
10204 // Emit relocations for a section.
10205
10206 template<int size, bool big_endian>
10207 void
10208 Target_mips<size, big_endian>::relocate_relocs(
10209                         const Relocate_info<size, big_endian>* relinfo,
10210                         unsigned int sh_type,
10211                         const unsigned char* prelocs,
10212                         size_t reloc_count,
10213                         Output_section* output_section,
10214                         typename elfcpp::Elf_types<size>::Elf_Off
10215                           offset_in_output_section,
10216                         unsigned char* view,
10217                         Mips_address view_address,
10218                         section_size_type view_size,
10219                         unsigned char* reloc_view,
10220                         section_size_type reloc_view_size)
10221 {
10222   if (sh_type == elfcpp::SHT_REL)
10223     {
10224       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10225           Classify_reloc;
10226
10227       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10228         relinfo,
10229         prelocs,
10230         reloc_count,
10231         output_section,
10232         offset_in_output_section,
10233         view,
10234         view_address,
10235         view_size,
10236         reloc_view,
10237         reloc_view_size);
10238     }
10239   else if (sh_type == elfcpp::SHT_RELA)
10240     {
10241       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10242           Classify_reloc;
10243
10244       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10245         relinfo,
10246         prelocs,
10247         reloc_count,
10248         output_section,
10249         offset_in_output_section,
10250         view,
10251         view_address,
10252         view_size,
10253         reloc_view,
10254         reloc_view_size);
10255     }
10256   else
10257     gold_unreachable();
10258 }
10259
10260 // Perform target-specific processing in a relocatable link.  This is
10261 // only used if we use the relocation strategy RELOC_SPECIAL.
10262
10263 template<int size, bool big_endian>
10264 void
10265 Target_mips<size, big_endian>::relocate_special_relocatable(
10266     const Relocate_info<size, big_endian>* relinfo,
10267     unsigned int sh_type,
10268     const unsigned char* preloc_in,
10269     size_t relnum,
10270     Output_section* output_section,
10271     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10272     unsigned char* view,
10273     Mips_address view_address,
10274     section_size_type,
10275     unsigned char* preloc_out)
10276 {
10277   // We can only handle REL type relocation sections.
10278   gold_assert(sh_type == elfcpp::SHT_REL);
10279
10280   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10281     Reltype;
10282   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10283     Reltype_write;
10284
10285   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10286
10287   const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10288
10289   Mips_relobj<size, big_endian>* object =
10290     Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10291   const unsigned int local_count = object->local_symbol_count();
10292
10293   Reltype reloc(preloc_in);
10294   Reltype_write reloc_write(preloc_out);
10295
10296   elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10297   const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10298   const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10299
10300   // Get the new symbol index.
10301   // We only use RELOC_SPECIAL strategy in local relocations.
10302   gold_assert(r_sym < local_count);
10303
10304   // We are adjusting a section symbol.  We need to find
10305   // the symbol table index of the section symbol for
10306   // the output section corresponding to input section
10307   // in which this symbol is defined.
10308   bool is_ordinary;
10309   unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10310   gold_assert(is_ordinary);
10311   Output_section* os = object->output_section(shndx);
10312   gold_assert(os != NULL);
10313   gold_assert(os->needs_symtab_index());
10314   unsigned int new_symndx = os->symtab_index();
10315
10316   // Get the new offset--the location in the output section where
10317   // this relocation should be applied.
10318
10319   Mips_address offset = reloc.get_r_offset();
10320   Mips_address new_offset;
10321   if (offset_in_output_section != invalid_address)
10322     new_offset = offset + offset_in_output_section;
10323   else
10324     {
10325       section_offset_type sot_offset =
10326         convert_types<section_offset_type, Mips_address>(offset);
10327       section_offset_type new_sot_offset =
10328         output_section->output_offset(object, relinfo->data_shndx,
10329                                       sot_offset);
10330       gold_assert(new_sot_offset != -1);
10331       new_offset = new_sot_offset;
10332     }
10333
10334   // In an object file, r_offset is an offset within the section.
10335   // In an executable or dynamic object, generated by
10336   // --emit-relocs, r_offset is an absolute address.
10337   if (!parameters->options().relocatable())
10338     {
10339       new_offset += view_address;
10340       if (offset_in_output_section != invalid_address)
10341         new_offset -= offset_in_output_section;
10342     }
10343
10344   reloc_write.put_r_offset(new_offset);
10345   reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10346
10347   // Handle the reloc addend.
10348   // The relocation uses a section symbol in the input file.
10349   // We are adjusting it to use a section symbol in the output
10350   // file.  The input section symbol refers to some address in
10351   // the input section.  We need the relocation in the output
10352   // file to refer to that same address.  This adjustment to
10353   // the addend is the same calculation we use for a simple
10354   // absolute relocation for the input section symbol.
10355   Valtype calculated_value = 0;
10356   const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10357
10358   unsigned char* paddend = view + offset;
10359   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10360   switch (r_type)
10361     {
10362     case elfcpp::R_MIPS_26:
10363       reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10364           offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10365           false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10366           false, &calculated_value);
10367       break;
10368
10369     default:
10370       gold_unreachable();
10371     }
10372
10373   // Report any errors.
10374   switch (reloc_status)
10375     {
10376     case Reloc_funcs::STATUS_OKAY:
10377       break;
10378     case Reloc_funcs::STATUS_OVERFLOW:
10379       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10380                              _("relocation overflow: "
10381                                "%u against local symbol %u in %s"),
10382                              r_type, r_sym, object->name().c_str());
10383       break;
10384     case Reloc_funcs::STATUS_BAD_RELOC:
10385       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10386         _("unexpected opcode while processing relocation"));
10387       break;
10388     default:
10389       gold_unreachable();
10390     }
10391 }
10392
10393 // Optimize the TLS relocation type based on what we know about the
10394 // symbol.  IS_FINAL is true if the final address of this symbol is
10395 // known at link time.
10396
10397 template<int size, bool big_endian>
10398 tls::Tls_optimization
10399 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10400 {
10401   // FIXME: Currently we do not do any TLS optimization.
10402   return tls::TLSOPT_NONE;
10403 }
10404
10405 // Scan a relocation for a local symbol.
10406
10407 template<int size, bool big_endian>
10408 inline void
10409 Target_mips<size, big_endian>::Scan::local(
10410                         Symbol_table* symtab,
10411                         Layout* layout,
10412                         Target_mips<size, big_endian>* target,
10413                         Sized_relobj_file<size, big_endian>* object,
10414                         unsigned int data_shndx,
10415                         Output_section* output_section,
10416                         const Relatype* rela,
10417                         const Reltype* rel,
10418                         unsigned int rel_type,
10419                         unsigned int r_type,
10420                         const elfcpp::Sym<size, big_endian>& lsym,
10421                         bool is_discarded)
10422 {
10423   if (is_discarded)
10424     return;
10425
10426   Mips_address r_offset;
10427   unsigned int r_sym;
10428   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10429
10430   if (rel_type == elfcpp::SHT_RELA)
10431     {
10432       r_offset = rela->get_r_offset();
10433       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10434           get_r_sym(rela);
10435       r_addend = rela->get_r_addend();
10436     }
10437   else
10438     {
10439       r_offset = rel->get_r_offset();
10440       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10441           get_r_sym(rel);
10442       r_addend = 0;
10443     }
10444
10445   Mips_relobj<size, big_endian>* mips_obj =
10446     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10447
10448   if (mips_obj->is_mips16_stub_section(data_shndx))
10449     {
10450       mips_obj->get_mips16_stub_section(data_shndx)
10451               ->new_local_reloc_found(r_type, r_sym);
10452     }
10453
10454   if (r_type == elfcpp::R_MIPS_NONE)
10455     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10456     // mips16 stub.
10457     return;
10458
10459   if (!mips16_call_reloc(r_type)
10460       && !mips_obj->section_allows_mips16_refs(data_shndx))
10461     // This reloc would need to refer to a MIPS16 hard-float stub, if
10462     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10463     // looking for relocs that would need to refer to MIPS16 stubs.
10464     mips_obj->add_local_non_16bit_call(r_sym);
10465
10466   if (r_type == elfcpp::R_MIPS16_26
10467       && !mips_obj->section_allows_mips16_refs(data_shndx))
10468     mips_obj->add_local_16bit_call(r_sym);
10469
10470   switch (r_type)
10471     {
10472     case elfcpp::R_MIPS_GOT16:
10473     case elfcpp::R_MIPS_CALL16:
10474     case elfcpp::R_MIPS_CALL_HI16:
10475     case elfcpp::R_MIPS_CALL_LO16:
10476     case elfcpp::R_MIPS_GOT_HI16:
10477     case elfcpp::R_MIPS_GOT_LO16:
10478     case elfcpp::R_MIPS_GOT_PAGE:
10479     case elfcpp::R_MIPS_GOT_OFST:
10480     case elfcpp::R_MIPS_GOT_DISP:
10481     case elfcpp::R_MIPS_TLS_GOTTPREL:
10482     case elfcpp::R_MIPS_TLS_GD:
10483     case elfcpp::R_MIPS_TLS_LDM:
10484     case elfcpp::R_MIPS16_GOT16:
10485     case elfcpp::R_MIPS16_CALL16:
10486     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10487     case elfcpp::R_MIPS16_TLS_GD:
10488     case elfcpp::R_MIPS16_TLS_LDM:
10489     case elfcpp::R_MICROMIPS_GOT16:
10490     case elfcpp::R_MICROMIPS_CALL16:
10491     case elfcpp::R_MICROMIPS_CALL_HI16:
10492     case elfcpp::R_MICROMIPS_CALL_LO16:
10493     case elfcpp::R_MICROMIPS_GOT_HI16:
10494     case elfcpp::R_MICROMIPS_GOT_LO16:
10495     case elfcpp::R_MICROMIPS_GOT_PAGE:
10496     case elfcpp::R_MICROMIPS_GOT_OFST:
10497     case elfcpp::R_MICROMIPS_GOT_DISP:
10498     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10499     case elfcpp::R_MICROMIPS_TLS_GD:
10500     case elfcpp::R_MICROMIPS_TLS_LDM:
10501     case elfcpp::R_MIPS_EH:
10502       // We need a GOT section.
10503       target->got_section(symtab, layout);
10504       break;
10505
10506     default:
10507       break;
10508     }
10509
10510   if (call_lo16_reloc(r_type)
10511       || got_lo16_reloc(r_type)
10512       || got_disp_reloc(r_type)
10513       || eh_reloc(r_type))
10514     {
10515       // We may need a local GOT entry for this relocation.  We
10516       // don't count R_MIPS_GOT_PAGE because we can estimate the
10517       // maximum number of pages needed by looking at the size of
10518       // the segment.  Similar comments apply to R_MIPS*_GOT16 and
10519       // R_MIPS*_CALL16.  We don't count R_MIPS_GOT_HI16, or
10520       // R_MIPS_CALL_HI16 because these are always followed by an
10521       // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10522       Mips_output_data_got<size, big_endian>* got =
10523         target->got_section(symtab, layout);
10524       bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10525       got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10526                                    is_section_symbol);
10527     }
10528
10529   switch (r_type)
10530     {
10531     case elfcpp::R_MIPS_CALL16:
10532     case elfcpp::R_MIPS16_CALL16:
10533     case elfcpp::R_MICROMIPS_CALL16:
10534       gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10535                  (unsigned long)r_offset);
10536       return;
10537
10538     case elfcpp::R_MIPS_GOT_PAGE:
10539     case elfcpp::R_MICROMIPS_GOT_PAGE:
10540     case elfcpp::R_MIPS16_GOT16:
10541     case elfcpp::R_MIPS_GOT16:
10542     case elfcpp::R_MIPS_GOT_HI16:
10543     case elfcpp::R_MIPS_GOT_LO16:
10544     case elfcpp::R_MICROMIPS_GOT16:
10545     case elfcpp::R_MICROMIPS_GOT_HI16:
10546     case elfcpp::R_MICROMIPS_GOT_LO16:
10547       {
10548         // This relocation needs a page entry in the GOT.
10549         // Get the section contents.
10550         section_size_type view_size = 0;
10551         const unsigned char* view = object->section_contents(data_shndx,
10552                                                              &view_size, false);
10553         view += r_offset;
10554
10555         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10556         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10557                                                         : r_addend);
10558
10559         if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10560           target->got16_addends_.push_back(got16_addend<size, big_endian>(
10561               object, data_shndx, r_type, r_sym, addend));
10562         else
10563           target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10564         break;
10565       }
10566
10567     case elfcpp::R_MIPS_HI16:
10568     case elfcpp::R_MIPS_PCHI16:
10569     case elfcpp::R_MIPS16_HI16:
10570     case elfcpp::R_MICROMIPS_HI16:
10571       // Record the reloc so that we can check whether the corresponding LO16
10572       // part exists.
10573       if (rel_type == elfcpp::SHT_REL)
10574         target->got16_addends_.push_back(got16_addend<size, big_endian>(
10575             object, data_shndx, r_type, r_sym, 0));
10576       break;
10577
10578     case elfcpp::R_MIPS_LO16:
10579     case elfcpp::R_MIPS_PCLO16:
10580     case elfcpp::R_MIPS16_LO16:
10581     case elfcpp::R_MICROMIPS_LO16:
10582       {
10583         if (rel_type != elfcpp::SHT_REL)
10584           break;
10585
10586         // Find corresponding GOT16/HI16 relocation.
10587
10588         // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10589         // be immediately following.  However, for the IRIX6 ABI, the next
10590         // relocation may be a composed relocation consisting of several
10591         // relocations for the same address.  In that case, the R_MIPS_LO16
10592         // relocation may occur as one of these.  We permit a similar
10593         // extension in general, as that is useful for GCC.
10594
10595         // In some cases GCC dead code elimination removes the LO16 but
10596         // keeps the corresponding HI16.  This is strictly speaking a
10597         // violation of the ABI but not immediately harmful.
10598
10599         typename std::list<got16_addend<size, big_endian> >::iterator it =
10600           target->got16_addends_.begin();
10601         while (it != target->got16_addends_.end())
10602           {
10603             got16_addend<size, big_endian> _got16_addend = *it;
10604
10605             // TODO(sasa): Split got16_addends_ list into two lists - one for
10606             // GOT16 relocs and the other for HI16 relocs.
10607
10608             // Report an error if we find HI16 or GOT16 reloc from the
10609             // previous section without the matching LO16 part.
10610             if (_got16_addend.object != object
10611                 || _got16_addend.shndx != data_shndx)
10612               {
10613                 gold_error("Can't find matching LO16 reloc");
10614                 break;
10615               }
10616
10617             if (_got16_addend.r_sym != r_sym
10618                 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10619               {
10620                 ++it;
10621                 continue;
10622               }
10623
10624             // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10625             // For GOT16, we need to calculate combined addend and record GOT page
10626             // entry.
10627             if (got16_reloc(_got16_addend.r_type))
10628               {
10629
10630                 section_size_type view_size = 0;
10631                 const unsigned char* view = object->section_contents(data_shndx,
10632                                                                      &view_size,
10633                                                                      false);
10634                 view += r_offset;
10635
10636                 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10637                 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10638
10639                 addend = (_got16_addend.addend << 16) + addend;
10640                 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10641                                                              addend);
10642               }
10643
10644             it = target->got16_addends_.erase(it);
10645           }
10646         break;
10647       }
10648     }
10649
10650   switch (r_type)
10651     {
10652     case elfcpp::R_MIPS_32:
10653     case elfcpp::R_MIPS_REL32:
10654     case elfcpp::R_MIPS_64:
10655       {
10656         if (parameters->options().output_is_position_independent())
10657           {
10658             // If building a shared library (or a position-independent
10659             // executable), we need to create a dynamic relocation for
10660             // this location.
10661             if (is_readonly_section(output_section))
10662               break;
10663             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10664             rel_dyn->add_symbolless_local_addend(object, r_sym,
10665                                                  elfcpp::R_MIPS_REL32,
10666                                                  output_section, data_shndx,
10667                                                  r_offset);
10668           }
10669         break;
10670       }
10671
10672     case elfcpp::R_MIPS_TLS_GOTTPREL:
10673     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10674     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10675     case elfcpp::R_MIPS_TLS_LDM:
10676     case elfcpp::R_MIPS16_TLS_LDM:
10677     case elfcpp::R_MICROMIPS_TLS_LDM:
10678     case elfcpp::R_MIPS_TLS_GD:
10679     case elfcpp::R_MIPS16_TLS_GD:
10680     case elfcpp::R_MICROMIPS_TLS_GD:
10681       {
10682         bool output_is_shared = parameters->options().shared();
10683         const tls::Tls_optimization optimized_type
10684             = Target_mips<size, big_endian>::optimize_tls_reloc(
10685                                              !output_is_shared, r_type);
10686         switch (r_type)
10687           {
10688           case elfcpp::R_MIPS_TLS_GD:
10689           case elfcpp::R_MIPS16_TLS_GD:
10690           case elfcpp::R_MICROMIPS_TLS_GD:
10691             if (optimized_type == tls::TLSOPT_NONE)
10692               {
10693                 // Create a pair of GOT entries for the module index and
10694                 // dtv-relative offset.
10695                 Mips_output_data_got<size, big_endian>* got =
10696                   target->got_section(symtab, layout);
10697                 unsigned int shndx = lsym.get_st_shndx();
10698                 bool is_ordinary;
10699                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10700                 if (!is_ordinary)
10701                   {
10702                     object->error(_("local symbol %u has bad shndx %u"),
10703                                   r_sym, shndx);
10704                     break;
10705                   }
10706                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10707                                              shndx, false);
10708               }
10709             else
10710               {
10711                 // FIXME: TLS optimization not supported yet.
10712                 gold_unreachable();
10713               }
10714             break;
10715
10716           case elfcpp::R_MIPS_TLS_LDM:
10717           case elfcpp::R_MIPS16_TLS_LDM:
10718           case elfcpp::R_MICROMIPS_TLS_LDM:
10719             if (optimized_type == tls::TLSOPT_NONE)
10720               {
10721                 // We always record LDM symbols as local with index 0.
10722                 target->got_section()->record_local_got_symbol(mips_obj, 0,
10723                                                                r_addend, r_type,
10724                                                                -1U, false);
10725               }
10726             else
10727               {
10728                 // FIXME: TLS optimization not supported yet.
10729                 gold_unreachable();
10730               }
10731             break;
10732           case elfcpp::R_MIPS_TLS_GOTTPREL:
10733           case elfcpp::R_MIPS16_TLS_GOTTPREL:
10734           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10735             layout->set_has_static_tls();
10736             if (optimized_type == tls::TLSOPT_NONE)
10737               {
10738                 // Create a GOT entry for the tp-relative offset.
10739                 Mips_output_data_got<size, big_endian>* got =
10740                   target->got_section(symtab, layout);
10741                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10742                                              -1U, false);
10743               }
10744             else
10745               {
10746                 // FIXME: TLS optimization not supported yet.
10747                 gold_unreachable();
10748               }
10749             break;
10750
10751           default:
10752             gold_unreachable();
10753         }
10754       }
10755       break;
10756
10757     default:
10758       break;
10759     }
10760
10761   // Refuse some position-dependent relocations when creating a
10762   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
10763   // not PIC, but we can create dynamic relocations and the result
10764   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
10765   // combined with R_MIPS_GOT16.
10766   if (parameters->options().shared())
10767     {
10768       switch (r_type)
10769         {
10770         case elfcpp::R_MIPS16_HI16:
10771         case elfcpp::R_MIPS_HI16:
10772         case elfcpp::R_MIPS_HIGHER:
10773         case elfcpp::R_MIPS_HIGHEST:
10774         case elfcpp::R_MICROMIPS_HI16:
10775         case elfcpp::R_MICROMIPS_HIGHER:
10776         case elfcpp::R_MICROMIPS_HIGHEST:
10777           // Don't refuse a high part relocation if it's against
10778           // no symbol (e.g. part of a compound relocation).
10779           if (r_sym == 0)
10780             break;
10781           // Fall through.
10782
10783         case elfcpp::R_MIPS16_26:
10784         case elfcpp::R_MIPS_26:
10785         case elfcpp::R_MICROMIPS_26_S1:
10786           gold_error(_("%s: relocation %u against `%s' can not be used when "
10787                        "making a shared object; recompile with -fPIC"),
10788                      object->name().c_str(), r_type, "a local symbol");
10789         default:
10790           break;
10791         }
10792     }
10793 }
10794
10795 template<int size, bool big_endian>
10796 inline void
10797 Target_mips<size, big_endian>::Scan::local(
10798                         Symbol_table* symtab,
10799                         Layout* layout,
10800                         Target_mips<size, big_endian>* target,
10801                         Sized_relobj_file<size, big_endian>* object,
10802                         unsigned int data_shndx,
10803                         Output_section* output_section,
10804                         const Reltype& reloc,
10805                         unsigned int r_type,
10806                         const elfcpp::Sym<size, big_endian>& lsym,
10807                         bool is_discarded)
10808 {
10809   if (is_discarded)
10810     return;
10811
10812   local(
10813     symtab,
10814     layout,
10815     target,
10816     object,
10817     data_shndx,
10818     output_section,
10819     (const Relatype*) NULL,
10820     &reloc,
10821     elfcpp::SHT_REL,
10822     r_type,
10823     lsym, is_discarded);
10824 }
10825
10826
10827 template<int size, bool big_endian>
10828 inline void
10829 Target_mips<size, big_endian>::Scan::local(
10830                         Symbol_table* symtab,
10831                         Layout* layout,
10832                         Target_mips<size, big_endian>* target,
10833                         Sized_relobj_file<size, big_endian>* object,
10834                         unsigned int data_shndx,
10835                         Output_section* output_section,
10836                         const Relatype& reloc,
10837                         unsigned int r_type,
10838                         const elfcpp::Sym<size, big_endian>& lsym,
10839                         bool is_discarded)
10840 {
10841   if (is_discarded)
10842     return;
10843
10844   local(
10845     symtab,
10846     layout,
10847     target,
10848     object,
10849     data_shndx,
10850     output_section,
10851     &reloc,
10852     (const Reltype*) NULL,
10853     elfcpp::SHT_RELA,
10854     r_type,
10855     lsym, is_discarded);
10856 }
10857
10858 // Scan a relocation for a global symbol.
10859
10860 template<int size, bool big_endian>
10861 inline void
10862 Target_mips<size, big_endian>::Scan::global(
10863                                 Symbol_table* symtab,
10864                                 Layout* layout,
10865                                 Target_mips<size, big_endian>* target,
10866                                 Sized_relobj_file<size, big_endian>* object,
10867                                 unsigned int data_shndx,
10868                                 Output_section* output_section,
10869                                 const Relatype* rela,
10870                                 const Reltype* rel,
10871                                 unsigned int rel_type,
10872                                 unsigned int r_type,
10873                                 Symbol* gsym)
10874 {
10875   Mips_address r_offset;
10876   unsigned int r_sym;
10877   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10878
10879   if (rel_type == elfcpp::SHT_RELA)
10880     {
10881       r_offset = rela->get_r_offset();
10882       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10883           get_r_sym(rela);
10884       r_addend = rela->get_r_addend();
10885     }
10886   else
10887     {
10888       r_offset = rel->get_r_offset();
10889       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10890           get_r_sym(rel);
10891       r_addend = 0;
10892     }
10893
10894   Mips_relobj<size, big_endian>* mips_obj =
10895     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10896   Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10897
10898   if (mips_obj->is_mips16_stub_section(data_shndx))
10899     {
10900       mips_obj->get_mips16_stub_section(data_shndx)
10901               ->new_global_reloc_found(r_type, mips_sym);
10902     }
10903
10904   if (r_type == elfcpp::R_MIPS_NONE)
10905     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10906     // mips16 stub.
10907     return;
10908
10909   if (!mips16_call_reloc(r_type)
10910       && !mips_obj->section_allows_mips16_refs(data_shndx))
10911     // This reloc would need to refer to a MIPS16 hard-float stub, if
10912     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10913     // looking for relocs that would need to refer to MIPS16 stubs.
10914     mips_sym->set_need_fn_stub();
10915
10916   // We need PLT entries if there are static-only relocations against
10917   // an externally-defined function.  This can technically occur for
10918   // shared libraries if there are branches to the symbol, although it
10919   // is unlikely that this will be used in practice due to the short
10920   // ranges involved.  It can occur for any relative or absolute relocation
10921   // in executables; in that case, the PLT entry becomes the function's
10922   // canonical address.
10923   bool static_reloc = false;
10924
10925   // Set CAN_MAKE_DYNAMIC to true if we can convert this
10926   // relocation into a dynamic one.
10927   bool can_make_dynamic = false;
10928   switch (r_type)
10929     {
10930     case elfcpp::R_MIPS_GOT16:
10931     case elfcpp::R_MIPS_CALL16:
10932     case elfcpp::R_MIPS_CALL_HI16:
10933     case elfcpp::R_MIPS_CALL_LO16:
10934     case elfcpp::R_MIPS_GOT_HI16:
10935     case elfcpp::R_MIPS_GOT_LO16:
10936     case elfcpp::R_MIPS_GOT_PAGE:
10937     case elfcpp::R_MIPS_GOT_OFST:
10938     case elfcpp::R_MIPS_GOT_DISP:
10939     case elfcpp::R_MIPS_TLS_GOTTPREL:
10940     case elfcpp::R_MIPS_TLS_GD:
10941     case elfcpp::R_MIPS_TLS_LDM:
10942     case elfcpp::R_MIPS16_GOT16:
10943     case elfcpp::R_MIPS16_CALL16:
10944     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10945     case elfcpp::R_MIPS16_TLS_GD:
10946     case elfcpp::R_MIPS16_TLS_LDM:
10947     case elfcpp::R_MICROMIPS_GOT16:
10948     case elfcpp::R_MICROMIPS_CALL16:
10949     case elfcpp::R_MICROMIPS_CALL_HI16:
10950     case elfcpp::R_MICROMIPS_CALL_LO16:
10951     case elfcpp::R_MICROMIPS_GOT_HI16:
10952     case elfcpp::R_MICROMIPS_GOT_LO16:
10953     case elfcpp::R_MICROMIPS_GOT_PAGE:
10954     case elfcpp::R_MICROMIPS_GOT_OFST:
10955     case elfcpp::R_MICROMIPS_GOT_DISP:
10956     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10957     case elfcpp::R_MICROMIPS_TLS_GD:
10958     case elfcpp::R_MICROMIPS_TLS_LDM:
10959     case elfcpp::R_MIPS_EH:
10960       // We need a GOT section.
10961       target->got_section(symtab, layout);
10962       break;
10963
10964     // This is just a hint; it can safely be ignored.  Don't set
10965     // has_static_relocs for the corresponding symbol.
10966     case elfcpp::R_MIPS_JALR:
10967     case elfcpp::R_MICROMIPS_JALR:
10968       break;
10969
10970     case elfcpp::R_MIPS_GPREL16:
10971     case elfcpp::R_MIPS_GPREL32:
10972     case elfcpp::R_MIPS16_GPREL:
10973     case elfcpp::R_MICROMIPS_GPREL16:
10974       // TODO(sasa)
10975       // GP-relative relocations always resolve to a definition in a
10976       // regular input file, ignoring the one-definition rule.  This is
10977       // important for the GP setup sequence in NewABI code, which
10978       // always resolves to a local function even if other relocations
10979       // against the symbol wouldn't.
10980       //constrain_symbol_p = FALSE;
10981       break;
10982
10983     case elfcpp::R_MIPS_32:
10984     case elfcpp::R_MIPS_REL32:
10985     case elfcpp::R_MIPS_64:
10986       if ((parameters->options().shared()
10987           || (strcmp(gsym->name(), "__gnu_local_gp") != 0
10988           && (!is_readonly_section(output_section)
10989           || mips_obj->is_pic())))
10990           && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
10991         {
10992           if (r_type != elfcpp::R_MIPS_REL32)
10993             mips_sym->set_pointer_equality_needed();
10994           can_make_dynamic = true;
10995           break;
10996         }
10997       // Fall through.
10998
10999     default:
11000       // Most static relocations require pointer equality, except
11001       // for branches.
11002       mips_sym->set_pointer_equality_needed();
11003       // Fall through.
11004
11005     case elfcpp::R_MIPS_26:
11006     case elfcpp::R_MIPS_PC16:
11007     case elfcpp::R_MIPS_PC21_S2:
11008     case elfcpp::R_MIPS_PC26_S2:
11009     case elfcpp::R_MIPS16_26:
11010     case elfcpp::R_MICROMIPS_26_S1:
11011     case elfcpp::R_MICROMIPS_PC7_S1:
11012     case elfcpp::R_MICROMIPS_PC10_S1:
11013     case elfcpp::R_MICROMIPS_PC16_S1:
11014     case elfcpp::R_MICROMIPS_PC23_S2:
11015       static_reloc = true;
11016       mips_sym->set_has_static_relocs();
11017       break;
11018     }
11019
11020   // If there are call relocations against an externally-defined symbol,
11021   // see whether we can create a MIPS lazy-binding stub for it.  We can
11022   // only do this if all references to the function are through call
11023   // relocations, and in that case, the traditional lazy-binding stubs
11024   // are much more efficient than PLT entries.
11025   switch (r_type)
11026     {
11027     case elfcpp::R_MIPS16_CALL16:
11028     case elfcpp::R_MIPS_CALL16:
11029     case elfcpp::R_MIPS_CALL_HI16:
11030     case elfcpp::R_MIPS_CALL_LO16:
11031     case elfcpp::R_MIPS_JALR:
11032     case elfcpp::R_MICROMIPS_CALL16:
11033     case elfcpp::R_MICROMIPS_CALL_HI16:
11034     case elfcpp::R_MICROMIPS_CALL_LO16:
11035     case elfcpp::R_MICROMIPS_JALR:
11036       if (!mips_sym->no_lazy_stub())
11037         {
11038           if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11039               // Calls from shared objects to undefined symbols of type
11040               // STT_NOTYPE need lazy-binding stub.
11041               || (mips_sym->is_undefined() && parameters->options().shared()))
11042             target->mips_stubs_section(layout)->make_entry(mips_sym);
11043         }
11044       break;
11045     default:
11046       {
11047         // We must not create a stub for a symbol that has relocations
11048         // related to taking the function's address.
11049         mips_sym->set_no_lazy_stub();
11050         target->remove_lazy_stub_entry(mips_sym);
11051         break;
11052       }
11053   }
11054
11055   if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11056                                                    mips_sym->is_mips16()))
11057     mips_sym->set_has_nonpic_branches();
11058
11059   // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11060   // and has a special meaning.
11061   bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11062                                && strcmp(gsym->name(), "_gp_disp") == 0
11063                                && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11064   if (static_reloc && gsym->needs_plt_entry())
11065     {
11066       target->make_plt_entry(symtab, layout, mips_sym, r_type);
11067
11068       // Since this is not a PC-relative relocation, we may be
11069       // taking the address of a function.  In that case we need to
11070       // set the entry in the dynamic symbol table to the address of
11071       // the PLT entry.
11072       if (gsym->is_from_dynobj() && !parameters->options().shared())
11073         {
11074           gsym->set_needs_dynsym_value();
11075           // We distinguish between PLT entries and lazy-binding stubs by
11076           // giving the former an st_other value of STO_MIPS_PLT.  Set the
11077           // flag if there are any relocations in the binary where pointer
11078           // equality matters.
11079           if (mips_sym->pointer_equality_needed())
11080             mips_sym->set_mips_plt();
11081         }
11082     }
11083   if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11084     {
11085       // Absolute addressing relocations.
11086       // Make a dynamic relocation if necessary.
11087       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11088         {
11089           if (gsym->may_need_copy_reloc())
11090             {
11091               target->copy_reloc(symtab, layout, object, data_shndx,
11092                                  output_section, gsym, r_type, r_offset);
11093             }
11094           else if (can_make_dynamic)
11095             {
11096               // Create .rel.dyn section.
11097               target->rel_dyn_section(layout);
11098               target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11099                                     data_shndx, output_section, r_offset);
11100             }
11101           else
11102             gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11103                        gsym->name());
11104         }
11105     }
11106
11107   bool for_call = false;
11108   switch (r_type)
11109     {
11110     case elfcpp::R_MIPS_CALL16:
11111     case elfcpp::R_MIPS16_CALL16:
11112     case elfcpp::R_MICROMIPS_CALL16:
11113     case elfcpp::R_MIPS_CALL_HI16:
11114     case elfcpp::R_MIPS_CALL_LO16:
11115     case elfcpp::R_MICROMIPS_CALL_HI16:
11116     case elfcpp::R_MICROMIPS_CALL_LO16:
11117       for_call = true;
11118       // Fall through.
11119
11120     case elfcpp::R_MIPS16_GOT16:
11121     case elfcpp::R_MIPS_GOT16:
11122     case elfcpp::R_MIPS_GOT_HI16:
11123     case elfcpp::R_MIPS_GOT_LO16:
11124     case elfcpp::R_MICROMIPS_GOT16:
11125     case elfcpp::R_MICROMIPS_GOT_HI16:
11126     case elfcpp::R_MICROMIPS_GOT_LO16:
11127     case elfcpp::R_MIPS_GOT_DISP:
11128     case elfcpp::R_MICROMIPS_GOT_DISP:
11129     case elfcpp::R_MIPS_EH:
11130       {
11131         // The symbol requires a GOT entry.
11132         Mips_output_data_got<size, big_endian>* got =
11133           target->got_section(symtab, layout);
11134         got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11135                                       for_call);
11136         mips_sym->set_global_got_area(GGA_NORMAL);
11137       }
11138       break;
11139
11140     case elfcpp::R_MIPS_GOT_PAGE:
11141     case elfcpp::R_MICROMIPS_GOT_PAGE:
11142       {
11143         // This relocation needs a page entry in the GOT.
11144         // Get the section contents.
11145         section_size_type view_size = 0;
11146         const unsigned char* view =
11147           object->section_contents(data_shndx, &view_size, false);
11148         view += r_offset;
11149
11150         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11151         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11152                                                         : r_addend);
11153         Mips_output_data_got<size, big_endian>* got =
11154           target->got_section(symtab, layout);
11155         got->record_got_page_entry(mips_obj, r_sym, addend);
11156
11157         // If this is a global, overridable symbol, GOT_PAGE will
11158         // decay to GOT_DISP, so we'll need a GOT entry for it.
11159         bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11160                             && !mips_sym->object()->is_dynamic()
11161                             && !mips_sym->is_undefined());
11162         if (!def_regular
11163             || (parameters->options().output_is_position_independent()
11164                 && !parameters->options().Bsymbolic()
11165                 && !mips_sym->is_forced_local()))
11166           {
11167             got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11168                                           for_call);
11169             mips_sym->set_global_got_area(GGA_NORMAL);
11170           }
11171       }
11172       break;
11173
11174     case elfcpp::R_MIPS_TLS_GOTTPREL:
11175     case elfcpp::R_MIPS16_TLS_GOTTPREL:
11176     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11177     case elfcpp::R_MIPS_TLS_LDM:
11178     case elfcpp::R_MIPS16_TLS_LDM:
11179     case elfcpp::R_MICROMIPS_TLS_LDM:
11180     case elfcpp::R_MIPS_TLS_GD:
11181     case elfcpp::R_MIPS16_TLS_GD:
11182     case elfcpp::R_MICROMIPS_TLS_GD:
11183       {
11184         const bool is_final = gsym->final_value_is_known();
11185         const tls::Tls_optimization optimized_type =
11186           Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11187
11188         switch (r_type)
11189           {
11190           case elfcpp::R_MIPS_TLS_GD:
11191           case elfcpp::R_MIPS16_TLS_GD:
11192           case elfcpp::R_MICROMIPS_TLS_GD:
11193             if (optimized_type == tls::TLSOPT_NONE)
11194               {
11195                 // Create a pair of GOT entries for the module index and
11196                 // dtv-relative offset.
11197                 Mips_output_data_got<size, big_endian>* got =
11198                   target->got_section(symtab, layout);
11199                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11200                                               false);
11201               }
11202             else
11203               {
11204                 // FIXME: TLS optimization not supported yet.
11205                 gold_unreachable();
11206               }
11207             break;
11208
11209           case elfcpp::R_MIPS_TLS_LDM:
11210           case elfcpp::R_MIPS16_TLS_LDM:
11211           case elfcpp::R_MICROMIPS_TLS_LDM:
11212             if (optimized_type == tls::TLSOPT_NONE)
11213               {
11214                 // We always record LDM symbols as local with index 0.
11215                 target->got_section()->record_local_got_symbol(mips_obj, 0,
11216                                                                r_addend, r_type,
11217                                                                -1U, false);
11218               }
11219             else
11220               {
11221                 // FIXME: TLS optimization not supported yet.
11222                 gold_unreachable();
11223               }
11224             break;
11225           case elfcpp::R_MIPS_TLS_GOTTPREL:
11226           case elfcpp::R_MIPS16_TLS_GOTTPREL:
11227           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11228             layout->set_has_static_tls();
11229             if (optimized_type == tls::TLSOPT_NONE)
11230               {
11231                 // Create a GOT entry for the tp-relative offset.
11232                 Mips_output_data_got<size, big_endian>* got =
11233                   target->got_section(symtab, layout);
11234                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11235                                               false);
11236               }
11237             else
11238               {
11239                 // FIXME: TLS optimization not supported yet.
11240                 gold_unreachable();
11241               }
11242             break;
11243
11244           default:
11245             gold_unreachable();
11246         }
11247       }
11248       break;
11249     case elfcpp::R_MIPS_COPY:
11250     case elfcpp::R_MIPS_JUMP_SLOT:
11251       // These are relocations which should only be seen by the
11252       // dynamic linker, and should never be seen here.
11253       gold_error(_("%s: unexpected reloc %u in object file"),
11254                  object->name().c_str(), r_type);
11255       break;
11256
11257     default:
11258       break;
11259     }
11260
11261   // Refuse some position-dependent relocations when creating a
11262   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
11263   // not PIC, but we can create dynamic relocations and the result
11264   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
11265   // combined with R_MIPS_GOT16.
11266   if (parameters->options().shared())
11267     {
11268       switch (r_type)
11269         {
11270         case elfcpp::R_MIPS16_HI16:
11271         case elfcpp::R_MIPS_HI16:
11272         case elfcpp::R_MIPS_HIGHER:
11273         case elfcpp::R_MIPS_HIGHEST:
11274         case elfcpp::R_MICROMIPS_HI16:
11275         case elfcpp::R_MICROMIPS_HIGHER:
11276         case elfcpp::R_MICROMIPS_HIGHEST:
11277           // Don't refuse a high part relocation if it's against
11278           // no symbol (e.g. part of a compound relocation).
11279           if (r_sym == 0)
11280             break;
11281
11282           // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11283           // and has a special meaning.
11284           if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11285             break;
11286           // Fall through.
11287
11288         case elfcpp::R_MIPS16_26:
11289         case elfcpp::R_MIPS_26:
11290         case elfcpp::R_MICROMIPS_26_S1:
11291           gold_error(_("%s: relocation %u against `%s' can not be used when "
11292                        "making a shared object; recompile with -fPIC"),
11293                      object->name().c_str(), r_type, gsym->name());
11294         default:
11295           break;
11296         }
11297     }
11298 }
11299
11300 template<int size, bool big_endian>
11301 inline void
11302 Target_mips<size, big_endian>::Scan::global(
11303                                 Symbol_table* symtab,
11304                                 Layout* layout,
11305                                 Target_mips<size, big_endian>* target,
11306                                 Sized_relobj_file<size, big_endian>* object,
11307                                 unsigned int data_shndx,
11308                                 Output_section* output_section,
11309                                 const Relatype& reloc,
11310                                 unsigned int r_type,
11311                                 Symbol* gsym)
11312 {
11313   global(
11314     symtab,
11315     layout,
11316     target,
11317     object,
11318     data_shndx,
11319     output_section,
11320     &reloc,
11321     (const Reltype*) NULL,
11322     elfcpp::SHT_RELA,
11323     r_type,
11324     gsym);
11325 }
11326
11327 template<int size, bool big_endian>
11328 inline void
11329 Target_mips<size, big_endian>::Scan::global(
11330                                 Symbol_table* symtab,
11331                                 Layout* layout,
11332                                 Target_mips<size, big_endian>* target,
11333                                 Sized_relobj_file<size, big_endian>* object,
11334                                 unsigned int data_shndx,
11335                                 Output_section* output_section,
11336                                 const Reltype& reloc,
11337                                 unsigned int r_type,
11338                                 Symbol* gsym)
11339 {
11340   global(
11341     symtab,
11342     layout,
11343     target,
11344     object,
11345     data_shndx,
11346     output_section,
11347     (const Relatype*) NULL,
11348     &reloc,
11349     elfcpp::SHT_REL,
11350     r_type,
11351     gsym);
11352 }
11353
11354 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11355 // In cases where Scan::local() or Scan::global() has created
11356 // a dynamic relocation, the addend of the relocation is carried
11357 // in the data, and we must not apply the static relocation.
11358
11359 template<int size, bool big_endian>
11360 inline bool
11361 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11362     const Mips_symbol<size>* gsym,
11363     unsigned int r_type,
11364     Output_section* output_section,
11365     Target_mips* target)
11366 {
11367   // If the output section is not allocated, then we didn't call
11368   // scan_relocs, we didn't create a dynamic reloc, and we must apply
11369   // the reloc here.
11370   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11371       return true;
11372
11373   if (gsym == NULL)
11374     return true;
11375   else
11376     {
11377       // For global symbols, we use the same helper routines used in the
11378       // scan pass.
11379       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11380           && !gsym->may_need_copy_reloc())
11381         {
11382           // We have generated dynamic reloc (R_MIPS_REL32).
11383
11384           bool multi_got = false;
11385           if (target->has_got_section())
11386             multi_got = target->got_section()->multi_got();
11387           bool has_got_offset;
11388           if (!multi_got)
11389             has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11390           else
11391             has_got_offset = gsym->global_gotoffset() != -1U;
11392           if (!has_got_offset)
11393             return true;
11394           else
11395             // Apply the relocation only if the symbol is in the local got.
11396             // Do not apply the relocation if the symbol is in the global
11397             // got.
11398             return symbol_references_local(gsym, gsym->has_dynsym_index());
11399         }
11400       else
11401         // We have not generated dynamic reloc.
11402         return true;
11403     }
11404 }
11405
11406 // Perform a relocation.
11407
11408 template<int size, bool big_endian>
11409 inline bool
11410 Target_mips<size, big_endian>::Relocate::relocate(
11411                         const Relocate_info<size, big_endian>* relinfo,
11412                         unsigned int rel_type,
11413                         Target_mips* target,
11414                         Output_section* output_section,
11415                         size_t relnum,
11416                         const unsigned char* preloc,
11417                         const Sized_symbol<size>* gsym,
11418                         const Symbol_value<size>* psymval,
11419                         unsigned char* view,
11420                         Mips_address address,
11421                         section_size_type)
11422 {
11423   Mips_address r_offset;
11424   unsigned int r_sym;
11425   unsigned int r_type;
11426   unsigned int r_type2;
11427   unsigned int r_type3;
11428   unsigned char r_ssym;
11429   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11430   // r_offset and r_type of the next relocation is needed for resolving multiple
11431   // consecutive relocations with the same offset.
11432   Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11433   unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11434
11435   elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11436   size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
11437
11438   if (rel_type == elfcpp::SHT_RELA)
11439     {
11440       const Relatype rela(preloc);
11441       r_offset = rela.get_r_offset();
11442       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11443           get_r_sym(&rela);
11444       r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11445           get_r_type(&rela);
11446       r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11447           get_r_type2(&rela);
11448       r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11449           get_r_type3(&rela);
11450       r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11451           get_r_ssym(&rela);
11452       r_addend = rela.get_r_addend();
11453       // If this is not last relocation, get r_offset and r_type of the next
11454       // relocation.
11455       if (relnum + 1 < reloc_count)
11456         {
11457           const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11458           const Relatype next_rela(preloc + reloc_size);
11459           next_r_offset = next_rela.get_r_offset();
11460           next_r_type =
11461             Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11462               get_r_type(&next_rela);
11463         }
11464     }
11465   else
11466     {
11467       const Reltype rel(preloc);
11468       r_offset = rel.get_r_offset();
11469       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11470           get_r_sym(&rel);
11471       r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11472           get_r_type(&rel);
11473       r_ssym = 0;
11474       r_type2 = elfcpp::R_MIPS_NONE;
11475       r_type3 = elfcpp::R_MIPS_NONE;
11476       r_addend = 0;
11477       // If this is not last relocation, get r_offset and r_type of the next
11478       // relocation.
11479       if (relnum + 1 < reloc_count)
11480         {
11481           const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11482           const Reltype next_rel(preloc + reloc_size);
11483           next_r_offset = next_rel.get_r_offset();
11484           next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11485             get_r_type(&next_rel);
11486         }
11487     }
11488
11489   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11490   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11491
11492   Mips_relobj<size, big_endian>* object =
11493       Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11494
11495   bool target_is_16_bit_code = false;
11496   bool target_is_micromips_code = false;
11497   bool cross_mode_jump;
11498
11499   Symbol_value<size> symval;
11500
11501   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11502
11503   bool changed_symbol_value = false;
11504   if (gsym == NULL)
11505     {
11506       target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11507       target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11508       if (target_is_16_bit_code || target_is_micromips_code)
11509         {
11510           // MIPS16/microMIPS text labels should be treated as odd.
11511           symval.set_output_value(psymval->value(object, 1));
11512           psymval = &symval;
11513           changed_symbol_value = true;
11514         }
11515     }
11516   else
11517     {
11518       target_is_16_bit_code = mips_sym->is_mips16();
11519       target_is_micromips_code = mips_sym->is_micromips();
11520
11521       // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11522       // it odd.  This will cause something like .word SYM to come up with
11523       // the right value when it is loaded into the PC.
11524
11525       if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11526           && psymval->value(object, 0) != 0)
11527         {
11528           symval.set_output_value(psymval->value(object, 0) | 1);
11529           psymval = &symval;
11530           changed_symbol_value = true;
11531         }
11532
11533       // Pick the value to use for symbols defined in shared objects.
11534       if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11535           || mips_sym->has_lazy_stub())
11536         {
11537           Mips_address value;
11538           if (!mips_sym->has_lazy_stub())
11539             {
11540               // Prefer a standard MIPS PLT entry.
11541               if (mips_sym->has_mips_plt_offset())
11542                 {
11543                   value = target->plt_section()->mips_entry_address(mips_sym);
11544                   target_is_micromips_code = false;
11545                   target_is_16_bit_code = false;
11546                 }
11547               else
11548                 {
11549                   value = (target->plt_section()->comp_entry_address(mips_sym)
11550                            + 1);
11551                   if (target->is_output_micromips())
11552                     target_is_micromips_code = true;
11553                   else
11554                     target_is_16_bit_code = true;
11555                 }
11556             }
11557           else
11558             value = target->mips_stubs_section()->stub_address(mips_sym);
11559
11560           symval.set_output_value(value);
11561           psymval = &symval;
11562         }
11563     }
11564
11565   // TRUE if the symbol referred to by this relocation is "_gp_disp".
11566   // Note that such a symbol must always be a global symbol.
11567   bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11568                   && !object->is_newabi());
11569
11570   // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11571   // Note that such a symbol must always be a global symbol.
11572   bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11573
11574
11575   if (gp_disp)
11576     {
11577       if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11578         gold_error_at_location(relinfo, relnum, r_offset,
11579           _("relocations against _gp_disp are permitted only"
11580             " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11581     }
11582   else if (gnu_local_gp)
11583     {
11584       // __gnu_local_gp is _gp symbol.
11585       symval.set_output_value(target->adjusted_gp_value(object));
11586       psymval = &symval;
11587     }
11588
11589   // If this is a reference to a 16-bit function with a stub, we need
11590   // to redirect the relocation to the stub unless:
11591   //
11592   // (a) the relocation is for a MIPS16 JAL;
11593   //
11594   // (b) the relocation is for a MIPS16 PIC call, and there are no
11595   //     non-MIPS16 uses of the GOT slot; or
11596   //
11597   // (c) the section allows direct references to MIPS16 functions.
11598   if (r_type != elfcpp::R_MIPS16_26
11599       && ((mips_sym != NULL
11600            && mips_sym->has_mips16_fn_stub()
11601            && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11602           || (mips_sym == NULL
11603               && object->get_local_mips16_fn_stub(r_sym) != NULL))
11604       && !object->section_allows_mips16_refs(relinfo->data_shndx))
11605     {
11606       // This is a 32- or 64-bit call to a 16-bit function.  We should
11607       // have already noticed that we were going to need the
11608       // stub.
11609       Mips_address value;
11610       if (mips_sym == NULL)
11611         value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11612       else
11613         {
11614           gold_assert(mips_sym->need_fn_stub());
11615           if (mips_sym->has_la25_stub())
11616             value = target->la25_stub_section()->stub_address(mips_sym);
11617           else
11618             {
11619               value = mips_sym->template
11620                       get_mips16_fn_stub<big_endian>()->output_address();
11621             }
11622           }
11623       symval.set_output_value(value);
11624       psymval = &symval;
11625       changed_symbol_value = true;
11626
11627       // The target is 16-bit, but the stub isn't.
11628       target_is_16_bit_code = false;
11629     }
11630   // If this is a MIPS16 call with a stub, that is made through the PLT or
11631   // to a standard MIPS function, we need to redirect the call to the stub.
11632   // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11633   // indirect calls should use an indirect stub instead.
11634   else if (r_type == elfcpp::R_MIPS16_26
11635            && ((mips_sym != NULL
11636                 && (mips_sym->has_mips16_call_stub()
11637                     || mips_sym->has_mips16_call_fp_stub()))
11638                || (mips_sym == NULL
11639                    && object->get_local_mips16_call_stub(r_sym) != NULL))
11640            && ((mips_sym != NULL && mips_sym->has_plt_offset())
11641                || !target_is_16_bit_code))
11642     {
11643       Mips16_stub_section<size, big_endian>* call_stub;
11644       if (mips_sym == NULL)
11645         call_stub = object->get_local_mips16_call_stub(r_sym);
11646       else
11647         {
11648           // If both call_stub and call_fp_stub are defined, we can figure
11649           // out which one to use by checking which one appears in the input
11650           // file.
11651           if (mips_sym->has_mips16_call_stub()
11652               && mips_sym->has_mips16_call_fp_stub())
11653             {
11654               call_stub = NULL;
11655               for (unsigned int i = 1; i < object->shnum(); ++i)
11656                 {
11657                   if (object->is_mips16_call_fp_stub_section(i))
11658                     {
11659                       call_stub = mips_sym->template
11660                                   get_mips16_call_fp_stub<big_endian>();
11661                       break;
11662                     }
11663
11664                 }
11665               if (call_stub == NULL)
11666                 call_stub =
11667                   mips_sym->template get_mips16_call_stub<big_endian>();
11668             }
11669           else if (mips_sym->has_mips16_call_stub())
11670             call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11671           else
11672             call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11673         }
11674
11675       symval.set_output_value(call_stub->output_address());
11676       psymval = &symval;
11677       changed_symbol_value = true;
11678     }
11679   // If this is a direct call to a PIC function, redirect to the
11680   // non-PIC stub.
11681   else if (mips_sym != NULL
11682            && mips_sym->has_la25_stub()
11683            && relocation_needs_la25_stub<size, big_endian>(
11684                                        object, r_type, target_is_16_bit_code))
11685     {
11686       Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11687       if (mips_sym->is_micromips())
11688         value += 1;
11689       symval.set_output_value(value);
11690       psymval = &symval;
11691     }
11692   // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11693   // entry is used if a standard PLT entry has also been made.
11694   else if ((r_type == elfcpp::R_MIPS16_26
11695             || r_type == elfcpp::R_MICROMIPS_26_S1)
11696           && mips_sym != NULL
11697           && mips_sym->has_plt_offset()
11698           && mips_sym->has_comp_plt_offset()
11699           && mips_sym->has_mips_plt_offset())
11700     {
11701       Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11702                             + 1);
11703       symval.set_output_value(value);
11704       psymval = &symval;
11705
11706       target_is_16_bit_code = !target->is_output_micromips();
11707       target_is_micromips_code = target->is_output_micromips();
11708     }
11709
11710   // Make sure MIPS16 and microMIPS are not used together.
11711   if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11712       || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11713    {
11714       gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11715    }
11716
11717   // Calls from 16-bit code to 32-bit code and vice versa require the
11718   // mode change.  However, we can ignore calls to undefined weak symbols,
11719   // which should never be executed at runtime.  This exception is important
11720   // because the assembly writer may have "known" that any definition of the
11721   // symbol would be 16-bit code, and that direct jumps were therefore
11722   // acceptable.
11723   cross_mode_jump =
11724     (!(gsym != NULL && gsym->is_weak_undefined())
11725      && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11726          || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11727          || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11728              && (target_is_16_bit_code || target_is_micromips_code))));
11729
11730   bool local = (mips_sym == NULL
11731                 || (mips_sym->got_only_for_calls()
11732                     ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11733                     : symbol_references_local(mips_sym,
11734                                               mips_sym->has_dynsym_index())));
11735
11736   // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11737   // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
11738   // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11739   if (got_page_reloc(r_type) && !local)
11740     r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11741                                       : elfcpp::R_MIPS_GOT_DISP);
11742
11743   unsigned int got_offset = 0;
11744   int gp_offset = 0;
11745
11746   // Whether we have to extract addend from instruction.
11747   bool extract_addend = rel_type == elfcpp::SHT_REL;
11748   unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11749
11750   Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11751
11752   // For Mips64 N64 ABI, there may be up to three operations specified per
11753   // record, by the fields r_type, r_type2, and r_type3. The first operation
11754   // takes its addend from the relocation record. Each subsequent operation
11755   // takes as its addend the result of the previous operation.
11756   // The first operation in a record which references a symbol uses the symbol
11757   // implied by r_sym. The next operation in a record which references a symbol
11758   // uses the special symbol value given by the r_ssym field. A third operation
11759   // in a record which references a symbol will assume a NULL symbol,
11760   // i.e. value zero.
11761
11762   // TODO(Vladimir)
11763   // Check if a record references to a symbol.
11764   for (unsigned int i = 0; i < 3; ++i)
11765     {
11766       if (r_types[i] == elfcpp::R_MIPS_NONE)
11767         break;
11768
11769       // If we didn't apply previous relocation, use its result as addend
11770       // for current.
11771       if (this->calculate_only_)
11772         {
11773           r_addend = this->calculated_value_;
11774           extract_addend = false;
11775         }
11776
11777       // In the N32 and 64-bit ABIs there may be multiple consecutive
11778       // relocations for the same offset.  In that case we are
11779       // supposed to treat the output of each relocation as the addend
11780       // for the next.  For N64 ABI, we are checking offsets only in a
11781       // third operation in a record (r_type3).
11782       this->calculate_only_ =
11783         (object->is_n64() && i < 2
11784          ? r_types[i+1] != elfcpp::R_MIPS_NONE
11785          : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
11786
11787       if (object->is_n64())
11788         {
11789           if (i == 1)
11790             {
11791               // Handle special symbol for r_type2 relocation type.
11792               switch (r_ssym)
11793                 {
11794                 case RSS_UNDEF:
11795                   symval.set_output_value(0);
11796                   break;
11797                 case RSS_GP:
11798                   symval.set_output_value(target->gp_value());
11799                   break;
11800                 case RSS_GP0:
11801                   symval.set_output_value(object->gp_value());
11802                   break;
11803                 case RSS_LOC:
11804                   symval.set_output_value(address);
11805                   break;
11806                 default:
11807                   gold_unreachable();
11808                 }
11809               psymval = &symval;
11810             }
11811           else if (i == 2)
11812            {
11813             // For r_type3 symbol value is 0.
11814             symval.set_output_value(0);
11815            }
11816         }
11817
11818       bool update_got_entry = false;
11819       switch (r_types[i])
11820         {
11821         case elfcpp::R_MIPS_NONE:
11822           break;
11823         case elfcpp::R_MIPS_16:
11824           reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11825                                             extract_addend,
11826                                             this->calculate_only_,
11827                                             &this->calculated_value_);
11828           break;
11829
11830         case elfcpp::R_MIPS_32:
11831           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11832                                         target))
11833             reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11834                                               extract_addend,
11835                                               this->calculate_only_,
11836                                               &this->calculated_value_);
11837           if (mips_sym != NULL
11838               && (mips_sym->is_mips16() || mips_sym->is_micromips())
11839               && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11840             {
11841               // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11842               // already updated by adding +1.
11843               if (mips_sym->has_mips16_fn_stub())
11844                 {
11845                   gold_assert(mips_sym->need_fn_stub());
11846                   Mips16_stub_section<size, big_endian>* fn_stub =
11847                     mips_sym->template get_mips16_fn_stub<big_endian>();
11848
11849                   symval.set_output_value(fn_stub->output_address());
11850                   psymval = &symval;
11851                 }
11852               got_offset = mips_sym->global_gotoffset();
11853               update_got_entry = true;
11854             }
11855           break;
11856
11857         case elfcpp::R_MIPS_64:
11858           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11859                                         target))
11860             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11861                                               extract_addend,
11862                                               this->calculate_only_,
11863                                               &this->calculated_value_, false);
11864           else if (target->is_output_n64() && r_addend != 0)
11865             // Only apply the addend.  The static relocation was RELA, but the
11866             // dynamic relocation is REL, so we need to apply the addend.
11867             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11868                                               extract_addend,
11869                                               this->calculate_only_,
11870                                               &this->calculated_value_, true);
11871           break;
11872         case elfcpp::R_MIPS_REL32:
11873           gold_unreachable();
11874
11875         case elfcpp::R_MIPS_PC32:
11876           reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11877                                               r_addend, extract_addend,
11878                                               this->calculate_only_,
11879                                               &this->calculated_value_);
11880           break;
11881
11882         case elfcpp::R_MIPS16_26:
11883           // The calculation for R_MIPS16_26 is just the same as for an
11884           // R_MIPS_26.  It's only the storage of the relocated field into
11885           // the output file that's different.  So, we just fall through to the
11886           // R_MIPS_26 case here.
11887         case elfcpp::R_MIPS_26:
11888         case elfcpp::R_MICROMIPS_26_S1:
11889           reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11890               gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11891               r_types[i], target->jal_to_bal(), this->calculate_only_,
11892               &this->calculated_value_);
11893           break;
11894
11895         case elfcpp::R_MIPS_HI16:
11896         case elfcpp::R_MIPS16_HI16:
11897         case elfcpp::R_MICROMIPS_HI16:
11898           if (rel_type == elfcpp::SHT_RELA)
11899             reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11900                                                    r_addend, address,
11901                                                    gp_disp, r_types[i],
11902                                                    extract_addend, 0,
11903                                                    target,
11904                                                    this->calculate_only_,
11905                                                    &this->calculated_value_);
11906           else if (rel_type == elfcpp::SHT_REL)
11907             reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11908                                                 address, gp_disp, r_types[i],
11909                                                 r_sym, extract_addend);
11910           else
11911             gold_unreachable();
11912           break;
11913
11914         case elfcpp::R_MIPS_LO16:
11915         case elfcpp::R_MIPS16_LO16:
11916         case elfcpp::R_MICROMIPS_LO16:
11917         case elfcpp::R_MICROMIPS_HI0_LO16:
11918           reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11919                                               r_addend, extract_addend, address,
11920                                               gp_disp, r_types[i], r_sym,
11921                                               rel_type, this->calculate_only_,
11922                                               &this->calculated_value_);
11923           break;
11924
11925         case elfcpp::R_MIPS_LITERAL:
11926         case elfcpp::R_MICROMIPS_LITERAL:
11927           // Because we don't merge literal sections, we can handle this
11928           // just like R_MIPS_GPREL16.  In the long run, we should merge
11929           // shared literals, and then we will need to additional work
11930           // here.
11931
11932           // Fall through.
11933
11934         case elfcpp::R_MIPS_GPREL16:
11935         case elfcpp::R_MIPS16_GPREL:
11936         case elfcpp::R_MICROMIPS_GPREL7_S2:
11937         case elfcpp::R_MICROMIPS_GPREL16:
11938           reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11939                                              target->adjusted_gp_value(object),
11940                                              r_addend, extract_addend,
11941                                              gsym == NULL, r_types[i],
11942                                              this->calculate_only_,
11943                                              &this->calculated_value_);
11944           break;
11945
11946         case elfcpp::R_MIPS_PC16:
11947           reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11948                                               r_addend, extract_addend,
11949                                               this->calculate_only_,
11950                                               &this->calculated_value_);
11951           break;
11952
11953         case elfcpp::R_MIPS_PC21_S2:
11954           reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11955                                               r_addend, extract_addend,
11956                                               this->calculate_only_,
11957                                               &this->calculated_value_);
11958           break;
11959
11960         case elfcpp::R_MIPS_PC26_S2:
11961           reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11962                                               r_addend, extract_addend,
11963                                               this->calculate_only_,
11964                                               &this->calculated_value_);
11965           break;
11966
11967         case elfcpp::R_MIPS_PC18_S3:
11968           reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11969                                               r_addend, extract_addend,
11970                                               this->calculate_only_,
11971                                               &this->calculated_value_);
11972           break;
11973
11974         case elfcpp::R_MIPS_PC19_S2:
11975           reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
11976                                               r_addend, extract_addend,
11977                                               this->calculate_only_,
11978                                               &this->calculated_value_);
11979           break;
11980
11981         case elfcpp::R_MIPS_PCHI16:
11982           if (rel_type == elfcpp::SHT_RELA)
11983             reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
11984                                                      r_addend, address,
11985                                                      extract_addend, 0,
11986                                                      this->calculate_only_,
11987                                                      &this->calculated_value_);
11988           else if (rel_type == elfcpp::SHT_REL)
11989             reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
11990                                                   r_addend, address, r_sym,
11991                                                   extract_addend);
11992           else
11993             gold_unreachable();
11994           break;
11995
11996         case elfcpp::R_MIPS_PCLO16:
11997           reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
11998                                                 extract_addend, address, r_sym,
11999                                                 rel_type, this->calculate_only_,
12000                                                 &this->calculated_value_);
12001           break;
12002         case elfcpp::R_MICROMIPS_PC7_S1:
12003           reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
12004                                                       address, r_addend,
12005                                                       extract_addend,
12006                                                       this->calculate_only_,
12007                                                       &this->calculated_value_);
12008           break;
12009         case elfcpp::R_MICROMIPS_PC10_S1:
12010           reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
12011                                                       psymval, address,
12012                                                       r_addend, extract_addend,
12013                                                       this->calculate_only_,
12014                                                       &this->calculated_value_);
12015           break;
12016         case elfcpp::R_MICROMIPS_PC16_S1:
12017           reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
12018                                                       psymval, address,
12019                                                       r_addend, extract_addend,
12020                                                       this->calculate_only_,
12021                                                       &this->calculated_value_);
12022           break;
12023         case elfcpp::R_MIPS_GPREL32:
12024           reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12025                                               target->adjusted_gp_value(object),
12026                                               r_addend, extract_addend,
12027                                               this->calculate_only_,
12028                                               &this->calculated_value_);
12029           break;
12030         case elfcpp::R_MIPS_GOT_HI16:
12031         case elfcpp::R_MIPS_CALL_HI16:
12032         case elfcpp::R_MICROMIPS_GOT_HI16:
12033         case elfcpp::R_MICROMIPS_CALL_HI16:
12034           if (gsym != NULL)
12035             got_offset = target->got_section()->got_offset(gsym,
12036                                                            GOT_TYPE_STANDARD,
12037                                                            object);
12038           else
12039             got_offset = target->got_section()->got_offset(r_sym,
12040                                                            GOT_TYPE_STANDARD,
12041                                                            object, r_addend);
12042           gp_offset = target->got_section()->gp_offset(got_offset, object);
12043           reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
12044                                                   this->calculate_only_,
12045                                                   &this->calculated_value_);
12046           update_got_entry = changed_symbol_value;
12047           break;
12048
12049         case elfcpp::R_MIPS_GOT_LO16:
12050         case elfcpp::R_MIPS_CALL_LO16:
12051         case elfcpp::R_MICROMIPS_GOT_LO16:
12052         case elfcpp::R_MICROMIPS_CALL_LO16:
12053           if (gsym != NULL)
12054             got_offset = target->got_section()->got_offset(gsym,
12055                                                            GOT_TYPE_STANDARD,
12056                                                            object);
12057           else
12058             got_offset = target->got_section()->got_offset(r_sym,
12059                                                            GOT_TYPE_STANDARD,
12060                                                            object, r_addend);
12061           gp_offset = target->got_section()->gp_offset(got_offset, object);
12062           reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
12063                                                   this->calculate_only_,
12064                                                   &this->calculated_value_);
12065           update_got_entry = changed_symbol_value;
12066           break;
12067
12068         case elfcpp::R_MIPS_GOT_DISP:
12069         case elfcpp::R_MICROMIPS_GOT_DISP:
12070         case elfcpp::R_MIPS_EH:
12071           if (gsym != NULL)
12072             got_offset = target->got_section()->got_offset(gsym,
12073                                                            GOT_TYPE_STANDARD,
12074                                                            object);
12075           else
12076             got_offset = target->got_section()->got_offset(r_sym,
12077                                                            GOT_TYPE_STANDARD,
12078                                                            object, r_addend);
12079           gp_offset = target->got_section()->gp_offset(got_offset, object);
12080           if (eh_reloc(r_types[i]))
12081             reloc_status = Reloc_funcs::releh(view, gp_offset,
12082                                               this->calculate_only_,
12083                                               &this->calculated_value_);
12084           else
12085             reloc_status = Reloc_funcs::relgot(view, gp_offset,
12086                                                this->calculate_only_,
12087                                                &this->calculated_value_);
12088           break;
12089         case elfcpp::R_MIPS_CALL16:
12090         case elfcpp::R_MIPS16_CALL16:
12091         case elfcpp::R_MICROMIPS_CALL16:
12092           gold_assert(gsym != NULL);
12093           got_offset = target->got_section()->got_offset(gsym,
12094                                                          GOT_TYPE_STANDARD,
12095                                                          object);
12096           gp_offset = target->got_section()->gp_offset(got_offset, object);
12097           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12098                                              this->calculate_only_,
12099                                              &this->calculated_value_);
12100           // TODO(sasa): We should also initialize update_got_entry
12101           // in other place swhere relgot is called.
12102           update_got_entry = changed_symbol_value;
12103           break;
12104
12105         case elfcpp::R_MIPS_GOT16:
12106         case elfcpp::R_MIPS16_GOT16:
12107         case elfcpp::R_MICROMIPS_GOT16:
12108           if (gsym != NULL)
12109             {
12110               got_offset = target->got_section()->got_offset(gsym,
12111                                                              GOT_TYPE_STANDARD,
12112                                                              object);
12113               gp_offset = target->got_section()->gp_offset(got_offset, object);
12114               reloc_status = Reloc_funcs::relgot(view, gp_offset,
12115                                                  this->calculate_only_,
12116                                                  &this->calculated_value_);
12117             }
12118           else
12119             {
12120               if (rel_type == elfcpp::SHT_RELA)
12121                 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
12122                                                       psymval, r_addend,
12123                                                       extract_addend, 0,
12124                                                       target,
12125                                                       this->calculate_only_,
12126                                                       &this->calculated_value_);
12127               else if (rel_type == elfcpp::SHT_REL)
12128                 reloc_status = Reloc_funcs::relgot16_local(view, object,
12129                                                            psymval, r_addend,
12130                                                            extract_addend,
12131                                                            r_types[i], r_sym);
12132               else
12133                 gold_unreachable();
12134             }
12135           update_got_entry = changed_symbol_value;
12136           break;
12137
12138         case elfcpp::R_MIPS_TLS_GD:
12139         case elfcpp::R_MIPS16_TLS_GD:
12140         case elfcpp::R_MICROMIPS_TLS_GD:
12141           if (gsym != NULL)
12142             got_offset = target->got_section()->got_offset(gsym,
12143                                                            GOT_TYPE_TLS_PAIR,
12144                                                            object);
12145           else
12146             got_offset = target->got_section()->got_offset(r_sym,
12147                                                            GOT_TYPE_TLS_PAIR,
12148                                                            object, r_addend);
12149           gp_offset = target->got_section()->gp_offset(got_offset, object);
12150           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12151                                              this->calculate_only_,
12152                                              &this->calculated_value_);
12153           break;
12154
12155         case elfcpp::R_MIPS_TLS_GOTTPREL:
12156         case elfcpp::R_MIPS16_TLS_GOTTPREL:
12157         case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12158           if (gsym != NULL)
12159             got_offset = target->got_section()->got_offset(gsym,
12160                                                            GOT_TYPE_TLS_OFFSET,
12161                                                            object);
12162           else
12163             got_offset = target->got_section()->got_offset(r_sym,
12164                                                            GOT_TYPE_TLS_OFFSET,
12165                                                            object, r_addend);
12166           gp_offset = target->got_section()->gp_offset(got_offset, object);
12167           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12168                                              this->calculate_only_,
12169                                              &this->calculated_value_);
12170           break;
12171
12172         case elfcpp::R_MIPS_TLS_LDM:
12173         case elfcpp::R_MIPS16_TLS_LDM:
12174         case elfcpp::R_MICROMIPS_TLS_LDM:
12175           // Relocate the field with the offset of the GOT entry for
12176           // the module index.
12177           got_offset = target->got_section()->tls_ldm_offset(object);
12178           gp_offset = target->got_section()->gp_offset(got_offset, object);
12179           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12180                                              this->calculate_only_,
12181                                              &this->calculated_value_);
12182           break;
12183
12184         case elfcpp::R_MIPS_GOT_PAGE:
12185         case elfcpp::R_MICROMIPS_GOT_PAGE:
12186           reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12187                                                  r_addend, extract_addend,
12188                                                  this->calculate_only_,
12189                                                  &this->calculated_value_);
12190           break;
12191
12192         case elfcpp::R_MIPS_GOT_OFST:
12193         case elfcpp::R_MICROMIPS_GOT_OFST:
12194           reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12195                                                  r_addend, extract_addend,
12196                                                  local, this->calculate_only_,
12197                                                  &this->calculated_value_);
12198           break;
12199
12200         case elfcpp::R_MIPS_JALR:
12201         case elfcpp::R_MICROMIPS_JALR:
12202           // This relocation is only a hint.  In some cases, we optimize
12203           // it into a bal instruction.  But we don't try to optimize
12204           // when the symbol does not resolve locally.
12205           if (gsym == NULL
12206               || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12207             reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12208                                                 r_addend, extract_addend,
12209                                                 cross_mode_jump, r_types[i],
12210                                                 target->jalr_to_bal(),
12211                                                 target->jr_to_b(),
12212                                                 this->calculate_only_,
12213                                                 &this->calculated_value_);
12214           break;
12215
12216         case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12217         case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12218         case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12219           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12220                                                  elfcpp::DTP_OFFSET, r_addend,
12221                                                  extract_addend,
12222                                                  this->calculate_only_,
12223                                                  &this->calculated_value_);
12224           break;
12225         case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12226         case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12227         case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12228           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12229                                                  elfcpp::DTP_OFFSET, r_addend,
12230                                                  extract_addend,
12231                                                  this->calculate_only_,
12232                                                  &this->calculated_value_);
12233           break;
12234         case elfcpp::R_MIPS_TLS_DTPREL32:
12235         case elfcpp::R_MIPS_TLS_DTPREL64:
12236           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12237                                                elfcpp::DTP_OFFSET, r_addend,
12238                                                extract_addend,
12239                                                this->calculate_only_,
12240                                                &this->calculated_value_);
12241           break;
12242         case elfcpp::R_MIPS_TLS_TPREL_HI16:
12243         case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12244         case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12245           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12246                                                  elfcpp::TP_OFFSET, r_addend,
12247                                                  extract_addend,
12248                                                  this->calculate_only_,
12249                                                  &this->calculated_value_);
12250           break;
12251         case elfcpp::R_MIPS_TLS_TPREL_LO16:
12252         case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12253         case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12254           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12255                                                  elfcpp::TP_OFFSET, r_addend,
12256                                                  extract_addend,
12257                                                  this->calculate_only_,
12258                                                  &this->calculated_value_);
12259           break;
12260         case elfcpp::R_MIPS_TLS_TPREL32:
12261         case elfcpp::R_MIPS_TLS_TPREL64:
12262           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12263                                                elfcpp::TP_OFFSET, r_addend,
12264                                                extract_addend,
12265                                                this->calculate_only_,
12266                                                &this->calculated_value_);
12267           break;
12268         case elfcpp::R_MIPS_SUB:
12269         case elfcpp::R_MICROMIPS_SUB:
12270           reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12271                                              extract_addend,
12272                                              this->calculate_only_,
12273                                              &this->calculated_value_);
12274           break;
12275         case elfcpp::R_MIPS_HIGHER:
12276         case elfcpp::R_MICROMIPS_HIGHER:
12277           reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
12278                                                 extract_addend,
12279                                                 this->calculate_only_,
12280                                                 &this->calculated_value_);
12281           break;
12282         case elfcpp::R_MIPS_HIGHEST:
12283         case elfcpp::R_MICROMIPS_HIGHEST:
12284           reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12285                                                  r_addend, extract_addend,
12286                                                  this->calculate_only_,
12287                                                  &this->calculated_value_);
12288           break;
12289         default:
12290           gold_error_at_location(relinfo, relnum, r_offset,
12291                                  _("unsupported reloc %u"), r_types[i]);
12292           break;
12293         }
12294
12295       if (update_got_entry)
12296         {
12297           Mips_output_data_got<size, big_endian>* got = target->got_section();
12298           if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12299             got->update_got_entry(got->get_primary_got_offset(mips_sym),
12300                                   psymval->value(object, 0));
12301           else
12302             got->update_got_entry(got_offset, psymval->value(object, 0));
12303         }
12304     }
12305
12306   bool jal_shuffle = jal_reloc(r_type);
12307   Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12308
12309   // Report any errors.
12310   switch (reloc_status)
12311     {
12312     case Reloc_funcs::STATUS_OKAY:
12313       break;
12314     case Reloc_funcs::STATUS_OVERFLOW:
12315       if (gsym == NULL)
12316         gold_error_at_location(relinfo, relnum, r_offset,
12317                                _("relocation overflow: "
12318                                  "%u against local symbol %u in %s"),
12319                                r_type, r_sym, object->name().c_str());
12320       else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12321         gold_error_at_location(relinfo, relnum, r_offset,
12322                                _("relocation overflow: "
12323                                  "%u against '%s' defined in %s"),
12324                                r_type, gsym->demangled_name().c_str(),
12325                                gsym->object()->name().c_str());
12326       else
12327         gold_error_at_location(relinfo, relnum, r_offset,
12328                                _("relocation overflow: %u against '%s'"),
12329                                r_type, gsym->demangled_name().c_str());
12330       break;
12331     case Reloc_funcs::STATUS_BAD_RELOC:
12332       gold_error_at_location(relinfo, relnum, r_offset,
12333         _("unexpected opcode while processing relocation"));
12334       break;
12335     case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12336       gold_error_at_location(relinfo, relnum, r_offset,
12337         _("unaligned PC-relative relocation"));
12338       break;
12339     default:
12340       gold_unreachable();
12341     }
12342
12343   return true;
12344 }
12345
12346 // Get the Reference_flags for a particular relocation.
12347
12348 template<int size, bool big_endian>
12349 int
12350 Target_mips<size, big_endian>::Scan::get_reference_flags(
12351                        unsigned int r_type)
12352 {
12353   switch (r_type)
12354     {
12355     case elfcpp::R_MIPS_NONE:
12356       // No symbol reference.
12357       return 0;
12358
12359     case elfcpp::R_MIPS_16:
12360     case elfcpp::R_MIPS_32:
12361     case elfcpp::R_MIPS_64:
12362     case elfcpp::R_MIPS_HI16:
12363     case elfcpp::R_MIPS_LO16:
12364     case elfcpp::R_MIPS_HIGHER:
12365     case elfcpp::R_MIPS_HIGHEST:
12366     case elfcpp::R_MIPS16_HI16:
12367     case elfcpp::R_MIPS16_LO16:
12368     case elfcpp::R_MICROMIPS_HI16:
12369     case elfcpp::R_MICROMIPS_LO16:
12370     case elfcpp::R_MICROMIPS_HIGHER:
12371     case elfcpp::R_MICROMIPS_HIGHEST:
12372       return Symbol::ABSOLUTE_REF;
12373
12374     case elfcpp::R_MIPS_26:
12375     case elfcpp::R_MIPS16_26:
12376     case elfcpp::R_MICROMIPS_26_S1:
12377       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12378
12379     case elfcpp::R_MIPS_PC18_S3:
12380     case elfcpp::R_MIPS_PC19_S2:
12381     case elfcpp::R_MIPS_PCHI16:
12382     case elfcpp::R_MIPS_PCLO16:
12383     case elfcpp::R_MIPS_GPREL32:
12384     case elfcpp::R_MIPS_GPREL16:
12385     case elfcpp::R_MIPS_REL32:
12386     case elfcpp::R_MIPS16_GPREL:
12387       return Symbol::RELATIVE_REF;
12388
12389     case elfcpp::R_MIPS_PC16:
12390     case elfcpp::R_MIPS_PC32:
12391     case elfcpp::R_MIPS_PC21_S2:
12392     case elfcpp::R_MIPS_PC26_S2:
12393     case elfcpp::R_MIPS_JALR:
12394     case elfcpp::R_MICROMIPS_JALR:
12395       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12396
12397     case elfcpp::R_MIPS_GOT16:
12398     case elfcpp::R_MIPS_CALL16:
12399     case elfcpp::R_MIPS_GOT_DISP:
12400     case elfcpp::R_MIPS_GOT_HI16:
12401     case elfcpp::R_MIPS_GOT_LO16:
12402     case elfcpp::R_MIPS_CALL_HI16:
12403     case elfcpp::R_MIPS_CALL_LO16:
12404     case elfcpp::R_MIPS_LITERAL:
12405     case elfcpp::R_MIPS_GOT_PAGE:
12406     case elfcpp::R_MIPS_GOT_OFST:
12407     case elfcpp::R_MIPS16_GOT16:
12408     case elfcpp::R_MIPS16_CALL16:
12409     case elfcpp::R_MICROMIPS_GOT16:
12410     case elfcpp::R_MICROMIPS_CALL16:
12411     case elfcpp::R_MICROMIPS_GOT_HI16:
12412     case elfcpp::R_MICROMIPS_GOT_LO16:
12413     case elfcpp::R_MICROMIPS_CALL_HI16:
12414     case elfcpp::R_MICROMIPS_CALL_LO16:
12415     case elfcpp::R_MIPS_EH:
12416       // Absolute in GOT.
12417       return Symbol::RELATIVE_REF;
12418
12419     case elfcpp::R_MIPS_TLS_DTPMOD32:
12420     case elfcpp::R_MIPS_TLS_DTPREL32:
12421     case elfcpp::R_MIPS_TLS_DTPMOD64:
12422     case elfcpp::R_MIPS_TLS_DTPREL64:
12423     case elfcpp::R_MIPS_TLS_GD:
12424     case elfcpp::R_MIPS_TLS_LDM:
12425     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12426     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12427     case elfcpp::R_MIPS_TLS_GOTTPREL:
12428     case elfcpp::R_MIPS_TLS_TPREL32:
12429     case elfcpp::R_MIPS_TLS_TPREL64:
12430     case elfcpp::R_MIPS_TLS_TPREL_HI16:
12431     case elfcpp::R_MIPS_TLS_TPREL_LO16:
12432     case elfcpp::R_MIPS16_TLS_GD:
12433     case elfcpp::R_MIPS16_TLS_GOTTPREL:
12434     case elfcpp::R_MICROMIPS_TLS_GD:
12435     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12436     case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12437     case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12438       return Symbol::TLS_REF;
12439
12440     case elfcpp::R_MIPS_COPY:
12441     case elfcpp::R_MIPS_JUMP_SLOT:
12442     default:
12443       // Not expected.  We will give an error later.
12444       return 0;
12445     }
12446 }
12447
12448 // Report an unsupported relocation against a local symbol.
12449
12450 template<int size, bool big_endian>
12451 void
12452 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12453                         Sized_relobj_file<size, big_endian>* object,
12454                         unsigned int r_type)
12455 {
12456   gold_error(_("%s: unsupported reloc %u against local symbol"),
12457              object->name().c_str(), r_type);
12458 }
12459
12460 // Report an unsupported relocation against a global symbol.
12461
12462 template<int size, bool big_endian>
12463 void
12464 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12465                         Sized_relobj_file<size, big_endian>* object,
12466                         unsigned int r_type,
12467                         Symbol* gsym)
12468 {
12469   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12470              object->name().c_str(), r_type, gsym->demangled_name().c_str());
12471 }
12472
12473 // Return printable name for ABI.
12474 template<int size, bool big_endian>
12475 const char*
12476 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12477 {
12478   switch (e_flags & elfcpp::EF_MIPS_ABI)
12479     {
12480     case 0:
12481       if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12482         return "N32";
12483       else if (size == 64)
12484         return "64";
12485       else
12486         return "none";
12487     case elfcpp::E_MIPS_ABI_O32:
12488       return "O32";
12489     case elfcpp::E_MIPS_ABI_O64:
12490       return "O64";
12491     case elfcpp::E_MIPS_ABI_EABI32:
12492       return "EABI32";
12493     case elfcpp::E_MIPS_ABI_EABI64:
12494       return "EABI64";
12495     default:
12496       return "unknown abi";
12497     }
12498 }
12499
12500 template<int size, bool big_endian>
12501 const char*
12502 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12503 {
12504   switch (e_flags & elfcpp::EF_MIPS_MACH)
12505     {
12506     case elfcpp::E_MIPS_MACH_3900:
12507       return "mips:3900";
12508     case elfcpp::E_MIPS_MACH_4010:
12509       return "mips:4010";
12510     case elfcpp::E_MIPS_MACH_4100:
12511       return "mips:4100";
12512     case elfcpp::E_MIPS_MACH_4111:
12513       return "mips:4111";
12514     case elfcpp::E_MIPS_MACH_4120:
12515       return "mips:4120";
12516     case elfcpp::E_MIPS_MACH_4650:
12517       return "mips:4650";
12518     case elfcpp::E_MIPS_MACH_5400:
12519       return "mips:5400";
12520     case elfcpp::E_MIPS_MACH_5500:
12521       return "mips:5500";
12522     case elfcpp::E_MIPS_MACH_5900:
12523       return "mips:5900";
12524     case elfcpp::E_MIPS_MACH_SB1:
12525       return "mips:sb1";
12526     case elfcpp::E_MIPS_MACH_9000:
12527       return "mips:9000";
12528     case elfcpp::E_MIPS_MACH_LS2E:
12529       return "mips:loongson_2e";
12530     case elfcpp::E_MIPS_MACH_LS2F:
12531       return "mips:loongson_2f";
12532     case elfcpp::E_MIPS_MACH_GS464:
12533       return "mips:gs464";
12534     case elfcpp::E_MIPS_MACH_GS464E:
12535       return "mips:gs464e";
12536     case elfcpp::E_MIPS_MACH_OCTEON:
12537       return "mips:octeon";
12538     case elfcpp::E_MIPS_MACH_OCTEON2:
12539       return "mips:octeon2";
12540     case elfcpp::E_MIPS_MACH_OCTEON3:
12541       return "mips:octeon3";
12542     case elfcpp::E_MIPS_MACH_XLR:
12543       return "mips:xlr";
12544     default:
12545       switch (e_flags & elfcpp::EF_MIPS_ARCH)
12546         {
12547         default:
12548         case elfcpp::E_MIPS_ARCH_1:
12549           return "mips:3000";
12550
12551         case elfcpp::E_MIPS_ARCH_2:
12552           return "mips:6000";
12553
12554         case elfcpp::E_MIPS_ARCH_3:
12555           return "mips:4000";
12556
12557         case elfcpp::E_MIPS_ARCH_4:
12558           return "mips:8000";
12559
12560         case elfcpp::E_MIPS_ARCH_5:
12561           return "mips:mips5";
12562
12563         case elfcpp::E_MIPS_ARCH_32:
12564           return "mips:isa32";
12565
12566         case elfcpp::E_MIPS_ARCH_64:
12567           return "mips:isa64";
12568
12569         case elfcpp::E_MIPS_ARCH_32R2:
12570           return "mips:isa32r2";
12571
12572         case elfcpp::E_MIPS_ARCH_32R6:
12573           return "mips:isa32r6";
12574
12575         case elfcpp::E_MIPS_ARCH_64R2:
12576           return "mips:isa64r2";
12577
12578         case elfcpp::E_MIPS_ARCH_64R6:
12579           return "mips:isa64r6";
12580         }
12581     }
12582     return "unknown CPU";
12583 }
12584
12585 template<int size, bool big_endian>
12586 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12587 {
12588   size,                 // size
12589   big_endian,           // is_big_endian
12590   elfcpp::EM_MIPS,      // machine_code
12591   true,                 // has_make_symbol
12592   false,                // has_resolve
12593   false,                // has_code_fill
12594   true,                 // is_default_stack_executable
12595   false,                // can_icf_inline_merge_sections
12596   '\0',                 // wrap_char
12597   size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1",      // dynamic_linker
12598   0x400000,             // default_text_segment_address
12599   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
12600   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
12601   false,                // isolate_execinstr
12602   0,                    // rosegment_gap
12603   elfcpp::SHN_UNDEF,    // small_common_shndx
12604   elfcpp::SHN_UNDEF,    // large_common_shndx
12605   0,                    // small_common_section_flags
12606   0,                    // large_common_section_flags
12607   NULL,                 // attributes_section
12608   NULL,                 // attributes_vendor
12609   "__start",            // entry_symbol_name
12610   32,                   // hash_entry_size
12611   elfcpp::SHT_PROGBITS, // unwind_section_type
12612 };
12613
12614 template<int size, bool big_endian>
12615 class Target_mips_nacl : public Target_mips<size, big_endian>
12616 {
12617  public:
12618   Target_mips_nacl()
12619     : Target_mips<size, big_endian>(&mips_nacl_info)
12620   { }
12621
12622  private:
12623   static const Target::Target_info mips_nacl_info;
12624 };
12625
12626 template<int size, bool big_endian>
12627 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12628 {
12629   size,                 // size
12630   big_endian,           // is_big_endian
12631   elfcpp::EM_MIPS,      // machine_code
12632   true,                 // has_make_symbol
12633   false,                // has_resolve
12634   false,                // has_code_fill
12635   true,                 // is_default_stack_executable
12636   false,                // can_icf_inline_merge_sections
12637   '\0',                 // wrap_char
12638   "/lib/ld.so.1",       // dynamic_linker
12639   0x20000,              // default_text_segment_address
12640   0x10000,              // abi_pagesize (overridable by -z max-page-size)
12641   0x10000,              // common_pagesize (overridable by -z common-page-size)
12642   true,                 // isolate_execinstr
12643   0x10000000,           // rosegment_gap
12644   elfcpp::SHN_UNDEF,    // small_common_shndx
12645   elfcpp::SHN_UNDEF,    // large_common_shndx
12646   0,                    // small_common_section_flags
12647   0,                    // large_common_section_flags
12648   NULL,                 // attributes_section
12649   NULL,                 // attributes_vendor
12650   "_start",             // entry_symbol_name
12651   32,                   // hash_entry_size
12652   elfcpp::SHT_PROGBITS, // unwind_section_type
12653 };
12654
12655 // Target selector for Mips.  Note this is never instantiated directly.
12656 // It's only used in Target_selector_mips_nacl, below.
12657
12658 template<int size, bool big_endian>
12659 class Target_selector_mips : public Target_selector
12660 {
12661 public:
12662   Target_selector_mips()
12663     : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12664                 (size == 64 ?
12665                   (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12666                   (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12667                 (size == 64 ?
12668                   (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12669                   (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12670   { }
12671
12672   Target* do_instantiate_target()
12673   { return new Target_mips<size, big_endian>(); }
12674 };
12675
12676 template<int size, bool big_endian>
12677 class Target_selector_mips_nacl
12678   : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12679                                 Target_mips_nacl<size, big_endian> >
12680 {
12681  public:
12682   Target_selector_mips_nacl()
12683     : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12684                            Target_mips_nacl<size, big_endian> >(
12685         // NaCl currently supports only MIPS32 little-endian.
12686         "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12687   { }
12688 };
12689
12690 Target_selector_mips_nacl<32, true> target_selector_mips32;
12691 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12692 Target_selector_mips_nacl<64, true> target_selector_mips64;
12693 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12694
12695 } // End anonymous namespace.