MIPS: Fix GOT page counter in multi-got links
[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), num_pages(0)
635   { }
636
637   Got_page_entry(Object* object_, unsigned int symndx_)
638     : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
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   // The maximum number of page entries needed for RANGES.
648   unsigned int num_pages;
649 };
650
651 // Hash for Got_page_entry.
652
653 struct Got_page_entry_hash
654 {
655   size_t
656   operator()(Got_page_entry* entry) const
657   { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
658 };
659
660 // Equality for Got_page_entry.
661
662 struct Got_page_entry_eq
663 {
664   bool
665   operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
666   {
667     return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
668   }
669 };
670
671 // This class is used to hold .got information when linking.
672
673 template<int size, bool big_endian>
674 class Mips_got_info
675 {
676   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
677   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
678     Reloc_section;
679   typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
680
681   // Unordered set of GOT entries.
682   typedef Unordered_set<Mips_got_entry<size, big_endian>*,
683       Mips_got_entry_hash<size, big_endian>,
684       Mips_got_entry_eq<size, big_endian> > Got_entry_set;
685
686   // Unordered set of GOT page entries.
687   typedef Unordered_set<Got_page_entry*,
688       Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
689
690   // Unordered set of global GOT entries.
691   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
692       Global_got_entry_set;
693
694  public:
695   Mips_got_info()
696     : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
697       tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
698       got_entries_(), got_page_entries_(), got_page_offset_start_(0),
699       got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
700       offset_(0)
701   { }
702
703   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
704   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
705   void
706   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
707                           unsigned int symndx, Mips_address addend,
708                           unsigned int r_type, unsigned int shndx,
709                           bool is_section_symbol);
710
711   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
712   // in OBJECT.  FOR_CALL is true if the caller is only interested in
713   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
714   // relocation.
715   void
716   record_global_got_symbol(Mips_symbol<size>* mips_sym,
717                            Mips_relobj<size, big_endian>* object,
718                            unsigned int r_type, bool dyn_reloc, bool for_call);
719
720   // Add ENTRY to master GOT and to OBJECT's GOT.
721   void
722   record_got_entry(Mips_got_entry<size, big_endian>* entry,
723                    Mips_relobj<size, big_endian>* object);
724
725   // Record that OBJECT has a page relocation against symbol SYMNDX and
726   // that ADDEND is the addend for that relocation.
727   void
728   record_got_page_entry(Mips_relobj<size, big_endian>* object,
729                         unsigned int symndx, int addend);
730
731   // Create all entries that should be in the local part of the GOT.
732   void
733   add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
734
735   // Create GOT page entries.
736   void
737   add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
738
739   // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
740   void
741   add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
742                      unsigned int non_reloc_only_global_gotno);
743
744   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
745   void
746   add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
747
748   // Create TLS GOT entries.
749   void
750   add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
751
752   // Decide whether the symbol needs an entry in the global part of the primary
753   // GOT, setting global_got_area accordingly.  Count the number of global
754   // symbols that are in the primary GOT only because they have dynamic
755   // relocations R_MIPS_REL32 against them (reloc_only_gotno).
756   void
757   count_got_symbols(Symbol_table* symtab);
758
759   // Return the offset of GOT page entry for VALUE.
760   unsigned int
761   get_got_page_offset(Mips_address value,
762                       Mips_output_data_got<size, big_endian>* got);
763
764   // Count the number of GOT entries required.
765   void
766   count_got_entries();
767
768   // Count the number of GOT entries required by ENTRY.  Accumulate the result.
769   void
770   count_got_entry(Mips_got_entry<size, big_endian>* entry);
771
772   // Add FROM's GOT entries.
773   void
774   add_got_entries(Mips_got_info<size, big_endian>* from);
775
776   // Add FROM's GOT page entries.
777   void
778   add_got_page_entries(Mips_got_info<size, big_endian>* from);
779
780   // Return GOT size.
781   unsigned int
782   got_size() const
783   { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
784              + this->tls_gotno_) * size/8);
785   }
786
787   // Return the number of local GOT entries.
788   unsigned int
789   local_gotno() const
790   { return this->local_gotno_; }
791
792   // Return the maximum number of page GOT entries needed.
793   unsigned int
794   page_gotno() const
795   { return this->page_gotno_; }
796
797   // Return the number of global GOT entries.
798   unsigned int
799   global_gotno() const
800   { return this->global_gotno_; }
801
802   // Set the number of global GOT entries.
803   void
804   set_global_gotno(unsigned int global_gotno)
805   { this->global_gotno_ = global_gotno; }
806
807   // Return the number of GGA_RELOC_ONLY global GOT entries.
808   unsigned int
809   reloc_only_gotno() const
810   { return this->reloc_only_gotno_; }
811
812   // Return the number of TLS GOT entries.
813   unsigned int
814   tls_gotno() const
815   { return this->tls_gotno_; }
816
817   // Return the GOT type for this GOT.  Used for multi-GOT links only.
818   unsigned int
819   multigot_got_type(unsigned int got_type) const
820   {
821     switch (got_type)
822       {
823       case GOT_TYPE_STANDARD:
824         return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
825       case GOT_TYPE_TLS_OFFSET:
826         return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
827       case GOT_TYPE_TLS_PAIR:
828         return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
829       default:
830         gold_unreachable();
831       }
832   }
833
834   // Remove lazy-binding stubs for global symbols in this GOT.
835   void
836   remove_lazy_stubs(Target_mips<size, big_endian>* target);
837
838   // Return offset of this GOT from the start of .got section.
839   unsigned int
840   offset() const
841   { return this->offset_; }
842
843   // Set offset of this GOT from the start of .got section.
844   void
845   set_offset(unsigned int offset)
846   { this->offset_ = offset; }
847
848   // Set index of this GOT in multi-GOT links.
849   void
850   set_index(unsigned int index)
851   { this->index_ = index; }
852
853   // Return next GOT in multi-GOT links.
854   Mips_got_info<size, big_endian>*
855   next() const
856   { return this->next_; }
857
858   // Set next GOT in multi-GOT links.
859   void
860   set_next(Mips_got_info<size, big_endian>* next)
861   { this->next_ = next; }
862
863   // Return the offset of TLS LDM entry for this GOT.
864   unsigned int
865   tls_ldm_offset() const
866   { return this->tls_ldm_offset_; }
867
868   // Set the offset of TLS LDM entry for this GOT.
869   void
870   set_tls_ldm_offset(unsigned int tls_ldm_offset)
871   { this->tls_ldm_offset_ = tls_ldm_offset; }
872
873   Global_got_entry_set&
874   global_got_symbols()
875   { return this->global_got_symbols_; }
876
877   // Return the GOT_TLS_* type required by relocation type R_TYPE.
878   static int
879   mips_elf_reloc_tls_type(unsigned int r_type)
880   {
881     if (tls_gd_reloc(r_type))
882       return GOT_TLS_GD;
883
884     if (tls_ldm_reloc(r_type))
885       return GOT_TLS_LDM;
886
887     if (tls_gottprel_reloc(r_type))
888       return GOT_TLS_IE;
889
890     return GOT_TLS_NONE;
891   }
892
893   // Return the number of GOT slots needed for GOT TLS type TYPE.
894   static int
895   mips_tls_got_entries(unsigned int type)
896   {
897     switch (type)
898       {
899       case GOT_TLS_GD:
900       case GOT_TLS_LDM:
901         return 2;
902
903       case GOT_TLS_IE:
904         return 1;
905
906       case GOT_TLS_NONE:
907         return 0;
908
909       default:
910         gold_unreachable();
911       }
912   }
913
914  private:
915   // The number of local GOT entries.
916   unsigned int local_gotno_;
917   // The maximum number of page GOT entries needed.
918   unsigned int page_gotno_;
919   // The number of global GOT entries.
920   unsigned int global_gotno_;
921   // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
922   unsigned int reloc_only_gotno_;
923   // The number of TLS GOT entries.
924   unsigned int tls_gotno_;
925   // The offset of TLS LDM entry for this GOT.
926   unsigned int tls_ldm_offset_;
927   // All symbols that have global GOT entry.
928   Global_got_entry_set global_got_symbols_;
929   // A hash table holding GOT entries.
930   Got_entry_set got_entries_;
931   // A hash table of GOT page entries.
932   Got_page_entry_set got_page_entries_;
933   // The offset of first GOT page entry for this GOT.
934   unsigned int got_page_offset_start_;
935   // The offset of next available GOT page entry for this GOT.
936   unsigned int got_page_offset_next_;
937   // A hash table that maps GOT page entry value to the GOT offset where
938   // the entry is located.
939   Got_page_offsets got_page_offsets_;
940   // In multi-GOT links, a pointer to the next GOT.
941   Mips_got_info<size, big_endian>* next_;
942   // Index of this GOT in multi-GOT links.
943   unsigned int index_;
944   // The offset of this GOT in multi-GOT links.
945   unsigned int offset_;
946 };
947
948 // This is a helper class used during relocation scan.  It records GOT16 addend.
949
950 template<int size, bool big_endian>
951 struct got16_addend
952 {
953   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
954
955   got16_addend(const Sized_relobj_file<size, big_endian>* _object,
956                unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
957                Mips_address _addend)
958     : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
959       addend(_addend)
960   { }
961
962   const Sized_relobj_file<size, big_endian>* object;
963   unsigned int shndx;
964   unsigned int r_type;
965   unsigned int r_sym;
966   Mips_address addend;
967 };
968
969 // .MIPS.abiflags section content
970
971 template<bool big_endian>
972 struct Mips_abiflags
973 {
974   typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
975   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
976   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
977
978   Mips_abiflags()
979     : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
980       cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
981   { }
982
983   // Version of flags structure.
984   Valtype16 version;
985   // The level of the ISA: 1-5, 32, 64.
986   Valtype8 isa_level;
987   // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
988   Valtype8 isa_rev;
989   // The size of general purpose registers.
990   Valtype8 gpr_size;
991   // The size of co-processor 1 registers.
992   Valtype8 cpr1_size;
993   // The size of co-processor 2 registers.
994   Valtype8 cpr2_size;
995   // The floating-point ABI.
996   Valtype8 fp_abi;
997   // Processor-specific extension.
998   Valtype32 isa_ext;
999   // Mask of ASEs used.
1000   Valtype32 ases;
1001   // Mask of general flags.
1002   Valtype32 flags1;
1003   Valtype32 flags2;
1004 };
1005
1006 // Mips_symbol class.  Holds additional symbol information needed for Mips.
1007
1008 template<int size>
1009 class Mips_symbol : public Sized_symbol<size>
1010 {
1011  public:
1012   Mips_symbol()
1013     : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1014       has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1015       pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1016       global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1017       needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1018       comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1019       mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1020   { }
1021
1022   // Return whether this is a MIPS16 symbol.
1023   bool
1024   is_mips16() const
1025   {
1026     // (st_other & STO_MIPS16) == STO_MIPS16
1027     return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1028             == elfcpp::STO_MIPS16 >> 2);
1029   }
1030
1031   // Return whether this is a microMIPS symbol.
1032   bool
1033   is_micromips() const
1034   {
1035     // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1036     return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1037             == elfcpp::STO_MICROMIPS >> 2);
1038   }
1039
1040   // Return whether the symbol needs MIPS16 fn_stub.
1041   bool
1042   need_fn_stub() const
1043   { return this->need_fn_stub_; }
1044
1045   // Set that the symbol needs MIPS16 fn_stub.
1046   void
1047   set_need_fn_stub()
1048   { this->need_fn_stub_ = true; }
1049
1050   // Return whether this symbol is referenced by branch relocations from
1051   // any non-PIC input file.
1052   bool
1053   has_nonpic_branches() const
1054   { return this->has_nonpic_branches_; }
1055
1056   // Set that this symbol is referenced by branch relocations from
1057   // any non-PIC input file.
1058   void
1059   set_has_nonpic_branches()
1060   { this->has_nonpic_branches_ = true; }
1061
1062   // Return the offset of the la25 stub for this symbol from the start of the
1063   // la25 stub section.
1064   unsigned int
1065   la25_stub_offset() const
1066   { return this->la25_stub_offset_; }
1067
1068   // Set the offset of the la25 stub for this symbol from the start of the
1069   // la25 stub section.
1070   void
1071   set_la25_stub_offset(unsigned int offset)
1072   { this->la25_stub_offset_ = offset; }
1073
1074   // Return whether the symbol has la25 stub.  This is true if this symbol is
1075   // for a PIC function, and there are non-PIC branches and jumps to it.
1076   bool
1077   has_la25_stub() const
1078   { return this->la25_stub_offset_ != -1U; }
1079
1080   // Return whether there is a relocation against this symbol that must be
1081   // resolved by the static linker (that is, the relocation cannot possibly
1082   // be made dynamic).
1083   bool
1084   has_static_relocs() const
1085   { return this->has_static_relocs_; }
1086
1087   // Set that there is a relocation against this symbol that must be resolved
1088   // by the static linker (that is, the relocation cannot possibly be made
1089   // dynamic).
1090   void
1091   set_has_static_relocs()
1092   { this->has_static_relocs_ = true; }
1093
1094   // Return whether we must not create a lazy-binding stub for this symbol.
1095   bool
1096   no_lazy_stub() const
1097   { return this->no_lazy_stub_; }
1098
1099   // Set that we must not create a lazy-binding stub for this symbol.
1100   void
1101   set_no_lazy_stub()
1102   { this->no_lazy_stub_ = true; }
1103
1104   // Return the offset of the lazy-binding stub for this symbol from the start
1105   // of .MIPS.stubs section.
1106   unsigned int
1107   lazy_stub_offset() const
1108   { return this->lazy_stub_offset_; }
1109
1110   // Set the offset of the lazy-binding stub for this symbol from the start
1111   // of .MIPS.stubs section.
1112   void
1113   set_lazy_stub_offset(unsigned int offset)
1114   { this->lazy_stub_offset_ = offset; }
1115
1116   // Return whether there are any relocations for this symbol where
1117   // pointer equality matters.
1118   bool
1119   pointer_equality_needed() const
1120   { return this->pointer_equality_needed_; }
1121
1122   // Set that there are relocations for this symbol where pointer equality
1123   // matters.
1124   void
1125   set_pointer_equality_needed()
1126   { this->pointer_equality_needed_ = true; }
1127
1128   // Return global GOT area where this symbol in located.
1129   Global_got_area
1130   global_got_area() const
1131   { return this->global_got_area_; }
1132
1133   // Set global GOT area where this symbol in located.
1134   void
1135   set_global_got_area(Global_got_area global_got_area)
1136   { this->global_got_area_ = global_got_area; }
1137
1138   // Return the global GOT offset for this symbol.  For multi-GOT links, this
1139   // returns the offset from the start of .got section to the first GOT entry
1140   // for the symbol.  Note that in multi-GOT links the symbol can have entry
1141   // in more than one GOT.
1142   unsigned int
1143   global_gotoffset() const
1144   { return this->global_gotoffset_; }
1145
1146   // Set the global GOT offset for this symbol.  Note that in multi-GOT links
1147   // the symbol can have entry in more than one GOT.  This method will set
1148   // the offset only if it is less than current offset.
1149   void
1150   set_global_gotoffset(unsigned int offset)
1151   {
1152     if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1153       this->global_gotoffset_ = offset;
1154   }
1155
1156   // Return whether all GOT relocations for this symbol are for calls.
1157   bool
1158   got_only_for_calls() const
1159   { return this->got_only_for_calls_; }
1160
1161   // Set that there is a GOT relocation for this symbol that is not for call.
1162   void
1163   set_got_not_only_for_calls()
1164   { this->got_only_for_calls_ = false; }
1165
1166   // Return whether this is a PIC symbol.
1167   bool
1168   is_pic() const
1169   {
1170     // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1171     return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1172             == (elfcpp::STO_MIPS_PIC >> 2));
1173   }
1174
1175   // Set the flag in st_other field that marks this symbol as PIC.
1176   void
1177   set_pic()
1178   {
1179     if (this->is_mips16())
1180       // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1181       this->set_nonvis((this->nonvis()
1182                         & ~((elfcpp::STO_MIPS16 >> 2)
1183                             | (elfcpp::STO_MIPS_FLAGS >> 2)))
1184                        | (elfcpp::STO_MIPS_PIC >> 2));
1185     else
1186       // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1187       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1188                        | (elfcpp::STO_MIPS_PIC >> 2));
1189   }
1190
1191   // Set the flag in st_other field that marks this symbol as PLT.
1192   void
1193   set_mips_plt()
1194   {
1195     if (this->is_mips16())
1196       // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1197       this->set_nonvis((this->nonvis()
1198                         & ((elfcpp::STO_MIPS16 >> 2)
1199                            | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1200                        | (elfcpp::STO_MIPS_PLT >> 2));
1201
1202     else
1203       // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1204       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1205                        | (elfcpp::STO_MIPS_PLT >> 2));
1206   }
1207
1208   // Downcast a base pointer to a Mips_symbol pointer.
1209   static Mips_symbol<size>*
1210   as_mips_sym(Symbol* sym)
1211   { return static_cast<Mips_symbol<size>*>(sym); }
1212
1213   // Downcast a base pointer to a Mips_symbol pointer.
1214   static const Mips_symbol<size>*
1215   as_mips_sym(const Symbol* sym)
1216   { return static_cast<const Mips_symbol<size>*>(sym); }
1217
1218   // Return whether the symbol has lazy-binding stub.
1219   bool
1220   has_lazy_stub() const
1221   { return this->has_lazy_stub_; }
1222
1223   // Set whether the symbol has lazy-binding stub.
1224   void
1225   set_has_lazy_stub(bool has_lazy_stub)
1226   { this->has_lazy_stub_ = has_lazy_stub; }
1227
1228   // Return whether the symbol needs a standard PLT entry.
1229   bool
1230   needs_mips_plt() const
1231   { return this->needs_mips_plt_; }
1232
1233   // Set whether the symbol needs a standard PLT entry.
1234   void
1235   set_needs_mips_plt(bool needs_mips_plt)
1236   { this->needs_mips_plt_ = needs_mips_plt; }
1237
1238   // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1239   // entry.
1240   bool
1241   needs_comp_plt() const
1242   { return this->needs_comp_plt_; }
1243
1244   // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1245   void
1246   set_needs_comp_plt(bool needs_comp_plt)
1247   { this->needs_comp_plt_ = needs_comp_plt; }
1248
1249   // Return standard PLT entry offset, or -1 if none.
1250   unsigned int
1251   mips_plt_offset() const
1252   { return this->mips_plt_offset_; }
1253
1254   // Set standard PLT entry offset.
1255   void
1256   set_mips_plt_offset(unsigned int mips_plt_offset)
1257   { this->mips_plt_offset_ = mips_plt_offset; }
1258
1259   // Return whether the symbol has standard PLT entry.
1260   bool
1261   has_mips_plt_offset() const
1262   { return this->mips_plt_offset_ != -1U; }
1263
1264   // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1265   unsigned int
1266   comp_plt_offset() const
1267   { return this->comp_plt_offset_; }
1268
1269   // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1270   void
1271   set_comp_plt_offset(unsigned int comp_plt_offset)
1272   { this->comp_plt_offset_ = comp_plt_offset; }
1273
1274   // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1275   bool
1276   has_comp_plt_offset() const
1277   { return this->comp_plt_offset_ != -1U; }
1278
1279   // Return MIPS16 fn stub for a symbol.
1280   template<bool big_endian>
1281   Mips16_stub_section<size, big_endian>*
1282   get_mips16_fn_stub() const
1283   {
1284     return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1285   }
1286
1287   // Set MIPS16 fn stub for a symbol.
1288   void
1289   set_mips16_fn_stub(Mips16_stub_section_base* stub)
1290   { this->mips16_fn_stub_ = stub; }
1291
1292   // Return whether symbol has MIPS16 fn stub.
1293   bool
1294   has_mips16_fn_stub() const
1295   { return this->mips16_fn_stub_ != NULL; }
1296
1297   // Return MIPS16 call stub for a symbol.
1298   template<bool big_endian>
1299   Mips16_stub_section<size, big_endian>*
1300   get_mips16_call_stub() const
1301   {
1302     return static_cast<Mips16_stub_section<size, big_endian>*>(
1303       mips16_call_stub_);
1304   }
1305
1306   // Set MIPS16 call stub for a symbol.
1307   void
1308   set_mips16_call_stub(Mips16_stub_section_base* stub)
1309   { this->mips16_call_stub_ = stub; }
1310
1311   // Return whether symbol has MIPS16 call stub.
1312   bool
1313   has_mips16_call_stub() const
1314   { return this->mips16_call_stub_ != NULL; }
1315
1316   // Return MIPS16 call_fp stub for a symbol.
1317   template<bool big_endian>
1318   Mips16_stub_section<size, big_endian>*
1319   get_mips16_call_fp_stub() const
1320   {
1321     return static_cast<Mips16_stub_section<size, big_endian>*>(
1322       mips16_call_fp_stub_);
1323   }
1324
1325   // Set MIPS16 call_fp stub for a symbol.
1326   void
1327   set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1328   { this->mips16_call_fp_stub_ = stub; }
1329
1330   // Return whether symbol has MIPS16 call_fp stub.
1331   bool
1332   has_mips16_call_fp_stub() const
1333   { return this->mips16_call_fp_stub_ != NULL; }
1334
1335   bool
1336   get_applied_secondary_got_fixup() const
1337   { return applied_secondary_got_fixup_; }
1338
1339   void
1340   set_applied_secondary_got_fixup()
1341   { this->applied_secondary_got_fixup_ = true; }
1342
1343   // Return the hash of this symbol.
1344   size_t
1345   hash() const
1346   {
1347     return gold::string_hash<char>(this->name());
1348   }
1349
1350  private:
1351   // Whether the symbol needs MIPS16 fn_stub.  This is true if this symbol
1352   // appears in any relocs other than a 16 bit call.
1353   bool need_fn_stub_;
1354
1355   // True if this symbol is referenced by branch relocations from
1356   // any non-PIC input file.  This is used to determine whether an
1357   // la25 stub is required.
1358   bool has_nonpic_branches_;
1359
1360   // The offset of the la25 stub for this symbol from the start of the
1361   // la25 stub section.
1362   unsigned int la25_stub_offset_;
1363
1364   // True if there is a relocation against this symbol that must be
1365   // resolved by the static linker (that is, the relocation cannot
1366   // possibly be made dynamic).
1367   bool has_static_relocs_;
1368
1369   // Whether we must not create a lazy-binding stub for this symbol.
1370   // This is true if the symbol has relocations related to taking the
1371   // function's address.
1372   bool no_lazy_stub_;
1373
1374   // The offset of the lazy-binding stub for this symbol from the start of
1375   // .MIPS.stubs section.
1376   unsigned int lazy_stub_offset_;
1377
1378   // True if there are any relocations for this symbol where pointer equality
1379   // matters.
1380   bool pointer_equality_needed_;
1381
1382   // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1383   // in the global part of the GOT.
1384   Global_got_area global_got_area_;
1385
1386   // The global GOT offset for this symbol.  For multi-GOT links, this is offset
1387   // from the start of .got section to the first GOT entry for the symbol.
1388   // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1389   unsigned int global_gotoffset_;
1390
1391   // Whether all GOT relocations for this symbol are for calls.
1392   bool got_only_for_calls_;
1393   // Whether the symbol has lazy-binding stub.
1394   bool has_lazy_stub_;
1395   // Whether the symbol needs a standard PLT entry.
1396   bool needs_mips_plt_;
1397   // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1398   bool needs_comp_plt_;
1399   // Standard PLT entry offset, or -1 if none.
1400   unsigned int mips_plt_offset_;
1401   // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1402   unsigned int comp_plt_offset_;
1403   // MIPS16 fn stub for a symbol.
1404   Mips16_stub_section_base* mips16_fn_stub_;
1405   // MIPS16 call stub for a symbol.
1406   Mips16_stub_section_base* mips16_call_stub_;
1407   // MIPS16 call_fp stub for a symbol.
1408   Mips16_stub_section_base* mips16_call_fp_stub_;
1409
1410   bool applied_secondary_got_fixup_;
1411 };
1412
1413 // Mips16_stub_section class.
1414
1415 // The mips16 compiler uses a couple of special sections to handle
1416 // floating point arguments.
1417
1418 // Section names that look like .mips16.fn.FNNAME contain stubs that
1419 // copy floating point arguments from the fp regs to the gp regs and
1420 // then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1421 // call should be redirected to the stub instead.  If no 32 bit
1422 // function calls FNNAME, the stub should be discarded.  We need to
1423 // consider any reference to the function, not just a call, because
1424 // if the address of the function is taken we will need the stub,
1425 // since the address might be passed to a 32 bit function.
1426
1427 // Section names that look like .mips16.call.FNNAME contain stubs
1428 // that copy floating point arguments from the gp regs to the fp
1429 // regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1430 // then any 16 bit function that calls FNNAME should be redirected
1431 // to the stub instead.  If FNNAME is not a 32 bit function, the
1432 // stub should be discarded.
1433
1434 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1435 // which call FNNAME and then copy the return value from the fp regs
1436 // to the gp regs.  These stubs store the return address in $18 while
1437 // calling FNNAME; any function which might call one of these stubs
1438 // must arrange to save $18 around the call.  (This case is not
1439 // needed for 32 bit functions that call 16 bit functions, because
1440 // 16 bit functions always return floating point values in both
1441 // $f0/$f1 and $2/$3.)
1442
1443 // Note that in all cases FNNAME might be defined statically.
1444 // Therefore, FNNAME is not used literally.  Instead, the relocation
1445 // information will indicate which symbol the section is for.
1446
1447 // We record any stubs that we find in the symbol table.
1448
1449 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1450
1451 class Mips16_stub_section_base { };
1452
1453 template<int size, bool big_endian>
1454 class Mips16_stub_section : public Mips16_stub_section_base
1455 {
1456   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1457
1458  public:
1459   Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1460     : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1461       found_r_mips_none_(false)
1462   {
1463     gold_assert(object->is_mips16_fn_stub_section(shndx)
1464                 || object->is_mips16_call_stub_section(shndx)
1465                 || object->is_mips16_call_fp_stub_section(shndx));
1466   }
1467
1468   // Return the object of this stub section.
1469   Mips_relobj<size, big_endian>*
1470   object() const
1471   { return this->object_; }
1472
1473   // Return the size of a section.
1474   uint64_t
1475   section_size() const
1476   { return this->object_->section_size(this->shndx_); }
1477
1478   // Return section index of this stub section.
1479   unsigned int
1480   shndx() const
1481   { return this->shndx_; }
1482
1483   // Return symbol index, if stub is for a local function.
1484   unsigned int
1485   r_sym() const
1486   { return this->r_sym_; }
1487
1488   // Return symbol, if stub is for a global function.
1489   Mips_symbol<size>*
1490   gsym() const
1491   { return this->gsym_; }
1492
1493   // Return whether stub is for a local function.
1494   bool
1495   is_for_local_function() const
1496   { return this->gsym_ == NULL; }
1497
1498   // This method is called when a new relocation R_TYPE for local symbol R_SYM
1499   // is found in the stub section.  Try to find stub target.
1500   void
1501   new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1502   {
1503     // To find target symbol for this stub, trust the first R_MIPS_NONE
1504     // relocation, if any.  Otherwise trust the first relocation, whatever
1505     // its kind.
1506     if (this->found_r_mips_none_)
1507       return;
1508     if (r_type == elfcpp::R_MIPS_NONE)
1509       {
1510         this->r_sym_ = r_sym;
1511         this->gsym_ = NULL;
1512         this->found_r_mips_none_ = true;
1513       }
1514     else if (!is_target_found())
1515       this->r_sym_ = r_sym;
1516   }
1517
1518   // This method is called when a new relocation R_TYPE for global symbol GSYM
1519   // is found in the stub section.  Try to find stub target.
1520   void
1521   new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1522   {
1523     // To find target symbol for this stub, trust the first R_MIPS_NONE
1524     // relocation, if any.  Otherwise trust the first relocation, whatever
1525     // its kind.
1526     if (this->found_r_mips_none_)
1527       return;
1528     if (r_type == elfcpp::R_MIPS_NONE)
1529       {
1530         this->gsym_ = gsym;
1531         this->r_sym_ = 0;
1532         this->found_r_mips_none_ = true;
1533       }
1534     else if (!is_target_found())
1535       this->gsym_ = gsym;
1536   }
1537
1538   // Return whether we found the stub target.
1539   bool
1540   is_target_found() const
1541   { return this->r_sym_ != 0 || this->gsym_ != NULL;  }
1542
1543   // Return whether this is a fn stub.
1544   bool
1545   is_fn_stub() const
1546   { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1547
1548   // Return whether this is a call stub.
1549   bool
1550   is_call_stub() const
1551   { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1552
1553   // Return whether this is a call_fp stub.
1554   bool
1555   is_call_fp_stub() const
1556   { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1557
1558   // Return the output address.
1559   Mips_address
1560   output_address() const
1561   {
1562     return (this->object_->output_section(this->shndx_)->address()
1563             + this->object_->output_section_offset(this->shndx_));
1564   }
1565
1566  private:
1567   // The object of this stub section.
1568   Mips_relobj<size, big_endian>* object_;
1569   // The section index of this stub section.
1570   unsigned int shndx_;
1571   // The symbol index, if stub is for a local function.
1572   unsigned int r_sym_;
1573   // The symbol, if stub is for a global function.
1574   Mips_symbol<size>* gsym_;
1575   // True if we found R_MIPS_NONE relocation in this stub.
1576   bool found_r_mips_none_;
1577 };
1578
1579 // Mips_relobj class.
1580
1581 template<int size, bool big_endian>
1582 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1583 {
1584   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1585   typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1586     Mips16_stubs_int_map;
1587   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1588
1589  public:
1590   Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1591               const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1592     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1593       processor_specific_flags_(0), local_symbol_is_mips16_(),
1594       local_symbol_is_micromips_(), mips16_stub_sections_(),
1595       local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1596       local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1597       merge_processor_specific_data_(true), got_info_(NULL),
1598       section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1599       section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
1600       attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
1601       cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1602   {
1603     this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1604     this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1605   }
1606
1607   ~Mips_relobj()
1608   { delete this->attributes_section_data_; }
1609
1610   // Downcast a base pointer to a Mips_relobj pointer.  This is
1611   // not type-safe but we only use Mips_relobj not the base class.
1612   static Mips_relobj<size, big_endian>*
1613   as_mips_relobj(Relobj* relobj)
1614   { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1615
1616   // Downcast a base pointer to a Mips_relobj pointer.  This is
1617   // not type-safe but we only use Mips_relobj not the base class.
1618   static const Mips_relobj<size, big_endian>*
1619   as_mips_relobj(const Relobj* relobj)
1620   { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1621
1622   // Processor-specific flags in ELF file header.  This is valid only after
1623   // reading symbols.
1624   elfcpp::Elf_Word
1625   processor_specific_flags() const
1626   { return this->processor_specific_flags_; }
1627
1628   // Whether a local symbol is MIPS16 symbol.  R_SYM is the symbol table
1629   // index.  This is only valid after do_count_local_symbol is called.
1630   bool
1631   local_symbol_is_mips16(unsigned int r_sym) const
1632   {
1633     gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1634     return this->local_symbol_is_mips16_[r_sym];
1635   }
1636
1637   // Whether a local symbol is microMIPS symbol.  R_SYM is the symbol table
1638   // index.  This is only valid after do_count_local_symbol is called.
1639   bool
1640   local_symbol_is_micromips(unsigned int r_sym) const
1641   {
1642     gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1643     return this->local_symbol_is_micromips_[r_sym];
1644   }
1645
1646   // Get or create MIPS16 stub section.
1647   Mips16_stub_section<size, big_endian>*
1648   get_mips16_stub_section(unsigned int shndx)
1649   {
1650     typename Mips16_stubs_int_map::const_iterator it =
1651       this->mips16_stub_sections_.find(shndx);
1652     if (it != this->mips16_stub_sections_.end())
1653       return (*it).second;
1654
1655     Mips16_stub_section<size, big_endian>* stub_section =
1656       new Mips16_stub_section<size, big_endian>(this, shndx);
1657     this->mips16_stub_sections_.insert(
1658       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1659         stub_section->shndx(), stub_section));
1660     return stub_section;
1661   }
1662
1663   // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1664   // object doesn't have fn stub for R_SYM.
1665   Mips16_stub_section<size, big_endian>*
1666   get_local_mips16_fn_stub(unsigned int r_sym) const
1667   {
1668     typename Mips16_stubs_int_map::const_iterator it =
1669       this->local_mips16_fn_stubs_.find(r_sym);
1670     if (it != this->local_mips16_fn_stubs_.end())
1671       return (*it).second;
1672     return NULL;
1673   }
1674
1675   // Record that this object has MIPS16 fn stub for local symbol.  This method
1676   // is only called if we decided not to discard the stub.
1677   void
1678   add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1679   {
1680     gold_assert(stub->is_for_local_function());
1681     unsigned int r_sym = stub->r_sym();
1682     this->local_mips16_fn_stubs_.insert(
1683       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1684         r_sym, stub));
1685   }
1686
1687   // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1688   // object doesn't have call stub for R_SYM.
1689   Mips16_stub_section<size, big_endian>*
1690   get_local_mips16_call_stub(unsigned int r_sym) const
1691   {
1692     typename Mips16_stubs_int_map::const_iterator it =
1693       this->local_mips16_call_stubs_.find(r_sym);
1694     if (it != this->local_mips16_call_stubs_.end())
1695       return (*it).second;
1696     return NULL;
1697   }
1698
1699   // Record that this object has MIPS16 call stub for local symbol.  This method
1700   // is only called if we decided not to discard the stub.
1701   void
1702   add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1703   {
1704     gold_assert(stub->is_for_local_function());
1705     unsigned int r_sym = stub->r_sym();
1706     this->local_mips16_call_stubs_.insert(
1707       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1708         r_sym, stub));
1709   }
1710
1711   // Record that we found "non 16-bit" call relocation against local symbol
1712   // SYMNDX.  This reloc would need to refer to a MIPS16 fn stub, if there
1713   // is one.
1714   void
1715   add_local_non_16bit_call(unsigned int symndx)
1716   { this->local_non_16bit_calls_.insert(symndx); }
1717
1718   // Return true if there is any "non 16-bit" call relocation against local
1719   // symbol SYMNDX in this object.
1720   bool
1721   has_local_non_16bit_call_relocs(unsigned int symndx)
1722   {
1723     return (this->local_non_16bit_calls_.find(symndx)
1724             != this->local_non_16bit_calls_.end());
1725   }
1726
1727   // Record that we found 16-bit call relocation R_MIPS16_26 against local
1728   // symbol SYMNDX.  Local MIPS16 call or call_fp stubs will only be needed
1729   // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1730   void
1731   add_local_16bit_call(unsigned int symndx)
1732   { this->local_16bit_calls_.insert(symndx); }
1733
1734   // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1735   // symbol SYMNDX in this object.
1736   bool
1737   has_local_16bit_call_relocs(unsigned int symndx)
1738   {
1739     return (this->local_16bit_calls_.find(symndx)
1740             != this->local_16bit_calls_.end());
1741   }
1742
1743   // Get gp value that was used to create this object.
1744   Mips_address
1745   gp_value() const
1746   { return this->gp_; }
1747
1748   // Return whether the object is a PIC object.
1749   bool
1750   is_pic() const
1751   { return this->is_pic_; }
1752
1753   // Return whether the object uses N32 ABI.
1754   bool
1755   is_n32() const
1756   { return this->is_n32_; }
1757
1758   // Return whether the object uses N64 ABI.
1759   bool
1760   is_n64() const
1761   { return size == 64; }
1762
1763   // Return whether the object uses NewABI conventions.
1764   bool
1765   is_newabi() const
1766   { return this->is_n32() || this->is_n64(); }
1767
1768   // Return Mips_got_info for this object.
1769   Mips_got_info<size, big_endian>*
1770   get_got_info() const
1771   { return this->got_info_; }
1772
1773   // Return Mips_got_info for this object.  Create new info if it doesn't exist.
1774   Mips_got_info<size, big_endian>*
1775   get_or_create_got_info()
1776   {
1777     if (!this->got_info_)
1778       this->got_info_ = new Mips_got_info<size, big_endian>();
1779     return this->got_info_;
1780   }
1781
1782   // Set Mips_got_info for this object.
1783   void
1784   set_got_info(Mips_got_info<size, big_endian>* got_info)
1785   { this->got_info_ = got_info; }
1786
1787   // Whether a section SHDNX is a MIPS16 stub section.  This is only valid
1788   // after do_read_symbols is called.
1789   bool
1790   is_mips16_stub_section(unsigned int shndx)
1791   {
1792     return (is_mips16_fn_stub_section(shndx)
1793             || is_mips16_call_stub_section(shndx)
1794             || is_mips16_call_fp_stub_section(shndx));
1795   }
1796
1797   // Return TRUE if relocations in section SHNDX can refer directly to a
1798   // MIPS16 function rather than to a hard-float stub.  This is only valid
1799   // after do_read_symbols is called.
1800   bool
1801   section_allows_mips16_refs(unsigned int shndx)
1802   {
1803     return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1804   }
1805
1806   // Whether a section SHDNX is a MIPS16 fn stub section.  This is only valid
1807   // after do_read_symbols is called.
1808   bool
1809   is_mips16_fn_stub_section(unsigned int shndx)
1810   {
1811     gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1812     return this->section_is_mips16_fn_stub_[shndx];
1813   }
1814
1815   // Whether a section SHDNX is a MIPS16 call stub section.  This is only valid
1816   // after do_read_symbols is called.
1817   bool
1818   is_mips16_call_stub_section(unsigned int shndx)
1819   {
1820     gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1821     return this->section_is_mips16_call_stub_[shndx];
1822   }
1823
1824   // Whether a section SHDNX is a MIPS16 call_fp stub section.  This is only
1825   // valid after do_read_symbols is called.
1826   bool
1827   is_mips16_call_fp_stub_section(unsigned int shndx)
1828   {
1829     gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1830     return this->section_is_mips16_call_fp_stub_[shndx];
1831   }
1832
1833   // Discard MIPS16 stub secions that are not needed.
1834   void
1835   discard_mips16_stub_sections(Symbol_table* symtab);
1836
1837   // Return whether there is a .reginfo section.
1838   bool
1839   has_reginfo_section() const
1840   { return this->has_reginfo_section_; }
1841
1842   // Return whether we want to merge processor-specific data.
1843   bool
1844   merge_processor_specific_data() const
1845   { return this->merge_processor_specific_data_; }
1846
1847   // Return gprmask from the .reginfo section of this object.
1848   Valtype
1849   gprmask() const
1850   { return this->gprmask_; }
1851
1852   // Return cprmask1 from the .reginfo section of this object.
1853   Valtype
1854   cprmask1() const
1855   { return this->cprmask1_; }
1856
1857   // Return cprmask2 from the .reginfo section of this object.
1858   Valtype
1859   cprmask2() const
1860   { return this->cprmask2_; }
1861
1862   // Return cprmask3 from the .reginfo section of this object.
1863   Valtype
1864   cprmask3() const
1865   { return this->cprmask3_; }
1866
1867   // Return cprmask4 from the .reginfo section of this object.
1868   Valtype
1869   cprmask4() const
1870   { return this->cprmask4_; }
1871
1872   // This is the contents of the .MIPS.abiflags section if there is one.
1873   Mips_abiflags<big_endian>*
1874   abiflags()
1875   { return this->abiflags_; }
1876
1877   // This is the contents of the .gnu.attribute section if there is one.
1878   const Attributes_section_data*
1879   attributes_section_data() const
1880   { return this->attributes_section_data_; }
1881
1882  protected:
1883   // Count the local symbols.
1884   void
1885   do_count_local_symbols(Stringpool_template<char>*,
1886                          Stringpool_template<char>*);
1887
1888   // Read the symbol information.
1889   void
1890   do_read_symbols(Read_symbols_data* sd);
1891
1892  private:
1893   // The name of the options section.
1894   const char* mips_elf_options_section_name()
1895   { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1896
1897   // processor-specific flags in ELF file header.
1898   elfcpp::Elf_Word processor_specific_flags_;
1899
1900   // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1901   // This is only valid after do_count_local_symbol is called.
1902   std::vector<bool> local_symbol_is_mips16_;
1903
1904   // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1905   // This is only valid after do_count_local_symbol is called.
1906   std::vector<bool> local_symbol_is_micromips_;
1907
1908   // Map from section index to the MIPS16 stub for that section.  This contains
1909   // all stubs found in this object.
1910   Mips16_stubs_int_map mips16_stub_sections_;
1911
1912   // Local symbols that have "non 16-bit" call relocation.  This relocation
1913   // would need to refer to a MIPS16 fn stub, if there is one.
1914   std::set<unsigned int> local_non_16bit_calls_;
1915
1916   // Local symbols that have 16-bit call relocation R_MIPS16_26.  Local MIPS16
1917   // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1918   // relocation that refers to the stub symbol.
1919   std::set<unsigned int> local_16bit_calls_;
1920
1921   // Map from local symbol index to the MIPS16 fn stub for that symbol.
1922   // This contains only the stubs that we decided not to discard.
1923   Mips16_stubs_int_map local_mips16_fn_stubs_;
1924
1925   // Map from local symbol index to the MIPS16 call stub for that symbol.
1926   // This contains only the stubs that we decided not to discard.
1927   Mips16_stubs_int_map local_mips16_call_stubs_;
1928
1929   // gp value that was used to create this object.
1930   Mips_address gp_;
1931   // Whether the object is a PIC object.
1932   bool is_pic_ : 1;
1933   // Whether the object uses N32 ABI.
1934   bool is_n32_ : 1;
1935   // Whether the object contains a .reginfo section.
1936   bool has_reginfo_section_ : 1;
1937   // Whether we merge processor-specific data of this object to output.
1938   bool merge_processor_specific_data_ : 1;
1939   // The Mips_got_info for this object.
1940   Mips_got_info<size, big_endian>* got_info_;
1941
1942   // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1943   // This is only valid after do_read_symbols is called.
1944   std::vector<bool> section_is_mips16_fn_stub_;
1945
1946   // Bit vector to tell if a section is a MIPS16 call stub section or not.
1947   // This is only valid after do_read_symbols is called.
1948   std::vector<bool> section_is_mips16_call_stub_;
1949
1950   // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1951   // This is only valid after do_read_symbols is called.
1952   std::vector<bool> section_is_mips16_call_fp_stub_;
1953
1954   // .pdr section index.
1955   unsigned int pdr_shndx_;
1956
1957   // Object attributes if there is a .gnu.attributes section or NULL.
1958   Attributes_section_data* attributes_section_data_;
1959
1960   // Object abiflags if there is a .MIPS.abiflags section or NULL.
1961   Mips_abiflags<big_endian>* abiflags_;
1962
1963   // gprmask from the .reginfo section of this object.
1964   Valtype gprmask_;
1965   // cprmask1 from the .reginfo section of this object.
1966   Valtype cprmask1_;
1967   // cprmask2 from the .reginfo section of this object.
1968   Valtype cprmask2_;
1969   // cprmask3 from the .reginfo section of this object.
1970   Valtype cprmask3_;
1971   // cprmask4 from the .reginfo section of this object.
1972   Valtype cprmask4_;
1973 };
1974
1975 // Mips_output_data_got class.
1976
1977 template<int size, bool big_endian>
1978 class Mips_output_data_got : public Output_data_got<size, big_endian>
1979 {
1980   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1981   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1982     Reloc_section;
1983   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1984
1985  public:
1986   Mips_output_data_got(Target_mips<size, big_endian>* target,
1987       Symbol_table* symtab, Layout* layout)
1988     : Output_data_got<size, big_endian>(), target_(target),
1989       symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1990       first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1991       secondary_got_relocs_()
1992   {
1993     this->master_got_info_ = new Mips_got_info<size, big_endian>();
1994     this->set_addralign(16);
1995   }
1996
1997   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1998   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1999   void
2000   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
2001                           unsigned int symndx, Mips_address addend,
2002                           unsigned int r_type, unsigned int shndx,
2003                           bool is_section_symbol)
2004   {
2005     this->master_got_info_->record_local_got_symbol(object, symndx, addend,
2006                                                     r_type, shndx,
2007                                                     is_section_symbol);
2008   }
2009
2010   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
2011   // in OBJECT.  FOR_CALL is true if the caller is only interested in
2012   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
2013   // relocation.
2014   void
2015   record_global_got_symbol(Mips_symbol<size>* mips_sym,
2016                            Mips_relobj<size, big_endian>* object,
2017                            unsigned int r_type, bool dyn_reloc, bool for_call)
2018   {
2019     this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2020                                                      dyn_reloc, for_call);
2021   }
2022
2023   // Record that OBJECT has a page relocation against symbol SYMNDX and
2024   // that ADDEND is the addend for that relocation.
2025   void
2026   record_got_page_entry(Mips_relobj<size, big_endian>* object,
2027                         unsigned int symndx, int addend)
2028   { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2029
2030   // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
2031   // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2032   // applied in a static link.
2033   void
2034   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2035                    Mips_symbol<size>* gsym)
2036   { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2037
2038   // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
2039   // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
2040   // relocation that needs to be applied in a static link.
2041   void
2042   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2043                    Sized_relobj_file<size, big_endian>* relobj,
2044                    unsigned int index)
2045   {
2046     this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2047                                                 index));
2048   }
2049
2050   // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2051   // secondary GOT at OFFSET.
2052   void
2053   add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2054                           Mips_symbol<size>* gsym)
2055   {
2056     this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2057                                                        r_type, gsym));
2058   }
2059
2060   // Update GOT entry at OFFSET with VALUE.
2061   void
2062   update_got_entry(unsigned int offset, Mips_address value)
2063   {
2064     elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2065   }
2066
2067   // Return the number of entries in local part of the GOT.  This includes
2068   // local entries, page entries and 2 reserved entries.
2069   unsigned int
2070   get_local_gotno() const
2071   {
2072     if (!this->multi_got())
2073       {
2074         return (2 + this->master_got_info_->local_gotno()
2075                 + this->master_got_info_->page_gotno());
2076       }
2077     else
2078       return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2079   }
2080
2081   // Return dynamic symbol table index of the first symbol with global GOT
2082   // entry.
2083   unsigned int
2084   first_global_got_dynsym_index() const
2085   { return this->first_global_got_dynsym_index_; }
2086
2087   // Set dynamic symbol table index of the first symbol with global GOT entry.
2088   void
2089   set_first_global_got_dynsym_index(unsigned int index)
2090   { this->first_global_got_dynsym_index_ = index; }
2091
2092   // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
2093   // larger than 64K, create multi-GOT.
2094   void
2095   lay_out_got(Layout* layout, Symbol_table* symtab,
2096               const Input_objects* input_objects);
2097
2098   // Create multi-GOT.  For every GOT, add local, global and TLS entries.
2099   void
2100   lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2101
2102   // Attempt to merge GOTs of different input objects.
2103   void
2104   merge_gots(const Input_objects* input_objects);
2105
2106   // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
2107   // this would lead to overflow, true if they were merged successfully.
2108   bool
2109   merge_got_with(Mips_got_info<size, big_endian>* from,
2110                  Mips_relobj<size, big_endian>* object,
2111                  Mips_got_info<size, big_endian>* to);
2112
2113   // Return the offset of GOT page entry for VALUE.  For multi-GOT links,
2114   // use OBJECT's GOT.
2115   unsigned int
2116   get_got_page_offset(Mips_address value,
2117                       const Mips_relobj<size, big_endian>* object)
2118   {
2119     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2120                                           ? this->master_got_info_
2121                                           : object->get_got_info());
2122     gold_assert(g != NULL);
2123     return g->get_got_page_offset(value, this);
2124   }
2125
2126   // Return the GOT offset of type GOT_TYPE of the global symbol
2127   // GSYM.  For multi-GOT links, use OBJECT's GOT.
2128   unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2129                           Mips_relobj<size, big_endian>* object) const
2130   {
2131     if (!this->multi_got())
2132       return gsym->got_offset(got_type);
2133     else
2134       {
2135         Mips_got_info<size, big_endian>* g = object->get_got_info();
2136         gold_assert(g != NULL);
2137         return gsym->got_offset(g->multigot_got_type(got_type));
2138       }
2139   }
2140
2141   // Return the GOT offset of type GOT_TYPE of the local symbol
2142   // SYMNDX.
2143   unsigned int
2144   got_offset(unsigned int symndx, unsigned int got_type,
2145              Sized_relobj_file<size, big_endian>* object,
2146              uint64_t addend) const
2147   { return object->local_got_offset(symndx, got_type, addend); }
2148
2149   // Return the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2150   unsigned int
2151   tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2152   {
2153     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2154                                           ? this->master_got_info_
2155                                           : object->get_got_info());
2156     gold_assert(g != NULL);
2157     return g->tls_ldm_offset();
2158   }
2159
2160   // Set the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2161   void
2162   set_tls_ldm_offset(unsigned int tls_ldm_offset,
2163                      Mips_relobj<size, big_endian>* object)
2164   {
2165     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2166                                           ? this->master_got_info_
2167                                           : object->get_got_info());
2168     gold_assert(g != NULL);
2169     g->set_tls_ldm_offset(tls_ldm_offset);
2170   }
2171
2172   // Return true for multi-GOT links.
2173   bool
2174   multi_got() const
2175   { return this->primary_got_ != NULL; }
2176
2177   // Return the offset of OBJECT's GOT from the start of .got section.
2178   unsigned int
2179   get_got_offset(const Mips_relobj<size, big_endian>* object)
2180   {
2181     if (!this->multi_got())
2182       return 0;
2183     else
2184       {
2185         Mips_got_info<size, big_endian>* g = object->get_got_info();
2186         return g != NULL ? g->offset() : 0;
2187       }
2188   }
2189
2190   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2191   void
2192   add_reloc_only_entries()
2193   { this->master_got_info_->add_reloc_only_entries(this); }
2194
2195   // Return offset of the primary GOT's entry for global symbol.
2196   unsigned int
2197   get_primary_got_offset(const Mips_symbol<size>* sym) const
2198   {
2199     gold_assert(sym->global_got_area() != GGA_NONE);
2200     return (this->get_local_gotno() + sym->dynsym_index()
2201             - this->first_global_got_dynsym_index()) * size/8;
2202   }
2203
2204   // For the entry at offset GOT_OFFSET, return its offset from the gp.
2205   // Input argument GOT_OFFSET is always global offset from the start of
2206   // .got section, for both single and multi-GOT links.
2207   // For single GOT links, this returns GOT_OFFSET - 0x7FF0.  For multi-GOT
2208   // links, the return value is object_got_offset - 0x7FF0, where
2209   // object_got_offset is offset in the OBJECT's GOT.
2210   int
2211   gp_offset(unsigned int got_offset,
2212             const Mips_relobj<size, big_endian>* object) const
2213   {
2214     return (this->address() + got_offset
2215             - this->target_->adjusted_gp_value(object));
2216   }
2217
2218  protected:
2219   // Write out the GOT table.
2220   void
2221   do_write(Output_file*);
2222
2223  private:
2224
2225   // This class represent dynamic relocations that need to be applied by
2226   // gold because we are using TLS relocations in a static link.
2227   class Static_reloc
2228   {
2229    public:
2230     Static_reloc(unsigned int got_offset, unsigned int r_type,
2231                  Mips_symbol<size>* gsym)
2232       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2233     { this->u_.global.symbol = gsym; }
2234
2235     Static_reloc(unsigned int got_offset, unsigned int r_type,
2236           Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2237       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2238     {
2239       this->u_.local.relobj = relobj;
2240       this->u_.local.index = index;
2241     }
2242
2243     // Return the GOT offset.
2244     unsigned int
2245     got_offset() const
2246     { return this->got_offset_; }
2247
2248     // Relocation type.
2249     unsigned int
2250     r_type() const
2251     { return this->r_type_; }
2252
2253     // Whether the symbol is global or not.
2254     bool
2255     symbol_is_global() const
2256     { return this->symbol_is_global_; }
2257
2258     // For a relocation against a global symbol, the global symbol.
2259     Mips_symbol<size>*
2260     symbol() const
2261     {
2262       gold_assert(this->symbol_is_global_);
2263       return this->u_.global.symbol;
2264     }
2265
2266     // For a relocation against a local symbol, the defining object.
2267     Sized_relobj_file<size, big_endian>*
2268     relobj() const
2269     {
2270       gold_assert(!this->symbol_is_global_);
2271       return this->u_.local.relobj;
2272     }
2273
2274     // For a relocation against a local symbol, the local symbol index.
2275     unsigned int
2276     index() const
2277     {
2278       gold_assert(!this->symbol_is_global_);
2279       return this->u_.local.index;
2280     }
2281
2282    private:
2283     // GOT offset of the entry to which this relocation is applied.
2284     unsigned int got_offset_;
2285     // Type of relocation.
2286     unsigned int r_type_;
2287     // Whether this relocation is against a global symbol.
2288     bool symbol_is_global_;
2289     // A global or local symbol.
2290     union
2291     {
2292       struct
2293       {
2294         // For a global symbol, the symbol itself.
2295         Mips_symbol<size>* symbol;
2296       } global;
2297       struct
2298       {
2299         // For a local symbol, the object defining object.
2300         Sized_relobj_file<size, big_endian>* relobj;
2301         // For a local symbol, the symbol index.
2302         unsigned int index;
2303       } local;
2304     } u_;
2305   };
2306
2307   // The target.
2308   Target_mips<size, big_endian>* target_;
2309   // The symbol table.
2310   Symbol_table* symbol_table_;
2311   // The layout.
2312   Layout* layout_;
2313   // Static relocs to be applied to the GOT.
2314   std::vector<Static_reloc> static_relocs_;
2315   // .got section view.
2316   unsigned char* got_view_;
2317   // The dynamic symbol table index of the first symbol with global GOT entry.
2318   unsigned int first_global_got_dynsym_index_;
2319   // The master GOT information.
2320   Mips_got_info<size, big_endian>* master_got_info_;
2321   // The  primary GOT information.
2322   Mips_got_info<size, big_endian>* primary_got_;
2323   // Secondary GOT fixups.
2324   std::vector<Static_reloc> secondary_got_relocs_;
2325 };
2326
2327 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2328 // two ways of creating these interfaces.  The first is to add:
2329 //
2330 //      lui     $25,%hi(func)
2331 //      j       func
2332 //      addiu   $25,$25,%lo(func)
2333 //
2334 // to a separate trampoline section.  The second is to add:
2335 //
2336 //      lui     $25,%hi(func)
2337 //      addiu   $25,$25,%lo(func)
2338 //
2339 // immediately before a PIC function "func", but only if a function is at the
2340 // beginning of the section, and the section is not too heavily aligned (i.e we
2341 // would need to add no more than 2 nops before the stub.)
2342 //
2343 // We only create stubs of the first type.
2344
2345 template<int size, bool big_endian>
2346 class Mips_output_data_la25_stub : public Output_section_data
2347 {
2348   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2349
2350  public:
2351   Mips_output_data_la25_stub()
2352   : Output_section_data(size == 32 ? 4 : 8), symbols_()
2353   { }
2354
2355   // Create LA25 stub for a symbol.
2356   void
2357   create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2358                    Mips_symbol<size>* gsym);
2359
2360   // Return output address of a stub.
2361   Mips_address
2362   stub_address(const Mips_symbol<size>* sym) const
2363   {
2364     gold_assert(sym->has_la25_stub());
2365     return this->address() + sym->la25_stub_offset();
2366   }
2367
2368  protected:
2369   void
2370   do_adjust_output_section(Output_section* os)
2371   { os->set_entsize(0); }
2372
2373  private:
2374   // Template for standard LA25 stub.
2375   static const uint32_t la25_stub_entry[];
2376   // Template for microMIPS LA25 stub.
2377   static const uint32_t la25_stub_micromips_entry[];
2378
2379   // Set the final size.
2380   void
2381   set_final_data_size()
2382   { this->set_data_size(this->symbols_.size() * 16); }
2383
2384   // Create a symbol for SYM stub's value and size, to help make the
2385   // disassembly easier to read.
2386   void
2387   create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2388                      Target_mips<size, big_endian>* target, uint64_t symsize);
2389
2390   // Write to a map file.
2391   void
2392   do_print_to_mapfile(Mapfile* mapfile) const
2393   { mapfile->print_output_data(this, _(".LA25.stubs")); }
2394
2395   // Write out the LA25 stub section.
2396   void
2397   do_write(Output_file*);
2398
2399   // Symbols that have LA25 stubs.
2400   std::vector<Mips_symbol<size>*> symbols_;
2401 };
2402
2403 // MIPS-specific relocation writer.
2404
2405 template<int sh_type, bool dynamic, int size, bool big_endian>
2406 struct Mips_output_reloc_writer;
2407
2408 template<int sh_type, bool dynamic, bool big_endian>
2409 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2410 {
2411   typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2412   typedef std::vector<Output_reloc_type> Relocs;
2413
2414   static void
2415   write(typename Relocs::const_iterator p, unsigned char* pov)
2416   { p->write(pov); }
2417 };
2418
2419 template<int sh_type, bool dynamic, bool big_endian>
2420 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2421 {
2422   typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2423   typedef std::vector<Output_reloc_type> Relocs;
2424
2425   static void
2426   write(typename Relocs::const_iterator p, unsigned char* pov)
2427   {
2428     elfcpp::Mips64_rel_write<big_endian> orel(pov);
2429     orel.put_r_offset(p->get_address());
2430     orel.put_r_sym(p->get_symbol_index());
2431     orel.put_r_ssym(RSS_UNDEF);
2432     orel.put_r_type(p->type());
2433     if (p->type() == elfcpp::R_MIPS_REL32)
2434       orel.put_r_type2(elfcpp::R_MIPS_64);
2435     else
2436       orel.put_r_type2(elfcpp::R_MIPS_NONE);
2437     orel.put_r_type3(elfcpp::R_MIPS_NONE);
2438   }
2439 };
2440
2441 template<int sh_type, bool dynamic, int size, bool big_endian>
2442 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2443                                                         size, big_endian>
2444 {
2445  public:
2446   Mips_output_data_reloc(bool sort_relocs)
2447     : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2448   { }
2449
2450  protected:
2451   // Write out the data.
2452   void
2453   do_write(Output_file* of)
2454   {
2455     typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2456         big_endian> Writer;
2457     this->template do_write_generic<Writer>(of);
2458   }
2459 };
2460
2461
2462 // A class to handle the PLT data.
2463
2464 template<int size, bool big_endian>
2465 class Mips_output_data_plt : public Output_section_data
2466 {
2467   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2468   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2469                                  size, big_endian> Reloc_section;
2470
2471  public:
2472   // Create the PLT section.  The ordinary .got section is an argument,
2473   // since we need to refer to the start.
2474   Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2475                        Target_mips<size, big_endian>* target)
2476     : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2477       plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2478       target_(target)
2479   {
2480     this->rel_ = new Reloc_section(false);
2481     layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2482                                     elfcpp::SHF_ALLOC, this->rel_,
2483                                     ORDER_DYNAMIC_PLT_RELOCS, false);
2484   }
2485
2486   // Add an entry to the PLT for a symbol referenced by r_type relocation.
2487   void
2488   add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2489
2490   // Return the .rel.plt section data.
2491   Reloc_section*
2492   rel_plt() const
2493   { return this->rel_; }
2494
2495   // Return the number of PLT entries.
2496   unsigned int
2497   entry_count() const
2498   { return this->symbols_.size(); }
2499
2500   // Return the offset of the first non-reserved PLT entry.
2501   unsigned int
2502   first_plt_entry_offset() const
2503   { return sizeof(plt0_entry_o32); }
2504
2505   // Return the size of a PLT entry.
2506   unsigned int
2507   plt_entry_size() const
2508   { return sizeof(plt_entry); }
2509
2510   // Set final PLT offsets.  For each symbol, determine whether standard or
2511   // compressed (MIPS16 or microMIPS) PLT entry is used.
2512   void
2513   set_plt_offsets();
2514
2515   // Return the offset of the first standard PLT entry.
2516   unsigned int
2517   first_mips_plt_offset() const
2518   { return this->plt_header_size_; }
2519
2520   // Return the offset of the first compressed PLT entry.
2521   unsigned int
2522   first_comp_plt_offset() const
2523   { return this->plt_header_size_ + this->plt_mips_offset_; }
2524
2525   // Return whether there are any standard PLT entries.
2526   bool
2527   has_standard_entries() const
2528   { return this->plt_mips_offset_ > 0; }
2529
2530   // Return the output address of standard PLT entry.
2531   Mips_address
2532   mips_entry_address(const Mips_symbol<size>* sym) const
2533   {
2534     gold_assert (sym->has_mips_plt_offset());
2535     return (this->address() + this->first_mips_plt_offset()
2536             + sym->mips_plt_offset());
2537   }
2538
2539   // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2540   Mips_address
2541   comp_entry_address(const Mips_symbol<size>* sym) const
2542   {
2543     gold_assert (sym->has_comp_plt_offset());
2544     return (this->address() + this->first_comp_plt_offset()
2545             + sym->comp_plt_offset());
2546   }
2547
2548  protected:
2549   void
2550   do_adjust_output_section(Output_section* os)
2551   { os->set_entsize(0); }
2552
2553   // Write to a map file.
2554   void
2555   do_print_to_mapfile(Mapfile* mapfile) const
2556   { mapfile->print_output_data(this, _(".plt")); }
2557
2558  private:
2559   // Template for the first PLT entry.
2560   static const uint32_t plt0_entry_o32[];
2561   static const uint32_t plt0_entry_n32[];
2562   static const uint32_t plt0_entry_n64[];
2563   static const uint32_t plt0_entry_micromips_o32[];
2564   static const uint32_t plt0_entry_micromips32_o32[];
2565
2566   // Template for subsequent PLT entries.
2567   static const uint32_t plt_entry[];
2568   static const uint32_t plt_entry_r6[];
2569   static const uint32_t plt_entry_mips16_o32[];
2570   static const uint32_t plt_entry_micromips_o32[];
2571   static const uint32_t plt_entry_micromips32_o32[];
2572
2573   // Set the final size.
2574   void
2575   set_final_data_size()
2576   {
2577     this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2578                         + this->plt_comp_offset_);
2579   }
2580
2581   // Write out the PLT data.
2582   void
2583   do_write(Output_file*);
2584
2585   // Return whether the plt header contains microMIPS code.  For the sake of
2586   // cache alignment always use a standard header whenever any standard entries
2587   // are present even if microMIPS entries are present as well.  This also lets
2588   // the microMIPS header rely on the value of $v0 only set by microMIPS
2589   // entries, for a small size reduction.
2590   bool
2591   is_plt_header_compressed() const
2592   {
2593     gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2594     return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2595   }
2596
2597   // Return the size of the PLT header.
2598   unsigned int
2599   get_plt_header_size() const
2600   {
2601     if (this->target_->is_output_n64())
2602       return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2603     else if (this->target_->is_output_n32())
2604       return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2605     else if (!this->is_plt_header_compressed())
2606       return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2607     else if (this->target_->use_32bit_micromips_instructions())
2608       return (2 * sizeof(plt0_entry_micromips32_o32)
2609               / sizeof(plt0_entry_micromips32_o32[0]));
2610     else
2611       return (2 * sizeof(plt0_entry_micromips_o32)
2612               / sizeof(plt0_entry_micromips_o32[0]));
2613   }
2614
2615   // Return the PLT header entry.
2616   const uint32_t*
2617   get_plt_header_entry() const
2618   {
2619     if (this->target_->is_output_n64())
2620       return plt0_entry_n64;
2621     else if (this->target_->is_output_n32())
2622       return plt0_entry_n32;
2623     else if (!this->is_plt_header_compressed())
2624       return plt0_entry_o32;
2625     else if (this->target_->use_32bit_micromips_instructions())
2626       return plt0_entry_micromips32_o32;
2627     else
2628       return plt0_entry_micromips_o32;
2629   }
2630
2631   // Return the size of the standard PLT entry.
2632   unsigned int
2633   standard_plt_entry_size() const
2634   { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2635
2636   // Return the size of the compressed PLT entry.
2637   unsigned int
2638   compressed_plt_entry_size() const
2639   {
2640     gold_assert(!this->target_->is_output_newabi());
2641
2642     if (!this->target_->is_output_micromips())
2643       return (2 * sizeof(plt_entry_mips16_o32)
2644               / sizeof(plt_entry_mips16_o32[0]));
2645     else if (this->target_->use_32bit_micromips_instructions())
2646       return (2 * sizeof(plt_entry_micromips32_o32)
2647               / sizeof(plt_entry_micromips32_o32[0]));
2648     else
2649       return (2 * sizeof(plt_entry_micromips_o32)
2650               / sizeof(plt_entry_micromips_o32[0]));
2651   }
2652
2653   // The reloc section.
2654   Reloc_section* rel_;
2655   // The .got.plt section.
2656   Output_data_space* got_plt_;
2657   // Symbols that have PLT entry.
2658   std::vector<Mips_symbol<size>*> symbols_;
2659   // The offset of the next standard PLT entry to create.
2660   unsigned int plt_mips_offset_;
2661   // The offset of the next compressed PLT entry to create.
2662   unsigned int plt_comp_offset_;
2663   // The size of the PLT header in bytes.
2664   unsigned int plt_header_size_;
2665   // The target.
2666   Target_mips<size, big_endian>* target_;
2667 };
2668
2669 // A class to handle the .MIPS.stubs data.
2670
2671 template<int size, bool big_endian>
2672 class Mips_output_data_mips_stubs : public Output_section_data
2673 {
2674   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2675
2676   // Unordered set of .MIPS.stubs entries.
2677   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2678       Mips_stubs_entry_set;
2679
2680  public:
2681    Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2682      : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2683        stub_offsets_are_set_(false), target_(target)
2684    { }
2685
2686   // Create entry for a symbol.
2687   void
2688   make_entry(Mips_symbol<size>*);
2689
2690   // Remove entry for a symbol.
2691   void
2692   remove_entry(Mips_symbol<size>* gsym);
2693
2694   // Set stub offsets for symbols.  This method expects that the number of
2695   // entries in dynamic symbol table is set.
2696   void
2697   set_lazy_stub_offsets();
2698
2699   void
2700   set_needs_dynsym_value();
2701
2702    // Set the number of entries in dynamic symbol table.
2703   void
2704   set_dynsym_count(unsigned int dynsym_count)
2705   { this->dynsym_count_ = dynsym_count; }
2706
2707   // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2708   // count is greater than 0x10000.  If the dynamic symbol count is less than
2709   // 0x10000, the stub will be 4 bytes smaller.
2710   // There's no disadvantage from using microMIPS code here, so for the sake of
2711   // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2712   // output produced at all.  This has a benefit of stubs being shorter by
2713   // 4 bytes each too, unless in the insn32 mode.
2714   unsigned int
2715   stub_max_size() const
2716   {
2717     if (!this->target_->is_output_micromips()
2718         || this->target_->use_32bit_micromips_instructions())
2719       return 20;
2720     else
2721       return 16;
2722   }
2723
2724   // Return the size of the stub.  This method expects that the final dynsym
2725   // count is set.
2726   unsigned int
2727   stub_size() const
2728   {
2729     gold_assert(this->dynsym_count_ != -1U);
2730     if (this->dynsym_count_ > 0x10000)
2731       return this->stub_max_size();
2732     else
2733       return this->stub_max_size() - 4;
2734   }
2735
2736   // Return output address of a stub.
2737   Mips_address
2738   stub_address(const Mips_symbol<size>* sym) const
2739   {
2740     gold_assert(sym->has_lazy_stub());
2741     return this->address() + sym->lazy_stub_offset();
2742   }
2743
2744  protected:
2745   void
2746   do_adjust_output_section(Output_section* os)
2747   { os->set_entsize(0); }
2748
2749   // Write to a map file.
2750   void
2751   do_print_to_mapfile(Mapfile* mapfile) const
2752   { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2753
2754  private:
2755   static const uint32_t lazy_stub_normal_1[];
2756   static const uint32_t lazy_stub_normal_1_n64[];
2757   static const uint32_t lazy_stub_normal_2[];
2758   static const uint32_t lazy_stub_normal_2_n64[];
2759   static const uint32_t lazy_stub_big[];
2760   static const uint32_t lazy_stub_big_n64[];
2761
2762   static const uint32_t lazy_stub_micromips_normal_1[];
2763   static const uint32_t lazy_stub_micromips_normal_1_n64[];
2764   static const uint32_t lazy_stub_micromips_normal_2[];
2765   static const uint32_t lazy_stub_micromips_normal_2_n64[];
2766   static const uint32_t lazy_stub_micromips_big[];
2767   static const uint32_t lazy_stub_micromips_big_n64[];
2768
2769   static const uint32_t lazy_stub_micromips32_normal_1[];
2770   static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2771   static const uint32_t lazy_stub_micromips32_normal_2[];
2772   static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2773   static const uint32_t lazy_stub_micromips32_big[];
2774   static const uint32_t lazy_stub_micromips32_big_n64[];
2775
2776   // Set the final size.
2777   void
2778   set_final_data_size()
2779   { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2780
2781   // Write out the .MIPS.stubs data.
2782   void
2783   do_write(Output_file*);
2784
2785   // .MIPS.stubs symbols
2786   Mips_stubs_entry_set symbols_;
2787   // Number of entries in dynamic symbol table.
2788   unsigned int dynsym_count_;
2789   // Whether the stub offsets are set.
2790   bool stub_offsets_are_set_;
2791   // The target.
2792   Target_mips<size, big_endian>* target_;
2793 };
2794
2795 // This class handles Mips .reginfo output section.
2796
2797 template<int size, bool big_endian>
2798 class Mips_output_section_reginfo : public Output_section_data
2799 {
2800   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2801
2802  public:
2803   Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2804                               Valtype gprmask, Valtype cprmask1,
2805                               Valtype cprmask2, Valtype cprmask3,
2806                               Valtype cprmask4)
2807     : Output_section_data(24, 4, true), target_(target),
2808       gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2809       cprmask3_(cprmask3), cprmask4_(cprmask4)
2810   { }
2811
2812  protected:
2813   // Write to a map file.
2814   void
2815   do_print_to_mapfile(Mapfile* mapfile) const
2816   { mapfile->print_output_data(this, _(".reginfo")); }
2817
2818   // Write out reginfo section.
2819   void
2820   do_write(Output_file* of);
2821
2822  private:
2823   Target_mips<size, big_endian>* target_;
2824
2825   // gprmask of the output .reginfo section.
2826   Valtype gprmask_;
2827   // cprmask1 of the output .reginfo section.
2828   Valtype cprmask1_;
2829   // cprmask2 of the output .reginfo section.
2830   Valtype cprmask2_;
2831   // cprmask3 of the output .reginfo section.
2832   Valtype cprmask3_;
2833   // cprmask4 of the output .reginfo section.
2834   Valtype cprmask4_;
2835 };
2836
2837 // This class handles .MIPS.options output section.
2838
2839 template<int size, bool big_endian>
2840 class Mips_output_section_options : public Output_section
2841 {
2842  public:
2843   Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
2844                               elfcpp::Elf_Xword flags,
2845                               Target_mips<size, big_endian>* target)
2846     : Output_section(name, type, flags), target_(target)
2847   {
2848     // After the input sections are written, we only need to update
2849     // ri_gp_value field of ODK_REGINFO entries.
2850     this->set_after_input_sections();
2851   }
2852
2853  protected:
2854   // Write out option section.
2855   void
2856   do_write(Output_file* of);
2857
2858  private:
2859   Target_mips<size, big_endian>* target_;
2860 };
2861
2862 // This class handles .MIPS.abiflags output section.
2863
2864 template<int size, bool big_endian>
2865 class Mips_output_section_abiflags : public Output_section_data
2866 {
2867  public:
2868   Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2869     : Output_section_data(24, 8, true), abiflags_(abiflags)
2870   { }
2871
2872  protected:
2873   // Write to a map file.
2874   void
2875   do_print_to_mapfile(Mapfile* mapfile) const
2876   { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2877
2878   void
2879   do_write(Output_file* of);
2880
2881  private:
2882   const Mips_abiflags<big_endian>& abiflags_;
2883 };
2884
2885 // The MIPS target has relocation types which default handling of relocatable
2886 // relocation cannot process.  So we have to extend the default code.
2887
2888 template<bool big_endian, typename Classify_reloc>
2889 class Mips_scan_relocatable_relocs :
2890   public Default_scan_relocatable_relocs<Classify_reloc>
2891 {
2892  public:
2893   // Return the strategy to use for a local symbol which is a section
2894   // symbol, given the relocation type.
2895   inline Relocatable_relocs::Reloc_strategy
2896   local_section_strategy(unsigned int r_type, Relobj* object)
2897   {
2898     if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2899       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2900     else
2901       {
2902         switch (r_type)
2903           {
2904           case elfcpp::R_MIPS_26:
2905             return Relocatable_relocs::RELOC_SPECIAL;
2906
2907           default:
2908             return Default_scan_relocatable_relocs<Classify_reloc>::
2909                 local_section_strategy(r_type, object);
2910           }
2911       }
2912   }
2913 };
2914
2915 // Mips_copy_relocs class.  The only difference from the base class is the
2916 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2917 // Mips cannot convert all relocation types to dynamic relocs.  If a reloc
2918 // cannot be made dynamic, a COPY reloc is emitted.
2919
2920 template<int sh_type, int size, bool big_endian>
2921 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2922 {
2923  public:
2924   Mips_copy_relocs()
2925     : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2926   { }
2927
2928   // Emit any saved relocations which turn out to be needed.  This is
2929   // called after all the relocs have been scanned.
2930   void
2931   emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2932             Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2933
2934  private:
2935   typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2936     Copy_reloc_entry;
2937
2938   // Emit this reloc if appropriate.  This is called after we have
2939   // scanned all the relocations, so we know whether we emitted a
2940   // COPY relocation for SYM_.
2941   void
2942   emit_entry(Copy_reloc_entry& entry,
2943              Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2944              Symbol_table* symtab, Layout* layout,
2945              Target_mips<size, big_endian>* target);
2946 };
2947
2948
2949 // Return true if the symbol SYM should be considered to resolve local
2950 // to the current module, and false otherwise.  The logic is taken from
2951 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2952 static bool
2953 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2954                   bool local_protected)
2955 {
2956   // If it's a local sym, of course we resolve locally.
2957   if (sym == NULL)
2958     return true;
2959
2960   // STV_HIDDEN or STV_INTERNAL ones must be local.
2961   if (sym->visibility() == elfcpp::STV_HIDDEN
2962       || sym->visibility() == elfcpp::STV_INTERNAL)
2963     return true;
2964
2965   // If we don't have a definition in a regular file, then we can't
2966   // resolve locally.  The sym is either undefined or dynamic.
2967   if (sym->is_from_dynobj() || sym->is_undefined())
2968     return false;
2969
2970   // Forced local symbols resolve locally.
2971   if (sym->is_forced_local())
2972     return true;
2973
2974   // As do non-dynamic symbols.
2975   if (!has_dynsym_entry)
2976     return true;
2977
2978   // At this point, we know the symbol is defined and dynamic.  In an
2979   // executable it must resolve locally, likewise when building symbolic
2980   // shared libraries.
2981   if (parameters->options().output_is_executable()
2982       || parameters->options().Bsymbolic())
2983     return true;
2984
2985   // Now deal with defined dynamic symbols in shared libraries.  Ones
2986   // with default visibility might not resolve locally.
2987   if (sym->visibility() == elfcpp::STV_DEFAULT)
2988     return false;
2989
2990   // STV_PROTECTED non-function symbols are local.
2991   if (sym->type() != elfcpp::STT_FUNC)
2992     return true;
2993
2994   // Function pointer equality tests may require that STV_PROTECTED
2995   // symbols be treated as dynamic symbols.  If the address of a
2996   // function not defined in an executable is set to that function's
2997   // plt entry in the executable, then the address of the function in
2998   // a shared library must also be the plt entry in the executable.
2999   return local_protected;
3000 }
3001
3002 // Return TRUE if references to this symbol always reference the symbol in this
3003 // object.
3004 static bool
3005 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3006 {
3007   return symbol_refs_local(sym, has_dynsym_entry, false);
3008 }
3009
3010 // Return TRUE if calls to this symbol always call the version in this object.
3011 static bool
3012 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3013 {
3014   return symbol_refs_local(sym, has_dynsym_entry, true);
3015 }
3016
3017 // Compare GOT offsets of two symbols.
3018
3019 template<int size, bool big_endian>
3020 static bool
3021 got_offset_compare(Symbol* sym1, Symbol* sym2)
3022 {
3023   Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
3024   Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
3025   unsigned int area1 = mips_sym1->global_got_area();
3026   unsigned int area2 = mips_sym2->global_got_area();
3027   gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
3028
3029   // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3030   if (area1 != area2)
3031     return area1 < area2;
3032
3033   return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
3034 }
3035
3036 // This method divides dynamic symbols into symbols that have GOT entry, and
3037 // symbols that don't have GOT entry.  It also sorts symbols with the GOT entry.
3038 // Mips ABI requires that symbols with the GOT entry must be at the end of
3039 // dynamic symbol table, and the order in dynamic symbol table must match the
3040 // order in GOT.
3041
3042 template<int size, bool big_endian>
3043 static void
3044 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3045                     std::vector<Symbol*>* non_got_symbols,
3046                     std::vector<Symbol*>* got_symbols)
3047 {
3048   for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3049        p != dyn_symbols->end();
3050        ++p)
3051     {
3052       Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3053       if (mips_sym->global_got_area() == GGA_NORMAL
3054           || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3055         got_symbols->push_back(mips_sym);
3056       else
3057         non_got_symbols->push_back(mips_sym);
3058     }
3059
3060   std::sort(got_symbols->begin(), got_symbols->end(),
3061             got_offset_compare<size, big_endian>);
3062 }
3063
3064 // Functor class for processing the global symbol table.
3065
3066 template<int size, bool big_endian>
3067 class Symbol_visitor_check_symbols
3068 {
3069  public:
3070   Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3071     Layout* layout, Symbol_table* symtab)
3072     : target_(target), layout_(layout), symtab_(symtab)
3073   { }
3074
3075   void
3076   operator()(Sized_symbol<size>* sym)
3077   {
3078     Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3079     if (local_pic_function<size, big_endian>(mips_sym))
3080       {
3081         // SYM is a function that might need $25 to be valid on entry.
3082         // If we're creating a non-PIC relocatable object, mark SYM as
3083         // being PIC.  If we're creating a non-relocatable object with
3084         // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3085         // stub.
3086         if (parameters->options().relocatable())
3087           {
3088             if (!parameters->options().output_is_position_independent())
3089               mips_sym->set_pic();
3090           }
3091         else if (mips_sym->has_nonpic_branches())
3092           {
3093             this->target_->la25_stub_section(layout_)
3094                 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3095           }
3096       }
3097   }
3098
3099  private:
3100   Target_mips<size, big_endian>* target_;
3101   Layout* layout_;
3102   Symbol_table* symtab_;
3103 };
3104
3105 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3106 // and endianness. The relocation format for MIPS-64 is non-standard.
3107
3108 template<int sh_type, int size, bool big_endian>
3109 struct Mips_reloc_types;
3110
3111 template<bool big_endian>
3112 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3113 {
3114   typedef typename elfcpp::Rel<32, big_endian> Reloc;
3115   typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3116
3117   static typename elfcpp::Elf_types<32>::Elf_Swxword
3118   get_r_addend(const Reloc*)
3119   { return 0; }
3120
3121   static inline void
3122   set_reloc_addend(Reloc_write*,
3123                    typename elfcpp::Elf_types<32>::Elf_Swxword)
3124   { gold_unreachable(); }
3125 };
3126
3127 template<bool big_endian>
3128 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3129 {
3130   typedef typename elfcpp::Rela<32, big_endian> Reloc;
3131   typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3132
3133   static typename elfcpp::Elf_types<32>::Elf_Swxword
3134   get_r_addend(const Reloc* reloc)
3135   { return reloc->get_r_addend(); }
3136
3137   static inline void
3138   set_reloc_addend(Reloc_write* p,
3139                    typename elfcpp::Elf_types<32>::Elf_Swxword val)
3140   { p->put_r_addend(val); }
3141 };
3142
3143 template<bool big_endian>
3144 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3145 {
3146   typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3147   typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3148
3149   static typename elfcpp::Elf_types<64>::Elf_Swxword
3150   get_r_addend(const Reloc*)
3151   { return 0; }
3152
3153   static inline void
3154   set_reloc_addend(Reloc_write*,
3155                    typename elfcpp::Elf_types<64>::Elf_Swxword)
3156   { gold_unreachable(); }
3157 };
3158
3159 template<bool big_endian>
3160 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3161 {
3162   typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3163   typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3164
3165   static typename elfcpp::Elf_types<64>::Elf_Swxword
3166   get_r_addend(const Reloc* reloc)
3167   { return reloc->get_r_addend(); }
3168
3169   static inline void
3170   set_reloc_addend(Reloc_write* p,
3171                    typename elfcpp::Elf_types<64>::Elf_Swxword val)
3172   { p->put_r_addend(val); }
3173 };
3174
3175 // Forward declaration.
3176 static unsigned int
3177 mips_get_size_for_reloc(unsigned int, Relobj*);
3178
3179 // A class for inquiring about properties of a relocation,
3180 // used while scanning relocs during a relocatable link and
3181 // garbage collection.
3182
3183 template<int sh_type_, int size, bool big_endian>
3184 class Mips_classify_reloc;
3185
3186 template<int sh_type_, bool big_endian>
3187 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3188     public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3189 {
3190  public:
3191   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3192       Reltype;
3193   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3194       Reltype_write;
3195
3196   // Return the symbol referred to by the relocation.
3197   static inline unsigned int
3198   get_r_sym(const Reltype* reloc)
3199   { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3200
3201   // Return the type of the relocation.
3202   static inline unsigned int
3203   get_r_type(const Reltype* reloc)
3204   { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3205
3206   static inline unsigned int
3207   get_r_type2(const Reltype*)
3208   { return 0; }
3209
3210   static inline unsigned int
3211   get_r_type3(const Reltype*)
3212   { return 0; }
3213
3214   static inline unsigned int
3215   get_r_ssym(const Reltype*)
3216   { return 0; }
3217
3218   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3219   static inline unsigned int
3220   get_r_addend(const Reltype* reloc)
3221   {
3222     if (sh_type_ == elfcpp::SHT_REL)
3223       return 0;
3224     return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3225   }
3226
3227   // Write the r_info field to a new reloc, using the r_info field from
3228   // the original reloc, replacing the r_sym field with R_SYM.
3229   static inline void
3230   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3231   {
3232     unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3233     new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3234   }
3235
3236   // Write the r_addend field to a new reloc.
3237   static inline void
3238   put_r_addend(Reltype_write* to,
3239                typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3240   { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3241
3242   // Return the size of the addend of the relocation (only used for SHT_REL).
3243   static unsigned int
3244   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3245   { return mips_get_size_for_reloc(r_type, obj); }
3246 };
3247
3248 template<int sh_type_, bool big_endian>
3249 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3250     public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3251 {
3252  public:
3253   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3254       Reltype;
3255   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3256       Reltype_write;
3257
3258   // Return the symbol referred to by the relocation.
3259   static inline unsigned int
3260   get_r_sym(const Reltype* reloc)
3261   { return reloc->get_r_sym(); }
3262
3263   // Return the r_type of the relocation.
3264   static inline unsigned int
3265   get_r_type(const Reltype* reloc)
3266   { return reloc->get_r_type(); }
3267
3268   // Return the r_type2 of the relocation.
3269   static inline unsigned int
3270   get_r_type2(const Reltype* reloc)
3271   { return reloc->get_r_type2(); }
3272
3273   // Return the r_type3 of the relocation.
3274   static inline unsigned int
3275   get_r_type3(const Reltype* reloc)
3276   { return reloc->get_r_type3(); }
3277
3278   // Return the special symbol of the relocation.
3279   static inline unsigned int
3280   get_r_ssym(const Reltype* reloc)
3281   { return reloc->get_r_ssym(); }
3282
3283   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3284   static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3285   get_r_addend(const Reltype* reloc)
3286   {
3287     if (sh_type_ == elfcpp::SHT_REL)
3288       return 0;
3289     return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3290   }
3291
3292   // Write the r_info field to a new reloc, using the r_info field from
3293   // the original reloc, replacing the r_sym field with R_SYM.
3294   static inline void
3295   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3296   {
3297     new_reloc->put_r_sym(r_sym);
3298     new_reloc->put_r_ssym(reloc->get_r_ssym());
3299     new_reloc->put_r_type3(reloc->get_r_type3());
3300     new_reloc->put_r_type2(reloc->get_r_type2());
3301     new_reloc->put_r_type(reloc->get_r_type());
3302   }
3303
3304   // Write the r_addend field to a new reloc.
3305   static inline void
3306   put_r_addend(Reltype_write* to,
3307                typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3308   { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3309
3310   // Return the size of the addend of the relocation (only used for SHT_REL).
3311   static unsigned int
3312   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3313   { return mips_get_size_for_reloc(r_type, obj); }
3314 };
3315
3316 template<int size, bool big_endian>
3317 class Target_mips : public Sized_target<size, big_endian>
3318 {
3319   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3320   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3321     Reloc_section;
3322   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3323   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3324   typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3325       Reltype;
3326   typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3327       Relatype;
3328
3329  public:
3330   Target_mips(const Target::Target_info* info = &mips_info)
3331     : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3332       got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3333       dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3334       mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3335       mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
3336       entry_symbol_is_compressed_(false), insn32_(false)
3337   {
3338     this->add_machine_extensions();
3339   }
3340
3341   // The offset of $gp from the beginning of the .got section.
3342   static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3343
3344   // The maximum size of the GOT for it to be addressable using 16-bit
3345   // offsets from $gp.
3346   static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3347
3348   // Make a new symbol table entry for the Mips target.
3349   Sized_symbol<size>*
3350   make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3351   { return new Mips_symbol<size>(); }
3352
3353   // Process the relocations to determine unreferenced sections for
3354   // garbage collection.
3355   void
3356   gc_process_relocs(Symbol_table* symtab,
3357                     Layout* layout,
3358                     Sized_relobj_file<size, big_endian>* object,
3359                     unsigned int data_shndx,
3360                     unsigned int sh_type,
3361                     const unsigned char* prelocs,
3362                     size_t reloc_count,
3363                     Output_section* output_section,
3364                     bool needs_special_offset_handling,
3365                     size_t local_symbol_count,
3366                     const unsigned char* plocal_symbols);
3367
3368   // Scan the relocations to look for symbol adjustments.
3369   void
3370   scan_relocs(Symbol_table* symtab,
3371               Layout* layout,
3372               Sized_relobj_file<size, big_endian>* object,
3373               unsigned int data_shndx,
3374               unsigned int sh_type,
3375               const unsigned char* prelocs,
3376               size_t reloc_count,
3377               Output_section* output_section,
3378               bool needs_special_offset_handling,
3379               size_t local_symbol_count,
3380               const unsigned char* plocal_symbols);
3381
3382   // Finalize the sections.
3383   void
3384   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3385
3386   // Relocate a section.
3387   void
3388   relocate_section(const Relocate_info<size, big_endian>*,
3389                    unsigned int sh_type,
3390                    const unsigned char* prelocs,
3391                    size_t reloc_count,
3392                    Output_section* output_section,
3393                    bool needs_special_offset_handling,
3394                    unsigned char* view,
3395                    Mips_address view_address,
3396                    section_size_type view_size,
3397                    const Reloc_symbol_changes*);
3398
3399   // Scan the relocs during a relocatable link.
3400   void
3401   scan_relocatable_relocs(Symbol_table* symtab,
3402                           Layout* layout,
3403                           Sized_relobj_file<size, big_endian>* object,
3404                           unsigned int data_shndx,
3405                           unsigned int sh_type,
3406                           const unsigned char* prelocs,
3407                           size_t reloc_count,
3408                           Output_section* output_section,
3409                           bool needs_special_offset_handling,
3410                           size_t local_symbol_count,
3411                           const unsigned char* plocal_symbols,
3412                           Relocatable_relocs*);
3413
3414   // Scan the relocs for --emit-relocs.
3415   void
3416   emit_relocs_scan(Symbol_table* symtab,
3417                    Layout* layout,
3418                    Sized_relobj_file<size, big_endian>* object,
3419                    unsigned int data_shndx,
3420                    unsigned int sh_type,
3421                    const unsigned char* prelocs,
3422                    size_t reloc_count,
3423                    Output_section* output_section,
3424                    bool needs_special_offset_handling,
3425                    size_t local_symbol_count,
3426                    const unsigned char* plocal_syms,
3427                    Relocatable_relocs* rr);
3428
3429   // Emit relocations for a section.
3430   void
3431   relocate_relocs(const Relocate_info<size, big_endian>*,
3432                   unsigned int sh_type,
3433                   const unsigned char* prelocs,
3434                   size_t reloc_count,
3435                   Output_section* output_section,
3436                   typename elfcpp::Elf_types<size>::Elf_Off
3437                     offset_in_output_section,
3438                   unsigned char* view,
3439                   Mips_address view_address,
3440                   section_size_type view_size,
3441                   unsigned char* reloc_view,
3442                   section_size_type reloc_view_size);
3443
3444   // Perform target-specific processing in a relocatable link.  This is
3445   // only used if we use the relocation strategy RELOC_SPECIAL.
3446   void
3447   relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3448                                unsigned int sh_type,
3449                                const unsigned char* preloc_in,
3450                                size_t relnum,
3451                                Output_section* output_section,
3452                                typename elfcpp::Elf_types<size>::Elf_Off
3453                                  offset_in_output_section,
3454                                unsigned char* view,
3455                                Mips_address view_address,
3456                                section_size_type view_size,
3457                                unsigned char* preloc_out);
3458
3459   // Return whether SYM is defined by the ABI.
3460   bool
3461   do_is_defined_by_abi(const Symbol* sym) const
3462   {
3463     return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3464             || (strcmp(sym->name(), "_gp_disp") == 0)
3465             || (strcmp(sym->name(), "___tls_get_addr") == 0));
3466   }
3467
3468   // Return the number of entries in the GOT.
3469   unsigned int
3470   got_entry_count() const
3471   {
3472     if (!this->has_got_section())
3473       return 0;
3474     return this->got_size() / (size/8);
3475   }
3476
3477   // Return the number of entries in the PLT.
3478   unsigned int
3479   plt_entry_count() const
3480   {
3481     if (this->plt_ == NULL)
3482       return 0;
3483     return this->plt_->entry_count();
3484   }
3485
3486   // Return the offset of the first non-reserved PLT entry.
3487   unsigned int
3488   first_plt_entry_offset() const
3489   { return this->plt_->first_plt_entry_offset(); }
3490
3491   // Return the size of each PLT entry.
3492   unsigned int
3493   plt_entry_size() const
3494   { return this->plt_->plt_entry_size(); }
3495
3496   // Get the GOT section, creating it if necessary.
3497   Mips_output_data_got<size, big_endian>*
3498   got_section(Symbol_table*, Layout*);
3499
3500   // Get the GOT section.
3501   Mips_output_data_got<size, big_endian>*
3502   got_section() const
3503   {
3504     gold_assert(this->got_ != NULL);
3505     return this->got_;
3506   }
3507
3508   // Get the .MIPS.stubs section, creating it if necessary.
3509   Mips_output_data_mips_stubs<size, big_endian>*
3510   mips_stubs_section(Layout* layout);
3511
3512   // Get the .MIPS.stubs section.
3513   Mips_output_data_mips_stubs<size, big_endian>*
3514   mips_stubs_section() const
3515   {
3516     gold_assert(this->mips_stubs_ != NULL);
3517     return this->mips_stubs_;
3518   }
3519
3520   // Get the LA25 stub section, creating it if necessary.
3521   Mips_output_data_la25_stub<size, big_endian>*
3522   la25_stub_section(Layout*);
3523
3524   // Get the LA25 stub section.
3525   Mips_output_data_la25_stub<size, big_endian>*
3526   la25_stub_section()
3527   {
3528     gold_assert(this->la25_stub_ != NULL);
3529     return this->la25_stub_;
3530   }
3531
3532   // Get gp value.  It has the value of .got + 0x7FF0.
3533   Mips_address
3534   gp_value() const
3535   {
3536     if (this->gp_ != NULL)
3537       return this->gp_->value();
3538     return 0;
3539   }
3540
3541   // Get gp value.  It has the value of .got + 0x7FF0.  Adjust it for
3542   // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3543   Mips_address
3544   adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3545   {
3546     if (this->gp_ == NULL)
3547       return 0;
3548
3549     bool multi_got = false;
3550     if (this->has_got_section())
3551       multi_got = this->got_section()->multi_got();
3552     if (!multi_got)
3553       return this->gp_->value();
3554     else
3555       return this->gp_->value() + this->got_section()->get_got_offset(object);
3556   }
3557
3558   // Get the dynamic reloc section, creating it if necessary.
3559   Reloc_section*
3560   rel_dyn_section(Layout*);
3561
3562   bool
3563   do_has_custom_set_dynsym_indexes() const
3564   { return true; }
3565
3566   // Don't emit input .reginfo/.MIPS.abiflags sections to
3567   // output .reginfo/.MIPS.abiflags.
3568   bool
3569   do_should_include_section(elfcpp::Elf_Word sh_type) const
3570   {
3571     return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3572              && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3573   }
3574
3575   // Set the dynamic symbol indexes.  INDEX is the index of the first
3576   // global dynamic symbol.  Pointers to the symbols are stored into the
3577   // vector SYMS.  The names are added to DYNPOOL.  This returns an
3578   // updated dynamic symbol index.
3579   unsigned int
3580   do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3581                         std::vector<Symbol*>* syms, Stringpool* dynpool,
3582                         Versions* versions, Symbol_table* symtab) const;
3583
3584   // Remove .MIPS.stubs entry for a symbol.
3585   void
3586   remove_lazy_stub_entry(Mips_symbol<size>* sym)
3587   {
3588     if (this->mips_stubs_ != NULL)
3589       this->mips_stubs_->remove_entry(sym);
3590   }
3591
3592   // The value to write into got[1] for SVR4 targets, to identify it is
3593   // a GNU object.  The dynamic linker can then use got[1] to store the
3594   // module pointer.
3595   uint64_t
3596   mips_elf_gnu_got1_mask()
3597   {
3598     if (this->is_output_n64())
3599       return (uint64_t)1 << 63;
3600     else
3601       return 1 << 31;
3602   }
3603
3604   // Whether the output has microMIPS code.  This is valid only after
3605   // merge_obj_e_flags() is called.
3606   bool
3607   is_output_micromips() const
3608   {
3609     gold_assert(this->are_processor_specific_flags_set());
3610     return elfcpp::is_micromips(this->processor_specific_flags());
3611   }
3612
3613   // Whether the output uses N32 ABI.  This is valid only after
3614   // merge_obj_e_flags() is called.
3615   bool
3616   is_output_n32() const
3617   {
3618     gold_assert(this->are_processor_specific_flags_set());
3619     return elfcpp::abi_n32(this->processor_specific_flags());
3620   }
3621
3622   // Whether the output uses R6 ISA.  This is valid only after
3623   // merge_obj_e_flags() is called.
3624   bool
3625   is_output_r6() const
3626   {
3627     gold_assert(this->are_processor_specific_flags_set());
3628     return elfcpp::r6_isa(this->processor_specific_flags());
3629   }
3630
3631   // Whether the output uses N64 ABI.
3632   bool
3633   is_output_n64() const
3634   { return size == 64; }
3635
3636   // Whether the output uses NEWABI.  This is valid only after
3637   // merge_obj_e_flags() is called.
3638   bool
3639   is_output_newabi() const
3640   { return this->is_output_n32() || this->is_output_n64(); }
3641
3642   // Whether we can only use 32-bit microMIPS instructions.
3643   bool
3644   use_32bit_micromips_instructions() const
3645   { return this->insn32_; }
3646
3647   // Return the r_sym field from a relocation.
3648   unsigned int
3649   get_r_sym(const unsigned char* preloc) const
3650   {
3651     // Since REL and RELA relocs share the same structure through
3652     // the r_info field, we can just use REL here.
3653     Reltype rel(preloc);
3654     return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3655         get_r_sym(&rel);
3656   }
3657
3658  protected:
3659   // Return the value to use for a dynamic symbol which requires special
3660   // treatment.  This is how we support equality comparisons of function
3661   // pointers across shared library boundaries, as described in the
3662   // processor specific ABI supplement.
3663   uint64_t
3664   do_dynsym_value(const Symbol* gsym) const;
3665
3666   // Make an ELF object.
3667   Object*
3668   do_make_elf_object(const std::string&, Input_file*, off_t,
3669                      const elfcpp::Ehdr<size, big_endian>& ehdr);
3670
3671   Object*
3672   do_make_elf_object(const std::string&, Input_file*, off_t,
3673                      const elfcpp::Ehdr<size, !big_endian>&)
3674   { gold_unreachable(); }
3675
3676   // Make an output section.
3677   Output_section*
3678   do_make_output_section(const char* name, elfcpp::Elf_Word type,
3679                          elfcpp::Elf_Xword flags)
3680     {
3681       if (type == elfcpp::SHT_MIPS_OPTIONS)
3682         return new Mips_output_section_options<size, big_endian>(name, type,
3683                                                                  flags, this);
3684       else
3685         return new Output_section(name, type, flags);
3686     }
3687
3688   // Adjust ELF file header.
3689   void
3690   do_adjust_elf_header(unsigned char* view, int len);
3691
3692   // Get the custom dynamic tag value.
3693   unsigned int
3694   do_dynamic_tag_custom_value(elfcpp::DT) const;
3695
3696   // Adjust the value written to the dynamic symbol table.
3697   virtual void
3698   do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3699   {
3700     elfcpp::Sym<size, big_endian> isym(view);
3701     elfcpp::Sym_write<size, big_endian> osym(view);
3702     const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3703
3704     // Keep dynamic compressed symbols odd.  This allows the dynamic linker
3705     // to treat compressed symbols like any other.
3706     Mips_address value = isym.get_st_value();
3707     if (mips_sym->is_mips16() && value != 0)
3708       {
3709         if (!mips_sym->has_mips16_fn_stub())
3710           value |= 1;
3711         else
3712           {
3713             // If we have a MIPS16 function with a stub, the dynamic symbol
3714             // must refer to the stub, since only the stub uses the standard
3715             // calling conventions.  Stub contains MIPS32 code, so don't add +1
3716             // in this case.
3717
3718             // There is a code which does this in the method
3719             // Target_mips::do_dynsym_value, but that code will only be
3720             // executed if the symbol is from dynobj.
3721             // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3722             // table.
3723
3724             Mips16_stub_section<size, big_endian>* fn_stub =
3725               mips_sym->template get_mips16_fn_stub<big_endian>();
3726             value = fn_stub->output_address();
3727             osym.put_st_size(fn_stub->section_size());
3728           }
3729
3730         osym.put_st_value(value);
3731         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3732                           mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3733       }
3734     else if ((mips_sym->is_micromips()
3735               // Stubs are always microMIPS if there is any microMIPS code in
3736               // the output.
3737               || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3738              && value != 0)
3739       {
3740         osym.put_st_value(value | 1);
3741         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3742                           mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3743       }
3744   }
3745
3746  private:
3747   // The class which scans relocations.
3748   class Scan
3749   {
3750    public:
3751     Scan()
3752     { }
3753
3754     static inline int
3755     get_reference_flags(unsigned int r_type);
3756
3757     inline void
3758     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3759           Sized_relobj_file<size, big_endian>* object,
3760           unsigned int data_shndx,
3761           Output_section* output_section,
3762           const Reltype& reloc, unsigned int r_type,
3763           const elfcpp::Sym<size, big_endian>& lsym,
3764           bool is_discarded);
3765
3766     inline void
3767     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3768           Sized_relobj_file<size, big_endian>* object,
3769           unsigned int data_shndx,
3770           Output_section* output_section,
3771           const Relatype& reloc, unsigned int r_type,
3772           const elfcpp::Sym<size, big_endian>& lsym,
3773           bool is_discarded);
3774
3775     inline void
3776     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3777           Sized_relobj_file<size, big_endian>* object,
3778           unsigned int data_shndx,
3779           Output_section* output_section,
3780           const Relatype* rela,
3781           const Reltype* rel,
3782           unsigned int rel_type,
3783           unsigned int r_type,
3784           const elfcpp::Sym<size, big_endian>& lsym,
3785           bool is_discarded);
3786
3787     inline void
3788     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3789            Sized_relobj_file<size, big_endian>* object,
3790            unsigned int data_shndx,
3791            Output_section* output_section,
3792            const Reltype& reloc, unsigned int r_type,
3793            Symbol* gsym);
3794
3795     inline void
3796     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3797            Sized_relobj_file<size, big_endian>* object,
3798            unsigned int data_shndx,
3799            Output_section* output_section,
3800            const Relatype& reloc, unsigned int r_type,
3801            Symbol* gsym);
3802
3803     inline void
3804     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3805            Sized_relobj_file<size, big_endian>* object,
3806            unsigned int data_shndx,
3807            Output_section* output_section,
3808            const Relatype* rela,
3809            const Reltype* rel,
3810            unsigned int rel_type,
3811            unsigned int r_type,
3812            Symbol* gsym);
3813
3814     inline bool
3815     local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3816                                         Target_mips*,
3817                                         Sized_relobj_file<size, big_endian>*,
3818                                         unsigned int,
3819                                         Output_section*,
3820                                         const Reltype&,
3821                                         unsigned int,
3822                                         const elfcpp::Sym<size, big_endian>&)
3823     { return false; }
3824
3825     inline bool
3826     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3827                                          Target_mips*,
3828                                          Sized_relobj_file<size, big_endian>*,
3829                                          unsigned int,
3830                                          Output_section*,
3831                                          const Reltype&,
3832                                          unsigned int, Symbol*)
3833     { return false; }
3834
3835     inline bool
3836     local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3837                                         Target_mips*,
3838                                         Sized_relobj_file<size, big_endian>*,
3839                                         unsigned int,
3840                                         Output_section*,
3841                                         const Relatype&,
3842                                         unsigned int,
3843                                         const elfcpp::Sym<size, big_endian>&)
3844     { return false; }
3845
3846     inline bool
3847     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3848                                          Target_mips*,
3849                                          Sized_relobj_file<size, big_endian>*,
3850                                          unsigned int,
3851                                          Output_section*,
3852                                          const Relatype&,
3853                                          unsigned int, Symbol*)
3854     { return false; }
3855    private:
3856     static void
3857     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3858                             unsigned int r_type);
3859
3860     static void
3861     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3862                              unsigned int r_type, Symbol*);
3863   };
3864
3865   // The class which implements relocation.
3866   class Relocate
3867   {
3868    public:
3869     Relocate()
3870       : calculated_value_(0), calculate_only_(false)
3871     { }
3872
3873     ~Relocate()
3874     { }
3875
3876     // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3877     inline bool
3878     should_apply_static_reloc(const Mips_symbol<size>* gsym,
3879                               unsigned int r_type,
3880                               Output_section* output_section,
3881                               Target_mips* target);
3882
3883     // Do a relocation.  Return false if the caller should not issue
3884     // any warnings about this relocation.
3885     inline bool
3886     relocate(const Relocate_info<size, big_endian>*, unsigned int,
3887              Target_mips*, Output_section*, size_t, const unsigned char*,
3888              const Sized_symbol<size>*, const Symbol_value<size>*,
3889              unsigned char*, Mips_address, section_size_type);
3890
3891    private:
3892     // Result of the relocation.
3893     Valtype calculated_value_;
3894     // Whether we have to calculate relocation instead of applying it.
3895     bool calculate_only_;
3896   };
3897
3898   // This POD class holds the dynamic relocations that should be emitted instead
3899   // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations.  We will emit these
3900   // relocations if it turns out that the symbol does not have static
3901   // relocations.
3902   class Dyn_reloc
3903   {
3904    public:
3905     Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3906               Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3907               Output_section* output_section, Mips_address r_offset)
3908       : sym_(sym), r_type_(r_type), relobj_(relobj),
3909         shndx_(shndx), output_section_(output_section),
3910         r_offset_(r_offset)
3911     { }
3912
3913     // Emit this reloc if appropriate.  This is called after we have
3914     // scanned all the relocations, so we know whether the symbol has
3915     // static relocations.
3916     void
3917     emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3918          Symbol_table* symtab)
3919     {
3920       if (!this->sym_->has_static_relocs())
3921         {
3922           got->record_global_got_symbol(this->sym_, this->relobj_,
3923                                         this->r_type_, true, false);
3924           if (!symbol_references_local(this->sym_,
3925                                 this->sym_->should_add_dynsym_entry(symtab)))
3926             rel_dyn->add_global(this->sym_, this->r_type_,
3927                                 this->output_section_, this->relobj_,
3928                                 this->shndx_, this->r_offset_);
3929           else
3930             rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3931                                           this->output_section_, this->relobj_,
3932                                           this->shndx_, this->r_offset_);
3933         }
3934     }
3935
3936    private:
3937     Mips_symbol<size>* sym_;
3938     unsigned int r_type_;
3939     Mips_relobj<size, big_endian>* relobj_;
3940     unsigned int shndx_;
3941     Output_section* output_section_;
3942     Mips_address r_offset_;
3943   };
3944
3945   // Adjust TLS relocation type based on the options and whether this
3946   // is a local symbol.
3947   static tls::Tls_optimization
3948   optimize_tls_reloc(bool is_final, int r_type);
3949
3950   // Return whether there is a GOT section.
3951   bool
3952   has_got_section() const
3953   { return this->got_ != NULL; }
3954
3955   // Check whether the given ELF header flags describe a 32-bit binary.
3956   bool
3957   mips_32bit_flags(elfcpp::Elf_Word);
3958
3959   enum Mips_mach {
3960     mach_mips3000             = 3000,
3961     mach_mips3900             = 3900,
3962     mach_mips4000             = 4000,
3963     mach_mips4010             = 4010,
3964     mach_mips4100             = 4100,
3965     mach_mips4111             = 4111,
3966     mach_mips4120             = 4120,
3967     mach_mips4300             = 4300,
3968     mach_mips4400             = 4400,
3969     mach_mips4600             = 4600,
3970     mach_mips4650             = 4650,
3971     mach_mips5000             = 5000,
3972     mach_mips5400             = 5400,
3973     mach_mips5500             = 5500,
3974     mach_mips5900             = 5900,
3975     mach_mips6000             = 6000,
3976     mach_mips7000             = 7000,
3977     mach_mips8000             = 8000,
3978     mach_mips9000             = 9000,
3979     mach_mips10000            = 10000,
3980     mach_mips12000            = 12000,
3981     mach_mips14000            = 14000,
3982     mach_mips16000            = 16000,
3983     mach_mips16               = 16,
3984     mach_mips5                = 5,
3985     mach_mips_loongson_2e     = 3001,
3986     mach_mips_loongson_2f     = 3002,
3987     mach_mips_loongson_3a     = 3003,
3988     mach_mips_sb1             = 12310201, // octal 'SB', 01
3989     mach_mips_octeon          = 6501,
3990     mach_mips_octeonp         = 6601,
3991     mach_mips_octeon2         = 6502,
3992     mach_mips_octeon3         = 6503,
3993     mach_mips_xlr             = 887682,   // decimal 'XLR'
3994     mach_mipsisa32            = 32,
3995     mach_mipsisa32r2          = 33,
3996     mach_mipsisa32r3          = 34,
3997     mach_mipsisa32r5          = 36,
3998     mach_mipsisa32r6          = 37,
3999     mach_mipsisa64            = 64,
4000     mach_mipsisa64r2          = 65,
4001     mach_mipsisa64r3          = 66,
4002     mach_mipsisa64r5          = 68,
4003     mach_mipsisa64r6          = 69,
4004     mach_mips_micromips       = 96
4005   };
4006
4007   // Return the MACH for a MIPS e_flags value.
4008   unsigned int
4009   elf_mips_mach(elfcpp::Elf_Word);
4010
4011   // Return the MACH for each .MIPS.abiflags ISA Extension.
4012   unsigned int
4013   mips_isa_ext_mach(unsigned int);
4014
4015   // Return the .MIPS.abiflags value representing each ISA Extension.
4016   unsigned int
4017   mips_isa_ext(unsigned int);
4018
4019   // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4020   void
4021   update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4022                       Mips_abiflags<big_endian>*);
4023
4024   // Infer the content of the ABI flags based on the elf header.
4025   void
4026   infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4027
4028   // Create abiflags from elf header or from .MIPS.abiflags section.
4029   void
4030   create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4031
4032   // Return the meaning of fp_abi, or "unknown" if not known.
4033   const char*
4034   fp_abi_string(int);
4035
4036   // Select fp_abi.
4037   int
4038   select_fp_abi(const std::string&, int, int);
4039
4040   // Merge attributes from input object.
4041   void
4042   merge_obj_attributes(const std::string&, const Attributes_section_data*);
4043
4044   // Merge abiflags from input object.
4045   void
4046   merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4047
4048   // Check whether machine EXTENSION is an extension of machine BASE.
4049   bool
4050   mips_mach_extends(unsigned int, unsigned int);
4051
4052   // Merge file header flags from input object.
4053   void
4054   merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4055
4056   // Encode ISA level and revision as a single value.
4057   int
4058   level_rev(unsigned char isa_level, unsigned char isa_rev) const
4059   { return (isa_level << 3) | isa_rev; }
4060
4061   // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4062   static inline bool
4063   jal_to_bal()
4064   { return false; }
4065
4066   // True if we are linking for CPUs that are faster if JALR is converted to
4067   // BAL.  This should be safe for all architectures.  We enable this predicate
4068   // for all CPUs.
4069   static inline bool
4070   jalr_to_bal()
4071   { return true; }
4072
4073   // True if we are linking for CPUs that are faster if JR is converted to B.
4074   // This should be safe for all architectures.  We enable this predicate for
4075   // all CPUs.
4076   static inline bool
4077   jr_to_b()
4078   { return true; }
4079
4080   // Return the size of the GOT section.
4081   section_size_type
4082   got_size() const
4083   {
4084     gold_assert(this->got_ != NULL);
4085     return this->got_->data_size();
4086   }
4087
4088   // Create a PLT entry for a global symbol referenced by r_type relocation.
4089   void
4090   make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4091                  unsigned int r_type);
4092
4093   // Get the PLT section.
4094   Mips_output_data_plt<size, big_endian>*
4095   plt_section() const
4096   {
4097     gold_assert(this->plt_ != NULL);
4098     return this->plt_;
4099   }
4100
4101   // Get the GOT PLT section.
4102   const Mips_output_data_plt<size, big_endian>*
4103   got_plt_section() const
4104   {
4105     gold_assert(this->got_plt_ != NULL);
4106     return this->got_plt_;
4107   }
4108
4109   // Copy a relocation against a global symbol.
4110   void
4111   copy_reloc(Symbol_table* symtab, Layout* layout,
4112              Sized_relobj_file<size, big_endian>* object,
4113              unsigned int shndx, Output_section* output_section,
4114              Symbol* sym, unsigned int r_type, Mips_address r_offset)
4115   {
4116     this->copy_relocs_.copy_reloc(symtab, layout,
4117                                   symtab->get_sized_symbol<size>(sym),
4118                                   object, shndx, output_section,
4119                                   r_type, r_offset, 0,
4120                                   this->rel_dyn_section(layout));
4121   }
4122
4123   void
4124   dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4125                 Mips_relobj<size, big_endian>* relobj,
4126                 unsigned int shndx, Output_section* output_section,
4127                 Mips_address r_offset)
4128   {
4129     this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4130                                           output_section, r_offset));
4131   }
4132
4133   // Calculate value of _gp symbol.
4134   void
4135   set_gp(Layout*, Symbol_table*);
4136
4137   const char*
4138   elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4139   const char*
4140   elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4141
4142   // Adds entries that describe how machines relate to one another.  The entries
4143   // are ordered topologically with MIPS I extensions listed last.  First
4144   // element is extension, second element is base.
4145   void
4146   add_machine_extensions()
4147   {
4148     // MIPS64r2 extensions.
4149     this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4150     this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4151     this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4152     this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4153     this->add_extension(mach_mips_loongson_3a, 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   // Add the same entry to the OBJECT's GOT.
5798   Got_page_entry* entry2 = new Got_page_entry(*entry);
5799   Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5800   typename Got_page_entry_set::iterator it2 =
5801     g2->got_page_entries_.find(entry);
5802   if (it2 != g2->got_page_entries_.end())
5803     entry2 = *it2;
5804   else
5805     g2->got_page_entries_.insert(entry2);
5806
5807   // Skip over ranges whose maximum extent cannot share a page entry
5808   // with ADDEND.
5809   range_ptr = &entry->ranges;
5810   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5811     range_ptr = &(*range_ptr)->next;
5812
5813   // If we scanned to the end of the list, or found a range whose
5814   // minimum extent cannot share a page entry with ADDEND, create
5815   // a new singleton range.
5816   range = *range_ptr;
5817   if (!range || addend < range->min_addend - 0xffff)
5818     {
5819       range = new Got_page_range();
5820       range->next = *range_ptr;
5821       range->min_addend = addend;
5822       range->max_addend = addend;
5823
5824       *range_ptr = range;
5825       ++entry->num_pages;
5826       ++entry2->num_pages;
5827       ++this->page_gotno_;
5828       ++g2->page_gotno_;
5829       return;
5830     }
5831
5832   // Remember how many pages the old range contributed.
5833   old_pages = range->get_max_pages();
5834
5835   // Update the ranges.
5836   if (addend < range->min_addend)
5837     range->min_addend = addend;
5838   else if (addend > range->max_addend)
5839     {
5840       if (range->next && addend >= range->next->min_addend - 0xffff)
5841         {
5842           old_pages += range->next->get_max_pages();
5843           range->max_addend = range->next->max_addend;
5844           range->next = range->next->next;
5845         }
5846       else
5847         range->max_addend = addend;
5848     }
5849
5850   // Record any change in the total estimate.
5851   new_pages = range->get_max_pages();
5852   if (old_pages != new_pages)
5853     {
5854       entry->num_pages += new_pages - old_pages;
5855       entry2->num_pages += new_pages - old_pages;
5856       this->page_gotno_ += new_pages - old_pages;
5857       g2->page_gotno_ += new_pages - old_pages;
5858     }
5859 }
5860
5861 // Create all entries that should be in the local part of the GOT.
5862
5863 template<int size, bool big_endian>
5864 void
5865 Mips_got_info<size, big_endian>::add_local_entries(
5866     Target_mips<size, big_endian>* target, Layout* layout)
5867 {
5868   Mips_output_data_got<size, big_endian>* got = target->got_section();
5869   // First two GOT entries are reserved.  The first entry will be filled at
5870   // runtime.  The second entry will be used by some runtime loaders.
5871   got->add_constant(0);
5872   got->add_constant(target->mips_elf_gnu_got1_mask());
5873
5874   for (typename Got_entry_set::iterator
5875        p = this->got_entries_.begin();
5876        p != this->got_entries_.end();
5877        ++p)
5878     {
5879       Mips_got_entry<size, big_endian>* entry = *p;
5880       if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5881         {
5882           got->add_local(entry->object(), entry->symndx(),
5883                          GOT_TYPE_STANDARD, entry->addend());
5884           unsigned int got_offset = entry->object()->local_got_offset(
5885               entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5886           if (got->multi_got() && this->index_ > 0
5887               && parameters->options().output_is_position_independent())
5888           {
5889             if (!entry->is_section_symbol())
5890               target->rel_dyn_section(layout)->add_local(entry->object(),
5891                   entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5892             else
5893               target->rel_dyn_section(layout)->add_symbolless_local_addend(
5894                   entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5895                   got, got_offset);
5896           }
5897         }
5898     }
5899
5900   this->add_page_entries(target, layout);
5901
5902   // Add global entries that should be in the local area.
5903   for (typename Got_entry_set::iterator
5904        p = this->got_entries_.begin();
5905        p != this->got_entries_.end();
5906        ++p)
5907     {
5908       Mips_got_entry<size, big_endian>* entry = *p;
5909       if (!entry->is_for_global_symbol())
5910         continue;
5911
5912       Mips_symbol<size>* mips_sym = entry->sym();
5913       if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5914         {
5915           unsigned int got_type;
5916           if (!got->multi_got())
5917             got_type = GOT_TYPE_STANDARD;
5918           else
5919             got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5920           if (got->add_global(mips_sym, got_type))
5921             {
5922               mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5923               if (got->multi_got() && this->index_ > 0
5924                   && parameters->options().output_is_position_independent())
5925                 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5926                     mips_sym, elfcpp::R_MIPS_REL32, got,
5927                     mips_sym->got_offset(got_type));
5928             }
5929         }
5930     }
5931 }
5932
5933 // Create GOT page entries.
5934
5935 template<int size, bool big_endian>
5936 void
5937 Mips_got_info<size, big_endian>::add_page_entries(
5938     Target_mips<size, big_endian>* target, Layout* layout)
5939 {
5940   if (this->page_gotno_ == 0)
5941     return;
5942
5943   Mips_output_data_got<size, big_endian>* got = target->got_section();
5944   this->got_page_offset_start_ = got->add_constant(0);
5945   if (got->multi_got() && this->index_ > 0
5946       && parameters->options().output_is_position_independent())
5947     target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5948                                                   this->got_page_offset_start_);
5949   int num_entries = this->page_gotno_;
5950   unsigned int prev_offset = this->got_page_offset_start_;
5951   while (--num_entries > 0)
5952     {
5953       unsigned int next_offset = got->add_constant(0);
5954       if (got->multi_got() && this->index_ > 0
5955           && parameters->options().output_is_position_independent())
5956         target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5957                                                       next_offset);
5958       gold_assert(next_offset == prev_offset + size/8);
5959       prev_offset = next_offset;
5960     }
5961   this->got_page_offset_next_ = this->got_page_offset_start_;
5962 }
5963
5964 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5965
5966 template<int size, bool big_endian>
5967 void
5968 Mips_got_info<size, big_endian>::add_global_entries(
5969     Target_mips<size, big_endian>* target, Layout* layout,
5970     unsigned int non_reloc_only_global_gotno)
5971 {
5972   Mips_output_data_got<size, big_endian>* got = target->got_section();
5973   // Add GGA_NORMAL entries.
5974   unsigned int count = 0;
5975   for (typename Got_entry_set::iterator
5976        p = this->got_entries_.begin();
5977        p != this->got_entries_.end();
5978        ++p)
5979     {
5980       Mips_got_entry<size, big_endian>* entry = *p;
5981       if (!entry->is_for_global_symbol())
5982         continue;
5983
5984       Mips_symbol<size>* mips_sym = entry->sym();
5985       if (mips_sym->global_got_area() != GGA_NORMAL)
5986         continue;
5987
5988       unsigned int got_type;
5989       if (!got->multi_got())
5990         got_type = GOT_TYPE_STANDARD;
5991       else
5992         // In multi-GOT links, global symbol can be in both primary and
5993         // secondary GOT(s).  By creating custom GOT type
5994         // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5995         // is added to secondary GOT(s).
5996         got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5997       if (!got->add_global(mips_sym, got_type))
5998         continue;
5999
6000       mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6001       if (got->multi_got() && this->index_ == 0)
6002         count++;
6003       if (got->multi_got() && this->index_ > 0)
6004         {
6005           if (parameters->options().output_is_position_independent()
6006               || (!parameters->doing_static_link()
6007                   && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
6008             {
6009               target->rel_dyn_section(layout)->add_global(
6010                   mips_sym, elfcpp::R_MIPS_REL32, got,
6011                   mips_sym->got_offset(got_type));
6012               got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
6013                                            elfcpp::R_MIPS_REL32, mips_sym);
6014             }
6015         }
6016     }
6017
6018   if (!got->multi_got() || this->index_ == 0)
6019     {
6020       if (got->multi_got())
6021         {
6022           // We need to allocate space in the primary GOT for GGA_NORMAL entries
6023           // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
6024           // entries correspond to dynamic symbol indexes.
6025           while (count < non_reloc_only_global_gotno)
6026             {
6027               got->add_constant(0);
6028               ++count;
6029             }
6030         }
6031
6032       // Add GGA_RELOC_ONLY entries.
6033       got->add_reloc_only_entries();
6034     }
6035 }
6036
6037 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6038
6039 template<int size, bool big_endian>
6040 void
6041 Mips_got_info<size, big_endian>::add_reloc_only_entries(
6042     Mips_output_data_got<size, big_endian>* got)
6043 {
6044   for (typename Global_got_entry_set::iterator
6045        p = this->global_got_symbols_.begin();
6046        p != this->global_got_symbols_.end();
6047        ++p)
6048     {
6049       Mips_symbol<size>* mips_sym = *p;
6050       if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6051         {
6052           unsigned int got_type;
6053           if (!got->multi_got())
6054             got_type = GOT_TYPE_STANDARD;
6055           else
6056             got_type = GOT_TYPE_STANDARD_MULTIGOT;
6057           if (got->add_global(mips_sym, got_type))
6058             mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6059         }
6060     }
6061 }
6062
6063 // Create TLS GOT entries.
6064
6065 template<int size, bool big_endian>
6066 void
6067 Mips_got_info<size, big_endian>::add_tls_entries(
6068     Target_mips<size, big_endian>* target, Layout* layout)
6069 {
6070   Mips_output_data_got<size, big_endian>* got = target->got_section();
6071   // Add local tls entries.
6072   for (typename Got_entry_set::iterator
6073        p = this->got_entries_.begin();
6074        p != this->got_entries_.end();
6075        ++p)
6076     {
6077       Mips_got_entry<size, big_endian>* entry = *p;
6078       if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6079         continue;
6080
6081       if (entry->tls_type() == GOT_TLS_GD)
6082         {
6083           unsigned int got_type = GOT_TYPE_TLS_PAIR;
6084           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6085                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6086           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6087                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6088
6089           if (!parameters->doing_static_link())
6090             {
6091               got->add_local_pair_with_rel(entry->object(), entry->symndx(),
6092                                            entry->shndx(), got_type,
6093                                            target->rel_dyn_section(layout),
6094                                            r_type1, entry->addend());
6095               unsigned int got_offset =
6096                 entry->object()->local_got_offset(entry->symndx(), got_type,
6097                                                   entry->addend());
6098               got->add_static_reloc(got_offset + size/8, r_type2,
6099                                     entry->object(), entry->symndx());
6100             }
6101           else
6102             {
6103               // We are doing a static link.  Mark it as belong to module 1,
6104               // the executable.
6105               unsigned int got_offset = got->add_constant(1);
6106               entry->object()->set_local_got_offset(entry->symndx(), got_type,
6107                                                     got_offset,
6108                                                     entry->addend());
6109               got->add_constant(0);
6110               got->add_static_reloc(got_offset + size/8, r_type2,
6111                                     entry->object(), entry->symndx());
6112             }
6113         }
6114       else if (entry->tls_type() == GOT_TLS_IE)
6115         {
6116           unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6117           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6118                                             : elfcpp::R_MIPS_TLS_TPREL64);
6119           if (!parameters->doing_static_link())
6120             got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
6121                                     target->rel_dyn_section(layout), r_type,
6122                                     entry->addend());
6123           else
6124             {
6125               got->add_local(entry->object(), entry->symndx(), got_type,
6126                              entry->addend());
6127               unsigned int got_offset =
6128                   entry->object()->local_got_offset(entry->symndx(), got_type,
6129                                                     entry->addend());
6130               got->add_static_reloc(got_offset, r_type, entry->object(),
6131                                     entry->symndx());
6132             }
6133         }
6134       else if (entry->tls_type() == GOT_TLS_LDM)
6135         {
6136           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6137                                             : elfcpp::R_MIPS_TLS_DTPMOD64);
6138           unsigned int got_offset;
6139           if (!parameters->doing_static_link())
6140             {
6141               got_offset = got->add_constant(0);
6142               target->rel_dyn_section(layout)->add_local(
6143                   entry->object(), 0, r_type, got, got_offset);
6144             }
6145           else
6146             // We are doing a static link.  Just mark it as belong to module 1,
6147             // the executable.
6148             got_offset = got->add_constant(1);
6149
6150           got->add_constant(0);
6151           got->set_tls_ldm_offset(got_offset, entry->object());
6152         }
6153       else
6154         gold_unreachable();
6155     }
6156
6157   // Add global tls entries.
6158   for (typename Got_entry_set::iterator
6159        p = this->got_entries_.begin();
6160        p != this->got_entries_.end();
6161        ++p)
6162     {
6163       Mips_got_entry<size, big_endian>* entry = *p;
6164       if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6165         continue;
6166
6167       Mips_symbol<size>* mips_sym = entry->sym();
6168       if (entry->tls_type() == GOT_TLS_GD)
6169         {
6170           unsigned int got_type;
6171           if (!got->multi_got())
6172             got_type = GOT_TYPE_TLS_PAIR;
6173           else
6174             got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6175           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6176                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6177           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6178                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6179           if (!parameters->doing_static_link())
6180             got->add_global_pair_with_rel(mips_sym, got_type,
6181                              target->rel_dyn_section(layout), r_type1, r_type2);
6182           else
6183             {
6184               // Add a GOT pair for for R_MIPS_TLS_GD.  The creates a pair of
6185               // GOT entries.  The first one is initialized to be 1, which is the
6186               // module index for the main executable and the second one 0.  A
6187               // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6188               // the second GOT entry and will be applied by gold.
6189               unsigned int got_offset = got->add_constant(1);
6190               mips_sym->set_got_offset(got_type, got_offset);
6191               got->add_constant(0);
6192               got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6193             }
6194         }
6195       else if (entry->tls_type() == GOT_TLS_IE)
6196         {
6197           unsigned int got_type;
6198           if (!got->multi_got())
6199             got_type = GOT_TYPE_TLS_OFFSET;
6200           else
6201             got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
6202           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6203                                             : elfcpp::R_MIPS_TLS_TPREL64);
6204           if (!parameters->doing_static_link())
6205             got->add_global_with_rel(mips_sym, got_type,
6206                                      target->rel_dyn_section(layout), r_type);
6207           else
6208             {
6209               got->add_global(mips_sym, got_type);
6210               unsigned int got_offset = mips_sym->got_offset(got_type);
6211               got->add_static_reloc(got_offset, r_type, mips_sym);
6212             }
6213         }
6214       else
6215         gold_unreachable();
6216     }
6217 }
6218
6219 // Decide whether the symbol needs an entry in the global part of the primary
6220 // GOT, setting global_got_area accordingly.  Count the number of global
6221 // symbols that are in the primary GOT only because they have dynamic
6222 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6223
6224 template<int size, bool big_endian>
6225 void
6226 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6227 {
6228   for (typename Global_got_entry_set::iterator
6229        p = this->global_got_symbols_.begin();
6230        p != this->global_got_symbols_.end();
6231        ++p)
6232     {
6233       Mips_symbol<size>* sym = *p;
6234       // Make a final decision about whether the symbol belongs in the
6235       // local or global GOT.  Symbols that bind locally can (and in the
6236       // case of forced-local symbols, must) live in the local GOT.
6237       // Those that are aren't in the dynamic symbol table must also
6238       // live in the local GOT.
6239
6240       if (!sym->should_add_dynsym_entry(symtab)
6241           || (sym->got_only_for_calls()
6242               ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6243               : symbol_references_local(sym,
6244                                         sym->should_add_dynsym_entry(symtab))))
6245         // The symbol belongs in the local GOT.  We no longer need this
6246         // entry if it was only used for relocations; those relocations
6247         // will be against the null or section symbol instead.
6248         sym->set_global_got_area(GGA_NONE);
6249       else if (sym->global_got_area() == GGA_RELOC_ONLY)
6250         {
6251           ++this->reloc_only_gotno_;
6252           ++this->global_gotno_ ;
6253         }
6254     }
6255 }
6256
6257 // Return the offset of GOT page entry for VALUE.  Initialize the entry with
6258 // VALUE if it is not initialized.
6259
6260 template<int size, bool big_endian>
6261 unsigned int
6262 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6263     Mips_output_data_got<size, big_endian>* got)
6264 {
6265   typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6266   if (it != this->got_page_offsets_.end())
6267     return it->second;
6268
6269   gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6270               + (size/8) * this->page_gotno_);
6271
6272   unsigned int got_offset = this->got_page_offset_next_;
6273   this->got_page_offsets_[value] = got_offset;
6274   this->got_page_offset_next_ += size/8;
6275   got->update_got_entry(got_offset, value);
6276   return got_offset;
6277 }
6278
6279 // Remove lazy-binding stubs for global symbols in this GOT.
6280
6281 template<int size, bool big_endian>
6282 void
6283 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6284     Target_mips<size, big_endian>* target)
6285 {
6286   for (typename Got_entry_set::iterator
6287        p = this->got_entries_.begin();
6288        p != this->got_entries_.end();
6289        ++p)
6290     {
6291       Mips_got_entry<size, big_endian>* entry = *p;
6292       if (entry->is_for_global_symbol())
6293         target->remove_lazy_stub_entry(entry->sym());
6294     }
6295 }
6296
6297 // Count the number of GOT entries required.
6298
6299 template<int size, bool big_endian>
6300 void
6301 Mips_got_info<size, big_endian>::count_got_entries()
6302 {
6303   for (typename Got_entry_set::iterator
6304        p = this->got_entries_.begin();
6305        p != this->got_entries_.end();
6306        ++p)
6307     {
6308       this->count_got_entry(*p);
6309     }
6310 }
6311
6312 // Count the number of GOT entries required by ENTRY.  Accumulate the result.
6313
6314 template<int size, bool big_endian>
6315 void
6316 Mips_got_info<size, big_endian>::count_got_entry(
6317     Mips_got_entry<size, big_endian>* entry)
6318 {
6319   if (entry->is_tls_entry())
6320     this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6321   else if (entry->is_for_local_symbol()
6322            || entry->sym()->global_got_area() == GGA_NONE)
6323     ++this->local_gotno_;
6324   else
6325     ++this->global_gotno_;
6326 }
6327
6328 // Add FROM's GOT entries.
6329
6330 template<int size, bool big_endian>
6331 void
6332 Mips_got_info<size, big_endian>::add_got_entries(
6333     Mips_got_info<size, big_endian>* from)
6334 {
6335   for (typename Got_entry_set::iterator
6336        p = from->got_entries_.begin();
6337        p != from->got_entries_.end();
6338        ++p)
6339     {
6340       Mips_got_entry<size, big_endian>* entry = *p;
6341       if (this->got_entries_.find(entry) == this->got_entries_.end())
6342         {
6343           Mips_got_entry<size, big_endian>* entry2 =
6344             new Mips_got_entry<size, big_endian>(*entry);
6345           this->got_entries_.insert(entry2);
6346           this->count_got_entry(entry);
6347         }
6348     }
6349 }
6350
6351 // Add FROM's GOT page entries.
6352
6353 template<int size, bool big_endian>
6354 void
6355 Mips_got_info<size, big_endian>::add_got_page_entries(
6356     Mips_got_info<size, big_endian>* from)
6357 {
6358   for (typename Got_page_entry_set::iterator
6359        p = from->got_page_entries_.begin();
6360        p != from->got_page_entries_.end();
6361        ++p)
6362     {
6363       Got_page_entry* entry = *p;
6364       if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
6365         {
6366           Got_page_entry* entry2 = new Got_page_entry(*entry);
6367           this->got_page_entries_.insert(entry2);
6368           this->page_gotno_ += entry->num_pages;
6369         }
6370     }
6371 }
6372
6373 // Mips_output_data_got methods.
6374
6375 // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
6376 // larger than 64K, create multi-GOT.
6377
6378 template<int size, bool big_endian>
6379 void
6380 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6381     Symbol_table* symtab, const Input_objects* input_objects)
6382 {
6383   // Decide which symbols need to go in the global part of the GOT and
6384   // count the number of reloc-only GOT symbols.
6385   this->master_got_info_->count_got_symbols(symtab);
6386
6387   // Count the number of GOT entries.
6388   this->master_got_info_->count_got_entries();
6389
6390   unsigned int got_size = this->master_got_info_->got_size();
6391   if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6392     this->lay_out_multi_got(layout, input_objects);
6393   else
6394     {
6395       // Record that all objects use single GOT.
6396       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6397            p != input_objects->relobj_end();
6398            ++p)
6399         {
6400           Mips_relobj<size, big_endian>* object =
6401             Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6402           if (object->get_got_info() != NULL)
6403             object->set_got_info(this->master_got_info_);
6404         }
6405
6406       this->master_got_info_->add_local_entries(this->target_, layout);
6407       this->master_got_info_->add_global_entries(this->target_, layout,
6408                                                  /*not used*/-1U);
6409       this->master_got_info_->add_tls_entries(this->target_, layout);
6410     }
6411 }
6412
6413 // Create multi-GOT.  For every GOT, add local, global and TLS entries.
6414
6415 template<int size, bool big_endian>
6416 void
6417 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6418     const Input_objects* input_objects)
6419 {
6420   // Try to merge the GOTs of input objects together, as long as they
6421   // don't seem to exceed the maximum GOT size, choosing one of them
6422   // to be the primary GOT.
6423   this->merge_gots(input_objects);
6424
6425   // Every symbol that is referenced in a dynamic relocation must be
6426   // present in the primary GOT.
6427   this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6428
6429   // Add GOT entries.
6430   unsigned int i = 0;
6431   unsigned int offset = 0;
6432   Mips_got_info<size, big_endian>* g = this->primary_got_;
6433   do
6434     {
6435       g->set_index(i);
6436       g->set_offset(offset);
6437
6438       g->add_local_entries(this->target_, layout);
6439       if (i == 0)
6440         g->add_global_entries(this->target_, layout,
6441                               (this->master_got_info_->global_gotno()
6442                                - this->master_got_info_->reloc_only_gotno()));
6443       else
6444         g->add_global_entries(this->target_, layout, /*not used*/-1U);
6445       g->add_tls_entries(this->target_, layout);
6446
6447       // Forbid global symbols in every non-primary GOT from having
6448       // lazy-binding stubs.
6449       if (i > 0)
6450         g->remove_lazy_stubs(this->target_);
6451
6452       ++i;
6453       offset += g->got_size();
6454       g = g->next();
6455     }
6456   while (g);
6457 }
6458
6459 // Attempt to merge GOTs of different input objects.  Try to use as much as
6460 // possible of the primary GOT, since it doesn't require explicit dynamic
6461 // relocations, but don't use objects that would reference global symbols
6462 // out of the addressable range.  Failing the primary GOT, attempt to merge
6463 // with the current GOT, or finish the current GOT and then make make the new
6464 // GOT current.
6465
6466 template<int size, bool big_endian>
6467 void
6468 Mips_output_data_got<size, big_endian>::merge_gots(
6469     const Input_objects* input_objects)
6470 {
6471   gold_assert(this->primary_got_ == NULL);
6472   Mips_got_info<size, big_endian>* current = NULL;
6473
6474   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6475        p != input_objects->relobj_end();
6476        ++p)
6477     {
6478       Mips_relobj<size, big_endian>* object =
6479         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6480
6481       Mips_got_info<size, big_endian>* g = object->get_got_info();
6482       if (g == NULL)
6483         continue;
6484
6485       g->count_got_entries();
6486
6487       // Work out the number of page, local and TLS entries.
6488       unsigned int estimate = this->master_got_info_->page_gotno();
6489       if (estimate > g->page_gotno())
6490         estimate = g->page_gotno();
6491       estimate += g->local_gotno() + g->tls_gotno();
6492
6493       // We place TLS GOT entries after both locals and globals.  The globals
6494       // for the primary GOT may overflow the normal GOT size limit, so be
6495       // sure not to merge a GOT which requires TLS with the primary GOT in that
6496       // case.  This doesn't affect non-primary GOTs.
6497       estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6498                                       : g->global_gotno());
6499
6500       unsigned int max_count =
6501         Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6502       if (estimate <= max_count)
6503         {
6504           // If we don't have a primary GOT, use it as
6505           // a starting point for the primary GOT.
6506           if (!this->primary_got_)
6507             {
6508               this->primary_got_ = g;
6509               continue;
6510             }
6511
6512           // Try merging with the primary GOT.
6513           if (this->merge_got_with(g, object, this->primary_got_))
6514             continue;
6515         }
6516
6517       // If we can merge with the last-created GOT, do it.
6518       if (current && this->merge_got_with(g, object, current))
6519         continue;
6520
6521       // Well, we couldn't merge, so create a new GOT.  Don't check if it
6522       // fits; if it turns out that it doesn't, we'll get relocation
6523       // overflows anyway.
6524       g->set_next(current);
6525       current = g;
6526     }
6527
6528   // If we do not find any suitable primary GOT, create an empty one.
6529   if (this->primary_got_ == NULL)
6530     this->primary_got_ = new Mips_got_info<size, big_endian>();
6531
6532   // Link primary GOT with secondary GOTs.
6533   this->primary_got_->set_next(current);
6534 }
6535
6536 // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
6537 // this would lead to overflow, true if they were merged successfully.
6538
6539 template<int size, bool big_endian>
6540 bool
6541 Mips_output_data_got<size, big_endian>::merge_got_with(
6542     Mips_got_info<size, big_endian>* from,
6543     Mips_relobj<size, big_endian>* object,
6544     Mips_got_info<size, big_endian>* to)
6545 {
6546   // Work out how many page entries we would need for the combined GOT.
6547   unsigned int estimate = this->master_got_info_->page_gotno();
6548   if (estimate >= from->page_gotno() + to->page_gotno())
6549     estimate = from->page_gotno() + to->page_gotno();
6550
6551   // Conservatively estimate how many local and TLS entries would be needed.
6552   estimate += from->local_gotno() + to->local_gotno();
6553   estimate += from->tls_gotno() + to->tls_gotno();
6554
6555   // If we're merging with the primary got, any TLS relocations will
6556   // come after the full set of global entries.  Otherwise estimate those
6557   // conservatively as well.
6558   if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6559     estimate += this->master_got_info_->global_gotno();
6560   else
6561     estimate += from->global_gotno() + to->global_gotno();
6562
6563   // Bail out if the combined GOT might be too big.
6564   unsigned int max_count =
6565     Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6566   if (estimate > max_count)
6567     return false;
6568
6569   // Transfer the object's GOT information from FROM to TO.
6570   to->add_got_entries(from);
6571   to->add_got_page_entries(from);
6572
6573   // Record that OBJECT should use output GOT TO.
6574   object->set_got_info(to);
6575
6576   return true;
6577 }
6578
6579 // Write out the GOT.
6580
6581 template<int size, bool big_endian>
6582 void
6583 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6584 {
6585   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6586       Mips_stubs_entry_set;
6587
6588   // Call parent to write out GOT.
6589   Output_data_got<size, big_endian>::do_write(of);
6590
6591   const off_t offset = this->offset();
6592   const section_size_type oview_size =
6593     convert_to_section_size_type(this->data_size());
6594   unsigned char* const oview = of->get_output_view(offset, oview_size);
6595
6596   // Needed for fixing values of .got section.
6597   this->got_view_ = oview;
6598
6599   // Write lazy stub addresses.
6600   for (typename Mips_stubs_entry_set::iterator
6601        p = this->master_got_info_->global_got_symbols().begin();
6602        p != this->master_got_info_->global_got_symbols().end();
6603        ++p)
6604     {
6605       Mips_symbol<size>* mips_sym = *p;
6606       if (mips_sym->has_lazy_stub())
6607         {
6608           Valtype* wv = reinterpret_cast<Valtype*>(
6609             oview + this->get_primary_got_offset(mips_sym));
6610           Valtype value =
6611             this->target_->mips_stubs_section()->stub_address(mips_sym);
6612           elfcpp::Swap<size, big_endian>::writeval(wv, value);
6613         }
6614     }
6615
6616   // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6617   for (typename Mips_stubs_entry_set::iterator
6618        p = this->master_got_info_->global_got_symbols().begin();
6619        p != this->master_got_info_->global_got_symbols().end();
6620        ++p)
6621     {
6622       Mips_symbol<size>* mips_sym = *p;
6623       if (!this->multi_got()
6624           && (mips_sym->is_mips16() || mips_sym->is_micromips())
6625           && mips_sym->global_got_area() == GGA_NONE
6626           && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6627         {
6628           Valtype* wv = reinterpret_cast<Valtype*>(
6629             oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6630           Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6631           if (value != 0)
6632             {
6633               value |= 1;
6634               elfcpp::Swap<size, big_endian>::writeval(wv, value);
6635             }
6636         }
6637     }
6638
6639   if (!this->secondary_got_relocs_.empty())
6640     {
6641       // Fixup for the secondary GOT R_MIPS_REL32 relocs.  For global
6642       // secondary GOT entries with non-zero initial value copy the value
6643       // to the corresponding primary GOT entry, and set the secondary GOT
6644       // entry to zero.
6645       // TODO(sasa): This is workaround.  It needs to be investigated further.
6646
6647       for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6648         {
6649           Static_reloc& reloc(this->secondary_got_relocs_[i]);
6650           if (reloc.symbol_is_global())
6651             {
6652               Mips_symbol<size>* gsym = reloc.symbol();
6653               gold_assert(gsym != NULL);
6654
6655               unsigned got_offset = reloc.got_offset();
6656               gold_assert(got_offset < oview_size);
6657
6658               // Find primary GOT entry.
6659               Valtype* wv_prim = reinterpret_cast<Valtype*>(
6660                 oview + this->get_primary_got_offset(gsym));
6661
6662               // Find secondary GOT entry.
6663               Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6664
6665               Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6666               if (value != 0)
6667                 {
6668                   elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6669                   elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6670                   gsym->set_applied_secondary_got_fixup();
6671                 }
6672             }
6673         }
6674
6675       of->write_output_view(offset, oview_size, oview);
6676     }
6677
6678   // We are done if there is no fix up.
6679   if (this->static_relocs_.empty())
6680     return;
6681
6682   Output_segment* tls_segment = this->layout_->tls_segment();
6683   gold_assert(tls_segment != NULL);
6684
6685   for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6686     {
6687       Static_reloc& reloc(this->static_relocs_[i]);
6688
6689       Mips_address value;
6690       if (!reloc.symbol_is_global())
6691         {
6692           Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6693           const Symbol_value<size>* psymval =
6694             object->local_symbol(reloc.index());
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           bool is_ordinary;
6699           unsigned int shndx = psymval->input_shndx(&is_ordinary);
6700           if ((shndx == elfcpp::SHN_UNDEF)
6701               || (is_ordinary
6702                   && shndx != elfcpp::SHN_UNDEF
6703                   && !object->is_section_included(shndx)
6704                   && !this->symbol_table_->is_section_folded(object, shndx)))
6705             {
6706               gold_error(_("undefined or discarded local symbol %u from "
6707                            " object %s in GOT"),
6708                          reloc.index(), reloc.relobj()->name().c_str());
6709               continue;
6710             }
6711
6712           value = psymval->value(object, 0);
6713         }
6714       else
6715         {
6716           const Mips_symbol<size>* gsym = reloc.symbol();
6717           gold_assert(gsym != NULL);
6718
6719           // We are doing static linking.  Issue an error and skip this
6720           // relocation if the symbol is undefined or in a discarded_section
6721           // unless it is a weakly_undefined symbol.
6722           if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6723               && !gsym->is_weak_undefined())
6724             {
6725               gold_error(_("undefined or discarded symbol %s in GOT"),
6726                          gsym->name());
6727               continue;
6728             }
6729
6730           if (!gsym->is_weak_undefined())
6731             value = gsym->value();
6732           else
6733             value = 0;
6734         }
6735
6736       unsigned got_offset = reloc.got_offset();
6737       gold_assert(got_offset < oview_size);
6738
6739       Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6740       Valtype x;
6741
6742       switch (reloc.r_type())
6743         {
6744         case elfcpp::R_MIPS_TLS_DTPMOD32:
6745         case elfcpp::R_MIPS_TLS_DTPMOD64:
6746           x = value;
6747           break;
6748         case elfcpp::R_MIPS_TLS_DTPREL32:
6749         case elfcpp::R_MIPS_TLS_DTPREL64:
6750           x = value - elfcpp::DTP_OFFSET;
6751           break;
6752         case elfcpp::R_MIPS_TLS_TPREL32:
6753         case elfcpp::R_MIPS_TLS_TPREL64:
6754           x = value - elfcpp::TP_OFFSET;
6755           break;
6756         default:
6757           gold_unreachable();
6758           break;
6759         }
6760
6761       elfcpp::Swap<size, big_endian>::writeval(wv, x);
6762     }
6763
6764   of->write_output_view(offset, oview_size, oview);
6765 }
6766
6767 // Mips_relobj methods.
6768
6769 // Count the local symbols.  The Mips backend needs to know if a symbol
6770 // is a MIPS16 or microMIPS function or not.  For global symbols, it is easy
6771 // because the Symbol object keeps the ELF symbol type and st_other field.
6772 // For local symbol it is harder because we cannot access this information.
6773 // So we override the do_count_local_symbol in parent and scan local symbols to
6774 // mark MIPS16 and microMIPS functions.  This is not the most efficient way but
6775 // I do not want to slow down other ports by calling a per symbol target hook
6776 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6777
6778 template<int size, bool big_endian>
6779 void
6780 Mips_relobj<size, big_endian>::do_count_local_symbols(
6781     Stringpool_template<char>* pool,
6782     Stringpool_template<char>* dynpool)
6783 {
6784   // Ask parent to count the local symbols.
6785   Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6786   const unsigned int loccount = this->local_symbol_count();
6787   if (loccount == 0)
6788     return;
6789
6790   // Initialize the mips16 and micromips function bit-vector.
6791   this->local_symbol_is_mips16_.resize(loccount, false);
6792   this->local_symbol_is_micromips_.resize(loccount, false);
6793
6794   // Read the symbol table section header.
6795   const unsigned int symtab_shndx = this->symtab_shndx();
6796   elfcpp::Shdr<size, big_endian>
6797     symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6798   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6799
6800   // Read the local symbols.
6801   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6802   gold_assert(loccount == symtabshdr.get_sh_info());
6803   off_t locsize = loccount * sym_size;
6804   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6805                                               locsize, true, true);
6806
6807   // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6808
6809   // Skip the first dummy symbol.
6810   psyms += sym_size;
6811   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6812     {
6813       elfcpp::Sym<size, big_endian> sym(psyms);
6814       unsigned char st_other = sym.get_st_other();
6815       this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6816       this->local_symbol_is_micromips_[i] =
6817         elfcpp::elf_st_is_micromips(st_other);
6818     }
6819 }
6820
6821 // Read the symbol information.
6822
6823 template<int size, bool big_endian>
6824 void
6825 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6826 {
6827   // Call parent class to read symbol information.
6828   this->base_read_symbols(sd);
6829
6830   // If this input file is a binary file, it has no processor
6831   // specific data.
6832   Input_file::Format format = this->input_file()->format();
6833   if (format != Input_file::FORMAT_ELF)
6834     {
6835       gold_assert(format == Input_file::FORMAT_BINARY);
6836       this->merge_processor_specific_data_ = false;
6837       return;
6838     }
6839
6840   // Read processor-specific flags in ELF file header.
6841   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6842                                             elfcpp::Elf_sizes<size>::ehdr_size,
6843                                             true, false);
6844   elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6845   this->processor_specific_flags_ = ehdr.get_e_flags();
6846
6847   // Get the section names.
6848   const unsigned char* pnamesu = sd->section_names->data();
6849   const char* pnames = reinterpret_cast<const char*>(pnamesu);
6850
6851   // Initialize the mips16 stub section bit-vectors.
6852   this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6853   this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6854   this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6855
6856   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6857   const unsigned char* pshdrs = sd->section_headers->data();
6858   const unsigned char* ps = pshdrs + shdr_size;
6859   bool must_merge_processor_specific_data = false;
6860   for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6861     {
6862       elfcpp::Shdr<size, big_endian> shdr(ps);
6863
6864       // Sometimes an object has no contents except the section name string
6865       // table and an empty symbol table with the undefined symbol.  We
6866       // don't want to merge processor-specific data from such an object.
6867       if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6868         {
6869           // Symbol table is not empty.
6870           const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
6871             elfcpp::Elf_sizes<size>::sym_size;
6872           if (shdr.get_sh_size() > sym_size)
6873             must_merge_processor_specific_data = true;
6874         }
6875       else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6876         // If this is neither an empty symbol table nor a string table,
6877         // be conservative.
6878         must_merge_processor_specific_data = true;
6879
6880       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6881         {
6882           this->has_reginfo_section_ = true;
6883           // Read the gp value that was used to create this object.  We need the
6884           // gp value while processing relocs.  The .reginfo section is not used
6885           // in the 64-bit MIPS ELF ABI.
6886           section_offset_type section_offset = shdr.get_sh_offset();
6887           section_size_type section_size =
6888             convert_to_section_size_type(shdr.get_sh_size());
6889           const unsigned char* view =
6890              this->get_view(section_offset, section_size, true, false);
6891
6892           this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6893
6894           // Read the rest of .reginfo.
6895           this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6896           this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6897           this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6898           this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6899           this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6900         }
6901
6902       if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6903         {
6904           gold_assert(this->attributes_section_data_ == NULL);
6905           section_offset_type section_offset = shdr.get_sh_offset();
6906           section_size_type section_size =
6907             convert_to_section_size_type(shdr.get_sh_size());
6908           const unsigned char* view =
6909             this->get_view(section_offset, section_size, true, false);
6910           this->attributes_section_data_ =
6911             new Attributes_section_data(view, section_size);
6912         }
6913
6914       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6915         {
6916           gold_assert(this->abiflags_ == NULL);
6917           section_offset_type section_offset = shdr.get_sh_offset();
6918           section_size_type section_size =
6919             convert_to_section_size_type(shdr.get_sh_size());
6920           const unsigned char* view =
6921             this->get_view(section_offset, section_size, true, false);
6922           this->abiflags_ = new Mips_abiflags<big_endian>();
6923
6924           this->abiflags_->version =
6925             elfcpp::Swap<16, big_endian>::readval(view);
6926           if (this->abiflags_->version != 0)
6927             {
6928               gold_error(_("%s: .MIPS.abiflags section has "
6929                            "unsupported version %u"),
6930                          this->name().c_str(),
6931                          this->abiflags_->version);
6932               break;
6933             }
6934           this->abiflags_->isa_level =
6935             elfcpp::Swap<8, big_endian>::readval(view + 2);
6936           this->abiflags_->isa_rev =
6937             elfcpp::Swap<8, big_endian>::readval(view + 3);
6938           this->abiflags_->gpr_size =
6939             elfcpp::Swap<8, big_endian>::readval(view + 4);
6940           this->abiflags_->cpr1_size =
6941             elfcpp::Swap<8, big_endian>::readval(view + 5);
6942           this->abiflags_->cpr2_size =
6943             elfcpp::Swap<8, big_endian>::readval(view + 6);
6944           this->abiflags_->fp_abi =
6945             elfcpp::Swap<8, big_endian>::readval(view + 7);
6946           this->abiflags_->isa_ext =
6947             elfcpp::Swap<32, big_endian>::readval(view + 8);
6948           this->abiflags_->ases =
6949             elfcpp::Swap<32, big_endian>::readval(view + 12);
6950           this->abiflags_->flags1 =
6951             elfcpp::Swap<32, big_endian>::readval(view + 16);
6952           this->abiflags_->flags2 =
6953             elfcpp::Swap<32, big_endian>::readval(view + 20);
6954         }
6955
6956       // In the 64-bit ABI, .MIPS.options section holds register information.
6957       // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6958       // starts with this header:
6959       //
6960       // typedef struct
6961       // {
6962       //   // Type of option.
6963       //   unsigned char kind[1];
6964       //   // Size of option descriptor, including header.
6965       //   unsigned char size[1];
6966       //   // Section index of affected section, or 0 for global option.
6967       //   unsigned char section[2];
6968       //   // Information specific to this kind of option.
6969       //   unsigned char info[4];
6970       // };
6971       //
6972       // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6973       // the gp value based on what we find.  We may see both SHT_MIPS_REGINFO
6974       // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6975
6976       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6977         {
6978           section_offset_type section_offset = shdr.get_sh_offset();
6979           section_size_type section_size =
6980             convert_to_section_size_type(shdr.get_sh_size());
6981           const unsigned char* view =
6982              this->get_view(section_offset, section_size, true, false);
6983           const unsigned char* end = view + section_size;
6984
6985           while (view + 8 <= end)
6986             {
6987               unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6988               unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6989               if (sz < 8)
6990                 {
6991                   gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6992                                "than its header"),
6993                              this->name().c_str(),
6994                              this->mips_elf_options_section_name(), sz);
6995                   break;
6996                 }
6997
6998               if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6999                 {
7000                   // In the 64 bit ABI, an ODK_REGINFO option is the following
7001                   // structure.  The info field of the options header is not
7002                   // used.
7003                   //
7004                   // typedef struct
7005                   // {
7006                   //   // Mask of general purpose registers used.
7007                   //   unsigned char ri_gprmask[4];
7008                   //   // Padding.
7009                   //   unsigned char ri_pad[4];
7010                   //   // Mask of co-processor registers used.
7011                   //   unsigned char ri_cprmask[4][4];
7012                   //   // GP register value for this object file.
7013                   //   unsigned char ri_gp_value[8];
7014                   // };
7015
7016                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7017                                                                       + 32);
7018                 }
7019               else if (kind == elfcpp::ODK_REGINFO)
7020                 {
7021                   // In the 32 bit ABI, an ODK_REGINFO option is the following
7022                   // structure.  The info field of the options header is not
7023                   // used.  The same structure is used in .reginfo section.
7024                   //
7025                   // typedef struct
7026                   // {
7027                   //   unsigned char ri_gprmask[4];
7028                   //   unsigned char ri_cprmask[4][4];
7029                   //   unsigned char ri_gp_value[4];
7030                   // };
7031
7032                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7033                                                                       + 28);
7034                 }
7035               view += sz;
7036             }
7037         }
7038
7039       const char* name = pnames + shdr.get_sh_name();
7040       this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
7041       this->section_is_mips16_call_stub_[i] =
7042         is_prefix_of(".mips16.call.", name);
7043       this->section_is_mips16_call_fp_stub_[i] =
7044         is_prefix_of(".mips16.call.fp.", name);
7045
7046       if (strcmp(name, ".pdr") == 0)
7047         {
7048           gold_assert(this->pdr_shndx_ == -1U);
7049           this->pdr_shndx_ = i;
7050         }
7051     }
7052
7053   // This is rare.
7054   if (!must_merge_processor_specific_data)
7055     this->merge_processor_specific_data_ = false;
7056 }
7057
7058 // Discard MIPS16 stub secions that are not needed.
7059
7060 template<int size, bool big_endian>
7061 void
7062 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7063 {
7064   for (typename Mips16_stubs_int_map::const_iterator
7065        it = this->mips16_stub_sections_.begin();
7066        it != this->mips16_stub_sections_.end(); ++it)
7067     {
7068       Mips16_stub_section<size, big_endian>* stub_section = it->second;
7069       if (!stub_section->is_target_found())
7070         {
7071           gold_error(_("no relocation found in mips16 stub section '%s'"),
7072                      stub_section->object()
7073                        ->section_name(stub_section->shndx()).c_str());
7074         }
7075
7076       bool discard = false;
7077       if (stub_section->is_for_local_function())
7078         {
7079           if (stub_section->is_fn_stub())
7080             {
7081               // This stub is for a local symbol.  This stub will only
7082               // be needed if there is some relocation in this object,
7083               // other than a 16 bit function call, which refers to this
7084               // symbol.
7085               if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7086                 discard = true;
7087               else
7088                 this->add_local_mips16_fn_stub(stub_section);
7089             }
7090           else
7091             {
7092               // This stub is for a local symbol.  This stub will only
7093               // be needed if there is some relocation (R_MIPS16_26) in
7094               // this object that refers to this symbol.
7095               gold_assert(stub_section->is_call_stub()
7096                           || stub_section->is_call_fp_stub());
7097               if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
7098                 discard = true;
7099               else
7100                 this->add_local_mips16_call_stub(stub_section);
7101             }
7102         }
7103       else
7104         {
7105           Mips_symbol<size>* gsym = stub_section->gsym();
7106           if (stub_section->is_fn_stub())
7107             {
7108               if (gsym->has_mips16_fn_stub())
7109                 // We already have a stub for this function.
7110                 discard = true;
7111               else
7112                 {
7113                   gsym->set_mips16_fn_stub(stub_section);
7114                   if (gsym->should_add_dynsym_entry(symtab))
7115                     {
7116                       // If we have a MIPS16 function with a stub, the
7117                       // dynamic symbol must refer to the stub, since only
7118                       // the stub uses the standard calling conventions.
7119                       gsym->set_need_fn_stub();
7120                       if (gsym->is_from_dynobj())
7121                         gsym->set_needs_dynsym_value();
7122                     }
7123                 }
7124               if (!gsym->need_fn_stub())
7125                 discard = true;
7126             }
7127           else if (stub_section->is_call_stub())
7128             {
7129               if (gsym->is_mips16())
7130                 // We don't need the call_stub; this is a 16 bit
7131                 // function, so calls from other 16 bit functions are
7132                 // OK.
7133                 discard = true;
7134               else if (gsym->has_mips16_call_stub())
7135                 // We already have a stub for this function.
7136                 discard = true;
7137               else
7138                 gsym->set_mips16_call_stub(stub_section);
7139             }
7140           else
7141             {
7142               gold_assert(stub_section->is_call_fp_stub());
7143               if (gsym->is_mips16())
7144                 // We don't need the call_stub; this is a 16 bit
7145                 // function, so calls from other 16 bit functions are
7146                 // OK.
7147                 discard = true;
7148               else if (gsym->has_mips16_call_fp_stub())
7149                 // We already have a stub for this function.
7150                 discard = true;
7151               else
7152                 gsym->set_mips16_call_fp_stub(stub_section);
7153             }
7154         }
7155       if (discard)
7156         this->set_output_section(stub_section->shndx(), NULL);
7157    }
7158 }
7159
7160 // Mips_output_data_la25_stub methods.
7161
7162 // Template for standard LA25 stub.
7163 template<int size, bool big_endian>
7164 const uint32_t
7165 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7166 {
7167   0x3c190000,           // lui $25,%hi(func)
7168   0x08000000,           // j func
7169   0x27390000,           // add $25,$25,%lo(func)
7170   0x00000000            // nop
7171 };
7172
7173 // Template for microMIPS LA25 stub.
7174 template<int size, bool big_endian>
7175 const uint32_t
7176 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7177 {
7178   0x41b9, 0x0000,       // lui t9,%hi(func)
7179   0xd400, 0x0000,       // j func
7180   0x3339, 0x0000,       // addiu t9,t9,%lo(func)
7181   0x0000, 0x0000        // nop
7182 };
7183
7184 // Create la25 stub for a symbol.
7185
7186 template<int size, bool big_endian>
7187 void
7188 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7189     Symbol_table* symtab, Target_mips<size, big_endian>* target,
7190     Mips_symbol<size>* gsym)
7191 {
7192   if (!gsym->has_la25_stub())
7193     {
7194       gsym->set_la25_stub_offset(this->symbols_.size() * 16);
7195       this->symbols_.push_back(gsym);
7196       this->create_stub_symbol(gsym, symtab, target, 16);
7197     }
7198 }
7199
7200 // Create a symbol for SYM stub's value and size, to help make the disassembly
7201 // easier to read.
7202
7203 template<int size, bool big_endian>
7204 void
7205 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7206     Mips_symbol<size>* sym, Symbol_table* symtab,
7207     Target_mips<size, big_endian>* target, uint64_t symsize)
7208 {
7209   std::string name(".pic.");
7210   name += sym->name();
7211
7212   unsigned int offset = sym->la25_stub_offset();
7213   if (sym->is_micromips())
7214     offset |= 1;
7215
7216   // Make it a local function.
7217   Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7218                                       Symbol_table::PREDEFINED,
7219                                       target->la25_stub_section(),
7220                                       offset, symsize, elfcpp::STT_FUNC,
7221                                       elfcpp::STB_LOCAL,
7222                                       elfcpp::STV_DEFAULT, 0,
7223                                       false, false);
7224   new_sym->set_is_forced_local();
7225 }
7226
7227 // Write out la25 stubs.  This uses the hand-coded instructions above,
7228 // and adjusts them as needed.
7229
7230 template<int size, bool big_endian>
7231 void
7232 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7233 {
7234   const off_t offset = this->offset();
7235   const section_size_type oview_size =
7236     convert_to_section_size_type(this->data_size());
7237   unsigned char* const oview = of->get_output_view(offset, oview_size);
7238
7239   for (typename std::vector<Mips_symbol<size>*>::iterator
7240        p = this->symbols_.begin();
7241        p != this->symbols_.end();
7242        ++p)
7243     {
7244       Mips_symbol<size>* sym = *p;
7245       unsigned char* pov = oview + sym->la25_stub_offset();
7246
7247       Mips_address target = sym->value();
7248       if (!sym->is_micromips())
7249         {
7250           elfcpp::Swap<32, big_endian>::writeval(pov,
7251               la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7252           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7253               la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7254           elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7255               la25_stub_entry[2] | (target & 0xffff));
7256           elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7257         }
7258       else
7259         {
7260           target |= 1;
7261           // First stub instruction.  Paste high 16-bits of the target.
7262           elfcpp::Swap<16, big_endian>::writeval(pov,
7263                                                  la25_stub_micromips_entry[0]);
7264           elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7265               ((target + 0x8000) >> 16) & 0xffff);
7266           // Second stub instruction.  Paste low 26-bits of the target, shifted
7267           // right by 1.
7268           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7269               la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7270           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7271               la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7272           // Third stub instruction.  Paste low 16-bits of the target.
7273           elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7274                                                  la25_stub_micromips_entry[4]);
7275           elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7276           // Fourth stub instruction.
7277           elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7278                                                  la25_stub_micromips_entry[6]);
7279           elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7280                                                  la25_stub_micromips_entry[7]);
7281         }
7282     }
7283
7284   of->write_output_view(offset, oview_size, oview);
7285 }
7286
7287 // Mips_output_data_plt methods.
7288
7289 // The format of the first PLT entry in an O32 executable.
7290 template<int size, bool big_endian>
7291 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7292 {
7293   0x3c1c0000,         // lui $28, %hi(&GOTPLT[0])
7294   0x8f990000,         // lw $25, %lo(&GOTPLT[0])($28)
7295   0x279c0000,         // addiu $28, $28, %lo(&GOTPLT[0])
7296   0x031cc023,         // subu $24, $24, $28
7297   0x03e07825,         // or $15, $31, zero
7298   0x0018c082,         // srl $24, $24, 2
7299   0x0320f809,         // jalr $25
7300   0x2718fffe          // subu $24, $24, 2
7301 };
7302
7303 // The format of the first PLT entry in an N32 executable.  Different
7304 // because gp ($28) is not available; we use t2 ($14) instead.
7305 template<int size, bool big_endian>
7306 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7307 {
7308   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7309   0x8dd90000,         // lw $25, %lo(&GOTPLT[0])($14)
7310   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7311   0x030ec023,         // subu $24, $24, $14
7312   0x03e07825,         // or $15, $31, zero
7313   0x0018c082,         // srl $24, $24, 2
7314   0x0320f809,         // jalr $25
7315   0x2718fffe          // subu $24, $24, 2
7316 };
7317
7318 // The format of the first PLT entry in an N64 executable.  Different
7319 // from N32 because of the increased size of GOT entries.
7320 template<int size, bool big_endian>
7321 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7322 {
7323   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7324   0xddd90000,         // ld $25, %lo(&GOTPLT[0])($14)
7325   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7326   0x030ec023,         // subu $24, $24, $14
7327   0x03e07825,         // or $15, $31, zero
7328   0x0018c0c2,         // srl $24, $24, 3
7329   0x0320f809,         // jalr $25
7330   0x2718fffe          // subu $24, $24, 2
7331 };
7332
7333 // The format of the microMIPS first PLT entry in an O32 executable.
7334 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7335 // of the GOTPLT entry handled, so this stub may only be used when
7336 // all the subsequent PLT entries are microMIPS code too.
7337 //
7338 // The trailing NOP is for alignment and correct disassembly only.
7339 template<int size, bool big_endian>
7340 const uint32_t Mips_output_data_plt<size, big_endian>::
7341 plt0_entry_micromips_o32[] =
7342 {
7343   0x7980, 0x0000,      // addiupc $3, (&GOTPLT[0]) - .
7344   0xff23, 0x0000,      // lw $25, 0($3)
7345   0x0535,              // subu $2, $2, $3
7346   0x2525,              // srl $2, $2, 2
7347   0x3302, 0xfffe,      // subu $24, $2, 2
7348   0x0dff,              // move $15, $31
7349   0x45f9,              // jalrs $25
7350   0x0f83,              // move $28, $3
7351   0x0c00               // nop
7352 };
7353
7354 // The format of the microMIPS first PLT entry in an O32 executable
7355 // in the insn32 mode.
7356 template<int size, bool big_endian>
7357 const uint32_t Mips_output_data_plt<size, big_endian>::
7358 plt0_entry_micromips32_o32[] =
7359 {
7360   0x41bc, 0x0000,      // lui $28, %hi(&GOTPLT[0])
7361   0xff3c, 0x0000,      // lw $25, %lo(&GOTPLT[0])($28)
7362   0x339c, 0x0000,      // addiu $28, $28, %lo(&GOTPLT[0])
7363   0x0398, 0xc1d0,      // subu $24, $24, $28
7364   0x001f, 0x7a90,      // or $15, $31, zero
7365   0x0318, 0x1040,      // srl $24, $24, 2
7366   0x03f9, 0x0f3c,      // jalr $25
7367   0x3318, 0xfffe       // subu $24, $24, 2
7368 };
7369
7370 // The format of subsequent standard entries in the PLT.
7371 template<int size, bool big_endian>
7372 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7373 {
7374   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7375   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7376   0x03200008,           // jr $25
7377   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7378 };
7379
7380 // The format of subsequent R6 PLT entries.
7381 template<int size, bool big_endian>
7382 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7383 {
7384   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7385   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7386   0x03200009,           // jr $25
7387   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7388 };
7389
7390 // The format of subsequent MIPS16 o32 PLT entries.  We use v1 ($3) as a
7391 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7392 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7393 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7394 // target function address in register v0.
7395 template<int size, bool big_endian>
7396 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7397 {
7398   0xb303,              // lw $3, 12($pc)
7399   0x651b,              // move $24, $3
7400   0x9b60,              // lw $3, 0($3)
7401   0xeb00,              // jr $3
7402   0x653b,              // move $25, $3
7403   0x6500,              // nop
7404   0x0000, 0x0000       // .word (.got.plt entry)
7405 };
7406
7407 // The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
7408 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7409 template<int size, bool big_endian>
7410 const uint32_t Mips_output_data_plt<size, big_endian>::
7411 plt_entry_micromips_o32[] =
7412 {
7413   0x7900, 0x0000,      // addiupc $2, (.got.plt entry) - .
7414   0xff22, 0x0000,      // lw $25, 0($2)
7415   0x4599,              // jr $25
7416   0x0f02               // move $24, $2
7417 };
7418
7419 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7420 template<int size, bool big_endian>
7421 const uint32_t Mips_output_data_plt<size, big_endian>::
7422 plt_entry_micromips32_o32[] =
7423 {
7424   0x41af, 0x0000,      // lui $15, %hi(.got.plt entry)
7425   0xff2f, 0x0000,      // lw $25, %lo(.got.plt entry)($15)
7426   0x0019, 0x0f3c,      // jr $25
7427   0x330f, 0x0000       // addiu $24, $15, %lo(.got.plt entry)
7428 };
7429
7430 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7431
7432 template<int size, bool big_endian>
7433 void
7434 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7435                                                   unsigned int r_type)
7436 {
7437   gold_assert(!gsym->has_plt_offset());
7438
7439   // Final PLT offset for a symbol will be set in method set_plt_offsets().
7440   gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7441                        + sizeof(plt0_entry_o32));
7442   this->symbols_.push_back(gsym);
7443
7444   // Record whether the relocation requires a standard MIPS
7445   // or a compressed code entry.
7446   if (jal_reloc(r_type))
7447    {
7448      if (r_type == elfcpp::R_MIPS_26)
7449        gsym->set_needs_mips_plt(true);
7450      else
7451        gsym->set_needs_comp_plt(true);
7452    }
7453
7454   section_offset_type got_offset = this->got_plt_->current_data_size();
7455
7456   // Every PLT entry needs a GOT entry which points back to the PLT
7457   // entry (this will be changed by the dynamic linker, normally
7458   // lazily when the function is called).
7459   this->got_plt_->set_current_data_size(got_offset + size/8);
7460
7461   gsym->set_needs_dynsym_entry();
7462   this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7463                          got_offset);
7464 }
7465
7466 // Set final PLT offsets.  For each symbol, determine whether standard or
7467 // compressed (MIPS16 or microMIPS) PLT entry is used.
7468
7469 template<int size, bool big_endian>
7470 void
7471 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7472 {
7473   // The sizes of individual PLT entries.
7474   unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7475   unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7476                                       ? this->compressed_plt_entry_size() : 0);
7477
7478   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7479        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7480     {
7481       Mips_symbol<size>* mips_sym = *p;
7482
7483       // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7484       // so always use a standard entry there.
7485       //
7486       // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7487       // all MIPS16 calls will go via that stub, and there is no benefit
7488       // to having a MIPS16 entry.  And in the case of call_stub a
7489       // standard entry actually has to be used as the stub ends with a J
7490       // instruction.
7491       if (this->target_->is_output_newabi()
7492           || mips_sym->has_mips16_call_stub()
7493           || mips_sym->has_mips16_call_fp_stub())
7494         {
7495           mips_sym->set_needs_mips_plt(true);
7496           mips_sym->set_needs_comp_plt(false);
7497         }
7498
7499       // Otherwise, if there are no direct calls to the function, we
7500       // have a free choice of whether to use standard or compressed
7501       // entries.  Prefer microMIPS entries if the object is known to
7502       // contain microMIPS code, so that it becomes possible to create
7503       // pure microMIPS binaries.  Prefer standard entries otherwise,
7504       // because MIPS16 ones are no smaller and are usually slower.
7505       if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7506         {
7507           if (this->target_->is_output_micromips())
7508             mips_sym->set_needs_comp_plt(true);
7509           else
7510             mips_sym->set_needs_mips_plt(true);
7511         }
7512
7513       if (mips_sym->needs_mips_plt())
7514         {
7515           mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7516           this->plt_mips_offset_ += plt_mips_entry_size;
7517         }
7518       if (mips_sym->needs_comp_plt())
7519         {
7520           mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7521           this->plt_comp_offset_ += plt_comp_entry_size;
7522         }
7523     }
7524
7525     // Figure out the size of the PLT header if we know that we are using it.
7526     if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7527       this->plt_header_size_ = this->get_plt_header_size();
7528 }
7529
7530 // Write out the PLT.  This uses the hand-coded instructions above,
7531 // and adjusts them as needed.
7532
7533 template<int size, bool big_endian>
7534 void
7535 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7536 {
7537   const off_t offset = this->offset();
7538   const section_size_type oview_size =
7539     convert_to_section_size_type(this->data_size());
7540   unsigned char* const oview = of->get_output_view(offset, oview_size);
7541
7542   const off_t gotplt_file_offset = this->got_plt_->offset();
7543   const section_size_type gotplt_size =
7544     convert_to_section_size_type(this->got_plt_->data_size());
7545   unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7546                                                          gotplt_size);
7547   unsigned char* pov = oview;
7548
7549   Mips_address plt_address = this->address();
7550
7551   // Calculate the address of .got.plt.
7552   Mips_address gotplt_addr = this->got_plt_->address();
7553   Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7554   Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7555
7556   // The PLT sequence is not safe for N64 if .got.plt's address can
7557   // not be loaded in two instructions.
7558   gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7559               || ~(gotplt_addr | 0x7fffffff) == 0);
7560
7561   // Write the PLT header.
7562   const uint32_t* plt0_entry = this->get_plt_header_entry();
7563   if (plt0_entry == plt0_entry_micromips_o32)
7564     {
7565       // Write microMIPS PLT header.
7566       gold_assert(gotplt_addr % 4 == 0);
7567
7568       Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7569
7570       // ADDIUPC has a span of +/-16MB, check we're in range.
7571       if (gotpc_offset + 0x1000000 >= 0x2000000)
7572        {
7573          gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7574                     "ADDIUPC"), (long)gotpc_offset);
7575          return;
7576        }
7577
7578       elfcpp::Swap<16, big_endian>::writeval(pov,
7579                  plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7580       elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7581                                              (gotpc_offset >> 2) & 0xffff);
7582       pov += 4;
7583       for (unsigned int i = 2;
7584            i < (sizeof(plt0_entry_micromips_o32)
7585                 / sizeof(plt0_entry_micromips_o32[0]));
7586            i++)
7587         {
7588           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7589           pov += 2;
7590         }
7591     }
7592   else if (plt0_entry == plt0_entry_micromips32_o32)
7593     {
7594       // Write microMIPS PLT header in insn32 mode.
7595       elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7596       elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7597       elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7598       elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7599       elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7600       elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7601       pov += 12;
7602       for (unsigned int i = 6;
7603            i < (sizeof(plt0_entry_micromips32_o32)
7604                 / sizeof(plt0_entry_micromips32_o32[0]));
7605            i++)
7606         {
7607           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7608           pov += 2;
7609         }
7610     }
7611   else
7612     {
7613       // Write standard PLT header.
7614       elfcpp::Swap<32, big_endian>::writeval(pov,
7615                                              plt0_entry[0] | gotplt_addr_high);
7616       elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7617                                              plt0_entry[1] | gotplt_addr_low);
7618       elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7619                                              plt0_entry[2] | gotplt_addr_low);
7620       pov += 12;
7621       for (int i = 3; i < 8; i++)
7622         {
7623           elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7624           pov += 4;
7625         }
7626     }
7627
7628
7629   unsigned char* gotplt_pov = gotplt_view;
7630   unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7631
7632   // The first two entries in .got.plt are reserved.
7633   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7634   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7635
7636   unsigned int gotplt_offset = 2 * got_entry_size;
7637   gotplt_pov += 2 * got_entry_size;
7638
7639   // Calculate the address of the PLT header.
7640   Mips_address header_address = (plt_address
7641                                  + (this->is_plt_header_compressed() ? 1 : 0));
7642
7643   // Initialize compressed PLT area view.
7644   unsigned char* pov2 = pov + this->plt_mips_offset_;
7645
7646   // Write the PLT entries.
7647   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7648        p = this->symbols_.begin();
7649        p != this->symbols_.end();
7650        ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7651     {
7652       Mips_symbol<size>* mips_sym = *p;
7653
7654       // Calculate the address of the .got.plt entry.
7655       uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7656       uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7657                                        & 0xffff);
7658       uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7659
7660       // Initially point the .got.plt entry at the PLT header.
7661       if (this->target_->is_output_n64())
7662         elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7663       else
7664         elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7665
7666       // Now handle the PLT itself.  First the standard entry.
7667       if (mips_sym->has_mips_plt_offset())
7668         {
7669           // Pick the load opcode (LW or LD).
7670           uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7671                                                          : 0x8c000000;
7672
7673           const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7674                                                                 : plt_entry;
7675
7676           // Fill in the PLT entry itself.
7677           elfcpp::Swap<32, big_endian>::writeval(pov,
7678               entry[0] | gotplt_entry_addr_hi);
7679           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7680               entry[1] | gotplt_entry_addr_lo | load);
7681           elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
7682           elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7683               entry[3] | gotplt_entry_addr_lo);
7684           pov += 16;
7685         }
7686
7687       // Now the compressed entry.  They come after any standard ones.
7688       if (mips_sym->has_comp_plt_offset())
7689         {
7690           if (!this->target_->is_output_micromips())
7691             {
7692               // Write MIPS16 PLT entry.
7693               const uint32_t* plt_entry = plt_entry_mips16_o32;
7694
7695               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7696               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7697               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7698               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7699               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7700               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7701               elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7702                                                      gotplt_entry_addr);
7703               pov2 += 16;
7704             }
7705           else if (this->target_->use_32bit_micromips_instructions())
7706             {
7707               // Write microMIPS PLT entry in insn32 mode.
7708               const uint32_t* plt_entry = plt_entry_micromips32_o32;
7709
7710               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7711               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7712                                                      gotplt_entry_addr_hi);
7713               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7714               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7715                                                      gotplt_entry_addr_lo);
7716               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7717               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7718               elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7719               elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7720                                                      gotplt_entry_addr_lo);
7721               pov2 += 16;
7722             }
7723           else
7724             {
7725               // Write microMIPS PLT entry.
7726               const uint32_t* plt_entry = plt_entry_micromips_o32;
7727
7728               gold_assert(gotplt_entry_addr % 4 == 0);
7729
7730               Mips_address loc_address = plt_address + pov2 - oview;
7731               int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7732
7733               // ADDIUPC has a span of +/-16MB, check we're in range.
7734               if (gotpc_offset + 0x1000000 >= 0x2000000)
7735                 {
7736                   gold_error(_(".got.plt offset of %ld from .plt beyond the "
7737                              "range of ADDIUPC"), (long)gotpc_offset);
7738                   return;
7739                 }
7740
7741               elfcpp::Swap<16, big_endian>::writeval(pov2,
7742                           plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7743               elfcpp::Swap<16, big_endian>::writeval(
7744                   pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7745               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7746               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7747               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7748               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7749               pov2 += 12;
7750             }
7751         }
7752     }
7753
7754   // Check the number of bytes written for standard entries.
7755   gold_assert(static_cast<section_size_type>(
7756       pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7757   // Check the number of bytes written for compressed entries.
7758   gold_assert((static_cast<section_size_type>(pov2 - pov)
7759                == this->plt_comp_offset_));
7760   // Check the total number of bytes written.
7761   gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7762
7763   gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7764               == gotplt_size);
7765
7766   of->write_output_view(offset, oview_size, oview);
7767   of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7768 }
7769
7770 // Mips_output_data_mips_stubs methods.
7771
7772 // The format of the lazy binding stub when dynamic symbol count is less than
7773 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7774 template<int size, bool big_endian>
7775 const uint32_t
7776 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7777 {
7778   0x8f998010,         // lw t9,0x8010(gp)
7779   0x03e07825,         // or t7,ra,zero
7780   0x0320f809,         // jalr t9,ra
7781   0x24180000          // addiu t8,zero,DYN_INDEX sign extended
7782 };
7783
7784 // The format of the lazy binding stub when dynamic symbol count is less than
7785 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7786 template<int size, bool big_endian>
7787 const uint32_t
7788 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7789 {
7790   0xdf998010,         // ld t9,0x8010(gp)
7791   0x03e07825,         // or t7,ra,zero
7792   0x0320f809,         // jalr t9,ra
7793   0x64180000          // daddiu t8,zero,DYN_INDEX sign extended
7794 };
7795
7796 // The format of the lazy binding stub when dynamic symbol count is less than
7797 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7798 template<int size, bool big_endian>
7799 const uint32_t
7800 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7801 {
7802   0x8f998010,         // lw t9,0x8010(gp)
7803   0x03e07825,         // or t7,ra,zero
7804   0x0320f809,         // jalr t9,ra
7805   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7806 };
7807
7808 // The format of the lazy binding stub when dynamic symbol count is less than
7809 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7810 template<int size, bool big_endian>
7811 const uint32_t
7812 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7813 {
7814   0xdf998010,         // ld t9,0x8010(gp)
7815   0x03e07825,         // or t7,ra,zero
7816   0x0320f809,         // jalr t9,ra
7817   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7818 };
7819
7820 // The format of the lazy binding stub when dynamic symbol count is greater than
7821 // 64K, and ABI is not N64.
7822 template<int size, bool big_endian>
7823 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7824 {
7825   0x8f998010,         // lw t9,0x8010(gp)
7826   0x03e07825,         // or t7,ra,zero
7827   0x3c180000,         // lui t8,DYN_INDEX
7828   0x0320f809,         // jalr t9,ra
7829   0x37180000          // ori t8,t8,DYN_INDEX
7830 };
7831
7832 // The format of the lazy binding stub when dynamic symbol count is greater than
7833 // 64K, and ABI is N64.
7834 template<int size, bool big_endian>
7835 const uint32_t
7836 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7837 {
7838   0xdf998010,         // ld t9,0x8010(gp)
7839   0x03e07825,         // or t7,ra,zero
7840   0x3c180000,         // lui t8,DYN_INDEX
7841   0x0320f809,         // jalr t9,ra
7842   0x37180000          // ori t8,t8,DYN_INDEX
7843 };
7844
7845 // microMIPS stubs.
7846
7847 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7848 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7849 template<int size, bool big_endian>
7850 const uint32_t
7851 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7852 {
7853   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7854   0x0dff,             // move t7,ra
7855   0x45d9,             // jalr t9
7856   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7857 };
7858
7859 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7860 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7861 template<int size, bool big_endian>
7862 const uint32_t
7863 Mips_output_data_mips_stubs<size, big_endian>::
7864 lazy_stub_micromips_normal_1_n64[] =
7865 {
7866   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7867   0x0dff,             // move t7,ra
7868   0x45d9,             // jalr t9
7869   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7870 };
7871
7872 // The format of the microMIPS lazy binding stub when dynamic symbol
7873 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7874 // and ABI is not N64.
7875 template<int size, bool big_endian>
7876 const uint32_t
7877 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7878 {
7879   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7880   0x0dff,             // move t7,ra
7881   0x45d9,             // jalr t9
7882   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7883 };
7884
7885 // The format of the microMIPS lazy binding stub when dynamic symbol
7886 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7887 // and ABI is N64.
7888 template<int size, bool big_endian>
7889 const uint32_t
7890 Mips_output_data_mips_stubs<size, big_endian>::
7891 lazy_stub_micromips_normal_2_n64[] =
7892 {
7893   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7894   0x0dff,             // move t7,ra
7895   0x45d9,             // jalr t9
7896   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7897 };
7898
7899 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7900 // greater than 64K, and ABI is not N64.
7901 template<int size, bool big_endian>
7902 const uint32_t
7903 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7904 {
7905   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7906   0x0dff,             // move t7,ra
7907   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7908   0x45d9,             // jalr t9
7909   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7910 };
7911
7912 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7913 // greater than 64K, and ABI is N64.
7914 template<int size, bool big_endian>
7915 const uint32_t
7916 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7917 {
7918   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7919   0x0dff,             // move t7,ra
7920   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7921   0x45d9,             // jalr t9
7922   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7923 };
7924
7925 // 32-bit microMIPS stubs.
7926
7927 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7928 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7929 // can use only 32-bit instructions.
7930 template<int size, bool big_endian>
7931 const uint32_t
7932 Mips_output_data_mips_stubs<size, big_endian>::
7933 lazy_stub_micromips32_normal_1[] =
7934 {
7935   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7936   0x001f, 0x7a90,     // or t7,ra,zero
7937   0x03f9, 0x0f3c,     // jalr ra,t9
7938   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7939 };
7940
7941 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7942 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7943 // use only 32-bit instructions.
7944 template<int size, bool big_endian>
7945 const uint32_t
7946 Mips_output_data_mips_stubs<size, big_endian>::
7947 lazy_stub_micromips32_normal_1_n64[] =
7948 {
7949   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7950   0x001f, 0x7a90,     // or t7,ra,zero
7951   0x03f9, 0x0f3c,     // jalr ra,t9
7952   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7953 };
7954
7955 // The format of the microMIPS lazy binding stub when dynamic symbol
7956 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7957 // ABI is not N64, and we can use only 32-bit instructions.
7958 template<int size, bool big_endian>
7959 const uint32_t
7960 Mips_output_data_mips_stubs<size, big_endian>::
7961 lazy_stub_micromips32_normal_2[] =
7962 {
7963   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7964   0x001f, 0x7a90,     // or t7,ra,zero
7965   0x03f9, 0x0f3c,     // jalr ra,t9
7966   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7967 };
7968
7969 // The format of the microMIPS lazy binding stub when dynamic symbol
7970 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7971 // ABI is N64, and we can use only 32-bit instructions.
7972 template<int size, bool big_endian>
7973 const uint32_t
7974 Mips_output_data_mips_stubs<size, big_endian>::
7975 lazy_stub_micromips32_normal_2_n64[] =
7976 {
7977   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7978   0x001f, 0x7a90,     // or t7,ra,zero
7979   0x03f9, 0x0f3c,     // jalr ra,t9
7980   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7981 };
7982
7983 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7984 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7985 template<int size, bool big_endian>
7986 const uint32_t
7987 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7988 {
7989   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7990   0x001f, 0x7a90,     // or t7,ra,zero
7991   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7992   0x03f9, 0x0f3c,     // jalr ra,t9
7993   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7994 };
7995
7996 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7997 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7998 template<int size, bool big_endian>
7999 const uint32_t
8000 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
8001 {
8002   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
8003   0x001f, 0x7a90,     // or t7,ra,zero
8004   0x41b8, 0x0000,     // lui t8,DYN_INDEX
8005   0x03f9, 0x0f3c,     // jalr ra,t9
8006   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
8007 };
8008
8009 // Create entry for a symbol.
8010
8011 template<int size, bool big_endian>
8012 void
8013 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
8014     Mips_symbol<size>* gsym)
8015 {
8016   if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
8017     {
8018       this->symbols_.insert(gsym);
8019       gsym->set_has_lazy_stub(true);
8020     }
8021 }
8022
8023 // Remove entry for a symbol.
8024
8025 template<int size, bool big_endian>
8026 void
8027 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8028     Mips_symbol<size>* gsym)
8029 {
8030   if (gsym->has_lazy_stub())
8031     {
8032       this->symbols_.erase(gsym);
8033       gsym->set_has_lazy_stub(false);
8034     }
8035 }
8036
8037 // Set stub offsets for symbols.  This method expects that the number of
8038 // entries in dynamic symbol table is set.
8039
8040 template<int size, bool big_endian>
8041 void
8042 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8043 {
8044   gold_assert(this->dynsym_count_ != -1U);
8045
8046   if (this->stub_offsets_are_set_)
8047     return;
8048
8049   unsigned int stub_size = this->stub_size();
8050   unsigned int offset = 0;
8051   for (typename Mips_stubs_entry_set::const_iterator
8052        p = this->symbols_.begin();
8053        p != this->symbols_.end();
8054        ++p, offset += stub_size)
8055     {
8056       Mips_symbol<size>* mips_sym = *p;
8057       mips_sym->set_lazy_stub_offset(offset);
8058     }
8059   this->stub_offsets_are_set_ = true;
8060 }
8061
8062 template<int size, bool big_endian>
8063 void
8064 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8065 {
8066   for (typename Mips_stubs_entry_set::const_iterator
8067        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8068     {
8069       Mips_symbol<size>* sym = *p;
8070       if (sym->is_from_dynobj())
8071         sym->set_needs_dynsym_value();
8072     }
8073 }
8074
8075 // Write out the .MIPS.stubs.  This uses the hand-coded instructions and
8076 // adjusts them as needed.
8077
8078 template<int size, bool big_endian>
8079 void
8080 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
8081 {
8082   const off_t offset = this->offset();
8083   const section_size_type oview_size =
8084     convert_to_section_size_type(this->data_size());
8085   unsigned char* const oview = of->get_output_view(offset, oview_size);
8086
8087   bool big_stub = this->dynsym_count_ > 0x10000;
8088
8089   unsigned char* pov = oview;
8090   for (typename Mips_stubs_entry_set::const_iterator
8091        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8092     {
8093       Mips_symbol<size>* sym = *p;
8094       const uint32_t* lazy_stub;
8095       bool n64 = this->target_->is_output_n64();
8096
8097       if (!this->target_->is_output_micromips())
8098         {
8099           // Write standard (non-microMIPS) stub.
8100           if (!big_stub)
8101             {
8102               if (sym->dynsym_index() & ~0x7fff)
8103                 // Dynsym index is between 32K and 64K.
8104                 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
8105               else
8106                 // Dynsym index is less than 32K.
8107                 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8108             }
8109           else
8110             lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8111
8112           unsigned int i = 0;
8113           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8114           elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8115           pov += 8;
8116
8117           i += 2;
8118           if (big_stub)
8119             {
8120               // LUI instruction of the big stub.  Paste high 16 bits of the
8121               // dynsym index.
8122               elfcpp::Swap<32, big_endian>::writeval(pov,
8123                   lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
8124               pov += 4;
8125               i += 1;
8126             }
8127           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8128           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8129           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8130               lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8131           pov += 8;
8132         }
8133       else if (this->target_->use_32bit_micromips_instructions())
8134         {
8135           // Write microMIPS stub in insn32 mode.
8136           if (!big_stub)
8137             {
8138               if (sym->dynsym_index() & ~0x7fff)
8139                 // Dynsym index is between 32K and 64K.
8140                 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8141                                 : lazy_stub_micromips32_normal_2;
8142               else
8143                 // Dynsym index is less than 32K.
8144                 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8145                                 : lazy_stub_micromips32_normal_1;
8146             }
8147           else
8148             lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8149                             : lazy_stub_micromips32_big;
8150
8151           unsigned int i = 0;
8152           // First stub instruction.  We emit 32-bit microMIPS instructions by
8153           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8154           // the instruction where the opcode is must always come first, for
8155           // both little and big endian.
8156           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8157           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8158           // Second stub instruction.
8159           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8160           elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8161           pov += 8;
8162           i += 4;
8163           if (big_stub)
8164             {
8165               // LUI instruction of the big stub.  Paste high 16 bits of the
8166               // dynsym index.
8167               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8168               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8169                   (sym->dynsym_index() >> 16) & 0x7fff);
8170               pov += 4;
8171               i += 2;
8172             }
8173           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8174           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8175           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8176           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8177           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8178               sym->dynsym_index() & 0xffff);
8179           pov += 8;
8180         }
8181       else
8182         {
8183           // Write microMIPS stub.
8184           if (!big_stub)
8185             {
8186               if (sym->dynsym_index() & ~0x7fff)
8187                 // Dynsym index is between 32K and 64K.
8188                 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8189                                 : lazy_stub_micromips_normal_2;
8190               else
8191                 // Dynsym index is less than 32K.
8192                 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8193                                 : lazy_stub_micromips_normal_1;
8194             }
8195           else
8196             lazy_stub = n64 ? lazy_stub_micromips_big_n64
8197                             : lazy_stub_micromips_big;
8198
8199           unsigned int i = 0;
8200           // First stub instruction.  We emit 32-bit microMIPS instructions by
8201           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8202           // the instruction where the opcode is must always come first, for
8203           // both little and big endian.
8204           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8205           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8206           // Second stub instruction.
8207           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8208           pov += 6;
8209           i += 3;
8210           if (big_stub)
8211             {
8212               // LUI instruction of the big stub.  Paste high 16 bits of the
8213               // dynsym index.
8214               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8215               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8216                   (sym->dynsym_index() >> 16) & 0x7fff);
8217               pov += 4;
8218               i += 2;
8219             }
8220           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8221           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8222           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8223           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8224               sym->dynsym_index() & 0xffff);
8225           pov += 6;
8226         }
8227     }
8228
8229   // We always allocate 20 bytes for every stub, because final dynsym count is
8230   // not known in method do_finalize_sections.  There are 4 unused bytes per
8231   // stub if final dynsym count is less than 0x10000.
8232   unsigned int used = pov - oview;
8233   unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8234   gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8235
8236   // Fill the unused space with zeroes.
8237   // TODO(sasa): Can we strip unused bytes during the relaxation?
8238   if (unused > 0)
8239     memset(pov, 0, unused);
8240
8241   of->write_output_view(offset, oview_size, oview);
8242 }
8243
8244 // Mips_output_section_reginfo methods.
8245
8246 template<int size, bool big_endian>
8247 void
8248 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8249 {
8250   off_t offset = this->offset();
8251   off_t data_size = this->data_size();
8252
8253   unsigned char* view = of->get_output_view(offset, data_size);
8254   elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8255   elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8256   elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8257   elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8258   elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8259   // Write the gp value.
8260   elfcpp::Swap<size, big_endian>::writeval(view + 20,
8261                                            this->target_->gp_value());
8262
8263   of->write_output_view(offset, data_size, view);
8264 }
8265
8266 // Mips_output_section_options methods.
8267
8268 template<int size, bool big_endian>
8269 void
8270 Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
8271 {
8272   off_t offset = this->offset();
8273   const section_size_type oview_size =
8274     convert_to_section_size_type(this->data_size());
8275   unsigned char* view = of->get_output_view(offset, oview_size);
8276   const unsigned char* end = view + oview_size;
8277
8278   while (view + 8 <= end)
8279     {
8280       unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8281       unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8282       if (sz < 8)
8283         {
8284           gold_error(_("Warning: bad `%s' option size %u smaller "
8285                        "than its header in output section"),
8286                      this->name(), sz);
8287           break;
8288         }
8289
8290       // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
8291       if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
8292         elfcpp::Swap<size, big_endian>::writeval(view + 32,
8293                                                  this->target_->gp_value());
8294       else if (kind == elfcpp::ODK_REGINFO)
8295         elfcpp::Swap<size, big_endian>::writeval(view + 28,
8296                                                  this->target_->gp_value());
8297
8298       view += sz;
8299     }
8300
8301   of->write_output_view(offset, oview_size, view);
8302 }
8303
8304 // Mips_output_section_abiflags methods.
8305
8306 template<int size, bool big_endian>
8307 void
8308 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8309 {
8310   off_t offset = this->offset();
8311   off_t data_size = this->data_size();
8312
8313   unsigned char* view = of->get_output_view(offset, data_size);
8314   elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8315   elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8316   elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8317   elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8318   elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8319   elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8320   elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8321   elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8322   elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8323   elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8324   elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8325
8326   of->write_output_view(offset, data_size, view);
8327 }
8328
8329 // Mips_copy_relocs methods.
8330
8331 // Emit any saved relocs.
8332
8333 template<int sh_type, int size, bool big_endian>
8334 void
8335 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8336     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8337     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8338 {
8339   for (typename Copy_relocs<sh_type, size, big_endian>::
8340        Copy_reloc_entries::iterator p = this->entries_.begin();
8341        p != this->entries_.end();
8342        ++p)
8343     emit_entry(*p, reloc_section, symtab, layout, target);
8344
8345   // We no longer need the saved information.
8346   this->entries_.clear();
8347 }
8348
8349 // Emit the reloc if appropriate.
8350
8351 template<int sh_type, int size, bool big_endian>
8352 void
8353 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8354     Copy_reloc_entry& entry,
8355     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8356     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8357 {
8358   // If the symbol is no longer defined in a dynamic object, then we
8359   // emitted a COPY relocation, and we do not want to emit this
8360   // dynamic relocation.
8361   if (!entry.sym_->is_from_dynobj())
8362     return;
8363
8364   bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8365                            || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8366                            || entry.reloc_type_ == elfcpp::R_MIPS_64);
8367
8368   Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8369   if (can_make_dynamic && !sym->has_static_relocs())
8370     {
8371       Mips_relobj<size, big_endian>* object =
8372         Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8373       target->got_section(symtab, layout)->record_global_got_symbol(
8374                           sym, object, entry.reloc_type_, true, false);
8375       if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8376         target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8377             entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8378       else
8379         target->rel_dyn_section(layout)->add_symbolless_global_addend(
8380             sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8381             entry.shndx_, entry.address_);
8382     }
8383   else
8384     this->make_copy_reloc(symtab, layout,
8385                           static_cast<Sized_symbol<size>*>(entry.sym_),
8386                           entry.relobj_,
8387                           reloc_section);
8388 }
8389
8390 // Target_mips methods.
8391
8392 // Return the value to use for a dynamic symbol which requires special
8393 // treatment.  This is how we support equality comparisons of function
8394 // pointers across shared library boundaries, as described in the
8395 // processor specific ABI supplement.
8396
8397 template<int size, bool big_endian>
8398 uint64_t
8399 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8400 {
8401   uint64_t value = 0;
8402   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8403
8404   if (!mips_sym->has_lazy_stub())
8405     {
8406       if (mips_sym->has_plt_offset())
8407         {
8408           // We distinguish between PLT entries and lazy-binding stubs by
8409           // giving the former an st_other value of STO_MIPS_PLT.  Set the
8410           // value to the stub address if there are any relocations in the
8411           // binary where pointer equality matters.
8412           if (mips_sym->pointer_equality_needed())
8413             {
8414               // Prefer a standard MIPS PLT entry.
8415               if (mips_sym->has_mips_plt_offset())
8416                 value = this->plt_section()->mips_entry_address(mips_sym);
8417               else
8418                 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8419             }
8420           else
8421             value = 0;
8422         }
8423     }
8424   else
8425     {
8426       // First, set stub offsets for symbols.  This method expects that the
8427       // number of entries in dynamic symbol table is set.
8428       this->mips_stubs_section()->set_lazy_stub_offsets();
8429
8430       // The run-time linker uses the st_value field of the symbol
8431       // to reset the global offset table entry for this external
8432       // to its stub address when unlinking a shared object.
8433       value = this->mips_stubs_section()->stub_address(mips_sym);
8434     }
8435
8436   if (mips_sym->has_mips16_fn_stub())
8437     {
8438       // If we have a MIPS16 function with a stub, the dynamic symbol must
8439       // refer to the stub, since only the stub uses the standard calling
8440       // conventions.
8441       value = mips_sym->template
8442               get_mips16_fn_stub<big_endian>()->output_address();
8443     }
8444
8445   return value;
8446 }
8447
8448 // Get the dynamic reloc section, creating it if necessary.  It's always
8449 // .rel.dyn, even for MIPS64.
8450
8451 template<int size, bool big_endian>
8452 typename Target_mips<size, big_endian>::Reloc_section*
8453 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8454 {
8455   if (this->rel_dyn_ == NULL)
8456     {
8457       gold_assert(layout != NULL);
8458       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8459       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8460                                       elfcpp::SHF_ALLOC, this->rel_dyn_,
8461                                       ORDER_DYNAMIC_RELOCS, false);
8462
8463       // First entry in .rel.dyn has to be null.
8464       // This is hack - we define dummy output data and set its address to 0,
8465       // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8466       // This ensures that the entry is null.
8467       Output_data* od = new Output_data_zero_fill(0, 0);
8468       od->set_address(0);
8469       this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8470     }
8471   return this->rel_dyn_;
8472 }
8473
8474 // Get the GOT section, creating it if necessary.
8475
8476 template<int size, bool big_endian>
8477 Mips_output_data_got<size, big_endian>*
8478 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8479                                            Layout* layout)
8480 {
8481   if (this->got_ == NULL)
8482     {
8483       gold_assert(symtab != NULL && layout != NULL);
8484
8485       this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8486                                                               layout);
8487       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8488                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8489                                       elfcpp::SHF_MIPS_GPREL),
8490                                       this->got_, ORDER_DATA, false);
8491
8492       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8493       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8494                                     Symbol_table::PREDEFINED,
8495                                     this->got_,
8496                                     0, 0, elfcpp::STT_OBJECT,
8497                                     elfcpp::STB_GLOBAL,
8498                                     elfcpp::STV_HIDDEN, 0,
8499                                     false, false);
8500     }
8501
8502   return this->got_;
8503 }
8504
8505 // Calculate value of _gp symbol.
8506
8507 template<int size, bool big_endian>
8508 void
8509 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8510 {
8511   gold_assert(this->gp_ == NULL);
8512
8513   Sized_symbol<size>* gp =
8514     static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8515
8516   // Set _gp symbol if the linker script hasn't created it.
8517   if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
8518     {
8519       // If there is no .got section, gp should be based on .sdata.
8520       Output_data* gp_section = (this->got_ != NULL
8521                                  ? this->got_->output_section()
8522                                  : layout->find_output_section(".sdata"));
8523
8524       if (gp_section != NULL)
8525         gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8526                                           "_gp", NULL, Symbol_table::PREDEFINED,
8527                                           gp_section, MIPS_GP_OFFSET, 0,
8528                                           elfcpp::STT_NOTYPE,
8529                                           elfcpp::STB_LOCAL,
8530                                           elfcpp::STV_DEFAULT,
8531                                           0, false, false));
8532     }
8533
8534   this->gp_ = gp;
8535 }
8536
8537 // Set the dynamic symbol indexes.  INDEX is the index of the first
8538 // global dynamic symbol.  Pointers to the symbols are stored into the
8539 // vector SYMS.  The names are added to DYNPOOL.  This returns an
8540 // updated dynamic symbol index.
8541
8542 template<int size, bool big_endian>
8543 unsigned int
8544 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8545     std::vector<Symbol*>* dyn_symbols, unsigned int index,
8546     std::vector<Symbol*>* syms, Stringpool* dynpool,
8547     Versions* versions, Symbol_table* symtab) const
8548 {
8549   std::vector<Symbol*> non_got_symbols;
8550   std::vector<Symbol*> got_symbols;
8551
8552   reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8553                                         &got_symbols);
8554
8555   for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8556        p != non_got_symbols.end();
8557        ++p)
8558     {
8559       Symbol* sym = *p;
8560
8561       // Note that SYM may already have a dynamic symbol index, since
8562       // some symbols appear more than once in the symbol table, with
8563       // and without a version.
8564
8565       if (!sym->has_dynsym_index())
8566         {
8567           sym->set_dynsym_index(index);
8568           ++index;
8569           syms->push_back(sym);
8570           dynpool->add(sym->name(), false, NULL);
8571
8572           // Record any version information.
8573           if (sym->version() != NULL)
8574             versions->record_version(symtab, dynpool, sym);
8575
8576           // If the symbol is defined in a dynamic object and is
8577           // referenced in a regular object, then mark the dynamic
8578           // object as needed.  This is used to implement --as-needed.
8579           if (sym->is_from_dynobj() && sym->in_reg())
8580             sym->object()->set_is_needed();
8581         }
8582     }
8583
8584   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8585        p != got_symbols.end();
8586        ++p)
8587     {
8588       Symbol* sym = *p;
8589       if (!sym->has_dynsym_index())
8590         {
8591           // Record any version information.
8592           if (sym->version() != NULL)
8593             versions->record_version(symtab, dynpool, sym);
8594         }
8595     }
8596
8597   index = versions->finalize(symtab, index, syms);
8598
8599   int got_sym_count = 0;
8600   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8601        p != got_symbols.end();
8602        ++p)
8603     {
8604       Symbol* sym = *p;
8605
8606       if (!sym->has_dynsym_index())
8607         {
8608           ++got_sym_count;
8609           sym->set_dynsym_index(index);
8610           ++index;
8611           syms->push_back(sym);
8612           dynpool->add(sym->name(), false, NULL);
8613
8614           // If the symbol is defined in a dynamic object and is
8615           // referenced in a regular object, then mark the dynamic
8616           // object as needed.  This is used to implement --as-needed.
8617           if (sym->is_from_dynobj() && sym->in_reg())
8618             sym->object()->set_is_needed();
8619         }
8620     }
8621
8622   // Set index of the first symbol that has .got entry.
8623   this->got_->set_first_global_got_dynsym_index(
8624     got_sym_count > 0 ? index - got_sym_count : -1U);
8625
8626   if (this->mips_stubs_ != NULL)
8627     this->mips_stubs_->set_dynsym_count(index);
8628
8629   return index;
8630 }
8631
8632 // Create a PLT entry for a global symbol referenced by r_type relocation.
8633
8634 template<int size, bool big_endian>
8635 void
8636 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8637                                               Layout* layout,
8638                                               Mips_symbol<size>* gsym,
8639                                               unsigned int r_type)
8640 {
8641   if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8642     return;
8643
8644   if (this->plt_ == NULL)
8645     {
8646       // Create the GOT section first.
8647       this->got_section(symtab, layout);
8648
8649       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8650       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8651                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8652                                       this->got_plt_, ORDER_DATA, false);
8653
8654       // The first two entries are reserved.
8655       this->got_plt_->set_current_data_size(2 * size/8);
8656
8657       this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8658                                                               this->got_plt_,
8659                                                               this);
8660       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8661                                       (elfcpp::SHF_ALLOC
8662                                        | elfcpp::SHF_EXECINSTR),
8663                                       this->plt_, ORDER_PLT, false);
8664
8665       // Make the sh_info field of .rel.plt point to .plt.
8666       Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
8667       rel_plt_os->set_info_section(this->plt_->output_section());
8668     }
8669
8670   this->plt_->add_entry(gsym, r_type);
8671 }
8672
8673
8674 // Get the .MIPS.stubs section, creating it if necessary.
8675
8676 template<int size, bool big_endian>
8677 Mips_output_data_mips_stubs<size, big_endian>*
8678 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8679 {
8680   if (this->mips_stubs_ == NULL)
8681     {
8682       this->mips_stubs_ =
8683         new Mips_output_data_mips_stubs<size, big_endian>(this);
8684       layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8685                                       (elfcpp::SHF_ALLOC
8686                                        | elfcpp::SHF_EXECINSTR),
8687                                       this->mips_stubs_, ORDER_PLT, false);
8688     }
8689   return this->mips_stubs_;
8690 }
8691
8692 // Get the LA25 stub section, creating it if necessary.
8693
8694 template<int size, bool big_endian>
8695 Mips_output_data_la25_stub<size, big_endian>*
8696 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8697 {
8698   if (this->la25_stub_ == NULL)
8699     {
8700       this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8701       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8702                                       (elfcpp::SHF_ALLOC
8703                                        | elfcpp::SHF_EXECINSTR),
8704                                       this->la25_stub_, ORDER_TEXT, false);
8705     }
8706   return this->la25_stub_;
8707 }
8708
8709 // Process the relocations to determine unreferenced sections for
8710 // garbage collection.
8711
8712 template<int size, bool big_endian>
8713 void
8714 Target_mips<size, big_endian>::gc_process_relocs(
8715                         Symbol_table* symtab,
8716                         Layout* layout,
8717                         Sized_relobj_file<size, big_endian>* object,
8718                         unsigned int data_shndx,
8719                         unsigned int sh_type,
8720                         const unsigned char* prelocs,
8721                         size_t reloc_count,
8722                         Output_section* output_section,
8723                         bool needs_special_offset_handling,
8724                         size_t local_symbol_count,
8725                         const unsigned char* plocal_symbols)
8726 {
8727   typedef Target_mips<size, big_endian> Mips;
8728
8729   if (sh_type == elfcpp::SHT_REL)
8730     {
8731       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8732           Classify_reloc;
8733
8734       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8735         symtab,
8736         layout,
8737         this,
8738         object,
8739         data_shndx,
8740         prelocs,
8741         reloc_count,
8742         output_section,
8743         needs_special_offset_handling,
8744         local_symbol_count,
8745         plocal_symbols);
8746     }
8747   else if (sh_type == elfcpp::SHT_RELA)
8748     {
8749       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8750           Classify_reloc;
8751
8752       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8753         symtab,
8754         layout,
8755         this,
8756         object,
8757         data_shndx,
8758         prelocs,
8759         reloc_count,
8760         output_section,
8761         needs_special_offset_handling,
8762         local_symbol_count,
8763         plocal_symbols);
8764     }
8765   else
8766     gold_unreachable();
8767 }
8768
8769 // Scan relocations for a section.
8770
8771 template<int size, bool big_endian>
8772 void
8773 Target_mips<size, big_endian>::scan_relocs(
8774                         Symbol_table* symtab,
8775                         Layout* layout,
8776                         Sized_relobj_file<size, big_endian>* object,
8777                         unsigned int data_shndx,
8778                         unsigned int sh_type,
8779                         const unsigned char* prelocs,
8780                         size_t reloc_count,
8781                         Output_section* output_section,
8782                         bool needs_special_offset_handling,
8783                         size_t local_symbol_count,
8784                         const unsigned char* plocal_symbols)
8785 {
8786   typedef Target_mips<size, big_endian> Mips;
8787
8788   if (sh_type == elfcpp::SHT_REL)
8789     {
8790       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8791           Classify_reloc;
8792
8793       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8794         symtab,
8795         layout,
8796         this,
8797         object,
8798         data_shndx,
8799         prelocs,
8800         reloc_count,
8801         output_section,
8802         needs_special_offset_handling,
8803         local_symbol_count,
8804         plocal_symbols);
8805     }
8806   else if (sh_type == elfcpp::SHT_RELA)
8807     {
8808       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8809           Classify_reloc;
8810
8811       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8812         symtab,
8813         layout,
8814         this,
8815         object,
8816         data_shndx,
8817         prelocs,
8818         reloc_count,
8819         output_section,
8820         needs_special_offset_handling,
8821         local_symbol_count,
8822         plocal_symbols);
8823     }
8824 }
8825
8826 template<int size, bool big_endian>
8827 bool
8828 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8829 {
8830   return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8831           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8832           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8833           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8834           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8835           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8836           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
8837           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
8838 }
8839
8840 // Return the MACH for a MIPS e_flags value.
8841 template<int size, bool big_endian>
8842 unsigned int
8843 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8844 {
8845   switch (flags & elfcpp::EF_MIPS_MACH)
8846     {
8847     case elfcpp::E_MIPS_MACH_3900:
8848       return mach_mips3900;
8849
8850     case elfcpp::E_MIPS_MACH_4010:
8851       return mach_mips4010;
8852
8853     case elfcpp::E_MIPS_MACH_4100:
8854       return mach_mips4100;
8855
8856     case elfcpp::E_MIPS_MACH_4111:
8857       return mach_mips4111;
8858
8859     case elfcpp::E_MIPS_MACH_4120:
8860       return mach_mips4120;
8861
8862     case elfcpp::E_MIPS_MACH_4650:
8863       return mach_mips4650;
8864
8865     case elfcpp::E_MIPS_MACH_5400:
8866       return mach_mips5400;
8867
8868     case elfcpp::E_MIPS_MACH_5500:
8869       return mach_mips5500;
8870
8871     case elfcpp::E_MIPS_MACH_5900:
8872       return mach_mips5900;
8873
8874     case elfcpp::E_MIPS_MACH_9000:
8875       return mach_mips9000;
8876
8877     case elfcpp::E_MIPS_MACH_SB1:
8878       return mach_mips_sb1;
8879
8880     case elfcpp::E_MIPS_MACH_LS2E:
8881       return mach_mips_loongson_2e;
8882
8883     case elfcpp::E_MIPS_MACH_LS2F:
8884       return mach_mips_loongson_2f;
8885
8886     case elfcpp::E_MIPS_MACH_LS3A:
8887       return mach_mips_loongson_3a;
8888
8889     case elfcpp::E_MIPS_MACH_OCTEON3:
8890       return mach_mips_octeon3;
8891
8892     case elfcpp::E_MIPS_MACH_OCTEON2:
8893       return mach_mips_octeon2;
8894
8895     case elfcpp::E_MIPS_MACH_OCTEON:
8896       return mach_mips_octeon;
8897
8898     case elfcpp::E_MIPS_MACH_XLR:
8899       return mach_mips_xlr;
8900
8901     default:
8902       switch (flags & elfcpp::EF_MIPS_ARCH)
8903         {
8904         default:
8905         case elfcpp::E_MIPS_ARCH_1:
8906           return mach_mips3000;
8907
8908         case elfcpp::E_MIPS_ARCH_2:
8909           return mach_mips6000;
8910
8911         case elfcpp::E_MIPS_ARCH_3:
8912           return mach_mips4000;
8913
8914         case elfcpp::E_MIPS_ARCH_4:
8915           return mach_mips8000;
8916
8917         case elfcpp::E_MIPS_ARCH_5:
8918           return mach_mips5;
8919
8920         case elfcpp::E_MIPS_ARCH_32:
8921           return mach_mipsisa32;
8922
8923         case elfcpp::E_MIPS_ARCH_64:
8924           return mach_mipsisa64;
8925
8926         case elfcpp::E_MIPS_ARCH_32R2:
8927           return mach_mipsisa32r2;
8928
8929         case elfcpp::E_MIPS_ARCH_32R6:
8930           return mach_mipsisa32r6;
8931
8932         case elfcpp::E_MIPS_ARCH_64R2:
8933           return mach_mipsisa64r2;
8934
8935         case elfcpp::E_MIPS_ARCH_64R6:
8936           return mach_mipsisa64r6;
8937         }
8938     }
8939
8940   return 0;
8941 }
8942
8943 // Return the MACH for each .MIPS.abiflags ISA Extension.
8944
8945 template<int size, bool big_endian>
8946 unsigned int
8947 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8948 {
8949   switch (isa_ext)
8950     {
8951     case elfcpp::AFL_EXT_3900:
8952       return mach_mips3900;
8953
8954     case elfcpp::AFL_EXT_4010:
8955       return mach_mips4010;
8956
8957     case elfcpp::AFL_EXT_4100:
8958       return mach_mips4100;
8959
8960     case elfcpp::AFL_EXT_4111:
8961       return mach_mips4111;
8962
8963     case elfcpp::AFL_EXT_4120:
8964       return mach_mips4120;
8965
8966     case elfcpp::AFL_EXT_4650:
8967       return mach_mips4650;
8968
8969     case elfcpp::AFL_EXT_5400:
8970       return mach_mips5400;
8971
8972     case elfcpp::AFL_EXT_5500:
8973       return mach_mips5500;
8974
8975     case elfcpp::AFL_EXT_5900:
8976       return mach_mips5900;
8977
8978     case elfcpp::AFL_EXT_10000:
8979       return mach_mips10000;
8980
8981     case elfcpp::AFL_EXT_LOONGSON_2E:
8982       return mach_mips_loongson_2e;
8983
8984     case elfcpp::AFL_EXT_LOONGSON_2F:
8985       return mach_mips_loongson_2f;
8986
8987     case elfcpp::AFL_EXT_LOONGSON_3A:
8988       return mach_mips_loongson_3a;
8989
8990     case elfcpp::AFL_EXT_SB1:
8991       return mach_mips_sb1;
8992
8993     case elfcpp::AFL_EXT_OCTEON:
8994       return mach_mips_octeon;
8995
8996     case elfcpp::AFL_EXT_OCTEONP:
8997       return mach_mips_octeonp;
8998
8999     case elfcpp::AFL_EXT_OCTEON2:
9000       return mach_mips_octeon2;
9001
9002     case elfcpp::AFL_EXT_XLR:
9003       return mach_mips_xlr;
9004
9005     default:
9006       return mach_mips3000;
9007     }
9008 }
9009
9010 // Return the .MIPS.abiflags value representing each ISA Extension.
9011
9012 template<int size, bool big_endian>
9013 unsigned int
9014 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
9015 {
9016   switch (mips_mach)
9017     {
9018     case mach_mips3900:
9019       return elfcpp::AFL_EXT_3900;
9020
9021     case mach_mips4010:
9022       return elfcpp::AFL_EXT_4010;
9023
9024     case mach_mips4100:
9025       return elfcpp::AFL_EXT_4100;
9026
9027     case mach_mips4111:
9028       return elfcpp::AFL_EXT_4111;
9029
9030     case mach_mips4120:
9031       return elfcpp::AFL_EXT_4120;
9032
9033     case mach_mips4650:
9034       return elfcpp::AFL_EXT_4650;
9035
9036     case mach_mips5400:
9037       return elfcpp::AFL_EXT_5400;
9038
9039     case mach_mips5500:
9040       return elfcpp::AFL_EXT_5500;
9041
9042     case mach_mips5900:
9043       return elfcpp::AFL_EXT_5900;
9044
9045     case mach_mips10000:
9046       return elfcpp::AFL_EXT_10000;
9047
9048     case mach_mips_loongson_2e:
9049       return elfcpp::AFL_EXT_LOONGSON_2E;
9050
9051     case mach_mips_loongson_2f:
9052       return elfcpp::AFL_EXT_LOONGSON_2F;
9053
9054     case mach_mips_loongson_3a:
9055       return elfcpp::AFL_EXT_LOONGSON_3A;
9056
9057     case mach_mips_sb1:
9058       return elfcpp::AFL_EXT_SB1;
9059
9060     case mach_mips_octeon:
9061       return elfcpp::AFL_EXT_OCTEON;
9062
9063     case mach_mips_octeonp:
9064       return elfcpp::AFL_EXT_OCTEONP;
9065
9066     case mach_mips_octeon3:
9067       return elfcpp::AFL_EXT_OCTEON3;
9068
9069     case mach_mips_octeon2:
9070       return elfcpp::AFL_EXT_OCTEON2;
9071
9072     case mach_mips_xlr:
9073       return elfcpp::AFL_EXT_XLR;
9074
9075     default:
9076       return 0;
9077     }
9078 }
9079
9080 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
9081
9082 template<int size, bool big_endian>
9083 void
9084 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9085     elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9086 {
9087   int new_isa = 0;
9088   switch (e_flags & elfcpp::EF_MIPS_ARCH)
9089     {
9090     case elfcpp::E_MIPS_ARCH_1:
9091       new_isa = this->level_rev(1, 0);
9092       break;
9093     case elfcpp::E_MIPS_ARCH_2:
9094       new_isa = this->level_rev(2, 0);
9095       break;
9096     case elfcpp::E_MIPS_ARCH_3:
9097       new_isa = this->level_rev(3, 0);
9098       break;
9099     case elfcpp::E_MIPS_ARCH_4:
9100       new_isa = this->level_rev(4, 0);
9101       break;
9102     case elfcpp::E_MIPS_ARCH_5:
9103       new_isa = this->level_rev(5, 0);
9104       break;
9105     case elfcpp::E_MIPS_ARCH_32:
9106       new_isa = this->level_rev(32, 1);
9107       break;
9108     case elfcpp::E_MIPS_ARCH_32R2:
9109       new_isa = this->level_rev(32, 2);
9110       break;
9111     case elfcpp::E_MIPS_ARCH_32R6:
9112       new_isa = this->level_rev(32, 6);
9113       break;
9114     case elfcpp::E_MIPS_ARCH_64:
9115       new_isa = this->level_rev(64, 1);
9116       break;
9117     case elfcpp::E_MIPS_ARCH_64R2:
9118       new_isa = this->level_rev(64, 2);
9119       break;
9120     case elfcpp::E_MIPS_ARCH_64R6:
9121       new_isa = this->level_rev(64, 6);
9122       break;
9123     default:
9124       gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9125                  this->elf_mips_mach_name(e_flags));
9126     }
9127
9128   if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9129     {
9130       // Decode a single value into level and revision.
9131       abiflags->isa_level = new_isa >> 3;
9132       abiflags->isa_rev = new_isa & 0x7;
9133     }
9134
9135   // Update the isa_ext if needed.
9136   if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9137       this->elf_mips_mach(e_flags)))
9138     abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9139 }
9140
9141 // Infer the content of the ABI flags based on the elf header.
9142
9143 template<int size, bool big_endian>
9144 void
9145 Target_mips<size, big_endian>::infer_abiflags(
9146     Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9147 {
9148   const Attributes_section_data* pasd = relobj->attributes_section_data();
9149   int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9150   elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9151
9152   this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9153   if (pasd != NULL)
9154     {
9155       // Read fp_abi from the .gnu.attribute section.
9156       const Object_attribute* attr =
9157         pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9158       attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9159     }
9160
9161   abiflags->fp_abi = attr_fp_abi;
9162   abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9163   abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9164   abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9165                                                        : elfcpp::AFL_REG_64;
9166
9167   if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9168       || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9169       || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9170       && abiflags->gpr_size == elfcpp::AFL_REG_32))
9171     abiflags->cpr1_size = elfcpp::AFL_REG_32;
9172   else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9173            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9174            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9175     abiflags->cpr1_size = elfcpp::AFL_REG_64;
9176
9177   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9178     abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9179   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9180     abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9181   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9182     abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9183
9184   if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9185       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9186       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9187       && abiflags->isa_level >= 32
9188       && abiflags->isa_ext != elfcpp::AFL_EXT_LOONGSON_3A)
9189     abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9190 }
9191
9192 // Create abiflags from elf header or from .MIPS.abiflags section.
9193
9194 template<int size, bool big_endian>
9195 void
9196 Target_mips<size, big_endian>::create_abiflags(
9197     Mips_relobj<size, big_endian>* relobj,
9198     Mips_abiflags<big_endian>* abiflags)
9199 {
9200   Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9201   Mips_abiflags<big_endian> header_abiflags;
9202
9203   this->infer_abiflags(relobj, &header_abiflags);
9204
9205   if (sec_abiflags == NULL)
9206     {
9207       // If there is no input .MIPS.abiflags section, use abiflags created
9208       // from elf header.
9209       *abiflags = header_abiflags;
9210       return;
9211     }
9212
9213   this->has_abiflags_section_ = true;
9214
9215   // It is not possible to infer the correct ISA revision for R3 or R5
9216   // so drop down to R2 for the checks.
9217   unsigned char isa_rev = sec_abiflags->isa_rev;
9218   if (isa_rev == 3 || isa_rev == 5)
9219     isa_rev = 2;
9220
9221   // Check compatibility between abiflags created from elf header
9222   // and abiflags from .MIPS.abiflags section in this object file.
9223   if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9224       < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9225     gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9226                  relobj->name().c_str());
9227   if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9228       && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9229     gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9230                    ".MIPS.abiflags"), relobj->name().c_str());
9231   if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9232     gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9233                  relobj->name().c_str());
9234   // The isa_ext is allowed to be an extension of what can be inferred
9235   // from e_flags.
9236   if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9237                                this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9238     gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9239                    ".MIPS.abiflags"), relobj->name().c_str());
9240   if (sec_abiflags->flags2 != 0)
9241     gold_warning(_("%s: Unexpected flag in the flags2 field of "
9242                    ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9243                                              sec_abiflags->flags2);
9244   // Use abiflags from .MIPS.abiflags section.
9245   *abiflags = *sec_abiflags;
9246 }
9247
9248 // Return the meaning of fp_abi, or "unknown" if not known.
9249
9250 template<int size, bool big_endian>
9251 const char*
9252 Target_mips<size, big_endian>::fp_abi_string(int fp)
9253 {
9254   switch (fp)
9255     {
9256     case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9257       return "-mdouble-float";
9258     case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9259       return "-msingle-float";
9260     case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9261       return "-msoft-float";
9262     case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9263       return _("-mips32r2 -mfp64 (12 callee-saved)");
9264     case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9265       return "-mfpxx";
9266     case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9267       return "-mgp32 -mfp64";
9268     case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9269       return "-mgp32 -mfp64 -mno-odd-spreg";
9270     default:
9271       return "unknown";
9272     }
9273 }
9274
9275 // Select fp_abi.
9276
9277 template<int size, bool big_endian>
9278 int
9279 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9280                                              int out_fp)
9281 {
9282   if (in_fp == out_fp)
9283     return out_fp;
9284
9285   if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9286     return in_fp;
9287   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9288            && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9289                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9290                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9291     return in_fp;
9292   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9293            && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9294                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9295                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9296     return out_fp; // Keep the current setting.
9297   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9298            && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9299     return in_fp;
9300   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9301            && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9302     return out_fp; // Keep the current setting.
9303   else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9304     gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9305                  fp_abi_string(in_fp), fp_abi_string(out_fp));
9306   return out_fp;
9307 }
9308
9309 // Merge attributes from input object.
9310
9311 template<int size, bool big_endian>
9312 void
9313 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9314     const Attributes_section_data* pasd)
9315 {
9316   // Return if there is no attributes section data.
9317   if (pasd == NULL)
9318     return;
9319
9320   // If output has no object attributes, just copy.
9321   if (this->attributes_section_data_ == NULL)
9322     {
9323       this->attributes_section_data_ = new Attributes_section_data(*pasd);
9324       return;
9325     }
9326
9327   Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9328       Object_attribute::OBJ_ATTR_GNU);
9329
9330   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9331   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9332
9333   // Merge Tag_compatibility attributes and any common GNU ones.
9334   this->attributes_section_data_->merge(name.c_str(), pasd);
9335 }
9336
9337 // Merge abiflags from input object.
9338
9339 template<int size, bool big_endian>
9340 void
9341 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9342     Mips_abiflags<big_endian>* in_abiflags)
9343 {
9344   // If output has no abiflags, just copy.
9345   if (this->abiflags_ == NULL)
9346   {
9347     this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9348     return;
9349   }
9350
9351   this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9352                                                 this->abiflags_->fp_abi);
9353
9354   // Merge abiflags.
9355   this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9356                                         in_abiflags->isa_level);
9357   this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9358                                       in_abiflags->isa_rev);
9359   this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9360                                        in_abiflags->gpr_size);
9361   this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9362                                         in_abiflags->cpr1_size);
9363   this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9364                                         in_abiflags->cpr2_size);
9365   this->abiflags_->ases |= in_abiflags->ases;
9366   this->abiflags_->flags1 |= in_abiflags->flags1;
9367 }
9368
9369 // Check whether machine EXTENSION is an extension of machine BASE.
9370 template<int size, bool big_endian>
9371 bool
9372 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9373                                                  unsigned int extension)
9374 {
9375   if (extension == base)
9376     return true;
9377
9378   if ((base == mach_mipsisa32)
9379       && this->mips_mach_extends(mach_mipsisa64, extension))
9380     return true;
9381
9382   if ((base == mach_mipsisa32r2)
9383       && this->mips_mach_extends(mach_mipsisa64r2, extension))
9384     return true;
9385
9386   for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9387     if (extension == this->mips_mach_extensions_[i].first)
9388       {
9389         extension = this->mips_mach_extensions_[i].second;
9390         if (extension == base)
9391           return true;
9392       }
9393
9394   return false;
9395 }
9396
9397 // Merge file header flags from input object.
9398
9399 template<int size, bool big_endian>
9400 void
9401 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9402                                                  elfcpp::Elf_Word in_flags)
9403 {
9404   // If flags are not set yet, just copy them.
9405   if (!this->are_processor_specific_flags_set())
9406     {
9407       this->set_processor_specific_flags(in_flags);
9408       this->mach_ = this->elf_mips_mach(in_flags);
9409       return;
9410     }
9411
9412   elfcpp::Elf_Word new_flags = in_flags;
9413   elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9414   elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9415   merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9416
9417   // Check flag compatibility.
9418   new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9419   old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9420
9421   // Some IRIX 6 BSD-compatibility objects have this bit set.  It
9422   // doesn't seem to matter.
9423   new_flags &= ~elfcpp::EF_MIPS_XGOT;
9424   old_flags &= ~elfcpp::EF_MIPS_XGOT;
9425
9426   // MIPSpro generates ucode info in n64 objects.  Again, we should
9427   // just be able to ignore this.
9428   new_flags &= ~elfcpp::EF_MIPS_UCODE;
9429   old_flags &= ~elfcpp::EF_MIPS_UCODE;
9430
9431   if (new_flags == old_flags)
9432     {
9433       this->set_processor_specific_flags(merged_flags);
9434       return;
9435     }
9436
9437   if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9438       != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9439     gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9440                  name.c_str());
9441
9442   if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9443     merged_flags |= elfcpp::EF_MIPS_CPIC;
9444   if (!(new_flags & elfcpp::EF_MIPS_PIC))
9445     merged_flags &= ~elfcpp::EF_MIPS_PIC;
9446
9447   new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9448   old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9449
9450   // Compare the ISAs.
9451   if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9452     gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9453   else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9454     {
9455       // Output ISA isn't the same as, or an extension of, input ISA.
9456       if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9457         {
9458           // Copy the architecture info from input object to output.  Also copy
9459           // the 32-bit flag (if set) so that we continue to recognise
9460           // output as a 32-bit binary.
9461           this->mach_ = this->elf_mips_mach(in_flags);
9462           merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9463           merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9464                            | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9465
9466           // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9467           this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9468
9469           // Copy across the ABI flags if output doesn't use them
9470           // and if that was what caused us to treat input object as 32-bit.
9471           if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9472               && this->mips_32bit_flags(new_flags)
9473               && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9474             merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9475         }
9476       else
9477         // The ISAs aren't compatible.
9478         gold_error(_("%s: linking %s module with previous %s modules"),
9479                    name.c_str(), this->elf_mips_mach_name(in_flags),
9480                    this->elf_mips_mach_name(merged_flags));
9481     }
9482
9483   new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9484                 | elfcpp::EF_MIPS_32BITMODE));
9485   old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9486                 | elfcpp::EF_MIPS_32BITMODE));
9487
9488   // Compare ABIs.
9489   if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9490     {
9491       // Only error if both are set (to different values).
9492       if ((new_flags & elfcpp::EF_MIPS_ABI)
9493            && (old_flags & elfcpp::EF_MIPS_ABI))
9494         gold_error(_("%s: ABI mismatch: linking %s module with "
9495                      "previous %s modules"), name.c_str(),
9496                    this->elf_mips_abi_name(in_flags),
9497                    this->elf_mips_abi_name(merged_flags));
9498
9499       new_flags &= ~elfcpp::EF_MIPS_ABI;
9500       old_flags &= ~elfcpp::EF_MIPS_ABI;
9501     }
9502
9503   // Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
9504   // and allow arbitrary mixing of the remaining ASEs (retain the union).
9505   if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9506       != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9507     {
9508       int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9509       int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9510       int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9511       int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9512       int micro_mis = old_m16 && new_micro;
9513       int m16_mis = old_micro && new_m16;
9514
9515       if (m16_mis || micro_mis)
9516         gold_error(_("%s: ASE mismatch: linking %s module with "
9517                      "previous %s modules"), name.c_str(),
9518                    m16_mis ? "MIPS16" : "microMIPS",
9519                    m16_mis ? "microMIPS" : "MIPS16");
9520
9521       merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9522
9523       new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9524       old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9525     }
9526
9527   // Compare NaN encodings.
9528   if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9529     {
9530       gold_error(_("%s: linking %s module with previous %s modules"),
9531                  name.c_str(),
9532                  (new_flags & elfcpp::EF_MIPS_NAN2008
9533                   ? "-mnan=2008" : "-mnan=legacy"),
9534                  (old_flags & elfcpp::EF_MIPS_NAN2008
9535                   ? "-mnan=2008" : "-mnan=legacy"));
9536
9537       new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9538       old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9539     }
9540
9541   // Compare FP64 state.
9542   if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9543     {
9544       gold_error(_("%s: linking %s module with previous %s modules"),
9545                  name.c_str(),
9546                  (new_flags & elfcpp::EF_MIPS_FP64
9547                   ? "-mfp64" : "-mfp32"),
9548                  (old_flags & elfcpp::EF_MIPS_FP64
9549                   ? "-mfp64" : "-mfp32"));
9550
9551       new_flags &= ~elfcpp::EF_MIPS_FP64;
9552       old_flags &= ~elfcpp::EF_MIPS_FP64;
9553     }
9554
9555   // Warn about any other mismatches.
9556   if (new_flags != old_flags)
9557     gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9558                  "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9559
9560   this->set_processor_specific_flags(merged_flags);
9561 }
9562
9563 // Adjust ELF file header.
9564
9565 template<int size, bool big_endian>
9566 void
9567 Target_mips<size, big_endian>::do_adjust_elf_header(
9568     unsigned char* view,
9569     int len)
9570 {
9571   gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9572
9573   elfcpp::Ehdr<size, big_endian> ehdr(view);
9574   unsigned char e_ident[elfcpp::EI_NIDENT];
9575   elfcpp::Elf_Word flags = this->processor_specific_flags();
9576   memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9577
9578   unsigned char ei_abiversion = 0;
9579   elfcpp::Elf_Half type = ehdr.get_e_type();
9580   if (type == elfcpp::ET_EXEC
9581       && parameters->options().copyreloc()
9582       && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9583           == elfcpp::EF_MIPS_CPIC)
9584     ei_abiversion = 1;
9585
9586   if (this->abiflags_ != NULL
9587       && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9588           || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9589     ei_abiversion = 3;
9590
9591   e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9592   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9593   oehdr.put_e_ident(e_ident);
9594
9595   if (this->entry_symbol_is_compressed_)
9596     oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9597 }
9598
9599 // do_make_elf_object to override the same function in the base class.
9600 // We need to use a target-specific sub-class of
9601 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9602 // Hence we need to have our own ELF object creation.
9603
9604 template<int size, bool big_endian>
9605 Object*
9606 Target_mips<size, big_endian>::do_make_elf_object(
9607     const std::string& name,
9608     Input_file* input_file,
9609     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9610 {
9611   int et = ehdr.get_e_type();
9612   // ET_EXEC files are valid input for --just-symbols/-R,
9613   // and we treat them as relocatable objects.
9614   if (et == elfcpp::ET_REL
9615       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9616     {
9617       Mips_relobj<size, big_endian>* obj =
9618         new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9619       obj->setup();
9620       return obj;
9621     }
9622   else if (et == elfcpp::ET_DYN)
9623     {
9624       // TODO(sasa): Should we create Mips_dynobj?
9625       return Target::do_make_elf_object(name, input_file, offset, ehdr);
9626     }
9627   else
9628     {
9629       gold_error(_("%s: unsupported ELF file type %d"),
9630                  name.c_str(), et);
9631       return NULL;
9632     }
9633 }
9634
9635 // Finalize the sections.
9636
9637 template <int size, bool big_endian>
9638 void
9639 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9640                                         const Input_objects* input_objects,
9641                                         Symbol_table* symtab)
9642 {
9643   const bool relocatable = parameters->options().relocatable();
9644
9645   // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9646   // DT_FINI have correct values.
9647   Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9648       symtab->lookup(parameters->options().init()));
9649   if (init != NULL && (init->is_mips16() || init->is_micromips()))
9650     init->set_value(init->value() | 1);
9651   Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9652       symtab->lookup(parameters->options().fini()));
9653   if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9654     fini->set_value(fini->value() | 1);
9655
9656   // Check whether the entry symbol is mips16 or micromips.  This is needed to
9657   // adjust entry address in ELF header.
9658   Mips_symbol<size>* entry =
9659     static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9660   this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9661                                        || entry->is_micromips()));
9662
9663   if (!parameters->doing_static_link()
9664       && (strcmp(parameters->options().hash_style(), "gnu") == 0
9665           || strcmp(parameters->options().hash_style(), "both") == 0))
9666     {
9667       // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9668       // ways.  .gnu.hash needs symbols to be grouped by hash code whereas the
9669       // MIPS ABI requires a mapping between the GOT and the symbol table.
9670       gold_error(".gnu.hash is incompatible with the MIPS ABI");
9671     }
9672
9673   // Check whether the final section that was scanned has HI16 or GOT16
9674   // relocations without the corresponding LO16 part.
9675   if (this->got16_addends_.size() > 0)
9676       gold_error("Can't find matching LO16 reloc");
9677
9678   Valtype gprmask = 0;
9679   Valtype cprmask1 = 0;
9680   Valtype cprmask2 = 0;
9681   Valtype cprmask3 = 0;
9682   Valtype cprmask4 = 0;
9683   bool has_reginfo_section = false;
9684
9685   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9686        p != input_objects->relobj_end();
9687        ++p)
9688     {
9689       Mips_relobj<size, big_endian>* relobj =
9690         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9691
9692       // Check for any mips16 stub sections that we can discard.
9693       if (!relocatable)
9694         relobj->discard_mips16_stub_sections(symtab);
9695
9696       if (!relobj->merge_processor_specific_data())
9697         continue;
9698
9699       // Merge .reginfo contents of input objects.
9700       if (relobj->has_reginfo_section())
9701         {
9702           has_reginfo_section = true;
9703           gprmask |= relobj->gprmask();
9704           cprmask1 |= relobj->cprmask1();
9705           cprmask2 |= relobj->cprmask2();
9706           cprmask3 |= relobj->cprmask3();
9707           cprmask4 |= relobj->cprmask4();
9708         }
9709
9710       // Merge processor specific flags.
9711       Mips_abiflags<big_endian> in_abiflags;
9712
9713       this->create_abiflags(relobj, &in_abiflags);
9714       this->merge_obj_e_flags(relobj->name(),
9715                               relobj->processor_specific_flags());
9716       this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9717       this->merge_obj_attributes(relobj->name(),
9718                                  relobj->attributes_section_data());
9719     }
9720
9721   // Create a .gnu.attributes section if we have merged any attributes
9722   // from inputs.
9723   if (this->attributes_section_data_ != NULL)
9724     {
9725       Output_attributes_section_data* attributes_section =
9726         new Output_attributes_section_data(*this->attributes_section_data_);
9727       layout->add_output_section_data(".gnu.attributes",
9728                                       elfcpp::SHT_GNU_ATTRIBUTES, 0,
9729                                       attributes_section, ORDER_INVALID, false);
9730     }
9731
9732   // Create .MIPS.abiflags output section if there is an input section.
9733   if (this->has_abiflags_section_)
9734     {
9735       Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9736         new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9737
9738       Output_section* os =
9739         layout->add_output_section_data(".MIPS.abiflags",
9740                                         elfcpp::SHT_MIPS_ABIFLAGS,
9741                                         elfcpp::SHF_ALLOC,
9742                                         abiflags_section, ORDER_INVALID, false);
9743
9744       if (!relocatable && os != NULL)
9745         {
9746           Output_segment* abiflags_segment =
9747             layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9748           abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9749         }
9750     }
9751
9752   if (has_reginfo_section && !parameters->options().gc_sections())
9753     {
9754       // Create .reginfo output section.
9755       Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9756         new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9757                                                           cprmask1, cprmask2,
9758                                                           cprmask3, cprmask4);
9759
9760       Output_section* os =
9761         layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9762                                         elfcpp::SHF_ALLOC, reginfo_section,
9763                                         ORDER_INVALID, false);
9764
9765       if (!relocatable && os != NULL)
9766         {
9767           Output_segment* reginfo_segment =
9768             layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9769                                         elfcpp::PF_R);
9770           reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9771         }
9772     }
9773
9774   if (this->plt_ != NULL)
9775     {
9776       // Set final PLT offsets for symbols.
9777       this->plt_section()->set_plt_offsets();
9778
9779       // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9780       // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9781       // there are no standard PLT entries present.
9782       unsigned char nonvis = 0;
9783       if (this->is_output_micromips()
9784           && !this->plt_section()->has_standard_entries())
9785         nonvis = elfcpp::STO_MICROMIPS >> 2;
9786       symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9787                                     Symbol_table::PREDEFINED,
9788                                     this->plt_,
9789                                     0, 0, elfcpp::STT_FUNC,
9790                                     elfcpp::STB_LOCAL,
9791                                     elfcpp::STV_DEFAULT, nonvis,
9792                                     false, false);
9793     }
9794
9795   if (this->mips_stubs_ != NULL)
9796     {
9797       // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9798       unsigned char nonvis = 0;
9799       if (this->is_output_micromips())
9800         nonvis = elfcpp::STO_MICROMIPS >> 2;
9801       symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9802                                     Symbol_table::PREDEFINED,
9803                                     this->mips_stubs_,
9804                                     0, 0, elfcpp::STT_FUNC,
9805                                     elfcpp::STB_LOCAL,
9806                                     elfcpp::STV_DEFAULT, nonvis,
9807                                     false, false);
9808     }
9809
9810   if (!relocatable && !parameters->doing_static_link())
9811     // In case there is no .got section, create one.
9812     this->got_section(symtab, layout);
9813
9814   // Emit any relocs we saved in an attempt to avoid generating COPY
9815   // relocs.
9816   if (this->copy_relocs_.any_saved_relocs())
9817     this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9818                                  this);
9819
9820   // Set _gp value.
9821   this->set_gp(layout, symtab);
9822
9823   // Emit dynamic relocs.
9824   for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9825        p != this->dyn_relocs_.end();
9826        ++p)
9827     p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9828
9829   if (this->has_got_section())
9830     this->got_section()->lay_out_got(layout, symtab, input_objects);
9831
9832   if (this->mips_stubs_ != NULL)
9833     this->mips_stubs_->set_needs_dynsym_value();
9834
9835   // Check for functions that might need $25 to be valid on entry.
9836   // TODO(sasa): Can we do this without iterating over all symbols?
9837   typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9838   symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9839                                                                symtab));
9840
9841   // Add NULL segment.
9842   if (!relocatable)
9843     layout->make_output_segment(elfcpp::PT_NULL, 0);
9844
9845   // Fill in some more dynamic tags.
9846   // TODO(sasa): Add more dynamic tags.
9847   const Reloc_section* rel_plt = (this->plt_ == NULL
9848                                   ? NULL : this->plt_->rel_plt());
9849   layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9850                                   this->rel_dyn_, true, false);
9851
9852   Output_data_dynamic* const odyn = layout->dynamic_data();
9853   if (odyn != NULL
9854       && !relocatable
9855       && !parameters->doing_static_link())
9856   {
9857     unsigned int d_val;
9858     // This element holds a 32-bit version id for the Runtime
9859     // Linker Interface.  This will start at integer value 1.
9860     d_val = 0x01;
9861     odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9862
9863     // Dynamic flags
9864     d_val = elfcpp::RHF_NOTPOT;
9865     odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9866
9867     // Save layout for using when emitting custom dynamic tags.
9868     this->layout_ = layout;
9869
9870     // This member holds the base address of the segment.
9871     odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9872
9873     // This member holds the number of entries in the .dynsym section.
9874     odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9875
9876     // This member holds the index of the first dynamic symbol
9877     // table entry that corresponds to an entry in the global offset table.
9878     odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9879
9880     // This member holds the number of local GOT entries.
9881     odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9882                        this->got_->get_local_gotno());
9883
9884     if (this->plt_ != NULL)
9885       // DT_MIPS_PLTGOT dynamic tag
9886       odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9887
9888     if (!parameters->options().shared())
9889       {
9890         this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9891
9892         layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9893                                         (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9894                                         this->rld_map_, ORDER_INVALID, false);
9895
9896         // __RLD_MAP will be filled in by the runtime loader to contain
9897         // a pointer to the _r_debug structure.
9898         Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9899                                             Symbol_table::PREDEFINED,
9900                                             this->rld_map_,
9901                                             0, 0, elfcpp::STT_OBJECT,
9902                                             elfcpp::STB_GLOBAL,
9903                                             elfcpp::STV_DEFAULT, 0,
9904                                             false, false);
9905
9906         if (!rld_map->is_forced_local())
9907           rld_map->set_needs_dynsym_entry();
9908
9909         if (!parameters->options().pie())
9910           // This member holds the absolute address of the debug pointer.
9911           odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9912         else
9913           // This member holds the offset to the debug pointer,
9914           // relative to the address of the tag.
9915           odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9916       }
9917   }
9918 }
9919
9920 // Get the custom dynamic tag value.
9921 template<int size, bool big_endian>
9922 unsigned int
9923 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9924 {
9925   switch (tag)
9926     {
9927     case elfcpp::DT_MIPS_BASE_ADDRESS:
9928       {
9929         // The base address of the segment.
9930         // At this point, the segment list has been sorted into final order,
9931         // so just return vaddr of the first readable PT_LOAD segment.
9932         Output_segment* seg =
9933           this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9934         gold_assert(seg != NULL);
9935         return seg->vaddr();
9936       }
9937
9938     case elfcpp::DT_MIPS_SYMTABNO:
9939       // The number of entries in the .dynsym section.
9940       return this->get_dt_mips_symtabno();
9941
9942     case elfcpp::DT_MIPS_GOTSYM:
9943       {
9944         // The index of the first dynamic symbol table entry that corresponds
9945         // to an entry in the GOT.
9946         if (this->got_->first_global_got_dynsym_index() != -1U)
9947           return this->got_->first_global_got_dynsym_index();
9948         else
9949           // In case if we don't have global GOT symbols we default to setting
9950           // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9951           return this->get_dt_mips_symtabno();
9952       }
9953
9954     case elfcpp::DT_MIPS_RLD_MAP_REL:
9955       {
9956         // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9957         // relative to the address of the tag.
9958         Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9959         unsigned int entry_offset =
9960           odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9961         gold_assert(entry_offset != -1U);
9962         return this->rld_map_->address() - (odyn->address() + entry_offset);
9963       }
9964     default:
9965       gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9966     }
9967
9968   return (unsigned int)-1;
9969 }
9970
9971 // Relocate section data.
9972
9973 template<int size, bool big_endian>
9974 void
9975 Target_mips<size, big_endian>::relocate_section(
9976                         const Relocate_info<size, big_endian>* relinfo,
9977                         unsigned int sh_type,
9978                         const unsigned char* prelocs,
9979                         size_t reloc_count,
9980                         Output_section* output_section,
9981                         bool needs_special_offset_handling,
9982                         unsigned char* view,
9983                         Mips_address address,
9984                         section_size_type view_size,
9985                         const Reloc_symbol_changes* reloc_symbol_changes)
9986 {
9987   typedef Target_mips<size, big_endian> Mips;
9988   typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9989
9990   if (sh_type == elfcpp::SHT_REL)
9991     {
9992       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9993           Classify_reloc;
9994
9995       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9996                              gold::Default_comdat_behavior, Classify_reloc>(
9997         relinfo,
9998         this,
9999         prelocs,
10000         reloc_count,
10001         output_section,
10002         needs_special_offset_handling,
10003         view,
10004         address,
10005         view_size,
10006         reloc_symbol_changes);
10007     }
10008   else if (sh_type == elfcpp::SHT_RELA)
10009     {
10010       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10011           Classify_reloc;
10012
10013       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
10014                              gold::Default_comdat_behavior, Classify_reloc>(
10015         relinfo,
10016         this,
10017         prelocs,
10018         reloc_count,
10019         output_section,
10020         needs_special_offset_handling,
10021         view,
10022         address,
10023         view_size,
10024         reloc_symbol_changes);
10025     }
10026 }
10027
10028 // Return the size of a relocation while scanning during a relocatable
10029 // link.
10030
10031 unsigned int
10032 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
10033 {
10034   switch (r_type)
10035     {
10036     case elfcpp::R_MIPS_NONE:
10037     case elfcpp::R_MIPS_TLS_DTPMOD64:
10038     case elfcpp::R_MIPS_TLS_DTPREL64:
10039     case elfcpp::R_MIPS_TLS_TPREL64:
10040       return 0;
10041
10042     case elfcpp::R_MIPS_32:
10043     case elfcpp::R_MIPS_TLS_DTPMOD32:
10044     case elfcpp::R_MIPS_TLS_DTPREL32:
10045     case elfcpp::R_MIPS_TLS_TPREL32:
10046     case elfcpp::R_MIPS_REL32:
10047     case elfcpp::R_MIPS_PC32:
10048     case elfcpp::R_MIPS_GPREL32:
10049     case elfcpp::R_MIPS_JALR:
10050     case elfcpp::R_MIPS_EH:
10051       return 4;
10052
10053     case elfcpp::R_MIPS_16:
10054     case elfcpp::R_MIPS_HI16:
10055     case elfcpp::R_MIPS_LO16:
10056     case elfcpp::R_MIPS_HIGHER:
10057     case elfcpp::R_MIPS_HIGHEST:
10058     case elfcpp::R_MIPS_GPREL16:
10059     case elfcpp::R_MIPS16_HI16:
10060     case elfcpp::R_MIPS16_LO16:
10061     case elfcpp::R_MIPS_PC16:
10062     case elfcpp::R_MIPS_PCHI16:
10063     case elfcpp::R_MIPS_PCLO16:
10064     case elfcpp::R_MIPS_GOT16:
10065     case elfcpp::R_MIPS16_GOT16:
10066     case elfcpp::R_MIPS_CALL16:
10067     case elfcpp::R_MIPS16_CALL16:
10068     case elfcpp::R_MIPS_GOT_HI16:
10069     case elfcpp::R_MIPS_CALL_HI16:
10070     case elfcpp::R_MIPS_GOT_LO16:
10071     case elfcpp::R_MIPS_CALL_LO16:
10072     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10073     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10074     case elfcpp::R_MIPS_TLS_TPREL_HI16:
10075     case elfcpp::R_MIPS_TLS_TPREL_LO16:
10076     case elfcpp::R_MIPS16_GPREL:
10077     case elfcpp::R_MIPS_GOT_DISP:
10078     case elfcpp::R_MIPS_LITERAL:
10079     case elfcpp::R_MIPS_GOT_PAGE:
10080     case elfcpp::R_MIPS_GOT_OFST:
10081     case elfcpp::R_MIPS_TLS_GD:
10082     case elfcpp::R_MIPS_TLS_LDM:
10083     case elfcpp::R_MIPS_TLS_GOTTPREL:
10084       return 2;
10085
10086     // These relocations are not byte sized
10087     case elfcpp::R_MIPS_26:
10088     case elfcpp::R_MIPS16_26:
10089     case elfcpp::R_MIPS_PC21_S2:
10090     case elfcpp::R_MIPS_PC26_S2:
10091     case elfcpp::R_MIPS_PC18_S3:
10092     case elfcpp::R_MIPS_PC19_S2:
10093       return 4;
10094
10095     case elfcpp::R_MIPS_COPY:
10096     case elfcpp::R_MIPS_JUMP_SLOT:
10097       object->error(_("unexpected reloc %u in object file"), r_type);
10098       return 0;
10099
10100     default:
10101       object->error(_("unsupported reloc %u in object file"), r_type);
10102       return 0;
10103   }
10104 }
10105
10106 // Scan the relocs during a relocatable link.
10107
10108 template<int size, bool big_endian>
10109 void
10110 Target_mips<size, big_endian>::scan_relocatable_relocs(
10111                         Symbol_table* symtab,
10112                         Layout* layout,
10113                         Sized_relobj_file<size, big_endian>* object,
10114                         unsigned int data_shndx,
10115                         unsigned int sh_type,
10116                         const unsigned char* prelocs,
10117                         size_t reloc_count,
10118                         Output_section* output_section,
10119                         bool needs_special_offset_handling,
10120                         size_t local_symbol_count,
10121                         const unsigned char* plocal_symbols,
10122                         Relocatable_relocs* rr)
10123 {
10124   if (sh_type == elfcpp::SHT_REL)
10125     {
10126       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10127           Classify_reloc;
10128       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10129           Scan_relocatable_relocs;
10130
10131       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10132         symtab,
10133         layout,
10134         object,
10135         data_shndx,
10136         prelocs,
10137         reloc_count,
10138         output_section,
10139         needs_special_offset_handling,
10140         local_symbol_count,
10141         plocal_symbols,
10142         rr);
10143     }
10144   else if (sh_type == elfcpp::SHT_RELA)
10145     {
10146       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10147           Classify_reloc;
10148       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10149           Scan_relocatable_relocs;
10150
10151       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10152         symtab,
10153         layout,
10154         object,
10155         data_shndx,
10156         prelocs,
10157         reloc_count,
10158         output_section,
10159         needs_special_offset_handling,
10160         local_symbol_count,
10161         plocal_symbols,
10162         rr);
10163     }
10164   else
10165     gold_unreachable();
10166 }
10167
10168 // Scan the relocs for --emit-relocs.
10169
10170 template<int size, bool big_endian>
10171 void
10172 Target_mips<size, big_endian>::emit_relocs_scan(
10173     Symbol_table* symtab,
10174     Layout* layout,
10175     Sized_relobj_file<size, big_endian>* object,
10176     unsigned int data_shndx,
10177     unsigned int sh_type,
10178     const unsigned char* prelocs,
10179     size_t reloc_count,
10180     Output_section* output_section,
10181     bool needs_special_offset_handling,
10182     size_t local_symbol_count,
10183     const unsigned char* plocal_syms,
10184     Relocatable_relocs* rr)
10185 {
10186   if (sh_type == elfcpp::SHT_REL)
10187     {
10188       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10189           Classify_reloc;
10190       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10191           Emit_relocs_strategy;
10192
10193       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10194         symtab,
10195         layout,
10196         object,
10197         data_shndx,
10198         prelocs,
10199         reloc_count,
10200         output_section,
10201         needs_special_offset_handling,
10202         local_symbol_count,
10203         plocal_syms,
10204         rr);
10205     }
10206   else if (sh_type == elfcpp::SHT_RELA)
10207     {
10208       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10209           Classify_reloc;
10210       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10211           Emit_relocs_strategy;
10212
10213       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10214         symtab,
10215         layout,
10216         object,
10217         data_shndx,
10218         prelocs,
10219         reloc_count,
10220         output_section,
10221         needs_special_offset_handling,
10222         local_symbol_count,
10223         plocal_syms,
10224         rr);
10225     }
10226   else
10227     gold_unreachable();
10228 }
10229
10230 // Emit relocations for a section.
10231
10232 template<int size, bool big_endian>
10233 void
10234 Target_mips<size, big_endian>::relocate_relocs(
10235                         const Relocate_info<size, big_endian>* relinfo,
10236                         unsigned int sh_type,
10237                         const unsigned char* prelocs,
10238                         size_t reloc_count,
10239                         Output_section* output_section,
10240                         typename elfcpp::Elf_types<size>::Elf_Off
10241                           offset_in_output_section,
10242                         unsigned char* view,
10243                         Mips_address view_address,
10244                         section_size_type view_size,
10245                         unsigned char* reloc_view,
10246                         section_size_type reloc_view_size)
10247 {
10248   if (sh_type == elfcpp::SHT_REL)
10249     {
10250       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10251           Classify_reloc;
10252
10253       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10254         relinfo,
10255         prelocs,
10256         reloc_count,
10257         output_section,
10258         offset_in_output_section,
10259         view,
10260         view_address,
10261         view_size,
10262         reloc_view,
10263         reloc_view_size);
10264     }
10265   else if (sh_type == elfcpp::SHT_RELA)
10266     {
10267       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10268           Classify_reloc;
10269
10270       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10271         relinfo,
10272         prelocs,
10273         reloc_count,
10274         output_section,
10275         offset_in_output_section,
10276         view,
10277         view_address,
10278         view_size,
10279         reloc_view,
10280         reloc_view_size);
10281     }
10282   else
10283     gold_unreachable();
10284 }
10285
10286 // Perform target-specific processing in a relocatable link.  This is
10287 // only used if we use the relocation strategy RELOC_SPECIAL.
10288
10289 template<int size, bool big_endian>
10290 void
10291 Target_mips<size, big_endian>::relocate_special_relocatable(
10292     const Relocate_info<size, big_endian>* relinfo,
10293     unsigned int sh_type,
10294     const unsigned char* preloc_in,
10295     size_t relnum,
10296     Output_section* output_section,
10297     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10298     unsigned char* view,
10299     Mips_address view_address,
10300     section_size_type,
10301     unsigned char* preloc_out)
10302 {
10303   // We can only handle REL type relocation sections.
10304   gold_assert(sh_type == elfcpp::SHT_REL);
10305
10306   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10307     Reltype;
10308   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10309     Reltype_write;
10310
10311   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10312
10313   const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10314
10315   Mips_relobj<size, big_endian>* object =
10316     Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10317   const unsigned int local_count = object->local_symbol_count();
10318
10319   Reltype reloc(preloc_in);
10320   Reltype_write reloc_write(preloc_out);
10321
10322   elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10323   const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10324   const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10325
10326   // Get the new symbol index.
10327   // We only use RELOC_SPECIAL strategy in local relocations.
10328   gold_assert(r_sym < local_count);
10329
10330   // We are adjusting a section symbol.  We need to find
10331   // the symbol table index of the section symbol for
10332   // the output section corresponding to input section
10333   // in which this symbol is defined.
10334   bool is_ordinary;
10335   unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10336   gold_assert(is_ordinary);
10337   Output_section* os = object->output_section(shndx);
10338   gold_assert(os != NULL);
10339   gold_assert(os->needs_symtab_index());
10340   unsigned int new_symndx = os->symtab_index();
10341
10342   // Get the new offset--the location in the output section where
10343   // this relocation should be applied.
10344
10345   Mips_address offset = reloc.get_r_offset();
10346   Mips_address new_offset;
10347   if (offset_in_output_section != invalid_address)
10348     new_offset = offset + offset_in_output_section;
10349   else
10350     {
10351       section_offset_type sot_offset =
10352         convert_types<section_offset_type, Mips_address>(offset);
10353       section_offset_type new_sot_offset =
10354         output_section->output_offset(object, relinfo->data_shndx,
10355                                       sot_offset);
10356       gold_assert(new_sot_offset != -1);
10357       new_offset = new_sot_offset;
10358     }
10359
10360   // In an object file, r_offset is an offset within the section.
10361   // In an executable or dynamic object, generated by
10362   // --emit-relocs, r_offset is an absolute address.
10363   if (!parameters->options().relocatable())
10364     {
10365       new_offset += view_address;
10366       if (offset_in_output_section != invalid_address)
10367         new_offset -= offset_in_output_section;
10368     }
10369
10370   reloc_write.put_r_offset(new_offset);
10371   reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10372
10373   // Handle the reloc addend.
10374   // The relocation uses a section symbol in the input file.
10375   // We are adjusting it to use a section symbol in the output
10376   // file.  The input section symbol refers to some address in
10377   // the input section.  We need the relocation in the output
10378   // file to refer to that same address.  This adjustment to
10379   // the addend is the same calculation we use for a simple
10380   // absolute relocation for the input section symbol.
10381   Valtype calculated_value = 0;
10382   const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10383
10384   unsigned char* paddend = view + offset;
10385   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10386   switch (r_type)
10387     {
10388     case elfcpp::R_MIPS_26:
10389       reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10390           offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10391           false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10392           false, &calculated_value);
10393       break;
10394
10395     default:
10396       gold_unreachable();
10397     }
10398
10399   // Report any errors.
10400   switch (reloc_status)
10401     {
10402     case Reloc_funcs::STATUS_OKAY:
10403       break;
10404     case Reloc_funcs::STATUS_OVERFLOW:
10405       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10406                              _("relocation overflow: "
10407                                "%u against local symbol %u in %s"),
10408                              r_type, r_sym, object->name().c_str());
10409       break;
10410     case Reloc_funcs::STATUS_BAD_RELOC:
10411       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10412         _("unexpected opcode while processing relocation"));
10413       break;
10414     default:
10415       gold_unreachable();
10416     }
10417 }
10418
10419 // Optimize the TLS relocation type based on what we know about the
10420 // symbol.  IS_FINAL is true if the final address of this symbol is
10421 // known at link time.
10422
10423 template<int size, bool big_endian>
10424 tls::Tls_optimization
10425 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10426 {
10427   // FIXME: Currently we do not do any TLS optimization.
10428   return tls::TLSOPT_NONE;
10429 }
10430
10431 // Scan a relocation for a local symbol.
10432
10433 template<int size, bool big_endian>
10434 inline void
10435 Target_mips<size, big_endian>::Scan::local(
10436                         Symbol_table* symtab,
10437                         Layout* layout,
10438                         Target_mips<size, big_endian>* target,
10439                         Sized_relobj_file<size, big_endian>* object,
10440                         unsigned int data_shndx,
10441                         Output_section* output_section,
10442                         const Relatype* rela,
10443                         const Reltype* rel,
10444                         unsigned int rel_type,
10445                         unsigned int r_type,
10446                         const elfcpp::Sym<size, big_endian>& lsym,
10447                         bool is_discarded)
10448 {
10449   if (is_discarded)
10450     return;
10451
10452   Mips_address r_offset;
10453   unsigned int r_sym;
10454   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10455
10456   if (rel_type == elfcpp::SHT_RELA)
10457     {
10458       r_offset = rela->get_r_offset();
10459       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10460           get_r_sym(rela);
10461       r_addend = rela->get_r_addend();
10462     }
10463   else
10464     {
10465       r_offset = rel->get_r_offset();
10466       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10467           get_r_sym(rel);
10468       r_addend = 0;
10469     }
10470
10471   Mips_relobj<size, big_endian>* mips_obj =
10472     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10473
10474   if (mips_obj->is_mips16_stub_section(data_shndx))
10475     {
10476       mips_obj->get_mips16_stub_section(data_shndx)
10477               ->new_local_reloc_found(r_type, r_sym);
10478     }
10479
10480   if (r_type == elfcpp::R_MIPS_NONE)
10481     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10482     // mips16 stub.
10483     return;
10484
10485   if (!mips16_call_reloc(r_type)
10486       && !mips_obj->section_allows_mips16_refs(data_shndx))
10487     // This reloc would need to refer to a MIPS16 hard-float stub, if
10488     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10489     // looking for relocs that would need to refer to MIPS16 stubs.
10490     mips_obj->add_local_non_16bit_call(r_sym);
10491
10492   if (r_type == elfcpp::R_MIPS16_26
10493       && !mips_obj->section_allows_mips16_refs(data_shndx))
10494     mips_obj->add_local_16bit_call(r_sym);
10495
10496   switch (r_type)
10497     {
10498     case elfcpp::R_MIPS_GOT16:
10499     case elfcpp::R_MIPS_CALL16:
10500     case elfcpp::R_MIPS_CALL_HI16:
10501     case elfcpp::R_MIPS_CALL_LO16:
10502     case elfcpp::R_MIPS_GOT_HI16:
10503     case elfcpp::R_MIPS_GOT_LO16:
10504     case elfcpp::R_MIPS_GOT_PAGE:
10505     case elfcpp::R_MIPS_GOT_OFST:
10506     case elfcpp::R_MIPS_GOT_DISP:
10507     case elfcpp::R_MIPS_TLS_GOTTPREL:
10508     case elfcpp::R_MIPS_TLS_GD:
10509     case elfcpp::R_MIPS_TLS_LDM:
10510     case elfcpp::R_MIPS16_GOT16:
10511     case elfcpp::R_MIPS16_CALL16:
10512     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10513     case elfcpp::R_MIPS16_TLS_GD:
10514     case elfcpp::R_MIPS16_TLS_LDM:
10515     case elfcpp::R_MICROMIPS_GOT16:
10516     case elfcpp::R_MICROMIPS_CALL16:
10517     case elfcpp::R_MICROMIPS_CALL_HI16:
10518     case elfcpp::R_MICROMIPS_CALL_LO16:
10519     case elfcpp::R_MICROMIPS_GOT_HI16:
10520     case elfcpp::R_MICROMIPS_GOT_LO16:
10521     case elfcpp::R_MICROMIPS_GOT_PAGE:
10522     case elfcpp::R_MICROMIPS_GOT_OFST:
10523     case elfcpp::R_MICROMIPS_GOT_DISP:
10524     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10525     case elfcpp::R_MICROMIPS_TLS_GD:
10526     case elfcpp::R_MICROMIPS_TLS_LDM:
10527     case elfcpp::R_MIPS_EH:
10528       // We need a GOT section.
10529       target->got_section(symtab, layout);
10530       break;
10531
10532     default:
10533       break;
10534     }
10535
10536   if (call_lo16_reloc(r_type)
10537       || got_lo16_reloc(r_type)
10538       || got_disp_reloc(r_type)
10539       || eh_reloc(r_type))
10540     {
10541       // We may need a local GOT entry for this relocation.  We
10542       // don't count R_MIPS_GOT_PAGE because we can estimate the
10543       // maximum number of pages needed by looking at the size of
10544       // the segment.  Similar comments apply to R_MIPS*_GOT16 and
10545       // R_MIPS*_CALL16.  We don't count R_MIPS_GOT_HI16, or
10546       // R_MIPS_CALL_HI16 because these are always followed by an
10547       // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10548       Mips_output_data_got<size, big_endian>* got =
10549         target->got_section(symtab, layout);
10550       bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10551       got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10552                                    is_section_symbol);
10553     }
10554
10555   switch (r_type)
10556     {
10557     case elfcpp::R_MIPS_CALL16:
10558     case elfcpp::R_MIPS16_CALL16:
10559     case elfcpp::R_MICROMIPS_CALL16:
10560       gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10561                  (unsigned long)r_offset);
10562       return;
10563
10564     case elfcpp::R_MIPS_GOT_PAGE:
10565     case elfcpp::R_MICROMIPS_GOT_PAGE:
10566     case elfcpp::R_MIPS16_GOT16:
10567     case elfcpp::R_MIPS_GOT16:
10568     case elfcpp::R_MIPS_GOT_HI16:
10569     case elfcpp::R_MIPS_GOT_LO16:
10570     case elfcpp::R_MICROMIPS_GOT16:
10571     case elfcpp::R_MICROMIPS_GOT_HI16:
10572     case elfcpp::R_MICROMIPS_GOT_LO16:
10573       {
10574         // This relocation needs a page entry in the GOT.
10575         // Get the section contents.
10576         section_size_type view_size = 0;
10577         const unsigned char* view = object->section_contents(data_shndx,
10578                                                              &view_size, false);
10579         view += r_offset;
10580
10581         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10582         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10583                                                         : r_addend);
10584
10585         if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10586           target->got16_addends_.push_back(got16_addend<size, big_endian>(
10587               object, data_shndx, r_type, r_sym, addend));
10588         else
10589           target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10590         break;
10591       }
10592
10593     case elfcpp::R_MIPS_HI16:
10594     case elfcpp::R_MIPS_PCHI16:
10595     case elfcpp::R_MIPS16_HI16:
10596     case elfcpp::R_MICROMIPS_HI16:
10597       // Record the reloc so that we can check whether the corresponding LO16
10598       // part exists.
10599       if (rel_type == elfcpp::SHT_REL)
10600         target->got16_addends_.push_back(got16_addend<size, big_endian>(
10601             object, data_shndx, r_type, r_sym, 0));
10602       break;
10603
10604     case elfcpp::R_MIPS_LO16:
10605     case elfcpp::R_MIPS_PCLO16:
10606     case elfcpp::R_MIPS16_LO16:
10607     case elfcpp::R_MICROMIPS_LO16:
10608       {
10609         if (rel_type != elfcpp::SHT_REL)
10610           break;
10611
10612         // Find corresponding GOT16/HI16 relocation.
10613
10614         // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10615         // be immediately following.  However, for the IRIX6 ABI, the next
10616         // relocation may be a composed relocation consisting of several
10617         // relocations for the same address.  In that case, the R_MIPS_LO16
10618         // relocation may occur as one of these.  We permit a similar
10619         // extension in general, as that is useful for GCC.
10620
10621         // In some cases GCC dead code elimination removes the LO16 but
10622         // keeps the corresponding HI16.  This is strictly speaking a
10623         // violation of the ABI but not immediately harmful.
10624
10625         typename std::list<got16_addend<size, big_endian> >::iterator it =
10626           target->got16_addends_.begin();
10627         while (it != target->got16_addends_.end())
10628           {
10629             got16_addend<size, big_endian> _got16_addend = *it;
10630
10631             // TODO(sasa): Split got16_addends_ list into two lists - one for
10632             // GOT16 relocs and the other for HI16 relocs.
10633
10634             // Report an error if we find HI16 or GOT16 reloc from the
10635             // previous section without the matching LO16 part.
10636             if (_got16_addend.object != object
10637                 || _got16_addend.shndx != data_shndx)
10638               {
10639                 gold_error("Can't find matching LO16 reloc");
10640                 break;
10641               }
10642
10643             if (_got16_addend.r_sym != r_sym
10644                 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10645               {
10646                 ++it;
10647                 continue;
10648               }
10649
10650             // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10651             // For GOT16, we need to calculate combined addend and record GOT page
10652             // entry.
10653             if (got16_reloc(_got16_addend.r_type))
10654               {
10655
10656                 section_size_type view_size = 0;
10657                 const unsigned char* view = object->section_contents(data_shndx,
10658                                                                      &view_size,
10659                                                                      false);
10660                 view += r_offset;
10661
10662                 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10663                 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10664
10665                 addend = (_got16_addend.addend << 16) + addend;
10666                 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10667                                                              addend);
10668               }
10669
10670             it = target->got16_addends_.erase(it);
10671           }
10672         break;
10673       }
10674     }
10675
10676   switch (r_type)
10677     {
10678     case elfcpp::R_MIPS_32:
10679     case elfcpp::R_MIPS_REL32:
10680     case elfcpp::R_MIPS_64:
10681       {
10682         if (parameters->options().output_is_position_independent())
10683           {
10684             // If building a shared library (or a position-independent
10685             // executable), we need to create a dynamic relocation for
10686             // this location.
10687             if (is_readonly_section(output_section))
10688               break;
10689             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10690             rel_dyn->add_symbolless_local_addend(object, r_sym,
10691                                                  elfcpp::R_MIPS_REL32,
10692                                                  output_section, data_shndx,
10693                                                  r_offset);
10694           }
10695         break;
10696       }
10697
10698     case elfcpp::R_MIPS_TLS_GOTTPREL:
10699     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10700     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10701     case elfcpp::R_MIPS_TLS_LDM:
10702     case elfcpp::R_MIPS16_TLS_LDM:
10703     case elfcpp::R_MICROMIPS_TLS_LDM:
10704     case elfcpp::R_MIPS_TLS_GD:
10705     case elfcpp::R_MIPS16_TLS_GD:
10706     case elfcpp::R_MICROMIPS_TLS_GD:
10707       {
10708         bool output_is_shared = parameters->options().shared();
10709         const tls::Tls_optimization optimized_type
10710             = Target_mips<size, big_endian>::optimize_tls_reloc(
10711                                              !output_is_shared, r_type);
10712         switch (r_type)
10713           {
10714           case elfcpp::R_MIPS_TLS_GD:
10715           case elfcpp::R_MIPS16_TLS_GD:
10716           case elfcpp::R_MICROMIPS_TLS_GD:
10717             if (optimized_type == tls::TLSOPT_NONE)
10718               {
10719                 // Create a pair of GOT entries for the module index and
10720                 // dtv-relative offset.
10721                 Mips_output_data_got<size, big_endian>* got =
10722                   target->got_section(symtab, layout);
10723                 unsigned int shndx = lsym.get_st_shndx();
10724                 bool is_ordinary;
10725                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10726                 if (!is_ordinary)
10727                   {
10728                     object->error(_("local symbol %u has bad shndx %u"),
10729                                   r_sym, shndx);
10730                     break;
10731                   }
10732                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10733                                              shndx, false);
10734               }
10735             else
10736               {
10737                 // FIXME: TLS optimization not supported yet.
10738                 gold_unreachable();
10739               }
10740             break;
10741
10742           case elfcpp::R_MIPS_TLS_LDM:
10743           case elfcpp::R_MIPS16_TLS_LDM:
10744           case elfcpp::R_MICROMIPS_TLS_LDM:
10745             if (optimized_type == tls::TLSOPT_NONE)
10746               {
10747                 // We always record LDM symbols as local with index 0.
10748                 target->got_section()->record_local_got_symbol(mips_obj, 0,
10749                                                                r_addend, r_type,
10750                                                                -1U, false);
10751               }
10752             else
10753               {
10754                 // FIXME: TLS optimization not supported yet.
10755                 gold_unreachable();
10756               }
10757             break;
10758           case elfcpp::R_MIPS_TLS_GOTTPREL:
10759           case elfcpp::R_MIPS16_TLS_GOTTPREL:
10760           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10761             layout->set_has_static_tls();
10762             if (optimized_type == tls::TLSOPT_NONE)
10763               {
10764                 // Create a GOT entry for the tp-relative offset.
10765                 Mips_output_data_got<size, big_endian>* got =
10766                   target->got_section(symtab, layout);
10767                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10768                                              -1U, false);
10769               }
10770             else
10771               {
10772                 // FIXME: TLS optimization not supported yet.
10773                 gold_unreachable();
10774               }
10775             break;
10776
10777           default:
10778             gold_unreachable();
10779         }
10780       }
10781       break;
10782
10783     default:
10784       break;
10785     }
10786
10787   // Refuse some position-dependent relocations when creating a
10788   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
10789   // not PIC, but we can create dynamic relocations and the result
10790   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
10791   // combined with R_MIPS_GOT16.
10792   if (parameters->options().shared())
10793     {
10794       switch (r_type)
10795         {
10796         case elfcpp::R_MIPS16_HI16:
10797         case elfcpp::R_MIPS_HI16:
10798         case elfcpp::R_MIPS_HIGHER:
10799         case elfcpp::R_MIPS_HIGHEST:
10800         case elfcpp::R_MICROMIPS_HI16:
10801         case elfcpp::R_MICROMIPS_HIGHER:
10802         case elfcpp::R_MICROMIPS_HIGHEST:
10803           // Don't refuse a high part relocation if it's against
10804           // no symbol (e.g. part of a compound relocation).
10805           if (r_sym == 0)
10806             break;
10807           // Fall through.
10808
10809         case elfcpp::R_MIPS16_26:
10810         case elfcpp::R_MIPS_26:
10811         case elfcpp::R_MICROMIPS_26_S1:
10812           gold_error(_("%s: relocation %u against `%s' can not be used when "
10813                        "making a shared object; recompile with -fPIC"),
10814                      object->name().c_str(), r_type, "a local symbol");
10815         default:
10816           break;
10817         }
10818     }
10819 }
10820
10821 template<int size, bool big_endian>
10822 inline void
10823 Target_mips<size, big_endian>::Scan::local(
10824                         Symbol_table* symtab,
10825                         Layout* layout,
10826                         Target_mips<size, big_endian>* target,
10827                         Sized_relobj_file<size, big_endian>* object,
10828                         unsigned int data_shndx,
10829                         Output_section* output_section,
10830                         const Reltype& reloc,
10831                         unsigned int r_type,
10832                         const elfcpp::Sym<size, big_endian>& lsym,
10833                         bool is_discarded)
10834 {
10835   if (is_discarded)
10836     return;
10837
10838   local(
10839     symtab,
10840     layout,
10841     target,
10842     object,
10843     data_shndx,
10844     output_section,
10845     (const Relatype*) NULL,
10846     &reloc,
10847     elfcpp::SHT_REL,
10848     r_type,
10849     lsym, is_discarded);
10850 }
10851
10852
10853 template<int size, bool big_endian>
10854 inline void
10855 Target_mips<size, big_endian>::Scan::local(
10856                         Symbol_table* symtab,
10857                         Layout* layout,
10858                         Target_mips<size, big_endian>* target,
10859                         Sized_relobj_file<size, big_endian>* object,
10860                         unsigned int data_shndx,
10861                         Output_section* output_section,
10862                         const Relatype& reloc,
10863                         unsigned int r_type,
10864                         const elfcpp::Sym<size, big_endian>& lsym,
10865                         bool is_discarded)
10866 {
10867   if (is_discarded)
10868     return;
10869
10870   local(
10871     symtab,
10872     layout,
10873     target,
10874     object,
10875     data_shndx,
10876     output_section,
10877     &reloc,
10878     (const Reltype*) NULL,
10879     elfcpp::SHT_RELA,
10880     r_type,
10881     lsym, is_discarded);
10882 }
10883
10884 // Scan a relocation for a global symbol.
10885
10886 template<int size, bool big_endian>
10887 inline void
10888 Target_mips<size, big_endian>::Scan::global(
10889                                 Symbol_table* symtab,
10890                                 Layout* layout,
10891                                 Target_mips<size, big_endian>* target,
10892                                 Sized_relobj_file<size, big_endian>* object,
10893                                 unsigned int data_shndx,
10894                                 Output_section* output_section,
10895                                 const Relatype* rela,
10896                                 const Reltype* rel,
10897                                 unsigned int rel_type,
10898                                 unsigned int r_type,
10899                                 Symbol* gsym)
10900 {
10901   Mips_address r_offset;
10902   unsigned int r_sym;
10903   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10904
10905   if (rel_type == elfcpp::SHT_RELA)
10906     {
10907       r_offset = rela->get_r_offset();
10908       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10909           get_r_sym(rela);
10910       r_addend = rela->get_r_addend();
10911     }
10912   else
10913     {
10914       r_offset = rel->get_r_offset();
10915       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10916           get_r_sym(rel);
10917       r_addend = 0;
10918     }
10919
10920   Mips_relobj<size, big_endian>* mips_obj =
10921     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10922   Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10923
10924   if (mips_obj->is_mips16_stub_section(data_shndx))
10925     {
10926       mips_obj->get_mips16_stub_section(data_shndx)
10927               ->new_global_reloc_found(r_type, mips_sym);
10928     }
10929
10930   if (r_type == elfcpp::R_MIPS_NONE)
10931     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10932     // mips16 stub.
10933     return;
10934
10935   if (!mips16_call_reloc(r_type)
10936       && !mips_obj->section_allows_mips16_refs(data_shndx))
10937     // This reloc would need to refer to a MIPS16 hard-float stub, if
10938     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10939     // looking for relocs that would need to refer to MIPS16 stubs.
10940     mips_sym->set_need_fn_stub();
10941
10942   // We need PLT entries if there are static-only relocations against
10943   // an externally-defined function.  This can technically occur for
10944   // shared libraries if there are branches to the symbol, although it
10945   // is unlikely that this will be used in practice due to the short
10946   // ranges involved.  It can occur for any relative or absolute relocation
10947   // in executables; in that case, the PLT entry becomes the function's
10948   // canonical address.
10949   bool static_reloc = false;
10950
10951   // Set CAN_MAKE_DYNAMIC to true if we can convert this
10952   // relocation into a dynamic one.
10953   bool can_make_dynamic = false;
10954   switch (r_type)
10955     {
10956     case elfcpp::R_MIPS_GOT16:
10957     case elfcpp::R_MIPS_CALL16:
10958     case elfcpp::R_MIPS_CALL_HI16:
10959     case elfcpp::R_MIPS_CALL_LO16:
10960     case elfcpp::R_MIPS_GOT_HI16:
10961     case elfcpp::R_MIPS_GOT_LO16:
10962     case elfcpp::R_MIPS_GOT_PAGE:
10963     case elfcpp::R_MIPS_GOT_OFST:
10964     case elfcpp::R_MIPS_GOT_DISP:
10965     case elfcpp::R_MIPS_TLS_GOTTPREL:
10966     case elfcpp::R_MIPS_TLS_GD:
10967     case elfcpp::R_MIPS_TLS_LDM:
10968     case elfcpp::R_MIPS16_GOT16:
10969     case elfcpp::R_MIPS16_CALL16:
10970     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10971     case elfcpp::R_MIPS16_TLS_GD:
10972     case elfcpp::R_MIPS16_TLS_LDM:
10973     case elfcpp::R_MICROMIPS_GOT16:
10974     case elfcpp::R_MICROMIPS_CALL16:
10975     case elfcpp::R_MICROMIPS_CALL_HI16:
10976     case elfcpp::R_MICROMIPS_CALL_LO16:
10977     case elfcpp::R_MICROMIPS_GOT_HI16:
10978     case elfcpp::R_MICROMIPS_GOT_LO16:
10979     case elfcpp::R_MICROMIPS_GOT_PAGE:
10980     case elfcpp::R_MICROMIPS_GOT_OFST:
10981     case elfcpp::R_MICROMIPS_GOT_DISP:
10982     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10983     case elfcpp::R_MICROMIPS_TLS_GD:
10984     case elfcpp::R_MICROMIPS_TLS_LDM:
10985     case elfcpp::R_MIPS_EH:
10986       // We need a GOT section.
10987       target->got_section(symtab, layout);
10988       break;
10989
10990     // This is just a hint; it can safely be ignored.  Don't set
10991     // has_static_relocs for the corresponding symbol.
10992     case elfcpp::R_MIPS_JALR:
10993     case elfcpp::R_MICROMIPS_JALR:
10994       break;
10995
10996     case elfcpp::R_MIPS_GPREL16:
10997     case elfcpp::R_MIPS_GPREL32:
10998     case elfcpp::R_MIPS16_GPREL:
10999     case elfcpp::R_MICROMIPS_GPREL16:
11000       // TODO(sasa)
11001       // GP-relative relocations always resolve to a definition in a
11002       // regular input file, ignoring the one-definition rule.  This is
11003       // important for the GP setup sequence in NewABI code, which
11004       // always resolves to a local function even if other relocations
11005       // against the symbol wouldn't.
11006       //constrain_symbol_p = FALSE;
11007       break;
11008
11009     case elfcpp::R_MIPS_32:
11010     case elfcpp::R_MIPS_REL32:
11011     case elfcpp::R_MIPS_64:
11012       if ((parameters->options().shared()
11013           || (strcmp(gsym->name(), "__gnu_local_gp") != 0
11014           && (!is_readonly_section(output_section)
11015           || mips_obj->is_pic())))
11016           && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
11017         {
11018           if (r_type != elfcpp::R_MIPS_REL32)
11019             mips_sym->set_pointer_equality_needed();
11020           can_make_dynamic = true;
11021           break;
11022         }
11023       // Fall through.
11024
11025     default:
11026       // Most static relocations require pointer equality, except
11027       // for branches.
11028       mips_sym->set_pointer_equality_needed();
11029       // Fall through.
11030
11031     case elfcpp::R_MIPS_26:
11032     case elfcpp::R_MIPS_PC16:
11033     case elfcpp::R_MIPS_PC21_S2:
11034     case elfcpp::R_MIPS_PC26_S2:
11035     case elfcpp::R_MIPS16_26:
11036     case elfcpp::R_MICROMIPS_26_S1:
11037     case elfcpp::R_MICROMIPS_PC7_S1:
11038     case elfcpp::R_MICROMIPS_PC10_S1:
11039     case elfcpp::R_MICROMIPS_PC16_S1:
11040     case elfcpp::R_MICROMIPS_PC23_S2:
11041       static_reloc = true;
11042       mips_sym->set_has_static_relocs();
11043       break;
11044     }
11045
11046   // If there are call relocations against an externally-defined symbol,
11047   // see whether we can create a MIPS lazy-binding stub for it.  We can
11048   // only do this if all references to the function are through call
11049   // relocations, and in that case, the traditional lazy-binding stubs
11050   // are much more efficient than PLT entries.
11051   switch (r_type)
11052     {
11053     case elfcpp::R_MIPS16_CALL16:
11054     case elfcpp::R_MIPS_CALL16:
11055     case elfcpp::R_MIPS_CALL_HI16:
11056     case elfcpp::R_MIPS_CALL_LO16:
11057     case elfcpp::R_MIPS_JALR:
11058     case elfcpp::R_MICROMIPS_CALL16:
11059     case elfcpp::R_MICROMIPS_CALL_HI16:
11060     case elfcpp::R_MICROMIPS_CALL_LO16:
11061     case elfcpp::R_MICROMIPS_JALR:
11062       if (!mips_sym->no_lazy_stub())
11063         {
11064           if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11065               // Calls from shared objects to undefined symbols of type
11066               // STT_NOTYPE need lazy-binding stub.
11067               || (mips_sym->is_undefined() && parameters->options().shared()))
11068             target->mips_stubs_section(layout)->make_entry(mips_sym);
11069         }
11070       break;
11071     default:
11072       {
11073         // We must not create a stub for a symbol that has relocations
11074         // related to taking the function's address.
11075         mips_sym->set_no_lazy_stub();
11076         target->remove_lazy_stub_entry(mips_sym);
11077         break;
11078       }
11079   }
11080
11081   if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11082                                                    mips_sym->is_mips16()))
11083     mips_sym->set_has_nonpic_branches();
11084
11085   // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11086   // and has a special meaning.
11087   bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11088                                && strcmp(gsym->name(), "_gp_disp") == 0
11089                                && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11090   if (static_reloc && gsym->needs_plt_entry())
11091     {
11092       target->make_plt_entry(symtab, layout, mips_sym, r_type);
11093
11094       // Since this is not a PC-relative relocation, we may be
11095       // taking the address of a function.  In that case we need to
11096       // set the entry in the dynamic symbol table to the address of
11097       // the PLT entry.
11098       if (gsym->is_from_dynobj() && !parameters->options().shared())
11099         {
11100           gsym->set_needs_dynsym_value();
11101           // We distinguish between PLT entries and lazy-binding stubs by
11102           // giving the former an st_other value of STO_MIPS_PLT.  Set the
11103           // flag if there are any relocations in the binary where pointer
11104           // equality matters.
11105           if (mips_sym->pointer_equality_needed())
11106             mips_sym->set_mips_plt();
11107         }
11108     }
11109   if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11110     {
11111       // Absolute addressing relocations.
11112       // Make a dynamic relocation if necessary.
11113       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11114         {
11115           if (gsym->may_need_copy_reloc())
11116             {
11117               target->copy_reloc(symtab, layout, object, data_shndx,
11118                                  output_section, gsym, r_type, r_offset);
11119             }
11120           else if (can_make_dynamic)
11121             {
11122               // Create .rel.dyn section.
11123               target->rel_dyn_section(layout);
11124               target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11125                                     data_shndx, output_section, r_offset);
11126             }
11127           else
11128             gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11129                        gsym->name());
11130         }
11131     }
11132
11133   bool for_call = false;
11134   switch (r_type)
11135     {
11136     case elfcpp::R_MIPS_CALL16:
11137     case elfcpp::R_MIPS16_CALL16:
11138     case elfcpp::R_MICROMIPS_CALL16:
11139     case elfcpp::R_MIPS_CALL_HI16:
11140     case elfcpp::R_MIPS_CALL_LO16:
11141     case elfcpp::R_MICROMIPS_CALL_HI16:
11142     case elfcpp::R_MICROMIPS_CALL_LO16:
11143       for_call = true;
11144       // Fall through.
11145
11146     case elfcpp::R_MIPS16_GOT16:
11147     case elfcpp::R_MIPS_GOT16:
11148     case elfcpp::R_MIPS_GOT_HI16:
11149     case elfcpp::R_MIPS_GOT_LO16:
11150     case elfcpp::R_MICROMIPS_GOT16:
11151     case elfcpp::R_MICROMIPS_GOT_HI16:
11152     case elfcpp::R_MICROMIPS_GOT_LO16:
11153     case elfcpp::R_MIPS_GOT_DISP:
11154     case elfcpp::R_MICROMIPS_GOT_DISP:
11155     case elfcpp::R_MIPS_EH:
11156       {
11157         // The symbol requires a GOT entry.
11158         Mips_output_data_got<size, big_endian>* got =
11159           target->got_section(symtab, layout);
11160         got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11161                                       for_call);
11162         mips_sym->set_global_got_area(GGA_NORMAL);
11163       }
11164       break;
11165
11166     case elfcpp::R_MIPS_GOT_PAGE:
11167     case elfcpp::R_MICROMIPS_GOT_PAGE:
11168       {
11169         // This relocation needs a page entry in the GOT.
11170         // Get the section contents.
11171         section_size_type view_size = 0;
11172         const unsigned char* view =
11173           object->section_contents(data_shndx, &view_size, false);
11174         view += r_offset;
11175
11176         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11177         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11178                                                         : r_addend);
11179         Mips_output_data_got<size, big_endian>* got =
11180           target->got_section(symtab, layout);
11181         got->record_got_page_entry(mips_obj, r_sym, addend);
11182
11183         // If this is a global, overridable symbol, GOT_PAGE will
11184         // decay to GOT_DISP, so we'll need a GOT entry for it.
11185         bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11186                             && !mips_sym->object()->is_dynamic()
11187                             && !mips_sym->is_undefined());
11188         if (!def_regular
11189             || (parameters->options().output_is_position_independent()
11190                 && !parameters->options().Bsymbolic()
11191                 && !mips_sym->is_forced_local()))
11192           {
11193             got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11194                                           for_call);
11195             mips_sym->set_global_got_area(GGA_NORMAL);
11196           }
11197       }
11198       break;
11199
11200     case elfcpp::R_MIPS_TLS_GOTTPREL:
11201     case elfcpp::R_MIPS16_TLS_GOTTPREL:
11202     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11203     case elfcpp::R_MIPS_TLS_LDM:
11204     case elfcpp::R_MIPS16_TLS_LDM:
11205     case elfcpp::R_MICROMIPS_TLS_LDM:
11206     case elfcpp::R_MIPS_TLS_GD:
11207     case elfcpp::R_MIPS16_TLS_GD:
11208     case elfcpp::R_MICROMIPS_TLS_GD:
11209       {
11210         const bool is_final = gsym->final_value_is_known();
11211         const tls::Tls_optimization optimized_type =
11212           Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11213
11214         switch (r_type)
11215           {
11216           case elfcpp::R_MIPS_TLS_GD:
11217           case elfcpp::R_MIPS16_TLS_GD:
11218           case elfcpp::R_MICROMIPS_TLS_GD:
11219             if (optimized_type == tls::TLSOPT_NONE)
11220               {
11221                 // Create a pair of GOT entries for the module index and
11222                 // dtv-relative offset.
11223                 Mips_output_data_got<size, big_endian>* got =
11224                   target->got_section(symtab, layout);
11225                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11226                                               false);
11227               }
11228             else
11229               {
11230                 // FIXME: TLS optimization not supported yet.
11231                 gold_unreachable();
11232               }
11233             break;
11234
11235           case elfcpp::R_MIPS_TLS_LDM:
11236           case elfcpp::R_MIPS16_TLS_LDM:
11237           case elfcpp::R_MICROMIPS_TLS_LDM:
11238             if (optimized_type == tls::TLSOPT_NONE)
11239               {
11240                 // We always record LDM symbols as local with index 0.
11241                 target->got_section()->record_local_got_symbol(mips_obj, 0,
11242                                                                r_addend, r_type,
11243                                                                -1U, false);
11244               }
11245             else
11246               {
11247                 // FIXME: TLS optimization not supported yet.
11248                 gold_unreachable();
11249               }
11250             break;
11251           case elfcpp::R_MIPS_TLS_GOTTPREL:
11252           case elfcpp::R_MIPS16_TLS_GOTTPREL:
11253           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11254             layout->set_has_static_tls();
11255             if (optimized_type == tls::TLSOPT_NONE)
11256               {
11257                 // Create a GOT entry for the tp-relative offset.
11258                 Mips_output_data_got<size, big_endian>* got =
11259                   target->got_section(symtab, layout);
11260                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11261                                               false);
11262               }
11263             else
11264               {
11265                 // FIXME: TLS optimization not supported yet.
11266                 gold_unreachable();
11267               }
11268             break;
11269
11270           default:
11271             gold_unreachable();
11272         }
11273       }
11274       break;
11275     case elfcpp::R_MIPS_COPY:
11276     case elfcpp::R_MIPS_JUMP_SLOT:
11277       // These are relocations which should only be seen by the
11278       // dynamic linker, and should never be seen here.
11279       gold_error(_("%s: unexpected reloc %u in object file"),
11280                  object->name().c_str(), r_type);
11281       break;
11282
11283     default:
11284       break;
11285     }
11286
11287   // Refuse some position-dependent relocations when creating a
11288   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
11289   // not PIC, but we can create dynamic relocations and the result
11290   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
11291   // combined with R_MIPS_GOT16.
11292   if (parameters->options().shared())
11293     {
11294       switch (r_type)
11295         {
11296         case elfcpp::R_MIPS16_HI16:
11297         case elfcpp::R_MIPS_HI16:
11298         case elfcpp::R_MIPS_HIGHER:
11299         case elfcpp::R_MIPS_HIGHEST:
11300         case elfcpp::R_MICROMIPS_HI16:
11301         case elfcpp::R_MICROMIPS_HIGHER:
11302         case elfcpp::R_MICROMIPS_HIGHEST:
11303           // Don't refuse a high part relocation if it's against
11304           // no symbol (e.g. part of a compound relocation).
11305           if (r_sym == 0)
11306             break;
11307
11308           // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11309           // and has a special meaning.
11310           if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11311             break;
11312           // Fall through.
11313
11314         case elfcpp::R_MIPS16_26:
11315         case elfcpp::R_MIPS_26:
11316         case elfcpp::R_MICROMIPS_26_S1:
11317           gold_error(_("%s: relocation %u against `%s' can not be used when "
11318                        "making a shared object; recompile with -fPIC"),
11319                      object->name().c_str(), r_type, gsym->name());
11320         default:
11321           break;
11322         }
11323     }
11324 }
11325
11326 template<int size, bool big_endian>
11327 inline void
11328 Target_mips<size, big_endian>::Scan::global(
11329                                 Symbol_table* symtab,
11330                                 Layout* layout,
11331                                 Target_mips<size, big_endian>* target,
11332                                 Sized_relobj_file<size, big_endian>* object,
11333                                 unsigned int data_shndx,
11334                                 Output_section* output_section,
11335                                 const Relatype& reloc,
11336                                 unsigned int r_type,
11337                                 Symbol* gsym)
11338 {
11339   global(
11340     symtab,
11341     layout,
11342     target,
11343     object,
11344     data_shndx,
11345     output_section,
11346     &reloc,
11347     (const Reltype*) NULL,
11348     elfcpp::SHT_RELA,
11349     r_type,
11350     gsym);
11351 }
11352
11353 template<int size, bool big_endian>
11354 inline void
11355 Target_mips<size, big_endian>::Scan::global(
11356                                 Symbol_table* symtab,
11357                                 Layout* layout,
11358                                 Target_mips<size, big_endian>* target,
11359                                 Sized_relobj_file<size, big_endian>* object,
11360                                 unsigned int data_shndx,
11361                                 Output_section* output_section,
11362                                 const Reltype& reloc,
11363                                 unsigned int r_type,
11364                                 Symbol* gsym)
11365 {
11366   global(
11367     symtab,
11368     layout,
11369     target,
11370     object,
11371     data_shndx,
11372     output_section,
11373     (const Relatype*) NULL,
11374     &reloc,
11375     elfcpp::SHT_REL,
11376     r_type,
11377     gsym);
11378 }
11379
11380 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11381 // In cases where Scan::local() or Scan::global() has created
11382 // a dynamic relocation, the addend of the relocation is carried
11383 // in the data, and we must not apply the static relocation.
11384
11385 template<int size, bool big_endian>
11386 inline bool
11387 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11388     const Mips_symbol<size>* gsym,
11389     unsigned int r_type,
11390     Output_section* output_section,
11391     Target_mips* target)
11392 {
11393   // If the output section is not allocated, then we didn't call
11394   // scan_relocs, we didn't create a dynamic reloc, and we must apply
11395   // the reloc here.
11396   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11397       return true;
11398
11399   if (gsym == NULL)
11400     return true;
11401   else
11402     {
11403       // For global symbols, we use the same helper routines used in the
11404       // scan pass.
11405       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11406           && !gsym->may_need_copy_reloc())
11407         {
11408           // We have generated dynamic reloc (R_MIPS_REL32).
11409
11410           bool multi_got = false;
11411           if (target->has_got_section())
11412             multi_got = target->got_section()->multi_got();
11413           bool has_got_offset;
11414           if (!multi_got)
11415             has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11416           else
11417             has_got_offset = gsym->global_gotoffset() != -1U;
11418           if (!has_got_offset)
11419             return true;
11420           else
11421             // Apply the relocation only if the symbol is in the local got.
11422             // Do not apply the relocation if the symbol is in the global
11423             // got.
11424             return symbol_references_local(gsym, gsym->has_dynsym_index());
11425         }
11426       else
11427         // We have not generated dynamic reloc.
11428         return true;
11429     }
11430 }
11431
11432 // Perform a relocation.
11433
11434 template<int size, bool big_endian>
11435 inline bool
11436 Target_mips<size, big_endian>::Relocate::relocate(
11437                         const Relocate_info<size, big_endian>* relinfo,
11438                         unsigned int rel_type,
11439                         Target_mips* target,
11440                         Output_section* output_section,
11441                         size_t relnum,
11442                         const unsigned char* preloc,
11443                         const Sized_symbol<size>* gsym,
11444                         const Symbol_value<size>* psymval,
11445                         unsigned char* view,
11446                         Mips_address address,
11447                         section_size_type)
11448 {
11449   Mips_address r_offset;
11450   unsigned int r_sym;
11451   unsigned int r_type;
11452   unsigned int r_type2;
11453   unsigned int r_type3;
11454   unsigned char r_ssym;
11455   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11456   // r_offset and r_type of the next relocation is needed for resolving multiple
11457   // consecutive relocations with the same offset.
11458   Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11459   unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11460
11461   elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11462   size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
11463
11464   if (rel_type == elfcpp::SHT_RELA)
11465     {
11466       const Relatype rela(preloc);
11467       r_offset = rela.get_r_offset();
11468       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11469           get_r_sym(&rela);
11470       r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11471           get_r_type(&rela);
11472       r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11473           get_r_type2(&rela);
11474       r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11475           get_r_type3(&rela);
11476       r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11477           get_r_ssym(&rela);
11478       r_addend = rela.get_r_addend();
11479       // If this is not last relocation, get r_offset and r_type of the next
11480       // relocation.
11481       if (relnum + 1 < reloc_count)
11482         {
11483           const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11484           const Relatype next_rela(preloc + reloc_size);
11485           next_r_offset = next_rela.get_r_offset();
11486           next_r_type =
11487             Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11488               get_r_type(&next_rela);
11489         }
11490     }
11491   else
11492     {
11493       const Reltype rel(preloc);
11494       r_offset = rel.get_r_offset();
11495       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11496           get_r_sym(&rel);
11497       r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11498           get_r_type(&rel);
11499       r_ssym = 0;
11500       r_type2 = elfcpp::R_MIPS_NONE;
11501       r_type3 = elfcpp::R_MIPS_NONE;
11502       r_addend = 0;
11503       // If this is not last relocation, get r_offset and r_type of the next
11504       // relocation.
11505       if (relnum + 1 < reloc_count)
11506         {
11507           const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11508           const Reltype next_rel(preloc + reloc_size);
11509           next_r_offset = next_rel.get_r_offset();
11510           next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11511             get_r_type(&next_rel);
11512         }
11513     }
11514
11515   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11516   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11517
11518   Mips_relobj<size, big_endian>* object =
11519       Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11520
11521   bool target_is_16_bit_code = false;
11522   bool target_is_micromips_code = false;
11523   bool cross_mode_jump;
11524
11525   Symbol_value<size> symval;
11526
11527   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11528
11529   bool changed_symbol_value = false;
11530   if (gsym == NULL)
11531     {
11532       target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11533       target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11534       if (target_is_16_bit_code || target_is_micromips_code)
11535         {
11536           // MIPS16/microMIPS text labels should be treated as odd.
11537           symval.set_output_value(psymval->value(object, 1));
11538           psymval = &symval;
11539           changed_symbol_value = true;
11540         }
11541     }
11542   else
11543     {
11544       target_is_16_bit_code = mips_sym->is_mips16();
11545       target_is_micromips_code = mips_sym->is_micromips();
11546
11547       // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11548       // it odd.  This will cause something like .word SYM to come up with
11549       // the right value when it is loaded into the PC.
11550
11551       if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11552           && psymval->value(object, 0) != 0)
11553         {
11554           symval.set_output_value(psymval->value(object, 0) | 1);
11555           psymval = &symval;
11556           changed_symbol_value = true;
11557         }
11558
11559       // Pick the value to use for symbols defined in shared objects.
11560       if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11561           || mips_sym->has_lazy_stub())
11562         {
11563           Mips_address value;
11564           if (!mips_sym->has_lazy_stub())
11565             {
11566               // Prefer a standard MIPS PLT entry.
11567               if (mips_sym->has_mips_plt_offset())
11568                 {
11569                   value = target->plt_section()->mips_entry_address(mips_sym);
11570                   target_is_micromips_code = false;
11571                   target_is_16_bit_code = false;
11572                 }
11573               else
11574                 {
11575                   value = (target->plt_section()->comp_entry_address(mips_sym)
11576                            + 1);
11577                   if (target->is_output_micromips())
11578                     target_is_micromips_code = true;
11579                   else
11580                     target_is_16_bit_code = true;
11581                 }
11582             }
11583           else
11584             value = target->mips_stubs_section()->stub_address(mips_sym);
11585
11586           symval.set_output_value(value);
11587           psymval = &symval;
11588         }
11589     }
11590
11591   // TRUE if the symbol referred to by this relocation is "_gp_disp".
11592   // Note that such a symbol must always be a global symbol.
11593   bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11594                   && !object->is_newabi());
11595
11596   // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11597   // Note that such a symbol must always be a global symbol.
11598   bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11599
11600
11601   if (gp_disp)
11602     {
11603       if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11604         gold_error_at_location(relinfo, relnum, r_offset,
11605           _("relocations against _gp_disp are permitted only"
11606             " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11607     }
11608   else if (gnu_local_gp)
11609     {
11610       // __gnu_local_gp is _gp symbol.
11611       symval.set_output_value(target->adjusted_gp_value(object));
11612       psymval = &symval;
11613     }
11614
11615   // If this is a reference to a 16-bit function with a stub, we need
11616   // to redirect the relocation to the stub unless:
11617   //
11618   // (a) the relocation is for a MIPS16 JAL;
11619   //
11620   // (b) the relocation is for a MIPS16 PIC call, and there are no
11621   //     non-MIPS16 uses of the GOT slot; or
11622   //
11623   // (c) the section allows direct references to MIPS16 functions.
11624   if (r_type != elfcpp::R_MIPS16_26
11625       && ((mips_sym != NULL
11626            && mips_sym->has_mips16_fn_stub()
11627            && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11628           || (mips_sym == NULL
11629               && object->get_local_mips16_fn_stub(r_sym) != NULL))
11630       && !object->section_allows_mips16_refs(relinfo->data_shndx))
11631     {
11632       // This is a 32- or 64-bit call to a 16-bit function.  We should
11633       // have already noticed that we were going to need the
11634       // stub.
11635       Mips_address value;
11636       if (mips_sym == NULL)
11637         value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11638       else
11639         {
11640           gold_assert(mips_sym->need_fn_stub());
11641           if (mips_sym->has_la25_stub())
11642             value = target->la25_stub_section()->stub_address(mips_sym);
11643           else
11644             {
11645               value = mips_sym->template
11646                       get_mips16_fn_stub<big_endian>()->output_address();
11647             }
11648           }
11649       symval.set_output_value(value);
11650       psymval = &symval;
11651       changed_symbol_value = true;
11652
11653       // The target is 16-bit, but the stub isn't.
11654       target_is_16_bit_code = false;
11655     }
11656   // If this is a MIPS16 call with a stub, that is made through the PLT or
11657   // to a standard MIPS function, we need to redirect the call to the stub.
11658   // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11659   // indirect calls should use an indirect stub instead.
11660   else if (r_type == elfcpp::R_MIPS16_26
11661            && ((mips_sym != NULL
11662                 && (mips_sym->has_mips16_call_stub()
11663                     || mips_sym->has_mips16_call_fp_stub()))
11664                || (mips_sym == NULL
11665                    && object->get_local_mips16_call_stub(r_sym) != NULL))
11666            && ((mips_sym != NULL && mips_sym->has_plt_offset())
11667                || !target_is_16_bit_code))
11668     {
11669       Mips16_stub_section<size, big_endian>* call_stub;
11670       if (mips_sym == NULL)
11671         call_stub = object->get_local_mips16_call_stub(r_sym);
11672       else
11673         {
11674           // If both call_stub and call_fp_stub are defined, we can figure
11675           // out which one to use by checking which one appears in the input
11676           // file.
11677           if (mips_sym->has_mips16_call_stub()
11678               && mips_sym->has_mips16_call_fp_stub())
11679             {
11680               call_stub = NULL;
11681               for (unsigned int i = 1; i < object->shnum(); ++i)
11682                 {
11683                   if (object->is_mips16_call_fp_stub_section(i))
11684                     {
11685                       call_stub = mips_sym->template
11686                                   get_mips16_call_fp_stub<big_endian>();
11687                       break;
11688                     }
11689
11690                 }
11691               if (call_stub == NULL)
11692                 call_stub =
11693                   mips_sym->template get_mips16_call_stub<big_endian>();
11694             }
11695           else if (mips_sym->has_mips16_call_stub())
11696             call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11697           else
11698             call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11699         }
11700
11701       symval.set_output_value(call_stub->output_address());
11702       psymval = &symval;
11703       changed_symbol_value = true;
11704     }
11705   // If this is a direct call to a PIC function, redirect to the
11706   // non-PIC stub.
11707   else if (mips_sym != NULL
11708            && mips_sym->has_la25_stub()
11709            && relocation_needs_la25_stub<size, big_endian>(
11710                                        object, r_type, target_is_16_bit_code))
11711     {
11712       Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11713       if (mips_sym->is_micromips())
11714         value += 1;
11715       symval.set_output_value(value);
11716       psymval = &symval;
11717     }
11718   // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11719   // entry is used if a standard PLT entry has also been made.
11720   else if ((r_type == elfcpp::R_MIPS16_26
11721             || r_type == elfcpp::R_MICROMIPS_26_S1)
11722           && mips_sym != NULL
11723           && mips_sym->has_plt_offset()
11724           && mips_sym->has_comp_plt_offset()
11725           && mips_sym->has_mips_plt_offset())
11726     {
11727       Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11728                             + 1);
11729       symval.set_output_value(value);
11730       psymval = &symval;
11731
11732       target_is_16_bit_code = !target->is_output_micromips();
11733       target_is_micromips_code = target->is_output_micromips();
11734     }
11735
11736   // Make sure MIPS16 and microMIPS are not used together.
11737   if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11738       || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11739    {
11740       gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11741    }
11742
11743   // Calls from 16-bit code to 32-bit code and vice versa require the
11744   // mode change.  However, we can ignore calls to undefined weak symbols,
11745   // which should never be executed at runtime.  This exception is important
11746   // because the assembly writer may have "known" that any definition of the
11747   // symbol would be 16-bit code, and that direct jumps were therefore
11748   // acceptable.
11749   cross_mode_jump =
11750     (!(gsym != NULL && gsym->is_weak_undefined())
11751      && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11752          || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11753          || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11754              && (target_is_16_bit_code || target_is_micromips_code))));
11755
11756   bool local = (mips_sym == NULL
11757                 || (mips_sym->got_only_for_calls()
11758                     ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11759                     : symbol_references_local(mips_sym,
11760                                               mips_sym->has_dynsym_index())));
11761
11762   // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11763   // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
11764   // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11765   if (got_page_reloc(r_type) && !local)
11766     r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11767                                       : elfcpp::R_MIPS_GOT_DISP);
11768
11769   unsigned int got_offset = 0;
11770   int gp_offset = 0;
11771
11772   // Whether we have to extract addend from instruction.
11773   bool extract_addend = rel_type == elfcpp::SHT_REL;
11774   unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11775
11776   Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11777
11778   // For Mips64 N64 ABI, there may be up to three operations specified per
11779   // record, by the fields r_type, r_type2, and r_type3. The first operation
11780   // takes its addend from the relocation record. Each subsequent operation
11781   // takes as its addend the result of the previous operation.
11782   // The first operation in a record which references a symbol uses the symbol
11783   // implied by r_sym. The next operation in a record which references a symbol
11784   // uses the special symbol value given by the r_ssym field. A third operation
11785   // in a record which references a symbol will assume a NULL symbol,
11786   // i.e. value zero.
11787
11788   // TODO(Vladimir)
11789   // Check if a record references to a symbol.
11790   for (unsigned int i = 0; i < 3; ++i)
11791     {
11792       if (r_types[i] == elfcpp::R_MIPS_NONE)
11793         break;
11794
11795       // If we didn't apply previous relocation, use its result as addend
11796       // for current.
11797       if (this->calculate_only_)
11798         {
11799           r_addend = this->calculated_value_;
11800           extract_addend = false;
11801         }
11802
11803       // In the N32 and 64-bit ABIs there may be multiple consecutive
11804       // relocations for the same offset.  In that case we are
11805       // supposed to treat the output of each relocation as the addend
11806       // for the next.  For N64 ABI, we are checking offsets only in a
11807       // third operation in a record (r_type3).
11808       this->calculate_only_ =
11809         (object->is_n64() && i < 2
11810          ? r_types[i+1] != elfcpp::R_MIPS_NONE
11811          : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
11812
11813       if (object->is_n64())
11814         {
11815           if (i == 1)
11816             {
11817               // Handle special symbol for r_type2 relocation type.
11818               switch (r_ssym)
11819                 {
11820                 case RSS_UNDEF:
11821                   symval.set_output_value(0);
11822                   break;
11823                 case RSS_GP:
11824                   symval.set_output_value(target->gp_value());
11825                   break;
11826                 case RSS_GP0:
11827                   symval.set_output_value(object->gp_value());
11828                   break;
11829                 case RSS_LOC:
11830                   symval.set_output_value(address);
11831                   break;
11832                 default:
11833                   gold_unreachable();
11834                 }
11835               psymval = &symval;
11836             }
11837           else if (i == 2)
11838            {
11839             // For r_type3 symbol value is 0.
11840             symval.set_output_value(0);
11841            }
11842         }
11843
11844       bool update_got_entry = false;
11845       switch (r_types[i])
11846         {
11847         case elfcpp::R_MIPS_NONE:
11848           break;
11849         case elfcpp::R_MIPS_16:
11850           reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11851                                             extract_addend,
11852                                             this->calculate_only_,
11853                                             &this->calculated_value_);
11854           break;
11855
11856         case elfcpp::R_MIPS_32:
11857           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11858                                         target))
11859             reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11860                                               extract_addend,
11861                                               this->calculate_only_,
11862                                               &this->calculated_value_);
11863           if (mips_sym != NULL
11864               && (mips_sym->is_mips16() || mips_sym->is_micromips())
11865               && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11866             {
11867               // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11868               // already updated by adding +1.
11869               if (mips_sym->has_mips16_fn_stub())
11870                 {
11871                   gold_assert(mips_sym->need_fn_stub());
11872                   Mips16_stub_section<size, big_endian>* fn_stub =
11873                     mips_sym->template get_mips16_fn_stub<big_endian>();
11874
11875                   symval.set_output_value(fn_stub->output_address());
11876                   psymval = &symval;
11877                 }
11878               got_offset = mips_sym->global_gotoffset();
11879               update_got_entry = true;
11880             }
11881           break;
11882
11883         case elfcpp::R_MIPS_64:
11884           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11885                                         target))
11886             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11887                                               extract_addend,
11888                                               this->calculate_only_,
11889                                               &this->calculated_value_, false);
11890           else if (target->is_output_n64() && r_addend != 0)
11891             // Only apply the addend.  The static relocation was RELA, but the
11892             // dynamic relocation is REL, so we need to apply the addend.
11893             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11894                                               extract_addend,
11895                                               this->calculate_only_,
11896                                               &this->calculated_value_, true);
11897           break;
11898         case elfcpp::R_MIPS_REL32:
11899           gold_unreachable();
11900
11901         case elfcpp::R_MIPS_PC32:
11902           reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11903                                               r_addend, extract_addend,
11904                                               this->calculate_only_,
11905                                               &this->calculated_value_);
11906           break;
11907
11908         case elfcpp::R_MIPS16_26:
11909           // The calculation for R_MIPS16_26 is just the same as for an
11910           // R_MIPS_26.  It's only the storage of the relocated field into
11911           // the output file that's different.  So, we just fall through to the
11912           // R_MIPS_26 case here.
11913         case elfcpp::R_MIPS_26:
11914         case elfcpp::R_MICROMIPS_26_S1:
11915           reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11916               gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11917               r_types[i], target->jal_to_bal(), this->calculate_only_,
11918               &this->calculated_value_);
11919           break;
11920
11921         case elfcpp::R_MIPS_HI16:
11922         case elfcpp::R_MIPS16_HI16:
11923         case elfcpp::R_MICROMIPS_HI16:
11924           if (rel_type == elfcpp::SHT_RELA)
11925             reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11926                                                    r_addend, address,
11927                                                    gp_disp, r_types[i],
11928                                                    extract_addend, 0,
11929                                                    target,
11930                                                    this->calculate_only_,
11931                                                    &this->calculated_value_);
11932           else if (rel_type == elfcpp::SHT_REL)
11933             reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11934                                                 address, gp_disp, r_types[i],
11935                                                 r_sym, extract_addend);
11936           else
11937             gold_unreachable();
11938           break;
11939
11940         case elfcpp::R_MIPS_LO16:
11941         case elfcpp::R_MIPS16_LO16:
11942         case elfcpp::R_MICROMIPS_LO16:
11943         case elfcpp::R_MICROMIPS_HI0_LO16:
11944           reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11945                                               r_addend, extract_addend, address,
11946                                               gp_disp, r_types[i], r_sym,
11947                                               rel_type, this->calculate_only_,
11948                                               &this->calculated_value_);
11949           break;
11950
11951         case elfcpp::R_MIPS_LITERAL:
11952         case elfcpp::R_MICROMIPS_LITERAL:
11953           // Because we don't merge literal sections, we can handle this
11954           // just like R_MIPS_GPREL16.  In the long run, we should merge
11955           // shared literals, and then we will need to additional work
11956           // here.
11957
11958           // Fall through.
11959
11960         case elfcpp::R_MIPS_GPREL16:
11961         case elfcpp::R_MIPS16_GPREL:
11962         case elfcpp::R_MICROMIPS_GPREL7_S2:
11963         case elfcpp::R_MICROMIPS_GPREL16:
11964           reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11965                                              target->adjusted_gp_value(object),
11966                                              r_addend, extract_addend,
11967                                              gsym == NULL, r_types[i],
11968                                              this->calculate_only_,
11969                                              &this->calculated_value_);
11970           break;
11971
11972         case elfcpp::R_MIPS_PC16:
11973           reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11974                                               r_addend, extract_addend,
11975                                               this->calculate_only_,
11976                                               &this->calculated_value_);
11977           break;
11978
11979         case elfcpp::R_MIPS_PC21_S2:
11980           reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11981                                               r_addend, extract_addend,
11982                                               this->calculate_only_,
11983                                               &this->calculated_value_);
11984           break;
11985
11986         case elfcpp::R_MIPS_PC26_S2:
11987           reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11988                                               r_addend, extract_addend,
11989                                               this->calculate_only_,
11990                                               &this->calculated_value_);
11991           break;
11992
11993         case elfcpp::R_MIPS_PC18_S3:
11994           reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11995                                               r_addend, extract_addend,
11996                                               this->calculate_only_,
11997                                               &this->calculated_value_);
11998           break;
11999
12000         case elfcpp::R_MIPS_PC19_S2:
12001           reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
12002                                               r_addend, extract_addend,
12003                                               this->calculate_only_,
12004                                               &this->calculated_value_);
12005           break;
12006
12007         case elfcpp::R_MIPS_PCHI16:
12008           if (rel_type == elfcpp::SHT_RELA)
12009             reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
12010                                                      r_addend, address,
12011                                                      extract_addend, 0,
12012                                                      this->calculate_only_,
12013                                                      &this->calculated_value_);
12014           else if (rel_type == elfcpp::SHT_REL)
12015             reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
12016                                                   r_addend, address, r_sym,
12017                                                   extract_addend);
12018           else
12019             gold_unreachable();
12020           break;
12021
12022         case elfcpp::R_MIPS_PCLO16:
12023           reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
12024                                                 extract_addend, address, r_sym,
12025                                                 rel_type, this->calculate_only_,
12026                                                 &this->calculated_value_);
12027           break;
12028         case elfcpp::R_MICROMIPS_PC7_S1:
12029           reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
12030                                                       address, r_addend,
12031                                                       extract_addend,
12032                                                       this->calculate_only_,
12033                                                       &this->calculated_value_);
12034           break;
12035         case elfcpp::R_MICROMIPS_PC10_S1:
12036           reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
12037                                                       psymval, address,
12038                                                       r_addend, extract_addend,
12039                                                       this->calculate_only_,
12040                                                       &this->calculated_value_);
12041           break;
12042         case elfcpp::R_MICROMIPS_PC16_S1:
12043           reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
12044                                                       psymval, address,
12045                                                       r_addend, extract_addend,
12046                                                       this->calculate_only_,
12047                                                       &this->calculated_value_);
12048           break;
12049         case elfcpp::R_MIPS_GPREL32:
12050           reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12051                                               target->adjusted_gp_value(object),
12052                                               r_addend, extract_addend,
12053                                               this->calculate_only_,
12054                                               &this->calculated_value_);
12055           break;
12056         case elfcpp::R_MIPS_GOT_HI16:
12057         case elfcpp::R_MIPS_CALL_HI16:
12058         case elfcpp::R_MICROMIPS_GOT_HI16:
12059         case elfcpp::R_MICROMIPS_CALL_HI16:
12060           if (gsym != NULL)
12061             got_offset = target->got_section()->got_offset(gsym,
12062                                                            GOT_TYPE_STANDARD,
12063                                                            object);
12064           else
12065             got_offset = target->got_section()->got_offset(r_sym,
12066                                                            GOT_TYPE_STANDARD,
12067                                                            object, r_addend);
12068           gp_offset = target->got_section()->gp_offset(got_offset, object);
12069           reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
12070                                                   this->calculate_only_,
12071                                                   &this->calculated_value_);
12072           update_got_entry = changed_symbol_value;
12073           break;
12074
12075         case elfcpp::R_MIPS_GOT_LO16:
12076         case elfcpp::R_MIPS_CALL_LO16:
12077         case elfcpp::R_MICROMIPS_GOT_LO16:
12078         case elfcpp::R_MICROMIPS_CALL_LO16:
12079           if (gsym != NULL)
12080             got_offset = target->got_section()->got_offset(gsym,
12081                                                            GOT_TYPE_STANDARD,
12082                                                            object);
12083           else
12084             got_offset = target->got_section()->got_offset(r_sym,
12085                                                            GOT_TYPE_STANDARD,
12086                                                            object, r_addend);
12087           gp_offset = target->got_section()->gp_offset(got_offset, object);
12088           reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
12089                                                   this->calculate_only_,
12090                                                   &this->calculated_value_);
12091           update_got_entry = changed_symbol_value;
12092           break;
12093
12094         case elfcpp::R_MIPS_GOT_DISP:
12095         case elfcpp::R_MICROMIPS_GOT_DISP:
12096         case elfcpp::R_MIPS_EH:
12097           if (gsym != NULL)
12098             got_offset = target->got_section()->got_offset(gsym,
12099                                                            GOT_TYPE_STANDARD,
12100                                                            object);
12101           else
12102             got_offset = target->got_section()->got_offset(r_sym,
12103                                                            GOT_TYPE_STANDARD,
12104                                                            object, r_addend);
12105           gp_offset = target->got_section()->gp_offset(got_offset, object);
12106           if (eh_reloc(r_types[i]))
12107             reloc_status = Reloc_funcs::releh(view, gp_offset,
12108                                               this->calculate_only_,
12109                                               &this->calculated_value_);
12110           else
12111             reloc_status = Reloc_funcs::relgot(view, gp_offset,
12112                                                this->calculate_only_,
12113                                                &this->calculated_value_);
12114           break;
12115         case elfcpp::R_MIPS_CALL16:
12116         case elfcpp::R_MIPS16_CALL16:
12117         case elfcpp::R_MICROMIPS_CALL16:
12118           gold_assert(gsym != NULL);
12119           got_offset = target->got_section()->got_offset(gsym,
12120                                                          GOT_TYPE_STANDARD,
12121                                                          object);
12122           gp_offset = target->got_section()->gp_offset(got_offset, object);
12123           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12124                                              this->calculate_only_,
12125                                              &this->calculated_value_);
12126           // TODO(sasa): We should also initialize update_got_entry
12127           // in other place swhere relgot is called.
12128           update_got_entry = changed_symbol_value;
12129           break;
12130
12131         case elfcpp::R_MIPS_GOT16:
12132         case elfcpp::R_MIPS16_GOT16:
12133         case elfcpp::R_MICROMIPS_GOT16:
12134           if (gsym != NULL)
12135             {
12136               got_offset = target->got_section()->got_offset(gsym,
12137                                                              GOT_TYPE_STANDARD,
12138                                                              object);
12139               gp_offset = target->got_section()->gp_offset(got_offset, object);
12140               reloc_status = Reloc_funcs::relgot(view, gp_offset,
12141                                                  this->calculate_only_,
12142                                                  &this->calculated_value_);
12143             }
12144           else
12145             {
12146               if (rel_type == elfcpp::SHT_RELA)
12147                 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
12148                                                       psymval, r_addend,
12149                                                       extract_addend, 0,
12150                                                       target,
12151                                                       this->calculate_only_,
12152                                                       &this->calculated_value_);
12153               else if (rel_type == elfcpp::SHT_REL)
12154                 reloc_status = Reloc_funcs::relgot16_local(view, object,
12155                                                            psymval, r_addend,
12156                                                            extract_addend,
12157                                                            r_types[i], r_sym);
12158               else
12159                 gold_unreachable();
12160             }
12161           update_got_entry = changed_symbol_value;
12162           break;
12163
12164         case elfcpp::R_MIPS_TLS_GD:
12165         case elfcpp::R_MIPS16_TLS_GD:
12166         case elfcpp::R_MICROMIPS_TLS_GD:
12167           if (gsym != NULL)
12168             got_offset = target->got_section()->got_offset(gsym,
12169                                                            GOT_TYPE_TLS_PAIR,
12170                                                            object);
12171           else
12172             got_offset = target->got_section()->got_offset(r_sym,
12173                                                            GOT_TYPE_TLS_PAIR,
12174                                                            object, r_addend);
12175           gp_offset = target->got_section()->gp_offset(got_offset, object);
12176           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12177                                              this->calculate_only_,
12178                                              &this->calculated_value_);
12179           break;
12180
12181         case elfcpp::R_MIPS_TLS_GOTTPREL:
12182         case elfcpp::R_MIPS16_TLS_GOTTPREL:
12183         case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12184           if (gsym != NULL)
12185             got_offset = target->got_section()->got_offset(gsym,
12186                                                            GOT_TYPE_TLS_OFFSET,
12187                                                            object);
12188           else
12189             got_offset = target->got_section()->got_offset(r_sym,
12190                                                            GOT_TYPE_TLS_OFFSET,
12191                                                            object, r_addend);
12192           gp_offset = target->got_section()->gp_offset(got_offset, object);
12193           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12194                                              this->calculate_only_,
12195                                              &this->calculated_value_);
12196           break;
12197
12198         case elfcpp::R_MIPS_TLS_LDM:
12199         case elfcpp::R_MIPS16_TLS_LDM:
12200         case elfcpp::R_MICROMIPS_TLS_LDM:
12201           // Relocate the field with the offset of the GOT entry for
12202           // the module index.
12203           got_offset = target->got_section()->tls_ldm_offset(object);
12204           gp_offset = target->got_section()->gp_offset(got_offset, object);
12205           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12206                                              this->calculate_only_,
12207                                              &this->calculated_value_);
12208           break;
12209
12210         case elfcpp::R_MIPS_GOT_PAGE:
12211         case elfcpp::R_MICROMIPS_GOT_PAGE:
12212           reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12213                                                  r_addend, extract_addend,
12214                                                  this->calculate_only_,
12215                                                  &this->calculated_value_);
12216           break;
12217
12218         case elfcpp::R_MIPS_GOT_OFST:
12219         case elfcpp::R_MICROMIPS_GOT_OFST:
12220           reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12221                                                  r_addend, extract_addend,
12222                                                  local, this->calculate_only_,
12223                                                  &this->calculated_value_);
12224           break;
12225
12226         case elfcpp::R_MIPS_JALR:
12227         case elfcpp::R_MICROMIPS_JALR:
12228           // This relocation is only a hint.  In some cases, we optimize
12229           // it into a bal instruction.  But we don't try to optimize
12230           // when the symbol does not resolve locally.
12231           if (gsym == NULL
12232               || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12233             reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12234                                                 r_addend, extract_addend,
12235                                                 cross_mode_jump, r_types[i],
12236                                                 target->jalr_to_bal(),
12237                                                 target->jr_to_b(),
12238                                                 this->calculate_only_,
12239                                                 &this->calculated_value_);
12240           break;
12241
12242         case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12243         case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12244         case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12245           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12246                                                  elfcpp::DTP_OFFSET, r_addend,
12247                                                  extract_addend,
12248                                                  this->calculate_only_,
12249                                                  &this->calculated_value_);
12250           break;
12251         case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12252         case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12253         case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12254           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12255                                                  elfcpp::DTP_OFFSET, r_addend,
12256                                                  extract_addend,
12257                                                  this->calculate_only_,
12258                                                  &this->calculated_value_);
12259           break;
12260         case elfcpp::R_MIPS_TLS_DTPREL32:
12261         case elfcpp::R_MIPS_TLS_DTPREL64:
12262           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12263                                                elfcpp::DTP_OFFSET, r_addend,
12264                                                extract_addend,
12265                                                this->calculate_only_,
12266                                                &this->calculated_value_);
12267           break;
12268         case elfcpp::R_MIPS_TLS_TPREL_HI16:
12269         case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12270         case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12271           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12272                                                  elfcpp::TP_OFFSET, r_addend,
12273                                                  extract_addend,
12274                                                  this->calculate_only_,
12275                                                  &this->calculated_value_);
12276           break;
12277         case elfcpp::R_MIPS_TLS_TPREL_LO16:
12278         case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12279         case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12280           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12281                                                  elfcpp::TP_OFFSET, r_addend,
12282                                                  extract_addend,
12283                                                  this->calculate_only_,
12284                                                  &this->calculated_value_);
12285           break;
12286         case elfcpp::R_MIPS_TLS_TPREL32:
12287         case elfcpp::R_MIPS_TLS_TPREL64:
12288           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12289                                                elfcpp::TP_OFFSET, r_addend,
12290                                                extract_addend,
12291                                                this->calculate_only_,
12292                                                &this->calculated_value_);
12293           break;
12294         case elfcpp::R_MIPS_SUB:
12295         case elfcpp::R_MICROMIPS_SUB:
12296           reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12297                                              extract_addend,
12298                                              this->calculate_only_,
12299                                              &this->calculated_value_);
12300           break;
12301         case elfcpp::R_MIPS_HIGHER:
12302         case elfcpp::R_MICROMIPS_HIGHER:
12303           reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
12304                                                 extract_addend,
12305                                                 this->calculate_only_,
12306                                                 &this->calculated_value_);
12307           break;
12308         case elfcpp::R_MIPS_HIGHEST:
12309         case elfcpp::R_MICROMIPS_HIGHEST:
12310           reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12311                                                  r_addend, extract_addend,
12312                                                  this->calculate_only_,
12313                                                  &this->calculated_value_);
12314           break;
12315         default:
12316           gold_error_at_location(relinfo, relnum, r_offset,
12317                                  _("unsupported reloc %u"), r_types[i]);
12318           break;
12319         }
12320
12321       if (update_got_entry)
12322         {
12323           Mips_output_data_got<size, big_endian>* got = target->got_section();
12324           if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12325             got->update_got_entry(got->get_primary_got_offset(mips_sym),
12326                                   psymval->value(object, 0));
12327           else
12328             got->update_got_entry(got_offset, psymval->value(object, 0));
12329         }
12330     }
12331
12332   bool jal_shuffle = jal_reloc(r_type);
12333   Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12334
12335   // Report any errors.
12336   switch (reloc_status)
12337     {
12338     case Reloc_funcs::STATUS_OKAY:
12339       break;
12340     case Reloc_funcs::STATUS_OVERFLOW:
12341       if (gsym == NULL)
12342         gold_error_at_location(relinfo, relnum, r_offset,
12343                                _("relocation overflow: "
12344                                  "%u against local symbol %u in %s"),
12345                                r_type, r_sym, object->name().c_str());
12346       else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12347         gold_error_at_location(relinfo, relnum, r_offset,
12348                                _("relocation overflow: "
12349                                  "%u against '%s' defined in %s"),
12350                                r_type, gsym->demangled_name().c_str(),
12351                                gsym->object()->name().c_str());
12352       else
12353         gold_error_at_location(relinfo, relnum, r_offset,
12354                                _("relocation overflow: %u against '%s'"),
12355                                r_type, gsym->demangled_name().c_str());
12356       break;
12357     case Reloc_funcs::STATUS_BAD_RELOC:
12358       gold_error_at_location(relinfo, relnum, r_offset,
12359         _("unexpected opcode while processing relocation"));
12360       break;
12361     case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12362       gold_error_at_location(relinfo, relnum, r_offset,
12363         _("unaligned PC-relative relocation"));
12364       break;
12365     default:
12366       gold_unreachable();
12367     }
12368
12369   return true;
12370 }
12371
12372 // Get the Reference_flags for a particular relocation.
12373
12374 template<int size, bool big_endian>
12375 int
12376 Target_mips<size, big_endian>::Scan::get_reference_flags(
12377                        unsigned int r_type)
12378 {
12379   switch (r_type)
12380     {
12381     case elfcpp::R_MIPS_NONE:
12382       // No symbol reference.
12383       return 0;
12384
12385     case elfcpp::R_MIPS_16:
12386     case elfcpp::R_MIPS_32:
12387     case elfcpp::R_MIPS_64:
12388     case elfcpp::R_MIPS_HI16:
12389     case elfcpp::R_MIPS_LO16:
12390     case elfcpp::R_MIPS_HIGHER:
12391     case elfcpp::R_MIPS_HIGHEST:
12392     case elfcpp::R_MIPS16_HI16:
12393     case elfcpp::R_MIPS16_LO16:
12394     case elfcpp::R_MICROMIPS_HI16:
12395     case elfcpp::R_MICROMIPS_LO16:
12396     case elfcpp::R_MICROMIPS_HIGHER:
12397     case elfcpp::R_MICROMIPS_HIGHEST:
12398       return Symbol::ABSOLUTE_REF;
12399
12400     case elfcpp::R_MIPS_26:
12401     case elfcpp::R_MIPS16_26:
12402     case elfcpp::R_MICROMIPS_26_S1:
12403       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12404
12405     case elfcpp::R_MIPS_PC18_S3:
12406     case elfcpp::R_MIPS_PC19_S2:
12407     case elfcpp::R_MIPS_PCHI16:
12408     case elfcpp::R_MIPS_PCLO16:
12409     case elfcpp::R_MIPS_GPREL32:
12410     case elfcpp::R_MIPS_GPREL16:
12411     case elfcpp::R_MIPS_REL32:
12412     case elfcpp::R_MIPS16_GPREL:
12413       return Symbol::RELATIVE_REF;
12414
12415     case elfcpp::R_MIPS_PC16:
12416     case elfcpp::R_MIPS_PC32:
12417     case elfcpp::R_MIPS_PC21_S2:
12418     case elfcpp::R_MIPS_PC26_S2:
12419     case elfcpp::R_MIPS_JALR:
12420     case elfcpp::R_MICROMIPS_JALR:
12421       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12422
12423     case elfcpp::R_MIPS_GOT16:
12424     case elfcpp::R_MIPS_CALL16:
12425     case elfcpp::R_MIPS_GOT_DISP:
12426     case elfcpp::R_MIPS_GOT_HI16:
12427     case elfcpp::R_MIPS_GOT_LO16:
12428     case elfcpp::R_MIPS_CALL_HI16:
12429     case elfcpp::R_MIPS_CALL_LO16:
12430     case elfcpp::R_MIPS_LITERAL:
12431     case elfcpp::R_MIPS_GOT_PAGE:
12432     case elfcpp::R_MIPS_GOT_OFST:
12433     case elfcpp::R_MIPS16_GOT16:
12434     case elfcpp::R_MIPS16_CALL16:
12435     case elfcpp::R_MICROMIPS_GOT16:
12436     case elfcpp::R_MICROMIPS_CALL16:
12437     case elfcpp::R_MICROMIPS_GOT_HI16:
12438     case elfcpp::R_MICROMIPS_GOT_LO16:
12439     case elfcpp::R_MICROMIPS_CALL_HI16:
12440     case elfcpp::R_MICROMIPS_CALL_LO16:
12441     case elfcpp::R_MIPS_EH:
12442       // Absolute in GOT.
12443       return Symbol::RELATIVE_REF;
12444
12445     case elfcpp::R_MIPS_TLS_DTPMOD32:
12446     case elfcpp::R_MIPS_TLS_DTPREL32:
12447     case elfcpp::R_MIPS_TLS_DTPMOD64:
12448     case elfcpp::R_MIPS_TLS_DTPREL64:
12449     case elfcpp::R_MIPS_TLS_GD:
12450     case elfcpp::R_MIPS_TLS_LDM:
12451     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12452     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12453     case elfcpp::R_MIPS_TLS_GOTTPREL:
12454     case elfcpp::R_MIPS_TLS_TPREL32:
12455     case elfcpp::R_MIPS_TLS_TPREL64:
12456     case elfcpp::R_MIPS_TLS_TPREL_HI16:
12457     case elfcpp::R_MIPS_TLS_TPREL_LO16:
12458     case elfcpp::R_MIPS16_TLS_GD:
12459     case elfcpp::R_MIPS16_TLS_GOTTPREL:
12460     case elfcpp::R_MICROMIPS_TLS_GD:
12461     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12462     case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12463     case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12464       return Symbol::TLS_REF;
12465
12466     case elfcpp::R_MIPS_COPY:
12467     case elfcpp::R_MIPS_JUMP_SLOT:
12468     default:
12469       // Not expected.  We will give an error later.
12470       return 0;
12471     }
12472 }
12473
12474 // Report an unsupported relocation against a local symbol.
12475
12476 template<int size, bool big_endian>
12477 void
12478 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12479                         Sized_relobj_file<size, big_endian>* object,
12480                         unsigned int r_type)
12481 {
12482   gold_error(_("%s: unsupported reloc %u against local symbol"),
12483              object->name().c_str(), r_type);
12484 }
12485
12486 // Report an unsupported relocation against a global symbol.
12487
12488 template<int size, bool big_endian>
12489 void
12490 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12491                         Sized_relobj_file<size, big_endian>* object,
12492                         unsigned int r_type,
12493                         Symbol* gsym)
12494 {
12495   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12496              object->name().c_str(), r_type, gsym->demangled_name().c_str());
12497 }
12498
12499 // Return printable name for ABI.
12500 template<int size, bool big_endian>
12501 const char*
12502 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12503 {
12504   switch (e_flags & elfcpp::EF_MIPS_ABI)
12505     {
12506     case 0:
12507       if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12508         return "N32";
12509       else if (size == 64)
12510         return "64";
12511       else
12512         return "none";
12513     case elfcpp::E_MIPS_ABI_O32:
12514       return "O32";
12515     case elfcpp::E_MIPS_ABI_O64:
12516       return "O64";
12517     case elfcpp::E_MIPS_ABI_EABI32:
12518       return "EABI32";
12519     case elfcpp::E_MIPS_ABI_EABI64:
12520       return "EABI64";
12521     default:
12522       return "unknown abi";
12523     }
12524 }
12525
12526 template<int size, bool big_endian>
12527 const char*
12528 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12529 {
12530   switch (e_flags & elfcpp::EF_MIPS_MACH)
12531     {
12532     case elfcpp::E_MIPS_MACH_3900:
12533       return "mips:3900";
12534     case elfcpp::E_MIPS_MACH_4010:
12535       return "mips:4010";
12536     case elfcpp::E_MIPS_MACH_4100:
12537       return "mips:4100";
12538     case elfcpp::E_MIPS_MACH_4111:
12539       return "mips:4111";
12540     case elfcpp::E_MIPS_MACH_4120:
12541       return "mips:4120";
12542     case elfcpp::E_MIPS_MACH_4650:
12543       return "mips:4650";
12544     case elfcpp::E_MIPS_MACH_5400:
12545       return "mips:5400";
12546     case elfcpp::E_MIPS_MACH_5500:
12547       return "mips:5500";
12548     case elfcpp::E_MIPS_MACH_5900:
12549       return "mips:5900";
12550     case elfcpp::E_MIPS_MACH_SB1:
12551       return "mips:sb1";
12552     case elfcpp::E_MIPS_MACH_9000:
12553       return "mips:9000";
12554     case elfcpp::E_MIPS_MACH_LS2E:
12555       return "mips:loongson_2e";
12556     case elfcpp::E_MIPS_MACH_LS2F:
12557       return "mips:loongson_2f";
12558     case elfcpp::E_MIPS_MACH_LS3A:
12559       return "mips:loongson_3a";
12560     case elfcpp::E_MIPS_MACH_OCTEON:
12561       return "mips:octeon";
12562     case elfcpp::E_MIPS_MACH_OCTEON2:
12563       return "mips:octeon2";
12564     case elfcpp::E_MIPS_MACH_OCTEON3:
12565       return "mips:octeon3";
12566     case elfcpp::E_MIPS_MACH_XLR:
12567       return "mips:xlr";
12568     default:
12569       switch (e_flags & elfcpp::EF_MIPS_ARCH)
12570         {
12571         default:
12572         case elfcpp::E_MIPS_ARCH_1:
12573           return "mips:3000";
12574
12575         case elfcpp::E_MIPS_ARCH_2:
12576           return "mips:6000";
12577
12578         case elfcpp::E_MIPS_ARCH_3:
12579           return "mips:4000";
12580
12581         case elfcpp::E_MIPS_ARCH_4:
12582           return "mips:8000";
12583
12584         case elfcpp::E_MIPS_ARCH_5:
12585           return "mips:mips5";
12586
12587         case elfcpp::E_MIPS_ARCH_32:
12588           return "mips:isa32";
12589
12590         case elfcpp::E_MIPS_ARCH_64:
12591           return "mips:isa64";
12592
12593         case elfcpp::E_MIPS_ARCH_32R2:
12594           return "mips:isa32r2";
12595
12596         case elfcpp::E_MIPS_ARCH_32R6:
12597           return "mips:isa32r6";
12598
12599         case elfcpp::E_MIPS_ARCH_64R2:
12600           return "mips:isa64r2";
12601
12602         case elfcpp::E_MIPS_ARCH_64R6:
12603           return "mips:isa64r6";
12604         }
12605     }
12606     return "unknown CPU";
12607 }
12608
12609 template<int size, bool big_endian>
12610 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12611 {
12612   size,                 // size
12613   big_endian,           // is_big_endian
12614   elfcpp::EM_MIPS,      // machine_code
12615   true,                 // has_make_symbol
12616   false,                // has_resolve
12617   false,                // has_code_fill
12618   true,                 // is_default_stack_executable
12619   false,                // can_icf_inline_merge_sections
12620   '\0',                 // wrap_char
12621   size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1",      // dynamic_linker
12622   0x400000,             // default_text_segment_address
12623   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
12624   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
12625   false,                // isolate_execinstr
12626   0,                    // rosegment_gap
12627   elfcpp::SHN_UNDEF,    // small_common_shndx
12628   elfcpp::SHN_UNDEF,    // large_common_shndx
12629   0,                    // small_common_section_flags
12630   0,                    // large_common_section_flags
12631   NULL,                 // attributes_section
12632   NULL,                 // attributes_vendor
12633   "__start",            // entry_symbol_name
12634   32,                   // hash_entry_size
12635   elfcpp::SHT_PROGBITS, // unwind_section_type
12636 };
12637
12638 template<int size, bool big_endian>
12639 class Target_mips_nacl : public Target_mips<size, big_endian>
12640 {
12641  public:
12642   Target_mips_nacl()
12643     : Target_mips<size, big_endian>(&mips_nacl_info)
12644   { }
12645
12646  private:
12647   static const Target::Target_info mips_nacl_info;
12648 };
12649
12650 template<int size, bool big_endian>
12651 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12652 {
12653   size,                 // size
12654   big_endian,           // is_big_endian
12655   elfcpp::EM_MIPS,      // machine_code
12656   true,                 // has_make_symbol
12657   false,                // has_resolve
12658   false,                // has_code_fill
12659   true,                 // is_default_stack_executable
12660   false,                // can_icf_inline_merge_sections
12661   '\0',                 // wrap_char
12662   "/lib/ld.so.1",       // dynamic_linker
12663   0x20000,              // default_text_segment_address
12664   0x10000,              // abi_pagesize (overridable by -z max-page-size)
12665   0x10000,              // common_pagesize (overridable by -z common-page-size)
12666   true,                 // isolate_execinstr
12667   0x10000000,           // rosegment_gap
12668   elfcpp::SHN_UNDEF,    // small_common_shndx
12669   elfcpp::SHN_UNDEF,    // large_common_shndx
12670   0,                    // small_common_section_flags
12671   0,                    // large_common_section_flags
12672   NULL,                 // attributes_section
12673   NULL,                 // attributes_vendor
12674   "_start",             // entry_symbol_name
12675   32,                   // hash_entry_size
12676   elfcpp::SHT_PROGBITS, // unwind_section_type
12677 };
12678
12679 // Target selector for Mips.  Note this is never instantiated directly.
12680 // It's only used in Target_selector_mips_nacl, below.
12681
12682 template<int size, bool big_endian>
12683 class Target_selector_mips : public Target_selector
12684 {
12685 public:
12686   Target_selector_mips()
12687     : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12688                 (size == 64 ?
12689                   (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12690                   (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12691                 (size == 64 ?
12692                   (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12693                   (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12694   { }
12695
12696   Target* do_instantiate_target()
12697   { return new Target_mips<size, big_endian>(); }
12698 };
12699
12700 template<int size, bool big_endian>
12701 class Target_selector_mips_nacl
12702   : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12703                                 Target_mips_nacl<size, big_endian> >
12704 {
12705  public:
12706   Target_selector_mips_nacl()
12707     : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12708                            Target_mips_nacl<size, big_endian> >(
12709         // NaCl currently supports only MIPS32 little-endian.
12710         "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12711   { }
12712 };
12713
12714 Target_selector_mips_nacl<32, true> target_selector_mips32;
12715 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12716 Target_selector_mips_nacl<64, true> target_selector_mips64;
12717 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12718
12719 } // End anonymous namespace.