xref: /netbsd-src/external/gpl3/binutils/dist/gold/mips.cc (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 // mips.cc -- mips target support for gold.
2 
3 // Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 // Written by Sasa Stankovic <sasa.stankovic@imgtec.com>
5 //        and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
6 // This file contains borrowed and adapted code from bfd/elfxx-mips.c.
7 
8 // This file is part of gold.
9 
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14 
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
24 
25 #include "gold.h"
26 
27 #include <algorithm>
28 #include <set>
29 #include <sstream>
30 #include "demangle.h"
31 
32 #include "elfcpp.h"
33 #include "parameters.h"
34 #include "reloc.h"
35 #include "mips.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "layout.h"
39 #include "output.h"
40 #include "copy-relocs.h"
41 #include "target.h"
42 #include "target-reloc.h"
43 #include "target-select.h"
44 #include "tls.h"
45 #include "errors.h"
46 #include "gc.h"
47 #include "attributes.h"
48 #include "nacl.h"
49 
50 namespace
51 {
52 using namespace gold;
53 
54 template<int size, bool big_endian>
55 class Mips_output_data_plt;
56 
57 template<int size, bool big_endian>
58 class Mips_output_data_got;
59 
60 template<int size, bool big_endian>
61 class Target_mips;
62 
63 template<int size, bool big_endian>
64 class Mips_output_section_reginfo;
65 
66 template<int size, bool big_endian>
67 class Mips_output_section_options;
68 
69 template<int size, bool big_endian>
70 class Mips_output_data_la25_stub;
71 
72 template<int size, bool big_endian>
73 class Mips_output_data_mips_stubs;
74 
75 template<int size>
76 class Mips_symbol;
77 
78 template<int size, bool big_endian>
79 class Mips_got_info;
80 
81 template<int size, bool big_endian>
82 class Mips_relobj;
83 
84 class Mips16_stub_section_base;
85 
86 template<int size, bool big_endian>
87 class Mips16_stub_section;
88 
89 // The ABI says that every symbol used by dynamic relocations must have
90 // a global GOT entry.  Among other things, this provides the dynamic
91 // linker with a free, directly-indexed cache.  The GOT can therefore
92 // contain symbols that are not referenced by GOT relocations themselves
93 // (in other words, it may have symbols that are not referenced by things
94 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
95 
96 // GOT relocations are less likely to overflow if we put the associated
97 // GOT entries towards the beginning.  We therefore divide the global
98 // GOT entries into two areas: "normal" and "reloc-only".  Entries in
99 // the first area can be used for both dynamic relocations and GP-relative
100 // accesses, while those in the "reloc-only" area are for dynamic
101 // relocations only.
102 
103 // These GGA_* ("Global GOT Area") values are organised so that lower
104 // values are more general than higher values.  Also, non-GGA_NONE
105 // values are ordered by the position of the area in the GOT.
106 
107 enum Global_got_area
108 {
109   GGA_NORMAL = 0,
110   GGA_RELOC_ONLY = 1,
111   GGA_NONE = 2
112 };
113 
114 // The types of GOT entries needed for this platform.
115 // These values are exposed to the ABI in an incremental link.
116 // Do not renumber existing values without changing the version
117 // number of the .gnu_incremental_inputs section.
118 enum Got_type
119 {
120   GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
121   GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
122   GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
123 
124   // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
125   GOT_TYPE_STANDARD_MULTIGOT = 3,
126   GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
127   GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
128 };
129 
130 // TLS type of GOT entry.
131 enum Got_tls_type
132 {
133   GOT_TLS_NONE = 0,
134   GOT_TLS_GD = 1,
135   GOT_TLS_LDM = 2,
136   GOT_TLS_IE = 4
137 };
138 
139 // Values found in the r_ssym field of a relocation entry.
140 enum Special_relocation_symbol
141 {
142   RSS_UNDEF = 0,    // None - value is zero.
143   RSS_GP = 1,       // Value of GP.
144   RSS_GP0 = 2,      // Value of GP in object being relocated.
145   RSS_LOC = 3       // Address of location being relocated.
146 };
147 
148 // Whether the section is readonly.
149 static inline bool
is_readonly_section(Output_section * output_section)150 is_readonly_section(Output_section* output_section)
151 {
152   elfcpp::Elf_Xword section_flags = output_section->flags();
153   elfcpp::Elf_Word section_type = output_section->type();
154 
155   if (section_type == elfcpp::SHT_NOBITS)
156     return false;
157 
158   if (section_flags & elfcpp::SHF_WRITE)
159     return false;
160 
161   return true;
162 }
163 
164 // Return TRUE if a relocation of type R_TYPE from OBJECT might
165 // require an la25 stub.  See also local_pic_function, which determines
166 // whether the destination function ever requires a stub.
167 template<int size, bool big_endian>
168 static inline bool
relocation_needs_la25_stub(Mips_relobj<size,big_endian> * object,unsigned int r_type,bool target_is_16_bit_code)169 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
170                            unsigned int r_type, bool target_is_16_bit_code)
171 {
172   // We specifically ignore branches and jumps from EF_PIC objects,
173   // where the onus is on the compiler or programmer to perform any
174   // necessary initialization of $25.  Sometimes such initialization
175   // is unnecessary; for example, -mno-shared functions do not use
176   // the incoming value of $25, and may therefore be called directly.
177   if (object->is_pic())
178     return false;
179 
180   switch (r_type)
181     {
182     case elfcpp::R_MIPS_26:
183     case elfcpp::R_MIPS_PC16:
184     case elfcpp::R_MIPS_PC21_S2:
185     case elfcpp::R_MIPS_PC26_S2:
186     case elfcpp::R_MICROMIPS_26_S1:
187     case elfcpp::R_MICROMIPS_PC7_S1:
188     case elfcpp::R_MICROMIPS_PC10_S1:
189     case elfcpp::R_MICROMIPS_PC16_S1:
190     case elfcpp::R_MICROMIPS_PC23_S2:
191       return true;
192 
193     case elfcpp::R_MIPS16_26:
194       return !target_is_16_bit_code;
195 
196     default:
197       return false;
198     }
199 }
200 
201 // Return true if SYM is a locally-defined PIC function, in the sense
202 // that it or its fn_stub might need $25 to be valid on entry.
203 // Note that MIPS16 functions set up $gp using PC-relative instructions,
204 // so they themselves never need $25 to be valid.  Only non-MIPS16
205 // entry points are of interest here.
206 template<int size, bool big_endian>
207 static inline bool
local_pic_function(Mips_symbol<size> * sym)208 local_pic_function(Mips_symbol<size>* sym)
209 {
210   bool def_regular = (sym->source() == Symbol::FROM_OBJECT
211                       && !sym->object()->is_dynamic()
212                       && !sym->is_undefined());
213 
214   if (sym->is_defined() && def_regular)
215     {
216       Mips_relobj<size, big_endian>* object =
217         static_cast<Mips_relobj<size, big_endian>*>(sym->object());
218 
219       if ((object->is_pic() || sym->is_pic())
220           && (!sym->is_mips16()
221               || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
222         return true;
223     }
224   return false;
225 }
226 
227 static inline bool
hi16_reloc(int r_type)228 hi16_reloc(int r_type)
229 {
230   return (r_type == elfcpp::R_MIPS_HI16
231           || r_type == elfcpp::R_MIPS16_HI16
232           || r_type == elfcpp::R_MICROMIPS_HI16
233           || r_type == elfcpp::R_MIPS_PCHI16);
234 }
235 
236 static inline bool
lo16_reloc(int r_type)237 lo16_reloc(int r_type)
238 {
239   return (r_type == elfcpp::R_MIPS_LO16
240           || r_type == elfcpp::R_MIPS16_LO16
241           || r_type == elfcpp::R_MICROMIPS_LO16
242           || r_type == elfcpp::R_MIPS_PCLO16);
243 }
244 
245 static inline bool
got16_reloc(unsigned int r_type)246 got16_reloc(unsigned int r_type)
247 {
248   return (r_type == elfcpp::R_MIPS_GOT16
249           || r_type == elfcpp::R_MIPS16_GOT16
250           || r_type == elfcpp::R_MICROMIPS_GOT16);
251 }
252 
253 static inline bool
call_lo16_reloc(unsigned int r_type)254 call_lo16_reloc(unsigned int r_type)
255 {
256   return (r_type == elfcpp::R_MIPS_CALL_LO16
257           || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
258 }
259 
260 static inline bool
got_lo16_reloc(unsigned int r_type)261 got_lo16_reloc(unsigned int r_type)
262 {
263   return (r_type == elfcpp::R_MIPS_GOT_LO16
264           || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
265 }
266 
267 static inline bool
eh_reloc(unsigned int r_type)268 eh_reloc(unsigned int r_type)
269 {
270   return (r_type == elfcpp::R_MIPS_EH);
271 }
272 
273 static inline bool
got_disp_reloc(unsigned int r_type)274 got_disp_reloc(unsigned int r_type)
275 {
276   return (r_type == elfcpp::R_MIPS_GOT_DISP
277           || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
278 }
279 
280 static inline bool
got_page_reloc(unsigned int r_type)281 got_page_reloc(unsigned int r_type)
282 {
283   return (r_type == elfcpp::R_MIPS_GOT_PAGE
284           || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
285 }
286 
287 static inline bool
tls_gd_reloc(unsigned int r_type)288 tls_gd_reloc(unsigned int r_type)
289 {
290   return (r_type == elfcpp::R_MIPS_TLS_GD
291           || r_type == elfcpp::R_MIPS16_TLS_GD
292           || r_type == elfcpp::R_MICROMIPS_TLS_GD);
293 }
294 
295 static inline bool
tls_gottprel_reloc(unsigned int r_type)296 tls_gottprel_reloc(unsigned int r_type)
297 {
298   return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
299           || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
300           || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
301 }
302 
303 static inline bool
tls_ldm_reloc(unsigned int r_type)304 tls_ldm_reloc(unsigned int r_type)
305 {
306   return (r_type == elfcpp::R_MIPS_TLS_LDM
307           || r_type == elfcpp::R_MIPS16_TLS_LDM
308           || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
309 }
310 
311 static inline bool
mips16_call_reloc(unsigned int r_type)312 mips16_call_reloc(unsigned int r_type)
313 {
314   return (r_type == elfcpp::R_MIPS16_26
315           || r_type == elfcpp::R_MIPS16_CALL16);
316 }
317 
318 static inline bool
jal_reloc(unsigned int r_type)319 jal_reloc(unsigned int r_type)
320 {
321   return (r_type == elfcpp::R_MIPS_26
322           || r_type == elfcpp::R_MIPS16_26
323           || r_type == elfcpp::R_MICROMIPS_26_S1);
324 }
325 
326 static inline bool
micromips_branch_reloc(unsigned int r_type)327 micromips_branch_reloc(unsigned int r_type)
328 {
329   return (r_type == elfcpp::R_MICROMIPS_26_S1
330           || r_type == elfcpp::R_MICROMIPS_PC16_S1
331           || r_type == elfcpp::R_MICROMIPS_PC10_S1
332           || r_type == elfcpp::R_MICROMIPS_PC7_S1);
333 }
334 
335 // Check if R_TYPE is a MIPS16 reloc.
336 static inline bool
mips16_reloc(unsigned int r_type)337 mips16_reloc(unsigned int r_type)
338 {
339   switch (r_type)
340     {
341     case elfcpp::R_MIPS16_26:
342     case elfcpp::R_MIPS16_GPREL:
343     case elfcpp::R_MIPS16_GOT16:
344     case elfcpp::R_MIPS16_CALL16:
345     case elfcpp::R_MIPS16_HI16:
346     case elfcpp::R_MIPS16_LO16:
347     case elfcpp::R_MIPS16_TLS_GD:
348     case elfcpp::R_MIPS16_TLS_LDM:
349     case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
350     case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
351     case elfcpp::R_MIPS16_TLS_GOTTPREL:
352     case elfcpp::R_MIPS16_TLS_TPREL_HI16:
353     case elfcpp::R_MIPS16_TLS_TPREL_LO16:
354       return true;
355 
356     default:
357       return false;
358     }
359 }
360 
361 // Check if R_TYPE is a microMIPS reloc.
362 static inline bool
micromips_reloc(unsigned int r_type)363 micromips_reloc(unsigned int r_type)
364 {
365   switch (r_type)
366     {
367     case elfcpp::R_MICROMIPS_26_S1:
368     case elfcpp::R_MICROMIPS_HI16:
369     case elfcpp::R_MICROMIPS_LO16:
370     case elfcpp::R_MICROMIPS_GPREL16:
371     case elfcpp::R_MICROMIPS_LITERAL:
372     case elfcpp::R_MICROMIPS_GOT16:
373     case elfcpp::R_MICROMIPS_PC7_S1:
374     case elfcpp::R_MICROMIPS_PC10_S1:
375     case elfcpp::R_MICROMIPS_PC16_S1:
376     case elfcpp::R_MICROMIPS_CALL16:
377     case elfcpp::R_MICROMIPS_GOT_DISP:
378     case elfcpp::R_MICROMIPS_GOT_PAGE:
379     case elfcpp::R_MICROMIPS_GOT_OFST:
380     case elfcpp::R_MICROMIPS_GOT_HI16:
381     case elfcpp::R_MICROMIPS_GOT_LO16:
382     case elfcpp::R_MICROMIPS_SUB:
383     case elfcpp::R_MICROMIPS_HIGHER:
384     case elfcpp::R_MICROMIPS_HIGHEST:
385     case elfcpp::R_MICROMIPS_CALL_HI16:
386     case elfcpp::R_MICROMIPS_CALL_LO16:
387     case elfcpp::R_MICROMIPS_SCN_DISP:
388     case elfcpp::R_MICROMIPS_JALR:
389     case elfcpp::R_MICROMIPS_HI0_LO16:
390     case elfcpp::R_MICROMIPS_TLS_GD:
391     case elfcpp::R_MICROMIPS_TLS_LDM:
392     case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
393     case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
394     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
395     case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
396     case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
397     case elfcpp::R_MICROMIPS_GPREL7_S2:
398     case elfcpp::R_MICROMIPS_PC23_S2:
399       return true;
400 
401     default:
402       return false;
403     }
404 }
405 
406 static inline bool
is_matching_lo16_reloc(unsigned int high_reloc,unsigned int lo16_reloc)407 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
408 {
409   switch (high_reloc)
410     {
411     case elfcpp::R_MIPS_HI16:
412     case elfcpp::R_MIPS_GOT16:
413       return lo16_reloc == elfcpp::R_MIPS_LO16;
414     case elfcpp::R_MIPS_PCHI16:
415       return lo16_reloc == elfcpp::R_MIPS_PCLO16;
416     case elfcpp::R_MIPS16_HI16:
417     case elfcpp::R_MIPS16_GOT16:
418       return lo16_reloc == elfcpp::R_MIPS16_LO16;
419     case elfcpp::R_MICROMIPS_HI16:
420     case elfcpp::R_MICROMIPS_GOT16:
421       return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
422     default:
423       return false;
424     }
425 }
426 
427 // This class is used to hold information about one GOT entry.
428 // There are three types of entry:
429 //
430 //    (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
431 //          (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
432 //    (2) a SYMBOL address, where SYMBOL is not local to an input object
433 //          (sym != NULL, symndx == -1)
434 //    (3) a TLS LDM slot (there's only one of these per GOT.)
435 //          (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
436 
437 template<int size, bool big_endian>
438 class Mips_got_entry
439 {
440   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
441 
442  public:
Mips_got_entry(Mips_relobj<size,big_endian> * object,unsigned int symndx,Mips_address addend,unsigned char tls_type,unsigned int shndx,bool is_section_symbol)443   Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
444                  Mips_address addend, unsigned char tls_type,
445                  unsigned int shndx, bool is_section_symbol)
446     : addend_(addend), symndx_(symndx), tls_type_(tls_type),
447       is_section_symbol_(is_section_symbol), shndx_(shndx)
448   { this->d.object = object; }
449 
Mips_got_entry(Mips_symbol<size> * sym,unsigned char tls_type)450   Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
451     : addend_(0), symndx_(-1U), tls_type_(tls_type),
452       is_section_symbol_(false), shndx_(-1U)
453   { this->d.sym = sym; }
454 
455   // Return whether this entry is for a local symbol.
456   bool
is_for_local_symbol() const457   is_for_local_symbol() const
458   { return this->symndx_ != -1U; }
459 
460   // Return whether this entry is for a global symbol.
461   bool
is_for_global_symbol() const462   is_for_global_symbol() const
463   { return this->symndx_ == -1U; }
464 
465   // Return the hash of this entry.
466   size_t
hash() const467   hash() const
468   {
469     if (this->tls_type_ == GOT_TLS_LDM)
470       return this->symndx_ + (1 << 18);
471 
472     size_t name_hash_value = gold::string_hash<char>(
473         (this->symndx_ != -1U)
474          ? this->d.object->name().c_str()
475          : this->d.sym->name());
476     size_t addend = this->addend_;
477     return name_hash_value ^ this->symndx_ ^ (addend << 16);
478   }
479 
480   // Return whether this entry is equal to OTHER.
481   bool
equals(Mips_got_entry<size,big_endian> * other) const482   equals(Mips_got_entry<size, big_endian>* other) const
483   {
484     if (this->symndx_ != other->symndx_
485         || this->tls_type_ != other->tls_type_)
486       return false;
487 
488     if (this->tls_type_ == GOT_TLS_LDM)
489       return true;
490 
491     return (((this->symndx_ != -1U)
492               ? (this->d.object == other->d.object)
493               : (this->d.sym == other->d.sym))
494             && (this->addend_ == other->addend_));
495   }
496 
497   // Return input object that needs this GOT entry.
498   Mips_relobj<size, big_endian>*
object() const499   object() const
500   {
501     gold_assert(this->symndx_ != -1U);
502     return this->d.object;
503   }
504 
505   // Return local symbol index for local GOT entries.
506   unsigned int
symndx() const507   symndx() const
508   {
509     gold_assert(this->symndx_ != -1U);
510     return this->symndx_;
511   }
512 
513   // Return the relocation addend for local GOT entries.
514   Mips_address
addend() const515   addend() const
516   { return this->addend_; }
517 
518   // Return global symbol for global GOT entries.
519   Mips_symbol<size>*
sym() const520   sym() const
521   {
522     gold_assert(this->symndx_ == -1U);
523     return this->d.sym;
524   }
525 
526   // Return whether this is a TLS GOT entry.
527   bool
is_tls_entry() const528   is_tls_entry() const
529   { return this->tls_type_ != GOT_TLS_NONE; }
530 
531   // Return TLS type of this GOT entry.
532   unsigned char
tls_type() const533   tls_type() const
534   { return this->tls_type_; }
535 
536   // Return section index of the local symbol for local GOT entries.
537   unsigned int
shndx() const538   shndx() const
539   { return this->shndx_; }
540 
541   // Return whether this is a STT_SECTION symbol.
542   bool
is_section_symbol() const543   is_section_symbol() const
544   { return this->is_section_symbol_; }
545 
546  private:
547   // The addend.
548   Mips_address addend_;
549 
550   // The index of the symbol if we have a local symbol; -1 otherwise.
551   unsigned int symndx_;
552 
553   union
554   {
555     // The input object for local symbols that needs the GOT entry.
556     Mips_relobj<size, big_endian>* object;
557     // If symndx == -1, the global symbol corresponding to this GOT entry.  The
558     // symbol's entry is in the local area if mips_sym->global_got_area is
559     // GGA_NONE, otherwise it is in the global area.
560     Mips_symbol<size>* sym;
561   } d;
562 
563   // The TLS type of this GOT entry.  An LDM GOT entry will be a local
564   // symbol entry with r_symndx == 0.
565   unsigned char tls_type_;
566 
567   // Whether this is a STT_SECTION symbol.
568   bool is_section_symbol_;
569 
570   // For local GOT entries, section index of the local symbol.
571   unsigned int shndx_;
572 };
573 
574 // Hash for Mips_got_entry.
575 
576 template<int size, bool big_endian>
577 class Mips_got_entry_hash
578 {
579  public:
580   size_t
operator ()(Mips_got_entry<size,big_endian> * entry) const581   operator()(Mips_got_entry<size, big_endian>* entry) const
582   { return entry->hash(); }
583 };
584 
585 // Equality for Mips_got_entry.
586 
587 template<int size, bool big_endian>
588 class Mips_got_entry_eq
589 {
590  public:
591   bool
operator ()(Mips_got_entry<size,big_endian> * e1,Mips_got_entry<size,big_endian> * e2) const592   operator()(Mips_got_entry<size, big_endian>* e1,
593              Mips_got_entry<size, big_endian>* e2) const
594   { return e1->equals(e2); }
595 };
596 
597 // Hash for Mips_symbol.
598 
599 template<int size>
600 class Mips_symbol_hash
601 {
602  public:
603   size_t
operator ()(Mips_symbol<size> * sym) const604   operator()(Mips_symbol<size>* sym) const
605   { return sym->hash(); }
606 };
607 
608 // Got_page_range.  This class describes a range of addends: [MIN_ADDEND,
609 // MAX_ADDEND].  The instances form a non-overlapping list that is sorted by
610 // increasing MIN_ADDEND.
611 
612 struct Got_page_range
613 {
Got_page_range__anon5561842d0111::Got_page_range614   Got_page_range()
615     : next(NULL), min_addend(0), max_addend(0)
616   { }
617 
618   Got_page_range* next;
619   int min_addend;
620   int max_addend;
621 
622   // Return the maximum number of GOT page entries required.
623   int
get_max_pages__anon5561842d0111::Got_page_range624   get_max_pages()
625   { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
626 };
627 
628 // Got_page_entry.  This class describes the range of addends that are applied
629 // to page relocations against a given symbol.
630 
631 struct Got_page_entry
632 {
Got_page_entry__anon5561842d0111::Got_page_entry633   Got_page_entry()
634     : object(NULL), symndx(-1U), ranges(NULL)
635   { }
636 
Got_page_entry__anon5561842d0111::Got_page_entry637   Got_page_entry(Object* object_, unsigned int symndx_)
638     : object(object_), symndx(symndx_), ranges(NULL)
639   { }
640 
641   // The input object that needs the GOT page entry.
642   Object* object;
643   // The index of the symbol, as stored in the relocation r_info.
644   unsigned int symndx;
645   // The ranges for this page entry.
646   Got_page_range* ranges;
647 };
648 
649 // Hash for Got_page_entry.
650 
651 struct Got_page_entry_hash
652 {
653   size_t
operator ()__anon5561842d0111::Got_page_entry_hash654   operator()(Got_page_entry* entry) const
655   { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
656 };
657 
658 // Equality for Got_page_entry.
659 
660 struct Got_page_entry_eq
661 {
662   bool
operator ()__anon5561842d0111::Got_page_entry_eq663   operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
664   {
665     return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
666   }
667 };
668 
669 // This class is used to hold .got information when linking.
670 
671 template<int size, bool big_endian>
672 class Mips_got_info
673 {
674   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
675   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
676     Reloc_section;
677   typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
678 
679   // Unordered set of GOT entries.
680   typedef Unordered_set<Mips_got_entry<size, big_endian>*,
681       Mips_got_entry_hash<size, big_endian>,
682       Mips_got_entry_eq<size, big_endian> > Got_entry_set;
683 
684   // Unordered set of GOT page entries.
685   typedef Unordered_set<Got_page_entry*,
686       Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
687 
688   // Unordered set of global GOT entries.
689   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
690       Global_got_entry_set;
691 
692  public:
Mips_got_info()693   Mips_got_info()
694     : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
695       tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
696       got_entries_(), got_page_entries_(), got_page_offset_start_(0),
697       got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
698       offset_(0)
699   { }
700 
701   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
702   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
703   void
704   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
705                           unsigned int symndx, Mips_address addend,
706                           unsigned int r_type, unsigned int shndx,
707                           bool is_section_symbol);
708 
709   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
710   // in OBJECT.  FOR_CALL is true if the caller is only interested in
711   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
712   // relocation.
713   void
714   record_global_got_symbol(Mips_symbol<size>* mips_sym,
715                            Mips_relobj<size, big_endian>* object,
716                            unsigned int r_type, bool dyn_reloc, bool for_call);
717 
718   // Add ENTRY to master GOT and to OBJECT's GOT.
719   void
720   record_got_entry(Mips_got_entry<size, big_endian>* entry,
721                    Mips_relobj<size, big_endian>* object);
722 
723   // Record that OBJECT has a page relocation against symbol SYMNDX and
724   // that ADDEND is the addend for that relocation.
725   void
726   record_got_page_entry(Mips_relobj<size, big_endian>* object,
727                         unsigned int symndx, int addend);
728 
729   // Create all entries that should be in the local part of the GOT.
730   void
731   add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
732 
733   // Create GOT page entries.
734   void
735   add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
736 
737   // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
738   void
739   add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
740                      unsigned int non_reloc_only_global_gotno);
741 
742   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
743   void
744   add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
745 
746   // Create TLS GOT entries.
747   void
748   add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
749 
750   // Decide whether the symbol needs an entry in the global part of the primary
751   // GOT, setting global_got_area accordingly.  Count the number of global
752   // symbols that are in the primary GOT only because they have dynamic
753   // relocations R_MIPS_REL32 against them (reloc_only_gotno).
754   void
755   count_got_symbols(Symbol_table* symtab);
756 
757   // Return the offset of GOT page entry for VALUE.
758   unsigned int
759   get_got_page_offset(Mips_address value,
760                       Mips_output_data_got<size, big_endian>* got);
761 
762   // Count the number of GOT entries required.
763   void
764   count_got_entries();
765 
766   // Count the number of GOT entries required by ENTRY.  Accumulate the result.
767   void
768   count_got_entry(Mips_got_entry<size, big_endian>* entry);
769 
770   // Add FROM's GOT entries.
771   void
772   add_got_entries(Mips_got_info<size, big_endian>* from);
773 
774   // Add FROM's GOT page entries.
775   void
776   add_got_page_count(Mips_got_info<size, big_endian>* from);
777 
778   // Return GOT size.
779   unsigned int
got_size() const780   got_size() const
781   { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
782              + this->tls_gotno_) * size/8);
783   }
784 
785   // Return the number of local GOT entries.
786   unsigned int
local_gotno() const787   local_gotno() const
788   { return this->local_gotno_; }
789 
790   // Return the maximum number of page GOT entries needed.
791   unsigned int
page_gotno() const792   page_gotno() const
793   { return this->page_gotno_; }
794 
795   // Return the number of global GOT entries.
796   unsigned int
global_gotno() const797   global_gotno() const
798   { return this->global_gotno_; }
799 
800   // Set the number of global GOT entries.
801   void
set_global_gotno(unsigned int global_gotno)802   set_global_gotno(unsigned int global_gotno)
803   { this->global_gotno_ = global_gotno; }
804 
805   // Return the number of GGA_RELOC_ONLY global GOT entries.
806   unsigned int
reloc_only_gotno() const807   reloc_only_gotno() const
808   { return this->reloc_only_gotno_; }
809 
810   // Return the number of TLS GOT entries.
811   unsigned int
tls_gotno() const812   tls_gotno() const
813   { return this->tls_gotno_; }
814 
815   // Return the GOT type for this GOT.  Used for multi-GOT links only.
816   unsigned int
multigot_got_type(unsigned int got_type) const817   multigot_got_type(unsigned int got_type) const
818   {
819     switch (got_type)
820       {
821       case GOT_TYPE_STANDARD:
822         return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
823       case GOT_TYPE_TLS_OFFSET:
824         return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
825       case GOT_TYPE_TLS_PAIR:
826         return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
827       default:
828         gold_unreachable();
829       }
830   }
831 
832   // Remove lazy-binding stubs for global symbols in this GOT.
833   void
834   remove_lazy_stubs(Target_mips<size, big_endian>* target);
835 
836   // Return offset of this GOT from the start of .got section.
837   unsigned int
offset() const838   offset() const
839   { return this->offset_; }
840 
841   // Set offset of this GOT from the start of .got section.
842   void
set_offset(unsigned int offset)843   set_offset(unsigned int offset)
844   { this->offset_ = offset; }
845 
846   // Set index of this GOT in multi-GOT links.
847   void
set_index(unsigned int index)848   set_index(unsigned int index)
849   { this->index_ = index; }
850 
851   // Return next GOT in multi-GOT links.
852   Mips_got_info<size, big_endian>*
next() const853   next() const
854   { return this->next_; }
855 
856   // Set next GOT in multi-GOT links.
857   void
set_next(Mips_got_info<size,big_endian> * next)858   set_next(Mips_got_info<size, big_endian>* next)
859   { this->next_ = next; }
860 
861   // Return the offset of TLS LDM entry for this GOT.
862   unsigned int
tls_ldm_offset() const863   tls_ldm_offset() const
864   { return this->tls_ldm_offset_; }
865 
866   // Set the offset of TLS LDM entry for this GOT.
867   void
set_tls_ldm_offset(unsigned int tls_ldm_offset)868   set_tls_ldm_offset(unsigned int tls_ldm_offset)
869   { this->tls_ldm_offset_ = tls_ldm_offset; }
870 
871   Global_got_entry_set&
global_got_symbols()872   global_got_symbols()
873   { return this->global_got_symbols_; }
874 
875   // Return the GOT_TLS_* type required by relocation type R_TYPE.
876   static int
mips_elf_reloc_tls_type(unsigned int r_type)877   mips_elf_reloc_tls_type(unsigned int r_type)
878   {
879     if (tls_gd_reloc(r_type))
880       return GOT_TLS_GD;
881 
882     if (tls_ldm_reloc(r_type))
883       return GOT_TLS_LDM;
884 
885     if (tls_gottprel_reloc(r_type))
886       return GOT_TLS_IE;
887 
888     return GOT_TLS_NONE;
889   }
890 
891   // Return the number of GOT slots needed for GOT TLS type TYPE.
892   static int
mips_tls_got_entries(unsigned int type)893   mips_tls_got_entries(unsigned int type)
894   {
895     switch (type)
896       {
897       case GOT_TLS_GD:
898       case GOT_TLS_LDM:
899         return 2;
900 
901       case GOT_TLS_IE:
902         return 1;
903 
904       case GOT_TLS_NONE:
905         return 0;
906 
907       default:
908         gold_unreachable();
909       }
910   }
911 
912  private:
913   // The number of local GOT entries.
914   unsigned int local_gotno_;
915   // The maximum number of page GOT entries needed.
916   unsigned int page_gotno_;
917   // The number of global GOT entries.
918   unsigned int global_gotno_;
919   // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
920   unsigned int reloc_only_gotno_;
921   // The number of TLS GOT entries.
922   unsigned int tls_gotno_;
923   // The offset of TLS LDM entry for this GOT.
924   unsigned int tls_ldm_offset_;
925   // All symbols that have global GOT entry.
926   Global_got_entry_set global_got_symbols_;
927   // A hash table holding GOT entries.
928   Got_entry_set got_entries_;
929   // A hash table of GOT page entries (only used in master GOT).
930   Got_page_entry_set got_page_entries_;
931   // The offset of first GOT page entry for this GOT.
932   unsigned int got_page_offset_start_;
933   // The offset of next available GOT page entry for this GOT.
934   unsigned int got_page_offset_next_;
935   // A hash table that maps GOT page entry value to the GOT offset where
936   // the entry is located.
937   Got_page_offsets got_page_offsets_;
938   // In multi-GOT links, a pointer to the next GOT.
939   Mips_got_info<size, big_endian>* next_;
940   // Index of this GOT in multi-GOT links.
941   unsigned int index_;
942   // The offset of this GOT in multi-GOT links.
943   unsigned int offset_;
944 };
945 
946 // This is a helper class used during relocation scan.  It records GOT16 addend.
947 
948 template<int size, bool big_endian>
949 struct got16_addend
950 {
951   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
952 
got16_addend__anon5561842d0111::got16_addend953   got16_addend(const Sized_relobj_file<size, big_endian>* _object,
954                unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
955                Mips_address _addend)
956     : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
957       addend(_addend)
958   { }
959 
960   const Sized_relobj_file<size, big_endian>* object;
961   unsigned int shndx;
962   unsigned int r_type;
963   unsigned int r_sym;
964   Mips_address addend;
965 };
966 
967 // .MIPS.abiflags section content
968 
969 template<bool big_endian>
970 struct Mips_abiflags
971 {
972   typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
973   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
974   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
975 
Mips_abiflags__anon5561842d0111::Mips_abiflags976   Mips_abiflags()
977     : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
978       cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
979   { }
980 
981   // Version of flags structure.
982   Valtype16 version;
983   // The level of the ISA: 1-5, 32, 64.
984   Valtype8 isa_level;
985   // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
986   Valtype8 isa_rev;
987   // The size of general purpose registers.
988   Valtype8 gpr_size;
989   // The size of co-processor 1 registers.
990   Valtype8 cpr1_size;
991   // The size of co-processor 2 registers.
992   Valtype8 cpr2_size;
993   // The floating-point ABI.
994   Valtype8 fp_abi;
995   // Processor-specific extension.
996   Valtype32 isa_ext;
997   // Mask of ASEs used.
998   Valtype32 ases;
999   // Mask of general flags.
1000   Valtype32 flags1;
1001   Valtype32 flags2;
1002 };
1003 
1004 // Mips_symbol class.  Holds additional symbol information needed for Mips.
1005 
1006 template<int size>
1007 class Mips_symbol : public Sized_symbol<size>
1008 {
1009  public:
Mips_symbol()1010   Mips_symbol()
1011     : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1012       has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1013       pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1014       global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1015       needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1016       comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1017       mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1018   { }
1019 
1020   // Return whether this is a MIPS16 symbol.
1021   bool
is_mips16() const1022   is_mips16() const
1023   {
1024     // (st_other & STO_MIPS16) == STO_MIPS16
1025     return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1026             == elfcpp::STO_MIPS16 >> 2);
1027   }
1028 
1029   // Return whether this is a microMIPS symbol.
1030   bool
is_micromips() const1031   is_micromips() const
1032   {
1033     // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1034     return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1035             == elfcpp::STO_MICROMIPS >> 2);
1036   }
1037 
1038   // Return whether the symbol needs MIPS16 fn_stub.
1039   bool
need_fn_stub() const1040   need_fn_stub() const
1041   { return this->need_fn_stub_; }
1042 
1043   // Set that the symbol needs MIPS16 fn_stub.
1044   void
set_need_fn_stub()1045   set_need_fn_stub()
1046   { this->need_fn_stub_ = true; }
1047 
1048   // Return whether this symbol is referenced by branch relocations from
1049   // any non-PIC input file.
1050   bool
has_nonpic_branches() const1051   has_nonpic_branches() const
1052   { return this->has_nonpic_branches_; }
1053 
1054   // Set that this symbol is referenced by branch relocations from
1055   // any non-PIC input file.
1056   void
set_has_nonpic_branches()1057   set_has_nonpic_branches()
1058   { this->has_nonpic_branches_ = true; }
1059 
1060   // Return the offset of the la25 stub for this symbol from the start of the
1061   // la25 stub section.
1062   unsigned int
la25_stub_offset() const1063   la25_stub_offset() const
1064   { return this->la25_stub_offset_; }
1065 
1066   // Set the offset of the la25 stub for this symbol from the start of the
1067   // la25 stub section.
1068   void
set_la25_stub_offset(unsigned int offset)1069   set_la25_stub_offset(unsigned int offset)
1070   { this->la25_stub_offset_ = offset; }
1071 
1072   // Return whether the symbol has la25 stub.  This is true if this symbol is
1073   // for a PIC function, and there are non-PIC branches and jumps to it.
1074   bool
has_la25_stub() const1075   has_la25_stub() const
1076   { return this->la25_stub_offset_ != -1U; }
1077 
1078   // Return whether there is a relocation against this symbol that must be
1079   // resolved by the static linker (that is, the relocation cannot possibly
1080   // be made dynamic).
1081   bool
has_static_relocs() const1082   has_static_relocs() const
1083   { return this->has_static_relocs_; }
1084 
1085   // Set that there is a relocation against this symbol that must be resolved
1086   // by the static linker (that is, the relocation cannot possibly be made
1087   // dynamic).
1088   void
set_has_static_relocs()1089   set_has_static_relocs()
1090   { this->has_static_relocs_ = true; }
1091 
1092   // Return whether we must not create a lazy-binding stub for this symbol.
1093   bool
no_lazy_stub() const1094   no_lazy_stub() const
1095   { return this->no_lazy_stub_; }
1096 
1097   // Set that we must not create a lazy-binding stub for this symbol.
1098   void
set_no_lazy_stub()1099   set_no_lazy_stub()
1100   { this->no_lazy_stub_ = true; }
1101 
1102   // Return the offset of the lazy-binding stub for this symbol from the start
1103   // of .MIPS.stubs section.
1104   unsigned int
lazy_stub_offset() const1105   lazy_stub_offset() const
1106   { return this->lazy_stub_offset_; }
1107 
1108   // Set the offset of the lazy-binding stub for this symbol from the start
1109   // of .MIPS.stubs section.
1110   void
set_lazy_stub_offset(unsigned int offset)1111   set_lazy_stub_offset(unsigned int offset)
1112   { this->lazy_stub_offset_ = offset; }
1113 
1114   // Return whether there are any relocations for this symbol where
1115   // pointer equality matters.
1116   bool
pointer_equality_needed() const1117   pointer_equality_needed() const
1118   { return this->pointer_equality_needed_; }
1119 
1120   // Set that there are relocations for this symbol where pointer equality
1121   // matters.
1122   void
set_pointer_equality_needed()1123   set_pointer_equality_needed()
1124   { this->pointer_equality_needed_ = true; }
1125 
1126   // Return global GOT area where this symbol in located.
1127   Global_got_area
global_got_area() const1128   global_got_area() const
1129   { return this->global_got_area_; }
1130 
1131   // Set global GOT area where this symbol in located.
1132   void
set_global_got_area(Global_got_area global_got_area)1133   set_global_got_area(Global_got_area global_got_area)
1134   { this->global_got_area_ = global_got_area; }
1135 
1136   // Return the global GOT offset for this symbol.  For multi-GOT links, this
1137   // returns the offset from the start of .got section to the first GOT entry
1138   // for the symbol.  Note that in multi-GOT links the symbol can have entry
1139   // in more than one GOT.
1140   unsigned int
global_gotoffset() const1141   global_gotoffset() const
1142   { return this->global_gotoffset_; }
1143 
1144   // Set the global GOT offset for this symbol.  Note that in multi-GOT links
1145   // the symbol can have entry in more than one GOT.  This method will set
1146   // the offset only if it is less than current offset.
1147   void
set_global_gotoffset(unsigned int offset)1148   set_global_gotoffset(unsigned int offset)
1149   {
1150     if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1151       this->global_gotoffset_ = offset;
1152   }
1153 
1154   // Return whether all GOT relocations for this symbol are for calls.
1155   bool
got_only_for_calls() const1156   got_only_for_calls() const
1157   { return this->got_only_for_calls_; }
1158 
1159   // Set that there is a GOT relocation for this symbol that is not for call.
1160   void
set_got_not_only_for_calls()1161   set_got_not_only_for_calls()
1162   { this->got_only_for_calls_ = false; }
1163 
1164   // Return whether this is a PIC symbol.
1165   bool
is_pic() const1166   is_pic() const
1167   {
1168     // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1169     return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1170             == (elfcpp::STO_MIPS_PIC >> 2));
1171   }
1172 
1173   // Set the flag in st_other field that marks this symbol as PIC.
1174   void
set_pic()1175   set_pic()
1176   {
1177     if (this->is_mips16())
1178       // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1179       this->set_nonvis((this->nonvis()
1180                         & ~((elfcpp::STO_MIPS16 >> 2)
1181                             | (elfcpp::STO_MIPS_FLAGS >> 2)))
1182                        | (elfcpp::STO_MIPS_PIC >> 2));
1183     else
1184       // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1185       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1186                        | (elfcpp::STO_MIPS_PIC >> 2));
1187   }
1188 
1189   // Set the flag in st_other field that marks this symbol as PLT.
1190   void
set_mips_plt()1191   set_mips_plt()
1192   {
1193     if (this->is_mips16())
1194       // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1195       this->set_nonvis((this->nonvis()
1196                         & ((elfcpp::STO_MIPS16 >> 2)
1197                            | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1198                        | (elfcpp::STO_MIPS_PLT >> 2));
1199 
1200     else
1201       // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1202       this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1203                        | (elfcpp::STO_MIPS_PLT >> 2));
1204   }
1205 
1206   // Downcast a base pointer to a Mips_symbol pointer.
1207   static Mips_symbol<size>*
as_mips_sym(Symbol * sym)1208   as_mips_sym(Symbol* sym)
1209   { return static_cast<Mips_symbol<size>*>(sym); }
1210 
1211   // Downcast a base pointer to a Mips_symbol pointer.
1212   static const Mips_symbol<size>*
as_mips_sym(const Symbol * sym)1213   as_mips_sym(const Symbol* sym)
1214   { return static_cast<const Mips_symbol<size>*>(sym); }
1215 
1216   // Return whether the symbol has lazy-binding stub.
1217   bool
has_lazy_stub() const1218   has_lazy_stub() const
1219   { return this->has_lazy_stub_; }
1220 
1221   // Set whether the symbol has lazy-binding stub.
1222   void
set_has_lazy_stub(bool has_lazy_stub)1223   set_has_lazy_stub(bool has_lazy_stub)
1224   { this->has_lazy_stub_ = has_lazy_stub; }
1225 
1226   // Return whether the symbol needs a standard PLT entry.
1227   bool
needs_mips_plt() const1228   needs_mips_plt() const
1229   { return this->needs_mips_plt_; }
1230 
1231   // Set whether the symbol needs a standard PLT entry.
1232   void
set_needs_mips_plt(bool needs_mips_plt)1233   set_needs_mips_plt(bool needs_mips_plt)
1234   { this->needs_mips_plt_ = needs_mips_plt; }
1235 
1236   // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1237   // entry.
1238   bool
needs_comp_plt() const1239   needs_comp_plt() const
1240   { return this->needs_comp_plt_; }
1241 
1242   // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1243   void
set_needs_comp_plt(bool needs_comp_plt)1244   set_needs_comp_plt(bool needs_comp_plt)
1245   { this->needs_comp_plt_ = needs_comp_plt; }
1246 
1247   // Return standard PLT entry offset, or -1 if none.
1248   unsigned int
mips_plt_offset() const1249   mips_plt_offset() const
1250   { return this->mips_plt_offset_; }
1251 
1252   // Set standard PLT entry offset.
1253   void
set_mips_plt_offset(unsigned int mips_plt_offset)1254   set_mips_plt_offset(unsigned int mips_plt_offset)
1255   { this->mips_plt_offset_ = mips_plt_offset; }
1256 
1257   // Return whether the symbol has standard PLT entry.
1258   bool
has_mips_plt_offset() const1259   has_mips_plt_offset() const
1260   { return this->mips_plt_offset_ != -1U; }
1261 
1262   // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1263   unsigned int
comp_plt_offset() const1264   comp_plt_offset() const
1265   { return this->comp_plt_offset_; }
1266 
1267   // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1268   void
set_comp_plt_offset(unsigned int comp_plt_offset)1269   set_comp_plt_offset(unsigned int comp_plt_offset)
1270   { this->comp_plt_offset_ = comp_plt_offset; }
1271 
1272   // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1273   bool
has_comp_plt_offset() const1274   has_comp_plt_offset() const
1275   { return this->comp_plt_offset_ != -1U; }
1276 
1277   // Return MIPS16 fn stub for a symbol.
1278   template<bool big_endian>
1279   Mips16_stub_section<size, big_endian>*
get_mips16_fn_stub() const1280   get_mips16_fn_stub() const
1281   {
1282     return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1283   }
1284 
1285   // Set MIPS16 fn stub for a symbol.
1286   void
set_mips16_fn_stub(Mips16_stub_section_base * stub)1287   set_mips16_fn_stub(Mips16_stub_section_base* stub)
1288   { this->mips16_fn_stub_ = stub; }
1289 
1290   // Return whether symbol has MIPS16 fn stub.
1291   bool
has_mips16_fn_stub() const1292   has_mips16_fn_stub() const
1293   { return this->mips16_fn_stub_ != NULL; }
1294 
1295   // Return MIPS16 call stub for a symbol.
1296   template<bool big_endian>
1297   Mips16_stub_section<size, big_endian>*
get_mips16_call_stub() const1298   get_mips16_call_stub() const
1299   {
1300     return static_cast<Mips16_stub_section<size, big_endian>*>(
1301       mips16_call_stub_);
1302   }
1303 
1304   // Set MIPS16 call stub for a symbol.
1305   void
set_mips16_call_stub(Mips16_stub_section_base * stub)1306   set_mips16_call_stub(Mips16_stub_section_base* stub)
1307   { this->mips16_call_stub_ = stub; }
1308 
1309   // Return whether symbol has MIPS16 call stub.
1310   bool
has_mips16_call_stub() const1311   has_mips16_call_stub() const
1312   { return this->mips16_call_stub_ != NULL; }
1313 
1314   // Return MIPS16 call_fp stub for a symbol.
1315   template<bool big_endian>
1316   Mips16_stub_section<size, big_endian>*
get_mips16_call_fp_stub() const1317   get_mips16_call_fp_stub() const
1318   {
1319     return static_cast<Mips16_stub_section<size, big_endian>*>(
1320       mips16_call_fp_stub_);
1321   }
1322 
1323   // Set MIPS16 call_fp stub for a symbol.
1324   void
set_mips16_call_fp_stub(Mips16_stub_section_base * stub)1325   set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1326   { this->mips16_call_fp_stub_ = stub; }
1327 
1328   // Return whether symbol has MIPS16 call_fp stub.
1329   bool
has_mips16_call_fp_stub() const1330   has_mips16_call_fp_stub() const
1331   { return this->mips16_call_fp_stub_ != NULL; }
1332 
1333   bool
get_applied_secondary_got_fixup() const1334   get_applied_secondary_got_fixup() const
1335   { return applied_secondary_got_fixup_; }
1336 
1337   void
set_applied_secondary_got_fixup()1338   set_applied_secondary_got_fixup()
1339   { this->applied_secondary_got_fixup_ = true; }
1340 
1341   // Return the hash of this symbol.
1342   size_t
hash() const1343   hash() const
1344   {
1345     return gold::string_hash<char>(this->name());
1346   }
1347 
1348  private:
1349   // Whether the symbol needs MIPS16 fn_stub.  This is true if this symbol
1350   // appears in any relocs other than a 16 bit call.
1351   bool need_fn_stub_;
1352 
1353   // True if this symbol is referenced by branch relocations from
1354   // any non-PIC input file.  This is used to determine whether an
1355   // la25 stub is required.
1356   bool has_nonpic_branches_;
1357 
1358   // The offset of the la25 stub for this symbol from the start of the
1359   // la25 stub section.
1360   unsigned int la25_stub_offset_;
1361 
1362   // True if there is a relocation against this symbol that must be
1363   // resolved by the static linker (that is, the relocation cannot
1364   // possibly be made dynamic).
1365   bool has_static_relocs_;
1366 
1367   // Whether we must not create a lazy-binding stub for this symbol.
1368   // This is true if the symbol has relocations related to taking the
1369   // function's address.
1370   bool no_lazy_stub_;
1371 
1372   // The offset of the lazy-binding stub for this symbol from the start of
1373   // .MIPS.stubs section.
1374   unsigned int lazy_stub_offset_;
1375 
1376   // True if there are any relocations for this symbol where pointer equality
1377   // matters.
1378   bool pointer_equality_needed_;
1379 
1380   // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1381   // in the global part of the GOT.
1382   Global_got_area global_got_area_;
1383 
1384   // The global GOT offset for this symbol.  For multi-GOT links, this is offset
1385   // from the start of .got section to the first GOT entry for the symbol.
1386   // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1387   unsigned int global_gotoffset_;
1388 
1389   // Whether all GOT relocations for this symbol are for calls.
1390   bool got_only_for_calls_;
1391   // Whether the symbol has lazy-binding stub.
1392   bool has_lazy_stub_;
1393   // Whether the symbol needs a standard PLT entry.
1394   bool needs_mips_plt_;
1395   // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1396   bool needs_comp_plt_;
1397   // Standard PLT entry offset, or -1 if none.
1398   unsigned int mips_plt_offset_;
1399   // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1400   unsigned int comp_plt_offset_;
1401   // MIPS16 fn stub for a symbol.
1402   Mips16_stub_section_base* mips16_fn_stub_;
1403   // MIPS16 call stub for a symbol.
1404   Mips16_stub_section_base* mips16_call_stub_;
1405   // MIPS16 call_fp stub for a symbol.
1406   Mips16_stub_section_base* mips16_call_fp_stub_;
1407 
1408   bool applied_secondary_got_fixup_;
1409 };
1410 
1411 // Mips16_stub_section class.
1412 
1413 // The mips16 compiler uses a couple of special sections to handle
1414 // floating point arguments.
1415 
1416 // Section names that look like .mips16.fn.FNNAME contain stubs that
1417 // copy floating point arguments from the fp regs to the gp regs and
1418 // then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1419 // call should be redirected to the stub instead.  If no 32 bit
1420 // function calls FNNAME, the stub should be discarded.  We need to
1421 // consider any reference to the function, not just a call, because
1422 // if the address of the function is taken we will need the stub,
1423 // since the address might be passed to a 32 bit function.
1424 
1425 // Section names that look like .mips16.call.FNNAME contain stubs
1426 // that copy floating point arguments from the gp regs to the fp
1427 // regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1428 // then any 16 bit function that calls FNNAME should be redirected
1429 // to the stub instead.  If FNNAME is not a 32 bit function, the
1430 // stub should be discarded.
1431 
1432 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1433 // which call FNNAME and then copy the return value from the fp regs
1434 // to the gp regs.  These stubs store the return address in $18 while
1435 // calling FNNAME; any function which might call one of these stubs
1436 // must arrange to save $18 around the call.  (This case is not
1437 // needed for 32 bit functions that call 16 bit functions, because
1438 // 16 bit functions always return floating point values in both
1439 // $f0/$f1 and $2/$3.)
1440 
1441 // Note that in all cases FNNAME might be defined statically.
1442 // Therefore, FNNAME is not used literally.  Instead, the relocation
1443 // information will indicate which symbol the section is for.
1444 
1445 // We record any stubs that we find in the symbol table.
1446 
1447 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1448 
1449 class Mips16_stub_section_base { };
1450 
1451 template<int size, bool big_endian>
1452 class Mips16_stub_section : public Mips16_stub_section_base
1453 {
1454   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1455 
1456  public:
Mips16_stub_section(Mips_relobj<size,big_endian> * object,unsigned int shndx)1457   Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1458     : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1459       found_r_mips_none_(false)
1460   {
1461     gold_assert(object->is_mips16_fn_stub_section(shndx)
1462                 || object->is_mips16_call_stub_section(shndx)
1463                 || object->is_mips16_call_fp_stub_section(shndx));
1464   }
1465 
1466   // Return the object of this stub section.
1467   Mips_relobj<size, big_endian>*
object() const1468   object() const
1469   { return this->object_; }
1470 
1471   // Return the size of a section.
1472   uint64_t
section_size() const1473   section_size() const
1474   { return this->object_->section_size(this->shndx_); }
1475 
1476   // Return section index of this stub section.
1477   unsigned int
shndx() const1478   shndx() const
1479   { return this->shndx_; }
1480 
1481   // Return symbol index, if stub is for a local function.
1482   unsigned int
r_sym() const1483   r_sym() const
1484   { return this->r_sym_; }
1485 
1486   // Return symbol, if stub is for a global function.
1487   Mips_symbol<size>*
gsym() const1488   gsym() const
1489   { return this->gsym_; }
1490 
1491   // Return whether stub is for a local function.
1492   bool
is_for_local_function() const1493   is_for_local_function() const
1494   { return this->gsym_ == NULL; }
1495 
1496   // This method is called when a new relocation R_TYPE for local symbol R_SYM
1497   // is found in the stub section.  Try to find stub target.
1498   void
new_local_reloc_found(unsigned int r_type,unsigned int r_sym)1499   new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1500   {
1501     // To find target symbol for this stub, trust the first R_MIPS_NONE
1502     // relocation, if any.  Otherwise trust the first relocation, whatever
1503     // its kind.
1504     if (this->found_r_mips_none_)
1505       return;
1506     if (r_type == elfcpp::R_MIPS_NONE)
1507       {
1508         this->r_sym_ = r_sym;
1509         this->gsym_ = NULL;
1510         this->found_r_mips_none_ = true;
1511       }
1512     else if (!is_target_found())
1513       this->r_sym_ = r_sym;
1514   }
1515 
1516   // This method is called when a new relocation R_TYPE for global symbol GSYM
1517   // is found in the stub section.  Try to find stub target.
1518   void
new_global_reloc_found(unsigned int r_type,Mips_symbol<size> * gsym)1519   new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1520   {
1521     // To find target symbol for this stub, trust the first R_MIPS_NONE
1522     // relocation, if any.  Otherwise trust the first relocation, whatever
1523     // its kind.
1524     if (this->found_r_mips_none_)
1525       return;
1526     if (r_type == elfcpp::R_MIPS_NONE)
1527       {
1528         this->gsym_ = gsym;
1529         this->r_sym_ = 0;
1530         this->found_r_mips_none_ = true;
1531       }
1532     else if (!is_target_found())
1533       this->gsym_ = gsym;
1534   }
1535 
1536   // Return whether we found the stub target.
1537   bool
is_target_found() const1538   is_target_found() const
1539   { return this->r_sym_ != 0 || this->gsym_ != NULL;  }
1540 
1541   // Return whether this is a fn stub.
1542   bool
is_fn_stub() const1543   is_fn_stub() const
1544   { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1545 
1546   // Return whether this is a call stub.
1547   bool
is_call_stub() const1548   is_call_stub() const
1549   { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1550 
1551   // Return whether this is a call_fp stub.
1552   bool
is_call_fp_stub() const1553   is_call_fp_stub() const
1554   { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1555 
1556   // Return the output address.
1557   Mips_address
output_address() const1558   output_address() const
1559   {
1560     return (this->object_->output_section(this->shndx_)->address()
1561             + this->object_->output_section_offset(this->shndx_));
1562   }
1563 
1564  private:
1565   // The object of this stub section.
1566   Mips_relobj<size, big_endian>* object_;
1567   // The section index of this stub section.
1568   unsigned int shndx_;
1569   // The symbol index, if stub is for a local function.
1570   unsigned int r_sym_;
1571   // The symbol, if stub is for a global function.
1572   Mips_symbol<size>* gsym_;
1573   // True if we found R_MIPS_NONE relocation in this stub.
1574   bool found_r_mips_none_;
1575 };
1576 
1577 // Mips_relobj class.
1578 
1579 template<int size, bool big_endian>
1580 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1581 {
1582   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1583   typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1584     Mips16_stubs_int_map;
1585   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1586 
1587  public:
Mips_relobj(const std::string & name,Input_file * input_file,off_t offset,const typename elfcpp::Ehdr<size,big_endian> & ehdr)1588   Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1589               const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1590     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1591       processor_specific_flags_(0), local_symbol_is_mips16_(),
1592       local_symbol_is_micromips_(), mips16_stub_sections_(),
1593       local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1594       local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1595       merge_processor_specific_data_(true), got_info_(NULL),
1596       section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1597       section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
1598       attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
1599       cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1600   {
1601     this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1602     this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1603   }
1604 
~Mips_relobj()1605   ~Mips_relobj()
1606   { delete this->attributes_section_data_; }
1607 
1608   // Downcast a base pointer to a Mips_relobj pointer.  This is
1609   // not type-safe but we only use Mips_relobj not the base class.
1610   static Mips_relobj<size, big_endian>*
as_mips_relobj(Relobj * relobj)1611   as_mips_relobj(Relobj* relobj)
1612   { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1613 
1614   // Downcast a base pointer to a Mips_relobj pointer.  This is
1615   // not type-safe but we only use Mips_relobj not the base class.
1616   static const Mips_relobj<size, big_endian>*
as_mips_relobj(const Relobj * relobj)1617   as_mips_relobj(const Relobj* relobj)
1618   { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1619 
1620   // Processor-specific flags in ELF file header.  This is valid only after
1621   // reading symbols.
1622   elfcpp::Elf_Word
processor_specific_flags() const1623   processor_specific_flags() const
1624   { return this->processor_specific_flags_; }
1625 
1626   // Whether a local symbol is MIPS16 symbol.  R_SYM is the symbol table
1627   // index.  This is only valid after do_count_local_symbol is called.
1628   bool
local_symbol_is_mips16(unsigned int r_sym) const1629   local_symbol_is_mips16(unsigned int r_sym) const
1630   {
1631     gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1632     return this->local_symbol_is_mips16_[r_sym];
1633   }
1634 
1635   // Whether a local symbol is microMIPS symbol.  R_SYM is the symbol table
1636   // index.  This is only valid after do_count_local_symbol is called.
1637   bool
local_symbol_is_micromips(unsigned int r_sym) const1638   local_symbol_is_micromips(unsigned int r_sym) const
1639   {
1640     gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1641     return this->local_symbol_is_micromips_[r_sym];
1642   }
1643 
1644   // Get or create MIPS16 stub section.
1645   Mips16_stub_section<size, big_endian>*
get_mips16_stub_section(unsigned int shndx)1646   get_mips16_stub_section(unsigned int shndx)
1647   {
1648     typename Mips16_stubs_int_map::const_iterator it =
1649       this->mips16_stub_sections_.find(shndx);
1650     if (it != this->mips16_stub_sections_.end())
1651       return (*it).second;
1652 
1653     Mips16_stub_section<size, big_endian>* stub_section =
1654       new Mips16_stub_section<size, big_endian>(this, shndx);
1655     this->mips16_stub_sections_.insert(
1656       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1657         stub_section->shndx(), stub_section));
1658     return stub_section;
1659   }
1660 
1661   // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1662   // object doesn't have fn stub for R_SYM.
1663   Mips16_stub_section<size, big_endian>*
get_local_mips16_fn_stub(unsigned int r_sym) const1664   get_local_mips16_fn_stub(unsigned int r_sym) const
1665   {
1666     typename Mips16_stubs_int_map::const_iterator it =
1667       this->local_mips16_fn_stubs_.find(r_sym);
1668     if (it != this->local_mips16_fn_stubs_.end())
1669       return (*it).second;
1670     return NULL;
1671   }
1672 
1673   // Record that this object has MIPS16 fn stub for local symbol.  This method
1674   // is only called if we decided not to discard the stub.
1675   void
add_local_mips16_fn_stub(Mips16_stub_section<size,big_endian> * stub)1676   add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1677   {
1678     gold_assert(stub->is_for_local_function());
1679     unsigned int r_sym = stub->r_sym();
1680     this->local_mips16_fn_stubs_.insert(
1681       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1682         r_sym, stub));
1683   }
1684 
1685   // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1686   // object doesn't have call stub for R_SYM.
1687   Mips16_stub_section<size, big_endian>*
get_local_mips16_call_stub(unsigned int r_sym) const1688   get_local_mips16_call_stub(unsigned int r_sym) const
1689   {
1690     typename Mips16_stubs_int_map::const_iterator it =
1691       this->local_mips16_call_stubs_.find(r_sym);
1692     if (it != this->local_mips16_call_stubs_.end())
1693       return (*it).second;
1694     return NULL;
1695   }
1696 
1697   // Record that this object has MIPS16 call stub for local symbol.  This method
1698   // is only called if we decided not to discard the stub.
1699   void
add_local_mips16_call_stub(Mips16_stub_section<size,big_endian> * stub)1700   add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1701   {
1702     gold_assert(stub->is_for_local_function());
1703     unsigned int r_sym = stub->r_sym();
1704     this->local_mips16_call_stubs_.insert(
1705       std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1706         r_sym, stub));
1707   }
1708 
1709   // Record that we found "non 16-bit" call relocation against local symbol
1710   // SYMNDX.  This reloc would need to refer to a MIPS16 fn stub, if there
1711   // is one.
1712   void
add_local_non_16bit_call(unsigned int symndx)1713   add_local_non_16bit_call(unsigned int symndx)
1714   { this->local_non_16bit_calls_.insert(symndx); }
1715 
1716   // Return true if there is any "non 16-bit" call relocation against local
1717   // symbol SYMNDX in this object.
1718   bool
has_local_non_16bit_call_relocs(unsigned int symndx)1719   has_local_non_16bit_call_relocs(unsigned int symndx)
1720   {
1721     return (this->local_non_16bit_calls_.find(symndx)
1722             != this->local_non_16bit_calls_.end());
1723   }
1724 
1725   // Record that we found 16-bit call relocation R_MIPS16_26 against local
1726   // symbol SYMNDX.  Local MIPS16 call or call_fp stubs will only be needed
1727   // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1728   void
add_local_16bit_call(unsigned int symndx)1729   add_local_16bit_call(unsigned int symndx)
1730   { this->local_16bit_calls_.insert(symndx); }
1731 
1732   // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1733   // symbol SYMNDX in this object.
1734   bool
has_local_16bit_call_relocs(unsigned int symndx)1735   has_local_16bit_call_relocs(unsigned int symndx)
1736   {
1737     return (this->local_16bit_calls_.find(symndx)
1738             != this->local_16bit_calls_.end());
1739   }
1740 
1741   // Get gp value that was used to create this object.
1742   Mips_address
gp_value() const1743   gp_value() const
1744   { return this->gp_; }
1745 
1746   // Return whether the object is a PIC object.
1747   bool
is_pic() const1748   is_pic() const
1749   { return this->is_pic_; }
1750 
1751   // Return whether the object uses N32 ABI.
1752   bool
is_n32() const1753   is_n32() const
1754   { return this->is_n32_; }
1755 
1756   // Return whether the object uses N64 ABI.
1757   bool
is_n64() const1758   is_n64() const
1759   { return size == 64; }
1760 
1761   // Return whether the object uses NewABI conventions.
1762   bool
is_newabi() const1763   is_newabi() const
1764   { return this->is_n32() || this->is_n64(); }
1765 
1766   // Return Mips_got_info for this object.
1767   Mips_got_info<size, big_endian>*
get_got_info() const1768   get_got_info() const
1769   { return this->got_info_; }
1770 
1771   // Return Mips_got_info for this object.  Create new info if it doesn't exist.
1772   Mips_got_info<size, big_endian>*
get_or_create_got_info()1773   get_or_create_got_info()
1774   {
1775     if (!this->got_info_)
1776       this->got_info_ = new Mips_got_info<size, big_endian>();
1777     return this->got_info_;
1778   }
1779 
1780   // Set Mips_got_info for this object.
1781   void
set_got_info(Mips_got_info<size,big_endian> * got_info)1782   set_got_info(Mips_got_info<size, big_endian>* got_info)
1783   { this->got_info_ = got_info; }
1784 
1785   // Whether a section SHDNX is a MIPS16 stub section.  This is only valid
1786   // after do_read_symbols is called.
1787   bool
is_mips16_stub_section(unsigned int shndx)1788   is_mips16_stub_section(unsigned int shndx)
1789   {
1790     return (is_mips16_fn_stub_section(shndx)
1791             || is_mips16_call_stub_section(shndx)
1792             || is_mips16_call_fp_stub_section(shndx));
1793   }
1794 
1795   // Return TRUE if relocations in section SHNDX can refer directly to a
1796   // MIPS16 function rather than to a hard-float stub.  This is only valid
1797   // after do_read_symbols is called.
1798   bool
section_allows_mips16_refs(unsigned int shndx)1799   section_allows_mips16_refs(unsigned int shndx)
1800   {
1801     return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1802   }
1803 
1804   // Whether a section SHDNX is a MIPS16 fn stub section.  This is only valid
1805   // after do_read_symbols is called.
1806   bool
is_mips16_fn_stub_section(unsigned int shndx)1807   is_mips16_fn_stub_section(unsigned int shndx)
1808   {
1809     gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1810     return this->section_is_mips16_fn_stub_[shndx];
1811   }
1812 
1813   // Whether a section SHDNX is a MIPS16 call stub section.  This is only valid
1814   // after do_read_symbols is called.
1815   bool
is_mips16_call_stub_section(unsigned int shndx)1816   is_mips16_call_stub_section(unsigned int shndx)
1817   {
1818     gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1819     return this->section_is_mips16_call_stub_[shndx];
1820   }
1821 
1822   // Whether a section SHDNX is a MIPS16 call_fp stub section.  This is only
1823   // valid after do_read_symbols is called.
1824   bool
is_mips16_call_fp_stub_section(unsigned int shndx)1825   is_mips16_call_fp_stub_section(unsigned int shndx)
1826   {
1827     gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1828     return this->section_is_mips16_call_fp_stub_[shndx];
1829   }
1830 
1831   // Discard MIPS16 stub secions that are not needed.
1832   void
1833   discard_mips16_stub_sections(Symbol_table* symtab);
1834 
1835   // Return whether there is a .reginfo section.
1836   bool
has_reginfo_section() const1837   has_reginfo_section() const
1838   { return this->has_reginfo_section_; }
1839 
1840   // Return whether we want to merge processor-specific data.
1841   bool
merge_processor_specific_data() const1842   merge_processor_specific_data() const
1843   { return this->merge_processor_specific_data_; }
1844 
1845   // Return gprmask from the .reginfo section of this object.
1846   Valtype
gprmask() const1847   gprmask() const
1848   { return this->gprmask_; }
1849 
1850   // Return cprmask1 from the .reginfo section of this object.
1851   Valtype
cprmask1() const1852   cprmask1() const
1853   { return this->cprmask1_; }
1854 
1855   // Return cprmask2 from the .reginfo section of this object.
1856   Valtype
cprmask2() const1857   cprmask2() const
1858   { return this->cprmask2_; }
1859 
1860   // Return cprmask3 from the .reginfo section of this object.
1861   Valtype
cprmask3() const1862   cprmask3() const
1863   { return this->cprmask3_; }
1864 
1865   // Return cprmask4 from the .reginfo section of this object.
1866   Valtype
cprmask4() const1867   cprmask4() const
1868   { return this->cprmask4_; }
1869 
1870   // This is the contents of the .MIPS.abiflags section if there is one.
1871   Mips_abiflags<big_endian>*
abiflags()1872   abiflags()
1873   { return this->abiflags_; }
1874 
1875   // This is the contents of the .gnu.attribute section if there is one.
1876   const Attributes_section_data*
attributes_section_data() const1877   attributes_section_data() const
1878   { return this->attributes_section_data_; }
1879 
1880  protected:
1881   // Count the local symbols.
1882   void
1883   do_count_local_symbols(Stringpool_template<char>*,
1884                          Stringpool_template<char>*);
1885 
1886   // Read the symbol information.
1887   void
1888   do_read_symbols(Read_symbols_data* sd);
1889 
1890  private:
1891   // The name of the options section.
mips_elf_options_section_name()1892   const char* mips_elf_options_section_name()
1893   { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1894 
1895   // processor-specific flags in ELF file header.
1896   elfcpp::Elf_Word processor_specific_flags_;
1897 
1898   // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1899   // This is only valid after do_count_local_symbol is called.
1900   std::vector<bool> local_symbol_is_mips16_;
1901 
1902   // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1903   // This is only valid after do_count_local_symbol is called.
1904   std::vector<bool> local_symbol_is_micromips_;
1905 
1906   // Map from section index to the MIPS16 stub for that section.  This contains
1907   // all stubs found in this object.
1908   Mips16_stubs_int_map mips16_stub_sections_;
1909 
1910   // Local symbols that have "non 16-bit" call relocation.  This relocation
1911   // would need to refer to a MIPS16 fn stub, if there is one.
1912   std::set<unsigned int> local_non_16bit_calls_;
1913 
1914   // Local symbols that have 16-bit call relocation R_MIPS16_26.  Local MIPS16
1915   // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1916   // relocation that refers to the stub symbol.
1917   std::set<unsigned int> local_16bit_calls_;
1918 
1919   // Map from local symbol index to the MIPS16 fn stub for that symbol.
1920   // This contains only the stubs that we decided not to discard.
1921   Mips16_stubs_int_map local_mips16_fn_stubs_;
1922 
1923   // Map from local symbol index to the MIPS16 call stub for that symbol.
1924   // This contains only the stubs that we decided not to discard.
1925   Mips16_stubs_int_map local_mips16_call_stubs_;
1926 
1927   // gp value that was used to create this object.
1928   Mips_address gp_;
1929   // Whether the object is a PIC object.
1930   bool is_pic_ : 1;
1931   // Whether the object uses N32 ABI.
1932   bool is_n32_ : 1;
1933   // Whether the object contains a .reginfo section.
1934   bool has_reginfo_section_ : 1;
1935   // Whether we merge processor-specific data of this object to output.
1936   bool merge_processor_specific_data_ : 1;
1937   // The Mips_got_info for this object.
1938   Mips_got_info<size, big_endian>* got_info_;
1939 
1940   // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1941   // This is only valid after do_read_symbols is called.
1942   std::vector<bool> section_is_mips16_fn_stub_;
1943 
1944   // Bit vector to tell if a section is a MIPS16 call stub section or not.
1945   // This is only valid after do_read_symbols is called.
1946   std::vector<bool> section_is_mips16_call_stub_;
1947 
1948   // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1949   // This is only valid after do_read_symbols is called.
1950   std::vector<bool> section_is_mips16_call_fp_stub_;
1951 
1952   // .pdr section index.
1953   unsigned int pdr_shndx_;
1954 
1955   // Object attributes if there is a .gnu.attributes section or NULL.
1956   Attributes_section_data* attributes_section_data_;
1957 
1958   // Object abiflags if there is a .MIPS.abiflags section or NULL.
1959   Mips_abiflags<big_endian>* abiflags_;
1960 
1961   // gprmask from the .reginfo section of this object.
1962   Valtype gprmask_;
1963   // cprmask1 from the .reginfo section of this object.
1964   Valtype cprmask1_;
1965   // cprmask2 from the .reginfo section of this object.
1966   Valtype cprmask2_;
1967   // cprmask3 from the .reginfo section of this object.
1968   Valtype cprmask3_;
1969   // cprmask4 from the .reginfo section of this object.
1970   Valtype cprmask4_;
1971 };
1972 
1973 // Mips_output_data_got class.
1974 
1975 template<int size, bool big_endian>
1976 class Mips_output_data_got : public Output_data_got<size, big_endian>
1977 {
1978   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1979   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1980     Reloc_section;
1981   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1982 
1983  public:
Mips_output_data_got(Target_mips<size,big_endian> * target,Symbol_table * symtab,Layout * layout)1984   Mips_output_data_got(Target_mips<size, big_endian>* target,
1985       Symbol_table* symtab, Layout* layout)
1986     : Output_data_got<size, big_endian>(), target_(target),
1987       symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1988       first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1989       secondary_got_relocs_()
1990   {
1991     this->master_got_info_ = new Mips_got_info<size, big_endian>();
1992     this->set_addralign(16);
1993   }
1994 
1995   // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1996   // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1997   void
record_local_got_symbol(Mips_relobj<size,big_endian> * object,unsigned int symndx,Mips_address addend,unsigned int r_type,unsigned int shndx,bool is_section_symbol)1998   record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1999                           unsigned int symndx, Mips_address addend,
2000                           unsigned int r_type, unsigned int shndx,
2001                           bool is_section_symbol)
2002   {
2003     this->master_got_info_->record_local_got_symbol(object, symndx, addend,
2004                                                     r_type, shndx,
2005                                                     is_section_symbol);
2006   }
2007 
2008   // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
2009   // in OBJECT.  FOR_CALL is true if the caller is only interested in
2010   // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
2011   // relocation.
2012   void
record_global_got_symbol(Mips_symbol<size> * mips_sym,Mips_relobj<size,big_endian> * object,unsigned int r_type,bool dyn_reloc,bool for_call)2013   record_global_got_symbol(Mips_symbol<size>* mips_sym,
2014                            Mips_relobj<size, big_endian>* object,
2015                            unsigned int r_type, bool dyn_reloc, bool for_call)
2016   {
2017     this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2018                                                      dyn_reloc, for_call);
2019   }
2020 
2021   // Record that OBJECT has a page relocation against symbol SYMNDX and
2022   // that ADDEND is the addend for that relocation.
2023   void
record_got_page_entry(Mips_relobj<size,big_endian> * object,unsigned int symndx,int addend)2024   record_got_page_entry(Mips_relobj<size, big_endian>* object,
2025                         unsigned int symndx, int addend)
2026   { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2027 
2028   // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
2029   // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2030   // applied in a static link.
2031   void
add_static_reloc(unsigned int got_offset,unsigned int r_type,Mips_symbol<size> * gsym)2032   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2033                    Mips_symbol<size>* gsym)
2034   { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2035 
2036   // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
2037   // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
2038   // relocation that needs to be applied in a static link.
2039   void
add_static_reloc(unsigned int got_offset,unsigned int r_type,Sized_relobj_file<size,big_endian> * relobj,unsigned int index)2040   add_static_reloc(unsigned int got_offset, unsigned int r_type,
2041                    Sized_relobj_file<size, big_endian>* relobj,
2042                    unsigned int index)
2043   {
2044     this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2045                                                 index));
2046   }
2047 
2048   // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2049   // secondary GOT at OFFSET.
2050   void
add_secondary_got_reloc(unsigned int got_offset,unsigned int r_type,Mips_symbol<size> * gsym)2051   add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2052                           Mips_symbol<size>* gsym)
2053   {
2054     this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2055                                                        r_type, gsym));
2056   }
2057 
2058   // Update GOT entry at OFFSET with VALUE.
2059   void
update_got_entry(unsigned int offset,Mips_address value)2060   update_got_entry(unsigned int offset, Mips_address value)
2061   {
2062     elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2063   }
2064 
2065   // Return the number of entries in local part of the GOT.  This includes
2066   // local entries, page entries and 2 reserved entries.
2067   unsigned int
get_local_gotno() const2068   get_local_gotno() const
2069   {
2070     if (!this->multi_got())
2071       {
2072         return (2 + this->master_got_info_->local_gotno()
2073                 + this->master_got_info_->page_gotno());
2074       }
2075     else
2076       return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2077   }
2078 
2079   // Return dynamic symbol table index of the first symbol with global GOT
2080   // entry.
2081   unsigned int
first_global_got_dynsym_index() const2082   first_global_got_dynsym_index() const
2083   { return this->first_global_got_dynsym_index_; }
2084 
2085   // Set dynamic symbol table index of the first symbol with global GOT entry.
2086   void
set_first_global_got_dynsym_index(unsigned int index)2087   set_first_global_got_dynsym_index(unsigned int index)
2088   { this->first_global_got_dynsym_index_ = index; }
2089 
2090   // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
2091   // larger than 64K, create multi-GOT.
2092   void
2093   lay_out_got(Layout* layout, Symbol_table* symtab,
2094               const Input_objects* input_objects);
2095 
2096   // Create multi-GOT.  For every GOT, add local, global and TLS entries.
2097   void
2098   lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2099 
2100   // Attempt to merge GOTs of different input objects.
2101   void
2102   merge_gots(const Input_objects* input_objects);
2103 
2104   // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
2105   // this would lead to overflow, true if they were merged successfully.
2106   bool
2107   merge_got_with(Mips_got_info<size, big_endian>* from,
2108                  Mips_relobj<size, big_endian>* object,
2109                  Mips_got_info<size, big_endian>* to);
2110 
2111   // Return the offset of GOT page entry for VALUE.  For multi-GOT links,
2112   // use OBJECT's GOT.
2113   unsigned int
get_got_page_offset(Mips_address value,const Mips_relobj<size,big_endian> * object)2114   get_got_page_offset(Mips_address value,
2115                       const Mips_relobj<size, big_endian>* object)
2116   {
2117     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2118                                           ? this->master_got_info_
2119                                           : object->get_got_info());
2120     gold_assert(g != NULL);
2121     return g->get_got_page_offset(value, this);
2122   }
2123 
2124   // Return the GOT offset of type GOT_TYPE of the global symbol
2125   // GSYM.  For multi-GOT links, use OBJECT's GOT.
got_offset(const Symbol * gsym,unsigned int got_type,Mips_relobj<size,big_endian> * object) const2126   unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2127                           Mips_relobj<size, big_endian>* object) const
2128   {
2129     if (!this->multi_got())
2130       return gsym->got_offset(got_type);
2131     else
2132       {
2133         Mips_got_info<size, big_endian>* g = object->get_got_info();
2134         gold_assert(g != NULL);
2135         return gsym->got_offset(g->multigot_got_type(got_type));
2136       }
2137   }
2138 
2139   // Return the GOT offset of type GOT_TYPE of the local symbol
2140   // SYMNDX.
2141   unsigned int
got_offset(unsigned int symndx,unsigned int got_type,Sized_relobj_file<size,big_endian> * object,uint64_t addend) const2142   got_offset(unsigned int symndx, unsigned int got_type,
2143              Sized_relobj_file<size, big_endian>* object,
2144              uint64_t addend) const
2145   { return object->local_got_offset(symndx, got_type, addend); }
2146 
2147   // Return the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2148   unsigned int
tls_ldm_offset(Mips_relobj<size,big_endian> * object) const2149   tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2150   {
2151     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2152                                           ? this->master_got_info_
2153                                           : object->get_got_info());
2154     gold_assert(g != NULL);
2155     return g->tls_ldm_offset();
2156   }
2157 
2158   // Set the offset of TLS LDM entry.  For multi-GOT links, use OBJECT's GOT.
2159   void
set_tls_ldm_offset(unsigned int tls_ldm_offset,Mips_relobj<size,big_endian> * object)2160   set_tls_ldm_offset(unsigned int tls_ldm_offset,
2161                      Mips_relobj<size, big_endian>* object)
2162   {
2163     Mips_got_info<size, big_endian>* g = (!this->multi_got()
2164                                           ? this->master_got_info_
2165                                           : object->get_got_info());
2166     gold_assert(g != NULL);
2167     g->set_tls_ldm_offset(tls_ldm_offset);
2168   }
2169 
2170   // Return true for multi-GOT links.
2171   bool
multi_got() const2172   multi_got() const
2173   { return this->primary_got_ != NULL; }
2174 
2175   // Return the offset of OBJECT's GOT from the start of .got section.
2176   unsigned int
get_got_offset(const Mips_relobj<size,big_endian> * object)2177   get_got_offset(const Mips_relobj<size, big_endian>* object)
2178   {
2179     if (!this->multi_got())
2180       return 0;
2181     else
2182       {
2183         Mips_got_info<size, big_endian>* g = object->get_got_info();
2184         return g != NULL ? g->offset() : 0;
2185       }
2186   }
2187 
2188   // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2189   void
add_reloc_only_entries()2190   add_reloc_only_entries()
2191   { this->master_got_info_->add_reloc_only_entries(this); }
2192 
2193   // Return offset of the primary GOT's entry for global symbol.
2194   unsigned int
get_primary_got_offset(const Mips_symbol<size> * sym) const2195   get_primary_got_offset(const Mips_symbol<size>* sym) const
2196   {
2197     gold_assert(sym->global_got_area() != GGA_NONE);
2198     return (this->get_local_gotno() + sym->dynsym_index()
2199             - this->first_global_got_dynsym_index()) * size/8;
2200   }
2201 
2202   // For the entry at offset GOT_OFFSET, return its offset from the gp.
2203   // Input argument GOT_OFFSET is always global offset from the start of
2204   // .got section, for both single and multi-GOT links.
2205   // For single GOT links, this returns GOT_OFFSET - 0x7FF0.  For multi-GOT
2206   // links, the return value is object_got_offset - 0x7FF0, where
2207   // object_got_offset is offset in the OBJECT's GOT.
2208   int
gp_offset(unsigned int got_offset,const Mips_relobj<size,big_endian> * object) const2209   gp_offset(unsigned int got_offset,
2210             const Mips_relobj<size, big_endian>* object) const
2211   {
2212     return (this->address() + got_offset
2213             - this->target_->adjusted_gp_value(object));
2214   }
2215 
2216  protected:
2217   // Write out the GOT table.
2218   void
2219   do_write(Output_file*);
2220 
2221  private:
2222 
2223   // This class represent dynamic relocations that need to be applied by
2224   // gold because we are using TLS relocations in a static link.
2225   class Static_reloc
2226   {
2227    public:
Static_reloc(unsigned int got_offset,unsigned int r_type,Mips_symbol<size> * gsym)2228     Static_reloc(unsigned int got_offset, unsigned int r_type,
2229                  Mips_symbol<size>* gsym)
2230       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2231     { this->u_.global.symbol = gsym; }
2232 
Static_reloc(unsigned int got_offset,unsigned int r_type,Sized_relobj_file<size,big_endian> * relobj,unsigned int index)2233     Static_reloc(unsigned int got_offset, unsigned int r_type,
2234           Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2235       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2236     {
2237       this->u_.local.relobj = relobj;
2238       this->u_.local.index = index;
2239     }
2240 
2241     // Return the GOT offset.
2242     unsigned int
got_offset() const2243     got_offset() const
2244     { return this->got_offset_; }
2245 
2246     // Relocation type.
2247     unsigned int
r_type() const2248     r_type() const
2249     { return this->r_type_; }
2250 
2251     // Whether the symbol is global or not.
2252     bool
symbol_is_global() const2253     symbol_is_global() const
2254     { return this->symbol_is_global_; }
2255 
2256     // For a relocation against a global symbol, the global symbol.
2257     Mips_symbol<size>*
symbol() const2258     symbol() const
2259     {
2260       gold_assert(this->symbol_is_global_);
2261       return this->u_.global.symbol;
2262     }
2263 
2264     // For a relocation against a local symbol, the defining object.
2265     Sized_relobj_file<size, big_endian>*
relobj() const2266     relobj() const
2267     {
2268       gold_assert(!this->symbol_is_global_);
2269       return this->u_.local.relobj;
2270     }
2271 
2272     // For a relocation against a local symbol, the local symbol index.
2273     unsigned int
index() const2274     index() const
2275     {
2276       gold_assert(!this->symbol_is_global_);
2277       return this->u_.local.index;
2278     }
2279 
2280    private:
2281     // GOT offset of the entry to which this relocation is applied.
2282     unsigned int got_offset_;
2283     // Type of relocation.
2284     unsigned int r_type_;
2285     // Whether this relocation is against a global symbol.
2286     bool symbol_is_global_;
2287     // A global or local symbol.
2288     union
2289     {
2290       struct
2291       {
2292         // For a global symbol, the symbol itself.
2293         Mips_symbol<size>* symbol;
2294       } global;
2295       struct
2296       {
2297         // For a local symbol, the object defining object.
2298         Sized_relobj_file<size, big_endian>* relobj;
2299         // For a local symbol, the symbol index.
2300         unsigned int index;
2301       } local;
2302     } u_;
2303   };
2304 
2305   // The target.
2306   Target_mips<size, big_endian>* target_;
2307   // The symbol table.
2308   Symbol_table* symbol_table_;
2309   // The layout.
2310   Layout* layout_;
2311   // Static relocs to be applied to the GOT.
2312   std::vector<Static_reloc> static_relocs_;
2313   // .got section view.
2314   unsigned char* got_view_;
2315   // The dynamic symbol table index of the first symbol with global GOT entry.
2316   unsigned int first_global_got_dynsym_index_;
2317   // The master GOT information.
2318   Mips_got_info<size, big_endian>* master_got_info_;
2319   // The  primary GOT information.
2320   Mips_got_info<size, big_endian>* primary_got_;
2321   // Secondary GOT fixups.
2322   std::vector<Static_reloc> secondary_got_relocs_;
2323 };
2324 
2325 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2326 // two ways of creating these interfaces.  The first is to add:
2327 //
2328 //      lui     $25,%hi(func)
2329 //      j       func
2330 //      addiu   $25,$25,%lo(func)
2331 //
2332 // to a separate trampoline section.  The second is to add:
2333 //
2334 //      lui     $25,%hi(func)
2335 //      addiu   $25,$25,%lo(func)
2336 //
2337 // immediately before a PIC function "func", but only if a function is at the
2338 // beginning of the section, and the section is not too heavily aligned (i.e we
2339 // would need to add no more than 2 nops before the stub.)
2340 //
2341 // We only create stubs of the first type.
2342 
2343 template<int size, bool big_endian>
2344 class Mips_output_data_la25_stub : public Output_section_data
2345 {
2346   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2347 
2348  public:
Mips_output_data_la25_stub()2349   Mips_output_data_la25_stub()
2350   : Output_section_data(size == 32 ? 4 : 8), symbols_()
2351   { }
2352 
2353   // Create LA25 stub for a symbol.
2354   void
2355   create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2356                    Mips_symbol<size>* gsym);
2357 
2358   // Return output address of a stub.
2359   Mips_address
stub_address(const Mips_symbol<size> * sym) const2360   stub_address(const Mips_symbol<size>* sym) const
2361   {
2362     gold_assert(sym->has_la25_stub());
2363     return this->address() + sym->la25_stub_offset();
2364   }
2365 
2366  protected:
2367   void
do_adjust_output_section(Output_section * os)2368   do_adjust_output_section(Output_section* os)
2369   { os->set_entsize(0); }
2370 
2371  private:
2372   // Template for standard LA25 stub.
2373   static const uint32_t la25_stub_entry[];
2374   // Template for microMIPS LA25 stub.
2375   static const uint32_t la25_stub_micromips_entry[];
2376 
2377   // Set the final size.
2378   void
set_final_data_size()2379   set_final_data_size()
2380   { this->set_data_size(this->symbols_.size() * 16); }
2381 
2382   // Create a symbol for SYM stub's value and size, to help make the
2383   // disassembly easier to read.
2384   void
2385   create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2386                      Target_mips<size, big_endian>* target, uint64_t symsize);
2387 
2388   // Write to a map file.
2389   void
do_print_to_mapfile(Mapfile * mapfile) const2390   do_print_to_mapfile(Mapfile* mapfile) const
2391   { mapfile->print_output_data(this, _(".LA25.stubs")); }
2392 
2393   // Write out the LA25 stub section.
2394   void
2395   do_write(Output_file*);
2396 
2397   // Symbols that have LA25 stubs.
2398   std::vector<Mips_symbol<size>*> symbols_;
2399 };
2400 
2401 // MIPS-specific relocation writer.
2402 
2403 template<int sh_type, bool dynamic, int size, bool big_endian>
2404 struct Mips_output_reloc_writer;
2405 
2406 template<int sh_type, bool dynamic, bool big_endian>
2407 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2408 {
2409   typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2410   typedef std::vector<Output_reloc_type> Relocs;
2411 
2412   static void
write__anon5561842d0111::Mips_output_reloc_writer2413   write(typename Relocs::const_iterator p, unsigned char* pov)
2414   { p->write(pov); }
2415 };
2416 
2417 template<int sh_type, bool dynamic, bool big_endian>
2418 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2419 {
2420   typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2421   typedef std::vector<Output_reloc_type> Relocs;
2422 
2423   static void
write__anon5561842d0111::Mips_output_reloc_writer2424   write(typename Relocs::const_iterator p, unsigned char* pov)
2425   {
2426     elfcpp::Mips64_rel_write<big_endian> orel(pov);
2427     orel.put_r_offset(p->get_address());
2428     orel.put_r_sym(p->get_symbol_index());
2429     orel.put_r_ssym(RSS_UNDEF);
2430     orel.put_r_type(p->type());
2431     if (p->type() == elfcpp::R_MIPS_REL32)
2432       orel.put_r_type2(elfcpp::R_MIPS_64);
2433     else
2434       orel.put_r_type2(elfcpp::R_MIPS_NONE);
2435     orel.put_r_type3(elfcpp::R_MIPS_NONE);
2436   }
2437 };
2438 
2439 template<int sh_type, bool dynamic, int size, bool big_endian>
2440 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2441                                                         size, big_endian>
2442 {
2443  public:
Mips_output_data_reloc(bool sort_relocs)2444   Mips_output_data_reloc(bool sort_relocs)
2445     : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2446   { }
2447 
2448  protected:
2449   // Write out the data.
2450   void
do_write(Output_file * of)2451   do_write(Output_file* of)
2452   {
2453     typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2454         big_endian> Writer;
2455     this->template do_write_generic<Writer>(of);
2456   }
2457 };
2458 
2459 
2460 // A class to handle the PLT data.
2461 
2462 template<int size, bool big_endian>
2463 class Mips_output_data_plt : public Output_section_data
2464 {
2465   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2466   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2467                                  size, big_endian> Reloc_section;
2468 
2469  public:
2470   // Create the PLT section.  The ordinary .got section is an argument,
2471   // since we need to refer to the start.
Mips_output_data_plt(Layout * layout,Output_data_space * got_plt,Target_mips<size,big_endian> * target)2472   Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2473                        Target_mips<size, big_endian>* target)
2474     : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2475       plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2476       target_(target)
2477   {
2478     this->rel_ = new Reloc_section(false);
2479     layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2480                                     elfcpp::SHF_ALLOC, this->rel_,
2481                                     ORDER_DYNAMIC_PLT_RELOCS, false);
2482   }
2483 
2484   // Add an entry to the PLT for a symbol referenced by r_type relocation.
2485   void
2486   add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2487 
2488   // Return the .rel.plt section data.
2489   Reloc_section*
rel_plt() const2490   rel_plt() const
2491   { return this->rel_; }
2492 
2493   // Return the number of PLT entries.
2494   unsigned int
entry_count() const2495   entry_count() const
2496   { return this->symbols_.size(); }
2497 
2498   // Return the offset of the first non-reserved PLT entry.
2499   unsigned int
first_plt_entry_offset() const2500   first_plt_entry_offset() const
2501   { return sizeof(plt0_entry_o32); }
2502 
2503   // Return the size of a PLT entry.
2504   unsigned int
plt_entry_size() const2505   plt_entry_size() const
2506   { return sizeof(plt_entry); }
2507 
2508   // Set final PLT offsets.  For each symbol, determine whether standard or
2509   // compressed (MIPS16 or microMIPS) PLT entry is used.
2510   void
2511   set_plt_offsets();
2512 
2513   // Return the offset of the first standard PLT entry.
2514   unsigned int
first_mips_plt_offset() const2515   first_mips_plt_offset() const
2516   { return this->plt_header_size_; }
2517 
2518   // Return the offset of the first compressed PLT entry.
2519   unsigned int
first_comp_plt_offset() const2520   first_comp_plt_offset() const
2521   { return this->plt_header_size_ + this->plt_mips_offset_; }
2522 
2523   // Return whether there are any standard PLT entries.
2524   bool
has_standard_entries() const2525   has_standard_entries() const
2526   { return this->plt_mips_offset_ > 0; }
2527 
2528   // Return the output address of standard PLT entry.
2529   Mips_address
mips_entry_address(const Mips_symbol<size> * sym) const2530   mips_entry_address(const Mips_symbol<size>* sym) const
2531   {
2532     gold_assert (sym->has_mips_plt_offset());
2533     return (this->address() + this->first_mips_plt_offset()
2534             + sym->mips_plt_offset());
2535   }
2536 
2537   // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2538   Mips_address
comp_entry_address(const Mips_symbol<size> * sym) const2539   comp_entry_address(const Mips_symbol<size>* sym) const
2540   {
2541     gold_assert (sym->has_comp_plt_offset());
2542     return (this->address() + this->first_comp_plt_offset()
2543             + sym->comp_plt_offset());
2544   }
2545 
2546  protected:
2547   void
do_adjust_output_section(Output_section * os)2548   do_adjust_output_section(Output_section* os)
2549   { os->set_entsize(0); }
2550 
2551   // Write to a map file.
2552   void
do_print_to_mapfile(Mapfile * mapfile) const2553   do_print_to_mapfile(Mapfile* mapfile) const
2554   { mapfile->print_output_data(this, _(".plt")); }
2555 
2556  private:
2557   // Template for the first PLT entry.
2558   static const uint32_t plt0_entry_o32[];
2559   static const uint32_t plt0_entry_n32[];
2560   static const uint32_t plt0_entry_n64[];
2561   static const uint32_t plt0_entry_micromips_o32[];
2562   static const uint32_t plt0_entry_micromips32_o32[];
2563 
2564   // Template for subsequent PLT entries.
2565   static const uint32_t plt_entry[];
2566   static const uint32_t plt_entry_r6[];
2567   static const uint32_t plt_entry_mips16_o32[];
2568   static const uint32_t plt_entry_micromips_o32[];
2569   static const uint32_t plt_entry_micromips32_o32[];
2570 
2571   // Set the final size.
2572   void
set_final_data_size()2573   set_final_data_size()
2574   {
2575     this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2576                         + this->plt_comp_offset_);
2577   }
2578 
2579   // Write out the PLT data.
2580   void
2581   do_write(Output_file*);
2582 
2583   // Return whether the plt header contains microMIPS code.  For the sake of
2584   // cache alignment always use a standard header whenever any standard entries
2585   // are present even if microMIPS entries are present as well.  This also lets
2586   // the microMIPS header rely on the value of $v0 only set by microMIPS
2587   // entries, for a small size reduction.
2588   bool
is_plt_header_compressed() const2589   is_plt_header_compressed() const
2590   {
2591     gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2592     return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2593   }
2594 
2595   // Return the size of the PLT header.
2596   unsigned int
get_plt_header_size() const2597   get_plt_header_size() const
2598   {
2599     if (this->target_->is_output_n64())
2600       return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2601     else if (this->target_->is_output_n32())
2602       return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2603     else if (!this->is_plt_header_compressed())
2604       return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2605     else if (this->target_->use_32bit_micromips_instructions())
2606       return (2 * sizeof(plt0_entry_micromips32_o32)
2607               / sizeof(plt0_entry_micromips32_o32[0]));
2608     else
2609       return (2 * sizeof(plt0_entry_micromips_o32)
2610               / sizeof(plt0_entry_micromips_o32[0]));
2611   }
2612 
2613   // Return the PLT header entry.
2614   const uint32_t*
get_plt_header_entry() const2615   get_plt_header_entry() const
2616   {
2617     if (this->target_->is_output_n64())
2618       return plt0_entry_n64;
2619     else if (this->target_->is_output_n32())
2620       return plt0_entry_n32;
2621     else if (!this->is_plt_header_compressed())
2622       return plt0_entry_o32;
2623     else if (this->target_->use_32bit_micromips_instructions())
2624       return plt0_entry_micromips32_o32;
2625     else
2626       return plt0_entry_micromips_o32;
2627   }
2628 
2629   // Return the size of the standard PLT entry.
2630   unsigned int
standard_plt_entry_size() const2631   standard_plt_entry_size() const
2632   { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2633 
2634   // Return the size of the compressed PLT entry.
2635   unsigned int
compressed_plt_entry_size() const2636   compressed_plt_entry_size() const
2637   {
2638     gold_assert(!this->target_->is_output_newabi());
2639 
2640     if (!this->target_->is_output_micromips())
2641       return (2 * sizeof(plt_entry_mips16_o32)
2642               / sizeof(plt_entry_mips16_o32[0]));
2643     else if (this->target_->use_32bit_micromips_instructions())
2644       return (2 * sizeof(plt_entry_micromips32_o32)
2645               / sizeof(plt_entry_micromips32_o32[0]));
2646     else
2647       return (2 * sizeof(plt_entry_micromips_o32)
2648               / sizeof(plt_entry_micromips_o32[0]));
2649   }
2650 
2651   // The reloc section.
2652   Reloc_section* rel_;
2653   // The .got.plt section.
2654   Output_data_space* got_plt_;
2655   // Symbols that have PLT entry.
2656   std::vector<Mips_symbol<size>*> symbols_;
2657   // The offset of the next standard PLT entry to create.
2658   unsigned int plt_mips_offset_;
2659   // The offset of the next compressed PLT entry to create.
2660   unsigned int plt_comp_offset_;
2661   // The size of the PLT header in bytes.
2662   unsigned int plt_header_size_;
2663   // The target.
2664   Target_mips<size, big_endian>* target_;
2665 };
2666 
2667 // A class to handle the .MIPS.stubs data.
2668 
2669 template<int size, bool big_endian>
2670 class Mips_output_data_mips_stubs : public Output_section_data
2671 {
2672   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2673 
2674   // Unordered set of .MIPS.stubs entries.
2675   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2676       Mips_stubs_entry_set;
2677 
2678  public:
Mips_output_data_mips_stubs(Target_mips<size,big_endian> * target)2679    Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2680      : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2681        stub_offsets_are_set_(false), target_(target)
2682    { }
2683 
2684   // Create entry for a symbol.
2685   void
2686   make_entry(Mips_symbol<size>*);
2687 
2688   // Remove entry for a symbol.
2689   void
2690   remove_entry(Mips_symbol<size>* gsym);
2691 
2692   // Set stub offsets for symbols.  This method expects that the number of
2693   // entries in dynamic symbol table is set.
2694   void
2695   set_lazy_stub_offsets();
2696 
2697   void
2698   set_needs_dynsym_value();
2699 
2700    // Set the number of entries in dynamic symbol table.
2701   void
set_dynsym_count(unsigned int dynsym_count)2702   set_dynsym_count(unsigned int dynsym_count)
2703   { this->dynsym_count_ = dynsym_count; }
2704 
2705   // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2706   // count is greater than 0x10000.  If the dynamic symbol count is less than
2707   // 0x10000, the stub will be 4 bytes smaller.
2708   // There's no disadvantage from using microMIPS code here, so for the sake of
2709   // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2710   // output produced at all.  This has a benefit of stubs being shorter by
2711   // 4 bytes each too, unless in the insn32 mode.
2712   unsigned int
stub_max_size() const2713   stub_max_size() const
2714   {
2715     if (!this->target_->is_output_micromips()
2716         || this->target_->use_32bit_micromips_instructions())
2717       return 20;
2718     else
2719       return 16;
2720   }
2721 
2722   // Return the size of the stub.  This method expects that the final dynsym
2723   // count is set.
2724   unsigned int
stub_size() const2725   stub_size() const
2726   {
2727     gold_assert(this->dynsym_count_ != -1U);
2728     if (this->dynsym_count_ > 0x10000)
2729       return this->stub_max_size();
2730     else
2731       return this->stub_max_size() - 4;
2732   }
2733 
2734   // Return output address of a stub.
2735   Mips_address
stub_address(const Mips_symbol<size> * sym) const2736   stub_address(const Mips_symbol<size>* sym) const
2737   {
2738     gold_assert(sym->has_lazy_stub());
2739     return this->address() + sym->lazy_stub_offset();
2740   }
2741 
2742  protected:
2743   void
do_adjust_output_section(Output_section * os)2744   do_adjust_output_section(Output_section* os)
2745   { os->set_entsize(0); }
2746 
2747   // Write to a map file.
2748   void
do_print_to_mapfile(Mapfile * mapfile) const2749   do_print_to_mapfile(Mapfile* mapfile) const
2750   { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2751 
2752  private:
2753   static const uint32_t lazy_stub_normal_1[];
2754   static const uint32_t lazy_stub_normal_1_n64[];
2755   static const uint32_t lazy_stub_normal_2[];
2756   static const uint32_t lazy_stub_normal_2_n64[];
2757   static const uint32_t lazy_stub_big[];
2758   static const uint32_t lazy_stub_big_n64[];
2759 
2760   static const uint32_t lazy_stub_micromips_normal_1[];
2761   static const uint32_t lazy_stub_micromips_normal_1_n64[];
2762   static const uint32_t lazy_stub_micromips_normal_2[];
2763   static const uint32_t lazy_stub_micromips_normal_2_n64[];
2764   static const uint32_t lazy_stub_micromips_big[];
2765   static const uint32_t lazy_stub_micromips_big_n64[];
2766 
2767   static const uint32_t lazy_stub_micromips32_normal_1[];
2768   static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2769   static const uint32_t lazy_stub_micromips32_normal_2[];
2770   static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2771   static const uint32_t lazy_stub_micromips32_big[];
2772   static const uint32_t lazy_stub_micromips32_big_n64[];
2773 
2774   // Set the final size.
2775   void
set_final_data_size()2776   set_final_data_size()
2777   { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2778 
2779   // Write out the .MIPS.stubs data.
2780   void
2781   do_write(Output_file*);
2782 
2783   // .MIPS.stubs symbols
2784   Mips_stubs_entry_set symbols_;
2785   // Number of entries in dynamic symbol table.
2786   unsigned int dynsym_count_;
2787   // Whether the stub offsets are set.
2788   bool stub_offsets_are_set_;
2789   // The target.
2790   Target_mips<size, big_endian>* target_;
2791 };
2792 
2793 // This class handles Mips .reginfo output section.
2794 
2795 template<int size, bool big_endian>
2796 class Mips_output_section_reginfo : public Output_section_data
2797 {
2798   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2799 
2800  public:
Mips_output_section_reginfo(Target_mips<size,big_endian> * target,Valtype gprmask,Valtype cprmask1,Valtype cprmask2,Valtype cprmask3,Valtype cprmask4)2801   Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2802                               Valtype gprmask, Valtype cprmask1,
2803                               Valtype cprmask2, Valtype cprmask3,
2804                               Valtype cprmask4)
2805     : Output_section_data(24, 4, true), target_(target),
2806       gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2807       cprmask3_(cprmask3), cprmask4_(cprmask4)
2808   { }
2809 
2810  protected:
2811   // Write to a map file.
2812   void
do_print_to_mapfile(Mapfile * mapfile) const2813   do_print_to_mapfile(Mapfile* mapfile) const
2814   { mapfile->print_output_data(this, _(".reginfo")); }
2815 
2816   // Write out reginfo section.
2817   void
2818   do_write(Output_file* of);
2819 
2820  private:
2821   Target_mips<size, big_endian>* target_;
2822 
2823   // gprmask of the output .reginfo section.
2824   Valtype gprmask_;
2825   // cprmask1 of the output .reginfo section.
2826   Valtype cprmask1_;
2827   // cprmask2 of the output .reginfo section.
2828   Valtype cprmask2_;
2829   // cprmask3 of the output .reginfo section.
2830   Valtype cprmask3_;
2831   // cprmask4 of the output .reginfo section.
2832   Valtype cprmask4_;
2833 };
2834 
2835 // This class handles .MIPS.options output section.
2836 
2837 template<int size, bool big_endian>
2838 class Mips_output_section_options : public Output_section
2839 {
2840  public:
Mips_output_section_options(const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags,Target_mips<size,big_endian> * target)2841   Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
2842                               elfcpp::Elf_Xword flags,
2843                               Target_mips<size, big_endian>* target)
2844     : Output_section(name, type, flags), target_(target)
2845   {
2846     // After the input sections are written, we only need to update
2847     // ri_gp_value field of ODK_REGINFO entries.
2848     this->set_after_input_sections();
2849   }
2850 
2851  protected:
2852   // Write out option section.
2853   void
2854   do_write(Output_file* of);
2855 
2856  private:
2857   Target_mips<size, big_endian>* target_;
2858 };
2859 
2860 // This class handles .MIPS.abiflags output section.
2861 
2862 template<int size, bool big_endian>
2863 class Mips_output_section_abiflags : public Output_section_data
2864 {
2865  public:
Mips_output_section_abiflags(const Mips_abiflags<big_endian> & abiflags)2866   Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2867     : Output_section_data(24, 8, true), abiflags_(abiflags)
2868   { }
2869 
2870  protected:
2871   // Write to a map file.
2872   void
do_print_to_mapfile(Mapfile * mapfile) const2873   do_print_to_mapfile(Mapfile* mapfile) const
2874   { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2875 
2876   void
2877   do_write(Output_file* of);
2878 
2879  private:
2880   const Mips_abiflags<big_endian>& abiflags_;
2881 };
2882 
2883 // The MIPS target has relocation types which default handling of relocatable
2884 // relocation cannot process.  So we have to extend the default code.
2885 
2886 template<bool big_endian, typename Classify_reloc>
2887 class Mips_scan_relocatable_relocs :
2888   public Default_scan_relocatable_relocs<Classify_reloc>
2889 {
2890  public:
2891   // Return the strategy to use for a local symbol which is a section
2892   // symbol, given the relocation type.
2893   inline Relocatable_relocs::Reloc_strategy
local_section_strategy(unsigned int r_type,Relobj * object)2894   local_section_strategy(unsigned int r_type, Relobj* object)
2895   {
2896     if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2897       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2898     else
2899       {
2900         switch (r_type)
2901           {
2902           case elfcpp::R_MIPS_26:
2903             return Relocatable_relocs::RELOC_SPECIAL;
2904 
2905           default:
2906             return Default_scan_relocatable_relocs<Classify_reloc>::
2907                 local_section_strategy(r_type, object);
2908           }
2909       }
2910   }
2911 };
2912 
2913 // Mips_copy_relocs class.  The only difference from the base class is the
2914 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2915 // Mips cannot convert all relocation types to dynamic relocs.  If a reloc
2916 // cannot be made dynamic, a COPY reloc is emitted.
2917 
2918 template<int sh_type, int size, bool big_endian>
2919 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2920 {
2921  public:
Mips_copy_relocs()2922   Mips_copy_relocs()
2923     : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2924   { }
2925 
2926   // Emit any saved relocations which turn out to be needed.  This is
2927   // called after all the relocs have been scanned.
2928   void
2929   emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2930             Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2931 
2932  private:
2933   typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2934     Copy_reloc_entry;
2935 
2936   // Emit this reloc if appropriate.  This is called after we have
2937   // scanned all the relocations, so we know whether we emitted a
2938   // COPY relocation for SYM_.
2939   void
2940   emit_entry(Copy_reloc_entry& entry,
2941              Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2942              Symbol_table* symtab, Layout* layout,
2943              Target_mips<size, big_endian>* target);
2944 };
2945 
2946 
2947 // Return true if the symbol SYM should be considered to resolve local
2948 // to the current module, and false otherwise.  The logic is taken from
2949 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2950 static bool
symbol_refs_local(const Symbol * sym,bool has_dynsym_entry,bool local_protected)2951 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2952                   bool local_protected)
2953 {
2954   // If it's a local sym, of course we resolve locally.
2955   if (sym == NULL)
2956     return true;
2957 
2958   // STV_HIDDEN or STV_INTERNAL ones must be local.
2959   if (sym->visibility() == elfcpp::STV_HIDDEN
2960       || sym->visibility() == elfcpp::STV_INTERNAL)
2961     return true;
2962 
2963   // If we don't have a definition in a regular file, then we can't
2964   // resolve locally.  The sym is either undefined or dynamic.
2965   if (sym->is_from_dynobj() || sym->is_undefined())
2966     return false;
2967 
2968   // Forced local symbols resolve locally.
2969   if (sym->is_forced_local())
2970     return true;
2971 
2972   // As do non-dynamic symbols.
2973   if (!has_dynsym_entry)
2974     return true;
2975 
2976   // At this point, we know the symbol is defined and dynamic.  In an
2977   // executable it must resolve locally, likewise when building symbolic
2978   // shared libraries.
2979   if (parameters->options().output_is_executable()
2980       || parameters->options().Bsymbolic())
2981     return true;
2982 
2983   // Now deal with defined dynamic symbols in shared libraries.  Ones
2984   // with default visibility might not resolve locally.
2985   if (sym->visibility() == elfcpp::STV_DEFAULT)
2986     return false;
2987 
2988   // STV_PROTECTED non-function symbols are local.
2989   if (sym->type() != elfcpp::STT_FUNC)
2990     return true;
2991 
2992   // Function pointer equality tests may require that STV_PROTECTED
2993   // symbols be treated as dynamic symbols.  If the address of a
2994   // function not defined in an executable is set to that function's
2995   // plt entry in the executable, then the address of the function in
2996   // a shared library must also be the plt entry in the executable.
2997   return local_protected;
2998 }
2999 
3000 // Return TRUE if references to this symbol always reference the symbol in this
3001 // object.
3002 static bool
symbol_references_local(const Symbol * sym,bool has_dynsym_entry)3003 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3004 {
3005   return symbol_refs_local(sym, has_dynsym_entry, false);
3006 }
3007 
3008 // Return TRUE if calls to this symbol always call the version in this object.
3009 static bool
symbol_calls_local(const Symbol * sym,bool has_dynsym_entry)3010 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3011 {
3012   return symbol_refs_local(sym, has_dynsym_entry, true);
3013 }
3014 
3015 // Compare GOT offsets of two symbols.
3016 
3017 template<int size, bool big_endian>
3018 static bool
got_offset_compare(Symbol * sym1,Symbol * sym2)3019 got_offset_compare(Symbol* sym1, Symbol* sym2)
3020 {
3021   Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
3022   Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
3023   unsigned int area1 = mips_sym1->global_got_area();
3024   unsigned int area2 = mips_sym2->global_got_area();
3025   gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
3026 
3027   // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3028   if (area1 != area2)
3029     return area1 < area2;
3030 
3031   return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
3032 }
3033 
3034 // This method divides dynamic symbols into symbols that have GOT entry, and
3035 // symbols that don't have GOT entry.  It also sorts symbols with the GOT entry.
3036 // Mips ABI requires that symbols with the GOT entry must be at the end of
3037 // dynamic symbol table, and the order in dynamic symbol table must match the
3038 // order in GOT.
3039 
3040 template<int size, bool big_endian>
3041 static void
reorder_dyn_symbols(std::vector<Symbol * > * dyn_symbols,std::vector<Symbol * > * non_got_symbols,std::vector<Symbol * > * got_symbols)3042 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3043                     std::vector<Symbol*>* non_got_symbols,
3044                     std::vector<Symbol*>* got_symbols)
3045 {
3046   for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3047        p != dyn_symbols->end();
3048        ++p)
3049     {
3050       Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3051       if (mips_sym->global_got_area() == GGA_NORMAL
3052           || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3053         got_symbols->push_back(mips_sym);
3054       else
3055         non_got_symbols->push_back(mips_sym);
3056     }
3057 
3058   std::sort(got_symbols->begin(), got_symbols->end(),
3059             got_offset_compare<size, big_endian>);
3060 }
3061 
3062 // Functor class for processing the global symbol table.
3063 
3064 template<int size, bool big_endian>
3065 class Symbol_visitor_check_symbols
3066 {
3067  public:
Symbol_visitor_check_symbols(Target_mips<size,big_endian> * target,Layout * layout,Symbol_table * symtab)3068   Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3069     Layout* layout, Symbol_table* symtab)
3070     : target_(target), layout_(layout), symtab_(symtab)
3071   { }
3072 
3073   void
operator ()(Sized_symbol<size> * sym)3074   operator()(Sized_symbol<size>* sym)
3075   {
3076     Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3077     if (local_pic_function<size, big_endian>(mips_sym))
3078       {
3079         // SYM is a function that might need $25 to be valid on entry.
3080         // If we're creating a non-PIC relocatable object, mark SYM as
3081         // being PIC.  If we're creating a non-relocatable object with
3082         // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3083         // stub.
3084         if (parameters->options().relocatable())
3085           {
3086             if (!parameters->options().output_is_position_independent())
3087               mips_sym->set_pic();
3088           }
3089         else if (mips_sym->has_nonpic_branches())
3090           {
3091             this->target_->la25_stub_section(layout_)
3092                 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3093           }
3094       }
3095   }
3096 
3097  private:
3098   Target_mips<size, big_endian>* target_;
3099   Layout* layout_;
3100   Symbol_table* symtab_;
3101 };
3102 
3103 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3104 // and endianness. The relocation format for MIPS-64 is non-standard.
3105 
3106 template<int sh_type, int size, bool big_endian>
3107 struct Mips_reloc_types;
3108 
3109 template<bool big_endian>
3110 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3111 {
3112   typedef typename elfcpp::Rel<32, big_endian> Reloc;
3113   typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3114 
3115   static typename elfcpp::Elf_types<32>::Elf_Swxword
get_r_addend__anon5561842d0111::Mips_reloc_types3116   get_r_addend(const Reloc*)
3117   { return 0; }
3118 
3119   static inline void
set_reloc_addend__anon5561842d0111::Mips_reloc_types3120   set_reloc_addend(Reloc_write*,
3121 		   typename elfcpp::Elf_types<32>::Elf_Swxword)
3122   { gold_unreachable(); }
3123 };
3124 
3125 template<bool big_endian>
3126 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3127 {
3128   typedef typename elfcpp::Rela<32, big_endian> Reloc;
3129   typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3130 
3131   static typename elfcpp::Elf_types<32>::Elf_Swxword
get_r_addend__anon5561842d0111::Mips_reloc_types3132   get_r_addend(const Reloc* reloc)
3133   { return reloc->get_r_addend(); }
3134 
3135   static inline void
set_reloc_addend__anon5561842d0111::Mips_reloc_types3136   set_reloc_addend(Reloc_write* p,
3137 		   typename elfcpp::Elf_types<32>::Elf_Swxword val)
3138   { p->put_r_addend(val); }
3139 };
3140 
3141 template<bool big_endian>
3142 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3143 {
3144   typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3145   typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3146 
3147   static typename elfcpp::Elf_types<64>::Elf_Swxword
get_r_addend__anon5561842d0111::Mips_reloc_types3148   get_r_addend(const Reloc*)
3149   { return 0; }
3150 
3151   static inline void
set_reloc_addend__anon5561842d0111::Mips_reloc_types3152   set_reloc_addend(Reloc_write*,
3153 		   typename elfcpp::Elf_types<64>::Elf_Swxword)
3154   { gold_unreachable(); }
3155 };
3156 
3157 template<bool big_endian>
3158 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3159 {
3160   typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3161   typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3162 
3163   static typename elfcpp::Elf_types<64>::Elf_Swxword
get_r_addend__anon5561842d0111::Mips_reloc_types3164   get_r_addend(const Reloc* reloc)
3165   { return reloc->get_r_addend(); }
3166 
3167   static inline void
set_reloc_addend__anon5561842d0111::Mips_reloc_types3168   set_reloc_addend(Reloc_write* p,
3169 		   typename elfcpp::Elf_types<64>::Elf_Swxword val)
3170   { p->put_r_addend(val); }
3171 };
3172 
3173 // Forward declaration.
3174 static unsigned int
3175 mips_get_size_for_reloc(unsigned int, Relobj*);
3176 
3177 // A class for inquiring about properties of a relocation,
3178 // used while scanning relocs during a relocatable link and
3179 // garbage collection.
3180 
3181 template<int sh_type_, int size, bool big_endian>
3182 class Mips_classify_reloc;
3183 
3184 template<int sh_type_, bool big_endian>
3185 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3186     public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3187 {
3188  public:
3189   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3190       Reltype;
3191   typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3192       Reltype_write;
3193 
3194   // Return the symbol referred to by the relocation.
3195   static inline unsigned int
get_r_sym(const Reltype * reloc)3196   get_r_sym(const Reltype* reloc)
3197   { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3198 
3199   // Return the type of the relocation.
3200   static inline unsigned int
get_r_type(const Reltype * reloc)3201   get_r_type(const Reltype* reloc)
3202   { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3203 
3204   static inline unsigned int
get_r_type2(const Reltype *)3205   get_r_type2(const Reltype*)
3206   { return 0; }
3207 
3208   static inline unsigned int
get_r_type3(const Reltype *)3209   get_r_type3(const Reltype*)
3210   { return 0; }
3211 
3212   static inline unsigned int
get_r_ssym(const Reltype *)3213   get_r_ssym(const Reltype*)
3214   { return 0; }
3215 
3216   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3217   static inline unsigned int
get_r_addend(const Reltype * reloc)3218   get_r_addend(const Reltype* reloc)
3219   {
3220     if (sh_type_ == elfcpp::SHT_REL)
3221       return 0;
3222     return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3223   }
3224 
3225   // Write the r_info field to a new reloc, using the r_info field from
3226   // the original reloc, replacing the r_sym field with R_SYM.
3227   static inline void
put_r_info(Reltype_write * new_reloc,Reltype * reloc,unsigned int r_sym)3228   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3229   {
3230     unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3231     new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3232   }
3233 
3234   // Write the r_addend field to a new reloc.
3235   static inline void
put_r_addend(Reltype_write * to,typename elfcpp::Elf_types<32>::Elf_Swxword addend)3236   put_r_addend(Reltype_write* to,
3237 	       typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3238   { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3239 
3240   // Return the size of the addend of the relocation (only used for SHT_REL).
3241   static unsigned int
get_size_for_reloc(unsigned int r_type,Relobj * obj)3242   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3243   { return mips_get_size_for_reloc(r_type, obj); }
3244 };
3245 
3246 template<int sh_type_, bool big_endian>
3247 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3248     public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3249 {
3250  public:
3251   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3252       Reltype;
3253   typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3254       Reltype_write;
3255 
3256   // Return the symbol referred to by the relocation.
3257   static inline unsigned int
get_r_sym(const Reltype * reloc)3258   get_r_sym(const Reltype* reloc)
3259   { return reloc->get_r_sym(); }
3260 
3261   // Return the r_type of the relocation.
3262   static inline unsigned int
get_r_type(const Reltype * reloc)3263   get_r_type(const Reltype* reloc)
3264   { return reloc->get_r_type(); }
3265 
3266   // Return the r_type2 of the relocation.
3267   static inline unsigned int
get_r_type2(const Reltype * reloc)3268   get_r_type2(const Reltype* reloc)
3269   { return reloc->get_r_type2(); }
3270 
3271   // Return the r_type3 of the relocation.
3272   static inline unsigned int
get_r_type3(const Reltype * reloc)3273   get_r_type3(const Reltype* reloc)
3274   { return reloc->get_r_type3(); }
3275 
3276   // Return the special symbol of the relocation.
3277   static inline unsigned int
get_r_ssym(const Reltype * reloc)3278   get_r_ssym(const Reltype* reloc)
3279   { return reloc->get_r_ssym(); }
3280 
3281   // Return the explicit addend of the relocation (return 0 for SHT_REL).
3282   static inline typename elfcpp::Elf_types<64>::Elf_Swxword
get_r_addend(const Reltype * reloc)3283   get_r_addend(const Reltype* reloc)
3284   {
3285     if (sh_type_ == elfcpp::SHT_REL)
3286       return 0;
3287     return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3288   }
3289 
3290   // Write the r_info field to a new reloc, using the r_info field from
3291   // the original reloc, replacing the r_sym field with R_SYM.
3292   static inline void
put_r_info(Reltype_write * new_reloc,Reltype * reloc,unsigned int r_sym)3293   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3294   {
3295     new_reloc->put_r_sym(r_sym);
3296     new_reloc->put_r_ssym(reloc->get_r_ssym());
3297     new_reloc->put_r_type3(reloc->get_r_type3());
3298     new_reloc->put_r_type2(reloc->get_r_type2());
3299     new_reloc->put_r_type(reloc->get_r_type());
3300   }
3301 
3302   // Write the r_addend field to a new reloc.
3303   static inline void
put_r_addend(Reltype_write * to,typename elfcpp::Elf_types<64>::Elf_Swxword addend)3304   put_r_addend(Reltype_write* to,
3305 	       typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3306   { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3307 
3308   // Return the size of the addend of the relocation (only used for SHT_REL).
3309   static unsigned int
get_size_for_reloc(unsigned int r_type,Relobj * obj)3310   get_size_for_reloc(unsigned int r_type, Relobj* obj)
3311   { return mips_get_size_for_reloc(r_type, obj); }
3312 };
3313 
3314 template<int size, bool big_endian>
3315 class Target_mips : public Sized_target<size, big_endian>
3316 {
3317   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3318   typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3319     Reloc_section;
3320   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3321   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3322   typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3323       Reltype;
3324   typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3325       Relatype;
3326 
3327  public:
Target_mips(const Target::Target_info * info=& mips_info)3328   Target_mips(const Target::Target_info* info = &mips_info)
3329     : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3330       got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3331       dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3332       mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3333       mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
3334       entry_symbol_is_compressed_(false), insn32_(false)
3335   {
3336     this->add_machine_extensions();
3337   }
3338 
3339   // The offset of $gp from the beginning of the .got section.
3340   static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3341 
3342   // The maximum size of the GOT for it to be addressable using 16-bit
3343   // offsets from $gp.
3344   static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3345 
3346   // Make a new symbol table entry for the Mips target.
3347   Sized_symbol<size>*
make_symbol(const char *,elfcpp::STT,Object *,unsigned int,uint64_t)3348   make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3349   { return new Mips_symbol<size>(); }
3350 
3351   // Process the relocations to determine unreferenced sections for
3352   // garbage collection.
3353   void
3354   gc_process_relocs(Symbol_table* symtab,
3355                     Layout* layout,
3356                     Sized_relobj_file<size, big_endian>* object,
3357                     unsigned int data_shndx,
3358                     unsigned int sh_type,
3359                     const unsigned char* prelocs,
3360                     size_t reloc_count,
3361                     Output_section* output_section,
3362                     bool needs_special_offset_handling,
3363                     size_t local_symbol_count,
3364                     const unsigned char* plocal_symbols);
3365 
3366   // Scan the relocations to look for symbol adjustments.
3367   void
3368   scan_relocs(Symbol_table* symtab,
3369               Layout* layout,
3370               Sized_relobj_file<size, big_endian>* object,
3371               unsigned int data_shndx,
3372               unsigned int sh_type,
3373               const unsigned char* prelocs,
3374               size_t reloc_count,
3375               Output_section* output_section,
3376               bool needs_special_offset_handling,
3377               size_t local_symbol_count,
3378               const unsigned char* plocal_symbols);
3379 
3380   // Finalize the sections.
3381   void
3382   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3383 
3384   // Relocate a section.
3385   void
3386   relocate_section(const Relocate_info<size, big_endian>*,
3387                    unsigned int sh_type,
3388                    const unsigned char* prelocs,
3389                    size_t reloc_count,
3390                    Output_section* output_section,
3391                    bool needs_special_offset_handling,
3392                    unsigned char* view,
3393                    Mips_address view_address,
3394                    section_size_type view_size,
3395                    const Reloc_symbol_changes*);
3396 
3397   // Scan the relocs during a relocatable link.
3398   void
3399   scan_relocatable_relocs(Symbol_table* symtab,
3400                           Layout* layout,
3401                           Sized_relobj_file<size, big_endian>* object,
3402                           unsigned int data_shndx,
3403                           unsigned int sh_type,
3404                           const unsigned char* prelocs,
3405                           size_t reloc_count,
3406                           Output_section* output_section,
3407                           bool needs_special_offset_handling,
3408                           size_t local_symbol_count,
3409                           const unsigned char* plocal_symbols,
3410                           Relocatable_relocs*);
3411 
3412   // Scan the relocs for --emit-relocs.
3413   void
3414   emit_relocs_scan(Symbol_table* symtab,
3415 		   Layout* layout,
3416 		   Sized_relobj_file<size, big_endian>* object,
3417 		   unsigned int data_shndx,
3418 		   unsigned int sh_type,
3419 		   const unsigned char* prelocs,
3420 		   size_t reloc_count,
3421 		   Output_section* output_section,
3422 		   bool needs_special_offset_handling,
3423 		   size_t local_symbol_count,
3424 		   const unsigned char* plocal_syms,
3425 		   Relocatable_relocs* rr);
3426 
3427   // Emit relocations for a section.
3428   void
3429   relocate_relocs(const Relocate_info<size, big_endian>*,
3430                   unsigned int sh_type,
3431                   const unsigned char* prelocs,
3432                   size_t reloc_count,
3433                   Output_section* output_section,
3434                   typename elfcpp::Elf_types<size>::Elf_Off
3435                     offset_in_output_section,
3436                   unsigned char* view,
3437                   Mips_address view_address,
3438                   section_size_type view_size,
3439                   unsigned char* reloc_view,
3440                   section_size_type reloc_view_size);
3441 
3442   // Perform target-specific processing in a relocatable link.  This is
3443   // only used if we use the relocation strategy RELOC_SPECIAL.
3444   void
3445   relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3446                                unsigned int sh_type,
3447                                const unsigned char* preloc_in,
3448                                size_t relnum,
3449                                Output_section* output_section,
3450                                typename elfcpp::Elf_types<size>::Elf_Off
3451                                  offset_in_output_section,
3452                                unsigned char* view,
3453                                Mips_address view_address,
3454                                section_size_type view_size,
3455                                unsigned char* preloc_out);
3456 
3457   // Return whether SYM is defined by the ABI.
3458   bool
do_is_defined_by_abi(const Symbol * sym) const3459   do_is_defined_by_abi(const Symbol* sym) const
3460   {
3461     return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3462             || (strcmp(sym->name(), "_gp_disp") == 0)
3463             || (strcmp(sym->name(), "___tls_get_addr") == 0));
3464   }
3465 
3466   // Return the number of entries in the GOT.
3467   unsigned int
got_entry_count() const3468   got_entry_count() const
3469   {
3470     if (!this->has_got_section())
3471       return 0;
3472     return this->got_size() / (size/8);
3473   }
3474 
3475   // Return the number of entries in the PLT.
3476   unsigned int
plt_entry_count() const3477   plt_entry_count() const
3478   {
3479     if (this->plt_ == NULL)
3480       return 0;
3481     return this->plt_->entry_count();
3482   }
3483 
3484   // Return the offset of the first non-reserved PLT entry.
3485   unsigned int
first_plt_entry_offset() const3486   first_plt_entry_offset() const
3487   { return this->plt_->first_plt_entry_offset(); }
3488 
3489   // Return the size of each PLT entry.
3490   unsigned int
plt_entry_size() const3491   plt_entry_size() const
3492   { return this->plt_->plt_entry_size(); }
3493 
3494   // Get the GOT section, creating it if necessary.
3495   Mips_output_data_got<size, big_endian>*
3496   got_section(Symbol_table*, Layout*);
3497 
3498   // Get the GOT section.
3499   Mips_output_data_got<size, big_endian>*
got_section() const3500   got_section() const
3501   {
3502     gold_assert(this->got_ != NULL);
3503     return this->got_;
3504   }
3505 
3506   // Get the .MIPS.stubs section, creating it if necessary.
3507   Mips_output_data_mips_stubs<size, big_endian>*
3508   mips_stubs_section(Layout* layout);
3509 
3510   // Get the .MIPS.stubs section.
3511   Mips_output_data_mips_stubs<size, big_endian>*
mips_stubs_section() const3512   mips_stubs_section() const
3513   {
3514     gold_assert(this->mips_stubs_ != NULL);
3515     return this->mips_stubs_;
3516   }
3517 
3518   // Get the LA25 stub section, creating it if necessary.
3519   Mips_output_data_la25_stub<size, big_endian>*
3520   la25_stub_section(Layout*);
3521 
3522   // Get the LA25 stub section.
3523   Mips_output_data_la25_stub<size, big_endian>*
la25_stub_section()3524   la25_stub_section()
3525   {
3526     gold_assert(this->la25_stub_ != NULL);
3527     return this->la25_stub_;
3528   }
3529 
3530   // Get gp value.  It has the value of .got + 0x7FF0.
3531   Mips_address
gp_value() const3532   gp_value() const
3533   {
3534     if (this->gp_ != NULL)
3535       return this->gp_->value();
3536     return 0;
3537   }
3538 
3539   // Get gp value.  It has the value of .got + 0x7FF0.  Adjust it for
3540   // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3541   Mips_address
adjusted_gp_value(const Mips_relobj<size,big_endian> * object)3542   adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3543   {
3544     if (this->gp_ == NULL)
3545       return 0;
3546 
3547     bool multi_got = false;
3548     if (this->has_got_section())
3549       multi_got = this->got_section()->multi_got();
3550     if (!multi_got)
3551       return this->gp_->value();
3552     else
3553       return this->gp_->value() + this->got_section()->get_got_offset(object);
3554   }
3555 
3556   // Get the dynamic reloc section, creating it if necessary.
3557   Reloc_section*
3558   rel_dyn_section(Layout*);
3559 
3560   bool
do_has_custom_set_dynsym_indexes() const3561   do_has_custom_set_dynsym_indexes() const
3562   { return true; }
3563 
3564   // Don't emit input .reginfo/.MIPS.abiflags sections to
3565   // output .reginfo/.MIPS.abiflags.
3566   bool
do_should_include_section(elfcpp::Elf_Word sh_type) const3567   do_should_include_section(elfcpp::Elf_Word sh_type) const
3568   {
3569     return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3570              && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3571   }
3572 
3573   // Set the dynamic symbol indexes.  INDEX is the index of the first
3574   // global dynamic symbol.  Pointers to the symbols are stored into the
3575   // vector SYMS.  The names are added to DYNPOOL.  This returns an
3576   // updated dynamic symbol index.
3577   unsigned int
3578   do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3579                         std::vector<Symbol*>* syms, Stringpool* dynpool,
3580                         Versions* versions, Symbol_table* symtab) const;
3581 
3582   // Remove .MIPS.stubs entry for a symbol.
3583   void
remove_lazy_stub_entry(Mips_symbol<size> * sym)3584   remove_lazy_stub_entry(Mips_symbol<size>* sym)
3585   {
3586     if (this->mips_stubs_ != NULL)
3587       this->mips_stubs_->remove_entry(sym);
3588   }
3589 
3590   // The value to write into got[1] for SVR4 targets, to identify it is
3591   // a GNU object.  The dynamic linker can then use got[1] to store the
3592   // module pointer.
3593   uint64_t
mips_elf_gnu_got1_mask()3594   mips_elf_gnu_got1_mask()
3595   {
3596     if (this->is_output_n64())
3597       return (uint64_t)1 << 63;
3598     else
3599       return 1 << 31;
3600   }
3601 
3602   // Whether the output has microMIPS code.  This is valid only after
3603   // merge_obj_e_flags() is called.
3604   bool
is_output_micromips() const3605   is_output_micromips() const
3606   {
3607     gold_assert(this->are_processor_specific_flags_set());
3608     return elfcpp::is_micromips(this->processor_specific_flags());
3609   }
3610 
3611   // Whether the output uses N32 ABI.  This is valid only after
3612   // merge_obj_e_flags() is called.
3613   bool
is_output_n32() const3614   is_output_n32() const
3615   {
3616     gold_assert(this->are_processor_specific_flags_set());
3617     return elfcpp::abi_n32(this->processor_specific_flags());
3618   }
3619 
3620   // Whether the output uses R6 ISA.  This is valid only after
3621   // merge_obj_e_flags() is called.
3622   bool
is_output_r6() const3623   is_output_r6() const
3624   {
3625     gold_assert(this->are_processor_specific_flags_set());
3626     return elfcpp::r6_isa(this->processor_specific_flags());
3627   }
3628 
3629   // Whether the output uses N64 ABI.
3630   bool
is_output_n64() const3631   is_output_n64() const
3632   { return size == 64; }
3633 
3634   // Whether the output uses NEWABI.  This is valid only after
3635   // merge_obj_e_flags() is called.
3636   bool
is_output_newabi() const3637   is_output_newabi() const
3638   { return this->is_output_n32() || this->is_output_n64(); }
3639 
3640   // Whether we can only use 32-bit microMIPS instructions.
3641   bool
use_32bit_micromips_instructions() const3642   use_32bit_micromips_instructions() const
3643   { return this->insn32_; }
3644 
3645   // Return the r_sym field from a relocation.
3646   unsigned int
get_r_sym(const unsigned char * preloc) const3647   get_r_sym(const unsigned char* preloc) const
3648   {
3649     // Since REL and RELA relocs share the same structure through
3650     // the r_info field, we can just use REL here.
3651     Reltype rel(preloc);
3652     return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3653 	get_r_sym(&rel);
3654   }
3655 
3656  protected:
3657   // Return the value to use for a dynamic symbol which requires special
3658   // treatment.  This is how we support equality comparisons of function
3659   // pointers across shared library boundaries, as described in the
3660   // processor specific ABI supplement.
3661   uint64_t
3662   do_dynsym_value(const Symbol* gsym) const;
3663 
3664   // Make an ELF object.
3665   Object*
3666   do_make_elf_object(const std::string&, Input_file*, off_t,
3667                      const elfcpp::Ehdr<size, big_endian>& ehdr);
3668 
3669   Object*
do_make_elf_object(const std::string &,Input_file *,off_t,const elfcpp::Ehdr<size,!big_endian> &)3670   do_make_elf_object(const std::string&, Input_file*, off_t,
3671                      const elfcpp::Ehdr<size, !big_endian>&)
3672   { gold_unreachable(); }
3673 
3674   // Make an output section.
3675   Output_section*
do_make_output_section(const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags)3676   do_make_output_section(const char* name, elfcpp::Elf_Word type,
3677                          elfcpp::Elf_Xword flags)
3678     {
3679       if (type == elfcpp::SHT_MIPS_OPTIONS)
3680         return new Mips_output_section_options<size, big_endian>(name, type,
3681                                                                  flags, this);
3682       else
3683         return new Output_section(name, type, flags);
3684     }
3685 
3686   // Adjust ELF file header.
3687   void
3688   do_adjust_elf_header(unsigned char* view, int len);
3689 
3690   // Get the custom dynamic tag value.
3691   unsigned int
3692   do_dynamic_tag_custom_value(elfcpp::DT) const;
3693 
3694   // Adjust the value written to the dynamic symbol table.
3695   virtual void
do_adjust_dyn_symbol(const Symbol * sym,unsigned char * view) const3696   do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3697   {
3698     elfcpp::Sym<size, big_endian> isym(view);
3699     elfcpp::Sym_write<size, big_endian> osym(view);
3700     const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3701 
3702     // Keep dynamic compressed symbols odd.  This allows the dynamic linker
3703     // to treat compressed symbols like any other.
3704     Mips_address value = isym.get_st_value();
3705     if (mips_sym->is_mips16() && value != 0)
3706       {
3707         if (!mips_sym->has_mips16_fn_stub())
3708           value |= 1;
3709         else
3710           {
3711             // If we have a MIPS16 function with a stub, the dynamic symbol
3712             // must refer to the stub, since only the stub uses the standard
3713             // calling conventions.  Stub contains MIPS32 code, so don't add +1
3714             // in this case.
3715 
3716             // There is a code which does this in the method
3717             // Target_mips::do_dynsym_value, but that code will only be
3718             // executed if the symbol is from dynobj.
3719             // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3720             // table.
3721 
3722             Mips16_stub_section<size, big_endian>* fn_stub =
3723               mips_sym->template get_mips16_fn_stub<big_endian>();
3724             value = fn_stub->output_address();
3725             osym.put_st_size(fn_stub->section_size());
3726           }
3727 
3728         osym.put_st_value(value);
3729         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3730                           mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3731       }
3732     else if ((mips_sym->is_micromips()
3733               // Stubs are always microMIPS if there is any microMIPS code in
3734               // the output.
3735               || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3736              && value != 0)
3737       {
3738         osym.put_st_value(value | 1);
3739         osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3740                           mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3741       }
3742   }
3743 
3744  private:
3745   // The class which scans relocations.
3746   class Scan
3747   {
3748    public:
Scan()3749     Scan()
3750     { }
3751 
3752     static inline int
3753     get_reference_flags(unsigned int r_type);
3754 
3755     inline void
3756     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3757           Sized_relobj_file<size, big_endian>* object,
3758           unsigned int data_shndx,
3759           Output_section* output_section,
3760           const Reltype& reloc, unsigned int r_type,
3761           const elfcpp::Sym<size, big_endian>& lsym,
3762           bool is_discarded);
3763 
3764     inline void
3765     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3766           Sized_relobj_file<size, big_endian>* object,
3767           unsigned int data_shndx,
3768           Output_section* output_section,
3769           const Relatype& reloc, unsigned int r_type,
3770           const elfcpp::Sym<size, big_endian>& lsym,
3771           bool is_discarded);
3772 
3773     inline void
3774     local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3775           Sized_relobj_file<size, big_endian>* object,
3776           unsigned int data_shndx,
3777           Output_section* output_section,
3778           const Relatype* rela,
3779           const Reltype* rel,
3780           unsigned int rel_type,
3781           unsigned int r_type,
3782           const elfcpp::Sym<size, big_endian>& lsym,
3783           bool is_discarded);
3784 
3785     inline void
3786     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3787            Sized_relobj_file<size, big_endian>* object,
3788            unsigned int data_shndx,
3789            Output_section* output_section,
3790            const Reltype& reloc, unsigned int r_type,
3791            Symbol* gsym);
3792 
3793     inline void
3794     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3795            Sized_relobj_file<size, big_endian>* object,
3796            unsigned int data_shndx,
3797            Output_section* output_section,
3798            const Relatype& reloc, unsigned int r_type,
3799            Symbol* gsym);
3800 
3801     inline void
3802     global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3803            Sized_relobj_file<size, big_endian>* object,
3804            unsigned int data_shndx,
3805            Output_section* output_section,
3806            const Relatype* rela,
3807            const Reltype* rel,
3808            unsigned int rel_type,
3809            unsigned int r_type,
3810            Symbol* gsym);
3811 
3812     inline bool
local_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_mips *,Sized_relobj_file<size,big_endian> *,unsigned int,Output_section *,const Reltype &,unsigned int,const elfcpp::Sym<size,big_endian> &)3813     local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3814                                         Target_mips*,
3815                                         Sized_relobj_file<size, big_endian>*,
3816                                         unsigned int,
3817                                         Output_section*,
3818                                         const Reltype&,
3819                                         unsigned int,
3820                                         const elfcpp::Sym<size, big_endian>&)
3821     { return false; }
3822 
3823     inline bool
global_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_mips *,Sized_relobj_file<size,big_endian> *,unsigned int,Output_section *,const Reltype &,unsigned int,Symbol *)3824     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3825                                          Target_mips*,
3826                                          Sized_relobj_file<size, big_endian>*,
3827                                          unsigned int,
3828                                          Output_section*,
3829                                          const Reltype&,
3830                                          unsigned int, Symbol*)
3831     { return false; }
3832 
3833     inline bool
local_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_mips *,Sized_relobj_file<size,big_endian> *,unsigned int,Output_section *,const Relatype &,unsigned int,const elfcpp::Sym<size,big_endian> &)3834     local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3835                                         Target_mips*,
3836                                         Sized_relobj_file<size, big_endian>*,
3837                                         unsigned int,
3838                                         Output_section*,
3839                                         const Relatype&,
3840                                         unsigned int,
3841                                         const elfcpp::Sym<size, big_endian>&)
3842     { return false; }
3843 
3844     inline bool
global_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_mips *,Sized_relobj_file<size,big_endian> *,unsigned int,Output_section *,const Relatype &,unsigned int,Symbol *)3845     global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3846                                          Target_mips*,
3847                                          Sized_relobj_file<size, big_endian>*,
3848                                          unsigned int,
3849                                          Output_section*,
3850                                          const Relatype&,
3851                                          unsigned int, Symbol*)
3852     { return false; }
3853    private:
3854     static void
3855     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3856                             unsigned int r_type);
3857 
3858     static void
3859     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3860                              unsigned int r_type, Symbol*);
3861   };
3862 
3863   // The class which implements relocation.
3864   class Relocate
3865   {
3866    public:
Relocate()3867     Relocate()
3868       : calculated_value_(0), calculate_only_(false)
3869     { }
3870 
~Relocate()3871     ~Relocate()
3872     { }
3873 
3874     // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3875     inline bool
3876     should_apply_static_reloc(const Mips_symbol<size>* gsym,
3877                               unsigned int r_type,
3878                               Output_section* output_section,
3879                               Target_mips* target);
3880 
3881     // Do a relocation.  Return false if the caller should not issue
3882     // any warnings about this relocation.
3883     inline bool
3884     relocate(const Relocate_info<size, big_endian>*, unsigned int,
3885 	     Target_mips*, Output_section*, size_t, const unsigned char*,
3886 	     const Sized_symbol<size>*, const Symbol_value<size>*,
3887 	     unsigned char*, Mips_address, section_size_type);
3888 
3889    private:
3890     // Result of the relocation.
3891     Valtype calculated_value_;
3892     // Whether we have to calculate relocation instead of applying it.
3893     bool calculate_only_;
3894   };
3895 
3896   // This POD class holds the dynamic relocations that should be emitted instead
3897   // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations.  We will emit these
3898   // relocations if it turns out that the symbol does not have static
3899   // relocations.
3900   class Dyn_reloc
3901   {
3902    public:
Dyn_reloc(Mips_symbol<size> * sym,unsigned int r_type,Mips_relobj<size,big_endian> * relobj,unsigned int shndx,Output_section * output_section,Mips_address r_offset)3903     Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3904               Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3905               Output_section* output_section, Mips_address r_offset)
3906       : sym_(sym), r_type_(r_type), relobj_(relobj),
3907         shndx_(shndx), output_section_(output_section),
3908         r_offset_(r_offset)
3909     { }
3910 
3911     // Emit this reloc if appropriate.  This is called after we have
3912     // scanned all the relocations, so we know whether the symbol has
3913     // static relocations.
3914     void
emit(Reloc_section * rel_dyn,Mips_output_data_got<size,big_endian> * got,Symbol_table * symtab)3915     emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3916          Symbol_table* symtab)
3917     {
3918       if (!this->sym_->has_static_relocs())
3919         {
3920           got->record_global_got_symbol(this->sym_, this->relobj_,
3921                                         this->r_type_, true, false);
3922           if (!symbol_references_local(this->sym_,
3923                                 this->sym_->should_add_dynsym_entry(symtab)))
3924             rel_dyn->add_global(this->sym_, this->r_type_,
3925                                 this->output_section_, this->relobj_,
3926                                 this->shndx_, this->r_offset_);
3927           else
3928             rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3929                                           this->output_section_, this->relobj_,
3930                                           this->shndx_, this->r_offset_);
3931         }
3932     }
3933 
3934    private:
3935     Mips_symbol<size>* sym_;
3936     unsigned int r_type_;
3937     Mips_relobj<size, big_endian>* relobj_;
3938     unsigned int shndx_;
3939     Output_section* output_section_;
3940     Mips_address r_offset_;
3941   };
3942 
3943   // Adjust TLS relocation type based on the options and whether this
3944   // is a local symbol.
3945   static tls::Tls_optimization
3946   optimize_tls_reloc(bool is_final, int r_type);
3947 
3948   // Return whether there is a GOT section.
3949   bool
has_got_section() const3950   has_got_section() const
3951   { return this->got_ != NULL; }
3952 
3953   // Check whether the given ELF header flags describe a 32-bit binary.
3954   bool
3955   mips_32bit_flags(elfcpp::Elf_Word);
3956 
3957   enum Mips_mach {
3958     mach_mips3000             = 3000,
3959     mach_mips3900             = 3900,
3960     mach_mips4000             = 4000,
3961     mach_mips4010             = 4010,
3962     mach_mips4100             = 4100,
3963     mach_mips4111             = 4111,
3964     mach_mips4120             = 4120,
3965     mach_mips4300             = 4300,
3966     mach_mips4400             = 4400,
3967     mach_mips4600             = 4600,
3968     mach_mips4650             = 4650,
3969     mach_mips5000             = 5000,
3970     mach_mips5400             = 5400,
3971     mach_mips5500             = 5500,
3972     mach_mips5900             = 5900,
3973     mach_mips6000             = 6000,
3974     mach_mips7000             = 7000,
3975     mach_mips8000             = 8000,
3976     mach_mips9000             = 9000,
3977     mach_mips10000            = 10000,
3978     mach_mips12000            = 12000,
3979     mach_mips14000            = 14000,
3980     mach_mips16000            = 16000,
3981     mach_mips16               = 16,
3982     mach_mips5                = 5,
3983     mach_mips_loongson_2e     = 3001,
3984     mach_mips_loongson_2f     = 3002,
3985     mach_mips_gs464           = 3003,
3986     mach_mips_gs464e          = 3004,
3987     mach_mips_gs264e          = 3005,
3988     mach_mips_sb1             = 12310201, // octal 'SB', 01
3989     mach_mips_octeon          = 6501,
3990     mach_mips_octeonp         = 6601,
3991     mach_mips_octeon2         = 6502,
3992     mach_mips_octeon3         = 6503,
3993     mach_mips_xlr             = 887682,   // decimal 'XLR'
3994     mach_mipsisa32            = 32,
3995     mach_mipsisa32r2          = 33,
3996     mach_mipsisa32r3          = 34,
3997     mach_mipsisa32r5          = 36,
3998     mach_mipsisa32r6          = 37,
3999     mach_mipsisa64            = 64,
4000     mach_mipsisa64r2          = 65,
4001     mach_mipsisa64r3          = 66,
4002     mach_mipsisa64r5          = 68,
4003     mach_mipsisa64r6          = 69,
4004     mach_mips_micromips       = 96
4005   };
4006 
4007   // Return the MACH for a MIPS e_flags value.
4008   unsigned int
4009   elf_mips_mach(elfcpp::Elf_Word);
4010 
4011   // Return the MACH for each .MIPS.abiflags ISA Extension.
4012   unsigned int
4013   mips_isa_ext_mach(unsigned int);
4014 
4015   // Return the .MIPS.abiflags value representing each ISA Extension.
4016   unsigned int
4017   mips_isa_ext(unsigned int);
4018 
4019   // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4020   void
4021   update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4022                       Mips_abiflags<big_endian>*);
4023 
4024   // Infer the content of the ABI flags based on the elf header.
4025   void
4026   infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4027 
4028   // Create abiflags from elf header or from .MIPS.abiflags section.
4029   void
4030   create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4031 
4032   // Return the meaning of fp_abi, or "unknown" if not known.
4033   const char*
4034   fp_abi_string(int);
4035 
4036   // Select fp_abi.
4037   int
4038   select_fp_abi(const std::string&, int, int);
4039 
4040   // Merge attributes from input object.
4041   void
4042   merge_obj_attributes(const std::string&, const Attributes_section_data*);
4043 
4044   // Merge abiflags from input object.
4045   void
4046   merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4047 
4048   // Check whether machine EXTENSION is an extension of machine BASE.
4049   bool
4050   mips_mach_extends(unsigned int, unsigned int);
4051 
4052   // Merge file header flags from input object.
4053   void
4054   merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4055 
4056   // Encode ISA level and revision as a single value.
4057   int
level_rev(unsigned char isa_level,unsigned char isa_rev) const4058   level_rev(unsigned char isa_level, unsigned char isa_rev) const
4059   { return (isa_level << 3) | isa_rev; }
4060 
4061   // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4062   static inline bool
jal_to_bal()4063   jal_to_bal()
4064   { return false; }
4065 
4066   // True if we are linking for CPUs that are faster if JALR is converted to
4067   // BAL.  This should be safe for all architectures.  We enable this predicate
4068   // for all CPUs.
4069   static inline bool
jalr_to_bal()4070   jalr_to_bal()
4071   { return true; }
4072 
4073   // True if we are linking for CPUs that are faster if JR is converted to B.
4074   // This should be safe for all architectures.  We enable this predicate for
4075   // all CPUs.
4076   static inline bool
jr_to_b()4077   jr_to_b()
4078   { return true; }
4079 
4080   // Return the size of the GOT section.
4081   section_size_type
got_size() const4082   got_size() const
4083   {
4084     gold_assert(this->got_ != NULL);
4085     return this->got_->data_size();
4086   }
4087 
4088   // Create a PLT entry for a global symbol referenced by r_type relocation.
4089   void
4090   make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4091                  unsigned int r_type);
4092 
4093   // Get the PLT section.
4094   Mips_output_data_plt<size, big_endian>*
plt_section() const4095   plt_section() const
4096   {
4097     gold_assert(this->plt_ != NULL);
4098     return this->plt_;
4099   }
4100 
4101   // Get the GOT PLT section.
4102   const Mips_output_data_plt<size, big_endian>*
got_plt_section() const4103   got_plt_section() const
4104   {
4105     gold_assert(this->got_plt_ != NULL);
4106     return this->got_plt_;
4107   }
4108 
4109   // Copy a relocation against a global symbol.
4110   void
copy_reloc(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int shndx,Output_section * output_section,Symbol * sym,unsigned int r_type,Mips_address r_offset)4111   copy_reloc(Symbol_table* symtab, Layout* layout,
4112              Sized_relobj_file<size, big_endian>* object,
4113              unsigned int shndx, Output_section* output_section,
4114              Symbol* sym, unsigned int r_type, Mips_address r_offset)
4115   {
4116     this->copy_relocs_.copy_reloc(symtab, layout,
4117                                   symtab->get_sized_symbol<size>(sym),
4118                                   object, shndx, output_section,
4119                                   r_type, r_offset, 0,
4120                                   this->rel_dyn_section(layout));
4121   }
4122 
4123   void
dynamic_reloc(Mips_symbol<size> * sym,unsigned int r_type,Mips_relobj<size,big_endian> * relobj,unsigned int shndx,Output_section * output_section,Mips_address r_offset)4124   dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4125                 Mips_relobj<size, big_endian>* relobj,
4126                 unsigned int shndx, Output_section* output_section,
4127                 Mips_address r_offset)
4128   {
4129     this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4130                                           output_section, r_offset));
4131   }
4132 
4133   // Calculate value of _gp symbol.
4134   void
4135   set_gp(Layout*, Symbol_table*);
4136 
4137   const char*
4138   elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4139   const char*
4140   elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4141 
4142   // Adds entries that describe how machines relate to one another.  The entries
4143   // are ordered topologically with MIPS I extensions listed last.  First
4144   // element is extension, second element is base.
4145   void
add_machine_extensions()4146   add_machine_extensions()
4147   {
4148     // MIPS64r2 extensions.
4149     this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4150     this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4151     this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4152     this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4153     this->add_extension(mach_mips_gs264e, mach_mips_gs464e);
4154     this->add_extension(mach_mips_gs464e, mach_mips_gs464);
4155     this->add_extension(mach_mips_gs464, mach_mipsisa64r2);
4156 
4157     // MIPS64 extensions.
4158     this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4159     this->add_extension(mach_mips_sb1, mach_mipsisa64);
4160     this->add_extension(mach_mips_xlr, mach_mipsisa64);
4161 
4162     // MIPS V extensions.
4163     this->add_extension(mach_mipsisa64, mach_mips5);
4164 
4165     // R10000 extensions.
4166     this->add_extension(mach_mips12000, mach_mips10000);
4167     this->add_extension(mach_mips14000, mach_mips10000);
4168     this->add_extension(mach_mips16000, mach_mips10000);
4169 
4170     // R5000 extensions.  Note: the vr5500 ISA is an extension of the core
4171     // vr5400 ISA, but doesn't include the multimedia stuff.  It seems
4172     // better to allow vr5400 and vr5500 code to be merged anyway, since
4173     // many libraries will just use the core ISA.  Perhaps we could add
4174     // some sort of ASE flag if this ever proves a problem.
4175     this->add_extension(mach_mips5500, mach_mips5400);
4176     this->add_extension(mach_mips5400, mach_mips5000);
4177 
4178     // MIPS IV extensions.
4179     this->add_extension(mach_mips5, mach_mips8000);
4180     this->add_extension(mach_mips10000, mach_mips8000);
4181     this->add_extension(mach_mips5000, mach_mips8000);
4182     this->add_extension(mach_mips7000, mach_mips8000);
4183     this->add_extension(mach_mips9000, mach_mips8000);
4184 
4185     // VR4100 extensions.
4186     this->add_extension(mach_mips4120, mach_mips4100);
4187     this->add_extension(mach_mips4111, mach_mips4100);
4188 
4189     // MIPS III extensions.
4190     this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4191     this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4192     this->add_extension(mach_mips8000, mach_mips4000);
4193     this->add_extension(mach_mips4650, mach_mips4000);
4194     this->add_extension(mach_mips4600, mach_mips4000);
4195     this->add_extension(mach_mips4400, mach_mips4000);
4196     this->add_extension(mach_mips4300, mach_mips4000);
4197     this->add_extension(mach_mips4100, mach_mips4000);
4198     this->add_extension(mach_mips4010, mach_mips4000);
4199     this->add_extension(mach_mips5900, mach_mips4000);
4200 
4201     // MIPS32 extensions.
4202     this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4203 
4204     // MIPS II extensions.
4205     this->add_extension(mach_mips4000, mach_mips6000);
4206     this->add_extension(mach_mipsisa32, mach_mips6000);
4207 
4208     // MIPS I extensions.
4209     this->add_extension(mach_mips6000, mach_mips3000);
4210     this->add_extension(mach_mips3900, mach_mips3000);
4211   }
4212 
4213   // Add value to MIPS extenstions.
4214   void
add_extension(unsigned int base,unsigned int extension)4215   add_extension(unsigned int base, unsigned int extension)
4216   {
4217     std::pair<unsigned int, unsigned int> ext(base, extension);
4218     this->mips_mach_extensions_.push_back(ext);
4219   }
4220 
4221   // Return the number of entries in the .dynsym section.
get_dt_mips_symtabno() const4222   unsigned int get_dt_mips_symtabno() const
4223   {
4224     return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4225                            / elfcpp::Elf_sizes<size>::sym_size));
4226     // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4227   }
4228 
4229   // Information about this specific target which we pass to the
4230   // general Target structure.
4231   static const Target::Target_info mips_info;
4232   // The GOT section.
4233   Mips_output_data_got<size, big_endian>* got_;
4234   // gp symbol.  It has the value of .got + 0x7FF0.
4235   Sized_symbol<size>* gp_;
4236   // The PLT section.
4237   Mips_output_data_plt<size, big_endian>* plt_;
4238   // The GOT PLT section.
4239   Output_data_space* got_plt_;
4240   // The dynamic reloc section.
4241   Reloc_section* rel_dyn_;
4242   // The .rld_map section.
4243   Output_data_zero_fill* rld_map_;
4244   // Relocs saved to avoid a COPY reloc.
4245   Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4246 
4247   // A list of dyn relocs to be saved.
4248   std::vector<Dyn_reloc> dyn_relocs_;
4249 
4250   // The LA25 stub section.
4251   Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4252   // Architecture extensions.
4253   std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4254   // .MIPS.stubs
4255   Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4256 
4257   // Attributes section data in output.
4258   Attributes_section_data* attributes_section_data_;
4259   // .MIPS.abiflags section data in output.
4260   Mips_abiflags<big_endian>* abiflags_;
4261 
4262   unsigned int mach_;
4263   Layout* layout_;
4264 
4265   typename std::list<got16_addend<size, big_endian> > got16_addends_;
4266 
4267   // Whether there is an input .MIPS.abiflags section.
4268   bool has_abiflags_section_;
4269 
4270   // Whether the entry symbol is mips16 or micromips.
4271   bool entry_symbol_is_compressed_;
4272 
4273   // Whether we can use only 32-bit microMIPS instructions.
4274   // TODO(sasa): This should be a linker option.
4275   bool insn32_;
4276 };
4277 
4278 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4279 // It records high part of the relocation pair.
4280 
4281 template<int size, bool big_endian>
4282 struct reloc_high
4283 {
4284   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4285 
reloc_high__anon5561842d0111::reloc_high4286   reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4287              const Symbol_value<size>* _psymval, Mips_address _addend,
4288              unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4289              Mips_address _address = 0, bool _gp_disp = false)
4290     : view(_view), object(_object), psymval(_psymval), addend(_addend),
4291       r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4292       address(_address), gp_disp(_gp_disp)
4293   { }
4294 
4295   unsigned char* view;
4296   const Mips_relobj<size, big_endian>* object;
4297   const Symbol_value<size>* psymval;
4298   Mips_address addend;
4299   unsigned int r_type;
4300   unsigned int r_sym;
4301   bool extract_addend;
4302   Mips_address address;
4303   bool gp_disp;
4304 };
4305 
4306 template<int size, bool big_endian>
4307 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4308 {
4309   typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4310   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4311   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4312   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4313   typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4314 
4315  public:
4316   typedef enum
4317   {
4318     STATUS_OKAY,            // No error during relocation.
4319     STATUS_OVERFLOW,        // Relocation overflow.
4320     STATUS_BAD_RELOC,       // Relocation cannot be applied.
4321     STATUS_PCREL_UNALIGNED  // Unaligned PC-relative relocation.
4322   } Status;
4323 
4324  private:
4325   typedef Relocate_functions<size, big_endian> Base;
4326   typedef Mips_relocate_functions<size, big_endian> This;
4327 
4328   static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4329   static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4330   static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
4331 
4332   template<int valsize>
4333   static inline typename This::Status
check_overflow(Valtype value)4334   check_overflow(Valtype value)
4335   {
4336     if (size == 32)
4337       return (Bits<valsize>::has_overflow32(value)
4338               ? This::STATUS_OVERFLOW
4339               : This::STATUS_OKAY);
4340 
4341     return (Bits<valsize>::has_overflow(value)
4342             ? This::STATUS_OVERFLOW
4343             : This::STATUS_OKAY);
4344   }
4345 
4346   static inline bool
should_shuffle_micromips_reloc(unsigned int r_type)4347   should_shuffle_micromips_reloc(unsigned int r_type)
4348   {
4349     return (micromips_reloc(r_type)
4350             && r_type != elfcpp::R_MICROMIPS_PC7_S1
4351             && r_type != elfcpp::R_MICROMIPS_PC10_S1
4352             && r_type != elfcpp::R_MICROMIPS_GPREL7_S2);
4353   }
4354 
4355  public:
4356   //   R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4357   //   Most mips16 instructions are 16 bits, but these instructions
4358   //   are 32 bits.
4359   //
4360   //   The format of these instructions is:
4361   //
4362   //   +--------------+--------------------------------+
4363   //   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
4364   //   +--------------+--------------------------------+
4365   //   |                Immediate  15:0                |
4366   //   +-----------------------------------------------+
4367   //
4368   //   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
4369   //   Note that the immediate value in the first word is swapped.
4370   //
4371   //   When producing a relocatable object file, R_MIPS16_26 is
4372   //   handled mostly like R_MIPS_26.  In particular, the addend is
4373   //   stored as a straight 26-bit value in a 32-bit instruction.
4374   //   (gas makes life simpler for itself by never adjusting a
4375   //   R_MIPS16_26 reloc to be against a section, so the addend is
4376   //   always zero).  However, the 32 bit instruction is stored as 2
4377   //   16-bit values, rather than a single 32-bit value.  In a
4378   //   big-endian file, the result is the same; in a little-endian
4379   //   file, the two 16-bit halves of the 32 bit value are swapped.
4380   //   This is so that a disassembler can recognize the jal
4381   //   instruction.
4382   //
4383   //   When doing a final link, R_MIPS16_26 is treated as a 32 bit
4384   //   instruction stored as two 16-bit values.  The addend A is the
4385   //   contents of the targ26 field.  The calculation is the same as
4386   //   R_MIPS_26.  When storing the calculated value, reorder the
4387   //   immediate value as shown above, and don't forget to store the
4388   //   value as two 16-bit values.
4389   //
4390   //   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4391   //   defined as
4392   //
4393   //   big-endian:
4394   //   +--------+----------------------+
4395   //   |        |                      |
4396   //   |        |    targ26-16         |
4397   //   |31    26|25                   0|
4398   //   +--------+----------------------+
4399   //
4400   //   little-endian:
4401   //   +----------+------+-------------+
4402   //   |          |      |             |
4403   //   |  sub1    |      |     sub2    |
4404   //   |0        9|10  15|16         31|
4405   //   +----------+--------------------+
4406   //   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4407   //   ((sub1 << 16) | sub2)).
4408   //
4409   //   When producing a relocatable object file, the calculation is
4410   //   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4411   //   When producing a fully linked file, the calculation is
4412   //   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4413   //   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4414   //
4415   //   The table below lists the other MIPS16 instruction relocations.
4416   //   Each one is calculated in the same way as the non-MIPS16 relocation
4417   //   given on the right, but using the extended MIPS16 layout of 16-bit
4418   //   immediate fields:
4419   //
4420   //      R_MIPS16_GPREL          R_MIPS_GPREL16
4421   //      R_MIPS16_GOT16          R_MIPS_GOT16
4422   //      R_MIPS16_CALL16         R_MIPS_CALL16
4423   //      R_MIPS16_HI16           R_MIPS_HI16
4424   //      R_MIPS16_LO16           R_MIPS_LO16
4425   //
4426   //   A typical instruction will have a format like this:
4427   //
4428   //   +--------------+--------------------------------+
4429   //   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
4430   //   +--------------+--------------------------------+
4431   //   |    Major     |   rx   |   ry   |   Imm  4:0   |
4432   //   +--------------+--------------------------------+
4433   //
4434   //   EXTEND is the five bit value 11110.  Major is the instruction
4435   //   opcode.
4436   //
4437   //   All we need to do here is shuffle the bits appropriately.
4438   //   As above, the two 16-bit halves must be swapped on a
4439   //   little-endian system.
4440 
4441   // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4442   // on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1,
4443   // R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply
4444   // to 16-bit instructions.
4445 
4446   static void
mips_reloc_unshuffle(unsigned char * view,unsigned int r_type,bool jal_shuffle)4447   mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4448                        bool jal_shuffle)
4449   {
4450     if (!mips16_reloc(r_type)
4451         && !should_shuffle_micromips_reloc(r_type))
4452       return;
4453 
4454     // Pick up the first and second halfwords of the instruction.
4455     Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4456     Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4457     Valtype32 val;
4458 
4459     if (micromips_reloc(r_type)
4460         || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4461       val = first << 16 | second;
4462     else if (r_type != elfcpp::R_MIPS16_26)
4463       val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4464              | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4465     else
4466       val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4467              | ((first & 0x1f) << 21) | second);
4468 
4469     elfcpp::Swap<32, big_endian>::writeval(view, val);
4470   }
4471 
4472   static void
mips_reloc_shuffle(unsigned char * view,unsigned int r_type,bool jal_shuffle)4473   mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4474   {
4475     if (!mips16_reloc(r_type)
4476         && !should_shuffle_micromips_reloc(r_type))
4477       return;
4478 
4479     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4480     Valtype16 first, second;
4481 
4482     if (micromips_reloc(r_type)
4483         || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4484       {
4485         second = val & 0xffff;
4486         first = val >> 16;
4487       }
4488     else if (r_type != elfcpp::R_MIPS16_26)
4489       {
4490         second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4491         first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4492       }
4493     else
4494       {
4495         second = val & 0xffff;
4496         first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4497                  | ((val >> 21) & 0x1f);
4498       }
4499 
4500     elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4501     elfcpp::Swap<16, big_endian>::writeval(view, first);
4502   }
4503 
4504   // R_MIPS_16: S + sign-extend(A)
4505   static inline typename This::Status
rel16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4506   rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4507         const Symbol_value<size>* psymval, Mips_address addend_a,
4508         bool extract_addend, bool calculate_only, Valtype* calculated_value)
4509   {
4510     Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4511     Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4512 
4513     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4514                                      : addend_a);
4515 
4516     Valtype x = psymval->value(object, addend);
4517     val = Bits<16>::bit_select32(val, x, 0xffffU);
4518 
4519     if (calculate_only)
4520       {
4521         *calculated_value = x;
4522         return This::STATUS_OKAY;
4523       }
4524     else
4525       elfcpp::Swap<16, big_endian>::writeval(wv, val);
4526 
4527     return check_overflow<16>(x);
4528   }
4529 
4530   // R_MIPS_32: S + A
4531   static inline typename This::Status
rel32(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4532   rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4533         const Symbol_value<size>* psymval, Mips_address addend_a,
4534         bool extract_addend, bool calculate_only, Valtype* calculated_value)
4535   {
4536     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4537     Valtype addend = (extract_addend
4538                         ? elfcpp::Swap<32, big_endian>::readval(wv)
4539                         : addend_a);
4540     Valtype x = psymval->value(object, addend);
4541 
4542     if (calculate_only)
4543       *calculated_value = x;
4544     else
4545       elfcpp::Swap<32, big_endian>::writeval(wv, x);
4546 
4547     return This::STATUS_OKAY;
4548   }
4549 
4550   // R_MIPS_JALR, R_MICROMIPS_JALR
4551   static inline typename This::Status
reljalr(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool cross_mode_jump,unsigned int r_type,bool jalr_to_bal,bool jr_to_b,bool calculate_only,Valtype * calculated_value)4552   reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4553           const Symbol_value<size>* psymval, Mips_address address,
4554           Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4555           unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4556           bool calculate_only, Valtype* calculated_value)
4557   {
4558     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4559     Valtype addend = extract_addend ? 0 : addend_a;
4560     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4561 
4562     // Try converting J(AL)R to B(AL), if the target is in range.
4563     if (r_type == elfcpp::R_MIPS_JALR
4564         && !cross_mode_jump
4565         && ((jalr_to_bal && val == 0x0320f809)    // jalr t9
4566             || (jr_to_b && val == 0x03200008)))   // jr t9
4567       {
4568         int offset = psymval->value(object, addend) - (address + 4);
4569         if (!Bits<18>::has_overflow32(offset))
4570           {
4571             if (val == 0x03200008)   // jr t9
4572               val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff);  // b addr
4573             else
4574               val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4575           }
4576       }
4577 
4578     if (calculate_only)
4579       *calculated_value = val;
4580     else
4581       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4582 
4583     return This::STATUS_OKAY;
4584   }
4585 
4586   // R_MIPS_PC32: S + A - P
4587   static inline typename This::Status
relpc32(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4588   relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4589           const Symbol_value<size>* psymval, Mips_address address,
4590           Mips_address addend_a, bool extract_addend, bool calculate_only,
4591           Valtype* calculated_value)
4592   {
4593     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4594     Valtype addend = (extract_addend
4595                         ? elfcpp::Swap<32, big_endian>::readval(wv)
4596                         : addend_a);
4597     Valtype x = psymval->value(object, addend) - address;
4598 
4599     if (calculate_only)
4600        *calculated_value = x;
4601     else
4602       elfcpp::Swap<32, big_endian>::writeval(wv, x);
4603 
4604     return This::STATUS_OKAY;
4605   }
4606 
4607   // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4608   static inline typename This::Status
rel26(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,bool local,Mips_address addend_a,bool extract_addend,const Symbol * gsym,bool cross_mode_jump,unsigned int r_type,bool jal_to_bal,bool calculate_only,Valtype * calculated_value)4609   rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4610         const Symbol_value<size>* psymval, Mips_address address,
4611         bool local, Mips_address addend_a, bool extract_addend,
4612         const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4613         bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4614   {
4615     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4616     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4617 
4618     Valtype addend;
4619     if (extract_addend)
4620       {
4621         if (r_type == elfcpp::R_MICROMIPS_26_S1)
4622           addend = (val & 0x03ffffff) << 1;
4623         else
4624           addend = (val & 0x03ffffff) << 2;
4625       }
4626     else
4627       addend = addend_a;
4628 
4629     // Make sure the target of JALX is word-aligned.  Bit 0 must be
4630     // the correct ISA mode selector and bit 1 must be 0.
4631     if (!calculate_only && cross_mode_jump
4632         && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4633       {
4634         gold_warning(_("JALX to a non-word-aligned address"));
4635         return This::STATUS_BAD_RELOC;
4636       }
4637 
4638     // Shift is 2, unusually, for microMIPS JALX.
4639     unsigned int shift =
4640         (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4641 
4642     Valtype x;
4643     if (local)
4644       x = addend | ((address + 4) & (0xfc000000 << shift));
4645     else
4646       {
4647         if (shift == 1)
4648           x = Bits<27>::sign_extend32(addend);
4649         else
4650           x = Bits<28>::sign_extend32(addend);
4651       }
4652     x = psymval->value(object, x) >> shift;
4653 
4654     if (!calculate_only && !local && !gsym->is_weak_undefined()
4655         && ((x >> 26) != ((address + 4) >> (26 + shift))))
4656       return This::STATUS_OVERFLOW;
4657 
4658     val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4659 
4660     // If required, turn JAL into JALX.
4661     if (cross_mode_jump)
4662       {
4663         bool ok;
4664         Valtype32 opcode = val >> 26;
4665         Valtype32 jalx_opcode;
4666 
4667         // Check to see if the opcode is already JAL or JALX.
4668         if (r_type == elfcpp::R_MIPS16_26)
4669           {
4670             ok = (opcode == 0x6) || (opcode == 0x7);
4671             jalx_opcode = 0x7;
4672           }
4673         else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4674           {
4675             ok = (opcode == 0x3d) || (opcode == 0x3c);
4676             jalx_opcode = 0x3c;
4677           }
4678         else
4679           {
4680             ok = (opcode == 0x3) || (opcode == 0x1d);
4681             jalx_opcode = 0x1d;
4682           }
4683 
4684         // If the opcode is not JAL or JALX, there's a problem.  We cannot
4685         // convert J or JALS to JALX.
4686         if (!calculate_only && !ok)
4687           {
4688             gold_error(_("Unsupported jump between ISA modes; consider "
4689                          "recompiling with interlinking enabled."));
4690             return This::STATUS_BAD_RELOC;
4691           }
4692 
4693         // Make this the JALX opcode.
4694         val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4695       }
4696 
4697     // Try converting JAL to BAL, if the target is in range.
4698     if (!parameters->options().relocatable()
4699         && !cross_mode_jump
4700         && ((jal_to_bal
4701             && r_type == elfcpp::R_MIPS_26
4702             && (val >> 26) == 0x3)))    // jal addr
4703       {
4704         Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4705         int offset = dest - (address + 4);
4706         if (!Bits<18>::has_overflow32(offset))
4707           {
4708             if (val == 0x03200008)   // jr t9
4709               val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff);  // b addr
4710             else
4711               val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4712           }
4713       }
4714 
4715     if (calculate_only)
4716       *calculated_value = val;
4717     else
4718       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4719 
4720     return This::STATUS_OKAY;
4721   }
4722 
4723   // R_MIPS_PC16
4724   static inline typename This::Status
relpc16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4725   relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4726           const Symbol_value<size>* psymval, Mips_address address,
4727           Mips_address addend_a, bool extract_addend, bool calculate_only,
4728           Valtype* calculated_value)
4729   {
4730     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4731     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4732 
4733     Valtype addend = (extract_addend
4734                       ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4735                       : addend_a);
4736 
4737     Valtype x = psymval->value(object, addend) - address;
4738     val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4739 
4740     if (calculate_only)
4741       {
4742         *calculated_value = x >> 2;
4743         return This::STATUS_OKAY;
4744       }
4745     else
4746       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4747 
4748     if (psymval->value(object, addend) & 3)
4749       return This::STATUS_PCREL_UNALIGNED;
4750 
4751     return check_overflow<18>(x);
4752   }
4753 
4754   // R_MIPS_PC21_S2
4755   static inline typename This::Status
relpc21(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4756   relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4757           const Symbol_value<size>* psymval, Mips_address address,
4758           Mips_address addend_a, bool extract_addend, bool calculate_only,
4759           Valtype* calculated_value)
4760   {
4761     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4762     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4763 
4764     Valtype addend = (extract_addend
4765                       ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4766                       : addend_a);
4767 
4768     Valtype x = psymval->value(object, addend) - address;
4769     val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4770 
4771     if (calculate_only)
4772       {
4773         *calculated_value = x >> 2;
4774         return This::STATUS_OKAY;
4775       }
4776     else
4777       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4778 
4779     if (psymval->value(object, addend) & 3)
4780       return This::STATUS_PCREL_UNALIGNED;
4781 
4782     return check_overflow<23>(x);
4783   }
4784 
4785   // R_MIPS_PC26_S2
4786   static inline typename This::Status
relpc26(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4787   relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4788           const Symbol_value<size>* psymval, Mips_address address,
4789           Mips_address addend_a, bool extract_addend, bool calculate_only,
4790           Valtype* calculated_value)
4791   {
4792     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4793     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4794 
4795     Valtype addend = (extract_addend
4796                       ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4797                       : addend_a);
4798 
4799     Valtype x = psymval->value(object, addend) - address;
4800     val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4801 
4802     if (calculate_only)
4803       {
4804         *calculated_value = x >> 2;
4805         return This::STATUS_OKAY;
4806       }
4807     else
4808       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4809 
4810     if (psymval->value(object, addend) & 3)
4811       return This::STATUS_PCREL_UNALIGNED;
4812 
4813     return check_overflow<28>(x);
4814   }
4815 
4816   // R_MIPS_PC18_S3
4817   static inline typename This::Status
relpc18(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4818   relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4819           const Symbol_value<size>* psymval, Mips_address address,
4820           Mips_address addend_a, bool extract_addend, bool calculate_only,
4821           Valtype* calculated_value)
4822   {
4823     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4824     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4825 
4826     Valtype addend = (extract_addend
4827                       ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4828                       : addend_a);
4829 
4830     Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4831     val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4832 
4833     if (calculate_only)
4834       {
4835         *calculated_value = x >> 3;
4836         return This::STATUS_OKAY;
4837       }
4838     else
4839       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4840 
4841     if (psymval->value(object, addend) & 7)
4842       return This::STATUS_PCREL_UNALIGNED;
4843 
4844     return check_overflow<21>(x);
4845   }
4846 
4847   // R_MIPS_PC19_S2
4848   static inline typename This::Status
relpc19(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4849   relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4850           const Symbol_value<size>* psymval, Mips_address address,
4851           Mips_address addend_a, bool extract_addend, bool calculate_only,
4852           Valtype* calculated_value)
4853   {
4854     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4855     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4856 
4857     Valtype addend = (extract_addend
4858                       ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4859                       : addend_a);
4860 
4861     Valtype x = psymval->value(object, addend) - address;
4862     val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4863 
4864     if (calculate_only)
4865       {
4866         *calculated_value = x >> 2;
4867         return This::STATUS_OKAY;
4868       }
4869     else
4870       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4871 
4872     if (psymval->value(object, addend) & 3)
4873       return This::STATUS_PCREL_UNALIGNED;
4874 
4875     return check_overflow<21>(x);
4876   }
4877 
4878   // R_MIPS_PCHI16
4879   static inline typename This::Status
relpchi16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend,Mips_address address,unsigned int r_sym,bool extract_addend)4880   relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4881             const Symbol_value<size>* psymval, Mips_address addend,
4882             Mips_address address, unsigned int r_sym, bool extract_addend)
4883   {
4884     // Record the relocation.  It will be resolved when we find pclo16 part.
4885     pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4886                             addend, 0, r_sym, extract_addend, address));
4887     return This::STATUS_OKAY;
4888   }
4889 
4890   // R_MIPS_PCHI16
4891   static inline typename This::Status
do_relpchi16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_hi,Mips_address address,bool extract_addend,Valtype32 addend_lo,bool calculate_only,Valtype * calculated_value)4892   do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4893              const Symbol_value<size>* psymval, Mips_address addend_hi,
4894              Mips_address address, bool extract_addend, Valtype32 addend_lo,
4895              bool calculate_only, Valtype* calculated_value)
4896   {
4897     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4898     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4899 
4900     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4901                                        : addend_hi);
4902 
4903     Valtype value = psymval->value(object, addend) - address;
4904     Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4905     val = Bits<32>::bit_select32(val, x, 0xffff);
4906 
4907     if (calculate_only)
4908       *calculated_value = x;
4909     else
4910       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4911 
4912     return This::STATUS_OKAY;
4913   }
4914 
4915   // R_MIPS_PCLO16
4916   static inline typename This::Status
relpclo16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,Mips_address address,unsigned int r_sym,unsigned int rel_type,bool calculate_only,Valtype * calculated_value)4917   relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4918             const Symbol_value<size>* psymval, Mips_address addend_a,
4919             bool extract_addend, Mips_address address, unsigned int r_sym,
4920             unsigned int rel_type, bool calculate_only,
4921             Valtype* calculated_value)
4922   {
4923     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4924     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4925 
4926     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4927                                      : addend_a);
4928 
4929     if (rel_type == elfcpp::SHT_REL)
4930       {
4931         // Resolve pending R_MIPS_PCHI16 relocations.
4932         typename std::list<reloc_high<size, big_endian> >::iterator it =
4933             pchi16_relocs.begin();
4934         while (it != pchi16_relocs.end())
4935           {
4936             reloc_high<size, big_endian> pchi16 = *it;
4937             if (pchi16.r_sym == r_sym)
4938               {
4939                 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4940                              pchi16.addend, pchi16.address,
4941                              pchi16.extract_addend, addend, calculate_only,
4942                              calculated_value);
4943                 it = pchi16_relocs.erase(it);
4944               }
4945             else
4946               ++it;
4947           }
4948       }
4949 
4950     // Resolve R_MIPS_PCLO16 relocation.
4951     Valtype x = psymval->value(object, addend) - address;
4952     val = Bits<32>::bit_select32(val, x, 0xffff);
4953 
4954     if (calculate_only)
4955       *calculated_value = x;
4956     else
4957       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4958 
4959     return This::STATUS_OKAY;
4960   }
4961 
4962   // R_MICROMIPS_PC7_S1
4963   static inline typename This::Status
relmicromips_pc7_s1(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4964   relmicromips_pc7_s1(unsigned char* view,
4965                       const Mips_relobj<size, big_endian>* object,
4966                       const Symbol_value<size>* psymval, Mips_address address,
4967                       Mips_address addend_a, bool extract_addend,
4968                       bool calculate_only, Valtype* calculated_value)
4969   {
4970     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4971     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4972 
4973     Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4974                                     : addend_a;
4975 
4976     Valtype x = psymval->value(object, addend) - address;
4977     val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4978 
4979     if (calculate_only)
4980       {
4981         *calculated_value = x >> 1;
4982         return This::STATUS_OKAY;
4983       }
4984     else
4985       elfcpp::Swap<32, big_endian>::writeval(wv, val);
4986 
4987     return check_overflow<8>(x);
4988   }
4989 
4990   // R_MICROMIPS_PC10_S1
4991   static inline typename This::Status
relmicromips_pc10_s1(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)4992   relmicromips_pc10_s1(unsigned char* view,
4993                        const Mips_relobj<size, big_endian>* object,
4994                        const Symbol_value<size>* psymval, Mips_address address,
4995                        Mips_address addend_a, bool extract_addend,
4996                        bool calculate_only, Valtype* calculated_value)
4997   {
4998     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4999     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5000 
5001     Valtype addend = (extract_addend
5002                       ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
5003                       : addend_a);
5004 
5005     Valtype x = psymval->value(object, addend) - address;
5006     val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
5007 
5008     if (calculate_only)
5009       {
5010         *calculated_value = x >> 1;
5011         return This::STATUS_OKAY;
5012       }
5013     else
5014       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5015 
5016     return check_overflow<11>(x);
5017   }
5018 
5019   // R_MICROMIPS_PC16_S1
5020   static inline typename This::Status
relmicromips_pc16_s1(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address address,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5021   relmicromips_pc16_s1(unsigned char* view,
5022                        const Mips_relobj<size, big_endian>* object,
5023                        const Symbol_value<size>* psymval, Mips_address address,
5024                        Mips_address addend_a, bool extract_addend,
5025                        bool calculate_only, Valtype* calculated_value)
5026   {
5027     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5028     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5029 
5030     Valtype addend = (extract_addend
5031                       ? Bits<17>::sign_extend32((val & 0xffff) << 1)
5032                       : addend_a);
5033 
5034     Valtype x = psymval->value(object, addend) - address;
5035     val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
5036 
5037     if (calculate_only)
5038       {
5039         *calculated_value = x >> 1;
5040         return This::STATUS_OKAY;
5041       }
5042     else
5043       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5044 
5045     return check_overflow<17>(x);
5046   }
5047 
5048   // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5049   static inline typename This::Status
relhi16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend,Mips_address address,bool gp_disp,unsigned int r_type,unsigned int r_sym,bool extract_addend)5050   relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5051           const Symbol_value<size>* psymval, Mips_address addend,
5052           Mips_address address, bool gp_disp, unsigned int r_type,
5053           unsigned int r_sym, bool extract_addend)
5054   {
5055     // Record the relocation.  It will be resolved when we find lo16 part.
5056     hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5057                           addend, r_type, r_sym, extract_addend, address,
5058                           gp_disp));
5059     return This::STATUS_OKAY;
5060   }
5061 
5062   // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5063   static inline typename This::Status
do_relhi16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_hi,Mips_address address,bool is_gp_disp,unsigned int r_type,bool extract_addend,Valtype32 addend_lo,Target_mips<size,big_endian> * target,bool calculate_only,Valtype * calculated_value)5064   do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5065              const Symbol_value<size>* psymval, Mips_address addend_hi,
5066              Mips_address address, bool is_gp_disp, unsigned int r_type,
5067              bool extract_addend, Valtype32 addend_lo,
5068              Target_mips<size, big_endian>* target, bool calculate_only,
5069              Valtype* calculated_value)
5070   {
5071     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5072     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5073 
5074     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5075                                        : addend_hi);
5076 
5077     Valtype32 value;
5078     if (!is_gp_disp)
5079       value = psymval->value(object, addend);
5080     else
5081       {
5082         // For MIPS16 ABI code we generate this sequence
5083         //    0: li      $v0,%hi(_gp_disp)
5084         //    4: addiupc $v1,%lo(_gp_disp)
5085         //    8: sll     $v0,16
5086         //   12: addu    $v0,$v1
5087         //   14: move    $gp,$v0
5088         // So the offsets of hi and lo relocs are the same, but the
5089         // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5090         // ADDIUPC clears the low two bits of the instruction address,
5091         // so the base is ($t9 + 4) & ~3.
5092         Valtype32 gp_disp;
5093         if (r_type == elfcpp::R_MIPS16_HI16)
5094           gp_disp = (target->adjusted_gp_value(object)
5095                      - ((address + 4) & ~0x3));
5096         // The microMIPS .cpload sequence uses the same assembly
5097         // instructions as the traditional psABI version, but the
5098         // incoming $t9 has the low bit set.
5099         else if (r_type == elfcpp::R_MICROMIPS_HI16)
5100           gp_disp = target->adjusted_gp_value(object) - address - 1;
5101         else
5102           gp_disp = target->adjusted_gp_value(object) - address;
5103         value = gp_disp + addend;
5104       }
5105     Valtype x = ((value + 0x8000) >> 16) & 0xffff;
5106     val = Bits<32>::bit_select32(val, x, 0xffff);
5107 
5108     if (calculate_only)
5109       {
5110         *calculated_value = x;
5111         return This::STATUS_OKAY;
5112       }
5113     else
5114       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5115 
5116     return (is_gp_disp ? check_overflow<16>(x)
5117                        : This::STATUS_OKAY);
5118   }
5119 
5120   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5121   static inline typename This::Status
relgot16_local(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,unsigned int r_type,unsigned int r_sym)5122   relgot16_local(unsigned char* view,
5123                  const Mips_relobj<size, big_endian>* object,
5124                  const Symbol_value<size>* psymval, Mips_address addend_a,
5125                  bool extract_addend, unsigned int r_type, unsigned int r_sym)
5126   {
5127     // Record the relocation.  It will be resolved when we find lo16 part.
5128     got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5129                            addend_a, r_type, r_sym, extract_addend));
5130     return This::STATUS_OKAY;
5131   }
5132 
5133   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5134   static inline typename This::Status
do_relgot16_local(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_hi,bool extract_addend,Valtype32 addend_lo,Target_mips<size,big_endian> * target,bool calculate_only,Valtype * calculated_value)5135   do_relgot16_local(unsigned char* view,
5136                     const Mips_relobj<size, big_endian>* object,
5137                     const Symbol_value<size>* psymval, Mips_address addend_hi,
5138                     bool extract_addend, Valtype32 addend_lo,
5139                     Target_mips<size, big_endian>* target, bool calculate_only,
5140                     Valtype* calculated_value)
5141   {
5142     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5143     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5144 
5145     Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5146                                        : addend_hi);
5147 
5148     // Find GOT page entry.
5149     Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5150                           & 0xffff;
5151     value <<= 16;
5152     unsigned int got_offset =
5153       target->got_section()->get_got_page_offset(value, object);
5154 
5155     // Resolve the relocation.
5156     Valtype x = target->got_section()->gp_offset(got_offset, object);
5157     val = Bits<32>::bit_select32(val, x, 0xffff);
5158 
5159     if (calculate_only)
5160       {
5161         *calculated_value = x;
5162         return This::STATUS_OKAY;
5163       }
5164     else
5165       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5166 
5167     return check_overflow<16>(x);
5168   }
5169 
5170   // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5171   static inline typename This::Status
rello16(Target_mips<size,big_endian> * target,unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,Mips_address address,bool is_gp_disp,unsigned int r_type,unsigned int r_sym,unsigned int rel_type,bool calculate_only,Valtype * calculated_value)5172   rello16(Target_mips<size, big_endian>* target, unsigned char* view,
5173           const Mips_relobj<size, big_endian>* object,
5174           const Symbol_value<size>* psymval, Mips_address addend_a,
5175           bool extract_addend, Mips_address address, bool is_gp_disp,
5176           unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
5177           bool calculate_only, Valtype* calculated_value)
5178   {
5179     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5180     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5181 
5182     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5183                                      : addend_a);
5184 
5185     if (rel_type == elfcpp::SHT_REL)
5186       {
5187         typename This::Status reloc_status = This::STATUS_OKAY;
5188         // Resolve pending R_MIPS_HI16 relocations.
5189         typename std::list<reloc_high<size, big_endian> >::iterator it =
5190           hi16_relocs.begin();
5191         while (it != hi16_relocs.end())
5192           {
5193             reloc_high<size, big_endian> hi16 = *it;
5194             if (hi16.r_sym == r_sym
5195                 && is_matching_lo16_reloc(hi16.r_type, r_type))
5196               {
5197                 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
5198                 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
5199                                        hi16.addend, hi16.address, hi16.gp_disp,
5200                                        hi16.r_type, hi16.extract_addend, addend,
5201                                        target, calculate_only, calculated_value);
5202                 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
5203                 if (reloc_status == This::STATUS_OVERFLOW)
5204                   return This::STATUS_OVERFLOW;
5205                 it = hi16_relocs.erase(it);
5206               }
5207             else
5208               ++it;
5209           }
5210 
5211         // Resolve pending local R_MIPS_GOT16 relocations.
5212         typename std::list<reloc_high<size, big_endian> >::iterator it2 =
5213           got16_relocs.begin();
5214         while (it2 != got16_relocs.end())
5215           {
5216             reloc_high<size, big_endian> got16 = *it2;
5217             if (got16.r_sym == r_sym
5218                 && is_matching_lo16_reloc(got16.r_type, r_type))
5219               {
5220                 mips_reloc_unshuffle(got16.view, got16.r_type, false);
5221 
5222                 reloc_status = do_relgot16_local(got16.view, got16.object,
5223                                      got16.psymval, got16.addend,
5224                                      got16.extract_addend, addend, target,
5225                                      calculate_only, calculated_value);
5226 
5227                 mips_reloc_shuffle(got16.view, got16.r_type, false);
5228                 if (reloc_status == This::STATUS_OVERFLOW)
5229                   return This::STATUS_OVERFLOW;
5230                 it2 = got16_relocs.erase(it2);
5231               }
5232             else
5233               ++it2;
5234           }
5235       }
5236 
5237     // Resolve R_MIPS_LO16 relocation.
5238     Valtype x;
5239     if (!is_gp_disp)
5240       x = psymval->value(object, addend);
5241     else
5242       {
5243         // See the comment for R_MIPS16_HI16 above for the reason
5244         // for this conditional.
5245         Valtype32 gp_disp;
5246         if (r_type == elfcpp::R_MIPS16_LO16)
5247           gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
5248         else if (r_type == elfcpp::R_MICROMIPS_LO16
5249                  || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
5250           gp_disp = target->adjusted_gp_value(object) - address + 3;
5251         else
5252           gp_disp = target->adjusted_gp_value(object) - address + 4;
5253         // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5254         // for overflow.  Relocations against _gp_disp are normally
5255         // generated from the .cpload pseudo-op.  It generates code
5256         // that normally looks like this:
5257 
5258         //   lui    $gp,%hi(_gp_disp)
5259         //   addiu  $gp,$gp,%lo(_gp_disp)
5260         //   addu   $gp,$gp,$t9
5261 
5262         // Here $t9 holds the address of the function being called,
5263         // as required by the MIPS ELF ABI.  The R_MIPS_LO16
5264         // relocation can easily overflow in this situation, but the
5265         // R_MIPS_HI16 relocation will handle the overflow.
5266         // Therefore, we consider this a bug in the MIPS ABI, and do
5267         // not check for overflow here.
5268         x = gp_disp + addend;
5269       }
5270     val = Bits<32>::bit_select32(val, x, 0xffff);
5271 
5272     if (calculate_only)
5273       *calculated_value = x;
5274     else
5275       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5276 
5277     return This::STATUS_OKAY;
5278   }
5279 
5280   // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5281   // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5282   // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5283   // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5284   // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5285   // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5286   static inline typename This::Status
relgot(unsigned char * view,int gp_offset,bool calculate_only,Valtype * calculated_value)5287   relgot(unsigned char* view, int gp_offset, bool calculate_only,
5288          Valtype* calculated_value)
5289   {
5290     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5291     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5292     Valtype x = gp_offset;
5293     val = Bits<32>::bit_select32(val, x, 0xffff);
5294 
5295     if (calculate_only)
5296       {
5297         *calculated_value = x;
5298         return This::STATUS_OKAY;
5299       }
5300     else
5301       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5302 
5303     return check_overflow<16>(x);
5304   }
5305 
5306   // R_MIPS_EH
5307   static inline typename This::Status
releh(unsigned char * view,int gp_offset,bool calculate_only,Valtype * calculated_value)5308   releh(unsigned char* view, int gp_offset, bool calculate_only,
5309         Valtype* calculated_value)
5310   {
5311     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5312     Valtype x = gp_offset;
5313 
5314     if (calculate_only)
5315       {
5316         *calculated_value = x;
5317         return This::STATUS_OKAY;
5318       }
5319     else
5320       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5321 
5322     return check_overflow<32>(x);
5323   }
5324 
5325   // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5326   static inline typename This::Status
relgotpage(Target_mips<size,big_endian> * target,unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5327   relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5328              const Mips_relobj<size, big_endian>* object,
5329              const Symbol_value<size>* psymval, Mips_address addend_a,
5330              bool extract_addend, bool calculate_only,
5331              Valtype* calculated_value)
5332   {
5333     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5334     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5335     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5336 
5337     // Find a GOT page entry that points to within 32KB of symbol + addend.
5338     Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5339     unsigned int  got_offset =
5340       target->got_section()->get_got_page_offset(value, object);
5341 
5342     Valtype x = target->got_section()->gp_offset(got_offset, object);
5343     val = Bits<32>::bit_select32(val, x, 0xffff);
5344 
5345     if (calculate_only)
5346       {
5347         *calculated_value = x;
5348         return This::STATUS_OKAY;
5349       }
5350     else
5351       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5352 
5353     return check_overflow<16>(x);
5354   }
5355 
5356   // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5357   static inline typename This::Status
relgotofst(Target_mips<size,big_endian> * target,unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool local,bool calculate_only,Valtype * calculated_value)5358   relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5359              const Mips_relobj<size, big_endian>* object,
5360              const Symbol_value<size>* psymval, Mips_address addend_a,
5361              bool extract_addend, bool local, bool calculate_only,
5362              Valtype* calculated_value)
5363   {
5364     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5365     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5366     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5367 
5368     // For a local symbol, find a GOT page entry that points to within 32KB of
5369     // symbol + addend.  Relocation value is the offset of the GOT page entry's
5370     // value from symbol + addend.
5371     // For a global symbol, relocation value is addend.
5372     Valtype x;
5373     if (local)
5374       {
5375         // Find GOT page entry.
5376         Mips_address value = ((psymval->value(object, addend) + 0x8000)
5377                               & ~0xffff);
5378         target->got_section()->get_got_page_offset(value, object);
5379 
5380         x = psymval->value(object, addend) - value;
5381       }
5382     else
5383       x = addend;
5384     val = Bits<32>::bit_select32(val, x, 0xffff);
5385 
5386     if (calculate_only)
5387       {
5388         *calculated_value = x;
5389         return This::STATUS_OKAY;
5390       }
5391     else
5392       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5393 
5394     return check_overflow<16>(x);
5395   }
5396 
5397   // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5398   // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5399   static inline typename This::Status
relgot_hi16(unsigned char * view,int gp_offset,bool calculate_only,Valtype * calculated_value)5400   relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5401               Valtype* calculated_value)
5402   {
5403     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5404     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5405     Valtype x = gp_offset;
5406     x = ((x + 0x8000) >> 16) & 0xffff;
5407     val = Bits<32>::bit_select32(val, x, 0xffff);
5408 
5409     if (calculate_only)
5410       *calculated_value = x;
5411     else
5412       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5413 
5414     return This::STATUS_OKAY;
5415   }
5416 
5417   // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5418   // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5419   static inline typename This::Status
relgot_lo16(unsigned char * view,int gp_offset,bool calculate_only,Valtype * calculated_value)5420   relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5421               Valtype* calculated_value)
5422   {
5423     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5424     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5425     Valtype x = gp_offset;
5426     val = Bits<32>::bit_select32(val, x, 0xffff);
5427 
5428     if (calculate_only)
5429       *calculated_value = x;
5430     else
5431       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5432 
5433     return This::STATUS_OKAY;
5434   }
5435 
5436   // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5437   // R_MICROMIPS_GPREL16
5438   static inline typename This::Status
relgprel(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address gp,Mips_address addend_a,bool extract_addend,bool local,bool calculate_only,Valtype * calculated_value)5439   relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5440            const Symbol_value<size>* psymval, Mips_address gp,
5441            Mips_address addend_a, bool extract_addend, bool local,
5442            bool calculate_only, Valtype* calculated_value)
5443   {
5444     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5445     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5446 
5447     Valtype addend;
5448     if (extract_addend)
5449       {
5450 	addend = val & 0xffff;
5451         // Only sign-extend the addend if it was extracted from the
5452         // instruction.  If the addend was separate, leave it alone,
5453         // otherwise we may lose significant bits.
5454         addend = Bits<16>::sign_extend32(addend);
5455       }
5456     else
5457       addend = addend_a;
5458 
5459     Valtype x = psymval->value(object, addend) - gp;
5460 
5461     // If the symbol was local, any earlier relocatable links will
5462     // have adjusted its addend with the gp offset, so compensate
5463     // for that now.  Don't do it for symbols forced local in this
5464     // link, though, since they won't have had the gp offset applied
5465     // to them before.
5466     if (local)
5467       x += object->gp_value();
5468 
5469     val = Bits<32>::bit_select32(val, x, 0xffff);
5470 
5471     if (calculate_only)
5472       {
5473         *calculated_value = x;
5474         return This::STATUS_OKAY;
5475       }
5476     else
5477       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5478 
5479     if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5480       {
5481 	gold_error(_("small-data section too large;"
5482 		     " lower small-data size limit (see option -G)"));
5483         return This::STATUS_OVERFLOW;
5484       }
5485     return This::STATUS_OKAY;
5486   }
5487 
5488   // R_MICROMIPS_GPREL7_S2
5489   static inline typename This::Status
relgprel7(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address gp,Mips_address addend_a,bool extract_addend,bool local,bool calculate_only,Valtype * calculated_value)5490   relgprel7(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5491 	    const Symbol_value<size>* psymval, Mips_address gp,
5492 	    Mips_address addend_a, bool extract_addend, bool local,
5493 	    bool calculate_only, Valtype* calculated_value)
5494   {
5495     Valtype16* wv = reinterpret_cast<Valtype16*>(view);
5496     Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
5497 
5498     Valtype addend;
5499     if (extract_addend)
5500       {
5501 	addend = (val & 0x7f) << 2;
5502 	addend = Bits<9>::sign_extend32(addend);
5503       }
5504     else
5505       addend = addend_a;
5506 
5507     Valtype x = psymval->value(object, addend) - gp;
5508 
5509     if (local)
5510       x += object->gp_value();
5511 
5512     val = Bits<16>::bit_select32(val, x >> 2, 0x7f);
5513 
5514     if (calculate_only)
5515       {
5516 	*calculated_value = x;
5517 	return This::STATUS_OKAY;
5518       }
5519     else
5520       elfcpp::Swap<16, big_endian>::writeval(wv, val);
5521 
5522     if (check_overflow<9>(x) == This::STATUS_OVERFLOW)
5523       {
5524 	gold_error(_("small-data section too large;"
5525 		     " lower small-data size limit (see option -G)"));
5526 	return This::STATUS_OVERFLOW;
5527       }
5528     return This::STATUS_OKAY;
5529   }
5530 
5531   // R_MIPS_GPREL32
5532   static inline typename This::Status
relgprel32(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address gp,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5533   relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5534              const Symbol_value<size>* psymval, Mips_address gp,
5535              Mips_address addend_a, bool extract_addend, bool calculate_only,
5536              Valtype* calculated_value)
5537   {
5538     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5539     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5540     Valtype addend = extract_addend ? val : addend_a;
5541 
5542     // R_MIPS_GPREL32 relocations are defined for local symbols only.
5543     Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5544 
5545     if (calculate_only)
5546       *calculated_value = x;
5547     else
5548       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5549 
5550     return This::STATUS_OKAY;
5551  }
5552 
5553   // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5554   // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5555   // R_MICROMIPS_TLS_DTPREL_HI16
5556   static inline typename This::Status
tlsrelhi16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Valtype32 tp_offset,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5557   tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5558              const Symbol_value<size>* psymval, Valtype32 tp_offset,
5559              Mips_address addend_a, bool extract_addend, bool calculate_only,
5560              Valtype* calculated_value)
5561   {
5562     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5563     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5564     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5565 
5566     // tls symbol values are relative to tls_segment()->vaddr()
5567     Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5568     val = Bits<32>::bit_select32(val, x, 0xffff);
5569 
5570     if (calculate_only)
5571       *calculated_value = x;
5572     else
5573       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5574 
5575     return This::STATUS_OKAY;
5576   }
5577 
5578   // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5579   // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5580   // R_MICROMIPS_TLS_DTPREL_LO16,
5581   static inline typename This::Status
tlsrello16(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Valtype32 tp_offset,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5582   tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5583              const Symbol_value<size>* psymval, Valtype32 tp_offset,
5584              Mips_address addend_a, bool extract_addend, bool calculate_only,
5585              Valtype* calculated_value)
5586   {
5587     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5588     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5589     Valtype addend = extract_addend ? val & 0xffff : addend_a;
5590 
5591     // tls symbol values are relative to tls_segment()->vaddr()
5592     Valtype x = psymval->value(object, addend) - tp_offset;
5593     val = Bits<32>::bit_select32(val, x, 0xffff);
5594 
5595     if (calculate_only)
5596       *calculated_value = x;
5597     else
5598       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5599 
5600     return This::STATUS_OKAY;
5601   }
5602 
5603   // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5604   // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5605   static inline typename This::Status
tlsrel32(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Valtype32 tp_offset,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5606   tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5607            const Symbol_value<size>* psymval, Valtype32 tp_offset,
5608            Mips_address addend_a, bool extract_addend, bool calculate_only,
5609            Valtype* calculated_value)
5610   {
5611     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5612     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5613     Valtype addend = extract_addend ? val : addend_a;
5614 
5615     // tls symbol values are relative to tls_segment()->vaddr()
5616     Valtype x = psymval->value(object, addend) - tp_offset;
5617 
5618     if (calculate_only)
5619       *calculated_value = x;
5620     else
5621       elfcpp::Swap<32, big_endian>::writeval(wv, x);
5622 
5623     return This::STATUS_OKAY;
5624   }
5625 
5626   // R_MIPS_SUB, R_MICROMIPS_SUB
5627   static inline typename This::Status
relsub(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5628   relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5629          const Symbol_value<size>* psymval, Mips_address addend_a,
5630          bool extract_addend, bool calculate_only, Valtype* calculated_value)
5631   {
5632     Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5633     Valtype64 addend = (extract_addend
5634                         ? elfcpp::Swap<64, big_endian>::readval(wv)
5635                         : addend_a);
5636 
5637     Valtype64 x = psymval->value(object, -addend);
5638     if (calculate_only)
5639       *calculated_value = x;
5640     else
5641       elfcpp::Swap<64, big_endian>::writeval(wv, x);
5642 
5643     return This::STATUS_OKAY;
5644   }
5645 
5646   // R_MIPS_64: S + A
5647   static inline typename This::Status
rel64(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value,bool apply_addend_only)5648   rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5649         const Symbol_value<size>* psymval, Mips_address addend_a,
5650         bool extract_addend, bool calculate_only, Valtype* calculated_value,
5651         bool apply_addend_only)
5652   {
5653     Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5654     Valtype64 addend = (extract_addend
5655                         ? elfcpp::Swap<64, big_endian>::readval(wv)
5656                         : addend_a);
5657 
5658     Valtype64 x = psymval->value(object, addend);
5659     if (calculate_only)
5660       *calculated_value = x;
5661     else
5662       {
5663         if (apply_addend_only)
5664           x = addend;
5665         elfcpp::Swap<64, big_endian>::writeval(wv, x);
5666       }
5667 
5668     return This::STATUS_OKAY;
5669   }
5670 
5671   // R_MIPS_HIGHER, R_MICROMIPS_HIGHER
5672   static inline typename This::Status
relhigher(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5673   relhigher(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5674             const Symbol_value<size>* psymval, Mips_address addend_a,
5675             bool extract_addend, bool calculate_only, Valtype* calculated_value)
5676   {
5677     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5678     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5679     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5680                                      : addend_a);
5681 
5682     Valtype x = psymval->value(object, addend);
5683     x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
5684     val = Bits<32>::bit_select32(val, x, 0xffff);
5685 
5686     if (calculate_only)
5687       *calculated_value = x;
5688     else
5689       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5690 
5691     return This::STATUS_OKAY;
5692   }
5693 
5694   // R_MIPS_HIGHEST, R_MICROMIPS_HIGHEST
5695   static inline typename This::Status
relhighest(unsigned char * view,const Mips_relobj<size,big_endian> * object,const Symbol_value<size> * psymval,Mips_address addend_a,bool extract_addend,bool calculate_only,Valtype * calculated_value)5696   relhighest(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5697              const Symbol_value<size>* psymval, Mips_address addend_a,
5698              bool extract_addend, bool calculate_only,
5699              Valtype* calculated_value)
5700   {
5701     Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5702     Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5703     Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5704                                      : addend_a);
5705 
5706     Valtype x = psymval->value(object, addend);
5707     x = ((x + (uint64_t) 0x800080008000llu) >> 48) & 0xffff;
5708     val = Bits<32>::bit_select32(val, x, 0xffff);
5709 
5710     if (calculate_only)
5711       *calculated_value = x;
5712     else
5713       elfcpp::Swap<32, big_endian>::writeval(wv, val);
5714 
5715     return This::STATUS_OKAY;
5716   }
5717 };
5718 
5719 template<int size, bool big_endian>
5720 typename std::list<reloc_high<size, big_endian> >
5721     Mips_relocate_functions<size, big_endian>::hi16_relocs;
5722 
5723 template<int size, bool big_endian>
5724 typename std::list<reloc_high<size, big_endian> >
5725     Mips_relocate_functions<size, big_endian>::got16_relocs;
5726 
5727 template<int size, bool big_endian>
5728 typename std::list<reloc_high<size, big_endian> >
5729     Mips_relocate_functions<size, big_endian>::pchi16_relocs;
5730 
5731 // Mips_got_info methods.
5732 
5733 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5734 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5735 
5736 template<int size, bool big_endian>
5737 void
record_local_got_symbol(Mips_relobj<size,big_endian> * object,unsigned int symndx,Mips_address addend,unsigned int r_type,unsigned int shndx,bool is_section_symbol)5738 Mips_got_info<size, big_endian>::record_local_got_symbol(
5739     Mips_relobj<size, big_endian>* object, unsigned int symndx,
5740     Mips_address addend, unsigned int r_type, unsigned int shndx,
5741     bool is_section_symbol)
5742 {
5743   Mips_got_entry<size, big_endian>* entry =
5744     new Mips_got_entry<size, big_endian>(object, symndx, addend,
5745                                          mips_elf_reloc_tls_type(r_type),
5746                                          shndx, is_section_symbol);
5747   this->record_got_entry(entry, object);
5748 }
5749 
5750 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5751 // in OBJECT.  FOR_CALL is true if the caller is only interested in
5752 // using the GOT entry for calls.  DYN_RELOC is true if R_TYPE is a dynamic
5753 // relocation.
5754 
5755 template<int size, bool big_endian>
5756 void
record_global_got_symbol(Mips_symbol<size> * mips_sym,Mips_relobj<size,big_endian> * object,unsigned int r_type,bool dyn_reloc,bool for_call)5757 Mips_got_info<size, big_endian>::record_global_got_symbol(
5758     Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5759     unsigned int r_type, bool dyn_reloc, bool for_call)
5760 {
5761   if (!for_call)
5762     mips_sym->set_got_not_only_for_calls();
5763 
5764   // A global symbol in the GOT must also be in the dynamic symbol table.
5765   if (!mips_sym->needs_dynsym_entry() && !mips_sym->is_forced_local())
5766     {
5767       switch (mips_sym->visibility())
5768         {
5769         case elfcpp::STV_INTERNAL:
5770         case elfcpp::STV_HIDDEN:
5771           mips_sym->set_is_forced_local();
5772           break;
5773         default:
5774           mips_sym->set_needs_dynsym_entry();
5775           break;
5776         }
5777     }
5778 
5779   unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5780   if (tls_type == GOT_TLS_NONE)
5781     this->global_got_symbols_.insert(mips_sym);
5782 
5783   if (dyn_reloc)
5784     {
5785       if (mips_sym->global_got_area() == GGA_NONE)
5786         mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5787       return;
5788     }
5789 
5790   Mips_got_entry<size, big_endian>* entry =
5791     new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5792 
5793   this->record_got_entry(entry, object);
5794 }
5795 
5796 // Add ENTRY to master GOT and to OBJECT's GOT.
5797 
5798 template<int size, bool big_endian>
5799 void
record_got_entry(Mips_got_entry<size,big_endian> * entry,Mips_relobj<size,big_endian> * object)5800 Mips_got_info<size, big_endian>::record_got_entry(
5801     Mips_got_entry<size, big_endian>* entry,
5802     Mips_relobj<size, big_endian>* object)
5803 {
5804   this->got_entries_.insert(entry);
5805 
5806   // Create the GOT entry for the OBJECT's GOT.
5807   Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5808   Mips_got_entry<size, big_endian>* entry2 =
5809     new Mips_got_entry<size, big_endian>(*entry);
5810 
5811   g->got_entries_.insert(entry2);
5812 }
5813 
5814 // Record that OBJECT has a page relocation against symbol SYMNDX and
5815 // that ADDEND is the addend for that relocation.
5816 // This function creates an upper bound on the number of GOT slots
5817 // required; no attempt is made to combine references to non-overridable
5818 // global symbols across multiple input files.
5819 
5820 template<int size, bool big_endian>
5821 void
record_got_page_entry(Mips_relobj<size,big_endian> * object,unsigned int symndx,int addend)5822 Mips_got_info<size, big_endian>::record_got_page_entry(
5823     Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5824 {
5825   struct Got_page_range **range_ptr, *range;
5826   int old_pages, new_pages;
5827 
5828   // Find the Got_page_entry for this symbol.
5829   Got_page_entry* entry = new Got_page_entry(object, symndx);
5830   typename Got_page_entry_set::iterator it =
5831     this->got_page_entries_.find(entry);
5832   if (it != this->got_page_entries_.end())
5833     entry = *it;
5834   else
5835     this->got_page_entries_.insert(entry);
5836 
5837   // Get the object's GOT, but we don't need to insert an entry here.
5838   Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5839 
5840   // Skip over ranges whose maximum extent cannot share a page entry
5841   // with ADDEND.
5842   range_ptr = &entry->ranges;
5843   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5844     range_ptr = &(*range_ptr)->next;
5845 
5846   // If we scanned to the end of the list, or found a range whose
5847   // minimum extent cannot share a page entry with ADDEND, create
5848   // a new singleton range.
5849   range = *range_ptr;
5850   if (!range || addend < range->min_addend - 0xffff)
5851     {
5852       range = new Got_page_range();
5853       range->next = *range_ptr;
5854       range->min_addend = addend;
5855       range->max_addend = addend;
5856 
5857       *range_ptr = range;
5858       ++this->page_gotno_;
5859       ++g2->page_gotno_;
5860       return;
5861     }
5862 
5863   // Remember how many pages the old range contributed.
5864   old_pages = range->get_max_pages();
5865 
5866   // Update the ranges.
5867   if (addend < range->min_addend)
5868     range->min_addend = addend;
5869   else if (addend > range->max_addend)
5870     {
5871       if (range->next && addend >= range->next->min_addend - 0xffff)
5872         {
5873           old_pages += range->next->get_max_pages();
5874           range->max_addend = range->next->max_addend;
5875           range->next = range->next->next;
5876         }
5877       else
5878         range->max_addend = addend;
5879     }
5880 
5881   // Record any change in the total estimate.
5882   new_pages = range->get_max_pages();
5883   if (old_pages != new_pages)
5884     {
5885       this->page_gotno_ += new_pages - old_pages;
5886       g2->page_gotno_ += new_pages - old_pages;
5887     }
5888 }
5889 
5890 // Create all entries that should be in the local part of the GOT.
5891 
5892 template<int size, bool big_endian>
5893 void
add_local_entries(Target_mips<size,big_endian> * target,Layout * layout)5894 Mips_got_info<size, big_endian>::add_local_entries(
5895     Target_mips<size, big_endian>* target, Layout* layout)
5896 {
5897   Mips_output_data_got<size, big_endian>* got = target->got_section();
5898   // First two GOT entries are reserved.  The first entry will be filled at
5899   // runtime.  The second entry will be used by some runtime loaders.
5900   got->add_constant(0);
5901   got->add_constant(target->mips_elf_gnu_got1_mask());
5902 
5903   for (typename Got_entry_set::iterator
5904        p = this->got_entries_.begin();
5905        p != this->got_entries_.end();
5906        ++p)
5907     {
5908       Mips_got_entry<size, big_endian>* entry = *p;
5909       if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5910         {
5911           got->add_local(entry->object(), entry->symndx(),
5912                          GOT_TYPE_STANDARD, entry->addend());
5913           unsigned int got_offset = entry->object()->local_got_offset(
5914               entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5915           if (got->multi_got() && this->index_ > 0
5916               && parameters->options().output_is_position_independent())
5917           {
5918             if (!entry->is_section_symbol())
5919               target->rel_dyn_section(layout)->add_local(entry->object(),
5920                   entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5921             else
5922               target->rel_dyn_section(layout)->add_symbolless_local_addend(
5923                   entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5924                   got, got_offset);
5925           }
5926         }
5927     }
5928 
5929   this->add_page_entries(target, layout);
5930 
5931   // Add global entries that should be in the local area.
5932   for (typename Got_entry_set::iterator
5933        p = this->got_entries_.begin();
5934        p != this->got_entries_.end();
5935        ++p)
5936     {
5937       Mips_got_entry<size, big_endian>* entry = *p;
5938       if (!entry->is_for_global_symbol())
5939         continue;
5940 
5941       Mips_symbol<size>* mips_sym = entry->sym();
5942       if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5943         {
5944           unsigned int got_type;
5945           if (!got->multi_got())
5946             got_type = GOT_TYPE_STANDARD;
5947           else
5948             got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5949           if (got->add_global(mips_sym, got_type))
5950             {
5951               mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5952               if (got->multi_got() && this->index_ > 0
5953                   && parameters->options().output_is_position_independent())
5954                 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5955                     mips_sym, elfcpp::R_MIPS_REL32, got,
5956                     mips_sym->got_offset(got_type));
5957             }
5958         }
5959     }
5960 }
5961 
5962 // Create GOT page entries.
5963 
5964 template<int size, bool big_endian>
5965 void
add_page_entries(Target_mips<size,big_endian> * target,Layout * layout)5966 Mips_got_info<size, big_endian>::add_page_entries(
5967     Target_mips<size, big_endian>* target, Layout* layout)
5968 {
5969   if (this->page_gotno_ == 0)
5970     return;
5971 
5972   Mips_output_data_got<size, big_endian>* got = target->got_section();
5973   this->got_page_offset_start_ = got->add_constant(0);
5974   if (got->multi_got() && this->index_ > 0
5975       && parameters->options().output_is_position_independent())
5976     target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5977                                                   this->got_page_offset_start_);
5978   int num_entries = this->page_gotno_;
5979   unsigned int prev_offset = this->got_page_offset_start_;
5980   while (--num_entries > 0)
5981     {
5982       unsigned int next_offset = got->add_constant(0);
5983       if (got->multi_got() && this->index_ > 0
5984           && parameters->options().output_is_position_independent())
5985         target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5986                                                       next_offset);
5987       gold_assert(next_offset == prev_offset + size/8);
5988       prev_offset = next_offset;
5989     }
5990   this->got_page_offset_next_ = this->got_page_offset_start_;
5991 }
5992 
5993 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5994 
5995 template<int size, bool big_endian>
5996 void
add_global_entries(Target_mips<size,big_endian> * target,Layout * layout,unsigned int non_reloc_only_global_gotno)5997 Mips_got_info<size, big_endian>::add_global_entries(
5998     Target_mips<size, big_endian>* target, Layout* layout,
5999     unsigned int non_reloc_only_global_gotno)
6000 {
6001   Mips_output_data_got<size, big_endian>* got = target->got_section();
6002   // Add GGA_NORMAL entries.
6003   unsigned int count = 0;
6004   for (typename Got_entry_set::iterator
6005        p = this->got_entries_.begin();
6006        p != this->got_entries_.end();
6007        ++p)
6008     {
6009       Mips_got_entry<size, big_endian>* entry = *p;
6010       if (!entry->is_for_global_symbol())
6011         continue;
6012 
6013       Mips_symbol<size>* mips_sym = entry->sym();
6014       if (mips_sym->global_got_area() != GGA_NORMAL)
6015         continue;
6016 
6017       unsigned int got_type;
6018       if (!got->multi_got())
6019         got_type = GOT_TYPE_STANDARD;
6020       else
6021         // In multi-GOT links, global symbol can be in both primary and
6022         // secondary GOT(s).  By creating custom GOT type
6023         // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
6024         // is added to secondary GOT(s).
6025         got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
6026       if (!got->add_global(mips_sym, got_type))
6027         continue;
6028 
6029       mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6030       if (got->multi_got() && this->index_ == 0)
6031         count++;
6032       if (got->multi_got() && this->index_ > 0)
6033         {
6034           if (parameters->options().output_is_position_independent()
6035               || (!parameters->doing_static_link()
6036                   && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
6037             {
6038               target->rel_dyn_section(layout)->add_global(
6039                   mips_sym, elfcpp::R_MIPS_REL32, got,
6040                   mips_sym->got_offset(got_type));
6041               got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
6042                                            elfcpp::R_MIPS_REL32, mips_sym);
6043             }
6044         }
6045     }
6046 
6047   if (!got->multi_got() || this->index_ == 0)
6048     {
6049       if (got->multi_got())
6050         {
6051           // We need to allocate space in the primary GOT for GGA_NORMAL entries
6052           // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
6053           // entries correspond to dynamic symbol indexes.
6054           while (count < non_reloc_only_global_gotno)
6055             {
6056               got->add_constant(0);
6057               ++count;
6058             }
6059         }
6060 
6061       // Add GGA_RELOC_ONLY entries.
6062       got->add_reloc_only_entries();
6063     }
6064 }
6065 
6066 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6067 
6068 template<int size, bool big_endian>
6069 void
add_reloc_only_entries(Mips_output_data_got<size,big_endian> * got)6070 Mips_got_info<size, big_endian>::add_reloc_only_entries(
6071     Mips_output_data_got<size, big_endian>* got)
6072 {
6073   for (typename Global_got_entry_set::iterator
6074        p = this->global_got_symbols_.begin();
6075        p != this->global_got_symbols_.end();
6076        ++p)
6077     {
6078       Mips_symbol<size>* mips_sym = *p;
6079       if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6080         {
6081           unsigned int got_type;
6082           if (!got->multi_got())
6083             got_type = GOT_TYPE_STANDARD;
6084           else
6085             got_type = GOT_TYPE_STANDARD_MULTIGOT;
6086           if (got->add_global(mips_sym, got_type))
6087             mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6088         }
6089     }
6090 }
6091 
6092 // Create TLS GOT entries.
6093 
6094 template<int size, bool big_endian>
6095 void
add_tls_entries(Target_mips<size,big_endian> * target,Layout * layout)6096 Mips_got_info<size, big_endian>::add_tls_entries(
6097     Target_mips<size, big_endian>* target, Layout* layout)
6098 {
6099   Mips_output_data_got<size, big_endian>* got = target->got_section();
6100   // Add local tls entries.
6101   for (typename Got_entry_set::iterator
6102        p = this->got_entries_.begin();
6103        p != this->got_entries_.end();
6104        ++p)
6105     {
6106       Mips_got_entry<size, big_endian>* entry = *p;
6107       if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6108         continue;
6109 
6110       if (entry->tls_type() == GOT_TLS_GD)
6111         {
6112           unsigned int got_type = GOT_TYPE_TLS_PAIR;
6113           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6114                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6115           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6116                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6117 
6118           if (!parameters->doing_static_link())
6119             {
6120               got->add_local_pair_with_rel(entry->object(), entry->symndx(),
6121                                            entry->shndx(), got_type,
6122                                            target->rel_dyn_section(layout),
6123                                            r_type1, entry->addend());
6124               unsigned int got_offset =
6125                 entry->object()->local_got_offset(entry->symndx(), got_type,
6126                                                   entry->addend());
6127               got->add_static_reloc(got_offset + size/8, r_type2,
6128                                     entry->object(), entry->symndx());
6129             }
6130           else
6131             {
6132               // We are doing a static link.  Mark it as belong to module 1,
6133               // the executable.
6134               unsigned int got_offset = got->add_constant(1);
6135               entry->object()->set_local_got_offset(entry->symndx(), got_type,
6136                                                     got_offset,
6137                                                     entry->addend());
6138               got->add_constant(0);
6139               got->add_static_reloc(got_offset + size/8, r_type2,
6140                                     entry->object(), entry->symndx());
6141             }
6142         }
6143       else if (entry->tls_type() == GOT_TLS_IE)
6144         {
6145           unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6146           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6147                                             : elfcpp::R_MIPS_TLS_TPREL64);
6148           if (!parameters->doing_static_link())
6149             got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
6150                                     target->rel_dyn_section(layout), r_type,
6151                                     entry->addend());
6152           else
6153             {
6154               got->add_local(entry->object(), entry->symndx(), got_type,
6155                              entry->addend());
6156               unsigned int got_offset =
6157                   entry->object()->local_got_offset(entry->symndx(), got_type,
6158                                                     entry->addend());
6159               got->add_static_reloc(got_offset, r_type, entry->object(),
6160                                     entry->symndx());
6161             }
6162         }
6163       else if (entry->tls_type() == GOT_TLS_LDM)
6164         {
6165           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6166                                             : elfcpp::R_MIPS_TLS_DTPMOD64);
6167           unsigned int got_offset;
6168           if (!parameters->doing_static_link())
6169             {
6170               got_offset = got->add_constant(0);
6171               target->rel_dyn_section(layout)->add_local(
6172                   entry->object(), 0, r_type, got, got_offset);
6173             }
6174           else
6175             // We are doing a static link.  Just mark it as belong to module 1,
6176             // the executable.
6177             got_offset = got->add_constant(1);
6178 
6179           got->add_constant(0);
6180           got->set_tls_ldm_offset(got_offset, entry->object());
6181         }
6182       else
6183         gold_unreachable();
6184     }
6185 
6186   // Add global tls entries.
6187   for (typename Got_entry_set::iterator
6188        p = this->got_entries_.begin();
6189        p != this->got_entries_.end();
6190        ++p)
6191     {
6192       Mips_got_entry<size, big_endian>* entry = *p;
6193       if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6194         continue;
6195 
6196       Mips_symbol<size>* mips_sym = entry->sym();
6197       if (entry->tls_type() == GOT_TLS_GD)
6198         {
6199           unsigned int got_type;
6200           if (!got->multi_got())
6201             got_type = GOT_TYPE_TLS_PAIR;
6202           else
6203             got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6204           unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6205                                              : elfcpp::R_MIPS_TLS_DTPMOD64);
6206           unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6207                                              : elfcpp::R_MIPS_TLS_DTPREL64);
6208           if (!parameters->doing_static_link())
6209             got->add_global_pair_with_rel(mips_sym, got_type,
6210                              target->rel_dyn_section(layout), r_type1, r_type2);
6211           else
6212             {
6213               // Add a GOT pair for for R_MIPS_TLS_GD.  The creates a pair of
6214               // GOT entries.  The first one is initialized to be 1, which is the
6215               // module index for the main executable and the second one 0.  A
6216               // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6217               // the second GOT entry and will be applied by gold.
6218               unsigned int got_offset = got->add_constant(1);
6219               mips_sym->set_got_offset(got_type, got_offset);
6220               got->add_constant(0);
6221               got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6222             }
6223         }
6224       else if (entry->tls_type() == GOT_TLS_IE)
6225         {
6226           unsigned int got_type;
6227           if (!got->multi_got())
6228             got_type = GOT_TYPE_TLS_OFFSET;
6229           else
6230             got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
6231           unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6232                                             : elfcpp::R_MIPS_TLS_TPREL64);
6233           if (!parameters->doing_static_link())
6234             got->add_global_with_rel(mips_sym, got_type,
6235                                      target->rel_dyn_section(layout), r_type);
6236           else
6237             {
6238               got->add_global(mips_sym, got_type);
6239               unsigned int got_offset = mips_sym->got_offset(got_type);
6240               got->add_static_reloc(got_offset, r_type, mips_sym);
6241             }
6242         }
6243       else
6244         gold_unreachable();
6245     }
6246 }
6247 
6248 // Decide whether the symbol needs an entry in the global part of the primary
6249 // GOT, setting global_got_area accordingly.  Count the number of global
6250 // symbols that are in the primary GOT only because they have dynamic
6251 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6252 
6253 template<int size, bool big_endian>
6254 void
count_got_symbols(Symbol_table * symtab)6255 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6256 {
6257   for (typename Global_got_entry_set::iterator
6258        p = this->global_got_symbols_.begin();
6259        p != this->global_got_symbols_.end();
6260        ++p)
6261     {
6262       Mips_symbol<size>* sym = *p;
6263       // Make a final decision about whether the symbol belongs in the
6264       // local or global GOT.  Symbols that bind locally can (and in the
6265       // case of forced-local symbols, must) live in the local GOT.
6266       // Those that are aren't in the dynamic symbol table must also
6267       // live in the local GOT.
6268 
6269       if (!sym->should_add_dynsym_entry(symtab)
6270           || (sym->got_only_for_calls()
6271               ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6272               : symbol_references_local(sym,
6273                                         sym->should_add_dynsym_entry(symtab))))
6274         // The symbol belongs in the local GOT.  We no longer need this
6275         // entry if it was only used for relocations; those relocations
6276         // will be against the null or section symbol instead.
6277         sym->set_global_got_area(GGA_NONE);
6278       else if (sym->global_got_area() == GGA_RELOC_ONLY)
6279         {
6280           ++this->reloc_only_gotno_;
6281           ++this->global_gotno_ ;
6282         }
6283     }
6284 }
6285 
6286 // Return the offset of GOT page entry for VALUE.  Initialize the entry with
6287 // VALUE if it is not initialized.
6288 
6289 template<int size, bool big_endian>
6290 unsigned int
get_got_page_offset(Mips_address value,Mips_output_data_got<size,big_endian> * got)6291 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6292     Mips_output_data_got<size, big_endian>* got)
6293 {
6294   typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6295   if (it != this->got_page_offsets_.end())
6296     return it->second;
6297 
6298   gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6299               + (size/8) * this->page_gotno_);
6300 
6301   unsigned int got_offset = this->got_page_offset_next_;
6302   this->got_page_offsets_[value] = got_offset;
6303   this->got_page_offset_next_ += size/8;
6304   got->update_got_entry(got_offset, value);
6305   return got_offset;
6306 }
6307 
6308 // Remove lazy-binding stubs for global symbols in this GOT.
6309 
6310 template<int size, bool big_endian>
6311 void
remove_lazy_stubs(Target_mips<size,big_endian> * target)6312 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6313     Target_mips<size, big_endian>* target)
6314 {
6315   for (typename Got_entry_set::iterator
6316        p = this->got_entries_.begin();
6317        p != this->got_entries_.end();
6318        ++p)
6319     {
6320       Mips_got_entry<size, big_endian>* entry = *p;
6321       if (entry->is_for_global_symbol())
6322         target->remove_lazy_stub_entry(entry->sym());
6323     }
6324 }
6325 
6326 // Count the number of GOT entries required.
6327 
6328 template<int size, bool big_endian>
6329 void
count_got_entries()6330 Mips_got_info<size, big_endian>::count_got_entries()
6331 {
6332   for (typename Got_entry_set::iterator
6333        p = this->got_entries_.begin();
6334        p != this->got_entries_.end();
6335        ++p)
6336     {
6337       this->count_got_entry(*p);
6338     }
6339 }
6340 
6341 // Count the number of GOT entries required by ENTRY.  Accumulate the result.
6342 
6343 template<int size, bool big_endian>
6344 void
count_got_entry(Mips_got_entry<size,big_endian> * entry)6345 Mips_got_info<size, big_endian>::count_got_entry(
6346     Mips_got_entry<size, big_endian>* entry)
6347 {
6348   if (entry->is_tls_entry())
6349     this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6350   else if (entry->is_for_local_symbol()
6351            || entry->sym()->global_got_area() == GGA_NONE)
6352     ++this->local_gotno_;
6353   else
6354     ++this->global_gotno_;
6355 }
6356 
6357 // Add FROM's GOT entries.
6358 
6359 template<int size, bool big_endian>
6360 void
add_got_entries(Mips_got_info<size,big_endian> * from)6361 Mips_got_info<size, big_endian>::add_got_entries(
6362     Mips_got_info<size, big_endian>* from)
6363 {
6364   for (typename Got_entry_set::iterator
6365        p = from->got_entries_.begin();
6366        p != from->got_entries_.end();
6367        ++p)
6368     {
6369       Mips_got_entry<size, big_endian>* entry = *p;
6370       if (this->got_entries_.find(entry) == this->got_entries_.end())
6371         {
6372           Mips_got_entry<size, big_endian>* entry2 =
6373             new Mips_got_entry<size, big_endian>(*entry);
6374           this->got_entries_.insert(entry2);
6375           this->count_got_entry(entry);
6376         }
6377     }
6378 }
6379 
6380 // Add FROM's GOT page entries.
6381 
6382 template<int size, bool big_endian>
6383 void
add_got_page_count(Mips_got_info<size,big_endian> * from)6384 Mips_got_info<size, big_endian>::add_got_page_count(
6385     Mips_got_info<size, big_endian>* from)
6386 {
6387   this->page_gotno_ += from->page_gotno_;
6388 }
6389 
6390 // Mips_output_data_got methods.
6391 
6392 // Lay out the GOT.  Add local, global and TLS entries.  If GOT is
6393 // larger than 64K, create multi-GOT.
6394 
6395 template<int size, bool big_endian>
6396 void
lay_out_got(Layout * layout,Symbol_table * symtab,const Input_objects * input_objects)6397 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6398     Symbol_table* symtab, const Input_objects* input_objects)
6399 {
6400   // Decide which symbols need to go in the global part of the GOT and
6401   // count the number of reloc-only GOT symbols.
6402   this->master_got_info_->count_got_symbols(symtab);
6403 
6404   // Count the number of GOT entries.
6405   this->master_got_info_->count_got_entries();
6406 
6407   unsigned int got_size = this->master_got_info_->got_size();
6408   if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6409     this->lay_out_multi_got(layout, input_objects);
6410   else
6411     {
6412       // Record that all objects use single GOT.
6413       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6414            p != input_objects->relobj_end();
6415            ++p)
6416         {
6417           Mips_relobj<size, big_endian>* object =
6418             Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6419           if (object->get_got_info() != NULL)
6420             object->set_got_info(this->master_got_info_);
6421         }
6422 
6423       this->master_got_info_->add_local_entries(this->target_, layout);
6424       this->master_got_info_->add_global_entries(this->target_, layout,
6425                                                  /*not used*/-1U);
6426       this->master_got_info_->add_tls_entries(this->target_, layout);
6427     }
6428 }
6429 
6430 // Create multi-GOT.  For every GOT, add local, global and TLS entries.
6431 
6432 template<int size, bool big_endian>
6433 void
lay_out_multi_got(Layout * layout,const Input_objects * input_objects)6434 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6435     const Input_objects* input_objects)
6436 {
6437   // Try to merge the GOTs of input objects together, as long as they
6438   // don't seem to exceed the maximum GOT size, choosing one of them
6439   // to be the primary GOT.
6440   this->merge_gots(input_objects);
6441 
6442   // Every symbol that is referenced in a dynamic relocation must be
6443   // present in the primary GOT.
6444   this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6445 
6446   // Add GOT entries.
6447   unsigned int i = 0;
6448   unsigned int offset = 0;
6449   Mips_got_info<size, big_endian>* g = this->primary_got_;
6450   do
6451     {
6452       g->set_index(i);
6453       g->set_offset(offset);
6454 
6455       g->add_local_entries(this->target_, layout);
6456       if (i == 0)
6457         g->add_global_entries(this->target_, layout,
6458                               (this->master_got_info_->global_gotno()
6459                                - this->master_got_info_->reloc_only_gotno()));
6460       else
6461         g->add_global_entries(this->target_, layout, /*not used*/-1U);
6462       g->add_tls_entries(this->target_, layout);
6463 
6464       // Forbid global symbols in every non-primary GOT from having
6465       // lazy-binding stubs.
6466       if (i > 0)
6467         g->remove_lazy_stubs(this->target_);
6468 
6469       ++i;
6470       offset += g->got_size();
6471       g = g->next();
6472     }
6473   while (g);
6474 }
6475 
6476 // Attempt to merge GOTs of different input objects.  Try to use as much as
6477 // possible of the primary GOT, since it doesn't require explicit dynamic
6478 // relocations, but don't use objects that would reference global symbols
6479 // out of the addressable range.  Failing the primary GOT, attempt to merge
6480 // with the current GOT, or finish the current GOT and then make make the new
6481 // GOT current.
6482 
6483 template<int size, bool big_endian>
6484 void
merge_gots(const Input_objects * input_objects)6485 Mips_output_data_got<size, big_endian>::merge_gots(
6486     const Input_objects* input_objects)
6487 {
6488   gold_assert(this->primary_got_ == NULL);
6489   Mips_got_info<size, big_endian>* current = NULL;
6490 
6491   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6492        p != input_objects->relobj_end();
6493        ++p)
6494     {
6495       Mips_relobj<size, big_endian>* object =
6496         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6497 
6498       Mips_got_info<size, big_endian>* g = object->get_got_info();
6499       if (g == NULL)
6500         continue;
6501 
6502       g->count_got_entries();
6503 
6504       // Work out the number of page, local and TLS entries.
6505       unsigned int estimate = this->master_got_info_->page_gotno();
6506       if (estimate > g->page_gotno())
6507         estimate = g->page_gotno();
6508       estimate += g->local_gotno() + g->tls_gotno();
6509 
6510       // We place TLS GOT entries after both locals and globals.  The globals
6511       // for the primary GOT may overflow the normal GOT size limit, so be
6512       // sure not to merge a GOT which requires TLS with the primary GOT in that
6513       // case.  This doesn't affect non-primary GOTs.
6514       estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6515                                       : g->global_gotno());
6516 
6517       unsigned int max_count =
6518         Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6519       if (estimate <= max_count)
6520         {
6521           // If we don't have a primary GOT, use it as
6522           // a starting point for the primary GOT.
6523           if (!this->primary_got_)
6524             {
6525               this->primary_got_ = g;
6526               continue;
6527             }
6528 
6529           // Try merging with the primary GOT.
6530           if (this->merge_got_with(g, object, this->primary_got_))
6531             continue;
6532         }
6533 
6534       // If we can merge with the last-created GOT, do it.
6535       if (current && this->merge_got_with(g, object, current))
6536         continue;
6537 
6538       // Well, we couldn't merge, so create a new GOT.  Don't check if it
6539       // fits; if it turns out that it doesn't, we'll get relocation
6540       // overflows anyway.
6541       g->set_next(current);
6542       current = g;
6543     }
6544 
6545   // If we do not find any suitable primary GOT, create an empty one.
6546   if (this->primary_got_ == NULL)
6547     this->primary_got_ = new Mips_got_info<size, big_endian>();
6548 
6549   // Link primary GOT with secondary GOTs.
6550   this->primary_got_->set_next(current);
6551 }
6552 
6553 // Consider merging FROM, which is OBJECT's GOT, into TO.  Return false if
6554 // this would lead to overflow, true if they were merged successfully.
6555 
6556 template<int size, bool big_endian>
6557 bool
merge_got_with(Mips_got_info<size,big_endian> * from,Mips_relobj<size,big_endian> * object,Mips_got_info<size,big_endian> * to)6558 Mips_output_data_got<size, big_endian>::merge_got_with(
6559     Mips_got_info<size, big_endian>* from,
6560     Mips_relobj<size, big_endian>* object,
6561     Mips_got_info<size, big_endian>* to)
6562 {
6563   // Work out how many page entries we would need for the combined GOT.
6564   unsigned int estimate = this->master_got_info_->page_gotno();
6565   if (estimate >= from->page_gotno() + to->page_gotno())
6566     estimate = from->page_gotno() + to->page_gotno();
6567 
6568   // Conservatively estimate how many local and TLS entries would be needed.
6569   estimate += from->local_gotno() + to->local_gotno();
6570   estimate += from->tls_gotno() + to->tls_gotno();
6571 
6572   // If we're merging with the primary got, any TLS relocations will
6573   // come after the full set of global entries.  Otherwise estimate those
6574   // conservatively as well.
6575   if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6576     estimate += this->master_got_info_->global_gotno();
6577   else
6578     estimate += from->global_gotno() + to->global_gotno();
6579 
6580   // Bail out if the combined GOT might be too big.
6581   unsigned int max_count =
6582     Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6583   if (estimate > max_count)
6584     return false;
6585 
6586   // Transfer the object's GOT information from FROM to TO.
6587   to->add_got_entries(from);
6588   to->add_got_page_count(from);
6589 
6590   // Record that OBJECT should use output GOT TO.
6591   object->set_got_info(to);
6592 
6593   return true;
6594 }
6595 
6596 // Write out the GOT.
6597 
6598 template<int size, bool big_endian>
6599 void
do_write(Output_file * of)6600 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6601 {
6602   typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6603       Mips_stubs_entry_set;
6604 
6605   // Call parent to write out GOT.
6606   Output_data_got<size, big_endian>::do_write(of);
6607 
6608   const off_t offset = this->offset();
6609   const section_size_type oview_size =
6610     convert_to_section_size_type(this->data_size());
6611   unsigned char* const oview = of->get_output_view(offset, oview_size);
6612 
6613   // Needed for fixing values of .got section.
6614   this->got_view_ = oview;
6615 
6616   // Write lazy stub addresses.
6617   for (typename Mips_stubs_entry_set::iterator
6618        p = this->master_got_info_->global_got_symbols().begin();
6619        p != this->master_got_info_->global_got_symbols().end();
6620        ++p)
6621     {
6622       Mips_symbol<size>* mips_sym = *p;
6623       if (mips_sym->has_lazy_stub())
6624         {
6625           Valtype* wv = reinterpret_cast<Valtype*>(
6626             oview + this->get_primary_got_offset(mips_sym));
6627           Valtype value =
6628             this->target_->mips_stubs_section()->stub_address(mips_sym);
6629           elfcpp::Swap<size, big_endian>::writeval(wv, value);
6630         }
6631     }
6632 
6633   // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6634   for (typename Mips_stubs_entry_set::iterator
6635        p = this->master_got_info_->global_got_symbols().begin();
6636        p != this->master_got_info_->global_got_symbols().end();
6637        ++p)
6638     {
6639       Mips_symbol<size>* mips_sym = *p;
6640       if (!this->multi_got()
6641           && (mips_sym->is_mips16() || mips_sym->is_micromips())
6642           && mips_sym->global_got_area() == GGA_NONE
6643           && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6644         {
6645           Valtype* wv = reinterpret_cast<Valtype*>(
6646             oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6647           Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6648           if (value != 0)
6649             {
6650               value |= 1;
6651               elfcpp::Swap<size, big_endian>::writeval(wv, value);
6652             }
6653         }
6654     }
6655 
6656   if (!this->secondary_got_relocs_.empty())
6657     {
6658       // Fixup for the secondary GOT R_MIPS_REL32 relocs.  For global
6659       // secondary GOT entries with non-zero initial value copy the value
6660       // to the corresponding primary GOT entry, and set the secondary GOT
6661       // entry to zero.
6662       // TODO(sasa): This is workaround.  It needs to be investigated further.
6663 
6664       for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6665         {
6666           Static_reloc& reloc(this->secondary_got_relocs_[i]);
6667           if (reloc.symbol_is_global())
6668             {
6669               Mips_symbol<size>* gsym = reloc.symbol();
6670               gold_assert(gsym != NULL);
6671 
6672               unsigned got_offset = reloc.got_offset();
6673               gold_assert(got_offset < oview_size);
6674 
6675               // Find primary GOT entry.
6676               Valtype* wv_prim = reinterpret_cast<Valtype*>(
6677                 oview + this->get_primary_got_offset(gsym));
6678 
6679               // Find secondary GOT entry.
6680               Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6681 
6682               Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6683               if (value != 0)
6684                 {
6685                   elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6686                   elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6687                   gsym->set_applied_secondary_got_fixup();
6688                 }
6689             }
6690         }
6691 
6692       of->write_output_view(offset, oview_size, oview);
6693     }
6694 
6695   // We are done if there is no fix up.
6696   if (this->static_relocs_.empty())
6697     return;
6698 
6699   Output_segment* tls_segment = this->layout_->tls_segment();
6700   gold_assert(tls_segment != NULL);
6701 
6702   for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6703     {
6704       Static_reloc& reloc(this->static_relocs_[i]);
6705 
6706       Mips_address value;
6707       if (!reloc.symbol_is_global())
6708         {
6709           Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6710           const Symbol_value<size>* psymval =
6711             object->local_symbol(reloc.index());
6712 
6713           // We are doing static linking.  Issue an error and skip this
6714           // relocation if the symbol is undefined or in a discarded_section.
6715           bool is_ordinary;
6716           unsigned int shndx = psymval->input_shndx(&is_ordinary);
6717           if ((shndx == elfcpp::SHN_UNDEF)
6718               || (is_ordinary
6719                   && shndx != elfcpp::SHN_UNDEF
6720                   && !object->is_section_included(shndx)
6721                   && !this->symbol_table_->is_section_folded(object, shndx)))
6722             {
6723               gold_error(_("undefined or discarded local symbol %u from "
6724                            " object %s in GOT"),
6725                          reloc.index(), reloc.relobj()->name().c_str());
6726               continue;
6727             }
6728 
6729           value = psymval->value(object, 0);
6730         }
6731       else
6732         {
6733           const Mips_symbol<size>* gsym = reloc.symbol();
6734           gold_assert(gsym != NULL);
6735 
6736           // We are doing static linking.  Issue an error and skip this
6737           // relocation if the symbol is undefined or in a discarded_section
6738           // unless it is a weakly_undefined symbol.
6739           if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6740               && !gsym->is_weak_undefined())
6741             {
6742               gold_error(_("undefined or discarded symbol %s in GOT"),
6743                          gsym->name());
6744               continue;
6745             }
6746 
6747           if (!gsym->is_weak_undefined())
6748             value = gsym->value();
6749           else
6750             value = 0;
6751         }
6752 
6753       unsigned got_offset = reloc.got_offset();
6754       gold_assert(got_offset < oview_size);
6755 
6756       Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6757       Valtype x;
6758 
6759       switch (reloc.r_type())
6760         {
6761         case elfcpp::R_MIPS_TLS_DTPMOD32:
6762         case elfcpp::R_MIPS_TLS_DTPMOD64:
6763           x = value;
6764           break;
6765         case elfcpp::R_MIPS_TLS_DTPREL32:
6766         case elfcpp::R_MIPS_TLS_DTPREL64:
6767           x = value - elfcpp::DTP_OFFSET;
6768           break;
6769         case elfcpp::R_MIPS_TLS_TPREL32:
6770         case elfcpp::R_MIPS_TLS_TPREL64:
6771           x = value - elfcpp::TP_OFFSET;
6772           break;
6773         default:
6774           gold_unreachable();
6775           break;
6776         }
6777 
6778       elfcpp::Swap<size, big_endian>::writeval(wv, x);
6779     }
6780 
6781   of->write_output_view(offset, oview_size, oview);
6782 }
6783 
6784 // Mips_relobj methods.
6785 
6786 // Count the local symbols.  The Mips backend needs to know if a symbol
6787 // is a MIPS16 or microMIPS function or not.  For global symbols, it is easy
6788 // because the Symbol object keeps the ELF symbol type and st_other field.
6789 // For local symbol it is harder because we cannot access this information.
6790 // So we override the do_count_local_symbol in parent and scan local symbols to
6791 // mark MIPS16 and microMIPS functions.  This is not the most efficient way but
6792 // I do not want to slow down other ports by calling a per symbol target hook
6793 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6794 
6795 template<int size, bool big_endian>
6796 void
do_count_local_symbols(Stringpool_template<char> * pool,Stringpool_template<char> * dynpool)6797 Mips_relobj<size, big_endian>::do_count_local_symbols(
6798     Stringpool_template<char>* pool,
6799     Stringpool_template<char>* dynpool)
6800 {
6801   // Ask parent to count the local symbols.
6802   Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6803   const unsigned int loccount = this->local_symbol_count();
6804   if (loccount == 0)
6805     return;
6806 
6807   // Initialize the mips16 and micromips function bit-vector.
6808   this->local_symbol_is_mips16_.resize(loccount, false);
6809   this->local_symbol_is_micromips_.resize(loccount, false);
6810 
6811   // Read the symbol table section header.
6812   const unsigned int symtab_shndx = this->symtab_shndx();
6813   elfcpp::Shdr<size, big_endian>
6814     symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6815   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6816 
6817   // Read the local symbols.
6818   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6819   gold_assert(loccount == symtabshdr.get_sh_info());
6820   off_t locsize = loccount * sym_size;
6821   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6822                                               locsize, true, true);
6823 
6824   // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6825 
6826   // Skip the first dummy symbol.
6827   psyms += sym_size;
6828   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6829     {
6830       elfcpp::Sym<size, big_endian> sym(psyms);
6831       unsigned char st_other = sym.get_st_other();
6832       this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6833       this->local_symbol_is_micromips_[i] =
6834         elfcpp::elf_st_is_micromips(st_other);
6835     }
6836 }
6837 
6838 // Read the symbol information.
6839 
6840 template<int size, bool big_endian>
6841 void
do_read_symbols(Read_symbols_data * sd)6842 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6843 {
6844   // Call parent class to read symbol information.
6845   this->base_read_symbols(sd);
6846 
6847   // If this input file is a binary file, it has no processor
6848   // specific data.
6849   Input_file::Format format = this->input_file()->format();
6850   if (format != Input_file::FORMAT_ELF)
6851     {
6852       gold_assert(format == Input_file::FORMAT_BINARY);
6853       this->merge_processor_specific_data_ = false;
6854       return;
6855     }
6856 
6857   // Read processor-specific flags in ELF file header.
6858   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6859                                             elfcpp::Elf_sizes<size>::ehdr_size,
6860                                             true, false);
6861   elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6862   this->processor_specific_flags_ = ehdr.get_e_flags();
6863 
6864   // Get the section names.
6865   const unsigned char* pnamesu = sd->section_names->data();
6866   const char* pnames = reinterpret_cast<const char*>(pnamesu);
6867 
6868   // Initialize the mips16 stub section bit-vectors.
6869   this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6870   this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6871   this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6872 
6873   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6874   const unsigned char* pshdrs = sd->section_headers->data();
6875   const unsigned char* ps = pshdrs + shdr_size;
6876   bool must_merge_processor_specific_data = false;
6877   for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6878     {
6879       elfcpp::Shdr<size, big_endian> shdr(ps);
6880 
6881       // Sometimes an object has no contents except the section name string
6882       // table and an empty symbol table with the undefined symbol.  We
6883       // don't want to merge processor-specific data from such an object.
6884       if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6885         {
6886           // Symbol table is not empty.
6887           const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
6888             elfcpp::Elf_sizes<size>::sym_size;
6889           if (shdr.get_sh_size() > sym_size)
6890             must_merge_processor_specific_data = true;
6891         }
6892       else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6893         // If this is neither an empty symbol table nor a string table,
6894         // be conservative.
6895         must_merge_processor_specific_data = true;
6896 
6897       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6898         {
6899           this->has_reginfo_section_ = true;
6900           // Read the gp value that was used to create this object.  We need the
6901           // gp value while processing relocs.  The .reginfo section is not used
6902           // in the 64-bit MIPS ELF ABI.
6903           section_offset_type section_offset = shdr.get_sh_offset();
6904           section_size_type section_size =
6905             convert_to_section_size_type(shdr.get_sh_size());
6906           const unsigned char* view =
6907              this->get_view(section_offset, section_size, true, false);
6908 
6909           this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6910 
6911           // Read the rest of .reginfo.
6912           this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6913           this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6914           this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6915           this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6916           this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6917         }
6918 
6919       if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6920         {
6921           gold_assert(this->attributes_section_data_ == NULL);
6922           section_offset_type section_offset = shdr.get_sh_offset();
6923           section_size_type section_size =
6924             convert_to_section_size_type(shdr.get_sh_size());
6925           const unsigned char* view =
6926             this->get_view(section_offset, section_size, true, false);
6927           this->attributes_section_data_ =
6928             new Attributes_section_data(view, section_size);
6929         }
6930 
6931       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6932         {
6933           gold_assert(this->abiflags_ == NULL);
6934           section_offset_type section_offset = shdr.get_sh_offset();
6935           section_size_type section_size =
6936             convert_to_section_size_type(shdr.get_sh_size());
6937           const unsigned char* view =
6938             this->get_view(section_offset, section_size, true, false);
6939           this->abiflags_ = new Mips_abiflags<big_endian>();
6940 
6941           this->abiflags_->version =
6942             elfcpp::Swap<16, big_endian>::readval(view);
6943           if (this->abiflags_->version != 0)
6944             {
6945               gold_error(_("%s: .MIPS.abiflags section has "
6946                            "unsupported version %u"),
6947                          this->name().c_str(),
6948                          this->abiflags_->version);
6949               break;
6950             }
6951           this->abiflags_->isa_level =
6952             elfcpp::Swap<8, big_endian>::readval(view + 2);
6953           this->abiflags_->isa_rev =
6954             elfcpp::Swap<8, big_endian>::readval(view + 3);
6955           this->abiflags_->gpr_size =
6956             elfcpp::Swap<8, big_endian>::readval(view + 4);
6957           this->abiflags_->cpr1_size =
6958             elfcpp::Swap<8, big_endian>::readval(view + 5);
6959           this->abiflags_->cpr2_size =
6960             elfcpp::Swap<8, big_endian>::readval(view + 6);
6961           this->abiflags_->fp_abi =
6962             elfcpp::Swap<8, big_endian>::readval(view + 7);
6963           this->abiflags_->isa_ext =
6964             elfcpp::Swap<32, big_endian>::readval(view + 8);
6965           this->abiflags_->ases =
6966             elfcpp::Swap<32, big_endian>::readval(view + 12);
6967           this->abiflags_->flags1 =
6968             elfcpp::Swap<32, big_endian>::readval(view + 16);
6969           this->abiflags_->flags2 =
6970             elfcpp::Swap<32, big_endian>::readval(view + 20);
6971         }
6972 
6973       // In the 64-bit ABI, .MIPS.options section holds register information.
6974       // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6975       // starts with this header:
6976       //
6977       // typedef struct
6978       // {
6979       //   // Type of option.
6980       //   unsigned char kind[1];
6981       //   // Size of option descriptor, including header.
6982       //   unsigned char size[1];
6983       //   // Section index of affected section, or 0 for global option.
6984       //   unsigned char section[2];
6985       //   // Information specific to this kind of option.
6986       //   unsigned char info[4];
6987       // };
6988       //
6989       // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6990       // the gp value based on what we find.  We may see both SHT_MIPS_REGINFO
6991       // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6992 
6993       if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6994         {
6995           section_offset_type section_offset = shdr.get_sh_offset();
6996           section_size_type section_size =
6997             convert_to_section_size_type(shdr.get_sh_size());
6998           const unsigned char* view =
6999              this->get_view(section_offset, section_size, true, false);
7000           const unsigned char* end = view + section_size;
7001 
7002           while (view + 8 <= end)
7003             {
7004               unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
7005               unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
7006               if (sz < 8)
7007                 {
7008                   gold_error(_("%s: Warning: bad `%s' option size %u smaller "
7009                                "than its header"),
7010                              this->name().c_str(),
7011                              this->mips_elf_options_section_name(), sz);
7012                   break;
7013                 }
7014 
7015               if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
7016                 {
7017                   // In the 64 bit ABI, an ODK_REGINFO option is the following
7018                   // structure.  The info field of the options header is not
7019                   // used.
7020                   //
7021                   // typedef struct
7022                   // {
7023                   //   // Mask of general purpose registers used.
7024                   //   unsigned char ri_gprmask[4];
7025                   //   // Padding.
7026                   //   unsigned char ri_pad[4];
7027                   //   // Mask of co-processor registers used.
7028                   //   unsigned char ri_cprmask[4][4];
7029                   //   // GP register value for this object file.
7030                   //   unsigned char ri_gp_value[8];
7031                   // };
7032 
7033                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7034                                                                       + 32);
7035                 }
7036               else if (kind == elfcpp::ODK_REGINFO)
7037                 {
7038                   // In the 32 bit ABI, an ODK_REGINFO option is the following
7039                   // structure.  The info field of the options header is not
7040                   // used.  The same structure is used in .reginfo section.
7041                   //
7042                   // typedef struct
7043                   // {
7044                   //   unsigned char ri_gprmask[4];
7045                   //   unsigned char ri_cprmask[4][4];
7046                   //   unsigned char ri_gp_value[4];
7047                   // };
7048 
7049                   this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7050                                                                       + 28);
7051                 }
7052               view += sz;
7053             }
7054         }
7055 
7056       const char* name = pnames + shdr.get_sh_name();
7057       this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
7058       this->section_is_mips16_call_stub_[i] =
7059         is_prefix_of(".mips16.call.", name);
7060       this->section_is_mips16_call_fp_stub_[i] =
7061         is_prefix_of(".mips16.call.fp.", name);
7062 
7063       if (strcmp(name, ".pdr") == 0)
7064         {
7065           gold_assert(this->pdr_shndx_ == -1U);
7066           this->pdr_shndx_ = i;
7067         }
7068     }
7069 
7070   // This is rare.
7071   if (!must_merge_processor_specific_data)
7072     this->merge_processor_specific_data_ = false;
7073 }
7074 
7075 // Discard MIPS16 stub secions that are not needed.
7076 
7077 template<int size, bool big_endian>
7078 void
discard_mips16_stub_sections(Symbol_table * symtab)7079 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7080 {
7081   for (typename Mips16_stubs_int_map::const_iterator
7082        it = this->mips16_stub_sections_.begin();
7083        it != this->mips16_stub_sections_.end(); ++it)
7084     {
7085       Mips16_stub_section<size, big_endian>* stub_section = it->second;
7086       if (!stub_section->is_target_found())
7087         {
7088           gold_error(_("no relocation found in mips16 stub section '%s'"),
7089                      stub_section->object()
7090                        ->section_name(stub_section->shndx()).c_str());
7091         }
7092 
7093       bool discard = false;
7094       if (stub_section->is_for_local_function())
7095         {
7096           if (stub_section->is_fn_stub())
7097             {
7098               // This stub is for a local symbol.  This stub will only
7099               // be needed if there is some relocation in this object,
7100               // other than a 16 bit function call, which refers to this
7101               // symbol.
7102               if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7103                 discard = true;
7104               else
7105                 this->add_local_mips16_fn_stub(stub_section);
7106             }
7107           else
7108             {
7109               // This stub is for a local symbol.  This stub will only
7110               // be needed if there is some relocation (R_MIPS16_26) in
7111               // this object that refers to this symbol.
7112               gold_assert(stub_section->is_call_stub()
7113                           || stub_section->is_call_fp_stub());
7114               if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
7115                 discard = true;
7116               else
7117                 this->add_local_mips16_call_stub(stub_section);
7118             }
7119         }
7120       else
7121         {
7122           Mips_symbol<size>* gsym = stub_section->gsym();
7123           if (stub_section->is_fn_stub())
7124             {
7125               if (gsym->has_mips16_fn_stub())
7126                 // We already have a stub for this function.
7127                 discard = true;
7128               else
7129                 {
7130                   gsym->set_mips16_fn_stub(stub_section);
7131                   if (gsym->should_add_dynsym_entry(symtab))
7132                     {
7133                       // If we have a MIPS16 function with a stub, the
7134                       // dynamic symbol must refer to the stub, since only
7135                       // the stub uses the standard calling conventions.
7136                       gsym->set_need_fn_stub();
7137                       if (gsym->is_from_dynobj())
7138                         gsym->set_needs_dynsym_value();
7139                     }
7140                 }
7141               if (!gsym->need_fn_stub())
7142                 discard = true;
7143             }
7144           else if (stub_section->is_call_stub())
7145             {
7146               if (gsym->is_mips16())
7147                 // We don't need the call_stub; this is a 16 bit
7148                 // function, so calls from other 16 bit functions are
7149                 // OK.
7150                 discard = true;
7151               else if (gsym->has_mips16_call_stub())
7152                 // We already have a stub for this function.
7153                 discard = true;
7154               else
7155                 gsym->set_mips16_call_stub(stub_section);
7156             }
7157           else
7158             {
7159               gold_assert(stub_section->is_call_fp_stub());
7160               if (gsym->is_mips16())
7161                 // We don't need the call_stub; this is a 16 bit
7162                 // function, so calls from other 16 bit functions are
7163                 // OK.
7164                 discard = true;
7165               else if (gsym->has_mips16_call_fp_stub())
7166                 // We already have a stub for this function.
7167                 discard = true;
7168               else
7169                 gsym->set_mips16_call_fp_stub(stub_section);
7170             }
7171         }
7172       if (discard)
7173         this->set_output_section(stub_section->shndx(), NULL);
7174    }
7175 }
7176 
7177 // Mips_output_data_la25_stub methods.
7178 
7179 // Template for standard LA25 stub.
7180 template<int size, bool big_endian>
7181 const uint32_t
7182 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7183 {
7184   0x3c190000,           // lui $25,%hi(func)
7185   0x08000000,           // j func
7186   0x27390000,           // add $25,$25,%lo(func)
7187   0x00000000            // nop
7188 };
7189 
7190 // Template for microMIPS LA25 stub.
7191 template<int size, bool big_endian>
7192 const uint32_t
7193 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7194 {
7195   0x41b9, 0x0000,       // lui t9,%hi(func)
7196   0xd400, 0x0000,       // j func
7197   0x3339, 0x0000,       // addiu t9,t9,%lo(func)
7198   0x0000, 0x0000        // nop
7199 };
7200 
7201 // Create la25 stub for a symbol.
7202 
7203 template<int size, bool big_endian>
7204 void
create_la25_stub(Symbol_table * symtab,Target_mips<size,big_endian> * target,Mips_symbol<size> * gsym)7205 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7206     Symbol_table* symtab, Target_mips<size, big_endian>* target,
7207     Mips_symbol<size>* gsym)
7208 {
7209   if (!gsym->has_la25_stub())
7210     {
7211       gsym->set_la25_stub_offset(this->symbols_.size() * 16);
7212       this->symbols_.push_back(gsym);
7213       this->create_stub_symbol(gsym, symtab, target, 16);
7214     }
7215 }
7216 
7217 // Create a symbol for SYM stub's value and size, to help make the disassembly
7218 // easier to read.
7219 
7220 template<int size, bool big_endian>
7221 void
create_stub_symbol(Mips_symbol<size> * sym,Symbol_table * symtab,Target_mips<size,big_endian> * target,uint64_t symsize)7222 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7223     Mips_symbol<size>* sym, Symbol_table* symtab,
7224     Target_mips<size, big_endian>* target, uint64_t symsize)
7225 {
7226   std::string name(".pic.");
7227   name += sym->name();
7228 
7229   unsigned int offset = sym->la25_stub_offset();
7230   if (sym->is_micromips())
7231     offset |= 1;
7232 
7233   // Make it a local function.
7234   Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7235                                       Symbol_table::PREDEFINED,
7236                                       target->la25_stub_section(),
7237                                       offset, symsize, elfcpp::STT_FUNC,
7238                                       elfcpp::STB_LOCAL,
7239                                       elfcpp::STV_DEFAULT, 0,
7240                                       false, false);
7241   new_sym->set_is_forced_local();
7242 }
7243 
7244 // Write out la25 stubs.  This uses the hand-coded instructions above,
7245 // and adjusts them as needed.
7246 
7247 template<int size, bool big_endian>
7248 void
do_write(Output_file * of)7249 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7250 {
7251   const off_t offset = this->offset();
7252   const section_size_type oview_size =
7253     convert_to_section_size_type(this->data_size());
7254   unsigned char* const oview = of->get_output_view(offset, oview_size);
7255 
7256   for (typename std::vector<Mips_symbol<size>*>::iterator
7257        p = this->symbols_.begin();
7258        p != this->symbols_.end();
7259        ++p)
7260     {
7261       Mips_symbol<size>* sym = *p;
7262       unsigned char* pov = oview + sym->la25_stub_offset();
7263 
7264       Mips_address target = sym->value();
7265       if (!sym->is_micromips())
7266         {
7267           elfcpp::Swap<32, big_endian>::writeval(pov,
7268               la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7269           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7270               la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7271           elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7272               la25_stub_entry[2] | (target & 0xffff));
7273           elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7274         }
7275       else
7276         {
7277           target |= 1;
7278           // First stub instruction.  Paste high 16-bits of the target.
7279           elfcpp::Swap<16, big_endian>::writeval(pov,
7280                                                  la25_stub_micromips_entry[0]);
7281           elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7282               ((target + 0x8000) >> 16) & 0xffff);
7283           // Second stub instruction.  Paste low 26-bits of the target, shifted
7284           // right by 1.
7285           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7286               la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7287           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7288               la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7289           // Third stub instruction.  Paste low 16-bits of the target.
7290           elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7291                                                  la25_stub_micromips_entry[4]);
7292           elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7293           // Fourth stub instruction.
7294           elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7295                                                  la25_stub_micromips_entry[6]);
7296           elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7297                                                  la25_stub_micromips_entry[7]);
7298         }
7299     }
7300 
7301   of->write_output_view(offset, oview_size, oview);
7302 }
7303 
7304 // Mips_output_data_plt methods.
7305 
7306 // The format of the first PLT entry in an O32 executable.
7307 template<int size, bool big_endian>
7308 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7309 {
7310   0x3c1c0000,         // lui $28, %hi(&GOTPLT[0])
7311   0x8f990000,         // lw $25, %lo(&GOTPLT[0])($28)
7312   0x279c0000,         // addiu $28, $28, %lo(&GOTPLT[0])
7313   0x031cc023,         // subu $24, $24, $28
7314   0x03e07825,         // or $15, $31, zero
7315   0x0018c082,         // srl $24, $24, 2
7316   0x0320f809,         // jalr $25
7317   0x2718fffe          // subu $24, $24, 2
7318 };
7319 
7320 // The format of the first PLT entry in an N32 executable.  Different
7321 // because gp ($28) is not available; we use t2 ($14) instead.
7322 template<int size, bool big_endian>
7323 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7324 {
7325   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7326   0x8dd90000,         // lw $25, %lo(&GOTPLT[0])($14)
7327   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7328   0x030ec023,         // subu $24, $24, $14
7329   0x03e07825,         // or $15, $31, zero
7330   0x0018c082,         // srl $24, $24, 2
7331   0x0320f809,         // jalr $25
7332   0x2718fffe          // subu $24, $24, 2
7333 };
7334 
7335 // The format of the first PLT entry in an N64 executable.  Different
7336 // from N32 because of the increased size of GOT entries.
7337 template<int size, bool big_endian>
7338 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7339 {
7340   0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
7341   0xddd90000,         // ld $25, %lo(&GOTPLT[0])($14)
7342   0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
7343   0x030ec023,         // subu $24, $24, $14
7344   0x03e07825,         // or $15, $31, zero
7345   0x0018c0c2,         // srl $24, $24, 3
7346   0x0320f809,         // jalr $25
7347   0x2718fffe          // subu $24, $24, 2
7348 };
7349 
7350 // The format of the microMIPS first PLT entry in an O32 executable.
7351 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7352 // of the GOTPLT entry handled, so this stub may only be used when
7353 // all the subsequent PLT entries are microMIPS code too.
7354 //
7355 // The trailing NOP is for alignment and correct disassembly only.
7356 template<int size, bool big_endian>
7357 const uint32_t Mips_output_data_plt<size, big_endian>::
7358 plt0_entry_micromips_o32[] =
7359 {
7360   0x7980, 0x0000,      // addiupc $3, (&GOTPLT[0]) - .
7361   0xff23, 0x0000,      // lw $25, 0($3)
7362   0x0535,              // subu $2, $2, $3
7363   0x2525,              // srl $2, $2, 2
7364   0x3302, 0xfffe,      // subu $24, $2, 2
7365   0x0dff,              // move $15, $31
7366   0x45f9,              // jalrs $25
7367   0x0f83,              // move $28, $3
7368   0x0c00               // nop
7369 };
7370 
7371 // The format of the microMIPS first PLT entry in an O32 executable
7372 // in the insn32 mode.
7373 template<int size, bool big_endian>
7374 const uint32_t Mips_output_data_plt<size, big_endian>::
7375 plt0_entry_micromips32_o32[] =
7376 {
7377   0x41bc, 0x0000,      // lui $28, %hi(&GOTPLT[0])
7378   0xff3c, 0x0000,      // lw $25, %lo(&GOTPLT[0])($28)
7379   0x339c, 0x0000,      // addiu $28, $28, %lo(&GOTPLT[0])
7380   0x0398, 0xc1d0,      // subu $24, $24, $28
7381   0x001f, 0x7a90,      // or $15, $31, zero
7382   0x0318, 0x1040,      // srl $24, $24, 2
7383   0x03f9, 0x0f3c,      // jalr $25
7384   0x3318, 0xfffe       // subu $24, $24, 2
7385 };
7386 
7387 // The format of subsequent standard entries in the PLT.
7388 template<int size, bool big_endian>
7389 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7390 {
7391   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7392   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7393   0x03200008,           // jr $25
7394   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7395 };
7396 
7397 // The format of subsequent R6 PLT entries.
7398 template<int size, bool big_endian>
7399 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7400 {
7401   0x3c0f0000,           // lui $15, %hi(.got.plt entry)
7402   0x01f90000,           // l[wd] $25, %lo(.got.plt entry)($15)
7403   0x03200009,           // jr $25
7404   0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
7405 };
7406 
7407 // The format of subsequent MIPS16 o32 PLT entries.  We use v1 ($3) as a
7408 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7409 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7410 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7411 // target function address in register v0.
7412 template<int size, bool big_endian>
7413 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7414 {
7415   0xb303,              // lw $3, 12($pc)
7416   0x651b,              // move $24, $3
7417   0x9b60,              // lw $3, 0($3)
7418   0xeb00,              // jr $3
7419   0x653b,              // move $25, $3
7420   0x6500,              // nop
7421   0x0000, 0x0000       // .word (.got.plt entry)
7422 };
7423 
7424 // The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
7425 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7426 template<int size, bool big_endian>
7427 const uint32_t Mips_output_data_plt<size, big_endian>::
7428 plt_entry_micromips_o32[] =
7429 {
7430   0x7900, 0x0000,      // addiupc $2, (.got.plt entry) - .
7431   0xff22, 0x0000,      // lw $25, 0($2)
7432   0x4599,              // jr $25
7433   0x0f02               // move $24, $2
7434 };
7435 
7436 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7437 template<int size, bool big_endian>
7438 const uint32_t Mips_output_data_plt<size, big_endian>::
7439 plt_entry_micromips32_o32[] =
7440 {
7441   0x41af, 0x0000,      // lui $15, %hi(.got.plt entry)
7442   0xff2f, 0x0000,      // lw $25, %lo(.got.plt entry)($15)
7443   0x0019, 0x0f3c,      // jr $25
7444   0x330f, 0x0000       // addiu $24, $15, %lo(.got.plt entry)
7445 };
7446 
7447 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7448 
7449 template<int size, bool big_endian>
7450 void
add_entry(Mips_symbol<size> * gsym,unsigned int r_type)7451 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7452                                                   unsigned int r_type)
7453 {
7454   gold_assert(!gsym->has_plt_offset());
7455 
7456   // Final PLT offset for a symbol will be set in method set_plt_offsets().
7457   gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7458                        + sizeof(plt0_entry_o32));
7459   this->symbols_.push_back(gsym);
7460 
7461   // Record whether the relocation requires a standard MIPS
7462   // or a compressed code entry.
7463   if (jal_reloc(r_type))
7464    {
7465      if (r_type == elfcpp::R_MIPS_26)
7466        gsym->set_needs_mips_plt(true);
7467      else
7468        gsym->set_needs_comp_plt(true);
7469    }
7470 
7471   section_offset_type got_offset = this->got_plt_->current_data_size();
7472 
7473   // Every PLT entry needs a GOT entry which points back to the PLT
7474   // entry (this will be changed by the dynamic linker, normally
7475   // lazily when the function is called).
7476   this->got_plt_->set_current_data_size(got_offset + size/8);
7477 
7478   gsym->set_needs_dynsym_entry();
7479   this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7480                          got_offset);
7481 }
7482 
7483 // Set final PLT offsets.  For each symbol, determine whether standard or
7484 // compressed (MIPS16 or microMIPS) PLT entry is used.
7485 
7486 template<int size, bool big_endian>
7487 void
set_plt_offsets()7488 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7489 {
7490   // The sizes of individual PLT entries.
7491   unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7492   unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7493                                       ? this->compressed_plt_entry_size() : 0);
7494 
7495   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7496        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7497     {
7498       Mips_symbol<size>* mips_sym = *p;
7499 
7500       // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7501       // so always use a standard entry there.
7502       //
7503       // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7504       // all MIPS16 calls will go via that stub, and there is no benefit
7505       // to having a MIPS16 entry.  And in the case of call_stub a
7506       // standard entry actually has to be used as the stub ends with a J
7507       // instruction.
7508       if (this->target_->is_output_newabi()
7509           || mips_sym->has_mips16_call_stub()
7510           || mips_sym->has_mips16_call_fp_stub())
7511         {
7512           mips_sym->set_needs_mips_plt(true);
7513           mips_sym->set_needs_comp_plt(false);
7514         }
7515 
7516       // Otherwise, if there are no direct calls to the function, we
7517       // have a free choice of whether to use standard or compressed
7518       // entries.  Prefer microMIPS entries if the object is known to
7519       // contain microMIPS code, so that it becomes possible to create
7520       // pure microMIPS binaries.  Prefer standard entries otherwise,
7521       // because MIPS16 ones are no smaller and are usually slower.
7522       if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7523         {
7524           if (this->target_->is_output_micromips())
7525             mips_sym->set_needs_comp_plt(true);
7526           else
7527             mips_sym->set_needs_mips_plt(true);
7528         }
7529 
7530       if (mips_sym->needs_mips_plt())
7531         {
7532           mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7533           this->plt_mips_offset_ += plt_mips_entry_size;
7534         }
7535       if (mips_sym->needs_comp_plt())
7536         {
7537           mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7538           this->plt_comp_offset_ += plt_comp_entry_size;
7539         }
7540     }
7541 
7542     // Figure out the size of the PLT header if we know that we are using it.
7543     if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7544       this->plt_header_size_ = this->get_plt_header_size();
7545 }
7546 
7547 // Write out the PLT.  This uses the hand-coded instructions above,
7548 // and adjusts them as needed.
7549 
7550 template<int size, bool big_endian>
7551 void
do_write(Output_file * of)7552 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7553 {
7554   const off_t offset = this->offset();
7555   const section_size_type oview_size =
7556     convert_to_section_size_type(this->data_size());
7557   unsigned char* const oview = of->get_output_view(offset, oview_size);
7558 
7559   const off_t gotplt_file_offset = this->got_plt_->offset();
7560   const section_size_type gotplt_size =
7561     convert_to_section_size_type(this->got_plt_->data_size());
7562   unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7563                                                          gotplt_size);
7564   unsigned char* pov = oview;
7565 
7566   Mips_address plt_address = this->address();
7567 
7568   // Calculate the address of .got.plt.
7569   Mips_address gotplt_addr = this->got_plt_->address();
7570   Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7571   Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7572 
7573   // The PLT sequence is not safe for N64 if .got.plt's address can
7574   // not be loaded in two instructions.
7575   gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7576               || ~(gotplt_addr | 0x7fffffff) == 0);
7577 
7578   // Write the PLT header.
7579   const uint32_t* plt0_entry = this->get_plt_header_entry();
7580   if (plt0_entry == plt0_entry_micromips_o32)
7581     {
7582       // Write microMIPS PLT header.
7583       gold_assert(gotplt_addr % 4 == 0);
7584 
7585       Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7586 
7587       // ADDIUPC has a span of +/-16MB, check we're in range.
7588       if (gotpc_offset + 0x1000000 >= 0x2000000)
7589        {
7590          gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7591                     "ADDIUPC"), (long)gotpc_offset);
7592          return;
7593        }
7594 
7595       elfcpp::Swap<16, big_endian>::writeval(pov,
7596                  plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7597       elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7598                                              (gotpc_offset >> 2) & 0xffff);
7599       pov += 4;
7600       for (unsigned int i = 2;
7601            i < (sizeof(plt0_entry_micromips_o32)
7602                 / sizeof(plt0_entry_micromips_o32[0]));
7603            i++)
7604         {
7605           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7606           pov += 2;
7607         }
7608     }
7609   else if (plt0_entry == plt0_entry_micromips32_o32)
7610     {
7611       // Write microMIPS PLT header in insn32 mode.
7612       elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7613       elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7614       elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7615       elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7616       elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7617       elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7618       pov += 12;
7619       for (unsigned int i = 6;
7620            i < (sizeof(plt0_entry_micromips32_o32)
7621                 / sizeof(plt0_entry_micromips32_o32[0]));
7622            i++)
7623         {
7624           elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7625           pov += 2;
7626         }
7627     }
7628   else
7629     {
7630       // Write standard PLT header.
7631       elfcpp::Swap<32, big_endian>::writeval(pov,
7632                                              plt0_entry[0] | gotplt_addr_high);
7633       elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7634                                              plt0_entry[1] | gotplt_addr_low);
7635       elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7636                                              plt0_entry[2] | gotplt_addr_low);
7637       pov += 12;
7638       for (int i = 3; i < 8; i++)
7639         {
7640           elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7641           pov += 4;
7642         }
7643     }
7644 
7645 
7646   unsigned char* gotplt_pov = gotplt_view;
7647   unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7648 
7649   // The first two entries in .got.plt are reserved.
7650   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7651   elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7652 
7653   unsigned int gotplt_offset = 2 * got_entry_size;
7654   gotplt_pov += 2 * got_entry_size;
7655 
7656   // Calculate the address of the PLT header.
7657   Mips_address header_address = (plt_address
7658                                  + (this->is_plt_header_compressed() ? 1 : 0));
7659 
7660   // Initialize compressed PLT area view.
7661   unsigned char* pov2 = pov + this->plt_mips_offset_;
7662 
7663   // Write the PLT entries.
7664   for (typename std::vector<Mips_symbol<size>*>::const_iterator
7665        p = this->symbols_.begin();
7666        p != this->symbols_.end();
7667        ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7668     {
7669       Mips_symbol<size>* mips_sym = *p;
7670 
7671       // Calculate the address of the .got.plt entry.
7672       uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7673       uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7674                                        & 0xffff);
7675       uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7676 
7677       // Initially point the .got.plt entry at the PLT header.
7678       if (this->target_->is_output_n64())
7679         elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7680       else
7681         elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7682 
7683       // Now handle the PLT itself.  First the standard entry.
7684       if (mips_sym->has_mips_plt_offset())
7685         {
7686           // Pick the load opcode (LW or LD).
7687           uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7688                                                          : 0x8c000000;
7689 
7690           const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7691                                                                 : plt_entry;
7692 
7693           // Fill in the PLT entry itself.
7694           elfcpp::Swap<32, big_endian>::writeval(pov,
7695               entry[0] | gotplt_entry_addr_hi);
7696           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7697               entry[1] | gotplt_entry_addr_lo | load);
7698           elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
7699           elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7700               entry[3] | gotplt_entry_addr_lo);
7701           pov += 16;
7702         }
7703 
7704       // Now the compressed entry.  They come after any standard ones.
7705       if (mips_sym->has_comp_plt_offset())
7706         {
7707           if (!this->target_->is_output_micromips())
7708             {
7709               // Write MIPS16 PLT entry.
7710               const uint32_t* plt_entry = plt_entry_mips16_o32;
7711 
7712               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7713               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7714               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7715               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7716               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7717               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7718               elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7719                                                      gotplt_entry_addr);
7720               pov2 += 16;
7721             }
7722           else if (this->target_->use_32bit_micromips_instructions())
7723             {
7724               // Write microMIPS PLT entry in insn32 mode.
7725               const uint32_t* plt_entry = plt_entry_micromips32_o32;
7726 
7727               elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7728               elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7729                                                      gotplt_entry_addr_hi);
7730               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7731               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7732                                                      gotplt_entry_addr_lo);
7733               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7734               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7735               elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7736               elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7737                                                      gotplt_entry_addr_lo);
7738               pov2 += 16;
7739             }
7740           else
7741             {
7742               // Write microMIPS PLT entry.
7743               const uint32_t* plt_entry = plt_entry_micromips_o32;
7744 
7745               gold_assert(gotplt_entry_addr % 4 == 0);
7746 
7747               Mips_address loc_address = plt_address + pov2 - oview;
7748               int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7749 
7750               // ADDIUPC has a span of +/-16MB, check we're in range.
7751               if (gotpc_offset + 0x1000000 >= 0x2000000)
7752                 {
7753                   gold_error(_(".got.plt offset of %ld from .plt beyond the "
7754                              "range of ADDIUPC"), (long)gotpc_offset);
7755                   return;
7756                 }
7757 
7758               elfcpp::Swap<16, big_endian>::writeval(pov2,
7759                           plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7760               elfcpp::Swap<16, big_endian>::writeval(
7761                   pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7762               elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7763               elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7764               elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7765               elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7766               pov2 += 12;
7767             }
7768         }
7769     }
7770 
7771   // Check the number of bytes written for standard entries.
7772   gold_assert(static_cast<section_size_type>(
7773       pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7774   // Check the number of bytes written for compressed entries.
7775   gold_assert((static_cast<section_size_type>(pov2 - pov)
7776                == this->plt_comp_offset_));
7777   // Check the total number of bytes written.
7778   gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7779 
7780   gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7781               == gotplt_size);
7782 
7783   of->write_output_view(offset, oview_size, oview);
7784   of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7785 }
7786 
7787 // Mips_output_data_mips_stubs methods.
7788 
7789 // The format of the lazy binding stub when dynamic symbol count is less than
7790 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7791 template<int size, bool big_endian>
7792 const uint32_t
7793 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7794 {
7795   0x8f998010,         // lw t9,0x8010(gp)
7796   0x03e07825,         // or t7,ra,zero
7797   0x0320f809,         // jalr t9,ra
7798   0x24180000          // addiu t8,zero,DYN_INDEX sign extended
7799 };
7800 
7801 // The format of the lazy binding stub when dynamic symbol count is less than
7802 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7803 template<int size, bool big_endian>
7804 const uint32_t
7805 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7806 {
7807   0xdf998010,         // ld t9,0x8010(gp)
7808   0x03e07825,         // or t7,ra,zero
7809   0x0320f809,         // jalr t9,ra
7810   0x64180000          // daddiu t8,zero,DYN_INDEX sign extended
7811 };
7812 
7813 // The format of the lazy binding stub when dynamic symbol count is less than
7814 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7815 template<int size, bool big_endian>
7816 const uint32_t
7817 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7818 {
7819   0x8f998010,         // lw t9,0x8010(gp)
7820   0x03e07825,         // or t7,ra,zero
7821   0x0320f809,         // jalr t9,ra
7822   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7823 };
7824 
7825 // The format of the lazy binding stub when dynamic symbol count is less than
7826 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7827 template<int size, bool big_endian>
7828 const uint32_t
7829 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7830 {
7831   0xdf998010,         // ld t9,0x8010(gp)
7832   0x03e07825,         // or t7,ra,zero
7833   0x0320f809,         // jalr t9,ra
7834   0x34180000          // ori t8,zero,DYN_INDEX unsigned
7835 };
7836 
7837 // The format of the lazy binding stub when dynamic symbol count is greater than
7838 // 64K, and ABI is not N64.
7839 template<int size, bool big_endian>
7840 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7841 {
7842   0x8f998010,         // lw t9,0x8010(gp)
7843   0x03e07825,         // or t7,ra,zero
7844   0x3c180000,         // lui t8,DYN_INDEX
7845   0x0320f809,         // jalr t9,ra
7846   0x37180000          // ori t8,t8,DYN_INDEX
7847 };
7848 
7849 // The format of the lazy binding stub when dynamic symbol count is greater than
7850 // 64K, and ABI is N64.
7851 template<int size, bool big_endian>
7852 const uint32_t
7853 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7854 {
7855   0xdf998010,         // ld t9,0x8010(gp)
7856   0x03e07825,         // or t7,ra,zero
7857   0x3c180000,         // lui t8,DYN_INDEX
7858   0x0320f809,         // jalr t9,ra
7859   0x37180000          // ori t8,t8,DYN_INDEX
7860 };
7861 
7862 // microMIPS stubs.
7863 
7864 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7865 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7866 template<int size, bool big_endian>
7867 const uint32_t
7868 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7869 {
7870   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7871   0x0dff,             // move t7,ra
7872   0x45d9,             // jalr t9
7873   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7874 };
7875 
7876 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7877 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7878 template<int size, bool big_endian>
7879 const uint32_t
7880 Mips_output_data_mips_stubs<size, big_endian>::
7881 lazy_stub_micromips_normal_1_n64[] =
7882 {
7883   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7884   0x0dff,             // move t7,ra
7885   0x45d9,             // jalr t9
7886   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7887 };
7888 
7889 // The format of the microMIPS lazy binding stub when dynamic symbol
7890 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7891 // and ABI is not N64.
7892 template<int size, bool big_endian>
7893 const uint32_t
7894 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7895 {
7896   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7897   0x0dff,             // move t7,ra
7898   0x45d9,             // jalr t9
7899   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7900 };
7901 
7902 // The format of the microMIPS lazy binding stub when dynamic symbol
7903 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7904 // and ABI is N64.
7905 template<int size, bool big_endian>
7906 const uint32_t
7907 Mips_output_data_mips_stubs<size, big_endian>::
7908 lazy_stub_micromips_normal_2_n64[] =
7909 {
7910   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7911   0x0dff,             // move t7,ra
7912   0x45d9,             // jalr t9
7913   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7914 };
7915 
7916 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7917 // greater than 64K, and ABI is not N64.
7918 template<int size, bool big_endian>
7919 const uint32_t
7920 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7921 {
7922   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7923   0x0dff,             // move t7,ra
7924   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7925   0x45d9,             // jalr t9
7926   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7927 };
7928 
7929 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7930 // greater than 64K, and ABI is N64.
7931 template<int size, bool big_endian>
7932 const uint32_t
7933 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7934 {
7935   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7936   0x0dff,             // move t7,ra
7937   0x41b8, 0x0000,     // lui t8,DYN_INDEX
7938   0x45d9,             // jalr t9
7939   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
7940 };
7941 
7942 // 32-bit microMIPS stubs.
7943 
7944 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7945 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7946 // can use only 32-bit instructions.
7947 template<int size, bool big_endian>
7948 const uint32_t
7949 Mips_output_data_mips_stubs<size, big_endian>::
7950 lazy_stub_micromips32_normal_1[] =
7951 {
7952   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7953   0x001f, 0x7a90,     // or t7,ra,zero
7954   0x03f9, 0x0f3c,     // jalr ra,t9
7955   0x3300, 0x0000      // addiu t8,zero,DYN_INDEX sign extended
7956 };
7957 
7958 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7959 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7960 // use only 32-bit instructions.
7961 template<int size, bool big_endian>
7962 const uint32_t
7963 Mips_output_data_mips_stubs<size, big_endian>::
7964 lazy_stub_micromips32_normal_1_n64[] =
7965 {
7966   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7967   0x001f, 0x7a90,     // or t7,ra,zero
7968   0x03f9, 0x0f3c,     // jalr ra,t9
7969   0x5f00, 0x0000      // daddiu t8,zero,DYN_INDEX sign extended
7970 };
7971 
7972 // The format of the microMIPS lazy binding stub when dynamic symbol
7973 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7974 // ABI is not N64, and we can use only 32-bit instructions.
7975 template<int size, bool big_endian>
7976 const uint32_t
7977 Mips_output_data_mips_stubs<size, big_endian>::
7978 lazy_stub_micromips32_normal_2[] =
7979 {
7980   0xff3c, 0x8010,     // lw t9,0x8010(gp)
7981   0x001f, 0x7a90,     // or t7,ra,zero
7982   0x03f9, 0x0f3c,     // jalr ra,t9
7983   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7984 };
7985 
7986 // The format of the microMIPS lazy binding stub when dynamic symbol
7987 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7988 // ABI is N64, and we can use only 32-bit instructions.
7989 template<int size, bool big_endian>
7990 const uint32_t
7991 Mips_output_data_mips_stubs<size, big_endian>::
7992 lazy_stub_micromips32_normal_2_n64[] =
7993 {
7994   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
7995   0x001f, 0x7a90,     // or t7,ra,zero
7996   0x03f9, 0x0f3c,     // jalr ra,t9
7997   0x5300, 0x0000      // ori t8,zero,DYN_INDEX unsigned
7998 };
7999 
8000 // The format of the microMIPS lazy binding stub when dynamic symbol count is
8001 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
8002 template<int size, bool big_endian>
8003 const uint32_t
8004 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
8005 {
8006   0xff3c, 0x8010,     // lw t9,0x8010(gp)
8007   0x001f, 0x7a90,     // or t7,ra,zero
8008   0x41b8, 0x0000,     // lui t8,DYN_INDEX
8009   0x03f9, 0x0f3c,     // jalr ra,t9
8010   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
8011 };
8012 
8013 // The format of the microMIPS lazy binding stub when dynamic symbol count is
8014 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
8015 template<int size, bool big_endian>
8016 const uint32_t
8017 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
8018 {
8019   0xdf3c, 0x8010,     // ld t9,0x8010(gp)
8020   0x001f, 0x7a90,     // or t7,ra,zero
8021   0x41b8, 0x0000,     // lui t8,DYN_INDEX
8022   0x03f9, 0x0f3c,     // jalr ra,t9
8023   0x5318, 0x0000      // ori t8,t8,DYN_INDEX
8024 };
8025 
8026 // Create entry for a symbol.
8027 
8028 template<int size, bool big_endian>
8029 void
make_entry(Mips_symbol<size> * gsym)8030 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
8031     Mips_symbol<size>* gsym)
8032 {
8033   if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
8034     {
8035       this->symbols_.insert(gsym);
8036       gsym->set_has_lazy_stub(true);
8037     }
8038 }
8039 
8040 // Remove entry for a symbol.
8041 
8042 template<int size, bool big_endian>
8043 void
remove_entry(Mips_symbol<size> * gsym)8044 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8045     Mips_symbol<size>* gsym)
8046 {
8047   if (gsym->has_lazy_stub())
8048     {
8049       this->symbols_.erase(gsym);
8050       gsym->set_has_lazy_stub(false);
8051     }
8052 }
8053 
8054 // Set stub offsets for symbols.  This method expects that the number of
8055 // entries in dynamic symbol table is set.
8056 
8057 template<int size, bool big_endian>
8058 void
set_lazy_stub_offsets()8059 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8060 {
8061   gold_assert(this->dynsym_count_ != -1U);
8062 
8063   if (this->stub_offsets_are_set_)
8064     return;
8065 
8066   unsigned int stub_size = this->stub_size();
8067   unsigned int offset = 0;
8068   for (typename Mips_stubs_entry_set::const_iterator
8069        p = this->symbols_.begin();
8070        p != this->symbols_.end();
8071        ++p, offset += stub_size)
8072     {
8073       Mips_symbol<size>* mips_sym = *p;
8074       mips_sym->set_lazy_stub_offset(offset);
8075     }
8076   this->stub_offsets_are_set_ = true;
8077 }
8078 
8079 template<int size, bool big_endian>
8080 void
set_needs_dynsym_value()8081 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8082 {
8083   for (typename Mips_stubs_entry_set::const_iterator
8084        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8085     {
8086       Mips_symbol<size>* sym = *p;
8087       if (sym->is_from_dynobj())
8088         sym->set_needs_dynsym_value();
8089     }
8090 }
8091 
8092 // Write out the .MIPS.stubs.  This uses the hand-coded instructions and
8093 // adjusts them as needed.
8094 
8095 template<int size, bool big_endian>
8096 void
do_write(Output_file * of)8097 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
8098 {
8099   const off_t offset = this->offset();
8100   const section_size_type oview_size =
8101     convert_to_section_size_type(this->data_size());
8102   unsigned char* const oview = of->get_output_view(offset, oview_size);
8103 
8104   bool big_stub = this->dynsym_count_ > 0x10000;
8105 
8106   unsigned char* pov = oview;
8107   for (typename Mips_stubs_entry_set::const_iterator
8108        p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8109     {
8110       Mips_symbol<size>* sym = *p;
8111       const uint32_t* lazy_stub;
8112       bool n64 = this->target_->is_output_n64();
8113 
8114       if (!this->target_->is_output_micromips())
8115         {
8116           // Write standard (non-microMIPS) stub.
8117           if (!big_stub)
8118             {
8119               if (sym->dynsym_index() & ~0x7fff)
8120                 // Dynsym index is between 32K and 64K.
8121                 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
8122               else
8123                 // Dynsym index is less than 32K.
8124                 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8125             }
8126           else
8127             lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8128 
8129           unsigned int i = 0;
8130           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8131           elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8132           pov += 8;
8133 
8134           i += 2;
8135           if (big_stub)
8136             {
8137               // LUI instruction of the big stub.  Paste high 16 bits of the
8138               // dynsym index.
8139               elfcpp::Swap<32, big_endian>::writeval(pov,
8140                   lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
8141               pov += 4;
8142               i += 1;
8143             }
8144           elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8145           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8146           elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8147               lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8148           pov += 8;
8149         }
8150       else if (this->target_->use_32bit_micromips_instructions())
8151         {
8152           // Write microMIPS stub in insn32 mode.
8153           if (!big_stub)
8154             {
8155               if (sym->dynsym_index() & ~0x7fff)
8156                 // Dynsym index is between 32K and 64K.
8157                 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8158                                 : lazy_stub_micromips32_normal_2;
8159               else
8160                 // Dynsym index is less than 32K.
8161                 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8162                                 : lazy_stub_micromips32_normal_1;
8163             }
8164           else
8165             lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8166                             : lazy_stub_micromips32_big;
8167 
8168           unsigned int i = 0;
8169           // First stub instruction.  We emit 32-bit microMIPS instructions by
8170           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8171           // the instruction where the opcode is must always come first, for
8172           // both little and big endian.
8173           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8174           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8175           // Second stub instruction.
8176           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8177           elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8178           pov += 8;
8179           i += 4;
8180           if (big_stub)
8181             {
8182               // LUI instruction of the big stub.  Paste high 16 bits of the
8183               // dynsym index.
8184               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8185               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8186                   (sym->dynsym_index() >> 16) & 0x7fff);
8187               pov += 4;
8188               i += 2;
8189             }
8190           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8191           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8192           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8193           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8194           elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8195               sym->dynsym_index() & 0xffff);
8196           pov += 8;
8197         }
8198       else
8199         {
8200           // Write microMIPS stub.
8201           if (!big_stub)
8202             {
8203               if (sym->dynsym_index() & ~0x7fff)
8204                 // Dynsym index is between 32K and 64K.
8205                 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8206                                 : lazy_stub_micromips_normal_2;
8207               else
8208                 // Dynsym index is less than 32K.
8209                 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8210                                 : lazy_stub_micromips_normal_1;
8211             }
8212           else
8213             lazy_stub = n64 ? lazy_stub_micromips_big_n64
8214                             : lazy_stub_micromips_big;
8215 
8216           unsigned int i = 0;
8217           // First stub instruction.  We emit 32-bit microMIPS instructions by
8218           // emitting two 16-bit parts because on microMIPS the 16-bit part of
8219           // the instruction where the opcode is must always come first, for
8220           // both little and big endian.
8221           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8222           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8223           // Second stub instruction.
8224           elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8225           pov += 6;
8226           i += 3;
8227           if (big_stub)
8228             {
8229               // LUI instruction of the big stub.  Paste high 16 bits of the
8230               // dynsym index.
8231               elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8232               elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8233                   (sym->dynsym_index() >> 16) & 0x7fff);
8234               pov += 4;
8235               i += 2;
8236             }
8237           elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8238           // Last stub instruction.  Paste low 16 bits of the dynsym index.
8239           elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8240           elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8241               sym->dynsym_index() & 0xffff);
8242           pov += 6;
8243         }
8244     }
8245 
8246   // We always allocate 20 bytes for every stub, because final dynsym count is
8247   // not known in method do_finalize_sections.  There are 4 unused bytes per
8248   // stub if final dynsym count is less than 0x10000.
8249   unsigned int used = pov - oview;
8250   unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8251   gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8252 
8253   // Fill the unused space with zeroes.
8254   // TODO(sasa): Can we strip unused bytes during the relaxation?
8255   if (unused > 0)
8256     memset(pov, 0, unused);
8257 
8258   of->write_output_view(offset, oview_size, oview);
8259 }
8260 
8261 // Mips_output_section_reginfo methods.
8262 
8263 template<int size, bool big_endian>
8264 void
do_write(Output_file * of)8265 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8266 {
8267   off_t offset = this->offset();
8268   off_t data_size = this->data_size();
8269 
8270   unsigned char* view = of->get_output_view(offset, data_size);
8271   elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8272   elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8273   elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8274   elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8275   elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8276   // Write the gp value.
8277   elfcpp::Swap<size, big_endian>::writeval(view + 20,
8278                                            this->target_->gp_value());
8279 
8280   of->write_output_view(offset, data_size, view);
8281 }
8282 
8283 // Mips_output_section_options methods.
8284 
8285 template<int size, bool big_endian>
8286 void
do_write(Output_file * of)8287 Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
8288 {
8289   off_t offset = this->offset();
8290   const section_size_type oview_size =
8291     convert_to_section_size_type(this->data_size());
8292   unsigned char* view = of->get_output_view(offset, oview_size);
8293   const unsigned char* end = view + oview_size;
8294 
8295   while (view + 8 <= end)
8296     {
8297       unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8298       unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8299       if (sz < 8)
8300         {
8301           gold_error(_("Warning: bad `%s' option size %u smaller "
8302                        "than its header in output section"),
8303                      this->name(), sz);
8304           break;
8305         }
8306 
8307       // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
8308       if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
8309         elfcpp::Swap<size, big_endian>::writeval(view + 32,
8310                                                  this->target_->gp_value());
8311       else if (kind == elfcpp::ODK_REGINFO)
8312         elfcpp::Swap<size, big_endian>::writeval(view + 28,
8313                                                  this->target_->gp_value());
8314 
8315       view += sz;
8316     }
8317 
8318   of->write_output_view(offset, oview_size, view);
8319 }
8320 
8321 // Mips_output_section_abiflags methods.
8322 
8323 template<int size, bool big_endian>
8324 void
do_write(Output_file * of)8325 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8326 {
8327   off_t offset = this->offset();
8328   off_t data_size = this->data_size();
8329 
8330   unsigned char* view = of->get_output_view(offset, data_size);
8331   elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8332   elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8333   elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8334   elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8335   elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8336   elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8337   elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8338   elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8339   elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8340   elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8341   elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8342 
8343   of->write_output_view(offset, data_size, view);
8344 }
8345 
8346 // Mips_copy_relocs methods.
8347 
8348 // Emit any saved relocs.
8349 
8350 template<int sh_type, int size, bool big_endian>
8351 void
emit_mips(Output_data_reloc<sh_type,true,size,big_endian> * reloc_section,Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target)8352 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8353     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8354     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8355 {
8356   for (typename Copy_relocs<sh_type, size, big_endian>::
8357        Copy_reloc_entries::iterator p = this->entries_.begin();
8358        p != this->entries_.end();
8359        ++p)
8360     emit_entry(*p, reloc_section, symtab, layout, target);
8361 
8362   // We no longer need the saved information.
8363   this->entries_.clear();
8364 }
8365 
8366 // Emit the reloc if appropriate.
8367 
8368 template<int sh_type, int size, bool big_endian>
8369 void
emit_entry(Copy_reloc_entry & entry,Output_data_reloc<sh_type,true,size,big_endian> * reloc_section,Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target)8370 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8371     Copy_reloc_entry& entry,
8372     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8373     Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8374 {
8375   // If the symbol is no longer defined in a dynamic object, then we
8376   // emitted a COPY relocation, and we do not want to emit this
8377   // dynamic relocation.
8378   if (!entry.sym_->is_from_dynobj())
8379     return;
8380 
8381   bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8382                            || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8383                            || entry.reloc_type_ == elfcpp::R_MIPS_64);
8384 
8385   Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8386   if (can_make_dynamic && !sym->has_static_relocs())
8387     {
8388       Mips_relobj<size, big_endian>* object =
8389         Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8390       target->got_section(symtab, layout)->record_global_got_symbol(
8391                           sym, object, entry.reloc_type_, true, false);
8392       if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8393         target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8394             entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8395       else
8396         target->rel_dyn_section(layout)->add_symbolless_global_addend(
8397             sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8398             entry.shndx_, entry.address_);
8399     }
8400   else
8401     this->make_copy_reloc(symtab, layout,
8402                           static_cast<Sized_symbol<size>*>(entry.sym_),
8403                           entry.relobj_,
8404                           reloc_section);
8405 }
8406 
8407 // Target_mips methods.
8408 
8409 // Return the value to use for a dynamic symbol which requires special
8410 // treatment.  This is how we support equality comparisons of function
8411 // pointers across shared library boundaries, as described in the
8412 // processor specific ABI supplement.
8413 
8414 template<int size, bool big_endian>
8415 uint64_t
do_dynsym_value(const Symbol * gsym) const8416 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8417 {
8418   uint64_t value = 0;
8419   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8420 
8421   if (!mips_sym->has_lazy_stub())
8422     {
8423       if (mips_sym->has_plt_offset())
8424         {
8425           // We distinguish between PLT entries and lazy-binding stubs by
8426           // giving the former an st_other value of STO_MIPS_PLT.  Set the
8427           // value to the stub address if there are any relocations in the
8428           // binary where pointer equality matters.
8429           if (mips_sym->pointer_equality_needed())
8430             {
8431               // Prefer a standard MIPS PLT entry.
8432               if (mips_sym->has_mips_plt_offset())
8433                 value = this->plt_section()->mips_entry_address(mips_sym);
8434               else
8435                 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8436             }
8437           else
8438             value = 0;
8439         }
8440     }
8441   else
8442     {
8443       // First, set stub offsets for symbols.  This method expects that the
8444       // number of entries in dynamic symbol table is set.
8445       this->mips_stubs_section()->set_lazy_stub_offsets();
8446 
8447       // The run-time linker uses the st_value field of the symbol
8448       // to reset the global offset table entry for this external
8449       // to its stub address when unlinking a shared object.
8450       value = this->mips_stubs_section()->stub_address(mips_sym);
8451     }
8452 
8453   if (mips_sym->has_mips16_fn_stub())
8454     {
8455       // If we have a MIPS16 function with a stub, the dynamic symbol must
8456       // refer to the stub, since only the stub uses the standard calling
8457       // conventions.
8458       value = mips_sym->template
8459               get_mips16_fn_stub<big_endian>()->output_address();
8460     }
8461 
8462   return value;
8463 }
8464 
8465 // Get the dynamic reloc section, creating it if necessary.  It's always
8466 // .rel.dyn, even for MIPS64.
8467 
8468 template<int size, bool big_endian>
8469 typename Target_mips<size, big_endian>::Reloc_section*
rel_dyn_section(Layout * layout)8470 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8471 {
8472   if (this->rel_dyn_ == NULL)
8473     {
8474       gold_assert(layout != NULL);
8475       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8476       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8477                                       elfcpp::SHF_ALLOC, this->rel_dyn_,
8478                                       ORDER_DYNAMIC_RELOCS, false);
8479 
8480       // First entry in .rel.dyn has to be null.
8481       // This is hack - we define dummy output data and set its address to 0,
8482       // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8483       // This ensures that the entry is null.
8484       Output_data* od = new Output_data_zero_fill(0, 0);
8485       od->set_address(0);
8486       this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8487     }
8488   return this->rel_dyn_;
8489 }
8490 
8491 // Get the GOT section, creating it if necessary.
8492 
8493 template<int size, bool big_endian>
8494 Mips_output_data_got<size, big_endian>*
got_section(Symbol_table * symtab,Layout * layout)8495 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8496                                            Layout* layout)
8497 {
8498   if (this->got_ == NULL)
8499     {
8500       gold_assert(symtab != NULL && layout != NULL);
8501 
8502       this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8503                                                               layout);
8504       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8505                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8506                                       elfcpp::SHF_MIPS_GPREL),
8507                                       this->got_, ORDER_DATA, false);
8508 
8509       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8510       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8511                                     Symbol_table::PREDEFINED,
8512                                     this->got_,
8513                                     0, 0, elfcpp::STT_OBJECT,
8514                                     elfcpp::STB_GLOBAL,
8515                                     elfcpp::STV_HIDDEN, 0,
8516                                     false, false);
8517     }
8518 
8519   return this->got_;
8520 }
8521 
8522 // Calculate value of _gp symbol.
8523 
8524 template<int size, bool big_endian>
8525 void
set_gp(Layout * layout,Symbol_table * symtab)8526 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8527 {
8528   gold_assert(this->gp_ == NULL);
8529 
8530   Sized_symbol<size>* gp =
8531     static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8532 
8533   // Set _gp symbol if the linker script hasn't created it.
8534   if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
8535     {
8536       // If there is no .got section, gp should be based on .sdata.
8537       Output_data* gp_section = (this->got_ != NULL
8538                                  ? this->got_->output_section()
8539                                  : layout->find_output_section(".sdata"));
8540 
8541       if (gp_section != NULL)
8542         gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8543                                           "_gp", NULL, Symbol_table::PREDEFINED,
8544                                           gp_section, MIPS_GP_OFFSET, 0,
8545                                           elfcpp::STT_NOTYPE,
8546                                           elfcpp::STB_LOCAL,
8547                                           elfcpp::STV_DEFAULT,
8548                                           0, false, false));
8549     }
8550 
8551   this->gp_ = gp;
8552 }
8553 
8554 // Set the dynamic symbol indexes.  INDEX is the index of the first
8555 // global dynamic symbol.  Pointers to the symbols are stored into the
8556 // vector SYMS.  The names are added to DYNPOOL.  This returns an
8557 // updated dynamic symbol index.
8558 
8559 template<int size, bool big_endian>
8560 unsigned int
do_set_dynsym_indexes(std::vector<Symbol * > * dyn_symbols,unsigned int index,std::vector<Symbol * > * syms,Stringpool * dynpool,Versions * versions,Symbol_table * symtab) const8561 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8562     std::vector<Symbol*>* dyn_symbols, unsigned int index,
8563     std::vector<Symbol*>* syms, Stringpool* dynpool,
8564     Versions* versions, Symbol_table* symtab) const
8565 {
8566   std::vector<Symbol*> non_got_symbols;
8567   std::vector<Symbol*> got_symbols;
8568 
8569   reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8570                                         &got_symbols);
8571 
8572   for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8573        p != non_got_symbols.end();
8574        ++p)
8575     {
8576       Symbol* sym = *p;
8577 
8578       // Note that SYM may already have a dynamic symbol index, since
8579       // some symbols appear more than once in the symbol table, with
8580       // and without a version.
8581 
8582       if (!sym->has_dynsym_index())
8583         {
8584           sym->set_dynsym_index(index);
8585           ++index;
8586           syms->push_back(sym);
8587           dynpool->add(sym->name(), false, NULL);
8588 
8589           // Record any version information.
8590           if (sym->version() != NULL)
8591             versions->record_version(symtab, dynpool, sym);
8592 
8593           // If the symbol is defined in a dynamic object and is
8594           // referenced in a regular object, then mark the dynamic
8595           // object as needed.  This is used to implement --as-needed.
8596           if (sym->is_from_dynobj() && sym->in_reg())
8597             sym->object()->set_is_needed();
8598         }
8599     }
8600 
8601   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8602        p != got_symbols.end();
8603        ++p)
8604     {
8605       Symbol* sym = *p;
8606       if (!sym->has_dynsym_index())
8607         {
8608           // Record any version information.
8609           if (sym->version() != NULL)
8610             versions->record_version(symtab, dynpool, sym);
8611         }
8612     }
8613 
8614   index = versions->finalize(symtab, index, syms);
8615 
8616   int got_sym_count = 0;
8617   for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8618        p != got_symbols.end();
8619        ++p)
8620     {
8621       Symbol* sym = *p;
8622 
8623       if (!sym->has_dynsym_index())
8624         {
8625           ++got_sym_count;
8626           sym->set_dynsym_index(index);
8627           ++index;
8628           syms->push_back(sym);
8629           dynpool->add(sym->name(), false, NULL);
8630 
8631           // If the symbol is defined in a dynamic object and is
8632           // referenced in a regular object, then mark the dynamic
8633           // object as needed.  This is used to implement --as-needed.
8634           if (sym->is_from_dynobj() && sym->in_reg())
8635             sym->object()->set_is_needed();
8636         }
8637     }
8638 
8639   // Set index of the first symbol that has .got entry.
8640   this->got_->set_first_global_got_dynsym_index(
8641     got_sym_count > 0 ? index - got_sym_count : -1U);
8642 
8643   if (this->mips_stubs_ != NULL)
8644     this->mips_stubs_->set_dynsym_count(index);
8645 
8646   return index;
8647 }
8648 
8649 // Create a PLT entry for a global symbol referenced by r_type relocation.
8650 
8651 template<int size, bool big_endian>
8652 void
make_plt_entry(Symbol_table * symtab,Layout * layout,Mips_symbol<size> * gsym,unsigned int r_type)8653 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8654                                               Layout* layout,
8655                                               Mips_symbol<size>* gsym,
8656                                               unsigned int r_type)
8657 {
8658   if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8659     return;
8660 
8661   if (this->plt_ == NULL)
8662     {
8663       // Create the GOT section first.
8664       this->got_section(symtab, layout);
8665 
8666       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8667       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8668                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8669                                       this->got_plt_, ORDER_DATA, false);
8670 
8671       // The first two entries are reserved.
8672       this->got_plt_->set_current_data_size(2 * size/8);
8673 
8674       this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8675                                                               this->got_plt_,
8676                                                               this);
8677       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8678                                       (elfcpp::SHF_ALLOC
8679                                        | elfcpp::SHF_EXECINSTR),
8680                                       this->plt_, ORDER_PLT, false);
8681 
8682       // Make the sh_info field of .rel.plt point to .plt.
8683       Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
8684       rel_plt_os->set_info_section(this->plt_->output_section());
8685     }
8686 
8687   this->plt_->add_entry(gsym, r_type);
8688 }
8689 
8690 
8691 // Get the .MIPS.stubs section, creating it if necessary.
8692 
8693 template<int size, bool big_endian>
8694 Mips_output_data_mips_stubs<size, big_endian>*
mips_stubs_section(Layout * layout)8695 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8696 {
8697   if (this->mips_stubs_ == NULL)
8698     {
8699       this->mips_stubs_ =
8700         new Mips_output_data_mips_stubs<size, big_endian>(this);
8701       layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8702                                       (elfcpp::SHF_ALLOC
8703                                        | elfcpp::SHF_EXECINSTR),
8704                                       this->mips_stubs_, ORDER_PLT, false);
8705     }
8706   return this->mips_stubs_;
8707 }
8708 
8709 // Get the LA25 stub section, creating it if necessary.
8710 
8711 template<int size, bool big_endian>
8712 Mips_output_data_la25_stub<size, big_endian>*
la25_stub_section(Layout * layout)8713 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8714 {
8715   if (this->la25_stub_ == NULL)
8716     {
8717       this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8718       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8719                                       (elfcpp::SHF_ALLOC
8720                                        | elfcpp::SHF_EXECINSTR),
8721                                       this->la25_stub_, ORDER_TEXT, false);
8722     }
8723   return this->la25_stub_;
8724 }
8725 
8726 // Process the relocations to determine unreferenced sections for
8727 // garbage collection.
8728 
8729 template<int size, bool big_endian>
8730 void
gc_process_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)8731 Target_mips<size, big_endian>::gc_process_relocs(
8732                         Symbol_table* symtab,
8733                         Layout* layout,
8734                         Sized_relobj_file<size, big_endian>* object,
8735                         unsigned int data_shndx,
8736                         unsigned int sh_type,
8737                         const unsigned char* prelocs,
8738                         size_t reloc_count,
8739                         Output_section* output_section,
8740                         bool needs_special_offset_handling,
8741                         size_t local_symbol_count,
8742                         const unsigned char* plocal_symbols)
8743 {
8744   typedef Target_mips<size, big_endian> Mips;
8745 
8746   if (sh_type == elfcpp::SHT_REL)
8747     {
8748       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8749           Classify_reloc;
8750 
8751       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8752         symtab,
8753         layout,
8754         this,
8755         object,
8756         data_shndx,
8757         prelocs,
8758         reloc_count,
8759         output_section,
8760         needs_special_offset_handling,
8761         local_symbol_count,
8762         plocal_symbols);
8763     }
8764   else if (sh_type == elfcpp::SHT_RELA)
8765     {
8766       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8767           Classify_reloc;
8768 
8769       gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8770         symtab,
8771         layout,
8772         this,
8773         object,
8774         data_shndx,
8775         prelocs,
8776         reloc_count,
8777         output_section,
8778         needs_special_offset_handling,
8779         local_symbol_count,
8780         plocal_symbols);
8781     }
8782   else
8783     gold_unreachable();
8784 }
8785 
8786 // Scan relocations for a section.
8787 
8788 template<int size, bool big_endian>
8789 void
scan_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)8790 Target_mips<size, big_endian>::scan_relocs(
8791                         Symbol_table* symtab,
8792                         Layout* layout,
8793                         Sized_relobj_file<size, big_endian>* object,
8794                         unsigned int data_shndx,
8795                         unsigned int sh_type,
8796                         const unsigned char* prelocs,
8797                         size_t reloc_count,
8798                         Output_section* output_section,
8799                         bool needs_special_offset_handling,
8800                         size_t local_symbol_count,
8801                         const unsigned char* plocal_symbols)
8802 {
8803   typedef Target_mips<size, big_endian> Mips;
8804 
8805   if (sh_type == elfcpp::SHT_REL)
8806     {
8807       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8808 	  Classify_reloc;
8809 
8810       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8811 	symtab,
8812 	layout,
8813 	this,
8814 	object,
8815 	data_shndx,
8816 	prelocs,
8817 	reloc_count,
8818 	output_section,
8819 	needs_special_offset_handling,
8820 	local_symbol_count,
8821 	plocal_symbols);
8822     }
8823   else if (sh_type == elfcpp::SHT_RELA)
8824     {
8825       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8826 	  Classify_reloc;
8827 
8828       gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8829 	symtab,
8830 	layout,
8831 	this,
8832 	object,
8833 	data_shndx,
8834 	prelocs,
8835 	reloc_count,
8836 	output_section,
8837 	needs_special_offset_handling,
8838 	local_symbol_count,
8839 	plocal_symbols);
8840     }
8841 }
8842 
8843 template<int size, bool big_endian>
8844 bool
mips_32bit_flags(elfcpp::Elf_Word flags)8845 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8846 {
8847   return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8848           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::EF_MIPS_ABI_O32
8849           || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::EF_MIPS_ABI_EABI32
8850           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::EF_MIPS_ARCH_1
8851           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::EF_MIPS_ARCH_2
8852           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::EF_MIPS_ARCH_32
8853           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::EF_MIPS_ARCH_32R2
8854           || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::EF_MIPS_ARCH_32R6);
8855 }
8856 
8857 // Return the MACH for a MIPS e_flags value.
8858 template<int size, bool big_endian>
8859 unsigned int
elf_mips_mach(elfcpp::Elf_Word flags)8860 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8861 {
8862   switch (flags & elfcpp::EF_MIPS_MACH)
8863     {
8864     case elfcpp::EF_MIPS_MACH_3900:
8865       return mach_mips3900;
8866 
8867     case elfcpp::EF_MIPS_MACH_4010:
8868       return mach_mips4010;
8869 
8870     case elfcpp::EF_MIPS_MACH_4100:
8871       return mach_mips4100;
8872 
8873     case elfcpp::EF_MIPS_MACH_4111:
8874       return mach_mips4111;
8875 
8876     case elfcpp::EF_MIPS_MACH_4120:
8877       return mach_mips4120;
8878 
8879     case elfcpp::EF_MIPS_MACH_4650:
8880       return mach_mips4650;
8881 
8882     case elfcpp::EF_MIPS_MACH_5400:
8883       return mach_mips5400;
8884 
8885     case elfcpp::EF_MIPS_MACH_5500:
8886       return mach_mips5500;
8887 
8888     case elfcpp::EF_MIPS_MACH_5900:
8889       return mach_mips5900;
8890 
8891     case elfcpp::EF_MIPS_MACH_9000:
8892       return mach_mips9000;
8893 
8894     case elfcpp::EF_MIPS_MACH_SB1:
8895       return mach_mips_sb1;
8896 
8897     case elfcpp::EF_MIPS_MACH_LS2E:
8898       return mach_mips_loongson_2e;
8899 
8900     case elfcpp::EF_MIPS_MACH_LS2F:
8901       return mach_mips_loongson_2f;
8902 
8903     case elfcpp::EF_MIPS_MACH_GS464:
8904       return mach_mips_gs464;
8905 
8906     case elfcpp::EF_MIPS_MACH_GS464E:
8907       return mach_mips_gs464e;
8908 
8909     case elfcpp::EF_MIPS_MACH_GS264E:
8910       return mach_mips_gs264e;
8911 
8912     case elfcpp::EF_MIPS_MACH_OCTEON3:
8913       return mach_mips_octeon3;
8914 
8915     case elfcpp::EF_MIPS_MACH_OCTEON2:
8916       return mach_mips_octeon2;
8917 
8918     case elfcpp::EF_MIPS_MACH_OCTEON:
8919       return mach_mips_octeon;
8920 
8921     case elfcpp::EF_MIPS_MACH_XLR:
8922       return mach_mips_xlr;
8923 
8924     default:
8925       switch (flags & elfcpp::EF_MIPS_ARCH)
8926         {
8927         default:
8928         case elfcpp::EF_MIPS_ARCH_1:
8929           return mach_mips3000;
8930 
8931         case elfcpp::EF_MIPS_ARCH_2:
8932           return mach_mips6000;
8933 
8934         case elfcpp::EF_MIPS_ARCH_3:
8935           return mach_mips4000;
8936 
8937         case elfcpp::EF_MIPS_ARCH_4:
8938           return mach_mips8000;
8939 
8940         case elfcpp::EF_MIPS_ARCH_5:
8941           return mach_mips5;
8942 
8943         case elfcpp::EF_MIPS_ARCH_32:
8944           return mach_mipsisa32;
8945 
8946         case elfcpp::EF_MIPS_ARCH_64:
8947           return mach_mipsisa64;
8948 
8949         case elfcpp::EF_MIPS_ARCH_32R2:
8950           return mach_mipsisa32r2;
8951 
8952         case elfcpp::EF_MIPS_ARCH_32R6:
8953           return mach_mipsisa32r6;
8954 
8955         case elfcpp::EF_MIPS_ARCH_64R2:
8956           return mach_mipsisa64r2;
8957 
8958         case elfcpp::EF_MIPS_ARCH_64R6:
8959           return mach_mipsisa64r6;
8960         }
8961     }
8962 
8963   return 0;
8964 }
8965 
8966 // Return the MACH for each .MIPS.abiflags ISA Extension.
8967 
8968 template<int size, bool big_endian>
8969 unsigned int
mips_isa_ext_mach(unsigned int isa_ext)8970 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8971 {
8972   switch (isa_ext)
8973     {
8974     case elfcpp::AFL_EXT_3900:
8975       return mach_mips3900;
8976 
8977     case elfcpp::AFL_EXT_4010:
8978       return mach_mips4010;
8979 
8980     case elfcpp::AFL_EXT_4100:
8981       return mach_mips4100;
8982 
8983     case elfcpp::AFL_EXT_4111:
8984       return mach_mips4111;
8985 
8986     case elfcpp::AFL_EXT_4120:
8987       return mach_mips4120;
8988 
8989     case elfcpp::AFL_EXT_4650:
8990       return mach_mips4650;
8991 
8992     case elfcpp::AFL_EXT_5400:
8993       return mach_mips5400;
8994 
8995     case elfcpp::AFL_EXT_5500:
8996       return mach_mips5500;
8997 
8998     case elfcpp::AFL_EXT_5900:
8999       return mach_mips5900;
9000 
9001     case elfcpp::AFL_EXT_10000:
9002       return mach_mips10000;
9003 
9004     case elfcpp::AFL_EXT_LOONGSON_2E:
9005       return mach_mips_loongson_2e;
9006 
9007     case elfcpp::AFL_EXT_LOONGSON_2F:
9008       return mach_mips_loongson_2f;
9009 
9010     case elfcpp::AFL_EXT_SB1:
9011       return mach_mips_sb1;
9012 
9013     case elfcpp::AFL_EXT_OCTEON:
9014       return mach_mips_octeon;
9015 
9016     case elfcpp::AFL_EXT_OCTEONP:
9017       return mach_mips_octeonp;
9018 
9019     case elfcpp::AFL_EXT_OCTEON2:
9020       return mach_mips_octeon2;
9021 
9022     case elfcpp::AFL_EXT_XLR:
9023       return mach_mips_xlr;
9024 
9025     default:
9026       return mach_mips3000;
9027     }
9028 }
9029 
9030 // Return the .MIPS.abiflags value representing each ISA Extension.
9031 
9032 template<int size, bool big_endian>
9033 unsigned int
mips_isa_ext(unsigned int mips_mach)9034 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
9035 {
9036   switch (mips_mach)
9037     {
9038     case mach_mips3900:
9039       return elfcpp::AFL_EXT_3900;
9040 
9041     case mach_mips4010:
9042       return elfcpp::AFL_EXT_4010;
9043 
9044     case mach_mips4100:
9045       return elfcpp::AFL_EXT_4100;
9046 
9047     case mach_mips4111:
9048       return elfcpp::AFL_EXT_4111;
9049 
9050     case mach_mips4120:
9051       return elfcpp::AFL_EXT_4120;
9052 
9053     case mach_mips4650:
9054       return elfcpp::AFL_EXT_4650;
9055 
9056     case mach_mips5400:
9057       return elfcpp::AFL_EXT_5400;
9058 
9059     case mach_mips5500:
9060       return elfcpp::AFL_EXT_5500;
9061 
9062     case mach_mips5900:
9063       return elfcpp::AFL_EXT_5900;
9064 
9065     case mach_mips10000:
9066       return elfcpp::AFL_EXT_10000;
9067 
9068     case mach_mips_loongson_2e:
9069       return elfcpp::AFL_EXT_LOONGSON_2E;
9070 
9071     case mach_mips_loongson_2f:
9072       return elfcpp::AFL_EXT_LOONGSON_2F;
9073 
9074     case mach_mips_sb1:
9075       return elfcpp::AFL_EXT_SB1;
9076 
9077     case mach_mips_octeon:
9078       return elfcpp::AFL_EXT_OCTEON;
9079 
9080     case mach_mips_octeonp:
9081       return elfcpp::AFL_EXT_OCTEONP;
9082 
9083     case mach_mips_octeon3:
9084       return elfcpp::AFL_EXT_OCTEON3;
9085 
9086     case mach_mips_octeon2:
9087       return elfcpp::AFL_EXT_OCTEON2;
9088 
9089     case mach_mips_xlr:
9090       return elfcpp::AFL_EXT_XLR;
9091 
9092     default:
9093       return 0;
9094     }
9095 }
9096 
9097 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
9098 
9099 template<int size, bool big_endian>
9100 void
update_abiflags_isa(const std::string & name,elfcpp::Elf_Word e_flags,Mips_abiflags<big_endian> * abiflags)9101 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9102     elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9103 {
9104   int new_isa = 0;
9105   switch (e_flags & elfcpp::EF_MIPS_ARCH)
9106     {
9107     case elfcpp::EF_MIPS_ARCH_1:
9108       new_isa = this->level_rev(1, 0);
9109       break;
9110     case elfcpp::EF_MIPS_ARCH_2:
9111       new_isa = this->level_rev(2, 0);
9112       break;
9113     case elfcpp::EF_MIPS_ARCH_3:
9114       new_isa = this->level_rev(3, 0);
9115       break;
9116     case elfcpp::EF_MIPS_ARCH_4:
9117       new_isa = this->level_rev(4, 0);
9118       break;
9119     case elfcpp::EF_MIPS_ARCH_5:
9120       new_isa = this->level_rev(5, 0);
9121       break;
9122     case elfcpp::EF_MIPS_ARCH_32:
9123       new_isa = this->level_rev(32, 1);
9124       break;
9125     case elfcpp::EF_MIPS_ARCH_32R2:
9126       new_isa = this->level_rev(32, 2);
9127       break;
9128     case elfcpp::EF_MIPS_ARCH_32R6:
9129       new_isa = this->level_rev(32, 6);
9130       break;
9131     case elfcpp::EF_MIPS_ARCH_64:
9132       new_isa = this->level_rev(64, 1);
9133       break;
9134     case elfcpp::EF_MIPS_ARCH_64R2:
9135       new_isa = this->level_rev(64, 2);
9136       break;
9137     case elfcpp::EF_MIPS_ARCH_64R6:
9138       new_isa = this->level_rev(64, 6);
9139       break;
9140     default:
9141       gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9142                  this->elf_mips_mach_name(e_flags));
9143     }
9144 
9145   if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9146     {
9147       // Decode a single value into level and revision.
9148       abiflags->isa_level = new_isa >> 3;
9149       abiflags->isa_rev = new_isa & 0x7;
9150     }
9151 
9152   // Update the isa_ext if needed.
9153   if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9154       this->elf_mips_mach(e_flags)))
9155     abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9156 }
9157 
9158 // Infer the content of the ABI flags based on the elf header.
9159 
9160 template<int size, bool big_endian>
9161 void
infer_abiflags(Mips_relobj<size,big_endian> * relobj,Mips_abiflags<big_endian> * abiflags)9162 Target_mips<size, big_endian>::infer_abiflags(
9163     Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9164 {
9165   const Attributes_section_data* pasd = relobj->attributes_section_data();
9166   int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9167   elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9168 
9169   this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9170   if (pasd != NULL)
9171     {
9172       // Read fp_abi from the .gnu.attribute section.
9173       const Object_attribute* attr =
9174         pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9175       attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9176     }
9177 
9178   abiflags->fp_abi = attr_fp_abi;
9179   abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9180   abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9181   abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9182                                                        : elfcpp::AFL_REG_64;
9183 
9184   if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9185       || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9186       || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9187       && abiflags->gpr_size == elfcpp::AFL_REG_32))
9188     abiflags->cpr1_size = elfcpp::AFL_REG_32;
9189   else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9190            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9191            || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9192     abiflags->cpr1_size = elfcpp::AFL_REG_64;
9193 
9194   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9195     abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9196   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9197     abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9198   if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9199     abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9200 
9201   if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9202       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9203       && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9204       && abiflags->isa_level >= 32
9205       && abiflags->ases != elfcpp::AFL_ASE_LOONGSON_EXT)
9206     abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9207 }
9208 
9209 // Create abiflags from elf header or from .MIPS.abiflags section.
9210 
9211 template<int size, bool big_endian>
9212 void
create_abiflags(Mips_relobj<size,big_endian> * relobj,Mips_abiflags<big_endian> * abiflags)9213 Target_mips<size, big_endian>::create_abiflags(
9214     Mips_relobj<size, big_endian>* relobj,
9215     Mips_abiflags<big_endian>* abiflags)
9216 {
9217   Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9218   Mips_abiflags<big_endian> header_abiflags;
9219 
9220   this->infer_abiflags(relobj, &header_abiflags);
9221 
9222   if (sec_abiflags == NULL)
9223     {
9224       // If there is no input .MIPS.abiflags section, use abiflags created
9225       // from elf header.
9226       *abiflags = header_abiflags;
9227       return;
9228     }
9229 
9230   this->has_abiflags_section_ = true;
9231 
9232   // It is not possible to infer the correct ISA revision for R3 or R5
9233   // so drop down to R2 for the checks.
9234   unsigned char isa_rev = sec_abiflags->isa_rev;
9235   if (isa_rev == 3 || isa_rev == 5)
9236     isa_rev = 2;
9237 
9238   // Check compatibility between abiflags created from elf header
9239   // and abiflags from .MIPS.abiflags section in this object file.
9240   if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9241       < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9242     gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9243                  relobj->name().c_str());
9244   if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9245       && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9246     gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9247                    ".MIPS.abiflags"), relobj->name().c_str());
9248   if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9249     gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9250                  relobj->name().c_str());
9251   // The isa_ext is allowed to be an extension of what can be inferred
9252   // from e_flags.
9253   if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9254                                this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9255     gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9256                    ".MIPS.abiflags"), relobj->name().c_str());
9257   if (sec_abiflags->flags2 != 0)
9258     gold_warning(_("%s: Unexpected flag in the flags2 field of "
9259                    ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9260                                              sec_abiflags->flags2);
9261   // Use abiflags from .MIPS.abiflags section.
9262   *abiflags = *sec_abiflags;
9263 }
9264 
9265 // Return the meaning of fp_abi, or "unknown" if not known.
9266 
9267 template<int size, bool big_endian>
9268 const char*
fp_abi_string(int fp)9269 Target_mips<size, big_endian>::fp_abi_string(int fp)
9270 {
9271   switch (fp)
9272     {
9273     case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9274       return "-mdouble-float";
9275     case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9276       return "-msingle-float";
9277     case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9278       return "-msoft-float";
9279     case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9280       return _("-mips32r2 -mfp64 (12 callee-saved)");
9281     case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9282       return "-mfpxx";
9283     case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9284       return "-mgp32 -mfp64";
9285     case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9286       return "-mgp32 -mfp64 -mno-odd-spreg";
9287     default:
9288       return "unknown";
9289     }
9290 }
9291 
9292 // Select fp_abi.
9293 
9294 template<int size, bool big_endian>
9295 int
select_fp_abi(const std::string & name,int in_fp,int out_fp)9296 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9297                                              int out_fp)
9298 {
9299   if (in_fp == out_fp)
9300     return out_fp;
9301 
9302   if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9303     return in_fp;
9304   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9305            && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9306                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9307                || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9308     return in_fp;
9309   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9310            && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9311                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9312                || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9313     return out_fp; // Keep the current setting.
9314   else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9315            && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9316     return in_fp;
9317   else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9318            && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9319     return out_fp; // Keep the current setting.
9320   else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9321     gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9322                  fp_abi_string(in_fp), fp_abi_string(out_fp));
9323   return out_fp;
9324 }
9325 
9326 // Merge attributes from input object.
9327 
9328 template<int size, bool big_endian>
9329 void
merge_obj_attributes(const std::string & name,const Attributes_section_data * pasd)9330 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9331     const Attributes_section_data* pasd)
9332 {
9333   // Return if there is no attributes section data.
9334   if (pasd == NULL)
9335     return;
9336 
9337   // If output has no object attributes, just copy.
9338   if (this->attributes_section_data_ == NULL)
9339     {
9340       this->attributes_section_data_ = new Attributes_section_data(*pasd);
9341       return;
9342     }
9343 
9344   Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9345       Object_attribute::OBJ_ATTR_GNU);
9346 
9347   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9348   out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9349 
9350   // Merge Tag_compatibility attributes and any common GNU ones.
9351   this->attributes_section_data_->merge(name.c_str(), pasd);
9352 }
9353 
9354 // Merge abiflags from input object.
9355 
9356 template<int size, bool big_endian>
9357 void
merge_obj_abiflags(const std::string & name,Mips_abiflags<big_endian> * in_abiflags)9358 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9359     Mips_abiflags<big_endian>* in_abiflags)
9360 {
9361   // If output has no abiflags, just copy.
9362   if (this->abiflags_ == NULL)
9363   {
9364     this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9365     return;
9366   }
9367 
9368   this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9369                                                 this->abiflags_->fp_abi);
9370 
9371   // Merge abiflags.
9372   this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9373                                         in_abiflags->isa_level);
9374   this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9375                                       in_abiflags->isa_rev);
9376   this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9377                                        in_abiflags->gpr_size);
9378   this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9379                                         in_abiflags->cpr1_size);
9380   this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9381                                         in_abiflags->cpr2_size);
9382   this->abiflags_->ases |= in_abiflags->ases;
9383   this->abiflags_->flags1 |= in_abiflags->flags1;
9384 }
9385 
9386 // Check whether machine EXTENSION is an extension of machine BASE.
9387 template<int size, bool big_endian>
9388 bool
mips_mach_extends(unsigned int base,unsigned int extension)9389 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9390                                                  unsigned int extension)
9391 {
9392   if (extension == base)
9393     return true;
9394 
9395   if ((base == mach_mipsisa32)
9396       && this->mips_mach_extends(mach_mipsisa64, extension))
9397     return true;
9398 
9399   if ((base == mach_mipsisa32r2)
9400       && this->mips_mach_extends(mach_mipsisa64r2, extension))
9401     return true;
9402 
9403   for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9404     if (extension == this->mips_mach_extensions_[i].first)
9405       {
9406         extension = this->mips_mach_extensions_[i].second;
9407         if (extension == base)
9408           return true;
9409       }
9410 
9411   return false;
9412 }
9413 
9414 // Merge file header flags from input object.
9415 
9416 template<int size, bool big_endian>
9417 void
merge_obj_e_flags(const std::string & name,elfcpp::Elf_Word in_flags)9418 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9419                                                  elfcpp::Elf_Word in_flags)
9420 {
9421   // If flags are not set yet, just copy them.
9422   if (!this->are_processor_specific_flags_set())
9423     {
9424       this->set_processor_specific_flags(in_flags);
9425       this->mach_ = this->elf_mips_mach(in_flags);
9426       return;
9427     }
9428 
9429   elfcpp::Elf_Word new_flags = in_flags;
9430   elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9431   elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9432   merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9433 
9434   // Check flag compatibility.
9435   new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9436   old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9437 
9438   // Some IRIX 6 BSD-compatibility objects have this bit set.  It
9439   // doesn't seem to matter.
9440   new_flags &= ~elfcpp::EF_MIPS_XGOT;
9441   old_flags &= ~elfcpp::EF_MIPS_XGOT;
9442 
9443   // MIPSpro generates ucode info in n64 objects.  Again, we should
9444   // just be able to ignore this.
9445   new_flags &= ~elfcpp::EF_MIPS_UCODE;
9446   old_flags &= ~elfcpp::EF_MIPS_UCODE;
9447 
9448   if (new_flags == old_flags)
9449     {
9450       this->set_processor_specific_flags(merged_flags);
9451       return;
9452     }
9453 
9454   if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9455       != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9456     gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9457                  name.c_str());
9458 
9459   if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9460     merged_flags |= elfcpp::EF_MIPS_CPIC;
9461   if (!(new_flags & elfcpp::EF_MIPS_PIC))
9462     merged_flags &= ~elfcpp::EF_MIPS_PIC;
9463 
9464   new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9465   old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9466 
9467   // Compare the ISAs.
9468   if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9469     gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9470   else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9471     {
9472       // Output ISA isn't the same as, or an extension of, input ISA.
9473       if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9474         {
9475           // Copy the architecture info from input object to output.  Also copy
9476           // the 32-bit flag (if set) so that we continue to recognise
9477           // output as a 32-bit binary.
9478           this->mach_ = this->elf_mips_mach(in_flags);
9479           merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9480           merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9481                            | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9482 
9483           // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9484           this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9485 
9486           // Copy across the ABI flags if output doesn't use them
9487           // and if that was what caused us to treat input object as 32-bit.
9488           if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9489               && this->mips_32bit_flags(new_flags)
9490               && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9491             merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9492         }
9493       else
9494         // The ISAs aren't compatible.
9495         gold_error(_("%s: linking %s module with previous %s modules"),
9496                    name.c_str(), this->elf_mips_mach_name(in_flags),
9497                    this->elf_mips_mach_name(merged_flags));
9498     }
9499 
9500   new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9501                 | elfcpp::EF_MIPS_32BITMODE));
9502   old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9503                 | elfcpp::EF_MIPS_32BITMODE));
9504 
9505   // Compare ABIs.
9506   if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9507     {
9508       // Only error if both are set (to different values).
9509       if ((new_flags & elfcpp::EF_MIPS_ABI)
9510            && (old_flags & elfcpp::EF_MIPS_ABI))
9511         gold_error(_("%s: ABI mismatch: linking %s module with "
9512                      "previous %s modules"), name.c_str(),
9513                    this->elf_mips_abi_name(in_flags),
9514                    this->elf_mips_abi_name(merged_flags));
9515 
9516       new_flags &= ~elfcpp::EF_MIPS_ABI;
9517       old_flags &= ~elfcpp::EF_MIPS_ABI;
9518     }
9519 
9520   // Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
9521   // and allow arbitrary mixing of the remaining ASEs (retain the union).
9522   if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9523       != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9524     {
9525       int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9526       int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9527       int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9528       int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9529       int micro_mis = old_m16 && new_micro;
9530       int m16_mis = old_micro && new_m16;
9531 
9532       if (m16_mis || micro_mis)
9533         gold_error(_("%s: ASE mismatch: linking %s module with "
9534                      "previous %s modules"), name.c_str(),
9535                    m16_mis ? "MIPS16" : "microMIPS",
9536                    m16_mis ? "microMIPS" : "MIPS16");
9537 
9538       merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9539 
9540       new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9541       old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9542     }
9543 
9544   // Compare NaN encodings.
9545   if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9546     {
9547       gold_error(_("%s: linking %s module with previous %s modules"),
9548                  name.c_str(),
9549                  (new_flags & elfcpp::EF_MIPS_NAN2008
9550                   ? "-mnan=2008" : "-mnan=legacy"),
9551                  (old_flags & elfcpp::EF_MIPS_NAN2008
9552                   ? "-mnan=2008" : "-mnan=legacy"));
9553 
9554       new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9555       old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9556     }
9557 
9558   // Compare FP64 state.
9559   if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9560     {
9561       gold_error(_("%s: linking %s module with previous %s modules"),
9562                  name.c_str(),
9563                  (new_flags & elfcpp::EF_MIPS_FP64
9564                   ? "-mfp64" : "-mfp32"),
9565                  (old_flags & elfcpp::EF_MIPS_FP64
9566                   ? "-mfp64" : "-mfp32"));
9567 
9568       new_flags &= ~elfcpp::EF_MIPS_FP64;
9569       old_flags &= ~elfcpp::EF_MIPS_FP64;
9570     }
9571 
9572   // Warn about any other mismatches.
9573   if (new_flags != old_flags)
9574     gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9575                  "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9576 
9577   this->set_processor_specific_flags(merged_flags);
9578 }
9579 
9580 // Adjust ELF file header.
9581 
9582 template<int size, bool big_endian>
9583 void
do_adjust_elf_header(unsigned char * view,int len)9584 Target_mips<size, big_endian>::do_adjust_elf_header(
9585     unsigned char* view,
9586     int len)
9587 {
9588   gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9589 
9590   elfcpp::Ehdr<size, big_endian> ehdr(view);
9591   unsigned char e_ident[elfcpp::EI_NIDENT];
9592   elfcpp::Elf_Word flags = this->processor_specific_flags();
9593   memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9594 
9595   unsigned char ei_abiversion = 0;
9596   elfcpp::Elf_Half type = ehdr.get_e_type();
9597   if (type == elfcpp::ET_EXEC
9598       && parameters->options().copyreloc()
9599       && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9600           == elfcpp::EF_MIPS_CPIC)
9601     ei_abiversion = 1;
9602 
9603   if (this->abiflags_ != NULL
9604       && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9605           || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9606     ei_abiversion = 3;
9607 
9608   e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9609   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9610   oehdr.put_e_ident(e_ident);
9611 
9612   if (this->entry_symbol_is_compressed_)
9613     oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9614 }
9615 
9616 // do_make_elf_object to override the same function in the base class.
9617 // We need to use a target-specific sub-class of
9618 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9619 // Hence we need to have our own ELF object creation.
9620 
9621 template<int size, bool big_endian>
9622 Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<size,big_endian> & ehdr)9623 Target_mips<size, big_endian>::do_make_elf_object(
9624     const std::string& name,
9625     Input_file* input_file,
9626     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9627 {
9628   int et = ehdr.get_e_type();
9629   // ET_EXEC files are valid input for --just-symbols/-R,
9630   // and we treat them as relocatable objects.
9631   if (et == elfcpp::ET_REL
9632       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9633     {
9634       Mips_relobj<size, big_endian>* obj =
9635         new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9636       obj->setup();
9637       return obj;
9638     }
9639   else if (et == elfcpp::ET_DYN)
9640     {
9641       // TODO(sasa): Should we create Mips_dynobj?
9642       return Target::do_make_elf_object(name, input_file, offset, ehdr);
9643     }
9644   else
9645     {
9646       gold_error(_("%s: unsupported ELF file type %d"),
9647                  name.c_str(), et);
9648       return NULL;
9649     }
9650 }
9651 
9652 // Finalize the sections.
9653 
9654 template <int size, bool big_endian>
9655 void
do_finalize_sections(Layout * layout,const Input_objects * input_objects,Symbol_table * symtab)9656 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9657                                         const Input_objects* input_objects,
9658                                         Symbol_table* symtab)
9659 {
9660   const bool relocatable = parameters->options().relocatable();
9661 
9662   // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9663   // DT_FINI have correct values.
9664   Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9665       symtab->lookup(parameters->options().init()));
9666   if (init != NULL && (init->is_mips16() || init->is_micromips()))
9667     init->set_value(init->value() | 1);
9668   Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9669       symtab->lookup(parameters->options().fini()));
9670   if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9671     fini->set_value(fini->value() | 1);
9672 
9673   // Check whether the entry symbol is mips16 or micromips.  This is needed to
9674   // adjust entry address in ELF header.
9675   Mips_symbol<size>* entry =
9676     static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9677   this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9678                                        || entry->is_micromips()));
9679 
9680   if (!parameters->doing_static_link()
9681       && (strcmp(parameters->options().hash_style(), "gnu") == 0
9682           || strcmp(parameters->options().hash_style(), "both") == 0))
9683     {
9684       // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9685       // ways.  .gnu.hash needs symbols to be grouped by hash code whereas the
9686       // MIPS ABI requires a mapping between the GOT and the symbol table.
9687       gold_error(".gnu.hash is incompatible with the MIPS ABI");
9688     }
9689 
9690   // Check whether the final section that was scanned has HI16 or GOT16
9691   // relocations without the corresponding LO16 part.
9692   if (this->got16_addends_.size() > 0)
9693       gold_error("Can't find matching LO16 reloc");
9694 
9695   Valtype gprmask = 0;
9696   Valtype cprmask1 = 0;
9697   Valtype cprmask2 = 0;
9698   Valtype cprmask3 = 0;
9699   Valtype cprmask4 = 0;
9700   bool has_reginfo_section = false;
9701 
9702   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9703        p != input_objects->relobj_end();
9704        ++p)
9705     {
9706       Mips_relobj<size, big_endian>* relobj =
9707         Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9708 
9709       // Check for any mips16 stub sections that we can discard.
9710       if (!relocatable)
9711         relobj->discard_mips16_stub_sections(symtab);
9712 
9713       if (!relobj->merge_processor_specific_data())
9714         continue;
9715 
9716       // Merge .reginfo contents of input objects.
9717       if (relobj->has_reginfo_section())
9718         {
9719           has_reginfo_section = true;
9720           gprmask |= relobj->gprmask();
9721           cprmask1 |= relobj->cprmask1();
9722           cprmask2 |= relobj->cprmask2();
9723           cprmask3 |= relobj->cprmask3();
9724           cprmask4 |= relobj->cprmask4();
9725         }
9726 
9727       // Merge processor specific flags.
9728       Mips_abiflags<big_endian> in_abiflags;
9729 
9730       this->create_abiflags(relobj, &in_abiflags);
9731       this->merge_obj_e_flags(relobj->name(),
9732                               relobj->processor_specific_flags());
9733       this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9734       this->merge_obj_attributes(relobj->name(),
9735                                  relobj->attributes_section_data());
9736     }
9737 
9738   // Create a .gnu.attributes section if we have merged any attributes
9739   // from inputs.
9740   if (this->attributes_section_data_ != NULL)
9741     {
9742       Output_attributes_section_data* attributes_section =
9743         new Output_attributes_section_data(*this->attributes_section_data_);
9744       layout->add_output_section_data(".gnu.attributes",
9745                                       elfcpp::SHT_GNU_ATTRIBUTES, 0,
9746                                       attributes_section, ORDER_INVALID, false);
9747     }
9748 
9749   // Create .MIPS.abiflags output section if there is an input section.
9750   if (this->has_abiflags_section_)
9751     {
9752       Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9753         new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9754 
9755       Output_section* os =
9756         layout->add_output_section_data(".MIPS.abiflags",
9757                                         elfcpp::SHT_MIPS_ABIFLAGS,
9758                                         elfcpp::SHF_ALLOC,
9759                                         abiflags_section, ORDER_INVALID, false);
9760 
9761       if (!relocatable && os != NULL)
9762         {
9763           Output_segment* abiflags_segment =
9764             layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9765           abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9766         }
9767     }
9768 
9769   if (has_reginfo_section && !parameters->options().gc_sections())
9770     {
9771       // Create .reginfo output section.
9772       Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9773         new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9774                                                           cprmask1, cprmask2,
9775                                                           cprmask3, cprmask4);
9776 
9777       Output_section* os =
9778         layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9779                                         elfcpp::SHF_ALLOC, reginfo_section,
9780                                         ORDER_INVALID, false);
9781 
9782       if (!relocatable && os != NULL)
9783         {
9784           Output_segment* reginfo_segment =
9785             layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9786                                         elfcpp::PF_R);
9787           reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9788         }
9789     }
9790 
9791   if (this->plt_ != NULL)
9792     {
9793       // Set final PLT offsets for symbols.
9794       this->plt_section()->set_plt_offsets();
9795 
9796       // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9797       // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9798       // there are no standard PLT entries present.
9799       unsigned char nonvis = 0;
9800       if (this->is_output_micromips()
9801           && !this->plt_section()->has_standard_entries())
9802         nonvis = elfcpp::STO_MICROMIPS >> 2;
9803       symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9804                                     Symbol_table::PREDEFINED,
9805                                     this->plt_,
9806                                     0, 0, elfcpp::STT_FUNC,
9807                                     elfcpp::STB_LOCAL,
9808                                     elfcpp::STV_DEFAULT, nonvis,
9809                                     false, false);
9810     }
9811 
9812   if (this->mips_stubs_ != NULL)
9813     {
9814       // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9815       unsigned char nonvis = 0;
9816       if (this->is_output_micromips())
9817         nonvis = elfcpp::STO_MICROMIPS >> 2;
9818       symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9819                                     Symbol_table::PREDEFINED,
9820                                     this->mips_stubs_,
9821                                     0, 0, elfcpp::STT_FUNC,
9822                                     elfcpp::STB_LOCAL,
9823                                     elfcpp::STV_DEFAULT, nonvis,
9824                                     false, false);
9825     }
9826 
9827   if (!relocatable && !parameters->doing_static_link())
9828     // In case there is no .got section, create one.
9829     this->got_section(symtab, layout);
9830 
9831   // Emit any relocs we saved in an attempt to avoid generating COPY
9832   // relocs.
9833   if (this->copy_relocs_.any_saved_relocs())
9834     this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9835                                  this);
9836 
9837   // Set _gp value.
9838   this->set_gp(layout, symtab);
9839 
9840   // Emit dynamic relocs.
9841   for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9842        p != this->dyn_relocs_.end();
9843        ++p)
9844     p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9845 
9846   if (this->has_got_section())
9847     this->got_section()->lay_out_got(layout, symtab, input_objects);
9848 
9849   if (this->mips_stubs_ != NULL)
9850     this->mips_stubs_->set_needs_dynsym_value();
9851 
9852   // Check for functions that might need $25 to be valid on entry.
9853   // TODO(sasa): Can we do this without iterating over all symbols?
9854   typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9855   symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9856                                                                symtab));
9857 
9858   // Add NULL segment.
9859   if (!relocatable)
9860     layout->make_output_segment(elfcpp::PT_NULL, 0);
9861 
9862   // Fill in some more dynamic tags.
9863   // TODO(sasa): Add more dynamic tags.
9864   const Reloc_section* rel_plt = (this->plt_ == NULL
9865                                   ? NULL : this->plt_->rel_plt());
9866   layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9867 				  this->rel_dyn_, true, false, false);
9868 
9869   Output_data_dynamic* const odyn = layout->dynamic_data();
9870   if (odyn != NULL
9871       && !relocatable
9872       && !parameters->doing_static_link())
9873   {
9874     unsigned int d_val;
9875     // This element holds a 32-bit version id for the Runtime
9876     // Linker Interface.  This will start at integer value 1.
9877     d_val = 0x01;
9878     odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9879 
9880     // Dynamic flags
9881     d_val = elfcpp::RHF_NOTPOT;
9882     odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9883 
9884     // Save layout for using when emitting custom dynamic tags.
9885     this->layout_ = layout;
9886 
9887     // This member holds the base address of the segment.
9888     odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9889 
9890     // This member holds the number of entries in the .dynsym section.
9891     odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9892 
9893     // This member holds the index of the first dynamic symbol
9894     // table entry that corresponds to an entry in the global offset table.
9895     odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9896 
9897     // This member holds the number of local GOT entries.
9898     odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9899                        this->got_->get_local_gotno());
9900 
9901     if (this->plt_ != NULL)
9902       // DT_MIPS_PLTGOT dynamic tag
9903       odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9904 
9905     if (!parameters->options().shared())
9906       {
9907         this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9908 
9909         layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9910                                         (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9911                                         this->rld_map_, ORDER_INVALID, false);
9912 
9913         // __RLD_MAP will be filled in by the runtime loader to contain
9914         // a pointer to the _r_debug structure.
9915         Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9916                                             Symbol_table::PREDEFINED,
9917                                             this->rld_map_,
9918                                             0, 0, elfcpp::STT_OBJECT,
9919                                             elfcpp::STB_GLOBAL,
9920                                             elfcpp::STV_DEFAULT, 0,
9921                                             false, false);
9922 
9923         if (!rld_map->is_forced_local())
9924           rld_map->set_needs_dynsym_entry();
9925 
9926         if (!parameters->options().pie())
9927           // This member holds the absolute address of the debug pointer.
9928           odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9929         else
9930           // This member holds the offset to the debug pointer,
9931           // relative to the address of the tag.
9932           odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9933       }
9934   }
9935 }
9936 
9937 // Get the custom dynamic tag value.
9938 template<int size, bool big_endian>
9939 unsigned int
do_dynamic_tag_custom_value(elfcpp::DT tag) const9940 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9941 {
9942   switch (tag)
9943     {
9944     case elfcpp::DT_MIPS_BASE_ADDRESS:
9945       {
9946         // The base address of the segment.
9947         // At this point, the segment list has been sorted into final order,
9948         // so just return vaddr of the first readable PT_LOAD segment.
9949         Output_segment* seg =
9950           this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9951         gold_assert(seg != NULL);
9952         return seg->vaddr();
9953       }
9954 
9955     case elfcpp::DT_MIPS_SYMTABNO:
9956       // The number of entries in the .dynsym section.
9957       return this->get_dt_mips_symtabno();
9958 
9959     case elfcpp::DT_MIPS_GOTSYM:
9960       {
9961         // The index of the first dynamic symbol table entry that corresponds
9962         // to an entry in the GOT.
9963         if (this->got_->first_global_got_dynsym_index() != -1U)
9964           return this->got_->first_global_got_dynsym_index();
9965         else
9966           // In case if we don't have global GOT symbols we default to setting
9967           // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9968           return this->get_dt_mips_symtabno();
9969       }
9970 
9971     case elfcpp::DT_MIPS_RLD_MAP_REL:
9972       {
9973         // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9974         // relative to the address of the tag.
9975         Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9976         unsigned int entry_offset =
9977           odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9978         gold_assert(entry_offset != -1U);
9979         return this->rld_map_->address() - (odyn->address() + entry_offset);
9980       }
9981     default:
9982       gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9983     }
9984 
9985   return (unsigned int)-1;
9986 }
9987 
9988 // Relocate section data.
9989 
9990 template<int size, bool big_endian>
9991 void
relocate_section(const Relocate_info<size,big_endian> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,unsigned char * view,Mips_address address,section_size_type view_size,const Reloc_symbol_changes * reloc_symbol_changes)9992 Target_mips<size, big_endian>::relocate_section(
9993                         const Relocate_info<size, big_endian>* relinfo,
9994                         unsigned int sh_type,
9995                         const unsigned char* prelocs,
9996                         size_t reloc_count,
9997                         Output_section* output_section,
9998                         bool needs_special_offset_handling,
9999                         unsigned char* view,
10000                         Mips_address address,
10001                         section_size_type view_size,
10002                         const Reloc_symbol_changes* reloc_symbol_changes)
10003 {
10004   typedef Target_mips<size, big_endian> Mips;
10005   typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
10006 
10007   if (sh_type == elfcpp::SHT_REL)
10008     {
10009       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10010 	  Classify_reloc;
10011 
10012       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
10013 			     gold::Default_comdat_behavior, Classify_reloc>(
10014 	relinfo,
10015 	this,
10016 	prelocs,
10017 	reloc_count,
10018 	output_section,
10019 	needs_special_offset_handling,
10020 	view,
10021 	address,
10022 	view_size,
10023 	reloc_symbol_changes);
10024     }
10025   else if (sh_type == elfcpp::SHT_RELA)
10026     {
10027       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10028 	  Classify_reloc;
10029 
10030       gold::relocate_section<size, big_endian, Mips, Mips_relocate,
10031 			     gold::Default_comdat_behavior, Classify_reloc>(
10032 	relinfo,
10033 	this,
10034 	prelocs,
10035 	reloc_count,
10036 	output_section,
10037 	needs_special_offset_handling,
10038 	view,
10039 	address,
10040 	view_size,
10041 	reloc_symbol_changes);
10042     }
10043 }
10044 
10045 // Return the size of a relocation while scanning during a relocatable
10046 // link.
10047 
10048 unsigned int
mips_get_size_for_reloc(unsigned int r_type,Relobj * object)10049 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
10050 {
10051   switch (r_type)
10052     {
10053     case elfcpp::R_MIPS_NONE:
10054     case elfcpp::R_MIPS_TLS_DTPMOD64:
10055     case elfcpp::R_MIPS_TLS_DTPREL64:
10056     case elfcpp::R_MIPS_TLS_TPREL64:
10057       return 0;
10058 
10059     case elfcpp::R_MIPS_32:
10060     case elfcpp::R_MIPS_TLS_DTPMOD32:
10061     case elfcpp::R_MIPS_TLS_DTPREL32:
10062     case elfcpp::R_MIPS_TLS_TPREL32:
10063     case elfcpp::R_MIPS_REL32:
10064     case elfcpp::R_MIPS_PC32:
10065     case elfcpp::R_MIPS_GPREL32:
10066     case elfcpp::R_MIPS_JALR:
10067     case elfcpp::R_MIPS_EH:
10068       return 4;
10069 
10070     case elfcpp::R_MIPS_16:
10071     case elfcpp::R_MIPS_HI16:
10072     case elfcpp::R_MIPS_LO16:
10073     case elfcpp::R_MIPS_HIGHER:
10074     case elfcpp::R_MIPS_HIGHEST:
10075     case elfcpp::R_MIPS_GPREL16:
10076     case elfcpp::R_MIPS16_HI16:
10077     case elfcpp::R_MIPS16_LO16:
10078     case elfcpp::R_MIPS_PC16:
10079     case elfcpp::R_MIPS_PCHI16:
10080     case elfcpp::R_MIPS_PCLO16:
10081     case elfcpp::R_MIPS_GOT16:
10082     case elfcpp::R_MIPS16_GOT16:
10083     case elfcpp::R_MIPS_CALL16:
10084     case elfcpp::R_MIPS16_CALL16:
10085     case elfcpp::R_MIPS_GOT_HI16:
10086     case elfcpp::R_MIPS_CALL_HI16:
10087     case elfcpp::R_MIPS_GOT_LO16:
10088     case elfcpp::R_MIPS_CALL_LO16:
10089     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10090     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10091     case elfcpp::R_MIPS_TLS_TPREL_HI16:
10092     case elfcpp::R_MIPS_TLS_TPREL_LO16:
10093     case elfcpp::R_MIPS16_GPREL:
10094     case elfcpp::R_MIPS_GOT_DISP:
10095     case elfcpp::R_MIPS_LITERAL:
10096     case elfcpp::R_MIPS_GOT_PAGE:
10097     case elfcpp::R_MIPS_GOT_OFST:
10098     case elfcpp::R_MIPS_TLS_GD:
10099     case elfcpp::R_MIPS_TLS_LDM:
10100     case elfcpp::R_MIPS_TLS_GOTTPREL:
10101       return 2;
10102 
10103     // These relocations are not byte sized
10104     case elfcpp::R_MIPS_26:
10105     case elfcpp::R_MIPS16_26:
10106     case elfcpp::R_MIPS_PC21_S2:
10107     case elfcpp::R_MIPS_PC26_S2:
10108     case elfcpp::R_MIPS_PC18_S3:
10109     case elfcpp::R_MIPS_PC19_S2:
10110       return 4;
10111 
10112     case elfcpp::R_MIPS_COPY:
10113     case elfcpp::R_MIPS_JUMP_SLOT:
10114       object->error(_("unexpected reloc %u in object file"), r_type);
10115       return 0;
10116 
10117     default:
10118       object->error(_("unsupported reloc %u in object file"), r_type);
10119       return 0;
10120   }
10121 }
10122 
10123 // Scan the relocs during a relocatable link.
10124 
10125 template<int size, bool big_endian>
10126 void
scan_relocatable_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols,Relocatable_relocs * rr)10127 Target_mips<size, big_endian>::scan_relocatable_relocs(
10128                         Symbol_table* symtab,
10129                         Layout* layout,
10130                         Sized_relobj_file<size, big_endian>* object,
10131                         unsigned int data_shndx,
10132                         unsigned int sh_type,
10133                         const unsigned char* prelocs,
10134                         size_t reloc_count,
10135                         Output_section* output_section,
10136                         bool needs_special_offset_handling,
10137                         size_t local_symbol_count,
10138                         const unsigned char* plocal_symbols,
10139                         Relocatable_relocs* rr)
10140 {
10141   if (sh_type == elfcpp::SHT_REL)
10142     {
10143       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10144           Classify_reloc;
10145       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10146           Scan_relocatable_relocs;
10147 
10148       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10149         symtab,
10150         layout,
10151         object,
10152         data_shndx,
10153         prelocs,
10154         reloc_count,
10155         output_section,
10156         needs_special_offset_handling,
10157         local_symbol_count,
10158         plocal_symbols,
10159         rr);
10160     }
10161   else if (sh_type == elfcpp::SHT_RELA)
10162     {
10163       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10164           Classify_reloc;
10165       typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10166           Scan_relocatable_relocs;
10167 
10168       gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10169         symtab,
10170         layout,
10171         object,
10172         data_shndx,
10173         prelocs,
10174         reloc_count,
10175         output_section,
10176         needs_special_offset_handling,
10177         local_symbol_count,
10178         plocal_symbols,
10179         rr);
10180     }
10181   else
10182     gold_unreachable();
10183 }
10184 
10185 // Scan the relocs for --emit-relocs.
10186 
10187 template<int size, bool big_endian>
10188 void
emit_relocs_scan(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_syms,Relocatable_relocs * rr)10189 Target_mips<size, big_endian>::emit_relocs_scan(
10190     Symbol_table* symtab,
10191     Layout* layout,
10192     Sized_relobj_file<size, big_endian>* object,
10193     unsigned int data_shndx,
10194     unsigned int sh_type,
10195     const unsigned char* prelocs,
10196     size_t reloc_count,
10197     Output_section* output_section,
10198     bool needs_special_offset_handling,
10199     size_t local_symbol_count,
10200     const unsigned char* plocal_syms,
10201     Relocatable_relocs* rr)
10202 {
10203   if (sh_type == elfcpp::SHT_REL)
10204     {
10205       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10206           Classify_reloc;
10207       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10208           Emit_relocs_strategy;
10209 
10210       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10211         symtab,
10212         layout,
10213         object,
10214         data_shndx,
10215         prelocs,
10216         reloc_count,
10217         output_section,
10218         needs_special_offset_handling,
10219         local_symbol_count,
10220         plocal_syms,
10221         rr);
10222     }
10223   else if (sh_type == elfcpp::SHT_RELA)
10224     {
10225       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10226           Classify_reloc;
10227       typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10228           Emit_relocs_strategy;
10229 
10230       gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10231         symtab,
10232         layout,
10233         object,
10234         data_shndx,
10235         prelocs,
10236         reloc_count,
10237         output_section,
10238         needs_special_offset_handling,
10239         local_symbol_count,
10240         plocal_syms,
10241         rr);
10242     }
10243   else
10244     gold_unreachable();
10245 }
10246 
10247 // Emit relocations for a section.
10248 
10249 template<int size, bool big_endian>
10250 void
relocate_relocs(const Relocate_info<size,big_endian> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,unsigned char * view,Mips_address view_address,section_size_type view_size,unsigned char * reloc_view,section_size_type reloc_view_size)10251 Target_mips<size, big_endian>::relocate_relocs(
10252                         const Relocate_info<size, big_endian>* relinfo,
10253                         unsigned int sh_type,
10254                         const unsigned char* prelocs,
10255                         size_t reloc_count,
10256                         Output_section* output_section,
10257                         typename elfcpp::Elf_types<size>::Elf_Off
10258                           offset_in_output_section,
10259                         unsigned char* view,
10260                         Mips_address view_address,
10261                         section_size_type view_size,
10262                         unsigned char* reloc_view,
10263                         section_size_type reloc_view_size)
10264 {
10265   if (sh_type == elfcpp::SHT_REL)
10266     {
10267       typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10268           Classify_reloc;
10269 
10270       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10271         relinfo,
10272         prelocs,
10273         reloc_count,
10274         output_section,
10275         offset_in_output_section,
10276         view,
10277         view_address,
10278         view_size,
10279         reloc_view,
10280         reloc_view_size);
10281     }
10282   else if (sh_type == elfcpp::SHT_RELA)
10283     {
10284       typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10285           Classify_reloc;
10286 
10287       gold::relocate_relocs<size, big_endian, Classify_reloc>(
10288         relinfo,
10289         prelocs,
10290         reloc_count,
10291         output_section,
10292         offset_in_output_section,
10293         view,
10294         view_address,
10295         view_size,
10296         reloc_view,
10297         reloc_view_size);
10298     }
10299   else
10300     gold_unreachable();
10301 }
10302 
10303 // Perform target-specific processing in a relocatable link.  This is
10304 // only used if we use the relocation strategy RELOC_SPECIAL.
10305 
10306 template<int size, bool big_endian>
10307 void
relocate_special_relocatable(const Relocate_info<size,big_endian> * relinfo,unsigned int sh_type,const unsigned char * preloc_in,size_t relnum,Output_section * output_section,typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,unsigned char * view,Mips_address view_address,section_size_type,unsigned char * preloc_out)10308 Target_mips<size, big_endian>::relocate_special_relocatable(
10309     const Relocate_info<size, big_endian>* relinfo,
10310     unsigned int sh_type,
10311     const unsigned char* preloc_in,
10312     size_t relnum,
10313     Output_section* output_section,
10314     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10315     unsigned char* view,
10316     Mips_address view_address,
10317     section_size_type,
10318     unsigned char* preloc_out)
10319 {
10320   // We can only handle REL type relocation sections.
10321   gold_assert(sh_type == elfcpp::SHT_REL);
10322 
10323   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10324     Reltype;
10325   typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10326     Reltype_write;
10327 
10328   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10329 
10330   const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10331 
10332   Mips_relobj<size, big_endian>* object =
10333     Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10334   const unsigned int local_count = object->local_symbol_count();
10335 
10336   Reltype reloc(preloc_in);
10337   Reltype_write reloc_write(preloc_out);
10338 
10339   elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10340   const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10341   const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10342 
10343   // Get the new symbol index.
10344   // We only use RELOC_SPECIAL strategy in local relocations.
10345   gold_assert(r_sym < local_count);
10346 
10347   // We are adjusting a section symbol.  We need to find
10348   // the symbol table index of the section symbol for
10349   // the output section corresponding to input section
10350   // in which this symbol is defined.
10351   bool is_ordinary;
10352   unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10353   gold_assert(is_ordinary);
10354   Output_section* os = object->output_section(shndx);
10355   gold_assert(os != NULL);
10356   gold_assert(os->needs_symtab_index());
10357   unsigned int new_symndx = os->symtab_index();
10358 
10359   // Get the new offset--the location in the output section where
10360   // this relocation should be applied.
10361 
10362   Mips_address offset = reloc.get_r_offset();
10363   Mips_address new_offset;
10364   if (offset_in_output_section != invalid_address)
10365     new_offset = offset + offset_in_output_section;
10366   else
10367     {
10368       section_offset_type sot_offset =
10369         convert_types<section_offset_type, Mips_address>(offset);
10370       section_offset_type new_sot_offset =
10371         output_section->output_offset(object, relinfo->data_shndx,
10372                                       sot_offset);
10373       gold_assert(new_sot_offset != -1);
10374       new_offset = new_sot_offset;
10375     }
10376 
10377   // In an object file, r_offset is an offset within the section.
10378   // In an executable or dynamic object, generated by
10379   // --emit-relocs, r_offset is an absolute address.
10380   if (!parameters->options().relocatable())
10381     {
10382       new_offset += view_address;
10383       if (offset_in_output_section != invalid_address)
10384         new_offset -= offset_in_output_section;
10385     }
10386 
10387   reloc_write.put_r_offset(new_offset);
10388   reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10389 
10390   // Handle the reloc addend.
10391   // The relocation uses a section symbol in the input file.
10392   // We are adjusting it to use a section symbol in the output
10393   // file.  The input section symbol refers to some address in
10394   // the input section.  We need the relocation in the output
10395   // file to refer to that same address.  This adjustment to
10396   // the addend is the same calculation we use for a simple
10397   // absolute relocation for the input section symbol.
10398   Valtype calculated_value = 0;
10399   const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10400 
10401   unsigned char* paddend = view + offset;
10402   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10403   switch (r_type)
10404     {
10405     case elfcpp::R_MIPS_26:
10406       reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10407           offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10408           false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10409           false, &calculated_value);
10410       break;
10411 
10412     default:
10413       gold_unreachable();
10414     }
10415 
10416   // Report any errors.
10417   switch (reloc_status)
10418     {
10419     case Reloc_funcs::STATUS_OKAY:
10420       break;
10421     case Reloc_funcs::STATUS_OVERFLOW:
10422       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10423 			     _("relocation overflow: "
10424 			       "%u against local symbol %u in %s"),
10425 			     r_type, r_sym, object->name().c_str());
10426       break;
10427     case Reloc_funcs::STATUS_BAD_RELOC:
10428       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10429         _("unexpected opcode while processing relocation"));
10430       break;
10431     default:
10432       gold_unreachable();
10433     }
10434 }
10435 
10436 // Optimize the TLS relocation type based on what we know about the
10437 // symbol.  IS_FINAL is true if the final address of this symbol is
10438 // known at link time.
10439 
10440 template<int size, bool big_endian>
10441 tls::Tls_optimization
optimize_tls_reloc(bool,int)10442 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10443 {
10444   // FIXME: Currently we do not do any TLS optimization.
10445   return tls::TLSOPT_NONE;
10446 }
10447 
10448 // Scan a relocation for a local symbol.
10449 
10450 template<int size, bool big_endian>
10451 inline void
local(Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const Relatype * rela,const Reltype * rel,unsigned int rel_type,unsigned int r_type,const elfcpp::Sym<size,big_endian> & lsym,bool is_discarded)10452 Target_mips<size, big_endian>::Scan::local(
10453                         Symbol_table* symtab,
10454                         Layout* layout,
10455                         Target_mips<size, big_endian>* target,
10456                         Sized_relobj_file<size, big_endian>* object,
10457                         unsigned int data_shndx,
10458                         Output_section* output_section,
10459                         const Relatype* rela,
10460                         const Reltype* rel,
10461                         unsigned int rel_type,
10462                         unsigned int r_type,
10463                         const elfcpp::Sym<size, big_endian>& lsym,
10464                         bool is_discarded)
10465 {
10466   if (is_discarded)
10467     return;
10468 
10469   Mips_address r_offset;
10470   unsigned int r_sym;
10471   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10472 
10473   if (rel_type == elfcpp::SHT_RELA)
10474     {
10475       r_offset = rela->get_r_offset();
10476       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10477 	  get_r_sym(rela);
10478       r_addend = rela->get_r_addend();
10479     }
10480   else
10481     {
10482       r_offset = rel->get_r_offset();
10483       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10484 	  get_r_sym(rel);
10485       r_addend = 0;
10486     }
10487 
10488   Mips_relobj<size, big_endian>* mips_obj =
10489     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10490 
10491   if (mips_obj->is_mips16_stub_section(data_shndx))
10492     {
10493       mips_obj->get_mips16_stub_section(data_shndx)
10494               ->new_local_reloc_found(r_type, r_sym);
10495     }
10496 
10497   if (r_type == elfcpp::R_MIPS_NONE)
10498     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10499     // mips16 stub.
10500     return;
10501 
10502   if (!mips16_call_reloc(r_type)
10503       && !mips_obj->section_allows_mips16_refs(data_shndx))
10504     // This reloc would need to refer to a MIPS16 hard-float stub, if
10505     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10506     // looking for relocs that would need to refer to MIPS16 stubs.
10507     mips_obj->add_local_non_16bit_call(r_sym);
10508 
10509   if (r_type == elfcpp::R_MIPS16_26
10510       && !mips_obj->section_allows_mips16_refs(data_shndx))
10511     mips_obj->add_local_16bit_call(r_sym);
10512 
10513   switch (r_type)
10514     {
10515     case elfcpp::R_MIPS_GOT16:
10516     case elfcpp::R_MIPS_CALL16:
10517     case elfcpp::R_MIPS_CALL_HI16:
10518     case elfcpp::R_MIPS_CALL_LO16:
10519     case elfcpp::R_MIPS_GOT_HI16:
10520     case elfcpp::R_MIPS_GOT_LO16:
10521     case elfcpp::R_MIPS_GOT_PAGE:
10522     case elfcpp::R_MIPS_GOT_OFST:
10523     case elfcpp::R_MIPS_GOT_DISP:
10524     case elfcpp::R_MIPS_TLS_GOTTPREL:
10525     case elfcpp::R_MIPS_TLS_GD:
10526     case elfcpp::R_MIPS_TLS_LDM:
10527     case elfcpp::R_MIPS16_GOT16:
10528     case elfcpp::R_MIPS16_CALL16:
10529     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10530     case elfcpp::R_MIPS16_TLS_GD:
10531     case elfcpp::R_MIPS16_TLS_LDM:
10532     case elfcpp::R_MICROMIPS_GOT16:
10533     case elfcpp::R_MICROMIPS_CALL16:
10534     case elfcpp::R_MICROMIPS_CALL_HI16:
10535     case elfcpp::R_MICROMIPS_CALL_LO16:
10536     case elfcpp::R_MICROMIPS_GOT_HI16:
10537     case elfcpp::R_MICROMIPS_GOT_LO16:
10538     case elfcpp::R_MICROMIPS_GOT_PAGE:
10539     case elfcpp::R_MICROMIPS_GOT_OFST:
10540     case elfcpp::R_MICROMIPS_GOT_DISP:
10541     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10542     case elfcpp::R_MICROMIPS_TLS_GD:
10543     case elfcpp::R_MICROMIPS_TLS_LDM:
10544     case elfcpp::R_MIPS_EH:
10545       // We need a GOT section.
10546       target->got_section(symtab, layout);
10547       break;
10548 
10549     default:
10550       break;
10551     }
10552 
10553   if (call_lo16_reloc(r_type)
10554       || got_lo16_reloc(r_type)
10555       || got_disp_reloc(r_type)
10556       || eh_reloc(r_type))
10557     {
10558       // We may need a local GOT entry for this relocation.  We
10559       // don't count R_MIPS_GOT_PAGE because we can estimate the
10560       // maximum number of pages needed by looking at the size of
10561       // the segment.  Similar comments apply to R_MIPS*_GOT16 and
10562       // R_MIPS*_CALL16.  We don't count R_MIPS_GOT_HI16, or
10563       // R_MIPS_CALL_HI16 because these are always followed by an
10564       // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10565       Mips_output_data_got<size, big_endian>* got =
10566         target->got_section(symtab, layout);
10567       bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10568       got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10569                                    is_section_symbol);
10570     }
10571 
10572   switch (r_type)
10573     {
10574     case elfcpp::R_MIPS_CALL16:
10575     case elfcpp::R_MIPS16_CALL16:
10576     case elfcpp::R_MICROMIPS_CALL16:
10577       gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10578                  (unsigned long)r_offset);
10579       return;
10580 
10581     case elfcpp::R_MIPS_GOT_PAGE:
10582     case elfcpp::R_MICROMIPS_GOT_PAGE:
10583     case elfcpp::R_MIPS16_GOT16:
10584     case elfcpp::R_MIPS_GOT16:
10585     case elfcpp::R_MIPS_GOT_HI16:
10586     case elfcpp::R_MIPS_GOT_LO16:
10587     case elfcpp::R_MICROMIPS_GOT16:
10588     case elfcpp::R_MICROMIPS_GOT_HI16:
10589     case elfcpp::R_MICROMIPS_GOT_LO16:
10590       {
10591         // This relocation needs a page entry in the GOT.
10592         // Get the section contents.
10593         section_size_type view_size = 0;
10594         const unsigned char* view = object->section_contents(data_shndx,
10595                                                              &view_size, false);
10596         view += r_offset;
10597 
10598         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10599         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10600                                                         : r_addend);
10601 
10602         if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10603           target->got16_addends_.push_back(got16_addend<size, big_endian>(
10604               object, data_shndx, r_type, r_sym, addend));
10605         else
10606           target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10607         break;
10608       }
10609 
10610     case elfcpp::R_MIPS_HI16:
10611     case elfcpp::R_MIPS_PCHI16:
10612     case elfcpp::R_MIPS16_HI16:
10613     case elfcpp::R_MICROMIPS_HI16:
10614       // Record the reloc so that we can check whether the corresponding LO16
10615       // part exists.
10616       if (rel_type == elfcpp::SHT_REL)
10617         target->got16_addends_.push_back(got16_addend<size, big_endian>(
10618             object, data_shndx, r_type, r_sym, 0));
10619       break;
10620 
10621     case elfcpp::R_MIPS_LO16:
10622     case elfcpp::R_MIPS_PCLO16:
10623     case elfcpp::R_MIPS16_LO16:
10624     case elfcpp::R_MICROMIPS_LO16:
10625       {
10626         if (rel_type != elfcpp::SHT_REL)
10627           break;
10628 
10629         // Find corresponding GOT16/HI16 relocation.
10630 
10631         // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10632         // be immediately following.  However, for the IRIX6 ABI, the next
10633         // relocation may be a composed relocation consisting of several
10634         // relocations for the same address.  In that case, the R_MIPS_LO16
10635         // relocation may occur as one of these.  We permit a similar
10636         // extension in general, as that is useful for GCC.
10637 
10638         // In some cases GCC dead code elimination removes the LO16 but
10639         // keeps the corresponding HI16.  This is strictly speaking a
10640         // violation of the ABI but not immediately harmful.
10641 
10642         typename std::list<got16_addend<size, big_endian> >::iterator it =
10643           target->got16_addends_.begin();
10644         while (it != target->got16_addends_.end())
10645           {
10646             got16_addend<size, big_endian> _got16_addend = *it;
10647 
10648             // TODO(sasa): Split got16_addends_ list into two lists - one for
10649             // GOT16 relocs and the other for HI16 relocs.
10650 
10651             // Report an error if we find HI16 or GOT16 reloc from the
10652             // previous section without the matching LO16 part.
10653             if (_got16_addend.object != object
10654                 || _got16_addend.shndx != data_shndx)
10655               {
10656                 gold_error("Can't find matching LO16 reloc");
10657                 break;
10658               }
10659 
10660             if (_got16_addend.r_sym != r_sym
10661                 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10662               {
10663                 ++it;
10664                 continue;
10665               }
10666 
10667             // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10668             // For GOT16, we need to calculate combined addend and record GOT page
10669             // entry.
10670             if (got16_reloc(_got16_addend.r_type))
10671               {
10672 
10673                 section_size_type view_size = 0;
10674                 const unsigned char* view = object->section_contents(data_shndx,
10675                                                                      &view_size,
10676                                                                      false);
10677                 view += r_offset;
10678 
10679                 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10680                 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10681 
10682                 addend = (_got16_addend.addend << 16) + addend;
10683                 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10684                                                              addend);
10685               }
10686 
10687             it = target->got16_addends_.erase(it);
10688           }
10689         break;
10690       }
10691     }
10692 
10693   switch (r_type)
10694     {
10695     case elfcpp::R_MIPS_32:
10696     case elfcpp::R_MIPS_REL32:
10697     case elfcpp::R_MIPS_64:
10698       {
10699         if (parameters->options().output_is_position_independent())
10700           {
10701             // If building a shared library (or a position-independent
10702             // executable), we need to create a dynamic relocation for
10703             // this location.
10704             if (is_readonly_section(output_section))
10705               break;
10706             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10707             rel_dyn->add_symbolless_local_addend(object, r_sym,
10708                                                  elfcpp::R_MIPS_REL32,
10709                                                  output_section, data_shndx,
10710                                                  r_offset);
10711           }
10712         break;
10713       }
10714 
10715     case elfcpp::R_MIPS_TLS_GOTTPREL:
10716     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10717     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10718     case elfcpp::R_MIPS_TLS_LDM:
10719     case elfcpp::R_MIPS16_TLS_LDM:
10720     case elfcpp::R_MICROMIPS_TLS_LDM:
10721     case elfcpp::R_MIPS_TLS_GD:
10722     case elfcpp::R_MIPS16_TLS_GD:
10723     case elfcpp::R_MICROMIPS_TLS_GD:
10724       {
10725         bool output_is_shared = parameters->options().shared();
10726         const tls::Tls_optimization optimized_type
10727             = Target_mips<size, big_endian>::optimize_tls_reloc(
10728                                              !output_is_shared, r_type);
10729         switch (r_type)
10730           {
10731           case elfcpp::R_MIPS_TLS_GD:
10732           case elfcpp::R_MIPS16_TLS_GD:
10733           case elfcpp::R_MICROMIPS_TLS_GD:
10734             if (optimized_type == tls::TLSOPT_NONE)
10735               {
10736                 // Create a pair of GOT entries for the module index and
10737                 // dtv-relative offset.
10738                 Mips_output_data_got<size, big_endian>* got =
10739                   target->got_section(symtab, layout);
10740                 unsigned int shndx = lsym.get_st_shndx();
10741                 bool is_ordinary;
10742                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10743                 if (!is_ordinary)
10744                   {
10745                     object->error(_("local symbol %u has bad shndx %u"),
10746                                   r_sym, shndx);
10747                     break;
10748                   }
10749                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10750                                              shndx, false);
10751               }
10752             else
10753               {
10754                 // FIXME: TLS optimization not supported yet.
10755                 gold_unreachable();
10756               }
10757             break;
10758 
10759           case elfcpp::R_MIPS_TLS_LDM:
10760           case elfcpp::R_MIPS16_TLS_LDM:
10761           case elfcpp::R_MICROMIPS_TLS_LDM:
10762             if (optimized_type == tls::TLSOPT_NONE)
10763               {
10764                 // We always record LDM symbols as local with index 0.
10765                 target->got_section()->record_local_got_symbol(mips_obj, 0,
10766                                                                r_addend, r_type,
10767                                                                -1U, false);
10768               }
10769             else
10770               {
10771                 // FIXME: TLS optimization not supported yet.
10772                 gold_unreachable();
10773               }
10774             break;
10775           case elfcpp::R_MIPS_TLS_GOTTPREL:
10776           case elfcpp::R_MIPS16_TLS_GOTTPREL:
10777           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10778             layout->set_has_static_tls();
10779             if (optimized_type == tls::TLSOPT_NONE)
10780               {
10781                 // Create a GOT entry for the tp-relative offset.
10782                 Mips_output_data_got<size, big_endian>* got =
10783                   target->got_section(symtab, layout);
10784                 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10785                                              -1U, false);
10786               }
10787             else
10788               {
10789                 // FIXME: TLS optimization not supported yet.
10790                 gold_unreachable();
10791               }
10792             break;
10793 
10794           default:
10795             gold_unreachable();
10796         }
10797       }
10798       break;
10799 
10800     default:
10801       break;
10802     }
10803 
10804   // Refuse some position-dependent relocations when creating a
10805   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
10806   // not PIC, but we can create dynamic relocations and the result
10807   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
10808   // combined with R_MIPS_GOT16.
10809   if (parameters->options().shared())
10810     {
10811       switch (r_type)
10812         {
10813         case elfcpp::R_MIPS16_HI16:
10814         case elfcpp::R_MIPS_HI16:
10815         case elfcpp::R_MIPS_HIGHER:
10816         case elfcpp::R_MIPS_HIGHEST:
10817         case elfcpp::R_MICROMIPS_HI16:
10818         case elfcpp::R_MICROMIPS_HIGHER:
10819         case elfcpp::R_MICROMIPS_HIGHEST:
10820           // Don't refuse a high part relocation if it's against
10821           // no symbol (e.g. part of a compound relocation).
10822           if (r_sym == 0)
10823             break;
10824 	  // Fall through.
10825 
10826         case elfcpp::R_MIPS16_26:
10827         case elfcpp::R_MIPS_26:
10828         case elfcpp::R_MICROMIPS_26_S1:
10829           gold_error(_("%s: relocation %u against `%s' can not be used when "
10830                        "making a shared object; recompile with -fPIC"),
10831                      object->name().c_str(), r_type, "a local symbol");
10832         default:
10833           break;
10834         }
10835     }
10836 }
10837 
10838 template<int size, bool big_endian>
10839 inline void
local(Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const Reltype & reloc,unsigned int r_type,const elfcpp::Sym<size,big_endian> & lsym,bool is_discarded)10840 Target_mips<size, big_endian>::Scan::local(
10841                         Symbol_table* symtab,
10842                         Layout* layout,
10843                         Target_mips<size, big_endian>* target,
10844                         Sized_relobj_file<size, big_endian>* object,
10845                         unsigned int data_shndx,
10846                         Output_section* output_section,
10847                         const Reltype& reloc,
10848                         unsigned int r_type,
10849                         const elfcpp::Sym<size, big_endian>& lsym,
10850                         bool is_discarded)
10851 {
10852   if (is_discarded)
10853     return;
10854 
10855   local(
10856     symtab,
10857     layout,
10858     target,
10859     object,
10860     data_shndx,
10861     output_section,
10862     (const Relatype*) NULL,
10863     &reloc,
10864     elfcpp::SHT_REL,
10865     r_type,
10866     lsym, is_discarded);
10867 }
10868 
10869 
10870 template<int size, bool big_endian>
10871 inline void
local(Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const Relatype & reloc,unsigned int r_type,const elfcpp::Sym<size,big_endian> & lsym,bool is_discarded)10872 Target_mips<size, big_endian>::Scan::local(
10873                         Symbol_table* symtab,
10874                         Layout* layout,
10875                         Target_mips<size, big_endian>* target,
10876                         Sized_relobj_file<size, big_endian>* object,
10877                         unsigned int data_shndx,
10878                         Output_section* output_section,
10879                         const Relatype& reloc,
10880                         unsigned int r_type,
10881                         const elfcpp::Sym<size, big_endian>& lsym,
10882                         bool is_discarded)
10883 {
10884   if (is_discarded)
10885     return;
10886 
10887   local(
10888     symtab,
10889     layout,
10890     target,
10891     object,
10892     data_shndx,
10893     output_section,
10894     &reloc,
10895     (const Reltype*) NULL,
10896     elfcpp::SHT_RELA,
10897     r_type,
10898     lsym, is_discarded);
10899 }
10900 
10901 // Scan a relocation for a global symbol.
10902 
10903 template<int size, bool big_endian>
10904 inline void
global(Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const Relatype * rela,const Reltype * rel,unsigned int rel_type,unsigned int r_type,Symbol * gsym)10905 Target_mips<size, big_endian>::Scan::global(
10906                                 Symbol_table* symtab,
10907                                 Layout* layout,
10908                                 Target_mips<size, big_endian>* target,
10909                                 Sized_relobj_file<size, big_endian>* object,
10910                                 unsigned int data_shndx,
10911                                 Output_section* output_section,
10912                                 const Relatype* rela,
10913                                 const Reltype* rel,
10914                                 unsigned int rel_type,
10915                                 unsigned int r_type,
10916                                 Symbol* gsym)
10917 {
10918   Mips_address r_offset;
10919   unsigned int r_sym;
10920   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10921 
10922   if (rel_type == elfcpp::SHT_RELA)
10923     {
10924       r_offset = rela->get_r_offset();
10925       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10926 	  get_r_sym(rela);
10927       r_addend = rela->get_r_addend();
10928     }
10929   else
10930     {
10931       r_offset = rel->get_r_offset();
10932       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10933 	  get_r_sym(rel);
10934       r_addend = 0;
10935     }
10936 
10937   Mips_relobj<size, big_endian>* mips_obj =
10938     Mips_relobj<size, big_endian>::as_mips_relobj(object);
10939   Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10940 
10941   if (mips_obj->is_mips16_stub_section(data_shndx))
10942     {
10943       mips_obj->get_mips16_stub_section(data_shndx)
10944               ->new_global_reloc_found(r_type, mips_sym);
10945     }
10946 
10947   if (r_type == elfcpp::R_MIPS_NONE)
10948     // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10949     // mips16 stub.
10950     return;
10951 
10952   if (!mips16_call_reloc(r_type)
10953       && !mips_obj->section_allows_mips16_refs(data_shndx))
10954     // This reloc would need to refer to a MIPS16 hard-float stub, if
10955     // there is one.  We ignore MIPS16 stub sections and .pdr section when
10956     // looking for relocs that would need to refer to MIPS16 stubs.
10957     mips_sym->set_need_fn_stub();
10958 
10959   // We need PLT entries if there are static-only relocations against
10960   // an externally-defined function.  This can technically occur for
10961   // shared libraries if there are branches to the symbol, although it
10962   // is unlikely that this will be used in practice due to the short
10963   // ranges involved.  It can occur for any relative or absolute relocation
10964   // in executables; in that case, the PLT entry becomes the function's
10965   // canonical address.
10966   bool static_reloc = false;
10967 
10968   // Set CAN_MAKE_DYNAMIC to true if we can convert this
10969   // relocation into a dynamic one.
10970   bool can_make_dynamic = false;
10971   switch (r_type)
10972     {
10973     case elfcpp::R_MIPS_GOT16:
10974     case elfcpp::R_MIPS_CALL16:
10975     case elfcpp::R_MIPS_CALL_HI16:
10976     case elfcpp::R_MIPS_CALL_LO16:
10977     case elfcpp::R_MIPS_GOT_HI16:
10978     case elfcpp::R_MIPS_GOT_LO16:
10979     case elfcpp::R_MIPS_GOT_PAGE:
10980     case elfcpp::R_MIPS_GOT_OFST:
10981     case elfcpp::R_MIPS_GOT_DISP:
10982     case elfcpp::R_MIPS_TLS_GOTTPREL:
10983     case elfcpp::R_MIPS_TLS_GD:
10984     case elfcpp::R_MIPS_TLS_LDM:
10985     case elfcpp::R_MIPS16_GOT16:
10986     case elfcpp::R_MIPS16_CALL16:
10987     case elfcpp::R_MIPS16_TLS_GOTTPREL:
10988     case elfcpp::R_MIPS16_TLS_GD:
10989     case elfcpp::R_MIPS16_TLS_LDM:
10990     case elfcpp::R_MICROMIPS_GOT16:
10991     case elfcpp::R_MICROMIPS_CALL16:
10992     case elfcpp::R_MICROMIPS_CALL_HI16:
10993     case elfcpp::R_MICROMIPS_CALL_LO16:
10994     case elfcpp::R_MICROMIPS_GOT_HI16:
10995     case elfcpp::R_MICROMIPS_GOT_LO16:
10996     case elfcpp::R_MICROMIPS_GOT_PAGE:
10997     case elfcpp::R_MICROMIPS_GOT_OFST:
10998     case elfcpp::R_MICROMIPS_GOT_DISP:
10999     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11000     case elfcpp::R_MICROMIPS_TLS_GD:
11001     case elfcpp::R_MICROMIPS_TLS_LDM:
11002     case elfcpp::R_MIPS_EH:
11003       // We need a GOT section.
11004       target->got_section(symtab, layout);
11005       break;
11006 
11007     // This is just a hint; it can safely be ignored.  Don't set
11008     // has_static_relocs for the corresponding symbol.
11009     case elfcpp::R_MIPS_JALR:
11010     case elfcpp::R_MICROMIPS_JALR:
11011       break;
11012 
11013     case elfcpp::R_MIPS_GPREL16:
11014     case elfcpp::R_MIPS_GPREL32:
11015     case elfcpp::R_MIPS16_GPREL:
11016     case elfcpp::R_MICROMIPS_GPREL16:
11017       // TODO(sasa)
11018       // GP-relative relocations always resolve to a definition in a
11019       // regular input file, ignoring the one-definition rule.  This is
11020       // important for the GP setup sequence in NewABI code, which
11021       // always resolves to a local function even if other relocations
11022       // against the symbol wouldn't.
11023       //constrain_symbol_p = FALSE;
11024       break;
11025 
11026     case elfcpp::R_MIPS_32:
11027     case elfcpp::R_MIPS_REL32:
11028     case elfcpp::R_MIPS_64:
11029       if ((parameters->options().shared()
11030           || (strcmp(gsym->name(), "__gnu_local_gp") != 0
11031           && (!is_readonly_section(output_section)
11032           || mips_obj->is_pic())))
11033           && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
11034         {
11035           if (r_type != elfcpp::R_MIPS_REL32)
11036             mips_sym->set_pointer_equality_needed();
11037           can_make_dynamic = true;
11038           break;
11039         }
11040       // Fall through.
11041 
11042     default:
11043       // Most static relocations require pointer equality, except
11044       // for branches.
11045       mips_sym->set_pointer_equality_needed();
11046       // Fall through.
11047 
11048     case elfcpp::R_MIPS_26:
11049     case elfcpp::R_MIPS_PC16:
11050     case elfcpp::R_MIPS_PC21_S2:
11051     case elfcpp::R_MIPS_PC26_S2:
11052     case elfcpp::R_MIPS16_26:
11053     case elfcpp::R_MICROMIPS_26_S1:
11054     case elfcpp::R_MICROMIPS_PC7_S1:
11055     case elfcpp::R_MICROMIPS_PC10_S1:
11056     case elfcpp::R_MICROMIPS_PC16_S1:
11057     case elfcpp::R_MICROMIPS_PC23_S2:
11058       static_reloc = true;
11059       mips_sym->set_has_static_relocs();
11060       break;
11061     }
11062 
11063   // If there are call relocations against an externally-defined symbol,
11064   // see whether we can create a MIPS lazy-binding stub for it.  We can
11065   // only do this if all references to the function are through call
11066   // relocations, and in that case, the traditional lazy-binding stubs
11067   // are much more efficient than PLT entries.
11068   switch (r_type)
11069     {
11070     case elfcpp::R_MIPS16_CALL16:
11071     case elfcpp::R_MIPS_CALL16:
11072     case elfcpp::R_MIPS_CALL_HI16:
11073     case elfcpp::R_MIPS_CALL_LO16:
11074     case elfcpp::R_MIPS_JALR:
11075     case elfcpp::R_MICROMIPS_CALL16:
11076     case elfcpp::R_MICROMIPS_CALL_HI16:
11077     case elfcpp::R_MICROMIPS_CALL_LO16:
11078     case elfcpp::R_MICROMIPS_JALR:
11079       if (!mips_sym->no_lazy_stub())
11080         {
11081           if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11082               // Calls from shared objects to undefined symbols of type
11083               // STT_NOTYPE need lazy-binding stub.
11084               || (mips_sym->is_undefined() && parameters->options().shared()))
11085             target->mips_stubs_section(layout)->make_entry(mips_sym);
11086         }
11087       break;
11088     default:
11089       {
11090         // We must not create a stub for a symbol that has relocations
11091         // related to taking the function's address.
11092         mips_sym->set_no_lazy_stub();
11093         target->remove_lazy_stub_entry(mips_sym);
11094         break;
11095       }
11096   }
11097 
11098   if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11099                                                    mips_sym->is_mips16()))
11100     mips_sym->set_has_nonpic_branches();
11101 
11102   // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11103   // and has a special meaning.
11104   bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11105                                && strcmp(gsym->name(), "_gp_disp") == 0
11106                                && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11107   if (static_reloc && gsym->needs_plt_entry())
11108     {
11109       target->make_plt_entry(symtab, layout, mips_sym, r_type);
11110 
11111       // Since this is not a PC-relative relocation, we may be
11112       // taking the address of a function.  In that case we need to
11113       // set the entry in the dynamic symbol table to the address of
11114       // the PLT entry.
11115       if (gsym->is_from_dynobj() && !parameters->options().shared())
11116         {
11117           gsym->set_needs_dynsym_value();
11118           // We distinguish between PLT entries and lazy-binding stubs by
11119           // giving the former an st_other value of STO_MIPS_PLT.  Set the
11120           // flag if there are any relocations in the binary where pointer
11121           // equality matters.
11122           if (mips_sym->pointer_equality_needed())
11123             mips_sym->set_mips_plt();
11124         }
11125     }
11126   if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11127     {
11128       // Absolute addressing relocations.
11129       // Make a dynamic relocation if necessary.
11130       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11131         {
11132           if (gsym->may_need_copy_reloc())
11133             {
11134               target->copy_reloc(symtab, layout, object, data_shndx,
11135                                  output_section, gsym, r_type, r_offset);
11136             }
11137           else if (can_make_dynamic)
11138             {
11139               // Create .rel.dyn section.
11140               target->rel_dyn_section(layout);
11141               target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11142                                     data_shndx, output_section, r_offset);
11143             }
11144           else
11145             gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11146                        gsym->name());
11147         }
11148     }
11149 
11150   bool for_call = false;
11151   switch (r_type)
11152     {
11153     case elfcpp::R_MIPS_CALL16:
11154     case elfcpp::R_MIPS16_CALL16:
11155     case elfcpp::R_MICROMIPS_CALL16:
11156     case elfcpp::R_MIPS_CALL_HI16:
11157     case elfcpp::R_MIPS_CALL_LO16:
11158     case elfcpp::R_MICROMIPS_CALL_HI16:
11159     case elfcpp::R_MICROMIPS_CALL_LO16:
11160       for_call = true;
11161       // Fall through.
11162 
11163     case elfcpp::R_MIPS16_GOT16:
11164     case elfcpp::R_MIPS_GOT16:
11165     case elfcpp::R_MIPS_GOT_HI16:
11166     case elfcpp::R_MIPS_GOT_LO16:
11167     case elfcpp::R_MICROMIPS_GOT16:
11168     case elfcpp::R_MICROMIPS_GOT_HI16:
11169     case elfcpp::R_MICROMIPS_GOT_LO16:
11170     case elfcpp::R_MIPS_GOT_DISP:
11171     case elfcpp::R_MICROMIPS_GOT_DISP:
11172     case elfcpp::R_MIPS_EH:
11173       {
11174         // The symbol requires a GOT entry.
11175         Mips_output_data_got<size, big_endian>* got =
11176           target->got_section(symtab, layout);
11177         got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11178                                       for_call);
11179         mips_sym->set_global_got_area(GGA_NORMAL);
11180       }
11181       break;
11182 
11183     case elfcpp::R_MIPS_GOT_PAGE:
11184     case elfcpp::R_MICROMIPS_GOT_PAGE:
11185       {
11186         // This relocation needs a page entry in the GOT.
11187         // Get the section contents.
11188         section_size_type view_size = 0;
11189         const unsigned char* view =
11190           object->section_contents(data_shndx, &view_size, false);
11191         view += r_offset;
11192 
11193         Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11194         Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11195                                                         : r_addend);
11196         Mips_output_data_got<size, big_endian>* got =
11197           target->got_section(symtab, layout);
11198         got->record_got_page_entry(mips_obj, r_sym, addend);
11199 
11200         // If this is a global, overridable symbol, GOT_PAGE will
11201         // decay to GOT_DISP, so we'll need a GOT entry for it.
11202         bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11203                             && !mips_sym->object()->is_dynamic()
11204                             && !mips_sym->is_undefined());
11205         if (!def_regular
11206             || (parameters->options().output_is_position_independent()
11207                 && !parameters->options().Bsymbolic()
11208                 && !mips_sym->is_forced_local()))
11209           {
11210             got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11211                                           for_call);
11212             mips_sym->set_global_got_area(GGA_NORMAL);
11213           }
11214       }
11215       break;
11216 
11217     case elfcpp::R_MIPS_TLS_GOTTPREL:
11218     case elfcpp::R_MIPS16_TLS_GOTTPREL:
11219     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11220     case elfcpp::R_MIPS_TLS_LDM:
11221     case elfcpp::R_MIPS16_TLS_LDM:
11222     case elfcpp::R_MICROMIPS_TLS_LDM:
11223     case elfcpp::R_MIPS_TLS_GD:
11224     case elfcpp::R_MIPS16_TLS_GD:
11225     case elfcpp::R_MICROMIPS_TLS_GD:
11226       {
11227         const bool is_final = gsym->final_value_is_known();
11228         const tls::Tls_optimization optimized_type =
11229           Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11230 
11231         switch (r_type)
11232           {
11233           case elfcpp::R_MIPS_TLS_GD:
11234           case elfcpp::R_MIPS16_TLS_GD:
11235           case elfcpp::R_MICROMIPS_TLS_GD:
11236             if (optimized_type == tls::TLSOPT_NONE)
11237               {
11238                 // Create a pair of GOT entries for the module index and
11239                 // dtv-relative offset.
11240                 Mips_output_data_got<size, big_endian>* got =
11241                   target->got_section(symtab, layout);
11242                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11243                                               false);
11244               }
11245             else
11246               {
11247                 // FIXME: TLS optimization not supported yet.
11248                 gold_unreachable();
11249               }
11250             break;
11251 
11252           case elfcpp::R_MIPS_TLS_LDM:
11253           case elfcpp::R_MIPS16_TLS_LDM:
11254           case elfcpp::R_MICROMIPS_TLS_LDM:
11255             if (optimized_type == tls::TLSOPT_NONE)
11256               {
11257                 // We always record LDM symbols as local with index 0.
11258                 target->got_section()->record_local_got_symbol(mips_obj, 0,
11259                                                                r_addend, r_type,
11260                                                                -1U, false);
11261               }
11262             else
11263               {
11264                 // FIXME: TLS optimization not supported yet.
11265                 gold_unreachable();
11266               }
11267             break;
11268           case elfcpp::R_MIPS_TLS_GOTTPREL:
11269           case elfcpp::R_MIPS16_TLS_GOTTPREL:
11270           case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11271             layout->set_has_static_tls();
11272             if (optimized_type == tls::TLSOPT_NONE)
11273               {
11274                 // Create a GOT entry for the tp-relative offset.
11275                 Mips_output_data_got<size, big_endian>* got =
11276                   target->got_section(symtab, layout);
11277                 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11278                                               false);
11279               }
11280             else
11281               {
11282                 // FIXME: TLS optimization not supported yet.
11283                 gold_unreachable();
11284               }
11285             break;
11286 
11287           default:
11288             gold_unreachable();
11289         }
11290       }
11291       break;
11292     case elfcpp::R_MIPS_COPY:
11293     case elfcpp::R_MIPS_JUMP_SLOT:
11294       // These are relocations which should only be seen by the
11295       // dynamic linker, and should never be seen here.
11296       gold_error(_("%s: unexpected reloc %u in object file"),
11297                  object->name().c_str(), r_type);
11298       break;
11299 
11300     default:
11301       break;
11302     }
11303 
11304   // Refuse some position-dependent relocations when creating a
11305   // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
11306   // not PIC, but we can create dynamic relocations and the result
11307   // will be fine.  Also do not refuse R_MIPS_LO16, which can be
11308   // combined with R_MIPS_GOT16.
11309   if (parameters->options().shared())
11310     {
11311       switch (r_type)
11312         {
11313         case elfcpp::R_MIPS16_HI16:
11314         case elfcpp::R_MIPS_HI16:
11315         case elfcpp::R_MIPS_HIGHER:
11316         case elfcpp::R_MIPS_HIGHEST:
11317         case elfcpp::R_MICROMIPS_HI16:
11318         case elfcpp::R_MICROMIPS_HIGHER:
11319         case elfcpp::R_MICROMIPS_HIGHEST:
11320           // Don't refuse a high part relocation if it's against
11321           // no symbol (e.g. part of a compound relocation).
11322           if (r_sym == 0)
11323             break;
11324 
11325           // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11326           // and has a special meaning.
11327           if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11328             break;
11329 	  // Fall through.
11330 
11331         case elfcpp::R_MIPS16_26:
11332         case elfcpp::R_MIPS_26:
11333         case elfcpp::R_MICROMIPS_26_S1:
11334           gold_error(_("%s: relocation %u against `%s' can not be used when "
11335                        "making a shared object; recompile with -fPIC"),
11336                      object->name().c_str(), r_type, gsym->name());
11337         default:
11338           break;
11339         }
11340     }
11341 }
11342 
11343 template<int size, bool big_endian>
11344 inline void
global(Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const Relatype & reloc,unsigned int r_type,Symbol * gsym)11345 Target_mips<size, big_endian>::Scan::global(
11346                                 Symbol_table* symtab,
11347                                 Layout* layout,
11348                                 Target_mips<size, big_endian>* target,
11349                                 Sized_relobj_file<size, big_endian>* object,
11350                                 unsigned int data_shndx,
11351                                 Output_section* output_section,
11352                                 const Relatype& reloc,
11353                                 unsigned int r_type,
11354                                 Symbol* gsym)
11355 {
11356   global(
11357     symtab,
11358     layout,
11359     target,
11360     object,
11361     data_shndx,
11362     output_section,
11363     &reloc,
11364     (const Reltype*) NULL,
11365     elfcpp::SHT_RELA,
11366     r_type,
11367     gsym);
11368 }
11369 
11370 template<int size, bool big_endian>
11371 inline void
global(Symbol_table * symtab,Layout * layout,Target_mips<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const Reltype & reloc,unsigned int r_type,Symbol * gsym)11372 Target_mips<size, big_endian>::Scan::global(
11373                                 Symbol_table* symtab,
11374                                 Layout* layout,
11375                                 Target_mips<size, big_endian>* target,
11376                                 Sized_relobj_file<size, big_endian>* object,
11377                                 unsigned int data_shndx,
11378                                 Output_section* output_section,
11379                                 const Reltype& reloc,
11380                                 unsigned int r_type,
11381                                 Symbol* gsym)
11382 {
11383   global(
11384     symtab,
11385     layout,
11386     target,
11387     object,
11388     data_shndx,
11389     output_section,
11390     (const Relatype*) NULL,
11391     &reloc,
11392     elfcpp::SHT_REL,
11393     r_type,
11394     gsym);
11395 }
11396 
11397 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11398 // In cases where Scan::local() or Scan::global() has created
11399 // a dynamic relocation, the addend of the relocation is carried
11400 // in the data, and we must not apply the static relocation.
11401 
11402 template<int size, bool big_endian>
11403 inline bool
should_apply_static_reloc(const Mips_symbol<size> * gsym,unsigned int r_type,Output_section * output_section,Target_mips * target)11404 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11405     const Mips_symbol<size>* gsym,
11406     unsigned int r_type,
11407     Output_section* output_section,
11408     Target_mips* target)
11409 {
11410   // If the output section is not allocated, then we didn't call
11411   // scan_relocs, we didn't create a dynamic reloc, and we must apply
11412   // the reloc here.
11413   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11414       return true;
11415 
11416   if (gsym == NULL)
11417     return true;
11418   else
11419     {
11420       // For global symbols, we use the same helper routines used in the
11421       // scan pass.
11422       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11423           && !gsym->may_need_copy_reloc())
11424         {
11425           // We have generated dynamic reloc (R_MIPS_REL32).
11426 
11427           bool multi_got = false;
11428           if (target->has_got_section())
11429             multi_got = target->got_section()->multi_got();
11430           bool has_got_offset;
11431           if (!multi_got)
11432             has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11433           else
11434             has_got_offset = gsym->global_gotoffset() != -1U;
11435           if (!has_got_offset)
11436             return true;
11437           else
11438             // Apply the relocation only if the symbol is in the local got.
11439             // Do not apply the relocation if the symbol is in the global
11440             // got.
11441             return symbol_references_local(gsym, gsym->has_dynsym_index());
11442         }
11443       else
11444         // We have not generated dynamic reloc.
11445         return true;
11446     }
11447 }
11448 
11449 // Perform a relocation.
11450 
11451 template<int size, bool big_endian>
11452 inline bool
relocate(const Relocate_info<size,big_endian> * relinfo,unsigned int rel_type,Target_mips * target,Output_section * output_section,size_t relnum,const unsigned char * preloc,const Sized_symbol<size> * gsym,const Symbol_value<size> * psymval,unsigned char * view,Mips_address address,section_size_type)11453 Target_mips<size, big_endian>::Relocate::relocate(
11454                         const Relocate_info<size, big_endian>* relinfo,
11455                         unsigned int rel_type,
11456                         Target_mips* target,
11457                         Output_section* output_section,
11458                         size_t relnum,
11459                         const unsigned char* preloc,
11460                         const Sized_symbol<size>* gsym,
11461                         const Symbol_value<size>* psymval,
11462                         unsigned char* view,
11463                         Mips_address address,
11464                         section_size_type)
11465 {
11466   Mips_address r_offset;
11467   unsigned int r_sym;
11468   unsigned int r_type;
11469   unsigned int r_type2;
11470   unsigned int r_type3;
11471   unsigned char r_ssym;
11472   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11473   // r_offset and r_type of the next relocation is needed for resolving multiple
11474   // consecutive relocations with the same offset.
11475   Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11476   unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11477 
11478   elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11479   size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
11480 
11481   if (rel_type == elfcpp::SHT_RELA)
11482     {
11483       const Relatype rela(preloc);
11484       r_offset = rela.get_r_offset();
11485       r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11486 	  get_r_sym(&rela);
11487       r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11488 	  get_r_type(&rela);
11489       r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11490           get_r_type2(&rela);
11491       r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11492           get_r_type3(&rela);
11493       r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11494           get_r_ssym(&rela);
11495       r_addend = rela.get_r_addend();
11496       // If this is not last relocation, get r_offset and r_type of the next
11497       // relocation.
11498       if (relnum + 1 < reloc_count)
11499         {
11500           const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11501           const Relatype next_rela(preloc + reloc_size);
11502           next_r_offset = next_rela.get_r_offset();
11503           next_r_type =
11504             Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11505               get_r_type(&next_rela);
11506         }
11507     }
11508   else
11509     {
11510       const Reltype rel(preloc);
11511       r_offset = rel.get_r_offset();
11512       r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11513 	  get_r_sym(&rel);
11514       r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11515 	  get_r_type(&rel);
11516       r_ssym = 0;
11517       r_type2 = elfcpp::R_MIPS_NONE;
11518       r_type3 = elfcpp::R_MIPS_NONE;
11519       r_addend = 0;
11520       // If this is not last relocation, get r_offset and r_type of the next
11521       // relocation.
11522       if (relnum + 1 < reloc_count)
11523         {
11524           const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11525           const Reltype next_rel(preloc + reloc_size);
11526           next_r_offset = next_rel.get_r_offset();
11527           next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11528             get_r_type(&next_rel);
11529         }
11530     }
11531 
11532   typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11533   typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11534 
11535   Mips_relobj<size, big_endian>* object =
11536       Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11537 
11538   bool target_is_16_bit_code = false;
11539   bool target_is_micromips_code = false;
11540   bool cross_mode_jump;
11541 
11542   Symbol_value<size> symval;
11543 
11544   const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11545 
11546   bool changed_symbol_value = false;
11547   if (gsym == NULL)
11548     {
11549       target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11550       target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11551       if (target_is_16_bit_code || target_is_micromips_code)
11552         {
11553           // MIPS16/microMIPS text labels should be treated as odd.
11554           symval.set_output_value(psymval->value(object, 1));
11555           psymval = &symval;
11556           changed_symbol_value = true;
11557         }
11558     }
11559   else
11560     {
11561       target_is_16_bit_code = mips_sym->is_mips16();
11562       target_is_micromips_code = mips_sym->is_micromips();
11563 
11564       // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11565       // it odd.  This will cause something like .word SYM to come up with
11566       // the right value when it is loaded into the PC.
11567 
11568       if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11569           && psymval->value(object, 0) != 0)
11570         {
11571           symval.set_output_value(psymval->value(object, 0) | 1);
11572           psymval = &symval;
11573           changed_symbol_value = true;
11574         }
11575 
11576       // Pick the value to use for symbols defined in shared objects.
11577       if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11578           || mips_sym->has_lazy_stub())
11579         {
11580           Mips_address value;
11581           if (!mips_sym->has_lazy_stub())
11582             {
11583               // Prefer a standard MIPS PLT entry.
11584               if (mips_sym->has_mips_plt_offset())
11585                 {
11586                   value = target->plt_section()->mips_entry_address(mips_sym);
11587                   target_is_micromips_code = false;
11588                   target_is_16_bit_code = false;
11589                 }
11590               else
11591                 {
11592                   value = (target->plt_section()->comp_entry_address(mips_sym)
11593                            + 1);
11594                   if (target->is_output_micromips())
11595                     target_is_micromips_code = true;
11596                   else
11597                     target_is_16_bit_code = true;
11598                 }
11599             }
11600           else
11601             value = target->mips_stubs_section()->stub_address(mips_sym);
11602 
11603           symval.set_output_value(value);
11604           psymval = &symval;
11605         }
11606     }
11607 
11608   // TRUE if the symbol referred to by this relocation is "_gp_disp".
11609   // Note that such a symbol must always be a global symbol.
11610   bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11611                   && !object->is_newabi());
11612 
11613   // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11614   // Note that such a symbol must always be a global symbol.
11615   bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11616 
11617 
11618   if (gp_disp)
11619     {
11620       if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11621         gold_error_at_location(relinfo, relnum, r_offset,
11622           _("relocations against _gp_disp are permitted only"
11623             " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11624     }
11625   else if (gnu_local_gp)
11626     {
11627       // __gnu_local_gp is _gp symbol.
11628       symval.set_output_value(target->adjusted_gp_value(object));
11629       psymval = &symval;
11630     }
11631 
11632   // If this is a reference to a 16-bit function with a stub, we need
11633   // to redirect the relocation to the stub unless:
11634   //
11635   // (a) the relocation is for a MIPS16 JAL;
11636   //
11637   // (b) the relocation is for a MIPS16 PIC call, and there are no
11638   //     non-MIPS16 uses of the GOT slot; or
11639   //
11640   // (c) the section allows direct references to MIPS16 functions.
11641   if (r_type != elfcpp::R_MIPS16_26
11642       && ((mips_sym != NULL
11643            && mips_sym->has_mips16_fn_stub()
11644            && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11645           || (mips_sym == NULL
11646               && object->get_local_mips16_fn_stub(r_sym) != NULL))
11647       && !object->section_allows_mips16_refs(relinfo->data_shndx))
11648     {
11649       // This is a 32- or 64-bit call to a 16-bit function.  We should
11650       // have already noticed that we were going to need the
11651       // stub.
11652       Mips_address value;
11653       if (mips_sym == NULL)
11654         value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11655       else
11656         {
11657           gold_assert(mips_sym->need_fn_stub());
11658           if (mips_sym->has_la25_stub())
11659             value = target->la25_stub_section()->stub_address(mips_sym);
11660           else
11661             {
11662               value = mips_sym->template
11663                       get_mips16_fn_stub<big_endian>()->output_address();
11664             }
11665           }
11666       symval.set_output_value(value);
11667       psymval = &symval;
11668       changed_symbol_value = true;
11669 
11670       // The target is 16-bit, but the stub isn't.
11671       target_is_16_bit_code = false;
11672     }
11673   // If this is a MIPS16 call with a stub, that is made through the PLT or
11674   // to a standard MIPS function, we need to redirect the call to the stub.
11675   // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11676   // indirect calls should use an indirect stub instead.
11677   else if (r_type == elfcpp::R_MIPS16_26
11678            && ((mips_sym != NULL
11679                 && (mips_sym->has_mips16_call_stub()
11680                     || mips_sym->has_mips16_call_fp_stub()))
11681                || (mips_sym == NULL
11682                    && object->get_local_mips16_call_stub(r_sym) != NULL))
11683            && ((mips_sym != NULL && mips_sym->has_plt_offset())
11684                || !target_is_16_bit_code))
11685     {
11686       Mips16_stub_section<size, big_endian>* call_stub;
11687       if (mips_sym == NULL)
11688         call_stub = object->get_local_mips16_call_stub(r_sym);
11689       else
11690         {
11691           // If both call_stub and call_fp_stub are defined, we can figure
11692           // out which one to use by checking which one appears in the input
11693           // file.
11694           if (mips_sym->has_mips16_call_stub()
11695               && mips_sym->has_mips16_call_fp_stub())
11696             {
11697               call_stub = NULL;
11698               for (unsigned int i = 1; i < object->shnum(); ++i)
11699                 {
11700                   if (object->is_mips16_call_fp_stub_section(i))
11701                     {
11702                       call_stub = mips_sym->template
11703                                   get_mips16_call_fp_stub<big_endian>();
11704                       break;
11705                     }
11706 
11707                 }
11708               if (call_stub == NULL)
11709                 call_stub =
11710                   mips_sym->template get_mips16_call_stub<big_endian>();
11711             }
11712           else if (mips_sym->has_mips16_call_stub())
11713             call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11714           else
11715             call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11716         }
11717 
11718       symval.set_output_value(call_stub->output_address());
11719       psymval = &symval;
11720       changed_symbol_value = true;
11721     }
11722   // If this is a direct call to a PIC function, redirect to the
11723   // non-PIC stub.
11724   else if (mips_sym != NULL
11725            && mips_sym->has_la25_stub()
11726            && relocation_needs_la25_stub<size, big_endian>(
11727                                        object, r_type, target_is_16_bit_code))
11728     {
11729       Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11730       if (mips_sym->is_micromips())
11731         value += 1;
11732       symval.set_output_value(value);
11733       psymval = &symval;
11734     }
11735   // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11736   // entry is used if a standard PLT entry has also been made.
11737   else if ((r_type == elfcpp::R_MIPS16_26
11738             || r_type == elfcpp::R_MICROMIPS_26_S1)
11739           && mips_sym != NULL
11740           && mips_sym->has_plt_offset()
11741           && mips_sym->has_comp_plt_offset()
11742           && mips_sym->has_mips_plt_offset())
11743     {
11744       Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11745                             + 1);
11746       symval.set_output_value(value);
11747       psymval = &symval;
11748 
11749       target_is_16_bit_code = !target->is_output_micromips();
11750       target_is_micromips_code = target->is_output_micromips();
11751     }
11752 
11753   // Make sure MIPS16 and microMIPS are not used together.
11754   if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11755       || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11756    {
11757       gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11758    }
11759 
11760   // Calls from 16-bit code to 32-bit code and vice versa require the
11761   // mode change.  However, we can ignore calls to undefined weak symbols,
11762   // which should never be executed at runtime.  This exception is important
11763   // because the assembly writer may have "known" that any definition of the
11764   // symbol would be 16-bit code, and that direct jumps were therefore
11765   // acceptable.
11766   cross_mode_jump =
11767     (!(gsym != NULL && gsym->is_weak_undefined())
11768      && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11769          || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11770          || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11771              && (target_is_16_bit_code || target_is_micromips_code))));
11772 
11773   bool local = (mips_sym == NULL
11774                 || (mips_sym->got_only_for_calls()
11775                     ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11776                     : symbol_references_local(mips_sym,
11777                                               mips_sym->has_dynsym_index())));
11778 
11779   // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11780   // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
11781   // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11782   if (got_page_reloc(r_type) && !local)
11783     r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11784                                       : elfcpp::R_MIPS_GOT_DISP);
11785 
11786   unsigned int got_offset = 0;
11787   int gp_offset = 0;
11788 
11789   // Whether we have to extract addend from instruction.
11790   bool extract_addend = rel_type == elfcpp::SHT_REL;
11791   unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11792 
11793   Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11794 
11795   // For Mips64 N64 ABI, there may be up to three operations specified per
11796   // record, by the fields r_type, r_type2, and r_type3. The first operation
11797   // takes its addend from the relocation record. Each subsequent operation
11798   // takes as its addend the result of the previous operation.
11799   // The first operation in a record which references a symbol uses the symbol
11800   // implied by r_sym. The next operation in a record which references a symbol
11801   // uses the special symbol value given by the r_ssym field. A third operation
11802   // in a record which references a symbol will assume a NULL symbol,
11803   // i.e. value zero.
11804 
11805   // TODO(Vladimir)
11806   // Check if a record references to a symbol.
11807   for (unsigned int i = 0; i < 3; ++i)
11808     {
11809       if (r_types[i] == elfcpp::R_MIPS_NONE)
11810         break;
11811 
11812       // If we didn't apply previous relocation, use its result as addend
11813       // for current.
11814       if (this->calculate_only_)
11815         {
11816           r_addend = this->calculated_value_;
11817           extract_addend = false;
11818         }
11819 
11820       // In the N32 and 64-bit ABIs there may be multiple consecutive
11821       // relocations for the same offset.  In that case we are
11822       // supposed to treat the output of each relocation as the addend
11823       // for the next.  For N64 ABI, we are checking offsets only in a
11824       // third operation in a record (r_type3).
11825       this->calculate_only_ =
11826         (object->is_n64() && i < 2
11827          ? r_types[i+1] != elfcpp::R_MIPS_NONE
11828          : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
11829 
11830       if (object->is_n64())
11831         {
11832           if (i == 1)
11833             {
11834               // Handle special symbol for r_type2 relocation type.
11835               switch (r_ssym)
11836                 {
11837                 case RSS_UNDEF:
11838                   symval.set_output_value(0);
11839                   break;
11840                 case RSS_GP:
11841                   symval.set_output_value(target->gp_value());
11842                   break;
11843                 case RSS_GP0:
11844                   symval.set_output_value(object->gp_value());
11845                   break;
11846                 case RSS_LOC:
11847                   symval.set_output_value(address);
11848                   break;
11849                 default:
11850                   gold_unreachable();
11851                 }
11852               psymval = &symval;
11853             }
11854           else if (i == 2)
11855            {
11856             // For r_type3 symbol value is 0.
11857             symval.set_output_value(0);
11858            }
11859         }
11860 
11861       bool update_got_entry = false;
11862       switch (r_types[i])
11863         {
11864         case elfcpp::R_MIPS_NONE:
11865           break;
11866         case elfcpp::R_MIPS_16:
11867           reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11868                                             extract_addend,
11869                                             this->calculate_only_,
11870                                             &this->calculated_value_);
11871           break;
11872 
11873         case elfcpp::R_MIPS_32:
11874           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11875                                         target))
11876             reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11877                                               extract_addend,
11878                                               this->calculate_only_,
11879                                               &this->calculated_value_);
11880           if (mips_sym != NULL
11881               && (mips_sym->is_mips16() || mips_sym->is_micromips())
11882               && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11883             {
11884               // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11885               // already updated by adding +1.
11886               if (mips_sym->has_mips16_fn_stub())
11887                 {
11888                   gold_assert(mips_sym->need_fn_stub());
11889                   Mips16_stub_section<size, big_endian>* fn_stub =
11890                     mips_sym->template get_mips16_fn_stub<big_endian>();
11891 
11892                   symval.set_output_value(fn_stub->output_address());
11893                   psymval = &symval;
11894                 }
11895               got_offset = mips_sym->global_gotoffset();
11896               update_got_entry = true;
11897             }
11898           break;
11899 
11900         case elfcpp::R_MIPS_64:
11901           if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11902                                         target))
11903             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11904                                               extract_addend,
11905                                               this->calculate_only_,
11906                                               &this->calculated_value_, false);
11907           else if (target->is_output_n64() && r_addend != 0)
11908             // Only apply the addend.  The static relocation was RELA, but the
11909             // dynamic relocation is REL, so we need to apply the addend.
11910             reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11911                                               extract_addend,
11912                                               this->calculate_only_,
11913                                               &this->calculated_value_, true);
11914           break;
11915         case elfcpp::R_MIPS_REL32:
11916           gold_unreachable();
11917 
11918         case elfcpp::R_MIPS_PC32:
11919           reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11920                                               r_addend, extract_addend,
11921                                               this->calculate_only_,
11922                                               &this->calculated_value_);
11923           break;
11924 
11925         case elfcpp::R_MIPS16_26:
11926           // The calculation for R_MIPS16_26 is just the same as for an
11927           // R_MIPS_26.  It's only the storage of the relocated field into
11928           // the output file that's different.  So, we just fall through to the
11929           // R_MIPS_26 case here.
11930         case elfcpp::R_MIPS_26:
11931         case elfcpp::R_MICROMIPS_26_S1:
11932           reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11933               gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11934               r_types[i], target->jal_to_bal(), this->calculate_only_,
11935               &this->calculated_value_);
11936           break;
11937 
11938         case elfcpp::R_MIPS_HI16:
11939         case elfcpp::R_MIPS16_HI16:
11940         case elfcpp::R_MICROMIPS_HI16:
11941           if (rel_type == elfcpp::SHT_RELA)
11942             reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11943                                                    r_addend, address,
11944                                                    gp_disp, r_types[i],
11945                                                    extract_addend, 0,
11946                                                    target,
11947                                                    this->calculate_only_,
11948                                                    &this->calculated_value_);
11949           else if (rel_type == elfcpp::SHT_REL)
11950             reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11951                                                 address, gp_disp, r_types[i],
11952                                                 r_sym, extract_addend);
11953           else
11954             gold_unreachable();
11955           break;
11956 
11957         case elfcpp::R_MIPS_LO16:
11958         case elfcpp::R_MIPS16_LO16:
11959         case elfcpp::R_MICROMIPS_LO16:
11960         case elfcpp::R_MICROMIPS_HI0_LO16:
11961           reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11962                                               r_addend, extract_addend, address,
11963                                               gp_disp, r_types[i], r_sym,
11964                                               rel_type, this->calculate_only_,
11965                                               &this->calculated_value_);
11966           break;
11967 
11968         case elfcpp::R_MIPS_LITERAL:
11969         case elfcpp::R_MICROMIPS_LITERAL:
11970           // Because we don't merge literal sections, we can handle this
11971           // just like R_MIPS_GPREL16.  In the long run, we should merge
11972           // shared literals, and then we will need to additional work
11973           // here.
11974 
11975           // Fall through.
11976 
11977         case elfcpp::R_MIPS_GPREL16:
11978         case elfcpp::R_MIPS16_GPREL:
11979         case elfcpp::R_MICROMIPS_GPREL16:
11980           reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11981                                              target->adjusted_gp_value(object),
11982                                              r_addend, extract_addend,
11983                                              gsym == NULL,
11984                                              this->calculate_only_,
11985                                              &this->calculated_value_);
11986           break;
11987 
11988         case elfcpp::R_MICROMIPS_GPREL7_S2:
11989           reloc_status = Reloc_funcs::relgprel7(view, object, psymval,
11990                                              target->adjusted_gp_value(object),
11991                                              r_addend, extract_addend,
11992                                              gsym == NULL,
11993                                              this->calculate_only_,
11994                                              &this->calculated_value_);
11995           break;
11996 
11997         case elfcpp::R_MIPS_PC16:
11998           reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11999                                               r_addend, extract_addend,
12000                                               this->calculate_only_,
12001                                               &this->calculated_value_);
12002           break;
12003 
12004         case elfcpp::R_MIPS_PC21_S2:
12005           reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
12006                                               r_addend, extract_addend,
12007                                               this->calculate_only_,
12008                                               &this->calculated_value_);
12009           break;
12010 
12011         case elfcpp::R_MIPS_PC26_S2:
12012           reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
12013                                               r_addend, extract_addend,
12014                                               this->calculate_only_,
12015                                               &this->calculated_value_);
12016           break;
12017 
12018         case elfcpp::R_MIPS_PC18_S3:
12019           reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
12020                                               r_addend, extract_addend,
12021                                               this->calculate_only_,
12022                                               &this->calculated_value_);
12023           break;
12024 
12025         case elfcpp::R_MIPS_PC19_S2:
12026           reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
12027                                               r_addend, extract_addend,
12028                                               this->calculate_only_,
12029                                               &this->calculated_value_);
12030           break;
12031 
12032         case elfcpp::R_MIPS_PCHI16:
12033           if (rel_type == elfcpp::SHT_RELA)
12034             reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
12035                                                      r_addend, address,
12036                                                      extract_addend, 0,
12037                                                      this->calculate_only_,
12038                                                      &this->calculated_value_);
12039           else if (rel_type == elfcpp::SHT_REL)
12040             reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
12041                                                   r_addend, address, r_sym,
12042                                                   extract_addend);
12043           else
12044             gold_unreachable();
12045           break;
12046 
12047         case elfcpp::R_MIPS_PCLO16:
12048           reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
12049                                                 extract_addend, address, r_sym,
12050                                                 rel_type, this->calculate_only_,
12051                                                 &this->calculated_value_);
12052           break;
12053         case elfcpp::R_MICROMIPS_PC7_S1:
12054           reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
12055                                                       address, r_addend,
12056                                                       extract_addend,
12057                                                       this->calculate_only_,
12058                                                       &this->calculated_value_);
12059           break;
12060         case elfcpp::R_MICROMIPS_PC10_S1:
12061           reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
12062                                                       psymval, address,
12063                                                       r_addend, extract_addend,
12064                                                       this->calculate_only_,
12065                                                       &this->calculated_value_);
12066           break;
12067         case elfcpp::R_MICROMIPS_PC16_S1:
12068           reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
12069                                                       psymval, address,
12070                                                       r_addend, extract_addend,
12071                                                       this->calculate_only_,
12072                                                       &this->calculated_value_);
12073           break;
12074         case elfcpp::R_MIPS_GPREL32:
12075           reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12076                                               target->adjusted_gp_value(object),
12077                                               r_addend, extract_addend,
12078                                               this->calculate_only_,
12079                                               &this->calculated_value_);
12080           break;
12081         case elfcpp::R_MIPS_GOT_HI16:
12082         case elfcpp::R_MIPS_CALL_HI16:
12083         case elfcpp::R_MICROMIPS_GOT_HI16:
12084         case elfcpp::R_MICROMIPS_CALL_HI16:
12085           if (gsym != NULL)
12086             got_offset = target->got_section()->got_offset(gsym,
12087                                                            GOT_TYPE_STANDARD,
12088                                                            object);
12089           else
12090             got_offset = target->got_section()->got_offset(r_sym,
12091                                                            GOT_TYPE_STANDARD,
12092                                                            object, r_addend);
12093           gp_offset = target->got_section()->gp_offset(got_offset, object);
12094           reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
12095                                                   this->calculate_only_,
12096                                                   &this->calculated_value_);
12097           update_got_entry = changed_symbol_value;
12098           break;
12099 
12100         case elfcpp::R_MIPS_GOT_LO16:
12101         case elfcpp::R_MIPS_CALL_LO16:
12102         case elfcpp::R_MICROMIPS_GOT_LO16:
12103         case elfcpp::R_MICROMIPS_CALL_LO16:
12104           if (gsym != NULL)
12105             got_offset = target->got_section()->got_offset(gsym,
12106                                                            GOT_TYPE_STANDARD,
12107                                                            object);
12108           else
12109             got_offset = target->got_section()->got_offset(r_sym,
12110                                                            GOT_TYPE_STANDARD,
12111                                                            object, r_addend);
12112           gp_offset = target->got_section()->gp_offset(got_offset, object);
12113           reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
12114                                                   this->calculate_only_,
12115                                                   &this->calculated_value_);
12116           update_got_entry = changed_symbol_value;
12117           break;
12118 
12119         case elfcpp::R_MIPS_GOT_DISP:
12120         case elfcpp::R_MICROMIPS_GOT_DISP:
12121         case elfcpp::R_MIPS_EH:
12122           if (gsym != NULL)
12123             got_offset = target->got_section()->got_offset(gsym,
12124                                                            GOT_TYPE_STANDARD,
12125                                                            object);
12126           else
12127             got_offset = target->got_section()->got_offset(r_sym,
12128                                                            GOT_TYPE_STANDARD,
12129                                                            object, r_addend);
12130           gp_offset = target->got_section()->gp_offset(got_offset, object);
12131           if (eh_reloc(r_types[i]))
12132             reloc_status = Reloc_funcs::releh(view, gp_offset,
12133                                               this->calculate_only_,
12134                                               &this->calculated_value_);
12135           else
12136             reloc_status = Reloc_funcs::relgot(view, gp_offset,
12137                                                this->calculate_only_,
12138                                                &this->calculated_value_);
12139           break;
12140         case elfcpp::R_MIPS_CALL16:
12141         case elfcpp::R_MIPS16_CALL16:
12142         case elfcpp::R_MICROMIPS_CALL16:
12143           gold_assert(gsym != NULL);
12144           got_offset = target->got_section()->got_offset(gsym,
12145                                                          GOT_TYPE_STANDARD,
12146                                                          object);
12147           gp_offset = target->got_section()->gp_offset(got_offset, object);
12148           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12149                                              this->calculate_only_,
12150                                              &this->calculated_value_);
12151           // TODO(sasa): We should also initialize update_got_entry
12152           // in other place swhere relgot is called.
12153           update_got_entry = changed_symbol_value;
12154           break;
12155 
12156         case elfcpp::R_MIPS_GOT16:
12157         case elfcpp::R_MIPS16_GOT16:
12158         case elfcpp::R_MICROMIPS_GOT16:
12159           if (gsym != NULL)
12160             {
12161               got_offset = target->got_section()->got_offset(gsym,
12162                                                              GOT_TYPE_STANDARD,
12163                                                              object);
12164               gp_offset = target->got_section()->gp_offset(got_offset, object);
12165               reloc_status = Reloc_funcs::relgot(view, gp_offset,
12166                                                  this->calculate_only_,
12167                                                  &this->calculated_value_);
12168             }
12169           else
12170             {
12171               if (rel_type == elfcpp::SHT_RELA)
12172                 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
12173                                                       psymval, r_addend,
12174                                                       extract_addend, 0,
12175                                                       target,
12176                                                       this->calculate_only_,
12177                                                       &this->calculated_value_);
12178               else if (rel_type == elfcpp::SHT_REL)
12179                 reloc_status = Reloc_funcs::relgot16_local(view, object,
12180                                                            psymval, r_addend,
12181                                                            extract_addend,
12182                                                            r_types[i], r_sym);
12183               else
12184                 gold_unreachable();
12185             }
12186           update_got_entry = changed_symbol_value;
12187           break;
12188 
12189         case elfcpp::R_MIPS_TLS_GD:
12190         case elfcpp::R_MIPS16_TLS_GD:
12191         case elfcpp::R_MICROMIPS_TLS_GD:
12192           if (gsym != NULL)
12193             got_offset = target->got_section()->got_offset(gsym,
12194                                                            GOT_TYPE_TLS_PAIR,
12195                                                            object);
12196           else
12197             got_offset = target->got_section()->got_offset(r_sym,
12198                                                            GOT_TYPE_TLS_PAIR,
12199                                                            object, r_addend);
12200           gp_offset = target->got_section()->gp_offset(got_offset, object);
12201           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12202                                              this->calculate_only_,
12203                                              &this->calculated_value_);
12204           break;
12205 
12206         case elfcpp::R_MIPS_TLS_GOTTPREL:
12207         case elfcpp::R_MIPS16_TLS_GOTTPREL:
12208         case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12209           if (gsym != NULL)
12210             got_offset = target->got_section()->got_offset(gsym,
12211                                                            GOT_TYPE_TLS_OFFSET,
12212                                                            object);
12213           else
12214             got_offset = target->got_section()->got_offset(r_sym,
12215                                                            GOT_TYPE_TLS_OFFSET,
12216                                                            object, r_addend);
12217           gp_offset = target->got_section()->gp_offset(got_offset, object);
12218           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12219                                              this->calculate_only_,
12220                                              &this->calculated_value_);
12221           break;
12222 
12223         case elfcpp::R_MIPS_TLS_LDM:
12224         case elfcpp::R_MIPS16_TLS_LDM:
12225         case elfcpp::R_MICROMIPS_TLS_LDM:
12226           // Relocate the field with the offset of the GOT entry for
12227           // the module index.
12228           got_offset = target->got_section()->tls_ldm_offset(object);
12229           gp_offset = target->got_section()->gp_offset(got_offset, object);
12230           reloc_status = Reloc_funcs::relgot(view, gp_offset,
12231                                              this->calculate_only_,
12232                                              &this->calculated_value_);
12233           break;
12234 
12235         case elfcpp::R_MIPS_GOT_PAGE:
12236         case elfcpp::R_MICROMIPS_GOT_PAGE:
12237           reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12238                                                  r_addend, extract_addend,
12239                                                  this->calculate_only_,
12240                                                  &this->calculated_value_);
12241           break;
12242 
12243         case elfcpp::R_MIPS_GOT_OFST:
12244         case elfcpp::R_MICROMIPS_GOT_OFST:
12245           reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12246                                                  r_addend, extract_addend,
12247                                                  local, this->calculate_only_,
12248                                                  &this->calculated_value_);
12249           break;
12250 
12251         case elfcpp::R_MIPS_JALR:
12252         case elfcpp::R_MICROMIPS_JALR:
12253           // This relocation is only a hint.  In some cases, we optimize
12254           // it into a bal instruction.  But we don't try to optimize
12255           // when the symbol does not resolve locally.
12256           if (gsym == NULL
12257               || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12258             reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12259                                                 r_addend, extract_addend,
12260                                                 cross_mode_jump, r_types[i],
12261                                                 target->jalr_to_bal(),
12262                                                 target->jr_to_b(),
12263                                                 this->calculate_only_,
12264                                                 &this->calculated_value_);
12265           break;
12266 
12267         case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12268         case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12269         case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12270           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12271                                                  elfcpp::DTP_OFFSET, r_addend,
12272                                                  extract_addend,
12273                                                  this->calculate_only_,
12274                                                  &this->calculated_value_);
12275           break;
12276         case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12277         case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12278         case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12279           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12280                                                  elfcpp::DTP_OFFSET, r_addend,
12281                                                  extract_addend,
12282                                                  this->calculate_only_,
12283                                                  &this->calculated_value_);
12284           break;
12285         case elfcpp::R_MIPS_TLS_DTPREL32:
12286         case elfcpp::R_MIPS_TLS_DTPREL64:
12287           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12288                                                elfcpp::DTP_OFFSET, r_addend,
12289                                                extract_addend,
12290                                                this->calculate_only_,
12291                                                &this->calculated_value_);
12292           break;
12293         case elfcpp::R_MIPS_TLS_TPREL_HI16:
12294         case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12295         case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12296           reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12297                                                  elfcpp::TP_OFFSET, r_addend,
12298                                                  extract_addend,
12299                                                  this->calculate_only_,
12300                                                  &this->calculated_value_);
12301           break;
12302         case elfcpp::R_MIPS_TLS_TPREL_LO16:
12303         case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12304         case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12305           reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12306                                                  elfcpp::TP_OFFSET, r_addend,
12307                                                  extract_addend,
12308                                                  this->calculate_only_,
12309                                                  &this->calculated_value_);
12310           break;
12311         case elfcpp::R_MIPS_TLS_TPREL32:
12312         case elfcpp::R_MIPS_TLS_TPREL64:
12313           reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12314                                                elfcpp::TP_OFFSET, r_addend,
12315                                                extract_addend,
12316                                                this->calculate_only_,
12317                                                &this->calculated_value_);
12318           break;
12319         case elfcpp::R_MIPS_SUB:
12320         case elfcpp::R_MICROMIPS_SUB:
12321           reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12322                                              extract_addend,
12323                                              this->calculate_only_,
12324                                              &this->calculated_value_);
12325           break;
12326         case elfcpp::R_MIPS_HIGHER:
12327         case elfcpp::R_MICROMIPS_HIGHER:
12328           reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
12329                                                 extract_addend,
12330                                                 this->calculate_only_,
12331                                                 &this->calculated_value_);
12332           break;
12333         case elfcpp::R_MIPS_HIGHEST:
12334         case elfcpp::R_MICROMIPS_HIGHEST:
12335           reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12336                                                  r_addend, extract_addend,
12337                                                  this->calculate_only_,
12338                                                  &this->calculated_value_);
12339           break;
12340         default:
12341           gold_error_at_location(relinfo, relnum, r_offset,
12342                                  _("unsupported reloc %u"), r_types[i]);
12343           break;
12344         }
12345 
12346       if (update_got_entry)
12347         {
12348           Mips_output_data_got<size, big_endian>* got = target->got_section();
12349           if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12350             got->update_got_entry(got->get_primary_got_offset(mips_sym),
12351                                   psymval->value(object, 0));
12352           else
12353             got->update_got_entry(got_offset, psymval->value(object, 0));
12354         }
12355     }
12356 
12357   bool jal_shuffle = jal_reloc(r_type);
12358   Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12359 
12360   // Report any errors.
12361   switch (reloc_status)
12362     {
12363     case Reloc_funcs::STATUS_OKAY:
12364       break;
12365     case Reloc_funcs::STATUS_OVERFLOW:
12366       if (gsym == NULL)
12367         gold_error_at_location(relinfo, relnum, r_offset,
12368                                _("relocation overflow: "
12369                                  "%u against local symbol %u in %s"),
12370                                r_type, r_sym, object->name().c_str());
12371       else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12372         gold_error_at_location(relinfo, relnum, r_offset,
12373                                _("relocation overflow: "
12374                                  "%u against '%s' defined in %s"),
12375                                r_type, gsym->demangled_name().c_str(),
12376                                gsym->object()->name().c_str());
12377       else
12378         gold_error_at_location(relinfo, relnum, r_offset,
12379                                _("relocation overflow: %u against '%s'"),
12380                                r_type, gsym->demangled_name().c_str());
12381       break;
12382     case Reloc_funcs::STATUS_BAD_RELOC:
12383       gold_error_at_location(relinfo, relnum, r_offset,
12384         _("unexpected opcode while processing relocation"));
12385       break;
12386     case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12387       gold_error_at_location(relinfo, relnum, r_offset,
12388         _("unaligned PC-relative relocation"));
12389       break;
12390     default:
12391       gold_unreachable();
12392     }
12393 
12394   return true;
12395 }
12396 
12397 // Get the Reference_flags for a particular relocation.
12398 
12399 template<int size, bool big_endian>
12400 int
get_reference_flags(unsigned int r_type)12401 Target_mips<size, big_endian>::Scan::get_reference_flags(
12402                        unsigned int r_type)
12403 {
12404   switch (r_type)
12405     {
12406     case elfcpp::R_MIPS_NONE:
12407       // No symbol reference.
12408       return 0;
12409 
12410     case elfcpp::R_MIPS_16:
12411     case elfcpp::R_MIPS_32:
12412     case elfcpp::R_MIPS_64:
12413     case elfcpp::R_MIPS_HI16:
12414     case elfcpp::R_MIPS_LO16:
12415     case elfcpp::R_MIPS_HIGHER:
12416     case elfcpp::R_MIPS_HIGHEST:
12417     case elfcpp::R_MIPS16_HI16:
12418     case elfcpp::R_MIPS16_LO16:
12419     case elfcpp::R_MICROMIPS_HI16:
12420     case elfcpp::R_MICROMIPS_LO16:
12421     case elfcpp::R_MICROMIPS_HIGHER:
12422     case elfcpp::R_MICROMIPS_HIGHEST:
12423       return Symbol::ABSOLUTE_REF;
12424 
12425     case elfcpp::R_MIPS_26:
12426     case elfcpp::R_MIPS16_26:
12427     case elfcpp::R_MICROMIPS_26_S1:
12428       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12429 
12430     case elfcpp::R_MIPS_PC18_S3:
12431     case elfcpp::R_MIPS_PC19_S2:
12432     case elfcpp::R_MIPS_PCHI16:
12433     case elfcpp::R_MIPS_PCLO16:
12434     case elfcpp::R_MIPS_GPREL32:
12435     case elfcpp::R_MIPS_GPREL16:
12436     case elfcpp::R_MIPS_REL32:
12437     case elfcpp::R_MIPS16_GPREL:
12438       return Symbol::RELATIVE_REF;
12439 
12440     case elfcpp::R_MIPS_PC16:
12441     case elfcpp::R_MIPS_PC32:
12442     case elfcpp::R_MIPS_PC21_S2:
12443     case elfcpp::R_MIPS_PC26_S2:
12444     case elfcpp::R_MIPS_JALR:
12445     case elfcpp::R_MICROMIPS_JALR:
12446       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12447 
12448     case elfcpp::R_MIPS_GOT16:
12449     case elfcpp::R_MIPS_CALL16:
12450     case elfcpp::R_MIPS_GOT_DISP:
12451     case elfcpp::R_MIPS_GOT_HI16:
12452     case elfcpp::R_MIPS_GOT_LO16:
12453     case elfcpp::R_MIPS_CALL_HI16:
12454     case elfcpp::R_MIPS_CALL_LO16:
12455     case elfcpp::R_MIPS_LITERAL:
12456     case elfcpp::R_MIPS_GOT_PAGE:
12457     case elfcpp::R_MIPS_GOT_OFST:
12458     case elfcpp::R_MIPS16_GOT16:
12459     case elfcpp::R_MIPS16_CALL16:
12460     case elfcpp::R_MICROMIPS_GOT16:
12461     case elfcpp::R_MICROMIPS_CALL16:
12462     case elfcpp::R_MICROMIPS_GOT_HI16:
12463     case elfcpp::R_MICROMIPS_GOT_LO16:
12464     case elfcpp::R_MICROMIPS_CALL_HI16:
12465     case elfcpp::R_MICROMIPS_CALL_LO16:
12466     case elfcpp::R_MIPS_EH:
12467       // Absolute in GOT.
12468       return Symbol::RELATIVE_REF;
12469 
12470     case elfcpp::R_MIPS_TLS_DTPMOD32:
12471     case elfcpp::R_MIPS_TLS_DTPREL32:
12472     case elfcpp::R_MIPS_TLS_DTPMOD64:
12473     case elfcpp::R_MIPS_TLS_DTPREL64:
12474     case elfcpp::R_MIPS_TLS_GD:
12475     case elfcpp::R_MIPS_TLS_LDM:
12476     case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12477     case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12478     case elfcpp::R_MIPS_TLS_GOTTPREL:
12479     case elfcpp::R_MIPS_TLS_TPREL32:
12480     case elfcpp::R_MIPS_TLS_TPREL64:
12481     case elfcpp::R_MIPS_TLS_TPREL_HI16:
12482     case elfcpp::R_MIPS_TLS_TPREL_LO16:
12483     case elfcpp::R_MIPS16_TLS_GD:
12484     case elfcpp::R_MIPS16_TLS_GOTTPREL:
12485     case elfcpp::R_MICROMIPS_TLS_GD:
12486     case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12487     case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12488     case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12489       return Symbol::TLS_REF;
12490 
12491     case elfcpp::R_MIPS_COPY:
12492     case elfcpp::R_MIPS_JUMP_SLOT:
12493     default:
12494       // Not expected.  We will give an error later.
12495       return 0;
12496     }
12497 }
12498 
12499 // Report an unsupported relocation against a local symbol.
12500 
12501 template<int size, bool big_endian>
12502 void
unsupported_reloc_local(Sized_relobj_file<size,big_endian> * object,unsigned int r_type)12503 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12504                         Sized_relobj_file<size, big_endian>* object,
12505                         unsigned int r_type)
12506 {
12507   gold_error(_("%s: unsupported reloc %u against local symbol"),
12508              object->name().c_str(), r_type);
12509 }
12510 
12511 // Report an unsupported relocation against a global symbol.
12512 
12513 template<int size, bool big_endian>
12514 void
unsupported_reloc_global(Sized_relobj_file<size,big_endian> * object,unsigned int r_type,Symbol * gsym)12515 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12516                         Sized_relobj_file<size, big_endian>* object,
12517                         unsigned int r_type,
12518                         Symbol* gsym)
12519 {
12520   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12521              object->name().c_str(), r_type, gsym->demangled_name().c_str());
12522 }
12523 
12524 // Return printable name for ABI.
12525 template<int size, bool big_endian>
12526 const char*
elf_mips_abi_name(elfcpp::Elf_Word e_flags)12527 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12528 {
12529   switch (e_flags & elfcpp::EF_MIPS_ABI)
12530     {
12531     case 0:
12532       if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12533         return "N32";
12534       else if (size == 64)
12535         return "64";
12536       else
12537         return "none";
12538     case elfcpp::EF_MIPS_ABI_O32:
12539       return "O32";
12540     case elfcpp::EF_MIPS_ABI_O64:
12541       return "O64";
12542     case elfcpp::EF_MIPS_ABI_EABI32:
12543       return "EABI32";
12544     case elfcpp::EF_MIPS_ABI_EABI64:
12545       return "EABI64";
12546     default:
12547       return "unknown abi";
12548     }
12549 }
12550 
12551 template<int size, bool big_endian>
12552 const char*
elf_mips_mach_name(elfcpp::Elf_Word e_flags)12553 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12554 {
12555   switch (e_flags & elfcpp::EF_MIPS_MACH)
12556     {
12557     case elfcpp::EF_MIPS_MACH_3900:
12558       return "mips:3900";
12559     case elfcpp::EF_MIPS_MACH_4010:
12560       return "mips:4010";
12561     case elfcpp::EF_MIPS_MACH_4100:
12562       return "mips:4100";
12563     case elfcpp::EF_MIPS_MACH_4111:
12564       return "mips:4111";
12565     case elfcpp::EF_MIPS_MACH_4120:
12566       return "mips:4120";
12567     case elfcpp::EF_MIPS_MACH_4650:
12568       return "mips:4650";
12569     case elfcpp::EF_MIPS_MACH_5400:
12570       return "mips:5400";
12571     case elfcpp::EF_MIPS_MACH_5500:
12572       return "mips:5500";
12573     case elfcpp::EF_MIPS_MACH_5900:
12574       return "mips:5900";
12575     case elfcpp::EF_MIPS_MACH_SB1:
12576       return "mips:sb1";
12577     case elfcpp::EF_MIPS_MACH_9000:
12578       return "mips:9000";
12579     case elfcpp::EF_MIPS_MACH_LS2E:
12580       return "mips:loongson_2e";
12581     case elfcpp::EF_MIPS_MACH_LS2F:
12582       return "mips:loongson_2f";
12583     case elfcpp::EF_MIPS_MACH_GS464:
12584       return "mips:gs464";
12585     case elfcpp::EF_MIPS_MACH_GS464E:
12586       return "mips:gs464e";
12587     case elfcpp::EF_MIPS_MACH_GS264E:
12588       return "mips:gs264e";
12589     case elfcpp::EF_MIPS_MACH_OCTEON:
12590       return "mips:octeon";
12591     case elfcpp::EF_MIPS_MACH_OCTEON2:
12592       return "mips:octeon2";
12593     case elfcpp::EF_MIPS_MACH_OCTEON3:
12594       return "mips:octeon3";
12595     case elfcpp::EF_MIPS_MACH_XLR:
12596       return "mips:xlr";
12597     default:
12598       switch (e_flags & elfcpp::EF_MIPS_ARCH)
12599         {
12600         default:
12601         case elfcpp::EF_MIPS_ARCH_1:
12602           return "mips:3000";
12603 
12604         case elfcpp::EF_MIPS_ARCH_2:
12605           return "mips:6000";
12606 
12607         case elfcpp::EF_MIPS_ARCH_3:
12608           return "mips:4000";
12609 
12610         case elfcpp::EF_MIPS_ARCH_4:
12611           return "mips:8000";
12612 
12613         case elfcpp::EF_MIPS_ARCH_5:
12614           return "mips:mips5";
12615 
12616         case elfcpp::EF_MIPS_ARCH_32:
12617           return "mips:isa32";
12618 
12619         case elfcpp::EF_MIPS_ARCH_64:
12620           return "mips:isa64";
12621 
12622         case elfcpp::EF_MIPS_ARCH_32R2:
12623           return "mips:isa32r2";
12624 
12625         case elfcpp::EF_MIPS_ARCH_32R6:
12626           return "mips:isa32r6";
12627 
12628         case elfcpp::EF_MIPS_ARCH_64R2:
12629           return "mips:isa64r2";
12630 
12631         case elfcpp::EF_MIPS_ARCH_64R6:
12632           return "mips:isa64r6";
12633         }
12634     }
12635     return "unknown CPU";
12636 }
12637 
12638 template<int size, bool big_endian>
12639 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12640 {
12641   size,                 // size
12642   big_endian,           // is_big_endian
12643   elfcpp::EM_MIPS,      // machine_code
12644   true,                 // has_make_symbol
12645   false,                // has_resolve
12646   false,                // has_code_fill
12647   true,                 // is_default_stack_executable
12648   false,                // can_icf_inline_merge_sections
12649   '\0',                 // wrap_char
12650   size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1",      // dynamic_linker
12651   0x400000,             // default_text_segment_address
12652   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
12653   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
12654   false,                // isolate_execinstr
12655   0,                    // rosegment_gap
12656   elfcpp::SHN_UNDEF,    // small_common_shndx
12657   elfcpp::SHN_UNDEF,    // large_common_shndx
12658   0,                    // small_common_section_flags
12659   0,                    // large_common_section_flags
12660   NULL,                 // attributes_section
12661   NULL,                 // attributes_vendor
12662   "__start",		// entry_symbol_name
12663   32,			// hash_entry_size
12664   elfcpp::SHT_PROGBITS,	// unwind_section_type
12665 };
12666 
12667 template<int size, bool big_endian>
12668 class Target_mips_nacl : public Target_mips<size, big_endian>
12669 {
12670  public:
Target_mips_nacl()12671   Target_mips_nacl()
12672     : Target_mips<size, big_endian>(&mips_nacl_info)
12673   { }
12674 
12675  private:
12676   static const Target::Target_info mips_nacl_info;
12677 };
12678 
12679 template<int size, bool big_endian>
12680 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12681 {
12682   size,                 // size
12683   big_endian,           // is_big_endian
12684   elfcpp::EM_MIPS,      // machine_code
12685   true,                 // has_make_symbol
12686   false,                // has_resolve
12687   false,                // has_code_fill
12688   true,                 // is_default_stack_executable
12689   false,                // can_icf_inline_merge_sections
12690   '\0',                 // wrap_char
12691   "/lib/ld.so.1",       // dynamic_linker
12692   0x20000,              // default_text_segment_address
12693   0x10000,              // abi_pagesize (overridable by -z max-page-size)
12694   0x10000,              // common_pagesize (overridable by -z common-page-size)
12695   true,                 // isolate_execinstr
12696   0x10000000,           // rosegment_gap
12697   elfcpp::SHN_UNDEF,    // small_common_shndx
12698   elfcpp::SHN_UNDEF,    // large_common_shndx
12699   0,                    // small_common_section_flags
12700   0,                    // large_common_section_flags
12701   NULL,                 // attributes_section
12702   NULL,                 // attributes_vendor
12703   "_start",             // entry_symbol_name
12704   32,			// hash_entry_size
12705   elfcpp::SHT_PROGBITS,	// unwind_section_type
12706 };
12707 
12708 // Target selector for Mips.  Note this is never instantiated directly.
12709 // It's only used in Target_selector_mips_nacl, below.
12710 
12711 template<int size, bool big_endian>
12712 class Target_selector_mips : public Target_selector
12713 {
12714 public:
Target_selector_mips()12715   Target_selector_mips()
12716     : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12717                 (size == 64 ?
12718                   (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12719                   (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12720                 (size == 64 ?
12721                   (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12722                   (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12723   { }
12724 
do_instantiate_target()12725   Target* do_instantiate_target()
12726   { return new Target_mips<size, big_endian>(); }
12727 };
12728 
12729 template<int size, bool big_endian>
12730 class Target_selector_mips_nacl
12731   : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12732                                 Target_mips_nacl<size, big_endian> >
12733 {
12734  public:
Target_selector_mips_nacl()12735   Target_selector_mips_nacl()
12736     : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12737                            Target_mips_nacl<size, big_endian> >(
12738         // NaCl currently supports only MIPS32 little-endian.
12739         "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12740   { }
12741 };
12742 
12743 Target_selector_mips_nacl<32, true> target_selector_mips32;
12744 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12745 Target_selector_mips_nacl<64, true> target_selector_mips64;
12746 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12747 
12748 } // End anonymous namespace.
12749