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