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