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