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