Update year range in copyright notice of binutils files
[external/binutils.git] / gold / mips.cc
1 // mips.cc -- mips target support for gold.
2
3 // Copyright (C) 2011-2018 Free Software Foundation, Inc.
4 // Written by Sasa Stankovic <sasa.stankovic@imgtec.com>
5 //        and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
6 // This file contains borrowed and adapted code from bfd/elfxx-mips.c.
7
8 // This file is part of gold.
9
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
24
25 #include "gold.h"
26
27 #include <algorithm>
28 #include <set>
29 #include <sstream>
30 #include "demangle.h"
31
32 #include "elfcpp.h"
33 #include "parameters.h"
34 #include "reloc.h"
35 #include "mips.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "layout.h"
39 #include "output.h"
40 #include "copy-relocs.h"
41 #include "target.h"
42 #include "target-reloc.h"
43 #include "target-select.h"
44 #include "tls.h"
45 #include "errors.h"
46 #include "gc.h"
47 #include "attributes.h"
48 #include "nacl.h"
49
50 namespace
51 {
52 using namespace gold;
53
54 template<int size, bool big_endian>
55 class Mips_output_data_plt;
56
57 template<int size, bool big_endian>
58 class Mips_output_data_got;
59
60 template<int size, bool big_endian>
61 class Target_mips;
62
63 template<int size, bool big_endian>
64 class Mips_output_section_reginfo;
65
66 template<int size, bool big_endian>
67 class Mips_output_section_options;
68
69 template<int size, bool big_endian>
70 class Mips_output_data_la25_stub;
71
72 template<int size, bool big_endian>
73 class Mips_output_data_mips_stubs;
74
75 template<int size>
76 class Mips_symbol;
77
78 template<int size, bool big_endian>
79 class Mips_got_info;
80
81 template<int size, bool big_endian>
82 class Mips_relobj;
83
84 class Mips16_stub_section_base;
85
86 template<int size, bool big_endian>
87 class Mips16_stub_section;
88
89 // The ABI says that every symbol used by dynamic relocations must have
90 // a global GOT entry.  Among other things, this provides the dynamic
91 // linker with a free, directly-indexed cache.  The GOT can therefore
92 // contain symbols that are not referenced by GOT relocations themselves
93 // (in other words, it may have symbols that are not referenced by things
94 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
95
96 // GOT relocations are less likely to overflow if we put the associated
97 // GOT entries towards the beginning.  We therefore divide the global
98 // GOT entries into two areas: "normal" and "reloc-only".  Entries in
99 // the first area can be used for both dynamic relocations and GP-relative
100 // accesses, while those in the "reloc-only" area are for dynamic
101 // relocations only.
102
103 // These GGA_* ("Global GOT Area") values are organised so that lower
104 // values are more general than higher values.  Also, non-GGA_NONE
105 // values are ordered by the position of the area in the GOT.
106
107 enum Global_got_area
108 {
109   GGA_NORMAL = 0,
110   GGA_RELOC_ONLY = 1,
111   GGA_NONE = 2
112 };
113
114 // The types of GOT entries needed for this platform.
115 // These values are exposed to the ABI in an incremental link.
116 // Do not renumber existing values without changing the version
117 // number of the .gnu_incremental_inputs section.
118 enum Got_type
119 {
120   GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
121   GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
122   GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
123
124   // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
125   GOT_TYPE_STANDARD_MULTIGOT = 3,
126   GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
127   GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
128 };
129
130 // TLS type of GOT entry.
131 enum Got_tls_type
132 {
133   GOT_TLS_NONE = 0,
134   GOT_TLS_GD = 1,
135   GOT_TLS_LDM = 2,
136   GOT_TLS_IE = 4
137 };
138
139 // Values found in the r_ssym field of a relocation entry.
140 enum Special_relocation_symbol
141 {
142   RSS_UNDEF = 0,    // None - value is zero.
143   RSS_GP = 1,       // Value of GP.
144   RSS_GP0 = 2,      // Value of GP in object being relocated.
145   RSS_LOC = 3       // Address of location being relocated.
146 };
147
148 // Whether the section is readonly.
149 static inline bool
150 is_readonly_section(Output_section* output_section)
151 {
152   elfcpp::Elf_Xword section_flags = output_section->flags();
153   elfcpp::Elf_Word section_type = output_section->type();
154
155   if (section_type == elfcpp::SHT_NOBITS)
156     return false;
157
158   if (section_flags & elfcpp::SHF_WRITE)
159     return false;
160
161   return true;
162 }
163
164 // Return TRUE if a relocation of type R_TYPE from OBJECT might
165 // require an la25 stub.  See also local_pic_function, which determines
166 // whether the destination function ever requires a stub.
167 template<int size, bool big_endian>
168 static inline bool
169 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
170                            unsigned int r_type, bool target_is_16_bit_code)
171 {
172   // We specifically ignore branches and jumps from EF_PIC objects,
173   // where the onus is on the compiler or programmer to perform any
174   // necessary initialization of $25.  Sometimes such initialization
175   // is unnecessary; for example, -mno-shared functions do not use
176   // the incoming value of $25, and may therefore be called directly.
177   if (object->is_pic())
178     return false;
179
180   switch (r_type)
181     {
182     case elfcpp::R_MIPS_26:
183     case elfcpp::R_MIPS_PC16:
184     case elfcpp::R_MIPS_PC21_S2:
185     case elfcpp::R_MIPS_PC26_S2:
186     case elfcpp::R_MICROMIPS_26_S1:
187     case elfcpp::R_MICROMIPS_PC7_S1:
188     case elfcpp::R_MICROMIPS_PC10_S1:
189     case elfcpp::R_MICROMIPS_PC16_S1:
190     case elfcpp::R_MICROMIPS_PC23_S2:
191       return true;
192
193     case elfcpp::R_MIPS16_26:
194       return !target_is_16_bit_code;
195
196     default:
197       return false;
198     }
199 }
200
201 // Return true if SYM is a locally-defined PIC function, in the sense
202 // that it or its fn_stub might need $25 to be valid on entry.
203 // Note that MIPS16 functions set up $gp using PC-relative instructions,
204 // so they themselves never need $25 to be valid.  Only non-MIPS16
205 // entry points are of interest here.
206 template<int size, bool big_endian>
207 static inline bool
208 local_pic_function(Mips_symbol<size>* sym)
209 {
210   bool def_regular = (sym->source() == Symbol::FROM_OBJECT
211                       && !sym->object()->is_dynamic()
212                       && !sym->is_undefined());
213
214   if (sym->is_defined() && def_regular)
215     {
216       Mips_relobj<size, big_endian>* object =
217         static_cast<Mips_relobj<size, big_endian>*>(sym->object());
218
219       if ((object->is_pic() || sym->is_pic())
220           && (!sym->is_mips16()
221               || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
222         return true;
223     }
224   return false;
225 }
226
227 static inline bool
228 hi16_reloc(int r_type)
229 {
230   return (r_type == elfcpp::R_MIPS_HI16
231           || r_type == elfcpp::R_MIPS16_HI16
232           || r_type == elfcpp::R_MICROMIPS_HI16
233           || r_type == elfcpp::R_MIPS_PCHI16);
234 }
235
236 static inline bool
237 lo16_reloc(int r_type)
238 {
239   return (r_type == elfcpp::R_MIPS_LO16
240           || r_type == elfcpp::R_MIPS16_LO16
241           || r_type == elfcpp::R_MICROMIPS_LO16
242           || r_type == elfcpp::R_MIPS_PCLO16);
243 }
244
245 static inline bool
246 got16_reloc(unsigned int r_type)
247 {
248   return (r_type == elfcpp::R_MIPS_GOT16
249           || r_type == elfcpp::R_MIPS16_GOT16
250           || r_type == elfcpp::R_MICROMIPS_GOT16);
251 }
252
253 static inline bool
254 call_lo16_reloc(unsigned int r_type)
255 {
256   return (r_type == elfcpp::R_MIPS_CALL_LO16
257           || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
258 }
259
260 static inline bool
261 got_lo16_reloc(unsigned int r_type)
262 {
263   return (r_type == elfcpp::R_MIPS_GOT_LO16
264           || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
265 }
266
267 static inline bool
268 eh_reloc(unsigned int r_type)
269 {
270   return (r_type == elfcpp::R_MIPS_EH);
271 }
272
273 static inline bool
274 got_disp_reloc(unsigned int r_type)
275 {
276   return (r_type == elfcpp::R_MIPS_GOT_DISP
277           || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
278 }
279
280 static inline bool
281 got_page_reloc(unsigned int r_type)
282 {
283   return (r_type == elfcpp::R_MIPS_GOT_PAGE
284           || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
285 }
286
287 static inline bool
288 tls_gd_reloc(unsigned int r_type)
289 {
290   return (r_type == elfcpp::R_MIPS_TLS_GD
291           || r_type == elfcpp::R_MIPS16_TLS_GD
292           || r_type == elfcpp::R_MICROMIPS_TLS_GD);
293 }
294
295 static inline bool
296 tls_gottprel_reloc(unsigned int r_type)
297 {
298   return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
299           || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
300           || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
301 }
302
303 static inline bool
304 tls_ldm_reloc(unsigned int r_type)
305 {
306   return (r_type == elfcpp::R_MIPS_TLS_LDM
307           || r_type == elfcpp::R_MIPS16_TLS_LDM
308           || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
309 }
310
311 static inline bool
312 mips16_call_reloc(unsigned int r_type)
313 {
314   return (r_type == elfcpp::R_MIPS16_26
315           || r_type == elfcpp::R_MIPS16_CALL16);
316 }
317
318 static inline bool
319 jal_reloc(unsigned int r_type)
320 {
321   return (r_type == elfcpp::R_MIPS_26
322           || r_type == elfcpp::R_MIPS16_26
323           || r_type == elfcpp::R_MICROMIPS_26_S1);
324 }
325
326 static inline bool
327 micromips_branch_reloc(unsigned int r_type)
328 {
329   return (r_type == elfcpp::R_MICROMIPS_26_S1
330           || r_type == elfcpp::R_MICROMIPS_PC16_S1
331           || r_type == elfcpp::R_MICROMIPS_PC10_S1
332           || r_type == elfcpp::R_MICROMIPS_PC7_S1);
333 }
334
335 // Check if R_TYPE is a MIPS16 reloc.
336 static inline bool
337 mips16_reloc(unsigned int r_type)
338 {
339   switch (r_type)
340     {
341     case elfcpp::R_MIPS16_26:
342     case elfcpp::R_MIPS16_GPREL:
343     case elfcpp::R_MIPS16_GOT16:
344     case elfcpp::R_MIPS16_CALL16:
345     case elfcpp::R_MIPS16_HI16:
346     case elfcpp::R_MIPS16_LO16:
347     case elfcpp::R_MIPS16_TLS_GD:
348     case elfcpp::R_MIPS16_TLS_LDM:
349     case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
350     case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
351     case elfcpp::R_MIPS16_TLS_GOTTPREL:
352     case elfcpp::R_MIPS16_TLS_TPREL_HI16:
353     case elfcpp::R_MIPS16_TLS_TPREL_LO16:
354       return true;
355
356     default:
357       return false;
358     }
359 }
360
361 // Check if R_TYPE is a microMIPS reloc.
362 static inline bool
363 micromips_reloc(unsigned int r_type)
364 {
365   switch (r_type)
366     {
367     case elfcpp::R_MICROMIPS_26_S1:
368     case elfcpp::R_MICROMIPS_HI16:
369     case elfcpp::R_MICROMIPS_LO16:
370     case elfcpp::R_MICROMIPS_GPREL16:
371     case elfcpp::R_MICROMIPS_LITERAL:
372     case elfcpp::R_MICROMIPS_GOT16:
373     case elfcpp::R_MICROMIPS_PC7_S1:
374     case elfcpp::R_MICROMIPS_PC10_S1:
375     case elfcpp::R_MICROMIPS_PC16_S1:
376     case elfcpp::R_MICROMIPS_CALL16:
377     case elfcpp::R_MICROMIPS_GOT_DISP:
378     case elfcpp::R_MICROMIPS_GOT_PAGE:
379     case elfcpp::R_MICROMIPS_GOT_OFST:
380     case elfcpp::R_MICROMIPS_GOT_HI16:
381     case elfcpp::R_MICROMIPS_GOT_LO16:
382     case elfcpp::R_MICROMIPS_SUB:
383     case elfcpp::R_MICROMIPS_HIGHER:
384     case elfcpp::R_MICROMIPS_HIGHEST:
385     case elfcpp::R_MICROMIPS_CALL_HI16:
386     case elfcpp::R_MICROMIPS_CALL_LO16:
387     case elfcpp::R_MICROMIPS_SCN_DISP:
388     case elfcpp::R_MICROMIPS_JALR:
389     case elfcpp::R_MICROMIPS_HI0_LO16:
390     case elfcpp::R_MICROMIPS_TLS_GD:
391     case elfcpp::R_MICROMIPS_TLS_LDM:
392     case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
393     case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
394     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
395     case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
396     case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
397     case elfcpp::R_MICROMIPS_GPREL7_S2:
398     case elfcpp::R_MICROMIPS_PC23_S2:
399       return true;
400
401     default:
402       return false;
403     }
404 }
405
406 static inline bool
407 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
408 {
409   switch (high_reloc)
410     {
411     case elfcpp::R_MIPS_HI16:
412     case elfcpp::R_MIPS_GOT16:
413       return lo16_reloc == elfcpp::R_MIPS_LO16;
414     case elfcpp::R_MIPS_PCHI16:
415       return lo16_reloc == elfcpp::R_MIPS_PCLO16;
416     case elfcpp::R_MIPS16_HI16:
417     case elfcpp::R_MIPS16_GOT16:
418       return lo16_reloc == elfcpp::R_MIPS16_LO16;
419     case elfcpp::R_MICROMIPS_HI16:
420     case elfcpp::R_MICROMIPS_GOT16:
421       return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
422     default:
423       return false;
424     }
425 }
426
427 // This class is used to hold information about one GOT entry.
428 // There are three types of entry:
429 //
430 //    (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
431 //          (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
432 //    (2) a SYMBOL address, where SYMBOL is not local to an input object
433 //          (sym != NULL, symndx == -1)
434 //    (3) a TLS LDM slot (there's only one of these per GOT.)
435 //          (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
436
437 template<int size, bool big_endian>
438 class Mips_got_entry
439 {
440   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
441
442  public:
443   Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
444                  Mips_address addend, unsigned char tls_type,
445                  unsigned int shndx, bool is_section_symbol)
446     : addend_(addend), symndx_(symndx), tls_type_(tls_type),
447       is_section_symbol_(is_section_symbol), shndx_(shndx)
448   { this->d.object = object; }
449
450   Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
451     : addend_(0), symndx_(-1U), tls_type_(tls_type),
452       is_section_symbol_(false), shndx_(-1U)
453   { this->d.sym = sym; }
454
455   // Return whether this entry is for a local symbol.
456   bool
457   is_for_local_symbol() const
458   { return this->symndx_ != -1U; }
459
460   // Return whether this entry is for a global symbol.
461   bool
462   is_for_global_symbol() const
463   { return this->symndx_ == -1U; }
464
465   // Return the hash of this entry.
466   size_t
467   hash() const
468   {
469     if (this->tls_type_ == GOT_TLS_LDM)
470       return this->symndx_ + (1 << 18);
471
472     size_t name_hash_value = gold::string_hash<char>(
473         (this->symndx_ != -1U)
474          ? this->d.object->name().c_str()
475          : this->d.sym->name());
476     size_t addend = this->addend_;
477     return name_hash_value ^ this->symndx_ ^ (addend << 16);
478   }
479
480   // Return whether this entry is equal to OTHER.
481   bool
482   equals(Mips_got_entry<size, big_endian>* other) const
483   {
484     if (this->symndx_ != other->symndx_
485         || this->tls_type_ != other->tls_type_)
486       return false;
487
488     if (this->tls_type_ == GOT_TLS_LDM)
489       return true;
490
491     return (((this->symndx_ != -1U)
492               ? (this->d.object == other->d.object)
493               : (this->d.sym == other->d.sym))
494             && (this->addend_ == other->addend_));
495   }
496
497   // Return input object that needs this GOT entry.
498   Mips_relobj<size, big_endian>*
499   object() const
500   {
501     gold_assert(this->symndx_ != -1U);
502     return this->d.object;
503   }
504
505   // Return local symbol index for local GOT entries.
506   unsigned int
507   symndx() const
508   {
509     gold_assert(this->symndx_ != -1U);
510     return this->symndx_;
511   }
512
513   // Return the relocation addend for local GOT entries.
514   Mips_address
515   addend() const
516   { return this->addend_; }
517
518   // Return global symbol for global GOT entries.
519   Mips_symbol<size>*
520   sym() const
521   {
522     gold_assert(this->symndx_ == -1U);
523     return this->d.sym;
524   }
525
526   // Return whether this is a TLS GOT entry.
527   bool
528   is_tls_entry() const
529   { return this->tls_type_ != GOT_TLS_NONE; }
530
531   // Return TLS type of this GOT entry.
532   unsigned char
533   tls_type() const
534   { return this->tls_type_; }
535
536   // Return section index of the local symbol for local GOT entries.
537   unsigned int
538   shndx() const
539   { return this->shndx_; }
540
541   // Return whether this is a STT_SECTION symbol.
542   bool
543   is_section_symbol() const
544   { return this->is_section_symbol_; }
545
546  private:
547   // The addend.
548   Mips_address addend_;
549
550   // The index of the symbol if we have a local symbol; -1 otherwise.
551   unsigned int symndx_;
552
553   union
554   {
555     // The input object for local symbols that needs the GOT entry.
556     Mips_relobj<size, big_endian>* object;
557     // If symndx == -1, the global symbol corresponding to this GOT entry.  The
558     // symbol's entry is in the local area if mips_sym->global_got_area is
559     // GGA_NONE, otherwise it is in the global area.
560     Mips_symbol<size>* sym;
561   } d;
562
563   // The TLS type of this GOT entry.  An LDM GOT entry will be a local
564   // symbol entry with r_symndx == 0.
565   unsigned char tls_type_;
566
567   // Whether this is a STT_SECTION symbol.
568   bool is_section_symbol_;
569
570   // For local GOT entries, section index of the local symbol.
571   unsigned int shndx_;
572 };
573
574 // Hash for Mips_got_entry.
575
576 template<int size, bool big_endian>
577 class Mips_got_entry_hash
578 {
579  public:
580   size_t
581   operator()(Mips_got_entry<size, big_endian>* entry) const
582   { return entry->hash(); }
583 };
584
585 // Equality for Mips_got_entry.
586
587 template<int size, bool big_endian>
588 class Mips_got_entry_eq
589 {
590  public:
591   bool
592   operator()(Mips_got_entry<size, big_endian>* e1,
593              Mips_got_entry<size, big_endian>* e2) const
594   { return e1->equals(e2); }
595 };
596
597 // Hash for Mips_symbol.
598
599 template<int size>
600 class Mips_symbol_hash
601 {
602  public:
603   size_t
604   operator()(Mips_symbol<size>* sym) const
605   { return sym->hash(); }
606 };
607
608 // Got_page_range.  This class describes a range of addends: [MIN_ADDEND,
609 // MAX_ADDEND].  The instances form a non-overlapping list that is sorted by
610 // increasing MIN_ADDEND.
611
612 struct Got_page_range
613 {
614   Got_page_range()
615     : next(NULL), min_addend(0), max_addend(0)
616   { }
617
618   Got_page_range* next;
619   int min_addend;
620   int max_addend;
621
622   // Return the maximum number of GOT page entries required.
623   int
624   get_max_pages()
625   { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
626 };
627
628 // Got_page_entry.  This class describes the range of addends that are applied
629 // to page relocations against a given symbol.
630
631 struct Got_page_entry
632 {
633   Got_page_entry()
634     : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
635   { }
636
637   Got_page_entry(Object* object_, unsigned int symndx_)
638     : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
639   { }
640
641   // The input object that needs the GOT page entry.
642   Object* object;
643   // The index of the symbol, as stored in the relocation r_info.
644   unsigned int symndx;
645   // The ranges for this page entry.
646   Got_page_range* ranges;
647   // The maximum number of page entries needed for RANGES.
648   unsigned int num_pages;
649 };
650
651 // Hash for Got_page_entry.
652
653 struct Got_page_entry_hash
654 {
655   size_t
656   operator()(Got_page_entry* entry) const
657   { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
658 };
659
660 // Equality for Got_page_entry.
661
662 struct Got_page_entry_eq
663 {
664   bool
665   operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
666   {
667     return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
668   }
669 };
670
671 // This class is used to hold .got information when linking.
672
673 template<int size, bool big_endian>
674 class Mips_got_info
675 {
676   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
677   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
678     Reloc_section;
679   typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
680
681   // Unordered set of GOT entries.
682   typedef Unordered_set<Mips_got_entry<size, big_endian>*,
683       Mips_got_entry_hash<size, big_endian>,
684       Mips_got_entry_eq<size, big_endian> > Got_entry_set;
685
686   // Unordered set of GOT page entries.
687   typedef Unordered_set<Got_page_entry*,
688       Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
689
690   // Unordered set of global GOT entries.
691   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
692       Global_got_entry_set;
693
694  public:
695   Mips_got_info()
696     : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
697       tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
698       got_entries_(), got_page_entries_(), got_page_offset_start_(0),
699       got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
700       offset_(0)
701   { }
702
703   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
704   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
705   void
706   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
707                           unsigned int symndx, Mips_address addend,
708                           unsigned int r_type, unsigned int shndx,
709                           bool is_section_symbol);
710
711   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
712   // in OBJECT.  FOR_CALL is true if the caller is only interested in
713   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
714   // relocation.
715   void
716   record_global_got_symbol(Mips_symbol<size>* mips_sym,
717                            Mips_relobj<size, big_endian>* object,
718                            unsigned int r_type, bool dyn_reloc, bool for_call);
719
720   // Add ENTRY to master GOT and to OBJECT's GOT.
721   void
722   record_got_entry(Mips_got_entry<size, big_endian>* entry,
723                    Mips_relobj<size, big_endian>* object);
724
725   // Record that OBJECT has a page relocation against symbol SYMNDX and
726   // that ADDEND is the addend for that relocation.
727   void
728   record_got_page_entry(Mips_relobj<size, big_endian>* object,
729                         unsigned int symndx, int addend);
730
731   // Create all entries that should be in the local part of the GOT.
732   void
733   add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
734
735   // Create GOT page entries.
736   void
737   add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
738
739   // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
740   void
741   add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
742                      unsigned int non_reloc_only_global_gotno);
743
744   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
745   void
746   add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
747
748   // Create TLS GOT entries.
749   void
750   add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
751
752   // Decide whether the symbol needs an entry in the global part of the primary
753   // GOT, setting global_got_area accordingly.  Count the number of global
754   // symbols that are in the primary GOT only because they have dynamic
755   // relocations R_MIPS_REL32 against them (reloc_only_gotno).
756   void
757   count_got_symbols(Symbol_table* symtab);
758
759   // Return the offset of GOT page entry for VALUE.
760   unsigned int
761   get_got_page_offset(Mips_address value,
762                       Mips_output_data_got<size, big_endian>* got);
763
764   // Count the number of GOT entries required.
765   void
766   count_got_entries();
767
768   // Count the number of GOT entries required by ENTRY.  Accumulate the result.
769   void
770   count_got_entry(Mips_got_entry<size, big_endian>* entry);
771
772   // Add FROM's GOT entries.
773   void
774   add_got_entries(Mips_got_info<size, big_endian>* from);
775
776   // Add FROM's GOT page entries.
777   void
778   add_got_page_entries(Mips_got_info<size, big_endian>* from);
779
780   // Return GOT size.
781   unsigned int
782   got_size() const
783   { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
784              + this->tls_gotno_) * size/8);
785   }
786
787   // Return the number of local GOT entries.
788   unsigned int
789   local_gotno() const
790   { return this->local_gotno_; }
791
792   // Return the maximum number of page GOT entries needed.
793   unsigned int
794   page_gotno() const
795   { return this->page_gotno_; }
796
797   // Return the number of global GOT entries.
798   unsigned int
799   global_gotno() const
800   { return this->global_gotno_; }
801
802   // Set the number of global GOT entries.
803   void
804   set_global_gotno(unsigned int global_gotno)
805   { this->global_gotno_ = global_gotno; }
806
807   // Return the number of GGA_RELOC_ONLY global GOT entries.
808   unsigned int
809   reloc_only_gotno() const
810   { return this->reloc_only_gotno_; }
811
812   // Return the number of TLS GOT entries.
813   unsigned int
814   tls_gotno() const
815   { return this->tls_gotno_; }
816
817   // Return the GOT type for this GOT.  Used for multi-GOT links only.
818   unsigned int
819   multigot_got_type(unsigned int got_type) const
820   {
821     switch (got_type)
822       {
823       case GOT_TYPE_STANDARD:
824         return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
825       case GOT_TYPE_TLS_OFFSET:
826         return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
827       case GOT_TYPE_TLS_PAIR:
828         return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
829       default:
830         gold_unreachable();
831       }
832   }
833
834   // Remove lazy-binding stubs for global symbols in this GOT.
835   void
836   remove_lazy_stubs(Target_mips<size, big_endian>* target);
837
838   // Return offset of this GOT from the start of .got section.
839   unsigned int
840   offset() const
841   { return this->offset_; }
842
843   // Set offset of this GOT from the start of .got section.
844   void
845   set_offset(unsigned int offset)
846   { this->offset_ = offset; }
847
848   // Set index of this GOT in multi-GOT links.
849   void
850   set_index(unsigned int index)
851   { this->index_ = index; }
852
853   // Return next GOT in multi-GOT links.
854   Mips_got_info<size, big_endian>*
855   next() const
856   { return this->next_; }
857
858   // Set next GOT in multi-GOT links.
859   void
860   set_next(Mips_got_info<size, big_endian>* next)
861   { this->next_ = next; }
862
863   // Return the offset of TLS LDM entry for this GOT.
864   unsigned int
865   tls_ldm_offset() const
866   { return this->tls_ldm_offset_; }
867
868   // Set the offset of TLS LDM entry for this GOT.
869   void
870   set_tls_ldm_offset(unsigned int tls_ldm_offset)
871   { this->tls_ldm_offset_ = tls_ldm_offset; }
872
873   Global_got_entry_set&
874   global_got_symbols()
875   { return this->global_got_symbols_; }
876
877   // Return the GOT_TLS_* type required by relocation type R_TYPE.
878   static int
879   mips_elf_reloc_tls_type(unsigned int r_type)
880   {
881     if (tls_gd_reloc(r_type))
882       return GOT_TLS_GD;
883
884     if (tls_ldm_reloc(r_type))
885       return GOT_TLS_LDM;
886
887     if (tls_gottprel_reloc(r_type))
888       return GOT_TLS_IE;
889
890     return GOT_TLS_NONE;
891   }
892
893   // Return the number of GOT slots needed for GOT TLS type TYPE.
894   static int
895   mips_tls_got_entries(unsigned int type)
896   {
897     switch (type)
898       {
899       case GOT_TLS_GD:
900       case GOT_TLS_LDM:
901         return 2;
902
903       case GOT_TLS_IE:
904         return 1;
905
906       case GOT_TLS_NONE:
907         return 0;
908
909       default:
910         gold_unreachable();
911       }
912   }
913
914  private:
915   // The number of local GOT entries.
916   unsigned int local_gotno_;
917   // The maximum number of page GOT entries needed.
918   unsigned int page_gotno_;
919   // The number of global GOT entries.
920   unsigned int global_gotno_;
921   // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
922   unsigned int reloc_only_gotno_;
923   // The number of TLS GOT entries.
924   unsigned int tls_gotno_;
925   // The offset of TLS LDM entry for this GOT.
926   unsigned int tls_ldm_offset_;
927   // All symbols that have global GOT entry.
928   Global_got_entry_set global_got_symbols_;
929   // A hash table holding GOT entries.
930   Got_entry_set got_entries_;
931   // A hash table of GOT page entries.
932   Got_page_entry_set got_page_entries_;
933   // The offset of first GOT page entry for this GOT.
934   unsigned int got_page_offset_start_;
935   // The offset of next available GOT page entry for this GOT.
936   unsigned int got_page_offset_next_;
937   // A hash table that maps GOT page entry value to the GOT offset where
938   // the entry is located.
939   Got_page_offsets got_page_offsets_;
940   // In multi-GOT links, a pointer to the next GOT.
941   Mips_got_info<size, big_endian>* next_;
942   // Index of this GOT in multi-GOT links.
943   unsigned int index_;
944   // The offset of this GOT in multi-GOT links.
945   unsigned int offset_;
946 };
947
948 // This is a helper class used during relocation scan.  It records GOT16 addend.
949
950 template<int size, bool big_endian>
951 struct got16_addend
952 {
953   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
954
955   got16_addend(const Sized_relobj_file<size, big_endian>* _object,
956                unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
957                Mips_address _addend)
958     : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
959       addend(_addend)
960   { }
961
962   const Sized_relobj_file<size, big_endian>* object;
963   unsigned int shndx;
964   unsigned int r_type;
965   unsigned int r_sym;
966   Mips_address addend;
967 };
968
969 // .MIPS.abiflags section content
970
971 template<bool big_endian>
972 struct Mips_abiflags
973 {
974   typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
975   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
976   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
977
978   Mips_abiflags()
979     : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
980       cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
981   { }
982
983   // Version of flags structure.
984   Valtype16 version;
985   // The level of the ISA: 1-5, 32, 64.
986   Valtype8 isa_level;
987   // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
988   Valtype8 isa_rev;
989   // The size of general purpose registers.
990   Valtype8 gpr_size;
991   // The size of co-processor 1 registers.
992   Valtype8 cpr1_size;
993   // The size of co-processor 2 registers.
994   Valtype8 cpr2_size;
995   // The floating-point ABI.
996   Valtype8 fp_abi;
997   // Processor-specific extension.
998   Valtype32 isa_ext;
999   // Mask of ASEs used.
1000   Valtype32 ases;
1001   // Mask of general flags.
1002   Valtype32 flags1;
1003   Valtype32 flags2;
1004 };
1005
1006 // Mips_symbol class.  Holds additional symbol information needed for Mips.
1007
1008 template<int size>
1009 class Mips_symbol : public Sized_symbol<size>
1010 {
1011  public:
1012   Mips_symbol()
1013     : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1014       has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1015       pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1016       global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1017       needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1018       comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1019       mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1020   { }
1021
1022   // Return whether this is a MIPS16 symbol.
1023   bool
1024   is_mips16() const
1025   {
1026     // (st_other & STO_MIPS16) == STO_MIPS16
1027     return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1028             == elfcpp::STO_MIPS16 >> 2);
1029   }
1030
1031   // Return whether this is a microMIPS symbol.
1032   bool
1033   is_micromips() const
1034   {
1035     // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1036     return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1037             == elfcpp::STO_MICROMIPS >> 2);
1038   }
1039
1040   // Return whether the symbol needs MIPS16 fn_stub.
1041   bool
1042   need_fn_stub() const
1043   { return this->need_fn_stub_; }
1044
1045   // Set that the symbol needs MIPS16 fn_stub.
1046   void
1047   set_need_fn_stub()
1048   { this->need_fn_stub_ = true; }
1049
1050   // Return whether this symbol is referenced by branch relocations from
1051   // any non-PIC input file.
1052   bool
1053   has_nonpic_branches() const
1054   { return this->has_nonpic_branches_; }
1055
1056   // Set that this symbol is referenced by branch relocations from
1057   // any non-PIC input file.
1058   void
1059   set_has_nonpic_branches()
1060   { this->has_nonpic_branches_ = true; }
1061
1062   // Return the offset of the la25 stub for this symbol from the start of the
1063   // la25 stub section.
1064   unsigned int
1065   la25_stub_offset() const
1066   { return this->la25_stub_offset_; }
1067
1068   // Set the offset of the la25 stub for this symbol from the start of the
1069   // la25 stub section.
1070   void
1071   set_la25_stub_offset(unsigned int offset)
1072   { this->la25_stub_offset_ = offset; }
1073
1074   // Return whether the symbol has la25 stub.  This is true if this symbol is
1075   // for a PIC function, and there are non-PIC branches and jumps to it.
1076   bool
1077   has_la25_stub() const
1078   { return this->la25_stub_offset_ != -1U; }
1079
1080   // Return whether there is a relocation against this symbol that must be
1081   // resolved by the static linker (that is, the relocation cannot possibly
1082   // be made dynamic).
1083   bool
1084   has_static_relocs() const
1085   { return this->has_static_relocs_; }
1086
1087   // Set that there is a relocation against this symbol that must be resolved
1088   // by the static linker (that is, the relocation cannot possibly be made
1089   // dynamic).
1090   void
1091   set_has_static_relocs()
1092   { this->has_static_relocs_ = true; }
1093
1094   // Return whether we must not create a lazy-binding stub for this symbol.
1095   bool
1096   no_lazy_stub() const
1097   { return this->no_lazy_stub_; }
1098
1099   // Set that we must not create a lazy-binding stub for this symbol.
1100   void
1101   set_no_lazy_stub()
1102   { this->no_lazy_stub_ = true; }
1103
1104   // Return the offset of the lazy-binding stub for this symbol from the start
1105   // of .MIPS.stubs section.
1106   unsigned int
1107   lazy_stub_offset() const
1108   { return this->lazy_stub_offset_; }
1109
1110   // Set the offset of the lazy-binding stub for this symbol from the start
1111   // of .MIPS.stubs section.
1112   void
1113   set_lazy_stub_offset(unsigned int offset)
1114   { this->lazy_stub_offset_ = offset; }
1115
1116   // Return whether there are any relocations for this symbol where
1117   // pointer equality matters.
1118   bool
1119   pointer_equality_needed() const
1120   { return this->pointer_equality_needed_; }
1121
1122   // Set that there are relocations for this symbol where pointer equality
1123   // matters.
1124   void
1125   set_pointer_equality_needed()
1126   { this->pointer_equality_needed_ = true; }
1127
1128   // Return global GOT area where this symbol in located.
1129   Global_got_area
1130   global_got_area() const
1131   { return this->global_got_area_; }
1132
1133   // Set global GOT area where this symbol in located.
1134   void
1135   set_global_got_area(Global_got_area global_got_area)
1136   { this->global_got_area_ = global_got_area; }
1137
1138   // Return the global GOT offset for this symbol.  For multi-GOT links, this
1139   // returns the offset from the start of .got section to the first GOT entry
1140   // for the symbol.  Note that in multi-GOT links the symbol can have entry
1141   // in more than one GOT.
1142   unsigned int
1143   global_gotoffset() const
1144   { return this->global_gotoffset_; }
1145
1146   // Set the global GOT offset for this symbol.  Note that in multi-GOT links
1147   // the symbol can have entry in more than one GOT.  This method will set
1148   // the offset only if it is less than current offset.
1149   void
1150   set_global_gotoffset(unsigned int offset)
1151   {
1152     if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1153       this->global_gotoffset_ = offset;
1154   }
1155
1156   // Return whether all GOT relocations for this symbol are for calls.
1157   bool
1158   got_only_for_calls() const
1159   { return this->got_only_for_calls_; }
1160
1161   // Set that there is a GOT relocation for this symbol that is not for call.
1162   void
1163   set_got_not_only_for_calls()
1164   { this->got_only_for_calls_ = false; }
1165
1166   // Return whether this is a PIC symbol.
1167   bool
1168   is_pic() const
1169   {
1170     // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1171     return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1172             == (elfcpp::STO_MIPS_PIC >> 2));
1173   }
1174
1175   // Set the flag in st_other field that marks this symbol as PIC.
1176   void
1177   set_pic()
1178   {
1179     if (this->is_mips16())
1180       // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1181       this->set_nonvis((this->nonvis()
1182                         & ~((elfcpp::STO_MIPS16 >> 2)
1183                             | (elfcpp::STO_MIPS_FLAGS >> 2)))
1184                        | (elfcpp::STO_MIPS_PIC >> 2));
1185     else
1186       // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1187       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1188                        | (elfcpp::STO_MIPS_PIC >> 2));
1189   }
1190
1191   // Set the flag in st_other field that marks this symbol as PLT.
1192   void
1193   set_mips_plt()
1194   {
1195     if (this->is_mips16())
1196       // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1197       this->set_nonvis((this->nonvis()
1198                         & ((elfcpp::STO_MIPS16 >> 2)
1199                            | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1200                        | (elfcpp::STO_MIPS_PLT >> 2));
1201
1202     else
1203       // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1204       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1205                        | (elfcpp::STO_MIPS_PLT >> 2));
1206   }
1207
1208   // Downcast a base pointer to a Mips_symbol pointer.
1209   static Mips_symbol<size>*
1210   as_mips_sym(Symbol* sym)
1211   { return static_cast<Mips_symbol<size>*>(sym); }
1212
1213   // Downcast a base pointer to a Mips_symbol pointer.
1214   static const Mips_symbol<size>*
1215   as_mips_sym(const Symbol* sym)
1216   { return static_cast<const Mips_symbol<size>*>(sym); }
1217
1218   // Return whether the symbol has lazy-binding stub.
1219   bool
1220   has_lazy_stub() const
1221   { return this->has_lazy_stub_; }
1222
1223   // Set whether the symbol has lazy-binding stub.
1224   void
1225   set_has_lazy_stub(bool has_lazy_stub)
1226   { this->has_lazy_stub_ = has_lazy_stub; }
1227
1228   // Return whether the symbol needs a standard PLT entry.
1229   bool
1230   needs_mips_plt() const
1231   { return this->needs_mips_plt_; }
1232
1233   // Set whether the symbol needs a standard PLT entry.
1234   void
1235   set_needs_mips_plt(bool needs_mips_plt)
1236   { this->needs_mips_plt_ = needs_mips_plt; }
1237
1238   // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1239   // entry.
1240   bool
1241   needs_comp_plt() const
1242   { return this->needs_comp_plt_; }
1243
1244   // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1245   void
1246   set_needs_comp_plt(bool needs_comp_plt)
1247   { this->needs_comp_plt_ = needs_comp_plt; }
1248
1249   // Return standard PLT entry offset, or -1 if none.
1250   unsigned int
1251   mips_plt_offset() const
1252   { return this->mips_plt_offset_; }
1253
1254   // Set standard PLT entry offset.
1255   void
1256   set_mips_plt_offset(unsigned int mips_plt_offset)
1257   { this->mips_plt_offset_ = mips_plt_offset; }
1258
1259   // Return whether the symbol has standard PLT entry.
1260   bool
1261   has_mips_plt_offset() const
1262   { return this->mips_plt_offset_ != -1U; }
1263
1264   // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1265   unsigned int
1266   comp_plt_offset() const
1267   { return this->comp_plt_offset_; }
1268
1269   // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1270   void
1271   set_comp_plt_offset(unsigned int comp_plt_offset)
1272   { this->comp_plt_offset_ = comp_plt_offset; }
1273
1274   // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1275   bool
1276   has_comp_plt_offset() const
1277   { return this->comp_plt_offset_ != -1U; }
1278
1279   // Return MIPS16 fn stub for a symbol.
1280   template<bool big_endian>
1281   Mips16_stub_section<size, big_endian>*
1282   get_mips16_fn_stub() const
1283   {
1284     return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1285   }
1286
1287   // Set MIPS16 fn stub for a symbol.
1288   void
1289   set_mips16_fn_stub(Mips16_stub_section_base* stub)
1290   { this->mips16_fn_stub_ = stub; }
1291
1292   // Return whether symbol has MIPS16 fn stub.
1293   bool
1294   has_mips16_fn_stub() const
1295   { return this->mips16_fn_stub_ != NULL; }
1296
1297   // Return MIPS16 call stub for a symbol.
1298   template<bool big_endian>
1299   Mips16_stub_section<size, big_endian>*
1300   get_mips16_call_stub() const
1301   {
1302     return static_cast<Mips16_stub_section<size, big_endian>*>(
1303       mips16_call_stub_);
1304   }
1305
1306   // Set MIPS16 call stub for a symbol.
1307   void
1308   set_mips16_call_stub(Mips16_stub_section_base* stub)
1309   { this->mips16_call_stub_ = stub; }
1310
1311   // Return whether symbol has MIPS16 call stub.
1312   bool
1313   has_mips16_call_stub() const
1314   { return this->mips16_call_stub_ != NULL; }
1315
1316   // Return MIPS16 call_fp stub for a symbol.
1317   template<bool big_endian>
1318   Mips16_stub_section<size, big_endian>*
1319   get_mips16_call_fp_stub() const
1320   {
1321     return static_cast<Mips16_stub_section<size, big_endian>*>(
1322       mips16_call_fp_stub_);
1323   }
1324
1325   // Set MIPS16 call_fp stub for a symbol.
1326   void
1327   set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1328   { this->mips16_call_fp_stub_ = stub; }
1329
1330   // Return whether symbol has MIPS16 call_fp stub.
1331   bool
1332   has_mips16_call_fp_stub() const
1333   { return this->mips16_call_fp_stub_ != NULL; }
1334
1335   bool
1336   get_applied_secondary_got_fixup() const
1337   { return applied_secondary_got_fixup_; }
1338
1339   void
1340   set_applied_secondary_got_fixup()
1341   { this->applied_secondary_got_fixup_ = true; }
1342
1343   // Return the hash of this symbol.
1344   size_t
1345   hash() const
1346   {
1347     return gold::string_hash<char>(this->name());
1348   }
1349
1350  private:
1351   // Whether the symbol needs MIPS16 fn_stub.  This is true if this symbol
1352   // appears in any relocs other than a 16 bit call.
1353   bool need_fn_stub_;
1354
1355   // True if this symbol is referenced by branch relocations from
1356   // any non-PIC input file.  This is used to determine whether an
1357   // la25 stub is required.
1358   bool has_nonpic_branches_;
1359
1360   // The offset of the la25 stub for this symbol from the start of the
1361   // la25 stub section.
1362   unsigned int la25_stub_offset_;
1363
1364   // True if there is a relocation against this symbol that must be
1365   // resolved by the static linker (that is, the relocation cannot
1366   // possibly be made dynamic).
1367   bool has_static_relocs_;
1368
1369   // Whether we must not create a lazy-binding stub for this symbol.
1370   // This is true if the symbol has relocations related to taking the
1371   // function's address.
1372   bool no_lazy_stub_;
1373
1374   // The offset of the lazy-binding stub for this symbol from the start of
1375   // .MIPS.stubs section.
1376   unsigned int lazy_stub_offset_;
1377
1378   // True if there are any relocations for this symbol where pointer equality
1379   // matters.
1380   bool pointer_equality_needed_;
1381
1382   // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1383   // in the global part of the GOT.
1384   Global_got_area global_got_area_;
1385
1386   // The global GOT offset for this symbol.  For multi-GOT links, this is offset
1387   // from the start of .got section to the first GOT entry for the symbol.
1388   // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1389   unsigned int global_gotoffset_;
1390
1391   // Whether all GOT relocations for this symbol are for calls.
1392   bool got_only_for_calls_;
1393   // Whether the symbol has lazy-binding stub.
1394   bool has_lazy_stub_;
1395   // Whether the symbol needs a standard PLT entry.
1396   bool needs_mips_plt_;
1397   // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1398   bool needs_comp_plt_;
1399   // Standard PLT entry offset, or -1 if none.
1400   unsigned int mips_plt_offset_;
1401   // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1402   unsigned int comp_plt_offset_;
1403   // MIPS16 fn stub for a symbol.
1404   Mips16_stub_section_base* mips16_fn_stub_;
1405   // MIPS16 call stub for a symbol.
1406   Mips16_stub_section_base* mips16_call_stub_;
1407   // MIPS16 call_fp stub for a symbol.
1408   Mips16_stub_section_base* mips16_call_fp_stub_;
1409
1410   bool applied_secondary_got_fixup_;
1411 };
1412
1413 // Mips16_stub_section class.
1414
1415 // The mips16 compiler uses a couple of special sections to handle
1416 // floating point arguments.
1417
1418 // Section names that look like .mips16.fn.FNNAME contain stubs that
1419 // copy floating point arguments from the fp regs to the gp regs and
1420 // then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1421 // call should be redirected to the stub instead.  If no 32 bit
1422 // function calls FNNAME, the stub should be discarded.  We need to
1423 // consider any reference to the function, not just a call, because
1424 // if the address of the function is taken we will need the stub,
1425 // since the address might be passed to a 32 bit function.
1426
1427 // Section names that look like .mips16.call.FNNAME contain stubs
1428 // that copy floating point arguments from the gp regs to the fp
1429 // regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1430 // then any 16 bit function that calls FNNAME should be redirected
1431 // to the stub instead.  If FNNAME is not a 32 bit function, the
1432 // stub should be discarded.
1433
1434 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1435 // which call FNNAME and then copy the return value from the fp regs
1436 // to the gp regs.  These stubs store the return address in $18 while
1437 // calling FNNAME; any function which might call one of these stubs
1438 // must arrange to save $18 around the call.  (This case is not
1439 // needed for 32 bit functions that call 16 bit functions, because
1440 // 16 bit functions always return floating point values in both
1441 // $f0/$f1 and $2/$3.)
1442
1443 // Note that in all cases FNNAME might be defined statically.
1444 // Therefore, FNNAME is not used literally.  Instead, the relocation
1445 // information will indicate which symbol the section is for.
1446
1447 // We record any stubs that we find in the symbol table.
1448
1449 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1450
1451 class Mips16_stub_section_base { };
1452
1453 template<int size, bool big_endian>
1454 class Mips16_stub_section : public Mips16_stub_section_base
1455 {
1456   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1457
1458  public:
1459   Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1460     : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1461       found_r_mips_none_(false)
1462   {
1463     gold_assert(object->is_mips16_fn_stub_section(shndx)
1464                 || object->is_mips16_call_stub_section(shndx)
1465                 || object->is_mips16_call_fp_stub_section(shndx));
1466   }
1467
1468   // Return the object of this stub section.
1469   Mips_relobj<size, big_endian>*
1470   object() const
1471   { return this->object_; }
1472
1473   // Return the size of a section.
1474   uint64_t
1475   section_size() const
1476   { return this->object_->section_size(this->shndx_); }
1477
1478   // Return section index of this stub section.
1479   unsigned int
1480   shndx() const
1481   { return this->shndx_; }
1482
1483   // Return symbol index, if stub is for a local function.
1484   unsigned int
1485   r_sym() const
1486   { return this->r_sym_; }
1487
1488   // Return symbol, if stub is for a global function.
1489   Mips_symbol<size>*
1490   gsym() const
1491   { return this->gsym_; }
1492
1493   // Return whether stub is for a local function.
1494   bool
1495   is_for_local_function() const
1496   { return this->gsym_ == NULL; }
1497
1498   // This method is called when a new relocation R_TYPE for local symbol R_SYM
1499   // is found in the stub section.  Try to find stub target.
1500   void
1501   new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1502   {
1503     // To find target symbol for this stub, trust the first R_MIPS_NONE
1504     // relocation, if any.  Otherwise trust the first relocation, whatever
1505     // its kind.
1506     if (this->found_r_mips_none_)
1507       return;
1508     if (r_type == elfcpp::R_MIPS_NONE)
1509       {
1510         this->r_sym_ = r_sym;
1511         this->gsym_ = NULL;
1512         this->found_r_mips_none_ = true;
1513       }
1514     else if (!is_target_found())
1515       this->r_sym_ = r_sym;
1516   }
1517
1518   // This method is called when a new relocation R_TYPE for global symbol GSYM
1519   // is found in the stub section.  Try to find stub target.
1520   void
1521   new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1522   {
1523     // To find target symbol for this stub, trust the first R_MIPS_NONE
1524     // relocation, if any.  Otherwise trust the first relocation, whatever
1525     // its kind.
1526     if (this->found_r_mips_none_)
1527       return;
1528     if (r_type == elfcpp::R_MIPS_NONE)
1529       {
1530         this->gsym_ = gsym;
1531         this->r_sym_ = 0;
1532         this->found_r_mips_none_ = true;
1533       }
1534     else if (!is_target_found())
1535       this->gsym_ = gsym;
1536   }
1537
1538   // Return whether we found the stub target.
1539   bool
1540   is_target_found() const
1541   { return this->r_sym_ != 0 || this->gsym_ != NULL;  }
1542
1543   // Return whether this is a fn stub.
1544   bool
1545   is_fn_stub() const
1546   { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1547
1548   // Return whether this is a call stub.
1549   bool
1550   is_call_stub() const
1551   { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1552
1553   // Return whether this is a call_fp stub.
1554   bool
1555   is_call_fp_stub() const
1556   { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1557
1558   // Return the output address.
1559   Mips_address
1560   output_address() const
1561   {
1562     return (this->object_->output_section(this->shndx_)->address()
1563             + this->object_->output_section_offset(this->shndx_));
1564   }
1565
1566  private:
1567   // The object of this stub section.
1568   Mips_relobj<size, big_endian>* object_;
1569   // The section index of this stub section.
1570   unsigned int shndx_;
1571   // The symbol index, if stub is for a local function.
1572   unsigned int r_sym_;
1573   // The symbol, if stub is for a global function.
1574   Mips_symbol<size>* gsym_;
1575   // True if we found R_MIPS_NONE relocation in this stub.
1576   bool found_r_mips_none_;
1577 };
1578
1579 // Mips_relobj class.
1580
1581 template<int size, bool big_endian>
1582 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1583 {
1584   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1585   typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1586     Mips16_stubs_int_map;
1587   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1588
1589  public:
1590   Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1591               const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1592     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1593       processor_specific_flags_(0), local_symbol_is_mips16_(),
1594       local_symbol_is_micromips_(), mips16_stub_sections_(),
1595       local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1596       local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1597       merge_processor_specific_data_(true), got_info_(NULL),
1598       section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1599       section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
1600       attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
1601       cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1602   {
1603     this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1604     this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1605   }
1606
1607   ~Mips_relobj()
1608   { delete this->attributes_section_data_; }
1609
1610   // Downcast a base pointer to a Mips_relobj pointer.  This is
1611   // not type-safe but we only use Mips_relobj not the base class.
1612   static Mips_relobj<size, big_endian>*
1613   as_mips_relobj(Relobj* relobj)
1614   { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1615
1616   // Downcast a base pointer to a Mips_relobj pointer.  This is
1617   // not type-safe but we only use Mips_relobj not the base class.
1618   static const Mips_relobj<size, big_endian>*
1619   as_mips_relobj(const Relobj* relobj)
1620   { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1621
1622   // Processor-specific flags in ELF file header.  This is valid only after
1623   // reading symbols.
1624   elfcpp::Elf_Word
1625   processor_specific_flags() const
1626   { return this->processor_specific_flags_; }
1627
1628   // Whether a local symbol is MIPS16 symbol.  R_SYM is the symbol table
1629   // index.  This is only valid after do_count_local_symbol is called.
1630   bool
1631   local_symbol_is_mips16(unsigned int r_sym) const
1632   {
1633     gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1634     return this->local_symbol_is_mips16_[r_sym];
1635   }
1636
1637   // Whether a local symbol is microMIPS symbol.  R_SYM is the symbol table
1638   // index.  This is only valid after do_count_local_symbol is called.
1639   bool
1640   local_symbol_is_micromips(unsigned int r_sym) const
1641   {
1642     gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1643     return this->local_symbol_is_micromips_[r_sym];
1644   }
1645
1646   // Get or create MIPS16 stub section.
1647   Mips16_stub_section<size, big_endian>*
1648   get_mips16_stub_section(unsigned int shndx)
1649   {
1650     typename Mips16_stubs_int_map::const_iterator it =
1651       this->mips16_stub_sections_.find(shndx);
1652     if (it != this->mips16_stub_sections_.end())
1653       return (*it).second;
1654
1655     Mips16_stub_section<size, big_endian>* stub_section =
1656       new Mips16_stub_section<size, big_endian>(this, shndx);
1657     this->mips16_stub_sections_.insert(
1658       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1659         stub_section->shndx(), stub_section));
1660     return stub_section;
1661   }
1662
1663   // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1664   // object doesn't have fn stub for R_SYM.
1665   Mips16_stub_section<size, big_endian>*
1666   get_local_mips16_fn_stub(unsigned int r_sym) const
1667   {
1668     typename Mips16_stubs_int_map::const_iterator it =
1669       this->local_mips16_fn_stubs_.find(r_sym);
1670     if (it != this->local_mips16_fn_stubs_.end())
1671       return (*it).second;
1672     return NULL;
1673   }
1674
1675   // Record that this object has MIPS16 fn stub for local symbol.  This method
1676   // is only called if we decided not to discard the stub.
1677   void
1678   add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1679   {
1680     gold_assert(stub->is_for_local_function());
1681     unsigned int r_sym = stub->r_sym();
1682     this->local_mips16_fn_stubs_.insert(
1683       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1684         r_sym, stub));
1685   }
1686
1687   // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1688   // object doesn't have call stub for R_SYM.
1689   Mips16_stub_section<size, big_endian>*
1690   get_local_mips16_call_stub(unsigned int r_sym) const
1691   {
1692     typename Mips16_stubs_int_map::const_iterator it =
1693       this->local_mips16_call_stubs_.find(r_sym);
1694     if (it != this->local_mips16_call_stubs_.end())
1695       return (*it).second;
1696     return NULL;
1697   }
1698
1699   // Record that this object has MIPS16 call stub for local symbol.  This method
1700   // is only called if we decided not to discard the stub.
1701   void
1702   add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1703   {
1704     gold_assert(stub->is_for_local_function());
1705     unsigned int r_sym = stub->r_sym();
1706     this->local_mips16_call_stubs_.insert(
1707       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1708         r_sym, stub));
1709   }
1710
1711   // Record that we found "non 16-bit" call relocation against local symbol
1712   // SYMNDX.  This reloc would need to refer to a MIPS16 fn stub, if there
1713   // is one.
1714   void
1715   add_local_non_16bit_call(unsigned int symndx)
1716   { this->local_non_16bit_calls_.insert(symndx); }
1717
1718   // Return true if there is any "non 16-bit" call relocation against local
1719   // symbol SYMNDX in this object.
1720   bool
1721   has_local_non_16bit_call_relocs(unsigned int symndx)
1722   {
1723     return (this->local_non_16bit_calls_.find(symndx)
1724             != this->local_non_16bit_calls_.end());
1725   }
1726
1727   // Record that we found 16-bit call relocation R_MIPS16_26 against local
1728   // symbol SYMNDX.  Local MIPS16 call or call_fp stubs will only be needed
1729   // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1730   void
1731   add_local_16bit_call(unsigned int symndx)
1732   { this->local_16bit_calls_.insert(symndx); }
1733
1734   // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1735   // symbol SYMNDX in this object.
1736   bool
1737   has_local_16bit_call_relocs(unsigned int symndx)
1738   {
1739     return (this->local_16bit_calls_.find(symndx)
1740             != this->local_16bit_calls_.end());
1741   }
1742
1743   // Get gp value that was used to create this object.
1744   Mips_address
1745   gp_value() const
1746   { return this->gp_; }
1747
1748   // Return whether the object is a PIC object.
1749   bool
1750   is_pic() const
1751   { return this->is_pic_; }
1752
1753   // Return whether the object uses N32 ABI.
1754   bool
1755   is_n32() const
1756   { return this->is_n32_; }
1757
1758   // Return whether the object uses N64 ABI.
1759   bool
1760   is_n64() const
1761   { return size == 64; }
1762
1763   // Return whether the object uses NewABI conventions.
1764   bool
1765   is_newabi() const
1766   { return this->is_n32() || this->is_n64(); }
1767
1768   // Return Mips_got_info for this object.
1769   Mips_got_info<size, big_endian>*
1770   get_got_info() const
1771   { return this->got_info_; }
1772
1773   // Return Mips_got_info for this object.  Create new info if it doesn't exist.
1774   Mips_got_info<size, big_endian>*
1775   get_or_create_got_info()
1776   {
1777     if (!this->got_info_)
1778       this->got_info_ = new Mips_got_info<size, big_endian>();
1779     return this->got_info_;
1780   }
1781
1782   // Set Mips_got_info for this object.
1783   void
1784   set_got_info(Mips_got_info<size, big_endian>* got_info)
1785   { this->got_info_ = got_info; }
1786
1787   // Whether a section SHDNX is a MIPS16 stub section.  This is only valid
1788   // after do_read_symbols is called.
1789   bool
1790   is_mips16_stub_section(unsigned int shndx)
1791   {
1792     return (is_mips16_fn_stub_section(shndx)
1793             || is_mips16_call_stub_section(shndx)
1794             || is_mips16_call_fp_stub_section(shndx));
1795   }
1796
1797   // Return TRUE if relocations in section SHNDX can refer directly to a
1798   // MIPS16 function rather than to a hard-float stub.  This is only valid
1799   // after do_read_symbols is called.
1800   bool
1801   section_allows_mips16_refs(unsigned int shndx)
1802   {
1803     return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1804   }
1805
1806   // Whether a section SHDNX is a MIPS16 fn stub section.  This is only valid
1807   // after do_read_symbols is called.
1808   bool
1809   is_mips16_fn_stub_section(unsigned int shndx)
1810   {
1811     gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1812     return this->section_is_mips16_fn_stub_[shndx];
1813   }
1814
1815   // Whether a section SHDNX is a MIPS16 call stub section.  This is only valid
1816   // after do_read_symbols is called.
1817   bool
1818   is_mips16_call_stub_section(unsigned int shndx)
1819   {
1820     gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1821     return this->section_is_mips16_call_stub_[shndx];
1822   }
1823
1824   // Whether a section SHDNX is a MIPS16 call_fp stub section.  This is only
1825   // valid after do_read_symbols is called.
1826   bool
1827   is_mips16_call_fp_stub_section(unsigned int shndx)
1828   {
1829     gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1830     return this->section_is_mips16_call_fp_stub_[shndx];
1831   }
1832
1833   // Discard MIPS16 stub secions that are not needed.
1834   void
1835   discard_mips16_stub_sections(Symbol_table* symtab);
1836
1837   // Return whether there is a .reginfo section.
1838   bool
1839   has_reginfo_section() const
1840   { return this->has_reginfo_section_; }
1841
1842   // Return whether we want to merge processor-specific data.
1843   bool
1844   merge_processor_specific_data() const
1845   { return this->merge_processor_specific_data_; }
1846
1847   // Return gprmask from the .reginfo section of this object.
1848   Valtype
1849   gprmask() const
1850   { return this->gprmask_; }
1851
1852   // Return cprmask1 from the .reginfo section of this object.
1853   Valtype
1854   cprmask1() const
1855   { return this->cprmask1_; }
1856
1857   // Return cprmask2 from the .reginfo section of this object.
1858   Valtype
1859   cprmask2() const
1860   { return this->cprmask2_; }
1861
1862   // Return cprmask3 from the .reginfo section of this object.
1863   Valtype
1864   cprmask3() const
1865   { return this->cprmask3_; }
1866
1867   // Return cprmask4 from the .reginfo section of this object.
1868   Valtype
1869   cprmask4() const
1870   { return this->cprmask4_; }
1871
1872   // This is the contents of the .MIPS.abiflags section if there is one.
1873   Mips_abiflags<big_endian>*
1874   abiflags()
1875   { return this->abiflags_; }
1876
1877   // This is the contents of the .gnu.attribute section if there is one.
1878   const Attributes_section_data*
1879   attributes_section_data() const
1880   { return this->attributes_section_data_; }
1881
1882  protected:
1883   // Count the local symbols.
1884   void
1885   do_count_local_symbols(Stringpool_template<char>*,
1886                          Stringpool_template<char>*);
1887
1888   // Read the symbol information.
1889   void
1890   do_read_symbols(Read_symbols_data* sd);
1891
1892  private:
1893   // The name of the options section.
1894   const char* mips_elf_options_section_name()
1895   { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1896
1897   // processor-specific flags in ELF file header.
1898   elfcpp::Elf_Word processor_specific_flags_;
1899
1900   // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1901   // This is only valid after do_count_local_symbol is called.
1902   std::vector<bool> local_symbol_is_mips16_;
1903
1904   // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1905   // This is only valid after do_count_local_symbol is called.
1906   std::vector<bool> local_symbol_is_micromips_;
1907
1908   // Map from section index to the MIPS16 stub for that section.  This contains
1909   // all stubs found in this object.
1910   Mips16_stubs_int_map mips16_stub_sections_;
1911
1912   // Local symbols that have "non 16-bit" call relocation.  This relocation
1913   // would need to refer to a MIPS16 fn stub, if there is one.
1914   std::set<unsigned int> local_non_16bit_calls_;
1915
1916   // Local symbols that have 16-bit call relocation R_MIPS16_26.  Local MIPS16
1917   // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1918   // relocation that refers to the stub symbol.
1919   std::set<unsigned int> local_16bit_calls_;
1920
1921   // Map from local symbol index to the MIPS16 fn stub for that symbol.
1922   // This contains only the stubs that we decided not to discard.
1923   Mips16_stubs_int_map local_mips16_fn_stubs_;
1924
1925   // Map from local symbol index to the MIPS16 call stub for that symbol.
1926   // This contains only the stubs that we decided not to discard.
1927   Mips16_stubs_int_map local_mips16_call_stubs_;
1928
1929   // gp value that was used to create this object.
1930   Mips_address gp_;
1931   // Whether the object is a PIC object.
1932   bool is_pic_ : 1;
1933   // Whether the object uses N32 ABI.
1934   bool is_n32_ : 1;
1935   // Whether the object contains a .reginfo section.
1936   bool has_reginfo_section_ : 1;
1937   // Whether we merge processor-specific data of this object to output.
1938   bool merge_processor_specific_data_ : 1;
1939   // The Mips_got_info for this object.
1940   Mips_got_info<size, big_endian>* got_info_;
1941
1942   // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1943   // This is only valid after do_read_symbols is called.
1944   std::vector<bool> section_is_mips16_fn_stub_;
1945
1946   // Bit vector to tell if a section is a MIPS16 call stub section or not.
1947   // This is only valid after do_read_symbols is called.
1948   std::vector<bool> section_is_mips16_call_stub_;
1949
1950   // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1951   // This is only valid after do_read_symbols is called.
1952   std::vector<bool> section_is_mips16_call_fp_stub_;
1953
1954   // .pdr section index.
1955   unsigned int pdr_shndx_;
1956
1957   // Object attributes if there is a .gnu.attributes section or NULL.
1958   Attributes_section_data* attributes_section_data_;
1959
1960   // Object abiflags if there is a .MIPS.abiflags section or NULL.
1961   Mips_abiflags<big_endian>* abiflags_;
1962
1963   // gprmask from the .reginfo section of this object.
1964   Valtype gprmask_;
1965   // cprmask1 from the .reginfo section of this object.
1966   Valtype cprmask1_;
1967   // cprmask2 from the .reginfo section of this object.
1968   Valtype cprmask2_;
1969   // cprmask3 from the .reginfo section of this object.
1970   Valtype cprmask3_;
1971   // cprmask4 from the .reginfo section of this object.
1972   Valtype cprmask4_;
1973 };
1974
1975 // Mips_output_data_got class.
1976
1977 template<int size, bool big_endian>
1978 class Mips_output_data_got : public Output_data_got<size, big_endian>
1979 {
1980   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1981   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1982     Reloc_section;
1983   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1984
1985  public:
1986   Mips_output_data_got(Target_mips<size, big_endian>* target,
1987       Symbol_table* symtab, Layout* layout)
1988     : Output_data_got<size, big_endian>(), target_(target),
1989       symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1990       first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1991       secondary_got_relocs_()
1992   {
1993     this->master_got_info_ = new Mips_got_info<size, big_endian>();
1994     this->set_addralign(16);
1995   }
1996
1997   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1998   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1999   void
2000   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
2001                           unsigned int symndx, Mips_address addend,
2002                           unsigned int r_type, unsigned int shndx,
2003                           bool is_section_symbol)
2004   {
2005     this->master_got_info_->record_local_got_symbol(object, symndx, addend,
2006                                                     r_type, shndx,
2007                                                     is_section_symbol);
2008   }
2009
2010   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
2011   // in OBJECT.  FOR_CALL is true if the caller is only interested in
2012   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
2013   // relocation.
2014   void
2015   record_global_got_symbol(Mips_symbol<size>* mips_sym,
2016                            Mips_relobj<size, big_endian>* object,
2017                            unsigned int r_type, bool dyn_reloc, bool for_call)
2018   {
2019     this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2020                                                      dyn_reloc, for_call);
2021   }
2022
2023   // Record that OBJECT has a page relocation against symbol SYMNDX and
2024   // that ADDEND is the addend for that relocation.
2025   void
2026   record_got_page_entry(Mips_relobj<size, big_endian>* object,
2027                         unsigned int symndx, int addend)
2028   { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2029
2030   // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
2031   // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2032   // applied in a static link.
2033   void
2034   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2035                    Mips_symbol<size>* gsym)
2036   { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2037
2038   // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
2039   // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
2040   // relocation that needs to be applied in a static link.
2041   void
2042   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2043                    Sized_relobj_file<size, big_endian>* relobj,
2044                    unsigned int index)
2045   {
2046     this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2047                                                 index));
2048   }
2049
2050   // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2051   // secondary GOT at OFFSET.
2052   void
2053   add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2054                           Mips_symbol<size>* gsym)
2055   {
2056     this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2057                                                        r_type, gsym));
2058   }
2059
2060   // Update GOT entry at OFFSET with VALUE.
2061   void
2062   update_got_entry(unsigned int offset, Mips_address value)
2063   {
2064     elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2065   }
2066
2067   // Return the number of entries in local part of the GOT.  This includes
2068   // local entries, page entries and 2 reserved entries.
2069   unsigned int
2070   get_local_gotno() const
2071   {
2072     if (!this->multi_got())
2073       {
2074         return (2 + this->master_got_info_->local_gotno()
2075                 + this->master_got_info_->page_gotno());
2076       }
2077     else
2078       return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2079   }
2080
2081   // Return dynamic symbol table index of the first symbol with global GOT
2082   // entry.
2083   unsigned int
2084   first_global_got_dynsym_index() const
2085   { return this->first_global_got_dynsym_index_; }
2086
2087   // Set dynamic symbol table index of the first symbol with global GOT entry.
2088   void
2089   set_first_global_got_dynsym_index(unsigned int index)
2090   { this->first_global_got_dynsym_index_ = index; }
2091
2092   // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
2093   // larger than 64K, create multi-GOT.
2094   void
2095   lay_out_got(Layout* layout, Symbol_table* symtab,
2096               const Input_objects* input_objects);
2097
2098   // Create multi-GOT.  For every GOT, add local, global and TLS entries.
2099   void
2100   lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2101
2102   // Attempt to merge GOTs of different input objects.
2103   void
2104   merge_gots(const Input_objects* input_objects);
2105
2106   // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
2107   // this would lead to overflow, true if they were merged successfully.
2108   bool
2109   merge_got_with(Mips_got_info<size, big_endian>* from,
2110                  Mips_relobj<size, big_endian>* object,
2111                  Mips_got_info<size, big_endian>* to);
2112
2113   // Return the offset of GOT page entry for VALUE.  For multi-GOT links,
2114   // use OBJECT's GOT.
2115   unsigned int
2116   get_got_page_offset(Mips_address value,
2117                       const Mips_relobj<size, big_endian>* object)
2118   {
2119     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2120                                           ? this->master_got_info_
2121                                           : object->get_got_info());
2122     gold_assert(g != NULL);
2123     return g->get_got_page_offset(value, this);
2124   }
2125
2126   // Return the GOT offset of type GOT_TYPE of the global symbol
2127   // GSYM.  For multi-GOT links, use OBJECT's GOT.
2128   unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2129                           Mips_relobj<size, big_endian>* object) const
2130   {
2131     if (!this->multi_got())
2132       return gsym->got_offset(got_type);
2133     else
2134       {
2135         Mips_got_info<size, big_endian>* g = object->get_got_info();
2136         gold_assert(g != NULL);
2137         return gsym->got_offset(g->multigot_got_type(got_type));
2138       }
2139   }
2140
2141   // Return the GOT offset of type GOT_TYPE of the local symbol
2142   // SYMNDX.
2143   unsigned int
2144   got_offset(unsigned int symndx, unsigned int got_type,
2145              Sized_relobj_file<size, big_endian>* object,
2146              uint64_t addend) const
2147   { return object->local_got_offset(symndx, got_type, addend); }
2148
2149   // Return the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2150   unsigned int
2151   tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2152   {
2153     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2154                                           ? this->master_got_info_
2155                                           : object->get_got_info());
2156     gold_assert(g != NULL);
2157     return g->tls_ldm_offset();
2158   }
2159
2160   // Set the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2161   void
2162   set_tls_ldm_offset(unsigned int tls_ldm_offset,
2163                      Mips_relobj<size, big_endian>* object)
2164   {
2165     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2166                                           ? this->master_got_info_
2167                                           : object->get_got_info());
2168     gold_assert(g != NULL);
2169     g->set_tls_ldm_offset(tls_ldm_offset);
2170   }
2171
2172   // Return true for multi-GOT links.
2173   bool
2174   multi_got() const
2175   { return this->primary_got_ != NULL; }
2176
2177   // Return the offset of OBJECT's GOT from the start of .got section.
2178   unsigned int
2179   get_got_offset(const Mips_relobj<size, big_endian>* object)
2180   {
2181     if (!this->multi_got())
2182       return 0;
2183     else
2184       {
2185         Mips_got_info<size, big_endian>* g = object->get_got_info();
2186         return g != NULL ? g->offset() : 0;
2187       }
2188   }
2189
2190   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2191   void
2192   add_reloc_only_entries()
2193   { this->master_got_info_->add_reloc_only_entries(this); }
2194
2195   // Return offset of the primary GOT's entry for global symbol.
2196   unsigned int
2197   get_primary_got_offset(const Mips_symbol<size>* sym) const
2198   {
2199     gold_assert(sym->global_got_area() != GGA_NONE);
2200     return (this->get_local_gotno() + sym->dynsym_index()
2201             - this->first_global_got_dynsym_index()) * size/8;
2202   }
2203
2204   // For the entry at offset GOT_OFFSET, return its offset from the gp.
2205   // Input argument GOT_OFFSET is always global offset from the start of
2206   // .got section, for both single and multi-GOT links.
2207   // For single GOT links, this returns GOT_OFFSET - 0x7FF0.  For multi-GOT
2208   // links, the return value is object_got_offset - 0x7FF0, where
2209   // object_got_offset is offset in the OBJECT's GOT.
2210   int
2211   gp_offset(unsigned int got_offset,
2212             const Mips_relobj<size, big_endian>* object) const
2213   {
2214     return (this->address() + got_offset
2215             - this->target_->adjusted_gp_value(object));
2216   }
2217
2218  protected:
2219   // Write out the GOT table.
2220   void
2221   do_write(Output_file*);
2222
2223  private:
2224
2225   // This class represent dynamic relocations that need to be applied by
2226   // gold because we are using TLS relocations in a static link.
2227   class Static_reloc
2228   {
2229    public:
2230     Static_reloc(unsigned int got_offset, unsigned int r_type,
2231                  Mips_symbol<size>* gsym)
2232       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2233     { this->u_.global.symbol = gsym; }
2234
2235     Static_reloc(unsigned int got_offset, unsigned int r_type,
2236           Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2237       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2238     {
2239       this->u_.local.relobj = relobj;
2240       this->u_.local.index = index;
2241     }
2242
2243     // Return the GOT offset.
2244     unsigned int
2245     got_offset() const
2246     { return this->got_offset_; }
2247
2248     // Relocation type.
2249     unsigned int
2250     r_type() const
2251     { return this->r_type_; }
2252
2253     // Whether the symbol is global or not.
2254     bool
2255     symbol_is_global() const
2256     { return this->symbol_is_global_; }
2257
2258     // For a relocation against a global symbol, the global symbol.
2259     Mips_symbol<size>*
2260     symbol() const
2261     {
2262       gold_assert(this->symbol_is_global_);
2263       return this->u_.global.symbol;
2264     }
2265
2266     // For a relocation against a local symbol, the defining object.
2267     Sized_relobj_file<size, big_endian>*
2268     relobj() const
2269     {
2270       gold_assert(!this->symbol_is_global_);
2271       return this->u_.local.relobj;
2272     }
2273
2274     // For a relocation against a local symbol, the local symbol index.
2275     unsigned int
2276     index() const
2277     {
2278       gold_assert(!this->symbol_is_global_);
2279       return this->u_.local.index;
2280     }
2281
2282    private:
2283     // GOT offset of the entry to which this relocation is applied.
2284     unsigned int got_offset_;
2285     // Type of relocation.
2286     unsigned int r_type_;
2287     // Whether this relocation is against a global symbol.
2288     bool symbol_is_global_;
2289     // A global or local symbol.
2290     union
2291     {
2292       struct
2293       {
2294         // For a global symbol, the symbol itself.
2295         Mips_symbol<size>* symbol;
2296       } global;
2297       struct
2298       {
2299         // For a local symbol, the object defining object.
2300         Sized_relobj_file<size, big_endian>* relobj;
2301         // For a local symbol, the symbol index.
2302         unsigned int index;
2303       } local;
2304     } u_;
2305   };
2306
2307   // The target.
2308   Target_mips<size, big_endian>* target_;
2309   // The symbol table.
2310   Symbol_table* symbol_table_;
2311   // The layout.
2312   Layout* layout_;
2313   // Static relocs to be applied to the GOT.
2314   std::vector<Static_reloc> static_relocs_;
2315   // .got section view.
2316   unsigned char* got_view_;
2317   // The dynamic symbol table index of the first symbol with global GOT entry.
2318   unsigned int first_global_got_dynsym_index_;
2319   // The master GOT information.
2320   Mips_got_info<size, big_endian>* master_got_info_;
2321   // The  primary GOT information.
2322   Mips_got_info<size, big_endian>* primary_got_;
2323   // Secondary GOT fixups.
2324   std::vector<Static_reloc> secondary_got_relocs_;
2325 };
2326
2327 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2328 // two ways of creating these interfaces.  The first is to add:
2329 //
2330 //      lui     $25,%hi(func)
2331 //      j       func
2332 //      addiu   $25,$25,%lo(func)
2333 //
2334 // to a separate trampoline section.  The second is to add:
2335 //
2336 //      lui     $25,%hi(func)
2337 //      addiu   $25,$25,%lo(func)
2338 //
2339 // immediately before a PIC function "func", but only if a function is at the
2340 // beginning of the section, and the section is not too heavily aligned (i.e we
2341 // would need to add no more than 2 nops before the stub.)
2342 //
2343 // We only create stubs of the first type.
2344
2345 template<int size, bool big_endian>
2346 class Mips_output_data_la25_stub : public Output_section_data
2347 {
2348   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2349
2350  public:
2351   Mips_output_data_la25_stub()
2352   : Output_section_data(size == 32 ? 4 : 8), symbols_()
2353   { }
2354
2355   // Create LA25 stub for a symbol.
2356   void
2357   create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2358                    Mips_symbol<size>* gsym);
2359
2360   // Return output address of a stub.
2361   Mips_address
2362   stub_address(const Mips_symbol<size>* sym) const
2363   {
2364     gold_assert(sym->has_la25_stub());
2365     return this->address() + sym->la25_stub_offset();
2366   }
2367
2368  protected:
2369   void
2370   do_adjust_output_section(Output_section* os)
2371   { os->set_entsize(0); }
2372
2373  private:
2374   // Template for standard LA25 stub.
2375   static const uint32_t la25_stub_entry[];
2376   // Template for microMIPS LA25 stub.
2377   static const uint32_t la25_stub_micromips_entry[];
2378
2379   // Set the final size.
2380   void
2381   set_final_data_size()
2382   { this->set_data_size(this->symbols_.size() * 16); }
2383
2384   // Create a symbol for SYM stub's value and size, to help make the
2385   // disassembly easier to read.
2386   void
2387   create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2388                      Target_mips<size, big_endian>* target, uint64_t symsize);
2389
2390   // Write to a map file.
2391   void
2392   do_print_to_mapfile(Mapfile* mapfile) const
2393   { mapfile->print_output_data(this, _(".LA25.stubs")); }
2394
2395   // Write out the LA25 stub section.
2396   void
2397   do_write(Output_file*);
2398
2399   // Symbols that have LA25 stubs.
2400   std::vector<Mips_symbol<size>*> symbols_;
2401 };
2402
2403 // MIPS-specific relocation writer.
2404
2405 template<int sh_type, bool dynamic, int size, bool big_endian>
2406 struct Mips_output_reloc_writer;
2407
2408 template<int sh_type, bool dynamic, bool big_endian>
2409 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2410 {
2411   typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2412   typedef std::vector<Output_reloc_type> Relocs;
2413
2414   static void
2415   write(typename Relocs::const_iterator p, unsigned char* pov)
2416   { p->write(pov); }
2417 };
2418
2419 template<int sh_type, bool dynamic, bool big_endian>
2420 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2421 {
2422   typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2423   typedef std::vector<Output_reloc_type> Relocs;
2424
2425   static void
2426   write(typename Relocs::const_iterator p, unsigned char* pov)
2427   {
2428     elfcpp::Mips64_rel_write<big_endian> orel(pov);
2429     orel.put_r_offset(p->get_address());
2430     orel.put_r_sym(p->get_symbol_index());
2431     orel.put_r_ssym(RSS_UNDEF);
2432     orel.put_r_type(p->type());
2433     if (p->type() == elfcpp::R_MIPS_REL32)
2434       orel.put_r_type2(elfcpp::R_MIPS_64);
2435     else
2436       orel.put_r_type2(elfcpp::R_MIPS_NONE);
2437     orel.put_r_type3(elfcpp::R_MIPS_NONE);
2438   }
2439 };
2440
2441 template<int sh_type, bool dynamic, int size, bool big_endian>
2442 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2443                                                         size, big_endian>
2444 {
2445  public:
2446   Mips_output_data_reloc(bool sort_relocs)
2447     : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2448   { }
2449
2450  protected:
2451   // Write out the data.
2452   void
2453   do_write(Output_file* of)
2454   {
2455     typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2456         big_endian> Writer;
2457     this->template do_write_generic<Writer>(of);
2458   }
2459 };
2460
2461
2462 // A class to handle the PLT data.
2463
2464 template<int size, bool big_endian>
2465 class Mips_output_data_plt : public Output_section_data
2466 {
2467   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2468   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2469                                  size, big_endian> Reloc_section;
2470
2471  public:
2472   // Create the PLT section.  The ordinary .got section is an argument,
2473   // since we need to refer to the start.
2474   Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2475                        Target_mips<size, big_endian>* target)
2476     : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2477       plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2478       target_(target)
2479   {
2480     this->rel_ = new Reloc_section(false);
2481     layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2482                                     elfcpp::SHF_ALLOC, this->rel_,
2483                                     ORDER_DYNAMIC_PLT_RELOCS, false);
2484   }
2485
2486   // Add an entry to the PLT for a symbol referenced by r_type relocation.
2487   void
2488   add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2489
2490   // Return the .rel.plt section data.
2491   Reloc_section*
2492   rel_plt() const
2493   { return this->rel_; }
2494
2495   // Return the number of PLT entries.
2496   unsigned int
2497   entry_count() const
2498   { return this->symbols_.size(); }
2499
2500   // Return the offset of the first non-reserved PLT entry.
2501   unsigned int
2502   first_plt_entry_offset() const
2503   { return sizeof(plt0_entry_o32); }
2504
2505   // Return the size of a PLT entry.
2506   unsigned int
2507   plt_entry_size() const
2508   { return sizeof(plt_entry); }
2509
2510   // Set final PLT offsets.  For each symbol, determine whether standard or
2511   // compressed (MIPS16 or microMIPS) PLT entry is used.
2512   void
2513   set_plt_offsets();
2514
2515   // Return the offset of the first standard PLT entry.
2516   unsigned int
2517   first_mips_plt_offset() const
2518   { return this->plt_header_size_; }
2519
2520   // Return the offset of the first compressed PLT entry.
2521   unsigned int
2522   first_comp_plt_offset() const
2523   { return this->plt_header_size_ + this->plt_mips_offset_; }
2524
2525   // Return whether there are any standard PLT entries.
2526   bool
2527   has_standard_entries() const
2528   { return this->plt_mips_offset_ > 0; }
2529
2530   // Return the output address of standard PLT entry.
2531   Mips_address
2532   mips_entry_address(const Mips_symbol<size>* sym) const
2533   {
2534     gold_assert (sym->has_mips_plt_offset());
2535     return (this->address() + this->first_mips_plt_offset()
2536             + sym->mips_plt_offset());
2537   }
2538
2539   // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2540   Mips_address
2541   comp_entry_address(const Mips_symbol<size>* sym) const
2542   {
2543     gold_assert (sym->has_comp_plt_offset());
2544     return (this->address() + this->first_comp_plt_offset()
2545             + sym->comp_plt_offset());
2546   }
2547
2548  protected:
2549   void
2550   do_adjust_output_section(Output_section* os)
2551   { os->set_entsize(0); }
2552
2553   // Write to a map file.
2554   void
2555   do_print_to_mapfile(Mapfile* mapfile) const
2556   { mapfile->print_output_data(this, _(".plt")); }
2557
2558  private:
2559   // Template for the first PLT entry.
2560   static const uint32_t plt0_entry_o32[];
2561   static const uint32_t plt0_entry_n32[];
2562   static const uint32_t plt0_entry_n64[];
2563   static const uint32_t plt0_entry_micromips_o32[];
2564   static const uint32_t plt0_entry_micromips32_o32[];
2565
2566   // Template for subsequent PLT entries.
2567   static const uint32_t plt_entry[];
2568   static const uint32_t plt_entry_r6[];
2569   static const uint32_t plt_entry_mips16_o32[];
2570   static const uint32_t plt_entry_micromips_o32[];
2571   static const uint32_t plt_entry_micromips32_o32[];
2572
2573   // Set the final size.
2574   void
2575   set_final_data_size()
2576   {
2577     this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2578                         + this->plt_comp_offset_);
2579   }
2580
2581   // Write out the PLT data.
2582   void
2583   do_write(Output_file*);
2584
2585   // Return whether the plt header contains microMIPS code.  For the sake of
2586   // cache alignment always use a standard header whenever any standard entries
2587   // are present even if microMIPS entries are present as well.  This also lets
2588   // the microMIPS header rely on the value of $v0 only set by microMIPS
2589   // entries, for a small size reduction.
2590   bool
2591   is_plt_header_compressed() const
2592   {
2593     gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2594     return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2595   }
2596
2597   // Return the size of the PLT header.
2598   unsigned int
2599   get_plt_header_size() const
2600   {
2601     if (this->target_->is_output_n64())
2602       return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2603     else if (this->target_->is_output_n32())
2604       return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2605     else if (!this->is_plt_header_compressed())
2606       return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2607     else if (this->target_->use_32bit_micromips_instructions())
2608       return (2 * sizeof(plt0_entry_micromips32_o32)
2609               / sizeof(plt0_entry_micromips32_o32[0]));
2610     else
2611       return (2 * sizeof(plt0_entry_micromips_o32)
2612               / sizeof(plt0_entry_micromips_o32[0]));
2613   }
2614
2615   // Return the PLT header entry.
2616   const uint32_t*
2617   get_plt_header_entry() const
2618   {
2619     if (this->target_->is_output_n64())
2620       return plt0_entry_n64;
2621     else if (this->target_->is_output_n32())
2622       return plt0_entry_n32;
2623     else if (!this->is_plt_header_compressed())
2624       return plt0_entry_o32;
2625     else if (this->target_->use_32bit_micromips_instructions())
2626       return plt0_entry_micromips32_o32;
2627     else
2628       return plt0_entry_micromips_o32;
2629   }
2630
2631   // Return the size of the standard PLT entry.
2632   unsigned int
2633   standard_plt_entry_size() const
2634   { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2635
2636   // Return the size of the compressed PLT entry.
2637   unsigned int
2638   compressed_plt_entry_size() const
2639   {
2640     gold_assert(!this->target_->is_output_newabi());
2641
2642     if (!this->target_->is_output_micromips())
2643       return (2 * sizeof(plt_entry_mips16_o32)
2644               / sizeof(plt_entry_mips16_o32[0]));
2645     else if (this->target_->use_32bit_micromips_instructions())
2646       return (2 * sizeof(plt_entry_micromips32_o32)
2647               / sizeof(plt_entry_micromips32_o32[0]));
2648     else
2649       return (2 * sizeof(plt_entry_micromips_o32)
2650               / sizeof(plt_entry_micromips_o32[0]));
2651   }
2652
2653   // The reloc section.
2654   Reloc_section* rel_;
2655   // The .got.plt section.
2656   Output_data_space* got_plt_;
2657   // Symbols that have PLT entry.
2658   std::vector<Mips_symbol<size>*> symbols_;
2659   // The offset of the next standard PLT entry to create.
2660   unsigned int plt_mips_offset_;
2661   // The offset of the next compressed PLT entry to create.
2662   unsigned int plt_comp_offset_;
2663   // The size of the PLT header in bytes.
2664   unsigned int plt_header_size_;
2665   // The target.
2666   Target_mips<size, big_endian>* target_;
2667 };
2668
2669 // A class to handle the .MIPS.stubs data.
2670
2671 template<int size, bool big_endian>
2672 class Mips_output_data_mips_stubs : public Output_section_data
2673 {
2674   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2675
2676   // Unordered set of .MIPS.stubs entries.
2677   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2678       Mips_stubs_entry_set;
2679
2680  public:
2681    Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2682      : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2683        stub_offsets_are_set_(false), target_(target)
2684    { }
2685
2686   // Create entry for a symbol.
2687   void
2688   make_entry(Mips_symbol<size>*);
2689
2690   // Remove entry for a symbol.
2691   void
2692   remove_entry(Mips_symbol<size>* gsym);
2693
2694   // Set stub offsets for symbols.  This method expects that the number of
2695   // entries in dynamic symbol table is set.
2696   void
2697   set_lazy_stub_offsets();
2698
2699   void
2700   set_needs_dynsym_value();
2701
2702    // Set the number of entries in dynamic symbol table.
2703   void
2704   set_dynsym_count(unsigned int dynsym_count)
2705   { this->dynsym_count_ = dynsym_count; }
2706
2707   // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2708   // count is greater than 0x10000.  If the dynamic symbol count is less than
2709   // 0x10000, the stub will be 4 bytes smaller.
2710   // There's no disadvantage from using microMIPS code here, so for the sake of
2711   // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2712   // output produced at all.  This has a benefit of stubs being shorter by
2713   // 4 bytes each too, unless in the insn32 mode.
2714   unsigned int
2715   stub_max_size() const
2716   {
2717     if (!this->target_->is_output_micromips()
2718         || this->target_->use_32bit_micromips_instructions())
2719       return 20;
2720     else
2721       return 16;
2722   }
2723
2724   // Return the size of the stub.  This method expects that the final dynsym
2725   // count is set.
2726   unsigned int
2727   stub_size() const
2728   {
2729     gold_assert(this->dynsym_count_ != -1U);
2730     if (this->dynsym_count_ > 0x10000)
2731       return this->stub_max_size();
2732     else
2733       return this->stub_max_size() - 4;
2734   }
2735
2736   // Return output address of a stub.
2737   Mips_address
2738   stub_address(const Mips_symbol<size>* sym) const
2739   {
2740     gold_assert(sym->has_lazy_stub());
2741     return this->address() + sym->lazy_stub_offset();
2742   }
2743
2744  protected:
2745   void
2746   do_adjust_output_section(Output_section* os)
2747   { os->set_entsize(0); }
2748
2749   // Write to a map file.
2750   void
2751   do_print_to_mapfile(Mapfile* mapfile) const
2752   { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2753
2754  private:
2755   static const uint32_t lazy_stub_normal_1[];
2756   static const uint32_t lazy_stub_normal_1_n64[];
2757   static const uint32_t lazy_stub_normal_2[];
2758   static const uint32_t lazy_stub_normal_2_n64[];
2759   static const uint32_t lazy_stub_big[];
2760   static const uint32_t lazy_stub_big_n64[];
2761
2762   static const uint32_t lazy_stub_micromips_normal_1[];
2763   static const uint32_t lazy_stub_micromips_normal_1_n64[];
2764   static const uint32_t lazy_stub_micromips_normal_2[];
2765   static const uint32_t lazy_stub_micromips_normal_2_n64[];
2766   static const uint32_t lazy_stub_micromips_big[];
2767   static const uint32_t lazy_stub_micromips_big_n64[];
2768
2769   static const uint32_t lazy_stub_micromips32_normal_1[];
2770   static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2771   static const uint32_t lazy_stub_micromips32_normal_2[];
2772   static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2773   static const uint32_t lazy_stub_micromips32_big[];
2774   static const uint32_t lazy_stub_micromips32_big_n64[];
2775
2776   // Set the final size.
2777   void
2778   set_final_data_size()
2779   { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2780
2781   // Write out the .MIPS.stubs data.
2782   void
2783   do_write(Output_file*);
2784
2785   // .MIPS.stubs symbols
2786   Mips_stubs_entry_set symbols_;
2787   // Number of entries in dynamic symbol table.
2788   unsigned int dynsym_count_;
2789   // Whether the stub offsets are set.
2790   bool stub_offsets_are_set_;
2791   // The target.
2792   Target_mips<size, big_endian>* target_;
2793 };
2794
2795 // This class handles Mips .reginfo output section.
2796
2797 template<int size, bool big_endian>
2798 class Mips_output_section_reginfo : public Output_section_data
2799 {
2800   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2801
2802  public:
2803   Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2804                               Valtype gprmask, Valtype cprmask1,
2805                               Valtype cprmask2, Valtype cprmask3,
2806                               Valtype cprmask4)
2807     : Output_section_data(24, 4, true), target_(target),
2808       gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2809       cprmask3_(cprmask3), cprmask4_(cprmask4)
2810   { }
2811
2812  protected:
2813   // Write to a map file.
2814   void
2815   do_print_to_mapfile(Mapfile* mapfile) const
2816   { mapfile->print_output_data(this, _(".reginfo")); }
2817
2818   // Write out reginfo section.
2819   void
2820   do_write(Output_file* of);
2821
2822  private:
2823   Target_mips<size, big_endian>* target_;
2824
2825   // gprmask of the output .reginfo section.
2826   Valtype gprmask_;
2827   // cprmask1 of the output .reginfo section.
2828   Valtype cprmask1_;
2829   // cprmask2 of the output .reginfo section.
2830   Valtype cprmask2_;
2831   // cprmask3 of the output .reginfo section.
2832   Valtype cprmask3_;
2833   // cprmask4 of the output .reginfo section.
2834   Valtype cprmask4_;
2835 };
2836
2837 // This class handles .MIPS.options output section.
2838
2839 template<int size, bool big_endian>
2840 class Mips_output_section_options : public Output_section
2841 {
2842  public:
2843   Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
2844                               elfcpp::Elf_Xword flags,
2845                               Target_mips<size, big_endian>* target)
2846     : Output_section(name, type, flags), target_(target)
2847   {
2848     // After the input sections are written, we only need to update
2849     // ri_gp_value field of ODK_REGINFO entries.
2850     this->set_after_input_sections();
2851   }
2852
2853  protected:
2854   // Write out option section.
2855   void
2856   do_write(Output_file* of);
2857
2858  private:
2859   Target_mips<size, big_endian>* target_;
2860 };
2861
2862 // This class handles .MIPS.abiflags output section.
2863
2864 template<int size, bool big_endian>
2865 class Mips_output_section_abiflags : public Output_section_data
2866 {
2867  public:
2868   Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2869     : Output_section_data(24, 8, true), abiflags_(abiflags)
2870   { }
2871
2872  protected:
2873   // Write to a map file.
2874   void
2875   do_print_to_mapfile(Mapfile* mapfile) const
2876   { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2877
2878   void
2879   do_write(Output_file* of);
2880
2881  private:
2882   const Mips_abiflags<big_endian>& abiflags_;
2883 };
2884
2885 // The MIPS target has relocation types which default handling of relocatable
2886 // relocation cannot process.  So we have to extend the default code.
2887
2888 template<bool big_endian, typename Classify_reloc>
2889 class Mips_scan_relocatable_relocs :
2890   public Default_scan_relocatable_relocs<Classify_reloc>
2891 {
2892  public:
2893   // Return the strategy to use for a local symbol which is a section
2894   // symbol, given the relocation type.
2895   inline Relocatable_relocs::Reloc_strategy
2896   local_section_strategy(unsigned int r_type, Relobj* object)
2897   {
2898     if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2899       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2900     else
2901       {
2902         switch (r_type)
2903           {
2904           case elfcpp::R_MIPS_26:
2905             return Relocatable_relocs::RELOC_SPECIAL;
2906
2907           default:
2908             return Default_scan_relocatable_relocs<Classify_reloc>::
2909                 local_section_strategy(r_type, object);
2910           }
2911       }
2912   }
2913 };
2914
2915 // Mips_copy_relocs class.  The only difference from the base class is the
2916 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2917 // Mips cannot convert all relocation types to dynamic relocs.  If a reloc
2918 // cannot be made dynamic, a COPY reloc is emitted.
2919
2920 template<int sh_type, int size, bool big_endian>
2921 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2922 {
2923  public:
2924   Mips_copy_relocs()
2925     : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2926   { }
2927
2928   // Emit any saved relocations which turn out to be needed.  This is
2929   // called after all the relocs have been scanned.
2930   void
2931   emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2932             Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2933
2934  private:
2935   typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2936     Copy_reloc_entry;
2937
2938   // Emit this reloc if appropriate.  This is called after we have
2939   // scanned all the relocations, so we know whether we emitted a
2940   // COPY relocation for SYM_.
2941   void
2942   emit_entry(Copy_reloc_entry& entry,
2943              Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2944              Symbol_table* symtab, Layout* layout,
2945              Target_mips<size, big_endian>* target);
2946 };
2947
2948
2949 // Return true if the symbol SYM should be considered to resolve local
2950 // to the current module, and false otherwise.  The logic is taken from
2951 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2952 static bool
2953 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2954                   bool local_protected)
2955 {
2956   // If it's a local sym, of course we resolve locally.
2957   if (sym == NULL)
2958     return true;
2959
2960   // STV_HIDDEN or STV_INTERNAL ones must be local.
2961   if (sym->visibility() == elfcpp::STV_HIDDEN
2962       || sym->visibility() == elfcpp::STV_INTERNAL)
2963     return true;
2964
2965   // If we don't have a definition in a regular file, then we can't
2966   // resolve locally.  The sym is either undefined or dynamic.
2967   if (sym->is_from_dynobj() || sym->is_undefined())
2968     return false;
2969
2970   // Forced local symbols resolve locally.
2971   if (sym->is_forced_local())
2972     return true;
2973
2974   // As do non-dynamic symbols.
2975   if (!has_dynsym_entry)
2976     return true;
2977
2978   // At this point, we know the symbol is defined and dynamic.  In an
2979   // executable it must resolve locally, likewise when building symbolic
2980   // shared libraries.
2981   if (parameters->options().output_is_executable()
2982       || parameters->options().Bsymbolic())
2983     return true;
2984
2985   // Now deal with defined dynamic symbols in shared libraries.  Ones
2986   // with default visibility might not resolve locally.
2987   if (sym->visibility() == elfcpp::STV_DEFAULT)
2988     return false;
2989
2990   // STV_PROTECTED non-function symbols are local.
2991   if (sym->type() != elfcpp::STT_FUNC)
2992     return true;
2993
2994   // Function pointer equality tests may require that STV_PROTECTED
2995   // symbols be treated as dynamic symbols.  If the address of a
2996   // function not defined in an executable is set to that function's
2997   // plt entry in the executable, then the address of the function in
2998   // a shared library must also be the plt entry in the executable.
2999   return local_protected;
3000 }
3001
3002 // Return TRUE if references to this symbol always reference the symbol in this
3003 // object.
3004 static bool
3005 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3006 {
3007   return symbol_refs_local(sym, has_dynsym_entry, false);
3008 }
3009
3010 // Return TRUE if calls to this symbol always call the version in this object.
3011 static bool
3012 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3013 {
3014   return symbol_refs_local(sym, has_dynsym_entry, true);
3015 }
3016
3017 // Compare GOT offsets of two symbols.
3018
3019 template<int size, bool big_endian>
3020 static bool
3021 got_offset_compare(Symbol* sym1, Symbol* sym2)
3022 {
3023   Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
3024   Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
3025   unsigned int area1 = mips_sym1->global_got_area();
3026   unsigned int area2 = mips_sym2->global_got_area();
3027   gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
3028
3029   // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3030   if (area1 != area2)
3031     return area1 < area2;
3032
3033   return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
3034 }
3035
3036 // This method divides dynamic symbols into symbols that have GOT entry, and
3037 // symbols that don't have GOT entry.  It also sorts symbols with the GOT entry.
3038 // Mips ABI requires that symbols with the GOT entry must be at the end of
3039 // dynamic symbol table, and the order in dynamic symbol table must match the
3040 // order in GOT.
3041
3042 template<int size, bool big_endian>
3043 static void
3044 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3045                     std::vector<Symbol*>* non_got_symbols,
3046                     std::vector<Symbol*>* got_symbols)
3047 {
3048   for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3049        p != dyn_symbols->end();
3050        ++p)
3051     {
3052       Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3053       if (mips_sym->global_got_area() == GGA_NORMAL
3054           || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3055         got_symbols->push_back(mips_sym);
3056       else
3057         non_got_symbols->push_back(mips_sym);
3058     }
3059
3060   std::sort(got_symbols->begin(), got_symbols->end(),
3061             got_offset_compare<size, big_endian>);
3062 }
3063
3064 // Functor class for processing the global symbol table.
3065
3066 template<int size, bool big_endian>
3067 class Symbol_visitor_check_symbols
3068 {
3069  public:
3070   Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3071     Layout* layout, Symbol_table* symtab)
3072     : target_(target), layout_(layout), symtab_(symtab)
3073   { }
3074
3075   void
3076   operator()(Sized_symbol<size>* sym)
3077   {
3078     Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3079     if (local_pic_function<size, big_endian>(mips_sym))
3080       {
3081         // SYM is a function that might need $25 to be valid on entry.
3082         // If we're creating a non-PIC relocatable object, mark SYM as
3083         // being PIC.  If we're creating a non-relocatable object with
3084         // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3085         // stub.
3086         if (parameters->options().relocatable())
3087           {
3088             if (!parameters->options().output_is_position_independent())
3089               mips_sym->set_pic();
3090           }
3091         else if (mips_sym->has_nonpic_branches())
3092           {
3093             this->target_->la25_stub_section(layout_)
3094                 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3095           }
3096       }
3097   }
3098
3099  private:
3100   Target_mips<size, big_endian>* target_;
3101   Layout* layout_;
3102   Symbol_table* symtab_;
3103 };
3104
3105 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3106 // and endianness. The relocation format for MIPS-64 is non-standard.
3107
3108 template<int sh_type, int size, bool big_endian>
3109 struct Mips_reloc_types;
3110
3111 template<bool big_endian>
3112 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3113 {
3114   typedef typename elfcpp::Rel<32, big_endian> Reloc;
3115   typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3116
3117   static typename elfcpp::Elf_types<32>::Elf_Swxword
3118   get_r_addend(const Reloc*)
3119   { return 0; }
3120
3121   static inline void
3122   set_reloc_addend(Reloc_write*,
3123                    typename elfcpp::Elf_types<32>::Elf_Swxword)
3124   { gold_unreachable(); }
3125 };
3126
3127 template<bool big_endian>
3128 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3129 {
3130   typedef typename elfcpp::Rela<32, big_endian> Reloc;
3131   typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3132
3133   static typename elfcpp::Elf_types<32>::Elf_Swxword
3134   get_r_addend(const Reloc* reloc)
3135   { return reloc->get_r_addend(); }
3136
3137   static inline void
3138   set_reloc_addend(Reloc_write* p,
3139                    typename elfcpp::Elf_types<32>::Elf_Swxword val)
3140   { p->put_r_addend(val); }
3141 };
3142
3143 template<bool big_endian>
3144 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3145 {
3146   typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3147   typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3148
3149   static typename elfcpp::Elf_types<64>::Elf_Swxword
3150   get_r_addend(const Reloc*)
3151   { return 0; }
3152
3153   static inline void
3154   set_reloc_addend(Reloc_write*,
3155                    typename elfcpp::Elf_types<64>::Elf_Swxword)
3156   { gold_unreachable(); }
3157 };
3158
3159 template<bool big_endian>
3160 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3161 {
3162   typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3163   typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3164
3165   static typename elfcpp::Elf_types<64>::Elf_Swxword
3166   get_r_addend(const Reloc* reloc)
3167   { return reloc->get_r_addend(); }
3168
3169   static inline void
3170   set_reloc_addend(Reloc_write* p,
3171                    typename elfcpp::Elf_types<64>::Elf_Swxword val)
3172   { p->put_r_addend(val); }
3173 };
3174
3175 // Forward declaration.
3176 static unsigned int
3177 mips_get_size_for_reloc(unsigned int, Relobj*);
3178
3179 // A class for inquiring about properties of a relocation,
3180 // used while scanning relocs during a relocatable link and
3181 // garbage collection.
3182
3183 template<int sh_type_, int size, bool big_endian>
3184 class Mips_classify_reloc;
3185
3186 template<int sh_type_, bool big_endian>
3187 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3188     public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3189 {
3190  public:
3191   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3192       Reltype;
3193   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3194       Reltype_write;
3195
3196   // Return the symbol referred to by the relocation.
3197   static inline unsigned int
3198   get_r_sym(const Reltype* reloc)
3199   { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3200
3201   // Return the type of the relocation.
3202   static inline unsigned int
3203   get_r_type(const Reltype* reloc)
3204   { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3205
3206   static inline unsigned int
3207   get_r_type2(const Reltype*)
3208   { return 0; }
3209
3210   static inline unsigned int
3211   get_r_type3(const Reltype*)
3212   { return 0; }
3213
3214   static inline unsigned int
3215   get_r_ssym(const Reltype*)
3216   { return 0; }
3217
3218   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3219   static inline unsigned int
3220   get_r_addend(const Reltype* reloc)
3221   {
3222     if (sh_type_ == elfcpp::SHT_REL)
3223       return 0;
3224     return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3225   }
3226
3227   // Write the r_info field to a new reloc, using the r_info field from
3228   // the original reloc, replacing the r_sym field with R_SYM.
3229   static inline void
3230   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3231   {
3232     unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3233     new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3234   }
3235
3236   // Write the r_addend field to a new reloc.
3237   static inline void
3238   put_r_addend(Reltype_write* to,
3239                typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3240   { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3241
3242   // Return the size of the addend of the relocation (only used for SHT_REL).
3243   static unsigned int
3244   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3245   { return mips_get_size_for_reloc(r_type, obj); }
3246 };
3247
3248 template<int sh_type_, bool big_endian>
3249 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3250     public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3251 {
3252  public:
3253   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3254       Reltype;
3255   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3256       Reltype_write;
3257
3258   // Return the symbol referred to by the relocation.
3259   static inline unsigned int
3260   get_r_sym(const Reltype* reloc)
3261   { return reloc->get_r_sym(); }
3262
3263   // Return the r_type of the relocation.
3264   static inline unsigned int
3265   get_r_type(const Reltype* reloc)
3266   { return reloc->get_r_type(); }
3267
3268   // Return the r_type2 of the relocation.
3269   static inline unsigned int
3270   get_r_type2(const Reltype* reloc)
3271   { return reloc->get_r_type2(); }
3272
3273   // Return the r_type3 of the relocation.
3274   static inline unsigned int
3275   get_r_type3(const Reltype* reloc)
3276   { return reloc->get_r_type3(); }
3277
3278   // Return the special symbol of the relocation.
3279   static inline unsigned int
3280   get_r_ssym(const Reltype* reloc)
3281   { return reloc->get_r_ssym(); }
3282
3283   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3284   static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3285   get_r_addend(const Reltype* reloc)
3286   {
3287     if (sh_type_ == elfcpp::SHT_REL)
3288       return 0;
3289     return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3290   }
3291
3292   // Write the r_info field to a new reloc, using the r_info field from
3293   // the original reloc, replacing the r_sym field with R_SYM.
3294   static inline void
3295   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3296   {
3297     new_reloc->put_r_sym(r_sym);
3298     new_reloc->put_r_ssym(reloc->get_r_ssym());
3299     new_reloc->put_r_type3(reloc->get_r_type3());
3300     new_reloc->put_r_type2(reloc->get_r_type2());
3301     new_reloc->put_r_type(reloc->get_r_type());
3302   }
3303
3304   // Write the r_addend field to a new reloc.
3305   static inline void
3306   put_r_addend(Reltype_write* to,
3307                typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3308   { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3309
3310   // Return the size of the addend of the relocation (only used for SHT_REL).
3311   static unsigned int
3312   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3313   { return mips_get_size_for_reloc(r_type, obj); }
3314 };
3315
3316 template<int size, bool big_endian>
3317 class Target_mips : public Sized_target<size, big_endian>
3318 {
3319   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3320   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3321     Reloc_section;
3322   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3323   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3324   typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3325       Reltype;
3326   typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3327       Relatype;
3328
3329  public:
3330   Target_mips(const Target::Target_info* info = &mips_info)
3331     : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3332       got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3333       dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3334       mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3335       mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
3336       entry_symbol_is_compressed_(false), insn32_(false)
3337   {
3338     this->add_machine_extensions();
3339   }
3340
3341   // The offset of $gp from the beginning of the .got section.
3342   static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3343
3344   // The maximum size of the GOT for it to be addressable using 16-bit
3345   // offsets from $gp.
3346   static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3347
3348   // Make a new symbol table entry for the Mips target.
3349   Sized_symbol<size>*
3350   make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3351   { return new Mips_symbol<size>(); }
3352
3353   // Process the relocations to determine unreferenced sections for
3354   // garbage collection.
3355   void
3356   gc_process_relocs(Symbol_table* symtab,
3357                     Layout* layout,
3358                     Sized_relobj_file<size, big_endian>* object,
3359                     unsigned int data_shndx,
3360                     unsigned int sh_type,
3361                     const unsigned char* prelocs,
3362                     size_t reloc_count,
3363                     Output_section* output_section,
3364                     bool needs_special_offset_handling,
3365                     size_t local_symbol_count,
3366                     const unsigned char* plocal_symbols);
3367
3368   // Scan the relocations to look for symbol adjustments.
3369   void
3370   scan_relocs(Symbol_table* symtab,
3371               Layout* layout,
3372               Sized_relobj_file<size, big_endian>* object,
3373               unsigned int data_shndx,
3374               unsigned int sh_type,
3375               const unsigned char* prelocs,
3376               size_t reloc_count,
3377               Output_section* output_section,
3378               bool needs_special_offset_handling,
3379               size_t local_symbol_count,
3380               const unsigned char* plocal_symbols);
3381
3382   // Finalize the sections.
3383   void
3384   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3385
3386   // Relocate a section.
3387   void
3388   relocate_section(const Relocate_info<size, big_endian>*,
3389                    unsigned int sh_type,
3390                    const unsigned char* prelocs,
3391                    size_t reloc_count,
3392                    Output_section* output_section,
3393                    bool needs_special_offset_handling,
3394                    unsigned char* view,
3395                    Mips_address view_address,
3396                    section_size_type view_size,
3397                    const Reloc_symbol_changes*);
3398
3399   // Scan the relocs during a relocatable link.
3400   void
3401   scan_relocatable_relocs(Symbol_table* symtab,
3402                           Layout* layout,
3403                           Sized_relobj_file<size, big_endian>* object,
3404                           unsigned int data_shndx,
3405                           unsigned int sh_type,
3406                           const unsigned char* prelocs,
3407                           size_t reloc_count,
3408                           Output_section* output_section,
3409                           bool needs_special_offset_handling,
3410                           size_t local_symbol_count,
3411                           const unsigned char* plocal_symbols,
3412                           Relocatable_relocs*);
3413
3414   // Scan the relocs for --emit-relocs.
3415   void
3416   emit_relocs_scan(Symbol_table* symtab,
3417                    Layout* layout,
3418                    Sized_relobj_file<size, big_endian>* object,
3419                    unsigned int data_shndx,
3420                    unsigned int sh_type,
3421                    const unsigned char* prelocs,
3422                    size_t reloc_count,
3423                    Output_section* output_section,
3424                    bool needs_special_offset_handling,
3425                    size_t local_symbol_count,
3426                    const unsigned char* plocal_syms,
3427                    Relocatable_relocs* rr);
3428
3429   // Emit relocations for a section.
3430   void
3431   relocate_relocs(const Relocate_info<size, big_endian>*,
3432                   unsigned int sh_type,
3433                   const unsigned char* prelocs,
3434                   size_t reloc_count,
3435                   Output_section* output_section,
3436                   typename elfcpp::Elf_types<size>::Elf_Off
3437                     offset_in_output_section,
3438                   unsigned char* view,
3439                   Mips_address view_address,
3440                   section_size_type view_size,
3441                   unsigned char* reloc_view,
3442                   section_size_type reloc_view_size);
3443
3444   // Perform target-specific processing in a relocatable link.  This is
3445   // only used if we use the relocation strategy RELOC_SPECIAL.
3446   void
3447   relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3448                                unsigned int sh_type,
3449                                const unsigned char* preloc_in,
3450                                size_t relnum,
3451                                Output_section* output_section,
3452                                typename elfcpp::Elf_types<size>::Elf_Off
3453                                  offset_in_output_section,
3454                                unsigned char* view,
3455                                Mips_address view_address,
3456                                section_size_type view_size,
3457                                unsigned char* preloc_out);
3458
3459   // Return whether SYM is defined by the ABI.
3460   bool
3461   do_is_defined_by_abi(const Symbol* sym) const
3462   {
3463     return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3464             || (strcmp(sym->name(), "_gp_disp") == 0)
3465             || (strcmp(sym->name(), "___tls_get_addr") == 0));
3466   }
3467
3468   // Return the number of entries in the GOT.
3469   unsigned int
3470   got_entry_count() const
3471   {
3472     if (!this->has_got_section())
3473       return 0;
3474     return this->got_size() / (size/8);
3475   }
3476
3477   // Return the number of entries in the PLT.
3478   unsigned int
3479   plt_entry_count() const
3480   {
3481     if (this->plt_ == NULL)
3482       return 0;
3483     return this->plt_->entry_count();
3484   }
3485
3486   // Return the offset of the first non-reserved PLT entry.
3487   unsigned int
3488   first_plt_entry_offset() const
3489   { return this->plt_->first_plt_entry_offset(); }
3490
3491   // Return the size of each PLT entry.
3492   unsigned int
3493   plt_entry_size() const
3494   { return this->plt_->plt_entry_size(); }
3495
3496   // Get the GOT section, creating it if necessary.
3497   Mips_output_data_got<size, big_endian>*
3498   got_section(Symbol_table*, Layout*);
3499
3500   // Get the GOT section.
3501   Mips_output_data_got<size, big_endian>*
3502   got_section() const
3503   {
3504     gold_assert(this->got_ != NULL);
3505     return this->got_;
3506   }
3507
3508   // Get the .MIPS.stubs section, creating it if necessary.
3509   Mips_output_data_mips_stubs<size, big_endian>*
3510   mips_stubs_section(Layout* layout);
3511
3512   // Get the .MIPS.stubs section.
3513   Mips_output_data_mips_stubs<size, big_endian>*
3514   mips_stubs_section() const
3515   {
3516     gold_assert(this->mips_stubs_ != NULL);
3517     return this->mips_stubs_;
3518   }
3519
3520   // Get the LA25 stub section, creating it if necessary.
3521   Mips_output_data_la25_stub<size, big_endian>*
3522   la25_stub_section(Layout*);
3523
3524   // Get the LA25 stub section.
3525   Mips_output_data_la25_stub<size, big_endian>*
3526   la25_stub_section()
3527   {
3528     gold_assert(this->la25_stub_ != NULL);
3529     return this->la25_stub_;
3530   }
3531
3532   // Get gp value.  It has the value of .got + 0x7FF0.
3533   Mips_address
3534   gp_value() const
3535   {
3536     if (this->gp_ != NULL)
3537       return this->gp_->value();
3538     return 0;
3539   }
3540
3541   // Get gp value.  It has the value of .got + 0x7FF0.  Adjust it for
3542   // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3543   Mips_address
3544   adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3545   {
3546     if (this->gp_ == NULL)
3547       return 0;
3548
3549     bool multi_got = false;
3550     if (this->has_got_section())
3551       multi_got = this->got_section()->multi_got();
3552     if (!multi_got)
3553       return this->gp_->value();
3554     else
3555       return this->gp_->value() + this->got_section()->get_got_offset(object);
3556   }
3557
3558   // Get the dynamic reloc section, creating it if necessary.
3559   Reloc_section*
3560   rel_dyn_section(Layout*);
3561
3562   bool
3563   do_has_custom_set_dynsym_indexes() const
3564   { return true; }
3565
3566   // Don't emit input .reginfo/.MIPS.abiflags sections to
3567   // output .reginfo/.MIPS.abiflags.
3568   bool
3569   do_should_include_section(elfcpp::Elf_Word sh_type) const
3570   {
3571     return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3572              && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3573   }
3574
3575   // Set the dynamic symbol indexes.  INDEX is the index of the first
3576   // global dynamic symbol.  Pointers to the symbols are stored into the
3577   // vector SYMS.  The names are added to DYNPOOL.  This returns an
3578   // updated dynamic symbol index.
3579   unsigned int
3580   do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3581                         std::vector<Symbol*>* syms, Stringpool* dynpool,
3582                         Versions* versions, Symbol_table* symtab) const;
3583
3584   // Remove .MIPS.stubs entry for a symbol.
3585   void
3586   remove_lazy_stub_entry(Mips_symbol<size>* sym)
3587   {
3588     if (this->mips_stubs_ != NULL)
3589       this->mips_stubs_->remove_entry(sym);
3590   }
3591
3592   // The value to write into got[1] for SVR4 targets, to identify it is
3593   // a GNU object.  The dynamic linker can then use got[1] to store the
3594   // module pointer.
3595   uint64_t
3596   mips_elf_gnu_got1_mask()
3597   {
3598     if (this->is_output_n64())
3599       return (uint64_t)1 << 63;
3600     else
3601       return 1 << 31;
3602   }
3603
3604   // Whether the output has microMIPS code.  This is valid only after
3605   // merge_obj_e_flags() is called.
3606   bool
3607   is_output_micromips() const
3608   {
3609     gold_assert(this->are_processor_specific_flags_set());
3610     return elfcpp::is_micromips(this->processor_specific_flags());
3611   }
3612
3613   // Whether the output uses N32 ABI.  This is valid only after
3614   // merge_obj_e_flags() is called.
3615   bool
3616   is_output_n32() const
3617   {
3618     gold_assert(this->are_processor_specific_flags_set());
3619     return elfcpp::abi_n32(this->processor_specific_flags());
3620   }
3621
3622   // Whether the output uses R6 ISA.  This is valid only after
3623   // merge_obj_e_flags() is called.
3624   bool
3625   is_output_r6() const
3626   {
3627     gold_assert(this->are_processor_specific_flags_set());
3628     return elfcpp::r6_isa(this->processor_specific_flags());
3629   }
3630
3631   // Whether the output uses N64 ABI.
3632   bool
3633   is_output_n64() const
3634   { return size == 64; }
3635
3636   // Whether the output uses NEWABI.  This is valid only after
3637   // merge_obj_e_flags() is called.
3638   bool
3639   is_output_newabi() const
3640   { return this->is_output_n32() || this->is_output_n64(); }
3641
3642   // Whether we can only use 32-bit microMIPS instructions.
3643   bool
3644   use_32bit_micromips_instructions() const
3645   { return this->insn32_; }
3646
3647   // Return the r_sym field from a relocation.
3648   unsigned int
3649   get_r_sym(const unsigned char* preloc) const
3650   {
3651     // Since REL and RELA relocs share the same structure through
3652     // the r_info field, we can just use REL here.
3653     Reltype rel(preloc);
3654     return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3655         get_r_sym(&rel);
3656   }
3657
3658  protected:
3659   // Return the value to use for a dynamic symbol which requires special
3660   // treatment.  This is how we support equality comparisons of function
3661   // pointers across shared library boundaries, as described in the
3662   // processor specific ABI supplement.
3663   uint64_t
3664   do_dynsym_value(const Symbol* gsym) const;
3665
3666   // Make an ELF object.
3667   Object*
3668   do_make_elf_object(const std::string&, Input_file*, off_t,
3669                      const elfcpp::Ehdr<size, big_endian>& ehdr);
3670
3671   Object*
3672   do_make_elf_object(const std::string&, Input_file*, off_t,
3673                      const elfcpp::Ehdr<size, !big_endian>&)
3674   { gold_unreachable(); }
3675
3676   // Make an output section.
3677   Output_section*
3678   do_make_output_section(const char* name, elfcpp::Elf_Word type,
3679                          elfcpp::Elf_Xword flags)
3680     {
3681       if (type == elfcpp::SHT_MIPS_OPTIONS)
3682         return new Mips_output_section_options<size, big_endian>(name, type,
3683                                                                  flags, this);
3684       else
3685         return new Output_section(name, type, flags);
3686     }
3687
3688   // Adjust ELF file header.
3689   void
3690   do_adjust_elf_header(unsigned char* view, int len);
3691
3692   // Get the custom dynamic tag value.
3693   unsigned int
3694   do_dynamic_tag_custom_value(elfcpp::DT) const;
3695
3696   // Adjust the value written to the dynamic symbol table.
3697   virtual void
3698   do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3699   {
3700     elfcpp::Sym<size, big_endian> isym(view);
3701     elfcpp::Sym_write<size, big_endian> osym(view);
3702     const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3703
3704     // Keep dynamic compressed symbols odd.  This allows the dynamic linker
3705     // to treat compressed symbols like any other.
3706     Mips_address value = isym.get_st_value();
3707     if (mips_sym->is_mips16() && value != 0)
3708       {
3709         if (!mips_sym->has_mips16_fn_stub())
3710           value |= 1;
3711         else
3712           {
3713             // If we have a MIPS16 function with a stub, the dynamic symbol
3714             // must refer to the stub, since only the stub uses the standard
3715             // calling conventions.  Stub contains MIPS32 code, so don't add +1
3716             // in this case.
3717
3718             // There is a code which does this in the method
3719             // Target_mips::do_dynsym_value, but that code will only be
3720             // executed if the symbol is from dynobj.
3721             // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3722             // table.
3723
3724             Mips16_stub_section<size, big_endian>* fn_stub =
3725               mips_sym->template get_mips16_fn_stub<big_endian>();
3726             value = fn_stub->output_address();
3727             osym.put_st_size(fn_stub->section_size());
3728           }
3729
3730         osym.put_st_value(value);
3731         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3732                           mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3733       }
3734     else if ((mips_sym->is_micromips()
3735               // Stubs are always microMIPS if there is any microMIPS code in
3736               // the output.
3737               || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3738              && value != 0)
3739       {
3740         osym.put_st_value(value | 1);
3741         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3742                           mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3743       }
3744   }
3745
3746  private:
3747   // The class which scans relocations.
3748   class Scan
3749   {
3750    public:
3751     Scan()
3752     { }
3753
3754     static inline int
3755     get_reference_flags(unsigned int r_type);
3756
3757     inline void
3758     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3759           Sized_relobj_file<size, big_endian>* object,
3760           unsigned int data_shndx,
3761           Output_section* output_section,
3762           const Reltype& reloc, unsigned int r_type,
3763           const elfcpp::Sym<size, big_endian>& lsym,
3764           bool is_discarded);
3765
3766     inline void
3767     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3768           Sized_relobj_file<size, big_endian>* object,
3769           unsigned int data_shndx,
3770           Output_section* output_section,
3771           const Relatype& reloc, unsigned int r_type,
3772           const elfcpp::Sym<size, big_endian>& lsym,
3773           bool is_discarded);
3774
3775     inline void
3776     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3777           Sized_relobj_file<size, big_endian>* object,
3778           unsigned int data_shndx,
3779           Output_section* output_section,
3780           const Relatype* rela,
3781           const Reltype* rel,
3782           unsigned int rel_type,
3783           unsigned int r_type,
3784           const elfcpp::Sym<size, big_endian>& lsym,
3785           bool is_discarded);
3786
3787     inline void
3788     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3789            Sized_relobj_file<size, big_endian>* object,
3790            unsigned int data_shndx,
3791            Output_section* output_section,
3792            const Reltype& reloc, unsigned int r_type,
3793            Symbol* gsym);
3794
3795     inline void
3796     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3797            Sized_relobj_file<size, big_endian>* object,
3798            unsigned int data_shndx,
3799            Output_section* output_section,
3800            const Relatype& reloc, unsigned int r_type,
3801            Symbol* gsym);
3802
3803     inline void
3804     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3805            Sized_relobj_file<size, big_endian>* object,
3806            unsigned int data_shndx,
3807            Output_section* output_section,
3808            const Relatype* rela,
3809            const Reltype* rel,
3810            unsigned int rel_type,
3811            unsigned int r_type,
3812            Symbol* gsym);
3813
3814     inline bool
3815     local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3816                                         Target_mips*,
3817                                         Sized_relobj_file<size, big_endian>*,
3818                                         unsigned int,
3819                                         Output_section*,
3820                                         const Reltype&,
3821                                         unsigned int,
3822                                         const elfcpp::Sym<size, big_endian>&)
3823     { return false; }
3824
3825     inline bool
3826     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3827                                          Target_mips*,
3828                                          Sized_relobj_file<size, big_endian>*,
3829                                          unsigned int,
3830                                          Output_section*,
3831                                          const Reltype&,
3832                                          unsigned int, Symbol*)
3833     { return false; }
3834
3835     inline bool
3836     local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3837                                         Target_mips*,
3838                                         Sized_relobj_file<size, big_endian>*,
3839                                         unsigned int,
3840                                         Output_section*,
3841                                         const Relatype&,
3842                                         unsigned int,
3843                                         const elfcpp::Sym<size, big_endian>&)
3844     { return false; }
3845
3846     inline bool
3847     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3848                                          Target_mips*,
3849                                          Sized_relobj_file<size, big_endian>*,
3850                                          unsigned int,
3851                                          Output_section*,
3852                                          const Relatype&,
3853                                          unsigned int, Symbol*)
3854     { return false; }
3855    private:
3856     static void
3857     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3858                             unsigned int r_type);
3859
3860     static void
3861     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3862                              unsigned int r_type, Symbol*);
3863   };
3864
3865   // The class which implements relocation.
3866   class Relocate
3867   {
3868    public:
3869     Relocate()
3870       : calculated_value_(0), calculate_only_(false)
3871     { }
3872
3873     ~Relocate()
3874     { }
3875
3876     // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3877     inline bool
3878     should_apply_static_reloc(const Mips_symbol<size>* gsym,
3879                               unsigned int r_type,
3880                               Output_section* output_section,
3881                               Target_mips* target);
3882
3883     // Do a relocation.  Return false if the caller should not issue
3884     // any warnings about this relocation.
3885     inline bool
3886     relocate(const Relocate_info<size, big_endian>*, unsigned int,
3887              Target_mips*, Output_section*, size_t, const unsigned char*,
3888              const Sized_symbol<size>*, const Symbol_value<size>*,
3889              unsigned char*, Mips_address, section_size_type);
3890
3891    private:
3892     // Result of the relocation.
3893     Valtype calculated_value_;
3894     // Whether we have to calculate relocation instead of applying it.
3895     bool calculate_only_;
3896   };
3897
3898   // This POD class holds the dynamic relocations that should be emitted instead
3899   // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations.  We will emit these
3900   // relocations if it turns out that the symbol does not have static
3901   // relocations.
3902   class Dyn_reloc
3903   {
3904    public:
3905     Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3906               Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3907               Output_section* output_section, Mips_address r_offset)
3908       : sym_(sym), r_type_(r_type), relobj_(relobj),
3909         shndx_(shndx), output_section_(output_section),
3910         r_offset_(r_offset)
3911     { }
3912
3913     // Emit this reloc if appropriate.  This is called after we have
3914     // scanned all the relocations, so we know whether the symbol has
3915     // static relocations.
3916     void
3917     emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3918          Symbol_table* symtab)
3919     {
3920       if (!this->sym_->has_static_relocs())
3921         {
3922           got->record_global_got_symbol(this->sym_, this->relobj_,
3923                                         this->r_type_, true, false);
3924           if (!symbol_references_local(this->sym_,
3925                                 this->sym_->should_add_dynsym_entry(symtab)))
3926             rel_dyn->add_global(this->sym_, this->r_type_,
3927                                 this->output_section_, this->relobj_,
3928                                 this->shndx_, this->r_offset_);
3929           else
3930             rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3931                                           this->output_section_, this->relobj_,
3932                                           this->shndx_, this->r_offset_);
3933         }
3934     }
3935
3936    private:
3937     Mips_symbol<size>* sym_;
3938     unsigned int r_type_;
3939     Mips_relobj<size, big_endian>* relobj_;
3940     unsigned int shndx_;
3941     Output_section* output_section_;
3942     Mips_address r_offset_;
3943   };
3944
3945   // Adjust TLS relocation type based on the options and whether this
3946   // is a local symbol.
3947   static tls::Tls_optimization
3948   optimize_tls_reloc(bool is_final, int r_type);
3949
3950   // Return whether there is a GOT section.
3951   bool
3952   has_got_section() const
3953   { return this->got_ != NULL; }
3954
3955   // Check whether the given ELF header flags describe a 32-bit binary.
3956   bool
3957   mips_32bit_flags(elfcpp::Elf_Word);
3958
3959   enum Mips_mach {
3960     mach_mips3000             = 3000,
3961     mach_mips3900             = 3900,
3962     mach_mips4000             = 4000,
3963     mach_mips4010             = 4010,
3964     mach_mips4100             = 4100,
3965     mach_mips4111             = 4111,
3966     mach_mips4120             = 4120,
3967     mach_mips4300             = 4300,
3968     mach_mips4400             = 4400,
3969     mach_mips4600             = 4600,
3970     mach_mips4650             = 4650,
3971     mach_mips5000             = 5000,
3972     mach_mips5400             = 5400,
3973     mach_mips5500             = 5500,
3974     mach_mips5900             = 5900,
3975     mach_mips6000             = 6000,
3976     mach_mips7000             = 7000,
3977     mach_mips8000             = 8000,
3978     mach_mips9000             = 9000,
3979     mach_mips10000            = 10000,
3980     mach_mips12000            = 12000,
3981     mach_mips14000            = 14000,
3982     mach_mips16000            = 16000,
3983     mach_mips16               = 16,
3984     mach_mips5                = 5,
3985     mach_mips_loongson_2e     = 3001,
3986     mach_mips_loongson_2f     = 3002,
3987     mach_mips_loongson_3a     = 3003,
3988     mach_mips_sb1             = 12310201, // octal 'SB', 01
3989     mach_mips_octeon          = 6501,
3990     mach_mips_octeonp         = 6601,
3991     mach_mips_octeon2         = 6502,
3992     mach_mips_octeon3         = 6503,
3993     mach_mips_xlr             = 887682,   // decimal 'XLR'
3994     mach_mipsisa32            = 32,
3995     mach_mipsisa32r2          = 33,
3996     mach_mipsisa32r3          = 34,
3997     mach_mipsisa32r5          = 36,
3998     mach_mipsisa32r6          = 37,
3999     mach_mipsisa64            = 64,
4000     mach_mipsisa64r2          = 65,
4001     mach_mipsisa64r3          = 66,
4002     mach_mipsisa64r5          = 68,
4003     mach_mipsisa64r6          = 69,
4004     mach_mips_micromips       = 96
4005   };
4006
4007   // Return the MACH for a MIPS e_flags value.
4008   unsigned int
4009   elf_mips_mach(elfcpp::Elf_Word);
4010
4011   // Return the MACH for each .MIPS.abiflags ISA Extension.
4012   unsigned int
4013   mips_isa_ext_mach(unsigned int);
4014
4015   // Return the .MIPS.abiflags value representing each ISA Extension.
4016   unsigned int
4017   mips_isa_ext(unsigned int);
4018
4019   // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4020   void
4021   update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4022                       Mips_abiflags<big_endian>*);
4023
4024   // Infer the content of the ABI flags based on the elf header.
4025   void
4026   infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4027
4028   // Create abiflags from elf header or from .MIPS.abiflags section.
4029   void
4030   create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4031
4032   // Return the meaning of fp_abi, or "unknown" if not known.
4033   const char*
4034   fp_abi_string(int);
4035
4036   // Select fp_abi.
4037   int
4038   select_fp_abi(const std::string&, int, int);
4039
4040   // Merge attributes from input object.
4041   void
4042   merge_obj_attributes(const std::string&, const Attributes_section_data*);
4043
4044   // Merge abiflags from input object.
4045   void
4046   merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4047
4048   // Check whether machine EXTENSION is an extension of machine BASE.
4049   bool
4050   mips_mach_extends(unsigned int, unsigned int);
4051
4052   // Merge file header flags from input object.
4053   void
4054   merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4055
4056   // Encode ISA level and revision as a single value.
4057   int
4058   level_rev(unsigned char isa_level, unsigned char isa_rev) const
4059   { return (isa_level << 3) | isa_rev; }
4060
4061   // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4062   static inline bool
4063   jal_to_bal()
4064   { return false; }
4065
4066   // True if we are linking for CPUs that are faster if JALR is converted to
4067   // BAL.  This should be safe for all architectures.  We enable this predicate
4068   // for all CPUs.
4069   static inline bool
4070   jalr_to_bal()
4071   { return true; }
4072
4073   // True if we are linking for CPUs that are faster if JR is converted to B.
4074   // This should be safe for all architectures.  We enable this predicate for
4075   // all CPUs.
4076   static inline bool
4077   jr_to_b()
4078   { return true; }
4079
4080   // Return the size of the GOT section.
4081   section_size_type
4082   got_size() const
4083   {
4084     gold_assert(this->got_ != NULL);
4085     return this->got_->data_size();
4086   }
4087
4088   // Create a PLT entry for a global symbol referenced by r_type relocation.
4089   void
4090   make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4091                  unsigned int r_type);
4092
4093   // Get the PLT section.
4094   Mips_output_data_plt<size, big_endian>*
4095   plt_section() const
4096   {
4097     gold_assert(this->plt_ != NULL);
4098     return this->plt_;
4099   }
4100
4101   // Get the GOT PLT section.
4102   const Mips_output_data_plt<size, big_endian>*
4103   got_plt_section() const
4104   {
4105     gold_assert(this->got_plt_ != NULL);
4106     return this->got_plt_;
4107   }
4108
4109   // Copy a relocation against a global symbol.
4110   void
4111   copy_reloc(Symbol_table* symtab, Layout* layout,
4112              Sized_relobj_file<size, big_endian>* object,
4113              unsigned int shndx, Output_section* output_section,
4114              Symbol* sym, unsigned int r_type, Mips_address r_offset)
4115   {
4116     this->copy_relocs_.copy_reloc(symtab, layout,
4117                                   symtab->get_sized_symbol<size>(sym),
4118                                   object, shndx, output_section,
4119                                   r_type, r_offset, 0,
4120                                   this->rel_dyn_section(layout));
4121   }
4122
4123   void
4124   dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4125                 Mips_relobj<size, big_endian>* relobj,
4126                 unsigned int shndx, Output_section* output_section,
4127                 Mips_address r_offset)
4128   {
4129     this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4130                                           output_section, r_offset));
4131   }
4132
4133   // Calculate value of _gp symbol.
4134   void
4135   set_gp(Layout*, Symbol_table*);
4136
4137   const char*
4138   elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4139   const char*
4140   elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4141
4142   // Adds entries that describe how machines relate to one another.  The entries
4143   // are ordered topologically with MIPS I extensions listed last.  First
4144   // element is extension, second element is base.
4145   void
4146   add_machine_extensions()
4147   {
4148     // MIPS64r2 extensions.
4149     this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4150     this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4151     this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4152     this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4153     this->add_extension(mach_mips_loongson_3a, mach_mipsisa64r2);
4154
4155     // MIPS64 extensions.
4156     this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4157     this->add_extension(mach_mips_sb1, mach_mipsisa64);
4158     this->add_extension(mach_mips_xlr, mach_mipsisa64);
4159
4160     // MIPS V extensions.
4161     this->add_extension(mach_mipsisa64, mach_mips5);
4162
4163     // R10000 extensions.
4164     this->add_extension(mach_mips12000, mach_mips10000);
4165     this->add_extension(mach_mips14000, mach_mips10000);
4166     this->add_extension(mach_mips16000, mach_mips10000);
4167
4168     // R5000 extensions.  Note: the vr5500 ISA is an extension of the core
4169     // vr5400 ISA, but doesn't include the multimedia stuff.  It seems
4170     // better to allow vr5400 and vr5500 code to be merged anyway, since
4171     // many libraries will just use the core ISA.  Perhaps we could add
4172     // some sort of ASE flag if this ever proves a problem.
4173     this->add_extension(mach_mips5500, mach_mips5400);
4174     this->add_extension(mach_mips5400, mach_mips5000);
4175
4176     // MIPS IV extensions.
4177     this->add_extension(mach_mips5, mach_mips8000);
4178     this->add_extension(mach_mips10000, mach_mips8000);
4179     this->add_extension(mach_mips5000, mach_mips8000);
4180     this->add_extension(mach_mips7000, mach_mips8000);
4181     this->add_extension(mach_mips9000, mach_mips8000);
4182
4183     // VR4100 extensions.
4184     this->add_extension(mach_mips4120, mach_mips4100);
4185     this->add_extension(mach_mips4111, mach_mips4100);
4186
4187     // MIPS III extensions.
4188     this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4189     this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4190     this->add_extension(mach_mips8000, mach_mips4000);
4191     this->add_extension(mach_mips4650, mach_mips4000);
4192     this->add_extension(mach_mips4600, mach_mips4000);
4193     this->add_extension(mach_mips4400, mach_mips4000);
4194     this->add_extension(mach_mips4300, mach_mips4000);
4195     this->add_extension(mach_mips4100, mach_mips4000);
4196     this->add_extension(mach_mips4010, mach_mips4000);
4197     this->add_extension(mach_mips5900, mach_mips4000);
4198
4199     // MIPS32 extensions.
4200     this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4201
4202     // MIPS II extensions.
4203     this->add_extension(mach_mips4000, mach_mips6000);
4204     this->add_extension(mach_mipsisa32, mach_mips6000);
4205
4206     // MIPS I extensions.
4207     this->add_extension(mach_mips6000, mach_mips3000);
4208     this->add_extension(mach_mips3900, mach_mips3000);
4209   }
4210
4211   // Add value to MIPS extenstions.
4212   void
4213   add_extension(unsigned int base, unsigned int extension)
4214   {
4215     std::pair<unsigned int, unsigned int> ext(base, extension);
4216     this->mips_mach_extensions_.push_back(ext);
4217   }
4218
4219   // Return the number of entries in the .dynsym section.
4220   unsigned int get_dt_mips_symtabno() const
4221   {
4222     return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4223                            / elfcpp::Elf_sizes<size>::sym_size));
4224     // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4225   }
4226
4227   // Information about this specific target which we pass to the
4228   // general Target structure.
4229   static const Target::Target_info mips_info;
4230   // The GOT section.
4231   Mips_output_data_got<size, big_endian>* got_;
4232   // gp symbol.  It has the value of .got + 0x7FF0.
4233   Sized_symbol<size>* gp_;
4234   // The PLT section.
4235   Mips_output_data_plt<size, big_endian>* plt_;
4236   // The GOT PLT section.
4237   Output_data_space* got_plt_;
4238   // The dynamic reloc section.
4239   Reloc_section* rel_dyn_;
4240   // The .rld_map section.
4241   Output_data_zero_fill* rld_map_;
4242   // Relocs saved to avoid a COPY reloc.
4243   Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4244
4245   // A list of dyn relocs to be saved.
4246   std::vector<Dyn_reloc> dyn_relocs_;
4247
4248   // The LA25 stub section.
4249   Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4250   // Architecture extensions.
4251   std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4252   // .MIPS.stubs
4253   Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4254
4255   // Attributes section data in output.
4256   Attributes_section_data* attributes_section_data_;
4257   // .MIPS.abiflags section data in output.
4258   Mips_abiflags<big_endian>* abiflags_;
4259
4260   unsigned int mach_;
4261   Layout* layout_;
4262
4263   typename std::list<got16_addend<size, big_endian> > got16_addends_;
4264
4265   // Whether there is an input .MIPS.abiflags section.
4266   bool has_abiflags_section_;
4267
4268   // Whether the entry symbol is mips16 or micromips.
4269   bool entry_symbol_is_compressed_;
4270
4271   // Whether we can use only 32-bit microMIPS instructions.
4272   // TODO(sasa): This should be a linker option.
4273   bool insn32_;
4274 };
4275
4276 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4277 // It records high part of the relocation pair.
4278
4279 template<int size, bool big_endian>
4280 struct reloc_high
4281 {
4282   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4283
4284   reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4285              const Symbol_value<size>* _psymval, Mips_address _addend,
4286              unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4287              Mips_address _address = 0, bool _gp_disp = false)
4288     : view(_view), object(_object), psymval(_psymval), addend(_addend),
4289       r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4290       address(_address), gp_disp(_gp_disp)
4291   { }
4292
4293   unsigned char* view;
4294   const Mips_relobj<size, big_endian>* object;
4295   const Symbol_value<size>* psymval;
4296   Mips_address addend;
4297   unsigned int r_type;
4298   unsigned int r_sym;
4299   bool extract_addend;
4300   Mips_address address;
4301   bool gp_disp;
4302 };
4303
4304 template<int size, bool big_endian>
4305 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4306 {
4307   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4308   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4309   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4310   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4311   typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4312
4313  public:
4314   typedef enum
4315   {
4316     STATUS_OKAY,            // No error during relocation.
4317     STATUS_OVERFLOW,        // Relocation overflow.
4318     STATUS_BAD_RELOC,       // Relocation cannot be applied.
4319     STATUS_PCREL_UNALIGNED  // Unaligned PC-relative relocation.
4320   } Status;
4321
4322  private:
4323   typedef Relocate_functions<size, big_endian> Base;
4324   typedef Mips_relocate_functions<size, big_endian> This;
4325
4326   static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4327   static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4328   static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
4329
4330   template<int valsize>
4331   static inline typename This::Status
4332   check_overflow(Valtype value)
4333   {
4334     if (size == 32)
4335       return (Bits<valsize>::has_overflow32(value)
4336               ? This::STATUS_OVERFLOW
4337               : This::STATUS_OKAY);
4338
4339     return (Bits<valsize>::has_overflow(value)
4340             ? This::STATUS_OVERFLOW
4341             : This::STATUS_OKAY);
4342   }
4343
4344   static inline bool
4345   should_shuffle_micromips_reloc(unsigned int r_type)
4346   {
4347     return (micromips_reloc(r_type)
4348             && r_type != elfcpp::R_MICROMIPS_PC7_S1
4349             && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4350   }
4351
4352  public:
4353   //   R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4354   //   Most mips16 instructions are 16 bits, but these instructions
4355   //   are 32 bits.
4356   //
4357   //   The format of these instructions is:
4358   //
4359   //   +--------------+--------------------------------+
4360   //   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
4361   //   +--------------+--------------------------------+
4362   //   |                Immediate  15:0                |
4363   //   +-----------------------------------------------+
4364   //
4365   //   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
4366   //   Note that the immediate value in the first word is swapped.
4367   //
4368   //   When producing a relocatable object file, R_MIPS16_26 is
4369   //   handled mostly like R_MIPS_26.  In particular, the addend is
4370   //   stored as a straight 26-bit value in a 32-bit instruction.
4371   //   (gas makes life simpler for itself by never adjusting a
4372   //   R_MIPS16_26 reloc to be against a section, so the addend is
4373   //   always zero).  However, the 32 bit instruction is stored as 2
4374   //   16-bit values, rather than a single 32-bit value.  In a
4375   //   big-endian file, the result is the same; in a little-endian
4376   //   file, the two 16-bit halves of the 32 bit value are swapped.
4377   //   This is so that a disassembler can recognize the jal
4378   //   instruction.
4379   //
4380   //   When doing a final link, R_MIPS16_26 is treated as a 32 bit
4381   //   instruction stored as two 16-bit values.  The addend A is the
4382   //   contents of the targ26 field.  The calculation is the same as
4383   //   R_MIPS_26.  When storing the calculated value, reorder the
4384   //   immediate value as shown above, and don't forget to store the
4385   //   value as two 16-bit values.
4386   //
4387   //   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4388   //   defined as
4389   //
4390   //   big-endian:
4391   //   +--------+----------------------+
4392   //   |        |                      |
4393   //   |        |    targ26-16         |
4394   //   |31    26|25                   0|
4395   //   +--------+----------------------+
4396   //
4397   //   little-endian:
4398   //   +----------+------+-------------+
4399   //   |          |      |             |
4400   //   |  sub1    |      |     sub2    |
4401   //   |0        9|10  15|16         31|
4402   //   +----------+--------------------+
4403   //   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4404   //   ((sub1 << 16) | sub2)).
4405   //
4406   //   When producing a relocatable object file, the calculation is
4407   //   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4408   //   When producing a fully linked file, the calculation is
4409   //   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4410   //   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4411   //
4412   //   The table below lists the other MIPS16 instruction relocations.
4413   //   Each one is calculated in the same way as the non-MIPS16 relocation
4414   //   given on the right, but using the extended MIPS16 layout of 16-bit
4415   //   immediate fields:
4416   //
4417   //      R_MIPS16_GPREL          R_MIPS_GPREL16
4418   //      R_MIPS16_GOT16          R_MIPS_GOT16
4419   //      R_MIPS16_CALL16         R_MIPS_CALL16
4420   //      R_MIPS16_HI16           R_MIPS_HI16
4421   //      R_MIPS16_LO16           R_MIPS_LO16
4422   //
4423   //   A typical instruction will have a format like this:
4424   //
4425   //   +--------------+--------------------------------+
4426   //   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
4427   //   +--------------+--------------------------------+
4428   //   |    Major     |   rx   |   ry   |   Imm  4:0   |
4429   //   +--------------+--------------------------------+
4430   //
4431   //   EXTEND is the five bit value 11110.  Major is the instruction
4432   //   opcode.
4433   //
4434   //   All we need to do here is shuffle the bits appropriately.
4435   //   As above, the two 16-bit halves must be swapped on a
4436   //   little-endian system.
4437
4438   // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4439   // on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1
4440   // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4441
4442   static void
4443   mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4444                        bool jal_shuffle)
4445   {
4446     if (!mips16_reloc(r_type)
4447         && !should_shuffle_micromips_reloc(r_type))
4448       return;
4449
4450     // Pick up the first and second halfwords of the instruction.
4451     Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4452     Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4453     Valtype32 val;
4454
4455     if (micromips_reloc(r_type)
4456         || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4457       val = first << 16 | second;
4458     else if (r_type != elfcpp::R_MIPS16_26)
4459       val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4460              | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4461     else
4462       val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4463              | ((first & 0x1f) << 21) | second);
4464
4465     elfcpp::Swap<32, big_endian>::writeval(view, val);
4466   }
4467
4468   static void
4469   mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4470   {
4471     if (!mips16_reloc(r_type)
4472         && !should_shuffle_micromips_reloc(r_type))
4473       return;
4474
4475     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4476     Valtype16 first, second;
4477
4478     if (micromips_reloc(r_type)
4479         || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4480       {
4481         second = val & 0xffff;
4482         first = val >> 16;
4483       }
4484     else if (r_type != elfcpp::R_MIPS16_26)
4485       {
4486         second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4487         first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4488       }
4489     else
4490       {
4491         second = val & 0xffff;
4492         first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4493                  | ((val >> 21) & 0x1f);
4494       }
4495
4496     elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4497     elfcpp::Swap<16, big_endian>::writeval(view, first);
4498   }
4499
4500   // R_MIPS_16: S + sign-extend(A)
4501   static inline typename This::Status
4502   rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4503         const Symbol_value<size>* psymval, Mips_address addend_a,
4504         bool extract_addend, bool calculate_only, Valtype* calculated_value)
4505   {
4506     Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4507     Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4508
4509     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4510                                      : addend_a);
4511
4512     Valtype x = psymval->value(object, addend);
4513     val = Bits<16>::bit_select32(val, x, 0xffffU);
4514
4515     if (calculate_only)
4516       {
4517         *calculated_value = x;
4518         return This::STATUS_OKAY;
4519       }
4520     else
4521       elfcpp::Swap<16, big_endian>::writeval(wv, val);
4522
4523     return check_overflow<16>(x);
4524   }
4525
4526   // R_MIPS_32: S + A
4527   static inline typename This::Status
4528   rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4529         const Symbol_value<size>* psymval, Mips_address addend_a,
4530         bool extract_addend, bool calculate_only, Valtype* calculated_value)
4531   {
4532     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4533     Valtype addend = (extract_addend
4534                         ? elfcpp::Swap<32, big_endian>::readval(wv)
4535                         : addend_a);
4536     Valtype x = psymval->value(object, addend);
4537
4538     if (calculate_only)
4539       *calculated_value = x;
4540     else
4541       elfcpp::Swap<32, big_endian>::writeval(wv, x);
4542
4543     return This::STATUS_OKAY;
4544   }
4545
4546   // R_MIPS_JALR, R_MICROMIPS_JALR
4547   static inline typename This::Status
4548   reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4549           const Symbol_value<size>* psymval, Mips_address address,
4550           Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4551           unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4552           bool calculate_only, Valtype* calculated_value)
4553   {
4554     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4555     Valtype addend = extract_addend ? 0 : addend_a;
4556     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4557
4558     // Try converting J(AL)R to B(AL), if the target is in range.
4559     if (r_type == elfcpp::R_MIPS_JALR
4560         && !cross_mode_jump
4561         && ((jalr_to_bal && val == 0x0320f809)    // jalr t9
4562             || (jr_to_b && val == 0x03200008)))   // jr t9
4563       {
4564         int offset = psymval->value(object, addend) - (address + 4);
4565         if (!Bits<18>::has_overflow32(offset))
4566           {
4567             if (val == 0x03200008)   // jr t9
4568               val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff);  // b addr
4569             else
4570               val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4571           }
4572       }
4573
4574     if (calculate_only)
4575       *calculated_value = val;
4576     else
4577       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4578
4579     return This::STATUS_OKAY;
4580   }
4581
4582   // R_MIPS_PC32: S + A - P
4583   static inline typename This::Status
4584   relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4585           const Symbol_value<size>* psymval, Mips_address address,
4586           Mips_address addend_a, bool extract_addend, bool calculate_only,
4587           Valtype* calculated_value)
4588   {
4589     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4590     Valtype addend = (extract_addend
4591                         ? elfcpp::Swap<32, big_endian>::readval(wv)
4592                         : addend_a);
4593     Valtype x = psymval->value(object, addend) - address;
4594
4595     if (calculate_only)
4596        *calculated_value = x;
4597     else
4598       elfcpp::Swap<32, big_endian>::writeval(wv, x);
4599
4600     return This::STATUS_OKAY;
4601   }
4602
4603   // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4604   static inline typename This::Status
4605   rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4606         const Symbol_value<size>* psymval, Mips_address address,
4607         bool local, Mips_address addend_a, bool extract_addend,
4608         const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4609         bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4610   {
4611     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4612     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4613
4614     Valtype addend;
4615     if (extract_addend)
4616       {
4617         if (r_type == elfcpp::R_MICROMIPS_26_S1)
4618           addend = (val & 0x03ffffff) << 1;
4619         else
4620           addend = (val & 0x03ffffff) << 2;
4621       }
4622     else
4623       addend = addend_a;
4624
4625     // Make sure the target of JALX is word-aligned.  Bit 0 must be
4626     // the correct ISA mode selector and bit 1 must be 0.
4627     if (!calculate_only && cross_mode_jump
4628         && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4629       {
4630         gold_warning(_("JALX to a non-word-aligned address"));
4631         return This::STATUS_BAD_RELOC;
4632       }
4633
4634     // Shift is 2, unusually, for microMIPS JALX.
4635     unsigned int shift =
4636         (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4637
4638     Valtype x;
4639     if (local)
4640       x = addend | ((address + 4) & (0xfc000000 << shift));
4641     else
4642       {
4643         if (shift == 1)
4644           x = Bits<27>::sign_extend32(addend);
4645         else
4646           x = Bits<28>::sign_extend32(addend);
4647       }
4648     x = psymval->value(object, x) >> shift;
4649
4650     if (!calculate_only && !local && !gsym->is_weak_undefined()
4651         && ((x >> 26) != ((address + 4) >> (26 + shift))))
4652       return This::STATUS_OVERFLOW;
4653
4654     val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4655
4656     // If required, turn JAL into JALX.
4657     if (cross_mode_jump)
4658       {
4659         bool ok;
4660         Valtype32 opcode = val >> 26;
4661         Valtype32 jalx_opcode;
4662
4663         // Check to see if the opcode is already JAL or JALX.
4664         if (r_type == elfcpp::R_MIPS16_26)
4665           {
4666             ok = (opcode == 0x6) || (opcode == 0x7);
4667             jalx_opcode = 0x7;
4668           }
4669         else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4670           {
4671             ok = (opcode == 0x3d) || (opcode == 0x3c);
4672             jalx_opcode = 0x3c;
4673           }
4674         else
4675           {
4676             ok = (opcode == 0x3) || (opcode == 0x1d);
4677             jalx_opcode = 0x1d;
4678           }
4679
4680         // If the opcode is not JAL or JALX, there's a problem.  We cannot
4681         // convert J or JALS to JALX.
4682         if (!calculate_only && !ok)
4683           {
4684             gold_error(_("Unsupported jump between ISA modes; consider "
4685                          "recompiling with interlinking enabled."));
4686             return This::STATUS_BAD_RELOC;
4687           }
4688
4689         // Make this the JALX opcode.
4690         val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4691       }
4692
4693     // Try converting JAL to BAL, if the target is in range.
4694     if (!parameters->options().relocatable()
4695         && !cross_mode_jump
4696         && ((jal_to_bal
4697             && r_type == elfcpp::R_MIPS_26
4698             && (val >> 26) == 0x3)))    // jal addr
4699       {
4700         Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4701         int offset = dest - (address + 4);
4702         if (!Bits<18>::has_overflow32(offset))
4703           {
4704             if (val == 0x03200008)   // jr t9
4705               val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff);  // b addr
4706             else
4707               val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4708           }
4709       }
4710
4711     if (calculate_only)
4712       *calculated_value = val;
4713     else
4714       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4715
4716     return This::STATUS_OKAY;
4717   }
4718
4719   // R_MIPS_PC16
4720   static inline typename This::Status
4721   relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4722           const Symbol_value<size>* psymval, Mips_address address,
4723           Mips_address addend_a, bool extract_addend, bool calculate_only,
4724           Valtype* calculated_value)
4725   {
4726     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4727     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4728
4729     Valtype addend = (extract_addend
4730                       ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4731                       : addend_a);
4732
4733     Valtype x = psymval->value(object, addend) - address;
4734     val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4735
4736     if (calculate_only)
4737       {
4738         *calculated_value = x >> 2;
4739         return This::STATUS_OKAY;
4740       }
4741     else
4742       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4743
4744     if (psymval->value(object, addend) & 3)
4745       return This::STATUS_PCREL_UNALIGNED;
4746
4747     return check_overflow<18>(x);
4748   }
4749
4750   // R_MIPS_PC21_S2
4751   static inline typename This::Status
4752   relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4753           const Symbol_value<size>* psymval, Mips_address address,
4754           Mips_address addend_a, bool extract_addend, bool calculate_only,
4755           Valtype* calculated_value)
4756   {
4757     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4758     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4759
4760     Valtype addend = (extract_addend
4761                       ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4762                       : addend_a);
4763
4764     Valtype x = psymval->value(object, addend) - address;
4765     val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4766
4767     if (calculate_only)
4768       {
4769         *calculated_value = x >> 2;
4770         return This::STATUS_OKAY;
4771       }
4772     else
4773       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4774
4775     if (psymval->value(object, addend) & 3)
4776       return This::STATUS_PCREL_UNALIGNED;
4777
4778     return check_overflow<23>(x);
4779   }
4780
4781   // R_MIPS_PC26_S2
4782   static inline typename This::Status
4783   relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4784           const Symbol_value<size>* psymval, Mips_address address,
4785           Mips_address addend_a, bool extract_addend, bool calculate_only,
4786           Valtype* calculated_value)
4787   {
4788     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4789     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4790
4791     Valtype addend = (extract_addend
4792                       ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4793                       : addend_a);
4794
4795     Valtype x = psymval->value(object, addend) - address;
4796     val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4797
4798     if (calculate_only)
4799       {
4800         *calculated_value = x >> 2;
4801         return This::STATUS_OKAY;
4802       }
4803     else
4804       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4805
4806     if (psymval->value(object, addend) & 3)
4807       return This::STATUS_PCREL_UNALIGNED;
4808
4809     return check_overflow<28>(x);
4810   }
4811
4812   // R_MIPS_PC18_S3
4813   static inline typename This::Status
4814   relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4815           const Symbol_value<size>* psymval, Mips_address address,
4816           Mips_address addend_a, bool extract_addend, bool calculate_only,
4817           Valtype* calculated_value)
4818   {
4819     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4820     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4821
4822     Valtype addend = (extract_addend
4823                       ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4824                       : addend_a);
4825
4826     Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4827     val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4828
4829     if (calculate_only)
4830       {
4831         *calculated_value = x >> 3;
4832         return This::STATUS_OKAY;
4833       }
4834     else
4835       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4836
4837     if (psymval->value(object, addend) & 7)
4838       return This::STATUS_PCREL_UNALIGNED;
4839
4840     return check_overflow<21>(x);
4841   }
4842
4843   // R_MIPS_PC19_S2
4844   static inline typename This::Status
4845   relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4846           const Symbol_value<size>* psymval, Mips_address address,
4847           Mips_address addend_a, bool extract_addend, bool calculate_only,
4848           Valtype* calculated_value)
4849   {
4850     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4851     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4852
4853     Valtype addend = (extract_addend
4854                       ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4855                       : addend_a);
4856
4857     Valtype x = psymval->value(object, addend) - address;
4858     val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4859
4860     if (calculate_only)
4861       {
4862         *calculated_value = x >> 2;
4863         return This::STATUS_OKAY;
4864       }
4865     else
4866       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4867
4868     if (psymval->value(object, addend) & 3)
4869       return This::STATUS_PCREL_UNALIGNED;
4870
4871     return check_overflow<21>(x);
4872   }
4873
4874   // R_MIPS_PCHI16
4875   static inline typename This::Status
4876   relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4877             const Symbol_value<size>* psymval, Mips_address addend,
4878             Mips_address address, unsigned int r_sym, bool extract_addend)
4879   {
4880     // Record the relocation.  It will be resolved when we find pclo16 part.
4881     pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4882                             addend, 0, r_sym, extract_addend, address));
4883     return This::STATUS_OKAY;
4884   }
4885
4886   // R_MIPS_PCHI16
4887   static inline typename This::Status
4888   do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4889              const Symbol_value<size>* psymval, Mips_address addend_hi,
4890              Mips_address address, bool extract_addend, Valtype32 addend_lo,
4891              bool calculate_only, Valtype* calculated_value)
4892   {
4893     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4894     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4895
4896     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4897                                        : addend_hi);
4898
4899     Valtype value = psymval->value(object, addend) - address;
4900     Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4901     val = Bits<32>::bit_select32(val, x, 0xffff);
4902
4903     if (calculate_only)
4904       *calculated_value = x;
4905     else
4906       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4907
4908     return This::STATUS_OKAY;
4909   }
4910
4911   // R_MIPS_PCLO16
4912   static inline typename This::Status
4913   relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4914             const Symbol_value<size>* psymval, Mips_address addend_a,
4915             bool extract_addend, Mips_address address, unsigned int r_sym,
4916             unsigned int rel_type, bool calculate_only,
4917             Valtype* calculated_value)
4918   {
4919     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4920     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4921
4922     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4923                                      : addend_a);
4924
4925     if (rel_type == elfcpp::SHT_REL)
4926       {
4927         // Resolve pending R_MIPS_PCHI16 relocations.
4928         typename std::list<reloc_high<size, big_endian> >::iterator it =
4929             pchi16_relocs.begin();
4930         while (it != pchi16_relocs.end())
4931           {
4932             reloc_high<size, big_endian> pchi16 = *it;
4933             if (pchi16.r_sym == r_sym)
4934               {
4935                 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4936                              pchi16.addend, pchi16.address,
4937                              pchi16.extract_addend, addend, calculate_only,
4938                              calculated_value);
4939                 it = pchi16_relocs.erase(it);
4940               }
4941             else
4942               ++it;
4943           }
4944       }
4945
4946     // Resolve R_MIPS_PCLO16 relocation.
4947     Valtype x = psymval->value(object, addend) - address;
4948     val = Bits<32>::bit_select32(val, x, 0xffff);
4949
4950     if (calculate_only)
4951       *calculated_value = x;
4952     else
4953       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4954
4955     return This::STATUS_OKAY;
4956   }
4957
4958   // R_MICROMIPS_PC7_S1
4959   static inline typename This::Status
4960   relmicromips_pc7_s1(unsigned char* view,
4961                       const Mips_relobj<size, big_endian>* object,
4962                       const Symbol_value<size>* psymval, Mips_address address,
4963                       Mips_address addend_a, bool extract_addend,
4964                       bool calculate_only, Valtype* calculated_value)
4965   {
4966     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4967     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4968
4969     Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4970                                     : addend_a;
4971
4972     Valtype x = psymval->value(object, addend) - address;
4973     val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4974
4975     if (calculate_only)
4976       {
4977         *calculated_value = x >> 1;
4978         return This::STATUS_OKAY;
4979       }
4980     else
4981       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4982
4983     return check_overflow<8>(x);
4984   }
4985
4986   // R_MICROMIPS_PC10_S1
4987   static inline typename This::Status
4988   relmicromips_pc10_s1(unsigned char* view,
4989                        const Mips_relobj<size, big_endian>* object,
4990                        const Symbol_value<size>* psymval, Mips_address address,
4991                        Mips_address addend_a, bool extract_addend,
4992                        bool calculate_only, Valtype* calculated_value)
4993   {
4994     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4995     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4996
4997     Valtype addend = (extract_addend
4998                       ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4999                       : addend_a);
5000
5001     Valtype x = psymval->value(object, addend) - address;
5002     val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
5003
5004     if (calculate_only)
5005       {
5006         *calculated_value = x >> 1;
5007         return This::STATUS_OKAY;
5008       }
5009     else
5010       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5011
5012     return check_overflow<11>(x);
5013   }
5014
5015   // R_MICROMIPS_PC16_S1
5016   static inline typename This::Status
5017   relmicromips_pc16_s1(unsigned char* view,
5018                        const Mips_relobj<size, big_endian>* object,
5019                        const Symbol_value<size>* psymval, Mips_address address,
5020                        Mips_address addend_a, bool extract_addend,
5021                        bool calculate_only, Valtype* calculated_value)
5022   {
5023     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5024     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5025
5026     Valtype addend = (extract_addend
5027                       ? Bits<17>::sign_extend32((val & 0xffff) << 1)
5028                       : addend_a);
5029
5030     Valtype x = psymval->value(object, addend) - address;
5031     val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
5032
5033     if (calculate_only)
5034       {
5035         *calculated_value = x >> 1;
5036         return This::STATUS_OKAY;
5037       }
5038     else
5039       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5040
5041     return check_overflow<17>(x);
5042   }
5043
5044   // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5045   static inline typename This::Status
5046   relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5047           const Symbol_value<size>* psymval, Mips_address addend,
5048           Mips_address address, bool gp_disp, unsigned int r_type,
5049           unsigned int r_sym, bool extract_addend)
5050   {
5051     // Record the relocation.  It will be resolved when we find lo16 part.
5052     hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5053                           addend, r_type, r_sym, extract_addend, address,
5054                           gp_disp));
5055     return This::STATUS_OKAY;
5056   }
5057
5058   // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5059   static inline typename This::Status
5060   do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5061              const Symbol_value<size>* psymval, Mips_address addend_hi,
5062              Mips_address address, bool is_gp_disp, unsigned int r_type,
5063              bool extract_addend, Valtype32 addend_lo,
5064              Target_mips<size, big_endian>* target, bool calculate_only,
5065              Valtype* calculated_value)
5066   {
5067     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5068     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5069
5070     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5071                                        : addend_hi);
5072
5073     Valtype32 value;
5074     if (!is_gp_disp)
5075       value = psymval->value(object, addend);
5076     else
5077       {
5078         // For MIPS16 ABI code we generate this sequence
5079         //    0: li      $v0,%hi(_gp_disp)
5080         //    4: addiupc $v1,%lo(_gp_disp)
5081         //    8: sll     $v0,16
5082         //   12: addu    $v0,$v1
5083         //   14: move    $gp,$v0
5084         // So the offsets of hi and lo relocs are the same, but the
5085         // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5086         // ADDIUPC clears the low two bits of the instruction address,
5087         // so the base is ($t9 + 4) & ~3.
5088         Valtype32 gp_disp;
5089         if (r_type == elfcpp::R_MIPS16_HI16)
5090           gp_disp = (target->adjusted_gp_value(object)
5091                      - ((address + 4) & ~0x3));
5092         // The microMIPS .cpload sequence uses the same assembly
5093         // instructions as the traditional psABI version, but the
5094         // incoming $t9 has the low bit set.
5095         else if (r_type == elfcpp::R_MICROMIPS_HI16)
5096           gp_disp = target->adjusted_gp_value(object) - address - 1;
5097         else
5098           gp_disp = target->adjusted_gp_value(object) - address;
5099         value = gp_disp + addend;
5100       }
5101     Valtype x = ((value + 0x8000) >> 16) & 0xffff;
5102     val = Bits<32>::bit_select32(val, x, 0xffff);
5103
5104     if (calculate_only)
5105       {
5106         *calculated_value = x;
5107         return This::STATUS_OKAY;
5108       }
5109     else
5110       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5111
5112     return (is_gp_disp ? check_overflow<16>(x)
5113                        : This::STATUS_OKAY);
5114   }
5115
5116   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5117   static inline typename This::Status
5118   relgot16_local(unsigned char* view,
5119                  const Mips_relobj<size, big_endian>* object,
5120                  const Symbol_value<size>* psymval, Mips_address addend_a,
5121                  bool extract_addend, unsigned int r_type, unsigned int r_sym)
5122   {
5123     // Record the relocation.  It will be resolved when we find lo16 part.
5124     got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5125                            addend_a, r_type, r_sym, extract_addend));
5126     return This::STATUS_OKAY;
5127   }
5128
5129   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5130   static inline typename This::Status
5131   do_relgot16_local(unsigned char* view,
5132                     const Mips_relobj<size, big_endian>* object,
5133                     const Symbol_value<size>* psymval, Mips_address addend_hi,
5134                     bool extract_addend, Valtype32 addend_lo,
5135                     Target_mips<size, big_endian>* target, bool calculate_only,
5136                     Valtype* calculated_value)
5137   {
5138     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5139     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5140
5141     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5142                                        : addend_hi);
5143
5144     // Find GOT page entry.
5145     Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5146                           & 0xffff;
5147     value <<= 16;
5148     unsigned int got_offset =
5149       target->got_section()->get_got_page_offset(value, object);
5150
5151     // Resolve the relocation.
5152     Valtype x = target->got_section()->gp_offset(got_offset, object);
5153     val = Bits<32>::bit_select32(val, x, 0xffff);
5154
5155     if (calculate_only)
5156       {
5157         *calculated_value = x;
5158         return This::STATUS_OKAY;
5159       }
5160     else
5161       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5162
5163     return check_overflow<16>(x);
5164   }
5165
5166   // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5167   static inline typename This::Status
5168   rello16(Target_mips<size, big_endian>* target, unsigned char* view,
5169           const Mips_relobj<size, big_endian>* object,
5170           const Symbol_value<size>* psymval, Mips_address addend_a,
5171           bool extract_addend, Mips_address address, bool is_gp_disp,
5172           unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
5173           bool calculate_only, Valtype* calculated_value)
5174   {
5175     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5176     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5177
5178     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5179                                      : addend_a);
5180
5181     if (rel_type == elfcpp::SHT_REL)
5182       {
5183         typename This::Status reloc_status = This::STATUS_OKAY;
5184         // Resolve pending R_MIPS_HI16 relocations.
5185         typename std::list<reloc_high<size, big_endian> >::iterator it =
5186           hi16_relocs.begin();
5187         while (it != hi16_relocs.end())
5188           {
5189             reloc_high<size, big_endian> hi16 = *it;
5190             if (hi16.r_sym == r_sym
5191                 && is_matching_lo16_reloc(hi16.r_type, r_type))
5192               {
5193                 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
5194                 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
5195                                        hi16.addend, hi16.address, hi16.gp_disp,
5196                                        hi16.r_type, hi16.extract_addend, addend,
5197                                        target, calculate_only, calculated_value);
5198                 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
5199                 if (reloc_status == This::STATUS_OVERFLOW)
5200                   return This::STATUS_OVERFLOW;
5201                 it = hi16_relocs.erase(it);
5202               }
5203             else
5204               ++it;
5205           }
5206
5207         // Resolve pending local R_MIPS_GOT16 relocations.
5208         typename std::list<reloc_high<size, big_endian> >::iterator it2 =
5209           got16_relocs.begin();
5210         while (it2 != got16_relocs.end())
5211           {
5212             reloc_high<size, big_endian> got16 = *it2;
5213             if (got16.r_sym == r_sym
5214                 && is_matching_lo16_reloc(got16.r_type, r_type))
5215               {
5216                 mips_reloc_unshuffle(got16.view, got16.r_type, false);
5217
5218                 reloc_status = do_relgot16_local(got16.view, got16.object,
5219                                      got16.psymval, got16.addend,
5220                                      got16.extract_addend, addend, target,
5221                                      calculate_only, calculated_value);
5222
5223                 mips_reloc_shuffle(got16.view, got16.r_type, false);
5224                 if (reloc_status == This::STATUS_OVERFLOW)
5225                   return This::STATUS_OVERFLOW;
5226                 it2 = got16_relocs.erase(it2);
5227               }
5228             else
5229               ++it2;
5230           }
5231       }
5232
5233     // Resolve R_MIPS_LO16 relocation.
5234     Valtype x;
5235     if (!is_gp_disp)
5236       x = psymval->value(object, addend);
5237     else
5238       {
5239         // See the comment for R_MIPS16_HI16 above for the reason
5240         // for this conditional.
5241         Valtype32 gp_disp;
5242         if (r_type == elfcpp::R_MIPS16_LO16)
5243           gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
5244         else if (r_type == elfcpp::R_MICROMIPS_LO16
5245                  || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
5246           gp_disp = target->adjusted_gp_value(object) - address + 3;
5247         else
5248           gp_disp = target->adjusted_gp_value(object) - address + 4;
5249         // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5250         // for overflow.  Relocations against _gp_disp are normally
5251         // generated from the .cpload pseudo-op.  It generates code
5252         // that normally looks like this:
5253
5254         //   lui    $gp,%hi(_gp_disp)
5255         //   addiu  $gp,$gp,%lo(_gp_disp)
5256         //   addu   $gp,$gp,$t9
5257
5258         // Here $t9 holds the address of the function being called,
5259         // as required by the MIPS ELF ABI.  The R_MIPS_LO16
5260         // relocation can easily overflow in this situation, but the
5261         // R_MIPS_HI16 relocation will handle the overflow.
5262         // Therefore, we consider this a bug in the MIPS ABI, and do
5263         // not check for overflow here.
5264         x = gp_disp + addend;
5265       }
5266     val = Bits<32>::bit_select32(val, x, 0xffff);
5267
5268     if (calculate_only)
5269       *calculated_value = x;
5270     else
5271       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5272
5273     return This::STATUS_OKAY;
5274   }
5275
5276   // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5277   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5278   // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5279   // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5280   // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5281   // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5282   static inline typename This::Status
5283   relgot(unsigned char* view, int gp_offset, bool calculate_only,
5284          Valtype* calculated_value)
5285   {
5286     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5287     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5288     Valtype x = gp_offset;
5289     val = Bits<32>::bit_select32(val, x, 0xffff);
5290
5291     if (calculate_only)
5292       {
5293         *calculated_value = x;
5294         return This::STATUS_OKAY;
5295       }
5296     else
5297       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5298
5299     return check_overflow<16>(x);
5300   }
5301
5302   // R_MIPS_EH
5303   static inline typename This::Status
5304   releh(unsigned char* view, int gp_offset, bool calculate_only,
5305         Valtype* calculated_value)
5306   {
5307     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5308     Valtype x = gp_offset;
5309
5310     if (calculate_only)
5311       {
5312         *calculated_value = x;
5313         return This::STATUS_OKAY;
5314       }
5315     else
5316       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5317
5318     return check_overflow<32>(x);
5319   }
5320
5321   // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5322   static inline typename This::Status
5323   relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5324              const Mips_relobj<size, big_endian>* object,
5325              const Symbol_value<size>* psymval, Mips_address addend_a,
5326              bool extract_addend, bool calculate_only,
5327              Valtype* calculated_value)
5328   {
5329     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5330     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5331     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5332
5333     // Find a GOT page entry that points to within 32KB of symbol + addend.
5334     Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5335     unsigned int  got_offset =
5336       target->got_section()->get_got_page_offset(value, object);
5337
5338     Valtype x = target->got_section()->gp_offset(got_offset, object);
5339     val = Bits<32>::bit_select32(val, x, 0xffff);
5340
5341     if (calculate_only)
5342       {
5343         *calculated_value = x;
5344         return This::STATUS_OKAY;
5345       }
5346     else
5347       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5348
5349     return check_overflow<16>(x);
5350   }
5351
5352   // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5353   static inline typename This::Status
5354   relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5355              const Mips_relobj<size, big_endian>* object,
5356              const Symbol_value<size>* psymval, Mips_address addend_a,
5357              bool extract_addend, bool local, bool calculate_only,
5358              Valtype* calculated_value)
5359   {
5360     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5361     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5362     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5363
5364     // For a local symbol, find a GOT page entry that points to within 32KB of
5365     // symbol + addend.  Relocation value is the offset of the GOT page entry's
5366     // value from symbol + addend.
5367     // For a global symbol, relocation value is addend.
5368     Valtype x;
5369     if (local)
5370       {
5371         // Find GOT page entry.
5372         Mips_address value = ((psymval->value(object, addend) + 0x8000)
5373                               & ~0xffff);
5374         target->got_section()->get_got_page_offset(value, object);
5375
5376         x = psymval->value(object, addend) - value;
5377       }
5378     else
5379       x = addend;
5380     val = Bits<32>::bit_select32(val, x, 0xffff);
5381
5382     if (calculate_only)
5383       {
5384         *calculated_value = x;
5385         return This::STATUS_OKAY;
5386       }
5387     else
5388       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5389
5390     return check_overflow<16>(x);
5391   }
5392
5393   // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5394   // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5395   static inline typename This::Status
5396   relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5397               Valtype* calculated_value)
5398   {
5399     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5400     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5401     Valtype x = gp_offset;
5402     x = ((x + 0x8000) >> 16) & 0xffff;
5403     val = Bits<32>::bit_select32(val, x, 0xffff);
5404
5405     if (calculate_only)
5406       *calculated_value = x;
5407     else
5408       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5409
5410     return This::STATUS_OKAY;
5411   }
5412
5413   // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5414   // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5415   static inline typename This::Status
5416   relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5417               Valtype* calculated_value)
5418   {
5419     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5420     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5421     Valtype x = gp_offset;
5422     val = Bits<32>::bit_select32(val, x, 0xffff);
5423
5424     if (calculate_only)
5425       *calculated_value = x;
5426     else
5427       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5428
5429     return This::STATUS_OKAY;
5430   }
5431
5432   // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5433   // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5434   static inline typename This::Status
5435   relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5436            const Symbol_value<size>* psymval, Mips_address gp,
5437            Mips_address addend_a, bool extract_addend, bool local,
5438            unsigned int r_type, bool calculate_only,
5439            Valtype* calculated_value)
5440   {
5441     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5442     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5443
5444     Valtype addend;
5445     if (extract_addend)
5446       {
5447         if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5448           addend = (val & 0x7f) << 2;
5449         else
5450           addend = val & 0xffff;
5451         // Only sign-extend the addend if it was extracted from the
5452         // instruction.  If the addend was separate, leave it alone,
5453         // otherwise we may lose significant bits.
5454         addend = Bits<16>::sign_extend32(addend);
5455       }
5456     else
5457       addend = addend_a;
5458
5459     Valtype x = psymval->value(object, addend) - gp;
5460
5461     // If the symbol was local, any earlier relocatable links will
5462     // have adjusted its addend with the gp offset, so compensate
5463     // for that now.  Don't do it for symbols forced local in this
5464     // link, though, since they won't have had the gp offset applied
5465     // to them before.
5466     if (local)
5467       x += object->gp_value();
5468
5469     if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5470       val = Bits<32>::bit_select32(val, x, 0x7f);
5471     else
5472       val = Bits<32>::bit_select32(val, x, 0xffff);
5473
5474     if (calculate_only)
5475       {
5476         *calculated_value = x;
5477         return This::STATUS_OKAY;
5478       }
5479     else
5480       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5481
5482     if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5483       {
5484         gold_error(_("small-data section exceeds 64KB; lower small-data size "
5485                      "limit (see option -G)"));
5486         return This::STATUS_OVERFLOW;
5487       }
5488     return This::STATUS_OKAY;
5489   }
5490
5491   // R_MIPS_GPREL32
5492   static inline typename This::Status
5493   relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5494              const Symbol_value<size>* psymval, Mips_address gp,
5495              Mips_address addend_a, bool extract_addend, bool calculate_only,
5496              Valtype* calculated_value)
5497   {
5498     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5499     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5500     Valtype addend = extract_addend ? val : addend_a;
5501
5502     // R_MIPS_GPREL32 relocations are defined for local symbols only.
5503     Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5504
5505     if (calculate_only)
5506       *calculated_value = x;
5507     else
5508       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5509
5510     return This::STATUS_OKAY;
5511  }
5512
5513   // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5514   // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5515   // R_MICROMIPS_TLS_DTPREL_HI16
5516   static inline typename This::Status
5517   tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5518              const Symbol_value<size>* psymval, Valtype32 tp_offset,
5519              Mips_address addend_a, bool extract_addend, bool calculate_only,
5520              Valtype* calculated_value)
5521   {
5522     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5523     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5524     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5525
5526     // tls symbol values are relative to tls_segment()->vaddr()
5527     Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5528     val = Bits<32>::bit_select32(val, x, 0xffff);
5529
5530     if (calculate_only)
5531       *calculated_value = x;
5532     else
5533       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5534
5535     return This::STATUS_OKAY;
5536   }
5537
5538   // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5539   // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5540   // R_MICROMIPS_TLS_DTPREL_LO16,
5541   static inline typename This::Status
5542   tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5543              const Symbol_value<size>* psymval, Valtype32 tp_offset,
5544              Mips_address addend_a, bool extract_addend, bool calculate_only,
5545              Valtype* calculated_value)
5546   {
5547     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5548     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5549     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5550
5551     // tls symbol values are relative to tls_segment()->vaddr()
5552     Valtype x = psymval->value(object, addend) - tp_offset;
5553     val = Bits<32>::bit_select32(val, x, 0xffff);
5554
5555     if (calculate_only)
5556       *calculated_value = x;
5557     else
5558       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5559
5560     return This::STATUS_OKAY;
5561   }
5562
5563   // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5564   // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5565   static inline typename This::Status
5566   tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5567            const Symbol_value<size>* psymval, Valtype32 tp_offset,
5568            Mips_address addend_a, bool extract_addend, bool calculate_only,
5569            Valtype* calculated_value)
5570   {
5571     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5572     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5573     Valtype addend = extract_addend ? val : addend_a;
5574
5575     // tls symbol values are relative to tls_segment()->vaddr()
5576     Valtype x = psymval->value(object, addend) - tp_offset;
5577
5578     if (calculate_only)
5579       *calculated_value = x;
5580     else
5581       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5582
5583     return This::STATUS_OKAY;
5584   }
5585
5586   // R_MIPS_SUB, R_MICROMIPS_SUB
5587   static inline typename This::Status
5588   relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5589          const Symbol_value<size>* psymval, Mips_address addend_a,
5590          bool extract_addend, bool calculate_only, Valtype* calculated_value)
5591   {
5592     Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5593     Valtype64 addend = (extract_addend
5594                         ? elfcpp::Swap<64, big_endian>::readval(wv)
5595                         : addend_a);
5596
5597     Valtype64 x = psymval->value(object, -addend);
5598     if (calculate_only)
5599       *calculated_value = x;
5600     else
5601       elfcpp::Swap<64, big_endian>::writeval(wv, x);
5602
5603     return This::STATUS_OKAY;
5604   }
5605
5606   // R_MIPS_64: S + A
5607   static inline typename This::Status
5608   rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5609         const Symbol_value<size>* psymval, Mips_address addend_a,
5610         bool extract_addend, bool calculate_only, Valtype* calculated_value,
5611         bool apply_addend_only)
5612   {
5613     Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5614     Valtype64 addend = (extract_addend
5615                         ? elfcpp::Swap<64, big_endian>::readval(wv)
5616                         : addend_a);
5617
5618     Valtype64 x = psymval->value(object, addend);
5619     if (calculate_only)
5620       *calculated_value = x;
5621     else
5622       {
5623         if (apply_addend_only)
5624           x = addend;
5625         elfcpp::Swap<64, big_endian>::writeval(wv, x);
5626       }
5627
5628     return This::STATUS_OKAY;
5629   }
5630
5631   // R_MIPS_HIGHER, R_MICROMIPS_HIGHER
5632   static inline typename This::Status
5633   relhigher(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5634             const Symbol_value<size>* psymval, Mips_address addend_a,
5635             bool extract_addend, bool calculate_only, Valtype* calculated_value)
5636   {
5637     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5638     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5639     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5640                                      : addend_a);
5641
5642     Valtype x = psymval->value(object, addend);
5643     x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
5644     val = Bits<32>::bit_select32(val, x, 0xffff);
5645
5646     if (calculate_only)
5647       *calculated_value = x;
5648     else
5649       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5650
5651     return This::STATUS_OKAY;
5652   }
5653
5654   // R_MIPS_HIGHEST, R_MICROMIPS_HIGHEST
5655   static inline typename This::Status
5656   relhighest(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5657              const Symbol_value<size>* psymval, Mips_address addend_a,
5658              bool extract_addend, bool calculate_only,
5659              Valtype* calculated_value)
5660   {
5661     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5662     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5663     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5664                                      : addend_a);
5665
5666     Valtype x = psymval->value(object, addend);
5667     x = ((x + (uint64_t) 0x800080008000llu) >> 48) & 0xffff;
5668     val = Bits<32>::bit_select32(val, x, 0xffff);
5669
5670     if (calculate_only)
5671       *calculated_value = x;
5672     else
5673       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5674
5675     return This::STATUS_OKAY;
5676   }
5677 };
5678
5679 template<int size, bool big_endian>
5680 typename std::list<reloc_high<size, big_endian> >
5681     Mips_relocate_functions<size, big_endian>::hi16_relocs;
5682
5683 template<int size, bool big_endian>
5684 typename std::list<reloc_high<size, big_endian> >
5685     Mips_relocate_functions<size, big_endian>::got16_relocs;
5686
5687 template<int size, bool big_endian>
5688 typename std::list<reloc_high<size, big_endian> >
5689     Mips_relocate_functions<size, big_endian>::pchi16_relocs;
5690
5691 // Mips_got_info methods.
5692
5693 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5694 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5695
5696 template<int size, bool big_endian>
5697 void
5698 Mips_got_info<size, big_endian>::record_local_got_symbol(
5699     Mips_relobj<size, big_endian>* object, unsigned int symndx,
5700     Mips_address addend, unsigned int r_type, unsigned int shndx,
5701     bool is_section_symbol)
5702 {
5703   Mips_got_entry<size, big_endian>* entry =
5704     new Mips_got_entry<size, big_endian>(object, symndx, addend,
5705                                          mips_elf_reloc_tls_type(r_type),
5706                                          shndx, is_section_symbol);
5707   this->record_got_entry(entry, object);
5708 }
5709
5710 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5711 // in OBJECT.  FOR_CALL is true if the caller is only interested in
5712 // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
5713 // relocation.
5714
5715 template<int size, bool big_endian>
5716 void
5717 Mips_got_info<size, big_endian>::record_global_got_symbol(
5718     Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5719     unsigned int r_type, bool dyn_reloc, bool for_call)
5720 {
5721   if (!for_call)
5722     mips_sym->set_got_not_only_for_calls();
5723
5724   // A global symbol in the GOT must also be in the dynamic symbol table.
5725   if (!mips_sym->needs_dynsym_entry() && !mips_sym->is_forced_local())
5726     {
5727       switch (mips_sym->visibility())
5728         {
5729         case elfcpp::STV_INTERNAL:
5730         case elfcpp::STV_HIDDEN:
5731           mips_sym->set_is_forced_local();
5732           break;
5733         default:
5734           mips_sym->set_needs_dynsym_entry();
5735           break;
5736         }
5737     }
5738
5739   unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5740   if (tls_type == GOT_TLS_NONE)
5741     this->global_got_symbols_.insert(mips_sym);
5742
5743   if (dyn_reloc)
5744     {
5745       if (mips_sym->global_got_area() == GGA_NONE)
5746         mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5747       return;
5748     }
5749
5750   Mips_got_entry<size, big_endian>* entry =
5751     new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5752
5753   this->record_got_entry(entry, object);
5754 }
5755
5756 // Add ENTRY to master GOT and to OBJECT's GOT.
5757
5758 template<int size, bool big_endian>
5759 void
5760 Mips_got_info<size, big_endian>::record_got_entry(
5761     Mips_got_entry<size, big_endian>* entry,
5762     Mips_relobj<size, big_endian>* object)
5763 {
5764   this->got_entries_.insert(entry);
5765
5766   // Create the GOT entry for the OBJECT's GOT.
5767   Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5768   Mips_got_entry<size, big_endian>* entry2 =
5769     new Mips_got_entry<size, big_endian>(*entry);
5770
5771   g->got_entries_.insert(entry2);
5772 }
5773
5774 // Record that OBJECT has a page relocation against symbol SYMNDX and
5775 // that ADDEND is the addend for that relocation.
5776 // This function creates an upper bound on the number of GOT slots
5777 // required; no attempt is made to combine references to non-overridable
5778 // global symbols across multiple input files.
5779
5780 template<int size, bool big_endian>
5781 void
5782 Mips_got_info<size, big_endian>::record_got_page_entry(
5783     Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5784 {
5785   struct Got_page_range **range_ptr, *range;
5786   int old_pages, new_pages;
5787
5788   // Find the Got_page_entry for this symbol.
5789   Got_page_entry* entry = new Got_page_entry(object, symndx);
5790   typename Got_page_entry_set::iterator it =
5791     this->got_page_entries_.find(entry);
5792   if (it != this->got_page_entries_.end())
5793     entry = *it;
5794   else
5795     this->got_page_entries_.insert(entry);
5796
5797   // Add the same entry to the OBJECT's GOT.
5798   Got_page_entry* entry2 = NULL;
5799   Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5800   if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5801     {
5802       entry2 = new Got_page_entry(*entry);
5803       g2->got_page_entries_.insert(entry2);
5804     }
5805
5806   // Skip over ranges whose maximum extent cannot share a page entry
5807   // with ADDEND.
5808   range_ptr = &entry->ranges;
5809   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5810     range_ptr = &(*range_ptr)->next;
5811
5812   // If we scanned to the end of the list, or found a range whose
5813   // minimum extent cannot share a page entry with ADDEND, create
5814   // a new singleton range.
5815   range = *range_ptr;
5816   if (!range || addend < range->min_addend - 0xffff)
5817     {
5818       range = new Got_page_range();
5819       range->next = *range_ptr;
5820       range->min_addend = addend;
5821       range->max_addend = addend;
5822
5823       *range_ptr = range;
5824       ++entry->num_pages;
5825       if (entry2 != NULL)
5826         ++entry2->num_pages;
5827       ++this->page_gotno_;
5828       ++g2->page_gotno_;
5829       return;
5830     }
5831
5832   // Remember how many pages the old range contributed.
5833   old_pages = range->get_max_pages();
5834
5835   // Update the ranges.
5836   if (addend < range->min_addend)
5837     range->min_addend = addend;
5838   else if (addend > range->max_addend)
5839     {
5840       if (range->next && addend >= range->next->min_addend - 0xffff)
5841         {
5842           old_pages += range->next->get_max_pages();
5843           range->max_addend = range->next->max_addend;
5844           range->next = range->next->next;
5845         }
5846       else
5847         range->max_addend = addend;
5848     }
5849
5850   // Record any change in the total estimate.
5851   new_pages = range->get_max_pages();
5852   if (old_pages != new_pages)
5853     {
5854       entry->num_pages += new_pages - old_pages;
5855       if (entry2 != NULL)
5856         entry2->num_pages += new_pages - old_pages;
5857       this->page_gotno_ += new_pages - old_pages;
5858       g2->page_gotno_ += new_pages - old_pages;
5859     }
5860 }
5861
5862 // Create all entries that should be in the local part of the GOT.
5863
5864 template<int size, bool big_endian>
5865 void
5866 Mips_got_info<size, big_endian>::add_local_entries(
5867     Target_mips<size, big_endian>* target, Layout* layout)
5868 {
5869   Mips_output_data_got<size, big_endian>* got = target->got_section();
5870   // First two GOT entries are reserved.  The first entry will be filled at
5871   // runtime.  The second entry will be used by some runtime loaders.
5872   got->add_constant(0);
5873   got->add_constant(target->mips_elf_gnu_got1_mask());
5874
5875   for (typename Got_entry_set::iterator
5876        p = this->got_entries_.begin();
5877        p != this->got_entries_.end();
5878        ++p)
5879     {
5880       Mips_got_entry<size, big_endian>* entry = *p;
5881       if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5882         {
5883           got->add_local(entry->object(), entry->symndx(),
5884                          GOT_TYPE_STANDARD, entry->addend());
5885           unsigned int got_offset = entry->object()->local_got_offset(
5886               entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5887           if (got->multi_got() && this->index_ > 0
5888               && parameters->options().output_is_position_independent())
5889           {
5890             if (!entry->is_section_symbol())
5891               target->rel_dyn_section(layout)->add_local(entry->object(),
5892                   entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5893             else
5894               target->rel_dyn_section(layout)->add_symbolless_local_addend(
5895                   entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5896                   got, got_offset);
5897           }
5898         }
5899     }
5900
5901   this->add_page_entries(target, layout);
5902
5903   // Add global entries that should be in the local area.
5904   for (typename Got_entry_set::iterator
5905        p = this->got_entries_.begin();
5906        p != this->got_entries_.end();
5907        ++p)
5908     {
5909       Mips_got_entry<size, big_endian>* entry = *p;
5910       if (!entry->is_for_global_symbol())
5911         continue;
5912
5913       Mips_symbol<size>* mips_sym = entry->sym();
5914       if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5915         {
5916           unsigned int got_type;
5917           if (!got->multi_got())
5918             got_type = GOT_TYPE_STANDARD;
5919           else
5920             got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5921           if (got->add_global(mips_sym, got_type))
5922             {
5923               mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5924               if (got->multi_got() && this->index_ > 0
5925                   && parameters->options().output_is_position_independent())
5926                 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5927                     mips_sym, elfcpp::R_MIPS_REL32, got,
5928                     mips_sym->got_offset(got_type));
5929             }
5930         }
5931     }
5932 }
5933
5934 // Create GOT page entries.
5935
5936 template<int size, bool big_endian>
5937 void
5938 Mips_got_info<size, big_endian>::add_page_entries(
5939     Target_mips<size, big_endian>* target, Layout* layout)
5940 {
5941   if (this->page_gotno_ == 0)
5942     return;
5943
5944   Mips_output_data_got<size, big_endian>* got = target->got_section();
5945   this->got_page_offset_start_ = got->add_constant(0);
5946   if (got->multi_got() && this->index_ > 0
5947       && parameters->options().output_is_position_independent())
5948     target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5949                                                   this->got_page_offset_start_);
5950   int num_entries = this->page_gotno_;
5951   unsigned int prev_offset = this->got_page_offset_start_;
5952   while (--num_entries > 0)
5953     {
5954       unsigned int next_offset = got->add_constant(0);
5955       if (got->multi_got() && this->index_ > 0
5956           && parameters->options().output_is_position_independent())
5957         target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5958                                                       next_offset);
5959       gold_assert(next_offset == prev_offset + size/8);
5960       prev_offset = next_offset;
5961     }
5962   this->got_page_offset_next_ = this->got_page_offset_start_;
5963 }
5964
5965 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5966
5967 template<int size, bool big_endian>
5968 void
5969 Mips_got_info<size, big_endian>::add_global_entries(
5970     Target_mips<size, big_endian>* target, Layout* layout,
5971     unsigned int non_reloc_only_global_gotno)
5972 {
5973   Mips_output_data_got<size, big_endian>* got = target->got_section();
5974   // Add GGA_NORMAL entries.
5975   unsigned int count = 0;
5976   for (typename Got_entry_set::iterator
5977        p = this->got_entries_.begin();
5978        p != this->got_entries_.end();
5979        ++p)
5980     {
5981       Mips_got_entry<size, big_endian>* entry = *p;
5982       if (!entry->is_for_global_symbol())
5983         continue;
5984
5985       Mips_symbol<size>* mips_sym = entry->sym();
5986       if (mips_sym->global_got_area() != GGA_NORMAL)
5987         continue;
5988
5989       unsigned int got_type;
5990       if (!got->multi_got())
5991         got_type = GOT_TYPE_STANDARD;
5992       else
5993         // In multi-GOT links, global symbol can be in both primary and
5994         // secondary GOT(s).  By creating custom GOT type
5995         // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5996         // is added to secondary GOT(s).
5997         got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5998       if (!got->add_global(mips_sym, got_type))
5999         continue;
6000
6001       mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6002       if (got->multi_got() && this->index_ == 0)
6003         count++;
6004       if (got->multi_got() && this->index_ > 0)
6005         {
6006           if (parameters->options().output_is_position_independent()
6007               || (!parameters->doing_static_link()
6008                   && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
6009             {
6010               target->rel_dyn_section(layout)->add_global(
6011                   mips_sym, elfcpp::R_MIPS_REL32, got,
6012                   mips_sym->got_offset(got_type));
6013               got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
6014                                            elfcpp::R_MIPS_REL32, mips_sym);
6015             }
6016         }
6017     }
6018
6019   if (!got->multi_got() || this->index_ == 0)
6020     {
6021       if (got->multi_got())
6022         {
6023           // We need to allocate space in the primary GOT for GGA_NORMAL entries
6024           // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
6025           // entries correspond to dynamic symbol indexes.
6026           while (count < non_reloc_only_global_gotno)
6027             {
6028               got->add_constant(0);
6029               ++count;
6030             }
6031         }
6032
6033       // Add GGA_RELOC_ONLY entries.
6034       got->add_reloc_only_entries();
6035     }
6036 }
6037
6038 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6039
6040 template<int size, bool big_endian>
6041 void
6042 Mips_got_info<size, big_endian>::add_reloc_only_entries(
6043     Mips_output_data_got<size, big_endian>* got)
6044 {
6045   for (typename Global_got_entry_set::iterator
6046        p = this->global_got_symbols_.begin();
6047        p != this->global_got_symbols_.end();
6048        ++p)
6049     {
6050       Mips_symbol<size>* mips_sym = *p;
6051       if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6052         {
6053           unsigned int got_type;
6054           if (!got->multi_got())
6055             got_type = GOT_TYPE_STANDARD;
6056           else
6057             got_type = GOT_TYPE_STANDARD_MULTIGOT;
6058           if (got->add_global(mips_sym, got_type))
6059             mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6060         }
6061     }
6062 }
6063
6064 // Create TLS GOT entries.
6065
6066 template<int size, bool big_endian>
6067 void
6068 Mips_got_info<size, big_endian>::add_tls_entries(
6069     Target_mips<size, big_endian>* target, Layout* layout)
6070 {
6071   Mips_output_data_got<size, big_endian>* got = target->got_section();
6072   // Add local tls entries.
6073   for (typename Got_entry_set::iterator
6074        p = this->got_entries_.begin();
6075        p != this->got_entries_.end();
6076        ++p)
6077     {
6078       Mips_got_entry<size, big_endian>* entry = *p;
6079       if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6080         continue;
6081
6082       if (entry->tls_type() == GOT_TLS_GD)
6083         {
6084           unsigned int got_type = GOT_TYPE_TLS_PAIR;
6085           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6086                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6087           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6088                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6089
6090           if (!parameters->doing_static_link())
6091             {
6092               got->add_local_pair_with_rel(entry->object(), entry->symndx(),
6093                                            entry->shndx(), got_type,
6094                                            target->rel_dyn_section(layout),
6095                                            r_type1, entry->addend());
6096               unsigned int got_offset =
6097                 entry->object()->local_got_offset(entry->symndx(), got_type,
6098                                                   entry->addend());
6099               got->add_static_reloc(got_offset + size/8, r_type2,
6100                                     entry->object(), entry->symndx());
6101             }
6102           else
6103             {
6104               // We are doing a static link.  Mark it as belong to module 1,
6105               // the executable.
6106               unsigned int got_offset = got->add_constant(1);
6107               entry->object()->set_local_got_offset(entry->symndx(), got_type,
6108                                                     got_offset,
6109                                                     entry->addend());
6110               got->add_constant(0);
6111               got->add_static_reloc(got_offset + size/8, r_type2,
6112                                     entry->object(), entry->symndx());
6113             }
6114         }
6115       else if (entry->tls_type() == GOT_TLS_IE)
6116         {
6117           unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6118           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6119                                             : elfcpp::R_MIPS_TLS_TPREL64);
6120           if (!parameters->doing_static_link())
6121             got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
6122                                     target->rel_dyn_section(layout), r_type,
6123                                     entry->addend());
6124           else
6125             {
6126               got->add_local(entry->object(), entry->symndx(), got_type,
6127                              entry->addend());
6128               unsigned int got_offset =
6129                   entry->object()->local_got_offset(entry->symndx(), got_type,
6130                                                     entry->addend());
6131               got->add_static_reloc(got_offset, r_type, entry->object(),
6132                                     entry->symndx());
6133             }
6134         }
6135       else if (entry->tls_type() == GOT_TLS_LDM)
6136         {
6137           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6138                                             : elfcpp::R_MIPS_TLS_DTPMOD64);
6139           unsigned int got_offset;
6140           if (!parameters->doing_static_link())
6141             {
6142               got_offset = got->add_constant(0);
6143               target->rel_dyn_section(layout)->add_local(
6144                   entry->object(), 0, r_type, got, got_offset);
6145             }
6146           else
6147             // We are doing a static link.  Just mark it as belong to module 1,
6148             // the executable.
6149             got_offset = got->add_constant(1);
6150
6151           got->add_constant(0);
6152           got->set_tls_ldm_offset(got_offset, entry->object());
6153         }
6154       else
6155         gold_unreachable();
6156     }
6157
6158   // Add global tls entries.
6159   for (typename Got_entry_set::iterator
6160        p = this->got_entries_.begin();
6161        p != this->got_entries_.end();
6162        ++p)
6163     {
6164       Mips_got_entry<size, big_endian>* entry = *p;
6165       if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6166         continue;
6167
6168       Mips_symbol<size>* mips_sym = entry->sym();
6169       if (entry->tls_type() == GOT_TLS_GD)
6170         {
6171           unsigned int got_type;
6172           if (!got->multi_got())
6173             got_type = GOT_TYPE_TLS_PAIR;
6174           else
6175             got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6176           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6177                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6178           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6179                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6180           if (!parameters->doing_static_link())
6181             got->add_global_pair_with_rel(mips_sym, got_type,
6182                              target->rel_dyn_section(layout), r_type1, r_type2);
6183           else
6184             {
6185               // Add a GOT pair for for R_MIPS_TLS_GD.  The creates a pair of
6186               // GOT entries.  The first one is initialized to be 1, which is the
6187               // module index for the main executable and the second one 0.  A
6188               // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6189               // the second GOT entry and will be applied by gold.
6190               unsigned int got_offset = got->add_constant(1);
6191               mips_sym->set_got_offset(got_type, got_offset);
6192               got->add_constant(0);
6193               got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6194             }
6195         }
6196       else if (entry->tls_type() == GOT_TLS_IE)
6197         {
6198           unsigned int got_type;
6199           if (!got->multi_got())
6200             got_type = GOT_TYPE_TLS_OFFSET;
6201           else
6202             got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
6203           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6204                                             : elfcpp::R_MIPS_TLS_TPREL64);
6205           if (!parameters->doing_static_link())
6206             got->add_global_with_rel(mips_sym, got_type,
6207                                      target->rel_dyn_section(layout), r_type);
6208           else
6209             {
6210               got->add_global(mips_sym, got_type);
6211               unsigned int got_offset = mips_sym->got_offset(got_type);
6212               got->add_static_reloc(got_offset, r_type, mips_sym);
6213             }
6214         }
6215       else
6216         gold_unreachable();
6217     }
6218 }
6219
6220 // Decide whether the symbol needs an entry in the global part of the primary
6221 // GOT, setting global_got_area accordingly.  Count the number of global
6222 // symbols that are in the primary GOT only because they have dynamic
6223 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6224
6225 template<int size, bool big_endian>
6226 void
6227 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6228 {
6229   for (typename Global_got_entry_set::iterator
6230        p = this->global_got_symbols_.begin();
6231        p != this->global_got_symbols_.end();
6232        ++p)
6233     {
6234       Mips_symbol<size>* sym = *p;
6235       // Make a final decision about whether the symbol belongs in the
6236       // local or global GOT.  Symbols that bind locally can (and in the
6237       // case of forced-local symbols, must) live in the local GOT.
6238       // Those that are aren't in the dynamic symbol table must also
6239       // live in the local GOT.
6240
6241       if (!sym->should_add_dynsym_entry(symtab)
6242           || (sym->got_only_for_calls()
6243               ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6244               : symbol_references_local(sym,
6245                                         sym->should_add_dynsym_entry(symtab))))
6246         // The symbol belongs in the local GOT.  We no longer need this
6247         // entry if it was only used for relocations; those relocations
6248         // will be against the null or section symbol instead.
6249         sym->set_global_got_area(GGA_NONE);
6250       else if (sym->global_got_area() == GGA_RELOC_ONLY)
6251         {
6252           ++this->reloc_only_gotno_;
6253           ++this->global_gotno_ ;
6254         }
6255     }
6256 }
6257
6258 // Return the offset of GOT page entry for VALUE.  Initialize the entry with
6259 // VALUE if it is not initialized.
6260
6261 template<int size, bool big_endian>
6262 unsigned int
6263 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6264     Mips_output_data_got<size, big_endian>* got)
6265 {
6266   typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6267   if (it != this->got_page_offsets_.end())
6268     return it->second;
6269
6270   gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6271               + (size/8) * this->page_gotno_);
6272
6273   unsigned int got_offset = this->got_page_offset_next_;
6274   this->got_page_offsets_[value] = got_offset;
6275   this->got_page_offset_next_ += size/8;
6276   got->update_got_entry(got_offset, value);
6277   return got_offset;
6278 }
6279
6280 // Remove lazy-binding stubs for global symbols in this GOT.
6281
6282 template<int size, bool big_endian>
6283 void
6284 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6285     Target_mips<size, big_endian>* target)
6286 {
6287   for (typename Got_entry_set::iterator
6288        p = this->got_entries_.begin();
6289        p != this->got_entries_.end();
6290        ++p)
6291     {
6292       Mips_got_entry<size, big_endian>* entry = *p;
6293       if (entry->is_for_global_symbol())
6294         target->remove_lazy_stub_entry(entry->sym());
6295     }
6296 }
6297
6298 // Count the number of GOT entries required.
6299
6300 template<int size, bool big_endian>
6301 void
6302 Mips_got_info<size, big_endian>::count_got_entries()
6303 {
6304   for (typename Got_entry_set::iterator
6305        p = this->got_entries_.begin();
6306        p != this->got_entries_.end();
6307        ++p)
6308     {
6309       this->count_got_entry(*p);
6310     }
6311 }
6312
6313 // Count the number of GOT entries required by ENTRY.  Accumulate the result.
6314
6315 template<int size, bool big_endian>
6316 void
6317 Mips_got_info<size, big_endian>::count_got_entry(
6318     Mips_got_entry<size, big_endian>* entry)
6319 {
6320   if (entry->is_tls_entry())
6321     this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6322   else if (entry->is_for_local_symbol()
6323            || entry->sym()->global_got_area() == GGA_NONE)
6324     ++this->local_gotno_;
6325   else
6326     ++this->global_gotno_;
6327 }
6328
6329 // Add FROM's GOT entries.
6330
6331 template<int size, bool big_endian>
6332 void
6333 Mips_got_info<size, big_endian>::add_got_entries(
6334     Mips_got_info<size, big_endian>* from)
6335 {
6336   for (typename Got_entry_set::iterator
6337        p = from->got_entries_.begin();
6338        p != from->got_entries_.end();
6339        ++p)
6340     {
6341       Mips_got_entry<size, big_endian>* entry = *p;
6342       if (this->got_entries_.find(entry) == this->got_entries_.end())
6343         {
6344           Mips_got_entry<size, big_endian>* entry2 =
6345             new Mips_got_entry<size, big_endian>(*entry);
6346           this->got_entries_.insert(entry2);
6347           this->count_got_entry(entry);
6348         }
6349     }
6350 }
6351
6352 // Add FROM's GOT page entries.
6353
6354 template<int size, bool big_endian>
6355 void
6356 Mips_got_info<size, big_endian>::add_got_page_entries(
6357     Mips_got_info<size, big_endian>* from)
6358 {
6359   for (typename Got_page_entry_set::iterator
6360        p = from->got_page_entries_.begin();
6361        p != from->got_page_entries_.end();
6362        ++p)
6363     {
6364       Got_page_entry* entry = *p;
6365       if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
6366         {
6367           Got_page_entry* entry2 = new Got_page_entry(*entry);
6368           this->got_page_entries_.insert(entry2);
6369           this->page_gotno_ += entry->num_pages;
6370         }
6371     }
6372 }
6373
6374 // Mips_output_data_got methods.
6375
6376 // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
6377 // larger than 64K, create multi-GOT.
6378
6379 template<int size, bool big_endian>
6380 void
6381 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6382     Symbol_table* symtab, const Input_objects* input_objects)
6383 {
6384   // Decide which symbols need to go in the global part of the GOT and
6385   // count the number of reloc-only GOT symbols.
6386   this->master_got_info_->count_got_symbols(symtab);
6387
6388   // Count the number of GOT entries.
6389   this->master_got_info_->count_got_entries();
6390
6391   unsigned int got_size = this->master_got_info_->got_size();
6392   if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6393     this->lay_out_multi_got(layout, input_objects);
6394   else
6395     {
6396       // Record that all objects use single GOT.
6397       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6398            p != input_objects->relobj_end();
6399            ++p)
6400         {
6401           Mips_relobj<size, big_endian>* object =
6402             Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6403           if (object->get_got_info() != NULL)
6404             object->set_got_info(this->master_got_info_);
6405         }
6406
6407       this->master_got_info_->add_local_entries(this->target_, layout);
6408       this->master_got_info_->add_global_entries(this->target_, layout,
6409                                                  /*not used*/-1U);
6410       this->master_got_info_->add_tls_entries(this->target_, layout);
6411     }
6412 }
6413
6414 // Create multi-GOT.  For every GOT, add local, global and TLS entries.
6415
6416 template<int size, bool big_endian>
6417 void
6418 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6419     const Input_objects* input_objects)
6420 {
6421   // Try to merge the GOTs of input objects together, as long as they
6422   // don't seem to exceed the maximum GOT size, choosing one of them
6423   // to be the primary GOT.
6424   this->merge_gots(input_objects);
6425
6426   // Every symbol that is referenced in a dynamic relocation must be
6427   // present in the primary GOT.
6428   this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6429
6430   // Add GOT entries.
6431   unsigned int i = 0;
6432   unsigned int offset = 0;
6433   Mips_got_info<size, big_endian>* g = this->primary_got_;
6434   do
6435     {
6436       g->set_index(i);
6437       g->set_offset(offset);
6438
6439       g->add_local_entries(this->target_, layout);
6440       if (i == 0)
6441         g->add_global_entries(this->target_, layout,
6442                               (this->master_got_info_->global_gotno()
6443                                - this->master_got_info_->reloc_only_gotno()));
6444       else
6445         g->add_global_entries(this->target_, layout, /*not used*/-1U);
6446       g->add_tls_entries(this->target_, layout);
6447
6448       // Forbid global symbols in every non-primary GOT from having
6449       // lazy-binding stubs.
6450       if (i > 0)
6451         g->remove_lazy_stubs(this->target_);
6452
6453       ++i;
6454       offset += g->got_size();
6455       g = g->next();
6456     }
6457   while (g);
6458 }
6459
6460 // Attempt to merge GOTs of different input objects.  Try to use as much as
6461 // possible of the primary GOT, since it doesn't require explicit dynamic
6462 // relocations, but don't use objects that would reference global symbols
6463 // out of the addressable range.  Failing the primary GOT, attempt to merge
6464 // with the current GOT, or finish the current GOT and then make make the new
6465 // GOT current.
6466
6467 template<int size, bool big_endian>
6468 void
6469 Mips_output_data_got<size, big_endian>::merge_gots(
6470     const Input_objects* input_objects)
6471 {
6472   gold_assert(this->primary_got_ == NULL);
6473   Mips_got_info<size, big_endian>* current = NULL;
6474
6475   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6476        p != input_objects->relobj_end();
6477        ++p)
6478     {
6479       Mips_relobj<size, big_endian>* object =
6480         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6481
6482       Mips_got_info<size, big_endian>* g = object->get_got_info();
6483       if (g == NULL)
6484         continue;
6485
6486       g->count_got_entries();
6487
6488       // Work out the number of page, local and TLS entries.
6489       unsigned int estimate = this->master_got_info_->page_gotno();
6490       if (estimate > g->page_gotno())
6491         estimate = g->page_gotno();
6492       estimate += g->local_gotno() + g->tls_gotno();
6493
6494       // We place TLS GOT entries after both locals and globals.  The globals
6495       // for the primary GOT may overflow the normal GOT size limit, so be
6496       // sure not to merge a GOT which requires TLS with the primary GOT in that
6497       // case.  This doesn't affect non-primary GOTs.
6498       estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6499                                       : g->global_gotno());
6500
6501       unsigned int max_count =
6502         Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6503       if (estimate <= max_count)
6504         {
6505           // If we don't have a primary GOT, use it as
6506           // a starting point for the primary GOT.
6507           if (!this->primary_got_)
6508             {
6509               this->primary_got_ = g;
6510               continue;
6511             }
6512
6513           // Try merging with the primary GOT.
6514           if (this->merge_got_with(g, object, this->primary_got_))
6515             continue;
6516         }
6517
6518       // If we can merge with the last-created GOT, do it.
6519       if (current && this->merge_got_with(g, object, current))
6520         continue;
6521
6522       // Well, we couldn't merge, so create a new GOT.  Don't check if it
6523       // fits; if it turns out that it doesn't, we'll get relocation
6524       // overflows anyway.
6525       g->set_next(current);
6526       current = g;
6527     }
6528
6529   // If we do not find any suitable primary GOT, create an empty one.
6530   if (this->primary_got_ == NULL)
6531     this->primary_got_ = new Mips_got_info<size, big_endian>();
6532
6533   // Link primary GOT with secondary GOTs.
6534   this->primary_got_->set_next(current);
6535 }
6536
6537 // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
6538 // this would lead to overflow, true if they were merged successfully.
6539
6540 template<int size, bool big_endian>
6541 bool
6542 Mips_output_data_got<size, big_endian>::merge_got_with(
6543     Mips_got_info<size, big_endian>* from,
6544     Mips_relobj<size, big_endian>* object,
6545     Mips_got_info<size, big_endian>* to)
6546 {
6547   // Work out how many page entries we would need for the combined GOT.
6548   unsigned int estimate = this->master_got_info_->page_gotno();
6549   if (estimate >= from->page_gotno() + to->page_gotno())
6550     estimate = from->page_gotno() + to->page_gotno();
6551
6552   // Conservatively estimate how many local and TLS entries would be needed.
6553   estimate += from->local_gotno() + to->local_gotno();
6554   estimate += from->tls_gotno() + to->tls_gotno();
6555
6556   // If we're merging with the primary got, any TLS relocations will
6557   // come after the full set of global entries.  Otherwise estimate those
6558   // conservatively as well.
6559   if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6560     estimate += this->master_got_info_->global_gotno();
6561   else
6562     estimate += from->global_gotno() + to->global_gotno();
6563
6564   // Bail out if the combined GOT might be too big.
6565   unsigned int max_count =
6566     Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6567   if (estimate > max_count)
6568     return false;
6569
6570   // Transfer the object's GOT information from FROM to TO.
6571   to->add_got_entries(from);
6572   to->add_got_page_entries(from);
6573
6574   // Record that OBJECT should use output GOT TO.
6575   object->set_got_info(to);
6576
6577   return true;
6578 }
6579
6580 // Write out the GOT.
6581
6582 template<int size, bool big_endian>
6583 void
6584 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6585 {
6586   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6587       Mips_stubs_entry_set;
6588
6589   // Call parent to write out GOT.
6590   Output_data_got<size, big_endian>::do_write(of);
6591
6592   const off_t offset = this->offset();
6593   const section_size_type oview_size =
6594     convert_to_section_size_type(this->data_size());
6595   unsigned char* const oview = of->get_output_view(offset, oview_size);
6596
6597   // Needed for fixing values of .got section.
6598   this->got_view_ = oview;
6599
6600   // Write lazy stub addresses.
6601   for (typename Mips_stubs_entry_set::iterator
6602        p = this->master_got_info_->global_got_symbols().begin();
6603        p != this->master_got_info_->global_got_symbols().end();
6604        ++p)
6605     {
6606       Mips_symbol<size>* mips_sym = *p;
6607       if (mips_sym->has_lazy_stub())
6608         {
6609           Valtype* wv = reinterpret_cast<Valtype*>(
6610             oview + this->get_primary_got_offset(mips_sym));
6611           Valtype value =
6612             this->target_->mips_stubs_section()->stub_address(mips_sym);
6613           elfcpp::Swap<size, big_endian>::writeval(wv, value);
6614         }
6615     }
6616
6617   // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6618   for (typename Mips_stubs_entry_set::iterator
6619        p = this->master_got_info_->global_got_symbols().begin();
6620        p != this->master_got_info_->global_got_symbols().end();
6621        ++p)
6622     {
6623       Mips_symbol<size>* mips_sym = *p;
6624       if (!this->multi_got()
6625           && (mips_sym->is_mips16() || mips_sym->is_micromips())
6626           && mips_sym->global_got_area() == GGA_NONE
6627           && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6628         {
6629           Valtype* wv = reinterpret_cast<Valtype*>(
6630             oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6631           Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6632           if (value != 0)
6633             {
6634               value |= 1;
6635               elfcpp::Swap<size, big_endian>::writeval(wv, value);
6636             }
6637         }
6638     }
6639
6640   if (!this->secondary_got_relocs_.empty())
6641     {
6642       // Fixup for the secondary GOT R_MIPS_REL32 relocs.  For global
6643       // secondary GOT entries with non-zero initial value copy the value
6644       // to the corresponding primary GOT entry, and set the secondary GOT
6645       // entry to zero.
6646       // TODO(sasa): This is workaround.  It needs to be investigated further.
6647
6648       for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6649         {
6650           Static_reloc& reloc(this->secondary_got_relocs_[i]);
6651           if (reloc.symbol_is_global())
6652             {
6653               Mips_symbol<size>* gsym = reloc.symbol();
6654               gold_assert(gsym != NULL);
6655
6656               unsigned got_offset = reloc.got_offset();
6657               gold_assert(got_offset < oview_size);
6658
6659               // Find primary GOT entry.
6660               Valtype* wv_prim = reinterpret_cast<Valtype*>(
6661                 oview + this->get_primary_got_offset(gsym));
6662
6663               // Find secondary GOT entry.
6664               Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6665
6666               Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6667               if (value != 0)
6668                 {
6669                   elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6670                   elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6671                   gsym->set_applied_secondary_got_fixup();
6672                 }
6673             }
6674         }
6675
6676       of->write_output_view(offset, oview_size, oview);
6677     }
6678
6679   // We are done if there is no fix up.
6680   if (this->static_relocs_.empty())
6681     return;
6682
6683   Output_segment* tls_segment = this->layout_->tls_segment();
6684   gold_assert(tls_segment != NULL);
6685
6686   for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6687     {
6688       Static_reloc& reloc(this->static_relocs_[i]);
6689
6690       Mips_address value;
6691       if (!reloc.symbol_is_global())
6692         {
6693           Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6694           const Symbol_value<size>* psymval =
6695             object->local_symbol(reloc.index());
6696
6697           // We are doing static linking.  Issue an error and skip this
6698           // relocation if the symbol is undefined or in a discarded_section.
6699           bool is_ordinary;
6700           unsigned int shndx = psymval->input_shndx(&is_ordinary);
6701           if ((shndx == elfcpp::SHN_UNDEF)
6702               || (is_ordinary
6703                   && shndx != elfcpp::SHN_UNDEF
6704                   && !object->is_section_included(shndx)
6705                   && !this->symbol_table_->is_section_folded(object, shndx)))
6706             {
6707               gold_error(_("undefined or discarded local symbol %u from "
6708                            " object %s in GOT"),
6709                          reloc.index(), reloc.relobj()->name().c_str());
6710               continue;
6711             }
6712
6713           value = psymval->value(object, 0);
6714         }
6715       else
6716         {
6717           const Mips_symbol<size>* gsym = reloc.symbol();
6718           gold_assert(gsym != NULL);
6719
6720           // We are doing static linking.  Issue an error and skip this
6721           // relocation if the symbol is undefined or in a discarded_section
6722           // unless it is a weakly_undefined symbol.
6723           if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6724               && !gsym->is_weak_undefined())
6725             {
6726               gold_error(_("undefined or discarded symbol %s in GOT"),
6727                          gsym->name());
6728               continue;
6729             }
6730
6731           if (!gsym->is_weak_undefined())
6732             value = gsym->value();
6733           else
6734             value = 0;
6735         }
6736
6737       unsigned got_offset = reloc.got_offset();
6738       gold_assert(got_offset < oview_size);
6739
6740       Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6741       Valtype x;
6742
6743       switch (reloc.r_type())
6744         {
6745         case elfcpp::R_MIPS_TLS_DTPMOD32:
6746         case elfcpp::R_MIPS_TLS_DTPMOD64:
6747           x = value;
6748           break;
6749         case elfcpp::R_MIPS_TLS_DTPREL32:
6750         case elfcpp::R_MIPS_TLS_DTPREL64:
6751           x = value - elfcpp::DTP_OFFSET;
6752           break;
6753         case elfcpp::R_MIPS_TLS_TPREL32:
6754         case elfcpp::R_MIPS_TLS_TPREL64:
6755           x = value - elfcpp::TP_OFFSET;
6756           break;
6757         default:
6758           gold_unreachable();
6759           break;
6760         }
6761
6762       elfcpp::Swap<size, big_endian>::writeval(wv, x);
6763     }
6764
6765   of->write_output_view(offset, oview_size, oview);
6766 }
6767
6768 // Mips_relobj methods.
6769
6770 // Count the local symbols.  The Mips backend needs to know if a symbol
6771 // is a MIPS16 or microMIPS function or not.  For global symbols, it is easy
6772 // because the Symbol object keeps the ELF symbol type and st_other field.
6773 // For local symbol it is harder because we cannot access this information.
6774 // So we override the do_count_local_symbol in parent and scan local symbols to
6775 // mark MIPS16 and microMIPS functions.  This is not the most efficient way but
6776 // I do not want to slow down other ports by calling a per symbol target hook
6777 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6778
6779 template<int size, bool big_endian>
6780 void
6781 Mips_relobj<size, big_endian>::do_count_local_symbols(
6782     Stringpool_template<char>* pool,
6783     Stringpool_template<char>* dynpool)
6784 {
6785   // Ask parent to count the local symbols.
6786   Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6787   const unsigned int loccount = this->local_symbol_count();
6788   if (loccount == 0)
6789     return;
6790
6791   // Initialize the mips16 and micromips function bit-vector.
6792   this->local_symbol_is_mips16_.resize(loccount, false);
6793   this->local_symbol_is_micromips_.resize(loccount, false);
6794
6795   // Read the symbol table section header.
6796   const unsigned int symtab_shndx = this->symtab_shndx();
6797   elfcpp::Shdr<size, big_endian>
6798     symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6799   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6800
6801   // Read the local symbols.
6802   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6803   gold_assert(loccount == symtabshdr.get_sh_info());
6804   off_t locsize = loccount * sym_size;
6805   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6806                                               locsize, true, true);
6807
6808   // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6809
6810   // Skip the first dummy symbol.
6811   psyms += sym_size;
6812   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6813     {
6814       elfcpp::Sym<size, big_endian> sym(psyms);
6815       unsigned char st_other = sym.get_st_other();
6816       this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6817       this->local_symbol_is_micromips_[i] =
6818         elfcpp::elf_st_is_micromips(st_other);
6819     }
6820 }
6821
6822 // Read the symbol information.
6823
6824 template<int size, bool big_endian>
6825 void
6826 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6827 {
6828   // Call parent class to read symbol information.
6829   this->base_read_symbols(sd);
6830
6831   // If this input file is a binary file, it has no processor
6832   // specific data.
6833   Input_file::Format format = this->input_file()->format();
6834   if (format != Input_file::FORMAT_ELF)
6835     {
6836       gold_assert(format == Input_file::FORMAT_BINARY);
6837       this->merge_processor_specific_data_ = false;
6838       return;
6839     }
6840
6841   // Read processor-specific flags in ELF file header.
6842   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6843                                             elfcpp::Elf_sizes<size>::ehdr_size,
6844                                             true, false);
6845   elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6846   this->processor_specific_flags_ = ehdr.get_e_flags();
6847
6848   // Get the section names.
6849   const unsigned char* pnamesu = sd->section_names->data();
6850   const char* pnames = reinterpret_cast<const char*>(pnamesu);
6851
6852   // Initialize the mips16 stub section bit-vectors.
6853   this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6854   this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6855   this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6856
6857   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6858   const unsigned char* pshdrs = sd->section_headers->data();
6859   const unsigned char* ps = pshdrs + shdr_size;
6860   bool must_merge_processor_specific_data = false;
6861   for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6862     {
6863       elfcpp::Shdr<size, big_endian> shdr(ps);
6864
6865       // Sometimes an object has no contents except the section name string
6866       // table and an empty symbol table with the undefined symbol.  We
6867       // don't want to merge processor-specific data from such an object.
6868       if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6869         {
6870           // Symbol table is not empty.
6871           const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
6872             elfcpp::Elf_sizes<size>::sym_size;
6873           if (shdr.get_sh_size() > sym_size)
6874             must_merge_processor_specific_data = true;
6875         }
6876       else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6877         // If this is neither an empty symbol table nor a string table,
6878         // be conservative.
6879         must_merge_processor_specific_data = true;
6880
6881       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6882         {
6883           this->has_reginfo_section_ = true;
6884           // Read the gp value that was used to create this object.  We need the
6885           // gp value while processing relocs.  The .reginfo section is not used
6886           // in the 64-bit MIPS ELF ABI.
6887           section_offset_type section_offset = shdr.get_sh_offset();
6888           section_size_type section_size =
6889             convert_to_section_size_type(shdr.get_sh_size());
6890           const unsigned char* view =
6891              this->get_view(section_offset, section_size, true, false);
6892
6893           this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6894
6895           // Read the rest of .reginfo.
6896           this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6897           this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6898           this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6899           this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6900           this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6901         }
6902
6903       if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6904         {
6905           gold_assert(this->attributes_section_data_ == NULL);
6906           section_offset_type section_offset = shdr.get_sh_offset();
6907           section_size_type section_size =
6908             convert_to_section_size_type(shdr.get_sh_size());
6909           const unsigned char* view =
6910             this->get_view(section_offset, section_size, true, false);
6911           this->attributes_section_data_ =
6912             new Attributes_section_data(view, section_size);
6913         }
6914
6915       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6916         {
6917           gold_assert(this->abiflags_ == NULL);
6918           section_offset_type section_offset = shdr.get_sh_offset();
6919           section_size_type section_size =
6920             convert_to_section_size_type(shdr.get_sh_size());
6921           const unsigned char* view =
6922             this->get_view(section_offset, section_size, true, false);
6923           this->abiflags_ = new Mips_abiflags<big_endian>();
6924
6925           this->abiflags_->version =
6926             elfcpp::Swap<16, big_endian>::readval(view);
6927           if (this->abiflags_->version != 0)
6928             {
6929               gold_error(_("%s: .MIPS.abiflags section has "
6930                            "unsupported version %u"),
6931                          this->name().c_str(),
6932                          this->abiflags_->version);
6933               break;
6934             }
6935           this->abiflags_->isa_level =
6936             elfcpp::Swap<8, big_endian>::readval(view + 2);
6937           this->abiflags_->isa_rev =
6938             elfcpp::Swap<8, big_endian>::readval(view + 3);
6939           this->abiflags_->gpr_size =
6940             elfcpp::Swap<8, big_endian>::readval(view + 4);
6941           this->abiflags_->cpr1_size =
6942             elfcpp::Swap<8, big_endian>::readval(view + 5);
6943           this->abiflags_->cpr2_size =
6944             elfcpp::Swap<8, big_endian>::readval(view + 6);
6945           this->abiflags_->fp_abi =
6946             elfcpp::Swap<8, big_endian>::readval(view + 7);
6947           this->abiflags_->isa_ext =
6948             elfcpp::Swap<32, big_endian>::readval(view + 8);
6949           this->abiflags_->ases =
6950             elfcpp::Swap<32, big_endian>::readval(view + 12);
6951           this->abiflags_->flags1 =
6952             elfcpp::Swap<32, big_endian>::readval(view + 16);
6953           this->abiflags_->flags2 =
6954             elfcpp::Swap<32, big_endian>::readval(view + 20);
6955         }
6956
6957       // In the 64-bit ABI, .MIPS.options section holds register information.
6958       // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6959       // starts with this header:
6960       //
6961       // typedef struct
6962       // {
6963       //   // Type of option.
6964       //   unsigned char kind[1];
6965       //   // Size of option descriptor, including header.
6966       //   unsigned char size[1];
6967       //   // Section index of affected section, or 0 for global option.
6968       //   unsigned char section[2];
6969       //   // Information specific to this kind of option.
6970       //   unsigned char info[4];
6971       // };
6972       //
6973       // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6974       // the gp value based on what we find.  We may see both SHT_MIPS_REGINFO
6975       // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6976
6977       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6978         {
6979           section_offset_type section_offset = shdr.get_sh_offset();
6980           section_size_type section_size =
6981             convert_to_section_size_type(shdr.get_sh_size());
6982           const unsigned char* view =
6983              this->get_view(section_offset, section_size, true, false);
6984           const unsigned char* end = view + section_size;
6985
6986           while (view + 8 <= end)
6987             {
6988               unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6989               unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6990               if (sz < 8)
6991                 {
6992                   gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6993                                "than its header"),
6994                              this->name().c_str(),
6995                              this->mips_elf_options_section_name(), sz);
6996                   break;
6997                 }
6998
6999               if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
7000                 {
7001                   // In the 64 bit ABI, an ODK_REGINFO option is the following
7002                   // structure.  The info field of the options header is not
7003                   // used.
7004                   //
7005                   // typedef struct
7006                   // {
7007                   //   // Mask of general purpose registers used.
7008                   //   unsigned char ri_gprmask[4];
7009                   //   // Padding.
7010                   //   unsigned char ri_pad[4];
7011                   //   // Mask of co-processor registers used.
7012                   //   unsigned char ri_cprmask[4][4];
7013                   //   // GP register value for this object file.
7014                   //   unsigned char ri_gp_value[8];
7015                   // };
7016
7017                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7018                                                                       + 32);
7019                 }
7020               else if (kind == elfcpp::ODK_REGINFO)
7021                 {
7022                   // In the 32 bit ABI, an ODK_REGINFO option is the following
7023                   // structure.  The info field of the options header is not
7024                   // used.  The same structure is used in .reginfo section.
7025                   //
7026                   // typedef struct
7027                   // {
7028                   //   unsigned char ri_gprmask[4];
7029                   //   unsigned char ri_cprmask[4][4];
7030                   //   unsigned char ri_gp_value[4];
7031                   // };
7032
7033                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7034                                                                       + 28);
7035                 }
7036               view += sz;
7037             }
7038         }
7039
7040       const char* name = pnames + shdr.get_sh_name();
7041       this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
7042       this->section_is_mips16_call_stub_[i] =
7043         is_prefix_of(".mips16.call.", name);
7044       this->section_is_mips16_call_fp_stub_[i] =
7045         is_prefix_of(".mips16.call.fp.", name);
7046
7047       if (strcmp(name, ".pdr") == 0)
7048         {
7049           gold_assert(this->pdr_shndx_ == -1U);
7050           this->pdr_shndx_ = i;
7051         }
7052     }
7053
7054   // This is rare.
7055   if (!must_merge_processor_specific_data)
7056     this->merge_processor_specific_data_ = false;
7057 }
7058
7059 // Discard MIPS16 stub secions that are not needed.
7060
7061 template<int size, bool big_endian>
7062 void
7063 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7064 {
7065   for (typename Mips16_stubs_int_map::const_iterator
7066        it = this->mips16_stub_sections_.begin();
7067        it != this->mips16_stub_sections_.end(); ++it)
7068     {
7069       Mips16_stub_section<size, big_endian>* stub_section = it->second;
7070       if (!stub_section->is_target_found())
7071         {
7072           gold_error(_("no relocation found in mips16 stub section '%s'"),
7073                      stub_section->object()
7074                        ->section_name(stub_section->shndx()).c_str());
7075         }
7076
7077       bool discard = false;
7078       if (stub_section->is_for_local_function())
7079         {
7080           if (stub_section->is_fn_stub())
7081             {
7082               // This stub is for a local symbol.  This stub will only
7083               // be needed if there is some relocation in this object,
7084               // other than a 16 bit function call, which refers to this
7085               // symbol.
7086               if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7087                 discard = true;
7088               else
7089                 this->add_local_mips16_fn_stub(stub_section);
7090             }
7091           else
7092             {
7093               // This stub is for a local symbol.  This stub will only
7094               // be needed if there is some relocation (R_MIPS16_26) in
7095               // this object that refers to this symbol.
7096               gold_assert(stub_section->is_call_stub()
7097                           || stub_section->is_call_fp_stub());
7098               if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
7099                 discard = true;
7100               else
7101                 this->add_local_mips16_call_stub(stub_section);
7102             }
7103         }
7104       else
7105         {
7106           Mips_symbol<size>* gsym = stub_section->gsym();
7107           if (stub_section->is_fn_stub())
7108             {
7109               if (gsym->has_mips16_fn_stub())
7110                 // We already have a stub for this function.
7111                 discard = true;
7112               else
7113                 {
7114                   gsym->set_mips16_fn_stub(stub_section);
7115                   if (gsym->should_add_dynsym_entry(symtab))
7116                     {
7117                       // If we have a MIPS16 function with a stub, the
7118                       // dynamic symbol must refer to the stub, since only
7119                       // the stub uses the standard calling conventions.
7120                       gsym->set_need_fn_stub();
7121                       if (gsym->is_from_dynobj())
7122                         gsym->set_needs_dynsym_value();
7123                     }
7124                 }
7125               if (!gsym->need_fn_stub())
7126                 discard = true;
7127             }
7128           else if (stub_section->is_call_stub())
7129             {
7130               if (gsym->is_mips16())
7131                 // We don't need the call_stub; this is a 16 bit
7132                 // function, so calls from other 16 bit functions are
7133                 // OK.
7134                 discard = true;
7135               else if (gsym->has_mips16_call_stub())
7136                 // We already have a stub for this function.
7137                 discard = true;
7138               else
7139                 gsym->set_mips16_call_stub(stub_section);
7140             }
7141           else
7142             {
7143               gold_assert(stub_section->is_call_fp_stub());
7144               if (gsym->is_mips16())
7145                 // We don't need the call_stub; this is a 16 bit
7146                 // function, so calls from other 16 bit functions are
7147                 // OK.
7148                 discard = true;
7149               else if (gsym->has_mips16_call_fp_stub())
7150                 // We already have a stub for this function.
7151                 discard = true;
7152               else
7153                 gsym->set_mips16_call_fp_stub(stub_section);
7154             }
7155         }
7156       if (discard)
7157         this->set_output_section(stub_section->shndx(), NULL);
7158    }
7159 }
7160
7161 // Mips_output_data_la25_stub methods.
7162
7163 // Template for standard LA25 stub.
7164 template<int size, bool big_endian>
7165 const uint32_t
7166 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7167 {
7168   0x3c190000,           // lui $25,%hi(func)
7169   0x08000000,           // j func
7170   0x27390000,           // add $25,$25,%lo(func)
7171   0x00000000            // nop
7172 };
7173
7174 // Template for microMIPS LA25 stub.
7175 template<int size, bool big_endian>
7176 const uint32_t
7177 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7178 {
7179   0x41b9, 0x0000,       // lui t9,%hi(func)
7180   0xd400, 0x0000,       // j func
7181   0x3339, 0x0000,       // addiu t9,t9,%lo(func)
7182   0x0000, 0x0000        // nop
7183 };
7184
7185 // Create la25 stub for a symbol.
7186
7187 template<int size, bool big_endian>
7188 void
7189 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7190     Symbol_table* symtab, Target_mips<size, big_endian>* target,
7191     Mips_symbol<size>* gsym)
7192 {
7193   if (!gsym->has_la25_stub())
7194     {
7195       gsym->set_la25_stub_offset(this->symbols_.size() * 16);
7196       this->symbols_.push_back(gsym);
7197       this->create_stub_symbol(gsym, symtab, target, 16);
7198     }
7199 }
7200
7201 // Create a symbol for SYM stub's value and size, to help make the disassembly
7202 // easier to read.
7203
7204 template<int size, bool big_endian>
7205 void
7206 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7207     Mips_symbol<size>* sym, Symbol_table* symtab,
7208     Target_mips<size, big_endian>* target, uint64_t symsize)
7209 {
7210   std::string name(".pic.");
7211   name += sym->name();
7212
7213   unsigned int offset = sym->la25_stub_offset();
7214   if (sym->is_micromips())
7215     offset |= 1;
7216
7217   // Make it a local function.
7218   Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7219                                       Symbol_table::PREDEFINED,
7220                                       target->la25_stub_section(),
7221                                       offset, symsize, elfcpp::STT_FUNC,
7222                                       elfcpp::STB_LOCAL,
7223                                       elfcpp::STV_DEFAULT, 0,
7224                                       false, false);
7225   new_sym->set_is_forced_local();
7226 }
7227
7228 // Write out la25 stubs.  This uses the hand-coded instructions above,
7229 // and adjusts them as needed.
7230
7231 template<int size, bool big_endian>
7232 void
7233 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7234 {
7235   const off_t offset = this->offset();
7236   const section_size_type oview_size =
7237     convert_to_section_size_type(this->data_size());
7238   unsigned char* const oview = of->get_output_view(offset, oview_size);
7239
7240   for (typename std::vector<Mips_symbol<size>*>::iterator
7241        p = this->symbols_.begin();
7242        p != this->symbols_.end();
7243        ++p)
7244     {
7245       Mips_symbol<size>* sym = *p;
7246       unsigned char* pov = oview + sym->la25_stub_offset();
7247
7248       Mips_address target = sym->value();
7249       if (!sym->is_micromips())
7250         {
7251           elfcpp::Swap<32, big_endian>::writeval(pov,
7252               la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7253           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7254               la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7255           elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7256               la25_stub_entry[2] | (target & 0xffff));
7257           elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7258         }
7259       else
7260         {
7261           target |= 1;
7262           // First stub instruction.  Paste high 16-bits of the target.
7263           elfcpp::Swap<16, big_endian>::writeval(pov,
7264                                                  la25_stub_micromips_entry[0]);
7265           elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7266               ((target + 0x8000) >> 16) & 0xffff);
7267           // Second stub instruction.  Paste low 26-bits of the target, shifted
7268           // right by 1.
7269           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7270               la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7271           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7272               la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7273           // Third stub instruction.  Paste low 16-bits of the target.
7274           elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7275                                                  la25_stub_micromips_entry[4]);
7276           elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7277           // Fourth stub instruction.
7278           elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7279                                                  la25_stub_micromips_entry[6]);
7280           elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7281                                                  la25_stub_micromips_entry[7]);
7282         }
7283     }
7284
7285   of->write_output_view(offset, oview_size, oview);
7286 }
7287
7288 // Mips_output_data_plt methods.
7289
7290 // The format of the first PLT entry in an O32 executable.
7291 template<int size, bool big_endian>
7292 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7293 {
7294   0x3c1c0000,         // lui $28, %hi(&GOTPLT[0])
7295   0x8f990000,         // lw $25, %lo(&GOTPLT[0])($28)
7296   0x279c0000,         // addiu $28, $28, %lo(&GOTPLT[0])
7297   0x031cc023,         // subu $24, $24, $28
7298   0x03e07825,         // or $15, $31, zero
7299   0x0018c082,         // srl $24, $24, 2
7300   0x0320f809,         // jalr $25
7301   0x2718fffe          // subu $24, $24, 2
7302 };
7303
7304 // The format of the first PLT entry in an N32 executable.  Different
7305 // because gp ($28) is not available; we use t2 ($14) instead.
7306 template<int size, bool big_endian>
7307 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7308 {
7309   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7310   0x8dd90000,         // lw $25, %lo(&GOTPLT[0])($14)
7311   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7312   0x030ec023,         // subu $24, $24, $14
7313   0x03e07825,         // or $15, $31, zero
7314   0x0018c082,         // srl $24, $24, 2
7315   0x0320f809,         // jalr $25
7316   0x2718fffe          // subu $24, $24, 2
7317 };
7318
7319 // The format of the first PLT entry in an N64 executable.  Different
7320 // from N32 because of the increased size of GOT entries.
7321 template<int size, bool big_endian>
7322 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7323 {
7324   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7325   0xddd90000,         // ld $25, %lo(&GOTPLT[0])($14)
7326   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7327   0x030ec023,         // subu $24, $24, $14
7328   0x03e07825,         // or $15, $31, zero
7329   0x0018c0c2,         // srl $24, $24, 3
7330   0x0320f809,         // jalr $25
7331   0x2718fffe          // subu $24, $24, 2
7332 };
7333
7334 // The format of the microMIPS first PLT entry in an O32 executable.
7335 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7336 // of the GOTPLT entry handled, so this stub may only be used when
7337 // all the subsequent PLT entries are microMIPS code too.
7338 //
7339 // The trailing NOP is for alignment and correct disassembly only.
7340 template<int size, bool big_endian>
7341 const uint32_t Mips_output_data_plt<size, big_endian>::
7342 plt0_entry_micromips_o32[] =
7343 {
7344   0x7980, 0x0000,      // addiupc $3, (&GOTPLT[0]) - .
7345   0xff23, 0x0000,      // lw $25, 0($3)
7346   0x0535,              // subu $2, $2, $3
7347   0x2525,              // srl $2, $2, 2
7348   0x3302, 0xfffe,      // subu $24, $2, 2
7349   0x0dff,              // move $15, $31
7350   0x45f9,              // jalrs $25
7351   0x0f83,              // move $28, $3
7352   0x0c00               // nop
7353 };
7354
7355 // The format of the microMIPS first PLT entry in an O32 executable
7356 // in the insn32 mode.
7357 template<int size, bool big_endian>
7358 const uint32_t Mips_output_data_plt<size, big_endian>::
7359 plt0_entry_micromips32_o32[] =
7360 {
7361   0x41bc, 0x0000,      // lui $28, %hi(&GOTPLT[0])
7362   0xff3c, 0x0000,      // lw $25, %lo(&GOTPLT[0])($28)
7363   0x339c, 0x0000,      // addiu $28, $28, %lo(&GOTPLT[0])
7364   0x0398, 0xc1d0,      // subu $24, $24, $28
7365   0x001f, 0x7a90,      // or $15, $31, zero
7366   0x0318, 0x1040,      // srl $24, $24, 2
7367   0x03f9, 0x0f3c,      // jalr $25
7368   0x3318, 0xfffe       // subu $24, $24, 2
7369 };
7370
7371 // The format of subsequent standard entries in the PLT.
7372 template<int size, bool big_endian>
7373 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7374 {
7375   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7376   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7377   0x03200008,           // jr $25
7378   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7379 };
7380
7381 // The format of subsequent R6 PLT entries.
7382 template<int size, bool big_endian>
7383 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7384 {
7385   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7386   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7387   0x03200009,           // jr $25
7388   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7389 };
7390
7391 // The format of subsequent MIPS16 o32 PLT entries.  We use v1 ($3) as a
7392 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7393 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7394 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7395 // target function address in register v0.
7396 template<int size, bool big_endian>
7397 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7398 {
7399   0xb303,              // lw $3, 12($pc)
7400   0x651b,              // move $24, $3
7401   0x9b60,              // lw $3, 0($3)
7402   0xeb00,              // jr $3
7403   0x653b,              // move $25, $3
7404   0x6500,              // nop
7405   0x0000, 0x0000       // .word (.got.plt entry)
7406 };
7407
7408 // The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
7409 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7410 template<int size, bool big_endian>
7411 const uint32_t Mips_output_data_plt<size, big_endian>::
7412 plt_entry_micromips_o32[] =
7413 {
7414   0x7900, 0x0000,      // addiupc $2, (.got.plt entry) - .
7415   0xff22, 0x0000,      // lw $25, 0($2)
7416   0x4599,              // jr $25
7417   0x0f02               // move $24, $2
7418 };
7419
7420 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7421 template<int size, bool big_endian>
7422 const uint32_t Mips_output_data_plt<size, big_endian>::
7423 plt_entry_micromips32_o32[] =
7424 {
7425   0x41af, 0x0000,      // lui $15, %hi(.got.plt entry)
7426   0xff2f, 0x0000,      // lw $25, %lo(.got.plt entry)($15)
7427   0x0019, 0x0f3c,      // jr $25
7428   0x330f, 0x0000       // addiu $24, $15, %lo(.got.plt entry)
7429 };
7430
7431 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7432
7433 template<int size, bool big_endian>
7434 void
7435 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7436                                                   unsigned int r_type)
7437 {
7438   gold_assert(!gsym->has_plt_offset());
7439
7440   // Final PLT offset for a symbol will be set in method set_plt_offsets().
7441   gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7442                        + sizeof(plt0_entry_o32));
7443   this->symbols_.push_back(gsym);
7444
7445   // Record whether the relocation requires a standard MIPS
7446   // or a compressed code entry.
7447   if (jal_reloc(r_type))
7448    {
7449      if (r_type == elfcpp::R_MIPS_26)
7450        gsym->set_needs_mips_plt(true);
7451      else
7452        gsym->set_needs_comp_plt(true);
7453    }
7454
7455   section_offset_type got_offset = this->got_plt_->current_data_size();
7456
7457   // Every PLT entry needs a GOT entry which points back to the PLT
7458   // entry (this will be changed by the dynamic linker, normally
7459   // lazily when the function is called).
7460   this->got_plt_->set_current_data_size(got_offset + size/8);
7461
7462   gsym->set_needs_dynsym_entry();
7463   this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7464                          got_offset);
7465 }
7466
7467 // Set final PLT offsets.  For each symbol, determine whether standard or
7468 // compressed (MIPS16 or microMIPS) PLT entry is used.
7469
7470 template<int size, bool big_endian>
7471 void
7472 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7473 {
7474   // The sizes of individual PLT entries.
7475   unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7476   unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7477                                       ? this->compressed_plt_entry_size() : 0);
7478
7479   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7480        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7481     {
7482       Mips_symbol<size>* mips_sym = *p;
7483
7484       // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7485       // so always use a standard entry there.
7486       //
7487       // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7488       // all MIPS16 calls will go via that stub, and there is no benefit
7489       // to having a MIPS16 entry.  And in the case of call_stub a
7490       // standard entry actually has to be used as the stub ends with a J
7491       // instruction.
7492       if (this->target_->is_output_newabi()
7493           || mips_sym->has_mips16_call_stub()
7494           || mips_sym->has_mips16_call_fp_stub())
7495         {
7496           mips_sym->set_needs_mips_plt(true);
7497           mips_sym->set_needs_comp_plt(false);
7498         }
7499
7500       // Otherwise, if there are no direct calls to the function, we
7501       // have a free choice of whether to use standard or compressed
7502       // entries.  Prefer microMIPS entries if the object is known to
7503       // contain microMIPS code, so that it becomes possible to create
7504       // pure microMIPS binaries.  Prefer standard entries otherwise,
7505       // because MIPS16 ones are no smaller and are usually slower.
7506       if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7507         {
7508           if (this->target_->is_output_micromips())
7509             mips_sym->set_needs_comp_plt(true);
7510           else
7511             mips_sym->set_needs_mips_plt(true);
7512         }
7513
7514       if (mips_sym->needs_mips_plt())
7515         {
7516           mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7517           this->plt_mips_offset_ += plt_mips_entry_size;
7518         }
7519       if (mips_sym->needs_comp_plt())
7520         {
7521           mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7522           this->plt_comp_offset_ += plt_comp_entry_size;
7523         }
7524     }
7525
7526     // Figure out the size of the PLT header if we know that we are using it.
7527     if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7528       this->plt_header_size_ = this->get_plt_header_size();
7529 }
7530
7531 // Write out the PLT.  This uses the hand-coded instructions above,
7532 // and adjusts them as needed.
7533
7534 template<int size, bool big_endian>
7535 void
7536 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7537 {
7538   const off_t offset = this->offset();
7539   const section_size_type oview_size =
7540     convert_to_section_size_type(this->data_size());
7541   unsigned char* const oview = of->get_output_view(offset, oview_size);
7542
7543   const off_t gotplt_file_offset = this->got_plt_->offset();
7544   const section_size_type gotplt_size =
7545     convert_to_section_size_type(this->got_plt_->data_size());
7546   unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7547                                                          gotplt_size);
7548   unsigned char* pov = oview;
7549
7550   Mips_address plt_address = this->address();
7551
7552   // Calculate the address of .got.plt.
7553   Mips_address gotplt_addr = this->got_plt_->address();
7554   Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7555   Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7556
7557   // The PLT sequence is not safe for N64 if .got.plt's address can
7558   // not be loaded in two instructions.
7559   gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7560               || ~(gotplt_addr | 0x7fffffff) == 0);
7561
7562   // Write the PLT header.
7563   const uint32_t* plt0_entry = this->get_plt_header_entry();
7564   if (plt0_entry == plt0_entry_micromips_o32)
7565     {
7566       // Write microMIPS PLT header.
7567       gold_assert(gotplt_addr % 4 == 0);
7568
7569       Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7570
7571       // ADDIUPC has a span of +/-16MB, check we're in range.
7572       if (gotpc_offset + 0x1000000 >= 0x2000000)
7573        {
7574          gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7575                     "ADDIUPC"), (long)gotpc_offset);
7576          return;
7577        }
7578
7579       elfcpp::Swap<16, big_endian>::writeval(pov,
7580                  plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7581       elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7582                                              (gotpc_offset >> 2) & 0xffff);
7583       pov += 4;
7584       for (unsigned int i = 2;
7585            i < (sizeof(plt0_entry_micromips_o32)
7586                 / sizeof(plt0_entry_micromips_o32[0]));
7587            i++)
7588         {
7589           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7590           pov += 2;
7591         }
7592     }
7593   else if (plt0_entry == plt0_entry_micromips32_o32)
7594     {
7595       // Write microMIPS PLT header in insn32 mode.
7596       elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7597       elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7598       elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7599       elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7600       elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7601       elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7602       pov += 12;
7603       for (unsigned int i = 6;
7604            i < (sizeof(plt0_entry_micromips32_o32)
7605                 / sizeof(plt0_entry_micromips32_o32[0]));
7606            i++)
7607         {
7608           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7609           pov += 2;
7610         }
7611     }
7612   else
7613     {
7614       // Write standard PLT header.
7615       elfcpp::Swap<32, big_endian>::writeval(pov,
7616                                              plt0_entry[0] | gotplt_addr_high);
7617       elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7618                                              plt0_entry[1] | gotplt_addr_low);
7619       elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7620                                              plt0_entry[2] | gotplt_addr_low);
7621       pov += 12;
7622       for (int i = 3; i < 8; i++)
7623         {
7624           elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7625           pov += 4;
7626         }
7627     }
7628
7629
7630   unsigned char* gotplt_pov = gotplt_view;
7631   unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7632
7633   // The first two entries in .got.plt are reserved.
7634   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7635   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7636
7637   unsigned int gotplt_offset = 2 * got_entry_size;
7638   gotplt_pov += 2 * got_entry_size;
7639
7640   // Calculate the address of the PLT header.
7641   Mips_address header_address = (plt_address
7642                                  + (this->is_plt_header_compressed() ? 1 : 0));
7643
7644   // Initialize compressed PLT area view.
7645   unsigned char* pov2 = pov + this->plt_mips_offset_;
7646
7647   // Write the PLT entries.
7648   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7649        p = this->symbols_.begin();
7650        p != this->symbols_.end();
7651        ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7652     {
7653       Mips_symbol<size>* mips_sym = *p;
7654
7655       // Calculate the address of the .got.plt entry.
7656       uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7657       uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7658                                        & 0xffff);
7659       uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7660
7661       // Initially point the .got.plt entry at the PLT header.
7662       if (this->target_->is_output_n64())
7663         elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7664       else
7665         elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7666
7667       // Now handle the PLT itself.  First the standard entry.
7668       if (mips_sym->has_mips_plt_offset())
7669         {
7670           // Pick the load opcode (LW or LD).
7671           uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7672                                                          : 0x8c000000;
7673
7674           const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7675                                                                 : plt_entry;
7676
7677           // Fill in the PLT entry itself.
7678           elfcpp::Swap<32, big_endian>::writeval(pov,
7679               entry[0] | gotplt_entry_addr_hi);
7680           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7681               entry[1] | gotplt_entry_addr_lo | load);
7682           elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
7683           elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7684               entry[3] | gotplt_entry_addr_lo);
7685           pov += 16;
7686         }
7687
7688       // Now the compressed entry.  They come after any standard ones.
7689       if (mips_sym->has_comp_plt_offset())
7690         {
7691           if (!this->target_->is_output_micromips())
7692             {
7693               // Write MIPS16 PLT entry.
7694               const uint32_t* plt_entry = plt_entry_mips16_o32;
7695
7696               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7697               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7698               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7699               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7700               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7701               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7702               elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7703                                                      gotplt_entry_addr);
7704               pov2 += 16;
7705             }
7706           else if (this->target_->use_32bit_micromips_instructions())
7707             {
7708               // Write microMIPS PLT entry in insn32 mode.
7709               const uint32_t* plt_entry = plt_entry_micromips32_o32;
7710
7711               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7712               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7713                                                      gotplt_entry_addr_hi);
7714               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7715               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7716                                                      gotplt_entry_addr_lo);
7717               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7718               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7719               elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7720               elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7721                                                      gotplt_entry_addr_lo);
7722               pov2 += 16;
7723             }
7724           else
7725             {
7726               // Write microMIPS PLT entry.
7727               const uint32_t* plt_entry = plt_entry_micromips_o32;
7728
7729               gold_assert(gotplt_entry_addr % 4 == 0);
7730
7731               Mips_address loc_address = plt_address + pov2 - oview;
7732               int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7733
7734               // ADDIUPC has a span of +/-16MB, check we're in range.
7735               if (gotpc_offset + 0x1000000 >= 0x2000000)
7736                 {
7737                   gold_error(_(".got.plt offset of %ld from .plt beyond the "
7738                              "range of ADDIUPC"), (long)gotpc_offset);
7739                   return;
7740                 }
7741
7742               elfcpp::Swap<16, big_endian>::writeval(pov2,
7743                           plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7744               elfcpp::Swap<16, big_endian>::writeval(
7745                   pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7746               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7747               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7748               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7749               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7750               pov2 += 12;
7751             }
7752         }
7753     }
7754
7755   // Check the number of bytes written for standard entries.
7756   gold_assert(static_cast<section_size_type>(
7757       pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7758   // Check the number of bytes written for compressed entries.
7759   gold_assert((static_cast<section_size_type>(pov2 - pov)
7760                == this->plt_comp_offset_));
7761   // Check the total number of bytes written.
7762   gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7763
7764   gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7765               == gotplt_size);
7766
7767   of->write_output_view(offset, oview_size, oview);
7768   of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7769 }
7770
7771 // Mips_output_data_mips_stubs methods.
7772
7773 // The format of the lazy binding stub when dynamic symbol count is less than
7774 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7775 template<int size, bool big_endian>
7776 const uint32_t
7777 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7778 {
7779   0x8f998010,         // lw t9,0x8010(gp)
7780   0x03e07825,         // or t7,ra,zero
7781   0x0320f809,         // jalr t9,ra
7782   0x24180000          // addiu t8,zero,DYN_INDEX sign extended
7783 };
7784
7785 // The format of the lazy binding stub when dynamic symbol count is less than
7786 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7787 template<int size, bool big_endian>
7788 const uint32_t
7789 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7790 {
7791   0xdf998010,         // ld t9,0x8010(gp)
7792   0x03e07825,         // or t7,ra,zero
7793   0x0320f809,         // jalr t9,ra
7794   0x64180000          // daddiu t8,zero,DYN_INDEX sign extended
7795 };
7796
7797 // The format of the lazy binding stub when dynamic symbol count is less than
7798 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7799 template<int size, bool big_endian>
7800 const uint32_t
7801 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7802 {
7803   0x8f998010,         // lw t9,0x8010(gp)
7804   0x03e07825,         // or t7,ra,zero
7805   0x0320f809,         // jalr t9,ra
7806   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7807 };
7808
7809 // The format of the lazy binding stub when dynamic symbol count is less than
7810 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7811 template<int size, bool big_endian>
7812 const uint32_t
7813 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7814 {
7815   0xdf998010,         // ld t9,0x8010(gp)
7816   0x03e07825,         // or t7,ra,zero
7817   0x0320f809,         // jalr t9,ra
7818   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7819 };
7820
7821 // The format of the lazy binding stub when dynamic symbol count is greater than
7822 // 64K, and ABI is not N64.
7823 template<int size, bool big_endian>
7824 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7825 {
7826   0x8f998010,         // lw t9,0x8010(gp)
7827   0x03e07825,         // or t7,ra,zero
7828   0x3c180000,         // lui t8,DYN_INDEX
7829   0x0320f809,         // jalr t9,ra
7830   0x37180000          // ori t8,t8,DYN_INDEX
7831 };
7832
7833 // The format of the lazy binding stub when dynamic symbol count is greater than
7834 // 64K, and ABI is N64.
7835 template<int size, bool big_endian>
7836 const uint32_t
7837 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7838 {
7839   0xdf998010,         // ld t9,0x8010(gp)
7840   0x03e07825,         // or t7,ra,zero
7841   0x3c180000,         // lui t8,DYN_INDEX
7842   0x0320f809,         // jalr t9,ra
7843   0x37180000          // ori t8,t8,DYN_INDEX
7844 };
7845
7846 // microMIPS stubs.
7847
7848 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7849 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7850 template<int size, bool big_endian>
7851 const uint32_t
7852 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7853 {
7854   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7855   0x0dff,             // move t7,ra
7856   0x45d9,             // jalr t9
7857   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7858 };
7859
7860 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7861 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7862 template<int size, bool big_endian>
7863 const uint32_t
7864 Mips_output_data_mips_stubs<size, big_endian>::
7865 lazy_stub_micromips_normal_1_n64[] =
7866 {
7867   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7868   0x0dff,             // move t7,ra
7869   0x45d9,             // jalr t9
7870   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7871 };
7872
7873 // The format of the microMIPS lazy binding stub when dynamic symbol
7874 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7875 // and ABI is not N64.
7876 template<int size, bool big_endian>
7877 const uint32_t
7878 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7879 {
7880   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7881   0x0dff,             // move t7,ra
7882   0x45d9,             // jalr t9
7883   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7884 };
7885
7886 // The format of the microMIPS lazy binding stub when dynamic symbol
7887 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7888 // and ABI is N64.
7889 template<int size, bool big_endian>
7890 const uint32_t
7891 Mips_output_data_mips_stubs<size, big_endian>::
7892 lazy_stub_micromips_normal_2_n64[] =
7893 {
7894   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7895   0x0dff,             // move t7,ra
7896   0x45d9,             // jalr t9
7897   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7898 };
7899
7900 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7901 // greater than 64K, and ABI is not N64.
7902 template<int size, bool big_endian>
7903 const uint32_t
7904 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7905 {
7906   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7907   0x0dff,             // move t7,ra
7908   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7909   0x45d9,             // jalr t9
7910   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7911 };
7912
7913 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7914 // greater than 64K, and ABI is N64.
7915 template<int size, bool big_endian>
7916 const uint32_t
7917 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7918 {
7919   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7920   0x0dff,             // move t7,ra
7921   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7922   0x45d9,             // jalr t9
7923   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7924 };
7925
7926 // 32-bit microMIPS stubs.
7927
7928 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7929 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7930 // can use only 32-bit instructions.
7931 template<int size, bool big_endian>
7932 const uint32_t
7933 Mips_output_data_mips_stubs<size, big_endian>::
7934 lazy_stub_micromips32_normal_1[] =
7935 {
7936   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7937   0x001f, 0x7a90,     // or t7,ra,zero
7938   0x03f9, 0x0f3c,     // jalr ra,t9
7939   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7940 };
7941
7942 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7943 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7944 // use only 32-bit instructions.
7945 template<int size, bool big_endian>
7946 const uint32_t
7947 Mips_output_data_mips_stubs<size, big_endian>::
7948 lazy_stub_micromips32_normal_1_n64[] =
7949 {
7950   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7951   0x001f, 0x7a90,     // or t7,ra,zero
7952   0x03f9, 0x0f3c,     // jalr ra,t9
7953   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7954 };
7955
7956 // The format of the microMIPS lazy binding stub when dynamic symbol
7957 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7958 // ABI is not N64, and we can use only 32-bit instructions.
7959 template<int size, bool big_endian>
7960 const uint32_t
7961 Mips_output_data_mips_stubs<size, big_endian>::
7962 lazy_stub_micromips32_normal_2[] =
7963 {
7964   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7965   0x001f, 0x7a90,     // or t7,ra,zero
7966   0x03f9, 0x0f3c,     // jalr ra,t9
7967   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7968 };
7969
7970 // The format of the microMIPS lazy binding stub when dynamic symbol
7971 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7972 // ABI is N64, and we can use only 32-bit instructions.
7973 template<int size, bool big_endian>
7974 const uint32_t
7975 Mips_output_data_mips_stubs<size, big_endian>::
7976 lazy_stub_micromips32_normal_2_n64[] =
7977 {
7978   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7979   0x001f, 0x7a90,     // or t7,ra,zero
7980   0x03f9, 0x0f3c,     // jalr ra,t9
7981   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7982 };
7983
7984 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7985 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7986 template<int size, bool big_endian>
7987 const uint32_t
7988 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7989 {
7990   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7991   0x001f, 0x7a90,     // or t7,ra,zero
7992   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7993   0x03f9, 0x0f3c,     // jalr ra,t9
7994   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7995 };
7996
7997 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7998 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7999 template<int size, bool big_endian>
8000 const uint32_t
8001 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
8002 {
8003   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
8004   0x001f, 0x7a90,     // or t7,ra,zero
8005   0x41b8, 0x0000,     // lui t8,DYN_INDEX
8006   0x03f9, 0x0f3c,     // jalr ra,t9
8007   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
8008 };
8009
8010 // Create entry for a symbol.
8011
8012 template<int size, bool big_endian>
8013 void
8014 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
8015     Mips_symbol<size>* gsym)
8016 {
8017   if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
8018     {
8019       this->symbols_.insert(gsym);
8020       gsym->set_has_lazy_stub(true);
8021     }
8022 }
8023
8024 // Remove entry for a symbol.
8025
8026 template<int size, bool big_endian>
8027 void
8028 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8029     Mips_symbol<size>* gsym)
8030 {
8031   if (gsym->has_lazy_stub())
8032     {
8033       this->symbols_.erase(gsym);
8034       gsym->set_has_lazy_stub(false);
8035     }
8036 }
8037
8038 // Set stub offsets for symbols.  This method expects that the number of
8039 // entries in dynamic symbol table is set.
8040
8041 template<int size, bool big_endian>
8042 void
8043 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8044 {
8045   gold_assert(this->dynsym_count_ != -1U);
8046
8047   if (this->stub_offsets_are_set_)
8048     return;
8049
8050   unsigned int stub_size = this->stub_size();
8051   unsigned int offset = 0;
8052   for (typename Mips_stubs_entry_set::const_iterator
8053        p = this->symbols_.begin();
8054        p != this->symbols_.end();
8055        ++p, offset += stub_size)
8056     {
8057       Mips_symbol<size>* mips_sym = *p;
8058       mips_sym->set_lazy_stub_offset(offset);
8059     }
8060   this->stub_offsets_are_set_ = true;
8061 }
8062
8063 template<int size, bool big_endian>
8064 void
8065 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8066 {
8067   for (typename Mips_stubs_entry_set::const_iterator
8068        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8069     {
8070       Mips_symbol<size>* sym = *p;
8071       if (sym->is_from_dynobj())
8072         sym->set_needs_dynsym_value();
8073     }
8074 }
8075
8076 // Write out the .MIPS.stubs.  This uses the hand-coded instructions and
8077 // adjusts them as needed.
8078
8079 template<int size, bool big_endian>
8080 void
8081 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
8082 {
8083   const off_t offset = this->offset();
8084   const section_size_type oview_size =
8085     convert_to_section_size_type(this->data_size());
8086   unsigned char* const oview = of->get_output_view(offset, oview_size);
8087
8088   bool big_stub = this->dynsym_count_ > 0x10000;
8089
8090   unsigned char* pov = oview;
8091   for (typename Mips_stubs_entry_set::const_iterator
8092        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8093     {
8094       Mips_symbol<size>* sym = *p;
8095       const uint32_t* lazy_stub;
8096       bool n64 = this->target_->is_output_n64();
8097
8098       if (!this->target_->is_output_micromips())
8099         {
8100           // Write standard (non-microMIPS) stub.
8101           if (!big_stub)
8102             {
8103               if (sym->dynsym_index() & ~0x7fff)
8104                 // Dynsym index is between 32K and 64K.
8105                 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
8106               else
8107                 // Dynsym index is less than 32K.
8108                 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8109             }
8110           else
8111             lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8112
8113           unsigned int i = 0;
8114           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8115           elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8116           pov += 8;
8117
8118           i += 2;
8119           if (big_stub)
8120             {
8121               // LUI instruction of the big stub.  Paste high 16 bits of the
8122               // dynsym index.
8123               elfcpp::Swap<32, big_endian>::writeval(pov,
8124                   lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
8125               pov += 4;
8126               i += 1;
8127             }
8128           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8129           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8130           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8131               lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8132           pov += 8;
8133         }
8134       else if (this->target_->use_32bit_micromips_instructions())
8135         {
8136           // Write microMIPS stub in insn32 mode.
8137           if (!big_stub)
8138             {
8139               if (sym->dynsym_index() & ~0x7fff)
8140                 // Dynsym index is between 32K and 64K.
8141                 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8142                                 : lazy_stub_micromips32_normal_2;
8143               else
8144                 // Dynsym index is less than 32K.
8145                 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8146                                 : lazy_stub_micromips32_normal_1;
8147             }
8148           else
8149             lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8150                             : lazy_stub_micromips32_big;
8151
8152           unsigned int i = 0;
8153           // First stub instruction.  We emit 32-bit microMIPS instructions by
8154           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8155           // the instruction where the opcode is must always come first, for
8156           // both little and big endian.
8157           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8158           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8159           // Second stub instruction.
8160           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8161           elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8162           pov += 8;
8163           i += 4;
8164           if (big_stub)
8165             {
8166               // LUI instruction of the big stub.  Paste high 16 bits of the
8167               // dynsym index.
8168               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8169               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8170                   (sym->dynsym_index() >> 16) & 0x7fff);
8171               pov += 4;
8172               i += 2;
8173             }
8174           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8175           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8176           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8177           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8178           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8179               sym->dynsym_index() & 0xffff);
8180           pov += 8;
8181         }
8182       else
8183         {
8184           // Write microMIPS stub.
8185           if (!big_stub)
8186             {
8187               if (sym->dynsym_index() & ~0x7fff)
8188                 // Dynsym index is between 32K and 64K.
8189                 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8190                                 : lazy_stub_micromips_normal_2;
8191               else
8192                 // Dynsym index is less than 32K.
8193                 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8194                                 : lazy_stub_micromips_normal_1;
8195             }
8196           else
8197             lazy_stub = n64 ? lazy_stub_micromips_big_n64
8198                             : lazy_stub_micromips_big;
8199
8200           unsigned int i = 0;
8201           // First stub instruction.  We emit 32-bit microMIPS instructions by
8202           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8203           // the instruction where the opcode is must always come first, for
8204           // both little and big endian.
8205           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8206           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8207           // Second stub instruction.
8208           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8209           pov += 6;
8210           i += 3;
8211           if (big_stub)
8212             {
8213               // LUI instruction of the big stub.  Paste high 16 bits of the
8214               // dynsym index.
8215               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8216               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8217                   (sym->dynsym_index() >> 16) & 0x7fff);
8218               pov += 4;
8219               i += 2;
8220             }
8221           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8222           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8223           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8224           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8225               sym->dynsym_index() & 0xffff);
8226           pov += 6;
8227         }
8228     }
8229
8230   // We always allocate 20 bytes for every stub, because final dynsym count is
8231   // not known in method do_finalize_sections.  There are 4 unused bytes per
8232   // stub if final dynsym count is less than 0x10000.
8233   unsigned int used = pov - oview;
8234   unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8235   gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8236
8237   // Fill the unused space with zeroes.
8238   // TODO(sasa): Can we strip unused bytes during the relaxation?
8239   if (unused > 0)
8240     memset(pov, 0, unused);
8241
8242   of->write_output_view(offset, oview_size, oview);
8243 }
8244
8245 // Mips_output_section_reginfo methods.
8246
8247 template<int size, bool big_endian>
8248 void
8249 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8250 {
8251   off_t offset = this->offset();
8252   off_t data_size = this->data_size();
8253
8254   unsigned char* view = of->get_output_view(offset, data_size);
8255   elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8256   elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8257   elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8258   elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8259   elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8260   // Write the gp value.
8261   elfcpp::Swap<size, big_endian>::writeval(view + 20,
8262                                            this->target_->gp_value());
8263
8264   of->write_output_view(offset, data_size, view);
8265 }
8266
8267 // Mips_output_section_options methods.
8268
8269 template<int size, bool big_endian>
8270 void
8271 Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
8272 {
8273   off_t offset = this->offset();
8274   const section_size_type oview_size =
8275     convert_to_section_size_type(this->data_size());
8276   unsigned char* view = of->get_output_view(offset, oview_size);
8277   const unsigned char* end = view + oview_size;
8278
8279   while (view + 8 <= end)
8280     {
8281       unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8282       unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8283       if (sz < 8)
8284         {
8285           gold_error(_("Warning: bad `%s' option size %u smaller "
8286                        "than its header in output section"),
8287                      this->name(), sz);
8288           break;
8289         }
8290
8291       // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
8292       if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
8293         elfcpp::Swap<size, big_endian>::writeval(view + 32,
8294                                                  this->target_->gp_value());
8295       else if (kind == elfcpp::ODK_REGINFO)
8296         elfcpp::Swap<size, big_endian>::writeval(view + 28,
8297                                                  this->target_->gp_value());
8298
8299       view += sz;
8300     }
8301
8302   of->write_output_view(offset, oview_size, view);
8303 }
8304
8305 // Mips_output_section_abiflags methods.
8306
8307 template<int size, bool big_endian>
8308 void
8309 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8310 {
8311   off_t offset = this->offset();
8312   off_t data_size = this->data_size();
8313
8314   unsigned char* view = of->get_output_view(offset, data_size);
8315   elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8316   elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8317   elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8318   elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8319   elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8320   elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8321   elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8322   elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8323   elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8324   elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8325   elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8326
8327   of->write_output_view(offset, data_size, view);
8328 }
8329
8330 // Mips_copy_relocs methods.
8331
8332 // Emit any saved relocs.
8333
8334 template<int sh_type, int size, bool big_endian>
8335 void
8336 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8337     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8338     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8339 {
8340   for (typename Copy_relocs<sh_type, size, big_endian>::
8341        Copy_reloc_entries::iterator p = this->entries_.begin();
8342        p != this->entries_.end();
8343        ++p)
8344     emit_entry(*p, reloc_section, symtab, layout, target);
8345
8346   // We no longer need the saved information.
8347   this->entries_.clear();
8348 }
8349
8350 // Emit the reloc if appropriate.
8351
8352 template<int sh_type, int size, bool big_endian>
8353 void
8354 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8355     Copy_reloc_entry& entry,
8356     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8357     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8358 {
8359   // If the symbol is no longer defined in a dynamic object, then we
8360   // emitted a COPY relocation, and we do not want to emit this
8361   // dynamic relocation.
8362   if (!entry.sym_->is_from_dynobj())
8363     return;
8364
8365   bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8366                            || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8367                            || entry.reloc_type_ == elfcpp::R_MIPS_64);
8368
8369   Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8370   if (can_make_dynamic && !sym->has_static_relocs())
8371     {
8372       Mips_relobj<size, big_endian>* object =
8373         Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8374       target->got_section(symtab, layout)->record_global_got_symbol(
8375                           sym, object, entry.reloc_type_, true, false);
8376       if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8377         target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8378             entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8379       else
8380         target->rel_dyn_section(layout)->add_symbolless_global_addend(
8381             sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8382             entry.shndx_, entry.address_);
8383     }
8384   else
8385     this->make_copy_reloc(symtab, layout,
8386                           static_cast<Sized_symbol<size>*>(entry.sym_),
8387                           entry.relobj_,
8388                           reloc_section);
8389 }
8390
8391 // Target_mips methods.
8392
8393 // Return the value to use for a dynamic symbol which requires special
8394 // treatment.  This is how we support equality comparisons of function
8395 // pointers across shared library boundaries, as described in the
8396 // processor specific ABI supplement.
8397
8398 template<int size, bool big_endian>
8399 uint64_t
8400 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8401 {
8402   uint64_t value = 0;
8403   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8404
8405   if (!mips_sym->has_lazy_stub())
8406     {
8407       if (mips_sym->has_plt_offset())
8408         {
8409           // We distinguish between PLT entries and lazy-binding stubs by
8410           // giving the former an st_other value of STO_MIPS_PLT.  Set the
8411           // value to the stub address if there are any relocations in the
8412           // binary where pointer equality matters.
8413           if (mips_sym->pointer_equality_needed())
8414             {
8415               // Prefer a standard MIPS PLT entry.
8416               if (mips_sym->has_mips_plt_offset())
8417                 value = this->plt_section()->mips_entry_address(mips_sym);
8418               else
8419                 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8420             }
8421           else
8422             value = 0;
8423         }
8424     }
8425   else
8426     {
8427       // First, set stub offsets for symbols.  This method expects that the
8428       // number of entries in dynamic symbol table is set.
8429       this->mips_stubs_section()->set_lazy_stub_offsets();
8430
8431       // The run-time linker uses the st_value field of the symbol
8432       // to reset the global offset table entry for this external
8433       // to its stub address when unlinking a shared object.
8434       value = this->mips_stubs_section()->stub_address(mips_sym);
8435     }
8436
8437   if (mips_sym->has_mips16_fn_stub())
8438     {
8439       // If we have a MIPS16 function with a stub, the dynamic symbol must
8440       // refer to the stub, since only the stub uses the standard calling
8441       // conventions.
8442       value = mips_sym->template
8443               get_mips16_fn_stub<big_endian>()->output_address();
8444     }
8445
8446   return value;
8447 }
8448
8449 // Get the dynamic reloc section, creating it if necessary.  It's always
8450 // .rel.dyn, even for MIPS64.
8451
8452 template<int size, bool big_endian>
8453 typename Target_mips<size, big_endian>::Reloc_section*
8454 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8455 {
8456   if (this->rel_dyn_ == NULL)
8457     {
8458       gold_assert(layout != NULL);
8459       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8460       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8461                                       elfcpp::SHF_ALLOC, this->rel_dyn_,
8462                                       ORDER_DYNAMIC_RELOCS, false);
8463
8464       // First entry in .rel.dyn has to be null.
8465       // This is hack - we define dummy output data and set its address to 0,
8466       // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8467       // This ensures that the entry is null.
8468       Output_data* od = new Output_data_zero_fill(0, 0);
8469       od->set_address(0);
8470       this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8471     }
8472   return this->rel_dyn_;
8473 }
8474
8475 // Get the GOT section, creating it if necessary.
8476
8477 template<int size, bool big_endian>
8478 Mips_output_data_got<size, big_endian>*
8479 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8480                                            Layout* layout)
8481 {
8482   if (this->got_ == NULL)
8483     {
8484       gold_assert(symtab != NULL && layout != NULL);
8485
8486       this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8487                                                               layout);
8488       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8489                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8490                                       elfcpp::SHF_MIPS_GPREL),
8491                                       this->got_, ORDER_DATA, false);
8492
8493       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8494       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8495                                     Symbol_table::PREDEFINED,
8496                                     this->got_,
8497                                     0, 0, elfcpp::STT_OBJECT,
8498                                     elfcpp::STB_GLOBAL,
8499                                     elfcpp::STV_HIDDEN, 0,
8500                                     false, false);
8501     }
8502
8503   return this->got_;
8504 }
8505
8506 // Calculate value of _gp symbol.
8507
8508 template<int size, bool big_endian>
8509 void
8510 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8511 {
8512   gold_assert(this->gp_ == NULL);
8513
8514   Sized_symbol<size>* gp =
8515     static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8516
8517   // Set _gp symbol if the linker script hasn't created it.
8518   if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
8519     {
8520       // If there is no .got section, gp should be based on .sdata.
8521       Output_data* gp_section = (this->got_ != NULL
8522                                  ? this->got_->output_section()
8523                                  : layout->find_output_section(".sdata"));
8524
8525       if (gp_section != NULL)
8526         gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8527                                           "_gp", NULL, Symbol_table::PREDEFINED,
8528                                           gp_section, MIPS_GP_OFFSET, 0,
8529                                           elfcpp::STT_NOTYPE,
8530                                           elfcpp::STB_LOCAL,
8531                                           elfcpp::STV_DEFAULT,
8532                                           0, false, false));
8533     }
8534
8535   this->gp_ = gp;
8536 }
8537
8538 // Set the dynamic symbol indexes.  INDEX is the index of the first
8539 // global dynamic symbol.  Pointers to the symbols are stored into the
8540 // vector SYMS.  The names are added to DYNPOOL.  This returns an
8541 // updated dynamic symbol index.
8542
8543 template<int size, bool big_endian>
8544 unsigned int
8545 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8546     std::vector<Symbol*>* dyn_symbols, unsigned int index,
8547     std::vector<Symbol*>* syms, Stringpool* dynpool,
8548     Versions* versions, Symbol_table* symtab) const
8549 {
8550   std::vector<Symbol*> non_got_symbols;
8551   std::vector<Symbol*> got_symbols;
8552
8553   reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8554                                         &got_symbols);
8555
8556   for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8557        p != non_got_symbols.end();
8558        ++p)
8559     {
8560       Symbol* sym = *p;
8561
8562       // Note that SYM may already have a dynamic symbol index, since
8563       // some symbols appear more than once in the symbol table, with
8564       // and without a version.
8565
8566       if (!sym->has_dynsym_index())
8567         {
8568           sym->set_dynsym_index(index);
8569           ++index;
8570           syms->push_back(sym);
8571           dynpool->add(sym->name(), false, NULL);
8572
8573           // Record any version information.
8574           if (sym->version() != NULL)
8575             versions->record_version(symtab, dynpool, sym);
8576
8577           // If the symbol is defined in a dynamic object and is
8578           // referenced in a regular object, then mark the dynamic
8579           // object as needed.  This is used to implement --as-needed.
8580           if (sym->is_from_dynobj() && sym->in_reg())
8581             sym->object()->set_is_needed();
8582         }
8583     }
8584
8585   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8586        p != got_symbols.end();
8587        ++p)
8588     {
8589       Symbol* sym = *p;
8590       if (!sym->has_dynsym_index())
8591         {
8592           // Record any version information.
8593           if (sym->version() != NULL)
8594             versions->record_version(symtab, dynpool, sym);
8595         }
8596     }
8597
8598   index = versions->finalize(symtab, index, syms);
8599
8600   int got_sym_count = 0;
8601   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8602        p != got_symbols.end();
8603        ++p)
8604     {
8605       Symbol* sym = *p;
8606
8607       if (!sym->has_dynsym_index())
8608         {
8609           ++got_sym_count;
8610           sym->set_dynsym_index(index);
8611           ++index;
8612           syms->push_back(sym);
8613           dynpool->add(sym->name(), false, NULL);
8614
8615           // If the symbol is defined in a dynamic object and is
8616           // referenced in a regular object, then mark the dynamic
8617           // object as needed.  This is used to implement --as-needed.
8618           if (sym->is_from_dynobj() && sym->in_reg())
8619             sym->object()->set_is_needed();
8620         }
8621     }
8622
8623   // Set index of the first symbol that has .got entry.
8624   this->got_->set_first_global_got_dynsym_index(
8625     got_sym_count > 0 ? index - got_sym_count : -1U);
8626
8627   if (this->mips_stubs_ != NULL)
8628     this->mips_stubs_->set_dynsym_count(index);
8629
8630   return index;
8631 }
8632
8633 // Create a PLT entry for a global symbol referenced by r_type relocation.
8634
8635 template<int size, bool big_endian>
8636 void
8637 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8638                                               Layout* layout,
8639                                               Mips_symbol<size>* gsym,
8640                                               unsigned int r_type)
8641 {
8642   if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8643     return;
8644
8645   if (this->plt_ == NULL)
8646     {
8647       // Create the GOT section first.
8648       this->got_section(symtab, layout);
8649
8650       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8651       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8652                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8653                                       this->got_plt_, ORDER_DATA, false);
8654
8655       // The first two entries are reserved.
8656       this->got_plt_->set_current_data_size(2 * size/8);
8657
8658       this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8659                                                               this->got_plt_,
8660                                                               this);
8661       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8662                                       (elfcpp::SHF_ALLOC
8663                                        | elfcpp::SHF_EXECINSTR),
8664                                       this->plt_, ORDER_PLT, false);
8665
8666       // Make the sh_info field of .rel.plt point to .plt.
8667       Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
8668       rel_plt_os->set_info_section(this->plt_->output_section());
8669     }
8670
8671   this->plt_->add_entry(gsym, r_type);
8672 }
8673
8674
8675 // Get the .MIPS.stubs section, creating it if necessary.
8676
8677 template<int size, bool big_endian>
8678 Mips_output_data_mips_stubs<size, big_endian>*
8679 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8680 {
8681   if (this->mips_stubs_ == NULL)
8682     {
8683       this->mips_stubs_ =
8684         new Mips_output_data_mips_stubs<size, big_endian>(this);
8685       layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8686                                       (elfcpp::SHF_ALLOC
8687                                        | elfcpp::SHF_EXECINSTR),
8688                                       this->mips_stubs_, ORDER_PLT, false);
8689     }
8690   return this->mips_stubs_;
8691 }
8692
8693 // Get the LA25 stub section, creating it if necessary.
8694
8695 template<int size, bool big_endian>
8696 Mips_output_data_la25_stub<size, big_endian>*
8697 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8698 {
8699   if (this->la25_stub_ == NULL)
8700     {
8701       this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8702       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8703                                       (elfcpp::SHF_ALLOC
8704                                        | elfcpp::SHF_EXECINSTR),
8705                                       this->la25_stub_, ORDER_TEXT, false);
8706     }
8707   return this->la25_stub_;
8708 }
8709
8710 // Process the relocations to determine unreferenced sections for
8711 // garbage collection.
8712
8713 template<int size, bool big_endian>
8714 void
8715 Target_mips<size, big_endian>::gc_process_relocs(
8716                         Symbol_table* symtab,
8717                         Layout* layout,
8718                         Sized_relobj_file<size, big_endian>* object,
8719                         unsigned int data_shndx,
8720                         unsigned int sh_type,
8721                         const unsigned char* prelocs,
8722                         size_t reloc_count,
8723                         Output_section* output_section,
8724                         bool needs_special_offset_handling,
8725                         size_t local_symbol_count,
8726                         const unsigned char* plocal_symbols)
8727 {
8728   typedef Target_mips<size, big_endian> Mips;
8729
8730   if (sh_type == elfcpp::SHT_REL)
8731     {
8732       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8733           Classify_reloc;
8734
8735       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8736         symtab,
8737         layout,
8738         this,
8739         object,
8740         data_shndx,
8741         prelocs,
8742         reloc_count,
8743         output_section,
8744         needs_special_offset_handling,
8745         local_symbol_count,
8746         plocal_symbols);
8747     }
8748   else if (sh_type == elfcpp::SHT_RELA)
8749     {
8750       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8751           Classify_reloc;
8752
8753       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8754         symtab,
8755         layout,
8756         this,
8757         object,
8758         data_shndx,
8759         prelocs,
8760         reloc_count,
8761         output_section,
8762         needs_special_offset_handling,
8763         local_symbol_count,
8764         plocal_symbols);
8765     }
8766   else
8767     gold_unreachable();
8768 }
8769
8770 // Scan relocations for a section.
8771
8772 template<int size, bool big_endian>
8773 void
8774 Target_mips<size, big_endian>::scan_relocs(
8775                         Symbol_table* symtab,
8776                         Layout* layout,
8777                         Sized_relobj_file<size, big_endian>* object,
8778                         unsigned int data_shndx,
8779                         unsigned int sh_type,
8780                         const unsigned char* prelocs,
8781                         size_t reloc_count,
8782                         Output_section* output_section,
8783                         bool needs_special_offset_handling,
8784                         size_t local_symbol_count,
8785                         const unsigned char* plocal_symbols)
8786 {
8787   typedef Target_mips<size, big_endian> Mips;
8788
8789   if (sh_type == elfcpp::SHT_REL)
8790     {
8791       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8792           Classify_reloc;
8793
8794       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8795         symtab,
8796         layout,
8797         this,
8798         object,
8799         data_shndx,
8800         prelocs,
8801         reloc_count,
8802         output_section,
8803         needs_special_offset_handling,
8804         local_symbol_count,
8805         plocal_symbols);
8806     }
8807   else if (sh_type == elfcpp::SHT_RELA)
8808     {
8809       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8810           Classify_reloc;
8811
8812       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8813         symtab,
8814         layout,
8815         this,
8816         object,
8817         data_shndx,
8818         prelocs,
8819         reloc_count,
8820         output_section,
8821         needs_special_offset_handling,
8822         local_symbol_count,
8823         plocal_symbols);
8824     }
8825 }
8826
8827 template<int size, bool big_endian>
8828 bool
8829 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8830 {
8831   return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8832           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8833           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8834           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8835           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8836           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8837           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
8838           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
8839 }
8840
8841 // Return the MACH for a MIPS e_flags value.
8842 template<int size, bool big_endian>
8843 unsigned int
8844 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8845 {
8846   switch (flags & elfcpp::EF_MIPS_MACH)
8847     {
8848     case elfcpp::E_MIPS_MACH_3900:
8849       return mach_mips3900;
8850
8851     case elfcpp::E_MIPS_MACH_4010:
8852       return mach_mips4010;
8853
8854     case elfcpp::E_MIPS_MACH_4100:
8855       return mach_mips4100;
8856
8857     case elfcpp::E_MIPS_MACH_4111:
8858       return mach_mips4111;
8859
8860     case elfcpp::E_MIPS_MACH_4120:
8861       return mach_mips4120;
8862
8863     case elfcpp::E_MIPS_MACH_4650:
8864       return mach_mips4650;
8865
8866     case elfcpp::E_MIPS_MACH_5400:
8867       return mach_mips5400;
8868
8869     case elfcpp::E_MIPS_MACH_5500:
8870       return mach_mips5500;
8871
8872     case elfcpp::E_MIPS_MACH_5900:
8873       return mach_mips5900;
8874
8875     case elfcpp::E_MIPS_MACH_9000:
8876       return mach_mips9000;
8877
8878     case elfcpp::E_MIPS_MACH_SB1:
8879       return mach_mips_sb1;
8880
8881     case elfcpp::E_MIPS_MACH_LS2E:
8882       return mach_mips_loongson_2e;
8883
8884     case elfcpp::E_MIPS_MACH_LS2F:
8885       return mach_mips_loongson_2f;
8886
8887     case elfcpp::E_MIPS_MACH_LS3A:
8888       return mach_mips_loongson_3a;
8889
8890     case elfcpp::E_MIPS_MACH_OCTEON3:
8891       return mach_mips_octeon3;
8892
8893     case elfcpp::E_MIPS_MACH_OCTEON2:
8894       return mach_mips_octeon2;
8895
8896     case elfcpp::E_MIPS_MACH_OCTEON:
8897       return mach_mips_octeon;
8898
8899     case elfcpp::E_MIPS_MACH_XLR:
8900       return mach_mips_xlr;
8901
8902     default:
8903       switch (flags & elfcpp::EF_MIPS_ARCH)
8904         {
8905         default:
8906         case elfcpp::E_MIPS_ARCH_1:
8907           return mach_mips3000;
8908
8909         case elfcpp::E_MIPS_ARCH_2:
8910           return mach_mips6000;
8911
8912         case elfcpp::E_MIPS_ARCH_3:
8913           return mach_mips4000;
8914
8915         case elfcpp::E_MIPS_ARCH_4:
8916           return mach_mips8000;
8917
8918         case elfcpp::E_MIPS_ARCH_5:
8919           return mach_mips5;
8920
8921         case elfcpp::E_MIPS_ARCH_32:
8922           return mach_mipsisa32;
8923
8924         case elfcpp::E_MIPS_ARCH_64:
8925           return mach_mipsisa64;
8926
8927         case elfcpp::E_MIPS_ARCH_32R2:
8928           return mach_mipsisa32r2;
8929
8930         case elfcpp::E_MIPS_ARCH_32R6:
8931           return mach_mipsisa32r6;
8932
8933         case elfcpp::E_MIPS_ARCH_64R2:
8934           return mach_mipsisa64r2;
8935
8936         case elfcpp::E_MIPS_ARCH_64R6:
8937           return mach_mipsisa64r6;
8938         }
8939     }
8940
8941   return 0;
8942 }
8943
8944 // Return the MACH for each .MIPS.abiflags ISA Extension.
8945
8946 template<int size, bool big_endian>
8947 unsigned int
8948 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8949 {
8950   switch (isa_ext)
8951     {
8952     case elfcpp::AFL_EXT_3900:
8953       return mach_mips3900;
8954
8955     case elfcpp::AFL_EXT_4010:
8956       return mach_mips4010;
8957
8958     case elfcpp::AFL_EXT_4100:
8959       return mach_mips4100;
8960
8961     case elfcpp::AFL_EXT_4111:
8962       return mach_mips4111;
8963
8964     case elfcpp::AFL_EXT_4120:
8965       return mach_mips4120;
8966
8967     case elfcpp::AFL_EXT_4650:
8968       return mach_mips4650;
8969
8970     case elfcpp::AFL_EXT_5400:
8971       return mach_mips5400;
8972
8973     case elfcpp::AFL_EXT_5500:
8974       return mach_mips5500;
8975
8976     case elfcpp::AFL_EXT_5900:
8977       return mach_mips5900;
8978
8979     case elfcpp::AFL_EXT_10000:
8980       return mach_mips10000;
8981
8982     case elfcpp::AFL_EXT_LOONGSON_2E:
8983       return mach_mips_loongson_2e;
8984
8985     case elfcpp::AFL_EXT_LOONGSON_2F:
8986       return mach_mips_loongson_2f;
8987
8988     case elfcpp::AFL_EXT_LOONGSON_3A:
8989       return mach_mips_loongson_3a;
8990
8991     case elfcpp::AFL_EXT_SB1:
8992       return mach_mips_sb1;
8993
8994     case elfcpp::AFL_EXT_OCTEON:
8995       return mach_mips_octeon;
8996
8997     case elfcpp::AFL_EXT_OCTEONP:
8998       return mach_mips_octeonp;
8999
9000     case elfcpp::AFL_EXT_OCTEON2:
9001       return mach_mips_octeon2;
9002
9003     case elfcpp::AFL_EXT_XLR:
9004       return mach_mips_xlr;
9005
9006     default:
9007       return mach_mips3000;
9008     }
9009 }
9010
9011 // Return the .MIPS.abiflags value representing each ISA Extension.
9012
9013 template<int size, bool big_endian>
9014 unsigned int
9015 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
9016 {
9017   switch (mips_mach)
9018     {
9019     case mach_mips3900:
9020       return elfcpp::AFL_EXT_3900;
9021
9022     case mach_mips4010:
9023       return elfcpp::AFL_EXT_4010;
9024
9025     case mach_mips4100:
9026       return elfcpp::AFL_EXT_4100;
9027
9028     case mach_mips4111:
9029       return elfcpp::AFL_EXT_4111;
9030
9031     case mach_mips4120:
9032       return elfcpp::AFL_EXT_4120;
9033
9034     case mach_mips4650:
9035       return elfcpp::AFL_EXT_4650;
9036
9037     case mach_mips5400:
9038       return elfcpp::AFL_EXT_5400;
9039
9040     case mach_mips5500:
9041       return elfcpp::AFL_EXT_5500;
9042
9043     case mach_mips5900:
9044       return elfcpp::AFL_EXT_5900;
9045
9046     case mach_mips10000:
9047       return elfcpp::AFL_EXT_10000;
9048
9049     case mach_mips_loongson_2e:
9050       return elfcpp::AFL_EXT_LOONGSON_2E;
9051
9052     case mach_mips_loongson_2f:
9053       return elfcpp::AFL_EXT_LOONGSON_2F;
9054
9055     case mach_mips_loongson_3a:
9056       return elfcpp::AFL_EXT_LOONGSON_3A;
9057
9058     case mach_mips_sb1:
9059       return elfcpp::AFL_EXT_SB1;
9060
9061     case mach_mips_octeon:
9062       return elfcpp::AFL_EXT_OCTEON;
9063
9064     case mach_mips_octeonp:
9065       return elfcpp::AFL_EXT_OCTEONP;
9066
9067     case mach_mips_octeon3:
9068       return elfcpp::AFL_EXT_OCTEON3;
9069
9070     case mach_mips_octeon2:
9071       return elfcpp::AFL_EXT_OCTEON2;
9072
9073     case mach_mips_xlr:
9074       return elfcpp::AFL_EXT_XLR;
9075
9076     default:
9077       return 0;
9078     }
9079 }
9080
9081 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
9082
9083 template<int size, bool big_endian>
9084 void
9085 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9086     elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9087 {
9088   int new_isa = 0;
9089   switch (e_flags & elfcpp::EF_MIPS_ARCH)
9090     {
9091     case elfcpp::E_MIPS_ARCH_1:
9092       new_isa = this->level_rev(1, 0);
9093       break;
9094     case elfcpp::E_MIPS_ARCH_2:
9095       new_isa = this->level_rev(2, 0);
9096       break;
9097     case elfcpp::E_MIPS_ARCH_3:
9098       new_isa = this->level_rev(3, 0);
9099       break;
9100     case elfcpp::E_MIPS_ARCH_4:
9101       new_isa = this->level_rev(4, 0);
9102       break;
9103     case elfcpp::E_MIPS_ARCH_5:
9104       new_isa = this->level_rev(5, 0);
9105       break;
9106     case elfcpp::E_MIPS_ARCH_32:
9107       new_isa = this->level_rev(32, 1);
9108       break;
9109     case elfcpp::E_MIPS_ARCH_32R2:
9110       new_isa = this->level_rev(32, 2);
9111       break;
9112     case elfcpp::E_MIPS_ARCH_32R6:
9113       new_isa = this->level_rev(32, 6);
9114       break;
9115     case elfcpp::E_MIPS_ARCH_64:
9116       new_isa = this->level_rev(64, 1);
9117       break;
9118     case elfcpp::E_MIPS_ARCH_64R2:
9119       new_isa = this->level_rev(64, 2);
9120       break;
9121     case elfcpp::E_MIPS_ARCH_64R6:
9122       new_isa = this->level_rev(64, 6);
9123       break;
9124     default:
9125       gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9126                  this->elf_mips_mach_name(e_flags));
9127     }
9128
9129   if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9130     {
9131       // Decode a single value into level and revision.
9132       abiflags->isa_level = new_isa >> 3;
9133       abiflags->isa_rev = new_isa & 0x7;
9134     }
9135
9136   // Update the isa_ext if needed.
9137   if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9138       this->elf_mips_mach(e_flags)))
9139     abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9140 }
9141
9142 // Infer the content of the ABI flags based on the elf header.
9143
9144 template<int size, bool big_endian>
9145 void
9146 Target_mips<size, big_endian>::infer_abiflags(
9147     Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9148 {
9149   const Attributes_section_data* pasd = relobj->attributes_section_data();
9150   int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9151   elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9152
9153   this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9154   if (pasd != NULL)
9155     {
9156       // Read fp_abi from the .gnu.attribute section.
9157       const Object_attribute* attr =
9158         pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9159       attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9160     }
9161
9162   abiflags->fp_abi = attr_fp_abi;
9163   abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9164   abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9165   abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9166                                                        : elfcpp::AFL_REG_64;
9167
9168   if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9169       || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9170       || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9171       && abiflags->gpr_size == elfcpp::AFL_REG_32))
9172     abiflags->cpr1_size = elfcpp::AFL_REG_32;
9173   else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9174            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9175            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9176     abiflags->cpr1_size = elfcpp::AFL_REG_64;
9177
9178   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9179     abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9180   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9181     abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9182   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9183     abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9184
9185   if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9186       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9187       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9188       && abiflags->isa_level >= 32
9189       && abiflags->isa_ext != elfcpp::AFL_EXT_LOONGSON_3A)
9190     abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9191 }
9192
9193 // Create abiflags from elf header or from .MIPS.abiflags section.
9194
9195 template<int size, bool big_endian>
9196 void
9197 Target_mips<size, big_endian>::create_abiflags(
9198     Mips_relobj<size, big_endian>* relobj,
9199     Mips_abiflags<big_endian>* abiflags)
9200 {
9201   Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9202   Mips_abiflags<big_endian> header_abiflags;
9203
9204   this->infer_abiflags(relobj, &header_abiflags);
9205
9206   if (sec_abiflags == NULL)
9207     {
9208       // If there is no input .MIPS.abiflags section, use abiflags created
9209       // from elf header.
9210       *abiflags = header_abiflags;
9211       return;
9212     }
9213
9214   this->has_abiflags_section_ = true;
9215
9216   // It is not possible to infer the correct ISA revision for R3 or R5
9217   // so drop down to R2 for the checks.
9218   unsigned char isa_rev = sec_abiflags->isa_rev;
9219   if (isa_rev == 3 || isa_rev == 5)
9220     isa_rev = 2;
9221
9222   // Check compatibility between abiflags created from elf header
9223   // and abiflags from .MIPS.abiflags section in this object file.
9224   if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9225       < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9226     gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9227                  relobj->name().c_str());
9228   if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9229       && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9230     gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9231                    ".MIPS.abiflags"), relobj->name().c_str());
9232   if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9233     gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9234                  relobj->name().c_str());
9235   // The isa_ext is allowed to be an extension of what can be inferred
9236   // from e_flags.
9237   if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9238                                this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9239     gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9240                    ".MIPS.abiflags"), relobj->name().c_str());
9241   if (sec_abiflags->flags2 != 0)
9242     gold_warning(_("%s: Unexpected flag in the flags2 field of "
9243                    ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9244                                              sec_abiflags->flags2);
9245   // Use abiflags from .MIPS.abiflags section.
9246   *abiflags = *sec_abiflags;
9247 }
9248
9249 // Return the meaning of fp_abi, or "unknown" if not known.
9250
9251 template<int size, bool big_endian>
9252 const char*
9253 Target_mips<size, big_endian>::fp_abi_string(int fp)
9254 {
9255   switch (fp)
9256     {
9257     case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9258       return "-mdouble-float";
9259     case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9260       return "-msingle-float";
9261     case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9262       return "-msoft-float";
9263     case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9264       return _("-mips32r2 -mfp64 (12 callee-saved)");
9265     case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9266       return "-mfpxx";
9267     case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9268       return "-mgp32 -mfp64";
9269     case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9270       return "-mgp32 -mfp64 -mno-odd-spreg";
9271     default:
9272       return "unknown";
9273     }
9274 }
9275
9276 // Select fp_abi.
9277
9278 template<int size, bool big_endian>
9279 int
9280 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9281                                              int out_fp)
9282 {
9283   if (in_fp == out_fp)
9284     return out_fp;
9285
9286   if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9287     return in_fp;
9288   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9289            && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9290                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9291                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9292     return in_fp;
9293   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9294            && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9295                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9296                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9297     return out_fp; // Keep the current setting.
9298   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9299            && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9300     return in_fp;
9301   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9302            && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9303     return out_fp; // Keep the current setting.
9304   else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9305     gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9306                  fp_abi_string(in_fp), fp_abi_string(out_fp));
9307   return out_fp;
9308 }
9309
9310 // Merge attributes from input object.
9311
9312 template<int size, bool big_endian>
9313 void
9314 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9315     const Attributes_section_data* pasd)
9316 {
9317   // Return if there is no attributes section data.
9318   if (pasd == NULL)
9319     return;
9320
9321   // If output has no object attributes, just copy.
9322   if (this->attributes_section_data_ == NULL)
9323     {
9324       this->attributes_section_data_ = new Attributes_section_data(*pasd);
9325       return;
9326     }
9327
9328   Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9329       Object_attribute::OBJ_ATTR_GNU);
9330
9331   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9332   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9333
9334   // Merge Tag_compatibility attributes and any common GNU ones.
9335   this->attributes_section_data_->merge(name.c_str(), pasd);
9336 }
9337
9338 // Merge abiflags from input object.
9339
9340 template<int size, bool big_endian>
9341 void
9342 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9343     Mips_abiflags<big_endian>* in_abiflags)
9344 {
9345   // If output has no abiflags, just copy.
9346   if (this->abiflags_ == NULL)
9347   {
9348     this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9349     return;
9350   }
9351
9352   this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9353                                                 this->abiflags_->fp_abi);
9354
9355   // Merge abiflags.
9356   this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9357                                         in_abiflags->isa_level);
9358   this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9359                                       in_abiflags->isa_rev);
9360   this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9361                                        in_abiflags->gpr_size);
9362   this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9363                                         in_abiflags->cpr1_size);
9364   this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9365                                         in_abiflags->cpr2_size);
9366   this->abiflags_->ases |= in_abiflags->ases;
9367   this->abiflags_->flags1 |= in_abiflags->flags1;
9368 }
9369
9370 // Check whether machine EXTENSION is an extension of machine BASE.
9371 template<int size, bool big_endian>
9372 bool
9373 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9374                                                  unsigned int extension)
9375 {
9376   if (extension == base)
9377     return true;
9378
9379   if ((base == mach_mipsisa32)
9380       && this->mips_mach_extends(mach_mipsisa64, extension))
9381     return true;
9382
9383   if ((base == mach_mipsisa32r2)
9384       && this->mips_mach_extends(mach_mipsisa64r2, extension))
9385     return true;
9386
9387   for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9388     if (extension == this->mips_mach_extensions_[i].first)
9389       {
9390         extension = this->mips_mach_extensions_[i].second;
9391         if (extension == base)
9392           return true;
9393       }
9394
9395   return false;
9396 }
9397
9398 // Merge file header flags from input object.
9399
9400 template<int size, bool big_endian>
9401 void
9402 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9403                                                  elfcpp::Elf_Word in_flags)
9404 {
9405   // If flags are not set yet, just copy them.
9406   if (!this->are_processor_specific_flags_set())
9407     {
9408       this->set_processor_specific_flags(in_flags);
9409       this->mach_ = this->elf_mips_mach(in_flags);
9410       return;
9411     }
9412
9413   elfcpp::Elf_Word new_flags = in_flags;
9414   elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9415   elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9416   merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9417
9418   // Check flag compatibility.
9419   new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9420   old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9421
9422   // Some IRIX 6 BSD-compatibility objects have this bit set.  It
9423   // doesn't seem to matter.
9424   new_flags &= ~elfcpp::EF_MIPS_XGOT;
9425   old_flags &= ~elfcpp::EF_MIPS_XGOT;
9426
9427   // MIPSpro generates ucode info in n64 objects.  Again, we should
9428   // just be able to ignore this.
9429   new_flags &= ~elfcpp::EF_MIPS_UCODE;
9430   old_flags &= ~elfcpp::EF_MIPS_UCODE;
9431
9432   if (new_flags == old_flags)
9433     {
9434       this->set_processor_specific_flags(merged_flags);
9435       return;
9436     }
9437
9438   if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9439       != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9440     gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9441                  name.c_str());
9442
9443   if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9444     merged_flags |= elfcpp::EF_MIPS_CPIC;
9445   if (!(new_flags & elfcpp::EF_MIPS_PIC))
9446     merged_flags &= ~elfcpp::EF_MIPS_PIC;
9447
9448   new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9449   old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9450
9451   // Compare the ISAs.
9452   if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9453     gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9454   else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9455     {
9456       // Output ISA isn't the same as, or an extension of, input ISA.
9457       if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9458         {
9459           // Copy the architecture info from input object to output.  Also copy
9460           // the 32-bit flag (if set) so that we continue to recognise
9461           // output as a 32-bit binary.
9462           this->mach_ = this->elf_mips_mach(in_flags);
9463           merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9464           merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9465                            | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9466
9467           // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9468           this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9469
9470           // Copy across the ABI flags if output doesn't use them
9471           // and if that was what caused us to treat input object as 32-bit.
9472           if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9473               && this->mips_32bit_flags(new_flags)
9474               && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9475             merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9476         }
9477       else
9478         // The ISAs aren't compatible.
9479         gold_error(_("%s: linking %s module with previous %s modules"),
9480                    name.c_str(), this->elf_mips_mach_name(in_flags),
9481                    this->elf_mips_mach_name(merged_flags));
9482     }
9483
9484   new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9485                 | elfcpp::EF_MIPS_32BITMODE));
9486   old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9487                 | elfcpp::EF_MIPS_32BITMODE));
9488
9489   // Compare ABIs.
9490   if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9491     {
9492       // Only error if both are set (to different values).
9493       if ((new_flags & elfcpp::EF_MIPS_ABI)
9494            && (old_flags & elfcpp::EF_MIPS_ABI))
9495         gold_error(_("%s: ABI mismatch: linking %s module with "
9496                      "previous %s modules"), name.c_str(),
9497                    this->elf_mips_abi_name(in_flags),
9498                    this->elf_mips_abi_name(merged_flags));
9499
9500       new_flags &= ~elfcpp::EF_MIPS_ABI;
9501       old_flags &= ~elfcpp::EF_MIPS_ABI;
9502     }
9503
9504   // Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
9505   // and allow arbitrary mixing of the remaining ASEs (retain the union).
9506   if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9507       != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9508     {
9509       int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9510       int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9511       int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9512       int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9513       int micro_mis = old_m16 && new_micro;
9514       int m16_mis = old_micro && new_m16;
9515
9516       if (m16_mis || micro_mis)
9517         gold_error(_("%s: ASE mismatch: linking %s module with "
9518                      "previous %s modules"), name.c_str(),
9519                    m16_mis ? "MIPS16" : "microMIPS",
9520                    m16_mis ? "microMIPS" : "MIPS16");
9521
9522       merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9523
9524       new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9525       old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9526     }
9527
9528   // Compare NaN encodings.
9529   if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9530     {
9531       gold_error(_("%s: linking %s module with previous %s modules"),
9532                  name.c_str(),
9533                  (new_flags & elfcpp::EF_MIPS_NAN2008
9534                   ? "-mnan=2008" : "-mnan=legacy"),
9535                  (old_flags & elfcpp::EF_MIPS_NAN2008
9536                   ? "-mnan=2008" : "-mnan=legacy"));
9537
9538       new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9539       old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9540     }
9541
9542   // Compare FP64 state.
9543   if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9544     {
9545       gold_error(_("%s: linking %s module with previous %s modules"),
9546                  name.c_str(),
9547                  (new_flags & elfcpp::EF_MIPS_FP64
9548                   ? "-mfp64" : "-mfp32"),
9549                  (old_flags & elfcpp::EF_MIPS_FP64
9550                   ? "-mfp64" : "-mfp32"));
9551
9552       new_flags &= ~elfcpp::EF_MIPS_FP64;
9553       old_flags &= ~elfcpp::EF_MIPS_FP64;
9554     }
9555
9556   // Warn about any other mismatches.
9557   if (new_flags != old_flags)
9558     gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9559                  "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9560
9561   this->set_processor_specific_flags(merged_flags);
9562 }
9563
9564 // Adjust ELF file header.
9565
9566 template<int size, bool big_endian>
9567 void
9568 Target_mips<size, big_endian>::do_adjust_elf_header(
9569     unsigned char* view,
9570     int len)
9571 {
9572   gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9573
9574   elfcpp::Ehdr<size, big_endian> ehdr(view);
9575   unsigned char e_ident[elfcpp::EI_NIDENT];
9576   elfcpp::Elf_Word flags = this->processor_specific_flags();
9577   memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9578
9579   unsigned char ei_abiversion = 0;
9580   elfcpp::Elf_Half type = ehdr.get_e_type();
9581   if (type == elfcpp::ET_EXEC
9582       && parameters->options().copyreloc()
9583       && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9584           == elfcpp::EF_MIPS_CPIC)
9585     ei_abiversion = 1;
9586
9587   if (this->abiflags_ != NULL
9588       && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9589           || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9590     ei_abiversion = 3;
9591
9592   e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9593   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9594   oehdr.put_e_ident(e_ident);
9595
9596   if (this->entry_symbol_is_compressed_)
9597     oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9598 }
9599
9600 // do_make_elf_object to override the same function in the base class.
9601 // We need to use a target-specific sub-class of
9602 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9603 // Hence we need to have our own ELF object creation.
9604
9605 template<int size, bool big_endian>
9606 Object*
9607 Target_mips<size, big_endian>::do_make_elf_object(
9608     const std::string& name,
9609     Input_file* input_file,
9610     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9611 {
9612   int et = ehdr.get_e_type();
9613   // ET_EXEC files are valid input for --just-symbols/-R,
9614   // and we treat them as relocatable objects.
9615   if (et == elfcpp::ET_REL
9616       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9617     {
9618       Mips_relobj<size, big_endian>* obj =
9619         new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9620       obj->setup();
9621       return obj;
9622     }
9623   else if (et == elfcpp::ET_DYN)
9624     {
9625       // TODO(sasa): Should we create Mips_dynobj?
9626       return Target::do_make_elf_object(name, input_file, offset, ehdr);
9627     }
9628   else
9629     {
9630       gold_error(_("%s: unsupported ELF file type %d"),
9631                  name.c_str(), et);
9632       return NULL;
9633     }
9634 }
9635
9636 // Finalize the sections.
9637
9638 template <int size, bool big_endian>
9639 void
9640 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9641                                         const Input_objects* input_objects,
9642                                         Symbol_table* symtab)
9643 {
9644   const bool relocatable = parameters->options().relocatable();
9645
9646   // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9647   // DT_FINI have correct values.
9648   Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9649       symtab->lookup(parameters->options().init()));
9650   if (init != NULL && (init->is_mips16() || init->is_micromips()))
9651     init->set_value(init->value() | 1);
9652   Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9653       symtab->lookup(parameters->options().fini()));
9654   if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9655     fini->set_value(fini->value() | 1);
9656
9657   // Check whether the entry symbol is mips16 or micromips.  This is needed to
9658   // adjust entry address in ELF header.
9659   Mips_symbol<size>* entry =
9660     static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9661   this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9662                                        || entry->is_micromips()));
9663
9664   if (!parameters->doing_static_link()
9665       && (strcmp(parameters->options().hash_style(), "gnu") == 0
9666           || strcmp(parameters->options().hash_style(), "both") == 0))
9667     {
9668       // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9669       // ways.  .gnu.hash needs symbols to be grouped by hash code whereas the
9670       // MIPS ABI requires a mapping between the GOT and the symbol table.
9671       gold_error(".gnu.hash is incompatible with the MIPS ABI");
9672     }
9673
9674   // Check whether the final section that was scanned has HI16 or GOT16
9675   // relocations without the corresponding LO16 part.
9676   if (this->got16_addends_.size() > 0)
9677       gold_error("Can't find matching LO16 reloc");
9678
9679   Valtype gprmask = 0;
9680   Valtype cprmask1 = 0;
9681   Valtype cprmask2 = 0;
9682   Valtype cprmask3 = 0;
9683   Valtype cprmask4 = 0;
9684   bool has_reginfo_section = false;
9685
9686   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9687        p != input_objects->relobj_end();
9688        ++p)
9689     {
9690       Mips_relobj<size, big_endian>* relobj =
9691         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9692
9693       // Check for any mips16 stub sections that we can discard.
9694       if (!relocatable)
9695         relobj->discard_mips16_stub_sections(symtab);
9696
9697       if (!relobj->merge_processor_specific_data())
9698         continue;
9699
9700       // Merge .reginfo contents of input objects.
9701       if (relobj->has_reginfo_section())
9702         {
9703           has_reginfo_section = true;
9704           gprmask |= relobj->gprmask();
9705           cprmask1 |= relobj->cprmask1();
9706           cprmask2 |= relobj->cprmask2();
9707           cprmask3 |= relobj->cprmask3();
9708           cprmask4 |= relobj->cprmask4();
9709         }
9710
9711       // Merge processor specific flags.
9712       Mips_abiflags<big_endian> in_abiflags;
9713
9714       this->create_abiflags(relobj, &in_abiflags);
9715       this->merge_obj_e_flags(relobj->name(),
9716                               relobj->processor_specific_flags());
9717       this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9718       this->merge_obj_attributes(relobj->name(),
9719                                  relobj->attributes_section_data());
9720     }
9721
9722   // Create a .gnu.attributes section if we have merged any attributes
9723   // from inputs.
9724   if (this->attributes_section_data_ != NULL)
9725     {
9726       Output_attributes_section_data* attributes_section =
9727         new Output_attributes_section_data(*this->attributes_section_data_);
9728       layout->add_output_section_data(".gnu.attributes",
9729                                       elfcpp::SHT_GNU_ATTRIBUTES, 0,
9730                                       attributes_section, ORDER_INVALID, false);
9731     }
9732
9733   // Create .MIPS.abiflags output section if there is an input section.
9734   if (this->has_abiflags_section_)
9735     {
9736       Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9737         new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9738
9739       Output_section* os =
9740         layout->add_output_section_data(".MIPS.abiflags",
9741                                         elfcpp::SHT_MIPS_ABIFLAGS,
9742                                         elfcpp::SHF_ALLOC,
9743                                         abiflags_section, ORDER_INVALID, false);
9744
9745       if (!relocatable && os != NULL)
9746         {
9747           Output_segment* abiflags_segment =
9748             layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9749           abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9750         }
9751     }
9752
9753   if (has_reginfo_section && !parameters->options().gc_sections())
9754     {
9755       // Create .reginfo output section.
9756       Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9757         new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9758                                                           cprmask1, cprmask2,
9759                                                           cprmask3, cprmask4);
9760
9761       Output_section* os =
9762         layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9763                                         elfcpp::SHF_ALLOC, reginfo_section,
9764                                         ORDER_INVALID, false);
9765
9766       if (!relocatable && os != NULL)
9767         {
9768           Output_segment* reginfo_segment =
9769             layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9770                                         elfcpp::PF_R);
9771           reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9772         }
9773     }
9774
9775   if (this->plt_ != NULL)
9776     {
9777       // Set final PLT offsets for symbols.
9778       this->plt_section()->set_plt_offsets();
9779
9780       // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9781       // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9782       // there are no standard PLT entries present.
9783       unsigned char nonvis = 0;
9784       if (this->is_output_micromips()
9785           && !this->plt_section()->has_standard_entries())
9786         nonvis = elfcpp::STO_MICROMIPS >> 2;
9787       symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9788                                     Symbol_table::PREDEFINED,
9789                                     this->plt_,
9790                                     0, 0, elfcpp::STT_FUNC,
9791                                     elfcpp::STB_LOCAL,
9792                                     elfcpp::STV_DEFAULT, nonvis,
9793                                     false, false);
9794     }
9795
9796   if (this->mips_stubs_ != NULL)
9797     {
9798       // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9799       unsigned char nonvis = 0;
9800       if (this->is_output_micromips())
9801         nonvis = elfcpp::STO_MICROMIPS >> 2;
9802       symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9803                                     Symbol_table::PREDEFINED,
9804                                     this->mips_stubs_,
9805                                     0, 0, elfcpp::STT_FUNC,
9806                                     elfcpp::STB_LOCAL,
9807                                     elfcpp::STV_DEFAULT, nonvis,
9808                                     false, false);
9809     }
9810
9811   if (!relocatable && !parameters->doing_static_link())
9812     // In case there is no .got section, create one.
9813     this->got_section(symtab, layout);
9814
9815   // Emit any relocs we saved in an attempt to avoid generating COPY
9816   // relocs.
9817   if (this->copy_relocs_.any_saved_relocs())
9818     this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9819                                  this);
9820
9821   // Set _gp value.
9822   this->set_gp(layout, symtab);
9823
9824   // Emit dynamic relocs.
9825   for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9826        p != this->dyn_relocs_.end();
9827        ++p)
9828     p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9829
9830   if (this->has_got_section())
9831     this->got_section()->lay_out_got(layout, symtab, input_objects);
9832
9833   if (this->mips_stubs_ != NULL)
9834     this->mips_stubs_->set_needs_dynsym_value();
9835
9836   // Check for functions that might need $25 to be valid on entry.
9837   // TODO(sasa): Can we do this without iterating over all symbols?
9838   typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9839   symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9840                                                                symtab));
9841
9842   // Add NULL segment.
9843   if (!relocatable)
9844     layout->make_output_segment(elfcpp::PT_NULL, 0);
9845
9846   // Fill in some more dynamic tags.
9847   // TODO(sasa): Add more dynamic tags.
9848   const Reloc_section* rel_plt = (this->plt_ == NULL
9849                                   ? NULL : this->plt_->rel_plt());
9850   layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9851                                   this->rel_dyn_, true, false);
9852
9853   Output_data_dynamic* const odyn = layout->dynamic_data();
9854   if (odyn != NULL
9855       && !relocatable
9856       && !parameters->doing_static_link())
9857   {
9858     unsigned int d_val;
9859     // This element holds a 32-bit version id for the Runtime
9860     // Linker Interface.  This will start at integer value 1.
9861     d_val = 0x01;
9862     odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9863
9864     // Dynamic flags
9865     d_val = elfcpp::RHF_NOTPOT;
9866     odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9867
9868     // Save layout for using when emitting custom dynamic tags.
9869     this->layout_ = layout;
9870
9871     // This member holds the base address of the segment.
9872     odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9873
9874     // This member holds the number of entries in the .dynsym section.
9875     odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9876
9877     // This member holds the index of the first dynamic symbol
9878     // table entry that corresponds to an entry in the global offset table.
9879     odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9880
9881     // This member holds the number of local GOT entries.
9882     odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9883                        this->got_->get_local_gotno());
9884
9885     if (this->plt_ != NULL)
9886       // DT_MIPS_PLTGOT dynamic tag
9887       odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9888
9889     if (!parameters->options().shared())
9890       {
9891         this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9892
9893         layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9894                                         (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9895                                         this->rld_map_, ORDER_INVALID, false);
9896
9897         // __RLD_MAP will be filled in by the runtime loader to contain
9898         // a pointer to the _r_debug structure.
9899         Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9900                                             Symbol_table::PREDEFINED,
9901                                             this->rld_map_,
9902                                             0, 0, elfcpp::STT_OBJECT,
9903                                             elfcpp::STB_GLOBAL,
9904                                             elfcpp::STV_DEFAULT, 0,
9905                                             false, false);
9906
9907         if (!rld_map->is_forced_local())
9908           rld_map->set_needs_dynsym_entry();
9909
9910         if (!parameters->options().pie())
9911           // This member holds the absolute address of the debug pointer.
9912           odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9913         else
9914           // This member holds the offset to the debug pointer,
9915           // relative to the address of the tag.
9916           odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9917       }
9918   }
9919 }
9920
9921 // Get the custom dynamic tag value.
9922 template<int size, bool big_endian>
9923 unsigned int
9924 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9925 {
9926   switch (tag)
9927     {
9928     case elfcpp::DT_MIPS_BASE_ADDRESS:
9929       {
9930         // The base address of the segment.
9931         // At this point, the segment list has been sorted into final order,
9932         // so just return vaddr of the first readable PT_LOAD segment.
9933         Output_segment* seg =
9934           this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9935         gold_assert(seg != NULL);
9936         return seg->vaddr();
9937       }
9938
9939     case elfcpp::DT_MIPS_SYMTABNO:
9940       // The number of entries in the .dynsym section.
9941       return this->get_dt_mips_symtabno();
9942
9943     case elfcpp::DT_MIPS_GOTSYM:
9944       {
9945         // The index of the first dynamic symbol table entry that corresponds
9946         // to an entry in the GOT.
9947         if (this->got_->first_global_got_dynsym_index() != -1U)
9948           return this->got_->first_global_got_dynsym_index();
9949         else
9950           // In case if we don't have global GOT symbols we default to setting
9951           // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9952           return this->get_dt_mips_symtabno();
9953       }
9954
9955     case elfcpp::DT_MIPS_RLD_MAP_REL:
9956       {
9957         // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9958         // relative to the address of the tag.
9959         Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9960         unsigned int entry_offset =
9961           odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9962         gold_assert(entry_offset != -1U);
9963         return this->rld_map_->address() - (odyn->address() + entry_offset);
9964       }
9965     default:
9966       gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9967     }
9968
9969   return (unsigned int)-1;
9970 }
9971
9972 // Relocate section data.
9973
9974 template<int size, bool big_endian>
9975 void
9976 Target_mips<size, big_endian>::relocate_section(
9977                         const Relocate_info<size, big_endian>* relinfo,
9978                         unsigned int sh_type,
9979                         const unsigned char* prelocs,
9980                         size_t reloc_count,
9981                         Output_section* output_section,
9982                         bool needs_special_offset_handling,
9983                         unsigned char* view,
9984                         Mips_address address,
9985                         section_size_type view_size,
9986                         const Reloc_symbol_changes* reloc_symbol_changes)
9987 {
9988   typedef Target_mips<size, big_endian> Mips;
9989   typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9990
9991   if (sh_type == elfcpp::SHT_REL)
9992     {
9993       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9994           Classify_reloc;
9995
9996       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9997                              gold::Default_comdat_behavior, Classify_reloc>(
9998         relinfo,
9999         this,
10000         prelocs,
10001         reloc_count,
10002         output_section,
10003         needs_special_offset_handling,
10004         view,
10005         address,
10006         view_size,
10007         reloc_symbol_changes);
10008     }
10009   else if (sh_type == elfcpp::SHT_RELA)
10010     {
10011       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10012           Classify_reloc;
10013
10014       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
10015                              gold::Default_comdat_behavior, Classify_reloc>(
10016         relinfo,
10017         this,
10018         prelocs,
10019         reloc_count,
10020         output_section,
10021         needs_special_offset_handling,
10022         view,
10023         address,
10024         view_size,
10025         reloc_symbol_changes);
10026     }
10027 }
10028
10029 // Return the size of a relocation while scanning during a relocatable
10030 // link.
10031
10032 unsigned int
10033 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
10034 {
10035   switch (r_type)
10036     {
10037     case elfcpp::R_MIPS_NONE:
10038     case elfcpp::R_MIPS_TLS_DTPMOD64:
10039     case elfcpp::R_MIPS_TLS_DTPREL64:
10040     case elfcpp::R_MIPS_TLS_TPREL64:
10041       return 0;
10042
10043     case elfcpp::R_MIPS_32:
10044     case elfcpp::R_MIPS_TLS_DTPMOD32:
10045     case elfcpp::R_MIPS_TLS_DTPREL32:
10046     case elfcpp::R_MIPS_TLS_TPREL32:
10047     case elfcpp::R_MIPS_REL32:
10048     case elfcpp::R_MIPS_PC32:
10049     case elfcpp::R_MIPS_GPREL32:
10050     case elfcpp::R_MIPS_JALR:
10051     case elfcpp::R_MIPS_EH:
10052       return 4;
10053
10054     case elfcpp::R_MIPS_16:
10055     case elfcpp::R_MIPS_HI16:
10056     case elfcpp::R_MIPS_LO16:
10057     case elfcpp::R_MIPS_HIGHER:
10058     case elfcpp::R_MIPS_HIGHEST:
10059     case elfcpp::R_MIPS_GPREL16:
10060     case elfcpp::R_MIPS16_HI16:
10061     case elfcpp::R_MIPS16_LO16:
10062     case elfcpp::R_MIPS_PC16:
10063     case elfcpp::R_MIPS_PCHI16:
10064     case elfcpp::R_MIPS_PCLO16:
10065     case elfcpp::R_MIPS_GOT16:
10066     case elfcpp::R_MIPS16_GOT16:
10067     case elfcpp::R_MIPS_CALL16:
10068     case elfcpp::R_MIPS16_CALL16:
10069     case elfcpp::R_MIPS_GOT_HI16:
10070     case elfcpp::R_MIPS_CALL_HI16:
10071     case elfcpp::R_MIPS_GOT_LO16:
10072     case elfcpp::R_MIPS_CALL_LO16:
10073     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10074     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10075     case elfcpp::R_MIPS_TLS_TPREL_HI16:
10076     case elfcpp::R_MIPS_TLS_TPREL_LO16:
10077     case elfcpp::R_MIPS16_GPREL:
10078     case elfcpp::R_MIPS_GOT_DISP:
10079     case elfcpp::R_MIPS_LITERAL:
10080     case elfcpp::R_MIPS_GOT_PAGE:
10081     case elfcpp::R_MIPS_GOT_OFST:
10082     case elfcpp::R_MIPS_TLS_GD:
10083     case elfcpp::R_MIPS_TLS_LDM:
10084     case elfcpp::R_MIPS_TLS_GOTTPREL:
10085       return 2;
10086
10087     // These relocations are not byte sized
10088     case elfcpp::R_MIPS_26:
10089     case elfcpp::R_MIPS16_26:
10090     case elfcpp::R_MIPS_PC21_S2:
10091     case elfcpp::R_MIPS_PC26_S2:
10092     case elfcpp::R_MIPS_PC18_S3:
10093     case elfcpp::R_MIPS_PC19_S2:
10094       return 4;
10095
10096     case elfcpp::R_MIPS_COPY:
10097     case elfcpp::R_MIPS_JUMP_SLOT:
10098       object->error(_("unexpected reloc %u in object file"), r_type);
10099       return 0;
10100
10101     default:
10102       object->error(_("unsupported reloc %u in object file"), r_type);
10103       return 0;
10104   }
10105 }
10106
10107 // Scan the relocs during a relocatable link.
10108
10109 template<int size, bool big_endian>
10110 void
10111 Target_mips<size, big_endian>::scan_relocatable_relocs(
10112                         Symbol_table* symtab,
10113                         Layout* layout,
10114                         Sized_relobj_file<size, big_endian>* object,
10115                         unsigned int data_shndx,
10116                         unsigned int sh_type,
10117                         const unsigned char* prelocs,
10118                         size_t reloc_count,
10119                         Output_section* output_section,
10120                         bool needs_special_offset_handling,
10121                         size_t local_symbol_count,
10122                         const unsigned char* plocal_symbols,
10123                         Relocatable_relocs* rr)
10124 {
10125   if (sh_type == elfcpp::SHT_REL)
10126     {
10127       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10128           Classify_reloc;
10129       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10130           Scan_relocatable_relocs;
10131
10132       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10133         symtab,
10134         layout,
10135         object,
10136         data_shndx,
10137         prelocs,
10138         reloc_count,
10139         output_section,
10140         needs_special_offset_handling,
10141         local_symbol_count,
10142         plocal_symbols,
10143         rr);
10144     }
10145   else if (sh_type == elfcpp::SHT_RELA)
10146     {
10147       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10148           Classify_reloc;
10149       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10150           Scan_relocatable_relocs;
10151
10152       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10153         symtab,
10154         layout,
10155         object,
10156         data_shndx,
10157         prelocs,
10158         reloc_count,
10159         output_section,
10160         needs_special_offset_handling,
10161         local_symbol_count,
10162         plocal_symbols,
10163         rr);
10164     }
10165   else
10166     gold_unreachable();
10167 }
10168
10169 // Scan the relocs for --emit-relocs.
10170
10171 template<int size, bool big_endian>
10172 void
10173 Target_mips<size, big_endian>::emit_relocs_scan(
10174     Symbol_table* symtab,
10175     Layout* layout,
10176     Sized_relobj_file<size, big_endian>* object,
10177     unsigned int data_shndx,
10178     unsigned int sh_type,
10179     const unsigned char* prelocs,
10180     size_t reloc_count,
10181     Output_section* output_section,
10182     bool needs_special_offset_handling,
10183     size_t local_symbol_count,
10184     const unsigned char* plocal_syms,
10185     Relocatable_relocs* rr)
10186 {
10187   if (sh_type == elfcpp::SHT_REL)
10188     {
10189       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10190           Classify_reloc;
10191       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10192           Emit_relocs_strategy;
10193
10194       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10195         symtab,
10196         layout,
10197         object,
10198         data_shndx,
10199         prelocs,
10200         reloc_count,
10201         output_section,
10202         needs_special_offset_handling,
10203         local_symbol_count,
10204         plocal_syms,
10205         rr);
10206     }
10207   else if (sh_type == elfcpp::SHT_RELA)
10208     {
10209       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10210           Classify_reloc;
10211       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10212           Emit_relocs_strategy;
10213
10214       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10215         symtab,
10216         layout,
10217         object,
10218         data_shndx,
10219         prelocs,
10220         reloc_count,
10221         output_section,
10222         needs_special_offset_handling,
10223         local_symbol_count,
10224         plocal_syms,
10225         rr);
10226     }
10227   else
10228     gold_unreachable();
10229 }
10230
10231 // Emit relocations for a section.
10232
10233 template<int size, bool big_endian>
10234 void
10235 Target_mips<size, big_endian>::relocate_relocs(
10236                         const Relocate_info<size, big_endian>* relinfo,
10237                         unsigned int sh_type,
10238                         const unsigned char* prelocs,
10239                         size_t reloc_count,
10240                         Output_section* output_section,
10241                         typename elfcpp::Elf_types<size>::Elf_Off
10242                           offset_in_output_section,
10243                         unsigned char* view,
10244                         Mips_address view_address,
10245                         section_size_type view_size,
10246                         unsigned char* reloc_view,
10247                         section_size_type reloc_view_size)
10248 {
10249   if (sh_type == elfcpp::SHT_REL)
10250     {
10251       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10252           Classify_reloc;
10253
10254       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10255         relinfo,
10256         prelocs,
10257         reloc_count,
10258         output_section,
10259         offset_in_output_section,
10260         view,
10261         view_address,
10262         view_size,
10263         reloc_view,
10264         reloc_view_size);
10265     }
10266   else if (sh_type == elfcpp::SHT_RELA)
10267     {
10268       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10269           Classify_reloc;
10270
10271       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10272         relinfo,
10273         prelocs,
10274         reloc_count,
10275         output_section,
10276         offset_in_output_section,
10277         view,
10278         view_address,
10279         view_size,
10280         reloc_view,
10281         reloc_view_size);
10282     }
10283   else
10284     gold_unreachable();
10285 }
10286
10287 // Perform target-specific processing in a relocatable link.  This is
10288 // only used if we use the relocation strategy RELOC_SPECIAL.
10289
10290 template<int size, bool big_endian>
10291 void
10292 Target_mips<size, big_endian>::relocate_special_relocatable(
10293     const Relocate_info<size, big_endian>* relinfo,
10294     unsigned int sh_type,
10295     const unsigned char* preloc_in,
10296     size_t relnum,
10297     Output_section* output_section,
10298     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10299     unsigned char* view,
10300     Mips_address view_address,
10301     section_size_type,
10302     unsigned char* preloc_out)
10303 {
10304   // We can only handle REL type relocation sections.
10305   gold_assert(sh_type == elfcpp::SHT_REL);
10306
10307   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10308     Reltype;
10309   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10310     Reltype_write;
10311
10312   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10313
10314   const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10315
10316   Mips_relobj<size, big_endian>* object =
10317     Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10318   const unsigned int local_count = object->local_symbol_count();
10319
10320   Reltype reloc(preloc_in);
10321   Reltype_write reloc_write(preloc_out);
10322
10323   elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10324   const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10325   const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10326
10327   // Get the new symbol index.
10328   // We only use RELOC_SPECIAL strategy in local relocations.
10329   gold_assert(r_sym < local_count);
10330
10331   // We are adjusting a section symbol.  We need to find
10332   // the symbol table index of the section symbol for
10333   // the output section corresponding to input section
10334   // in which this symbol is defined.
10335   bool is_ordinary;
10336   unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10337   gold_assert(is_ordinary);
10338   Output_section* os = object->output_section(shndx);
10339   gold_assert(os != NULL);
10340   gold_assert(os->needs_symtab_index());
10341   unsigned int new_symndx = os->symtab_index();
10342
10343   // Get the new offset--the location in the output section where
10344   // this relocation should be applied.
10345
10346   Mips_address offset = reloc.get_r_offset();
10347   Mips_address new_offset;
10348   if (offset_in_output_section != invalid_address)
10349     new_offset = offset + offset_in_output_section;
10350   else
10351     {
10352       section_offset_type sot_offset =
10353         convert_types<section_offset_type, Mips_address>(offset);
10354       section_offset_type new_sot_offset =
10355         output_section->output_offset(object, relinfo->data_shndx,
10356                                       sot_offset);
10357       gold_assert(new_sot_offset != -1);
10358       new_offset = new_sot_offset;
10359     }
10360
10361   // In an object file, r_offset is an offset within the section.
10362   // In an executable or dynamic object, generated by
10363   // --emit-relocs, r_offset is an absolute address.
10364   if (!parameters->options().relocatable())
10365     {
10366       new_offset += view_address;
10367       if (offset_in_output_section != invalid_address)
10368         new_offset -= offset_in_output_section;
10369     }
10370
10371   reloc_write.put_r_offset(new_offset);
10372   reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10373
10374   // Handle the reloc addend.
10375   // The relocation uses a section symbol in the input file.
10376   // We are adjusting it to use a section symbol in the output
10377   // file.  The input section symbol refers to some address in
10378   // the input section.  We need the relocation in the output
10379   // file to refer to that same address.  This adjustment to
10380   // the addend is the same calculation we use for a simple
10381   // absolute relocation for the input section symbol.
10382   Valtype calculated_value = 0;
10383   const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10384
10385   unsigned char* paddend = view + offset;
10386   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10387   switch (r_type)
10388     {
10389     case elfcpp::R_MIPS_26:
10390       reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10391           offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10392           false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10393           false, &calculated_value);
10394       break;
10395
10396     default:
10397       gold_unreachable();
10398     }
10399
10400   // Report any errors.
10401   switch (reloc_status)
10402     {
10403     case Reloc_funcs::STATUS_OKAY:
10404       break;
10405     case Reloc_funcs::STATUS_OVERFLOW:
10406       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10407                              _("relocation overflow: "
10408                                "%u against local symbol %u in %s"),
10409                              r_type, r_sym, object->name().c_str());
10410       break;
10411     case Reloc_funcs::STATUS_BAD_RELOC:
10412       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10413         _("unexpected opcode while processing relocation"));
10414       break;
10415     default:
10416       gold_unreachable();
10417     }
10418 }
10419
10420 // Optimize the TLS relocation type based on what we know about the
10421 // symbol.  IS_FINAL is true if the final address of this symbol is
10422 // known at link time.
10423
10424 template<int size, bool big_endian>
10425 tls::Tls_optimization
10426 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10427 {
10428   // FIXME: Currently we do not do any TLS optimization.
10429   return tls::TLSOPT_NONE;
10430 }
10431
10432 // Scan a relocation for a local symbol.
10433
10434 template<int size, bool big_endian>
10435 inline void
10436 Target_mips<size, big_endian>::Scan::local(
10437                         Symbol_table* symtab,
10438                         Layout* layout,
10439                         Target_mips<size, big_endian>* target,
10440                         Sized_relobj_file<size, big_endian>* object,
10441                         unsigned int data_shndx,
10442                         Output_section* output_section,
10443                         const Relatype* rela,
10444                         const Reltype* rel,
10445                         unsigned int rel_type,
10446                         unsigned int r_type,
10447                         const elfcpp::Sym<size, big_endian>& lsym,
10448                         bool is_discarded)
10449 {
10450   if (is_discarded)
10451     return;
10452
10453   Mips_address r_offset;
10454   unsigned int r_sym;
10455   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10456
10457   if (rel_type == elfcpp::SHT_RELA)
10458     {
10459       r_offset = rela->get_r_offset();
10460       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10461           get_r_sym(rela);
10462       r_addend = rela->get_r_addend();
10463     }
10464   else
10465     {
10466       r_offset = rel->get_r_offset();
10467       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10468           get_r_sym(rel);
10469       r_addend = 0;
10470     }
10471
10472   Mips_relobj<size, big_endian>* mips_obj =
10473     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10474
10475   if (mips_obj->is_mips16_stub_section(data_shndx))
10476     {
10477       mips_obj->get_mips16_stub_section(data_shndx)
10478               ->new_local_reloc_found(r_type, r_sym);
10479     }
10480
10481   if (r_type == elfcpp::R_MIPS_NONE)
10482     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10483     // mips16 stub.
10484     return;
10485
10486   if (!mips16_call_reloc(r_type)
10487       && !mips_obj->section_allows_mips16_refs(data_shndx))
10488     // This reloc would need to refer to a MIPS16 hard-float stub, if
10489     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10490     // looking for relocs that would need to refer to MIPS16 stubs.
10491     mips_obj->add_local_non_16bit_call(r_sym);
10492
10493   if (r_type == elfcpp::R_MIPS16_26
10494       && !mips_obj->section_allows_mips16_refs(data_shndx))
10495     mips_obj->add_local_16bit_call(r_sym);
10496
10497   switch (r_type)
10498     {
10499     case elfcpp::R_MIPS_GOT16:
10500     case elfcpp::R_MIPS_CALL16:
10501     case elfcpp::R_MIPS_CALL_HI16:
10502     case elfcpp::R_MIPS_CALL_LO16:
10503     case elfcpp::R_MIPS_GOT_HI16:
10504     case elfcpp::R_MIPS_GOT_LO16:
10505     case elfcpp::R_MIPS_GOT_PAGE:
10506     case elfcpp::R_MIPS_GOT_OFST:
10507     case elfcpp::R_MIPS_GOT_DISP:
10508     case elfcpp::R_MIPS_TLS_GOTTPREL:
10509     case elfcpp::R_MIPS_TLS_GD:
10510     case elfcpp::R_MIPS_TLS_LDM:
10511     case elfcpp::R_MIPS16_GOT16:
10512     case elfcpp::R_MIPS16_CALL16:
10513     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10514     case elfcpp::R_MIPS16_TLS_GD:
10515     case elfcpp::R_MIPS16_TLS_LDM:
10516     case elfcpp::R_MICROMIPS_GOT16:
10517     case elfcpp::R_MICROMIPS_CALL16:
10518     case elfcpp::R_MICROMIPS_CALL_HI16:
10519     case elfcpp::R_MICROMIPS_CALL_LO16:
10520     case elfcpp::R_MICROMIPS_GOT_HI16:
10521     case elfcpp::R_MICROMIPS_GOT_LO16:
10522     case elfcpp::R_MICROMIPS_GOT_PAGE:
10523     case elfcpp::R_MICROMIPS_GOT_OFST:
10524     case elfcpp::R_MICROMIPS_GOT_DISP:
10525     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10526     case elfcpp::R_MICROMIPS_TLS_GD:
10527     case elfcpp::R_MICROMIPS_TLS_LDM:
10528     case elfcpp::R_MIPS_EH:
10529       // We need a GOT section.
10530       target->got_section(symtab, layout);
10531       break;
10532
10533     default:
10534       break;
10535     }
10536
10537   if (call_lo16_reloc(r_type)
10538       || got_lo16_reloc(r_type)
10539       || got_disp_reloc(r_type)
10540       || eh_reloc(r_type))
10541     {
10542       // We may need a local GOT entry for this relocation.  We
10543       // don't count R_MIPS_GOT_PAGE because we can estimate the
10544       // maximum number of pages needed by looking at the size of
10545       // the segment.  Similar comments apply to R_MIPS*_GOT16 and
10546       // R_MIPS*_CALL16.  We don't count R_MIPS_GOT_HI16, or
10547       // R_MIPS_CALL_HI16 because these are always followed by an
10548       // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10549       Mips_output_data_got<size, big_endian>* got =
10550         target->got_section(symtab, layout);
10551       bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10552       got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10553                                    is_section_symbol);
10554     }
10555
10556   switch (r_type)
10557     {
10558     case elfcpp::R_MIPS_CALL16:
10559     case elfcpp::R_MIPS16_CALL16:
10560     case elfcpp::R_MICROMIPS_CALL16:
10561       gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10562                  (unsigned long)r_offset);
10563       return;
10564
10565     case elfcpp::R_MIPS_GOT_PAGE:
10566     case elfcpp::R_MICROMIPS_GOT_PAGE:
10567     case elfcpp::R_MIPS16_GOT16:
10568     case elfcpp::R_MIPS_GOT16:
10569     case elfcpp::R_MIPS_GOT_HI16:
10570     case elfcpp::R_MIPS_GOT_LO16:
10571     case elfcpp::R_MICROMIPS_GOT16:
10572     case elfcpp::R_MICROMIPS_GOT_HI16:
10573     case elfcpp::R_MICROMIPS_GOT_LO16:
10574       {
10575         // This relocation needs a page entry in the GOT.
10576         // Get the section contents.
10577         section_size_type view_size = 0;
10578         const unsigned char* view = object->section_contents(data_shndx,
10579                                                              &view_size, false);
10580         view += r_offset;
10581
10582         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10583         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10584                                                         : r_addend);
10585
10586         if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10587           target->got16_addends_.push_back(got16_addend<size, big_endian>(
10588               object, data_shndx, r_type, r_sym, addend));
10589         else
10590           target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10591         break;
10592       }
10593
10594     case elfcpp::R_MIPS_HI16:
10595     case elfcpp::R_MIPS_PCHI16:
10596     case elfcpp::R_MIPS16_HI16:
10597     case elfcpp::R_MICROMIPS_HI16:
10598       // Record the reloc so that we can check whether the corresponding LO16
10599       // part exists.
10600       if (rel_type == elfcpp::SHT_REL)
10601         target->got16_addends_.push_back(got16_addend<size, big_endian>(
10602             object, data_shndx, r_type, r_sym, 0));
10603       break;
10604
10605     case elfcpp::R_MIPS_LO16:
10606     case elfcpp::R_MIPS_PCLO16:
10607     case elfcpp::R_MIPS16_LO16:
10608     case elfcpp::R_MICROMIPS_LO16:
10609       {
10610         if (rel_type != elfcpp::SHT_REL)
10611           break;
10612
10613         // Find corresponding GOT16/HI16 relocation.
10614
10615         // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10616         // be immediately following.  However, for the IRIX6 ABI, the next
10617         // relocation may be a composed relocation consisting of several
10618         // relocations for the same address.  In that case, the R_MIPS_LO16
10619         // relocation may occur as one of these.  We permit a similar
10620         // extension in general, as that is useful for GCC.
10621
10622         // In some cases GCC dead code elimination removes the LO16 but
10623         // keeps the corresponding HI16.  This is strictly speaking a
10624         // violation of the ABI but not immediately harmful.
10625
10626         typename std::list<got16_addend<size, big_endian> >::iterator it =
10627           target->got16_addends_.begin();
10628         while (it != target->got16_addends_.end())
10629           {
10630             got16_addend<size, big_endian> _got16_addend = *it;
10631
10632             // TODO(sasa): Split got16_addends_ list into two lists - one for
10633             // GOT16 relocs and the other for HI16 relocs.
10634
10635             // Report an error if we find HI16 or GOT16 reloc from the
10636             // previous section without the matching LO16 part.
10637             if (_got16_addend.object != object
10638                 || _got16_addend.shndx != data_shndx)
10639               {
10640                 gold_error("Can't find matching LO16 reloc");
10641                 break;
10642               }
10643
10644             if (_got16_addend.r_sym != r_sym
10645                 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10646               {
10647                 ++it;
10648                 continue;
10649               }
10650
10651             // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10652             // For GOT16, we need to calculate combined addend and record GOT page
10653             // entry.
10654             if (got16_reloc(_got16_addend.r_type))
10655               {
10656
10657                 section_size_type view_size = 0;
10658                 const unsigned char* view = object->section_contents(data_shndx,
10659                                                                      &view_size,
10660                                                                      false);
10661                 view += r_offset;
10662
10663                 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10664                 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10665
10666                 addend = (_got16_addend.addend << 16) + addend;
10667                 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10668                                                              addend);
10669               }
10670
10671             it = target->got16_addends_.erase(it);
10672           }
10673         break;
10674       }
10675     }
10676
10677   switch (r_type)
10678     {
10679     case elfcpp::R_MIPS_32:
10680     case elfcpp::R_MIPS_REL32:
10681     case elfcpp::R_MIPS_64:
10682       {
10683         if (parameters->options().output_is_position_independent())
10684           {
10685             // If building a shared library (or a position-independent
10686             // executable), we need to create a dynamic relocation for
10687             // this location.
10688             if (is_readonly_section(output_section))
10689               break;
10690             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10691             rel_dyn->add_symbolless_local_addend(object, r_sym,
10692                                                  elfcpp::R_MIPS_REL32,
10693                                                  output_section, data_shndx,
10694                                                  r_offset);
10695           }
10696         break;
10697       }
10698
10699     case elfcpp::R_MIPS_TLS_GOTTPREL:
10700     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10701     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10702     case elfcpp::R_MIPS_TLS_LDM:
10703     case elfcpp::R_MIPS16_TLS_LDM:
10704     case elfcpp::R_MICROMIPS_TLS_LDM:
10705     case elfcpp::R_MIPS_TLS_GD:
10706     case elfcpp::R_MIPS16_TLS_GD:
10707     case elfcpp::R_MICROMIPS_TLS_GD:
10708       {
10709         bool output_is_shared = parameters->options().shared();
10710         const tls::Tls_optimization optimized_type
10711             = Target_mips<size, big_endian>::optimize_tls_reloc(
10712                                              !output_is_shared, r_type);
10713         switch (r_type)
10714           {
10715           case elfcpp::R_MIPS_TLS_GD:
10716           case elfcpp::R_MIPS16_TLS_GD:
10717           case elfcpp::R_MICROMIPS_TLS_GD:
10718             if (optimized_type == tls::TLSOPT_NONE)
10719               {
10720                 // Create a pair of GOT entries for the module index and
10721                 // dtv-relative offset.
10722                 Mips_output_data_got<size, big_endian>* got =
10723                   target->got_section(symtab, layout);
10724                 unsigned int shndx = lsym.get_st_shndx();
10725                 bool is_ordinary;
10726                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10727                 if (!is_ordinary)
10728                   {
10729                     object->error(_("local symbol %u has bad shndx %u"),
10730                                   r_sym, shndx);
10731                     break;
10732                   }
10733                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10734                                              shndx, false);
10735               }
10736             else
10737               {
10738                 // FIXME: TLS optimization not supported yet.
10739                 gold_unreachable();
10740               }
10741             break;
10742
10743           case elfcpp::R_MIPS_TLS_LDM:
10744           case elfcpp::R_MIPS16_TLS_LDM:
10745           case elfcpp::R_MICROMIPS_TLS_LDM:
10746             if (optimized_type == tls::TLSOPT_NONE)
10747               {
10748                 // We always record LDM symbols as local with index 0.
10749                 target->got_section()->record_local_got_symbol(mips_obj, 0,
10750                                                                r_addend, r_type,
10751                                                                -1U, false);
10752               }
10753             else
10754               {
10755                 // FIXME: TLS optimization not supported yet.
10756                 gold_unreachable();
10757               }
10758             break;
10759           case elfcpp::R_MIPS_TLS_GOTTPREL:
10760           case elfcpp::R_MIPS16_TLS_GOTTPREL:
10761           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10762             layout->set_has_static_tls();
10763             if (optimized_type == tls::TLSOPT_NONE)
10764               {
10765                 // Create a GOT entry for the tp-relative offset.
10766                 Mips_output_data_got<size, big_endian>* got =
10767                   target->got_section(symtab, layout);
10768                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10769                                              -1U, false);
10770               }
10771             else
10772               {
10773                 // FIXME: TLS optimization not supported yet.
10774                 gold_unreachable();
10775               }
10776             break;
10777
10778           default:
10779             gold_unreachable();
10780         }
10781       }
10782       break;
10783
10784     default:
10785       break;
10786     }
10787
10788   // Refuse some position-dependent relocations when creating a
10789   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
10790   // not PIC, but we can create dynamic relocations and the result
10791   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
10792   // combined with R_MIPS_GOT16.
10793   if (parameters->options().shared())
10794     {
10795       switch (r_type)
10796         {
10797         case elfcpp::R_MIPS16_HI16:
10798         case elfcpp::R_MIPS_HI16:
10799         case elfcpp::R_MIPS_HIGHER:
10800         case elfcpp::R_MIPS_HIGHEST:
10801         case elfcpp::R_MICROMIPS_HI16:
10802         case elfcpp::R_MICROMIPS_HIGHER:
10803         case elfcpp::R_MICROMIPS_HIGHEST:
10804           // Don't refuse a high part relocation if it's against
10805           // no symbol (e.g. part of a compound relocation).
10806           if (r_sym == 0)
10807             break;
10808           // Fall through.
10809
10810         case elfcpp::R_MIPS16_26:
10811         case elfcpp::R_MIPS_26:
10812         case elfcpp::R_MICROMIPS_26_S1:
10813           gold_error(_("%s: relocation %u against `%s' can not be used when "
10814                        "making a shared object; recompile with -fPIC"),
10815                      object->name().c_str(), r_type, "a local symbol");
10816         default:
10817           break;
10818         }
10819     }
10820 }
10821
10822 template<int size, bool big_endian>
10823 inline void
10824 Target_mips<size, big_endian>::Scan::local(
10825                         Symbol_table* symtab,
10826                         Layout* layout,
10827                         Target_mips<size, big_endian>* target,
10828                         Sized_relobj_file<size, big_endian>* object,
10829                         unsigned int data_shndx,
10830                         Output_section* output_section,
10831                         const Reltype& reloc,
10832                         unsigned int r_type,
10833                         const elfcpp::Sym<size, big_endian>& lsym,
10834                         bool is_discarded)
10835 {
10836   if (is_discarded)
10837     return;
10838
10839   local(
10840     symtab,
10841     layout,
10842     target,
10843     object,
10844     data_shndx,
10845     output_section,
10846     (const Relatype*) NULL,
10847     &reloc,
10848     elfcpp::SHT_REL,
10849     r_type,
10850     lsym, is_discarded);
10851 }
10852
10853
10854 template<int size, bool big_endian>
10855 inline void
10856 Target_mips<size, big_endian>::Scan::local(
10857                         Symbol_table* symtab,
10858                         Layout* layout,
10859                         Target_mips<size, big_endian>* target,
10860                         Sized_relobj_file<size, big_endian>* object,
10861                         unsigned int data_shndx,
10862                         Output_section* output_section,
10863                         const Relatype& reloc,
10864                         unsigned int r_type,
10865                         const elfcpp::Sym<size, big_endian>& lsym,
10866                         bool is_discarded)
10867 {
10868   if (is_discarded)
10869     return;
10870
10871   local(
10872     symtab,
10873     layout,
10874     target,
10875     object,
10876     data_shndx,
10877     output_section,
10878     &reloc,
10879     (const Reltype*) NULL,
10880     elfcpp::SHT_RELA,
10881     r_type,
10882     lsym, is_discarded);
10883 }
10884
10885 // Scan a relocation for a global symbol.
10886
10887 template<int size, bool big_endian>
10888 inline void
10889 Target_mips<size, big_endian>::Scan::global(
10890                                 Symbol_table* symtab,
10891                                 Layout* layout,
10892                                 Target_mips<size, big_endian>* target,
10893                                 Sized_relobj_file<size, big_endian>* object,
10894                                 unsigned int data_shndx,
10895                                 Output_section* output_section,
10896                                 const Relatype* rela,
10897                                 const Reltype* rel,
10898                                 unsigned int rel_type,
10899                                 unsigned int r_type,
10900                                 Symbol* gsym)
10901 {
10902   Mips_address r_offset;
10903   unsigned int r_sym;
10904   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10905
10906   if (rel_type == elfcpp::SHT_RELA)
10907     {
10908       r_offset = rela->get_r_offset();
10909       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10910           get_r_sym(rela);
10911       r_addend = rela->get_r_addend();
10912     }
10913   else
10914     {
10915       r_offset = rel->get_r_offset();
10916       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10917           get_r_sym(rel);
10918       r_addend = 0;
10919     }
10920
10921   Mips_relobj<size, big_endian>* mips_obj =
10922     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10923   Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10924
10925   if (mips_obj->is_mips16_stub_section(data_shndx))
10926     {
10927       mips_obj->get_mips16_stub_section(data_shndx)
10928               ->new_global_reloc_found(r_type, mips_sym);
10929     }
10930
10931   if (r_type == elfcpp::R_MIPS_NONE)
10932     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10933     // mips16 stub.
10934     return;
10935
10936   if (!mips16_call_reloc(r_type)
10937       && !mips_obj->section_allows_mips16_refs(data_shndx))
10938     // This reloc would need to refer to a MIPS16 hard-float stub, if
10939     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10940     // looking for relocs that would need to refer to MIPS16 stubs.
10941     mips_sym->set_need_fn_stub();
10942
10943   // We need PLT entries if there are static-only relocations against
10944   // an externally-defined function.  This can technically occur for
10945   // shared libraries if there are branches to the symbol, although it
10946   // is unlikely that this will be used in practice due to the short
10947   // ranges involved.  It can occur for any relative or absolute relocation
10948   // in executables; in that case, the PLT entry becomes the function's
10949   // canonical address.
10950   bool static_reloc = false;
10951
10952   // Set CAN_MAKE_DYNAMIC to true if we can convert this
10953   // relocation into a dynamic one.
10954   bool can_make_dynamic = false;
10955   switch (r_type)
10956     {
10957     case elfcpp::R_MIPS_GOT16:
10958     case elfcpp::R_MIPS_CALL16:
10959     case elfcpp::R_MIPS_CALL_HI16:
10960     case elfcpp::R_MIPS_CALL_LO16:
10961     case elfcpp::R_MIPS_GOT_HI16:
10962     case elfcpp::R_MIPS_GOT_LO16:
10963     case elfcpp::R_MIPS_GOT_PAGE:
10964     case elfcpp::R_MIPS_GOT_OFST:
10965     case elfcpp::R_MIPS_GOT_DISP:
10966     case elfcpp::R_MIPS_TLS_GOTTPREL:
10967     case elfcpp::R_MIPS_TLS_GD:
10968     case elfcpp::R_MIPS_TLS_LDM:
10969     case elfcpp::R_MIPS16_GOT16:
10970     case elfcpp::R_MIPS16_CALL16:
10971     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10972     case elfcpp::R_MIPS16_TLS_GD:
10973     case elfcpp::R_MIPS16_TLS_LDM:
10974     case elfcpp::R_MICROMIPS_GOT16:
10975     case elfcpp::R_MICROMIPS_CALL16:
10976     case elfcpp::R_MICROMIPS_CALL_HI16:
10977     case elfcpp::R_MICROMIPS_CALL_LO16:
10978     case elfcpp::R_MICROMIPS_GOT_HI16:
10979     case elfcpp::R_MICROMIPS_GOT_LO16:
10980     case elfcpp::R_MICROMIPS_GOT_PAGE:
10981     case elfcpp::R_MICROMIPS_GOT_OFST:
10982     case elfcpp::R_MICROMIPS_GOT_DISP:
10983     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10984     case elfcpp::R_MICROMIPS_TLS_GD:
10985     case elfcpp::R_MICROMIPS_TLS_LDM:
10986     case elfcpp::R_MIPS_EH:
10987       // We need a GOT section.
10988       target->got_section(symtab, layout);
10989       break;
10990
10991     // This is just a hint; it can safely be ignored.  Don't set
10992     // has_static_relocs for the corresponding symbol.
10993     case elfcpp::R_MIPS_JALR:
10994     case elfcpp::R_MICROMIPS_JALR:
10995       break;
10996
10997     case elfcpp::R_MIPS_GPREL16:
10998     case elfcpp::R_MIPS_GPREL32:
10999     case elfcpp::R_MIPS16_GPREL:
11000     case elfcpp::R_MICROMIPS_GPREL16:
11001       // TODO(sasa)
11002       // GP-relative relocations always resolve to a definition in a
11003       // regular input file, ignoring the one-definition rule.  This is
11004       // important for the GP setup sequence in NewABI code, which
11005       // always resolves to a local function even if other relocations
11006       // against the symbol wouldn't.
11007       //constrain_symbol_p = FALSE;
11008       break;
11009
11010     case elfcpp::R_MIPS_32:
11011     case elfcpp::R_MIPS_REL32:
11012     case elfcpp::R_MIPS_64:
11013       if ((parameters->options().shared()
11014           || (strcmp(gsym->name(), "__gnu_local_gp") != 0
11015           && (!is_readonly_section(output_section)
11016           || mips_obj->is_pic())))
11017           && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
11018         {
11019           if (r_type != elfcpp::R_MIPS_REL32)
11020             mips_sym->set_pointer_equality_needed();
11021           can_make_dynamic = true;
11022           break;
11023         }
11024       // Fall through.
11025
11026     default:
11027       // Most static relocations require pointer equality, except
11028       // for branches.
11029       mips_sym->set_pointer_equality_needed();
11030       // Fall through.
11031
11032     case elfcpp::R_MIPS_26:
11033     case elfcpp::R_MIPS_PC16:
11034     case elfcpp::R_MIPS_PC21_S2:
11035     case elfcpp::R_MIPS_PC26_S2:
11036     case elfcpp::R_MIPS16_26:
11037     case elfcpp::R_MICROMIPS_26_S1:
11038     case elfcpp::R_MICROMIPS_PC7_S1:
11039     case elfcpp::R_MICROMIPS_PC10_S1:
11040     case elfcpp::R_MICROMIPS_PC16_S1:
11041     case elfcpp::R_MICROMIPS_PC23_S2:
11042       static_reloc = true;
11043       mips_sym->set_has_static_relocs();
11044       break;
11045     }
11046
11047   // If there are call relocations against an externally-defined symbol,
11048   // see whether we can create a MIPS lazy-binding stub for it.  We can
11049   // only do this if all references to the function are through call
11050   // relocations, and in that case, the traditional lazy-binding stubs
11051   // are much more efficient than PLT entries.
11052   switch (r_type)
11053     {
11054     case elfcpp::R_MIPS16_CALL16:
11055     case elfcpp::R_MIPS_CALL16:
11056     case elfcpp::R_MIPS_CALL_HI16:
11057     case elfcpp::R_MIPS_CALL_LO16:
11058     case elfcpp::R_MIPS_JALR:
11059     case elfcpp::R_MICROMIPS_CALL16:
11060     case elfcpp::R_MICROMIPS_CALL_HI16:
11061     case elfcpp::R_MICROMIPS_CALL_LO16:
11062     case elfcpp::R_MICROMIPS_JALR:
11063       if (!mips_sym->no_lazy_stub())
11064         {
11065           if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11066               // Calls from shared objects to undefined symbols of type
11067               // STT_NOTYPE need lazy-binding stub.
11068               || (mips_sym->is_undefined() && parameters->options().shared()))
11069             target->mips_stubs_section(layout)->make_entry(mips_sym);
11070         }
11071       break;
11072     default:
11073       {
11074         // We must not create a stub for a symbol that has relocations
11075         // related to taking the function's address.
11076         mips_sym->set_no_lazy_stub();
11077         target->remove_lazy_stub_entry(mips_sym);
11078         break;
11079       }
11080   }
11081
11082   if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11083                                                    mips_sym->is_mips16()))
11084     mips_sym->set_has_nonpic_branches();
11085
11086   // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11087   // and has a special meaning.
11088   bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11089                                && strcmp(gsym->name(), "_gp_disp") == 0
11090                                && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11091   if (static_reloc && gsym->needs_plt_entry())
11092     {
11093       target->make_plt_entry(symtab, layout, mips_sym, r_type);
11094
11095       // Since this is not a PC-relative relocation, we may be
11096       // taking the address of a function.  In that case we need to
11097       // set the entry in the dynamic symbol table to the address of
11098       // the PLT entry.
11099       if (gsym->is_from_dynobj() && !parameters->options().shared())
11100         {
11101           gsym->set_needs_dynsym_value();
11102           // We distinguish between PLT entries and lazy-binding stubs by
11103           // giving the former an st_other value of STO_MIPS_PLT.  Set the
11104           // flag if there are any relocations in the binary where pointer
11105           // equality matters.
11106           if (mips_sym->pointer_equality_needed())
11107             mips_sym->set_mips_plt();
11108         }
11109     }
11110   if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11111     {
11112       // Absolute addressing relocations.
11113       // Make a dynamic relocation if necessary.
11114       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11115         {
11116           if (gsym->may_need_copy_reloc())
11117             {
11118               target->copy_reloc(symtab, layout, object, data_shndx,
11119                                  output_section, gsym, r_type, r_offset);
11120             }
11121           else if (can_make_dynamic)
11122             {
11123               // Create .rel.dyn section.
11124               target->rel_dyn_section(layout);
11125               target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11126                                     data_shndx, output_section, r_offset);
11127             }
11128           else
11129             gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11130                        gsym->name());
11131         }
11132     }
11133
11134   bool for_call = false;
11135   switch (r_type)
11136     {
11137     case elfcpp::R_MIPS_CALL16:
11138     case elfcpp::R_MIPS16_CALL16:
11139     case elfcpp::R_MICROMIPS_CALL16:
11140     case elfcpp::R_MIPS_CALL_HI16:
11141     case elfcpp::R_MIPS_CALL_LO16:
11142     case elfcpp::R_MICROMIPS_CALL_HI16:
11143     case elfcpp::R_MICROMIPS_CALL_LO16:
11144       for_call = true;
11145       // Fall through.
11146
11147     case elfcpp::R_MIPS16_GOT16:
11148     case elfcpp::R_MIPS_GOT16:
11149     case elfcpp::R_MIPS_GOT_HI16:
11150     case elfcpp::R_MIPS_GOT_LO16:
11151     case elfcpp::R_MICROMIPS_GOT16:
11152     case elfcpp::R_MICROMIPS_GOT_HI16:
11153     case elfcpp::R_MICROMIPS_GOT_LO16:
11154     case elfcpp::R_MIPS_GOT_DISP:
11155     case elfcpp::R_MICROMIPS_GOT_DISP:
11156     case elfcpp::R_MIPS_EH:
11157       {
11158         // The symbol requires a GOT entry.
11159         Mips_output_data_got<size, big_endian>* got =
11160           target->got_section(symtab, layout);
11161         got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11162                                       for_call);
11163         mips_sym->set_global_got_area(GGA_NORMAL);
11164       }
11165       break;
11166
11167     case elfcpp::R_MIPS_GOT_PAGE:
11168     case elfcpp::R_MICROMIPS_GOT_PAGE:
11169       {
11170         // This relocation needs a page entry in the GOT.
11171         // Get the section contents.
11172         section_size_type view_size = 0;
11173         const unsigned char* view =
11174           object->section_contents(data_shndx, &view_size, false);
11175         view += r_offset;
11176
11177         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11178         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11179                                                         : r_addend);
11180         Mips_output_data_got<size, big_endian>* got =
11181           target->got_section(symtab, layout);
11182         got->record_got_page_entry(mips_obj, r_sym, addend);
11183
11184         // If this is a global, overridable symbol, GOT_PAGE will
11185         // decay to GOT_DISP, so we'll need a GOT entry for it.
11186         bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11187                             && !mips_sym->object()->is_dynamic()
11188                             && !mips_sym->is_undefined());
11189         if (!def_regular
11190             || (parameters->options().output_is_position_independent()
11191                 && !parameters->options().Bsymbolic()
11192                 && !mips_sym->is_forced_local()))
11193           {
11194             got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11195                                           for_call);
11196             mips_sym->set_global_got_area(GGA_NORMAL);
11197           }
11198       }
11199       break;
11200
11201     case elfcpp::R_MIPS_TLS_GOTTPREL:
11202     case elfcpp::R_MIPS16_TLS_GOTTPREL:
11203     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11204     case elfcpp::R_MIPS_TLS_LDM:
11205     case elfcpp::R_MIPS16_TLS_LDM:
11206     case elfcpp::R_MICROMIPS_TLS_LDM:
11207     case elfcpp::R_MIPS_TLS_GD:
11208     case elfcpp::R_MIPS16_TLS_GD:
11209     case elfcpp::R_MICROMIPS_TLS_GD:
11210       {
11211         const bool is_final = gsym->final_value_is_known();
11212         const tls::Tls_optimization optimized_type =
11213           Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11214
11215         switch (r_type)
11216           {
11217           case elfcpp::R_MIPS_TLS_GD:
11218           case elfcpp::R_MIPS16_TLS_GD:
11219           case elfcpp::R_MICROMIPS_TLS_GD:
11220             if (optimized_type == tls::TLSOPT_NONE)
11221               {
11222                 // Create a pair of GOT entries for the module index and
11223                 // dtv-relative offset.
11224                 Mips_output_data_got<size, big_endian>* got =
11225                   target->got_section(symtab, layout);
11226                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11227                                               false);
11228               }
11229             else
11230               {
11231                 // FIXME: TLS optimization not supported yet.
11232                 gold_unreachable();
11233               }
11234             break;
11235
11236           case elfcpp::R_MIPS_TLS_LDM:
11237           case elfcpp::R_MIPS16_TLS_LDM:
11238           case elfcpp::R_MICROMIPS_TLS_LDM:
11239             if (optimized_type == tls::TLSOPT_NONE)
11240               {
11241                 // We always record LDM symbols as local with index 0.
11242                 target->got_section()->record_local_got_symbol(mips_obj, 0,
11243                                                                r_addend, r_type,
11244                                                                -1U, false);
11245               }
11246             else
11247               {
11248                 // FIXME: TLS optimization not supported yet.
11249                 gold_unreachable();
11250               }
11251             break;
11252           case elfcpp::R_MIPS_TLS_GOTTPREL:
11253           case elfcpp::R_MIPS16_TLS_GOTTPREL:
11254           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11255             layout->set_has_static_tls();
11256             if (optimized_type == tls::TLSOPT_NONE)
11257               {
11258                 // Create a GOT entry for the tp-relative offset.
11259                 Mips_output_data_got<size, big_endian>* got =
11260                   target->got_section(symtab, layout);
11261                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11262                                               false);
11263               }
11264             else
11265               {
11266                 // FIXME: TLS optimization not supported yet.
11267                 gold_unreachable();
11268               }
11269             break;
11270
11271           default:
11272             gold_unreachable();
11273         }
11274       }
11275       break;
11276     case elfcpp::R_MIPS_COPY:
11277     case elfcpp::R_MIPS_JUMP_SLOT:
11278       // These are relocations which should only be seen by the
11279       // dynamic linker, and should never be seen here.
11280       gold_error(_("%s: unexpected reloc %u in object file"),
11281                  object->name().c_str(), r_type);
11282       break;
11283
11284     default:
11285       break;
11286     }
11287
11288   // Refuse some position-dependent relocations when creating a
11289   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
11290   // not PIC, but we can create dynamic relocations and the result
11291   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
11292   // combined with R_MIPS_GOT16.
11293   if (parameters->options().shared())
11294     {
11295       switch (r_type)
11296         {
11297         case elfcpp::R_MIPS16_HI16:
11298         case elfcpp::R_MIPS_HI16:
11299         case elfcpp::R_MIPS_HIGHER:
11300         case elfcpp::R_MIPS_HIGHEST:
11301         case elfcpp::R_MICROMIPS_HI16:
11302         case elfcpp::R_MICROMIPS_HIGHER:
11303         case elfcpp::R_MICROMIPS_HIGHEST:
11304           // Don't refuse a high part relocation if it's against
11305           // no symbol (e.g. part of a compound relocation).
11306           if (r_sym == 0)
11307             break;
11308
11309           // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11310           // and has a special meaning.
11311           if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11312             break;
11313           // Fall through.
11314
11315         case elfcpp::R_MIPS16_26:
11316         case elfcpp::R_MIPS_26:
11317         case elfcpp::R_MICROMIPS_26_S1:
11318           gold_error(_("%s: relocation %u against `%s' can not be used when "
11319                        "making a shared object; recompile with -fPIC"),
11320                      object->name().c_str(), r_type, gsym->name());
11321         default:
11322           break;
11323         }
11324     }
11325 }
11326
11327 template<int size, bool big_endian>
11328 inline void
11329 Target_mips<size, big_endian>::Scan::global(
11330                                 Symbol_table* symtab,
11331                                 Layout* layout,
11332                                 Target_mips<size, big_endian>* target,
11333                                 Sized_relobj_file<size, big_endian>* object,
11334                                 unsigned int data_shndx,
11335                                 Output_section* output_section,
11336                                 const Relatype& reloc,
11337                                 unsigned int r_type,
11338                                 Symbol* gsym)
11339 {
11340   global(
11341     symtab,
11342     layout,
11343     target,
11344     object,
11345     data_shndx,
11346     output_section,
11347     &reloc,
11348     (const Reltype*) NULL,
11349     elfcpp::SHT_RELA,
11350     r_type,
11351     gsym);
11352 }
11353
11354 template<int size, bool big_endian>
11355 inline void
11356 Target_mips<size, big_endian>::Scan::global(
11357                                 Symbol_table* symtab,
11358                                 Layout* layout,
11359                                 Target_mips<size, big_endian>* target,
11360                                 Sized_relobj_file<size, big_endian>* object,
11361                                 unsigned int data_shndx,
11362                                 Output_section* output_section,
11363                                 const Reltype& reloc,
11364                                 unsigned int r_type,
11365                                 Symbol* gsym)
11366 {
11367   global(
11368     symtab,
11369     layout,
11370     target,
11371     object,
11372     data_shndx,
11373     output_section,
11374     (const Relatype*) NULL,
11375     &reloc,
11376     elfcpp::SHT_REL,
11377     r_type,
11378     gsym);
11379 }
11380
11381 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11382 // In cases where Scan::local() or Scan::global() has created
11383 // a dynamic relocation, the addend of the relocation is carried
11384 // in the data, and we must not apply the static relocation.
11385
11386 template<int size, bool big_endian>
11387 inline bool
11388 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11389     const Mips_symbol<size>* gsym,
11390     unsigned int r_type,
11391     Output_section* output_section,
11392     Target_mips* target)
11393 {
11394   // If the output section is not allocated, then we didn't call
11395   // scan_relocs, we didn't create a dynamic reloc, and we must apply
11396   // the reloc here.
11397   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11398       return true;
11399
11400   if (gsym == NULL)
11401     return true;
11402   else
11403     {
11404       // For global symbols, we use the same helper routines used in the
11405       // scan pass.
11406       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11407           && !gsym->may_need_copy_reloc())
11408         {
11409           // We have generated dynamic reloc (R_MIPS_REL32).
11410
11411           bool multi_got = false;
11412           if (target->has_got_section())
11413             multi_got = target->got_section()->multi_got();
11414           bool has_got_offset;
11415           if (!multi_got)
11416             has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11417           else
11418             has_got_offset = gsym->global_gotoffset() != -1U;
11419           if (!has_got_offset)
11420             return true;
11421           else
11422             // Apply the relocation only if the symbol is in the local got.
11423             // Do not apply the relocation if the symbol is in the global
11424             // got.
11425             return symbol_references_local(gsym, gsym->has_dynsym_index());
11426         }
11427       else
11428         // We have not generated dynamic reloc.
11429         return true;
11430     }
11431 }
11432
11433 // Perform a relocation.
11434
11435 template<int size, bool big_endian>
11436 inline bool
11437 Target_mips<size, big_endian>::Relocate::relocate(
11438                         const Relocate_info<size, big_endian>* relinfo,
11439                         unsigned int rel_type,
11440                         Target_mips* target,
11441                         Output_section* output_section,
11442                         size_t relnum,
11443                         const unsigned char* preloc,
11444                         const Sized_symbol<size>* gsym,
11445                         const Symbol_value<size>* psymval,
11446                         unsigned char* view,
11447                         Mips_address address,
11448                         section_size_type)
11449 {
11450   Mips_address r_offset;
11451   unsigned int r_sym;
11452   unsigned int r_type;
11453   unsigned int r_type2;
11454   unsigned int r_type3;
11455   unsigned char r_ssym;
11456   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11457   // r_offset and r_type of the next relocation is needed for resolving multiple
11458   // consecutive relocations with the same offset.
11459   Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11460   unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11461
11462   elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11463   size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
11464
11465   if (rel_type == elfcpp::SHT_RELA)
11466     {
11467       const Relatype rela(preloc);
11468       r_offset = rela.get_r_offset();
11469       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11470           get_r_sym(&rela);
11471       r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11472           get_r_type(&rela);
11473       r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11474           get_r_type2(&rela);
11475       r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11476           get_r_type3(&rela);
11477       r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11478           get_r_ssym(&rela);
11479       r_addend = rela.get_r_addend();
11480       // If this is not last relocation, get r_offset and r_type of the next
11481       // relocation.
11482       if (relnum + 1 < reloc_count)
11483         {
11484           const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11485           const Relatype next_rela(preloc + reloc_size);
11486           next_r_offset = next_rela.get_r_offset();
11487           next_r_type =
11488             Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11489               get_r_type(&next_rela);
11490         }
11491     }
11492   else
11493     {
11494       const Reltype rel(preloc);
11495       r_offset = rel.get_r_offset();
11496       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11497           get_r_sym(&rel);
11498       r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11499           get_r_type(&rel);
11500       r_ssym = 0;
11501       r_type2 = elfcpp::R_MIPS_NONE;
11502       r_type3 = elfcpp::R_MIPS_NONE;
11503       r_addend = 0;
11504       // If this is not last relocation, get r_offset and r_type of the next
11505       // relocation.
11506       if (relnum + 1 < reloc_count)
11507         {
11508           const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11509           const Reltype next_rel(preloc + reloc_size);
11510           next_r_offset = next_rel.get_r_offset();
11511           next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11512             get_r_type(&next_rel);
11513         }
11514     }
11515
11516   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11517   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11518
11519   Mips_relobj<size, big_endian>* object =
11520       Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11521
11522   bool target_is_16_bit_code = false;
11523   bool target_is_micromips_code = false;
11524   bool cross_mode_jump;
11525
11526   Symbol_value<size> symval;
11527
11528   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11529
11530   bool changed_symbol_value = false;
11531   if (gsym == NULL)
11532     {
11533       target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11534       target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11535       if (target_is_16_bit_code || target_is_micromips_code)
11536         {
11537           // MIPS16/microMIPS text labels should be treated as odd.
11538           symval.set_output_value(psymval->value(object, 1));
11539           psymval = &symval;
11540           changed_symbol_value = true;
11541         }
11542     }
11543   else
11544     {
11545       target_is_16_bit_code = mips_sym->is_mips16();
11546       target_is_micromips_code = mips_sym->is_micromips();
11547
11548       // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11549       // it odd.  This will cause something like .word SYM to come up with
11550       // the right value when it is loaded into the PC.
11551
11552       if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11553           && psymval->value(object, 0) != 0)
11554         {
11555           symval.set_output_value(psymval->value(object, 0) | 1);
11556           psymval = &symval;
11557           changed_symbol_value = true;
11558         }
11559
11560       // Pick the value to use for symbols defined in shared objects.
11561       if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11562           || mips_sym->has_lazy_stub())
11563         {
11564           Mips_address value;
11565           if (!mips_sym->has_lazy_stub())
11566             {
11567               // Prefer a standard MIPS PLT entry.
11568               if (mips_sym->has_mips_plt_offset())
11569                 {
11570                   value = target->plt_section()->mips_entry_address(mips_sym);
11571                   target_is_micromips_code = false;
11572                   target_is_16_bit_code = false;
11573                 }
11574               else
11575                 {
11576                   value = (target->plt_section()->comp_entry_address(mips_sym)
11577                            + 1);
11578                   if (target->is_output_micromips())
11579                     target_is_micromips_code = true;
11580                   else
11581                     target_is_16_bit_code = true;
11582                 }
11583             }
11584           else
11585             value = target->mips_stubs_section()->stub_address(mips_sym);
11586
11587           symval.set_output_value(value);
11588           psymval = &symval;
11589         }
11590     }
11591
11592   // TRUE if the symbol referred to by this relocation is "_gp_disp".
11593   // Note that such a symbol must always be a global symbol.
11594   bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11595                   && !object->is_newabi());
11596
11597   // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11598   // Note that such a symbol must always be a global symbol.
11599   bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11600
11601
11602   if (gp_disp)
11603     {
11604       if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11605         gold_error_at_location(relinfo, relnum, r_offset,
11606           _("relocations against _gp_disp are permitted only"
11607             " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11608     }
11609   else if (gnu_local_gp)
11610     {
11611       // __gnu_local_gp is _gp symbol.
11612       symval.set_output_value(target->adjusted_gp_value(object));
11613       psymval = &symval;
11614     }
11615
11616   // If this is a reference to a 16-bit function with a stub, we need
11617   // to redirect the relocation to the stub unless:
11618   //
11619   // (a) the relocation is for a MIPS16 JAL;
11620   //
11621   // (b) the relocation is for a MIPS16 PIC call, and there are no
11622   //     non-MIPS16 uses of the GOT slot; or
11623   //
11624   // (c) the section allows direct references to MIPS16 functions.
11625   if (r_type != elfcpp::R_MIPS16_26
11626       && ((mips_sym != NULL
11627            && mips_sym->has_mips16_fn_stub()
11628            && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11629           || (mips_sym == NULL
11630               && object->get_local_mips16_fn_stub(r_sym) != NULL))
11631       && !object->section_allows_mips16_refs(relinfo->data_shndx))
11632     {
11633       // This is a 32- or 64-bit call to a 16-bit function.  We should
11634       // have already noticed that we were going to need the
11635       // stub.
11636       Mips_address value;
11637       if (mips_sym == NULL)
11638         value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11639       else
11640         {
11641           gold_assert(mips_sym->need_fn_stub());
11642           if (mips_sym->has_la25_stub())
11643             value = target->la25_stub_section()->stub_address(mips_sym);
11644           else
11645             {
11646               value = mips_sym->template
11647                       get_mips16_fn_stub<big_endian>()->output_address();
11648             }
11649           }
11650       symval.set_output_value(value);
11651       psymval = &symval;
11652       changed_symbol_value = true;
11653
11654       // The target is 16-bit, but the stub isn't.
11655       target_is_16_bit_code = false;
11656     }
11657   // If this is a MIPS16 call with a stub, that is made through the PLT or
11658   // to a standard MIPS function, we need to redirect the call to the stub.
11659   // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11660   // indirect calls should use an indirect stub instead.
11661   else if (r_type == elfcpp::R_MIPS16_26
11662            && ((mips_sym != NULL
11663                 && (mips_sym->has_mips16_call_stub()
11664                     || mips_sym->has_mips16_call_fp_stub()))
11665                || (mips_sym == NULL
11666                    && object->get_local_mips16_call_stub(r_sym) != NULL))
11667            && ((mips_sym != NULL && mips_sym->has_plt_offset())
11668                || !target_is_16_bit_code))
11669     {
11670       Mips16_stub_section<size, big_endian>* call_stub;
11671       if (mips_sym == NULL)
11672         call_stub = object->get_local_mips16_call_stub(r_sym);
11673       else
11674         {
11675           // If both call_stub and call_fp_stub are defined, we can figure
11676           // out which one to use by checking which one appears in the input
11677           // file.
11678           if (mips_sym->has_mips16_call_stub()
11679               && mips_sym->has_mips16_call_fp_stub())
11680             {
11681               call_stub = NULL;
11682               for (unsigned int i = 1; i < object->shnum(); ++i)
11683                 {
11684                   if (object->is_mips16_call_fp_stub_section(i))
11685                     {
11686                       call_stub = mips_sym->template
11687                                   get_mips16_call_fp_stub<big_endian>();
11688                       break;
11689                     }
11690
11691                 }
11692               if (call_stub == NULL)
11693                 call_stub =
11694                   mips_sym->template get_mips16_call_stub<big_endian>();
11695             }
11696           else if (mips_sym->has_mips16_call_stub())
11697             call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11698           else
11699             call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11700         }
11701
11702       symval.set_output_value(call_stub->output_address());
11703       psymval = &symval;
11704       changed_symbol_value = true;
11705     }
11706   // If this is a direct call to a PIC function, redirect to the
11707   // non-PIC stub.
11708   else if (mips_sym != NULL
11709            && mips_sym->has_la25_stub()
11710            && relocation_needs_la25_stub<size, big_endian>(
11711                                        object, r_type, target_is_16_bit_code))
11712     {
11713       Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11714       if (mips_sym->is_micromips())
11715         value += 1;
11716       symval.set_output_value(value);
11717       psymval = &symval;
11718     }
11719   // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11720   // entry is used if a standard PLT entry has also been made.
11721   else if ((r_type == elfcpp::R_MIPS16_26
11722             || r_type == elfcpp::R_MICROMIPS_26_S1)
11723           && mips_sym != NULL
11724           && mips_sym->has_plt_offset()
11725           && mips_sym->has_comp_plt_offset()
11726           && mips_sym->has_mips_plt_offset())
11727     {
11728       Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11729                             + 1);
11730       symval.set_output_value(value);
11731       psymval = &symval;
11732
11733       target_is_16_bit_code = !target->is_output_micromips();
11734       target_is_micromips_code = target->is_output_micromips();
11735     }
11736
11737   // Make sure MIPS16 and microMIPS are not used together.
11738   if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11739       || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11740    {
11741       gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11742    }
11743
11744   // Calls from 16-bit code to 32-bit code and vice versa require the
11745   // mode change.  However, we can ignore calls to undefined weak symbols,
11746   // which should never be executed at runtime.  This exception is important
11747   // because the assembly writer may have "known" that any definition of the
11748   // symbol would be 16-bit code, and that direct jumps were therefore
11749   // acceptable.
11750   cross_mode_jump =
11751     (!(gsym != NULL && gsym->is_weak_undefined())
11752      && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11753          || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11754          || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11755              && (target_is_16_bit_code || target_is_micromips_code))));
11756
11757   bool local = (mips_sym == NULL
11758                 || (mips_sym->got_only_for_calls()
11759                     ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11760                     : symbol_references_local(mips_sym,
11761                                               mips_sym->has_dynsym_index())));
11762
11763   // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11764   // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
11765   // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11766   if (got_page_reloc(r_type) && !local)
11767     r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11768                                       : elfcpp::R_MIPS_GOT_DISP);
11769
11770   unsigned int got_offset = 0;
11771   int gp_offset = 0;
11772
11773   // Whether we have to extract addend from instruction.
11774   bool extract_addend = rel_type == elfcpp::SHT_REL;
11775   unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11776
11777   Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11778
11779   // For Mips64 N64 ABI, there may be up to three operations specified per
11780   // record, by the fields r_type, r_type2, and r_type3. The first operation
11781   // takes its addend from the relocation record. Each subsequent operation
11782   // takes as its addend the result of the previous operation.
11783   // The first operation in a record which references a symbol uses the symbol
11784   // implied by r_sym. The next operation in a record which references a symbol
11785   // uses the special symbol value given by the r_ssym field. A third operation
11786   // in a record which references a symbol will assume a NULL symbol,
11787   // i.e. value zero.
11788
11789   // TODO(Vladimir)
11790   // Check if a record references to a symbol.
11791   for (unsigned int i = 0; i < 3; ++i)
11792     {
11793       if (r_types[i] == elfcpp::R_MIPS_NONE)
11794         break;
11795
11796       // If we didn't apply previous relocation, use its result as addend
11797       // for current.
11798       if (this->calculate_only_)
11799         {
11800           r_addend = this->calculated_value_;
11801           extract_addend = false;
11802         }
11803
11804       // In the N32 and 64-bit ABIs there may be multiple consecutive
11805       // relocations for the same offset.  In that case we are
11806       // supposed to treat the output of each relocation as the addend
11807       // for the next.  For N64 ABI, we are checking offsets only in a
11808       // third operation in a record (r_type3).
11809       this->calculate_only_ =
11810         (object->is_n64() && i < 2
11811          ? r_types[i+1] != elfcpp::R_MIPS_NONE
11812          : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
11813
11814       if (object->is_n64())
11815         {
11816           if (i == 1)
11817             {
11818               // Handle special symbol for r_type2 relocation type.
11819               switch (r_ssym)
11820                 {
11821                 case RSS_UNDEF:
11822                   symval.set_output_value(0);
11823                   break;
11824                 case RSS_GP:
11825                   symval.set_output_value(target->gp_value());
11826                   break;
11827                 case RSS_GP0:
11828                   symval.set_output_value(object->gp_value());
11829                   break;
11830                 case RSS_LOC:
11831                   symval.set_output_value(address);
11832                   break;
11833                 default:
11834                   gold_unreachable();
11835                 }
11836               psymval = &symval;
11837             }
11838           else if (i == 2)
11839            {
11840             // For r_type3 symbol value is 0.
11841             symval.set_output_value(0);
11842            }
11843         }
11844
11845       bool update_got_entry = false;
11846       switch (r_types[i])
11847         {
11848         case elfcpp::R_MIPS_NONE:
11849           break;
11850         case elfcpp::R_MIPS_16:
11851           reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11852                                             extract_addend,
11853                                             this->calculate_only_,
11854                                             &this->calculated_value_);
11855           break;
11856
11857         case elfcpp::R_MIPS_32:
11858           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11859                                         target))
11860             reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11861                                               extract_addend,
11862                                               this->calculate_only_,
11863                                               &this->calculated_value_);
11864           if (mips_sym != NULL
11865               && (mips_sym->is_mips16() || mips_sym->is_micromips())
11866               && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11867             {
11868               // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11869               // already updated by adding +1.
11870               if (mips_sym->has_mips16_fn_stub())
11871                 {
11872                   gold_assert(mips_sym->need_fn_stub());
11873                   Mips16_stub_section<size, big_endian>* fn_stub =
11874                     mips_sym->template get_mips16_fn_stub<big_endian>();
11875
11876                   symval.set_output_value(fn_stub->output_address());
11877                   psymval = &symval;
11878                 }
11879               got_offset = mips_sym->global_gotoffset();
11880               update_got_entry = true;
11881             }
11882           break;
11883
11884         case elfcpp::R_MIPS_64:
11885           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11886                                         target))
11887             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11888                                               extract_addend,
11889                                               this->calculate_only_,
11890                                               &this->calculated_value_, false);
11891           else if (target->is_output_n64() && r_addend != 0)
11892             // Only apply the addend.  The static relocation was RELA, but the
11893             // dynamic relocation is REL, so we need to apply the addend.
11894             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11895                                               extract_addend,
11896                                               this->calculate_only_,
11897                                               &this->calculated_value_, true);
11898           break;
11899         case elfcpp::R_MIPS_REL32:
11900           gold_unreachable();
11901
11902         case elfcpp::R_MIPS_PC32:
11903           reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11904                                               r_addend, extract_addend,
11905                                               this->calculate_only_,
11906                                               &this->calculated_value_);
11907           break;
11908
11909         case elfcpp::R_MIPS16_26:
11910           // The calculation for R_MIPS16_26 is just the same as for an
11911           // R_MIPS_26.  It's only the storage of the relocated field into
11912           // the output file that's different.  So, we just fall through to the
11913           // R_MIPS_26 case here.
11914         case elfcpp::R_MIPS_26:
11915         case elfcpp::R_MICROMIPS_26_S1:
11916           reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11917               gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11918               r_types[i], target->jal_to_bal(), this->calculate_only_,
11919               &this->calculated_value_);
11920           break;
11921
11922         case elfcpp::R_MIPS_HI16:
11923         case elfcpp::R_MIPS16_HI16:
11924         case elfcpp::R_MICROMIPS_HI16:
11925           if (rel_type == elfcpp::SHT_RELA)
11926             reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11927                                                    r_addend, address,
11928                                                    gp_disp, r_types[i],
11929                                                    extract_addend, 0,
11930                                                    target,
11931                                                    this->calculate_only_,
11932                                                    &this->calculated_value_);
11933           else if (rel_type == elfcpp::SHT_REL)
11934             reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11935                                                 address, gp_disp, r_types[i],
11936                                                 r_sym, extract_addend);
11937           else
11938             gold_unreachable();
11939           break;
11940
11941         case elfcpp::R_MIPS_LO16:
11942         case elfcpp::R_MIPS16_LO16:
11943         case elfcpp::R_MICROMIPS_LO16:
11944         case elfcpp::R_MICROMIPS_HI0_LO16:
11945           reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11946                                               r_addend, extract_addend, address,
11947                                               gp_disp, r_types[i], r_sym,
11948                                               rel_type, this->calculate_only_,
11949                                               &this->calculated_value_);
11950           break;
11951
11952         case elfcpp::R_MIPS_LITERAL:
11953         case elfcpp::R_MICROMIPS_LITERAL:
11954           // Because we don't merge literal sections, we can handle this
11955           // just like R_MIPS_GPREL16.  In the long run, we should merge
11956           // shared literals, and then we will need to additional work
11957           // here.
11958
11959           // Fall through.
11960
11961         case elfcpp::R_MIPS_GPREL16:
11962         case elfcpp::R_MIPS16_GPREL:
11963         case elfcpp::R_MICROMIPS_GPREL7_S2:
11964         case elfcpp::R_MICROMIPS_GPREL16:
11965           reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11966                                              target->adjusted_gp_value(object),
11967                                              r_addend, extract_addend,
11968                                              gsym == NULL, r_types[i],
11969                                              this->calculate_only_,
11970                                              &this->calculated_value_);
11971           break;
11972
11973         case elfcpp::R_MIPS_PC16:
11974           reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11975                                               r_addend, extract_addend,
11976                                               this->calculate_only_,
11977                                               &this->calculated_value_);
11978           break;
11979
11980         case elfcpp::R_MIPS_PC21_S2:
11981           reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11982                                               r_addend, extract_addend,
11983                                               this->calculate_only_,
11984                                               &this->calculated_value_);
11985           break;
11986
11987         case elfcpp::R_MIPS_PC26_S2:
11988           reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11989                                               r_addend, extract_addend,
11990                                               this->calculate_only_,
11991                                               &this->calculated_value_);
11992           break;
11993
11994         case elfcpp::R_MIPS_PC18_S3:
11995           reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11996                                               r_addend, extract_addend,
11997                                               this->calculate_only_,
11998                                               &this->calculated_value_);
11999           break;
12000
12001         case elfcpp::R_MIPS_PC19_S2:
12002           reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
12003                                               r_addend, extract_addend,
12004                                               this->calculate_only_,
12005                                               &this->calculated_value_);
12006           break;
12007
12008         case elfcpp::R_MIPS_PCHI16:
12009           if (rel_type == elfcpp::SHT_RELA)
12010             reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
12011                                                      r_addend, address,
12012                                                      extract_addend, 0,
12013                                                      this->calculate_only_,
12014                                                      &this->calculated_value_);
12015           else if (rel_type == elfcpp::SHT_REL)
12016             reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
12017                                                   r_addend, address, r_sym,
12018                                                   extract_addend);
12019           else
12020             gold_unreachable();
12021           break;
12022
12023         case elfcpp::R_MIPS_PCLO16:
12024           reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
12025                                                 extract_addend, address, r_sym,
12026                                                 rel_type, this->calculate_only_,
12027                                                 &this->calculated_value_);
12028           break;
12029         case elfcpp::R_MICROMIPS_PC7_S1:
12030           reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
12031                                                       address, r_addend,
12032                                                       extract_addend,
12033                                                       this->calculate_only_,
12034                                                       &this->calculated_value_);
12035           break;
12036         case elfcpp::R_MICROMIPS_PC10_S1:
12037           reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
12038                                                       psymval, address,
12039                                                       r_addend, extract_addend,
12040                                                       this->calculate_only_,
12041                                                       &this->calculated_value_);
12042           break;
12043         case elfcpp::R_MICROMIPS_PC16_S1:
12044           reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
12045                                                       psymval, address,
12046                                                       r_addend, extract_addend,
12047                                                       this->calculate_only_,
12048                                                       &this->calculated_value_);
12049           break;
12050         case elfcpp::R_MIPS_GPREL32:
12051           reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12052                                               target->adjusted_gp_value(object),
12053                                               r_addend, extract_addend,
12054                                               this->calculate_only_,
12055                                               &this->calculated_value_);
12056           break;
12057         case elfcpp::R_MIPS_GOT_HI16:
12058         case elfcpp::R_MIPS_CALL_HI16:
12059         case elfcpp::R_MICROMIPS_GOT_HI16:
12060         case elfcpp::R_MICROMIPS_CALL_HI16:
12061           if (gsym != NULL)
12062             got_offset = target->got_section()->got_offset(gsym,
12063                                                            GOT_TYPE_STANDARD,
12064                                                            object);
12065           else
12066             got_offset = target->got_section()->got_offset(r_sym,
12067                                                            GOT_TYPE_STANDARD,
12068                                                            object, r_addend);
12069           gp_offset = target->got_section()->gp_offset(got_offset, object);
12070           reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
12071                                                   this->calculate_only_,
12072                                                   &this->calculated_value_);
12073           update_got_entry = changed_symbol_value;
12074           break;
12075
12076         case elfcpp::R_MIPS_GOT_LO16:
12077         case elfcpp::R_MIPS_CALL_LO16:
12078         case elfcpp::R_MICROMIPS_GOT_LO16:
12079         case elfcpp::R_MICROMIPS_CALL_LO16:
12080           if (gsym != NULL)
12081             got_offset = target->got_section()->got_offset(gsym,
12082                                                            GOT_TYPE_STANDARD,
12083                                                            object);
12084           else
12085             got_offset = target->got_section()->got_offset(r_sym,
12086                                                            GOT_TYPE_STANDARD,
12087                                                            object, r_addend);
12088           gp_offset = target->got_section()->gp_offset(got_offset, object);
12089           reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
12090                                                   this->calculate_only_,
12091                                                   &this->calculated_value_);
12092           update_got_entry = changed_symbol_value;
12093           break;
12094
12095         case elfcpp::R_MIPS_GOT_DISP:
12096         case elfcpp::R_MICROMIPS_GOT_DISP:
12097         case elfcpp::R_MIPS_EH:
12098           if (gsym != NULL)
12099             got_offset = target->got_section()->got_offset(gsym,
12100                                                            GOT_TYPE_STANDARD,
12101                                                            object);
12102           else
12103             got_offset = target->got_section()->got_offset(r_sym,
12104                                                            GOT_TYPE_STANDARD,
12105                                                            object, r_addend);
12106           gp_offset = target->got_section()->gp_offset(got_offset, object);
12107           if (eh_reloc(r_types[i]))
12108             reloc_status = Reloc_funcs::releh(view, gp_offset,
12109                                               this->calculate_only_,
12110                                               &this->calculated_value_);
12111           else
12112             reloc_status = Reloc_funcs::relgot(view, gp_offset,
12113                                                this->calculate_only_,
12114                                                &this->calculated_value_);
12115           break;
12116         case elfcpp::R_MIPS_CALL16:
12117         case elfcpp::R_MIPS16_CALL16:
12118         case elfcpp::R_MICROMIPS_CALL16:
12119           gold_assert(gsym != NULL);
12120           got_offset = target->got_section()->got_offset(gsym,
12121                                                          GOT_TYPE_STANDARD,
12122                                                          object);
12123           gp_offset = target->got_section()->gp_offset(got_offset, object);
12124           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12125                                              this->calculate_only_,
12126                                              &this->calculated_value_);
12127           // TODO(sasa): We should also initialize update_got_entry
12128           // in other place swhere relgot is called.
12129           update_got_entry = changed_symbol_value;
12130           break;
12131
12132         case elfcpp::R_MIPS_GOT16:
12133         case elfcpp::R_MIPS16_GOT16:
12134         case elfcpp::R_MICROMIPS_GOT16:
12135           if (gsym != NULL)
12136             {
12137               got_offset = target->got_section()->got_offset(gsym,
12138                                                              GOT_TYPE_STANDARD,
12139                                                              object);
12140               gp_offset = target->got_section()->gp_offset(got_offset, object);
12141               reloc_status = Reloc_funcs::relgot(view, gp_offset,
12142                                                  this->calculate_only_,
12143                                                  &this->calculated_value_);
12144             }
12145           else
12146             {
12147               if (rel_type == elfcpp::SHT_RELA)
12148                 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
12149                                                       psymval, r_addend,
12150                                                       extract_addend, 0,
12151                                                       target,
12152                                                       this->calculate_only_,
12153                                                       &this->calculated_value_);
12154               else if (rel_type == elfcpp::SHT_REL)
12155                 reloc_status = Reloc_funcs::relgot16_local(view, object,
12156                                                            psymval, r_addend,
12157                                                            extract_addend,
12158                                                            r_types[i], r_sym);
12159               else
12160                 gold_unreachable();
12161             }
12162           update_got_entry = changed_symbol_value;
12163           break;
12164
12165         case elfcpp::R_MIPS_TLS_GD:
12166         case elfcpp::R_MIPS16_TLS_GD:
12167         case elfcpp::R_MICROMIPS_TLS_GD:
12168           if (gsym != NULL)
12169             got_offset = target->got_section()->got_offset(gsym,
12170                                                            GOT_TYPE_TLS_PAIR,
12171                                                            object);
12172           else
12173             got_offset = target->got_section()->got_offset(r_sym,
12174                                                            GOT_TYPE_TLS_PAIR,
12175                                                            object, r_addend);
12176           gp_offset = target->got_section()->gp_offset(got_offset, object);
12177           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12178                                              this->calculate_only_,
12179                                              &this->calculated_value_);
12180           break;
12181
12182         case elfcpp::R_MIPS_TLS_GOTTPREL:
12183         case elfcpp::R_MIPS16_TLS_GOTTPREL:
12184         case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12185           if (gsym != NULL)
12186             got_offset = target->got_section()->got_offset(gsym,
12187                                                            GOT_TYPE_TLS_OFFSET,
12188                                                            object);
12189           else
12190             got_offset = target->got_section()->got_offset(r_sym,
12191                                                            GOT_TYPE_TLS_OFFSET,
12192                                                            object, r_addend);
12193           gp_offset = target->got_section()->gp_offset(got_offset, object);
12194           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12195                                              this->calculate_only_,
12196                                              &this->calculated_value_);
12197           break;
12198
12199         case elfcpp::R_MIPS_TLS_LDM:
12200         case elfcpp::R_MIPS16_TLS_LDM:
12201         case elfcpp::R_MICROMIPS_TLS_LDM:
12202           // Relocate the field with the offset of the GOT entry for
12203           // the module index.
12204           got_offset = target->got_section()->tls_ldm_offset(object);
12205           gp_offset = target->got_section()->gp_offset(got_offset, object);
12206           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12207                                              this->calculate_only_,
12208                                              &this->calculated_value_);
12209           break;
12210
12211         case elfcpp::R_MIPS_GOT_PAGE:
12212         case elfcpp::R_MICROMIPS_GOT_PAGE:
12213           reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12214                                                  r_addend, extract_addend,
12215                                                  this->calculate_only_,
12216                                                  &this->calculated_value_);
12217           break;
12218
12219         case elfcpp::R_MIPS_GOT_OFST:
12220         case elfcpp::R_MICROMIPS_GOT_OFST:
12221           reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12222                                                  r_addend, extract_addend,
12223                                                  local, this->calculate_only_,
12224                                                  &this->calculated_value_);
12225           break;
12226
12227         case elfcpp::R_MIPS_JALR:
12228         case elfcpp::R_MICROMIPS_JALR:
12229           // This relocation is only a hint.  In some cases, we optimize
12230           // it into a bal instruction.  But we don't try to optimize
12231           // when the symbol does not resolve locally.
12232           if (gsym == NULL
12233               || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12234             reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12235                                                 r_addend, extract_addend,
12236                                                 cross_mode_jump, r_types[i],
12237                                                 target->jalr_to_bal(),
12238                                                 target->jr_to_b(),
12239                                                 this->calculate_only_,
12240                                                 &this->calculated_value_);
12241           break;
12242
12243         case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12244         case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12245         case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12246           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12247                                                  elfcpp::DTP_OFFSET, r_addend,
12248                                                  extract_addend,
12249                                                  this->calculate_only_,
12250                                                  &this->calculated_value_);
12251           break;
12252         case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12253         case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12254         case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12255           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12256                                                  elfcpp::DTP_OFFSET, r_addend,
12257                                                  extract_addend,
12258                                                  this->calculate_only_,
12259                                                  &this->calculated_value_);
12260           break;
12261         case elfcpp::R_MIPS_TLS_DTPREL32:
12262         case elfcpp::R_MIPS_TLS_DTPREL64:
12263           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12264                                                elfcpp::DTP_OFFSET, r_addend,
12265                                                extract_addend,
12266                                                this->calculate_only_,
12267                                                &this->calculated_value_);
12268           break;
12269         case elfcpp::R_MIPS_TLS_TPREL_HI16:
12270         case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12271         case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12272           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12273                                                  elfcpp::TP_OFFSET, r_addend,
12274                                                  extract_addend,
12275                                                  this->calculate_only_,
12276                                                  &this->calculated_value_);
12277           break;
12278         case elfcpp::R_MIPS_TLS_TPREL_LO16:
12279         case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12280         case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12281           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12282                                                  elfcpp::TP_OFFSET, r_addend,
12283                                                  extract_addend,
12284                                                  this->calculate_only_,
12285                                                  &this->calculated_value_);
12286           break;
12287         case elfcpp::R_MIPS_TLS_TPREL32:
12288         case elfcpp::R_MIPS_TLS_TPREL64:
12289           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12290                                                elfcpp::TP_OFFSET, r_addend,
12291                                                extract_addend,
12292                                                this->calculate_only_,
12293                                                &this->calculated_value_);
12294           break;
12295         case elfcpp::R_MIPS_SUB:
12296         case elfcpp::R_MICROMIPS_SUB:
12297           reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12298                                              extract_addend,
12299                                              this->calculate_only_,
12300                                              &this->calculated_value_);
12301           break;
12302         case elfcpp::R_MIPS_HIGHER:
12303         case elfcpp::R_MICROMIPS_HIGHER:
12304           reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
12305                                                 extract_addend,
12306                                                 this->calculate_only_,
12307                                                 &this->calculated_value_);
12308           break;
12309         case elfcpp::R_MIPS_HIGHEST:
12310         case elfcpp::R_MICROMIPS_HIGHEST:
12311           reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12312                                                  r_addend, extract_addend,
12313                                                  this->calculate_only_,
12314                                                  &this->calculated_value_);
12315           break;
12316         default:
12317           gold_error_at_location(relinfo, relnum, r_offset,
12318                                  _("unsupported reloc %u"), r_types[i]);
12319           break;
12320         }
12321
12322       if (update_got_entry)
12323         {
12324           Mips_output_data_got<size, big_endian>* got = target->got_section();
12325           if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12326             got->update_got_entry(got->get_primary_got_offset(mips_sym),
12327                                   psymval->value(object, 0));
12328           else
12329             got->update_got_entry(got_offset, psymval->value(object, 0));
12330         }
12331     }
12332
12333   bool jal_shuffle = jal_reloc(r_type);
12334   Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12335
12336   // Report any errors.
12337   switch (reloc_status)
12338     {
12339     case Reloc_funcs::STATUS_OKAY:
12340       break;
12341     case Reloc_funcs::STATUS_OVERFLOW:
12342       if (gsym == NULL)
12343         gold_error_at_location(relinfo, relnum, r_offset,
12344                                _("relocation overflow: "
12345                                  "%u against local symbol %u in %s"),
12346                                r_type, r_sym, object->name().c_str());
12347       else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12348         gold_error_at_location(relinfo, relnum, r_offset,
12349                                _("relocation overflow: "
12350                                  "%u against '%s' defined in %s"),
12351                                r_type, gsym->demangled_name().c_str(),
12352                                gsym->object()->name().c_str());
12353       else
12354         gold_error_at_location(relinfo, relnum, r_offset,
12355                                _("relocation overflow: %u against '%s'"),
12356                                r_type, gsym->demangled_name().c_str());
12357       break;
12358     case Reloc_funcs::STATUS_BAD_RELOC:
12359       gold_error_at_location(relinfo, relnum, r_offset,
12360         _("unexpected opcode while processing relocation"));
12361       break;
12362     case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12363       gold_error_at_location(relinfo, relnum, r_offset,
12364         _("unaligned PC-relative relocation"));
12365       break;
12366     default:
12367       gold_unreachable();
12368     }
12369
12370   return true;
12371 }
12372
12373 // Get the Reference_flags for a particular relocation.
12374
12375 template<int size, bool big_endian>
12376 int
12377 Target_mips<size, big_endian>::Scan::get_reference_flags(
12378                        unsigned int r_type)
12379 {
12380   switch (r_type)
12381     {
12382     case elfcpp::R_MIPS_NONE:
12383       // No symbol reference.
12384       return 0;
12385
12386     case elfcpp::R_MIPS_16:
12387     case elfcpp::R_MIPS_32:
12388     case elfcpp::R_MIPS_64:
12389     case elfcpp::R_MIPS_HI16:
12390     case elfcpp::R_MIPS_LO16:
12391     case elfcpp::R_MIPS_HIGHER:
12392     case elfcpp::R_MIPS_HIGHEST:
12393     case elfcpp::R_MIPS16_HI16:
12394     case elfcpp::R_MIPS16_LO16:
12395     case elfcpp::R_MICROMIPS_HI16:
12396     case elfcpp::R_MICROMIPS_LO16:
12397     case elfcpp::R_MICROMIPS_HIGHER:
12398     case elfcpp::R_MICROMIPS_HIGHEST:
12399       return Symbol::ABSOLUTE_REF;
12400
12401     case elfcpp::R_MIPS_26:
12402     case elfcpp::R_MIPS16_26:
12403     case elfcpp::R_MICROMIPS_26_S1:
12404       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12405
12406     case elfcpp::R_MIPS_PC18_S3:
12407     case elfcpp::R_MIPS_PC19_S2:
12408     case elfcpp::R_MIPS_PCHI16:
12409     case elfcpp::R_MIPS_PCLO16:
12410     case elfcpp::R_MIPS_GPREL32:
12411     case elfcpp::R_MIPS_GPREL16:
12412     case elfcpp::R_MIPS_REL32:
12413     case elfcpp::R_MIPS16_GPREL:
12414       return Symbol::RELATIVE_REF;
12415
12416     case elfcpp::R_MIPS_PC16:
12417     case elfcpp::R_MIPS_PC32:
12418     case elfcpp::R_MIPS_PC21_S2:
12419     case elfcpp::R_MIPS_PC26_S2:
12420     case elfcpp::R_MIPS_JALR:
12421     case elfcpp::R_MICROMIPS_JALR:
12422       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12423
12424     case elfcpp::R_MIPS_GOT16:
12425     case elfcpp::R_MIPS_CALL16:
12426     case elfcpp::R_MIPS_GOT_DISP:
12427     case elfcpp::R_MIPS_GOT_HI16:
12428     case elfcpp::R_MIPS_GOT_LO16:
12429     case elfcpp::R_MIPS_CALL_HI16:
12430     case elfcpp::R_MIPS_CALL_LO16:
12431     case elfcpp::R_MIPS_LITERAL:
12432     case elfcpp::R_MIPS_GOT_PAGE:
12433     case elfcpp::R_MIPS_GOT_OFST:
12434     case elfcpp::R_MIPS16_GOT16:
12435     case elfcpp::R_MIPS16_CALL16:
12436     case elfcpp::R_MICROMIPS_GOT16:
12437     case elfcpp::R_MICROMIPS_CALL16:
12438     case elfcpp::R_MICROMIPS_GOT_HI16:
12439     case elfcpp::R_MICROMIPS_GOT_LO16:
12440     case elfcpp::R_MICROMIPS_CALL_HI16:
12441     case elfcpp::R_MICROMIPS_CALL_LO16:
12442     case elfcpp::R_MIPS_EH:
12443       // Absolute in GOT.
12444       return Symbol::RELATIVE_REF;
12445
12446     case elfcpp::R_MIPS_TLS_DTPMOD32:
12447     case elfcpp::R_MIPS_TLS_DTPREL32:
12448     case elfcpp::R_MIPS_TLS_DTPMOD64:
12449     case elfcpp::R_MIPS_TLS_DTPREL64:
12450     case elfcpp::R_MIPS_TLS_GD:
12451     case elfcpp::R_MIPS_TLS_LDM:
12452     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12453     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12454     case elfcpp::R_MIPS_TLS_GOTTPREL:
12455     case elfcpp::R_MIPS_TLS_TPREL32:
12456     case elfcpp::R_MIPS_TLS_TPREL64:
12457     case elfcpp::R_MIPS_TLS_TPREL_HI16:
12458     case elfcpp::R_MIPS_TLS_TPREL_LO16:
12459     case elfcpp::R_MIPS16_TLS_GD:
12460     case elfcpp::R_MIPS16_TLS_GOTTPREL:
12461     case elfcpp::R_MICROMIPS_TLS_GD:
12462     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12463     case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12464     case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12465       return Symbol::TLS_REF;
12466
12467     case elfcpp::R_MIPS_COPY:
12468     case elfcpp::R_MIPS_JUMP_SLOT:
12469     default:
12470       // Not expected.  We will give an error later.
12471       return 0;
12472     }
12473 }
12474
12475 // Report an unsupported relocation against a local symbol.
12476
12477 template<int size, bool big_endian>
12478 void
12479 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12480                         Sized_relobj_file<size, big_endian>* object,
12481                         unsigned int r_type)
12482 {
12483   gold_error(_("%s: unsupported reloc %u against local symbol"),
12484              object->name().c_str(), r_type);
12485 }
12486
12487 // Report an unsupported relocation against a global symbol.
12488
12489 template<int size, bool big_endian>
12490 void
12491 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12492                         Sized_relobj_file<size, big_endian>* object,
12493                         unsigned int r_type,
12494                         Symbol* gsym)
12495 {
12496   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12497              object->name().c_str(), r_type, gsym->demangled_name().c_str());
12498 }
12499
12500 // Return printable name for ABI.
12501 template<int size, bool big_endian>
12502 const char*
12503 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12504 {
12505   switch (e_flags & elfcpp::EF_MIPS_ABI)
12506     {
12507     case 0:
12508       if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12509         return "N32";
12510       else if (size == 64)
12511         return "64";
12512       else
12513         return "none";
12514     case elfcpp::E_MIPS_ABI_O32:
12515       return "O32";
12516     case elfcpp::E_MIPS_ABI_O64:
12517       return "O64";
12518     case elfcpp::E_MIPS_ABI_EABI32:
12519       return "EABI32";
12520     case elfcpp::E_MIPS_ABI_EABI64:
12521       return "EABI64";
12522     default:
12523       return "unknown abi";
12524     }
12525 }
12526
12527 template<int size, bool big_endian>
12528 const char*
12529 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12530 {
12531   switch (e_flags & elfcpp::EF_MIPS_MACH)
12532     {
12533     case elfcpp::E_MIPS_MACH_3900:
12534       return "mips:3900";
12535     case elfcpp::E_MIPS_MACH_4010:
12536       return "mips:4010";
12537     case elfcpp::E_MIPS_MACH_4100:
12538       return "mips:4100";
12539     case elfcpp::E_MIPS_MACH_4111:
12540       return "mips:4111";
12541     case elfcpp::E_MIPS_MACH_4120:
12542       return "mips:4120";
12543     case elfcpp::E_MIPS_MACH_4650:
12544       return "mips:4650";
12545     case elfcpp::E_MIPS_MACH_5400:
12546       return "mips:5400";
12547     case elfcpp::E_MIPS_MACH_5500:
12548       return "mips:5500";
12549     case elfcpp::E_MIPS_MACH_5900:
12550       return "mips:5900";
12551     case elfcpp::E_MIPS_MACH_SB1:
12552       return "mips:sb1";
12553     case elfcpp::E_MIPS_MACH_9000:
12554       return "mips:9000";
12555     case elfcpp::E_MIPS_MACH_LS2E:
12556       return "mips:loongson_2e";
12557     case elfcpp::E_MIPS_MACH_LS2F:
12558       return "mips:loongson_2f";
12559     case elfcpp::E_MIPS_MACH_LS3A:
12560       return "mips:loongson_3a";
12561     case elfcpp::E_MIPS_MACH_OCTEON:
12562       return "mips:octeon";
12563     case elfcpp::E_MIPS_MACH_OCTEON2:
12564       return "mips:octeon2";
12565     case elfcpp::E_MIPS_MACH_OCTEON3:
12566       return "mips:octeon3";
12567     case elfcpp::E_MIPS_MACH_XLR:
12568       return "mips:xlr";
12569     default:
12570       switch (e_flags & elfcpp::EF_MIPS_ARCH)
12571         {
12572         default:
12573         case elfcpp::E_MIPS_ARCH_1:
12574           return "mips:3000";
12575
12576         case elfcpp::E_MIPS_ARCH_2:
12577           return "mips:6000";
12578
12579         case elfcpp::E_MIPS_ARCH_3:
12580           return "mips:4000";
12581
12582         case elfcpp::E_MIPS_ARCH_4:
12583           return "mips:8000";
12584
12585         case elfcpp::E_MIPS_ARCH_5:
12586           return "mips:mips5";
12587
12588         case elfcpp::E_MIPS_ARCH_32:
12589           return "mips:isa32";
12590
12591         case elfcpp::E_MIPS_ARCH_64:
12592           return "mips:isa64";
12593
12594         case elfcpp::E_MIPS_ARCH_32R2:
12595           return "mips:isa32r2";
12596
12597         case elfcpp::E_MIPS_ARCH_32R6:
12598           return "mips:isa32r6";
12599
12600         case elfcpp::E_MIPS_ARCH_64R2:
12601           return "mips:isa64r2";
12602
12603         case elfcpp::E_MIPS_ARCH_64R6:
12604           return "mips:isa64r6";
12605         }
12606     }
12607     return "unknown CPU";
12608 }
12609
12610 template<int size, bool big_endian>
12611 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12612 {
12613   size,                 // size
12614   big_endian,           // is_big_endian
12615   elfcpp::EM_MIPS,      // machine_code
12616   true,                 // has_make_symbol
12617   false,                // has_resolve
12618   false,                // has_code_fill
12619   true,                 // is_default_stack_executable
12620   false,                // can_icf_inline_merge_sections
12621   '\0',                 // wrap_char
12622   size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1",      // dynamic_linker
12623   0x400000,             // default_text_segment_address
12624   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
12625   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
12626   false,                // isolate_execinstr
12627   0,                    // rosegment_gap
12628   elfcpp::SHN_UNDEF,    // small_common_shndx
12629   elfcpp::SHN_UNDEF,    // large_common_shndx
12630   0,                    // small_common_section_flags
12631   0,                    // large_common_section_flags
12632   NULL,                 // attributes_section
12633   NULL,                 // attributes_vendor
12634   "__start",            // entry_symbol_name
12635   32,                   // hash_entry_size
12636 };
12637
12638 template<int size, bool big_endian>
12639 class Target_mips_nacl : public Target_mips<size, big_endian>
12640 {
12641  public:
12642   Target_mips_nacl()
12643     : Target_mips<size, big_endian>(&mips_nacl_info)
12644   { }
12645
12646  private:
12647   static const Target::Target_info mips_nacl_info;
12648 };
12649
12650 template<int size, bool big_endian>
12651 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12652 {
12653   size,                 // size
12654   big_endian,           // is_big_endian
12655   elfcpp::EM_MIPS,      // machine_code
12656   true,                 // has_make_symbol
12657   false,                // has_resolve
12658   false,                // has_code_fill
12659   true,                 // is_default_stack_executable
12660   false,                // can_icf_inline_merge_sections
12661   '\0',                 // wrap_char
12662   "/lib/ld.so.1",       // dynamic_linker
12663   0x20000,              // default_text_segment_address
12664   0x10000,              // abi_pagesize (overridable by -z max-page-size)
12665   0x10000,              // common_pagesize (overridable by -z common-page-size)
12666   true,                 // isolate_execinstr
12667   0x10000000,           // rosegment_gap
12668   elfcpp::SHN_UNDEF,    // small_common_shndx
12669   elfcpp::SHN_UNDEF,    // large_common_shndx
12670   0,                    // small_common_section_flags
12671   0,                    // large_common_section_flags
12672   NULL,                 // attributes_section
12673   NULL,                 // attributes_vendor
12674   "_start",             // entry_symbol_name
12675   32,                   // hash_entry_size
12676 };
12677
12678 // Target selector for Mips.  Note this is never instantiated directly.
12679 // It's only used in Target_selector_mips_nacl, below.
12680
12681 template<int size, bool big_endian>
12682 class Target_selector_mips : public Target_selector
12683 {
12684 public:
12685   Target_selector_mips()
12686     : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12687                 (size == 64 ?
12688                   (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12689                   (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12690                 (size == 64 ?
12691                   (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12692                   (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12693   { }
12694
12695   Target* do_instantiate_target()
12696   { return new Target_mips<size, big_endian>(); }
12697 };
12698
12699 template<int size, bool big_endian>
12700 class Target_selector_mips_nacl
12701   : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12702                                 Target_mips_nacl<size, big_endian> >
12703 {
12704  public:
12705   Target_selector_mips_nacl()
12706     : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12707                            Target_mips_nacl<size, big_endian> >(
12708         // NaCl currently supports only MIPS32 little-endian.
12709         "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12710   { }
12711 };
12712
12713 Target_selector_mips_nacl<32, true> target_selector_mips32;
12714 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12715 Target_selector_mips_nacl<64, true> target_selector_mips64;
12716 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12717
12718 } // End anonymous namespace.