1 // mips.cc -- mips target support for gold.
3 // Copyright (C) 2011-2016 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"
53 template<int size, bool big_endian>
54 class Mips_output_data_plt;
56 template<int size, bool big_endian>
57 class Mips_output_data_got;
59 template<int size, bool big_endian>
62 template<int size, bool big_endian>
63 class Mips_output_section_reginfo;
65 template<int size, bool big_endian>
66 class Mips_output_data_la25_stub;
68 template<int size, bool big_endian>
69 class Mips_output_data_mips_stubs;
74 template<int size, bool big_endian>
77 template<int size, bool big_endian>
80 class Mips16_stub_section_base;
82 template<int size, bool big_endian>
83 class Mips16_stub_section;
85 // The ABI says that every symbol used by dynamic relocations must have
86 // a global GOT entry. Among other things, this provides the dynamic
87 // linker with a free, directly-indexed cache. The GOT can therefore
88 // contain symbols that are not referenced by GOT relocations themselves
89 // (in other words, it may have symbols that are not referenced by things
90 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
92 // GOT relocations are less likely to overflow if we put the associated
93 // GOT entries towards the beginning. We therefore divide the global
94 // GOT entries into two areas: "normal" and "reloc-only". Entries in
95 // the first area can be used for both dynamic relocations and GP-relative
96 // accesses, while those in the "reloc-only" area are for dynamic
99 // These GGA_* ("Global GOT Area") values are organised so that lower
100 // values are more general than higher values. Also, non-GGA_NONE
101 // values are ordered by the position of the area in the GOT.
110 // The types of GOT entries needed for this platform.
111 // These values are exposed to the ABI in an incremental link.
112 // Do not renumber existing values without changing the version
113 // number of the .gnu_incremental_inputs section.
116 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
117 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
118 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
120 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
121 GOT_TYPE_STANDARD_MULTIGOT = 3,
122 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
123 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
126 // TLS type of GOT entry.
135 // Values found in the r_ssym field of a relocation entry.
136 enum Special_relocation_symbol
138 RSS_UNDEF = 0, // None - value is zero.
139 RSS_GP = 1, // Value of GP.
140 RSS_GP0 = 2, // Value of GP in object being relocated.
141 RSS_LOC = 3 // Address of location being relocated.
144 // Whether the section is readonly.
146 is_readonly_section(Output_section* output_section)
148 elfcpp::Elf_Xword section_flags = output_section->flags();
149 elfcpp::Elf_Word section_type = output_section->type();
151 if (section_type == elfcpp::SHT_NOBITS)
154 if (section_flags & elfcpp::SHF_WRITE)
160 // Return TRUE if a relocation of type R_TYPE from OBJECT might
161 // require an la25 stub. See also local_pic_function, which determines
162 // whether the destination function ever requires a stub.
163 template<int size, bool big_endian>
165 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
166 unsigned int r_type, bool target_is_16_bit_code)
168 // We specifically ignore branches and jumps from EF_PIC objects,
169 // where the onus is on the compiler or programmer to perform any
170 // necessary initialization of $25. Sometimes such initialization
171 // is unnecessary; for example, -mno-shared functions do not use
172 // the incoming value of $25, and may therefore be called directly.
173 if (object->is_pic())
178 case elfcpp::R_MIPS_26:
179 case elfcpp::R_MIPS_PC16:
180 case elfcpp::R_MICROMIPS_26_S1:
181 case elfcpp::R_MICROMIPS_PC7_S1:
182 case elfcpp::R_MICROMIPS_PC10_S1:
183 case elfcpp::R_MICROMIPS_PC16_S1:
184 case elfcpp::R_MICROMIPS_PC23_S2:
187 case elfcpp::R_MIPS16_26:
188 return !target_is_16_bit_code;
195 // Return true if SYM is a locally-defined PIC function, in the sense
196 // that it or its fn_stub might need $25 to be valid on entry.
197 // Note that MIPS16 functions set up $gp using PC-relative instructions,
198 // so they themselves never need $25 to be valid. Only non-MIPS16
199 // entry points are of interest here.
200 template<int size, bool big_endian>
202 local_pic_function(Mips_symbol<size>* sym)
204 bool def_regular = (sym->source() == Symbol::FROM_OBJECT
205 && !sym->object()->is_dynamic()
206 && !sym->is_undefined());
208 if (sym->is_defined() && def_regular)
210 Mips_relobj<size, big_endian>* object =
211 static_cast<Mips_relobj<size, big_endian>*>(sym->object());
213 if ((object->is_pic() || sym->is_pic())
214 && (!sym->is_mips16()
215 || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
222 hi16_reloc(int r_type)
224 return (r_type == elfcpp::R_MIPS_HI16
225 || r_type == elfcpp::R_MIPS16_HI16
226 || r_type == elfcpp::R_MICROMIPS_HI16);
230 lo16_reloc(int r_type)
232 return (r_type == elfcpp::R_MIPS_LO16
233 || r_type == elfcpp::R_MIPS16_LO16
234 || r_type == elfcpp::R_MICROMIPS_LO16);
238 got16_reloc(unsigned int r_type)
240 return (r_type == elfcpp::R_MIPS_GOT16
241 || r_type == elfcpp::R_MIPS16_GOT16
242 || r_type == elfcpp::R_MICROMIPS_GOT16);
246 call_lo16_reloc(unsigned int r_type)
248 return (r_type == elfcpp::R_MIPS_CALL_LO16
249 || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
253 got_lo16_reloc(unsigned int r_type)
255 return (r_type == elfcpp::R_MIPS_GOT_LO16
256 || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
260 eh_reloc(unsigned int r_type)
262 return (r_type == elfcpp::R_MIPS_EH);
266 got_disp_reloc(unsigned int r_type)
268 return (r_type == elfcpp::R_MIPS_GOT_DISP
269 || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
273 got_page_reloc(unsigned int r_type)
275 return (r_type == elfcpp::R_MIPS_GOT_PAGE
276 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
280 tls_gd_reloc(unsigned int r_type)
282 return (r_type == elfcpp::R_MIPS_TLS_GD
283 || r_type == elfcpp::R_MIPS16_TLS_GD
284 || r_type == elfcpp::R_MICROMIPS_TLS_GD);
288 tls_gottprel_reloc(unsigned int r_type)
290 return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
291 || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
292 || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
296 tls_ldm_reloc(unsigned int r_type)
298 return (r_type == elfcpp::R_MIPS_TLS_LDM
299 || r_type == elfcpp::R_MIPS16_TLS_LDM
300 || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
304 mips16_call_reloc(unsigned int r_type)
306 return (r_type == elfcpp::R_MIPS16_26
307 || r_type == elfcpp::R_MIPS16_CALL16);
311 jal_reloc(unsigned int r_type)
313 return (r_type == elfcpp::R_MIPS_26
314 || r_type == elfcpp::R_MIPS16_26
315 || r_type == elfcpp::R_MICROMIPS_26_S1);
319 micromips_branch_reloc(unsigned int r_type)
321 return (r_type == elfcpp::R_MICROMIPS_26_S1
322 || r_type == elfcpp::R_MICROMIPS_PC16_S1
323 || r_type == elfcpp::R_MICROMIPS_PC10_S1
324 || r_type == elfcpp::R_MICROMIPS_PC7_S1);
327 // Check if R_TYPE is a MIPS16 reloc.
329 mips16_reloc(unsigned int r_type)
333 case elfcpp::R_MIPS16_26:
334 case elfcpp::R_MIPS16_GPREL:
335 case elfcpp::R_MIPS16_GOT16:
336 case elfcpp::R_MIPS16_CALL16:
337 case elfcpp::R_MIPS16_HI16:
338 case elfcpp::R_MIPS16_LO16:
339 case elfcpp::R_MIPS16_TLS_GD:
340 case elfcpp::R_MIPS16_TLS_LDM:
341 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
342 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
343 case elfcpp::R_MIPS16_TLS_GOTTPREL:
344 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
345 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
353 // Check if R_TYPE is a microMIPS reloc.
355 micromips_reloc(unsigned int r_type)
359 case elfcpp::R_MICROMIPS_26_S1:
360 case elfcpp::R_MICROMIPS_HI16:
361 case elfcpp::R_MICROMIPS_LO16:
362 case elfcpp::R_MICROMIPS_GPREL16:
363 case elfcpp::R_MICROMIPS_LITERAL:
364 case elfcpp::R_MICROMIPS_GOT16:
365 case elfcpp::R_MICROMIPS_PC7_S1:
366 case elfcpp::R_MICROMIPS_PC10_S1:
367 case elfcpp::R_MICROMIPS_PC16_S1:
368 case elfcpp::R_MICROMIPS_CALL16:
369 case elfcpp::R_MICROMIPS_GOT_DISP:
370 case elfcpp::R_MICROMIPS_GOT_PAGE:
371 case elfcpp::R_MICROMIPS_GOT_OFST:
372 case elfcpp::R_MICROMIPS_GOT_HI16:
373 case elfcpp::R_MICROMIPS_GOT_LO16:
374 case elfcpp::R_MICROMIPS_SUB:
375 case elfcpp::R_MICROMIPS_HIGHER:
376 case elfcpp::R_MICROMIPS_HIGHEST:
377 case elfcpp::R_MICROMIPS_CALL_HI16:
378 case elfcpp::R_MICROMIPS_CALL_LO16:
379 case elfcpp::R_MICROMIPS_SCN_DISP:
380 case elfcpp::R_MICROMIPS_JALR:
381 case elfcpp::R_MICROMIPS_HI0_LO16:
382 case elfcpp::R_MICROMIPS_TLS_GD:
383 case elfcpp::R_MICROMIPS_TLS_LDM:
384 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
385 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
386 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
387 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
388 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
389 case elfcpp::R_MICROMIPS_GPREL7_S2:
390 case elfcpp::R_MICROMIPS_PC23_S2:
399 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
403 case elfcpp::R_MIPS_HI16:
404 case elfcpp::R_MIPS_GOT16:
405 return lo16_reloc == elfcpp::R_MIPS_LO16;
406 case elfcpp::R_MIPS16_HI16:
407 case elfcpp::R_MIPS16_GOT16:
408 return lo16_reloc == elfcpp::R_MIPS16_LO16;
409 case elfcpp::R_MICROMIPS_HI16:
410 case elfcpp::R_MICROMIPS_GOT16:
411 return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
417 // This class is used to hold information about one GOT entry.
418 // There are three types of entry:
420 // (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
421 // (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
422 // (2) a SYMBOL address, where SYMBOL is not local to an input object
423 // (sym != NULL, symndx == -1)
424 // (3) a TLS LDM slot (there's only one of these per GOT.)
425 // (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
427 template<int size, bool big_endian>
430 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
433 Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
434 Mips_address addend, unsigned char tls_type,
435 unsigned int shndx, bool is_section_symbol)
436 : addend_(addend), symndx_(symndx), tls_type_(tls_type),
437 is_section_symbol_(is_section_symbol), shndx_(shndx)
438 { this->d.object = object; }
440 Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
441 : addend_(0), symndx_(-1U), tls_type_(tls_type),
442 is_section_symbol_(false), shndx_(-1U)
443 { this->d.sym = sym; }
445 // Return whether this entry is for a local symbol.
447 is_for_local_symbol() const
448 { return this->symndx_ != -1U; }
450 // Return whether this entry is for a global symbol.
452 is_for_global_symbol() const
453 { return this->symndx_ == -1U; }
455 // Return the hash of this entry.
459 if (this->tls_type_ == GOT_TLS_LDM)
460 return this->symndx_ + (1 << 18);
462 size_t name_hash_value = gold::string_hash<char>(
463 (this->symndx_ != -1U)
464 ? this->d.object->name().c_str()
465 : this->d.sym->name());
466 size_t addend = this->addend_;
467 return name_hash_value ^ this->symndx_ ^ addend;
470 // Return whether this entry is equal to OTHER.
472 equals(Mips_got_entry<size, big_endian>* other) const
474 if (this->tls_type_ == GOT_TLS_LDM)
477 return ((this->tls_type_ == other->tls_type_)
478 && (this->symndx_ == other->symndx_)
479 && ((this->symndx_ != -1U)
480 ? (this->d.object == other->d.object)
481 : (this->d.sym == other->d.sym))
482 && (this->addend_ == other->addend_));
485 // Return input object that needs this GOT entry.
486 Mips_relobj<size, big_endian>*
489 gold_assert(this->symndx_ != -1U);
490 return this->d.object;
493 // Return local symbol index for local GOT entries.
497 gold_assert(this->symndx_ != -1U);
498 return this->symndx_;
501 // Return the relocation addend for local GOT entries.
504 { return this->addend_; }
506 // Return global symbol for global GOT entries.
510 gold_assert(this->symndx_ == -1U);
514 // Return whether this is a TLS GOT entry.
517 { return this->tls_type_ != GOT_TLS_NONE; }
519 // Return TLS type of this GOT entry.
522 { return this->tls_type_; }
524 // Return section index of the local symbol for local GOT entries.
527 { return this->shndx_; }
529 // Return whether this is a STT_SECTION symbol.
531 is_section_symbol() const
532 { return this->is_section_symbol_; }
536 Mips_address addend_;
538 // The index of the symbol if we have a local symbol; -1 otherwise.
539 unsigned int symndx_;
543 // The input object for local symbols that needs the GOT entry.
544 Mips_relobj<size, big_endian>* object;
545 // If symndx == -1, the global symbol corresponding to this GOT entry. The
546 // symbol's entry is in the local area if mips_sym->global_got_area is
547 // GGA_NONE, otherwise it is in the global area.
548 Mips_symbol<size>* sym;
551 // The TLS type of this GOT entry. An LDM GOT entry will be a local
552 // symbol entry with r_symndx == 0.
553 unsigned char tls_type_;
555 // Whether this is a STT_SECTION symbol.
556 bool is_section_symbol_;
558 // For local GOT entries, section index of the local symbol.
562 // Hash for Mips_got_entry.
564 template<int size, bool big_endian>
565 class Mips_got_entry_hash
569 operator()(Mips_got_entry<size, big_endian>* entry) const
570 { return entry->hash(); }
573 // Equality for Mips_got_entry.
575 template<int size, bool big_endian>
576 class Mips_got_entry_eq
580 operator()(Mips_got_entry<size, big_endian>* e1,
581 Mips_got_entry<size, big_endian>* e2) const
582 { return e1->equals(e2); }
585 // Hash for Mips_symbol.
588 class Mips_symbol_hash
592 operator()(Mips_symbol<size>* sym) const
593 { return sym->hash(); }
596 // Got_page_range. This class describes a range of addends: [MIN_ADDEND,
597 // MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
598 // increasing MIN_ADDEND.
600 struct Got_page_range
603 : next(NULL), min_addend(0), max_addend(0)
606 Got_page_range* next;
610 // Return the maximum number of GOT page entries required.
613 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
616 // Got_page_entry. This class describes the range of addends that are applied
617 // to page relocations against a given symbol.
619 struct Got_page_entry
622 : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
625 Got_page_entry(Object* object_, unsigned int symndx_)
626 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
629 // The input object that needs the GOT page entry.
631 // The index of the symbol, as stored in the relocation r_info.
633 // The ranges for this page entry.
634 Got_page_range* ranges;
635 // The maximum number of page entries needed for RANGES.
636 unsigned int num_pages;
639 // Hash for Got_page_entry.
641 struct Got_page_entry_hash
644 operator()(Got_page_entry* entry) const
645 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
648 // Equality for Got_page_entry.
650 struct Got_page_entry_eq
653 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
655 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
659 // This class is used to hold .got information when linking.
661 template<int size, bool big_endian>
664 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
665 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
667 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
669 // Unordered set of GOT entries.
670 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
671 Mips_got_entry_hash<size, big_endian>,
672 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
674 // Unordered set of GOT page entries.
675 typedef Unordered_set<Got_page_entry*,
676 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
678 // Unordered set of global GOT entries.
679 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
680 Global_got_entry_set;
684 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
685 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
686 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
687 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
691 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
692 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
694 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
695 unsigned int symndx, Mips_address addend,
696 unsigned int r_type, unsigned int shndx,
697 bool is_section_symbol);
699 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
700 // in OBJECT. FOR_CALL is true if the caller is only interested in
701 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
704 record_global_got_symbol(Mips_symbol<size>* mips_sym,
705 Mips_relobj<size, big_endian>* object,
706 unsigned int r_type, bool dyn_reloc, bool for_call);
708 // Add ENTRY to master GOT and to OBJECT's GOT.
710 record_got_entry(Mips_got_entry<size, big_endian>* entry,
711 Mips_relobj<size, big_endian>* object);
713 // Record that OBJECT has a page relocation against symbol SYMNDX and
714 // that ADDEND is the addend for that relocation.
716 record_got_page_entry(Mips_relobj<size, big_endian>* object,
717 unsigned int symndx, int addend);
719 // Create all entries that should be in the local part of the GOT.
721 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
723 // Create GOT page entries.
725 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
727 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
729 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
730 unsigned int non_reloc_only_global_gotno);
732 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
734 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
736 // Create TLS GOT entries.
738 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
740 // Decide whether the symbol needs an entry in the global part of the primary
741 // GOT, setting global_got_area accordingly. Count the number of global
742 // symbols that are in the primary GOT only because they have dynamic
743 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
745 count_got_symbols(Symbol_table* symtab);
747 // Return the offset of GOT page entry for VALUE.
749 get_got_page_offset(Mips_address value,
750 Mips_output_data_got<size, big_endian>* got);
752 // Count the number of GOT entries required.
756 // Count the number of GOT entries required by ENTRY. Accumulate the result.
758 count_got_entry(Mips_got_entry<size, big_endian>* entry);
760 // Add FROM's GOT entries.
762 add_got_entries(Mips_got_info<size, big_endian>* from);
764 // Add FROM's GOT page entries.
766 add_got_page_entries(Mips_got_info<size, big_endian>* from);
771 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
772 + this->tls_gotno_) * size/8);
775 // Return the number of local GOT entries.
778 { return this->local_gotno_; }
780 // Return the maximum number of page GOT entries needed.
783 { return this->page_gotno_; }
785 // Return the number of global GOT entries.
788 { return this->global_gotno_; }
790 // Set the number of global GOT entries.
792 set_global_gotno(unsigned int global_gotno)
793 { this->global_gotno_ = global_gotno; }
795 // Return the number of GGA_RELOC_ONLY global GOT entries.
797 reloc_only_gotno() const
798 { return this->reloc_only_gotno_; }
800 // Return the number of TLS GOT entries.
803 { return this->tls_gotno_; }
805 // Return the GOT type for this GOT. Used for multi-GOT links only.
807 multigot_got_type(unsigned int got_type) const
811 case GOT_TYPE_STANDARD:
812 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
813 case GOT_TYPE_TLS_OFFSET:
814 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
815 case GOT_TYPE_TLS_PAIR:
816 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
822 // Remove lazy-binding stubs for global symbols in this GOT.
824 remove_lazy_stubs(Target_mips<size, big_endian>* target);
826 // Return offset of this GOT from the start of .got section.
829 { return this->offset_; }
831 // Set offset of this GOT from the start of .got section.
833 set_offset(unsigned int offset)
834 { this->offset_ = offset; }
836 // Set index of this GOT in multi-GOT links.
838 set_index(unsigned int index)
839 { this->index_ = index; }
841 // Return next GOT in multi-GOT links.
842 Mips_got_info<size, big_endian>*
844 { return this->next_; }
846 // Set next GOT in multi-GOT links.
848 set_next(Mips_got_info<size, big_endian>* next)
849 { this->next_ = next; }
851 // Return the offset of TLS LDM entry for this GOT.
853 tls_ldm_offset() const
854 { return this->tls_ldm_offset_; }
856 // Set the offset of TLS LDM entry for this GOT.
858 set_tls_ldm_offset(unsigned int tls_ldm_offset)
859 { this->tls_ldm_offset_ = tls_ldm_offset; }
861 Global_got_entry_set&
863 { return this->global_got_symbols_; }
865 // Return the GOT_TLS_* type required by relocation type R_TYPE.
867 mips_elf_reloc_tls_type(unsigned int r_type)
869 if (tls_gd_reloc(r_type))
872 if (tls_ldm_reloc(r_type))
875 if (tls_gottprel_reloc(r_type))
881 // Return the number of GOT slots needed for GOT TLS type TYPE.
883 mips_tls_got_entries(unsigned int type)
903 // The number of local GOT entries.
904 unsigned int local_gotno_;
905 // The maximum number of page GOT entries needed.
906 unsigned int page_gotno_;
907 // The number of global GOT entries.
908 unsigned int global_gotno_;
909 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
910 unsigned int reloc_only_gotno_;
911 // The number of TLS GOT entries.
912 unsigned int tls_gotno_;
913 // The offset of TLS LDM entry for this GOT.
914 unsigned int tls_ldm_offset_;
915 // All symbols that have global GOT entry.
916 Global_got_entry_set global_got_symbols_;
917 // A hash table holding GOT entries.
918 Got_entry_set got_entries_;
919 // A hash table of GOT page entries.
920 Got_page_entry_set got_page_entries_;
921 // The offset of first GOT page entry for this GOT.
922 unsigned int got_page_offset_start_;
923 // The offset of next available GOT page entry for this GOT.
924 unsigned int got_page_offset_next_;
925 // A hash table that maps GOT page entry value to the GOT offset where
926 // the entry is located.
927 Got_page_offsets got_page_offsets_;
928 // In multi-GOT links, a pointer to the next GOT.
929 Mips_got_info<size, big_endian>* next_;
930 // Index of this GOT in multi-GOT links.
932 // The offset of this GOT in multi-GOT links.
933 unsigned int offset_;
936 // This is a helper class used during relocation scan. It records GOT16 addend.
938 template<int size, bool big_endian>
941 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
943 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
944 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
945 Mips_address _addend)
946 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
950 const Sized_relobj_file<size, big_endian>* object;
957 // Mips_symbol class. Holds additional symbol information needed for Mips.
960 class Mips_symbol : public Sized_symbol<size>
964 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
965 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
966 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
967 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
968 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
969 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
970 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
973 // Return whether this is a MIPS16 symbol.
977 // (st_other & STO_MIPS16) == STO_MIPS16
978 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
979 == elfcpp::STO_MIPS16 >> 2);
982 // Return whether this is a microMIPS symbol.
986 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
987 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
988 == elfcpp::STO_MICROMIPS >> 2);
991 // Return whether the symbol needs MIPS16 fn_stub.
994 { return this->need_fn_stub_; }
996 // Set that the symbol needs MIPS16 fn_stub.
999 { this->need_fn_stub_ = true; }
1001 // Return whether this symbol is referenced by branch relocations from
1002 // any non-PIC input file.
1004 has_nonpic_branches() const
1005 { return this->has_nonpic_branches_; }
1007 // Set that this symbol is referenced by branch relocations from
1008 // any non-PIC input file.
1010 set_has_nonpic_branches()
1011 { this->has_nonpic_branches_ = true; }
1013 // Return the offset of the la25 stub for this symbol from the start of the
1014 // la25 stub section.
1016 la25_stub_offset() const
1017 { return this->la25_stub_offset_; }
1019 // Set the offset of the la25 stub for this symbol from the start of the
1020 // la25 stub section.
1022 set_la25_stub_offset(unsigned int offset)
1023 { this->la25_stub_offset_ = offset; }
1025 // Return whether the symbol has la25 stub. This is true if this symbol is
1026 // for a PIC function, and there are non-PIC branches and jumps to it.
1028 has_la25_stub() const
1029 { return this->la25_stub_offset_ != -1U; }
1031 // Return whether there is a relocation against this symbol that must be
1032 // resolved by the static linker (that is, the relocation cannot possibly
1033 // be made dynamic).
1035 has_static_relocs() const
1036 { return this->has_static_relocs_; }
1038 // Set that there is a relocation against this symbol that must be resolved
1039 // by the static linker (that is, the relocation cannot possibly be made
1042 set_has_static_relocs()
1043 { this->has_static_relocs_ = true; }
1045 // Return whether we must not create a lazy-binding stub for this symbol.
1047 no_lazy_stub() const
1048 { return this->no_lazy_stub_; }
1050 // Set that we must not create a lazy-binding stub for this symbol.
1053 { this->no_lazy_stub_ = true; }
1055 // Return the offset of the lazy-binding stub for this symbol from the start
1056 // of .MIPS.stubs section.
1058 lazy_stub_offset() const
1059 { return this->lazy_stub_offset_; }
1061 // Set the offset of the lazy-binding stub for this symbol from the start
1062 // of .MIPS.stubs section.
1064 set_lazy_stub_offset(unsigned int offset)
1065 { this->lazy_stub_offset_ = offset; }
1067 // Return whether there are any relocations for this symbol where
1068 // pointer equality matters.
1070 pointer_equality_needed() const
1071 { return this->pointer_equality_needed_; }
1073 // Set that there are relocations for this symbol where pointer equality
1076 set_pointer_equality_needed()
1077 { this->pointer_equality_needed_ = true; }
1079 // Return global GOT area where this symbol in located.
1081 global_got_area() const
1082 { return this->global_got_area_; }
1084 // Set global GOT area where this symbol in located.
1086 set_global_got_area(Global_got_area global_got_area)
1087 { this->global_got_area_ = global_got_area; }
1089 // Return the global GOT offset for this symbol. For multi-GOT links, this
1090 // returns the offset from the start of .got section to the first GOT entry
1091 // for the symbol. Note that in multi-GOT links the symbol can have entry
1092 // in more than one GOT.
1094 global_gotoffset() const
1095 { return this->global_gotoffset_; }
1097 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1098 // the symbol can have entry in more than one GOT. This method will set
1099 // the offset only if it is less than current offset.
1101 set_global_gotoffset(unsigned int offset)
1103 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1104 this->global_gotoffset_ = offset;
1107 // Return whether all GOT relocations for this symbol are for calls.
1109 got_only_for_calls() const
1110 { return this->got_only_for_calls_; }
1112 // Set that there is a GOT relocation for this symbol that is not for call.
1114 set_got_not_only_for_calls()
1115 { this->got_only_for_calls_ = false; }
1117 // Return whether this is a PIC symbol.
1121 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1122 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1123 == (elfcpp::STO_MIPS_PIC >> 2));
1126 // Set the flag in st_other field that marks this symbol as PIC.
1130 if (this->is_mips16())
1131 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1132 this->set_nonvis((this->nonvis()
1133 & ~((elfcpp::STO_MIPS16 >> 2)
1134 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1135 | (elfcpp::STO_MIPS_PIC >> 2));
1137 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1138 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1139 | (elfcpp::STO_MIPS_PIC >> 2));
1142 // Set the flag in st_other field that marks this symbol as PLT.
1146 if (this->is_mips16())
1147 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1148 this->set_nonvis((this->nonvis()
1149 & ((elfcpp::STO_MIPS16 >> 2)
1150 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1151 | (elfcpp::STO_MIPS_PLT >> 2));
1154 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1155 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1156 | (elfcpp::STO_MIPS_PLT >> 2));
1159 // Downcast a base pointer to a Mips_symbol pointer.
1160 static Mips_symbol<size>*
1161 as_mips_sym(Symbol* sym)
1162 { return static_cast<Mips_symbol<size>*>(sym); }
1164 // Downcast a base pointer to a Mips_symbol pointer.
1165 static const Mips_symbol<size>*
1166 as_mips_sym(const Symbol* sym)
1167 { return static_cast<const Mips_symbol<size>*>(sym); }
1169 // Return whether the symbol has lazy-binding stub.
1171 has_lazy_stub() const
1172 { return this->has_lazy_stub_; }
1174 // Set whether the symbol has lazy-binding stub.
1176 set_has_lazy_stub(bool has_lazy_stub)
1177 { this->has_lazy_stub_ = has_lazy_stub; }
1179 // Return whether the symbol needs a standard PLT entry.
1181 needs_mips_plt() const
1182 { return this->needs_mips_plt_; }
1184 // Set whether the symbol needs a standard PLT entry.
1186 set_needs_mips_plt(bool needs_mips_plt)
1187 { this->needs_mips_plt_ = needs_mips_plt; }
1189 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1192 needs_comp_plt() const
1193 { return this->needs_comp_plt_; }
1195 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1197 set_needs_comp_plt(bool needs_comp_plt)
1198 { this->needs_comp_plt_ = needs_comp_plt; }
1200 // Return standard PLT entry offset, or -1 if none.
1202 mips_plt_offset() const
1203 { return this->mips_plt_offset_; }
1205 // Set standard PLT entry offset.
1207 set_mips_plt_offset(unsigned int mips_plt_offset)
1208 { this->mips_plt_offset_ = mips_plt_offset; }
1210 // Return whether the symbol has standard PLT entry.
1212 has_mips_plt_offset() const
1213 { return this->mips_plt_offset_ != -1U; }
1215 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1217 comp_plt_offset() const
1218 { return this->comp_plt_offset_; }
1220 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1222 set_comp_plt_offset(unsigned int comp_plt_offset)
1223 { this->comp_plt_offset_ = comp_plt_offset; }
1225 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1227 has_comp_plt_offset() const
1228 { return this->comp_plt_offset_ != -1U; }
1230 // Return MIPS16 fn stub for a symbol.
1231 template<bool big_endian>
1232 Mips16_stub_section<size, big_endian>*
1233 get_mips16_fn_stub() const
1235 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1238 // Set MIPS16 fn stub for a symbol.
1240 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1241 { this->mips16_fn_stub_ = stub; }
1243 // Return whether symbol has MIPS16 fn stub.
1245 has_mips16_fn_stub() const
1246 { return this->mips16_fn_stub_ != NULL; }
1248 // Return MIPS16 call stub for a symbol.
1249 template<bool big_endian>
1250 Mips16_stub_section<size, big_endian>*
1251 get_mips16_call_stub() const
1253 return static_cast<Mips16_stub_section<size, big_endian>*>(
1257 // Set MIPS16 call stub for a symbol.
1259 set_mips16_call_stub(Mips16_stub_section_base* stub)
1260 { this->mips16_call_stub_ = stub; }
1262 // Return whether symbol has MIPS16 call stub.
1264 has_mips16_call_stub() const
1265 { return this->mips16_call_stub_ != NULL; }
1267 // Return MIPS16 call_fp stub for a symbol.
1268 template<bool big_endian>
1269 Mips16_stub_section<size, big_endian>*
1270 get_mips16_call_fp_stub() const
1272 return static_cast<Mips16_stub_section<size, big_endian>*>(
1273 mips16_call_fp_stub_);
1276 // Set MIPS16 call_fp stub for a symbol.
1278 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1279 { this->mips16_call_fp_stub_ = stub; }
1281 // Return whether symbol has MIPS16 call_fp stub.
1283 has_mips16_call_fp_stub() const
1284 { return this->mips16_call_fp_stub_ != NULL; }
1287 get_applied_secondary_got_fixup() const
1288 { return applied_secondary_got_fixup_; }
1291 set_applied_secondary_got_fixup()
1292 { this->applied_secondary_got_fixup_ = true; }
1294 // Return the hash of this symbol.
1298 return gold::string_hash<char>(this->name());
1302 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1303 // appears in any relocs other than a 16 bit call.
1306 // True if this symbol is referenced by branch relocations from
1307 // any non-PIC input file. This is used to determine whether an
1308 // la25 stub is required.
1309 bool has_nonpic_branches_;
1311 // The offset of the la25 stub for this symbol from the start of the
1312 // la25 stub section.
1313 unsigned int la25_stub_offset_;
1315 // True if there is a relocation against this symbol that must be
1316 // resolved by the static linker (that is, the relocation cannot
1317 // possibly be made dynamic).
1318 bool has_static_relocs_;
1320 // Whether we must not create a lazy-binding stub for this symbol.
1321 // This is true if the symbol has relocations related to taking the
1322 // function's address.
1325 // The offset of the lazy-binding stub for this symbol from the start of
1326 // .MIPS.stubs section.
1327 unsigned int lazy_stub_offset_;
1329 // True if there are any relocations for this symbol where pointer equality
1331 bool pointer_equality_needed_;
1333 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1334 // in the global part of the GOT.
1335 Global_got_area global_got_area_;
1337 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1338 // from the start of .got section to the first GOT entry for the symbol.
1339 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1340 unsigned int global_gotoffset_;
1342 // Whether all GOT relocations for this symbol are for calls.
1343 bool got_only_for_calls_;
1344 // Whether the symbol has lazy-binding stub.
1345 bool has_lazy_stub_;
1346 // Whether the symbol needs a standard PLT entry.
1347 bool needs_mips_plt_;
1348 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1349 bool needs_comp_plt_;
1350 // Standard PLT entry offset, or -1 if none.
1351 unsigned int mips_plt_offset_;
1352 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1353 unsigned int comp_plt_offset_;
1354 // MIPS16 fn stub for a symbol.
1355 Mips16_stub_section_base* mips16_fn_stub_;
1356 // MIPS16 call stub for a symbol.
1357 Mips16_stub_section_base* mips16_call_stub_;
1358 // MIPS16 call_fp stub for a symbol.
1359 Mips16_stub_section_base* mips16_call_fp_stub_;
1361 bool applied_secondary_got_fixup_;
1364 // Mips16_stub_section class.
1366 // The mips16 compiler uses a couple of special sections to handle
1367 // floating point arguments.
1369 // Section names that look like .mips16.fn.FNNAME contain stubs that
1370 // copy floating point arguments from the fp regs to the gp regs and
1371 // then jump to FNNAME. If any 32 bit function calls FNNAME, the
1372 // call should be redirected to the stub instead. If no 32 bit
1373 // function calls FNNAME, the stub should be discarded. We need to
1374 // consider any reference to the function, not just a call, because
1375 // if the address of the function is taken we will need the stub,
1376 // since the address might be passed to a 32 bit function.
1378 // Section names that look like .mips16.call.FNNAME contain stubs
1379 // that copy floating point arguments from the gp regs to the fp
1380 // regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1381 // then any 16 bit function that calls FNNAME should be redirected
1382 // to the stub instead. If FNNAME is not a 32 bit function, the
1383 // stub should be discarded.
1385 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1386 // which call FNNAME and then copy the return value from the fp regs
1387 // to the gp regs. These stubs store the return address in $18 while
1388 // calling FNNAME; any function which might call one of these stubs
1389 // must arrange to save $18 around the call. (This case is not
1390 // needed for 32 bit functions that call 16 bit functions, because
1391 // 16 bit functions always return floating point values in both
1392 // $f0/$f1 and $2/$3.)
1394 // Note that in all cases FNNAME might be defined statically.
1395 // Therefore, FNNAME is not used literally. Instead, the relocation
1396 // information will indicate which symbol the section is for.
1398 // We record any stubs that we find in the symbol table.
1400 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1402 class Mips16_stub_section_base { };
1404 template<int size, bool big_endian>
1405 class Mips16_stub_section : public Mips16_stub_section_base
1407 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1410 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1411 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1412 found_r_mips_none_(false)
1414 gold_assert(object->is_mips16_fn_stub_section(shndx)
1415 || object->is_mips16_call_stub_section(shndx)
1416 || object->is_mips16_call_fp_stub_section(shndx));
1419 // Return the object of this stub section.
1420 Mips_relobj<size, big_endian>*
1422 { return this->object_; }
1424 // Return the size of a section.
1426 section_size() const
1427 { return this->object_->section_size(this->shndx_); }
1429 // Return section index of this stub section.
1432 { return this->shndx_; }
1434 // Return symbol index, if stub is for a local function.
1437 { return this->r_sym_; }
1439 // Return symbol, if stub is for a global function.
1442 { return this->gsym_; }
1444 // Return whether stub is for a local function.
1446 is_for_local_function() const
1447 { return this->gsym_ == NULL; }
1449 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1450 // is found in the stub section. Try to find stub target.
1452 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1454 // To find target symbol for this stub, trust the first R_MIPS_NONE
1455 // relocation, if any. Otherwise trust the first relocation, whatever
1457 if (this->found_r_mips_none_)
1459 if (r_type == elfcpp::R_MIPS_NONE)
1461 this->r_sym_ = r_sym;
1463 this->found_r_mips_none_ = true;
1465 else if (!is_target_found())
1466 this->r_sym_ = r_sym;
1469 // This method is called when a new relocation R_TYPE for global symbol GSYM
1470 // is found in the stub section. Try to find stub target.
1472 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1474 // To find target symbol for this stub, trust the first R_MIPS_NONE
1475 // relocation, if any. Otherwise trust the first relocation, whatever
1477 if (this->found_r_mips_none_)
1479 if (r_type == elfcpp::R_MIPS_NONE)
1483 this->found_r_mips_none_ = true;
1485 else if (!is_target_found())
1489 // Return whether we found the stub target.
1491 is_target_found() const
1492 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1494 // Return whether this is a fn stub.
1497 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1499 // Return whether this is a call stub.
1501 is_call_stub() const
1502 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1504 // Return whether this is a call_fp stub.
1506 is_call_fp_stub() const
1507 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1509 // Return the output address.
1511 output_address() const
1513 return (this->object_->output_section(this->shndx_)->address()
1514 + this->object_->output_section_offset(this->shndx_));
1518 // The object of this stub section.
1519 Mips_relobj<size, big_endian>* object_;
1520 // The section index of this stub section.
1521 unsigned int shndx_;
1522 // The symbol index, if stub is for a local function.
1523 unsigned int r_sym_;
1524 // The symbol, if stub is for a global function.
1525 Mips_symbol<size>* gsym_;
1526 // True if we found R_MIPS_NONE relocation in this stub.
1527 bool found_r_mips_none_;
1530 // Mips_relobj class.
1532 template<int size, bool big_endian>
1533 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1535 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1536 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1537 Mips16_stubs_int_map;
1538 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1541 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1542 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1543 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1544 processor_specific_flags_(0), local_symbol_is_mips16_(),
1545 local_symbol_is_micromips_(), mips16_stub_sections_(),
1546 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1547 local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1548 got_info_(NULL), section_is_mips16_fn_stub_(),
1549 section_is_mips16_call_stub_(), section_is_mips16_call_fp_stub_(),
1550 pdr_shndx_(-1U), gprmask_(0), cprmask1_(0), cprmask2_(0), cprmask3_(0),
1553 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1554 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1560 // Downcast a base pointer to a Mips_relobj pointer. This is
1561 // not type-safe but we only use Mips_relobj not the base class.
1562 static Mips_relobj<size, big_endian>*
1563 as_mips_relobj(Relobj* relobj)
1564 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1566 // Downcast a base pointer to a Mips_relobj pointer. This is
1567 // not type-safe but we only use Mips_relobj not the base class.
1568 static const Mips_relobj<size, big_endian>*
1569 as_mips_relobj(const Relobj* relobj)
1570 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1572 // Processor-specific flags in ELF file header. This is valid only after
1575 processor_specific_flags() const
1576 { return this->processor_specific_flags_; }
1578 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1579 // index. This is only valid after do_count_local_symbol is called.
1581 local_symbol_is_mips16(unsigned int r_sym) const
1583 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1584 return this->local_symbol_is_mips16_[r_sym];
1587 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1588 // index. This is only valid after do_count_local_symbol is called.
1590 local_symbol_is_micromips(unsigned int r_sym) const
1592 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1593 return this->local_symbol_is_micromips_[r_sym];
1596 // Get or create MIPS16 stub section.
1597 Mips16_stub_section<size, big_endian>*
1598 get_mips16_stub_section(unsigned int shndx)
1600 typename Mips16_stubs_int_map::const_iterator it =
1601 this->mips16_stub_sections_.find(shndx);
1602 if (it != this->mips16_stub_sections_.end())
1603 return (*it).second;
1605 Mips16_stub_section<size, big_endian>* stub_section =
1606 new Mips16_stub_section<size, big_endian>(this, shndx);
1607 this->mips16_stub_sections_.insert(
1608 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1609 stub_section->shndx(), stub_section));
1610 return stub_section;
1613 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1614 // object doesn't have fn stub for R_SYM.
1615 Mips16_stub_section<size, big_endian>*
1616 get_local_mips16_fn_stub(unsigned int r_sym) const
1618 typename Mips16_stubs_int_map::const_iterator it =
1619 this->local_mips16_fn_stubs_.find(r_sym);
1620 if (it != this->local_mips16_fn_stubs_.end())
1621 return (*it).second;
1625 // Record that this object has MIPS16 fn stub for local symbol. This method
1626 // is only called if we decided not to discard the stub.
1628 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1630 gold_assert(stub->is_for_local_function());
1631 unsigned int r_sym = stub->r_sym();
1632 this->local_mips16_fn_stubs_.insert(
1633 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1637 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1638 // object doesn't have call stub for R_SYM.
1639 Mips16_stub_section<size, big_endian>*
1640 get_local_mips16_call_stub(unsigned int r_sym) const
1642 typename Mips16_stubs_int_map::const_iterator it =
1643 this->local_mips16_call_stubs_.find(r_sym);
1644 if (it != this->local_mips16_call_stubs_.end())
1645 return (*it).second;
1649 // Record that this object has MIPS16 call stub for local symbol. This method
1650 // is only called if we decided not to discard the stub.
1652 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1654 gold_assert(stub->is_for_local_function());
1655 unsigned int r_sym = stub->r_sym();
1656 this->local_mips16_call_stubs_.insert(
1657 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1661 // Record that we found "non 16-bit" call relocation against local symbol
1662 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1665 add_local_non_16bit_call(unsigned int symndx)
1666 { this->local_non_16bit_calls_.insert(symndx); }
1668 // Return true if there is any "non 16-bit" call relocation against local
1669 // symbol SYMNDX in this object.
1671 has_local_non_16bit_call_relocs(unsigned int symndx)
1673 return (this->local_non_16bit_calls_.find(symndx)
1674 != this->local_non_16bit_calls_.end());
1677 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1678 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1679 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1681 add_local_16bit_call(unsigned int symndx)
1682 { this->local_16bit_calls_.insert(symndx); }
1684 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1685 // symbol SYMNDX in this object.
1687 has_local_16bit_call_relocs(unsigned int symndx)
1689 return (this->local_16bit_calls_.find(symndx)
1690 != this->local_16bit_calls_.end());
1693 // Get gp value that was used to create this object.
1696 { return this->gp_; }
1698 // Return whether the object is a PIC object.
1701 { return this->is_pic_; }
1703 // Return whether the object uses N32 ABI.
1706 { return this->is_n32_; }
1708 // Return whether the object uses N64 ABI.
1711 { return size == 64; }
1713 // Return whether the object uses NewABI conventions.
1716 { return this->is_n32() || this->is_n64(); }
1718 // Return Mips_got_info for this object.
1719 Mips_got_info<size, big_endian>*
1720 get_got_info() const
1721 { return this->got_info_; }
1723 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1724 Mips_got_info<size, big_endian>*
1725 get_or_create_got_info()
1727 if (!this->got_info_)
1728 this->got_info_ = new Mips_got_info<size, big_endian>();
1729 return this->got_info_;
1732 // Set Mips_got_info for this object.
1734 set_got_info(Mips_got_info<size, big_endian>* got_info)
1735 { this->got_info_ = got_info; }
1737 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1738 // after do_read_symbols is called.
1740 is_mips16_stub_section(unsigned int shndx)
1742 return (is_mips16_fn_stub_section(shndx)
1743 || is_mips16_call_stub_section(shndx)
1744 || is_mips16_call_fp_stub_section(shndx));
1747 // Return TRUE if relocations in section SHNDX can refer directly to a
1748 // MIPS16 function rather than to a hard-float stub. This is only valid
1749 // after do_read_symbols is called.
1751 section_allows_mips16_refs(unsigned int shndx)
1753 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1756 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1757 // after do_read_symbols is called.
1759 is_mips16_fn_stub_section(unsigned int shndx)
1761 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1762 return this->section_is_mips16_fn_stub_[shndx];
1765 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1766 // after do_read_symbols is called.
1768 is_mips16_call_stub_section(unsigned int shndx)
1770 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1771 return this->section_is_mips16_call_stub_[shndx];
1774 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1775 // valid after do_read_symbols is called.
1777 is_mips16_call_fp_stub_section(unsigned int shndx)
1779 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1780 return this->section_is_mips16_call_fp_stub_[shndx];
1783 // Discard MIPS16 stub secions that are not needed.
1785 discard_mips16_stub_sections(Symbol_table* symtab);
1787 // Return whether there is a .reginfo section.
1789 has_reginfo_section() const
1790 { return this->has_reginfo_section_; }
1792 // Return gprmask from the .reginfo section of this object.
1795 { return this->gprmask_; }
1797 // Return cprmask1 from the .reginfo section of this object.
1800 { return this->cprmask1_; }
1802 // Return cprmask2 from the .reginfo section of this object.
1805 { return this->cprmask2_; }
1807 // Return cprmask3 from the .reginfo section of this object.
1810 { return this->cprmask3_; }
1812 // Return cprmask4 from the .reginfo section of this object.
1815 { return this->cprmask4_; }
1818 // Count the local symbols.
1820 do_count_local_symbols(Stringpool_template<char>*,
1821 Stringpool_template<char>*);
1823 // Read the symbol information.
1825 do_read_symbols(Read_symbols_data* sd);
1828 // The name of the options section.
1829 const char* mips_elf_options_section_name()
1830 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1832 // processor-specific flags in ELF file header.
1833 elfcpp::Elf_Word processor_specific_flags_;
1835 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1836 // This is only valid after do_count_local_symbol is called.
1837 std::vector<bool> local_symbol_is_mips16_;
1839 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1840 // This is only valid after do_count_local_symbol is called.
1841 std::vector<bool> local_symbol_is_micromips_;
1843 // Map from section index to the MIPS16 stub for that section. This contains
1844 // all stubs found in this object.
1845 Mips16_stubs_int_map mips16_stub_sections_;
1847 // Local symbols that have "non 16-bit" call relocation. This relocation
1848 // would need to refer to a MIPS16 fn stub, if there is one.
1849 std::set<unsigned int> local_non_16bit_calls_;
1851 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1852 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1853 // relocation that refers to the stub symbol.
1854 std::set<unsigned int> local_16bit_calls_;
1856 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1857 // This contains only the stubs that we decided not to discard.
1858 Mips16_stubs_int_map local_mips16_fn_stubs_;
1860 // Map from local symbol index to the MIPS16 call stub for that symbol.
1861 // This contains only the stubs that we decided not to discard.
1862 Mips16_stubs_int_map local_mips16_call_stubs_;
1864 // gp value that was used to create this object.
1866 // Whether the object is a PIC object.
1868 // Whether the object uses N32 ABI.
1870 // Whether the object contains a .reginfo section.
1871 bool has_reginfo_section_ : 1;
1872 // The Mips_got_info for this object.
1873 Mips_got_info<size, big_endian>* got_info_;
1875 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1876 // This is only valid after do_read_symbols is called.
1877 std::vector<bool> section_is_mips16_fn_stub_;
1879 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1880 // This is only valid after do_read_symbols is called.
1881 std::vector<bool> section_is_mips16_call_stub_;
1883 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1884 // This is only valid after do_read_symbols is called.
1885 std::vector<bool> section_is_mips16_call_fp_stub_;
1887 // .pdr section index.
1888 unsigned int pdr_shndx_;
1890 // gprmask from the .reginfo section of this object.
1892 // cprmask1 from the .reginfo section of this object.
1894 // cprmask2 from the .reginfo section of this object.
1896 // cprmask3 from the .reginfo section of this object.
1898 // cprmask4 from the .reginfo section of this object.
1902 // Mips_output_data_got class.
1904 template<int size, bool big_endian>
1905 class Mips_output_data_got : public Output_data_got<size, big_endian>
1907 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1908 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1910 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1913 Mips_output_data_got(Target_mips<size, big_endian>* target,
1914 Symbol_table* symtab, Layout* layout)
1915 : Output_data_got<size, big_endian>(), target_(target),
1916 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1917 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1918 secondary_got_relocs_()
1920 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1921 this->set_addralign(16);
1924 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1925 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1927 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1928 unsigned int symndx, Mips_address addend,
1929 unsigned int r_type, unsigned int shndx,
1930 bool is_section_symbol)
1932 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
1937 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
1938 // in OBJECT. FOR_CALL is true if the caller is only interested in
1939 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
1942 record_global_got_symbol(Mips_symbol<size>* mips_sym,
1943 Mips_relobj<size, big_endian>* object,
1944 unsigned int r_type, bool dyn_reloc, bool for_call)
1946 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
1947 dyn_reloc, for_call);
1950 // Record that OBJECT has a page relocation against symbol SYMNDX and
1951 // that ADDEND is the addend for that relocation.
1953 record_got_page_entry(Mips_relobj<size, big_endian>* object,
1954 unsigned int symndx, int addend)
1955 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
1957 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1958 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1959 // applied in a static link.
1961 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1962 Mips_symbol<size>* gsym)
1963 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1965 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1966 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1967 // relocation that needs to be applied in a static link.
1969 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1970 Sized_relobj_file<size, big_endian>* relobj,
1973 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1977 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
1978 // secondary GOT at OFFSET.
1980 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
1981 Mips_symbol<size>* gsym)
1983 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
1987 // Update GOT entry at OFFSET with VALUE.
1989 update_got_entry(unsigned int offset, Mips_address value)
1991 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
1994 // Return the number of entries in local part of the GOT. This includes
1995 // local entries, page entries and 2 reserved entries.
1997 get_local_gotno() const
1999 if (!this->multi_got())
2001 return (2 + this->master_got_info_->local_gotno()
2002 + this->master_got_info_->page_gotno());
2005 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2008 // Return dynamic symbol table index of the first symbol with global GOT
2011 first_global_got_dynsym_index() const
2012 { return this->first_global_got_dynsym_index_; }
2014 // Set dynamic symbol table index of the first symbol with global GOT entry.
2016 set_first_global_got_dynsym_index(unsigned int index)
2017 { this->first_global_got_dynsym_index_ = index; }
2019 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2020 // larger than 64K, create multi-GOT.
2022 lay_out_got(Layout* layout, Symbol_table* symtab,
2023 const Input_objects* input_objects);
2025 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2027 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2029 // Attempt to merge GOTs of different input objects.
2031 merge_gots(const Input_objects* input_objects);
2033 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
2034 // this would lead to overflow, true if they were merged successfully.
2036 merge_got_with(Mips_got_info<size, big_endian>* from,
2037 Mips_relobj<size, big_endian>* object,
2038 Mips_got_info<size, big_endian>* to);
2040 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2041 // use OBJECT's GOT.
2043 get_got_page_offset(Mips_address value,
2044 const Mips_relobj<size, big_endian>* object)
2046 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2047 ? this->master_got_info_
2048 : object->get_got_info());
2049 gold_assert(g != NULL);
2050 return g->get_got_page_offset(value, this);
2053 // Return the GOT offset of type GOT_TYPE of the global symbol
2054 // GSYM. For multi-GOT links, use OBJECT's GOT.
2055 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2056 Mips_relobj<size, big_endian>* object) const
2058 if (!this->multi_got())
2059 return gsym->got_offset(got_type);
2062 Mips_got_info<size, big_endian>* g = object->get_got_info();
2063 gold_assert(g != NULL);
2064 return gsym->got_offset(g->multigot_got_type(got_type));
2068 // Return the GOT offset of type GOT_TYPE of the local symbol
2071 got_offset(unsigned int symndx, unsigned int got_type,
2072 Sized_relobj_file<size, big_endian>* object,
2073 uint64_t addend) const
2074 { return object->local_got_offset(symndx, got_type, addend); }
2076 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2078 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2080 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2081 ? this->master_got_info_
2082 : object->get_got_info());
2083 gold_assert(g != NULL);
2084 return g->tls_ldm_offset();
2087 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2089 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2090 Mips_relobj<size, big_endian>* object)
2092 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2093 ? this->master_got_info_
2094 : object->get_got_info());
2095 gold_assert(g != NULL);
2096 g->set_tls_ldm_offset(tls_ldm_offset);
2099 // Return true for multi-GOT links.
2102 { return this->primary_got_ != NULL; }
2104 // Return the offset of OBJECT's GOT from the start of .got section.
2106 get_got_offset(const Mips_relobj<size, big_endian>* object)
2108 if (!this->multi_got())
2112 Mips_got_info<size, big_endian>* g = object->get_got_info();
2113 return g != NULL ? g->offset() : 0;
2117 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2119 add_reloc_only_entries()
2120 { this->master_got_info_->add_reloc_only_entries(this); }
2122 // Return offset of the primary GOT's entry for global symbol.
2124 get_primary_got_offset(const Mips_symbol<size>* sym) const
2126 gold_assert(sym->global_got_area() != GGA_NONE);
2127 return (this->get_local_gotno() + sym->dynsym_index()
2128 - this->first_global_got_dynsym_index()) * size/8;
2131 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2132 // Input argument GOT_OFFSET is always global offset from the start of
2133 // .got section, for both single and multi-GOT links.
2134 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2135 // links, the return value is object_got_offset - 0x7FF0, where
2136 // object_got_offset is offset in the OBJECT's GOT.
2138 gp_offset(unsigned int got_offset,
2139 const Mips_relobj<size, big_endian>* object) const
2141 return (this->address() + got_offset
2142 - this->target_->adjusted_gp_value(object));
2146 // Write out the GOT table.
2148 do_write(Output_file*);
2152 // This class represent dynamic relocations that need to be applied by
2153 // gold because we are using TLS relocations in a static link.
2157 Static_reloc(unsigned int got_offset, unsigned int r_type,
2158 Mips_symbol<size>* gsym)
2159 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2160 { this->u_.global.symbol = gsym; }
2162 Static_reloc(unsigned int got_offset, unsigned int r_type,
2163 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2164 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2166 this->u_.local.relobj = relobj;
2167 this->u_.local.index = index;
2170 // Return the GOT offset.
2173 { return this->got_offset_; }
2178 { return this->r_type_; }
2180 // Whether the symbol is global or not.
2182 symbol_is_global() const
2183 { return this->symbol_is_global_; }
2185 // For a relocation against a global symbol, the global symbol.
2189 gold_assert(this->symbol_is_global_);
2190 return this->u_.global.symbol;
2193 // For a relocation against a local symbol, the defining object.
2194 Sized_relobj_file<size, big_endian>*
2197 gold_assert(!this->symbol_is_global_);
2198 return this->u_.local.relobj;
2201 // For a relocation against a local symbol, the local symbol index.
2205 gold_assert(!this->symbol_is_global_);
2206 return this->u_.local.index;
2210 // GOT offset of the entry to which this relocation is applied.
2211 unsigned int got_offset_;
2212 // Type of relocation.
2213 unsigned int r_type_;
2214 // Whether this relocation is against a global symbol.
2215 bool symbol_is_global_;
2216 // A global or local symbol.
2221 // For a global symbol, the symbol itself.
2222 Mips_symbol<size>* symbol;
2226 // For a local symbol, the object defining object.
2227 Sized_relobj_file<size, big_endian>* relobj;
2228 // For a local symbol, the symbol index.
2235 Target_mips<size, big_endian>* target_;
2236 // The symbol table.
2237 Symbol_table* symbol_table_;
2240 // Static relocs to be applied to the GOT.
2241 std::vector<Static_reloc> static_relocs_;
2242 // .got section view.
2243 unsigned char* got_view_;
2244 // The dynamic symbol table index of the first symbol with global GOT entry.
2245 unsigned int first_global_got_dynsym_index_;
2246 // The master GOT information.
2247 Mips_got_info<size, big_endian>* master_got_info_;
2248 // The primary GOT information.
2249 Mips_got_info<size, big_endian>* primary_got_;
2250 // Secondary GOT fixups.
2251 std::vector<Static_reloc> secondary_got_relocs_;
2254 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2255 // two ways of creating these interfaces. The first is to add:
2257 // lui $25,%hi(func)
2259 // addiu $25,$25,%lo(func)
2261 // to a separate trampoline section. The second is to add:
2263 // lui $25,%hi(func)
2264 // addiu $25,$25,%lo(func)
2266 // immediately before a PIC function "func", but only if a function is at the
2267 // beginning of the section, and the section is not too heavily aligned (i.e we
2268 // would need to add no more than 2 nops before the stub.)
2270 // We only create stubs of the first type.
2272 template<int size, bool big_endian>
2273 class Mips_output_data_la25_stub : public Output_section_data
2275 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2278 Mips_output_data_la25_stub()
2279 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2282 // Create LA25 stub for a symbol.
2284 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2285 Mips_symbol<size>* gsym);
2287 // Return output address of a stub.
2289 stub_address(const Mips_symbol<size>* sym) const
2291 gold_assert(sym->has_la25_stub());
2292 return this->address() + sym->la25_stub_offset();
2297 do_adjust_output_section(Output_section* os)
2298 { os->set_entsize(0); }
2301 // Template for standard LA25 stub.
2302 static const uint32_t la25_stub_entry[];
2303 // Template for microMIPS LA25 stub.
2304 static const uint32_t la25_stub_micromips_entry[];
2306 // Set the final size.
2308 set_final_data_size()
2309 { this->set_data_size(this->symbols_.size() * 16); }
2311 // Create a symbol for SYM stub's value and size, to help make the
2312 // disassembly easier to read.
2314 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2315 Target_mips<size, big_endian>* target, uint64_t symsize);
2317 // Write to a map file.
2319 do_print_to_mapfile(Mapfile* mapfile) const
2320 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2322 // Write out the LA25 stub section.
2324 do_write(Output_file*);
2326 // Symbols that have LA25 stubs.
2327 std::vector<Mips_symbol<size>*> symbols_;
2330 // MIPS-specific relocation writer.
2332 template<int sh_type, bool dynamic, int size, bool big_endian>
2333 struct Mips_output_reloc_writer;
2335 template<int sh_type, bool dynamic, bool big_endian>
2336 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2338 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2339 typedef std::vector<Output_reloc_type> Relocs;
2342 write(typename Relocs::const_iterator p, unsigned char* pov)
2346 template<int sh_type, bool dynamic, bool big_endian>
2347 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2349 typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2350 typedef std::vector<Output_reloc_type> Relocs;
2353 write(typename Relocs::const_iterator p, unsigned char* pov)
2355 elfcpp::Mips64_rel_write<big_endian> orel(pov);
2356 orel.put_r_offset(p->get_address());
2357 orel.put_r_sym(p->get_symbol_index());
2358 orel.put_r_ssym(RSS_UNDEF);
2359 orel.put_r_type(p->type());
2360 if (p->type() == elfcpp::R_MIPS_REL32)
2361 orel.put_r_type2(elfcpp::R_MIPS_64);
2363 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2364 orel.put_r_type3(elfcpp::R_MIPS_NONE);
2368 template<int sh_type, bool dynamic, int size, bool big_endian>
2369 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2373 Mips_output_data_reloc(bool sort_relocs)
2374 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2378 // Write out the data.
2380 do_write(Output_file* of)
2382 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2384 this->template do_write_generic<Writer>(of);
2389 // A class to handle the PLT data.
2391 template<int size, bool big_endian>
2392 class Mips_output_data_plt : public Output_section_data
2394 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2395 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2396 size, big_endian> Reloc_section;
2399 // Create the PLT section. The ordinary .got section is an argument,
2400 // since we need to refer to the start.
2401 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2402 Target_mips<size, big_endian>* target)
2403 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2404 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2407 this->rel_ = new Reloc_section(false);
2408 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2409 elfcpp::SHF_ALLOC, this->rel_,
2410 ORDER_DYNAMIC_PLT_RELOCS, false);
2413 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2415 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2417 // Return the .rel.plt section data.
2418 const Reloc_section*
2420 { return this->rel_; }
2422 // Return the number of PLT entries.
2425 { return this->symbols_.size(); }
2427 // Return the offset of the first non-reserved PLT entry.
2429 first_plt_entry_offset() const
2430 { return sizeof(plt0_entry_o32); }
2432 // Return the size of a PLT entry.
2434 plt_entry_size() const
2435 { return sizeof(plt_entry); }
2437 // Set final PLT offsets. For each symbol, determine whether standard or
2438 // compressed (MIPS16 or microMIPS) PLT entry is used.
2442 // Return the offset of the first standard PLT entry.
2444 first_mips_plt_offset() const
2445 { return this->plt_header_size_; }
2447 // Return the offset of the first compressed PLT entry.
2449 first_comp_plt_offset() const
2450 { return this->plt_header_size_ + this->plt_mips_offset_; }
2452 // Return whether there are any standard PLT entries.
2454 has_standard_entries() const
2455 { return this->plt_mips_offset_ > 0; }
2457 // Return the output address of standard PLT entry.
2459 mips_entry_address(const Mips_symbol<size>* sym) const
2461 gold_assert (sym->has_mips_plt_offset());
2462 return (this->address() + this->first_mips_plt_offset()
2463 + sym->mips_plt_offset());
2466 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2468 comp_entry_address(const Mips_symbol<size>* sym) const
2470 gold_assert (sym->has_comp_plt_offset());
2471 return (this->address() + this->first_comp_plt_offset()
2472 + sym->comp_plt_offset());
2477 do_adjust_output_section(Output_section* os)
2478 { os->set_entsize(0); }
2480 // Write to a map file.
2482 do_print_to_mapfile(Mapfile* mapfile) const
2483 { mapfile->print_output_data(this, _(".plt")); }
2486 // Template for the first PLT entry.
2487 static const uint32_t plt0_entry_o32[];
2488 static const uint32_t plt0_entry_n32[];
2489 static const uint32_t plt0_entry_n64[];
2490 static const uint32_t plt0_entry_micromips_o32[];
2491 static const uint32_t plt0_entry_micromips32_o32[];
2493 // Template for subsequent PLT entries.
2494 static const uint32_t plt_entry[];
2495 static const uint32_t plt_entry_mips16_o32[];
2496 static const uint32_t plt_entry_micromips_o32[];
2497 static const uint32_t plt_entry_micromips32_o32[];
2499 // Set the final size.
2501 set_final_data_size()
2503 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2504 + this->plt_comp_offset_);
2507 // Write out the PLT data.
2509 do_write(Output_file*);
2511 // Return whether the plt header contains microMIPS code. For the sake of
2512 // cache alignment always use a standard header whenever any standard entries
2513 // are present even if microMIPS entries are present as well. This also lets
2514 // the microMIPS header rely on the value of $v0 only set by microMIPS
2515 // entries, for a small size reduction.
2517 is_plt_header_compressed() const
2519 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2520 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2523 // Return the size of the PLT header.
2525 get_plt_header_size() const
2527 if (this->target_->is_output_n64())
2528 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2529 else if (this->target_->is_output_n32())
2530 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2531 else if (!this->is_plt_header_compressed())
2532 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2533 else if (this->target_->use_32bit_micromips_instructions())
2534 return (2 * sizeof(plt0_entry_micromips32_o32)
2535 / sizeof(plt0_entry_micromips32_o32[0]));
2537 return (2 * sizeof(plt0_entry_micromips_o32)
2538 / sizeof(plt0_entry_micromips_o32[0]));
2541 // Return the PLT header entry.
2543 get_plt_header_entry() const
2545 if (this->target_->is_output_n64())
2546 return plt0_entry_n64;
2547 else if (this->target_->is_output_n32())
2548 return plt0_entry_n32;
2549 else if (!this->is_plt_header_compressed())
2550 return plt0_entry_o32;
2551 else if (this->target_->use_32bit_micromips_instructions())
2552 return plt0_entry_micromips32_o32;
2554 return plt0_entry_micromips_o32;
2557 // Return the size of the standard PLT entry.
2559 standard_plt_entry_size() const
2560 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2562 // Return the size of the compressed PLT entry.
2564 compressed_plt_entry_size() const
2566 gold_assert(!this->target_->is_output_newabi());
2568 if (!this->target_->is_output_micromips())
2569 return (2 * sizeof(plt_entry_mips16_o32)
2570 / sizeof(plt_entry_mips16_o32[0]));
2571 else if (this->target_->use_32bit_micromips_instructions())
2572 return (2 * sizeof(plt_entry_micromips32_o32)
2573 / sizeof(plt_entry_micromips32_o32[0]));
2575 return (2 * sizeof(plt_entry_micromips_o32)
2576 / sizeof(plt_entry_micromips_o32[0]));
2579 // The reloc section.
2580 Reloc_section* rel_;
2581 // The .got.plt section.
2582 Output_data_space* got_plt_;
2583 // Symbols that have PLT entry.
2584 std::vector<Mips_symbol<size>*> symbols_;
2585 // The offset of the next standard PLT entry to create.
2586 unsigned int plt_mips_offset_;
2587 // The offset of the next compressed PLT entry to create.
2588 unsigned int plt_comp_offset_;
2589 // The size of the PLT header in bytes.
2590 unsigned int plt_header_size_;
2592 Target_mips<size, big_endian>* target_;
2595 // A class to handle the .MIPS.stubs data.
2597 template<int size, bool big_endian>
2598 class Mips_output_data_mips_stubs : public Output_section_data
2600 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2602 // Unordered set of .MIPS.stubs entries.
2603 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2604 Mips_stubs_entry_set;
2607 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2608 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2609 stub_offsets_are_set_(false), target_(target)
2612 // Create entry for a symbol.
2614 make_entry(Mips_symbol<size>*);
2616 // Remove entry for a symbol.
2618 remove_entry(Mips_symbol<size>* gsym);
2620 // Set stub offsets for symbols. This method expects that the number of
2621 // entries in dynamic symbol table is set.
2623 set_lazy_stub_offsets();
2626 set_needs_dynsym_value();
2628 // Set the number of entries in dynamic symbol table.
2630 set_dynsym_count(unsigned int dynsym_count)
2631 { this->dynsym_count_ = dynsym_count; }
2633 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2634 // count is greater than 0x10000. If the dynamic symbol count is less than
2635 // 0x10000, the stub will be 4 bytes smaller.
2636 // There's no disadvantage from using microMIPS code here, so for the sake of
2637 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2638 // output produced at all. This has a benefit of stubs being shorter by
2639 // 4 bytes each too, unless in the insn32 mode.
2641 stub_max_size() const
2643 if (!this->target_->is_output_micromips()
2644 || this->target_->use_32bit_micromips_instructions())
2650 // Return the size of the stub. This method expects that the final dynsym
2655 gold_assert(this->dynsym_count_ != -1U);
2656 if (this->dynsym_count_ > 0x10000)
2657 return this->stub_max_size();
2659 return this->stub_max_size() - 4;
2662 // Return output address of a stub.
2664 stub_address(const Mips_symbol<size>* sym) const
2666 gold_assert(sym->has_lazy_stub());
2667 return this->address() + sym->lazy_stub_offset();
2672 do_adjust_output_section(Output_section* os)
2673 { os->set_entsize(0); }
2675 // Write to a map file.
2677 do_print_to_mapfile(Mapfile* mapfile) const
2678 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2681 static const uint32_t lazy_stub_normal_1[];
2682 static const uint32_t lazy_stub_normal_1_n64[];
2683 static const uint32_t lazy_stub_normal_2[];
2684 static const uint32_t lazy_stub_normal_2_n64[];
2685 static const uint32_t lazy_stub_big[];
2686 static const uint32_t lazy_stub_big_n64[];
2688 static const uint32_t lazy_stub_micromips_normal_1[];
2689 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2690 static const uint32_t lazy_stub_micromips_normal_2[];
2691 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2692 static const uint32_t lazy_stub_micromips_big[];
2693 static const uint32_t lazy_stub_micromips_big_n64[];
2695 static const uint32_t lazy_stub_micromips32_normal_1[];
2696 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2697 static const uint32_t lazy_stub_micromips32_normal_2[];
2698 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2699 static const uint32_t lazy_stub_micromips32_big[];
2700 static const uint32_t lazy_stub_micromips32_big_n64[];
2702 // Set the final size.
2704 set_final_data_size()
2705 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2707 // Write out the .MIPS.stubs data.
2709 do_write(Output_file*);
2711 // .MIPS.stubs symbols
2712 Mips_stubs_entry_set symbols_;
2713 // Number of entries in dynamic symbol table.
2714 unsigned int dynsym_count_;
2715 // Whether the stub offsets are set.
2716 bool stub_offsets_are_set_;
2718 Target_mips<size, big_endian>* target_;
2721 // This class handles Mips .reginfo output section.
2723 template<int size, bool big_endian>
2724 class Mips_output_section_reginfo : public Output_section_data
2726 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2729 Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2730 Valtype gprmask, Valtype cprmask1,
2731 Valtype cprmask2, Valtype cprmask3,
2733 : Output_section_data(24, 4, true), target_(target),
2734 gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2735 cprmask3_(cprmask3), cprmask4_(cprmask4)
2739 // Write to a map file.
2741 do_print_to_mapfile(Mapfile* mapfile) const
2742 { mapfile->print_output_data(this, _(".reginfo")); }
2744 // Write out reginfo section.
2746 do_write(Output_file* of);
2749 Target_mips<size, big_endian>* target_;
2751 // gprmask of the output .reginfo section.
2753 // cprmask1 of the output .reginfo section.
2755 // cprmask2 of the output .reginfo section.
2757 // cprmask3 of the output .reginfo section.
2759 // cprmask4 of the output .reginfo section.
2763 // The MIPS target has relocation types which default handling of relocatable
2764 // relocation cannot process. So we have to extend the default code.
2766 template<bool big_endian, typename Classify_reloc>
2767 class Mips_scan_relocatable_relocs :
2768 public Default_scan_relocatable_relocs<Classify_reloc>
2771 // Return the strategy to use for a local symbol which is a section
2772 // symbol, given the relocation type.
2773 inline Relocatable_relocs::Reloc_strategy
2774 local_section_strategy(unsigned int r_type, Relobj* object)
2776 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2777 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2782 case elfcpp::R_MIPS_26:
2783 return Relocatable_relocs::RELOC_SPECIAL;
2786 return Default_scan_relocatable_relocs<Classify_reloc>::
2787 local_section_strategy(r_type, object);
2793 // Mips_copy_relocs class. The only difference from the base class is the
2794 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2795 // Mips cannot convert all relocation types to dynamic relocs. If a reloc
2796 // cannot be made dynamic, a COPY reloc is emitted.
2798 template<int sh_type, int size, bool big_endian>
2799 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2803 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2806 // Emit any saved relocations which turn out to be needed. This is
2807 // called after all the relocs have been scanned.
2809 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2810 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2813 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2816 // Emit this reloc if appropriate. This is called after we have
2817 // scanned all the relocations, so we know whether we emitted a
2818 // COPY relocation for SYM_.
2820 emit_entry(Copy_reloc_entry& entry,
2821 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2822 Symbol_table* symtab, Layout* layout,
2823 Target_mips<size, big_endian>* target);
2827 // Return true if the symbol SYM should be considered to resolve local
2828 // to the current module, and false otherwise. The logic is taken from
2829 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2831 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2832 bool local_protected)
2834 // If it's a local sym, of course we resolve locally.
2838 // STV_HIDDEN or STV_INTERNAL ones must be local.
2839 if (sym->visibility() == elfcpp::STV_HIDDEN
2840 || sym->visibility() == elfcpp::STV_INTERNAL)
2843 // If we don't have a definition in a regular file, then we can't
2844 // resolve locally. The sym is either undefined or dynamic.
2845 if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic()
2846 || sym->is_undefined())
2849 // Forced local symbols resolve locally.
2850 if (sym->is_forced_local())
2853 // As do non-dynamic symbols.
2854 if (!has_dynsym_entry)
2857 // At this point, we know the symbol is defined and dynamic. In an
2858 // executable it must resolve locally, likewise when building symbolic
2859 // shared libraries.
2860 if (parameters->options().output_is_executable()
2861 || parameters->options().Bsymbolic())
2864 // Now deal with defined dynamic symbols in shared libraries. Ones
2865 // with default visibility might not resolve locally.
2866 if (sym->visibility() == elfcpp::STV_DEFAULT)
2869 // STV_PROTECTED non-function symbols are local.
2870 if (sym->type() != elfcpp::STT_FUNC)
2873 // Function pointer equality tests may require that STV_PROTECTED
2874 // symbols be treated as dynamic symbols. If the address of a
2875 // function not defined in an executable is set to that function's
2876 // plt entry in the executable, then the address of the function in
2877 // a shared library must also be the plt entry in the executable.
2878 return local_protected;
2881 // Return TRUE if references to this symbol always reference the symbol in this
2884 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
2886 return symbol_refs_local(sym, has_dynsym_entry, false);
2889 // Return TRUE if calls to this symbol always call the version in this object.
2891 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
2893 return symbol_refs_local(sym, has_dynsym_entry, true);
2896 // Compare GOT offsets of two symbols.
2898 template<int size, bool big_endian>
2900 got_offset_compare(Symbol* sym1, Symbol* sym2)
2902 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
2903 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
2904 unsigned int area1 = mips_sym1->global_got_area();
2905 unsigned int area2 = mips_sym2->global_got_area();
2906 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
2908 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
2910 return area1 < area2;
2912 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
2915 // This method divides dynamic symbols into symbols that have GOT entry, and
2916 // symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
2917 // Mips ABI requires that symbols with the GOT entry must be at the end of
2918 // dynamic symbol table, and the order in dynamic symbol table must match the
2921 template<int size, bool big_endian>
2923 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
2924 std::vector<Symbol*>* non_got_symbols,
2925 std::vector<Symbol*>* got_symbols)
2927 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
2928 p != dyn_symbols->end();
2931 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
2932 if (mips_sym->global_got_area() == GGA_NORMAL
2933 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
2934 got_symbols->push_back(mips_sym);
2936 non_got_symbols->push_back(mips_sym);
2939 std::sort(got_symbols->begin(), got_symbols->end(),
2940 got_offset_compare<size, big_endian>);
2943 // Functor class for processing the global symbol table.
2945 template<int size, bool big_endian>
2946 class Symbol_visitor_check_symbols
2949 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
2950 Layout* layout, Symbol_table* symtab)
2951 : target_(target), layout_(layout), symtab_(symtab)
2955 operator()(Sized_symbol<size>* sym)
2957 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
2958 if (local_pic_function<size, big_endian>(mips_sym))
2960 // SYM is a function that might need $25 to be valid on entry.
2961 // If we're creating a non-PIC relocatable object, mark SYM as
2962 // being PIC. If we're creating a non-relocatable object with
2963 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
2965 if (parameters->options().relocatable())
2967 if (!parameters->options().output_is_position_independent())
2968 mips_sym->set_pic();
2970 else if (mips_sym->has_nonpic_branches())
2972 this->target_->la25_stub_section(layout_)
2973 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
2979 Target_mips<size, big_endian>* target_;
2981 Symbol_table* symtab_;
2984 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
2985 // and endianness. The relocation format for MIPS-64 is non-standard.
2987 template<int sh_type, int size, bool big_endian>
2988 struct Mips_reloc_types;
2990 template<bool big_endian>
2991 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
2993 typedef typename elfcpp::Rel<32, big_endian> Reloc;
2994 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
2996 static typename elfcpp::Elf_types<32>::Elf_Swxword
2997 get_r_addend(const Reloc*)
3001 set_reloc_addend(Reloc_write*,
3002 typename elfcpp::Elf_types<32>::Elf_Swxword)
3003 { gold_unreachable(); }
3006 template<bool big_endian>
3007 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3009 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3010 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3012 static typename elfcpp::Elf_types<32>::Elf_Swxword
3013 get_r_addend(const Reloc* reloc)
3014 { return reloc->get_r_addend(); }
3017 set_reloc_addend(Reloc_write* p,
3018 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3019 { p->put_r_addend(val); }
3022 template<bool big_endian>
3023 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3025 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3026 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3028 static typename elfcpp::Elf_types<64>::Elf_Swxword
3029 get_r_addend(const Reloc*)
3033 set_reloc_addend(Reloc_write*,
3034 typename elfcpp::Elf_types<64>::Elf_Swxword)
3035 { gold_unreachable(); }
3038 template<bool big_endian>
3039 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3041 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3042 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3044 static typename elfcpp::Elf_types<64>::Elf_Swxword
3045 get_r_addend(const Reloc* reloc)
3046 { return reloc->get_r_addend(); }
3049 set_reloc_addend(Reloc_write* p,
3050 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3051 { p->put_r_addend(val); }
3054 // Forward declaration.
3056 mips_get_size_for_reloc(unsigned int, Relobj*);
3058 // A class for inquiring about properties of a relocation,
3059 // used while scanning relocs during a relocatable link and
3060 // garbage collection.
3062 template<int sh_type_, int size, bool big_endian>
3063 class Mips_classify_reloc;
3065 template<int sh_type_, bool big_endian>
3066 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3067 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3070 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3072 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3075 // Return the symbol referred to by the relocation.
3076 static inline unsigned int
3077 get_r_sym(const Reltype* reloc)
3078 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3080 // Return the type of the relocation.
3081 static inline unsigned int
3082 get_r_type(const Reltype* reloc)
3083 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3085 static inline unsigned int
3086 get_r_type2(const Reltype*)
3089 static inline unsigned int
3090 get_r_type3(const Reltype*)
3093 static inline unsigned int
3094 get_r_ssym(const Reltype*)
3097 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3098 static inline unsigned int
3099 get_r_addend(const Reltype* reloc)
3101 if (sh_type_ == elfcpp::SHT_REL)
3103 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3106 // Write the r_info field to a new reloc, using the r_info field from
3107 // the original reloc, replacing the r_sym field with R_SYM.
3109 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3111 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3112 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3115 // Write the r_addend field to a new reloc.
3117 put_r_addend(Reltype_write* to,
3118 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3119 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3121 // Return the size of the addend of the relocation (only used for SHT_REL).
3123 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3124 { return mips_get_size_for_reloc(r_type, obj); }
3127 template<int sh_type_, bool big_endian>
3128 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3129 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3132 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3134 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3137 // Return the symbol referred to by the relocation.
3138 static inline unsigned int
3139 get_r_sym(const Reltype* reloc)
3140 { return reloc->get_r_sym(); }
3142 // Return the r_type of the relocation.
3143 static inline unsigned int
3144 get_r_type(const Reltype* reloc)
3145 { return reloc->get_r_type(); }
3147 // Return the r_type2 of the relocation.
3148 static inline unsigned int
3149 get_r_type2(const Reltype* reloc)
3150 { return reloc->get_r_type2(); }
3152 // Return the r_type3 of the relocation.
3153 static inline unsigned int
3154 get_r_type3(const Reltype* reloc)
3155 { return reloc->get_r_type3(); }
3157 // Return the special symbol of the relocation.
3158 static inline unsigned int
3159 get_r_ssym(const Reltype* reloc)
3160 { return reloc->get_r_ssym(); }
3162 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3163 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3164 get_r_addend(const Reltype* reloc)
3166 if (sh_type_ == elfcpp::SHT_REL)
3168 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3171 // Write the r_info field to a new reloc, using the r_info field from
3172 // the original reloc, replacing the r_sym field with R_SYM.
3174 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3176 new_reloc->put_r_sym(r_sym);
3177 new_reloc->put_r_ssym(reloc->get_r_ssym());
3178 new_reloc->put_r_type3(reloc->get_r_type3());
3179 new_reloc->put_r_type2(reloc->get_r_type2());
3180 new_reloc->put_r_type(reloc->get_r_type());
3183 // Write the r_addend field to a new reloc.
3185 put_r_addend(Reltype_write* to,
3186 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3187 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3189 // Return the size of the addend of the relocation (only used for SHT_REL).
3191 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3192 { return mips_get_size_for_reloc(r_type, obj); }
3195 template<int size, bool big_endian>
3196 class Target_mips : public Sized_target<size, big_endian>
3198 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3199 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3201 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3202 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3203 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3205 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3209 Target_mips(const Target::Target_info* info = &mips_info)
3210 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3211 got_plt_(NULL), rel_dyn_(NULL), copy_relocs_(),
3212 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3213 mips_stubs_(NULL), mach_(0), layout_(NULL), got16_addends_(),
3214 entry_symbol_is_compressed_(false), insn32_(false)
3216 this->add_machine_extensions();
3219 // The offset of $gp from the beginning of the .got section.
3220 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3222 // The maximum size of the GOT for it to be addressable using 16-bit
3223 // offsets from $gp.
3224 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3226 // Make a new symbol table entry for the Mips target.
3228 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3229 { return new Mips_symbol<size>(); }
3231 // Process the relocations to determine unreferenced sections for
3232 // garbage collection.
3234 gc_process_relocs(Symbol_table* symtab,
3236 Sized_relobj_file<size, big_endian>* object,
3237 unsigned int data_shndx,
3238 unsigned int sh_type,
3239 const unsigned char* prelocs,
3241 Output_section* output_section,
3242 bool needs_special_offset_handling,
3243 size_t local_symbol_count,
3244 const unsigned char* plocal_symbols);
3246 // Scan the relocations to look for symbol adjustments.
3248 scan_relocs(Symbol_table* symtab,
3250 Sized_relobj_file<size, big_endian>* object,
3251 unsigned int data_shndx,
3252 unsigned int sh_type,
3253 const unsigned char* prelocs,
3255 Output_section* output_section,
3256 bool needs_special_offset_handling,
3257 size_t local_symbol_count,
3258 const unsigned char* plocal_symbols);
3260 // Finalize the sections.
3262 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3264 // Relocate a section.
3266 relocate_section(const Relocate_info<size, big_endian>*,
3267 unsigned int sh_type,
3268 const unsigned char* prelocs,
3270 Output_section* output_section,
3271 bool needs_special_offset_handling,
3272 unsigned char* view,
3273 Mips_address view_address,
3274 section_size_type view_size,
3275 const Reloc_symbol_changes*);
3277 // Scan the relocs during a relocatable link.
3279 scan_relocatable_relocs(Symbol_table* symtab,
3281 Sized_relobj_file<size, big_endian>* object,
3282 unsigned int data_shndx,
3283 unsigned int sh_type,
3284 const unsigned char* prelocs,
3286 Output_section* output_section,
3287 bool needs_special_offset_handling,
3288 size_t local_symbol_count,
3289 const unsigned char* plocal_symbols,
3290 Relocatable_relocs*);
3292 // Scan the relocs for --emit-relocs.
3294 emit_relocs_scan(Symbol_table* symtab,
3296 Sized_relobj_file<size, big_endian>* object,
3297 unsigned int data_shndx,
3298 unsigned int sh_type,
3299 const unsigned char* prelocs,
3301 Output_section* output_section,
3302 bool needs_special_offset_handling,
3303 size_t local_symbol_count,
3304 const unsigned char* plocal_syms,
3305 Relocatable_relocs* rr);
3307 // Emit relocations for a section.
3309 relocate_relocs(const Relocate_info<size, big_endian>*,
3310 unsigned int sh_type,
3311 const unsigned char* prelocs,
3313 Output_section* output_section,
3314 typename elfcpp::Elf_types<size>::Elf_Off
3315 offset_in_output_section,
3316 unsigned char* view,
3317 Mips_address view_address,
3318 section_size_type view_size,
3319 unsigned char* reloc_view,
3320 section_size_type reloc_view_size);
3322 // Perform target-specific processing in a relocatable link. This is
3323 // only used if we use the relocation strategy RELOC_SPECIAL.
3325 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3326 unsigned int sh_type,
3327 const unsigned char* preloc_in,
3329 Output_section* output_section,
3330 typename elfcpp::Elf_types<size>::Elf_Off
3331 offset_in_output_section,
3332 unsigned char* view,
3333 Mips_address view_address,
3334 section_size_type view_size,
3335 unsigned char* preloc_out);
3337 // Return whether SYM is defined by the ABI.
3339 do_is_defined_by_abi(const Symbol* sym) const
3341 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3342 || (strcmp(sym->name(), "_gp_disp") == 0)
3343 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3346 // Return the number of entries in the GOT.
3348 got_entry_count() const
3350 if (!this->has_got_section())
3352 return this->got_size() / (size/8);
3355 // Return the number of entries in the PLT.
3357 plt_entry_count() const
3359 if (this->plt_ == NULL)
3361 return this->plt_->entry_count();
3364 // Return the offset of the first non-reserved PLT entry.
3366 first_plt_entry_offset() const
3367 { return this->plt_->first_plt_entry_offset(); }
3369 // Return the size of each PLT entry.
3371 plt_entry_size() const
3372 { return this->plt_->plt_entry_size(); }
3374 // Get the GOT section, creating it if necessary.
3375 Mips_output_data_got<size, big_endian>*
3376 got_section(Symbol_table*, Layout*);
3378 // Get the GOT section.
3379 Mips_output_data_got<size, big_endian>*
3382 gold_assert(this->got_ != NULL);
3386 // Get the .MIPS.stubs section, creating it if necessary.
3387 Mips_output_data_mips_stubs<size, big_endian>*
3388 mips_stubs_section(Layout* layout);
3390 // Get the .MIPS.stubs section.
3391 Mips_output_data_mips_stubs<size, big_endian>*
3392 mips_stubs_section() const
3394 gold_assert(this->mips_stubs_ != NULL);
3395 return this->mips_stubs_;
3398 // Get the LA25 stub section, creating it if necessary.
3399 Mips_output_data_la25_stub<size, big_endian>*
3400 la25_stub_section(Layout*);
3402 // Get the LA25 stub section.
3403 Mips_output_data_la25_stub<size, big_endian>*
3406 gold_assert(this->la25_stub_ != NULL);
3407 return this->la25_stub_;
3410 // Get gp value. It has the value of .got + 0x7FF0.
3414 if (this->gp_ != NULL)
3415 return this->gp_->value();
3419 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3420 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3422 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3424 if (this->gp_ == NULL)
3427 bool multi_got = false;
3428 if (this->has_got_section())
3429 multi_got = this->got_section()->multi_got();
3431 return this->gp_->value();
3433 return this->gp_->value() + this->got_section()->get_got_offset(object);
3436 // Get the dynamic reloc section, creating it if necessary.
3438 rel_dyn_section(Layout*);
3441 do_has_custom_set_dynsym_indexes() const
3444 // Don't emit input .reginfo sections to output .reginfo.
3446 do_should_include_section(elfcpp::Elf_Word sh_type) const
3447 { return sh_type != elfcpp::SHT_MIPS_REGINFO; }
3449 // Set the dynamic symbol indexes. INDEX is the index of the first
3450 // global dynamic symbol. Pointers to the symbols are stored into the
3451 // vector SYMS. The names are added to DYNPOOL. This returns an
3452 // updated dynamic symbol index.
3454 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3455 std::vector<Symbol*>* syms, Stringpool* dynpool,
3456 Versions* versions, Symbol_table* symtab) const;
3458 // Remove .MIPS.stubs entry for a symbol.
3460 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3462 if (this->mips_stubs_ != NULL)
3463 this->mips_stubs_->remove_entry(sym);
3466 // The value to write into got[1] for SVR4 targets, to identify it is
3467 // a GNU object. The dynamic linker can then use got[1] to store the
3470 mips_elf_gnu_got1_mask()
3472 if (this->is_output_n64())
3473 return (uint64_t)1 << 63;
3478 // Whether the output has microMIPS code. This is valid only after
3479 // merge_processor_specific_flags() is called.
3481 is_output_micromips() const
3483 gold_assert(this->are_processor_specific_flags_set());
3484 return elfcpp::is_micromips(this->processor_specific_flags());
3487 // Whether the output uses N32 ABI. This is valid only after
3488 // merge_processor_specific_flags() is called.
3490 is_output_n32() const
3492 gold_assert(this->are_processor_specific_flags_set());
3493 return elfcpp::abi_n32(this->processor_specific_flags());
3496 // Whether the output uses N64 ABI.
3498 is_output_n64() const
3499 { return size == 64; }
3501 // Whether the output uses NEWABI. This is valid only after
3502 // merge_processor_specific_flags() is called.
3504 is_output_newabi() const
3505 { return this->is_output_n32() || this->is_output_n64(); }
3507 // Whether we can only use 32-bit microMIPS instructions.
3509 use_32bit_micromips_instructions() const
3510 { return this->insn32_; }
3512 // Return the r_sym field from a relocation.
3514 get_r_sym(const unsigned char* preloc) const
3516 // Since REL and RELA relocs share the same structure through
3517 // the r_info field, we can just use REL here.
3518 Reltype rel(preloc);
3519 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3524 // Return the value to use for a dynamic symbol which requires special
3525 // treatment. This is how we support equality comparisons of function
3526 // pointers across shared library boundaries, as described in the
3527 // processor specific ABI supplement.
3529 do_dynsym_value(const Symbol* gsym) const;
3531 // Make an ELF object.
3533 do_make_elf_object(const std::string&, Input_file*, off_t,
3534 const elfcpp::Ehdr<size, big_endian>& ehdr);
3537 do_make_elf_object(const std::string&, Input_file*, off_t,
3538 const elfcpp::Ehdr<size, !big_endian>&)
3539 { gold_unreachable(); }
3541 // Adjust ELF file header.
3543 do_adjust_elf_header(unsigned char* view, int len);
3545 // Get the custom dynamic tag value.
3547 do_dynamic_tag_custom_value(elfcpp::DT) const;
3549 // Adjust the value written to the dynamic symbol table.
3551 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3553 elfcpp::Sym<size, big_endian> isym(view);
3554 elfcpp::Sym_write<size, big_endian> osym(view);
3555 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3557 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3558 // to treat compressed symbols like any other.
3559 Mips_address value = isym.get_st_value();
3560 if (mips_sym->is_mips16() && value != 0)
3562 if (!mips_sym->has_mips16_fn_stub())
3566 // If we have a MIPS16 function with a stub, the dynamic symbol
3567 // must refer to the stub, since only the stub uses the standard
3568 // calling conventions. Stub contains MIPS32 code, so don't add +1
3571 // There is a code which does this in the method
3572 // Target_mips::do_dynsym_value, but that code will only be
3573 // executed if the symbol is from dynobj.
3574 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3577 Mips16_stub_section<size, big_endian>* fn_stub =
3578 mips_sym->template get_mips16_fn_stub<big_endian>();
3579 value = fn_stub->output_address();
3580 osym.put_st_size(fn_stub->section_size());
3583 osym.put_st_value(value);
3584 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3585 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3587 else if ((mips_sym->is_micromips()
3588 // Stubs are always microMIPS if there is any microMIPS code in
3590 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3593 osym.put_st_value(value | 1);
3594 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3595 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3600 // The class which scans relocations.
3608 get_reference_flags(unsigned int r_type);
3611 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3612 Sized_relobj_file<size, big_endian>* object,
3613 unsigned int data_shndx,
3614 Output_section* output_section,
3615 const Reltype& reloc, unsigned int r_type,
3616 const elfcpp::Sym<size, big_endian>& lsym,
3620 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3621 Sized_relobj_file<size, big_endian>* object,
3622 unsigned int data_shndx,
3623 Output_section* output_section,
3624 const Relatype& reloc, unsigned int r_type,
3625 const elfcpp::Sym<size, big_endian>& lsym,
3629 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3630 Sized_relobj_file<size, big_endian>* object,
3631 unsigned int data_shndx,
3632 Output_section* output_section,
3633 const Relatype* rela,
3635 unsigned int rel_type,
3636 unsigned int r_type,
3637 const elfcpp::Sym<size, big_endian>& lsym,
3641 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3642 Sized_relobj_file<size, big_endian>* object,
3643 unsigned int data_shndx,
3644 Output_section* output_section,
3645 const Reltype& reloc, unsigned int r_type,
3649 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3650 Sized_relobj_file<size, big_endian>* object,
3651 unsigned int data_shndx,
3652 Output_section* output_section,
3653 const Relatype& reloc, unsigned int r_type,
3657 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3658 Sized_relobj_file<size, big_endian>* object,
3659 unsigned int data_shndx,
3660 Output_section* output_section,
3661 const Relatype* rela,
3663 unsigned int rel_type,
3664 unsigned int r_type,
3668 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3670 Sized_relobj_file<size, big_endian>*,
3675 const elfcpp::Sym<size, big_endian>&)
3679 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3681 Sized_relobj_file<size, big_endian>*,
3685 unsigned int, Symbol*)
3689 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3691 Sized_relobj_file<size, big_endian>*,
3696 const elfcpp::Sym<size, big_endian>&)
3700 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3702 Sized_relobj_file<size, big_endian>*,
3706 unsigned int, Symbol*)
3710 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3711 unsigned int r_type);
3714 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3715 unsigned int r_type, Symbol*);
3718 // The class which implements relocation.
3728 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3730 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3731 unsigned int r_type,
3732 Output_section* output_section,
3733 Target_mips* target);
3735 // Do a relocation. Return false if the caller should not issue
3736 // any warnings about this relocation.
3738 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3739 Target_mips*, Output_section*, size_t, const unsigned char*,
3740 const Sized_symbol<size>*, const Symbol_value<size>*,
3741 unsigned char*, Mips_address, section_size_type);
3744 // This POD class holds the dynamic relocations that should be emitted instead
3745 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3746 // relocations if it turns out that the symbol does not have static
3751 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3752 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3753 Output_section* output_section, Mips_address r_offset)
3754 : sym_(sym), r_type_(r_type), relobj_(relobj),
3755 shndx_(shndx), output_section_(output_section),
3759 // Emit this reloc if appropriate. This is called after we have
3760 // scanned all the relocations, so we know whether the symbol has
3761 // static relocations.
3763 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3764 Symbol_table* symtab)
3766 if (!this->sym_->has_static_relocs())
3768 got->record_global_got_symbol(this->sym_, this->relobj_,
3769 this->r_type_, true, false);
3770 if (!symbol_references_local(this->sym_,
3771 this->sym_->should_add_dynsym_entry(symtab)))
3772 rel_dyn->add_global(this->sym_, this->r_type_,
3773 this->output_section_, this->relobj_,
3774 this->shndx_, this->r_offset_);
3776 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3777 this->output_section_, this->relobj_,
3778 this->shndx_, this->r_offset_);
3783 Mips_symbol<size>* sym_;
3784 unsigned int r_type_;
3785 Mips_relobj<size, big_endian>* relobj_;
3786 unsigned int shndx_;
3787 Output_section* output_section_;
3788 Mips_address r_offset_;
3791 // Adjust TLS relocation type based on the options and whether this
3792 // is a local symbol.
3793 static tls::Tls_optimization
3794 optimize_tls_reloc(bool is_final, int r_type);
3796 // Return whether there is a GOT section.
3798 has_got_section() const
3799 { return this->got_ != NULL; }
3801 // Check whether the given ELF header flags describe a 32-bit binary.
3803 mips_32bit_flags(elfcpp::Elf_Word);
3806 mach_mips3000 = 3000,
3807 mach_mips3900 = 3900,
3808 mach_mips4000 = 4000,
3809 mach_mips4010 = 4010,
3810 mach_mips4100 = 4100,
3811 mach_mips4111 = 4111,
3812 mach_mips4120 = 4120,
3813 mach_mips4300 = 4300,
3814 mach_mips4400 = 4400,
3815 mach_mips4600 = 4600,
3816 mach_mips4650 = 4650,
3817 mach_mips5000 = 5000,
3818 mach_mips5400 = 5400,
3819 mach_mips5500 = 5500,
3820 mach_mips6000 = 6000,
3821 mach_mips7000 = 7000,
3822 mach_mips8000 = 8000,
3823 mach_mips9000 = 9000,
3824 mach_mips10000 = 10000,
3825 mach_mips12000 = 12000,
3826 mach_mips14000 = 14000,
3827 mach_mips16000 = 16000,
3830 mach_mips_loongson_2e = 3001,
3831 mach_mips_loongson_2f = 3002,
3832 mach_mips_loongson_3a = 3003,
3833 mach_mips_sb1 = 12310201, // octal 'SB', 01
3834 mach_mips_octeon = 6501,
3835 mach_mips_octeonp = 6601,
3836 mach_mips_octeon2 = 6502,
3837 mach_mips_xlr = 887682, // decimal 'XLR'
3838 mach_mipsisa32 = 32,
3839 mach_mipsisa32r2 = 33,
3840 mach_mipsisa64 = 64,
3841 mach_mipsisa64r2 = 65,
3842 mach_mips_micromips = 96
3845 // Return the MACH for a MIPS e_flags value.
3847 elf_mips_mach(elfcpp::Elf_Word);
3849 // Check whether machine EXTENSION is an extension of machine BASE.
3851 mips_mach_extends(unsigned int, unsigned int);
3853 // Merge processor specific flags.
3855 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word, bool);
3857 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
3862 // True if we are linking for CPUs that are faster if JALR is converted to
3863 // BAL. This should be safe for all architectures. We enable this predicate
3869 // True if we are linking for CPUs that are faster if JR is converted to B.
3870 // This should be safe for all architectures. We enable this predicate for
3876 // Return the size of the GOT section.
3880 gold_assert(this->got_ != NULL);
3881 return this->got_->data_size();
3884 // Create a PLT entry for a global symbol referenced by r_type relocation.
3886 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
3887 unsigned int r_type);
3889 // Get the PLT section.
3890 Mips_output_data_plt<size, big_endian>*
3893 gold_assert(this->plt_ != NULL);
3897 // Get the GOT PLT section.
3898 const Mips_output_data_plt<size, big_endian>*
3899 got_plt_section() const
3901 gold_assert(this->got_plt_ != NULL);
3902 return this->got_plt_;
3905 // Copy a relocation against a global symbol.
3907 copy_reloc(Symbol_table* symtab, Layout* layout,
3908 Sized_relobj_file<size, big_endian>* object,
3909 unsigned int shndx, Output_section* output_section,
3910 Symbol* sym, unsigned int r_type, Mips_address r_offset)
3912 this->copy_relocs_.copy_reloc(symtab, layout,
3913 symtab->get_sized_symbol<size>(sym),
3914 object, shndx, output_section,
3915 r_type, r_offset, 0,
3916 this->rel_dyn_section(layout));
3920 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3921 Mips_relobj<size, big_endian>* relobj,
3922 unsigned int shndx, Output_section* output_section,
3923 Mips_address r_offset)
3925 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
3926 output_section, r_offset));
3929 // Calculate value of _gp symbol.
3931 set_gp(Layout*, Symbol_table*);
3934 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
3936 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
3938 // Adds entries that describe how machines relate to one another. The entries
3939 // are ordered topologically with MIPS I extensions listed last. First
3940 // element is extension, second element is base.
3942 add_machine_extensions()
3944 // MIPS64r2 extensions.
3945 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
3946 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
3947 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
3949 // MIPS64 extensions.
3950 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
3951 this->add_extension(mach_mips_sb1, mach_mipsisa64);
3952 this->add_extension(mach_mips_xlr, mach_mipsisa64);
3953 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64);
3955 // MIPS V extensions.
3956 this->add_extension(mach_mipsisa64, mach_mips5);
3958 // R10000 extensions.
3959 this->add_extension(mach_mips12000, mach_mips10000);
3960 this->add_extension(mach_mips14000, mach_mips10000);
3961 this->add_extension(mach_mips16000, mach_mips10000);
3963 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
3964 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
3965 // better to allow vr5400 and vr5500 code to be merged anyway, since
3966 // many libraries will just use the core ISA. Perhaps we could add
3967 // some sort of ASE flag if this ever proves a problem.
3968 this->add_extension(mach_mips5500, mach_mips5400);
3969 this->add_extension(mach_mips5400, mach_mips5000);
3971 // MIPS IV extensions.
3972 this->add_extension(mach_mips5, mach_mips8000);
3973 this->add_extension(mach_mips10000, mach_mips8000);
3974 this->add_extension(mach_mips5000, mach_mips8000);
3975 this->add_extension(mach_mips7000, mach_mips8000);
3976 this->add_extension(mach_mips9000, mach_mips8000);
3978 // VR4100 extensions.
3979 this->add_extension(mach_mips4120, mach_mips4100);
3980 this->add_extension(mach_mips4111, mach_mips4100);
3982 // MIPS III extensions.
3983 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
3984 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
3985 this->add_extension(mach_mips8000, mach_mips4000);
3986 this->add_extension(mach_mips4650, mach_mips4000);
3987 this->add_extension(mach_mips4600, mach_mips4000);
3988 this->add_extension(mach_mips4400, mach_mips4000);
3989 this->add_extension(mach_mips4300, mach_mips4000);
3990 this->add_extension(mach_mips4100, mach_mips4000);
3991 this->add_extension(mach_mips4010, mach_mips4000);
3993 // MIPS32 extensions.
3994 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
3996 // MIPS II extensions.
3997 this->add_extension(mach_mips4000, mach_mips6000);
3998 this->add_extension(mach_mipsisa32, mach_mips6000);
4000 // MIPS I extensions.
4001 this->add_extension(mach_mips6000, mach_mips3000);
4002 this->add_extension(mach_mips3900, mach_mips3000);
4005 // Add value to MIPS extenstions.
4007 add_extension(unsigned int base, unsigned int extension)
4009 std::pair<unsigned int, unsigned int> ext(base, extension);
4010 this->mips_mach_extensions_.push_back(ext);
4013 // Return the number of entries in the .dynsym section.
4014 unsigned int get_dt_mips_symtabno() const
4016 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4017 / elfcpp::Elf_sizes<size>::sym_size));
4018 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4021 // Information about this specific target which we pass to the
4022 // general Target structure.
4023 static const Target::Target_info mips_info;
4025 Mips_output_data_got<size, big_endian>* got_;
4026 // gp symbol. It has the value of .got + 0x7FF0.
4027 Sized_symbol<size>* gp_;
4029 Mips_output_data_plt<size, big_endian>* plt_;
4030 // The GOT PLT section.
4031 Output_data_space* got_plt_;
4032 // The dynamic reloc section.
4033 Reloc_section* rel_dyn_;
4034 // Relocs saved to avoid a COPY reloc.
4035 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4037 // A list of dyn relocs to be saved.
4038 std::vector<Dyn_reloc> dyn_relocs_;
4040 // The LA25 stub section.
4041 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4042 // Architecture extensions.
4043 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4045 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4050 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4052 // Whether the entry symbol is mips16 or micromips.
4053 bool entry_symbol_is_compressed_;
4055 // Whether we can use only 32-bit microMIPS instructions.
4056 // TODO(sasa): This should be a linker option.
4060 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4061 // It records high part of the relocation pair.
4063 template<int size, bool big_endian>
4066 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4068 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4069 const Symbol_value<size>* _psymval, Mips_address _addend,
4070 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4071 Mips_address _address = 0, bool _gp_disp = false)
4072 : view(_view), object(_object), psymval(_psymval), addend(_addend),
4073 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4074 address(_address), gp_disp(_gp_disp)
4077 unsigned char* view;
4078 const Mips_relobj<size, big_endian>* object;
4079 const Symbol_value<size>* psymval;
4080 Mips_address addend;
4081 unsigned int r_type;
4083 bool extract_addend;
4084 Mips_address address;
4088 template<int size, bool big_endian>
4089 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4091 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4092 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4093 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4094 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4095 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4100 STATUS_OKAY, // No error during relocation.
4101 STATUS_OVERFLOW, // Relocation overflow.
4102 STATUS_BAD_RELOC // Relocation cannot be applied.
4106 typedef Relocate_functions<size, big_endian> Base;
4107 typedef Mips_relocate_functions<size, big_endian> This;
4109 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4110 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4112 template<int valsize>
4113 static inline typename This::Status
4114 check_overflow(Valtype value)
4117 return (Bits<valsize>::has_overflow32(value)
4118 ? This::STATUS_OVERFLOW
4119 : This::STATUS_OKAY);
4121 return (Bits<valsize>::has_overflow(value)
4122 ? This::STATUS_OVERFLOW
4123 : This::STATUS_OKAY);
4127 should_shuffle_micromips_reloc(unsigned int r_type)
4129 return (micromips_reloc(r_type)
4130 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4131 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4135 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4136 // Most mips16 instructions are 16 bits, but these instructions
4139 // The format of these instructions is:
4141 // +--------------+--------------------------------+
4142 // | JALX | X| Imm 20:16 | Imm 25:21 |
4143 // +--------------+--------------------------------+
4144 // | Immediate 15:0 |
4145 // +-----------------------------------------------+
4147 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4148 // Note that the immediate value in the first word is swapped.
4150 // When producing a relocatable object file, R_MIPS16_26 is
4151 // handled mostly like R_MIPS_26. In particular, the addend is
4152 // stored as a straight 26-bit value in a 32-bit instruction.
4153 // (gas makes life simpler for itself by never adjusting a
4154 // R_MIPS16_26 reloc to be against a section, so the addend is
4155 // always zero). However, the 32 bit instruction is stored as 2
4156 // 16-bit values, rather than a single 32-bit value. In a
4157 // big-endian file, the result is the same; in a little-endian
4158 // file, the two 16-bit halves of the 32 bit value are swapped.
4159 // This is so that a disassembler can recognize the jal
4162 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4163 // instruction stored as two 16-bit values. The addend A is the
4164 // contents of the targ26 field. The calculation is the same as
4165 // R_MIPS_26. When storing the calculated value, reorder the
4166 // immediate value as shown above, and don't forget to store the
4167 // value as two 16-bit values.
4169 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4173 // +--------+----------------------+
4177 // +--------+----------------------+
4180 // +----------+------+-------------+
4182 // | sub1 | | sub2 |
4183 // |0 9|10 15|16 31|
4184 // +----------+--------------------+
4185 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4186 // ((sub1 << 16) | sub2)).
4188 // When producing a relocatable object file, the calculation is
4189 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4190 // When producing a fully linked file, the calculation is
4191 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4192 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4194 // The table below lists the other MIPS16 instruction relocations.
4195 // Each one is calculated in the same way as the non-MIPS16 relocation
4196 // given on the right, but using the extended MIPS16 layout of 16-bit
4197 // immediate fields:
4199 // R_MIPS16_GPREL R_MIPS_GPREL16
4200 // R_MIPS16_GOT16 R_MIPS_GOT16
4201 // R_MIPS16_CALL16 R_MIPS_CALL16
4202 // R_MIPS16_HI16 R_MIPS_HI16
4203 // R_MIPS16_LO16 R_MIPS_LO16
4205 // A typical instruction will have a format like this:
4207 // +--------------+--------------------------------+
4208 // | EXTEND | Imm 10:5 | Imm 15:11 |
4209 // +--------------+--------------------------------+
4210 // | Major | rx | ry | Imm 4:0 |
4211 // +--------------+--------------------------------+
4213 // EXTEND is the five bit value 11110. Major is the instruction
4216 // All we need to do here is shuffle the bits appropriately.
4217 // As above, the two 16-bit halves must be swapped on a
4218 // little-endian system.
4220 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4221 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4222 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4225 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4228 if (!mips16_reloc(r_type)
4229 && !should_shuffle_micromips_reloc(r_type))
4232 // Pick up the first and second halfwords of the instruction.
4233 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4234 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4237 if (micromips_reloc(r_type)
4238 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4239 val = first << 16 | second;
4240 else if (r_type != elfcpp::R_MIPS16_26)
4241 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4242 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4244 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4245 | ((first & 0x1f) << 21) | second);
4247 elfcpp::Swap<32, big_endian>::writeval(view, val);
4251 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4253 if (!mips16_reloc(r_type)
4254 && !should_shuffle_micromips_reloc(r_type))
4257 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4258 Valtype16 first, second;
4260 if (micromips_reloc(r_type)
4261 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4263 second = val & 0xffff;
4266 else if (r_type != elfcpp::R_MIPS16_26)
4268 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4269 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4273 second = val & 0xffff;
4274 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4275 | ((val >> 21) & 0x1f);
4278 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4279 elfcpp::Swap<16, big_endian>::writeval(view, first);
4282 // R_MIPS_16: S + sign-extend(A)
4283 static inline typename This::Status
4284 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4285 const Symbol_value<size>* psymval, Mips_address addend_a,
4286 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4288 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4289 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4291 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4294 Valtype x = psymval->value(object, addend);
4295 val = Bits<16>::bit_select32(val, x, 0xffffU);
4299 *calculated_value = x;
4300 return This::STATUS_OKAY;
4303 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4305 return check_overflow<16>(x);
4309 static inline typename This::Status
4310 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4311 const Symbol_value<size>* psymval, Mips_address addend_a,
4312 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4314 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4315 Valtype addend = (extract_addend
4316 ? elfcpp::Swap<32, big_endian>::readval(wv)
4318 Valtype x = psymval->value(object, addend);
4321 *calculated_value = x;
4323 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4325 return This::STATUS_OKAY;
4328 // R_MIPS_JALR, R_MICROMIPS_JALR
4329 static inline typename This::Status
4330 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4331 const Symbol_value<size>* psymval, Mips_address address,
4332 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4333 unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4334 bool calculate_only, Valtype* calculated_value)
4336 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4337 Valtype addend = extract_addend ? 0 : addend_a;
4338 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4340 // Try converting J(AL)R to B(AL), if the target is in range.
4341 if (!parameters->options().relocatable()
4342 && r_type == elfcpp::R_MIPS_JALR
4344 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4345 || (jr_to_b && val == 0x03200008))) // jr t9
4347 int offset = psymval->value(object, addend) - (address + 4);
4348 if (!Bits<18>::has_overflow32(offset))
4350 if (val == 0x03200008) // jr t9
4351 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4353 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4358 *calculated_value = val;
4360 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4362 return This::STATUS_OKAY;
4365 // R_MIPS_PC32: S + A - P
4366 static inline typename This::Status
4367 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4368 const Symbol_value<size>* psymval, Mips_address address,
4369 Mips_address addend_a, bool extract_addend, bool calculate_only,
4370 Valtype* calculated_value)
4372 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4373 Valtype addend = (extract_addend
4374 ? elfcpp::Swap<32, big_endian>::readval(wv)
4376 Valtype x = psymval->value(object, addend) - address;
4379 *calculated_value = x;
4381 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4383 return This::STATUS_OKAY;
4386 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4387 static inline typename This::Status
4388 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4389 const Symbol_value<size>* psymval, Mips_address address,
4390 bool local, Mips_address addend_a, bool extract_addend,
4391 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4392 bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4394 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4395 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4400 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4401 addend = (val & 0x03ffffff) << 1;
4403 addend = (val & 0x03ffffff) << 2;
4408 // Make sure the target of JALX is word-aligned. Bit 0 must be
4409 // the correct ISA mode selector and bit 1 must be 0.
4410 if (!calculate_only && cross_mode_jump
4411 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4413 gold_warning(_("JALX to a non-word-aligned address"));
4414 return This::STATUS_BAD_RELOC;
4417 // Shift is 2, unusually, for microMIPS JALX.
4418 unsigned int shift =
4419 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4423 x = addend | ((address + 4) & (0xfc000000 << shift));
4427 x = Bits<27>::sign_extend32(addend);
4429 x = Bits<28>::sign_extend32(addend);
4431 x = psymval->value(object, x) >> shift;
4433 if (!calculate_only && !local && !gsym->is_weak_undefined())
4435 if ((x >> 26) != ((address + 4) >> (26 + shift)))
4437 gold_error(_("relocation truncated to fit: %u against '%s'"),
4438 r_type, gsym->name());
4439 return This::STATUS_OVERFLOW;
4443 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4445 // If required, turn JAL into JALX.
4446 if (cross_mode_jump)
4449 Valtype32 opcode = val >> 26;
4450 Valtype32 jalx_opcode;
4452 // Check to see if the opcode is already JAL or JALX.
4453 if (r_type == elfcpp::R_MIPS16_26)
4455 ok = (opcode == 0x6) || (opcode == 0x7);
4458 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4460 ok = (opcode == 0x3d) || (opcode == 0x3c);
4465 ok = (opcode == 0x3) || (opcode == 0x1d);
4469 // If the opcode is not JAL or JALX, there's a problem. We cannot
4470 // convert J or JALS to JALX.
4471 if (!calculate_only && !ok)
4473 gold_error(_("Unsupported jump between ISA modes; consider "
4474 "recompiling with interlinking enabled."));
4475 return This::STATUS_BAD_RELOC;
4478 // Make this the JALX opcode.
4479 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4482 // Try converting JAL to BAL, if the target is in range.
4483 if (!parameters->options().relocatable()
4486 && r_type == elfcpp::R_MIPS_26
4487 && (val >> 26) == 0x3))) // jal addr
4489 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4490 int offset = dest - (address + 4);
4491 if (!Bits<18>::has_overflow32(offset))
4493 if (val == 0x03200008) // jr t9
4494 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4496 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4501 *calculated_value = val;
4503 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4505 return This::STATUS_OKAY;
4509 static inline typename This::Status
4510 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4511 const Symbol_value<size>* psymval, Mips_address address,
4512 Mips_address addend_a, bool extract_addend, bool calculate_only,
4513 Valtype* calculated_value)
4515 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4516 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4518 Valtype addend = (extract_addend
4519 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4522 Valtype x = psymval->value(object, addend) - address;
4523 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4527 *calculated_value = x >> 2;
4528 return This::STATUS_OKAY;
4531 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4533 return check_overflow<18>(x);
4536 // R_MICROMIPS_PC7_S1
4537 static inline typename This::Status
4538 relmicromips_pc7_s1(unsigned char* view,
4539 const Mips_relobj<size, big_endian>* object,
4540 const Symbol_value<size>* psymval, Mips_address address,
4541 Mips_address addend_a, bool extract_addend,
4542 bool calculate_only, Valtype* calculated_value)
4544 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4545 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4547 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4550 Valtype x = psymval->value(object, addend) - address;
4551 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4555 *calculated_value = x >> 1;
4556 return This::STATUS_OKAY;
4559 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4561 return check_overflow<8>(x);
4564 // R_MICROMIPS_PC10_S1
4565 static inline typename This::Status
4566 relmicromips_pc10_s1(unsigned char* view,
4567 const Mips_relobj<size, big_endian>* object,
4568 const Symbol_value<size>* psymval, Mips_address address,
4569 Mips_address addend_a, bool extract_addend,
4570 bool calculate_only, Valtype* calculated_value)
4572 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4573 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4575 Valtype addend = (extract_addend
4576 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4579 Valtype x = psymval->value(object, addend) - address;
4580 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
4584 *calculated_value = x >> 1;
4585 return This::STATUS_OKAY;
4588 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4590 return check_overflow<11>(x);
4593 // R_MICROMIPS_PC16_S1
4594 static inline typename This::Status
4595 relmicromips_pc16_s1(unsigned char* view,
4596 const Mips_relobj<size, big_endian>* object,
4597 const Symbol_value<size>* psymval, Mips_address address,
4598 Mips_address addend_a, bool extract_addend,
4599 bool calculate_only, Valtype* calculated_value)
4601 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4602 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4604 Valtype addend = (extract_addend
4605 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
4608 Valtype x = psymval->value(object, addend) - address;
4609 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
4613 *calculated_value = x >> 1;
4614 return This::STATUS_OKAY;
4617 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4619 return check_overflow<17>(x);
4622 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4623 static inline typename This::Status
4624 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4625 const Symbol_value<size>* psymval, Mips_address addend,
4626 Mips_address address, bool gp_disp, unsigned int r_type,
4627 unsigned int r_sym, bool extract_addend)
4629 // Record the relocation. It will be resolved when we find lo16 part.
4630 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4631 addend, r_type, r_sym, extract_addend, address,
4633 return This::STATUS_OKAY;
4636 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4637 static inline typename This::Status
4638 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4639 const Symbol_value<size>* psymval, Mips_address addend_hi,
4640 Mips_address address, bool is_gp_disp, unsigned int r_type,
4641 bool extract_addend, Valtype32 addend_lo,
4642 Target_mips<size, big_endian>* target, bool calculate_only,
4643 Valtype* calculated_value)
4645 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4646 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4648 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4653 value = psymval->value(object, addend);
4656 // For MIPS16 ABI code we generate this sequence
4657 // 0: li $v0,%hi(_gp_disp)
4658 // 4: addiupc $v1,%lo(_gp_disp)
4662 // So the offsets of hi and lo relocs are the same, but the
4663 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
4664 // ADDIUPC clears the low two bits of the instruction address,
4665 // so the base is ($t9 + 4) & ~3.
4667 if (r_type == elfcpp::R_MIPS16_HI16)
4668 gp_disp = (target->adjusted_gp_value(object)
4669 - ((address + 4) & ~0x3));
4670 // The microMIPS .cpload sequence uses the same assembly
4671 // instructions as the traditional psABI version, but the
4672 // incoming $t9 has the low bit set.
4673 else if (r_type == elfcpp::R_MICROMIPS_HI16)
4674 gp_disp = target->adjusted_gp_value(object) - address - 1;
4676 gp_disp = target->adjusted_gp_value(object) - address;
4677 value = gp_disp + addend;
4679 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4680 val = Bits<32>::bit_select32(val, x, 0xffff);
4684 *calculated_value = x;
4685 return This::STATUS_OKAY;
4688 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4690 return (is_gp_disp ? check_overflow<16>(x)
4691 : This::STATUS_OKAY);
4694 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4695 static inline typename This::Status
4696 relgot16_local(unsigned char* view,
4697 const Mips_relobj<size, big_endian>* object,
4698 const Symbol_value<size>* psymval, Mips_address addend_a,
4699 bool extract_addend, unsigned int r_type, unsigned int r_sym)
4701 // Record the relocation. It will be resolved when we find lo16 part.
4702 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4703 addend_a, r_type, r_sym, extract_addend));
4704 return This::STATUS_OKAY;
4707 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4708 static inline typename This::Status
4709 do_relgot16_local(unsigned char* view,
4710 const Mips_relobj<size, big_endian>* object,
4711 const Symbol_value<size>* psymval, Mips_address addend_hi,
4712 bool extract_addend, Valtype32 addend_lo,
4713 Target_mips<size, big_endian>* target, bool calculate_only,
4714 Valtype* calculated_value)
4716 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4717 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4719 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4722 // Find GOT page entry.
4723 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
4726 unsigned int got_offset =
4727 target->got_section()->get_got_page_offset(value, object);
4729 // Resolve the relocation.
4730 Valtype x = target->got_section()->gp_offset(got_offset, object);
4731 val = Bits<32>::bit_select32(val, x, 0xffff);
4735 *calculated_value = x;
4736 return This::STATUS_OKAY;
4739 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4741 return check_overflow<16>(x);
4744 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
4745 static inline typename This::Status
4746 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
4747 const Mips_relobj<size, big_endian>* object,
4748 const Symbol_value<size>* psymval, Mips_address addend_a,
4749 bool extract_addend, Mips_address address, bool is_gp_disp,
4750 unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
4751 bool calculate_only, Valtype* calculated_value)
4753 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4754 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4756 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4759 if (rel_type == elfcpp::SHT_REL)
4761 typename This::Status reloc_status = This::STATUS_OKAY;
4762 // Resolve pending R_MIPS_HI16 relocations.
4763 typename std::list<reloc_high<size, big_endian> >::iterator it =
4764 hi16_relocs.begin();
4765 while (it != hi16_relocs.end())
4767 reloc_high<size, big_endian> hi16 = *it;
4768 if (hi16.r_sym == r_sym
4769 && is_matching_lo16_reloc(hi16.r_type, r_type))
4771 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
4772 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
4773 hi16.addend, hi16.address, hi16.gp_disp,
4774 hi16.r_type, hi16.extract_addend, addend,
4775 target, calculate_only, calculated_value);
4776 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
4777 if (reloc_status == This::STATUS_OVERFLOW)
4778 return This::STATUS_OVERFLOW;
4779 it = hi16_relocs.erase(it);
4785 // Resolve pending local R_MIPS_GOT16 relocations.
4786 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
4787 got16_relocs.begin();
4788 while (it2 != got16_relocs.end())
4790 reloc_high<size, big_endian> got16 = *it2;
4791 if (got16.r_sym == r_sym
4792 && is_matching_lo16_reloc(got16.r_type, r_type))
4794 mips_reloc_unshuffle(got16.view, got16.r_type, false);
4796 reloc_status = do_relgot16_local(got16.view, got16.object,
4797 got16.psymval, got16.addend,
4798 got16.extract_addend, addend, target,
4799 calculate_only, calculated_value);
4801 mips_reloc_shuffle(got16.view, got16.r_type, false);
4802 if (reloc_status == This::STATUS_OVERFLOW)
4803 return This::STATUS_OVERFLOW;
4804 it2 = got16_relocs.erase(it2);
4811 // Resolve R_MIPS_LO16 relocation.
4814 x = psymval->value(object, addend);
4817 // See the comment for R_MIPS16_HI16 above for the reason
4818 // for this conditional.
4820 if (r_type == elfcpp::R_MIPS16_LO16)
4821 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
4822 else if (r_type == elfcpp::R_MICROMIPS_LO16
4823 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
4824 gp_disp = target->adjusted_gp_value(object) - address + 3;
4826 gp_disp = target->adjusted_gp_value(object) - address + 4;
4827 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
4828 // for overflow. Relocations against _gp_disp are normally
4829 // generated from the .cpload pseudo-op. It generates code
4830 // that normally looks like this:
4832 // lui $gp,%hi(_gp_disp)
4833 // addiu $gp,$gp,%lo(_gp_disp)
4836 // Here $t9 holds the address of the function being called,
4837 // as required by the MIPS ELF ABI. The R_MIPS_LO16
4838 // relocation can easily overflow in this situation, but the
4839 // R_MIPS_HI16 relocation will handle the overflow.
4840 // Therefore, we consider this a bug in the MIPS ABI, and do
4841 // not check for overflow here.
4842 x = gp_disp + addend;
4844 val = Bits<32>::bit_select32(val, x, 0xffff);
4847 *calculated_value = x;
4849 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4851 return This::STATUS_OKAY;
4854 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
4855 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4856 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
4857 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
4858 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
4859 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
4860 static inline typename This::Status
4861 relgot(unsigned char* view, int gp_offset, bool calculate_only,
4862 Valtype* calculated_value)
4864 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4865 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4866 Valtype x = gp_offset;
4867 val = Bits<32>::bit_select32(val, x, 0xffff);
4871 *calculated_value = x;
4872 return This::STATUS_OKAY;
4875 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4877 return check_overflow<16>(x);
4881 static inline typename This::Status
4882 releh(unsigned char* view, int gp_offset, bool calculate_only,
4883 Valtype* calculated_value)
4885 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4886 Valtype x = gp_offset;
4890 *calculated_value = x;
4891 return This::STATUS_OKAY;
4894 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4896 return check_overflow<32>(x);
4899 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
4900 static inline typename This::Status
4901 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
4902 const Mips_relobj<size, big_endian>* object,
4903 const Symbol_value<size>* psymval, Mips_address addend_a,
4904 bool extract_addend, bool calculate_only,
4905 Valtype* calculated_value)
4907 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4908 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4909 Valtype addend = extract_addend ? val & 0xffff : addend_a;
4911 // Find a GOT page entry that points to within 32KB of symbol + addend.
4912 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
4913 unsigned int got_offset =
4914 target->got_section()->get_got_page_offset(value, object);
4916 Valtype x = target->got_section()->gp_offset(got_offset, object);
4917 val = Bits<32>::bit_select32(val, x, 0xffff);
4921 *calculated_value = x;
4922 return This::STATUS_OKAY;
4925 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4927 return check_overflow<16>(x);
4930 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
4931 static inline typename This::Status
4932 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
4933 const Mips_relobj<size, big_endian>* object,
4934 const Symbol_value<size>* psymval, Mips_address addend_a,
4935 bool extract_addend, bool local, bool calculate_only,
4936 Valtype* calculated_value)
4938 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4939 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4940 Valtype addend = extract_addend ? val & 0xffff : addend_a;
4942 // For a local symbol, find a GOT page entry that points to within 32KB of
4943 // symbol + addend. Relocation value is the offset of the GOT page entry's
4944 // value from symbol + addend.
4945 // For a global symbol, relocation value is addend.
4949 // Find GOT page entry.
4950 Mips_address value = ((psymval->value(object, addend) + 0x8000)
4952 target->got_section()->get_got_page_offset(value, object);
4954 x = psymval->value(object, addend) - value;
4958 val = Bits<32>::bit_select32(val, x, 0xffff);
4962 *calculated_value = x;
4963 return This::STATUS_OKAY;
4966 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4968 return check_overflow<16>(x);
4971 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
4972 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
4973 static inline typename This::Status
4974 relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
4975 Valtype* calculated_value)
4977 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4978 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4979 Valtype x = gp_offset;
4980 x = ((x + 0x8000) >> 16) & 0xffff;
4981 val = Bits<32>::bit_select32(val, x, 0xffff);
4984 *calculated_value = x;
4986 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4988 return This::STATUS_OKAY;
4991 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
4992 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
4993 static inline typename This::Status
4994 relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
4995 Valtype* calculated_value)
4997 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4998 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4999 Valtype x = gp_offset;
5000 val = Bits<32>::bit_select32(val, x, 0xffff);
5003 *calculated_value = x;
5005 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5007 return This::STATUS_OKAY;
5010 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5011 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5012 static inline typename This::Status
5013 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5014 const Symbol_value<size>* psymval, Mips_address gp,
5015 Mips_address addend_a, bool extract_addend, bool local,
5016 unsigned int r_type, bool calculate_only,
5017 Valtype* calculated_value)
5019 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5020 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5025 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5026 addend = (val & 0x7f) << 2;
5028 addend = val & 0xffff;
5029 // Only sign-extend the addend if it was extracted from the
5030 // instruction. If the addend was separate, leave it alone,
5031 // otherwise we may lose significant bits.
5032 addend = Bits<16>::sign_extend32(addend);
5037 Valtype x = psymval->value(object, addend) - gp;
5039 // If the symbol was local, any earlier relocatable links will
5040 // have adjusted its addend with the gp offset, so compensate
5041 // for that now. Don't do it for symbols forced local in this
5042 // link, though, since they won't have had the gp offset applied
5045 x += object->gp_value();
5047 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5048 val = Bits<32>::bit_select32(val, x, 0x7f);
5050 val = Bits<32>::bit_select32(val, x, 0xffff);
5054 *calculated_value = x;
5055 return This::STATUS_OKAY;
5058 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5060 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5062 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5063 "limit (see option -G)"));
5064 return This::STATUS_OVERFLOW;
5066 return This::STATUS_OKAY;
5070 static inline typename This::Status
5071 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5072 const Symbol_value<size>* psymval, Mips_address gp,
5073 Mips_address addend_a, bool extract_addend, bool calculate_only,
5074 Valtype* calculated_value)
5076 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5077 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5078 Valtype addend = extract_addend ? val : addend_a;
5080 // R_MIPS_GPREL32 relocations are defined for local symbols only.
5081 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5084 *calculated_value = x;
5086 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5088 return This::STATUS_OKAY;
5091 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5092 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5093 // R_MICROMIPS_TLS_DTPREL_HI16
5094 static inline typename This::Status
5095 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5096 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5097 Mips_address addend_a, bool extract_addend, bool calculate_only,
5098 Valtype* calculated_value)
5100 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5101 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5102 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5104 // tls symbol values are relative to tls_segment()->vaddr()
5105 Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5106 val = Bits<32>::bit_select32(val, x, 0xffff);
5109 *calculated_value = x;
5111 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5113 return This::STATUS_OKAY;
5116 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5117 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5118 // R_MICROMIPS_TLS_DTPREL_LO16,
5119 static inline typename This::Status
5120 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5121 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5122 Mips_address addend_a, bool extract_addend, bool calculate_only,
5123 Valtype* calculated_value)
5125 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5126 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5127 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5129 // tls symbol values are relative to tls_segment()->vaddr()
5130 Valtype x = psymval->value(object, addend) - tp_offset;
5131 val = Bits<32>::bit_select32(val, x, 0xffff);
5134 *calculated_value = x;
5136 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5138 return This::STATUS_OKAY;
5141 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5142 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5143 static inline typename This::Status
5144 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5145 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5146 Mips_address addend_a, bool extract_addend, bool calculate_only,
5147 Valtype* calculated_value)
5149 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5150 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5151 Valtype addend = extract_addend ? val : addend_a;
5153 // tls symbol values are relative to tls_segment()->vaddr()
5154 Valtype x = psymval->value(object, addend) - tp_offset;
5157 *calculated_value = x;
5159 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5161 return This::STATUS_OKAY;
5164 // R_MIPS_SUB, R_MICROMIPS_SUB
5165 static inline typename This::Status
5166 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5167 const Symbol_value<size>* psymval, Mips_address addend_a,
5168 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5170 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5171 Valtype64 addend = (extract_addend
5172 ? elfcpp::Swap<64, big_endian>::readval(wv)
5175 Valtype64 x = psymval->value(object, -addend);
5177 *calculated_value = x;
5179 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5181 return This::STATUS_OKAY;
5185 static inline typename This::Status
5186 rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5187 const Symbol_value<size>* psymval, Mips_address addend_a,
5188 bool extract_addend, bool calculate_only, Valtype* calculated_value,
5189 bool apply_addend_only)
5191 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5192 Valtype64 addend = (extract_addend
5193 ? elfcpp::Swap<64, big_endian>::readval(wv)
5196 Valtype64 x = psymval->value(object, addend);
5198 *calculated_value = x;
5201 if (apply_addend_only)
5203 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5206 return This::STATUS_OKAY;
5211 template<int size, bool big_endian>
5212 typename std::list<reloc_high<size, big_endian> >
5213 Mips_relocate_functions<size, big_endian>::hi16_relocs;
5215 template<int size, bool big_endian>
5216 typename std::list<reloc_high<size, big_endian> >
5217 Mips_relocate_functions<size, big_endian>::got16_relocs;
5219 // Mips_got_info methods.
5221 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5222 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5224 template<int size, bool big_endian>
5226 Mips_got_info<size, big_endian>::record_local_got_symbol(
5227 Mips_relobj<size, big_endian>* object, unsigned int symndx,
5228 Mips_address addend, unsigned int r_type, unsigned int shndx,
5229 bool is_section_symbol)
5231 Mips_got_entry<size, big_endian>* entry =
5232 new Mips_got_entry<size, big_endian>(object, symndx, addend,
5233 mips_elf_reloc_tls_type(r_type),
5234 shndx, is_section_symbol);
5235 this->record_got_entry(entry, object);
5238 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5239 // in OBJECT. FOR_CALL is true if the caller is only interested in
5240 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5243 template<int size, bool big_endian>
5245 Mips_got_info<size, big_endian>::record_global_got_symbol(
5246 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5247 unsigned int r_type, bool dyn_reloc, bool for_call)
5250 mips_sym->set_got_not_only_for_calls();
5252 // A global symbol in the GOT must also be in the dynamic symbol table.
5253 if (!mips_sym->needs_dynsym_entry())
5255 switch (mips_sym->visibility())
5257 case elfcpp::STV_INTERNAL:
5258 case elfcpp::STV_HIDDEN:
5259 mips_sym->set_is_forced_local();
5262 mips_sym->set_needs_dynsym_entry();
5267 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5268 if (tls_type == GOT_TLS_NONE)
5269 this->global_got_symbols_.insert(mips_sym);
5273 if (mips_sym->global_got_area() == GGA_NONE)
5274 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5278 Mips_got_entry<size, big_endian>* entry =
5279 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5281 this->record_got_entry(entry, object);
5284 // Add ENTRY to master GOT and to OBJECT's GOT.
5286 template<int size, bool big_endian>
5288 Mips_got_info<size, big_endian>::record_got_entry(
5289 Mips_got_entry<size, big_endian>* entry,
5290 Mips_relobj<size, big_endian>* object)
5292 this->got_entries_.insert(entry);
5294 // Create the GOT entry for the OBJECT's GOT.
5295 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5296 Mips_got_entry<size, big_endian>* entry2 =
5297 new Mips_got_entry<size, big_endian>(*entry);
5299 g->got_entries_.insert(entry2);
5302 // Record that OBJECT has a page relocation against symbol SYMNDX and
5303 // that ADDEND is the addend for that relocation.
5304 // This function creates an upper bound on the number of GOT slots
5305 // required; no attempt is made to combine references to non-overridable
5306 // global symbols across multiple input files.
5308 template<int size, bool big_endian>
5310 Mips_got_info<size, big_endian>::record_got_page_entry(
5311 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5313 struct Got_page_range **range_ptr, *range;
5314 int old_pages, new_pages;
5316 // Find the Got_page_entry for this symbol.
5317 Got_page_entry* entry = new Got_page_entry(object, symndx);
5318 typename Got_page_entry_set::iterator it =
5319 this->got_page_entries_.find(entry);
5320 if (it != this->got_page_entries_.end())
5323 this->got_page_entries_.insert(entry);
5325 // Add the same entry to the OBJECT's GOT.
5326 Got_page_entry* entry2 = NULL;
5327 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5328 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5330 entry2 = new Got_page_entry(*entry);
5331 g2->got_page_entries_.insert(entry2);
5334 // Skip over ranges whose maximum extent cannot share a page entry
5336 range_ptr = &entry->ranges;
5337 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5338 range_ptr = &(*range_ptr)->next;
5340 // If we scanned to the end of the list, or found a range whose
5341 // minimum extent cannot share a page entry with ADDEND, create
5342 // a new singleton range.
5344 if (!range || addend < range->min_addend - 0xffff)
5346 range = new Got_page_range();
5347 range->next = *range_ptr;
5348 range->min_addend = addend;
5349 range->max_addend = addend;
5354 ++entry2->num_pages;
5355 ++this->page_gotno_;
5360 // Remember how many pages the old range contributed.
5361 old_pages = range->get_max_pages();
5363 // Update the ranges.
5364 if (addend < range->min_addend)
5365 range->min_addend = addend;
5366 else if (addend > range->max_addend)
5368 if (range->next && addend >= range->next->min_addend - 0xffff)
5370 old_pages += range->next->get_max_pages();
5371 range->max_addend = range->next->max_addend;
5372 range->next = range->next->next;
5375 range->max_addend = addend;
5378 // Record any change in the total estimate.
5379 new_pages = range->get_max_pages();
5380 if (old_pages != new_pages)
5382 entry->num_pages += new_pages - old_pages;
5384 entry2->num_pages += new_pages - old_pages;
5385 this->page_gotno_ += new_pages - old_pages;
5386 g2->page_gotno_ += new_pages - old_pages;
5390 // Create all entries that should be in the local part of the GOT.
5392 template<int size, bool big_endian>
5394 Mips_got_info<size, big_endian>::add_local_entries(
5395 Target_mips<size, big_endian>* target, Layout* layout)
5397 Mips_output_data_got<size, big_endian>* got = target->got_section();
5398 // First two GOT entries are reserved. The first entry will be filled at
5399 // runtime. The second entry will be used by some runtime loaders.
5400 got->add_constant(0);
5401 got->add_constant(target->mips_elf_gnu_got1_mask());
5403 for (typename Got_entry_set::iterator
5404 p = this->got_entries_.begin();
5405 p != this->got_entries_.end();
5408 Mips_got_entry<size, big_endian>* entry = *p;
5409 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5411 got->add_local(entry->object(), entry->symndx(),
5412 GOT_TYPE_STANDARD, entry->addend());
5413 unsigned int got_offset = entry->object()->local_got_offset(
5414 entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5415 if (got->multi_got() && this->index_ > 0
5416 && parameters->options().output_is_position_independent())
5418 if (!entry->is_section_symbol())
5419 target->rel_dyn_section(layout)->add_local(entry->object(),
5420 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5422 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5423 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5429 this->add_page_entries(target, layout);
5431 // Add global entries that should be in the local area.
5432 for (typename Got_entry_set::iterator
5433 p = this->got_entries_.begin();
5434 p != this->got_entries_.end();
5437 Mips_got_entry<size, big_endian>* entry = *p;
5438 if (!entry->is_for_global_symbol())
5441 Mips_symbol<size>* mips_sym = entry->sym();
5442 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5444 unsigned int got_type;
5445 if (!got->multi_got())
5446 got_type = GOT_TYPE_STANDARD;
5448 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5449 if (got->add_global(mips_sym, got_type))
5451 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5452 if (got->multi_got() && this->index_ > 0
5453 && parameters->options().output_is_position_independent())
5454 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5455 mips_sym, elfcpp::R_MIPS_REL32, got,
5456 mips_sym->got_offset(got_type));
5462 // Create GOT page entries.
5464 template<int size, bool big_endian>
5466 Mips_got_info<size, big_endian>::add_page_entries(
5467 Target_mips<size, big_endian>* target, Layout* layout)
5469 if (this->page_gotno_ == 0)
5472 Mips_output_data_got<size, big_endian>* got = target->got_section();
5473 this->got_page_offset_start_ = got->add_constant(0);
5474 if (got->multi_got() && this->index_ > 0
5475 && parameters->options().output_is_position_independent())
5476 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5477 this->got_page_offset_start_);
5478 int num_entries = this->page_gotno_;
5479 unsigned int prev_offset = this->got_page_offset_start_;
5480 while (--num_entries > 0)
5482 unsigned int next_offset = got->add_constant(0);
5483 if (got->multi_got() && this->index_ > 0
5484 && parameters->options().output_is_position_independent())
5485 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5487 gold_assert(next_offset == prev_offset + size/8);
5488 prev_offset = next_offset;
5490 this->got_page_offset_next_ = this->got_page_offset_start_;
5493 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5495 template<int size, bool big_endian>
5497 Mips_got_info<size, big_endian>::add_global_entries(
5498 Target_mips<size, big_endian>* target, Layout* layout,
5499 unsigned int non_reloc_only_global_gotno)
5501 Mips_output_data_got<size, big_endian>* got = target->got_section();
5502 // Add GGA_NORMAL entries.
5503 unsigned int count = 0;
5504 for (typename Got_entry_set::iterator
5505 p = this->got_entries_.begin();
5506 p != this->got_entries_.end();
5509 Mips_got_entry<size, big_endian>* entry = *p;
5510 if (!entry->is_for_global_symbol())
5513 Mips_symbol<size>* mips_sym = entry->sym();
5514 if (mips_sym->global_got_area() != GGA_NORMAL)
5517 unsigned int got_type;
5518 if (!got->multi_got())
5519 got_type = GOT_TYPE_STANDARD;
5521 // In multi-GOT links, global symbol can be in both primary and
5522 // secondary GOT(s). By creating custom GOT type
5523 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5524 // is added to secondary GOT(s).
5525 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5526 if (!got->add_global(mips_sym, got_type))
5529 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5530 if (got->multi_got() && this->index_ == 0)
5532 if (got->multi_got() && this->index_ > 0)
5534 if (parameters->options().output_is_position_independent()
5535 || (!parameters->doing_static_link()
5536 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
5538 target->rel_dyn_section(layout)->add_global(
5539 mips_sym, elfcpp::R_MIPS_REL32, got,
5540 mips_sym->got_offset(got_type));
5541 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
5542 elfcpp::R_MIPS_REL32, mips_sym);
5547 if (!got->multi_got() || this->index_ == 0)
5549 if (got->multi_got())
5551 // We need to allocate space in the primary GOT for GGA_NORMAL entries
5552 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
5553 // entries correspond to dynamic symbol indexes.
5554 while (count < non_reloc_only_global_gotno)
5556 got->add_constant(0);
5561 // Add GGA_RELOC_ONLY entries.
5562 got->add_reloc_only_entries();
5566 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
5568 template<int size, bool big_endian>
5570 Mips_got_info<size, big_endian>::add_reloc_only_entries(
5571 Mips_output_data_got<size, big_endian>* got)
5573 for (typename Global_got_entry_set::iterator
5574 p = this->global_got_symbols_.begin();
5575 p != this->global_got_symbols_.end();
5578 Mips_symbol<size>* mips_sym = *p;
5579 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
5581 unsigned int got_type;
5582 if (!got->multi_got())
5583 got_type = GOT_TYPE_STANDARD;
5585 got_type = GOT_TYPE_STANDARD_MULTIGOT;
5586 if (got->add_global(mips_sym, got_type))
5587 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5592 // Create TLS GOT entries.
5594 template<int size, bool big_endian>
5596 Mips_got_info<size, big_endian>::add_tls_entries(
5597 Target_mips<size, big_endian>* target, Layout* layout)
5599 Mips_output_data_got<size, big_endian>* got = target->got_section();
5600 // Add local tls entries.
5601 for (typename Got_entry_set::iterator
5602 p = this->got_entries_.begin();
5603 p != this->got_entries_.end();
5606 Mips_got_entry<size, big_endian>* entry = *p;
5607 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
5610 if (entry->tls_type() == GOT_TLS_GD)
5612 unsigned int got_type = GOT_TYPE_TLS_PAIR;
5613 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5614 : elfcpp::R_MIPS_TLS_DTPMOD64);
5615 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5616 : elfcpp::R_MIPS_TLS_DTPREL64);
5618 if (!parameters->doing_static_link())
5620 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
5621 entry->shndx(), got_type,
5622 target->rel_dyn_section(layout),
5623 r_type1, entry->addend());
5624 unsigned int got_offset =
5625 entry->object()->local_got_offset(entry->symndx(), got_type,
5627 got->add_static_reloc(got_offset + size/8, r_type2,
5628 entry->object(), entry->symndx());
5632 // We are doing a static link. Mark it as belong to module 1,
5634 unsigned int got_offset = got->add_constant(1);
5635 entry->object()->set_local_got_offset(entry->symndx(), got_type,
5638 got->add_constant(0);
5639 got->add_static_reloc(got_offset + size/8, r_type2,
5640 entry->object(), entry->symndx());
5643 else if (entry->tls_type() == GOT_TLS_IE)
5645 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
5646 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5647 : elfcpp::R_MIPS_TLS_TPREL64);
5648 if (!parameters->doing_static_link())
5649 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
5650 target->rel_dyn_section(layout), r_type,
5654 got->add_local(entry->object(), entry->symndx(), got_type,
5656 unsigned int got_offset =
5657 entry->object()->local_got_offset(entry->symndx(), got_type,
5659 got->add_static_reloc(got_offset, r_type, entry->object(),
5663 else if (entry->tls_type() == GOT_TLS_LDM)
5665 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5666 : elfcpp::R_MIPS_TLS_DTPMOD64);
5667 unsigned int got_offset;
5668 if (!parameters->doing_static_link())
5670 got_offset = got->add_constant(0);
5671 target->rel_dyn_section(layout)->add_local(
5672 entry->object(), 0, r_type, got, got_offset);
5675 // We are doing a static link. Just mark it as belong to module 1,
5677 got_offset = got->add_constant(1);
5679 got->add_constant(0);
5680 got->set_tls_ldm_offset(got_offset, entry->object());
5686 // Add global tls entries.
5687 for (typename Got_entry_set::iterator
5688 p = this->got_entries_.begin();
5689 p != this->got_entries_.end();
5692 Mips_got_entry<size, big_endian>* entry = *p;
5693 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
5696 Mips_symbol<size>* mips_sym = entry->sym();
5697 if (entry->tls_type() == GOT_TLS_GD)
5699 unsigned int got_type;
5700 if (!got->multi_got())
5701 got_type = GOT_TYPE_TLS_PAIR;
5703 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
5704 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5705 : elfcpp::R_MIPS_TLS_DTPMOD64);
5706 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5707 : elfcpp::R_MIPS_TLS_DTPREL64);
5708 if (!parameters->doing_static_link())
5709 got->add_global_pair_with_rel(mips_sym, got_type,
5710 target->rel_dyn_section(layout), r_type1, r_type2);
5713 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
5714 // GOT entries. The first one is initialized to be 1, which is the
5715 // module index for the main executable and the second one 0. A
5716 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
5717 // the second GOT entry and will be applied by gold.
5718 unsigned int got_offset = got->add_constant(1);
5719 mips_sym->set_got_offset(got_type, got_offset);
5720 got->add_constant(0);
5721 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
5724 else if (entry->tls_type() == GOT_TLS_IE)
5726 unsigned int got_type;
5727 if (!got->multi_got())
5728 got_type = GOT_TYPE_TLS_OFFSET;
5730 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
5731 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5732 : elfcpp::R_MIPS_TLS_TPREL64);
5733 if (!parameters->doing_static_link())
5734 got->add_global_with_rel(mips_sym, got_type,
5735 target->rel_dyn_section(layout), r_type);
5738 got->add_global(mips_sym, got_type);
5739 unsigned int got_offset = mips_sym->got_offset(got_type);
5740 got->add_static_reloc(got_offset, r_type, mips_sym);
5748 // Decide whether the symbol needs an entry in the global part of the primary
5749 // GOT, setting global_got_area accordingly. Count the number of global
5750 // symbols that are in the primary GOT only because they have dynamic
5751 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
5753 template<int size, bool big_endian>
5755 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
5757 for (typename Global_got_entry_set::iterator
5758 p = this->global_got_symbols_.begin();
5759 p != this->global_got_symbols_.end();
5762 Mips_symbol<size>* sym = *p;
5763 // Make a final decision about whether the symbol belongs in the
5764 // local or global GOT. Symbols that bind locally can (and in the
5765 // case of forced-local symbols, must) live in the local GOT.
5766 // Those that are aren't in the dynamic symbol table must also
5767 // live in the local GOT.
5769 if (!sym->should_add_dynsym_entry(symtab)
5770 || (sym->got_only_for_calls()
5771 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
5772 : symbol_references_local(sym,
5773 sym->should_add_dynsym_entry(symtab))))
5774 // The symbol belongs in the local GOT. We no longer need this
5775 // entry if it was only used for relocations; those relocations
5776 // will be against the null or section symbol instead.
5777 sym->set_global_got_area(GGA_NONE);
5778 else if (sym->global_got_area() == GGA_RELOC_ONLY)
5780 ++this->reloc_only_gotno_;
5781 ++this->global_gotno_ ;
5786 // Return the offset of GOT page entry for VALUE. Initialize the entry with
5787 // VALUE if it is not initialized.
5789 template<int size, bool big_endian>
5791 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
5792 Mips_output_data_got<size, big_endian>* got)
5794 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
5795 if (it != this->got_page_offsets_.end())
5798 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
5799 + (size/8) * this->page_gotno_);
5801 unsigned int got_offset = this->got_page_offset_next_;
5802 this->got_page_offsets_[value] = got_offset;
5803 this->got_page_offset_next_ += size/8;
5804 got->update_got_entry(got_offset, value);
5808 // Remove lazy-binding stubs for global symbols in this GOT.
5810 template<int size, bool big_endian>
5812 Mips_got_info<size, big_endian>::remove_lazy_stubs(
5813 Target_mips<size, big_endian>* target)
5815 for (typename Got_entry_set::iterator
5816 p = this->got_entries_.begin();
5817 p != this->got_entries_.end();
5820 Mips_got_entry<size, big_endian>* entry = *p;
5821 if (entry->is_for_global_symbol())
5822 target->remove_lazy_stub_entry(entry->sym());
5826 // Count the number of GOT entries required.
5828 template<int size, bool big_endian>
5830 Mips_got_info<size, big_endian>::count_got_entries()
5832 for (typename Got_entry_set::iterator
5833 p = this->got_entries_.begin();
5834 p != this->got_entries_.end();
5837 this->count_got_entry(*p);
5841 // Count the number of GOT entries required by ENTRY. Accumulate the result.
5843 template<int size, bool big_endian>
5845 Mips_got_info<size, big_endian>::count_got_entry(
5846 Mips_got_entry<size, big_endian>* entry)
5848 if (entry->is_tls_entry())
5849 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
5850 else if (entry->is_for_local_symbol()
5851 || entry->sym()->global_got_area() == GGA_NONE)
5852 ++this->local_gotno_;
5854 ++this->global_gotno_;
5857 // Add FROM's GOT entries.
5859 template<int size, bool big_endian>
5861 Mips_got_info<size, big_endian>::add_got_entries(
5862 Mips_got_info<size, big_endian>* from)
5864 for (typename Got_entry_set::iterator
5865 p = from->got_entries_.begin();
5866 p != from->got_entries_.end();
5869 Mips_got_entry<size, big_endian>* entry = *p;
5870 if (this->got_entries_.find(entry) == this->got_entries_.end())
5872 Mips_got_entry<size, big_endian>* entry2 =
5873 new Mips_got_entry<size, big_endian>(*entry);
5874 this->got_entries_.insert(entry2);
5875 this->count_got_entry(entry);
5880 // Add FROM's GOT page entries.
5882 template<int size, bool big_endian>
5884 Mips_got_info<size, big_endian>::add_got_page_entries(
5885 Mips_got_info<size, big_endian>* from)
5887 for (typename Got_page_entry_set::iterator
5888 p = from->got_page_entries_.begin();
5889 p != from->got_page_entries_.end();
5892 Got_page_entry* entry = *p;
5893 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
5895 Got_page_entry* entry2 = new Got_page_entry(*entry);
5896 this->got_page_entries_.insert(entry2);
5897 this->page_gotno_ += entry->num_pages;
5902 // Mips_output_data_got methods.
5904 // Lay out the GOT. Add local, global and TLS entries. If GOT is
5905 // larger than 64K, create multi-GOT.
5907 template<int size, bool big_endian>
5909 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
5910 Symbol_table* symtab, const Input_objects* input_objects)
5912 // Decide which symbols need to go in the global part of the GOT and
5913 // count the number of reloc-only GOT symbols.
5914 this->master_got_info_->count_got_symbols(symtab);
5916 // Count the number of GOT entries.
5917 this->master_got_info_->count_got_entries();
5919 unsigned int got_size = this->master_got_info_->got_size();
5920 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
5921 this->lay_out_multi_got(layout, input_objects);
5924 // Record that all objects use single GOT.
5925 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5926 p != input_objects->relobj_end();
5929 Mips_relobj<size, big_endian>* object =
5930 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
5931 if (object->get_got_info() != NULL)
5932 object->set_got_info(this->master_got_info_);
5935 this->master_got_info_->add_local_entries(this->target_, layout);
5936 this->master_got_info_->add_global_entries(this->target_, layout,
5938 this->master_got_info_->add_tls_entries(this->target_, layout);
5942 // Create multi-GOT. For every GOT, add local, global and TLS entries.
5944 template<int size, bool big_endian>
5946 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
5947 const Input_objects* input_objects)
5949 // Try to merge the GOTs of input objects together, as long as they
5950 // don't seem to exceed the maximum GOT size, choosing one of them
5951 // to be the primary GOT.
5952 this->merge_gots(input_objects);
5954 // Every symbol that is referenced in a dynamic relocation must be
5955 // present in the primary GOT.
5956 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
5960 unsigned int offset = 0;
5961 Mips_got_info<size, big_endian>* g = this->primary_got_;
5965 g->set_offset(offset);
5967 g->add_local_entries(this->target_, layout);
5969 g->add_global_entries(this->target_, layout,
5970 (this->master_got_info_->global_gotno()
5971 - this->master_got_info_->reloc_only_gotno()));
5973 g->add_global_entries(this->target_, layout, /*not used*/-1U);
5974 g->add_tls_entries(this->target_, layout);
5976 // Forbid global symbols in every non-primary GOT from having
5977 // lazy-binding stubs.
5979 g->remove_lazy_stubs(this->target_);
5982 offset += g->got_size();
5988 // Attempt to merge GOTs of different input objects. Try to use as much as
5989 // possible of the primary GOT, since it doesn't require explicit dynamic
5990 // relocations, but don't use objects that would reference global symbols
5991 // out of the addressable range. Failing the primary GOT, attempt to merge
5992 // with the current GOT, or finish the current GOT and then make make the new
5995 template<int size, bool big_endian>
5997 Mips_output_data_got<size, big_endian>::merge_gots(
5998 const Input_objects* input_objects)
6000 gold_assert(this->primary_got_ == NULL);
6001 Mips_got_info<size, big_endian>* current = NULL;
6003 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6004 p != input_objects->relobj_end();
6007 Mips_relobj<size, big_endian>* object =
6008 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6010 Mips_got_info<size, big_endian>* g = object->get_got_info();
6014 g->count_got_entries();
6016 // Work out the number of page, local and TLS entries.
6017 unsigned int estimate = this->master_got_info_->page_gotno();
6018 if (estimate > g->page_gotno())
6019 estimate = g->page_gotno();
6020 estimate += g->local_gotno() + g->tls_gotno();
6022 // We place TLS GOT entries after both locals and globals. The globals
6023 // for the primary GOT may overflow the normal GOT size limit, so be
6024 // sure not to merge a GOT which requires TLS with the primary GOT in that
6025 // case. This doesn't affect non-primary GOTs.
6026 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6027 : g->global_gotno());
6029 unsigned int max_count =
6030 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6031 if (estimate <= max_count)
6033 // If we don't have a primary GOT, use it as
6034 // a starting point for the primary GOT.
6035 if (!this->primary_got_)
6037 this->primary_got_ = g;
6041 // Try merging with the primary GOT.
6042 if (this->merge_got_with(g, object, this->primary_got_))
6046 // If we can merge with the last-created GOT, do it.
6047 if (current && this->merge_got_with(g, object, current))
6050 // Well, we couldn't merge, so create a new GOT. Don't check if it
6051 // fits; if it turns out that it doesn't, we'll get relocation
6052 // overflows anyway.
6053 g->set_next(current);
6057 // If we do not find any suitable primary GOT, create an empty one.
6058 if (this->primary_got_ == NULL)
6059 this->primary_got_ = new Mips_got_info<size, big_endian>();
6061 // Link primary GOT with secondary GOTs.
6062 this->primary_got_->set_next(current);
6065 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6066 // this would lead to overflow, true if they were merged successfully.
6068 template<int size, bool big_endian>
6070 Mips_output_data_got<size, big_endian>::merge_got_with(
6071 Mips_got_info<size, big_endian>* from,
6072 Mips_relobj<size, big_endian>* object,
6073 Mips_got_info<size, big_endian>* to)
6075 // Work out how many page entries we would need for the combined GOT.
6076 unsigned int estimate = this->master_got_info_->page_gotno();
6077 if (estimate >= from->page_gotno() + to->page_gotno())
6078 estimate = from->page_gotno() + to->page_gotno();
6080 // Conservatively estimate how many local and TLS entries would be needed.
6081 estimate += from->local_gotno() + to->local_gotno();
6082 estimate += from->tls_gotno() + to->tls_gotno();
6084 // If we're merging with the primary got, any TLS relocations will
6085 // come after the full set of global entries. Otherwise estimate those
6086 // conservatively as well.
6087 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6088 estimate += this->master_got_info_->global_gotno();
6090 estimate += from->global_gotno() + to->global_gotno();
6092 // Bail out if the combined GOT might be too big.
6093 unsigned int max_count =
6094 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6095 if (estimate > max_count)
6098 // Transfer the object's GOT information from FROM to TO.
6099 to->add_got_entries(from);
6100 to->add_got_page_entries(from);
6102 // Record that OBJECT should use output GOT TO.
6103 object->set_got_info(to);
6108 // Write out the GOT.
6110 template<int size, bool big_endian>
6112 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6114 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6115 Mips_stubs_entry_set;
6117 // Call parent to write out GOT.
6118 Output_data_got<size, big_endian>::do_write(of);
6120 const off_t offset = this->offset();
6121 const section_size_type oview_size =
6122 convert_to_section_size_type(this->data_size());
6123 unsigned char* const oview = of->get_output_view(offset, oview_size);
6125 // Needed for fixing values of .got section.
6126 this->got_view_ = oview;
6128 // Write lazy stub addresses.
6129 for (typename Mips_stubs_entry_set::iterator
6130 p = this->master_got_info_->global_got_symbols().begin();
6131 p != this->master_got_info_->global_got_symbols().end();
6134 Mips_symbol<size>* mips_sym = *p;
6135 if (mips_sym->has_lazy_stub())
6137 Valtype* wv = reinterpret_cast<Valtype*>(
6138 oview + this->get_primary_got_offset(mips_sym));
6140 this->target_->mips_stubs_section()->stub_address(mips_sym);
6141 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6145 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6146 for (typename Mips_stubs_entry_set::iterator
6147 p = this->master_got_info_->global_got_symbols().begin();
6148 p != this->master_got_info_->global_got_symbols().end();
6151 Mips_symbol<size>* mips_sym = *p;
6152 if (!this->multi_got()
6153 && (mips_sym->is_mips16() || mips_sym->is_micromips())
6154 && mips_sym->global_got_area() == GGA_NONE
6155 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6157 Valtype* wv = reinterpret_cast<Valtype*>(
6158 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6159 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6163 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6168 if (!this->secondary_got_relocs_.empty())
6170 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6171 // secondary GOT entries with non-zero initial value copy the value
6172 // to the corresponding primary GOT entry, and set the secondary GOT
6174 // TODO(sasa): This is workaround. It needs to be investigated further.
6176 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6178 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6179 if (reloc.symbol_is_global())
6181 Mips_symbol<size>* gsym = reloc.symbol();
6182 gold_assert(gsym != NULL);
6184 unsigned got_offset = reloc.got_offset();
6185 gold_assert(got_offset < oview_size);
6187 // Find primary GOT entry.
6188 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6189 oview + this->get_primary_got_offset(gsym));
6191 // Find secondary GOT entry.
6192 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6194 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6197 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6198 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6199 gsym->set_applied_secondary_got_fixup();
6204 of->write_output_view(offset, oview_size, oview);
6207 // We are done if there is no fix up.
6208 if (this->static_relocs_.empty())
6211 Output_segment* tls_segment = this->layout_->tls_segment();
6212 gold_assert(tls_segment != NULL);
6214 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6216 Static_reloc& reloc(this->static_relocs_[i]);
6219 if (!reloc.symbol_is_global())
6221 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6222 const Symbol_value<size>* psymval =
6223 object->local_symbol(reloc.index());
6225 // We are doing static linking. Issue an error and skip this
6226 // relocation if the symbol is undefined or in a discarded_section.
6228 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6229 if ((shndx == elfcpp::SHN_UNDEF)
6231 && shndx != elfcpp::SHN_UNDEF
6232 && !object->is_section_included(shndx)
6233 && !this->symbol_table_->is_section_folded(object, shndx)))
6235 gold_error(_("undefined or discarded local symbol %u from "
6236 " object %s in GOT"),
6237 reloc.index(), reloc.relobj()->name().c_str());
6241 value = psymval->value(object, 0);
6245 const Mips_symbol<size>* gsym = reloc.symbol();
6246 gold_assert(gsym != NULL);
6248 // We are doing static linking. Issue an error and skip this
6249 // relocation if the symbol is undefined or in a discarded_section
6250 // unless it is a weakly_undefined symbol.
6251 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6252 && !gsym->is_weak_undefined())
6254 gold_error(_("undefined or discarded symbol %s in GOT"),
6259 if (!gsym->is_weak_undefined())
6260 value = gsym->value();
6265 unsigned got_offset = reloc.got_offset();
6266 gold_assert(got_offset < oview_size);
6268 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6271 switch (reloc.r_type())
6273 case elfcpp::R_MIPS_TLS_DTPMOD32:
6274 case elfcpp::R_MIPS_TLS_DTPMOD64:
6277 case elfcpp::R_MIPS_TLS_DTPREL32:
6278 case elfcpp::R_MIPS_TLS_DTPREL64:
6279 x = value - elfcpp::DTP_OFFSET;
6281 case elfcpp::R_MIPS_TLS_TPREL32:
6282 case elfcpp::R_MIPS_TLS_TPREL64:
6283 x = value - elfcpp::TP_OFFSET;
6290 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6293 of->write_output_view(offset, oview_size, oview);
6296 // Mips_relobj methods.
6298 // Count the local symbols. The Mips backend needs to know if a symbol
6299 // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6300 // because the Symbol object keeps the ELF symbol type and st_other field.
6301 // For local symbol it is harder because we cannot access this information.
6302 // So we override the do_count_local_symbol in parent and scan local symbols to
6303 // mark MIPS16 and microMIPS functions. This is not the most efficient way but
6304 // I do not want to slow down other ports by calling a per symbol target hook
6305 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6307 template<int size, bool big_endian>
6309 Mips_relobj<size, big_endian>::do_count_local_symbols(
6310 Stringpool_template<char>* pool,
6311 Stringpool_template<char>* dynpool)
6313 // Ask parent to count the local symbols.
6314 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6315 const unsigned int loccount = this->local_symbol_count();
6319 // Initialize the mips16 and micromips function bit-vector.
6320 this->local_symbol_is_mips16_.resize(loccount, false);
6321 this->local_symbol_is_micromips_.resize(loccount, false);
6323 // Read the symbol table section header.
6324 const unsigned int symtab_shndx = this->symtab_shndx();
6325 elfcpp::Shdr<size, big_endian>
6326 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6327 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6329 // Read the local symbols.
6330 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6331 gold_assert(loccount == symtabshdr.get_sh_info());
6332 off_t locsize = loccount * sym_size;
6333 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6334 locsize, true, true);
6336 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6338 // Skip the first dummy symbol.
6340 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6342 elfcpp::Sym<size, big_endian> sym(psyms);
6343 unsigned char st_other = sym.get_st_other();
6344 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6345 this->local_symbol_is_micromips_[i] =
6346 elfcpp::elf_st_is_micromips(st_other);
6350 // Read the symbol information.
6352 template<int size, bool big_endian>
6354 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6356 // Call parent class to read symbol information.
6357 this->base_read_symbols(sd);
6359 // Read processor-specific flags in ELF file header.
6360 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6361 elfcpp::Elf_sizes<size>::ehdr_size,
6363 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6364 this->processor_specific_flags_ = ehdr.get_e_flags();
6366 // Get the section names.
6367 const unsigned char* pnamesu = sd->section_names->data();
6368 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6370 // Initialize the mips16 stub section bit-vectors.
6371 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6372 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6373 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6375 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6376 const unsigned char* pshdrs = sd->section_headers->data();
6377 const unsigned char* ps = pshdrs + shdr_size;
6378 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6380 elfcpp::Shdr<size, big_endian> shdr(ps);
6382 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6384 this->has_reginfo_section_ = true;
6385 // Read the gp value that was used to create this object. We need the
6386 // gp value while processing relocs. The .reginfo section is not used
6387 // in the 64-bit MIPS ELF ABI.
6388 section_offset_type section_offset = shdr.get_sh_offset();
6389 section_size_type section_size =
6390 convert_to_section_size_type(shdr.get_sh_size());
6391 const unsigned char* view =
6392 this->get_view(section_offset, section_size, true, false);
6394 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6396 // Read the rest of .reginfo.
6397 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6398 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6399 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6400 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6401 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6404 // In the 64-bit ABI, .MIPS.options section holds register information.
6405 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6406 // starts with this header:
6410 // // Type of option.
6411 // unsigned char kind[1];
6412 // // Size of option descriptor, including header.
6413 // unsigned char size[1];
6414 // // Section index of affected section, or 0 for global option.
6415 // unsigned char section[2];
6416 // // Information specific to this kind of option.
6417 // unsigned char info[4];
6420 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6421 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6422 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6424 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6426 section_offset_type section_offset = shdr.get_sh_offset();
6427 section_size_type section_size =
6428 convert_to_section_size_type(shdr.get_sh_size());
6429 const unsigned char* view =
6430 this->get_view(section_offset, section_size, true, false);
6431 const unsigned char* end = view + section_size;
6433 while (view + 8 <= end)
6435 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6436 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6439 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6441 this->name().c_str(),
6442 this->mips_elf_options_section_name(), sz);
6446 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6448 // In the 64 bit ABI, an ODK_REGINFO option is the following
6449 // structure. The info field of the options header is not
6454 // // Mask of general purpose registers used.
6455 // unsigned char ri_gprmask[4];
6457 // unsigned char ri_pad[4];
6458 // // Mask of co-processor registers used.
6459 // unsigned char ri_cprmask[4][4];
6460 // // GP register value for this object file.
6461 // unsigned char ri_gp_value[8];
6464 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6467 else if (kind == elfcpp::ODK_REGINFO)
6469 // In the 32 bit ABI, an ODK_REGINFO option is the following
6470 // structure. The info field of the options header is not
6471 // used. The same structure is used in .reginfo section.
6475 // unsigned char ri_gprmask[4];
6476 // unsigned char ri_cprmask[4][4];
6477 // unsigned char ri_gp_value[4];
6480 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6487 const char* name = pnames + shdr.get_sh_name();
6488 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
6489 this->section_is_mips16_call_stub_[i] =
6490 is_prefix_of(".mips16.call.", name);
6491 this->section_is_mips16_call_fp_stub_[i] =
6492 is_prefix_of(".mips16.call.fp.", name);
6494 if (strcmp(name, ".pdr") == 0)
6496 gold_assert(this->pdr_shndx_ == -1U);
6497 this->pdr_shndx_ = i;
6502 // Discard MIPS16 stub secions that are not needed.
6504 template<int size, bool big_endian>
6506 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
6508 for (typename Mips16_stubs_int_map::const_iterator
6509 it = this->mips16_stub_sections_.begin();
6510 it != this->mips16_stub_sections_.end(); ++it)
6512 Mips16_stub_section<size, big_endian>* stub_section = it->second;
6513 if (!stub_section->is_target_found())
6515 gold_error(_("no relocation found in mips16 stub section '%s'"),
6516 stub_section->object()
6517 ->section_name(stub_section->shndx()).c_str());
6520 bool discard = false;
6521 if (stub_section->is_for_local_function())
6523 if (stub_section->is_fn_stub())
6525 // This stub is for a local symbol. This stub will only
6526 // be needed if there is some relocation in this object,
6527 // other than a 16 bit function call, which refers to this
6529 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
6532 this->add_local_mips16_fn_stub(stub_section);
6536 // This stub is for a local symbol. This stub will only
6537 // be needed if there is some relocation (R_MIPS16_26) in
6538 // this object that refers to this symbol.
6539 gold_assert(stub_section->is_call_stub()
6540 || stub_section->is_call_fp_stub());
6541 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
6544 this->add_local_mips16_call_stub(stub_section);
6549 Mips_symbol<size>* gsym = stub_section->gsym();
6550 if (stub_section->is_fn_stub())
6552 if (gsym->has_mips16_fn_stub())
6553 // We already have a stub for this function.
6557 gsym->set_mips16_fn_stub(stub_section);
6558 if (gsym->should_add_dynsym_entry(symtab))
6560 // If we have a MIPS16 function with a stub, the
6561 // dynamic symbol must refer to the stub, since only
6562 // the stub uses the standard calling conventions.
6563 gsym->set_need_fn_stub();
6564 if (gsym->is_from_dynobj())
6565 gsym->set_needs_dynsym_value();
6568 if (!gsym->need_fn_stub())
6571 else if (stub_section->is_call_stub())
6573 if (gsym->is_mips16())
6574 // We don't need the call_stub; this is a 16 bit
6575 // function, so calls from other 16 bit functions are
6578 else if (gsym->has_mips16_call_stub())
6579 // We already have a stub for this function.
6582 gsym->set_mips16_call_stub(stub_section);
6586 gold_assert(stub_section->is_call_fp_stub());
6587 if (gsym->is_mips16())
6588 // We don't need the call_stub; this is a 16 bit
6589 // function, so calls from other 16 bit functions are
6592 else if (gsym->has_mips16_call_fp_stub())
6593 // We already have a stub for this function.
6596 gsym->set_mips16_call_fp_stub(stub_section);
6600 this->set_output_section(stub_section->shndx(), NULL);
6604 // Mips_output_data_la25_stub methods.
6606 // Template for standard LA25 stub.
6607 template<int size, bool big_endian>
6609 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
6611 0x3c190000, // lui $25,%hi(func)
6612 0x08000000, // j func
6613 0x27390000, // add $25,$25,%lo(func)
6617 // Template for microMIPS LA25 stub.
6618 template<int size, bool big_endian>
6620 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
6622 0x41b9, 0x0000, // lui t9,%hi(func)
6623 0xd400, 0x0000, // j func
6624 0x3339, 0x0000, // addiu t9,t9,%lo(func)
6625 0x0000, 0x0000 // nop
6628 // Create la25 stub for a symbol.
6630 template<int size, bool big_endian>
6632 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
6633 Symbol_table* symtab, Target_mips<size, big_endian>* target,
6634 Mips_symbol<size>* gsym)
6636 if (!gsym->has_la25_stub())
6638 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
6639 this->symbols_.push_back(gsym);
6640 this->create_stub_symbol(gsym, symtab, target, 16);
6644 // Create a symbol for SYM stub's value and size, to help make the disassembly
6647 template<int size, bool big_endian>
6649 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
6650 Mips_symbol<size>* sym, Symbol_table* symtab,
6651 Target_mips<size, big_endian>* target, uint64_t symsize)
6653 std::string name(".pic.");
6654 name += sym->name();
6656 unsigned int offset = sym->la25_stub_offset();
6657 if (sym->is_micromips())
6660 // Make it a local function.
6661 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
6662 Symbol_table::PREDEFINED,
6663 target->la25_stub_section(),
6664 offset, symsize, elfcpp::STT_FUNC,
6666 elfcpp::STV_DEFAULT, 0,
6668 new_sym->set_is_forced_local();
6671 // Write out la25 stubs. This uses the hand-coded instructions above,
6672 // and adjusts them as needed.
6674 template<int size, bool big_endian>
6676 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
6678 const off_t offset = this->offset();
6679 const section_size_type oview_size =
6680 convert_to_section_size_type(this->data_size());
6681 unsigned char* const oview = of->get_output_view(offset, oview_size);
6683 for (typename std::vector<Mips_symbol<size>*>::iterator
6684 p = this->symbols_.begin();
6685 p != this->symbols_.end();
6688 Mips_symbol<size>* sym = *p;
6689 unsigned char* pov = oview + sym->la25_stub_offset();
6691 Mips_address target = sym->value();
6692 if (!sym->is_micromips())
6694 elfcpp::Swap<32, big_endian>::writeval(pov,
6695 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
6696 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
6697 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
6698 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
6699 la25_stub_entry[2] | (target & 0xffff));
6700 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
6705 // First stub instruction. Paste high 16-bits of the target.
6706 elfcpp::Swap<16, big_endian>::writeval(pov,
6707 la25_stub_micromips_entry[0]);
6708 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
6709 ((target + 0x8000) >> 16) & 0xffff);
6710 // Second stub instruction. Paste low 26-bits of the target, shifted
6712 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
6713 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
6714 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
6715 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
6716 // Third stub instruction. Paste low 16-bits of the target.
6717 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
6718 la25_stub_micromips_entry[4]);
6719 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
6720 // Fourth stub instruction.
6721 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
6722 la25_stub_micromips_entry[6]);
6723 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
6724 la25_stub_micromips_entry[7]);
6728 of->write_output_view(offset, oview_size, oview);
6731 // Mips_output_data_plt methods.
6733 // The format of the first PLT entry in an O32 executable.
6734 template<int size, bool big_endian>
6735 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
6737 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
6738 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
6739 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
6740 0x031cc023, // subu $24, $24, $28
6741 0x03e07825, // or $15, $31, zero
6742 0x0018c082, // srl $24, $24, 2
6743 0x0320f809, // jalr $25
6744 0x2718fffe // subu $24, $24, 2
6747 // The format of the first PLT entry in an N32 executable. Different
6748 // because gp ($28) is not available; we use t2 ($14) instead.
6749 template<int size, bool big_endian>
6750 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
6752 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6753 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
6754 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6755 0x030ec023, // subu $24, $24, $14
6756 0x03e07825, // or $15, $31, zero
6757 0x0018c082, // srl $24, $24, 2
6758 0x0320f809, // jalr $25
6759 0x2718fffe // subu $24, $24, 2
6762 // The format of the first PLT entry in an N64 executable. Different
6763 // from N32 because of the increased size of GOT entries.
6764 template<int size, bool big_endian>
6765 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
6767 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6768 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
6769 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6770 0x030ec023, // subu $24, $24, $14
6771 0x03e07825, // or $15, $31, zero
6772 0x0018c0c2, // srl $24, $24, 3
6773 0x0320f809, // jalr $25
6774 0x2718fffe // subu $24, $24, 2
6777 // The format of the microMIPS first PLT entry in an O32 executable.
6778 // We rely on v0 ($2) rather than t8 ($24) to contain the address
6779 // of the GOTPLT entry handled, so this stub may only be used when
6780 // all the subsequent PLT entries are microMIPS code too.
6782 // The trailing NOP is for alignment and correct disassembly only.
6783 template<int size, bool big_endian>
6784 const uint32_t Mips_output_data_plt<size, big_endian>::
6785 plt0_entry_micromips_o32[] =
6787 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
6788 0xff23, 0x0000, // lw $25, 0($3)
6789 0x0535, // subu $2, $2, $3
6790 0x2525, // srl $2, $2, 2
6791 0x3302, 0xfffe, // subu $24, $2, 2
6792 0x0dff, // move $15, $31
6793 0x45f9, // jalrs $25
6794 0x0f83, // move $28, $3
6798 // The format of the microMIPS first PLT entry in an O32 executable
6799 // in the insn32 mode.
6800 template<int size, bool big_endian>
6801 const uint32_t Mips_output_data_plt<size, big_endian>::
6802 plt0_entry_micromips32_o32[] =
6804 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
6805 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
6806 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
6807 0x0398, 0xc1d0, // subu $24, $24, $28
6808 0x001f, 0x7a90, // or $15, $31, zero
6809 0x0318, 0x1040, // srl $24, $24, 2
6810 0x03f9, 0x0f3c, // jalr $25
6811 0x3318, 0xfffe // subu $24, $24, 2
6814 // The format of subsequent standard entries in the PLT.
6815 template<int size, bool big_endian>
6816 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
6818 0x3c0f0000, // lui $15, %hi(.got.plt entry)
6819 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
6820 0x03200008, // jr $25
6821 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
6824 // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
6825 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
6826 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
6827 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
6828 // target function address in register v0.
6829 template<int size, bool big_endian>
6830 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
6832 0xb303, // lw $3, 12($pc)
6833 0x651b, // move $24, $3
6834 0x9b60, // lw $3, 0($3)
6836 0x653b, // move $25, $3
6838 0x0000, 0x0000 // .word (.got.plt entry)
6841 // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
6842 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
6843 template<int size, bool big_endian>
6844 const uint32_t Mips_output_data_plt<size, big_endian>::
6845 plt_entry_micromips_o32[] =
6847 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
6848 0xff22, 0x0000, // lw $25, 0($2)
6850 0x0f02 // move $24, $2
6853 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
6854 template<int size, bool big_endian>
6855 const uint32_t Mips_output_data_plt<size, big_endian>::
6856 plt_entry_micromips32_o32[] =
6858 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
6859 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
6860 0x0019, 0x0f3c, // jr $25
6861 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
6864 // Add an entry to the PLT for a symbol referenced by r_type relocation.
6866 template<int size, bool big_endian>
6868 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
6869 unsigned int r_type)
6871 gold_assert(!gsym->has_plt_offset());
6873 // Final PLT offset for a symbol will be set in method set_plt_offsets().
6874 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
6875 + sizeof(plt0_entry_o32));
6876 this->symbols_.push_back(gsym);
6878 // Record whether the relocation requires a standard MIPS
6879 // or a compressed code entry.
6880 if (jal_reloc(r_type))
6882 if (r_type == elfcpp::R_MIPS_26)
6883 gsym->set_needs_mips_plt(true);
6885 gsym->set_needs_comp_plt(true);
6888 section_offset_type got_offset = this->got_plt_->current_data_size();
6890 // Every PLT entry needs a GOT entry which points back to the PLT
6891 // entry (this will be changed by the dynamic linker, normally
6892 // lazily when the function is called).
6893 this->got_plt_->set_current_data_size(got_offset + size/8);
6895 gsym->set_needs_dynsym_entry();
6896 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
6900 // Set final PLT offsets. For each symbol, determine whether standard or
6901 // compressed (MIPS16 or microMIPS) PLT entry is used.
6903 template<int size, bool big_endian>
6905 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
6907 // The sizes of individual PLT entries.
6908 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
6909 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
6910 ? this->compressed_plt_entry_size() : 0);
6912 for (typename std::vector<Mips_symbol<size>*>::const_iterator
6913 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
6915 Mips_symbol<size>* mips_sym = *p;
6917 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
6918 // so always use a standard entry there.
6920 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
6921 // all MIPS16 calls will go via that stub, and there is no benefit
6922 // to having a MIPS16 entry. And in the case of call_stub a
6923 // standard entry actually has to be used as the stub ends with a J
6925 if (this->target_->is_output_newabi()
6926 || mips_sym->has_mips16_call_stub()
6927 || mips_sym->has_mips16_call_fp_stub())
6929 mips_sym->set_needs_mips_plt(true);
6930 mips_sym->set_needs_comp_plt(false);
6933 // Otherwise, if there are no direct calls to the function, we
6934 // have a free choice of whether to use standard or compressed
6935 // entries. Prefer microMIPS entries if the object is known to
6936 // contain microMIPS code, so that it becomes possible to create
6937 // pure microMIPS binaries. Prefer standard entries otherwise,
6938 // because MIPS16 ones are no smaller and are usually slower.
6939 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
6941 if (this->target_->is_output_micromips())
6942 mips_sym->set_needs_comp_plt(true);
6944 mips_sym->set_needs_mips_plt(true);
6947 if (mips_sym->needs_mips_plt())
6949 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
6950 this->plt_mips_offset_ += plt_mips_entry_size;
6952 if (mips_sym->needs_comp_plt())
6954 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
6955 this->plt_comp_offset_ += plt_comp_entry_size;
6959 // Figure out the size of the PLT header if we know that we are using it.
6960 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
6961 this->plt_header_size_ = this->get_plt_header_size();
6964 // Write out the PLT. This uses the hand-coded instructions above,
6965 // and adjusts them as needed.
6967 template<int size, bool big_endian>
6969 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
6971 const off_t offset = this->offset();
6972 const section_size_type oview_size =
6973 convert_to_section_size_type(this->data_size());
6974 unsigned char* const oview = of->get_output_view(offset, oview_size);
6976 const off_t gotplt_file_offset = this->got_plt_->offset();
6977 const section_size_type gotplt_size =
6978 convert_to_section_size_type(this->got_plt_->data_size());
6979 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
6981 unsigned char* pov = oview;
6983 Mips_address plt_address = this->address();
6985 // Calculate the address of .got.plt.
6986 Mips_address gotplt_addr = this->got_plt_->address();
6987 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
6988 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
6990 // The PLT sequence is not safe for N64 if .got.plt's address can
6991 // not be loaded in two instructions.
6992 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
6993 || ~(gotplt_addr | 0x7fffffff) == 0);
6995 // Write the PLT header.
6996 const uint32_t* plt0_entry = this->get_plt_header_entry();
6997 if (plt0_entry == plt0_entry_micromips_o32)
6999 // Write microMIPS PLT header.
7000 gold_assert(gotplt_addr % 4 == 0);
7002 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7004 // ADDIUPC has a span of +/-16MB, check we're in range.
7005 if (gotpc_offset + 0x1000000 >= 0x2000000)
7007 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7008 "ADDIUPC"), (long)gotpc_offset);
7012 elfcpp::Swap<16, big_endian>::writeval(pov,
7013 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7014 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7015 (gotpc_offset >> 2) & 0xffff);
7017 for (unsigned int i = 2;
7018 i < (sizeof(plt0_entry_micromips_o32)
7019 / sizeof(plt0_entry_micromips_o32[0]));
7022 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7026 else if (plt0_entry == plt0_entry_micromips32_o32)
7028 // Write microMIPS PLT header in insn32 mode.
7029 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7030 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7031 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7032 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7033 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7034 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7036 for (unsigned int i = 6;
7037 i < (sizeof(plt0_entry_micromips32_o32)
7038 / sizeof(plt0_entry_micromips32_o32[0]));
7041 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7047 // Write standard PLT header.
7048 elfcpp::Swap<32, big_endian>::writeval(pov,
7049 plt0_entry[0] | gotplt_addr_high);
7050 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7051 plt0_entry[1] | gotplt_addr_low);
7052 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7053 plt0_entry[2] | gotplt_addr_low);
7055 for (int i = 3; i < 8; i++)
7057 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7063 unsigned char* gotplt_pov = gotplt_view;
7064 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7066 // The first two entries in .got.plt are reserved.
7067 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7068 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7070 unsigned int gotplt_offset = 2 * got_entry_size;
7071 gotplt_pov += 2 * got_entry_size;
7073 // Calculate the address of the PLT header.
7074 Mips_address header_address = (plt_address
7075 + (this->is_plt_header_compressed() ? 1 : 0));
7077 // Initialize compressed PLT area view.
7078 unsigned char* pov2 = pov + this->plt_mips_offset_;
7080 // Write the PLT entries.
7081 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7082 p = this->symbols_.begin();
7083 p != this->symbols_.end();
7084 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7086 Mips_symbol<size>* mips_sym = *p;
7088 // Calculate the address of the .got.plt entry.
7089 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7090 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7092 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7094 // Initially point the .got.plt entry at the PLT header.
7095 if (this->target_->is_output_n64())
7096 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7098 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7100 // Now handle the PLT itself. First the standard entry.
7101 if (mips_sym->has_mips_plt_offset())
7103 // Pick the load opcode (LW or LD).
7104 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7107 // Fill in the PLT entry itself.
7108 elfcpp::Swap<32, big_endian>::writeval(pov,
7109 plt_entry[0] | gotplt_entry_addr_hi);
7110 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7111 plt_entry[1] | gotplt_entry_addr_lo | load);
7112 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_entry[2]);
7113 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7114 plt_entry[3] | gotplt_entry_addr_lo);
7118 // Now the compressed entry. They come after any standard ones.
7119 if (mips_sym->has_comp_plt_offset())
7121 if (!this->target_->is_output_micromips())
7123 // Write MIPS16 PLT entry.
7124 const uint32_t* plt_entry = plt_entry_mips16_o32;
7126 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7127 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7128 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7129 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7130 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7131 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7132 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7136 else if (this->target_->use_32bit_micromips_instructions())
7138 // Write microMIPS PLT entry in insn32 mode.
7139 const uint32_t* plt_entry = plt_entry_micromips32_o32;
7141 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7142 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7143 gotplt_entry_addr_hi);
7144 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7145 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7146 gotplt_entry_addr_lo);
7147 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7148 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7149 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7150 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7151 gotplt_entry_addr_lo);
7156 // Write microMIPS PLT entry.
7157 const uint32_t* plt_entry = plt_entry_micromips_o32;
7159 gold_assert(gotplt_entry_addr % 4 == 0);
7161 Mips_address loc_address = plt_address + pov2 - oview;
7162 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7164 // ADDIUPC has a span of +/-16MB, check we're in range.
7165 if (gotpc_offset + 0x1000000 >= 0x2000000)
7167 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7168 "range of ADDIUPC"), (long)gotpc_offset);
7172 elfcpp::Swap<16, big_endian>::writeval(pov2,
7173 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7174 elfcpp::Swap<16, big_endian>::writeval(
7175 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7176 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7177 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7178 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7179 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7185 // Check the number of bytes written for standard entries.
7186 gold_assert(static_cast<section_size_type>(
7187 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7188 // Check the number of bytes written for compressed entries.
7189 gold_assert((static_cast<section_size_type>(pov2 - pov)
7190 == this->plt_comp_offset_));
7191 // Check the total number of bytes written.
7192 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7194 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7197 of->write_output_view(offset, oview_size, oview);
7198 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7201 // Mips_output_data_mips_stubs methods.
7203 // The format of the lazy binding stub when dynamic symbol count is less than
7204 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7205 template<int size, bool big_endian>
7207 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7209 0x8f998010, // lw t9,0x8010(gp)
7210 0x03e07825, // or t7,ra,zero
7211 0x0320f809, // jalr t9,ra
7212 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7215 // The format of the lazy binding stub when dynamic symbol count is less than
7216 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7217 template<int size, bool big_endian>
7219 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7221 0xdf998010, // ld t9,0x8010(gp)
7222 0x03e07825, // or t7,ra,zero
7223 0x0320f809, // jalr t9,ra
7224 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7227 // The format of the lazy binding stub when dynamic symbol count is less than
7228 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7229 template<int size, bool big_endian>
7231 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7233 0x8f998010, // lw t9,0x8010(gp)
7234 0x03e07825, // or t7,ra,zero
7235 0x0320f809, // jalr t9,ra
7236 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7239 // The format of the lazy binding stub when dynamic symbol count is less than
7240 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7241 template<int size, bool big_endian>
7243 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7245 0xdf998010, // ld t9,0x8010(gp)
7246 0x03e07825, // or t7,ra,zero
7247 0x0320f809, // jalr t9,ra
7248 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7251 // The format of the lazy binding stub when dynamic symbol count is greater than
7252 // 64K, and ABI is not N64.
7253 template<int size, bool big_endian>
7254 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7256 0x8f998010, // lw t9,0x8010(gp)
7257 0x03e07825, // or t7,ra,zero
7258 0x3c180000, // lui t8,DYN_INDEX
7259 0x0320f809, // jalr t9,ra
7260 0x37180000 // ori t8,t8,DYN_INDEX
7263 // The format of the lazy binding stub when dynamic symbol count is greater than
7264 // 64K, and ABI is N64.
7265 template<int size, bool big_endian>
7267 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7269 0xdf998010, // ld t9,0x8010(gp)
7270 0x03e07825, // or t7,ra,zero
7271 0x3c180000, // lui t8,DYN_INDEX
7272 0x0320f809, // jalr t9,ra
7273 0x37180000 // ori t8,t8,DYN_INDEX
7278 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7279 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7280 template<int size, bool big_endian>
7282 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7284 0xff3c, 0x8010, // lw t9,0x8010(gp)
7285 0x0dff, // move t7,ra
7287 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7290 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7291 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7292 template<int size, bool big_endian>
7294 Mips_output_data_mips_stubs<size, big_endian>::
7295 lazy_stub_micromips_normal_1_n64[] =
7297 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7298 0x0dff, // move t7,ra
7300 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7303 // The format of the microMIPS lazy binding stub when dynamic symbol
7304 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7305 // and ABI is not N64.
7306 template<int size, bool big_endian>
7308 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7310 0xff3c, 0x8010, // lw t9,0x8010(gp)
7311 0x0dff, // move t7,ra
7313 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7316 // The format of the microMIPS lazy binding stub when dynamic symbol
7317 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7319 template<int size, bool big_endian>
7321 Mips_output_data_mips_stubs<size, big_endian>::
7322 lazy_stub_micromips_normal_2_n64[] =
7324 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7325 0x0dff, // move t7,ra
7327 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7330 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7331 // greater than 64K, and ABI is not N64.
7332 template<int size, bool big_endian>
7334 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7336 0xff3c, 0x8010, // lw t9,0x8010(gp)
7337 0x0dff, // move t7,ra
7338 0x41b8, 0x0000, // lui t8,DYN_INDEX
7340 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7343 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7344 // greater than 64K, and ABI is N64.
7345 template<int size, bool big_endian>
7347 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7349 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7350 0x0dff, // move t7,ra
7351 0x41b8, 0x0000, // lui t8,DYN_INDEX
7353 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7356 // 32-bit microMIPS stubs.
7358 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7359 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7360 // can use only 32-bit instructions.
7361 template<int size, bool big_endian>
7363 Mips_output_data_mips_stubs<size, big_endian>::
7364 lazy_stub_micromips32_normal_1[] =
7366 0xff3c, 0x8010, // lw t9,0x8010(gp)
7367 0x001f, 0x7a90, // or t7,ra,zero
7368 0x03f9, 0x0f3c, // jalr ra,t9
7369 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7372 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7373 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7374 // use only 32-bit instructions.
7375 template<int size, bool big_endian>
7377 Mips_output_data_mips_stubs<size, big_endian>::
7378 lazy_stub_micromips32_normal_1_n64[] =
7380 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7381 0x001f, 0x7a90, // or t7,ra,zero
7382 0x03f9, 0x0f3c, // jalr ra,t9
7383 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7386 // The format of the microMIPS lazy binding stub when dynamic symbol
7387 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7388 // ABI is not N64, and we can use only 32-bit instructions.
7389 template<int size, bool big_endian>
7391 Mips_output_data_mips_stubs<size, big_endian>::
7392 lazy_stub_micromips32_normal_2[] =
7394 0xff3c, 0x8010, // lw t9,0x8010(gp)
7395 0x001f, 0x7a90, // or t7,ra,zero
7396 0x03f9, 0x0f3c, // jalr ra,t9
7397 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7400 // The format of the microMIPS lazy binding stub when dynamic symbol
7401 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7402 // ABI is N64, and we can use only 32-bit instructions.
7403 template<int size, bool big_endian>
7405 Mips_output_data_mips_stubs<size, big_endian>::
7406 lazy_stub_micromips32_normal_2_n64[] =
7408 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7409 0x001f, 0x7a90, // or t7,ra,zero
7410 0x03f9, 0x0f3c, // jalr ra,t9
7411 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7414 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7415 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7416 template<int size, bool big_endian>
7418 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7420 0xff3c, 0x8010, // lw t9,0x8010(gp)
7421 0x001f, 0x7a90, // or t7,ra,zero
7422 0x41b8, 0x0000, // lui t8,DYN_INDEX
7423 0x03f9, 0x0f3c, // jalr ra,t9
7424 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7427 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7428 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7429 template<int size, bool big_endian>
7431 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
7433 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7434 0x001f, 0x7a90, // or t7,ra,zero
7435 0x41b8, 0x0000, // lui t8,DYN_INDEX
7436 0x03f9, 0x0f3c, // jalr ra,t9
7437 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7440 // Create entry for a symbol.
7442 template<int size, bool big_endian>
7444 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7445 Mips_symbol<size>* gsym)
7447 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7449 this->symbols_.insert(gsym);
7450 gsym->set_has_lazy_stub(true);
7454 // Remove entry for a symbol.
7456 template<int size, bool big_endian>
7458 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
7459 Mips_symbol<size>* gsym)
7461 if (gsym->has_lazy_stub())
7463 this->symbols_.erase(gsym);
7464 gsym->set_has_lazy_stub(false);
7468 // Set stub offsets for symbols. This method expects that the number of
7469 // entries in dynamic symbol table is set.
7471 template<int size, bool big_endian>
7473 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
7475 gold_assert(this->dynsym_count_ != -1U);
7477 if (this->stub_offsets_are_set_)
7480 unsigned int stub_size = this->stub_size();
7481 unsigned int offset = 0;
7482 for (typename Mips_stubs_entry_set::const_iterator
7483 p = this->symbols_.begin();
7484 p != this->symbols_.end();
7485 ++p, offset += stub_size)
7487 Mips_symbol<size>* mips_sym = *p;
7488 mips_sym->set_lazy_stub_offset(offset);
7490 this->stub_offsets_are_set_ = true;
7493 template<int size, bool big_endian>
7495 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
7497 for (typename Mips_stubs_entry_set::const_iterator
7498 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7500 Mips_symbol<size>* sym = *p;
7501 if (sym->is_from_dynobj())
7502 sym->set_needs_dynsym_value();
7506 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
7507 // adjusts them as needed.
7509 template<int size, bool big_endian>
7511 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
7513 const off_t offset = this->offset();
7514 const section_size_type oview_size =
7515 convert_to_section_size_type(this->data_size());
7516 unsigned char* const oview = of->get_output_view(offset, oview_size);
7518 bool big_stub = this->dynsym_count_ > 0x10000;
7520 unsigned char* pov = oview;
7521 for (typename Mips_stubs_entry_set::const_iterator
7522 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7524 Mips_symbol<size>* sym = *p;
7525 const uint32_t* lazy_stub;
7526 bool n64 = this->target_->is_output_n64();
7528 if (!this->target_->is_output_micromips())
7530 // Write standard (non-microMIPS) stub.
7533 if (sym->dynsym_index() & ~0x7fff)
7534 // Dynsym index is between 32K and 64K.
7535 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
7537 // Dynsym index is less than 32K.
7538 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
7541 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
7544 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7545 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
7551 // LUI instruction of the big stub. Paste high 16 bits of the
7553 elfcpp::Swap<32, big_endian>::writeval(pov,
7554 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
7558 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7559 // Last stub instruction. Paste low 16 bits of the dynsym index.
7560 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7561 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
7564 else if (this->target_->use_32bit_micromips_instructions())
7566 // Write microMIPS stub in insn32 mode.
7569 if (sym->dynsym_index() & ~0x7fff)
7570 // Dynsym index is between 32K and 64K.
7571 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
7572 : lazy_stub_micromips32_normal_2;
7574 // Dynsym index is less than 32K.
7575 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
7576 : lazy_stub_micromips32_normal_1;
7579 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
7580 : lazy_stub_micromips32_big;
7583 // First stub instruction. We emit 32-bit microMIPS instructions by
7584 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7585 // the instruction where the opcode is must always come first, for
7586 // both little and big endian.
7587 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7588 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7589 // Second stub instruction.
7590 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7591 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
7596 // LUI instruction of the big stub. Paste high 16 bits of the
7598 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7599 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7600 (sym->dynsym_index() >> 16) & 0x7fff);
7604 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7605 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7606 // Last stub instruction. Paste low 16 bits of the dynsym index.
7607 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7608 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7609 sym->dynsym_index() & 0xffff);
7614 // Write microMIPS stub.
7617 if (sym->dynsym_index() & ~0x7fff)
7618 // Dynsym index is between 32K and 64K.
7619 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
7620 : lazy_stub_micromips_normal_2;
7622 // Dynsym index is less than 32K.
7623 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
7624 : lazy_stub_micromips_normal_1;
7627 lazy_stub = n64 ? lazy_stub_micromips_big_n64
7628 : lazy_stub_micromips_big;
7631 // First stub instruction. We emit 32-bit microMIPS instructions by
7632 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7633 // the instruction where the opcode is must always come first, for
7634 // both little and big endian.
7635 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7636 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7637 // Second stub instruction.
7638 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7643 // LUI instruction of the big stub. Paste high 16 bits of the
7645 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7646 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7647 (sym->dynsym_index() >> 16) & 0x7fff);
7651 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7652 // Last stub instruction. Paste low 16 bits of the dynsym index.
7653 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7654 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7655 sym->dynsym_index() & 0xffff);
7660 // We always allocate 20 bytes for every stub, because final dynsym count is
7661 // not known in method do_finalize_sections. There are 4 unused bytes per
7662 // stub if final dynsym count is less than 0x10000.
7663 unsigned int used = pov - oview;
7664 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
7665 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
7667 // Fill the unused space with zeroes.
7668 // TODO(sasa): Can we strip unused bytes during the relaxation?
7670 memset(pov, 0, unused);
7672 of->write_output_view(offset, oview_size, oview);
7675 // Mips_output_section_reginfo methods.
7677 template<int size, bool big_endian>
7679 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
7681 off_t offset = this->offset();
7682 off_t data_size = this->data_size();
7684 unsigned char* view = of->get_output_view(offset, data_size);
7685 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
7686 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
7687 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
7688 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
7689 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
7690 // Write the gp value.
7691 elfcpp::Swap<size, big_endian>::writeval(view + 20,
7692 this->target_->gp_value());
7694 of->write_output_view(offset, data_size, view);
7697 // Mips_copy_relocs methods.
7699 // Emit any saved relocs.
7701 template<int sh_type, int size, bool big_endian>
7703 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
7704 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7705 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7707 for (typename Copy_relocs<sh_type, size, big_endian>::
7708 Copy_reloc_entries::iterator p = this->entries_.begin();
7709 p != this->entries_.end();
7711 emit_entry(*p, reloc_section, symtab, layout, target);
7713 // We no longer need the saved information.
7714 this->entries_.clear();
7717 // Emit the reloc if appropriate.
7719 template<int sh_type, int size, bool big_endian>
7721 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
7722 Copy_reloc_entry& entry,
7723 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7724 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7726 // If the symbol is no longer defined in a dynamic object, then we
7727 // emitted a COPY relocation, and we do not want to emit this
7728 // dynamic relocation.
7729 if (!entry.sym_->is_from_dynobj())
7732 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
7733 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
7734 || entry.reloc_type_ == elfcpp::R_MIPS_64);
7736 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
7737 if (can_make_dynamic && !sym->has_static_relocs())
7739 Mips_relobj<size, big_endian>* object =
7740 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
7741 target->got_section(symtab, layout)->record_global_got_symbol(
7742 sym, object, entry.reloc_type_, true, false);
7743 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
7744 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
7745 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
7747 target->rel_dyn_section(layout)->add_symbolless_global_addend(
7748 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
7749 entry.shndx_, entry.address_);
7752 this->make_copy_reloc(symtab, layout,
7753 static_cast<Sized_symbol<size>*>(entry.sym_),
7758 // Target_mips methods.
7760 // Return the value to use for a dynamic symbol which requires special
7761 // treatment. This is how we support equality comparisons of function
7762 // pointers across shared library boundaries, as described in the
7763 // processor specific ABI supplement.
7765 template<int size, bool big_endian>
7767 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7770 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
7772 if (!mips_sym->has_lazy_stub())
7774 if (mips_sym->has_plt_offset())
7776 // We distinguish between PLT entries and lazy-binding stubs by
7777 // giving the former an st_other value of STO_MIPS_PLT. Set the
7778 // value to the stub address if there are any relocations in the
7779 // binary where pointer equality matters.
7780 if (mips_sym->pointer_equality_needed())
7782 // Prefer a standard MIPS PLT entry.
7783 if (mips_sym->has_mips_plt_offset())
7784 value = this->plt_section()->mips_entry_address(mips_sym);
7786 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
7794 // First, set stub offsets for symbols. This method expects that the
7795 // number of entries in dynamic symbol table is set.
7796 this->mips_stubs_section()->set_lazy_stub_offsets();
7798 // The run-time linker uses the st_value field of the symbol
7799 // to reset the global offset table entry for this external
7800 // to its stub address when unlinking a shared object.
7801 value = this->mips_stubs_section()->stub_address(mips_sym);
7804 if (mips_sym->has_mips16_fn_stub())
7806 // If we have a MIPS16 function with a stub, the dynamic symbol must
7807 // refer to the stub, since only the stub uses the standard calling
7809 value = mips_sym->template
7810 get_mips16_fn_stub<big_endian>()->output_address();
7816 // Get the dynamic reloc section, creating it if necessary. It's always
7817 // .rel.dyn, even for MIPS64.
7819 template<int size, bool big_endian>
7820 typename Target_mips<size, big_endian>::Reloc_section*
7821 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
7823 if (this->rel_dyn_ == NULL)
7825 gold_assert(layout != NULL);
7826 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
7827 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
7828 elfcpp::SHF_ALLOC, this->rel_dyn_,
7829 ORDER_DYNAMIC_RELOCS, false);
7831 // First entry in .rel.dyn has to be null.
7832 // This is hack - we define dummy output data and set its address to 0,
7833 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
7834 // This ensures that the entry is null.
7835 Output_data* od = new Output_data_zero_fill(0, 0);
7837 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
7839 return this->rel_dyn_;
7842 // Get the GOT section, creating it if necessary.
7844 template<int size, bool big_endian>
7845 Mips_output_data_got<size, big_endian>*
7846 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
7849 if (this->got_ == NULL)
7851 gold_assert(symtab != NULL && layout != NULL);
7853 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
7855 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
7856 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
7857 elfcpp::SHF_MIPS_GPREL),
7858 this->got_, ORDER_DATA, false);
7860 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
7861 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
7862 Symbol_table::PREDEFINED,
7864 0, 0, elfcpp::STT_OBJECT,
7866 elfcpp::STV_DEFAULT, 0,
7873 // Calculate value of _gp symbol.
7875 template<int size, bool big_endian>
7877 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
7879 if (this->gp_ != NULL)
7882 Output_data* section = layout->find_output_section(".got");
7883 if (section == NULL)
7885 // If there is no .got section, gp should be based on .sdata.
7886 // TODO(sasa): This is probably not needed. This was needed for older
7887 // MIPS architectures which accessed both GOT and .sdata section using
7888 // gp-relative addressing. Modern Mips Linux ELF architectures don't
7889 // access .sdata using gp-relative addressing.
7890 for (Layout::Section_list::const_iterator
7891 p = layout->section_list().begin();
7892 p != layout->section_list().end();
7895 if (strcmp((*p)->name(), ".sdata") == 0)
7903 Sized_symbol<size>* gp =
7904 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
7907 if (gp->source() != Symbol::IS_CONSTANT && section != NULL)
7908 gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0,
7911 elfcpp::STV_DEFAULT, 0,
7915 else if (section != NULL)
7917 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
7918 "_gp", NULL, Symbol_table::PREDEFINED,
7919 section, MIPS_GP_OFFSET, 0,
7922 elfcpp::STV_DEFAULT,
7928 // Set the dynamic symbol indexes. INDEX is the index of the first
7929 // global dynamic symbol. Pointers to the symbols are stored into the
7930 // vector SYMS. The names are added to DYNPOOL. This returns an
7931 // updated dynamic symbol index.
7933 template<int size, bool big_endian>
7935 Target_mips<size, big_endian>::do_set_dynsym_indexes(
7936 std::vector<Symbol*>* dyn_symbols, unsigned int index,
7937 std::vector<Symbol*>* syms, Stringpool* dynpool,
7938 Versions* versions, Symbol_table* symtab) const
7940 std::vector<Symbol*> non_got_symbols;
7941 std::vector<Symbol*> got_symbols;
7943 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
7946 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
7947 p != non_got_symbols.end();
7952 // Note that SYM may already have a dynamic symbol index, since
7953 // some symbols appear more than once in the symbol table, with
7954 // and without a version.
7956 if (!sym->has_dynsym_index())
7958 sym->set_dynsym_index(index);
7960 syms->push_back(sym);
7961 dynpool->add(sym->name(), false, NULL);
7963 // Record any version information.
7964 if (sym->version() != NULL)
7965 versions->record_version(symtab, dynpool, sym);
7967 // If the symbol is defined in a dynamic object and is
7968 // referenced in a regular object, then mark the dynamic
7969 // object as needed. This is used to implement --as-needed.
7970 if (sym->is_from_dynobj() && sym->in_reg())
7971 sym->object()->set_is_needed();
7975 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
7976 p != got_symbols.end();
7980 if (!sym->has_dynsym_index())
7982 // Record any version information.
7983 if (sym->version() != NULL)
7984 versions->record_version(symtab, dynpool, sym);
7988 index = versions->finalize(symtab, index, syms);
7990 int got_sym_count = 0;
7991 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
7992 p != got_symbols.end();
7997 if (!sym->has_dynsym_index())
8000 sym->set_dynsym_index(index);
8002 syms->push_back(sym);
8003 dynpool->add(sym->name(), false, NULL);
8005 // If the symbol is defined in a dynamic object and is
8006 // referenced in a regular object, then mark the dynamic
8007 // object as needed. This is used to implement --as-needed.
8008 if (sym->is_from_dynobj() && sym->in_reg())
8009 sym->object()->set_is_needed();
8013 // Set index of the first symbol that has .got entry.
8014 this->got_->set_first_global_got_dynsym_index(
8015 got_sym_count > 0 ? index - got_sym_count : -1U);
8017 if (this->mips_stubs_ != NULL)
8018 this->mips_stubs_->set_dynsym_count(index);
8023 // Create a PLT entry for a global symbol referenced by r_type relocation.
8025 template<int size, bool big_endian>
8027 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8029 Mips_symbol<size>* gsym,
8030 unsigned int r_type)
8032 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8035 if (this->plt_ == NULL)
8037 // Create the GOT section first.
8038 this->got_section(symtab, layout);
8040 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8041 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8042 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8043 this->got_plt_, ORDER_DATA, false);
8045 // The first two entries are reserved.
8046 this->got_plt_->set_current_data_size(2 * size/8);
8048 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8051 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8053 | elfcpp::SHF_EXECINSTR),
8054 this->plt_, ORDER_PLT, false);
8057 this->plt_->add_entry(gsym, r_type);
8061 // Get the .MIPS.stubs section, creating it if necessary.
8063 template<int size, bool big_endian>
8064 Mips_output_data_mips_stubs<size, big_endian>*
8065 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8067 if (this->mips_stubs_ == NULL)
8070 new Mips_output_data_mips_stubs<size, big_endian>(this);
8071 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8073 | elfcpp::SHF_EXECINSTR),
8074 this->mips_stubs_, ORDER_PLT, false);
8076 return this->mips_stubs_;
8079 // Get the LA25 stub section, creating it if necessary.
8081 template<int size, bool big_endian>
8082 Mips_output_data_la25_stub<size, big_endian>*
8083 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8085 if (this->la25_stub_ == NULL)
8087 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8088 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8090 | elfcpp::SHF_EXECINSTR),
8091 this->la25_stub_, ORDER_TEXT, false);
8093 return this->la25_stub_;
8096 // Process the relocations to determine unreferenced sections for
8097 // garbage collection.
8099 template<int size, bool big_endian>
8101 Target_mips<size, big_endian>::gc_process_relocs(
8102 Symbol_table* symtab,
8104 Sized_relobj_file<size, big_endian>* object,
8105 unsigned int data_shndx,
8106 unsigned int sh_type,
8107 const unsigned char* prelocs,
8109 Output_section* output_section,
8110 bool needs_special_offset_handling,
8111 size_t local_symbol_count,
8112 const unsigned char* plocal_symbols)
8114 typedef Target_mips<size, big_endian> Mips;
8116 if (sh_type == elfcpp::SHT_REL)
8118 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8121 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8130 needs_special_offset_handling,
8134 else if (sh_type == elfcpp::SHT_RELA)
8136 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8139 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8148 needs_special_offset_handling,
8156 // Scan relocations for a section.
8158 template<int size, bool big_endian>
8160 Target_mips<size, big_endian>::scan_relocs(
8161 Symbol_table* symtab,
8163 Sized_relobj_file<size, big_endian>* object,
8164 unsigned int data_shndx,
8165 unsigned int sh_type,
8166 const unsigned char* prelocs,
8168 Output_section* output_section,
8169 bool needs_special_offset_handling,
8170 size_t local_symbol_count,
8171 const unsigned char* plocal_symbols)
8173 typedef Target_mips<size, big_endian> Mips;
8175 if (sh_type == elfcpp::SHT_REL)
8177 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8180 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8189 needs_special_offset_handling,
8193 else if (sh_type == elfcpp::SHT_RELA)
8195 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8198 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8207 needs_special_offset_handling,
8213 template<int size, bool big_endian>
8215 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8217 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8218 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8219 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8220 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8221 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8222 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8223 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
8226 // Return the MACH for a MIPS e_flags value.
8227 template<int size, bool big_endian>
8229 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8231 switch (flags & elfcpp::EF_MIPS_MACH)
8233 case elfcpp::E_MIPS_MACH_3900:
8234 return mach_mips3900;
8236 case elfcpp::E_MIPS_MACH_4010:
8237 return mach_mips4010;
8239 case elfcpp::E_MIPS_MACH_4100:
8240 return mach_mips4100;
8242 case elfcpp::E_MIPS_MACH_4111:
8243 return mach_mips4111;
8245 case elfcpp::E_MIPS_MACH_4120:
8246 return mach_mips4120;
8248 case elfcpp::E_MIPS_MACH_4650:
8249 return mach_mips4650;
8251 case elfcpp::E_MIPS_MACH_5400:
8252 return mach_mips5400;
8254 case elfcpp::E_MIPS_MACH_5500:
8255 return mach_mips5500;
8257 case elfcpp::E_MIPS_MACH_9000:
8258 return mach_mips9000;
8260 case elfcpp::E_MIPS_MACH_SB1:
8261 return mach_mips_sb1;
8263 case elfcpp::E_MIPS_MACH_LS2E:
8264 return mach_mips_loongson_2e;
8266 case elfcpp::E_MIPS_MACH_LS2F:
8267 return mach_mips_loongson_2f;
8269 case elfcpp::E_MIPS_MACH_LS3A:
8270 return mach_mips_loongson_3a;
8272 case elfcpp::E_MIPS_MACH_OCTEON2:
8273 return mach_mips_octeon2;
8275 case elfcpp::E_MIPS_MACH_OCTEON:
8276 return mach_mips_octeon;
8278 case elfcpp::E_MIPS_MACH_XLR:
8279 return mach_mips_xlr;
8282 switch (flags & elfcpp::EF_MIPS_ARCH)
8285 case elfcpp::E_MIPS_ARCH_1:
8286 return mach_mips3000;
8288 case elfcpp::E_MIPS_ARCH_2:
8289 return mach_mips6000;
8291 case elfcpp::E_MIPS_ARCH_3:
8292 return mach_mips4000;
8294 case elfcpp::E_MIPS_ARCH_4:
8295 return mach_mips8000;
8297 case elfcpp::E_MIPS_ARCH_5:
8300 case elfcpp::E_MIPS_ARCH_32:
8301 return mach_mipsisa32;
8303 case elfcpp::E_MIPS_ARCH_64:
8304 return mach_mipsisa64;
8306 case elfcpp::E_MIPS_ARCH_32R2:
8307 return mach_mipsisa32r2;
8309 case elfcpp::E_MIPS_ARCH_64R2:
8310 return mach_mipsisa64r2;
8317 // Check whether machine EXTENSION is an extension of machine BASE.
8318 template<int size, bool big_endian>
8320 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
8321 unsigned int extension)
8323 if (extension == base)
8326 if ((base == mach_mipsisa32)
8327 && this->mips_mach_extends(mach_mipsisa64, extension))
8330 if ((base == mach_mipsisa32r2)
8331 && this->mips_mach_extends(mach_mipsisa64r2, extension))
8334 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
8335 if (extension == this->mips_mach_extensions_[i].first)
8337 extension = this->mips_mach_extensions_[i].second;
8338 if (extension == base)
8345 template<int size, bool big_endian>
8347 Target_mips<size, big_endian>::merge_processor_specific_flags(
8348 const std::string& name, elfcpp::Elf_Word in_flags, bool dyn_obj)
8350 // If flags are not set yet, just copy them.
8351 if (!this->are_processor_specific_flags_set())
8353 this->set_processor_specific_flags(in_flags);
8354 this->mach_ = this->elf_mips_mach(in_flags);
8358 elfcpp::Elf_Word new_flags = in_flags;
8359 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
8360 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
8361 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
8363 // Check flag compatibility.
8364 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
8365 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
8367 // Some IRIX 6 BSD-compatibility objects have this bit set. It
8368 // doesn't seem to matter.
8369 new_flags &= ~elfcpp::EF_MIPS_XGOT;
8370 old_flags &= ~elfcpp::EF_MIPS_XGOT;
8372 // MIPSpro generates ucode info in n64 objects. Again, we should
8373 // just be able to ignore this.
8374 new_flags &= ~elfcpp::EF_MIPS_UCODE;
8375 old_flags &= ~elfcpp::EF_MIPS_UCODE;
8377 // DSOs should only be linked with CPIC code.
8379 new_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC;
8381 if (new_flags == old_flags)
8383 this->set_processor_specific_flags(merged_flags);
8387 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
8388 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
8389 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
8392 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
8393 merged_flags |= elfcpp::EF_MIPS_CPIC;
8394 if (!(new_flags & elfcpp::EF_MIPS_PIC))
8395 merged_flags &= ~elfcpp::EF_MIPS_PIC;
8397 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
8398 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
8400 // Compare the ISAs.
8401 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
8402 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
8403 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
8405 // Output ISA isn't the same as, or an extension of, input ISA.
8406 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
8408 // Copy the architecture info from input object to output. Also copy
8409 // the 32-bit flag (if set) so that we continue to recognise
8410 // output as a 32-bit binary.
8411 this->mach_ = this->elf_mips_mach(in_flags);
8412 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
8413 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
8414 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
8416 // Copy across the ABI flags if output doesn't use them
8417 // and if that was what caused us to treat input object as 32-bit.
8418 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
8419 && this->mips_32bit_flags(new_flags)
8420 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
8421 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
8424 // The ISAs aren't compatible.
8425 gold_error(_("%s: linking %s module with previous %s modules"),
8426 name.c_str(), this->elf_mips_mach_name(in_flags),
8427 this->elf_mips_mach_name(merged_flags));
8430 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
8431 | elfcpp::EF_MIPS_32BITMODE));
8432 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
8433 | elfcpp::EF_MIPS_32BITMODE));
8436 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
8438 // Only error if both are set (to different values).
8439 if ((new_flags & elfcpp::EF_MIPS_ABI)
8440 && (old_flags & elfcpp::EF_MIPS_ABI))
8441 gold_error(_("%s: ABI mismatch: linking %s module with "
8442 "previous %s modules"), name.c_str(),
8443 this->elf_mips_abi_name(in_flags),
8444 this->elf_mips_abi_name(merged_flags));
8446 new_flags &= ~elfcpp::EF_MIPS_ABI;
8447 old_flags &= ~elfcpp::EF_MIPS_ABI;
8450 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
8451 // and allow arbitrary mixing of the remaining ASEs (retain the union).
8452 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
8453 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
8455 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
8456 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
8457 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
8458 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
8459 int micro_mis = old_m16 && new_micro;
8460 int m16_mis = old_micro && new_m16;
8462 if (m16_mis || micro_mis)
8463 gold_error(_("%s: ASE mismatch: linking %s module with "
8464 "previous %s modules"), name.c_str(),
8465 m16_mis ? "MIPS16" : "microMIPS",
8466 m16_mis ? "microMIPS" : "MIPS16");
8468 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
8470 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
8471 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
8474 // Warn about any other mismatches.
8475 if (new_flags != old_flags)
8476 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
8477 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
8479 this->set_processor_specific_flags(merged_flags);
8482 // Adjust ELF file header.
8484 template<int size, bool big_endian>
8486 Target_mips<size, big_endian>::do_adjust_elf_header(
8487 unsigned char* view,
8490 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
8492 if (!this->entry_symbol_is_compressed_)
8495 elfcpp::Ehdr<size, big_endian> ehdr(view);
8496 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
8498 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
8501 // do_make_elf_object to override the same function in the base class.
8502 // We need to use a target-specific sub-class of
8503 // Sized_relobj_file<size, big_endian> to store Mips specific information.
8504 // Hence we need to have our own ELF object creation.
8506 template<int size, bool big_endian>
8508 Target_mips<size, big_endian>::do_make_elf_object(
8509 const std::string& name,
8510 Input_file* input_file,
8511 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
8513 int et = ehdr.get_e_type();
8514 // ET_EXEC files are valid input for --just-symbols/-R,
8515 // and we treat them as relocatable objects.
8516 if (et == elfcpp::ET_REL
8517 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
8519 Mips_relobj<size, big_endian>* obj =
8520 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
8524 else if (et == elfcpp::ET_DYN)
8526 // TODO(sasa): Should we create Mips_dynobj?
8527 return Target::do_make_elf_object(name, input_file, offset, ehdr);
8531 gold_error(_("%s: unsupported ELF file type %d"),
8537 // Finalize the sections.
8539 template <int size, bool big_endian>
8541 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
8542 const Input_objects* input_objects,
8543 Symbol_table* symtab)
8545 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
8546 // DT_FINI have correct values.
8547 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
8548 symtab->lookup(parameters->options().init()));
8549 if (init != NULL && (init->is_mips16() || init->is_micromips()))
8550 init->set_value(init->value() | 1);
8551 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
8552 symtab->lookup(parameters->options().fini()));
8553 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
8554 fini->set_value(fini->value() | 1);
8556 // Check whether the entry symbol is mips16 or micromips. This is needed to
8557 // adjust entry address in ELF header.
8558 Mips_symbol<size>* entry =
8559 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
8560 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
8561 || entry->is_micromips()));
8563 if (!parameters->doing_static_link()
8564 && (strcmp(parameters->options().hash_style(), "gnu") == 0
8565 || strcmp(parameters->options().hash_style(), "both") == 0))
8567 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
8568 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
8569 // MIPS ABI requires a mapping between the GOT and the symbol table.
8570 gold_error(".gnu.hash is incompatible with the MIPS ABI");
8573 // Check whether the final section that was scanned has HI16 or GOT16
8574 // relocations without the corresponding LO16 part.
8575 if (this->got16_addends_.size() > 0)
8576 gold_error("Can't find matching LO16 reloc");
8579 this->set_gp(layout, symtab);
8581 // Check for any mips16 stub sections that we can discard.
8582 if (!parameters->options().relocatable())
8584 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8585 p != input_objects->relobj_end();
8588 Mips_relobj<size, big_endian>* object =
8589 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
8590 object->discard_mips16_stub_sections(symtab);
8594 Valtype gprmask = 0;
8595 Valtype cprmask1 = 0;
8596 Valtype cprmask2 = 0;
8597 Valtype cprmask3 = 0;
8598 Valtype cprmask4 = 0;
8599 bool has_reginfo_section = false;
8601 // Merge processor-specific flags.
8602 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8603 p != input_objects->relobj_end();
8606 Mips_relobj<size, big_endian>* relobj =
8607 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
8609 // Merge .reginfo contents of input objects.
8610 if (relobj->has_reginfo_section())
8612 has_reginfo_section = true;
8613 gprmask |= relobj->gprmask();
8614 cprmask1 |= relobj->cprmask1();
8615 cprmask2 |= relobj->cprmask2();
8616 cprmask3 |= relobj->cprmask3();
8617 cprmask4 |= relobj->cprmask4();
8620 Input_file::Format format = relobj->input_file()->format();
8621 if (format == Input_file::FORMAT_ELF)
8623 // Read processor-specific flags in ELF file header.
8624 const unsigned char* pehdr = relobj->get_view(
8625 elfcpp::file_header_offset,
8626 elfcpp::Elf_sizes<size>::ehdr_size,
8629 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
8630 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
8631 // If all input sections will be discarded, don't use this object
8632 // file for merging processor specific flags.
8633 bool should_merge_processor_specific_flags = false;
8635 for (unsigned int i = 1; i < relobj->shnum(); ++i)
8636 if (relobj->output_section(i) != NULL)
8638 should_merge_processor_specific_flags = true;
8642 if (should_merge_processor_specific_flags)
8643 this->merge_processor_specific_flags(relobj->name(), in_flags,
8648 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8649 p != input_objects->dynobj_end();
8652 Sized_dynobj<size, big_endian>* dynobj =
8653 static_cast<Sized_dynobj<size, big_endian>*>(*p);
8655 // Read processor-specific flags.
8656 const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset,
8657 elfcpp::Elf_sizes<size>::ehdr_size,
8660 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
8661 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
8663 this->merge_processor_specific_flags(dynobj->name(), in_flags, true);
8666 if (has_reginfo_section && !parameters->options().gc_sections())
8668 // Create .reginfo output section.
8669 Mips_output_section_reginfo<size, big_endian>* reginfo_section =
8670 new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
8672 cprmask3, cprmask4);
8674 Output_section* os =
8675 layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
8676 elfcpp::SHF_ALLOC, reginfo_section,
8677 ORDER_INVALID, false);
8679 if (!parameters->options().relocatable() && os != NULL)
8681 Output_segment* reginfo_segment =
8682 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
8684 reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
8688 if (this->plt_ != NULL)
8690 // Set final PLT offsets for symbols.
8691 this->plt_section()->set_plt_offsets();
8693 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
8694 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
8695 // there are no standard PLT entries present.
8696 unsigned char nonvis = 0;
8697 if (this->is_output_micromips()
8698 && !this->plt_section()->has_standard_entries())
8699 nonvis = elfcpp::STO_MICROMIPS >> 2;
8700 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
8701 Symbol_table::PREDEFINED,
8703 0, 0, elfcpp::STT_FUNC,
8705 elfcpp::STV_DEFAULT, nonvis,
8709 if (this->mips_stubs_ != NULL)
8711 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
8712 unsigned char nonvis = 0;
8713 if (this->is_output_micromips())
8714 nonvis = elfcpp::STO_MICROMIPS >> 2;
8715 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
8716 Symbol_table::PREDEFINED,
8718 0, 0, elfcpp::STT_FUNC,
8720 elfcpp::STV_DEFAULT, nonvis,
8724 if (!parameters->options().relocatable() && !parameters->doing_static_link())
8725 // In case there is no .got section, create one.
8726 this->got_section(symtab, layout);
8728 // Emit any relocs we saved in an attempt to avoid generating COPY
8730 if (this->copy_relocs_.any_saved_relocs())
8731 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
8734 // Emit dynamic relocs.
8735 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
8736 p != this->dyn_relocs_.end();
8738 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
8740 if (this->has_got_section())
8741 this->got_section()->lay_out_got(layout, symtab, input_objects);
8743 if (this->mips_stubs_ != NULL)
8744 this->mips_stubs_->set_needs_dynsym_value();
8746 // Check for functions that might need $25 to be valid on entry.
8747 // TODO(sasa): Can we do this without iterating over all symbols?
8748 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
8749 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
8752 // Add NULL segment.
8753 if (!parameters->options().relocatable())
8754 layout->make_output_segment(elfcpp::PT_NULL, 0);
8756 // Fill in some more dynamic tags.
8757 // TODO(sasa): Add more dynamic tags.
8758 const Reloc_section* rel_plt = (this->plt_ == NULL
8759 ? NULL : this->plt_->rel_plt());
8760 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
8761 this->rel_dyn_, true, false);
8763 Output_data_dynamic* const odyn = layout->dynamic_data();
8765 && !parameters->options().relocatable()
8766 && !parameters->doing_static_link())
8769 // This element holds a 32-bit version id for the Runtime
8770 // Linker Interface. This will start at integer value 1.
8772 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
8775 d_val = elfcpp::RHF_NOTPOT;
8776 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
8778 // Save layout for using when emiting custom dynamic tags.
8779 this->layout_ = layout;
8781 // This member holds the base address of the segment.
8782 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
8784 // This member holds the number of entries in the .dynsym section.
8785 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
8787 // This member holds the index of the first dynamic symbol
8788 // table entry that corresponds to an entry in the global offset table.
8789 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
8791 // This member holds the number of local GOT entries.
8792 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
8793 this->got_->get_local_gotno());
8795 if (this->plt_ != NULL)
8796 // DT_MIPS_PLTGOT dynamic tag
8797 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
8801 // Get the custom dynamic tag value.
8802 template<int size, bool big_endian>
8804 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
8808 case elfcpp::DT_MIPS_BASE_ADDRESS:
8810 // The base address of the segment.
8811 // At this point, the segment list has been sorted into final order,
8812 // so just return vaddr of the first readable PT_LOAD segment.
8813 Output_segment* seg =
8814 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
8815 gold_assert(seg != NULL);
8816 return seg->vaddr();
8819 case elfcpp::DT_MIPS_SYMTABNO:
8820 // The number of entries in the .dynsym section.
8821 return this->get_dt_mips_symtabno();
8823 case elfcpp::DT_MIPS_GOTSYM:
8825 // The index of the first dynamic symbol table entry that corresponds
8826 // to an entry in the GOT.
8827 if (this->got_->first_global_got_dynsym_index() != -1U)
8828 return this->got_->first_global_got_dynsym_index();
8830 // In case if we don't have global GOT symbols we default to setting
8831 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
8832 return this->get_dt_mips_symtabno();
8836 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
8839 return (unsigned int)-1;
8842 // Relocate section data.
8844 template<int size, bool big_endian>
8846 Target_mips<size, big_endian>::relocate_section(
8847 const Relocate_info<size, big_endian>* relinfo,
8848 unsigned int sh_type,
8849 const unsigned char* prelocs,
8851 Output_section* output_section,
8852 bool needs_special_offset_handling,
8853 unsigned char* view,
8854 Mips_address address,
8855 section_size_type view_size,
8856 const Reloc_symbol_changes* reloc_symbol_changes)
8858 typedef Target_mips<size, big_endian> Mips;
8859 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
8861 if (sh_type == elfcpp::SHT_REL)
8863 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8866 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
8867 gold::Default_comdat_behavior, Classify_reloc>(
8873 needs_special_offset_handling,
8877 reloc_symbol_changes);
8879 else if (sh_type == elfcpp::SHT_RELA)
8881 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8884 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
8885 gold::Default_comdat_behavior, Classify_reloc>(
8891 needs_special_offset_handling,
8895 reloc_symbol_changes);
8899 // Return the size of a relocation while scanning during a relocatable
8903 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
8907 case elfcpp::R_MIPS_NONE:
8908 case elfcpp::R_MIPS_TLS_DTPMOD64:
8909 case elfcpp::R_MIPS_TLS_DTPREL64:
8910 case elfcpp::R_MIPS_TLS_TPREL64:
8913 case elfcpp::R_MIPS_32:
8914 case elfcpp::R_MIPS_TLS_DTPMOD32:
8915 case elfcpp::R_MIPS_TLS_DTPREL32:
8916 case elfcpp::R_MIPS_TLS_TPREL32:
8917 case elfcpp::R_MIPS_REL32:
8918 case elfcpp::R_MIPS_PC32:
8919 case elfcpp::R_MIPS_GPREL32:
8920 case elfcpp::R_MIPS_JALR:
8921 case elfcpp::R_MIPS_EH:
8924 case elfcpp::R_MIPS_16:
8925 case elfcpp::R_MIPS_HI16:
8926 case elfcpp::R_MIPS_LO16:
8927 case elfcpp::R_MIPS_GPREL16:
8928 case elfcpp::R_MIPS16_HI16:
8929 case elfcpp::R_MIPS16_LO16:
8930 case elfcpp::R_MIPS_PC16:
8931 case elfcpp::R_MIPS_GOT16:
8932 case elfcpp::R_MIPS16_GOT16:
8933 case elfcpp::R_MIPS_CALL16:
8934 case elfcpp::R_MIPS16_CALL16:
8935 case elfcpp::R_MIPS_GOT_HI16:
8936 case elfcpp::R_MIPS_CALL_HI16:
8937 case elfcpp::R_MIPS_GOT_LO16:
8938 case elfcpp::R_MIPS_CALL_LO16:
8939 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
8940 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
8941 case elfcpp::R_MIPS_TLS_TPREL_HI16:
8942 case elfcpp::R_MIPS_TLS_TPREL_LO16:
8943 case elfcpp::R_MIPS16_GPREL:
8944 case elfcpp::R_MIPS_GOT_DISP:
8945 case elfcpp::R_MIPS_LITERAL:
8946 case elfcpp::R_MIPS_GOT_PAGE:
8947 case elfcpp::R_MIPS_GOT_OFST:
8948 case elfcpp::R_MIPS_TLS_GD:
8949 case elfcpp::R_MIPS_TLS_LDM:
8950 case elfcpp::R_MIPS_TLS_GOTTPREL:
8953 // These relocations are not byte sized
8954 case elfcpp::R_MIPS_26:
8955 case elfcpp::R_MIPS16_26:
8958 case elfcpp::R_MIPS_COPY:
8959 case elfcpp::R_MIPS_JUMP_SLOT:
8960 object->error(_("unexpected reloc %u in object file"), r_type);
8964 object->error(_("unsupported reloc %u in object file"), r_type);
8969 // Scan the relocs during a relocatable link.
8971 template<int size, bool big_endian>
8973 Target_mips<size, big_endian>::scan_relocatable_relocs(
8974 Symbol_table* symtab,
8976 Sized_relobj_file<size, big_endian>* object,
8977 unsigned int data_shndx,
8978 unsigned int sh_type,
8979 const unsigned char* prelocs,
8981 Output_section* output_section,
8982 bool needs_special_offset_handling,
8983 size_t local_symbol_count,
8984 const unsigned char* plocal_symbols,
8985 Relocatable_relocs* rr)
8987 if (sh_type == elfcpp::SHT_REL)
8989 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8991 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
8992 Scan_relocatable_relocs;
8994 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
9002 needs_special_offset_handling,
9007 else if (sh_type == elfcpp::SHT_RELA)
9009 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9011 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
9012 Scan_relocatable_relocs;
9014 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
9022 needs_special_offset_handling,
9031 // Scan the relocs for --emit-relocs.
9033 template<int size, bool big_endian>
9035 Target_mips<size, big_endian>::emit_relocs_scan(
9036 Symbol_table* symtab,
9038 Sized_relobj_file<size, big_endian>* object,
9039 unsigned int data_shndx,
9040 unsigned int sh_type,
9041 const unsigned char* prelocs,
9043 Output_section* output_section,
9044 bool needs_special_offset_handling,
9045 size_t local_symbol_count,
9046 const unsigned char* plocal_syms,
9047 Relocatable_relocs* rr)
9049 if (sh_type == elfcpp::SHT_REL)
9051 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9053 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9054 Emit_relocs_strategy;
9056 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9064 needs_special_offset_handling,
9069 else if (sh_type == elfcpp::SHT_RELA)
9071 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9073 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9074 Emit_relocs_strategy;
9076 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9084 needs_special_offset_handling,
9093 // Emit relocations for a section.
9095 template<int size, bool big_endian>
9097 Target_mips<size, big_endian>::relocate_relocs(
9098 const Relocate_info<size, big_endian>* relinfo,
9099 unsigned int sh_type,
9100 const unsigned char* prelocs,
9102 Output_section* output_section,
9103 typename elfcpp::Elf_types<size>::Elf_Off
9104 offset_in_output_section,
9105 unsigned char* view,
9106 Mips_address view_address,
9107 section_size_type view_size,
9108 unsigned char* reloc_view,
9109 section_size_type reloc_view_size)
9111 if (sh_type == elfcpp::SHT_REL)
9113 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9116 gold::relocate_relocs<size, big_endian, Classify_reloc>(
9121 offset_in_output_section,
9128 else if (sh_type == elfcpp::SHT_RELA)
9130 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9133 gold::relocate_relocs<size, big_endian, Classify_reloc>(
9138 offset_in_output_section,
9149 // Perform target-specific processing in a relocatable link. This is
9150 // only used if we use the relocation strategy RELOC_SPECIAL.
9152 template<int size, bool big_endian>
9154 Target_mips<size, big_endian>::relocate_special_relocatable(
9155 const Relocate_info<size, big_endian>* relinfo,
9156 unsigned int sh_type,
9157 const unsigned char* preloc_in,
9159 Output_section* output_section,
9160 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
9161 unsigned char* view,
9162 Mips_address view_address,
9164 unsigned char* preloc_out)
9166 // We can only handle REL type relocation sections.
9167 gold_assert(sh_type == elfcpp::SHT_REL);
9169 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
9171 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
9174 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
9176 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
9178 Mips_relobj<size, big_endian>* object =
9179 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
9180 const unsigned int local_count = object->local_symbol_count();
9182 Reltype reloc(preloc_in);
9183 Reltype_write reloc_write(preloc_out);
9185 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
9186 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9187 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9189 // Get the new symbol index.
9190 // We only use RELOC_SPECIAL strategy in local relocations.
9191 gold_assert(r_sym < local_count);
9193 // We are adjusting a section symbol. We need to find
9194 // the symbol table index of the section symbol for
9195 // the output section corresponding to input section
9196 // in which this symbol is defined.
9198 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
9199 gold_assert(is_ordinary);
9200 Output_section* os = object->output_section(shndx);
9201 gold_assert(os != NULL);
9202 gold_assert(os->needs_symtab_index());
9203 unsigned int new_symndx = os->symtab_index();
9205 // Get the new offset--the location in the output section where
9206 // this relocation should be applied.
9208 Mips_address offset = reloc.get_r_offset();
9209 Mips_address new_offset;
9210 if (offset_in_output_section != invalid_address)
9211 new_offset = offset + offset_in_output_section;
9214 section_offset_type sot_offset =
9215 convert_types<section_offset_type, Mips_address>(offset);
9216 section_offset_type new_sot_offset =
9217 output_section->output_offset(object, relinfo->data_shndx,
9219 gold_assert(new_sot_offset != -1);
9220 new_offset = new_sot_offset;
9223 // In an object file, r_offset is an offset within the section.
9224 // In an executable or dynamic object, generated by
9225 // --emit-relocs, r_offset is an absolute address.
9226 if (!parameters->options().relocatable())
9228 new_offset += view_address;
9229 if (offset_in_output_section != invalid_address)
9230 new_offset -= offset_in_output_section;
9233 reloc_write.put_r_offset(new_offset);
9234 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
9236 // Handle the reloc addend.
9237 // The relocation uses a section symbol in the input file.
9238 // We are adjusting it to use a section symbol in the output
9239 // file. The input section symbol refers to some address in
9240 // the input section. We need the relocation in the output
9241 // file to refer to that same address. This adjustment to
9242 // the addend is the same calculation we use for a simple
9243 // absolute relocation for the input section symbol.
9244 Valtype calculated_value = 0;
9245 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
9247 unsigned char* paddend = view + offset;
9248 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
9251 case elfcpp::R_MIPS_26:
9252 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
9253 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
9254 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
9255 false, &calculated_value);
9262 // Report any errors.
9263 switch (reloc_status)
9265 case Reloc_funcs::STATUS_OKAY:
9267 case Reloc_funcs::STATUS_OVERFLOW:
9268 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9269 _("relocation overflow"));
9271 case Reloc_funcs::STATUS_BAD_RELOC:
9272 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9273 _("unexpected opcode while processing relocation"));
9280 // Optimize the TLS relocation type based on what we know about the
9281 // symbol. IS_FINAL is true if the final address of this symbol is
9282 // known at link time.
9284 template<int size, bool big_endian>
9285 tls::Tls_optimization
9286 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
9288 // FIXME: Currently we do not do any TLS optimization.
9289 return tls::TLSOPT_NONE;
9292 // Scan a relocation for a local symbol.
9294 template<int size, bool big_endian>
9296 Target_mips<size, big_endian>::Scan::local(
9297 Symbol_table* symtab,
9299 Target_mips<size, big_endian>* target,
9300 Sized_relobj_file<size, big_endian>* object,
9301 unsigned int data_shndx,
9302 Output_section* output_section,
9303 const Relatype* rela,
9305 unsigned int rel_type,
9306 unsigned int r_type,
9307 const elfcpp::Sym<size, big_endian>& lsym,
9313 Mips_address r_offset;
9315 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
9317 if (rel_type == elfcpp::SHT_RELA)
9319 r_offset = rela->get_r_offset();
9320 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
9322 r_addend = rela->get_r_addend();
9326 r_offset = rel->get_r_offset();
9327 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
9332 Mips_relobj<size, big_endian>* mips_obj =
9333 Mips_relobj<size, big_endian>::as_mips_relobj(object);
9335 if (mips_obj->is_mips16_stub_section(data_shndx))
9337 mips_obj->get_mips16_stub_section(data_shndx)
9338 ->new_local_reloc_found(r_type, r_sym);
9341 if (r_type == elfcpp::R_MIPS_NONE)
9342 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
9346 if (!mips16_call_reloc(r_type)
9347 && !mips_obj->section_allows_mips16_refs(data_shndx))
9348 // This reloc would need to refer to a MIPS16 hard-float stub, if
9349 // there is one. We ignore MIPS16 stub sections and .pdr section when
9350 // looking for relocs that would need to refer to MIPS16 stubs.
9351 mips_obj->add_local_non_16bit_call(r_sym);
9353 if (r_type == elfcpp::R_MIPS16_26
9354 && !mips_obj->section_allows_mips16_refs(data_shndx))
9355 mips_obj->add_local_16bit_call(r_sym);
9359 case elfcpp::R_MIPS_GOT16:
9360 case elfcpp::R_MIPS_CALL16:
9361 case elfcpp::R_MIPS_CALL_HI16:
9362 case elfcpp::R_MIPS_CALL_LO16:
9363 case elfcpp::R_MIPS_GOT_HI16:
9364 case elfcpp::R_MIPS_GOT_LO16:
9365 case elfcpp::R_MIPS_GOT_PAGE:
9366 case elfcpp::R_MIPS_GOT_OFST:
9367 case elfcpp::R_MIPS_GOT_DISP:
9368 case elfcpp::R_MIPS_TLS_GOTTPREL:
9369 case elfcpp::R_MIPS_TLS_GD:
9370 case elfcpp::R_MIPS_TLS_LDM:
9371 case elfcpp::R_MIPS16_GOT16:
9372 case elfcpp::R_MIPS16_CALL16:
9373 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9374 case elfcpp::R_MIPS16_TLS_GD:
9375 case elfcpp::R_MIPS16_TLS_LDM:
9376 case elfcpp::R_MICROMIPS_GOT16:
9377 case elfcpp::R_MICROMIPS_CALL16:
9378 case elfcpp::R_MICROMIPS_CALL_HI16:
9379 case elfcpp::R_MICROMIPS_CALL_LO16:
9380 case elfcpp::R_MICROMIPS_GOT_HI16:
9381 case elfcpp::R_MICROMIPS_GOT_LO16:
9382 case elfcpp::R_MICROMIPS_GOT_PAGE:
9383 case elfcpp::R_MICROMIPS_GOT_OFST:
9384 case elfcpp::R_MICROMIPS_GOT_DISP:
9385 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9386 case elfcpp::R_MICROMIPS_TLS_GD:
9387 case elfcpp::R_MICROMIPS_TLS_LDM:
9388 case elfcpp::R_MIPS_EH:
9389 // We need a GOT section.
9390 target->got_section(symtab, layout);
9397 if (call_lo16_reloc(r_type)
9398 || got_lo16_reloc(r_type)
9399 || got_disp_reloc(r_type)
9400 || eh_reloc(r_type))
9402 // We may need a local GOT entry for this relocation. We
9403 // don't count R_MIPS_GOT_PAGE because we can estimate the
9404 // maximum number of pages needed by looking at the size of
9405 // the segment. Similar comments apply to R_MIPS*_GOT16 and
9406 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
9407 // R_MIPS_CALL_HI16 because these are always followed by an
9408 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
9409 Mips_output_data_got<size, big_endian>* got =
9410 target->got_section(symtab, layout);
9411 bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
9412 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
9418 case elfcpp::R_MIPS_CALL16:
9419 case elfcpp::R_MIPS16_CALL16:
9420 case elfcpp::R_MICROMIPS_CALL16:
9421 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
9422 (unsigned long)r_offset);
9425 case elfcpp::R_MIPS_GOT_PAGE:
9426 case elfcpp::R_MICROMIPS_GOT_PAGE:
9427 case elfcpp::R_MIPS16_GOT16:
9428 case elfcpp::R_MIPS_GOT16:
9429 case elfcpp::R_MIPS_GOT_HI16:
9430 case elfcpp::R_MIPS_GOT_LO16:
9431 case elfcpp::R_MICROMIPS_GOT16:
9432 case elfcpp::R_MICROMIPS_GOT_HI16:
9433 case elfcpp::R_MICROMIPS_GOT_LO16:
9435 // This relocation needs a page entry in the GOT.
9436 // Get the section contents.
9437 section_size_type view_size = 0;
9438 const unsigned char* view = object->section_contents(data_shndx,
9442 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
9443 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
9446 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
9447 target->got16_addends_.push_back(got16_addend<size, big_endian>(
9448 object, data_shndx, r_type, r_sym, addend));
9450 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
9454 case elfcpp::R_MIPS_HI16:
9455 case elfcpp::R_MIPS16_HI16:
9456 case elfcpp::R_MICROMIPS_HI16:
9457 // Record the reloc so that we can check whether the corresponding LO16
9459 if (rel_type == elfcpp::SHT_REL)
9460 target->got16_addends_.push_back(got16_addend<size, big_endian>(
9461 object, data_shndx, r_type, r_sym, 0));
9464 case elfcpp::R_MIPS_LO16:
9465 case elfcpp::R_MIPS16_LO16:
9466 case elfcpp::R_MICROMIPS_LO16:
9468 if (rel_type != elfcpp::SHT_REL)
9471 // Find corresponding GOT16/HI16 relocation.
9473 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
9474 // be immediately following. However, for the IRIX6 ABI, the next
9475 // relocation may be a composed relocation consisting of several
9476 // relocations for the same address. In that case, the R_MIPS_LO16
9477 // relocation may occur as one of these. We permit a similar
9478 // extension in general, as that is useful for GCC.
9480 // In some cases GCC dead code elimination removes the LO16 but
9481 // keeps the corresponding HI16. This is strictly speaking a
9482 // violation of the ABI but not immediately harmful.
9484 typename std::list<got16_addend<size, big_endian> >::iterator it =
9485 target->got16_addends_.begin();
9486 while (it != target->got16_addends_.end())
9488 got16_addend<size, big_endian> _got16_addend = *it;
9490 // TODO(sasa): Split got16_addends_ list into two lists - one for
9491 // GOT16 relocs and the other for HI16 relocs.
9493 // Report an error if we find HI16 or GOT16 reloc from the
9494 // previous section without the matching LO16 part.
9495 if (_got16_addend.object != object
9496 || _got16_addend.shndx != data_shndx)
9498 gold_error("Can't find matching LO16 reloc");
9502 if (_got16_addend.r_sym != r_sym
9503 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
9509 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
9510 // For GOT16, we need to calculate combined addend and record GOT page
9512 if (got16_reloc(_got16_addend.r_type))
9515 section_size_type view_size = 0;
9516 const unsigned char* view = object->section_contents(data_shndx,
9521 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
9522 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
9524 addend = (_got16_addend.addend << 16) + addend;
9525 target->got_section()->record_got_page_entry(mips_obj, r_sym,
9529 it = target->got16_addends_.erase(it);
9537 case elfcpp::R_MIPS_32:
9538 case elfcpp::R_MIPS_REL32:
9539 case elfcpp::R_MIPS_64:
9541 if (parameters->options().output_is_position_independent())
9543 // If building a shared library (or a position-independent
9544 // executable), we need to create a dynamic relocation for
9546 if (is_readonly_section(output_section))
9548 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9549 rel_dyn->add_symbolless_local_addend(object, r_sym,
9550 elfcpp::R_MIPS_REL32,
9551 output_section, data_shndx,
9557 case elfcpp::R_MIPS_TLS_GOTTPREL:
9558 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9559 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9560 case elfcpp::R_MIPS_TLS_LDM:
9561 case elfcpp::R_MIPS16_TLS_LDM:
9562 case elfcpp::R_MICROMIPS_TLS_LDM:
9563 case elfcpp::R_MIPS_TLS_GD:
9564 case elfcpp::R_MIPS16_TLS_GD:
9565 case elfcpp::R_MICROMIPS_TLS_GD:
9567 bool output_is_shared = parameters->options().shared();
9568 const tls::Tls_optimization optimized_type
9569 = Target_mips<size, big_endian>::optimize_tls_reloc(
9570 !output_is_shared, r_type);
9573 case elfcpp::R_MIPS_TLS_GD:
9574 case elfcpp::R_MIPS16_TLS_GD:
9575 case elfcpp::R_MICROMIPS_TLS_GD:
9576 if (optimized_type == tls::TLSOPT_NONE)
9578 // Create a pair of GOT entries for the module index and
9579 // dtv-relative offset.
9580 Mips_output_data_got<size, big_endian>* got =
9581 target->got_section(symtab, layout);
9582 unsigned int shndx = lsym.get_st_shndx();
9584 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
9587 object->error(_("local symbol %u has bad shndx %u"),
9591 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
9596 // FIXME: TLS optimization not supported yet.
9601 case elfcpp::R_MIPS_TLS_LDM:
9602 case elfcpp::R_MIPS16_TLS_LDM:
9603 case elfcpp::R_MICROMIPS_TLS_LDM:
9604 if (optimized_type == tls::TLSOPT_NONE)
9606 // We always record LDM symbols as local with index 0.
9607 target->got_section()->record_local_got_symbol(mips_obj, 0,
9613 // FIXME: TLS optimization not supported yet.
9617 case elfcpp::R_MIPS_TLS_GOTTPREL:
9618 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9619 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9620 layout->set_has_static_tls();
9621 if (optimized_type == tls::TLSOPT_NONE)
9623 // Create a GOT entry for the tp-relative offset.
9624 Mips_output_data_got<size, big_endian>* got =
9625 target->got_section(symtab, layout);
9626 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
9631 // FIXME: TLS optimization not supported yet.
9646 // Refuse some position-dependent relocations when creating a
9647 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9648 // not PIC, but we can create dynamic relocations and the result
9649 // will be fine. Also do not refuse R_MIPS_LO16, which can be
9650 // combined with R_MIPS_GOT16.
9651 if (parameters->options().shared())
9655 case elfcpp::R_MIPS16_HI16:
9656 case elfcpp::R_MIPS_HI16:
9657 case elfcpp::R_MICROMIPS_HI16:
9658 // Don't refuse a high part relocation if it's against
9659 // no symbol (e.g. part of a compound relocation).
9665 case elfcpp::R_MIPS16_26:
9666 case elfcpp::R_MIPS_26:
9667 case elfcpp::R_MICROMIPS_26_S1:
9668 gold_error(_("%s: relocation %u against `%s' can not be used when "
9669 "making a shared object; recompile with -fPIC"),
9670 object->name().c_str(), r_type, "a local symbol");
9677 template<int size, bool big_endian>
9679 Target_mips<size, big_endian>::Scan::local(
9680 Symbol_table* symtab,
9682 Target_mips<size, big_endian>* target,
9683 Sized_relobj_file<size, big_endian>* object,
9684 unsigned int data_shndx,
9685 Output_section* output_section,
9686 const Reltype& reloc,
9687 unsigned int r_type,
9688 const elfcpp::Sym<size, big_endian>& lsym,
9701 (const Relatype*) NULL,
9705 lsym, is_discarded);
9709 template<int size, bool big_endian>
9711 Target_mips<size, big_endian>::Scan::local(
9712 Symbol_table* symtab,
9714 Target_mips<size, big_endian>* target,
9715 Sized_relobj_file<size, big_endian>* object,
9716 unsigned int data_shndx,
9717 Output_section* output_section,
9718 const Relatype& reloc,
9719 unsigned int r_type,
9720 const elfcpp::Sym<size, big_endian>& lsym,
9734 (const Reltype*) NULL,
9737 lsym, is_discarded);
9740 // Scan a relocation for a global symbol.
9742 template<int size, bool big_endian>
9744 Target_mips<size, big_endian>::Scan::global(
9745 Symbol_table* symtab,
9747 Target_mips<size, big_endian>* target,
9748 Sized_relobj_file<size, big_endian>* object,
9749 unsigned int data_shndx,
9750 Output_section* output_section,
9751 const Relatype* rela,
9753 unsigned int rel_type,
9754 unsigned int r_type,
9757 Mips_address r_offset;
9759 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
9761 if (rel_type == elfcpp::SHT_RELA)
9763 r_offset = rela->get_r_offset();
9764 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
9766 r_addend = rela->get_r_addend();
9770 r_offset = rel->get_r_offset();
9771 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
9776 Mips_relobj<size, big_endian>* mips_obj =
9777 Mips_relobj<size, big_endian>::as_mips_relobj(object);
9778 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
9780 if (mips_obj->is_mips16_stub_section(data_shndx))
9782 mips_obj->get_mips16_stub_section(data_shndx)
9783 ->new_global_reloc_found(r_type, mips_sym);
9786 if (r_type == elfcpp::R_MIPS_NONE)
9787 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
9791 if (!mips16_call_reloc(r_type)
9792 && !mips_obj->section_allows_mips16_refs(data_shndx))
9793 // This reloc would need to refer to a MIPS16 hard-float stub, if
9794 // there is one. We ignore MIPS16 stub sections and .pdr section when
9795 // looking for relocs that would need to refer to MIPS16 stubs.
9796 mips_sym->set_need_fn_stub();
9798 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
9799 // section. We check here to avoid creating a dynamic reloc against
9800 // _GLOBAL_OFFSET_TABLE_.
9801 if (!target->has_got_section()
9802 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9803 target->got_section(symtab, layout);
9805 // We need PLT entries if there are static-only relocations against
9806 // an externally-defined function. This can technically occur for
9807 // shared libraries if there are branches to the symbol, although it
9808 // is unlikely that this will be used in practice due to the short
9809 // ranges involved. It can occur for any relative or absolute relocation
9810 // in executables; in that case, the PLT entry becomes the function's
9811 // canonical address.
9812 bool static_reloc = false;
9814 // Set CAN_MAKE_DYNAMIC to true if we can convert this
9815 // relocation into a dynamic one.
9816 bool can_make_dynamic = false;
9819 case elfcpp::R_MIPS_GOT16:
9820 case elfcpp::R_MIPS_CALL16:
9821 case elfcpp::R_MIPS_CALL_HI16:
9822 case elfcpp::R_MIPS_CALL_LO16:
9823 case elfcpp::R_MIPS_GOT_HI16:
9824 case elfcpp::R_MIPS_GOT_LO16:
9825 case elfcpp::R_MIPS_GOT_PAGE:
9826 case elfcpp::R_MIPS_GOT_OFST:
9827 case elfcpp::R_MIPS_GOT_DISP:
9828 case elfcpp::R_MIPS_TLS_GOTTPREL:
9829 case elfcpp::R_MIPS_TLS_GD:
9830 case elfcpp::R_MIPS_TLS_LDM:
9831 case elfcpp::R_MIPS16_GOT16:
9832 case elfcpp::R_MIPS16_CALL16:
9833 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9834 case elfcpp::R_MIPS16_TLS_GD:
9835 case elfcpp::R_MIPS16_TLS_LDM:
9836 case elfcpp::R_MICROMIPS_GOT16:
9837 case elfcpp::R_MICROMIPS_CALL16:
9838 case elfcpp::R_MICROMIPS_CALL_HI16:
9839 case elfcpp::R_MICROMIPS_CALL_LO16:
9840 case elfcpp::R_MICROMIPS_GOT_HI16:
9841 case elfcpp::R_MICROMIPS_GOT_LO16:
9842 case elfcpp::R_MICROMIPS_GOT_PAGE:
9843 case elfcpp::R_MICROMIPS_GOT_OFST:
9844 case elfcpp::R_MICROMIPS_GOT_DISP:
9845 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9846 case elfcpp::R_MICROMIPS_TLS_GD:
9847 case elfcpp::R_MICROMIPS_TLS_LDM:
9848 case elfcpp::R_MIPS_EH:
9849 // We need a GOT section.
9850 target->got_section(symtab, layout);
9853 // This is just a hint; it can safely be ignored. Don't set
9854 // has_static_relocs for the corresponding symbol.
9855 case elfcpp::R_MIPS_JALR:
9856 case elfcpp::R_MICROMIPS_JALR:
9859 case elfcpp::R_MIPS_GPREL16:
9860 case elfcpp::R_MIPS_GPREL32:
9861 case elfcpp::R_MIPS16_GPREL:
9862 case elfcpp::R_MICROMIPS_GPREL16:
9864 // GP-relative relocations always resolve to a definition in a
9865 // regular input file, ignoring the one-definition rule. This is
9866 // important for the GP setup sequence in NewABI code, which
9867 // always resolves to a local function even if other relocations
9868 // against the symbol wouldn't.
9869 //constrain_symbol_p = FALSE;
9872 case elfcpp::R_MIPS_32:
9873 case elfcpp::R_MIPS_REL32:
9874 case elfcpp::R_MIPS_64:
9875 if ((parameters->options().shared()
9876 || (strcmp(gsym->name(), "__gnu_local_gp") != 0
9877 && (!is_readonly_section(output_section)
9878 || mips_obj->is_pic())))
9879 && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
9881 if (r_type != elfcpp::R_MIPS_REL32)
9882 mips_sym->set_pointer_equality_needed();
9883 can_make_dynamic = true;
9889 // Most static relocations require pointer equality, except
9891 mips_sym->set_pointer_equality_needed();
9895 case elfcpp::R_MIPS_26:
9896 case elfcpp::R_MIPS_PC16:
9897 case elfcpp::R_MIPS16_26:
9898 case elfcpp::R_MICROMIPS_26_S1:
9899 case elfcpp::R_MICROMIPS_PC7_S1:
9900 case elfcpp::R_MICROMIPS_PC10_S1:
9901 case elfcpp::R_MICROMIPS_PC16_S1:
9902 case elfcpp::R_MICROMIPS_PC23_S2:
9903 static_reloc = true;
9904 mips_sym->set_has_static_relocs();
9908 // If there are call relocations against an externally-defined symbol,
9909 // see whether we can create a MIPS lazy-binding stub for it. We can
9910 // only do this if all references to the function are through call
9911 // relocations, and in that case, the traditional lazy-binding stubs
9912 // are much more efficient than PLT entries.
9915 case elfcpp::R_MIPS16_CALL16:
9916 case elfcpp::R_MIPS_CALL16:
9917 case elfcpp::R_MIPS_CALL_HI16:
9918 case elfcpp::R_MIPS_CALL_LO16:
9919 case elfcpp::R_MIPS_JALR:
9920 case elfcpp::R_MICROMIPS_CALL16:
9921 case elfcpp::R_MICROMIPS_CALL_HI16:
9922 case elfcpp::R_MICROMIPS_CALL_LO16:
9923 case elfcpp::R_MICROMIPS_JALR:
9924 if (!mips_sym->no_lazy_stub())
9926 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
9927 // Calls from shared objects to undefined symbols of type
9928 // STT_NOTYPE need lazy-binding stub.
9929 || (mips_sym->is_undefined() && parameters->options().shared()))
9930 target->mips_stubs_section(layout)->make_entry(mips_sym);
9935 // We must not create a stub for a symbol that has relocations
9936 // related to taking the function's address.
9937 mips_sym->set_no_lazy_stub();
9938 target->remove_lazy_stub_entry(mips_sym);
9943 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
9944 mips_sym->is_mips16()))
9945 mips_sym->set_has_nonpic_branches();
9947 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
9948 // and has a special meaning.
9949 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
9950 && strcmp(gsym->name(), "_gp_disp") == 0
9951 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
9952 if (static_reloc && gsym->needs_plt_entry())
9954 target->make_plt_entry(symtab, layout, mips_sym, r_type);
9956 // Since this is not a PC-relative relocation, we may be
9957 // taking the address of a function. In that case we need to
9958 // set the entry in the dynamic symbol table to the address of
9960 if (gsym->is_from_dynobj() && !parameters->options().shared())
9962 gsym->set_needs_dynsym_value();
9963 // We distinguish between PLT entries and lazy-binding stubs by
9964 // giving the former an st_other value of STO_MIPS_PLT. Set the
9965 // flag if there are any relocations in the binary where pointer
9966 // equality matters.
9967 if (mips_sym->pointer_equality_needed())
9968 mips_sym->set_mips_plt();
9971 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
9973 // Absolute addressing relocations.
9974 // Make a dynamic relocation if necessary.
9975 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
9977 if (gsym->may_need_copy_reloc())
9979 target->copy_reloc(symtab, layout, object, data_shndx,
9980 output_section, gsym, r_type, r_offset);
9982 else if (can_make_dynamic)
9984 // Create .rel.dyn section.
9985 target->rel_dyn_section(layout);
9986 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
9987 data_shndx, output_section, r_offset);
9990 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
9995 bool for_call = false;
9998 case elfcpp::R_MIPS_CALL16:
9999 case elfcpp::R_MIPS16_CALL16:
10000 case elfcpp::R_MICROMIPS_CALL16:
10001 case elfcpp::R_MIPS_CALL_HI16:
10002 case elfcpp::R_MIPS_CALL_LO16:
10003 case elfcpp::R_MICROMIPS_CALL_HI16:
10004 case elfcpp::R_MICROMIPS_CALL_LO16:
10008 case elfcpp::R_MIPS16_GOT16:
10009 case elfcpp::R_MIPS_GOT16:
10010 case elfcpp::R_MIPS_GOT_HI16:
10011 case elfcpp::R_MIPS_GOT_LO16:
10012 case elfcpp::R_MICROMIPS_GOT16:
10013 case elfcpp::R_MICROMIPS_GOT_HI16:
10014 case elfcpp::R_MICROMIPS_GOT_LO16:
10015 case elfcpp::R_MIPS_GOT_DISP:
10016 case elfcpp::R_MICROMIPS_GOT_DISP:
10017 case elfcpp::R_MIPS_EH:
10019 // The symbol requires a GOT entry.
10020 Mips_output_data_got<size, big_endian>* got =
10021 target->got_section(symtab, layout);
10022 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10024 mips_sym->set_global_got_area(GGA_NORMAL);
10028 case elfcpp::R_MIPS_GOT_PAGE:
10029 case elfcpp::R_MICROMIPS_GOT_PAGE:
10031 // This relocation needs a page entry in the GOT.
10032 // Get the section contents.
10033 section_size_type view_size = 0;
10034 const unsigned char* view =
10035 object->section_contents(data_shndx, &view_size, false);
10038 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10039 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10041 Mips_output_data_got<size, big_endian>* got =
10042 target->got_section(symtab, layout);
10043 got->record_got_page_entry(mips_obj, r_sym, addend);
10045 // If this is a global, overridable symbol, GOT_PAGE will
10046 // decay to GOT_DISP, so we'll need a GOT entry for it.
10047 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
10048 && !mips_sym->object()->is_dynamic()
10049 && !mips_sym->is_undefined());
10051 || (parameters->options().output_is_position_independent()
10052 && !parameters->options().Bsymbolic()
10053 && !mips_sym->is_forced_local()))
10055 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10057 mips_sym->set_global_got_area(GGA_NORMAL);
10062 case elfcpp::R_MIPS_TLS_GOTTPREL:
10063 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10064 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10065 case elfcpp::R_MIPS_TLS_LDM:
10066 case elfcpp::R_MIPS16_TLS_LDM:
10067 case elfcpp::R_MICROMIPS_TLS_LDM:
10068 case elfcpp::R_MIPS_TLS_GD:
10069 case elfcpp::R_MIPS16_TLS_GD:
10070 case elfcpp::R_MICROMIPS_TLS_GD:
10072 const bool is_final = gsym->final_value_is_known();
10073 const tls::Tls_optimization optimized_type =
10074 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
10078 case elfcpp::R_MIPS_TLS_GD:
10079 case elfcpp::R_MIPS16_TLS_GD:
10080 case elfcpp::R_MICROMIPS_TLS_GD:
10081 if (optimized_type == tls::TLSOPT_NONE)
10083 // Create a pair of GOT entries for the module index and
10084 // dtv-relative offset.
10085 Mips_output_data_got<size, big_endian>* got =
10086 target->got_section(symtab, layout);
10087 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10092 // FIXME: TLS optimization not supported yet.
10093 gold_unreachable();
10097 case elfcpp::R_MIPS_TLS_LDM:
10098 case elfcpp::R_MIPS16_TLS_LDM:
10099 case elfcpp::R_MICROMIPS_TLS_LDM:
10100 if (optimized_type == tls::TLSOPT_NONE)
10102 // We always record LDM symbols as local with index 0.
10103 target->got_section()->record_local_got_symbol(mips_obj, 0,
10109 // FIXME: TLS optimization not supported yet.
10110 gold_unreachable();
10113 case elfcpp::R_MIPS_TLS_GOTTPREL:
10114 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10115 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10116 layout->set_has_static_tls();
10117 if (optimized_type == tls::TLSOPT_NONE)
10119 // Create a GOT entry for the tp-relative offset.
10120 Mips_output_data_got<size, big_endian>* got =
10121 target->got_section(symtab, layout);
10122 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10127 // FIXME: TLS optimization not supported yet.
10128 gold_unreachable();
10133 gold_unreachable();
10137 case elfcpp::R_MIPS_COPY:
10138 case elfcpp::R_MIPS_JUMP_SLOT:
10139 // These are relocations which should only be seen by the
10140 // dynamic linker, and should never be seen here.
10141 gold_error(_("%s: unexpected reloc %u in object file"),
10142 object->name().c_str(), r_type);
10149 // Refuse some position-dependent relocations when creating a
10150 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10151 // not PIC, but we can create dynamic relocations and the result
10152 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10153 // combined with R_MIPS_GOT16.
10154 if (parameters->options().shared())
10158 case elfcpp::R_MIPS16_HI16:
10159 case elfcpp::R_MIPS_HI16:
10160 case elfcpp::R_MICROMIPS_HI16:
10161 // Don't refuse a high part relocation if it's against
10162 // no symbol (e.g. part of a compound relocation).
10166 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
10167 // and has a special meaning.
10168 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
10173 case elfcpp::R_MIPS16_26:
10174 case elfcpp::R_MIPS_26:
10175 case elfcpp::R_MICROMIPS_26_S1:
10176 gold_error(_("%s: relocation %u against `%s' can not be used when "
10177 "making a shared object; recompile with -fPIC"),
10178 object->name().c_str(), r_type, gsym->name());
10185 template<int size, bool big_endian>
10187 Target_mips<size, big_endian>::Scan::global(
10188 Symbol_table* symtab,
10190 Target_mips<size, big_endian>* target,
10191 Sized_relobj_file<size, big_endian>* object,
10192 unsigned int data_shndx,
10193 Output_section* output_section,
10194 const Relatype& reloc,
10195 unsigned int r_type,
10206 (const Reltype*) NULL,
10212 template<int size, bool big_endian>
10214 Target_mips<size, big_endian>::Scan::global(
10215 Symbol_table* symtab,
10217 Target_mips<size, big_endian>* target,
10218 Sized_relobj_file<size, big_endian>* object,
10219 unsigned int data_shndx,
10220 Output_section* output_section,
10221 const Reltype& reloc,
10222 unsigned int r_type,
10232 (const Relatype*) NULL,
10239 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
10240 // In cases where Scan::local() or Scan::global() has created
10241 // a dynamic relocation, the addend of the relocation is carried
10242 // in the data, and we must not apply the static relocation.
10244 template<int size, bool big_endian>
10246 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
10247 const Mips_symbol<size>* gsym,
10248 unsigned int r_type,
10249 Output_section* output_section,
10250 Target_mips* target)
10252 // If the output section is not allocated, then we didn't call
10253 // scan_relocs, we didn't create a dynamic reloc, and we must apply
10255 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
10262 // For global symbols, we use the same helper routines used in the
10264 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
10265 && !gsym->may_need_copy_reloc())
10267 // We have generated dynamic reloc (R_MIPS_REL32).
10269 bool multi_got = false;
10270 if (target->has_got_section())
10271 multi_got = target->got_section()->multi_got();
10272 bool has_got_offset;
10274 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
10276 has_got_offset = gsym->global_gotoffset() != -1U;
10277 if (!has_got_offset)
10280 // Apply the relocation only if the symbol is in the local got.
10281 // Do not apply the relocation if the symbol is in the global
10283 return symbol_references_local(gsym, gsym->has_dynsym_index());
10286 // We have not generated dynamic reloc.
10291 // Perform a relocation.
10293 template<int size, bool big_endian>
10295 Target_mips<size, big_endian>::Relocate::relocate(
10296 const Relocate_info<size, big_endian>* relinfo,
10297 unsigned int rel_type,
10298 Target_mips* target,
10299 Output_section* output_section,
10301 const unsigned char* preloc,
10302 const Sized_symbol<size>* gsym,
10303 const Symbol_value<size>* psymval,
10304 unsigned char* view,
10305 Mips_address address,
10308 Mips_address r_offset;
10309 unsigned int r_sym;
10310 unsigned int r_type;
10311 unsigned int r_type2;
10312 unsigned int r_type3;
10313 unsigned char r_ssym;
10314 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10316 if (rel_type == elfcpp::SHT_RELA)
10318 const Relatype rela(preloc);
10319 r_offset = rela.get_r_offset();
10320 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10322 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10324 r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10325 get_r_type2(&rela);
10326 r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10327 get_r_type3(&rela);
10328 r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10330 r_addend = rela.get_r_addend();
10334 const Reltype rel(preloc);
10335 r_offset = rel.get_r_offset();
10336 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10338 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10346 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10347 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10349 Mips_relobj<size, big_endian>* object =
10350 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10352 bool target_is_16_bit_code = false;
10353 bool target_is_micromips_code = false;
10354 bool cross_mode_jump;
10356 Symbol_value<size> symval;
10358 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10360 bool changed_symbol_value = false;
10363 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
10364 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
10365 if (target_is_16_bit_code || target_is_micromips_code)
10367 // MIPS16/microMIPS text labels should be treated as odd.
10368 symval.set_output_value(psymval->value(object, 1));
10370 changed_symbol_value = true;
10375 target_is_16_bit_code = mips_sym->is_mips16();
10376 target_is_micromips_code = mips_sym->is_micromips();
10378 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
10379 // it odd. This will cause something like .word SYM to come up with
10380 // the right value when it is loaded into the PC.
10382 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
10383 && psymval->value(object, 0) != 0)
10385 symval.set_output_value(psymval->value(object, 0) | 1);
10387 changed_symbol_value = true;
10390 // Pick the value to use for symbols defined in shared objects.
10391 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
10392 || mips_sym->has_lazy_stub())
10394 Mips_address value;
10395 if (!mips_sym->has_lazy_stub())
10397 // Prefer a standard MIPS PLT entry.
10398 if (mips_sym->has_mips_plt_offset())
10400 value = target->plt_section()->mips_entry_address(mips_sym);
10401 target_is_micromips_code = false;
10402 target_is_16_bit_code = false;
10406 value = (target->plt_section()->comp_entry_address(mips_sym)
10408 if (target->is_output_micromips())
10409 target_is_micromips_code = true;
10411 target_is_16_bit_code = true;
10415 value = target->mips_stubs_section()->stub_address(mips_sym);
10417 symval.set_output_value(value);
10422 // TRUE if the symbol referred to by this relocation is "_gp_disp".
10423 // Note that such a symbol must always be a global symbol.
10424 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
10425 && !object->is_newabi());
10427 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
10428 // Note that such a symbol must always be a global symbol.
10429 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
10434 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
10435 gold_error_at_location(relinfo, relnum, r_offset,
10436 _("relocations against _gp_disp are permitted only"
10437 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
10439 else if (gnu_local_gp)
10441 // __gnu_local_gp is _gp symbol.
10442 symval.set_output_value(target->adjusted_gp_value(object));
10446 // If this is a reference to a 16-bit function with a stub, we need
10447 // to redirect the relocation to the stub unless:
10449 // (a) the relocation is for a MIPS16 JAL;
10451 // (b) the relocation is for a MIPS16 PIC call, and there are no
10452 // non-MIPS16 uses of the GOT slot; or
10454 // (c) the section allows direct references to MIPS16 functions.
10455 if (r_type != elfcpp::R_MIPS16_26
10456 && !parameters->options().relocatable()
10457 && ((mips_sym != NULL
10458 && mips_sym->has_mips16_fn_stub()
10459 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
10460 || (mips_sym == NULL
10461 && object->get_local_mips16_fn_stub(r_sym) != NULL))
10462 && !object->section_allows_mips16_refs(relinfo->data_shndx))
10464 // This is a 32- or 64-bit call to a 16-bit function. We should
10465 // have already noticed that we were going to need the
10467 Mips_address value;
10468 if (mips_sym == NULL)
10469 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
10472 gold_assert(mips_sym->need_fn_stub());
10473 if (mips_sym->has_la25_stub())
10474 value = target->la25_stub_section()->stub_address(mips_sym);
10477 value = mips_sym->template
10478 get_mips16_fn_stub<big_endian>()->output_address();
10481 symval.set_output_value(value);
10483 changed_symbol_value = true;
10485 // The target is 16-bit, but the stub isn't.
10486 target_is_16_bit_code = false;
10488 // If this is a MIPS16 call with a stub, that is made through the PLT or
10489 // to a standard MIPS function, we need to redirect the call to the stub.
10490 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
10491 // indirect calls should use an indirect stub instead.
10492 else if (r_type == elfcpp::R_MIPS16_26 && !parameters->options().relocatable()
10493 && ((mips_sym != NULL
10494 && (mips_sym->has_mips16_call_stub()
10495 || mips_sym->has_mips16_call_fp_stub()))
10496 || (mips_sym == NULL
10497 && object->get_local_mips16_call_stub(r_sym) != NULL))
10498 && ((mips_sym != NULL && mips_sym->has_plt_offset())
10499 || !target_is_16_bit_code))
10501 Mips16_stub_section<size, big_endian>* call_stub;
10502 if (mips_sym == NULL)
10503 call_stub = object->get_local_mips16_call_stub(r_sym);
10506 // If both call_stub and call_fp_stub are defined, we can figure
10507 // out which one to use by checking which one appears in the input
10509 if (mips_sym->has_mips16_call_stub()
10510 && mips_sym->has_mips16_call_fp_stub())
10513 for (unsigned int i = 1; i < object->shnum(); ++i)
10515 if (object->is_mips16_call_fp_stub_section(i))
10517 call_stub = mips_sym->template
10518 get_mips16_call_fp_stub<big_endian>();
10523 if (call_stub == NULL)
10525 mips_sym->template get_mips16_call_stub<big_endian>();
10527 else if (mips_sym->has_mips16_call_stub())
10528 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
10530 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
10533 symval.set_output_value(call_stub->output_address());
10535 changed_symbol_value = true;
10537 // If this is a direct call to a PIC function, redirect to the
10539 else if (mips_sym != NULL
10540 && mips_sym->has_la25_stub()
10541 && relocation_needs_la25_stub<size, big_endian>(
10542 object, r_type, target_is_16_bit_code))
10544 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
10545 if (mips_sym->is_micromips())
10547 symval.set_output_value(value);
10550 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
10551 // entry is used if a standard PLT entry has also been made.
10552 else if ((r_type == elfcpp::R_MIPS16_26
10553 || r_type == elfcpp::R_MICROMIPS_26_S1)
10554 && !parameters->options().relocatable()
10555 && mips_sym != NULL
10556 && mips_sym->has_plt_offset()
10557 && mips_sym->has_comp_plt_offset()
10558 && mips_sym->has_mips_plt_offset())
10560 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
10562 symval.set_output_value(value);
10565 target_is_16_bit_code = !target->is_output_micromips();
10566 target_is_micromips_code = target->is_output_micromips();
10569 // Make sure MIPS16 and microMIPS are not used together.
10570 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
10571 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
10573 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
10576 // Calls from 16-bit code to 32-bit code and vice versa require the
10577 // mode change. However, we can ignore calls to undefined weak symbols,
10578 // which should never be executed at runtime. This exception is important
10579 // because the assembly writer may have "known" that any definition of the
10580 // symbol would be 16-bit code, and that direct jumps were therefore
10583 (!parameters->options().relocatable()
10584 && !(gsym != NULL && gsym->is_weak_undefined())
10585 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
10586 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
10587 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
10588 && (target_is_16_bit_code || target_is_micromips_code))));
10590 bool local = (mips_sym == NULL
10591 || (mips_sym->got_only_for_calls()
10592 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
10593 : symbol_references_local(mips_sym,
10594 mips_sym->has_dynsym_index())));
10596 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
10597 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
10598 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
10599 if (got_page_reloc(r_type) && !local)
10600 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
10601 : elfcpp::R_MIPS_GOT_DISP);
10603 unsigned int got_offset = 0;
10606 bool calculate_only = false;
10607 Valtype calculated_value = 0;
10608 bool extract_addend = rel_type == elfcpp::SHT_REL;
10609 unsigned int r_types[3] = { r_type, r_type2, r_type3 };
10611 Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
10613 // For Mips64 N64 ABI, there may be up to three operations specified per
10614 // record, by the fields r_type, r_type2, and r_type3. The first operation
10615 // takes its addend from the relocation record. Each subsequent operation
10616 // takes as its addend the result of the previous operation.
10617 // The first operation in a record which references a symbol uses the symbol
10618 // implied by r_sym. The next operation in a record which references a symbol
10619 // uses the special symbol value given by the r_ssym field. A third operation
10620 // in a record which references a symbol will assume a NULL symbol,
10621 // i.e. value zero.
10624 // Check if a record references to a symbol.
10625 for (unsigned int i = 0; i < 3; ++i)
10627 if (r_types[i] == elfcpp::R_MIPS_NONE)
10631 // Check if the next relocation is for the same instruction.
10632 calculate_only = i == 2 ? false
10633 : r_types[i+1] != elfcpp::R_MIPS_NONE;
10635 if (object->is_n64())
10639 // Handle special symbol for r_type2 relocation type.
10643 symval.set_output_value(0);
10646 symval.set_output_value(target->gp_value());
10649 symval.set_output_value(object->gp_value());
10652 symval.set_output_value(address);
10655 gold_unreachable();
10661 // For r_type3 symbol value is 0.
10662 symval.set_output_value(0);
10666 bool update_got_entry = false;
10667 switch (r_types[i])
10669 case elfcpp::R_MIPS_NONE:
10671 case elfcpp::R_MIPS_16:
10672 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
10673 extract_addend, calculate_only,
10674 &calculated_value);
10677 case elfcpp::R_MIPS_32:
10678 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
10680 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
10681 extract_addend, calculate_only,
10682 &calculated_value);
10683 if (mips_sym != NULL
10684 && (mips_sym->is_mips16() || mips_sym->is_micromips())
10685 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
10687 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
10688 // already updated by adding +1.
10689 if (mips_sym->has_mips16_fn_stub())
10691 gold_assert(mips_sym->need_fn_stub());
10692 Mips16_stub_section<size, big_endian>* fn_stub =
10693 mips_sym->template get_mips16_fn_stub<big_endian>();
10695 symval.set_output_value(fn_stub->output_address());
10698 got_offset = mips_sym->global_gotoffset();
10699 update_got_entry = true;
10703 case elfcpp::R_MIPS_64:
10704 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
10706 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
10707 extract_addend, calculate_only,
10708 &calculated_value, false);
10709 else if (target->is_output_n64() && r_addend != 0)
10710 // Only apply the addend. The static relocation was RELA, but the
10711 // dynamic relocation is REL, so we need to apply the addend.
10712 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
10713 extract_addend, calculate_only,
10714 &calculated_value, true);
10716 case elfcpp::R_MIPS_REL32:
10717 gold_unreachable();
10719 case elfcpp::R_MIPS_PC32:
10720 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
10721 r_addend, extract_addend,
10723 &calculated_value);
10726 case elfcpp::R_MIPS16_26:
10727 // The calculation for R_MIPS16_26 is just the same as for an
10728 // R_MIPS_26. It's only the storage of the relocated field into
10729 // the output file that's different. So, we just fall through to the
10730 // R_MIPS_26 case here.
10731 case elfcpp::R_MIPS_26:
10732 case elfcpp::R_MICROMIPS_26_S1:
10733 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
10734 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
10735 r_types[i], target->jal_to_bal(), calculate_only,
10736 &calculated_value);
10739 case elfcpp::R_MIPS_HI16:
10740 case elfcpp::R_MIPS16_HI16:
10741 case elfcpp::R_MICROMIPS_HI16:
10742 if (rel_type == elfcpp::SHT_RELA)
10743 reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
10745 gp_disp, r_types[i],
10747 target, calculate_only,
10748 &calculated_value);
10749 else if (rel_type == elfcpp::SHT_REL)
10750 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
10751 address, gp_disp, r_types[i],
10752 r_sym, extract_addend);
10754 gold_unreachable();
10757 case elfcpp::R_MIPS_LO16:
10758 case elfcpp::R_MIPS16_LO16:
10759 case elfcpp::R_MICROMIPS_LO16:
10760 case elfcpp::R_MICROMIPS_HI0_LO16:
10761 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
10762 r_addend, extract_addend, address,
10763 gp_disp, r_types[i], r_sym,
10764 rel_type, calculate_only,
10765 &calculated_value);
10768 case elfcpp::R_MIPS_LITERAL:
10769 case elfcpp::R_MICROMIPS_LITERAL:
10770 // Because we don't merge literal sections, we can handle this
10771 // just like R_MIPS_GPREL16. In the long run, we should merge
10772 // shared literals, and then we will need to additional work
10777 case elfcpp::R_MIPS_GPREL16:
10778 case elfcpp::R_MIPS16_GPREL:
10779 case elfcpp::R_MICROMIPS_GPREL7_S2:
10780 case elfcpp::R_MICROMIPS_GPREL16:
10781 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
10782 target->adjusted_gp_value(object),
10783 r_addend, extract_addend,
10784 gsym == NULL, r_types[i],
10785 calculate_only, &calculated_value);
10788 case elfcpp::R_MIPS_PC16:
10789 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
10790 r_addend, extract_addend,
10792 &calculated_value);
10794 case elfcpp::R_MICROMIPS_PC7_S1:
10795 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
10799 &calculated_value);
10801 case elfcpp::R_MICROMIPS_PC10_S1:
10802 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
10804 r_addend, extract_addend,
10806 &calculated_value);
10808 case elfcpp::R_MICROMIPS_PC16_S1:
10809 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
10811 r_addend, extract_addend,
10813 &calculated_value);
10815 case elfcpp::R_MIPS_GPREL32:
10816 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
10817 target->adjusted_gp_value(object),
10818 r_addend, extract_addend,
10820 &calculated_value);
10822 case elfcpp::R_MIPS_GOT_HI16:
10823 case elfcpp::R_MIPS_CALL_HI16:
10824 case elfcpp::R_MICROMIPS_GOT_HI16:
10825 case elfcpp::R_MICROMIPS_CALL_HI16:
10827 got_offset = target->got_section()->got_offset(gsym,
10831 got_offset = target->got_section()->got_offset(r_sym,
10834 gp_offset = target->got_section()->gp_offset(got_offset, object);
10835 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
10837 &calculated_value);
10838 update_got_entry = changed_symbol_value;
10841 case elfcpp::R_MIPS_GOT_LO16:
10842 case elfcpp::R_MIPS_CALL_LO16:
10843 case elfcpp::R_MICROMIPS_GOT_LO16:
10844 case elfcpp::R_MICROMIPS_CALL_LO16:
10846 got_offset = target->got_section()->got_offset(gsym,
10850 got_offset = target->got_section()->got_offset(r_sym,
10853 gp_offset = target->got_section()->gp_offset(got_offset, object);
10854 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
10856 &calculated_value);
10857 update_got_entry = changed_symbol_value;
10860 case elfcpp::R_MIPS_GOT_DISP:
10861 case elfcpp::R_MICROMIPS_GOT_DISP:
10862 case elfcpp::R_MIPS_EH:
10864 got_offset = target->got_section()->got_offset(gsym,
10868 got_offset = target->got_section()->got_offset(r_sym,
10871 gp_offset = target->got_section()->gp_offset(got_offset, object);
10872 if (eh_reloc(r_types[i]))
10873 reloc_status = Reloc_funcs::releh(view, gp_offset,
10875 &calculated_value);
10877 reloc_status = Reloc_funcs::relgot(view, gp_offset,
10879 &calculated_value);
10881 case elfcpp::R_MIPS_CALL16:
10882 case elfcpp::R_MIPS16_CALL16:
10883 case elfcpp::R_MICROMIPS_CALL16:
10884 gold_assert(gsym != NULL);
10885 got_offset = target->got_section()->got_offset(gsym,
10888 gp_offset = target->got_section()->gp_offset(got_offset, object);
10889 reloc_status = Reloc_funcs::relgot(view, gp_offset,
10890 calculate_only, &calculated_value);
10891 // TODO(sasa): We should also initialize update_got_entry
10892 // in other place swhere relgot is called.
10893 update_got_entry = changed_symbol_value;
10896 case elfcpp::R_MIPS_GOT16:
10897 case elfcpp::R_MIPS16_GOT16:
10898 case elfcpp::R_MICROMIPS_GOT16:
10901 got_offset = target->got_section()->got_offset(gsym,
10904 gp_offset = target->got_section()->gp_offset(got_offset, object);
10905 reloc_status = Reloc_funcs::relgot(view, gp_offset,
10907 &calculated_value);
10911 if (rel_type == elfcpp::SHT_RELA)
10912 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
10917 &calculated_value);
10918 else if (rel_type == elfcpp::SHT_REL)
10919 reloc_status = Reloc_funcs::relgot16_local(view, object,
10922 r_types[i], r_sym);
10924 gold_unreachable();
10926 update_got_entry = changed_symbol_value;
10929 case elfcpp::R_MIPS_TLS_GD:
10930 case elfcpp::R_MIPS16_TLS_GD:
10931 case elfcpp::R_MICROMIPS_TLS_GD:
10933 got_offset = target->got_section()->got_offset(gsym,
10937 got_offset = target->got_section()->got_offset(r_sym,
10940 gp_offset = target->got_section()->gp_offset(got_offset, object);
10941 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
10942 &calculated_value);
10945 case elfcpp::R_MIPS_TLS_GOTTPREL:
10946 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10947 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10949 got_offset = target->got_section()->got_offset(gsym,
10950 GOT_TYPE_TLS_OFFSET,
10953 got_offset = target->got_section()->got_offset(r_sym,
10954 GOT_TYPE_TLS_OFFSET,
10956 gp_offset = target->got_section()->gp_offset(got_offset, object);
10957 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
10958 &calculated_value);
10961 case elfcpp::R_MIPS_TLS_LDM:
10962 case elfcpp::R_MIPS16_TLS_LDM:
10963 case elfcpp::R_MICROMIPS_TLS_LDM:
10964 // Relocate the field with the offset of the GOT entry for
10965 // the module index.
10966 got_offset = target->got_section()->tls_ldm_offset(object);
10967 gp_offset = target->got_section()->gp_offset(got_offset, object);
10968 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
10969 &calculated_value);
10972 case elfcpp::R_MIPS_GOT_PAGE:
10973 case elfcpp::R_MICROMIPS_GOT_PAGE:
10974 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
10975 r_addend, extract_addend,
10977 &calculated_value);
10980 case elfcpp::R_MIPS_GOT_OFST:
10981 case elfcpp::R_MICROMIPS_GOT_OFST:
10982 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
10983 r_addend, extract_addend,
10984 local, calculate_only,
10985 &calculated_value);
10988 case elfcpp::R_MIPS_JALR:
10989 case elfcpp::R_MICROMIPS_JALR:
10990 // This relocation is only a hint. In some cases, we optimize
10991 // it into a bal instruction. But we don't try to optimize
10992 // when the symbol does not resolve locally.
10994 || symbol_calls_local(gsym, gsym->has_dynsym_index()))
10995 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
10996 r_addend, extract_addend,
10997 cross_mode_jump, r_types[i],
10998 target->jalr_to_bal(),
11001 &calculated_value);
11004 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
11005 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
11006 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
11007 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
11008 elfcpp::DTP_OFFSET, r_addend,
11009 extract_addend, calculate_only,
11010 &calculated_value);
11012 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
11013 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
11014 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
11015 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
11016 elfcpp::DTP_OFFSET, r_addend,
11017 extract_addend, calculate_only,
11018 &calculated_value);
11020 case elfcpp::R_MIPS_TLS_DTPREL32:
11021 case elfcpp::R_MIPS_TLS_DTPREL64:
11022 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
11023 elfcpp::DTP_OFFSET, r_addend,
11024 extract_addend, calculate_only,
11025 &calculated_value);
11027 case elfcpp::R_MIPS_TLS_TPREL_HI16:
11028 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
11029 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
11030 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
11031 elfcpp::TP_OFFSET, r_addend,
11032 extract_addend, calculate_only,
11033 &calculated_value);
11035 case elfcpp::R_MIPS_TLS_TPREL_LO16:
11036 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
11037 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
11038 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
11039 elfcpp::TP_OFFSET, r_addend,
11040 extract_addend, calculate_only,
11041 &calculated_value);
11043 case elfcpp::R_MIPS_TLS_TPREL32:
11044 case elfcpp::R_MIPS_TLS_TPREL64:
11045 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
11046 elfcpp::TP_OFFSET, r_addend,
11047 extract_addend, calculate_only,
11048 &calculated_value);
11050 case elfcpp::R_MIPS_SUB:
11051 case elfcpp::R_MICROMIPS_SUB:
11052 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
11054 calculate_only, &calculated_value);
11057 gold_error_at_location(relinfo, relnum, r_offset,
11058 _("unsupported reloc %u"), r_types[i]);
11062 if (update_got_entry)
11064 Mips_output_data_got<size, big_endian>* got = target->got_section();
11065 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
11066 got->update_got_entry(got->get_primary_got_offset(mips_sym),
11067 psymval->value(object, 0));
11069 got->update_got_entry(got_offset, psymval->value(object, 0));
11072 r_addend = calculated_value;
11075 bool jal_shuffle = jal_reloc(r_type) ? !parameters->options().relocatable()
11077 Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
11079 // Report any errors.
11080 switch (reloc_status)
11082 case Reloc_funcs::STATUS_OKAY:
11084 case Reloc_funcs::STATUS_OVERFLOW:
11085 gold_error_at_location(relinfo, relnum, r_offset,
11086 _("relocation overflow"));
11088 case Reloc_funcs::STATUS_BAD_RELOC:
11089 gold_error_at_location(relinfo, relnum, r_offset,
11090 _("unexpected opcode while processing relocation"));
11093 gold_unreachable();
11099 // Get the Reference_flags for a particular relocation.
11101 template<int size, bool big_endian>
11103 Target_mips<size, big_endian>::Scan::get_reference_flags(
11104 unsigned int r_type)
11108 case elfcpp::R_MIPS_NONE:
11109 // No symbol reference.
11112 case elfcpp::R_MIPS_16:
11113 case elfcpp::R_MIPS_32:
11114 case elfcpp::R_MIPS_64:
11115 case elfcpp::R_MIPS_HI16:
11116 case elfcpp::R_MIPS_LO16:
11117 case elfcpp::R_MIPS16_HI16:
11118 case elfcpp::R_MIPS16_LO16:
11119 case elfcpp::R_MICROMIPS_HI16:
11120 case elfcpp::R_MICROMIPS_LO16:
11121 return Symbol::ABSOLUTE_REF;
11123 case elfcpp::R_MIPS_26:
11124 case elfcpp::R_MIPS16_26:
11125 case elfcpp::R_MICROMIPS_26_S1:
11126 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
11128 case elfcpp::R_MIPS_GPREL32:
11129 case elfcpp::R_MIPS_GPREL16:
11130 case elfcpp::R_MIPS_REL32:
11131 case elfcpp::R_MIPS16_GPREL:
11132 return Symbol::RELATIVE_REF;
11134 case elfcpp::R_MIPS_PC16:
11135 case elfcpp::R_MIPS_PC32:
11136 case elfcpp::R_MIPS_JALR:
11137 case elfcpp::R_MICROMIPS_JALR:
11138 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
11140 case elfcpp::R_MIPS_GOT16:
11141 case elfcpp::R_MIPS_CALL16:
11142 case elfcpp::R_MIPS_GOT_DISP:
11143 case elfcpp::R_MIPS_GOT_HI16:
11144 case elfcpp::R_MIPS_GOT_LO16:
11145 case elfcpp::R_MIPS_CALL_HI16:
11146 case elfcpp::R_MIPS_CALL_LO16:
11147 case elfcpp::R_MIPS_LITERAL:
11148 case elfcpp::R_MIPS_GOT_PAGE:
11149 case elfcpp::R_MIPS_GOT_OFST:
11150 case elfcpp::R_MIPS16_GOT16:
11151 case elfcpp::R_MIPS16_CALL16:
11152 case elfcpp::R_MICROMIPS_GOT16:
11153 case elfcpp::R_MICROMIPS_CALL16:
11154 case elfcpp::R_MICROMIPS_GOT_HI16:
11155 case elfcpp::R_MICROMIPS_GOT_LO16:
11156 case elfcpp::R_MICROMIPS_CALL_HI16:
11157 case elfcpp::R_MICROMIPS_CALL_LO16:
11158 case elfcpp::R_MIPS_EH:
11159 // Absolute in GOT.
11160 return Symbol::RELATIVE_REF;
11162 case elfcpp::R_MIPS_TLS_DTPMOD32:
11163 case elfcpp::R_MIPS_TLS_DTPREL32:
11164 case elfcpp::R_MIPS_TLS_DTPMOD64:
11165 case elfcpp::R_MIPS_TLS_DTPREL64:
11166 case elfcpp::R_MIPS_TLS_GD:
11167 case elfcpp::R_MIPS_TLS_LDM:
11168 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
11169 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
11170 case elfcpp::R_MIPS_TLS_GOTTPREL:
11171 case elfcpp::R_MIPS_TLS_TPREL32:
11172 case elfcpp::R_MIPS_TLS_TPREL64:
11173 case elfcpp::R_MIPS_TLS_TPREL_HI16:
11174 case elfcpp::R_MIPS_TLS_TPREL_LO16:
11175 case elfcpp::R_MIPS16_TLS_GD:
11176 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11177 case elfcpp::R_MICROMIPS_TLS_GD:
11178 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11179 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
11180 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
11181 return Symbol::TLS_REF;
11183 case elfcpp::R_MIPS_COPY:
11184 case elfcpp::R_MIPS_JUMP_SLOT:
11186 gold_unreachable();
11187 // Not expected. We will give an error later.
11192 // Report an unsupported relocation against a local symbol.
11194 template<int size, bool big_endian>
11196 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
11197 Sized_relobj_file<size, big_endian>* object,
11198 unsigned int r_type)
11200 gold_error(_("%s: unsupported reloc %u against local symbol"),
11201 object->name().c_str(), r_type);
11204 // Report an unsupported relocation against a global symbol.
11206 template<int size, bool big_endian>
11208 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
11209 Sized_relobj_file<size, big_endian>* object,
11210 unsigned int r_type,
11213 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
11214 object->name().c_str(), r_type, gsym->demangled_name().c_str());
11217 // Return printable name for ABI.
11218 template<int size, bool big_endian>
11220 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
11222 switch (e_flags & elfcpp::EF_MIPS_ABI)
11225 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
11227 else if (size == 64)
11231 case elfcpp::E_MIPS_ABI_O32:
11233 case elfcpp::E_MIPS_ABI_O64:
11235 case elfcpp::E_MIPS_ABI_EABI32:
11237 case elfcpp::E_MIPS_ABI_EABI64:
11240 return "unknown abi";
11244 template<int size, bool big_endian>
11246 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
11248 switch (e_flags & elfcpp::EF_MIPS_MACH)
11250 case elfcpp::E_MIPS_MACH_3900:
11251 return "mips:3900";
11252 case elfcpp::E_MIPS_MACH_4010:
11253 return "mips:4010";
11254 case elfcpp::E_MIPS_MACH_4100:
11255 return "mips:4100";
11256 case elfcpp::E_MIPS_MACH_4111:
11257 return "mips:4111";
11258 case elfcpp::E_MIPS_MACH_4120:
11259 return "mips:4120";
11260 case elfcpp::E_MIPS_MACH_4650:
11261 return "mips:4650";
11262 case elfcpp::E_MIPS_MACH_5400:
11263 return "mips:5400";
11264 case elfcpp::E_MIPS_MACH_5500:
11265 return "mips:5500";
11266 case elfcpp::E_MIPS_MACH_SB1:
11268 case elfcpp::E_MIPS_MACH_9000:
11269 return "mips:9000";
11270 case elfcpp::E_MIPS_MACH_LS2E:
11271 return "mips:loongson-2e";
11272 case elfcpp::E_MIPS_MACH_LS2F:
11273 return "mips:loongson-2f";
11274 case elfcpp::E_MIPS_MACH_LS3A:
11275 return "mips:loongson-3a";
11276 case elfcpp::E_MIPS_MACH_OCTEON:
11277 return "mips:octeon";
11278 case elfcpp::E_MIPS_MACH_OCTEON2:
11279 return "mips:octeon2";
11280 case elfcpp::E_MIPS_MACH_XLR:
11283 switch (e_flags & elfcpp::EF_MIPS_ARCH)
11286 case elfcpp::E_MIPS_ARCH_1:
11287 return "mips:3000";
11289 case elfcpp::E_MIPS_ARCH_2:
11290 return "mips:6000";
11292 case elfcpp::E_MIPS_ARCH_3:
11293 return "mips:4000";
11295 case elfcpp::E_MIPS_ARCH_4:
11296 return "mips:8000";
11298 case elfcpp::E_MIPS_ARCH_5:
11299 return "mips:mips5";
11301 case elfcpp::E_MIPS_ARCH_32:
11302 return "mips:isa32";
11304 case elfcpp::E_MIPS_ARCH_64:
11305 return "mips:isa64";
11307 case elfcpp::E_MIPS_ARCH_32R2:
11308 return "mips:isa32r2";
11310 case elfcpp::E_MIPS_ARCH_64R2:
11311 return "mips:isa64r2";
11314 return "unknown CPU";
11317 template<int size, bool big_endian>
11318 const Target::Target_info Target_mips<size, big_endian>::mips_info =
11321 big_endian, // is_big_endian
11322 elfcpp::EM_MIPS, // machine_code
11323 true, // has_make_symbol
11324 false, // has_resolve
11325 false, // has_code_fill
11326 true, // is_default_stack_executable
11327 false, // can_icf_inline_merge_sections
11329 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
11330 0x400000, // default_text_segment_address
11331 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
11332 4 * 1024, // common_pagesize (overridable by -z common-page-size)
11333 false, // isolate_execinstr
11334 0, // rosegment_gap
11335 elfcpp::SHN_UNDEF, // small_common_shndx
11336 elfcpp::SHN_UNDEF, // large_common_shndx
11337 0, // small_common_section_flags
11338 0, // large_common_section_flags
11339 NULL, // attributes_section
11340 NULL, // attributes_vendor
11341 "__start", // entry_symbol_name
11342 32, // hash_entry_size
11345 template<int size, bool big_endian>
11346 class Target_mips_nacl : public Target_mips<size, big_endian>
11350 : Target_mips<size, big_endian>(&mips_nacl_info)
11354 static const Target::Target_info mips_nacl_info;
11357 template<int size, bool big_endian>
11358 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
11361 big_endian, // is_big_endian
11362 elfcpp::EM_MIPS, // machine_code
11363 true, // has_make_symbol
11364 false, // has_resolve
11365 false, // has_code_fill
11366 true, // is_default_stack_executable
11367 false, // can_icf_inline_merge_sections
11369 "/lib/ld.so.1", // dynamic_linker
11370 0x20000, // default_text_segment_address
11371 0x10000, // abi_pagesize (overridable by -z max-page-size)
11372 0x10000, // common_pagesize (overridable by -z common-page-size)
11373 true, // isolate_execinstr
11374 0x10000000, // rosegment_gap
11375 elfcpp::SHN_UNDEF, // small_common_shndx
11376 elfcpp::SHN_UNDEF, // large_common_shndx
11377 0, // small_common_section_flags
11378 0, // large_common_section_flags
11379 NULL, // attributes_section
11380 NULL, // attributes_vendor
11381 "_start", // entry_symbol_name
11382 32, // hash_entry_size
11385 // Target selector for Mips. Note this is never instantiated directly.
11386 // It's only used in Target_selector_mips_nacl, below.
11388 template<int size, bool big_endian>
11389 class Target_selector_mips : public Target_selector
11392 Target_selector_mips()
11393 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
11395 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
11396 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
11398 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
11399 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
11402 Target* do_instantiate_target()
11403 { return new Target_mips<size, big_endian>(); }
11406 template<int size, bool big_endian>
11407 class Target_selector_mips_nacl
11408 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
11409 Target_mips_nacl<size, big_endian> >
11412 Target_selector_mips_nacl()
11413 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
11414 Target_mips_nacl<size, big_endian> >(
11415 // NaCl currently supports only MIPS32 little-endian.
11416 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
11420 Target_selector_mips_nacl<32, true> target_selector_mips32;
11421 Target_selector_mips_nacl<32, false> target_selector_mips32el;
11422 Target_selector_mips_nacl<64, true> target_selector_mips64;
11423 Target_selector_mips_nacl<64, false> target_selector_mips64el;
11425 } // End anonymous namespace.