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