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