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