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