1 // mips.cc -- mips target support for gold.
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.
8 // This file is part of gold.
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.
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.
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.
33 #include "parameters.h"
40 #include "copy-relocs.h"
42 #include "target-reloc.h"
43 #include "target-select.h"
47 #include "attributes.h"
54 template<int size, bool big_endian>
55 class Mips_output_data_plt;
57 template<int size, bool big_endian>
58 class Mips_output_data_got;
60 template<int size, bool big_endian>
63 template<int size, bool big_endian>
64 class Mips_output_section_reginfo;
66 template<int size, bool big_endian>
67 class Mips_output_section_options;
69 template<int size, bool big_endian>
70 class Mips_output_data_la25_stub;
72 template<int size, bool big_endian>
73 class Mips_output_data_mips_stubs;
78 template<int size, bool big_endian>
81 template<int size, bool big_endian>
84 class Mips16_stub_section_base;
86 template<int size, bool big_endian>
87 class Mips16_stub_section;
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).
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
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.
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.
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
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
130 // TLS type of GOT entry.
139 // Values found in the r_ssym field of a relocation entry.
140 enum Special_relocation_symbol
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.
148 // Whether the section is readonly.
150 is_readonly_section(Output_section* output_section)
152 elfcpp::Elf_Xword section_flags = output_section->flags();
153 elfcpp::Elf_Word section_type = output_section->type();
155 if (section_type == elfcpp::SHT_NOBITS)
158 if (section_flags & elfcpp::SHF_WRITE)
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>
169 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
170 unsigned int r_type, bool target_is_16_bit_code)
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())
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:
193 case elfcpp::R_MIPS16_26:
194 return !target_is_16_bit_code;
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>
208 local_pic_function(Mips_symbol<size>* sym)
210 bool def_regular = (sym->source() == Symbol::FROM_OBJECT
211 && !sym->object()->is_dynamic()
212 && !sym->is_undefined());
214 if (sym->is_defined() && def_regular)
216 Mips_relobj<size, big_endian>* object =
217 static_cast<Mips_relobj<size, big_endian>*>(sym->object());
219 if ((object->is_pic() || sym->is_pic())
220 && (!sym->is_mips16()
221 || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
228 hi16_reloc(int r_type)
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);
237 lo16_reloc(int r_type)
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);
246 got16_reloc(unsigned int r_type)
248 return (r_type == elfcpp::R_MIPS_GOT16
249 || r_type == elfcpp::R_MIPS16_GOT16
250 || r_type == elfcpp::R_MICROMIPS_GOT16);
254 call_lo16_reloc(unsigned int r_type)
256 return (r_type == elfcpp::R_MIPS_CALL_LO16
257 || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
261 got_lo16_reloc(unsigned int r_type)
263 return (r_type == elfcpp::R_MIPS_GOT_LO16
264 || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
268 eh_reloc(unsigned int r_type)
270 return (r_type == elfcpp::R_MIPS_EH);
274 got_disp_reloc(unsigned int r_type)
276 return (r_type == elfcpp::R_MIPS_GOT_DISP
277 || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
281 got_page_reloc(unsigned int r_type)
283 return (r_type == elfcpp::R_MIPS_GOT_PAGE
284 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
288 tls_gd_reloc(unsigned int r_type)
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);
296 tls_gottprel_reloc(unsigned int r_type)
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);
304 tls_ldm_reloc(unsigned int r_type)
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);
312 mips16_call_reloc(unsigned int r_type)
314 return (r_type == elfcpp::R_MIPS16_26
315 || r_type == elfcpp::R_MIPS16_CALL16);
319 jal_reloc(unsigned int r_type)
321 return (r_type == elfcpp::R_MIPS_26
322 || r_type == elfcpp::R_MIPS16_26
323 || r_type == elfcpp::R_MICROMIPS_26_S1);
327 micromips_branch_reloc(unsigned int r_type)
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);
335 // Check if R_TYPE is a MIPS16 reloc.
337 mips16_reloc(unsigned int r_type)
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:
361 // Check if R_TYPE is a microMIPS reloc.
363 micromips_reloc(unsigned int r_type)
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:
407 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
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;
427 // This class is used to hold information about one GOT entry.
428 // There are three types of entry:
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)
437 template<int size, bool big_endian>
440 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
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; }
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; }
455 // Return whether this entry is for a local symbol.
457 is_for_local_symbol() const
458 { return this->symndx_ != -1U; }
460 // Return whether this entry is for a global symbol.
462 is_for_global_symbol() const
463 { return this->symndx_ == -1U; }
465 // Return the hash of this entry.
469 if (this->tls_type_ == GOT_TLS_LDM)
470 return this->symndx_ + (1 << 18);
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);
480 // Return whether this entry is equal to OTHER.
482 equals(Mips_got_entry<size, big_endian>* other) const
484 if (this->symndx_ != other->symndx_
485 || this->tls_type_ != other->tls_type_)
488 if (this->tls_type_ == GOT_TLS_LDM)
491 return (((this->symndx_ != -1U)
492 ? (this->d.object == other->d.object)
493 : (this->d.sym == other->d.sym))
494 && (this->addend_ == other->addend_));
497 // Return input object that needs this GOT entry.
498 Mips_relobj<size, big_endian>*
501 gold_assert(this->symndx_ != -1U);
502 return this->d.object;
505 // Return local symbol index for local GOT entries.
509 gold_assert(this->symndx_ != -1U);
510 return this->symndx_;
513 // Return the relocation addend for local GOT entries.
516 { return this->addend_; }
518 // Return global symbol for global GOT entries.
522 gold_assert(this->symndx_ == -1U);
526 // Return whether this is a TLS GOT entry.
529 { return this->tls_type_ != GOT_TLS_NONE; }
531 // Return TLS type of this GOT entry.
534 { return this->tls_type_; }
536 // Return section index of the local symbol for local GOT entries.
539 { return this->shndx_; }
541 // Return whether this is a STT_SECTION symbol.
543 is_section_symbol() const
544 { return this->is_section_symbol_; }
548 Mips_address addend_;
550 // The index of the symbol if we have a local symbol; -1 otherwise.
551 unsigned int symndx_;
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;
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_;
567 // Whether this is a STT_SECTION symbol.
568 bool is_section_symbol_;
570 // For local GOT entries, section index of the local symbol.
574 // Hash for Mips_got_entry.
576 template<int size, bool big_endian>
577 class Mips_got_entry_hash
581 operator()(Mips_got_entry<size, big_endian>* entry) const
582 { return entry->hash(); }
585 // Equality for Mips_got_entry.
587 template<int size, bool big_endian>
588 class Mips_got_entry_eq
592 operator()(Mips_got_entry<size, big_endian>* e1,
593 Mips_got_entry<size, big_endian>* e2) const
594 { return e1->equals(e2); }
597 // Hash for Mips_symbol.
600 class Mips_symbol_hash
604 operator()(Mips_symbol<size>* sym) const
605 { return sym->hash(); }
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.
612 struct Got_page_range
615 : next(NULL), min_addend(0), max_addend(0)
618 Got_page_range* next;
622 // Return the maximum number of GOT page entries required.
625 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
628 // Got_page_entry. This class describes the range of addends that are applied
629 // to page relocations against a given symbol.
631 struct Got_page_entry
634 : object(NULL), symndx(-1U), ranges(NULL)
637 Got_page_entry(Object* object_, unsigned int symndx_)
638 : object(object_), symndx(symndx_), ranges(NULL)
641 // The input object that needs the GOT page entry.
643 // The index of the symbol, as stored in the relocation r_info.
645 // The ranges for this page entry.
646 Got_page_range* ranges;
649 // Hash for Got_page_entry.
651 struct Got_page_entry_hash
654 operator()(Got_page_entry* entry) const
655 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
658 // Equality for Got_page_entry.
660 struct Got_page_entry_eq
663 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
665 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
669 // This class is used to hold .got information when linking.
671 template<int size, bool big_endian>
674 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
675 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
677 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
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;
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;
688 // Unordered set of global GOT entries.
689 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
690 Global_got_entry_set;
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),
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.
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);
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
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);
718 // Add ENTRY to master GOT and to OBJECT's GOT.
720 record_got_entry(Mips_got_entry<size, big_endian>* entry,
721 Mips_relobj<size, big_endian>* object);
723 // Record that OBJECT has a page relocation against symbol SYMNDX and
724 // that ADDEND is the addend for that relocation.
726 record_got_page_entry(Mips_relobj<size, big_endian>* object,
727 unsigned int symndx, int addend);
729 // Create all entries that should be in the local part of the GOT.
731 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
733 // Create GOT page entries.
735 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
737 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
739 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
740 unsigned int non_reloc_only_global_gotno);
742 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
744 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
746 // Create TLS GOT entries.
748 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
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).
755 count_got_symbols(Symbol_table* symtab);
757 // Return the offset of GOT page entry for VALUE.
759 get_got_page_offset(Mips_address value,
760 Mips_output_data_got<size, big_endian>* got);
762 // Count the number of GOT entries required.
766 // Count the number of GOT entries required by ENTRY. Accumulate the result.
768 count_got_entry(Mips_got_entry<size, big_endian>* entry);
770 // Add FROM's GOT entries.
772 add_got_entries(Mips_got_info<size, big_endian>* from);
774 // Add FROM's GOT page entries.
776 add_got_page_count(Mips_got_info<size, big_endian>* from);
781 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
782 + this->tls_gotno_) * size/8);
785 // Return the number of local GOT entries.
788 { return this->local_gotno_; }
790 // Return the maximum number of page GOT entries needed.
793 { return this->page_gotno_; }
795 // Return the number of global GOT entries.
798 { return this->global_gotno_; }
800 // Set the number of global GOT entries.
802 set_global_gotno(unsigned int global_gotno)
803 { this->global_gotno_ = global_gotno; }
805 // Return the number of GGA_RELOC_ONLY global GOT entries.
807 reloc_only_gotno() const
808 { return this->reloc_only_gotno_; }
810 // Return the number of TLS GOT entries.
813 { return this->tls_gotno_; }
815 // Return the GOT type for this GOT. Used for multi-GOT links only.
817 multigot_got_type(unsigned int got_type) const
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_;
832 // Remove lazy-binding stubs for global symbols in this GOT.
834 remove_lazy_stubs(Target_mips<size, big_endian>* target);
836 // Return offset of this GOT from the start of .got section.
839 { return this->offset_; }
841 // Set offset of this GOT from the start of .got section.
843 set_offset(unsigned int offset)
844 { this->offset_ = offset; }
846 // Set index of this GOT in multi-GOT links.
848 set_index(unsigned int index)
849 { this->index_ = index; }
851 // Return next GOT in multi-GOT links.
852 Mips_got_info<size, big_endian>*
854 { return this->next_; }
856 // Set next GOT in multi-GOT links.
858 set_next(Mips_got_info<size, big_endian>* next)
859 { this->next_ = next; }
861 // Return the offset of TLS LDM entry for this GOT.
863 tls_ldm_offset() const
864 { return this->tls_ldm_offset_; }
866 // Set the offset of TLS LDM entry for this GOT.
868 set_tls_ldm_offset(unsigned int tls_ldm_offset)
869 { this->tls_ldm_offset_ = tls_ldm_offset; }
871 Global_got_entry_set&
873 { return this->global_got_symbols_; }
875 // Return the GOT_TLS_* type required by relocation type R_TYPE.
877 mips_elf_reloc_tls_type(unsigned int r_type)
879 if (tls_gd_reloc(r_type))
882 if (tls_ldm_reloc(r_type))
885 if (tls_gottprel_reloc(r_type))
891 // Return the number of GOT slots needed for GOT TLS type TYPE.
893 mips_tls_got_entries(unsigned int type)
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.
942 // The offset of this GOT in multi-GOT links.
943 unsigned int offset_;
946 // This is a helper class used during relocation scan. It records GOT16 addend.
948 template<int size, bool big_endian>
951 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
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),
960 const Sized_relobj_file<size, big_endian>* object;
967 // .MIPS.abiflags section content
969 template<bool big_endian>
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;
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)
981 // Version of flags structure.
983 // The level of the ISA: 1-5, 32, 64.
985 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
987 // The size of general purpose registers.
989 // The size of co-processor 1 registers.
991 // The size of co-processor 2 registers.
993 // The floating-point ABI.
995 // Processor-specific extension.
997 // Mask of ASEs used.
999 // Mask of general flags.
1004 // Mips_symbol class. Holds additional symbol information needed for Mips.
1007 class Mips_symbol : public Sized_symbol<size>
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)
1020 // Return whether this is a MIPS16 symbol.
1024 // (st_other & STO_MIPS16) == STO_MIPS16
1025 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1026 == elfcpp::STO_MIPS16 >> 2);
1029 // Return whether this is a microMIPS symbol.
1031 is_micromips() const
1033 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1034 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1035 == elfcpp::STO_MICROMIPS >> 2);
1038 // Return whether the symbol needs MIPS16 fn_stub.
1040 need_fn_stub() const
1041 { return this->need_fn_stub_; }
1043 // Set that the symbol needs MIPS16 fn_stub.
1046 { this->need_fn_stub_ = true; }
1048 // Return whether this symbol is referenced by branch relocations from
1049 // any non-PIC input file.
1051 has_nonpic_branches() const
1052 { return this->has_nonpic_branches_; }
1054 // Set that this symbol is referenced by branch relocations from
1055 // any non-PIC input file.
1057 set_has_nonpic_branches()
1058 { this->has_nonpic_branches_ = true; }
1060 // Return the offset of the la25 stub for this symbol from the start of the
1061 // la25 stub section.
1063 la25_stub_offset() const
1064 { return this->la25_stub_offset_; }
1066 // Set the offset of the la25 stub for this symbol from the start of the
1067 // la25 stub section.
1069 set_la25_stub_offset(unsigned int offset)
1070 { this->la25_stub_offset_ = offset; }
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.
1075 has_la25_stub() const
1076 { return this->la25_stub_offset_ != -1U; }
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).
1082 has_static_relocs() const
1083 { return this->has_static_relocs_; }
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
1089 set_has_static_relocs()
1090 { this->has_static_relocs_ = true; }
1092 // Return whether we must not create a lazy-binding stub for this symbol.
1094 no_lazy_stub() const
1095 { return this->no_lazy_stub_; }
1097 // Set that we must not create a lazy-binding stub for this symbol.
1100 { this->no_lazy_stub_ = true; }
1102 // Return the offset of the lazy-binding stub for this symbol from the start
1103 // of .MIPS.stubs section.
1105 lazy_stub_offset() const
1106 { return this->lazy_stub_offset_; }
1108 // Set the offset of the lazy-binding stub for this symbol from the start
1109 // of .MIPS.stubs section.
1111 set_lazy_stub_offset(unsigned int offset)
1112 { this->lazy_stub_offset_ = offset; }
1114 // Return whether there are any relocations for this symbol where
1115 // pointer equality matters.
1117 pointer_equality_needed() const
1118 { return this->pointer_equality_needed_; }
1120 // Set that there are relocations for this symbol where pointer equality
1123 set_pointer_equality_needed()
1124 { this->pointer_equality_needed_ = true; }
1126 // Return global GOT area where this symbol in located.
1128 global_got_area() const
1129 { return this->global_got_area_; }
1131 // Set global GOT area where this symbol in located.
1133 set_global_got_area(Global_got_area global_got_area)
1134 { this->global_got_area_ = global_got_area; }
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.
1141 global_gotoffset() const
1142 { return this->global_gotoffset_; }
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.
1148 set_global_gotoffset(unsigned int offset)
1150 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1151 this->global_gotoffset_ = offset;
1154 // Return whether all GOT relocations for this symbol are for calls.
1156 got_only_for_calls() const
1157 { return this->got_only_for_calls_; }
1159 // Set that there is a GOT relocation for this symbol that is not for call.
1161 set_got_not_only_for_calls()
1162 { this->got_only_for_calls_ = false; }
1164 // Return whether this is a PIC symbol.
1168 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1169 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1170 == (elfcpp::STO_MIPS_PIC >> 2));
1173 // Set the flag in st_other field that marks this symbol as PIC.
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));
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));
1189 // Set the flag in st_other field that marks this symbol as PLT.
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));
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));
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); }
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); }
1216 // Return whether the symbol has lazy-binding stub.
1218 has_lazy_stub() const
1219 { return this->has_lazy_stub_; }
1221 // Set whether the symbol has lazy-binding stub.
1223 set_has_lazy_stub(bool has_lazy_stub)
1224 { this->has_lazy_stub_ = has_lazy_stub; }
1226 // Return whether the symbol needs a standard PLT entry.
1228 needs_mips_plt() const
1229 { return this->needs_mips_plt_; }
1231 // Set whether the symbol needs a standard PLT entry.
1233 set_needs_mips_plt(bool needs_mips_plt)
1234 { this->needs_mips_plt_ = needs_mips_plt; }
1236 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1239 needs_comp_plt() const
1240 { return this->needs_comp_plt_; }
1242 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1244 set_needs_comp_plt(bool needs_comp_plt)
1245 { this->needs_comp_plt_ = needs_comp_plt; }
1247 // Return standard PLT entry offset, or -1 if none.
1249 mips_plt_offset() const
1250 { return this->mips_plt_offset_; }
1252 // Set standard PLT entry offset.
1254 set_mips_plt_offset(unsigned int mips_plt_offset)
1255 { this->mips_plt_offset_ = mips_plt_offset; }
1257 // Return whether the symbol has standard PLT entry.
1259 has_mips_plt_offset() const
1260 { return this->mips_plt_offset_ != -1U; }
1262 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1264 comp_plt_offset() const
1265 { return this->comp_plt_offset_; }
1267 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1269 set_comp_plt_offset(unsigned int comp_plt_offset)
1270 { this->comp_plt_offset_ = comp_plt_offset; }
1272 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1274 has_comp_plt_offset() const
1275 { return this->comp_plt_offset_ != -1U; }
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
1282 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1285 // Set MIPS16 fn stub for a symbol.
1287 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1288 { this->mips16_fn_stub_ = stub; }
1290 // Return whether symbol has MIPS16 fn stub.
1292 has_mips16_fn_stub() const
1293 { return this->mips16_fn_stub_ != NULL; }
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
1300 return static_cast<Mips16_stub_section<size, big_endian>*>(
1304 // Set MIPS16 call stub for a symbol.
1306 set_mips16_call_stub(Mips16_stub_section_base* stub)
1307 { this->mips16_call_stub_ = stub; }
1309 // Return whether symbol has MIPS16 call stub.
1311 has_mips16_call_stub() const
1312 { return this->mips16_call_stub_ != NULL; }
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
1319 return static_cast<Mips16_stub_section<size, big_endian>*>(
1320 mips16_call_fp_stub_);
1323 // Set MIPS16 call_fp stub for a symbol.
1325 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1326 { this->mips16_call_fp_stub_ = stub; }
1328 // Return whether symbol has MIPS16 call_fp stub.
1330 has_mips16_call_fp_stub() const
1331 { return this->mips16_call_fp_stub_ != NULL; }
1334 get_applied_secondary_got_fixup() const
1335 { return applied_secondary_got_fixup_; }
1338 set_applied_secondary_got_fixup()
1339 { this->applied_secondary_got_fixup_ = true; }
1341 // Return the hash of this symbol.
1345 return gold::string_hash<char>(this->name());
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.
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_;
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_;
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_;
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.
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_;
1376 // True if there are any relocations for this symbol where pointer equality
1378 bool pointer_equality_needed_;
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_;
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_;
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_;
1408 bool applied_secondary_got_fixup_;
1411 // Mips16_stub_section class.
1413 // The mips16 compiler uses a couple of special sections to handle
1414 // floating point arguments.
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.
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.
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.)
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.
1445 // We record any stubs that we find in the symbol table.
1447 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1449 class Mips16_stub_section_base { };
1451 template<int size, bool big_endian>
1452 class Mips16_stub_section : public Mips16_stub_section_base
1454 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
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)
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));
1466 // Return the object of this stub section.
1467 Mips_relobj<size, big_endian>*
1469 { return this->object_; }
1471 // Return the size of a section.
1473 section_size() const
1474 { return this->object_->section_size(this->shndx_); }
1476 // Return section index of this stub section.
1479 { return this->shndx_; }
1481 // Return symbol index, if stub is for a local function.
1484 { return this->r_sym_; }
1486 // Return symbol, if stub is for a global function.
1489 { return this->gsym_; }
1491 // Return whether stub is for a local function.
1493 is_for_local_function() const
1494 { return this->gsym_ == NULL; }
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.
1499 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1501 // To find target symbol for this stub, trust the first R_MIPS_NONE
1502 // relocation, if any. Otherwise trust the first relocation, whatever
1504 if (this->found_r_mips_none_)
1506 if (r_type == elfcpp::R_MIPS_NONE)
1508 this->r_sym_ = r_sym;
1510 this->found_r_mips_none_ = true;
1512 else if (!is_target_found())
1513 this->r_sym_ = r_sym;
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.
1519 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1521 // To find target symbol for this stub, trust the first R_MIPS_NONE
1522 // relocation, if any. Otherwise trust the first relocation, whatever
1524 if (this->found_r_mips_none_)
1526 if (r_type == elfcpp::R_MIPS_NONE)
1530 this->found_r_mips_none_ = true;
1532 else if (!is_target_found())
1536 // Return whether we found the stub target.
1538 is_target_found() const
1539 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1541 // Return whether this is a fn stub.
1544 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1546 // Return whether this is a call stub.
1548 is_call_stub() const
1549 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1551 // Return whether this is a call_fp stub.
1553 is_call_fp_stub() const
1554 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1556 // Return the output address.
1558 output_address() const
1560 return (this->object_->output_section(this->shndx_)->address()
1561 + this->object_->output_section_offset(this->shndx_));
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_;
1577 // Mips_relobj class.
1579 template<int size, bool big_endian>
1580 class Mips_relobj : public Sized_relobj_file<size, big_endian>
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;
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)
1601 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1602 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1606 { delete this->attributes_section_data_; }
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); }
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); }
1620 // Processor-specific flags in ELF file header. This is valid only after
1623 processor_specific_flags() const
1624 { return this->processor_specific_flags_; }
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.
1629 local_symbol_is_mips16(unsigned int r_sym) const
1631 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1632 return this->local_symbol_is_mips16_[r_sym];
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.
1638 local_symbol_is_micromips(unsigned int r_sym) const
1640 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1641 return this->local_symbol_is_micromips_[r_sym];
1644 // Get or create MIPS16 stub section.
1645 Mips16_stub_section<size, big_endian>*
1646 get_mips16_stub_section(unsigned int shndx)
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;
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;
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
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;
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.
1676 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
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>*>(
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
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;
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.
1700 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
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>*>(
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
1713 add_local_non_16bit_call(unsigned int symndx)
1714 { this->local_non_16bit_calls_.insert(symndx); }
1716 // Return true if there is any "non 16-bit" call relocation against local
1717 // symbol SYMNDX in this object.
1719 has_local_non_16bit_call_relocs(unsigned int symndx)
1721 return (this->local_non_16bit_calls_.find(symndx)
1722 != this->local_non_16bit_calls_.end());
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.
1729 add_local_16bit_call(unsigned int symndx)
1730 { this->local_16bit_calls_.insert(symndx); }
1732 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1733 // symbol SYMNDX in this object.
1735 has_local_16bit_call_relocs(unsigned int symndx)
1737 return (this->local_16bit_calls_.find(symndx)
1738 != this->local_16bit_calls_.end());
1741 // Get gp value that was used to create this object.
1744 { return this->gp_; }
1746 // Return whether the object is a PIC object.
1749 { return this->is_pic_; }
1751 // Return whether the object uses N32 ABI.
1754 { return this->is_n32_; }
1756 // Return whether the object uses N64 ABI.
1759 { return size == 64; }
1761 // Return whether the object uses NewABI conventions.
1764 { return this->is_n32() || this->is_n64(); }
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_; }
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()
1775 if (!this->got_info_)
1776 this->got_info_ = new Mips_got_info<size, big_endian>();
1777 return this->got_info_;
1780 // Set Mips_got_info for this object.
1782 set_got_info(Mips_got_info<size, big_endian>* got_info)
1783 { this->got_info_ = got_info; }
1785 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1786 // after do_read_symbols is called.
1788 is_mips16_stub_section(unsigned int shndx)
1790 return (is_mips16_fn_stub_section(shndx)
1791 || is_mips16_call_stub_section(shndx)
1792 || is_mips16_call_fp_stub_section(shndx));
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.
1799 section_allows_mips16_refs(unsigned int shndx)
1801 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1804 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1805 // after do_read_symbols is called.
1807 is_mips16_fn_stub_section(unsigned int shndx)
1809 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1810 return this->section_is_mips16_fn_stub_[shndx];
1813 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1814 // after do_read_symbols is called.
1816 is_mips16_call_stub_section(unsigned int shndx)
1818 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1819 return this->section_is_mips16_call_stub_[shndx];
1822 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1823 // valid after do_read_symbols is called.
1825 is_mips16_call_fp_stub_section(unsigned int shndx)
1827 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1828 return this->section_is_mips16_call_fp_stub_[shndx];
1831 // Discard MIPS16 stub secions that are not needed.
1833 discard_mips16_stub_sections(Symbol_table* symtab);
1835 // Return whether there is a .reginfo section.
1837 has_reginfo_section() const
1838 { return this->has_reginfo_section_; }
1840 // Return whether we want to merge processor-specific data.
1842 merge_processor_specific_data() const
1843 { return this->merge_processor_specific_data_; }
1845 // Return gprmask from the .reginfo section of this object.
1848 { return this->gprmask_; }
1850 // Return cprmask1 from the .reginfo section of this object.
1853 { return this->cprmask1_; }
1855 // Return cprmask2 from the .reginfo section of this object.
1858 { return this->cprmask2_; }
1860 // Return cprmask3 from the .reginfo section of this object.
1863 { return this->cprmask3_; }
1865 // Return cprmask4 from the .reginfo section of this object.
1868 { return this->cprmask4_; }
1870 // This is the contents of the .MIPS.abiflags section if there is one.
1871 Mips_abiflags<big_endian>*
1873 { return this->abiflags_; }
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_; }
1881 // Count the local symbols.
1883 do_count_local_symbols(Stringpool_template<char>*,
1884 Stringpool_template<char>*);
1886 // Read the symbol information.
1888 do_read_symbols(Read_symbols_data* sd);
1891 // The name of the options section.
1892 const char* mips_elf_options_section_name()
1893 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1895 // processor-specific flags in ELF file header.
1896 elfcpp::Elf_Word processor_specific_flags_;
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_;
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_;
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_;
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_;
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_;
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_;
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_;
1927 // gp value that was used to create this object.
1929 // Whether the object is a PIC object.
1931 // Whether the object uses N32 ABI.
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_;
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_;
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_;
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_;
1952 // .pdr section index.
1953 unsigned int pdr_shndx_;
1955 // Object attributes if there is a .gnu.attributes section or NULL.
1956 Attributes_section_data* attributes_section_data_;
1958 // Object abiflags if there is a .MIPS.abiflags section or NULL.
1959 Mips_abiflags<big_endian>* abiflags_;
1961 // gprmask from the .reginfo section of this object.
1963 // cprmask1 from the .reginfo section of this object.
1965 // cprmask2 from the .reginfo section of this object.
1967 // cprmask3 from the .reginfo section of this object.
1969 // cprmask4 from the .reginfo section of this object.
1973 // Mips_output_data_got class.
1975 template<int size, bool big_endian>
1976 class Mips_output_data_got : public Output_data_got<size, big_endian>
1978 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1979 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1981 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
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_()
1991 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1992 this->set_addralign(16);
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.
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)
2003 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
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
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)
2017 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2018 dyn_reloc, for_call);
2021 // Record that OBJECT has a page relocation against symbol SYMNDX and
2022 // that ADDEND is the addend for that relocation.
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); }
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.
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)); }
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.
2040 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2041 Sized_relobj_file<size, big_endian>* relobj,
2044 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2048 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2049 // secondary GOT at OFFSET.
2051 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2052 Mips_symbol<size>* gsym)
2054 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2058 // Update GOT entry at OFFSET with VALUE.
2060 update_got_entry(unsigned int offset, Mips_address value)
2062 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2065 // Return the number of entries in local part of the GOT. This includes
2066 // local entries, page entries and 2 reserved entries.
2068 get_local_gotno() const
2070 if (!this->multi_got())
2072 return (2 + this->master_got_info_->local_gotno()
2073 + this->master_got_info_->page_gotno());
2076 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2079 // Return dynamic symbol table index of the first symbol with global GOT
2082 first_global_got_dynsym_index() const
2083 { return this->first_global_got_dynsym_index_; }
2085 // Set dynamic symbol table index of the first symbol with global GOT entry.
2087 set_first_global_got_dynsym_index(unsigned int index)
2088 { this->first_global_got_dynsym_index_ = index; }
2090 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2091 // larger than 64K, create multi-GOT.
2093 lay_out_got(Layout* layout, Symbol_table* symtab,
2094 const Input_objects* input_objects);
2096 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2098 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2100 // Attempt to merge GOTs of different input objects.
2102 merge_gots(const Input_objects* input_objects);
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.
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);
2111 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2112 // use OBJECT's GOT.
2114 get_got_page_offset(Mips_address value,
2115 const Mips_relobj<size, big_endian>* object)
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);
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
2129 if (!this->multi_got())
2130 return gsym->got_offset(got_type);
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));
2139 // Return the GOT offset of type GOT_TYPE of the local symbol
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); }
2147 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2149 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
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();
2158 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2160 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2161 Mips_relobj<size, big_endian>* object)
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);
2170 // Return true for multi-GOT links.
2173 { return this->primary_got_ != NULL; }
2175 // Return the offset of OBJECT's GOT from the start of .got section.
2177 get_got_offset(const Mips_relobj<size, big_endian>* object)
2179 if (!this->multi_got())
2183 Mips_got_info<size, big_endian>* g = object->get_got_info();
2184 return g != NULL ? g->offset() : 0;
2188 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2190 add_reloc_only_entries()
2191 { this->master_got_info_->add_reloc_only_entries(this); }
2193 // Return offset of the primary GOT's entry for global symbol.
2195 get_primary_got_offset(const Mips_symbol<size>* sym) const
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;
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.
2209 gp_offset(unsigned int got_offset,
2210 const Mips_relobj<size, big_endian>* object) const
2212 return (this->address() + got_offset
2213 - this->target_->adjusted_gp_value(object));
2217 // Write out the GOT table.
2219 do_write(Output_file*);
2223 // This class represent dynamic relocations that need to be applied by
2224 // gold because we are using TLS relocations in a static link.
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; }
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)
2237 this->u_.local.relobj = relobj;
2238 this->u_.local.index = index;
2241 // Return the GOT offset.
2244 { return this->got_offset_; }
2249 { return this->r_type_; }
2251 // Whether the symbol is global or not.
2253 symbol_is_global() const
2254 { return this->symbol_is_global_; }
2256 // For a relocation against a global symbol, the global symbol.
2260 gold_assert(this->symbol_is_global_);
2261 return this->u_.global.symbol;
2264 // For a relocation against a local symbol, the defining object.
2265 Sized_relobj_file<size, big_endian>*
2268 gold_assert(!this->symbol_is_global_);
2269 return this->u_.local.relobj;
2272 // For a relocation against a local symbol, the local symbol index.
2276 gold_assert(!this->symbol_is_global_);
2277 return this->u_.local.index;
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.
2292 // For a global symbol, the symbol itself.
2293 Mips_symbol<size>* symbol;
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.
2306 Target_mips<size, big_endian>* target_;
2307 // The symbol table.
2308 Symbol_table* symbol_table_;
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_;
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:
2328 // lui $25,%hi(func)
2330 // addiu $25,$25,%lo(func)
2332 // to a separate trampoline section. The second is to add:
2334 // lui $25,%hi(func)
2335 // addiu $25,$25,%lo(func)
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.)
2341 // We only create stubs of the first type.
2343 template<int size, bool big_endian>
2344 class Mips_output_data_la25_stub : public Output_section_data
2346 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2349 Mips_output_data_la25_stub()
2350 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2353 // Create LA25 stub for a symbol.
2355 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2356 Mips_symbol<size>* gsym);
2358 // Return output address of a stub.
2360 stub_address(const Mips_symbol<size>* sym) const
2362 gold_assert(sym->has_la25_stub());
2363 return this->address() + sym->la25_stub_offset();
2368 do_adjust_output_section(Output_section* os)
2369 { os->set_entsize(0); }
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[];
2377 // Set the final size.
2379 set_final_data_size()
2380 { this->set_data_size(this->symbols_.size() * 16); }
2382 // Create a symbol for SYM stub's value and size, to help make the
2383 // disassembly easier to read.
2385 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2386 Target_mips<size, big_endian>* target, uint64_t symsize);
2388 // Write to a map file.
2390 do_print_to_mapfile(Mapfile* mapfile) const
2391 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2393 // Write out the LA25 stub section.
2395 do_write(Output_file*);
2397 // Symbols that have LA25 stubs.
2398 std::vector<Mips_symbol<size>*> symbols_;
2401 // MIPS-specific relocation writer.
2403 template<int sh_type, bool dynamic, int size, bool big_endian>
2404 struct Mips_output_reloc_writer;
2406 template<int sh_type, bool dynamic, bool big_endian>
2407 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2409 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2410 typedef std::vector<Output_reloc_type> Relocs;
2413 write(typename Relocs::const_iterator p, unsigned char* pov)
2417 template<int sh_type, bool dynamic, bool big_endian>
2418 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2420 typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2421 typedef std::vector<Output_reloc_type> Relocs;
2424 write(typename Relocs::const_iterator p, unsigned char* pov)
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);
2434 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2435 orel.put_r_type3(elfcpp::R_MIPS_NONE);
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,
2444 Mips_output_data_reloc(bool sort_relocs)
2445 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2449 // Write out the data.
2451 do_write(Output_file* of)
2453 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2455 this->template do_write_generic<Writer>(of);
2460 // A class to handle the PLT data.
2462 template<int size, bool big_endian>
2463 class Mips_output_data_plt : public Output_section_data
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;
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),
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);
2484 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2486 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2488 // Return the .rel.plt section data.
2491 { return this->rel_; }
2493 // Return the number of PLT entries.
2496 { return this->symbols_.size(); }
2498 // Return the offset of the first non-reserved PLT entry.
2500 first_plt_entry_offset() const
2501 { return sizeof(plt0_entry_o32); }
2503 // Return the size of a PLT entry.
2505 plt_entry_size() const
2506 { return sizeof(plt_entry); }
2508 // Set final PLT offsets. For each symbol, determine whether standard or
2509 // compressed (MIPS16 or microMIPS) PLT entry is used.
2513 // Return the offset of the first standard PLT entry.
2515 first_mips_plt_offset() const
2516 { return this->plt_header_size_; }
2518 // Return the offset of the first compressed PLT entry.
2520 first_comp_plt_offset() const
2521 { return this->plt_header_size_ + this->plt_mips_offset_; }
2523 // Return whether there are any standard PLT entries.
2525 has_standard_entries() const
2526 { return this->plt_mips_offset_ > 0; }
2528 // Return the output address of standard PLT entry.
2530 mips_entry_address(const Mips_symbol<size>* sym) const
2532 gold_assert (sym->has_mips_plt_offset());
2533 return (this->address() + this->first_mips_plt_offset()
2534 + sym->mips_plt_offset());
2537 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2539 comp_entry_address(const Mips_symbol<size>* sym) const
2541 gold_assert (sym->has_comp_plt_offset());
2542 return (this->address() + this->first_comp_plt_offset()
2543 + sym->comp_plt_offset());
2548 do_adjust_output_section(Output_section* os)
2549 { os->set_entsize(0); }
2551 // Write to a map file.
2553 do_print_to_mapfile(Mapfile* mapfile) const
2554 { mapfile->print_output_data(this, _(".plt")); }
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[];
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[];
2571 // Set the final size.
2573 set_final_data_size()
2575 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2576 + this->plt_comp_offset_);
2579 // Write out the PLT data.
2581 do_write(Output_file*);
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.
2589 is_plt_header_compressed() const
2591 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2592 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2595 // Return the size of the PLT header.
2597 get_plt_header_size() const
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]));
2609 return (2 * sizeof(plt0_entry_micromips_o32)
2610 / sizeof(plt0_entry_micromips_o32[0]));
2613 // Return the PLT header entry.
2615 get_plt_header_entry() const
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;
2626 return plt0_entry_micromips_o32;
2629 // Return the size of the standard PLT entry.
2631 standard_plt_entry_size() const
2632 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2634 // Return the size of the compressed PLT entry.
2636 compressed_plt_entry_size() const
2638 gold_assert(!this->target_->is_output_newabi());
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]));
2647 return (2 * sizeof(plt_entry_micromips_o32)
2648 / sizeof(plt_entry_micromips_o32[0]));
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_;
2664 Target_mips<size, big_endian>* target_;
2667 // A class to handle the .MIPS.stubs data.
2669 template<int size, bool big_endian>
2670 class Mips_output_data_mips_stubs : public Output_section_data
2672 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2674 // Unordered set of .MIPS.stubs entries.
2675 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2676 Mips_stubs_entry_set;
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)
2684 // Create entry for a symbol.
2686 make_entry(Mips_symbol<size>*);
2688 // Remove entry for a symbol.
2690 remove_entry(Mips_symbol<size>* gsym);
2692 // Set stub offsets for symbols. This method expects that the number of
2693 // entries in dynamic symbol table is set.
2695 set_lazy_stub_offsets();
2698 set_needs_dynsym_value();
2700 // Set the number of entries in dynamic symbol table.
2702 set_dynsym_count(unsigned int dynsym_count)
2703 { this->dynsym_count_ = dynsym_count; }
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.
2713 stub_max_size() const
2715 if (!this->target_->is_output_micromips()
2716 || this->target_->use_32bit_micromips_instructions())
2722 // Return the size of the stub. This method expects that the final dynsym
2727 gold_assert(this->dynsym_count_ != -1U);
2728 if (this->dynsym_count_ > 0x10000)
2729 return this->stub_max_size();
2731 return this->stub_max_size() - 4;
2734 // Return output address of a stub.
2736 stub_address(const Mips_symbol<size>* sym) const
2738 gold_assert(sym->has_lazy_stub());
2739 return this->address() + sym->lazy_stub_offset();
2744 do_adjust_output_section(Output_section* os)
2745 { os->set_entsize(0); }
2747 // Write to a map file.
2749 do_print_to_mapfile(Mapfile* mapfile) const
2750 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
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[];
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[];
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[];
2774 // Set the final size.
2776 set_final_data_size()
2777 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2779 // Write out the .MIPS.stubs data.
2781 do_write(Output_file*);
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_;
2790 Target_mips<size, big_endian>* target_;
2793 // This class handles Mips .reginfo output section.
2795 template<int size, bool big_endian>
2796 class Mips_output_section_reginfo : public Output_section_data
2798 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2801 Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2802 Valtype gprmask, Valtype cprmask1,
2803 Valtype cprmask2, Valtype cprmask3,
2805 : Output_section_data(24, 4, true), target_(target),
2806 gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2807 cprmask3_(cprmask3), cprmask4_(cprmask4)
2811 // Write to a map file.
2813 do_print_to_mapfile(Mapfile* mapfile) const
2814 { mapfile->print_output_data(this, _(".reginfo")); }
2816 // Write out reginfo section.
2818 do_write(Output_file* of);
2821 Target_mips<size, big_endian>* target_;
2823 // gprmask of the output .reginfo section.
2825 // cprmask1 of the output .reginfo section.
2827 // cprmask2 of the output .reginfo section.
2829 // cprmask3 of the output .reginfo section.
2831 // cprmask4 of the output .reginfo section.
2835 // This class handles .MIPS.options output section.
2837 template<int size, bool big_endian>
2838 class Mips_output_section_options : public Output_section
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)
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();
2852 // Write out option section.
2854 do_write(Output_file* of);
2857 Target_mips<size, big_endian>* target_;
2860 // This class handles .MIPS.abiflags output section.
2862 template<int size, bool big_endian>
2863 class Mips_output_section_abiflags : public Output_section_data
2866 Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2867 : Output_section_data(24, 8, true), abiflags_(abiflags)
2871 // Write to a map file.
2873 do_print_to_mapfile(Mapfile* mapfile) const
2874 { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2877 do_write(Output_file* of);
2880 const Mips_abiflags<big_endian>& abiflags_;
2883 // The MIPS target has relocation types which default handling of relocatable
2884 // relocation cannot process. So we have to extend the default code.
2886 template<bool big_endian, typename Classify_reloc>
2887 class Mips_scan_relocatable_relocs :
2888 public Default_scan_relocatable_relocs<Classify_reloc>
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)
2896 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2897 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2902 case elfcpp::R_MIPS_26:
2903 return Relocatable_relocs::RELOC_SPECIAL;
2906 return Default_scan_relocatable_relocs<Classify_reloc>::
2907 local_section_strategy(r_type, object);
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.
2918 template<int sh_type, int size, bool big_endian>
2919 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2923 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2926 // Emit any saved relocations which turn out to be needed. This is
2927 // called after all the relocs have been scanned.
2929 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2930 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2933 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
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_.
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);
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.
2951 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2952 bool local_protected)
2954 // If it's a local sym, of course we resolve locally.
2958 // STV_HIDDEN or STV_INTERNAL ones must be local.
2959 if (sym->visibility() == elfcpp::STV_HIDDEN
2960 || sym->visibility() == elfcpp::STV_INTERNAL)
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())
2968 // Forced local symbols resolve locally.
2969 if (sym->is_forced_local())
2972 // As do non-dynamic symbols.
2973 if (!has_dynsym_entry)
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())
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)
2988 // STV_PROTECTED non-function symbols are local.
2989 if (sym->type() != elfcpp::STT_FUNC)
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;
3000 // Return TRUE if references to this symbol always reference the symbol in this
3003 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3005 return symbol_refs_local(sym, has_dynsym_entry, false);
3008 // Return TRUE if calls to this symbol always call the version in this object.
3010 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3012 return symbol_refs_local(sym, has_dynsym_entry, true);
3015 // Compare GOT offsets of two symbols.
3017 template<int size, bool big_endian>
3019 got_offset_compare(Symbol* sym1, Symbol* sym2)
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);
3027 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3029 return area1 < area2;
3031 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
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
3040 template<int size, bool big_endian>
3042 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3043 std::vector<Symbol*>* non_got_symbols,
3044 std::vector<Symbol*>* got_symbols)
3046 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3047 p != dyn_symbols->end();
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);
3055 non_got_symbols->push_back(mips_sym);
3058 std::sort(got_symbols->begin(), got_symbols->end(),
3059 got_offset_compare<size, big_endian>);
3062 // Functor class for processing the global symbol table.
3064 template<int size, bool big_endian>
3065 class Symbol_visitor_check_symbols
3068 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3069 Layout* layout, Symbol_table* symtab)
3070 : target_(target), layout_(layout), symtab_(symtab)
3074 operator()(Sized_symbol<size>* sym)
3076 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3077 if (local_pic_function<size, big_endian>(mips_sym))
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
3084 if (parameters->options().relocatable())
3086 if (!parameters->options().output_is_position_independent())
3087 mips_sym->set_pic();
3089 else if (mips_sym->has_nonpic_branches())
3091 this->target_->la25_stub_section(layout_)
3092 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3098 Target_mips<size, big_endian>* target_;
3100 Symbol_table* symtab_;
3103 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3104 // and endianness. The relocation format for MIPS-64 is non-standard.
3106 template<int sh_type, int size, bool big_endian>
3107 struct Mips_reloc_types;
3109 template<bool big_endian>
3110 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3112 typedef typename elfcpp::Rel<32, big_endian> Reloc;
3113 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3115 static typename elfcpp::Elf_types<32>::Elf_Swxword
3116 get_r_addend(const Reloc*)
3120 set_reloc_addend(Reloc_write*,
3121 typename elfcpp::Elf_types<32>::Elf_Swxword)
3122 { gold_unreachable(); }
3125 template<bool big_endian>
3126 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3128 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3129 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3131 static typename elfcpp::Elf_types<32>::Elf_Swxword
3132 get_r_addend(const Reloc* reloc)
3133 { return reloc->get_r_addend(); }
3136 set_reloc_addend(Reloc_write* p,
3137 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3138 { p->put_r_addend(val); }
3141 template<bool big_endian>
3142 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3144 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3145 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3147 static typename elfcpp::Elf_types<64>::Elf_Swxword
3148 get_r_addend(const Reloc*)
3152 set_reloc_addend(Reloc_write*,
3153 typename elfcpp::Elf_types<64>::Elf_Swxword)
3154 { gold_unreachable(); }
3157 template<bool big_endian>
3158 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3160 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3161 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3163 static typename elfcpp::Elf_types<64>::Elf_Swxword
3164 get_r_addend(const Reloc* reloc)
3165 { return reloc->get_r_addend(); }
3168 set_reloc_addend(Reloc_write* p,
3169 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3170 { p->put_r_addend(val); }
3173 // Forward declaration.
3175 mips_get_size_for_reloc(unsigned int, Relobj*);
3177 // A class for inquiring about properties of a relocation,
3178 // used while scanning relocs during a relocatable link and
3179 // garbage collection.
3181 template<int sh_type_, int size, bool big_endian>
3182 class Mips_classify_reloc;
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>
3189 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3191 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
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()); }
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()); }
3204 static inline unsigned int
3205 get_r_type2(const Reltype*)
3208 static inline unsigned int
3209 get_r_type3(const Reltype*)
3212 static inline unsigned int
3213 get_r_ssym(const Reltype*)
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)
3220 if (sh_type_ == elfcpp::SHT_REL)
3222 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
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.
3228 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
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));
3234 // Write the r_addend field to a new reloc.
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); }
3240 // Return the size of the addend of the relocation (only used for SHT_REL).
3242 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3243 { return mips_get_size_for_reloc(r_type, obj); }
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>
3251 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3253 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
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(); }
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(); }
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(); }
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(); }
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(); }
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)
3285 if (sh_type_ == elfcpp::SHT_REL)
3287 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
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.
3293 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
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());
3302 // Write the r_addend field to a new reloc.
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); }
3308 // Return the size of the addend of the relocation (only used for SHT_REL).
3310 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3311 { return mips_get_size_for_reloc(r_type, obj); }
3314 template<int size, bool big_endian>
3315 class Target_mips : public Sized_target<size, big_endian>
3317 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3318 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
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
3324 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
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)
3336 this->add_machine_extensions();
3339 // The offset of $gp from the beginning of the .got section.
3340 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
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;
3346 // Make a new symbol table entry for the Mips target.
3348 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3349 { return new Mips_symbol<size>(); }
3351 // Process the relocations to determine unreferenced sections for
3352 // garbage collection.
3354 gc_process_relocs(Symbol_table* symtab,
3356 Sized_relobj_file<size, big_endian>* object,
3357 unsigned int data_shndx,
3358 unsigned int sh_type,
3359 const unsigned char* prelocs,
3361 Output_section* output_section,
3362 bool needs_special_offset_handling,
3363 size_t local_symbol_count,
3364 const unsigned char* plocal_symbols);
3366 // Scan the relocations to look for symbol adjustments.
3368 scan_relocs(Symbol_table* symtab,
3370 Sized_relobj_file<size, big_endian>* object,
3371 unsigned int data_shndx,
3372 unsigned int sh_type,
3373 const unsigned char* prelocs,
3375 Output_section* output_section,
3376 bool needs_special_offset_handling,
3377 size_t local_symbol_count,
3378 const unsigned char* plocal_symbols);
3380 // Finalize the sections.
3382 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3384 // Relocate a section.
3386 relocate_section(const Relocate_info<size, big_endian>*,
3387 unsigned int sh_type,
3388 const unsigned char* prelocs,
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*);
3397 // Scan the relocs during a relocatable link.
3399 scan_relocatable_relocs(Symbol_table* symtab,
3401 Sized_relobj_file<size, big_endian>* object,
3402 unsigned int data_shndx,
3403 unsigned int sh_type,
3404 const unsigned char* prelocs,
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*);
3412 // Scan the relocs for --emit-relocs.
3414 emit_relocs_scan(Symbol_table* symtab,
3416 Sized_relobj_file<size, big_endian>* object,
3417 unsigned int data_shndx,
3418 unsigned int sh_type,
3419 const unsigned char* prelocs,
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);
3427 // Emit relocations for a section.
3429 relocate_relocs(const Relocate_info<size, big_endian>*,
3430 unsigned int sh_type,
3431 const unsigned char* prelocs,
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);
3442 // Perform target-specific processing in a relocatable link. This is
3443 // only used if we use the relocation strategy RELOC_SPECIAL.
3445 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3446 unsigned int sh_type,
3447 const unsigned char* preloc_in,
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);
3457 // Return whether SYM is defined by the ABI.
3459 do_is_defined_by_abi(const Symbol* sym) const
3461 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3462 || (strcmp(sym->name(), "_gp_disp") == 0)
3463 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3466 // Return the number of entries in the GOT.
3468 got_entry_count() const
3470 if (!this->has_got_section())
3472 return this->got_size() / (size/8);
3475 // Return the number of entries in the PLT.
3477 plt_entry_count() const
3479 if (this->plt_ == NULL)
3481 return this->plt_->entry_count();
3484 // Return the offset of the first non-reserved PLT entry.
3486 first_plt_entry_offset() const
3487 { return this->plt_->first_plt_entry_offset(); }
3489 // Return the size of each PLT entry.
3491 plt_entry_size() const
3492 { return this->plt_->plt_entry_size(); }
3494 // Get the GOT section, creating it if necessary.
3495 Mips_output_data_got<size, big_endian>*
3496 got_section(Symbol_table*, Layout*);
3498 // Get the GOT section.
3499 Mips_output_data_got<size, big_endian>*
3502 gold_assert(this->got_ != NULL);
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);
3510 // Get the .MIPS.stubs section.
3511 Mips_output_data_mips_stubs<size, big_endian>*
3512 mips_stubs_section() const
3514 gold_assert(this->mips_stubs_ != NULL);
3515 return this->mips_stubs_;
3518 // Get the LA25 stub section, creating it if necessary.
3519 Mips_output_data_la25_stub<size, big_endian>*
3520 la25_stub_section(Layout*);
3522 // Get the LA25 stub section.
3523 Mips_output_data_la25_stub<size, big_endian>*
3526 gold_assert(this->la25_stub_ != NULL);
3527 return this->la25_stub_;
3530 // Get gp value. It has the value of .got + 0x7FF0.
3534 if (this->gp_ != NULL)
3535 return this->gp_->value();
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.
3542 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3544 if (this->gp_ == NULL)
3547 bool multi_got = false;
3548 if (this->has_got_section())
3549 multi_got = this->got_section()->multi_got();
3551 return this->gp_->value();
3553 return this->gp_->value() + this->got_section()->get_got_offset(object);
3556 // Get the dynamic reloc section, creating it if necessary.
3558 rel_dyn_section(Layout*);
3561 do_has_custom_set_dynsym_indexes() const
3564 // Don't emit input .reginfo/.MIPS.abiflags sections to
3565 // output .reginfo/.MIPS.abiflags.
3567 do_should_include_section(elfcpp::Elf_Word sh_type) const
3569 return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3570 && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
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.
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;
3582 // Remove .MIPS.stubs entry for a symbol.
3584 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3586 if (this->mips_stubs_ != NULL)
3587 this->mips_stubs_->remove_entry(sym);
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
3594 mips_elf_gnu_got1_mask()
3596 if (this->is_output_n64())
3597 return (uint64_t)1 << 63;
3602 // Whether the output has microMIPS code. This is valid only after
3603 // merge_obj_e_flags() is called.
3605 is_output_micromips() const
3607 gold_assert(this->are_processor_specific_flags_set());
3608 return elfcpp::is_micromips(this->processor_specific_flags());
3611 // Whether the output uses N32 ABI. This is valid only after
3612 // merge_obj_e_flags() is called.
3614 is_output_n32() const
3616 gold_assert(this->are_processor_specific_flags_set());
3617 return elfcpp::abi_n32(this->processor_specific_flags());
3620 // Whether the output uses R6 ISA. This is valid only after
3621 // merge_obj_e_flags() is called.
3623 is_output_r6() const
3625 gold_assert(this->are_processor_specific_flags_set());
3626 return elfcpp::r6_isa(this->processor_specific_flags());
3629 // Whether the output uses N64 ABI.
3631 is_output_n64() const
3632 { return size == 64; }
3634 // Whether the output uses NEWABI. This is valid only after
3635 // merge_obj_e_flags() is called.
3637 is_output_newabi() const
3638 { return this->is_output_n32() || this->is_output_n64(); }
3640 // Whether we can only use 32-bit microMIPS instructions.
3642 use_32bit_micromips_instructions() const
3643 { return this->insn32_; }
3645 // Return the r_sym field from a relocation.
3647 get_r_sym(const unsigned char* preloc) const
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>::
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.
3662 do_dynsym_value(const Symbol* gsym) const;
3664 // Make an ELF object.
3666 do_make_elf_object(const std::string&, Input_file*, off_t,
3667 const elfcpp::Ehdr<size, big_endian>& ehdr);
3670 do_make_elf_object(const std::string&, Input_file*, off_t,
3671 const elfcpp::Ehdr<size, !big_endian>&)
3672 { gold_unreachable(); }
3674 // Make an output section.
3676 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3677 elfcpp::Elf_Xword flags)
3679 if (type == elfcpp::SHT_MIPS_OPTIONS)
3680 return new Mips_output_section_options<size, big_endian>(name, type,
3683 return new Output_section(name, type, flags);
3686 // Adjust ELF file header.
3688 do_adjust_elf_header(unsigned char* view, int len);
3690 // Get the custom dynamic tag value.
3692 do_dynamic_tag_custom_value(elfcpp::DT) const;
3694 // Adjust the value written to the dynamic symbol table.
3696 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
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);
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)
3707 if (!mips_sym->has_mips16_fn_stub())
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
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
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());
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)));
3732 else if ((mips_sym->is_micromips()
3733 // Stubs are always microMIPS if there is any microMIPS code in
3735 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
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)));
3745 // The class which scans relocations.
3753 get_reference_flags(unsigned int r_type);
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,
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,
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,
3780 unsigned int rel_type,
3781 unsigned int r_type,
3782 const elfcpp::Sym<size, big_endian>& lsym,
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,
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,
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,
3808 unsigned int rel_type,
3809 unsigned int r_type,
3813 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3815 Sized_relobj_file<size, big_endian>*,
3820 const elfcpp::Sym<size, big_endian>&)
3824 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3826 Sized_relobj_file<size, big_endian>*,
3830 unsigned int, Symbol*)
3834 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3836 Sized_relobj_file<size, big_endian>*,
3841 const elfcpp::Sym<size, big_endian>&)
3845 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3847 Sized_relobj_file<size, big_endian>*,
3851 unsigned int, Symbol*)
3855 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3856 unsigned int r_type);
3859 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3860 unsigned int r_type, Symbol*);
3863 // The class which implements relocation.
3868 : calculated_value_(0), calculate_only_(false)
3874 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3876 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3877 unsigned int r_type,
3878 Output_section* output_section,
3879 Target_mips* target);
3881 // Do a relocation. Return false if the caller should not issue
3882 // any warnings about this relocation.
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);
3890 // Result of the relocation.
3891 Valtype calculated_value_;
3892 // Whether we have to calculate relocation instead of applying it.
3893 bool calculate_only_;
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
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),
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.
3915 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3916 Symbol_table* symtab)
3918 if (!this->sym_->has_static_relocs())
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_);
3928 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3929 this->output_section_, this->relobj_,
3930 this->shndx_, this->r_offset_);
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_;
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);
3948 // Return whether there is a GOT section.
3950 has_got_section() const
3951 { return this->got_ != NULL; }
3953 // Check whether the given ELF header flags describe a 32-bit binary.
3955 mips_32bit_flags(elfcpp::Elf_Word);
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,
3983 mach_mips_loongson_2e = 3001,
3984 mach_mips_loongson_2f = 3002,
3985 mach_mips_gs464 = 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
4005 // Return the MACH for a MIPS e_flags value.
4007 elf_mips_mach(elfcpp::Elf_Word);
4009 // Return the MACH for each .MIPS.abiflags ISA Extension.
4011 mips_isa_ext_mach(unsigned int);
4013 // Return the .MIPS.abiflags value representing each ISA Extension.
4015 mips_isa_ext(unsigned int);
4017 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4019 update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4020 Mips_abiflags<big_endian>*);
4022 // Infer the content of the ABI flags based on the elf header.
4024 infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4026 // Create abiflags from elf header or from .MIPS.abiflags section.
4028 create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4030 // Return the meaning of fp_abi, or "unknown" if not known.
4036 select_fp_abi(const std::string&, int, int);
4038 // Merge attributes from input object.
4040 merge_obj_attributes(const std::string&, const Attributes_section_data*);
4042 // Merge abiflags from input object.
4044 merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4046 // Check whether machine EXTENSION is an extension of machine BASE.
4048 mips_mach_extends(unsigned int, unsigned int);
4050 // Merge file header flags from input object.
4052 merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4054 // Encode ISA level and revision as a single value.
4056 level_rev(unsigned char isa_level, unsigned char isa_rev) const
4057 { return (isa_level << 3) | isa_rev; }
4059 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
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
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
4078 // Return the size of the GOT section.
4082 gold_assert(this->got_ != NULL);
4083 return this->got_->data_size();
4086 // Create a PLT entry for a global symbol referenced by r_type relocation.
4088 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4089 unsigned int r_type);
4091 // Get the PLT section.
4092 Mips_output_data_plt<size, big_endian>*
4095 gold_assert(this->plt_ != NULL);
4099 // Get the GOT PLT section.
4100 const Mips_output_data_plt<size, big_endian>*
4101 got_plt_section() const
4103 gold_assert(this->got_plt_ != NULL);
4104 return this->got_plt_;
4107 // Copy a relocation against a global symbol.
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)
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));
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)
4127 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4128 output_section, r_offset));
4131 // Calculate value of _gp symbol.
4133 set_gp(Layout*, Symbol_table*);
4136 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4138 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
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.
4144 add_machine_extensions()
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_gs464, mach_mipsisa64r2);
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);
4158 // MIPS V extensions.
4159 this->add_extension(mach_mipsisa64, mach_mips5);
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);
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);
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);
4181 // VR4100 extensions.
4182 this->add_extension(mach_mips4120, mach_mips4100);
4183 this->add_extension(mach_mips4111, mach_mips4100);
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);
4197 // MIPS32 extensions.
4198 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4200 // MIPS II extensions.
4201 this->add_extension(mach_mips4000, mach_mips6000);
4202 this->add_extension(mach_mipsisa32, mach_mips6000);
4204 // MIPS I extensions.
4205 this->add_extension(mach_mips6000, mach_mips3000);
4206 this->add_extension(mach_mips3900, mach_mips3000);
4209 // Add value to MIPS extenstions.
4211 add_extension(unsigned int base, unsigned int extension)
4213 std::pair<unsigned int, unsigned int> ext(base, extension);
4214 this->mips_mach_extensions_.push_back(ext);
4217 // Return the number of entries in the .dynsym section.
4218 unsigned int get_dt_mips_symtabno() const
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.
4225 // Information about this specific target which we pass to the
4226 // general Target structure.
4227 static const Target::Target_info mips_info;
4229 Mips_output_data_got<size, big_endian>* got_;
4230 // gp symbol. It has the value of .got + 0x7FF0.
4231 Sized_symbol<size>* gp_;
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_;
4243 // A list of dyn relocs to be saved.
4244 std::vector<Dyn_reloc> dyn_relocs_;
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_;
4251 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
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_;
4261 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4263 // Whether there is an input .MIPS.abiflags section.
4264 bool has_abiflags_section_;
4266 // Whether the entry symbol is mips16 or micromips.
4267 bool entry_symbol_is_compressed_;
4269 // Whether we can use only 32-bit microMIPS instructions.
4270 // TODO(sasa): This should be a linker option.
4274 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4275 // It records high part of the relocation pair.
4277 template<int size, bool big_endian>
4280 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
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)
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;
4297 bool extract_addend;
4298 Mips_address address;
4302 template<int size, bool big_endian>
4303 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
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;
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.
4321 typedef Relocate_functions<size, big_endian> Base;
4322 typedef Mips_relocate_functions<size, big_endian> This;
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;
4328 template<int valsize>
4329 static inline typename This::Status
4330 check_overflow(Valtype value)
4333 return (Bits<valsize>::has_overflow32(value)
4334 ? This::STATUS_OVERFLOW
4335 : This::STATUS_OKAY);
4337 return (Bits<valsize>::has_overflow(value)
4338 ? This::STATUS_OVERFLOW
4339 : This::STATUS_OKAY);
4343 should_shuffle_micromips_reloc(unsigned int r_type)
4345 return (micromips_reloc(r_type)
4346 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4347 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4351 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4352 // Most mips16 instructions are 16 bits, but these instructions
4355 // The format of these instructions is:
4357 // +--------------+--------------------------------+
4358 // | JALX | X| Imm 20:16 | Imm 25:21 |
4359 // +--------------+--------------------------------+
4360 // | Immediate 15:0 |
4361 // +-----------------------------------------------+
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.
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
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.
4385 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4389 // +--------+----------------------+
4393 // +--------+----------------------+
4396 // +----------+------+-------------+
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)).
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)
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:
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
4421 // A typical instruction will have a format like this:
4423 // +--------------+--------------------------------+
4424 // | EXTEND | Imm 10:5 | Imm 15:11 |
4425 // +--------------+--------------------------------+
4426 // | Major | rx | ry | Imm 4:0 |
4427 // +--------------+--------------------------------+
4429 // EXTEND is the five bit value 11110. Major is the instruction
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.
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.
4441 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4444 if (!mips16_reloc(r_type)
4445 && !should_shuffle_micromips_reloc(r_type))
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);
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));
4460 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4461 | ((first & 0x1f) << 21) | second);
4463 elfcpp::Swap<32, big_endian>::writeval(view, val);
4467 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4469 if (!mips16_reloc(r_type)
4470 && !should_shuffle_micromips_reloc(r_type))
4473 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4474 Valtype16 first, second;
4476 if (micromips_reloc(r_type)
4477 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4479 second = val & 0xffff;
4482 else if (r_type != elfcpp::R_MIPS16_26)
4484 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4485 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4489 second = val & 0xffff;
4490 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4491 | ((val >> 21) & 0x1f);
4494 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4495 elfcpp::Swap<16, big_endian>::writeval(view, first);
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)
4504 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4505 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4507 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4510 Valtype x = psymval->value(object, addend);
4511 val = Bits<16>::bit_select32(val, x, 0xffffU);
4515 *calculated_value = x;
4516 return This::STATUS_OKAY;
4519 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4521 return check_overflow<16>(x);
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)
4530 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4531 Valtype addend = (extract_addend
4532 ? elfcpp::Swap<32, big_endian>::readval(wv)
4534 Valtype x = psymval->value(object, addend);
4537 *calculated_value = x;
4539 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4541 return This::STATUS_OKAY;
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)
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);
4556 // Try converting J(AL)R to B(AL), if the target is in range.
4557 if (r_type == elfcpp::R_MIPS_JALR
4559 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4560 || (jr_to_b && val == 0x03200008))) // jr t9
4562 int offset = psymval->value(object, addend) - (address + 4);
4563 if (!Bits<18>::has_overflow32(offset))
4565 if (val == 0x03200008) // jr t9
4566 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4568 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4573 *calculated_value = val;
4575 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4577 return This::STATUS_OKAY;
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)
4587 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4588 Valtype addend = (extract_addend
4589 ? elfcpp::Swap<32, big_endian>::readval(wv)
4591 Valtype x = psymval->value(object, addend) - address;
4594 *calculated_value = x;
4596 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4598 return This::STATUS_OKAY;
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)
4609 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4610 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4615 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4616 addend = (val & 0x03ffffff) << 1;
4618 addend = (val & 0x03ffffff) << 2;
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))
4628 gold_warning(_("JALX to a non-word-aligned address"));
4629 return This::STATUS_BAD_RELOC;
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;
4638 x = addend | ((address + 4) & (0xfc000000 << shift));
4642 x = Bits<27>::sign_extend32(addend);
4644 x = Bits<28>::sign_extend32(addend);
4646 x = psymval->value(object, x) >> shift;
4648 if (!calculate_only && !local && !gsym->is_weak_undefined()
4649 && ((x >> 26) != ((address + 4) >> (26 + shift))))
4650 return This::STATUS_OVERFLOW;
4652 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4654 // If required, turn JAL into JALX.
4655 if (cross_mode_jump)
4658 Valtype32 opcode = val >> 26;
4659 Valtype32 jalx_opcode;
4661 // Check to see if the opcode is already JAL or JALX.
4662 if (r_type == elfcpp::R_MIPS16_26)
4664 ok = (opcode == 0x6) || (opcode == 0x7);
4667 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4669 ok = (opcode == 0x3d) || (opcode == 0x3c);
4674 ok = (opcode == 0x3) || (opcode == 0x1d);
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)
4682 gold_error(_("Unsupported jump between ISA modes; consider "
4683 "recompiling with interlinking enabled."));
4684 return This::STATUS_BAD_RELOC;
4687 // Make this the JALX opcode.
4688 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4691 // Try converting JAL to BAL, if the target is in range.
4692 if (!parameters->options().relocatable()
4695 && r_type == elfcpp::R_MIPS_26
4696 && (val >> 26) == 0x3))) // jal addr
4698 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4699 int offset = dest - (address + 4);
4700 if (!Bits<18>::has_overflow32(offset))
4702 if (val == 0x03200008) // jr t9
4703 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4705 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4710 *calculated_value = val;
4712 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4714 return This::STATUS_OKAY;
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)
4724 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4725 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4727 Valtype addend = (extract_addend
4728 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4731 Valtype x = psymval->value(object, addend) - address;
4732 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4736 *calculated_value = x >> 2;
4737 return This::STATUS_OKAY;
4740 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4742 if (psymval->value(object, addend) & 3)
4743 return This::STATUS_PCREL_UNALIGNED;
4745 return check_overflow<18>(x);
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)
4755 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4756 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4758 Valtype addend = (extract_addend
4759 ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4762 Valtype x = psymval->value(object, addend) - address;
4763 val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4767 *calculated_value = x >> 2;
4768 return This::STATUS_OKAY;
4771 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4773 if (psymval->value(object, addend) & 3)
4774 return This::STATUS_PCREL_UNALIGNED;
4776 return check_overflow<23>(x);
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)
4786 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4787 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4789 Valtype addend = (extract_addend
4790 ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4793 Valtype x = psymval->value(object, addend) - address;
4794 val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4798 *calculated_value = x >> 2;
4799 return This::STATUS_OKAY;
4802 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4804 if (psymval->value(object, addend) & 3)
4805 return This::STATUS_PCREL_UNALIGNED;
4807 return check_overflow<28>(x);
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)
4817 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4818 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4820 Valtype addend = (extract_addend
4821 ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4824 Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4825 val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4829 *calculated_value = x >> 3;
4830 return This::STATUS_OKAY;
4833 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4835 if (psymval->value(object, addend) & 7)
4836 return This::STATUS_PCREL_UNALIGNED;
4838 return check_overflow<21>(x);
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)
4848 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4849 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4851 Valtype addend = (extract_addend
4852 ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4855 Valtype x = psymval->value(object, addend) - address;
4856 val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4860 *calculated_value = x >> 2;
4861 return This::STATUS_OKAY;
4864 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4866 if (psymval->value(object, addend) & 3)
4867 return This::STATUS_PCREL_UNALIGNED;
4869 return check_overflow<21>(x);
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)
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;
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)
4891 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4892 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4894 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4897 Valtype value = psymval->value(object, addend) - address;
4898 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4899 val = Bits<32>::bit_select32(val, x, 0xffff);
4902 *calculated_value = x;
4904 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4906 return This::STATUS_OKAY;
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)
4917 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4918 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4920 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4923 if (rel_type == elfcpp::SHT_REL)
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())
4930 reloc_high<size, big_endian> pchi16 = *it;
4931 if (pchi16.r_sym == r_sym)
4933 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4934 pchi16.addend, pchi16.address,
4935 pchi16.extract_addend, addend, calculate_only,
4937 it = pchi16_relocs.erase(it);
4944 // Resolve R_MIPS_PCLO16 relocation.
4945 Valtype x = psymval->value(object, addend) - address;
4946 val = Bits<32>::bit_select32(val, x, 0xffff);
4949 *calculated_value = x;
4951 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4953 return This::STATUS_OKAY;
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)
4964 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4965 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4967 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4970 Valtype x = psymval->value(object, addend) - address;
4971 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4975 *calculated_value = x >> 1;
4976 return This::STATUS_OKAY;
4979 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4981 return check_overflow<8>(x);
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)
4992 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4993 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4995 Valtype addend = (extract_addend
4996 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4999 Valtype x = psymval->value(object, addend) - address;
5000 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
5004 *calculated_value = x >> 1;
5005 return This::STATUS_OKAY;
5008 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5010 return check_overflow<11>(x);
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)
5021 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5022 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5024 Valtype addend = (extract_addend
5025 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
5028 Valtype x = psymval->value(object, addend) - address;
5029 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
5033 *calculated_value = x >> 1;
5034 return This::STATUS_OKAY;
5037 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5039 return check_overflow<17>(x);
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)
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,
5053 return This::STATUS_OKAY;
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)
5065 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5066 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5068 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5073 value = psymval->value(object, addend);
5076 // For MIPS16 ABI code we generate this sequence
5077 // 0: li $v0,%hi(_gp_disp)
5078 // 4: addiupc $v1,%lo(_gp_disp)
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.
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;
5096 gp_disp = target->adjusted_gp_value(object) - address;
5097 value = gp_disp + addend;
5099 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
5100 val = Bits<32>::bit_select32(val, x, 0xffff);
5104 *calculated_value = x;
5105 return This::STATUS_OKAY;
5108 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5110 return (is_gp_disp ? check_overflow<16>(x)
5111 : This::STATUS_OKAY);
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)
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;
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)
5136 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5137 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5139 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5142 // Find GOT page entry.
5143 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5146 unsigned int got_offset =
5147 target->got_section()->get_got_page_offset(value, object);
5149 // Resolve the relocation.
5150 Valtype x = target->got_section()->gp_offset(got_offset, object);
5151 val = Bits<32>::bit_select32(val, x, 0xffff);
5155 *calculated_value = x;
5156 return This::STATUS_OKAY;
5159 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5161 return check_overflow<16>(x);
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)
5173 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5174 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5176 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5179 if (rel_type == elfcpp::SHT_REL)
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())
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))
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);
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())
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))
5214 mips_reloc_unshuffle(got16.view, got16.r_type, false);
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);
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);
5231 // Resolve R_MIPS_LO16 relocation.
5234 x = psymval->value(object, addend);
5237 // See the comment for R_MIPS16_HI16 above for the reason
5238 // for this conditional.
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;
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:
5252 // lui $gp,%hi(_gp_disp)
5253 // addiu $gp,$gp,%lo(_gp_disp)
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;
5264 val = Bits<32>::bit_select32(val, x, 0xffff);
5267 *calculated_value = x;
5269 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5271 return This::STATUS_OKAY;
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)
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);
5291 *calculated_value = x;
5292 return This::STATUS_OKAY;
5295 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5297 return check_overflow<16>(x);
5301 static inline typename This::Status
5302 releh(unsigned char* view, int gp_offset, bool calculate_only,
5303 Valtype* calculated_value)
5305 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5306 Valtype x = gp_offset;
5310 *calculated_value = x;
5311 return This::STATUS_OKAY;
5314 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5316 return check_overflow<32>(x);
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)
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;
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);
5336 Valtype x = target->got_section()->gp_offset(got_offset, object);
5337 val = Bits<32>::bit_select32(val, x, 0xffff);
5341 *calculated_value = x;
5342 return This::STATUS_OKAY;
5345 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5347 return check_overflow<16>(x);
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)
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;
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.
5369 // Find GOT page entry.
5370 Mips_address value = ((psymval->value(object, addend) + 0x8000)
5372 target->got_section()->get_got_page_offset(value, object);
5374 x = psymval->value(object, addend) - value;
5378 val = Bits<32>::bit_select32(val, x, 0xffff);
5382 *calculated_value = x;
5383 return This::STATUS_OKAY;
5386 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5388 return check_overflow<16>(x);
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)
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);
5404 *calculated_value = x;
5406 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5408 return This::STATUS_OKAY;
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)
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);
5423 *calculated_value = x;
5425 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5427 return This::STATUS_OKAY;
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)
5439 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5440 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5445 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5446 addend = (val & 0x7f) << 2;
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);
5457 Valtype x = psymval->value(object, addend) - gp;
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
5465 x += object->gp_value();
5467 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5468 val = Bits<32>::bit_select32(val, x, 0x7f);
5470 val = Bits<32>::bit_select32(val, x, 0xffff);
5474 *calculated_value = x;
5475 return This::STATUS_OKAY;
5478 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5480 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5482 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5483 "limit (see option -G)"));
5484 return This::STATUS_OVERFLOW;
5486 return This::STATUS_OKAY;
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)
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;
5500 // R_MIPS_GPREL32 relocations are defined for local symbols only.
5501 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5504 *calculated_value = x;
5506 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5508 return This::STATUS_OKAY;
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)
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;
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);
5529 *calculated_value = x;
5531 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5533 return This::STATUS_OKAY;
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)
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;
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);
5554 *calculated_value = x;
5556 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5558 return This::STATUS_OKAY;
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)
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;
5573 // tls symbol values are relative to tls_segment()->vaddr()
5574 Valtype x = psymval->value(object, addend) - tp_offset;
5577 *calculated_value = x;
5579 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5581 return This::STATUS_OKAY;
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)
5590 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5591 Valtype64 addend = (extract_addend
5592 ? elfcpp::Swap<64, big_endian>::readval(wv)
5595 Valtype64 x = psymval->value(object, -addend);
5597 *calculated_value = x;
5599 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5601 return This::STATUS_OKAY;
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)
5611 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5612 Valtype64 addend = (extract_addend
5613 ? elfcpp::Swap<64, big_endian>::readval(wv)
5616 Valtype64 x = psymval->value(object, addend);
5618 *calculated_value = x;
5621 if (apply_addend_only)
5623 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5626 return This::STATUS_OKAY;
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)
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)
5640 Valtype x = psymval->value(object, addend);
5641 x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
5642 val = Bits<32>::bit_select32(val, x, 0xffff);
5645 *calculated_value = x;
5647 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5649 return This::STATUS_OKAY;
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)
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)
5664 Valtype x = psymval->value(object, addend);
5665 x = ((x + (uint64_t) 0x800080008000llu) >> 48) & 0xffff;
5666 val = Bits<32>::bit_select32(val, x, 0xffff);
5669 *calculated_value = x;
5671 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5673 return This::STATUS_OKAY;
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;
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;
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;
5689 // Mips_got_info methods.
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.
5694 template<int size, bool big_endian>
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)
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);
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
5713 template<int size, bool big_endian>
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)
5720 mips_sym->set_got_not_only_for_calls();
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())
5725 switch (mips_sym->visibility())
5727 case elfcpp::STV_INTERNAL:
5728 case elfcpp::STV_HIDDEN:
5729 mips_sym->set_is_forced_local();
5732 mips_sym->set_needs_dynsym_entry();
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);
5743 if (mips_sym->global_got_area() == GGA_NONE)
5744 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5748 Mips_got_entry<size, big_endian>* entry =
5749 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5751 this->record_got_entry(entry, object);
5754 // Add ENTRY to master GOT and to OBJECT's GOT.
5756 template<int size, bool big_endian>
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)
5762 this->got_entries_.insert(entry);
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);
5769 g->got_entries_.insert(entry2);
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.
5778 template<int size, bool big_endian>
5780 Mips_got_info<size, big_endian>::record_got_page_entry(
5781 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5783 struct Got_page_range **range_ptr, *range;
5784 int old_pages, new_pages;
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())
5793 this->got_page_entries_.insert(entry);
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();
5798 // Skip over ranges whose maximum extent cannot share a page entry
5800 range_ptr = &entry->ranges;
5801 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5802 range_ptr = &(*range_ptr)->next;
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.
5808 if (!range || addend < range->min_addend - 0xffff)
5810 range = new Got_page_range();
5811 range->next = *range_ptr;
5812 range->min_addend = addend;
5813 range->max_addend = addend;
5816 ++this->page_gotno_;
5821 // Remember how many pages the old range contributed.
5822 old_pages = range->get_max_pages();
5824 // Update the ranges.
5825 if (addend < range->min_addend)
5826 range->min_addend = addend;
5827 else if (addend > range->max_addend)
5829 if (range->next && addend >= range->next->min_addend - 0xffff)
5831 old_pages += range->next->get_max_pages();
5832 range->max_addend = range->next->max_addend;
5833 range->next = range->next->next;
5836 range->max_addend = addend;
5839 // Record any change in the total estimate.
5840 new_pages = range->get_max_pages();
5841 if (old_pages != new_pages)
5843 this->page_gotno_ += new_pages - old_pages;
5844 g2->page_gotno_ += new_pages - old_pages;
5848 // Create all entries that should be in the local part of the GOT.
5850 template<int size, bool big_endian>
5852 Mips_got_info<size, big_endian>::add_local_entries(
5853 Target_mips<size, big_endian>* target, Layout* layout)
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());
5861 for (typename Got_entry_set::iterator
5862 p = this->got_entries_.begin();
5863 p != this->got_entries_.end();
5866 Mips_got_entry<size, big_endian>* entry = *p;
5867 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
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())
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);
5880 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5881 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5887 this->add_page_entries(target, layout);
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();
5895 Mips_got_entry<size, big_endian>* entry = *p;
5896 if (!entry->is_for_global_symbol())
5899 Mips_symbol<size>* mips_sym = entry->sym();
5900 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5902 unsigned int got_type;
5903 if (!got->multi_got())
5904 got_type = GOT_TYPE_STANDARD;
5906 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5907 if (got->add_global(mips_sym, got_type))
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));
5920 // Create GOT page entries.
5922 template<int size, bool big_endian>
5924 Mips_got_info<size, big_endian>::add_page_entries(
5925 Target_mips<size, big_endian>* target, Layout* layout)
5927 if (this->page_gotno_ == 0)
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)
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,
5945 gold_assert(next_offset == prev_offset + size/8);
5946 prev_offset = next_offset;
5948 this->got_page_offset_next_ = this->got_page_offset_start_;
5951 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5953 template<int size, bool big_endian>
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)
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();
5967 Mips_got_entry<size, big_endian>* entry = *p;
5968 if (!entry->is_for_global_symbol())
5971 Mips_symbol<size>* mips_sym = entry->sym();
5972 if (mips_sym->global_got_area() != GGA_NORMAL)
5975 unsigned int got_type;
5976 if (!got->multi_got())
5977 got_type = GOT_TYPE_STANDARD;
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))
5987 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5988 if (got->multi_got() && this->index_ == 0)
5990 if (got->multi_got() && this->index_ > 0)
5992 if (parameters->options().output_is_position_independent()
5993 || (!parameters->doing_static_link()
5994 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
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);
6005 if (!got->multi_got() || this->index_ == 0)
6007 if (got->multi_got())
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)
6014 got->add_constant(0);
6019 // Add GGA_RELOC_ONLY entries.
6020 got->add_reloc_only_entries();
6024 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6026 template<int size, bool big_endian>
6028 Mips_got_info<size, big_endian>::add_reloc_only_entries(
6029 Mips_output_data_got<size, big_endian>* got)
6031 for (typename Global_got_entry_set::iterator
6032 p = this->global_got_symbols_.begin();
6033 p != this->global_got_symbols_.end();
6036 Mips_symbol<size>* mips_sym = *p;
6037 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6039 unsigned int got_type;
6040 if (!got->multi_got())
6041 got_type = GOT_TYPE_STANDARD;
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));
6050 // Create TLS GOT entries.
6052 template<int size, bool big_endian>
6054 Mips_got_info<size, big_endian>::add_tls_entries(
6055 Target_mips<size, big_endian>* target, Layout* layout)
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();
6064 Mips_got_entry<size, big_endian>* entry = *p;
6065 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6068 if (entry->tls_type() == GOT_TLS_GD)
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);
6076 if (!parameters->doing_static_link())
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,
6085 got->add_static_reloc(got_offset + size/8, r_type2,
6086 entry->object(), entry->symndx());
6090 // We are doing a static link. Mark it as belong to module 1,
6092 unsigned int got_offset = got->add_constant(1);
6093 entry->object()->set_local_got_offset(entry->symndx(), got_type,
6096 got->add_constant(0);
6097 got->add_static_reloc(got_offset + size/8, r_type2,
6098 entry->object(), entry->symndx());
6101 else if (entry->tls_type() == GOT_TLS_IE)
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,
6112 got->add_local(entry->object(), entry->symndx(), got_type,
6114 unsigned int got_offset =
6115 entry->object()->local_got_offset(entry->symndx(), got_type,
6117 got->add_static_reloc(got_offset, r_type, entry->object(),
6121 else if (entry->tls_type() == GOT_TLS_LDM)
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())
6128 got_offset = got->add_constant(0);
6129 target->rel_dyn_section(layout)->add_local(
6130 entry->object(), 0, r_type, got, got_offset);
6133 // We are doing a static link. Just mark it as belong to module 1,
6135 got_offset = got->add_constant(1);
6137 got->add_constant(0);
6138 got->set_tls_ldm_offset(got_offset, entry->object());
6144 // Add global tls entries.
6145 for (typename Got_entry_set::iterator
6146 p = this->got_entries_.begin();
6147 p != this->got_entries_.end();
6150 Mips_got_entry<size, big_endian>* entry = *p;
6151 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6154 Mips_symbol<size>* mips_sym = entry->sym();
6155 if (entry->tls_type() == GOT_TLS_GD)
6157 unsigned int got_type;
6158 if (!got->multi_got())
6159 got_type = GOT_TYPE_TLS_PAIR;
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);
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);
6182 else if (entry->tls_type() == GOT_TLS_IE)
6184 unsigned int got_type;
6185 if (!got->multi_got())
6186 got_type = GOT_TYPE_TLS_OFFSET;
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);
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);
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).
6211 template<int size, bool big_endian>
6213 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6215 for (typename Global_got_entry_set::iterator
6216 p = this->global_got_symbols_.begin();
6217 p != this->global_got_symbols_.end();
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.
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)
6238 ++this->reloc_only_gotno_;
6239 ++this->global_gotno_ ;
6244 // Return the offset of GOT page entry for VALUE. Initialize the entry with
6245 // VALUE if it is not initialized.
6247 template<int size, bool big_endian>
6249 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6250 Mips_output_data_got<size, big_endian>* got)
6252 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6253 if (it != this->got_page_offsets_.end())
6256 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6257 + (size/8) * this->page_gotno_);
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);
6266 // Remove lazy-binding stubs for global symbols in this GOT.
6268 template<int size, bool big_endian>
6270 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6271 Target_mips<size, big_endian>* target)
6273 for (typename Got_entry_set::iterator
6274 p = this->got_entries_.begin();
6275 p != this->got_entries_.end();
6278 Mips_got_entry<size, big_endian>* entry = *p;
6279 if (entry->is_for_global_symbol())
6280 target->remove_lazy_stub_entry(entry->sym());
6284 // Count the number of GOT entries required.
6286 template<int size, bool big_endian>
6288 Mips_got_info<size, big_endian>::count_got_entries()
6290 for (typename Got_entry_set::iterator
6291 p = this->got_entries_.begin();
6292 p != this->got_entries_.end();
6295 this->count_got_entry(*p);
6299 // Count the number of GOT entries required by ENTRY. Accumulate the result.
6301 template<int size, bool big_endian>
6303 Mips_got_info<size, big_endian>::count_got_entry(
6304 Mips_got_entry<size, big_endian>* entry)
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_;
6312 ++this->global_gotno_;
6315 // Add FROM's GOT entries.
6317 template<int size, bool big_endian>
6319 Mips_got_info<size, big_endian>::add_got_entries(
6320 Mips_got_info<size, big_endian>* from)
6322 for (typename Got_entry_set::iterator
6323 p = from->got_entries_.begin();
6324 p != from->got_entries_.end();
6327 Mips_got_entry<size, big_endian>* entry = *p;
6328 if (this->got_entries_.find(entry) == this->got_entries_.end())
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);
6338 // Add FROM's GOT page entries.
6340 template<int size, bool big_endian>
6342 Mips_got_info<size, big_endian>::add_got_page_count(
6343 Mips_got_info<size, big_endian>* from)
6345 this->page_gotno_ += from->page_gotno_;
6348 // Mips_output_data_got methods.
6350 // Lay out the GOT. Add local, global and TLS entries. If GOT is
6351 // larger than 64K, create multi-GOT.
6353 template<int size, bool big_endian>
6355 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6356 Symbol_table* symtab, const Input_objects* input_objects)
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);
6362 // Count the number of GOT entries.
6363 this->master_got_info_->count_got_entries();
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);
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();
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_);
6381 this->master_got_info_->add_local_entries(this->target_, layout);
6382 this->master_got_info_->add_global_entries(this->target_, layout,
6384 this->master_got_info_->add_tls_entries(this->target_, layout);
6388 // Create multi-GOT. For every GOT, add local, global and TLS entries.
6390 template<int size, bool big_endian>
6392 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6393 const Input_objects* input_objects)
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);
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());
6406 unsigned int offset = 0;
6407 Mips_got_info<size, big_endian>* g = this->primary_got_;
6411 g->set_offset(offset);
6413 g->add_local_entries(this->target_, layout);
6415 g->add_global_entries(this->target_, layout,
6416 (this->master_got_info_->global_gotno()
6417 - this->master_got_info_->reloc_only_gotno()));
6419 g->add_global_entries(this->target_, layout, /*not used*/-1U);
6420 g->add_tls_entries(this->target_, layout);
6422 // Forbid global symbols in every non-primary GOT from having
6423 // lazy-binding stubs.
6425 g->remove_lazy_stubs(this->target_);
6428 offset += g->got_size();
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
6441 template<int size, bool big_endian>
6443 Mips_output_data_got<size, big_endian>::merge_gots(
6444 const Input_objects* input_objects)
6446 gold_assert(this->primary_got_ == NULL);
6447 Mips_got_info<size, big_endian>* current = NULL;
6449 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6450 p != input_objects->relobj_end();
6453 Mips_relobj<size, big_endian>* object =
6454 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6456 Mips_got_info<size, big_endian>* g = object->get_got_info();
6460 g->count_got_entries();
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();
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());
6475 unsigned int max_count =
6476 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6477 if (estimate <= max_count)
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_)
6483 this->primary_got_ = g;
6487 // Try merging with the primary GOT.
6488 if (this->merge_got_with(g, object, this->primary_got_))
6492 // If we can merge with the last-created GOT, do it.
6493 if (current && this->merge_got_with(g, object, current))
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);
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>();
6507 // Link primary GOT with secondary GOTs.
6508 this->primary_got_->set_next(current);
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.
6514 template<int size, bool big_endian>
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)
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();
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();
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();
6536 estimate += from->global_gotno() + to->global_gotno();
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)
6544 // Transfer the object's GOT information from FROM to TO.
6545 to->add_got_entries(from);
6546 to->add_got_page_count(from);
6548 // Record that OBJECT should use output GOT TO.
6549 object->set_got_info(to);
6554 // Write out the GOT.
6556 template<int size, bool big_endian>
6558 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6560 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6561 Mips_stubs_entry_set;
6563 // Call parent to write out GOT.
6564 Output_data_got<size, big_endian>::do_write(of);
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);
6571 // Needed for fixing values of .got section.
6572 this->got_view_ = oview;
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();
6580 Mips_symbol<size>* mips_sym = *p;
6581 if (mips_sym->has_lazy_stub())
6583 Valtype* wv = reinterpret_cast<Valtype*>(
6584 oview + this->get_primary_got_offset(mips_sym));
6586 this->target_->mips_stubs_section()->stub_address(mips_sym);
6587 elfcpp::Swap<size, big_endian>::writeval(wv, value);
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();
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))
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);
6609 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6614 if (!this->secondary_got_relocs_.empty())
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
6620 // TODO(sasa): This is workaround. It needs to be investigated further.
6622 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6624 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6625 if (reloc.symbol_is_global())
6627 Mips_symbol<size>* gsym = reloc.symbol();
6628 gold_assert(gsym != NULL);
6630 unsigned got_offset = reloc.got_offset();
6631 gold_assert(got_offset < oview_size);
6633 // Find primary GOT entry.
6634 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6635 oview + this->get_primary_got_offset(gsym));
6637 // Find secondary GOT entry.
6638 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6640 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
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();
6650 of->write_output_view(offset, oview_size, oview);
6653 // We are done if there is no fix up.
6654 if (this->static_relocs_.empty())
6657 Output_segment* tls_segment = this->layout_->tls_segment();
6658 gold_assert(tls_segment != NULL);
6660 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6662 Static_reloc& reloc(this->static_relocs_[i]);
6665 if (!reloc.symbol_is_global())
6667 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6668 const Symbol_value<size>* psymval =
6669 object->local_symbol(reloc.index());
6671 // We are doing static linking. Issue an error and skip this
6672 // relocation if the symbol is undefined or in a discarded_section.
6674 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6675 if ((shndx == elfcpp::SHN_UNDEF)
6677 && shndx != elfcpp::SHN_UNDEF
6678 && !object->is_section_included(shndx)
6679 && !this->symbol_table_->is_section_folded(object, shndx)))
6681 gold_error(_("undefined or discarded local symbol %u from "
6682 " object %s in GOT"),
6683 reloc.index(), reloc.relobj()->name().c_str());
6687 value = psymval->value(object, 0);
6691 const Mips_symbol<size>* gsym = reloc.symbol();
6692 gold_assert(gsym != NULL);
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())
6700 gold_error(_("undefined or discarded symbol %s in GOT"),
6705 if (!gsym->is_weak_undefined())
6706 value = gsym->value();
6711 unsigned got_offset = reloc.got_offset();
6712 gold_assert(got_offset < oview_size);
6714 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6717 switch (reloc.r_type())
6719 case elfcpp::R_MIPS_TLS_DTPMOD32:
6720 case elfcpp::R_MIPS_TLS_DTPMOD64:
6723 case elfcpp::R_MIPS_TLS_DTPREL32:
6724 case elfcpp::R_MIPS_TLS_DTPREL64:
6725 x = value - elfcpp::DTP_OFFSET;
6727 case elfcpp::R_MIPS_TLS_TPREL32:
6728 case elfcpp::R_MIPS_TLS_TPREL64:
6729 x = value - elfcpp::TP_OFFSET;
6736 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6739 of->write_output_view(offset, oview_size, oview);
6742 // Mips_relobj methods.
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.
6753 template<int size, bool big_endian>
6755 Mips_relobj<size, big_endian>::do_count_local_symbols(
6756 Stringpool_template<char>* pool,
6757 Stringpool_template<char>* dynpool)
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();
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);
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);
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);
6782 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6784 // Skip the first dummy symbol.
6786 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
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);
6796 // Read the symbol information.
6798 template<int size, bool big_endian>
6800 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6802 // Call parent class to read symbol information.
6803 this->base_read_symbols(sd);
6805 // If this input file is a binary file, it has no processor
6807 Input_file::Format format = this->input_file()->format();
6808 if (format != Input_file::FORMAT_ELF)
6810 gold_assert(format == Input_file::FORMAT_BINARY);
6811 this->merge_processor_specific_data_ = false;
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,
6819 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6820 this->processor_specific_flags_ = ehdr.get_e_flags();
6822 // Get the section names.
6823 const unsigned char* pnamesu = sd->section_names->data();
6824 const char* pnames = reinterpret_cast<const char*>(pnamesu);
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);
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)
6837 elfcpp::Shdr<size, big_endian> shdr(ps);
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)
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;
6850 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6851 // If this is neither an empty symbol table nor a string table,
6853 must_merge_processor_specific_data = true;
6855 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
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);
6867 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
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);
6877 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
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);
6889 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
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>();
6899 this->abiflags_->version =
6900 elfcpp::Swap<16, big_endian>::readval(view);
6901 if (this->abiflags_->version != 0)
6903 gold_error(_("%s: .MIPS.abiflags section has "
6904 "unsupported version %u"),
6905 this->name().c_str(),
6906 this->abiflags_->version);
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);
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:
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];
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.
6951 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
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;
6960 while (view + 8 <= end)
6962 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6963 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6966 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6968 this->name().c_str(),
6969 this->mips_elf_options_section_name(), sz);
6973 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6975 // In the 64 bit ABI, an ODK_REGINFO option is the following
6976 // structure. The info field of the options header is not
6981 // // Mask of general purpose registers used.
6982 // unsigned char ri_gprmask[4];
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];
6991 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6994 else if (kind == elfcpp::ODK_REGINFO)
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.
7002 // unsigned char ri_gprmask[4];
7003 // unsigned char ri_cprmask[4][4];
7004 // unsigned char ri_gp_value[4];
7007 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
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);
7021 if (strcmp(name, ".pdr") == 0)
7023 gold_assert(this->pdr_shndx_ == -1U);
7024 this->pdr_shndx_ = i;
7029 if (!must_merge_processor_specific_data)
7030 this->merge_processor_specific_data_ = false;
7033 // Discard MIPS16 stub secions that are not needed.
7035 template<int size, bool big_endian>
7037 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7039 for (typename Mips16_stubs_int_map::const_iterator
7040 it = this->mips16_stub_sections_.begin();
7041 it != this->mips16_stub_sections_.end(); ++it)
7043 Mips16_stub_section<size, big_endian>* stub_section = it->second;
7044 if (!stub_section->is_target_found())
7046 gold_error(_("no relocation found in mips16 stub section '%s'"),
7047 stub_section->object()
7048 ->section_name(stub_section->shndx()).c_str());
7051 bool discard = false;
7052 if (stub_section->is_for_local_function())
7054 if (stub_section->is_fn_stub())
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
7060 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7063 this->add_local_mips16_fn_stub(stub_section);
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()))
7075 this->add_local_mips16_call_stub(stub_section);
7080 Mips_symbol<size>* gsym = stub_section->gsym();
7081 if (stub_section->is_fn_stub())
7083 if (gsym->has_mips16_fn_stub())
7084 // We already have a stub for this function.
7088 gsym->set_mips16_fn_stub(stub_section);
7089 if (gsym->should_add_dynsym_entry(symtab))
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();
7099 if (!gsym->need_fn_stub())
7102 else if (stub_section->is_call_stub())
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
7109 else if (gsym->has_mips16_call_stub())
7110 // We already have a stub for this function.
7113 gsym->set_mips16_call_stub(stub_section);
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
7123 else if (gsym->has_mips16_call_fp_stub())
7124 // We already have a stub for this function.
7127 gsym->set_mips16_call_fp_stub(stub_section);
7131 this->set_output_section(stub_section->shndx(), NULL);
7135 // Mips_output_data_la25_stub methods.
7137 // Template for standard LA25 stub.
7138 template<int size, bool big_endian>
7140 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7142 0x3c190000, // lui $25,%hi(func)
7143 0x08000000, // j func
7144 0x27390000, // add $25,$25,%lo(func)
7148 // Template for microMIPS LA25 stub.
7149 template<int size, bool big_endian>
7151 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7153 0x41b9, 0x0000, // lui t9,%hi(func)
7154 0xd400, 0x0000, // j func
7155 0x3339, 0x0000, // addiu t9,t9,%lo(func)
7156 0x0000, 0x0000 // nop
7159 // Create la25 stub for a symbol.
7161 template<int size, bool big_endian>
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)
7167 if (!gsym->has_la25_stub())
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);
7175 // Create a symbol for SYM stub's value and size, to help make the disassembly
7178 template<int size, bool big_endian>
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)
7184 std::string name(".pic.");
7185 name += sym->name();
7187 unsigned int offset = sym->la25_stub_offset();
7188 if (sym->is_micromips())
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,
7197 elfcpp::STV_DEFAULT, 0,
7199 new_sym->set_is_forced_local();
7202 // Write out la25 stubs. This uses the hand-coded instructions above,
7203 // and adjusts them as needed.
7205 template<int size, bool big_endian>
7207 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
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);
7214 for (typename std::vector<Mips_symbol<size>*>::iterator
7215 p = this->symbols_.begin();
7216 p != this->symbols_.end();
7219 Mips_symbol<size>* sym = *p;
7220 unsigned char* pov = oview + sym->la25_stub_offset();
7222 Mips_address target = sym->value();
7223 if (!sym->is_micromips())
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]);
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
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]);
7259 of->write_output_view(offset, oview_size, oview);
7262 // Mips_output_data_plt methods.
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[] =
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
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[] =
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
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[] =
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
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.
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[] =
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
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[] =
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
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[] =
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)
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[] =
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)
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[] =
7373 0xb303, // lw $3, 12($pc)
7374 0x651b, // move $24, $3
7375 0x9b60, // lw $3, 0($3)
7377 0x653b, // move $25, $3
7379 0x0000, 0x0000 // .word (.got.plt entry)
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[] =
7388 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
7389 0xff22, 0x0000, // lw $25, 0($2)
7391 0x0f02 // move $24, $2
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[] =
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)
7405 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7407 template<int size, bool big_endian>
7409 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7410 unsigned int r_type)
7412 gold_assert(!gsym->has_plt_offset());
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);
7419 // Record whether the relocation requires a standard MIPS
7420 // or a compressed code entry.
7421 if (jal_reloc(r_type))
7423 if (r_type == elfcpp::R_MIPS_26)
7424 gsym->set_needs_mips_plt(true);
7426 gsym->set_needs_comp_plt(true);
7429 section_offset_type got_offset = this->got_plt_->current_data_size();
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);
7436 gsym->set_needs_dynsym_entry();
7437 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7441 // Set final PLT offsets. For each symbol, determine whether standard or
7442 // compressed (MIPS16 or microMIPS) PLT entry is used.
7444 template<int size, bool big_endian>
7446 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
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);
7453 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7454 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7456 Mips_symbol<size>* mips_sym = *p;
7458 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7459 // so always use a standard entry there.
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
7466 if (this->target_->is_output_newabi()
7467 || mips_sym->has_mips16_call_stub()
7468 || mips_sym->has_mips16_call_fp_stub())
7470 mips_sym->set_needs_mips_plt(true);
7471 mips_sym->set_needs_comp_plt(false);
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())
7482 if (this->target_->is_output_micromips())
7483 mips_sym->set_needs_comp_plt(true);
7485 mips_sym->set_needs_mips_plt(true);
7488 if (mips_sym->needs_mips_plt())
7490 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7491 this->plt_mips_offset_ += plt_mips_entry_size;
7493 if (mips_sym->needs_comp_plt())
7495 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7496 this->plt_comp_offset_ += plt_comp_entry_size;
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();
7505 // Write out the PLT. This uses the hand-coded instructions above,
7506 // and adjusts them as needed.
7508 template<int size, bool big_endian>
7510 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
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);
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,
7522 unsigned char* pov = oview;
7524 Mips_address plt_address = this->address();
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;
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);
7536 // Write the PLT header.
7537 const uint32_t* plt0_entry = this->get_plt_header_entry();
7538 if (plt0_entry == plt0_entry_micromips_o32)
7540 // Write microMIPS PLT header.
7541 gold_assert(gotplt_addr % 4 == 0);
7543 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7545 // ADDIUPC has a span of +/-16MB, check we're in range.
7546 if (gotpc_offset + 0x1000000 >= 0x2000000)
7548 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7549 "ADDIUPC"), (long)gotpc_offset);
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);
7558 for (unsigned int i = 2;
7559 i < (sizeof(plt0_entry_micromips_o32)
7560 / sizeof(plt0_entry_micromips_o32[0]));
7563 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7567 else if (plt0_entry == plt0_entry_micromips32_o32)
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);
7577 for (unsigned int i = 6;
7578 i < (sizeof(plt0_entry_micromips32_o32)
7579 / sizeof(plt0_entry_micromips32_o32[0]));
7582 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
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);
7596 for (int i = 3; i < 8; i++)
7598 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7604 unsigned char* gotplt_pov = gotplt_view;
7605 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
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);
7611 unsigned int gotplt_offset = 2 * got_entry_size;
7612 gotplt_pov += 2 * got_entry_size;
7614 // Calculate the address of the PLT header.
7615 Mips_address header_address = (plt_address
7616 + (this->is_plt_header_compressed() ? 1 : 0));
7618 // Initialize compressed PLT area view.
7619 unsigned char* pov2 = pov + this->plt_mips_offset_;
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)
7627 Mips_symbol<size>* mips_sym = *p;
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)
7633 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
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);
7639 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7641 // Now handle the PLT itself. First the standard entry.
7642 if (mips_sym->has_mips_plt_offset())
7644 // Pick the load opcode (LW or LD).
7645 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7648 const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
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);
7662 // Now the compressed entry. They come after any standard ones.
7663 if (mips_sym->has_comp_plt_offset())
7665 if (!this->target_->is_output_micromips())
7667 // Write MIPS16 PLT entry.
7668 const uint32_t* plt_entry = plt_entry_mips16_o32;
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,
7680 else if (this->target_->use_32bit_micromips_instructions())
7682 // Write microMIPS PLT entry in insn32 mode.
7683 const uint32_t* plt_entry = plt_entry_micromips32_o32;
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);
7700 // Write microMIPS PLT entry.
7701 const uint32_t* plt_entry = plt_entry_micromips_o32;
7703 gold_assert(gotplt_entry_addr % 4 == 0);
7705 Mips_address loc_address = plt_address + pov2 - oview;
7706 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7708 // ADDIUPC has a span of +/-16MB, check we're in range.
7709 if (gotpc_offset + 0x1000000 >= 0x2000000)
7711 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7712 "range of ADDIUPC"), (long)gotpc_offset);
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]);
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);
7738 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7741 of->write_output_view(offset, oview_size, oview);
7742 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7745 // Mips_output_data_mips_stubs methods.
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>
7751 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
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
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>
7763 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
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
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>
7775 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
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
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>
7787 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
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
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] =
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
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>
7811 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
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
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>
7826 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7828 0xff3c, 0x8010, // lw t9,0x8010(gp)
7829 0x0dff, // move t7,ra
7831 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
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>
7838 Mips_output_data_mips_stubs<size, big_endian>::
7839 lazy_stub_micromips_normal_1_n64[] =
7841 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7842 0x0dff, // move t7,ra
7844 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
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>
7852 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7854 0xff3c, 0x8010, // lw t9,0x8010(gp)
7855 0x0dff, // move t7,ra
7857 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
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,
7863 template<int size, bool big_endian>
7865 Mips_output_data_mips_stubs<size, big_endian>::
7866 lazy_stub_micromips_normal_2_n64[] =
7868 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7869 0x0dff, // move t7,ra
7871 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
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>
7878 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7880 0xff3c, 0x8010, // lw t9,0x8010(gp)
7881 0x0dff, // move t7,ra
7882 0x41b8, 0x0000, // lui t8,DYN_INDEX
7884 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
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>
7891 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7893 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7894 0x0dff, // move t7,ra
7895 0x41b8, 0x0000, // lui t8,DYN_INDEX
7897 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7900 // 32-bit microMIPS stubs.
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>
7907 Mips_output_data_mips_stubs<size, big_endian>::
7908 lazy_stub_micromips32_normal_1[] =
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
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>
7921 Mips_output_data_mips_stubs<size, big_endian>::
7922 lazy_stub_micromips32_normal_1_n64[] =
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
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>
7935 Mips_output_data_mips_stubs<size, big_endian>::
7936 lazy_stub_micromips32_normal_2[] =
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
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>
7949 Mips_output_data_mips_stubs<size, big_endian>::
7950 lazy_stub_micromips32_normal_2_n64[] =
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
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>
7962 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
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
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>
7975 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
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
7984 // Create entry for a symbol.
7986 template<int size, bool big_endian>
7988 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7989 Mips_symbol<size>* gsym)
7991 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7993 this->symbols_.insert(gsym);
7994 gsym->set_has_lazy_stub(true);
7998 // Remove entry for a symbol.
8000 template<int size, bool big_endian>
8002 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8003 Mips_symbol<size>* gsym)
8005 if (gsym->has_lazy_stub())
8007 this->symbols_.erase(gsym);
8008 gsym->set_has_lazy_stub(false);
8012 // Set stub offsets for symbols. This method expects that the number of
8013 // entries in dynamic symbol table is set.
8015 template<int size, bool big_endian>
8017 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8019 gold_assert(this->dynsym_count_ != -1U);
8021 if (this->stub_offsets_are_set_)
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)
8031 Mips_symbol<size>* mips_sym = *p;
8032 mips_sym->set_lazy_stub_offset(offset);
8034 this->stub_offsets_are_set_ = true;
8037 template<int size, bool big_endian>
8039 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8041 for (typename Mips_stubs_entry_set::const_iterator
8042 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8044 Mips_symbol<size>* sym = *p;
8045 if (sym->is_from_dynobj())
8046 sym->set_needs_dynsym_value();
8050 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
8051 // adjusts them as needed.
8053 template<int size, bool big_endian>
8055 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
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);
8062 bool big_stub = this->dynsym_count_ > 0x10000;
8064 unsigned char* pov = oview;
8065 for (typename Mips_stubs_entry_set::const_iterator
8066 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8068 Mips_symbol<size>* sym = *p;
8069 const uint32_t* lazy_stub;
8070 bool n64 = this->target_->is_output_n64();
8072 if (!this->target_->is_output_micromips())
8074 // Write standard (non-microMIPS) stub.
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;
8081 // Dynsym index is less than 32K.
8082 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8085 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8088 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8089 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8095 // LUI instruction of the big stub. Paste high 16 bits of the
8097 elfcpp::Swap<32, big_endian>::writeval(pov,
8098 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
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));
8108 else if (this->target_->use_32bit_micromips_instructions())
8110 // Write microMIPS stub in insn32 mode.
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;
8118 // Dynsym index is less than 32K.
8119 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8120 : lazy_stub_micromips32_normal_1;
8123 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8124 : lazy_stub_micromips32_big;
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]);
8140 // LUI instruction of the big stub. Paste high 16 bits of the
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);
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);
8158 // Write microMIPS stub.
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;
8166 // Dynsym index is less than 32K.
8167 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8168 : lazy_stub_micromips_normal_1;
8171 lazy_stub = n64 ? lazy_stub_micromips_big_n64
8172 : lazy_stub_micromips_big;
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]);
8187 // LUI instruction of the big stub. Paste high 16 bits of the
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);
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);
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);
8211 // Fill the unused space with zeroes.
8212 // TODO(sasa): Can we strip unused bytes during the relaxation?
8214 memset(pov, 0, unused);
8216 of->write_output_view(offset, oview_size, oview);
8219 // Mips_output_section_reginfo methods.
8221 template<int size, bool big_endian>
8223 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8225 off_t offset = this->offset();
8226 off_t data_size = this->data_size();
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());
8238 of->write_output_view(offset, data_size, view);
8241 // Mips_output_section_options methods.
8243 template<int size, bool big_endian>
8245 Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
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;
8253 while (view + 8 <= end)
8255 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8256 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8259 gold_error(_("Warning: bad `%s' option size %u smaller "
8260 "than its header in output section"),
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());
8276 of->write_output_view(offset, oview_size, view);
8279 // Mips_output_section_abiflags methods.
8281 template<int size, bool big_endian>
8283 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8285 off_t offset = this->offset();
8286 off_t data_size = this->data_size();
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);
8301 of->write_output_view(offset, data_size, view);
8304 // Mips_copy_relocs methods.
8306 // Emit any saved relocs.
8308 template<int sh_type, int size, bool big_endian>
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)
8314 for (typename Copy_relocs<sh_type, size, big_endian>::
8315 Copy_reloc_entries::iterator p = this->entries_.begin();
8316 p != this->entries_.end();
8318 emit_entry(*p, reloc_section, symtab, layout, target);
8320 // We no longer need the saved information.
8321 this->entries_.clear();
8324 // Emit the reloc if appropriate.
8326 template<int sh_type, int size, bool big_endian>
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)
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())
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);
8343 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8344 if (can_make_dynamic && !sym->has_static_relocs())
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_);
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_);
8359 this->make_copy_reloc(symtab, layout,
8360 static_cast<Sized_symbol<size>*>(entry.sym_),
8365 // Target_mips methods.
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.
8372 template<int size, bool big_endian>
8374 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8377 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8379 if (!mips_sym->has_lazy_stub())
8381 if (mips_sym->has_plt_offset())
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())
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);
8393 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
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();
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);
8411 if (mips_sym->has_mips16_fn_stub())
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
8416 value = mips_sym->template
8417 get_mips16_fn_stub<big_endian>()->output_address();
8423 // Get the dynamic reloc section, creating it if necessary. It's always
8424 // .rel.dyn, even for MIPS64.
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)
8430 if (this->rel_dyn_ == NULL)
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);
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);
8444 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8446 return this->rel_dyn_;
8449 // Get the GOT section, creating it if necessary.
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,
8456 if (this->got_ == NULL)
8458 gold_assert(symtab != NULL && layout != NULL);
8460 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
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);
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,
8471 0, 0, elfcpp::STT_OBJECT,
8473 elfcpp::STV_HIDDEN, 0,
8480 // Calculate value of _gp symbol.
8482 template<int size, bool big_endian>
8484 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8486 gold_assert(this->gp_ == NULL);
8488 Sized_symbol<size>* gp =
8489 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8491 // Set _gp symbol if the linker script hasn't created it.
8492 if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
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"));
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,
8505 elfcpp::STV_DEFAULT,
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.
8517 template<int size, bool big_endian>
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
8524 std::vector<Symbol*> non_got_symbols;
8525 std::vector<Symbol*> got_symbols;
8527 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8530 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8531 p != non_got_symbols.end();
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.
8540 if (!sym->has_dynsym_index())
8542 sym->set_dynsym_index(index);
8544 syms->push_back(sym);
8545 dynpool->add(sym->name(), false, NULL);
8547 // Record any version information.
8548 if (sym->version() != NULL)
8549 versions->record_version(symtab, dynpool, sym);
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();
8559 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8560 p != got_symbols.end();
8564 if (!sym->has_dynsym_index())
8566 // Record any version information.
8567 if (sym->version() != NULL)
8568 versions->record_version(symtab, dynpool, sym);
8572 index = versions->finalize(symtab, index, syms);
8574 int got_sym_count = 0;
8575 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8576 p != got_symbols.end();
8581 if (!sym->has_dynsym_index())
8584 sym->set_dynsym_index(index);
8586 syms->push_back(sym);
8587 dynpool->add(sym->name(), false, NULL);
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();
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);
8601 if (this->mips_stubs_ != NULL)
8602 this->mips_stubs_->set_dynsym_count(index);
8607 // Create a PLT entry for a global symbol referenced by r_type relocation.
8609 template<int size, bool big_endian>
8611 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8613 Mips_symbol<size>* gsym,
8614 unsigned int r_type)
8616 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8619 if (this->plt_ == NULL)
8621 // Create the GOT section first.
8622 this->got_section(symtab, layout);
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);
8629 // The first two entries are reserved.
8630 this->got_plt_->set_current_data_size(2 * size/8);
8632 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8635 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8637 | elfcpp::SHF_EXECINSTR),
8638 this->plt_, ORDER_PLT, false);
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());
8645 this->plt_->add_entry(gsym, r_type);
8649 // Get the .MIPS.stubs section, creating it if necessary.
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)
8655 if (this->mips_stubs_ == NULL)
8658 new Mips_output_data_mips_stubs<size, big_endian>(this);
8659 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8661 | elfcpp::SHF_EXECINSTR),
8662 this->mips_stubs_, ORDER_PLT, false);
8664 return this->mips_stubs_;
8667 // Get the LA25 stub section, creating it if necessary.
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)
8673 if (this->la25_stub_ == NULL)
8675 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8676 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8678 | elfcpp::SHF_EXECINSTR),
8679 this->la25_stub_, ORDER_TEXT, false);
8681 return this->la25_stub_;
8684 // Process the relocations to determine unreferenced sections for
8685 // garbage collection.
8687 template<int size, bool big_endian>
8689 Target_mips<size, big_endian>::gc_process_relocs(
8690 Symbol_table* symtab,
8692 Sized_relobj_file<size, big_endian>* object,
8693 unsigned int data_shndx,
8694 unsigned int sh_type,
8695 const unsigned char* prelocs,
8697 Output_section* output_section,
8698 bool needs_special_offset_handling,
8699 size_t local_symbol_count,
8700 const unsigned char* plocal_symbols)
8702 typedef Target_mips<size, big_endian> Mips;
8704 if (sh_type == elfcpp::SHT_REL)
8706 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8709 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8718 needs_special_offset_handling,
8722 else if (sh_type == elfcpp::SHT_RELA)
8724 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8727 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8736 needs_special_offset_handling,
8744 // Scan relocations for a section.
8746 template<int size, bool big_endian>
8748 Target_mips<size, big_endian>::scan_relocs(
8749 Symbol_table* symtab,
8751 Sized_relobj_file<size, big_endian>* object,
8752 unsigned int data_shndx,
8753 unsigned int sh_type,
8754 const unsigned char* prelocs,
8756 Output_section* output_section,
8757 bool needs_special_offset_handling,
8758 size_t local_symbol_count,
8759 const unsigned char* plocal_symbols)
8761 typedef Target_mips<size, big_endian> Mips;
8763 if (sh_type == elfcpp::SHT_REL)
8765 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8768 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8777 needs_special_offset_handling,
8781 else if (sh_type == elfcpp::SHT_RELA)
8783 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8786 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8795 needs_special_offset_handling,
8801 template<int size, bool big_endian>
8803 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
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);
8815 // Return the MACH for a MIPS e_flags value.
8816 template<int size, bool big_endian>
8818 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8820 switch (flags & elfcpp::EF_MIPS_MACH)
8822 case elfcpp::E_MIPS_MACH_3900:
8823 return mach_mips3900;
8825 case elfcpp::E_MIPS_MACH_4010:
8826 return mach_mips4010;
8828 case elfcpp::E_MIPS_MACH_4100:
8829 return mach_mips4100;
8831 case elfcpp::E_MIPS_MACH_4111:
8832 return mach_mips4111;
8834 case elfcpp::E_MIPS_MACH_4120:
8835 return mach_mips4120;
8837 case elfcpp::E_MIPS_MACH_4650:
8838 return mach_mips4650;
8840 case elfcpp::E_MIPS_MACH_5400:
8841 return mach_mips5400;
8843 case elfcpp::E_MIPS_MACH_5500:
8844 return mach_mips5500;
8846 case elfcpp::E_MIPS_MACH_5900:
8847 return mach_mips5900;
8849 case elfcpp::E_MIPS_MACH_9000:
8850 return mach_mips9000;
8852 case elfcpp::E_MIPS_MACH_SB1:
8853 return mach_mips_sb1;
8855 case elfcpp::E_MIPS_MACH_LS2E:
8856 return mach_mips_loongson_2e;
8858 case elfcpp::E_MIPS_MACH_LS2F:
8859 return mach_mips_loongson_2f;
8861 case elfcpp::E_MIPS_MACH_GS464:
8862 return mach_mips_gs464;
8864 case elfcpp::E_MIPS_MACH_OCTEON3:
8865 return mach_mips_octeon3;
8867 case elfcpp::E_MIPS_MACH_OCTEON2:
8868 return mach_mips_octeon2;
8870 case elfcpp::E_MIPS_MACH_OCTEON:
8871 return mach_mips_octeon;
8873 case elfcpp::E_MIPS_MACH_XLR:
8874 return mach_mips_xlr;
8877 switch (flags & elfcpp::EF_MIPS_ARCH)
8880 case elfcpp::E_MIPS_ARCH_1:
8881 return mach_mips3000;
8883 case elfcpp::E_MIPS_ARCH_2:
8884 return mach_mips6000;
8886 case elfcpp::E_MIPS_ARCH_3:
8887 return mach_mips4000;
8889 case elfcpp::E_MIPS_ARCH_4:
8890 return mach_mips8000;
8892 case elfcpp::E_MIPS_ARCH_5:
8895 case elfcpp::E_MIPS_ARCH_32:
8896 return mach_mipsisa32;
8898 case elfcpp::E_MIPS_ARCH_64:
8899 return mach_mipsisa64;
8901 case elfcpp::E_MIPS_ARCH_32R2:
8902 return mach_mipsisa32r2;
8904 case elfcpp::E_MIPS_ARCH_32R6:
8905 return mach_mipsisa32r6;
8907 case elfcpp::E_MIPS_ARCH_64R2:
8908 return mach_mipsisa64r2;
8910 case elfcpp::E_MIPS_ARCH_64R6:
8911 return mach_mipsisa64r6;
8918 // Return the MACH for each .MIPS.abiflags ISA Extension.
8920 template<int size, bool big_endian>
8922 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8926 case elfcpp::AFL_EXT_3900:
8927 return mach_mips3900;
8929 case elfcpp::AFL_EXT_4010:
8930 return mach_mips4010;
8932 case elfcpp::AFL_EXT_4100:
8933 return mach_mips4100;
8935 case elfcpp::AFL_EXT_4111:
8936 return mach_mips4111;
8938 case elfcpp::AFL_EXT_4120:
8939 return mach_mips4120;
8941 case elfcpp::AFL_EXT_4650:
8942 return mach_mips4650;
8944 case elfcpp::AFL_EXT_5400:
8945 return mach_mips5400;
8947 case elfcpp::AFL_EXT_5500:
8948 return mach_mips5500;
8950 case elfcpp::AFL_EXT_5900:
8951 return mach_mips5900;
8953 case elfcpp::AFL_EXT_10000:
8954 return mach_mips10000;
8956 case elfcpp::AFL_EXT_LOONGSON_2E:
8957 return mach_mips_loongson_2e;
8959 case elfcpp::AFL_EXT_LOONGSON_2F:
8960 return mach_mips_loongson_2f;
8962 case elfcpp::AFL_EXT_SB1:
8963 return mach_mips_sb1;
8965 case elfcpp::AFL_EXT_OCTEON:
8966 return mach_mips_octeon;
8968 case elfcpp::AFL_EXT_OCTEONP:
8969 return mach_mips_octeonp;
8971 case elfcpp::AFL_EXT_OCTEON2:
8972 return mach_mips_octeon2;
8974 case elfcpp::AFL_EXT_XLR:
8975 return mach_mips_xlr;
8978 return mach_mips3000;
8982 // Return the .MIPS.abiflags value representing each ISA Extension.
8984 template<int size, bool big_endian>
8986 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
8991 return elfcpp::AFL_EXT_3900;
8994 return elfcpp::AFL_EXT_4010;
8997 return elfcpp::AFL_EXT_4100;
9000 return elfcpp::AFL_EXT_4111;
9003 return elfcpp::AFL_EXT_4120;
9006 return elfcpp::AFL_EXT_4650;
9009 return elfcpp::AFL_EXT_5400;
9012 return elfcpp::AFL_EXT_5500;
9015 return elfcpp::AFL_EXT_5900;
9017 case mach_mips10000:
9018 return elfcpp::AFL_EXT_10000;
9020 case mach_mips_loongson_2e:
9021 return elfcpp::AFL_EXT_LOONGSON_2E;
9023 case mach_mips_loongson_2f:
9024 return elfcpp::AFL_EXT_LOONGSON_2F;
9027 return elfcpp::AFL_EXT_SB1;
9029 case mach_mips_octeon:
9030 return elfcpp::AFL_EXT_OCTEON;
9032 case mach_mips_octeonp:
9033 return elfcpp::AFL_EXT_OCTEONP;
9035 case mach_mips_octeon3:
9036 return elfcpp::AFL_EXT_OCTEON3;
9038 case mach_mips_octeon2:
9039 return elfcpp::AFL_EXT_OCTEON2;
9042 return elfcpp::AFL_EXT_XLR;
9049 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
9051 template<int size, bool big_endian>
9053 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9054 elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9057 switch (e_flags & elfcpp::EF_MIPS_ARCH)
9059 case elfcpp::E_MIPS_ARCH_1:
9060 new_isa = this->level_rev(1, 0);
9062 case elfcpp::E_MIPS_ARCH_2:
9063 new_isa = this->level_rev(2, 0);
9065 case elfcpp::E_MIPS_ARCH_3:
9066 new_isa = this->level_rev(3, 0);
9068 case elfcpp::E_MIPS_ARCH_4:
9069 new_isa = this->level_rev(4, 0);
9071 case elfcpp::E_MIPS_ARCH_5:
9072 new_isa = this->level_rev(5, 0);
9074 case elfcpp::E_MIPS_ARCH_32:
9075 new_isa = this->level_rev(32, 1);
9077 case elfcpp::E_MIPS_ARCH_32R2:
9078 new_isa = this->level_rev(32, 2);
9080 case elfcpp::E_MIPS_ARCH_32R6:
9081 new_isa = this->level_rev(32, 6);
9083 case elfcpp::E_MIPS_ARCH_64:
9084 new_isa = this->level_rev(64, 1);
9086 case elfcpp::E_MIPS_ARCH_64R2:
9087 new_isa = this->level_rev(64, 2);
9089 case elfcpp::E_MIPS_ARCH_64R6:
9090 new_isa = this->level_rev(64, 6);
9093 gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9094 this->elf_mips_mach_name(e_flags));
9097 if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9099 // Decode a single value into level and revision.
9100 abiflags->isa_level = new_isa >> 3;
9101 abiflags->isa_rev = new_isa & 0x7;
9104 // Update the isa_ext if needed.
9105 if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9106 this->elf_mips_mach(e_flags)))
9107 abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9110 // Infer the content of the ABI flags based on the elf header.
9112 template<int size, bool big_endian>
9114 Target_mips<size, big_endian>::infer_abiflags(
9115 Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9117 const Attributes_section_data* pasd = relobj->attributes_section_data();
9118 int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9119 elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9121 this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9124 // Read fp_abi from the .gnu.attribute section.
9125 const Object_attribute* attr =
9126 pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9127 attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9130 abiflags->fp_abi = attr_fp_abi;
9131 abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9132 abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9133 abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9134 : elfcpp::AFL_REG_64;
9136 if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9137 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9138 || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9139 && abiflags->gpr_size == elfcpp::AFL_REG_32))
9140 abiflags->cpr1_size = elfcpp::AFL_REG_32;
9141 else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9142 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9143 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9144 abiflags->cpr1_size = elfcpp::AFL_REG_64;
9146 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9147 abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9148 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9149 abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9150 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9151 abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9153 if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9154 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9155 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9156 && abiflags->isa_level >= 32
9157 && abiflags->ases != elfcpp::AFL_ASE_LOONGSON_EXT)
9158 abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9161 // Create abiflags from elf header or from .MIPS.abiflags section.
9163 template<int size, bool big_endian>
9165 Target_mips<size, big_endian>::create_abiflags(
9166 Mips_relobj<size, big_endian>* relobj,
9167 Mips_abiflags<big_endian>* abiflags)
9169 Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9170 Mips_abiflags<big_endian> header_abiflags;
9172 this->infer_abiflags(relobj, &header_abiflags);
9174 if (sec_abiflags == NULL)
9176 // If there is no input .MIPS.abiflags section, use abiflags created
9178 *abiflags = header_abiflags;
9182 this->has_abiflags_section_ = true;
9184 // It is not possible to infer the correct ISA revision for R3 or R5
9185 // so drop down to R2 for the checks.
9186 unsigned char isa_rev = sec_abiflags->isa_rev;
9187 if (isa_rev == 3 || isa_rev == 5)
9190 // Check compatibility between abiflags created from elf header
9191 // and abiflags from .MIPS.abiflags section in this object file.
9192 if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9193 < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9194 gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9195 relobj->name().c_str());
9196 if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9197 && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9198 gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9199 ".MIPS.abiflags"), relobj->name().c_str());
9200 if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9201 gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9202 relobj->name().c_str());
9203 // The isa_ext is allowed to be an extension of what can be inferred
9205 if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9206 this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9207 gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9208 ".MIPS.abiflags"), relobj->name().c_str());
9209 if (sec_abiflags->flags2 != 0)
9210 gold_warning(_("%s: Unexpected flag in the flags2 field of "
9211 ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9212 sec_abiflags->flags2);
9213 // Use abiflags from .MIPS.abiflags section.
9214 *abiflags = *sec_abiflags;
9217 // Return the meaning of fp_abi, or "unknown" if not known.
9219 template<int size, bool big_endian>
9221 Target_mips<size, big_endian>::fp_abi_string(int fp)
9225 case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9226 return "-mdouble-float";
9227 case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9228 return "-msingle-float";
9229 case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9230 return "-msoft-float";
9231 case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9232 return _("-mips32r2 -mfp64 (12 callee-saved)");
9233 case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9235 case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9236 return "-mgp32 -mfp64";
9237 case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9238 return "-mgp32 -mfp64 -mno-odd-spreg";
9246 template<int size, bool big_endian>
9248 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9251 if (in_fp == out_fp)
9254 if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9256 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9257 && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9258 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9259 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9261 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9262 && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9263 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9264 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9265 return out_fp; // Keep the current setting.
9266 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9267 && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9269 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9270 && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9271 return out_fp; // Keep the current setting.
9272 else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9273 gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9274 fp_abi_string(in_fp), fp_abi_string(out_fp));
9278 // Merge attributes from input object.
9280 template<int size, bool big_endian>
9282 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9283 const Attributes_section_data* pasd)
9285 // Return if there is no attributes section data.
9289 // If output has no object attributes, just copy.
9290 if (this->attributes_section_data_ == NULL)
9292 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9296 Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9297 Object_attribute::OBJ_ATTR_GNU);
9299 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9300 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9302 // Merge Tag_compatibility attributes and any common GNU ones.
9303 this->attributes_section_data_->merge(name.c_str(), pasd);
9306 // Merge abiflags from input object.
9308 template<int size, bool big_endian>
9310 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9311 Mips_abiflags<big_endian>* in_abiflags)
9313 // If output has no abiflags, just copy.
9314 if (this->abiflags_ == NULL)
9316 this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9320 this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9321 this->abiflags_->fp_abi);
9324 this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9325 in_abiflags->isa_level);
9326 this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9327 in_abiflags->isa_rev);
9328 this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9329 in_abiflags->gpr_size);
9330 this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9331 in_abiflags->cpr1_size);
9332 this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9333 in_abiflags->cpr2_size);
9334 this->abiflags_->ases |= in_abiflags->ases;
9335 this->abiflags_->flags1 |= in_abiflags->flags1;
9338 // Check whether machine EXTENSION is an extension of machine BASE.
9339 template<int size, bool big_endian>
9341 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9342 unsigned int extension)
9344 if (extension == base)
9347 if ((base == mach_mipsisa32)
9348 && this->mips_mach_extends(mach_mipsisa64, extension))
9351 if ((base == mach_mipsisa32r2)
9352 && this->mips_mach_extends(mach_mipsisa64r2, extension))
9355 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9356 if (extension == this->mips_mach_extensions_[i].first)
9358 extension = this->mips_mach_extensions_[i].second;
9359 if (extension == base)
9366 // Merge file header flags from input object.
9368 template<int size, bool big_endian>
9370 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9371 elfcpp::Elf_Word in_flags)
9373 // If flags are not set yet, just copy them.
9374 if (!this->are_processor_specific_flags_set())
9376 this->set_processor_specific_flags(in_flags);
9377 this->mach_ = this->elf_mips_mach(in_flags);
9381 elfcpp::Elf_Word new_flags = in_flags;
9382 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9383 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9384 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9386 // Check flag compatibility.
9387 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9388 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9390 // Some IRIX 6 BSD-compatibility objects have this bit set. It
9391 // doesn't seem to matter.
9392 new_flags &= ~elfcpp::EF_MIPS_XGOT;
9393 old_flags &= ~elfcpp::EF_MIPS_XGOT;
9395 // MIPSpro generates ucode info in n64 objects. Again, we should
9396 // just be able to ignore this.
9397 new_flags &= ~elfcpp::EF_MIPS_UCODE;
9398 old_flags &= ~elfcpp::EF_MIPS_UCODE;
9400 if (new_flags == old_flags)
9402 this->set_processor_specific_flags(merged_flags);
9406 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9407 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9408 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9411 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9412 merged_flags |= elfcpp::EF_MIPS_CPIC;
9413 if (!(new_flags & elfcpp::EF_MIPS_PIC))
9414 merged_flags &= ~elfcpp::EF_MIPS_PIC;
9416 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9417 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9419 // Compare the ISAs.
9420 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9421 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9422 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9424 // Output ISA isn't the same as, or an extension of, input ISA.
9425 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9427 // Copy the architecture info from input object to output. Also copy
9428 // the 32-bit flag (if set) so that we continue to recognise
9429 // output as a 32-bit binary.
9430 this->mach_ = this->elf_mips_mach(in_flags);
9431 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9432 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9433 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9435 // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9436 this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9438 // Copy across the ABI flags if output doesn't use them
9439 // and if that was what caused us to treat input object as 32-bit.
9440 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9441 && this->mips_32bit_flags(new_flags)
9442 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9443 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9446 // The ISAs aren't compatible.
9447 gold_error(_("%s: linking %s module with previous %s modules"),
9448 name.c_str(), this->elf_mips_mach_name(in_flags),
9449 this->elf_mips_mach_name(merged_flags));
9452 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9453 | elfcpp::EF_MIPS_32BITMODE));
9454 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9455 | elfcpp::EF_MIPS_32BITMODE));
9458 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9460 // Only error if both are set (to different values).
9461 if ((new_flags & elfcpp::EF_MIPS_ABI)
9462 && (old_flags & elfcpp::EF_MIPS_ABI))
9463 gold_error(_("%s: ABI mismatch: linking %s module with "
9464 "previous %s modules"), name.c_str(),
9465 this->elf_mips_abi_name(in_flags),
9466 this->elf_mips_abi_name(merged_flags));
9468 new_flags &= ~elfcpp::EF_MIPS_ABI;
9469 old_flags &= ~elfcpp::EF_MIPS_ABI;
9472 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
9473 // and allow arbitrary mixing of the remaining ASEs (retain the union).
9474 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9475 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9477 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9478 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9479 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9480 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9481 int micro_mis = old_m16 && new_micro;
9482 int m16_mis = old_micro && new_m16;
9484 if (m16_mis || micro_mis)
9485 gold_error(_("%s: ASE mismatch: linking %s module with "
9486 "previous %s modules"), name.c_str(),
9487 m16_mis ? "MIPS16" : "microMIPS",
9488 m16_mis ? "microMIPS" : "MIPS16");
9490 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9492 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9493 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9496 // Compare NaN encodings.
9497 if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9499 gold_error(_("%s: linking %s module with previous %s modules"),
9501 (new_flags & elfcpp::EF_MIPS_NAN2008
9502 ? "-mnan=2008" : "-mnan=legacy"),
9503 (old_flags & elfcpp::EF_MIPS_NAN2008
9504 ? "-mnan=2008" : "-mnan=legacy"));
9506 new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9507 old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9510 // Compare FP64 state.
9511 if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9513 gold_error(_("%s: linking %s module with previous %s modules"),
9515 (new_flags & elfcpp::EF_MIPS_FP64
9516 ? "-mfp64" : "-mfp32"),
9517 (old_flags & elfcpp::EF_MIPS_FP64
9518 ? "-mfp64" : "-mfp32"));
9520 new_flags &= ~elfcpp::EF_MIPS_FP64;
9521 old_flags &= ~elfcpp::EF_MIPS_FP64;
9524 // Warn about any other mismatches.
9525 if (new_flags != old_flags)
9526 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9527 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9529 this->set_processor_specific_flags(merged_flags);
9532 // Adjust ELF file header.
9534 template<int size, bool big_endian>
9536 Target_mips<size, big_endian>::do_adjust_elf_header(
9537 unsigned char* view,
9540 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9542 elfcpp::Ehdr<size, big_endian> ehdr(view);
9543 unsigned char e_ident[elfcpp::EI_NIDENT];
9544 elfcpp::Elf_Word flags = this->processor_specific_flags();
9545 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9547 unsigned char ei_abiversion = 0;
9548 elfcpp::Elf_Half type = ehdr.get_e_type();
9549 if (type == elfcpp::ET_EXEC
9550 && parameters->options().copyreloc()
9551 && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9552 == elfcpp::EF_MIPS_CPIC)
9555 if (this->abiflags_ != NULL
9556 && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9557 || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9560 e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9561 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9562 oehdr.put_e_ident(e_ident);
9564 if (this->entry_symbol_is_compressed_)
9565 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9568 // do_make_elf_object to override the same function in the base class.
9569 // We need to use a target-specific sub-class of
9570 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9571 // Hence we need to have our own ELF object creation.
9573 template<int size, bool big_endian>
9575 Target_mips<size, big_endian>::do_make_elf_object(
9576 const std::string& name,
9577 Input_file* input_file,
9578 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9580 int et = ehdr.get_e_type();
9581 // ET_EXEC files are valid input for --just-symbols/-R,
9582 // and we treat them as relocatable objects.
9583 if (et == elfcpp::ET_REL
9584 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9586 Mips_relobj<size, big_endian>* obj =
9587 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9591 else if (et == elfcpp::ET_DYN)
9593 // TODO(sasa): Should we create Mips_dynobj?
9594 return Target::do_make_elf_object(name, input_file, offset, ehdr);
9598 gold_error(_("%s: unsupported ELF file type %d"),
9604 // Finalize the sections.
9606 template <int size, bool big_endian>
9608 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9609 const Input_objects* input_objects,
9610 Symbol_table* symtab)
9612 const bool relocatable = parameters->options().relocatable();
9614 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9615 // DT_FINI have correct values.
9616 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9617 symtab->lookup(parameters->options().init()));
9618 if (init != NULL && (init->is_mips16() || init->is_micromips()))
9619 init->set_value(init->value() | 1);
9620 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9621 symtab->lookup(parameters->options().fini()));
9622 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9623 fini->set_value(fini->value() | 1);
9625 // Check whether the entry symbol is mips16 or micromips. This is needed to
9626 // adjust entry address in ELF header.
9627 Mips_symbol<size>* entry =
9628 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9629 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9630 || entry->is_micromips()));
9632 if (!parameters->doing_static_link()
9633 && (strcmp(parameters->options().hash_style(), "gnu") == 0
9634 || strcmp(parameters->options().hash_style(), "both") == 0))
9636 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9637 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
9638 // MIPS ABI requires a mapping between the GOT and the symbol table.
9639 gold_error(".gnu.hash is incompatible with the MIPS ABI");
9642 // Check whether the final section that was scanned has HI16 or GOT16
9643 // relocations without the corresponding LO16 part.
9644 if (this->got16_addends_.size() > 0)
9645 gold_error("Can't find matching LO16 reloc");
9647 Valtype gprmask = 0;
9648 Valtype cprmask1 = 0;
9649 Valtype cprmask2 = 0;
9650 Valtype cprmask3 = 0;
9651 Valtype cprmask4 = 0;
9652 bool has_reginfo_section = false;
9654 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9655 p != input_objects->relobj_end();
9658 Mips_relobj<size, big_endian>* relobj =
9659 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9661 // Check for any mips16 stub sections that we can discard.
9663 relobj->discard_mips16_stub_sections(symtab);
9665 if (!relobj->merge_processor_specific_data())
9668 // Merge .reginfo contents of input objects.
9669 if (relobj->has_reginfo_section())
9671 has_reginfo_section = true;
9672 gprmask |= relobj->gprmask();
9673 cprmask1 |= relobj->cprmask1();
9674 cprmask2 |= relobj->cprmask2();
9675 cprmask3 |= relobj->cprmask3();
9676 cprmask4 |= relobj->cprmask4();
9679 // Merge processor specific flags.
9680 Mips_abiflags<big_endian> in_abiflags;
9682 this->create_abiflags(relobj, &in_abiflags);
9683 this->merge_obj_e_flags(relobj->name(),
9684 relobj->processor_specific_flags());
9685 this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9686 this->merge_obj_attributes(relobj->name(),
9687 relobj->attributes_section_data());
9690 // Create a .gnu.attributes section if we have merged any attributes
9692 if (this->attributes_section_data_ != NULL)
9694 Output_attributes_section_data* attributes_section =
9695 new Output_attributes_section_data(*this->attributes_section_data_);
9696 layout->add_output_section_data(".gnu.attributes",
9697 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9698 attributes_section, ORDER_INVALID, false);
9701 // Create .MIPS.abiflags output section if there is an input section.
9702 if (this->has_abiflags_section_)
9704 Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9705 new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9707 Output_section* os =
9708 layout->add_output_section_data(".MIPS.abiflags",
9709 elfcpp::SHT_MIPS_ABIFLAGS,
9711 abiflags_section, ORDER_INVALID, false);
9713 if (!relocatable && os != NULL)
9715 Output_segment* abiflags_segment =
9716 layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9717 abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9721 if (has_reginfo_section && !parameters->options().gc_sections())
9723 // Create .reginfo output section.
9724 Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9725 new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9727 cprmask3, cprmask4);
9729 Output_section* os =
9730 layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9731 elfcpp::SHF_ALLOC, reginfo_section,
9732 ORDER_INVALID, false);
9734 if (!relocatable && os != NULL)
9736 Output_segment* reginfo_segment =
9737 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9739 reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9743 if (this->plt_ != NULL)
9745 // Set final PLT offsets for symbols.
9746 this->plt_section()->set_plt_offsets();
9748 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9749 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9750 // there are no standard PLT entries present.
9751 unsigned char nonvis = 0;
9752 if (this->is_output_micromips()
9753 && !this->plt_section()->has_standard_entries())
9754 nonvis = elfcpp::STO_MICROMIPS >> 2;
9755 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9756 Symbol_table::PREDEFINED,
9758 0, 0, elfcpp::STT_FUNC,
9760 elfcpp::STV_DEFAULT, nonvis,
9764 if (this->mips_stubs_ != NULL)
9766 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9767 unsigned char nonvis = 0;
9768 if (this->is_output_micromips())
9769 nonvis = elfcpp::STO_MICROMIPS >> 2;
9770 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9771 Symbol_table::PREDEFINED,
9773 0, 0, elfcpp::STT_FUNC,
9775 elfcpp::STV_DEFAULT, nonvis,
9779 if (!relocatable && !parameters->doing_static_link())
9780 // In case there is no .got section, create one.
9781 this->got_section(symtab, layout);
9783 // Emit any relocs we saved in an attempt to avoid generating COPY
9785 if (this->copy_relocs_.any_saved_relocs())
9786 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9790 this->set_gp(layout, symtab);
9792 // Emit dynamic relocs.
9793 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9794 p != this->dyn_relocs_.end();
9796 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9798 if (this->has_got_section())
9799 this->got_section()->lay_out_got(layout, symtab, input_objects);
9801 if (this->mips_stubs_ != NULL)
9802 this->mips_stubs_->set_needs_dynsym_value();
9804 // Check for functions that might need $25 to be valid on entry.
9805 // TODO(sasa): Can we do this without iterating over all symbols?
9806 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9807 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9810 // Add NULL segment.
9812 layout->make_output_segment(elfcpp::PT_NULL, 0);
9814 // Fill in some more dynamic tags.
9815 // TODO(sasa): Add more dynamic tags.
9816 const Reloc_section* rel_plt = (this->plt_ == NULL
9817 ? NULL : this->plt_->rel_plt());
9818 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9819 this->rel_dyn_, true, false);
9821 Output_data_dynamic* const odyn = layout->dynamic_data();
9824 && !parameters->doing_static_link())
9827 // This element holds a 32-bit version id for the Runtime
9828 // Linker Interface. This will start at integer value 1.
9830 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9833 d_val = elfcpp::RHF_NOTPOT;
9834 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9836 // Save layout for using when emitting custom dynamic tags.
9837 this->layout_ = layout;
9839 // This member holds the base address of the segment.
9840 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9842 // This member holds the number of entries in the .dynsym section.
9843 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9845 // This member holds the index of the first dynamic symbol
9846 // table entry that corresponds to an entry in the global offset table.
9847 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9849 // This member holds the number of local GOT entries.
9850 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9851 this->got_->get_local_gotno());
9853 if (this->plt_ != NULL)
9854 // DT_MIPS_PLTGOT dynamic tag
9855 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9857 if (!parameters->options().shared())
9859 this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9861 layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9862 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9863 this->rld_map_, ORDER_INVALID, false);
9865 // __RLD_MAP will be filled in by the runtime loader to contain
9866 // a pointer to the _r_debug structure.
9867 Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9868 Symbol_table::PREDEFINED,
9870 0, 0, elfcpp::STT_OBJECT,
9872 elfcpp::STV_DEFAULT, 0,
9875 if (!rld_map->is_forced_local())
9876 rld_map->set_needs_dynsym_entry();
9878 if (!parameters->options().pie())
9879 // This member holds the absolute address of the debug pointer.
9880 odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9882 // This member holds the offset to the debug pointer,
9883 // relative to the address of the tag.
9884 odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9889 // Get the custom dynamic tag value.
9890 template<int size, bool big_endian>
9892 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9896 case elfcpp::DT_MIPS_BASE_ADDRESS:
9898 // The base address of the segment.
9899 // At this point, the segment list has been sorted into final order,
9900 // so just return vaddr of the first readable PT_LOAD segment.
9901 Output_segment* seg =
9902 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9903 gold_assert(seg != NULL);
9904 return seg->vaddr();
9907 case elfcpp::DT_MIPS_SYMTABNO:
9908 // The number of entries in the .dynsym section.
9909 return this->get_dt_mips_symtabno();
9911 case elfcpp::DT_MIPS_GOTSYM:
9913 // The index of the first dynamic symbol table entry that corresponds
9914 // to an entry in the GOT.
9915 if (this->got_->first_global_got_dynsym_index() != -1U)
9916 return this->got_->first_global_got_dynsym_index();
9918 // In case if we don't have global GOT symbols we default to setting
9919 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9920 return this->get_dt_mips_symtabno();
9923 case elfcpp::DT_MIPS_RLD_MAP_REL:
9925 // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9926 // relative to the address of the tag.
9927 Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9928 unsigned int entry_offset =
9929 odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9930 gold_assert(entry_offset != -1U);
9931 return this->rld_map_->address() - (odyn->address() + entry_offset);
9934 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9937 return (unsigned int)-1;
9940 // Relocate section data.
9942 template<int size, bool big_endian>
9944 Target_mips<size, big_endian>::relocate_section(
9945 const Relocate_info<size, big_endian>* relinfo,
9946 unsigned int sh_type,
9947 const unsigned char* prelocs,
9949 Output_section* output_section,
9950 bool needs_special_offset_handling,
9951 unsigned char* view,
9952 Mips_address address,
9953 section_size_type view_size,
9954 const Reloc_symbol_changes* reloc_symbol_changes)
9956 typedef Target_mips<size, big_endian> Mips;
9957 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9959 if (sh_type == elfcpp::SHT_REL)
9961 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9964 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9965 gold::Default_comdat_behavior, Classify_reloc>(
9971 needs_special_offset_handling,
9975 reloc_symbol_changes);
9977 else if (sh_type == elfcpp::SHT_RELA)
9979 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9982 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9983 gold::Default_comdat_behavior, Classify_reloc>(
9989 needs_special_offset_handling,
9993 reloc_symbol_changes);
9997 // Return the size of a relocation while scanning during a relocatable
10001 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
10005 case elfcpp::R_MIPS_NONE:
10006 case elfcpp::R_MIPS_TLS_DTPMOD64:
10007 case elfcpp::R_MIPS_TLS_DTPREL64:
10008 case elfcpp::R_MIPS_TLS_TPREL64:
10011 case elfcpp::R_MIPS_32:
10012 case elfcpp::R_MIPS_TLS_DTPMOD32:
10013 case elfcpp::R_MIPS_TLS_DTPREL32:
10014 case elfcpp::R_MIPS_TLS_TPREL32:
10015 case elfcpp::R_MIPS_REL32:
10016 case elfcpp::R_MIPS_PC32:
10017 case elfcpp::R_MIPS_GPREL32:
10018 case elfcpp::R_MIPS_JALR:
10019 case elfcpp::R_MIPS_EH:
10022 case elfcpp::R_MIPS_16:
10023 case elfcpp::R_MIPS_HI16:
10024 case elfcpp::R_MIPS_LO16:
10025 case elfcpp::R_MIPS_HIGHER:
10026 case elfcpp::R_MIPS_HIGHEST:
10027 case elfcpp::R_MIPS_GPREL16:
10028 case elfcpp::R_MIPS16_HI16:
10029 case elfcpp::R_MIPS16_LO16:
10030 case elfcpp::R_MIPS_PC16:
10031 case elfcpp::R_MIPS_PCHI16:
10032 case elfcpp::R_MIPS_PCLO16:
10033 case elfcpp::R_MIPS_GOT16:
10034 case elfcpp::R_MIPS16_GOT16:
10035 case elfcpp::R_MIPS_CALL16:
10036 case elfcpp::R_MIPS16_CALL16:
10037 case elfcpp::R_MIPS_GOT_HI16:
10038 case elfcpp::R_MIPS_CALL_HI16:
10039 case elfcpp::R_MIPS_GOT_LO16:
10040 case elfcpp::R_MIPS_CALL_LO16:
10041 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10042 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10043 case elfcpp::R_MIPS_TLS_TPREL_HI16:
10044 case elfcpp::R_MIPS_TLS_TPREL_LO16:
10045 case elfcpp::R_MIPS16_GPREL:
10046 case elfcpp::R_MIPS_GOT_DISP:
10047 case elfcpp::R_MIPS_LITERAL:
10048 case elfcpp::R_MIPS_GOT_PAGE:
10049 case elfcpp::R_MIPS_GOT_OFST:
10050 case elfcpp::R_MIPS_TLS_GD:
10051 case elfcpp::R_MIPS_TLS_LDM:
10052 case elfcpp::R_MIPS_TLS_GOTTPREL:
10055 // These relocations are not byte sized
10056 case elfcpp::R_MIPS_26:
10057 case elfcpp::R_MIPS16_26:
10058 case elfcpp::R_MIPS_PC21_S2:
10059 case elfcpp::R_MIPS_PC26_S2:
10060 case elfcpp::R_MIPS_PC18_S3:
10061 case elfcpp::R_MIPS_PC19_S2:
10064 case elfcpp::R_MIPS_COPY:
10065 case elfcpp::R_MIPS_JUMP_SLOT:
10066 object->error(_("unexpected reloc %u in object file"), r_type);
10070 object->error(_("unsupported reloc %u in object file"), r_type);
10075 // Scan the relocs during a relocatable link.
10077 template<int size, bool big_endian>
10079 Target_mips<size, big_endian>::scan_relocatable_relocs(
10080 Symbol_table* symtab,
10082 Sized_relobj_file<size, big_endian>* object,
10083 unsigned int data_shndx,
10084 unsigned int sh_type,
10085 const unsigned char* prelocs,
10086 size_t reloc_count,
10087 Output_section* output_section,
10088 bool needs_special_offset_handling,
10089 size_t local_symbol_count,
10090 const unsigned char* plocal_symbols,
10091 Relocatable_relocs* rr)
10093 if (sh_type == elfcpp::SHT_REL)
10095 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10097 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10098 Scan_relocatable_relocs;
10100 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10108 needs_special_offset_handling,
10109 local_symbol_count,
10113 else if (sh_type == elfcpp::SHT_RELA)
10115 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10117 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10118 Scan_relocatable_relocs;
10120 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10128 needs_special_offset_handling,
10129 local_symbol_count,
10134 gold_unreachable();
10137 // Scan the relocs for --emit-relocs.
10139 template<int size, bool big_endian>
10141 Target_mips<size, big_endian>::emit_relocs_scan(
10142 Symbol_table* symtab,
10144 Sized_relobj_file<size, big_endian>* object,
10145 unsigned int data_shndx,
10146 unsigned int sh_type,
10147 const unsigned char* prelocs,
10148 size_t reloc_count,
10149 Output_section* output_section,
10150 bool needs_special_offset_handling,
10151 size_t local_symbol_count,
10152 const unsigned char* plocal_syms,
10153 Relocatable_relocs* rr)
10155 if (sh_type == elfcpp::SHT_REL)
10157 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10159 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10160 Emit_relocs_strategy;
10162 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10170 needs_special_offset_handling,
10171 local_symbol_count,
10175 else if (sh_type == elfcpp::SHT_RELA)
10177 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10179 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10180 Emit_relocs_strategy;
10182 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10190 needs_special_offset_handling,
10191 local_symbol_count,
10196 gold_unreachable();
10199 // Emit relocations for a section.
10201 template<int size, bool big_endian>
10203 Target_mips<size, big_endian>::relocate_relocs(
10204 const Relocate_info<size, big_endian>* relinfo,
10205 unsigned int sh_type,
10206 const unsigned char* prelocs,
10207 size_t reloc_count,
10208 Output_section* output_section,
10209 typename elfcpp::Elf_types<size>::Elf_Off
10210 offset_in_output_section,
10211 unsigned char* view,
10212 Mips_address view_address,
10213 section_size_type view_size,
10214 unsigned char* reloc_view,
10215 section_size_type reloc_view_size)
10217 if (sh_type == elfcpp::SHT_REL)
10219 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10222 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10227 offset_in_output_section,
10234 else if (sh_type == elfcpp::SHT_RELA)
10236 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10239 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10244 offset_in_output_section,
10252 gold_unreachable();
10255 // Perform target-specific processing in a relocatable link. This is
10256 // only used if we use the relocation strategy RELOC_SPECIAL.
10258 template<int size, bool big_endian>
10260 Target_mips<size, big_endian>::relocate_special_relocatable(
10261 const Relocate_info<size, big_endian>* relinfo,
10262 unsigned int sh_type,
10263 const unsigned char* preloc_in,
10265 Output_section* output_section,
10266 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10267 unsigned char* view,
10268 Mips_address view_address,
10270 unsigned char* preloc_out)
10272 // We can only handle REL type relocation sections.
10273 gold_assert(sh_type == elfcpp::SHT_REL);
10275 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10277 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10280 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10282 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10284 Mips_relobj<size, big_endian>* object =
10285 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10286 const unsigned int local_count = object->local_symbol_count();
10288 Reltype reloc(preloc_in);
10289 Reltype_write reloc_write(preloc_out);
10291 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10292 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10293 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10295 // Get the new symbol index.
10296 // We only use RELOC_SPECIAL strategy in local relocations.
10297 gold_assert(r_sym < local_count);
10299 // We are adjusting a section symbol. We need to find
10300 // the symbol table index of the section symbol for
10301 // the output section corresponding to input section
10302 // in which this symbol is defined.
10304 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10305 gold_assert(is_ordinary);
10306 Output_section* os = object->output_section(shndx);
10307 gold_assert(os != NULL);
10308 gold_assert(os->needs_symtab_index());
10309 unsigned int new_symndx = os->symtab_index();
10311 // Get the new offset--the location in the output section where
10312 // this relocation should be applied.
10314 Mips_address offset = reloc.get_r_offset();
10315 Mips_address new_offset;
10316 if (offset_in_output_section != invalid_address)
10317 new_offset = offset + offset_in_output_section;
10320 section_offset_type sot_offset =
10321 convert_types<section_offset_type, Mips_address>(offset);
10322 section_offset_type new_sot_offset =
10323 output_section->output_offset(object, relinfo->data_shndx,
10325 gold_assert(new_sot_offset != -1);
10326 new_offset = new_sot_offset;
10329 // In an object file, r_offset is an offset within the section.
10330 // In an executable or dynamic object, generated by
10331 // --emit-relocs, r_offset is an absolute address.
10332 if (!parameters->options().relocatable())
10334 new_offset += view_address;
10335 if (offset_in_output_section != invalid_address)
10336 new_offset -= offset_in_output_section;
10339 reloc_write.put_r_offset(new_offset);
10340 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10342 // Handle the reloc addend.
10343 // The relocation uses a section symbol in the input file.
10344 // We are adjusting it to use a section symbol in the output
10345 // file. The input section symbol refers to some address in
10346 // the input section. We need the relocation in the output
10347 // file to refer to that same address. This adjustment to
10348 // the addend is the same calculation we use for a simple
10349 // absolute relocation for the input section symbol.
10350 Valtype calculated_value = 0;
10351 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10353 unsigned char* paddend = view + offset;
10354 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10357 case elfcpp::R_MIPS_26:
10358 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10359 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10360 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10361 false, &calculated_value);
10365 gold_unreachable();
10368 // Report any errors.
10369 switch (reloc_status)
10371 case Reloc_funcs::STATUS_OKAY:
10373 case Reloc_funcs::STATUS_OVERFLOW:
10374 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10375 _("relocation overflow: "
10376 "%u against local symbol %u in %s"),
10377 r_type, r_sym, object->name().c_str());
10379 case Reloc_funcs::STATUS_BAD_RELOC:
10380 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10381 _("unexpected opcode while processing relocation"));
10384 gold_unreachable();
10388 // Optimize the TLS relocation type based on what we know about the
10389 // symbol. IS_FINAL is true if the final address of this symbol is
10390 // known at link time.
10392 template<int size, bool big_endian>
10393 tls::Tls_optimization
10394 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10396 // FIXME: Currently we do not do any TLS optimization.
10397 return tls::TLSOPT_NONE;
10400 // Scan a relocation for a local symbol.
10402 template<int size, bool big_endian>
10404 Target_mips<size, big_endian>::Scan::local(
10405 Symbol_table* symtab,
10407 Target_mips<size, big_endian>* target,
10408 Sized_relobj_file<size, big_endian>* object,
10409 unsigned int data_shndx,
10410 Output_section* output_section,
10411 const Relatype* rela,
10412 const Reltype* rel,
10413 unsigned int rel_type,
10414 unsigned int r_type,
10415 const elfcpp::Sym<size, big_endian>& lsym,
10421 Mips_address r_offset;
10422 unsigned int r_sym;
10423 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10425 if (rel_type == elfcpp::SHT_RELA)
10427 r_offset = rela->get_r_offset();
10428 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10430 r_addend = rela->get_r_addend();
10434 r_offset = rel->get_r_offset();
10435 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10440 Mips_relobj<size, big_endian>* mips_obj =
10441 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10443 if (mips_obj->is_mips16_stub_section(data_shndx))
10445 mips_obj->get_mips16_stub_section(data_shndx)
10446 ->new_local_reloc_found(r_type, r_sym);
10449 if (r_type == elfcpp::R_MIPS_NONE)
10450 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10454 if (!mips16_call_reloc(r_type)
10455 && !mips_obj->section_allows_mips16_refs(data_shndx))
10456 // This reloc would need to refer to a MIPS16 hard-float stub, if
10457 // there is one. We ignore MIPS16 stub sections and .pdr section when
10458 // looking for relocs that would need to refer to MIPS16 stubs.
10459 mips_obj->add_local_non_16bit_call(r_sym);
10461 if (r_type == elfcpp::R_MIPS16_26
10462 && !mips_obj->section_allows_mips16_refs(data_shndx))
10463 mips_obj->add_local_16bit_call(r_sym);
10467 case elfcpp::R_MIPS_GOT16:
10468 case elfcpp::R_MIPS_CALL16:
10469 case elfcpp::R_MIPS_CALL_HI16:
10470 case elfcpp::R_MIPS_CALL_LO16:
10471 case elfcpp::R_MIPS_GOT_HI16:
10472 case elfcpp::R_MIPS_GOT_LO16:
10473 case elfcpp::R_MIPS_GOT_PAGE:
10474 case elfcpp::R_MIPS_GOT_OFST:
10475 case elfcpp::R_MIPS_GOT_DISP:
10476 case elfcpp::R_MIPS_TLS_GOTTPREL:
10477 case elfcpp::R_MIPS_TLS_GD:
10478 case elfcpp::R_MIPS_TLS_LDM:
10479 case elfcpp::R_MIPS16_GOT16:
10480 case elfcpp::R_MIPS16_CALL16:
10481 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10482 case elfcpp::R_MIPS16_TLS_GD:
10483 case elfcpp::R_MIPS16_TLS_LDM:
10484 case elfcpp::R_MICROMIPS_GOT16:
10485 case elfcpp::R_MICROMIPS_CALL16:
10486 case elfcpp::R_MICROMIPS_CALL_HI16:
10487 case elfcpp::R_MICROMIPS_CALL_LO16:
10488 case elfcpp::R_MICROMIPS_GOT_HI16:
10489 case elfcpp::R_MICROMIPS_GOT_LO16:
10490 case elfcpp::R_MICROMIPS_GOT_PAGE:
10491 case elfcpp::R_MICROMIPS_GOT_OFST:
10492 case elfcpp::R_MICROMIPS_GOT_DISP:
10493 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10494 case elfcpp::R_MICROMIPS_TLS_GD:
10495 case elfcpp::R_MICROMIPS_TLS_LDM:
10496 case elfcpp::R_MIPS_EH:
10497 // We need a GOT section.
10498 target->got_section(symtab, layout);
10505 if (call_lo16_reloc(r_type)
10506 || got_lo16_reloc(r_type)
10507 || got_disp_reloc(r_type)
10508 || eh_reloc(r_type))
10510 // We may need a local GOT entry for this relocation. We
10511 // don't count R_MIPS_GOT_PAGE because we can estimate the
10512 // maximum number of pages needed by looking at the size of
10513 // the segment. Similar comments apply to R_MIPS*_GOT16 and
10514 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
10515 // R_MIPS_CALL_HI16 because these are always followed by an
10516 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10517 Mips_output_data_got<size, big_endian>* got =
10518 target->got_section(symtab, layout);
10519 bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10520 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10521 is_section_symbol);
10526 case elfcpp::R_MIPS_CALL16:
10527 case elfcpp::R_MIPS16_CALL16:
10528 case elfcpp::R_MICROMIPS_CALL16:
10529 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10530 (unsigned long)r_offset);
10533 case elfcpp::R_MIPS_GOT_PAGE:
10534 case elfcpp::R_MICROMIPS_GOT_PAGE:
10535 case elfcpp::R_MIPS16_GOT16:
10536 case elfcpp::R_MIPS_GOT16:
10537 case elfcpp::R_MIPS_GOT_HI16:
10538 case elfcpp::R_MIPS_GOT_LO16:
10539 case elfcpp::R_MICROMIPS_GOT16:
10540 case elfcpp::R_MICROMIPS_GOT_HI16:
10541 case elfcpp::R_MICROMIPS_GOT_LO16:
10543 // This relocation needs a page entry in the GOT.
10544 // Get the section contents.
10545 section_size_type view_size = 0;
10546 const unsigned char* view = object->section_contents(data_shndx,
10547 &view_size, false);
10550 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10551 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10554 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10555 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10556 object, data_shndx, r_type, r_sym, addend));
10558 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10562 case elfcpp::R_MIPS_HI16:
10563 case elfcpp::R_MIPS_PCHI16:
10564 case elfcpp::R_MIPS16_HI16:
10565 case elfcpp::R_MICROMIPS_HI16:
10566 // Record the reloc so that we can check whether the corresponding LO16
10568 if (rel_type == elfcpp::SHT_REL)
10569 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10570 object, data_shndx, r_type, r_sym, 0));
10573 case elfcpp::R_MIPS_LO16:
10574 case elfcpp::R_MIPS_PCLO16:
10575 case elfcpp::R_MIPS16_LO16:
10576 case elfcpp::R_MICROMIPS_LO16:
10578 if (rel_type != elfcpp::SHT_REL)
10581 // Find corresponding GOT16/HI16 relocation.
10583 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10584 // be immediately following. However, for the IRIX6 ABI, the next
10585 // relocation may be a composed relocation consisting of several
10586 // relocations for the same address. In that case, the R_MIPS_LO16
10587 // relocation may occur as one of these. We permit a similar
10588 // extension in general, as that is useful for GCC.
10590 // In some cases GCC dead code elimination removes the LO16 but
10591 // keeps the corresponding HI16. This is strictly speaking a
10592 // violation of the ABI but not immediately harmful.
10594 typename std::list<got16_addend<size, big_endian> >::iterator it =
10595 target->got16_addends_.begin();
10596 while (it != target->got16_addends_.end())
10598 got16_addend<size, big_endian> _got16_addend = *it;
10600 // TODO(sasa): Split got16_addends_ list into two lists - one for
10601 // GOT16 relocs and the other for HI16 relocs.
10603 // Report an error if we find HI16 or GOT16 reloc from the
10604 // previous section without the matching LO16 part.
10605 if (_got16_addend.object != object
10606 || _got16_addend.shndx != data_shndx)
10608 gold_error("Can't find matching LO16 reloc");
10612 if (_got16_addend.r_sym != r_sym
10613 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10619 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10620 // For GOT16, we need to calculate combined addend and record GOT page
10622 if (got16_reloc(_got16_addend.r_type))
10625 section_size_type view_size = 0;
10626 const unsigned char* view = object->section_contents(data_shndx,
10631 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10632 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10634 addend = (_got16_addend.addend << 16) + addend;
10635 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10639 it = target->got16_addends_.erase(it);
10647 case elfcpp::R_MIPS_32:
10648 case elfcpp::R_MIPS_REL32:
10649 case elfcpp::R_MIPS_64:
10651 if (parameters->options().output_is_position_independent())
10653 // If building a shared library (or a position-independent
10654 // executable), we need to create a dynamic relocation for
10656 if (is_readonly_section(output_section))
10658 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10659 rel_dyn->add_symbolless_local_addend(object, r_sym,
10660 elfcpp::R_MIPS_REL32,
10661 output_section, data_shndx,
10667 case elfcpp::R_MIPS_TLS_GOTTPREL:
10668 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10669 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10670 case elfcpp::R_MIPS_TLS_LDM:
10671 case elfcpp::R_MIPS16_TLS_LDM:
10672 case elfcpp::R_MICROMIPS_TLS_LDM:
10673 case elfcpp::R_MIPS_TLS_GD:
10674 case elfcpp::R_MIPS16_TLS_GD:
10675 case elfcpp::R_MICROMIPS_TLS_GD:
10677 bool output_is_shared = parameters->options().shared();
10678 const tls::Tls_optimization optimized_type
10679 = Target_mips<size, big_endian>::optimize_tls_reloc(
10680 !output_is_shared, r_type);
10683 case elfcpp::R_MIPS_TLS_GD:
10684 case elfcpp::R_MIPS16_TLS_GD:
10685 case elfcpp::R_MICROMIPS_TLS_GD:
10686 if (optimized_type == tls::TLSOPT_NONE)
10688 // Create a pair of GOT entries for the module index and
10689 // dtv-relative offset.
10690 Mips_output_data_got<size, big_endian>* got =
10691 target->got_section(symtab, layout);
10692 unsigned int shndx = lsym.get_st_shndx();
10694 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10697 object->error(_("local symbol %u has bad shndx %u"),
10701 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10706 // FIXME: TLS optimization not supported yet.
10707 gold_unreachable();
10711 case elfcpp::R_MIPS_TLS_LDM:
10712 case elfcpp::R_MIPS16_TLS_LDM:
10713 case elfcpp::R_MICROMIPS_TLS_LDM:
10714 if (optimized_type == tls::TLSOPT_NONE)
10716 // We always record LDM symbols as local with index 0.
10717 target->got_section()->record_local_got_symbol(mips_obj, 0,
10723 // FIXME: TLS optimization not supported yet.
10724 gold_unreachable();
10727 case elfcpp::R_MIPS_TLS_GOTTPREL:
10728 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10729 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10730 layout->set_has_static_tls();
10731 if (optimized_type == tls::TLSOPT_NONE)
10733 // Create a GOT entry for the tp-relative offset.
10734 Mips_output_data_got<size, big_endian>* got =
10735 target->got_section(symtab, layout);
10736 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10741 // FIXME: TLS optimization not supported yet.
10742 gold_unreachable();
10747 gold_unreachable();
10756 // Refuse some position-dependent relocations when creating a
10757 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10758 // not PIC, but we can create dynamic relocations and the result
10759 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10760 // combined with R_MIPS_GOT16.
10761 if (parameters->options().shared())
10765 case elfcpp::R_MIPS16_HI16:
10766 case elfcpp::R_MIPS_HI16:
10767 case elfcpp::R_MIPS_HIGHER:
10768 case elfcpp::R_MIPS_HIGHEST:
10769 case elfcpp::R_MICROMIPS_HI16:
10770 case elfcpp::R_MICROMIPS_HIGHER:
10771 case elfcpp::R_MICROMIPS_HIGHEST:
10772 // Don't refuse a high part relocation if it's against
10773 // no symbol (e.g. part of a compound relocation).
10778 case elfcpp::R_MIPS16_26:
10779 case elfcpp::R_MIPS_26:
10780 case elfcpp::R_MICROMIPS_26_S1:
10781 gold_error(_("%s: relocation %u against `%s' can not be used when "
10782 "making a shared object; recompile with -fPIC"),
10783 object->name().c_str(), r_type, "a local symbol");
10790 template<int size, bool big_endian>
10792 Target_mips<size, big_endian>::Scan::local(
10793 Symbol_table* symtab,
10795 Target_mips<size, big_endian>* target,
10796 Sized_relobj_file<size, big_endian>* object,
10797 unsigned int data_shndx,
10798 Output_section* output_section,
10799 const Reltype& reloc,
10800 unsigned int r_type,
10801 const elfcpp::Sym<size, big_endian>& lsym,
10814 (const Relatype*) NULL,
10818 lsym, is_discarded);
10822 template<int size, bool big_endian>
10824 Target_mips<size, big_endian>::Scan::local(
10825 Symbol_table* symtab,
10827 Target_mips<size, big_endian>* target,
10828 Sized_relobj_file<size, big_endian>* object,
10829 unsigned int data_shndx,
10830 Output_section* output_section,
10831 const Relatype& reloc,
10832 unsigned int r_type,
10833 const elfcpp::Sym<size, big_endian>& lsym,
10847 (const Reltype*) NULL,
10850 lsym, is_discarded);
10853 // Scan a relocation for a global symbol.
10855 template<int size, bool big_endian>
10857 Target_mips<size, big_endian>::Scan::global(
10858 Symbol_table* symtab,
10860 Target_mips<size, big_endian>* target,
10861 Sized_relobj_file<size, big_endian>* object,
10862 unsigned int data_shndx,
10863 Output_section* output_section,
10864 const Relatype* rela,
10865 const Reltype* rel,
10866 unsigned int rel_type,
10867 unsigned int r_type,
10870 Mips_address r_offset;
10871 unsigned int r_sym;
10872 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10874 if (rel_type == elfcpp::SHT_RELA)
10876 r_offset = rela->get_r_offset();
10877 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10879 r_addend = rela->get_r_addend();
10883 r_offset = rel->get_r_offset();
10884 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10889 Mips_relobj<size, big_endian>* mips_obj =
10890 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10891 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10893 if (mips_obj->is_mips16_stub_section(data_shndx))
10895 mips_obj->get_mips16_stub_section(data_shndx)
10896 ->new_global_reloc_found(r_type, mips_sym);
10899 if (r_type == elfcpp::R_MIPS_NONE)
10900 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10904 if (!mips16_call_reloc(r_type)
10905 && !mips_obj->section_allows_mips16_refs(data_shndx))
10906 // This reloc would need to refer to a MIPS16 hard-float stub, if
10907 // there is one. We ignore MIPS16 stub sections and .pdr section when
10908 // looking for relocs that would need to refer to MIPS16 stubs.
10909 mips_sym->set_need_fn_stub();
10911 // We need PLT entries if there are static-only relocations against
10912 // an externally-defined function. This can technically occur for
10913 // shared libraries if there are branches to the symbol, although it
10914 // is unlikely that this will be used in practice due to the short
10915 // ranges involved. It can occur for any relative or absolute relocation
10916 // in executables; in that case, the PLT entry becomes the function's
10917 // canonical address.
10918 bool static_reloc = false;
10920 // Set CAN_MAKE_DYNAMIC to true if we can convert this
10921 // relocation into a dynamic one.
10922 bool can_make_dynamic = false;
10925 case elfcpp::R_MIPS_GOT16:
10926 case elfcpp::R_MIPS_CALL16:
10927 case elfcpp::R_MIPS_CALL_HI16:
10928 case elfcpp::R_MIPS_CALL_LO16:
10929 case elfcpp::R_MIPS_GOT_HI16:
10930 case elfcpp::R_MIPS_GOT_LO16:
10931 case elfcpp::R_MIPS_GOT_PAGE:
10932 case elfcpp::R_MIPS_GOT_OFST:
10933 case elfcpp::R_MIPS_GOT_DISP:
10934 case elfcpp::R_MIPS_TLS_GOTTPREL:
10935 case elfcpp::R_MIPS_TLS_GD:
10936 case elfcpp::R_MIPS_TLS_LDM:
10937 case elfcpp::R_MIPS16_GOT16:
10938 case elfcpp::R_MIPS16_CALL16:
10939 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10940 case elfcpp::R_MIPS16_TLS_GD:
10941 case elfcpp::R_MIPS16_TLS_LDM:
10942 case elfcpp::R_MICROMIPS_GOT16:
10943 case elfcpp::R_MICROMIPS_CALL16:
10944 case elfcpp::R_MICROMIPS_CALL_HI16:
10945 case elfcpp::R_MICROMIPS_CALL_LO16:
10946 case elfcpp::R_MICROMIPS_GOT_HI16:
10947 case elfcpp::R_MICROMIPS_GOT_LO16:
10948 case elfcpp::R_MICROMIPS_GOT_PAGE:
10949 case elfcpp::R_MICROMIPS_GOT_OFST:
10950 case elfcpp::R_MICROMIPS_GOT_DISP:
10951 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10952 case elfcpp::R_MICROMIPS_TLS_GD:
10953 case elfcpp::R_MICROMIPS_TLS_LDM:
10954 case elfcpp::R_MIPS_EH:
10955 // We need a GOT section.
10956 target->got_section(symtab, layout);
10959 // This is just a hint; it can safely be ignored. Don't set
10960 // has_static_relocs for the corresponding symbol.
10961 case elfcpp::R_MIPS_JALR:
10962 case elfcpp::R_MICROMIPS_JALR:
10965 case elfcpp::R_MIPS_GPREL16:
10966 case elfcpp::R_MIPS_GPREL32:
10967 case elfcpp::R_MIPS16_GPREL:
10968 case elfcpp::R_MICROMIPS_GPREL16:
10970 // GP-relative relocations always resolve to a definition in a
10971 // regular input file, ignoring the one-definition rule. This is
10972 // important for the GP setup sequence in NewABI code, which
10973 // always resolves to a local function even if other relocations
10974 // against the symbol wouldn't.
10975 //constrain_symbol_p = FALSE;
10978 case elfcpp::R_MIPS_32:
10979 case elfcpp::R_MIPS_REL32:
10980 case elfcpp::R_MIPS_64:
10981 if ((parameters->options().shared()
10982 || (strcmp(gsym->name(), "__gnu_local_gp") != 0
10983 && (!is_readonly_section(output_section)
10984 || mips_obj->is_pic())))
10985 && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
10987 if (r_type != elfcpp::R_MIPS_REL32)
10988 mips_sym->set_pointer_equality_needed();
10989 can_make_dynamic = true;
10995 // Most static relocations require pointer equality, except
10997 mips_sym->set_pointer_equality_needed();
11000 case elfcpp::R_MIPS_26:
11001 case elfcpp::R_MIPS_PC16:
11002 case elfcpp::R_MIPS_PC21_S2:
11003 case elfcpp::R_MIPS_PC26_S2:
11004 case elfcpp::R_MIPS16_26:
11005 case elfcpp::R_MICROMIPS_26_S1:
11006 case elfcpp::R_MICROMIPS_PC7_S1:
11007 case elfcpp::R_MICROMIPS_PC10_S1:
11008 case elfcpp::R_MICROMIPS_PC16_S1:
11009 case elfcpp::R_MICROMIPS_PC23_S2:
11010 static_reloc = true;
11011 mips_sym->set_has_static_relocs();
11015 // If there are call relocations against an externally-defined symbol,
11016 // see whether we can create a MIPS lazy-binding stub for it. We can
11017 // only do this if all references to the function are through call
11018 // relocations, and in that case, the traditional lazy-binding stubs
11019 // are much more efficient than PLT entries.
11022 case elfcpp::R_MIPS16_CALL16:
11023 case elfcpp::R_MIPS_CALL16:
11024 case elfcpp::R_MIPS_CALL_HI16:
11025 case elfcpp::R_MIPS_CALL_LO16:
11026 case elfcpp::R_MIPS_JALR:
11027 case elfcpp::R_MICROMIPS_CALL16:
11028 case elfcpp::R_MICROMIPS_CALL_HI16:
11029 case elfcpp::R_MICROMIPS_CALL_LO16:
11030 case elfcpp::R_MICROMIPS_JALR:
11031 if (!mips_sym->no_lazy_stub())
11033 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11034 // Calls from shared objects to undefined symbols of type
11035 // STT_NOTYPE need lazy-binding stub.
11036 || (mips_sym->is_undefined() && parameters->options().shared()))
11037 target->mips_stubs_section(layout)->make_entry(mips_sym);
11042 // We must not create a stub for a symbol that has relocations
11043 // related to taking the function's address.
11044 mips_sym->set_no_lazy_stub();
11045 target->remove_lazy_stub_entry(mips_sym);
11050 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11051 mips_sym->is_mips16()))
11052 mips_sym->set_has_nonpic_branches();
11054 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11055 // and has a special meaning.
11056 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11057 && strcmp(gsym->name(), "_gp_disp") == 0
11058 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11059 if (static_reloc && gsym->needs_plt_entry())
11061 target->make_plt_entry(symtab, layout, mips_sym, r_type);
11063 // Since this is not a PC-relative relocation, we may be
11064 // taking the address of a function. In that case we need to
11065 // set the entry in the dynamic symbol table to the address of
11067 if (gsym->is_from_dynobj() && !parameters->options().shared())
11069 gsym->set_needs_dynsym_value();
11070 // We distinguish between PLT entries and lazy-binding stubs by
11071 // giving the former an st_other value of STO_MIPS_PLT. Set the
11072 // flag if there are any relocations in the binary where pointer
11073 // equality matters.
11074 if (mips_sym->pointer_equality_needed())
11075 mips_sym->set_mips_plt();
11078 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11080 // Absolute addressing relocations.
11081 // Make a dynamic relocation if necessary.
11082 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11084 if (gsym->may_need_copy_reloc())
11086 target->copy_reloc(symtab, layout, object, data_shndx,
11087 output_section, gsym, r_type, r_offset);
11089 else if (can_make_dynamic)
11091 // Create .rel.dyn section.
11092 target->rel_dyn_section(layout);
11093 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11094 data_shndx, output_section, r_offset);
11097 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11102 bool for_call = false;
11105 case elfcpp::R_MIPS_CALL16:
11106 case elfcpp::R_MIPS16_CALL16:
11107 case elfcpp::R_MICROMIPS_CALL16:
11108 case elfcpp::R_MIPS_CALL_HI16:
11109 case elfcpp::R_MIPS_CALL_LO16:
11110 case elfcpp::R_MICROMIPS_CALL_HI16:
11111 case elfcpp::R_MICROMIPS_CALL_LO16:
11115 case elfcpp::R_MIPS16_GOT16:
11116 case elfcpp::R_MIPS_GOT16:
11117 case elfcpp::R_MIPS_GOT_HI16:
11118 case elfcpp::R_MIPS_GOT_LO16:
11119 case elfcpp::R_MICROMIPS_GOT16:
11120 case elfcpp::R_MICROMIPS_GOT_HI16:
11121 case elfcpp::R_MICROMIPS_GOT_LO16:
11122 case elfcpp::R_MIPS_GOT_DISP:
11123 case elfcpp::R_MICROMIPS_GOT_DISP:
11124 case elfcpp::R_MIPS_EH:
11126 // The symbol requires a GOT entry.
11127 Mips_output_data_got<size, big_endian>* got =
11128 target->got_section(symtab, layout);
11129 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11131 mips_sym->set_global_got_area(GGA_NORMAL);
11135 case elfcpp::R_MIPS_GOT_PAGE:
11136 case elfcpp::R_MICROMIPS_GOT_PAGE:
11138 // This relocation needs a page entry in the GOT.
11139 // Get the section contents.
11140 section_size_type view_size = 0;
11141 const unsigned char* view =
11142 object->section_contents(data_shndx, &view_size, false);
11145 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11146 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11148 Mips_output_data_got<size, big_endian>* got =
11149 target->got_section(symtab, layout);
11150 got->record_got_page_entry(mips_obj, r_sym, addend);
11152 // If this is a global, overridable symbol, GOT_PAGE will
11153 // decay to GOT_DISP, so we'll need a GOT entry for it.
11154 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11155 && !mips_sym->object()->is_dynamic()
11156 && !mips_sym->is_undefined());
11158 || (parameters->options().output_is_position_independent()
11159 && !parameters->options().Bsymbolic()
11160 && !mips_sym->is_forced_local()))
11162 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11164 mips_sym->set_global_got_area(GGA_NORMAL);
11169 case elfcpp::R_MIPS_TLS_GOTTPREL:
11170 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11171 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11172 case elfcpp::R_MIPS_TLS_LDM:
11173 case elfcpp::R_MIPS16_TLS_LDM:
11174 case elfcpp::R_MICROMIPS_TLS_LDM:
11175 case elfcpp::R_MIPS_TLS_GD:
11176 case elfcpp::R_MIPS16_TLS_GD:
11177 case elfcpp::R_MICROMIPS_TLS_GD:
11179 const bool is_final = gsym->final_value_is_known();
11180 const tls::Tls_optimization optimized_type =
11181 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11185 case elfcpp::R_MIPS_TLS_GD:
11186 case elfcpp::R_MIPS16_TLS_GD:
11187 case elfcpp::R_MICROMIPS_TLS_GD:
11188 if (optimized_type == tls::TLSOPT_NONE)
11190 // Create a pair of GOT entries for the module index and
11191 // dtv-relative offset.
11192 Mips_output_data_got<size, big_endian>* got =
11193 target->got_section(symtab, layout);
11194 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11199 // FIXME: TLS optimization not supported yet.
11200 gold_unreachable();
11204 case elfcpp::R_MIPS_TLS_LDM:
11205 case elfcpp::R_MIPS16_TLS_LDM:
11206 case elfcpp::R_MICROMIPS_TLS_LDM:
11207 if (optimized_type == tls::TLSOPT_NONE)
11209 // We always record LDM symbols as local with index 0.
11210 target->got_section()->record_local_got_symbol(mips_obj, 0,
11216 // FIXME: TLS optimization not supported yet.
11217 gold_unreachable();
11220 case elfcpp::R_MIPS_TLS_GOTTPREL:
11221 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11222 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11223 layout->set_has_static_tls();
11224 if (optimized_type == tls::TLSOPT_NONE)
11226 // Create a GOT entry for the tp-relative offset.
11227 Mips_output_data_got<size, big_endian>* got =
11228 target->got_section(symtab, layout);
11229 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11234 // FIXME: TLS optimization not supported yet.
11235 gold_unreachable();
11240 gold_unreachable();
11244 case elfcpp::R_MIPS_COPY:
11245 case elfcpp::R_MIPS_JUMP_SLOT:
11246 // These are relocations which should only be seen by the
11247 // dynamic linker, and should never be seen here.
11248 gold_error(_("%s: unexpected reloc %u in object file"),
11249 object->name().c_str(), r_type);
11256 // Refuse some position-dependent relocations when creating a
11257 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
11258 // not PIC, but we can create dynamic relocations and the result
11259 // will be fine. Also do not refuse R_MIPS_LO16, which can be
11260 // combined with R_MIPS_GOT16.
11261 if (parameters->options().shared())
11265 case elfcpp::R_MIPS16_HI16:
11266 case elfcpp::R_MIPS_HI16:
11267 case elfcpp::R_MIPS_HIGHER:
11268 case elfcpp::R_MIPS_HIGHEST:
11269 case elfcpp::R_MICROMIPS_HI16:
11270 case elfcpp::R_MICROMIPS_HIGHER:
11271 case elfcpp::R_MICROMIPS_HIGHEST:
11272 // Don't refuse a high part relocation if it's against
11273 // no symbol (e.g. part of a compound relocation).
11277 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11278 // and has a special meaning.
11279 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11283 case elfcpp::R_MIPS16_26:
11284 case elfcpp::R_MIPS_26:
11285 case elfcpp::R_MICROMIPS_26_S1:
11286 gold_error(_("%s: relocation %u against `%s' can not be used when "
11287 "making a shared object; recompile with -fPIC"),
11288 object->name().c_str(), r_type, gsym->name());
11295 template<int size, bool big_endian>
11297 Target_mips<size, big_endian>::Scan::global(
11298 Symbol_table* symtab,
11300 Target_mips<size, big_endian>* target,
11301 Sized_relobj_file<size, big_endian>* object,
11302 unsigned int data_shndx,
11303 Output_section* output_section,
11304 const Relatype& reloc,
11305 unsigned int r_type,
11316 (const Reltype*) NULL,
11322 template<int size, bool big_endian>
11324 Target_mips<size, big_endian>::Scan::global(
11325 Symbol_table* symtab,
11327 Target_mips<size, big_endian>* target,
11328 Sized_relobj_file<size, big_endian>* object,
11329 unsigned int data_shndx,
11330 Output_section* output_section,
11331 const Reltype& reloc,
11332 unsigned int r_type,
11342 (const Relatype*) NULL,
11349 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11350 // In cases where Scan::local() or Scan::global() has created
11351 // a dynamic relocation, the addend of the relocation is carried
11352 // in the data, and we must not apply the static relocation.
11354 template<int size, bool big_endian>
11356 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11357 const Mips_symbol<size>* gsym,
11358 unsigned int r_type,
11359 Output_section* output_section,
11360 Target_mips* target)
11362 // If the output section is not allocated, then we didn't call
11363 // scan_relocs, we didn't create a dynamic reloc, and we must apply
11365 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11372 // For global symbols, we use the same helper routines used in the
11374 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11375 && !gsym->may_need_copy_reloc())
11377 // We have generated dynamic reloc (R_MIPS_REL32).
11379 bool multi_got = false;
11380 if (target->has_got_section())
11381 multi_got = target->got_section()->multi_got();
11382 bool has_got_offset;
11384 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11386 has_got_offset = gsym->global_gotoffset() != -1U;
11387 if (!has_got_offset)
11390 // Apply the relocation only if the symbol is in the local got.
11391 // Do not apply the relocation if the symbol is in the global
11393 return symbol_references_local(gsym, gsym->has_dynsym_index());
11396 // We have not generated dynamic reloc.
11401 // Perform a relocation.
11403 template<int size, bool big_endian>
11405 Target_mips<size, big_endian>::Relocate::relocate(
11406 const Relocate_info<size, big_endian>* relinfo,
11407 unsigned int rel_type,
11408 Target_mips* target,
11409 Output_section* output_section,
11411 const unsigned char* preloc,
11412 const Sized_symbol<size>* gsym,
11413 const Symbol_value<size>* psymval,
11414 unsigned char* view,
11415 Mips_address address,
11418 Mips_address r_offset;
11419 unsigned int r_sym;
11420 unsigned int r_type;
11421 unsigned int r_type2;
11422 unsigned int r_type3;
11423 unsigned char r_ssym;
11424 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11425 // r_offset and r_type of the next relocation is needed for resolving multiple
11426 // consecutive relocations with the same offset.
11427 Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11428 unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11430 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11431 size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
11433 if (rel_type == elfcpp::SHT_RELA)
11435 const Relatype rela(preloc);
11436 r_offset = rela.get_r_offset();
11437 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11439 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11441 r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11442 get_r_type2(&rela);
11443 r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11444 get_r_type3(&rela);
11445 r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11447 r_addend = rela.get_r_addend();
11448 // If this is not last relocation, get r_offset and r_type of the next
11450 if (relnum + 1 < reloc_count)
11452 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11453 const Relatype next_rela(preloc + reloc_size);
11454 next_r_offset = next_rela.get_r_offset();
11456 Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11457 get_r_type(&next_rela);
11462 const Reltype rel(preloc);
11463 r_offset = rel.get_r_offset();
11464 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11466 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11469 r_type2 = elfcpp::R_MIPS_NONE;
11470 r_type3 = elfcpp::R_MIPS_NONE;
11472 // If this is not last relocation, get r_offset and r_type of the next
11474 if (relnum + 1 < reloc_count)
11476 const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11477 const Reltype next_rel(preloc + reloc_size);
11478 next_r_offset = next_rel.get_r_offset();
11479 next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11480 get_r_type(&next_rel);
11484 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11485 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11487 Mips_relobj<size, big_endian>* object =
11488 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11490 bool target_is_16_bit_code = false;
11491 bool target_is_micromips_code = false;
11492 bool cross_mode_jump;
11494 Symbol_value<size> symval;
11496 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11498 bool changed_symbol_value = false;
11501 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11502 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11503 if (target_is_16_bit_code || target_is_micromips_code)
11505 // MIPS16/microMIPS text labels should be treated as odd.
11506 symval.set_output_value(psymval->value(object, 1));
11508 changed_symbol_value = true;
11513 target_is_16_bit_code = mips_sym->is_mips16();
11514 target_is_micromips_code = mips_sym->is_micromips();
11516 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11517 // it odd. This will cause something like .word SYM to come up with
11518 // the right value when it is loaded into the PC.
11520 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11521 && psymval->value(object, 0) != 0)
11523 symval.set_output_value(psymval->value(object, 0) | 1);
11525 changed_symbol_value = true;
11528 // Pick the value to use for symbols defined in shared objects.
11529 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11530 || mips_sym->has_lazy_stub())
11532 Mips_address value;
11533 if (!mips_sym->has_lazy_stub())
11535 // Prefer a standard MIPS PLT entry.
11536 if (mips_sym->has_mips_plt_offset())
11538 value = target->plt_section()->mips_entry_address(mips_sym);
11539 target_is_micromips_code = false;
11540 target_is_16_bit_code = false;
11544 value = (target->plt_section()->comp_entry_address(mips_sym)
11546 if (target->is_output_micromips())
11547 target_is_micromips_code = true;
11549 target_is_16_bit_code = true;
11553 value = target->mips_stubs_section()->stub_address(mips_sym);
11555 symval.set_output_value(value);
11560 // TRUE if the symbol referred to by this relocation is "_gp_disp".
11561 // Note that such a symbol must always be a global symbol.
11562 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11563 && !object->is_newabi());
11565 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11566 // Note that such a symbol must always be a global symbol.
11567 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11572 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11573 gold_error_at_location(relinfo, relnum, r_offset,
11574 _("relocations against _gp_disp are permitted only"
11575 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11577 else if (gnu_local_gp)
11579 // __gnu_local_gp is _gp symbol.
11580 symval.set_output_value(target->adjusted_gp_value(object));
11584 // If this is a reference to a 16-bit function with a stub, we need
11585 // to redirect the relocation to the stub unless:
11587 // (a) the relocation is for a MIPS16 JAL;
11589 // (b) the relocation is for a MIPS16 PIC call, and there are no
11590 // non-MIPS16 uses of the GOT slot; or
11592 // (c) the section allows direct references to MIPS16 functions.
11593 if (r_type != elfcpp::R_MIPS16_26
11594 && ((mips_sym != NULL
11595 && mips_sym->has_mips16_fn_stub()
11596 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11597 || (mips_sym == NULL
11598 && object->get_local_mips16_fn_stub(r_sym) != NULL))
11599 && !object->section_allows_mips16_refs(relinfo->data_shndx))
11601 // This is a 32- or 64-bit call to a 16-bit function. We should
11602 // have already noticed that we were going to need the
11604 Mips_address value;
11605 if (mips_sym == NULL)
11606 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11609 gold_assert(mips_sym->need_fn_stub());
11610 if (mips_sym->has_la25_stub())
11611 value = target->la25_stub_section()->stub_address(mips_sym);
11614 value = mips_sym->template
11615 get_mips16_fn_stub<big_endian>()->output_address();
11618 symval.set_output_value(value);
11620 changed_symbol_value = true;
11622 // The target is 16-bit, but the stub isn't.
11623 target_is_16_bit_code = false;
11625 // If this is a MIPS16 call with a stub, that is made through the PLT or
11626 // to a standard MIPS function, we need to redirect the call to the stub.
11627 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11628 // indirect calls should use an indirect stub instead.
11629 else if (r_type == elfcpp::R_MIPS16_26
11630 && ((mips_sym != NULL
11631 && (mips_sym->has_mips16_call_stub()
11632 || mips_sym->has_mips16_call_fp_stub()))
11633 || (mips_sym == NULL
11634 && object->get_local_mips16_call_stub(r_sym) != NULL))
11635 && ((mips_sym != NULL && mips_sym->has_plt_offset())
11636 || !target_is_16_bit_code))
11638 Mips16_stub_section<size, big_endian>* call_stub;
11639 if (mips_sym == NULL)
11640 call_stub = object->get_local_mips16_call_stub(r_sym);
11643 // If both call_stub and call_fp_stub are defined, we can figure
11644 // out which one to use by checking which one appears in the input
11646 if (mips_sym->has_mips16_call_stub()
11647 && mips_sym->has_mips16_call_fp_stub())
11650 for (unsigned int i = 1; i < object->shnum(); ++i)
11652 if (object->is_mips16_call_fp_stub_section(i))
11654 call_stub = mips_sym->template
11655 get_mips16_call_fp_stub<big_endian>();
11660 if (call_stub == NULL)
11662 mips_sym->template get_mips16_call_stub<big_endian>();
11664 else if (mips_sym->has_mips16_call_stub())
11665 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11667 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11670 symval.set_output_value(call_stub->output_address());
11672 changed_symbol_value = true;
11674 // If this is a direct call to a PIC function, redirect to the
11676 else if (mips_sym != NULL
11677 && mips_sym->has_la25_stub()
11678 && relocation_needs_la25_stub<size, big_endian>(
11679 object, r_type, target_is_16_bit_code))
11681 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11682 if (mips_sym->is_micromips())
11684 symval.set_output_value(value);
11687 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11688 // entry is used if a standard PLT entry has also been made.
11689 else if ((r_type == elfcpp::R_MIPS16_26
11690 || r_type == elfcpp::R_MICROMIPS_26_S1)
11691 && mips_sym != NULL
11692 && mips_sym->has_plt_offset()
11693 && mips_sym->has_comp_plt_offset()
11694 && mips_sym->has_mips_plt_offset())
11696 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11698 symval.set_output_value(value);
11701 target_is_16_bit_code = !target->is_output_micromips();
11702 target_is_micromips_code = target->is_output_micromips();
11705 // Make sure MIPS16 and microMIPS are not used together.
11706 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11707 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11709 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11712 // Calls from 16-bit code to 32-bit code and vice versa require the
11713 // mode change. However, we can ignore calls to undefined weak symbols,
11714 // which should never be executed at runtime. This exception is important
11715 // because the assembly writer may have "known" that any definition of the
11716 // symbol would be 16-bit code, and that direct jumps were therefore
11719 (!(gsym != NULL && gsym->is_weak_undefined())
11720 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11721 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11722 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11723 && (target_is_16_bit_code || target_is_micromips_code))));
11725 bool local = (mips_sym == NULL
11726 || (mips_sym->got_only_for_calls()
11727 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11728 : symbol_references_local(mips_sym,
11729 mips_sym->has_dynsym_index())));
11731 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11732 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
11733 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11734 if (got_page_reloc(r_type) && !local)
11735 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11736 : elfcpp::R_MIPS_GOT_DISP);
11738 unsigned int got_offset = 0;
11741 // Whether we have to extract addend from instruction.
11742 bool extract_addend = rel_type == elfcpp::SHT_REL;
11743 unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11745 Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11747 // For Mips64 N64 ABI, there may be up to three operations specified per
11748 // record, by the fields r_type, r_type2, and r_type3. The first operation
11749 // takes its addend from the relocation record. Each subsequent operation
11750 // takes as its addend the result of the previous operation.
11751 // The first operation in a record which references a symbol uses the symbol
11752 // implied by r_sym. The next operation in a record which references a symbol
11753 // uses the special symbol value given by the r_ssym field. A third operation
11754 // in a record which references a symbol will assume a NULL symbol,
11755 // i.e. value zero.
11758 // Check if a record references to a symbol.
11759 for (unsigned int i = 0; i < 3; ++i)
11761 if (r_types[i] == elfcpp::R_MIPS_NONE)
11764 // If we didn't apply previous relocation, use its result as addend
11766 if (this->calculate_only_)
11768 r_addend = this->calculated_value_;
11769 extract_addend = false;
11772 // In the N32 and 64-bit ABIs there may be multiple consecutive
11773 // relocations for the same offset. In that case we are
11774 // supposed to treat the output of each relocation as the addend
11775 // for the next. For N64 ABI, we are checking offsets only in a
11776 // third operation in a record (r_type3).
11777 this->calculate_only_ =
11778 (object->is_n64() && i < 2
11779 ? r_types[i+1] != elfcpp::R_MIPS_NONE
11780 : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
11782 if (object->is_n64())
11786 // Handle special symbol for r_type2 relocation type.
11790 symval.set_output_value(0);
11793 symval.set_output_value(target->gp_value());
11796 symval.set_output_value(object->gp_value());
11799 symval.set_output_value(address);
11802 gold_unreachable();
11808 // For r_type3 symbol value is 0.
11809 symval.set_output_value(0);
11813 bool update_got_entry = false;
11814 switch (r_types[i])
11816 case elfcpp::R_MIPS_NONE:
11818 case elfcpp::R_MIPS_16:
11819 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11821 this->calculate_only_,
11822 &this->calculated_value_);
11825 case elfcpp::R_MIPS_32:
11826 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11828 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11830 this->calculate_only_,
11831 &this->calculated_value_);
11832 if (mips_sym != NULL
11833 && (mips_sym->is_mips16() || mips_sym->is_micromips())
11834 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11836 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11837 // already updated by adding +1.
11838 if (mips_sym->has_mips16_fn_stub())
11840 gold_assert(mips_sym->need_fn_stub());
11841 Mips16_stub_section<size, big_endian>* fn_stub =
11842 mips_sym->template get_mips16_fn_stub<big_endian>();
11844 symval.set_output_value(fn_stub->output_address());
11847 got_offset = mips_sym->global_gotoffset();
11848 update_got_entry = true;
11852 case elfcpp::R_MIPS_64:
11853 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11855 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11857 this->calculate_only_,
11858 &this->calculated_value_, false);
11859 else if (target->is_output_n64() && r_addend != 0)
11860 // Only apply the addend. The static relocation was RELA, but the
11861 // dynamic relocation is REL, so we need to apply the addend.
11862 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11864 this->calculate_only_,
11865 &this->calculated_value_, true);
11867 case elfcpp::R_MIPS_REL32:
11868 gold_unreachable();
11870 case elfcpp::R_MIPS_PC32:
11871 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11872 r_addend, extract_addend,
11873 this->calculate_only_,
11874 &this->calculated_value_);
11877 case elfcpp::R_MIPS16_26:
11878 // The calculation for R_MIPS16_26 is just the same as for an
11879 // R_MIPS_26. It's only the storage of the relocated field into
11880 // the output file that's different. So, we just fall through to the
11881 // R_MIPS_26 case here.
11882 case elfcpp::R_MIPS_26:
11883 case elfcpp::R_MICROMIPS_26_S1:
11884 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11885 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11886 r_types[i], target->jal_to_bal(), this->calculate_only_,
11887 &this->calculated_value_);
11890 case elfcpp::R_MIPS_HI16:
11891 case elfcpp::R_MIPS16_HI16:
11892 case elfcpp::R_MICROMIPS_HI16:
11893 if (rel_type == elfcpp::SHT_RELA)
11894 reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11896 gp_disp, r_types[i],
11899 this->calculate_only_,
11900 &this->calculated_value_);
11901 else if (rel_type == elfcpp::SHT_REL)
11902 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11903 address, gp_disp, r_types[i],
11904 r_sym, extract_addend);
11906 gold_unreachable();
11909 case elfcpp::R_MIPS_LO16:
11910 case elfcpp::R_MIPS16_LO16:
11911 case elfcpp::R_MICROMIPS_LO16:
11912 case elfcpp::R_MICROMIPS_HI0_LO16:
11913 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11914 r_addend, extract_addend, address,
11915 gp_disp, r_types[i], r_sym,
11916 rel_type, this->calculate_only_,
11917 &this->calculated_value_);
11920 case elfcpp::R_MIPS_LITERAL:
11921 case elfcpp::R_MICROMIPS_LITERAL:
11922 // Because we don't merge literal sections, we can handle this
11923 // just like R_MIPS_GPREL16. In the long run, we should merge
11924 // shared literals, and then we will need to additional work
11929 case elfcpp::R_MIPS_GPREL16:
11930 case elfcpp::R_MIPS16_GPREL:
11931 case elfcpp::R_MICROMIPS_GPREL7_S2:
11932 case elfcpp::R_MICROMIPS_GPREL16:
11933 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11934 target->adjusted_gp_value(object),
11935 r_addend, extract_addend,
11936 gsym == NULL, r_types[i],
11937 this->calculate_only_,
11938 &this->calculated_value_);
11941 case elfcpp::R_MIPS_PC16:
11942 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11943 r_addend, extract_addend,
11944 this->calculate_only_,
11945 &this->calculated_value_);
11948 case elfcpp::R_MIPS_PC21_S2:
11949 reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11950 r_addend, extract_addend,
11951 this->calculate_only_,
11952 &this->calculated_value_);
11955 case elfcpp::R_MIPS_PC26_S2:
11956 reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11957 r_addend, extract_addend,
11958 this->calculate_only_,
11959 &this->calculated_value_);
11962 case elfcpp::R_MIPS_PC18_S3:
11963 reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11964 r_addend, extract_addend,
11965 this->calculate_only_,
11966 &this->calculated_value_);
11969 case elfcpp::R_MIPS_PC19_S2:
11970 reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
11971 r_addend, extract_addend,
11972 this->calculate_only_,
11973 &this->calculated_value_);
11976 case elfcpp::R_MIPS_PCHI16:
11977 if (rel_type == elfcpp::SHT_RELA)
11978 reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
11981 this->calculate_only_,
11982 &this->calculated_value_);
11983 else if (rel_type == elfcpp::SHT_REL)
11984 reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
11985 r_addend, address, r_sym,
11988 gold_unreachable();
11991 case elfcpp::R_MIPS_PCLO16:
11992 reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
11993 extract_addend, address, r_sym,
11994 rel_type, this->calculate_only_,
11995 &this->calculated_value_);
11997 case elfcpp::R_MICROMIPS_PC7_S1:
11998 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
12001 this->calculate_only_,
12002 &this->calculated_value_);
12004 case elfcpp::R_MICROMIPS_PC10_S1:
12005 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
12007 r_addend, extract_addend,
12008 this->calculate_only_,
12009 &this->calculated_value_);
12011 case elfcpp::R_MICROMIPS_PC16_S1:
12012 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
12014 r_addend, extract_addend,
12015 this->calculate_only_,
12016 &this->calculated_value_);
12018 case elfcpp::R_MIPS_GPREL32:
12019 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12020 target->adjusted_gp_value(object),
12021 r_addend, extract_addend,
12022 this->calculate_only_,
12023 &this->calculated_value_);
12025 case elfcpp::R_MIPS_GOT_HI16:
12026 case elfcpp::R_MIPS_CALL_HI16:
12027 case elfcpp::R_MICROMIPS_GOT_HI16:
12028 case elfcpp::R_MICROMIPS_CALL_HI16:
12030 got_offset = target->got_section()->got_offset(gsym,
12034 got_offset = target->got_section()->got_offset(r_sym,
12037 gp_offset = target->got_section()->gp_offset(got_offset, object);
12038 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
12039 this->calculate_only_,
12040 &this->calculated_value_);
12041 update_got_entry = changed_symbol_value;
12044 case elfcpp::R_MIPS_GOT_LO16:
12045 case elfcpp::R_MIPS_CALL_LO16:
12046 case elfcpp::R_MICROMIPS_GOT_LO16:
12047 case elfcpp::R_MICROMIPS_CALL_LO16:
12049 got_offset = target->got_section()->got_offset(gsym,
12053 got_offset = target->got_section()->got_offset(r_sym,
12056 gp_offset = target->got_section()->gp_offset(got_offset, object);
12057 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
12058 this->calculate_only_,
12059 &this->calculated_value_);
12060 update_got_entry = changed_symbol_value;
12063 case elfcpp::R_MIPS_GOT_DISP:
12064 case elfcpp::R_MICROMIPS_GOT_DISP:
12065 case elfcpp::R_MIPS_EH:
12067 got_offset = target->got_section()->got_offset(gsym,
12071 got_offset = target->got_section()->got_offset(r_sym,
12074 gp_offset = target->got_section()->gp_offset(got_offset, object);
12075 if (eh_reloc(r_types[i]))
12076 reloc_status = Reloc_funcs::releh(view, gp_offset,
12077 this->calculate_only_,
12078 &this->calculated_value_);
12080 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12081 this->calculate_only_,
12082 &this->calculated_value_);
12084 case elfcpp::R_MIPS_CALL16:
12085 case elfcpp::R_MIPS16_CALL16:
12086 case elfcpp::R_MICROMIPS_CALL16:
12087 gold_assert(gsym != NULL);
12088 got_offset = target->got_section()->got_offset(gsym,
12091 gp_offset = target->got_section()->gp_offset(got_offset, object);
12092 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12093 this->calculate_only_,
12094 &this->calculated_value_);
12095 // TODO(sasa): We should also initialize update_got_entry
12096 // in other place swhere relgot is called.
12097 update_got_entry = changed_symbol_value;
12100 case elfcpp::R_MIPS_GOT16:
12101 case elfcpp::R_MIPS16_GOT16:
12102 case elfcpp::R_MICROMIPS_GOT16:
12105 got_offset = target->got_section()->got_offset(gsym,
12108 gp_offset = target->got_section()->gp_offset(got_offset, object);
12109 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12110 this->calculate_only_,
12111 &this->calculated_value_);
12115 if (rel_type == elfcpp::SHT_RELA)
12116 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
12120 this->calculate_only_,
12121 &this->calculated_value_);
12122 else if (rel_type == elfcpp::SHT_REL)
12123 reloc_status = Reloc_funcs::relgot16_local(view, object,
12126 r_types[i], r_sym);
12128 gold_unreachable();
12130 update_got_entry = changed_symbol_value;
12133 case elfcpp::R_MIPS_TLS_GD:
12134 case elfcpp::R_MIPS16_TLS_GD:
12135 case elfcpp::R_MICROMIPS_TLS_GD:
12137 got_offset = target->got_section()->got_offset(gsym,
12141 got_offset = target->got_section()->got_offset(r_sym,
12144 gp_offset = target->got_section()->gp_offset(got_offset, object);
12145 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12146 this->calculate_only_,
12147 &this->calculated_value_);
12150 case elfcpp::R_MIPS_TLS_GOTTPREL:
12151 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12152 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12154 got_offset = target->got_section()->got_offset(gsym,
12155 GOT_TYPE_TLS_OFFSET,
12158 got_offset = target->got_section()->got_offset(r_sym,
12159 GOT_TYPE_TLS_OFFSET,
12161 gp_offset = target->got_section()->gp_offset(got_offset, object);
12162 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12163 this->calculate_only_,
12164 &this->calculated_value_);
12167 case elfcpp::R_MIPS_TLS_LDM:
12168 case elfcpp::R_MIPS16_TLS_LDM:
12169 case elfcpp::R_MICROMIPS_TLS_LDM:
12170 // Relocate the field with the offset of the GOT entry for
12171 // the module index.
12172 got_offset = target->got_section()->tls_ldm_offset(object);
12173 gp_offset = target->got_section()->gp_offset(got_offset, object);
12174 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12175 this->calculate_only_,
12176 &this->calculated_value_);
12179 case elfcpp::R_MIPS_GOT_PAGE:
12180 case elfcpp::R_MICROMIPS_GOT_PAGE:
12181 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12182 r_addend, extract_addend,
12183 this->calculate_only_,
12184 &this->calculated_value_);
12187 case elfcpp::R_MIPS_GOT_OFST:
12188 case elfcpp::R_MICROMIPS_GOT_OFST:
12189 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12190 r_addend, extract_addend,
12191 local, this->calculate_only_,
12192 &this->calculated_value_);
12195 case elfcpp::R_MIPS_JALR:
12196 case elfcpp::R_MICROMIPS_JALR:
12197 // This relocation is only a hint. In some cases, we optimize
12198 // it into a bal instruction. But we don't try to optimize
12199 // when the symbol does not resolve locally.
12201 || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12202 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12203 r_addend, extract_addend,
12204 cross_mode_jump, r_types[i],
12205 target->jalr_to_bal(),
12207 this->calculate_only_,
12208 &this->calculated_value_);
12211 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12212 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12213 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12214 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12215 elfcpp::DTP_OFFSET, r_addend,
12217 this->calculate_only_,
12218 &this->calculated_value_);
12220 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12221 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12222 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12223 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12224 elfcpp::DTP_OFFSET, r_addend,
12226 this->calculate_only_,
12227 &this->calculated_value_);
12229 case elfcpp::R_MIPS_TLS_DTPREL32:
12230 case elfcpp::R_MIPS_TLS_DTPREL64:
12231 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12232 elfcpp::DTP_OFFSET, r_addend,
12234 this->calculate_only_,
12235 &this->calculated_value_);
12237 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12238 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12239 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12240 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12241 elfcpp::TP_OFFSET, r_addend,
12243 this->calculate_only_,
12244 &this->calculated_value_);
12246 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12247 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12248 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12249 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12250 elfcpp::TP_OFFSET, r_addend,
12252 this->calculate_only_,
12253 &this->calculated_value_);
12255 case elfcpp::R_MIPS_TLS_TPREL32:
12256 case elfcpp::R_MIPS_TLS_TPREL64:
12257 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12258 elfcpp::TP_OFFSET, r_addend,
12260 this->calculate_only_,
12261 &this->calculated_value_);
12263 case elfcpp::R_MIPS_SUB:
12264 case elfcpp::R_MICROMIPS_SUB:
12265 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12267 this->calculate_only_,
12268 &this->calculated_value_);
12270 case elfcpp::R_MIPS_HIGHER:
12271 case elfcpp::R_MICROMIPS_HIGHER:
12272 reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
12274 this->calculate_only_,
12275 &this->calculated_value_);
12277 case elfcpp::R_MIPS_HIGHEST:
12278 case elfcpp::R_MICROMIPS_HIGHEST:
12279 reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12280 r_addend, extract_addend,
12281 this->calculate_only_,
12282 &this->calculated_value_);
12285 gold_error_at_location(relinfo, relnum, r_offset,
12286 _("unsupported reloc %u"), r_types[i]);
12290 if (update_got_entry)
12292 Mips_output_data_got<size, big_endian>* got = target->got_section();
12293 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12294 got->update_got_entry(got->get_primary_got_offset(mips_sym),
12295 psymval->value(object, 0));
12297 got->update_got_entry(got_offset, psymval->value(object, 0));
12301 bool jal_shuffle = jal_reloc(r_type);
12302 Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12304 // Report any errors.
12305 switch (reloc_status)
12307 case Reloc_funcs::STATUS_OKAY:
12309 case Reloc_funcs::STATUS_OVERFLOW:
12311 gold_error_at_location(relinfo, relnum, r_offset,
12312 _("relocation overflow: "
12313 "%u against local symbol %u in %s"),
12314 r_type, r_sym, object->name().c_str());
12315 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12316 gold_error_at_location(relinfo, relnum, r_offset,
12317 _("relocation overflow: "
12318 "%u against '%s' defined in %s"),
12319 r_type, gsym->demangled_name().c_str(),
12320 gsym->object()->name().c_str());
12322 gold_error_at_location(relinfo, relnum, r_offset,
12323 _("relocation overflow: %u against '%s'"),
12324 r_type, gsym->demangled_name().c_str());
12326 case Reloc_funcs::STATUS_BAD_RELOC:
12327 gold_error_at_location(relinfo, relnum, r_offset,
12328 _("unexpected opcode while processing relocation"));
12330 case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12331 gold_error_at_location(relinfo, relnum, r_offset,
12332 _("unaligned PC-relative relocation"));
12335 gold_unreachable();
12341 // Get the Reference_flags for a particular relocation.
12343 template<int size, bool big_endian>
12345 Target_mips<size, big_endian>::Scan::get_reference_flags(
12346 unsigned int r_type)
12350 case elfcpp::R_MIPS_NONE:
12351 // No symbol reference.
12354 case elfcpp::R_MIPS_16:
12355 case elfcpp::R_MIPS_32:
12356 case elfcpp::R_MIPS_64:
12357 case elfcpp::R_MIPS_HI16:
12358 case elfcpp::R_MIPS_LO16:
12359 case elfcpp::R_MIPS_HIGHER:
12360 case elfcpp::R_MIPS_HIGHEST:
12361 case elfcpp::R_MIPS16_HI16:
12362 case elfcpp::R_MIPS16_LO16:
12363 case elfcpp::R_MICROMIPS_HI16:
12364 case elfcpp::R_MICROMIPS_LO16:
12365 case elfcpp::R_MICROMIPS_HIGHER:
12366 case elfcpp::R_MICROMIPS_HIGHEST:
12367 return Symbol::ABSOLUTE_REF;
12369 case elfcpp::R_MIPS_26:
12370 case elfcpp::R_MIPS16_26:
12371 case elfcpp::R_MICROMIPS_26_S1:
12372 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12374 case elfcpp::R_MIPS_PC18_S3:
12375 case elfcpp::R_MIPS_PC19_S2:
12376 case elfcpp::R_MIPS_PCHI16:
12377 case elfcpp::R_MIPS_PCLO16:
12378 case elfcpp::R_MIPS_GPREL32:
12379 case elfcpp::R_MIPS_GPREL16:
12380 case elfcpp::R_MIPS_REL32:
12381 case elfcpp::R_MIPS16_GPREL:
12382 return Symbol::RELATIVE_REF;
12384 case elfcpp::R_MIPS_PC16:
12385 case elfcpp::R_MIPS_PC32:
12386 case elfcpp::R_MIPS_PC21_S2:
12387 case elfcpp::R_MIPS_PC26_S2:
12388 case elfcpp::R_MIPS_JALR:
12389 case elfcpp::R_MICROMIPS_JALR:
12390 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12392 case elfcpp::R_MIPS_GOT16:
12393 case elfcpp::R_MIPS_CALL16:
12394 case elfcpp::R_MIPS_GOT_DISP:
12395 case elfcpp::R_MIPS_GOT_HI16:
12396 case elfcpp::R_MIPS_GOT_LO16:
12397 case elfcpp::R_MIPS_CALL_HI16:
12398 case elfcpp::R_MIPS_CALL_LO16:
12399 case elfcpp::R_MIPS_LITERAL:
12400 case elfcpp::R_MIPS_GOT_PAGE:
12401 case elfcpp::R_MIPS_GOT_OFST:
12402 case elfcpp::R_MIPS16_GOT16:
12403 case elfcpp::R_MIPS16_CALL16:
12404 case elfcpp::R_MICROMIPS_GOT16:
12405 case elfcpp::R_MICROMIPS_CALL16:
12406 case elfcpp::R_MICROMIPS_GOT_HI16:
12407 case elfcpp::R_MICROMIPS_GOT_LO16:
12408 case elfcpp::R_MICROMIPS_CALL_HI16:
12409 case elfcpp::R_MICROMIPS_CALL_LO16:
12410 case elfcpp::R_MIPS_EH:
12411 // Absolute in GOT.
12412 return Symbol::RELATIVE_REF;
12414 case elfcpp::R_MIPS_TLS_DTPMOD32:
12415 case elfcpp::R_MIPS_TLS_DTPREL32:
12416 case elfcpp::R_MIPS_TLS_DTPMOD64:
12417 case elfcpp::R_MIPS_TLS_DTPREL64:
12418 case elfcpp::R_MIPS_TLS_GD:
12419 case elfcpp::R_MIPS_TLS_LDM:
12420 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12421 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12422 case elfcpp::R_MIPS_TLS_GOTTPREL:
12423 case elfcpp::R_MIPS_TLS_TPREL32:
12424 case elfcpp::R_MIPS_TLS_TPREL64:
12425 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12426 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12427 case elfcpp::R_MIPS16_TLS_GD:
12428 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12429 case elfcpp::R_MICROMIPS_TLS_GD:
12430 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12431 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12432 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12433 return Symbol::TLS_REF;
12435 case elfcpp::R_MIPS_COPY:
12436 case elfcpp::R_MIPS_JUMP_SLOT:
12438 // Not expected. We will give an error later.
12443 // Report an unsupported relocation against a local symbol.
12445 template<int size, bool big_endian>
12447 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12448 Sized_relobj_file<size, big_endian>* object,
12449 unsigned int r_type)
12451 gold_error(_("%s: unsupported reloc %u against local symbol"),
12452 object->name().c_str(), r_type);
12455 // Report an unsupported relocation against a global symbol.
12457 template<int size, bool big_endian>
12459 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12460 Sized_relobj_file<size, big_endian>* object,
12461 unsigned int r_type,
12464 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12465 object->name().c_str(), r_type, gsym->demangled_name().c_str());
12468 // Return printable name for ABI.
12469 template<int size, bool big_endian>
12471 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12473 switch (e_flags & elfcpp::EF_MIPS_ABI)
12476 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12478 else if (size == 64)
12482 case elfcpp::E_MIPS_ABI_O32:
12484 case elfcpp::E_MIPS_ABI_O64:
12486 case elfcpp::E_MIPS_ABI_EABI32:
12488 case elfcpp::E_MIPS_ABI_EABI64:
12491 return "unknown abi";
12495 template<int size, bool big_endian>
12497 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12499 switch (e_flags & elfcpp::EF_MIPS_MACH)
12501 case elfcpp::E_MIPS_MACH_3900:
12502 return "mips:3900";
12503 case elfcpp::E_MIPS_MACH_4010:
12504 return "mips:4010";
12505 case elfcpp::E_MIPS_MACH_4100:
12506 return "mips:4100";
12507 case elfcpp::E_MIPS_MACH_4111:
12508 return "mips:4111";
12509 case elfcpp::E_MIPS_MACH_4120:
12510 return "mips:4120";
12511 case elfcpp::E_MIPS_MACH_4650:
12512 return "mips:4650";
12513 case elfcpp::E_MIPS_MACH_5400:
12514 return "mips:5400";
12515 case elfcpp::E_MIPS_MACH_5500:
12516 return "mips:5500";
12517 case elfcpp::E_MIPS_MACH_5900:
12518 return "mips:5900";
12519 case elfcpp::E_MIPS_MACH_SB1:
12521 case elfcpp::E_MIPS_MACH_9000:
12522 return "mips:9000";
12523 case elfcpp::E_MIPS_MACH_LS2E:
12524 return "mips:loongson_2e";
12525 case elfcpp::E_MIPS_MACH_LS2F:
12526 return "mips:loongson_2f";
12527 case elfcpp::E_MIPS_MACH_GS464:
12528 return "mips:gs464";
12529 case elfcpp::E_MIPS_MACH_OCTEON:
12530 return "mips:octeon";
12531 case elfcpp::E_MIPS_MACH_OCTEON2:
12532 return "mips:octeon2";
12533 case elfcpp::E_MIPS_MACH_OCTEON3:
12534 return "mips:octeon3";
12535 case elfcpp::E_MIPS_MACH_XLR:
12538 switch (e_flags & elfcpp::EF_MIPS_ARCH)
12541 case elfcpp::E_MIPS_ARCH_1:
12542 return "mips:3000";
12544 case elfcpp::E_MIPS_ARCH_2:
12545 return "mips:6000";
12547 case elfcpp::E_MIPS_ARCH_3:
12548 return "mips:4000";
12550 case elfcpp::E_MIPS_ARCH_4:
12551 return "mips:8000";
12553 case elfcpp::E_MIPS_ARCH_5:
12554 return "mips:mips5";
12556 case elfcpp::E_MIPS_ARCH_32:
12557 return "mips:isa32";
12559 case elfcpp::E_MIPS_ARCH_64:
12560 return "mips:isa64";
12562 case elfcpp::E_MIPS_ARCH_32R2:
12563 return "mips:isa32r2";
12565 case elfcpp::E_MIPS_ARCH_32R6:
12566 return "mips:isa32r6";
12568 case elfcpp::E_MIPS_ARCH_64R2:
12569 return "mips:isa64r2";
12571 case elfcpp::E_MIPS_ARCH_64R6:
12572 return "mips:isa64r6";
12575 return "unknown CPU";
12578 template<int size, bool big_endian>
12579 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12582 big_endian, // is_big_endian
12583 elfcpp::EM_MIPS, // machine_code
12584 true, // has_make_symbol
12585 false, // has_resolve
12586 false, // has_code_fill
12587 true, // is_default_stack_executable
12588 false, // can_icf_inline_merge_sections
12590 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
12591 0x400000, // default_text_segment_address
12592 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
12593 4 * 1024, // common_pagesize (overridable by -z common-page-size)
12594 false, // isolate_execinstr
12595 0, // rosegment_gap
12596 elfcpp::SHN_UNDEF, // small_common_shndx
12597 elfcpp::SHN_UNDEF, // large_common_shndx
12598 0, // small_common_section_flags
12599 0, // large_common_section_flags
12600 NULL, // attributes_section
12601 NULL, // attributes_vendor
12602 "__start", // entry_symbol_name
12603 32, // hash_entry_size
12604 elfcpp::SHT_PROGBITS, // unwind_section_type
12607 template<int size, bool big_endian>
12608 class Target_mips_nacl : public Target_mips<size, big_endian>
12612 : Target_mips<size, big_endian>(&mips_nacl_info)
12616 static const Target::Target_info mips_nacl_info;
12619 template<int size, bool big_endian>
12620 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12623 big_endian, // is_big_endian
12624 elfcpp::EM_MIPS, // machine_code
12625 true, // has_make_symbol
12626 false, // has_resolve
12627 false, // has_code_fill
12628 true, // is_default_stack_executable
12629 false, // can_icf_inline_merge_sections
12631 "/lib/ld.so.1", // dynamic_linker
12632 0x20000, // default_text_segment_address
12633 0x10000, // abi_pagesize (overridable by -z max-page-size)
12634 0x10000, // common_pagesize (overridable by -z common-page-size)
12635 true, // isolate_execinstr
12636 0x10000000, // rosegment_gap
12637 elfcpp::SHN_UNDEF, // small_common_shndx
12638 elfcpp::SHN_UNDEF, // large_common_shndx
12639 0, // small_common_section_flags
12640 0, // large_common_section_flags
12641 NULL, // attributes_section
12642 NULL, // attributes_vendor
12643 "_start", // entry_symbol_name
12644 32, // hash_entry_size
12645 elfcpp::SHT_PROGBITS, // unwind_section_type
12648 // Target selector for Mips. Note this is never instantiated directly.
12649 // It's only used in Target_selector_mips_nacl, below.
12651 template<int size, bool big_endian>
12652 class Target_selector_mips : public Target_selector
12655 Target_selector_mips()
12656 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12658 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12659 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12661 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12662 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12665 Target* do_instantiate_target()
12666 { return new Target_mips<size, big_endian>(); }
12669 template<int size, bool big_endian>
12670 class Target_selector_mips_nacl
12671 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12672 Target_mips_nacl<size, big_endian> >
12675 Target_selector_mips_nacl()
12676 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12677 Target_mips_nacl<size, big_endian> >(
12678 // NaCl currently supports only MIPS32 little-endian.
12679 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12683 Target_selector_mips_nacl<32, true> target_selector_mips32;
12684 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12685 Target_selector_mips_nacl<64, true> target_selector_mips64;
12686 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12688 } // End anonymous namespace.