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"
47 #include "attributes.h"
54 template<int size, bool big_endian>
55 class Mips_output_data_plt;
57 template<int size, bool big_endian>
58 class Mips_output_data_got;
60 template<int size, bool big_endian>
63 template<int size, bool big_endian>
64 class Mips_output_section_reginfo;
66 template<int size, bool big_endian>
67 class Mips_output_data_la25_stub;
69 template<int size, bool big_endian>
70 class Mips_output_data_mips_stubs;
75 template<int size, bool big_endian>
78 template<int size, bool big_endian>
81 class Mips16_stub_section_base;
83 template<int size, bool big_endian>
84 class Mips16_stub_section;
86 // The ABI says that every symbol used by dynamic relocations must have
87 // a global GOT entry. Among other things, this provides the dynamic
88 // linker with a free, directly-indexed cache. The GOT can therefore
89 // contain symbols that are not referenced by GOT relocations themselves
90 // (in other words, it may have symbols that are not referenced by things
91 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
93 // GOT relocations are less likely to overflow if we put the associated
94 // GOT entries towards the beginning. We therefore divide the global
95 // GOT entries into two areas: "normal" and "reloc-only". Entries in
96 // the first area can be used for both dynamic relocations and GP-relative
97 // accesses, while those in the "reloc-only" area are for dynamic
100 // These GGA_* ("Global GOT Area") values are organised so that lower
101 // values are more general than higher values. Also, non-GGA_NONE
102 // values are ordered by the position of the area in the GOT.
111 // The types of GOT entries needed for this platform.
112 // These values are exposed to the ABI in an incremental link.
113 // Do not renumber existing values without changing the version
114 // number of the .gnu_incremental_inputs section.
117 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
118 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
119 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
121 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
122 GOT_TYPE_STANDARD_MULTIGOT = 3,
123 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
124 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
127 // TLS type of GOT entry.
136 // Values found in the r_ssym field of a relocation entry.
137 enum Special_relocation_symbol
139 RSS_UNDEF = 0, // None - value is zero.
140 RSS_GP = 1, // Value of GP.
141 RSS_GP0 = 2, // Value of GP in object being relocated.
142 RSS_LOC = 3 // Address of location being relocated.
145 // Whether the section is readonly.
147 is_readonly_section(Output_section* output_section)
149 elfcpp::Elf_Xword section_flags = output_section->flags();
150 elfcpp::Elf_Word section_type = output_section->type();
152 if (section_type == elfcpp::SHT_NOBITS)
155 if (section_flags & elfcpp::SHF_WRITE)
161 // Return TRUE if a relocation of type R_TYPE from OBJECT might
162 // require an la25 stub. See also local_pic_function, which determines
163 // whether the destination function ever requires a stub.
164 template<int size, bool big_endian>
166 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
167 unsigned int r_type, bool target_is_16_bit_code)
169 // We specifically ignore branches and jumps from EF_PIC objects,
170 // where the onus is on the compiler or programmer to perform any
171 // necessary initialization of $25. Sometimes such initialization
172 // is unnecessary; for example, -mno-shared functions do not use
173 // the incoming value of $25, and may therefore be called directly.
174 if (object->is_pic())
179 case elfcpp::R_MIPS_26:
180 case elfcpp::R_MIPS_PC16:
181 case elfcpp::R_MICROMIPS_26_S1:
182 case elfcpp::R_MICROMIPS_PC7_S1:
183 case elfcpp::R_MICROMIPS_PC10_S1:
184 case elfcpp::R_MICROMIPS_PC16_S1:
185 case elfcpp::R_MICROMIPS_PC23_S2:
188 case elfcpp::R_MIPS16_26:
189 return !target_is_16_bit_code;
196 // Return true if SYM is a locally-defined PIC function, in the sense
197 // that it or its fn_stub might need $25 to be valid on entry.
198 // Note that MIPS16 functions set up $gp using PC-relative instructions,
199 // so they themselves never need $25 to be valid. Only non-MIPS16
200 // entry points are of interest here.
201 template<int size, bool big_endian>
203 local_pic_function(Mips_symbol<size>* sym)
205 bool def_regular = (sym->source() == Symbol::FROM_OBJECT
206 && !sym->object()->is_dynamic()
207 && !sym->is_undefined());
209 if (sym->is_defined() && def_regular)
211 Mips_relobj<size, big_endian>* object =
212 static_cast<Mips_relobj<size, big_endian>*>(sym->object());
214 if ((object->is_pic() || sym->is_pic())
215 && (!sym->is_mips16()
216 || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
223 hi16_reloc(int r_type)
225 return (r_type == elfcpp::R_MIPS_HI16
226 || r_type == elfcpp::R_MIPS16_HI16
227 || r_type == elfcpp::R_MICROMIPS_HI16);
231 lo16_reloc(int r_type)
233 return (r_type == elfcpp::R_MIPS_LO16
234 || r_type == elfcpp::R_MIPS16_LO16
235 || r_type == elfcpp::R_MICROMIPS_LO16);
239 got16_reloc(unsigned int r_type)
241 return (r_type == elfcpp::R_MIPS_GOT16
242 || r_type == elfcpp::R_MIPS16_GOT16
243 || r_type == elfcpp::R_MICROMIPS_GOT16);
247 call_lo16_reloc(unsigned int r_type)
249 return (r_type == elfcpp::R_MIPS_CALL_LO16
250 || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
254 got_lo16_reloc(unsigned int r_type)
256 return (r_type == elfcpp::R_MIPS_GOT_LO16
257 || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
261 eh_reloc(unsigned int r_type)
263 return (r_type == elfcpp::R_MIPS_EH);
267 got_disp_reloc(unsigned int r_type)
269 return (r_type == elfcpp::R_MIPS_GOT_DISP
270 || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
274 got_page_reloc(unsigned int r_type)
276 return (r_type == elfcpp::R_MIPS_GOT_PAGE
277 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
281 tls_gd_reloc(unsigned int r_type)
283 return (r_type == elfcpp::R_MIPS_TLS_GD
284 || r_type == elfcpp::R_MIPS16_TLS_GD
285 || r_type == elfcpp::R_MICROMIPS_TLS_GD);
289 tls_gottprel_reloc(unsigned int r_type)
291 return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
292 || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
293 || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
297 tls_ldm_reloc(unsigned int r_type)
299 return (r_type == elfcpp::R_MIPS_TLS_LDM
300 || r_type == elfcpp::R_MIPS16_TLS_LDM
301 || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
305 mips16_call_reloc(unsigned int r_type)
307 return (r_type == elfcpp::R_MIPS16_26
308 || r_type == elfcpp::R_MIPS16_CALL16);
312 jal_reloc(unsigned int r_type)
314 return (r_type == elfcpp::R_MIPS_26
315 || r_type == elfcpp::R_MIPS16_26
316 || r_type == elfcpp::R_MICROMIPS_26_S1);
320 micromips_branch_reloc(unsigned int r_type)
322 return (r_type == elfcpp::R_MICROMIPS_26_S1
323 || r_type == elfcpp::R_MICROMIPS_PC16_S1
324 || r_type == elfcpp::R_MICROMIPS_PC10_S1
325 || r_type == elfcpp::R_MICROMIPS_PC7_S1);
328 // Check if R_TYPE is a MIPS16 reloc.
330 mips16_reloc(unsigned int r_type)
334 case elfcpp::R_MIPS16_26:
335 case elfcpp::R_MIPS16_GPREL:
336 case elfcpp::R_MIPS16_GOT16:
337 case elfcpp::R_MIPS16_CALL16:
338 case elfcpp::R_MIPS16_HI16:
339 case elfcpp::R_MIPS16_LO16:
340 case elfcpp::R_MIPS16_TLS_GD:
341 case elfcpp::R_MIPS16_TLS_LDM:
342 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
343 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
344 case elfcpp::R_MIPS16_TLS_GOTTPREL:
345 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
346 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
354 // Check if R_TYPE is a microMIPS reloc.
356 micromips_reloc(unsigned int r_type)
360 case elfcpp::R_MICROMIPS_26_S1:
361 case elfcpp::R_MICROMIPS_HI16:
362 case elfcpp::R_MICROMIPS_LO16:
363 case elfcpp::R_MICROMIPS_GPREL16:
364 case elfcpp::R_MICROMIPS_LITERAL:
365 case elfcpp::R_MICROMIPS_GOT16:
366 case elfcpp::R_MICROMIPS_PC7_S1:
367 case elfcpp::R_MICROMIPS_PC10_S1:
368 case elfcpp::R_MICROMIPS_PC16_S1:
369 case elfcpp::R_MICROMIPS_CALL16:
370 case elfcpp::R_MICROMIPS_GOT_DISP:
371 case elfcpp::R_MICROMIPS_GOT_PAGE:
372 case elfcpp::R_MICROMIPS_GOT_OFST:
373 case elfcpp::R_MICROMIPS_GOT_HI16:
374 case elfcpp::R_MICROMIPS_GOT_LO16:
375 case elfcpp::R_MICROMIPS_SUB:
376 case elfcpp::R_MICROMIPS_HIGHER:
377 case elfcpp::R_MICROMIPS_HIGHEST:
378 case elfcpp::R_MICROMIPS_CALL_HI16:
379 case elfcpp::R_MICROMIPS_CALL_LO16:
380 case elfcpp::R_MICROMIPS_SCN_DISP:
381 case elfcpp::R_MICROMIPS_JALR:
382 case elfcpp::R_MICROMIPS_HI0_LO16:
383 case elfcpp::R_MICROMIPS_TLS_GD:
384 case elfcpp::R_MICROMIPS_TLS_LDM:
385 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
386 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
387 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
388 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
389 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
390 case elfcpp::R_MICROMIPS_GPREL7_S2:
391 case elfcpp::R_MICROMIPS_PC23_S2:
400 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
404 case elfcpp::R_MIPS_HI16:
405 case elfcpp::R_MIPS_GOT16:
406 return lo16_reloc == elfcpp::R_MIPS_LO16;
407 case elfcpp::R_MIPS16_HI16:
408 case elfcpp::R_MIPS16_GOT16:
409 return lo16_reloc == elfcpp::R_MIPS16_LO16;
410 case elfcpp::R_MICROMIPS_HI16:
411 case elfcpp::R_MICROMIPS_GOT16:
412 return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
418 // This class is used to hold information about one GOT entry.
419 // There are three types of entry:
421 // (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
422 // (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
423 // (2) a SYMBOL address, where SYMBOL is not local to an input object
424 // (sym != NULL, symndx == -1)
425 // (3) a TLS LDM slot (there's only one of these per GOT.)
426 // (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
428 template<int size, bool big_endian>
431 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
434 Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
435 Mips_address addend, unsigned char tls_type,
436 unsigned int shndx, bool is_section_symbol)
437 : addend_(addend), symndx_(symndx), tls_type_(tls_type),
438 is_section_symbol_(is_section_symbol), shndx_(shndx)
439 { this->d.object = object; }
441 Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
442 : addend_(0), symndx_(-1U), tls_type_(tls_type),
443 is_section_symbol_(false), shndx_(-1U)
444 { this->d.sym = sym; }
446 // Return whether this entry is for a local symbol.
448 is_for_local_symbol() const
449 { return this->symndx_ != -1U; }
451 // Return whether this entry is for a global symbol.
453 is_for_global_symbol() const
454 { return this->symndx_ == -1U; }
456 // Return the hash of this entry.
460 if (this->tls_type_ == GOT_TLS_LDM)
461 return this->symndx_ + (1 << 18);
463 size_t name_hash_value = gold::string_hash<char>(
464 (this->symndx_ != -1U)
465 ? this->d.object->name().c_str()
466 : this->d.sym->name());
467 size_t addend = this->addend_;
468 return name_hash_value ^ this->symndx_ ^ addend;
471 // Return whether this entry is equal to OTHER.
473 equals(Mips_got_entry<size, big_endian>* other) const
475 if (this->tls_type_ == GOT_TLS_LDM)
478 return ((this->tls_type_ == other->tls_type_)
479 && (this->symndx_ == other->symndx_)
480 && ((this->symndx_ != -1U)
481 ? (this->d.object == other->d.object)
482 : (this->d.sym == other->d.sym))
483 && (this->addend_ == other->addend_));
486 // Return input object that needs this GOT entry.
487 Mips_relobj<size, big_endian>*
490 gold_assert(this->symndx_ != -1U);
491 return this->d.object;
494 // Return local symbol index for local GOT entries.
498 gold_assert(this->symndx_ != -1U);
499 return this->symndx_;
502 // Return the relocation addend for local GOT entries.
505 { return this->addend_; }
507 // Return global symbol for global GOT entries.
511 gold_assert(this->symndx_ == -1U);
515 // Return whether this is a TLS GOT entry.
518 { return this->tls_type_ != GOT_TLS_NONE; }
520 // Return TLS type of this GOT entry.
523 { return this->tls_type_; }
525 // Return section index of the local symbol for local GOT entries.
528 { return this->shndx_; }
530 // Return whether this is a STT_SECTION symbol.
532 is_section_symbol() const
533 { return this->is_section_symbol_; }
537 Mips_address addend_;
539 // The index of the symbol if we have a local symbol; -1 otherwise.
540 unsigned int symndx_;
544 // The input object for local symbols that needs the GOT entry.
545 Mips_relobj<size, big_endian>* object;
546 // If symndx == -1, the global symbol corresponding to this GOT entry. The
547 // symbol's entry is in the local area if mips_sym->global_got_area is
548 // GGA_NONE, otherwise it is in the global area.
549 Mips_symbol<size>* sym;
552 // The TLS type of this GOT entry. An LDM GOT entry will be a local
553 // symbol entry with r_symndx == 0.
554 unsigned char tls_type_;
556 // Whether this is a STT_SECTION symbol.
557 bool is_section_symbol_;
559 // For local GOT entries, section index of the local symbol.
563 // Hash for Mips_got_entry.
565 template<int size, bool big_endian>
566 class Mips_got_entry_hash
570 operator()(Mips_got_entry<size, big_endian>* entry) const
571 { return entry->hash(); }
574 // Equality for Mips_got_entry.
576 template<int size, bool big_endian>
577 class Mips_got_entry_eq
581 operator()(Mips_got_entry<size, big_endian>* e1,
582 Mips_got_entry<size, big_endian>* e2) const
583 { return e1->equals(e2); }
586 // Hash for Mips_symbol.
589 class Mips_symbol_hash
593 operator()(Mips_symbol<size>* sym) const
594 { return sym->hash(); }
597 // Got_page_range. This class describes a range of addends: [MIN_ADDEND,
598 // MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
599 // increasing MIN_ADDEND.
601 struct Got_page_range
604 : next(NULL), min_addend(0), max_addend(0)
607 Got_page_range* next;
611 // Return the maximum number of GOT page entries required.
614 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
617 // Got_page_entry. This class describes the range of addends that are applied
618 // to page relocations against a given symbol.
620 struct Got_page_entry
623 : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
626 Got_page_entry(Object* object_, unsigned int symndx_)
627 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
630 // The input object that needs the GOT page entry.
632 // The index of the symbol, as stored in the relocation r_info.
634 // The ranges for this page entry.
635 Got_page_range* ranges;
636 // The maximum number of page entries needed for RANGES.
637 unsigned int num_pages;
640 // Hash for Got_page_entry.
642 struct Got_page_entry_hash
645 operator()(Got_page_entry* entry) const
646 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
649 // Equality for Got_page_entry.
651 struct Got_page_entry_eq
654 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
656 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
660 // This class is used to hold .got information when linking.
662 template<int size, bool big_endian>
665 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
666 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
668 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
670 // Unordered set of GOT entries.
671 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
672 Mips_got_entry_hash<size, big_endian>,
673 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
675 // Unordered set of GOT page entries.
676 typedef Unordered_set<Got_page_entry*,
677 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
679 // Unordered set of global GOT entries.
680 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
681 Global_got_entry_set;
685 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
686 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
687 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
688 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
692 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
693 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
695 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
696 unsigned int symndx, Mips_address addend,
697 unsigned int r_type, unsigned int shndx,
698 bool is_section_symbol);
700 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
701 // in OBJECT. FOR_CALL is true if the caller is only interested in
702 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
705 record_global_got_symbol(Mips_symbol<size>* mips_sym,
706 Mips_relobj<size, big_endian>* object,
707 unsigned int r_type, bool dyn_reloc, bool for_call);
709 // Add ENTRY to master GOT and to OBJECT's GOT.
711 record_got_entry(Mips_got_entry<size, big_endian>* entry,
712 Mips_relobj<size, big_endian>* object);
714 // Record that OBJECT has a page relocation against symbol SYMNDX and
715 // that ADDEND is the addend for that relocation.
717 record_got_page_entry(Mips_relobj<size, big_endian>* object,
718 unsigned int symndx, int addend);
720 // Create all entries that should be in the local part of the GOT.
722 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
724 // Create GOT page entries.
726 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
728 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
730 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
731 unsigned int non_reloc_only_global_gotno);
733 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
735 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
737 // Create TLS GOT entries.
739 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
741 // Decide whether the symbol needs an entry in the global part of the primary
742 // GOT, setting global_got_area accordingly. Count the number of global
743 // symbols that are in the primary GOT only because they have dynamic
744 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
746 count_got_symbols(Symbol_table* symtab);
748 // Return the offset of GOT page entry for VALUE.
750 get_got_page_offset(Mips_address value,
751 Mips_output_data_got<size, big_endian>* got);
753 // Count the number of GOT entries required.
757 // Count the number of GOT entries required by ENTRY. Accumulate the result.
759 count_got_entry(Mips_got_entry<size, big_endian>* entry);
761 // Add FROM's GOT entries.
763 add_got_entries(Mips_got_info<size, big_endian>* from);
765 // Add FROM's GOT page entries.
767 add_got_page_entries(Mips_got_info<size, big_endian>* from);
772 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
773 + this->tls_gotno_) * size/8);
776 // Return the number of local GOT entries.
779 { return this->local_gotno_; }
781 // Return the maximum number of page GOT entries needed.
784 { return this->page_gotno_; }
786 // Return the number of global GOT entries.
789 { return this->global_gotno_; }
791 // Set the number of global GOT entries.
793 set_global_gotno(unsigned int global_gotno)
794 { this->global_gotno_ = global_gotno; }
796 // Return the number of GGA_RELOC_ONLY global GOT entries.
798 reloc_only_gotno() const
799 { return this->reloc_only_gotno_; }
801 // Return the number of TLS GOT entries.
804 { return this->tls_gotno_; }
806 // Return the GOT type for this GOT. Used for multi-GOT links only.
808 multigot_got_type(unsigned int got_type) const
812 case GOT_TYPE_STANDARD:
813 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
814 case GOT_TYPE_TLS_OFFSET:
815 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
816 case GOT_TYPE_TLS_PAIR:
817 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
823 // Remove lazy-binding stubs for global symbols in this GOT.
825 remove_lazy_stubs(Target_mips<size, big_endian>* target);
827 // Return offset of this GOT from the start of .got section.
830 { return this->offset_; }
832 // Set offset of this GOT from the start of .got section.
834 set_offset(unsigned int offset)
835 { this->offset_ = offset; }
837 // Set index of this GOT in multi-GOT links.
839 set_index(unsigned int index)
840 { this->index_ = index; }
842 // Return next GOT in multi-GOT links.
843 Mips_got_info<size, big_endian>*
845 { return this->next_; }
847 // Set next GOT in multi-GOT links.
849 set_next(Mips_got_info<size, big_endian>* next)
850 { this->next_ = next; }
852 // Return the offset of TLS LDM entry for this GOT.
854 tls_ldm_offset() const
855 { return this->tls_ldm_offset_; }
857 // Set the offset of TLS LDM entry for this GOT.
859 set_tls_ldm_offset(unsigned int tls_ldm_offset)
860 { this->tls_ldm_offset_ = tls_ldm_offset; }
862 Global_got_entry_set&
864 { return this->global_got_symbols_; }
866 // Return the GOT_TLS_* type required by relocation type R_TYPE.
868 mips_elf_reloc_tls_type(unsigned int r_type)
870 if (tls_gd_reloc(r_type))
873 if (tls_ldm_reloc(r_type))
876 if (tls_gottprel_reloc(r_type))
882 // Return the number of GOT slots needed for GOT TLS type TYPE.
884 mips_tls_got_entries(unsigned int type)
904 // The number of local GOT entries.
905 unsigned int local_gotno_;
906 // The maximum number of page GOT entries needed.
907 unsigned int page_gotno_;
908 // The number of global GOT entries.
909 unsigned int global_gotno_;
910 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
911 unsigned int reloc_only_gotno_;
912 // The number of TLS GOT entries.
913 unsigned int tls_gotno_;
914 // The offset of TLS LDM entry for this GOT.
915 unsigned int tls_ldm_offset_;
916 // All symbols that have global GOT entry.
917 Global_got_entry_set global_got_symbols_;
918 // A hash table holding GOT entries.
919 Got_entry_set got_entries_;
920 // A hash table of GOT page entries.
921 Got_page_entry_set got_page_entries_;
922 // The offset of first GOT page entry for this GOT.
923 unsigned int got_page_offset_start_;
924 // The offset of next available GOT page entry for this GOT.
925 unsigned int got_page_offset_next_;
926 // A hash table that maps GOT page entry value to the GOT offset where
927 // the entry is located.
928 Got_page_offsets got_page_offsets_;
929 // In multi-GOT links, a pointer to the next GOT.
930 Mips_got_info<size, big_endian>* next_;
931 // Index of this GOT in multi-GOT links.
933 // The offset of this GOT in multi-GOT links.
934 unsigned int offset_;
937 // This is a helper class used during relocation scan. It records GOT16 addend.
939 template<int size, bool big_endian>
942 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
944 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
945 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
946 Mips_address _addend)
947 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
951 const Sized_relobj_file<size, big_endian>* object;
958 // .MIPS.abiflags section content
960 template<bool big_endian>
963 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
964 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
965 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
968 : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
969 cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
972 // Version of flags structure.
974 // The level of the ISA: 1-5, 32, 64.
976 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
978 // The size of general purpose registers.
980 // The size of co-processor 1 registers.
982 // The size of co-processor 2 registers.
984 // The floating-point ABI.
986 // Processor-specific extension.
988 // Mask of ASEs used.
990 // Mask of general flags.
995 // Mips_symbol class. Holds additional symbol information needed for Mips.
998 class Mips_symbol : public Sized_symbol<size>
1002 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1003 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1004 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1005 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1006 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1007 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1008 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1011 // Return whether this is a MIPS16 symbol.
1015 // (st_other & STO_MIPS16) == STO_MIPS16
1016 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1017 == elfcpp::STO_MIPS16 >> 2);
1020 // Return whether this is a microMIPS symbol.
1022 is_micromips() const
1024 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1025 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1026 == elfcpp::STO_MICROMIPS >> 2);
1029 // Return whether the symbol needs MIPS16 fn_stub.
1031 need_fn_stub() const
1032 { return this->need_fn_stub_; }
1034 // Set that the symbol needs MIPS16 fn_stub.
1037 { this->need_fn_stub_ = true; }
1039 // Return whether this symbol is referenced by branch relocations from
1040 // any non-PIC input file.
1042 has_nonpic_branches() const
1043 { return this->has_nonpic_branches_; }
1045 // Set that this symbol is referenced by branch relocations from
1046 // any non-PIC input file.
1048 set_has_nonpic_branches()
1049 { this->has_nonpic_branches_ = true; }
1051 // Return the offset of the la25 stub for this symbol from the start of the
1052 // la25 stub section.
1054 la25_stub_offset() const
1055 { return this->la25_stub_offset_; }
1057 // Set the offset of the la25 stub for this symbol from the start of the
1058 // la25 stub section.
1060 set_la25_stub_offset(unsigned int offset)
1061 { this->la25_stub_offset_ = offset; }
1063 // Return whether the symbol has la25 stub. This is true if this symbol is
1064 // for a PIC function, and there are non-PIC branches and jumps to it.
1066 has_la25_stub() const
1067 { return this->la25_stub_offset_ != -1U; }
1069 // Return whether there is a relocation against this symbol that must be
1070 // resolved by the static linker (that is, the relocation cannot possibly
1071 // be made dynamic).
1073 has_static_relocs() const
1074 { return this->has_static_relocs_; }
1076 // Set that there is a relocation against this symbol that must be resolved
1077 // by the static linker (that is, the relocation cannot possibly be made
1080 set_has_static_relocs()
1081 { this->has_static_relocs_ = true; }
1083 // Return whether we must not create a lazy-binding stub for this symbol.
1085 no_lazy_stub() const
1086 { return this->no_lazy_stub_; }
1088 // Set that we must not create a lazy-binding stub for this symbol.
1091 { this->no_lazy_stub_ = true; }
1093 // Return the offset of the lazy-binding stub for this symbol from the start
1094 // of .MIPS.stubs section.
1096 lazy_stub_offset() const
1097 { return this->lazy_stub_offset_; }
1099 // Set the offset of the lazy-binding stub for this symbol from the start
1100 // of .MIPS.stubs section.
1102 set_lazy_stub_offset(unsigned int offset)
1103 { this->lazy_stub_offset_ = offset; }
1105 // Return whether there are any relocations for this symbol where
1106 // pointer equality matters.
1108 pointer_equality_needed() const
1109 { return this->pointer_equality_needed_; }
1111 // Set that there are relocations for this symbol where pointer equality
1114 set_pointer_equality_needed()
1115 { this->pointer_equality_needed_ = true; }
1117 // Return global GOT area where this symbol in located.
1119 global_got_area() const
1120 { return this->global_got_area_; }
1122 // Set global GOT area where this symbol in located.
1124 set_global_got_area(Global_got_area global_got_area)
1125 { this->global_got_area_ = global_got_area; }
1127 // Return the global GOT offset for this symbol. For multi-GOT links, this
1128 // returns the offset from the start of .got section to the first GOT entry
1129 // for the symbol. Note that in multi-GOT links the symbol can have entry
1130 // in more than one GOT.
1132 global_gotoffset() const
1133 { return this->global_gotoffset_; }
1135 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1136 // the symbol can have entry in more than one GOT. This method will set
1137 // the offset only if it is less than current offset.
1139 set_global_gotoffset(unsigned int offset)
1141 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1142 this->global_gotoffset_ = offset;
1145 // Return whether all GOT relocations for this symbol are for calls.
1147 got_only_for_calls() const
1148 { return this->got_only_for_calls_; }
1150 // Set that there is a GOT relocation for this symbol that is not for call.
1152 set_got_not_only_for_calls()
1153 { this->got_only_for_calls_ = false; }
1155 // Return whether this is a PIC symbol.
1159 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1160 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1161 == (elfcpp::STO_MIPS_PIC >> 2));
1164 // Set the flag in st_other field that marks this symbol as PIC.
1168 if (this->is_mips16())
1169 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1170 this->set_nonvis((this->nonvis()
1171 & ~((elfcpp::STO_MIPS16 >> 2)
1172 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1173 | (elfcpp::STO_MIPS_PIC >> 2));
1175 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1176 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1177 | (elfcpp::STO_MIPS_PIC >> 2));
1180 // Set the flag in st_other field that marks this symbol as PLT.
1184 if (this->is_mips16())
1185 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1186 this->set_nonvis((this->nonvis()
1187 & ((elfcpp::STO_MIPS16 >> 2)
1188 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1189 | (elfcpp::STO_MIPS_PLT >> 2));
1192 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1193 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1194 | (elfcpp::STO_MIPS_PLT >> 2));
1197 // Downcast a base pointer to a Mips_symbol pointer.
1198 static Mips_symbol<size>*
1199 as_mips_sym(Symbol* sym)
1200 { return static_cast<Mips_symbol<size>*>(sym); }
1202 // Downcast a base pointer to a Mips_symbol pointer.
1203 static const Mips_symbol<size>*
1204 as_mips_sym(const Symbol* sym)
1205 { return static_cast<const Mips_symbol<size>*>(sym); }
1207 // Return whether the symbol has lazy-binding stub.
1209 has_lazy_stub() const
1210 { return this->has_lazy_stub_; }
1212 // Set whether the symbol has lazy-binding stub.
1214 set_has_lazy_stub(bool has_lazy_stub)
1215 { this->has_lazy_stub_ = has_lazy_stub; }
1217 // Return whether the symbol needs a standard PLT entry.
1219 needs_mips_plt() const
1220 { return this->needs_mips_plt_; }
1222 // Set whether the symbol needs a standard PLT entry.
1224 set_needs_mips_plt(bool needs_mips_plt)
1225 { this->needs_mips_plt_ = needs_mips_plt; }
1227 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1230 needs_comp_plt() const
1231 { return this->needs_comp_plt_; }
1233 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1235 set_needs_comp_plt(bool needs_comp_plt)
1236 { this->needs_comp_plt_ = needs_comp_plt; }
1238 // Return standard PLT entry offset, or -1 if none.
1240 mips_plt_offset() const
1241 { return this->mips_plt_offset_; }
1243 // Set standard PLT entry offset.
1245 set_mips_plt_offset(unsigned int mips_plt_offset)
1246 { this->mips_plt_offset_ = mips_plt_offset; }
1248 // Return whether the symbol has standard PLT entry.
1250 has_mips_plt_offset() const
1251 { return this->mips_plt_offset_ != -1U; }
1253 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1255 comp_plt_offset() const
1256 { return this->comp_plt_offset_; }
1258 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1260 set_comp_plt_offset(unsigned int comp_plt_offset)
1261 { this->comp_plt_offset_ = comp_plt_offset; }
1263 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1265 has_comp_plt_offset() const
1266 { return this->comp_plt_offset_ != -1U; }
1268 // Return MIPS16 fn stub for a symbol.
1269 template<bool big_endian>
1270 Mips16_stub_section<size, big_endian>*
1271 get_mips16_fn_stub() const
1273 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1276 // Set MIPS16 fn stub for a symbol.
1278 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1279 { this->mips16_fn_stub_ = stub; }
1281 // Return whether symbol has MIPS16 fn stub.
1283 has_mips16_fn_stub() const
1284 { return this->mips16_fn_stub_ != NULL; }
1286 // Return MIPS16 call stub for a symbol.
1287 template<bool big_endian>
1288 Mips16_stub_section<size, big_endian>*
1289 get_mips16_call_stub() const
1291 return static_cast<Mips16_stub_section<size, big_endian>*>(
1295 // Set MIPS16 call stub for a symbol.
1297 set_mips16_call_stub(Mips16_stub_section_base* stub)
1298 { this->mips16_call_stub_ = stub; }
1300 // Return whether symbol has MIPS16 call stub.
1302 has_mips16_call_stub() const
1303 { return this->mips16_call_stub_ != NULL; }
1305 // Return MIPS16 call_fp stub for a symbol.
1306 template<bool big_endian>
1307 Mips16_stub_section<size, big_endian>*
1308 get_mips16_call_fp_stub() const
1310 return static_cast<Mips16_stub_section<size, big_endian>*>(
1311 mips16_call_fp_stub_);
1314 // Set MIPS16 call_fp stub for a symbol.
1316 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1317 { this->mips16_call_fp_stub_ = stub; }
1319 // Return whether symbol has MIPS16 call_fp stub.
1321 has_mips16_call_fp_stub() const
1322 { return this->mips16_call_fp_stub_ != NULL; }
1325 get_applied_secondary_got_fixup() const
1326 { return applied_secondary_got_fixup_; }
1329 set_applied_secondary_got_fixup()
1330 { this->applied_secondary_got_fixup_ = true; }
1332 // Return the hash of this symbol.
1336 return gold::string_hash<char>(this->name());
1340 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1341 // appears in any relocs other than a 16 bit call.
1344 // True if this symbol is referenced by branch relocations from
1345 // any non-PIC input file. This is used to determine whether an
1346 // la25 stub is required.
1347 bool has_nonpic_branches_;
1349 // The offset of the la25 stub for this symbol from the start of the
1350 // la25 stub section.
1351 unsigned int la25_stub_offset_;
1353 // True if there is a relocation against this symbol that must be
1354 // resolved by the static linker (that is, the relocation cannot
1355 // possibly be made dynamic).
1356 bool has_static_relocs_;
1358 // Whether we must not create a lazy-binding stub for this symbol.
1359 // This is true if the symbol has relocations related to taking the
1360 // function's address.
1363 // The offset of the lazy-binding stub for this symbol from the start of
1364 // .MIPS.stubs section.
1365 unsigned int lazy_stub_offset_;
1367 // True if there are any relocations for this symbol where pointer equality
1369 bool pointer_equality_needed_;
1371 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1372 // in the global part of the GOT.
1373 Global_got_area global_got_area_;
1375 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1376 // from the start of .got section to the first GOT entry for the symbol.
1377 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1378 unsigned int global_gotoffset_;
1380 // Whether all GOT relocations for this symbol are for calls.
1381 bool got_only_for_calls_;
1382 // Whether the symbol has lazy-binding stub.
1383 bool has_lazy_stub_;
1384 // Whether the symbol needs a standard PLT entry.
1385 bool needs_mips_plt_;
1386 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1387 bool needs_comp_plt_;
1388 // Standard PLT entry offset, or -1 if none.
1389 unsigned int mips_plt_offset_;
1390 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1391 unsigned int comp_plt_offset_;
1392 // MIPS16 fn stub for a symbol.
1393 Mips16_stub_section_base* mips16_fn_stub_;
1394 // MIPS16 call stub for a symbol.
1395 Mips16_stub_section_base* mips16_call_stub_;
1396 // MIPS16 call_fp stub for a symbol.
1397 Mips16_stub_section_base* mips16_call_fp_stub_;
1399 bool applied_secondary_got_fixup_;
1402 // Mips16_stub_section class.
1404 // The mips16 compiler uses a couple of special sections to handle
1405 // floating point arguments.
1407 // Section names that look like .mips16.fn.FNNAME contain stubs that
1408 // copy floating point arguments from the fp regs to the gp regs and
1409 // then jump to FNNAME. If any 32 bit function calls FNNAME, the
1410 // call should be redirected to the stub instead. If no 32 bit
1411 // function calls FNNAME, the stub should be discarded. We need to
1412 // consider any reference to the function, not just a call, because
1413 // if the address of the function is taken we will need the stub,
1414 // since the address might be passed to a 32 bit function.
1416 // Section names that look like .mips16.call.FNNAME contain stubs
1417 // that copy floating point arguments from the gp regs to the fp
1418 // regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1419 // then any 16 bit function that calls FNNAME should be redirected
1420 // to the stub instead. If FNNAME is not a 32 bit function, the
1421 // stub should be discarded.
1423 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1424 // which call FNNAME and then copy the return value from the fp regs
1425 // to the gp regs. These stubs store the return address in $18 while
1426 // calling FNNAME; any function which might call one of these stubs
1427 // must arrange to save $18 around the call. (This case is not
1428 // needed for 32 bit functions that call 16 bit functions, because
1429 // 16 bit functions always return floating point values in both
1430 // $f0/$f1 and $2/$3.)
1432 // Note that in all cases FNNAME might be defined statically.
1433 // Therefore, FNNAME is not used literally. Instead, the relocation
1434 // information will indicate which symbol the section is for.
1436 // We record any stubs that we find in the symbol table.
1438 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1440 class Mips16_stub_section_base { };
1442 template<int size, bool big_endian>
1443 class Mips16_stub_section : public Mips16_stub_section_base
1445 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1448 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1449 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1450 found_r_mips_none_(false)
1452 gold_assert(object->is_mips16_fn_stub_section(shndx)
1453 || object->is_mips16_call_stub_section(shndx)
1454 || object->is_mips16_call_fp_stub_section(shndx));
1457 // Return the object of this stub section.
1458 Mips_relobj<size, big_endian>*
1460 { return this->object_; }
1462 // Return the size of a section.
1464 section_size() const
1465 { return this->object_->section_size(this->shndx_); }
1467 // Return section index of this stub section.
1470 { return this->shndx_; }
1472 // Return symbol index, if stub is for a local function.
1475 { return this->r_sym_; }
1477 // Return symbol, if stub is for a global function.
1480 { return this->gsym_; }
1482 // Return whether stub is for a local function.
1484 is_for_local_function() const
1485 { return this->gsym_ == NULL; }
1487 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1488 // is found in the stub section. Try to find stub target.
1490 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1492 // To find target symbol for this stub, trust the first R_MIPS_NONE
1493 // relocation, if any. Otherwise trust the first relocation, whatever
1495 if (this->found_r_mips_none_)
1497 if (r_type == elfcpp::R_MIPS_NONE)
1499 this->r_sym_ = r_sym;
1501 this->found_r_mips_none_ = true;
1503 else if (!is_target_found())
1504 this->r_sym_ = r_sym;
1507 // This method is called when a new relocation R_TYPE for global symbol GSYM
1508 // is found in the stub section. Try to find stub target.
1510 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1512 // To find target symbol for this stub, trust the first R_MIPS_NONE
1513 // relocation, if any. Otherwise trust the first relocation, whatever
1515 if (this->found_r_mips_none_)
1517 if (r_type == elfcpp::R_MIPS_NONE)
1521 this->found_r_mips_none_ = true;
1523 else if (!is_target_found())
1527 // Return whether we found the stub target.
1529 is_target_found() const
1530 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1532 // Return whether this is a fn stub.
1535 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1537 // Return whether this is a call stub.
1539 is_call_stub() const
1540 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1542 // Return whether this is a call_fp stub.
1544 is_call_fp_stub() const
1545 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1547 // Return the output address.
1549 output_address() const
1551 return (this->object_->output_section(this->shndx_)->address()
1552 + this->object_->output_section_offset(this->shndx_));
1556 // The object of this stub section.
1557 Mips_relobj<size, big_endian>* object_;
1558 // The section index of this stub section.
1559 unsigned int shndx_;
1560 // The symbol index, if stub is for a local function.
1561 unsigned int r_sym_;
1562 // The symbol, if stub is for a global function.
1563 Mips_symbol<size>* gsym_;
1564 // True if we found R_MIPS_NONE relocation in this stub.
1565 bool found_r_mips_none_;
1568 // Mips_relobj class.
1570 template<int size, bool big_endian>
1571 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1573 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1574 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1575 Mips16_stubs_int_map;
1576 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1579 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1580 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1581 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1582 processor_specific_flags_(0), local_symbol_is_mips16_(),
1583 local_symbol_is_micromips_(), mips16_stub_sections_(),
1584 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1585 local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1586 got_info_(NULL), section_is_mips16_fn_stub_(),
1587 section_is_mips16_call_stub_(), section_is_mips16_call_fp_stub_(),
1588 pdr_shndx_(-1U), attributes_section_data_(NULL), abiflags_(NULL),
1589 gprmask_(0), cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1591 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1592 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1596 { delete this->attributes_section_data_; }
1598 // Downcast a base pointer to a Mips_relobj pointer. This is
1599 // not type-safe but we only use Mips_relobj not the base class.
1600 static Mips_relobj<size, big_endian>*
1601 as_mips_relobj(Relobj* relobj)
1602 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1604 // Downcast a base pointer to a Mips_relobj pointer. This is
1605 // not type-safe but we only use Mips_relobj not the base class.
1606 static const Mips_relobj<size, big_endian>*
1607 as_mips_relobj(const Relobj* relobj)
1608 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1610 // Processor-specific flags in ELF file header. This is valid only after
1613 processor_specific_flags() const
1614 { return this->processor_specific_flags_; }
1616 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1617 // index. This is only valid after do_count_local_symbol is called.
1619 local_symbol_is_mips16(unsigned int r_sym) const
1621 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1622 return this->local_symbol_is_mips16_[r_sym];
1625 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1626 // index. This is only valid after do_count_local_symbol is called.
1628 local_symbol_is_micromips(unsigned int r_sym) const
1630 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1631 return this->local_symbol_is_micromips_[r_sym];
1634 // Get or create MIPS16 stub section.
1635 Mips16_stub_section<size, big_endian>*
1636 get_mips16_stub_section(unsigned int shndx)
1638 typename Mips16_stubs_int_map::const_iterator it =
1639 this->mips16_stub_sections_.find(shndx);
1640 if (it != this->mips16_stub_sections_.end())
1641 return (*it).second;
1643 Mips16_stub_section<size, big_endian>* stub_section =
1644 new Mips16_stub_section<size, big_endian>(this, shndx);
1645 this->mips16_stub_sections_.insert(
1646 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1647 stub_section->shndx(), stub_section));
1648 return stub_section;
1651 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1652 // object doesn't have fn stub for R_SYM.
1653 Mips16_stub_section<size, big_endian>*
1654 get_local_mips16_fn_stub(unsigned int r_sym) const
1656 typename Mips16_stubs_int_map::const_iterator it =
1657 this->local_mips16_fn_stubs_.find(r_sym);
1658 if (it != this->local_mips16_fn_stubs_.end())
1659 return (*it).second;
1663 // Record that this object has MIPS16 fn stub for local symbol. This method
1664 // is only called if we decided not to discard the stub.
1666 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1668 gold_assert(stub->is_for_local_function());
1669 unsigned int r_sym = stub->r_sym();
1670 this->local_mips16_fn_stubs_.insert(
1671 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1675 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1676 // object doesn't have call stub for R_SYM.
1677 Mips16_stub_section<size, big_endian>*
1678 get_local_mips16_call_stub(unsigned int r_sym) const
1680 typename Mips16_stubs_int_map::const_iterator it =
1681 this->local_mips16_call_stubs_.find(r_sym);
1682 if (it != this->local_mips16_call_stubs_.end())
1683 return (*it).second;
1687 // Record that this object has MIPS16 call stub for local symbol. This method
1688 // is only called if we decided not to discard the stub.
1690 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1692 gold_assert(stub->is_for_local_function());
1693 unsigned int r_sym = stub->r_sym();
1694 this->local_mips16_call_stubs_.insert(
1695 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1699 // Record that we found "non 16-bit" call relocation against local symbol
1700 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1703 add_local_non_16bit_call(unsigned int symndx)
1704 { this->local_non_16bit_calls_.insert(symndx); }
1706 // Return true if there is any "non 16-bit" call relocation against local
1707 // symbol SYMNDX in this object.
1709 has_local_non_16bit_call_relocs(unsigned int symndx)
1711 return (this->local_non_16bit_calls_.find(symndx)
1712 != this->local_non_16bit_calls_.end());
1715 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1716 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1717 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1719 add_local_16bit_call(unsigned int symndx)
1720 { this->local_16bit_calls_.insert(symndx); }
1722 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1723 // symbol SYMNDX in this object.
1725 has_local_16bit_call_relocs(unsigned int symndx)
1727 return (this->local_16bit_calls_.find(symndx)
1728 != this->local_16bit_calls_.end());
1731 // Get gp value that was used to create this object.
1734 { return this->gp_; }
1736 // Return whether the object is a PIC object.
1739 { return this->is_pic_; }
1741 // Return whether the object uses N32 ABI.
1744 { return this->is_n32_; }
1746 // Return whether the object uses N64 ABI.
1749 { return size == 64; }
1751 // Return whether the object uses NewABI conventions.
1754 { return this->is_n32() || this->is_n64(); }
1756 // Return Mips_got_info for this object.
1757 Mips_got_info<size, big_endian>*
1758 get_got_info() const
1759 { return this->got_info_; }
1761 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1762 Mips_got_info<size, big_endian>*
1763 get_or_create_got_info()
1765 if (!this->got_info_)
1766 this->got_info_ = new Mips_got_info<size, big_endian>();
1767 return this->got_info_;
1770 // Set Mips_got_info for this object.
1772 set_got_info(Mips_got_info<size, big_endian>* got_info)
1773 { this->got_info_ = got_info; }
1775 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1776 // after do_read_symbols is called.
1778 is_mips16_stub_section(unsigned int shndx)
1780 return (is_mips16_fn_stub_section(shndx)
1781 || is_mips16_call_stub_section(shndx)
1782 || is_mips16_call_fp_stub_section(shndx));
1785 // Return TRUE if relocations in section SHNDX can refer directly to a
1786 // MIPS16 function rather than to a hard-float stub. This is only valid
1787 // after do_read_symbols is called.
1789 section_allows_mips16_refs(unsigned int shndx)
1791 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1794 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1795 // after do_read_symbols is called.
1797 is_mips16_fn_stub_section(unsigned int shndx)
1799 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1800 return this->section_is_mips16_fn_stub_[shndx];
1803 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1804 // after do_read_symbols is called.
1806 is_mips16_call_stub_section(unsigned int shndx)
1808 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1809 return this->section_is_mips16_call_stub_[shndx];
1812 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1813 // valid after do_read_symbols is called.
1815 is_mips16_call_fp_stub_section(unsigned int shndx)
1817 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1818 return this->section_is_mips16_call_fp_stub_[shndx];
1821 // Discard MIPS16 stub secions that are not needed.
1823 discard_mips16_stub_sections(Symbol_table* symtab);
1825 // Return whether there is a .reginfo section.
1827 has_reginfo_section() const
1828 { return this->has_reginfo_section_; }
1830 // Return gprmask from the .reginfo section of this object.
1833 { return this->gprmask_; }
1835 // Return cprmask1 from the .reginfo section of this object.
1838 { return this->cprmask1_; }
1840 // Return cprmask2 from the .reginfo section of this object.
1843 { return this->cprmask2_; }
1845 // Return cprmask3 from the .reginfo section of this object.
1848 { return this->cprmask3_; }
1850 // Return cprmask4 from the .reginfo section of this object.
1853 { return this->cprmask4_; }
1855 // This is the contents of the .MIPS.abiflags section if there is one.
1856 Mips_abiflags<big_endian>*
1858 { return this->abiflags_; }
1860 // This is the contents of the .gnu.attribute section if there is one.
1861 const Attributes_section_data*
1862 attributes_section_data() const
1863 { return this->attributes_section_data_; }
1866 // Count the local symbols.
1868 do_count_local_symbols(Stringpool_template<char>*,
1869 Stringpool_template<char>*);
1871 // Read the symbol information.
1873 do_read_symbols(Read_symbols_data* sd);
1876 // The name of the options section.
1877 const char* mips_elf_options_section_name()
1878 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1880 // processor-specific flags in ELF file header.
1881 elfcpp::Elf_Word processor_specific_flags_;
1883 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1884 // This is only valid after do_count_local_symbol is called.
1885 std::vector<bool> local_symbol_is_mips16_;
1887 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1888 // This is only valid after do_count_local_symbol is called.
1889 std::vector<bool> local_symbol_is_micromips_;
1891 // Map from section index to the MIPS16 stub for that section. This contains
1892 // all stubs found in this object.
1893 Mips16_stubs_int_map mips16_stub_sections_;
1895 // Local symbols that have "non 16-bit" call relocation. This relocation
1896 // would need to refer to a MIPS16 fn stub, if there is one.
1897 std::set<unsigned int> local_non_16bit_calls_;
1899 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1900 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1901 // relocation that refers to the stub symbol.
1902 std::set<unsigned int> local_16bit_calls_;
1904 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1905 // This contains only the stubs that we decided not to discard.
1906 Mips16_stubs_int_map local_mips16_fn_stubs_;
1908 // Map from local symbol index to the MIPS16 call stub for that symbol.
1909 // This contains only the stubs that we decided not to discard.
1910 Mips16_stubs_int_map local_mips16_call_stubs_;
1912 // gp value that was used to create this object.
1914 // Whether the object is a PIC object.
1916 // Whether the object uses N32 ABI.
1918 // Whether the object contains a .reginfo section.
1919 bool has_reginfo_section_ : 1;
1920 // The Mips_got_info for this object.
1921 Mips_got_info<size, big_endian>* got_info_;
1923 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1924 // This is only valid after do_read_symbols is called.
1925 std::vector<bool> section_is_mips16_fn_stub_;
1927 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1928 // This is only valid after do_read_symbols is called.
1929 std::vector<bool> section_is_mips16_call_stub_;
1931 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1932 // This is only valid after do_read_symbols is called.
1933 std::vector<bool> section_is_mips16_call_fp_stub_;
1935 // .pdr section index.
1936 unsigned int pdr_shndx_;
1938 // Object attributes if there is a .gnu.attributes section or NULL.
1939 Attributes_section_data* attributes_section_data_;
1941 // Object abiflags if there is a .MIPS.abiflags section or NULL.
1942 Mips_abiflags<big_endian>* abiflags_;
1944 // gprmask from the .reginfo section of this object.
1946 // cprmask1 from the .reginfo section of this object.
1948 // cprmask2 from the .reginfo section of this object.
1950 // cprmask3 from the .reginfo section of this object.
1952 // cprmask4 from the .reginfo section of this object.
1956 // Mips_output_data_got class.
1958 template<int size, bool big_endian>
1959 class Mips_output_data_got : public Output_data_got<size, big_endian>
1961 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1962 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1964 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1967 Mips_output_data_got(Target_mips<size, big_endian>* target,
1968 Symbol_table* symtab, Layout* layout)
1969 : Output_data_got<size, big_endian>(), target_(target),
1970 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1971 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1972 secondary_got_relocs_()
1974 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1975 this->set_addralign(16);
1978 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1979 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1981 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1982 unsigned int symndx, Mips_address addend,
1983 unsigned int r_type, unsigned int shndx,
1984 bool is_section_symbol)
1986 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
1991 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
1992 // in OBJECT. FOR_CALL is true if the caller is only interested in
1993 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
1996 record_global_got_symbol(Mips_symbol<size>* mips_sym,
1997 Mips_relobj<size, big_endian>* object,
1998 unsigned int r_type, bool dyn_reloc, bool for_call)
2000 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2001 dyn_reloc, for_call);
2004 // Record that OBJECT has a page relocation against symbol SYMNDX and
2005 // that ADDEND is the addend for that relocation.
2007 record_got_page_entry(Mips_relobj<size, big_endian>* object,
2008 unsigned int symndx, int addend)
2009 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2011 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
2012 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2013 // applied in a static link.
2015 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2016 Mips_symbol<size>* gsym)
2017 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2019 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
2020 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
2021 // relocation that needs to be applied in a static link.
2023 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2024 Sized_relobj_file<size, big_endian>* relobj,
2027 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2031 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2032 // secondary GOT at OFFSET.
2034 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2035 Mips_symbol<size>* gsym)
2037 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2041 // Update GOT entry at OFFSET with VALUE.
2043 update_got_entry(unsigned int offset, Mips_address value)
2045 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2048 // Return the number of entries in local part of the GOT. This includes
2049 // local entries, page entries and 2 reserved entries.
2051 get_local_gotno() const
2053 if (!this->multi_got())
2055 return (2 + this->master_got_info_->local_gotno()
2056 + this->master_got_info_->page_gotno());
2059 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2062 // Return dynamic symbol table index of the first symbol with global GOT
2065 first_global_got_dynsym_index() const
2066 { return this->first_global_got_dynsym_index_; }
2068 // Set dynamic symbol table index of the first symbol with global GOT entry.
2070 set_first_global_got_dynsym_index(unsigned int index)
2071 { this->first_global_got_dynsym_index_ = index; }
2073 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2074 // larger than 64K, create multi-GOT.
2076 lay_out_got(Layout* layout, Symbol_table* symtab,
2077 const Input_objects* input_objects);
2079 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2081 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2083 // Attempt to merge GOTs of different input objects.
2085 merge_gots(const Input_objects* input_objects);
2087 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
2088 // this would lead to overflow, true if they were merged successfully.
2090 merge_got_with(Mips_got_info<size, big_endian>* from,
2091 Mips_relobj<size, big_endian>* object,
2092 Mips_got_info<size, big_endian>* to);
2094 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2095 // use OBJECT's GOT.
2097 get_got_page_offset(Mips_address value,
2098 const Mips_relobj<size, big_endian>* object)
2100 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2101 ? this->master_got_info_
2102 : object->get_got_info());
2103 gold_assert(g != NULL);
2104 return g->get_got_page_offset(value, this);
2107 // Return the GOT offset of type GOT_TYPE of the global symbol
2108 // GSYM. For multi-GOT links, use OBJECT's GOT.
2109 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2110 Mips_relobj<size, big_endian>* object) const
2112 if (!this->multi_got())
2113 return gsym->got_offset(got_type);
2116 Mips_got_info<size, big_endian>* g = object->get_got_info();
2117 gold_assert(g != NULL);
2118 return gsym->got_offset(g->multigot_got_type(got_type));
2122 // Return the GOT offset of type GOT_TYPE of the local symbol
2125 got_offset(unsigned int symndx, unsigned int got_type,
2126 Sized_relobj_file<size, big_endian>* object,
2127 uint64_t addend) const
2128 { return object->local_got_offset(symndx, got_type, addend); }
2130 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2132 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2134 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2135 ? this->master_got_info_
2136 : object->get_got_info());
2137 gold_assert(g != NULL);
2138 return g->tls_ldm_offset();
2141 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2143 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2144 Mips_relobj<size, big_endian>* object)
2146 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2147 ? this->master_got_info_
2148 : object->get_got_info());
2149 gold_assert(g != NULL);
2150 g->set_tls_ldm_offset(tls_ldm_offset);
2153 // Return true for multi-GOT links.
2156 { return this->primary_got_ != NULL; }
2158 // Return the offset of OBJECT's GOT from the start of .got section.
2160 get_got_offset(const Mips_relobj<size, big_endian>* object)
2162 if (!this->multi_got())
2166 Mips_got_info<size, big_endian>* g = object->get_got_info();
2167 return g != NULL ? g->offset() : 0;
2171 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2173 add_reloc_only_entries()
2174 { this->master_got_info_->add_reloc_only_entries(this); }
2176 // Return offset of the primary GOT's entry for global symbol.
2178 get_primary_got_offset(const Mips_symbol<size>* sym) const
2180 gold_assert(sym->global_got_area() != GGA_NONE);
2181 return (this->get_local_gotno() + sym->dynsym_index()
2182 - this->first_global_got_dynsym_index()) * size/8;
2185 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2186 // Input argument GOT_OFFSET is always global offset from the start of
2187 // .got section, for both single and multi-GOT links.
2188 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2189 // links, the return value is object_got_offset - 0x7FF0, where
2190 // object_got_offset is offset in the OBJECT's GOT.
2192 gp_offset(unsigned int got_offset,
2193 const Mips_relobj<size, big_endian>* object) const
2195 return (this->address() + got_offset
2196 - this->target_->adjusted_gp_value(object));
2200 // Write out the GOT table.
2202 do_write(Output_file*);
2206 // This class represent dynamic relocations that need to be applied by
2207 // gold because we are using TLS relocations in a static link.
2211 Static_reloc(unsigned int got_offset, unsigned int r_type,
2212 Mips_symbol<size>* gsym)
2213 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2214 { this->u_.global.symbol = gsym; }
2216 Static_reloc(unsigned int got_offset, unsigned int r_type,
2217 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2218 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2220 this->u_.local.relobj = relobj;
2221 this->u_.local.index = index;
2224 // Return the GOT offset.
2227 { return this->got_offset_; }
2232 { return this->r_type_; }
2234 // Whether the symbol is global or not.
2236 symbol_is_global() const
2237 { return this->symbol_is_global_; }
2239 // For a relocation against a global symbol, the global symbol.
2243 gold_assert(this->symbol_is_global_);
2244 return this->u_.global.symbol;
2247 // For a relocation against a local symbol, the defining object.
2248 Sized_relobj_file<size, big_endian>*
2251 gold_assert(!this->symbol_is_global_);
2252 return this->u_.local.relobj;
2255 // For a relocation against a local symbol, the local symbol index.
2259 gold_assert(!this->symbol_is_global_);
2260 return this->u_.local.index;
2264 // GOT offset of the entry to which this relocation is applied.
2265 unsigned int got_offset_;
2266 // Type of relocation.
2267 unsigned int r_type_;
2268 // Whether this relocation is against a global symbol.
2269 bool symbol_is_global_;
2270 // A global or local symbol.
2275 // For a global symbol, the symbol itself.
2276 Mips_symbol<size>* symbol;
2280 // For a local symbol, the object defining object.
2281 Sized_relobj_file<size, big_endian>* relobj;
2282 // For a local symbol, the symbol index.
2289 Target_mips<size, big_endian>* target_;
2290 // The symbol table.
2291 Symbol_table* symbol_table_;
2294 // Static relocs to be applied to the GOT.
2295 std::vector<Static_reloc> static_relocs_;
2296 // .got section view.
2297 unsigned char* got_view_;
2298 // The dynamic symbol table index of the first symbol with global GOT entry.
2299 unsigned int first_global_got_dynsym_index_;
2300 // The master GOT information.
2301 Mips_got_info<size, big_endian>* master_got_info_;
2302 // The primary GOT information.
2303 Mips_got_info<size, big_endian>* primary_got_;
2304 // Secondary GOT fixups.
2305 std::vector<Static_reloc> secondary_got_relocs_;
2308 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2309 // two ways of creating these interfaces. The first is to add:
2311 // lui $25,%hi(func)
2313 // addiu $25,$25,%lo(func)
2315 // to a separate trampoline section. The second is to add:
2317 // lui $25,%hi(func)
2318 // addiu $25,$25,%lo(func)
2320 // immediately before a PIC function "func", but only if a function is at the
2321 // beginning of the section, and the section is not too heavily aligned (i.e we
2322 // would need to add no more than 2 nops before the stub.)
2324 // We only create stubs of the first type.
2326 template<int size, bool big_endian>
2327 class Mips_output_data_la25_stub : public Output_section_data
2329 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2332 Mips_output_data_la25_stub()
2333 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2336 // Create LA25 stub for a symbol.
2338 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2339 Mips_symbol<size>* gsym);
2341 // Return output address of a stub.
2343 stub_address(const Mips_symbol<size>* sym) const
2345 gold_assert(sym->has_la25_stub());
2346 return this->address() + sym->la25_stub_offset();
2351 do_adjust_output_section(Output_section* os)
2352 { os->set_entsize(0); }
2355 // Template for standard LA25 stub.
2356 static const uint32_t la25_stub_entry[];
2357 // Template for microMIPS LA25 stub.
2358 static const uint32_t la25_stub_micromips_entry[];
2360 // Set the final size.
2362 set_final_data_size()
2363 { this->set_data_size(this->symbols_.size() * 16); }
2365 // Create a symbol for SYM stub's value and size, to help make the
2366 // disassembly easier to read.
2368 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2369 Target_mips<size, big_endian>* target, uint64_t symsize);
2371 // Write to a map file.
2373 do_print_to_mapfile(Mapfile* mapfile) const
2374 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2376 // Write out the LA25 stub section.
2378 do_write(Output_file*);
2380 // Symbols that have LA25 stubs.
2381 std::vector<Mips_symbol<size>*> symbols_;
2384 // MIPS-specific relocation writer.
2386 template<int sh_type, bool dynamic, int size, bool big_endian>
2387 struct Mips_output_reloc_writer;
2389 template<int sh_type, bool dynamic, bool big_endian>
2390 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2392 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2393 typedef std::vector<Output_reloc_type> Relocs;
2396 write(typename Relocs::const_iterator p, unsigned char* pov)
2400 template<int sh_type, bool dynamic, bool big_endian>
2401 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2403 typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2404 typedef std::vector<Output_reloc_type> Relocs;
2407 write(typename Relocs::const_iterator p, unsigned char* pov)
2409 elfcpp::Mips64_rel_write<big_endian> orel(pov);
2410 orel.put_r_offset(p->get_address());
2411 orel.put_r_sym(p->get_symbol_index());
2412 orel.put_r_ssym(RSS_UNDEF);
2413 orel.put_r_type(p->type());
2414 if (p->type() == elfcpp::R_MIPS_REL32)
2415 orel.put_r_type2(elfcpp::R_MIPS_64);
2417 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2418 orel.put_r_type3(elfcpp::R_MIPS_NONE);
2422 template<int sh_type, bool dynamic, int size, bool big_endian>
2423 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2427 Mips_output_data_reloc(bool sort_relocs)
2428 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2432 // Write out the data.
2434 do_write(Output_file* of)
2436 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2438 this->template do_write_generic<Writer>(of);
2443 // A class to handle the PLT data.
2445 template<int size, bool big_endian>
2446 class Mips_output_data_plt : public Output_section_data
2448 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2449 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2450 size, big_endian> Reloc_section;
2453 // Create the PLT section. The ordinary .got section is an argument,
2454 // since we need to refer to the start.
2455 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2456 Target_mips<size, big_endian>* target)
2457 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2458 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2461 this->rel_ = new Reloc_section(false);
2462 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2463 elfcpp::SHF_ALLOC, this->rel_,
2464 ORDER_DYNAMIC_PLT_RELOCS, false);
2467 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2469 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2471 // Return the .rel.plt section data.
2472 const Reloc_section*
2474 { return this->rel_; }
2476 // Return the number of PLT entries.
2479 { return this->symbols_.size(); }
2481 // Return the offset of the first non-reserved PLT entry.
2483 first_plt_entry_offset() const
2484 { return sizeof(plt0_entry_o32); }
2486 // Return the size of a PLT entry.
2488 plt_entry_size() const
2489 { return sizeof(plt_entry); }
2491 // Set final PLT offsets. For each symbol, determine whether standard or
2492 // compressed (MIPS16 or microMIPS) PLT entry is used.
2496 // Return the offset of the first standard PLT entry.
2498 first_mips_plt_offset() const
2499 { return this->plt_header_size_; }
2501 // Return the offset of the first compressed PLT entry.
2503 first_comp_plt_offset() const
2504 { return this->plt_header_size_ + this->plt_mips_offset_; }
2506 // Return whether there are any standard PLT entries.
2508 has_standard_entries() const
2509 { return this->plt_mips_offset_ > 0; }
2511 // Return the output address of standard PLT entry.
2513 mips_entry_address(const Mips_symbol<size>* sym) const
2515 gold_assert (sym->has_mips_plt_offset());
2516 return (this->address() + this->first_mips_plt_offset()
2517 + sym->mips_plt_offset());
2520 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2522 comp_entry_address(const Mips_symbol<size>* sym) const
2524 gold_assert (sym->has_comp_plt_offset());
2525 return (this->address() + this->first_comp_plt_offset()
2526 + sym->comp_plt_offset());
2531 do_adjust_output_section(Output_section* os)
2532 { os->set_entsize(0); }
2534 // Write to a map file.
2536 do_print_to_mapfile(Mapfile* mapfile) const
2537 { mapfile->print_output_data(this, _(".plt")); }
2540 // Template for the first PLT entry.
2541 static const uint32_t plt0_entry_o32[];
2542 static const uint32_t plt0_entry_n32[];
2543 static const uint32_t plt0_entry_n64[];
2544 static const uint32_t plt0_entry_micromips_o32[];
2545 static const uint32_t plt0_entry_micromips32_o32[];
2547 // Template for subsequent PLT entries.
2548 static const uint32_t plt_entry[];
2549 static const uint32_t plt_entry_mips16_o32[];
2550 static const uint32_t plt_entry_micromips_o32[];
2551 static const uint32_t plt_entry_micromips32_o32[];
2553 // Set the final size.
2555 set_final_data_size()
2557 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2558 + this->plt_comp_offset_);
2561 // Write out the PLT data.
2563 do_write(Output_file*);
2565 // Return whether the plt header contains microMIPS code. For the sake of
2566 // cache alignment always use a standard header whenever any standard entries
2567 // are present even if microMIPS entries are present as well. This also lets
2568 // the microMIPS header rely on the value of $v0 only set by microMIPS
2569 // entries, for a small size reduction.
2571 is_plt_header_compressed() const
2573 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2574 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2577 // Return the size of the PLT header.
2579 get_plt_header_size() const
2581 if (this->target_->is_output_n64())
2582 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2583 else if (this->target_->is_output_n32())
2584 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2585 else if (!this->is_plt_header_compressed())
2586 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2587 else if (this->target_->use_32bit_micromips_instructions())
2588 return (2 * sizeof(plt0_entry_micromips32_o32)
2589 / sizeof(plt0_entry_micromips32_o32[0]));
2591 return (2 * sizeof(plt0_entry_micromips_o32)
2592 / sizeof(plt0_entry_micromips_o32[0]));
2595 // Return the PLT header entry.
2597 get_plt_header_entry() const
2599 if (this->target_->is_output_n64())
2600 return plt0_entry_n64;
2601 else if (this->target_->is_output_n32())
2602 return plt0_entry_n32;
2603 else if (!this->is_plt_header_compressed())
2604 return plt0_entry_o32;
2605 else if (this->target_->use_32bit_micromips_instructions())
2606 return plt0_entry_micromips32_o32;
2608 return plt0_entry_micromips_o32;
2611 // Return the size of the standard PLT entry.
2613 standard_plt_entry_size() const
2614 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2616 // Return the size of the compressed PLT entry.
2618 compressed_plt_entry_size() const
2620 gold_assert(!this->target_->is_output_newabi());
2622 if (!this->target_->is_output_micromips())
2623 return (2 * sizeof(plt_entry_mips16_o32)
2624 / sizeof(plt_entry_mips16_o32[0]));
2625 else if (this->target_->use_32bit_micromips_instructions())
2626 return (2 * sizeof(plt_entry_micromips32_o32)
2627 / sizeof(plt_entry_micromips32_o32[0]));
2629 return (2 * sizeof(plt_entry_micromips_o32)
2630 / sizeof(plt_entry_micromips_o32[0]));
2633 // The reloc section.
2634 Reloc_section* rel_;
2635 // The .got.plt section.
2636 Output_data_space* got_plt_;
2637 // Symbols that have PLT entry.
2638 std::vector<Mips_symbol<size>*> symbols_;
2639 // The offset of the next standard PLT entry to create.
2640 unsigned int plt_mips_offset_;
2641 // The offset of the next compressed PLT entry to create.
2642 unsigned int plt_comp_offset_;
2643 // The size of the PLT header in bytes.
2644 unsigned int plt_header_size_;
2646 Target_mips<size, big_endian>* target_;
2649 // A class to handle the .MIPS.stubs data.
2651 template<int size, bool big_endian>
2652 class Mips_output_data_mips_stubs : public Output_section_data
2654 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2656 // Unordered set of .MIPS.stubs entries.
2657 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2658 Mips_stubs_entry_set;
2661 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2662 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2663 stub_offsets_are_set_(false), target_(target)
2666 // Create entry for a symbol.
2668 make_entry(Mips_symbol<size>*);
2670 // Remove entry for a symbol.
2672 remove_entry(Mips_symbol<size>* gsym);
2674 // Set stub offsets for symbols. This method expects that the number of
2675 // entries in dynamic symbol table is set.
2677 set_lazy_stub_offsets();
2680 set_needs_dynsym_value();
2682 // Set the number of entries in dynamic symbol table.
2684 set_dynsym_count(unsigned int dynsym_count)
2685 { this->dynsym_count_ = dynsym_count; }
2687 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2688 // count is greater than 0x10000. If the dynamic symbol count is less than
2689 // 0x10000, the stub will be 4 bytes smaller.
2690 // There's no disadvantage from using microMIPS code here, so for the sake of
2691 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2692 // output produced at all. This has a benefit of stubs being shorter by
2693 // 4 bytes each too, unless in the insn32 mode.
2695 stub_max_size() const
2697 if (!this->target_->is_output_micromips()
2698 || this->target_->use_32bit_micromips_instructions())
2704 // Return the size of the stub. This method expects that the final dynsym
2709 gold_assert(this->dynsym_count_ != -1U);
2710 if (this->dynsym_count_ > 0x10000)
2711 return this->stub_max_size();
2713 return this->stub_max_size() - 4;
2716 // Return output address of a stub.
2718 stub_address(const Mips_symbol<size>* sym) const
2720 gold_assert(sym->has_lazy_stub());
2721 return this->address() + sym->lazy_stub_offset();
2726 do_adjust_output_section(Output_section* os)
2727 { os->set_entsize(0); }
2729 // Write to a map file.
2731 do_print_to_mapfile(Mapfile* mapfile) const
2732 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2735 static const uint32_t lazy_stub_normal_1[];
2736 static const uint32_t lazy_stub_normal_1_n64[];
2737 static const uint32_t lazy_stub_normal_2[];
2738 static const uint32_t lazy_stub_normal_2_n64[];
2739 static const uint32_t lazy_stub_big[];
2740 static const uint32_t lazy_stub_big_n64[];
2742 static const uint32_t lazy_stub_micromips_normal_1[];
2743 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2744 static const uint32_t lazy_stub_micromips_normal_2[];
2745 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2746 static const uint32_t lazy_stub_micromips_big[];
2747 static const uint32_t lazy_stub_micromips_big_n64[];
2749 static const uint32_t lazy_stub_micromips32_normal_1[];
2750 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2751 static const uint32_t lazy_stub_micromips32_normal_2[];
2752 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2753 static const uint32_t lazy_stub_micromips32_big[];
2754 static const uint32_t lazy_stub_micromips32_big_n64[];
2756 // Set the final size.
2758 set_final_data_size()
2759 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2761 // Write out the .MIPS.stubs data.
2763 do_write(Output_file*);
2765 // .MIPS.stubs symbols
2766 Mips_stubs_entry_set symbols_;
2767 // Number of entries in dynamic symbol table.
2768 unsigned int dynsym_count_;
2769 // Whether the stub offsets are set.
2770 bool stub_offsets_are_set_;
2772 Target_mips<size, big_endian>* target_;
2775 // This class handles Mips .reginfo output section.
2777 template<int size, bool big_endian>
2778 class Mips_output_section_reginfo : public Output_section_data
2780 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2783 Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2784 Valtype gprmask, Valtype cprmask1,
2785 Valtype cprmask2, Valtype cprmask3,
2787 : Output_section_data(24, 4, true), target_(target),
2788 gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2789 cprmask3_(cprmask3), cprmask4_(cprmask4)
2793 // Write to a map file.
2795 do_print_to_mapfile(Mapfile* mapfile) const
2796 { mapfile->print_output_data(this, _(".reginfo")); }
2798 // Write out reginfo section.
2800 do_write(Output_file* of);
2803 Target_mips<size, big_endian>* target_;
2805 // gprmask of the output .reginfo section.
2807 // cprmask1 of the output .reginfo section.
2809 // cprmask2 of the output .reginfo section.
2811 // cprmask3 of the output .reginfo section.
2813 // cprmask4 of the output .reginfo section.
2817 // This class handles .MIPS.abiflags output section.
2819 template<int size, bool big_endian>
2820 class Mips_output_section_abiflags : public Output_section_data
2823 Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2824 : Output_section_data(24, 8, true), abiflags_(abiflags)
2828 // Write to a map file.
2830 do_print_to_mapfile(Mapfile* mapfile) const
2831 { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2834 do_write(Output_file* of);
2837 const Mips_abiflags<big_endian>& abiflags_;
2840 // The MIPS target has relocation types which default handling of relocatable
2841 // relocation cannot process. So we have to extend the default code.
2843 template<bool big_endian, typename Classify_reloc>
2844 class Mips_scan_relocatable_relocs :
2845 public Default_scan_relocatable_relocs<Classify_reloc>
2848 // Return the strategy to use for a local symbol which is a section
2849 // symbol, given the relocation type.
2850 inline Relocatable_relocs::Reloc_strategy
2851 local_section_strategy(unsigned int r_type, Relobj* object)
2853 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2854 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2859 case elfcpp::R_MIPS_26:
2860 return Relocatable_relocs::RELOC_SPECIAL;
2863 return Default_scan_relocatable_relocs<Classify_reloc>::
2864 local_section_strategy(r_type, object);
2870 // Mips_copy_relocs class. The only difference from the base class is the
2871 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2872 // Mips cannot convert all relocation types to dynamic relocs. If a reloc
2873 // cannot be made dynamic, a COPY reloc is emitted.
2875 template<int sh_type, int size, bool big_endian>
2876 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2880 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2883 // Emit any saved relocations which turn out to be needed. This is
2884 // called after all the relocs have been scanned.
2886 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2887 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2890 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2893 // Emit this reloc if appropriate. This is called after we have
2894 // scanned all the relocations, so we know whether we emitted a
2895 // COPY relocation for SYM_.
2897 emit_entry(Copy_reloc_entry& entry,
2898 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2899 Symbol_table* symtab, Layout* layout,
2900 Target_mips<size, big_endian>* target);
2904 // Return true if the symbol SYM should be considered to resolve local
2905 // to the current module, and false otherwise. The logic is taken from
2906 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2908 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2909 bool local_protected)
2911 // If it's a local sym, of course we resolve locally.
2915 // STV_HIDDEN or STV_INTERNAL ones must be local.
2916 if (sym->visibility() == elfcpp::STV_HIDDEN
2917 || sym->visibility() == elfcpp::STV_INTERNAL)
2920 // If we don't have a definition in a regular file, then we can't
2921 // resolve locally. The sym is either undefined or dynamic.
2922 if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic()
2923 || sym->is_undefined())
2926 // Forced local symbols resolve locally.
2927 if (sym->is_forced_local())
2930 // As do non-dynamic symbols.
2931 if (!has_dynsym_entry)
2934 // At this point, we know the symbol is defined and dynamic. In an
2935 // executable it must resolve locally, likewise when building symbolic
2936 // shared libraries.
2937 if (parameters->options().output_is_executable()
2938 || parameters->options().Bsymbolic())
2941 // Now deal with defined dynamic symbols in shared libraries. Ones
2942 // with default visibility might not resolve locally.
2943 if (sym->visibility() == elfcpp::STV_DEFAULT)
2946 // STV_PROTECTED non-function symbols are local.
2947 if (sym->type() != elfcpp::STT_FUNC)
2950 // Function pointer equality tests may require that STV_PROTECTED
2951 // symbols be treated as dynamic symbols. If the address of a
2952 // function not defined in an executable is set to that function's
2953 // plt entry in the executable, then the address of the function in
2954 // a shared library must also be the plt entry in the executable.
2955 return local_protected;
2958 // Return TRUE if references to this symbol always reference the symbol in this
2961 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
2963 return symbol_refs_local(sym, has_dynsym_entry, false);
2966 // Return TRUE if calls to this symbol always call the version in this object.
2968 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
2970 return symbol_refs_local(sym, has_dynsym_entry, true);
2973 // Compare GOT offsets of two symbols.
2975 template<int size, bool big_endian>
2977 got_offset_compare(Symbol* sym1, Symbol* sym2)
2979 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
2980 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
2981 unsigned int area1 = mips_sym1->global_got_area();
2982 unsigned int area2 = mips_sym2->global_got_area();
2983 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
2985 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
2987 return area1 < area2;
2989 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
2992 // This method divides dynamic symbols into symbols that have GOT entry, and
2993 // symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
2994 // Mips ABI requires that symbols with the GOT entry must be at the end of
2995 // dynamic symbol table, and the order in dynamic symbol table must match the
2998 template<int size, bool big_endian>
3000 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3001 std::vector<Symbol*>* non_got_symbols,
3002 std::vector<Symbol*>* got_symbols)
3004 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3005 p != dyn_symbols->end();
3008 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3009 if (mips_sym->global_got_area() == GGA_NORMAL
3010 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3011 got_symbols->push_back(mips_sym);
3013 non_got_symbols->push_back(mips_sym);
3016 std::sort(got_symbols->begin(), got_symbols->end(),
3017 got_offset_compare<size, big_endian>);
3020 // Functor class for processing the global symbol table.
3022 template<int size, bool big_endian>
3023 class Symbol_visitor_check_symbols
3026 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3027 Layout* layout, Symbol_table* symtab)
3028 : target_(target), layout_(layout), symtab_(symtab)
3032 operator()(Sized_symbol<size>* sym)
3034 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3035 if (local_pic_function<size, big_endian>(mips_sym))
3037 // SYM is a function that might need $25 to be valid on entry.
3038 // If we're creating a non-PIC relocatable object, mark SYM as
3039 // being PIC. If we're creating a non-relocatable object with
3040 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3042 if (parameters->options().relocatable())
3044 if (!parameters->options().output_is_position_independent())
3045 mips_sym->set_pic();
3047 else if (mips_sym->has_nonpic_branches())
3049 this->target_->la25_stub_section(layout_)
3050 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3056 Target_mips<size, big_endian>* target_;
3058 Symbol_table* symtab_;
3061 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3062 // and endianness. The relocation format for MIPS-64 is non-standard.
3064 template<int sh_type, int size, bool big_endian>
3065 struct Mips_reloc_types;
3067 template<bool big_endian>
3068 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3070 typedef typename elfcpp::Rel<32, big_endian> Reloc;
3071 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3073 static typename elfcpp::Elf_types<32>::Elf_Swxword
3074 get_r_addend(const Reloc*)
3078 set_reloc_addend(Reloc_write*,
3079 typename elfcpp::Elf_types<32>::Elf_Swxword)
3080 { gold_unreachable(); }
3083 template<bool big_endian>
3084 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3086 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3087 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3089 static typename elfcpp::Elf_types<32>::Elf_Swxword
3090 get_r_addend(const Reloc* reloc)
3091 { return reloc->get_r_addend(); }
3094 set_reloc_addend(Reloc_write* p,
3095 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3096 { p->put_r_addend(val); }
3099 template<bool big_endian>
3100 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3102 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3103 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3105 static typename elfcpp::Elf_types<64>::Elf_Swxword
3106 get_r_addend(const Reloc*)
3110 set_reloc_addend(Reloc_write*,
3111 typename elfcpp::Elf_types<64>::Elf_Swxword)
3112 { gold_unreachable(); }
3115 template<bool big_endian>
3116 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3118 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3119 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3121 static typename elfcpp::Elf_types<64>::Elf_Swxword
3122 get_r_addend(const Reloc* reloc)
3123 { return reloc->get_r_addend(); }
3126 set_reloc_addend(Reloc_write* p,
3127 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3128 { p->put_r_addend(val); }
3131 // Forward declaration.
3133 mips_get_size_for_reloc(unsigned int, Relobj*);
3135 // A class for inquiring about properties of a relocation,
3136 // used while scanning relocs during a relocatable link and
3137 // garbage collection.
3139 template<int sh_type_, int size, bool big_endian>
3140 class Mips_classify_reloc;
3142 template<int sh_type_, bool big_endian>
3143 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3144 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3147 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3149 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3152 // Return the symbol referred to by the relocation.
3153 static inline unsigned int
3154 get_r_sym(const Reltype* reloc)
3155 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3157 // Return the type of the relocation.
3158 static inline unsigned int
3159 get_r_type(const Reltype* reloc)
3160 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3162 static inline unsigned int
3163 get_r_type2(const Reltype*)
3166 static inline unsigned int
3167 get_r_type3(const Reltype*)
3170 static inline unsigned int
3171 get_r_ssym(const Reltype*)
3174 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3175 static inline unsigned int
3176 get_r_addend(const Reltype* reloc)
3178 if (sh_type_ == elfcpp::SHT_REL)
3180 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3183 // Write the r_info field to a new reloc, using the r_info field from
3184 // the original reloc, replacing the r_sym field with R_SYM.
3186 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3188 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3189 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3192 // Write the r_addend field to a new reloc.
3194 put_r_addend(Reltype_write* to,
3195 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3196 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3198 // Return the size of the addend of the relocation (only used for SHT_REL).
3200 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3201 { return mips_get_size_for_reloc(r_type, obj); }
3204 template<int sh_type_, bool big_endian>
3205 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3206 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3209 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3211 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3214 // Return the symbol referred to by the relocation.
3215 static inline unsigned int
3216 get_r_sym(const Reltype* reloc)
3217 { return reloc->get_r_sym(); }
3219 // Return the r_type of the relocation.
3220 static inline unsigned int
3221 get_r_type(const Reltype* reloc)
3222 { return reloc->get_r_type(); }
3224 // Return the r_type2 of the relocation.
3225 static inline unsigned int
3226 get_r_type2(const Reltype* reloc)
3227 { return reloc->get_r_type2(); }
3229 // Return the r_type3 of the relocation.
3230 static inline unsigned int
3231 get_r_type3(const Reltype* reloc)
3232 { return reloc->get_r_type3(); }
3234 // Return the special symbol of the relocation.
3235 static inline unsigned int
3236 get_r_ssym(const Reltype* reloc)
3237 { return reloc->get_r_ssym(); }
3239 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3240 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3241 get_r_addend(const Reltype* reloc)
3243 if (sh_type_ == elfcpp::SHT_REL)
3245 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3248 // Write the r_info field to a new reloc, using the r_info field from
3249 // the original reloc, replacing the r_sym field with R_SYM.
3251 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3253 new_reloc->put_r_sym(r_sym);
3254 new_reloc->put_r_ssym(reloc->get_r_ssym());
3255 new_reloc->put_r_type3(reloc->get_r_type3());
3256 new_reloc->put_r_type2(reloc->get_r_type2());
3257 new_reloc->put_r_type(reloc->get_r_type());
3260 // Write the r_addend field to a new reloc.
3262 put_r_addend(Reltype_write* to,
3263 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3264 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3266 // Return the size of the addend of the relocation (only used for SHT_REL).
3268 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3269 { return mips_get_size_for_reloc(r_type, obj); }
3272 template<int size, bool big_endian>
3273 class Target_mips : public Sized_target<size, big_endian>
3275 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3276 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3278 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3279 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3280 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3282 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3286 Target_mips(const Target::Target_info* info = &mips_info)
3287 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3288 got_plt_(NULL), rel_dyn_(NULL), copy_relocs_(), dyn_relocs_(),
3289 la25_stub_(NULL), mips_mach_extensions_(), mips_stubs_(NULL),
3290 attributes_section_data_(NULL), abiflags_(NULL), mach_(0), layout_(NULL),
3291 got16_addends_(), has_abiflags_section_(false),
3292 entry_symbol_is_compressed_(false), insn32_(false)
3294 this->add_machine_extensions();
3297 // The offset of $gp from the beginning of the .got section.
3298 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3300 // The maximum size of the GOT for it to be addressable using 16-bit
3301 // offsets from $gp.
3302 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3304 // Make a new symbol table entry for the Mips target.
3306 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3307 { return new Mips_symbol<size>(); }
3309 // Process the relocations to determine unreferenced sections for
3310 // garbage collection.
3312 gc_process_relocs(Symbol_table* symtab,
3314 Sized_relobj_file<size, big_endian>* object,
3315 unsigned int data_shndx,
3316 unsigned int sh_type,
3317 const unsigned char* prelocs,
3319 Output_section* output_section,
3320 bool needs_special_offset_handling,
3321 size_t local_symbol_count,
3322 const unsigned char* plocal_symbols);
3324 // Scan the relocations to look for symbol adjustments.
3326 scan_relocs(Symbol_table* symtab,
3328 Sized_relobj_file<size, big_endian>* object,
3329 unsigned int data_shndx,
3330 unsigned int sh_type,
3331 const unsigned char* prelocs,
3333 Output_section* output_section,
3334 bool needs_special_offset_handling,
3335 size_t local_symbol_count,
3336 const unsigned char* plocal_symbols);
3338 // Finalize the sections.
3340 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3342 // Relocate a section.
3344 relocate_section(const Relocate_info<size, big_endian>*,
3345 unsigned int sh_type,
3346 const unsigned char* prelocs,
3348 Output_section* output_section,
3349 bool needs_special_offset_handling,
3350 unsigned char* view,
3351 Mips_address view_address,
3352 section_size_type view_size,
3353 const Reloc_symbol_changes*);
3355 // Scan the relocs during a relocatable link.
3357 scan_relocatable_relocs(Symbol_table* symtab,
3359 Sized_relobj_file<size, big_endian>* object,
3360 unsigned int data_shndx,
3361 unsigned int sh_type,
3362 const unsigned char* prelocs,
3364 Output_section* output_section,
3365 bool needs_special_offset_handling,
3366 size_t local_symbol_count,
3367 const unsigned char* plocal_symbols,
3368 Relocatable_relocs*);
3370 // Scan the relocs for --emit-relocs.
3372 emit_relocs_scan(Symbol_table* symtab,
3374 Sized_relobj_file<size, big_endian>* object,
3375 unsigned int data_shndx,
3376 unsigned int sh_type,
3377 const unsigned char* prelocs,
3379 Output_section* output_section,
3380 bool needs_special_offset_handling,
3381 size_t local_symbol_count,
3382 const unsigned char* plocal_syms,
3383 Relocatable_relocs* rr);
3385 // Emit relocations for a section.
3387 relocate_relocs(const Relocate_info<size, big_endian>*,
3388 unsigned int sh_type,
3389 const unsigned char* prelocs,
3391 Output_section* output_section,
3392 typename elfcpp::Elf_types<size>::Elf_Off
3393 offset_in_output_section,
3394 unsigned char* view,
3395 Mips_address view_address,
3396 section_size_type view_size,
3397 unsigned char* reloc_view,
3398 section_size_type reloc_view_size);
3400 // Perform target-specific processing in a relocatable link. This is
3401 // only used if we use the relocation strategy RELOC_SPECIAL.
3403 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3404 unsigned int sh_type,
3405 const unsigned char* preloc_in,
3407 Output_section* output_section,
3408 typename elfcpp::Elf_types<size>::Elf_Off
3409 offset_in_output_section,
3410 unsigned char* view,
3411 Mips_address view_address,
3412 section_size_type view_size,
3413 unsigned char* preloc_out);
3415 // Return whether SYM is defined by the ABI.
3417 do_is_defined_by_abi(const Symbol* sym) const
3419 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3420 || (strcmp(sym->name(), "_gp_disp") == 0)
3421 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3424 // Return the number of entries in the GOT.
3426 got_entry_count() const
3428 if (!this->has_got_section())
3430 return this->got_size() / (size/8);
3433 // Return the number of entries in the PLT.
3435 plt_entry_count() const
3437 if (this->plt_ == NULL)
3439 return this->plt_->entry_count();
3442 // Return the offset of the first non-reserved PLT entry.
3444 first_plt_entry_offset() const
3445 { return this->plt_->first_plt_entry_offset(); }
3447 // Return the size of each PLT entry.
3449 plt_entry_size() const
3450 { return this->plt_->plt_entry_size(); }
3452 // Get the GOT section, creating it if necessary.
3453 Mips_output_data_got<size, big_endian>*
3454 got_section(Symbol_table*, Layout*);
3456 // Get the GOT section.
3457 Mips_output_data_got<size, big_endian>*
3460 gold_assert(this->got_ != NULL);
3464 // Get the .MIPS.stubs section, creating it if necessary.
3465 Mips_output_data_mips_stubs<size, big_endian>*
3466 mips_stubs_section(Layout* layout);
3468 // Get the .MIPS.stubs section.
3469 Mips_output_data_mips_stubs<size, big_endian>*
3470 mips_stubs_section() const
3472 gold_assert(this->mips_stubs_ != NULL);
3473 return this->mips_stubs_;
3476 // Get the LA25 stub section, creating it if necessary.
3477 Mips_output_data_la25_stub<size, big_endian>*
3478 la25_stub_section(Layout*);
3480 // Get the LA25 stub section.
3481 Mips_output_data_la25_stub<size, big_endian>*
3484 gold_assert(this->la25_stub_ != NULL);
3485 return this->la25_stub_;
3488 // Get gp value. It has the value of .got + 0x7FF0.
3492 if (this->gp_ != NULL)
3493 return this->gp_->value();
3497 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3498 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3500 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3502 if (this->gp_ == NULL)
3505 bool multi_got = false;
3506 if (this->has_got_section())
3507 multi_got = this->got_section()->multi_got();
3509 return this->gp_->value();
3511 return this->gp_->value() + this->got_section()->get_got_offset(object);
3514 // Get the dynamic reloc section, creating it if necessary.
3516 rel_dyn_section(Layout*);
3519 do_has_custom_set_dynsym_indexes() const
3522 // Don't emit input .reginfo/.MIPS.abiflags sections to
3523 // output .reginfo/.MIPS.abiflags.
3525 do_should_include_section(elfcpp::Elf_Word sh_type) const
3527 return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3528 && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3531 // Set the dynamic symbol indexes. INDEX is the index of the first
3532 // global dynamic symbol. Pointers to the symbols are stored into the
3533 // vector SYMS. The names are added to DYNPOOL. This returns an
3534 // updated dynamic symbol index.
3536 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3537 std::vector<Symbol*>* syms, Stringpool* dynpool,
3538 Versions* versions, Symbol_table* symtab) const;
3540 // Remove .MIPS.stubs entry for a symbol.
3542 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3544 if (this->mips_stubs_ != NULL)
3545 this->mips_stubs_->remove_entry(sym);
3548 // The value to write into got[1] for SVR4 targets, to identify it is
3549 // a GNU object. The dynamic linker can then use got[1] to store the
3552 mips_elf_gnu_got1_mask()
3554 if (this->is_output_n64())
3555 return (uint64_t)1 << 63;
3560 // Whether the output has microMIPS code. This is valid only after
3561 // merge_obj_e_flags() is called.
3563 is_output_micromips() const
3565 gold_assert(this->are_processor_specific_flags_set());
3566 return elfcpp::is_micromips(this->processor_specific_flags());
3569 // Whether the output uses N32 ABI. This is valid only after
3570 // merge_obj_e_flags() is called.
3572 is_output_n32() const
3574 gold_assert(this->are_processor_specific_flags_set());
3575 return elfcpp::abi_n32(this->processor_specific_flags());
3578 // Whether the output uses N64 ABI.
3580 is_output_n64() const
3581 { return size == 64; }
3583 // Whether the output uses NEWABI. This is valid only after
3584 // merge_obj_e_flags() is called.
3586 is_output_newabi() const
3587 { return this->is_output_n32() || this->is_output_n64(); }
3589 // Whether we can only use 32-bit microMIPS instructions.
3591 use_32bit_micromips_instructions() const
3592 { return this->insn32_; }
3594 // Return the r_sym field from a relocation.
3596 get_r_sym(const unsigned char* preloc) const
3598 // Since REL and RELA relocs share the same structure through
3599 // the r_info field, we can just use REL here.
3600 Reltype rel(preloc);
3601 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3606 // Return the value to use for a dynamic symbol which requires special
3607 // treatment. This is how we support equality comparisons of function
3608 // pointers across shared library boundaries, as described in the
3609 // processor specific ABI supplement.
3611 do_dynsym_value(const Symbol* gsym) const;
3613 // Make an ELF object.
3615 do_make_elf_object(const std::string&, Input_file*, off_t,
3616 const elfcpp::Ehdr<size, big_endian>& ehdr);
3619 do_make_elf_object(const std::string&, Input_file*, off_t,
3620 const elfcpp::Ehdr<size, !big_endian>&)
3621 { gold_unreachable(); }
3623 // Adjust ELF file header.
3625 do_adjust_elf_header(unsigned char* view, int len);
3627 // Get the custom dynamic tag value.
3629 do_dynamic_tag_custom_value(elfcpp::DT) const;
3631 // Adjust the value written to the dynamic symbol table.
3633 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3635 elfcpp::Sym<size, big_endian> isym(view);
3636 elfcpp::Sym_write<size, big_endian> osym(view);
3637 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3639 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3640 // to treat compressed symbols like any other.
3641 Mips_address value = isym.get_st_value();
3642 if (mips_sym->is_mips16() && value != 0)
3644 if (!mips_sym->has_mips16_fn_stub())
3648 // If we have a MIPS16 function with a stub, the dynamic symbol
3649 // must refer to the stub, since only the stub uses the standard
3650 // calling conventions. Stub contains MIPS32 code, so don't add +1
3653 // There is a code which does this in the method
3654 // Target_mips::do_dynsym_value, but that code will only be
3655 // executed if the symbol is from dynobj.
3656 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3659 Mips16_stub_section<size, big_endian>* fn_stub =
3660 mips_sym->template get_mips16_fn_stub<big_endian>();
3661 value = fn_stub->output_address();
3662 osym.put_st_size(fn_stub->section_size());
3665 osym.put_st_value(value);
3666 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3667 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3669 else if ((mips_sym->is_micromips()
3670 // Stubs are always microMIPS if there is any microMIPS code in
3672 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3675 osym.put_st_value(value | 1);
3676 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3677 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3682 // The class which scans relocations.
3690 get_reference_flags(unsigned int r_type);
3693 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3694 Sized_relobj_file<size, big_endian>* object,
3695 unsigned int data_shndx,
3696 Output_section* output_section,
3697 const Reltype& reloc, unsigned int r_type,
3698 const elfcpp::Sym<size, big_endian>& lsym,
3702 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3703 Sized_relobj_file<size, big_endian>* object,
3704 unsigned int data_shndx,
3705 Output_section* output_section,
3706 const Relatype& reloc, unsigned int r_type,
3707 const elfcpp::Sym<size, big_endian>& lsym,
3711 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3712 Sized_relobj_file<size, big_endian>* object,
3713 unsigned int data_shndx,
3714 Output_section* output_section,
3715 const Relatype* rela,
3717 unsigned int rel_type,
3718 unsigned int r_type,
3719 const elfcpp::Sym<size, big_endian>& lsym,
3723 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3724 Sized_relobj_file<size, big_endian>* object,
3725 unsigned int data_shndx,
3726 Output_section* output_section,
3727 const Reltype& reloc, unsigned int r_type,
3731 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3732 Sized_relobj_file<size, big_endian>* object,
3733 unsigned int data_shndx,
3734 Output_section* output_section,
3735 const Relatype& reloc, unsigned int r_type,
3739 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3740 Sized_relobj_file<size, big_endian>* object,
3741 unsigned int data_shndx,
3742 Output_section* output_section,
3743 const Relatype* rela,
3745 unsigned int rel_type,
3746 unsigned int r_type,
3750 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3752 Sized_relobj_file<size, big_endian>*,
3757 const elfcpp::Sym<size, big_endian>&)
3761 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3763 Sized_relobj_file<size, big_endian>*,
3767 unsigned int, Symbol*)
3771 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3773 Sized_relobj_file<size, big_endian>*,
3778 const elfcpp::Sym<size, big_endian>&)
3782 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3784 Sized_relobj_file<size, big_endian>*,
3788 unsigned int, Symbol*)
3792 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3793 unsigned int r_type);
3796 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3797 unsigned int r_type, Symbol*);
3800 // The class which implements relocation.
3810 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3812 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3813 unsigned int r_type,
3814 Output_section* output_section,
3815 Target_mips* target);
3817 // Do a relocation. Return false if the caller should not issue
3818 // any warnings about this relocation.
3820 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3821 Target_mips*, Output_section*, size_t, const unsigned char*,
3822 const Sized_symbol<size>*, const Symbol_value<size>*,
3823 unsigned char*, Mips_address, section_size_type);
3826 // This POD class holds the dynamic relocations that should be emitted instead
3827 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3828 // relocations if it turns out that the symbol does not have static
3833 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3834 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3835 Output_section* output_section, Mips_address r_offset)
3836 : sym_(sym), r_type_(r_type), relobj_(relobj),
3837 shndx_(shndx), output_section_(output_section),
3841 // Emit this reloc if appropriate. This is called after we have
3842 // scanned all the relocations, so we know whether the symbol has
3843 // static relocations.
3845 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3846 Symbol_table* symtab)
3848 if (!this->sym_->has_static_relocs())
3850 got->record_global_got_symbol(this->sym_, this->relobj_,
3851 this->r_type_, true, false);
3852 if (!symbol_references_local(this->sym_,
3853 this->sym_->should_add_dynsym_entry(symtab)))
3854 rel_dyn->add_global(this->sym_, this->r_type_,
3855 this->output_section_, this->relobj_,
3856 this->shndx_, this->r_offset_);
3858 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3859 this->output_section_, this->relobj_,
3860 this->shndx_, this->r_offset_);
3865 Mips_symbol<size>* sym_;
3866 unsigned int r_type_;
3867 Mips_relobj<size, big_endian>* relobj_;
3868 unsigned int shndx_;
3869 Output_section* output_section_;
3870 Mips_address r_offset_;
3873 // Adjust TLS relocation type based on the options and whether this
3874 // is a local symbol.
3875 static tls::Tls_optimization
3876 optimize_tls_reloc(bool is_final, int r_type);
3878 // Return whether there is a GOT section.
3880 has_got_section() const
3881 { return this->got_ != NULL; }
3883 // Check whether the given ELF header flags describe a 32-bit binary.
3885 mips_32bit_flags(elfcpp::Elf_Word);
3888 mach_mips3000 = 3000,
3889 mach_mips3900 = 3900,
3890 mach_mips4000 = 4000,
3891 mach_mips4010 = 4010,
3892 mach_mips4100 = 4100,
3893 mach_mips4111 = 4111,
3894 mach_mips4120 = 4120,
3895 mach_mips4300 = 4300,
3896 mach_mips4400 = 4400,
3897 mach_mips4600 = 4600,
3898 mach_mips4650 = 4650,
3899 mach_mips5000 = 5000,
3900 mach_mips5400 = 5400,
3901 mach_mips5500 = 5500,
3902 mach_mips5900 = 5900,
3903 mach_mips6000 = 6000,
3904 mach_mips7000 = 7000,
3905 mach_mips8000 = 8000,
3906 mach_mips9000 = 9000,
3907 mach_mips10000 = 10000,
3908 mach_mips12000 = 12000,
3909 mach_mips14000 = 14000,
3910 mach_mips16000 = 16000,
3913 mach_mips_loongson_2e = 3001,
3914 mach_mips_loongson_2f = 3002,
3915 mach_mips_loongson_3a = 3003,
3916 mach_mips_sb1 = 12310201, // octal 'SB', 01
3917 mach_mips_octeon = 6501,
3918 mach_mips_octeonp = 6601,
3919 mach_mips_octeon2 = 6502,
3920 mach_mips_octeon3 = 6503,
3921 mach_mips_xlr = 887682, // decimal 'XLR'
3922 mach_mipsisa32 = 32,
3923 mach_mipsisa32r2 = 33,
3924 mach_mipsisa32r3 = 34,
3925 mach_mipsisa32r5 = 36,
3926 mach_mipsisa64 = 64,
3927 mach_mipsisa64r2 = 65,
3928 mach_mipsisa64r3 = 66,
3929 mach_mipsisa64r5 = 68,
3930 mach_mips_micromips = 96
3933 // Return the MACH for a MIPS e_flags value.
3935 elf_mips_mach(elfcpp::Elf_Word);
3937 // Return the MACH for each .MIPS.abiflags ISA Extension.
3939 mips_isa_ext_mach(unsigned int);
3941 // Return the .MIPS.abiflags value representing each ISA Extension.
3943 mips_isa_ext(unsigned int);
3945 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
3947 update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
3948 Mips_abiflags<big_endian>*);
3950 // Infer the content of the ABI flags based on the elf header.
3952 infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
3954 // Create abiflags from elf header or from .MIPS.abiflags section.
3956 create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
3958 // Return the meaning of fp_abi, or "unknown" if not known.
3964 select_fp_abi(const std::string&, int, int);
3966 // Merge attributes from input object.
3968 merge_obj_attributes(const std::string&, const Attributes_section_data*);
3970 // Merge abiflags from input object.
3972 merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
3974 // Check whether machine EXTENSION is an extension of machine BASE.
3976 mips_mach_extends(unsigned int, unsigned int);
3978 // Merge file header flags from input object.
3980 merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
3982 // Encode ISA level and revision as a single value.
3984 level_rev(unsigned char isa_level, unsigned char isa_rev) const
3985 { return (isa_level << 3) | isa_rev; }
3987 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
3992 // True if we are linking for CPUs that are faster if JALR is converted to
3993 // BAL. This should be safe for all architectures. We enable this predicate
3999 // True if we are linking for CPUs that are faster if JR is converted to B.
4000 // This should be safe for all architectures. We enable this predicate for
4006 // Return the size of the GOT section.
4010 gold_assert(this->got_ != NULL);
4011 return this->got_->data_size();
4014 // Create a PLT entry for a global symbol referenced by r_type relocation.
4016 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4017 unsigned int r_type);
4019 // Get the PLT section.
4020 Mips_output_data_plt<size, big_endian>*
4023 gold_assert(this->plt_ != NULL);
4027 // Get the GOT PLT section.
4028 const Mips_output_data_plt<size, big_endian>*
4029 got_plt_section() const
4031 gold_assert(this->got_plt_ != NULL);
4032 return this->got_plt_;
4035 // Copy a relocation against a global symbol.
4037 copy_reloc(Symbol_table* symtab, Layout* layout,
4038 Sized_relobj_file<size, big_endian>* object,
4039 unsigned int shndx, Output_section* output_section,
4040 Symbol* sym, unsigned int r_type, Mips_address r_offset)
4042 this->copy_relocs_.copy_reloc(symtab, layout,
4043 symtab->get_sized_symbol<size>(sym),
4044 object, shndx, output_section,
4045 r_type, r_offset, 0,
4046 this->rel_dyn_section(layout));
4050 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4051 Mips_relobj<size, big_endian>* relobj,
4052 unsigned int shndx, Output_section* output_section,
4053 Mips_address r_offset)
4055 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4056 output_section, r_offset));
4059 // Calculate value of _gp symbol.
4061 set_gp(Layout*, Symbol_table*);
4064 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4066 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4068 // Adds entries that describe how machines relate to one another. The entries
4069 // are ordered topologically with MIPS I extensions listed last. First
4070 // element is extension, second element is base.
4072 add_machine_extensions()
4074 // MIPS64r2 extensions.
4075 this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4076 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4077 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4078 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4079 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64r2);
4081 // MIPS64 extensions.
4082 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4083 this->add_extension(mach_mips_sb1, mach_mipsisa64);
4084 this->add_extension(mach_mips_xlr, mach_mipsisa64);
4086 // MIPS V extensions.
4087 this->add_extension(mach_mipsisa64, mach_mips5);
4089 // R10000 extensions.
4090 this->add_extension(mach_mips12000, mach_mips10000);
4091 this->add_extension(mach_mips14000, mach_mips10000);
4092 this->add_extension(mach_mips16000, mach_mips10000);
4094 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
4095 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
4096 // better to allow vr5400 and vr5500 code to be merged anyway, since
4097 // many libraries will just use the core ISA. Perhaps we could add
4098 // some sort of ASE flag if this ever proves a problem.
4099 this->add_extension(mach_mips5500, mach_mips5400);
4100 this->add_extension(mach_mips5400, mach_mips5000);
4102 // MIPS IV extensions.
4103 this->add_extension(mach_mips5, mach_mips8000);
4104 this->add_extension(mach_mips10000, mach_mips8000);
4105 this->add_extension(mach_mips5000, mach_mips8000);
4106 this->add_extension(mach_mips7000, mach_mips8000);
4107 this->add_extension(mach_mips9000, mach_mips8000);
4109 // VR4100 extensions.
4110 this->add_extension(mach_mips4120, mach_mips4100);
4111 this->add_extension(mach_mips4111, mach_mips4100);
4113 // MIPS III extensions.
4114 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4115 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4116 this->add_extension(mach_mips8000, mach_mips4000);
4117 this->add_extension(mach_mips4650, mach_mips4000);
4118 this->add_extension(mach_mips4600, mach_mips4000);
4119 this->add_extension(mach_mips4400, mach_mips4000);
4120 this->add_extension(mach_mips4300, mach_mips4000);
4121 this->add_extension(mach_mips4100, mach_mips4000);
4122 this->add_extension(mach_mips4010, mach_mips4000);
4123 this->add_extension(mach_mips5900, mach_mips4000);
4125 // MIPS32 extensions.
4126 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4128 // MIPS II extensions.
4129 this->add_extension(mach_mips4000, mach_mips6000);
4130 this->add_extension(mach_mipsisa32, mach_mips6000);
4132 // MIPS I extensions.
4133 this->add_extension(mach_mips6000, mach_mips3000);
4134 this->add_extension(mach_mips3900, mach_mips3000);
4137 // Add value to MIPS extenstions.
4139 add_extension(unsigned int base, unsigned int extension)
4141 std::pair<unsigned int, unsigned int> ext(base, extension);
4142 this->mips_mach_extensions_.push_back(ext);
4145 // Return the number of entries in the .dynsym section.
4146 unsigned int get_dt_mips_symtabno() const
4148 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4149 / elfcpp::Elf_sizes<size>::sym_size));
4150 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4153 // Information about this specific target which we pass to the
4154 // general Target structure.
4155 static const Target::Target_info mips_info;
4157 Mips_output_data_got<size, big_endian>* got_;
4158 // gp symbol. It has the value of .got + 0x7FF0.
4159 Sized_symbol<size>* gp_;
4161 Mips_output_data_plt<size, big_endian>* plt_;
4162 // The GOT PLT section.
4163 Output_data_space* got_plt_;
4164 // The dynamic reloc section.
4165 Reloc_section* rel_dyn_;
4166 // Relocs saved to avoid a COPY reloc.
4167 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4169 // A list of dyn relocs to be saved.
4170 std::vector<Dyn_reloc> dyn_relocs_;
4172 // The LA25 stub section.
4173 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4174 // Architecture extensions.
4175 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4177 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4179 // Attributes section data in output.
4180 Attributes_section_data* attributes_section_data_;
4181 // .MIPS.abiflags section data in output.
4182 Mips_abiflags<big_endian>* abiflags_;
4187 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4189 // Whether there is an input .MIPS.abiflags section.
4190 bool has_abiflags_section_;
4192 // Whether the entry symbol is mips16 or micromips.
4193 bool entry_symbol_is_compressed_;
4195 // Whether we can use only 32-bit microMIPS instructions.
4196 // TODO(sasa): This should be a linker option.
4200 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4201 // It records high part of the relocation pair.
4203 template<int size, bool big_endian>
4206 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4208 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4209 const Symbol_value<size>* _psymval, Mips_address _addend,
4210 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4211 Mips_address _address = 0, bool _gp_disp = false)
4212 : view(_view), object(_object), psymval(_psymval), addend(_addend),
4213 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4214 address(_address), gp_disp(_gp_disp)
4217 unsigned char* view;
4218 const Mips_relobj<size, big_endian>* object;
4219 const Symbol_value<size>* psymval;
4220 Mips_address addend;
4221 unsigned int r_type;
4223 bool extract_addend;
4224 Mips_address address;
4228 template<int size, bool big_endian>
4229 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4231 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4232 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4233 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4234 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4235 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4240 STATUS_OKAY, // No error during relocation.
4241 STATUS_OVERFLOW, // Relocation overflow.
4242 STATUS_BAD_RELOC // Relocation cannot be applied.
4246 typedef Relocate_functions<size, big_endian> Base;
4247 typedef Mips_relocate_functions<size, big_endian> This;
4249 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4250 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4252 template<int valsize>
4253 static inline typename This::Status
4254 check_overflow(Valtype value)
4257 return (Bits<valsize>::has_overflow32(value)
4258 ? This::STATUS_OVERFLOW
4259 : This::STATUS_OKAY);
4261 return (Bits<valsize>::has_overflow(value)
4262 ? This::STATUS_OVERFLOW
4263 : This::STATUS_OKAY);
4267 should_shuffle_micromips_reloc(unsigned int r_type)
4269 return (micromips_reloc(r_type)
4270 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4271 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4275 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4276 // Most mips16 instructions are 16 bits, but these instructions
4279 // The format of these instructions is:
4281 // +--------------+--------------------------------+
4282 // | JALX | X| Imm 20:16 | Imm 25:21 |
4283 // +--------------+--------------------------------+
4284 // | Immediate 15:0 |
4285 // +-----------------------------------------------+
4287 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4288 // Note that the immediate value in the first word is swapped.
4290 // When producing a relocatable object file, R_MIPS16_26 is
4291 // handled mostly like R_MIPS_26. In particular, the addend is
4292 // stored as a straight 26-bit value in a 32-bit instruction.
4293 // (gas makes life simpler for itself by never adjusting a
4294 // R_MIPS16_26 reloc to be against a section, so the addend is
4295 // always zero). However, the 32 bit instruction is stored as 2
4296 // 16-bit values, rather than a single 32-bit value. In a
4297 // big-endian file, the result is the same; in a little-endian
4298 // file, the two 16-bit halves of the 32 bit value are swapped.
4299 // This is so that a disassembler can recognize the jal
4302 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4303 // instruction stored as two 16-bit values. The addend A is the
4304 // contents of the targ26 field. The calculation is the same as
4305 // R_MIPS_26. When storing the calculated value, reorder the
4306 // immediate value as shown above, and don't forget to store the
4307 // value as two 16-bit values.
4309 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4313 // +--------+----------------------+
4317 // +--------+----------------------+
4320 // +----------+------+-------------+
4322 // | sub1 | | sub2 |
4323 // |0 9|10 15|16 31|
4324 // +----------+--------------------+
4325 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4326 // ((sub1 << 16) | sub2)).
4328 // When producing a relocatable object file, the calculation is
4329 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4330 // When producing a fully linked file, the calculation is
4331 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4332 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4334 // The table below lists the other MIPS16 instruction relocations.
4335 // Each one is calculated in the same way as the non-MIPS16 relocation
4336 // given on the right, but using the extended MIPS16 layout of 16-bit
4337 // immediate fields:
4339 // R_MIPS16_GPREL R_MIPS_GPREL16
4340 // R_MIPS16_GOT16 R_MIPS_GOT16
4341 // R_MIPS16_CALL16 R_MIPS_CALL16
4342 // R_MIPS16_HI16 R_MIPS_HI16
4343 // R_MIPS16_LO16 R_MIPS_LO16
4345 // A typical instruction will have a format like this:
4347 // +--------------+--------------------------------+
4348 // | EXTEND | Imm 10:5 | Imm 15:11 |
4349 // +--------------+--------------------------------+
4350 // | Major | rx | ry | Imm 4:0 |
4351 // +--------------+--------------------------------+
4353 // EXTEND is the five bit value 11110. Major is the instruction
4356 // All we need to do here is shuffle the bits appropriately.
4357 // As above, the two 16-bit halves must be swapped on a
4358 // little-endian system.
4360 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4361 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4362 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4365 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4368 if (!mips16_reloc(r_type)
4369 && !should_shuffle_micromips_reloc(r_type))
4372 // Pick up the first and second halfwords of the instruction.
4373 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4374 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4377 if (micromips_reloc(r_type)
4378 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4379 val = first << 16 | second;
4380 else if (r_type != elfcpp::R_MIPS16_26)
4381 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4382 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4384 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4385 | ((first & 0x1f) << 21) | second);
4387 elfcpp::Swap<32, big_endian>::writeval(view, val);
4391 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4393 if (!mips16_reloc(r_type)
4394 && !should_shuffle_micromips_reloc(r_type))
4397 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4398 Valtype16 first, second;
4400 if (micromips_reloc(r_type)
4401 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4403 second = val & 0xffff;
4406 else if (r_type != elfcpp::R_MIPS16_26)
4408 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4409 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4413 second = val & 0xffff;
4414 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4415 | ((val >> 21) & 0x1f);
4418 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4419 elfcpp::Swap<16, big_endian>::writeval(view, first);
4422 // R_MIPS_16: S + sign-extend(A)
4423 static inline typename This::Status
4424 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4425 const Symbol_value<size>* psymval, Mips_address addend_a,
4426 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4428 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4429 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4431 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4434 Valtype x = psymval->value(object, addend);
4435 val = Bits<16>::bit_select32(val, x, 0xffffU);
4439 *calculated_value = x;
4440 return This::STATUS_OKAY;
4443 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4445 return check_overflow<16>(x);
4449 static inline typename This::Status
4450 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4451 const Symbol_value<size>* psymval, Mips_address addend_a,
4452 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4454 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4455 Valtype addend = (extract_addend
4456 ? elfcpp::Swap<32, big_endian>::readval(wv)
4458 Valtype x = psymval->value(object, addend);
4461 *calculated_value = x;
4463 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4465 return This::STATUS_OKAY;
4468 // R_MIPS_JALR, R_MICROMIPS_JALR
4469 static inline typename This::Status
4470 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4471 const Symbol_value<size>* psymval, Mips_address address,
4472 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4473 unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4474 bool calculate_only, Valtype* calculated_value)
4476 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4477 Valtype addend = extract_addend ? 0 : addend_a;
4478 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4480 // Try converting J(AL)R to B(AL), if the target is in range.
4481 if (!parameters->options().relocatable()
4482 && r_type == elfcpp::R_MIPS_JALR
4484 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4485 || (jr_to_b && val == 0x03200008))) // jr t9
4487 int offset = psymval->value(object, addend) - (address + 4);
4488 if (!Bits<18>::has_overflow32(offset))
4490 if (val == 0x03200008) // jr t9
4491 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4493 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4498 *calculated_value = val;
4500 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4502 return This::STATUS_OKAY;
4505 // R_MIPS_PC32: S + A - P
4506 static inline typename This::Status
4507 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4508 const Symbol_value<size>* psymval, Mips_address address,
4509 Mips_address addend_a, bool extract_addend, bool calculate_only,
4510 Valtype* calculated_value)
4512 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4513 Valtype addend = (extract_addend
4514 ? elfcpp::Swap<32, big_endian>::readval(wv)
4516 Valtype x = psymval->value(object, addend) - address;
4519 *calculated_value = x;
4521 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4523 return This::STATUS_OKAY;
4526 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4527 static inline typename This::Status
4528 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4529 const Symbol_value<size>* psymval, Mips_address address,
4530 bool local, Mips_address addend_a, bool extract_addend,
4531 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4532 bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4534 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4535 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4540 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4541 addend = (val & 0x03ffffff) << 1;
4543 addend = (val & 0x03ffffff) << 2;
4548 // Make sure the target of JALX is word-aligned. Bit 0 must be
4549 // the correct ISA mode selector and bit 1 must be 0.
4550 if (!calculate_only && cross_mode_jump
4551 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4553 gold_warning(_("JALX to a non-word-aligned address"));
4554 return This::STATUS_BAD_RELOC;
4557 // Shift is 2, unusually, for microMIPS JALX.
4558 unsigned int shift =
4559 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4563 x = addend | ((address + 4) & (0xfc000000 << shift));
4567 x = Bits<27>::sign_extend32(addend);
4569 x = Bits<28>::sign_extend32(addend);
4571 x = psymval->value(object, x) >> shift;
4573 if (!calculate_only && !local && !gsym->is_weak_undefined())
4575 if ((x >> 26) != ((address + 4) >> (26 + shift)))
4577 gold_error(_("relocation truncated to fit: %u against '%s'"),
4578 r_type, gsym->name());
4579 return This::STATUS_OVERFLOW;
4583 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4585 // If required, turn JAL into JALX.
4586 if (cross_mode_jump)
4589 Valtype32 opcode = val >> 26;
4590 Valtype32 jalx_opcode;
4592 // Check to see if the opcode is already JAL or JALX.
4593 if (r_type == elfcpp::R_MIPS16_26)
4595 ok = (opcode == 0x6) || (opcode == 0x7);
4598 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4600 ok = (opcode == 0x3d) || (opcode == 0x3c);
4605 ok = (opcode == 0x3) || (opcode == 0x1d);
4609 // If the opcode is not JAL or JALX, there's a problem. We cannot
4610 // convert J or JALS to JALX.
4611 if (!calculate_only && !ok)
4613 gold_error(_("Unsupported jump between ISA modes; consider "
4614 "recompiling with interlinking enabled."));
4615 return This::STATUS_BAD_RELOC;
4618 // Make this the JALX opcode.
4619 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4622 // Try converting JAL to BAL, if the target is in range.
4623 if (!parameters->options().relocatable()
4626 && r_type == elfcpp::R_MIPS_26
4627 && (val >> 26) == 0x3))) // jal addr
4629 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4630 int offset = dest - (address + 4);
4631 if (!Bits<18>::has_overflow32(offset))
4633 if (val == 0x03200008) // jr t9
4634 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4636 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4641 *calculated_value = val;
4643 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4645 return This::STATUS_OKAY;
4649 static inline typename This::Status
4650 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4651 const Symbol_value<size>* psymval, Mips_address address,
4652 Mips_address addend_a, bool extract_addend, bool calculate_only,
4653 Valtype* calculated_value)
4655 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4656 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4658 Valtype addend = (extract_addend
4659 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4662 Valtype x = psymval->value(object, addend) - address;
4663 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4667 *calculated_value = x >> 2;
4668 return This::STATUS_OKAY;
4671 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4673 return check_overflow<18>(x);
4676 // R_MICROMIPS_PC7_S1
4677 static inline typename This::Status
4678 relmicromips_pc7_s1(unsigned char* view,
4679 const Mips_relobj<size, big_endian>* object,
4680 const Symbol_value<size>* psymval, Mips_address address,
4681 Mips_address addend_a, bool extract_addend,
4682 bool calculate_only, Valtype* calculated_value)
4684 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4685 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4687 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4690 Valtype x = psymval->value(object, addend) - address;
4691 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4695 *calculated_value = x >> 1;
4696 return This::STATUS_OKAY;
4699 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4701 return check_overflow<8>(x);
4704 // R_MICROMIPS_PC10_S1
4705 static inline typename This::Status
4706 relmicromips_pc10_s1(unsigned char* view,
4707 const Mips_relobj<size, big_endian>* object,
4708 const Symbol_value<size>* psymval, Mips_address address,
4709 Mips_address addend_a, bool extract_addend,
4710 bool calculate_only, Valtype* calculated_value)
4712 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4713 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4715 Valtype addend = (extract_addend
4716 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4719 Valtype x = psymval->value(object, addend) - address;
4720 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
4724 *calculated_value = x >> 1;
4725 return This::STATUS_OKAY;
4728 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4730 return check_overflow<11>(x);
4733 // R_MICROMIPS_PC16_S1
4734 static inline typename This::Status
4735 relmicromips_pc16_s1(unsigned char* view,
4736 const Mips_relobj<size, big_endian>* object,
4737 const Symbol_value<size>* psymval, Mips_address address,
4738 Mips_address addend_a, bool extract_addend,
4739 bool calculate_only, Valtype* calculated_value)
4741 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4742 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4744 Valtype addend = (extract_addend
4745 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
4748 Valtype x = psymval->value(object, addend) - address;
4749 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
4753 *calculated_value = x >> 1;
4754 return This::STATUS_OKAY;
4757 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4759 return check_overflow<17>(x);
4762 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4763 static inline typename This::Status
4764 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4765 const Symbol_value<size>* psymval, Mips_address addend,
4766 Mips_address address, bool gp_disp, unsigned int r_type,
4767 unsigned int r_sym, bool extract_addend)
4769 // Record the relocation. It will be resolved when we find lo16 part.
4770 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4771 addend, r_type, r_sym, extract_addend, address,
4773 return This::STATUS_OKAY;
4776 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4777 static inline typename This::Status
4778 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4779 const Symbol_value<size>* psymval, Mips_address addend_hi,
4780 Mips_address address, bool is_gp_disp, unsigned int r_type,
4781 bool extract_addend, Valtype32 addend_lo,
4782 Target_mips<size, big_endian>* target, bool calculate_only,
4783 Valtype* calculated_value)
4785 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4786 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4788 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4793 value = psymval->value(object, addend);
4796 // For MIPS16 ABI code we generate this sequence
4797 // 0: li $v0,%hi(_gp_disp)
4798 // 4: addiupc $v1,%lo(_gp_disp)
4802 // So the offsets of hi and lo relocs are the same, but the
4803 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
4804 // ADDIUPC clears the low two bits of the instruction address,
4805 // so the base is ($t9 + 4) & ~3.
4807 if (r_type == elfcpp::R_MIPS16_HI16)
4808 gp_disp = (target->adjusted_gp_value(object)
4809 - ((address + 4) & ~0x3));
4810 // The microMIPS .cpload sequence uses the same assembly
4811 // instructions as the traditional psABI version, but the
4812 // incoming $t9 has the low bit set.
4813 else if (r_type == elfcpp::R_MICROMIPS_HI16)
4814 gp_disp = target->adjusted_gp_value(object) - address - 1;
4816 gp_disp = target->adjusted_gp_value(object) - address;
4817 value = gp_disp + addend;
4819 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4820 val = Bits<32>::bit_select32(val, x, 0xffff);
4824 *calculated_value = x;
4825 return This::STATUS_OKAY;
4828 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4830 return (is_gp_disp ? check_overflow<16>(x)
4831 : This::STATUS_OKAY);
4834 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4835 static inline typename This::Status
4836 relgot16_local(unsigned char* view,
4837 const Mips_relobj<size, big_endian>* object,
4838 const Symbol_value<size>* psymval, Mips_address addend_a,
4839 bool extract_addend, unsigned int r_type, unsigned int r_sym)
4841 // Record the relocation. It will be resolved when we find lo16 part.
4842 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4843 addend_a, r_type, r_sym, extract_addend));
4844 return This::STATUS_OKAY;
4847 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4848 static inline typename This::Status
4849 do_relgot16_local(unsigned char* view,
4850 const Mips_relobj<size, big_endian>* object,
4851 const Symbol_value<size>* psymval, Mips_address addend_hi,
4852 bool extract_addend, Valtype32 addend_lo,
4853 Target_mips<size, big_endian>* target, bool calculate_only,
4854 Valtype* calculated_value)
4856 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4857 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4859 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4862 // Find GOT page entry.
4863 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
4866 unsigned int got_offset =
4867 target->got_section()->get_got_page_offset(value, object);
4869 // Resolve the relocation.
4870 Valtype x = target->got_section()->gp_offset(got_offset, object);
4871 val = Bits<32>::bit_select32(val, x, 0xffff);
4875 *calculated_value = x;
4876 return This::STATUS_OKAY;
4879 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4881 return check_overflow<16>(x);
4884 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
4885 static inline typename This::Status
4886 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
4887 const Mips_relobj<size, big_endian>* object,
4888 const Symbol_value<size>* psymval, Mips_address addend_a,
4889 bool extract_addend, Mips_address address, bool is_gp_disp,
4890 unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
4891 bool calculate_only, Valtype* calculated_value)
4893 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4894 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4896 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4899 if (rel_type == elfcpp::SHT_REL)
4901 typename This::Status reloc_status = This::STATUS_OKAY;
4902 // Resolve pending R_MIPS_HI16 relocations.
4903 typename std::list<reloc_high<size, big_endian> >::iterator it =
4904 hi16_relocs.begin();
4905 while (it != hi16_relocs.end())
4907 reloc_high<size, big_endian> hi16 = *it;
4908 if (hi16.r_sym == r_sym
4909 && is_matching_lo16_reloc(hi16.r_type, r_type))
4911 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
4912 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
4913 hi16.addend, hi16.address, hi16.gp_disp,
4914 hi16.r_type, hi16.extract_addend, addend,
4915 target, calculate_only, calculated_value);
4916 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
4917 if (reloc_status == This::STATUS_OVERFLOW)
4918 return This::STATUS_OVERFLOW;
4919 it = hi16_relocs.erase(it);
4925 // Resolve pending local R_MIPS_GOT16 relocations.
4926 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
4927 got16_relocs.begin();
4928 while (it2 != got16_relocs.end())
4930 reloc_high<size, big_endian> got16 = *it2;
4931 if (got16.r_sym == r_sym
4932 && is_matching_lo16_reloc(got16.r_type, r_type))
4934 mips_reloc_unshuffle(got16.view, got16.r_type, false);
4936 reloc_status = do_relgot16_local(got16.view, got16.object,
4937 got16.psymval, got16.addend,
4938 got16.extract_addend, addend, target,
4939 calculate_only, calculated_value);
4941 mips_reloc_shuffle(got16.view, got16.r_type, false);
4942 if (reloc_status == This::STATUS_OVERFLOW)
4943 return This::STATUS_OVERFLOW;
4944 it2 = got16_relocs.erase(it2);
4951 // Resolve R_MIPS_LO16 relocation.
4954 x = psymval->value(object, addend);
4957 // See the comment for R_MIPS16_HI16 above for the reason
4958 // for this conditional.
4960 if (r_type == elfcpp::R_MIPS16_LO16)
4961 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
4962 else if (r_type == elfcpp::R_MICROMIPS_LO16
4963 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
4964 gp_disp = target->adjusted_gp_value(object) - address + 3;
4966 gp_disp = target->adjusted_gp_value(object) - address + 4;
4967 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
4968 // for overflow. Relocations against _gp_disp are normally
4969 // generated from the .cpload pseudo-op. It generates code
4970 // that normally looks like this:
4972 // lui $gp,%hi(_gp_disp)
4973 // addiu $gp,$gp,%lo(_gp_disp)
4976 // Here $t9 holds the address of the function being called,
4977 // as required by the MIPS ELF ABI. The R_MIPS_LO16
4978 // relocation can easily overflow in this situation, but the
4979 // R_MIPS_HI16 relocation will handle the overflow.
4980 // Therefore, we consider this a bug in the MIPS ABI, and do
4981 // not check for overflow here.
4982 x = gp_disp + addend;
4984 val = Bits<32>::bit_select32(val, x, 0xffff);
4987 *calculated_value = x;
4989 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4991 return This::STATUS_OKAY;
4994 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
4995 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4996 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
4997 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
4998 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
4999 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5000 static inline typename This::Status
5001 relgot(unsigned char* view, int gp_offset, bool calculate_only,
5002 Valtype* calculated_value)
5004 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5005 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5006 Valtype x = gp_offset;
5007 val = Bits<32>::bit_select32(val, x, 0xffff);
5011 *calculated_value = x;
5012 return This::STATUS_OKAY;
5015 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5017 return check_overflow<16>(x);
5021 static inline typename This::Status
5022 releh(unsigned char* view, int gp_offset, bool calculate_only,
5023 Valtype* calculated_value)
5025 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5026 Valtype x = gp_offset;
5030 *calculated_value = x;
5031 return This::STATUS_OKAY;
5034 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5036 return check_overflow<32>(x);
5039 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5040 static inline typename This::Status
5041 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5042 const Mips_relobj<size, big_endian>* object,
5043 const Symbol_value<size>* psymval, Mips_address addend_a,
5044 bool extract_addend, bool calculate_only,
5045 Valtype* calculated_value)
5047 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5048 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5049 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5051 // Find a GOT page entry that points to within 32KB of symbol + addend.
5052 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5053 unsigned int got_offset =
5054 target->got_section()->get_got_page_offset(value, object);
5056 Valtype x = target->got_section()->gp_offset(got_offset, object);
5057 val = Bits<32>::bit_select32(val, x, 0xffff);
5061 *calculated_value = x;
5062 return This::STATUS_OKAY;
5065 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5067 return check_overflow<16>(x);
5070 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5071 static inline typename This::Status
5072 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5073 const Mips_relobj<size, big_endian>* object,
5074 const Symbol_value<size>* psymval, Mips_address addend_a,
5075 bool extract_addend, bool local, bool calculate_only,
5076 Valtype* calculated_value)
5078 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5079 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5080 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5082 // For a local symbol, find a GOT page entry that points to within 32KB of
5083 // symbol + addend. Relocation value is the offset of the GOT page entry's
5084 // value from symbol + addend.
5085 // For a global symbol, relocation value is addend.
5089 // Find GOT page entry.
5090 Mips_address value = ((psymval->value(object, addend) + 0x8000)
5092 target->got_section()->get_got_page_offset(value, object);
5094 x = psymval->value(object, addend) - value;
5098 val = Bits<32>::bit_select32(val, x, 0xffff);
5102 *calculated_value = x;
5103 return This::STATUS_OKAY;
5106 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5108 return check_overflow<16>(x);
5111 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5112 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5113 static inline typename This::Status
5114 relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5115 Valtype* calculated_value)
5117 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5118 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5119 Valtype x = gp_offset;
5120 x = ((x + 0x8000) >> 16) & 0xffff;
5121 val = Bits<32>::bit_select32(val, x, 0xffff);
5124 *calculated_value = x;
5126 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5128 return This::STATUS_OKAY;
5131 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5132 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5133 static inline typename This::Status
5134 relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5135 Valtype* calculated_value)
5137 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5138 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5139 Valtype x = gp_offset;
5140 val = Bits<32>::bit_select32(val, x, 0xffff);
5143 *calculated_value = x;
5145 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5147 return This::STATUS_OKAY;
5150 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5151 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5152 static inline typename This::Status
5153 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5154 const Symbol_value<size>* psymval, Mips_address gp,
5155 Mips_address addend_a, bool extract_addend, bool local,
5156 unsigned int r_type, bool calculate_only,
5157 Valtype* calculated_value)
5159 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5160 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5165 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5166 addend = (val & 0x7f) << 2;
5168 addend = val & 0xffff;
5169 // Only sign-extend the addend if it was extracted from the
5170 // instruction. If the addend was separate, leave it alone,
5171 // otherwise we may lose significant bits.
5172 addend = Bits<16>::sign_extend32(addend);
5177 Valtype x = psymval->value(object, addend) - gp;
5179 // If the symbol was local, any earlier relocatable links will
5180 // have adjusted its addend with the gp offset, so compensate
5181 // for that now. Don't do it for symbols forced local in this
5182 // link, though, since they won't have had the gp offset applied
5185 x += object->gp_value();
5187 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5188 val = Bits<32>::bit_select32(val, x, 0x7f);
5190 val = Bits<32>::bit_select32(val, x, 0xffff);
5194 *calculated_value = x;
5195 return This::STATUS_OKAY;
5198 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5200 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5202 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5203 "limit (see option -G)"));
5204 return This::STATUS_OVERFLOW;
5206 return This::STATUS_OKAY;
5210 static inline typename This::Status
5211 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5212 const Symbol_value<size>* psymval, Mips_address gp,
5213 Mips_address addend_a, bool extract_addend, bool calculate_only,
5214 Valtype* calculated_value)
5216 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5217 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5218 Valtype addend = extract_addend ? val : addend_a;
5220 // R_MIPS_GPREL32 relocations are defined for local symbols only.
5221 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5224 *calculated_value = x;
5226 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5228 return This::STATUS_OKAY;
5231 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5232 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5233 // R_MICROMIPS_TLS_DTPREL_HI16
5234 static inline typename This::Status
5235 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5236 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5237 Mips_address addend_a, bool extract_addend, bool calculate_only,
5238 Valtype* calculated_value)
5240 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5241 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5242 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5244 // tls symbol values are relative to tls_segment()->vaddr()
5245 Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5246 val = Bits<32>::bit_select32(val, x, 0xffff);
5249 *calculated_value = x;
5251 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5253 return This::STATUS_OKAY;
5256 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5257 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5258 // R_MICROMIPS_TLS_DTPREL_LO16,
5259 static inline typename This::Status
5260 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5261 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5262 Mips_address addend_a, bool extract_addend, bool calculate_only,
5263 Valtype* calculated_value)
5265 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5266 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5267 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5269 // tls symbol values are relative to tls_segment()->vaddr()
5270 Valtype x = psymval->value(object, addend) - tp_offset;
5271 val = Bits<32>::bit_select32(val, x, 0xffff);
5274 *calculated_value = x;
5276 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5278 return This::STATUS_OKAY;
5281 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5282 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5283 static inline typename This::Status
5284 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5285 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5286 Mips_address addend_a, bool extract_addend, bool calculate_only,
5287 Valtype* calculated_value)
5289 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5290 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5291 Valtype addend = extract_addend ? val : addend_a;
5293 // tls symbol values are relative to tls_segment()->vaddr()
5294 Valtype x = psymval->value(object, addend) - tp_offset;
5297 *calculated_value = x;
5299 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5301 return This::STATUS_OKAY;
5304 // R_MIPS_SUB, R_MICROMIPS_SUB
5305 static inline typename This::Status
5306 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5307 const Symbol_value<size>* psymval, Mips_address addend_a,
5308 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5310 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5311 Valtype64 addend = (extract_addend
5312 ? elfcpp::Swap<64, big_endian>::readval(wv)
5315 Valtype64 x = psymval->value(object, -addend);
5317 *calculated_value = x;
5319 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5321 return This::STATUS_OKAY;
5325 static inline typename This::Status
5326 rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5327 const Symbol_value<size>* psymval, Mips_address addend_a,
5328 bool extract_addend, bool calculate_only, Valtype* calculated_value,
5329 bool apply_addend_only)
5331 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5332 Valtype64 addend = (extract_addend
5333 ? elfcpp::Swap<64, big_endian>::readval(wv)
5336 Valtype64 x = psymval->value(object, addend);
5338 *calculated_value = x;
5341 if (apply_addend_only)
5343 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5346 return This::STATUS_OKAY;
5351 template<int size, bool big_endian>
5352 typename std::list<reloc_high<size, big_endian> >
5353 Mips_relocate_functions<size, big_endian>::hi16_relocs;
5355 template<int size, bool big_endian>
5356 typename std::list<reloc_high<size, big_endian> >
5357 Mips_relocate_functions<size, big_endian>::got16_relocs;
5359 // Mips_got_info methods.
5361 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5362 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5364 template<int size, bool big_endian>
5366 Mips_got_info<size, big_endian>::record_local_got_symbol(
5367 Mips_relobj<size, big_endian>* object, unsigned int symndx,
5368 Mips_address addend, unsigned int r_type, unsigned int shndx,
5369 bool is_section_symbol)
5371 Mips_got_entry<size, big_endian>* entry =
5372 new Mips_got_entry<size, big_endian>(object, symndx, addend,
5373 mips_elf_reloc_tls_type(r_type),
5374 shndx, is_section_symbol);
5375 this->record_got_entry(entry, object);
5378 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5379 // in OBJECT. FOR_CALL is true if the caller is only interested in
5380 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5383 template<int size, bool big_endian>
5385 Mips_got_info<size, big_endian>::record_global_got_symbol(
5386 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5387 unsigned int r_type, bool dyn_reloc, bool for_call)
5390 mips_sym->set_got_not_only_for_calls();
5392 // A global symbol in the GOT must also be in the dynamic symbol table.
5393 if (!mips_sym->needs_dynsym_entry())
5395 switch (mips_sym->visibility())
5397 case elfcpp::STV_INTERNAL:
5398 case elfcpp::STV_HIDDEN:
5399 mips_sym->set_is_forced_local();
5402 mips_sym->set_needs_dynsym_entry();
5407 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5408 if (tls_type == GOT_TLS_NONE)
5409 this->global_got_symbols_.insert(mips_sym);
5413 if (mips_sym->global_got_area() == GGA_NONE)
5414 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5418 Mips_got_entry<size, big_endian>* entry =
5419 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5421 this->record_got_entry(entry, object);
5424 // Add ENTRY to master GOT and to OBJECT's GOT.
5426 template<int size, bool big_endian>
5428 Mips_got_info<size, big_endian>::record_got_entry(
5429 Mips_got_entry<size, big_endian>* entry,
5430 Mips_relobj<size, big_endian>* object)
5432 this->got_entries_.insert(entry);
5434 // Create the GOT entry for the OBJECT's GOT.
5435 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5436 Mips_got_entry<size, big_endian>* entry2 =
5437 new Mips_got_entry<size, big_endian>(*entry);
5439 g->got_entries_.insert(entry2);
5442 // Record that OBJECT has a page relocation against symbol SYMNDX and
5443 // that ADDEND is the addend for that relocation.
5444 // This function creates an upper bound on the number of GOT slots
5445 // required; no attempt is made to combine references to non-overridable
5446 // global symbols across multiple input files.
5448 template<int size, bool big_endian>
5450 Mips_got_info<size, big_endian>::record_got_page_entry(
5451 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5453 struct Got_page_range **range_ptr, *range;
5454 int old_pages, new_pages;
5456 // Find the Got_page_entry for this symbol.
5457 Got_page_entry* entry = new Got_page_entry(object, symndx);
5458 typename Got_page_entry_set::iterator it =
5459 this->got_page_entries_.find(entry);
5460 if (it != this->got_page_entries_.end())
5463 this->got_page_entries_.insert(entry);
5465 // Add the same entry to the OBJECT's GOT.
5466 Got_page_entry* entry2 = NULL;
5467 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5468 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5470 entry2 = new Got_page_entry(*entry);
5471 g2->got_page_entries_.insert(entry2);
5474 // Skip over ranges whose maximum extent cannot share a page entry
5476 range_ptr = &entry->ranges;
5477 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5478 range_ptr = &(*range_ptr)->next;
5480 // If we scanned to the end of the list, or found a range whose
5481 // minimum extent cannot share a page entry with ADDEND, create
5482 // a new singleton range.
5484 if (!range || addend < range->min_addend - 0xffff)
5486 range = new Got_page_range();
5487 range->next = *range_ptr;
5488 range->min_addend = addend;
5489 range->max_addend = addend;
5494 ++entry2->num_pages;
5495 ++this->page_gotno_;
5500 // Remember how many pages the old range contributed.
5501 old_pages = range->get_max_pages();
5503 // Update the ranges.
5504 if (addend < range->min_addend)
5505 range->min_addend = addend;
5506 else if (addend > range->max_addend)
5508 if (range->next && addend >= range->next->min_addend - 0xffff)
5510 old_pages += range->next->get_max_pages();
5511 range->max_addend = range->next->max_addend;
5512 range->next = range->next->next;
5515 range->max_addend = addend;
5518 // Record any change in the total estimate.
5519 new_pages = range->get_max_pages();
5520 if (old_pages != new_pages)
5522 entry->num_pages += new_pages - old_pages;
5524 entry2->num_pages += new_pages - old_pages;
5525 this->page_gotno_ += new_pages - old_pages;
5526 g2->page_gotno_ += new_pages - old_pages;
5530 // Create all entries that should be in the local part of the GOT.
5532 template<int size, bool big_endian>
5534 Mips_got_info<size, big_endian>::add_local_entries(
5535 Target_mips<size, big_endian>* target, Layout* layout)
5537 Mips_output_data_got<size, big_endian>* got = target->got_section();
5538 // First two GOT entries are reserved. The first entry will be filled at
5539 // runtime. The second entry will be used by some runtime loaders.
5540 got->add_constant(0);
5541 got->add_constant(target->mips_elf_gnu_got1_mask());
5543 for (typename Got_entry_set::iterator
5544 p = this->got_entries_.begin();
5545 p != this->got_entries_.end();
5548 Mips_got_entry<size, big_endian>* entry = *p;
5549 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5551 got->add_local(entry->object(), entry->symndx(),
5552 GOT_TYPE_STANDARD, entry->addend());
5553 unsigned int got_offset = entry->object()->local_got_offset(
5554 entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5555 if (got->multi_got() && this->index_ > 0
5556 && parameters->options().output_is_position_independent())
5558 if (!entry->is_section_symbol())
5559 target->rel_dyn_section(layout)->add_local(entry->object(),
5560 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5562 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5563 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5569 this->add_page_entries(target, layout);
5571 // Add global entries that should be in the local area.
5572 for (typename Got_entry_set::iterator
5573 p = this->got_entries_.begin();
5574 p != this->got_entries_.end();
5577 Mips_got_entry<size, big_endian>* entry = *p;
5578 if (!entry->is_for_global_symbol())
5581 Mips_symbol<size>* mips_sym = entry->sym();
5582 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5584 unsigned int got_type;
5585 if (!got->multi_got())
5586 got_type = GOT_TYPE_STANDARD;
5588 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5589 if (got->add_global(mips_sym, got_type))
5591 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5592 if (got->multi_got() && this->index_ > 0
5593 && parameters->options().output_is_position_independent())
5594 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5595 mips_sym, elfcpp::R_MIPS_REL32, got,
5596 mips_sym->got_offset(got_type));
5602 // Create GOT page entries.
5604 template<int size, bool big_endian>
5606 Mips_got_info<size, big_endian>::add_page_entries(
5607 Target_mips<size, big_endian>* target, Layout* layout)
5609 if (this->page_gotno_ == 0)
5612 Mips_output_data_got<size, big_endian>* got = target->got_section();
5613 this->got_page_offset_start_ = got->add_constant(0);
5614 if (got->multi_got() && this->index_ > 0
5615 && parameters->options().output_is_position_independent())
5616 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5617 this->got_page_offset_start_);
5618 int num_entries = this->page_gotno_;
5619 unsigned int prev_offset = this->got_page_offset_start_;
5620 while (--num_entries > 0)
5622 unsigned int next_offset = got->add_constant(0);
5623 if (got->multi_got() && this->index_ > 0
5624 && parameters->options().output_is_position_independent())
5625 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5627 gold_assert(next_offset == prev_offset + size/8);
5628 prev_offset = next_offset;
5630 this->got_page_offset_next_ = this->got_page_offset_start_;
5633 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5635 template<int size, bool big_endian>
5637 Mips_got_info<size, big_endian>::add_global_entries(
5638 Target_mips<size, big_endian>* target, Layout* layout,
5639 unsigned int non_reloc_only_global_gotno)
5641 Mips_output_data_got<size, big_endian>* got = target->got_section();
5642 // Add GGA_NORMAL entries.
5643 unsigned int count = 0;
5644 for (typename Got_entry_set::iterator
5645 p = this->got_entries_.begin();
5646 p != this->got_entries_.end();
5649 Mips_got_entry<size, big_endian>* entry = *p;
5650 if (!entry->is_for_global_symbol())
5653 Mips_symbol<size>* mips_sym = entry->sym();
5654 if (mips_sym->global_got_area() != GGA_NORMAL)
5657 unsigned int got_type;
5658 if (!got->multi_got())
5659 got_type = GOT_TYPE_STANDARD;
5661 // In multi-GOT links, global symbol can be in both primary and
5662 // secondary GOT(s). By creating custom GOT type
5663 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5664 // is added to secondary GOT(s).
5665 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5666 if (!got->add_global(mips_sym, got_type))
5669 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5670 if (got->multi_got() && this->index_ == 0)
5672 if (got->multi_got() && this->index_ > 0)
5674 if (parameters->options().output_is_position_independent()
5675 || (!parameters->doing_static_link()
5676 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
5678 target->rel_dyn_section(layout)->add_global(
5679 mips_sym, elfcpp::R_MIPS_REL32, got,
5680 mips_sym->got_offset(got_type));
5681 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
5682 elfcpp::R_MIPS_REL32, mips_sym);
5687 if (!got->multi_got() || this->index_ == 0)
5689 if (got->multi_got())
5691 // We need to allocate space in the primary GOT for GGA_NORMAL entries
5692 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
5693 // entries correspond to dynamic symbol indexes.
5694 while (count < non_reloc_only_global_gotno)
5696 got->add_constant(0);
5701 // Add GGA_RELOC_ONLY entries.
5702 got->add_reloc_only_entries();
5706 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
5708 template<int size, bool big_endian>
5710 Mips_got_info<size, big_endian>::add_reloc_only_entries(
5711 Mips_output_data_got<size, big_endian>* got)
5713 for (typename Global_got_entry_set::iterator
5714 p = this->global_got_symbols_.begin();
5715 p != this->global_got_symbols_.end();
5718 Mips_symbol<size>* mips_sym = *p;
5719 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
5721 unsigned int got_type;
5722 if (!got->multi_got())
5723 got_type = GOT_TYPE_STANDARD;
5725 got_type = GOT_TYPE_STANDARD_MULTIGOT;
5726 if (got->add_global(mips_sym, got_type))
5727 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5732 // Create TLS GOT entries.
5734 template<int size, bool big_endian>
5736 Mips_got_info<size, big_endian>::add_tls_entries(
5737 Target_mips<size, big_endian>* target, Layout* layout)
5739 Mips_output_data_got<size, big_endian>* got = target->got_section();
5740 // Add local tls entries.
5741 for (typename Got_entry_set::iterator
5742 p = this->got_entries_.begin();
5743 p != this->got_entries_.end();
5746 Mips_got_entry<size, big_endian>* entry = *p;
5747 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
5750 if (entry->tls_type() == GOT_TLS_GD)
5752 unsigned int got_type = GOT_TYPE_TLS_PAIR;
5753 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5754 : elfcpp::R_MIPS_TLS_DTPMOD64);
5755 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5756 : elfcpp::R_MIPS_TLS_DTPREL64);
5758 if (!parameters->doing_static_link())
5760 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
5761 entry->shndx(), got_type,
5762 target->rel_dyn_section(layout),
5763 r_type1, entry->addend());
5764 unsigned int got_offset =
5765 entry->object()->local_got_offset(entry->symndx(), got_type,
5767 got->add_static_reloc(got_offset + size/8, r_type2,
5768 entry->object(), entry->symndx());
5772 // We are doing a static link. Mark it as belong to module 1,
5774 unsigned int got_offset = got->add_constant(1);
5775 entry->object()->set_local_got_offset(entry->symndx(), got_type,
5778 got->add_constant(0);
5779 got->add_static_reloc(got_offset + size/8, r_type2,
5780 entry->object(), entry->symndx());
5783 else if (entry->tls_type() == GOT_TLS_IE)
5785 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
5786 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5787 : elfcpp::R_MIPS_TLS_TPREL64);
5788 if (!parameters->doing_static_link())
5789 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
5790 target->rel_dyn_section(layout), r_type,
5794 got->add_local(entry->object(), entry->symndx(), got_type,
5796 unsigned int got_offset =
5797 entry->object()->local_got_offset(entry->symndx(), got_type,
5799 got->add_static_reloc(got_offset, r_type, entry->object(),
5803 else if (entry->tls_type() == GOT_TLS_LDM)
5805 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5806 : elfcpp::R_MIPS_TLS_DTPMOD64);
5807 unsigned int got_offset;
5808 if (!parameters->doing_static_link())
5810 got_offset = got->add_constant(0);
5811 target->rel_dyn_section(layout)->add_local(
5812 entry->object(), 0, r_type, got, got_offset);
5815 // We are doing a static link. Just mark it as belong to module 1,
5817 got_offset = got->add_constant(1);
5819 got->add_constant(0);
5820 got->set_tls_ldm_offset(got_offset, entry->object());
5826 // Add global tls entries.
5827 for (typename Got_entry_set::iterator
5828 p = this->got_entries_.begin();
5829 p != this->got_entries_.end();
5832 Mips_got_entry<size, big_endian>* entry = *p;
5833 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
5836 Mips_symbol<size>* mips_sym = entry->sym();
5837 if (entry->tls_type() == GOT_TLS_GD)
5839 unsigned int got_type;
5840 if (!got->multi_got())
5841 got_type = GOT_TYPE_TLS_PAIR;
5843 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
5844 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5845 : elfcpp::R_MIPS_TLS_DTPMOD64);
5846 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5847 : elfcpp::R_MIPS_TLS_DTPREL64);
5848 if (!parameters->doing_static_link())
5849 got->add_global_pair_with_rel(mips_sym, got_type,
5850 target->rel_dyn_section(layout), r_type1, r_type2);
5853 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
5854 // GOT entries. The first one is initialized to be 1, which is the
5855 // module index for the main executable and the second one 0. A
5856 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
5857 // the second GOT entry and will be applied by gold.
5858 unsigned int got_offset = got->add_constant(1);
5859 mips_sym->set_got_offset(got_type, got_offset);
5860 got->add_constant(0);
5861 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
5864 else if (entry->tls_type() == GOT_TLS_IE)
5866 unsigned int got_type;
5867 if (!got->multi_got())
5868 got_type = GOT_TYPE_TLS_OFFSET;
5870 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
5871 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5872 : elfcpp::R_MIPS_TLS_TPREL64);
5873 if (!parameters->doing_static_link())
5874 got->add_global_with_rel(mips_sym, got_type,
5875 target->rel_dyn_section(layout), r_type);
5878 got->add_global(mips_sym, got_type);
5879 unsigned int got_offset = mips_sym->got_offset(got_type);
5880 got->add_static_reloc(got_offset, r_type, mips_sym);
5888 // Decide whether the symbol needs an entry in the global part of the primary
5889 // GOT, setting global_got_area accordingly. Count the number of global
5890 // symbols that are in the primary GOT only because they have dynamic
5891 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
5893 template<int size, bool big_endian>
5895 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
5897 for (typename Global_got_entry_set::iterator
5898 p = this->global_got_symbols_.begin();
5899 p != this->global_got_symbols_.end();
5902 Mips_symbol<size>* sym = *p;
5903 // Make a final decision about whether the symbol belongs in the
5904 // local or global GOT. Symbols that bind locally can (and in the
5905 // case of forced-local symbols, must) live in the local GOT.
5906 // Those that are aren't in the dynamic symbol table must also
5907 // live in the local GOT.
5909 if (!sym->should_add_dynsym_entry(symtab)
5910 || (sym->got_only_for_calls()
5911 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
5912 : symbol_references_local(sym,
5913 sym->should_add_dynsym_entry(symtab))))
5914 // The symbol belongs in the local GOT. We no longer need this
5915 // entry if it was only used for relocations; those relocations
5916 // will be against the null or section symbol instead.
5917 sym->set_global_got_area(GGA_NONE);
5918 else if (sym->global_got_area() == GGA_RELOC_ONLY)
5920 ++this->reloc_only_gotno_;
5921 ++this->global_gotno_ ;
5926 // Return the offset of GOT page entry for VALUE. Initialize the entry with
5927 // VALUE if it is not initialized.
5929 template<int size, bool big_endian>
5931 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
5932 Mips_output_data_got<size, big_endian>* got)
5934 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
5935 if (it != this->got_page_offsets_.end())
5938 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
5939 + (size/8) * this->page_gotno_);
5941 unsigned int got_offset = this->got_page_offset_next_;
5942 this->got_page_offsets_[value] = got_offset;
5943 this->got_page_offset_next_ += size/8;
5944 got->update_got_entry(got_offset, value);
5948 // Remove lazy-binding stubs for global symbols in this GOT.
5950 template<int size, bool big_endian>
5952 Mips_got_info<size, big_endian>::remove_lazy_stubs(
5953 Target_mips<size, big_endian>* target)
5955 for (typename Got_entry_set::iterator
5956 p = this->got_entries_.begin();
5957 p != this->got_entries_.end();
5960 Mips_got_entry<size, big_endian>* entry = *p;
5961 if (entry->is_for_global_symbol())
5962 target->remove_lazy_stub_entry(entry->sym());
5966 // Count the number of GOT entries required.
5968 template<int size, bool big_endian>
5970 Mips_got_info<size, big_endian>::count_got_entries()
5972 for (typename Got_entry_set::iterator
5973 p = this->got_entries_.begin();
5974 p != this->got_entries_.end();
5977 this->count_got_entry(*p);
5981 // Count the number of GOT entries required by ENTRY. Accumulate the result.
5983 template<int size, bool big_endian>
5985 Mips_got_info<size, big_endian>::count_got_entry(
5986 Mips_got_entry<size, big_endian>* entry)
5988 if (entry->is_tls_entry())
5989 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
5990 else if (entry->is_for_local_symbol()
5991 || entry->sym()->global_got_area() == GGA_NONE)
5992 ++this->local_gotno_;
5994 ++this->global_gotno_;
5997 // Add FROM's GOT entries.
5999 template<int size, bool big_endian>
6001 Mips_got_info<size, big_endian>::add_got_entries(
6002 Mips_got_info<size, big_endian>* from)
6004 for (typename Got_entry_set::iterator
6005 p = from->got_entries_.begin();
6006 p != from->got_entries_.end();
6009 Mips_got_entry<size, big_endian>* entry = *p;
6010 if (this->got_entries_.find(entry) == this->got_entries_.end())
6012 Mips_got_entry<size, big_endian>* entry2 =
6013 new Mips_got_entry<size, big_endian>(*entry);
6014 this->got_entries_.insert(entry2);
6015 this->count_got_entry(entry);
6020 // Add FROM's GOT page entries.
6022 template<int size, bool big_endian>
6024 Mips_got_info<size, big_endian>::add_got_page_entries(
6025 Mips_got_info<size, big_endian>* from)
6027 for (typename Got_page_entry_set::iterator
6028 p = from->got_page_entries_.begin();
6029 p != from->got_page_entries_.end();
6032 Got_page_entry* entry = *p;
6033 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
6035 Got_page_entry* entry2 = new Got_page_entry(*entry);
6036 this->got_page_entries_.insert(entry2);
6037 this->page_gotno_ += entry->num_pages;
6042 // Mips_output_data_got methods.
6044 // Lay out the GOT. Add local, global and TLS entries. If GOT is
6045 // larger than 64K, create multi-GOT.
6047 template<int size, bool big_endian>
6049 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6050 Symbol_table* symtab, const Input_objects* input_objects)
6052 // Decide which symbols need to go in the global part of the GOT and
6053 // count the number of reloc-only GOT symbols.
6054 this->master_got_info_->count_got_symbols(symtab);
6056 // Count the number of GOT entries.
6057 this->master_got_info_->count_got_entries();
6059 unsigned int got_size = this->master_got_info_->got_size();
6060 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6061 this->lay_out_multi_got(layout, input_objects);
6064 // Record that all objects use single GOT.
6065 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6066 p != input_objects->relobj_end();
6069 Mips_relobj<size, big_endian>* object =
6070 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6071 if (object->get_got_info() != NULL)
6072 object->set_got_info(this->master_got_info_);
6075 this->master_got_info_->add_local_entries(this->target_, layout);
6076 this->master_got_info_->add_global_entries(this->target_, layout,
6078 this->master_got_info_->add_tls_entries(this->target_, layout);
6082 // Create multi-GOT. For every GOT, add local, global and TLS entries.
6084 template<int size, bool big_endian>
6086 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6087 const Input_objects* input_objects)
6089 // Try to merge the GOTs of input objects together, as long as they
6090 // don't seem to exceed the maximum GOT size, choosing one of them
6091 // to be the primary GOT.
6092 this->merge_gots(input_objects);
6094 // Every symbol that is referenced in a dynamic relocation must be
6095 // present in the primary GOT.
6096 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6100 unsigned int offset = 0;
6101 Mips_got_info<size, big_endian>* g = this->primary_got_;
6105 g->set_offset(offset);
6107 g->add_local_entries(this->target_, layout);
6109 g->add_global_entries(this->target_, layout,
6110 (this->master_got_info_->global_gotno()
6111 - this->master_got_info_->reloc_only_gotno()));
6113 g->add_global_entries(this->target_, layout, /*not used*/-1U);
6114 g->add_tls_entries(this->target_, layout);
6116 // Forbid global symbols in every non-primary GOT from having
6117 // lazy-binding stubs.
6119 g->remove_lazy_stubs(this->target_);
6122 offset += g->got_size();
6128 // Attempt to merge GOTs of different input objects. Try to use as much as
6129 // possible of the primary GOT, since it doesn't require explicit dynamic
6130 // relocations, but don't use objects that would reference global symbols
6131 // out of the addressable range. Failing the primary GOT, attempt to merge
6132 // with the current GOT, or finish the current GOT and then make make the new
6135 template<int size, bool big_endian>
6137 Mips_output_data_got<size, big_endian>::merge_gots(
6138 const Input_objects* input_objects)
6140 gold_assert(this->primary_got_ == NULL);
6141 Mips_got_info<size, big_endian>* current = NULL;
6143 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6144 p != input_objects->relobj_end();
6147 Mips_relobj<size, big_endian>* object =
6148 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6150 Mips_got_info<size, big_endian>* g = object->get_got_info();
6154 g->count_got_entries();
6156 // Work out the number of page, local and TLS entries.
6157 unsigned int estimate = this->master_got_info_->page_gotno();
6158 if (estimate > g->page_gotno())
6159 estimate = g->page_gotno();
6160 estimate += g->local_gotno() + g->tls_gotno();
6162 // We place TLS GOT entries after both locals and globals. The globals
6163 // for the primary GOT may overflow the normal GOT size limit, so be
6164 // sure not to merge a GOT which requires TLS with the primary GOT in that
6165 // case. This doesn't affect non-primary GOTs.
6166 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6167 : g->global_gotno());
6169 unsigned int max_count =
6170 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6171 if (estimate <= max_count)
6173 // If we don't have a primary GOT, use it as
6174 // a starting point for the primary GOT.
6175 if (!this->primary_got_)
6177 this->primary_got_ = g;
6181 // Try merging with the primary GOT.
6182 if (this->merge_got_with(g, object, this->primary_got_))
6186 // If we can merge with the last-created GOT, do it.
6187 if (current && this->merge_got_with(g, object, current))
6190 // Well, we couldn't merge, so create a new GOT. Don't check if it
6191 // fits; if it turns out that it doesn't, we'll get relocation
6192 // overflows anyway.
6193 g->set_next(current);
6197 // If we do not find any suitable primary GOT, create an empty one.
6198 if (this->primary_got_ == NULL)
6199 this->primary_got_ = new Mips_got_info<size, big_endian>();
6201 // Link primary GOT with secondary GOTs.
6202 this->primary_got_->set_next(current);
6205 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6206 // this would lead to overflow, true if they were merged successfully.
6208 template<int size, bool big_endian>
6210 Mips_output_data_got<size, big_endian>::merge_got_with(
6211 Mips_got_info<size, big_endian>* from,
6212 Mips_relobj<size, big_endian>* object,
6213 Mips_got_info<size, big_endian>* to)
6215 // Work out how many page entries we would need for the combined GOT.
6216 unsigned int estimate = this->master_got_info_->page_gotno();
6217 if (estimate >= from->page_gotno() + to->page_gotno())
6218 estimate = from->page_gotno() + to->page_gotno();
6220 // Conservatively estimate how many local and TLS entries would be needed.
6221 estimate += from->local_gotno() + to->local_gotno();
6222 estimate += from->tls_gotno() + to->tls_gotno();
6224 // If we're merging with the primary got, any TLS relocations will
6225 // come after the full set of global entries. Otherwise estimate those
6226 // conservatively as well.
6227 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6228 estimate += this->master_got_info_->global_gotno();
6230 estimate += from->global_gotno() + to->global_gotno();
6232 // Bail out if the combined GOT might be too big.
6233 unsigned int max_count =
6234 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6235 if (estimate > max_count)
6238 // Transfer the object's GOT information from FROM to TO.
6239 to->add_got_entries(from);
6240 to->add_got_page_entries(from);
6242 // Record that OBJECT should use output GOT TO.
6243 object->set_got_info(to);
6248 // Write out the GOT.
6250 template<int size, bool big_endian>
6252 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6254 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6255 Mips_stubs_entry_set;
6257 // Call parent to write out GOT.
6258 Output_data_got<size, big_endian>::do_write(of);
6260 const off_t offset = this->offset();
6261 const section_size_type oview_size =
6262 convert_to_section_size_type(this->data_size());
6263 unsigned char* const oview = of->get_output_view(offset, oview_size);
6265 // Needed for fixing values of .got section.
6266 this->got_view_ = oview;
6268 // Write lazy stub addresses.
6269 for (typename Mips_stubs_entry_set::iterator
6270 p = this->master_got_info_->global_got_symbols().begin();
6271 p != this->master_got_info_->global_got_symbols().end();
6274 Mips_symbol<size>* mips_sym = *p;
6275 if (mips_sym->has_lazy_stub())
6277 Valtype* wv = reinterpret_cast<Valtype*>(
6278 oview + this->get_primary_got_offset(mips_sym));
6280 this->target_->mips_stubs_section()->stub_address(mips_sym);
6281 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6285 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6286 for (typename Mips_stubs_entry_set::iterator
6287 p = this->master_got_info_->global_got_symbols().begin();
6288 p != this->master_got_info_->global_got_symbols().end();
6291 Mips_symbol<size>* mips_sym = *p;
6292 if (!this->multi_got()
6293 && (mips_sym->is_mips16() || mips_sym->is_micromips())
6294 && mips_sym->global_got_area() == GGA_NONE
6295 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6297 Valtype* wv = reinterpret_cast<Valtype*>(
6298 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6299 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6303 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6308 if (!this->secondary_got_relocs_.empty())
6310 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6311 // secondary GOT entries with non-zero initial value copy the value
6312 // to the corresponding primary GOT entry, and set the secondary GOT
6314 // TODO(sasa): This is workaround. It needs to be investigated further.
6316 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6318 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6319 if (reloc.symbol_is_global())
6321 Mips_symbol<size>* gsym = reloc.symbol();
6322 gold_assert(gsym != NULL);
6324 unsigned got_offset = reloc.got_offset();
6325 gold_assert(got_offset < oview_size);
6327 // Find primary GOT entry.
6328 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6329 oview + this->get_primary_got_offset(gsym));
6331 // Find secondary GOT entry.
6332 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6334 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6337 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6338 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6339 gsym->set_applied_secondary_got_fixup();
6344 of->write_output_view(offset, oview_size, oview);
6347 // We are done if there is no fix up.
6348 if (this->static_relocs_.empty())
6351 Output_segment* tls_segment = this->layout_->tls_segment();
6352 gold_assert(tls_segment != NULL);
6354 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6356 Static_reloc& reloc(this->static_relocs_[i]);
6359 if (!reloc.symbol_is_global())
6361 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6362 const Symbol_value<size>* psymval =
6363 object->local_symbol(reloc.index());
6365 // We are doing static linking. Issue an error and skip this
6366 // relocation if the symbol is undefined or in a discarded_section.
6368 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6369 if ((shndx == elfcpp::SHN_UNDEF)
6371 && shndx != elfcpp::SHN_UNDEF
6372 && !object->is_section_included(shndx)
6373 && !this->symbol_table_->is_section_folded(object, shndx)))
6375 gold_error(_("undefined or discarded local symbol %u from "
6376 " object %s in GOT"),
6377 reloc.index(), reloc.relobj()->name().c_str());
6381 value = psymval->value(object, 0);
6385 const Mips_symbol<size>* gsym = reloc.symbol();
6386 gold_assert(gsym != NULL);
6388 // We are doing static linking. Issue an error and skip this
6389 // relocation if the symbol is undefined or in a discarded_section
6390 // unless it is a weakly_undefined symbol.
6391 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6392 && !gsym->is_weak_undefined())
6394 gold_error(_("undefined or discarded symbol %s in GOT"),
6399 if (!gsym->is_weak_undefined())
6400 value = gsym->value();
6405 unsigned got_offset = reloc.got_offset();
6406 gold_assert(got_offset < oview_size);
6408 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6411 switch (reloc.r_type())
6413 case elfcpp::R_MIPS_TLS_DTPMOD32:
6414 case elfcpp::R_MIPS_TLS_DTPMOD64:
6417 case elfcpp::R_MIPS_TLS_DTPREL32:
6418 case elfcpp::R_MIPS_TLS_DTPREL64:
6419 x = value - elfcpp::DTP_OFFSET;
6421 case elfcpp::R_MIPS_TLS_TPREL32:
6422 case elfcpp::R_MIPS_TLS_TPREL64:
6423 x = value - elfcpp::TP_OFFSET;
6430 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6433 of->write_output_view(offset, oview_size, oview);
6436 // Mips_relobj methods.
6438 // Count the local symbols. The Mips backend needs to know if a symbol
6439 // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6440 // because the Symbol object keeps the ELF symbol type and st_other field.
6441 // For local symbol it is harder because we cannot access this information.
6442 // So we override the do_count_local_symbol in parent and scan local symbols to
6443 // mark MIPS16 and microMIPS functions. This is not the most efficient way but
6444 // I do not want to slow down other ports by calling a per symbol target hook
6445 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6447 template<int size, bool big_endian>
6449 Mips_relobj<size, big_endian>::do_count_local_symbols(
6450 Stringpool_template<char>* pool,
6451 Stringpool_template<char>* dynpool)
6453 // Ask parent to count the local symbols.
6454 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6455 const unsigned int loccount = this->local_symbol_count();
6459 // Initialize the mips16 and micromips function bit-vector.
6460 this->local_symbol_is_mips16_.resize(loccount, false);
6461 this->local_symbol_is_micromips_.resize(loccount, false);
6463 // Read the symbol table section header.
6464 const unsigned int symtab_shndx = this->symtab_shndx();
6465 elfcpp::Shdr<size, big_endian>
6466 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6467 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6469 // Read the local symbols.
6470 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6471 gold_assert(loccount == symtabshdr.get_sh_info());
6472 off_t locsize = loccount * sym_size;
6473 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6474 locsize, true, true);
6476 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6478 // Skip the first dummy symbol.
6480 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6482 elfcpp::Sym<size, big_endian> sym(psyms);
6483 unsigned char st_other = sym.get_st_other();
6484 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6485 this->local_symbol_is_micromips_[i] =
6486 elfcpp::elf_st_is_micromips(st_other);
6490 // Read the symbol information.
6492 template<int size, bool big_endian>
6494 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6496 // Call parent class to read symbol information.
6497 this->base_read_symbols(sd);
6499 // Read processor-specific flags in ELF file header.
6500 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6501 elfcpp::Elf_sizes<size>::ehdr_size,
6503 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6504 this->processor_specific_flags_ = ehdr.get_e_flags();
6506 // Get the section names.
6507 const unsigned char* pnamesu = sd->section_names->data();
6508 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6510 // Initialize the mips16 stub section bit-vectors.
6511 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6512 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6513 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6515 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6516 const unsigned char* pshdrs = sd->section_headers->data();
6517 const unsigned char* ps = pshdrs + shdr_size;
6518 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6520 elfcpp::Shdr<size, big_endian> shdr(ps);
6522 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6524 this->has_reginfo_section_ = true;
6525 // Read the gp value that was used to create this object. We need the
6526 // gp value while processing relocs. The .reginfo section is not used
6527 // in the 64-bit MIPS ELF ABI.
6528 section_offset_type section_offset = shdr.get_sh_offset();
6529 section_size_type section_size =
6530 convert_to_section_size_type(shdr.get_sh_size());
6531 const unsigned char* view =
6532 this->get_view(section_offset, section_size, true, false);
6534 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6536 // Read the rest of .reginfo.
6537 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6538 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6539 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6540 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6541 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6544 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6546 gold_assert(this->attributes_section_data_ == NULL);
6547 section_offset_type section_offset = shdr.get_sh_offset();
6548 section_size_type section_size =
6549 convert_to_section_size_type(shdr.get_sh_size());
6550 const unsigned char* view =
6551 this->get_view(section_offset, section_size, true, false);
6552 this->attributes_section_data_ =
6553 new Attributes_section_data(view, section_size);
6556 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6558 gold_assert(this->abiflags_ == NULL);
6559 section_offset_type section_offset = shdr.get_sh_offset();
6560 section_size_type section_size =
6561 convert_to_section_size_type(shdr.get_sh_size());
6562 const unsigned char* view =
6563 this->get_view(section_offset, section_size, true, false);
6564 this->abiflags_ = new Mips_abiflags<big_endian>();
6566 this->abiflags_->version =
6567 elfcpp::Swap<16, big_endian>::readval(view);
6568 if (this->abiflags_->version != 0)
6570 gold_error(_("%s: .MIPS.abiflags section has "
6571 "unsupported version %u"),
6572 this->name().c_str(),
6573 this->abiflags_->version);
6576 this->abiflags_->isa_level =
6577 elfcpp::Swap<8, big_endian>::readval(view + 2);
6578 this->abiflags_->isa_rev =
6579 elfcpp::Swap<8, big_endian>::readval(view + 3);
6580 this->abiflags_->gpr_size =
6581 elfcpp::Swap<8, big_endian>::readval(view + 4);
6582 this->abiflags_->cpr1_size =
6583 elfcpp::Swap<8, big_endian>::readval(view + 5);
6584 this->abiflags_->cpr2_size =
6585 elfcpp::Swap<8, big_endian>::readval(view + 6);
6586 this->abiflags_->fp_abi =
6587 elfcpp::Swap<8, big_endian>::readval(view + 7);
6588 this->abiflags_->isa_ext =
6589 elfcpp::Swap<32, big_endian>::readval(view + 8);
6590 this->abiflags_->ases =
6591 elfcpp::Swap<32, big_endian>::readval(view + 12);
6592 this->abiflags_->flags1 =
6593 elfcpp::Swap<32, big_endian>::readval(view + 16);
6594 this->abiflags_->flags2 =
6595 elfcpp::Swap<32, big_endian>::readval(view + 20);
6598 // In the 64-bit ABI, .MIPS.options section holds register information.
6599 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6600 // starts with this header:
6604 // // Type of option.
6605 // unsigned char kind[1];
6606 // // Size of option descriptor, including header.
6607 // unsigned char size[1];
6608 // // Section index of affected section, or 0 for global option.
6609 // unsigned char section[2];
6610 // // Information specific to this kind of option.
6611 // unsigned char info[4];
6614 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6615 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6616 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6618 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6620 section_offset_type section_offset = shdr.get_sh_offset();
6621 section_size_type section_size =
6622 convert_to_section_size_type(shdr.get_sh_size());
6623 const unsigned char* view =
6624 this->get_view(section_offset, section_size, true, false);
6625 const unsigned char* end = view + section_size;
6627 while (view + 8 <= end)
6629 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6630 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6633 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6635 this->name().c_str(),
6636 this->mips_elf_options_section_name(), sz);
6640 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6642 // In the 64 bit ABI, an ODK_REGINFO option is the following
6643 // structure. The info field of the options header is not
6648 // // Mask of general purpose registers used.
6649 // unsigned char ri_gprmask[4];
6651 // unsigned char ri_pad[4];
6652 // // Mask of co-processor registers used.
6653 // unsigned char ri_cprmask[4][4];
6654 // // GP register value for this object file.
6655 // unsigned char ri_gp_value[8];
6658 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6661 else if (kind == elfcpp::ODK_REGINFO)
6663 // In the 32 bit ABI, an ODK_REGINFO option is the following
6664 // structure. The info field of the options header is not
6665 // used. The same structure is used in .reginfo section.
6669 // unsigned char ri_gprmask[4];
6670 // unsigned char ri_cprmask[4][4];
6671 // unsigned char ri_gp_value[4];
6674 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6681 const char* name = pnames + shdr.get_sh_name();
6682 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
6683 this->section_is_mips16_call_stub_[i] =
6684 is_prefix_of(".mips16.call.", name);
6685 this->section_is_mips16_call_fp_stub_[i] =
6686 is_prefix_of(".mips16.call.fp.", name);
6688 if (strcmp(name, ".pdr") == 0)
6690 gold_assert(this->pdr_shndx_ == -1U);
6691 this->pdr_shndx_ = i;
6696 // Discard MIPS16 stub secions that are not needed.
6698 template<int size, bool big_endian>
6700 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
6702 for (typename Mips16_stubs_int_map::const_iterator
6703 it = this->mips16_stub_sections_.begin();
6704 it != this->mips16_stub_sections_.end(); ++it)
6706 Mips16_stub_section<size, big_endian>* stub_section = it->second;
6707 if (!stub_section->is_target_found())
6709 gold_error(_("no relocation found in mips16 stub section '%s'"),
6710 stub_section->object()
6711 ->section_name(stub_section->shndx()).c_str());
6714 bool discard = false;
6715 if (stub_section->is_for_local_function())
6717 if (stub_section->is_fn_stub())
6719 // This stub is for a local symbol. This stub will only
6720 // be needed if there is some relocation in this object,
6721 // other than a 16 bit function call, which refers to this
6723 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
6726 this->add_local_mips16_fn_stub(stub_section);
6730 // This stub is for a local symbol. This stub will only
6731 // be needed if there is some relocation (R_MIPS16_26) in
6732 // this object that refers to this symbol.
6733 gold_assert(stub_section->is_call_stub()
6734 || stub_section->is_call_fp_stub());
6735 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
6738 this->add_local_mips16_call_stub(stub_section);
6743 Mips_symbol<size>* gsym = stub_section->gsym();
6744 if (stub_section->is_fn_stub())
6746 if (gsym->has_mips16_fn_stub())
6747 // We already have a stub for this function.
6751 gsym->set_mips16_fn_stub(stub_section);
6752 if (gsym->should_add_dynsym_entry(symtab))
6754 // If we have a MIPS16 function with a stub, the
6755 // dynamic symbol must refer to the stub, since only
6756 // the stub uses the standard calling conventions.
6757 gsym->set_need_fn_stub();
6758 if (gsym->is_from_dynobj())
6759 gsym->set_needs_dynsym_value();
6762 if (!gsym->need_fn_stub())
6765 else if (stub_section->is_call_stub())
6767 if (gsym->is_mips16())
6768 // We don't need the call_stub; this is a 16 bit
6769 // function, so calls from other 16 bit functions are
6772 else if (gsym->has_mips16_call_stub())
6773 // We already have a stub for this function.
6776 gsym->set_mips16_call_stub(stub_section);
6780 gold_assert(stub_section->is_call_fp_stub());
6781 if (gsym->is_mips16())
6782 // We don't need the call_stub; this is a 16 bit
6783 // function, so calls from other 16 bit functions are
6786 else if (gsym->has_mips16_call_fp_stub())
6787 // We already have a stub for this function.
6790 gsym->set_mips16_call_fp_stub(stub_section);
6794 this->set_output_section(stub_section->shndx(), NULL);
6798 // Mips_output_data_la25_stub methods.
6800 // Template for standard LA25 stub.
6801 template<int size, bool big_endian>
6803 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
6805 0x3c190000, // lui $25,%hi(func)
6806 0x08000000, // j func
6807 0x27390000, // add $25,$25,%lo(func)
6811 // Template for microMIPS LA25 stub.
6812 template<int size, bool big_endian>
6814 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
6816 0x41b9, 0x0000, // lui t9,%hi(func)
6817 0xd400, 0x0000, // j func
6818 0x3339, 0x0000, // addiu t9,t9,%lo(func)
6819 0x0000, 0x0000 // nop
6822 // Create la25 stub for a symbol.
6824 template<int size, bool big_endian>
6826 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
6827 Symbol_table* symtab, Target_mips<size, big_endian>* target,
6828 Mips_symbol<size>* gsym)
6830 if (!gsym->has_la25_stub())
6832 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
6833 this->symbols_.push_back(gsym);
6834 this->create_stub_symbol(gsym, symtab, target, 16);
6838 // Create a symbol for SYM stub's value and size, to help make the disassembly
6841 template<int size, bool big_endian>
6843 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
6844 Mips_symbol<size>* sym, Symbol_table* symtab,
6845 Target_mips<size, big_endian>* target, uint64_t symsize)
6847 std::string name(".pic.");
6848 name += sym->name();
6850 unsigned int offset = sym->la25_stub_offset();
6851 if (sym->is_micromips())
6854 // Make it a local function.
6855 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
6856 Symbol_table::PREDEFINED,
6857 target->la25_stub_section(),
6858 offset, symsize, elfcpp::STT_FUNC,
6860 elfcpp::STV_DEFAULT, 0,
6862 new_sym->set_is_forced_local();
6865 // Write out la25 stubs. This uses the hand-coded instructions above,
6866 // and adjusts them as needed.
6868 template<int size, bool big_endian>
6870 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
6872 const off_t offset = this->offset();
6873 const section_size_type oview_size =
6874 convert_to_section_size_type(this->data_size());
6875 unsigned char* const oview = of->get_output_view(offset, oview_size);
6877 for (typename std::vector<Mips_symbol<size>*>::iterator
6878 p = this->symbols_.begin();
6879 p != this->symbols_.end();
6882 Mips_symbol<size>* sym = *p;
6883 unsigned char* pov = oview + sym->la25_stub_offset();
6885 Mips_address target = sym->value();
6886 if (!sym->is_micromips())
6888 elfcpp::Swap<32, big_endian>::writeval(pov,
6889 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
6890 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
6891 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
6892 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
6893 la25_stub_entry[2] | (target & 0xffff));
6894 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
6899 // First stub instruction. Paste high 16-bits of the target.
6900 elfcpp::Swap<16, big_endian>::writeval(pov,
6901 la25_stub_micromips_entry[0]);
6902 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
6903 ((target + 0x8000) >> 16) & 0xffff);
6904 // Second stub instruction. Paste low 26-bits of the target, shifted
6906 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
6907 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
6908 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
6909 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
6910 // Third stub instruction. Paste low 16-bits of the target.
6911 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
6912 la25_stub_micromips_entry[4]);
6913 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
6914 // Fourth stub instruction.
6915 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
6916 la25_stub_micromips_entry[6]);
6917 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
6918 la25_stub_micromips_entry[7]);
6922 of->write_output_view(offset, oview_size, oview);
6925 // Mips_output_data_plt methods.
6927 // The format of the first PLT entry in an O32 executable.
6928 template<int size, bool big_endian>
6929 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
6931 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
6932 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
6933 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
6934 0x031cc023, // subu $24, $24, $28
6935 0x03e07825, // or $15, $31, zero
6936 0x0018c082, // srl $24, $24, 2
6937 0x0320f809, // jalr $25
6938 0x2718fffe // subu $24, $24, 2
6941 // The format of the first PLT entry in an N32 executable. Different
6942 // because gp ($28) is not available; we use t2 ($14) instead.
6943 template<int size, bool big_endian>
6944 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
6946 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6947 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
6948 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6949 0x030ec023, // subu $24, $24, $14
6950 0x03e07825, // or $15, $31, zero
6951 0x0018c082, // srl $24, $24, 2
6952 0x0320f809, // jalr $25
6953 0x2718fffe // subu $24, $24, 2
6956 // The format of the first PLT entry in an N64 executable. Different
6957 // from N32 because of the increased size of GOT entries.
6958 template<int size, bool big_endian>
6959 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
6961 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6962 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
6963 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6964 0x030ec023, // subu $24, $24, $14
6965 0x03e07825, // or $15, $31, zero
6966 0x0018c0c2, // srl $24, $24, 3
6967 0x0320f809, // jalr $25
6968 0x2718fffe // subu $24, $24, 2
6971 // The format of the microMIPS first PLT entry in an O32 executable.
6972 // We rely on v0 ($2) rather than t8 ($24) to contain the address
6973 // of the GOTPLT entry handled, so this stub may only be used when
6974 // all the subsequent PLT entries are microMIPS code too.
6976 // The trailing NOP is for alignment and correct disassembly only.
6977 template<int size, bool big_endian>
6978 const uint32_t Mips_output_data_plt<size, big_endian>::
6979 plt0_entry_micromips_o32[] =
6981 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
6982 0xff23, 0x0000, // lw $25, 0($3)
6983 0x0535, // subu $2, $2, $3
6984 0x2525, // srl $2, $2, 2
6985 0x3302, 0xfffe, // subu $24, $2, 2
6986 0x0dff, // move $15, $31
6987 0x45f9, // jalrs $25
6988 0x0f83, // move $28, $3
6992 // The format of the microMIPS first PLT entry in an O32 executable
6993 // in the insn32 mode.
6994 template<int size, bool big_endian>
6995 const uint32_t Mips_output_data_plt<size, big_endian>::
6996 plt0_entry_micromips32_o32[] =
6998 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
6999 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
7000 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
7001 0x0398, 0xc1d0, // subu $24, $24, $28
7002 0x001f, 0x7a90, // or $15, $31, zero
7003 0x0318, 0x1040, // srl $24, $24, 2
7004 0x03f9, 0x0f3c, // jalr $25
7005 0x3318, 0xfffe // subu $24, $24, 2
7008 // The format of subsequent standard entries in the PLT.
7009 template<int size, bool big_endian>
7010 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7012 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7013 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7014 0x03200008, // jr $25
7015 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7018 // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
7019 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7020 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7021 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7022 // target function address in register v0.
7023 template<int size, bool big_endian>
7024 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7026 0xb303, // lw $3, 12($pc)
7027 0x651b, // move $24, $3
7028 0x9b60, // lw $3, 0($3)
7030 0x653b, // move $25, $3
7032 0x0000, 0x0000 // .word (.got.plt entry)
7035 // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
7036 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7037 template<int size, bool big_endian>
7038 const uint32_t Mips_output_data_plt<size, big_endian>::
7039 plt_entry_micromips_o32[] =
7041 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
7042 0xff22, 0x0000, // lw $25, 0($2)
7044 0x0f02 // move $24, $2
7047 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7048 template<int size, bool big_endian>
7049 const uint32_t Mips_output_data_plt<size, big_endian>::
7050 plt_entry_micromips32_o32[] =
7052 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
7053 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
7054 0x0019, 0x0f3c, // jr $25
7055 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
7058 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7060 template<int size, bool big_endian>
7062 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7063 unsigned int r_type)
7065 gold_assert(!gsym->has_plt_offset());
7067 // Final PLT offset for a symbol will be set in method set_plt_offsets().
7068 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7069 + sizeof(plt0_entry_o32));
7070 this->symbols_.push_back(gsym);
7072 // Record whether the relocation requires a standard MIPS
7073 // or a compressed code entry.
7074 if (jal_reloc(r_type))
7076 if (r_type == elfcpp::R_MIPS_26)
7077 gsym->set_needs_mips_plt(true);
7079 gsym->set_needs_comp_plt(true);
7082 section_offset_type got_offset = this->got_plt_->current_data_size();
7084 // Every PLT entry needs a GOT entry which points back to the PLT
7085 // entry (this will be changed by the dynamic linker, normally
7086 // lazily when the function is called).
7087 this->got_plt_->set_current_data_size(got_offset + size/8);
7089 gsym->set_needs_dynsym_entry();
7090 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7094 // Set final PLT offsets. For each symbol, determine whether standard or
7095 // compressed (MIPS16 or microMIPS) PLT entry is used.
7097 template<int size, bool big_endian>
7099 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7101 // The sizes of individual PLT entries.
7102 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7103 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7104 ? this->compressed_plt_entry_size() : 0);
7106 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7107 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7109 Mips_symbol<size>* mips_sym = *p;
7111 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7112 // so always use a standard entry there.
7114 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7115 // all MIPS16 calls will go via that stub, and there is no benefit
7116 // to having a MIPS16 entry. And in the case of call_stub a
7117 // standard entry actually has to be used as the stub ends with a J
7119 if (this->target_->is_output_newabi()
7120 || mips_sym->has_mips16_call_stub()
7121 || mips_sym->has_mips16_call_fp_stub())
7123 mips_sym->set_needs_mips_plt(true);
7124 mips_sym->set_needs_comp_plt(false);
7127 // Otherwise, if there are no direct calls to the function, we
7128 // have a free choice of whether to use standard or compressed
7129 // entries. Prefer microMIPS entries if the object is known to
7130 // contain microMIPS code, so that it becomes possible to create
7131 // pure microMIPS binaries. Prefer standard entries otherwise,
7132 // because MIPS16 ones are no smaller and are usually slower.
7133 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7135 if (this->target_->is_output_micromips())
7136 mips_sym->set_needs_comp_plt(true);
7138 mips_sym->set_needs_mips_plt(true);
7141 if (mips_sym->needs_mips_plt())
7143 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7144 this->plt_mips_offset_ += plt_mips_entry_size;
7146 if (mips_sym->needs_comp_plt())
7148 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7149 this->plt_comp_offset_ += plt_comp_entry_size;
7153 // Figure out the size of the PLT header if we know that we are using it.
7154 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7155 this->plt_header_size_ = this->get_plt_header_size();
7158 // Write out the PLT. This uses the hand-coded instructions above,
7159 // and adjusts them as needed.
7161 template<int size, bool big_endian>
7163 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7165 const off_t offset = this->offset();
7166 const section_size_type oview_size =
7167 convert_to_section_size_type(this->data_size());
7168 unsigned char* const oview = of->get_output_view(offset, oview_size);
7170 const off_t gotplt_file_offset = this->got_plt_->offset();
7171 const section_size_type gotplt_size =
7172 convert_to_section_size_type(this->got_plt_->data_size());
7173 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7175 unsigned char* pov = oview;
7177 Mips_address plt_address = this->address();
7179 // Calculate the address of .got.plt.
7180 Mips_address gotplt_addr = this->got_plt_->address();
7181 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7182 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7184 // The PLT sequence is not safe for N64 if .got.plt's address can
7185 // not be loaded in two instructions.
7186 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7187 || ~(gotplt_addr | 0x7fffffff) == 0);
7189 // Write the PLT header.
7190 const uint32_t* plt0_entry = this->get_plt_header_entry();
7191 if (plt0_entry == plt0_entry_micromips_o32)
7193 // Write microMIPS PLT header.
7194 gold_assert(gotplt_addr % 4 == 0);
7196 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7198 // ADDIUPC has a span of +/-16MB, check we're in range.
7199 if (gotpc_offset + 0x1000000 >= 0x2000000)
7201 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7202 "ADDIUPC"), (long)gotpc_offset);
7206 elfcpp::Swap<16, big_endian>::writeval(pov,
7207 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7208 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7209 (gotpc_offset >> 2) & 0xffff);
7211 for (unsigned int i = 2;
7212 i < (sizeof(plt0_entry_micromips_o32)
7213 / sizeof(plt0_entry_micromips_o32[0]));
7216 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7220 else if (plt0_entry == plt0_entry_micromips32_o32)
7222 // Write microMIPS PLT header in insn32 mode.
7223 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7224 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7225 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7226 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7227 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7228 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7230 for (unsigned int i = 6;
7231 i < (sizeof(plt0_entry_micromips32_o32)
7232 / sizeof(plt0_entry_micromips32_o32[0]));
7235 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7241 // Write standard PLT header.
7242 elfcpp::Swap<32, big_endian>::writeval(pov,
7243 plt0_entry[0] | gotplt_addr_high);
7244 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7245 plt0_entry[1] | gotplt_addr_low);
7246 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7247 plt0_entry[2] | gotplt_addr_low);
7249 for (int i = 3; i < 8; i++)
7251 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7257 unsigned char* gotplt_pov = gotplt_view;
7258 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7260 // The first two entries in .got.plt are reserved.
7261 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7262 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7264 unsigned int gotplt_offset = 2 * got_entry_size;
7265 gotplt_pov += 2 * got_entry_size;
7267 // Calculate the address of the PLT header.
7268 Mips_address header_address = (plt_address
7269 + (this->is_plt_header_compressed() ? 1 : 0));
7271 // Initialize compressed PLT area view.
7272 unsigned char* pov2 = pov + this->plt_mips_offset_;
7274 // Write the PLT entries.
7275 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7276 p = this->symbols_.begin();
7277 p != this->symbols_.end();
7278 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7280 Mips_symbol<size>* mips_sym = *p;
7282 // Calculate the address of the .got.plt entry.
7283 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7284 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7286 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7288 // Initially point the .got.plt entry at the PLT header.
7289 if (this->target_->is_output_n64())
7290 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7292 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7294 // Now handle the PLT itself. First the standard entry.
7295 if (mips_sym->has_mips_plt_offset())
7297 // Pick the load opcode (LW or LD).
7298 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7301 // Fill in the PLT entry itself.
7302 elfcpp::Swap<32, big_endian>::writeval(pov,
7303 plt_entry[0] | gotplt_entry_addr_hi);
7304 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7305 plt_entry[1] | gotplt_entry_addr_lo | load);
7306 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_entry[2]);
7307 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7308 plt_entry[3] | gotplt_entry_addr_lo);
7312 // Now the compressed entry. They come after any standard ones.
7313 if (mips_sym->has_comp_plt_offset())
7315 if (!this->target_->is_output_micromips())
7317 // Write MIPS16 PLT entry.
7318 const uint32_t* plt_entry = plt_entry_mips16_o32;
7320 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7321 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7322 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7323 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7324 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7325 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7326 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7330 else if (this->target_->use_32bit_micromips_instructions())
7332 // Write microMIPS PLT entry in insn32 mode.
7333 const uint32_t* plt_entry = plt_entry_micromips32_o32;
7335 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7336 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7337 gotplt_entry_addr_hi);
7338 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7339 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7340 gotplt_entry_addr_lo);
7341 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7342 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7343 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7344 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7345 gotplt_entry_addr_lo);
7350 // Write microMIPS PLT entry.
7351 const uint32_t* plt_entry = plt_entry_micromips_o32;
7353 gold_assert(gotplt_entry_addr % 4 == 0);
7355 Mips_address loc_address = plt_address + pov2 - oview;
7356 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7358 // ADDIUPC has a span of +/-16MB, check we're in range.
7359 if (gotpc_offset + 0x1000000 >= 0x2000000)
7361 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7362 "range of ADDIUPC"), (long)gotpc_offset);
7366 elfcpp::Swap<16, big_endian>::writeval(pov2,
7367 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7368 elfcpp::Swap<16, big_endian>::writeval(
7369 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7370 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7371 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7372 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7373 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7379 // Check the number of bytes written for standard entries.
7380 gold_assert(static_cast<section_size_type>(
7381 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7382 // Check the number of bytes written for compressed entries.
7383 gold_assert((static_cast<section_size_type>(pov2 - pov)
7384 == this->plt_comp_offset_));
7385 // Check the total number of bytes written.
7386 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7388 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7391 of->write_output_view(offset, oview_size, oview);
7392 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7395 // Mips_output_data_mips_stubs methods.
7397 // The format of the lazy binding stub when dynamic symbol count is less than
7398 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7399 template<int size, bool big_endian>
7401 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7403 0x8f998010, // lw t9,0x8010(gp)
7404 0x03e07825, // or t7,ra,zero
7405 0x0320f809, // jalr t9,ra
7406 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7409 // The format of the lazy binding stub when dynamic symbol count is less than
7410 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7411 template<int size, bool big_endian>
7413 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7415 0xdf998010, // ld t9,0x8010(gp)
7416 0x03e07825, // or t7,ra,zero
7417 0x0320f809, // jalr t9,ra
7418 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7421 // The format of the lazy binding stub when dynamic symbol count is less than
7422 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7423 template<int size, bool big_endian>
7425 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7427 0x8f998010, // lw t9,0x8010(gp)
7428 0x03e07825, // or t7,ra,zero
7429 0x0320f809, // jalr t9,ra
7430 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7433 // The format of the lazy binding stub when dynamic symbol count is less than
7434 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7435 template<int size, bool big_endian>
7437 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7439 0xdf998010, // ld t9,0x8010(gp)
7440 0x03e07825, // or t7,ra,zero
7441 0x0320f809, // jalr t9,ra
7442 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7445 // The format of the lazy binding stub when dynamic symbol count is greater than
7446 // 64K, and ABI is not N64.
7447 template<int size, bool big_endian>
7448 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7450 0x8f998010, // lw t9,0x8010(gp)
7451 0x03e07825, // or t7,ra,zero
7452 0x3c180000, // lui t8,DYN_INDEX
7453 0x0320f809, // jalr t9,ra
7454 0x37180000 // ori t8,t8,DYN_INDEX
7457 // The format of the lazy binding stub when dynamic symbol count is greater than
7458 // 64K, and ABI is N64.
7459 template<int size, bool big_endian>
7461 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7463 0xdf998010, // ld t9,0x8010(gp)
7464 0x03e07825, // or t7,ra,zero
7465 0x3c180000, // lui t8,DYN_INDEX
7466 0x0320f809, // jalr t9,ra
7467 0x37180000 // ori t8,t8,DYN_INDEX
7472 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7473 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7474 template<int size, bool big_endian>
7476 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7478 0xff3c, 0x8010, // lw t9,0x8010(gp)
7479 0x0dff, // move t7,ra
7481 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7484 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7485 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7486 template<int size, bool big_endian>
7488 Mips_output_data_mips_stubs<size, big_endian>::
7489 lazy_stub_micromips_normal_1_n64[] =
7491 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7492 0x0dff, // move t7,ra
7494 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7497 // The format of the microMIPS lazy binding stub when dynamic symbol
7498 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7499 // and ABI is not N64.
7500 template<int size, bool big_endian>
7502 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7504 0xff3c, 0x8010, // lw t9,0x8010(gp)
7505 0x0dff, // move t7,ra
7507 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7510 // The format of the microMIPS lazy binding stub when dynamic symbol
7511 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7513 template<int size, bool big_endian>
7515 Mips_output_data_mips_stubs<size, big_endian>::
7516 lazy_stub_micromips_normal_2_n64[] =
7518 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7519 0x0dff, // move t7,ra
7521 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7524 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7525 // greater than 64K, and ABI is not N64.
7526 template<int size, bool big_endian>
7528 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7530 0xff3c, 0x8010, // lw t9,0x8010(gp)
7531 0x0dff, // move t7,ra
7532 0x41b8, 0x0000, // lui t8,DYN_INDEX
7534 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7537 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7538 // greater than 64K, and ABI is N64.
7539 template<int size, bool big_endian>
7541 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7543 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7544 0x0dff, // move t7,ra
7545 0x41b8, 0x0000, // lui t8,DYN_INDEX
7547 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7550 // 32-bit microMIPS stubs.
7552 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7553 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7554 // can use only 32-bit instructions.
7555 template<int size, bool big_endian>
7557 Mips_output_data_mips_stubs<size, big_endian>::
7558 lazy_stub_micromips32_normal_1[] =
7560 0xff3c, 0x8010, // lw t9,0x8010(gp)
7561 0x001f, 0x7a90, // or t7,ra,zero
7562 0x03f9, 0x0f3c, // jalr ra,t9
7563 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7566 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7567 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7568 // use only 32-bit instructions.
7569 template<int size, bool big_endian>
7571 Mips_output_data_mips_stubs<size, big_endian>::
7572 lazy_stub_micromips32_normal_1_n64[] =
7574 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7575 0x001f, 0x7a90, // or t7,ra,zero
7576 0x03f9, 0x0f3c, // jalr ra,t9
7577 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7580 // The format of the microMIPS lazy binding stub when dynamic symbol
7581 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7582 // ABI is not N64, and we can use only 32-bit instructions.
7583 template<int size, bool big_endian>
7585 Mips_output_data_mips_stubs<size, big_endian>::
7586 lazy_stub_micromips32_normal_2[] =
7588 0xff3c, 0x8010, // lw t9,0x8010(gp)
7589 0x001f, 0x7a90, // or t7,ra,zero
7590 0x03f9, 0x0f3c, // jalr ra,t9
7591 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7594 // The format of the microMIPS lazy binding stub when dynamic symbol
7595 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7596 // ABI is N64, and we can use only 32-bit instructions.
7597 template<int size, bool big_endian>
7599 Mips_output_data_mips_stubs<size, big_endian>::
7600 lazy_stub_micromips32_normal_2_n64[] =
7602 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7603 0x001f, 0x7a90, // or t7,ra,zero
7604 0x03f9, 0x0f3c, // jalr ra,t9
7605 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7608 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7609 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7610 template<int size, bool big_endian>
7612 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7614 0xff3c, 0x8010, // lw t9,0x8010(gp)
7615 0x001f, 0x7a90, // or t7,ra,zero
7616 0x41b8, 0x0000, // lui t8,DYN_INDEX
7617 0x03f9, 0x0f3c, // jalr ra,t9
7618 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7621 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7622 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7623 template<int size, bool big_endian>
7625 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
7627 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7628 0x001f, 0x7a90, // or t7,ra,zero
7629 0x41b8, 0x0000, // lui t8,DYN_INDEX
7630 0x03f9, 0x0f3c, // jalr ra,t9
7631 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7634 // Create entry for a symbol.
7636 template<int size, bool big_endian>
7638 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7639 Mips_symbol<size>* gsym)
7641 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7643 this->symbols_.insert(gsym);
7644 gsym->set_has_lazy_stub(true);
7648 // Remove entry for a symbol.
7650 template<int size, bool big_endian>
7652 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
7653 Mips_symbol<size>* gsym)
7655 if (gsym->has_lazy_stub())
7657 this->symbols_.erase(gsym);
7658 gsym->set_has_lazy_stub(false);
7662 // Set stub offsets for symbols. This method expects that the number of
7663 // entries in dynamic symbol table is set.
7665 template<int size, bool big_endian>
7667 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
7669 gold_assert(this->dynsym_count_ != -1U);
7671 if (this->stub_offsets_are_set_)
7674 unsigned int stub_size = this->stub_size();
7675 unsigned int offset = 0;
7676 for (typename Mips_stubs_entry_set::const_iterator
7677 p = this->symbols_.begin();
7678 p != this->symbols_.end();
7679 ++p, offset += stub_size)
7681 Mips_symbol<size>* mips_sym = *p;
7682 mips_sym->set_lazy_stub_offset(offset);
7684 this->stub_offsets_are_set_ = true;
7687 template<int size, bool big_endian>
7689 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
7691 for (typename Mips_stubs_entry_set::const_iterator
7692 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7694 Mips_symbol<size>* sym = *p;
7695 if (sym->is_from_dynobj())
7696 sym->set_needs_dynsym_value();
7700 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
7701 // adjusts them as needed.
7703 template<int size, bool big_endian>
7705 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
7707 const off_t offset = this->offset();
7708 const section_size_type oview_size =
7709 convert_to_section_size_type(this->data_size());
7710 unsigned char* const oview = of->get_output_view(offset, oview_size);
7712 bool big_stub = this->dynsym_count_ > 0x10000;
7714 unsigned char* pov = oview;
7715 for (typename Mips_stubs_entry_set::const_iterator
7716 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7718 Mips_symbol<size>* sym = *p;
7719 const uint32_t* lazy_stub;
7720 bool n64 = this->target_->is_output_n64();
7722 if (!this->target_->is_output_micromips())
7724 // Write standard (non-microMIPS) stub.
7727 if (sym->dynsym_index() & ~0x7fff)
7728 // Dynsym index is between 32K and 64K.
7729 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
7731 // Dynsym index is less than 32K.
7732 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
7735 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
7738 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7739 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
7745 // LUI instruction of the big stub. Paste high 16 bits of the
7747 elfcpp::Swap<32, big_endian>::writeval(pov,
7748 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
7752 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7753 // Last stub instruction. Paste low 16 bits of the dynsym index.
7754 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7755 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
7758 else if (this->target_->use_32bit_micromips_instructions())
7760 // Write microMIPS stub in insn32 mode.
7763 if (sym->dynsym_index() & ~0x7fff)
7764 // Dynsym index is between 32K and 64K.
7765 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
7766 : lazy_stub_micromips32_normal_2;
7768 // Dynsym index is less than 32K.
7769 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
7770 : lazy_stub_micromips32_normal_1;
7773 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
7774 : lazy_stub_micromips32_big;
7777 // First stub instruction. We emit 32-bit microMIPS instructions by
7778 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7779 // the instruction where the opcode is must always come first, for
7780 // both little and big endian.
7781 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7782 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7783 // Second stub instruction.
7784 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7785 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
7790 // LUI instruction of the big stub. Paste high 16 bits of the
7792 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7793 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7794 (sym->dynsym_index() >> 16) & 0x7fff);
7798 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7799 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7800 // Last stub instruction. Paste low 16 bits of the dynsym index.
7801 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7802 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7803 sym->dynsym_index() & 0xffff);
7808 // Write microMIPS stub.
7811 if (sym->dynsym_index() & ~0x7fff)
7812 // Dynsym index is between 32K and 64K.
7813 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
7814 : lazy_stub_micromips_normal_2;
7816 // Dynsym index is less than 32K.
7817 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
7818 : lazy_stub_micromips_normal_1;
7821 lazy_stub = n64 ? lazy_stub_micromips_big_n64
7822 : lazy_stub_micromips_big;
7825 // First stub instruction. We emit 32-bit microMIPS instructions by
7826 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7827 // the instruction where the opcode is must always come first, for
7828 // both little and big endian.
7829 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7830 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7831 // Second stub instruction.
7832 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7837 // LUI instruction of the big stub. Paste high 16 bits of the
7839 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7840 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7841 (sym->dynsym_index() >> 16) & 0x7fff);
7845 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7846 // Last stub instruction. Paste low 16 bits of the dynsym index.
7847 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7848 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7849 sym->dynsym_index() & 0xffff);
7854 // We always allocate 20 bytes for every stub, because final dynsym count is
7855 // not known in method do_finalize_sections. There are 4 unused bytes per
7856 // stub if final dynsym count is less than 0x10000.
7857 unsigned int used = pov - oview;
7858 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
7859 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
7861 // Fill the unused space with zeroes.
7862 // TODO(sasa): Can we strip unused bytes during the relaxation?
7864 memset(pov, 0, unused);
7866 of->write_output_view(offset, oview_size, oview);
7869 // Mips_output_section_reginfo methods.
7871 template<int size, bool big_endian>
7873 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
7875 off_t offset = this->offset();
7876 off_t data_size = this->data_size();
7878 unsigned char* view = of->get_output_view(offset, data_size);
7879 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
7880 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
7881 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
7882 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
7883 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
7884 // Write the gp value.
7885 elfcpp::Swap<size, big_endian>::writeval(view + 20,
7886 this->target_->gp_value());
7888 of->write_output_view(offset, data_size, view);
7891 // Mips_output_section_abiflags methods.
7893 template<int size, bool big_endian>
7895 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
7897 off_t offset = this->offset();
7898 off_t data_size = this->data_size();
7900 unsigned char* view = of->get_output_view(offset, data_size);
7901 elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
7902 elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
7903 elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
7904 elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
7905 elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
7906 elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
7907 elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
7908 elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
7909 elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
7910 elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
7911 elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
7913 of->write_output_view(offset, data_size, view);
7916 // Mips_copy_relocs methods.
7918 // Emit any saved relocs.
7920 template<int sh_type, int size, bool big_endian>
7922 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
7923 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7924 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7926 for (typename Copy_relocs<sh_type, size, big_endian>::
7927 Copy_reloc_entries::iterator p = this->entries_.begin();
7928 p != this->entries_.end();
7930 emit_entry(*p, reloc_section, symtab, layout, target);
7932 // We no longer need the saved information.
7933 this->entries_.clear();
7936 // Emit the reloc if appropriate.
7938 template<int sh_type, int size, bool big_endian>
7940 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
7941 Copy_reloc_entry& entry,
7942 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7943 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7945 // If the symbol is no longer defined in a dynamic object, then we
7946 // emitted a COPY relocation, and we do not want to emit this
7947 // dynamic relocation.
7948 if (!entry.sym_->is_from_dynobj())
7951 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
7952 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
7953 || entry.reloc_type_ == elfcpp::R_MIPS_64);
7955 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
7956 if (can_make_dynamic && !sym->has_static_relocs())
7958 Mips_relobj<size, big_endian>* object =
7959 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
7960 target->got_section(symtab, layout)->record_global_got_symbol(
7961 sym, object, entry.reloc_type_, true, false);
7962 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
7963 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
7964 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
7966 target->rel_dyn_section(layout)->add_symbolless_global_addend(
7967 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
7968 entry.shndx_, entry.address_);
7971 this->make_copy_reloc(symtab, layout,
7972 static_cast<Sized_symbol<size>*>(entry.sym_),
7977 // Target_mips methods.
7979 // Return the value to use for a dynamic symbol which requires special
7980 // treatment. This is how we support equality comparisons of function
7981 // pointers across shared library boundaries, as described in the
7982 // processor specific ABI supplement.
7984 template<int size, bool big_endian>
7986 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7989 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
7991 if (!mips_sym->has_lazy_stub())
7993 if (mips_sym->has_plt_offset())
7995 // We distinguish between PLT entries and lazy-binding stubs by
7996 // giving the former an st_other value of STO_MIPS_PLT. Set the
7997 // value to the stub address if there are any relocations in the
7998 // binary where pointer equality matters.
7999 if (mips_sym->pointer_equality_needed())
8001 // Prefer a standard MIPS PLT entry.
8002 if (mips_sym->has_mips_plt_offset())
8003 value = this->plt_section()->mips_entry_address(mips_sym);
8005 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8013 // First, set stub offsets for symbols. This method expects that the
8014 // number of entries in dynamic symbol table is set.
8015 this->mips_stubs_section()->set_lazy_stub_offsets();
8017 // The run-time linker uses the st_value field of the symbol
8018 // to reset the global offset table entry for this external
8019 // to its stub address when unlinking a shared object.
8020 value = this->mips_stubs_section()->stub_address(mips_sym);
8023 if (mips_sym->has_mips16_fn_stub())
8025 // If we have a MIPS16 function with a stub, the dynamic symbol must
8026 // refer to the stub, since only the stub uses the standard calling
8028 value = mips_sym->template
8029 get_mips16_fn_stub<big_endian>()->output_address();
8035 // Get the dynamic reloc section, creating it if necessary. It's always
8036 // .rel.dyn, even for MIPS64.
8038 template<int size, bool big_endian>
8039 typename Target_mips<size, big_endian>::Reloc_section*
8040 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8042 if (this->rel_dyn_ == NULL)
8044 gold_assert(layout != NULL);
8045 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8046 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8047 elfcpp::SHF_ALLOC, this->rel_dyn_,
8048 ORDER_DYNAMIC_RELOCS, false);
8050 // First entry in .rel.dyn has to be null.
8051 // This is hack - we define dummy output data and set its address to 0,
8052 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8053 // This ensures that the entry is null.
8054 Output_data* od = new Output_data_zero_fill(0, 0);
8056 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8058 return this->rel_dyn_;
8061 // Get the GOT section, creating it if necessary.
8063 template<int size, bool big_endian>
8064 Mips_output_data_got<size, big_endian>*
8065 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8068 if (this->got_ == NULL)
8070 gold_assert(symtab != NULL && layout != NULL);
8072 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8074 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8075 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8076 elfcpp::SHF_MIPS_GPREL),
8077 this->got_, ORDER_DATA, false);
8079 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8080 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8081 Symbol_table::PREDEFINED,
8083 0, 0, elfcpp::STT_OBJECT,
8085 elfcpp::STV_DEFAULT, 0,
8092 // Calculate value of _gp symbol.
8094 template<int size, bool big_endian>
8096 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8098 if (this->gp_ != NULL)
8101 Output_data* section = layout->find_output_section(".got");
8102 if (section == NULL)
8104 // If there is no .got section, gp should be based on .sdata.
8105 // TODO(sasa): This is probably not needed. This was needed for older
8106 // MIPS architectures which accessed both GOT and .sdata section using
8107 // gp-relative addressing. Modern Mips Linux ELF architectures don't
8108 // access .sdata using gp-relative addressing.
8109 for (Layout::Section_list::const_iterator
8110 p = layout->section_list().begin();
8111 p != layout->section_list().end();
8114 if (strcmp((*p)->name(), ".sdata") == 0)
8122 Sized_symbol<size>* gp =
8123 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8126 if (gp->source() != Symbol::IS_CONSTANT && section != NULL)
8127 gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0,
8130 elfcpp::STV_DEFAULT, 0,
8134 else if (section != NULL)
8136 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8137 "_gp", NULL, Symbol_table::PREDEFINED,
8138 section, MIPS_GP_OFFSET, 0,
8141 elfcpp::STV_DEFAULT,
8147 // Set the dynamic symbol indexes. INDEX is the index of the first
8148 // global dynamic symbol. Pointers to the symbols are stored into the
8149 // vector SYMS. The names are added to DYNPOOL. This returns an
8150 // updated dynamic symbol index.
8152 template<int size, bool big_endian>
8154 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8155 std::vector<Symbol*>* dyn_symbols, unsigned int index,
8156 std::vector<Symbol*>* syms, Stringpool* dynpool,
8157 Versions* versions, Symbol_table* symtab) const
8159 std::vector<Symbol*> non_got_symbols;
8160 std::vector<Symbol*> got_symbols;
8162 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8165 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8166 p != non_got_symbols.end();
8171 // Note that SYM may already have a dynamic symbol index, since
8172 // some symbols appear more than once in the symbol table, with
8173 // and without a version.
8175 if (!sym->has_dynsym_index())
8177 sym->set_dynsym_index(index);
8179 syms->push_back(sym);
8180 dynpool->add(sym->name(), false, NULL);
8182 // Record any version information.
8183 if (sym->version() != NULL)
8184 versions->record_version(symtab, dynpool, sym);
8186 // If the symbol is defined in a dynamic object and is
8187 // referenced in a regular object, then mark the dynamic
8188 // object as needed. This is used to implement --as-needed.
8189 if (sym->is_from_dynobj() && sym->in_reg())
8190 sym->object()->set_is_needed();
8194 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8195 p != got_symbols.end();
8199 if (!sym->has_dynsym_index())
8201 // Record any version information.
8202 if (sym->version() != NULL)
8203 versions->record_version(symtab, dynpool, sym);
8207 index = versions->finalize(symtab, index, syms);
8209 int got_sym_count = 0;
8210 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8211 p != got_symbols.end();
8216 if (!sym->has_dynsym_index())
8219 sym->set_dynsym_index(index);
8221 syms->push_back(sym);
8222 dynpool->add(sym->name(), false, NULL);
8224 // If the symbol is defined in a dynamic object and is
8225 // referenced in a regular object, then mark the dynamic
8226 // object as needed. This is used to implement --as-needed.
8227 if (sym->is_from_dynobj() && sym->in_reg())
8228 sym->object()->set_is_needed();
8232 // Set index of the first symbol that has .got entry.
8233 this->got_->set_first_global_got_dynsym_index(
8234 got_sym_count > 0 ? index - got_sym_count : -1U);
8236 if (this->mips_stubs_ != NULL)
8237 this->mips_stubs_->set_dynsym_count(index);
8242 // Create a PLT entry for a global symbol referenced by r_type relocation.
8244 template<int size, bool big_endian>
8246 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8248 Mips_symbol<size>* gsym,
8249 unsigned int r_type)
8251 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8254 if (this->plt_ == NULL)
8256 // Create the GOT section first.
8257 this->got_section(symtab, layout);
8259 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8260 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8261 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8262 this->got_plt_, ORDER_DATA, false);
8264 // The first two entries are reserved.
8265 this->got_plt_->set_current_data_size(2 * size/8);
8267 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8270 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8272 | elfcpp::SHF_EXECINSTR),
8273 this->plt_, ORDER_PLT, false);
8276 this->plt_->add_entry(gsym, r_type);
8280 // Get the .MIPS.stubs section, creating it if necessary.
8282 template<int size, bool big_endian>
8283 Mips_output_data_mips_stubs<size, big_endian>*
8284 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8286 if (this->mips_stubs_ == NULL)
8289 new Mips_output_data_mips_stubs<size, big_endian>(this);
8290 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8292 | elfcpp::SHF_EXECINSTR),
8293 this->mips_stubs_, ORDER_PLT, false);
8295 return this->mips_stubs_;
8298 // Get the LA25 stub section, creating it if necessary.
8300 template<int size, bool big_endian>
8301 Mips_output_data_la25_stub<size, big_endian>*
8302 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8304 if (this->la25_stub_ == NULL)
8306 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8307 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8309 | elfcpp::SHF_EXECINSTR),
8310 this->la25_stub_, ORDER_TEXT, false);
8312 return this->la25_stub_;
8315 // Process the relocations to determine unreferenced sections for
8316 // garbage collection.
8318 template<int size, bool big_endian>
8320 Target_mips<size, big_endian>::gc_process_relocs(
8321 Symbol_table* symtab,
8323 Sized_relobj_file<size, big_endian>* object,
8324 unsigned int data_shndx,
8325 unsigned int sh_type,
8326 const unsigned char* prelocs,
8328 Output_section* output_section,
8329 bool needs_special_offset_handling,
8330 size_t local_symbol_count,
8331 const unsigned char* plocal_symbols)
8333 typedef Target_mips<size, big_endian> Mips;
8335 if (sh_type == elfcpp::SHT_REL)
8337 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8340 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8349 needs_special_offset_handling,
8353 else if (sh_type == elfcpp::SHT_RELA)
8355 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8358 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8367 needs_special_offset_handling,
8375 // Scan relocations for a section.
8377 template<int size, bool big_endian>
8379 Target_mips<size, big_endian>::scan_relocs(
8380 Symbol_table* symtab,
8382 Sized_relobj_file<size, big_endian>* object,
8383 unsigned int data_shndx,
8384 unsigned int sh_type,
8385 const unsigned char* prelocs,
8387 Output_section* output_section,
8388 bool needs_special_offset_handling,
8389 size_t local_symbol_count,
8390 const unsigned char* plocal_symbols)
8392 typedef Target_mips<size, big_endian> Mips;
8394 if (sh_type == elfcpp::SHT_REL)
8396 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8399 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8408 needs_special_offset_handling,
8412 else if (sh_type == elfcpp::SHT_RELA)
8414 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8417 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8426 needs_special_offset_handling,
8432 template<int size, bool big_endian>
8434 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8436 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8437 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8438 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8439 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8440 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8441 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8442 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
8445 // Return the MACH for a MIPS e_flags value.
8446 template<int size, bool big_endian>
8448 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8450 switch (flags & elfcpp::EF_MIPS_MACH)
8452 case elfcpp::E_MIPS_MACH_3900:
8453 return mach_mips3900;
8455 case elfcpp::E_MIPS_MACH_4010:
8456 return mach_mips4010;
8458 case elfcpp::E_MIPS_MACH_4100:
8459 return mach_mips4100;
8461 case elfcpp::E_MIPS_MACH_4111:
8462 return mach_mips4111;
8464 case elfcpp::E_MIPS_MACH_4120:
8465 return mach_mips4120;
8467 case elfcpp::E_MIPS_MACH_4650:
8468 return mach_mips4650;
8470 case elfcpp::E_MIPS_MACH_5400:
8471 return mach_mips5400;
8473 case elfcpp::E_MIPS_MACH_5500:
8474 return mach_mips5500;
8476 case elfcpp::E_MIPS_MACH_5900:
8477 return mach_mips5900;
8479 case elfcpp::E_MIPS_MACH_9000:
8480 return mach_mips9000;
8482 case elfcpp::E_MIPS_MACH_SB1:
8483 return mach_mips_sb1;
8485 case elfcpp::E_MIPS_MACH_LS2E:
8486 return mach_mips_loongson_2e;
8488 case elfcpp::E_MIPS_MACH_LS2F:
8489 return mach_mips_loongson_2f;
8491 case elfcpp::E_MIPS_MACH_LS3A:
8492 return mach_mips_loongson_3a;
8494 case elfcpp::E_MIPS_MACH_OCTEON3:
8495 return mach_mips_octeon3;
8497 case elfcpp::E_MIPS_MACH_OCTEON2:
8498 return mach_mips_octeon2;
8500 case elfcpp::E_MIPS_MACH_OCTEON:
8501 return mach_mips_octeon;
8503 case elfcpp::E_MIPS_MACH_XLR:
8504 return mach_mips_xlr;
8507 switch (flags & elfcpp::EF_MIPS_ARCH)
8510 case elfcpp::E_MIPS_ARCH_1:
8511 return mach_mips3000;
8513 case elfcpp::E_MIPS_ARCH_2:
8514 return mach_mips6000;
8516 case elfcpp::E_MIPS_ARCH_3:
8517 return mach_mips4000;
8519 case elfcpp::E_MIPS_ARCH_4:
8520 return mach_mips8000;
8522 case elfcpp::E_MIPS_ARCH_5:
8525 case elfcpp::E_MIPS_ARCH_32:
8526 return mach_mipsisa32;
8528 case elfcpp::E_MIPS_ARCH_64:
8529 return mach_mipsisa64;
8531 case elfcpp::E_MIPS_ARCH_32R2:
8532 return mach_mipsisa32r2;
8534 case elfcpp::E_MIPS_ARCH_64R2:
8535 return mach_mipsisa64r2;
8542 // Return the MACH for each .MIPS.abiflags ISA Extension.
8544 template<int size, bool big_endian>
8546 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8550 case elfcpp::AFL_EXT_3900:
8551 return mach_mips3900;
8553 case elfcpp::AFL_EXT_4010:
8554 return mach_mips4010;
8556 case elfcpp::AFL_EXT_4100:
8557 return mach_mips4100;
8559 case elfcpp::AFL_EXT_4111:
8560 return mach_mips4111;
8562 case elfcpp::AFL_EXT_4120:
8563 return mach_mips4120;
8565 case elfcpp::AFL_EXT_4650:
8566 return mach_mips4650;
8568 case elfcpp::AFL_EXT_5400:
8569 return mach_mips5400;
8571 case elfcpp::AFL_EXT_5500:
8572 return mach_mips5500;
8574 case elfcpp::AFL_EXT_5900:
8575 return mach_mips5900;
8577 case elfcpp::AFL_EXT_10000:
8578 return mach_mips10000;
8580 case elfcpp::AFL_EXT_LOONGSON_2E:
8581 return mach_mips_loongson_2e;
8583 case elfcpp::AFL_EXT_LOONGSON_2F:
8584 return mach_mips_loongson_2f;
8586 case elfcpp::AFL_EXT_LOONGSON_3A:
8587 return mach_mips_loongson_3a;
8589 case elfcpp::AFL_EXT_SB1:
8590 return mach_mips_sb1;
8592 case elfcpp::AFL_EXT_OCTEON:
8593 return mach_mips_octeon;
8595 case elfcpp::AFL_EXT_OCTEONP:
8596 return mach_mips_octeonp;
8598 case elfcpp::AFL_EXT_OCTEON2:
8599 return mach_mips_octeon2;
8601 case elfcpp::AFL_EXT_XLR:
8602 return mach_mips_xlr;
8605 return mach_mips3000;
8609 // Return the .MIPS.abiflags value representing each ISA Extension.
8611 template<int size, bool big_endian>
8613 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
8618 return elfcpp::AFL_EXT_3900;
8621 return elfcpp::AFL_EXT_4010;
8624 return elfcpp::AFL_EXT_4100;
8627 return elfcpp::AFL_EXT_4111;
8630 return elfcpp::AFL_EXT_4120;
8633 return elfcpp::AFL_EXT_4650;
8636 return elfcpp::AFL_EXT_5400;
8639 return elfcpp::AFL_EXT_5500;
8642 return elfcpp::AFL_EXT_5900;
8644 case mach_mips10000:
8645 return elfcpp::AFL_EXT_10000;
8647 case mach_mips_loongson_2e:
8648 return elfcpp::AFL_EXT_LOONGSON_2E;
8650 case mach_mips_loongson_2f:
8651 return elfcpp::AFL_EXT_LOONGSON_2F;
8653 case mach_mips_loongson_3a:
8654 return elfcpp::AFL_EXT_LOONGSON_3A;
8657 return elfcpp::AFL_EXT_SB1;
8659 case mach_mips_octeon:
8660 return elfcpp::AFL_EXT_OCTEON;
8662 case mach_mips_octeonp:
8663 return elfcpp::AFL_EXT_OCTEONP;
8665 case mach_mips_octeon3:
8666 return elfcpp::AFL_EXT_OCTEON3;
8668 case mach_mips_octeon2:
8669 return elfcpp::AFL_EXT_OCTEON2;
8672 return elfcpp::AFL_EXT_XLR;
8679 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
8681 template<int size, bool big_endian>
8683 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
8684 elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
8687 switch (e_flags & elfcpp::EF_MIPS_ARCH)
8689 case elfcpp::E_MIPS_ARCH_1:
8690 new_isa = this->level_rev(1, 0);
8692 case elfcpp::E_MIPS_ARCH_2:
8693 new_isa = this->level_rev(2, 0);
8695 case elfcpp::E_MIPS_ARCH_3:
8696 new_isa = this->level_rev(3, 0);
8698 case elfcpp::E_MIPS_ARCH_4:
8699 new_isa = this->level_rev(4, 0);
8701 case elfcpp::E_MIPS_ARCH_5:
8702 new_isa = this->level_rev(5, 0);
8704 case elfcpp::E_MIPS_ARCH_32:
8705 new_isa = this->level_rev(32, 1);
8707 case elfcpp::E_MIPS_ARCH_32R2:
8708 new_isa = this->level_rev(32, 2);
8710 case elfcpp::E_MIPS_ARCH_64:
8711 new_isa = this->level_rev(64, 1);
8713 case elfcpp::E_MIPS_ARCH_64R2:
8714 new_isa = this->level_rev(64, 2);
8717 gold_error(_("%s: Unknown architecture %s"), name.c_str(),
8718 this->elf_mips_mach_name(e_flags));
8721 if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
8723 // Decode a single value into level and revision.
8724 abiflags->isa_level = new_isa >> 3;
8725 abiflags->isa_rev = new_isa & 0x7;
8728 // Update the isa_ext if needed.
8729 if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
8730 this->elf_mips_mach(e_flags)))
8731 abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
8734 // Infer the content of the ABI flags based on the elf header.
8736 template<int size, bool big_endian>
8738 Target_mips<size, big_endian>::infer_abiflags(
8739 Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
8741 const Attributes_section_data* pasd = relobj->attributes_section_data();
8742 int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
8743 elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
8745 this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
8748 // Read fp_abi from the .gnu.attribute section.
8749 const Object_attribute* attr =
8750 pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
8751 attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
8754 abiflags->fp_abi = attr_fp_abi;
8755 abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
8756 abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
8757 abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
8758 : elfcpp::AFL_REG_64;
8760 if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
8761 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
8762 || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
8763 && abiflags->gpr_size == elfcpp::AFL_REG_32))
8764 abiflags->cpr1_size = elfcpp::AFL_REG_32;
8765 else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
8766 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
8767 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
8768 abiflags->cpr1_size = elfcpp::AFL_REG_64;
8770 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
8771 abiflags->ases |= elfcpp::AFL_ASE_MDMX;
8772 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
8773 abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
8774 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
8775 abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
8777 if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
8778 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
8779 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
8780 && abiflags->isa_level >= 32
8781 && abiflags->isa_ext != elfcpp::AFL_EXT_LOONGSON_3A)
8782 abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
8785 // Create abiflags from elf header or from .MIPS.abiflags section.
8787 template<int size, bool big_endian>
8789 Target_mips<size, big_endian>::create_abiflags(
8790 Mips_relobj<size, big_endian>* relobj,
8791 Mips_abiflags<big_endian>* abiflags)
8793 Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
8794 Mips_abiflags<big_endian> header_abiflags;
8796 this->infer_abiflags(relobj, &header_abiflags);
8798 if (sec_abiflags == NULL)
8800 // If there is no input .MIPS.abiflags section, use abiflags created
8802 *abiflags = header_abiflags;
8806 this->has_abiflags_section_ = true;
8808 // It is not possible to infer the correct ISA revision for R3 or R5
8809 // so drop down to R2 for the checks.
8810 unsigned char isa_rev = sec_abiflags->isa_rev;
8811 if (isa_rev == 3 || isa_rev == 5)
8814 // Check compatibility between abiflags created from elf header
8815 // and abiflags from .MIPS.abiflags section in this object file.
8816 if (this->level_rev(sec_abiflags->isa_level, isa_rev)
8817 < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
8818 gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
8819 relobj->name().c_str());
8820 if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
8821 && sec_abiflags->fp_abi != header_abiflags.fp_abi)
8822 gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
8823 ".MIPS.abiflags"), relobj->name().c_str());
8824 if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
8825 gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
8826 relobj->name().c_str());
8827 // The isa_ext is allowed to be an extension of what can be inferred
8829 if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
8830 this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
8831 gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
8832 ".MIPS.abiflags"), relobj->name().c_str());
8833 if (sec_abiflags->flags2 != 0)
8834 gold_warning(_("%s: Unexpected flag in the flags2 field of "
8835 ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
8836 sec_abiflags->flags2);
8837 // Use abiflags from .MIPS.abiflags section.
8838 *abiflags = *sec_abiflags;
8841 // Return the meaning of fp_abi, or "unknown" if not known.
8843 template<int size, bool big_endian>
8845 Target_mips<size, big_endian>::fp_abi_string(int fp)
8849 case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
8850 return "-mdouble-float";
8851 case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
8852 return "-msingle-float";
8853 case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
8854 return "-msoft-float";
8855 case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
8856 return _("-mips32r2 -mfp64 (12 callee-saved)");
8857 case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
8859 case elfcpp::Val_GNU_MIPS_ABI_FP_64:
8860 return "-mgp32 -mfp64";
8861 case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
8862 return "-mgp32 -mfp64 -mno-odd-spreg";
8870 template<int size, bool big_endian>
8872 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
8875 if (in_fp == out_fp)
8878 if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
8880 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
8881 && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
8882 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
8883 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
8885 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
8886 && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
8887 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
8888 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
8889 return out_fp; // Keep the current setting.
8890 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
8891 && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
8893 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
8894 && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
8895 return out_fp; // Keep the current setting.
8896 else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
8897 gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
8898 fp_abi_string(in_fp), fp_abi_string(out_fp));
8902 // Merge attributes from input object.
8904 template<int size, bool big_endian>
8906 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
8907 const Attributes_section_data* pasd)
8909 // Return if there is no attributes section data.
8913 // If output has no object attributes, just copy.
8914 if (this->attributes_section_data_ == NULL)
8916 this->attributes_section_data_ = new Attributes_section_data(*pasd);
8920 Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
8921 Object_attribute::OBJ_ATTR_GNU);
8923 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
8924 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
8926 // Merge Tag_compatibility attributes and any common GNU ones.
8927 this->attributes_section_data_->merge(name.c_str(), pasd);
8930 // Merge abiflags from input object.
8932 template<int size, bool big_endian>
8934 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
8935 Mips_abiflags<big_endian>* in_abiflags)
8937 // If output has no abiflags, just copy.
8938 if (this->abiflags_ == NULL)
8940 this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
8944 this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
8945 this->abiflags_->fp_abi);
8948 this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
8949 in_abiflags->isa_level);
8950 this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
8951 in_abiflags->isa_rev);
8952 this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
8953 in_abiflags->gpr_size);
8954 this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
8955 in_abiflags->cpr1_size);
8956 this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
8957 in_abiflags->cpr2_size);
8958 this->abiflags_->ases |= in_abiflags->ases;
8959 this->abiflags_->flags1 |= in_abiflags->flags1;
8962 // Check whether machine EXTENSION is an extension of machine BASE.
8963 template<int size, bool big_endian>
8965 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
8966 unsigned int extension)
8968 if (extension == base)
8971 if ((base == mach_mipsisa32)
8972 && this->mips_mach_extends(mach_mipsisa64, extension))
8975 if ((base == mach_mipsisa32r2)
8976 && this->mips_mach_extends(mach_mipsisa64r2, extension))
8979 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
8980 if (extension == this->mips_mach_extensions_[i].first)
8982 extension = this->mips_mach_extensions_[i].second;
8983 if (extension == base)
8990 // Merge file header flags from input object.
8992 template<int size, bool big_endian>
8994 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
8995 elfcpp::Elf_Word in_flags)
8997 // If flags are not set yet, just copy them.
8998 if (!this->are_processor_specific_flags_set())
9000 this->set_processor_specific_flags(in_flags);
9001 this->mach_ = this->elf_mips_mach(in_flags);
9005 elfcpp::Elf_Word new_flags = in_flags;
9006 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9007 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9008 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9010 // Check flag compatibility.
9011 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9012 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9014 // Some IRIX 6 BSD-compatibility objects have this bit set. It
9015 // doesn't seem to matter.
9016 new_flags &= ~elfcpp::EF_MIPS_XGOT;
9017 old_flags &= ~elfcpp::EF_MIPS_XGOT;
9019 // MIPSpro generates ucode info in n64 objects. Again, we should
9020 // just be able to ignore this.
9021 new_flags &= ~elfcpp::EF_MIPS_UCODE;
9022 old_flags &= ~elfcpp::EF_MIPS_UCODE;
9024 if (new_flags == old_flags)
9026 this->set_processor_specific_flags(merged_flags);
9030 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9031 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9032 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9035 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9036 merged_flags |= elfcpp::EF_MIPS_CPIC;
9037 if (!(new_flags & elfcpp::EF_MIPS_PIC))
9038 merged_flags &= ~elfcpp::EF_MIPS_PIC;
9040 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9041 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9043 // Compare the ISAs.
9044 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9045 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9046 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9048 // Output ISA isn't the same as, or an extension of, input ISA.
9049 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9051 // Copy the architecture info from input object to output. Also copy
9052 // the 32-bit flag (if set) so that we continue to recognise
9053 // output as a 32-bit binary.
9054 this->mach_ = this->elf_mips_mach(in_flags);
9055 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9056 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9057 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9059 // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9060 this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9062 // Copy across the ABI flags if output doesn't use them
9063 // and if that was what caused us to treat input object as 32-bit.
9064 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9065 && this->mips_32bit_flags(new_flags)
9066 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9067 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9070 // The ISAs aren't compatible.
9071 gold_error(_("%s: linking %s module with previous %s modules"),
9072 name.c_str(), this->elf_mips_mach_name(in_flags),
9073 this->elf_mips_mach_name(merged_flags));
9076 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9077 | elfcpp::EF_MIPS_32BITMODE));
9078 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9079 | elfcpp::EF_MIPS_32BITMODE));
9082 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9084 // Only error if both are set (to different values).
9085 if ((new_flags & elfcpp::EF_MIPS_ABI)
9086 && (old_flags & elfcpp::EF_MIPS_ABI))
9087 gold_error(_("%s: ABI mismatch: linking %s module with "
9088 "previous %s modules"), name.c_str(),
9089 this->elf_mips_abi_name(in_flags),
9090 this->elf_mips_abi_name(merged_flags));
9092 new_flags &= ~elfcpp::EF_MIPS_ABI;
9093 old_flags &= ~elfcpp::EF_MIPS_ABI;
9096 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
9097 // and allow arbitrary mixing of the remaining ASEs (retain the union).
9098 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9099 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9101 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9102 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9103 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9104 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9105 int micro_mis = old_m16 && new_micro;
9106 int m16_mis = old_micro && new_m16;
9108 if (m16_mis || micro_mis)
9109 gold_error(_("%s: ASE mismatch: linking %s module with "
9110 "previous %s modules"), name.c_str(),
9111 m16_mis ? "MIPS16" : "microMIPS",
9112 m16_mis ? "microMIPS" : "MIPS16");
9114 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9116 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9117 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9120 // Compare NaN encodings.
9121 if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9123 gold_error(_("%s: linking %s module with previous %s modules"),
9125 (new_flags & elfcpp::EF_MIPS_NAN2008
9126 ? "-mnan=2008" : "-mnan=legacy"),
9127 (old_flags & elfcpp::EF_MIPS_NAN2008
9128 ? "-mnan=2008" : "-mnan=legacy"));
9130 new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9131 old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9134 // Compare FP64 state.
9135 if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9137 gold_error(_("%s: linking %s module with previous %s modules"),
9139 (new_flags & elfcpp::EF_MIPS_FP64
9140 ? "-mfp64" : "-mfp32"),
9141 (old_flags & elfcpp::EF_MIPS_FP64
9142 ? "-mfp64" : "-mfp32"));
9144 new_flags &= ~elfcpp::EF_MIPS_FP64;
9145 old_flags &= ~elfcpp::EF_MIPS_FP64;
9148 // Warn about any other mismatches.
9149 if (new_flags != old_flags)
9150 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9151 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9153 this->set_processor_specific_flags(merged_flags);
9156 // Adjust ELF file header.
9158 template<int size, bool big_endian>
9160 Target_mips<size, big_endian>::do_adjust_elf_header(
9161 unsigned char* view,
9164 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9166 elfcpp::Ehdr<size, big_endian> ehdr(view);
9167 unsigned char e_ident[elfcpp::EI_NIDENT];
9168 elfcpp::Elf_Word flags = this->processor_specific_flags();
9169 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9171 unsigned char ei_abiversion = 0;
9172 elfcpp::Elf_Half type = ehdr.get_e_type();
9173 if (type == elfcpp::ET_EXEC
9174 && parameters->options().copyreloc()
9175 && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9176 == elfcpp::EF_MIPS_CPIC)
9179 if (this->abiflags_ != NULL
9180 && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9181 || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9184 e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9185 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9186 oehdr.put_e_ident(e_ident);
9188 if (this->entry_symbol_is_compressed_)
9189 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9192 // do_make_elf_object to override the same function in the base class.
9193 // We need to use a target-specific sub-class of
9194 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9195 // Hence we need to have our own ELF object creation.
9197 template<int size, bool big_endian>
9199 Target_mips<size, big_endian>::do_make_elf_object(
9200 const std::string& name,
9201 Input_file* input_file,
9202 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9204 int et = ehdr.get_e_type();
9205 // ET_EXEC files are valid input for --just-symbols/-R,
9206 // and we treat them as relocatable objects.
9207 if (et == elfcpp::ET_REL
9208 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9210 Mips_relobj<size, big_endian>* obj =
9211 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9215 else if (et == elfcpp::ET_DYN)
9217 // TODO(sasa): Should we create Mips_dynobj?
9218 return Target::do_make_elf_object(name, input_file, offset, ehdr);
9222 gold_error(_("%s: unsupported ELF file type %d"),
9228 // Finalize the sections.
9230 template <int size, bool big_endian>
9232 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9233 const Input_objects* input_objects,
9234 Symbol_table* symtab)
9236 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9237 // DT_FINI have correct values.
9238 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9239 symtab->lookup(parameters->options().init()));
9240 if (init != NULL && (init->is_mips16() || init->is_micromips()))
9241 init->set_value(init->value() | 1);
9242 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9243 symtab->lookup(parameters->options().fini()));
9244 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9245 fini->set_value(fini->value() | 1);
9247 // Check whether the entry symbol is mips16 or micromips. This is needed to
9248 // adjust entry address in ELF header.
9249 Mips_symbol<size>* entry =
9250 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9251 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9252 || entry->is_micromips()));
9254 if (!parameters->doing_static_link()
9255 && (strcmp(parameters->options().hash_style(), "gnu") == 0
9256 || strcmp(parameters->options().hash_style(), "both") == 0))
9258 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9259 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
9260 // MIPS ABI requires a mapping between the GOT and the symbol table.
9261 gold_error(".gnu.hash is incompatible with the MIPS ABI");
9264 // Check whether the final section that was scanned has HI16 or GOT16
9265 // relocations without the corresponding LO16 part.
9266 if (this->got16_addends_.size() > 0)
9267 gold_error("Can't find matching LO16 reloc");
9270 this->set_gp(layout, symtab);
9272 // Check for any mips16 stub sections that we can discard.
9273 if (!parameters->options().relocatable())
9275 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9276 p != input_objects->relobj_end();
9279 Mips_relobj<size, big_endian>* object =
9280 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9281 object->discard_mips16_stub_sections(symtab);
9285 Valtype gprmask = 0;
9286 Valtype cprmask1 = 0;
9287 Valtype cprmask2 = 0;
9288 Valtype cprmask3 = 0;
9289 Valtype cprmask4 = 0;
9290 bool has_reginfo_section = false;
9292 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9293 p != input_objects->relobj_end();
9296 Mips_relobj<size, big_endian>* relobj =
9297 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9299 // Merge .reginfo contents of input objects.
9300 if (relobj->has_reginfo_section())
9302 has_reginfo_section = true;
9303 gprmask |= relobj->gprmask();
9304 cprmask1 |= relobj->cprmask1();
9305 cprmask2 |= relobj->cprmask2();
9306 cprmask3 |= relobj->cprmask3();
9307 cprmask4 |= relobj->cprmask4();
9310 Input_file::Format format = relobj->input_file()->format();
9311 if (format != Input_file::FORMAT_ELF)
9314 // If all input sections will be discarded, don't use this object
9315 // file for merging processor specific flags.
9316 bool should_merge_processor_specific_flags = false;
9318 for (unsigned int i = 1; i < relobj->shnum(); ++i)
9319 if (relobj->output_section(i) != NULL)
9321 should_merge_processor_specific_flags = true;
9325 if (!should_merge_processor_specific_flags)
9328 // Merge processor specific flags.
9329 Mips_abiflags<big_endian> in_abiflags;
9331 this->create_abiflags(relobj, &in_abiflags);
9332 this->merge_obj_e_flags(relobj->name(),
9333 relobj->processor_specific_flags());
9334 this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9335 this->merge_obj_attributes(relobj->name(),
9336 relobj->attributes_section_data());
9339 // Create a .gnu.attributes section if we have merged any attributes
9341 if (this->attributes_section_data_ != NULL)
9343 Output_attributes_section_data* attributes_section =
9344 new Output_attributes_section_data(*this->attributes_section_data_);
9345 layout->add_output_section_data(".gnu.attributes",
9346 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9347 attributes_section, ORDER_INVALID, false);
9350 // Create .MIPS.abiflags output section if there is an input section.
9351 if (this->has_abiflags_section_)
9353 Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9354 new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9356 Output_section* os =
9357 layout->add_output_section_data(".MIPS.abiflags",
9358 elfcpp::SHT_MIPS_ABIFLAGS,
9360 abiflags_section, ORDER_INVALID, false);
9362 if (!parameters->options().relocatable() && os != NULL)
9364 Output_segment* abiflags_segment =
9365 layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9366 abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9370 if (has_reginfo_section && !parameters->options().gc_sections())
9372 // Create .reginfo output section.
9373 Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9374 new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9376 cprmask3, cprmask4);
9378 Output_section* os =
9379 layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9380 elfcpp::SHF_ALLOC, reginfo_section,
9381 ORDER_INVALID, false);
9383 if (!parameters->options().relocatable() && os != NULL)
9385 Output_segment* reginfo_segment =
9386 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9388 reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9392 if (this->plt_ != NULL)
9394 // Set final PLT offsets for symbols.
9395 this->plt_section()->set_plt_offsets();
9397 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9398 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9399 // there are no standard PLT entries present.
9400 unsigned char nonvis = 0;
9401 if (this->is_output_micromips()
9402 && !this->plt_section()->has_standard_entries())
9403 nonvis = elfcpp::STO_MICROMIPS >> 2;
9404 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9405 Symbol_table::PREDEFINED,
9407 0, 0, elfcpp::STT_FUNC,
9409 elfcpp::STV_DEFAULT, nonvis,
9413 if (this->mips_stubs_ != NULL)
9415 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9416 unsigned char nonvis = 0;
9417 if (this->is_output_micromips())
9418 nonvis = elfcpp::STO_MICROMIPS >> 2;
9419 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9420 Symbol_table::PREDEFINED,
9422 0, 0, elfcpp::STT_FUNC,
9424 elfcpp::STV_DEFAULT, nonvis,
9428 if (!parameters->options().relocatable() && !parameters->doing_static_link())
9429 // In case there is no .got section, create one.
9430 this->got_section(symtab, layout);
9432 // Emit any relocs we saved in an attempt to avoid generating COPY
9434 if (this->copy_relocs_.any_saved_relocs())
9435 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9438 // Emit dynamic relocs.
9439 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9440 p != this->dyn_relocs_.end();
9442 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9444 if (this->has_got_section())
9445 this->got_section()->lay_out_got(layout, symtab, input_objects);
9447 if (this->mips_stubs_ != NULL)
9448 this->mips_stubs_->set_needs_dynsym_value();
9450 // Check for functions that might need $25 to be valid on entry.
9451 // TODO(sasa): Can we do this without iterating over all symbols?
9452 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9453 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9456 // Add NULL segment.
9457 if (!parameters->options().relocatable())
9458 layout->make_output_segment(elfcpp::PT_NULL, 0);
9460 // Fill in some more dynamic tags.
9461 // TODO(sasa): Add more dynamic tags.
9462 const Reloc_section* rel_plt = (this->plt_ == NULL
9463 ? NULL : this->plt_->rel_plt());
9464 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9465 this->rel_dyn_, true, false);
9467 Output_data_dynamic* const odyn = layout->dynamic_data();
9469 && !parameters->options().relocatable()
9470 && !parameters->doing_static_link())
9473 // This element holds a 32-bit version id for the Runtime
9474 // Linker Interface. This will start at integer value 1.
9476 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9479 d_val = elfcpp::RHF_NOTPOT;
9480 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9482 // Save layout for using when emiting custom dynamic tags.
9483 this->layout_ = layout;
9485 // This member holds the base address of the segment.
9486 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9488 // This member holds the number of entries in the .dynsym section.
9489 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9491 // This member holds the index of the first dynamic symbol
9492 // table entry that corresponds to an entry in the global offset table.
9493 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9495 // This member holds the number of local GOT entries.
9496 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9497 this->got_->get_local_gotno());
9499 if (this->plt_ != NULL)
9500 // DT_MIPS_PLTGOT dynamic tag
9501 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9505 // Get the custom dynamic tag value.
9506 template<int size, bool big_endian>
9508 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9512 case elfcpp::DT_MIPS_BASE_ADDRESS:
9514 // The base address of the segment.
9515 // At this point, the segment list has been sorted into final order,
9516 // so just return vaddr of the first readable PT_LOAD segment.
9517 Output_segment* seg =
9518 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9519 gold_assert(seg != NULL);
9520 return seg->vaddr();
9523 case elfcpp::DT_MIPS_SYMTABNO:
9524 // The number of entries in the .dynsym section.
9525 return this->get_dt_mips_symtabno();
9527 case elfcpp::DT_MIPS_GOTSYM:
9529 // The index of the first dynamic symbol table entry that corresponds
9530 // to an entry in the GOT.
9531 if (this->got_->first_global_got_dynsym_index() != -1U)
9532 return this->got_->first_global_got_dynsym_index();
9534 // In case if we don't have global GOT symbols we default to setting
9535 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9536 return this->get_dt_mips_symtabno();
9540 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9543 return (unsigned int)-1;
9546 // Relocate section data.
9548 template<int size, bool big_endian>
9550 Target_mips<size, big_endian>::relocate_section(
9551 const Relocate_info<size, big_endian>* relinfo,
9552 unsigned int sh_type,
9553 const unsigned char* prelocs,
9555 Output_section* output_section,
9556 bool needs_special_offset_handling,
9557 unsigned char* view,
9558 Mips_address address,
9559 section_size_type view_size,
9560 const Reloc_symbol_changes* reloc_symbol_changes)
9562 typedef Target_mips<size, big_endian> Mips;
9563 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9565 if (sh_type == elfcpp::SHT_REL)
9567 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9570 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9571 gold::Default_comdat_behavior, Classify_reloc>(
9577 needs_special_offset_handling,
9581 reloc_symbol_changes);
9583 else if (sh_type == elfcpp::SHT_RELA)
9585 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9588 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9589 gold::Default_comdat_behavior, Classify_reloc>(
9595 needs_special_offset_handling,
9599 reloc_symbol_changes);
9603 // Return the size of a relocation while scanning during a relocatable
9607 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
9611 case elfcpp::R_MIPS_NONE:
9612 case elfcpp::R_MIPS_TLS_DTPMOD64:
9613 case elfcpp::R_MIPS_TLS_DTPREL64:
9614 case elfcpp::R_MIPS_TLS_TPREL64:
9617 case elfcpp::R_MIPS_32:
9618 case elfcpp::R_MIPS_TLS_DTPMOD32:
9619 case elfcpp::R_MIPS_TLS_DTPREL32:
9620 case elfcpp::R_MIPS_TLS_TPREL32:
9621 case elfcpp::R_MIPS_REL32:
9622 case elfcpp::R_MIPS_PC32:
9623 case elfcpp::R_MIPS_GPREL32:
9624 case elfcpp::R_MIPS_JALR:
9625 case elfcpp::R_MIPS_EH:
9628 case elfcpp::R_MIPS_16:
9629 case elfcpp::R_MIPS_HI16:
9630 case elfcpp::R_MIPS_LO16:
9631 case elfcpp::R_MIPS_GPREL16:
9632 case elfcpp::R_MIPS16_HI16:
9633 case elfcpp::R_MIPS16_LO16:
9634 case elfcpp::R_MIPS_PC16:
9635 case elfcpp::R_MIPS_GOT16:
9636 case elfcpp::R_MIPS16_GOT16:
9637 case elfcpp::R_MIPS_CALL16:
9638 case elfcpp::R_MIPS16_CALL16:
9639 case elfcpp::R_MIPS_GOT_HI16:
9640 case elfcpp::R_MIPS_CALL_HI16:
9641 case elfcpp::R_MIPS_GOT_LO16:
9642 case elfcpp::R_MIPS_CALL_LO16:
9643 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
9644 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
9645 case elfcpp::R_MIPS_TLS_TPREL_HI16:
9646 case elfcpp::R_MIPS_TLS_TPREL_LO16:
9647 case elfcpp::R_MIPS16_GPREL:
9648 case elfcpp::R_MIPS_GOT_DISP:
9649 case elfcpp::R_MIPS_LITERAL:
9650 case elfcpp::R_MIPS_GOT_PAGE:
9651 case elfcpp::R_MIPS_GOT_OFST:
9652 case elfcpp::R_MIPS_TLS_GD:
9653 case elfcpp::R_MIPS_TLS_LDM:
9654 case elfcpp::R_MIPS_TLS_GOTTPREL:
9657 // These relocations are not byte sized
9658 case elfcpp::R_MIPS_26:
9659 case elfcpp::R_MIPS16_26:
9662 case elfcpp::R_MIPS_COPY:
9663 case elfcpp::R_MIPS_JUMP_SLOT:
9664 object->error(_("unexpected reloc %u in object file"), r_type);
9668 object->error(_("unsupported reloc %u in object file"), r_type);
9673 // Scan the relocs during a relocatable link.
9675 template<int size, bool big_endian>
9677 Target_mips<size, big_endian>::scan_relocatable_relocs(
9678 Symbol_table* symtab,
9680 Sized_relobj_file<size, big_endian>* object,
9681 unsigned int data_shndx,
9682 unsigned int sh_type,
9683 const unsigned char* prelocs,
9685 Output_section* output_section,
9686 bool needs_special_offset_handling,
9687 size_t local_symbol_count,
9688 const unsigned char* plocal_symbols,
9689 Relocatable_relocs* rr)
9691 if (sh_type == elfcpp::SHT_REL)
9693 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9695 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
9696 Scan_relocatable_relocs;
9698 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
9706 needs_special_offset_handling,
9711 else if (sh_type == elfcpp::SHT_RELA)
9713 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9715 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
9716 Scan_relocatable_relocs;
9718 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
9726 needs_special_offset_handling,
9735 // Scan the relocs for --emit-relocs.
9737 template<int size, bool big_endian>
9739 Target_mips<size, big_endian>::emit_relocs_scan(
9740 Symbol_table* symtab,
9742 Sized_relobj_file<size, big_endian>* object,
9743 unsigned int data_shndx,
9744 unsigned int sh_type,
9745 const unsigned char* prelocs,
9747 Output_section* output_section,
9748 bool needs_special_offset_handling,
9749 size_t local_symbol_count,
9750 const unsigned char* plocal_syms,
9751 Relocatable_relocs* rr)
9753 if (sh_type == elfcpp::SHT_REL)
9755 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9757 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9758 Emit_relocs_strategy;
9760 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9768 needs_special_offset_handling,
9773 else if (sh_type == elfcpp::SHT_RELA)
9775 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9777 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9778 Emit_relocs_strategy;
9780 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9788 needs_special_offset_handling,
9797 // Emit relocations for a section.
9799 template<int size, bool big_endian>
9801 Target_mips<size, big_endian>::relocate_relocs(
9802 const Relocate_info<size, big_endian>* relinfo,
9803 unsigned int sh_type,
9804 const unsigned char* prelocs,
9806 Output_section* output_section,
9807 typename elfcpp::Elf_types<size>::Elf_Off
9808 offset_in_output_section,
9809 unsigned char* view,
9810 Mips_address view_address,
9811 section_size_type view_size,
9812 unsigned char* reloc_view,
9813 section_size_type reloc_view_size)
9815 if (sh_type == elfcpp::SHT_REL)
9817 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9820 gold::relocate_relocs<size, big_endian, Classify_reloc>(
9825 offset_in_output_section,
9832 else if (sh_type == elfcpp::SHT_RELA)
9834 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9837 gold::relocate_relocs<size, big_endian, Classify_reloc>(
9842 offset_in_output_section,
9853 // Perform target-specific processing in a relocatable link. This is
9854 // only used if we use the relocation strategy RELOC_SPECIAL.
9856 template<int size, bool big_endian>
9858 Target_mips<size, big_endian>::relocate_special_relocatable(
9859 const Relocate_info<size, big_endian>* relinfo,
9860 unsigned int sh_type,
9861 const unsigned char* preloc_in,
9863 Output_section* output_section,
9864 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
9865 unsigned char* view,
9866 Mips_address view_address,
9868 unsigned char* preloc_out)
9870 // We can only handle REL type relocation sections.
9871 gold_assert(sh_type == elfcpp::SHT_REL);
9873 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
9875 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
9878 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
9880 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
9882 Mips_relobj<size, big_endian>* object =
9883 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
9884 const unsigned int local_count = object->local_symbol_count();
9886 Reltype reloc(preloc_in);
9887 Reltype_write reloc_write(preloc_out);
9889 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
9890 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9891 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9893 // Get the new symbol index.
9894 // We only use RELOC_SPECIAL strategy in local relocations.
9895 gold_assert(r_sym < local_count);
9897 // We are adjusting a section symbol. We need to find
9898 // the symbol table index of the section symbol for
9899 // the output section corresponding to input section
9900 // in which this symbol is defined.
9902 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
9903 gold_assert(is_ordinary);
9904 Output_section* os = object->output_section(shndx);
9905 gold_assert(os != NULL);
9906 gold_assert(os->needs_symtab_index());
9907 unsigned int new_symndx = os->symtab_index();
9909 // Get the new offset--the location in the output section where
9910 // this relocation should be applied.
9912 Mips_address offset = reloc.get_r_offset();
9913 Mips_address new_offset;
9914 if (offset_in_output_section != invalid_address)
9915 new_offset = offset + offset_in_output_section;
9918 section_offset_type sot_offset =
9919 convert_types<section_offset_type, Mips_address>(offset);
9920 section_offset_type new_sot_offset =
9921 output_section->output_offset(object, relinfo->data_shndx,
9923 gold_assert(new_sot_offset != -1);
9924 new_offset = new_sot_offset;
9927 // In an object file, r_offset is an offset within the section.
9928 // In an executable or dynamic object, generated by
9929 // --emit-relocs, r_offset is an absolute address.
9930 if (!parameters->options().relocatable())
9932 new_offset += view_address;
9933 if (offset_in_output_section != invalid_address)
9934 new_offset -= offset_in_output_section;
9937 reloc_write.put_r_offset(new_offset);
9938 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
9940 // Handle the reloc addend.
9941 // The relocation uses a section symbol in the input file.
9942 // We are adjusting it to use a section symbol in the output
9943 // file. The input section symbol refers to some address in
9944 // the input section. We need the relocation in the output
9945 // file to refer to that same address. This adjustment to
9946 // the addend is the same calculation we use for a simple
9947 // absolute relocation for the input section symbol.
9948 Valtype calculated_value = 0;
9949 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
9951 unsigned char* paddend = view + offset;
9952 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
9955 case elfcpp::R_MIPS_26:
9956 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
9957 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
9958 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
9959 false, &calculated_value);
9966 // Report any errors.
9967 switch (reloc_status)
9969 case Reloc_funcs::STATUS_OKAY:
9971 case Reloc_funcs::STATUS_OVERFLOW:
9972 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9973 _("relocation overflow"));
9975 case Reloc_funcs::STATUS_BAD_RELOC:
9976 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9977 _("unexpected opcode while processing relocation"));
9984 // Optimize the TLS relocation type based on what we know about the
9985 // symbol. IS_FINAL is true if the final address of this symbol is
9986 // known at link time.
9988 template<int size, bool big_endian>
9989 tls::Tls_optimization
9990 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
9992 // FIXME: Currently we do not do any TLS optimization.
9993 return tls::TLSOPT_NONE;
9996 // Scan a relocation for a local symbol.
9998 template<int size, bool big_endian>
10000 Target_mips<size, big_endian>::Scan::local(
10001 Symbol_table* symtab,
10003 Target_mips<size, big_endian>* target,
10004 Sized_relobj_file<size, big_endian>* object,
10005 unsigned int data_shndx,
10006 Output_section* output_section,
10007 const Relatype* rela,
10008 const Reltype* rel,
10009 unsigned int rel_type,
10010 unsigned int r_type,
10011 const elfcpp::Sym<size, big_endian>& lsym,
10017 Mips_address r_offset;
10018 unsigned int r_sym;
10019 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10021 if (rel_type == elfcpp::SHT_RELA)
10023 r_offset = rela->get_r_offset();
10024 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10026 r_addend = rela->get_r_addend();
10030 r_offset = rel->get_r_offset();
10031 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10036 Mips_relobj<size, big_endian>* mips_obj =
10037 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10039 if (mips_obj->is_mips16_stub_section(data_shndx))
10041 mips_obj->get_mips16_stub_section(data_shndx)
10042 ->new_local_reloc_found(r_type, r_sym);
10045 if (r_type == elfcpp::R_MIPS_NONE)
10046 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10050 if (!mips16_call_reloc(r_type)
10051 && !mips_obj->section_allows_mips16_refs(data_shndx))
10052 // This reloc would need to refer to a MIPS16 hard-float stub, if
10053 // there is one. We ignore MIPS16 stub sections and .pdr section when
10054 // looking for relocs that would need to refer to MIPS16 stubs.
10055 mips_obj->add_local_non_16bit_call(r_sym);
10057 if (r_type == elfcpp::R_MIPS16_26
10058 && !mips_obj->section_allows_mips16_refs(data_shndx))
10059 mips_obj->add_local_16bit_call(r_sym);
10063 case elfcpp::R_MIPS_GOT16:
10064 case elfcpp::R_MIPS_CALL16:
10065 case elfcpp::R_MIPS_CALL_HI16:
10066 case elfcpp::R_MIPS_CALL_LO16:
10067 case elfcpp::R_MIPS_GOT_HI16:
10068 case elfcpp::R_MIPS_GOT_LO16:
10069 case elfcpp::R_MIPS_GOT_PAGE:
10070 case elfcpp::R_MIPS_GOT_OFST:
10071 case elfcpp::R_MIPS_GOT_DISP:
10072 case elfcpp::R_MIPS_TLS_GOTTPREL:
10073 case elfcpp::R_MIPS_TLS_GD:
10074 case elfcpp::R_MIPS_TLS_LDM:
10075 case elfcpp::R_MIPS16_GOT16:
10076 case elfcpp::R_MIPS16_CALL16:
10077 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10078 case elfcpp::R_MIPS16_TLS_GD:
10079 case elfcpp::R_MIPS16_TLS_LDM:
10080 case elfcpp::R_MICROMIPS_GOT16:
10081 case elfcpp::R_MICROMIPS_CALL16:
10082 case elfcpp::R_MICROMIPS_CALL_HI16:
10083 case elfcpp::R_MICROMIPS_CALL_LO16:
10084 case elfcpp::R_MICROMIPS_GOT_HI16:
10085 case elfcpp::R_MICROMIPS_GOT_LO16:
10086 case elfcpp::R_MICROMIPS_GOT_PAGE:
10087 case elfcpp::R_MICROMIPS_GOT_OFST:
10088 case elfcpp::R_MICROMIPS_GOT_DISP:
10089 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10090 case elfcpp::R_MICROMIPS_TLS_GD:
10091 case elfcpp::R_MICROMIPS_TLS_LDM:
10092 case elfcpp::R_MIPS_EH:
10093 // We need a GOT section.
10094 target->got_section(symtab, layout);
10101 if (call_lo16_reloc(r_type)
10102 || got_lo16_reloc(r_type)
10103 || got_disp_reloc(r_type)
10104 || eh_reloc(r_type))
10106 // We may need a local GOT entry for this relocation. We
10107 // don't count R_MIPS_GOT_PAGE because we can estimate the
10108 // maximum number of pages needed by looking at the size of
10109 // the segment. Similar comments apply to R_MIPS*_GOT16 and
10110 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
10111 // R_MIPS_CALL_HI16 because these are always followed by an
10112 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10113 Mips_output_data_got<size, big_endian>* got =
10114 target->got_section(symtab, layout);
10115 bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10116 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10117 is_section_symbol);
10122 case elfcpp::R_MIPS_CALL16:
10123 case elfcpp::R_MIPS16_CALL16:
10124 case elfcpp::R_MICROMIPS_CALL16:
10125 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10126 (unsigned long)r_offset);
10129 case elfcpp::R_MIPS_GOT_PAGE:
10130 case elfcpp::R_MICROMIPS_GOT_PAGE:
10131 case elfcpp::R_MIPS16_GOT16:
10132 case elfcpp::R_MIPS_GOT16:
10133 case elfcpp::R_MIPS_GOT_HI16:
10134 case elfcpp::R_MIPS_GOT_LO16:
10135 case elfcpp::R_MICROMIPS_GOT16:
10136 case elfcpp::R_MICROMIPS_GOT_HI16:
10137 case elfcpp::R_MICROMIPS_GOT_LO16:
10139 // This relocation needs a page entry in the GOT.
10140 // Get the section contents.
10141 section_size_type view_size = 0;
10142 const unsigned char* view = object->section_contents(data_shndx,
10143 &view_size, false);
10146 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10147 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10150 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10151 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10152 object, data_shndx, r_type, r_sym, addend));
10154 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10158 case elfcpp::R_MIPS_HI16:
10159 case elfcpp::R_MIPS16_HI16:
10160 case elfcpp::R_MICROMIPS_HI16:
10161 // Record the reloc so that we can check whether the corresponding LO16
10163 if (rel_type == elfcpp::SHT_REL)
10164 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10165 object, data_shndx, r_type, r_sym, 0));
10168 case elfcpp::R_MIPS_LO16:
10169 case elfcpp::R_MIPS16_LO16:
10170 case elfcpp::R_MICROMIPS_LO16:
10172 if (rel_type != elfcpp::SHT_REL)
10175 // Find corresponding GOT16/HI16 relocation.
10177 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10178 // be immediately following. However, for the IRIX6 ABI, the next
10179 // relocation may be a composed relocation consisting of several
10180 // relocations for the same address. In that case, the R_MIPS_LO16
10181 // relocation may occur as one of these. We permit a similar
10182 // extension in general, as that is useful for GCC.
10184 // In some cases GCC dead code elimination removes the LO16 but
10185 // keeps the corresponding HI16. This is strictly speaking a
10186 // violation of the ABI but not immediately harmful.
10188 typename std::list<got16_addend<size, big_endian> >::iterator it =
10189 target->got16_addends_.begin();
10190 while (it != target->got16_addends_.end())
10192 got16_addend<size, big_endian> _got16_addend = *it;
10194 // TODO(sasa): Split got16_addends_ list into two lists - one for
10195 // GOT16 relocs and the other for HI16 relocs.
10197 // Report an error if we find HI16 or GOT16 reloc from the
10198 // previous section without the matching LO16 part.
10199 if (_got16_addend.object != object
10200 || _got16_addend.shndx != data_shndx)
10202 gold_error("Can't find matching LO16 reloc");
10206 if (_got16_addend.r_sym != r_sym
10207 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10213 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10214 // For GOT16, we need to calculate combined addend and record GOT page
10216 if (got16_reloc(_got16_addend.r_type))
10219 section_size_type view_size = 0;
10220 const unsigned char* view = object->section_contents(data_shndx,
10225 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10226 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10228 addend = (_got16_addend.addend << 16) + addend;
10229 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10233 it = target->got16_addends_.erase(it);
10241 case elfcpp::R_MIPS_32:
10242 case elfcpp::R_MIPS_REL32:
10243 case elfcpp::R_MIPS_64:
10245 if (parameters->options().output_is_position_independent())
10247 // If building a shared library (or a position-independent
10248 // executable), we need to create a dynamic relocation for
10250 if (is_readonly_section(output_section))
10252 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10253 rel_dyn->add_symbolless_local_addend(object, r_sym,
10254 elfcpp::R_MIPS_REL32,
10255 output_section, data_shndx,
10261 case elfcpp::R_MIPS_TLS_GOTTPREL:
10262 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10263 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10264 case elfcpp::R_MIPS_TLS_LDM:
10265 case elfcpp::R_MIPS16_TLS_LDM:
10266 case elfcpp::R_MICROMIPS_TLS_LDM:
10267 case elfcpp::R_MIPS_TLS_GD:
10268 case elfcpp::R_MIPS16_TLS_GD:
10269 case elfcpp::R_MICROMIPS_TLS_GD:
10271 bool output_is_shared = parameters->options().shared();
10272 const tls::Tls_optimization optimized_type
10273 = Target_mips<size, big_endian>::optimize_tls_reloc(
10274 !output_is_shared, r_type);
10277 case elfcpp::R_MIPS_TLS_GD:
10278 case elfcpp::R_MIPS16_TLS_GD:
10279 case elfcpp::R_MICROMIPS_TLS_GD:
10280 if (optimized_type == tls::TLSOPT_NONE)
10282 // Create a pair of GOT entries for the module index and
10283 // dtv-relative offset.
10284 Mips_output_data_got<size, big_endian>* got =
10285 target->got_section(symtab, layout);
10286 unsigned int shndx = lsym.get_st_shndx();
10288 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10291 object->error(_("local symbol %u has bad shndx %u"),
10295 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10300 // FIXME: TLS optimization not supported yet.
10301 gold_unreachable();
10305 case elfcpp::R_MIPS_TLS_LDM:
10306 case elfcpp::R_MIPS16_TLS_LDM:
10307 case elfcpp::R_MICROMIPS_TLS_LDM:
10308 if (optimized_type == tls::TLSOPT_NONE)
10310 // We always record LDM symbols as local with index 0.
10311 target->got_section()->record_local_got_symbol(mips_obj, 0,
10317 // FIXME: TLS optimization not supported yet.
10318 gold_unreachable();
10321 case elfcpp::R_MIPS_TLS_GOTTPREL:
10322 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10323 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10324 layout->set_has_static_tls();
10325 if (optimized_type == tls::TLSOPT_NONE)
10327 // Create a GOT entry for the tp-relative offset.
10328 Mips_output_data_got<size, big_endian>* got =
10329 target->got_section(symtab, layout);
10330 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10335 // FIXME: TLS optimization not supported yet.
10336 gold_unreachable();
10341 gold_unreachable();
10350 // Refuse some position-dependent relocations when creating a
10351 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10352 // not PIC, but we can create dynamic relocations and the result
10353 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10354 // combined with R_MIPS_GOT16.
10355 if (parameters->options().shared())
10359 case elfcpp::R_MIPS16_HI16:
10360 case elfcpp::R_MIPS_HI16:
10361 case elfcpp::R_MICROMIPS_HI16:
10362 // Don't refuse a high part relocation if it's against
10363 // no symbol (e.g. part of a compound relocation).
10369 case elfcpp::R_MIPS16_26:
10370 case elfcpp::R_MIPS_26:
10371 case elfcpp::R_MICROMIPS_26_S1:
10372 gold_error(_("%s: relocation %u against `%s' can not be used when "
10373 "making a shared object; recompile with -fPIC"),
10374 object->name().c_str(), r_type, "a local symbol");
10381 template<int size, bool big_endian>
10383 Target_mips<size, big_endian>::Scan::local(
10384 Symbol_table* symtab,
10386 Target_mips<size, big_endian>* target,
10387 Sized_relobj_file<size, big_endian>* object,
10388 unsigned int data_shndx,
10389 Output_section* output_section,
10390 const Reltype& reloc,
10391 unsigned int r_type,
10392 const elfcpp::Sym<size, big_endian>& lsym,
10405 (const Relatype*) NULL,
10409 lsym, is_discarded);
10413 template<int size, bool big_endian>
10415 Target_mips<size, big_endian>::Scan::local(
10416 Symbol_table* symtab,
10418 Target_mips<size, big_endian>* target,
10419 Sized_relobj_file<size, big_endian>* object,
10420 unsigned int data_shndx,
10421 Output_section* output_section,
10422 const Relatype& reloc,
10423 unsigned int r_type,
10424 const elfcpp::Sym<size, big_endian>& lsym,
10438 (const Reltype*) NULL,
10441 lsym, is_discarded);
10444 // Scan a relocation for a global symbol.
10446 template<int size, bool big_endian>
10448 Target_mips<size, big_endian>::Scan::global(
10449 Symbol_table* symtab,
10451 Target_mips<size, big_endian>* target,
10452 Sized_relobj_file<size, big_endian>* object,
10453 unsigned int data_shndx,
10454 Output_section* output_section,
10455 const Relatype* rela,
10456 const Reltype* rel,
10457 unsigned int rel_type,
10458 unsigned int r_type,
10461 Mips_address r_offset;
10462 unsigned int r_sym;
10463 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10465 if (rel_type == elfcpp::SHT_RELA)
10467 r_offset = rela->get_r_offset();
10468 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10470 r_addend = rela->get_r_addend();
10474 r_offset = rel->get_r_offset();
10475 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10480 Mips_relobj<size, big_endian>* mips_obj =
10481 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10482 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10484 if (mips_obj->is_mips16_stub_section(data_shndx))
10486 mips_obj->get_mips16_stub_section(data_shndx)
10487 ->new_global_reloc_found(r_type, mips_sym);
10490 if (r_type == elfcpp::R_MIPS_NONE)
10491 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10495 if (!mips16_call_reloc(r_type)
10496 && !mips_obj->section_allows_mips16_refs(data_shndx))
10497 // This reloc would need to refer to a MIPS16 hard-float stub, if
10498 // there is one. We ignore MIPS16 stub sections and .pdr section when
10499 // looking for relocs that would need to refer to MIPS16 stubs.
10500 mips_sym->set_need_fn_stub();
10502 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
10503 // section. We check here to avoid creating a dynamic reloc against
10504 // _GLOBAL_OFFSET_TABLE_.
10505 if (!target->has_got_section()
10506 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
10507 target->got_section(symtab, layout);
10509 // We need PLT entries if there are static-only relocations against
10510 // an externally-defined function. This can technically occur for
10511 // shared libraries if there are branches to the symbol, although it
10512 // is unlikely that this will be used in practice due to the short
10513 // ranges involved. It can occur for any relative or absolute relocation
10514 // in executables; in that case, the PLT entry becomes the function's
10515 // canonical address.
10516 bool static_reloc = false;
10518 // Set CAN_MAKE_DYNAMIC to true if we can convert this
10519 // relocation into a dynamic one.
10520 bool can_make_dynamic = false;
10523 case elfcpp::R_MIPS_GOT16:
10524 case elfcpp::R_MIPS_CALL16:
10525 case elfcpp::R_MIPS_CALL_HI16:
10526 case elfcpp::R_MIPS_CALL_LO16:
10527 case elfcpp::R_MIPS_GOT_HI16:
10528 case elfcpp::R_MIPS_GOT_LO16:
10529 case elfcpp::R_MIPS_GOT_PAGE:
10530 case elfcpp::R_MIPS_GOT_OFST:
10531 case elfcpp::R_MIPS_GOT_DISP:
10532 case elfcpp::R_MIPS_TLS_GOTTPREL:
10533 case elfcpp::R_MIPS_TLS_GD:
10534 case elfcpp::R_MIPS_TLS_LDM:
10535 case elfcpp::R_MIPS16_GOT16:
10536 case elfcpp::R_MIPS16_CALL16:
10537 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10538 case elfcpp::R_MIPS16_TLS_GD:
10539 case elfcpp::R_MIPS16_TLS_LDM:
10540 case elfcpp::R_MICROMIPS_GOT16:
10541 case elfcpp::R_MICROMIPS_CALL16:
10542 case elfcpp::R_MICROMIPS_CALL_HI16:
10543 case elfcpp::R_MICROMIPS_CALL_LO16:
10544 case elfcpp::R_MICROMIPS_GOT_HI16:
10545 case elfcpp::R_MICROMIPS_GOT_LO16:
10546 case elfcpp::R_MICROMIPS_GOT_PAGE:
10547 case elfcpp::R_MICROMIPS_GOT_OFST:
10548 case elfcpp::R_MICROMIPS_GOT_DISP:
10549 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10550 case elfcpp::R_MICROMIPS_TLS_GD:
10551 case elfcpp::R_MICROMIPS_TLS_LDM:
10552 case elfcpp::R_MIPS_EH:
10553 // We need a GOT section.
10554 target->got_section(symtab, layout);
10557 // This is just a hint; it can safely be ignored. Don't set
10558 // has_static_relocs for the corresponding symbol.
10559 case elfcpp::R_MIPS_JALR:
10560 case elfcpp::R_MICROMIPS_JALR:
10563 case elfcpp::R_MIPS_GPREL16:
10564 case elfcpp::R_MIPS_GPREL32:
10565 case elfcpp::R_MIPS16_GPREL:
10566 case elfcpp::R_MICROMIPS_GPREL16:
10568 // GP-relative relocations always resolve to a definition in a
10569 // regular input file, ignoring the one-definition rule. This is
10570 // important for the GP setup sequence in NewABI code, which
10571 // always resolves to a local function even if other relocations
10572 // against the symbol wouldn't.
10573 //constrain_symbol_p = FALSE;
10576 case elfcpp::R_MIPS_32:
10577 case elfcpp::R_MIPS_REL32:
10578 case elfcpp::R_MIPS_64:
10579 if ((parameters->options().shared()
10580 || (strcmp(gsym->name(), "__gnu_local_gp") != 0
10581 && (!is_readonly_section(output_section)
10582 || mips_obj->is_pic())))
10583 && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
10585 if (r_type != elfcpp::R_MIPS_REL32)
10586 mips_sym->set_pointer_equality_needed();
10587 can_make_dynamic = true;
10593 // Most static relocations require pointer equality, except
10595 mips_sym->set_pointer_equality_needed();
10599 case elfcpp::R_MIPS_26:
10600 case elfcpp::R_MIPS_PC16:
10601 case elfcpp::R_MIPS16_26:
10602 case elfcpp::R_MICROMIPS_26_S1:
10603 case elfcpp::R_MICROMIPS_PC7_S1:
10604 case elfcpp::R_MICROMIPS_PC10_S1:
10605 case elfcpp::R_MICROMIPS_PC16_S1:
10606 case elfcpp::R_MICROMIPS_PC23_S2:
10607 static_reloc = true;
10608 mips_sym->set_has_static_relocs();
10612 // If there are call relocations against an externally-defined symbol,
10613 // see whether we can create a MIPS lazy-binding stub for it. We can
10614 // only do this if all references to the function are through call
10615 // relocations, and in that case, the traditional lazy-binding stubs
10616 // are much more efficient than PLT entries.
10619 case elfcpp::R_MIPS16_CALL16:
10620 case elfcpp::R_MIPS_CALL16:
10621 case elfcpp::R_MIPS_CALL_HI16:
10622 case elfcpp::R_MIPS_CALL_LO16:
10623 case elfcpp::R_MIPS_JALR:
10624 case elfcpp::R_MICROMIPS_CALL16:
10625 case elfcpp::R_MICROMIPS_CALL_HI16:
10626 case elfcpp::R_MICROMIPS_CALL_LO16:
10627 case elfcpp::R_MICROMIPS_JALR:
10628 if (!mips_sym->no_lazy_stub())
10630 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
10631 // Calls from shared objects to undefined symbols of type
10632 // STT_NOTYPE need lazy-binding stub.
10633 || (mips_sym->is_undefined() && parameters->options().shared()))
10634 target->mips_stubs_section(layout)->make_entry(mips_sym);
10639 // We must not create a stub for a symbol that has relocations
10640 // related to taking the function's address.
10641 mips_sym->set_no_lazy_stub();
10642 target->remove_lazy_stub_entry(mips_sym);
10647 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
10648 mips_sym->is_mips16()))
10649 mips_sym->set_has_nonpic_branches();
10651 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
10652 // and has a special meaning.
10653 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
10654 && strcmp(gsym->name(), "_gp_disp") == 0
10655 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
10656 if (static_reloc && gsym->needs_plt_entry())
10658 target->make_plt_entry(symtab, layout, mips_sym, r_type);
10660 // Since this is not a PC-relative relocation, we may be
10661 // taking the address of a function. In that case we need to
10662 // set the entry in the dynamic symbol table to the address of
10664 if (gsym->is_from_dynobj() && !parameters->options().shared())
10666 gsym->set_needs_dynsym_value();
10667 // We distinguish between PLT entries and lazy-binding stubs by
10668 // giving the former an st_other value of STO_MIPS_PLT. Set the
10669 // flag if there are any relocations in the binary where pointer
10670 // equality matters.
10671 if (mips_sym->pointer_equality_needed())
10672 mips_sym->set_mips_plt();
10675 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
10677 // Absolute addressing relocations.
10678 // Make a dynamic relocation if necessary.
10679 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
10681 if (gsym->may_need_copy_reloc())
10683 target->copy_reloc(symtab, layout, object, data_shndx,
10684 output_section, gsym, r_type, r_offset);
10686 else if (can_make_dynamic)
10688 // Create .rel.dyn section.
10689 target->rel_dyn_section(layout);
10690 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
10691 data_shndx, output_section, r_offset);
10694 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
10699 bool for_call = false;
10702 case elfcpp::R_MIPS_CALL16:
10703 case elfcpp::R_MIPS16_CALL16:
10704 case elfcpp::R_MICROMIPS_CALL16:
10705 case elfcpp::R_MIPS_CALL_HI16:
10706 case elfcpp::R_MIPS_CALL_LO16:
10707 case elfcpp::R_MICROMIPS_CALL_HI16:
10708 case elfcpp::R_MICROMIPS_CALL_LO16:
10712 case elfcpp::R_MIPS16_GOT16:
10713 case elfcpp::R_MIPS_GOT16:
10714 case elfcpp::R_MIPS_GOT_HI16:
10715 case elfcpp::R_MIPS_GOT_LO16:
10716 case elfcpp::R_MICROMIPS_GOT16:
10717 case elfcpp::R_MICROMIPS_GOT_HI16:
10718 case elfcpp::R_MICROMIPS_GOT_LO16:
10719 case elfcpp::R_MIPS_GOT_DISP:
10720 case elfcpp::R_MICROMIPS_GOT_DISP:
10721 case elfcpp::R_MIPS_EH:
10723 // The symbol requires a GOT entry.
10724 Mips_output_data_got<size, big_endian>* got =
10725 target->got_section(symtab, layout);
10726 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10728 mips_sym->set_global_got_area(GGA_NORMAL);
10732 case elfcpp::R_MIPS_GOT_PAGE:
10733 case elfcpp::R_MICROMIPS_GOT_PAGE:
10735 // This relocation needs a page entry in the GOT.
10736 // Get the section contents.
10737 section_size_type view_size = 0;
10738 const unsigned char* view =
10739 object->section_contents(data_shndx, &view_size, false);
10742 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10743 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10745 Mips_output_data_got<size, big_endian>* got =
10746 target->got_section(symtab, layout);
10747 got->record_got_page_entry(mips_obj, r_sym, addend);
10749 // If this is a global, overridable symbol, GOT_PAGE will
10750 // decay to GOT_DISP, so we'll need a GOT entry for it.
10751 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
10752 && !mips_sym->object()->is_dynamic()
10753 && !mips_sym->is_undefined());
10755 || (parameters->options().output_is_position_independent()
10756 && !parameters->options().Bsymbolic()
10757 && !mips_sym->is_forced_local()))
10759 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10761 mips_sym->set_global_got_area(GGA_NORMAL);
10766 case elfcpp::R_MIPS_TLS_GOTTPREL:
10767 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10768 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10769 case elfcpp::R_MIPS_TLS_LDM:
10770 case elfcpp::R_MIPS16_TLS_LDM:
10771 case elfcpp::R_MICROMIPS_TLS_LDM:
10772 case elfcpp::R_MIPS_TLS_GD:
10773 case elfcpp::R_MIPS16_TLS_GD:
10774 case elfcpp::R_MICROMIPS_TLS_GD:
10776 const bool is_final = gsym->final_value_is_known();
10777 const tls::Tls_optimization optimized_type =
10778 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
10782 case elfcpp::R_MIPS_TLS_GD:
10783 case elfcpp::R_MIPS16_TLS_GD:
10784 case elfcpp::R_MICROMIPS_TLS_GD:
10785 if (optimized_type == tls::TLSOPT_NONE)
10787 // Create a pair of GOT entries for the module index and
10788 // dtv-relative offset.
10789 Mips_output_data_got<size, big_endian>* got =
10790 target->got_section(symtab, layout);
10791 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10796 // FIXME: TLS optimization not supported yet.
10797 gold_unreachable();
10801 case elfcpp::R_MIPS_TLS_LDM:
10802 case elfcpp::R_MIPS16_TLS_LDM:
10803 case elfcpp::R_MICROMIPS_TLS_LDM:
10804 if (optimized_type == tls::TLSOPT_NONE)
10806 // We always record LDM symbols as local with index 0.
10807 target->got_section()->record_local_got_symbol(mips_obj, 0,
10813 // FIXME: TLS optimization not supported yet.
10814 gold_unreachable();
10817 case elfcpp::R_MIPS_TLS_GOTTPREL:
10818 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10819 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10820 layout->set_has_static_tls();
10821 if (optimized_type == tls::TLSOPT_NONE)
10823 // Create a GOT entry for the tp-relative offset.
10824 Mips_output_data_got<size, big_endian>* got =
10825 target->got_section(symtab, layout);
10826 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
10831 // FIXME: TLS optimization not supported yet.
10832 gold_unreachable();
10837 gold_unreachable();
10841 case elfcpp::R_MIPS_COPY:
10842 case elfcpp::R_MIPS_JUMP_SLOT:
10843 // These are relocations which should only be seen by the
10844 // dynamic linker, and should never be seen here.
10845 gold_error(_("%s: unexpected reloc %u in object file"),
10846 object->name().c_str(), r_type);
10853 // Refuse some position-dependent relocations when creating a
10854 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10855 // not PIC, but we can create dynamic relocations and the result
10856 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10857 // combined with R_MIPS_GOT16.
10858 if (parameters->options().shared())
10862 case elfcpp::R_MIPS16_HI16:
10863 case elfcpp::R_MIPS_HI16:
10864 case elfcpp::R_MICROMIPS_HI16:
10865 // Don't refuse a high part relocation if it's against
10866 // no symbol (e.g. part of a compound relocation).
10870 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
10871 // and has a special meaning.
10872 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
10877 case elfcpp::R_MIPS16_26:
10878 case elfcpp::R_MIPS_26:
10879 case elfcpp::R_MICROMIPS_26_S1:
10880 gold_error(_("%s: relocation %u against `%s' can not be used when "
10881 "making a shared object; recompile with -fPIC"),
10882 object->name().c_str(), r_type, gsym->name());
10889 template<int size, bool big_endian>
10891 Target_mips<size, big_endian>::Scan::global(
10892 Symbol_table* symtab,
10894 Target_mips<size, big_endian>* target,
10895 Sized_relobj_file<size, big_endian>* object,
10896 unsigned int data_shndx,
10897 Output_section* output_section,
10898 const Relatype& reloc,
10899 unsigned int r_type,
10910 (const Reltype*) NULL,
10916 template<int size, bool big_endian>
10918 Target_mips<size, big_endian>::Scan::global(
10919 Symbol_table* symtab,
10921 Target_mips<size, big_endian>* target,
10922 Sized_relobj_file<size, big_endian>* object,
10923 unsigned int data_shndx,
10924 Output_section* output_section,
10925 const Reltype& reloc,
10926 unsigned int r_type,
10936 (const Relatype*) NULL,
10943 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
10944 // In cases where Scan::local() or Scan::global() has created
10945 // a dynamic relocation, the addend of the relocation is carried
10946 // in the data, and we must not apply the static relocation.
10948 template<int size, bool big_endian>
10950 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
10951 const Mips_symbol<size>* gsym,
10952 unsigned int r_type,
10953 Output_section* output_section,
10954 Target_mips* target)
10956 // If the output section is not allocated, then we didn't call
10957 // scan_relocs, we didn't create a dynamic reloc, and we must apply
10959 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
10966 // For global symbols, we use the same helper routines used in the
10968 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
10969 && !gsym->may_need_copy_reloc())
10971 // We have generated dynamic reloc (R_MIPS_REL32).
10973 bool multi_got = false;
10974 if (target->has_got_section())
10975 multi_got = target->got_section()->multi_got();
10976 bool has_got_offset;
10978 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
10980 has_got_offset = gsym->global_gotoffset() != -1U;
10981 if (!has_got_offset)
10984 // Apply the relocation only if the symbol is in the local got.
10985 // Do not apply the relocation if the symbol is in the global
10987 return symbol_references_local(gsym, gsym->has_dynsym_index());
10990 // We have not generated dynamic reloc.
10995 // Perform a relocation.
10997 template<int size, bool big_endian>
10999 Target_mips<size, big_endian>::Relocate::relocate(
11000 const Relocate_info<size, big_endian>* relinfo,
11001 unsigned int rel_type,
11002 Target_mips* target,
11003 Output_section* output_section,
11005 const unsigned char* preloc,
11006 const Sized_symbol<size>* gsym,
11007 const Symbol_value<size>* psymval,
11008 unsigned char* view,
11009 Mips_address address,
11012 Mips_address r_offset;
11013 unsigned int r_sym;
11014 unsigned int r_type;
11015 unsigned int r_type2;
11016 unsigned int r_type3;
11017 unsigned char r_ssym;
11018 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11020 if (rel_type == elfcpp::SHT_RELA)
11022 const Relatype rela(preloc);
11023 r_offset = rela.get_r_offset();
11024 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11026 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11028 r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11029 get_r_type2(&rela);
11030 r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11031 get_r_type3(&rela);
11032 r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11034 r_addend = rela.get_r_addend();
11038 const Reltype rel(preloc);
11039 r_offset = rel.get_r_offset();
11040 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11042 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11050 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11051 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11053 Mips_relobj<size, big_endian>* object =
11054 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11056 bool target_is_16_bit_code = false;
11057 bool target_is_micromips_code = false;
11058 bool cross_mode_jump;
11060 Symbol_value<size> symval;
11062 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11064 bool changed_symbol_value = false;
11067 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11068 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11069 if (target_is_16_bit_code || target_is_micromips_code)
11071 // MIPS16/microMIPS text labels should be treated as odd.
11072 symval.set_output_value(psymval->value(object, 1));
11074 changed_symbol_value = true;
11079 target_is_16_bit_code = mips_sym->is_mips16();
11080 target_is_micromips_code = mips_sym->is_micromips();
11082 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11083 // it odd. This will cause something like .word SYM to come up with
11084 // the right value when it is loaded into the PC.
11086 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11087 && psymval->value(object, 0) != 0)
11089 symval.set_output_value(psymval->value(object, 0) | 1);
11091 changed_symbol_value = true;
11094 // Pick the value to use for symbols defined in shared objects.
11095 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11096 || mips_sym->has_lazy_stub())
11098 Mips_address value;
11099 if (!mips_sym->has_lazy_stub())
11101 // Prefer a standard MIPS PLT entry.
11102 if (mips_sym->has_mips_plt_offset())
11104 value = target->plt_section()->mips_entry_address(mips_sym);
11105 target_is_micromips_code = false;
11106 target_is_16_bit_code = false;
11110 value = (target->plt_section()->comp_entry_address(mips_sym)
11112 if (target->is_output_micromips())
11113 target_is_micromips_code = true;
11115 target_is_16_bit_code = true;
11119 value = target->mips_stubs_section()->stub_address(mips_sym);
11121 symval.set_output_value(value);
11126 // TRUE if the symbol referred to by this relocation is "_gp_disp".
11127 // Note that such a symbol must always be a global symbol.
11128 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11129 && !object->is_newabi());
11131 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11132 // Note that such a symbol must always be a global symbol.
11133 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11138 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11139 gold_error_at_location(relinfo, relnum, r_offset,
11140 _("relocations against _gp_disp are permitted only"
11141 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11143 else if (gnu_local_gp)
11145 // __gnu_local_gp is _gp symbol.
11146 symval.set_output_value(target->adjusted_gp_value(object));
11150 // If this is a reference to a 16-bit function with a stub, we need
11151 // to redirect the relocation to the stub unless:
11153 // (a) the relocation is for a MIPS16 JAL;
11155 // (b) the relocation is for a MIPS16 PIC call, and there are no
11156 // non-MIPS16 uses of the GOT slot; or
11158 // (c) the section allows direct references to MIPS16 functions.
11159 if (r_type != elfcpp::R_MIPS16_26
11160 && !parameters->options().relocatable()
11161 && ((mips_sym != NULL
11162 && mips_sym->has_mips16_fn_stub()
11163 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11164 || (mips_sym == NULL
11165 && object->get_local_mips16_fn_stub(r_sym) != NULL))
11166 && !object->section_allows_mips16_refs(relinfo->data_shndx))
11168 // This is a 32- or 64-bit call to a 16-bit function. We should
11169 // have already noticed that we were going to need the
11171 Mips_address value;
11172 if (mips_sym == NULL)
11173 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11176 gold_assert(mips_sym->need_fn_stub());
11177 if (mips_sym->has_la25_stub())
11178 value = target->la25_stub_section()->stub_address(mips_sym);
11181 value = mips_sym->template
11182 get_mips16_fn_stub<big_endian>()->output_address();
11185 symval.set_output_value(value);
11187 changed_symbol_value = true;
11189 // The target is 16-bit, but the stub isn't.
11190 target_is_16_bit_code = false;
11192 // If this is a MIPS16 call with a stub, that is made through the PLT or
11193 // to a standard MIPS function, we need to redirect the call to the stub.
11194 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11195 // indirect calls should use an indirect stub instead.
11196 else if (r_type == elfcpp::R_MIPS16_26 && !parameters->options().relocatable()
11197 && ((mips_sym != NULL
11198 && (mips_sym->has_mips16_call_stub()
11199 || mips_sym->has_mips16_call_fp_stub()))
11200 || (mips_sym == NULL
11201 && object->get_local_mips16_call_stub(r_sym) != NULL))
11202 && ((mips_sym != NULL && mips_sym->has_plt_offset())
11203 || !target_is_16_bit_code))
11205 Mips16_stub_section<size, big_endian>* call_stub;
11206 if (mips_sym == NULL)
11207 call_stub = object->get_local_mips16_call_stub(r_sym);
11210 // If both call_stub and call_fp_stub are defined, we can figure
11211 // out which one to use by checking which one appears in the input
11213 if (mips_sym->has_mips16_call_stub()
11214 && mips_sym->has_mips16_call_fp_stub())
11217 for (unsigned int i = 1; i < object->shnum(); ++i)
11219 if (object->is_mips16_call_fp_stub_section(i))
11221 call_stub = mips_sym->template
11222 get_mips16_call_fp_stub<big_endian>();
11227 if (call_stub == NULL)
11229 mips_sym->template get_mips16_call_stub<big_endian>();
11231 else if (mips_sym->has_mips16_call_stub())
11232 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11234 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11237 symval.set_output_value(call_stub->output_address());
11239 changed_symbol_value = true;
11241 // If this is a direct call to a PIC function, redirect to the
11243 else if (mips_sym != NULL
11244 && mips_sym->has_la25_stub()
11245 && relocation_needs_la25_stub<size, big_endian>(
11246 object, r_type, target_is_16_bit_code))
11248 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11249 if (mips_sym->is_micromips())
11251 symval.set_output_value(value);
11254 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11255 // entry is used if a standard PLT entry has also been made.
11256 else if ((r_type == elfcpp::R_MIPS16_26
11257 || r_type == elfcpp::R_MICROMIPS_26_S1)
11258 && !parameters->options().relocatable()
11259 && mips_sym != NULL
11260 && mips_sym->has_plt_offset()
11261 && mips_sym->has_comp_plt_offset()
11262 && mips_sym->has_mips_plt_offset())
11264 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11266 symval.set_output_value(value);
11269 target_is_16_bit_code = !target->is_output_micromips();
11270 target_is_micromips_code = target->is_output_micromips();
11273 // Make sure MIPS16 and microMIPS are not used together.
11274 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11275 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11277 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11280 // Calls from 16-bit code to 32-bit code and vice versa require the
11281 // mode change. However, we can ignore calls to undefined weak symbols,
11282 // which should never be executed at runtime. This exception is important
11283 // because the assembly writer may have "known" that any definition of the
11284 // symbol would be 16-bit code, and that direct jumps were therefore
11287 (!parameters->options().relocatable()
11288 && !(gsym != NULL && gsym->is_weak_undefined())
11289 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11290 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11291 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11292 && (target_is_16_bit_code || target_is_micromips_code))));
11294 bool local = (mips_sym == NULL
11295 || (mips_sym->got_only_for_calls()
11296 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11297 : symbol_references_local(mips_sym,
11298 mips_sym->has_dynsym_index())));
11300 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11301 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
11302 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11303 if (got_page_reloc(r_type) && !local)
11304 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11305 : elfcpp::R_MIPS_GOT_DISP);
11307 unsigned int got_offset = 0;
11310 bool calculate_only = false;
11311 Valtype calculated_value = 0;
11312 bool extract_addend = rel_type == elfcpp::SHT_REL;
11313 unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11315 Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11317 // For Mips64 N64 ABI, there may be up to three operations specified per
11318 // record, by the fields r_type, r_type2, and r_type3. The first operation
11319 // takes its addend from the relocation record. Each subsequent operation
11320 // takes as its addend the result of the previous operation.
11321 // The first operation in a record which references a symbol uses the symbol
11322 // implied by r_sym. The next operation in a record which references a symbol
11323 // uses the special symbol value given by the r_ssym field. A third operation
11324 // in a record which references a symbol will assume a NULL symbol,
11325 // i.e. value zero.
11328 // Check if a record references to a symbol.
11329 for (unsigned int i = 0; i < 3; ++i)
11331 if (r_types[i] == elfcpp::R_MIPS_NONE)
11335 // Check if the next relocation is for the same instruction.
11336 calculate_only = i == 2 ? false
11337 : r_types[i+1] != elfcpp::R_MIPS_NONE;
11339 if (object->is_n64())
11343 // Handle special symbol for r_type2 relocation type.
11347 symval.set_output_value(0);
11350 symval.set_output_value(target->gp_value());
11353 symval.set_output_value(object->gp_value());
11356 symval.set_output_value(address);
11359 gold_unreachable();
11365 // For r_type3 symbol value is 0.
11366 symval.set_output_value(0);
11370 bool update_got_entry = false;
11371 switch (r_types[i])
11373 case elfcpp::R_MIPS_NONE:
11375 case elfcpp::R_MIPS_16:
11376 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11377 extract_addend, calculate_only,
11378 &calculated_value);
11381 case elfcpp::R_MIPS_32:
11382 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11384 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11385 extract_addend, calculate_only,
11386 &calculated_value);
11387 if (mips_sym != NULL
11388 && (mips_sym->is_mips16() || mips_sym->is_micromips())
11389 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11391 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11392 // already updated by adding +1.
11393 if (mips_sym->has_mips16_fn_stub())
11395 gold_assert(mips_sym->need_fn_stub());
11396 Mips16_stub_section<size, big_endian>* fn_stub =
11397 mips_sym->template get_mips16_fn_stub<big_endian>();
11399 symval.set_output_value(fn_stub->output_address());
11402 got_offset = mips_sym->global_gotoffset();
11403 update_got_entry = true;
11407 case elfcpp::R_MIPS_64:
11408 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11410 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11411 extract_addend, calculate_only,
11412 &calculated_value, false);
11413 else if (target->is_output_n64() && r_addend != 0)
11414 // Only apply the addend. The static relocation was RELA, but the
11415 // dynamic relocation is REL, so we need to apply the addend.
11416 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11417 extract_addend, calculate_only,
11418 &calculated_value, true);
11420 case elfcpp::R_MIPS_REL32:
11421 gold_unreachable();
11423 case elfcpp::R_MIPS_PC32:
11424 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11425 r_addend, extract_addend,
11427 &calculated_value);
11430 case elfcpp::R_MIPS16_26:
11431 // The calculation for R_MIPS16_26 is just the same as for an
11432 // R_MIPS_26. It's only the storage of the relocated field into
11433 // the output file that's different. So, we just fall through to the
11434 // R_MIPS_26 case here.
11435 case elfcpp::R_MIPS_26:
11436 case elfcpp::R_MICROMIPS_26_S1:
11437 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11438 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11439 r_types[i], target->jal_to_bal(), calculate_only,
11440 &calculated_value);
11443 case elfcpp::R_MIPS_HI16:
11444 case elfcpp::R_MIPS16_HI16:
11445 case elfcpp::R_MICROMIPS_HI16:
11446 if (rel_type == elfcpp::SHT_RELA)
11447 reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11449 gp_disp, r_types[i],
11451 target, calculate_only,
11452 &calculated_value);
11453 else if (rel_type == elfcpp::SHT_REL)
11454 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11455 address, gp_disp, r_types[i],
11456 r_sym, extract_addend);
11458 gold_unreachable();
11461 case elfcpp::R_MIPS_LO16:
11462 case elfcpp::R_MIPS16_LO16:
11463 case elfcpp::R_MICROMIPS_LO16:
11464 case elfcpp::R_MICROMIPS_HI0_LO16:
11465 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11466 r_addend, extract_addend, address,
11467 gp_disp, r_types[i], r_sym,
11468 rel_type, calculate_only,
11469 &calculated_value);
11472 case elfcpp::R_MIPS_LITERAL:
11473 case elfcpp::R_MICROMIPS_LITERAL:
11474 // Because we don't merge literal sections, we can handle this
11475 // just like R_MIPS_GPREL16. In the long run, we should merge
11476 // shared literals, and then we will need to additional work
11481 case elfcpp::R_MIPS_GPREL16:
11482 case elfcpp::R_MIPS16_GPREL:
11483 case elfcpp::R_MICROMIPS_GPREL7_S2:
11484 case elfcpp::R_MICROMIPS_GPREL16:
11485 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11486 target->adjusted_gp_value(object),
11487 r_addend, extract_addend,
11488 gsym == NULL, r_types[i],
11489 calculate_only, &calculated_value);
11492 case elfcpp::R_MIPS_PC16:
11493 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11494 r_addend, extract_addend,
11496 &calculated_value);
11498 case elfcpp::R_MICROMIPS_PC7_S1:
11499 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
11503 &calculated_value);
11505 case elfcpp::R_MICROMIPS_PC10_S1:
11506 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
11508 r_addend, extract_addend,
11510 &calculated_value);
11512 case elfcpp::R_MICROMIPS_PC16_S1:
11513 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
11515 r_addend, extract_addend,
11517 &calculated_value);
11519 case elfcpp::R_MIPS_GPREL32:
11520 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
11521 target->adjusted_gp_value(object),
11522 r_addend, extract_addend,
11524 &calculated_value);
11526 case elfcpp::R_MIPS_GOT_HI16:
11527 case elfcpp::R_MIPS_CALL_HI16:
11528 case elfcpp::R_MICROMIPS_GOT_HI16:
11529 case elfcpp::R_MICROMIPS_CALL_HI16:
11531 got_offset = target->got_section()->got_offset(gsym,
11535 got_offset = target->got_section()->got_offset(r_sym,
11538 gp_offset = target->got_section()->gp_offset(got_offset, object);
11539 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
11541 &calculated_value);
11542 update_got_entry = changed_symbol_value;
11545 case elfcpp::R_MIPS_GOT_LO16:
11546 case elfcpp::R_MIPS_CALL_LO16:
11547 case elfcpp::R_MICROMIPS_GOT_LO16:
11548 case elfcpp::R_MICROMIPS_CALL_LO16:
11550 got_offset = target->got_section()->got_offset(gsym,
11554 got_offset = target->got_section()->got_offset(r_sym,
11557 gp_offset = target->got_section()->gp_offset(got_offset, object);
11558 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
11560 &calculated_value);
11561 update_got_entry = changed_symbol_value;
11564 case elfcpp::R_MIPS_GOT_DISP:
11565 case elfcpp::R_MICROMIPS_GOT_DISP:
11566 case elfcpp::R_MIPS_EH:
11568 got_offset = target->got_section()->got_offset(gsym,
11572 got_offset = target->got_section()->got_offset(r_sym,
11575 gp_offset = target->got_section()->gp_offset(got_offset, object);
11576 if (eh_reloc(r_types[i]))
11577 reloc_status = Reloc_funcs::releh(view, gp_offset,
11579 &calculated_value);
11581 reloc_status = Reloc_funcs::relgot(view, gp_offset,
11583 &calculated_value);
11585 case elfcpp::R_MIPS_CALL16:
11586 case elfcpp::R_MIPS16_CALL16:
11587 case elfcpp::R_MICROMIPS_CALL16:
11588 gold_assert(gsym != NULL);
11589 got_offset = target->got_section()->got_offset(gsym,
11592 gp_offset = target->got_section()->gp_offset(got_offset, object);
11593 reloc_status = Reloc_funcs::relgot(view, gp_offset,
11594 calculate_only, &calculated_value);
11595 // TODO(sasa): We should also initialize update_got_entry
11596 // in other place swhere relgot is called.
11597 update_got_entry = changed_symbol_value;
11600 case elfcpp::R_MIPS_GOT16:
11601 case elfcpp::R_MIPS16_GOT16:
11602 case elfcpp::R_MICROMIPS_GOT16:
11605 got_offset = target->got_section()->got_offset(gsym,
11608 gp_offset = target->got_section()->gp_offset(got_offset, object);
11609 reloc_status = Reloc_funcs::relgot(view, gp_offset,
11611 &calculated_value);
11615 if (rel_type == elfcpp::SHT_RELA)
11616 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
11621 &calculated_value);
11622 else if (rel_type == elfcpp::SHT_REL)
11623 reloc_status = Reloc_funcs::relgot16_local(view, object,
11626 r_types[i], r_sym);
11628 gold_unreachable();
11630 update_got_entry = changed_symbol_value;
11633 case elfcpp::R_MIPS_TLS_GD:
11634 case elfcpp::R_MIPS16_TLS_GD:
11635 case elfcpp::R_MICROMIPS_TLS_GD:
11637 got_offset = target->got_section()->got_offset(gsym,
11641 got_offset = target->got_section()->got_offset(r_sym,
11644 gp_offset = target->got_section()->gp_offset(got_offset, object);
11645 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
11646 &calculated_value);
11649 case elfcpp::R_MIPS_TLS_GOTTPREL:
11650 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11651 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11653 got_offset = target->got_section()->got_offset(gsym,
11654 GOT_TYPE_TLS_OFFSET,
11657 got_offset = target->got_section()->got_offset(r_sym,
11658 GOT_TYPE_TLS_OFFSET,
11660 gp_offset = target->got_section()->gp_offset(got_offset, object);
11661 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
11662 &calculated_value);
11665 case elfcpp::R_MIPS_TLS_LDM:
11666 case elfcpp::R_MIPS16_TLS_LDM:
11667 case elfcpp::R_MICROMIPS_TLS_LDM:
11668 // Relocate the field with the offset of the GOT entry for
11669 // the module index.
11670 got_offset = target->got_section()->tls_ldm_offset(object);
11671 gp_offset = target->got_section()->gp_offset(got_offset, object);
11672 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
11673 &calculated_value);
11676 case elfcpp::R_MIPS_GOT_PAGE:
11677 case elfcpp::R_MICROMIPS_GOT_PAGE:
11678 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
11679 r_addend, extract_addend,
11681 &calculated_value);
11684 case elfcpp::R_MIPS_GOT_OFST:
11685 case elfcpp::R_MICROMIPS_GOT_OFST:
11686 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
11687 r_addend, extract_addend,
11688 local, calculate_only,
11689 &calculated_value);
11692 case elfcpp::R_MIPS_JALR:
11693 case elfcpp::R_MICROMIPS_JALR:
11694 // This relocation is only a hint. In some cases, we optimize
11695 // it into a bal instruction. But we don't try to optimize
11696 // when the symbol does not resolve locally.
11698 || symbol_calls_local(gsym, gsym->has_dynsym_index()))
11699 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
11700 r_addend, extract_addend,
11701 cross_mode_jump, r_types[i],
11702 target->jalr_to_bal(),
11705 &calculated_value);
11708 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
11709 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
11710 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
11711 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
11712 elfcpp::DTP_OFFSET, r_addend,
11713 extract_addend, calculate_only,
11714 &calculated_value);
11716 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
11717 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
11718 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
11719 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
11720 elfcpp::DTP_OFFSET, r_addend,
11721 extract_addend, calculate_only,
11722 &calculated_value);
11724 case elfcpp::R_MIPS_TLS_DTPREL32:
11725 case elfcpp::R_MIPS_TLS_DTPREL64:
11726 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
11727 elfcpp::DTP_OFFSET, r_addend,
11728 extract_addend, calculate_only,
11729 &calculated_value);
11731 case elfcpp::R_MIPS_TLS_TPREL_HI16:
11732 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
11733 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
11734 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
11735 elfcpp::TP_OFFSET, r_addend,
11736 extract_addend, calculate_only,
11737 &calculated_value);
11739 case elfcpp::R_MIPS_TLS_TPREL_LO16:
11740 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
11741 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
11742 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
11743 elfcpp::TP_OFFSET, r_addend,
11744 extract_addend, calculate_only,
11745 &calculated_value);
11747 case elfcpp::R_MIPS_TLS_TPREL32:
11748 case elfcpp::R_MIPS_TLS_TPREL64:
11749 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
11750 elfcpp::TP_OFFSET, r_addend,
11751 extract_addend, calculate_only,
11752 &calculated_value);
11754 case elfcpp::R_MIPS_SUB:
11755 case elfcpp::R_MICROMIPS_SUB:
11756 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
11758 calculate_only, &calculated_value);
11761 gold_error_at_location(relinfo, relnum, r_offset,
11762 _("unsupported reloc %u"), r_types[i]);
11766 if (update_got_entry)
11768 Mips_output_data_got<size, big_endian>* got = target->got_section();
11769 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
11770 got->update_got_entry(got->get_primary_got_offset(mips_sym),
11771 psymval->value(object, 0));
11773 got->update_got_entry(got_offset, psymval->value(object, 0));
11776 r_addend = calculated_value;
11779 bool jal_shuffle = jal_reloc(r_type) ? !parameters->options().relocatable()
11781 Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
11783 // Report any errors.
11784 switch (reloc_status)
11786 case Reloc_funcs::STATUS_OKAY:
11788 case Reloc_funcs::STATUS_OVERFLOW:
11789 gold_error_at_location(relinfo, relnum, r_offset,
11790 _("relocation overflow"));
11792 case Reloc_funcs::STATUS_BAD_RELOC:
11793 gold_error_at_location(relinfo, relnum, r_offset,
11794 _("unexpected opcode while processing relocation"));
11797 gold_unreachable();
11803 // Get the Reference_flags for a particular relocation.
11805 template<int size, bool big_endian>
11807 Target_mips<size, big_endian>::Scan::get_reference_flags(
11808 unsigned int r_type)
11812 case elfcpp::R_MIPS_NONE:
11813 // No symbol reference.
11816 case elfcpp::R_MIPS_16:
11817 case elfcpp::R_MIPS_32:
11818 case elfcpp::R_MIPS_64:
11819 case elfcpp::R_MIPS_HI16:
11820 case elfcpp::R_MIPS_LO16:
11821 case elfcpp::R_MIPS16_HI16:
11822 case elfcpp::R_MIPS16_LO16:
11823 case elfcpp::R_MICROMIPS_HI16:
11824 case elfcpp::R_MICROMIPS_LO16:
11825 return Symbol::ABSOLUTE_REF;
11827 case elfcpp::R_MIPS_26:
11828 case elfcpp::R_MIPS16_26:
11829 case elfcpp::R_MICROMIPS_26_S1:
11830 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
11832 case elfcpp::R_MIPS_GPREL32:
11833 case elfcpp::R_MIPS_GPREL16:
11834 case elfcpp::R_MIPS_REL32:
11835 case elfcpp::R_MIPS16_GPREL:
11836 return Symbol::RELATIVE_REF;
11838 case elfcpp::R_MIPS_PC16:
11839 case elfcpp::R_MIPS_PC32:
11840 case elfcpp::R_MIPS_JALR:
11841 case elfcpp::R_MICROMIPS_JALR:
11842 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
11844 case elfcpp::R_MIPS_GOT16:
11845 case elfcpp::R_MIPS_CALL16:
11846 case elfcpp::R_MIPS_GOT_DISP:
11847 case elfcpp::R_MIPS_GOT_HI16:
11848 case elfcpp::R_MIPS_GOT_LO16:
11849 case elfcpp::R_MIPS_CALL_HI16:
11850 case elfcpp::R_MIPS_CALL_LO16:
11851 case elfcpp::R_MIPS_LITERAL:
11852 case elfcpp::R_MIPS_GOT_PAGE:
11853 case elfcpp::R_MIPS_GOT_OFST:
11854 case elfcpp::R_MIPS16_GOT16:
11855 case elfcpp::R_MIPS16_CALL16:
11856 case elfcpp::R_MICROMIPS_GOT16:
11857 case elfcpp::R_MICROMIPS_CALL16:
11858 case elfcpp::R_MICROMIPS_GOT_HI16:
11859 case elfcpp::R_MICROMIPS_GOT_LO16:
11860 case elfcpp::R_MICROMIPS_CALL_HI16:
11861 case elfcpp::R_MICROMIPS_CALL_LO16:
11862 case elfcpp::R_MIPS_EH:
11863 // Absolute in GOT.
11864 return Symbol::RELATIVE_REF;
11866 case elfcpp::R_MIPS_TLS_DTPMOD32:
11867 case elfcpp::R_MIPS_TLS_DTPREL32:
11868 case elfcpp::R_MIPS_TLS_DTPMOD64:
11869 case elfcpp::R_MIPS_TLS_DTPREL64:
11870 case elfcpp::R_MIPS_TLS_GD:
11871 case elfcpp::R_MIPS_TLS_LDM:
11872 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
11873 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
11874 case elfcpp::R_MIPS_TLS_GOTTPREL:
11875 case elfcpp::R_MIPS_TLS_TPREL32:
11876 case elfcpp::R_MIPS_TLS_TPREL64:
11877 case elfcpp::R_MIPS_TLS_TPREL_HI16:
11878 case elfcpp::R_MIPS_TLS_TPREL_LO16:
11879 case elfcpp::R_MIPS16_TLS_GD:
11880 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11881 case elfcpp::R_MICROMIPS_TLS_GD:
11882 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11883 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
11884 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
11885 return Symbol::TLS_REF;
11887 case elfcpp::R_MIPS_COPY:
11888 case elfcpp::R_MIPS_JUMP_SLOT:
11890 gold_unreachable();
11891 // Not expected. We will give an error later.
11896 // Report an unsupported relocation against a local symbol.
11898 template<int size, bool big_endian>
11900 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
11901 Sized_relobj_file<size, big_endian>* object,
11902 unsigned int r_type)
11904 gold_error(_("%s: unsupported reloc %u against local symbol"),
11905 object->name().c_str(), r_type);
11908 // Report an unsupported relocation against a global symbol.
11910 template<int size, bool big_endian>
11912 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
11913 Sized_relobj_file<size, big_endian>* object,
11914 unsigned int r_type,
11917 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
11918 object->name().c_str(), r_type, gsym->demangled_name().c_str());
11921 // Return printable name for ABI.
11922 template<int size, bool big_endian>
11924 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
11926 switch (e_flags & elfcpp::EF_MIPS_ABI)
11929 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
11931 else if (size == 64)
11935 case elfcpp::E_MIPS_ABI_O32:
11937 case elfcpp::E_MIPS_ABI_O64:
11939 case elfcpp::E_MIPS_ABI_EABI32:
11941 case elfcpp::E_MIPS_ABI_EABI64:
11944 return "unknown abi";
11948 template<int size, bool big_endian>
11950 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
11952 switch (e_flags & elfcpp::EF_MIPS_MACH)
11954 case elfcpp::E_MIPS_MACH_3900:
11955 return "mips:3900";
11956 case elfcpp::E_MIPS_MACH_4010:
11957 return "mips:4010";
11958 case elfcpp::E_MIPS_MACH_4100:
11959 return "mips:4100";
11960 case elfcpp::E_MIPS_MACH_4111:
11961 return "mips:4111";
11962 case elfcpp::E_MIPS_MACH_4120:
11963 return "mips:4120";
11964 case elfcpp::E_MIPS_MACH_4650:
11965 return "mips:4650";
11966 case elfcpp::E_MIPS_MACH_5400:
11967 return "mips:5400";
11968 case elfcpp::E_MIPS_MACH_5500:
11969 return "mips:5500";
11970 case elfcpp::E_MIPS_MACH_5900:
11971 return "mips:5900";
11972 case elfcpp::E_MIPS_MACH_SB1:
11974 case elfcpp::E_MIPS_MACH_9000:
11975 return "mips:9000";
11976 case elfcpp::E_MIPS_MACH_LS2E:
11977 return "mips:loongson_2e";
11978 case elfcpp::E_MIPS_MACH_LS2F:
11979 return "mips:loongson_2f";
11980 case elfcpp::E_MIPS_MACH_LS3A:
11981 return "mips:loongson_3a";
11982 case elfcpp::E_MIPS_MACH_OCTEON:
11983 return "mips:octeon";
11984 case elfcpp::E_MIPS_MACH_OCTEON2:
11985 return "mips:octeon2";
11986 case elfcpp::E_MIPS_MACH_OCTEON3:
11987 return "mips:octeon3";
11988 case elfcpp::E_MIPS_MACH_XLR:
11991 switch (e_flags & elfcpp::EF_MIPS_ARCH)
11994 case elfcpp::E_MIPS_ARCH_1:
11995 return "mips:3000";
11997 case elfcpp::E_MIPS_ARCH_2:
11998 return "mips:6000";
12000 case elfcpp::E_MIPS_ARCH_3:
12001 return "mips:4000";
12003 case elfcpp::E_MIPS_ARCH_4:
12004 return "mips:8000";
12006 case elfcpp::E_MIPS_ARCH_5:
12007 return "mips:mips5";
12009 case elfcpp::E_MIPS_ARCH_32:
12010 return "mips:isa32";
12012 case elfcpp::E_MIPS_ARCH_64:
12013 return "mips:isa64";
12015 case elfcpp::E_MIPS_ARCH_32R2:
12016 return "mips:isa32r2";
12018 case elfcpp::E_MIPS_ARCH_64R2:
12019 return "mips:isa64r2";
12022 return "unknown CPU";
12025 template<int size, bool big_endian>
12026 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12029 big_endian, // is_big_endian
12030 elfcpp::EM_MIPS, // machine_code
12031 true, // has_make_symbol
12032 false, // has_resolve
12033 false, // has_code_fill
12034 true, // is_default_stack_executable
12035 false, // can_icf_inline_merge_sections
12037 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
12038 0x400000, // default_text_segment_address
12039 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
12040 4 * 1024, // common_pagesize (overridable by -z common-page-size)
12041 false, // isolate_execinstr
12042 0, // rosegment_gap
12043 elfcpp::SHN_UNDEF, // small_common_shndx
12044 elfcpp::SHN_UNDEF, // large_common_shndx
12045 0, // small_common_section_flags
12046 0, // large_common_section_flags
12047 NULL, // attributes_section
12048 NULL, // attributes_vendor
12049 "__start", // entry_symbol_name
12050 32, // hash_entry_size
12053 template<int size, bool big_endian>
12054 class Target_mips_nacl : public Target_mips<size, big_endian>
12058 : Target_mips<size, big_endian>(&mips_nacl_info)
12062 static const Target::Target_info mips_nacl_info;
12065 template<int size, bool big_endian>
12066 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12069 big_endian, // is_big_endian
12070 elfcpp::EM_MIPS, // machine_code
12071 true, // has_make_symbol
12072 false, // has_resolve
12073 false, // has_code_fill
12074 true, // is_default_stack_executable
12075 false, // can_icf_inline_merge_sections
12077 "/lib/ld.so.1", // dynamic_linker
12078 0x20000, // default_text_segment_address
12079 0x10000, // abi_pagesize (overridable by -z max-page-size)
12080 0x10000, // common_pagesize (overridable by -z common-page-size)
12081 true, // isolate_execinstr
12082 0x10000000, // rosegment_gap
12083 elfcpp::SHN_UNDEF, // small_common_shndx
12084 elfcpp::SHN_UNDEF, // large_common_shndx
12085 0, // small_common_section_flags
12086 0, // large_common_section_flags
12087 NULL, // attributes_section
12088 NULL, // attributes_vendor
12089 "_start", // entry_symbol_name
12090 32, // hash_entry_size
12093 // Target selector for Mips. Note this is never instantiated directly.
12094 // It's only used in Target_selector_mips_nacl, below.
12096 template<int size, bool big_endian>
12097 class Target_selector_mips : public Target_selector
12100 Target_selector_mips()
12101 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12103 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12104 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12106 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12107 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12110 Target* do_instantiate_target()
12111 { return new Target_mips<size, big_endian>(); }
12114 template<int size, bool big_endian>
12115 class Target_selector_mips_nacl
12116 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12117 Target_mips_nacl<size, big_endian> >
12120 Target_selector_mips_nacl()
12121 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12122 Target_mips_nacl<size, big_endian> >(
12123 // NaCl currently supports only MIPS32 little-endian.
12124 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12128 Target_selector_mips_nacl<32, true> target_selector_mips32;
12129 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12130 Target_selector_mips_nacl<64, true> target_selector_mips64;
12131 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12133 } // End anonymous namespace.