1 // mips.cc -- mips target support for gold.
3 // Copyright (C) 2011-2017 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), num_pages(0)
637 Got_page_entry(Object* object_, unsigned int symndx_)
638 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
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;
647 // The maximum number of page entries needed for RANGES.
648 unsigned int num_pages;
651 // Hash for Got_page_entry.
653 struct Got_page_entry_hash
656 operator()(Got_page_entry* entry) const
657 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
660 // Equality for Got_page_entry.
662 struct Got_page_entry_eq
665 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
667 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
671 // This class is used to hold .got information when linking.
673 template<int size, bool big_endian>
676 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
677 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
679 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
681 // Unordered set of GOT entries.
682 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
683 Mips_got_entry_hash<size, big_endian>,
684 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
686 // Unordered set of GOT page entries.
687 typedef Unordered_set<Got_page_entry*,
688 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
690 // Unordered set of global GOT entries.
691 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
692 Global_got_entry_set;
696 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
697 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
698 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
699 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
703 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
704 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
706 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
707 unsigned int symndx, Mips_address addend,
708 unsigned int r_type, unsigned int shndx,
709 bool is_section_symbol);
711 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
712 // in OBJECT. FOR_CALL is true if the caller is only interested in
713 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
716 record_global_got_symbol(Mips_symbol<size>* mips_sym,
717 Mips_relobj<size, big_endian>* object,
718 unsigned int r_type, bool dyn_reloc, bool for_call);
720 // Add ENTRY to master GOT and to OBJECT's GOT.
722 record_got_entry(Mips_got_entry<size, big_endian>* entry,
723 Mips_relobj<size, big_endian>* object);
725 // Record that OBJECT has a page relocation against symbol SYMNDX and
726 // that ADDEND is the addend for that relocation.
728 record_got_page_entry(Mips_relobj<size, big_endian>* object,
729 unsigned int symndx, int addend);
731 // Create all entries that should be in the local part of the GOT.
733 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
735 // Create GOT page entries.
737 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
739 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
741 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
742 unsigned int non_reloc_only_global_gotno);
744 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
746 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
748 // Create TLS GOT entries.
750 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
752 // Decide whether the symbol needs an entry in the global part of the primary
753 // GOT, setting global_got_area accordingly. Count the number of global
754 // symbols that are in the primary GOT only because they have dynamic
755 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
757 count_got_symbols(Symbol_table* symtab);
759 // Return the offset of GOT page entry for VALUE.
761 get_got_page_offset(Mips_address value,
762 Mips_output_data_got<size, big_endian>* got);
764 // Count the number of GOT entries required.
768 // Count the number of GOT entries required by ENTRY. Accumulate the result.
770 count_got_entry(Mips_got_entry<size, big_endian>* entry);
772 // Add FROM's GOT entries.
774 add_got_entries(Mips_got_info<size, big_endian>* from);
776 // Add FROM's GOT page entries.
778 add_got_page_entries(Mips_got_info<size, big_endian>* from);
783 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
784 + this->tls_gotno_) * size/8);
787 // Return the number of local GOT entries.
790 { return this->local_gotno_; }
792 // Return the maximum number of page GOT entries needed.
795 { return this->page_gotno_; }
797 // Return the number of global GOT entries.
800 { return this->global_gotno_; }
802 // Set the number of global GOT entries.
804 set_global_gotno(unsigned int global_gotno)
805 { this->global_gotno_ = global_gotno; }
807 // Return the number of GGA_RELOC_ONLY global GOT entries.
809 reloc_only_gotno() const
810 { return this->reloc_only_gotno_; }
812 // Return the number of TLS GOT entries.
815 { return this->tls_gotno_; }
817 // Return the GOT type for this GOT. Used for multi-GOT links only.
819 multigot_got_type(unsigned int got_type) const
823 case GOT_TYPE_STANDARD:
824 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
825 case GOT_TYPE_TLS_OFFSET:
826 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
827 case GOT_TYPE_TLS_PAIR:
828 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
834 // Remove lazy-binding stubs for global symbols in this GOT.
836 remove_lazy_stubs(Target_mips<size, big_endian>* target);
838 // Return offset of this GOT from the start of .got section.
841 { return this->offset_; }
843 // Set offset of this GOT from the start of .got section.
845 set_offset(unsigned int offset)
846 { this->offset_ = offset; }
848 // Set index of this GOT in multi-GOT links.
850 set_index(unsigned int index)
851 { this->index_ = index; }
853 // Return next GOT in multi-GOT links.
854 Mips_got_info<size, big_endian>*
856 { return this->next_; }
858 // Set next GOT in multi-GOT links.
860 set_next(Mips_got_info<size, big_endian>* next)
861 { this->next_ = next; }
863 // Return the offset of TLS LDM entry for this GOT.
865 tls_ldm_offset() const
866 { return this->tls_ldm_offset_; }
868 // Set the offset of TLS LDM entry for this GOT.
870 set_tls_ldm_offset(unsigned int tls_ldm_offset)
871 { this->tls_ldm_offset_ = tls_ldm_offset; }
873 Global_got_entry_set&
875 { return this->global_got_symbols_; }
877 // Return the GOT_TLS_* type required by relocation type R_TYPE.
879 mips_elf_reloc_tls_type(unsigned int r_type)
881 if (tls_gd_reloc(r_type))
884 if (tls_ldm_reloc(r_type))
887 if (tls_gottprel_reloc(r_type))
893 // Return the number of GOT slots needed for GOT TLS type TYPE.
895 mips_tls_got_entries(unsigned int type)
915 // The number of local GOT entries.
916 unsigned int local_gotno_;
917 // The maximum number of page GOT entries needed.
918 unsigned int page_gotno_;
919 // The number of global GOT entries.
920 unsigned int global_gotno_;
921 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
922 unsigned int reloc_only_gotno_;
923 // The number of TLS GOT entries.
924 unsigned int tls_gotno_;
925 // The offset of TLS LDM entry for this GOT.
926 unsigned int tls_ldm_offset_;
927 // All symbols that have global GOT entry.
928 Global_got_entry_set global_got_symbols_;
929 // A hash table holding GOT entries.
930 Got_entry_set got_entries_;
931 // A hash table of GOT page entries.
932 Got_page_entry_set got_page_entries_;
933 // The offset of first GOT page entry for this GOT.
934 unsigned int got_page_offset_start_;
935 // The offset of next available GOT page entry for this GOT.
936 unsigned int got_page_offset_next_;
937 // A hash table that maps GOT page entry value to the GOT offset where
938 // the entry is located.
939 Got_page_offsets got_page_offsets_;
940 // In multi-GOT links, a pointer to the next GOT.
941 Mips_got_info<size, big_endian>* next_;
942 // Index of this GOT in multi-GOT links.
944 // The offset of this GOT in multi-GOT links.
945 unsigned int offset_;
948 // This is a helper class used during relocation scan. It records GOT16 addend.
950 template<int size, bool big_endian>
953 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
955 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
956 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
957 Mips_address _addend)
958 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
962 const Sized_relobj_file<size, big_endian>* object;
969 // .MIPS.abiflags section content
971 template<bool big_endian>
974 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
975 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
976 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
979 : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
980 cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
983 // Version of flags structure.
985 // The level of the ISA: 1-5, 32, 64.
987 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
989 // The size of general purpose registers.
991 // The size of co-processor 1 registers.
993 // The size of co-processor 2 registers.
995 // The floating-point ABI.
997 // Processor-specific extension.
999 // Mask of ASEs used.
1001 // Mask of general flags.
1006 // Mips_symbol class. Holds additional symbol information needed for Mips.
1009 class Mips_symbol : public Sized_symbol<size>
1013 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1014 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1015 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1016 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1017 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1018 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1019 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1022 // Return whether this is a MIPS16 symbol.
1026 // (st_other & STO_MIPS16) == STO_MIPS16
1027 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1028 == elfcpp::STO_MIPS16 >> 2);
1031 // Return whether this is a microMIPS symbol.
1033 is_micromips() const
1035 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1036 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1037 == elfcpp::STO_MICROMIPS >> 2);
1040 // Return whether the symbol needs MIPS16 fn_stub.
1042 need_fn_stub() const
1043 { return this->need_fn_stub_; }
1045 // Set that the symbol needs MIPS16 fn_stub.
1048 { this->need_fn_stub_ = true; }
1050 // Return whether this symbol is referenced by branch relocations from
1051 // any non-PIC input file.
1053 has_nonpic_branches() const
1054 { return this->has_nonpic_branches_; }
1056 // Set that this symbol is referenced by branch relocations from
1057 // any non-PIC input file.
1059 set_has_nonpic_branches()
1060 { this->has_nonpic_branches_ = true; }
1062 // Return the offset of the la25 stub for this symbol from the start of the
1063 // la25 stub section.
1065 la25_stub_offset() const
1066 { return this->la25_stub_offset_; }
1068 // Set the offset of the la25 stub for this symbol from the start of the
1069 // la25 stub section.
1071 set_la25_stub_offset(unsigned int offset)
1072 { this->la25_stub_offset_ = offset; }
1074 // Return whether the symbol has la25 stub. This is true if this symbol is
1075 // for a PIC function, and there are non-PIC branches and jumps to it.
1077 has_la25_stub() const
1078 { return this->la25_stub_offset_ != -1U; }
1080 // Return whether there is a relocation against this symbol that must be
1081 // resolved by the static linker (that is, the relocation cannot possibly
1082 // be made dynamic).
1084 has_static_relocs() const
1085 { return this->has_static_relocs_; }
1087 // Set that there is a relocation against this symbol that must be resolved
1088 // by the static linker (that is, the relocation cannot possibly be made
1091 set_has_static_relocs()
1092 { this->has_static_relocs_ = true; }
1094 // Return whether we must not create a lazy-binding stub for this symbol.
1096 no_lazy_stub() const
1097 { return this->no_lazy_stub_; }
1099 // Set that we must not create a lazy-binding stub for this symbol.
1102 { this->no_lazy_stub_ = true; }
1104 // Return the offset of the lazy-binding stub for this symbol from the start
1105 // of .MIPS.stubs section.
1107 lazy_stub_offset() const
1108 { return this->lazy_stub_offset_; }
1110 // Set the offset of the lazy-binding stub for this symbol from the start
1111 // of .MIPS.stubs section.
1113 set_lazy_stub_offset(unsigned int offset)
1114 { this->lazy_stub_offset_ = offset; }
1116 // Return whether there are any relocations for this symbol where
1117 // pointer equality matters.
1119 pointer_equality_needed() const
1120 { return this->pointer_equality_needed_; }
1122 // Set that there are relocations for this symbol where pointer equality
1125 set_pointer_equality_needed()
1126 { this->pointer_equality_needed_ = true; }
1128 // Return global GOT area where this symbol in located.
1130 global_got_area() const
1131 { return this->global_got_area_; }
1133 // Set global GOT area where this symbol in located.
1135 set_global_got_area(Global_got_area global_got_area)
1136 { this->global_got_area_ = global_got_area; }
1138 // Return the global GOT offset for this symbol. For multi-GOT links, this
1139 // returns the offset from the start of .got section to the first GOT entry
1140 // for the symbol. Note that in multi-GOT links the symbol can have entry
1141 // in more than one GOT.
1143 global_gotoffset() const
1144 { return this->global_gotoffset_; }
1146 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1147 // the symbol can have entry in more than one GOT. This method will set
1148 // the offset only if it is less than current offset.
1150 set_global_gotoffset(unsigned int offset)
1152 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1153 this->global_gotoffset_ = offset;
1156 // Return whether all GOT relocations for this symbol are for calls.
1158 got_only_for_calls() const
1159 { return this->got_only_for_calls_; }
1161 // Set that there is a GOT relocation for this symbol that is not for call.
1163 set_got_not_only_for_calls()
1164 { this->got_only_for_calls_ = false; }
1166 // Return whether this is a PIC symbol.
1170 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1171 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1172 == (elfcpp::STO_MIPS_PIC >> 2));
1175 // Set the flag in st_other field that marks this symbol as PIC.
1179 if (this->is_mips16())
1180 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1181 this->set_nonvis((this->nonvis()
1182 & ~((elfcpp::STO_MIPS16 >> 2)
1183 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1184 | (elfcpp::STO_MIPS_PIC >> 2));
1186 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1187 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1188 | (elfcpp::STO_MIPS_PIC >> 2));
1191 // Set the flag in st_other field that marks this symbol as PLT.
1195 if (this->is_mips16())
1196 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1197 this->set_nonvis((this->nonvis()
1198 & ((elfcpp::STO_MIPS16 >> 2)
1199 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1200 | (elfcpp::STO_MIPS_PLT >> 2));
1203 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1204 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1205 | (elfcpp::STO_MIPS_PLT >> 2));
1208 // Downcast a base pointer to a Mips_symbol pointer.
1209 static Mips_symbol<size>*
1210 as_mips_sym(Symbol* sym)
1211 { return static_cast<Mips_symbol<size>*>(sym); }
1213 // Downcast a base pointer to a Mips_symbol pointer.
1214 static const Mips_symbol<size>*
1215 as_mips_sym(const Symbol* sym)
1216 { return static_cast<const Mips_symbol<size>*>(sym); }
1218 // Return whether the symbol has lazy-binding stub.
1220 has_lazy_stub() const
1221 { return this->has_lazy_stub_; }
1223 // Set whether the symbol has lazy-binding stub.
1225 set_has_lazy_stub(bool has_lazy_stub)
1226 { this->has_lazy_stub_ = has_lazy_stub; }
1228 // Return whether the symbol needs a standard PLT entry.
1230 needs_mips_plt() const
1231 { return this->needs_mips_plt_; }
1233 // Set whether the symbol needs a standard PLT entry.
1235 set_needs_mips_plt(bool needs_mips_plt)
1236 { this->needs_mips_plt_ = needs_mips_plt; }
1238 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1241 needs_comp_plt() const
1242 { return this->needs_comp_plt_; }
1244 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1246 set_needs_comp_plt(bool needs_comp_plt)
1247 { this->needs_comp_plt_ = needs_comp_plt; }
1249 // Return standard PLT entry offset, or -1 if none.
1251 mips_plt_offset() const
1252 { return this->mips_plt_offset_; }
1254 // Set standard PLT entry offset.
1256 set_mips_plt_offset(unsigned int mips_plt_offset)
1257 { this->mips_plt_offset_ = mips_plt_offset; }
1259 // Return whether the symbol has standard PLT entry.
1261 has_mips_plt_offset() const
1262 { return this->mips_plt_offset_ != -1U; }
1264 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1266 comp_plt_offset() const
1267 { return this->comp_plt_offset_; }
1269 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1271 set_comp_plt_offset(unsigned int comp_plt_offset)
1272 { this->comp_plt_offset_ = comp_plt_offset; }
1274 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1276 has_comp_plt_offset() const
1277 { return this->comp_plt_offset_ != -1U; }
1279 // Return MIPS16 fn stub for a symbol.
1280 template<bool big_endian>
1281 Mips16_stub_section<size, big_endian>*
1282 get_mips16_fn_stub() const
1284 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1287 // Set MIPS16 fn stub for a symbol.
1289 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1290 { this->mips16_fn_stub_ = stub; }
1292 // Return whether symbol has MIPS16 fn stub.
1294 has_mips16_fn_stub() const
1295 { return this->mips16_fn_stub_ != NULL; }
1297 // Return MIPS16 call stub for a symbol.
1298 template<bool big_endian>
1299 Mips16_stub_section<size, big_endian>*
1300 get_mips16_call_stub() const
1302 return static_cast<Mips16_stub_section<size, big_endian>*>(
1306 // Set MIPS16 call stub for a symbol.
1308 set_mips16_call_stub(Mips16_stub_section_base* stub)
1309 { this->mips16_call_stub_ = stub; }
1311 // Return whether symbol has MIPS16 call stub.
1313 has_mips16_call_stub() const
1314 { return this->mips16_call_stub_ != NULL; }
1316 // Return MIPS16 call_fp stub for a symbol.
1317 template<bool big_endian>
1318 Mips16_stub_section<size, big_endian>*
1319 get_mips16_call_fp_stub() const
1321 return static_cast<Mips16_stub_section<size, big_endian>*>(
1322 mips16_call_fp_stub_);
1325 // Set MIPS16 call_fp stub for a symbol.
1327 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1328 { this->mips16_call_fp_stub_ = stub; }
1330 // Return whether symbol has MIPS16 call_fp stub.
1332 has_mips16_call_fp_stub() const
1333 { return this->mips16_call_fp_stub_ != NULL; }
1336 get_applied_secondary_got_fixup() const
1337 { return applied_secondary_got_fixup_; }
1340 set_applied_secondary_got_fixup()
1341 { this->applied_secondary_got_fixup_ = true; }
1343 // Return the hash of this symbol.
1347 return gold::string_hash<char>(this->name());
1351 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1352 // appears in any relocs other than a 16 bit call.
1355 // True if this symbol is referenced by branch relocations from
1356 // any non-PIC input file. This is used to determine whether an
1357 // la25 stub is required.
1358 bool has_nonpic_branches_;
1360 // The offset of the la25 stub for this symbol from the start of the
1361 // la25 stub section.
1362 unsigned int la25_stub_offset_;
1364 // True if there is a relocation against this symbol that must be
1365 // resolved by the static linker (that is, the relocation cannot
1366 // possibly be made dynamic).
1367 bool has_static_relocs_;
1369 // Whether we must not create a lazy-binding stub for this symbol.
1370 // This is true if the symbol has relocations related to taking the
1371 // function's address.
1374 // The offset of the lazy-binding stub for this symbol from the start of
1375 // .MIPS.stubs section.
1376 unsigned int lazy_stub_offset_;
1378 // True if there are any relocations for this symbol where pointer equality
1380 bool pointer_equality_needed_;
1382 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1383 // in the global part of the GOT.
1384 Global_got_area global_got_area_;
1386 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1387 // from the start of .got section to the first GOT entry for the symbol.
1388 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1389 unsigned int global_gotoffset_;
1391 // Whether all GOT relocations for this symbol are for calls.
1392 bool got_only_for_calls_;
1393 // Whether the symbol has lazy-binding stub.
1394 bool has_lazy_stub_;
1395 // Whether the symbol needs a standard PLT entry.
1396 bool needs_mips_plt_;
1397 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1398 bool needs_comp_plt_;
1399 // Standard PLT entry offset, or -1 if none.
1400 unsigned int mips_plt_offset_;
1401 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1402 unsigned int comp_plt_offset_;
1403 // MIPS16 fn stub for a symbol.
1404 Mips16_stub_section_base* mips16_fn_stub_;
1405 // MIPS16 call stub for a symbol.
1406 Mips16_stub_section_base* mips16_call_stub_;
1407 // MIPS16 call_fp stub for a symbol.
1408 Mips16_stub_section_base* mips16_call_fp_stub_;
1410 bool applied_secondary_got_fixup_;
1413 // Mips16_stub_section class.
1415 // The mips16 compiler uses a couple of special sections to handle
1416 // floating point arguments.
1418 // Section names that look like .mips16.fn.FNNAME contain stubs that
1419 // copy floating point arguments from the fp regs to the gp regs and
1420 // then jump to FNNAME. If any 32 bit function calls FNNAME, the
1421 // call should be redirected to the stub instead. If no 32 bit
1422 // function calls FNNAME, the stub should be discarded. We need to
1423 // consider any reference to the function, not just a call, because
1424 // if the address of the function is taken we will need the stub,
1425 // since the address might be passed to a 32 bit function.
1427 // Section names that look like .mips16.call.FNNAME contain stubs
1428 // that copy floating point arguments from the gp regs to the fp
1429 // regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1430 // then any 16 bit function that calls FNNAME should be redirected
1431 // to the stub instead. If FNNAME is not a 32 bit function, the
1432 // stub should be discarded.
1434 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1435 // which call FNNAME and then copy the return value from the fp regs
1436 // to the gp regs. These stubs store the return address in $18 while
1437 // calling FNNAME; any function which might call one of these stubs
1438 // must arrange to save $18 around the call. (This case is not
1439 // needed for 32 bit functions that call 16 bit functions, because
1440 // 16 bit functions always return floating point values in both
1441 // $f0/$f1 and $2/$3.)
1443 // Note that in all cases FNNAME might be defined statically.
1444 // Therefore, FNNAME is not used literally. Instead, the relocation
1445 // information will indicate which symbol the section is for.
1447 // We record any stubs that we find in the symbol table.
1449 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1451 class Mips16_stub_section_base { };
1453 template<int size, bool big_endian>
1454 class Mips16_stub_section : public Mips16_stub_section_base
1456 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1459 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1460 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1461 found_r_mips_none_(false)
1463 gold_assert(object->is_mips16_fn_stub_section(shndx)
1464 || object->is_mips16_call_stub_section(shndx)
1465 || object->is_mips16_call_fp_stub_section(shndx));
1468 // Return the object of this stub section.
1469 Mips_relobj<size, big_endian>*
1471 { return this->object_; }
1473 // Return the size of a section.
1475 section_size() const
1476 { return this->object_->section_size(this->shndx_); }
1478 // Return section index of this stub section.
1481 { return this->shndx_; }
1483 // Return symbol index, if stub is for a local function.
1486 { return this->r_sym_; }
1488 // Return symbol, if stub is for a global function.
1491 { return this->gsym_; }
1493 // Return whether stub is for a local function.
1495 is_for_local_function() const
1496 { return this->gsym_ == NULL; }
1498 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1499 // is found in the stub section. Try to find stub target.
1501 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1503 // To find target symbol for this stub, trust the first R_MIPS_NONE
1504 // relocation, if any. Otherwise trust the first relocation, whatever
1506 if (this->found_r_mips_none_)
1508 if (r_type == elfcpp::R_MIPS_NONE)
1510 this->r_sym_ = r_sym;
1512 this->found_r_mips_none_ = true;
1514 else if (!is_target_found())
1515 this->r_sym_ = r_sym;
1518 // This method is called when a new relocation R_TYPE for global symbol GSYM
1519 // is found in the stub section. Try to find stub target.
1521 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1523 // To find target symbol for this stub, trust the first R_MIPS_NONE
1524 // relocation, if any. Otherwise trust the first relocation, whatever
1526 if (this->found_r_mips_none_)
1528 if (r_type == elfcpp::R_MIPS_NONE)
1532 this->found_r_mips_none_ = true;
1534 else if (!is_target_found())
1538 // Return whether we found the stub target.
1540 is_target_found() const
1541 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1543 // Return whether this is a fn stub.
1546 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1548 // Return whether this is a call stub.
1550 is_call_stub() const
1551 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1553 // Return whether this is a call_fp stub.
1555 is_call_fp_stub() const
1556 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1558 // Return the output address.
1560 output_address() const
1562 return (this->object_->output_section(this->shndx_)->address()
1563 + this->object_->output_section_offset(this->shndx_));
1567 // The object of this stub section.
1568 Mips_relobj<size, big_endian>* object_;
1569 // The section index of this stub section.
1570 unsigned int shndx_;
1571 // The symbol index, if stub is for a local function.
1572 unsigned int r_sym_;
1573 // The symbol, if stub is for a global function.
1574 Mips_symbol<size>* gsym_;
1575 // True if we found R_MIPS_NONE relocation in this stub.
1576 bool found_r_mips_none_;
1579 // Mips_relobj class.
1581 template<int size, bool big_endian>
1582 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1584 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1585 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1586 Mips16_stubs_int_map;
1587 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1590 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1591 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1592 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1593 processor_specific_flags_(0), local_symbol_is_mips16_(),
1594 local_symbol_is_micromips_(), mips16_stub_sections_(),
1595 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1596 local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1597 merge_processor_specific_data_(true), got_info_(NULL),
1598 section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1599 section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
1600 attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
1601 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1603 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1604 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1608 { delete this->attributes_section_data_; }
1610 // Downcast a base pointer to a Mips_relobj pointer. This is
1611 // not type-safe but we only use Mips_relobj not the base class.
1612 static Mips_relobj<size, big_endian>*
1613 as_mips_relobj(Relobj* relobj)
1614 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1616 // Downcast a base pointer to a Mips_relobj pointer. This is
1617 // not type-safe but we only use Mips_relobj not the base class.
1618 static const Mips_relobj<size, big_endian>*
1619 as_mips_relobj(const Relobj* relobj)
1620 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1622 // Processor-specific flags in ELF file header. This is valid only after
1625 processor_specific_flags() const
1626 { return this->processor_specific_flags_; }
1628 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1629 // index. This is only valid after do_count_local_symbol is called.
1631 local_symbol_is_mips16(unsigned int r_sym) const
1633 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1634 return this->local_symbol_is_mips16_[r_sym];
1637 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1638 // index. This is only valid after do_count_local_symbol is called.
1640 local_symbol_is_micromips(unsigned int r_sym) const
1642 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1643 return this->local_symbol_is_micromips_[r_sym];
1646 // Get or create MIPS16 stub section.
1647 Mips16_stub_section<size, big_endian>*
1648 get_mips16_stub_section(unsigned int shndx)
1650 typename Mips16_stubs_int_map::const_iterator it =
1651 this->mips16_stub_sections_.find(shndx);
1652 if (it != this->mips16_stub_sections_.end())
1653 return (*it).second;
1655 Mips16_stub_section<size, big_endian>* stub_section =
1656 new Mips16_stub_section<size, big_endian>(this, shndx);
1657 this->mips16_stub_sections_.insert(
1658 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1659 stub_section->shndx(), stub_section));
1660 return stub_section;
1663 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1664 // object doesn't have fn stub for R_SYM.
1665 Mips16_stub_section<size, big_endian>*
1666 get_local_mips16_fn_stub(unsigned int r_sym) const
1668 typename Mips16_stubs_int_map::const_iterator it =
1669 this->local_mips16_fn_stubs_.find(r_sym);
1670 if (it != this->local_mips16_fn_stubs_.end())
1671 return (*it).second;
1675 // Record that this object has MIPS16 fn stub for local symbol. This method
1676 // is only called if we decided not to discard the stub.
1678 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1680 gold_assert(stub->is_for_local_function());
1681 unsigned int r_sym = stub->r_sym();
1682 this->local_mips16_fn_stubs_.insert(
1683 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1687 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1688 // object doesn't have call stub for R_SYM.
1689 Mips16_stub_section<size, big_endian>*
1690 get_local_mips16_call_stub(unsigned int r_sym) const
1692 typename Mips16_stubs_int_map::const_iterator it =
1693 this->local_mips16_call_stubs_.find(r_sym);
1694 if (it != this->local_mips16_call_stubs_.end())
1695 return (*it).second;
1699 // Record that this object has MIPS16 call stub for local symbol. This method
1700 // is only called if we decided not to discard the stub.
1702 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1704 gold_assert(stub->is_for_local_function());
1705 unsigned int r_sym = stub->r_sym();
1706 this->local_mips16_call_stubs_.insert(
1707 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1711 // Record that we found "non 16-bit" call relocation against local symbol
1712 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1715 add_local_non_16bit_call(unsigned int symndx)
1716 { this->local_non_16bit_calls_.insert(symndx); }
1718 // Return true if there is any "non 16-bit" call relocation against local
1719 // symbol SYMNDX in this object.
1721 has_local_non_16bit_call_relocs(unsigned int symndx)
1723 return (this->local_non_16bit_calls_.find(symndx)
1724 != this->local_non_16bit_calls_.end());
1727 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1728 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1729 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1731 add_local_16bit_call(unsigned int symndx)
1732 { this->local_16bit_calls_.insert(symndx); }
1734 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1735 // symbol SYMNDX in this object.
1737 has_local_16bit_call_relocs(unsigned int symndx)
1739 return (this->local_16bit_calls_.find(symndx)
1740 != this->local_16bit_calls_.end());
1743 // Get gp value that was used to create this object.
1746 { return this->gp_; }
1748 // Return whether the object is a PIC object.
1751 { return this->is_pic_; }
1753 // Return whether the object uses N32 ABI.
1756 { return this->is_n32_; }
1758 // Return whether the object uses N64 ABI.
1761 { return size == 64; }
1763 // Return whether the object uses NewABI conventions.
1766 { return this->is_n32() || this->is_n64(); }
1768 // Return Mips_got_info for this object.
1769 Mips_got_info<size, big_endian>*
1770 get_got_info() const
1771 { return this->got_info_; }
1773 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1774 Mips_got_info<size, big_endian>*
1775 get_or_create_got_info()
1777 if (!this->got_info_)
1778 this->got_info_ = new Mips_got_info<size, big_endian>();
1779 return this->got_info_;
1782 // Set Mips_got_info for this object.
1784 set_got_info(Mips_got_info<size, big_endian>* got_info)
1785 { this->got_info_ = got_info; }
1787 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1788 // after do_read_symbols is called.
1790 is_mips16_stub_section(unsigned int shndx)
1792 return (is_mips16_fn_stub_section(shndx)
1793 || is_mips16_call_stub_section(shndx)
1794 || is_mips16_call_fp_stub_section(shndx));
1797 // Return TRUE if relocations in section SHNDX can refer directly to a
1798 // MIPS16 function rather than to a hard-float stub. This is only valid
1799 // after do_read_symbols is called.
1801 section_allows_mips16_refs(unsigned int shndx)
1803 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1806 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1807 // after do_read_symbols is called.
1809 is_mips16_fn_stub_section(unsigned int shndx)
1811 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1812 return this->section_is_mips16_fn_stub_[shndx];
1815 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1816 // after do_read_symbols is called.
1818 is_mips16_call_stub_section(unsigned int shndx)
1820 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1821 return this->section_is_mips16_call_stub_[shndx];
1824 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1825 // valid after do_read_symbols is called.
1827 is_mips16_call_fp_stub_section(unsigned int shndx)
1829 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1830 return this->section_is_mips16_call_fp_stub_[shndx];
1833 // Discard MIPS16 stub secions that are not needed.
1835 discard_mips16_stub_sections(Symbol_table* symtab);
1837 // Return whether there is a .reginfo section.
1839 has_reginfo_section() const
1840 { return this->has_reginfo_section_; }
1842 // Return whether we want to merge processor-specific data.
1844 merge_processor_specific_data() const
1845 { return this->merge_processor_specific_data_; }
1847 // Return gprmask from the .reginfo section of this object.
1850 { return this->gprmask_; }
1852 // Return cprmask1 from the .reginfo section of this object.
1855 { return this->cprmask1_; }
1857 // Return cprmask2 from the .reginfo section of this object.
1860 { return this->cprmask2_; }
1862 // Return cprmask3 from the .reginfo section of this object.
1865 { return this->cprmask3_; }
1867 // Return cprmask4 from the .reginfo section of this object.
1870 { return this->cprmask4_; }
1872 // This is the contents of the .MIPS.abiflags section if there is one.
1873 Mips_abiflags<big_endian>*
1875 { return this->abiflags_; }
1877 // This is the contents of the .gnu.attribute section if there is one.
1878 const Attributes_section_data*
1879 attributes_section_data() const
1880 { return this->attributes_section_data_; }
1883 // Count the local symbols.
1885 do_count_local_symbols(Stringpool_template<char>*,
1886 Stringpool_template<char>*);
1888 // Read the symbol information.
1890 do_read_symbols(Read_symbols_data* sd);
1893 // The name of the options section.
1894 const char* mips_elf_options_section_name()
1895 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1897 // processor-specific flags in ELF file header.
1898 elfcpp::Elf_Word processor_specific_flags_;
1900 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1901 // This is only valid after do_count_local_symbol is called.
1902 std::vector<bool> local_symbol_is_mips16_;
1904 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1905 // This is only valid after do_count_local_symbol is called.
1906 std::vector<bool> local_symbol_is_micromips_;
1908 // Map from section index to the MIPS16 stub for that section. This contains
1909 // all stubs found in this object.
1910 Mips16_stubs_int_map mips16_stub_sections_;
1912 // Local symbols that have "non 16-bit" call relocation. This relocation
1913 // would need to refer to a MIPS16 fn stub, if there is one.
1914 std::set<unsigned int> local_non_16bit_calls_;
1916 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1917 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1918 // relocation that refers to the stub symbol.
1919 std::set<unsigned int> local_16bit_calls_;
1921 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1922 // This contains only the stubs that we decided not to discard.
1923 Mips16_stubs_int_map local_mips16_fn_stubs_;
1925 // Map from local symbol index to the MIPS16 call stub for that symbol.
1926 // This contains only the stubs that we decided not to discard.
1927 Mips16_stubs_int_map local_mips16_call_stubs_;
1929 // gp value that was used to create this object.
1931 // Whether the object is a PIC object.
1933 // Whether the object uses N32 ABI.
1935 // Whether the object contains a .reginfo section.
1936 bool has_reginfo_section_ : 1;
1937 // Whether we merge processor-specific data of this object to output.
1938 bool merge_processor_specific_data_ : 1;
1939 // The Mips_got_info for this object.
1940 Mips_got_info<size, big_endian>* got_info_;
1942 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1943 // This is only valid after do_read_symbols is called.
1944 std::vector<bool> section_is_mips16_fn_stub_;
1946 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1947 // This is only valid after do_read_symbols is called.
1948 std::vector<bool> section_is_mips16_call_stub_;
1950 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1951 // This is only valid after do_read_symbols is called.
1952 std::vector<bool> section_is_mips16_call_fp_stub_;
1954 // .pdr section index.
1955 unsigned int pdr_shndx_;
1957 // Object attributes if there is a .gnu.attributes section or NULL.
1958 Attributes_section_data* attributes_section_data_;
1960 // Object abiflags if there is a .MIPS.abiflags section or NULL.
1961 Mips_abiflags<big_endian>* abiflags_;
1963 // gprmask from the .reginfo section of this object.
1965 // cprmask1 from the .reginfo section of this object.
1967 // cprmask2 from the .reginfo section of this object.
1969 // cprmask3 from the .reginfo section of this object.
1971 // cprmask4 from the .reginfo section of this object.
1975 // Mips_output_data_got class.
1977 template<int size, bool big_endian>
1978 class Mips_output_data_got : public Output_data_got<size, big_endian>
1980 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1981 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1983 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1986 Mips_output_data_got(Target_mips<size, big_endian>* target,
1987 Symbol_table* symtab, Layout* layout)
1988 : Output_data_got<size, big_endian>(), target_(target),
1989 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1990 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1991 secondary_got_relocs_()
1993 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1994 this->set_addralign(16);
1997 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1998 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
2000 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
2001 unsigned int symndx, Mips_address addend,
2002 unsigned int r_type, unsigned int shndx,
2003 bool is_section_symbol)
2005 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
2010 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
2011 // in OBJECT. FOR_CALL is true if the caller is only interested in
2012 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
2015 record_global_got_symbol(Mips_symbol<size>* mips_sym,
2016 Mips_relobj<size, big_endian>* object,
2017 unsigned int r_type, bool dyn_reloc, bool for_call)
2019 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2020 dyn_reloc, for_call);
2023 // Record that OBJECT has a page relocation against symbol SYMNDX and
2024 // that ADDEND is the addend for that relocation.
2026 record_got_page_entry(Mips_relobj<size, big_endian>* object,
2027 unsigned int symndx, int addend)
2028 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2030 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
2031 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2032 // applied in a static link.
2034 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2035 Mips_symbol<size>* gsym)
2036 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2038 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
2039 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
2040 // relocation that needs to be applied in a static link.
2042 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2043 Sized_relobj_file<size, big_endian>* relobj,
2046 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2050 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2051 // secondary GOT at OFFSET.
2053 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2054 Mips_symbol<size>* gsym)
2056 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2060 // Update GOT entry at OFFSET with VALUE.
2062 update_got_entry(unsigned int offset, Mips_address value)
2064 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2067 // Return the number of entries in local part of the GOT. This includes
2068 // local entries, page entries and 2 reserved entries.
2070 get_local_gotno() const
2072 if (!this->multi_got())
2074 return (2 + this->master_got_info_->local_gotno()
2075 + this->master_got_info_->page_gotno());
2078 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2081 // Return dynamic symbol table index of the first symbol with global GOT
2084 first_global_got_dynsym_index() const
2085 { return this->first_global_got_dynsym_index_; }
2087 // Set dynamic symbol table index of the first symbol with global GOT entry.
2089 set_first_global_got_dynsym_index(unsigned int index)
2090 { this->first_global_got_dynsym_index_ = index; }
2092 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2093 // larger than 64K, create multi-GOT.
2095 lay_out_got(Layout* layout, Symbol_table* symtab,
2096 const Input_objects* input_objects);
2098 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2100 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2102 // Attempt to merge GOTs of different input objects.
2104 merge_gots(const Input_objects* input_objects);
2106 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
2107 // this would lead to overflow, true if they were merged successfully.
2109 merge_got_with(Mips_got_info<size, big_endian>* from,
2110 Mips_relobj<size, big_endian>* object,
2111 Mips_got_info<size, big_endian>* to);
2113 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2114 // use OBJECT's GOT.
2116 get_got_page_offset(Mips_address value,
2117 const Mips_relobj<size, big_endian>* object)
2119 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2120 ? this->master_got_info_
2121 : object->get_got_info());
2122 gold_assert(g != NULL);
2123 return g->get_got_page_offset(value, this);
2126 // Return the GOT offset of type GOT_TYPE of the global symbol
2127 // GSYM. For multi-GOT links, use OBJECT's GOT.
2128 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2129 Mips_relobj<size, big_endian>* object) const
2131 if (!this->multi_got())
2132 return gsym->got_offset(got_type);
2135 Mips_got_info<size, big_endian>* g = object->get_got_info();
2136 gold_assert(g != NULL);
2137 return gsym->got_offset(g->multigot_got_type(got_type));
2141 // Return the GOT offset of type GOT_TYPE of the local symbol
2144 got_offset(unsigned int symndx, unsigned int got_type,
2145 Sized_relobj_file<size, big_endian>* object,
2146 uint64_t addend) const
2147 { return object->local_got_offset(symndx, got_type, addend); }
2149 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2151 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2153 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2154 ? this->master_got_info_
2155 : object->get_got_info());
2156 gold_assert(g != NULL);
2157 return g->tls_ldm_offset();
2160 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2162 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2163 Mips_relobj<size, big_endian>* object)
2165 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2166 ? this->master_got_info_
2167 : object->get_got_info());
2168 gold_assert(g != NULL);
2169 g->set_tls_ldm_offset(tls_ldm_offset);
2172 // Return true for multi-GOT links.
2175 { return this->primary_got_ != NULL; }
2177 // Return the offset of OBJECT's GOT from the start of .got section.
2179 get_got_offset(const Mips_relobj<size, big_endian>* object)
2181 if (!this->multi_got())
2185 Mips_got_info<size, big_endian>* g = object->get_got_info();
2186 return g != NULL ? g->offset() : 0;
2190 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2192 add_reloc_only_entries()
2193 { this->master_got_info_->add_reloc_only_entries(this); }
2195 // Return offset of the primary GOT's entry for global symbol.
2197 get_primary_got_offset(const Mips_symbol<size>* sym) const
2199 gold_assert(sym->global_got_area() != GGA_NONE);
2200 return (this->get_local_gotno() + sym->dynsym_index()
2201 - this->first_global_got_dynsym_index()) * size/8;
2204 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2205 // Input argument GOT_OFFSET is always global offset from the start of
2206 // .got section, for both single and multi-GOT links.
2207 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2208 // links, the return value is object_got_offset - 0x7FF0, where
2209 // object_got_offset is offset in the OBJECT's GOT.
2211 gp_offset(unsigned int got_offset,
2212 const Mips_relobj<size, big_endian>* object) const
2214 return (this->address() + got_offset
2215 - this->target_->adjusted_gp_value(object));
2219 // Write out the GOT table.
2221 do_write(Output_file*);
2225 // This class represent dynamic relocations that need to be applied by
2226 // gold because we are using TLS relocations in a static link.
2230 Static_reloc(unsigned int got_offset, unsigned int r_type,
2231 Mips_symbol<size>* gsym)
2232 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2233 { this->u_.global.symbol = gsym; }
2235 Static_reloc(unsigned int got_offset, unsigned int r_type,
2236 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2237 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2239 this->u_.local.relobj = relobj;
2240 this->u_.local.index = index;
2243 // Return the GOT offset.
2246 { return this->got_offset_; }
2251 { return this->r_type_; }
2253 // Whether the symbol is global or not.
2255 symbol_is_global() const
2256 { return this->symbol_is_global_; }
2258 // For a relocation against a global symbol, the global symbol.
2262 gold_assert(this->symbol_is_global_);
2263 return this->u_.global.symbol;
2266 // For a relocation against a local symbol, the defining object.
2267 Sized_relobj_file<size, big_endian>*
2270 gold_assert(!this->symbol_is_global_);
2271 return this->u_.local.relobj;
2274 // For a relocation against a local symbol, the local symbol index.
2278 gold_assert(!this->symbol_is_global_);
2279 return this->u_.local.index;
2283 // GOT offset of the entry to which this relocation is applied.
2284 unsigned int got_offset_;
2285 // Type of relocation.
2286 unsigned int r_type_;
2287 // Whether this relocation is against a global symbol.
2288 bool symbol_is_global_;
2289 // A global or local symbol.
2294 // For a global symbol, the symbol itself.
2295 Mips_symbol<size>* symbol;
2299 // For a local symbol, the object defining object.
2300 Sized_relobj_file<size, big_endian>* relobj;
2301 // For a local symbol, the symbol index.
2308 Target_mips<size, big_endian>* target_;
2309 // The symbol table.
2310 Symbol_table* symbol_table_;
2313 // Static relocs to be applied to the GOT.
2314 std::vector<Static_reloc> static_relocs_;
2315 // .got section view.
2316 unsigned char* got_view_;
2317 // The dynamic symbol table index of the first symbol with global GOT entry.
2318 unsigned int first_global_got_dynsym_index_;
2319 // The master GOT information.
2320 Mips_got_info<size, big_endian>* master_got_info_;
2321 // The primary GOT information.
2322 Mips_got_info<size, big_endian>* primary_got_;
2323 // Secondary GOT fixups.
2324 std::vector<Static_reloc> secondary_got_relocs_;
2327 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2328 // two ways of creating these interfaces. The first is to add:
2330 // lui $25,%hi(func)
2332 // addiu $25,$25,%lo(func)
2334 // to a separate trampoline section. The second is to add:
2336 // lui $25,%hi(func)
2337 // addiu $25,$25,%lo(func)
2339 // immediately before a PIC function "func", but only if a function is at the
2340 // beginning of the section, and the section is not too heavily aligned (i.e we
2341 // would need to add no more than 2 nops before the stub.)
2343 // We only create stubs of the first type.
2345 template<int size, bool big_endian>
2346 class Mips_output_data_la25_stub : public Output_section_data
2348 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2351 Mips_output_data_la25_stub()
2352 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2355 // Create LA25 stub for a symbol.
2357 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2358 Mips_symbol<size>* gsym);
2360 // Return output address of a stub.
2362 stub_address(const Mips_symbol<size>* sym) const
2364 gold_assert(sym->has_la25_stub());
2365 return this->address() + sym->la25_stub_offset();
2370 do_adjust_output_section(Output_section* os)
2371 { os->set_entsize(0); }
2374 // Template for standard LA25 stub.
2375 static const uint32_t la25_stub_entry[];
2376 // Template for microMIPS LA25 stub.
2377 static const uint32_t la25_stub_micromips_entry[];
2379 // Set the final size.
2381 set_final_data_size()
2382 { this->set_data_size(this->symbols_.size() * 16); }
2384 // Create a symbol for SYM stub's value and size, to help make the
2385 // disassembly easier to read.
2387 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2388 Target_mips<size, big_endian>* target, uint64_t symsize);
2390 // Write to a map file.
2392 do_print_to_mapfile(Mapfile* mapfile) const
2393 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2395 // Write out the LA25 stub section.
2397 do_write(Output_file*);
2399 // Symbols that have LA25 stubs.
2400 std::vector<Mips_symbol<size>*> symbols_;
2403 // MIPS-specific relocation writer.
2405 template<int sh_type, bool dynamic, int size, bool big_endian>
2406 struct Mips_output_reloc_writer;
2408 template<int sh_type, bool dynamic, bool big_endian>
2409 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2411 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2412 typedef std::vector<Output_reloc_type> Relocs;
2415 write(typename Relocs::const_iterator p, unsigned char* pov)
2419 template<int sh_type, bool dynamic, bool big_endian>
2420 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2422 typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2423 typedef std::vector<Output_reloc_type> Relocs;
2426 write(typename Relocs::const_iterator p, unsigned char* pov)
2428 elfcpp::Mips64_rel_write<big_endian> orel(pov);
2429 orel.put_r_offset(p->get_address());
2430 orel.put_r_sym(p->get_symbol_index());
2431 orel.put_r_ssym(RSS_UNDEF);
2432 orel.put_r_type(p->type());
2433 if (p->type() == elfcpp::R_MIPS_REL32)
2434 orel.put_r_type2(elfcpp::R_MIPS_64);
2436 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2437 orel.put_r_type3(elfcpp::R_MIPS_NONE);
2441 template<int sh_type, bool dynamic, int size, bool big_endian>
2442 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2446 Mips_output_data_reloc(bool sort_relocs)
2447 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2451 // Write out the data.
2453 do_write(Output_file* of)
2455 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2457 this->template do_write_generic<Writer>(of);
2462 // A class to handle the PLT data.
2464 template<int size, bool big_endian>
2465 class Mips_output_data_plt : public Output_section_data
2467 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2468 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2469 size, big_endian> Reloc_section;
2472 // Create the PLT section. The ordinary .got section is an argument,
2473 // since we need to refer to the start.
2474 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2475 Target_mips<size, big_endian>* target)
2476 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2477 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2480 this->rel_ = new Reloc_section(false);
2481 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2482 elfcpp::SHF_ALLOC, this->rel_,
2483 ORDER_DYNAMIC_PLT_RELOCS, false);
2486 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2488 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2490 // Return the .rel.plt section data.
2493 { return this->rel_; }
2495 // Return the number of PLT entries.
2498 { return this->symbols_.size(); }
2500 // Return the offset of the first non-reserved PLT entry.
2502 first_plt_entry_offset() const
2503 { return sizeof(plt0_entry_o32); }
2505 // Return the size of a PLT entry.
2507 plt_entry_size() const
2508 { return sizeof(plt_entry); }
2510 // Set final PLT offsets. For each symbol, determine whether standard or
2511 // compressed (MIPS16 or microMIPS) PLT entry is used.
2515 // Return the offset of the first standard PLT entry.
2517 first_mips_plt_offset() const
2518 { return this->plt_header_size_; }
2520 // Return the offset of the first compressed PLT entry.
2522 first_comp_plt_offset() const
2523 { return this->plt_header_size_ + this->plt_mips_offset_; }
2525 // Return whether there are any standard PLT entries.
2527 has_standard_entries() const
2528 { return this->plt_mips_offset_ > 0; }
2530 // Return the output address of standard PLT entry.
2532 mips_entry_address(const Mips_symbol<size>* sym) const
2534 gold_assert (sym->has_mips_plt_offset());
2535 return (this->address() + this->first_mips_plt_offset()
2536 + sym->mips_plt_offset());
2539 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2541 comp_entry_address(const Mips_symbol<size>* sym) const
2543 gold_assert (sym->has_comp_plt_offset());
2544 return (this->address() + this->first_comp_plt_offset()
2545 + sym->comp_plt_offset());
2550 do_adjust_output_section(Output_section* os)
2551 { os->set_entsize(0); }
2553 // Write to a map file.
2555 do_print_to_mapfile(Mapfile* mapfile) const
2556 { mapfile->print_output_data(this, _(".plt")); }
2559 // Template for the first PLT entry.
2560 static const uint32_t plt0_entry_o32[];
2561 static const uint32_t plt0_entry_n32[];
2562 static const uint32_t plt0_entry_n64[];
2563 static const uint32_t plt0_entry_micromips_o32[];
2564 static const uint32_t plt0_entry_micromips32_o32[];
2566 // Template for subsequent PLT entries.
2567 static const uint32_t plt_entry[];
2568 static const uint32_t plt_entry_r6[];
2569 static const uint32_t plt_entry_mips16_o32[];
2570 static const uint32_t plt_entry_micromips_o32[];
2571 static const uint32_t plt_entry_micromips32_o32[];
2573 // Set the final size.
2575 set_final_data_size()
2577 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2578 + this->plt_comp_offset_);
2581 // Write out the PLT data.
2583 do_write(Output_file*);
2585 // Return whether the plt header contains microMIPS code. For the sake of
2586 // cache alignment always use a standard header whenever any standard entries
2587 // are present even if microMIPS entries are present as well. This also lets
2588 // the microMIPS header rely on the value of $v0 only set by microMIPS
2589 // entries, for a small size reduction.
2591 is_plt_header_compressed() const
2593 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2594 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2597 // Return the size of the PLT header.
2599 get_plt_header_size() const
2601 if (this->target_->is_output_n64())
2602 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2603 else if (this->target_->is_output_n32())
2604 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2605 else if (!this->is_plt_header_compressed())
2606 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2607 else if (this->target_->use_32bit_micromips_instructions())
2608 return (2 * sizeof(plt0_entry_micromips32_o32)
2609 / sizeof(plt0_entry_micromips32_o32[0]));
2611 return (2 * sizeof(plt0_entry_micromips_o32)
2612 / sizeof(plt0_entry_micromips_o32[0]));
2615 // Return the PLT header entry.
2617 get_plt_header_entry() const
2619 if (this->target_->is_output_n64())
2620 return plt0_entry_n64;
2621 else if (this->target_->is_output_n32())
2622 return plt0_entry_n32;
2623 else if (!this->is_plt_header_compressed())
2624 return plt0_entry_o32;
2625 else if (this->target_->use_32bit_micromips_instructions())
2626 return plt0_entry_micromips32_o32;
2628 return plt0_entry_micromips_o32;
2631 // Return the size of the standard PLT entry.
2633 standard_plt_entry_size() const
2634 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2636 // Return the size of the compressed PLT entry.
2638 compressed_plt_entry_size() const
2640 gold_assert(!this->target_->is_output_newabi());
2642 if (!this->target_->is_output_micromips())
2643 return (2 * sizeof(plt_entry_mips16_o32)
2644 / sizeof(plt_entry_mips16_o32[0]));
2645 else if (this->target_->use_32bit_micromips_instructions())
2646 return (2 * sizeof(plt_entry_micromips32_o32)
2647 / sizeof(plt_entry_micromips32_o32[0]));
2649 return (2 * sizeof(plt_entry_micromips_o32)
2650 / sizeof(plt_entry_micromips_o32[0]));
2653 // The reloc section.
2654 Reloc_section* rel_;
2655 // The .got.plt section.
2656 Output_data_space* got_plt_;
2657 // Symbols that have PLT entry.
2658 std::vector<Mips_symbol<size>*> symbols_;
2659 // The offset of the next standard PLT entry to create.
2660 unsigned int plt_mips_offset_;
2661 // The offset of the next compressed PLT entry to create.
2662 unsigned int plt_comp_offset_;
2663 // The size of the PLT header in bytes.
2664 unsigned int plt_header_size_;
2666 Target_mips<size, big_endian>* target_;
2669 // A class to handle the .MIPS.stubs data.
2671 template<int size, bool big_endian>
2672 class Mips_output_data_mips_stubs : public Output_section_data
2674 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2676 // Unordered set of .MIPS.stubs entries.
2677 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2678 Mips_stubs_entry_set;
2681 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2682 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2683 stub_offsets_are_set_(false), target_(target)
2686 // Create entry for a symbol.
2688 make_entry(Mips_symbol<size>*);
2690 // Remove entry for a symbol.
2692 remove_entry(Mips_symbol<size>* gsym);
2694 // Set stub offsets for symbols. This method expects that the number of
2695 // entries in dynamic symbol table is set.
2697 set_lazy_stub_offsets();
2700 set_needs_dynsym_value();
2702 // Set the number of entries in dynamic symbol table.
2704 set_dynsym_count(unsigned int dynsym_count)
2705 { this->dynsym_count_ = dynsym_count; }
2707 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2708 // count is greater than 0x10000. If the dynamic symbol count is less than
2709 // 0x10000, the stub will be 4 bytes smaller.
2710 // There's no disadvantage from using microMIPS code here, so for the sake of
2711 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2712 // output produced at all. This has a benefit of stubs being shorter by
2713 // 4 bytes each too, unless in the insn32 mode.
2715 stub_max_size() const
2717 if (!this->target_->is_output_micromips()
2718 || this->target_->use_32bit_micromips_instructions())
2724 // Return the size of the stub. This method expects that the final dynsym
2729 gold_assert(this->dynsym_count_ != -1U);
2730 if (this->dynsym_count_ > 0x10000)
2731 return this->stub_max_size();
2733 return this->stub_max_size() - 4;
2736 // Return output address of a stub.
2738 stub_address(const Mips_symbol<size>* sym) const
2740 gold_assert(sym->has_lazy_stub());
2741 return this->address() + sym->lazy_stub_offset();
2746 do_adjust_output_section(Output_section* os)
2747 { os->set_entsize(0); }
2749 // Write to a map file.
2751 do_print_to_mapfile(Mapfile* mapfile) const
2752 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2755 static const uint32_t lazy_stub_normal_1[];
2756 static const uint32_t lazy_stub_normal_1_n64[];
2757 static const uint32_t lazy_stub_normal_2[];
2758 static const uint32_t lazy_stub_normal_2_n64[];
2759 static const uint32_t lazy_stub_big[];
2760 static const uint32_t lazy_stub_big_n64[];
2762 static const uint32_t lazy_stub_micromips_normal_1[];
2763 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2764 static const uint32_t lazy_stub_micromips_normal_2[];
2765 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2766 static const uint32_t lazy_stub_micromips_big[];
2767 static const uint32_t lazy_stub_micromips_big_n64[];
2769 static const uint32_t lazy_stub_micromips32_normal_1[];
2770 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2771 static const uint32_t lazy_stub_micromips32_normal_2[];
2772 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2773 static const uint32_t lazy_stub_micromips32_big[];
2774 static const uint32_t lazy_stub_micromips32_big_n64[];
2776 // Set the final size.
2778 set_final_data_size()
2779 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2781 // Write out the .MIPS.stubs data.
2783 do_write(Output_file*);
2785 // .MIPS.stubs symbols
2786 Mips_stubs_entry_set symbols_;
2787 // Number of entries in dynamic symbol table.
2788 unsigned int dynsym_count_;
2789 // Whether the stub offsets are set.
2790 bool stub_offsets_are_set_;
2792 Target_mips<size, big_endian>* target_;
2795 // This class handles Mips .reginfo output section.
2797 template<int size, bool big_endian>
2798 class Mips_output_section_reginfo : public Output_section_data
2800 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2803 Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2804 Valtype gprmask, Valtype cprmask1,
2805 Valtype cprmask2, Valtype cprmask3,
2807 : Output_section_data(24, 4, true), target_(target),
2808 gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2809 cprmask3_(cprmask3), cprmask4_(cprmask4)
2813 // Write to a map file.
2815 do_print_to_mapfile(Mapfile* mapfile) const
2816 { mapfile->print_output_data(this, _(".reginfo")); }
2818 // Write out reginfo section.
2820 do_write(Output_file* of);
2823 Target_mips<size, big_endian>* target_;
2825 // gprmask of the output .reginfo section.
2827 // cprmask1 of the output .reginfo section.
2829 // cprmask2 of the output .reginfo section.
2831 // cprmask3 of the output .reginfo section.
2833 // cprmask4 of the output .reginfo section.
2837 // This class handles .MIPS.options output section.
2839 template<int size, bool big_endian>
2840 class Mips_output_section_options : public Output_section
2843 Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
2844 elfcpp::Elf_Xword flags,
2845 Target_mips<size, big_endian>* target)
2846 : Output_section(name, type, flags), target_(target)
2848 // After the input sections are written, we only need to update
2849 // ri_gp_value field of ODK_REGINFO entries.
2850 this->set_after_input_sections();
2854 // Write out option section.
2856 do_write(Output_file* of);
2859 Target_mips<size, big_endian>* target_;
2862 // This class handles .MIPS.abiflags output section.
2864 template<int size, bool big_endian>
2865 class Mips_output_section_abiflags : public Output_section_data
2868 Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2869 : Output_section_data(24, 8, true), abiflags_(abiflags)
2873 // Write to a map file.
2875 do_print_to_mapfile(Mapfile* mapfile) const
2876 { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2879 do_write(Output_file* of);
2882 const Mips_abiflags<big_endian>& abiflags_;
2885 // The MIPS target has relocation types which default handling of relocatable
2886 // relocation cannot process. So we have to extend the default code.
2888 template<bool big_endian, typename Classify_reloc>
2889 class Mips_scan_relocatable_relocs :
2890 public Default_scan_relocatable_relocs<Classify_reloc>
2893 // Return the strategy to use for a local symbol which is a section
2894 // symbol, given the relocation type.
2895 inline Relocatable_relocs::Reloc_strategy
2896 local_section_strategy(unsigned int r_type, Relobj* object)
2898 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2899 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2904 case elfcpp::R_MIPS_26:
2905 return Relocatable_relocs::RELOC_SPECIAL;
2908 return Default_scan_relocatable_relocs<Classify_reloc>::
2909 local_section_strategy(r_type, object);
2915 // Mips_copy_relocs class. The only difference from the base class is the
2916 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2917 // Mips cannot convert all relocation types to dynamic relocs. If a reloc
2918 // cannot be made dynamic, a COPY reloc is emitted.
2920 template<int sh_type, int size, bool big_endian>
2921 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2925 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2928 // Emit any saved relocations which turn out to be needed. This is
2929 // called after all the relocs have been scanned.
2931 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2932 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2935 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2938 // Emit this reloc if appropriate. This is called after we have
2939 // scanned all the relocations, so we know whether we emitted a
2940 // COPY relocation for SYM_.
2942 emit_entry(Copy_reloc_entry& entry,
2943 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2944 Symbol_table* symtab, Layout* layout,
2945 Target_mips<size, big_endian>* target);
2949 // Return true if the symbol SYM should be considered to resolve local
2950 // to the current module, and false otherwise. The logic is taken from
2951 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2953 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2954 bool local_protected)
2956 // If it's a local sym, of course we resolve locally.
2960 // STV_HIDDEN or STV_INTERNAL ones must be local.
2961 if (sym->visibility() == elfcpp::STV_HIDDEN
2962 || sym->visibility() == elfcpp::STV_INTERNAL)
2965 // If we don't have a definition in a regular file, then we can't
2966 // resolve locally. The sym is either undefined or dynamic.
2967 if (sym->is_from_dynobj() || sym->is_undefined())
2970 // Forced local symbols resolve locally.
2971 if (sym->is_forced_local())
2974 // As do non-dynamic symbols.
2975 if (!has_dynsym_entry)
2978 // At this point, we know the symbol is defined and dynamic. In an
2979 // executable it must resolve locally, likewise when building symbolic
2980 // shared libraries.
2981 if (parameters->options().output_is_executable()
2982 || parameters->options().Bsymbolic())
2985 // Now deal with defined dynamic symbols in shared libraries. Ones
2986 // with default visibility might not resolve locally.
2987 if (sym->visibility() == elfcpp::STV_DEFAULT)
2990 // STV_PROTECTED non-function symbols are local.
2991 if (sym->type() != elfcpp::STT_FUNC)
2994 // Function pointer equality tests may require that STV_PROTECTED
2995 // symbols be treated as dynamic symbols. If the address of a
2996 // function not defined in an executable is set to that function's
2997 // plt entry in the executable, then the address of the function in
2998 // a shared library must also be the plt entry in the executable.
2999 return local_protected;
3002 // Return TRUE if references to this symbol always reference the symbol in this
3005 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3007 return symbol_refs_local(sym, has_dynsym_entry, false);
3010 // Return TRUE if calls to this symbol always call the version in this object.
3012 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3014 return symbol_refs_local(sym, has_dynsym_entry, true);
3017 // Compare GOT offsets of two symbols.
3019 template<int size, bool big_endian>
3021 got_offset_compare(Symbol* sym1, Symbol* sym2)
3023 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
3024 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
3025 unsigned int area1 = mips_sym1->global_got_area();
3026 unsigned int area2 = mips_sym2->global_got_area();
3027 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
3029 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3031 return area1 < area2;
3033 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
3036 // This method divides dynamic symbols into symbols that have GOT entry, and
3037 // symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
3038 // Mips ABI requires that symbols with the GOT entry must be at the end of
3039 // dynamic symbol table, and the order in dynamic symbol table must match the
3042 template<int size, bool big_endian>
3044 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3045 std::vector<Symbol*>* non_got_symbols,
3046 std::vector<Symbol*>* got_symbols)
3048 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3049 p != dyn_symbols->end();
3052 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3053 if (mips_sym->global_got_area() == GGA_NORMAL
3054 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3055 got_symbols->push_back(mips_sym);
3057 non_got_symbols->push_back(mips_sym);
3060 std::sort(got_symbols->begin(), got_symbols->end(),
3061 got_offset_compare<size, big_endian>);
3064 // Functor class for processing the global symbol table.
3066 template<int size, bool big_endian>
3067 class Symbol_visitor_check_symbols
3070 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3071 Layout* layout, Symbol_table* symtab)
3072 : target_(target), layout_(layout), symtab_(symtab)
3076 operator()(Sized_symbol<size>* sym)
3078 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3079 if (local_pic_function<size, big_endian>(mips_sym))
3081 // SYM is a function that might need $25 to be valid on entry.
3082 // If we're creating a non-PIC relocatable object, mark SYM as
3083 // being PIC. If we're creating a non-relocatable object with
3084 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3086 if (parameters->options().relocatable())
3088 if (!parameters->options().output_is_position_independent())
3089 mips_sym->set_pic();
3091 else if (mips_sym->has_nonpic_branches())
3093 this->target_->la25_stub_section(layout_)
3094 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3100 Target_mips<size, big_endian>* target_;
3102 Symbol_table* symtab_;
3105 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3106 // and endianness. The relocation format for MIPS-64 is non-standard.
3108 template<int sh_type, int size, bool big_endian>
3109 struct Mips_reloc_types;
3111 template<bool big_endian>
3112 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3114 typedef typename elfcpp::Rel<32, big_endian> Reloc;
3115 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3117 static typename elfcpp::Elf_types<32>::Elf_Swxword
3118 get_r_addend(const Reloc*)
3122 set_reloc_addend(Reloc_write*,
3123 typename elfcpp::Elf_types<32>::Elf_Swxword)
3124 { gold_unreachable(); }
3127 template<bool big_endian>
3128 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3130 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3131 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3133 static typename elfcpp::Elf_types<32>::Elf_Swxword
3134 get_r_addend(const Reloc* reloc)
3135 { return reloc->get_r_addend(); }
3138 set_reloc_addend(Reloc_write* p,
3139 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3140 { p->put_r_addend(val); }
3143 template<bool big_endian>
3144 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3146 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3147 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3149 static typename elfcpp::Elf_types<64>::Elf_Swxword
3150 get_r_addend(const Reloc*)
3154 set_reloc_addend(Reloc_write*,
3155 typename elfcpp::Elf_types<64>::Elf_Swxword)
3156 { gold_unreachable(); }
3159 template<bool big_endian>
3160 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3162 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3163 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3165 static typename elfcpp::Elf_types<64>::Elf_Swxword
3166 get_r_addend(const Reloc* reloc)
3167 { return reloc->get_r_addend(); }
3170 set_reloc_addend(Reloc_write* p,
3171 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3172 { p->put_r_addend(val); }
3175 // Forward declaration.
3177 mips_get_size_for_reloc(unsigned int, Relobj*);
3179 // A class for inquiring about properties of a relocation,
3180 // used while scanning relocs during a relocatable link and
3181 // garbage collection.
3183 template<int sh_type_, int size, bool big_endian>
3184 class Mips_classify_reloc;
3186 template<int sh_type_, bool big_endian>
3187 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3188 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3191 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3193 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3196 // Return the symbol referred to by the relocation.
3197 static inline unsigned int
3198 get_r_sym(const Reltype* reloc)
3199 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3201 // Return the type of the relocation.
3202 static inline unsigned int
3203 get_r_type(const Reltype* reloc)
3204 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3206 static inline unsigned int
3207 get_r_type2(const Reltype*)
3210 static inline unsigned int
3211 get_r_type3(const Reltype*)
3214 static inline unsigned int
3215 get_r_ssym(const Reltype*)
3218 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3219 static inline unsigned int
3220 get_r_addend(const Reltype* reloc)
3222 if (sh_type_ == elfcpp::SHT_REL)
3224 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3227 // Write the r_info field to a new reloc, using the r_info field from
3228 // the original reloc, replacing the r_sym field with R_SYM.
3230 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3232 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3233 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3236 // Write the r_addend field to a new reloc.
3238 put_r_addend(Reltype_write* to,
3239 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3240 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3242 // Return the size of the addend of the relocation (only used for SHT_REL).
3244 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3245 { return mips_get_size_for_reloc(r_type, obj); }
3248 template<int sh_type_, bool big_endian>
3249 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3250 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3253 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3255 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3258 // Return the symbol referred to by the relocation.
3259 static inline unsigned int
3260 get_r_sym(const Reltype* reloc)
3261 { return reloc->get_r_sym(); }
3263 // Return the r_type of the relocation.
3264 static inline unsigned int
3265 get_r_type(const Reltype* reloc)
3266 { return reloc->get_r_type(); }
3268 // Return the r_type2 of the relocation.
3269 static inline unsigned int
3270 get_r_type2(const Reltype* reloc)
3271 { return reloc->get_r_type2(); }
3273 // Return the r_type3 of the relocation.
3274 static inline unsigned int
3275 get_r_type3(const Reltype* reloc)
3276 { return reloc->get_r_type3(); }
3278 // Return the special symbol of the relocation.
3279 static inline unsigned int
3280 get_r_ssym(const Reltype* reloc)
3281 { return reloc->get_r_ssym(); }
3283 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3284 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3285 get_r_addend(const Reltype* reloc)
3287 if (sh_type_ == elfcpp::SHT_REL)
3289 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3292 // Write the r_info field to a new reloc, using the r_info field from
3293 // the original reloc, replacing the r_sym field with R_SYM.
3295 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3297 new_reloc->put_r_sym(r_sym);
3298 new_reloc->put_r_ssym(reloc->get_r_ssym());
3299 new_reloc->put_r_type3(reloc->get_r_type3());
3300 new_reloc->put_r_type2(reloc->get_r_type2());
3301 new_reloc->put_r_type(reloc->get_r_type());
3304 // Write the r_addend field to a new reloc.
3306 put_r_addend(Reltype_write* to,
3307 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3308 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3310 // Return the size of the addend of the relocation (only used for SHT_REL).
3312 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3313 { return mips_get_size_for_reloc(r_type, obj); }
3316 template<int size, bool big_endian>
3317 class Target_mips : public Sized_target<size, big_endian>
3319 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3320 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3322 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3323 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3324 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3326 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3330 Target_mips(const Target::Target_info* info = &mips_info)
3331 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3332 got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3333 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3334 mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3335 mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
3336 entry_symbol_is_compressed_(false), insn32_(false)
3338 this->add_machine_extensions();
3341 // The offset of $gp from the beginning of the .got section.
3342 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3344 // The maximum size of the GOT for it to be addressable using 16-bit
3345 // offsets from $gp.
3346 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3348 // Make a new symbol table entry for the Mips target.
3350 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3351 { return new Mips_symbol<size>(); }
3353 // Process the relocations to determine unreferenced sections for
3354 // garbage collection.
3356 gc_process_relocs(Symbol_table* symtab,
3358 Sized_relobj_file<size, big_endian>* object,
3359 unsigned int data_shndx,
3360 unsigned int sh_type,
3361 const unsigned char* prelocs,
3363 Output_section* output_section,
3364 bool needs_special_offset_handling,
3365 size_t local_symbol_count,
3366 const unsigned char* plocal_symbols);
3368 // Scan the relocations to look for symbol adjustments.
3370 scan_relocs(Symbol_table* symtab,
3372 Sized_relobj_file<size, big_endian>* object,
3373 unsigned int data_shndx,
3374 unsigned int sh_type,
3375 const unsigned char* prelocs,
3377 Output_section* output_section,
3378 bool needs_special_offset_handling,
3379 size_t local_symbol_count,
3380 const unsigned char* plocal_symbols);
3382 // Finalize the sections.
3384 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3386 // Relocate a section.
3388 relocate_section(const Relocate_info<size, big_endian>*,
3389 unsigned int sh_type,
3390 const unsigned char* prelocs,
3392 Output_section* output_section,
3393 bool needs_special_offset_handling,
3394 unsigned char* view,
3395 Mips_address view_address,
3396 section_size_type view_size,
3397 const Reloc_symbol_changes*);
3399 // Scan the relocs during a relocatable link.
3401 scan_relocatable_relocs(Symbol_table* symtab,
3403 Sized_relobj_file<size, big_endian>* object,
3404 unsigned int data_shndx,
3405 unsigned int sh_type,
3406 const unsigned char* prelocs,
3408 Output_section* output_section,
3409 bool needs_special_offset_handling,
3410 size_t local_symbol_count,
3411 const unsigned char* plocal_symbols,
3412 Relocatable_relocs*);
3414 // Scan the relocs for --emit-relocs.
3416 emit_relocs_scan(Symbol_table* symtab,
3418 Sized_relobj_file<size, big_endian>* object,
3419 unsigned int data_shndx,
3420 unsigned int sh_type,
3421 const unsigned char* prelocs,
3423 Output_section* output_section,
3424 bool needs_special_offset_handling,
3425 size_t local_symbol_count,
3426 const unsigned char* plocal_syms,
3427 Relocatable_relocs* rr);
3429 // Emit relocations for a section.
3431 relocate_relocs(const Relocate_info<size, big_endian>*,
3432 unsigned int sh_type,
3433 const unsigned char* prelocs,
3435 Output_section* output_section,
3436 typename elfcpp::Elf_types<size>::Elf_Off
3437 offset_in_output_section,
3438 unsigned char* view,
3439 Mips_address view_address,
3440 section_size_type view_size,
3441 unsigned char* reloc_view,
3442 section_size_type reloc_view_size);
3444 // Perform target-specific processing in a relocatable link. This is
3445 // only used if we use the relocation strategy RELOC_SPECIAL.
3447 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3448 unsigned int sh_type,
3449 const unsigned char* preloc_in,
3451 Output_section* output_section,
3452 typename elfcpp::Elf_types<size>::Elf_Off
3453 offset_in_output_section,
3454 unsigned char* view,
3455 Mips_address view_address,
3456 section_size_type view_size,
3457 unsigned char* preloc_out);
3459 // Return whether SYM is defined by the ABI.
3461 do_is_defined_by_abi(const Symbol* sym) const
3463 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3464 || (strcmp(sym->name(), "_gp_disp") == 0)
3465 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3468 // Return the number of entries in the GOT.
3470 got_entry_count() const
3472 if (!this->has_got_section())
3474 return this->got_size() / (size/8);
3477 // Return the number of entries in the PLT.
3479 plt_entry_count() const
3481 if (this->plt_ == NULL)
3483 return this->plt_->entry_count();
3486 // Return the offset of the first non-reserved PLT entry.
3488 first_plt_entry_offset() const
3489 { return this->plt_->first_plt_entry_offset(); }
3491 // Return the size of each PLT entry.
3493 plt_entry_size() const
3494 { return this->plt_->plt_entry_size(); }
3496 // Get the GOT section, creating it if necessary.
3497 Mips_output_data_got<size, big_endian>*
3498 got_section(Symbol_table*, Layout*);
3500 // Get the GOT section.
3501 Mips_output_data_got<size, big_endian>*
3504 gold_assert(this->got_ != NULL);
3508 // Get the .MIPS.stubs section, creating it if necessary.
3509 Mips_output_data_mips_stubs<size, big_endian>*
3510 mips_stubs_section(Layout* layout);
3512 // Get the .MIPS.stubs section.
3513 Mips_output_data_mips_stubs<size, big_endian>*
3514 mips_stubs_section() const
3516 gold_assert(this->mips_stubs_ != NULL);
3517 return this->mips_stubs_;
3520 // Get the LA25 stub section, creating it if necessary.
3521 Mips_output_data_la25_stub<size, big_endian>*
3522 la25_stub_section(Layout*);
3524 // Get the LA25 stub section.
3525 Mips_output_data_la25_stub<size, big_endian>*
3528 gold_assert(this->la25_stub_ != NULL);
3529 return this->la25_stub_;
3532 // Get gp value. It has the value of .got + 0x7FF0.
3536 if (this->gp_ != NULL)
3537 return this->gp_->value();
3541 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3542 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3544 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3546 if (this->gp_ == NULL)
3549 bool multi_got = false;
3550 if (this->has_got_section())
3551 multi_got = this->got_section()->multi_got();
3553 return this->gp_->value();
3555 return this->gp_->value() + this->got_section()->get_got_offset(object);
3558 // Get the dynamic reloc section, creating it if necessary.
3560 rel_dyn_section(Layout*);
3563 do_has_custom_set_dynsym_indexes() const
3566 // Don't emit input .reginfo/.MIPS.abiflags sections to
3567 // output .reginfo/.MIPS.abiflags.
3569 do_should_include_section(elfcpp::Elf_Word sh_type) const
3571 return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3572 && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3575 // Set the dynamic symbol indexes. INDEX is the index of the first
3576 // global dynamic symbol. Pointers to the symbols are stored into the
3577 // vector SYMS. The names are added to DYNPOOL. This returns an
3578 // updated dynamic symbol index.
3580 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3581 std::vector<Symbol*>* syms, Stringpool* dynpool,
3582 Versions* versions, Symbol_table* symtab) const;
3584 // Remove .MIPS.stubs entry for a symbol.
3586 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3588 if (this->mips_stubs_ != NULL)
3589 this->mips_stubs_->remove_entry(sym);
3592 // The value to write into got[1] for SVR4 targets, to identify it is
3593 // a GNU object. The dynamic linker can then use got[1] to store the
3596 mips_elf_gnu_got1_mask()
3598 if (this->is_output_n64())
3599 return (uint64_t)1 << 63;
3604 // Whether the output has microMIPS code. This is valid only after
3605 // merge_obj_e_flags() is called.
3607 is_output_micromips() const
3609 gold_assert(this->are_processor_specific_flags_set());
3610 return elfcpp::is_micromips(this->processor_specific_flags());
3613 // Whether the output uses N32 ABI. This is valid only after
3614 // merge_obj_e_flags() is called.
3616 is_output_n32() const
3618 gold_assert(this->are_processor_specific_flags_set());
3619 return elfcpp::abi_n32(this->processor_specific_flags());
3622 // Whether the output uses R6 ISA. This is valid only after
3623 // merge_obj_e_flags() is called.
3625 is_output_r6() const
3627 gold_assert(this->are_processor_specific_flags_set());
3628 return elfcpp::r6_isa(this->processor_specific_flags());
3631 // Whether the output uses N64 ABI.
3633 is_output_n64() const
3634 { return size == 64; }
3636 // Whether the output uses NEWABI. This is valid only after
3637 // merge_obj_e_flags() is called.
3639 is_output_newabi() const
3640 { return this->is_output_n32() || this->is_output_n64(); }
3642 // Whether we can only use 32-bit microMIPS instructions.
3644 use_32bit_micromips_instructions() const
3645 { return this->insn32_; }
3647 // Return the r_sym field from a relocation.
3649 get_r_sym(const unsigned char* preloc) const
3651 // Since REL and RELA relocs share the same structure through
3652 // the r_info field, we can just use REL here.
3653 Reltype rel(preloc);
3654 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3659 // Return the value to use for a dynamic symbol which requires special
3660 // treatment. This is how we support equality comparisons of function
3661 // pointers across shared library boundaries, as described in the
3662 // processor specific ABI supplement.
3664 do_dynsym_value(const Symbol* gsym) const;
3666 // Make an ELF object.
3668 do_make_elf_object(const std::string&, Input_file*, off_t,
3669 const elfcpp::Ehdr<size, big_endian>& ehdr);
3672 do_make_elf_object(const std::string&, Input_file*, off_t,
3673 const elfcpp::Ehdr<size, !big_endian>&)
3674 { gold_unreachable(); }
3676 // Make an output section.
3678 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3679 elfcpp::Elf_Xword flags)
3681 if (type == elfcpp::SHT_MIPS_OPTIONS)
3682 return new Mips_output_section_options<size, big_endian>(name, type,
3685 return new Output_section(name, type, flags);
3688 // Adjust ELF file header.
3690 do_adjust_elf_header(unsigned char* view, int len);
3692 // Get the custom dynamic tag value.
3694 do_dynamic_tag_custom_value(elfcpp::DT) const;
3696 // Adjust the value written to the dynamic symbol table.
3698 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3700 elfcpp::Sym<size, big_endian> isym(view);
3701 elfcpp::Sym_write<size, big_endian> osym(view);
3702 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3704 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3705 // to treat compressed symbols like any other.
3706 Mips_address value = isym.get_st_value();
3707 if (mips_sym->is_mips16() && value != 0)
3709 if (!mips_sym->has_mips16_fn_stub())
3713 // If we have a MIPS16 function with a stub, the dynamic symbol
3714 // must refer to the stub, since only the stub uses the standard
3715 // calling conventions. Stub contains MIPS32 code, so don't add +1
3718 // There is a code which does this in the method
3719 // Target_mips::do_dynsym_value, but that code will only be
3720 // executed if the symbol is from dynobj.
3721 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3724 Mips16_stub_section<size, big_endian>* fn_stub =
3725 mips_sym->template get_mips16_fn_stub<big_endian>();
3726 value = fn_stub->output_address();
3727 osym.put_st_size(fn_stub->section_size());
3730 osym.put_st_value(value);
3731 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3732 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3734 else if ((mips_sym->is_micromips()
3735 // Stubs are always microMIPS if there is any microMIPS code in
3737 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3740 osym.put_st_value(value | 1);
3741 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3742 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3747 // The class which scans relocations.
3755 get_reference_flags(unsigned int r_type);
3758 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3759 Sized_relobj_file<size, big_endian>* object,
3760 unsigned int data_shndx,
3761 Output_section* output_section,
3762 const Reltype& reloc, unsigned int r_type,
3763 const elfcpp::Sym<size, big_endian>& lsym,
3767 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3768 Sized_relobj_file<size, big_endian>* object,
3769 unsigned int data_shndx,
3770 Output_section* output_section,
3771 const Relatype& reloc, unsigned int r_type,
3772 const elfcpp::Sym<size, big_endian>& lsym,
3776 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3777 Sized_relobj_file<size, big_endian>* object,
3778 unsigned int data_shndx,
3779 Output_section* output_section,
3780 const Relatype* rela,
3782 unsigned int rel_type,
3783 unsigned int r_type,
3784 const elfcpp::Sym<size, big_endian>& lsym,
3788 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3789 Sized_relobj_file<size, big_endian>* object,
3790 unsigned int data_shndx,
3791 Output_section* output_section,
3792 const Reltype& reloc, unsigned int r_type,
3796 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3797 Sized_relobj_file<size, big_endian>* object,
3798 unsigned int data_shndx,
3799 Output_section* output_section,
3800 const Relatype& reloc, unsigned int r_type,
3804 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3805 Sized_relobj_file<size, big_endian>* object,
3806 unsigned int data_shndx,
3807 Output_section* output_section,
3808 const Relatype* rela,
3810 unsigned int rel_type,
3811 unsigned int r_type,
3815 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3817 Sized_relobj_file<size, big_endian>*,
3822 const elfcpp::Sym<size, big_endian>&)
3826 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3828 Sized_relobj_file<size, big_endian>*,
3832 unsigned int, Symbol*)
3836 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3838 Sized_relobj_file<size, big_endian>*,
3843 const elfcpp::Sym<size, big_endian>&)
3847 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3849 Sized_relobj_file<size, big_endian>*,
3853 unsigned int, Symbol*)
3857 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3858 unsigned int r_type);
3861 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3862 unsigned int r_type, Symbol*);
3865 // The class which implements relocation.
3870 : calculated_value_(0), calculate_only_(false)
3876 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3878 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3879 unsigned int r_type,
3880 Output_section* output_section,
3881 Target_mips* target);
3883 // Do a relocation. Return false if the caller should not issue
3884 // any warnings about this relocation.
3886 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3887 Target_mips*, Output_section*, size_t, const unsigned char*,
3888 const Sized_symbol<size>*, const Symbol_value<size>*,
3889 unsigned char*, Mips_address, section_size_type);
3892 // Result of the relocation.
3893 Valtype calculated_value_;
3894 // Whether we have to calculate relocation instead of applying it.
3895 bool calculate_only_;
3898 // This POD class holds the dynamic relocations that should be emitted instead
3899 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3900 // relocations if it turns out that the symbol does not have static
3905 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3906 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3907 Output_section* output_section, Mips_address r_offset)
3908 : sym_(sym), r_type_(r_type), relobj_(relobj),
3909 shndx_(shndx), output_section_(output_section),
3913 // Emit this reloc if appropriate. This is called after we have
3914 // scanned all the relocations, so we know whether the symbol has
3915 // static relocations.
3917 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3918 Symbol_table* symtab)
3920 if (!this->sym_->has_static_relocs())
3922 got->record_global_got_symbol(this->sym_, this->relobj_,
3923 this->r_type_, true, false);
3924 if (!symbol_references_local(this->sym_,
3925 this->sym_->should_add_dynsym_entry(symtab)))
3926 rel_dyn->add_global(this->sym_, this->r_type_,
3927 this->output_section_, this->relobj_,
3928 this->shndx_, this->r_offset_);
3930 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3931 this->output_section_, this->relobj_,
3932 this->shndx_, this->r_offset_);
3937 Mips_symbol<size>* sym_;
3938 unsigned int r_type_;
3939 Mips_relobj<size, big_endian>* relobj_;
3940 unsigned int shndx_;
3941 Output_section* output_section_;
3942 Mips_address r_offset_;
3945 // Adjust TLS relocation type based on the options and whether this
3946 // is a local symbol.
3947 static tls::Tls_optimization
3948 optimize_tls_reloc(bool is_final, int r_type);
3950 // Return whether there is a GOT section.
3952 has_got_section() const
3953 { return this->got_ != NULL; }
3955 // Check whether the given ELF header flags describe a 32-bit binary.
3957 mips_32bit_flags(elfcpp::Elf_Word);
3960 mach_mips3000 = 3000,
3961 mach_mips3900 = 3900,
3962 mach_mips4000 = 4000,
3963 mach_mips4010 = 4010,
3964 mach_mips4100 = 4100,
3965 mach_mips4111 = 4111,
3966 mach_mips4120 = 4120,
3967 mach_mips4300 = 4300,
3968 mach_mips4400 = 4400,
3969 mach_mips4600 = 4600,
3970 mach_mips4650 = 4650,
3971 mach_mips5000 = 5000,
3972 mach_mips5400 = 5400,
3973 mach_mips5500 = 5500,
3974 mach_mips5900 = 5900,
3975 mach_mips6000 = 6000,
3976 mach_mips7000 = 7000,
3977 mach_mips8000 = 8000,
3978 mach_mips9000 = 9000,
3979 mach_mips10000 = 10000,
3980 mach_mips12000 = 12000,
3981 mach_mips14000 = 14000,
3982 mach_mips16000 = 16000,
3985 mach_mips_loongson_2e = 3001,
3986 mach_mips_loongson_2f = 3002,
3987 mach_mips_loongson_3a = 3003,
3988 mach_mips_sb1 = 12310201, // octal 'SB', 01
3989 mach_mips_octeon = 6501,
3990 mach_mips_octeonp = 6601,
3991 mach_mips_octeon2 = 6502,
3992 mach_mips_octeon3 = 6503,
3993 mach_mips_xlr = 887682, // decimal 'XLR'
3994 mach_mipsisa32 = 32,
3995 mach_mipsisa32r2 = 33,
3996 mach_mipsisa32r3 = 34,
3997 mach_mipsisa32r5 = 36,
3998 mach_mipsisa32r6 = 37,
3999 mach_mipsisa64 = 64,
4000 mach_mipsisa64r2 = 65,
4001 mach_mipsisa64r3 = 66,
4002 mach_mipsisa64r5 = 68,
4003 mach_mipsisa64r6 = 69,
4004 mach_mips_micromips = 96
4007 // Return the MACH for a MIPS e_flags value.
4009 elf_mips_mach(elfcpp::Elf_Word);
4011 // Return the MACH for each .MIPS.abiflags ISA Extension.
4013 mips_isa_ext_mach(unsigned int);
4015 // Return the .MIPS.abiflags value representing each ISA Extension.
4017 mips_isa_ext(unsigned int);
4019 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4021 update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4022 Mips_abiflags<big_endian>*);
4024 // Infer the content of the ABI flags based on the elf header.
4026 infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4028 // Create abiflags from elf header or from .MIPS.abiflags section.
4030 create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4032 // Return the meaning of fp_abi, or "unknown" if not known.
4038 select_fp_abi(const std::string&, int, int);
4040 // Merge attributes from input object.
4042 merge_obj_attributes(const std::string&, const Attributes_section_data*);
4044 // Merge abiflags from input object.
4046 merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4048 // Check whether machine EXTENSION is an extension of machine BASE.
4050 mips_mach_extends(unsigned int, unsigned int);
4052 // Merge file header flags from input object.
4054 merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4056 // Encode ISA level and revision as a single value.
4058 level_rev(unsigned char isa_level, unsigned char isa_rev) const
4059 { return (isa_level << 3) | isa_rev; }
4061 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4066 // True if we are linking for CPUs that are faster if JALR is converted to
4067 // BAL. This should be safe for all architectures. We enable this predicate
4073 // True if we are linking for CPUs that are faster if JR is converted to B.
4074 // This should be safe for all architectures. We enable this predicate for
4080 // Return the size of the GOT section.
4084 gold_assert(this->got_ != NULL);
4085 return this->got_->data_size();
4088 // Create a PLT entry for a global symbol referenced by r_type relocation.
4090 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4091 unsigned int r_type);
4093 // Get the PLT section.
4094 Mips_output_data_plt<size, big_endian>*
4097 gold_assert(this->plt_ != NULL);
4101 // Get the GOT PLT section.
4102 const Mips_output_data_plt<size, big_endian>*
4103 got_plt_section() const
4105 gold_assert(this->got_plt_ != NULL);
4106 return this->got_plt_;
4109 // Copy a relocation against a global symbol.
4111 copy_reloc(Symbol_table* symtab, Layout* layout,
4112 Sized_relobj_file<size, big_endian>* object,
4113 unsigned int shndx, Output_section* output_section,
4114 Symbol* sym, unsigned int r_type, Mips_address r_offset)
4116 this->copy_relocs_.copy_reloc(symtab, layout,
4117 symtab->get_sized_symbol<size>(sym),
4118 object, shndx, output_section,
4119 r_type, r_offset, 0,
4120 this->rel_dyn_section(layout));
4124 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4125 Mips_relobj<size, big_endian>* relobj,
4126 unsigned int shndx, Output_section* output_section,
4127 Mips_address r_offset)
4129 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4130 output_section, r_offset));
4133 // Calculate value of _gp symbol.
4135 set_gp(Layout*, Symbol_table*);
4138 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4140 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4142 // Adds entries that describe how machines relate to one another. The entries
4143 // are ordered topologically with MIPS I extensions listed last. First
4144 // element is extension, second element is base.
4146 add_machine_extensions()
4148 // MIPS64r2 extensions.
4149 this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4150 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4151 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4152 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4153 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64r2);
4155 // MIPS64 extensions.
4156 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4157 this->add_extension(mach_mips_sb1, mach_mipsisa64);
4158 this->add_extension(mach_mips_xlr, mach_mipsisa64);
4160 // MIPS V extensions.
4161 this->add_extension(mach_mipsisa64, mach_mips5);
4163 // R10000 extensions.
4164 this->add_extension(mach_mips12000, mach_mips10000);
4165 this->add_extension(mach_mips14000, mach_mips10000);
4166 this->add_extension(mach_mips16000, mach_mips10000);
4168 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
4169 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
4170 // better to allow vr5400 and vr5500 code to be merged anyway, since
4171 // many libraries will just use the core ISA. Perhaps we could add
4172 // some sort of ASE flag if this ever proves a problem.
4173 this->add_extension(mach_mips5500, mach_mips5400);
4174 this->add_extension(mach_mips5400, mach_mips5000);
4176 // MIPS IV extensions.
4177 this->add_extension(mach_mips5, mach_mips8000);
4178 this->add_extension(mach_mips10000, mach_mips8000);
4179 this->add_extension(mach_mips5000, mach_mips8000);
4180 this->add_extension(mach_mips7000, mach_mips8000);
4181 this->add_extension(mach_mips9000, mach_mips8000);
4183 // VR4100 extensions.
4184 this->add_extension(mach_mips4120, mach_mips4100);
4185 this->add_extension(mach_mips4111, mach_mips4100);
4187 // MIPS III extensions.
4188 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4189 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4190 this->add_extension(mach_mips8000, mach_mips4000);
4191 this->add_extension(mach_mips4650, mach_mips4000);
4192 this->add_extension(mach_mips4600, mach_mips4000);
4193 this->add_extension(mach_mips4400, mach_mips4000);
4194 this->add_extension(mach_mips4300, mach_mips4000);
4195 this->add_extension(mach_mips4100, mach_mips4000);
4196 this->add_extension(mach_mips4010, mach_mips4000);
4197 this->add_extension(mach_mips5900, mach_mips4000);
4199 // MIPS32 extensions.
4200 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4202 // MIPS II extensions.
4203 this->add_extension(mach_mips4000, mach_mips6000);
4204 this->add_extension(mach_mipsisa32, mach_mips6000);
4206 // MIPS I extensions.
4207 this->add_extension(mach_mips6000, mach_mips3000);
4208 this->add_extension(mach_mips3900, mach_mips3000);
4211 // Add value to MIPS extenstions.
4213 add_extension(unsigned int base, unsigned int extension)
4215 std::pair<unsigned int, unsigned int> ext(base, extension);
4216 this->mips_mach_extensions_.push_back(ext);
4219 // Return the number of entries in the .dynsym section.
4220 unsigned int get_dt_mips_symtabno() const
4222 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4223 / elfcpp::Elf_sizes<size>::sym_size));
4224 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4227 // Information about this specific target which we pass to the
4228 // general Target structure.
4229 static const Target::Target_info mips_info;
4231 Mips_output_data_got<size, big_endian>* got_;
4232 // gp symbol. It has the value of .got + 0x7FF0.
4233 Sized_symbol<size>* gp_;
4235 Mips_output_data_plt<size, big_endian>* plt_;
4236 // The GOT PLT section.
4237 Output_data_space* got_plt_;
4238 // The dynamic reloc section.
4239 Reloc_section* rel_dyn_;
4240 // The .rld_map section.
4241 Output_data_zero_fill* rld_map_;
4242 // Relocs saved to avoid a COPY reloc.
4243 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4245 // A list of dyn relocs to be saved.
4246 std::vector<Dyn_reloc> dyn_relocs_;
4248 // The LA25 stub section.
4249 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4250 // Architecture extensions.
4251 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4253 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4255 // Attributes section data in output.
4256 Attributes_section_data* attributes_section_data_;
4257 // .MIPS.abiflags section data in output.
4258 Mips_abiflags<big_endian>* abiflags_;
4263 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4265 // Whether there is an input .MIPS.abiflags section.
4266 bool has_abiflags_section_;
4268 // Whether the entry symbol is mips16 or micromips.
4269 bool entry_symbol_is_compressed_;
4271 // Whether we can use only 32-bit microMIPS instructions.
4272 // TODO(sasa): This should be a linker option.
4276 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4277 // It records high part of the relocation pair.
4279 template<int size, bool big_endian>
4282 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4284 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4285 const Symbol_value<size>* _psymval, Mips_address _addend,
4286 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4287 Mips_address _address = 0, bool _gp_disp = false)
4288 : view(_view), object(_object), psymval(_psymval), addend(_addend),
4289 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4290 address(_address), gp_disp(_gp_disp)
4293 unsigned char* view;
4294 const Mips_relobj<size, big_endian>* object;
4295 const Symbol_value<size>* psymval;
4296 Mips_address addend;
4297 unsigned int r_type;
4299 bool extract_addend;
4300 Mips_address address;
4304 template<int size, bool big_endian>
4305 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4307 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4308 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4309 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4310 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4311 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4316 STATUS_OKAY, // No error during relocation.
4317 STATUS_OVERFLOW, // Relocation overflow.
4318 STATUS_BAD_RELOC, // Relocation cannot be applied.
4319 STATUS_PCREL_UNALIGNED // Unaligned PC-relative relocation.
4323 typedef Relocate_functions<size, big_endian> Base;
4324 typedef Mips_relocate_functions<size, big_endian> This;
4326 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4327 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4328 static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
4330 template<int valsize>
4331 static inline typename This::Status
4332 check_overflow(Valtype value)
4335 return (Bits<valsize>::has_overflow32(value)
4336 ? This::STATUS_OVERFLOW
4337 : This::STATUS_OKAY);
4339 return (Bits<valsize>::has_overflow(value)
4340 ? This::STATUS_OVERFLOW
4341 : This::STATUS_OKAY);
4345 should_shuffle_micromips_reloc(unsigned int r_type)
4347 return (micromips_reloc(r_type)
4348 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4349 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4353 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4354 // Most mips16 instructions are 16 bits, but these instructions
4357 // The format of these instructions is:
4359 // +--------------+--------------------------------+
4360 // | JALX | X| Imm 20:16 | Imm 25:21 |
4361 // +--------------+--------------------------------+
4362 // | Immediate 15:0 |
4363 // +-----------------------------------------------+
4365 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4366 // Note that the immediate value in the first word is swapped.
4368 // When producing a relocatable object file, R_MIPS16_26 is
4369 // handled mostly like R_MIPS_26. In particular, the addend is
4370 // stored as a straight 26-bit value in a 32-bit instruction.
4371 // (gas makes life simpler for itself by never adjusting a
4372 // R_MIPS16_26 reloc to be against a section, so the addend is
4373 // always zero). However, the 32 bit instruction is stored as 2
4374 // 16-bit values, rather than a single 32-bit value. In a
4375 // big-endian file, the result is the same; in a little-endian
4376 // file, the two 16-bit halves of the 32 bit value are swapped.
4377 // This is so that a disassembler can recognize the jal
4380 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4381 // instruction stored as two 16-bit values. The addend A is the
4382 // contents of the targ26 field. The calculation is the same as
4383 // R_MIPS_26. When storing the calculated value, reorder the
4384 // immediate value as shown above, and don't forget to store the
4385 // value as two 16-bit values.
4387 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4391 // +--------+----------------------+
4395 // +--------+----------------------+
4398 // +----------+------+-------------+
4400 // | sub1 | | sub2 |
4401 // |0 9|10 15|16 31|
4402 // +----------+--------------------+
4403 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4404 // ((sub1 << 16) | sub2)).
4406 // When producing a relocatable object file, the calculation is
4407 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4408 // When producing a fully linked file, the calculation is
4409 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4410 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4412 // The table below lists the other MIPS16 instruction relocations.
4413 // Each one is calculated in the same way as the non-MIPS16 relocation
4414 // given on the right, but using the extended MIPS16 layout of 16-bit
4415 // immediate fields:
4417 // R_MIPS16_GPREL R_MIPS_GPREL16
4418 // R_MIPS16_GOT16 R_MIPS_GOT16
4419 // R_MIPS16_CALL16 R_MIPS_CALL16
4420 // R_MIPS16_HI16 R_MIPS_HI16
4421 // R_MIPS16_LO16 R_MIPS_LO16
4423 // A typical instruction will have a format like this:
4425 // +--------------+--------------------------------+
4426 // | EXTEND | Imm 10:5 | Imm 15:11 |
4427 // +--------------+--------------------------------+
4428 // | Major | rx | ry | Imm 4:0 |
4429 // +--------------+--------------------------------+
4431 // EXTEND is the five bit value 11110. Major is the instruction
4434 // All we need to do here is shuffle the bits appropriately.
4435 // As above, the two 16-bit halves must be swapped on a
4436 // little-endian system.
4438 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4439 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4440 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4443 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4446 if (!mips16_reloc(r_type)
4447 && !should_shuffle_micromips_reloc(r_type))
4450 // Pick up the first and second halfwords of the instruction.
4451 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4452 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4455 if (micromips_reloc(r_type)
4456 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4457 val = first << 16 | second;
4458 else if (r_type != elfcpp::R_MIPS16_26)
4459 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4460 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4462 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4463 | ((first & 0x1f) << 21) | second);
4465 elfcpp::Swap<32, big_endian>::writeval(view, val);
4469 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4471 if (!mips16_reloc(r_type)
4472 && !should_shuffle_micromips_reloc(r_type))
4475 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4476 Valtype16 first, second;
4478 if (micromips_reloc(r_type)
4479 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4481 second = val & 0xffff;
4484 else if (r_type != elfcpp::R_MIPS16_26)
4486 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4487 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4491 second = val & 0xffff;
4492 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4493 | ((val >> 21) & 0x1f);
4496 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4497 elfcpp::Swap<16, big_endian>::writeval(view, first);
4500 // R_MIPS_16: S + sign-extend(A)
4501 static inline typename This::Status
4502 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4503 const Symbol_value<size>* psymval, Mips_address addend_a,
4504 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4506 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4507 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4509 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4512 Valtype x = psymval->value(object, addend);
4513 val = Bits<16>::bit_select32(val, x, 0xffffU);
4517 *calculated_value = x;
4518 return This::STATUS_OKAY;
4521 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4523 return check_overflow<16>(x);
4527 static inline typename This::Status
4528 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4529 const Symbol_value<size>* psymval, Mips_address addend_a,
4530 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4532 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4533 Valtype addend = (extract_addend
4534 ? elfcpp::Swap<32, big_endian>::readval(wv)
4536 Valtype x = psymval->value(object, addend);
4539 *calculated_value = x;
4541 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4543 return This::STATUS_OKAY;
4546 // R_MIPS_JALR, R_MICROMIPS_JALR
4547 static inline typename This::Status
4548 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4549 const Symbol_value<size>* psymval, Mips_address address,
4550 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4551 unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4552 bool calculate_only, Valtype* calculated_value)
4554 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4555 Valtype addend = extract_addend ? 0 : addend_a;
4556 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4558 // Try converting J(AL)R to B(AL), if the target is in range.
4559 if (r_type == elfcpp::R_MIPS_JALR
4561 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4562 || (jr_to_b && val == 0x03200008))) // jr t9
4564 int offset = psymval->value(object, addend) - (address + 4);
4565 if (!Bits<18>::has_overflow32(offset))
4567 if (val == 0x03200008) // jr t9
4568 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4570 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4575 *calculated_value = val;
4577 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4579 return This::STATUS_OKAY;
4582 // R_MIPS_PC32: S + A - P
4583 static inline typename This::Status
4584 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4585 const Symbol_value<size>* psymval, Mips_address address,
4586 Mips_address addend_a, bool extract_addend, bool calculate_only,
4587 Valtype* calculated_value)
4589 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4590 Valtype addend = (extract_addend
4591 ? elfcpp::Swap<32, big_endian>::readval(wv)
4593 Valtype x = psymval->value(object, addend) - address;
4596 *calculated_value = x;
4598 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4600 return This::STATUS_OKAY;
4603 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4604 static inline typename This::Status
4605 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4606 const Symbol_value<size>* psymval, Mips_address address,
4607 bool local, Mips_address addend_a, bool extract_addend,
4608 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4609 bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4611 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4612 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4617 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4618 addend = (val & 0x03ffffff) << 1;
4620 addend = (val & 0x03ffffff) << 2;
4625 // Make sure the target of JALX is word-aligned. Bit 0 must be
4626 // the correct ISA mode selector and bit 1 must be 0.
4627 if (!calculate_only && cross_mode_jump
4628 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4630 gold_warning(_("JALX to a non-word-aligned address"));
4631 return This::STATUS_BAD_RELOC;
4634 // Shift is 2, unusually, for microMIPS JALX.
4635 unsigned int shift =
4636 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4640 x = addend | ((address + 4) & (0xfc000000 << shift));
4644 x = Bits<27>::sign_extend32(addend);
4646 x = Bits<28>::sign_extend32(addend);
4648 x = psymval->value(object, x) >> shift;
4650 if (!calculate_only && !local && !gsym->is_weak_undefined()
4651 && ((x >> 26) != ((address + 4) >> (26 + shift))))
4652 return This::STATUS_OVERFLOW;
4654 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4656 // If required, turn JAL into JALX.
4657 if (cross_mode_jump)
4660 Valtype32 opcode = val >> 26;
4661 Valtype32 jalx_opcode;
4663 // Check to see if the opcode is already JAL or JALX.
4664 if (r_type == elfcpp::R_MIPS16_26)
4666 ok = (opcode == 0x6) || (opcode == 0x7);
4669 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4671 ok = (opcode == 0x3d) || (opcode == 0x3c);
4676 ok = (opcode == 0x3) || (opcode == 0x1d);
4680 // If the opcode is not JAL or JALX, there's a problem. We cannot
4681 // convert J or JALS to JALX.
4682 if (!calculate_only && !ok)
4684 gold_error(_("Unsupported jump between ISA modes; consider "
4685 "recompiling with interlinking enabled."));
4686 return This::STATUS_BAD_RELOC;
4689 // Make this the JALX opcode.
4690 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4693 // Try converting JAL to BAL, if the target is in range.
4694 if (!parameters->options().relocatable()
4697 && r_type == elfcpp::R_MIPS_26
4698 && (val >> 26) == 0x3))) // jal addr
4700 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4701 int offset = dest - (address + 4);
4702 if (!Bits<18>::has_overflow32(offset))
4704 if (val == 0x03200008) // jr t9
4705 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4707 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4712 *calculated_value = val;
4714 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4716 return This::STATUS_OKAY;
4720 static inline typename This::Status
4721 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4722 const Symbol_value<size>* psymval, Mips_address address,
4723 Mips_address addend_a, bool extract_addend, bool calculate_only,
4724 Valtype* calculated_value)
4726 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4727 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4729 Valtype addend = (extract_addend
4730 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4733 Valtype x = psymval->value(object, addend) - address;
4734 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4738 *calculated_value = x >> 2;
4739 return This::STATUS_OKAY;
4742 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4744 if (psymval->value(object, addend) & 3)
4745 return This::STATUS_PCREL_UNALIGNED;
4747 return check_overflow<18>(x);
4751 static inline typename This::Status
4752 relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4753 const Symbol_value<size>* psymval, Mips_address address,
4754 Mips_address addend_a, bool extract_addend, bool calculate_only,
4755 Valtype* calculated_value)
4757 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4758 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4760 Valtype addend = (extract_addend
4761 ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4764 Valtype x = psymval->value(object, addend) - address;
4765 val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4769 *calculated_value = x >> 2;
4770 return This::STATUS_OKAY;
4773 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4775 if (psymval->value(object, addend) & 3)
4776 return This::STATUS_PCREL_UNALIGNED;
4778 return check_overflow<23>(x);
4782 static inline typename This::Status
4783 relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4784 const Symbol_value<size>* psymval, Mips_address address,
4785 Mips_address addend_a, bool extract_addend, bool calculate_only,
4786 Valtype* calculated_value)
4788 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4789 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4791 Valtype addend = (extract_addend
4792 ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4795 Valtype x = psymval->value(object, addend) - address;
4796 val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4800 *calculated_value = x >> 2;
4801 return This::STATUS_OKAY;
4804 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4806 if (psymval->value(object, addend) & 3)
4807 return This::STATUS_PCREL_UNALIGNED;
4809 return check_overflow<28>(x);
4813 static inline typename This::Status
4814 relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4815 const Symbol_value<size>* psymval, Mips_address address,
4816 Mips_address addend_a, bool extract_addend, bool calculate_only,
4817 Valtype* calculated_value)
4819 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4820 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4822 Valtype addend = (extract_addend
4823 ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4826 Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4827 val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4831 *calculated_value = x >> 3;
4832 return This::STATUS_OKAY;
4835 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4837 if (psymval->value(object, addend) & 7)
4838 return This::STATUS_PCREL_UNALIGNED;
4840 return check_overflow<21>(x);
4844 static inline typename This::Status
4845 relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4846 const Symbol_value<size>* psymval, Mips_address address,
4847 Mips_address addend_a, bool extract_addend, bool calculate_only,
4848 Valtype* calculated_value)
4850 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4851 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4853 Valtype addend = (extract_addend
4854 ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4857 Valtype x = psymval->value(object, addend) - address;
4858 val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4862 *calculated_value = x >> 2;
4863 return This::STATUS_OKAY;
4866 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4868 if (psymval->value(object, addend) & 3)
4869 return This::STATUS_PCREL_UNALIGNED;
4871 return check_overflow<21>(x);
4875 static inline typename This::Status
4876 relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4877 const Symbol_value<size>* psymval, Mips_address addend,
4878 Mips_address address, unsigned int r_sym, bool extract_addend)
4880 // Record the relocation. It will be resolved when we find pclo16 part.
4881 pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4882 addend, 0, r_sym, extract_addend, address));
4883 return This::STATUS_OKAY;
4887 static inline typename This::Status
4888 do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4889 const Symbol_value<size>* psymval, Mips_address addend_hi,
4890 Mips_address address, bool extract_addend, Valtype32 addend_lo,
4891 bool calculate_only, Valtype* calculated_value)
4893 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4894 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4896 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4899 Valtype value = psymval->value(object, addend) - address;
4900 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4901 val = Bits<32>::bit_select32(val, x, 0xffff);
4904 *calculated_value = x;
4906 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4908 return This::STATUS_OKAY;
4912 static inline typename This::Status
4913 relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4914 const Symbol_value<size>* psymval, Mips_address addend_a,
4915 bool extract_addend, Mips_address address, unsigned int r_sym,
4916 unsigned int rel_type, bool calculate_only,
4917 Valtype* calculated_value)
4919 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4920 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4922 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4925 if (rel_type == elfcpp::SHT_REL)
4927 // Resolve pending R_MIPS_PCHI16 relocations.
4928 typename std::list<reloc_high<size, big_endian> >::iterator it =
4929 pchi16_relocs.begin();
4930 while (it != pchi16_relocs.end())
4932 reloc_high<size, big_endian> pchi16 = *it;
4933 if (pchi16.r_sym == r_sym)
4935 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4936 pchi16.addend, pchi16.address,
4937 pchi16.extract_addend, addend, calculate_only,
4939 it = pchi16_relocs.erase(it);
4946 // Resolve R_MIPS_PCLO16 relocation.
4947 Valtype x = psymval->value(object, addend) - address;
4948 val = Bits<32>::bit_select32(val, x, 0xffff);
4951 *calculated_value = x;
4953 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4955 return This::STATUS_OKAY;
4958 // R_MICROMIPS_PC7_S1
4959 static inline typename This::Status
4960 relmicromips_pc7_s1(unsigned char* view,
4961 const Mips_relobj<size, big_endian>* object,
4962 const Symbol_value<size>* psymval, Mips_address address,
4963 Mips_address addend_a, bool extract_addend,
4964 bool calculate_only, Valtype* calculated_value)
4966 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4967 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4969 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4972 Valtype x = psymval->value(object, addend) - address;
4973 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4977 *calculated_value = x >> 1;
4978 return This::STATUS_OKAY;
4981 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4983 return check_overflow<8>(x);
4986 // R_MICROMIPS_PC10_S1
4987 static inline typename This::Status
4988 relmicromips_pc10_s1(unsigned char* view,
4989 const Mips_relobj<size, big_endian>* object,
4990 const Symbol_value<size>* psymval, Mips_address address,
4991 Mips_address addend_a, bool extract_addend,
4992 bool calculate_only, Valtype* calculated_value)
4994 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4995 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4997 Valtype addend = (extract_addend
4998 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
5001 Valtype x = psymval->value(object, addend) - address;
5002 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
5006 *calculated_value = x >> 1;
5007 return This::STATUS_OKAY;
5010 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5012 return check_overflow<11>(x);
5015 // R_MICROMIPS_PC16_S1
5016 static inline typename This::Status
5017 relmicromips_pc16_s1(unsigned char* view,
5018 const Mips_relobj<size, big_endian>* object,
5019 const Symbol_value<size>* psymval, Mips_address address,
5020 Mips_address addend_a, bool extract_addend,
5021 bool calculate_only, Valtype* calculated_value)
5023 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5024 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5026 Valtype addend = (extract_addend
5027 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
5030 Valtype x = psymval->value(object, addend) - address;
5031 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
5035 *calculated_value = x >> 1;
5036 return This::STATUS_OKAY;
5039 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5041 return check_overflow<17>(x);
5044 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5045 static inline typename This::Status
5046 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5047 const Symbol_value<size>* psymval, Mips_address addend,
5048 Mips_address address, bool gp_disp, unsigned int r_type,
5049 unsigned int r_sym, bool extract_addend)
5051 // Record the relocation. It will be resolved when we find lo16 part.
5052 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5053 addend, r_type, r_sym, extract_addend, address,
5055 return This::STATUS_OKAY;
5058 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5059 static inline typename This::Status
5060 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5061 const Symbol_value<size>* psymval, Mips_address addend_hi,
5062 Mips_address address, bool is_gp_disp, unsigned int r_type,
5063 bool extract_addend, Valtype32 addend_lo,
5064 Target_mips<size, big_endian>* target, bool calculate_only,
5065 Valtype* calculated_value)
5067 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5068 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5070 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5075 value = psymval->value(object, addend);
5078 // For MIPS16 ABI code we generate this sequence
5079 // 0: li $v0,%hi(_gp_disp)
5080 // 4: addiupc $v1,%lo(_gp_disp)
5084 // So the offsets of hi and lo relocs are the same, but the
5085 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5086 // ADDIUPC clears the low two bits of the instruction address,
5087 // so the base is ($t9 + 4) & ~3.
5089 if (r_type == elfcpp::R_MIPS16_HI16)
5090 gp_disp = (target->adjusted_gp_value(object)
5091 - ((address + 4) & ~0x3));
5092 // The microMIPS .cpload sequence uses the same assembly
5093 // instructions as the traditional psABI version, but the
5094 // incoming $t9 has the low bit set.
5095 else if (r_type == elfcpp::R_MICROMIPS_HI16)
5096 gp_disp = target->adjusted_gp_value(object) - address - 1;
5098 gp_disp = target->adjusted_gp_value(object) - address;
5099 value = gp_disp + addend;
5101 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
5102 val = Bits<32>::bit_select32(val, x, 0xffff);
5106 *calculated_value = x;
5107 return This::STATUS_OKAY;
5110 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5112 return (is_gp_disp ? check_overflow<16>(x)
5113 : This::STATUS_OKAY);
5116 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5117 static inline typename This::Status
5118 relgot16_local(unsigned char* view,
5119 const Mips_relobj<size, big_endian>* object,
5120 const Symbol_value<size>* psymval, Mips_address addend_a,
5121 bool extract_addend, unsigned int r_type, unsigned int r_sym)
5123 // Record the relocation. It will be resolved when we find lo16 part.
5124 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5125 addend_a, r_type, r_sym, extract_addend));
5126 return This::STATUS_OKAY;
5129 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5130 static inline typename This::Status
5131 do_relgot16_local(unsigned char* view,
5132 const Mips_relobj<size, big_endian>* object,
5133 const Symbol_value<size>* psymval, Mips_address addend_hi,
5134 bool extract_addend, Valtype32 addend_lo,
5135 Target_mips<size, big_endian>* target, bool calculate_only,
5136 Valtype* calculated_value)
5138 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5139 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5141 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5144 // Find GOT page entry.
5145 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5148 unsigned int got_offset =
5149 target->got_section()->get_got_page_offset(value, object);
5151 // Resolve the relocation.
5152 Valtype x = target->got_section()->gp_offset(got_offset, object);
5153 val = Bits<32>::bit_select32(val, x, 0xffff);
5157 *calculated_value = x;
5158 return This::STATUS_OKAY;
5161 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5163 return check_overflow<16>(x);
5166 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5167 static inline typename This::Status
5168 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
5169 const Mips_relobj<size, big_endian>* object,
5170 const Symbol_value<size>* psymval, Mips_address addend_a,
5171 bool extract_addend, Mips_address address, bool is_gp_disp,
5172 unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
5173 bool calculate_only, Valtype* calculated_value)
5175 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5176 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5178 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5181 if (rel_type == elfcpp::SHT_REL)
5183 typename This::Status reloc_status = This::STATUS_OKAY;
5184 // Resolve pending R_MIPS_HI16 relocations.
5185 typename std::list<reloc_high<size, big_endian> >::iterator it =
5186 hi16_relocs.begin();
5187 while (it != hi16_relocs.end())
5189 reloc_high<size, big_endian> hi16 = *it;
5190 if (hi16.r_sym == r_sym
5191 && is_matching_lo16_reloc(hi16.r_type, r_type))
5193 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
5194 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
5195 hi16.addend, hi16.address, hi16.gp_disp,
5196 hi16.r_type, hi16.extract_addend, addend,
5197 target, calculate_only, calculated_value);
5198 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
5199 if (reloc_status == This::STATUS_OVERFLOW)
5200 return This::STATUS_OVERFLOW;
5201 it = hi16_relocs.erase(it);
5207 // Resolve pending local R_MIPS_GOT16 relocations.
5208 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
5209 got16_relocs.begin();
5210 while (it2 != got16_relocs.end())
5212 reloc_high<size, big_endian> got16 = *it2;
5213 if (got16.r_sym == r_sym
5214 && is_matching_lo16_reloc(got16.r_type, r_type))
5216 mips_reloc_unshuffle(got16.view, got16.r_type, false);
5218 reloc_status = do_relgot16_local(got16.view, got16.object,
5219 got16.psymval, got16.addend,
5220 got16.extract_addend, addend, target,
5221 calculate_only, calculated_value);
5223 mips_reloc_shuffle(got16.view, got16.r_type, false);
5224 if (reloc_status == This::STATUS_OVERFLOW)
5225 return This::STATUS_OVERFLOW;
5226 it2 = got16_relocs.erase(it2);
5233 // Resolve R_MIPS_LO16 relocation.
5236 x = psymval->value(object, addend);
5239 // See the comment for R_MIPS16_HI16 above for the reason
5240 // for this conditional.
5242 if (r_type == elfcpp::R_MIPS16_LO16)
5243 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
5244 else if (r_type == elfcpp::R_MICROMIPS_LO16
5245 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
5246 gp_disp = target->adjusted_gp_value(object) - address + 3;
5248 gp_disp = target->adjusted_gp_value(object) - address + 4;
5249 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5250 // for overflow. Relocations against _gp_disp are normally
5251 // generated from the .cpload pseudo-op. It generates code
5252 // that normally looks like this:
5254 // lui $gp,%hi(_gp_disp)
5255 // addiu $gp,$gp,%lo(_gp_disp)
5258 // Here $t9 holds the address of the function being called,
5259 // as required by the MIPS ELF ABI. The R_MIPS_LO16
5260 // relocation can easily overflow in this situation, but the
5261 // R_MIPS_HI16 relocation will handle the overflow.
5262 // Therefore, we consider this a bug in the MIPS ABI, and do
5263 // not check for overflow here.
5264 x = gp_disp + addend;
5266 val = Bits<32>::bit_select32(val, x, 0xffff);
5269 *calculated_value = x;
5271 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5273 return This::STATUS_OKAY;
5276 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5277 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5278 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5279 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5280 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5281 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5282 static inline typename This::Status
5283 relgot(unsigned char* view, int gp_offset, bool calculate_only,
5284 Valtype* calculated_value)
5286 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5287 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5288 Valtype x = gp_offset;
5289 val = Bits<32>::bit_select32(val, x, 0xffff);
5293 *calculated_value = x;
5294 return This::STATUS_OKAY;
5297 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5299 return check_overflow<16>(x);
5303 static inline typename This::Status
5304 releh(unsigned char* view, int gp_offset, bool calculate_only,
5305 Valtype* calculated_value)
5307 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5308 Valtype x = gp_offset;
5312 *calculated_value = x;
5313 return This::STATUS_OKAY;
5316 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5318 return check_overflow<32>(x);
5321 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5322 static inline typename This::Status
5323 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5324 const Mips_relobj<size, big_endian>* object,
5325 const Symbol_value<size>* psymval, Mips_address addend_a,
5326 bool extract_addend, bool calculate_only,
5327 Valtype* calculated_value)
5329 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5330 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5331 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5333 // Find a GOT page entry that points to within 32KB of symbol + addend.
5334 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5335 unsigned int got_offset =
5336 target->got_section()->get_got_page_offset(value, object);
5338 Valtype x = target->got_section()->gp_offset(got_offset, object);
5339 val = Bits<32>::bit_select32(val, x, 0xffff);
5343 *calculated_value = x;
5344 return This::STATUS_OKAY;
5347 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5349 return check_overflow<16>(x);
5352 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5353 static inline typename This::Status
5354 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5355 const Mips_relobj<size, big_endian>* object,
5356 const Symbol_value<size>* psymval, Mips_address addend_a,
5357 bool extract_addend, bool local, bool calculate_only,
5358 Valtype* calculated_value)
5360 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5361 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5362 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5364 // For a local symbol, find a GOT page entry that points to within 32KB of
5365 // symbol + addend. Relocation value is the offset of the GOT page entry's
5366 // value from symbol + addend.
5367 // For a global symbol, relocation value is addend.
5371 // Find GOT page entry.
5372 Mips_address value = ((psymval->value(object, addend) + 0x8000)
5374 target->got_section()->get_got_page_offset(value, object);
5376 x = psymval->value(object, addend) - value;
5380 val = Bits<32>::bit_select32(val, x, 0xffff);
5384 *calculated_value = x;
5385 return This::STATUS_OKAY;
5388 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5390 return check_overflow<16>(x);
5393 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5394 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5395 static inline typename This::Status
5396 relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5397 Valtype* calculated_value)
5399 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5400 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5401 Valtype x = gp_offset;
5402 x = ((x + 0x8000) >> 16) & 0xffff;
5403 val = Bits<32>::bit_select32(val, x, 0xffff);
5406 *calculated_value = x;
5408 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5410 return This::STATUS_OKAY;
5413 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5414 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5415 static inline typename This::Status
5416 relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5417 Valtype* calculated_value)
5419 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5420 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5421 Valtype x = gp_offset;
5422 val = Bits<32>::bit_select32(val, x, 0xffff);
5425 *calculated_value = x;
5427 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5429 return This::STATUS_OKAY;
5432 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5433 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5434 static inline typename This::Status
5435 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5436 const Symbol_value<size>* psymval, Mips_address gp,
5437 Mips_address addend_a, bool extract_addend, bool local,
5438 unsigned int r_type, bool calculate_only,
5439 Valtype* calculated_value)
5441 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5442 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5447 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5448 addend = (val & 0x7f) << 2;
5450 addend = val & 0xffff;
5451 // Only sign-extend the addend if it was extracted from the
5452 // instruction. If the addend was separate, leave it alone,
5453 // otherwise we may lose significant bits.
5454 addend = Bits<16>::sign_extend32(addend);
5459 Valtype x = psymval->value(object, addend) - gp;
5461 // If the symbol was local, any earlier relocatable links will
5462 // have adjusted its addend with the gp offset, so compensate
5463 // for that now. Don't do it for symbols forced local in this
5464 // link, though, since they won't have had the gp offset applied
5467 x += object->gp_value();
5469 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5470 val = Bits<32>::bit_select32(val, x, 0x7f);
5472 val = Bits<32>::bit_select32(val, x, 0xffff);
5476 *calculated_value = x;
5477 return This::STATUS_OKAY;
5480 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5482 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5484 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5485 "limit (see option -G)"));
5486 return This::STATUS_OVERFLOW;
5488 return This::STATUS_OKAY;
5492 static inline typename This::Status
5493 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5494 const Symbol_value<size>* psymval, Mips_address gp,
5495 Mips_address addend_a, bool extract_addend, bool calculate_only,
5496 Valtype* calculated_value)
5498 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5499 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5500 Valtype addend = extract_addend ? val : addend_a;
5502 // R_MIPS_GPREL32 relocations are defined for local symbols only.
5503 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5506 *calculated_value = x;
5508 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5510 return This::STATUS_OKAY;
5513 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5514 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5515 // R_MICROMIPS_TLS_DTPREL_HI16
5516 static inline typename This::Status
5517 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5518 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5519 Mips_address addend_a, bool extract_addend, bool calculate_only,
5520 Valtype* calculated_value)
5522 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5523 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5524 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5526 // tls symbol values are relative to tls_segment()->vaddr()
5527 Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5528 val = Bits<32>::bit_select32(val, x, 0xffff);
5531 *calculated_value = x;
5533 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5535 return This::STATUS_OKAY;
5538 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5539 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5540 // R_MICROMIPS_TLS_DTPREL_LO16,
5541 static inline typename This::Status
5542 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5543 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5544 Mips_address addend_a, bool extract_addend, bool calculate_only,
5545 Valtype* calculated_value)
5547 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5548 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5549 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5551 // tls symbol values are relative to tls_segment()->vaddr()
5552 Valtype x = psymval->value(object, addend) - tp_offset;
5553 val = Bits<32>::bit_select32(val, x, 0xffff);
5556 *calculated_value = x;
5558 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5560 return This::STATUS_OKAY;
5563 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5564 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5565 static inline typename This::Status
5566 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5567 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5568 Mips_address addend_a, bool extract_addend, bool calculate_only,
5569 Valtype* calculated_value)
5571 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5572 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5573 Valtype addend = extract_addend ? val : addend_a;
5575 // tls symbol values are relative to tls_segment()->vaddr()
5576 Valtype x = psymval->value(object, addend) - tp_offset;
5579 *calculated_value = x;
5581 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5583 return This::STATUS_OKAY;
5586 // R_MIPS_SUB, R_MICROMIPS_SUB
5587 static inline typename This::Status
5588 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5589 const Symbol_value<size>* psymval, Mips_address addend_a,
5590 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5592 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5593 Valtype64 addend = (extract_addend
5594 ? elfcpp::Swap<64, big_endian>::readval(wv)
5597 Valtype64 x = psymval->value(object, -addend);
5599 *calculated_value = x;
5601 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5603 return This::STATUS_OKAY;
5607 static inline typename This::Status
5608 rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5609 const Symbol_value<size>* psymval, Mips_address addend_a,
5610 bool extract_addend, bool calculate_only, Valtype* calculated_value,
5611 bool apply_addend_only)
5613 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5614 Valtype64 addend = (extract_addend
5615 ? elfcpp::Swap<64, big_endian>::readval(wv)
5618 Valtype64 x = psymval->value(object, addend);
5620 *calculated_value = x;
5623 if (apply_addend_only)
5625 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5628 return This::STATUS_OKAY;
5631 // R_MIPS_HIGHER, R_MICROMIPS_HIGHER
5632 static inline typename This::Status
5633 relhigher(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5634 const Symbol_value<size>* psymval, Mips_address addend_a,
5635 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5637 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5638 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5639 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5642 Valtype x = psymval->value(object, addend);
5643 x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
5644 val = Bits<32>::bit_select32(val, x, 0xffff);
5647 *calculated_value = x;
5649 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5651 return This::STATUS_OKAY;
5654 // R_MIPS_HIGHEST, R_MICROMIPS_HIGHEST
5655 static inline typename This::Status
5656 relhighest(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5657 const Symbol_value<size>* psymval, Mips_address addend_a,
5658 bool extract_addend, bool calculate_only,
5659 Valtype* calculated_value)
5661 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5662 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5663 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5666 Valtype x = psymval->value(object, addend);
5667 x = ((x + (uint64_t) 0x800080008000llu) >> 48) & 0xffff;
5668 val = Bits<32>::bit_select32(val, x, 0xffff);
5671 *calculated_value = x;
5673 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5675 return This::STATUS_OKAY;
5679 template<int size, bool big_endian>
5680 typename std::list<reloc_high<size, big_endian> >
5681 Mips_relocate_functions<size, big_endian>::hi16_relocs;
5683 template<int size, bool big_endian>
5684 typename std::list<reloc_high<size, big_endian> >
5685 Mips_relocate_functions<size, big_endian>::got16_relocs;
5687 template<int size, bool big_endian>
5688 typename std::list<reloc_high<size, big_endian> >
5689 Mips_relocate_functions<size, big_endian>::pchi16_relocs;
5691 // Mips_got_info methods.
5693 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5694 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5696 template<int size, bool big_endian>
5698 Mips_got_info<size, big_endian>::record_local_got_symbol(
5699 Mips_relobj<size, big_endian>* object, unsigned int symndx,
5700 Mips_address addend, unsigned int r_type, unsigned int shndx,
5701 bool is_section_symbol)
5703 Mips_got_entry<size, big_endian>* entry =
5704 new Mips_got_entry<size, big_endian>(object, symndx, addend,
5705 mips_elf_reloc_tls_type(r_type),
5706 shndx, is_section_symbol);
5707 this->record_got_entry(entry, object);
5710 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5711 // in OBJECT. FOR_CALL is true if the caller is only interested in
5712 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5715 template<int size, bool big_endian>
5717 Mips_got_info<size, big_endian>::record_global_got_symbol(
5718 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5719 unsigned int r_type, bool dyn_reloc, bool for_call)
5722 mips_sym->set_got_not_only_for_calls();
5724 // A global symbol in the GOT must also be in the dynamic symbol table.
5725 if (!mips_sym->needs_dynsym_entry() && !mips_sym->is_forced_local())
5727 switch (mips_sym->visibility())
5729 case elfcpp::STV_INTERNAL:
5730 case elfcpp::STV_HIDDEN:
5731 mips_sym->set_is_forced_local();
5734 mips_sym->set_needs_dynsym_entry();
5739 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5740 if (tls_type == GOT_TLS_NONE)
5741 this->global_got_symbols_.insert(mips_sym);
5745 if (mips_sym->global_got_area() == GGA_NONE)
5746 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5750 Mips_got_entry<size, big_endian>* entry =
5751 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5753 this->record_got_entry(entry, object);
5756 // Add ENTRY to master GOT and to OBJECT's GOT.
5758 template<int size, bool big_endian>
5760 Mips_got_info<size, big_endian>::record_got_entry(
5761 Mips_got_entry<size, big_endian>* entry,
5762 Mips_relobj<size, big_endian>* object)
5764 this->got_entries_.insert(entry);
5766 // Create the GOT entry for the OBJECT's GOT.
5767 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5768 Mips_got_entry<size, big_endian>* entry2 =
5769 new Mips_got_entry<size, big_endian>(*entry);
5771 g->got_entries_.insert(entry2);
5774 // Record that OBJECT has a page relocation against symbol SYMNDX and
5775 // that ADDEND is the addend for that relocation.
5776 // This function creates an upper bound on the number of GOT slots
5777 // required; no attempt is made to combine references to non-overridable
5778 // global symbols across multiple input files.
5780 template<int size, bool big_endian>
5782 Mips_got_info<size, big_endian>::record_got_page_entry(
5783 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5785 struct Got_page_range **range_ptr, *range;
5786 int old_pages, new_pages;
5788 // Find the Got_page_entry for this symbol.
5789 Got_page_entry* entry = new Got_page_entry(object, symndx);
5790 typename Got_page_entry_set::iterator it =
5791 this->got_page_entries_.find(entry);
5792 if (it != this->got_page_entries_.end())
5795 this->got_page_entries_.insert(entry);
5797 // Add the same entry to the OBJECT's GOT.
5798 Got_page_entry* entry2 = NULL;
5799 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5800 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5802 entry2 = new Got_page_entry(*entry);
5803 g2->got_page_entries_.insert(entry2);
5806 // Skip over ranges whose maximum extent cannot share a page entry
5808 range_ptr = &entry->ranges;
5809 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5810 range_ptr = &(*range_ptr)->next;
5812 // If we scanned to the end of the list, or found a range whose
5813 // minimum extent cannot share a page entry with ADDEND, create
5814 // a new singleton range.
5816 if (!range || addend < range->min_addend - 0xffff)
5818 range = new Got_page_range();
5819 range->next = *range_ptr;
5820 range->min_addend = addend;
5821 range->max_addend = addend;
5826 ++entry2->num_pages;
5827 ++this->page_gotno_;
5832 // Remember how many pages the old range contributed.
5833 old_pages = range->get_max_pages();
5835 // Update the ranges.
5836 if (addend < range->min_addend)
5837 range->min_addend = addend;
5838 else if (addend > range->max_addend)
5840 if (range->next && addend >= range->next->min_addend - 0xffff)
5842 old_pages += range->next->get_max_pages();
5843 range->max_addend = range->next->max_addend;
5844 range->next = range->next->next;
5847 range->max_addend = addend;
5850 // Record any change in the total estimate.
5851 new_pages = range->get_max_pages();
5852 if (old_pages != new_pages)
5854 entry->num_pages += new_pages - old_pages;
5856 entry2->num_pages += new_pages - old_pages;
5857 this->page_gotno_ += new_pages - old_pages;
5858 g2->page_gotno_ += new_pages - old_pages;
5862 // Create all entries that should be in the local part of the GOT.
5864 template<int size, bool big_endian>
5866 Mips_got_info<size, big_endian>::add_local_entries(
5867 Target_mips<size, big_endian>* target, Layout* layout)
5869 Mips_output_data_got<size, big_endian>* got = target->got_section();
5870 // First two GOT entries are reserved. The first entry will be filled at
5871 // runtime. The second entry will be used by some runtime loaders.
5872 got->add_constant(0);
5873 got->add_constant(target->mips_elf_gnu_got1_mask());
5875 for (typename Got_entry_set::iterator
5876 p = this->got_entries_.begin();
5877 p != this->got_entries_.end();
5880 Mips_got_entry<size, big_endian>* entry = *p;
5881 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5883 got->add_local(entry->object(), entry->symndx(),
5884 GOT_TYPE_STANDARD, entry->addend());
5885 unsigned int got_offset = entry->object()->local_got_offset(
5886 entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5887 if (got->multi_got() && this->index_ > 0
5888 && parameters->options().output_is_position_independent())
5890 if (!entry->is_section_symbol())
5891 target->rel_dyn_section(layout)->add_local(entry->object(),
5892 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5894 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5895 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5901 this->add_page_entries(target, layout);
5903 // Add global entries that should be in the local area.
5904 for (typename Got_entry_set::iterator
5905 p = this->got_entries_.begin();
5906 p != this->got_entries_.end();
5909 Mips_got_entry<size, big_endian>* entry = *p;
5910 if (!entry->is_for_global_symbol())
5913 Mips_symbol<size>* mips_sym = entry->sym();
5914 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5916 unsigned int got_type;
5917 if (!got->multi_got())
5918 got_type = GOT_TYPE_STANDARD;
5920 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5921 if (got->add_global(mips_sym, got_type))
5923 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5924 if (got->multi_got() && this->index_ > 0
5925 && parameters->options().output_is_position_independent())
5926 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5927 mips_sym, elfcpp::R_MIPS_REL32, got,
5928 mips_sym->got_offset(got_type));
5934 // Create GOT page entries.
5936 template<int size, bool big_endian>
5938 Mips_got_info<size, big_endian>::add_page_entries(
5939 Target_mips<size, big_endian>* target, Layout* layout)
5941 if (this->page_gotno_ == 0)
5944 Mips_output_data_got<size, big_endian>* got = target->got_section();
5945 this->got_page_offset_start_ = got->add_constant(0);
5946 if (got->multi_got() && this->index_ > 0
5947 && parameters->options().output_is_position_independent())
5948 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5949 this->got_page_offset_start_);
5950 int num_entries = this->page_gotno_;
5951 unsigned int prev_offset = this->got_page_offset_start_;
5952 while (--num_entries > 0)
5954 unsigned int next_offset = got->add_constant(0);
5955 if (got->multi_got() && this->index_ > 0
5956 && parameters->options().output_is_position_independent())
5957 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5959 gold_assert(next_offset == prev_offset + size/8);
5960 prev_offset = next_offset;
5962 this->got_page_offset_next_ = this->got_page_offset_start_;
5965 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5967 template<int size, bool big_endian>
5969 Mips_got_info<size, big_endian>::add_global_entries(
5970 Target_mips<size, big_endian>* target, Layout* layout,
5971 unsigned int non_reloc_only_global_gotno)
5973 Mips_output_data_got<size, big_endian>* got = target->got_section();
5974 // Add GGA_NORMAL entries.
5975 unsigned int count = 0;
5976 for (typename Got_entry_set::iterator
5977 p = this->got_entries_.begin();
5978 p != this->got_entries_.end();
5981 Mips_got_entry<size, big_endian>* entry = *p;
5982 if (!entry->is_for_global_symbol())
5985 Mips_symbol<size>* mips_sym = entry->sym();
5986 if (mips_sym->global_got_area() != GGA_NORMAL)
5989 unsigned int got_type;
5990 if (!got->multi_got())
5991 got_type = GOT_TYPE_STANDARD;
5993 // In multi-GOT links, global symbol can be in both primary and
5994 // secondary GOT(s). By creating custom GOT type
5995 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5996 // is added to secondary GOT(s).
5997 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5998 if (!got->add_global(mips_sym, got_type))
6001 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6002 if (got->multi_got() && this->index_ == 0)
6004 if (got->multi_got() && this->index_ > 0)
6006 if (parameters->options().output_is_position_independent()
6007 || (!parameters->doing_static_link()
6008 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
6010 target->rel_dyn_section(layout)->add_global(
6011 mips_sym, elfcpp::R_MIPS_REL32, got,
6012 mips_sym->got_offset(got_type));
6013 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
6014 elfcpp::R_MIPS_REL32, mips_sym);
6019 if (!got->multi_got() || this->index_ == 0)
6021 if (got->multi_got())
6023 // We need to allocate space in the primary GOT for GGA_NORMAL entries
6024 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
6025 // entries correspond to dynamic symbol indexes.
6026 while (count < non_reloc_only_global_gotno)
6028 got->add_constant(0);
6033 // Add GGA_RELOC_ONLY entries.
6034 got->add_reloc_only_entries();
6038 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6040 template<int size, bool big_endian>
6042 Mips_got_info<size, big_endian>::add_reloc_only_entries(
6043 Mips_output_data_got<size, big_endian>* got)
6045 for (typename Global_got_entry_set::iterator
6046 p = this->global_got_symbols_.begin();
6047 p != this->global_got_symbols_.end();
6050 Mips_symbol<size>* mips_sym = *p;
6051 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6053 unsigned int got_type;
6054 if (!got->multi_got())
6055 got_type = GOT_TYPE_STANDARD;
6057 got_type = GOT_TYPE_STANDARD_MULTIGOT;
6058 if (got->add_global(mips_sym, got_type))
6059 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6064 // Create TLS GOT entries.
6066 template<int size, bool big_endian>
6068 Mips_got_info<size, big_endian>::add_tls_entries(
6069 Target_mips<size, big_endian>* target, Layout* layout)
6071 Mips_output_data_got<size, big_endian>* got = target->got_section();
6072 // Add local tls entries.
6073 for (typename Got_entry_set::iterator
6074 p = this->got_entries_.begin();
6075 p != this->got_entries_.end();
6078 Mips_got_entry<size, big_endian>* entry = *p;
6079 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6082 if (entry->tls_type() == GOT_TLS_GD)
6084 unsigned int got_type = GOT_TYPE_TLS_PAIR;
6085 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6086 : elfcpp::R_MIPS_TLS_DTPMOD64);
6087 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6088 : elfcpp::R_MIPS_TLS_DTPREL64);
6090 if (!parameters->doing_static_link())
6092 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
6093 entry->shndx(), got_type,
6094 target->rel_dyn_section(layout),
6095 r_type1, entry->addend());
6096 unsigned int got_offset =
6097 entry->object()->local_got_offset(entry->symndx(), got_type,
6099 got->add_static_reloc(got_offset + size/8, r_type2,
6100 entry->object(), entry->symndx());
6104 // We are doing a static link. Mark it as belong to module 1,
6106 unsigned int got_offset = got->add_constant(1);
6107 entry->object()->set_local_got_offset(entry->symndx(), got_type,
6110 got->add_constant(0);
6111 got->add_static_reloc(got_offset + size/8, r_type2,
6112 entry->object(), entry->symndx());
6115 else if (entry->tls_type() == GOT_TLS_IE)
6117 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6118 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6119 : elfcpp::R_MIPS_TLS_TPREL64);
6120 if (!parameters->doing_static_link())
6121 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
6122 target->rel_dyn_section(layout), r_type,
6126 got->add_local(entry->object(), entry->symndx(), got_type,
6128 unsigned int got_offset =
6129 entry->object()->local_got_offset(entry->symndx(), got_type,
6131 got->add_static_reloc(got_offset, r_type, entry->object(),
6135 else if (entry->tls_type() == GOT_TLS_LDM)
6137 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6138 : elfcpp::R_MIPS_TLS_DTPMOD64);
6139 unsigned int got_offset;
6140 if (!parameters->doing_static_link())
6142 got_offset = got->add_constant(0);
6143 target->rel_dyn_section(layout)->add_local(
6144 entry->object(), 0, r_type, got, got_offset);
6147 // We are doing a static link. Just mark it as belong to module 1,
6149 got_offset = got->add_constant(1);
6151 got->add_constant(0);
6152 got->set_tls_ldm_offset(got_offset, entry->object());
6158 // Add global tls entries.
6159 for (typename Got_entry_set::iterator
6160 p = this->got_entries_.begin();
6161 p != this->got_entries_.end();
6164 Mips_got_entry<size, big_endian>* entry = *p;
6165 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6168 Mips_symbol<size>* mips_sym = entry->sym();
6169 if (entry->tls_type() == GOT_TLS_GD)
6171 unsigned int got_type;
6172 if (!got->multi_got())
6173 got_type = GOT_TYPE_TLS_PAIR;
6175 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6176 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6177 : elfcpp::R_MIPS_TLS_DTPMOD64);
6178 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6179 : elfcpp::R_MIPS_TLS_DTPREL64);
6180 if (!parameters->doing_static_link())
6181 got->add_global_pair_with_rel(mips_sym, got_type,
6182 target->rel_dyn_section(layout), r_type1, r_type2);
6185 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
6186 // GOT entries. The first one is initialized to be 1, which is the
6187 // module index for the main executable and the second one 0. A
6188 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6189 // the second GOT entry and will be applied by gold.
6190 unsigned int got_offset = got->add_constant(1);
6191 mips_sym->set_got_offset(got_type, got_offset);
6192 got->add_constant(0);
6193 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6196 else if (entry->tls_type() == GOT_TLS_IE)
6198 unsigned int got_type;
6199 if (!got->multi_got())
6200 got_type = GOT_TYPE_TLS_OFFSET;
6202 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
6203 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6204 : elfcpp::R_MIPS_TLS_TPREL64);
6205 if (!parameters->doing_static_link())
6206 got->add_global_with_rel(mips_sym, got_type,
6207 target->rel_dyn_section(layout), r_type);
6210 got->add_global(mips_sym, got_type);
6211 unsigned int got_offset = mips_sym->got_offset(got_type);
6212 got->add_static_reloc(got_offset, r_type, mips_sym);
6220 // Decide whether the symbol needs an entry in the global part of the primary
6221 // GOT, setting global_got_area accordingly. Count the number of global
6222 // symbols that are in the primary GOT only because they have dynamic
6223 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6225 template<int size, bool big_endian>
6227 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6229 for (typename Global_got_entry_set::iterator
6230 p = this->global_got_symbols_.begin();
6231 p != this->global_got_symbols_.end();
6234 Mips_symbol<size>* sym = *p;
6235 // Make a final decision about whether the symbol belongs in the
6236 // local or global GOT. Symbols that bind locally can (and in the
6237 // case of forced-local symbols, must) live in the local GOT.
6238 // Those that are aren't in the dynamic symbol table must also
6239 // live in the local GOT.
6241 if (!sym->should_add_dynsym_entry(symtab)
6242 || (sym->got_only_for_calls()
6243 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6244 : symbol_references_local(sym,
6245 sym->should_add_dynsym_entry(symtab))))
6246 // The symbol belongs in the local GOT. We no longer need this
6247 // entry if it was only used for relocations; those relocations
6248 // will be against the null or section symbol instead.
6249 sym->set_global_got_area(GGA_NONE);
6250 else if (sym->global_got_area() == GGA_RELOC_ONLY)
6252 ++this->reloc_only_gotno_;
6253 ++this->global_gotno_ ;
6258 // Return the offset of GOT page entry for VALUE. Initialize the entry with
6259 // VALUE if it is not initialized.
6261 template<int size, bool big_endian>
6263 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6264 Mips_output_data_got<size, big_endian>* got)
6266 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6267 if (it != this->got_page_offsets_.end())
6270 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6271 + (size/8) * this->page_gotno_);
6273 unsigned int got_offset = this->got_page_offset_next_;
6274 this->got_page_offsets_[value] = got_offset;
6275 this->got_page_offset_next_ += size/8;
6276 got->update_got_entry(got_offset, value);
6280 // Remove lazy-binding stubs for global symbols in this GOT.
6282 template<int size, bool big_endian>
6284 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6285 Target_mips<size, big_endian>* target)
6287 for (typename Got_entry_set::iterator
6288 p = this->got_entries_.begin();
6289 p != this->got_entries_.end();
6292 Mips_got_entry<size, big_endian>* entry = *p;
6293 if (entry->is_for_global_symbol())
6294 target->remove_lazy_stub_entry(entry->sym());
6298 // Count the number of GOT entries required.
6300 template<int size, bool big_endian>
6302 Mips_got_info<size, big_endian>::count_got_entries()
6304 for (typename Got_entry_set::iterator
6305 p = this->got_entries_.begin();
6306 p != this->got_entries_.end();
6309 this->count_got_entry(*p);
6313 // Count the number of GOT entries required by ENTRY. Accumulate the result.
6315 template<int size, bool big_endian>
6317 Mips_got_info<size, big_endian>::count_got_entry(
6318 Mips_got_entry<size, big_endian>* entry)
6320 if (entry->is_tls_entry())
6321 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6322 else if (entry->is_for_local_symbol()
6323 || entry->sym()->global_got_area() == GGA_NONE)
6324 ++this->local_gotno_;
6326 ++this->global_gotno_;
6329 // Add FROM's GOT entries.
6331 template<int size, bool big_endian>
6333 Mips_got_info<size, big_endian>::add_got_entries(
6334 Mips_got_info<size, big_endian>* from)
6336 for (typename Got_entry_set::iterator
6337 p = from->got_entries_.begin();
6338 p != from->got_entries_.end();
6341 Mips_got_entry<size, big_endian>* entry = *p;
6342 if (this->got_entries_.find(entry) == this->got_entries_.end())
6344 Mips_got_entry<size, big_endian>* entry2 =
6345 new Mips_got_entry<size, big_endian>(*entry);
6346 this->got_entries_.insert(entry2);
6347 this->count_got_entry(entry);
6352 // Add FROM's GOT page entries.
6354 template<int size, bool big_endian>
6356 Mips_got_info<size, big_endian>::add_got_page_entries(
6357 Mips_got_info<size, big_endian>* from)
6359 for (typename Got_page_entry_set::iterator
6360 p = from->got_page_entries_.begin();
6361 p != from->got_page_entries_.end();
6364 Got_page_entry* entry = *p;
6365 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
6367 Got_page_entry* entry2 = new Got_page_entry(*entry);
6368 this->got_page_entries_.insert(entry2);
6369 this->page_gotno_ += entry->num_pages;
6374 // Mips_output_data_got methods.
6376 // Lay out the GOT. Add local, global and TLS entries. If GOT is
6377 // larger than 64K, create multi-GOT.
6379 template<int size, bool big_endian>
6381 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6382 Symbol_table* symtab, const Input_objects* input_objects)
6384 // Decide which symbols need to go in the global part of the GOT and
6385 // count the number of reloc-only GOT symbols.
6386 this->master_got_info_->count_got_symbols(symtab);
6388 // Count the number of GOT entries.
6389 this->master_got_info_->count_got_entries();
6391 unsigned int got_size = this->master_got_info_->got_size();
6392 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6393 this->lay_out_multi_got(layout, input_objects);
6396 // Record that all objects use single GOT.
6397 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6398 p != input_objects->relobj_end();
6401 Mips_relobj<size, big_endian>* object =
6402 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6403 if (object->get_got_info() != NULL)
6404 object->set_got_info(this->master_got_info_);
6407 this->master_got_info_->add_local_entries(this->target_, layout);
6408 this->master_got_info_->add_global_entries(this->target_, layout,
6410 this->master_got_info_->add_tls_entries(this->target_, layout);
6414 // Create multi-GOT. For every GOT, add local, global and TLS entries.
6416 template<int size, bool big_endian>
6418 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6419 const Input_objects* input_objects)
6421 // Try to merge the GOTs of input objects together, as long as they
6422 // don't seem to exceed the maximum GOT size, choosing one of them
6423 // to be the primary GOT.
6424 this->merge_gots(input_objects);
6426 // Every symbol that is referenced in a dynamic relocation must be
6427 // present in the primary GOT.
6428 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6432 unsigned int offset = 0;
6433 Mips_got_info<size, big_endian>* g = this->primary_got_;
6437 g->set_offset(offset);
6439 g->add_local_entries(this->target_, layout);
6441 g->add_global_entries(this->target_, layout,
6442 (this->master_got_info_->global_gotno()
6443 - this->master_got_info_->reloc_only_gotno()));
6445 g->add_global_entries(this->target_, layout, /*not used*/-1U);
6446 g->add_tls_entries(this->target_, layout);
6448 // Forbid global symbols in every non-primary GOT from having
6449 // lazy-binding stubs.
6451 g->remove_lazy_stubs(this->target_);
6454 offset += g->got_size();
6460 // Attempt to merge GOTs of different input objects. Try to use as much as
6461 // possible of the primary GOT, since it doesn't require explicit dynamic
6462 // relocations, but don't use objects that would reference global symbols
6463 // out of the addressable range. Failing the primary GOT, attempt to merge
6464 // with the current GOT, or finish the current GOT and then make make the new
6467 template<int size, bool big_endian>
6469 Mips_output_data_got<size, big_endian>::merge_gots(
6470 const Input_objects* input_objects)
6472 gold_assert(this->primary_got_ == NULL);
6473 Mips_got_info<size, big_endian>* current = NULL;
6475 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6476 p != input_objects->relobj_end();
6479 Mips_relobj<size, big_endian>* object =
6480 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6482 Mips_got_info<size, big_endian>* g = object->get_got_info();
6486 g->count_got_entries();
6488 // Work out the number of page, local and TLS entries.
6489 unsigned int estimate = this->master_got_info_->page_gotno();
6490 if (estimate > g->page_gotno())
6491 estimate = g->page_gotno();
6492 estimate += g->local_gotno() + g->tls_gotno();
6494 // We place TLS GOT entries after both locals and globals. The globals
6495 // for the primary GOT may overflow the normal GOT size limit, so be
6496 // sure not to merge a GOT which requires TLS with the primary GOT in that
6497 // case. This doesn't affect non-primary GOTs.
6498 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6499 : g->global_gotno());
6501 unsigned int max_count =
6502 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6503 if (estimate <= max_count)
6505 // If we don't have a primary GOT, use it as
6506 // a starting point for the primary GOT.
6507 if (!this->primary_got_)
6509 this->primary_got_ = g;
6513 // Try merging with the primary GOT.
6514 if (this->merge_got_with(g, object, this->primary_got_))
6518 // If we can merge with the last-created GOT, do it.
6519 if (current && this->merge_got_with(g, object, current))
6522 // Well, we couldn't merge, so create a new GOT. Don't check if it
6523 // fits; if it turns out that it doesn't, we'll get relocation
6524 // overflows anyway.
6525 g->set_next(current);
6529 // If we do not find any suitable primary GOT, create an empty one.
6530 if (this->primary_got_ == NULL)
6531 this->primary_got_ = new Mips_got_info<size, big_endian>();
6533 // Link primary GOT with secondary GOTs.
6534 this->primary_got_->set_next(current);
6537 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6538 // this would lead to overflow, true if they were merged successfully.
6540 template<int size, bool big_endian>
6542 Mips_output_data_got<size, big_endian>::merge_got_with(
6543 Mips_got_info<size, big_endian>* from,
6544 Mips_relobj<size, big_endian>* object,
6545 Mips_got_info<size, big_endian>* to)
6547 // Work out how many page entries we would need for the combined GOT.
6548 unsigned int estimate = this->master_got_info_->page_gotno();
6549 if (estimate >= from->page_gotno() + to->page_gotno())
6550 estimate = from->page_gotno() + to->page_gotno();
6552 // Conservatively estimate how many local and TLS entries would be needed.
6553 estimate += from->local_gotno() + to->local_gotno();
6554 estimate += from->tls_gotno() + to->tls_gotno();
6556 // If we're merging with the primary got, any TLS relocations will
6557 // come after the full set of global entries. Otherwise estimate those
6558 // conservatively as well.
6559 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6560 estimate += this->master_got_info_->global_gotno();
6562 estimate += from->global_gotno() + to->global_gotno();
6564 // Bail out if the combined GOT might be too big.
6565 unsigned int max_count =
6566 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6567 if (estimate > max_count)
6570 // Transfer the object's GOT information from FROM to TO.
6571 to->add_got_entries(from);
6572 to->add_got_page_entries(from);
6574 // Record that OBJECT should use output GOT TO.
6575 object->set_got_info(to);
6580 // Write out the GOT.
6582 template<int size, bool big_endian>
6584 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6586 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6587 Mips_stubs_entry_set;
6589 // Call parent to write out GOT.
6590 Output_data_got<size, big_endian>::do_write(of);
6592 const off_t offset = this->offset();
6593 const section_size_type oview_size =
6594 convert_to_section_size_type(this->data_size());
6595 unsigned char* const oview = of->get_output_view(offset, oview_size);
6597 // Needed for fixing values of .got section.
6598 this->got_view_ = oview;
6600 // Write lazy stub addresses.
6601 for (typename Mips_stubs_entry_set::iterator
6602 p = this->master_got_info_->global_got_symbols().begin();
6603 p != this->master_got_info_->global_got_symbols().end();
6606 Mips_symbol<size>* mips_sym = *p;
6607 if (mips_sym->has_lazy_stub())
6609 Valtype* wv = reinterpret_cast<Valtype*>(
6610 oview + this->get_primary_got_offset(mips_sym));
6612 this->target_->mips_stubs_section()->stub_address(mips_sym);
6613 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6617 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6618 for (typename Mips_stubs_entry_set::iterator
6619 p = this->master_got_info_->global_got_symbols().begin();
6620 p != this->master_got_info_->global_got_symbols().end();
6623 Mips_symbol<size>* mips_sym = *p;
6624 if (!this->multi_got()
6625 && (mips_sym->is_mips16() || mips_sym->is_micromips())
6626 && mips_sym->global_got_area() == GGA_NONE
6627 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6629 Valtype* wv = reinterpret_cast<Valtype*>(
6630 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6631 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6635 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6640 if (!this->secondary_got_relocs_.empty())
6642 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6643 // secondary GOT entries with non-zero initial value copy the value
6644 // to the corresponding primary GOT entry, and set the secondary GOT
6646 // TODO(sasa): This is workaround. It needs to be investigated further.
6648 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6650 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6651 if (reloc.symbol_is_global())
6653 Mips_symbol<size>* gsym = reloc.symbol();
6654 gold_assert(gsym != NULL);
6656 unsigned got_offset = reloc.got_offset();
6657 gold_assert(got_offset < oview_size);
6659 // Find primary GOT entry.
6660 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6661 oview + this->get_primary_got_offset(gsym));
6663 // Find secondary GOT entry.
6664 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6666 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6669 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6670 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6671 gsym->set_applied_secondary_got_fixup();
6676 of->write_output_view(offset, oview_size, oview);
6679 // We are done if there is no fix up.
6680 if (this->static_relocs_.empty())
6683 Output_segment* tls_segment = this->layout_->tls_segment();
6684 gold_assert(tls_segment != NULL);
6686 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6688 Static_reloc& reloc(this->static_relocs_[i]);
6691 if (!reloc.symbol_is_global())
6693 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6694 const Symbol_value<size>* psymval =
6695 object->local_symbol(reloc.index());
6697 // We are doing static linking. Issue an error and skip this
6698 // relocation if the symbol is undefined or in a discarded_section.
6700 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6701 if ((shndx == elfcpp::SHN_UNDEF)
6703 && shndx != elfcpp::SHN_UNDEF
6704 && !object->is_section_included(shndx)
6705 && !this->symbol_table_->is_section_folded(object, shndx)))
6707 gold_error(_("undefined or discarded local symbol %u from "
6708 " object %s in GOT"),
6709 reloc.index(), reloc.relobj()->name().c_str());
6713 value = psymval->value(object, 0);
6717 const Mips_symbol<size>* gsym = reloc.symbol();
6718 gold_assert(gsym != NULL);
6720 // We are doing static linking. Issue an error and skip this
6721 // relocation if the symbol is undefined or in a discarded_section
6722 // unless it is a weakly_undefined symbol.
6723 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6724 && !gsym->is_weak_undefined())
6726 gold_error(_("undefined or discarded symbol %s in GOT"),
6731 if (!gsym->is_weak_undefined())
6732 value = gsym->value();
6737 unsigned got_offset = reloc.got_offset();
6738 gold_assert(got_offset < oview_size);
6740 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6743 switch (reloc.r_type())
6745 case elfcpp::R_MIPS_TLS_DTPMOD32:
6746 case elfcpp::R_MIPS_TLS_DTPMOD64:
6749 case elfcpp::R_MIPS_TLS_DTPREL32:
6750 case elfcpp::R_MIPS_TLS_DTPREL64:
6751 x = value - elfcpp::DTP_OFFSET;
6753 case elfcpp::R_MIPS_TLS_TPREL32:
6754 case elfcpp::R_MIPS_TLS_TPREL64:
6755 x = value - elfcpp::TP_OFFSET;
6762 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6765 of->write_output_view(offset, oview_size, oview);
6768 // Mips_relobj methods.
6770 // Count the local symbols. The Mips backend needs to know if a symbol
6771 // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6772 // because the Symbol object keeps the ELF symbol type and st_other field.
6773 // For local symbol it is harder because we cannot access this information.
6774 // So we override the do_count_local_symbol in parent and scan local symbols to
6775 // mark MIPS16 and microMIPS functions. This is not the most efficient way but
6776 // I do not want to slow down other ports by calling a per symbol target hook
6777 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6779 template<int size, bool big_endian>
6781 Mips_relobj<size, big_endian>::do_count_local_symbols(
6782 Stringpool_template<char>* pool,
6783 Stringpool_template<char>* dynpool)
6785 // Ask parent to count the local symbols.
6786 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6787 const unsigned int loccount = this->local_symbol_count();
6791 // Initialize the mips16 and micromips function bit-vector.
6792 this->local_symbol_is_mips16_.resize(loccount, false);
6793 this->local_symbol_is_micromips_.resize(loccount, false);
6795 // Read the symbol table section header.
6796 const unsigned int symtab_shndx = this->symtab_shndx();
6797 elfcpp::Shdr<size, big_endian>
6798 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6799 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6801 // Read the local symbols.
6802 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6803 gold_assert(loccount == symtabshdr.get_sh_info());
6804 off_t locsize = loccount * sym_size;
6805 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6806 locsize, true, true);
6808 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6810 // Skip the first dummy symbol.
6812 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6814 elfcpp::Sym<size, big_endian> sym(psyms);
6815 unsigned char st_other = sym.get_st_other();
6816 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6817 this->local_symbol_is_micromips_[i] =
6818 elfcpp::elf_st_is_micromips(st_other);
6822 // Read the symbol information.
6824 template<int size, bool big_endian>
6826 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6828 // Call parent class to read symbol information.
6829 this->base_read_symbols(sd);
6831 // If this input file is a binary file, it has no processor
6833 Input_file::Format format = this->input_file()->format();
6834 if (format != Input_file::FORMAT_ELF)
6836 gold_assert(format == Input_file::FORMAT_BINARY);
6837 this->merge_processor_specific_data_ = false;
6841 // Read processor-specific flags in ELF file header.
6842 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6843 elfcpp::Elf_sizes<size>::ehdr_size,
6845 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6846 this->processor_specific_flags_ = ehdr.get_e_flags();
6848 // Get the section names.
6849 const unsigned char* pnamesu = sd->section_names->data();
6850 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6852 // Initialize the mips16 stub section bit-vectors.
6853 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6854 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6855 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6857 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6858 const unsigned char* pshdrs = sd->section_headers->data();
6859 const unsigned char* ps = pshdrs + shdr_size;
6860 bool must_merge_processor_specific_data = false;
6861 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6863 elfcpp::Shdr<size, big_endian> shdr(ps);
6865 // Sometimes an object has no contents except the section name string
6866 // table and an empty symbol table with the undefined symbol. We
6867 // don't want to merge processor-specific data from such an object.
6868 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6870 // Symbol table is not empty.
6871 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
6872 elfcpp::Elf_sizes<size>::sym_size;
6873 if (shdr.get_sh_size() > sym_size)
6874 must_merge_processor_specific_data = true;
6876 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6877 // If this is neither an empty symbol table nor a string table,
6879 must_merge_processor_specific_data = true;
6881 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6883 this->has_reginfo_section_ = true;
6884 // Read the gp value that was used to create this object. We need the
6885 // gp value while processing relocs. The .reginfo section is not used
6886 // in the 64-bit MIPS ELF ABI.
6887 section_offset_type section_offset = shdr.get_sh_offset();
6888 section_size_type section_size =
6889 convert_to_section_size_type(shdr.get_sh_size());
6890 const unsigned char* view =
6891 this->get_view(section_offset, section_size, true, false);
6893 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6895 // Read the rest of .reginfo.
6896 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6897 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6898 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6899 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6900 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6903 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6905 gold_assert(this->attributes_section_data_ == NULL);
6906 section_offset_type section_offset = shdr.get_sh_offset();
6907 section_size_type section_size =
6908 convert_to_section_size_type(shdr.get_sh_size());
6909 const unsigned char* view =
6910 this->get_view(section_offset, section_size, true, false);
6911 this->attributes_section_data_ =
6912 new Attributes_section_data(view, section_size);
6915 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6917 gold_assert(this->abiflags_ == NULL);
6918 section_offset_type section_offset = shdr.get_sh_offset();
6919 section_size_type section_size =
6920 convert_to_section_size_type(shdr.get_sh_size());
6921 const unsigned char* view =
6922 this->get_view(section_offset, section_size, true, false);
6923 this->abiflags_ = new Mips_abiflags<big_endian>();
6925 this->abiflags_->version =
6926 elfcpp::Swap<16, big_endian>::readval(view);
6927 if (this->abiflags_->version != 0)
6929 gold_error(_("%s: .MIPS.abiflags section has "
6930 "unsupported version %u"),
6931 this->name().c_str(),
6932 this->abiflags_->version);
6935 this->abiflags_->isa_level =
6936 elfcpp::Swap<8, big_endian>::readval(view + 2);
6937 this->abiflags_->isa_rev =
6938 elfcpp::Swap<8, big_endian>::readval(view + 3);
6939 this->abiflags_->gpr_size =
6940 elfcpp::Swap<8, big_endian>::readval(view + 4);
6941 this->abiflags_->cpr1_size =
6942 elfcpp::Swap<8, big_endian>::readval(view + 5);
6943 this->abiflags_->cpr2_size =
6944 elfcpp::Swap<8, big_endian>::readval(view + 6);
6945 this->abiflags_->fp_abi =
6946 elfcpp::Swap<8, big_endian>::readval(view + 7);
6947 this->abiflags_->isa_ext =
6948 elfcpp::Swap<32, big_endian>::readval(view + 8);
6949 this->abiflags_->ases =
6950 elfcpp::Swap<32, big_endian>::readval(view + 12);
6951 this->abiflags_->flags1 =
6952 elfcpp::Swap<32, big_endian>::readval(view + 16);
6953 this->abiflags_->flags2 =
6954 elfcpp::Swap<32, big_endian>::readval(view + 20);
6957 // In the 64-bit ABI, .MIPS.options section holds register information.
6958 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6959 // starts with this header:
6963 // // Type of option.
6964 // unsigned char kind[1];
6965 // // Size of option descriptor, including header.
6966 // unsigned char size[1];
6967 // // Section index of affected section, or 0 for global option.
6968 // unsigned char section[2];
6969 // // Information specific to this kind of option.
6970 // unsigned char info[4];
6973 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6974 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6975 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6977 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6979 section_offset_type section_offset = shdr.get_sh_offset();
6980 section_size_type section_size =
6981 convert_to_section_size_type(shdr.get_sh_size());
6982 const unsigned char* view =
6983 this->get_view(section_offset, section_size, true, false);
6984 const unsigned char* end = view + section_size;
6986 while (view + 8 <= end)
6988 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6989 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6992 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6994 this->name().c_str(),
6995 this->mips_elf_options_section_name(), sz);
6999 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
7001 // In the 64 bit ABI, an ODK_REGINFO option is the following
7002 // structure. The info field of the options header is not
7007 // // Mask of general purpose registers used.
7008 // unsigned char ri_gprmask[4];
7010 // unsigned char ri_pad[4];
7011 // // Mask of co-processor registers used.
7012 // unsigned char ri_cprmask[4][4];
7013 // // GP register value for this object file.
7014 // unsigned char ri_gp_value[8];
7017 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7020 else if (kind == elfcpp::ODK_REGINFO)
7022 // In the 32 bit ABI, an ODK_REGINFO option is the following
7023 // structure. The info field of the options header is not
7024 // used. The same structure is used in .reginfo section.
7028 // unsigned char ri_gprmask[4];
7029 // unsigned char ri_cprmask[4][4];
7030 // unsigned char ri_gp_value[4];
7033 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7040 const char* name = pnames + shdr.get_sh_name();
7041 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
7042 this->section_is_mips16_call_stub_[i] =
7043 is_prefix_of(".mips16.call.", name);
7044 this->section_is_mips16_call_fp_stub_[i] =
7045 is_prefix_of(".mips16.call.fp.", name);
7047 if (strcmp(name, ".pdr") == 0)
7049 gold_assert(this->pdr_shndx_ == -1U);
7050 this->pdr_shndx_ = i;
7055 if (!must_merge_processor_specific_data)
7056 this->merge_processor_specific_data_ = false;
7059 // Discard MIPS16 stub secions that are not needed.
7061 template<int size, bool big_endian>
7063 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7065 for (typename Mips16_stubs_int_map::const_iterator
7066 it = this->mips16_stub_sections_.begin();
7067 it != this->mips16_stub_sections_.end(); ++it)
7069 Mips16_stub_section<size, big_endian>* stub_section = it->second;
7070 if (!stub_section->is_target_found())
7072 gold_error(_("no relocation found in mips16 stub section '%s'"),
7073 stub_section->object()
7074 ->section_name(stub_section->shndx()).c_str());
7077 bool discard = false;
7078 if (stub_section->is_for_local_function())
7080 if (stub_section->is_fn_stub())
7082 // This stub is for a local symbol. This stub will only
7083 // be needed if there is some relocation in this object,
7084 // other than a 16 bit function call, which refers to this
7086 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7089 this->add_local_mips16_fn_stub(stub_section);
7093 // This stub is for a local symbol. This stub will only
7094 // be needed if there is some relocation (R_MIPS16_26) in
7095 // this object that refers to this symbol.
7096 gold_assert(stub_section->is_call_stub()
7097 || stub_section->is_call_fp_stub());
7098 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
7101 this->add_local_mips16_call_stub(stub_section);
7106 Mips_symbol<size>* gsym = stub_section->gsym();
7107 if (stub_section->is_fn_stub())
7109 if (gsym->has_mips16_fn_stub())
7110 // We already have a stub for this function.
7114 gsym->set_mips16_fn_stub(stub_section);
7115 if (gsym->should_add_dynsym_entry(symtab))
7117 // If we have a MIPS16 function with a stub, the
7118 // dynamic symbol must refer to the stub, since only
7119 // the stub uses the standard calling conventions.
7120 gsym->set_need_fn_stub();
7121 if (gsym->is_from_dynobj())
7122 gsym->set_needs_dynsym_value();
7125 if (!gsym->need_fn_stub())
7128 else if (stub_section->is_call_stub())
7130 if (gsym->is_mips16())
7131 // We don't need the call_stub; this is a 16 bit
7132 // function, so calls from other 16 bit functions are
7135 else if (gsym->has_mips16_call_stub())
7136 // We already have a stub for this function.
7139 gsym->set_mips16_call_stub(stub_section);
7143 gold_assert(stub_section->is_call_fp_stub());
7144 if (gsym->is_mips16())
7145 // We don't need the call_stub; this is a 16 bit
7146 // function, so calls from other 16 bit functions are
7149 else if (gsym->has_mips16_call_fp_stub())
7150 // We already have a stub for this function.
7153 gsym->set_mips16_call_fp_stub(stub_section);
7157 this->set_output_section(stub_section->shndx(), NULL);
7161 // Mips_output_data_la25_stub methods.
7163 // Template for standard LA25 stub.
7164 template<int size, bool big_endian>
7166 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7168 0x3c190000, // lui $25,%hi(func)
7169 0x08000000, // j func
7170 0x27390000, // add $25,$25,%lo(func)
7174 // Template for microMIPS LA25 stub.
7175 template<int size, bool big_endian>
7177 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7179 0x41b9, 0x0000, // lui t9,%hi(func)
7180 0xd400, 0x0000, // j func
7181 0x3339, 0x0000, // addiu t9,t9,%lo(func)
7182 0x0000, 0x0000 // nop
7185 // Create la25 stub for a symbol.
7187 template<int size, bool big_endian>
7189 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7190 Symbol_table* symtab, Target_mips<size, big_endian>* target,
7191 Mips_symbol<size>* gsym)
7193 if (!gsym->has_la25_stub())
7195 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
7196 this->symbols_.push_back(gsym);
7197 this->create_stub_symbol(gsym, symtab, target, 16);
7201 // Create a symbol for SYM stub's value and size, to help make the disassembly
7204 template<int size, bool big_endian>
7206 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7207 Mips_symbol<size>* sym, Symbol_table* symtab,
7208 Target_mips<size, big_endian>* target, uint64_t symsize)
7210 std::string name(".pic.");
7211 name += sym->name();
7213 unsigned int offset = sym->la25_stub_offset();
7214 if (sym->is_micromips())
7217 // Make it a local function.
7218 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7219 Symbol_table::PREDEFINED,
7220 target->la25_stub_section(),
7221 offset, symsize, elfcpp::STT_FUNC,
7223 elfcpp::STV_DEFAULT, 0,
7225 new_sym->set_is_forced_local();
7228 // Write out la25 stubs. This uses the hand-coded instructions above,
7229 // and adjusts them as needed.
7231 template<int size, bool big_endian>
7233 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7235 const off_t offset = this->offset();
7236 const section_size_type oview_size =
7237 convert_to_section_size_type(this->data_size());
7238 unsigned char* const oview = of->get_output_view(offset, oview_size);
7240 for (typename std::vector<Mips_symbol<size>*>::iterator
7241 p = this->symbols_.begin();
7242 p != this->symbols_.end();
7245 Mips_symbol<size>* sym = *p;
7246 unsigned char* pov = oview + sym->la25_stub_offset();
7248 Mips_address target = sym->value();
7249 if (!sym->is_micromips())
7251 elfcpp::Swap<32, big_endian>::writeval(pov,
7252 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7253 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7254 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7255 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7256 la25_stub_entry[2] | (target & 0xffff));
7257 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7262 // First stub instruction. Paste high 16-bits of the target.
7263 elfcpp::Swap<16, big_endian>::writeval(pov,
7264 la25_stub_micromips_entry[0]);
7265 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7266 ((target + 0x8000) >> 16) & 0xffff);
7267 // Second stub instruction. Paste low 26-bits of the target, shifted
7269 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7270 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7271 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7272 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7273 // Third stub instruction. Paste low 16-bits of the target.
7274 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7275 la25_stub_micromips_entry[4]);
7276 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7277 // Fourth stub instruction.
7278 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7279 la25_stub_micromips_entry[6]);
7280 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7281 la25_stub_micromips_entry[7]);
7285 of->write_output_view(offset, oview_size, oview);
7288 // Mips_output_data_plt methods.
7290 // The format of the first PLT entry in an O32 executable.
7291 template<int size, bool big_endian>
7292 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7294 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
7295 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
7296 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
7297 0x031cc023, // subu $24, $24, $28
7298 0x03e07825, // or $15, $31, zero
7299 0x0018c082, // srl $24, $24, 2
7300 0x0320f809, // jalr $25
7301 0x2718fffe // subu $24, $24, 2
7304 // The format of the first PLT entry in an N32 executable. Different
7305 // because gp ($28) is not available; we use t2 ($14) instead.
7306 template<int size, bool big_endian>
7307 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7309 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7310 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
7311 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7312 0x030ec023, // subu $24, $24, $14
7313 0x03e07825, // or $15, $31, zero
7314 0x0018c082, // srl $24, $24, 2
7315 0x0320f809, // jalr $25
7316 0x2718fffe // subu $24, $24, 2
7319 // The format of the first PLT entry in an N64 executable. Different
7320 // from N32 because of the increased size of GOT entries.
7321 template<int size, bool big_endian>
7322 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7324 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7325 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
7326 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7327 0x030ec023, // subu $24, $24, $14
7328 0x03e07825, // or $15, $31, zero
7329 0x0018c0c2, // srl $24, $24, 3
7330 0x0320f809, // jalr $25
7331 0x2718fffe // subu $24, $24, 2
7334 // The format of the microMIPS first PLT entry in an O32 executable.
7335 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7336 // of the GOTPLT entry handled, so this stub may only be used when
7337 // all the subsequent PLT entries are microMIPS code too.
7339 // The trailing NOP is for alignment and correct disassembly only.
7340 template<int size, bool big_endian>
7341 const uint32_t Mips_output_data_plt<size, big_endian>::
7342 plt0_entry_micromips_o32[] =
7344 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
7345 0xff23, 0x0000, // lw $25, 0($3)
7346 0x0535, // subu $2, $2, $3
7347 0x2525, // srl $2, $2, 2
7348 0x3302, 0xfffe, // subu $24, $2, 2
7349 0x0dff, // move $15, $31
7350 0x45f9, // jalrs $25
7351 0x0f83, // move $28, $3
7355 // The format of the microMIPS first PLT entry in an O32 executable
7356 // in the insn32 mode.
7357 template<int size, bool big_endian>
7358 const uint32_t Mips_output_data_plt<size, big_endian>::
7359 plt0_entry_micromips32_o32[] =
7361 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
7362 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
7363 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
7364 0x0398, 0xc1d0, // subu $24, $24, $28
7365 0x001f, 0x7a90, // or $15, $31, zero
7366 0x0318, 0x1040, // srl $24, $24, 2
7367 0x03f9, 0x0f3c, // jalr $25
7368 0x3318, 0xfffe // subu $24, $24, 2
7371 // The format of subsequent standard entries in the PLT.
7372 template<int size, bool big_endian>
7373 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7375 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7376 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7377 0x03200008, // jr $25
7378 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7381 // The format of subsequent R6 PLT entries.
7382 template<int size, bool big_endian>
7383 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7385 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7386 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7387 0x03200009, // jr $25
7388 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7391 // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
7392 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7393 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7394 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7395 // target function address in register v0.
7396 template<int size, bool big_endian>
7397 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7399 0xb303, // lw $3, 12($pc)
7400 0x651b, // move $24, $3
7401 0x9b60, // lw $3, 0($3)
7403 0x653b, // move $25, $3
7405 0x0000, 0x0000 // .word (.got.plt entry)
7408 // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
7409 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7410 template<int size, bool big_endian>
7411 const uint32_t Mips_output_data_plt<size, big_endian>::
7412 plt_entry_micromips_o32[] =
7414 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
7415 0xff22, 0x0000, // lw $25, 0($2)
7417 0x0f02 // move $24, $2
7420 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7421 template<int size, bool big_endian>
7422 const uint32_t Mips_output_data_plt<size, big_endian>::
7423 plt_entry_micromips32_o32[] =
7425 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
7426 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
7427 0x0019, 0x0f3c, // jr $25
7428 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
7431 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7433 template<int size, bool big_endian>
7435 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7436 unsigned int r_type)
7438 gold_assert(!gsym->has_plt_offset());
7440 // Final PLT offset for a symbol will be set in method set_plt_offsets().
7441 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7442 + sizeof(plt0_entry_o32));
7443 this->symbols_.push_back(gsym);
7445 // Record whether the relocation requires a standard MIPS
7446 // or a compressed code entry.
7447 if (jal_reloc(r_type))
7449 if (r_type == elfcpp::R_MIPS_26)
7450 gsym->set_needs_mips_plt(true);
7452 gsym->set_needs_comp_plt(true);
7455 section_offset_type got_offset = this->got_plt_->current_data_size();
7457 // Every PLT entry needs a GOT entry which points back to the PLT
7458 // entry (this will be changed by the dynamic linker, normally
7459 // lazily when the function is called).
7460 this->got_plt_->set_current_data_size(got_offset + size/8);
7462 gsym->set_needs_dynsym_entry();
7463 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7467 // Set final PLT offsets. For each symbol, determine whether standard or
7468 // compressed (MIPS16 or microMIPS) PLT entry is used.
7470 template<int size, bool big_endian>
7472 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7474 // The sizes of individual PLT entries.
7475 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7476 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7477 ? this->compressed_plt_entry_size() : 0);
7479 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7480 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7482 Mips_symbol<size>* mips_sym = *p;
7484 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7485 // so always use a standard entry there.
7487 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7488 // all MIPS16 calls will go via that stub, and there is no benefit
7489 // to having a MIPS16 entry. And in the case of call_stub a
7490 // standard entry actually has to be used as the stub ends with a J
7492 if (this->target_->is_output_newabi()
7493 || mips_sym->has_mips16_call_stub()
7494 || mips_sym->has_mips16_call_fp_stub())
7496 mips_sym->set_needs_mips_plt(true);
7497 mips_sym->set_needs_comp_plt(false);
7500 // Otherwise, if there are no direct calls to the function, we
7501 // have a free choice of whether to use standard or compressed
7502 // entries. Prefer microMIPS entries if the object is known to
7503 // contain microMIPS code, so that it becomes possible to create
7504 // pure microMIPS binaries. Prefer standard entries otherwise,
7505 // because MIPS16 ones are no smaller and are usually slower.
7506 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7508 if (this->target_->is_output_micromips())
7509 mips_sym->set_needs_comp_plt(true);
7511 mips_sym->set_needs_mips_plt(true);
7514 if (mips_sym->needs_mips_plt())
7516 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7517 this->plt_mips_offset_ += plt_mips_entry_size;
7519 if (mips_sym->needs_comp_plt())
7521 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7522 this->plt_comp_offset_ += plt_comp_entry_size;
7526 // Figure out the size of the PLT header if we know that we are using it.
7527 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7528 this->plt_header_size_ = this->get_plt_header_size();
7531 // Write out the PLT. This uses the hand-coded instructions above,
7532 // and adjusts them as needed.
7534 template<int size, bool big_endian>
7536 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7538 const off_t offset = this->offset();
7539 const section_size_type oview_size =
7540 convert_to_section_size_type(this->data_size());
7541 unsigned char* const oview = of->get_output_view(offset, oview_size);
7543 const off_t gotplt_file_offset = this->got_plt_->offset();
7544 const section_size_type gotplt_size =
7545 convert_to_section_size_type(this->got_plt_->data_size());
7546 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7548 unsigned char* pov = oview;
7550 Mips_address plt_address = this->address();
7552 // Calculate the address of .got.plt.
7553 Mips_address gotplt_addr = this->got_plt_->address();
7554 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7555 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7557 // The PLT sequence is not safe for N64 if .got.plt's address can
7558 // not be loaded in two instructions.
7559 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7560 || ~(gotplt_addr | 0x7fffffff) == 0);
7562 // Write the PLT header.
7563 const uint32_t* plt0_entry = this->get_plt_header_entry();
7564 if (plt0_entry == plt0_entry_micromips_o32)
7566 // Write microMIPS PLT header.
7567 gold_assert(gotplt_addr % 4 == 0);
7569 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7571 // ADDIUPC has a span of +/-16MB, check we're in range.
7572 if (gotpc_offset + 0x1000000 >= 0x2000000)
7574 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7575 "ADDIUPC"), (long)gotpc_offset);
7579 elfcpp::Swap<16, big_endian>::writeval(pov,
7580 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7581 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7582 (gotpc_offset >> 2) & 0xffff);
7584 for (unsigned int i = 2;
7585 i < (sizeof(plt0_entry_micromips_o32)
7586 / sizeof(plt0_entry_micromips_o32[0]));
7589 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7593 else if (plt0_entry == plt0_entry_micromips32_o32)
7595 // Write microMIPS PLT header in insn32 mode.
7596 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7597 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7598 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7599 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7600 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7601 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7603 for (unsigned int i = 6;
7604 i < (sizeof(plt0_entry_micromips32_o32)
7605 / sizeof(plt0_entry_micromips32_o32[0]));
7608 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7614 // Write standard PLT header.
7615 elfcpp::Swap<32, big_endian>::writeval(pov,
7616 plt0_entry[0] | gotplt_addr_high);
7617 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7618 plt0_entry[1] | gotplt_addr_low);
7619 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7620 plt0_entry[2] | gotplt_addr_low);
7622 for (int i = 3; i < 8; i++)
7624 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7630 unsigned char* gotplt_pov = gotplt_view;
7631 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7633 // The first two entries in .got.plt are reserved.
7634 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7635 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7637 unsigned int gotplt_offset = 2 * got_entry_size;
7638 gotplt_pov += 2 * got_entry_size;
7640 // Calculate the address of the PLT header.
7641 Mips_address header_address = (plt_address
7642 + (this->is_plt_header_compressed() ? 1 : 0));
7644 // Initialize compressed PLT area view.
7645 unsigned char* pov2 = pov + this->plt_mips_offset_;
7647 // Write the PLT entries.
7648 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7649 p = this->symbols_.begin();
7650 p != this->symbols_.end();
7651 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7653 Mips_symbol<size>* mips_sym = *p;
7655 // Calculate the address of the .got.plt entry.
7656 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7657 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7659 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7661 // Initially point the .got.plt entry at the PLT header.
7662 if (this->target_->is_output_n64())
7663 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7665 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7667 // Now handle the PLT itself. First the standard entry.
7668 if (mips_sym->has_mips_plt_offset())
7670 // Pick the load opcode (LW or LD).
7671 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7674 const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7677 // Fill in the PLT entry itself.
7678 elfcpp::Swap<32, big_endian>::writeval(pov,
7679 entry[0] | gotplt_entry_addr_hi);
7680 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7681 entry[1] | gotplt_entry_addr_lo | load);
7682 elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
7683 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7684 entry[3] | gotplt_entry_addr_lo);
7688 // Now the compressed entry. They come after any standard ones.
7689 if (mips_sym->has_comp_plt_offset())
7691 if (!this->target_->is_output_micromips())
7693 // Write MIPS16 PLT entry.
7694 const uint32_t* plt_entry = plt_entry_mips16_o32;
7696 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7697 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7698 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7699 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7700 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7701 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7702 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7706 else if (this->target_->use_32bit_micromips_instructions())
7708 // Write microMIPS PLT entry in insn32 mode.
7709 const uint32_t* plt_entry = plt_entry_micromips32_o32;
7711 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7712 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7713 gotplt_entry_addr_hi);
7714 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7715 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7716 gotplt_entry_addr_lo);
7717 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7718 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7719 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7720 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7721 gotplt_entry_addr_lo);
7726 // Write microMIPS PLT entry.
7727 const uint32_t* plt_entry = plt_entry_micromips_o32;
7729 gold_assert(gotplt_entry_addr % 4 == 0);
7731 Mips_address loc_address = plt_address + pov2 - oview;
7732 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7734 // ADDIUPC has a span of +/-16MB, check we're in range.
7735 if (gotpc_offset + 0x1000000 >= 0x2000000)
7737 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7738 "range of ADDIUPC"), (long)gotpc_offset);
7742 elfcpp::Swap<16, big_endian>::writeval(pov2,
7743 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7744 elfcpp::Swap<16, big_endian>::writeval(
7745 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7746 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7747 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7748 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7749 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7755 // Check the number of bytes written for standard entries.
7756 gold_assert(static_cast<section_size_type>(
7757 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7758 // Check the number of bytes written for compressed entries.
7759 gold_assert((static_cast<section_size_type>(pov2 - pov)
7760 == this->plt_comp_offset_));
7761 // Check the total number of bytes written.
7762 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7764 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7767 of->write_output_view(offset, oview_size, oview);
7768 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7771 // Mips_output_data_mips_stubs methods.
7773 // The format of the lazy binding stub when dynamic symbol count is less than
7774 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7775 template<int size, bool big_endian>
7777 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7779 0x8f998010, // lw t9,0x8010(gp)
7780 0x03e07825, // or t7,ra,zero
7781 0x0320f809, // jalr t9,ra
7782 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7785 // The format of the lazy binding stub when dynamic symbol count is less than
7786 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7787 template<int size, bool big_endian>
7789 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7791 0xdf998010, // ld t9,0x8010(gp)
7792 0x03e07825, // or t7,ra,zero
7793 0x0320f809, // jalr t9,ra
7794 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7797 // The format of the lazy binding stub when dynamic symbol count is less than
7798 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7799 template<int size, bool big_endian>
7801 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7803 0x8f998010, // lw t9,0x8010(gp)
7804 0x03e07825, // or t7,ra,zero
7805 0x0320f809, // jalr t9,ra
7806 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7809 // The format of the lazy binding stub when dynamic symbol count is less than
7810 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7811 template<int size, bool big_endian>
7813 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7815 0xdf998010, // ld t9,0x8010(gp)
7816 0x03e07825, // or t7,ra,zero
7817 0x0320f809, // jalr t9,ra
7818 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7821 // The format of the lazy binding stub when dynamic symbol count is greater than
7822 // 64K, and ABI is not N64.
7823 template<int size, bool big_endian>
7824 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7826 0x8f998010, // lw t9,0x8010(gp)
7827 0x03e07825, // or t7,ra,zero
7828 0x3c180000, // lui t8,DYN_INDEX
7829 0x0320f809, // jalr t9,ra
7830 0x37180000 // ori t8,t8,DYN_INDEX
7833 // The format of the lazy binding stub when dynamic symbol count is greater than
7834 // 64K, and ABI is N64.
7835 template<int size, bool big_endian>
7837 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7839 0xdf998010, // ld t9,0x8010(gp)
7840 0x03e07825, // or t7,ra,zero
7841 0x3c180000, // lui t8,DYN_INDEX
7842 0x0320f809, // jalr t9,ra
7843 0x37180000 // ori t8,t8,DYN_INDEX
7848 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7849 // less than 64K, dynamic symbol index is less than 32K, 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_1[] =
7854 0xff3c, 0x8010, // lw t9,0x8010(gp)
7855 0x0dff, // move t7,ra
7857 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7860 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7861 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7862 template<int size, bool big_endian>
7864 Mips_output_data_mips_stubs<size, big_endian>::
7865 lazy_stub_micromips_normal_1_n64[] =
7867 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7868 0x0dff, // move t7,ra
7870 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7873 // The format of the microMIPS lazy binding stub when dynamic symbol
7874 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7875 // and ABI is not N64.
7876 template<int size, bool big_endian>
7878 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7880 0xff3c, 0x8010, // lw t9,0x8010(gp)
7881 0x0dff, // move t7,ra
7883 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7886 // The format of the microMIPS lazy binding stub when dynamic symbol
7887 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7889 template<int size, bool big_endian>
7891 Mips_output_data_mips_stubs<size, big_endian>::
7892 lazy_stub_micromips_normal_2_n64[] =
7894 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7895 0x0dff, // move t7,ra
7897 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7900 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7901 // greater than 64K, and ABI is not N64.
7902 template<int size, bool big_endian>
7904 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7906 0xff3c, 0x8010, // lw t9,0x8010(gp)
7907 0x0dff, // move t7,ra
7908 0x41b8, 0x0000, // lui t8,DYN_INDEX
7910 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7913 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7914 // greater than 64K, and ABI is N64.
7915 template<int size, bool big_endian>
7917 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7919 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7920 0x0dff, // move t7,ra
7921 0x41b8, 0x0000, // lui t8,DYN_INDEX
7923 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7926 // 32-bit microMIPS stubs.
7928 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7929 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7930 // can use only 32-bit instructions.
7931 template<int size, bool big_endian>
7933 Mips_output_data_mips_stubs<size, big_endian>::
7934 lazy_stub_micromips32_normal_1[] =
7936 0xff3c, 0x8010, // lw t9,0x8010(gp)
7937 0x001f, 0x7a90, // or t7,ra,zero
7938 0x03f9, 0x0f3c, // jalr ra,t9
7939 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7942 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7943 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7944 // use only 32-bit instructions.
7945 template<int size, bool big_endian>
7947 Mips_output_data_mips_stubs<size, big_endian>::
7948 lazy_stub_micromips32_normal_1_n64[] =
7950 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7951 0x001f, 0x7a90, // or t7,ra,zero
7952 0x03f9, 0x0f3c, // jalr ra,t9
7953 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7956 // The format of the microMIPS lazy binding stub when dynamic symbol
7957 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7958 // ABI is not N64, and we can use only 32-bit instructions.
7959 template<int size, bool big_endian>
7961 Mips_output_data_mips_stubs<size, big_endian>::
7962 lazy_stub_micromips32_normal_2[] =
7964 0xff3c, 0x8010, // lw t9,0x8010(gp)
7965 0x001f, 0x7a90, // or t7,ra,zero
7966 0x03f9, 0x0f3c, // jalr ra,t9
7967 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7970 // The format of the microMIPS lazy binding stub when dynamic symbol
7971 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7972 // 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>::
7976 lazy_stub_micromips32_normal_2_n64[] =
7978 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7979 0x001f, 0x7a90, // or t7,ra,zero
7980 0x03f9, 0x0f3c, // jalr ra,t9
7981 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7984 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7985 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7986 template<int size, bool big_endian>
7988 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7990 0xff3c, 0x8010, // lw t9,0x8010(gp)
7991 0x001f, 0x7a90, // or t7,ra,zero
7992 0x41b8, 0x0000, // lui t8,DYN_INDEX
7993 0x03f9, 0x0f3c, // jalr ra,t9
7994 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7997 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7998 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7999 template<int size, bool big_endian>
8001 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
8003 0xdf3c, 0x8010, // ld t9,0x8010(gp)
8004 0x001f, 0x7a90, // or t7,ra,zero
8005 0x41b8, 0x0000, // lui t8,DYN_INDEX
8006 0x03f9, 0x0f3c, // jalr ra,t9
8007 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
8010 // Create entry for a symbol.
8012 template<int size, bool big_endian>
8014 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
8015 Mips_symbol<size>* gsym)
8017 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
8019 this->symbols_.insert(gsym);
8020 gsym->set_has_lazy_stub(true);
8024 // Remove entry for a symbol.
8026 template<int size, bool big_endian>
8028 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8029 Mips_symbol<size>* gsym)
8031 if (gsym->has_lazy_stub())
8033 this->symbols_.erase(gsym);
8034 gsym->set_has_lazy_stub(false);
8038 // Set stub offsets for symbols. This method expects that the number of
8039 // entries in dynamic symbol table is set.
8041 template<int size, bool big_endian>
8043 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8045 gold_assert(this->dynsym_count_ != -1U);
8047 if (this->stub_offsets_are_set_)
8050 unsigned int stub_size = this->stub_size();
8051 unsigned int offset = 0;
8052 for (typename Mips_stubs_entry_set::const_iterator
8053 p = this->symbols_.begin();
8054 p != this->symbols_.end();
8055 ++p, offset += stub_size)
8057 Mips_symbol<size>* mips_sym = *p;
8058 mips_sym->set_lazy_stub_offset(offset);
8060 this->stub_offsets_are_set_ = true;
8063 template<int size, bool big_endian>
8065 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8067 for (typename Mips_stubs_entry_set::const_iterator
8068 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8070 Mips_symbol<size>* sym = *p;
8071 if (sym->is_from_dynobj())
8072 sym->set_needs_dynsym_value();
8076 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
8077 // adjusts them as needed.
8079 template<int size, bool big_endian>
8081 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
8083 const off_t offset = this->offset();
8084 const section_size_type oview_size =
8085 convert_to_section_size_type(this->data_size());
8086 unsigned char* const oview = of->get_output_view(offset, oview_size);
8088 bool big_stub = this->dynsym_count_ > 0x10000;
8090 unsigned char* pov = oview;
8091 for (typename Mips_stubs_entry_set::const_iterator
8092 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8094 Mips_symbol<size>* sym = *p;
8095 const uint32_t* lazy_stub;
8096 bool n64 = this->target_->is_output_n64();
8098 if (!this->target_->is_output_micromips())
8100 // Write standard (non-microMIPS) stub.
8103 if (sym->dynsym_index() & ~0x7fff)
8104 // Dynsym index is between 32K and 64K.
8105 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
8107 // Dynsym index is less than 32K.
8108 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8111 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8114 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8115 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8121 // LUI instruction of the big stub. Paste high 16 bits of the
8123 elfcpp::Swap<32, big_endian>::writeval(pov,
8124 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
8128 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8129 // Last stub instruction. Paste low 16 bits of the dynsym index.
8130 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8131 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8134 else if (this->target_->use_32bit_micromips_instructions())
8136 // Write microMIPS stub in insn32 mode.
8139 if (sym->dynsym_index() & ~0x7fff)
8140 // Dynsym index is between 32K and 64K.
8141 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8142 : lazy_stub_micromips32_normal_2;
8144 // Dynsym index is less than 32K.
8145 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8146 : lazy_stub_micromips32_normal_1;
8149 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8150 : lazy_stub_micromips32_big;
8153 // First stub instruction. We emit 32-bit microMIPS instructions by
8154 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8155 // the instruction where the opcode is must always come first, for
8156 // both little and big endian.
8157 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8158 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8159 // Second stub instruction.
8160 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8161 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8166 // LUI instruction of the big stub. Paste high 16 bits of the
8168 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8169 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8170 (sym->dynsym_index() >> 16) & 0x7fff);
8174 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8175 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8176 // Last stub instruction. Paste low 16 bits of the dynsym index.
8177 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8178 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8179 sym->dynsym_index() & 0xffff);
8184 // Write microMIPS stub.
8187 if (sym->dynsym_index() & ~0x7fff)
8188 // Dynsym index is between 32K and 64K.
8189 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8190 : lazy_stub_micromips_normal_2;
8192 // Dynsym index is less than 32K.
8193 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8194 : lazy_stub_micromips_normal_1;
8197 lazy_stub = n64 ? lazy_stub_micromips_big_n64
8198 : lazy_stub_micromips_big;
8201 // First stub instruction. We emit 32-bit microMIPS instructions by
8202 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8203 // the instruction where the opcode is must always come first, for
8204 // both little and big endian.
8205 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8206 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8207 // Second stub instruction.
8208 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8213 // LUI instruction of the big stub. Paste high 16 bits of the
8215 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8216 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8217 (sym->dynsym_index() >> 16) & 0x7fff);
8221 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8222 // Last stub instruction. Paste low 16 bits of the dynsym index.
8223 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8224 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8225 sym->dynsym_index() & 0xffff);
8230 // We always allocate 20 bytes for every stub, because final dynsym count is
8231 // not known in method do_finalize_sections. There are 4 unused bytes per
8232 // stub if final dynsym count is less than 0x10000.
8233 unsigned int used = pov - oview;
8234 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8235 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8237 // Fill the unused space with zeroes.
8238 // TODO(sasa): Can we strip unused bytes during the relaxation?
8240 memset(pov, 0, unused);
8242 of->write_output_view(offset, oview_size, oview);
8245 // Mips_output_section_reginfo methods.
8247 template<int size, bool big_endian>
8249 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8251 off_t offset = this->offset();
8252 off_t data_size = this->data_size();
8254 unsigned char* view = of->get_output_view(offset, data_size);
8255 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8256 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8257 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8258 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8259 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8260 // Write the gp value.
8261 elfcpp::Swap<size, big_endian>::writeval(view + 20,
8262 this->target_->gp_value());
8264 of->write_output_view(offset, data_size, view);
8267 // Mips_output_section_options methods.
8269 template<int size, bool big_endian>
8271 Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
8273 off_t offset = this->offset();
8274 const section_size_type oview_size =
8275 convert_to_section_size_type(this->data_size());
8276 unsigned char* view = of->get_output_view(offset, oview_size);
8277 const unsigned char* end = view + oview_size;
8279 while (view + 8 <= end)
8281 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8282 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8285 gold_error(_("Warning: bad `%s' option size %u smaller "
8286 "than its header in output section"),
8291 // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
8292 if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
8293 elfcpp::Swap<size, big_endian>::writeval(view + 32,
8294 this->target_->gp_value());
8295 else if (kind == elfcpp::ODK_REGINFO)
8296 elfcpp::Swap<size, big_endian>::writeval(view + 28,
8297 this->target_->gp_value());
8302 of->write_output_view(offset, oview_size, view);
8305 // Mips_output_section_abiflags methods.
8307 template<int size, bool big_endian>
8309 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8311 off_t offset = this->offset();
8312 off_t data_size = this->data_size();
8314 unsigned char* view = of->get_output_view(offset, data_size);
8315 elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8316 elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8317 elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8318 elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8319 elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8320 elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8321 elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8322 elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8323 elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8324 elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8325 elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8327 of->write_output_view(offset, data_size, view);
8330 // Mips_copy_relocs methods.
8332 // Emit any saved relocs.
8334 template<int sh_type, int size, bool big_endian>
8336 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8337 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8338 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8340 for (typename Copy_relocs<sh_type, size, big_endian>::
8341 Copy_reloc_entries::iterator p = this->entries_.begin();
8342 p != this->entries_.end();
8344 emit_entry(*p, reloc_section, symtab, layout, target);
8346 // We no longer need the saved information.
8347 this->entries_.clear();
8350 // Emit the reloc if appropriate.
8352 template<int sh_type, int size, bool big_endian>
8354 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8355 Copy_reloc_entry& entry,
8356 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8357 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8359 // If the symbol is no longer defined in a dynamic object, then we
8360 // emitted a COPY relocation, and we do not want to emit this
8361 // dynamic relocation.
8362 if (!entry.sym_->is_from_dynobj())
8365 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8366 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8367 || entry.reloc_type_ == elfcpp::R_MIPS_64);
8369 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8370 if (can_make_dynamic && !sym->has_static_relocs())
8372 Mips_relobj<size, big_endian>* object =
8373 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8374 target->got_section(symtab, layout)->record_global_got_symbol(
8375 sym, object, entry.reloc_type_, true, false);
8376 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8377 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8378 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8380 target->rel_dyn_section(layout)->add_symbolless_global_addend(
8381 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8382 entry.shndx_, entry.address_);
8385 this->make_copy_reloc(symtab, layout,
8386 static_cast<Sized_symbol<size>*>(entry.sym_),
8391 // Target_mips methods.
8393 // Return the value to use for a dynamic symbol which requires special
8394 // treatment. This is how we support equality comparisons of function
8395 // pointers across shared library boundaries, as described in the
8396 // processor specific ABI supplement.
8398 template<int size, bool big_endian>
8400 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8403 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8405 if (!mips_sym->has_lazy_stub())
8407 if (mips_sym->has_plt_offset())
8409 // We distinguish between PLT entries and lazy-binding stubs by
8410 // giving the former an st_other value of STO_MIPS_PLT. Set the
8411 // value to the stub address if there are any relocations in the
8412 // binary where pointer equality matters.
8413 if (mips_sym->pointer_equality_needed())
8415 // Prefer a standard MIPS PLT entry.
8416 if (mips_sym->has_mips_plt_offset())
8417 value = this->plt_section()->mips_entry_address(mips_sym);
8419 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8427 // First, set stub offsets for symbols. This method expects that the
8428 // number of entries in dynamic symbol table is set.
8429 this->mips_stubs_section()->set_lazy_stub_offsets();
8431 // The run-time linker uses the st_value field of the symbol
8432 // to reset the global offset table entry for this external
8433 // to its stub address when unlinking a shared object.
8434 value = this->mips_stubs_section()->stub_address(mips_sym);
8437 if (mips_sym->has_mips16_fn_stub())
8439 // If we have a MIPS16 function with a stub, the dynamic symbol must
8440 // refer to the stub, since only the stub uses the standard calling
8442 value = mips_sym->template
8443 get_mips16_fn_stub<big_endian>()->output_address();
8449 // Get the dynamic reloc section, creating it if necessary. It's always
8450 // .rel.dyn, even for MIPS64.
8452 template<int size, bool big_endian>
8453 typename Target_mips<size, big_endian>::Reloc_section*
8454 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8456 if (this->rel_dyn_ == NULL)
8458 gold_assert(layout != NULL);
8459 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8460 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8461 elfcpp::SHF_ALLOC, this->rel_dyn_,
8462 ORDER_DYNAMIC_RELOCS, false);
8464 // First entry in .rel.dyn has to be null.
8465 // This is hack - we define dummy output data and set its address to 0,
8466 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8467 // This ensures that the entry is null.
8468 Output_data* od = new Output_data_zero_fill(0, 0);
8470 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8472 return this->rel_dyn_;
8475 // Get the GOT section, creating it if necessary.
8477 template<int size, bool big_endian>
8478 Mips_output_data_got<size, big_endian>*
8479 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8482 if (this->got_ == NULL)
8484 gold_assert(symtab != NULL && layout != NULL);
8486 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8488 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8489 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8490 elfcpp::SHF_MIPS_GPREL),
8491 this->got_, ORDER_DATA, false);
8493 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8494 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8495 Symbol_table::PREDEFINED,
8497 0, 0, elfcpp::STT_OBJECT,
8499 elfcpp::STV_HIDDEN, 0,
8506 // Calculate value of _gp symbol.
8508 template<int size, bool big_endian>
8510 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8512 gold_assert(this->gp_ == NULL);
8514 Sized_symbol<size>* gp =
8515 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8517 // Set _gp symbol if the linker script hasn't created it.
8518 if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
8520 // If there is no .got section, gp should be based on .sdata.
8521 Output_data* gp_section = (this->got_ != NULL
8522 ? this->got_->output_section()
8523 : layout->find_output_section(".sdata"));
8525 if (gp_section != NULL)
8526 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8527 "_gp", NULL, Symbol_table::PREDEFINED,
8528 gp_section, MIPS_GP_OFFSET, 0,
8531 elfcpp::STV_DEFAULT,
8538 // Set the dynamic symbol indexes. INDEX is the index of the first
8539 // global dynamic symbol. Pointers to the symbols are stored into the
8540 // vector SYMS. The names are added to DYNPOOL. This returns an
8541 // updated dynamic symbol index.
8543 template<int size, bool big_endian>
8545 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8546 std::vector<Symbol*>* dyn_symbols, unsigned int index,
8547 std::vector<Symbol*>* syms, Stringpool* dynpool,
8548 Versions* versions, Symbol_table* symtab) const
8550 std::vector<Symbol*> non_got_symbols;
8551 std::vector<Symbol*> got_symbols;
8553 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8556 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8557 p != non_got_symbols.end();
8562 // Note that SYM may already have a dynamic symbol index, since
8563 // some symbols appear more than once in the symbol table, with
8564 // and without a version.
8566 if (!sym->has_dynsym_index())
8568 sym->set_dynsym_index(index);
8570 syms->push_back(sym);
8571 dynpool->add(sym->name(), false, NULL);
8573 // Record any version information.
8574 if (sym->version() != NULL)
8575 versions->record_version(symtab, dynpool, sym);
8577 // If the symbol is defined in a dynamic object and is
8578 // referenced in a regular object, then mark the dynamic
8579 // object as needed. This is used to implement --as-needed.
8580 if (sym->is_from_dynobj() && sym->in_reg())
8581 sym->object()->set_is_needed();
8585 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8586 p != got_symbols.end();
8590 if (!sym->has_dynsym_index())
8592 // Record any version information.
8593 if (sym->version() != NULL)
8594 versions->record_version(symtab, dynpool, sym);
8598 index = versions->finalize(symtab, index, syms);
8600 int got_sym_count = 0;
8601 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8602 p != got_symbols.end();
8607 if (!sym->has_dynsym_index())
8610 sym->set_dynsym_index(index);
8612 syms->push_back(sym);
8613 dynpool->add(sym->name(), false, NULL);
8615 // If the symbol is defined in a dynamic object and is
8616 // referenced in a regular object, then mark the dynamic
8617 // object as needed. This is used to implement --as-needed.
8618 if (sym->is_from_dynobj() && sym->in_reg())
8619 sym->object()->set_is_needed();
8623 // Set index of the first symbol that has .got entry.
8624 this->got_->set_first_global_got_dynsym_index(
8625 got_sym_count > 0 ? index - got_sym_count : -1U);
8627 if (this->mips_stubs_ != NULL)
8628 this->mips_stubs_->set_dynsym_count(index);
8633 // Create a PLT entry for a global symbol referenced by r_type relocation.
8635 template<int size, bool big_endian>
8637 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8639 Mips_symbol<size>* gsym,
8640 unsigned int r_type)
8642 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8645 if (this->plt_ == NULL)
8647 // Create the GOT section first.
8648 this->got_section(symtab, layout);
8650 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8651 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8652 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8653 this->got_plt_, ORDER_DATA, false);
8655 // The first two entries are reserved.
8656 this->got_plt_->set_current_data_size(2 * size/8);
8658 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8661 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8663 | elfcpp::SHF_EXECINSTR),
8664 this->plt_, ORDER_PLT, false);
8666 // Make the sh_info field of .rel.plt point to .plt.
8667 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
8668 rel_plt_os->set_info_section(this->plt_->output_section());
8671 this->plt_->add_entry(gsym, r_type);
8675 // Get the .MIPS.stubs section, creating it if necessary.
8677 template<int size, bool big_endian>
8678 Mips_output_data_mips_stubs<size, big_endian>*
8679 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8681 if (this->mips_stubs_ == NULL)
8684 new Mips_output_data_mips_stubs<size, big_endian>(this);
8685 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8687 | elfcpp::SHF_EXECINSTR),
8688 this->mips_stubs_, ORDER_PLT, false);
8690 return this->mips_stubs_;
8693 // Get the LA25 stub section, creating it if necessary.
8695 template<int size, bool big_endian>
8696 Mips_output_data_la25_stub<size, big_endian>*
8697 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8699 if (this->la25_stub_ == NULL)
8701 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8702 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8704 | elfcpp::SHF_EXECINSTR),
8705 this->la25_stub_, ORDER_TEXT, false);
8707 return this->la25_stub_;
8710 // Process the relocations to determine unreferenced sections for
8711 // garbage collection.
8713 template<int size, bool big_endian>
8715 Target_mips<size, big_endian>::gc_process_relocs(
8716 Symbol_table* symtab,
8718 Sized_relobj_file<size, big_endian>* object,
8719 unsigned int data_shndx,
8720 unsigned int sh_type,
8721 const unsigned char* prelocs,
8723 Output_section* output_section,
8724 bool needs_special_offset_handling,
8725 size_t local_symbol_count,
8726 const unsigned char* plocal_symbols)
8728 typedef Target_mips<size, big_endian> Mips;
8730 if (sh_type == elfcpp::SHT_REL)
8732 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8735 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8744 needs_special_offset_handling,
8748 else if (sh_type == elfcpp::SHT_RELA)
8750 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8753 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8762 needs_special_offset_handling,
8770 // Scan relocations for a section.
8772 template<int size, bool big_endian>
8774 Target_mips<size, big_endian>::scan_relocs(
8775 Symbol_table* symtab,
8777 Sized_relobj_file<size, big_endian>* object,
8778 unsigned int data_shndx,
8779 unsigned int sh_type,
8780 const unsigned char* prelocs,
8782 Output_section* output_section,
8783 bool needs_special_offset_handling,
8784 size_t local_symbol_count,
8785 const unsigned char* plocal_symbols)
8787 typedef Target_mips<size, big_endian> Mips;
8789 if (sh_type == elfcpp::SHT_REL)
8791 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8794 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8803 needs_special_offset_handling,
8807 else if (sh_type == elfcpp::SHT_RELA)
8809 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8812 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8821 needs_special_offset_handling,
8827 template<int size, bool big_endian>
8829 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8831 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8832 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8833 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8834 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8835 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8836 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8837 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
8838 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
8841 // Return the MACH for a MIPS e_flags value.
8842 template<int size, bool big_endian>
8844 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8846 switch (flags & elfcpp::EF_MIPS_MACH)
8848 case elfcpp::E_MIPS_MACH_3900:
8849 return mach_mips3900;
8851 case elfcpp::E_MIPS_MACH_4010:
8852 return mach_mips4010;
8854 case elfcpp::E_MIPS_MACH_4100:
8855 return mach_mips4100;
8857 case elfcpp::E_MIPS_MACH_4111:
8858 return mach_mips4111;
8860 case elfcpp::E_MIPS_MACH_4120:
8861 return mach_mips4120;
8863 case elfcpp::E_MIPS_MACH_4650:
8864 return mach_mips4650;
8866 case elfcpp::E_MIPS_MACH_5400:
8867 return mach_mips5400;
8869 case elfcpp::E_MIPS_MACH_5500:
8870 return mach_mips5500;
8872 case elfcpp::E_MIPS_MACH_5900:
8873 return mach_mips5900;
8875 case elfcpp::E_MIPS_MACH_9000:
8876 return mach_mips9000;
8878 case elfcpp::E_MIPS_MACH_SB1:
8879 return mach_mips_sb1;
8881 case elfcpp::E_MIPS_MACH_LS2E:
8882 return mach_mips_loongson_2e;
8884 case elfcpp::E_MIPS_MACH_LS2F:
8885 return mach_mips_loongson_2f;
8887 case elfcpp::E_MIPS_MACH_LS3A:
8888 return mach_mips_loongson_3a;
8890 case elfcpp::E_MIPS_MACH_OCTEON3:
8891 return mach_mips_octeon3;
8893 case elfcpp::E_MIPS_MACH_OCTEON2:
8894 return mach_mips_octeon2;
8896 case elfcpp::E_MIPS_MACH_OCTEON:
8897 return mach_mips_octeon;
8899 case elfcpp::E_MIPS_MACH_XLR:
8900 return mach_mips_xlr;
8903 switch (flags & elfcpp::EF_MIPS_ARCH)
8906 case elfcpp::E_MIPS_ARCH_1:
8907 return mach_mips3000;
8909 case elfcpp::E_MIPS_ARCH_2:
8910 return mach_mips6000;
8912 case elfcpp::E_MIPS_ARCH_3:
8913 return mach_mips4000;
8915 case elfcpp::E_MIPS_ARCH_4:
8916 return mach_mips8000;
8918 case elfcpp::E_MIPS_ARCH_5:
8921 case elfcpp::E_MIPS_ARCH_32:
8922 return mach_mipsisa32;
8924 case elfcpp::E_MIPS_ARCH_64:
8925 return mach_mipsisa64;
8927 case elfcpp::E_MIPS_ARCH_32R2:
8928 return mach_mipsisa32r2;
8930 case elfcpp::E_MIPS_ARCH_32R6:
8931 return mach_mipsisa32r6;
8933 case elfcpp::E_MIPS_ARCH_64R2:
8934 return mach_mipsisa64r2;
8936 case elfcpp::E_MIPS_ARCH_64R6:
8937 return mach_mipsisa64r6;
8944 // Return the MACH for each .MIPS.abiflags ISA Extension.
8946 template<int size, bool big_endian>
8948 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8952 case elfcpp::AFL_EXT_3900:
8953 return mach_mips3900;
8955 case elfcpp::AFL_EXT_4010:
8956 return mach_mips4010;
8958 case elfcpp::AFL_EXT_4100:
8959 return mach_mips4100;
8961 case elfcpp::AFL_EXT_4111:
8962 return mach_mips4111;
8964 case elfcpp::AFL_EXT_4120:
8965 return mach_mips4120;
8967 case elfcpp::AFL_EXT_4650:
8968 return mach_mips4650;
8970 case elfcpp::AFL_EXT_5400:
8971 return mach_mips5400;
8973 case elfcpp::AFL_EXT_5500:
8974 return mach_mips5500;
8976 case elfcpp::AFL_EXT_5900:
8977 return mach_mips5900;
8979 case elfcpp::AFL_EXT_10000:
8980 return mach_mips10000;
8982 case elfcpp::AFL_EXT_LOONGSON_2E:
8983 return mach_mips_loongson_2e;
8985 case elfcpp::AFL_EXT_LOONGSON_2F:
8986 return mach_mips_loongson_2f;
8988 case elfcpp::AFL_EXT_LOONGSON_3A:
8989 return mach_mips_loongson_3a;
8991 case elfcpp::AFL_EXT_SB1:
8992 return mach_mips_sb1;
8994 case elfcpp::AFL_EXT_OCTEON:
8995 return mach_mips_octeon;
8997 case elfcpp::AFL_EXT_OCTEONP:
8998 return mach_mips_octeonp;
9000 case elfcpp::AFL_EXT_OCTEON2:
9001 return mach_mips_octeon2;
9003 case elfcpp::AFL_EXT_XLR:
9004 return mach_mips_xlr;
9007 return mach_mips3000;
9011 // Return the .MIPS.abiflags value representing each ISA Extension.
9013 template<int size, bool big_endian>
9015 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
9020 return elfcpp::AFL_EXT_3900;
9023 return elfcpp::AFL_EXT_4010;
9026 return elfcpp::AFL_EXT_4100;
9029 return elfcpp::AFL_EXT_4111;
9032 return elfcpp::AFL_EXT_4120;
9035 return elfcpp::AFL_EXT_4650;
9038 return elfcpp::AFL_EXT_5400;
9041 return elfcpp::AFL_EXT_5500;
9044 return elfcpp::AFL_EXT_5900;
9046 case mach_mips10000:
9047 return elfcpp::AFL_EXT_10000;
9049 case mach_mips_loongson_2e:
9050 return elfcpp::AFL_EXT_LOONGSON_2E;
9052 case mach_mips_loongson_2f:
9053 return elfcpp::AFL_EXT_LOONGSON_2F;
9055 case mach_mips_loongson_3a:
9056 return elfcpp::AFL_EXT_LOONGSON_3A;
9059 return elfcpp::AFL_EXT_SB1;
9061 case mach_mips_octeon:
9062 return elfcpp::AFL_EXT_OCTEON;
9064 case mach_mips_octeonp:
9065 return elfcpp::AFL_EXT_OCTEONP;
9067 case mach_mips_octeon3:
9068 return elfcpp::AFL_EXT_OCTEON3;
9070 case mach_mips_octeon2:
9071 return elfcpp::AFL_EXT_OCTEON2;
9074 return elfcpp::AFL_EXT_XLR;
9081 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
9083 template<int size, bool big_endian>
9085 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9086 elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9089 switch (e_flags & elfcpp::EF_MIPS_ARCH)
9091 case elfcpp::E_MIPS_ARCH_1:
9092 new_isa = this->level_rev(1, 0);
9094 case elfcpp::E_MIPS_ARCH_2:
9095 new_isa = this->level_rev(2, 0);
9097 case elfcpp::E_MIPS_ARCH_3:
9098 new_isa = this->level_rev(3, 0);
9100 case elfcpp::E_MIPS_ARCH_4:
9101 new_isa = this->level_rev(4, 0);
9103 case elfcpp::E_MIPS_ARCH_5:
9104 new_isa = this->level_rev(5, 0);
9106 case elfcpp::E_MIPS_ARCH_32:
9107 new_isa = this->level_rev(32, 1);
9109 case elfcpp::E_MIPS_ARCH_32R2:
9110 new_isa = this->level_rev(32, 2);
9112 case elfcpp::E_MIPS_ARCH_32R6:
9113 new_isa = this->level_rev(32, 6);
9115 case elfcpp::E_MIPS_ARCH_64:
9116 new_isa = this->level_rev(64, 1);
9118 case elfcpp::E_MIPS_ARCH_64R2:
9119 new_isa = this->level_rev(64, 2);
9121 case elfcpp::E_MIPS_ARCH_64R6:
9122 new_isa = this->level_rev(64, 6);
9125 gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9126 this->elf_mips_mach_name(e_flags));
9129 if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9131 // Decode a single value into level and revision.
9132 abiflags->isa_level = new_isa >> 3;
9133 abiflags->isa_rev = new_isa & 0x7;
9136 // Update the isa_ext if needed.
9137 if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9138 this->elf_mips_mach(e_flags)))
9139 abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9142 // Infer the content of the ABI flags based on the elf header.
9144 template<int size, bool big_endian>
9146 Target_mips<size, big_endian>::infer_abiflags(
9147 Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9149 const Attributes_section_data* pasd = relobj->attributes_section_data();
9150 int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9151 elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9153 this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9156 // Read fp_abi from the .gnu.attribute section.
9157 const Object_attribute* attr =
9158 pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9159 attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9162 abiflags->fp_abi = attr_fp_abi;
9163 abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9164 abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9165 abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9166 : elfcpp::AFL_REG_64;
9168 if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9169 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9170 || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9171 && abiflags->gpr_size == elfcpp::AFL_REG_32))
9172 abiflags->cpr1_size = elfcpp::AFL_REG_32;
9173 else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9174 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9175 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9176 abiflags->cpr1_size = elfcpp::AFL_REG_64;
9178 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9179 abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9180 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9181 abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9182 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9183 abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9185 if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9186 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9187 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9188 && abiflags->isa_level >= 32
9189 && abiflags->isa_ext != elfcpp::AFL_EXT_LOONGSON_3A)
9190 abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9193 // Create abiflags from elf header or from .MIPS.abiflags section.
9195 template<int size, bool big_endian>
9197 Target_mips<size, big_endian>::create_abiflags(
9198 Mips_relobj<size, big_endian>* relobj,
9199 Mips_abiflags<big_endian>* abiflags)
9201 Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9202 Mips_abiflags<big_endian> header_abiflags;
9204 this->infer_abiflags(relobj, &header_abiflags);
9206 if (sec_abiflags == NULL)
9208 // If there is no input .MIPS.abiflags section, use abiflags created
9210 *abiflags = header_abiflags;
9214 this->has_abiflags_section_ = true;
9216 // It is not possible to infer the correct ISA revision for R3 or R5
9217 // so drop down to R2 for the checks.
9218 unsigned char isa_rev = sec_abiflags->isa_rev;
9219 if (isa_rev == 3 || isa_rev == 5)
9222 // Check compatibility between abiflags created from elf header
9223 // and abiflags from .MIPS.abiflags section in this object file.
9224 if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9225 < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9226 gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9227 relobj->name().c_str());
9228 if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9229 && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9230 gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9231 ".MIPS.abiflags"), relobj->name().c_str());
9232 if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9233 gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9234 relobj->name().c_str());
9235 // The isa_ext is allowed to be an extension of what can be inferred
9237 if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9238 this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9239 gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9240 ".MIPS.abiflags"), relobj->name().c_str());
9241 if (sec_abiflags->flags2 != 0)
9242 gold_warning(_("%s: Unexpected flag in the flags2 field of "
9243 ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9244 sec_abiflags->flags2);
9245 // Use abiflags from .MIPS.abiflags section.
9246 *abiflags = *sec_abiflags;
9249 // Return the meaning of fp_abi, or "unknown" if not known.
9251 template<int size, bool big_endian>
9253 Target_mips<size, big_endian>::fp_abi_string(int fp)
9257 case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9258 return "-mdouble-float";
9259 case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9260 return "-msingle-float";
9261 case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9262 return "-msoft-float";
9263 case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9264 return _("-mips32r2 -mfp64 (12 callee-saved)");
9265 case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9267 case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9268 return "-mgp32 -mfp64";
9269 case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9270 return "-mgp32 -mfp64 -mno-odd-spreg";
9278 template<int size, bool big_endian>
9280 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9283 if (in_fp == out_fp)
9286 if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9288 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9289 && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9290 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9291 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9293 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9294 && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9295 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9296 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9297 return out_fp; // Keep the current setting.
9298 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9299 && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9301 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9302 && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9303 return out_fp; // Keep the current setting.
9304 else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9305 gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9306 fp_abi_string(in_fp), fp_abi_string(out_fp));
9310 // Merge attributes from input object.
9312 template<int size, bool big_endian>
9314 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9315 const Attributes_section_data* pasd)
9317 // Return if there is no attributes section data.
9321 // If output has no object attributes, just copy.
9322 if (this->attributes_section_data_ == NULL)
9324 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9328 Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9329 Object_attribute::OBJ_ATTR_GNU);
9331 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9332 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9334 // Merge Tag_compatibility attributes and any common GNU ones.
9335 this->attributes_section_data_->merge(name.c_str(), pasd);
9338 // Merge abiflags from input object.
9340 template<int size, bool big_endian>
9342 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9343 Mips_abiflags<big_endian>* in_abiflags)
9345 // If output has no abiflags, just copy.
9346 if (this->abiflags_ == NULL)
9348 this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9352 this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9353 this->abiflags_->fp_abi);
9356 this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9357 in_abiflags->isa_level);
9358 this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9359 in_abiflags->isa_rev);
9360 this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9361 in_abiflags->gpr_size);
9362 this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9363 in_abiflags->cpr1_size);
9364 this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9365 in_abiflags->cpr2_size);
9366 this->abiflags_->ases |= in_abiflags->ases;
9367 this->abiflags_->flags1 |= in_abiflags->flags1;
9370 // Check whether machine EXTENSION is an extension of machine BASE.
9371 template<int size, bool big_endian>
9373 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9374 unsigned int extension)
9376 if (extension == base)
9379 if ((base == mach_mipsisa32)
9380 && this->mips_mach_extends(mach_mipsisa64, extension))
9383 if ((base == mach_mipsisa32r2)
9384 && this->mips_mach_extends(mach_mipsisa64r2, extension))
9387 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9388 if (extension == this->mips_mach_extensions_[i].first)
9390 extension = this->mips_mach_extensions_[i].second;
9391 if (extension == base)
9398 // Merge file header flags from input object.
9400 template<int size, bool big_endian>
9402 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9403 elfcpp::Elf_Word in_flags)
9405 // If flags are not set yet, just copy them.
9406 if (!this->are_processor_specific_flags_set())
9408 this->set_processor_specific_flags(in_flags);
9409 this->mach_ = this->elf_mips_mach(in_flags);
9413 elfcpp::Elf_Word new_flags = in_flags;
9414 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9415 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9416 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9418 // Check flag compatibility.
9419 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9420 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9422 // Some IRIX 6 BSD-compatibility objects have this bit set. It
9423 // doesn't seem to matter.
9424 new_flags &= ~elfcpp::EF_MIPS_XGOT;
9425 old_flags &= ~elfcpp::EF_MIPS_XGOT;
9427 // MIPSpro generates ucode info in n64 objects. Again, we should
9428 // just be able to ignore this.
9429 new_flags &= ~elfcpp::EF_MIPS_UCODE;
9430 old_flags &= ~elfcpp::EF_MIPS_UCODE;
9432 if (new_flags == old_flags)
9434 this->set_processor_specific_flags(merged_flags);
9438 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9439 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9440 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9443 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9444 merged_flags |= elfcpp::EF_MIPS_CPIC;
9445 if (!(new_flags & elfcpp::EF_MIPS_PIC))
9446 merged_flags &= ~elfcpp::EF_MIPS_PIC;
9448 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9449 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9451 // Compare the ISAs.
9452 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9453 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9454 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9456 // Output ISA isn't the same as, or an extension of, input ISA.
9457 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9459 // Copy the architecture info from input object to output. Also copy
9460 // the 32-bit flag (if set) so that we continue to recognise
9461 // output as a 32-bit binary.
9462 this->mach_ = this->elf_mips_mach(in_flags);
9463 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9464 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9465 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9467 // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9468 this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9470 // Copy across the ABI flags if output doesn't use them
9471 // and if that was what caused us to treat input object as 32-bit.
9472 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9473 && this->mips_32bit_flags(new_flags)
9474 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9475 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9478 // The ISAs aren't compatible.
9479 gold_error(_("%s: linking %s module with previous %s modules"),
9480 name.c_str(), this->elf_mips_mach_name(in_flags),
9481 this->elf_mips_mach_name(merged_flags));
9484 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9485 | elfcpp::EF_MIPS_32BITMODE));
9486 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9487 | elfcpp::EF_MIPS_32BITMODE));
9490 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9492 // Only error if both are set (to different values).
9493 if ((new_flags & elfcpp::EF_MIPS_ABI)
9494 && (old_flags & elfcpp::EF_MIPS_ABI))
9495 gold_error(_("%s: ABI mismatch: linking %s module with "
9496 "previous %s modules"), name.c_str(),
9497 this->elf_mips_abi_name(in_flags),
9498 this->elf_mips_abi_name(merged_flags));
9500 new_flags &= ~elfcpp::EF_MIPS_ABI;
9501 old_flags &= ~elfcpp::EF_MIPS_ABI;
9504 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
9505 // and allow arbitrary mixing of the remaining ASEs (retain the union).
9506 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9507 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9509 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9510 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9511 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9512 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9513 int micro_mis = old_m16 && new_micro;
9514 int m16_mis = old_micro && new_m16;
9516 if (m16_mis || micro_mis)
9517 gold_error(_("%s: ASE mismatch: linking %s module with "
9518 "previous %s modules"), name.c_str(),
9519 m16_mis ? "MIPS16" : "microMIPS",
9520 m16_mis ? "microMIPS" : "MIPS16");
9522 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9524 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9525 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9528 // Compare NaN encodings.
9529 if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9531 gold_error(_("%s: linking %s module with previous %s modules"),
9533 (new_flags & elfcpp::EF_MIPS_NAN2008
9534 ? "-mnan=2008" : "-mnan=legacy"),
9535 (old_flags & elfcpp::EF_MIPS_NAN2008
9536 ? "-mnan=2008" : "-mnan=legacy"));
9538 new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9539 old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9542 // Compare FP64 state.
9543 if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9545 gold_error(_("%s: linking %s module with previous %s modules"),
9547 (new_flags & elfcpp::EF_MIPS_FP64
9548 ? "-mfp64" : "-mfp32"),
9549 (old_flags & elfcpp::EF_MIPS_FP64
9550 ? "-mfp64" : "-mfp32"));
9552 new_flags &= ~elfcpp::EF_MIPS_FP64;
9553 old_flags &= ~elfcpp::EF_MIPS_FP64;
9556 // Warn about any other mismatches.
9557 if (new_flags != old_flags)
9558 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9559 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9561 this->set_processor_specific_flags(merged_flags);
9564 // Adjust ELF file header.
9566 template<int size, bool big_endian>
9568 Target_mips<size, big_endian>::do_adjust_elf_header(
9569 unsigned char* view,
9572 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9574 elfcpp::Ehdr<size, big_endian> ehdr(view);
9575 unsigned char e_ident[elfcpp::EI_NIDENT];
9576 elfcpp::Elf_Word flags = this->processor_specific_flags();
9577 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9579 unsigned char ei_abiversion = 0;
9580 elfcpp::Elf_Half type = ehdr.get_e_type();
9581 if (type == elfcpp::ET_EXEC
9582 && parameters->options().copyreloc()
9583 && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9584 == elfcpp::EF_MIPS_CPIC)
9587 if (this->abiflags_ != NULL
9588 && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9589 || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9592 e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9593 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9594 oehdr.put_e_ident(e_ident);
9596 if (this->entry_symbol_is_compressed_)
9597 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9600 // do_make_elf_object to override the same function in the base class.
9601 // We need to use a target-specific sub-class of
9602 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9603 // Hence we need to have our own ELF object creation.
9605 template<int size, bool big_endian>
9607 Target_mips<size, big_endian>::do_make_elf_object(
9608 const std::string& name,
9609 Input_file* input_file,
9610 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9612 int et = ehdr.get_e_type();
9613 // ET_EXEC files are valid input for --just-symbols/-R,
9614 // and we treat them as relocatable objects.
9615 if (et == elfcpp::ET_REL
9616 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9618 Mips_relobj<size, big_endian>* obj =
9619 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9623 else if (et == elfcpp::ET_DYN)
9625 // TODO(sasa): Should we create Mips_dynobj?
9626 return Target::do_make_elf_object(name, input_file, offset, ehdr);
9630 gold_error(_("%s: unsupported ELF file type %d"),
9636 // Finalize the sections.
9638 template <int size, bool big_endian>
9640 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9641 const Input_objects* input_objects,
9642 Symbol_table* symtab)
9644 const bool relocatable = parameters->options().relocatable();
9646 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9647 // DT_FINI have correct values.
9648 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9649 symtab->lookup(parameters->options().init()));
9650 if (init != NULL && (init->is_mips16() || init->is_micromips()))
9651 init->set_value(init->value() | 1);
9652 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9653 symtab->lookup(parameters->options().fini()));
9654 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9655 fini->set_value(fini->value() | 1);
9657 // Check whether the entry symbol is mips16 or micromips. This is needed to
9658 // adjust entry address in ELF header.
9659 Mips_symbol<size>* entry =
9660 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9661 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9662 || entry->is_micromips()));
9664 if (!parameters->doing_static_link()
9665 && (strcmp(parameters->options().hash_style(), "gnu") == 0
9666 || strcmp(parameters->options().hash_style(), "both") == 0))
9668 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9669 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
9670 // MIPS ABI requires a mapping between the GOT and the symbol table.
9671 gold_error(".gnu.hash is incompatible with the MIPS ABI");
9674 // Check whether the final section that was scanned has HI16 or GOT16
9675 // relocations without the corresponding LO16 part.
9676 if (this->got16_addends_.size() > 0)
9677 gold_error("Can't find matching LO16 reloc");
9679 Valtype gprmask = 0;
9680 Valtype cprmask1 = 0;
9681 Valtype cprmask2 = 0;
9682 Valtype cprmask3 = 0;
9683 Valtype cprmask4 = 0;
9684 bool has_reginfo_section = false;
9686 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9687 p != input_objects->relobj_end();
9690 Mips_relobj<size, big_endian>* relobj =
9691 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9693 // Check for any mips16 stub sections that we can discard.
9695 relobj->discard_mips16_stub_sections(symtab);
9697 if (!relobj->merge_processor_specific_data())
9700 // Merge .reginfo contents of input objects.
9701 if (relobj->has_reginfo_section())
9703 has_reginfo_section = true;
9704 gprmask |= relobj->gprmask();
9705 cprmask1 |= relobj->cprmask1();
9706 cprmask2 |= relobj->cprmask2();
9707 cprmask3 |= relobj->cprmask3();
9708 cprmask4 |= relobj->cprmask4();
9711 // Merge processor specific flags.
9712 Mips_abiflags<big_endian> in_abiflags;
9714 this->create_abiflags(relobj, &in_abiflags);
9715 this->merge_obj_e_flags(relobj->name(),
9716 relobj->processor_specific_flags());
9717 this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9718 this->merge_obj_attributes(relobj->name(),
9719 relobj->attributes_section_data());
9722 // Create a .gnu.attributes section if we have merged any attributes
9724 if (this->attributes_section_data_ != NULL)
9726 Output_attributes_section_data* attributes_section =
9727 new Output_attributes_section_data(*this->attributes_section_data_);
9728 layout->add_output_section_data(".gnu.attributes",
9729 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9730 attributes_section, ORDER_INVALID, false);
9733 // Create .MIPS.abiflags output section if there is an input section.
9734 if (this->has_abiflags_section_)
9736 Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9737 new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9739 Output_section* os =
9740 layout->add_output_section_data(".MIPS.abiflags",
9741 elfcpp::SHT_MIPS_ABIFLAGS,
9743 abiflags_section, ORDER_INVALID, false);
9745 if (!relocatable && os != NULL)
9747 Output_segment* abiflags_segment =
9748 layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9749 abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9753 if (has_reginfo_section && !parameters->options().gc_sections())
9755 // Create .reginfo output section.
9756 Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9757 new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9759 cprmask3, cprmask4);
9761 Output_section* os =
9762 layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9763 elfcpp::SHF_ALLOC, reginfo_section,
9764 ORDER_INVALID, false);
9766 if (!relocatable && os != NULL)
9768 Output_segment* reginfo_segment =
9769 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9771 reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9775 if (this->plt_ != NULL)
9777 // Set final PLT offsets for symbols.
9778 this->plt_section()->set_plt_offsets();
9780 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9781 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9782 // there are no standard PLT entries present.
9783 unsigned char nonvis = 0;
9784 if (this->is_output_micromips()
9785 && !this->plt_section()->has_standard_entries())
9786 nonvis = elfcpp::STO_MICROMIPS >> 2;
9787 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9788 Symbol_table::PREDEFINED,
9790 0, 0, elfcpp::STT_FUNC,
9792 elfcpp::STV_DEFAULT, nonvis,
9796 if (this->mips_stubs_ != NULL)
9798 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9799 unsigned char nonvis = 0;
9800 if (this->is_output_micromips())
9801 nonvis = elfcpp::STO_MICROMIPS >> 2;
9802 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9803 Symbol_table::PREDEFINED,
9805 0, 0, elfcpp::STT_FUNC,
9807 elfcpp::STV_DEFAULT, nonvis,
9811 if (!relocatable && !parameters->doing_static_link())
9812 // In case there is no .got section, create one.
9813 this->got_section(symtab, layout);
9815 // Emit any relocs we saved in an attempt to avoid generating COPY
9817 if (this->copy_relocs_.any_saved_relocs())
9818 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9822 this->set_gp(layout, symtab);
9824 // Emit dynamic relocs.
9825 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9826 p != this->dyn_relocs_.end();
9828 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9830 if (this->has_got_section())
9831 this->got_section()->lay_out_got(layout, symtab, input_objects);
9833 if (this->mips_stubs_ != NULL)
9834 this->mips_stubs_->set_needs_dynsym_value();
9836 // Check for functions that might need $25 to be valid on entry.
9837 // TODO(sasa): Can we do this without iterating over all symbols?
9838 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9839 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9842 // Add NULL segment.
9844 layout->make_output_segment(elfcpp::PT_NULL, 0);
9846 // Fill in some more dynamic tags.
9847 // TODO(sasa): Add more dynamic tags.
9848 const Reloc_section* rel_plt = (this->plt_ == NULL
9849 ? NULL : this->plt_->rel_plt());
9850 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9851 this->rel_dyn_, true, false);
9853 Output_data_dynamic* const odyn = layout->dynamic_data();
9856 && !parameters->doing_static_link())
9859 // This element holds a 32-bit version id for the Runtime
9860 // Linker Interface. This will start at integer value 1.
9862 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9865 d_val = elfcpp::RHF_NOTPOT;
9866 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9868 // Save layout for using when emitting custom dynamic tags.
9869 this->layout_ = layout;
9871 // This member holds the base address of the segment.
9872 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9874 // This member holds the number of entries in the .dynsym section.
9875 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9877 // This member holds the index of the first dynamic symbol
9878 // table entry that corresponds to an entry in the global offset table.
9879 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9881 // This member holds the number of local GOT entries.
9882 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9883 this->got_->get_local_gotno());
9885 if (this->plt_ != NULL)
9886 // DT_MIPS_PLTGOT dynamic tag
9887 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9889 if (!parameters->options().shared())
9891 this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9893 layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9894 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9895 this->rld_map_, ORDER_INVALID, false);
9897 // __RLD_MAP will be filled in by the runtime loader to contain
9898 // a pointer to the _r_debug structure.
9899 Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9900 Symbol_table::PREDEFINED,
9902 0, 0, elfcpp::STT_OBJECT,
9904 elfcpp::STV_DEFAULT, 0,
9907 if (!rld_map->is_forced_local())
9908 rld_map->set_needs_dynsym_entry();
9910 if (!parameters->options().pie())
9911 // This member holds the absolute address of the debug pointer.
9912 odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9914 // This member holds the offset to the debug pointer,
9915 // relative to the address of the tag.
9916 odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9921 // Get the custom dynamic tag value.
9922 template<int size, bool big_endian>
9924 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9928 case elfcpp::DT_MIPS_BASE_ADDRESS:
9930 // The base address of the segment.
9931 // At this point, the segment list has been sorted into final order,
9932 // so just return vaddr of the first readable PT_LOAD segment.
9933 Output_segment* seg =
9934 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9935 gold_assert(seg != NULL);
9936 return seg->vaddr();
9939 case elfcpp::DT_MIPS_SYMTABNO:
9940 // The number of entries in the .dynsym section.
9941 return this->get_dt_mips_symtabno();
9943 case elfcpp::DT_MIPS_GOTSYM:
9945 // The index of the first dynamic symbol table entry that corresponds
9946 // to an entry in the GOT.
9947 if (this->got_->first_global_got_dynsym_index() != -1U)
9948 return this->got_->first_global_got_dynsym_index();
9950 // In case if we don't have global GOT symbols we default to setting
9951 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9952 return this->get_dt_mips_symtabno();
9955 case elfcpp::DT_MIPS_RLD_MAP_REL:
9957 // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9958 // relative to the address of the tag.
9959 Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9960 unsigned int entry_offset =
9961 odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9962 gold_assert(entry_offset != -1U);
9963 return this->rld_map_->address() - (odyn->address() + entry_offset);
9966 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9969 return (unsigned int)-1;
9972 // Relocate section data.
9974 template<int size, bool big_endian>
9976 Target_mips<size, big_endian>::relocate_section(
9977 const Relocate_info<size, big_endian>* relinfo,
9978 unsigned int sh_type,
9979 const unsigned char* prelocs,
9981 Output_section* output_section,
9982 bool needs_special_offset_handling,
9983 unsigned char* view,
9984 Mips_address address,
9985 section_size_type view_size,
9986 const Reloc_symbol_changes* reloc_symbol_changes)
9988 typedef Target_mips<size, big_endian> Mips;
9989 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9991 if (sh_type == elfcpp::SHT_REL)
9993 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9996 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9997 gold::Default_comdat_behavior, Classify_reloc>(
10003 needs_special_offset_handling,
10007 reloc_symbol_changes);
10009 else if (sh_type == elfcpp::SHT_RELA)
10011 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10014 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
10015 gold::Default_comdat_behavior, Classify_reloc>(
10021 needs_special_offset_handling,
10025 reloc_symbol_changes);
10029 // Return the size of a relocation while scanning during a relocatable
10033 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
10037 case elfcpp::R_MIPS_NONE:
10038 case elfcpp::R_MIPS_TLS_DTPMOD64:
10039 case elfcpp::R_MIPS_TLS_DTPREL64:
10040 case elfcpp::R_MIPS_TLS_TPREL64:
10043 case elfcpp::R_MIPS_32:
10044 case elfcpp::R_MIPS_TLS_DTPMOD32:
10045 case elfcpp::R_MIPS_TLS_DTPREL32:
10046 case elfcpp::R_MIPS_TLS_TPREL32:
10047 case elfcpp::R_MIPS_REL32:
10048 case elfcpp::R_MIPS_PC32:
10049 case elfcpp::R_MIPS_GPREL32:
10050 case elfcpp::R_MIPS_JALR:
10051 case elfcpp::R_MIPS_EH:
10054 case elfcpp::R_MIPS_16:
10055 case elfcpp::R_MIPS_HI16:
10056 case elfcpp::R_MIPS_LO16:
10057 case elfcpp::R_MIPS_HIGHER:
10058 case elfcpp::R_MIPS_HIGHEST:
10059 case elfcpp::R_MIPS_GPREL16:
10060 case elfcpp::R_MIPS16_HI16:
10061 case elfcpp::R_MIPS16_LO16:
10062 case elfcpp::R_MIPS_PC16:
10063 case elfcpp::R_MIPS_PCHI16:
10064 case elfcpp::R_MIPS_PCLO16:
10065 case elfcpp::R_MIPS_GOT16:
10066 case elfcpp::R_MIPS16_GOT16:
10067 case elfcpp::R_MIPS_CALL16:
10068 case elfcpp::R_MIPS16_CALL16:
10069 case elfcpp::R_MIPS_GOT_HI16:
10070 case elfcpp::R_MIPS_CALL_HI16:
10071 case elfcpp::R_MIPS_GOT_LO16:
10072 case elfcpp::R_MIPS_CALL_LO16:
10073 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10074 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10075 case elfcpp::R_MIPS_TLS_TPREL_HI16:
10076 case elfcpp::R_MIPS_TLS_TPREL_LO16:
10077 case elfcpp::R_MIPS16_GPREL:
10078 case elfcpp::R_MIPS_GOT_DISP:
10079 case elfcpp::R_MIPS_LITERAL:
10080 case elfcpp::R_MIPS_GOT_PAGE:
10081 case elfcpp::R_MIPS_GOT_OFST:
10082 case elfcpp::R_MIPS_TLS_GD:
10083 case elfcpp::R_MIPS_TLS_LDM:
10084 case elfcpp::R_MIPS_TLS_GOTTPREL:
10087 // These relocations are not byte sized
10088 case elfcpp::R_MIPS_26:
10089 case elfcpp::R_MIPS16_26:
10090 case elfcpp::R_MIPS_PC21_S2:
10091 case elfcpp::R_MIPS_PC26_S2:
10092 case elfcpp::R_MIPS_PC18_S3:
10093 case elfcpp::R_MIPS_PC19_S2:
10096 case elfcpp::R_MIPS_COPY:
10097 case elfcpp::R_MIPS_JUMP_SLOT:
10098 object->error(_("unexpected reloc %u in object file"), r_type);
10102 object->error(_("unsupported reloc %u in object file"), r_type);
10107 // Scan the relocs during a relocatable link.
10109 template<int size, bool big_endian>
10111 Target_mips<size, big_endian>::scan_relocatable_relocs(
10112 Symbol_table* symtab,
10114 Sized_relobj_file<size, big_endian>* object,
10115 unsigned int data_shndx,
10116 unsigned int sh_type,
10117 const unsigned char* prelocs,
10118 size_t reloc_count,
10119 Output_section* output_section,
10120 bool needs_special_offset_handling,
10121 size_t local_symbol_count,
10122 const unsigned char* plocal_symbols,
10123 Relocatable_relocs* rr)
10125 if (sh_type == elfcpp::SHT_REL)
10127 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10129 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10130 Scan_relocatable_relocs;
10132 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10140 needs_special_offset_handling,
10141 local_symbol_count,
10145 else if (sh_type == elfcpp::SHT_RELA)
10147 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10149 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10150 Scan_relocatable_relocs;
10152 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10160 needs_special_offset_handling,
10161 local_symbol_count,
10166 gold_unreachable();
10169 // Scan the relocs for --emit-relocs.
10171 template<int size, bool big_endian>
10173 Target_mips<size, big_endian>::emit_relocs_scan(
10174 Symbol_table* symtab,
10176 Sized_relobj_file<size, big_endian>* object,
10177 unsigned int data_shndx,
10178 unsigned int sh_type,
10179 const unsigned char* prelocs,
10180 size_t reloc_count,
10181 Output_section* output_section,
10182 bool needs_special_offset_handling,
10183 size_t local_symbol_count,
10184 const unsigned char* plocal_syms,
10185 Relocatable_relocs* rr)
10187 if (sh_type == elfcpp::SHT_REL)
10189 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10191 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10192 Emit_relocs_strategy;
10194 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10202 needs_special_offset_handling,
10203 local_symbol_count,
10207 else if (sh_type == elfcpp::SHT_RELA)
10209 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10211 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10212 Emit_relocs_strategy;
10214 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10222 needs_special_offset_handling,
10223 local_symbol_count,
10228 gold_unreachable();
10231 // Emit relocations for a section.
10233 template<int size, bool big_endian>
10235 Target_mips<size, big_endian>::relocate_relocs(
10236 const Relocate_info<size, big_endian>* relinfo,
10237 unsigned int sh_type,
10238 const unsigned char* prelocs,
10239 size_t reloc_count,
10240 Output_section* output_section,
10241 typename elfcpp::Elf_types<size>::Elf_Off
10242 offset_in_output_section,
10243 unsigned char* view,
10244 Mips_address view_address,
10245 section_size_type view_size,
10246 unsigned char* reloc_view,
10247 section_size_type reloc_view_size)
10249 if (sh_type == elfcpp::SHT_REL)
10251 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10254 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10259 offset_in_output_section,
10266 else if (sh_type == elfcpp::SHT_RELA)
10268 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10271 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10276 offset_in_output_section,
10284 gold_unreachable();
10287 // Perform target-specific processing in a relocatable link. This is
10288 // only used if we use the relocation strategy RELOC_SPECIAL.
10290 template<int size, bool big_endian>
10292 Target_mips<size, big_endian>::relocate_special_relocatable(
10293 const Relocate_info<size, big_endian>* relinfo,
10294 unsigned int sh_type,
10295 const unsigned char* preloc_in,
10297 Output_section* output_section,
10298 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10299 unsigned char* view,
10300 Mips_address view_address,
10302 unsigned char* preloc_out)
10304 // We can only handle REL type relocation sections.
10305 gold_assert(sh_type == elfcpp::SHT_REL);
10307 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10309 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10312 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10314 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10316 Mips_relobj<size, big_endian>* object =
10317 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10318 const unsigned int local_count = object->local_symbol_count();
10320 Reltype reloc(preloc_in);
10321 Reltype_write reloc_write(preloc_out);
10323 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10324 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10325 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10327 // Get the new symbol index.
10328 // We only use RELOC_SPECIAL strategy in local relocations.
10329 gold_assert(r_sym < local_count);
10331 // We are adjusting a section symbol. We need to find
10332 // the symbol table index of the section symbol for
10333 // the output section corresponding to input section
10334 // in which this symbol is defined.
10336 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10337 gold_assert(is_ordinary);
10338 Output_section* os = object->output_section(shndx);
10339 gold_assert(os != NULL);
10340 gold_assert(os->needs_symtab_index());
10341 unsigned int new_symndx = os->symtab_index();
10343 // Get the new offset--the location in the output section where
10344 // this relocation should be applied.
10346 Mips_address offset = reloc.get_r_offset();
10347 Mips_address new_offset;
10348 if (offset_in_output_section != invalid_address)
10349 new_offset = offset + offset_in_output_section;
10352 section_offset_type sot_offset =
10353 convert_types<section_offset_type, Mips_address>(offset);
10354 section_offset_type new_sot_offset =
10355 output_section->output_offset(object, relinfo->data_shndx,
10357 gold_assert(new_sot_offset != -1);
10358 new_offset = new_sot_offset;
10361 // In an object file, r_offset is an offset within the section.
10362 // In an executable or dynamic object, generated by
10363 // --emit-relocs, r_offset is an absolute address.
10364 if (!parameters->options().relocatable())
10366 new_offset += view_address;
10367 if (offset_in_output_section != invalid_address)
10368 new_offset -= offset_in_output_section;
10371 reloc_write.put_r_offset(new_offset);
10372 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10374 // Handle the reloc addend.
10375 // The relocation uses a section symbol in the input file.
10376 // We are adjusting it to use a section symbol in the output
10377 // file. The input section symbol refers to some address in
10378 // the input section. We need the relocation in the output
10379 // file to refer to that same address. This adjustment to
10380 // the addend is the same calculation we use for a simple
10381 // absolute relocation for the input section symbol.
10382 Valtype calculated_value = 0;
10383 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10385 unsigned char* paddend = view + offset;
10386 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10389 case elfcpp::R_MIPS_26:
10390 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10391 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10392 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10393 false, &calculated_value);
10397 gold_unreachable();
10400 // Report any errors.
10401 switch (reloc_status)
10403 case Reloc_funcs::STATUS_OKAY:
10405 case Reloc_funcs::STATUS_OVERFLOW:
10406 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10407 _("relocation overflow: "
10408 "%u against local symbol %u in %s"),
10409 r_type, r_sym, object->name().c_str());
10411 case Reloc_funcs::STATUS_BAD_RELOC:
10412 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10413 _("unexpected opcode while processing relocation"));
10416 gold_unreachable();
10420 // Optimize the TLS relocation type based on what we know about the
10421 // symbol. IS_FINAL is true if the final address of this symbol is
10422 // known at link time.
10424 template<int size, bool big_endian>
10425 tls::Tls_optimization
10426 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10428 // FIXME: Currently we do not do any TLS optimization.
10429 return tls::TLSOPT_NONE;
10432 // Scan a relocation for a local symbol.
10434 template<int size, bool big_endian>
10436 Target_mips<size, big_endian>::Scan::local(
10437 Symbol_table* symtab,
10439 Target_mips<size, big_endian>* target,
10440 Sized_relobj_file<size, big_endian>* object,
10441 unsigned int data_shndx,
10442 Output_section* output_section,
10443 const Relatype* rela,
10444 const Reltype* rel,
10445 unsigned int rel_type,
10446 unsigned int r_type,
10447 const elfcpp::Sym<size, big_endian>& lsym,
10453 Mips_address r_offset;
10454 unsigned int r_sym;
10455 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10457 if (rel_type == elfcpp::SHT_RELA)
10459 r_offset = rela->get_r_offset();
10460 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10462 r_addend = rela->get_r_addend();
10466 r_offset = rel->get_r_offset();
10467 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10472 Mips_relobj<size, big_endian>* mips_obj =
10473 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10475 if (mips_obj->is_mips16_stub_section(data_shndx))
10477 mips_obj->get_mips16_stub_section(data_shndx)
10478 ->new_local_reloc_found(r_type, r_sym);
10481 if (r_type == elfcpp::R_MIPS_NONE)
10482 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10486 if (!mips16_call_reloc(r_type)
10487 && !mips_obj->section_allows_mips16_refs(data_shndx))
10488 // This reloc would need to refer to a MIPS16 hard-float stub, if
10489 // there is one. We ignore MIPS16 stub sections and .pdr section when
10490 // looking for relocs that would need to refer to MIPS16 stubs.
10491 mips_obj->add_local_non_16bit_call(r_sym);
10493 if (r_type == elfcpp::R_MIPS16_26
10494 && !mips_obj->section_allows_mips16_refs(data_shndx))
10495 mips_obj->add_local_16bit_call(r_sym);
10499 case elfcpp::R_MIPS_GOT16:
10500 case elfcpp::R_MIPS_CALL16:
10501 case elfcpp::R_MIPS_CALL_HI16:
10502 case elfcpp::R_MIPS_CALL_LO16:
10503 case elfcpp::R_MIPS_GOT_HI16:
10504 case elfcpp::R_MIPS_GOT_LO16:
10505 case elfcpp::R_MIPS_GOT_PAGE:
10506 case elfcpp::R_MIPS_GOT_OFST:
10507 case elfcpp::R_MIPS_GOT_DISP:
10508 case elfcpp::R_MIPS_TLS_GOTTPREL:
10509 case elfcpp::R_MIPS_TLS_GD:
10510 case elfcpp::R_MIPS_TLS_LDM:
10511 case elfcpp::R_MIPS16_GOT16:
10512 case elfcpp::R_MIPS16_CALL16:
10513 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10514 case elfcpp::R_MIPS16_TLS_GD:
10515 case elfcpp::R_MIPS16_TLS_LDM:
10516 case elfcpp::R_MICROMIPS_GOT16:
10517 case elfcpp::R_MICROMIPS_CALL16:
10518 case elfcpp::R_MICROMIPS_CALL_HI16:
10519 case elfcpp::R_MICROMIPS_CALL_LO16:
10520 case elfcpp::R_MICROMIPS_GOT_HI16:
10521 case elfcpp::R_MICROMIPS_GOT_LO16:
10522 case elfcpp::R_MICROMIPS_GOT_PAGE:
10523 case elfcpp::R_MICROMIPS_GOT_OFST:
10524 case elfcpp::R_MICROMIPS_GOT_DISP:
10525 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10526 case elfcpp::R_MICROMIPS_TLS_GD:
10527 case elfcpp::R_MICROMIPS_TLS_LDM:
10528 case elfcpp::R_MIPS_EH:
10529 // We need a GOT section.
10530 target->got_section(symtab, layout);
10537 if (call_lo16_reloc(r_type)
10538 || got_lo16_reloc(r_type)
10539 || got_disp_reloc(r_type)
10540 || eh_reloc(r_type))
10542 // We may need a local GOT entry for this relocation. We
10543 // don't count R_MIPS_GOT_PAGE because we can estimate the
10544 // maximum number of pages needed by looking at the size of
10545 // the segment. Similar comments apply to R_MIPS*_GOT16 and
10546 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
10547 // R_MIPS_CALL_HI16 because these are always followed by an
10548 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10549 Mips_output_data_got<size, big_endian>* got =
10550 target->got_section(symtab, layout);
10551 bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10552 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10553 is_section_symbol);
10558 case elfcpp::R_MIPS_CALL16:
10559 case elfcpp::R_MIPS16_CALL16:
10560 case elfcpp::R_MICROMIPS_CALL16:
10561 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10562 (unsigned long)r_offset);
10565 case elfcpp::R_MIPS_GOT_PAGE:
10566 case elfcpp::R_MICROMIPS_GOT_PAGE:
10567 case elfcpp::R_MIPS16_GOT16:
10568 case elfcpp::R_MIPS_GOT16:
10569 case elfcpp::R_MIPS_GOT_HI16:
10570 case elfcpp::R_MIPS_GOT_LO16:
10571 case elfcpp::R_MICROMIPS_GOT16:
10572 case elfcpp::R_MICROMIPS_GOT_HI16:
10573 case elfcpp::R_MICROMIPS_GOT_LO16:
10575 // This relocation needs a page entry in the GOT.
10576 // Get the section contents.
10577 section_size_type view_size = 0;
10578 const unsigned char* view = object->section_contents(data_shndx,
10579 &view_size, false);
10582 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10583 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10586 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10587 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10588 object, data_shndx, r_type, r_sym, addend));
10590 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10594 case elfcpp::R_MIPS_HI16:
10595 case elfcpp::R_MIPS_PCHI16:
10596 case elfcpp::R_MIPS16_HI16:
10597 case elfcpp::R_MICROMIPS_HI16:
10598 // Record the reloc so that we can check whether the corresponding LO16
10600 if (rel_type == elfcpp::SHT_REL)
10601 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10602 object, data_shndx, r_type, r_sym, 0));
10605 case elfcpp::R_MIPS_LO16:
10606 case elfcpp::R_MIPS_PCLO16:
10607 case elfcpp::R_MIPS16_LO16:
10608 case elfcpp::R_MICROMIPS_LO16:
10610 if (rel_type != elfcpp::SHT_REL)
10613 // Find corresponding GOT16/HI16 relocation.
10615 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10616 // be immediately following. However, for the IRIX6 ABI, the next
10617 // relocation may be a composed relocation consisting of several
10618 // relocations for the same address. In that case, the R_MIPS_LO16
10619 // relocation may occur as one of these. We permit a similar
10620 // extension in general, as that is useful for GCC.
10622 // In some cases GCC dead code elimination removes the LO16 but
10623 // keeps the corresponding HI16. This is strictly speaking a
10624 // violation of the ABI but not immediately harmful.
10626 typename std::list<got16_addend<size, big_endian> >::iterator it =
10627 target->got16_addends_.begin();
10628 while (it != target->got16_addends_.end())
10630 got16_addend<size, big_endian> _got16_addend = *it;
10632 // TODO(sasa): Split got16_addends_ list into two lists - one for
10633 // GOT16 relocs and the other for HI16 relocs.
10635 // Report an error if we find HI16 or GOT16 reloc from the
10636 // previous section without the matching LO16 part.
10637 if (_got16_addend.object != object
10638 || _got16_addend.shndx != data_shndx)
10640 gold_error("Can't find matching LO16 reloc");
10644 if (_got16_addend.r_sym != r_sym
10645 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10651 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10652 // For GOT16, we need to calculate combined addend and record GOT page
10654 if (got16_reloc(_got16_addend.r_type))
10657 section_size_type view_size = 0;
10658 const unsigned char* view = object->section_contents(data_shndx,
10663 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10664 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10666 addend = (_got16_addend.addend << 16) + addend;
10667 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10671 it = target->got16_addends_.erase(it);
10679 case elfcpp::R_MIPS_32:
10680 case elfcpp::R_MIPS_REL32:
10681 case elfcpp::R_MIPS_64:
10683 if (parameters->options().output_is_position_independent())
10685 // If building a shared library (or a position-independent
10686 // executable), we need to create a dynamic relocation for
10688 if (is_readonly_section(output_section))
10690 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10691 rel_dyn->add_symbolless_local_addend(object, r_sym,
10692 elfcpp::R_MIPS_REL32,
10693 output_section, data_shndx,
10699 case elfcpp::R_MIPS_TLS_GOTTPREL:
10700 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10701 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10702 case elfcpp::R_MIPS_TLS_LDM:
10703 case elfcpp::R_MIPS16_TLS_LDM:
10704 case elfcpp::R_MICROMIPS_TLS_LDM:
10705 case elfcpp::R_MIPS_TLS_GD:
10706 case elfcpp::R_MIPS16_TLS_GD:
10707 case elfcpp::R_MICROMIPS_TLS_GD:
10709 bool output_is_shared = parameters->options().shared();
10710 const tls::Tls_optimization optimized_type
10711 = Target_mips<size, big_endian>::optimize_tls_reloc(
10712 !output_is_shared, r_type);
10715 case elfcpp::R_MIPS_TLS_GD:
10716 case elfcpp::R_MIPS16_TLS_GD:
10717 case elfcpp::R_MICROMIPS_TLS_GD:
10718 if (optimized_type == tls::TLSOPT_NONE)
10720 // Create a pair of GOT entries for the module index and
10721 // dtv-relative offset.
10722 Mips_output_data_got<size, big_endian>* got =
10723 target->got_section(symtab, layout);
10724 unsigned int shndx = lsym.get_st_shndx();
10726 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10729 object->error(_("local symbol %u has bad shndx %u"),
10733 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10738 // FIXME: TLS optimization not supported yet.
10739 gold_unreachable();
10743 case elfcpp::R_MIPS_TLS_LDM:
10744 case elfcpp::R_MIPS16_TLS_LDM:
10745 case elfcpp::R_MICROMIPS_TLS_LDM:
10746 if (optimized_type == tls::TLSOPT_NONE)
10748 // We always record LDM symbols as local with index 0.
10749 target->got_section()->record_local_got_symbol(mips_obj, 0,
10755 // FIXME: TLS optimization not supported yet.
10756 gold_unreachable();
10759 case elfcpp::R_MIPS_TLS_GOTTPREL:
10760 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10761 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10762 layout->set_has_static_tls();
10763 if (optimized_type == tls::TLSOPT_NONE)
10765 // Create a GOT entry for the tp-relative offset.
10766 Mips_output_data_got<size, big_endian>* got =
10767 target->got_section(symtab, layout);
10768 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10773 // FIXME: TLS optimization not supported yet.
10774 gold_unreachable();
10779 gold_unreachable();
10788 // Refuse some position-dependent relocations when creating a
10789 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10790 // not PIC, but we can create dynamic relocations and the result
10791 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10792 // combined with R_MIPS_GOT16.
10793 if (parameters->options().shared())
10797 case elfcpp::R_MIPS16_HI16:
10798 case elfcpp::R_MIPS_HI16:
10799 case elfcpp::R_MIPS_HIGHER:
10800 case elfcpp::R_MIPS_HIGHEST:
10801 case elfcpp::R_MICROMIPS_HI16:
10802 case elfcpp::R_MICROMIPS_HIGHER:
10803 case elfcpp::R_MICROMIPS_HIGHEST:
10804 // Don't refuse a high part relocation if it's against
10805 // no symbol (e.g. part of a compound relocation).
10810 case elfcpp::R_MIPS16_26:
10811 case elfcpp::R_MIPS_26:
10812 case elfcpp::R_MICROMIPS_26_S1:
10813 gold_error(_("%s: relocation %u against `%s' can not be used when "
10814 "making a shared object; recompile with -fPIC"),
10815 object->name().c_str(), r_type, "a local symbol");
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 Reltype& reloc,
10832 unsigned int r_type,
10833 const elfcpp::Sym<size, big_endian>& lsym,
10846 (const Relatype*) NULL,
10850 lsym, is_discarded);
10854 template<int size, bool big_endian>
10856 Target_mips<size, big_endian>::Scan::local(
10857 Symbol_table* symtab,
10859 Target_mips<size, big_endian>* target,
10860 Sized_relobj_file<size, big_endian>* object,
10861 unsigned int data_shndx,
10862 Output_section* output_section,
10863 const Relatype& reloc,
10864 unsigned int r_type,
10865 const elfcpp::Sym<size, big_endian>& lsym,
10879 (const Reltype*) NULL,
10882 lsym, is_discarded);
10885 // Scan a relocation for a global symbol.
10887 template<int size, bool big_endian>
10889 Target_mips<size, big_endian>::Scan::global(
10890 Symbol_table* symtab,
10892 Target_mips<size, big_endian>* target,
10893 Sized_relobj_file<size, big_endian>* object,
10894 unsigned int data_shndx,
10895 Output_section* output_section,
10896 const Relatype* rela,
10897 const Reltype* rel,
10898 unsigned int rel_type,
10899 unsigned int r_type,
10902 Mips_address r_offset;
10903 unsigned int r_sym;
10904 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10906 if (rel_type == elfcpp::SHT_RELA)
10908 r_offset = rela->get_r_offset();
10909 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10911 r_addend = rela->get_r_addend();
10915 r_offset = rel->get_r_offset();
10916 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10921 Mips_relobj<size, big_endian>* mips_obj =
10922 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10923 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10925 if (mips_obj->is_mips16_stub_section(data_shndx))
10927 mips_obj->get_mips16_stub_section(data_shndx)
10928 ->new_global_reloc_found(r_type, mips_sym);
10931 if (r_type == elfcpp::R_MIPS_NONE)
10932 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10936 if (!mips16_call_reloc(r_type)
10937 && !mips_obj->section_allows_mips16_refs(data_shndx))
10938 // This reloc would need to refer to a MIPS16 hard-float stub, if
10939 // there is one. We ignore MIPS16 stub sections and .pdr section when
10940 // looking for relocs that would need to refer to MIPS16 stubs.
10941 mips_sym->set_need_fn_stub();
10943 // We need PLT entries if there are static-only relocations against
10944 // an externally-defined function. This can technically occur for
10945 // shared libraries if there are branches to the symbol, although it
10946 // is unlikely that this will be used in practice due to the short
10947 // ranges involved. It can occur for any relative or absolute relocation
10948 // in executables; in that case, the PLT entry becomes the function's
10949 // canonical address.
10950 bool static_reloc = false;
10952 // Set CAN_MAKE_DYNAMIC to true if we can convert this
10953 // relocation into a dynamic one.
10954 bool can_make_dynamic = false;
10957 case elfcpp::R_MIPS_GOT16:
10958 case elfcpp::R_MIPS_CALL16:
10959 case elfcpp::R_MIPS_CALL_HI16:
10960 case elfcpp::R_MIPS_CALL_LO16:
10961 case elfcpp::R_MIPS_GOT_HI16:
10962 case elfcpp::R_MIPS_GOT_LO16:
10963 case elfcpp::R_MIPS_GOT_PAGE:
10964 case elfcpp::R_MIPS_GOT_OFST:
10965 case elfcpp::R_MIPS_GOT_DISP:
10966 case elfcpp::R_MIPS_TLS_GOTTPREL:
10967 case elfcpp::R_MIPS_TLS_GD:
10968 case elfcpp::R_MIPS_TLS_LDM:
10969 case elfcpp::R_MIPS16_GOT16:
10970 case elfcpp::R_MIPS16_CALL16:
10971 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10972 case elfcpp::R_MIPS16_TLS_GD:
10973 case elfcpp::R_MIPS16_TLS_LDM:
10974 case elfcpp::R_MICROMIPS_GOT16:
10975 case elfcpp::R_MICROMIPS_CALL16:
10976 case elfcpp::R_MICROMIPS_CALL_HI16:
10977 case elfcpp::R_MICROMIPS_CALL_LO16:
10978 case elfcpp::R_MICROMIPS_GOT_HI16:
10979 case elfcpp::R_MICROMIPS_GOT_LO16:
10980 case elfcpp::R_MICROMIPS_GOT_PAGE:
10981 case elfcpp::R_MICROMIPS_GOT_OFST:
10982 case elfcpp::R_MICROMIPS_GOT_DISP:
10983 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10984 case elfcpp::R_MICROMIPS_TLS_GD:
10985 case elfcpp::R_MICROMIPS_TLS_LDM:
10986 case elfcpp::R_MIPS_EH:
10987 // We need a GOT section.
10988 target->got_section(symtab, layout);
10991 // This is just a hint; it can safely be ignored. Don't set
10992 // has_static_relocs for the corresponding symbol.
10993 case elfcpp::R_MIPS_JALR:
10994 case elfcpp::R_MICROMIPS_JALR:
10997 case elfcpp::R_MIPS_GPREL16:
10998 case elfcpp::R_MIPS_GPREL32:
10999 case elfcpp::R_MIPS16_GPREL:
11000 case elfcpp::R_MICROMIPS_GPREL16:
11002 // GP-relative relocations always resolve to a definition in a
11003 // regular input file, ignoring the one-definition rule. This is
11004 // important for the GP setup sequence in NewABI code, which
11005 // always resolves to a local function even if other relocations
11006 // against the symbol wouldn't.
11007 //constrain_symbol_p = FALSE;
11010 case elfcpp::R_MIPS_32:
11011 case elfcpp::R_MIPS_REL32:
11012 case elfcpp::R_MIPS_64:
11013 if ((parameters->options().shared()
11014 || (strcmp(gsym->name(), "__gnu_local_gp") != 0
11015 && (!is_readonly_section(output_section)
11016 || mips_obj->is_pic())))
11017 && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
11019 if (r_type != elfcpp::R_MIPS_REL32)
11020 mips_sym->set_pointer_equality_needed();
11021 can_make_dynamic = true;
11027 // Most static relocations require pointer equality, except
11029 mips_sym->set_pointer_equality_needed();
11032 case elfcpp::R_MIPS_26:
11033 case elfcpp::R_MIPS_PC16:
11034 case elfcpp::R_MIPS_PC21_S2:
11035 case elfcpp::R_MIPS_PC26_S2:
11036 case elfcpp::R_MIPS16_26:
11037 case elfcpp::R_MICROMIPS_26_S1:
11038 case elfcpp::R_MICROMIPS_PC7_S1:
11039 case elfcpp::R_MICROMIPS_PC10_S1:
11040 case elfcpp::R_MICROMIPS_PC16_S1:
11041 case elfcpp::R_MICROMIPS_PC23_S2:
11042 static_reloc = true;
11043 mips_sym->set_has_static_relocs();
11047 // If there are call relocations against an externally-defined symbol,
11048 // see whether we can create a MIPS lazy-binding stub for it. We can
11049 // only do this if all references to the function are through call
11050 // relocations, and in that case, the traditional lazy-binding stubs
11051 // are much more efficient than PLT entries.
11054 case elfcpp::R_MIPS16_CALL16:
11055 case elfcpp::R_MIPS_CALL16:
11056 case elfcpp::R_MIPS_CALL_HI16:
11057 case elfcpp::R_MIPS_CALL_LO16:
11058 case elfcpp::R_MIPS_JALR:
11059 case elfcpp::R_MICROMIPS_CALL16:
11060 case elfcpp::R_MICROMIPS_CALL_HI16:
11061 case elfcpp::R_MICROMIPS_CALL_LO16:
11062 case elfcpp::R_MICROMIPS_JALR:
11063 if (!mips_sym->no_lazy_stub())
11065 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11066 // Calls from shared objects to undefined symbols of type
11067 // STT_NOTYPE need lazy-binding stub.
11068 || (mips_sym->is_undefined() && parameters->options().shared()))
11069 target->mips_stubs_section(layout)->make_entry(mips_sym);
11074 // We must not create a stub for a symbol that has relocations
11075 // related to taking the function's address.
11076 mips_sym->set_no_lazy_stub();
11077 target->remove_lazy_stub_entry(mips_sym);
11082 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11083 mips_sym->is_mips16()))
11084 mips_sym->set_has_nonpic_branches();
11086 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11087 // and has a special meaning.
11088 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11089 && strcmp(gsym->name(), "_gp_disp") == 0
11090 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11091 if (static_reloc && gsym->needs_plt_entry())
11093 target->make_plt_entry(symtab, layout, mips_sym, r_type);
11095 // Since this is not a PC-relative relocation, we may be
11096 // taking the address of a function. In that case we need to
11097 // set the entry in the dynamic symbol table to the address of
11099 if (gsym->is_from_dynobj() && !parameters->options().shared())
11101 gsym->set_needs_dynsym_value();
11102 // We distinguish between PLT entries and lazy-binding stubs by
11103 // giving the former an st_other value of STO_MIPS_PLT. Set the
11104 // flag if there are any relocations in the binary where pointer
11105 // equality matters.
11106 if (mips_sym->pointer_equality_needed())
11107 mips_sym->set_mips_plt();
11110 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11112 // Absolute addressing relocations.
11113 // Make a dynamic relocation if necessary.
11114 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11116 if (gsym->may_need_copy_reloc())
11118 target->copy_reloc(symtab, layout, object, data_shndx,
11119 output_section, gsym, r_type, r_offset);
11121 else if (can_make_dynamic)
11123 // Create .rel.dyn section.
11124 target->rel_dyn_section(layout);
11125 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11126 data_shndx, output_section, r_offset);
11129 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11134 bool for_call = false;
11137 case elfcpp::R_MIPS_CALL16:
11138 case elfcpp::R_MIPS16_CALL16:
11139 case elfcpp::R_MICROMIPS_CALL16:
11140 case elfcpp::R_MIPS_CALL_HI16:
11141 case elfcpp::R_MIPS_CALL_LO16:
11142 case elfcpp::R_MICROMIPS_CALL_HI16:
11143 case elfcpp::R_MICROMIPS_CALL_LO16:
11147 case elfcpp::R_MIPS16_GOT16:
11148 case elfcpp::R_MIPS_GOT16:
11149 case elfcpp::R_MIPS_GOT_HI16:
11150 case elfcpp::R_MIPS_GOT_LO16:
11151 case elfcpp::R_MICROMIPS_GOT16:
11152 case elfcpp::R_MICROMIPS_GOT_HI16:
11153 case elfcpp::R_MICROMIPS_GOT_LO16:
11154 case elfcpp::R_MIPS_GOT_DISP:
11155 case elfcpp::R_MICROMIPS_GOT_DISP:
11156 case elfcpp::R_MIPS_EH:
11158 // The symbol requires a GOT entry.
11159 Mips_output_data_got<size, big_endian>* got =
11160 target->got_section(symtab, layout);
11161 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11163 mips_sym->set_global_got_area(GGA_NORMAL);
11167 case elfcpp::R_MIPS_GOT_PAGE:
11168 case elfcpp::R_MICROMIPS_GOT_PAGE:
11170 // This relocation needs a page entry in the GOT.
11171 // Get the section contents.
11172 section_size_type view_size = 0;
11173 const unsigned char* view =
11174 object->section_contents(data_shndx, &view_size, false);
11177 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11178 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11180 Mips_output_data_got<size, big_endian>* got =
11181 target->got_section(symtab, layout);
11182 got->record_got_page_entry(mips_obj, r_sym, addend);
11184 // If this is a global, overridable symbol, GOT_PAGE will
11185 // decay to GOT_DISP, so we'll need a GOT entry for it.
11186 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11187 && !mips_sym->object()->is_dynamic()
11188 && !mips_sym->is_undefined());
11190 || (parameters->options().output_is_position_independent()
11191 && !parameters->options().Bsymbolic()
11192 && !mips_sym->is_forced_local()))
11194 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11196 mips_sym->set_global_got_area(GGA_NORMAL);
11201 case elfcpp::R_MIPS_TLS_GOTTPREL:
11202 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11203 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11204 case elfcpp::R_MIPS_TLS_LDM:
11205 case elfcpp::R_MIPS16_TLS_LDM:
11206 case elfcpp::R_MICROMIPS_TLS_LDM:
11207 case elfcpp::R_MIPS_TLS_GD:
11208 case elfcpp::R_MIPS16_TLS_GD:
11209 case elfcpp::R_MICROMIPS_TLS_GD:
11211 const bool is_final = gsym->final_value_is_known();
11212 const tls::Tls_optimization optimized_type =
11213 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11217 case elfcpp::R_MIPS_TLS_GD:
11218 case elfcpp::R_MIPS16_TLS_GD:
11219 case elfcpp::R_MICROMIPS_TLS_GD:
11220 if (optimized_type == tls::TLSOPT_NONE)
11222 // Create a pair of GOT entries for the module index and
11223 // dtv-relative offset.
11224 Mips_output_data_got<size, big_endian>* got =
11225 target->got_section(symtab, layout);
11226 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11231 // FIXME: TLS optimization not supported yet.
11232 gold_unreachable();
11236 case elfcpp::R_MIPS_TLS_LDM:
11237 case elfcpp::R_MIPS16_TLS_LDM:
11238 case elfcpp::R_MICROMIPS_TLS_LDM:
11239 if (optimized_type == tls::TLSOPT_NONE)
11241 // We always record LDM symbols as local with index 0.
11242 target->got_section()->record_local_got_symbol(mips_obj, 0,
11248 // FIXME: TLS optimization not supported yet.
11249 gold_unreachable();
11252 case elfcpp::R_MIPS_TLS_GOTTPREL:
11253 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11254 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11255 layout->set_has_static_tls();
11256 if (optimized_type == tls::TLSOPT_NONE)
11258 // Create a GOT entry for the tp-relative offset.
11259 Mips_output_data_got<size, big_endian>* got =
11260 target->got_section(symtab, layout);
11261 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11266 // FIXME: TLS optimization not supported yet.
11267 gold_unreachable();
11272 gold_unreachable();
11276 case elfcpp::R_MIPS_COPY:
11277 case elfcpp::R_MIPS_JUMP_SLOT:
11278 // These are relocations which should only be seen by the
11279 // dynamic linker, and should never be seen here.
11280 gold_error(_("%s: unexpected reloc %u in object file"),
11281 object->name().c_str(), r_type);
11288 // Refuse some position-dependent relocations when creating a
11289 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
11290 // not PIC, but we can create dynamic relocations and the result
11291 // will be fine. Also do not refuse R_MIPS_LO16, which can be
11292 // combined with R_MIPS_GOT16.
11293 if (parameters->options().shared())
11297 case elfcpp::R_MIPS16_HI16:
11298 case elfcpp::R_MIPS_HI16:
11299 case elfcpp::R_MIPS_HIGHER:
11300 case elfcpp::R_MIPS_HIGHEST:
11301 case elfcpp::R_MICROMIPS_HI16:
11302 case elfcpp::R_MICROMIPS_HIGHER:
11303 case elfcpp::R_MICROMIPS_HIGHEST:
11304 // Don't refuse a high part relocation if it's against
11305 // no symbol (e.g. part of a compound relocation).
11309 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11310 // and has a special meaning.
11311 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11315 case elfcpp::R_MIPS16_26:
11316 case elfcpp::R_MIPS_26:
11317 case elfcpp::R_MICROMIPS_26_S1:
11318 gold_error(_("%s: relocation %u against `%s' can not be used when "
11319 "making a shared object; recompile with -fPIC"),
11320 object->name().c_str(), r_type, gsym->name());
11327 template<int size, bool big_endian>
11329 Target_mips<size, big_endian>::Scan::global(
11330 Symbol_table* symtab,
11332 Target_mips<size, big_endian>* target,
11333 Sized_relobj_file<size, big_endian>* object,
11334 unsigned int data_shndx,
11335 Output_section* output_section,
11336 const Relatype& reloc,
11337 unsigned int r_type,
11348 (const Reltype*) NULL,
11354 template<int size, bool big_endian>
11356 Target_mips<size, big_endian>::Scan::global(
11357 Symbol_table* symtab,
11359 Target_mips<size, big_endian>* target,
11360 Sized_relobj_file<size, big_endian>* object,
11361 unsigned int data_shndx,
11362 Output_section* output_section,
11363 const Reltype& reloc,
11364 unsigned int r_type,
11374 (const Relatype*) NULL,
11381 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11382 // In cases where Scan::local() or Scan::global() has created
11383 // a dynamic relocation, the addend of the relocation is carried
11384 // in the data, and we must not apply the static relocation.
11386 template<int size, bool big_endian>
11388 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11389 const Mips_symbol<size>* gsym,
11390 unsigned int r_type,
11391 Output_section* output_section,
11392 Target_mips* target)
11394 // If the output section is not allocated, then we didn't call
11395 // scan_relocs, we didn't create a dynamic reloc, and we must apply
11397 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11404 // For global symbols, we use the same helper routines used in the
11406 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11407 && !gsym->may_need_copy_reloc())
11409 // We have generated dynamic reloc (R_MIPS_REL32).
11411 bool multi_got = false;
11412 if (target->has_got_section())
11413 multi_got = target->got_section()->multi_got();
11414 bool has_got_offset;
11416 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11418 has_got_offset = gsym->global_gotoffset() != -1U;
11419 if (!has_got_offset)
11422 // Apply the relocation only if the symbol is in the local got.
11423 // Do not apply the relocation if the symbol is in the global
11425 return symbol_references_local(gsym, gsym->has_dynsym_index());
11428 // We have not generated dynamic reloc.
11433 // Perform a relocation.
11435 template<int size, bool big_endian>
11437 Target_mips<size, big_endian>::Relocate::relocate(
11438 const Relocate_info<size, big_endian>* relinfo,
11439 unsigned int rel_type,
11440 Target_mips* target,
11441 Output_section* output_section,
11443 const unsigned char* preloc,
11444 const Sized_symbol<size>* gsym,
11445 const Symbol_value<size>* psymval,
11446 unsigned char* view,
11447 Mips_address address,
11450 Mips_address r_offset;
11451 unsigned int r_sym;
11452 unsigned int r_type;
11453 unsigned int r_type2;
11454 unsigned int r_type3;
11455 unsigned char r_ssym;
11456 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11457 // r_offset and r_type of the next relocation is needed for resolving multiple
11458 // consecutive relocations with the same offset.
11459 Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11460 unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11462 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11463 size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
11465 if (rel_type == elfcpp::SHT_RELA)
11467 const Relatype rela(preloc);
11468 r_offset = rela.get_r_offset();
11469 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11471 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11473 r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11474 get_r_type2(&rela);
11475 r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11476 get_r_type3(&rela);
11477 r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11479 r_addend = rela.get_r_addend();
11480 // If this is not last relocation, get r_offset and r_type of the next
11482 if (relnum + 1 < reloc_count)
11484 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11485 const Relatype next_rela(preloc + reloc_size);
11486 next_r_offset = next_rela.get_r_offset();
11488 Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11489 get_r_type(&next_rela);
11494 const Reltype rel(preloc);
11495 r_offset = rel.get_r_offset();
11496 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11498 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11501 r_type2 = elfcpp::R_MIPS_NONE;
11502 r_type3 = elfcpp::R_MIPS_NONE;
11504 // If this is not last relocation, get r_offset and r_type of the next
11506 if (relnum + 1 < reloc_count)
11508 const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11509 const Reltype next_rel(preloc + reloc_size);
11510 next_r_offset = next_rel.get_r_offset();
11511 next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11512 get_r_type(&next_rel);
11516 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11517 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11519 Mips_relobj<size, big_endian>* object =
11520 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11522 bool target_is_16_bit_code = false;
11523 bool target_is_micromips_code = false;
11524 bool cross_mode_jump;
11526 Symbol_value<size> symval;
11528 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11530 bool changed_symbol_value = false;
11533 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11534 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11535 if (target_is_16_bit_code || target_is_micromips_code)
11537 // MIPS16/microMIPS text labels should be treated as odd.
11538 symval.set_output_value(psymval->value(object, 1));
11540 changed_symbol_value = true;
11545 target_is_16_bit_code = mips_sym->is_mips16();
11546 target_is_micromips_code = mips_sym->is_micromips();
11548 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11549 // it odd. This will cause something like .word SYM to come up with
11550 // the right value when it is loaded into the PC.
11552 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11553 && psymval->value(object, 0) != 0)
11555 symval.set_output_value(psymval->value(object, 0) | 1);
11557 changed_symbol_value = true;
11560 // Pick the value to use for symbols defined in shared objects.
11561 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11562 || mips_sym->has_lazy_stub())
11564 Mips_address value;
11565 if (!mips_sym->has_lazy_stub())
11567 // Prefer a standard MIPS PLT entry.
11568 if (mips_sym->has_mips_plt_offset())
11570 value = target->plt_section()->mips_entry_address(mips_sym);
11571 target_is_micromips_code = false;
11572 target_is_16_bit_code = false;
11576 value = (target->plt_section()->comp_entry_address(mips_sym)
11578 if (target->is_output_micromips())
11579 target_is_micromips_code = true;
11581 target_is_16_bit_code = true;
11585 value = target->mips_stubs_section()->stub_address(mips_sym);
11587 symval.set_output_value(value);
11592 // TRUE if the symbol referred to by this relocation is "_gp_disp".
11593 // Note that such a symbol must always be a global symbol.
11594 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11595 && !object->is_newabi());
11597 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11598 // Note that such a symbol must always be a global symbol.
11599 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11604 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11605 gold_error_at_location(relinfo, relnum, r_offset,
11606 _("relocations against _gp_disp are permitted only"
11607 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11609 else if (gnu_local_gp)
11611 // __gnu_local_gp is _gp symbol.
11612 symval.set_output_value(target->adjusted_gp_value(object));
11616 // If this is a reference to a 16-bit function with a stub, we need
11617 // to redirect the relocation to the stub unless:
11619 // (a) the relocation is for a MIPS16 JAL;
11621 // (b) the relocation is for a MIPS16 PIC call, and there are no
11622 // non-MIPS16 uses of the GOT slot; or
11624 // (c) the section allows direct references to MIPS16 functions.
11625 if (r_type != elfcpp::R_MIPS16_26
11626 && ((mips_sym != NULL
11627 && mips_sym->has_mips16_fn_stub()
11628 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11629 || (mips_sym == NULL
11630 && object->get_local_mips16_fn_stub(r_sym) != NULL))
11631 && !object->section_allows_mips16_refs(relinfo->data_shndx))
11633 // This is a 32- or 64-bit call to a 16-bit function. We should
11634 // have already noticed that we were going to need the
11636 Mips_address value;
11637 if (mips_sym == NULL)
11638 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11641 gold_assert(mips_sym->need_fn_stub());
11642 if (mips_sym->has_la25_stub())
11643 value = target->la25_stub_section()->stub_address(mips_sym);
11646 value = mips_sym->template
11647 get_mips16_fn_stub<big_endian>()->output_address();
11650 symval.set_output_value(value);
11652 changed_symbol_value = true;
11654 // The target is 16-bit, but the stub isn't.
11655 target_is_16_bit_code = false;
11657 // If this is a MIPS16 call with a stub, that is made through the PLT or
11658 // to a standard MIPS function, we need to redirect the call to the stub.
11659 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11660 // indirect calls should use an indirect stub instead.
11661 else if (r_type == elfcpp::R_MIPS16_26
11662 && ((mips_sym != NULL
11663 && (mips_sym->has_mips16_call_stub()
11664 || mips_sym->has_mips16_call_fp_stub()))
11665 || (mips_sym == NULL
11666 && object->get_local_mips16_call_stub(r_sym) != NULL))
11667 && ((mips_sym != NULL && mips_sym->has_plt_offset())
11668 || !target_is_16_bit_code))
11670 Mips16_stub_section<size, big_endian>* call_stub;
11671 if (mips_sym == NULL)
11672 call_stub = object->get_local_mips16_call_stub(r_sym);
11675 // If both call_stub and call_fp_stub are defined, we can figure
11676 // out which one to use by checking which one appears in the input
11678 if (mips_sym->has_mips16_call_stub()
11679 && mips_sym->has_mips16_call_fp_stub())
11682 for (unsigned int i = 1; i < object->shnum(); ++i)
11684 if (object->is_mips16_call_fp_stub_section(i))
11686 call_stub = mips_sym->template
11687 get_mips16_call_fp_stub<big_endian>();
11692 if (call_stub == NULL)
11694 mips_sym->template get_mips16_call_stub<big_endian>();
11696 else if (mips_sym->has_mips16_call_stub())
11697 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11699 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11702 symval.set_output_value(call_stub->output_address());
11704 changed_symbol_value = true;
11706 // If this is a direct call to a PIC function, redirect to the
11708 else if (mips_sym != NULL
11709 && mips_sym->has_la25_stub()
11710 && relocation_needs_la25_stub<size, big_endian>(
11711 object, r_type, target_is_16_bit_code))
11713 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11714 if (mips_sym->is_micromips())
11716 symval.set_output_value(value);
11719 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11720 // entry is used if a standard PLT entry has also been made.
11721 else if ((r_type == elfcpp::R_MIPS16_26
11722 || r_type == elfcpp::R_MICROMIPS_26_S1)
11723 && mips_sym != NULL
11724 && mips_sym->has_plt_offset()
11725 && mips_sym->has_comp_plt_offset()
11726 && mips_sym->has_mips_plt_offset())
11728 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11730 symval.set_output_value(value);
11733 target_is_16_bit_code = !target->is_output_micromips();
11734 target_is_micromips_code = target->is_output_micromips();
11737 // Make sure MIPS16 and microMIPS are not used together.
11738 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11739 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11741 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11744 // Calls from 16-bit code to 32-bit code and vice versa require the
11745 // mode change. However, we can ignore calls to undefined weak symbols,
11746 // which should never be executed at runtime. This exception is important
11747 // because the assembly writer may have "known" that any definition of the
11748 // symbol would be 16-bit code, and that direct jumps were therefore
11751 (!(gsym != NULL && gsym->is_weak_undefined())
11752 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11753 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11754 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11755 && (target_is_16_bit_code || target_is_micromips_code))));
11757 bool local = (mips_sym == NULL
11758 || (mips_sym->got_only_for_calls()
11759 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11760 : symbol_references_local(mips_sym,
11761 mips_sym->has_dynsym_index())));
11763 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11764 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
11765 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11766 if (got_page_reloc(r_type) && !local)
11767 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11768 : elfcpp::R_MIPS_GOT_DISP);
11770 unsigned int got_offset = 0;
11773 // Whether we have to extract addend from instruction.
11774 bool extract_addend = rel_type == elfcpp::SHT_REL;
11775 unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11777 Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11779 // For Mips64 N64 ABI, there may be up to three operations specified per
11780 // record, by the fields r_type, r_type2, and r_type3. The first operation
11781 // takes its addend from the relocation record. Each subsequent operation
11782 // takes as its addend the result of the previous operation.
11783 // The first operation in a record which references a symbol uses the symbol
11784 // implied by r_sym. The next operation in a record which references a symbol
11785 // uses the special symbol value given by the r_ssym field. A third operation
11786 // in a record which references a symbol will assume a NULL symbol,
11787 // i.e. value zero.
11790 // Check if a record references to a symbol.
11791 for (unsigned int i = 0; i < 3; ++i)
11793 if (r_types[i] == elfcpp::R_MIPS_NONE)
11796 // If we didn't apply previous relocation, use its result as addend
11798 if (this->calculate_only_)
11800 r_addend = this->calculated_value_;
11801 extract_addend = false;
11804 // In the N32 and 64-bit ABIs there may be multiple consecutive
11805 // relocations for the same offset. In that case we are
11806 // supposed to treat the output of each relocation as the addend
11807 // for the next. For N64 ABI, we are checking offsets only in a
11808 // third operation in a record (r_type3).
11809 this->calculate_only_ =
11810 (object->is_n64() && i < 2
11811 ? r_types[i+1] != elfcpp::R_MIPS_NONE
11812 : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
11814 if (object->is_n64())
11818 // Handle special symbol for r_type2 relocation type.
11822 symval.set_output_value(0);
11825 symval.set_output_value(target->gp_value());
11828 symval.set_output_value(object->gp_value());
11831 symval.set_output_value(address);
11834 gold_unreachable();
11840 // For r_type3 symbol value is 0.
11841 symval.set_output_value(0);
11845 bool update_got_entry = false;
11846 switch (r_types[i])
11848 case elfcpp::R_MIPS_NONE:
11850 case elfcpp::R_MIPS_16:
11851 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11853 this->calculate_only_,
11854 &this->calculated_value_);
11857 case elfcpp::R_MIPS_32:
11858 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11860 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11862 this->calculate_only_,
11863 &this->calculated_value_);
11864 if (mips_sym != NULL
11865 && (mips_sym->is_mips16() || mips_sym->is_micromips())
11866 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11868 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11869 // already updated by adding +1.
11870 if (mips_sym->has_mips16_fn_stub())
11872 gold_assert(mips_sym->need_fn_stub());
11873 Mips16_stub_section<size, big_endian>* fn_stub =
11874 mips_sym->template get_mips16_fn_stub<big_endian>();
11876 symval.set_output_value(fn_stub->output_address());
11879 got_offset = mips_sym->global_gotoffset();
11880 update_got_entry = true;
11884 case elfcpp::R_MIPS_64:
11885 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11887 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11889 this->calculate_only_,
11890 &this->calculated_value_, false);
11891 else if (target->is_output_n64() && r_addend != 0)
11892 // Only apply the addend. The static relocation was RELA, but the
11893 // dynamic relocation is REL, so we need to apply the addend.
11894 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11896 this->calculate_only_,
11897 &this->calculated_value_, true);
11899 case elfcpp::R_MIPS_REL32:
11900 gold_unreachable();
11902 case elfcpp::R_MIPS_PC32:
11903 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11904 r_addend, extract_addend,
11905 this->calculate_only_,
11906 &this->calculated_value_);
11909 case elfcpp::R_MIPS16_26:
11910 // The calculation for R_MIPS16_26 is just the same as for an
11911 // R_MIPS_26. It's only the storage of the relocated field into
11912 // the output file that's different. So, we just fall through to the
11913 // R_MIPS_26 case here.
11914 case elfcpp::R_MIPS_26:
11915 case elfcpp::R_MICROMIPS_26_S1:
11916 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11917 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11918 r_types[i], target->jal_to_bal(), this->calculate_only_,
11919 &this->calculated_value_);
11922 case elfcpp::R_MIPS_HI16:
11923 case elfcpp::R_MIPS16_HI16:
11924 case elfcpp::R_MICROMIPS_HI16:
11925 if (rel_type == elfcpp::SHT_RELA)
11926 reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11928 gp_disp, r_types[i],
11931 this->calculate_only_,
11932 &this->calculated_value_);
11933 else if (rel_type == elfcpp::SHT_REL)
11934 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11935 address, gp_disp, r_types[i],
11936 r_sym, extract_addend);
11938 gold_unreachable();
11941 case elfcpp::R_MIPS_LO16:
11942 case elfcpp::R_MIPS16_LO16:
11943 case elfcpp::R_MICROMIPS_LO16:
11944 case elfcpp::R_MICROMIPS_HI0_LO16:
11945 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11946 r_addend, extract_addend, address,
11947 gp_disp, r_types[i], r_sym,
11948 rel_type, this->calculate_only_,
11949 &this->calculated_value_);
11952 case elfcpp::R_MIPS_LITERAL:
11953 case elfcpp::R_MICROMIPS_LITERAL:
11954 // Because we don't merge literal sections, we can handle this
11955 // just like R_MIPS_GPREL16. In the long run, we should merge
11956 // shared literals, and then we will need to additional work
11961 case elfcpp::R_MIPS_GPREL16:
11962 case elfcpp::R_MIPS16_GPREL:
11963 case elfcpp::R_MICROMIPS_GPREL7_S2:
11964 case elfcpp::R_MICROMIPS_GPREL16:
11965 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11966 target->adjusted_gp_value(object),
11967 r_addend, extract_addend,
11968 gsym == NULL, r_types[i],
11969 this->calculate_only_,
11970 &this->calculated_value_);
11973 case elfcpp::R_MIPS_PC16:
11974 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11975 r_addend, extract_addend,
11976 this->calculate_only_,
11977 &this->calculated_value_);
11980 case elfcpp::R_MIPS_PC21_S2:
11981 reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11982 r_addend, extract_addend,
11983 this->calculate_only_,
11984 &this->calculated_value_);
11987 case elfcpp::R_MIPS_PC26_S2:
11988 reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11989 r_addend, extract_addend,
11990 this->calculate_only_,
11991 &this->calculated_value_);
11994 case elfcpp::R_MIPS_PC18_S3:
11995 reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11996 r_addend, extract_addend,
11997 this->calculate_only_,
11998 &this->calculated_value_);
12001 case elfcpp::R_MIPS_PC19_S2:
12002 reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
12003 r_addend, extract_addend,
12004 this->calculate_only_,
12005 &this->calculated_value_);
12008 case elfcpp::R_MIPS_PCHI16:
12009 if (rel_type == elfcpp::SHT_RELA)
12010 reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
12013 this->calculate_only_,
12014 &this->calculated_value_);
12015 else if (rel_type == elfcpp::SHT_REL)
12016 reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
12017 r_addend, address, r_sym,
12020 gold_unreachable();
12023 case elfcpp::R_MIPS_PCLO16:
12024 reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
12025 extract_addend, address, r_sym,
12026 rel_type, this->calculate_only_,
12027 &this->calculated_value_);
12029 case elfcpp::R_MICROMIPS_PC7_S1:
12030 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
12033 this->calculate_only_,
12034 &this->calculated_value_);
12036 case elfcpp::R_MICROMIPS_PC10_S1:
12037 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
12039 r_addend, extract_addend,
12040 this->calculate_only_,
12041 &this->calculated_value_);
12043 case elfcpp::R_MICROMIPS_PC16_S1:
12044 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
12046 r_addend, extract_addend,
12047 this->calculate_only_,
12048 &this->calculated_value_);
12050 case elfcpp::R_MIPS_GPREL32:
12051 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12052 target->adjusted_gp_value(object),
12053 r_addend, extract_addend,
12054 this->calculate_only_,
12055 &this->calculated_value_);
12057 case elfcpp::R_MIPS_GOT_HI16:
12058 case elfcpp::R_MIPS_CALL_HI16:
12059 case elfcpp::R_MICROMIPS_GOT_HI16:
12060 case elfcpp::R_MICROMIPS_CALL_HI16:
12062 got_offset = target->got_section()->got_offset(gsym,
12066 got_offset = target->got_section()->got_offset(r_sym,
12069 gp_offset = target->got_section()->gp_offset(got_offset, object);
12070 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
12071 this->calculate_only_,
12072 &this->calculated_value_);
12073 update_got_entry = changed_symbol_value;
12076 case elfcpp::R_MIPS_GOT_LO16:
12077 case elfcpp::R_MIPS_CALL_LO16:
12078 case elfcpp::R_MICROMIPS_GOT_LO16:
12079 case elfcpp::R_MICROMIPS_CALL_LO16:
12081 got_offset = target->got_section()->got_offset(gsym,
12085 got_offset = target->got_section()->got_offset(r_sym,
12088 gp_offset = target->got_section()->gp_offset(got_offset, object);
12089 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
12090 this->calculate_only_,
12091 &this->calculated_value_);
12092 update_got_entry = changed_symbol_value;
12095 case elfcpp::R_MIPS_GOT_DISP:
12096 case elfcpp::R_MICROMIPS_GOT_DISP:
12097 case elfcpp::R_MIPS_EH:
12099 got_offset = target->got_section()->got_offset(gsym,
12103 got_offset = target->got_section()->got_offset(r_sym,
12106 gp_offset = target->got_section()->gp_offset(got_offset, object);
12107 if (eh_reloc(r_types[i]))
12108 reloc_status = Reloc_funcs::releh(view, gp_offset,
12109 this->calculate_only_,
12110 &this->calculated_value_);
12112 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12113 this->calculate_only_,
12114 &this->calculated_value_);
12116 case elfcpp::R_MIPS_CALL16:
12117 case elfcpp::R_MIPS16_CALL16:
12118 case elfcpp::R_MICROMIPS_CALL16:
12119 gold_assert(gsym != NULL);
12120 got_offset = target->got_section()->got_offset(gsym,
12123 gp_offset = target->got_section()->gp_offset(got_offset, object);
12124 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12125 this->calculate_only_,
12126 &this->calculated_value_);
12127 // TODO(sasa): We should also initialize update_got_entry
12128 // in other place swhere relgot is called.
12129 update_got_entry = changed_symbol_value;
12132 case elfcpp::R_MIPS_GOT16:
12133 case elfcpp::R_MIPS16_GOT16:
12134 case elfcpp::R_MICROMIPS_GOT16:
12137 got_offset = target->got_section()->got_offset(gsym,
12140 gp_offset = target->got_section()->gp_offset(got_offset, object);
12141 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12142 this->calculate_only_,
12143 &this->calculated_value_);
12147 if (rel_type == elfcpp::SHT_RELA)
12148 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
12152 this->calculate_only_,
12153 &this->calculated_value_);
12154 else if (rel_type == elfcpp::SHT_REL)
12155 reloc_status = Reloc_funcs::relgot16_local(view, object,
12158 r_types[i], r_sym);
12160 gold_unreachable();
12162 update_got_entry = changed_symbol_value;
12165 case elfcpp::R_MIPS_TLS_GD:
12166 case elfcpp::R_MIPS16_TLS_GD:
12167 case elfcpp::R_MICROMIPS_TLS_GD:
12169 got_offset = target->got_section()->got_offset(gsym,
12173 got_offset = target->got_section()->got_offset(r_sym,
12176 gp_offset = target->got_section()->gp_offset(got_offset, object);
12177 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12178 this->calculate_only_,
12179 &this->calculated_value_);
12182 case elfcpp::R_MIPS_TLS_GOTTPREL:
12183 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12184 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12186 got_offset = target->got_section()->got_offset(gsym,
12187 GOT_TYPE_TLS_OFFSET,
12190 got_offset = target->got_section()->got_offset(r_sym,
12191 GOT_TYPE_TLS_OFFSET,
12193 gp_offset = target->got_section()->gp_offset(got_offset, object);
12194 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12195 this->calculate_only_,
12196 &this->calculated_value_);
12199 case elfcpp::R_MIPS_TLS_LDM:
12200 case elfcpp::R_MIPS16_TLS_LDM:
12201 case elfcpp::R_MICROMIPS_TLS_LDM:
12202 // Relocate the field with the offset of the GOT entry for
12203 // the module index.
12204 got_offset = target->got_section()->tls_ldm_offset(object);
12205 gp_offset = target->got_section()->gp_offset(got_offset, object);
12206 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12207 this->calculate_only_,
12208 &this->calculated_value_);
12211 case elfcpp::R_MIPS_GOT_PAGE:
12212 case elfcpp::R_MICROMIPS_GOT_PAGE:
12213 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12214 r_addend, extract_addend,
12215 this->calculate_only_,
12216 &this->calculated_value_);
12219 case elfcpp::R_MIPS_GOT_OFST:
12220 case elfcpp::R_MICROMIPS_GOT_OFST:
12221 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12222 r_addend, extract_addend,
12223 local, this->calculate_only_,
12224 &this->calculated_value_);
12227 case elfcpp::R_MIPS_JALR:
12228 case elfcpp::R_MICROMIPS_JALR:
12229 // This relocation is only a hint. In some cases, we optimize
12230 // it into a bal instruction. But we don't try to optimize
12231 // when the symbol does not resolve locally.
12233 || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12234 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12235 r_addend, extract_addend,
12236 cross_mode_jump, r_types[i],
12237 target->jalr_to_bal(),
12239 this->calculate_only_,
12240 &this->calculated_value_);
12243 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12244 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12245 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12246 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12247 elfcpp::DTP_OFFSET, r_addend,
12249 this->calculate_only_,
12250 &this->calculated_value_);
12252 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12253 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12254 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12255 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12256 elfcpp::DTP_OFFSET, r_addend,
12258 this->calculate_only_,
12259 &this->calculated_value_);
12261 case elfcpp::R_MIPS_TLS_DTPREL32:
12262 case elfcpp::R_MIPS_TLS_DTPREL64:
12263 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12264 elfcpp::DTP_OFFSET, r_addend,
12266 this->calculate_only_,
12267 &this->calculated_value_);
12269 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12270 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12271 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12272 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12273 elfcpp::TP_OFFSET, r_addend,
12275 this->calculate_only_,
12276 &this->calculated_value_);
12278 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12279 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12280 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12281 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12282 elfcpp::TP_OFFSET, r_addend,
12284 this->calculate_only_,
12285 &this->calculated_value_);
12287 case elfcpp::R_MIPS_TLS_TPREL32:
12288 case elfcpp::R_MIPS_TLS_TPREL64:
12289 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12290 elfcpp::TP_OFFSET, r_addend,
12292 this->calculate_only_,
12293 &this->calculated_value_);
12295 case elfcpp::R_MIPS_SUB:
12296 case elfcpp::R_MICROMIPS_SUB:
12297 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12299 this->calculate_only_,
12300 &this->calculated_value_);
12302 case elfcpp::R_MIPS_HIGHER:
12303 case elfcpp::R_MICROMIPS_HIGHER:
12304 reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
12306 this->calculate_only_,
12307 &this->calculated_value_);
12309 case elfcpp::R_MIPS_HIGHEST:
12310 case elfcpp::R_MICROMIPS_HIGHEST:
12311 reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12312 r_addend, extract_addend,
12313 this->calculate_only_,
12314 &this->calculated_value_);
12317 gold_error_at_location(relinfo, relnum, r_offset,
12318 _("unsupported reloc %u"), r_types[i]);
12322 if (update_got_entry)
12324 Mips_output_data_got<size, big_endian>* got = target->got_section();
12325 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12326 got->update_got_entry(got->get_primary_got_offset(mips_sym),
12327 psymval->value(object, 0));
12329 got->update_got_entry(got_offset, psymval->value(object, 0));
12333 bool jal_shuffle = jal_reloc(r_type);
12334 Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12336 // Report any errors.
12337 switch (reloc_status)
12339 case Reloc_funcs::STATUS_OKAY:
12341 case Reloc_funcs::STATUS_OVERFLOW:
12343 gold_error_at_location(relinfo, relnum, r_offset,
12344 _("relocation overflow: "
12345 "%u against local symbol %u in %s"),
12346 r_type, r_sym, object->name().c_str());
12347 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12348 gold_error_at_location(relinfo, relnum, r_offset,
12349 _("relocation overflow: "
12350 "%u against '%s' defined in %s"),
12351 r_type, gsym->demangled_name().c_str(),
12352 gsym->object()->name().c_str());
12354 gold_error_at_location(relinfo, relnum, r_offset,
12355 _("relocation overflow: %u against '%s'"),
12356 r_type, gsym->demangled_name().c_str());
12358 case Reloc_funcs::STATUS_BAD_RELOC:
12359 gold_error_at_location(relinfo, relnum, r_offset,
12360 _("unexpected opcode while processing relocation"));
12362 case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12363 gold_error_at_location(relinfo, relnum, r_offset,
12364 _("unaligned PC-relative relocation"));
12367 gold_unreachable();
12373 // Get the Reference_flags for a particular relocation.
12375 template<int size, bool big_endian>
12377 Target_mips<size, big_endian>::Scan::get_reference_flags(
12378 unsigned int r_type)
12382 case elfcpp::R_MIPS_NONE:
12383 // No symbol reference.
12386 case elfcpp::R_MIPS_16:
12387 case elfcpp::R_MIPS_32:
12388 case elfcpp::R_MIPS_64:
12389 case elfcpp::R_MIPS_HI16:
12390 case elfcpp::R_MIPS_LO16:
12391 case elfcpp::R_MIPS_HIGHER:
12392 case elfcpp::R_MIPS_HIGHEST:
12393 case elfcpp::R_MIPS16_HI16:
12394 case elfcpp::R_MIPS16_LO16:
12395 case elfcpp::R_MICROMIPS_HI16:
12396 case elfcpp::R_MICROMIPS_LO16:
12397 case elfcpp::R_MICROMIPS_HIGHER:
12398 case elfcpp::R_MICROMIPS_HIGHEST:
12399 return Symbol::ABSOLUTE_REF;
12401 case elfcpp::R_MIPS_26:
12402 case elfcpp::R_MIPS16_26:
12403 case elfcpp::R_MICROMIPS_26_S1:
12404 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12406 case elfcpp::R_MIPS_PC18_S3:
12407 case elfcpp::R_MIPS_PC19_S2:
12408 case elfcpp::R_MIPS_PCHI16:
12409 case elfcpp::R_MIPS_PCLO16:
12410 case elfcpp::R_MIPS_GPREL32:
12411 case elfcpp::R_MIPS_GPREL16:
12412 case elfcpp::R_MIPS_REL32:
12413 case elfcpp::R_MIPS16_GPREL:
12414 return Symbol::RELATIVE_REF;
12416 case elfcpp::R_MIPS_PC16:
12417 case elfcpp::R_MIPS_PC32:
12418 case elfcpp::R_MIPS_PC21_S2:
12419 case elfcpp::R_MIPS_PC26_S2:
12420 case elfcpp::R_MIPS_JALR:
12421 case elfcpp::R_MICROMIPS_JALR:
12422 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12424 case elfcpp::R_MIPS_GOT16:
12425 case elfcpp::R_MIPS_CALL16:
12426 case elfcpp::R_MIPS_GOT_DISP:
12427 case elfcpp::R_MIPS_GOT_HI16:
12428 case elfcpp::R_MIPS_GOT_LO16:
12429 case elfcpp::R_MIPS_CALL_HI16:
12430 case elfcpp::R_MIPS_CALL_LO16:
12431 case elfcpp::R_MIPS_LITERAL:
12432 case elfcpp::R_MIPS_GOT_PAGE:
12433 case elfcpp::R_MIPS_GOT_OFST:
12434 case elfcpp::R_MIPS16_GOT16:
12435 case elfcpp::R_MIPS16_CALL16:
12436 case elfcpp::R_MICROMIPS_GOT16:
12437 case elfcpp::R_MICROMIPS_CALL16:
12438 case elfcpp::R_MICROMIPS_GOT_HI16:
12439 case elfcpp::R_MICROMIPS_GOT_LO16:
12440 case elfcpp::R_MICROMIPS_CALL_HI16:
12441 case elfcpp::R_MICROMIPS_CALL_LO16:
12442 case elfcpp::R_MIPS_EH:
12443 // Absolute in GOT.
12444 return Symbol::RELATIVE_REF;
12446 case elfcpp::R_MIPS_TLS_DTPMOD32:
12447 case elfcpp::R_MIPS_TLS_DTPREL32:
12448 case elfcpp::R_MIPS_TLS_DTPMOD64:
12449 case elfcpp::R_MIPS_TLS_DTPREL64:
12450 case elfcpp::R_MIPS_TLS_GD:
12451 case elfcpp::R_MIPS_TLS_LDM:
12452 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12453 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12454 case elfcpp::R_MIPS_TLS_GOTTPREL:
12455 case elfcpp::R_MIPS_TLS_TPREL32:
12456 case elfcpp::R_MIPS_TLS_TPREL64:
12457 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12458 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12459 case elfcpp::R_MIPS16_TLS_GD:
12460 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12461 case elfcpp::R_MICROMIPS_TLS_GD:
12462 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12463 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12464 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12465 return Symbol::TLS_REF;
12467 case elfcpp::R_MIPS_COPY:
12468 case elfcpp::R_MIPS_JUMP_SLOT:
12470 // Not expected. We will give an error later.
12475 // Report an unsupported relocation against a local symbol.
12477 template<int size, bool big_endian>
12479 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12480 Sized_relobj_file<size, big_endian>* object,
12481 unsigned int r_type)
12483 gold_error(_("%s: unsupported reloc %u against local symbol"),
12484 object->name().c_str(), r_type);
12487 // Report an unsupported relocation against a global symbol.
12489 template<int size, bool big_endian>
12491 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12492 Sized_relobj_file<size, big_endian>* object,
12493 unsigned int r_type,
12496 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12497 object->name().c_str(), r_type, gsym->demangled_name().c_str());
12500 // Return printable name for ABI.
12501 template<int size, bool big_endian>
12503 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12505 switch (e_flags & elfcpp::EF_MIPS_ABI)
12508 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12510 else if (size == 64)
12514 case elfcpp::E_MIPS_ABI_O32:
12516 case elfcpp::E_MIPS_ABI_O64:
12518 case elfcpp::E_MIPS_ABI_EABI32:
12520 case elfcpp::E_MIPS_ABI_EABI64:
12523 return "unknown abi";
12527 template<int size, bool big_endian>
12529 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12531 switch (e_flags & elfcpp::EF_MIPS_MACH)
12533 case elfcpp::E_MIPS_MACH_3900:
12534 return "mips:3900";
12535 case elfcpp::E_MIPS_MACH_4010:
12536 return "mips:4010";
12537 case elfcpp::E_MIPS_MACH_4100:
12538 return "mips:4100";
12539 case elfcpp::E_MIPS_MACH_4111:
12540 return "mips:4111";
12541 case elfcpp::E_MIPS_MACH_4120:
12542 return "mips:4120";
12543 case elfcpp::E_MIPS_MACH_4650:
12544 return "mips:4650";
12545 case elfcpp::E_MIPS_MACH_5400:
12546 return "mips:5400";
12547 case elfcpp::E_MIPS_MACH_5500:
12548 return "mips:5500";
12549 case elfcpp::E_MIPS_MACH_5900:
12550 return "mips:5900";
12551 case elfcpp::E_MIPS_MACH_SB1:
12553 case elfcpp::E_MIPS_MACH_9000:
12554 return "mips:9000";
12555 case elfcpp::E_MIPS_MACH_LS2E:
12556 return "mips:loongson_2e";
12557 case elfcpp::E_MIPS_MACH_LS2F:
12558 return "mips:loongson_2f";
12559 case elfcpp::E_MIPS_MACH_LS3A:
12560 return "mips:loongson_3a";
12561 case elfcpp::E_MIPS_MACH_OCTEON:
12562 return "mips:octeon";
12563 case elfcpp::E_MIPS_MACH_OCTEON2:
12564 return "mips:octeon2";
12565 case elfcpp::E_MIPS_MACH_OCTEON3:
12566 return "mips:octeon3";
12567 case elfcpp::E_MIPS_MACH_XLR:
12570 switch (e_flags & elfcpp::EF_MIPS_ARCH)
12573 case elfcpp::E_MIPS_ARCH_1:
12574 return "mips:3000";
12576 case elfcpp::E_MIPS_ARCH_2:
12577 return "mips:6000";
12579 case elfcpp::E_MIPS_ARCH_3:
12580 return "mips:4000";
12582 case elfcpp::E_MIPS_ARCH_4:
12583 return "mips:8000";
12585 case elfcpp::E_MIPS_ARCH_5:
12586 return "mips:mips5";
12588 case elfcpp::E_MIPS_ARCH_32:
12589 return "mips:isa32";
12591 case elfcpp::E_MIPS_ARCH_64:
12592 return "mips:isa64";
12594 case elfcpp::E_MIPS_ARCH_32R2:
12595 return "mips:isa32r2";
12597 case elfcpp::E_MIPS_ARCH_32R6:
12598 return "mips:isa32r6";
12600 case elfcpp::E_MIPS_ARCH_64R2:
12601 return "mips:isa64r2";
12603 case elfcpp::E_MIPS_ARCH_64R6:
12604 return "mips:isa64r6";
12607 return "unknown CPU";
12610 template<int size, bool big_endian>
12611 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12614 big_endian, // is_big_endian
12615 elfcpp::EM_MIPS, // machine_code
12616 true, // has_make_symbol
12617 false, // has_resolve
12618 false, // has_code_fill
12619 true, // is_default_stack_executable
12620 false, // can_icf_inline_merge_sections
12622 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
12623 0x400000, // default_text_segment_address
12624 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
12625 4 * 1024, // common_pagesize (overridable by -z common-page-size)
12626 false, // isolate_execinstr
12627 0, // rosegment_gap
12628 elfcpp::SHN_UNDEF, // small_common_shndx
12629 elfcpp::SHN_UNDEF, // large_common_shndx
12630 0, // small_common_section_flags
12631 0, // large_common_section_flags
12632 NULL, // attributes_section
12633 NULL, // attributes_vendor
12634 "__start", // entry_symbol_name
12635 32, // hash_entry_size
12638 template<int size, bool big_endian>
12639 class Target_mips_nacl : public Target_mips<size, big_endian>
12643 : Target_mips<size, big_endian>(&mips_nacl_info)
12647 static const Target::Target_info mips_nacl_info;
12650 template<int size, bool big_endian>
12651 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12654 big_endian, // is_big_endian
12655 elfcpp::EM_MIPS, // machine_code
12656 true, // has_make_symbol
12657 false, // has_resolve
12658 false, // has_code_fill
12659 true, // is_default_stack_executable
12660 false, // can_icf_inline_merge_sections
12662 "/lib/ld.so.1", // dynamic_linker
12663 0x20000, // default_text_segment_address
12664 0x10000, // abi_pagesize (overridable by -z max-page-size)
12665 0x10000, // common_pagesize (overridable by -z common-page-size)
12666 true, // isolate_execinstr
12667 0x10000000, // rosegment_gap
12668 elfcpp::SHN_UNDEF, // small_common_shndx
12669 elfcpp::SHN_UNDEF, // large_common_shndx
12670 0, // small_common_section_flags
12671 0, // large_common_section_flags
12672 NULL, // attributes_section
12673 NULL, // attributes_vendor
12674 "_start", // entry_symbol_name
12675 32, // hash_entry_size
12678 // Target selector for Mips. Note this is never instantiated directly.
12679 // It's only used in Target_selector_mips_nacl, below.
12681 template<int size, bool big_endian>
12682 class Target_selector_mips : public Target_selector
12685 Target_selector_mips()
12686 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12688 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12689 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12691 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12692 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12695 Target* do_instantiate_target()
12696 { return new Target_mips<size, big_endian>(); }
12699 template<int size, bool big_endian>
12700 class Target_selector_mips_nacl
12701 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12702 Target_mips_nacl<size, big_endian> >
12705 Target_selector_mips_nacl()
12706 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12707 Target_mips_nacl<size, big_endian> >(
12708 // NaCl currently supports only MIPS32 little-endian.
12709 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12713 Target_selector_mips_nacl<32, true> target_selector_mips32;
12714 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12715 Target_selector_mips_nacl<64, true> target_selector_mips64;
12716 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12718 } // End anonymous namespace.