Add ChangeLog entries for previous commit.
[binutils-gdb.git] / gold / mips.cc
blob9d5052162cf8973a6925ba69dab931973eeca1de
1 // mips.cc -- mips target support for gold.
3 // Copyright (C) 2011-2020 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.
25 #include "gold.h"
27 #include <algorithm>
28 #include <set>
29 #include <sstream>
30 #include "demangle.h"
32 #include "elfcpp.h"
33 #include "parameters.h"
34 #include "reloc.h"
35 #include "mips.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "layout.h"
39 #include "output.h"
40 #include "copy-relocs.h"
41 #include "target.h"
42 #include "target-reloc.h"
43 #include "target-select.h"
44 #include "tls.h"
45 #include "errors.h"
46 #include "gc.h"
47 #include "attributes.h"
48 #include "nacl.h"
50 namespace
52 using namespace gold;
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>
61 class Target_mips;
63 template<int size, bool big_endian>
64 class Mips_output_section_reginfo;
66 template<int size, bool big_endian>
67 class Mips_output_section_options;
69 template<int size, bool big_endian>
70 class Mips_output_data_la25_stub;
72 template<int size, bool big_endian>
73 class Mips_output_data_mips_stubs;
75 template<int size>
76 class Mips_symbol;
78 template<int size, bool big_endian>
79 class Mips_got_info;
81 template<int size, bool big_endian>
82 class Mips_relobj;
84 class Mips16_stub_section_base;
86 template<int size, bool big_endian>
87 class Mips16_stub_section;
89 // The ABI says that every symbol used by dynamic relocations must have
90 // a global GOT entry. Among other things, this provides the dynamic
91 // linker with a free, directly-indexed cache. The GOT can therefore
92 // contain symbols that are not referenced by GOT relocations themselves
93 // (in other words, it may have symbols that are not referenced by things
94 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
96 // GOT relocations are less likely to overflow if we put the associated
97 // GOT entries towards the beginning. We therefore divide the global
98 // GOT entries into two areas: "normal" and "reloc-only". Entries in
99 // the first area can be used for both dynamic relocations and GP-relative
100 // accesses, while those in the "reloc-only" area are for dynamic
101 // relocations only.
103 // These GGA_* ("Global GOT Area") values are organised so that lower
104 // values are more general than higher values. Also, non-GGA_NONE
105 // values are ordered by the position of the area in the GOT.
107 enum Global_got_area
109 GGA_NORMAL = 0,
110 GGA_RELOC_ONLY = 1,
111 GGA_NONE = 2
114 // The types of GOT entries needed for this platform.
115 // These values are exposed to the ABI in an incremental link.
116 // Do not renumber existing values without changing the version
117 // number of the .gnu_incremental_inputs section.
118 enum Got_type
120 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
121 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
122 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
124 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
125 GOT_TYPE_STANDARD_MULTIGOT = 3,
126 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
127 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
130 // TLS type of GOT entry.
131 enum Got_tls_type
133 GOT_TLS_NONE = 0,
134 GOT_TLS_GD = 1,
135 GOT_TLS_LDM = 2,
136 GOT_TLS_IE = 4
139 // Values found in the r_ssym field of a relocation entry.
140 enum Special_relocation_symbol
142 RSS_UNDEF = 0, // None - value is zero.
143 RSS_GP = 1, // Value of GP.
144 RSS_GP0 = 2, // Value of GP in object being relocated.
145 RSS_LOC = 3 // Address of location being relocated.
148 // Whether the section is readonly.
149 static inline bool
150 is_readonly_section(Output_section* output_section)
152 elfcpp::Elf_Xword section_flags = output_section->flags();
153 elfcpp::Elf_Word section_type = output_section->type();
155 if (section_type == elfcpp::SHT_NOBITS)
156 return false;
158 if (section_flags & elfcpp::SHF_WRITE)
159 return false;
161 return true;
164 // Return TRUE if a relocation of type R_TYPE from OBJECT might
165 // require an la25 stub. See also local_pic_function, which determines
166 // whether the destination function ever requires a stub.
167 template<int size, bool big_endian>
168 static inline bool
169 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
170 unsigned int r_type, bool target_is_16_bit_code)
172 // We specifically ignore branches and jumps from EF_PIC objects,
173 // where the onus is on the compiler or programmer to perform any
174 // necessary initialization of $25. Sometimes such initialization
175 // is unnecessary; for example, -mno-shared functions do not use
176 // the incoming value of $25, and may therefore be called directly.
177 if (object->is_pic())
178 return false;
180 switch (r_type)
182 case elfcpp::R_MIPS_26:
183 case elfcpp::R_MIPS_PC16:
184 case elfcpp::R_MIPS_PC21_S2:
185 case elfcpp::R_MIPS_PC26_S2:
186 case elfcpp::R_MICROMIPS_26_S1:
187 case elfcpp::R_MICROMIPS_PC7_S1:
188 case elfcpp::R_MICROMIPS_PC10_S1:
189 case elfcpp::R_MICROMIPS_PC16_S1:
190 case elfcpp::R_MICROMIPS_PC23_S2:
191 return true;
193 case elfcpp::R_MIPS16_26:
194 return !target_is_16_bit_code;
196 default:
197 return false;
201 // Return true if SYM is a locally-defined PIC function, in the sense
202 // that it or its fn_stub might need $25 to be valid on entry.
203 // Note that MIPS16 functions set up $gp using PC-relative instructions,
204 // so they themselves never need $25 to be valid. Only non-MIPS16
205 // entry points are of interest here.
206 template<int size, bool big_endian>
207 static inline bool
208 local_pic_function(Mips_symbol<size>* sym)
210 bool def_regular = (sym->source() == Symbol::FROM_OBJECT
211 && !sym->object()->is_dynamic()
212 && !sym->is_undefined());
214 if (sym->is_defined() && def_regular)
216 Mips_relobj<size, big_endian>* object =
217 static_cast<Mips_relobj<size, big_endian>*>(sym->object());
219 if ((object->is_pic() || sym->is_pic())
220 && (!sym->is_mips16()
221 || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
222 return true;
224 return false;
227 static inline bool
228 hi16_reloc(int r_type)
230 return (r_type == elfcpp::R_MIPS_HI16
231 || r_type == elfcpp::R_MIPS16_HI16
232 || r_type == elfcpp::R_MICROMIPS_HI16
233 || r_type == elfcpp::R_MIPS_PCHI16);
236 static inline bool
237 lo16_reloc(int r_type)
239 return (r_type == elfcpp::R_MIPS_LO16
240 || r_type == elfcpp::R_MIPS16_LO16
241 || r_type == elfcpp::R_MICROMIPS_LO16
242 || r_type == elfcpp::R_MIPS_PCLO16);
245 static inline bool
246 got16_reloc(unsigned int r_type)
248 return (r_type == elfcpp::R_MIPS_GOT16
249 || r_type == elfcpp::R_MIPS16_GOT16
250 || r_type == elfcpp::R_MICROMIPS_GOT16);
253 static inline bool
254 call_lo16_reloc(unsigned int r_type)
256 return (r_type == elfcpp::R_MIPS_CALL_LO16
257 || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
260 static inline bool
261 got_lo16_reloc(unsigned int r_type)
263 return (r_type == elfcpp::R_MIPS_GOT_LO16
264 || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
267 static inline bool
268 eh_reloc(unsigned int r_type)
270 return (r_type == elfcpp::R_MIPS_EH);
273 static inline bool
274 got_disp_reloc(unsigned int r_type)
276 return (r_type == elfcpp::R_MIPS_GOT_DISP
277 || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
280 static inline bool
281 got_page_reloc(unsigned int r_type)
283 return (r_type == elfcpp::R_MIPS_GOT_PAGE
284 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
287 static inline bool
288 tls_gd_reloc(unsigned int r_type)
290 return (r_type == elfcpp::R_MIPS_TLS_GD
291 || r_type == elfcpp::R_MIPS16_TLS_GD
292 || r_type == elfcpp::R_MICROMIPS_TLS_GD);
295 static inline bool
296 tls_gottprel_reloc(unsigned int r_type)
298 return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
299 || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
300 || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
303 static inline bool
304 tls_ldm_reloc(unsigned int r_type)
306 return (r_type == elfcpp::R_MIPS_TLS_LDM
307 || r_type == elfcpp::R_MIPS16_TLS_LDM
308 || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
311 static inline bool
312 mips16_call_reloc(unsigned int r_type)
314 return (r_type == elfcpp::R_MIPS16_26
315 || r_type == elfcpp::R_MIPS16_CALL16);
318 static inline bool
319 jal_reloc(unsigned int r_type)
321 return (r_type == elfcpp::R_MIPS_26
322 || r_type == elfcpp::R_MIPS16_26
323 || r_type == elfcpp::R_MICROMIPS_26_S1);
326 static inline bool
327 micromips_branch_reloc(unsigned int r_type)
329 return (r_type == elfcpp::R_MICROMIPS_26_S1
330 || r_type == elfcpp::R_MICROMIPS_PC16_S1
331 || r_type == elfcpp::R_MICROMIPS_PC10_S1
332 || r_type == elfcpp::R_MICROMIPS_PC7_S1);
335 // Check if R_TYPE is a MIPS16 reloc.
336 static inline bool
337 mips16_reloc(unsigned int r_type)
339 switch (r_type)
341 case elfcpp::R_MIPS16_26:
342 case elfcpp::R_MIPS16_GPREL:
343 case elfcpp::R_MIPS16_GOT16:
344 case elfcpp::R_MIPS16_CALL16:
345 case elfcpp::R_MIPS16_HI16:
346 case elfcpp::R_MIPS16_LO16:
347 case elfcpp::R_MIPS16_TLS_GD:
348 case elfcpp::R_MIPS16_TLS_LDM:
349 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
350 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
351 case elfcpp::R_MIPS16_TLS_GOTTPREL:
352 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
353 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
354 return true;
356 default:
357 return false;
361 // Check if R_TYPE is a microMIPS reloc.
362 static inline bool
363 micromips_reloc(unsigned int r_type)
365 switch (r_type)
367 case elfcpp::R_MICROMIPS_26_S1:
368 case elfcpp::R_MICROMIPS_HI16:
369 case elfcpp::R_MICROMIPS_LO16:
370 case elfcpp::R_MICROMIPS_GPREL16:
371 case elfcpp::R_MICROMIPS_LITERAL:
372 case elfcpp::R_MICROMIPS_GOT16:
373 case elfcpp::R_MICROMIPS_PC7_S1:
374 case elfcpp::R_MICROMIPS_PC10_S1:
375 case elfcpp::R_MICROMIPS_PC16_S1:
376 case elfcpp::R_MICROMIPS_CALL16:
377 case elfcpp::R_MICROMIPS_GOT_DISP:
378 case elfcpp::R_MICROMIPS_GOT_PAGE:
379 case elfcpp::R_MICROMIPS_GOT_OFST:
380 case elfcpp::R_MICROMIPS_GOT_HI16:
381 case elfcpp::R_MICROMIPS_GOT_LO16:
382 case elfcpp::R_MICROMIPS_SUB:
383 case elfcpp::R_MICROMIPS_HIGHER:
384 case elfcpp::R_MICROMIPS_HIGHEST:
385 case elfcpp::R_MICROMIPS_CALL_HI16:
386 case elfcpp::R_MICROMIPS_CALL_LO16:
387 case elfcpp::R_MICROMIPS_SCN_DISP:
388 case elfcpp::R_MICROMIPS_JALR:
389 case elfcpp::R_MICROMIPS_HI0_LO16:
390 case elfcpp::R_MICROMIPS_TLS_GD:
391 case elfcpp::R_MICROMIPS_TLS_LDM:
392 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
393 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
394 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
395 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
396 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
397 case elfcpp::R_MICROMIPS_GPREL7_S2:
398 case elfcpp::R_MICROMIPS_PC23_S2:
399 return true;
401 default:
402 return false;
406 static inline bool
407 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
409 switch (high_reloc)
411 case elfcpp::R_MIPS_HI16:
412 case elfcpp::R_MIPS_GOT16:
413 return lo16_reloc == elfcpp::R_MIPS_LO16;
414 case elfcpp::R_MIPS_PCHI16:
415 return lo16_reloc == elfcpp::R_MIPS_PCLO16;
416 case elfcpp::R_MIPS16_HI16:
417 case elfcpp::R_MIPS16_GOT16:
418 return lo16_reloc == elfcpp::R_MIPS16_LO16;
419 case elfcpp::R_MICROMIPS_HI16:
420 case elfcpp::R_MICROMIPS_GOT16:
421 return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
422 default:
423 return false;
427 // This class is used to hold information about one GOT entry.
428 // There are three types of entry:
430 // (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
431 // (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
432 // (2) a SYMBOL address, where SYMBOL is not local to an input object
433 // (sym != NULL, symndx == -1)
434 // (3) a TLS LDM slot (there's only one of these per GOT.)
435 // (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
437 template<int size, bool big_endian>
438 class Mips_got_entry
440 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
442 public:
443 Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
444 Mips_address addend, unsigned char tls_type,
445 unsigned int shndx, bool is_section_symbol)
446 : addend_(addend), symndx_(symndx), tls_type_(tls_type),
447 is_section_symbol_(is_section_symbol), shndx_(shndx)
448 { this->d.object = object; }
450 Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
451 : addend_(0), symndx_(-1U), tls_type_(tls_type),
452 is_section_symbol_(false), shndx_(-1U)
453 { this->d.sym = sym; }
455 // Return whether this entry is for a local symbol.
456 bool
457 is_for_local_symbol() const
458 { return this->symndx_ != -1U; }
460 // Return whether this entry is for a global symbol.
461 bool
462 is_for_global_symbol() const
463 { return this->symndx_ == -1U; }
465 // Return the hash of this entry.
466 size_t
467 hash() const
469 if (this->tls_type_ == GOT_TLS_LDM)
470 return this->symndx_ + (1 << 18);
472 size_t name_hash_value = gold::string_hash<char>(
473 (this->symndx_ != -1U)
474 ? this->d.object->name().c_str()
475 : this->d.sym->name());
476 size_t addend = this->addend_;
477 return name_hash_value ^ this->symndx_ ^ (addend << 16);
480 // Return whether this entry is equal to OTHER.
481 bool
482 equals(Mips_got_entry<size, big_endian>* other) const
484 if (this->symndx_ != other->symndx_
485 || this->tls_type_ != other->tls_type_)
486 return false;
488 if (this->tls_type_ == GOT_TLS_LDM)
489 return true;
491 return (((this->symndx_ != -1U)
492 ? (this->d.object == other->d.object)
493 : (this->d.sym == other->d.sym))
494 && (this->addend_ == other->addend_));
497 // Return input object that needs this GOT entry.
498 Mips_relobj<size, big_endian>*
499 object() const
501 gold_assert(this->symndx_ != -1U);
502 return this->d.object;
505 // Return local symbol index for local GOT entries.
506 unsigned int
507 symndx() const
509 gold_assert(this->symndx_ != -1U);
510 return this->symndx_;
513 // Return the relocation addend for local GOT entries.
514 Mips_address
515 addend() const
516 { return this->addend_; }
518 // Return global symbol for global GOT entries.
519 Mips_symbol<size>*
520 sym() const
522 gold_assert(this->symndx_ == -1U);
523 return this->d.sym;
526 // Return whether this is a TLS GOT entry.
527 bool
528 is_tls_entry() const
529 { return this->tls_type_ != GOT_TLS_NONE; }
531 // Return TLS type of this GOT entry.
532 unsigned char
533 tls_type() const
534 { return this->tls_type_; }
536 // Return section index of the local symbol for local GOT entries.
537 unsigned int
538 shndx() const
539 { return this->shndx_; }
541 // Return whether this is a STT_SECTION symbol.
542 bool
543 is_section_symbol() const
544 { return this->is_section_symbol_; }
546 private:
547 // The addend.
548 Mips_address addend_;
550 // The index of the symbol if we have a local symbol; -1 otherwise.
551 unsigned int symndx_;
553 union
555 // The input object for local symbols that needs the GOT entry.
556 Mips_relobj<size, big_endian>* object;
557 // If symndx == -1, the global symbol corresponding to this GOT entry. The
558 // symbol's entry is in the local area if mips_sym->global_got_area is
559 // GGA_NONE, otherwise it is in the global area.
560 Mips_symbol<size>* sym;
561 } d;
563 // The TLS type of this GOT entry. An LDM GOT entry will be a local
564 // symbol entry with r_symndx == 0.
565 unsigned char tls_type_;
567 // Whether this is a STT_SECTION symbol.
568 bool is_section_symbol_;
570 // For local GOT entries, section index of the local symbol.
571 unsigned int shndx_;
574 // Hash for Mips_got_entry.
576 template<int size, bool big_endian>
577 class Mips_got_entry_hash
579 public:
580 size_t
581 operator()(Mips_got_entry<size, big_endian>* entry) const
582 { return entry->hash(); }
585 // Equality for Mips_got_entry.
587 template<int size, bool big_endian>
588 class Mips_got_entry_eq
590 public:
591 bool
592 operator()(Mips_got_entry<size, big_endian>* e1,
593 Mips_got_entry<size, big_endian>* e2) const
594 { return e1->equals(e2); }
597 // Hash for Mips_symbol.
599 template<int size>
600 class Mips_symbol_hash
602 public:
603 size_t
604 operator()(Mips_symbol<size>* sym) const
605 { return sym->hash(); }
608 // Got_page_range. This class describes a range of addends: [MIN_ADDEND,
609 // MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
610 // increasing MIN_ADDEND.
612 struct Got_page_range
614 Got_page_range()
615 : next(NULL), min_addend(0), max_addend(0)
618 Got_page_range* next;
619 int min_addend;
620 int max_addend;
622 // Return the maximum number of GOT page entries required.
624 get_max_pages()
625 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
628 // Got_page_entry. This class describes the range of addends that are applied
629 // to page relocations against a given symbol.
631 struct Got_page_entry
633 Got_page_entry()
634 : object(NULL), symndx(-1U), ranges(NULL)
637 Got_page_entry(Object* object_, unsigned int symndx_)
638 : object(object_), symndx(symndx_), ranges(NULL)
641 // The input object that needs the GOT page entry.
642 Object* object;
643 // The index of the symbol, as stored in the relocation r_info.
644 unsigned int symndx;
645 // The ranges for this page entry.
646 Got_page_range* ranges;
649 // Hash for Got_page_entry.
651 struct Got_page_entry_hash
653 size_t
654 operator()(Got_page_entry* entry) const
655 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
658 // Equality for Got_page_entry.
660 struct Got_page_entry_eq
662 bool
663 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
665 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
669 // This class is used to hold .got information when linking.
671 template<int size, bool big_endian>
672 class Mips_got_info
674 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
675 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
676 Reloc_section;
677 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
679 // Unordered set of GOT entries.
680 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
681 Mips_got_entry_hash<size, big_endian>,
682 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
684 // Unordered set of GOT page entries.
685 typedef Unordered_set<Got_page_entry*,
686 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
688 // Unordered set of global GOT entries.
689 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
690 Global_got_entry_set;
692 public:
693 Mips_got_info()
694 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
695 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
696 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
697 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
698 offset_(0)
701 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
702 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
703 void
704 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
705 unsigned int symndx, Mips_address addend,
706 unsigned int r_type, unsigned int shndx,
707 bool is_section_symbol);
709 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
710 // in OBJECT. FOR_CALL is true if the caller is only interested in
711 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
712 // relocation.
713 void
714 record_global_got_symbol(Mips_symbol<size>* mips_sym,
715 Mips_relobj<size, big_endian>* object,
716 unsigned int r_type, bool dyn_reloc, bool for_call);
718 // Add ENTRY to master GOT and to OBJECT's GOT.
719 void
720 record_got_entry(Mips_got_entry<size, big_endian>* entry,
721 Mips_relobj<size, big_endian>* object);
723 // Record that OBJECT has a page relocation against symbol SYMNDX and
724 // that ADDEND is the addend for that relocation.
725 void
726 record_got_page_entry(Mips_relobj<size, big_endian>* object,
727 unsigned int symndx, int addend);
729 // Create all entries that should be in the local part of the GOT.
730 void
731 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
733 // Create GOT page entries.
734 void
735 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
737 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
738 void
739 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
740 unsigned int non_reloc_only_global_gotno);
742 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
743 void
744 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
746 // Create TLS GOT entries.
747 void
748 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
750 // Decide whether the symbol needs an entry in the global part of the primary
751 // GOT, setting global_got_area accordingly. Count the number of global
752 // symbols that are in the primary GOT only because they have dynamic
753 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
754 void
755 count_got_symbols(Symbol_table* symtab);
757 // Return the offset of GOT page entry for VALUE.
758 unsigned int
759 get_got_page_offset(Mips_address value,
760 Mips_output_data_got<size, big_endian>* got);
762 // Count the number of GOT entries required.
763 void
764 count_got_entries();
766 // Count the number of GOT entries required by ENTRY. Accumulate the result.
767 void
768 count_got_entry(Mips_got_entry<size, big_endian>* entry);
770 // Add FROM's GOT entries.
771 void
772 add_got_entries(Mips_got_info<size, big_endian>* from);
774 // Add FROM's GOT page entries.
775 void
776 add_got_page_count(Mips_got_info<size, big_endian>* from);
778 // Return GOT size.
779 unsigned int
780 got_size() const
781 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
782 + this->tls_gotno_) * size/8);
785 // Return the number of local GOT entries.
786 unsigned int
787 local_gotno() const
788 { return this->local_gotno_; }
790 // Return the maximum number of page GOT entries needed.
791 unsigned int
792 page_gotno() const
793 { return this->page_gotno_; }
795 // Return the number of global GOT entries.
796 unsigned int
797 global_gotno() const
798 { return this->global_gotno_; }
800 // Set the number of global GOT entries.
801 void
802 set_global_gotno(unsigned int global_gotno)
803 { this->global_gotno_ = global_gotno; }
805 // Return the number of GGA_RELOC_ONLY global GOT entries.
806 unsigned int
807 reloc_only_gotno() const
808 { return this->reloc_only_gotno_; }
810 // Return the number of TLS GOT entries.
811 unsigned int
812 tls_gotno() const
813 { return this->tls_gotno_; }
815 // Return the GOT type for this GOT. Used for multi-GOT links only.
816 unsigned int
817 multigot_got_type(unsigned int got_type) const
819 switch (got_type)
821 case GOT_TYPE_STANDARD:
822 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
823 case GOT_TYPE_TLS_OFFSET:
824 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
825 case GOT_TYPE_TLS_PAIR:
826 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
827 default:
828 gold_unreachable();
832 // Remove lazy-binding stubs for global symbols in this GOT.
833 void
834 remove_lazy_stubs(Target_mips<size, big_endian>* target);
836 // Return offset of this GOT from the start of .got section.
837 unsigned int
838 offset() const
839 { return this->offset_; }
841 // Set offset of this GOT from the start of .got section.
842 void
843 set_offset(unsigned int offset)
844 { this->offset_ = offset; }
846 // Set index of this GOT in multi-GOT links.
847 void
848 set_index(unsigned int index)
849 { this->index_ = index; }
851 // Return next GOT in multi-GOT links.
852 Mips_got_info<size, big_endian>*
853 next() const
854 { return this->next_; }
856 // Set next GOT in multi-GOT links.
857 void
858 set_next(Mips_got_info<size, big_endian>* next)
859 { this->next_ = next; }
861 // Return the offset of TLS LDM entry for this GOT.
862 unsigned int
863 tls_ldm_offset() const
864 { return this->tls_ldm_offset_; }
866 // Set the offset of TLS LDM entry for this GOT.
867 void
868 set_tls_ldm_offset(unsigned int tls_ldm_offset)
869 { this->tls_ldm_offset_ = tls_ldm_offset; }
871 Global_got_entry_set&
872 global_got_symbols()
873 { return this->global_got_symbols_; }
875 // Return the GOT_TLS_* type required by relocation type R_TYPE.
876 static int
877 mips_elf_reloc_tls_type(unsigned int r_type)
879 if (tls_gd_reloc(r_type))
880 return GOT_TLS_GD;
882 if (tls_ldm_reloc(r_type))
883 return GOT_TLS_LDM;
885 if (tls_gottprel_reloc(r_type))
886 return GOT_TLS_IE;
888 return GOT_TLS_NONE;
891 // Return the number of GOT slots needed for GOT TLS type TYPE.
892 static int
893 mips_tls_got_entries(unsigned int type)
895 switch (type)
897 case GOT_TLS_GD:
898 case GOT_TLS_LDM:
899 return 2;
901 case GOT_TLS_IE:
902 return 1;
904 case GOT_TLS_NONE:
905 return 0;
907 default:
908 gold_unreachable();
912 private:
913 // The number of local GOT entries.
914 unsigned int local_gotno_;
915 // The maximum number of page GOT entries needed.
916 unsigned int page_gotno_;
917 // The number of global GOT entries.
918 unsigned int global_gotno_;
919 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
920 unsigned int reloc_only_gotno_;
921 // The number of TLS GOT entries.
922 unsigned int tls_gotno_;
923 // The offset of TLS LDM entry for this GOT.
924 unsigned int tls_ldm_offset_;
925 // All symbols that have global GOT entry.
926 Global_got_entry_set global_got_symbols_;
927 // A hash table holding GOT entries.
928 Got_entry_set got_entries_;
929 // A hash table of GOT page entries (only used in master GOT).
930 Got_page_entry_set got_page_entries_;
931 // The offset of first GOT page entry for this GOT.
932 unsigned int got_page_offset_start_;
933 // The offset of next available GOT page entry for this GOT.
934 unsigned int got_page_offset_next_;
935 // A hash table that maps GOT page entry value to the GOT offset where
936 // the entry is located.
937 Got_page_offsets got_page_offsets_;
938 // In multi-GOT links, a pointer to the next GOT.
939 Mips_got_info<size, big_endian>* next_;
940 // Index of this GOT in multi-GOT links.
941 unsigned int index_;
942 // The offset of this GOT in multi-GOT links.
943 unsigned int offset_;
946 // This is a helper class used during relocation scan. It records GOT16 addend.
948 template<int size, bool big_endian>
949 struct got16_addend
951 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
953 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
954 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
955 Mips_address _addend)
956 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
957 addend(_addend)
960 const Sized_relobj_file<size, big_endian>* object;
961 unsigned int shndx;
962 unsigned int r_type;
963 unsigned int r_sym;
964 Mips_address addend;
967 // .MIPS.abiflags section content
969 template<bool big_endian>
970 struct Mips_abiflags
972 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
973 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
974 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
976 Mips_abiflags()
977 : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
978 cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
981 // Version of flags structure.
982 Valtype16 version;
983 // The level of the ISA: 1-5, 32, 64.
984 Valtype8 isa_level;
985 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
986 Valtype8 isa_rev;
987 // The size of general purpose registers.
988 Valtype8 gpr_size;
989 // The size of co-processor 1 registers.
990 Valtype8 cpr1_size;
991 // The size of co-processor 2 registers.
992 Valtype8 cpr2_size;
993 // The floating-point ABI.
994 Valtype8 fp_abi;
995 // Processor-specific extension.
996 Valtype32 isa_ext;
997 // Mask of ASEs used.
998 Valtype32 ases;
999 // Mask of general flags.
1000 Valtype32 flags1;
1001 Valtype32 flags2;
1004 // Mips_symbol class. Holds additional symbol information needed for Mips.
1006 template<int size>
1007 class Mips_symbol : public Sized_symbol<size>
1009 public:
1010 Mips_symbol()
1011 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1012 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1013 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1014 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1015 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1016 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1017 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1020 // Return whether this is a MIPS16 symbol.
1021 bool
1022 is_mips16() const
1024 // (st_other & STO_MIPS16) == STO_MIPS16
1025 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1026 == elfcpp::STO_MIPS16 >> 2);
1029 // Return whether this is a microMIPS symbol.
1030 bool
1031 is_micromips() const
1033 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1034 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1035 == elfcpp::STO_MICROMIPS >> 2);
1038 // Return whether the symbol needs MIPS16 fn_stub.
1039 bool
1040 need_fn_stub() const
1041 { return this->need_fn_stub_; }
1043 // Set that the symbol needs MIPS16 fn_stub.
1044 void
1045 set_need_fn_stub()
1046 { this->need_fn_stub_ = true; }
1048 // Return whether this symbol is referenced by branch relocations from
1049 // any non-PIC input file.
1050 bool
1051 has_nonpic_branches() const
1052 { return this->has_nonpic_branches_; }
1054 // Set that this symbol is referenced by branch relocations from
1055 // any non-PIC input file.
1056 void
1057 set_has_nonpic_branches()
1058 { this->has_nonpic_branches_ = true; }
1060 // Return the offset of the la25 stub for this symbol from the start of the
1061 // la25 stub section.
1062 unsigned int
1063 la25_stub_offset() const
1064 { return this->la25_stub_offset_; }
1066 // Set the offset of the la25 stub for this symbol from the start of the
1067 // la25 stub section.
1068 void
1069 set_la25_stub_offset(unsigned int offset)
1070 { this->la25_stub_offset_ = offset; }
1072 // Return whether the symbol has la25 stub. This is true if this symbol is
1073 // for a PIC function, and there are non-PIC branches and jumps to it.
1074 bool
1075 has_la25_stub() const
1076 { return this->la25_stub_offset_ != -1U; }
1078 // Return whether there is a relocation against this symbol that must be
1079 // resolved by the static linker (that is, the relocation cannot possibly
1080 // be made dynamic).
1081 bool
1082 has_static_relocs() const
1083 { return this->has_static_relocs_; }
1085 // Set that there is a relocation against this symbol that must be resolved
1086 // by the static linker (that is, the relocation cannot possibly be made
1087 // dynamic).
1088 void
1089 set_has_static_relocs()
1090 { this->has_static_relocs_ = true; }
1092 // Return whether we must not create a lazy-binding stub for this symbol.
1093 bool
1094 no_lazy_stub() const
1095 { return this->no_lazy_stub_; }
1097 // Set that we must not create a lazy-binding stub for this symbol.
1098 void
1099 set_no_lazy_stub()
1100 { this->no_lazy_stub_ = true; }
1102 // Return the offset of the lazy-binding stub for this symbol from the start
1103 // of .MIPS.stubs section.
1104 unsigned int
1105 lazy_stub_offset() const
1106 { return this->lazy_stub_offset_; }
1108 // Set the offset of the lazy-binding stub for this symbol from the start
1109 // of .MIPS.stubs section.
1110 void
1111 set_lazy_stub_offset(unsigned int offset)
1112 { this->lazy_stub_offset_ = offset; }
1114 // Return whether there are any relocations for this symbol where
1115 // pointer equality matters.
1116 bool
1117 pointer_equality_needed() const
1118 { return this->pointer_equality_needed_; }
1120 // Set that there are relocations for this symbol where pointer equality
1121 // matters.
1122 void
1123 set_pointer_equality_needed()
1124 { this->pointer_equality_needed_ = true; }
1126 // Return global GOT area where this symbol in located.
1127 Global_got_area
1128 global_got_area() const
1129 { return this->global_got_area_; }
1131 // Set global GOT area where this symbol in located.
1132 void
1133 set_global_got_area(Global_got_area global_got_area)
1134 { this->global_got_area_ = global_got_area; }
1136 // Return the global GOT offset for this symbol. For multi-GOT links, this
1137 // returns the offset from the start of .got section to the first GOT entry
1138 // for the symbol. Note that in multi-GOT links the symbol can have entry
1139 // in more than one GOT.
1140 unsigned int
1141 global_gotoffset() const
1142 { return this->global_gotoffset_; }
1144 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1145 // the symbol can have entry in more than one GOT. This method will set
1146 // the offset only if it is less than current offset.
1147 void
1148 set_global_gotoffset(unsigned int offset)
1150 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1151 this->global_gotoffset_ = offset;
1154 // Return whether all GOT relocations for this symbol are for calls.
1155 bool
1156 got_only_for_calls() const
1157 { return this->got_only_for_calls_; }
1159 // Set that there is a GOT relocation for this symbol that is not for call.
1160 void
1161 set_got_not_only_for_calls()
1162 { this->got_only_for_calls_ = false; }
1164 // Return whether this is a PIC symbol.
1165 bool
1166 is_pic() const
1168 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1169 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1170 == (elfcpp::STO_MIPS_PIC >> 2));
1173 // Set the flag in st_other field that marks this symbol as PIC.
1174 void
1175 set_pic()
1177 if (this->is_mips16())
1178 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1179 this->set_nonvis((this->nonvis()
1180 & ~((elfcpp::STO_MIPS16 >> 2)
1181 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1182 | (elfcpp::STO_MIPS_PIC >> 2));
1183 else
1184 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1185 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1186 | (elfcpp::STO_MIPS_PIC >> 2));
1189 // Set the flag in st_other field that marks this symbol as PLT.
1190 void
1191 set_mips_plt()
1193 if (this->is_mips16())
1194 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1195 this->set_nonvis((this->nonvis()
1196 & ((elfcpp::STO_MIPS16 >> 2)
1197 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1198 | (elfcpp::STO_MIPS_PLT >> 2));
1200 else
1201 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1202 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1203 | (elfcpp::STO_MIPS_PLT >> 2));
1206 // Downcast a base pointer to a Mips_symbol pointer.
1207 static Mips_symbol<size>*
1208 as_mips_sym(Symbol* sym)
1209 { return static_cast<Mips_symbol<size>*>(sym); }
1211 // Downcast a base pointer to a Mips_symbol pointer.
1212 static const Mips_symbol<size>*
1213 as_mips_sym(const Symbol* sym)
1214 { return static_cast<const Mips_symbol<size>*>(sym); }
1216 // Return whether the symbol has lazy-binding stub.
1217 bool
1218 has_lazy_stub() const
1219 { return this->has_lazy_stub_; }
1221 // Set whether the symbol has lazy-binding stub.
1222 void
1223 set_has_lazy_stub(bool has_lazy_stub)
1224 { this->has_lazy_stub_ = has_lazy_stub; }
1226 // Return whether the symbol needs a standard PLT entry.
1227 bool
1228 needs_mips_plt() const
1229 { return this->needs_mips_plt_; }
1231 // Set whether the symbol needs a standard PLT entry.
1232 void
1233 set_needs_mips_plt(bool needs_mips_plt)
1234 { this->needs_mips_plt_ = needs_mips_plt; }
1236 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1237 // entry.
1238 bool
1239 needs_comp_plt() const
1240 { return this->needs_comp_plt_; }
1242 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1243 void
1244 set_needs_comp_plt(bool needs_comp_plt)
1245 { this->needs_comp_plt_ = needs_comp_plt; }
1247 // Return standard PLT entry offset, or -1 if none.
1248 unsigned int
1249 mips_plt_offset() const
1250 { return this->mips_plt_offset_; }
1252 // Set standard PLT entry offset.
1253 void
1254 set_mips_plt_offset(unsigned int mips_plt_offset)
1255 { this->mips_plt_offset_ = mips_plt_offset; }
1257 // Return whether the symbol has standard PLT entry.
1258 bool
1259 has_mips_plt_offset() const
1260 { return this->mips_plt_offset_ != -1U; }
1262 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1263 unsigned int
1264 comp_plt_offset() const
1265 { return this->comp_plt_offset_; }
1267 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1268 void
1269 set_comp_plt_offset(unsigned int comp_plt_offset)
1270 { this->comp_plt_offset_ = comp_plt_offset; }
1272 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1273 bool
1274 has_comp_plt_offset() const
1275 { return this->comp_plt_offset_ != -1U; }
1277 // Return MIPS16 fn stub for a symbol.
1278 template<bool big_endian>
1279 Mips16_stub_section<size, big_endian>*
1280 get_mips16_fn_stub() const
1282 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1285 // Set MIPS16 fn stub for a symbol.
1286 void
1287 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1288 { this->mips16_fn_stub_ = stub; }
1290 // Return whether symbol has MIPS16 fn stub.
1291 bool
1292 has_mips16_fn_stub() const
1293 { return this->mips16_fn_stub_ != NULL; }
1295 // Return MIPS16 call stub for a symbol.
1296 template<bool big_endian>
1297 Mips16_stub_section<size, big_endian>*
1298 get_mips16_call_stub() const
1300 return static_cast<Mips16_stub_section<size, big_endian>*>(
1301 mips16_call_stub_);
1304 // Set MIPS16 call stub for a symbol.
1305 void
1306 set_mips16_call_stub(Mips16_stub_section_base* stub)
1307 { this->mips16_call_stub_ = stub; }
1309 // Return whether symbol has MIPS16 call stub.
1310 bool
1311 has_mips16_call_stub() const
1312 { return this->mips16_call_stub_ != NULL; }
1314 // Return MIPS16 call_fp stub for a symbol.
1315 template<bool big_endian>
1316 Mips16_stub_section<size, big_endian>*
1317 get_mips16_call_fp_stub() const
1319 return static_cast<Mips16_stub_section<size, big_endian>*>(
1320 mips16_call_fp_stub_);
1323 // Set MIPS16 call_fp stub for a symbol.
1324 void
1325 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1326 { this->mips16_call_fp_stub_ = stub; }
1328 // Return whether symbol has MIPS16 call_fp stub.
1329 bool
1330 has_mips16_call_fp_stub() const
1331 { return this->mips16_call_fp_stub_ != NULL; }
1333 bool
1334 get_applied_secondary_got_fixup() const
1335 { return applied_secondary_got_fixup_; }
1337 void
1338 set_applied_secondary_got_fixup()
1339 { this->applied_secondary_got_fixup_ = true; }
1341 // Return the hash of this symbol.
1342 size_t
1343 hash() const
1345 return gold::string_hash<char>(this->name());
1348 private:
1349 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1350 // appears in any relocs other than a 16 bit call.
1351 bool need_fn_stub_;
1353 // True if this symbol is referenced by branch relocations from
1354 // any non-PIC input file. This is used to determine whether an
1355 // la25 stub is required.
1356 bool has_nonpic_branches_;
1358 // The offset of the la25 stub for this symbol from the start of the
1359 // la25 stub section.
1360 unsigned int la25_stub_offset_;
1362 // True if there is a relocation against this symbol that must be
1363 // resolved by the static linker (that is, the relocation cannot
1364 // possibly be made dynamic).
1365 bool has_static_relocs_;
1367 // Whether we must not create a lazy-binding stub for this symbol.
1368 // This is true if the symbol has relocations related to taking the
1369 // function's address.
1370 bool no_lazy_stub_;
1372 // The offset of the lazy-binding stub for this symbol from the start of
1373 // .MIPS.stubs section.
1374 unsigned int lazy_stub_offset_;
1376 // True if there are any relocations for this symbol where pointer equality
1377 // matters.
1378 bool pointer_equality_needed_;
1380 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1381 // in the global part of the GOT.
1382 Global_got_area global_got_area_;
1384 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1385 // from the start of .got section to the first GOT entry for the symbol.
1386 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1387 unsigned int global_gotoffset_;
1389 // Whether all GOT relocations for this symbol are for calls.
1390 bool got_only_for_calls_;
1391 // Whether the symbol has lazy-binding stub.
1392 bool has_lazy_stub_;
1393 // Whether the symbol needs a standard PLT entry.
1394 bool needs_mips_plt_;
1395 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1396 bool needs_comp_plt_;
1397 // Standard PLT entry offset, or -1 if none.
1398 unsigned int mips_plt_offset_;
1399 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1400 unsigned int comp_plt_offset_;
1401 // MIPS16 fn stub for a symbol.
1402 Mips16_stub_section_base* mips16_fn_stub_;
1403 // MIPS16 call stub for a symbol.
1404 Mips16_stub_section_base* mips16_call_stub_;
1405 // MIPS16 call_fp stub for a symbol.
1406 Mips16_stub_section_base* mips16_call_fp_stub_;
1408 bool applied_secondary_got_fixup_;
1411 // Mips16_stub_section class.
1413 // The mips16 compiler uses a couple of special sections to handle
1414 // floating point arguments.
1416 // Section names that look like .mips16.fn.FNNAME contain stubs that
1417 // copy floating point arguments from the fp regs to the gp regs and
1418 // then jump to FNNAME. If any 32 bit function calls FNNAME, the
1419 // call should be redirected to the stub instead. If no 32 bit
1420 // function calls FNNAME, the stub should be discarded. We need to
1421 // consider any reference to the function, not just a call, because
1422 // if the address of the function is taken we will need the stub,
1423 // since the address might be passed to a 32 bit function.
1425 // Section names that look like .mips16.call.FNNAME contain stubs
1426 // that copy floating point arguments from the gp regs to the fp
1427 // regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1428 // then any 16 bit function that calls FNNAME should be redirected
1429 // to the stub instead. If FNNAME is not a 32 bit function, the
1430 // stub should be discarded.
1432 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1433 // which call FNNAME and then copy the return value from the fp regs
1434 // to the gp regs. These stubs store the return address in $18 while
1435 // calling FNNAME; any function which might call one of these stubs
1436 // must arrange to save $18 around the call. (This case is not
1437 // needed for 32 bit functions that call 16 bit functions, because
1438 // 16 bit functions always return floating point values in both
1439 // $f0/$f1 and $2/$3.)
1441 // Note that in all cases FNNAME might be defined statically.
1442 // Therefore, FNNAME is not used literally. Instead, the relocation
1443 // information will indicate which symbol the section is for.
1445 // We record any stubs that we find in the symbol table.
1447 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1449 class Mips16_stub_section_base { };
1451 template<int size, bool big_endian>
1452 class Mips16_stub_section : public Mips16_stub_section_base
1454 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1456 public:
1457 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1458 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1459 found_r_mips_none_(false)
1461 gold_assert(object->is_mips16_fn_stub_section(shndx)
1462 || object->is_mips16_call_stub_section(shndx)
1463 || object->is_mips16_call_fp_stub_section(shndx));
1466 // Return the object of this stub section.
1467 Mips_relobj<size, big_endian>*
1468 object() const
1469 { return this->object_; }
1471 // Return the size of a section.
1472 uint64_t
1473 section_size() const
1474 { return this->object_->section_size(this->shndx_); }
1476 // Return section index of this stub section.
1477 unsigned int
1478 shndx() const
1479 { return this->shndx_; }
1481 // Return symbol index, if stub is for a local function.
1482 unsigned int
1483 r_sym() const
1484 { return this->r_sym_; }
1486 // Return symbol, if stub is for a global function.
1487 Mips_symbol<size>*
1488 gsym() const
1489 { return this->gsym_; }
1491 // Return whether stub is for a local function.
1492 bool
1493 is_for_local_function() const
1494 { return this->gsym_ == NULL; }
1496 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1497 // is found in the stub section. Try to find stub target.
1498 void
1499 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1501 // To find target symbol for this stub, trust the first R_MIPS_NONE
1502 // relocation, if any. Otherwise trust the first relocation, whatever
1503 // its kind.
1504 if (this->found_r_mips_none_)
1505 return;
1506 if (r_type == elfcpp::R_MIPS_NONE)
1508 this->r_sym_ = r_sym;
1509 this->gsym_ = NULL;
1510 this->found_r_mips_none_ = true;
1512 else if (!is_target_found())
1513 this->r_sym_ = r_sym;
1516 // This method is called when a new relocation R_TYPE for global symbol GSYM
1517 // is found in the stub section. Try to find stub target.
1518 void
1519 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1521 // To find target symbol for this stub, trust the first R_MIPS_NONE
1522 // relocation, if any. Otherwise trust the first relocation, whatever
1523 // its kind.
1524 if (this->found_r_mips_none_)
1525 return;
1526 if (r_type == elfcpp::R_MIPS_NONE)
1528 this->gsym_ = gsym;
1529 this->r_sym_ = 0;
1530 this->found_r_mips_none_ = true;
1532 else if (!is_target_found())
1533 this->gsym_ = gsym;
1536 // Return whether we found the stub target.
1537 bool
1538 is_target_found() const
1539 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1541 // Return whether this is a fn stub.
1542 bool
1543 is_fn_stub() const
1544 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1546 // Return whether this is a call stub.
1547 bool
1548 is_call_stub() const
1549 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1551 // Return whether this is a call_fp stub.
1552 bool
1553 is_call_fp_stub() const
1554 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1556 // Return the output address.
1557 Mips_address
1558 output_address() const
1560 return (this->object_->output_section(this->shndx_)->address()
1561 + this->object_->output_section_offset(this->shndx_));
1564 private:
1565 // The object of this stub section.
1566 Mips_relobj<size, big_endian>* object_;
1567 // The section index of this stub section.
1568 unsigned int shndx_;
1569 // The symbol index, if stub is for a local function.
1570 unsigned int r_sym_;
1571 // The symbol, if stub is for a global function.
1572 Mips_symbol<size>* gsym_;
1573 // True if we found R_MIPS_NONE relocation in this stub.
1574 bool found_r_mips_none_;
1577 // Mips_relobj class.
1579 template<int size, bool big_endian>
1580 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1582 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1583 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1584 Mips16_stubs_int_map;
1585 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1587 public:
1588 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1589 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1590 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1591 processor_specific_flags_(0), local_symbol_is_mips16_(),
1592 local_symbol_is_micromips_(), mips16_stub_sections_(),
1593 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1594 local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1595 merge_processor_specific_data_(true), got_info_(NULL),
1596 section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1597 section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
1598 attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
1599 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1601 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1602 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1605 ~Mips_relobj()
1606 { delete this->attributes_section_data_; }
1608 // Downcast a base pointer to a Mips_relobj pointer. This is
1609 // not type-safe but we only use Mips_relobj not the base class.
1610 static Mips_relobj<size, big_endian>*
1611 as_mips_relobj(Relobj* relobj)
1612 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1614 // Downcast a base pointer to a Mips_relobj pointer. This is
1615 // not type-safe but we only use Mips_relobj not the base class.
1616 static const Mips_relobj<size, big_endian>*
1617 as_mips_relobj(const Relobj* relobj)
1618 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1620 // Processor-specific flags in ELF file header. This is valid only after
1621 // reading symbols.
1622 elfcpp::Elf_Word
1623 processor_specific_flags() const
1624 { return this->processor_specific_flags_; }
1626 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1627 // index. This is only valid after do_count_local_symbol is called.
1628 bool
1629 local_symbol_is_mips16(unsigned int r_sym) const
1631 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1632 return this->local_symbol_is_mips16_[r_sym];
1635 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1636 // index. This is only valid after do_count_local_symbol is called.
1637 bool
1638 local_symbol_is_micromips(unsigned int r_sym) const
1640 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1641 return this->local_symbol_is_micromips_[r_sym];
1644 // Get or create MIPS16 stub section.
1645 Mips16_stub_section<size, big_endian>*
1646 get_mips16_stub_section(unsigned int shndx)
1648 typename Mips16_stubs_int_map::const_iterator it =
1649 this->mips16_stub_sections_.find(shndx);
1650 if (it != this->mips16_stub_sections_.end())
1651 return (*it).second;
1653 Mips16_stub_section<size, big_endian>* stub_section =
1654 new Mips16_stub_section<size, big_endian>(this, shndx);
1655 this->mips16_stub_sections_.insert(
1656 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1657 stub_section->shndx(), stub_section));
1658 return stub_section;
1661 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1662 // object doesn't have fn stub for R_SYM.
1663 Mips16_stub_section<size, big_endian>*
1664 get_local_mips16_fn_stub(unsigned int r_sym) const
1666 typename Mips16_stubs_int_map::const_iterator it =
1667 this->local_mips16_fn_stubs_.find(r_sym);
1668 if (it != this->local_mips16_fn_stubs_.end())
1669 return (*it).second;
1670 return NULL;
1673 // Record that this object has MIPS16 fn stub for local symbol. This method
1674 // is only called if we decided not to discard the stub.
1675 void
1676 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1678 gold_assert(stub->is_for_local_function());
1679 unsigned int r_sym = stub->r_sym();
1680 this->local_mips16_fn_stubs_.insert(
1681 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1682 r_sym, stub));
1685 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1686 // object doesn't have call stub for R_SYM.
1687 Mips16_stub_section<size, big_endian>*
1688 get_local_mips16_call_stub(unsigned int r_sym) const
1690 typename Mips16_stubs_int_map::const_iterator it =
1691 this->local_mips16_call_stubs_.find(r_sym);
1692 if (it != this->local_mips16_call_stubs_.end())
1693 return (*it).second;
1694 return NULL;
1697 // Record that this object has MIPS16 call stub for local symbol. This method
1698 // is only called if we decided not to discard the stub.
1699 void
1700 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1702 gold_assert(stub->is_for_local_function());
1703 unsigned int r_sym = stub->r_sym();
1704 this->local_mips16_call_stubs_.insert(
1705 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1706 r_sym, stub));
1709 // Record that we found "non 16-bit" call relocation against local symbol
1710 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1711 // is one.
1712 void
1713 add_local_non_16bit_call(unsigned int symndx)
1714 { this->local_non_16bit_calls_.insert(symndx); }
1716 // Return true if there is any "non 16-bit" call relocation against local
1717 // symbol SYMNDX in this object.
1718 bool
1719 has_local_non_16bit_call_relocs(unsigned int symndx)
1721 return (this->local_non_16bit_calls_.find(symndx)
1722 != this->local_non_16bit_calls_.end());
1725 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1726 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1727 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1728 void
1729 add_local_16bit_call(unsigned int symndx)
1730 { this->local_16bit_calls_.insert(symndx); }
1732 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1733 // symbol SYMNDX in this object.
1734 bool
1735 has_local_16bit_call_relocs(unsigned int symndx)
1737 return (this->local_16bit_calls_.find(symndx)
1738 != this->local_16bit_calls_.end());
1741 // Get gp value that was used to create this object.
1742 Mips_address
1743 gp_value() const
1744 { return this->gp_; }
1746 // Return whether the object is a PIC object.
1747 bool
1748 is_pic() const
1749 { return this->is_pic_; }
1751 // Return whether the object uses N32 ABI.
1752 bool
1753 is_n32() const
1754 { return this->is_n32_; }
1756 // Return whether the object uses N64 ABI.
1757 bool
1758 is_n64() const
1759 { return size == 64; }
1761 // Return whether the object uses NewABI conventions.
1762 bool
1763 is_newabi() const
1764 { return this->is_n32() || this->is_n64(); }
1766 // Return Mips_got_info for this object.
1767 Mips_got_info<size, big_endian>*
1768 get_got_info() const
1769 { return this->got_info_; }
1771 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1772 Mips_got_info<size, big_endian>*
1773 get_or_create_got_info()
1775 if (!this->got_info_)
1776 this->got_info_ = new Mips_got_info<size, big_endian>();
1777 return this->got_info_;
1780 // Set Mips_got_info for this object.
1781 void
1782 set_got_info(Mips_got_info<size, big_endian>* got_info)
1783 { this->got_info_ = got_info; }
1785 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1786 // after do_read_symbols is called.
1787 bool
1788 is_mips16_stub_section(unsigned int shndx)
1790 return (is_mips16_fn_stub_section(shndx)
1791 || is_mips16_call_stub_section(shndx)
1792 || is_mips16_call_fp_stub_section(shndx));
1795 // Return TRUE if relocations in section SHNDX can refer directly to a
1796 // MIPS16 function rather than to a hard-float stub. This is only valid
1797 // after do_read_symbols is called.
1798 bool
1799 section_allows_mips16_refs(unsigned int shndx)
1801 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1804 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1805 // after do_read_symbols is called.
1806 bool
1807 is_mips16_fn_stub_section(unsigned int shndx)
1809 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1810 return this->section_is_mips16_fn_stub_[shndx];
1813 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1814 // after do_read_symbols is called.
1815 bool
1816 is_mips16_call_stub_section(unsigned int shndx)
1818 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1819 return this->section_is_mips16_call_stub_[shndx];
1822 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1823 // valid after do_read_symbols is called.
1824 bool
1825 is_mips16_call_fp_stub_section(unsigned int shndx)
1827 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1828 return this->section_is_mips16_call_fp_stub_[shndx];
1831 // Discard MIPS16 stub secions that are not needed.
1832 void
1833 discard_mips16_stub_sections(Symbol_table* symtab);
1835 // Return whether there is a .reginfo section.
1836 bool
1837 has_reginfo_section() const
1838 { return this->has_reginfo_section_; }
1840 // Return whether we want to merge processor-specific data.
1841 bool
1842 merge_processor_specific_data() const
1843 { return this->merge_processor_specific_data_; }
1845 // Return gprmask from the .reginfo section of this object.
1846 Valtype
1847 gprmask() const
1848 { return this->gprmask_; }
1850 // Return cprmask1 from the .reginfo section of this object.
1851 Valtype
1852 cprmask1() const
1853 { return this->cprmask1_; }
1855 // Return cprmask2 from the .reginfo section of this object.
1856 Valtype
1857 cprmask2() const
1858 { return this->cprmask2_; }
1860 // Return cprmask3 from the .reginfo section of this object.
1861 Valtype
1862 cprmask3() const
1863 { return this->cprmask3_; }
1865 // Return cprmask4 from the .reginfo section of this object.
1866 Valtype
1867 cprmask4() const
1868 { return this->cprmask4_; }
1870 // This is the contents of the .MIPS.abiflags section if there is one.
1871 Mips_abiflags<big_endian>*
1872 abiflags()
1873 { return this->abiflags_; }
1875 // This is the contents of the .gnu.attribute section if there is one.
1876 const Attributes_section_data*
1877 attributes_section_data() const
1878 { return this->attributes_section_data_; }
1880 protected:
1881 // Count the local symbols.
1882 void
1883 do_count_local_symbols(Stringpool_template<char>*,
1884 Stringpool_template<char>*);
1886 // Read the symbol information.
1887 void
1888 do_read_symbols(Read_symbols_data* sd);
1890 private:
1891 // The name of the options section.
1892 const char* mips_elf_options_section_name()
1893 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1895 // processor-specific flags in ELF file header.
1896 elfcpp::Elf_Word processor_specific_flags_;
1898 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1899 // This is only valid after do_count_local_symbol is called.
1900 std::vector<bool> local_symbol_is_mips16_;
1902 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1903 // This is only valid after do_count_local_symbol is called.
1904 std::vector<bool> local_symbol_is_micromips_;
1906 // Map from section index to the MIPS16 stub for that section. This contains
1907 // all stubs found in this object.
1908 Mips16_stubs_int_map mips16_stub_sections_;
1910 // Local symbols that have "non 16-bit" call relocation. This relocation
1911 // would need to refer to a MIPS16 fn stub, if there is one.
1912 std::set<unsigned int> local_non_16bit_calls_;
1914 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1915 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1916 // relocation that refers to the stub symbol.
1917 std::set<unsigned int> local_16bit_calls_;
1919 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1920 // This contains only the stubs that we decided not to discard.
1921 Mips16_stubs_int_map local_mips16_fn_stubs_;
1923 // Map from local symbol index to the MIPS16 call stub for that symbol.
1924 // This contains only the stubs that we decided not to discard.
1925 Mips16_stubs_int_map local_mips16_call_stubs_;
1927 // gp value that was used to create this object.
1928 Mips_address gp_;
1929 // Whether the object is a PIC object.
1930 bool is_pic_ : 1;
1931 // Whether the object uses N32 ABI.
1932 bool is_n32_ : 1;
1933 // Whether the object contains a .reginfo section.
1934 bool has_reginfo_section_ : 1;
1935 // Whether we merge processor-specific data of this object to output.
1936 bool merge_processor_specific_data_ : 1;
1937 // The Mips_got_info for this object.
1938 Mips_got_info<size, big_endian>* got_info_;
1940 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1941 // This is only valid after do_read_symbols is called.
1942 std::vector<bool> section_is_mips16_fn_stub_;
1944 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1945 // This is only valid after do_read_symbols is called.
1946 std::vector<bool> section_is_mips16_call_stub_;
1948 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1949 // This is only valid after do_read_symbols is called.
1950 std::vector<bool> section_is_mips16_call_fp_stub_;
1952 // .pdr section index.
1953 unsigned int pdr_shndx_;
1955 // Object attributes if there is a .gnu.attributes section or NULL.
1956 Attributes_section_data* attributes_section_data_;
1958 // Object abiflags if there is a .MIPS.abiflags section or NULL.
1959 Mips_abiflags<big_endian>* abiflags_;
1961 // gprmask from the .reginfo section of this object.
1962 Valtype gprmask_;
1963 // cprmask1 from the .reginfo section of this object.
1964 Valtype cprmask1_;
1965 // cprmask2 from the .reginfo section of this object.
1966 Valtype cprmask2_;
1967 // cprmask3 from the .reginfo section of this object.
1968 Valtype cprmask3_;
1969 // cprmask4 from the .reginfo section of this object.
1970 Valtype cprmask4_;
1973 // Mips_output_data_got class.
1975 template<int size, bool big_endian>
1976 class Mips_output_data_got : public Output_data_got<size, big_endian>
1978 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1979 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1980 Reloc_section;
1981 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1983 public:
1984 Mips_output_data_got(Target_mips<size, big_endian>* target,
1985 Symbol_table* symtab, Layout* layout)
1986 : Output_data_got<size, big_endian>(), target_(target),
1987 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1988 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1989 secondary_got_relocs_()
1991 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1992 this->set_addralign(16);
1995 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1996 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1997 void
1998 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1999 unsigned int symndx, Mips_address addend,
2000 unsigned int r_type, unsigned int shndx,
2001 bool is_section_symbol)
2003 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
2004 r_type, shndx,
2005 is_section_symbol);
2008 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
2009 // in OBJECT. FOR_CALL is true if the caller is only interested in
2010 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
2011 // relocation.
2012 void
2013 record_global_got_symbol(Mips_symbol<size>* mips_sym,
2014 Mips_relobj<size, big_endian>* object,
2015 unsigned int r_type, bool dyn_reloc, bool for_call)
2017 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2018 dyn_reloc, for_call);
2021 // Record that OBJECT has a page relocation against symbol SYMNDX and
2022 // that ADDEND is the addend for that relocation.
2023 void
2024 record_got_page_entry(Mips_relobj<size, big_endian>* object,
2025 unsigned int symndx, int addend)
2026 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2028 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
2029 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2030 // applied in a static link.
2031 void
2032 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2033 Mips_symbol<size>* gsym)
2034 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2036 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
2037 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
2038 // relocation that needs to be applied in a static link.
2039 void
2040 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2041 Sized_relobj_file<size, big_endian>* relobj,
2042 unsigned int index)
2044 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2045 index));
2048 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2049 // secondary GOT at OFFSET.
2050 void
2051 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2052 Mips_symbol<size>* gsym)
2054 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2055 r_type, gsym));
2058 // Update GOT entry at OFFSET with VALUE.
2059 void
2060 update_got_entry(unsigned int offset, Mips_address value)
2062 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2065 // Return the number of entries in local part of the GOT. This includes
2066 // local entries, page entries and 2 reserved entries.
2067 unsigned int
2068 get_local_gotno() const
2070 if (!this->multi_got())
2072 return (2 + this->master_got_info_->local_gotno()
2073 + this->master_got_info_->page_gotno());
2075 else
2076 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2079 // Return dynamic symbol table index of the first symbol with global GOT
2080 // entry.
2081 unsigned int
2082 first_global_got_dynsym_index() const
2083 { return this->first_global_got_dynsym_index_; }
2085 // Set dynamic symbol table index of the first symbol with global GOT entry.
2086 void
2087 set_first_global_got_dynsym_index(unsigned int index)
2088 { this->first_global_got_dynsym_index_ = index; }
2090 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2091 // larger than 64K, create multi-GOT.
2092 void
2093 lay_out_got(Layout* layout, Symbol_table* symtab,
2094 const Input_objects* input_objects);
2096 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2097 void
2098 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2100 // Attempt to merge GOTs of different input objects.
2101 void
2102 merge_gots(const Input_objects* input_objects);
2104 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
2105 // this would lead to overflow, true if they were merged successfully.
2106 bool
2107 merge_got_with(Mips_got_info<size, big_endian>* from,
2108 Mips_relobj<size, big_endian>* object,
2109 Mips_got_info<size, big_endian>* to);
2111 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2112 // use OBJECT's GOT.
2113 unsigned int
2114 get_got_page_offset(Mips_address value,
2115 const Mips_relobj<size, big_endian>* object)
2117 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2118 ? this->master_got_info_
2119 : object->get_got_info());
2120 gold_assert(g != NULL);
2121 return g->get_got_page_offset(value, this);
2124 // Return the GOT offset of type GOT_TYPE of the global symbol
2125 // GSYM. For multi-GOT links, use OBJECT's GOT.
2126 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2127 Mips_relobj<size, big_endian>* object) const
2129 if (!this->multi_got())
2130 return gsym->got_offset(got_type);
2131 else
2133 Mips_got_info<size, big_endian>* g = object->get_got_info();
2134 gold_assert(g != NULL);
2135 return gsym->got_offset(g->multigot_got_type(got_type));
2139 // Return the GOT offset of type GOT_TYPE of the local symbol
2140 // SYMNDX.
2141 unsigned int
2142 got_offset(unsigned int symndx, unsigned int got_type,
2143 Sized_relobj_file<size, big_endian>* object,
2144 uint64_t addend) const
2145 { return object->local_got_offset(symndx, got_type, addend); }
2147 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2148 unsigned int
2149 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2151 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2152 ? this->master_got_info_
2153 : object->get_got_info());
2154 gold_assert(g != NULL);
2155 return g->tls_ldm_offset();
2158 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2159 void
2160 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2161 Mips_relobj<size, big_endian>* object)
2163 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2164 ? this->master_got_info_
2165 : object->get_got_info());
2166 gold_assert(g != NULL);
2167 g->set_tls_ldm_offset(tls_ldm_offset);
2170 // Return true for multi-GOT links.
2171 bool
2172 multi_got() const
2173 { return this->primary_got_ != NULL; }
2175 // Return the offset of OBJECT's GOT from the start of .got section.
2176 unsigned int
2177 get_got_offset(const Mips_relobj<size, big_endian>* object)
2179 if (!this->multi_got())
2180 return 0;
2181 else
2183 Mips_got_info<size, big_endian>* g = object->get_got_info();
2184 return g != NULL ? g->offset() : 0;
2188 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2189 void
2190 add_reloc_only_entries()
2191 { this->master_got_info_->add_reloc_only_entries(this); }
2193 // Return offset of the primary GOT's entry for global symbol.
2194 unsigned int
2195 get_primary_got_offset(const Mips_symbol<size>* sym) const
2197 gold_assert(sym->global_got_area() != GGA_NONE);
2198 return (this->get_local_gotno() + sym->dynsym_index()
2199 - this->first_global_got_dynsym_index()) * size/8;
2202 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2203 // Input argument GOT_OFFSET is always global offset from the start of
2204 // .got section, for both single and multi-GOT links.
2205 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2206 // links, the return value is object_got_offset - 0x7FF0, where
2207 // object_got_offset is offset in the OBJECT's GOT.
2209 gp_offset(unsigned int got_offset,
2210 const Mips_relobj<size, big_endian>* object) const
2212 return (this->address() + got_offset
2213 - this->target_->adjusted_gp_value(object));
2216 protected:
2217 // Write out the GOT table.
2218 void
2219 do_write(Output_file*);
2221 private:
2223 // This class represent dynamic relocations that need to be applied by
2224 // gold because we are using TLS relocations in a static link.
2225 class Static_reloc
2227 public:
2228 Static_reloc(unsigned int got_offset, unsigned int r_type,
2229 Mips_symbol<size>* gsym)
2230 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2231 { this->u_.global.symbol = gsym; }
2233 Static_reloc(unsigned int got_offset, unsigned int r_type,
2234 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2235 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2237 this->u_.local.relobj = relobj;
2238 this->u_.local.index = index;
2241 // Return the GOT offset.
2242 unsigned int
2243 got_offset() const
2244 { return this->got_offset_; }
2246 // Relocation type.
2247 unsigned int
2248 r_type() const
2249 { return this->r_type_; }
2251 // Whether the symbol is global or not.
2252 bool
2253 symbol_is_global() const
2254 { return this->symbol_is_global_; }
2256 // For a relocation against a global symbol, the global symbol.
2257 Mips_symbol<size>*
2258 symbol() const
2260 gold_assert(this->symbol_is_global_);
2261 return this->u_.global.symbol;
2264 // For a relocation against a local symbol, the defining object.
2265 Sized_relobj_file<size, big_endian>*
2266 relobj() const
2268 gold_assert(!this->symbol_is_global_);
2269 return this->u_.local.relobj;
2272 // For a relocation against a local symbol, the local symbol index.
2273 unsigned int
2274 index() const
2276 gold_assert(!this->symbol_is_global_);
2277 return this->u_.local.index;
2280 private:
2281 // GOT offset of the entry to which this relocation is applied.
2282 unsigned int got_offset_;
2283 // Type of relocation.
2284 unsigned int r_type_;
2285 // Whether this relocation is against a global symbol.
2286 bool symbol_is_global_;
2287 // A global or local symbol.
2288 union
2290 struct
2292 // For a global symbol, the symbol itself.
2293 Mips_symbol<size>* symbol;
2294 } global;
2295 struct
2297 // For a local symbol, the object defining object.
2298 Sized_relobj_file<size, big_endian>* relobj;
2299 // For a local symbol, the symbol index.
2300 unsigned int index;
2301 } local;
2302 } u_;
2305 // The target.
2306 Target_mips<size, big_endian>* target_;
2307 // The symbol table.
2308 Symbol_table* symbol_table_;
2309 // The layout.
2310 Layout* layout_;
2311 // Static relocs to be applied to the GOT.
2312 std::vector<Static_reloc> static_relocs_;
2313 // .got section view.
2314 unsigned char* got_view_;
2315 // The dynamic symbol table index of the first symbol with global GOT entry.
2316 unsigned int first_global_got_dynsym_index_;
2317 // The master GOT information.
2318 Mips_got_info<size, big_endian>* master_got_info_;
2319 // The primary GOT information.
2320 Mips_got_info<size, big_endian>* primary_got_;
2321 // Secondary GOT fixups.
2322 std::vector<Static_reloc> secondary_got_relocs_;
2325 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2326 // two ways of creating these interfaces. The first is to add:
2328 // lui $25,%hi(func)
2329 // j func
2330 // addiu $25,$25,%lo(func)
2332 // to a separate trampoline section. The second is to add:
2334 // lui $25,%hi(func)
2335 // addiu $25,$25,%lo(func)
2337 // immediately before a PIC function "func", but only if a function is at the
2338 // beginning of the section, and the section is not too heavily aligned (i.e we
2339 // would need to add no more than 2 nops before the stub.)
2341 // We only create stubs of the first type.
2343 template<int size, bool big_endian>
2344 class Mips_output_data_la25_stub : public Output_section_data
2346 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2348 public:
2349 Mips_output_data_la25_stub()
2350 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2353 // Create LA25 stub for a symbol.
2354 void
2355 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2356 Mips_symbol<size>* gsym);
2358 // Return output address of a stub.
2359 Mips_address
2360 stub_address(const Mips_symbol<size>* sym) const
2362 gold_assert(sym->has_la25_stub());
2363 return this->address() + sym->la25_stub_offset();
2366 protected:
2367 void
2368 do_adjust_output_section(Output_section* os)
2369 { os->set_entsize(0); }
2371 private:
2372 // Template for standard LA25 stub.
2373 static const uint32_t la25_stub_entry[];
2374 // Template for microMIPS LA25 stub.
2375 static const uint32_t la25_stub_micromips_entry[];
2377 // Set the final size.
2378 void
2379 set_final_data_size()
2380 { this->set_data_size(this->symbols_.size() * 16); }
2382 // Create a symbol for SYM stub's value and size, to help make the
2383 // disassembly easier to read.
2384 void
2385 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2386 Target_mips<size, big_endian>* target, uint64_t symsize);
2388 // Write to a map file.
2389 void
2390 do_print_to_mapfile(Mapfile* mapfile) const
2391 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2393 // Write out the LA25 stub section.
2394 void
2395 do_write(Output_file*);
2397 // Symbols that have LA25 stubs.
2398 std::vector<Mips_symbol<size>*> symbols_;
2401 // MIPS-specific relocation writer.
2403 template<int sh_type, bool dynamic, int size, bool big_endian>
2404 struct Mips_output_reloc_writer;
2406 template<int sh_type, bool dynamic, bool big_endian>
2407 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2409 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2410 typedef std::vector<Output_reloc_type> Relocs;
2412 static void
2413 write(typename Relocs::const_iterator p, unsigned char* pov)
2414 { p->write(pov); }
2417 template<int sh_type, bool dynamic, bool big_endian>
2418 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2420 typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2421 typedef std::vector<Output_reloc_type> Relocs;
2423 static void
2424 write(typename Relocs::const_iterator p, unsigned char* pov)
2426 elfcpp::Mips64_rel_write<big_endian> orel(pov);
2427 orel.put_r_offset(p->get_address());
2428 orel.put_r_sym(p->get_symbol_index());
2429 orel.put_r_ssym(RSS_UNDEF);
2430 orel.put_r_type(p->type());
2431 if (p->type() == elfcpp::R_MIPS_REL32)
2432 orel.put_r_type2(elfcpp::R_MIPS_64);
2433 else
2434 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2435 orel.put_r_type3(elfcpp::R_MIPS_NONE);
2439 template<int sh_type, bool dynamic, int size, bool big_endian>
2440 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2441 size, big_endian>
2443 public:
2444 Mips_output_data_reloc(bool sort_relocs)
2445 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2448 protected:
2449 // Write out the data.
2450 void
2451 do_write(Output_file* of)
2453 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2454 big_endian> Writer;
2455 this->template do_write_generic<Writer>(of);
2460 // A class to handle the PLT data.
2462 template<int size, bool big_endian>
2463 class Mips_output_data_plt : public Output_section_data
2465 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2466 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2467 size, big_endian> Reloc_section;
2469 public:
2470 // Create the PLT section. The ordinary .got section is an argument,
2471 // since we need to refer to the start.
2472 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2473 Target_mips<size, big_endian>* target)
2474 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2475 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2476 target_(target)
2478 this->rel_ = new Reloc_section(false);
2479 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2480 elfcpp::SHF_ALLOC, this->rel_,
2481 ORDER_DYNAMIC_PLT_RELOCS, false);
2484 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2485 void
2486 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2488 // Return the .rel.plt section data.
2489 Reloc_section*
2490 rel_plt() const
2491 { return this->rel_; }
2493 // Return the number of PLT entries.
2494 unsigned int
2495 entry_count() const
2496 { return this->symbols_.size(); }
2498 // Return the offset of the first non-reserved PLT entry.
2499 unsigned int
2500 first_plt_entry_offset() const
2501 { return sizeof(plt0_entry_o32); }
2503 // Return the size of a PLT entry.
2504 unsigned int
2505 plt_entry_size() const
2506 { return sizeof(plt_entry); }
2508 // Set final PLT offsets. For each symbol, determine whether standard or
2509 // compressed (MIPS16 or microMIPS) PLT entry is used.
2510 void
2511 set_plt_offsets();
2513 // Return the offset of the first standard PLT entry.
2514 unsigned int
2515 first_mips_plt_offset() const
2516 { return this->plt_header_size_; }
2518 // Return the offset of the first compressed PLT entry.
2519 unsigned int
2520 first_comp_plt_offset() const
2521 { return this->plt_header_size_ + this->plt_mips_offset_; }
2523 // Return whether there are any standard PLT entries.
2524 bool
2525 has_standard_entries() const
2526 { return this->plt_mips_offset_ > 0; }
2528 // Return the output address of standard PLT entry.
2529 Mips_address
2530 mips_entry_address(const Mips_symbol<size>* sym) const
2532 gold_assert (sym->has_mips_plt_offset());
2533 return (this->address() + this->first_mips_plt_offset()
2534 + sym->mips_plt_offset());
2537 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2538 Mips_address
2539 comp_entry_address(const Mips_symbol<size>* sym) const
2541 gold_assert (sym->has_comp_plt_offset());
2542 return (this->address() + this->first_comp_plt_offset()
2543 + sym->comp_plt_offset());
2546 protected:
2547 void
2548 do_adjust_output_section(Output_section* os)
2549 { os->set_entsize(0); }
2551 // Write to a map file.
2552 void
2553 do_print_to_mapfile(Mapfile* mapfile) const
2554 { mapfile->print_output_data(this, _(".plt")); }
2556 private:
2557 // Template for the first PLT entry.
2558 static const uint32_t plt0_entry_o32[];
2559 static const uint32_t plt0_entry_n32[];
2560 static const uint32_t plt0_entry_n64[];
2561 static const uint32_t plt0_entry_micromips_o32[];
2562 static const uint32_t plt0_entry_micromips32_o32[];
2564 // Template for subsequent PLT entries.
2565 static const uint32_t plt_entry[];
2566 static const uint32_t plt_entry_r6[];
2567 static const uint32_t plt_entry_mips16_o32[];
2568 static const uint32_t plt_entry_micromips_o32[];
2569 static const uint32_t plt_entry_micromips32_o32[];
2571 // Set the final size.
2572 void
2573 set_final_data_size()
2575 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2576 + this->plt_comp_offset_);
2579 // Write out the PLT data.
2580 void
2581 do_write(Output_file*);
2583 // Return whether the plt header contains microMIPS code. For the sake of
2584 // cache alignment always use a standard header whenever any standard entries
2585 // are present even if microMIPS entries are present as well. This also lets
2586 // the microMIPS header rely on the value of $v0 only set by microMIPS
2587 // entries, for a small size reduction.
2588 bool
2589 is_plt_header_compressed() const
2591 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2592 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2595 // Return the size of the PLT header.
2596 unsigned int
2597 get_plt_header_size() const
2599 if (this->target_->is_output_n64())
2600 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2601 else if (this->target_->is_output_n32())
2602 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2603 else if (!this->is_plt_header_compressed())
2604 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2605 else if (this->target_->use_32bit_micromips_instructions())
2606 return (2 * sizeof(plt0_entry_micromips32_o32)
2607 / sizeof(plt0_entry_micromips32_o32[0]));
2608 else
2609 return (2 * sizeof(plt0_entry_micromips_o32)
2610 / sizeof(plt0_entry_micromips_o32[0]));
2613 // Return the PLT header entry.
2614 const uint32_t*
2615 get_plt_header_entry() const
2617 if (this->target_->is_output_n64())
2618 return plt0_entry_n64;
2619 else if (this->target_->is_output_n32())
2620 return plt0_entry_n32;
2621 else if (!this->is_plt_header_compressed())
2622 return plt0_entry_o32;
2623 else if (this->target_->use_32bit_micromips_instructions())
2624 return plt0_entry_micromips32_o32;
2625 else
2626 return plt0_entry_micromips_o32;
2629 // Return the size of the standard PLT entry.
2630 unsigned int
2631 standard_plt_entry_size() const
2632 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2634 // Return the size of the compressed PLT entry.
2635 unsigned int
2636 compressed_plt_entry_size() const
2638 gold_assert(!this->target_->is_output_newabi());
2640 if (!this->target_->is_output_micromips())
2641 return (2 * sizeof(plt_entry_mips16_o32)
2642 / sizeof(plt_entry_mips16_o32[0]));
2643 else if (this->target_->use_32bit_micromips_instructions())
2644 return (2 * sizeof(plt_entry_micromips32_o32)
2645 / sizeof(plt_entry_micromips32_o32[0]));
2646 else
2647 return (2 * sizeof(plt_entry_micromips_o32)
2648 / sizeof(plt_entry_micromips_o32[0]));
2651 // The reloc section.
2652 Reloc_section* rel_;
2653 // The .got.plt section.
2654 Output_data_space* got_plt_;
2655 // Symbols that have PLT entry.
2656 std::vector<Mips_symbol<size>*> symbols_;
2657 // The offset of the next standard PLT entry to create.
2658 unsigned int plt_mips_offset_;
2659 // The offset of the next compressed PLT entry to create.
2660 unsigned int plt_comp_offset_;
2661 // The size of the PLT header in bytes.
2662 unsigned int plt_header_size_;
2663 // The target.
2664 Target_mips<size, big_endian>* target_;
2667 // A class to handle the .MIPS.stubs data.
2669 template<int size, bool big_endian>
2670 class Mips_output_data_mips_stubs : public Output_section_data
2672 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2674 // Unordered set of .MIPS.stubs entries.
2675 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2676 Mips_stubs_entry_set;
2678 public:
2679 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2680 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2681 stub_offsets_are_set_(false), target_(target)
2684 // Create entry for a symbol.
2685 void
2686 make_entry(Mips_symbol<size>*);
2688 // Remove entry for a symbol.
2689 void
2690 remove_entry(Mips_symbol<size>* gsym);
2692 // Set stub offsets for symbols. This method expects that the number of
2693 // entries in dynamic symbol table is set.
2694 void
2695 set_lazy_stub_offsets();
2697 void
2698 set_needs_dynsym_value();
2700 // Set the number of entries in dynamic symbol table.
2701 void
2702 set_dynsym_count(unsigned int dynsym_count)
2703 { this->dynsym_count_ = dynsym_count; }
2705 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2706 // count is greater than 0x10000. If the dynamic symbol count is less than
2707 // 0x10000, the stub will be 4 bytes smaller.
2708 // There's no disadvantage from using microMIPS code here, so for the sake of
2709 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2710 // output produced at all. This has a benefit of stubs being shorter by
2711 // 4 bytes each too, unless in the insn32 mode.
2712 unsigned int
2713 stub_max_size() const
2715 if (!this->target_->is_output_micromips()
2716 || this->target_->use_32bit_micromips_instructions())
2717 return 20;
2718 else
2719 return 16;
2722 // Return the size of the stub. This method expects that the final dynsym
2723 // count is set.
2724 unsigned int
2725 stub_size() const
2727 gold_assert(this->dynsym_count_ != -1U);
2728 if (this->dynsym_count_ > 0x10000)
2729 return this->stub_max_size();
2730 else
2731 return this->stub_max_size() - 4;
2734 // Return output address of a stub.
2735 Mips_address
2736 stub_address(const Mips_symbol<size>* sym) const
2738 gold_assert(sym->has_lazy_stub());
2739 return this->address() + sym->lazy_stub_offset();
2742 protected:
2743 void
2744 do_adjust_output_section(Output_section* os)
2745 { os->set_entsize(0); }
2747 // Write to a map file.
2748 void
2749 do_print_to_mapfile(Mapfile* mapfile) const
2750 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2752 private:
2753 static const uint32_t lazy_stub_normal_1[];
2754 static const uint32_t lazy_stub_normal_1_n64[];
2755 static const uint32_t lazy_stub_normal_2[];
2756 static const uint32_t lazy_stub_normal_2_n64[];
2757 static const uint32_t lazy_stub_big[];
2758 static const uint32_t lazy_stub_big_n64[];
2760 static const uint32_t lazy_stub_micromips_normal_1[];
2761 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2762 static const uint32_t lazy_stub_micromips_normal_2[];
2763 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2764 static const uint32_t lazy_stub_micromips_big[];
2765 static const uint32_t lazy_stub_micromips_big_n64[];
2767 static const uint32_t lazy_stub_micromips32_normal_1[];
2768 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2769 static const uint32_t lazy_stub_micromips32_normal_2[];
2770 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2771 static const uint32_t lazy_stub_micromips32_big[];
2772 static const uint32_t lazy_stub_micromips32_big_n64[];
2774 // Set the final size.
2775 void
2776 set_final_data_size()
2777 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2779 // Write out the .MIPS.stubs data.
2780 void
2781 do_write(Output_file*);
2783 // .MIPS.stubs symbols
2784 Mips_stubs_entry_set symbols_;
2785 // Number of entries in dynamic symbol table.
2786 unsigned int dynsym_count_;
2787 // Whether the stub offsets are set.
2788 bool stub_offsets_are_set_;
2789 // The target.
2790 Target_mips<size, big_endian>* target_;
2793 // This class handles Mips .reginfo output section.
2795 template<int size, bool big_endian>
2796 class Mips_output_section_reginfo : public Output_section_data
2798 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2800 public:
2801 Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2802 Valtype gprmask, Valtype cprmask1,
2803 Valtype cprmask2, Valtype cprmask3,
2804 Valtype cprmask4)
2805 : Output_section_data(24, 4, true), target_(target),
2806 gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2807 cprmask3_(cprmask3), cprmask4_(cprmask4)
2810 protected:
2811 // Write to a map file.
2812 void
2813 do_print_to_mapfile(Mapfile* mapfile) const
2814 { mapfile->print_output_data(this, _(".reginfo")); }
2816 // Write out reginfo section.
2817 void
2818 do_write(Output_file* of);
2820 private:
2821 Target_mips<size, big_endian>* target_;
2823 // gprmask of the output .reginfo section.
2824 Valtype gprmask_;
2825 // cprmask1 of the output .reginfo section.
2826 Valtype cprmask1_;
2827 // cprmask2 of the output .reginfo section.
2828 Valtype cprmask2_;
2829 // cprmask3 of the output .reginfo section.
2830 Valtype cprmask3_;
2831 // cprmask4 of the output .reginfo section.
2832 Valtype cprmask4_;
2835 // This class handles .MIPS.options output section.
2837 template<int size, bool big_endian>
2838 class Mips_output_section_options : public Output_section
2840 public:
2841 Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
2842 elfcpp::Elf_Xword flags,
2843 Target_mips<size, big_endian>* target)
2844 : Output_section(name, type, flags), target_(target)
2846 // After the input sections are written, we only need to update
2847 // ri_gp_value field of ODK_REGINFO entries.
2848 this->set_after_input_sections();
2851 protected:
2852 // Write out option section.
2853 void
2854 do_write(Output_file* of);
2856 private:
2857 Target_mips<size, big_endian>* target_;
2860 // This class handles .MIPS.abiflags output section.
2862 template<int size, bool big_endian>
2863 class Mips_output_section_abiflags : public Output_section_data
2865 public:
2866 Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2867 : Output_section_data(24, 8, true), abiflags_(abiflags)
2870 protected:
2871 // Write to a map file.
2872 void
2873 do_print_to_mapfile(Mapfile* mapfile) const
2874 { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2876 void
2877 do_write(Output_file* of);
2879 private:
2880 const Mips_abiflags<big_endian>& abiflags_;
2883 // The MIPS target has relocation types which default handling of relocatable
2884 // relocation cannot process. So we have to extend the default code.
2886 template<bool big_endian, typename Classify_reloc>
2887 class Mips_scan_relocatable_relocs :
2888 public Default_scan_relocatable_relocs<Classify_reloc>
2890 public:
2891 // Return the strategy to use for a local symbol which is a section
2892 // symbol, given the relocation type.
2893 inline Relocatable_relocs::Reloc_strategy
2894 local_section_strategy(unsigned int r_type, Relobj* object)
2896 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2897 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2898 else
2900 switch (r_type)
2902 case elfcpp::R_MIPS_26:
2903 return Relocatable_relocs::RELOC_SPECIAL;
2905 default:
2906 return Default_scan_relocatable_relocs<Classify_reloc>::
2907 local_section_strategy(r_type, object);
2913 // Mips_copy_relocs class. The only difference from the base class is the
2914 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2915 // Mips cannot convert all relocation types to dynamic relocs. If a reloc
2916 // cannot be made dynamic, a COPY reloc is emitted.
2918 template<int sh_type, int size, bool big_endian>
2919 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2921 public:
2922 Mips_copy_relocs()
2923 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2926 // Emit any saved relocations which turn out to be needed. This is
2927 // called after all the relocs have been scanned.
2928 void
2929 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2930 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2932 private:
2933 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2934 Copy_reloc_entry;
2936 // Emit this reloc if appropriate. This is called after we have
2937 // scanned all the relocations, so we know whether we emitted a
2938 // COPY relocation for SYM_.
2939 void
2940 emit_entry(Copy_reloc_entry& entry,
2941 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2942 Symbol_table* symtab, Layout* layout,
2943 Target_mips<size, big_endian>* target);
2947 // Return true if the symbol SYM should be considered to resolve local
2948 // to the current module, and false otherwise. The logic is taken from
2949 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2950 static bool
2951 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2952 bool local_protected)
2954 // If it's a local sym, of course we resolve locally.
2955 if (sym == NULL)
2956 return true;
2958 // STV_HIDDEN or STV_INTERNAL ones must be local.
2959 if (sym->visibility() == elfcpp::STV_HIDDEN
2960 || sym->visibility() == elfcpp::STV_INTERNAL)
2961 return true;
2963 // If we don't have a definition in a regular file, then we can't
2964 // resolve locally. The sym is either undefined or dynamic.
2965 if (sym->is_from_dynobj() || sym->is_undefined())
2966 return false;
2968 // Forced local symbols resolve locally.
2969 if (sym->is_forced_local())
2970 return true;
2972 // As do non-dynamic symbols.
2973 if (!has_dynsym_entry)
2974 return true;
2976 // At this point, we know the symbol is defined and dynamic. In an
2977 // executable it must resolve locally, likewise when building symbolic
2978 // shared libraries.
2979 if (parameters->options().output_is_executable()
2980 || parameters->options().Bsymbolic())
2981 return true;
2983 // Now deal with defined dynamic symbols in shared libraries. Ones
2984 // with default visibility might not resolve locally.
2985 if (sym->visibility() == elfcpp::STV_DEFAULT)
2986 return false;
2988 // STV_PROTECTED non-function symbols are local.
2989 if (sym->type() != elfcpp::STT_FUNC)
2990 return true;
2992 // Function pointer equality tests may require that STV_PROTECTED
2993 // symbols be treated as dynamic symbols. If the address of a
2994 // function not defined in an executable is set to that function's
2995 // plt entry in the executable, then the address of the function in
2996 // a shared library must also be the plt entry in the executable.
2997 return local_protected;
3000 // Return TRUE if references to this symbol always reference the symbol in this
3001 // object.
3002 static bool
3003 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3005 return symbol_refs_local(sym, has_dynsym_entry, false);
3008 // Return TRUE if calls to this symbol always call the version in this object.
3009 static bool
3010 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3012 return symbol_refs_local(sym, has_dynsym_entry, true);
3015 // Compare GOT offsets of two symbols.
3017 template<int size, bool big_endian>
3018 static bool
3019 got_offset_compare(Symbol* sym1, Symbol* sym2)
3021 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
3022 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
3023 unsigned int area1 = mips_sym1->global_got_area();
3024 unsigned int area2 = mips_sym2->global_got_area();
3025 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
3027 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3028 if (area1 != area2)
3029 return area1 < area2;
3031 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
3034 // This method divides dynamic symbols into symbols that have GOT entry, and
3035 // symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
3036 // Mips ABI requires that symbols with the GOT entry must be at the end of
3037 // dynamic symbol table, and the order in dynamic symbol table must match the
3038 // order in GOT.
3040 template<int size, bool big_endian>
3041 static void
3042 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3043 std::vector<Symbol*>* non_got_symbols,
3044 std::vector<Symbol*>* got_symbols)
3046 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3047 p != dyn_symbols->end();
3048 ++p)
3050 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3051 if (mips_sym->global_got_area() == GGA_NORMAL
3052 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3053 got_symbols->push_back(mips_sym);
3054 else
3055 non_got_symbols->push_back(mips_sym);
3058 std::sort(got_symbols->begin(), got_symbols->end(),
3059 got_offset_compare<size, big_endian>);
3062 // Functor class for processing the global symbol table.
3064 template<int size, bool big_endian>
3065 class Symbol_visitor_check_symbols
3067 public:
3068 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3069 Layout* layout, Symbol_table* symtab)
3070 : target_(target), layout_(layout), symtab_(symtab)
3073 void
3074 operator()(Sized_symbol<size>* sym)
3076 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3077 if (local_pic_function<size, big_endian>(mips_sym))
3079 // SYM is a function that might need $25 to be valid on entry.
3080 // If we're creating a non-PIC relocatable object, mark SYM as
3081 // being PIC. If we're creating a non-relocatable object with
3082 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3083 // stub.
3084 if (parameters->options().relocatable())
3086 if (!parameters->options().output_is_position_independent())
3087 mips_sym->set_pic();
3089 else if (mips_sym->has_nonpic_branches())
3091 this->target_->la25_stub_section(layout_)
3092 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3097 private:
3098 Target_mips<size, big_endian>* target_;
3099 Layout* layout_;
3100 Symbol_table* symtab_;
3103 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3104 // and endianness. The relocation format for MIPS-64 is non-standard.
3106 template<int sh_type, int size, bool big_endian>
3107 struct Mips_reloc_types;
3109 template<bool big_endian>
3110 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3112 typedef typename elfcpp::Rel<32, big_endian> Reloc;
3113 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3115 static typename elfcpp::Elf_types<32>::Elf_Swxword
3116 get_r_addend(const Reloc*)
3117 { return 0; }
3119 static inline void
3120 set_reloc_addend(Reloc_write*,
3121 typename elfcpp::Elf_types<32>::Elf_Swxword)
3122 { gold_unreachable(); }
3125 template<bool big_endian>
3126 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3128 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3129 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3131 static typename elfcpp::Elf_types<32>::Elf_Swxword
3132 get_r_addend(const Reloc* reloc)
3133 { return reloc->get_r_addend(); }
3135 static inline void
3136 set_reloc_addend(Reloc_write* p,
3137 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3138 { p->put_r_addend(val); }
3141 template<bool big_endian>
3142 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3144 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3145 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3147 static typename elfcpp::Elf_types<64>::Elf_Swxword
3148 get_r_addend(const Reloc*)
3149 { return 0; }
3151 static inline void
3152 set_reloc_addend(Reloc_write*,
3153 typename elfcpp::Elf_types<64>::Elf_Swxword)
3154 { gold_unreachable(); }
3157 template<bool big_endian>
3158 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3160 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3161 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3163 static typename elfcpp::Elf_types<64>::Elf_Swxword
3164 get_r_addend(const Reloc* reloc)
3165 { return reloc->get_r_addend(); }
3167 static inline void
3168 set_reloc_addend(Reloc_write* p,
3169 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3170 { p->put_r_addend(val); }
3173 // Forward declaration.
3174 static unsigned int
3175 mips_get_size_for_reloc(unsigned int, Relobj*);
3177 // A class for inquiring about properties of a relocation,
3178 // used while scanning relocs during a relocatable link and
3179 // garbage collection.
3181 template<int sh_type_, int size, bool big_endian>
3182 class Mips_classify_reloc;
3184 template<int sh_type_, bool big_endian>
3185 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3186 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3188 public:
3189 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3190 Reltype;
3191 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3192 Reltype_write;
3194 // Return the symbol referred to by the relocation.
3195 static inline unsigned int
3196 get_r_sym(const Reltype* reloc)
3197 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3199 // Return the type of the relocation.
3200 static inline unsigned int
3201 get_r_type(const Reltype* reloc)
3202 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3204 static inline unsigned int
3205 get_r_type2(const Reltype*)
3206 { return 0; }
3208 static inline unsigned int
3209 get_r_type3(const Reltype*)
3210 { return 0; }
3212 static inline unsigned int
3213 get_r_ssym(const Reltype*)
3214 { return 0; }
3216 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3217 static inline unsigned int
3218 get_r_addend(const Reltype* reloc)
3220 if (sh_type_ == elfcpp::SHT_REL)
3221 return 0;
3222 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3225 // Write the r_info field to a new reloc, using the r_info field from
3226 // the original reloc, replacing the r_sym field with R_SYM.
3227 static inline void
3228 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3230 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3231 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3234 // Write the r_addend field to a new reloc.
3235 static inline void
3236 put_r_addend(Reltype_write* to,
3237 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3238 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3240 // Return the size of the addend of the relocation (only used for SHT_REL).
3241 static unsigned int
3242 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3243 { return mips_get_size_for_reloc(r_type, obj); }
3246 template<int sh_type_, bool big_endian>
3247 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3248 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3250 public:
3251 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3252 Reltype;
3253 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3254 Reltype_write;
3256 // Return the symbol referred to by the relocation.
3257 static inline unsigned int
3258 get_r_sym(const Reltype* reloc)
3259 { return reloc->get_r_sym(); }
3261 // Return the r_type of the relocation.
3262 static inline unsigned int
3263 get_r_type(const Reltype* reloc)
3264 { return reloc->get_r_type(); }
3266 // Return the r_type2 of the relocation.
3267 static inline unsigned int
3268 get_r_type2(const Reltype* reloc)
3269 { return reloc->get_r_type2(); }
3271 // Return the r_type3 of the relocation.
3272 static inline unsigned int
3273 get_r_type3(const Reltype* reloc)
3274 { return reloc->get_r_type3(); }
3276 // Return the special symbol of the relocation.
3277 static inline unsigned int
3278 get_r_ssym(const Reltype* reloc)
3279 { return reloc->get_r_ssym(); }
3281 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3282 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3283 get_r_addend(const Reltype* reloc)
3285 if (sh_type_ == elfcpp::SHT_REL)
3286 return 0;
3287 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3290 // Write the r_info field to a new reloc, using the r_info field from
3291 // the original reloc, replacing the r_sym field with R_SYM.
3292 static inline void
3293 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3295 new_reloc->put_r_sym(r_sym);
3296 new_reloc->put_r_ssym(reloc->get_r_ssym());
3297 new_reloc->put_r_type3(reloc->get_r_type3());
3298 new_reloc->put_r_type2(reloc->get_r_type2());
3299 new_reloc->put_r_type(reloc->get_r_type());
3302 // Write the r_addend field to a new reloc.
3303 static inline void
3304 put_r_addend(Reltype_write* to,
3305 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3306 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3308 // Return the size of the addend of the relocation (only used for SHT_REL).
3309 static unsigned int
3310 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3311 { return mips_get_size_for_reloc(r_type, obj); }
3314 template<int size, bool big_endian>
3315 class Target_mips : public Sized_target<size, big_endian>
3317 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3318 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3319 Reloc_section;
3320 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3321 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3322 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3323 Reltype;
3324 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3325 Relatype;
3327 public:
3328 Target_mips(const Target::Target_info* info = &mips_info)
3329 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3330 got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3331 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3332 mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3333 mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
3334 entry_symbol_is_compressed_(false), insn32_(false)
3336 this->add_machine_extensions();
3339 // The offset of $gp from the beginning of the .got section.
3340 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3342 // The maximum size of the GOT for it to be addressable using 16-bit
3343 // offsets from $gp.
3344 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3346 // Make a new symbol table entry for the Mips target.
3347 Sized_symbol<size>*
3348 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3349 { return new Mips_symbol<size>(); }
3351 // Process the relocations to determine unreferenced sections for
3352 // garbage collection.
3353 void
3354 gc_process_relocs(Symbol_table* symtab,
3355 Layout* layout,
3356 Sized_relobj_file<size, big_endian>* object,
3357 unsigned int data_shndx,
3358 unsigned int sh_type,
3359 const unsigned char* prelocs,
3360 size_t reloc_count,
3361 Output_section* output_section,
3362 bool needs_special_offset_handling,
3363 size_t local_symbol_count,
3364 const unsigned char* plocal_symbols);
3366 // Scan the relocations to look for symbol adjustments.
3367 void
3368 scan_relocs(Symbol_table* symtab,
3369 Layout* layout,
3370 Sized_relobj_file<size, big_endian>* object,
3371 unsigned int data_shndx,
3372 unsigned int sh_type,
3373 const unsigned char* prelocs,
3374 size_t reloc_count,
3375 Output_section* output_section,
3376 bool needs_special_offset_handling,
3377 size_t local_symbol_count,
3378 const unsigned char* plocal_symbols);
3380 // Finalize the sections.
3381 void
3382 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3384 // Relocate a section.
3385 void
3386 relocate_section(const Relocate_info<size, big_endian>*,
3387 unsigned int sh_type,
3388 const unsigned char* prelocs,
3389 size_t reloc_count,
3390 Output_section* output_section,
3391 bool needs_special_offset_handling,
3392 unsigned char* view,
3393 Mips_address view_address,
3394 section_size_type view_size,
3395 const Reloc_symbol_changes*);
3397 // Scan the relocs during a relocatable link.
3398 void
3399 scan_relocatable_relocs(Symbol_table* symtab,
3400 Layout* layout,
3401 Sized_relobj_file<size, big_endian>* object,
3402 unsigned int data_shndx,
3403 unsigned int sh_type,
3404 const unsigned char* prelocs,
3405 size_t reloc_count,
3406 Output_section* output_section,
3407 bool needs_special_offset_handling,
3408 size_t local_symbol_count,
3409 const unsigned char* plocal_symbols,
3410 Relocatable_relocs*);
3412 // Scan the relocs for --emit-relocs.
3413 void
3414 emit_relocs_scan(Symbol_table* symtab,
3415 Layout* layout,
3416 Sized_relobj_file<size, big_endian>* object,
3417 unsigned int data_shndx,
3418 unsigned int sh_type,
3419 const unsigned char* prelocs,
3420 size_t reloc_count,
3421 Output_section* output_section,
3422 bool needs_special_offset_handling,
3423 size_t local_symbol_count,
3424 const unsigned char* plocal_syms,
3425 Relocatable_relocs* rr);
3427 // Emit relocations for a section.
3428 void
3429 relocate_relocs(const Relocate_info<size, big_endian>*,
3430 unsigned int sh_type,
3431 const unsigned char* prelocs,
3432 size_t reloc_count,
3433 Output_section* output_section,
3434 typename elfcpp::Elf_types<size>::Elf_Off
3435 offset_in_output_section,
3436 unsigned char* view,
3437 Mips_address view_address,
3438 section_size_type view_size,
3439 unsigned char* reloc_view,
3440 section_size_type reloc_view_size);
3442 // Perform target-specific processing in a relocatable link. This is
3443 // only used if we use the relocation strategy RELOC_SPECIAL.
3444 void
3445 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3446 unsigned int sh_type,
3447 const unsigned char* preloc_in,
3448 size_t relnum,
3449 Output_section* output_section,
3450 typename elfcpp::Elf_types<size>::Elf_Off
3451 offset_in_output_section,
3452 unsigned char* view,
3453 Mips_address view_address,
3454 section_size_type view_size,
3455 unsigned char* preloc_out);
3457 // Return whether SYM is defined by the ABI.
3458 bool
3459 do_is_defined_by_abi(const Symbol* sym) const
3461 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3462 || (strcmp(sym->name(), "_gp_disp") == 0)
3463 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3466 // Return the number of entries in the GOT.
3467 unsigned int
3468 got_entry_count() const
3470 if (!this->has_got_section())
3471 return 0;
3472 return this->got_size() / (size/8);
3475 // Return the number of entries in the PLT.
3476 unsigned int
3477 plt_entry_count() const
3479 if (this->plt_ == NULL)
3480 return 0;
3481 return this->plt_->entry_count();
3484 // Return the offset of the first non-reserved PLT entry.
3485 unsigned int
3486 first_plt_entry_offset() const
3487 { return this->plt_->first_plt_entry_offset(); }
3489 // Return the size of each PLT entry.
3490 unsigned int
3491 plt_entry_size() const
3492 { return this->plt_->plt_entry_size(); }
3494 // Get the GOT section, creating it if necessary.
3495 Mips_output_data_got<size, big_endian>*
3496 got_section(Symbol_table*, Layout*);
3498 // Get the GOT section.
3499 Mips_output_data_got<size, big_endian>*
3500 got_section() const
3502 gold_assert(this->got_ != NULL);
3503 return this->got_;
3506 // Get the .MIPS.stubs section, creating it if necessary.
3507 Mips_output_data_mips_stubs<size, big_endian>*
3508 mips_stubs_section(Layout* layout);
3510 // Get the .MIPS.stubs section.
3511 Mips_output_data_mips_stubs<size, big_endian>*
3512 mips_stubs_section() const
3514 gold_assert(this->mips_stubs_ != NULL);
3515 return this->mips_stubs_;
3518 // Get the LA25 stub section, creating it if necessary.
3519 Mips_output_data_la25_stub<size, big_endian>*
3520 la25_stub_section(Layout*);
3522 // Get the LA25 stub section.
3523 Mips_output_data_la25_stub<size, big_endian>*
3524 la25_stub_section()
3526 gold_assert(this->la25_stub_ != NULL);
3527 return this->la25_stub_;
3530 // Get gp value. It has the value of .got + 0x7FF0.
3531 Mips_address
3532 gp_value() const
3534 if (this->gp_ != NULL)
3535 return this->gp_->value();
3536 return 0;
3539 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3540 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3541 Mips_address
3542 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3544 if (this->gp_ == NULL)
3545 return 0;
3547 bool multi_got = false;
3548 if (this->has_got_section())
3549 multi_got = this->got_section()->multi_got();
3550 if (!multi_got)
3551 return this->gp_->value();
3552 else
3553 return this->gp_->value() + this->got_section()->get_got_offset(object);
3556 // Get the dynamic reloc section, creating it if necessary.
3557 Reloc_section*
3558 rel_dyn_section(Layout*);
3560 bool
3561 do_has_custom_set_dynsym_indexes() const
3562 { return true; }
3564 // Don't emit input .reginfo/.MIPS.abiflags sections to
3565 // output .reginfo/.MIPS.abiflags.
3566 bool
3567 do_should_include_section(elfcpp::Elf_Word sh_type) const
3569 return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3570 && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3573 // Set the dynamic symbol indexes. INDEX is the index of the first
3574 // global dynamic symbol. Pointers to the symbols are stored into the
3575 // vector SYMS. The names are added to DYNPOOL. This returns an
3576 // updated dynamic symbol index.
3577 unsigned int
3578 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3579 std::vector<Symbol*>* syms, Stringpool* dynpool,
3580 Versions* versions, Symbol_table* symtab) const;
3582 // Remove .MIPS.stubs entry for a symbol.
3583 void
3584 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3586 if (this->mips_stubs_ != NULL)
3587 this->mips_stubs_->remove_entry(sym);
3590 // The value to write into got[1] for SVR4 targets, to identify it is
3591 // a GNU object. The dynamic linker can then use got[1] to store the
3592 // module pointer.
3593 uint64_t
3594 mips_elf_gnu_got1_mask()
3596 if (this->is_output_n64())
3597 return (uint64_t)1 << 63;
3598 else
3599 return 1 << 31;
3602 // Whether the output has microMIPS code. This is valid only after
3603 // merge_obj_e_flags() is called.
3604 bool
3605 is_output_micromips() const
3607 gold_assert(this->are_processor_specific_flags_set());
3608 return elfcpp::is_micromips(this->processor_specific_flags());
3611 // Whether the output uses N32 ABI. This is valid only after
3612 // merge_obj_e_flags() is called.
3613 bool
3614 is_output_n32() const
3616 gold_assert(this->are_processor_specific_flags_set());
3617 return elfcpp::abi_n32(this->processor_specific_flags());
3620 // Whether the output uses R6 ISA. This is valid only after
3621 // merge_obj_e_flags() is called.
3622 bool
3623 is_output_r6() const
3625 gold_assert(this->are_processor_specific_flags_set());
3626 return elfcpp::r6_isa(this->processor_specific_flags());
3629 // Whether the output uses N64 ABI.
3630 bool
3631 is_output_n64() const
3632 { return size == 64; }
3634 // Whether the output uses NEWABI. This is valid only after
3635 // merge_obj_e_flags() is called.
3636 bool
3637 is_output_newabi() const
3638 { return this->is_output_n32() || this->is_output_n64(); }
3640 // Whether we can only use 32-bit microMIPS instructions.
3641 bool
3642 use_32bit_micromips_instructions() const
3643 { return this->insn32_; }
3645 // Return the r_sym field from a relocation.
3646 unsigned int
3647 get_r_sym(const unsigned char* preloc) const
3649 // Since REL and RELA relocs share the same structure through
3650 // the r_info field, we can just use REL here.
3651 Reltype rel(preloc);
3652 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3653 get_r_sym(&rel);
3656 protected:
3657 // Return the value to use for a dynamic symbol which requires special
3658 // treatment. This is how we support equality comparisons of function
3659 // pointers across shared library boundaries, as described in the
3660 // processor specific ABI supplement.
3661 uint64_t
3662 do_dynsym_value(const Symbol* gsym) const;
3664 // Make an ELF object.
3665 Object*
3666 do_make_elf_object(const std::string&, Input_file*, off_t,
3667 const elfcpp::Ehdr<size, big_endian>& ehdr);
3669 Object*
3670 do_make_elf_object(const std::string&, Input_file*, off_t,
3671 const elfcpp::Ehdr<size, !big_endian>&)
3672 { gold_unreachable(); }
3674 // Make an output section.
3675 Output_section*
3676 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3677 elfcpp::Elf_Xword flags)
3679 if (type == elfcpp::SHT_MIPS_OPTIONS)
3680 return new Mips_output_section_options<size, big_endian>(name, type,
3681 flags, this);
3682 else
3683 return new Output_section(name, type, flags);
3686 // Adjust ELF file header.
3687 void
3688 do_adjust_elf_header(unsigned char* view, int len);
3690 // Get the custom dynamic tag value.
3691 unsigned int
3692 do_dynamic_tag_custom_value(elfcpp::DT) const;
3694 // Adjust the value written to the dynamic symbol table.
3695 virtual void
3696 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3698 elfcpp::Sym<size, big_endian> isym(view);
3699 elfcpp::Sym_write<size, big_endian> osym(view);
3700 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3702 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3703 // to treat compressed symbols like any other.
3704 Mips_address value = isym.get_st_value();
3705 if (mips_sym->is_mips16() && value != 0)
3707 if (!mips_sym->has_mips16_fn_stub())
3708 value |= 1;
3709 else
3711 // If we have a MIPS16 function with a stub, the dynamic symbol
3712 // must refer to the stub, since only the stub uses the standard
3713 // calling conventions. Stub contains MIPS32 code, so don't add +1
3714 // in this case.
3716 // There is a code which does this in the method
3717 // Target_mips::do_dynsym_value, but that code will only be
3718 // executed if the symbol is from dynobj.
3719 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3720 // table.
3722 Mips16_stub_section<size, big_endian>* fn_stub =
3723 mips_sym->template get_mips16_fn_stub<big_endian>();
3724 value = fn_stub->output_address();
3725 osym.put_st_size(fn_stub->section_size());
3728 osym.put_st_value(value);
3729 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3730 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3732 else if ((mips_sym->is_micromips()
3733 // Stubs are always microMIPS if there is any microMIPS code in
3734 // the output.
3735 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3736 && value != 0)
3738 osym.put_st_value(value | 1);
3739 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3740 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3744 private:
3745 // The class which scans relocations.
3746 class Scan
3748 public:
3749 Scan()
3752 static inline int
3753 get_reference_flags(unsigned int r_type);
3755 inline void
3756 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3757 Sized_relobj_file<size, big_endian>* object,
3758 unsigned int data_shndx,
3759 Output_section* output_section,
3760 const Reltype& reloc, unsigned int r_type,
3761 const elfcpp::Sym<size, big_endian>& lsym,
3762 bool is_discarded);
3764 inline void
3765 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3766 Sized_relobj_file<size, big_endian>* object,
3767 unsigned int data_shndx,
3768 Output_section* output_section,
3769 const Relatype& reloc, unsigned int r_type,
3770 const elfcpp::Sym<size, big_endian>& lsym,
3771 bool is_discarded);
3773 inline void
3774 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3775 Sized_relobj_file<size, big_endian>* object,
3776 unsigned int data_shndx,
3777 Output_section* output_section,
3778 const Relatype* rela,
3779 const Reltype* rel,
3780 unsigned int rel_type,
3781 unsigned int r_type,
3782 const elfcpp::Sym<size, big_endian>& lsym,
3783 bool is_discarded);
3785 inline void
3786 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3787 Sized_relobj_file<size, big_endian>* object,
3788 unsigned int data_shndx,
3789 Output_section* output_section,
3790 const Reltype& reloc, unsigned int r_type,
3791 Symbol* gsym);
3793 inline void
3794 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3795 Sized_relobj_file<size, big_endian>* object,
3796 unsigned int data_shndx,
3797 Output_section* output_section,
3798 const Relatype& reloc, unsigned int r_type,
3799 Symbol* gsym);
3801 inline void
3802 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3803 Sized_relobj_file<size, big_endian>* object,
3804 unsigned int data_shndx,
3805 Output_section* output_section,
3806 const Relatype* rela,
3807 const Reltype* rel,
3808 unsigned int rel_type,
3809 unsigned int r_type,
3810 Symbol* gsym);
3812 inline bool
3813 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3814 Target_mips*,
3815 Sized_relobj_file<size, big_endian>*,
3816 unsigned int,
3817 Output_section*,
3818 const Reltype&,
3819 unsigned int,
3820 const elfcpp::Sym<size, big_endian>&)
3821 { return false; }
3823 inline bool
3824 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3825 Target_mips*,
3826 Sized_relobj_file<size, big_endian>*,
3827 unsigned int,
3828 Output_section*,
3829 const Reltype&,
3830 unsigned int, Symbol*)
3831 { return false; }
3833 inline bool
3834 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3835 Target_mips*,
3836 Sized_relobj_file<size, big_endian>*,
3837 unsigned int,
3838 Output_section*,
3839 const Relatype&,
3840 unsigned int,
3841 const elfcpp::Sym<size, big_endian>&)
3842 { return false; }
3844 inline bool
3845 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3846 Target_mips*,
3847 Sized_relobj_file<size, big_endian>*,
3848 unsigned int,
3849 Output_section*,
3850 const Relatype&,
3851 unsigned int, Symbol*)
3852 { return false; }
3853 private:
3854 static void
3855 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3856 unsigned int r_type);
3858 static void
3859 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3860 unsigned int r_type, Symbol*);
3863 // The class which implements relocation.
3864 class Relocate
3866 public:
3867 Relocate()
3868 : calculated_value_(0), calculate_only_(false)
3871 ~Relocate()
3874 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3875 inline bool
3876 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3877 unsigned int r_type,
3878 Output_section* output_section,
3879 Target_mips* target);
3881 // Do a relocation. Return false if the caller should not issue
3882 // any warnings about this relocation.
3883 inline bool
3884 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3885 Target_mips*, Output_section*, size_t, const unsigned char*,
3886 const Sized_symbol<size>*, const Symbol_value<size>*,
3887 unsigned char*, Mips_address, section_size_type);
3889 private:
3890 // Result of the relocation.
3891 Valtype calculated_value_;
3892 // Whether we have to calculate relocation instead of applying it.
3893 bool calculate_only_;
3896 // This POD class holds the dynamic relocations that should be emitted instead
3897 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3898 // relocations if it turns out that the symbol does not have static
3899 // relocations.
3900 class Dyn_reloc
3902 public:
3903 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3904 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3905 Output_section* output_section, Mips_address r_offset)
3906 : sym_(sym), r_type_(r_type), relobj_(relobj),
3907 shndx_(shndx), output_section_(output_section),
3908 r_offset_(r_offset)
3911 // Emit this reloc if appropriate. This is called after we have
3912 // scanned all the relocations, so we know whether the symbol has
3913 // static relocations.
3914 void
3915 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3916 Symbol_table* symtab)
3918 if (!this->sym_->has_static_relocs())
3920 got->record_global_got_symbol(this->sym_, this->relobj_,
3921 this->r_type_, true, false);
3922 if (!symbol_references_local(this->sym_,
3923 this->sym_->should_add_dynsym_entry(symtab)))
3924 rel_dyn->add_global(this->sym_, this->r_type_,
3925 this->output_section_, this->relobj_,
3926 this->shndx_, this->r_offset_);
3927 else
3928 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3929 this->output_section_, this->relobj_,
3930 this->shndx_, this->r_offset_);
3934 private:
3935 Mips_symbol<size>* sym_;
3936 unsigned int r_type_;
3937 Mips_relobj<size, big_endian>* relobj_;
3938 unsigned int shndx_;
3939 Output_section* output_section_;
3940 Mips_address r_offset_;
3943 // Adjust TLS relocation type based on the options and whether this
3944 // is a local symbol.
3945 static tls::Tls_optimization
3946 optimize_tls_reloc(bool is_final, int r_type);
3948 // Return whether there is a GOT section.
3949 bool
3950 has_got_section() const
3951 { return this->got_ != NULL; }
3953 // Check whether the given ELF header flags describe a 32-bit binary.
3954 bool
3955 mips_32bit_flags(elfcpp::Elf_Word);
3957 enum Mips_mach {
3958 mach_mips3000 = 3000,
3959 mach_mips3900 = 3900,
3960 mach_mips4000 = 4000,
3961 mach_mips4010 = 4010,
3962 mach_mips4100 = 4100,
3963 mach_mips4111 = 4111,
3964 mach_mips4120 = 4120,
3965 mach_mips4300 = 4300,
3966 mach_mips4400 = 4400,
3967 mach_mips4600 = 4600,
3968 mach_mips4650 = 4650,
3969 mach_mips5000 = 5000,
3970 mach_mips5400 = 5400,
3971 mach_mips5500 = 5500,
3972 mach_mips5900 = 5900,
3973 mach_mips6000 = 6000,
3974 mach_mips7000 = 7000,
3975 mach_mips8000 = 8000,
3976 mach_mips9000 = 9000,
3977 mach_mips10000 = 10000,
3978 mach_mips12000 = 12000,
3979 mach_mips14000 = 14000,
3980 mach_mips16000 = 16000,
3981 mach_mips16 = 16,
3982 mach_mips5 = 5,
3983 mach_mips_loongson_2e = 3001,
3984 mach_mips_loongson_2f = 3002,
3985 mach_mips_gs464 = 3003,
3986 mach_mips_gs464e = 3004,
3987 mach_mips_gs264e = 3005,
3988 mach_mips_sb1 = 12310201, // octal 'SB', 01
3989 mach_mips_octeon = 6501,
3990 mach_mips_octeonp = 6601,
3991 mach_mips_octeon2 = 6502,
3992 mach_mips_octeon3 = 6503,
3993 mach_mips_xlr = 887682, // decimal 'XLR'
3994 mach_mipsisa32 = 32,
3995 mach_mipsisa32r2 = 33,
3996 mach_mipsisa32r3 = 34,
3997 mach_mipsisa32r5 = 36,
3998 mach_mipsisa32r6 = 37,
3999 mach_mipsisa64 = 64,
4000 mach_mipsisa64r2 = 65,
4001 mach_mipsisa64r3 = 66,
4002 mach_mipsisa64r5 = 68,
4003 mach_mipsisa64r6 = 69,
4004 mach_mips_micromips = 96
4007 // Return the MACH for a MIPS e_flags value.
4008 unsigned int
4009 elf_mips_mach(elfcpp::Elf_Word);
4011 // Return the MACH for each .MIPS.abiflags ISA Extension.
4012 unsigned int
4013 mips_isa_ext_mach(unsigned int);
4015 // Return the .MIPS.abiflags value representing each ISA Extension.
4016 unsigned int
4017 mips_isa_ext(unsigned int);
4019 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4020 void
4021 update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4022 Mips_abiflags<big_endian>*);
4024 // Infer the content of the ABI flags based on the elf header.
4025 void
4026 infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4028 // Create abiflags from elf header or from .MIPS.abiflags section.
4029 void
4030 create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4032 // Return the meaning of fp_abi, or "unknown" if not known.
4033 const char*
4034 fp_abi_string(int);
4036 // Select fp_abi.
4038 select_fp_abi(const std::string&, int, int);
4040 // Merge attributes from input object.
4041 void
4042 merge_obj_attributes(const std::string&, const Attributes_section_data*);
4044 // Merge abiflags from input object.
4045 void
4046 merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4048 // Check whether machine EXTENSION is an extension of machine BASE.
4049 bool
4050 mips_mach_extends(unsigned int, unsigned int);
4052 // Merge file header flags from input object.
4053 void
4054 merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4056 // Encode ISA level and revision as a single value.
4058 level_rev(unsigned char isa_level, unsigned char isa_rev) const
4059 { return (isa_level << 3) | isa_rev; }
4061 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4062 static inline bool
4063 jal_to_bal()
4064 { return false; }
4066 // True if we are linking for CPUs that are faster if JALR is converted to
4067 // BAL. This should be safe for all architectures. We enable this predicate
4068 // for all CPUs.
4069 static inline bool
4070 jalr_to_bal()
4071 { return true; }
4073 // True if we are linking for CPUs that are faster if JR is converted to B.
4074 // This should be safe for all architectures. We enable this predicate for
4075 // all CPUs.
4076 static inline bool
4077 jr_to_b()
4078 { return true; }
4080 // Return the size of the GOT section.
4081 section_size_type
4082 got_size() const
4084 gold_assert(this->got_ != NULL);
4085 return this->got_->data_size();
4088 // Create a PLT entry for a global symbol referenced by r_type relocation.
4089 void
4090 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4091 unsigned int r_type);
4093 // Get the PLT section.
4094 Mips_output_data_plt<size, big_endian>*
4095 plt_section() const
4097 gold_assert(this->plt_ != NULL);
4098 return this->plt_;
4101 // Get the GOT PLT section.
4102 const Mips_output_data_plt<size, big_endian>*
4103 got_plt_section() const
4105 gold_assert(this->got_plt_ != NULL);
4106 return this->got_plt_;
4109 // Copy a relocation against a global symbol.
4110 void
4111 copy_reloc(Symbol_table* symtab, Layout* layout,
4112 Sized_relobj_file<size, big_endian>* object,
4113 unsigned int shndx, Output_section* output_section,
4114 Symbol* sym, unsigned int r_type, Mips_address r_offset)
4116 this->copy_relocs_.copy_reloc(symtab, layout,
4117 symtab->get_sized_symbol<size>(sym),
4118 object, shndx, output_section,
4119 r_type, r_offset, 0,
4120 this->rel_dyn_section(layout));
4123 void
4124 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4125 Mips_relobj<size, big_endian>* relobj,
4126 unsigned int shndx, Output_section* output_section,
4127 Mips_address r_offset)
4129 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4130 output_section, r_offset));
4133 // Calculate value of _gp symbol.
4134 void
4135 set_gp(Layout*, Symbol_table*);
4137 const char*
4138 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4139 const char*
4140 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4142 // Adds entries that describe how machines relate to one another. The entries
4143 // are ordered topologically with MIPS I extensions listed last. First
4144 // element is extension, second element is base.
4145 void
4146 add_machine_extensions()
4148 // MIPS64r2 extensions.
4149 this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4150 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4151 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4152 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4153 this->add_extension(mach_mips_gs264e, mach_mips_gs464e);
4154 this->add_extension(mach_mips_gs464e, mach_mips_gs464);
4155 this->add_extension(mach_mips_gs464, mach_mipsisa64r2);
4157 // MIPS64 extensions.
4158 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4159 this->add_extension(mach_mips_sb1, mach_mipsisa64);
4160 this->add_extension(mach_mips_xlr, mach_mipsisa64);
4162 // MIPS V extensions.
4163 this->add_extension(mach_mipsisa64, mach_mips5);
4165 // R10000 extensions.
4166 this->add_extension(mach_mips12000, mach_mips10000);
4167 this->add_extension(mach_mips14000, mach_mips10000);
4168 this->add_extension(mach_mips16000, mach_mips10000);
4170 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
4171 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
4172 // better to allow vr5400 and vr5500 code to be merged anyway, since
4173 // many libraries will just use the core ISA. Perhaps we could add
4174 // some sort of ASE flag if this ever proves a problem.
4175 this->add_extension(mach_mips5500, mach_mips5400);
4176 this->add_extension(mach_mips5400, mach_mips5000);
4178 // MIPS IV extensions.
4179 this->add_extension(mach_mips5, mach_mips8000);
4180 this->add_extension(mach_mips10000, mach_mips8000);
4181 this->add_extension(mach_mips5000, mach_mips8000);
4182 this->add_extension(mach_mips7000, mach_mips8000);
4183 this->add_extension(mach_mips9000, mach_mips8000);
4185 // VR4100 extensions.
4186 this->add_extension(mach_mips4120, mach_mips4100);
4187 this->add_extension(mach_mips4111, mach_mips4100);
4189 // MIPS III extensions.
4190 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4191 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4192 this->add_extension(mach_mips8000, mach_mips4000);
4193 this->add_extension(mach_mips4650, mach_mips4000);
4194 this->add_extension(mach_mips4600, mach_mips4000);
4195 this->add_extension(mach_mips4400, mach_mips4000);
4196 this->add_extension(mach_mips4300, mach_mips4000);
4197 this->add_extension(mach_mips4100, mach_mips4000);
4198 this->add_extension(mach_mips4010, mach_mips4000);
4199 this->add_extension(mach_mips5900, mach_mips4000);
4201 // MIPS32 extensions.
4202 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4204 // MIPS II extensions.
4205 this->add_extension(mach_mips4000, mach_mips6000);
4206 this->add_extension(mach_mipsisa32, mach_mips6000);
4208 // MIPS I extensions.
4209 this->add_extension(mach_mips6000, mach_mips3000);
4210 this->add_extension(mach_mips3900, mach_mips3000);
4213 // Add value to MIPS extenstions.
4214 void
4215 add_extension(unsigned int base, unsigned int extension)
4217 std::pair<unsigned int, unsigned int> ext(base, extension);
4218 this->mips_mach_extensions_.push_back(ext);
4221 // Return the number of entries in the .dynsym section.
4222 unsigned int get_dt_mips_symtabno() const
4224 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4225 / elfcpp::Elf_sizes<size>::sym_size));
4226 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4229 // Information about this specific target which we pass to the
4230 // general Target structure.
4231 static const Target::Target_info mips_info;
4232 // The GOT section.
4233 Mips_output_data_got<size, big_endian>* got_;
4234 // gp symbol. It has the value of .got + 0x7FF0.
4235 Sized_symbol<size>* gp_;
4236 // The PLT section.
4237 Mips_output_data_plt<size, big_endian>* plt_;
4238 // The GOT PLT section.
4239 Output_data_space* got_plt_;
4240 // The dynamic reloc section.
4241 Reloc_section* rel_dyn_;
4242 // The .rld_map section.
4243 Output_data_zero_fill* rld_map_;
4244 // Relocs saved to avoid a COPY reloc.
4245 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4247 // A list of dyn relocs to be saved.
4248 std::vector<Dyn_reloc> dyn_relocs_;
4250 // The LA25 stub section.
4251 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4252 // Architecture extensions.
4253 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4254 // .MIPS.stubs
4255 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4257 // Attributes section data in output.
4258 Attributes_section_data* attributes_section_data_;
4259 // .MIPS.abiflags section data in output.
4260 Mips_abiflags<big_endian>* abiflags_;
4262 unsigned int mach_;
4263 Layout* layout_;
4265 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4267 // Whether there is an input .MIPS.abiflags section.
4268 bool has_abiflags_section_;
4270 // Whether the entry symbol is mips16 or micromips.
4271 bool entry_symbol_is_compressed_;
4273 // Whether we can use only 32-bit microMIPS instructions.
4274 // TODO(sasa): This should be a linker option.
4275 bool insn32_;
4278 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4279 // It records high part of the relocation pair.
4281 template<int size, bool big_endian>
4282 struct reloc_high
4284 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4286 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4287 const Symbol_value<size>* _psymval, Mips_address _addend,
4288 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4289 Mips_address _address = 0, bool _gp_disp = false)
4290 : view(_view), object(_object), psymval(_psymval), addend(_addend),
4291 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4292 address(_address), gp_disp(_gp_disp)
4295 unsigned char* view;
4296 const Mips_relobj<size, big_endian>* object;
4297 const Symbol_value<size>* psymval;
4298 Mips_address addend;
4299 unsigned int r_type;
4300 unsigned int r_sym;
4301 bool extract_addend;
4302 Mips_address address;
4303 bool gp_disp;
4306 template<int size, bool big_endian>
4307 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4309 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4310 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4311 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4312 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4313 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4315 public:
4316 typedef enum
4318 STATUS_OKAY, // No error during relocation.
4319 STATUS_OVERFLOW, // Relocation overflow.
4320 STATUS_BAD_RELOC, // Relocation cannot be applied.
4321 STATUS_PCREL_UNALIGNED // Unaligned PC-relative relocation.
4322 } Status;
4324 private:
4325 typedef Relocate_functions<size, big_endian> Base;
4326 typedef Mips_relocate_functions<size, big_endian> This;
4328 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4329 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4330 static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
4332 template<int valsize>
4333 static inline typename This::Status
4334 check_overflow(Valtype value)
4336 if (size == 32)
4337 return (Bits<valsize>::has_overflow32(value)
4338 ? This::STATUS_OVERFLOW
4339 : This::STATUS_OKAY);
4341 return (Bits<valsize>::has_overflow(value)
4342 ? This::STATUS_OVERFLOW
4343 : This::STATUS_OKAY);
4346 static inline bool
4347 should_shuffle_micromips_reloc(unsigned int r_type)
4349 return (micromips_reloc(r_type)
4350 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4351 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4354 public:
4355 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4356 // Most mips16 instructions are 16 bits, but these instructions
4357 // are 32 bits.
4359 // The format of these instructions is:
4361 // +--------------+--------------------------------+
4362 // | JALX | X| Imm 20:16 | Imm 25:21 |
4363 // +--------------+--------------------------------+
4364 // | Immediate 15:0 |
4365 // +-----------------------------------------------+
4367 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4368 // Note that the immediate value in the first word is swapped.
4370 // When producing a relocatable object file, R_MIPS16_26 is
4371 // handled mostly like R_MIPS_26. In particular, the addend is
4372 // stored as a straight 26-bit value in a 32-bit instruction.
4373 // (gas makes life simpler for itself by never adjusting a
4374 // R_MIPS16_26 reloc to be against a section, so the addend is
4375 // always zero). However, the 32 bit instruction is stored as 2
4376 // 16-bit values, rather than a single 32-bit value. In a
4377 // big-endian file, the result is the same; in a little-endian
4378 // file, the two 16-bit halves of the 32 bit value are swapped.
4379 // This is so that a disassembler can recognize the jal
4380 // instruction.
4382 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4383 // instruction stored as two 16-bit values. The addend A is the
4384 // contents of the targ26 field. The calculation is the same as
4385 // R_MIPS_26. When storing the calculated value, reorder the
4386 // immediate value as shown above, and don't forget to store the
4387 // value as two 16-bit values.
4389 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4390 // defined as
4392 // big-endian:
4393 // +--------+----------------------+
4394 // | | |
4395 // | | targ26-16 |
4396 // |31 26|25 0|
4397 // +--------+----------------------+
4399 // little-endian:
4400 // +----------+------+-------------+
4401 // | | | |
4402 // | sub1 | | sub2 |
4403 // |0 9|10 15|16 31|
4404 // +----------+--------------------+
4405 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4406 // ((sub1 << 16) | sub2)).
4408 // When producing a relocatable object file, the calculation is
4409 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4410 // When producing a fully linked file, the calculation is
4411 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4412 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4414 // The table below lists the other MIPS16 instruction relocations.
4415 // Each one is calculated in the same way as the non-MIPS16 relocation
4416 // given on the right, but using the extended MIPS16 layout of 16-bit
4417 // immediate fields:
4419 // R_MIPS16_GPREL R_MIPS_GPREL16
4420 // R_MIPS16_GOT16 R_MIPS_GOT16
4421 // R_MIPS16_CALL16 R_MIPS_CALL16
4422 // R_MIPS16_HI16 R_MIPS_HI16
4423 // R_MIPS16_LO16 R_MIPS_LO16
4425 // A typical instruction will have a format like this:
4427 // +--------------+--------------------------------+
4428 // | EXTEND | Imm 10:5 | Imm 15:11 |
4429 // +--------------+--------------------------------+
4430 // | Major | rx | ry | Imm 4:0 |
4431 // +--------------+--------------------------------+
4433 // EXTEND is the five bit value 11110. Major is the instruction
4434 // opcode.
4436 // All we need to do here is shuffle the bits appropriately.
4437 // As above, the two 16-bit halves must be swapped on a
4438 // little-endian system.
4440 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4441 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4442 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4444 static void
4445 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4446 bool jal_shuffle)
4448 if (!mips16_reloc(r_type)
4449 && !should_shuffle_micromips_reloc(r_type))
4450 return;
4452 // Pick up the first and second halfwords of the instruction.
4453 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4454 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4455 Valtype32 val;
4457 if (micromips_reloc(r_type)
4458 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4459 val = first << 16 | second;
4460 else if (r_type != elfcpp::R_MIPS16_26)
4461 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4462 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4463 else
4464 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4465 | ((first & 0x1f) << 21) | second);
4467 elfcpp::Swap<32, big_endian>::writeval(view, val);
4470 static void
4471 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4473 if (!mips16_reloc(r_type)
4474 && !should_shuffle_micromips_reloc(r_type))
4475 return;
4477 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4478 Valtype16 first, second;
4480 if (micromips_reloc(r_type)
4481 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4483 second = val & 0xffff;
4484 first = val >> 16;
4486 else if (r_type != elfcpp::R_MIPS16_26)
4488 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4489 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4491 else
4493 second = val & 0xffff;
4494 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4495 | ((val >> 21) & 0x1f);
4498 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4499 elfcpp::Swap<16, big_endian>::writeval(view, first);
4502 // R_MIPS_16: S + sign-extend(A)
4503 static inline typename This::Status
4504 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4505 const Symbol_value<size>* psymval, Mips_address addend_a,
4506 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4508 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4509 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4511 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4512 : addend_a);
4514 Valtype x = psymval->value(object, addend);
4515 val = Bits<16>::bit_select32(val, x, 0xffffU);
4517 if (calculate_only)
4519 *calculated_value = x;
4520 return This::STATUS_OKAY;
4522 else
4523 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4525 return check_overflow<16>(x);
4528 // R_MIPS_32: S + A
4529 static inline typename This::Status
4530 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4531 const Symbol_value<size>* psymval, Mips_address addend_a,
4532 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4534 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4535 Valtype addend = (extract_addend
4536 ? elfcpp::Swap<32, big_endian>::readval(wv)
4537 : addend_a);
4538 Valtype x = psymval->value(object, addend);
4540 if (calculate_only)
4541 *calculated_value = x;
4542 else
4543 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4545 return This::STATUS_OKAY;
4548 // R_MIPS_JALR, R_MICROMIPS_JALR
4549 static inline typename This::Status
4550 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4551 const Symbol_value<size>* psymval, Mips_address address,
4552 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4553 unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4554 bool calculate_only, Valtype* calculated_value)
4556 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4557 Valtype addend = extract_addend ? 0 : addend_a;
4558 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4560 // Try converting J(AL)R to B(AL), if the target is in range.
4561 if (r_type == elfcpp::R_MIPS_JALR
4562 && !cross_mode_jump
4563 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4564 || (jr_to_b && val == 0x03200008))) // jr t9
4566 int offset = psymval->value(object, addend) - (address + 4);
4567 if (!Bits<18>::has_overflow32(offset))
4569 if (val == 0x03200008) // jr t9
4570 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4571 else
4572 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4576 if (calculate_only)
4577 *calculated_value = val;
4578 else
4579 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4581 return This::STATUS_OKAY;
4584 // R_MIPS_PC32: S + A - P
4585 static inline typename This::Status
4586 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4587 const Symbol_value<size>* psymval, Mips_address address,
4588 Mips_address addend_a, bool extract_addend, bool calculate_only,
4589 Valtype* calculated_value)
4591 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4592 Valtype addend = (extract_addend
4593 ? elfcpp::Swap<32, big_endian>::readval(wv)
4594 : addend_a);
4595 Valtype x = psymval->value(object, addend) - address;
4597 if (calculate_only)
4598 *calculated_value = x;
4599 else
4600 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4602 return This::STATUS_OKAY;
4605 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4606 static inline typename This::Status
4607 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4608 const Symbol_value<size>* psymval, Mips_address address,
4609 bool local, Mips_address addend_a, bool extract_addend,
4610 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4611 bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4613 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4614 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4616 Valtype addend;
4617 if (extract_addend)
4619 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4620 addend = (val & 0x03ffffff) << 1;
4621 else
4622 addend = (val & 0x03ffffff) << 2;
4624 else
4625 addend = addend_a;
4627 // Make sure the target of JALX is word-aligned. Bit 0 must be
4628 // the correct ISA mode selector and bit 1 must be 0.
4629 if (!calculate_only && cross_mode_jump
4630 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4632 gold_warning(_("JALX to a non-word-aligned address"));
4633 return This::STATUS_BAD_RELOC;
4636 // Shift is 2, unusually, for microMIPS JALX.
4637 unsigned int shift =
4638 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4640 Valtype x;
4641 if (local)
4642 x = addend | ((address + 4) & (0xfc000000 << shift));
4643 else
4645 if (shift == 1)
4646 x = Bits<27>::sign_extend32(addend);
4647 else
4648 x = Bits<28>::sign_extend32(addend);
4650 x = psymval->value(object, x) >> shift;
4652 if (!calculate_only && !local && !gsym->is_weak_undefined()
4653 && ((x >> 26) != ((address + 4) >> (26 + shift))))
4654 return This::STATUS_OVERFLOW;
4656 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4658 // If required, turn JAL into JALX.
4659 if (cross_mode_jump)
4661 bool ok;
4662 Valtype32 opcode = val >> 26;
4663 Valtype32 jalx_opcode;
4665 // Check to see if the opcode is already JAL or JALX.
4666 if (r_type == elfcpp::R_MIPS16_26)
4668 ok = (opcode == 0x6) || (opcode == 0x7);
4669 jalx_opcode = 0x7;
4671 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4673 ok = (opcode == 0x3d) || (opcode == 0x3c);
4674 jalx_opcode = 0x3c;
4676 else
4678 ok = (opcode == 0x3) || (opcode == 0x1d);
4679 jalx_opcode = 0x1d;
4682 // If the opcode is not JAL or JALX, there's a problem. We cannot
4683 // convert J or JALS to JALX.
4684 if (!calculate_only && !ok)
4686 gold_error(_("Unsupported jump between ISA modes; consider "
4687 "recompiling with interlinking enabled."));
4688 return This::STATUS_BAD_RELOC;
4691 // Make this the JALX opcode.
4692 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4695 // Try converting JAL to BAL, if the target is in range.
4696 if (!parameters->options().relocatable()
4697 && !cross_mode_jump
4698 && ((jal_to_bal
4699 && r_type == elfcpp::R_MIPS_26
4700 && (val >> 26) == 0x3))) // jal addr
4702 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4703 int offset = dest - (address + 4);
4704 if (!Bits<18>::has_overflow32(offset))
4706 if (val == 0x03200008) // jr t9
4707 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4708 else
4709 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4713 if (calculate_only)
4714 *calculated_value = val;
4715 else
4716 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4718 return This::STATUS_OKAY;
4721 // R_MIPS_PC16
4722 static inline typename This::Status
4723 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4724 const Symbol_value<size>* psymval, Mips_address address,
4725 Mips_address addend_a, bool extract_addend, bool calculate_only,
4726 Valtype* calculated_value)
4728 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4729 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4731 Valtype addend = (extract_addend
4732 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4733 : addend_a);
4735 Valtype x = psymval->value(object, addend) - address;
4736 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4738 if (calculate_only)
4740 *calculated_value = x >> 2;
4741 return This::STATUS_OKAY;
4743 else
4744 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4746 if (psymval->value(object, addend) & 3)
4747 return This::STATUS_PCREL_UNALIGNED;
4749 return check_overflow<18>(x);
4752 // R_MIPS_PC21_S2
4753 static inline typename This::Status
4754 relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4755 const Symbol_value<size>* psymval, Mips_address address,
4756 Mips_address addend_a, bool extract_addend, bool calculate_only,
4757 Valtype* calculated_value)
4759 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4760 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4762 Valtype addend = (extract_addend
4763 ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4764 : addend_a);
4766 Valtype x = psymval->value(object, addend) - address;
4767 val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4769 if (calculate_only)
4771 *calculated_value = x >> 2;
4772 return This::STATUS_OKAY;
4774 else
4775 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4777 if (psymval->value(object, addend) & 3)
4778 return This::STATUS_PCREL_UNALIGNED;
4780 return check_overflow<23>(x);
4783 // R_MIPS_PC26_S2
4784 static inline typename This::Status
4785 relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4786 const Symbol_value<size>* psymval, Mips_address address,
4787 Mips_address addend_a, bool extract_addend, bool calculate_only,
4788 Valtype* calculated_value)
4790 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4791 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4793 Valtype addend = (extract_addend
4794 ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4795 : addend_a);
4797 Valtype x = psymval->value(object, addend) - address;
4798 val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4800 if (calculate_only)
4802 *calculated_value = x >> 2;
4803 return This::STATUS_OKAY;
4805 else
4806 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4808 if (psymval->value(object, addend) & 3)
4809 return This::STATUS_PCREL_UNALIGNED;
4811 return check_overflow<28>(x);
4814 // R_MIPS_PC18_S3
4815 static inline typename This::Status
4816 relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4817 const Symbol_value<size>* psymval, Mips_address address,
4818 Mips_address addend_a, bool extract_addend, bool calculate_only,
4819 Valtype* calculated_value)
4821 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4822 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4824 Valtype addend = (extract_addend
4825 ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4826 : addend_a);
4828 Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4829 val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4831 if (calculate_only)
4833 *calculated_value = x >> 3;
4834 return This::STATUS_OKAY;
4836 else
4837 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4839 if (psymval->value(object, addend) & 7)
4840 return This::STATUS_PCREL_UNALIGNED;
4842 return check_overflow<21>(x);
4845 // R_MIPS_PC19_S2
4846 static inline typename This::Status
4847 relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4848 const Symbol_value<size>* psymval, Mips_address address,
4849 Mips_address addend_a, bool extract_addend, bool calculate_only,
4850 Valtype* calculated_value)
4852 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4853 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4855 Valtype addend = (extract_addend
4856 ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4857 : addend_a);
4859 Valtype x = psymval->value(object, addend) - address;
4860 val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4862 if (calculate_only)
4864 *calculated_value = x >> 2;
4865 return This::STATUS_OKAY;
4867 else
4868 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4870 if (psymval->value(object, addend) & 3)
4871 return This::STATUS_PCREL_UNALIGNED;
4873 return check_overflow<21>(x);
4876 // R_MIPS_PCHI16
4877 static inline typename This::Status
4878 relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4879 const Symbol_value<size>* psymval, Mips_address addend,
4880 Mips_address address, unsigned int r_sym, bool extract_addend)
4882 // Record the relocation. It will be resolved when we find pclo16 part.
4883 pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4884 addend, 0, r_sym, extract_addend, address));
4885 return This::STATUS_OKAY;
4888 // R_MIPS_PCHI16
4889 static inline typename This::Status
4890 do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4891 const Symbol_value<size>* psymval, Mips_address addend_hi,
4892 Mips_address address, bool extract_addend, Valtype32 addend_lo,
4893 bool calculate_only, Valtype* calculated_value)
4895 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4896 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4898 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4899 : addend_hi);
4901 Valtype value = psymval->value(object, addend) - address;
4902 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4903 val = Bits<32>::bit_select32(val, x, 0xffff);
4905 if (calculate_only)
4906 *calculated_value = x;
4907 else
4908 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4910 return This::STATUS_OKAY;
4913 // R_MIPS_PCLO16
4914 static inline typename This::Status
4915 relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4916 const Symbol_value<size>* psymval, Mips_address addend_a,
4917 bool extract_addend, Mips_address address, unsigned int r_sym,
4918 unsigned int rel_type, bool calculate_only,
4919 Valtype* calculated_value)
4921 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4922 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4924 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4925 : addend_a);
4927 if (rel_type == elfcpp::SHT_REL)
4929 // Resolve pending R_MIPS_PCHI16 relocations.
4930 typename std::list<reloc_high<size, big_endian> >::iterator it =
4931 pchi16_relocs.begin();
4932 while (it != pchi16_relocs.end())
4934 reloc_high<size, big_endian> pchi16 = *it;
4935 if (pchi16.r_sym == r_sym)
4937 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4938 pchi16.addend, pchi16.address,
4939 pchi16.extract_addend, addend, calculate_only,
4940 calculated_value);
4941 it = pchi16_relocs.erase(it);
4943 else
4944 ++it;
4948 // Resolve R_MIPS_PCLO16 relocation.
4949 Valtype x = psymval->value(object, addend) - address;
4950 val = Bits<32>::bit_select32(val, x, 0xffff);
4952 if (calculate_only)
4953 *calculated_value = x;
4954 else
4955 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4957 return This::STATUS_OKAY;
4960 // R_MICROMIPS_PC7_S1
4961 static inline typename This::Status
4962 relmicromips_pc7_s1(unsigned char* view,
4963 const Mips_relobj<size, big_endian>* object,
4964 const Symbol_value<size>* psymval, Mips_address address,
4965 Mips_address addend_a, bool extract_addend,
4966 bool calculate_only, Valtype* calculated_value)
4968 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4969 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4971 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4972 : addend_a;
4974 Valtype x = psymval->value(object, addend) - address;
4975 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4977 if (calculate_only)
4979 *calculated_value = x >> 1;
4980 return This::STATUS_OKAY;
4982 else
4983 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4985 return check_overflow<8>(x);
4988 // R_MICROMIPS_PC10_S1
4989 static inline typename This::Status
4990 relmicromips_pc10_s1(unsigned char* view,
4991 const Mips_relobj<size, big_endian>* object,
4992 const Symbol_value<size>* psymval, Mips_address address,
4993 Mips_address addend_a, bool extract_addend,
4994 bool calculate_only, Valtype* calculated_value)
4996 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4997 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4999 Valtype addend = (extract_addend
5000 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
5001 : addend_a);
5003 Valtype x = psymval->value(object, addend) - address;
5004 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
5006 if (calculate_only)
5008 *calculated_value = x >> 1;
5009 return This::STATUS_OKAY;
5011 else
5012 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5014 return check_overflow<11>(x);
5017 // R_MICROMIPS_PC16_S1
5018 static inline typename This::Status
5019 relmicromips_pc16_s1(unsigned char* view,
5020 const Mips_relobj<size, big_endian>* object,
5021 const Symbol_value<size>* psymval, Mips_address address,
5022 Mips_address addend_a, bool extract_addend,
5023 bool calculate_only, Valtype* calculated_value)
5025 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5026 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5028 Valtype addend = (extract_addend
5029 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
5030 : addend_a);
5032 Valtype x = psymval->value(object, addend) - address;
5033 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
5035 if (calculate_only)
5037 *calculated_value = x >> 1;
5038 return This::STATUS_OKAY;
5040 else
5041 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5043 return check_overflow<17>(x);
5046 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5047 static inline typename This::Status
5048 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5049 const Symbol_value<size>* psymval, Mips_address addend,
5050 Mips_address address, bool gp_disp, unsigned int r_type,
5051 unsigned int r_sym, bool extract_addend)
5053 // Record the relocation. It will be resolved when we find lo16 part.
5054 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5055 addend, r_type, r_sym, extract_addend, address,
5056 gp_disp));
5057 return This::STATUS_OKAY;
5060 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5061 static inline typename This::Status
5062 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5063 const Symbol_value<size>* psymval, Mips_address addend_hi,
5064 Mips_address address, bool is_gp_disp, unsigned int r_type,
5065 bool extract_addend, Valtype32 addend_lo,
5066 Target_mips<size, big_endian>* target, bool calculate_only,
5067 Valtype* calculated_value)
5069 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5070 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5072 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5073 : addend_hi);
5075 Valtype32 value;
5076 if (!is_gp_disp)
5077 value = psymval->value(object, addend);
5078 else
5080 // For MIPS16 ABI code we generate this sequence
5081 // 0: li $v0,%hi(_gp_disp)
5082 // 4: addiupc $v1,%lo(_gp_disp)
5083 // 8: sll $v0,16
5084 // 12: addu $v0,$v1
5085 // 14: move $gp,$v0
5086 // So the offsets of hi and lo relocs are the same, but the
5087 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5088 // ADDIUPC clears the low two bits of the instruction address,
5089 // so the base is ($t9 + 4) & ~3.
5090 Valtype32 gp_disp;
5091 if (r_type == elfcpp::R_MIPS16_HI16)
5092 gp_disp = (target->adjusted_gp_value(object)
5093 - ((address + 4) & ~0x3));
5094 // The microMIPS .cpload sequence uses the same assembly
5095 // instructions as the traditional psABI version, but the
5096 // incoming $t9 has the low bit set.
5097 else if (r_type == elfcpp::R_MICROMIPS_HI16)
5098 gp_disp = target->adjusted_gp_value(object) - address - 1;
5099 else
5100 gp_disp = target->adjusted_gp_value(object) - address;
5101 value = gp_disp + addend;
5103 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
5104 val = Bits<32>::bit_select32(val, x, 0xffff);
5106 if (calculate_only)
5108 *calculated_value = x;
5109 return This::STATUS_OKAY;
5111 else
5112 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5114 return (is_gp_disp ? check_overflow<16>(x)
5115 : This::STATUS_OKAY);
5118 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5119 static inline typename This::Status
5120 relgot16_local(unsigned char* view,
5121 const Mips_relobj<size, big_endian>* object,
5122 const Symbol_value<size>* psymval, Mips_address addend_a,
5123 bool extract_addend, unsigned int r_type, unsigned int r_sym)
5125 // Record the relocation. It will be resolved when we find lo16 part.
5126 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5127 addend_a, r_type, r_sym, extract_addend));
5128 return This::STATUS_OKAY;
5131 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5132 static inline typename This::Status
5133 do_relgot16_local(unsigned char* view,
5134 const Mips_relobj<size, big_endian>* object,
5135 const Symbol_value<size>* psymval, Mips_address addend_hi,
5136 bool extract_addend, Valtype32 addend_lo,
5137 Target_mips<size, big_endian>* target, bool calculate_only,
5138 Valtype* calculated_value)
5140 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5141 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5143 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5144 : addend_hi);
5146 // Find GOT page entry.
5147 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5148 & 0xffff;
5149 value <<= 16;
5150 unsigned int got_offset =
5151 target->got_section()->get_got_page_offset(value, object);
5153 // Resolve the relocation.
5154 Valtype x = target->got_section()->gp_offset(got_offset, object);
5155 val = Bits<32>::bit_select32(val, x, 0xffff);
5157 if (calculate_only)
5159 *calculated_value = x;
5160 return This::STATUS_OKAY;
5162 else
5163 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5165 return check_overflow<16>(x);
5168 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5169 static inline typename This::Status
5170 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
5171 const Mips_relobj<size, big_endian>* object,
5172 const Symbol_value<size>* psymval, Mips_address addend_a,
5173 bool extract_addend, Mips_address address, bool is_gp_disp,
5174 unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
5175 bool calculate_only, Valtype* calculated_value)
5177 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5178 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5180 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5181 : addend_a);
5183 if (rel_type == elfcpp::SHT_REL)
5185 typename This::Status reloc_status = This::STATUS_OKAY;
5186 // Resolve pending R_MIPS_HI16 relocations.
5187 typename std::list<reloc_high<size, big_endian> >::iterator it =
5188 hi16_relocs.begin();
5189 while (it != hi16_relocs.end())
5191 reloc_high<size, big_endian> hi16 = *it;
5192 if (hi16.r_sym == r_sym
5193 && is_matching_lo16_reloc(hi16.r_type, r_type))
5195 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
5196 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
5197 hi16.addend, hi16.address, hi16.gp_disp,
5198 hi16.r_type, hi16.extract_addend, addend,
5199 target, calculate_only, calculated_value);
5200 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
5201 if (reloc_status == This::STATUS_OVERFLOW)
5202 return This::STATUS_OVERFLOW;
5203 it = hi16_relocs.erase(it);
5205 else
5206 ++it;
5209 // Resolve pending local R_MIPS_GOT16 relocations.
5210 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
5211 got16_relocs.begin();
5212 while (it2 != got16_relocs.end())
5214 reloc_high<size, big_endian> got16 = *it2;
5215 if (got16.r_sym == r_sym
5216 && is_matching_lo16_reloc(got16.r_type, r_type))
5218 mips_reloc_unshuffle(got16.view, got16.r_type, false);
5220 reloc_status = do_relgot16_local(got16.view, got16.object,
5221 got16.psymval, got16.addend,
5222 got16.extract_addend, addend, target,
5223 calculate_only, calculated_value);
5225 mips_reloc_shuffle(got16.view, got16.r_type, false);
5226 if (reloc_status == This::STATUS_OVERFLOW)
5227 return This::STATUS_OVERFLOW;
5228 it2 = got16_relocs.erase(it2);
5230 else
5231 ++it2;
5235 // Resolve R_MIPS_LO16 relocation.
5236 Valtype x;
5237 if (!is_gp_disp)
5238 x = psymval->value(object, addend);
5239 else
5241 // See the comment for R_MIPS16_HI16 above for the reason
5242 // for this conditional.
5243 Valtype32 gp_disp;
5244 if (r_type == elfcpp::R_MIPS16_LO16)
5245 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
5246 else if (r_type == elfcpp::R_MICROMIPS_LO16
5247 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
5248 gp_disp = target->adjusted_gp_value(object) - address + 3;
5249 else
5250 gp_disp = target->adjusted_gp_value(object) - address + 4;
5251 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5252 // for overflow. Relocations against _gp_disp are normally
5253 // generated from the .cpload pseudo-op. It generates code
5254 // that normally looks like this:
5256 // lui $gp,%hi(_gp_disp)
5257 // addiu $gp,$gp,%lo(_gp_disp)
5258 // addu $gp,$gp,$t9
5260 // Here $t9 holds the address of the function being called,
5261 // as required by the MIPS ELF ABI. The R_MIPS_LO16
5262 // relocation can easily overflow in this situation, but the
5263 // R_MIPS_HI16 relocation will handle the overflow.
5264 // Therefore, we consider this a bug in the MIPS ABI, and do
5265 // not check for overflow here.
5266 x = gp_disp + addend;
5268 val = Bits<32>::bit_select32(val, x, 0xffff);
5270 if (calculate_only)
5271 *calculated_value = x;
5272 else
5273 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5275 return This::STATUS_OKAY;
5278 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5279 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5280 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5281 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5282 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5283 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5284 static inline typename This::Status
5285 relgot(unsigned char* view, int gp_offset, bool calculate_only,
5286 Valtype* calculated_value)
5288 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5289 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5290 Valtype x = gp_offset;
5291 val = Bits<32>::bit_select32(val, x, 0xffff);
5293 if (calculate_only)
5295 *calculated_value = x;
5296 return This::STATUS_OKAY;
5298 else
5299 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5301 return check_overflow<16>(x);
5304 // R_MIPS_EH
5305 static inline typename This::Status
5306 releh(unsigned char* view, int gp_offset, bool calculate_only,
5307 Valtype* calculated_value)
5309 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5310 Valtype x = gp_offset;
5312 if (calculate_only)
5314 *calculated_value = x;
5315 return This::STATUS_OKAY;
5317 else
5318 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5320 return check_overflow<32>(x);
5323 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5324 static inline typename This::Status
5325 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5326 const Mips_relobj<size, big_endian>* object,
5327 const Symbol_value<size>* psymval, Mips_address addend_a,
5328 bool extract_addend, bool calculate_only,
5329 Valtype* calculated_value)
5331 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5332 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5333 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5335 // Find a GOT page entry that points to within 32KB of symbol + addend.
5336 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5337 unsigned int got_offset =
5338 target->got_section()->get_got_page_offset(value, object);
5340 Valtype x = target->got_section()->gp_offset(got_offset, object);
5341 val = Bits<32>::bit_select32(val, x, 0xffff);
5343 if (calculate_only)
5345 *calculated_value = x;
5346 return This::STATUS_OKAY;
5348 else
5349 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5351 return check_overflow<16>(x);
5354 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5355 static inline typename This::Status
5356 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5357 const Mips_relobj<size, big_endian>* object,
5358 const Symbol_value<size>* psymval, Mips_address addend_a,
5359 bool extract_addend, bool local, bool calculate_only,
5360 Valtype* calculated_value)
5362 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5363 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5364 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5366 // For a local symbol, find a GOT page entry that points to within 32KB of
5367 // symbol + addend. Relocation value is the offset of the GOT page entry's
5368 // value from symbol + addend.
5369 // For a global symbol, relocation value is addend.
5370 Valtype x;
5371 if (local)
5373 // Find GOT page entry.
5374 Mips_address value = ((psymval->value(object, addend) + 0x8000)
5375 & ~0xffff);
5376 target->got_section()->get_got_page_offset(value, object);
5378 x = psymval->value(object, addend) - value;
5380 else
5381 x = addend;
5382 val = Bits<32>::bit_select32(val, x, 0xffff);
5384 if (calculate_only)
5386 *calculated_value = x;
5387 return This::STATUS_OKAY;
5389 else
5390 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5392 return check_overflow<16>(x);
5395 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5396 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5397 static inline typename This::Status
5398 relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5399 Valtype* calculated_value)
5401 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5402 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5403 Valtype x = gp_offset;
5404 x = ((x + 0x8000) >> 16) & 0xffff;
5405 val = Bits<32>::bit_select32(val, x, 0xffff);
5407 if (calculate_only)
5408 *calculated_value = x;
5409 else
5410 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5412 return This::STATUS_OKAY;
5415 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5416 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5417 static inline typename This::Status
5418 relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5419 Valtype* calculated_value)
5421 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5422 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5423 Valtype x = gp_offset;
5424 val = Bits<32>::bit_select32(val, x, 0xffff);
5426 if (calculate_only)
5427 *calculated_value = x;
5428 else
5429 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5431 return This::STATUS_OKAY;
5434 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5435 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5436 static inline typename This::Status
5437 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5438 const Symbol_value<size>* psymval, Mips_address gp,
5439 Mips_address addend_a, bool extract_addend, bool local,
5440 unsigned int r_type, bool calculate_only,
5441 Valtype* calculated_value)
5443 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5444 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5446 Valtype addend;
5447 if (extract_addend)
5449 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5450 addend = (val & 0x7f) << 2;
5451 else
5452 addend = val & 0xffff;
5453 // Only sign-extend the addend if it was extracted from the
5454 // instruction. If the addend was separate, leave it alone,
5455 // otherwise we may lose significant bits.
5456 addend = Bits<16>::sign_extend32(addend);
5458 else
5459 addend = addend_a;
5461 Valtype x = psymval->value(object, addend) - gp;
5463 // If the symbol was local, any earlier relocatable links will
5464 // have adjusted its addend with the gp offset, so compensate
5465 // for that now. Don't do it for symbols forced local in this
5466 // link, though, since they won't have had the gp offset applied
5467 // to them before.
5468 if (local)
5469 x += object->gp_value();
5471 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5472 val = Bits<32>::bit_select32(val, x, 0x7f);
5473 else
5474 val = Bits<32>::bit_select32(val, x, 0xffff);
5476 if (calculate_only)
5478 *calculated_value = x;
5479 return This::STATUS_OKAY;
5481 else
5482 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5484 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5486 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5487 "limit (see option -G)"));
5488 return This::STATUS_OVERFLOW;
5490 return This::STATUS_OKAY;
5493 // R_MIPS_GPREL32
5494 static inline typename This::Status
5495 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5496 const Symbol_value<size>* psymval, Mips_address gp,
5497 Mips_address addend_a, bool extract_addend, bool calculate_only,
5498 Valtype* calculated_value)
5500 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5501 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5502 Valtype addend = extract_addend ? val : addend_a;
5504 // R_MIPS_GPREL32 relocations are defined for local symbols only.
5505 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5507 if (calculate_only)
5508 *calculated_value = x;
5509 else
5510 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5512 return This::STATUS_OKAY;
5515 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5516 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5517 // R_MICROMIPS_TLS_DTPREL_HI16
5518 static inline typename This::Status
5519 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5520 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5521 Mips_address addend_a, bool extract_addend, bool calculate_only,
5522 Valtype* calculated_value)
5524 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5525 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5526 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5528 // tls symbol values are relative to tls_segment()->vaddr()
5529 Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5530 val = Bits<32>::bit_select32(val, x, 0xffff);
5532 if (calculate_only)
5533 *calculated_value = x;
5534 else
5535 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5537 return This::STATUS_OKAY;
5540 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5541 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5542 // R_MICROMIPS_TLS_DTPREL_LO16,
5543 static inline typename This::Status
5544 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5545 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5546 Mips_address addend_a, bool extract_addend, bool calculate_only,
5547 Valtype* calculated_value)
5549 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5550 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5551 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5553 // tls symbol values are relative to tls_segment()->vaddr()
5554 Valtype x = psymval->value(object, addend) - tp_offset;
5555 val = Bits<32>::bit_select32(val, x, 0xffff);
5557 if (calculate_only)
5558 *calculated_value = x;
5559 else
5560 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5562 return This::STATUS_OKAY;
5565 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5566 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5567 static inline typename This::Status
5568 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5569 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5570 Mips_address addend_a, bool extract_addend, bool calculate_only,
5571 Valtype* calculated_value)
5573 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5574 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5575 Valtype addend = extract_addend ? val : addend_a;
5577 // tls symbol values are relative to tls_segment()->vaddr()
5578 Valtype x = psymval->value(object, addend) - tp_offset;
5580 if (calculate_only)
5581 *calculated_value = x;
5582 else
5583 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5585 return This::STATUS_OKAY;
5588 // R_MIPS_SUB, R_MICROMIPS_SUB
5589 static inline typename This::Status
5590 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5591 const Symbol_value<size>* psymval, Mips_address addend_a,
5592 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5594 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5595 Valtype64 addend = (extract_addend
5596 ? elfcpp::Swap<64, big_endian>::readval(wv)
5597 : addend_a);
5599 Valtype64 x = psymval->value(object, -addend);
5600 if (calculate_only)
5601 *calculated_value = x;
5602 else
5603 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5605 return This::STATUS_OKAY;
5608 // R_MIPS_64: S + A
5609 static inline typename This::Status
5610 rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5611 const Symbol_value<size>* psymval, Mips_address addend_a,
5612 bool extract_addend, bool calculate_only, Valtype* calculated_value,
5613 bool apply_addend_only)
5615 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5616 Valtype64 addend = (extract_addend
5617 ? elfcpp::Swap<64, big_endian>::readval(wv)
5618 : addend_a);
5620 Valtype64 x = psymval->value(object, addend);
5621 if (calculate_only)
5622 *calculated_value = x;
5623 else
5625 if (apply_addend_only)
5626 x = addend;
5627 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5630 return This::STATUS_OKAY;
5633 // R_MIPS_HIGHER, R_MICROMIPS_HIGHER
5634 static inline typename This::Status
5635 relhigher(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5636 const Symbol_value<size>* psymval, Mips_address addend_a,
5637 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5639 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5640 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5641 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5642 : addend_a);
5644 Valtype x = psymval->value(object, addend);
5645 x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
5646 val = Bits<32>::bit_select32(val, x, 0xffff);
5648 if (calculate_only)
5649 *calculated_value = x;
5650 else
5651 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5653 return This::STATUS_OKAY;
5656 // R_MIPS_HIGHEST, R_MICROMIPS_HIGHEST
5657 static inline typename This::Status
5658 relhighest(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5659 const Symbol_value<size>* psymval, Mips_address addend_a,
5660 bool extract_addend, bool calculate_only,
5661 Valtype* calculated_value)
5663 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5664 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5665 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5666 : addend_a);
5668 Valtype x = psymval->value(object, addend);
5669 x = ((x + (uint64_t) 0x800080008000llu) >> 48) & 0xffff;
5670 val = Bits<32>::bit_select32(val, x, 0xffff);
5672 if (calculate_only)
5673 *calculated_value = x;
5674 else
5675 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5677 return This::STATUS_OKAY;
5681 template<int size, bool big_endian>
5682 typename std::list<reloc_high<size, big_endian> >
5683 Mips_relocate_functions<size, big_endian>::hi16_relocs;
5685 template<int size, bool big_endian>
5686 typename std::list<reloc_high<size, big_endian> >
5687 Mips_relocate_functions<size, big_endian>::got16_relocs;
5689 template<int size, bool big_endian>
5690 typename std::list<reloc_high<size, big_endian> >
5691 Mips_relocate_functions<size, big_endian>::pchi16_relocs;
5693 // Mips_got_info methods.
5695 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5696 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5698 template<int size, bool big_endian>
5699 void
5700 Mips_got_info<size, big_endian>::record_local_got_symbol(
5701 Mips_relobj<size, big_endian>* object, unsigned int symndx,
5702 Mips_address addend, unsigned int r_type, unsigned int shndx,
5703 bool is_section_symbol)
5705 Mips_got_entry<size, big_endian>* entry =
5706 new Mips_got_entry<size, big_endian>(object, symndx, addend,
5707 mips_elf_reloc_tls_type(r_type),
5708 shndx, is_section_symbol);
5709 this->record_got_entry(entry, object);
5712 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5713 // in OBJECT. FOR_CALL is true if the caller is only interested in
5714 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5715 // relocation.
5717 template<int size, bool big_endian>
5718 void
5719 Mips_got_info<size, big_endian>::record_global_got_symbol(
5720 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5721 unsigned int r_type, bool dyn_reloc, bool for_call)
5723 if (!for_call)
5724 mips_sym->set_got_not_only_for_calls();
5726 // A global symbol in the GOT must also be in the dynamic symbol table.
5727 if (!mips_sym->needs_dynsym_entry() && !mips_sym->is_forced_local())
5729 switch (mips_sym->visibility())
5731 case elfcpp::STV_INTERNAL:
5732 case elfcpp::STV_HIDDEN:
5733 mips_sym->set_is_forced_local();
5734 break;
5735 default:
5736 mips_sym->set_needs_dynsym_entry();
5737 break;
5741 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5742 if (tls_type == GOT_TLS_NONE)
5743 this->global_got_symbols_.insert(mips_sym);
5745 if (dyn_reloc)
5747 if (mips_sym->global_got_area() == GGA_NONE)
5748 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5749 return;
5752 Mips_got_entry<size, big_endian>* entry =
5753 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5755 this->record_got_entry(entry, object);
5758 // Add ENTRY to master GOT and to OBJECT's GOT.
5760 template<int size, bool big_endian>
5761 void
5762 Mips_got_info<size, big_endian>::record_got_entry(
5763 Mips_got_entry<size, big_endian>* entry,
5764 Mips_relobj<size, big_endian>* object)
5766 this->got_entries_.insert(entry);
5768 // Create the GOT entry for the OBJECT's GOT.
5769 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5770 Mips_got_entry<size, big_endian>* entry2 =
5771 new Mips_got_entry<size, big_endian>(*entry);
5773 g->got_entries_.insert(entry2);
5776 // Record that OBJECT has a page relocation against symbol SYMNDX and
5777 // that ADDEND is the addend for that relocation.
5778 // This function creates an upper bound on the number of GOT slots
5779 // required; no attempt is made to combine references to non-overridable
5780 // global symbols across multiple input files.
5782 template<int size, bool big_endian>
5783 void
5784 Mips_got_info<size, big_endian>::record_got_page_entry(
5785 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5787 struct Got_page_range **range_ptr, *range;
5788 int old_pages, new_pages;
5790 // Find the Got_page_entry for this symbol.
5791 Got_page_entry* entry = new Got_page_entry(object, symndx);
5792 typename Got_page_entry_set::iterator it =
5793 this->got_page_entries_.find(entry);
5794 if (it != this->got_page_entries_.end())
5795 entry = *it;
5796 else
5797 this->got_page_entries_.insert(entry);
5799 // Get the object's GOT, but we don't need to insert an entry here.
5800 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5802 // Skip over ranges whose maximum extent cannot share a page entry
5803 // with ADDEND.
5804 range_ptr = &entry->ranges;
5805 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5806 range_ptr = &(*range_ptr)->next;
5808 // If we scanned to the end of the list, or found a range whose
5809 // minimum extent cannot share a page entry with ADDEND, create
5810 // a new singleton range.
5811 range = *range_ptr;
5812 if (!range || addend < range->min_addend - 0xffff)
5814 range = new Got_page_range();
5815 range->next = *range_ptr;
5816 range->min_addend = addend;
5817 range->max_addend = addend;
5819 *range_ptr = range;
5820 ++this->page_gotno_;
5821 ++g2->page_gotno_;
5822 return;
5825 // Remember how many pages the old range contributed.
5826 old_pages = range->get_max_pages();
5828 // Update the ranges.
5829 if (addend < range->min_addend)
5830 range->min_addend = addend;
5831 else if (addend > range->max_addend)
5833 if (range->next && addend >= range->next->min_addend - 0xffff)
5835 old_pages += range->next->get_max_pages();
5836 range->max_addend = range->next->max_addend;
5837 range->next = range->next->next;
5839 else
5840 range->max_addend = addend;
5843 // Record any change in the total estimate.
5844 new_pages = range->get_max_pages();
5845 if (old_pages != new_pages)
5847 this->page_gotno_ += new_pages - old_pages;
5848 g2->page_gotno_ += new_pages - old_pages;
5852 // Create all entries that should be in the local part of the GOT.
5854 template<int size, bool big_endian>
5855 void
5856 Mips_got_info<size, big_endian>::add_local_entries(
5857 Target_mips<size, big_endian>* target, Layout* layout)
5859 Mips_output_data_got<size, big_endian>* got = target->got_section();
5860 // First two GOT entries are reserved. The first entry will be filled at
5861 // runtime. The second entry will be used by some runtime loaders.
5862 got->add_constant(0);
5863 got->add_constant(target->mips_elf_gnu_got1_mask());
5865 for (typename Got_entry_set::iterator
5866 p = this->got_entries_.begin();
5867 p != this->got_entries_.end();
5868 ++p)
5870 Mips_got_entry<size, big_endian>* entry = *p;
5871 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5873 got->add_local(entry->object(), entry->symndx(),
5874 GOT_TYPE_STANDARD, entry->addend());
5875 unsigned int got_offset = entry->object()->local_got_offset(
5876 entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5877 if (got->multi_got() && this->index_ > 0
5878 && parameters->options().output_is_position_independent())
5880 if (!entry->is_section_symbol())
5881 target->rel_dyn_section(layout)->add_local(entry->object(),
5882 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5883 else
5884 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5885 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5886 got, got_offset);
5891 this->add_page_entries(target, layout);
5893 // Add global entries that should be in the local area.
5894 for (typename Got_entry_set::iterator
5895 p = this->got_entries_.begin();
5896 p != this->got_entries_.end();
5897 ++p)
5899 Mips_got_entry<size, big_endian>* entry = *p;
5900 if (!entry->is_for_global_symbol())
5901 continue;
5903 Mips_symbol<size>* mips_sym = entry->sym();
5904 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5906 unsigned int got_type;
5907 if (!got->multi_got())
5908 got_type = GOT_TYPE_STANDARD;
5909 else
5910 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5911 if (got->add_global(mips_sym, got_type))
5913 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5914 if (got->multi_got() && this->index_ > 0
5915 && parameters->options().output_is_position_independent())
5916 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5917 mips_sym, elfcpp::R_MIPS_REL32, got,
5918 mips_sym->got_offset(got_type));
5924 // Create GOT page entries.
5926 template<int size, bool big_endian>
5927 void
5928 Mips_got_info<size, big_endian>::add_page_entries(
5929 Target_mips<size, big_endian>* target, Layout* layout)
5931 if (this->page_gotno_ == 0)
5932 return;
5934 Mips_output_data_got<size, big_endian>* got = target->got_section();
5935 this->got_page_offset_start_ = got->add_constant(0);
5936 if (got->multi_got() && this->index_ > 0
5937 && parameters->options().output_is_position_independent())
5938 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5939 this->got_page_offset_start_);
5940 int num_entries = this->page_gotno_;
5941 unsigned int prev_offset = this->got_page_offset_start_;
5942 while (--num_entries > 0)
5944 unsigned int next_offset = got->add_constant(0);
5945 if (got->multi_got() && this->index_ > 0
5946 && parameters->options().output_is_position_independent())
5947 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5948 next_offset);
5949 gold_assert(next_offset == prev_offset + size/8);
5950 prev_offset = next_offset;
5952 this->got_page_offset_next_ = this->got_page_offset_start_;
5955 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5957 template<int size, bool big_endian>
5958 void
5959 Mips_got_info<size, big_endian>::add_global_entries(
5960 Target_mips<size, big_endian>* target, Layout* layout,
5961 unsigned int non_reloc_only_global_gotno)
5963 Mips_output_data_got<size, big_endian>* got = target->got_section();
5964 // Add GGA_NORMAL entries.
5965 unsigned int count = 0;
5966 for (typename Got_entry_set::iterator
5967 p = this->got_entries_.begin();
5968 p != this->got_entries_.end();
5969 ++p)
5971 Mips_got_entry<size, big_endian>* entry = *p;
5972 if (!entry->is_for_global_symbol())
5973 continue;
5975 Mips_symbol<size>* mips_sym = entry->sym();
5976 if (mips_sym->global_got_area() != GGA_NORMAL)
5977 continue;
5979 unsigned int got_type;
5980 if (!got->multi_got())
5981 got_type = GOT_TYPE_STANDARD;
5982 else
5983 // In multi-GOT links, global symbol can be in both primary and
5984 // secondary GOT(s). By creating custom GOT type
5985 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5986 // is added to secondary GOT(s).
5987 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5988 if (!got->add_global(mips_sym, got_type))
5989 continue;
5991 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5992 if (got->multi_got() && this->index_ == 0)
5993 count++;
5994 if (got->multi_got() && this->index_ > 0)
5996 if (parameters->options().output_is_position_independent()
5997 || (!parameters->doing_static_link()
5998 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
6000 target->rel_dyn_section(layout)->add_global(
6001 mips_sym, elfcpp::R_MIPS_REL32, got,
6002 mips_sym->got_offset(got_type));
6003 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
6004 elfcpp::R_MIPS_REL32, mips_sym);
6009 if (!got->multi_got() || this->index_ == 0)
6011 if (got->multi_got())
6013 // We need to allocate space in the primary GOT for GGA_NORMAL entries
6014 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
6015 // entries correspond to dynamic symbol indexes.
6016 while (count < non_reloc_only_global_gotno)
6018 got->add_constant(0);
6019 ++count;
6023 // Add GGA_RELOC_ONLY entries.
6024 got->add_reloc_only_entries();
6028 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6030 template<int size, bool big_endian>
6031 void
6032 Mips_got_info<size, big_endian>::add_reloc_only_entries(
6033 Mips_output_data_got<size, big_endian>* got)
6035 for (typename Global_got_entry_set::iterator
6036 p = this->global_got_symbols_.begin();
6037 p != this->global_got_symbols_.end();
6038 ++p)
6040 Mips_symbol<size>* mips_sym = *p;
6041 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6043 unsigned int got_type;
6044 if (!got->multi_got())
6045 got_type = GOT_TYPE_STANDARD;
6046 else
6047 got_type = GOT_TYPE_STANDARD_MULTIGOT;
6048 if (got->add_global(mips_sym, got_type))
6049 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6054 // Create TLS GOT entries.
6056 template<int size, bool big_endian>
6057 void
6058 Mips_got_info<size, big_endian>::add_tls_entries(
6059 Target_mips<size, big_endian>* target, Layout* layout)
6061 Mips_output_data_got<size, big_endian>* got = target->got_section();
6062 // Add local tls entries.
6063 for (typename Got_entry_set::iterator
6064 p = this->got_entries_.begin();
6065 p != this->got_entries_.end();
6066 ++p)
6068 Mips_got_entry<size, big_endian>* entry = *p;
6069 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6070 continue;
6072 if (entry->tls_type() == GOT_TLS_GD)
6074 unsigned int got_type = GOT_TYPE_TLS_PAIR;
6075 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6076 : elfcpp::R_MIPS_TLS_DTPMOD64);
6077 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6078 : elfcpp::R_MIPS_TLS_DTPREL64);
6080 if (!parameters->doing_static_link())
6082 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
6083 entry->shndx(), got_type,
6084 target->rel_dyn_section(layout),
6085 r_type1, entry->addend());
6086 unsigned int got_offset =
6087 entry->object()->local_got_offset(entry->symndx(), got_type,
6088 entry->addend());
6089 got->add_static_reloc(got_offset + size/8, r_type2,
6090 entry->object(), entry->symndx());
6092 else
6094 // We are doing a static link. Mark it as belong to module 1,
6095 // the executable.
6096 unsigned int got_offset = got->add_constant(1);
6097 entry->object()->set_local_got_offset(entry->symndx(), got_type,
6098 got_offset,
6099 entry->addend());
6100 got->add_constant(0);
6101 got->add_static_reloc(got_offset + size/8, r_type2,
6102 entry->object(), entry->symndx());
6105 else if (entry->tls_type() == GOT_TLS_IE)
6107 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6108 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6109 : elfcpp::R_MIPS_TLS_TPREL64);
6110 if (!parameters->doing_static_link())
6111 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
6112 target->rel_dyn_section(layout), r_type,
6113 entry->addend());
6114 else
6116 got->add_local(entry->object(), entry->symndx(), got_type,
6117 entry->addend());
6118 unsigned int got_offset =
6119 entry->object()->local_got_offset(entry->symndx(), got_type,
6120 entry->addend());
6121 got->add_static_reloc(got_offset, r_type, entry->object(),
6122 entry->symndx());
6125 else if (entry->tls_type() == GOT_TLS_LDM)
6127 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6128 : elfcpp::R_MIPS_TLS_DTPMOD64);
6129 unsigned int got_offset;
6130 if (!parameters->doing_static_link())
6132 got_offset = got->add_constant(0);
6133 target->rel_dyn_section(layout)->add_local(
6134 entry->object(), 0, r_type, got, got_offset);
6136 else
6137 // We are doing a static link. Just mark it as belong to module 1,
6138 // the executable.
6139 got_offset = got->add_constant(1);
6141 got->add_constant(0);
6142 got->set_tls_ldm_offset(got_offset, entry->object());
6144 else
6145 gold_unreachable();
6148 // Add global tls entries.
6149 for (typename Got_entry_set::iterator
6150 p = this->got_entries_.begin();
6151 p != this->got_entries_.end();
6152 ++p)
6154 Mips_got_entry<size, big_endian>* entry = *p;
6155 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6156 continue;
6158 Mips_symbol<size>* mips_sym = entry->sym();
6159 if (entry->tls_type() == GOT_TLS_GD)
6161 unsigned int got_type;
6162 if (!got->multi_got())
6163 got_type = GOT_TYPE_TLS_PAIR;
6164 else
6165 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6166 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6167 : elfcpp::R_MIPS_TLS_DTPMOD64);
6168 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6169 : elfcpp::R_MIPS_TLS_DTPREL64);
6170 if (!parameters->doing_static_link())
6171 got->add_global_pair_with_rel(mips_sym, got_type,
6172 target->rel_dyn_section(layout), r_type1, r_type2);
6173 else
6175 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
6176 // GOT entries. The first one is initialized to be 1, which is the
6177 // module index for the main executable and the second one 0. A
6178 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6179 // the second GOT entry and will be applied by gold.
6180 unsigned int got_offset = got->add_constant(1);
6181 mips_sym->set_got_offset(got_type, got_offset);
6182 got->add_constant(0);
6183 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6186 else if (entry->tls_type() == GOT_TLS_IE)
6188 unsigned int got_type;
6189 if (!got->multi_got())
6190 got_type = GOT_TYPE_TLS_OFFSET;
6191 else
6192 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
6193 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6194 : elfcpp::R_MIPS_TLS_TPREL64);
6195 if (!parameters->doing_static_link())
6196 got->add_global_with_rel(mips_sym, got_type,
6197 target->rel_dyn_section(layout), r_type);
6198 else
6200 got->add_global(mips_sym, got_type);
6201 unsigned int got_offset = mips_sym->got_offset(got_type);
6202 got->add_static_reloc(got_offset, r_type, mips_sym);
6205 else
6206 gold_unreachable();
6210 // Decide whether the symbol needs an entry in the global part of the primary
6211 // GOT, setting global_got_area accordingly. Count the number of global
6212 // symbols that are in the primary GOT only because they have dynamic
6213 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6215 template<int size, bool big_endian>
6216 void
6217 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6219 for (typename Global_got_entry_set::iterator
6220 p = this->global_got_symbols_.begin();
6221 p != this->global_got_symbols_.end();
6222 ++p)
6224 Mips_symbol<size>* sym = *p;
6225 // Make a final decision about whether the symbol belongs in the
6226 // local or global GOT. Symbols that bind locally can (and in the
6227 // case of forced-local symbols, must) live in the local GOT.
6228 // Those that are aren't in the dynamic symbol table must also
6229 // live in the local GOT.
6231 if (!sym->should_add_dynsym_entry(symtab)
6232 || (sym->got_only_for_calls()
6233 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6234 : symbol_references_local(sym,
6235 sym->should_add_dynsym_entry(symtab))))
6236 // The symbol belongs in the local GOT. We no longer need this
6237 // entry if it was only used for relocations; those relocations
6238 // will be against the null or section symbol instead.
6239 sym->set_global_got_area(GGA_NONE);
6240 else if (sym->global_got_area() == GGA_RELOC_ONLY)
6242 ++this->reloc_only_gotno_;
6243 ++this->global_gotno_ ;
6248 // Return the offset of GOT page entry for VALUE. Initialize the entry with
6249 // VALUE if it is not initialized.
6251 template<int size, bool big_endian>
6252 unsigned int
6253 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6254 Mips_output_data_got<size, big_endian>* got)
6256 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6257 if (it != this->got_page_offsets_.end())
6258 return it->second;
6260 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6261 + (size/8) * this->page_gotno_);
6263 unsigned int got_offset = this->got_page_offset_next_;
6264 this->got_page_offsets_[value] = got_offset;
6265 this->got_page_offset_next_ += size/8;
6266 got->update_got_entry(got_offset, value);
6267 return got_offset;
6270 // Remove lazy-binding stubs for global symbols in this GOT.
6272 template<int size, bool big_endian>
6273 void
6274 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6275 Target_mips<size, big_endian>* target)
6277 for (typename Got_entry_set::iterator
6278 p = this->got_entries_.begin();
6279 p != this->got_entries_.end();
6280 ++p)
6282 Mips_got_entry<size, big_endian>* entry = *p;
6283 if (entry->is_for_global_symbol())
6284 target->remove_lazy_stub_entry(entry->sym());
6288 // Count the number of GOT entries required.
6290 template<int size, bool big_endian>
6291 void
6292 Mips_got_info<size, big_endian>::count_got_entries()
6294 for (typename Got_entry_set::iterator
6295 p = this->got_entries_.begin();
6296 p != this->got_entries_.end();
6297 ++p)
6299 this->count_got_entry(*p);
6303 // Count the number of GOT entries required by ENTRY. Accumulate the result.
6305 template<int size, bool big_endian>
6306 void
6307 Mips_got_info<size, big_endian>::count_got_entry(
6308 Mips_got_entry<size, big_endian>* entry)
6310 if (entry->is_tls_entry())
6311 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6312 else if (entry->is_for_local_symbol()
6313 || entry->sym()->global_got_area() == GGA_NONE)
6314 ++this->local_gotno_;
6315 else
6316 ++this->global_gotno_;
6319 // Add FROM's GOT entries.
6321 template<int size, bool big_endian>
6322 void
6323 Mips_got_info<size, big_endian>::add_got_entries(
6324 Mips_got_info<size, big_endian>* from)
6326 for (typename Got_entry_set::iterator
6327 p = from->got_entries_.begin();
6328 p != from->got_entries_.end();
6329 ++p)
6331 Mips_got_entry<size, big_endian>* entry = *p;
6332 if (this->got_entries_.find(entry) == this->got_entries_.end())
6334 Mips_got_entry<size, big_endian>* entry2 =
6335 new Mips_got_entry<size, big_endian>(*entry);
6336 this->got_entries_.insert(entry2);
6337 this->count_got_entry(entry);
6342 // Add FROM's GOT page entries.
6344 template<int size, bool big_endian>
6345 void
6346 Mips_got_info<size, big_endian>::add_got_page_count(
6347 Mips_got_info<size, big_endian>* from)
6349 this->page_gotno_ += from->page_gotno_;
6352 // Mips_output_data_got methods.
6354 // Lay out the GOT. Add local, global and TLS entries. If GOT is
6355 // larger than 64K, create multi-GOT.
6357 template<int size, bool big_endian>
6358 void
6359 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6360 Symbol_table* symtab, const Input_objects* input_objects)
6362 // Decide which symbols need to go in the global part of the GOT and
6363 // count the number of reloc-only GOT symbols.
6364 this->master_got_info_->count_got_symbols(symtab);
6366 // Count the number of GOT entries.
6367 this->master_got_info_->count_got_entries();
6369 unsigned int got_size = this->master_got_info_->got_size();
6370 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6371 this->lay_out_multi_got(layout, input_objects);
6372 else
6374 // Record that all objects use single GOT.
6375 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6376 p != input_objects->relobj_end();
6377 ++p)
6379 Mips_relobj<size, big_endian>* object =
6380 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6381 if (object->get_got_info() != NULL)
6382 object->set_got_info(this->master_got_info_);
6385 this->master_got_info_->add_local_entries(this->target_, layout);
6386 this->master_got_info_->add_global_entries(this->target_, layout,
6387 /*not used*/-1U);
6388 this->master_got_info_->add_tls_entries(this->target_, layout);
6392 // Create multi-GOT. For every GOT, add local, global and TLS entries.
6394 template<int size, bool big_endian>
6395 void
6396 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6397 const Input_objects* input_objects)
6399 // Try to merge the GOTs of input objects together, as long as they
6400 // don't seem to exceed the maximum GOT size, choosing one of them
6401 // to be the primary GOT.
6402 this->merge_gots(input_objects);
6404 // Every symbol that is referenced in a dynamic relocation must be
6405 // present in the primary GOT.
6406 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6408 // Add GOT entries.
6409 unsigned int i = 0;
6410 unsigned int offset = 0;
6411 Mips_got_info<size, big_endian>* g = this->primary_got_;
6414 g->set_index(i);
6415 g->set_offset(offset);
6417 g->add_local_entries(this->target_, layout);
6418 if (i == 0)
6419 g->add_global_entries(this->target_, layout,
6420 (this->master_got_info_->global_gotno()
6421 - this->master_got_info_->reloc_only_gotno()));
6422 else
6423 g->add_global_entries(this->target_, layout, /*not used*/-1U);
6424 g->add_tls_entries(this->target_, layout);
6426 // Forbid global symbols in every non-primary GOT from having
6427 // lazy-binding stubs.
6428 if (i > 0)
6429 g->remove_lazy_stubs(this->target_);
6431 ++i;
6432 offset += g->got_size();
6433 g = g->next();
6435 while (g);
6438 // Attempt to merge GOTs of different input objects. Try to use as much as
6439 // possible of the primary GOT, since it doesn't require explicit dynamic
6440 // relocations, but don't use objects that would reference global symbols
6441 // out of the addressable range. Failing the primary GOT, attempt to merge
6442 // with the current GOT, or finish the current GOT and then make make the new
6443 // GOT current.
6445 template<int size, bool big_endian>
6446 void
6447 Mips_output_data_got<size, big_endian>::merge_gots(
6448 const Input_objects* input_objects)
6450 gold_assert(this->primary_got_ == NULL);
6451 Mips_got_info<size, big_endian>* current = NULL;
6453 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6454 p != input_objects->relobj_end();
6455 ++p)
6457 Mips_relobj<size, big_endian>* object =
6458 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6460 Mips_got_info<size, big_endian>* g = object->get_got_info();
6461 if (g == NULL)
6462 continue;
6464 g->count_got_entries();
6466 // Work out the number of page, local and TLS entries.
6467 unsigned int estimate = this->master_got_info_->page_gotno();
6468 if (estimate > g->page_gotno())
6469 estimate = g->page_gotno();
6470 estimate += g->local_gotno() + g->tls_gotno();
6472 // We place TLS GOT entries after both locals and globals. The globals
6473 // for the primary GOT may overflow the normal GOT size limit, so be
6474 // sure not to merge a GOT which requires TLS with the primary GOT in that
6475 // case. This doesn't affect non-primary GOTs.
6476 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6477 : g->global_gotno());
6479 unsigned int max_count =
6480 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6481 if (estimate <= max_count)
6483 // If we don't have a primary GOT, use it as
6484 // a starting point for the primary GOT.
6485 if (!this->primary_got_)
6487 this->primary_got_ = g;
6488 continue;
6491 // Try merging with the primary GOT.
6492 if (this->merge_got_with(g, object, this->primary_got_))
6493 continue;
6496 // If we can merge with the last-created GOT, do it.
6497 if (current && this->merge_got_with(g, object, current))
6498 continue;
6500 // Well, we couldn't merge, so create a new GOT. Don't check if it
6501 // fits; if it turns out that it doesn't, we'll get relocation
6502 // overflows anyway.
6503 g->set_next(current);
6504 current = g;
6507 // If we do not find any suitable primary GOT, create an empty one.
6508 if (this->primary_got_ == NULL)
6509 this->primary_got_ = new Mips_got_info<size, big_endian>();
6511 // Link primary GOT with secondary GOTs.
6512 this->primary_got_->set_next(current);
6515 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6516 // this would lead to overflow, true if they were merged successfully.
6518 template<int size, bool big_endian>
6519 bool
6520 Mips_output_data_got<size, big_endian>::merge_got_with(
6521 Mips_got_info<size, big_endian>* from,
6522 Mips_relobj<size, big_endian>* object,
6523 Mips_got_info<size, big_endian>* to)
6525 // Work out how many page entries we would need for the combined GOT.
6526 unsigned int estimate = this->master_got_info_->page_gotno();
6527 if (estimate >= from->page_gotno() + to->page_gotno())
6528 estimate = from->page_gotno() + to->page_gotno();
6530 // Conservatively estimate how many local and TLS entries would be needed.
6531 estimate += from->local_gotno() + to->local_gotno();
6532 estimate += from->tls_gotno() + to->tls_gotno();
6534 // If we're merging with the primary got, any TLS relocations will
6535 // come after the full set of global entries. Otherwise estimate those
6536 // conservatively as well.
6537 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6538 estimate += this->master_got_info_->global_gotno();
6539 else
6540 estimate += from->global_gotno() + to->global_gotno();
6542 // Bail out if the combined GOT might be too big.
6543 unsigned int max_count =
6544 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6545 if (estimate > max_count)
6546 return false;
6548 // Transfer the object's GOT information from FROM to TO.
6549 to->add_got_entries(from);
6550 to->add_got_page_count(from);
6552 // Record that OBJECT should use output GOT TO.
6553 object->set_got_info(to);
6555 return true;
6558 // Write out the GOT.
6560 template<int size, bool big_endian>
6561 void
6562 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6564 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6565 Mips_stubs_entry_set;
6567 // Call parent to write out GOT.
6568 Output_data_got<size, big_endian>::do_write(of);
6570 const off_t offset = this->offset();
6571 const section_size_type oview_size =
6572 convert_to_section_size_type(this->data_size());
6573 unsigned char* const oview = of->get_output_view(offset, oview_size);
6575 // Needed for fixing values of .got section.
6576 this->got_view_ = oview;
6578 // Write lazy stub addresses.
6579 for (typename Mips_stubs_entry_set::iterator
6580 p = this->master_got_info_->global_got_symbols().begin();
6581 p != this->master_got_info_->global_got_symbols().end();
6582 ++p)
6584 Mips_symbol<size>* mips_sym = *p;
6585 if (mips_sym->has_lazy_stub())
6587 Valtype* wv = reinterpret_cast<Valtype*>(
6588 oview + this->get_primary_got_offset(mips_sym));
6589 Valtype value =
6590 this->target_->mips_stubs_section()->stub_address(mips_sym);
6591 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6595 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6596 for (typename Mips_stubs_entry_set::iterator
6597 p = this->master_got_info_->global_got_symbols().begin();
6598 p != this->master_got_info_->global_got_symbols().end();
6599 ++p)
6601 Mips_symbol<size>* mips_sym = *p;
6602 if (!this->multi_got()
6603 && (mips_sym->is_mips16() || mips_sym->is_micromips())
6604 && mips_sym->global_got_area() == GGA_NONE
6605 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6607 Valtype* wv = reinterpret_cast<Valtype*>(
6608 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6609 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6610 if (value != 0)
6612 value |= 1;
6613 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6618 if (!this->secondary_got_relocs_.empty())
6620 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6621 // secondary GOT entries with non-zero initial value copy the value
6622 // to the corresponding primary GOT entry, and set the secondary GOT
6623 // entry to zero.
6624 // TODO(sasa): This is workaround. It needs to be investigated further.
6626 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6628 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6629 if (reloc.symbol_is_global())
6631 Mips_symbol<size>* gsym = reloc.symbol();
6632 gold_assert(gsym != NULL);
6634 unsigned got_offset = reloc.got_offset();
6635 gold_assert(got_offset < oview_size);
6637 // Find primary GOT entry.
6638 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6639 oview + this->get_primary_got_offset(gsym));
6641 // Find secondary GOT entry.
6642 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6644 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6645 if (value != 0)
6647 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6648 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6649 gsym->set_applied_secondary_got_fixup();
6654 of->write_output_view(offset, oview_size, oview);
6657 // We are done if there is no fix up.
6658 if (this->static_relocs_.empty())
6659 return;
6661 Output_segment* tls_segment = this->layout_->tls_segment();
6662 gold_assert(tls_segment != NULL);
6664 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6666 Static_reloc& reloc(this->static_relocs_[i]);
6668 Mips_address value;
6669 if (!reloc.symbol_is_global())
6671 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6672 const Symbol_value<size>* psymval =
6673 object->local_symbol(reloc.index());
6675 // We are doing static linking. Issue an error and skip this
6676 // relocation if the symbol is undefined or in a discarded_section.
6677 bool is_ordinary;
6678 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6679 if ((shndx == elfcpp::SHN_UNDEF)
6680 || (is_ordinary
6681 && shndx != elfcpp::SHN_UNDEF
6682 && !object->is_section_included(shndx)
6683 && !this->symbol_table_->is_section_folded(object, shndx)))
6685 gold_error(_("undefined or discarded local symbol %u from "
6686 " object %s in GOT"),
6687 reloc.index(), reloc.relobj()->name().c_str());
6688 continue;
6691 value = psymval->value(object, 0);
6693 else
6695 const Mips_symbol<size>* gsym = reloc.symbol();
6696 gold_assert(gsym != NULL);
6698 // We are doing static linking. Issue an error and skip this
6699 // relocation if the symbol is undefined or in a discarded_section
6700 // unless it is a weakly_undefined symbol.
6701 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6702 && !gsym->is_weak_undefined())
6704 gold_error(_("undefined or discarded symbol %s in GOT"),
6705 gsym->name());
6706 continue;
6709 if (!gsym->is_weak_undefined())
6710 value = gsym->value();
6711 else
6712 value = 0;
6715 unsigned got_offset = reloc.got_offset();
6716 gold_assert(got_offset < oview_size);
6718 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6719 Valtype x;
6721 switch (reloc.r_type())
6723 case elfcpp::R_MIPS_TLS_DTPMOD32:
6724 case elfcpp::R_MIPS_TLS_DTPMOD64:
6725 x = value;
6726 break;
6727 case elfcpp::R_MIPS_TLS_DTPREL32:
6728 case elfcpp::R_MIPS_TLS_DTPREL64:
6729 x = value - elfcpp::DTP_OFFSET;
6730 break;
6731 case elfcpp::R_MIPS_TLS_TPREL32:
6732 case elfcpp::R_MIPS_TLS_TPREL64:
6733 x = value - elfcpp::TP_OFFSET;
6734 break;
6735 default:
6736 gold_unreachable();
6737 break;
6740 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6743 of->write_output_view(offset, oview_size, oview);
6746 // Mips_relobj methods.
6748 // Count the local symbols. The Mips backend needs to know if a symbol
6749 // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6750 // because the Symbol object keeps the ELF symbol type and st_other field.
6751 // For local symbol it is harder because we cannot access this information.
6752 // So we override the do_count_local_symbol in parent and scan local symbols to
6753 // mark MIPS16 and microMIPS functions. This is not the most efficient way but
6754 // I do not want to slow down other ports by calling a per symbol target hook
6755 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6757 template<int size, bool big_endian>
6758 void
6759 Mips_relobj<size, big_endian>::do_count_local_symbols(
6760 Stringpool_template<char>* pool,
6761 Stringpool_template<char>* dynpool)
6763 // Ask parent to count the local symbols.
6764 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6765 const unsigned int loccount = this->local_symbol_count();
6766 if (loccount == 0)
6767 return;
6769 // Initialize the mips16 and micromips function bit-vector.
6770 this->local_symbol_is_mips16_.resize(loccount, false);
6771 this->local_symbol_is_micromips_.resize(loccount, false);
6773 // Read the symbol table section header.
6774 const unsigned int symtab_shndx = this->symtab_shndx();
6775 elfcpp::Shdr<size, big_endian>
6776 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6777 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6779 // Read the local symbols.
6780 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6781 gold_assert(loccount == symtabshdr.get_sh_info());
6782 off_t locsize = loccount * sym_size;
6783 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6784 locsize, true, true);
6786 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6788 // Skip the first dummy symbol.
6789 psyms += sym_size;
6790 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6792 elfcpp::Sym<size, big_endian> sym(psyms);
6793 unsigned char st_other = sym.get_st_other();
6794 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6795 this->local_symbol_is_micromips_[i] =
6796 elfcpp::elf_st_is_micromips(st_other);
6800 // Read the symbol information.
6802 template<int size, bool big_endian>
6803 void
6804 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6806 // Call parent class to read symbol information.
6807 this->base_read_symbols(sd);
6809 // If this input file is a binary file, it has no processor
6810 // specific data.
6811 Input_file::Format format = this->input_file()->format();
6812 if (format != Input_file::FORMAT_ELF)
6814 gold_assert(format == Input_file::FORMAT_BINARY);
6815 this->merge_processor_specific_data_ = false;
6816 return;
6819 // Read processor-specific flags in ELF file header.
6820 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6821 elfcpp::Elf_sizes<size>::ehdr_size,
6822 true, false);
6823 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6824 this->processor_specific_flags_ = ehdr.get_e_flags();
6826 // Get the section names.
6827 const unsigned char* pnamesu = sd->section_names->data();
6828 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6830 // Initialize the mips16 stub section bit-vectors.
6831 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6832 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6833 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6835 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6836 const unsigned char* pshdrs = sd->section_headers->data();
6837 const unsigned char* ps = pshdrs + shdr_size;
6838 bool must_merge_processor_specific_data = false;
6839 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6841 elfcpp::Shdr<size, big_endian> shdr(ps);
6843 // Sometimes an object has no contents except the section name string
6844 // table and an empty symbol table with the undefined symbol. We
6845 // don't want to merge processor-specific data from such an object.
6846 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6848 // Symbol table is not empty.
6849 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
6850 elfcpp::Elf_sizes<size>::sym_size;
6851 if (shdr.get_sh_size() > sym_size)
6852 must_merge_processor_specific_data = true;
6854 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6855 // If this is neither an empty symbol table nor a string table,
6856 // be conservative.
6857 must_merge_processor_specific_data = true;
6859 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6861 this->has_reginfo_section_ = true;
6862 // Read the gp value that was used to create this object. We need the
6863 // gp value while processing relocs. The .reginfo section is not used
6864 // in the 64-bit MIPS ELF ABI.
6865 section_offset_type section_offset = shdr.get_sh_offset();
6866 section_size_type section_size =
6867 convert_to_section_size_type(shdr.get_sh_size());
6868 const unsigned char* view =
6869 this->get_view(section_offset, section_size, true, false);
6871 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6873 // Read the rest of .reginfo.
6874 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6875 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6876 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6877 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6878 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6881 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6883 gold_assert(this->attributes_section_data_ == NULL);
6884 section_offset_type section_offset = shdr.get_sh_offset();
6885 section_size_type section_size =
6886 convert_to_section_size_type(shdr.get_sh_size());
6887 const unsigned char* view =
6888 this->get_view(section_offset, section_size, true, false);
6889 this->attributes_section_data_ =
6890 new Attributes_section_data(view, section_size);
6893 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6895 gold_assert(this->abiflags_ == NULL);
6896 section_offset_type section_offset = shdr.get_sh_offset();
6897 section_size_type section_size =
6898 convert_to_section_size_type(shdr.get_sh_size());
6899 const unsigned char* view =
6900 this->get_view(section_offset, section_size, true, false);
6901 this->abiflags_ = new Mips_abiflags<big_endian>();
6903 this->abiflags_->version =
6904 elfcpp::Swap<16, big_endian>::readval(view);
6905 if (this->abiflags_->version != 0)
6907 gold_error(_("%s: .MIPS.abiflags section has "
6908 "unsupported version %u"),
6909 this->name().c_str(),
6910 this->abiflags_->version);
6911 break;
6913 this->abiflags_->isa_level =
6914 elfcpp::Swap<8, big_endian>::readval(view + 2);
6915 this->abiflags_->isa_rev =
6916 elfcpp::Swap<8, big_endian>::readval(view + 3);
6917 this->abiflags_->gpr_size =
6918 elfcpp::Swap<8, big_endian>::readval(view + 4);
6919 this->abiflags_->cpr1_size =
6920 elfcpp::Swap<8, big_endian>::readval(view + 5);
6921 this->abiflags_->cpr2_size =
6922 elfcpp::Swap<8, big_endian>::readval(view + 6);
6923 this->abiflags_->fp_abi =
6924 elfcpp::Swap<8, big_endian>::readval(view + 7);
6925 this->abiflags_->isa_ext =
6926 elfcpp::Swap<32, big_endian>::readval(view + 8);
6927 this->abiflags_->ases =
6928 elfcpp::Swap<32, big_endian>::readval(view + 12);
6929 this->abiflags_->flags1 =
6930 elfcpp::Swap<32, big_endian>::readval(view + 16);
6931 this->abiflags_->flags2 =
6932 elfcpp::Swap<32, big_endian>::readval(view + 20);
6935 // In the 64-bit ABI, .MIPS.options section holds register information.
6936 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6937 // starts with this header:
6939 // typedef struct
6940 // {
6941 // // Type of option.
6942 // unsigned char kind[1];
6943 // // Size of option descriptor, including header.
6944 // unsigned char size[1];
6945 // // Section index of affected section, or 0 for global option.
6946 // unsigned char section[2];
6947 // // Information specific to this kind of option.
6948 // unsigned char info[4];
6949 // };
6951 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6952 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6953 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6955 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6957 section_offset_type section_offset = shdr.get_sh_offset();
6958 section_size_type section_size =
6959 convert_to_section_size_type(shdr.get_sh_size());
6960 const unsigned char* view =
6961 this->get_view(section_offset, section_size, true, false);
6962 const unsigned char* end = view + section_size;
6964 while (view + 8 <= end)
6966 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6967 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6968 if (sz < 8)
6970 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6971 "than its header"),
6972 this->name().c_str(),
6973 this->mips_elf_options_section_name(), sz);
6974 break;
6977 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6979 // In the 64 bit ABI, an ODK_REGINFO option is the following
6980 // structure. The info field of the options header is not
6981 // used.
6983 // typedef struct
6984 // {
6985 // // Mask of general purpose registers used.
6986 // unsigned char ri_gprmask[4];
6987 // // Padding.
6988 // unsigned char ri_pad[4];
6989 // // Mask of co-processor registers used.
6990 // unsigned char ri_cprmask[4][4];
6991 // // GP register value for this object file.
6992 // unsigned char ri_gp_value[8];
6993 // };
6995 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6996 + 32);
6998 else if (kind == elfcpp::ODK_REGINFO)
7000 // In the 32 bit ABI, an ODK_REGINFO option is the following
7001 // structure. The info field of the options header is not
7002 // used. The same structure is used in .reginfo section.
7004 // typedef struct
7005 // {
7006 // unsigned char ri_gprmask[4];
7007 // unsigned char ri_cprmask[4][4];
7008 // unsigned char ri_gp_value[4];
7009 // };
7011 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7012 + 28);
7014 view += sz;
7018 const char* name = pnames + shdr.get_sh_name();
7019 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
7020 this->section_is_mips16_call_stub_[i] =
7021 is_prefix_of(".mips16.call.", name);
7022 this->section_is_mips16_call_fp_stub_[i] =
7023 is_prefix_of(".mips16.call.fp.", name);
7025 if (strcmp(name, ".pdr") == 0)
7027 gold_assert(this->pdr_shndx_ == -1U);
7028 this->pdr_shndx_ = i;
7032 // This is rare.
7033 if (!must_merge_processor_specific_data)
7034 this->merge_processor_specific_data_ = false;
7037 // Discard MIPS16 stub secions that are not needed.
7039 template<int size, bool big_endian>
7040 void
7041 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7043 for (typename Mips16_stubs_int_map::const_iterator
7044 it = this->mips16_stub_sections_.begin();
7045 it != this->mips16_stub_sections_.end(); ++it)
7047 Mips16_stub_section<size, big_endian>* stub_section = it->second;
7048 if (!stub_section->is_target_found())
7050 gold_error(_("no relocation found in mips16 stub section '%s'"),
7051 stub_section->object()
7052 ->section_name(stub_section->shndx()).c_str());
7055 bool discard = false;
7056 if (stub_section->is_for_local_function())
7058 if (stub_section->is_fn_stub())
7060 // This stub is for a local symbol. This stub will only
7061 // be needed if there is some relocation in this object,
7062 // other than a 16 bit function call, which refers to this
7063 // symbol.
7064 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7065 discard = true;
7066 else
7067 this->add_local_mips16_fn_stub(stub_section);
7069 else
7071 // This stub is for a local symbol. This stub will only
7072 // be needed if there is some relocation (R_MIPS16_26) in
7073 // this object that refers to this symbol.
7074 gold_assert(stub_section->is_call_stub()
7075 || stub_section->is_call_fp_stub());
7076 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
7077 discard = true;
7078 else
7079 this->add_local_mips16_call_stub(stub_section);
7082 else
7084 Mips_symbol<size>* gsym = stub_section->gsym();
7085 if (stub_section->is_fn_stub())
7087 if (gsym->has_mips16_fn_stub())
7088 // We already have a stub for this function.
7089 discard = true;
7090 else
7092 gsym->set_mips16_fn_stub(stub_section);
7093 if (gsym->should_add_dynsym_entry(symtab))
7095 // If we have a MIPS16 function with a stub, the
7096 // dynamic symbol must refer to the stub, since only
7097 // the stub uses the standard calling conventions.
7098 gsym->set_need_fn_stub();
7099 if (gsym->is_from_dynobj())
7100 gsym->set_needs_dynsym_value();
7103 if (!gsym->need_fn_stub())
7104 discard = true;
7106 else if (stub_section->is_call_stub())
7108 if (gsym->is_mips16())
7109 // We don't need the call_stub; this is a 16 bit
7110 // function, so calls from other 16 bit functions are
7111 // OK.
7112 discard = true;
7113 else if (gsym->has_mips16_call_stub())
7114 // We already have a stub for this function.
7115 discard = true;
7116 else
7117 gsym->set_mips16_call_stub(stub_section);
7119 else
7121 gold_assert(stub_section->is_call_fp_stub());
7122 if (gsym->is_mips16())
7123 // We don't need the call_stub; this is a 16 bit
7124 // function, so calls from other 16 bit functions are
7125 // OK.
7126 discard = true;
7127 else if (gsym->has_mips16_call_fp_stub())
7128 // We already have a stub for this function.
7129 discard = true;
7130 else
7131 gsym->set_mips16_call_fp_stub(stub_section);
7134 if (discard)
7135 this->set_output_section(stub_section->shndx(), NULL);
7139 // Mips_output_data_la25_stub methods.
7141 // Template for standard LA25 stub.
7142 template<int size, bool big_endian>
7143 const uint32_t
7144 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7146 0x3c190000, // lui $25,%hi(func)
7147 0x08000000, // j func
7148 0x27390000, // add $25,$25,%lo(func)
7149 0x00000000 // nop
7152 // Template for microMIPS LA25 stub.
7153 template<int size, bool big_endian>
7154 const uint32_t
7155 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7157 0x41b9, 0x0000, // lui t9,%hi(func)
7158 0xd400, 0x0000, // j func
7159 0x3339, 0x0000, // addiu t9,t9,%lo(func)
7160 0x0000, 0x0000 // nop
7163 // Create la25 stub for a symbol.
7165 template<int size, bool big_endian>
7166 void
7167 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7168 Symbol_table* symtab, Target_mips<size, big_endian>* target,
7169 Mips_symbol<size>* gsym)
7171 if (!gsym->has_la25_stub())
7173 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
7174 this->symbols_.push_back(gsym);
7175 this->create_stub_symbol(gsym, symtab, target, 16);
7179 // Create a symbol for SYM stub's value and size, to help make the disassembly
7180 // easier to read.
7182 template<int size, bool big_endian>
7183 void
7184 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7185 Mips_symbol<size>* sym, Symbol_table* symtab,
7186 Target_mips<size, big_endian>* target, uint64_t symsize)
7188 std::string name(".pic.");
7189 name += sym->name();
7191 unsigned int offset = sym->la25_stub_offset();
7192 if (sym->is_micromips())
7193 offset |= 1;
7195 // Make it a local function.
7196 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7197 Symbol_table::PREDEFINED,
7198 target->la25_stub_section(),
7199 offset, symsize, elfcpp::STT_FUNC,
7200 elfcpp::STB_LOCAL,
7201 elfcpp::STV_DEFAULT, 0,
7202 false, false);
7203 new_sym->set_is_forced_local();
7206 // Write out la25 stubs. This uses the hand-coded instructions above,
7207 // and adjusts them as needed.
7209 template<int size, bool big_endian>
7210 void
7211 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7213 const off_t offset = this->offset();
7214 const section_size_type oview_size =
7215 convert_to_section_size_type(this->data_size());
7216 unsigned char* const oview = of->get_output_view(offset, oview_size);
7218 for (typename std::vector<Mips_symbol<size>*>::iterator
7219 p = this->symbols_.begin();
7220 p != this->symbols_.end();
7221 ++p)
7223 Mips_symbol<size>* sym = *p;
7224 unsigned char* pov = oview + sym->la25_stub_offset();
7226 Mips_address target = sym->value();
7227 if (!sym->is_micromips())
7229 elfcpp::Swap<32, big_endian>::writeval(pov,
7230 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7231 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7232 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7233 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7234 la25_stub_entry[2] | (target & 0xffff));
7235 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7237 else
7239 target |= 1;
7240 // First stub instruction. Paste high 16-bits of the target.
7241 elfcpp::Swap<16, big_endian>::writeval(pov,
7242 la25_stub_micromips_entry[0]);
7243 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7244 ((target + 0x8000) >> 16) & 0xffff);
7245 // Second stub instruction. Paste low 26-bits of the target, shifted
7246 // right by 1.
7247 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7248 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7249 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7250 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7251 // Third stub instruction. Paste low 16-bits of the target.
7252 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7253 la25_stub_micromips_entry[4]);
7254 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7255 // Fourth stub instruction.
7256 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7257 la25_stub_micromips_entry[6]);
7258 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7259 la25_stub_micromips_entry[7]);
7263 of->write_output_view(offset, oview_size, oview);
7266 // Mips_output_data_plt methods.
7268 // The format of the first PLT entry in an O32 executable.
7269 template<int size, bool big_endian>
7270 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7272 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
7273 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
7274 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
7275 0x031cc023, // subu $24, $24, $28
7276 0x03e07825, // or $15, $31, zero
7277 0x0018c082, // srl $24, $24, 2
7278 0x0320f809, // jalr $25
7279 0x2718fffe // subu $24, $24, 2
7282 // The format of the first PLT entry in an N32 executable. Different
7283 // because gp ($28) is not available; we use t2 ($14) instead.
7284 template<int size, bool big_endian>
7285 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7287 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7288 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
7289 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7290 0x030ec023, // subu $24, $24, $14
7291 0x03e07825, // or $15, $31, zero
7292 0x0018c082, // srl $24, $24, 2
7293 0x0320f809, // jalr $25
7294 0x2718fffe // subu $24, $24, 2
7297 // The format of the first PLT entry in an N64 executable. Different
7298 // from N32 because of the increased size of GOT entries.
7299 template<int size, bool big_endian>
7300 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7302 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7303 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
7304 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7305 0x030ec023, // subu $24, $24, $14
7306 0x03e07825, // or $15, $31, zero
7307 0x0018c0c2, // srl $24, $24, 3
7308 0x0320f809, // jalr $25
7309 0x2718fffe // subu $24, $24, 2
7312 // The format of the microMIPS first PLT entry in an O32 executable.
7313 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7314 // of the GOTPLT entry handled, so this stub may only be used when
7315 // all the subsequent PLT entries are microMIPS code too.
7317 // The trailing NOP is for alignment and correct disassembly only.
7318 template<int size, bool big_endian>
7319 const uint32_t Mips_output_data_plt<size, big_endian>::
7320 plt0_entry_micromips_o32[] =
7322 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
7323 0xff23, 0x0000, // lw $25, 0($3)
7324 0x0535, // subu $2, $2, $3
7325 0x2525, // srl $2, $2, 2
7326 0x3302, 0xfffe, // subu $24, $2, 2
7327 0x0dff, // move $15, $31
7328 0x45f9, // jalrs $25
7329 0x0f83, // move $28, $3
7330 0x0c00 // nop
7333 // The format of the microMIPS first PLT entry in an O32 executable
7334 // in the insn32 mode.
7335 template<int size, bool big_endian>
7336 const uint32_t Mips_output_data_plt<size, big_endian>::
7337 plt0_entry_micromips32_o32[] =
7339 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
7340 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
7341 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
7342 0x0398, 0xc1d0, // subu $24, $24, $28
7343 0x001f, 0x7a90, // or $15, $31, zero
7344 0x0318, 0x1040, // srl $24, $24, 2
7345 0x03f9, 0x0f3c, // jalr $25
7346 0x3318, 0xfffe // subu $24, $24, 2
7349 // The format of subsequent standard entries in the PLT.
7350 template<int size, bool big_endian>
7351 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7353 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7354 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7355 0x03200008, // jr $25
7356 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7359 // The format of subsequent R6 PLT entries.
7360 template<int size, bool big_endian>
7361 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7363 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7364 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7365 0x03200009, // jr $25
7366 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7369 // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
7370 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7371 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7372 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7373 // target function address in register v0.
7374 template<int size, bool big_endian>
7375 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7377 0xb303, // lw $3, 12($pc)
7378 0x651b, // move $24, $3
7379 0x9b60, // lw $3, 0($3)
7380 0xeb00, // jr $3
7381 0x653b, // move $25, $3
7382 0x6500, // nop
7383 0x0000, 0x0000 // .word (.got.plt entry)
7386 // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
7387 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7388 template<int size, bool big_endian>
7389 const uint32_t Mips_output_data_plt<size, big_endian>::
7390 plt_entry_micromips_o32[] =
7392 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
7393 0xff22, 0x0000, // lw $25, 0($2)
7394 0x4599, // jr $25
7395 0x0f02 // move $24, $2
7398 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7399 template<int size, bool big_endian>
7400 const uint32_t Mips_output_data_plt<size, big_endian>::
7401 plt_entry_micromips32_o32[] =
7403 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
7404 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
7405 0x0019, 0x0f3c, // jr $25
7406 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
7409 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7411 template<int size, bool big_endian>
7412 void
7413 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7414 unsigned int r_type)
7416 gold_assert(!gsym->has_plt_offset());
7418 // Final PLT offset for a symbol will be set in method set_plt_offsets().
7419 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7420 + sizeof(plt0_entry_o32));
7421 this->symbols_.push_back(gsym);
7423 // Record whether the relocation requires a standard MIPS
7424 // or a compressed code entry.
7425 if (jal_reloc(r_type))
7427 if (r_type == elfcpp::R_MIPS_26)
7428 gsym->set_needs_mips_plt(true);
7429 else
7430 gsym->set_needs_comp_plt(true);
7433 section_offset_type got_offset = this->got_plt_->current_data_size();
7435 // Every PLT entry needs a GOT entry which points back to the PLT
7436 // entry (this will be changed by the dynamic linker, normally
7437 // lazily when the function is called).
7438 this->got_plt_->set_current_data_size(got_offset + size/8);
7440 gsym->set_needs_dynsym_entry();
7441 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7442 got_offset);
7445 // Set final PLT offsets. For each symbol, determine whether standard or
7446 // compressed (MIPS16 or microMIPS) PLT entry is used.
7448 template<int size, bool big_endian>
7449 void
7450 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7452 // The sizes of individual PLT entries.
7453 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7454 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7455 ? this->compressed_plt_entry_size() : 0);
7457 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7458 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7460 Mips_symbol<size>* mips_sym = *p;
7462 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7463 // so always use a standard entry there.
7465 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7466 // all MIPS16 calls will go via that stub, and there is no benefit
7467 // to having a MIPS16 entry. And in the case of call_stub a
7468 // standard entry actually has to be used as the stub ends with a J
7469 // instruction.
7470 if (this->target_->is_output_newabi()
7471 || mips_sym->has_mips16_call_stub()
7472 || mips_sym->has_mips16_call_fp_stub())
7474 mips_sym->set_needs_mips_plt(true);
7475 mips_sym->set_needs_comp_plt(false);
7478 // Otherwise, if there are no direct calls to the function, we
7479 // have a free choice of whether to use standard or compressed
7480 // entries. Prefer microMIPS entries if the object is known to
7481 // contain microMIPS code, so that it becomes possible to create
7482 // pure microMIPS binaries. Prefer standard entries otherwise,
7483 // because MIPS16 ones are no smaller and are usually slower.
7484 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7486 if (this->target_->is_output_micromips())
7487 mips_sym->set_needs_comp_plt(true);
7488 else
7489 mips_sym->set_needs_mips_plt(true);
7492 if (mips_sym->needs_mips_plt())
7494 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7495 this->plt_mips_offset_ += plt_mips_entry_size;
7497 if (mips_sym->needs_comp_plt())
7499 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7500 this->plt_comp_offset_ += plt_comp_entry_size;
7504 // Figure out the size of the PLT header if we know that we are using it.
7505 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7506 this->plt_header_size_ = this->get_plt_header_size();
7509 // Write out the PLT. This uses the hand-coded instructions above,
7510 // and adjusts them as needed.
7512 template<int size, bool big_endian>
7513 void
7514 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7516 const off_t offset = this->offset();
7517 const section_size_type oview_size =
7518 convert_to_section_size_type(this->data_size());
7519 unsigned char* const oview = of->get_output_view(offset, oview_size);
7521 const off_t gotplt_file_offset = this->got_plt_->offset();
7522 const section_size_type gotplt_size =
7523 convert_to_section_size_type(this->got_plt_->data_size());
7524 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7525 gotplt_size);
7526 unsigned char* pov = oview;
7528 Mips_address plt_address = this->address();
7530 // Calculate the address of .got.plt.
7531 Mips_address gotplt_addr = this->got_plt_->address();
7532 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7533 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7535 // The PLT sequence is not safe for N64 if .got.plt's address can
7536 // not be loaded in two instructions.
7537 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7538 || ~(gotplt_addr | 0x7fffffff) == 0);
7540 // Write the PLT header.
7541 const uint32_t* plt0_entry = this->get_plt_header_entry();
7542 if (plt0_entry == plt0_entry_micromips_o32)
7544 // Write microMIPS PLT header.
7545 gold_assert(gotplt_addr % 4 == 0);
7547 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7549 // ADDIUPC has a span of +/-16MB, check we're in range.
7550 if (gotpc_offset + 0x1000000 >= 0x2000000)
7552 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7553 "ADDIUPC"), (long)gotpc_offset);
7554 return;
7557 elfcpp::Swap<16, big_endian>::writeval(pov,
7558 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7559 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7560 (gotpc_offset >> 2) & 0xffff);
7561 pov += 4;
7562 for (unsigned int i = 2;
7563 i < (sizeof(plt0_entry_micromips_o32)
7564 / sizeof(plt0_entry_micromips_o32[0]));
7565 i++)
7567 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7568 pov += 2;
7571 else if (plt0_entry == plt0_entry_micromips32_o32)
7573 // Write microMIPS PLT header in insn32 mode.
7574 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7575 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7576 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7577 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7578 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7579 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7580 pov += 12;
7581 for (unsigned int i = 6;
7582 i < (sizeof(plt0_entry_micromips32_o32)
7583 / sizeof(plt0_entry_micromips32_o32[0]));
7584 i++)
7586 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7587 pov += 2;
7590 else
7592 // Write standard PLT header.
7593 elfcpp::Swap<32, big_endian>::writeval(pov,
7594 plt0_entry[0] | gotplt_addr_high);
7595 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7596 plt0_entry[1] | gotplt_addr_low);
7597 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7598 plt0_entry[2] | gotplt_addr_low);
7599 pov += 12;
7600 for (int i = 3; i < 8; i++)
7602 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7603 pov += 4;
7608 unsigned char* gotplt_pov = gotplt_view;
7609 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7611 // The first two entries in .got.plt are reserved.
7612 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7613 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7615 unsigned int gotplt_offset = 2 * got_entry_size;
7616 gotplt_pov += 2 * got_entry_size;
7618 // Calculate the address of the PLT header.
7619 Mips_address header_address = (plt_address
7620 + (this->is_plt_header_compressed() ? 1 : 0));
7622 // Initialize compressed PLT area view.
7623 unsigned char* pov2 = pov + this->plt_mips_offset_;
7625 // Write the PLT entries.
7626 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7627 p = this->symbols_.begin();
7628 p != this->symbols_.end();
7629 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7631 Mips_symbol<size>* mips_sym = *p;
7633 // Calculate the address of the .got.plt entry.
7634 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7635 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7636 & 0xffff);
7637 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7639 // Initially point the .got.plt entry at the PLT header.
7640 if (this->target_->is_output_n64())
7641 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7642 else
7643 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7645 // Now handle the PLT itself. First the standard entry.
7646 if (mips_sym->has_mips_plt_offset())
7648 // Pick the load opcode (LW or LD).
7649 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7650 : 0x8c000000;
7652 const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7653 : plt_entry;
7655 // Fill in the PLT entry itself.
7656 elfcpp::Swap<32, big_endian>::writeval(pov,
7657 entry[0] | gotplt_entry_addr_hi);
7658 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7659 entry[1] | gotplt_entry_addr_lo | load);
7660 elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
7661 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7662 entry[3] | gotplt_entry_addr_lo);
7663 pov += 16;
7666 // Now the compressed entry. They come after any standard ones.
7667 if (mips_sym->has_comp_plt_offset())
7669 if (!this->target_->is_output_micromips())
7671 // Write MIPS16 PLT entry.
7672 const uint32_t* plt_entry = plt_entry_mips16_o32;
7674 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7675 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7676 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7677 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7678 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7679 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7680 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7681 gotplt_entry_addr);
7682 pov2 += 16;
7684 else if (this->target_->use_32bit_micromips_instructions())
7686 // Write microMIPS PLT entry in insn32 mode.
7687 const uint32_t* plt_entry = plt_entry_micromips32_o32;
7689 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7690 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7691 gotplt_entry_addr_hi);
7692 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7693 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7694 gotplt_entry_addr_lo);
7695 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7696 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7697 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7698 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7699 gotplt_entry_addr_lo);
7700 pov2 += 16;
7702 else
7704 // Write microMIPS PLT entry.
7705 const uint32_t* plt_entry = plt_entry_micromips_o32;
7707 gold_assert(gotplt_entry_addr % 4 == 0);
7709 Mips_address loc_address = plt_address + pov2 - oview;
7710 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7712 // ADDIUPC has a span of +/-16MB, check we're in range.
7713 if (gotpc_offset + 0x1000000 >= 0x2000000)
7715 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7716 "range of ADDIUPC"), (long)gotpc_offset);
7717 return;
7720 elfcpp::Swap<16, big_endian>::writeval(pov2,
7721 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7722 elfcpp::Swap<16, big_endian>::writeval(
7723 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7724 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7725 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7726 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7727 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7728 pov2 += 12;
7733 // Check the number of bytes written for standard entries.
7734 gold_assert(static_cast<section_size_type>(
7735 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7736 // Check the number of bytes written for compressed entries.
7737 gold_assert((static_cast<section_size_type>(pov2 - pov)
7738 == this->plt_comp_offset_));
7739 // Check the total number of bytes written.
7740 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7742 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7743 == gotplt_size);
7745 of->write_output_view(offset, oview_size, oview);
7746 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7749 // Mips_output_data_mips_stubs methods.
7751 // The format of the lazy binding stub when dynamic symbol count is less than
7752 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7753 template<int size, bool big_endian>
7754 const uint32_t
7755 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7757 0x8f998010, // lw t9,0x8010(gp)
7758 0x03e07825, // or t7,ra,zero
7759 0x0320f809, // jalr t9,ra
7760 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7763 // The format of the lazy binding stub when dynamic symbol count is less than
7764 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7765 template<int size, bool big_endian>
7766 const uint32_t
7767 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7769 0xdf998010, // ld t9,0x8010(gp)
7770 0x03e07825, // or t7,ra,zero
7771 0x0320f809, // jalr t9,ra
7772 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7775 // The format of the lazy binding stub when dynamic symbol count is less than
7776 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7777 template<int size, bool big_endian>
7778 const uint32_t
7779 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7781 0x8f998010, // lw t9,0x8010(gp)
7782 0x03e07825, // or t7,ra,zero
7783 0x0320f809, // jalr t9,ra
7784 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7787 // The format of the lazy binding stub when dynamic symbol count is less than
7788 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7789 template<int size, bool big_endian>
7790 const uint32_t
7791 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7793 0xdf998010, // ld t9,0x8010(gp)
7794 0x03e07825, // or t7,ra,zero
7795 0x0320f809, // jalr t9,ra
7796 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7799 // The format of the lazy binding stub when dynamic symbol count is greater than
7800 // 64K, and ABI is not N64.
7801 template<int size, bool big_endian>
7802 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7804 0x8f998010, // lw t9,0x8010(gp)
7805 0x03e07825, // or t7,ra,zero
7806 0x3c180000, // lui t8,DYN_INDEX
7807 0x0320f809, // jalr t9,ra
7808 0x37180000 // ori t8,t8,DYN_INDEX
7811 // The format of the lazy binding stub when dynamic symbol count is greater than
7812 // 64K, and ABI is N64.
7813 template<int size, bool big_endian>
7814 const uint32_t
7815 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7817 0xdf998010, // ld t9,0x8010(gp)
7818 0x03e07825, // or t7,ra,zero
7819 0x3c180000, // lui t8,DYN_INDEX
7820 0x0320f809, // jalr t9,ra
7821 0x37180000 // ori t8,t8,DYN_INDEX
7824 // microMIPS stubs.
7826 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7827 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7828 template<int size, bool big_endian>
7829 const uint32_t
7830 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7832 0xff3c, 0x8010, // lw t9,0x8010(gp)
7833 0x0dff, // move t7,ra
7834 0x45d9, // jalr t9
7835 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7838 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7839 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7840 template<int size, bool big_endian>
7841 const uint32_t
7842 Mips_output_data_mips_stubs<size, big_endian>::
7843 lazy_stub_micromips_normal_1_n64[] =
7845 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7846 0x0dff, // move t7,ra
7847 0x45d9, // jalr t9
7848 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7851 // The format of the microMIPS lazy binding stub when dynamic symbol
7852 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7853 // and ABI is not N64.
7854 template<int size, bool big_endian>
7855 const uint32_t
7856 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7858 0xff3c, 0x8010, // lw t9,0x8010(gp)
7859 0x0dff, // move t7,ra
7860 0x45d9, // jalr t9
7861 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7864 // The format of the microMIPS lazy binding stub when dynamic symbol
7865 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7866 // and ABI is N64.
7867 template<int size, bool big_endian>
7868 const uint32_t
7869 Mips_output_data_mips_stubs<size, big_endian>::
7870 lazy_stub_micromips_normal_2_n64[] =
7872 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7873 0x0dff, // move t7,ra
7874 0x45d9, // jalr t9
7875 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7878 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7879 // greater than 64K, and ABI is not N64.
7880 template<int size, bool big_endian>
7881 const uint32_t
7882 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7884 0xff3c, 0x8010, // lw t9,0x8010(gp)
7885 0x0dff, // move t7,ra
7886 0x41b8, 0x0000, // lui t8,DYN_INDEX
7887 0x45d9, // jalr t9
7888 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7891 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7892 // greater than 64K, and ABI is N64.
7893 template<int size, bool big_endian>
7894 const uint32_t
7895 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7897 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7898 0x0dff, // move t7,ra
7899 0x41b8, 0x0000, // lui t8,DYN_INDEX
7900 0x45d9, // jalr t9
7901 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7904 // 32-bit microMIPS stubs.
7906 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7907 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7908 // can use only 32-bit instructions.
7909 template<int size, bool big_endian>
7910 const uint32_t
7911 Mips_output_data_mips_stubs<size, big_endian>::
7912 lazy_stub_micromips32_normal_1[] =
7914 0xff3c, 0x8010, // lw t9,0x8010(gp)
7915 0x001f, 0x7a90, // or t7,ra,zero
7916 0x03f9, 0x0f3c, // jalr ra,t9
7917 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7920 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7921 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7922 // use only 32-bit instructions.
7923 template<int size, bool big_endian>
7924 const uint32_t
7925 Mips_output_data_mips_stubs<size, big_endian>::
7926 lazy_stub_micromips32_normal_1_n64[] =
7928 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7929 0x001f, 0x7a90, // or t7,ra,zero
7930 0x03f9, 0x0f3c, // jalr ra,t9
7931 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7934 // The format of the microMIPS lazy binding stub when dynamic symbol
7935 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7936 // ABI is not N64, and we can use only 32-bit instructions.
7937 template<int size, bool big_endian>
7938 const uint32_t
7939 Mips_output_data_mips_stubs<size, big_endian>::
7940 lazy_stub_micromips32_normal_2[] =
7942 0xff3c, 0x8010, // lw t9,0x8010(gp)
7943 0x001f, 0x7a90, // or t7,ra,zero
7944 0x03f9, 0x0f3c, // jalr ra,t9
7945 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7948 // The format of the microMIPS lazy binding stub when dynamic symbol
7949 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7950 // ABI is N64, and we can use only 32-bit instructions.
7951 template<int size, bool big_endian>
7952 const uint32_t
7953 Mips_output_data_mips_stubs<size, big_endian>::
7954 lazy_stub_micromips32_normal_2_n64[] =
7956 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7957 0x001f, 0x7a90, // or t7,ra,zero
7958 0x03f9, 0x0f3c, // jalr ra,t9
7959 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7962 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7963 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7964 template<int size, bool big_endian>
7965 const uint32_t
7966 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7968 0xff3c, 0x8010, // lw t9,0x8010(gp)
7969 0x001f, 0x7a90, // or t7,ra,zero
7970 0x41b8, 0x0000, // lui t8,DYN_INDEX
7971 0x03f9, 0x0f3c, // jalr ra,t9
7972 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7975 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7976 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7977 template<int size, bool big_endian>
7978 const uint32_t
7979 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
7981 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7982 0x001f, 0x7a90, // or t7,ra,zero
7983 0x41b8, 0x0000, // lui t8,DYN_INDEX
7984 0x03f9, 0x0f3c, // jalr ra,t9
7985 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7988 // Create entry for a symbol.
7990 template<int size, bool big_endian>
7991 void
7992 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7993 Mips_symbol<size>* gsym)
7995 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7997 this->symbols_.insert(gsym);
7998 gsym->set_has_lazy_stub(true);
8002 // Remove entry for a symbol.
8004 template<int size, bool big_endian>
8005 void
8006 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8007 Mips_symbol<size>* gsym)
8009 if (gsym->has_lazy_stub())
8011 this->symbols_.erase(gsym);
8012 gsym->set_has_lazy_stub(false);
8016 // Set stub offsets for symbols. This method expects that the number of
8017 // entries in dynamic symbol table is set.
8019 template<int size, bool big_endian>
8020 void
8021 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8023 gold_assert(this->dynsym_count_ != -1U);
8025 if (this->stub_offsets_are_set_)
8026 return;
8028 unsigned int stub_size = this->stub_size();
8029 unsigned int offset = 0;
8030 for (typename Mips_stubs_entry_set::const_iterator
8031 p = this->symbols_.begin();
8032 p != this->symbols_.end();
8033 ++p, offset += stub_size)
8035 Mips_symbol<size>* mips_sym = *p;
8036 mips_sym->set_lazy_stub_offset(offset);
8038 this->stub_offsets_are_set_ = true;
8041 template<int size, bool big_endian>
8042 void
8043 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8045 for (typename Mips_stubs_entry_set::const_iterator
8046 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8048 Mips_symbol<size>* sym = *p;
8049 if (sym->is_from_dynobj())
8050 sym->set_needs_dynsym_value();
8054 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
8055 // adjusts them as needed.
8057 template<int size, bool big_endian>
8058 void
8059 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
8061 const off_t offset = this->offset();
8062 const section_size_type oview_size =
8063 convert_to_section_size_type(this->data_size());
8064 unsigned char* const oview = of->get_output_view(offset, oview_size);
8066 bool big_stub = this->dynsym_count_ > 0x10000;
8068 unsigned char* pov = oview;
8069 for (typename Mips_stubs_entry_set::const_iterator
8070 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8072 Mips_symbol<size>* sym = *p;
8073 const uint32_t* lazy_stub;
8074 bool n64 = this->target_->is_output_n64();
8076 if (!this->target_->is_output_micromips())
8078 // Write standard (non-microMIPS) stub.
8079 if (!big_stub)
8081 if (sym->dynsym_index() & ~0x7fff)
8082 // Dynsym index is between 32K and 64K.
8083 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
8084 else
8085 // Dynsym index is less than 32K.
8086 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8088 else
8089 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8091 unsigned int i = 0;
8092 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8093 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8094 pov += 8;
8096 i += 2;
8097 if (big_stub)
8099 // LUI instruction of the big stub. Paste high 16 bits of the
8100 // dynsym index.
8101 elfcpp::Swap<32, big_endian>::writeval(pov,
8102 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
8103 pov += 4;
8104 i += 1;
8106 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8107 // Last stub instruction. Paste low 16 bits of the dynsym index.
8108 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8109 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8110 pov += 8;
8112 else if (this->target_->use_32bit_micromips_instructions())
8114 // Write microMIPS stub in insn32 mode.
8115 if (!big_stub)
8117 if (sym->dynsym_index() & ~0x7fff)
8118 // Dynsym index is between 32K and 64K.
8119 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8120 : lazy_stub_micromips32_normal_2;
8121 else
8122 // Dynsym index is less than 32K.
8123 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8124 : lazy_stub_micromips32_normal_1;
8126 else
8127 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8128 : lazy_stub_micromips32_big;
8130 unsigned int i = 0;
8131 // First stub instruction. We emit 32-bit microMIPS instructions by
8132 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8133 // the instruction where the opcode is must always come first, for
8134 // both little and big endian.
8135 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8136 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8137 // Second stub instruction.
8138 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8139 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8140 pov += 8;
8141 i += 4;
8142 if (big_stub)
8144 // LUI instruction of the big stub. Paste high 16 bits of the
8145 // dynsym index.
8146 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8147 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8148 (sym->dynsym_index() >> 16) & 0x7fff);
8149 pov += 4;
8150 i += 2;
8152 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8153 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8154 // Last stub instruction. Paste low 16 bits of the dynsym index.
8155 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8156 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8157 sym->dynsym_index() & 0xffff);
8158 pov += 8;
8160 else
8162 // Write microMIPS stub.
8163 if (!big_stub)
8165 if (sym->dynsym_index() & ~0x7fff)
8166 // Dynsym index is between 32K and 64K.
8167 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8168 : lazy_stub_micromips_normal_2;
8169 else
8170 // Dynsym index is less than 32K.
8171 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8172 : lazy_stub_micromips_normal_1;
8174 else
8175 lazy_stub = n64 ? lazy_stub_micromips_big_n64
8176 : lazy_stub_micromips_big;
8178 unsigned int i = 0;
8179 // First stub instruction. We emit 32-bit microMIPS instructions by
8180 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8181 // the instruction where the opcode is must always come first, for
8182 // both little and big endian.
8183 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8184 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8185 // Second stub instruction.
8186 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8187 pov += 6;
8188 i += 3;
8189 if (big_stub)
8191 // LUI instruction of the big stub. Paste high 16 bits of the
8192 // dynsym index.
8193 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8194 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8195 (sym->dynsym_index() >> 16) & 0x7fff);
8196 pov += 4;
8197 i += 2;
8199 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8200 // Last stub instruction. Paste low 16 bits of the dynsym index.
8201 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8202 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8203 sym->dynsym_index() & 0xffff);
8204 pov += 6;
8208 // We always allocate 20 bytes for every stub, because final dynsym count is
8209 // not known in method do_finalize_sections. There are 4 unused bytes per
8210 // stub if final dynsym count is less than 0x10000.
8211 unsigned int used = pov - oview;
8212 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8213 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8215 // Fill the unused space with zeroes.
8216 // TODO(sasa): Can we strip unused bytes during the relaxation?
8217 if (unused > 0)
8218 memset(pov, 0, unused);
8220 of->write_output_view(offset, oview_size, oview);
8223 // Mips_output_section_reginfo methods.
8225 template<int size, bool big_endian>
8226 void
8227 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8229 off_t offset = this->offset();
8230 off_t data_size = this->data_size();
8232 unsigned char* view = of->get_output_view(offset, data_size);
8233 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8234 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8235 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8236 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8237 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8238 // Write the gp value.
8239 elfcpp::Swap<size, big_endian>::writeval(view + 20,
8240 this->target_->gp_value());
8242 of->write_output_view(offset, data_size, view);
8245 // Mips_output_section_options methods.
8247 template<int size, bool big_endian>
8248 void
8249 Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
8251 off_t offset = this->offset();
8252 const section_size_type oview_size =
8253 convert_to_section_size_type(this->data_size());
8254 unsigned char* view = of->get_output_view(offset, oview_size);
8255 const unsigned char* end = view + oview_size;
8257 while (view + 8 <= end)
8259 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8260 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8261 if (sz < 8)
8263 gold_error(_("Warning: bad `%s' option size %u smaller "
8264 "than its header in output section"),
8265 this->name(), sz);
8266 break;
8269 // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
8270 if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
8271 elfcpp::Swap<size, big_endian>::writeval(view + 32,
8272 this->target_->gp_value());
8273 else if (kind == elfcpp::ODK_REGINFO)
8274 elfcpp::Swap<size, big_endian>::writeval(view + 28,
8275 this->target_->gp_value());
8277 view += sz;
8280 of->write_output_view(offset, oview_size, view);
8283 // Mips_output_section_abiflags methods.
8285 template<int size, bool big_endian>
8286 void
8287 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8289 off_t offset = this->offset();
8290 off_t data_size = this->data_size();
8292 unsigned char* view = of->get_output_view(offset, data_size);
8293 elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8294 elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8295 elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8296 elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8297 elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8298 elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8299 elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8300 elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8301 elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8302 elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8303 elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8305 of->write_output_view(offset, data_size, view);
8308 // Mips_copy_relocs methods.
8310 // Emit any saved relocs.
8312 template<int sh_type, int size, bool big_endian>
8313 void
8314 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8315 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8316 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8318 for (typename Copy_relocs<sh_type, size, big_endian>::
8319 Copy_reloc_entries::iterator p = this->entries_.begin();
8320 p != this->entries_.end();
8321 ++p)
8322 emit_entry(*p, reloc_section, symtab, layout, target);
8324 // We no longer need the saved information.
8325 this->entries_.clear();
8328 // Emit the reloc if appropriate.
8330 template<int sh_type, int size, bool big_endian>
8331 void
8332 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8333 Copy_reloc_entry& entry,
8334 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8335 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8337 // If the symbol is no longer defined in a dynamic object, then we
8338 // emitted a COPY relocation, and we do not want to emit this
8339 // dynamic relocation.
8340 if (!entry.sym_->is_from_dynobj())
8341 return;
8343 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8344 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8345 || entry.reloc_type_ == elfcpp::R_MIPS_64);
8347 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8348 if (can_make_dynamic && !sym->has_static_relocs())
8350 Mips_relobj<size, big_endian>* object =
8351 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8352 target->got_section(symtab, layout)->record_global_got_symbol(
8353 sym, object, entry.reloc_type_, true, false);
8354 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8355 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8356 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8357 else
8358 target->rel_dyn_section(layout)->add_symbolless_global_addend(
8359 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8360 entry.shndx_, entry.address_);
8362 else
8363 this->make_copy_reloc(symtab, layout,
8364 static_cast<Sized_symbol<size>*>(entry.sym_),
8365 entry.relobj_,
8366 reloc_section);
8369 // Target_mips methods.
8371 // Return the value to use for a dynamic symbol which requires special
8372 // treatment. This is how we support equality comparisons of function
8373 // pointers across shared library boundaries, as described in the
8374 // processor specific ABI supplement.
8376 template<int size, bool big_endian>
8377 uint64_t
8378 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8380 uint64_t value = 0;
8381 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8383 if (!mips_sym->has_lazy_stub())
8385 if (mips_sym->has_plt_offset())
8387 // We distinguish between PLT entries and lazy-binding stubs by
8388 // giving the former an st_other value of STO_MIPS_PLT. Set the
8389 // value to the stub address if there are any relocations in the
8390 // binary where pointer equality matters.
8391 if (mips_sym->pointer_equality_needed())
8393 // Prefer a standard MIPS PLT entry.
8394 if (mips_sym->has_mips_plt_offset())
8395 value = this->plt_section()->mips_entry_address(mips_sym);
8396 else
8397 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8399 else
8400 value = 0;
8403 else
8405 // First, set stub offsets for symbols. This method expects that the
8406 // number of entries in dynamic symbol table is set.
8407 this->mips_stubs_section()->set_lazy_stub_offsets();
8409 // The run-time linker uses the st_value field of the symbol
8410 // to reset the global offset table entry for this external
8411 // to its stub address when unlinking a shared object.
8412 value = this->mips_stubs_section()->stub_address(mips_sym);
8415 if (mips_sym->has_mips16_fn_stub())
8417 // If we have a MIPS16 function with a stub, the dynamic symbol must
8418 // refer to the stub, since only the stub uses the standard calling
8419 // conventions.
8420 value = mips_sym->template
8421 get_mips16_fn_stub<big_endian>()->output_address();
8424 return value;
8427 // Get the dynamic reloc section, creating it if necessary. It's always
8428 // .rel.dyn, even for MIPS64.
8430 template<int size, bool big_endian>
8431 typename Target_mips<size, big_endian>::Reloc_section*
8432 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8434 if (this->rel_dyn_ == NULL)
8436 gold_assert(layout != NULL);
8437 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8438 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8439 elfcpp::SHF_ALLOC, this->rel_dyn_,
8440 ORDER_DYNAMIC_RELOCS, false);
8442 // First entry in .rel.dyn has to be null.
8443 // This is hack - we define dummy output data and set its address to 0,
8444 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8445 // This ensures that the entry is null.
8446 Output_data* od = new Output_data_zero_fill(0, 0);
8447 od->set_address(0);
8448 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8450 return this->rel_dyn_;
8453 // Get the GOT section, creating it if necessary.
8455 template<int size, bool big_endian>
8456 Mips_output_data_got<size, big_endian>*
8457 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8458 Layout* layout)
8460 if (this->got_ == NULL)
8462 gold_assert(symtab != NULL && layout != NULL);
8464 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8465 layout);
8466 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8467 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8468 elfcpp::SHF_MIPS_GPREL),
8469 this->got_, ORDER_DATA, false);
8471 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8472 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8473 Symbol_table::PREDEFINED,
8474 this->got_,
8475 0, 0, elfcpp::STT_OBJECT,
8476 elfcpp::STB_GLOBAL,
8477 elfcpp::STV_HIDDEN, 0,
8478 false, false);
8481 return this->got_;
8484 // Calculate value of _gp symbol.
8486 template<int size, bool big_endian>
8487 void
8488 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8490 gold_assert(this->gp_ == NULL);
8492 Sized_symbol<size>* gp =
8493 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8495 // Set _gp symbol if the linker script hasn't created it.
8496 if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
8498 // If there is no .got section, gp should be based on .sdata.
8499 Output_data* gp_section = (this->got_ != NULL
8500 ? this->got_->output_section()
8501 : layout->find_output_section(".sdata"));
8503 if (gp_section != NULL)
8504 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8505 "_gp", NULL, Symbol_table::PREDEFINED,
8506 gp_section, MIPS_GP_OFFSET, 0,
8507 elfcpp::STT_NOTYPE,
8508 elfcpp::STB_LOCAL,
8509 elfcpp::STV_DEFAULT,
8510 0, false, false));
8513 this->gp_ = gp;
8516 // Set the dynamic symbol indexes. INDEX is the index of the first
8517 // global dynamic symbol. Pointers to the symbols are stored into the
8518 // vector SYMS. The names are added to DYNPOOL. This returns an
8519 // updated dynamic symbol index.
8521 template<int size, bool big_endian>
8522 unsigned int
8523 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8524 std::vector<Symbol*>* dyn_symbols, unsigned int index,
8525 std::vector<Symbol*>* syms, Stringpool* dynpool,
8526 Versions* versions, Symbol_table* symtab) const
8528 std::vector<Symbol*> non_got_symbols;
8529 std::vector<Symbol*> got_symbols;
8531 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8532 &got_symbols);
8534 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8535 p != non_got_symbols.end();
8536 ++p)
8538 Symbol* sym = *p;
8540 // Note that SYM may already have a dynamic symbol index, since
8541 // some symbols appear more than once in the symbol table, with
8542 // and without a version.
8544 if (!sym->has_dynsym_index())
8546 sym->set_dynsym_index(index);
8547 ++index;
8548 syms->push_back(sym);
8549 dynpool->add(sym->name(), false, NULL);
8551 // Record any version information.
8552 if (sym->version() != NULL)
8553 versions->record_version(symtab, dynpool, sym);
8555 // If the symbol is defined in a dynamic object and is
8556 // referenced in a regular object, then mark the dynamic
8557 // object as needed. This is used to implement --as-needed.
8558 if (sym->is_from_dynobj() && sym->in_reg())
8559 sym->object()->set_is_needed();
8563 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8564 p != got_symbols.end();
8565 ++p)
8567 Symbol* sym = *p;
8568 if (!sym->has_dynsym_index())
8570 // Record any version information.
8571 if (sym->version() != NULL)
8572 versions->record_version(symtab, dynpool, sym);
8576 index = versions->finalize(symtab, index, syms);
8578 int got_sym_count = 0;
8579 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8580 p != got_symbols.end();
8581 ++p)
8583 Symbol* sym = *p;
8585 if (!sym->has_dynsym_index())
8587 ++got_sym_count;
8588 sym->set_dynsym_index(index);
8589 ++index;
8590 syms->push_back(sym);
8591 dynpool->add(sym->name(), false, NULL);
8593 // If the symbol is defined in a dynamic object and is
8594 // referenced in a regular object, then mark the dynamic
8595 // object as needed. This is used to implement --as-needed.
8596 if (sym->is_from_dynobj() && sym->in_reg())
8597 sym->object()->set_is_needed();
8601 // Set index of the first symbol that has .got entry.
8602 this->got_->set_first_global_got_dynsym_index(
8603 got_sym_count > 0 ? index - got_sym_count : -1U);
8605 if (this->mips_stubs_ != NULL)
8606 this->mips_stubs_->set_dynsym_count(index);
8608 return index;
8611 // Create a PLT entry for a global symbol referenced by r_type relocation.
8613 template<int size, bool big_endian>
8614 void
8615 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8616 Layout* layout,
8617 Mips_symbol<size>* gsym,
8618 unsigned int r_type)
8620 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8621 return;
8623 if (this->plt_ == NULL)
8625 // Create the GOT section first.
8626 this->got_section(symtab, layout);
8628 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8629 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8630 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8631 this->got_plt_, ORDER_DATA, false);
8633 // The first two entries are reserved.
8634 this->got_plt_->set_current_data_size(2 * size/8);
8636 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8637 this->got_plt_,
8638 this);
8639 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8640 (elfcpp::SHF_ALLOC
8641 | elfcpp::SHF_EXECINSTR),
8642 this->plt_, ORDER_PLT, false);
8644 // Make the sh_info field of .rel.plt point to .plt.
8645 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
8646 rel_plt_os->set_info_section(this->plt_->output_section());
8649 this->plt_->add_entry(gsym, r_type);
8653 // Get the .MIPS.stubs section, creating it if necessary.
8655 template<int size, bool big_endian>
8656 Mips_output_data_mips_stubs<size, big_endian>*
8657 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8659 if (this->mips_stubs_ == NULL)
8661 this->mips_stubs_ =
8662 new Mips_output_data_mips_stubs<size, big_endian>(this);
8663 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8664 (elfcpp::SHF_ALLOC
8665 | elfcpp::SHF_EXECINSTR),
8666 this->mips_stubs_, ORDER_PLT, false);
8668 return this->mips_stubs_;
8671 // Get the LA25 stub section, creating it if necessary.
8673 template<int size, bool big_endian>
8674 Mips_output_data_la25_stub<size, big_endian>*
8675 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8677 if (this->la25_stub_ == NULL)
8679 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8680 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8681 (elfcpp::SHF_ALLOC
8682 | elfcpp::SHF_EXECINSTR),
8683 this->la25_stub_, ORDER_TEXT, false);
8685 return this->la25_stub_;
8688 // Process the relocations to determine unreferenced sections for
8689 // garbage collection.
8691 template<int size, bool big_endian>
8692 void
8693 Target_mips<size, big_endian>::gc_process_relocs(
8694 Symbol_table* symtab,
8695 Layout* layout,
8696 Sized_relobj_file<size, big_endian>* object,
8697 unsigned int data_shndx,
8698 unsigned int sh_type,
8699 const unsigned char* prelocs,
8700 size_t reloc_count,
8701 Output_section* output_section,
8702 bool needs_special_offset_handling,
8703 size_t local_symbol_count,
8704 const unsigned char* plocal_symbols)
8706 typedef Target_mips<size, big_endian> Mips;
8708 if (sh_type == elfcpp::SHT_REL)
8710 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8711 Classify_reloc;
8713 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8714 symtab,
8715 layout,
8716 this,
8717 object,
8718 data_shndx,
8719 prelocs,
8720 reloc_count,
8721 output_section,
8722 needs_special_offset_handling,
8723 local_symbol_count,
8724 plocal_symbols);
8726 else if (sh_type == elfcpp::SHT_RELA)
8728 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8729 Classify_reloc;
8731 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8732 symtab,
8733 layout,
8734 this,
8735 object,
8736 data_shndx,
8737 prelocs,
8738 reloc_count,
8739 output_section,
8740 needs_special_offset_handling,
8741 local_symbol_count,
8742 plocal_symbols);
8744 else
8745 gold_unreachable();
8748 // Scan relocations for a section.
8750 template<int size, bool big_endian>
8751 void
8752 Target_mips<size, big_endian>::scan_relocs(
8753 Symbol_table* symtab,
8754 Layout* layout,
8755 Sized_relobj_file<size, big_endian>* object,
8756 unsigned int data_shndx,
8757 unsigned int sh_type,
8758 const unsigned char* prelocs,
8759 size_t reloc_count,
8760 Output_section* output_section,
8761 bool needs_special_offset_handling,
8762 size_t local_symbol_count,
8763 const unsigned char* plocal_symbols)
8765 typedef Target_mips<size, big_endian> Mips;
8767 if (sh_type == elfcpp::SHT_REL)
8769 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8770 Classify_reloc;
8772 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8773 symtab,
8774 layout,
8775 this,
8776 object,
8777 data_shndx,
8778 prelocs,
8779 reloc_count,
8780 output_section,
8781 needs_special_offset_handling,
8782 local_symbol_count,
8783 plocal_symbols);
8785 else if (sh_type == elfcpp::SHT_RELA)
8787 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8788 Classify_reloc;
8790 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8791 symtab,
8792 layout,
8793 this,
8794 object,
8795 data_shndx,
8796 prelocs,
8797 reloc_count,
8798 output_section,
8799 needs_special_offset_handling,
8800 local_symbol_count,
8801 plocal_symbols);
8805 template<int size, bool big_endian>
8806 bool
8807 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8809 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8810 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8811 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8812 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8813 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8814 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8815 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
8816 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
8819 // Return the MACH for a MIPS e_flags value.
8820 template<int size, bool big_endian>
8821 unsigned int
8822 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8824 switch (flags & elfcpp::EF_MIPS_MACH)
8826 case elfcpp::E_MIPS_MACH_3900:
8827 return mach_mips3900;
8829 case elfcpp::E_MIPS_MACH_4010:
8830 return mach_mips4010;
8832 case elfcpp::E_MIPS_MACH_4100:
8833 return mach_mips4100;
8835 case elfcpp::E_MIPS_MACH_4111:
8836 return mach_mips4111;
8838 case elfcpp::E_MIPS_MACH_4120:
8839 return mach_mips4120;
8841 case elfcpp::E_MIPS_MACH_4650:
8842 return mach_mips4650;
8844 case elfcpp::E_MIPS_MACH_5400:
8845 return mach_mips5400;
8847 case elfcpp::E_MIPS_MACH_5500:
8848 return mach_mips5500;
8850 case elfcpp::E_MIPS_MACH_5900:
8851 return mach_mips5900;
8853 case elfcpp::E_MIPS_MACH_9000:
8854 return mach_mips9000;
8856 case elfcpp::E_MIPS_MACH_SB1:
8857 return mach_mips_sb1;
8859 case elfcpp::E_MIPS_MACH_LS2E:
8860 return mach_mips_loongson_2e;
8862 case elfcpp::E_MIPS_MACH_LS2F:
8863 return mach_mips_loongson_2f;
8865 case elfcpp::E_MIPS_MACH_GS464:
8866 return mach_mips_gs464;
8868 case elfcpp::E_MIPS_MACH_GS464E:
8869 return mach_mips_gs464e;
8871 case elfcpp::E_MIPS_MACH_GS264E:
8872 return mach_mips_gs264e;
8874 case elfcpp::E_MIPS_MACH_OCTEON3:
8875 return mach_mips_octeon3;
8877 case elfcpp::E_MIPS_MACH_OCTEON2:
8878 return mach_mips_octeon2;
8880 case elfcpp::E_MIPS_MACH_OCTEON:
8881 return mach_mips_octeon;
8883 case elfcpp::E_MIPS_MACH_XLR:
8884 return mach_mips_xlr;
8886 default:
8887 switch (flags & elfcpp::EF_MIPS_ARCH)
8889 default:
8890 case elfcpp::E_MIPS_ARCH_1:
8891 return mach_mips3000;
8893 case elfcpp::E_MIPS_ARCH_2:
8894 return mach_mips6000;
8896 case elfcpp::E_MIPS_ARCH_3:
8897 return mach_mips4000;
8899 case elfcpp::E_MIPS_ARCH_4:
8900 return mach_mips8000;
8902 case elfcpp::E_MIPS_ARCH_5:
8903 return mach_mips5;
8905 case elfcpp::E_MIPS_ARCH_32:
8906 return mach_mipsisa32;
8908 case elfcpp::E_MIPS_ARCH_64:
8909 return mach_mipsisa64;
8911 case elfcpp::E_MIPS_ARCH_32R2:
8912 return mach_mipsisa32r2;
8914 case elfcpp::E_MIPS_ARCH_32R6:
8915 return mach_mipsisa32r6;
8917 case elfcpp::E_MIPS_ARCH_64R2:
8918 return mach_mipsisa64r2;
8920 case elfcpp::E_MIPS_ARCH_64R6:
8921 return mach_mipsisa64r6;
8925 return 0;
8928 // Return the MACH for each .MIPS.abiflags ISA Extension.
8930 template<int size, bool big_endian>
8931 unsigned int
8932 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8934 switch (isa_ext)
8936 case elfcpp::AFL_EXT_3900:
8937 return mach_mips3900;
8939 case elfcpp::AFL_EXT_4010:
8940 return mach_mips4010;
8942 case elfcpp::AFL_EXT_4100:
8943 return mach_mips4100;
8945 case elfcpp::AFL_EXT_4111:
8946 return mach_mips4111;
8948 case elfcpp::AFL_EXT_4120:
8949 return mach_mips4120;
8951 case elfcpp::AFL_EXT_4650:
8952 return mach_mips4650;
8954 case elfcpp::AFL_EXT_5400:
8955 return mach_mips5400;
8957 case elfcpp::AFL_EXT_5500:
8958 return mach_mips5500;
8960 case elfcpp::AFL_EXT_5900:
8961 return mach_mips5900;
8963 case elfcpp::AFL_EXT_10000:
8964 return mach_mips10000;
8966 case elfcpp::AFL_EXT_LOONGSON_2E:
8967 return mach_mips_loongson_2e;
8969 case elfcpp::AFL_EXT_LOONGSON_2F:
8970 return mach_mips_loongson_2f;
8972 case elfcpp::AFL_EXT_SB1:
8973 return mach_mips_sb1;
8975 case elfcpp::AFL_EXT_OCTEON:
8976 return mach_mips_octeon;
8978 case elfcpp::AFL_EXT_OCTEONP:
8979 return mach_mips_octeonp;
8981 case elfcpp::AFL_EXT_OCTEON2:
8982 return mach_mips_octeon2;
8984 case elfcpp::AFL_EXT_XLR:
8985 return mach_mips_xlr;
8987 default:
8988 return mach_mips3000;
8992 // Return the .MIPS.abiflags value representing each ISA Extension.
8994 template<int size, bool big_endian>
8995 unsigned int
8996 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
8998 switch (mips_mach)
9000 case mach_mips3900:
9001 return elfcpp::AFL_EXT_3900;
9003 case mach_mips4010:
9004 return elfcpp::AFL_EXT_4010;
9006 case mach_mips4100:
9007 return elfcpp::AFL_EXT_4100;
9009 case mach_mips4111:
9010 return elfcpp::AFL_EXT_4111;
9012 case mach_mips4120:
9013 return elfcpp::AFL_EXT_4120;
9015 case mach_mips4650:
9016 return elfcpp::AFL_EXT_4650;
9018 case mach_mips5400:
9019 return elfcpp::AFL_EXT_5400;
9021 case mach_mips5500:
9022 return elfcpp::AFL_EXT_5500;
9024 case mach_mips5900:
9025 return elfcpp::AFL_EXT_5900;
9027 case mach_mips10000:
9028 return elfcpp::AFL_EXT_10000;
9030 case mach_mips_loongson_2e:
9031 return elfcpp::AFL_EXT_LOONGSON_2E;
9033 case mach_mips_loongson_2f:
9034 return elfcpp::AFL_EXT_LOONGSON_2F;
9036 case mach_mips_sb1:
9037 return elfcpp::AFL_EXT_SB1;
9039 case mach_mips_octeon:
9040 return elfcpp::AFL_EXT_OCTEON;
9042 case mach_mips_octeonp:
9043 return elfcpp::AFL_EXT_OCTEONP;
9045 case mach_mips_octeon3:
9046 return elfcpp::AFL_EXT_OCTEON3;
9048 case mach_mips_octeon2:
9049 return elfcpp::AFL_EXT_OCTEON2;
9051 case mach_mips_xlr:
9052 return elfcpp::AFL_EXT_XLR;
9054 default:
9055 return 0;
9059 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
9061 template<int size, bool big_endian>
9062 void
9063 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9064 elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9066 int new_isa = 0;
9067 switch (e_flags & elfcpp::EF_MIPS_ARCH)
9069 case elfcpp::E_MIPS_ARCH_1:
9070 new_isa = this->level_rev(1, 0);
9071 break;
9072 case elfcpp::E_MIPS_ARCH_2:
9073 new_isa = this->level_rev(2, 0);
9074 break;
9075 case elfcpp::E_MIPS_ARCH_3:
9076 new_isa = this->level_rev(3, 0);
9077 break;
9078 case elfcpp::E_MIPS_ARCH_4:
9079 new_isa = this->level_rev(4, 0);
9080 break;
9081 case elfcpp::E_MIPS_ARCH_5:
9082 new_isa = this->level_rev(5, 0);
9083 break;
9084 case elfcpp::E_MIPS_ARCH_32:
9085 new_isa = this->level_rev(32, 1);
9086 break;
9087 case elfcpp::E_MIPS_ARCH_32R2:
9088 new_isa = this->level_rev(32, 2);
9089 break;
9090 case elfcpp::E_MIPS_ARCH_32R6:
9091 new_isa = this->level_rev(32, 6);
9092 break;
9093 case elfcpp::E_MIPS_ARCH_64:
9094 new_isa = this->level_rev(64, 1);
9095 break;
9096 case elfcpp::E_MIPS_ARCH_64R2:
9097 new_isa = this->level_rev(64, 2);
9098 break;
9099 case elfcpp::E_MIPS_ARCH_64R6:
9100 new_isa = this->level_rev(64, 6);
9101 break;
9102 default:
9103 gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9104 this->elf_mips_mach_name(e_flags));
9107 if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9109 // Decode a single value into level and revision.
9110 abiflags->isa_level = new_isa >> 3;
9111 abiflags->isa_rev = new_isa & 0x7;
9114 // Update the isa_ext if needed.
9115 if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9116 this->elf_mips_mach(e_flags)))
9117 abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9120 // Infer the content of the ABI flags based on the elf header.
9122 template<int size, bool big_endian>
9123 void
9124 Target_mips<size, big_endian>::infer_abiflags(
9125 Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9127 const Attributes_section_data* pasd = relobj->attributes_section_data();
9128 int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9129 elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9131 this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9132 if (pasd != NULL)
9134 // Read fp_abi from the .gnu.attribute section.
9135 const Object_attribute* attr =
9136 pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9137 attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9140 abiflags->fp_abi = attr_fp_abi;
9141 abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9142 abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9143 abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9144 : elfcpp::AFL_REG_64;
9146 if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9147 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9148 || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9149 && abiflags->gpr_size == elfcpp::AFL_REG_32))
9150 abiflags->cpr1_size = elfcpp::AFL_REG_32;
9151 else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9152 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9153 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9154 abiflags->cpr1_size = elfcpp::AFL_REG_64;
9156 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9157 abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9158 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9159 abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9160 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9161 abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9163 if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9164 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9165 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9166 && abiflags->isa_level >= 32
9167 && abiflags->ases != elfcpp::AFL_ASE_LOONGSON_EXT)
9168 abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9171 // Create abiflags from elf header or from .MIPS.abiflags section.
9173 template<int size, bool big_endian>
9174 void
9175 Target_mips<size, big_endian>::create_abiflags(
9176 Mips_relobj<size, big_endian>* relobj,
9177 Mips_abiflags<big_endian>* abiflags)
9179 Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9180 Mips_abiflags<big_endian> header_abiflags;
9182 this->infer_abiflags(relobj, &header_abiflags);
9184 if (sec_abiflags == NULL)
9186 // If there is no input .MIPS.abiflags section, use abiflags created
9187 // from elf header.
9188 *abiflags = header_abiflags;
9189 return;
9192 this->has_abiflags_section_ = true;
9194 // It is not possible to infer the correct ISA revision for R3 or R5
9195 // so drop down to R2 for the checks.
9196 unsigned char isa_rev = sec_abiflags->isa_rev;
9197 if (isa_rev == 3 || isa_rev == 5)
9198 isa_rev = 2;
9200 // Check compatibility between abiflags created from elf header
9201 // and abiflags from .MIPS.abiflags section in this object file.
9202 if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9203 < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9204 gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9205 relobj->name().c_str());
9206 if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9207 && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9208 gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9209 ".MIPS.abiflags"), relobj->name().c_str());
9210 if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9211 gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9212 relobj->name().c_str());
9213 // The isa_ext is allowed to be an extension of what can be inferred
9214 // from e_flags.
9215 if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9216 this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9217 gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9218 ".MIPS.abiflags"), relobj->name().c_str());
9219 if (sec_abiflags->flags2 != 0)
9220 gold_warning(_("%s: Unexpected flag in the flags2 field of "
9221 ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9222 sec_abiflags->flags2);
9223 // Use abiflags from .MIPS.abiflags section.
9224 *abiflags = *sec_abiflags;
9227 // Return the meaning of fp_abi, or "unknown" if not known.
9229 template<int size, bool big_endian>
9230 const char*
9231 Target_mips<size, big_endian>::fp_abi_string(int fp)
9233 switch (fp)
9235 case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9236 return "-mdouble-float";
9237 case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9238 return "-msingle-float";
9239 case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9240 return "-msoft-float";
9241 case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9242 return _("-mips32r2 -mfp64 (12 callee-saved)");
9243 case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9244 return "-mfpxx";
9245 case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9246 return "-mgp32 -mfp64";
9247 case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9248 return "-mgp32 -mfp64 -mno-odd-spreg";
9249 default:
9250 return "unknown";
9254 // Select fp_abi.
9256 template<int size, bool big_endian>
9258 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9259 int out_fp)
9261 if (in_fp == out_fp)
9262 return out_fp;
9264 if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9265 return in_fp;
9266 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9267 && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9268 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9269 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9270 return in_fp;
9271 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9272 && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9273 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9274 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9275 return out_fp; // Keep the current setting.
9276 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9277 && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9278 return in_fp;
9279 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9280 && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9281 return out_fp; // Keep the current setting.
9282 else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9283 gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9284 fp_abi_string(in_fp), fp_abi_string(out_fp));
9285 return out_fp;
9288 // Merge attributes from input object.
9290 template<int size, bool big_endian>
9291 void
9292 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9293 const Attributes_section_data* pasd)
9295 // Return if there is no attributes section data.
9296 if (pasd == NULL)
9297 return;
9299 // If output has no object attributes, just copy.
9300 if (this->attributes_section_data_ == NULL)
9302 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9303 return;
9306 Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9307 Object_attribute::OBJ_ATTR_GNU);
9309 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9310 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9312 // Merge Tag_compatibility attributes and any common GNU ones.
9313 this->attributes_section_data_->merge(name.c_str(), pasd);
9316 // Merge abiflags from input object.
9318 template<int size, bool big_endian>
9319 void
9320 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9321 Mips_abiflags<big_endian>* in_abiflags)
9323 // If output has no abiflags, just copy.
9324 if (this->abiflags_ == NULL)
9326 this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9327 return;
9330 this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9331 this->abiflags_->fp_abi);
9333 // Merge abiflags.
9334 this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9335 in_abiflags->isa_level);
9336 this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9337 in_abiflags->isa_rev);
9338 this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9339 in_abiflags->gpr_size);
9340 this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9341 in_abiflags->cpr1_size);
9342 this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9343 in_abiflags->cpr2_size);
9344 this->abiflags_->ases |= in_abiflags->ases;
9345 this->abiflags_->flags1 |= in_abiflags->flags1;
9348 // Check whether machine EXTENSION is an extension of machine BASE.
9349 template<int size, bool big_endian>
9350 bool
9351 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9352 unsigned int extension)
9354 if (extension == base)
9355 return true;
9357 if ((base == mach_mipsisa32)
9358 && this->mips_mach_extends(mach_mipsisa64, extension))
9359 return true;
9361 if ((base == mach_mipsisa32r2)
9362 && this->mips_mach_extends(mach_mipsisa64r2, extension))
9363 return true;
9365 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9366 if (extension == this->mips_mach_extensions_[i].first)
9368 extension = this->mips_mach_extensions_[i].second;
9369 if (extension == base)
9370 return true;
9373 return false;
9376 // Merge file header flags from input object.
9378 template<int size, bool big_endian>
9379 void
9380 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9381 elfcpp::Elf_Word in_flags)
9383 // If flags are not set yet, just copy them.
9384 if (!this->are_processor_specific_flags_set())
9386 this->set_processor_specific_flags(in_flags);
9387 this->mach_ = this->elf_mips_mach(in_flags);
9388 return;
9391 elfcpp::Elf_Word new_flags = in_flags;
9392 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9393 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9394 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9396 // Check flag compatibility.
9397 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9398 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9400 // Some IRIX 6 BSD-compatibility objects have this bit set. It
9401 // doesn't seem to matter.
9402 new_flags &= ~elfcpp::EF_MIPS_XGOT;
9403 old_flags &= ~elfcpp::EF_MIPS_XGOT;
9405 // MIPSpro generates ucode info in n64 objects. Again, we should
9406 // just be able to ignore this.
9407 new_flags &= ~elfcpp::EF_MIPS_UCODE;
9408 old_flags &= ~elfcpp::EF_MIPS_UCODE;
9410 if (new_flags == old_flags)
9412 this->set_processor_specific_flags(merged_flags);
9413 return;
9416 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9417 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9418 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9419 name.c_str());
9421 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9422 merged_flags |= elfcpp::EF_MIPS_CPIC;
9423 if (!(new_flags & elfcpp::EF_MIPS_PIC))
9424 merged_flags &= ~elfcpp::EF_MIPS_PIC;
9426 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9427 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9429 // Compare the ISAs.
9430 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9431 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9432 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9434 // Output ISA isn't the same as, or an extension of, input ISA.
9435 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9437 // Copy the architecture info from input object to output. Also copy
9438 // the 32-bit flag (if set) so that we continue to recognise
9439 // output as a 32-bit binary.
9440 this->mach_ = this->elf_mips_mach(in_flags);
9441 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9442 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9443 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9445 // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9446 this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9448 // Copy across the ABI flags if output doesn't use them
9449 // and if that was what caused us to treat input object as 32-bit.
9450 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9451 && this->mips_32bit_flags(new_flags)
9452 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9453 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9455 else
9456 // The ISAs aren't compatible.
9457 gold_error(_("%s: linking %s module with previous %s modules"),
9458 name.c_str(), this->elf_mips_mach_name(in_flags),
9459 this->elf_mips_mach_name(merged_flags));
9462 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9463 | elfcpp::EF_MIPS_32BITMODE));
9464 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9465 | elfcpp::EF_MIPS_32BITMODE));
9467 // Compare ABIs.
9468 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9470 // Only error if both are set (to different values).
9471 if ((new_flags & elfcpp::EF_MIPS_ABI)
9472 && (old_flags & elfcpp::EF_MIPS_ABI))
9473 gold_error(_("%s: ABI mismatch: linking %s module with "
9474 "previous %s modules"), name.c_str(),
9475 this->elf_mips_abi_name(in_flags),
9476 this->elf_mips_abi_name(merged_flags));
9478 new_flags &= ~elfcpp::EF_MIPS_ABI;
9479 old_flags &= ~elfcpp::EF_MIPS_ABI;
9482 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
9483 // and allow arbitrary mixing of the remaining ASEs (retain the union).
9484 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9485 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9487 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9488 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9489 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9490 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9491 int micro_mis = old_m16 && new_micro;
9492 int m16_mis = old_micro && new_m16;
9494 if (m16_mis || micro_mis)
9495 gold_error(_("%s: ASE mismatch: linking %s module with "
9496 "previous %s modules"), name.c_str(),
9497 m16_mis ? "MIPS16" : "microMIPS",
9498 m16_mis ? "microMIPS" : "MIPS16");
9500 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9502 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9503 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9506 // Compare NaN encodings.
9507 if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9509 gold_error(_("%s: linking %s module with previous %s modules"),
9510 name.c_str(),
9511 (new_flags & elfcpp::EF_MIPS_NAN2008
9512 ? "-mnan=2008" : "-mnan=legacy"),
9513 (old_flags & elfcpp::EF_MIPS_NAN2008
9514 ? "-mnan=2008" : "-mnan=legacy"));
9516 new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9517 old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9520 // Compare FP64 state.
9521 if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9523 gold_error(_("%s: linking %s module with previous %s modules"),
9524 name.c_str(),
9525 (new_flags & elfcpp::EF_MIPS_FP64
9526 ? "-mfp64" : "-mfp32"),
9527 (old_flags & elfcpp::EF_MIPS_FP64
9528 ? "-mfp64" : "-mfp32"));
9530 new_flags &= ~elfcpp::EF_MIPS_FP64;
9531 old_flags &= ~elfcpp::EF_MIPS_FP64;
9534 // Warn about any other mismatches.
9535 if (new_flags != old_flags)
9536 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9537 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9539 this->set_processor_specific_flags(merged_flags);
9542 // Adjust ELF file header.
9544 template<int size, bool big_endian>
9545 void
9546 Target_mips<size, big_endian>::do_adjust_elf_header(
9547 unsigned char* view,
9548 int len)
9550 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9552 elfcpp::Ehdr<size, big_endian> ehdr(view);
9553 unsigned char e_ident[elfcpp::EI_NIDENT];
9554 elfcpp::Elf_Word flags = this->processor_specific_flags();
9555 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9557 unsigned char ei_abiversion = 0;
9558 elfcpp::Elf_Half type = ehdr.get_e_type();
9559 if (type == elfcpp::ET_EXEC
9560 && parameters->options().copyreloc()
9561 && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9562 == elfcpp::EF_MIPS_CPIC)
9563 ei_abiversion = 1;
9565 if (this->abiflags_ != NULL
9566 && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9567 || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9568 ei_abiversion = 3;
9570 e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9571 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9572 oehdr.put_e_ident(e_ident);
9574 if (this->entry_symbol_is_compressed_)
9575 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9578 // do_make_elf_object to override the same function in the base class.
9579 // We need to use a target-specific sub-class of
9580 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9581 // Hence we need to have our own ELF object creation.
9583 template<int size, bool big_endian>
9584 Object*
9585 Target_mips<size, big_endian>::do_make_elf_object(
9586 const std::string& name,
9587 Input_file* input_file,
9588 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9590 int et = ehdr.get_e_type();
9591 // ET_EXEC files are valid input for --just-symbols/-R,
9592 // and we treat them as relocatable objects.
9593 if (et == elfcpp::ET_REL
9594 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9596 Mips_relobj<size, big_endian>* obj =
9597 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9598 obj->setup();
9599 return obj;
9601 else if (et == elfcpp::ET_DYN)
9603 // TODO(sasa): Should we create Mips_dynobj?
9604 return Target::do_make_elf_object(name, input_file, offset, ehdr);
9606 else
9608 gold_error(_("%s: unsupported ELF file type %d"),
9609 name.c_str(), et);
9610 return NULL;
9614 // Finalize the sections.
9616 template <int size, bool big_endian>
9617 void
9618 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9619 const Input_objects* input_objects,
9620 Symbol_table* symtab)
9622 const bool relocatable = parameters->options().relocatable();
9624 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9625 // DT_FINI have correct values.
9626 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9627 symtab->lookup(parameters->options().init()));
9628 if (init != NULL && (init->is_mips16() || init->is_micromips()))
9629 init->set_value(init->value() | 1);
9630 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9631 symtab->lookup(parameters->options().fini()));
9632 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9633 fini->set_value(fini->value() | 1);
9635 // Check whether the entry symbol is mips16 or micromips. This is needed to
9636 // adjust entry address in ELF header.
9637 Mips_symbol<size>* entry =
9638 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9639 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9640 || entry->is_micromips()));
9642 if (!parameters->doing_static_link()
9643 && (strcmp(parameters->options().hash_style(), "gnu") == 0
9644 || strcmp(parameters->options().hash_style(), "both") == 0))
9646 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9647 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
9648 // MIPS ABI requires a mapping between the GOT and the symbol table.
9649 gold_error(".gnu.hash is incompatible with the MIPS ABI");
9652 // Check whether the final section that was scanned has HI16 or GOT16
9653 // relocations without the corresponding LO16 part.
9654 if (this->got16_addends_.size() > 0)
9655 gold_error("Can't find matching LO16 reloc");
9657 Valtype gprmask = 0;
9658 Valtype cprmask1 = 0;
9659 Valtype cprmask2 = 0;
9660 Valtype cprmask3 = 0;
9661 Valtype cprmask4 = 0;
9662 bool has_reginfo_section = false;
9664 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9665 p != input_objects->relobj_end();
9666 ++p)
9668 Mips_relobj<size, big_endian>* relobj =
9669 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9671 // Check for any mips16 stub sections that we can discard.
9672 if (!relocatable)
9673 relobj->discard_mips16_stub_sections(symtab);
9675 if (!relobj->merge_processor_specific_data())
9676 continue;
9678 // Merge .reginfo contents of input objects.
9679 if (relobj->has_reginfo_section())
9681 has_reginfo_section = true;
9682 gprmask |= relobj->gprmask();
9683 cprmask1 |= relobj->cprmask1();
9684 cprmask2 |= relobj->cprmask2();
9685 cprmask3 |= relobj->cprmask3();
9686 cprmask4 |= relobj->cprmask4();
9689 // Merge processor specific flags.
9690 Mips_abiflags<big_endian> in_abiflags;
9692 this->create_abiflags(relobj, &in_abiflags);
9693 this->merge_obj_e_flags(relobj->name(),
9694 relobj->processor_specific_flags());
9695 this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9696 this->merge_obj_attributes(relobj->name(),
9697 relobj->attributes_section_data());
9700 // Create a .gnu.attributes section if we have merged any attributes
9701 // from inputs.
9702 if (this->attributes_section_data_ != NULL)
9704 Output_attributes_section_data* attributes_section =
9705 new Output_attributes_section_data(*this->attributes_section_data_);
9706 layout->add_output_section_data(".gnu.attributes",
9707 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9708 attributes_section, ORDER_INVALID, false);
9711 // Create .MIPS.abiflags output section if there is an input section.
9712 if (this->has_abiflags_section_)
9714 Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9715 new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9717 Output_section* os =
9718 layout->add_output_section_data(".MIPS.abiflags",
9719 elfcpp::SHT_MIPS_ABIFLAGS,
9720 elfcpp::SHF_ALLOC,
9721 abiflags_section, ORDER_INVALID, false);
9723 if (!relocatable && os != NULL)
9725 Output_segment* abiflags_segment =
9726 layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9727 abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9731 if (has_reginfo_section && !parameters->options().gc_sections())
9733 // Create .reginfo output section.
9734 Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9735 new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9736 cprmask1, cprmask2,
9737 cprmask3, cprmask4);
9739 Output_section* os =
9740 layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9741 elfcpp::SHF_ALLOC, reginfo_section,
9742 ORDER_INVALID, false);
9744 if (!relocatable && os != NULL)
9746 Output_segment* reginfo_segment =
9747 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9748 elfcpp::PF_R);
9749 reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9753 if (this->plt_ != NULL)
9755 // Set final PLT offsets for symbols.
9756 this->plt_section()->set_plt_offsets();
9758 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9759 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9760 // there are no standard PLT entries present.
9761 unsigned char nonvis = 0;
9762 if (this->is_output_micromips()
9763 && !this->plt_section()->has_standard_entries())
9764 nonvis = elfcpp::STO_MICROMIPS >> 2;
9765 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9766 Symbol_table::PREDEFINED,
9767 this->plt_,
9768 0, 0, elfcpp::STT_FUNC,
9769 elfcpp::STB_LOCAL,
9770 elfcpp::STV_DEFAULT, nonvis,
9771 false, false);
9774 if (this->mips_stubs_ != NULL)
9776 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9777 unsigned char nonvis = 0;
9778 if (this->is_output_micromips())
9779 nonvis = elfcpp::STO_MICROMIPS >> 2;
9780 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9781 Symbol_table::PREDEFINED,
9782 this->mips_stubs_,
9783 0, 0, elfcpp::STT_FUNC,
9784 elfcpp::STB_LOCAL,
9785 elfcpp::STV_DEFAULT, nonvis,
9786 false, false);
9789 if (!relocatable && !parameters->doing_static_link())
9790 // In case there is no .got section, create one.
9791 this->got_section(symtab, layout);
9793 // Emit any relocs we saved in an attempt to avoid generating COPY
9794 // relocs.
9795 if (this->copy_relocs_.any_saved_relocs())
9796 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9797 this);
9799 // Set _gp value.
9800 this->set_gp(layout, symtab);
9802 // Emit dynamic relocs.
9803 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9804 p != this->dyn_relocs_.end();
9805 ++p)
9806 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9808 if (this->has_got_section())
9809 this->got_section()->lay_out_got(layout, symtab, input_objects);
9811 if (this->mips_stubs_ != NULL)
9812 this->mips_stubs_->set_needs_dynsym_value();
9814 // Check for functions that might need $25 to be valid on entry.
9815 // TODO(sasa): Can we do this without iterating over all symbols?
9816 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9817 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9818 symtab));
9820 // Add NULL segment.
9821 if (!relocatable)
9822 layout->make_output_segment(elfcpp::PT_NULL, 0);
9824 // Fill in some more dynamic tags.
9825 // TODO(sasa): Add more dynamic tags.
9826 const Reloc_section* rel_plt = (this->plt_ == NULL
9827 ? NULL : this->plt_->rel_plt());
9828 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9829 this->rel_dyn_, true, false);
9831 Output_data_dynamic* const odyn = layout->dynamic_data();
9832 if (odyn != NULL
9833 && !relocatable
9834 && !parameters->doing_static_link())
9836 unsigned int d_val;
9837 // This element holds a 32-bit version id for the Runtime
9838 // Linker Interface. This will start at integer value 1.
9839 d_val = 0x01;
9840 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9842 // Dynamic flags
9843 d_val = elfcpp::RHF_NOTPOT;
9844 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9846 // Save layout for using when emitting custom dynamic tags.
9847 this->layout_ = layout;
9849 // This member holds the base address of the segment.
9850 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9852 // This member holds the number of entries in the .dynsym section.
9853 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9855 // This member holds the index of the first dynamic symbol
9856 // table entry that corresponds to an entry in the global offset table.
9857 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9859 // This member holds the number of local GOT entries.
9860 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9861 this->got_->get_local_gotno());
9863 if (this->plt_ != NULL)
9864 // DT_MIPS_PLTGOT dynamic tag
9865 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9867 if (!parameters->options().shared())
9869 this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9871 layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9872 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9873 this->rld_map_, ORDER_INVALID, false);
9875 // __RLD_MAP will be filled in by the runtime loader to contain
9876 // a pointer to the _r_debug structure.
9877 Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9878 Symbol_table::PREDEFINED,
9879 this->rld_map_,
9880 0, 0, elfcpp::STT_OBJECT,
9881 elfcpp::STB_GLOBAL,
9882 elfcpp::STV_DEFAULT, 0,
9883 false, false);
9885 if (!rld_map->is_forced_local())
9886 rld_map->set_needs_dynsym_entry();
9888 if (!parameters->options().pie())
9889 // This member holds the absolute address of the debug pointer.
9890 odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9891 else
9892 // This member holds the offset to the debug pointer,
9893 // relative to the address of the tag.
9894 odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9899 // Get the custom dynamic tag value.
9900 template<int size, bool big_endian>
9901 unsigned int
9902 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9904 switch (tag)
9906 case elfcpp::DT_MIPS_BASE_ADDRESS:
9908 // The base address of the segment.
9909 // At this point, the segment list has been sorted into final order,
9910 // so just return vaddr of the first readable PT_LOAD segment.
9911 Output_segment* seg =
9912 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9913 gold_assert(seg != NULL);
9914 return seg->vaddr();
9917 case elfcpp::DT_MIPS_SYMTABNO:
9918 // The number of entries in the .dynsym section.
9919 return this->get_dt_mips_symtabno();
9921 case elfcpp::DT_MIPS_GOTSYM:
9923 // The index of the first dynamic symbol table entry that corresponds
9924 // to an entry in the GOT.
9925 if (this->got_->first_global_got_dynsym_index() != -1U)
9926 return this->got_->first_global_got_dynsym_index();
9927 else
9928 // In case if we don't have global GOT symbols we default to setting
9929 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9930 return this->get_dt_mips_symtabno();
9933 case elfcpp::DT_MIPS_RLD_MAP_REL:
9935 // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9936 // relative to the address of the tag.
9937 Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9938 unsigned int entry_offset =
9939 odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9940 gold_assert(entry_offset != -1U);
9941 return this->rld_map_->address() - (odyn->address() + entry_offset);
9943 default:
9944 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9947 return (unsigned int)-1;
9950 // Relocate section data.
9952 template<int size, bool big_endian>
9953 void
9954 Target_mips<size, big_endian>::relocate_section(
9955 const Relocate_info<size, big_endian>* relinfo,
9956 unsigned int sh_type,
9957 const unsigned char* prelocs,
9958 size_t reloc_count,
9959 Output_section* output_section,
9960 bool needs_special_offset_handling,
9961 unsigned char* view,
9962 Mips_address address,
9963 section_size_type view_size,
9964 const Reloc_symbol_changes* reloc_symbol_changes)
9966 typedef Target_mips<size, big_endian> Mips;
9967 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9969 if (sh_type == elfcpp::SHT_REL)
9971 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9972 Classify_reloc;
9974 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9975 gold::Default_comdat_behavior, Classify_reloc>(
9976 relinfo,
9977 this,
9978 prelocs,
9979 reloc_count,
9980 output_section,
9981 needs_special_offset_handling,
9982 view,
9983 address,
9984 view_size,
9985 reloc_symbol_changes);
9987 else if (sh_type == elfcpp::SHT_RELA)
9989 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9990 Classify_reloc;
9992 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9993 gold::Default_comdat_behavior, Classify_reloc>(
9994 relinfo,
9995 this,
9996 prelocs,
9997 reloc_count,
9998 output_section,
9999 needs_special_offset_handling,
10000 view,
10001 address,
10002 view_size,
10003 reloc_symbol_changes);
10007 // Return the size of a relocation while scanning during a relocatable
10008 // link.
10010 unsigned int
10011 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
10013 switch (r_type)
10015 case elfcpp::R_MIPS_NONE:
10016 case elfcpp::R_MIPS_TLS_DTPMOD64:
10017 case elfcpp::R_MIPS_TLS_DTPREL64:
10018 case elfcpp::R_MIPS_TLS_TPREL64:
10019 return 0;
10021 case elfcpp::R_MIPS_32:
10022 case elfcpp::R_MIPS_TLS_DTPMOD32:
10023 case elfcpp::R_MIPS_TLS_DTPREL32:
10024 case elfcpp::R_MIPS_TLS_TPREL32:
10025 case elfcpp::R_MIPS_REL32:
10026 case elfcpp::R_MIPS_PC32:
10027 case elfcpp::R_MIPS_GPREL32:
10028 case elfcpp::R_MIPS_JALR:
10029 case elfcpp::R_MIPS_EH:
10030 return 4;
10032 case elfcpp::R_MIPS_16:
10033 case elfcpp::R_MIPS_HI16:
10034 case elfcpp::R_MIPS_LO16:
10035 case elfcpp::R_MIPS_HIGHER:
10036 case elfcpp::R_MIPS_HIGHEST:
10037 case elfcpp::R_MIPS_GPREL16:
10038 case elfcpp::R_MIPS16_HI16:
10039 case elfcpp::R_MIPS16_LO16:
10040 case elfcpp::R_MIPS_PC16:
10041 case elfcpp::R_MIPS_PCHI16:
10042 case elfcpp::R_MIPS_PCLO16:
10043 case elfcpp::R_MIPS_GOT16:
10044 case elfcpp::R_MIPS16_GOT16:
10045 case elfcpp::R_MIPS_CALL16:
10046 case elfcpp::R_MIPS16_CALL16:
10047 case elfcpp::R_MIPS_GOT_HI16:
10048 case elfcpp::R_MIPS_CALL_HI16:
10049 case elfcpp::R_MIPS_GOT_LO16:
10050 case elfcpp::R_MIPS_CALL_LO16:
10051 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10052 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10053 case elfcpp::R_MIPS_TLS_TPREL_HI16:
10054 case elfcpp::R_MIPS_TLS_TPREL_LO16:
10055 case elfcpp::R_MIPS16_GPREL:
10056 case elfcpp::R_MIPS_GOT_DISP:
10057 case elfcpp::R_MIPS_LITERAL:
10058 case elfcpp::R_MIPS_GOT_PAGE:
10059 case elfcpp::R_MIPS_GOT_OFST:
10060 case elfcpp::R_MIPS_TLS_GD:
10061 case elfcpp::R_MIPS_TLS_LDM:
10062 case elfcpp::R_MIPS_TLS_GOTTPREL:
10063 return 2;
10065 // These relocations are not byte sized
10066 case elfcpp::R_MIPS_26:
10067 case elfcpp::R_MIPS16_26:
10068 case elfcpp::R_MIPS_PC21_S2:
10069 case elfcpp::R_MIPS_PC26_S2:
10070 case elfcpp::R_MIPS_PC18_S3:
10071 case elfcpp::R_MIPS_PC19_S2:
10072 return 4;
10074 case elfcpp::R_MIPS_COPY:
10075 case elfcpp::R_MIPS_JUMP_SLOT:
10076 object->error(_("unexpected reloc %u in object file"), r_type);
10077 return 0;
10079 default:
10080 object->error(_("unsupported reloc %u in object file"), r_type);
10081 return 0;
10085 // Scan the relocs during a relocatable link.
10087 template<int size, bool big_endian>
10088 void
10089 Target_mips<size, big_endian>::scan_relocatable_relocs(
10090 Symbol_table* symtab,
10091 Layout* layout,
10092 Sized_relobj_file<size, big_endian>* object,
10093 unsigned int data_shndx,
10094 unsigned int sh_type,
10095 const unsigned char* prelocs,
10096 size_t reloc_count,
10097 Output_section* output_section,
10098 bool needs_special_offset_handling,
10099 size_t local_symbol_count,
10100 const unsigned char* plocal_symbols,
10101 Relocatable_relocs* rr)
10103 if (sh_type == elfcpp::SHT_REL)
10105 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10106 Classify_reloc;
10107 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10108 Scan_relocatable_relocs;
10110 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10111 symtab,
10112 layout,
10113 object,
10114 data_shndx,
10115 prelocs,
10116 reloc_count,
10117 output_section,
10118 needs_special_offset_handling,
10119 local_symbol_count,
10120 plocal_symbols,
10121 rr);
10123 else if (sh_type == elfcpp::SHT_RELA)
10125 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10126 Classify_reloc;
10127 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10128 Scan_relocatable_relocs;
10130 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10131 symtab,
10132 layout,
10133 object,
10134 data_shndx,
10135 prelocs,
10136 reloc_count,
10137 output_section,
10138 needs_special_offset_handling,
10139 local_symbol_count,
10140 plocal_symbols,
10141 rr);
10143 else
10144 gold_unreachable();
10147 // Scan the relocs for --emit-relocs.
10149 template<int size, bool big_endian>
10150 void
10151 Target_mips<size, big_endian>::emit_relocs_scan(
10152 Symbol_table* symtab,
10153 Layout* layout,
10154 Sized_relobj_file<size, big_endian>* object,
10155 unsigned int data_shndx,
10156 unsigned int sh_type,
10157 const unsigned char* prelocs,
10158 size_t reloc_count,
10159 Output_section* output_section,
10160 bool needs_special_offset_handling,
10161 size_t local_symbol_count,
10162 const unsigned char* plocal_syms,
10163 Relocatable_relocs* rr)
10165 if (sh_type == elfcpp::SHT_REL)
10167 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10168 Classify_reloc;
10169 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10170 Emit_relocs_strategy;
10172 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10173 symtab,
10174 layout,
10175 object,
10176 data_shndx,
10177 prelocs,
10178 reloc_count,
10179 output_section,
10180 needs_special_offset_handling,
10181 local_symbol_count,
10182 plocal_syms,
10183 rr);
10185 else if (sh_type == elfcpp::SHT_RELA)
10187 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10188 Classify_reloc;
10189 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10190 Emit_relocs_strategy;
10192 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10193 symtab,
10194 layout,
10195 object,
10196 data_shndx,
10197 prelocs,
10198 reloc_count,
10199 output_section,
10200 needs_special_offset_handling,
10201 local_symbol_count,
10202 plocal_syms,
10203 rr);
10205 else
10206 gold_unreachable();
10209 // Emit relocations for a section.
10211 template<int size, bool big_endian>
10212 void
10213 Target_mips<size, big_endian>::relocate_relocs(
10214 const Relocate_info<size, big_endian>* relinfo,
10215 unsigned int sh_type,
10216 const unsigned char* prelocs,
10217 size_t reloc_count,
10218 Output_section* output_section,
10219 typename elfcpp::Elf_types<size>::Elf_Off
10220 offset_in_output_section,
10221 unsigned char* view,
10222 Mips_address view_address,
10223 section_size_type view_size,
10224 unsigned char* reloc_view,
10225 section_size_type reloc_view_size)
10227 if (sh_type == elfcpp::SHT_REL)
10229 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10230 Classify_reloc;
10232 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10233 relinfo,
10234 prelocs,
10235 reloc_count,
10236 output_section,
10237 offset_in_output_section,
10238 view,
10239 view_address,
10240 view_size,
10241 reloc_view,
10242 reloc_view_size);
10244 else if (sh_type == elfcpp::SHT_RELA)
10246 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10247 Classify_reloc;
10249 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10250 relinfo,
10251 prelocs,
10252 reloc_count,
10253 output_section,
10254 offset_in_output_section,
10255 view,
10256 view_address,
10257 view_size,
10258 reloc_view,
10259 reloc_view_size);
10261 else
10262 gold_unreachable();
10265 // Perform target-specific processing in a relocatable link. This is
10266 // only used if we use the relocation strategy RELOC_SPECIAL.
10268 template<int size, bool big_endian>
10269 void
10270 Target_mips<size, big_endian>::relocate_special_relocatable(
10271 const Relocate_info<size, big_endian>* relinfo,
10272 unsigned int sh_type,
10273 const unsigned char* preloc_in,
10274 size_t relnum,
10275 Output_section* output_section,
10276 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10277 unsigned char* view,
10278 Mips_address view_address,
10279 section_size_type,
10280 unsigned char* preloc_out)
10282 // We can only handle REL type relocation sections.
10283 gold_assert(sh_type == elfcpp::SHT_REL);
10285 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10286 Reltype;
10287 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10288 Reltype_write;
10290 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10292 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10294 Mips_relobj<size, big_endian>* object =
10295 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10296 const unsigned int local_count = object->local_symbol_count();
10298 Reltype reloc(preloc_in);
10299 Reltype_write reloc_write(preloc_out);
10301 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10302 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10303 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10305 // Get the new symbol index.
10306 // We only use RELOC_SPECIAL strategy in local relocations.
10307 gold_assert(r_sym < local_count);
10309 // We are adjusting a section symbol. We need to find
10310 // the symbol table index of the section symbol for
10311 // the output section corresponding to input section
10312 // in which this symbol is defined.
10313 bool is_ordinary;
10314 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10315 gold_assert(is_ordinary);
10316 Output_section* os = object->output_section(shndx);
10317 gold_assert(os != NULL);
10318 gold_assert(os->needs_symtab_index());
10319 unsigned int new_symndx = os->symtab_index();
10321 // Get the new offset--the location in the output section where
10322 // this relocation should be applied.
10324 Mips_address offset = reloc.get_r_offset();
10325 Mips_address new_offset;
10326 if (offset_in_output_section != invalid_address)
10327 new_offset = offset + offset_in_output_section;
10328 else
10330 section_offset_type sot_offset =
10331 convert_types<section_offset_type, Mips_address>(offset);
10332 section_offset_type new_sot_offset =
10333 output_section->output_offset(object, relinfo->data_shndx,
10334 sot_offset);
10335 gold_assert(new_sot_offset != -1);
10336 new_offset = new_sot_offset;
10339 // In an object file, r_offset is an offset within the section.
10340 // In an executable or dynamic object, generated by
10341 // --emit-relocs, r_offset is an absolute address.
10342 if (!parameters->options().relocatable())
10344 new_offset += view_address;
10345 if (offset_in_output_section != invalid_address)
10346 new_offset -= offset_in_output_section;
10349 reloc_write.put_r_offset(new_offset);
10350 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10352 // Handle the reloc addend.
10353 // The relocation uses a section symbol in the input file.
10354 // We are adjusting it to use a section symbol in the output
10355 // file. The input section symbol refers to some address in
10356 // the input section. We need the relocation in the output
10357 // file to refer to that same address. This adjustment to
10358 // the addend is the same calculation we use for a simple
10359 // absolute relocation for the input section symbol.
10360 Valtype calculated_value = 0;
10361 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10363 unsigned char* paddend = view + offset;
10364 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10365 switch (r_type)
10367 case elfcpp::R_MIPS_26:
10368 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10369 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10370 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10371 false, &calculated_value);
10372 break;
10374 default:
10375 gold_unreachable();
10378 // Report any errors.
10379 switch (reloc_status)
10381 case Reloc_funcs::STATUS_OKAY:
10382 break;
10383 case Reloc_funcs::STATUS_OVERFLOW:
10384 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10385 _("relocation overflow: "
10386 "%u against local symbol %u in %s"),
10387 r_type, r_sym, object->name().c_str());
10388 break;
10389 case Reloc_funcs::STATUS_BAD_RELOC:
10390 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10391 _("unexpected opcode while processing relocation"));
10392 break;
10393 default:
10394 gold_unreachable();
10398 // Optimize the TLS relocation type based on what we know about the
10399 // symbol. IS_FINAL is true if the final address of this symbol is
10400 // known at link time.
10402 template<int size, bool big_endian>
10403 tls::Tls_optimization
10404 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10406 // FIXME: Currently we do not do any TLS optimization.
10407 return tls::TLSOPT_NONE;
10410 // Scan a relocation for a local symbol.
10412 template<int size, bool big_endian>
10413 inline void
10414 Target_mips<size, big_endian>::Scan::local(
10415 Symbol_table* symtab,
10416 Layout* layout,
10417 Target_mips<size, big_endian>* target,
10418 Sized_relobj_file<size, big_endian>* object,
10419 unsigned int data_shndx,
10420 Output_section* output_section,
10421 const Relatype* rela,
10422 const Reltype* rel,
10423 unsigned int rel_type,
10424 unsigned int r_type,
10425 const elfcpp::Sym<size, big_endian>& lsym,
10426 bool is_discarded)
10428 if (is_discarded)
10429 return;
10431 Mips_address r_offset;
10432 unsigned int r_sym;
10433 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10435 if (rel_type == elfcpp::SHT_RELA)
10437 r_offset = rela->get_r_offset();
10438 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10439 get_r_sym(rela);
10440 r_addend = rela->get_r_addend();
10442 else
10444 r_offset = rel->get_r_offset();
10445 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10446 get_r_sym(rel);
10447 r_addend = 0;
10450 Mips_relobj<size, big_endian>* mips_obj =
10451 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10453 if (mips_obj->is_mips16_stub_section(data_shndx))
10455 mips_obj->get_mips16_stub_section(data_shndx)
10456 ->new_local_reloc_found(r_type, r_sym);
10459 if (r_type == elfcpp::R_MIPS_NONE)
10460 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10461 // mips16 stub.
10462 return;
10464 if (!mips16_call_reloc(r_type)
10465 && !mips_obj->section_allows_mips16_refs(data_shndx))
10466 // This reloc would need to refer to a MIPS16 hard-float stub, if
10467 // there is one. We ignore MIPS16 stub sections and .pdr section when
10468 // looking for relocs that would need to refer to MIPS16 stubs.
10469 mips_obj->add_local_non_16bit_call(r_sym);
10471 if (r_type == elfcpp::R_MIPS16_26
10472 && !mips_obj->section_allows_mips16_refs(data_shndx))
10473 mips_obj->add_local_16bit_call(r_sym);
10475 switch (r_type)
10477 case elfcpp::R_MIPS_GOT16:
10478 case elfcpp::R_MIPS_CALL16:
10479 case elfcpp::R_MIPS_CALL_HI16:
10480 case elfcpp::R_MIPS_CALL_LO16:
10481 case elfcpp::R_MIPS_GOT_HI16:
10482 case elfcpp::R_MIPS_GOT_LO16:
10483 case elfcpp::R_MIPS_GOT_PAGE:
10484 case elfcpp::R_MIPS_GOT_OFST:
10485 case elfcpp::R_MIPS_GOT_DISP:
10486 case elfcpp::R_MIPS_TLS_GOTTPREL:
10487 case elfcpp::R_MIPS_TLS_GD:
10488 case elfcpp::R_MIPS_TLS_LDM:
10489 case elfcpp::R_MIPS16_GOT16:
10490 case elfcpp::R_MIPS16_CALL16:
10491 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10492 case elfcpp::R_MIPS16_TLS_GD:
10493 case elfcpp::R_MIPS16_TLS_LDM:
10494 case elfcpp::R_MICROMIPS_GOT16:
10495 case elfcpp::R_MICROMIPS_CALL16:
10496 case elfcpp::R_MICROMIPS_CALL_HI16:
10497 case elfcpp::R_MICROMIPS_CALL_LO16:
10498 case elfcpp::R_MICROMIPS_GOT_HI16:
10499 case elfcpp::R_MICROMIPS_GOT_LO16:
10500 case elfcpp::R_MICROMIPS_GOT_PAGE:
10501 case elfcpp::R_MICROMIPS_GOT_OFST:
10502 case elfcpp::R_MICROMIPS_GOT_DISP:
10503 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10504 case elfcpp::R_MICROMIPS_TLS_GD:
10505 case elfcpp::R_MICROMIPS_TLS_LDM:
10506 case elfcpp::R_MIPS_EH:
10507 // We need a GOT section.
10508 target->got_section(symtab, layout);
10509 break;
10511 default:
10512 break;
10515 if (call_lo16_reloc(r_type)
10516 || got_lo16_reloc(r_type)
10517 || got_disp_reloc(r_type)
10518 || eh_reloc(r_type))
10520 // We may need a local GOT entry for this relocation. We
10521 // don't count R_MIPS_GOT_PAGE because we can estimate the
10522 // maximum number of pages needed by looking at the size of
10523 // the segment. Similar comments apply to R_MIPS*_GOT16 and
10524 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
10525 // R_MIPS_CALL_HI16 because these are always followed by an
10526 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10527 Mips_output_data_got<size, big_endian>* got =
10528 target->got_section(symtab, layout);
10529 bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10530 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10531 is_section_symbol);
10534 switch (r_type)
10536 case elfcpp::R_MIPS_CALL16:
10537 case elfcpp::R_MIPS16_CALL16:
10538 case elfcpp::R_MICROMIPS_CALL16:
10539 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10540 (unsigned long)r_offset);
10541 return;
10543 case elfcpp::R_MIPS_GOT_PAGE:
10544 case elfcpp::R_MICROMIPS_GOT_PAGE:
10545 case elfcpp::R_MIPS16_GOT16:
10546 case elfcpp::R_MIPS_GOT16:
10547 case elfcpp::R_MIPS_GOT_HI16:
10548 case elfcpp::R_MIPS_GOT_LO16:
10549 case elfcpp::R_MICROMIPS_GOT16:
10550 case elfcpp::R_MICROMIPS_GOT_HI16:
10551 case elfcpp::R_MICROMIPS_GOT_LO16:
10553 // This relocation needs a page entry in the GOT.
10554 // Get the section contents.
10555 section_size_type view_size = 0;
10556 const unsigned char* view = object->section_contents(data_shndx,
10557 &view_size, false);
10558 view += r_offset;
10560 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10561 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10562 : r_addend);
10564 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10565 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10566 object, data_shndx, r_type, r_sym, addend));
10567 else
10568 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10569 break;
10572 case elfcpp::R_MIPS_HI16:
10573 case elfcpp::R_MIPS_PCHI16:
10574 case elfcpp::R_MIPS16_HI16:
10575 case elfcpp::R_MICROMIPS_HI16:
10576 // Record the reloc so that we can check whether the corresponding LO16
10577 // part exists.
10578 if (rel_type == elfcpp::SHT_REL)
10579 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10580 object, data_shndx, r_type, r_sym, 0));
10581 break;
10583 case elfcpp::R_MIPS_LO16:
10584 case elfcpp::R_MIPS_PCLO16:
10585 case elfcpp::R_MIPS16_LO16:
10586 case elfcpp::R_MICROMIPS_LO16:
10588 if (rel_type != elfcpp::SHT_REL)
10589 break;
10591 // Find corresponding GOT16/HI16 relocation.
10593 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10594 // be immediately following. However, for the IRIX6 ABI, the next
10595 // relocation may be a composed relocation consisting of several
10596 // relocations for the same address. In that case, the R_MIPS_LO16
10597 // relocation may occur as one of these. We permit a similar
10598 // extension in general, as that is useful for GCC.
10600 // In some cases GCC dead code elimination removes the LO16 but
10601 // keeps the corresponding HI16. This is strictly speaking a
10602 // violation of the ABI but not immediately harmful.
10604 typename std::list<got16_addend<size, big_endian> >::iterator it =
10605 target->got16_addends_.begin();
10606 while (it != target->got16_addends_.end())
10608 got16_addend<size, big_endian> _got16_addend = *it;
10610 // TODO(sasa): Split got16_addends_ list into two lists - one for
10611 // GOT16 relocs and the other for HI16 relocs.
10613 // Report an error if we find HI16 or GOT16 reloc from the
10614 // previous section without the matching LO16 part.
10615 if (_got16_addend.object != object
10616 || _got16_addend.shndx != data_shndx)
10618 gold_error("Can't find matching LO16 reloc");
10619 break;
10622 if (_got16_addend.r_sym != r_sym
10623 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10625 ++it;
10626 continue;
10629 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10630 // For GOT16, we need to calculate combined addend and record GOT page
10631 // entry.
10632 if (got16_reloc(_got16_addend.r_type))
10635 section_size_type view_size = 0;
10636 const unsigned char* view = object->section_contents(data_shndx,
10637 &view_size,
10638 false);
10639 view += r_offset;
10641 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10642 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10644 addend = (_got16_addend.addend << 16) + addend;
10645 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10646 addend);
10649 it = target->got16_addends_.erase(it);
10651 break;
10655 switch (r_type)
10657 case elfcpp::R_MIPS_32:
10658 case elfcpp::R_MIPS_REL32:
10659 case elfcpp::R_MIPS_64:
10661 if (parameters->options().output_is_position_independent())
10663 // If building a shared library (or a position-independent
10664 // executable), we need to create a dynamic relocation for
10665 // this location.
10666 if (is_readonly_section(output_section))
10667 break;
10668 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10669 rel_dyn->add_symbolless_local_addend(object, r_sym,
10670 elfcpp::R_MIPS_REL32,
10671 output_section, data_shndx,
10672 r_offset);
10674 break;
10677 case elfcpp::R_MIPS_TLS_GOTTPREL:
10678 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10679 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10680 case elfcpp::R_MIPS_TLS_LDM:
10681 case elfcpp::R_MIPS16_TLS_LDM:
10682 case elfcpp::R_MICROMIPS_TLS_LDM:
10683 case elfcpp::R_MIPS_TLS_GD:
10684 case elfcpp::R_MIPS16_TLS_GD:
10685 case elfcpp::R_MICROMIPS_TLS_GD:
10687 bool output_is_shared = parameters->options().shared();
10688 const tls::Tls_optimization optimized_type
10689 = Target_mips<size, big_endian>::optimize_tls_reloc(
10690 !output_is_shared, r_type);
10691 switch (r_type)
10693 case elfcpp::R_MIPS_TLS_GD:
10694 case elfcpp::R_MIPS16_TLS_GD:
10695 case elfcpp::R_MICROMIPS_TLS_GD:
10696 if (optimized_type == tls::TLSOPT_NONE)
10698 // Create a pair of GOT entries for the module index and
10699 // dtv-relative offset.
10700 Mips_output_data_got<size, big_endian>* got =
10701 target->got_section(symtab, layout);
10702 unsigned int shndx = lsym.get_st_shndx();
10703 bool is_ordinary;
10704 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10705 if (!is_ordinary)
10707 object->error(_("local symbol %u has bad shndx %u"),
10708 r_sym, shndx);
10709 break;
10711 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10712 shndx, false);
10714 else
10716 // FIXME: TLS optimization not supported yet.
10717 gold_unreachable();
10719 break;
10721 case elfcpp::R_MIPS_TLS_LDM:
10722 case elfcpp::R_MIPS16_TLS_LDM:
10723 case elfcpp::R_MICROMIPS_TLS_LDM:
10724 if (optimized_type == tls::TLSOPT_NONE)
10726 // We always record LDM symbols as local with index 0.
10727 target->got_section()->record_local_got_symbol(mips_obj, 0,
10728 r_addend, r_type,
10729 -1U, false);
10731 else
10733 // FIXME: TLS optimization not supported yet.
10734 gold_unreachable();
10736 break;
10737 case elfcpp::R_MIPS_TLS_GOTTPREL:
10738 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10739 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10740 layout->set_has_static_tls();
10741 if (optimized_type == tls::TLSOPT_NONE)
10743 // Create a GOT entry for the tp-relative offset.
10744 Mips_output_data_got<size, big_endian>* got =
10745 target->got_section(symtab, layout);
10746 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10747 -1U, false);
10749 else
10751 // FIXME: TLS optimization not supported yet.
10752 gold_unreachable();
10754 break;
10756 default:
10757 gold_unreachable();
10760 break;
10762 default:
10763 break;
10766 // Refuse some position-dependent relocations when creating a
10767 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10768 // not PIC, but we can create dynamic relocations and the result
10769 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10770 // combined with R_MIPS_GOT16.
10771 if (parameters->options().shared())
10773 switch (r_type)
10775 case elfcpp::R_MIPS16_HI16:
10776 case elfcpp::R_MIPS_HI16:
10777 case elfcpp::R_MIPS_HIGHER:
10778 case elfcpp::R_MIPS_HIGHEST:
10779 case elfcpp::R_MICROMIPS_HI16:
10780 case elfcpp::R_MICROMIPS_HIGHER:
10781 case elfcpp::R_MICROMIPS_HIGHEST:
10782 // Don't refuse a high part relocation if it's against
10783 // no symbol (e.g. part of a compound relocation).
10784 if (r_sym == 0)
10785 break;
10786 // Fall through.
10788 case elfcpp::R_MIPS16_26:
10789 case elfcpp::R_MIPS_26:
10790 case elfcpp::R_MICROMIPS_26_S1:
10791 gold_error(_("%s: relocation %u against `%s' can not be used when "
10792 "making a shared object; recompile with -fPIC"),
10793 object->name().c_str(), r_type, "a local symbol");
10794 default:
10795 break;
10800 template<int size, bool big_endian>
10801 inline void
10802 Target_mips<size, big_endian>::Scan::local(
10803 Symbol_table* symtab,
10804 Layout* layout,
10805 Target_mips<size, big_endian>* target,
10806 Sized_relobj_file<size, big_endian>* object,
10807 unsigned int data_shndx,
10808 Output_section* output_section,
10809 const Reltype& reloc,
10810 unsigned int r_type,
10811 const elfcpp::Sym<size, big_endian>& lsym,
10812 bool is_discarded)
10814 if (is_discarded)
10815 return;
10817 local(
10818 symtab,
10819 layout,
10820 target,
10821 object,
10822 data_shndx,
10823 output_section,
10824 (const Relatype*) NULL,
10825 &reloc,
10826 elfcpp::SHT_REL,
10827 r_type,
10828 lsym, is_discarded);
10832 template<int size, bool big_endian>
10833 inline void
10834 Target_mips<size, big_endian>::Scan::local(
10835 Symbol_table* symtab,
10836 Layout* layout,
10837 Target_mips<size, big_endian>* target,
10838 Sized_relobj_file<size, big_endian>* object,
10839 unsigned int data_shndx,
10840 Output_section* output_section,
10841 const Relatype& reloc,
10842 unsigned int r_type,
10843 const elfcpp::Sym<size, big_endian>& lsym,
10844 bool is_discarded)
10846 if (is_discarded)
10847 return;
10849 local(
10850 symtab,
10851 layout,
10852 target,
10853 object,
10854 data_shndx,
10855 output_section,
10856 &reloc,
10857 (const Reltype*) NULL,
10858 elfcpp::SHT_RELA,
10859 r_type,
10860 lsym, is_discarded);
10863 // Scan a relocation for a global symbol.
10865 template<int size, bool big_endian>
10866 inline void
10867 Target_mips<size, big_endian>::Scan::global(
10868 Symbol_table* symtab,
10869 Layout* layout,
10870 Target_mips<size, big_endian>* target,
10871 Sized_relobj_file<size, big_endian>* object,
10872 unsigned int data_shndx,
10873 Output_section* output_section,
10874 const Relatype* rela,
10875 const Reltype* rel,
10876 unsigned int rel_type,
10877 unsigned int r_type,
10878 Symbol* gsym)
10880 Mips_address r_offset;
10881 unsigned int r_sym;
10882 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10884 if (rel_type == elfcpp::SHT_RELA)
10886 r_offset = rela->get_r_offset();
10887 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10888 get_r_sym(rela);
10889 r_addend = rela->get_r_addend();
10891 else
10893 r_offset = rel->get_r_offset();
10894 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10895 get_r_sym(rel);
10896 r_addend = 0;
10899 Mips_relobj<size, big_endian>* mips_obj =
10900 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10901 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10903 if (mips_obj->is_mips16_stub_section(data_shndx))
10905 mips_obj->get_mips16_stub_section(data_shndx)
10906 ->new_global_reloc_found(r_type, mips_sym);
10909 if (r_type == elfcpp::R_MIPS_NONE)
10910 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10911 // mips16 stub.
10912 return;
10914 if (!mips16_call_reloc(r_type)
10915 && !mips_obj->section_allows_mips16_refs(data_shndx))
10916 // This reloc would need to refer to a MIPS16 hard-float stub, if
10917 // there is one. We ignore MIPS16 stub sections and .pdr section when
10918 // looking for relocs that would need to refer to MIPS16 stubs.
10919 mips_sym->set_need_fn_stub();
10921 // We need PLT entries if there are static-only relocations against
10922 // an externally-defined function. This can technically occur for
10923 // shared libraries if there are branches to the symbol, although it
10924 // is unlikely that this will be used in practice due to the short
10925 // ranges involved. It can occur for any relative or absolute relocation
10926 // in executables; in that case, the PLT entry becomes the function's
10927 // canonical address.
10928 bool static_reloc = false;
10930 // Set CAN_MAKE_DYNAMIC to true if we can convert this
10931 // relocation into a dynamic one.
10932 bool can_make_dynamic = false;
10933 switch (r_type)
10935 case elfcpp::R_MIPS_GOT16:
10936 case elfcpp::R_MIPS_CALL16:
10937 case elfcpp::R_MIPS_CALL_HI16:
10938 case elfcpp::R_MIPS_CALL_LO16:
10939 case elfcpp::R_MIPS_GOT_HI16:
10940 case elfcpp::R_MIPS_GOT_LO16:
10941 case elfcpp::R_MIPS_GOT_PAGE:
10942 case elfcpp::R_MIPS_GOT_OFST:
10943 case elfcpp::R_MIPS_GOT_DISP:
10944 case elfcpp::R_MIPS_TLS_GOTTPREL:
10945 case elfcpp::R_MIPS_TLS_GD:
10946 case elfcpp::R_MIPS_TLS_LDM:
10947 case elfcpp::R_MIPS16_GOT16:
10948 case elfcpp::R_MIPS16_CALL16:
10949 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10950 case elfcpp::R_MIPS16_TLS_GD:
10951 case elfcpp::R_MIPS16_TLS_LDM:
10952 case elfcpp::R_MICROMIPS_GOT16:
10953 case elfcpp::R_MICROMIPS_CALL16:
10954 case elfcpp::R_MICROMIPS_CALL_HI16:
10955 case elfcpp::R_MICROMIPS_CALL_LO16:
10956 case elfcpp::R_MICROMIPS_GOT_HI16:
10957 case elfcpp::R_MICROMIPS_GOT_LO16:
10958 case elfcpp::R_MICROMIPS_GOT_PAGE:
10959 case elfcpp::R_MICROMIPS_GOT_OFST:
10960 case elfcpp::R_MICROMIPS_GOT_DISP:
10961 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10962 case elfcpp::R_MICROMIPS_TLS_GD:
10963 case elfcpp::R_MICROMIPS_TLS_LDM:
10964 case elfcpp::R_MIPS_EH:
10965 // We need a GOT section.
10966 target->got_section(symtab, layout);
10967 break;
10969 // This is just a hint; it can safely be ignored. Don't set
10970 // has_static_relocs for the corresponding symbol.
10971 case elfcpp::R_MIPS_JALR:
10972 case elfcpp::R_MICROMIPS_JALR:
10973 break;
10975 case elfcpp::R_MIPS_GPREL16:
10976 case elfcpp::R_MIPS_GPREL32:
10977 case elfcpp::R_MIPS16_GPREL:
10978 case elfcpp::R_MICROMIPS_GPREL16:
10979 // TODO(sasa)
10980 // GP-relative relocations always resolve to a definition in a
10981 // regular input file, ignoring the one-definition rule. This is
10982 // important for the GP setup sequence in NewABI code, which
10983 // always resolves to a local function even if other relocations
10984 // against the symbol wouldn't.
10985 //constrain_symbol_p = FALSE;
10986 break;
10988 case elfcpp::R_MIPS_32:
10989 case elfcpp::R_MIPS_REL32:
10990 case elfcpp::R_MIPS_64:
10991 if ((parameters->options().shared()
10992 || (strcmp(gsym->name(), "__gnu_local_gp") != 0
10993 && (!is_readonly_section(output_section)
10994 || mips_obj->is_pic())))
10995 && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
10997 if (r_type != elfcpp::R_MIPS_REL32)
10998 mips_sym->set_pointer_equality_needed();
10999 can_make_dynamic = true;
11000 break;
11002 // Fall through.
11004 default:
11005 // Most static relocations require pointer equality, except
11006 // for branches.
11007 mips_sym->set_pointer_equality_needed();
11008 // Fall through.
11010 case elfcpp::R_MIPS_26:
11011 case elfcpp::R_MIPS_PC16:
11012 case elfcpp::R_MIPS_PC21_S2:
11013 case elfcpp::R_MIPS_PC26_S2:
11014 case elfcpp::R_MIPS16_26:
11015 case elfcpp::R_MICROMIPS_26_S1:
11016 case elfcpp::R_MICROMIPS_PC7_S1:
11017 case elfcpp::R_MICROMIPS_PC10_S1:
11018 case elfcpp::R_MICROMIPS_PC16_S1:
11019 case elfcpp::R_MICROMIPS_PC23_S2:
11020 static_reloc = true;
11021 mips_sym->set_has_static_relocs();
11022 break;
11025 // If there are call relocations against an externally-defined symbol,
11026 // see whether we can create a MIPS lazy-binding stub for it. We can
11027 // only do this if all references to the function are through call
11028 // relocations, and in that case, the traditional lazy-binding stubs
11029 // are much more efficient than PLT entries.
11030 switch (r_type)
11032 case elfcpp::R_MIPS16_CALL16:
11033 case elfcpp::R_MIPS_CALL16:
11034 case elfcpp::R_MIPS_CALL_HI16:
11035 case elfcpp::R_MIPS_CALL_LO16:
11036 case elfcpp::R_MIPS_JALR:
11037 case elfcpp::R_MICROMIPS_CALL16:
11038 case elfcpp::R_MICROMIPS_CALL_HI16:
11039 case elfcpp::R_MICROMIPS_CALL_LO16:
11040 case elfcpp::R_MICROMIPS_JALR:
11041 if (!mips_sym->no_lazy_stub())
11043 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11044 // Calls from shared objects to undefined symbols of type
11045 // STT_NOTYPE need lazy-binding stub.
11046 || (mips_sym->is_undefined() && parameters->options().shared()))
11047 target->mips_stubs_section(layout)->make_entry(mips_sym);
11049 break;
11050 default:
11052 // We must not create a stub for a symbol that has relocations
11053 // related to taking the function's address.
11054 mips_sym->set_no_lazy_stub();
11055 target->remove_lazy_stub_entry(mips_sym);
11056 break;
11060 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11061 mips_sym->is_mips16()))
11062 mips_sym->set_has_nonpic_branches();
11064 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11065 // and has a special meaning.
11066 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11067 && strcmp(gsym->name(), "_gp_disp") == 0
11068 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11069 if (static_reloc && gsym->needs_plt_entry())
11071 target->make_plt_entry(symtab, layout, mips_sym, r_type);
11073 // Since this is not a PC-relative relocation, we may be
11074 // taking the address of a function. In that case we need to
11075 // set the entry in the dynamic symbol table to the address of
11076 // the PLT entry.
11077 if (gsym->is_from_dynobj() && !parameters->options().shared())
11079 gsym->set_needs_dynsym_value();
11080 // We distinguish between PLT entries and lazy-binding stubs by
11081 // giving the former an st_other value of STO_MIPS_PLT. Set the
11082 // flag if there are any relocations in the binary where pointer
11083 // equality matters.
11084 if (mips_sym->pointer_equality_needed())
11085 mips_sym->set_mips_plt();
11088 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11090 // Absolute addressing relocations.
11091 // Make a dynamic relocation if necessary.
11092 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11094 if (gsym->may_need_copy_reloc())
11096 target->copy_reloc(symtab, layout, object, data_shndx,
11097 output_section, gsym, r_type, r_offset);
11099 else if (can_make_dynamic)
11101 // Create .rel.dyn section.
11102 target->rel_dyn_section(layout);
11103 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11104 data_shndx, output_section, r_offset);
11106 else
11107 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11108 gsym->name());
11112 bool for_call = false;
11113 switch (r_type)
11115 case elfcpp::R_MIPS_CALL16:
11116 case elfcpp::R_MIPS16_CALL16:
11117 case elfcpp::R_MICROMIPS_CALL16:
11118 case elfcpp::R_MIPS_CALL_HI16:
11119 case elfcpp::R_MIPS_CALL_LO16:
11120 case elfcpp::R_MICROMIPS_CALL_HI16:
11121 case elfcpp::R_MICROMIPS_CALL_LO16:
11122 for_call = true;
11123 // Fall through.
11125 case elfcpp::R_MIPS16_GOT16:
11126 case elfcpp::R_MIPS_GOT16:
11127 case elfcpp::R_MIPS_GOT_HI16:
11128 case elfcpp::R_MIPS_GOT_LO16:
11129 case elfcpp::R_MICROMIPS_GOT16:
11130 case elfcpp::R_MICROMIPS_GOT_HI16:
11131 case elfcpp::R_MICROMIPS_GOT_LO16:
11132 case elfcpp::R_MIPS_GOT_DISP:
11133 case elfcpp::R_MICROMIPS_GOT_DISP:
11134 case elfcpp::R_MIPS_EH:
11136 // The symbol requires a GOT entry.
11137 Mips_output_data_got<size, big_endian>* got =
11138 target->got_section(symtab, layout);
11139 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11140 for_call);
11141 mips_sym->set_global_got_area(GGA_NORMAL);
11143 break;
11145 case elfcpp::R_MIPS_GOT_PAGE:
11146 case elfcpp::R_MICROMIPS_GOT_PAGE:
11148 // This relocation needs a page entry in the GOT.
11149 // Get the section contents.
11150 section_size_type view_size = 0;
11151 const unsigned char* view =
11152 object->section_contents(data_shndx, &view_size, false);
11153 view += r_offset;
11155 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11156 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11157 : r_addend);
11158 Mips_output_data_got<size, big_endian>* got =
11159 target->got_section(symtab, layout);
11160 got->record_got_page_entry(mips_obj, r_sym, addend);
11162 // If this is a global, overridable symbol, GOT_PAGE will
11163 // decay to GOT_DISP, so we'll need a GOT entry for it.
11164 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11165 && !mips_sym->object()->is_dynamic()
11166 && !mips_sym->is_undefined());
11167 if (!def_regular
11168 || (parameters->options().output_is_position_independent()
11169 && !parameters->options().Bsymbolic()
11170 && !mips_sym->is_forced_local()))
11172 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11173 for_call);
11174 mips_sym->set_global_got_area(GGA_NORMAL);
11177 break;
11179 case elfcpp::R_MIPS_TLS_GOTTPREL:
11180 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11181 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11182 case elfcpp::R_MIPS_TLS_LDM:
11183 case elfcpp::R_MIPS16_TLS_LDM:
11184 case elfcpp::R_MICROMIPS_TLS_LDM:
11185 case elfcpp::R_MIPS_TLS_GD:
11186 case elfcpp::R_MIPS16_TLS_GD:
11187 case elfcpp::R_MICROMIPS_TLS_GD:
11189 const bool is_final = gsym->final_value_is_known();
11190 const tls::Tls_optimization optimized_type =
11191 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11193 switch (r_type)
11195 case elfcpp::R_MIPS_TLS_GD:
11196 case elfcpp::R_MIPS16_TLS_GD:
11197 case elfcpp::R_MICROMIPS_TLS_GD:
11198 if (optimized_type == tls::TLSOPT_NONE)
11200 // Create a pair of GOT entries for the module index and
11201 // dtv-relative offset.
11202 Mips_output_data_got<size, big_endian>* got =
11203 target->got_section(symtab, layout);
11204 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11205 false);
11207 else
11209 // FIXME: TLS optimization not supported yet.
11210 gold_unreachable();
11212 break;
11214 case elfcpp::R_MIPS_TLS_LDM:
11215 case elfcpp::R_MIPS16_TLS_LDM:
11216 case elfcpp::R_MICROMIPS_TLS_LDM:
11217 if (optimized_type == tls::TLSOPT_NONE)
11219 // We always record LDM symbols as local with index 0.
11220 target->got_section()->record_local_got_symbol(mips_obj, 0,
11221 r_addend, r_type,
11222 -1U, false);
11224 else
11226 // FIXME: TLS optimization not supported yet.
11227 gold_unreachable();
11229 break;
11230 case elfcpp::R_MIPS_TLS_GOTTPREL:
11231 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11232 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11233 layout->set_has_static_tls();
11234 if (optimized_type == tls::TLSOPT_NONE)
11236 // Create a GOT entry for the tp-relative offset.
11237 Mips_output_data_got<size, big_endian>* got =
11238 target->got_section(symtab, layout);
11239 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11240 false);
11242 else
11244 // FIXME: TLS optimization not supported yet.
11245 gold_unreachable();
11247 break;
11249 default:
11250 gold_unreachable();
11253 break;
11254 case elfcpp::R_MIPS_COPY:
11255 case elfcpp::R_MIPS_JUMP_SLOT:
11256 // These are relocations which should only be seen by the
11257 // dynamic linker, and should never be seen here.
11258 gold_error(_("%s: unexpected reloc %u in object file"),
11259 object->name().c_str(), r_type);
11260 break;
11262 default:
11263 break;
11266 // Refuse some position-dependent relocations when creating a
11267 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
11268 // not PIC, but we can create dynamic relocations and the result
11269 // will be fine. Also do not refuse R_MIPS_LO16, which can be
11270 // combined with R_MIPS_GOT16.
11271 if (parameters->options().shared())
11273 switch (r_type)
11275 case elfcpp::R_MIPS16_HI16:
11276 case elfcpp::R_MIPS_HI16:
11277 case elfcpp::R_MIPS_HIGHER:
11278 case elfcpp::R_MIPS_HIGHEST:
11279 case elfcpp::R_MICROMIPS_HI16:
11280 case elfcpp::R_MICROMIPS_HIGHER:
11281 case elfcpp::R_MICROMIPS_HIGHEST:
11282 // Don't refuse a high part relocation if it's against
11283 // no symbol (e.g. part of a compound relocation).
11284 if (r_sym == 0)
11285 break;
11287 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11288 // and has a special meaning.
11289 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11290 break;
11291 // Fall through.
11293 case elfcpp::R_MIPS16_26:
11294 case elfcpp::R_MIPS_26:
11295 case elfcpp::R_MICROMIPS_26_S1:
11296 gold_error(_("%s: relocation %u against `%s' can not be used when "
11297 "making a shared object; recompile with -fPIC"),
11298 object->name().c_str(), r_type, gsym->name());
11299 default:
11300 break;
11305 template<int size, bool big_endian>
11306 inline void
11307 Target_mips<size, big_endian>::Scan::global(
11308 Symbol_table* symtab,
11309 Layout* layout,
11310 Target_mips<size, big_endian>* target,
11311 Sized_relobj_file<size, big_endian>* object,
11312 unsigned int data_shndx,
11313 Output_section* output_section,
11314 const Relatype& reloc,
11315 unsigned int r_type,
11316 Symbol* gsym)
11318 global(
11319 symtab,
11320 layout,
11321 target,
11322 object,
11323 data_shndx,
11324 output_section,
11325 &reloc,
11326 (const Reltype*) NULL,
11327 elfcpp::SHT_RELA,
11328 r_type,
11329 gsym);
11332 template<int size, bool big_endian>
11333 inline void
11334 Target_mips<size, big_endian>::Scan::global(
11335 Symbol_table* symtab,
11336 Layout* layout,
11337 Target_mips<size, big_endian>* target,
11338 Sized_relobj_file<size, big_endian>* object,
11339 unsigned int data_shndx,
11340 Output_section* output_section,
11341 const Reltype& reloc,
11342 unsigned int r_type,
11343 Symbol* gsym)
11345 global(
11346 symtab,
11347 layout,
11348 target,
11349 object,
11350 data_shndx,
11351 output_section,
11352 (const Relatype*) NULL,
11353 &reloc,
11354 elfcpp::SHT_REL,
11355 r_type,
11356 gsym);
11359 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11360 // In cases where Scan::local() or Scan::global() has created
11361 // a dynamic relocation, the addend of the relocation is carried
11362 // in the data, and we must not apply the static relocation.
11364 template<int size, bool big_endian>
11365 inline bool
11366 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11367 const Mips_symbol<size>* gsym,
11368 unsigned int r_type,
11369 Output_section* output_section,
11370 Target_mips* target)
11372 // If the output section is not allocated, then we didn't call
11373 // scan_relocs, we didn't create a dynamic reloc, and we must apply
11374 // the reloc here.
11375 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11376 return true;
11378 if (gsym == NULL)
11379 return true;
11380 else
11382 // For global symbols, we use the same helper routines used in the
11383 // scan pass.
11384 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11385 && !gsym->may_need_copy_reloc())
11387 // We have generated dynamic reloc (R_MIPS_REL32).
11389 bool multi_got = false;
11390 if (target->has_got_section())
11391 multi_got = target->got_section()->multi_got();
11392 bool has_got_offset;
11393 if (!multi_got)
11394 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11395 else
11396 has_got_offset = gsym->global_gotoffset() != -1U;
11397 if (!has_got_offset)
11398 return true;
11399 else
11400 // Apply the relocation only if the symbol is in the local got.
11401 // Do not apply the relocation if the symbol is in the global
11402 // got.
11403 return symbol_references_local(gsym, gsym->has_dynsym_index());
11405 else
11406 // We have not generated dynamic reloc.
11407 return true;
11411 // Perform a relocation.
11413 template<int size, bool big_endian>
11414 inline bool
11415 Target_mips<size, big_endian>::Relocate::relocate(
11416 const Relocate_info<size, big_endian>* relinfo,
11417 unsigned int rel_type,
11418 Target_mips* target,
11419 Output_section* output_section,
11420 size_t relnum,
11421 const unsigned char* preloc,
11422 const Sized_symbol<size>* gsym,
11423 const Symbol_value<size>* psymval,
11424 unsigned char* view,
11425 Mips_address address,
11426 section_size_type)
11428 Mips_address r_offset;
11429 unsigned int r_sym;
11430 unsigned int r_type;
11431 unsigned int r_type2;
11432 unsigned int r_type3;
11433 unsigned char r_ssym;
11434 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11435 // r_offset and r_type of the next relocation is needed for resolving multiple
11436 // consecutive relocations with the same offset.
11437 Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11438 unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11440 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11441 size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
11443 if (rel_type == elfcpp::SHT_RELA)
11445 const Relatype rela(preloc);
11446 r_offset = rela.get_r_offset();
11447 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11448 get_r_sym(&rela);
11449 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11450 get_r_type(&rela);
11451 r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11452 get_r_type2(&rela);
11453 r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11454 get_r_type3(&rela);
11455 r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11456 get_r_ssym(&rela);
11457 r_addend = rela.get_r_addend();
11458 // If this is not last relocation, get r_offset and r_type of the next
11459 // relocation.
11460 if (relnum + 1 < reloc_count)
11462 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11463 const Relatype next_rela(preloc + reloc_size);
11464 next_r_offset = next_rela.get_r_offset();
11465 next_r_type =
11466 Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11467 get_r_type(&next_rela);
11470 else
11472 const Reltype rel(preloc);
11473 r_offset = rel.get_r_offset();
11474 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11475 get_r_sym(&rel);
11476 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11477 get_r_type(&rel);
11478 r_ssym = 0;
11479 r_type2 = elfcpp::R_MIPS_NONE;
11480 r_type3 = elfcpp::R_MIPS_NONE;
11481 r_addend = 0;
11482 // If this is not last relocation, get r_offset and r_type of the next
11483 // relocation.
11484 if (relnum + 1 < reloc_count)
11486 const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11487 const Reltype next_rel(preloc + reloc_size);
11488 next_r_offset = next_rel.get_r_offset();
11489 next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11490 get_r_type(&next_rel);
11494 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11495 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11497 Mips_relobj<size, big_endian>* object =
11498 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11500 bool target_is_16_bit_code = false;
11501 bool target_is_micromips_code = false;
11502 bool cross_mode_jump;
11504 Symbol_value<size> symval;
11506 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11508 bool changed_symbol_value = false;
11509 if (gsym == NULL)
11511 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11512 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11513 if (target_is_16_bit_code || target_is_micromips_code)
11515 // MIPS16/microMIPS text labels should be treated as odd.
11516 symval.set_output_value(psymval->value(object, 1));
11517 psymval = &symval;
11518 changed_symbol_value = true;
11521 else
11523 target_is_16_bit_code = mips_sym->is_mips16();
11524 target_is_micromips_code = mips_sym->is_micromips();
11526 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11527 // it odd. This will cause something like .word SYM to come up with
11528 // the right value when it is loaded into the PC.
11530 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11531 && psymval->value(object, 0) != 0)
11533 symval.set_output_value(psymval->value(object, 0) | 1);
11534 psymval = &symval;
11535 changed_symbol_value = true;
11538 // Pick the value to use for symbols defined in shared objects.
11539 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11540 || mips_sym->has_lazy_stub())
11542 Mips_address value;
11543 if (!mips_sym->has_lazy_stub())
11545 // Prefer a standard MIPS PLT entry.
11546 if (mips_sym->has_mips_plt_offset())
11548 value = target->plt_section()->mips_entry_address(mips_sym);
11549 target_is_micromips_code = false;
11550 target_is_16_bit_code = false;
11552 else
11554 value = (target->plt_section()->comp_entry_address(mips_sym)
11555 + 1);
11556 if (target->is_output_micromips())
11557 target_is_micromips_code = true;
11558 else
11559 target_is_16_bit_code = true;
11562 else
11563 value = target->mips_stubs_section()->stub_address(mips_sym);
11565 symval.set_output_value(value);
11566 psymval = &symval;
11570 // TRUE if the symbol referred to by this relocation is "_gp_disp".
11571 // Note that such a symbol must always be a global symbol.
11572 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11573 && !object->is_newabi());
11575 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11576 // Note that such a symbol must always be a global symbol.
11577 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11580 if (gp_disp)
11582 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11583 gold_error_at_location(relinfo, relnum, r_offset,
11584 _("relocations against _gp_disp are permitted only"
11585 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11587 else if (gnu_local_gp)
11589 // __gnu_local_gp is _gp symbol.
11590 symval.set_output_value(target->adjusted_gp_value(object));
11591 psymval = &symval;
11594 // If this is a reference to a 16-bit function with a stub, we need
11595 // to redirect the relocation to the stub unless:
11597 // (a) the relocation is for a MIPS16 JAL;
11599 // (b) the relocation is for a MIPS16 PIC call, and there are no
11600 // non-MIPS16 uses of the GOT slot; or
11602 // (c) the section allows direct references to MIPS16 functions.
11603 if (r_type != elfcpp::R_MIPS16_26
11604 && ((mips_sym != NULL
11605 && mips_sym->has_mips16_fn_stub()
11606 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11607 || (mips_sym == NULL
11608 && object->get_local_mips16_fn_stub(r_sym) != NULL))
11609 && !object->section_allows_mips16_refs(relinfo->data_shndx))
11611 // This is a 32- or 64-bit call to a 16-bit function. We should
11612 // have already noticed that we were going to need the
11613 // stub.
11614 Mips_address value;
11615 if (mips_sym == NULL)
11616 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11617 else
11619 gold_assert(mips_sym->need_fn_stub());
11620 if (mips_sym->has_la25_stub())
11621 value = target->la25_stub_section()->stub_address(mips_sym);
11622 else
11624 value = mips_sym->template
11625 get_mips16_fn_stub<big_endian>()->output_address();
11628 symval.set_output_value(value);
11629 psymval = &symval;
11630 changed_symbol_value = true;
11632 // The target is 16-bit, but the stub isn't.
11633 target_is_16_bit_code = false;
11635 // If this is a MIPS16 call with a stub, that is made through the PLT or
11636 // to a standard MIPS function, we need to redirect the call to the stub.
11637 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11638 // indirect calls should use an indirect stub instead.
11639 else if (r_type == elfcpp::R_MIPS16_26
11640 && ((mips_sym != NULL
11641 && (mips_sym->has_mips16_call_stub()
11642 || mips_sym->has_mips16_call_fp_stub()))
11643 || (mips_sym == NULL
11644 && object->get_local_mips16_call_stub(r_sym) != NULL))
11645 && ((mips_sym != NULL && mips_sym->has_plt_offset())
11646 || !target_is_16_bit_code))
11648 Mips16_stub_section<size, big_endian>* call_stub;
11649 if (mips_sym == NULL)
11650 call_stub = object->get_local_mips16_call_stub(r_sym);
11651 else
11653 // If both call_stub and call_fp_stub are defined, we can figure
11654 // out which one to use by checking which one appears in the input
11655 // file.
11656 if (mips_sym->has_mips16_call_stub()
11657 && mips_sym->has_mips16_call_fp_stub())
11659 call_stub = NULL;
11660 for (unsigned int i = 1; i < object->shnum(); ++i)
11662 if (object->is_mips16_call_fp_stub_section(i))
11664 call_stub = mips_sym->template
11665 get_mips16_call_fp_stub<big_endian>();
11666 break;
11670 if (call_stub == NULL)
11671 call_stub =
11672 mips_sym->template get_mips16_call_stub<big_endian>();
11674 else if (mips_sym->has_mips16_call_stub())
11675 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11676 else
11677 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11680 symval.set_output_value(call_stub->output_address());
11681 psymval = &symval;
11682 changed_symbol_value = true;
11684 // If this is a direct call to a PIC function, redirect to the
11685 // non-PIC stub.
11686 else if (mips_sym != NULL
11687 && mips_sym->has_la25_stub()
11688 && relocation_needs_la25_stub<size, big_endian>(
11689 object, r_type, target_is_16_bit_code))
11691 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11692 if (mips_sym->is_micromips())
11693 value += 1;
11694 symval.set_output_value(value);
11695 psymval = &symval;
11697 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11698 // entry is used if a standard PLT entry has also been made.
11699 else if ((r_type == elfcpp::R_MIPS16_26
11700 || r_type == elfcpp::R_MICROMIPS_26_S1)
11701 && mips_sym != NULL
11702 && mips_sym->has_plt_offset()
11703 && mips_sym->has_comp_plt_offset()
11704 && mips_sym->has_mips_plt_offset())
11706 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11707 + 1);
11708 symval.set_output_value(value);
11709 psymval = &symval;
11711 target_is_16_bit_code = !target->is_output_micromips();
11712 target_is_micromips_code = target->is_output_micromips();
11715 // Make sure MIPS16 and microMIPS are not used together.
11716 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11717 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11719 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11722 // Calls from 16-bit code to 32-bit code and vice versa require the
11723 // mode change. However, we can ignore calls to undefined weak symbols,
11724 // which should never be executed at runtime. This exception is important
11725 // because the assembly writer may have "known" that any definition of the
11726 // symbol would be 16-bit code, and that direct jumps were therefore
11727 // acceptable.
11728 cross_mode_jump =
11729 (!(gsym != NULL && gsym->is_weak_undefined())
11730 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11731 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11732 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11733 && (target_is_16_bit_code || target_is_micromips_code))));
11735 bool local = (mips_sym == NULL
11736 || (mips_sym->got_only_for_calls()
11737 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11738 : symbol_references_local(mips_sym,
11739 mips_sym->has_dynsym_index())));
11741 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11742 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
11743 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11744 if (got_page_reloc(r_type) && !local)
11745 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11746 : elfcpp::R_MIPS_GOT_DISP);
11748 unsigned int got_offset = 0;
11749 int gp_offset = 0;
11751 // Whether we have to extract addend from instruction.
11752 bool extract_addend = rel_type == elfcpp::SHT_REL;
11753 unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11755 Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11757 // For Mips64 N64 ABI, there may be up to three operations specified per
11758 // record, by the fields r_type, r_type2, and r_type3. The first operation
11759 // takes its addend from the relocation record. Each subsequent operation
11760 // takes as its addend the result of the previous operation.
11761 // The first operation in a record which references a symbol uses the symbol
11762 // implied by r_sym. The next operation in a record which references a symbol
11763 // uses the special symbol value given by the r_ssym field. A third operation
11764 // in a record which references a symbol will assume a NULL symbol,
11765 // i.e. value zero.
11767 // TODO(Vladimir)
11768 // Check if a record references to a symbol.
11769 for (unsigned int i = 0; i < 3; ++i)
11771 if (r_types[i] == elfcpp::R_MIPS_NONE)
11772 break;
11774 // If we didn't apply previous relocation, use its result as addend
11775 // for current.
11776 if (this->calculate_only_)
11778 r_addend = this->calculated_value_;
11779 extract_addend = false;
11782 // In the N32 and 64-bit ABIs there may be multiple consecutive
11783 // relocations for the same offset. In that case we are
11784 // supposed to treat the output of each relocation as the addend
11785 // for the next. For N64 ABI, we are checking offsets only in a
11786 // third operation in a record (r_type3).
11787 this->calculate_only_ =
11788 (object->is_n64() && i < 2
11789 ? r_types[i+1] != elfcpp::R_MIPS_NONE
11790 : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
11792 if (object->is_n64())
11794 if (i == 1)
11796 // Handle special symbol for r_type2 relocation type.
11797 switch (r_ssym)
11799 case RSS_UNDEF:
11800 symval.set_output_value(0);
11801 break;
11802 case RSS_GP:
11803 symval.set_output_value(target->gp_value());
11804 break;
11805 case RSS_GP0:
11806 symval.set_output_value(object->gp_value());
11807 break;
11808 case RSS_LOC:
11809 symval.set_output_value(address);
11810 break;
11811 default:
11812 gold_unreachable();
11814 psymval = &symval;
11816 else if (i == 2)
11818 // For r_type3 symbol value is 0.
11819 symval.set_output_value(0);
11823 bool update_got_entry = false;
11824 switch (r_types[i])
11826 case elfcpp::R_MIPS_NONE:
11827 break;
11828 case elfcpp::R_MIPS_16:
11829 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11830 extract_addend,
11831 this->calculate_only_,
11832 &this->calculated_value_);
11833 break;
11835 case elfcpp::R_MIPS_32:
11836 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11837 target))
11838 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11839 extract_addend,
11840 this->calculate_only_,
11841 &this->calculated_value_);
11842 if (mips_sym != NULL
11843 && (mips_sym->is_mips16() || mips_sym->is_micromips())
11844 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11846 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11847 // already updated by adding +1.
11848 if (mips_sym->has_mips16_fn_stub())
11850 gold_assert(mips_sym->need_fn_stub());
11851 Mips16_stub_section<size, big_endian>* fn_stub =
11852 mips_sym->template get_mips16_fn_stub<big_endian>();
11854 symval.set_output_value(fn_stub->output_address());
11855 psymval = &symval;
11857 got_offset = mips_sym->global_gotoffset();
11858 update_got_entry = true;
11860 break;
11862 case elfcpp::R_MIPS_64:
11863 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11864 target))
11865 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11866 extract_addend,
11867 this->calculate_only_,
11868 &this->calculated_value_, false);
11869 else if (target->is_output_n64() && r_addend != 0)
11870 // Only apply the addend. The static relocation was RELA, but the
11871 // dynamic relocation is REL, so we need to apply the addend.
11872 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11873 extract_addend,
11874 this->calculate_only_,
11875 &this->calculated_value_, true);
11876 break;
11877 case elfcpp::R_MIPS_REL32:
11878 gold_unreachable();
11880 case elfcpp::R_MIPS_PC32:
11881 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11882 r_addend, extract_addend,
11883 this->calculate_only_,
11884 &this->calculated_value_);
11885 break;
11887 case elfcpp::R_MIPS16_26:
11888 // The calculation for R_MIPS16_26 is just the same as for an
11889 // R_MIPS_26. It's only the storage of the relocated field into
11890 // the output file that's different. So, we just fall through to the
11891 // R_MIPS_26 case here.
11892 case elfcpp::R_MIPS_26:
11893 case elfcpp::R_MICROMIPS_26_S1:
11894 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11895 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11896 r_types[i], target->jal_to_bal(), this->calculate_only_,
11897 &this->calculated_value_);
11898 break;
11900 case elfcpp::R_MIPS_HI16:
11901 case elfcpp::R_MIPS16_HI16:
11902 case elfcpp::R_MICROMIPS_HI16:
11903 if (rel_type == elfcpp::SHT_RELA)
11904 reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11905 r_addend, address,
11906 gp_disp, r_types[i],
11907 extract_addend, 0,
11908 target,
11909 this->calculate_only_,
11910 &this->calculated_value_);
11911 else if (rel_type == elfcpp::SHT_REL)
11912 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11913 address, gp_disp, r_types[i],
11914 r_sym, extract_addend);
11915 else
11916 gold_unreachable();
11917 break;
11919 case elfcpp::R_MIPS_LO16:
11920 case elfcpp::R_MIPS16_LO16:
11921 case elfcpp::R_MICROMIPS_LO16:
11922 case elfcpp::R_MICROMIPS_HI0_LO16:
11923 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11924 r_addend, extract_addend, address,
11925 gp_disp, r_types[i], r_sym,
11926 rel_type, this->calculate_only_,
11927 &this->calculated_value_);
11928 break;
11930 case elfcpp::R_MIPS_LITERAL:
11931 case elfcpp::R_MICROMIPS_LITERAL:
11932 // Because we don't merge literal sections, we can handle this
11933 // just like R_MIPS_GPREL16. In the long run, we should merge
11934 // shared literals, and then we will need to additional work
11935 // here.
11937 // Fall through.
11939 case elfcpp::R_MIPS_GPREL16:
11940 case elfcpp::R_MIPS16_GPREL:
11941 case elfcpp::R_MICROMIPS_GPREL7_S2:
11942 case elfcpp::R_MICROMIPS_GPREL16:
11943 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11944 target->adjusted_gp_value(object),
11945 r_addend, extract_addend,
11946 gsym == NULL, r_types[i],
11947 this->calculate_only_,
11948 &this->calculated_value_);
11949 break;
11951 case elfcpp::R_MIPS_PC16:
11952 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11953 r_addend, extract_addend,
11954 this->calculate_only_,
11955 &this->calculated_value_);
11956 break;
11958 case elfcpp::R_MIPS_PC21_S2:
11959 reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11960 r_addend, extract_addend,
11961 this->calculate_only_,
11962 &this->calculated_value_);
11963 break;
11965 case elfcpp::R_MIPS_PC26_S2:
11966 reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11967 r_addend, extract_addend,
11968 this->calculate_only_,
11969 &this->calculated_value_);
11970 break;
11972 case elfcpp::R_MIPS_PC18_S3:
11973 reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11974 r_addend, extract_addend,
11975 this->calculate_only_,
11976 &this->calculated_value_);
11977 break;
11979 case elfcpp::R_MIPS_PC19_S2:
11980 reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
11981 r_addend, extract_addend,
11982 this->calculate_only_,
11983 &this->calculated_value_);
11984 break;
11986 case elfcpp::R_MIPS_PCHI16:
11987 if (rel_type == elfcpp::SHT_RELA)
11988 reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
11989 r_addend, address,
11990 extract_addend, 0,
11991 this->calculate_only_,
11992 &this->calculated_value_);
11993 else if (rel_type == elfcpp::SHT_REL)
11994 reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
11995 r_addend, address, r_sym,
11996 extract_addend);
11997 else
11998 gold_unreachable();
11999 break;
12001 case elfcpp::R_MIPS_PCLO16:
12002 reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
12003 extract_addend, address, r_sym,
12004 rel_type, this->calculate_only_,
12005 &this->calculated_value_);
12006 break;
12007 case elfcpp::R_MICROMIPS_PC7_S1:
12008 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
12009 address, r_addend,
12010 extract_addend,
12011 this->calculate_only_,
12012 &this->calculated_value_);
12013 break;
12014 case elfcpp::R_MICROMIPS_PC10_S1:
12015 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
12016 psymval, address,
12017 r_addend, extract_addend,
12018 this->calculate_only_,
12019 &this->calculated_value_);
12020 break;
12021 case elfcpp::R_MICROMIPS_PC16_S1:
12022 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
12023 psymval, address,
12024 r_addend, extract_addend,
12025 this->calculate_only_,
12026 &this->calculated_value_);
12027 break;
12028 case elfcpp::R_MIPS_GPREL32:
12029 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12030 target->adjusted_gp_value(object),
12031 r_addend, extract_addend,
12032 this->calculate_only_,
12033 &this->calculated_value_);
12034 break;
12035 case elfcpp::R_MIPS_GOT_HI16:
12036 case elfcpp::R_MIPS_CALL_HI16:
12037 case elfcpp::R_MICROMIPS_GOT_HI16:
12038 case elfcpp::R_MICROMIPS_CALL_HI16:
12039 if (gsym != NULL)
12040 got_offset = target->got_section()->got_offset(gsym,
12041 GOT_TYPE_STANDARD,
12042 object);
12043 else
12044 got_offset = target->got_section()->got_offset(r_sym,
12045 GOT_TYPE_STANDARD,
12046 object, r_addend);
12047 gp_offset = target->got_section()->gp_offset(got_offset, object);
12048 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
12049 this->calculate_only_,
12050 &this->calculated_value_);
12051 update_got_entry = changed_symbol_value;
12052 break;
12054 case elfcpp::R_MIPS_GOT_LO16:
12055 case elfcpp::R_MIPS_CALL_LO16:
12056 case elfcpp::R_MICROMIPS_GOT_LO16:
12057 case elfcpp::R_MICROMIPS_CALL_LO16:
12058 if (gsym != NULL)
12059 got_offset = target->got_section()->got_offset(gsym,
12060 GOT_TYPE_STANDARD,
12061 object);
12062 else
12063 got_offset = target->got_section()->got_offset(r_sym,
12064 GOT_TYPE_STANDARD,
12065 object, r_addend);
12066 gp_offset = target->got_section()->gp_offset(got_offset, object);
12067 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
12068 this->calculate_only_,
12069 &this->calculated_value_);
12070 update_got_entry = changed_symbol_value;
12071 break;
12073 case elfcpp::R_MIPS_GOT_DISP:
12074 case elfcpp::R_MICROMIPS_GOT_DISP:
12075 case elfcpp::R_MIPS_EH:
12076 if (gsym != NULL)
12077 got_offset = target->got_section()->got_offset(gsym,
12078 GOT_TYPE_STANDARD,
12079 object);
12080 else
12081 got_offset = target->got_section()->got_offset(r_sym,
12082 GOT_TYPE_STANDARD,
12083 object, r_addend);
12084 gp_offset = target->got_section()->gp_offset(got_offset, object);
12085 if (eh_reloc(r_types[i]))
12086 reloc_status = Reloc_funcs::releh(view, gp_offset,
12087 this->calculate_only_,
12088 &this->calculated_value_);
12089 else
12090 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12091 this->calculate_only_,
12092 &this->calculated_value_);
12093 break;
12094 case elfcpp::R_MIPS_CALL16:
12095 case elfcpp::R_MIPS16_CALL16:
12096 case elfcpp::R_MICROMIPS_CALL16:
12097 gold_assert(gsym != NULL);
12098 got_offset = target->got_section()->got_offset(gsym,
12099 GOT_TYPE_STANDARD,
12100 object);
12101 gp_offset = target->got_section()->gp_offset(got_offset, object);
12102 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12103 this->calculate_only_,
12104 &this->calculated_value_);
12105 // TODO(sasa): We should also initialize update_got_entry
12106 // in other place swhere relgot is called.
12107 update_got_entry = changed_symbol_value;
12108 break;
12110 case elfcpp::R_MIPS_GOT16:
12111 case elfcpp::R_MIPS16_GOT16:
12112 case elfcpp::R_MICROMIPS_GOT16:
12113 if (gsym != NULL)
12115 got_offset = target->got_section()->got_offset(gsym,
12116 GOT_TYPE_STANDARD,
12117 object);
12118 gp_offset = target->got_section()->gp_offset(got_offset, object);
12119 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12120 this->calculate_only_,
12121 &this->calculated_value_);
12123 else
12125 if (rel_type == elfcpp::SHT_RELA)
12126 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
12127 psymval, r_addend,
12128 extract_addend, 0,
12129 target,
12130 this->calculate_only_,
12131 &this->calculated_value_);
12132 else if (rel_type == elfcpp::SHT_REL)
12133 reloc_status = Reloc_funcs::relgot16_local(view, object,
12134 psymval, r_addend,
12135 extract_addend,
12136 r_types[i], r_sym);
12137 else
12138 gold_unreachable();
12140 update_got_entry = changed_symbol_value;
12141 break;
12143 case elfcpp::R_MIPS_TLS_GD:
12144 case elfcpp::R_MIPS16_TLS_GD:
12145 case elfcpp::R_MICROMIPS_TLS_GD:
12146 if (gsym != NULL)
12147 got_offset = target->got_section()->got_offset(gsym,
12148 GOT_TYPE_TLS_PAIR,
12149 object);
12150 else
12151 got_offset = target->got_section()->got_offset(r_sym,
12152 GOT_TYPE_TLS_PAIR,
12153 object, r_addend);
12154 gp_offset = target->got_section()->gp_offset(got_offset, object);
12155 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12156 this->calculate_only_,
12157 &this->calculated_value_);
12158 break;
12160 case elfcpp::R_MIPS_TLS_GOTTPREL:
12161 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12162 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12163 if (gsym != NULL)
12164 got_offset = target->got_section()->got_offset(gsym,
12165 GOT_TYPE_TLS_OFFSET,
12166 object);
12167 else
12168 got_offset = target->got_section()->got_offset(r_sym,
12169 GOT_TYPE_TLS_OFFSET,
12170 object, r_addend);
12171 gp_offset = target->got_section()->gp_offset(got_offset, object);
12172 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12173 this->calculate_only_,
12174 &this->calculated_value_);
12175 break;
12177 case elfcpp::R_MIPS_TLS_LDM:
12178 case elfcpp::R_MIPS16_TLS_LDM:
12179 case elfcpp::R_MICROMIPS_TLS_LDM:
12180 // Relocate the field with the offset of the GOT entry for
12181 // the module index.
12182 got_offset = target->got_section()->tls_ldm_offset(object);
12183 gp_offset = target->got_section()->gp_offset(got_offset, object);
12184 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12185 this->calculate_only_,
12186 &this->calculated_value_);
12187 break;
12189 case elfcpp::R_MIPS_GOT_PAGE:
12190 case elfcpp::R_MICROMIPS_GOT_PAGE:
12191 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12192 r_addend, extract_addend,
12193 this->calculate_only_,
12194 &this->calculated_value_);
12195 break;
12197 case elfcpp::R_MIPS_GOT_OFST:
12198 case elfcpp::R_MICROMIPS_GOT_OFST:
12199 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12200 r_addend, extract_addend,
12201 local, this->calculate_only_,
12202 &this->calculated_value_);
12203 break;
12205 case elfcpp::R_MIPS_JALR:
12206 case elfcpp::R_MICROMIPS_JALR:
12207 // This relocation is only a hint. In some cases, we optimize
12208 // it into a bal instruction. But we don't try to optimize
12209 // when the symbol does not resolve locally.
12210 if (gsym == NULL
12211 || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12212 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12213 r_addend, extract_addend,
12214 cross_mode_jump, r_types[i],
12215 target->jalr_to_bal(),
12216 target->jr_to_b(),
12217 this->calculate_only_,
12218 &this->calculated_value_);
12219 break;
12221 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12222 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12223 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12224 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12225 elfcpp::DTP_OFFSET, r_addend,
12226 extract_addend,
12227 this->calculate_only_,
12228 &this->calculated_value_);
12229 break;
12230 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12231 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12232 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12233 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12234 elfcpp::DTP_OFFSET, r_addend,
12235 extract_addend,
12236 this->calculate_only_,
12237 &this->calculated_value_);
12238 break;
12239 case elfcpp::R_MIPS_TLS_DTPREL32:
12240 case elfcpp::R_MIPS_TLS_DTPREL64:
12241 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12242 elfcpp::DTP_OFFSET, r_addend,
12243 extract_addend,
12244 this->calculate_only_,
12245 &this->calculated_value_);
12246 break;
12247 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12248 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12249 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12250 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12251 elfcpp::TP_OFFSET, r_addend,
12252 extract_addend,
12253 this->calculate_only_,
12254 &this->calculated_value_);
12255 break;
12256 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12257 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12258 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12259 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12260 elfcpp::TP_OFFSET, r_addend,
12261 extract_addend,
12262 this->calculate_only_,
12263 &this->calculated_value_);
12264 break;
12265 case elfcpp::R_MIPS_TLS_TPREL32:
12266 case elfcpp::R_MIPS_TLS_TPREL64:
12267 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12268 elfcpp::TP_OFFSET, r_addend,
12269 extract_addend,
12270 this->calculate_only_,
12271 &this->calculated_value_);
12272 break;
12273 case elfcpp::R_MIPS_SUB:
12274 case elfcpp::R_MICROMIPS_SUB:
12275 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12276 extract_addend,
12277 this->calculate_only_,
12278 &this->calculated_value_);
12279 break;
12280 case elfcpp::R_MIPS_HIGHER:
12281 case elfcpp::R_MICROMIPS_HIGHER:
12282 reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
12283 extract_addend,
12284 this->calculate_only_,
12285 &this->calculated_value_);
12286 break;
12287 case elfcpp::R_MIPS_HIGHEST:
12288 case elfcpp::R_MICROMIPS_HIGHEST:
12289 reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12290 r_addend, extract_addend,
12291 this->calculate_only_,
12292 &this->calculated_value_);
12293 break;
12294 default:
12295 gold_error_at_location(relinfo, relnum, r_offset,
12296 _("unsupported reloc %u"), r_types[i]);
12297 break;
12300 if (update_got_entry)
12302 Mips_output_data_got<size, big_endian>* got = target->got_section();
12303 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12304 got->update_got_entry(got->get_primary_got_offset(mips_sym),
12305 psymval->value(object, 0));
12306 else
12307 got->update_got_entry(got_offset, psymval->value(object, 0));
12311 bool jal_shuffle = jal_reloc(r_type);
12312 Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12314 // Report any errors.
12315 switch (reloc_status)
12317 case Reloc_funcs::STATUS_OKAY:
12318 break;
12319 case Reloc_funcs::STATUS_OVERFLOW:
12320 if (gsym == NULL)
12321 gold_error_at_location(relinfo, relnum, r_offset,
12322 _("relocation overflow: "
12323 "%u against local symbol %u in %s"),
12324 r_type, r_sym, object->name().c_str());
12325 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12326 gold_error_at_location(relinfo, relnum, r_offset,
12327 _("relocation overflow: "
12328 "%u against '%s' defined in %s"),
12329 r_type, gsym->demangled_name().c_str(),
12330 gsym->object()->name().c_str());
12331 else
12332 gold_error_at_location(relinfo, relnum, r_offset,
12333 _("relocation overflow: %u against '%s'"),
12334 r_type, gsym->demangled_name().c_str());
12335 break;
12336 case Reloc_funcs::STATUS_BAD_RELOC:
12337 gold_error_at_location(relinfo, relnum, r_offset,
12338 _("unexpected opcode while processing relocation"));
12339 break;
12340 case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12341 gold_error_at_location(relinfo, relnum, r_offset,
12342 _("unaligned PC-relative relocation"));
12343 break;
12344 default:
12345 gold_unreachable();
12348 return true;
12351 // Get the Reference_flags for a particular relocation.
12353 template<int size, bool big_endian>
12355 Target_mips<size, big_endian>::Scan::get_reference_flags(
12356 unsigned int r_type)
12358 switch (r_type)
12360 case elfcpp::R_MIPS_NONE:
12361 // No symbol reference.
12362 return 0;
12364 case elfcpp::R_MIPS_16:
12365 case elfcpp::R_MIPS_32:
12366 case elfcpp::R_MIPS_64:
12367 case elfcpp::R_MIPS_HI16:
12368 case elfcpp::R_MIPS_LO16:
12369 case elfcpp::R_MIPS_HIGHER:
12370 case elfcpp::R_MIPS_HIGHEST:
12371 case elfcpp::R_MIPS16_HI16:
12372 case elfcpp::R_MIPS16_LO16:
12373 case elfcpp::R_MICROMIPS_HI16:
12374 case elfcpp::R_MICROMIPS_LO16:
12375 case elfcpp::R_MICROMIPS_HIGHER:
12376 case elfcpp::R_MICROMIPS_HIGHEST:
12377 return Symbol::ABSOLUTE_REF;
12379 case elfcpp::R_MIPS_26:
12380 case elfcpp::R_MIPS16_26:
12381 case elfcpp::R_MICROMIPS_26_S1:
12382 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12384 case elfcpp::R_MIPS_PC18_S3:
12385 case elfcpp::R_MIPS_PC19_S2:
12386 case elfcpp::R_MIPS_PCHI16:
12387 case elfcpp::R_MIPS_PCLO16:
12388 case elfcpp::R_MIPS_GPREL32:
12389 case elfcpp::R_MIPS_GPREL16:
12390 case elfcpp::R_MIPS_REL32:
12391 case elfcpp::R_MIPS16_GPREL:
12392 return Symbol::RELATIVE_REF;
12394 case elfcpp::R_MIPS_PC16:
12395 case elfcpp::R_MIPS_PC32:
12396 case elfcpp::R_MIPS_PC21_S2:
12397 case elfcpp::R_MIPS_PC26_S2:
12398 case elfcpp::R_MIPS_JALR:
12399 case elfcpp::R_MICROMIPS_JALR:
12400 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12402 case elfcpp::R_MIPS_GOT16:
12403 case elfcpp::R_MIPS_CALL16:
12404 case elfcpp::R_MIPS_GOT_DISP:
12405 case elfcpp::R_MIPS_GOT_HI16:
12406 case elfcpp::R_MIPS_GOT_LO16:
12407 case elfcpp::R_MIPS_CALL_HI16:
12408 case elfcpp::R_MIPS_CALL_LO16:
12409 case elfcpp::R_MIPS_LITERAL:
12410 case elfcpp::R_MIPS_GOT_PAGE:
12411 case elfcpp::R_MIPS_GOT_OFST:
12412 case elfcpp::R_MIPS16_GOT16:
12413 case elfcpp::R_MIPS16_CALL16:
12414 case elfcpp::R_MICROMIPS_GOT16:
12415 case elfcpp::R_MICROMIPS_CALL16:
12416 case elfcpp::R_MICROMIPS_GOT_HI16:
12417 case elfcpp::R_MICROMIPS_GOT_LO16:
12418 case elfcpp::R_MICROMIPS_CALL_HI16:
12419 case elfcpp::R_MICROMIPS_CALL_LO16:
12420 case elfcpp::R_MIPS_EH:
12421 // Absolute in GOT.
12422 return Symbol::RELATIVE_REF;
12424 case elfcpp::R_MIPS_TLS_DTPMOD32:
12425 case elfcpp::R_MIPS_TLS_DTPREL32:
12426 case elfcpp::R_MIPS_TLS_DTPMOD64:
12427 case elfcpp::R_MIPS_TLS_DTPREL64:
12428 case elfcpp::R_MIPS_TLS_GD:
12429 case elfcpp::R_MIPS_TLS_LDM:
12430 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12431 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12432 case elfcpp::R_MIPS_TLS_GOTTPREL:
12433 case elfcpp::R_MIPS_TLS_TPREL32:
12434 case elfcpp::R_MIPS_TLS_TPREL64:
12435 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12436 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12437 case elfcpp::R_MIPS16_TLS_GD:
12438 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12439 case elfcpp::R_MICROMIPS_TLS_GD:
12440 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12441 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12442 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12443 return Symbol::TLS_REF;
12445 case elfcpp::R_MIPS_COPY:
12446 case elfcpp::R_MIPS_JUMP_SLOT:
12447 default:
12448 // Not expected. We will give an error later.
12449 return 0;
12453 // Report an unsupported relocation against a local symbol.
12455 template<int size, bool big_endian>
12456 void
12457 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12458 Sized_relobj_file<size, big_endian>* object,
12459 unsigned int r_type)
12461 gold_error(_("%s: unsupported reloc %u against local symbol"),
12462 object->name().c_str(), r_type);
12465 // Report an unsupported relocation against a global symbol.
12467 template<int size, bool big_endian>
12468 void
12469 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12470 Sized_relobj_file<size, big_endian>* object,
12471 unsigned int r_type,
12472 Symbol* gsym)
12474 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12475 object->name().c_str(), r_type, gsym->demangled_name().c_str());
12478 // Return printable name for ABI.
12479 template<int size, bool big_endian>
12480 const char*
12481 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12483 switch (e_flags & elfcpp::EF_MIPS_ABI)
12485 case 0:
12486 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12487 return "N32";
12488 else if (size == 64)
12489 return "64";
12490 else
12491 return "none";
12492 case elfcpp::E_MIPS_ABI_O32:
12493 return "O32";
12494 case elfcpp::E_MIPS_ABI_O64:
12495 return "O64";
12496 case elfcpp::E_MIPS_ABI_EABI32:
12497 return "EABI32";
12498 case elfcpp::E_MIPS_ABI_EABI64:
12499 return "EABI64";
12500 default:
12501 return "unknown abi";
12505 template<int size, bool big_endian>
12506 const char*
12507 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12509 switch (e_flags & elfcpp::EF_MIPS_MACH)
12511 case elfcpp::E_MIPS_MACH_3900:
12512 return "mips:3900";
12513 case elfcpp::E_MIPS_MACH_4010:
12514 return "mips:4010";
12515 case elfcpp::E_MIPS_MACH_4100:
12516 return "mips:4100";
12517 case elfcpp::E_MIPS_MACH_4111:
12518 return "mips:4111";
12519 case elfcpp::E_MIPS_MACH_4120:
12520 return "mips:4120";
12521 case elfcpp::E_MIPS_MACH_4650:
12522 return "mips:4650";
12523 case elfcpp::E_MIPS_MACH_5400:
12524 return "mips:5400";
12525 case elfcpp::E_MIPS_MACH_5500:
12526 return "mips:5500";
12527 case elfcpp::E_MIPS_MACH_5900:
12528 return "mips:5900";
12529 case elfcpp::E_MIPS_MACH_SB1:
12530 return "mips:sb1";
12531 case elfcpp::E_MIPS_MACH_9000:
12532 return "mips:9000";
12533 case elfcpp::E_MIPS_MACH_LS2E:
12534 return "mips:loongson_2e";
12535 case elfcpp::E_MIPS_MACH_LS2F:
12536 return "mips:loongson_2f";
12537 case elfcpp::E_MIPS_MACH_GS464:
12538 return "mips:gs464";
12539 case elfcpp::E_MIPS_MACH_GS464E:
12540 return "mips:gs464e";
12541 case elfcpp::E_MIPS_MACH_GS264E:
12542 return "mips:gs264e";
12543 case elfcpp::E_MIPS_MACH_OCTEON:
12544 return "mips:octeon";
12545 case elfcpp::E_MIPS_MACH_OCTEON2:
12546 return "mips:octeon2";
12547 case elfcpp::E_MIPS_MACH_OCTEON3:
12548 return "mips:octeon3";
12549 case elfcpp::E_MIPS_MACH_XLR:
12550 return "mips:xlr";
12551 default:
12552 switch (e_flags & elfcpp::EF_MIPS_ARCH)
12554 default:
12555 case elfcpp::E_MIPS_ARCH_1:
12556 return "mips:3000";
12558 case elfcpp::E_MIPS_ARCH_2:
12559 return "mips:6000";
12561 case elfcpp::E_MIPS_ARCH_3:
12562 return "mips:4000";
12564 case elfcpp::E_MIPS_ARCH_4:
12565 return "mips:8000";
12567 case elfcpp::E_MIPS_ARCH_5:
12568 return "mips:mips5";
12570 case elfcpp::E_MIPS_ARCH_32:
12571 return "mips:isa32";
12573 case elfcpp::E_MIPS_ARCH_64:
12574 return "mips:isa64";
12576 case elfcpp::E_MIPS_ARCH_32R2:
12577 return "mips:isa32r2";
12579 case elfcpp::E_MIPS_ARCH_32R6:
12580 return "mips:isa32r6";
12582 case elfcpp::E_MIPS_ARCH_64R2:
12583 return "mips:isa64r2";
12585 case elfcpp::E_MIPS_ARCH_64R6:
12586 return "mips:isa64r6";
12589 return "unknown CPU";
12592 template<int size, bool big_endian>
12593 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12595 size, // size
12596 big_endian, // is_big_endian
12597 elfcpp::EM_MIPS, // machine_code
12598 true, // has_make_symbol
12599 false, // has_resolve
12600 false, // has_code_fill
12601 true, // is_default_stack_executable
12602 false, // can_icf_inline_merge_sections
12603 '\0', // wrap_char
12604 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
12605 0x400000, // default_text_segment_address
12606 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
12607 4 * 1024, // common_pagesize (overridable by -z common-page-size)
12608 false, // isolate_execinstr
12609 0, // rosegment_gap
12610 elfcpp::SHN_UNDEF, // small_common_shndx
12611 elfcpp::SHN_UNDEF, // large_common_shndx
12612 0, // small_common_section_flags
12613 0, // large_common_section_flags
12614 NULL, // attributes_section
12615 NULL, // attributes_vendor
12616 "__start", // entry_symbol_name
12617 32, // hash_entry_size
12618 elfcpp::SHT_PROGBITS, // unwind_section_type
12621 template<int size, bool big_endian>
12622 class Target_mips_nacl : public Target_mips<size, big_endian>
12624 public:
12625 Target_mips_nacl()
12626 : Target_mips<size, big_endian>(&mips_nacl_info)
12629 private:
12630 static const Target::Target_info mips_nacl_info;
12633 template<int size, bool big_endian>
12634 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12636 size, // size
12637 big_endian, // is_big_endian
12638 elfcpp::EM_MIPS, // machine_code
12639 true, // has_make_symbol
12640 false, // has_resolve
12641 false, // has_code_fill
12642 true, // is_default_stack_executable
12643 false, // can_icf_inline_merge_sections
12644 '\0', // wrap_char
12645 "/lib/ld.so.1", // dynamic_linker
12646 0x20000, // default_text_segment_address
12647 0x10000, // abi_pagesize (overridable by -z max-page-size)
12648 0x10000, // common_pagesize (overridable by -z common-page-size)
12649 true, // isolate_execinstr
12650 0x10000000, // rosegment_gap
12651 elfcpp::SHN_UNDEF, // small_common_shndx
12652 elfcpp::SHN_UNDEF, // large_common_shndx
12653 0, // small_common_section_flags
12654 0, // large_common_section_flags
12655 NULL, // attributes_section
12656 NULL, // attributes_vendor
12657 "_start", // entry_symbol_name
12658 32, // hash_entry_size
12659 elfcpp::SHT_PROGBITS, // unwind_section_type
12662 // Target selector for Mips. Note this is never instantiated directly.
12663 // It's only used in Target_selector_mips_nacl, below.
12665 template<int size, bool big_endian>
12666 class Target_selector_mips : public Target_selector
12668 public:
12669 Target_selector_mips()
12670 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12671 (size == 64 ?
12672 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12673 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12674 (size == 64 ?
12675 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12676 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12679 Target* do_instantiate_target()
12680 { return new Target_mips<size, big_endian>(); }
12683 template<int size, bool big_endian>
12684 class Target_selector_mips_nacl
12685 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12686 Target_mips_nacl<size, big_endian> >
12688 public:
12689 Target_selector_mips_nacl()
12690 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12691 Target_mips_nacl<size, big_endian> >(
12692 // NaCl currently supports only MIPS32 little-endian.
12693 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12697 Target_selector_mips_nacl<32, true> target_selector_mips32;
12698 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12699 Target_selector_mips_nacl<64, true> target_selector_mips64;
12700 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12702 } // End anonymous namespace.