locate(1): Bring in some updates/fixes from FreeBSD
[dragonfly.git] / contrib / binutils-2.27 / gold / mips.cc
blob8893e315dd57d78b1c36424bce4d2cffbb8a59ec
1 // mips.cc -- mips target support for gold.
3 // Copyright (C) 2011-2016 Free Software Foundation, Inc.
4 // Written by Sasa Stankovic <sasa.stankovic@imgtec.com>
5 // and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
6 // This file contains borrowed and adapted code from bfd/elfxx-mips.c.
8 // This file is part of gold.
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
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_data_la25_stub;
69 template<int size, bool big_endian>
70 class Mips_output_data_mips_stubs;
72 template<int size>
73 class Mips_symbol;
75 template<int size, bool big_endian>
76 class Mips_got_info;
78 template<int size, bool big_endian>
79 class Mips_relobj;
81 class Mips16_stub_section_base;
83 template<int size, bool big_endian>
84 class Mips16_stub_section;
86 // The ABI says that every symbol used by dynamic relocations must have
87 // a global GOT entry. Among other things, this provides the dynamic
88 // linker with a free, directly-indexed cache. The GOT can therefore
89 // contain symbols that are not referenced by GOT relocations themselves
90 // (in other words, it may have symbols that are not referenced by things
91 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
93 // GOT relocations are less likely to overflow if we put the associated
94 // GOT entries towards the beginning. We therefore divide the global
95 // GOT entries into two areas: "normal" and "reloc-only". Entries in
96 // the first area can be used for both dynamic relocations and GP-relative
97 // accesses, while those in the "reloc-only" area are for dynamic
98 // relocations only.
100 // These GGA_* ("Global GOT Area") values are organised so that lower
101 // values are more general than higher values. Also, non-GGA_NONE
102 // values are ordered by the position of the area in the GOT.
104 enum Global_got_area
106 GGA_NORMAL = 0,
107 GGA_RELOC_ONLY = 1,
108 GGA_NONE = 2
111 // The types of GOT entries needed for this platform.
112 // These values are exposed to the ABI in an incremental link.
113 // Do not renumber existing values without changing the version
114 // number of the .gnu_incremental_inputs section.
115 enum Got_type
117 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
118 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
119 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
121 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
122 GOT_TYPE_STANDARD_MULTIGOT = 3,
123 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
124 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
127 // TLS type of GOT entry.
128 enum Got_tls_type
130 GOT_TLS_NONE = 0,
131 GOT_TLS_GD = 1,
132 GOT_TLS_LDM = 2,
133 GOT_TLS_IE = 4
136 // Values found in the r_ssym field of a relocation entry.
137 enum Special_relocation_symbol
139 RSS_UNDEF = 0, // None - value is zero.
140 RSS_GP = 1, // Value of GP.
141 RSS_GP0 = 2, // Value of GP in object being relocated.
142 RSS_LOC = 3 // Address of location being relocated.
145 // Whether the section is readonly.
146 static inline bool
147 is_readonly_section(Output_section* output_section)
149 elfcpp::Elf_Xword section_flags = output_section->flags();
150 elfcpp::Elf_Word section_type = output_section->type();
152 if (section_type == elfcpp::SHT_NOBITS)
153 return false;
155 if (section_flags & elfcpp::SHF_WRITE)
156 return false;
158 return true;
161 // Return TRUE if a relocation of type R_TYPE from OBJECT might
162 // require an la25 stub. See also local_pic_function, which determines
163 // whether the destination function ever requires a stub.
164 template<int size, bool big_endian>
165 static inline bool
166 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
167 unsigned int r_type, bool target_is_16_bit_code)
169 // We specifically ignore branches and jumps from EF_PIC objects,
170 // where the onus is on the compiler or programmer to perform any
171 // necessary initialization of $25. Sometimes such initialization
172 // is unnecessary; for example, -mno-shared functions do not use
173 // the incoming value of $25, and may therefore be called directly.
174 if (object->is_pic())
175 return false;
177 switch (r_type)
179 case elfcpp::R_MIPS_26:
180 case elfcpp::R_MIPS_PC16:
181 case elfcpp::R_MIPS_PC21_S2:
182 case elfcpp::R_MIPS_PC26_S2:
183 case elfcpp::R_MICROMIPS_26_S1:
184 case elfcpp::R_MICROMIPS_PC7_S1:
185 case elfcpp::R_MICROMIPS_PC10_S1:
186 case elfcpp::R_MICROMIPS_PC16_S1:
187 case elfcpp::R_MICROMIPS_PC23_S2:
188 return true;
190 case elfcpp::R_MIPS16_26:
191 return !target_is_16_bit_code;
193 default:
194 return false;
198 // Return true if SYM is a locally-defined PIC function, in the sense
199 // that it or its fn_stub might need $25 to be valid on entry.
200 // Note that MIPS16 functions set up $gp using PC-relative instructions,
201 // so they themselves never need $25 to be valid. Only non-MIPS16
202 // entry points are of interest here.
203 template<int size, bool big_endian>
204 static inline bool
205 local_pic_function(Mips_symbol<size>* sym)
207 bool def_regular = (sym->source() == Symbol::FROM_OBJECT
208 && !sym->object()->is_dynamic()
209 && !sym->is_undefined());
211 if (sym->is_defined() && def_regular)
213 Mips_relobj<size, big_endian>* object =
214 static_cast<Mips_relobj<size, big_endian>*>(sym->object());
216 if ((object->is_pic() || sym->is_pic())
217 && (!sym->is_mips16()
218 || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
219 return true;
221 return false;
224 static inline bool
225 hi16_reloc(int r_type)
227 return (r_type == elfcpp::R_MIPS_HI16
228 || r_type == elfcpp::R_MIPS16_HI16
229 || r_type == elfcpp::R_MICROMIPS_HI16
230 || r_type == elfcpp::R_MIPS_PCHI16);
233 static inline bool
234 lo16_reloc(int r_type)
236 return (r_type == elfcpp::R_MIPS_LO16
237 || r_type == elfcpp::R_MIPS16_LO16
238 || r_type == elfcpp::R_MICROMIPS_LO16
239 || r_type == elfcpp::R_MIPS_PCLO16);
242 static inline bool
243 got16_reloc(unsigned int r_type)
245 return (r_type == elfcpp::R_MIPS_GOT16
246 || r_type == elfcpp::R_MIPS16_GOT16
247 || r_type == elfcpp::R_MICROMIPS_GOT16);
250 static inline bool
251 call_lo16_reloc(unsigned int r_type)
253 return (r_type == elfcpp::R_MIPS_CALL_LO16
254 || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
257 static inline bool
258 got_lo16_reloc(unsigned int r_type)
260 return (r_type == elfcpp::R_MIPS_GOT_LO16
261 || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
264 static inline bool
265 eh_reloc(unsigned int r_type)
267 return (r_type == elfcpp::R_MIPS_EH);
270 static inline bool
271 got_disp_reloc(unsigned int r_type)
273 return (r_type == elfcpp::R_MIPS_GOT_DISP
274 || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
277 static inline bool
278 got_page_reloc(unsigned int r_type)
280 return (r_type == elfcpp::R_MIPS_GOT_PAGE
281 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
284 static inline bool
285 tls_gd_reloc(unsigned int r_type)
287 return (r_type == elfcpp::R_MIPS_TLS_GD
288 || r_type == elfcpp::R_MIPS16_TLS_GD
289 || r_type == elfcpp::R_MICROMIPS_TLS_GD);
292 static inline bool
293 tls_gottprel_reloc(unsigned int r_type)
295 return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
296 || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
297 || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
300 static inline bool
301 tls_ldm_reloc(unsigned int r_type)
303 return (r_type == elfcpp::R_MIPS_TLS_LDM
304 || r_type == elfcpp::R_MIPS16_TLS_LDM
305 || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
308 static inline bool
309 mips16_call_reloc(unsigned int r_type)
311 return (r_type == elfcpp::R_MIPS16_26
312 || r_type == elfcpp::R_MIPS16_CALL16);
315 static inline bool
316 jal_reloc(unsigned int r_type)
318 return (r_type == elfcpp::R_MIPS_26
319 || r_type == elfcpp::R_MIPS16_26
320 || r_type == elfcpp::R_MICROMIPS_26_S1);
323 static inline bool
324 micromips_branch_reloc(unsigned int r_type)
326 return (r_type == elfcpp::R_MICROMIPS_26_S1
327 || r_type == elfcpp::R_MICROMIPS_PC16_S1
328 || r_type == elfcpp::R_MICROMIPS_PC10_S1
329 || r_type == elfcpp::R_MICROMIPS_PC7_S1);
332 // Check if R_TYPE is a MIPS16 reloc.
333 static inline bool
334 mips16_reloc(unsigned int r_type)
336 switch (r_type)
338 case elfcpp::R_MIPS16_26:
339 case elfcpp::R_MIPS16_GPREL:
340 case elfcpp::R_MIPS16_GOT16:
341 case elfcpp::R_MIPS16_CALL16:
342 case elfcpp::R_MIPS16_HI16:
343 case elfcpp::R_MIPS16_LO16:
344 case elfcpp::R_MIPS16_TLS_GD:
345 case elfcpp::R_MIPS16_TLS_LDM:
346 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
347 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
348 case elfcpp::R_MIPS16_TLS_GOTTPREL:
349 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
350 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
351 return true;
353 default:
354 return false;
358 // Check if R_TYPE is a microMIPS reloc.
359 static inline bool
360 micromips_reloc(unsigned int r_type)
362 switch (r_type)
364 case elfcpp::R_MICROMIPS_26_S1:
365 case elfcpp::R_MICROMIPS_HI16:
366 case elfcpp::R_MICROMIPS_LO16:
367 case elfcpp::R_MICROMIPS_GPREL16:
368 case elfcpp::R_MICROMIPS_LITERAL:
369 case elfcpp::R_MICROMIPS_GOT16:
370 case elfcpp::R_MICROMIPS_PC7_S1:
371 case elfcpp::R_MICROMIPS_PC10_S1:
372 case elfcpp::R_MICROMIPS_PC16_S1:
373 case elfcpp::R_MICROMIPS_CALL16:
374 case elfcpp::R_MICROMIPS_GOT_DISP:
375 case elfcpp::R_MICROMIPS_GOT_PAGE:
376 case elfcpp::R_MICROMIPS_GOT_OFST:
377 case elfcpp::R_MICROMIPS_GOT_HI16:
378 case elfcpp::R_MICROMIPS_GOT_LO16:
379 case elfcpp::R_MICROMIPS_SUB:
380 case elfcpp::R_MICROMIPS_HIGHER:
381 case elfcpp::R_MICROMIPS_HIGHEST:
382 case elfcpp::R_MICROMIPS_CALL_HI16:
383 case elfcpp::R_MICROMIPS_CALL_LO16:
384 case elfcpp::R_MICROMIPS_SCN_DISP:
385 case elfcpp::R_MICROMIPS_JALR:
386 case elfcpp::R_MICROMIPS_HI0_LO16:
387 case elfcpp::R_MICROMIPS_TLS_GD:
388 case elfcpp::R_MICROMIPS_TLS_LDM:
389 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
390 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
391 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
392 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
393 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
394 case elfcpp::R_MICROMIPS_GPREL7_S2:
395 case elfcpp::R_MICROMIPS_PC23_S2:
396 return true;
398 default:
399 return false;
403 static inline bool
404 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
406 switch (high_reloc)
408 case elfcpp::R_MIPS_HI16:
409 case elfcpp::R_MIPS_GOT16:
410 return lo16_reloc == elfcpp::R_MIPS_LO16;
411 case elfcpp::R_MIPS_PCHI16:
412 return lo16_reloc == elfcpp::R_MIPS_PCLO16;
413 case elfcpp::R_MIPS16_HI16:
414 case elfcpp::R_MIPS16_GOT16:
415 return lo16_reloc == elfcpp::R_MIPS16_LO16;
416 case elfcpp::R_MICROMIPS_HI16:
417 case elfcpp::R_MICROMIPS_GOT16:
418 return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
419 default:
420 return false;
424 // This class is used to hold information about one GOT entry.
425 // There are three types of entry:
427 // (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
428 // (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
429 // (2) a SYMBOL address, where SYMBOL is not local to an input object
430 // (sym != NULL, symndx == -1)
431 // (3) a TLS LDM slot (there's only one of these per GOT.)
432 // (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
434 template<int size, bool big_endian>
435 class Mips_got_entry
437 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
439 public:
440 Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
441 Mips_address addend, unsigned char tls_type,
442 unsigned int shndx, bool is_section_symbol)
443 : addend_(addend), symndx_(symndx), tls_type_(tls_type),
444 is_section_symbol_(is_section_symbol), shndx_(shndx)
445 { this->d.object = object; }
447 Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
448 : addend_(0), symndx_(-1U), tls_type_(tls_type),
449 is_section_symbol_(false), shndx_(-1U)
450 { this->d.sym = sym; }
452 // Return whether this entry is for a local symbol.
453 bool
454 is_for_local_symbol() const
455 { return this->symndx_ != -1U; }
457 // Return whether this entry is for a global symbol.
458 bool
459 is_for_global_symbol() const
460 { return this->symndx_ == -1U; }
462 // Return the hash of this entry.
463 size_t
464 hash() const
466 if (this->tls_type_ == GOT_TLS_LDM)
467 return this->symndx_ + (1 << 18);
469 size_t name_hash_value = gold::string_hash<char>(
470 (this->symndx_ != -1U)
471 ? this->d.object->name().c_str()
472 : this->d.sym->name());
473 size_t addend = this->addend_;
474 return name_hash_value ^ this->symndx_ ^ addend;
477 // Return whether this entry is equal to OTHER.
478 bool
479 equals(Mips_got_entry<size, big_endian>* other) const
481 if (this->tls_type_ == GOT_TLS_LDM)
482 return true;
484 return ((this->tls_type_ == other->tls_type_)
485 && (this->symndx_ == other->symndx_)
486 && ((this->symndx_ != -1U)
487 ? (this->d.object == other->d.object)
488 : (this->d.sym == other->d.sym))
489 && (this->addend_ == other->addend_));
492 // Return input object that needs this GOT entry.
493 Mips_relobj<size, big_endian>*
494 object() const
496 gold_assert(this->symndx_ != -1U);
497 return this->d.object;
500 // Return local symbol index for local GOT entries.
501 unsigned int
502 symndx() const
504 gold_assert(this->symndx_ != -1U);
505 return this->symndx_;
508 // Return the relocation addend for local GOT entries.
509 Mips_address
510 addend() const
511 { return this->addend_; }
513 // Return global symbol for global GOT entries.
514 Mips_symbol<size>*
515 sym() const
517 gold_assert(this->symndx_ == -1U);
518 return this->d.sym;
521 // Return whether this is a TLS GOT entry.
522 bool
523 is_tls_entry() const
524 { return this->tls_type_ != GOT_TLS_NONE; }
526 // Return TLS type of this GOT entry.
527 unsigned char
528 tls_type() const
529 { return this->tls_type_; }
531 // Return section index of the local symbol for local GOT entries.
532 unsigned int
533 shndx() const
534 { return this->shndx_; }
536 // Return whether this is a STT_SECTION symbol.
537 bool
538 is_section_symbol() const
539 { return this->is_section_symbol_; }
541 private:
542 // The addend.
543 Mips_address addend_;
545 // The index of the symbol if we have a local symbol; -1 otherwise.
546 unsigned int symndx_;
548 union
550 // The input object for local symbols that needs the GOT entry.
551 Mips_relobj<size, big_endian>* object;
552 // If symndx == -1, the global symbol corresponding to this GOT entry. The
553 // symbol's entry is in the local area if mips_sym->global_got_area is
554 // GGA_NONE, otherwise it is in the global area.
555 Mips_symbol<size>* sym;
556 } d;
558 // The TLS type of this GOT entry. An LDM GOT entry will be a local
559 // symbol entry with r_symndx == 0.
560 unsigned char tls_type_;
562 // Whether this is a STT_SECTION symbol.
563 bool is_section_symbol_;
565 // For local GOT entries, section index of the local symbol.
566 unsigned int shndx_;
569 // Hash for Mips_got_entry.
571 template<int size, bool big_endian>
572 class Mips_got_entry_hash
574 public:
575 size_t
576 operator()(Mips_got_entry<size, big_endian>* entry) const
577 { return entry->hash(); }
580 // Equality for Mips_got_entry.
582 template<int size, bool big_endian>
583 class Mips_got_entry_eq
585 public:
586 bool
587 operator()(Mips_got_entry<size, big_endian>* e1,
588 Mips_got_entry<size, big_endian>* e2) const
589 { return e1->equals(e2); }
592 // Hash for Mips_symbol.
594 template<int size>
595 class Mips_symbol_hash
597 public:
598 size_t
599 operator()(Mips_symbol<size>* sym) const
600 { return sym->hash(); }
603 // Got_page_range. This class describes a range of addends: [MIN_ADDEND,
604 // MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
605 // increasing MIN_ADDEND.
607 struct Got_page_range
609 Got_page_range()
610 : next(NULL), min_addend(0), max_addend(0)
613 Got_page_range* next;
614 int min_addend;
615 int max_addend;
617 // Return the maximum number of GOT page entries required.
619 get_max_pages()
620 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
623 // Got_page_entry. This class describes the range of addends that are applied
624 // to page relocations against a given symbol.
626 struct Got_page_entry
628 Got_page_entry()
629 : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
632 Got_page_entry(Object* object_, unsigned int symndx_)
633 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
636 // The input object that needs the GOT page entry.
637 Object* object;
638 // The index of the symbol, as stored in the relocation r_info.
639 unsigned int symndx;
640 // The ranges for this page entry.
641 Got_page_range* ranges;
642 // The maximum number of page entries needed for RANGES.
643 unsigned int num_pages;
646 // Hash for Got_page_entry.
648 struct Got_page_entry_hash
650 size_t
651 operator()(Got_page_entry* entry) const
652 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
655 // Equality for Got_page_entry.
657 struct Got_page_entry_eq
659 bool
660 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
662 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
666 // This class is used to hold .got information when linking.
668 template<int size, bool big_endian>
669 class Mips_got_info
671 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
672 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
673 Reloc_section;
674 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
676 // Unordered set of GOT entries.
677 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
678 Mips_got_entry_hash<size, big_endian>,
679 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
681 // Unordered set of GOT page entries.
682 typedef Unordered_set<Got_page_entry*,
683 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
685 // Unordered set of global GOT entries.
686 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
687 Global_got_entry_set;
689 public:
690 Mips_got_info()
691 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
692 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
693 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
694 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
695 offset_(0)
698 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
699 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
700 void
701 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
702 unsigned int symndx, Mips_address addend,
703 unsigned int r_type, unsigned int shndx,
704 bool is_section_symbol);
706 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
707 // in OBJECT. FOR_CALL is true if the caller is only interested in
708 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
709 // relocation.
710 void
711 record_global_got_symbol(Mips_symbol<size>* mips_sym,
712 Mips_relobj<size, big_endian>* object,
713 unsigned int r_type, bool dyn_reloc, bool for_call);
715 // Add ENTRY to master GOT and to OBJECT's GOT.
716 void
717 record_got_entry(Mips_got_entry<size, big_endian>* entry,
718 Mips_relobj<size, big_endian>* object);
720 // Record that OBJECT has a page relocation against symbol SYMNDX and
721 // that ADDEND is the addend for that relocation.
722 void
723 record_got_page_entry(Mips_relobj<size, big_endian>* object,
724 unsigned int symndx, int addend);
726 // Create all entries that should be in the local part of the GOT.
727 void
728 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
730 // Create GOT page entries.
731 void
732 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
734 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
735 void
736 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
737 unsigned int non_reloc_only_global_gotno);
739 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
740 void
741 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
743 // Create TLS GOT entries.
744 void
745 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
747 // Decide whether the symbol needs an entry in the global part of the primary
748 // GOT, setting global_got_area accordingly. Count the number of global
749 // symbols that are in the primary GOT only because they have dynamic
750 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
751 void
752 count_got_symbols(Symbol_table* symtab);
754 // Return the offset of GOT page entry for VALUE.
755 unsigned int
756 get_got_page_offset(Mips_address value,
757 Mips_output_data_got<size, big_endian>* got);
759 // Count the number of GOT entries required.
760 void
761 count_got_entries();
763 // Count the number of GOT entries required by ENTRY. Accumulate the result.
764 void
765 count_got_entry(Mips_got_entry<size, big_endian>* entry);
767 // Add FROM's GOT entries.
768 void
769 add_got_entries(Mips_got_info<size, big_endian>* from);
771 // Add FROM's GOT page entries.
772 void
773 add_got_page_entries(Mips_got_info<size, big_endian>* from);
775 // Return GOT size.
776 unsigned int
777 got_size() const
778 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
779 + this->tls_gotno_) * size/8);
782 // Return the number of local GOT entries.
783 unsigned int
784 local_gotno() const
785 { return this->local_gotno_; }
787 // Return the maximum number of page GOT entries needed.
788 unsigned int
789 page_gotno() const
790 { return this->page_gotno_; }
792 // Return the number of global GOT entries.
793 unsigned int
794 global_gotno() const
795 { return this->global_gotno_; }
797 // Set the number of global GOT entries.
798 void
799 set_global_gotno(unsigned int global_gotno)
800 { this->global_gotno_ = global_gotno; }
802 // Return the number of GGA_RELOC_ONLY global GOT entries.
803 unsigned int
804 reloc_only_gotno() const
805 { return this->reloc_only_gotno_; }
807 // Return the number of TLS GOT entries.
808 unsigned int
809 tls_gotno() const
810 { return this->tls_gotno_; }
812 // Return the GOT type for this GOT. Used for multi-GOT links only.
813 unsigned int
814 multigot_got_type(unsigned int got_type) const
816 switch (got_type)
818 case GOT_TYPE_STANDARD:
819 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
820 case GOT_TYPE_TLS_OFFSET:
821 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
822 case GOT_TYPE_TLS_PAIR:
823 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
824 default:
825 gold_unreachable();
829 // Remove lazy-binding stubs for global symbols in this GOT.
830 void
831 remove_lazy_stubs(Target_mips<size, big_endian>* target);
833 // Return offset of this GOT from the start of .got section.
834 unsigned int
835 offset() const
836 { return this->offset_; }
838 // Set offset of this GOT from the start of .got section.
839 void
840 set_offset(unsigned int offset)
841 { this->offset_ = offset; }
843 // Set index of this GOT in multi-GOT links.
844 void
845 set_index(unsigned int index)
846 { this->index_ = index; }
848 // Return next GOT in multi-GOT links.
849 Mips_got_info<size, big_endian>*
850 next() const
851 { return this->next_; }
853 // Set next GOT in multi-GOT links.
854 void
855 set_next(Mips_got_info<size, big_endian>* next)
856 { this->next_ = next; }
858 // Return the offset of TLS LDM entry for this GOT.
859 unsigned int
860 tls_ldm_offset() const
861 { return this->tls_ldm_offset_; }
863 // Set the offset of TLS LDM entry for this GOT.
864 void
865 set_tls_ldm_offset(unsigned int tls_ldm_offset)
866 { this->tls_ldm_offset_ = tls_ldm_offset; }
868 Global_got_entry_set&
869 global_got_symbols()
870 { return this->global_got_symbols_; }
872 // Return the GOT_TLS_* type required by relocation type R_TYPE.
873 static int
874 mips_elf_reloc_tls_type(unsigned int r_type)
876 if (tls_gd_reloc(r_type))
877 return GOT_TLS_GD;
879 if (tls_ldm_reloc(r_type))
880 return GOT_TLS_LDM;
882 if (tls_gottprel_reloc(r_type))
883 return GOT_TLS_IE;
885 return GOT_TLS_NONE;
888 // Return the number of GOT slots needed for GOT TLS type TYPE.
889 static int
890 mips_tls_got_entries(unsigned int type)
892 switch (type)
894 case GOT_TLS_GD:
895 case GOT_TLS_LDM:
896 return 2;
898 case GOT_TLS_IE:
899 return 1;
901 case GOT_TLS_NONE:
902 return 0;
904 default:
905 gold_unreachable();
909 private:
910 // The number of local GOT entries.
911 unsigned int local_gotno_;
912 // The maximum number of page GOT entries needed.
913 unsigned int page_gotno_;
914 // The number of global GOT entries.
915 unsigned int global_gotno_;
916 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
917 unsigned int reloc_only_gotno_;
918 // The number of TLS GOT entries.
919 unsigned int tls_gotno_;
920 // The offset of TLS LDM entry for this GOT.
921 unsigned int tls_ldm_offset_;
922 // All symbols that have global GOT entry.
923 Global_got_entry_set global_got_symbols_;
924 // A hash table holding GOT entries.
925 Got_entry_set got_entries_;
926 // A hash table of GOT page entries.
927 Got_page_entry_set got_page_entries_;
928 // The offset of first GOT page entry for this GOT.
929 unsigned int got_page_offset_start_;
930 // The offset of next available GOT page entry for this GOT.
931 unsigned int got_page_offset_next_;
932 // A hash table that maps GOT page entry value to the GOT offset where
933 // the entry is located.
934 Got_page_offsets got_page_offsets_;
935 // In multi-GOT links, a pointer to the next GOT.
936 Mips_got_info<size, big_endian>* next_;
937 // Index of this GOT in multi-GOT links.
938 unsigned int index_;
939 // The offset of this GOT in multi-GOT links.
940 unsigned int offset_;
943 // This is a helper class used during relocation scan. It records GOT16 addend.
945 template<int size, bool big_endian>
946 struct got16_addend
948 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
950 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
951 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
952 Mips_address _addend)
953 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
954 addend(_addend)
957 const Sized_relobj_file<size, big_endian>* object;
958 unsigned int shndx;
959 unsigned int r_type;
960 unsigned int r_sym;
961 Mips_address addend;
964 // .MIPS.abiflags section content
966 template<bool big_endian>
967 struct Mips_abiflags
969 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
970 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
971 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
973 Mips_abiflags()
974 : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
975 cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
978 // Version of flags structure.
979 Valtype16 version;
980 // The level of the ISA: 1-5, 32, 64.
981 Valtype8 isa_level;
982 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
983 Valtype8 isa_rev;
984 // The size of general purpose registers.
985 Valtype8 gpr_size;
986 // The size of co-processor 1 registers.
987 Valtype8 cpr1_size;
988 // The size of co-processor 2 registers.
989 Valtype8 cpr2_size;
990 // The floating-point ABI.
991 Valtype8 fp_abi;
992 // Processor-specific extension.
993 Valtype32 isa_ext;
994 // Mask of ASEs used.
995 Valtype32 ases;
996 // Mask of general flags.
997 Valtype32 flags1;
998 Valtype32 flags2;
1001 // Mips_symbol class. Holds additional symbol information needed for Mips.
1003 template<int size>
1004 class Mips_symbol : public Sized_symbol<size>
1006 public:
1007 Mips_symbol()
1008 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1009 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1010 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1011 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1012 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1013 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1014 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1017 // Return whether this is a MIPS16 symbol.
1018 bool
1019 is_mips16() const
1021 // (st_other & STO_MIPS16) == STO_MIPS16
1022 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1023 == elfcpp::STO_MIPS16 >> 2);
1026 // Return whether this is a microMIPS symbol.
1027 bool
1028 is_micromips() const
1030 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1031 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1032 == elfcpp::STO_MICROMIPS >> 2);
1035 // Return whether the symbol needs MIPS16 fn_stub.
1036 bool
1037 need_fn_stub() const
1038 { return this->need_fn_stub_; }
1040 // Set that the symbol needs MIPS16 fn_stub.
1041 void
1042 set_need_fn_stub()
1043 { this->need_fn_stub_ = true; }
1045 // Return whether this symbol is referenced by branch relocations from
1046 // any non-PIC input file.
1047 bool
1048 has_nonpic_branches() const
1049 { return this->has_nonpic_branches_; }
1051 // Set that this symbol is referenced by branch relocations from
1052 // any non-PIC input file.
1053 void
1054 set_has_nonpic_branches()
1055 { this->has_nonpic_branches_ = true; }
1057 // Return the offset of the la25 stub for this symbol from the start of the
1058 // la25 stub section.
1059 unsigned int
1060 la25_stub_offset() const
1061 { return this->la25_stub_offset_; }
1063 // Set the offset of the la25 stub for this symbol from the start of the
1064 // la25 stub section.
1065 void
1066 set_la25_stub_offset(unsigned int offset)
1067 { this->la25_stub_offset_ = offset; }
1069 // Return whether the symbol has la25 stub. This is true if this symbol is
1070 // for a PIC function, and there are non-PIC branches and jumps to it.
1071 bool
1072 has_la25_stub() const
1073 { return this->la25_stub_offset_ != -1U; }
1075 // Return whether there is a relocation against this symbol that must be
1076 // resolved by the static linker (that is, the relocation cannot possibly
1077 // be made dynamic).
1078 bool
1079 has_static_relocs() const
1080 { return this->has_static_relocs_; }
1082 // Set that there is a relocation against this symbol that must be resolved
1083 // by the static linker (that is, the relocation cannot possibly be made
1084 // dynamic).
1085 void
1086 set_has_static_relocs()
1087 { this->has_static_relocs_ = true; }
1089 // Return whether we must not create a lazy-binding stub for this symbol.
1090 bool
1091 no_lazy_stub() const
1092 { return this->no_lazy_stub_; }
1094 // Set that we must not create a lazy-binding stub for this symbol.
1095 void
1096 set_no_lazy_stub()
1097 { this->no_lazy_stub_ = true; }
1099 // Return the offset of the lazy-binding stub for this symbol from the start
1100 // of .MIPS.stubs section.
1101 unsigned int
1102 lazy_stub_offset() const
1103 { return this->lazy_stub_offset_; }
1105 // Set the offset of the lazy-binding stub for this symbol from the start
1106 // of .MIPS.stubs section.
1107 void
1108 set_lazy_stub_offset(unsigned int offset)
1109 { this->lazy_stub_offset_ = offset; }
1111 // Return whether there are any relocations for this symbol where
1112 // pointer equality matters.
1113 bool
1114 pointer_equality_needed() const
1115 { return this->pointer_equality_needed_; }
1117 // Set that there are relocations for this symbol where pointer equality
1118 // matters.
1119 void
1120 set_pointer_equality_needed()
1121 { this->pointer_equality_needed_ = true; }
1123 // Return global GOT area where this symbol in located.
1124 Global_got_area
1125 global_got_area() const
1126 { return this->global_got_area_; }
1128 // Set global GOT area where this symbol in located.
1129 void
1130 set_global_got_area(Global_got_area global_got_area)
1131 { this->global_got_area_ = global_got_area; }
1133 // Return the global GOT offset for this symbol. For multi-GOT links, this
1134 // returns the offset from the start of .got section to the first GOT entry
1135 // for the symbol. Note that in multi-GOT links the symbol can have entry
1136 // in more than one GOT.
1137 unsigned int
1138 global_gotoffset() const
1139 { return this->global_gotoffset_; }
1141 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1142 // the symbol can have entry in more than one GOT. This method will set
1143 // the offset only if it is less than current offset.
1144 void
1145 set_global_gotoffset(unsigned int offset)
1147 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1148 this->global_gotoffset_ = offset;
1151 // Return whether all GOT relocations for this symbol are for calls.
1152 bool
1153 got_only_for_calls() const
1154 { return this->got_only_for_calls_; }
1156 // Set that there is a GOT relocation for this symbol that is not for call.
1157 void
1158 set_got_not_only_for_calls()
1159 { this->got_only_for_calls_ = false; }
1161 // Return whether this is a PIC symbol.
1162 bool
1163 is_pic() const
1165 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1166 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1167 == (elfcpp::STO_MIPS_PIC >> 2));
1170 // Set the flag in st_other field that marks this symbol as PIC.
1171 void
1172 set_pic()
1174 if (this->is_mips16())
1175 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1176 this->set_nonvis((this->nonvis()
1177 & ~((elfcpp::STO_MIPS16 >> 2)
1178 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1179 | (elfcpp::STO_MIPS_PIC >> 2));
1180 else
1181 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1182 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1183 | (elfcpp::STO_MIPS_PIC >> 2));
1186 // Set the flag in st_other field that marks this symbol as PLT.
1187 void
1188 set_mips_plt()
1190 if (this->is_mips16())
1191 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1192 this->set_nonvis((this->nonvis()
1193 & ((elfcpp::STO_MIPS16 >> 2)
1194 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1195 | (elfcpp::STO_MIPS_PLT >> 2));
1197 else
1198 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1199 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1200 | (elfcpp::STO_MIPS_PLT >> 2));
1203 // Downcast a base pointer to a Mips_symbol pointer.
1204 static Mips_symbol<size>*
1205 as_mips_sym(Symbol* sym)
1206 { return static_cast<Mips_symbol<size>*>(sym); }
1208 // Downcast a base pointer to a Mips_symbol pointer.
1209 static const Mips_symbol<size>*
1210 as_mips_sym(const Symbol* sym)
1211 { return static_cast<const Mips_symbol<size>*>(sym); }
1213 // Return whether the symbol has lazy-binding stub.
1214 bool
1215 has_lazy_stub() const
1216 { return this->has_lazy_stub_; }
1218 // Set whether the symbol has lazy-binding stub.
1219 void
1220 set_has_lazy_stub(bool has_lazy_stub)
1221 { this->has_lazy_stub_ = has_lazy_stub; }
1223 // Return whether the symbol needs a standard PLT entry.
1224 bool
1225 needs_mips_plt() const
1226 { return this->needs_mips_plt_; }
1228 // Set whether the symbol needs a standard PLT entry.
1229 void
1230 set_needs_mips_plt(bool needs_mips_plt)
1231 { this->needs_mips_plt_ = needs_mips_plt; }
1233 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1234 // entry.
1235 bool
1236 needs_comp_plt() const
1237 { return this->needs_comp_plt_; }
1239 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1240 void
1241 set_needs_comp_plt(bool needs_comp_plt)
1242 { this->needs_comp_plt_ = needs_comp_plt; }
1244 // Return standard PLT entry offset, or -1 if none.
1245 unsigned int
1246 mips_plt_offset() const
1247 { return this->mips_plt_offset_; }
1249 // Set standard PLT entry offset.
1250 void
1251 set_mips_plt_offset(unsigned int mips_plt_offset)
1252 { this->mips_plt_offset_ = mips_plt_offset; }
1254 // Return whether the symbol has standard PLT entry.
1255 bool
1256 has_mips_plt_offset() const
1257 { return this->mips_plt_offset_ != -1U; }
1259 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1260 unsigned int
1261 comp_plt_offset() const
1262 { return this->comp_plt_offset_; }
1264 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1265 void
1266 set_comp_plt_offset(unsigned int comp_plt_offset)
1267 { this->comp_plt_offset_ = comp_plt_offset; }
1269 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1270 bool
1271 has_comp_plt_offset() const
1272 { return this->comp_plt_offset_ != -1U; }
1274 // Return MIPS16 fn stub for a symbol.
1275 template<bool big_endian>
1276 Mips16_stub_section<size, big_endian>*
1277 get_mips16_fn_stub() const
1279 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1282 // Set MIPS16 fn stub for a symbol.
1283 void
1284 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1285 { this->mips16_fn_stub_ = stub; }
1287 // Return whether symbol has MIPS16 fn stub.
1288 bool
1289 has_mips16_fn_stub() const
1290 { return this->mips16_fn_stub_ != NULL; }
1292 // Return MIPS16 call stub for a symbol.
1293 template<bool big_endian>
1294 Mips16_stub_section<size, big_endian>*
1295 get_mips16_call_stub() const
1297 return static_cast<Mips16_stub_section<size, big_endian>*>(
1298 mips16_call_stub_);
1301 // Set MIPS16 call stub for a symbol.
1302 void
1303 set_mips16_call_stub(Mips16_stub_section_base* stub)
1304 { this->mips16_call_stub_ = stub; }
1306 // Return whether symbol has MIPS16 call stub.
1307 bool
1308 has_mips16_call_stub() const
1309 { return this->mips16_call_stub_ != NULL; }
1311 // Return MIPS16 call_fp stub for a symbol.
1312 template<bool big_endian>
1313 Mips16_stub_section<size, big_endian>*
1314 get_mips16_call_fp_stub() const
1316 return static_cast<Mips16_stub_section<size, big_endian>*>(
1317 mips16_call_fp_stub_);
1320 // Set MIPS16 call_fp stub for a symbol.
1321 void
1322 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1323 { this->mips16_call_fp_stub_ = stub; }
1325 // Return whether symbol has MIPS16 call_fp stub.
1326 bool
1327 has_mips16_call_fp_stub() const
1328 { return this->mips16_call_fp_stub_ != NULL; }
1330 bool
1331 get_applied_secondary_got_fixup() const
1332 { return applied_secondary_got_fixup_; }
1334 void
1335 set_applied_secondary_got_fixup()
1336 { this->applied_secondary_got_fixup_ = true; }
1338 // Return the hash of this symbol.
1339 size_t
1340 hash() const
1342 return gold::string_hash<char>(this->name());
1345 private:
1346 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1347 // appears in any relocs other than a 16 bit call.
1348 bool need_fn_stub_;
1350 // True if this symbol is referenced by branch relocations from
1351 // any non-PIC input file. This is used to determine whether an
1352 // la25 stub is required.
1353 bool has_nonpic_branches_;
1355 // The offset of the la25 stub for this symbol from the start of the
1356 // la25 stub section.
1357 unsigned int la25_stub_offset_;
1359 // True if there is a relocation against this symbol that must be
1360 // resolved by the static linker (that is, the relocation cannot
1361 // possibly be made dynamic).
1362 bool has_static_relocs_;
1364 // Whether we must not create a lazy-binding stub for this symbol.
1365 // This is true if the symbol has relocations related to taking the
1366 // function's address.
1367 bool no_lazy_stub_;
1369 // The offset of the lazy-binding stub for this symbol from the start of
1370 // .MIPS.stubs section.
1371 unsigned int lazy_stub_offset_;
1373 // True if there are any relocations for this symbol where pointer equality
1374 // matters.
1375 bool pointer_equality_needed_;
1377 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1378 // in the global part of the GOT.
1379 Global_got_area global_got_area_;
1381 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1382 // from the start of .got section to the first GOT entry for the symbol.
1383 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1384 unsigned int global_gotoffset_;
1386 // Whether all GOT relocations for this symbol are for calls.
1387 bool got_only_for_calls_;
1388 // Whether the symbol has lazy-binding stub.
1389 bool has_lazy_stub_;
1390 // Whether the symbol needs a standard PLT entry.
1391 bool needs_mips_plt_;
1392 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1393 bool needs_comp_plt_;
1394 // Standard PLT entry offset, or -1 if none.
1395 unsigned int mips_plt_offset_;
1396 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1397 unsigned int comp_plt_offset_;
1398 // MIPS16 fn stub for a symbol.
1399 Mips16_stub_section_base* mips16_fn_stub_;
1400 // MIPS16 call stub for a symbol.
1401 Mips16_stub_section_base* mips16_call_stub_;
1402 // MIPS16 call_fp stub for a symbol.
1403 Mips16_stub_section_base* mips16_call_fp_stub_;
1405 bool applied_secondary_got_fixup_;
1408 // Mips16_stub_section class.
1410 // The mips16 compiler uses a couple of special sections to handle
1411 // floating point arguments.
1413 // Section names that look like .mips16.fn.FNNAME contain stubs that
1414 // copy floating point arguments from the fp regs to the gp regs and
1415 // then jump to FNNAME. If any 32 bit function calls FNNAME, the
1416 // call should be redirected to the stub instead. If no 32 bit
1417 // function calls FNNAME, the stub should be discarded. We need to
1418 // consider any reference to the function, not just a call, because
1419 // if the address of the function is taken we will need the stub,
1420 // since the address might be passed to a 32 bit function.
1422 // Section names that look like .mips16.call.FNNAME contain stubs
1423 // that copy floating point arguments from the gp regs to the fp
1424 // regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1425 // then any 16 bit function that calls FNNAME should be redirected
1426 // to the stub instead. If FNNAME is not a 32 bit function, the
1427 // stub should be discarded.
1429 // .mips16.call.fp.FNNAME sections are similar, but contain stubs
1430 // which call FNNAME and then copy the return value from the fp regs
1431 // to the gp regs. These stubs store the return address in $18 while
1432 // calling FNNAME; any function which might call one of these stubs
1433 // must arrange to save $18 around the call. (This case is not
1434 // needed for 32 bit functions that call 16 bit functions, because
1435 // 16 bit functions always return floating point values in both
1436 // $f0/$f1 and $2/$3.)
1438 // Note that in all cases FNNAME might be defined statically.
1439 // Therefore, FNNAME is not used literally. Instead, the relocation
1440 // information will indicate which symbol the section is for.
1442 // We record any stubs that we find in the symbol table.
1444 // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1446 class Mips16_stub_section_base { };
1448 template<int size, bool big_endian>
1449 class Mips16_stub_section : public Mips16_stub_section_base
1451 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1453 public:
1454 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1455 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1456 found_r_mips_none_(false)
1458 gold_assert(object->is_mips16_fn_stub_section(shndx)
1459 || object->is_mips16_call_stub_section(shndx)
1460 || object->is_mips16_call_fp_stub_section(shndx));
1463 // Return the object of this stub section.
1464 Mips_relobj<size, big_endian>*
1465 object() const
1466 { return this->object_; }
1468 // Return the size of a section.
1469 uint64_t
1470 section_size() const
1471 { return this->object_->section_size(this->shndx_); }
1473 // Return section index of this stub section.
1474 unsigned int
1475 shndx() const
1476 { return this->shndx_; }
1478 // Return symbol index, if stub is for a local function.
1479 unsigned int
1480 r_sym() const
1481 { return this->r_sym_; }
1483 // Return symbol, if stub is for a global function.
1484 Mips_symbol<size>*
1485 gsym() const
1486 { return this->gsym_; }
1488 // Return whether stub is for a local function.
1489 bool
1490 is_for_local_function() const
1491 { return this->gsym_ == NULL; }
1493 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1494 // is found in the stub section. Try to find stub target.
1495 void
1496 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1498 // To find target symbol for this stub, trust the first R_MIPS_NONE
1499 // relocation, if any. Otherwise trust the first relocation, whatever
1500 // its kind.
1501 if (this->found_r_mips_none_)
1502 return;
1503 if (r_type == elfcpp::R_MIPS_NONE)
1505 this->r_sym_ = r_sym;
1506 this->gsym_ = NULL;
1507 this->found_r_mips_none_ = true;
1509 else if (!is_target_found())
1510 this->r_sym_ = r_sym;
1513 // This method is called when a new relocation R_TYPE for global symbol GSYM
1514 // is found in the stub section. Try to find stub target.
1515 void
1516 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1518 // To find target symbol for this stub, trust the first R_MIPS_NONE
1519 // relocation, if any. Otherwise trust the first relocation, whatever
1520 // its kind.
1521 if (this->found_r_mips_none_)
1522 return;
1523 if (r_type == elfcpp::R_MIPS_NONE)
1525 this->gsym_ = gsym;
1526 this->r_sym_ = 0;
1527 this->found_r_mips_none_ = true;
1529 else if (!is_target_found())
1530 this->gsym_ = gsym;
1533 // Return whether we found the stub target.
1534 bool
1535 is_target_found() const
1536 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1538 // Return whether this is a fn stub.
1539 bool
1540 is_fn_stub() const
1541 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1543 // Return whether this is a call stub.
1544 bool
1545 is_call_stub() const
1546 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1548 // Return whether this is a call_fp stub.
1549 bool
1550 is_call_fp_stub() const
1551 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1553 // Return the output address.
1554 Mips_address
1555 output_address() const
1557 return (this->object_->output_section(this->shndx_)->address()
1558 + this->object_->output_section_offset(this->shndx_));
1561 private:
1562 // The object of this stub section.
1563 Mips_relobj<size, big_endian>* object_;
1564 // The section index of this stub section.
1565 unsigned int shndx_;
1566 // The symbol index, if stub is for a local function.
1567 unsigned int r_sym_;
1568 // The symbol, if stub is for a global function.
1569 Mips_symbol<size>* gsym_;
1570 // True if we found R_MIPS_NONE relocation in this stub.
1571 bool found_r_mips_none_;
1574 // Mips_relobj class.
1576 template<int size, bool big_endian>
1577 class Mips_relobj : public Sized_relobj_file<size, big_endian>
1579 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1580 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1581 Mips16_stubs_int_map;
1582 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1584 public:
1585 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1586 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1587 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1588 processor_specific_flags_(0), local_symbol_is_mips16_(),
1589 local_symbol_is_micromips_(), mips16_stub_sections_(),
1590 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1591 local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
1592 got_info_(NULL), section_is_mips16_fn_stub_(),
1593 section_is_mips16_call_stub_(), section_is_mips16_call_fp_stub_(),
1594 pdr_shndx_(-1U), attributes_section_data_(NULL), abiflags_(NULL),
1595 gprmask_(0), cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1597 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1598 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1601 ~Mips_relobj()
1602 { delete this->attributes_section_data_; }
1604 // Downcast a base pointer to a Mips_relobj pointer. This is
1605 // not type-safe but we only use Mips_relobj not the base class.
1606 static Mips_relobj<size, big_endian>*
1607 as_mips_relobj(Relobj* relobj)
1608 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1610 // Downcast a base pointer to a Mips_relobj pointer. This is
1611 // not type-safe but we only use Mips_relobj not the base class.
1612 static const Mips_relobj<size, big_endian>*
1613 as_mips_relobj(const Relobj* relobj)
1614 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1616 // Processor-specific flags in ELF file header. This is valid only after
1617 // reading symbols.
1618 elfcpp::Elf_Word
1619 processor_specific_flags() const
1620 { return this->processor_specific_flags_; }
1622 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1623 // index. This is only valid after do_count_local_symbol is called.
1624 bool
1625 local_symbol_is_mips16(unsigned int r_sym) const
1627 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1628 return this->local_symbol_is_mips16_[r_sym];
1631 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1632 // index. This is only valid after do_count_local_symbol is called.
1633 bool
1634 local_symbol_is_micromips(unsigned int r_sym) const
1636 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1637 return this->local_symbol_is_micromips_[r_sym];
1640 // Get or create MIPS16 stub section.
1641 Mips16_stub_section<size, big_endian>*
1642 get_mips16_stub_section(unsigned int shndx)
1644 typename Mips16_stubs_int_map::const_iterator it =
1645 this->mips16_stub_sections_.find(shndx);
1646 if (it != this->mips16_stub_sections_.end())
1647 return (*it).second;
1649 Mips16_stub_section<size, big_endian>* stub_section =
1650 new Mips16_stub_section<size, big_endian>(this, shndx);
1651 this->mips16_stub_sections_.insert(
1652 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1653 stub_section->shndx(), stub_section));
1654 return stub_section;
1657 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1658 // object doesn't have fn stub for R_SYM.
1659 Mips16_stub_section<size, big_endian>*
1660 get_local_mips16_fn_stub(unsigned int r_sym) const
1662 typename Mips16_stubs_int_map::const_iterator it =
1663 this->local_mips16_fn_stubs_.find(r_sym);
1664 if (it != this->local_mips16_fn_stubs_.end())
1665 return (*it).second;
1666 return NULL;
1669 // Record that this object has MIPS16 fn stub for local symbol. This method
1670 // is only called if we decided not to discard the stub.
1671 void
1672 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1674 gold_assert(stub->is_for_local_function());
1675 unsigned int r_sym = stub->r_sym();
1676 this->local_mips16_fn_stubs_.insert(
1677 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1678 r_sym, stub));
1681 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1682 // object doesn't have call stub for R_SYM.
1683 Mips16_stub_section<size, big_endian>*
1684 get_local_mips16_call_stub(unsigned int r_sym) const
1686 typename Mips16_stubs_int_map::const_iterator it =
1687 this->local_mips16_call_stubs_.find(r_sym);
1688 if (it != this->local_mips16_call_stubs_.end())
1689 return (*it).second;
1690 return NULL;
1693 // Record that this object has MIPS16 call stub for local symbol. This method
1694 // is only called if we decided not to discard the stub.
1695 void
1696 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1698 gold_assert(stub->is_for_local_function());
1699 unsigned int r_sym = stub->r_sym();
1700 this->local_mips16_call_stubs_.insert(
1701 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1702 r_sym, stub));
1705 // Record that we found "non 16-bit" call relocation against local symbol
1706 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1707 // is one.
1708 void
1709 add_local_non_16bit_call(unsigned int symndx)
1710 { this->local_non_16bit_calls_.insert(symndx); }
1712 // Return true if there is any "non 16-bit" call relocation against local
1713 // symbol SYMNDX in this object.
1714 bool
1715 has_local_non_16bit_call_relocs(unsigned int symndx)
1717 return (this->local_non_16bit_calls_.find(symndx)
1718 != this->local_non_16bit_calls_.end());
1721 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1722 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1723 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1724 void
1725 add_local_16bit_call(unsigned int symndx)
1726 { this->local_16bit_calls_.insert(symndx); }
1728 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1729 // symbol SYMNDX in this object.
1730 bool
1731 has_local_16bit_call_relocs(unsigned int symndx)
1733 return (this->local_16bit_calls_.find(symndx)
1734 != this->local_16bit_calls_.end());
1737 // Get gp value that was used to create this object.
1738 Mips_address
1739 gp_value() const
1740 { return this->gp_; }
1742 // Return whether the object is a PIC object.
1743 bool
1744 is_pic() const
1745 { return this->is_pic_; }
1747 // Return whether the object uses N32 ABI.
1748 bool
1749 is_n32() const
1750 { return this->is_n32_; }
1752 // Return whether the object uses N64 ABI.
1753 bool
1754 is_n64() const
1755 { return size == 64; }
1757 // Return whether the object uses NewABI conventions.
1758 bool
1759 is_newabi() const
1760 { return this->is_n32() || this->is_n64(); }
1762 // Return Mips_got_info for this object.
1763 Mips_got_info<size, big_endian>*
1764 get_got_info() const
1765 { return this->got_info_; }
1767 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1768 Mips_got_info<size, big_endian>*
1769 get_or_create_got_info()
1771 if (!this->got_info_)
1772 this->got_info_ = new Mips_got_info<size, big_endian>();
1773 return this->got_info_;
1776 // Set Mips_got_info for this object.
1777 void
1778 set_got_info(Mips_got_info<size, big_endian>* got_info)
1779 { this->got_info_ = got_info; }
1781 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1782 // after do_read_symbols is called.
1783 bool
1784 is_mips16_stub_section(unsigned int shndx)
1786 return (is_mips16_fn_stub_section(shndx)
1787 || is_mips16_call_stub_section(shndx)
1788 || is_mips16_call_fp_stub_section(shndx));
1791 // Return TRUE if relocations in section SHNDX can refer directly to a
1792 // MIPS16 function rather than to a hard-float stub. This is only valid
1793 // after do_read_symbols is called.
1794 bool
1795 section_allows_mips16_refs(unsigned int shndx)
1797 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1800 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1801 // after do_read_symbols is called.
1802 bool
1803 is_mips16_fn_stub_section(unsigned int shndx)
1805 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1806 return this->section_is_mips16_fn_stub_[shndx];
1809 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1810 // after do_read_symbols is called.
1811 bool
1812 is_mips16_call_stub_section(unsigned int shndx)
1814 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1815 return this->section_is_mips16_call_stub_[shndx];
1818 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1819 // valid after do_read_symbols is called.
1820 bool
1821 is_mips16_call_fp_stub_section(unsigned int shndx)
1823 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1824 return this->section_is_mips16_call_fp_stub_[shndx];
1827 // Discard MIPS16 stub secions that are not needed.
1828 void
1829 discard_mips16_stub_sections(Symbol_table* symtab);
1831 // Return whether there is a .reginfo section.
1832 bool
1833 has_reginfo_section() const
1834 { return this->has_reginfo_section_; }
1836 // Return gprmask from the .reginfo section of this object.
1837 Valtype
1838 gprmask() const
1839 { return this->gprmask_; }
1841 // Return cprmask1 from the .reginfo section of this object.
1842 Valtype
1843 cprmask1() const
1844 { return this->cprmask1_; }
1846 // Return cprmask2 from the .reginfo section of this object.
1847 Valtype
1848 cprmask2() const
1849 { return this->cprmask2_; }
1851 // Return cprmask3 from the .reginfo section of this object.
1852 Valtype
1853 cprmask3() const
1854 { return this->cprmask3_; }
1856 // Return cprmask4 from the .reginfo section of this object.
1857 Valtype
1858 cprmask4() const
1859 { return this->cprmask4_; }
1861 // This is the contents of the .MIPS.abiflags section if there is one.
1862 Mips_abiflags<big_endian>*
1863 abiflags()
1864 { return this->abiflags_; }
1866 // This is the contents of the .gnu.attribute section if there is one.
1867 const Attributes_section_data*
1868 attributes_section_data() const
1869 { return this->attributes_section_data_; }
1871 protected:
1872 // Count the local symbols.
1873 void
1874 do_count_local_symbols(Stringpool_template<char>*,
1875 Stringpool_template<char>*);
1877 // Read the symbol information.
1878 void
1879 do_read_symbols(Read_symbols_data* sd);
1881 private:
1882 // The name of the options section.
1883 const char* mips_elf_options_section_name()
1884 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1886 // processor-specific flags in ELF file header.
1887 elfcpp::Elf_Word processor_specific_flags_;
1889 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1890 // This is only valid after do_count_local_symbol is called.
1891 std::vector<bool> local_symbol_is_mips16_;
1893 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1894 // This is only valid after do_count_local_symbol is called.
1895 std::vector<bool> local_symbol_is_micromips_;
1897 // Map from section index to the MIPS16 stub for that section. This contains
1898 // all stubs found in this object.
1899 Mips16_stubs_int_map mips16_stub_sections_;
1901 // Local symbols that have "non 16-bit" call relocation. This relocation
1902 // would need to refer to a MIPS16 fn stub, if there is one.
1903 std::set<unsigned int> local_non_16bit_calls_;
1905 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1906 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1907 // relocation that refers to the stub symbol.
1908 std::set<unsigned int> local_16bit_calls_;
1910 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1911 // This contains only the stubs that we decided not to discard.
1912 Mips16_stubs_int_map local_mips16_fn_stubs_;
1914 // Map from local symbol index to the MIPS16 call stub for that symbol.
1915 // This contains only the stubs that we decided not to discard.
1916 Mips16_stubs_int_map local_mips16_call_stubs_;
1918 // gp value that was used to create this object.
1919 Mips_address gp_;
1920 // Whether the object is a PIC object.
1921 bool is_pic_ : 1;
1922 // Whether the object uses N32 ABI.
1923 bool is_n32_ : 1;
1924 // Whether the object contains a .reginfo section.
1925 bool has_reginfo_section_ : 1;
1926 // The Mips_got_info for this object.
1927 Mips_got_info<size, big_endian>* got_info_;
1929 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1930 // This is only valid after do_read_symbols is called.
1931 std::vector<bool> section_is_mips16_fn_stub_;
1933 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1934 // This is only valid after do_read_symbols is called.
1935 std::vector<bool> section_is_mips16_call_stub_;
1937 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1938 // This is only valid after do_read_symbols is called.
1939 std::vector<bool> section_is_mips16_call_fp_stub_;
1941 // .pdr section index.
1942 unsigned int pdr_shndx_;
1944 // Object attributes if there is a .gnu.attributes section or NULL.
1945 Attributes_section_data* attributes_section_data_;
1947 // Object abiflags if there is a .MIPS.abiflags section or NULL.
1948 Mips_abiflags<big_endian>* abiflags_;
1950 // gprmask from the .reginfo section of this object.
1951 Valtype gprmask_;
1952 // cprmask1 from the .reginfo section of this object.
1953 Valtype cprmask1_;
1954 // cprmask2 from the .reginfo section of this object.
1955 Valtype cprmask2_;
1956 // cprmask3 from the .reginfo section of this object.
1957 Valtype cprmask3_;
1958 // cprmask4 from the .reginfo section of this object.
1959 Valtype cprmask4_;
1962 // Mips_output_data_got class.
1964 template<int size, bool big_endian>
1965 class Mips_output_data_got : public Output_data_got<size, big_endian>
1967 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1968 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1969 Reloc_section;
1970 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1972 public:
1973 Mips_output_data_got(Target_mips<size, big_endian>* target,
1974 Symbol_table* symtab, Layout* layout)
1975 : Output_data_got<size, big_endian>(), target_(target),
1976 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1977 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1978 secondary_got_relocs_()
1980 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1981 this->set_addralign(16);
1984 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1985 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1986 void
1987 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1988 unsigned int symndx, Mips_address addend,
1989 unsigned int r_type, unsigned int shndx,
1990 bool is_section_symbol)
1992 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
1993 r_type, shndx,
1994 is_section_symbol);
1997 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
1998 // in OBJECT. FOR_CALL is true if the caller is only interested in
1999 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
2000 // relocation.
2001 void
2002 record_global_got_symbol(Mips_symbol<size>* mips_sym,
2003 Mips_relobj<size, big_endian>* object,
2004 unsigned int r_type, bool dyn_reloc, bool for_call)
2006 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2007 dyn_reloc, for_call);
2010 // Record that OBJECT has a page relocation against symbol SYMNDX and
2011 // that ADDEND is the addend for that relocation.
2012 void
2013 record_got_page_entry(Mips_relobj<size, big_endian>* object,
2014 unsigned int symndx, int addend)
2015 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2017 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
2018 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2019 // applied in a static link.
2020 void
2021 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2022 Mips_symbol<size>* gsym)
2023 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2025 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
2026 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
2027 // relocation that needs to be applied in a static link.
2028 void
2029 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2030 Sized_relobj_file<size, big_endian>* relobj,
2031 unsigned int index)
2033 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2034 index));
2037 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2038 // secondary GOT at OFFSET.
2039 void
2040 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2041 Mips_symbol<size>* gsym)
2043 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2044 r_type, gsym));
2047 // Update GOT entry at OFFSET with VALUE.
2048 void
2049 update_got_entry(unsigned int offset, Mips_address value)
2051 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2054 // Return the number of entries in local part of the GOT. This includes
2055 // local entries, page entries and 2 reserved entries.
2056 unsigned int
2057 get_local_gotno() const
2059 if (!this->multi_got())
2061 return (2 + this->master_got_info_->local_gotno()
2062 + this->master_got_info_->page_gotno());
2064 else
2065 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2068 // Return dynamic symbol table index of the first symbol with global GOT
2069 // entry.
2070 unsigned int
2071 first_global_got_dynsym_index() const
2072 { return this->first_global_got_dynsym_index_; }
2074 // Set dynamic symbol table index of the first symbol with global GOT entry.
2075 void
2076 set_first_global_got_dynsym_index(unsigned int index)
2077 { this->first_global_got_dynsym_index_ = index; }
2079 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2080 // larger than 64K, create multi-GOT.
2081 void
2082 lay_out_got(Layout* layout, Symbol_table* symtab,
2083 const Input_objects* input_objects);
2085 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2086 void
2087 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2089 // Attempt to merge GOTs of different input objects.
2090 void
2091 merge_gots(const Input_objects* input_objects);
2093 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
2094 // this would lead to overflow, true if they were merged successfully.
2095 bool
2096 merge_got_with(Mips_got_info<size, big_endian>* from,
2097 Mips_relobj<size, big_endian>* object,
2098 Mips_got_info<size, big_endian>* to);
2100 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2101 // use OBJECT's GOT.
2102 unsigned int
2103 get_got_page_offset(Mips_address value,
2104 const Mips_relobj<size, big_endian>* object)
2106 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2107 ? this->master_got_info_
2108 : object->get_got_info());
2109 gold_assert(g != NULL);
2110 return g->get_got_page_offset(value, this);
2113 // Return the GOT offset of type GOT_TYPE of the global symbol
2114 // GSYM. For multi-GOT links, use OBJECT's GOT.
2115 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2116 Mips_relobj<size, big_endian>* object) const
2118 if (!this->multi_got())
2119 return gsym->got_offset(got_type);
2120 else
2122 Mips_got_info<size, big_endian>* g = object->get_got_info();
2123 gold_assert(g != NULL);
2124 return gsym->got_offset(g->multigot_got_type(got_type));
2128 // Return the GOT offset of type GOT_TYPE of the local symbol
2129 // SYMNDX.
2130 unsigned int
2131 got_offset(unsigned int symndx, unsigned int got_type,
2132 Sized_relobj_file<size, big_endian>* object,
2133 uint64_t addend) const
2134 { return object->local_got_offset(symndx, got_type, addend); }
2136 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2137 unsigned int
2138 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2140 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2141 ? this->master_got_info_
2142 : object->get_got_info());
2143 gold_assert(g != NULL);
2144 return g->tls_ldm_offset();
2147 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2148 void
2149 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2150 Mips_relobj<size, big_endian>* object)
2152 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2153 ? this->master_got_info_
2154 : object->get_got_info());
2155 gold_assert(g != NULL);
2156 g->set_tls_ldm_offset(tls_ldm_offset);
2159 // Return true for multi-GOT links.
2160 bool
2161 multi_got() const
2162 { return this->primary_got_ != NULL; }
2164 // Return the offset of OBJECT's GOT from the start of .got section.
2165 unsigned int
2166 get_got_offset(const Mips_relobj<size, big_endian>* object)
2168 if (!this->multi_got())
2169 return 0;
2170 else
2172 Mips_got_info<size, big_endian>* g = object->get_got_info();
2173 return g != NULL ? g->offset() : 0;
2177 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2178 void
2179 add_reloc_only_entries()
2180 { this->master_got_info_->add_reloc_only_entries(this); }
2182 // Return offset of the primary GOT's entry for global symbol.
2183 unsigned int
2184 get_primary_got_offset(const Mips_symbol<size>* sym) const
2186 gold_assert(sym->global_got_area() != GGA_NONE);
2187 return (this->get_local_gotno() + sym->dynsym_index()
2188 - this->first_global_got_dynsym_index()) * size/8;
2191 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2192 // Input argument GOT_OFFSET is always global offset from the start of
2193 // .got section, for both single and multi-GOT links.
2194 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2195 // links, the return value is object_got_offset - 0x7FF0, where
2196 // object_got_offset is offset in the OBJECT's GOT.
2198 gp_offset(unsigned int got_offset,
2199 const Mips_relobj<size, big_endian>* object) const
2201 return (this->address() + got_offset
2202 - this->target_->adjusted_gp_value(object));
2205 protected:
2206 // Write out the GOT table.
2207 void
2208 do_write(Output_file*);
2210 private:
2212 // This class represent dynamic relocations that need to be applied by
2213 // gold because we are using TLS relocations in a static link.
2214 class Static_reloc
2216 public:
2217 Static_reloc(unsigned int got_offset, unsigned int r_type,
2218 Mips_symbol<size>* gsym)
2219 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2220 { this->u_.global.symbol = gsym; }
2222 Static_reloc(unsigned int got_offset, unsigned int r_type,
2223 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2224 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2226 this->u_.local.relobj = relobj;
2227 this->u_.local.index = index;
2230 // Return the GOT offset.
2231 unsigned int
2232 got_offset() const
2233 { return this->got_offset_; }
2235 // Relocation type.
2236 unsigned int
2237 r_type() const
2238 { return this->r_type_; }
2240 // Whether the symbol is global or not.
2241 bool
2242 symbol_is_global() const
2243 { return this->symbol_is_global_; }
2245 // For a relocation against a global symbol, the global symbol.
2246 Mips_symbol<size>*
2247 symbol() const
2249 gold_assert(this->symbol_is_global_);
2250 return this->u_.global.symbol;
2253 // For a relocation against a local symbol, the defining object.
2254 Sized_relobj_file<size, big_endian>*
2255 relobj() const
2257 gold_assert(!this->symbol_is_global_);
2258 return this->u_.local.relobj;
2261 // For a relocation against a local symbol, the local symbol index.
2262 unsigned int
2263 index() const
2265 gold_assert(!this->symbol_is_global_);
2266 return this->u_.local.index;
2269 private:
2270 // GOT offset of the entry to which this relocation is applied.
2271 unsigned int got_offset_;
2272 // Type of relocation.
2273 unsigned int r_type_;
2274 // Whether this relocation is against a global symbol.
2275 bool symbol_is_global_;
2276 // A global or local symbol.
2277 union
2279 struct
2281 // For a global symbol, the symbol itself.
2282 Mips_symbol<size>* symbol;
2283 } global;
2284 struct
2286 // For a local symbol, the object defining object.
2287 Sized_relobj_file<size, big_endian>* relobj;
2288 // For a local symbol, the symbol index.
2289 unsigned int index;
2290 } local;
2291 } u_;
2294 // The target.
2295 Target_mips<size, big_endian>* target_;
2296 // The symbol table.
2297 Symbol_table* symbol_table_;
2298 // The layout.
2299 Layout* layout_;
2300 // Static relocs to be applied to the GOT.
2301 std::vector<Static_reloc> static_relocs_;
2302 // .got section view.
2303 unsigned char* got_view_;
2304 // The dynamic symbol table index of the first symbol with global GOT entry.
2305 unsigned int first_global_got_dynsym_index_;
2306 // The master GOT information.
2307 Mips_got_info<size, big_endian>* master_got_info_;
2308 // The primary GOT information.
2309 Mips_got_info<size, big_endian>* primary_got_;
2310 // Secondary GOT fixups.
2311 std::vector<Static_reloc> secondary_got_relocs_;
2314 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2315 // two ways of creating these interfaces. The first is to add:
2317 // lui $25,%hi(func)
2318 // j func
2319 // addiu $25,$25,%lo(func)
2321 // to a separate trampoline section. The second is to add:
2323 // lui $25,%hi(func)
2324 // addiu $25,$25,%lo(func)
2326 // immediately before a PIC function "func", but only if a function is at the
2327 // beginning of the section, and the section is not too heavily aligned (i.e we
2328 // would need to add no more than 2 nops before the stub.)
2330 // We only create stubs of the first type.
2332 template<int size, bool big_endian>
2333 class Mips_output_data_la25_stub : public Output_section_data
2335 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2337 public:
2338 Mips_output_data_la25_stub()
2339 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2342 // Create LA25 stub for a symbol.
2343 void
2344 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2345 Mips_symbol<size>* gsym);
2347 // Return output address of a stub.
2348 Mips_address
2349 stub_address(const Mips_symbol<size>* sym) const
2351 gold_assert(sym->has_la25_stub());
2352 return this->address() + sym->la25_stub_offset();
2355 protected:
2356 void
2357 do_adjust_output_section(Output_section* os)
2358 { os->set_entsize(0); }
2360 private:
2361 // Template for standard LA25 stub.
2362 static const uint32_t la25_stub_entry[];
2363 // Template for microMIPS LA25 stub.
2364 static const uint32_t la25_stub_micromips_entry[];
2366 // Set the final size.
2367 void
2368 set_final_data_size()
2369 { this->set_data_size(this->symbols_.size() * 16); }
2371 // Create a symbol for SYM stub's value and size, to help make the
2372 // disassembly easier to read.
2373 void
2374 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2375 Target_mips<size, big_endian>* target, uint64_t symsize);
2377 // Write to a map file.
2378 void
2379 do_print_to_mapfile(Mapfile* mapfile) const
2380 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2382 // Write out the LA25 stub section.
2383 void
2384 do_write(Output_file*);
2386 // Symbols that have LA25 stubs.
2387 std::vector<Mips_symbol<size>*> symbols_;
2390 // MIPS-specific relocation writer.
2392 template<int sh_type, bool dynamic, int size, bool big_endian>
2393 struct Mips_output_reloc_writer;
2395 template<int sh_type, bool dynamic, bool big_endian>
2396 struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2398 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2399 typedef std::vector<Output_reloc_type> Relocs;
2401 static void
2402 write(typename Relocs::const_iterator p, unsigned char* pov)
2403 { p->write(pov); }
2406 template<int sh_type, bool dynamic, bool big_endian>
2407 struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2409 typedef Output_reloc<sh_type, dynamic, 64, 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)
2415 elfcpp::Mips64_rel_write<big_endian> orel(pov);
2416 orel.put_r_offset(p->get_address());
2417 orel.put_r_sym(p->get_symbol_index());
2418 orel.put_r_ssym(RSS_UNDEF);
2419 orel.put_r_type(p->type());
2420 if (p->type() == elfcpp::R_MIPS_REL32)
2421 orel.put_r_type2(elfcpp::R_MIPS_64);
2422 else
2423 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2424 orel.put_r_type3(elfcpp::R_MIPS_NONE);
2428 template<int sh_type, bool dynamic, int size, bool big_endian>
2429 class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2430 size, big_endian>
2432 public:
2433 Mips_output_data_reloc(bool sort_relocs)
2434 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2437 protected:
2438 // Write out the data.
2439 void
2440 do_write(Output_file* of)
2442 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2443 big_endian> Writer;
2444 this->template do_write_generic<Writer>(of);
2449 // A class to handle the PLT data.
2451 template<int size, bool big_endian>
2452 class Mips_output_data_plt : public Output_section_data
2454 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2455 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2456 size, big_endian> Reloc_section;
2458 public:
2459 // Create the PLT section. The ordinary .got section is an argument,
2460 // since we need to refer to the start.
2461 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2462 Target_mips<size, big_endian>* target)
2463 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2464 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2465 target_(target)
2467 this->rel_ = new Reloc_section(false);
2468 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2469 elfcpp::SHF_ALLOC, this->rel_,
2470 ORDER_DYNAMIC_PLT_RELOCS, false);
2473 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2474 void
2475 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2477 // Return the .rel.plt section data.
2478 const Reloc_section*
2479 rel_plt() const
2480 { return this->rel_; }
2482 // Return the number of PLT entries.
2483 unsigned int
2484 entry_count() const
2485 { return this->symbols_.size(); }
2487 // Return the offset of the first non-reserved PLT entry.
2488 unsigned int
2489 first_plt_entry_offset() const
2490 { return sizeof(plt0_entry_o32); }
2492 // Return the size of a PLT entry.
2493 unsigned int
2494 plt_entry_size() const
2495 { return sizeof(plt_entry); }
2497 // Set final PLT offsets. For each symbol, determine whether standard or
2498 // compressed (MIPS16 or microMIPS) PLT entry is used.
2499 void
2500 set_plt_offsets();
2502 // Return the offset of the first standard PLT entry.
2503 unsigned int
2504 first_mips_plt_offset() const
2505 { return this->plt_header_size_; }
2507 // Return the offset of the first compressed PLT entry.
2508 unsigned int
2509 first_comp_plt_offset() const
2510 { return this->plt_header_size_ + this->plt_mips_offset_; }
2512 // Return whether there are any standard PLT entries.
2513 bool
2514 has_standard_entries() const
2515 { return this->plt_mips_offset_ > 0; }
2517 // Return the output address of standard PLT entry.
2518 Mips_address
2519 mips_entry_address(const Mips_symbol<size>* sym) const
2521 gold_assert (sym->has_mips_plt_offset());
2522 return (this->address() + this->first_mips_plt_offset()
2523 + sym->mips_plt_offset());
2526 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2527 Mips_address
2528 comp_entry_address(const Mips_symbol<size>* sym) const
2530 gold_assert (sym->has_comp_plt_offset());
2531 return (this->address() + this->first_comp_plt_offset()
2532 + sym->comp_plt_offset());
2535 protected:
2536 void
2537 do_adjust_output_section(Output_section* os)
2538 { os->set_entsize(0); }
2540 // Write to a map file.
2541 void
2542 do_print_to_mapfile(Mapfile* mapfile) const
2543 { mapfile->print_output_data(this, _(".plt")); }
2545 private:
2546 // Template for the first PLT entry.
2547 static const uint32_t plt0_entry_o32[];
2548 static const uint32_t plt0_entry_n32[];
2549 static const uint32_t plt0_entry_n64[];
2550 static const uint32_t plt0_entry_micromips_o32[];
2551 static const uint32_t plt0_entry_micromips32_o32[];
2553 // Template for subsequent PLT entries.
2554 static const uint32_t plt_entry[];
2555 static const uint32_t plt_entry_r6[];
2556 static const uint32_t plt_entry_mips16_o32[];
2557 static const uint32_t plt_entry_micromips_o32[];
2558 static const uint32_t plt_entry_micromips32_o32[];
2560 // Set the final size.
2561 void
2562 set_final_data_size()
2564 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2565 + this->plt_comp_offset_);
2568 // Write out the PLT data.
2569 void
2570 do_write(Output_file*);
2572 // Return whether the plt header contains microMIPS code. For the sake of
2573 // cache alignment always use a standard header whenever any standard entries
2574 // are present even if microMIPS entries are present as well. This also lets
2575 // the microMIPS header rely on the value of $v0 only set by microMIPS
2576 // entries, for a small size reduction.
2577 bool
2578 is_plt_header_compressed() const
2580 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2581 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2584 // Return the size of the PLT header.
2585 unsigned int
2586 get_plt_header_size() const
2588 if (this->target_->is_output_n64())
2589 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2590 else if (this->target_->is_output_n32())
2591 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2592 else if (!this->is_plt_header_compressed())
2593 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2594 else if (this->target_->use_32bit_micromips_instructions())
2595 return (2 * sizeof(plt0_entry_micromips32_o32)
2596 / sizeof(plt0_entry_micromips32_o32[0]));
2597 else
2598 return (2 * sizeof(plt0_entry_micromips_o32)
2599 / sizeof(plt0_entry_micromips_o32[0]));
2602 // Return the PLT header entry.
2603 const uint32_t*
2604 get_plt_header_entry() const
2606 if (this->target_->is_output_n64())
2607 return plt0_entry_n64;
2608 else if (this->target_->is_output_n32())
2609 return plt0_entry_n32;
2610 else if (!this->is_plt_header_compressed())
2611 return plt0_entry_o32;
2612 else if (this->target_->use_32bit_micromips_instructions())
2613 return plt0_entry_micromips32_o32;
2614 else
2615 return plt0_entry_micromips_o32;
2618 // Return the size of the standard PLT entry.
2619 unsigned int
2620 standard_plt_entry_size() const
2621 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2623 // Return the size of the compressed PLT entry.
2624 unsigned int
2625 compressed_plt_entry_size() const
2627 gold_assert(!this->target_->is_output_newabi());
2629 if (!this->target_->is_output_micromips())
2630 return (2 * sizeof(plt_entry_mips16_o32)
2631 / sizeof(plt_entry_mips16_o32[0]));
2632 else if (this->target_->use_32bit_micromips_instructions())
2633 return (2 * sizeof(plt_entry_micromips32_o32)
2634 / sizeof(plt_entry_micromips32_o32[0]));
2635 else
2636 return (2 * sizeof(plt_entry_micromips_o32)
2637 / sizeof(plt_entry_micromips_o32[0]));
2640 // The reloc section.
2641 Reloc_section* rel_;
2642 // The .got.plt section.
2643 Output_data_space* got_plt_;
2644 // Symbols that have PLT entry.
2645 std::vector<Mips_symbol<size>*> symbols_;
2646 // The offset of the next standard PLT entry to create.
2647 unsigned int plt_mips_offset_;
2648 // The offset of the next compressed PLT entry to create.
2649 unsigned int plt_comp_offset_;
2650 // The size of the PLT header in bytes.
2651 unsigned int plt_header_size_;
2652 // The target.
2653 Target_mips<size, big_endian>* target_;
2656 // A class to handle the .MIPS.stubs data.
2658 template<int size, bool big_endian>
2659 class Mips_output_data_mips_stubs : public Output_section_data
2661 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2663 // Unordered set of .MIPS.stubs entries.
2664 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2665 Mips_stubs_entry_set;
2667 public:
2668 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2669 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2670 stub_offsets_are_set_(false), target_(target)
2673 // Create entry for a symbol.
2674 void
2675 make_entry(Mips_symbol<size>*);
2677 // Remove entry for a symbol.
2678 void
2679 remove_entry(Mips_symbol<size>* gsym);
2681 // Set stub offsets for symbols. This method expects that the number of
2682 // entries in dynamic symbol table is set.
2683 void
2684 set_lazy_stub_offsets();
2686 void
2687 set_needs_dynsym_value();
2689 // Set the number of entries in dynamic symbol table.
2690 void
2691 set_dynsym_count(unsigned int dynsym_count)
2692 { this->dynsym_count_ = dynsym_count; }
2694 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2695 // count is greater than 0x10000. If the dynamic symbol count is less than
2696 // 0x10000, the stub will be 4 bytes smaller.
2697 // There's no disadvantage from using microMIPS code here, so for the sake of
2698 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2699 // output produced at all. This has a benefit of stubs being shorter by
2700 // 4 bytes each too, unless in the insn32 mode.
2701 unsigned int
2702 stub_max_size() const
2704 if (!this->target_->is_output_micromips()
2705 || this->target_->use_32bit_micromips_instructions())
2706 return 20;
2707 else
2708 return 16;
2711 // Return the size of the stub. This method expects that the final dynsym
2712 // count is set.
2713 unsigned int
2714 stub_size() const
2716 gold_assert(this->dynsym_count_ != -1U);
2717 if (this->dynsym_count_ > 0x10000)
2718 return this->stub_max_size();
2719 else
2720 return this->stub_max_size() - 4;
2723 // Return output address of a stub.
2724 Mips_address
2725 stub_address(const Mips_symbol<size>* sym) const
2727 gold_assert(sym->has_lazy_stub());
2728 return this->address() + sym->lazy_stub_offset();
2731 protected:
2732 void
2733 do_adjust_output_section(Output_section* os)
2734 { os->set_entsize(0); }
2736 // Write to a map file.
2737 void
2738 do_print_to_mapfile(Mapfile* mapfile) const
2739 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2741 private:
2742 static const uint32_t lazy_stub_normal_1[];
2743 static const uint32_t lazy_stub_normal_1_n64[];
2744 static const uint32_t lazy_stub_normal_2[];
2745 static const uint32_t lazy_stub_normal_2_n64[];
2746 static const uint32_t lazy_stub_big[];
2747 static const uint32_t lazy_stub_big_n64[];
2749 static const uint32_t lazy_stub_micromips_normal_1[];
2750 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2751 static const uint32_t lazy_stub_micromips_normal_2[];
2752 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2753 static const uint32_t lazy_stub_micromips_big[];
2754 static const uint32_t lazy_stub_micromips_big_n64[];
2756 static const uint32_t lazy_stub_micromips32_normal_1[];
2757 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2758 static const uint32_t lazy_stub_micromips32_normal_2[];
2759 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2760 static const uint32_t lazy_stub_micromips32_big[];
2761 static const uint32_t lazy_stub_micromips32_big_n64[];
2763 // Set the final size.
2764 void
2765 set_final_data_size()
2766 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2768 // Write out the .MIPS.stubs data.
2769 void
2770 do_write(Output_file*);
2772 // .MIPS.stubs symbols
2773 Mips_stubs_entry_set symbols_;
2774 // Number of entries in dynamic symbol table.
2775 unsigned int dynsym_count_;
2776 // Whether the stub offsets are set.
2777 bool stub_offsets_are_set_;
2778 // The target.
2779 Target_mips<size, big_endian>* target_;
2782 // This class handles Mips .reginfo output section.
2784 template<int size, bool big_endian>
2785 class Mips_output_section_reginfo : public Output_section_data
2787 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2789 public:
2790 Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2791 Valtype gprmask, Valtype cprmask1,
2792 Valtype cprmask2, Valtype cprmask3,
2793 Valtype cprmask4)
2794 : Output_section_data(24, 4, true), target_(target),
2795 gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2796 cprmask3_(cprmask3), cprmask4_(cprmask4)
2799 protected:
2800 // Write to a map file.
2801 void
2802 do_print_to_mapfile(Mapfile* mapfile) const
2803 { mapfile->print_output_data(this, _(".reginfo")); }
2805 // Write out reginfo section.
2806 void
2807 do_write(Output_file* of);
2809 private:
2810 Target_mips<size, big_endian>* target_;
2812 // gprmask of the output .reginfo section.
2813 Valtype gprmask_;
2814 // cprmask1 of the output .reginfo section.
2815 Valtype cprmask1_;
2816 // cprmask2 of the output .reginfo section.
2817 Valtype cprmask2_;
2818 // cprmask3 of the output .reginfo section.
2819 Valtype cprmask3_;
2820 // cprmask4 of the output .reginfo section.
2821 Valtype cprmask4_;
2824 // This class handles .MIPS.abiflags output section.
2826 template<int size, bool big_endian>
2827 class Mips_output_section_abiflags : public Output_section_data
2829 public:
2830 Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2831 : Output_section_data(24, 8, true), abiflags_(abiflags)
2834 protected:
2835 // Write to a map file.
2836 void
2837 do_print_to_mapfile(Mapfile* mapfile) const
2838 { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2840 void
2841 do_write(Output_file* of);
2843 private:
2844 const Mips_abiflags<big_endian>& abiflags_;
2847 // The MIPS target has relocation types which default handling of relocatable
2848 // relocation cannot process. So we have to extend the default code.
2850 template<bool big_endian, typename Classify_reloc>
2851 class Mips_scan_relocatable_relocs :
2852 public Default_scan_relocatable_relocs<Classify_reloc>
2854 public:
2855 // Return the strategy to use for a local symbol which is a section
2856 // symbol, given the relocation type.
2857 inline Relocatable_relocs::Reloc_strategy
2858 local_section_strategy(unsigned int r_type, Relobj* object)
2860 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
2861 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2862 else
2864 switch (r_type)
2866 case elfcpp::R_MIPS_26:
2867 return Relocatable_relocs::RELOC_SPECIAL;
2869 default:
2870 return Default_scan_relocatable_relocs<Classify_reloc>::
2871 local_section_strategy(r_type, object);
2877 // Mips_copy_relocs class. The only difference from the base class is the
2878 // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2879 // Mips cannot convert all relocation types to dynamic relocs. If a reloc
2880 // cannot be made dynamic, a COPY reloc is emitted.
2882 template<int sh_type, int size, bool big_endian>
2883 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2885 public:
2886 Mips_copy_relocs()
2887 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2890 // Emit any saved relocations which turn out to be needed. This is
2891 // called after all the relocs have been scanned.
2892 void
2893 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2894 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2896 private:
2897 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2898 Copy_reloc_entry;
2900 // Emit this reloc if appropriate. This is called after we have
2901 // scanned all the relocations, so we know whether we emitted a
2902 // COPY relocation for SYM_.
2903 void
2904 emit_entry(Copy_reloc_entry& entry,
2905 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2906 Symbol_table* symtab, Layout* layout,
2907 Target_mips<size, big_endian>* target);
2911 // Return true if the symbol SYM should be considered to resolve local
2912 // to the current module, and false otherwise. The logic is taken from
2913 // GNU ld's method _bfd_elf_symbol_refs_local_p.
2914 static bool
2915 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2916 bool local_protected)
2918 // If it's a local sym, of course we resolve locally.
2919 if (sym == NULL)
2920 return true;
2922 // STV_HIDDEN or STV_INTERNAL ones must be local.
2923 if (sym->visibility() == elfcpp::STV_HIDDEN
2924 || sym->visibility() == elfcpp::STV_INTERNAL)
2925 return true;
2927 // If we don't have a definition in a regular file, then we can't
2928 // resolve locally. The sym is either undefined or dynamic.
2929 if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic()
2930 || sym->is_undefined())
2931 return false;
2933 // Forced local symbols resolve locally.
2934 if (sym->is_forced_local())
2935 return true;
2937 // As do non-dynamic symbols.
2938 if (!has_dynsym_entry)
2939 return true;
2941 // At this point, we know the symbol is defined and dynamic. In an
2942 // executable it must resolve locally, likewise when building symbolic
2943 // shared libraries.
2944 if (parameters->options().output_is_executable()
2945 || parameters->options().Bsymbolic())
2946 return true;
2948 // Now deal with defined dynamic symbols in shared libraries. Ones
2949 // with default visibility might not resolve locally.
2950 if (sym->visibility() == elfcpp::STV_DEFAULT)
2951 return false;
2953 // STV_PROTECTED non-function symbols are local.
2954 if (sym->type() != elfcpp::STT_FUNC)
2955 return true;
2957 // Function pointer equality tests may require that STV_PROTECTED
2958 // symbols be treated as dynamic symbols. If the address of a
2959 // function not defined in an executable is set to that function's
2960 // plt entry in the executable, then the address of the function in
2961 // a shared library must also be the plt entry in the executable.
2962 return local_protected;
2965 // Return TRUE if references to this symbol always reference the symbol in this
2966 // object.
2967 static bool
2968 symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
2970 return symbol_refs_local(sym, has_dynsym_entry, false);
2973 // Return TRUE if calls to this symbol always call the version in this object.
2974 static bool
2975 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
2977 return symbol_refs_local(sym, has_dynsym_entry, true);
2980 // Compare GOT offsets of two symbols.
2982 template<int size, bool big_endian>
2983 static bool
2984 got_offset_compare(Symbol* sym1, Symbol* sym2)
2986 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
2987 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
2988 unsigned int area1 = mips_sym1->global_got_area();
2989 unsigned int area2 = mips_sym2->global_got_area();
2990 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
2992 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
2993 if (area1 != area2)
2994 return area1 < area2;
2996 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
2999 // This method divides dynamic symbols into symbols that have GOT entry, and
3000 // symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
3001 // Mips ABI requires that symbols with the GOT entry must be at the end of
3002 // dynamic symbol table, and the order in dynamic symbol table must match the
3003 // order in GOT.
3005 template<int size, bool big_endian>
3006 static void
3007 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3008 std::vector<Symbol*>* non_got_symbols,
3009 std::vector<Symbol*>* got_symbols)
3011 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3012 p != dyn_symbols->end();
3013 ++p)
3015 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3016 if (mips_sym->global_got_area() == GGA_NORMAL
3017 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3018 got_symbols->push_back(mips_sym);
3019 else
3020 non_got_symbols->push_back(mips_sym);
3023 std::sort(got_symbols->begin(), got_symbols->end(),
3024 got_offset_compare<size, big_endian>);
3027 // Functor class for processing the global symbol table.
3029 template<int size, bool big_endian>
3030 class Symbol_visitor_check_symbols
3032 public:
3033 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3034 Layout* layout, Symbol_table* symtab)
3035 : target_(target), layout_(layout), symtab_(symtab)
3038 void
3039 operator()(Sized_symbol<size>* sym)
3041 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3042 if (local_pic_function<size, big_endian>(mips_sym))
3044 // SYM is a function that might need $25 to be valid on entry.
3045 // If we're creating a non-PIC relocatable object, mark SYM as
3046 // being PIC. If we're creating a non-relocatable object with
3047 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3048 // stub.
3049 if (parameters->options().relocatable())
3051 if (!parameters->options().output_is_position_independent())
3052 mips_sym->set_pic();
3054 else if (mips_sym->has_nonpic_branches())
3056 this->target_->la25_stub_section(layout_)
3057 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3062 private:
3063 Target_mips<size, big_endian>* target_;
3064 Layout* layout_;
3065 Symbol_table* symtab_;
3068 // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3069 // and endianness. The relocation format for MIPS-64 is non-standard.
3071 template<int sh_type, int size, bool big_endian>
3072 struct Mips_reloc_types;
3074 template<bool big_endian>
3075 struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3077 typedef typename elfcpp::Rel<32, big_endian> Reloc;
3078 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3080 static typename elfcpp::Elf_types<32>::Elf_Swxword
3081 get_r_addend(const Reloc*)
3082 { return 0; }
3084 static inline void
3085 set_reloc_addend(Reloc_write*,
3086 typename elfcpp::Elf_types<32>::Elf_Swxword)
3087 { gold_unreachable(); }
3090 template<bool big_endian>
3091 struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3093 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3094 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3096 static typename elfcpp::Elf_types<32>::Elf_Swxword
3097 get_r_addend(const Reloc* reloc)
3098 { return reloc->get_r_addend(); }
3100 static inline void
3101 set_reloc_addend(Reloc_write* p,
3102 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3103 { p->put_r_addend(val); }
3106 template<bool big_endian>
3107 struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3109 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3110 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3112 static typename elfcpp::Elf_types<64>::Elf_Swxword
3113 get_r_addend(const Reloc*)
3114 { return 0; }
3116 static inline void
3117 set_reloc_addend(Reloc_write*,
3118 typename elfcpp::Elf_types<64>::Elf_Swxword)
3119 { gold_unreachable(); }
3122 template<bool big_endian>
3123 struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3125 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3126 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3128 static typename elfcpp::Elf_types<64>::Elf_Swxword
3129 get_r_addend(const Reloc* reloc)
3130 { return reloc->get_r_addend(); }
3132 static inline void
3133 set_reloc_addend(Reloc_write* p,
3134 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3135 { p->put_r_addend(val); }
3138 // Forward declaration.
3139 static unsigned int
3140 mips_get_size_for_reloc(unsigned int, Relobj*);
3142 // A class for inquiring about properties of a relocation,
3143 // used while scanning relocs during a relocatable link and
3144 // garbage collection.
3146 template<int sh_type_, int size, bool big_endian>
3147 class Mips_classify_reloc;
3149 template<int sh_type_, bool big_endian>
3150 class Mips_classify_reloc<sh_type_, 32, big_endian> :
3151 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3153 public:
3154 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3155 Reltype;
3156 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3157 Reltype_write;
3159 // Return the symbol referred to by the relocation.
3160 static inline unsigned int
3161 get_r_sym(const Reltype* reloc)
3162 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3164 // Return the type of the relocation.
3165 static inline unsigned int
3166 get_r_type(const Reltype* reloc)
3167 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3169 static inline unsigned int
3170 get_r_type2(const Reltype*)
3171 { return 0; }
3173 static inline unsigned int
3174 get_r_type3(const Reltype*)
3175 { return 0; }
3177 static inline unsigned int
3178 get_r_ssym(const Reltype*)
3179 { return 0; }
3181 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3182 static inline unsigned int
3183 get_r_addend(const Reltype* reloc)
3185 if (sh_type_ == elfcpp::SHT_REL)
3186 return 0;
3187 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3190 // Write the r_info field to a new reloc, using the r_info field from
3191 // the original reloc, replacing the r_sym field with R_SYM.
3192 static inline void
3193 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3195 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
3196 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
3199 // Write the r_addend field to a new reloc.
3200 static inline void
3201 put_r_addend(Reltype_write* to,
3202 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3203 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3205 // Return the size of the addend of the relocation (only used for SHT_REL).
3206 static unsigned int
3207 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3208 { return mips_get_size_for_reloc(r_type, obj); }
3211 template<int sh_type_, bool big_endian>
3212 class Mips_classify_reloc<sh_type_, 64, big_endian> :
3213 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3215 public:
3216 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3217 Reltype;
3218 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3219 Reltype_write;
3221 // Return the symbol referred to by the relocation.
3222 static inline unsigned int
3223 get_r_sym(const Reltype* reloc)
3224 { return reloc->get_r_sym(); }
3226 // Return the r_type of the relocation.
3227 static inline unsigned int
3228 get_r_type(const Reltype* reloc)
3229 { return reloc->get_r_type(); }
3231 // Return the r_type2 of the relocation.
3232 static inline unsigned int
3233 get_r_type2(const Reltype* reloc)
3234 { return reloc->get_r_type2(); }
3236 // Return the r_type3 of the relocation.
3237 static inline unsigned int
3238 get_r_type3(const Reltype* reloc)
3239 { return reloc->get_r_type3(); }
3241 // Return the special symbol of the relocation.
3242 static inline unsigned int
3243 get_r_ssym(const Reltype* reloc)
3244 { return reloc->get_r_ssym(); }
3246 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3247 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3248 get_r_addend(const Reltype* reloc)
3250 if (sh_type_ == elfcpp::SHT_REL)
3251 return 0;
3252 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3255 // Write the r_info field to a new reloc, using the r_info field from
3256 // the original reloc, replacing the r_sym field with R_SYM.
3257 static inline void
3258 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3260 new_reloc->put_r_sym(r_sym);
3261 new_reloc->put_r_ssym(reloc->get_r_ssym());
3262 new_reloc->put_r_type3(reloc->get_r_type3());
3263 new_reloc->put_r_type2(reloc->get_r_type2());
3264 new_reloc->put_r_type(reloc->get_r_type());
3267 // Write the r_addend field to a new reloc.
3268 static inline void
3269 put_r_addend(Reltype_write* to,
3270 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3271 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3273 // Return the size of the addend of the relocation (only used for SHT_REL).
3274 static unsigned int
3275 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3276 { return mips_get_size_for_reloc(r_type, obj); }
3279 template<int size, bool big_endian>
3280 class Target_mips : public Sized_target<size, big_endian>
3282 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3283 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3284 Reloc_section;
3285 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3286 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3287 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3288 Reltype;
3289 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3290 Relatype;
3292 public:
3293 Target_mips(const Target::Target_info* info = &mips_info)
3294 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3295 got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3296 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3297 mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3298 mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
3299 entry_symbol_is_compressed_(false), insn32_(false)
3301 this->add_machine_extensions();
3304 // The offset of $gp from the beginning of the .got section.
3305 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3307 // The maximum size of the GOT for it to be addressable using 16-bit
3308 // offsets from $gp.
3309 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3311 // Make a new symbol table entry for the Mips target.
3312 Sized_symbol<size>*
3313 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
3314 { return new Mips_symbol<size>(); }
3316 // Process the relocations to determine unreferenced sections for
3317 // garbage collection.
3318 void
3319 gc_process_relocs(Symbol_table* symtab,
3320 Layout* layout,
3321 Sized_relobj_file<size, big_endian>* object,
3322 unsigned int data_shndx,
3323 unsigned int sh_type,
3324 const unsigned char* prelocs,
3325 size_t reloc_count,
3326 Output_section* output_section,
3327 bool needs_special_offset_handling,
3328 size_t local_symbol_count,
3329 const unsigned char* plocal_symbols);
3331 // Scan the relocations to look for symbol adjustments.
3332 void
3333 scan_relocs(Symbol_table* symtab,
3334 Layout* layout,
3335 Sized_relobj_file<size, big_endian>* object,
3336 unsigned int data_shndx,
3337 unsigned int sh_type,
3338 const unsigned char* prelocs,
3339 size_t reloc_count,
3340 Output_section* output_section,
3341 bool needs_special_offset_handling,
3342 size_t local_symbol_count,
3343 const unsigned char* plocal_symbols);
3345 // Finalize the sections.
3346 void
3347 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3349 // Relocate a section.
3350 void
3351 relocate_section(const Relocate_info<size, big_endian>*,
3352 unsigned int sh_type,
3353 const unsigned char* prelocs,
3354 size_t reloc_count,
3355 Output_section* output_section,
3356 bool needs_special_offset_handling,
3357 unsigned char* view,
3358 Mips_address view_address,
3359 section_size_type view_size,
3360 const Reloc_symbol_changes*);
3362 // Scan the relocs during a relocatable link.
3363 void
3364 scan_relocatable_relocs(Symbol_table* symtab,
3365 Layout* layout,
3366 Sized_relobj_file<size, big_endian>* object,
3367 unsigned int data_shndx,
3368 unsigned int sh_type,
3369 const unsigned char* prelocs,
3370 size_t reloc_count,
3371 Output_section* output_section,
3372 bool needs_special_offset_handling,
3373 size_t local_symbol_count,
3374 const unsigned char* plocal_symbols,
3375 Relocatable_relocs*);
3377 // Scan the relocs for --emit-relocs.
3378 void
3379 emit_relocs_scan(Symbol_table* symtab,
3380 Layout* layout,
3381 Sized_relobj_file<size, big_endian>* object,
3382 unsigned int data_shndx,
3383 unsigned int sh_type,
3384 const unsigned char* prelocs,
3385 size_t reloc_count,
3386 Output_section* output_section,
3387 bool needs_special_offset_handling,
3388 size_t local_symbol_count,
3389 const unsigned char* plocal_syms,
3390 Relocatable_relocs* rr);
3392 // Emit relocations for a section.
3393 void
3394 relocate_relocs(const Relocate_info<size, big_endian>*,
3395 unsigned int sh_type,
3396 const unsigned char* prelocs,
3397 size_t reloc_count,
3398 Output_section* output_section,
3399 typename elfcpp::Elf_types<size>::Elf_Off
3400 offset_in_output_section,
3401 unsigned char* view,
3402 Mips_address view_address,
3403 section_size_type view_size,
3404 unsigned char* reloc_view,
3405 section_size_type reloc_view_size);
3407 // Perform target-specific processing in a relocatable link. This is
3408 // only used if we use the relocation strategy RELOC_SPECIAL.
3409 void
3410 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3411 unsigned int sh_type,
3412 const unsigned char* preloc_in,
3413 size_t relnum,
3414 Output_section* output_section,
3415 typename elfcpp::Elf_types<size>::Elf_Off
3416 offset_in_output_section,
3417 unsigned char* view,
3418 Mips_address view_address,
3419 section_size_type view_size,
3420 unsigned char* preloc_out);
3422 // Return whether SYM is defined by the ABI.
3423 bool
3424 do_is_defined_by_abi(const Symbol* sym) const
3426 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3427 || (strcmp(sym->name(), "_gp_disp") == 0)
3428 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3431 // Return the number of entries in the GOT.
3432 unsigned int
3433 got_entry_count() const
3435 if (!this->has_got_section())
3436 return 0;
3437 return this->got_size() / (size/8);
3440 // Return the number of entries in the PLT.
3441 unsigned int
3442 plt_entry_count() const
3444 if (this->plt_ == NULL)
3445 return 0;
3446 return this->plt_->entry_count();
3449 // Return the offset of the first non-reserved PLT entry.
3450 unsigned int
3451 first_plt_entry_offset() const
3452 { return this->plt_->first_plt_entry_offset(); }
3454 // Return the size of each PLT entry.
3455 unsigned int
3456 plt_entry_size() const
3457 { return this->plt_->plt_entry_size(); }
3459 // Get the GOT section, creating it if necessary.
3460 Mips_output_data_got<size, big_endian>*
3461 got_section(Symbol_table*, Layout*);
3463 // Get the GOT section.
3464 Mips_output_data_got<size, big_endian>*
3465 got_section() const
3467 gold_assert(this->got_ != NULL);
3468 return this->got_;
3471 // Get the .MIPS.stubs section, creating it if necessary.
3472 Mips_output_data_mips_stubs<size, big_endian>*
3473 mips_stubs_section(Layout* layout);
3475 // Get the .MIPS.stubs section.
3476 Mips_output_data_mips_stubs<size, big_endian>*
3477 mips_stubs_section() const
3479 gold_assert(this->mips_stubs_ != NULL);
3480 return this->mips_stubs_;
3483 // Get the LA25 stub section, creating it if necessary.
3484 Mips_output_data_la25_stub<size, big_endian>*
3485 la25_stub_section(Layout*);
3487 // Get the LA25 stub section.
3488 Mips_output_data_la25_stub<size, big_endian>*
3489 la25_stub_section()
3491 gold_assert(this->la25_stub_ != NULL);
3492 return this->la25_stub_;
3495 // Get gp value. It has the value of .got + 0x7FF0.
3496 Mips_address
3497 gp_value() const
3499 if (this->gp_ != NULL)
3500 return this->gp_->value();
3501 return 0;
3504 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3505 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3506 Mips_address
3507 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3509 if (this->gp_ == NULL)
3510 return 0;
3512 bool multi_got = false;
3513 if (this->has_got_section())
3514 multi_got = this->got_section()->multi_got();
3515 if (!multi_got)
3516 return this->gp_->value();
3517 else
3518 return this->gp_->value() + this->got_section()->get_got_offset(object);
3521 // Get the dynamic reloc section, creating it if necessary.
3522 Reloc_section*
3523 rel_dyn_section(Layout*);
3525 bool
3526 do_has_custom_set_dynsym_indexes() const
3527 { return true; }
3529 // Don't emit input .reginfo/.MIPS.abiflags sections to
3530 // output .reginfo/.MIPS.abiflags.
3531 bool
3532 do_should_include_section(elfcpp::Elf_Word sh_type) const
3534 return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3535 && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3538 // Set the dynamic symbol indexes. INDEX is the index of the first
3539 // global dynamic symbol. Pointers to the symbols are stored into the
3540 // vector SYMS. The names are added to DYNPOOL. This returns an
3541 // updated dynamic symbol index.
3542 unsigned int
3543 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3544 std::vector<Symbol*>* syms, Stringpool* dynpool,
3545 Versions* versions, Symbol_table* symtab) const;
3547 // Remove .MIPS.stubs entry for a symbol.
3548 void
3549 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3551 if (this->mips_stubs_ != NULL)
3552 this->mips_stubs_->remove_entry(sym);
3555 // The value to write into got[1] for SVR4 targets, to identify it is
3556 // a GNU object. The dynamic linker can then use got[1] to store the
3557 // module pointer.
3558 uint64_t
3559 mips_elf_gnu_got1_mask()
3561 if (this->is_output_n64())
3562 return (uint64_t)1 << 63;
3563 else
3564 return 1 << 31;
3567 // Whether the output has microMIPS code. This is valid only after
3568 // merge_obj_e_flags() is called.
3569 bool
3570 is_output_micromips() const
3572 gold_assert(this->are_processor_specific_flags_set());
3573 return elfcpp::is_micromips(this->processor_specific_flags());
3576 // Whether the output uses N32 ABI. This is valid only after
3577 // merge_obj_e_flags() is called.
3578 bool
3579 is_output_n32() const
3581 gold_assert(this->are_processor_specific_flags_set());
3582 return elfcpp::abi_n32(this->processor_specific_flags());
3585 // Whether the output uses R6 ISA. This is valid only after
3586 // merge_obj_e_flags() is called.
3587 bool
3588 is_output_r6() const
3590 gold_assert(this->are_processor_specific_flags_set());
3591 return elfcpp::r6_isa(this->processor_specific_flags());
3594 // Whether the output uses N64 ABI.
3595 bool
3596 is_output_n64() const
3597 { return size == 64; }
3599 // Whether the output uses NEWABI. This is valid only after
3600 // merge_obj_e_flags() is called.
3601 bool
3602 is_output_newabi() const
3603 { return this->is_output_n32() || this->is_output_n64(); }
3605 // Whether we can only use 32-bit microMIPS instructions.
3606 bool
3607 use_32bit_micromips_instructions() const
3608 { return this->insn32_; }
3610 // Return the r_sym field from a relocation.
3611 unsigned int
3612 get_r_sym(const unsigned char* preloc) const
3614 // Since REL and RELA relocs share the same structure through
3615 // the r_info field, we can just use REL here.
3616 Reltype rel(preloc);
3617 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3618 get_r_sym(&rel);
3621 protected:
3622 // Return the value to use for a dynamic symbol which requires special
3623 // treatment. This is how we support equality comparisons of function
3624 // pointers across shared library boundaries, as described in the
3625 // processor specific ABI supplement.
3626 uint64_t
3627 do_dynsym_value(const Symbol* gsym) const;
3629 // Make an ELF object.
3630 Object*
3631 do_make_elf_object(const std::string&, Input_file*, off_t,
3632 const elfcpp::Ehdr<size, big_endian>& ehdr);
3634 Object*
3635 do_make_elf_object(const std::string&, Input_file*, off_t,
3636 const elfcpp::Ehdr<size, !big_endian>&)
3637 { gold_unreachable(); }
3639 // Adjust ELF file header.
3640 void
3641 do_adjust_elf_header(unsigned char* view, int len);
3643 // Get the custom dynamic tag value.
3644 unsigned int
3645 do_dynamic_tag_custom_value(elfcpp::DT) const;
3647 // Adjust the value written to the dynamic symbol table.
3648 virtual void
3649 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3651 elfcpp::Sym<size, big_endian> isym(view);
3652 elfcpp::Sym_write<size, big_endian> osym(view);
3653 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3655 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3656 // to treat compressed symbols like any other.
3657 Mips_address value = isym.get_st_value();
3658 if (mips_sym->is_mips16() && value != 0)
3660 if (!mips_sym->has_mips16_fn_stub())
3661 value |= 1;
3662 else
3664 // If we have a MIPS16 function with a stub, the dynamic symbol
3665 // must refer to the stub, since only the stub uses the standard
3666 // calling conventions. Stub contains MIPS32 code, so don't add +1
3667 // in this case.
3669 // There is a code which does this in the method
3670 // Target_mips::do_dynsym_value, but that code will only be
3671 // executed if the symbol is from dynobj.
3672 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3673 // table.
3675 Mips16_stub_section<size, big_endian>* fn_stub =
3676 mips_sym->template get_mips16_fn_stub<big_endian>();
3677 value = fn_stub->output_address();
3678 osym.put_st_size(fn_stub->section_size());
3681 osym.put_st_value(value);
3682 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3683 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3685 else if ((mips_sym->is_micromips()
3686 // Stubs are always microMIPS if there is any microMIPS code in
3687 // the output.
3688 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3689 && value != 0)
3691 osym.put_st_value(value | 1);
3692 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3693 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3697 private:
3698 // The class which scans relocations.
3699 class Scan
3701 public:
3702 Scan()
3705 static inline int
3706 get_reference_flags(unsigned int r_type);
3708 inline void
3709 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3710 Sized_relobj_file<size, big_endian>* object,
3711 unsigned int data_shndx,
3712 Output_section* output_section,
3713 const Reltype& reloc, unsigned int r_type,
3714 const elfcpp::Sym<size, big_endian>& lsym,
3715 bool is_discarded);
3717 inline void
3718 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3719 Sized_relobj_file<size, big_endian>* object,
3720 unsigned int data_shndx,
3721 Output_section* output_section,
3722 const Relatype& reloc, unsigned int r_type,
3723 const elfcpp::Sym<size, big_endian>& lsym,
3724 bool is_discarded);
3726 inline void
3727 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3728 Sized_relobj_file<size, big_endian>* object,
3729 unsigned int data_shndx,
3730 Output_section* output_section,
3731 const Relatype* rela,
3732 const Reltype* rel,
3733 unsigned int rel_type,
3734 unsigned int r_type,
3735 const elfcpp::Sym<size, big_endian>& lsym,
3736 bool is_discarded);
3738 inline void
3739 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3740 Sized_relobj_file<size, big_endian>* object,
3741 unsigned int data_shndx,
3742 Output_section* output_section,
3743 const Reltype& reloc, unsigned int r_type,
3744 Symbol* gsym);
3746 inline void
3747 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3748 Sized_relobj_file<size, big_endian>* object,
3749 unsigned int data_shndx,
3750 Output_section* output_section,
3751 const Relatype& reloc, unsigned int r_type,
3752 Symbol* gsym);
3754 inline void
3755 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3756 Sized_relobj_file<size, big_endian>* object,
3757 unsigned int data_shndx,
3758 Output_section* output_section,
3759 const Relatype* rela,
3760 const Reltype* rel,
3761 unsigned int rel_type,
3762 unsigned int r_type,
3763 Symbol* gsym);
3765 inline bool
3766 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3767 Target_mips*,
3768 Sized_relobj_file<size, big_endian>*,
3769 unsigned int,
3770 Output_section*,
3771 const Reltype&,
3772 unsigned int,
3773 const elfcpp::Sym<size, big_endian>&)
3774 { return false; }
3776 inline bool
3777 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3778 Target_mips*,
3779 Sized_relobj_file<size, big_endian>*,
3780 unsigned int,
3781 Output_section*,
3782 const Reltype&,
3783 unsigned int, Symbol*)
3784 { return false; }
3786 inline bool
3787 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3788 Target_mips*,
3789 Sized_relobj_file<size, big_endian>*,
3790 unsigned int,
3791 Output_section*,
3792 const Relatype&,
3793 unsigned int,
3794 const elfcpp::Sym<size, big_endian>&)
3795 { return false; }
3797 inline bool
3798 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3799 Target_mips*,
3800 Sized_relobj_file<size, big_endian>*,
3801 unsigned int,
3802 Output_section*,
3803 const Relatype&,
3804 unsigned int, Symbol*)
3805 { return false; }
3806 private:
3807 static void
3808 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3809 unsigned int r_type);
3811 static void
3812 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3813 unsigned int r_type, Symbol*);
3816 // The class which implements relocation.
3817 class Relocate
3819 public:
3820 Relocate()
3823 ~Relocate()
3826 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
3827 inline bool
3828 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3829 unsigned int r_type,
3830 Output_section* output_section,
3831 Target_mips* target);
3833 // Do a relocation. Return false if the caller should not issue
3834 // any warnings about this relocation.
3835 inline bool
3836 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3837 Target_mips*, Output_section*, size_t, const unsigned char*,
3838 const Sized_symbol<size>*, const Symbol_value<size>*,
3839 unsigned char*, Mips_address, section_size_type);
3842 // This POD class holds the dynamic relocations that should be emitted instead
3843 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3844 // relocations if it turns out that the symbol does not have static
3845 // relocations.
3846 class Dyn_reloc
3848 public:
3849 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3850 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3851 Output_section* output_section, Mips_address r_offset)
3852 : sym_(sym), r_type_(r_type), relobj_(relobj),
3853 shndx_(shndx), output_section_(output_section),
3854 r_offset_(r_offset)
3857 // Emit this reloc if appropriate. This is called after we have
3858 // scanned all the relocations, so we know whether the symbol has
3859 // static relocations.
3860 void
3861 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3862 Symbol_table* symtab)
3864 if (!this->sym_->has_static_relocs())
3866 got->record_global_got_symbol(this->sym_, this->relobj_,
3867 this->r_type_, true, false);
3868 if (!symbol_references_local(this->sym_,
3869 this->sym_->should_add_dynsym_entry(symtab)))
3870 rel_dyn->add_global(this->sym_, this->r_type_,
3871 this->output_section_, this->relobj_,
3872 this->shndx_, this->r_offset_);
3873 else
3874 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3875 this->output_section_, this->relobj_,
3876 this->shndx_, this->r_offset_);
3880 private:
3881 Mips_symbol<size>* sym_;
3882 unsigned int r_type_;
3883 Mips_relobj<size, big_endian>* relobj_;
3884 unsigned int shndx_;
3885 Output_section* output_section_;
3886 Mips_address r_offset_;
3889 // Adjust TLS relocation type based on the options and whether this
3890 // is a local symbol.
3891 static tls::Tls_optimization
3892 optimize_tls_reloc(bool is_final, int r_type);
3894 // Return whether there is a GOT section.
3895 bool
3896 has_got_section() const
3897 { return this->got_ != NULL; }
3899 // Check whether the given ELF header flags describe a 32-bit binary.
3900 bool
3901 mips_32bit_flags(elfcpp::Elf_Word);
3903 enum Mips_mach {
3904 mach_mips3000 = 3000,
3905 mach_mips3900 = 3900,
3906 mach_mips4000 = 4000,
3907 mach_mips4010 = 4010,
3908 mach_mips4100 = 4100,
3909 mach_mips4111 = 4111,
3910 mach_mips4120 = 4120,
3911 mach_mips4300 = 4300,
3912 mach_mips4400 = 4400,
3913 mach_mips4600 = 4600,
3914 mach_mips4650 = 4650,
3915 mach_mips5000 = 5000,
3916 mach_mips5400 = 5400,
3917 mach_mips5500 = 5500,
3918 mach_mips5900 = 5900,
3919 mach_mips6000 = 6000,
3920 mach_mips7000 = 7000,
3921 mach_mips8000 = 8000,
3922 mach_mips9000 = 9000,
3923 mach_mips10000 = 10000,
3924 mach_mips12000 = 12000,
3925 mach_mips14000 = 14000,
3926 mach_mips16000 = 16000,
3927 mach_mips16 = 16,
3928 mach_mips5 = 5,
3929 mach_mips_loongson_2e = 3001,
3930 mach_mips_loongson_2f = 3002,
3931 mach_mips_loongson_3a = 3003,
3932 mach_mips_sb1 = 12310201, // octal 'SB', 01
3933 mach_mips_octeon = 6501,
3934 mach_mips_octeonp = 6601,
3935 mach_mips_octeon2 = 6502,
3936 mach_mips_octeon3 = 6503,
3937 mach_mips_xlr = 887682, // decimal 'XLR'
3938 mach_mipsisa32 = 32,
3939 mach_mipsisa32r2 = 33,
3940 mach_mipsisa32r3 = 34,
3941 mach_mipsisa32r5 = 36,
3942 mach_mipsisa32r6 = 37,
3943 mach_mipsisa64 = 64,
3944 mach_mipsisa64r2 = 65,
3945 mach_mipsisa64r3 = 66,
3946 mach_mipsisa64r5 = 68,
3947 mach_mipsisa64r6 = 69,
3948 mach_mips_micromips = 96
3951 // Return the MACH for a MIPS e_flags value.
3952 unsigned int
3953 elf_mips_mach(elfcpp::Elf_Word);
3955 // Return the MACH for each .MIPS.abiflags ISA Extension.
3956 unsigned int
3957 mips_isa_ext_mach(unsigned int);
3959 // Return the .MIPS.abiflags value representing each ISA Extension.
3960 unsigned int
3961 mips_isa_ext(unsigned int);
3963 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
3964 void
3965 update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
3966 Mips_abiflags<big_endian>*);
3968 // Infer the content of the ABI flags based on the elf header.
3969 void
3970 infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
3972 // Create abiflags from elf header or from .MIPS.abiflags section.
3973 void
3974 create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
3976 // Return the meaning of fp_abi, or "unknown" if not known.
3977 const char*
3978 fp_abi_string(int);
3980 // Select fp_abi.
3982 select_fp_abi(const std::string&, int, int);
3984 // Merge attributes from input object.
3985 void
3986 merge_obj_attributes(const std::string&, const Attributes_section_data*);
3988 // Merge abiflags from input object.
3989 void
3990 merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
3992 // Check whether machine EXTENSION is an extension of machine BASE.
3993 bool
3994 mips_mach_extends(unsigned int, unsigned int);
3996 // Merge file header flags from input object.
3997 void
3998 merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4000 // Encode ISA level and revision as a single value.
4002 level_rev(unsigned char isa_level, unsigned char isa_rev) const
4003 { return (isa_level << 3) | isa_rev; }
4005 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4006 static inline bool
4007 jal_to_bal()
4008 { return false; }
4010 // True if we are linking for CPUs that are faster if JALR is converted to
4011 // BAL. This should be safe for all architectures. We enable this predicate
4012 // for all CPUs.
4013 static inline bool
4014 jalr_to_bal()
4015 { return true; }
4017 // True if we are linking for CPUs that are faster if JR is converted to B.
4018 // This should be safe for all architectures. We enable this predicate for
4019 // all CPUs.
4020 static inline bool
4021 jr_to_b()
4022 { return true; }
4024 // Return the size of the GOT section.
4025 section_size_type
4026 got_size() const
4028 gold_assert(this->got_ != NULL);
4029 return this->got_->data_size();
4032 // Create a PLT entry for a global symbol referenced by r_type relocation.
4033 void
4034 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4035 unsigned int r_type);
4037 // Get the PLT section.
4038 Mips_output_data_plt<size, big_endian>*
4039 plt_section() const
4041 gold_assert(this->plt_ != NULL);
4042 return this->plt_;
4045 // Get the GOT PLT section.
4046 const Mips_output_data_plt<size, big_endian>*
4047 got_plt_section() const
4049 gold_assert(this->got_plt_ != NULL);
4050 return this->got_plt_;
4053 // Copy a relocation against a global symbol.
4054 void
4055 copy_reloc(Symbol_table* symtab, Layout* layout,
4056 Sized_relobj_file<size, big_endian>* object,
4057 unsigned int shndx, Output_section* output_section,
4058 Symbol* sym, unsigned int r_type, Mips_address r_offset)
4060 this->copy_relocs_.copy_reloc(symtab, layout,
4061 symtab->get_sized_symbol<size>(sym),
4062 object, shndx, output_section,
4063 r_type, r_offset, 0,
4064 this->rel_dyn_section(layout));
4067 void
4068 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4069 Mips_relobj<size, big_endian>* relobj,
4070 unsigned int shndx, Output_section* output_section,
4071 Mips_address r_offset)
4073 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4074 output_section, r_offset));
4077 // Calculate value of _gp symbol.
4078 void
4079 set_gp(Layout*, Symbol_table*);
4081 const char*
4082 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
4083 const char*
4084 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4086 // Adds entries that describe how machines relate to one another. The entries
4087 // are ordered topologically with MIPS I extensions listed last. First
4088 // element is extension, second element is base.
4089 void
4090 add_machine_extensions()
4092 // MIPS64r2 extensions.
4093 this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
4094 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4095 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4096 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
4097 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64r2);
4099 // MIPS64 extensions.
4100 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4101 this->add_extension(mach_mips_sb1, mach_mipsisa64);
4102 this->add_extension(mach_mips_xlr, mach_mipsisa64);
4104 // MIPS V extensions.
4105 this->add_extension(mach_mipsisa64, mach_mips5);
4107 // R10000 extensions.
4108 this->add_extension(mach_mips12000, mach_mips10000);
4109 this->add_extension(mach_mips14000, mach_mips10000);
4110 this->add_extension(mach_mips16000, mach_mips10000);
4112 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
4113 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
4114 // better to allow vr5400 and vr5500 code to be merged anyway, since
4115 // many libraries will just use the core ISA. Perhaps we could add
4116 // some sort of ASE flag if this ever proves a problem.
4117 this->add_extension(mach_mips5500, mach_mips5400);
4118 this->add_extension(mach_mips5400, mach_mips5000);
4120 // MIPS IV extensions.
4121 this->add_extension(mach_mips5, mach_mips8000);
4122 this->add_extension(mach_mips10000, mach_mips8000);
4123 this->add_extension(mach_mips5000, mach_mips8000);
4124 this->add_extension(mach_mips7000, mach_mips8000);
4125 this->add_extension(mach_mips9000, mach_mips8000);
4127 // VR4100 extensions.
4128 this->add_extension(mach_mips4120, mach_mips4100);
4129 this->add_extension(mach_mips4111, mach_mips4100);
4131 // MIPS III extensions.
4132 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4133 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4134 this->add_extension(mach_mips8000, mach_mips4000);
4135 this->add_extension(mach_mips4650, mach_mips4000);
4136 this->add_extension(mach_mips4600, mach_mips4000);
4137 this->add_extension(mach_mips4400, mach_mips4000);
4138 this->add_extension(mach_mips4300, mach_mips4000);
4139 this->add_extension(mach_mips4100, mach_mips4000);
4140 this->add_extension(mach_mips4010, mach_mips4000);
4141 this->add_extension(mach_mips5900, mach_mips4000);
4143 // MIPS32 extensions.
4144 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4146 // MIPS II extensions.
4147 this->add_extension(mach_mips4000, mach_mips6000);
4148 this->add_extension(mach_mipsisa32, mach_mips6000);
4150 // MIPS I extensions.
4151 this->add_extension(mach_mips6000, mach_mips3000);
4152 this->add_extension(mach_mips3900, mach_mips3000);
4155 // Add value to MIPS extenstions.
4156 void
4157 add_extension(unsigned int base, unsigned int extension)
4159 std::pair<unsigned int, unsigned int> ext(base, extension);
4160 this->mips_mach_extensions_.push_back(ext);
4163 // Return the number of entries in the .dynsym section.
4164 unsigned int get_dt_mips_symtabno() const
4166 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4167 / elfcpp::Elf_sizes<size>::sym_size));
4168 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4171 // Information about this specific target which we pass to the
4172 // general Target structure.
4173 static const Target::Target_info mips_info;
4174 // The GOT section.
4175 Mips_output_data_got<size, big_endian>* got_;
4176 // gp symbol. It has the value of .got + 0x7FF0.
4177 Sized_symbol<size>* gp_;
4178 // The PLT section.
4179 Mips_output_data_plt<size, big_endian>* plt_;
4180 // The GOT PLT section.
4181 Output_data_space* got_plt_;
4182 // The dynamic reloc section.
4183 Reloc_section* rel_dyn_;
4184 // The .rld_map section.
4185 Output_data_zero_fill* rld_map_;
4186 // Relocs saved to avoid a COPY reloc.
4187 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4189 // A list of dyn relocs to be saved.
4190 std::vector<Dyn_reloc> dyn_relocs_;
4192 // The LA25 stub section.
4193 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4194 // Architecture extensions.
4195 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4196 // .MIPS.stubs
4197 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4199 // Attributes section data in output.
4200 Attributes_section_data* attributes_section_data_;
4201 // .MIPS.abiflags section data in output.
4202 Mips_abiflags<big_endian>* abiflags_;
4204 unsigned int mach_;
4205 Layout* layout_;
4207 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4209 // Whether there is an input .MIPS.abiflags section.
4210 bool has_abiflags_section_;
4212 // Whether the entry symbol is mips16 or micromips.
4213 bool entry_symbol_is_compressed_;
4215 // Whether we can use only 32-bit microMIPS instructions.
4216 // TODO(sasa): This should be a linker option.
4217 bool insn32_;
4220 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4221 // It records high part of the relocation pair.
4223 template<int size, bool big_endian>
4224 struct reloc_high
4226 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4228 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4229 const Symbol_value<size>* _psymval, Mips_address _addend,
4230 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
4231 Mips_address _address = 0, bool _gp_disp = false)
4232 : view(_view), object(_object), psymval(_psymval), addend(_addend),
4233 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4234 address(_address), gp_disp(_gp_disp)
4237 unsigned char* view;
4238 const Mips_relobj<size, big_endian>* object;
4239 const Symbol_value<size>* psymval;
4240 Mips_address addend;
4241 unsigned int r_type;
4242 unsigned int r_sym;
4243 bool extract_addend;
4244 Mips_address address;
4245 bool gp_disp;
4248 template<int size, bool big_endian>
4249 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4251 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4252 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4253 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4254 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
4255 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
4257 public:
4258 typedef enum
4260 STATUS_OKAY, // No error during relocation.
4261 STATUS_OVERFLOW, // Relocation overflow.
4262 STATUS_BAD_RELOC, // Relocation cannot be applied.
4263 STATUS_PCREL_UNALIGNED // Unaligned PC-relative relocation.
4264 } Status;
4266 private:
4267 typedef Relocate_functions<size, big_endian> Base;
4268 typedef Mips_relocate_functions<size, big_endian> This;
4270 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4271 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4272 static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
4274 template<int valsize>
4275 static inline typename This::Status
4276 check_overflow(Valtype value)
4278 if (size == 32)
4279 return (Bits<valsize>::has_overflow32(value)
4280 ? This::STATUS_OVERFLOW
4281 : This::STATUS_OKAY);
4283 return (Bits<valsize>::has_overflow(value)
4284 ? This::STATUS_OVERFLOW
4285 : This::STATUS_OKAY);
4288 static inline bool
4289 should_shuffle_micromips_reloc(unsigned int r_type)
4291 return (micromips_reloc(r_type)
4292 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4293 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4296 public:
4297 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4298 // Most mips16 instructions are 16 bits, but these instructions
4299 // are 32 bits.
4301 // The format of these instructions is:
4303 // +--------------+--------------------------------+
4304 // | JALX | X| Imm 20:16 | Imm 25:21 |
4305 // +--------------+--------------------------------+
4306 // | Immediate 15:0 |
4307 // +-----------------------------------------------+
4309 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4310 // Note that the immediate value in the first word is swapped.
4312 // When producing a relocatable object file, R_MIPS16_26 is
4313 // handled mostly like R_MIPS_26. In particular, the addend is
4314 // stored as a straight 26-bit value in a 32-bit instruction.
4315 // (gas makes life simpler for itself by never adjusting a
4316 // R_MIPS16_26 reloc to be against a section, so the addend is
4317 // always zero). However, the 32 bit instruction is stored as 2
4318 // 16-bit values, rather than a single 32-bit value. In a
4319 // big-endian file, the result is the same; in a little-endian
4320 // file, the two 16-bit halves of the 32 bit value are swapped.
4321 // This is so that a disassembler can recognize the jal
4322 // instruction.
4324 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4325 // instruction stored as two 16-bit values. The addend A is the
4326 // contents of the targ26 field. The calculation is the same as
4327 // R_MIPS_26. When storing the calculated value, reorder the
4328 // immediate value as shown above, and don't forget to store the
4329 // value as two 16-bit values.
4331 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4332 // defined as
4334 // big-endian:
4335 // +--------+----------------------+
4336 // | | |
4337 // | | targ26-16 |
4338 // |31 26|25 0|
4339 // +--------+----------------------+
4341 // little-endian:
4342 // +----------+------+-------------+
4343 // | | | |
4344 // | sub1 | | sub2 |
4345 // |0 9|10 15|16 31|
4346 // +----------+--------------------+
4347 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4348 // ((sub1 << 16) | sub2)).
4350 // When producing a relocatable object file, the calculation is
4351 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4352 // When producing a fully linked file, the calculation is
4353 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4354 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4356 // The table below lists the other MIPS16 instruction relocations.
4357 // Each one is calculated in the same way as the non-MIPS16 relocation
4358 // given on the right, but using the extended MIPS16 layout of 16-bit
4359 // immediate fields:
4361 // R_MIPS16_GPREL R_MIPS_GPREL16
4362 // R_MIPS16_GOT16 R_MIPS_GOT16
4363 // R_MIPS16_CALL16 R_MIPS_CALL16
4364 // R_MIPS16_HI16 R_MIPS_HI16
4365 // R_MIPS16_LO16 R_MIPS_LO16
4367 // A typical instruction will have a format like this:
4369 // +--------------+--------------------------------+
4370 // | EXTEND | Imm 10:5 | Imm 15:11 |
4371 // +--------------+--------------------------------+
4372 // | Major | rx | ry | Imm 4:0 |
4373 // +--------------+--------------------------------+
4375 // EXTEND is the five bit value 11110. Major is the instruction
4376 // opcode.
4378 // All we need to do here is shuffle the bits appropriately.
4379 // As above, the two 16-bit halves must be swapped on a
4380 // little-endian system.
4382 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4383 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4384 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4386 static void
4387 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4388 bool jal_shuffle)
4390 if (!mips16_reloc(r_type)
4391 && !should_shuffle_micromips_reloc(r_type))
4392 return;
4394 // Pick up the first and second halfwords of the instruction.
4395 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4396 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4397 Valtype32 val;
4399 if (micromips_reloc(r_type)
4400 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4401 val = first << 16 | second;
4402 else if (r_type != elfcpp::R_MIPS16_26)
4403 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4404 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4405 else
4406 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4407 | ((first & 0x1f) << 21) | second);
4409 elfcpp::Swap<32, big_endian>::writeval(view, val);
4412 static void
4413 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4415 if (!mips16_reloc(r_type)
4416 && !should_shuffle_micromips_reloc(r_type))
4417 return;
4419 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4420 Valtype16 first, second;
4422 if (micromips_reloc(r_type)
4423 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4425 second = val & 0xffff;
4426 first = val >> 16;
4428 else if (r_type != elfcpp::R_MIPS16_26)
4430 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4431 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4433 else
4435 second = val & 0xffff;
4436 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4437 | ((val >> 21) & 0x1f);
4440 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4441 elfcpp::Swap<16, big_endian>::writeval(view, first);
4444 // R_MIPS_16: S + sign-extend(A)
4445 static inline typename This::Status
4446 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4447 const Symbol_value<size>* psymval, Mips_address addend_a,
4448 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4450 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4451 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4453 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4454 : addend_a);
4456 Valtype x = psymval->value(object, addend);
4457 val = Bits<16>::bit_select32(val, x, 0xffffU);
4459 if (calculate_only)
4461 *calculated_value = x;
4462 return This::STATUS_OKAY;
4464 else
4465 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4467 return check_overflow<16>(x);
4470 // R_MIPS_32: S + A
4471 static inline typename This::Status
4472 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4473 const Symbol_value<size>* psymval, Mips_address addend_a,
4474 bool extract_addend, bool calculate_only, Valtype* calculated_value)
4476 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4477 Valtype addend = (extract_addend
4478 ? elfcpp::Swap<32, big_endian>::readval(wv)
4479 : addend_a);
4480 Valtype x = psymval->value(object, addend);
4482 if (calculate_only)
4483 *calculated_value = x;
4484 else
4485 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4487 return This::STATUS_OKAY;
4490 // R_MIPS_JALR, R_MICROMIPS_JALR
4491 static inline typename This::Status
4492 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4493 const Symbol_value<size>* psymval, Mips_address address,
4494 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4495 unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4496 bool calculate_only, Valtype* calculated_value)
4498 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4499 Valtype addend = extract_addend ? 0 : addend_a;
4500 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4502 // Try converting J(AL)R to B(AL), if the target is in range.
4503 if (!parameters->options().relocatable()
4504 && r_type == elfcpp::R_MIPS_JALR
4505 && !cross_mode_jump
4506 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4507 || (jr_to_b && val == 0x03200008))) // jr t9
4509 int offset = psymval->value(object, addend) - (address + 4);
4510 if (!Bits<18>::has_overflow32(offset))
4512 if (val == 0x03200008) // jr t9
4513 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4514 else
4515 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4519 if (calculate_only)
4520 *calculated_value = val;
4521 else
4522 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4524 return This::STATUS_OKAY;
4527 // R_MIPS_PC32: S + A - P
4528 static inline typename This::Status
4529 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4530 const Symbol_value<size>* psymval, Mips_address address,
4531 Mips_address addend_a, bool extract_addend, bool calculate_only,
4532 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) - address;
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_26, R_MIPS16_26, R_MICROMIPS_26_S1
4549 static inline typename This::Status
4550 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4551 const Symbol_value<size>* psymval, Mips_address address,
4552 bool local, Mips_address addend_a, bool extract_addend,
4553 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4554 bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
4556 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4557 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4559 Valtype addend;
4560 if (extract_addend)
4562 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4563 addend = (val & 0x03ffffff) << 1;
4564 else
4565 addend = (val & 0x03ffffff) << 2;
4567 else
4568 addend = addend_a;
4570 // Make sure the target of JALX is word-aligned. Bit 0 must be
4571 // the correct ISA mode selector and bit 1 must be 0.
4572 if (!calculate_only && cross_mode_jump
4573 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4575 gold_warning(_("JALX to a non-word-aligned address"));
4576 return This::STATUS_BAD_RELOC;
4579 // Shift is 2, unusually, for microMIPS JALX.
4580 unsigned int shift =
4581 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4583 Valtype x;
4584 if (local)
4585 x = addend | ((address + 4) & (0xfc000000 << shift));
4586 else
4588 if (shift == 1)
4589 x = Bits<27>::sign_extend32(addend);
4590 else
4591 x = Bits<28>::sign_extend32(addend);
4593 x = psymval->value(object, x) >> shift;
4595 if (!calculate_only && !local && !gsym->is_weak_undefined())
4597 if ((x >> 26) != ((address + 4) >> (26 + shift)))
4599 gold_error(_("relocation truncated to fit: %u against '%s'"),
4600 r_type, gsym->name());
4601 return This::STATUS_OVERFLOW;
4605 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4607 // If required, turn JAL into JALX.
4608 if (cross_mode_jump)
4610 bool ok;
4611 Valtype32 opcode = val >> 26;
4612 Valtype32 jalx_opcode;
4614 // Check to see if the opcode is already JAL or JALX.
4615 if (r_type == elfcpp::R_MIPS16_26)
4617 ok = (opcode == 0x6) || (opcode == 0x7);
4618 jalx_opcode = 0x7;
4620 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4622 ok = (opcode == 0x3d) || (opcode == 0x3c);
4623 jalx_opcode = 0x3c;
4625 else
4627 ok = (opcode == 0x3) || (opcode == 0x1d);
4628 jalx_opcode = 0x1d;
4631 // If the opcode is not JAL or JALX, there's a problem. We cannot
4632 // convert J or JALS to JALX.
4633 if (!calculate_only && !ok)
4635 gold_error(_("Unsupported jump between ISA modes; consider "
4636 "recompiling with interlinking enabled."));
4637 return This::STATUS_BAD_RELOC;
4640 // Make this the JALX opcode.
4641 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4644 // Try converting JAL to BAL, if the target is in range.
4645 if (!parameters->options().relocatable()
4646 && !cross_mode_jump
4647 && ((jal_to_bal
4648 && r_type == elfcpp::R_MIPS_26
4649 && (val >> 26) == 0x3))) // jal addr
4651 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4652 int offset = dest - (address + 4);
4653 if (!Bits<18>::has_overflow32(offset))
4655 if (val == 0x03200008) // jr t9
4656 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4657 else
4658 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4662 if (calculate_only)
4663 *calculated_value = val;
4664 else
4665 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4667 return This::STATUS_OKAY;
4670 // R_MIPS_PC16
4671 static inline typename This::Status
4672 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4673 const Symbol_value<size>* psymval, Mips_address address,
4674 Mips_address addend_a, bool extract_addend, bool calculate_only,
4675 Valtype* calculated_value)
4677 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4678 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4680 Valtype addend = (extract_addend
4681 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4682 : addend_a);
4684 Valtype x = psymval->value(object, addend) - address;
4685 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4687 if (calculate_only)
4689 *calculated_value = x >> 2;
4690 return This::STATUS_OKAY;
4692 else
4693 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4695 if (psymval->value(object, addend) & 3)
4696 return This::STATUS_PCREL_UNALIGNED;
4698 return check_overflow<18>(x);
4701 // R_MIPS_PC21_S2
4702 static inline typename This::Status
4703 relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4704 const Symbol_value<size>* psymval, Mips_address address,
4705 Mips_address addend_a, bool extract_addend, bool calculate_only,
4706 Valtype* calculated_value)
4708 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4709 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4711 Valtype addend = (extract_addend
4712 ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4713 : addend_a);
4715 Valtype x = psymval->value(object, addend) - address;
4716 val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4718 if (calculate_only)
4720 *calculated_value = x >> 2;
4721 return This::STATUS_OKAY;
4723 else
4724 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4726 if (psymval->value(object, addend) & 3)
4727 return This::STATUS_PCREL_UNALIGNED;
4729 return check_overflow<23>(x);
4732 // R_MIPS_PC26_S2
4733 static inline typename This::Status
4734 relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4735 const Symbol_value<size>* psymval, Mips_address address,
4736 Mips_address addend_a, bool extract_addend, bool calculate_only,
4737 Valtype* calculated_value)
4739 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4740 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4742 Valtype addend = (extract_addend
4743 ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4744 : addend_a);
4746 Valtype x = psymval->value(object, addend) - address;
4747 val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4749 if (calculate_only)
4751 *calculated_value = x >> 2;
4752 return This::STATUS_OKAY;
4754 else
4755 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4757 if (psymval->value(object, addend) & 3)
4758 return This::STATUS_PCREL_UNALIGNED;
4760 return check_overflow<28>(x);
4763 // R_MIPS_PC18_S3
4764 static inline typename This::Status
4765 relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4766 const Symbol_value<size>* psymval, Mips_address address,
4767 Mips_address addend_a, bool extract_addend, bool calculate_only,
4768 Valtype* calculated_value)
4770 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4771 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4773 Valtype addend = (extract_addend
4774 ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4775 : addend_a);
4777 Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4778 val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4780 if (calculate_only)
4782 *calculated_value = x >> 3;
4783 return This::STATUS_OKAY;
4785 else
4786 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4788 if (psymval->value(object, addend) & 7)
4789 return This::STATUS_PCREL_UNALIGNED;
4791 return check_overflow<21>(x);
4794 // R_MIPS_PC19_S2
4795 static inline typename This::Status
4796 relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4797 const Symbol_value<size>* psymval, Mips_address address,
4798 Mips_address addend_a, bool extract_addend, bool calculate_only,
4799 Valtype* calculated_value)
4801 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4802 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4804 Valtype addend = (extract_addend
4805 ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4806 : addend_a);
4808 Valtype x = psymval->value(object, addend) - address;
4809 val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4811 if (calculate_only)
4813 *calculated_value = x >> 2;
4814 return This::STATUS_OKAY;
4816 else
4817 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4819 if (psymval->value(object, addend) & 3)
4820 return This::STATUS_PCREL_UNALIGNED;
4822 return check_overflow<21>(x);
4825 // R_MIPS_PCHI16
4826 static inline typename This::Status
4827 relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4828 const Symbol_value<size>* psymval, Mips_address addend,
4829 Mips_address address, unsigned int r_sym, bool extract_addend)
4831 // Record the relocation. It will be resolved when we find pclo16 part.
4832 pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4833 addend, 0, r_sym, extract_addend, address));
4834 return This::STATUS_OKAY;
4837 // R_MIPS_PCHI16
4838 static inline typename This::Status
4839 do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4840 const Symbol_value<size>* psymval, Mips_address addend_hi,
4841 Mips_address address, bool extract_addend, Valtype32 addend_lo,
4842 bool calculate_only, Valtype* calculated_value)
4844 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4845 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4847 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4848 : addend_hi);
4850 Valtype value = psymval->value(object, addend) - address;
4851 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4852 val = Bits<32>::bit_select32(val, x, 0xffff);
4854 if (calculate_only)
4855 *calculated_value = x;
4856 else
4857 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4859 return This::STATUS_OKAY;
4862 // R_MIPS_PCLO16
4863 static inline typename This::Status
4864 relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4865 const Symbol_value<size>* psymval, Mips_address addend_a,
4866 bool extract_addend, Mips_address address, unsigned int r_sym,
4867 unsigned int rel_type, bool calculate_only,
4868 Valtype* calculated_value)
4870 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4871 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4873 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4874 : addend_a);
4876 if (rel_type == elfcpp::SHT_REL)
4878 // Resolve pending R_MIPS_PCHI16 relocations.
4879 typename std::list<reloc_high<size, big_endian> >::iterator it =
4880 pchi16_relocs.begin();
4881 while (it != pchi16_relocs.end())
4883 reloc_high<size, big_endian> pchi16 = *it;
4884 if (pchi16.r_sym == r_sym)
4886 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4887 pchi16.addend, pchi16.address,
4888 pchi16.extract_addend, addend, calculate_only,
4889 calculated_value);
4890 it = pchi16_relocs.erase(it);
4892 else
4893 ++it;
4897 // Resolve R_MIPS_PCLO16 relocation.
4898 Valtype x = psymval->value(object, addend) - address;
4899 val = Bits<32>::bit_select32(val, x, 0xffff);
4901 if (calculate_only)
4902 *calculated_value = x;
4903 else
4904 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4906 return This::STATUS_OKAY;
4909 // R_MICROMIPS_PC7_S1
4910 static inline typename This::Status
4911 relmicromips_pc7_s1(unsigned char* view,
4912 const Mips_relobj<size, big_endian>* object,
4913 const Symbol_value<size>* psymval, Mips_address address,
4914 Mips_address addend_a, bool extract_addend,
4915 bool calculate_only, Valtype* calculated_value)
4917 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4918 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4920 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4921 : addend_a;
4923 Valtype x = psymval->value(object, addend) - address;
4924 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4926 if (calculate_only)
4928 *calculated_value = x >> 1;
4929 return This::STATUS_OKAY;
4931 else
4932 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4934 return check_overflow<8>(x);
4937 // R_MICROMIPS_PC10_S1
4938 static inline typename This::Status
4939 relmicromips_pc10_s1(unsigned char* view,
4940 const Mips_relobj<size, big_endian>* object,
4941 const Symbol_value<size>* psymval, Mips_address address,
4942 Mips_address addend_a, bool extract_addend,
4943 bool calculate_only, Valtype* calculated_value)
4945 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4946 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4948 Valtype addend = (extract_addend
4949 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4950 : addend_a);
4952 Valtype x = psymval->value(object, addend) - address;
4953 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
4955 if (calculate_only)
4957 *calculated_value = x >> 1;
4958 return This::STATUS_OKAY;
4960 else
4961 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4963 return check_overflow<11>(x);
4966 // R_MICROMIPS_PC16_S1
4967 static inline typename This::Status
4968 relmicromips_pc16_s1(unsigned char* view,
4969 const Mips_relobj<size, big_endian>* object,
4970 const Symbol_value<size>* psymval, Mips_address address,
4971 Mips_address addend_a, bool extract_addend,
4972 bool calculate_only, Valtype* calculated_value)
4974 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4975 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4977 Valtype addend = (extract_addend
4978 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
4979 : addend_a);
4981 Valtype x = psymval->value(object, addend) - address;
4982 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
4984 if (calculate_only)
4986 *calculated_value = x >> 1;
4987 return This::STATUS_OKAY;
4989 else
4990 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4992 return check_overflow<17>(x);
4995 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4996 static inline typename This::Status
4997 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4998 const Symbol_value<size>* psymval, Mips_address addend,
4999 Mips_address address, bool gp_disp, unsigned int r_type,
5000 unsigned int r_sym, bool extract_addend)
5002 // Record the relocation. It will be resolved when we find lo16 part.
5003 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5004 addend, r_type, r_sym, extract_addend, address,
5005 gp_disp));
5006 return This::STATUS_OKAY;
5009 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5010 static inline typename This::Status
5011 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5012 const Symbol_value<size>* psymval, Mips_address addend_hi,
5013 Mips_address address, bool is_gp_disp, unsigned int r_type,
5014 bool extract_addend, Valtype32 addend_lo,
5015 Target_mips<size, big_endian>* target, bool calculate_only,
5016 Valtype* calculated_value)
5018 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5019 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5021 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5022 : addend_hi);
5024 Valtype32 value;
5025 if (!is_gp_disp)
5026 value = psymval->value(object, addend);
5027 else
5029 // For MIPS16 ABI code we generate this sequence
5030 // 0: li $v0,%hi(_gp_disp)
5031 // 4: addiupc $v1,%lo(_gp_disp)
5032 // 8: sll $v0,16
5033 // 12: addu $v0,$v1
5034 // 14: move $gp,$v0
5035 // So the offsets of hi and lo relocs are the same, but the
5036 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5037 // ADDIUPC clears the low two bits of the instruction address,
5038 // so the base is ($t9 + 4) & ~3.
5039 Valtype32 gp_disp;
5040 if (r_type == elfcpp::R_MIPS16_HI16)
5041 gp_disp = (target->adjusted_gp_value(object)
5042 - ((address + 4) & ~0x3));
5043 // The microMIPS .cpload sequence uses the same assembly
5044 // instructions as the traditional psABI version, but the
5045 // incoming $t9 has the low bit set.
5046 else if (r_type == elfcpp::R_MICROMIPS_HI16)
5047 gp_disp = target->adjusted_gp_value(object) - address - 1;
5048 else
5049 gp_disp = target->adjusted_gp_value(object) - address;
5050 value = gp_disp + addend;
5052 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
5053 val = Bits<32>::bit_select32(val, x, 0xffff);
5055 if (calculate_only)
5057 *calculated_value = x;
5058 return This::STATUS_OKAY;
5060 else
5061 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5063 return (is_gp_disp ? check_overflow<16>(x)
5064 : This::STATUS_OKAY);
5067 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5068 static inline typename This::Status
5069 relgot16_local(unsigned char* view,
5070 const Mips_relobj<size, big_endian>* object,
5071 const Symbol_value<size>* psymval, Mips_address addend_a,
5072 bool extract_addend, unsigned int r_type, unsigned int r_sym)
5074 // Record the relocation. It will be resolved when we find lo16 part.
5075 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
5076 addend_a, r_type, r_sym, extract_addend));
5077 return This::STATUS_OKAY;
5080 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5081 static inline typename This::Status
5082 do_relgot16_local(unsigned char* view,
5083 const Mips_relobj<size, big_endian>* object,
5084 const Symbol_value<size>* psymval, Mips_address addend_hi,
5085 bool extract_addend, Valtype32 addend_lo,
5086 Target_mips<size, big_endian>* target, bool calculate_only,
5087 Valtype* calculated_value)
5089 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5090 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5092 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
5093 : addend_hi);
5095 // Find GOT page entry.
5096 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5097 & 0xffff;
5098 value <<= 16;
5099 unsigned int got_offset =
5100 target->got_section()->get_got_page_offset(value, object);
5102 // Resolve the relocation.
5103 Valtype x = target->got_section()->gp_offset(got_offset, object);
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 check_overflow<16>(x);
5117 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5118 static inline typename This::Status
5119 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
5120 const Mips_relobj<size, big_endian>* object,
5121 const Symbol_value<size>* psymval, Mips_address addend_a,
5122 bool extract_addend, Mips_address address, bool is_gp_disp,
5123 unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
5124 bool calculate_only, Valtype* calculated_value)
5126 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5127 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5129 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5130 : addend_a);
5132 if (rel_type == elfcpp::SHT_REL)
5134 typename This::Status reloc_status = This::STATUS_OKAY;
5135 // Resolve pending R_MIPS_HI16 relocations.
5136 typename std::list<reloc_high<size, big_endian> >::iterator it =
5137 hi16_relocs.begin();
5138 while (it != hi16_relocs.end())
5140 reloc_high<size, big_endian> hi16 = *it;
5141 if (hi16.r_sym == r_sym
5142 && is_matching_lo16_reloc(hi16.r_type, r_type))
5144 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
5145 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
5146 hi16.addend, hi16.address, hi16.gp_disp,
5147 hi16.r_type, hi16.extract_addend, addend,
5148 target, calculate_only, calculated_value);
5149 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
5150 if (reloc_status == This::STATUS_OVERFLOW)
5151 return This::STATUS_OVERFLOW;
5152 it = hi16_relocs.erase(it);
5154 else
5155 ++it;
5158 // Resolve pending local R_MIPS_GOT16 relocations.
5159 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
5160 got16_relocs.begin();
5161 while (it2 != got16_relocs.end())
5163 reloc_high<size, big_endian> got16 = *it2;
5164 if (got16.r_sym == r_sym
5165 && is_matching_lo16_reloc(got16.r_type, r_type))
5167 mips_reloc_unshuffle(got16.view, got16.r_type, false);
5169 reloc_status = do_relgot16_local(got16.view, got16.object,
5170 got16.psymval, got16.addend,
5171 got16.extract_addend, addend, target,
5172 calculate_only, calculated_value);
5174 mips_reloc_shuffle(got16.view, got16.r_type, false);
5175 if (reloc_status == This::STATUS_OVERFLOW)
5176 return This::STATUS_OVERFLOW;
5177 it2 = got16_relocs.erase(it2);
5179 else
5180 ++it2;
5184 // Resolve R_MIPS_LO16 relocation.
5185 Valtype x;
5186 if (!is_gp_disp)
5187 x = psymval->value(object, addend);
5188 else
5190 // See the comment for R_MIPS16_HI16 above for the reason
5191 // for this conditional.
5192 Valtype32 gp_disp;
5193 if (r_type == elfcpp::R_MIPS16_LO16)
5194 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
5195 else if (r_type == elfcpp::R_MICROMIPS_LO16
5196 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
5197 gp_disp = target->adjusted_gp_value(object) - address + 3;
5198 else
5199 gp_disp = target->adjusted_gp_value(object) - address + 4;
5200 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5201 // for overflow. Relocations against _gp_disp are normally
5202 // generated from the .cpload pseudo-op. It generates code
5203 // that normally looks like this:
5205 // lui $gp,%hi(_gp_disp)
5206 // addiu $gp,$gp,%lo(_gp_disp)
5207 // addu $gp,$gp,$t9
5209 // Here $t9 holds the address of the function being called,
5210 // as required by the MIPS ELF ABI. The R_MIPS_LO16
5211 // relocation can easily overflow in this situation, but the
5212 // R_MIPS_HI16 relocation will handle the overflow.
5213 // Therefore, we consider this a bug in the MIPS ABI, and do
5214 // not check for overflow here.
5215 x = gp_disp + addend;
5217 val = Bits<32>::bit_select32(val, x, 0xffff);
5219 if (calculate_only)
5220 *calculated_value = x;
5221 else
5222 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5224 return This::STATUS_OKAY;
5227 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5228 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5229 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5230 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5231 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5232 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5233 static inline typename This::Status
5234 relgot(unsigned char* view, int gp_offset, bool calculate_only,
5235 Valtype* calculated_value)
5237 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5238 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5239 Valtype x = gp_offset;
5240 val = Bits<32>::bit_select32(val, x, 0xffff);
5242 if (calculate_only)
5244 *calculated_value = x;
5245 return This::STATUS_OKAY;
5247 else
5248 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5250 return check_overflow<16>(x);
5253 // R_MIPS_EH
5254 static inline typename This::Status
5255 releh(unsigned char* view, int gp_offset, bool calculate_only,
5256 Valtype* calculated_value)
5258 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5259 Valtype x = gp_offset;
5261 if (calculate_only)
5263 *calculated_value = x;
5264 return This::STATUS_OKAY;
5266 else
5267 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5269 return check_overflow<32>(x);
5272 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5273 static inline typename This::Status
5274 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5275 const Mips_relobj<size, big_endian>* object,
5276 const Symbol_value<size>* psymval, Mips_address addend_a,
5277 bool extract_addend, bool calculate_only,
5278 Valtype* calculated_value)
5280 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5281 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5282 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5284 // Find a GOT page entry that points to within 32KB of symbol + addend.
5285 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5286 unsigned int got_offset =
5287 target->got_section()->get_got_page_offset(value, object);
5289 Valtype x = target->got_section()->gp_offset(got_offset, object);
5290 val = Bits<32>::bit_select32(val, x, 0xffff);
5292 if (calculate_only)
5294 *calculated_value = x;
5295 return This::STATUS_OKAY;
5297 else
5298 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5300 return check_overflow<16>(x);
5303 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5304 static inline typename This::Status
5305 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5306 const Mips_relobj<size, big_endian>* object,
5307 const Symbol_value<size>* psymval, Mips_address addend_a,
5308 bool extract_addend, bool local, bool calculate_only,
5309 Valtype* calculated_value)
5311 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5312 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
5313 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5315 // For a local symbol, find a GOT page entry that points to within 32KB of
5316 // symbol + addend. Relocation value is the offset of the GOT page entry's
5317 // value from symbol + addend.
5318 // For a global symbol, relocation value is addend.
5319 Valtype x;
5320 if (local)
5322 // Find GOT page entry.
5323 Mips_address value = ((psymval->value(object, addend) + 0x8000)
5324 & ~0xffff);
5325 target->got_section()->get_got_page_offset(value, object);
5327 x = psymval->value(object, addend) - value;
5329 else
5330 x = addend;
5331 val = Bits<32>::bit_select32(val, x, 0xffff);
5333 if (calculate_only)
5335 *calculated_value = x;
5336 return This::STATUS_OKAY;
5338 else
5339 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5341 return check_overflow<16>(x);
5344 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5345 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5346 static inline typename This::Status
5347 relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5348 Valtype* calculated_value)
5350 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5351 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5352 Valtype x = gp_offset;
5353 x = ((x + 0x8000) >> 16) & 0xffff;
5354 val = Bits<32>::bit_select32(val, x, 0xffff);
5356 if (calculate_only)
5357 *calculated_value = x;
5358 else
5359 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5361 return This::STATUS_OKAY;
5364 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5365 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5366 static inline typename This::Status
5367 relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5368 Valtype* calculated_value)
5370 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5371 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5372 Valtype x = gp_offset;
5373 val = Bits<32>::bit_select32(val, x, 0xffff);
5375 if (calculate_only)
5376 *calculated_value = x;
5377 else
5378 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5380 return This::STATUS_OKAY;
5383 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5384 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5385 static inline typename This::Status
5386 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5387 const Symbol_value<size>* psymval, Mips_address gp,
5388 Mips_address addend_a, bool extract_addend, bool local,
5389 unsigned int r_type, bool calculate_only,
5390 Valtype* calculated_value)
5392 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5393 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5395 Valtype addend;
5396 if (extract_addend)
5398 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5399 addend = (val & 0x7f) << 2;
5400 else
5401 addend = val & 0xffff;
5402 // Only sign-extend the addend if it was extracted from the
5403 // instruction. If the addend was separate, leave it alone,
5404 // otherwise we may lose significant bits.
5405 addend = Bits<16>::sign_extend32(addend);
5407 else
5408 addend = addend_a;
5410 Valtype x = psymval->value(object, addend) - gp;
5412 // If the symbol was local, any earlier relocatable links will
5413 // have adjusted its addend with the gp offset, so compensate
5414 // for that now. Don't do it for symbols forced local in this
5415 // link, though, since they won't have had the gp offset applied
5416 // to them before.
5417 if (local)
5418 x += object->gp_value();
5420 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5421 val = Bits<32>::bit_select32(val, x, 0x7f);
5422 else
5423 val = Bits<32>::bit_select32(val, x, 0xffff);
5425 if (calculate_only)
5427 *calculated_value = x;
5428 return This::STATUS_OKAY;
5430 else
5431 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5433 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
5435 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5436 "limit (see option -G)"));
5437 return This::STATUS_OVERFLOW;
5439 return This::STATUS_OKAY;
5442 // R_MIPS_GPREL32
5443 static inline typename This::Status
5444 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5445 const Symbol_value<size>* psymval, Mips_address gp,
5446 Mips_address addend_a, bool extract_addend, bool calculate_only,
5447 Valtype* calculated_value)
5449 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5450 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5451 Valtype addend = extract_addend ? val : addend_a;
5453 // R_MIPS_GPREL32 relocations are defined for local symbols only.
5454 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5456 if (calculate_only)
5457 *calculated_value = x;
5458 else
5459 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5461 return This::STATUS_OKAY;
5464 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5465 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5466 // R_MICROMIPS_TLS_DTPREL_HI16
5467 static inline typename This::Status
5468 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5469 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5470 Mips_address addend_a, bool extract_addend, bool calculate_only,
5471 Valtype* calculated_value)
5473 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5474 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5475 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5477 // tls symbol values are relative to tls_segment()->vaddr()
5478 Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
5479 val = Bits<32>::bit_select32(val, x, 0xffff);
5481 if (calculate_only)
5482 *calculated_value = x;
5483 else
5484 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5486 return This::STATUS_OKAY;
5489 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5490 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5491 // R_MICROMIPS_TLS_DTPREL_LO16,
5492 static inline typename This::Status
5493 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5494 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5495 Mips_address addend_a, bool extract_addend, bool calculate_only,
5496 Valtype* calculated_value)
5498 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5499 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5500 Valtype addend = extract_addend ? val & 0xffff : addend_a;
5502 // tls symbol values are relative to tls_segment()->vaddr()
5503 Valtype x = psymval->value(object, addend) - tp_offset;
5504 val = Bits<32>::bit_select32(val, x, 0xffff);
5506 if (calculate_only)
5507 *calculated_value = x;
5508 else
5509 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5511 return This::STATUS_OKAY;
5514 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5515 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5516 static inline typename This::Status
5517 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5518 const Symbol_value<size>* psymval, Valtype32 tp_offset,
5519 Mips_address addend_a, bool extract_addend, bool calculate_only,
5520 Valtype* calculated_value)
5522 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5523 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5524 Valtype addend = extract_addend ? val : addend_a;
5526 // tls symbol values are relative to tls_segment()->vaddr()
5527 Valtype x = psymval->value(object, addend) - tp_offset;
5529 if (calculate_only)
5530 *calculated_value = x;
5531 else
5532 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5534 return This::STATUS_OKAY;
5537 // R_MIPS_SUB, R_MICROMIPS_SUB
5538 static inline typename This::Status
5539 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5540 const Symbol_value<size>* psymval, Mips_address addend_a,
5541 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5543 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5544 Valtype64 addend = (extract_addend
5545 ? elfcpp::Swap<64, big_endian>::readval(wv)
5546 : addend_a);
5548 Valtype64 x = psymval->value(object, -addend);
5549 if (calculate_only)
5550 *calculated_value = x;
5551 else
5552 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5554 return This::STATUS_OKAY;
5557 // R_MIPS_64: S + A
5558 static inline typename This::Status
5559 rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5560 const Symbol_value<size>* psymval, Mips_address addend_a,
5561 bool extract_addend, bool calculate_only, Valtype* calculated_value,
5562 bool apply_addend_only)
5564 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5565 Valtype64 addend = (extract_addend
5566 ? elfcpp::Swap<64, big_endian>::readval(wv)
5567 : addend_a);
5569 Valtype64 x = psymval->value(object, addend);
5570 if (calculate_only)
5571 *calculated_value = x;
5572 else
5574 if (apply_addend_only)
5575 x = addend;
5576 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5579 return This::STATUS_OKAY;
5584 template<int size, bool big_endian>
5585 typename std::list<reloc_high<size, big_endian> >
5586 Mips_relocate_functions<size, big_endian>::hi16_relocs;
5588 template<int size, bool big_endian>
5589 typename std::list<reloc_high<size, big_endian> >
5590 Mips_relocate_functions<size, big_endian>::got16_relocs;
5592 template<int size, bool big_endian>
5593 typename std::list<reloc_high<size, big_endian> >
5594 Mips_relocate_functions<size, big_endian>::pchi16_relocs;
5596 // Mips_got_info methods.
5598 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5599 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5601 template<int size, bool big_endian>
5602 void
5603 Mips_got_info<size, big_endian>::record_local_got_symbol(
5604 Mips_relobj<size, big_endian>* object, unsigned int symndx,
5605 Mips_address addend, unsigned int r_type, unsigned int shndx,
5606 bool is_section_symbol)
5608 Mips_got_entry<size, big_endian>* entry =
5609 new Mips_got_entry<size, big_endian>(object, symndx, addend,
5610 mips_elf_reloc_tls_type(r_type),
5611 shndx, is_section_symbol);
5612 this->record_got_entry(entry, object);
5615 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5616 // in OBJECT. FOR_CALL is true if the caller is only interested in
5617 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5618 // relocation.
5620 template<int size, bool big_endian>
5621 void
5622 Mips_got_info<size, big_endian>::record_global_got_symbol(
5623 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5624 unsigned int r_type, bool dyn_reloc, bool for_call)
5626 if (!for_call)
5627 mips_sym->set_got_not_only_for_calls();
5629 // A global symbol in the GOT must also be in the dynamic symbol table.
5630 if (!mips_sym->needs_dynsym_entry())
5632 switch (mips_sym->visibility())
5634 case elfcpp::STV_INTERNAL:
5635 case elfcpp::STV_HIDDEN:
5636 mips_sym->set_is_forced_local();
5637 break;
5638 default:
5639 mips_sym->set_needs_dynsym_entry();
5640 break;
5644 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5645 if (tls_type == GOT_TLS_NONE)
5646 this->global_got_symbols_.insert(mips_sym);
5648 if (dyn_reloc)
5650 if (mips_sym->global_got_area() == GGA_NONE)
5651 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5652 return;
5655 Mips_got_entry<size, big_endian>* entry =
5656 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
5658 this->record_got_entry(entry, object);
5661 // Add ENTRY to master GOT and to OBJECT's GOT.
5663 template<int size, bool big_endian>
5664 void
5665 Mips_got_info<size, big_endian>::record_got_entry(
5666 Mips_got_entry<size, big_endian>* entry,
5667 Mips_relobj<size, big_endian>* object)
5669 this->got_entries_.insert(entry);
5671 // Create the GOT entry for the OBJECT's GOT.
5672 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5673 Mips_got_entry<size, big_endian>* entry2 =
5674 new Mips_got_entry<size, big_endian>(*entry);
5676 g->got_entries_.insert(entry2);
5679 // Record that OBJECT has a page relocation against symbol SYMNDX and
5680 // that ADDEND is the addend for that relocation.
5681 // This function creates an upper bound on the number of GOT slots
5682 // required; no attempt is made to combine references to non-overridable
5683 // global symbols across multiple input files.
5685 template<int size, bool big_endian>
5686 void
5687 Mips_got_info<size, big_endian>::record_got_page_entry(
5688 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5690 struct Got_page_range **range_ptr, *range;
5691 int old_pages, new_pages;
5693 // Find the Got_page_entry for this symbol.
5694 Got_page_entry* entry = new Got_page_entry(object, symndx);
5695 typename Got_page_entry_set::iterator it =
5696 this->got_page_entries_.find(entry);
5697 if (it != this->got_page_entries_.end())
5698 entry = *it;
5699 else
5700 this->got_page_entries_.insert(entry);
5702 // Add the same entry to the OBJECT's GOT.
5703 Got_page_entry* entry2 = NULL;
5704 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5705 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5707 entry2 = new Got_page_entry(*entry);
5708 g2->got_page_entries_.insert(entry2);
5711 // Skip over ranges whose maximum extent cannot share a page entry
5712 // with ADDEND.
5713 range_ptr = &entry->ranges;
5714 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5715 range_ptr = &(*range_ptr)->next;
5717 // If we scanned to the end of the list, or found a range whose
5718 // minimum extent cannot share a page entry with ADDEND, create
5719 // a new singleton range.
5720 range = *range_ptr;
5721 if (!range || addend < range->min_addend - 0xffff)
5723 range = new Got_page_range();
5724 range->next = *range_ptr;
5725 range->min_addend = addend;
5726 range->max_addend = addend;
5728 *range_ptr = range;
5729 ++entry->num_pages;
5730 if (entry2 != NULL)
5731 ++entry2->num_pages;
5732 ++this->page_gotno_;
5733 ++g2->page_gotno_;
5734 return;
5737 // Remember how many pages the old range contributed.
5738 old_pages = range->get_max_pages();
5740 // Update the ranges.
5741 if (addend < range->min_addend)
5742 range->min_addend = addend;
5743 else if (addend > range->max_addend)
5745 if (range->next && addend >= range->next->min_addend - 0xffff)
5747 old_pages += range->next->get_max_pages();
5748 range->max_addend = range->next->max_addend;
5749 range->next = range->next->next;
5751 else
5752 range->max_addend = addend;
5755 // Record any change in the total estimate.
5756 new_pages = range->get_max_pages();
5757 if (old_pages != new_pages)
5759 entry->num_pages += new_pages - old_pages;
5760 if (entry2 != NULL)
5761 entry2->num_pages += new_pages - old_pages;
5762 this->page_gotno_ += new_pages - old_pages;
5763 g2->page_gotno_ += new_pages - old_pages;
5767 // Create all entries that should be in the local part of the GOT.
5769 template<int size, bool big_endian>
5770 void
5771 Mips_got_info<size, big_endian>::add_local_entries(
5772 Target_mips<size, big_endian>* target, Layout* layout)
5774 Mips_output_data_got<size, big_endian>* got = target->got_section();
5775 // First two GOT entries are reserved. The first entry will be filled at
5776 // runtime. The second entry will be used by some runtime loaders.
5777 got->add_constant(0);
5778 got->add_constant(target->mips_elf_gnu_got1_mask());
5780 for (typename Got_entry_set::iterator
5781 p = this->got_entries_.begin();
5782 p != this->got_entries_.end();
5783 ++p)
5785 Mips_got_entry<size, big_endian>* entry = *p;
5786 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5788 got->add_local(entry->object(), entry->symndx(),
5789 GOT_TYPE_STANDARD, entry->addend());
5790 unsigned int got_offset = entry->object()->local_got_offset(
5791 entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
5792 if (got->multi_got() && this->index_ > 0
5793 && parameters->options().output_is_position_independent())
5795 if (!entry->is_section_symbol())
5796 target->rel_dyn_section(layout)->add_local(entry->object(),
5797 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5798 else
5799 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5800 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5801 got, got_offset);
5806 this->add_page_entries(target, layout);
5808 // Add global entries that should be in the local area.
5809 for (typename Got_entry_set::iterator
5810 p = this->got_entries_.begin();
5811 p != this->got_entries_.end();
5812 ++p)
5814 Mips_got_entry<size, big_endian>* entry = *p;
5815 if (!entry->is_for_global_symbol())
5816 continue;
5818 Mips_symbol<size>* mips_sym = entry->sym();
5819 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5821 unsigned int got_type;
5822 if (!got->multi_got())
5823 got_type = GOT_TYPE_STANDARD;
5824 else
5825 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5826 if (got->add_global(mips_sym, got_type))
5828 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5829 if (got->multi_got() && this->index_ > 0
5830 && parameters->options().output_is_position_independent())
5831 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5832 mips_sym, elfcpp::R_MIPS_REL32, got,
5833 mips_sym->got_offset(got_type));
5839 // Create GOT page entries.
5841 template<int size, bool big_endian>
5842 void
5843 Mips_got_info<size, big_endian>::add_page_entries(
5844 Target_mips<size, big_endian>* target, Layout* layout)
5846 if (this->page_gotno_ == 0)
5847 return;
5849 Mips_output_data_got<size, big_endian>* got = target->got_section();
5850 this->got_page_offset_start_ = got->add_constant(0);
5851 if (got->multi_got() && this->index_ > 0
5852 && parameters->options().output_is_position_independent())
5853 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5854 this->got_page_offset_start_);
5855 int num_entries = this->page_gotno_;
5856 unsigned int prev_offset = this->got_page_offset_start_;
5857 while (--num_entries > 0)
5859 unsigned int next_offset = got->add_constant(0);
5860 if (got->multi_got() && this->index_ > 0
5861 && parameters->options().output_is_position_independent())
5862 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5863 next_offset);
5864 gold_assert(next_offset == prev_offset + size/8);
5865 prev_offset = next_offset;
5867 this->got_page_offset_next_ = this->got_page_offset_start_;
5870 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5872 template<int size, bool big_endian>
5873 void
5874 Mips_got_info<size, big_endian>::add_global_entries(
5875 Target_mips<size, big_endian>* target, Layout* layout,
5876 unsigned int non_reloc_only_global_gotno)
5878 Mips_output_data_got<size, big_endian>* got = target->got_section();
5879 // Add GGA_NORMAL entries.
5880 unsigned int count = 0;
5881 for (typename Got_entry_set::iterator
5882 p = this->got_entries_.begin();
5883 p != this->got_entries_.end();
5884 ++p)
5886 Mips_got_entry<size, big_endian>* entry = *p;
5887 if (!entry->is_for_global_symbol())
5888 continue;
5890 Mips_symbol<size>* mips_sym = entry->sym();
5891 if (mips_sym->global_got_area() != GGA_NORMAL)
5892 continue;
5894 unsigned int got_type;
5895 if (!got->multi_got())
5896 got_type = GOT_TYPE_STANDARD;
5897 else
5898 // In multi-GOT links, global symbol can be in both primary and
5899 // secondary GOT(s). By creating custom GOT type
5900 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5901 // is added to secondary GOT(s).
5902 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5903 if (!got->add_global(mips_sym, got_type))
5904 continue;
5906 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5907 if (got->multi_got() && this->index_ == 0)
5908 count++;
5909 if (got->multi_got() && this->index_ > 0)
5911 if (parameters->options().output_is_position_independent()
5912 || (!parameters->doing_static_link()
5913 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
5915 target->rel_dyn_section(layout)->add_global(
5916 mips_sym, elfcpp::R_MIPS_REL32, got,
5917 mips_sym->got_offset(got_type));
5918 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
5919 elfcpp::R_MIPS_REL32, mips_sym);
5924 if (!got->multi_got() || this->index_ == 0)
5926 if (got->multi_got())
5928 // We need to allocate space in the primary GOT for GGA_NORMAL entries
5929 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
5930 // entries correspond to dynamic symbol indexes.
5931 while (count < non_reloc_only_global_gotno)
5933 got->add_constant(0);
5934 ++count;
5938 // Add GGA_RELOC_ONLY entries.
5939 got->add_reloc_only_entries();
5943 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
5945 template<int size, bool big_endian>
5946 void
5947 Mips_got_info<size, big_endian>::add_reloc_only_entries(
5948 Mips_output_data_got<size, big_endian>* got)
5950 for (typename Global_got_entry_set::iterator
5951 p = this->global_got_symbols_.begin();
5952 p != this->global_got_symbols_.end();
5953 ++p)
5955 Mips_symbol<size>* mips_sym = *p;
5956 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
5958 unsigned int got_type;
5959 if (!got->multi_got())
5960 got_type = GOT_TYPE_STANDARD;
5961 else
5962 got_type = GOT_TYPE_STANDARD_MULTIGOT;
5963 if (got->add_global(mips_sym, got_type))
5964 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5969 // Create TLS GOT entries.
5971 template<int size, bool big_endian>
5972 void
5973 Mips_got_info<size, big_endian>::add_tls_entries(
5974 Target_mips<size, big_endian>* target, Layout* layout)
5976 Mips_output_data_got<size, big_endian>* got = target->got_section();
5977 // Add local tls entries.
5978 for (typename Got_entry_set::iterator
5979 p = this->got_entries_.begin();
5980 p != this->got_entries_.end();
5981 ++p)
5983 Mips_got_entry<size, big_endian>* entry = *p;
5984 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
5985 continue;
5987 if (entry->tls_type() == GOT_TLS_GD)
5989 unsigned int got_type = GOT_TYPE_TLS_PAIR;
5990 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5991 : elfcpp::R_MIPS_TLS_DTPMOD64);
5992 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5993 : elfcpp::R_MIPS_TLS_DTPREL64);
5995 if (!parameters->doing_static_link())
5997 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
5998 entry->shndx(), got_type,
5999 target->rel_dyn_section(layout),
6000 r_type1, entry->addend());
6001 unsigned int got_offset =
6002 entry->object()->local_got_offset(entry->symndx(), got_type,
6003 entry->addend());
6004 got->add_static_reloc(got_offset + size/8, r_type2,
6005 entry->object(), entry->symndx());
6007 else
6009 // We are doing a static link. Mark it as belong to module 1,
6010 // the executable.
6011 unsigned int got_offset = got->add_constant(1);
6012 entry->object()->set_local_got_offset(entry->symndx(), got_type,
6013 got_offset,
6014 entry->addend());
6015 got->add_constant(0);
6016 got->add_static_reloc(got_offset + size/8, r_type2,
6017 entry->object(), entry->symndx());
6020 else if (entry->tls_type() == GOT_TLS_IE)
6022 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6023 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6024 : elfcpp::R_MIPS_TLS_TPREL64);
6025 if (!parameters->doing_static_link())
6026 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
6027 target->rel_dyn_section(layout), r_type,
6028 entry->addend());
6029 else
6031 got->add_local(entry->object(), entry->symndx(), got_type,
6032 entry->addend());
6033 unsigned int got_offset =
6034 entry->object()->local_got_offset(entry->symndx(), got_type,
6035 entry->addend());
6036 got->add_static_reloc(got_offset, r_type, entry->object(),
6037 entry->symndx());
6040 else if (entry->tls_type() == GOT_TLS_LDM)
6042 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6043 : elfcpp::R_MIPS_TLS_DTPMOD64);
6044 unsigned int got_offset;
6045 if (!parameters->doing_static_link())
6047 got_offset = got->add_constant(0);
6048 target->rel_dyn_section(layout)->add_local(
6049 entry->object(), 0, r_type, got, got_offset);
6051 else
6052 // We are doing a static link. Just mark it as belong to module 1,
6053 // the executable.
6054 got_offset = got->add_constant(1);
6056 got->add_constant(0);
6057 got->set_tls_ldm_offset(got_offset, entry->object());
6059 else
6060 gold_unreachable();
6063 // Add global tls entries.
6064 for (typename Got_entry_set::iterator
6065 p = this->got_entries_.begin();
6066 p != this->got_entries_.end();
6067 ++p)
6069 Mips_got_entry<size, big_endian>* entry = *p;
6070 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6071 continue;
6073 Mips_symbol<size>* mips_sym = entry->sym();
6074 if (entry->tls_type() == GOT_TLS_GD)
6076 unsigned int got_type;
6077 if (!got->multi_got())
6078 got_type = GOT_TYPE_TLS_PAIR;
6079 else
6080 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6081 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6082 : elfcpp::R_MIPS_TLS_DTPMOD64);
6083 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6084 : elfcpp::R_MIPS_TLS_DTPREL64);
6085 if (!parameters->doing_static_link())
6086 got->add_global_pair_with_rel(mips_sym, got_type,
6087 target->rel_dyn_section(layout), r_type1, r_type2);
6088 else
6090 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
6091 // GOT entries. The first one is initialized to be 1, which is the
6092 // module index for the main executable and the second one 0. A
6093 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6094 // the second GOT entry and will be applied by gold.
6095 unsigned int got_offset = got->add_constant(1);
6096 mips_sym->set_got_offset(got_type, got_offset);
6097 got->add_constant(0);
6098 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6101 else if (entry->tls_type() == GOT_TLS_IE)
6103 unsigned int got_type;
6104 if (!got->multi_got())
6105 got_type = GOT_TYPE_TLS_OFFSET;
6106 else
6107 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
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_global_with_rel(mips_sym, got_type,
6112 target->rel_dyn_section(layout), r_type);
6113 else
6115 got->add_global(mips_sym, got_type);
6116 unsigned int got_offset = mips_sym->got_offset(got_type);
6117 got->add_static_reloc(got_offset, r_type, mips_sym);
6120 else
6121 gold_unreachable();
6125 // Decide whether the symbol needs an entry in the global part of the primary
6126 // GOT, setting global_got_area accordingly. Count the number of global
6127 // symbols that are in the primary GOT only because they have dynamic
6128 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6130 template<int size, bool big_endian>
6131 void
6132 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6134 for (typename Global_got_entry_set::iterator
6135 p = this->global_got_symbols_.begin();
6136 p != this->global_got_symbols_.end();
6137 ++p)
6139 Mips_symbol<size>* sym = *p;
6140 // Make a final decision about whether the symbol belongs in the
6141 // local or global GOT. Symbols that bind locally can (and in the
6142 // case of forced-local symbols, must) live in the local GOT.
6143 // Those that are aren't in the dynamic symbol table must also
6144 // live in the local GOT.
6146 if (!sym->should_add_dynsym_entry(symtab)
6147 || (sym->got_only_for_calls()
6148 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6149 : symbol_references_local(sym,
6150 sym->should_add_dynsym_entry(symtab))))
6151 // The symbol belongs in the local GOT. We no longer need this
6152 // entry if it was only used for relocations; those relocations
6153 // will be against the null or section symbol instead.
6154 sym->set_global_got_area(GGA_NONE);
6155 else if (sym->global_got_area() == GGA_RELOC_ONLY)
6157 ++this->reloc_only_gotno_;
6158 ++this->global_gotno_ ;
6163 // Return the offset of GOT page entry for VALUE. Initialize the entry with
6164 // VALUE if it is not initialized.
6166 template<int size, bool big_endian>
6167 unsigned int
6168 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6169 Mips_output_data_got<size, big_endian>* got)
6171 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6172 if (it != this->got_page_offsets_.end())
6173 return it->second;
6175 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6176 + (size/8) * this->page_gotno_);
6178 unsigned int got_offset = this->got_page_offset_next_;
6179 this->got_page_offsets_[value] = got_offset;
6180 this->got_page_offset_next_ += size/8;
6181 got->update_got_entry(got_offset, value);
6182 return got_offset;
6185 // Remove lazy-binding stubs for global symbols in this GOT.
6187 template<int size, bool big_endian>
6188 void
6189 Mips_got_info<size, big_endian>::remove_lazy_stubs(
6190 Target_mips<size, big_endian>* target)
6192 for (typename Got_entry_set::iterator
6193 p = this->got_entries_.begin();
6194 p != this->got_entries_.end();
6195 ++p)
6197 Mips_got_entry<size, big_endian>* entry = *p;
6198 if (entry->is_for_global_symbol())
6199 target->remove_lazy_stub_entry(entry->sym());
6203 // Count the number of GOT entries required.
6205 template<int size, bool big_endian>
6206 void
6207 Mips_got_info<size, big_endian>::count_got_entries()
6209 for (typename Got_entry_set::iterator
6210 p = this->got_entries_.begin();
6211 p != this->got_entries_.end();
6212 ++p)
6214 this->count_got_entry(*p);
6218 // Count the number of GOT entries required by ENTRY. Accumulate the result.
6220 template<int size, bool big_endian>
6221 void
6222 Mips_got_info<size, big_endian>::count_got_entry(
6223 Mips_got_entry<size, big_endian>* entry)
6225 if (entry->is_tls_entry())
6226 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6227 else if (entry->is_for_local_symbol()
6228 || entry->sym()->global_got_area() == GGA_NONE)
6229 ++this->local_gotno_;
6230 else
6231 ++this->global_gotno_;
6234 // Add FROM's GOT entries.
6236 template<int size, bool big_endian>
6237 void
6238 Mips_got_info<size, big_endian>::add_got_entries(
6239 Mips_got_info<size, big_endian>* from)
6241 for (typename Got_entry_set::iterator
6242 p = from->got_entries_.begin();
6243 p != from->got_entries_.end();
6244 ++p)
6246 Mips_got_entry<size, big_endian>* entry = *p;
6247 if (this->got_entries_.find(entry) == this->got_entries_.end())
6249 Mips_got_entry<size, big_endian>* entry2 =
6250 new Mips_got_entry<size, big_endian>(*entry);
6251 this->got_entries_.insert(entry2);
6252 this->count_got_entry(entry);
6257 // Add FROM's GOT page entries.
6259 template<int size, bool big_endian>
6260 void
6261 Mips_got_info<size, big_endian>::add_got_page_entries(
6262 Mips_got_info<size, big_endian>* from)
6264 for (typename Got_page_entry_set::iterator
6265 p = from->got_page_entries_.begin();
6266 p != from->got_page_entries_.end();
6267 ++p)
6269 Got_page_entry* entry = *p;
6270 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
6272 Got_page_entry* entry2 = new Got_page_entry(*entry);
6273 this->got_page_entries_.insert(entry2);
6274 this->page_gotno_ += entry->num_pages;
6279 // Mips_output_data_got methods.
6281 // Lay out the GOT. Add local, global and TLS entries. If GOT is
6282 // larger than 64K, create multi-GOT.
6284 template<int size, bool big_endian>
6285 void
6286 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6287 Symbol_table* symtab, const Input_objects* input_objects)
6289 // Decide which symbols need to go in the global part of the GOT and
6290 // count the number of reloc-only GOT symbols.
6291 this->master_got_info_->count_got_symbols(symtab);
6293 // Count the number of GOT entries.
6294 this->master_got_info_->count_got_entries();
6296 unsigned int got_size = this->master_got_info_->got_size();
6297 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6298 this->lay_out_multi_got(layout, input_objects);
6299 else
6301 // Record that all objects use single GOT.
6302 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6303 p != input_objects->relobj_end();
6304 ++p)
6306 Mips_relobj<size, big_endian>* object =
6307 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6308 if (object->get_got_info() != NULL)
6309 object->set_got_info(this->master_got_info_);
6312 this->master_got_info_->add_local_entries(this->target_, layout);
6313 this->master_got_info_->add_global_entries(this->target_, layout,
6314 /*not used*/-1U);
6315 this->master_got_info_->add_tls_entries(this->target_, layout);
6319 // Create multi-GOT. For every GOT, add local, global and TLS entries.
6321 template<int size, bool big_endian>
6322 void
6323 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6324 const Input_objects* input_objects)
6326 // Try to merge the GOTs of input objects together, as long as they
6327 // don't seem to exceed the maximum GOT size, choosing one of them
6328 // to be the primary GOT.
6329 this->merge_gots(input_objects);
6331 // Every symbol that is referenced in a dynamic relocation must be
6332 // present in the primary GOT.
6333 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6335 // Add GOT entries.
6336 unsigned int i = 0;
6337 unsigned int offset = 0;
6338 Mips_got_info<size, big_endian>* g = this->primary_got_;
6341 g->set_index(i);
6342 g->set_offset(offset);
6344 g->add_local_entries(this->target_, layout);
6345 if (i == 0)
6346 g->add_global_entries(this->target_, layout,
6347 (this->master_got_info_->global_gotno()
6348 - this->master_got_info_->reloc_only_gotno()));
6349 else
6350 g->add_global_entries(this->target_, layout, /*not used*/-1U);
6351 g->add_tls_entries(this->target_, layout);
6353 // Forbid global symbols in every non-primary GOT from having
6354 // lazy-binding stubs.
6355 if (i > 0)
6356 g->remove_lazy_stubs(this->target_);
6358 ++i;
6359 offset += g->got_size();
6360 g = g->next();
6362 while (g);
6365 // Attempt to merge GOTs of different input objects. Try to use as much as
6366 // possible of the primary GOT, since it doesn't require explicit dynamic
6367 // relocations, but don't use objects that would reference global symbols
6368 // out of the addressable range. Failing the primary GOT, attempt to merge
6369 // with the current GOT, or finish the current GOT and then make make the new
6370 // GOT current.
6372 template<int size, bool big_endian>
6373 void
6374 Mips_output_data_got<size, big_endian>::merge_gots(
6375 const Input_objects* input_objects)
6377 gold_assert(this->primary_got_ == NULL);
6378 Mips_got_info<size, big_endian>* current = NULL;
6380 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6381 p != input_objects->relobj_end();
6382 ++p)
6384 Mips_relobj<size, big_endian>* object =
6385 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6387 Mips_got_info<size, big_endian>* g = object->get_got_info();
6388 if (g == NULL)
6389 continue;
6391 g->count_got_entries();
6393 // Work out the number of page, local and TLS entries.
6394 unsigned int estimate = this->master_got_info_->page_gotno();
6395 if (estimate > g->page_gotno())
6396 estimate = g->page_gotno();
6397 estimate += g->local_gotno() + g->tls_gotno();
6399 // We place TLS GOT entries after both locals and globals. The globals
6400 // for the primary GOT may overflow the normal GOT size limit, so be
6401 // sure not to merge a GOT which requires TLS with the primary GOT in that
6402 // case. This doesn't affect non-primary GOTs.
6403 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6404 : g->global_gotno());
6406 unsigned int max_count =
6407 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6408 if (estimate <= max_count)
6410 // If we don't have a primary GOT, use it as
6411 // a starting point for the primary GOT.
6412 if (!this->primary_got_)
6414 this->primary_got_ = g;
6415 continue;
6418 // Try merging with the primary GOT.
6419 if (this->merge_got_with(g, object, this->primary_got_))
6420 continue;
6423 // If we can merge with the last-created GOT, do it.
6424 if (current && this->merge_got_with(g, object, current))
6425 continue;
6427 // Well, we couldn't merge, so create a new GOT. Don't check if it
6428 // fits; if it turns out that it doesn't, we'll get relocation
6429 // overflows anyway.
6430 g->set_next(current);
6431 current = g;
6434 // If we do not find any suitable primary GOT, create an empty one.
6435 if (this->primary_got_ == NULL)
6436 this->primary_got_ = new Mips_got_info<size, big_endian>();
6438 // Link primary GOT with secondary GOTs.
6439 this->primary_got_->set_next(current);
6442 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6443 // this would lead to overflow, true if they were merged successfully.
6445 template<int size, bool big_endian>
6446 bool
6447 Mips_output_data_got<size, big_endian>::merge_got_with(
6448 Mips_got_info<size, big_endian>* from,
6449 Mips_relobj<size, big_endian>* object,
6450 Mips_got_info<size, big_endian>* to)
6452 // Work out how many page entries we would need for the combined GOT.
6453 unsigned int estimate = this->master_got_info_->page_gotno();
6454 if (estimate >= from->page_gotno() + to->page_gotno())
6455 estimate = from->page_gotno() + to->page_gotno();
6457 // Conservatively estimate how many local and TLS entries would be needed.
6458 estimate += from->local_gotno() + to->local_gotno();
6459 estimate += from->tls_gotno() + to->tls_gotno();
6461 // If we're merging with the primary got, any TLS relocations will
6462 // come after the full set of global entries. Otherwise estimate those
6463 // conservatively as well.
6464 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6465 estimate += this->master_got_info_->global_gotno();
6466 else
6467 estimate += from->global_gotno() + to->global_gotno();
6469 // Bail out if the combined GOT might be too big.
6470 unsigned int max_count =
6471 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6472 if (estimate > max_count)
6473 return false;
6475 // Transfer the object's GOT information from FROM to TO.
6476 to->add_got_entries(from);
6477 to->add_got_page_entries(from);
6479 // Record that OBJECT should use output GOT TO.
6480 object->set_got_info(to);
6482 return true;
6485 // Write out the GOT.
6487 template<int size, bool big_endian>
6488 void
6489 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6491 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6492 Mips_stubs_entry_set;
6494 // Call parent to write out GOT.
6495 Output_data_got<size, big_endian>::do_write(of);
6497 const off_t offset = this->offset();
6498 const section_size_type oview_size =
6499 convert_to_section_size_type(this->data_size());
6500 unsigned char* const oview = of->get_output_view(offset, oview_size);
6502 // Needed for fixing values of .got section.
6503 this->got_view_ = oview;
6505 // Write lazy stub addresses.
6506 for (typename Mips_stubs_entry_set::iterator
6507 p = this->master_got_info_->global_got_symbols().begin();
6508 p != this->master_got_info_->global_got_symbols().end();
6509 ++p)
6511 Mips_symbol<size>* mips_sym = *p;
6512 if (mips_sym->has_lazy_stub())
6514 Valtype* wv = reinterpret_cast<Valtype*>(
6515 oview + this->get_primary_got_offset(mips_sym));
6516 Valtype value =
6517 this->target_->mips_stubs_section()->stub_address(mips_sym);
6518 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6522 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6523 for (typename Mips_stubs_entry_set::iterator
6524 p = this->master_got_info_->global_got_symbols().begin();
6525 p != this->master_got_info_->global_got_symbols().end();
6526 ++p)
6528 Mips_symbol<size>* mips_sym = *p;
6529 if (!this->multi_got()
6530 && (mips_sym->is_mips16() || mips_sym->is_micromips())
6531 && mips_sym->global_got_area() == GGA_NONE
6532 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6534 Valtype* wv = reinterpret_cast<Valtype*>(
6535 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6536 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6537 if (value != 0)
6539 value |= 1;
6540 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6545 if (!this->secondary_got_relocs_.empty())
6547 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6548 // secondary GOT entries with non-zero initial value copy the value
6549 // to the corresponding primary GOT entry, and set the secondary GOT
6550 // entry to zero.
6551 // TODO(sasa): This is workaround. It needs to be investigated further.
6553 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6555 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6556 if (reloc.symbol_is_global())
6558 Mips_symbol<size>* gsym = reloc.symbol();
6559 gold_assert(gsym != NULL);
6561 unsigned got_offset = reloc.got_offset();
6562 gold_assert(got_offset < oview_size);
6564 // Find primary GOT entry.
6565 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6566 oview + this->get_primary_got_offset(gsym));
6568 // Find secondary GOT entry.
6569 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6571 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6572 if (value != 0)
6574 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6575 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6576 gsym->set_applied_secondary_got_fixup();
6581 of->write_output_view(offset, oview_size, oview);
6584 // We are done if there is no fix up.
6585 if (this->static_relocs_.empty())
6586 return;
6588 Output_segment* tls_segment = this->layout_->tls_segment();
6589 gold_assert(tls_segment != NULL);
6591 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6593 Static_reloc& reloc(this->static_relocs_[i]);
6595 Mips_address value;
6596 if (!reloc.symbol_is_global())
6598 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6599 const Symbol_value<size>* psymval =
6600 object->local_symbol(reloc.index());
6602 // We are doing static linking. Issue an error and skip this
6603 // relocation if the symbol is undefined or in a discarded_section.
6604 bool is_ordinary;
6605 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6606 if ((shndx == elfcpp::SHN_UNDEF)
6607 || (is_ordinary
6608 && shndx != elfcpp::SHN_UNDEF
6609 && !object->is_section_included(shndx)
6610 && !this->symbol_table_->is_section_folded(object, shndx)))
6612 gold_error(_("undefined or discarded local symbol %u from "
6613 " object %s in GOT"),
6614 reloc.index(), reloc.relobj()->name().c_str());
6615 continue;
6618 value = psymval->value(object, 0);
6620 else
6622 const Mips_symbol<size>* gsym = reloc.symbol();
6623 gold_assert(gsym != NULL);
6625 // We are doing static linking. Issue an error and skip this
6626 // relocation if the symbol is undefined or in a discarded_section
6627 // unless it is a weakly_undefined symbol.
6628 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6629 && !gsym->is_weak_undefined())
6631 gold_error(_("undefined or discarded symbol %s in GOT"),
6632 gsym->name());
6633 continue;
6636 if (!gsym->is_weak_undefined())
6637 value = gsym->value();
6638 else
6639 value = 0;
6642 unsigned got_offset = reloc.got_offset();
6643 gold_assert(got_offset < oview_size);
6645 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6646 Valtype x;
6648 switch (reloc.r_type())
6650 case elfcpp::R_MIPS_TLS_DTPMOD32:
6651 case elfcpp::R_MIPS_TLS_DTPMOD64:
6652 x = value;
6653 break;
6654 case elfcpp::R_MIPS_TLS_DTPREL32:
6655 case elfcpp::R_MIPS_TLS_DTPREL64:
6656 x = value - elfcpp::DTP_OFFSET;
6657 break;
6658 case elfcpp::R_MIPS_TLS_TPREL32:
6659 case elfcpp::R_MIPS_TLS_TPREL64:
6660 x = value - elfcpp::TP_OFFSET;
6661 break;
6662 default:
6663 gold_unreachable();
6664 break;
6667 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6670 of->write_output_view(offset, oview_size, oview);
6673 // Mips_relobj methods.
6675 // Count the local symbols. The Mips backend needs to know if a symbol
6676 // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6677 // because the Symbol object keeps the ELF symbol type and st_other field.
6678 // For local symbol it is harder because we cannot access this information.
6679 // So we override the do_count_local_symbol in parent and scan local symbols to
6680 // mark MIPS16 and microMIPS functions. This is not the most efficient way but
6681 // I do not want to slow down other ports by calling a per symbol target hook
6682 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6684 template<int size, bool big_endian>
6685 void
6686 Mips_relobj<size, big_endian>::do_count_local_symbols(
6687 Stringpool_template<char>* pool,
6688 Stringpool_template<char>* dynpool)
6690 // Ask parent to count the local symbols.
6691 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6692 const unsigned int loccount = this->local_symbol_count();
6693 if (loccount == 0)
6694 return;
6696 // Initialize the mips16 and micromips function bit-vector.
6697 this->local_symbol_is_mips16_.resize(loccount, false);
6698 this->local_symbol_is_micromips_.resize(loccount, false);
6700 // Read the symbol table section header.
6701 const unsigned int symtab_shndx = this->symtab_shndx();
6702 elfcpp::Shdr<size, big_endian>
6703 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6704 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6706 // Read the local symbols.
6707 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6708 gold_assert(loccount == symtabshdr.get_sh_info());
6709 off_t locsize = loccount * sym_size;
6710 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6711 locsize, true, true);
6713 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6715 // Skip the first dummy symbol.
6716 psyms += sym_size;
6717 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6719 elfcpp::Sym<size, big_endian> sym(psyms);
6720 unsigned char st_other = sym.get_st_other();
6721 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6722 this->local_symbol_is_micromips_[i] =
6723 elfcpp::elf_st_is_micromips(st_other);
6727 // Read the symbol information.
6729 template<int size, bool big_endian>
6730 void
6731 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6733 // Call parent class to read symbol information.
6734 this->base_read_symbols(sd);
6736 // Read processor-specific flags in ELF file header.
6737 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6738 elfcpp::Elf_sizes<size>::ehdr_size,
6739 true, false);
6740 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6741 this->processor_specific_flags_ = ehdr.get_e_flags();
6743 // Get the section names.
6744 const unsigned char* pnamesu = sd->section_names->data();
6745 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6747 // Initialize the mips16 stub section bit-vectors.
6748 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6749 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6750 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6752 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6753 const unsigned char* pshdrs = sd->section_headers->data();
6754 const unsigned char* ps = pshdrs + shdr_size;
6755 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6757 elfcpp::Shdr<size, big_endian> shdr(ps);
6759 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6761 this->has_reginfo_section_ = true;
6762 // Read the gp value that was used to create this object. We need the
6763 // gp value while processing relocs. The .reginfo section is not used
6764 // in the 64-bit MIPS ELF ABI.
6765 section_offset_type section_offset = shdr.get_sh_offset();
6766 section_size_type section_size =
6767 convert_to_section_size_type(shdr.get_sh_size());
6768 const unsigned char* view =
6769 this->get_view(section_offset, section_size, true, false);
6771 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6773 // Read the rest of .reginfo.
6774 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6775 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6776 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6777 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6778 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6781 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6783 gold_assert(this->attributes_section_data_ == NULL);
6784 section_offset_type section_offset = shdr.get_sh_offset();
6785 section_size_type section_size =
6786 convert_to_section_size_type(shdr.get_sh_size());
6787 const unsigned char* view =
6788 this->get_view(section_offset, section_size, true, false);
6789 this->attributes_section_data_ =
6790 new Attributes_section_data(view, section_size);
6793 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6795 gold_assert(this->abiflags_ == NULL);
6796 section_offset_type section_offset = shdr.get_sh_offset();
6797 section_size_type section_size =
6798 convert_to_section_size_type(shdr.get_sh_size());
6799 const unsigned char* view =
6800 this->get_view(section_offset, section_size, true, false);
6801 this->abiflags_ = new Mips_abiflags<big_endian>();
6803 this->abiflags_->version =
6804 elfcpp::Swap<16, big_endian>::readval(view);
6805 if (this->abiflags_->version != 0)
6807 gold_error(_("%s: .MIPS.abiflags section has "
6808 "unsupported version %u"),
6809 this->name().c_str(),
6810 this->abiflags_->version);
6811 break;
6813 this->abiflags_->isa_level =
6814 elfcpp::Swap<8, big_endian>::readval(view + 2);
6815 this->abiflags_->isa_rev =
6816 elfcpp::Swap<8, big_endian>::readval(view + 3);
6817 this->abiflags_->gpr_size =
6818 elfcpp::Swap<8, big_endian>::readval(view + 4);
6819 this->abiflags_->cpr1_size =
6820 elfcpp::Swap<8, big_endian>::readval(view + 5);
6821 this->abiflags_->cpr2_size =
6822 elfcpp::Swap<8, big_endian>::readval(view + 6);
6823 this->abiflags_->fp_abi =
6824 elfcpp::Swap<8, big_endian>::readval(view + 7);
6825 this->abiflags_->isa_ext =
6826 elfcpp::Swap<32, big_endian>::readval(view + 8);
6827 this->abiflags_->ases =
6828 elfcpp::Swap<32, big_endian>::readval(view + 12);
6829 this->abiflags_->flags1 =
6830 elfcpp::Swap<32, big_endian>::readval(view + 16);
6831 this->abiflags_->flags2 =
6832 elfcpp::Swap<32, big_endian>::readval(view + 20);
6835 // In the 64-bit ABI, .MIPS.options section holds register information.
6836 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6837 // starts with this header:
6839 // typedef struct
6840 // {
6841 // // Type of option.
6842 // unsigned char kind[1];
6843 // // Size of option descriptor, including header.
6844 // unsigned char size[1];
6845 // // Section index of affected section, or 0 for global option.
6846 // unsigned char section[2];
6847 // // Information specific to this kind of option.
6848 // unsigned char info[4];
6849 // };
6851 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6852 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6853 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6855 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
6857 section_offset_type section_offset = shdr.get_sh_offset();
6858 section_size_type section_size =
6859 convert_to_section_size_type(shdr.get_sh_size());
6860 const unsigned char* view =
6861 this->get_view(section_offset, section_size, true, false);
6862 const unsigned char* end = view + section_size;
6864 while (view + 8 <= end)
6866 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6867 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6868 if (sz < 8)
6870 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6871 "than its header"),
6872 this->name().c_str(),
6873 this->mips_elf_options_section_name(), sz);
6874 break;
6877 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6879 // In the 64 bit ABI, an ODK_REGINFO option is the following
6880 // structure. The info field of the options header is not
6881 // used.
6883 // typedef struct
6884 // {
6885 // // Mask of general purpose registers used.
6886 // unsigned char ri_gprmask[4];
6887 // // Padding.
6888 // unsigned char ri_pad[4];
6889 // // Mask of co-processor registers used.
6890 // unsigned char ri_cprmask[4][4];
6891 // // GP register value for this object file.
6892 // unsigned char ri_gp_value[8];
6893 // };
6895 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6896 + 32);
6898 else if (kind == elfcpp::ODK_REGINFO)
6900 // In the 32 bit ABI, an ODK_REGINFO option is the following
6901 // structure. The info field of the options header is not
6902 // used. The same structure is used in .reginfo section.
6904 // typedef struct
6905 // {
6906 // unsigned char ri_gprmask[4];
6907 // unsigned char ri_cprmask[4][4];
6908 // unsigned char ri_gp_value[4];
6909 // };
6911 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6912 + 28);
6914 view += sz;
6918 const char* name = pnames + shdr.get_sh_name();
6919 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
6920 this->section_is_mips16_call_stub_[i] =
6921 is_prefix_of(".mips16.call.", name);
6922 this->section_is_mips16_call_fp_stub_[i] =
6923 is_prefix_of(".mips16.call.fp.", name);
6925 if (strcmp(name, ".pdr") == 0)
6927 gold_assert(this->pdr_shndx_ == -1U);
6928 this->pdr_shndx_ = i;
6933 // Discard MIPS16 stub secions that are not needed.
6935 template<int size, bool big_endian>
6936 void
6937 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
6939 for (typename Mips16_stubs_int_map::const_iterator
6940 it = this->mips16_stub_sections_.begin();
6941 it != this->mips16_stub_sections_.end(); ++it)
6943 Mips16_stub_section<size, big_endian>* stub_section = it->second;
6944 if (!stub_section->is_target_found())
6946 gold_error(_("no relocation found in mips16 stub section '%s'"),
6947 stub_section->object()
6948 ->section_name(stub_section->shndx()).c_str());
6951 bool discard = false;
6952 if (stub_section->is_for_local_function())
6954 if (stub_section->is_fn_stub())
6956 // This stub is for a local symbol. This stub will only
6957 // be needed if there is some relocation in this object,
6958 // other than a 16 bit function call, which refers to this
6959 // symbol.
6960 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
6961 discard = true;
6962 else
6963 this->add_local_mips16_fn_stub(stub_section);
6965 else
6967 // This stub is for a local symbol. This stub will only
6968 // be needed if there is some relocation (R_MIPS16_26) in
6969 // this object that refers to this symbol.
6970 gold_assert(stub_section->is_call_stub()
6971 || stub_section->is_call_fp_stub());
6972 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
6973 discard = true;
6974 else
6975 this->add_local_mips16_call_stub(stub_section);
6978 else
6980 Mips_symbol<size>* gsym = stub_section->gsym();
6981 if (stub_section->is_fn_stub())
6983 if (gsym->has_mips16_fn_stub())
6984 // We already have a stub for this function.
6985 discard = true;
6986 else
6988 gsym->set_mips16_fn_stub(stub_section);
6989 if (gsym->should_add_dynsym_entry(symtab))
6991 // If we have a MIPS16 function with a stub, the
6992 // dynamic symbol must refer to the stub, since only
6993 // the stub uses the standard calling conventions.
6994 gsym->set_need_fn_stub();
6995 if (gsym->is_from_dynobj())
6996 gsym->set_needs_dynsym_value();
6999 if (!gsym->need_fn_stub())
7000 discard = true;
7002 else if (stub_section->is_call_stub())
7004 if (gsym->is_mips16())
7005 // We don't need the call_stub; this is a 16 bit
7006 // function, so calls from other 16 bit functions are
7007 // OK.
7008 discard = true;
7009 else if (gsym->has_mips16_call_stub())
7010 // We already have a stub for this function.
7011 discard = true;
7012 else
7013 gsym->set_mips16_call_stub(stub_section);
7015 else
7017 gold_assert(stub_section->is_call_fp_stub());
7018 if (gsym->is_mips16())
7019 // We don't need the call_stub; this is a 16 bit
7020 // function, so calls from other 16 bit functions are
7021 // OK.
7022 discard = true;
7023 else if (gsym->has_mips16_call_fp_stub())
7024 // We already have a stub for this function.
7025 discard = true;
7026 else
7027 gsym->set_mips16_call_fp_stub(stub_section);
7030 if (discard)
7031 this->set_output_section(stub_section->shndx(), NULL);
7035 // Mips_output_data_la25_stub methods.
7037 // Template for standard LA25 stub.
7038 template<int size, bool big_endian>
7039 const uint32_t
7040 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7042 0x3c190000, // lui $25,%hi(func)
7043 0x08000000, // j func
7044 0x27390000, // add $25,$25,%lo(func)
7045 0x00000000 // nop
7048 // Template for microMIPS LA25 stub.
7049 template<int size, bool big_endian>
7050 const uint32_t
7051 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7053 0x41b9, 0x0000, // lui t9,%hi(func)
7054 0xd400, 0x0000, // j func
7055 0x3339, 0x0000, // addiu t9,t9,%lo(func)
7056 0x0000, 0x0000 // nop
7059 // Create la25 stub for a symbol.
7061 template<int size, bool big_endian>
7062 void
7063 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7064 Symbol_table* symtab, Target_mips<size, big_endian>* target,
7065 Mips_symbol<size>* gsym)
7067 if (!gsym->has_la25_stub())
7069 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
7070 this->symbols_.push_back(gsym);
7071 this->create_stub_symbol(gsym, symtab, target, 16);
7075 // Create a symbol for SYM stub's value and size, to help make the disassembly
7076 // easier to read.
7078 template<int size, bool big_endian>
7079 void
7080 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7081 Mips_symbol<size>* sym, Symbol_table* symtab,
7082 Target_mips<size, big_endian>* target, uint64_t symsize)
7084 std::string name(".pic.");
7085 name += sym->name();
7087 unsigned int offset = sym->la25_stub_offset();
7088 if (sym->is_micromips())
7089 offset |= 1;
7091 // Make it a local function.
7092 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7093 Symbol_table::PREDEFINED,
7094 target->la25_stub_section(),
7095 offset, symsize, elfcpp::STT_FUNC,
7096 elfcpp::STB_LOCAL,
7097 elfcpp::STV_DEFAULT, 0,
7098 false, false);
7099 new_sym->set_is_forced_local();
7102 // Write out la25 stubs. This uses the hand-coded instructions above,
7103 // and adjusts them as needed.
7105 template<int size, bool big_endian>
7106 void
7107 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7109 const off_t offset = this->offset();
7110 const section_size_type oview_size =
7111 convert_to_section_size_type(this->data_size());
7112 unsigned char* const oview = of->get_output_view(offset, oview_size);
7114 for (typename std::vector<Mips_symbol<size>*>::iterator
7115 p = this->symbols_.begin();
7116 p != this->symbols_.end();
7117 ++p)
7119 Mips_symbol<size>* sym = *p;
7120 unsigned char* pov = oview + sym->la25_stub_offset();
7122 Mips_address target = sym->value();
7123 if (!sym->is_micromips())
7125 elfcpp::Swap<32, big_endian>::writeval(pov,
7126 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7127 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7128 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7129 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7130 la25_stub_entry[2] | (target & 0xffff));
7131 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7133 else
7135 target |= 1;
7136 // First stub instruction. Paste high 16-bits of the target.
7137 elfcpp::Swap<16, big_endian>::writeval(pov,
7138 la25_stub_micromips_entry[0]);
7139 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7140 ((target + 0x8000) >> 16) & 0xffff);
7141 // Second stub instruction. Paste low 26-bits of the target, shifted
7142 // right by 1.
7143 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7144 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7145 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7146 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7147 // Third stub instruction. Paste low 16-bits of the target.
7148 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7149 la25_stub_micromips_entry[4]);
7150 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7151 // Fourth stub instruction.
7152 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7153 la25_stub_micromips_entry[6]);
7154 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7155 la25_stub_micromips_entry[7]);
7159 of->write_output_view(offset, oview_size, oview);
7162 // Mips_output_data_plt methods.
7164 // The format of the first PLT entry in an O32 executable.
7165 template<int size, bool big_endian>
7166 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7168 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
7169 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
7170 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
7171 0x031cc023, // subu $24, $24, $28
7172 0x03e07825, // or $15, $31, zero
7173 0x0018c082, // srl $24, $24, 2
7174 0x0320f809, // jalr $25
7175 0x2718fffe // subu $24, $24, 2
7178 // The format of the first PLT entry in an N32 executable. Different
7179 // because gp ($28) is not available; we use t2 ($14) instead.
7180 template<int size, bool big_endian>
7181 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7183 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7184 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
7185 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7186 0x030ec023, // subu $24, $24, $14
7187 0x03e07825, // or $15, $31, zero
7188 0x0018c082, // srl $24, $24, 2
7189 0x0320f809, // jalr $25
7190 0x2718fffe // subu $24, $24, 2
7193 // The format of the first PLT entry in an N64 executable. Different
7194 // from N32 because of the increased size of GOT entries.
7195 template<int size, bool big_endian>
7196 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7198 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7199 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
7200 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7201 0x030ec023, // subu $24, $24, $14
7202 0x03e07825, // or $15, $31, zero
7203 0x0018c0c2, // srl $24, $24, 3
7204 0x0320f809, // jalr $25
7205 0x2718fffe // subu $24, $24, 2
7208 // The format of the microMIPS first PLT entry in an O32 executable.
7209 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7210 // of the GOTPLT entry handled, so this stub may only be used when
7211 // all the subsequent PLT entries are microMIPS code too.
7213 // The trailing NOP is for alignment and correct disassembly only.
7214 template<int size, bool big_endian>
7215 const uint32_t Mips_output_data_plt<size, big_endian>::
7216 plt0_entry_micromips_o32[] =
7218 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
7219 0xff23, 0x0000, // lw $25, 0($3)
7220 0x0535, // subu $2, $2, $3
7221 0x2525, // srl $2, $2, 2
7222 0x3302, 0xfffe, // subu $24, $2, 2
7223 0x0dff, // move $15, $31
7224 0x45f9, // jalrs $25
7225 0x0f83, // move $28, $3
7226 0x0c00 // nop
7229 // The format of the microMIPS first PLT entry in an O32 executable
7230 // in the insn32 mode.
7231 template<int size, bool big_endian>
7232 const uint32_t Mips_output_data_plt<size, big_endian>::
7233 plt0_entry_micromips32_o32[] =
7235 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
7236 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
7237 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
7238 0x0398, 0xc1d0, // subu $24, $24, $28
7239 0x001f, 0x7a90, // or $15, $31, zero
7240 0x0318, 0x1040, // srl $24, $24, 2
7241 0x03f9, 0x0f3c, // jalr $25
7242 0x3318, 0xfffe // subu $24, $24, 2
7245 // The format of subsequent standard entries in the PLT.
7246 template<int size, bool big_endian>
7247 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7249 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7250 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7251 0x03200008, // jr $25
7252 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7255 // The format of subsequent R6 PLT entries.
7256 template<int size, bool big_endian>
7257 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7259 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7260 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7261 0x03200009, // jr $25
7262 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7265 // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
7266 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7267 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7268 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7269 // target function address in register v0.
7270 template<int size, bool big_endian>
7271 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7273 0xb303, // lw $3, 12($pc)
7274 0x651b, // move $24, $3
7275 0x9b60, // lw $3, 0($3)
7276 0xeb00, // jr $3
7277 0x653b, // move $25, $3
7278 0x6500, // nop
7279 0x0000, 0x0000 // .word (.got.plt entry)
7282 // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
7283 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7284 template<int size, bool big_endian>
7285 const uint32_t Mips_output_data_plt<size, big_endian>::
7286 plt_entry_micromips_o32[] =
7288 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
7289 0xff22, 0x0000, // lw $25, 0($2)
7290 0x4599, // jr $25
7291 0x0f02 // move $24, $2
7294 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7295 template<int size, bool big_endian>
7296 const uint32_t Mips_output_data_plt<size, big_endian>::
7297 plt_entry_micromips32_o32[] =
7299 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
7300 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
7301 0x0019, 0x0f3c, // jr $25
7302 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
7305 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7307 template<int size, bool big_endian>
7308 void
7309 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7310 unsigned int r_type)
7312 gold_assert(!gsym->has_plt_offset());
7314 // Final PLT offset for a symbol will be set in method set_plt_offsets().
7315 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7316 + sizeof(plt0_entry_o32));
7317 this->symbols_.push_back(gsym);
7319 // Record whether the relocation requires a standard MIPS
7320 // or a compressed code entry.
7321 if (jal_reloc(r_type))
7323 if (r_type == elfcpp::R_MIPS_26)
7324 gsym->set_needs_mips_plt(true);
7325 else
7326 gsym->set_needs_comp_plt(true);
7329 section_offset_type got_offset = this->got_plt_->current_data_size();
7331 // Every PLT entry needs a GOT entry which points back to the PLT
7332 // entry (this will be changed by the dynamic linker, normally
7333 // lazily when the function is called).
7334 this->got_plt_->set_current_data_size(got_offset + size/8);
7336 gsym->set_needs_dynsym_entry();
7337 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7338 got_offset);
7341 // Set final PLT offsets. For each symbol, determine whether standard or
7342 // compressed (MIPS16 or microMIPS) PLT entry is used.
7344 template<int size, bool big_endian>
7345 void
7346 Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7348 // The sizes of individual PLT entries.
7349 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7350 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7351 ? this->compressed_plt_entry_size() : 0);
7353 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7354 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7356 Mips_symbol<size>* mips_sym = *p;
7358 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7359 // so always use a standard entry there.
7361 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7362 // all MIPS16 calls will go via that stub, and there is no benefit
7363 // to having a MIPS16 entry. And in the case of call_stub a
7364 // standard entry actually has to be used as the stub ends with a J
7365 // instruction.
7366 if (this->target_->is_output_newabi()
7367 || mips_sym->has_mips16_call_stub()
7368 || mips_sym->has_mips16_call_fp_stub())
7370 mips_sym->set_needs_mips_plt(true);
7371 mips_sym->set_needs_comp_plt(false);
7374 // Otherwise, if there are no direct calls to the function, we
7375 // have a free choice of whether to use standard or compressed
7376 // entries. Prefer microMIPS entries if the object is known to
7377 // contain microMIPS code, so that it becomes possible to create
7378 // pure microMIPS binaries. Prefer standard entries otherwise,
7379 // because MIPS16 ones are no smaller and are usually slower.
7380 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7382 if (this->target_->is_output_micromips())
7383 mips_sym->set_needs_comp_plt(true);
7384 else
7385 mips_sym->set_needs_mips_plt(true);
7388 if (mips_sym->needs_mips_plt())
7390 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7391 this->plt_mips_offset_ += plt_mips_entry_size;
7393 if (mips_sym->needs_comp_plt())
7395 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7396 this->plt_comp_offset_ += plt_comp_entry_size;
7400 // Figure out the size of the PLT header if we know that we are using it.
7401 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7402 this->plt_header_size_ = this->get_plt_header_size();
7405 // Write out the PLT. This uses the hand-coded instructions above,
7406 // and adjusts them as needed.
7408 template<int size, bool big_endian>
7409 void
7410 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7412 const off_t offset = this->offset();
7413 const section_size_type oview_size =
7414 convert_to_section_size_type(this->data_size());
7415 unsigned char* const oview = of->get_output_view(offset, oview_size);
7417 const off_t gotplt_file_offset = this->got_plt_->offset();
7418 const section_size_type gotplt_size =
7419 convert_to_section_size_type(this->got_plt_->data_size());
7420 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7421 gotplt_size);
7422 unsigned char* pov = oview;
7424 Mips_address plt_address = this->address();
7426 // Calculate the address of .got.plt.
7427 Mips_address gotplt_addr = this->got_plt_->address();
7428 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7429 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7431 // The PLT sequence is not safe for N64 if .got.plt's address can
7432 // not be loaded in two instructions.
7433 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7434 || ~(gotplt_addr | 0x7fffffff) == 0);
7436 // Write the PLT header.
7437 const uint32_t* plt0_entry = this->get_plt_header_entry();
7438 if (plt0_entry == plt0_entry_micromips_o32)
7440 // Write microMIPS PLT header.
7441 gold_assert(gotplt_addr % 4 == 0);
7443 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7445 // ADDIUPC has a span of +/-16MB, check we're in range.
7446 if (gotpc_offset + 0x1000000 >= 0x2000000)
7448 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7449 "ADDIUPC"), (long)gotpc_offset);
7450 return;
7453 elfcpp::Swap<16, big_endian>::writeval(pov,
7454 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7455 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7456 (gotpc_offset >> 2) & 0xffff);
7457 pov += 4;
7458 for (unsigned int i = 2;
7459 i < (sizeof(plt0_entry_micromips_o32)
7460 / sizeof(plt0_entry_micromips_o32[0]));
7461 i++)
7463 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7464 pov += 2;
7467 else if (plt0_entry == plt0_entry_micromips32_o32)
7469 // Write microMIPS PLT header in insn32 mode.
7470 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7471 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7472 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7473 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7474 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7475 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7476 pov += 12;
7477 for (unsigned int i = 6;
7478 i < (sizeof(plt0_entry_micromips32_o32)
7479 / sizeof(plt0_entry_micromips32_o32[0]));
7480 i++)
7482 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7483 pov += 2;
7486 else
7488 // Write standard PLT header.
7489 elfcpp::Swap<32, big_endian>::writeval(pov,
7490 plt0_entry[0] | gotplt_addr_high);
7491 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7492 plt0_entry[1] | gotplt_addr_low);
7493 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7494 plt0_entry[2] | gotplt_addr_low);
7495 pov += 12;
7496 for (int i = 3; i < 8; i++)
7498 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7499 pov += 4;
7504 unsigned char* gotplt_pov = gotplt_view;
7505 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7507 // The first two entries in .got.plt are reserved.
7508 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7509 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7511 unsigned int gotplt_offset = 2 * got_entry_size;
7512 gotplt_pov += 2 * got_entry_size;
7514 // Calculate the address of the PLT header.
7515 Mips_address header_address = (plt_address
7516 + (this->is_plt_header_compressed() ? 1 : 0));
7518 // Initialize compressed PLT area view.
7519 unsigned char* pov2 = pov + this->plt_mips_offset_;
7521 // Write the PLT entries.
7522 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7523 p = this->symbols_.begin();
7524 p != this->symbols_.end();
7525 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7527 Mips_symbol<size>* mips_sym = *p;
7529 // Calculate the address of the .got.plt entry.
7530 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7531 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7532 & 0xffff);
7533 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7535 // Initially point the .got.plt entry at the PLT header.
7536 if (this->target_->is_output_n64())
7537 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7538 else
7539 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7541 // Now handle the PLT itself. First the standard entry.
7542 if (mips_sym->has_mips_plt_offset())
7544 // Pick the load opcode (LW or LD).
7545 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7546 : 0x8c000000;
7548 const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7549 : plt_entry;
7551 // Fill in the PLT entry itself.
7552 elfcpp::Swap<32, big_endian>::writeval(pov,
7553 entry[0] | gotplt_entry_addr_hi);
7554 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7555 entry[1] | gotplt_entry_addr_lo | load);
7556 elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
7557 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7558 entry[3] | gotplt_entry_addr_lo);
7559 pov += 16;
7562 // Now the compressed entry. They come after any standard ones.
7563 if (mips_sym->has_comp_plt_offset())
7565 if (!this->target_->is_output_micromips())
7567 // Write MIPS16 PLT entry.
7568 const uint32_t* plt_entry = plt_entry_mips16_o32;
7570 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7571 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7572 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7573 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7574 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7575 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7576 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7577 gotplt_entry_addr);
7578 pov2 += 16;
7580 else if (this->target_->use_32bit_micromips_instructions())
7582 // Write microMIPS PLT entry in insn32 mode.
7583 const uint32_t* plt_entry = plt_entry_micromips32_o32;
7585 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7586 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7587 gotplt_entry_addr_hi);
7588 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7589 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7590 gotplt_entry_addr_lo);
7591 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7592 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7593 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7594 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7595 gotplt_entry_addr_lo);
7596 pov2 += 16;
7598 else
7600 // Write microMIPS PLT entry.
7601 const uint32_t* plt_entry = plt_entry_micromips_o32;
7603 gold_assert(gotplt_entry_addr % 4 == 0);
7605 Mips_address loc_address = plt_address + pov2 - oview;
7606 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7608 // ADDIUPC has a span of +/-16MB, check we're in range.
7609 if (gotpc_offset + 0x1000000 >= 0x2000000)
7611 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7612 "range of ADDIUPC"), (long)gotpc_offset);
7613 return;
7616 elfcpp::Swap<16, big_endian>::writeval(pov2,
7617 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7618 elfcpp::Swap<16, big_endian>::writeval(
7619 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7620 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7621 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7622 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7623 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7624 pov2 += 12;
7629 // Check the number of bytes written for standard entries.
7630 gold_assert(static_cast<section_size_type>(
7631 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7632 // Check the number of bytes written for compressed entries.
7633 gold_assert((static_cast<section_size_type>(pov2 - pov)
7634 == this->plt_comp_offset_));
7635 // Check the total number of bytes written.
7636 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7638 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7639 == gotplt_size);
7641 of->write_output_view(offset, oview_size, oview);
7642 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7645 // Mips_output_data_mips_stubs methods.
7647 // The format of the lazy binding stub when dynamic symbol count is less than
7648 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7649 template<int size, bool big_endian>
7650 const uint32_t
7651 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7653 0x8f998010, // lw t9,0x8010(gp)
7654 0x03e07825, // or t7,ra,zero
7655 0x0320f809, // jalr t9,ra
7656 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7659 // The format of the lazy binding stub when dynamic symbol count is less than
7660 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7661 template<int size, bool big_endian>
7662 const uint32_t
7663 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7665 0xdf998010, // ld t9,0x8010(gp)
7666 0x03e07825, // or t7,ra,zero
7667 0x0320f809, // jalr t9,ra
7668 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7671 // The format of the lazy binding stub when dynamic symbol count is less than
7672 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7673 template<int size, bool big_endian>
7674 const uint32_t
7675 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7677 0x8f998010, // lw t9,0x8010(gp)
7678 0x03e07825, // or t7,ra,zero
7679 0x0320f809, // jalr t9,ra
7680 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7683 // The format of the lazy binding stub when dynamic symbol count is less than
7684 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7685 template<int size, bool big_endian>
7686 const uint32_t
7687 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7689 0xdf998010, // ld t9,0x8010(gp)
7690 0x03e07825, // or t7,ra,zero
7691 0x0320f809, // jalr t9,ra
7692 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7695 // The format of the lazy binding stub when dynamic symbol count is greater than
7696 // 64K, and ABI is not N64.
7697 template<int size, bool big_endian>
7698 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7700 0x8f998010, // lw t9,0x8010(gp)
7701 0x03e07825, // or t7,ra,zero
7702 0x3c180000, // lui t8,DYN_INDEX
7703 0x0320f809, // jalr t9,ra
7704 0x37180000 // ori t8,t8,DYN_INDEX
7707 // The format of the lazy binding stub when dynamic symbol count is greater than
7708 // 64K, and ABI is N64.
7709 template<int size, bool big_endian>
7710 const uint32_t
7711 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7713 0xdf998010, // ld t9,0x8010(gp)
7714 0x03e07825, // or t7,ra,zero
7715 0x3c180000, // lui t8,DYN_INDEX
7716 0x0320f809, // jalr t9,ra
7717 0x37180000 // ori t8,t8,DYN_INDEX
7720 // microMIPS stubs.
7722 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7723 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7724 template<int size, bool big_endian>
7725 const uint32_t
7726 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7728 0xff3c, 0x8010, // lw t9,0x8010(gp)
7729 0x0dff, // move t7,ra
7730 0x45d9, // jalr t9
7731 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7734 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7735 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7736 template<int size, bool big_endian>
7737 const uint32_t
7738 Mips_output_data_mips_stubs<size, big_endian>::
7739 lazy_stub_micromips_normal_1_n64[] =
7741 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7742 0x0dff, // move t7,ra
7743 0x45d9, // jalr t9
7744 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7747 // The format of the microMIPS lazy binding stub when dynamic symbol
7748 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7749 // and ABI is not N64.
7750 template<int size, bool big_endian>
7751 const uint32_t
7752 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7754 0xff3c, 0x8010, // lw t9,0x8010(gp)
7755 0x0dff, // move t7,ra
7756 0x45d9, // jalr t9
7757 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7760 // The format of the microMIPS lazy binding stub when dynamic symbol
7761 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7762 // and ABI is N64.
7763 template<int size, bool big_endian>
7764 const uint32_t
7765 Mips_output_data_mips_stubs<size, big_endian>::
7766 lazy_stub_micromips_normal_2_n64[] =
7768 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7769 0x0dff, // move t7,ra
7770 0x45d9, // jalr t9
7771 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7774 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7775 // greater than 64K, and ABI is not N64.
7776 template<int size, bool big_endian>
7777 const uint32_t
7778 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7780 0xff3c, 0x8010, // lw t9,0x8010(gp)
7781 0x0dff, // move t7,ra
7782 0x41b8, 0x0000, // lui t8,DYN_INDEX
7783 0x45d9, // jalr t9
7784 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7787 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7788 // greater than 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_micromips_big_n64[] =
7793 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7794 0x0dff, // move t7,ra
7795 0x41b8, 0x0000, // lui t8,DYN_INDEX
7796 0x45d9, // jalr t9
7797 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7800 // 32-bit microMIPS stubs.
7802 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7803 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7804 // can use only 32-bit instructions.
7805 template<int size, bool big_endian>
7806 const uint32_t
7807 Mips_output_data_mips_stubs<size, big_endian>::
7808 lazy_stub_micromips32_normal_1[] =
7810 0xff3c, 0x8010, // lw t9,0x8010(gp)
7811 0x001f, 0x7a90, // or t7,ra,zero
7812 0x03f9, 0x0f3c, // jalr ra,t9
7813 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7816 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7817 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7818 // use only 32-bit instructions.
7819 template<int size, bool big_endian>
7820 const uint32_t
7821 Mips_output_data_mips_stubs<size, big_endian>::
7822 lazy_stub_micromips32_normal_1_n64[] =
7824 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7825 0x001f, 0x7a90, // or t7,ra,zero
7826 0x03f9, 0x0f3c, // jalr ra,t9
7827 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7830 // The format of the microMIPS lazy binding stub when dynamic symbol
7831 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7832 // ABI is not N64, and we can use only 32-bit instructions.
7833 template<int size, bool big_endian>
7834 const uint32_t
7835 Mips_output_data_mips_stubs<size, big_endian>::
7836 lazy_stub_micromips32_normal_2[] =
7838 0xff3c, 0x8010, // lw t9,0x8010(gp)
7839 0x001f, 0x7a90, // or t7,ra,zero
7840 0x03f9, 0x0f3c, // jalr ra,t9
7841 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7844 // The format of the microMIPS lazy binding stub when dynamic symbol
7845 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7846 // ABI is N64, and we can use only 32-bit instructions.
7847 template<int size, bool big_endian>
7848 const uint32_t
7849 Mips_output_data_mips_stubs<size, big_endian>::
7850 lazy_stub_micromips32_normal_2_n64[] =
7852 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7853 0x001f, 0x7a90, // or t7,ra,zero
7854 0x03f9, 0x0f3c, // jalr ra,t9
7855 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7858 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7859 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7860 template<int size, bool big_endian>
7861 const uint32_t
7862 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7864 0xff3c, 0x8010, // lw t9,0x8010(gp)
7865 0x001f, 0x7a90, // or t7,ra,zero
7866 0x41b8, 0x0000, // lui t8,DYN_INDEX
7867 0x03f9, 0x0f3c, // jalr ra,t9
7868 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7871 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7872 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7873 template<int size, bool big_endian>
7874 const uint32_t
7875 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
7877 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7878 0x001f, 0x7a90, // or t7,ra,zero
7879 0x41b8, 0x0000, // lui t8,DYN_INDEX
7880 0x03f9, 0x0f3c, // jalr ra,t9
7881 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7884 // Create entry for a symbol.
7886 template<int size, bool big_endian>
7887 void
7888 Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7889 Mips_symbol<size>* gsym)
7891 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7893 this->symbols_.insert(gsym);
7894 gsym->set_has_lazy_stub(true);
7898 // Remove entry for a symbol.
7900 template<int size, bool big_endian>
7901 void
7902 Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
7903 Mips_symbol<size>* gsym)
7905 if (gsym->has_lazy_stub())
7907 this->symbols_.erase(gsym);
7908 gsym->set_has_lazy_stub(false);
7912 // Set stub offsets for symbols. This method expects that the number of
7913 // entries in dynamic symbol table is set.
7915 template<int size, bool big_endian>
7916 void
7917 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
7919 gold_assert(this->dynsym_count_ != -1U);
7921 if (this->stub_offsets_are_set_)
7922 return;
7924 unsigned int stub_size = this->stub_size();
7925 unsigned int offset = 0;
7926 for (typename Mips_stubs_entry_set::const_iterator
7927 p = this->symbols_.begin();
7928 p != this->symbols_.end();
7929 ++p, offset += stub_size)
7931 Mips_symbol<size>* mips_sym = *p;
7932 mips_sym->set_lazy_stub_offset(offset);
7934 this->stub_offsets_are_set_ = true;
7937 template<int size, bool big_endian>
7938 void
7939 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
7941 for (typename Mips_stubs_entry_set::const_iterator
7942 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7944 Mips_symbol<size>* sym = *p;
7945 if (sym->is_from_dynobj())
7946 sym->set_needs_dynsym_value();
7950 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
7951 // adjusts them as needed.
7953 template<int size, bool big_endian>
7954 void
7955 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
7957 const off_t offset = this->offset();
7958 const section_size_type oview_size =
7959 convert_to_section_size_type(this->data_size());
7960 unsigned char* const oview = of->get_output_view(offset, oview_size);
7962 bool big_stub = this->dynsym_count_ > 0x10000;
7964 unsigned char* pov = oview;
7965 for (typename Mips_stubs_entry_set::const_iterator
7966 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7968 Mips_symbol<size>* sym = *p;
7969 const uint32_t* lazy_stub;
7970 bool n64 = this->target_->is_output_n64();
7972 if (!this->target_->is_output_micromips())
7974 // Write standard (non-microMIPS) stub.
7975 if (!big_stub)
7977 if (sym->dynsym_index() & ~0x7fff)
7978 // Dynsym index is between 32K and 64K.
7979 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
7980 else
7981 // Dynsym index is less than 32K.
7982 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
7984 else
7985 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
7987 unsigned int i = 0;
7988 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7989 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
7990 pov += 8;
7992 i += 2;
7993 if (big_stub)
7995 // LUI instruction of the big stub. Paste high 16 bits of the
7996 // dynsym index.
7997 elfcpp::Swap<32, big_endian>::writeval(pov,
7998 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
7999 pov += 4;
8000 i += 1;
8002 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8003 // Last stub instruction. Paste low 16 bits of the dynsym index.
8004 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8005 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8006 pov += 8;
8008 else if (this->target_->use_32bit_micromips_instructions())
8010 // Write microMIPS stub in insn32 mode.
8011 if (!big_stub)
8013 if (sym->dynsym_index() & ~0x7fff)
8014 // Dynsym index is between 32K and 64K.
8015 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8016 : lazy_stub_micromips32_normal_2;
8017 else
8018 // Dynsym index is less than 32K.
8019 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8020 : lazy_stub_micromips32_normal_1;
8022 else
8023 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8024 : lazy_stub_micromips32_big;
8026 unsigned int i = 0;
8027 // First stub instruction. We emit 32-bit microMIPS instructions by
8028 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8029 // the instruction where the opcode is must always come first, for
8030 // both little and big endian.
8031 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8032 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8033 // Second stub instruction.
8034 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8035 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8036 pov += 8;
8037 i += 4;
8038 if (big_stub)
8040 // LUI instruction of the big stub. Paste high 16 bits of the
8041 // dynsym index.
8042 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8043 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8044 (sym->dynsym_index() >> 16) & 0x7fff);
8045 pov += 4;
8046 i += 2;
8048 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8049 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8050 // Last stub instruction. Paste low 16 bits of the dynsym index.
8051 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8052 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8053 sym->dynsym_index() & 0xffff);
8054 pov += 8;
8056 else
8058 // Write microMIPS stub.
8059 if (!big_stub)
8061 if (sym->dynsym_index() & ~0x7fff)
8062 // Dynsym index is between 32K and 64K.
8063 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8064 : lazy_stub_micromips_normal_2;
8065 else
8066 // Dynsym index is less than 32K.
8067 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8068 : lazy_stub_micromips_normal_1;
8070 else
8071 lazy_stub = n64 ? lazy_stub_micromips_big_n64
8072 : lazy_stub_micromips_big;
8074 unsigned int i = 0;
8075 // First stub instruction. We emit 32-bit microMIPS instructions by
8076 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8077 // the instruction where the opcode is must always come first, for
8078 // both little and big endian.
8079 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8080 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8081 // Second stub instruction.
8082 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8083 pov += 6;
8084 i += 3;
8085 if (big_stub)
8087 // LUI instruction of the big stub. Paste high 16 bits of the
8088 // dynsym index.
8089 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8090 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8091 (sym->dynsym_index() >> 16) & 0x7fff);
8092 pov += 4;
8093 i += 2;
8095 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8096 // Last stub instruction. Paste low 16 bits of the dynsym index.
8097 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8098 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8099 sym->dynsym_index() & 0xffff);
8100 pov += 6;
8104 // We always allocate 20 bytes for every stub, because final dynsym count is
8105 // not known in method do_finalize_sections. There are 4 unused bytes per
8106 // stub if final dynsym count is less than 0x10000.
8107 unsigned int used = pov - oview;
8108 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8109 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8111 // Fill the unused space with zeroes.
8112 // TODO(sasa): Can we strip unused bytes during the relaxation?
8113 if (unused > 0)
8114 memset(pov, 0, unused);
8116 of->write_output_view(offset, oview_size, oview);
8119 // Mips_output_section_reginfo methods.
8121 template<int size, bool big_endian>
8122 void
8123 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8125 off_t offset = this->offset();
8126 off_t data_size = this->data_size();
8128 unsigned char* view = of->get_output_view(offset, data_size);
8129 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8130 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8131 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8132 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8133 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8134 // Write the gp value.
8135 elfcpp::Swap<size, big_endian>::writeval(view + 20,
8136 this->target_->gp_value());
8138 of->write_output_view(offset, data_size, view);
8141 // Mips_output_section_abiflags methods.
8143 template<int size, bool big_endian>
8144 void
8145 Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8147 off_t offset = this->offset();
8148 off_t data_size = this->data_size();
8150 unsigned char* view = of->get_output_view(offset, data_size);
8151 elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8152 elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8153 elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8154 elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8155 elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8156 elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8157 elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8158 elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8159 elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8160 elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8161 elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8163 of->write_output_view(offset, data_size, view);
8166 // Mips_copy_relocs methods.
8168 // Emit any saved relocs.
8170 template<int sh_type, int size, bool big_endian>
8171 void
8172 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8173 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8174 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8176 for (typename Copy_relocs<sh_type, size, big_endian>::
8177 Copy_reloc_entries::iterator p = this->entries_.begin();
8178 p != this->entries_.end();
8179 ++p)
8180 emit_entry(*p, reloc_section, symtab, layout, target);
8182 // We no longer need the saved information.
8183 this->entries_.clear();
8186 // Emit the reloc if appropriate.
8188 template<int sh_type, int size, bool big_endian>
8189 void
8190 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8191 Copy_reloc_entry& entry,
8192 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8193 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8195 // If the symbol is no longer defined in a dynamic object, then we
8196 // emitted a COPY relocation, and we do not want to emit this
8197 // dynamic relocation.
8198 if (!entry.sym_->is_from_dynobj())
8199 return;
8201 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8202 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8203 || entry.reloc_type_ == elfcpp::R_MIPS_64);
8205 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8206 if (can_make_dynamic && !sym->has_static_relocs())
8208 Mips_relobj<size, big_endian>* object =
8209 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8210 target->got_section(symtab, layout)->record_global_got_symbol(
8211 sym, object, entry.reloc_type_, true, false);
8212 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8213 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8214 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8215 else
8216 target->rel_dyn_section(layout)->add_symbolless_global_addend(
8217 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8218 entry.shndx_, entry.address_);
8220 else
8221 this->make_copy_reloc(symtab, layout,
8222 static_cast<Sized_symbol<size>*>(entry.sym_),
8223 entry.relobj_,
8224 reloc_section);
8227 // Target_mips methods.
8229 // Return the value to use for a dynamic symbol which requires special
8230 // treatment. This is how we support equality comparisons of function
8231 // pointers across shared library boundaries, as described in the
8232 // processor specific ABI supplement.
8234 template<int size, bool big_endian>
8235 uint64_t
8236 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8238 uint64_t value = 0;
8239 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8241 if (!mips_sym->has_lazy_stub())
8243 if (mips_sym->has_plt_offset())
8245 // We distinguish between PLT entries and lazy-binding stubs by
8246 // giving the former an st_other value of STO_MIPS_PLT. Set the
8247 // value to the stub address if there are any relocations in the
8248 // binary where pointer equality matters.
8249 if (mips_sym->pointer_equality_needed())
8251 // Prefer a standard MIPS PLT entry.
8252 if (mips_sym->has_mips_plt_offset())
8253 value = this->plt_section()->mips_entry_address(mips_sym);
8254 else
8255 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8257 else
8258 value = 0;
8261 else
8263 // First, set stub offsets for symbols. This method expects that the
8264 // number of entries in dynamic symbol table is set.
8265 this->mips_stubs_section()->set_lazy_stub_offsets();
8267 // The run-time linker uses the st_value field of the symbol
8268 // to reset the global offset table entry for this external
8269 // to its stub address when unlinking a shared object.
8270 value = this->mips_stubs_section()->stub_address(mips_sym);
8273 if (mips_sym->has_mips16_fn_stub())
8275 // If we have a MIPS16 function with a stub, the dynamic symbol must
8276 // refer to the stub, since only the stub uses the standard calling
8277 // conventions.
8278 value = mips_sym->template
8279 get_mips16_fn_stub<big_endian>()->output_address();
8282 return value;
8285 // Get the dynamic reloc section, creating it if necessary. It's always
8286 // .rel.dyn, even for MIPS64.
8288 template<int size, bool big_endian>
8289 typename Target_mips<size, big_endian>::Reloc_section*
8290 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8292 if (this->rel_dyn_ == NULL)
8294 gold_assert(layout != NULL);
8295 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8296 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8297 elfcpp::SHF_ALLOC, this->rel_dyn_,
8298 ORDER_DYNAMIC_RELOCS, false);
8300 // First entry in .rel.dyn has to be null.
8301 // This is hack - we define dummy output data and set its address to 0,
8302 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8303 // This ensures that the entry is null.
8304 Output_data* od = new Output_data_zero_fill(0, 0);
8305 od->set_address(0);
8306 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8308 return this->rel_dyn_;
8311 // Get the GOT section, creating it if necessary.
8313 template<int size, bool big_endian>
8314 Mips_output_data_got<size, big_endian>*
8315 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8316 Layout* layout)
8318 if (this->got_ == NULL)
8320 gold_assert(symtab != NULL && layout != NULL);
8322 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8323 layout);
8324 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8325 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8326 elfcpp::SHF_MIPS_GPREL),
8327 this->got_, ORDER_DATA, false);
8329 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8330 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8331 Symbol_table::PREDEFINED,
8332 this->got_,
8333 0, 0, elfcpp::STT_OBJECT,
8334 elfcpp::STB_GLOBAL,
8335 elfcpp::STV_DEFAULT, 0,
8336 false, false);
8339 return this->got_;
8342 // Calculate value of _gp symbol.
8344 template<int size, bool big_endian>
8345 void
8346 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8348 if (this->gp_ != NULL)
8349 return;
8351 Output_data* section = layout->find_output_section(".got");
8352 if (section == NULL)
8354 // If there is no .got section, gp should be based on .sdata.
8355 // TODO(sasa): This is probably not needed. This was needed for older
8356 // MIPS architectures which accessed both GOT and .sdata section using
8357 // gp-relative addressing. Modern Mips Linux ELF architectures don't
8358 // access .sdata using gp-relative addressing.
8359 for (Layout::Section_list::const_iterator
8360 p = layout->section_list().begin();
8361 p != layout->section_list().end();
8362 ++p)
8364 if (strcmp((*p)->name(), ".sdata") == 0)
8366 section = *p;
8367 break;
8372 Sized_symbol<size>* gp =
8373 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
8374 if (gp != NULL)
8376 if (gp->source() != Symbol::IS_CONSTANT && section != NULL)
8377 gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0,
8378 elfcpp::STT_OBJECT,
8379 elfcpp::STB_GLOBAL,
8380 elfcpp::STV_DEFAULT, 0,
8381 false, false);
8382 this->gp_ = gp;
8384 else if (section != NULL)
8386 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8387 "_gp", NULL, Symbol_table::PREDEFINED,
8388 section, MIPS_GP_OFFSET, 0,
8389 elfcpp::STT_OBJECT,
8390 elfcpp::STB_GLOBAL,
8391 elfcpp::STV_DEFAULT,
8392 0, false, false));
8393 this->gp_ = gp;
8397 // Set the dynamic symbol indexes. INDEX is the index of the first
8398 // global dynamic symbol. Pointers to the symbols are stored into the
8399 // vector SYMS. The names are added to DYNPOOL. This returns an
8400 // updated dynamic symbol index.
8402 template<int size, bool big_endian>
8403 unsigned int
8404 Target_mips<size, big_endian>::do_set_dynsym_indexes(
8405 std::vector<Symbol*>* dyn_symbols, unsigned int index,
8406 std::vector<Symbol*>* syms, Stringpool* dynpool,
8407 Versions* versions, Symbol_table* symtab) const
8409 std::vector<Symbol*> non_got_symbols;
8410 std::vector<Symbol*> got_symbols;
8412 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8413 &got_symbols);
8415 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8416 p != non_got_symbols.end();
8417 ++p)
8419 Symbol* sym = *p;
8421 // Note that SYM may already have a dynamic symbol index, since
8422 // some symbols appear more than once in the symbol table, with
8423 // and without a version.
8425 if (!sym->has_dynsym_index())
8427 sym->set_dynsym_index(index);
8428 ++index;
8429 syms->push_back(sym);
8430 dynpool->add(sym->name(), false, NULL);
8432 // Record any version information.
8433 if (sym->version() != NULL)
8434 versions->record_version(symtab, dynpool, sym);
8436 // If the symbol is defined in a dynamic object and is
8437 // referenced in a regular object, then mark the dynamic
8438 // object as needed. This is used to implement --as-needed.
8439 if (sym->is_from_dynobj() && sym->in_reg())
8440 sym->object()->set_is_needed();
8444 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8445 p != got_symbols.end();
8446 ++p)
8448 Symbol* sym = *p;
8449 if (!sym->has_dynsym_index())
8451 // Record any version information.
8452 if (sym->version() != NULL)
8453 versions->record_version(symtab, dynpool, sym);
8457 index = versions->finalize(symtab, index, syms);
8459 int got_sym_count = 0;
8460 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8461 p != got_symbols.end();
8462 ++p)
8464 Symbol* sym = *p;
8466 if (!sym->has_dynsym_index())
8468 ++got_sym_count;
8469 sym->set_dynsym_index(index);
8470 ++index;
8471 syms->push_back(sym);
8472 dynpool->add(sym->name(), false, NULL);
8474 // If the symbol is defined in a dynamic object and is
8475 // referenced in a regular object, then mark the dynamic
8476 // object as needed. This is used to implement --as-needed.
8477 if (sym->is_from_dynobj() && sym->in_reg())
8478 sym->object()->set_is_needed();
8482 // Set index of the first symbol that has .got entry.
8483 this->got_->set_first_global_got_dynsym_index(
8484 got_sym_count > 0 ? index - got_sym_count : -1U);
8486 if (this->mips_stubs_ != NULL)
8487 this->mips_stubs_->set_dynsym_count(index);
8489 return index;
8492 // Create a PLT entry for a global symbol referenced by r_type relocation.
8494 template<int size, bool big_endian>
8495 void
8496 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8497 Layout* layout,
8498 Mips_symbol<size>* gsym,
8499 unsigned int r_type)
8501 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8502 return;
8504 if (this->plt_ == NULL)
8506 // Create the GOT section first.
8507 this->got_section(symtab, layout);
8509 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8510 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8511 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8512 this->got_plt_, ORDER_DATA, false);
8514 // The first two entries are reserved.
8515 this->got_plt_->set_current_data_size(2 * size/8);
8517 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8518 this->got_plt_,
8519 this);
8520 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8521 (elfcpp::SHF_ALLOC
8522 | elfcpp::SHF_EXECINSTR),
8523 this->plt_, ORDER_PLT, false);
8526 this->plt_->add_entry(gsym, r_type);
8530 // Get the .MIPS.stubs section, creating it if necessary.
8532 template<int size, bool big_endian>
8533 Mips_output_data_mips_stubs<size, big_endian>*
8534 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8536 if (this->mips_stubs_ == NULL)
8538 this->mips_stubs_ =
8539 new Mips_output_data_mips_stubs<size, big_endian>(this);
8540 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8541 (elfcpp::SHF_ALLOC
8542 | elfcpp::SHF_EXECINSTR),
8543 this->mips_stubs_, ORDER_PLT, false);
8545 return this->mips_stubs_;
8548 // Get the LA25 stub section, creating it if necessary.
8550 template<int size, bool big_endian>
8551 Mips_output_data_la25_stub<size, big_endian>*
8552 Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8554 if (this->la25_stub_ == NULL)
8556 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8557 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8558 (elfcpp::SHF_ALLOC
8559 | elfcpp::SHF_EXECINSTR),
8560 this->la25_stub_, ORDER_TEXT, false);
8562 return this->la25_stub_;
8565 // Process the relocations to determine unreferenced sections for
8566 // garbage collection.
8568 template<int size, bool big_endian>
8569 void
8570 Target_mips<size, big_endian>::gc_process_relocs(
8571 Symbol_table* symtab,
8572 Layout* layout,
8573 Sized_relobj_file<size, big_endian>* object,
8574 unsigned int data_shndx,
8575 unsigned int sh_type,
8576 const unsigned char* prelocs,
8577 size_t reloc_count,
8578 Output_section* output_section,
8579 bool needs_special_offset_handling,
8580 size_t local_symbol_count,
8581 const unsigned char* plocal_symbols)
8583 typedef Target_mips<size, big_endian> Mips;
8585 if (sh_type == elfcpp::SHT_REL)
8587 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8588 Classify_reloc;
8590 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8591 symtab,
8592 layout,
8593 this,
8594 object,
8595 data_shndx,
8596 prelocs,
8597 reloc_count,
8598 output_section,
8599 needs_special_offset_handling,
8600 local_symbol_count,
8601 plocal_symbols);
8603 else if (sh_type == elfcpp::SHT_RELA)
8605 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8606 Classify_reloc;
8608 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8609 symtab,
8610 layout,
8611 this,
8612 object,
8613 data_shndx,
8614 prelocs,
8615 reloc_count,
8616 output_section,
8617 needs_special_offset_handling,
8618 local_symbol_count,
8619 plocal_symbols);
8621 else
8622 gold_unreachable();
8625 // Scan relocations for a section.
8627 template<int size, bool big_endian>
8628 void
8629 Target_mips<size, big_endian>::scan_relocs(
8630 Symbol_table* symtab,
8631 Layout* layout,
8632 Sized_relobj_file<size, big_endian>* object,
8633 unsigned int data_shndx,
8634 unsigned int sh_type,
8635 const unsigned char* prelocs,
8636 size_t reloc_count,
8637 Output_section* output_section,
8638 bool needs_special_offset_handling,
8639 size_t local_symbol_count,
8640 const unsigned char* plocal_symbols)
8642 typedef Target_mips<size, big_endian> Mips;
8644 if (sh_type == elfcpp::SHT_REL)
8646 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8647 Classify_reloc;
8649 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8650 symtab,
8651 layout,
8652 this,
8653 object,
8654 data_shndx,
8655 prelocs,
8656 reloc_count,
8657 output_section,
8658 needs_special_offset_handling,
8659 local_symbol_count,
8660 plocal_symbols);
8662 else if (sh_type == elfcpp::SHT_RELA)
8664 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8665 Classify_reloc;
8667 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8668 symtab,
8669 layout,
8670 this,
8671 object,
8672 data_shndx,
8673 prelocs,
8674 reloc_count,
8675 output_section,
8676 needs_special_offset_handling,
8677 local_symbol_count,
8678 plocal_symbols);
8682 template<int size, bool big_endian>
8683 bool
8684 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8686 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8687 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8688 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8689 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8690 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8691 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8692 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
8693 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
8696 // Return the MACH for a MIPS e_flags value.
8697 template<int size, bool big_endian>
8698 unsigned int
8699 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8701 switch (flags & elfcpp::EF_MIPS_MACH)
8703 case elfcpp::E_MIPS_MACH_3900:
8704 return mach_mips3900;
8706 case elfcpp::E_MIPS_MACH_4010:
8707 return mach_mips4010;
8709 case elfcpp::E_MIPS_MACH_4100:
8710 return mach_mips4100;
8712 case elfcpp::E_MIPS_MACH_4111:
8713 return mach_mips4111;
8715 case elfcpp::E_MIPS_MACH_4120:
8716 return mach_mips4120;
8718 case elfcpp::E_MIPS_MACH_4650:
8719 return mach_mips4650;
8721 case elfcpp::E_MIPS_MACH_5400:
8722 return mach_mips5400;
8724 case elfcpp::E_MIPS_MACH_5500:
8725 return mach_mips5500;
8727 case elfcpp::E_MIPS_MACH_5900:
8728 return mach_mips5900;
8730 case elfcpp::E_MIPS_MACH_9000:
8731 return mach_mips9000;
8733 case elfcpp::E_MIPS_MACH_SB1:
8734 return mach_mips_sb1;
8736 case elfcpp::E_MIPS_MACH_LS2E:
8737 return mach_mips_loongson_2e;
8739 case elfcpp::E_MIPS_MACH_LS2F:
8740 return mach_mips_loongson_2f;
8742 case elfcpp::E_MIPS_MACH_LS3A:
8743 return mach_mips_loongson_3a;
8745 case elfcpp::E_MIPS_MACH_OCTEON3:
8746 return mach_mips_octeon3;
8748 case elfcpp::E_MIPS_MACH_OCTEON2:
8749 return mach_mips_octeon2;
8751 case elfcpp::E_MIPS_MACH_OCTEON:
8752 return mach_mips_octeon;
8754 case elfcpp::E_MIPS_MACH_XLR:
8755 return mach_mips_xlr;
8757 default:
8758 switch (flags & elfcpp::EF_MIPS_ARCH)
8760 default:
8761 case elfcpp::E_MIPS_ARCH_1:
8762 return mach_mips3000;
8764 case elfcpp::E_MIPS_ARCH_2:
8765 return mach_mips6000;
8767 case elfcpp::E_MIPS_ARCH_3:
8768 return mach_mips4000;
8770 case elfcpp::E_MIPS_ARCH_4:
8771 return mach_mips8000;
8773 case elfcpp::E_MIPS_ARCH_5:
8774 return mach_mips5;
8776 case elfcpp::E_MIPS_ARCH_32:
8777 return mach_mipsisa32;
8779 case elfcpp::E_MIPS_ARCH_64:
8780 return mach_mipsisa64;
8782 case elfcpp::E_MIPS_ARCH_32R2:
8783 return mach_mipsisa32r2;
8785 case elfcpp::E_MIPS_ARCH_32R6:
8786 return mach_mipsisa32r6;
8788 case elfcpp::E_MIPS_ARCH_64R2:
8789 return mach_mipsisa64r2;
8791 case elfcpp::E_MIPS_ARCH_64R6:
8792 return mach_mipsisa64r6;
8796 return 0;
8799 // Return the MACH for each .MIPS.abiflags ISA Extension.
8801 template<int size, bool big_endian>
8802 unsigned int
8803 Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8805 switch (isa_ext)
8807 case elfcpp::AFL_EXT_3900:
8808 return mach_mips3900;
8810 case elfcpp::AFL_EXT_4010:
8811 return mach_mips4010;
8813 case elfcpp::AFL_EXT_4100:
8814 return mach_mips4100;
8816 case elfcpp::AFL_EXT_4111:
8817 return mach_mips4111;
8819 case elfcpp::AFL_EXT_4120:
8820 return mach_mips4120;
8822 case elfcpp::AFL_EXT_4650:
8823 return mach_mips4650;
8825 case elfcpp::AFL_EXT_5400:
8826 return mach_mips5400;
8828 case elfcpp::AFL_EXT_5500:
8829 return mach_mips5500;
8831 case elfcpp::AFL_EXT_5900:
8832 return mach_mips5900;
8834 case elfcpp::AFL_EXT_10000:
8835 return mach_mips10000;
8837 case elfcpp::AFL_EXT_LOONGSON_2E:
8838 return mach_mips_loongson_2e;
8840 case elfcpp::AFL_EXT_LOONGSON_2F:
8841 return mach_mips_loongson_2f;
8843 case elfcpp::AFL_EXT_LOONGSON_3A:
8844 return mach_mips_loongson_3a;
8846 case elfcpp::AFL_EXT_SB1:
8847 return mach_mips_sb1;
8849 case elfcpp::AFL_EXT_OCTEON:
8850 return mach_mips_octeon;
8852 case elfcpp::AFL_EXT_OCTEONP:
8853 return mach_mips_octeonp;
8855 case elfcpp::AFL_EXT_OCTEON2:
8856 return mach_mips_octeon2;
8858 case elfcpp::AFL_EXT_XLR:
8859 return mach_mips_xlr;
8861 default:
8862 return mach_mips3000;
8866 // Return the .MIPS.abiflags value representing each ISA Extension.
8868 template<int size, bool big_endian>
8869 unsigned int
8870 Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
8872 switch (mips_mach)
8874 case mach_mips3900:
8875 return elfcpp::AFL_EXT_3900;
8877 case mach_mips4010:
8878 return elfcpp::AFL_EXT_4010;
8880 case mach_mips4100:
8881 return elfcpp::AFL_EXT_4100;
8883 case mach_mips4111:
8884 return elfcpp::AFL_EXT_4111;
8886 case mach_mips4120:
8887 return elfcpp::AFL_EXT_4120;
8889 case mach_mips4650:
8890 return elfcpp::AFL_EXT_4650;
8892 case mach_mips5400:
8893 return elfcpp::AFL_EXT_5400;
8895 case mach_mips5500:
8896 return elfcpp::AFL_EXT_5500;
8898 case mach_mips5900:
8899 return elfcpp::AFL_EXT_5900;
8901 case mach_mips10000:
8902 return elfcpp::AFL_EXT_10000;
8904 case mach_mips_loongson_2e:
8905 return elfcpp::AFL_EXT_LOONGSON_2E;
8907 case mach_mips_loongson_2f:
8908 return elfcpp::AFL_EXT_LOONGSON_2F;
8910 case mach_mips_loongson_3a:
8911 return elfcpp::AFL_EXT_LOONGSON_3A;
8913 case mach_mips_sb1:
8914 return elfcpp::AFL_EXT_SB1;
8916 case mach_mips_octeon:
8917 return elfcpp::AFL_EXT_OCTEON;
8919 case mach_mips_octeonp:
8920 return elfcpp::AFL_EXT_OCTEONP;
8922 case mach_mips_octeon3:
8923 return elfcpp::AFL_EXT_OCTEON3;
8925 case mach_mips_octeon2:
8926 return elfcpp::AFL_EXT_OCTEON2;
8928 case mach_mips_xlr:
8929 return elfcpp::AFL_EXT_XLR;
8931 default:
8932 return 0;
8936 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
8938 template<int size, bool big_endian>
8939 void
8940 Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
8941 elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
8943 int new_isa = 0;
8944 switch (e_flags & elfcpp::EF_MIPS_ARCH)
8946 case elfcpp::E_MIPS_ARCH_1:
8947 new_isa = this->level_rev(1, 0);
8948 break;
8949 case elfcpp::E_MIPS_ARCH_2:
8950 new_isa = this->level_rev(2, 0);
8951 break;
8952 case elfcpp::E_MIPS_ARCH_3:
8953 new_isa = this->level_rev(3, 0);
8954 break;
8955 case elfcpp::E_MIPS_ARCH_4:
8956 new_isa = this->level_rev(4, 0);
8957 break;
8958 case elfcpp::E_MIPS_ARCH_5:
8959 new_isa = this->level_rev(5, 0);
8960 break;
8961 case elfcpp::E_MIPS_ARCH_32:
8962 new_isa = this->level_rev(32, 1);
8963 break;
8964 case elfcpp::E_MIPS_ARCH_32R2:
8965 new_isa = this->level_rev(32, 2);
8966 break;
8967 case elfcpp::E_MIPS_ARCH_32R6:
8968 new_isa = this->level_rev(32, 6);
8969 break;
8970 case elfcpp::E_MIPS_ARCH_64:
8971 new_isa = this->level_rev(64, 1);
8972 break;
8973 case elfcpp::E_MIPS_ARCH_64R2:
8974 new_isa = this->level_rev(64, 2);
8975 break;
8976 case elfcpp::E_MIPS_ARCH_64R6:
8977 new_isa = this->level_rev(64, 6);
8978 break;
8979 default:
8980 gold_error(_("%s: Unknown architecture %s"), name.c_str(),
8981 this->elf_mips_mach_name(e_flags));
8984 if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
8986 // Decode a single value into level and revision.
8987 abiflags->isa_level = new_isa >> 3;
8988 abiflags->isa_rev = new_isa & 0x7;
8991 // Update the isa_ext if needed.
8992 if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
8993 this->elf_mips_mach(e_flags)))
8994 abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
8997 // Infer the content of the ABI flags based on the elf header.
8999 template<int size, bool big_endian>
9000 void
9001 Target_mips<size, big_endian>::infer_abiflags(
9002 Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9004 const Attributes_section_data* pasd = relobj->attributes_section_data();
9005 int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9006 elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9008 this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9009 if (pasd != NULL)
9011 // Read fp_abi from the .gnu.attribute section.
9012 const Object_attribute* attr =
9013 pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9014 attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9017 abiflags->fp_abi = attr_fp_abi;
9018 abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9019 abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9020 abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9021 : elfcpp::AFL_REG_64;
9023 if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9024 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9025 || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9026 && abiflags->gpr_size == elfcpp::AFL_REG_32))
9027 abiflags->cpr1_size = elfcpp::AFL_REG_32;
9028 else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9029 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9030 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9031 abiflags->cpr1_size = elfcpp::AFL_REG_64;
9033 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9034 abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9035 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9036 abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9037 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9038 abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9040 if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9041 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9042 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9043 && abiflags->isa_level >= 32
9044 && abiflags->isa_ext != elfcpp::AFL_EXT_LOONGSON_3A)
9045 abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9048 // Create abiflags from elf header or from .MIPS.abiflags section.
9050 template<int size, bool big_endian>
9051 void
9052 Target_mips<size, big_endian>::create_abiflags(
9053 Mips_relobj<size, big_endian>* relobj,
9054 Mips_abiflags<big_endian>* abiflags)
9056 Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9057 Mips_abiflags<big_endian> header_abiflags;
9059 this->infer_abiflags(relobj, &header_abiflags);
9061 if (sec_abiflags == NULL)
9063 // If there is no input .MIPS.abiflags section, use abiflags created
9064 // from elf header.
9065 *abiflags = header_abiflags;
9066 return;
9069 this->has_abiflags_section_ = true;
9071 // It is not possible to infer the correct ISA revision for R3 or R5
9072 // so drop down to R2 for the checks.
9073 unsigned char isa_rev = sec_abiflags->isa_rev;
9074 if (isa_rev == 3 || isa_rev == 5)
9075 isa_rev = 2;
9077 // Check compatibility between abiflags created from elf header
9078 // and abiflags from .MIPS.abiflags section in this object file.
9079 if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9080 < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9081 gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9082 relobj->name().c_str());
9083 if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9084 && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9085 gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9086 ".MIPS.abiflags"), relobj->name().c_str());
9087 if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9088 gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9089 relobj->name().c_str());
9090 // The isa_ext is allowed to be an extension of what can be inferred
9091 // from e_flags.
9092 if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9093 this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9094 gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9095 ".MIPS.abiflags"), relobj->name().c_str());
9096 if (sec_abiflags->flags2 != 0)
9097 gold_warning(_("%s: Unexpected flag in the flags2 field of "
9098 ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9099 sec_abiflags->flags2);
9100 // Use abiflags from .MIPS.abiflags section.
9101 *abiflags = *sec_abiflags;
9104 // Return the meaning of fp_abi, or "unknown" if not known.
9106 template<int size, bool big_endian>
9107 const char*
9108 Target_mips<size, big_endian>::fp_abi_string(int fp)
9110 switch (fp)
9112 case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9113 return "-mdouble-float";
9114 case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9115 return "-msingle-float";
9116 case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9117 return "-msoft-float";
9118 case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9119 return _("-mips32r2 -mfp64 (12 callee-saved)");
9120 case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9121 return "-mfpxx";
9122 case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9123 return "-mgp32 -mfp64";
9124 case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9125 return "-mgp32 -mfp64 -mno-odd-spreg";
9126 default:
9127 return "unknown";
9131 // Select fp_abi.
9133 template<int size, bool big_endian>
9135 Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9136 int out_fp)
9138 if (in_fp == out_fp)
9139 return out_fp;
9141 if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9142 return in_fp;
9143 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9144 && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9145 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9146 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9147 return in_fp;
9148 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9149 && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9150 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9151 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9152 return out_fp; // Keep the current setting.
9153 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9154 && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9155 return in_fp;
9156 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9157 && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9158 return out_fp; // Keep the current setting.
9159 else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9160 gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9161 fp_abi_string(in_fp), fp_abi_string(out_fp));
9162 return out_fp;
9165 // Merge attributes from input object.
9167 template<int size, bool big_endian>
9168 void
9169 Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9170 const Attributes_section_data* pasd)
9172 // Return if there is no attributes section data.
9173 if (pasd == NULL)
9174 return;
9176 // If output has no object attributes, just copy.
9177 if (this->attributes_section_data_ == NULL)
9179 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9180 return;
9183 Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9184 Object_attribute::OBJ_ATTR_GNU);
9186 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9187 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9189 // Merge Tag_compatibility attributes and any common GNU ones.
9190 this->attributes_section_data_->merge(name.c_str(), pasd);
9193 // Merge abiflags from input object.
9195 template<int size, bool big_endian>
9196 void
9197 Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9198 Mips_abiflags<big_endian>* in_abiflags)
9200 // If output has no abiflags, just copy.
9201 if (this->abiflags_ == NULL)
9203 this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9204 return;
9207 this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9208 this->abiflags_->fp_abi);
9210 // Merge abiflags.
9211 this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9212 in_abiflags->isa_level);
9213 this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9214 in_abiflags->isa_rev);
9215 this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9216 in_abiflags->gpr_size);
9217 this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9218 in_abiflags->cpr1_size);
9219 this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9220 in_abiflags->cpr2_size);
9221 this->abiflags_->ases |= in_abiflags->ases;
9222 this->abiflags_->flags1 |= in_abiflags->flags1;
9225 // Check whether machine EXTENSION is an extension of machine BASE.
9226 template<int size, bool big_endian>
9227 bool
9228 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9229 unsigned int extension)
9231 if (extension == base)
9232 return true;
9234 if ((base == mach_mipsisa32)
9235 && this->mips_mach_extends(mach_mipsisa64, extension))
9236 return true;
9238 if ((base == mach_mipsisa32r2)
9239 && this->mips_mach_extends(mach_mipsisa64r2, extension))
9240 return true;
9242 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9243 if (extension == this->mips_mach_extensions_[i].first)
9245 extension = this->mips_mach_extensions_[i].second;
9246 if (extension == base)
9247 return true;
9250 return false;
9253 // Merge file header flags from input object.
9255 template<int size, bool big_endian>
9256 void
9257 Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9258 elfcpp::Elf_Word in_flags)
9260 // If flags are not set yet, just copy them.
9261 if (!this->are_processor_specific_flags_set())
9263 this->set_processor_specific_flags(in_flags);
9264 this->mach_ = this->elf_mips_mach(in_flags);
9265 return;
9268 elfcpp::Elf_Word new_flags = in_flags;
9269 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9270 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9271 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9273 // Check flag compatibility.
9274 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9275 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9277 // Some IRIX 6 BSD-compatibility objects have this bit set. It
9278 // doesn't seem to matter.
9279 new_flags &= ~elfcpp::EF_MIPS_XGOT;
9280 old_flags &= ~elfcpp::EF_MIPS_XGOT;
9282 // MIPSpro generates ucode info in n64 objects. Again, we should
9283 // just be able to ignore this.
9284 new_flags &= ~elfcpp::EF_MIPS_UCODE;
9285 old_flags &= ~elfcpp::EF_MIPS_UCODE;
9287 if (new_flags == old_flags)
9289 this->set_processor_specific_flags(merged_flags);
9290 return;
9293 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9294 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9295 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9296 name.c_str());
9298 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9299 merged_flags |= elfcpp::EF_MIPS_CPIC;
9300 if (!(new_flags & elfcpp::EF_MIPS_PIC))
9301 merged_flags &= ~elfcpp::EF_MIPS_PIC;
9303 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9304 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9306 // Compare the ISAs.
9307 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9308 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9309 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9311 // Output ISA isn't the same as, or an extension of, input ISA.
9312 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9314 // Copy the architecture info from input object to output. Also copy
9315 // the 32-bit flag (if set) so that we continue to recognise
9316 // output as a 32-bit binary.
9317 this->mach_ = this->elf_mips_mach(in_flags);
9318 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9319 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9320 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9322 // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9323 this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9325 // Copy across the ABI flags if output doesn't use them
9326 // and if that was what caused us to treat input object as 32-bit.
9327 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9328 && this->mips_32bit_flags(new_flags)
9329 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9330 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9332 else
9333 // The ISAs aren't compatible.
9334 gold_error(_("%s: linking %s module with previous %s modules"),
9335 name.c_str(), this->elf_mips_mach_name(in_flags),
9336 this->elf_mips_mach_name(merged_flags));
9339 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9340 | elfcpp::EF_MIPS_32BITMODE));
9341 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9342 | elfcpp::EF_MIPS_32BITMODE));
9344 // Compare ABIs.
9345 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9347 // Only error if both are set (to different values).
9348 if ((new_flags & elfcpp::EF_MIPS_ABI)
9349 && (old_flags & elfcpp::EF_MIPS_ABI))
9350 gold_error(_("%s: ABI mismatch: linking %s module with "
9351 "previous %s modules"), name.c_str(),
9352 this->elf_mips_abi_name(in_flags),
9353 this->elf_mips_abi_name(merged_flags));
9355 new_flags &= ~elfcpp::EF_MIPS_ABI;
9356 old_flags &= ~elfcpp::EF_MIPS_ABI;
9359 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
9360 // and allow arbitrary mixing of the remaining ASEs (retain the union).
9361 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9362 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9364 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9365 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9366 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9367 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9368 int micro_mis = old_m16 && new_micro;
9369 int m16_mis = old_micro && new_m16;
9371 if (m16_mis || micro_mis)
9372 gold_error(_("%s: ASE mismatch: linking %s module with "
9373 "previous %s modules"), name.c_str(),
9374 m16_mis ? "MIPS16" : "microMIPS",
9375 m16_mis ? "microMIPS" : "MIPS16");
9377 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9379 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9380 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9383 // Compare NaN encodings.
9384 if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9386 gold_error(_("%s: linking %s module with previous %s modules"),
9387 name.c_str(),
9388 (new_flags & elfcpp::EF_MIPS_NAN2008
9389 ? "-mnan=2008" : "-mnan=legacy"),
9390 (old_flags & elfcpp::EF_MIPS_NAN2008
9391 ? "-mnan=2008" : "-mnan=legacy"));
9393 new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9394 old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9397 // Compare FP64 state.
9398 if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9400 gold_error(_("%s: linking %s module with previous %s modules"),
9401 name.c_str(),
9402 (new_flags & elfcpp::EF_MIPS_FP64
9403 ? "-mfp64" : "-mfp32"),
9404 (old_flags & elfcpp::EF_MIPS_FP64
9405 ? "-mfp64" : "-mfp32"));
9407 new_flags &= ~elfcpp::EF_MIPS_FP64;
9408 old_flags &= ~elfcpp::EF_MIPS_FP64;
9411 // Warn about any other mismatches.
9412 if (new_flags != old_flags)
9413 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9414 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9416 this->set_processor_specific_flags(merged_flags);
9419 // Adjust ELF file header.
9421 template<int size, bool big_endian>
9422 void
9423 Target_mips<size, big_endian>::do_adjust_elf_header(
9424 unsigned char* view,
9425 int len)
9427 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9429 elfcpp::Ehdr<size, big_endian> ehdr(view);
9430 unsigned char e_ident[elfcpp::EI_NIDENT];
9431 elfcpp::Elf_Word flags = this->processor_specific_flags();
9432 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9434 unsigned char ei_abiversion = 0;
9435 elfcpp::Elf_Half type = ehdr.get_e_type();
9436 if (type == elfcpp::ET_EXEC
9437 && parameters->options().copyreloc()
9438 && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9439 == elfcpp::EF_MIPS_CPIC)
9440 ei_abiversion = 1;
9442 if (this->abiflags_ != NULL
9443 && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9444 || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9445 ei_abiversion = 3;
9447 e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9448 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
9449 oehdr.put_e_ident(e_ident);
9451 if (this->entry_symbol_is_compressed_)
9452 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9455 // do_make_elf_object to override the same function in the base class.
9456 // We need to use a target-specific sub-class of
9457 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9458 // Hence we need to have our own ELF object creation.
9460 template<int size, bool big_endian>
9461 Object*
9462 Target_mips<size, big_endian>::do_make_elf_object(
9463 const std::string& name,
9464 Input_file* input_file,
9465 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9467 int et = ehdr.get_e_type();
9468 // ET_EXEC files are valid input for --just-symbols/-R,
9469 // and we treat them as relocatable objects.
9470 if (et == elfcpp::ET_REL
9471 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9473 Mips_relobj<size, big_endian>* obj =
9474 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9475 obj->setup();
9476 return obj;
9478 else if (et == elfcpp::ET_DYN)
9480 // TODO(sasa): Should we create Mips_dynobj?
9481 return Target::do_make_elf_object(name, input_file, offset, ehdr);
9483 else
9485 gold_error(_("%s: unsupported ELF file type %d"),
9486 name.c_str(), et);
9487 return NULL;
9491 // Finalize the sections.
9493 template <int size, bool big_endian>
9494 void
9495 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9496 const Input_objects* input_objects,
9497 Symbol_table* symtab)
9499 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9500 // DT_FINI have correct values.
9501 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9502 symtab->lookup(parameters->options().init()));
9503 if (init != NULL && (init->is_mips16() || init->is_micromips()))
9504 init->set_value(init->value() | 1);
9505 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9506 symtab->lookup(parameters->options().fini()));
9507 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9508 fini->set_value(fini->value() | 1);
9510 // Check whether the entry symbol is mips16 or micromips. This is needed to
9511 // adjust entry address in ELF header.
9512 Mips_symbol<size>* entry =
9513 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9514 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9515 || entry->is_micromips()));
9517 if (!parameters->doing_static_link()
9518 && (strcmp(parameters->options().hash_style(), "gnu") == 0
9519 || strcmp(parameters->options().hash_style(), "both") == 0))
9521 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9522 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
9523 // MIPS ABI requires a mapping between the GOT and the symbol table.
9524 gold_error(".gnu.hash is incompatible with the MIPS ABI");
9527 // Check whether the final section that was scanned has HI16 or GOT16
9528 // relocations without the corresponding LO16 part.
9529 if (this->got16_addends_.size() > 0)
9530 gold_error("Can't find matching LO16 reloc");
9532 // Set _gp value.
9533 this->set_gp(layout, symtab);
9535 // Check for any mips16 stub sections that we can discard.
9536 if (!parameters->options().relocatable())
9538 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9539 p != input_objects->relobj_end();
9540 ++p)
9542 Mips_relobj<size, big_endian>* object =
9543 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9544 object->discard_mips16_stub_sections(symtab);
9548 Valtype gprmask = 0;
9549 Valtype cprmask1 = 0;
9550 Valtype cprmask2 = 0;
9551 Valtype cprmask3 = 0;
9552 Valtype cprmask4 = 0;
9553 bool has_reginfo_section = false;
9555 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9556 p != input_objects->relobj_end();
9557 ++p)
9559 Mips_relobj<size, big_endian>* relobj =
9560 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9562 // Merge .reginfo contents of input objects.
9563 if (relobj->has_reginfo_section())
9565 has_reginfo_section = true;
9566 gprmask |= relobj->gprmask();
9567 cprmask1 |= relobj->cprmask1();
9568 cprmask2 |= relobj->cprmask2();
9569 cprmask3 |= relobj->cprmask3();
9570 cprmask4 |= relobj->cprmask4();
9573 Input_file::Format format = relobj->input_file()->format();
9574 if (format != Input_file::FORMAT_ELF)
9575 continue;
9577 // If all input sections will be discarded, don't use this object
9578 // file for merging processor specific flags.
9579 bool should_merge_processor_specific_flags = false;
9581 for (unsigned int i = 1; i < relobj->shnum(); ++i)
9582 if (relobj->output_section(i) != NULL)
9584 should_merge_processor_specific_flags = true;
9585 break;
9588 if (!should_merge_processor_specific_flags)
9589 continue;
9591 // Merge processor specific flags.
9592 Mips_abiflags<big_endian> in_abiflags;
9594 this->create_abiflags(relobj, &in_abiflags);
9595 this->merge_obj_e_flags(relobj->name(),
9596 relobj->processor_specific_flags());
9597 this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9598 this->merge_obj_attributes(relobj->name(),
9599 relobj->attributes_section_data());
9602 // Create a .gnu.attributes section if we have merged any attributes
9603 // from inputs.
9604 if (this->attributes_section_data_ != NULL)
9606 Output_attributes_section_data* attributes_section =
9607 new Output_attributes_section_data(*this->attributes_section_data_);
9608 layout->add_output_section_data(".gnu.attributes",
9609 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9610 attributes_section, ORDER_INVALID, false);
9613 // Create .MIPS.abiflags output section if there is an input section.
9614 if (this->has_abiflags_section_)
9616 Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9617 new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9619 Output_section* os =
9620 layout->add_output_section_data(".MIPS.abiflags",
9621 elfcpp::SHT_MIPS_ABIFLAGS,
9622 elfcpp::SHF_ALLOC,
9623 abiflags_section, ORDER_INVALID, false);
9625 if (!parameters->options().relocatable() && os != NULL)
9627 Output_segment* abiflags_segment =
9628 layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9629 abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9633 if (has_reginfo_section && !parameters->options().gc_sections())
9635 // Create .reginfo output section.
9636 Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9637 new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9638 cprmask1, cprmask2,
9639 cprmask3, cprmask4);
9641 Output_section* os =
9642 layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9643 elfcpp::SHF_ALLOC, reginfo_section,
9644 ORDER_INVALID, false);
9646 if (!parameters->options().relocatable() && os != NULL)
9648 Output_segment* reginfo_segment =
9649 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9650 elfcpp::PF_R);
9651 reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9655 if (this->plt_ != NULL)
9657 // Set final PLT offsets for symbols.
9658 this->plt_section()->set_plt_offsets();
9660 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9661 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9662 // there are no standard PLT entries present.
9663 unsigned char nonvis = 0;
9664 if (this->is_output_micromips()
9665 && !this->plt_section()->has_standard_entries())
9666 nonvis = elfcpp::STO_MICROMIPS >> 2;
9667 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9668 Symbol_table::PREDEFINED,
9669 this->plt_,
9670 0, 0, elfcpp::STT_FUNC,
9671 elfcpp::STB_LOCAL,
9672 elfcpp::STV_DEFAULT, nonvis,
9673 false, false);
9676 if (this->mips_stubs_ != NULL)
9678 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9679 unsigned char nonvis = 0;
9680 if (this->is_output_micromips())
9681 nonvis = elfcpp::STO_MICROMIPS >> 2;
9682 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9683 Symbol_table::PREDEFINED,
9684 this->mips_stubs_,
9685 0, 0, elfcpp::STT_FUNC,
9686 elfcpp::STB_LOCAL,
9687 elfcpp::STV_DEFAULT, nonvis,
9688 false, false);
9691 if (!parameters->options().relocatable() && !parameters->doing_static_link())
9692 // In case there is no .got section, create one.
9693 this->got_section(symtab, layout);
9695 // Emit any relocs we saved in an attempt to avoid generating COPY
9696 // relocs.
9697 if (this->copy_relocs_.any_saved_relocs())
9698 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9699 this);
9701 // Emit dynamic relocs.
9702 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9703 p != this->dyn_relocs_.end();
9704 ++p)
9705 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9707 if (this->has_got_section())
9708 this->got_section()->lay_out_got(layout, symtab, input_objects);
9710 if (this->mips_stubs_ != NULL)
9711 this->mips_stubs_->set_needs_dynsym_value();
9713 // Check for functions that might need $25 to be valid on entry.
9714 // TODO(sasa): Can we do this without iterating over all symbols?
9715 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9716 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9717 symtab));
9719 // Add NULL segment.
9720 if (!parameters->options().relocatable())
9721 layout->make_output_segment(elfcpp::PT_NULL, 0);
9723 // Fill in some more dynamic tags.
9724 // TODO(sasa): Add more dynamic tags.
9725 const Reloc_section* rel_plt = (this->plt_ == NULL
9726 ? NULL : this->plt_->rel_plt());
9727 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9728 this->rel_dyn_, true, false);
9730 Output_data_dynamic* const odyn = layout->dynamic_data();
9731 if (odyn != NULL
9732 && !parameters->options().relocatable()
9733 && !parameters->doing_static_link())
9735 unsigned int d_val;
9736 // This element holds a 32-bit version id for the Runtime
9737 // Linker Interface. This will start at integer value 1.
9738 d_val = 0x01;
9739 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9741 // Dynamic flags
9742 d_val = elfcpp::RHF_NOTPOT;
9743 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9745 // Save layout for using when emiting custom dynamic tags.
9746 this->layout_ = layout;
9748 // This member holds the base address of the segment.
9749 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9751 // This member holds the number of entries in the .dynsym section.
9752 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9754 // This member holds the index of the first dynamic symbol
9755 // table entry that corresponds to an entry in the global offset table.
9756 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9758 // This member holds the number of local GOT entries.
9759 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9760 this->got_->get_local_gotno());
9762 if (this->plt_ != NULL)
9763 // DT_MIPS_PLTGOT dynamic tag
9764 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
9766 if (!parameters->options().shared())
9768 this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9770 layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9771 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9772 this->rld_map_, ORDER_INVALID, false);
9774 // __RLD_MAP will be filled in by the runtime loader to contain
9775 // a pointer to the _r_debug structure.
9776 Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9777 Symbol_table::PREDEFINED,
9778 this->rld_map_,
9779 0, 0, elfcpp::STT_OBJECT,
9780 elfcpp::STB_GLOBAL,
9781 elfcpp::STV_DEFAULT, 0,
9782 false, false);
9784 rld_map->set_needs_dynsym_entry();
9786 if (!parameters->options().pie())
9787 // This member holds the absolute address of the debug pointer.
9788 odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9789 else
9790 // This member holds the offset to the debug pointer,
9791 // relative to the address of the tag.
9792 odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9797 // Get the custom dynamic tag value.
9798 template<int size, bool big_endian>
9799 unsigned int
9800 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9802 switch (tag)
9804 case elfcpp::DT_MIPS_BASE_ADDRESS:
9806 // The base address of the segment.
9807 // At this point, the segment list has been sorted into final order,
9808 // so just return vaddr of the first readable PT_LOAD segment.
9809 Output_segment* seg =
9810 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9811 gold_assert(seg != NULL);
9812 return seg->vaddr();
9815 case elfcpp::DT_MIPS_SYMTABNO:
9816 // The number of entries in the .dynsym section.
9817 return this->get_dt_mips_symtabno();
9819 case elfcpp::DT_MIPS_GOTSYM:
9821 // The index of the first dynamic symbol table entry that corresponds
9822 // to an entry in the GOT.
9823 if (this->got_->first_global_got_dynsym_index() != -1U)
9824 return this->got_->first_global_got_dynsym_index();
9825 else
9826 // In case if we don't have global GOT symbols we default to setting
9827 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9828 return this->get_dt_mips_symtabno();
9831 case elfcpp::DT_MIPS_RLD_MAP_REL:
9833 // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9834 // relative to the address of the tag.
9835 Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9836 unsigned int entry_offset =
9837 odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9838 gold_assert(entry_offset != -1U);
9839 return this->rld_map_->address() - (odyn->address() + entry_offset);
9841 default:
9842 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9845 return (unsigned int)-1;
9848 // Relocate section data.
9850 template<int size, bool big_endian>
9851 void
9852 Target_mips<size, big_endian>::relocate_section(
9853 const Relocate_info<size, big_endian>* relinfo,
9854 unsigned int sh_type,
9855 const unsigned char* prelocs,
9856 size_t reloc_count,
9857 Output_section* output_section,
9858 bool needs_special_offset_handling,
9859 unsigned char* view,
9860 Mips_address address,
9861 section_size_type view_size,
9862 const Reloc_symbol_changes* reloc_symbol_changes)
9864 typedef Target_mips<size, big_endian> Mips;
9865 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9867 if (sh_type == elfcpp::SHT_REL)
9869 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9870 Classify_reloc;
9872 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9873 gold::Default_comdat_behavior, Classify_reloc>(
9874 relinfo,
9875 this,
9876 prelocs,
9877 reloc_count,
9878 output_section,
9879 needs_special_offset_handling,
9880 view,
9881 address,
9882 view_size,
9883 reloc_symbol_changes);
9885 else if (sh_type == elfcpp::SHT_RELA)
9887 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9888 Classify_reloc;
9890 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9891 gold::Default_comdat_behavior, Classify_reloc>(
9892 relinfo,
9893 this,
9894 prelocs,
9895 reloc_count,
9896 output_section,
9897 needs_special_offset_handling,
9898 view,
9899 address,
9900 view_size,
9901 reloc_symbol_changes);
9905 // Return the size of a relocation while scanning during a relocatable
9906 // link.
9908 unsigned int
9909 mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
9911 switch (r_type)
9913 case elfcpp::R_MIPS_NONE:
9914 case elfcpp::R_MIPS_TLS_DTPMOD64:
9915 case elfcpp::R_MIPS_TLS_DTPREL64:
9916 case elfcpp::R_MIPS_TLS_TPREL64:
9917 return 0;
9919 case elfcpp::R_MIPS_32:
9920 case elfcpp::R_MIPS_TLS_DTPMOD32:
9921 case elfcpp::R_MIPS_TLS_DTPREL32:
9922 case elfcpp::R_MIPS_TLS_TPREL32:
9923 case elfcpp::R_MIPS_REL32:
9924 case elfcpp::R_MIPS_PC32:
9925 case elfcpp::R_MIPS_GPREL32:
9926 case elfcpp::R_MIPS_JALR:
9927 case elfcpp::R_MIPS_EH:
9928 return 4;
9930 case elfcpp::R_MIPS_16:
9931 case elfcpp::R_MIPS_HI16:
9932 case elfcpp::R_MIPS_LO16:
9933 case elfcpp::R_MIPS_GPREL16:
9934 case elfcpp::R_MIPS16_HI16:
9935 case elfcpp::R_MIPS16_LO16:
9936 case elfcpp::R_MIPS_PC16:
9937 case elfcpp::R_MIPS_PCHI16:
9938 case elfcpp::R_MIPS_PCLO16:
9939 case elfcpp::R_MIPS_GOT16:
9940 case elfcpp::R_MIPS16_GOT16:
9941 case elfcpp::R_MIPS_CALL16:
9942 case elfcpp::R_MIPS16_CALL16:
9943 case elfcpp::R_MIPS_GOT_HI16:
9944 case elfcpp::R_MIPS_CALL_HI16:
9945 case elfcpp::R_MIPS_GOT_LO16:
9946 case elfcpp::R_MIPS_CALL_LO16:
9947 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
9948 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
9949 case elfcpp::R_MIPS_TLS_TPREL_HI16:
9950 case elfcpp::R_MIPS_TLS_TPREL_LO16:
9951 case elfcpp::R_MIPS16_GPREL:
9952 case elfcpp::R_MIPS_GOT_DISP:
9953 case elfcpp::R_MIPS_LITERAL:
9954 case elfcpp::R_MIPS_GOT_PAGE:
9955 case elfcpp::R_MIPS_GOT_OFST:
9956 case elfcpp::R_MIPS_TLS_GD:
9957 case elfcpp::R_MIPS_TLS_LDM:
9958 case elfcpp::R_MIPS_TLS_GOTTPREL:
9959 return 2;
9961 // These relocations are not byte sized
9962 case elfcpp::R_MIPS_26:
9963 case elfcpp::R_MIPS16_26:
9964 case elfcpp::R_MIPS_PC21_S2:
9965 case elfcpp::R_MIPS_PC26_S2:
9966 case elfcpp::R_MIPS_PC18_S3:
9967 case elfcpp::R_MIPS_PC19_S2:
9968 return 4;
9970 case elfcpp::R_MIPS_COPY:
9971 case elfcpp::R_MIPS_JUMP_SLOT:
9972 object->error(_("unexpected reloc %u in object file"), r_type);
9973 return 0;
9975 default:
9976 object->error(_("unsupported reloc %u in object file"), r_type);
9977 return 0;
9981 // Scan the relocs during a relocatable link.
9983 template<int size, bool big_endian>
9984 void
9985 Target_mips<size, big_endian>::scan_relocatable_relocs(
9986 Symbol_table* symtab,
9987 Layout* layout,
9988 Sized_relobj_file<size, big_endian>* object,
9989 unsigned int data_shndx,
9990 unsigned int sh_type,
9991 const unsigned char* prelocs,
9992 size_t reloc_count,
9993 Output_section* output_section,
9994 bool needs_special_offset_handling,
9995 size_t local_symbol_count,
9996 const unsigned char* plocal_symbols,
9997 Relocatable_relocs* rr)
9999 if (sh_type == elfcpp::SHT_REL)
10001 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10002 Classify_reloc;
10003 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10004 Scan_relocatable_relocs;
10006 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10007 symtab,
10008 layout,
10009 object,
10010 data_shndx,
10011 prelocs,
10012 reloc_count,
10013 output_section,
10014 needs_special_offset_handling,
10015 local_symbol_count,
10016 plocal_symbols,
10017 rr);
10019 else if (sh_type == elfcpp::SHT_RELA)
10021 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10022 Classify_reloc;
10023 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10024 Scan_relocatable_relocs;
10026 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10027 symtab,
10028 layout,
10029 object,
10030 data_shndx,
10031 prelocs,
10032 reloc_count,
10033 output_section,
10034 needs_special_offset_handling,
10035 local_symbol_count,
10036 plocal_symbols,
10037 rr);
10039 else
10040 gold_unreachable();
10043 // Scan the relocs for --emit-relocs.
10045 template<int size, bool big_endian>
10046 void
10047 Target_mips<size, big_endian>::emit_relocs_scan(
10048 Symbol_table* symtab,
10049 Layout* layout,
10050 Sized_relobj_file<size, big_endian>* object,
10051 unsigned int data_shndx,
10052 unsigned int sh_type,
10053 const unsigned char* prelocs,
10054 size_t reloc_count,
10055 Output_section* output_section,
10056 bool needs_special_offset_handling,
10057 size_t local_symbol_count,
10058 const unsigned char* plocal_syms,
10059 Relocatable_relocs* rr)
10061 if (sh_type == elfcpp::SHT_REL)
10063 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10064 Classify_reloc;
10065 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10066 Emit_relocs_strategy;
10068 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10069 symtab,
10070 layout,
10071 object,
10072 data_shndx,
10073 prelocs,
10074 reloc_count,
10075 output_section,
10076 needs_special_offset_handling,
10077 local_symbol_count,
10078 plocal_syms,
10079 rr);
10081 else if (sh_type == elfcpp::SHT_RELA)
10083 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10084 Classify_reloc;
10085 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10086 Emit_relocs_strategy;
10088 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10089 symtab,
10090 layout,
10091 object,
10092 data_shndx,
10093 prelocs,
10094 reloc_count,
10095 output_section,
10096 needs_special_offset_handling,
10097 local_symbol_count,
10098 plocal_syms,
10099 rr);
10101 else
10102 gold_unreachable();
10105 // Emit relocations for a section.
10107 template<int size, bool big_endian>
10108 void
10109 Target_mips<size, big_endian>::relocate_relocs(
10110 const Relocate_info<size, big_endian>* relinfo,
10111 unsigned int sh_type,
10112 const unsigned char* prelocs,
10113 size_t reloc_count,
10114 Output_section* output_section,
10115 typename elfcpp::Elf_types<size>::Elf_Off
10116 offset_in_output_section,
10117 unsigned char* view,
10118 Mips_address view_address,
10119 section_size_type view_size,
10120 unsigned char* reloc_view,
10121 section_size_type reloc_view_size)
10123 if (sh_type == elfcpp::SHT_REL)
10125 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10126 Classify_reloc;
10128 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10129 relinfo,
10130 prelocs,
10131 reloc_count,
10132 output_section,
10133 offset_in_output_section,
10134 view,
10135 view_address,
10136 view_size,
10137 reloc_view,
10138 reloc_view_size);
10140 else if (sh_type == elfcpp::SHT_RELA)
10142 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10143 Classify_reloc;
10145 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10146 relinfo,
10147 prelocs,
10148 reloc_count,
10149 output_section,
10150 offset_in_output_section,
10151 view,
10152 view_address,
10153 view_size,
10154 reloc_view,
10155 reloc_view_size);
10157 else
10158 gold_unreachable();
10161 // Perform target-specific processing in a relocatable link. This is
10162 // only used if we use the relocation strategy RELOC_SPECIAL.
10164 template<int size, bool big_endian>
10165 void
10166 Target_mips<size, big_endian>::relocate_special_relocatable(
10167 const Relocate_info<size, big_endian>* relinfo,
10168 unsigned int sh_type,
10169 const unsigned char* preloc_in,
10170 size_t relnum,
10171 Output_section* output_section,
10172 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10173 unsigned char* view,
10174 Mips_address view_address,
10175 section_size_type,
10176 unsigned char* preloc_out)
10178 // We can only handle REL type relocation sections.
10179 gold_assert(sh_type == elfcpp::SHT_REL);
10181 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10182 Reltype;
10183 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10184 Reltype_write;
10186 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10188 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10190 Mips_relobj<size, big_endian>* object =
10191 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10192 const unsigned int local_count = object->local_symbol_count();
10194 Reltype reloc(preloc_in);
10195 Reltype_write reloc_write(preloc_out);
10197 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10198 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10199 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10201 // Get the new symbol index.
10202 // We only use RELOC_SPECIAL strategy in local relocations.
10203 gold_assert(r_sym < local_count);
10205 // We are adjusting a section symbol. We need to find
10206 // the symbol table index of the section symbol for
10207 // the output section corresponding to input section
10208 // in which this symbol is defined.
10209 bool is_ordinary;
10210 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10211 gold_assert(is_ordinary);
10212 Output_section* os = object->output_section(shndx);
10213 gold_assert(os != NULL);
10214 gold_assert(os->needs_symtab_index());
10215 unsigned int new_symndx = os->symtab_index();
10217 // Get the new offset--the location in the output section where
10218 // this relocation should be applied.
10220 Mips_address offset = reloc.get_r_offset();
10221 Mips_address new_offset;
10222 if (offset_in_output_section != invalid_address)
10223 new_offset = offset + offset_in_output_section;
10224 else
10226 section_offset_type sot_offset =
10227 convert_types<section_offset_type, Mips_address>(offset);
10228 section_offset_type new_sot_offset =
10229 output_section->output_offset(object, relinfo->data_shndx,
10230 sot_offset);
10231 gold_assert(new_sot_offset != -1);
10232 new_offset = new_sot_offset;
10235 // In an object file, r_offset is an offset within the section.
10236 // In an executable or dynamic object, generated by
10237 // --emit-relocs, r_offset is an absolute address.
10238 if (!parameters->options().relocatable())
10240 new_offset += view_address;
10241 if (offset_in_output_section != invalid_address)
10242 new_offset -= offset_in_output_section;
10245 reloc_write.put_r_offset(new_offset);
10246 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10248 // Handle the reloc addend.
10249 // The relocation uses a section symbol in the input file.
10250 // We are adjusting it to use a section symbol in the output
10251 // file. The input section symbol refers to some address in
10252 // the input section. We need the relocation in the output
10253 // file to refer to that same address. This adjustment to
10254 // the addend is the same calculation we use for a simple
10255 // absolute relocation for the input section symbol.
10256 Valtype calculated_value = 0;
10257 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10259 unsigned char* paddend = view + offset;
10260 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10261 switch (r_type)
10263 case elfcpp::R_MIPS_26:
10264 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10265 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
10266 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10267 false, &calculated_value);
10268 break;
10270 default:
10271 gold_unreachable();
10274 // Report any errors.
10275 switch (reloc_status)
10277 case Reloc_funcs::STATUS_OKAY:
10278 break;
10279 case Reloc_funcs::STATUS_OVERFLOW:
10280 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10281 _("relocation overflow"));
10282 break;
10283 case Reloc_funcs::STATUS_BAD_RELOC:
10284 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10285 _("unexpected opcode while processing relocation"));
10286 break;
10287 default:
10288 gold_unreachable();
10292 // Optimize the TLS relocation type based on what we know about the
10293 // symbol. IS_FINAL is true if the final address of this symbol is
10294 // known at link time.
10296 template<int size, bool big_endian>
10297 tls::Tls_optimization
10298 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10300 // FIXME: Currently we do not do any TLS optimization.
10301 return tls::TLSOPT_NONE;
10304 // Scan a relocation for a local symbol.
10306 template<int size, bool big_endian>
10307 inline void
10308 Target_mips<size, big_endian>::Scan::local(
10309 Symbol_table* symtab,
10310 Layout* layout,
10311 Target_mips<size, big_endian>* target,
10312 Sized_relobj_file<size, big_endian>* object,
10313 unsigned int data_shndx,
10314 Output_section* output_section,
10315 const Relatype* rela,
10316 const Reltype* rel,
10317 unsigned int rel_type,
10318 unsigned int r_type,
10319 const elfcpp::Sym<size, big_endian>& lsym,
10320 bool is_discarded)
10322 if (is_discarded)
10323 return;
10325 Mips_address r_offset;
10326 unsigned int r_sym;
10327 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10329 if (rel_type == elfcpp::SHT_RELA)
10331 r_offset = rela->get_r_offset();
10332 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10333 get_r_sym(rela);
10334 r_addend = rela->get_r_addend();
10336 else
10338 r_offset = rel->get_r_offset();
10339 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10340 get_r_sym(rel);
10341 r_addend = 0;
10344 Mips_relobj<size, big_endian>* mips_obj =
10345 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10347 if (mips_obj->is_mips16_stub_section(data_shndx))
10349 mips_obj->get_mips16_stub_section(data_shndx)
10350 ->new_local_reloc_found(r_type, r_sym);
10353 if (r_type == elfcpp::R_MIPS_NONE)
10354 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10355 // mips16 stub.
10356 return;
10358 if (!mips16_call_reloc(r_type)
10359 && !mips_obj->section_allows_mips16_refs(data_shndx))
10360 // This reloc would need to refer to a MIPS16 hard-float stub, if
10361 // there is one. We ignore MIPS16 stub sections and .pdr section when
10362 // looking for relocs that would need to refer to MIPS16 stubs.
10363 mips_obj->add_local_non_16bit_call(r_sym);
10365 if (r_type == elfcpp::R_MIPS16_26
10366 && !mips_obj->section_allows_mips16_refs(data_shndx))
10367 mips_obj->add_local_16bit_call(r_sym);
10369 switch (r_type)
10371 case elfcpp::R_MIPS_GOT16:
10372 case elfcpp::R_MIPS_CALL16:
10373 case elfcpp::R_MIPS_CALL_HI16:
10374 case elfcpp::R_MIPS_CALL_LO16:
10375 case elfcpp::R_MIPS_GOT_HI16:
10376 case elfcpp::R_MIPS_GOT_LO16:
10377 case elfcpp::R_MIPS_GOT_PAGE:
10378 case elfcpp::R_MIPS_GOT_OFST:
10379 case elfcpp::R_MIPS_GOT_DISP:
10380 case elfcpp::R_MIPS_TLS_GOTTPREL:
10381 case elfcpp::R_MIPS_TLS_GD:
10382 case elfcpp::R_MIPS_TLS_LDM:
10383 case elfcpp::R_MIPS16_GOT16:
10384 case elfcpp::R_MIPS16_CALL16:
10385 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10386 case elfcpp::R_MIPS16_TLS_GD:
10387 case elfcpp::R_MIPS16_TLS_LDM:
10388 case elfcpp::R_MICROMIPS_GOT16:
10389 case elfcpp::R_MICROMIPS_CALL16:
10390 case elfcpp::R_MICROMIPS_CALL_HI16:
10391 case elfcpp::R_MICROMIPS_CALL_LO16:
10392 case elfcpp::R_MICROMIPS_GOT_HI16:
10393 case elfcpp::R_MICROMIPS_GOT_LO16:
10394 case elfcpp::R_MICROMIPS_GOT_PAGE:
10395 case elfcpp::R_MICROMIPS_GOT_OFST:
10396 case elfcpp::R_MICROMIPS_GOT_DISP:
10397 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10398 case elfcpp::R_MICROMIPS_TLS_GD:
10399 case elfcpp::R_MICROMIPS_TLS_LDM:
10400 case elfcpp::R_MIPS_EH:
10401 // We need a GOT section.
10402 target->got_section(symtab, layout);
10403 break;
10405 default:
10406 break;
10409 if (call_lo16_reloc(r_type)
10410 || got_lo16_reloc(r_type)
10411 || got_disp_reloc(r_type)
10412 || eh_reloc(r_type))
10414 // We may need a local GOT entry for this relocation. We
10415 // don't count R_MIPS_GOT_PAGE because we can estimate the
10416 // maximum number of pages needed by looking at the size of
10417 // the segment. Similar comments apply to R_MIPS*_GOT16 and
10418 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
10419 // R_MIPS_CALL_HI16 because these are always followed by an
10420 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10421 Mips_output_data_got<size, big_endian>* got =
10422 target->got_section(symtab, layout);
10423 bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10424 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10425 is_section_symbol);
10428 switch (r_type)
10430 case elfcpp::R_MIPS_CALL16:
10431 case elfcpp::R_MIPS16_CALL16:
10432 case elfcpp::R_MICROMIPS_CALL16:
10433 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10434 (unsigned long)r_offset);
10435 return;
10437 case elfcpp::R_MIPS_GOT_PAGE:
10438 case elfcpp::R_MICROMIPS_GOT_PAGE:
10439 case elfcpp::R_MIPS16_GOT16:
10440 case elfcpp::R_MIPS_GOT16:
10441 case elfcpp::R_MIPS_GOT_HI16:
10442 case elfcpp::R_MIPS_GOT_LO16:
10443 case elfcpp::R_MICROMIPS_GOT16:
10444 case elfcpp::R_MICROMIPS_GOT_HI16:
10445 case elfcpp::R_MICROMIPS_GOT_LO16:
10447 // This relocation needs a page entry in the GOT.
10448 // Get the section contents.
10449 section_size_type view_size = 0;
10450 const unsigned char* view = object->section_contents(data_shndx,
10451 &view_size, false);
10452 view += r_offset;
10454 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10455 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10456 : r_addend);
10458 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10459 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10460 object, data_shndx, r_type, r_sym, addend));
10461 else
10462 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10463 break;
10466 case elfcpp::R_MIPS_HI16:
10467 case elfcpp::R_MIPS_PCHI16:
10468 case elfcpp::R_MIPS16_HI16:
10469 case elfcpp::R_MICROMIPS_HI16:
10470 // Record the reloc so that we can check whether the corresponding LO16
10471 // part exists.
10472 if (rel_type == elfcpp::SHT_REL)
10473 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10474 object, data_shndx, r_type, r_sym, 0));
10475 break;
10477 case elfcpp::R_MIPS_LO16:
10478 case elfcpp::R_MIPS_PCLO16:
10479 case elfcpp::R_MIPS16_LO16:
10480 case elfcpp::R_MICROMIPS_LO16:
10482 if (rel_type != elfcpp::SHT_REL)
10483 break;
10485 // Find corresponding GOT16/HI16 relocation.
10487 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10488 // be immediately following. However, for the IRIX6 ABI, the next
10489 // relocation may be a composed relocation consisting of several
10490 // relocations for the same address. In that case, the R_MIPS_LO16
10491 // relocation may occur as one of these. We permit a similar
10492 // extension in general, as that is useful for GCC.
10494 // In some cases GCC dead code elimination removes the LO16 but
10495 // keeps the corresponding HI16. This is strictly speaking a
10496 // violation of the ABI but not immediately harmful.
10498 typename std::list<got16_addend<size, big_endian> >::iterator it =
10499 target->got16_addends_.begin();
10500 while (it != target->got16_addends_.end())
10502 got16_addend<size, big_endian> _got16_addend = *it;
10504 // TODO(sasa): Split got16_addends_ list into two lists - one for
10505 // GOT16 relocs and the other for HI16 relocs.
10507 // Report an error if we find HI16 or GOT16 reloc from the
10508 // previous section without the matching LO16 part.
10509 if (_got16_addend.object != object
10510 || _got16_addend.shndx != data_shndx)
10512 gold_error("Can't find matching LO16 reloc");
10513 break;
10516 if (_got16_addend.r_sym != r_sym
10517 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10519 ++it;
10520 continue;
10523 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10524 // For GOT16, we need to calculate combined addend and record GOT page
10525 // entry.
10526 if (got16_reloc(_got16_addend.r_type))
10529 section_size_type view_size = 0;
10530 const unsigned char* view = object->section_contents(data_shndx,
10531 &view_size,
10532 false);
10533 view += r_offset;
10535 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10536 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10538 addend = (_got16_addend.addend << 16) + addend;
10539 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10540 addend);
10543 it = target->got16_addends_.erase(it);
10545 break;
10549 switch (r_type)
10551 case elfcpp::R_MIPS_32:
10552 case elfcpp::R_MIPS_REL32:
10553 case elfcpp::R_MIPS_64:
10555 if (parameters->options().output_is_position_independent())
10557 // If building a shared library (or a position-independent
10558 // executable), we need to create a dynamic relocation for
10559 // this location.
10560 if (is_readonly_section(output_section))
10561 break;
10562 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
10563 rel_dyn->add_symbolless_local_addend(object, r_sym,
10564 elfcpp::R_MIPS_REL32,
10565 output_section, data_shndx,
10566 r_offset);
10568 break;
10571 case elfcpp::R_MIPS_TLS_GOTTPREL:
10572 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10573 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10574 case elfcpp::R_MIPS_TLS_LDM:
10575 case elfcpp::R_MIPS16_TLS_LDM:
10576 case elfcpp::R_MICROMIPS_TLS_LDM:
10577 case elfcpp::R_MIPS_TLS_GD:
10578 case elfcpp::R_MIPS16_TLS_GD:
10579 case elfcpp::R_MICROMIPS_TLS_GD:
10581 bool output_is_shared = parameters->options().shared();
10582 const tls::Tls_optimization optimized_type
10583 = Target_mips<size, big_endian>::optimize_tls_reloc(
10584 !output_is_shared, r_type);
10585 switch (r_type)
10587 case elfcpp::R_MIPS_TLS_GD:
10588 case elfcpp::R_MIPS16_TLS_GD:
10589 case elfcpp::R_MICROMIPS_TLS_GD:
10590 if (optimized_type == tls::TLSOPT_NONE)
10592 // Create a pair of GOT entries for the module index and
10593 // dtv-relative offset.
10594 Mips_output_data_got<size, big_endian>* got =
10595 target->got_section(symtab, layout);
10596 unsigned int shndx = lsym.get_st_shndx();
10597 bool is_ordinary;
10598 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10599 if (!is_ordinary)
10601 object->error(_("local symbol %u has bad shndx %u"),
10602 r_sym, shndx);
10603 break;
10605 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10606 shndx, false);
10608 else
10610 // FIXME: TLS optimization not supported yet.
10611 gold_unreachable();
10613 break;
10615 case elfcpp::R_MIPS_TLS_LDM:
10616 case elfcpp::R_MIPS16_TLS_LDM:
10617 case elfcpp::R_MICROMIPS_TLS_LDM:
10618 if (optimized_type == tls::TLSOPT_NONE)
10620 // We always record LDM symbols as local with index 0.
10621 target->got_section()->record_local_got_symbol(mips_obj, 0,
10622 r_addend, r_type,
10623 -1U, false);
10625 else
10627 // FIXME: TLS optimization not supported yet.
10628 gold_unreachable();
10630 break;
10631 case elfcpp::R_MIPS_TLS_GOTTPREL:
10632 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10633 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10634 layout->set_has_static_tls();
10635 if (optimized_type == tls::TLSOPT_NONE)
10637 // Create a GOT entry for the tp-relative offset.
10638 Mips_output_data_got<size, big_endian>* got =
10639 target->got_section(symtab, layout);
10640 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
10641 -1U, false);
10643 else
10645 // FIXME: TLS optimization not supported yet.
10646 gold_unreachable();
10648 break;
10650 default:
10651 gold_unreachable();
10654 break;
10656 default:
10657 break;
10660 // Refuse some position-dependent relocations when creating a
10661 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10662 // not PIC, but we can create dynamic relocations and the result
10663 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10664 // combined with R_MIPS_GOT16.
10665 if (parameters->options().shared())
10667 switch (r_type)
10669 case elfcpp::R_MIPS16_HI16:
10670 case elfcpp::R_MIPS_HI16:
10671 case elfcpp::R_MICROMIPS_HI16:
10672 // Don't refuse a high part relocation if it's against
10673 // no symbol (e.g. part of a compound relocation).
10674 if (r_sym == 0)
10675 break;
10677 // FALLTHROUGH
10679 case elfcpp::R_MIPS16_26:
10680 case elfcpp::R_MIPS_26:
10681 case elfcpp::R_MICROMIPS_26_S1:
10682 gold_error(_("%s: relocation %u against `%s' can not be used when "
10683 "making a shared object; recompile with -fPIC"),
10684 object->name().c_str(), r_type, "a local symbol");
10685 default:
10686 break;
10691 template<int size, bool big_endian>
10692 inline void
10693 Target_mips<size, big_endian>::Scan::local(
10694 Symbol_table* symtab,
10695 Layout* layout,
10696 Target_mips<size, big_endian>* target,
10697 Sized_relobj_file<size, big_endian>* object,
10698 unsigned int data_shndx,
10699 Output_section* output_section,
10700 const Reltype& reloc,
10701 unsigned int r_type,
10702 const elfcpp::Sym<size, big_endian>& lsym,
10703 bool is_discarded)
10705 if (is_discarded)
10706 return;
10708 local(
10709 symtab,
10710 layout,
10711 target,
10712 object,
10713 data_shndx,
10714 output_section,
10715 (const Relatype*) NULL,
10716 &reloc,
10717 elfcpp::SHT_REL,
10718 r_type,
10719 lsym, is_discarded);
10723 template<int size, bool big_endian>
10724 inline void
10725 Target_mips<size, big_endian>::Scan::local(
10726 Symbol_table* symtab,
10727 Layout* layout,
10728 Target_mips<size, big_endian>* target,
10729 Sized_relobj_file<size, big_endian>* object,
10730 unsigned int data_shndx,
10731 Output_section* output_section,
10732 const Relatype& reloc,
10733 unsigned int r_type,
10734 const elfcpp::Sym<size, big_endian>& lsym,
10735 bool is_discarded)
10737 if (is_discarded)
10738 return;
10740 local(
10741 symtab,
10742 layout,
10743 target,
10744 object,
10745 data_shndx,
10746 output_section,
10747 &reloc,
10748 (const Reltype*) NULL,
10749 elfcpp::SHT_RELA,
10750 r_type,
10751 lsym, is_discarded);
10754 // Scan a relocation for a global symbol.
10756 template<int size, bool big_endian>
10757 inline void
10758 Target_mips<size, big_endian>::Scan::global(
10759 Symbol_table* symtab,
10760 Layout* layout,
10761 Target_mips<size, big_endian>* target,
10762 Sized_relobj_file<size, big_endian>* object,
10763 unsigned int data_shndx,
10764 Output_section* output_section,
10765 const Relatype* rela,
10766 const Reltype* rel,
10767 unsigned int rel_type,
10768 unsigned int r_type,
10769 Symbol* gsym)
10771 Mips_address r_offset;
10772 unsigned int r_sym;
10773 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10775 if (rel_type == elfcpp::SHT_RELA)
10777 r_offset = rela->get_r_offset();
10778 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10779 get_r_sym(rela);
10780 r_addend = rela->get_r_addend();
10782 else
10784 r_offset = rel->get_r_offset();
10785 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10786 get_r_sym(rel);
10787 r_addend = 0;
10790 Mips_relobj<size, big_endian>* mips_obj =
10791 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10792 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10794 if (mips_obj->is_mips16_stub_section(data_shndx))
10796 mips_obj->get_mips16_stub_section(data_shndx)
10797 ->new_global_reloc_found(r_type, mips_sym);
10800 if (r_type == elfcpp::R_MIPS_NONE)
10801 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10802 // mips16 stub.
10803 return;
10805 if (!mips16_call_reloc(r_type)
10806 && !mips_obj->section_allows_mips16_refs(data_shndx))
10807 // This reloc would need to refer to a MIPS16 hard-float stub, if
10808 // there is one. We ignore MIPS16 stub sections and .pdr section when
10809 // looking for relocs that would need to refer to MIPS16 stubs.
10810 mips_sym->set_need_fn_stub();
10812 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
10813 // section. We check here to avoid creating a dynamic reloc against
10814 // _GLOBAL_OFFSET_TABLE_.
10815 if (!target->has_got_section()
10816 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
10817 target->got_section(symtab, layout);
10819 // We need PLT entries if there are static-only relocations against
10820 // an externally-defined function. This can technically occur for
10821 // shared libraries if there are branches to the symbol, although it
10822 // is unlikely that this will be used in practice due to the short
10823 // ranges involved. It can occur for any relative or absolute relocation
10824 // in executables; in that case, the PLT entry becomes the function's
10825 // canonical address.
10826 bool static_reloc = false;
10828 // Set CAN_MAKE_DYNAMIC to true if we can convert this
10829 // relocation into a dynamic one.
10830 bool can_make_dynamic = false;
10831 switch (r_type)
10833 case elfcpp::R_MIPS_GOT16:
10834 case elfcpp::R_MIPS_CALL16:
10835 case elfcpp::R_MIPS_CALL_HI16:
10836 case elfcpp::R_MIPS_CALL_LO16:
10837 case elfcpp::R_MIPS_GOT_HI16:
10838 case elfcpp::R_MIPS_GOT_LO16:
10839 case elfcpp::R_MIPS_GOT_PAGE:
10840 case elfcpp::R_MIPS_GOT_OFST:
10841 case elfcpp::R_MIPS_GOT_DISP:
10842 case elfcpp::R_MIPS_TLS_GOTTPREL:
10843 case elfcpp::R_MIPS_TLS_GD:
10844 case elfcpp::R_MIPS_TLS_LDM:
10845 case elfcpp::R_MIPS16_GOT16:
10846 case elfcpp::R_MIPS16_CALL16:
10847 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10848 case elfcpp::R_MIPS16_TLS_GD:
10849 case elfcpp::R_MIPS16_TLS_LDM:
10850 case elfcpp::R_MICROMIPS_GOT16:
10851 case elfcpp::R_MICROMIPS_CALL16:
10852 case elfcpp::R_MICROMIPS_CALL_HI16:
10853 case elfcpp::R_MICROMIPS_CALL_LO16:
10854 case elfcpp::R_MICROMIPS_GOT_HI16:
10855 case elfcpp::R_MICROMIPS_GOT_LO16:
10856 case elfcpp::R_MICROMIPS_GOT_PAGE:
10857 case elfcpp::R_MICROMIPS_GOT_OFST:
10858 case elfcpp::R_MICROMIPS_GOT_DISP:
10859 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10860 case elfcpp::R_MICROMIPS_TLS_GD:
10861 case elfcpp::R_MICROMIPS_TLS_LDM:
10862 case elfcpp::R_MIPS_EH:
10863 // We need a GOT section.
10864 target->got_section(symtab, layout);
10865 break;
10867 // This is just a hint; it can safely be ignored. Don't set
10868 // has_static_relocs for the corresponding symbol.
10869 case elfcpp::R_MIPS_JALR:
10870 case elfcpp::R_MICROMIPS_JALR:
10871 break;
10873 case elfcpp::R_MIPS_GPREL16:
10874 case elfcpp::R_MIPS_GPREL32:
10875 case elfcpp::R_MIPS16_GPREL:
10876 case elfcpp::R_MICROMIPS_GPREL16:
10877 // TODO(sasa)
10878 // GP-relative relocations always resolve to a definition in a
10879 // regular input file, ignoring the one-definition rule. This is
10880 // important for the GP setup sequence in NewABI code, which
10881 // always resolves to a local function even if other relocations
10882 // against the symbol wouldn't.
10883 //constrain_symbol_p = FALSE;
10884 break;
10886 case elfcpp::R_MIPS_32:
10887 case elfcpp::R_MIPS_REL32:
10888 case elfcpp::R_MIPS_64:
10889 if ((parameters->options().shared()
10890 || (strcmp(gsym->name(), "__gnu_local_gp") != 0
10891 && (!is_readonly_section(output_section)
10892 || mips_obj->is_pic())))
10893 && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
10895 if (r_type != elfcpp::R_MIPS_REL32)
10896 mips_sym->set_pointer_equality_needed();
10897 can_make_dynamic = true;
10898 break;
10900 // Fall through.
10902 default:
10903 // Most static relocations require pointer equality, except
10904 // for branches.
10905 mips_sym->set_pointer_equality_needed();
10907 // Fall through.
10909 case elfcpp::R_MIPS_26:
10910 case elfcpp::R_MIPS_PC16:
10911 case elfcpp::R_MIPS_PC21_S2:
10912 case elfcpp::R_MIPS_PC26_S2:
10913 case elfcpp::R_MIPS16_26:
10914 case elfcpp::R_MICROMIPS_26_S1:
10915 case elfcpp::R_MICROMIPS_PC7_S1:
10916 case elfcpp::R_MICROMIPS_PC10_S1:
10917 case elfcpp::R_MICROMIPS_PC16_S1:
10918 case elfcpp::R_MICROMIPS_PC23_S2:
10919 static_reloc = true;
10920 mips_sym->set_has_static_relocs();
10921 break;
10924 // If there are call relocations against an externally-defined symbol,
10925 // see whether we can create a MIPS lazy-binding stub for it. We can
10926 // only do this if all references to the function are through call
10927 // relocations, and in that case, the traditional lazy-binding stubs
10928 // are much more efficient than PLT entries.
10929 switch (r_type)
10931 case elfcpp::R_MIPS16_CALL16:
10932 case elfcpp::R_MIPS_CALL16:
10933 case elfcpp::R_MIPS_CALL_HI16:
10934 case elfcpp::R_MIPS_CALL_LO16:
10935 case elfcpp::R_MIPS_JALR:
10936 case elfcpp::R_MICROMIPS_CALL16:
10937 case elfcpp::R_MICROMIPS_CALL_HI16:
10938 case elfcpp::R_MICROMIPS_CALL_LO16:
10939 case elfcpp::R_MICROMIPS_JALR:
10940 if (!mips_sym->no_lazy_stub())
10942 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
10943 // Calls from shared objects to undefined symbols of type
10944 // STT_NOTYPE need lazy-binding stub.
10945 || (mips_sym->is_undefined() && parameters->options().shared()))
10946 target->mips_stubs_section(layout)->make_entry(mips_sym);
10948 break;
10949 default:
10951 // We must not create a stub for a symbol that has relocations
10952 // related to taking the function's address.
10953 mips_sym->set_no_lazy_stub();
10954 target->remove_lazy_stub_entry(mips_sym);
10955 break;
10959 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
10960 mips_sym->is_mips16()))
10961 mips_sym->set_has_nonpic_branches();
10963 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
10964 // and has a special meaning.
10965 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
10966 && strcmp(gsym->name(), "_gp_disp") == 0
10967 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
10968 if (static_reloc && gsym->needs_plt_entry())
10970 target->make_plt_entry(symtab, layout, mips_sym, r_type);
10972 // Since this is not a PC-relative relocation, we may be
10973 // taking the address of a function. In that case we need to
10974 // set the entry in the dynamic symbol table to the address of
10975 // the PLT entry.
10976 if (gsym->is_from_dynobj() && !parameters->options().shared())
10978 gsym->set_needs_dynsym_value();
10979 // We distinguish between PLT entries and lazy-binding stubs by
10980 // giving the former an st_other value of STO_MIPS_PLT. Set the
10981 // flag if there are any relocations in the binary where pointer
10982 // equality matters.
10983 if (mips_sym->pointer_equality_needed())
10984 mips_sym->set_mips_plt();
10987 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
10989 // Absolute addressing relocations.
10990 // Make a dynamic relocation if necessary.
10991 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
10993 if (gsym->may_need_copy_reloc())
10995 target->copy_reloc(symtab, layout, object, data_shndx,
10996 output_section, gsym, r_type, r_offset);
10998 else if (can_make_dynamic)
11000 // Create .rel.dyn section.
11001 target->rel_dyn_section(layout);
11002 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11003 data_shndx, output_section, r_offset);
11005 else
11006 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11007 gsym->name());
11011 bool for_call = false;
11012 switch (r_type)
11014 case elfcpp::R_MIPS_CALL16:
11015 case elfcpp::R_MIPS16_CALL16:
11016 case elfcpp::R_MICROMIPS_CALL16:
11017 case elfcpp::R_MIPS_CALL_HI16:
11018 case elfcpp::R_MIPS_CALL_LO16:
11019 case elfcpp::R_MICROMIPS_CALL_HI16:
11020 case elfcpp::R_MICROMIPS_CALL_LO16:
11021 for_call = true;
11022 // Fall through.
11024 case elfcpp::R_MIPS16_GOT16:
11025 case elfcpp::R_MIPS_GOT16:
11026 case elfcpp::R_MIPS_GOT_HI16:
11027 case elfcpp::R_MIPS_GOT_LO16:
11028 case elfcpp::R_MICROMIPS_GOT16:
11029 case elfcpp::R_MICROMIPS_GOT_HI16:
11030 case elfcpp::R_MICROMIPS_GOT_LO16:
11031 case elfcpp::R_MIPS_GOT_DISP:
11032 case elfcpp::R_MICROMIPS_GOT_DISP:
11033 case elfcpp::R_MIPS_EH:
11035 // The symbol requires a GOT entry.
11036 Mips_output_data_got<size, big_endian>* got =
11037 target->got_section(symtab, layout);
11038 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11039 for_call);
11040 mips_sym->set_global_got_area(GGA_NORMAL);
11042 break;
11044 case elfcpp::R_MIPS_GOT_PAGE:
11045 case elfcpp::R_MICROMIPS_GOT_PAGE:
11047 // This relocation needs a page entry in the GOT.
11048 // Get the section contents.
11049 section_size_type view_size = 0;
11050 const unsigned char* view =
11051 object->section_contents(data_shndx, &view_size, false);
11052 view += r_offset;
11054 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11055 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11056 : r_addend);
11057 Mips_output_data_got<size, big_endian>* got =
11058 target->got_section(symtab, layout);
11059 got->record_got_page_entry(mips_obj, r_sym, addend);
11061 // If this is a global, overridable symbol, GOT_PAGE will
11062 // decay to GOT_DISP, so we'll need a GOT entry for it.
11063 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11064 && !mips_sym->object()->is_dynamic()
11065 && !mips_sym->is_undefined());
11066 if (!def_regular
11067 || (parameters->options().output_is_position_independent()
11068 && !parameters->options().Bsymbolic()
11069 && !mips_sym->is_forced_local()))
11071 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11072 for_call);
11073 mips_sym->set_global_got_area(GGA_NORMAL);
11076 break;
11078 case elfcpp::R_MIPS_TLS_GOTTPREL:
11079 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11080 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11081 case elfcpp::R_MIPS_TLS_LDM:
11082 case elfcpp::R_MIPS16_TLS_LDM:
11083 case elfcpp::R_MICROMIPS_TLS_LDM:
11084 case elfcpp::R_MIPS_TLS_GD:
11085 case elfcpp::R_MIPS16_TLS_GD:
11086 case elfcpp::R_MICROMIPS_TLS_GD:
11088 const bool is_final = gsym->final_value_is_known();
11089 const tls::Tls_optimization optimized_type =
11090 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11092 switch (r_type)
11094 case elfcpp::R_MIPS_TLS_GD:
11095 case elfcpp::R_MIPS16_TLS_GD:
11096 case elfcpp::R_MICROMIPS_TLS_GD:
11097 if (optimized_type == tls::TLSOPT_NONE)
11099 // Create a pair of GOT entries for the module index and
11100 // dtv-relative offset.
11101 Mips_output_data_got<size, big_endian>* got =
11102 target->got_section(symtab, layout);
11103 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11104 false);
11106 else
11108 // FIXME: TLS optimization not supported yet.
11109 gold_unreachable();
11111 break;
11113 case elfcpp::R_MIPS_TLS_LDM:
11114 case elfcpp::R_MIPS16_TLS_LDM:
11115 case elfcpp::R_MICROMIPS_TLS_LDM:
11116 if (optimized_type == tls::TLSOPT_NONE)
11118 // We always record LDM symbols as local with index 0.
11119 target->got_section()->record_local_got_symbol(mips_obj, 0,
11120 r_addend, r_type,
11121 -1U, false);
11123 else
11125 // FIXME: TLS optimization not supported yet.
11126 gold_unreachable();
11128 break;
11129 case elfcpp::R_MIPS_TLS_GOTTPREL:
11130 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11131 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11132 layout->set_has_static_tls();
11133 if (optimized_type == tls::TLSOPT_NONE)
11135 // Create a GOT entry for the tp-relative offset.
11136 Mips_output_data_got<size, big_endian>* got =
11137 target->got_section(symtab, layout);
11138 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11139 false);
11141 else
11143 // FIXME: TLS optimization not supported yet.
11144 gold_unreachable();
11146 break;
11148 default:
11149 gold_unreachable();
11152 break;
11153 case elfcpp::R_MIPS_COPY:
11154 case elfcpp::R_MIPS_JUMP_SLOT:
11155 // These are relocations which should only be seen by the
11156 // dynamic linker, and should never be seen here.
11157 gold_error(_("%s: unexpected reloc %u in object file"),
11158 object->name().c_str(), r_type);
11159 break;
11161 default:
11162 break;
11165 // Refuse some position-dependent relocations when creating a
11166 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
11167 // not PIC, but we can create dynamic relocations and the result
11168 // will be fine. Also do not refuse R_MIPS_LO16, which can be
11169 // combined with R_MIPS_GOT16.
11170 if (parameters->options().shared())
11172 switch (r_type)
11174 case elfcpp::R_MIPS16_HI16:
11175 case elfcpp::R_MIPS_HI16:
11176 case elfcpp::R_MICROMIPS_HI16:
11177 // Don't refuse a high part relocation if it's against
11178 // no symbol (e.g. part of a compound relocation).
11179 if (r_sym == 0)
11180 break;
11182 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11183 // and has a special meaning.
11184 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11185 break;
11187 // FALLTHROUGH
11189 case elfcpp::R_MIPS16_26:
11190 case elfcpp::R_MIPS_26:
11191 case elfcpp::R_MICROMIPS_26_S1:
11192 gold_error(_("%s: relocation %u against `%s' can not be used when "
11193 "making a shared object; recompile with -fPIC"),
11194 object->name().c_str(), r_type, gsym->name());
11195 default:
11196 break;
11201 template<int size, bool big_endian>
11202 inline void
11203 Target_mips<size, big_endian>::Scan::global(
11204 Symbol_table* symtab,
11205 Layout* layout,
11206 Target_mips<size, big_endian>* target,
11207 Sized_relobj_file<size, big_endian>* object,
11208 unsigned int data_shndx,
11209 Output_section* output_section,
11210 const Relatype& reloc,
11211 unsigned int r_type,
11212 Symbol* gsym)
11214 global(
11215 symtab,
11216 layout,
11217 target,
11218 object,
11219 data_shndx,
11220 output_section,
11221 &reloc,
11222 (const Reltype*) NULL,
11223 elfcpp::SHT_RELA,
11224 r_type,
11225 gsym);
11228 template<int size, bool big_endian>
11229 inline void
11230 Target_mips<size, big_endian>::Scan::global(
11231 Symbol_table* symtab,
11232 Layout* layout,
11233 Target_mips<size, big_endian>* target,
11234 Sized_relobj_file<size, big_endian>* object,
11235 unsigned int data_shndx,
11236 Output_section* output_section,
11237 const Reltype& reloc,
11238 unsigned int r_type,
11239 Symbol* gsym)
11241 global(
11242 symtab,
11243 layout,
11244 target,
11245 object,
11246 data_shndx,
11247 output_section,
11248 (const Relatype*) NULL,
11249 &reloc,
11250 elfcpp::SHT_REL,
11251 r_type,
11252 gsym);
11255 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11256 // In cases where Scan::local() or Scan::global() has created
11257 // a dynamic relocation, the addend of the relocation is carried
11258 // in the data, and we must not apply the static relocation.
11260 template<int size, bool big_endian>
11261 inline bool
11262 Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
11263 const Mips_symbol<size>* gsym,
11264 unsigned int r_type,
11265 Output_section* output_section,
11266 Target_mips* target)
11268 // If the output section is not allocated, then we didn't call
11269 // scan_relocs, we didn't create a dynamic reloc, and we must apply
11270 // the reloc here.
11271 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11272 return true;
11274 if (gsym == NULL)
11275 return true;
11276 else
11278 // For global symbols, we use the same helper routines used in the
11279 // scan pass.
11280 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11281 && !gsym->may_need_copy_reloc())
11283 // We have generated dynamic reloc (R_MIPS_REL32).
11285 bool multi_got = false;
11286 if (target->has_got_section())
11287 multi_got = target->got_section()->multi_got();
11288 bool has_got_offset;
11289 if (!multi_got)
11290 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11291 else
11292 has_got_offset = gsym->global_gotoffset() != -1U;
11293 if (!has_got_offset)
11294 return true;
11295 else
11296 // Apply the relocation only if the symbol is in the local got.
11297 // Do not apply the relocation if the symbol is in the global
11298 // got.
11299 return symbol_references_local(gsym, gsym->has_dynsym_index());
11301 else
11302 // We have not generated dynamic reloc.
11303 return true;
11307 // Perform a relocation.
11309 template<int size, bool big_endian>
11310 inline bool
11311 Target_mips<size, big_endian>::Relocate::relocate(
11312 const Relocate_info<size, big_endian>* relinfo,
11313 unsigned int rel_type,
11314 Target_mips* target,
11315 Output_section* output_section,
11316 size_t relnum,
11317 const unsigned char* preloc,
11318 const Sized_symbol<size>* gsym,
11319 const Symbol_value<size>* psymval,
11320 unsigned char* view,
11321 Mips_address address,
11322 section_size_type)
11324 Mips_address r_offset;
11325 unsigned int r_sym;
11326 unsigned int r_type;
11327 unsigned int r_type2;
11328 unsigned int r_type3;
11329 unsigned char r_ssym;
11330 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
11332 if (rel_type == elfcpp::SHT_RELA)
11334 const Relatype rela(preloc);
11335 r_offset = rela.get_r_offset();
11336 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11337 get_r_sym(&rela);
11338 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11339 get_r_type(&rela);
11340 r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11341 get_r_type2(&rela);
11342 r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11343 get_r_type3(&rela);
11344 r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11345 get_r_ssym(&rela);
11346 r_addend = rela.get_r_addend();
11348 else
11350 const Reltype rel(preloc);
11351 r_offset = rel.get_r_offset();
11352 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11353 get_r_sym(&rel);
11354 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11355 get_r_type(&rel);
11356 r_ssym = 0;
11357 r_type2 = 0;
11358 r_type3 = 0;
11359 r_addend = 0;
11362 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11363 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11365 Mips_relobj<size, big_endian>* object =
11366 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
11368 bool target_is_16_bit_code = false;
11369 bool target_is_micromips_code = false;
11370 bool cross_mode_jump;
11372 Symbol_value<size> symval;
11374 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11376 bool changed_symbol_value = false;
11377 if (gsym == NULL)
11379 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11380 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11381 if (target_is_16_bit_code || target_is_micromips_code)
11383 // MIPS16/microMIPS text labels should be treated as odd.
11384 symval.set_output_value(psymval->value(object, 1));
11385 psymval = &symval;
11386 changed_symbol_value = true;
11389 else
11391 target_is_16_bit_code = mips_sym->is_mips16();
11392 target_is_micromips_code = mips_sym->is_micromips();
11394 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11395 // it odd. This will cause something like .word SYM to come up with
11396 // the right value when it is loaded into the PC.
11398 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11399 && psymval->value(object, 0) != 0)
11401 symval.set_output_value(psymval->value(object, 0) | 1);
11402 psymval = &symval;
11403 changed_symbol_value = true;
11406 // Pick the value to use for symbols defined in shared objects.
11407 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11408 || mips_sym->has_lazy_stub())
11410 Mips_address value;
11411 if (!mips_sym->has_lazy_stub())
11413 // Prefer a standard MIPS PLT entry.
11414 if (mips_sym->has_mips_plt_offset())
11416 value = target->plt_section()->mips_entry_address(mips_sym);
11417 target_is_micromips_code = false;
11418 target_is_16_bit_code = false;
11420 else
11422 value = (target->plt_section()->comp_entry_address(mips_sym)
11423 + 1);
11424 if (target->is_output_micromips())
11425 target_is_micromips_code = true;
11426 else
11427 target_is_16_bit_code = true;
11430 else
11431 value = target->mips_stubs_section()->stub_address(mips_sym);
11433 symval.set_output_value(value);
11434 psymval = &symval;
11438 // TRUE if the symbol referred to by this relocation is "_gp_disp".
11439 // Note that such a symbol must always be a global symbol.
11440 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11441 && !object->is_newabi());
11443 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11444 // Note that such a symbol must always be a global symbol.
11445 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11448 if (gp_disp)
11450 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11451 gold_error_at_location(relinfo, relnum, r_offset,
11452 _("relocations against _gp_disp are permitted only"
11453 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11455 else if (gnu_local_gp)
11457 // __gnu_local_gp is _gp symbol.
11458 symval.set_output_value(target->adjusted_gp_value(object));
11459 psymval = &symval;
11462 // If this is a reference to a 16-bit function with a stub, we need
11463 // to redirect the relocation to the stub unless:
11465 // (a) the relocation is for a MIPS16 JAL;
11467 // (b) the relocation is for a MIPS16 PIC call, and there are no
11468 // non-MIPS16 uses of the GOT slot; or
11470 // (c) the section allows direct references to MIPS16 functions.
11471 if (r_type != elfcpp::R_MIPS16_26
11472 && !parameters->options().relocatable()
11473 && ((mips_sym != NULL
11474 && mips_sym->has_mips16_fn_stub()
11475 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11476 || (mips_sym == NULL
11477 && object->get_local_mips16_fn_stub(r_sym) != NULL))
11478 && !object->section_allows_mips16_refs(relinfo->data_shndx))
11480 // This is a 32- or 64-bit call to a 16-bit function. We should
11481 // have already noticed that we were going to need the
11482 // stub.
11483 Mips_address value;
11484 if (mips_sym == NULL)
11485 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11486 else
11488 gold_assert(mips_sym->need_fn_stub());
11489 if (mips_sym->has_la25_stub())
11490 value = target->la25_stub_section()->stub_address(mips_sym);
11491 else
11493 value = mips_sym->template
11494 get_mips16_fn_stub<big_endian>()->output_address();
11497 symval.set_output_value(value);
11498 psymval = &symval;
11499 changed_symbol_value = true;
11501 // The target is 16-bit, but the stub isn't.
11502 target_is_16_bit_code = false;
11504 // If this is a MIPS16 call with a stub, that is made through the PLT or
11505 // to a standard MIPS function, we need to redirect the call to the stub.
11506 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11507 // indirect calls should use an indirect stub instead.
11508 else if (r_type == elfcpp::R_MIPS16_26 && !parameters->options().relocatable()
11509 && ((mips_sym != NULL
11510 && (mips_sym->has_mips16_call_stub()
11511 || mips_sym->has_mips16_call_fp_stub()))
11512 || (mips_sym == NULL
11513 && object->get_local_mips16_call_stub(r_sym) != NULL))
11514 && ((mips_sym != NULL && mips_sym->has_plt_offset())
11515 || !target_is_16_bit_code))
11517 Mips16_stub_section<size, big_endian>* call_stub;
11518 if (mips_sym == NULL)
11519 call_stub = object->get_local_mips16_call_stub(r_sym);
11520 else
11522 // If both call_stub and call_fp_stub are defined, we can figure
11523 // out which one to use by checking which one appears in the input
11524 // file.
11525 if (mips_sym->has_mips16_call_stub()
11526 && mips_sym->has_mips16_call_fp_stub())
11528 call_stub = NULL;
11529 for (unsigned int i = 1; i < object->shnum(); ++i)
11531 if (object->is_mips16_call_fp_stub_section(i))
11533 call_stub = mips_sym->template
11534 get_mips16_call_fp_stub<big_endian>();
11535 break;
11539 if (call_stub == NULL)
11540 call_stub =
11541 mips_sym->template get_mips16_call_stub<big_endian>();
11543 else if (mips_sym->has_mips16_call_stub())
11544 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11545 else
11546 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11549 symval.set_output_value(call_stub->output_address());
11550 psymval = &symval;
11551 changed_symbol_value = true;
11553 // If this is a direct call to a PIC function, redirect to the
11554 // non-PIC stub.
11555 else if (mips_sym != NULL
11556 && mips_sym->has_la25_stub()
11557 && relocation_needs_la25_stub<size, big_endian>(
11558 object, r_type, target_is_16_bit_code))
11560 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11561 if (mips_sym->is_micromips())
11562 value += 1;
11563 symval.set_output_value(value);
11564 psymval = &symval;
11566 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11567 // entry is used if a standard PLT entry has also been made.
11568 else if ((r_type == elfcpp::R_MIPS16_26
11569 || r_type == elfcpp::R_MICROMIPS_26_S1)
11570 && !parameters->options().relocatable()
11571 && mips_sym != NULL
11572 && mips_sym->has_plt_offset()
11573 && mips_sym->has_comp_plt_offset()
11574 && mips_sym->has_mips_plt_offset())
11576 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11577 + 1);
11578 symval.set_output_value(value);
11579 psymval = &symval;
11581 target_is_16_bit_code = !target->is_output_micromips();
11582 target_is_micromips_code = target->is_output_micromips();
11585 // Make sure MIPS16 and microMIPS are not used together.
11586 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11587 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11589 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11592 // Calls from 16-bit code to 32-bit code and vice versa require the
11593 // mode change. However, we can ignore calls to undefined weak symbols,
11594 // which should never be executed at runtime. This exception is important
11595 // because the assembly writer may have "known" that any definition of the
11596 // symbol would be 16-bit code, and that direct jumps were therefore
11597 // acceptable.
11598 cross_mode_jump =
11599 (!parameters->options().relocatable()
11600 && !(gsym != NULL && gsym->is_weak_undefined())
11601 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11602 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11603 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11604 && (target_is_16_bit_code || target_is_micromips_code))));
11606 bool local = (mips_sym == NULL
11607 || (mips_sym->got_only_for_calls()
11608 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11609 : symbol_references_local(mips_sym,
11610 mips_sym->has_dynsym_index())));
11612 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11613 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
11614 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11615 if (got_page_reloc(r_type) && !local)
11616 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11617 : elfcpp::R_MIPS_GOT_DISP);
11619 unsigned int got_offset = 0;
11620 int gp_offset = 0;
11622 bool calculate_only = false;
11623 Valtype calculated_value = 0;
11624 bool extract_addend = rel_type == elfcpp::SHT_REL;
11625 unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11627 Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11629 // For Mips64 N64 ABI, there may be up to three operations specified per
11630 // record, by the fields r_type, r_type2, and r_type3. The first operation
11631 // takes its addend from the relocation record. Each subsequent operation
11632 // takes as its addend the result of the previous operation.
11633 // The first operation in a record which references a symbol uses the symbol
11634 // implied by r_sym. The next operation in a record which references a symbol
11635 // uses the special symbol value given by the r_ssym field. A third operation
11636 // in a record which references a symbol will assume a NULL symbol,
11637 // i.e. value zero.
11639 // TODO(Vladimir)
11640 // Check if a record references to a symbol.
11641 for (unsigned int i = 0; i < 3; ++i)
11643 if (r_types[i] == elfcpp::R_MIPS_NONE)
11644 break;
11646 // TODO(Vladimir)
11647 // Check if the next relocation is for the same instruction.
11648 calculate_only = i == 2 ? false
11649 : r_types[i+1] != elfcpp::R_MIPS_NONE;
11651 if (object->is_n64())
11653 if (i == 1)
11655 // Handle special symbol for r_type2 relocation type.
11656 switch (r_ssym)
11658 case RSS_UNDEF:
11659 symval.set_output_value(0);
11660 break;
11661 case RSS_GP:
11662 symval.set_output_value(target->gp_value());
11663 break;
11664 case RSS_GP0:
11665 symval.set_output_value(object->gp_value());
11666 break;
11667 case RSS_LOC:
11668 symval.set_output_value(address);
11669 break;
11670 default:
11671 gold_unreachable();
11673 psymval = &symval;
11675 else if (i == 2)
11677 // For r_type3 symbol value is 0.
11678 symval.set_output_value(0);
11682 bool update_got_entry = false;
11683 switch (r_types[i])
11685 case elfcpp::R_MIPS_NONE:
11686 break;
11687 case elfcpp::R_MIPS_16:
11688 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
11689 extract_addend, calculate_only,
11690 &calculated_value);
11691 break;
11693 case elfcpp::R_MIPS_32:
11694 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11695 target))
11696 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
11697 extract_addend, calculate_only,
11698 &calculated_value);
11699 if (mips_sym != NULL
11700 && (mips_sym->is_mips16() || mips_sym->is_micromips())
11701 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11703 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11704 // already updated by adding +1.
11705 if (mips_sym->has_mips16_fn_stub())
11707 gold_assert(mips_sym->need_fn_stub());
11708 Mips16_stub_section<size, big_endian>* fn_stub =
11709 mips_sym->template get_mips16_fn_stub<big_endian>();
11711 symval.set_output_value(fn_stub->output_address());
11712 psymval = &symval;
11714 got_offset = mips_sym->global_gotoffset();
11715 update_got_entry = true;
11717 break;
11719 case elfcpp::R_MIPS_64:
11720 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11721 target))
11722 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11723 extract_addend, calculate_only,
11724 &calculated_value, false);
11725 else if (target->is_output_n64() && r_addend != 0)
11726 // Only apply the addend. The static relocation was RELA, but the
11727 // dynamic relocation is REL, so we need to apply the addend.
11728 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
11729 extract_addend, calculate_only,
11730 &calculated_value, true);
11731 break;
11732 case elfcpp::R_MIPS_REL32:
11733 gold_unreachable();
11735 case elfcpp::R_MIPS_PC32:
11736 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11737 r_addend, extract_addend,
11738 calculate_only,
11739 &calculated_value);
11740 break;
11742 case elfcpp::R_MIPS16_26:
11743 // The calculation for R_MIPS16_26 is just the same as for an
11744 // R_MIPS_26. It's only the storage of the relocated field into
11745 // the output file that's different. So, we just fall through to the
11746 // R_MIPS_26 case here.
11747 case elfcpp::R_MIPS_26:
11748 case elfcpp::R_MICROMIPS_26_S1:
11749 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11750 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
11751 r_types[i], target->jal_to_bal(), calculate_only,
11752 &calculated_value);
11753 break;
11755 case elfcpp::R_MIPS_HI16:
11756 case elfcpp::R_MIPS16_HI16:
11757 case elfcpp::R_MICROMIPS_HI16:
11758 if (rel_type == elfcpp::SHT_RELA)
11759 reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11760 r_addend, address,
11761 gp_disp, r_types[i],
11762 extract_addend, 0,
11763 target, calculate_only,
11764 &calculated_value);
11765 else if (rel_type == elfcpp::SHT_REL)
11766 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11767 address, gp_disp, r_types[i],
11768 r_sym, extract_addend);
11769 else
11770 gold_unreachable();
11771 break;
11773 case elfcpp::R_MIPS_LO16:
11774 case elfcpp::R_MIPS16_LO16:
11775 case elfcpp::R_MICROMIPS_LO16:
11776 case elfcpp::R_MICROMIPS_HI0_LO16:
11777 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11778 r_addend, extract_addend, address,
11779 gp_disp, r_types[i], r_sym,
11780 rel_type, calculate_only,
11781 &calculated_value);
11782 break;
11784 case elfcpp::R_MIPS_LITERAL:
11785 case elfcpp::R_MICROMIPS_LITERAL:
11786 // Because we don't merge literal sections, we can handle this
11787 // just like R_MIPS_GPREL16. In the long run, we should merge
11788 // shared literals, and then we will need to additional work
11789 // here.
11791 // Fall through.
11793 case elfcpp::R_MIPS_GPREL16:
11794 case elfcpp::R_MIPS16_GPREL:
11795 case elfcpp::R_MICROMIPS_GPREL7_S2:
11796 case elfcpp::R_MICROMIPS_GPREL16:
11797 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11798 target->adjusted_gp_value(object),
11799 r_addend, extract_addend,
11800 gsym == NULL, r_types[i],
11801 calculate_only, &calculated_value);
11802 break;
11804 case elfcpp::R_MIPS_PC16:
11805 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11806 r_addend, extract_addend,
11807 calculate_only,
11808 &calculated_value);
11809 break;
11811 case elfcpp::R_MIPS_PC21_S2:
11812 reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11813 r_addend, extract_addend,
11814 calculate_only,
11815 &calculated_value);
11816 break;
11818 case elfcpp::R_MIPS_PC26_S2:
11819 reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11820 r_addend, extract_addend,
11821 calculate_only,
11822 &calculated_value);
11823 break;
11825 case elfcpp::R_MIPS_PC18_S3:
11826 reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11827 r_addend, extract_addend,
11828 calculate_only,
11829 &calculated_value);
11830 break;
11832 case elfcpp::R_MIPS_PC19_S2:
11833 reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
11834 r_addend, extract_addend,
11835 calculate_only,
11836 &calculated_value);
11837 break;
11839 case elfcpp::R_MIPS_PCHI16:
11840 if (rel_type == elfcpp::SHT_RELA)
11841 reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
11842 r_addend, address,
11843 extract_addend, 0,
11844 calculate_only,
11845 &calculated_value);
11846 else if (rel_type == elfcpp::SHT_REL)
11847 reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
11848 r_addend, address, r_sym,
11849 extract_addend);
11850 else
11851 gold_unreachable();
11852 break;
11854 case elfcpp::R_MIPS_PCLO16:
11855 reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
11856 extract_addend, address, r_sym,
11857 rel_type, calculate_only,
11858 &calculated_value);
11859 break;
11860 case elfcpp::R_MICROMIPS_PC7_S1:
11861 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
11862 address, r_addend,
11863 extract_addend,
11864 calculate_only,
11865 &calculated_value);
11866 break;
11867 case elfcpp::R_MICROMIPS_PC10_S1:
11868 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
11869 psymval, address,
11870 r_addend, extract_addend,
11871 calculate_only,
11872 &calculated_value);
11873 break;
11874 case elfcpp::R_MICROMIPS_PC16_S1:
11875 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
11876 psymval, address,
11877 r_addend, extract_addend,
11878 calculate_only,
11879 &calculated_value);
11880 break;
11881 case elfcpp::R_MIPS_GPREL32:
11882 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
11883 target->adjusted_gp_value(object),
11884 r_addend, extract_addend,
11885 calculate_only,
11886 &calculated_value);
11887 break;
11888 case elfcpp::R_MIPS_GOT_HI16:
11889 case elfcpp::R_MIPS_CALL_HI16:
11890 case elfcpp::R_MICROMIPS_GOT_HI16:
11891 case elfcpp::R_MICROMIPS_CALL_HI16:
11892 if (gsym != NULL)
11893 got_offset = target->got_section()->got_offset(gsym,
11894 GOT_TYPE_STANDARD,
11895 object);
11896 else
11897 got_offset = target->got_section()->got_offset(r_sym,
11898 GOT_TYPE_STANDARD,
11899 object, r_addend);
11900 gp_offset = target->got_section()->gp_offset(got_offset, object);
11901 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
11902 calculate_only,
11903 &calculated_value);
11904 update_got_entry = changed_symbol_value;
11905 break;
11907 case elfcpp::R_MIPS_GOT_LO16:
11908 case elfcpp::R_MIPS_CALL_LO16:
11909 case elfcpp::R_MICROMIPS_GOT_LO16:
11910 case elfcpp::R_MICROMIPS_CALL_LO16:
11911 if (gsym != NULL)
11912 got_offset = target->got_section()->got_offset(gsym,
11913 GOT_TYPE_STANDARD,
11914 object);
11915 else
11916 got_offset = target->got_section()->got_offset(r_sym,
11917 GOT_TYPE_STANDARD,
11918 object, r_addend);
11919 gp_offset = target->got_section()->gp_offset(got_offset, object);
11920 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
11921 calculate_only,
11922 &calculated_value);
11923 update_got_entry = changed_symbol_value;
11924 break;
11926 case elfcpp::R_MIPS_GOT_DISP:
11927 case elfcpp::R_MICROMIPS_GOT_DISP:
11928 case elfcpp::R_MIPS_EH:
11929 if (gsym != NULL)
11930 got_offset = target->got_section()->got_offset(gsym,
11931 GOT_TYPE_STANDARD,
11932 object);
11933 else
11934 got_offset = target->got_section()->got_offset(r_sym,
11935 GOT_TYPE_STANDARD,
11936 object, r_addend);
11937 gp_offset = target->got_section()->gp_offset(got_offset, object);
11938 if (eh_reloc(r_types[i]))
11939 reloc_status = Reloc_funcs::releh(view, gp_offset,
11940 calculate_only,
11941 &calculated_value);
11942 else
11943 reloc_status = Reloc_funcs::relgot(view, gp_offset,
11944 calculate_only,
11945 &calculated_value);
11946 break;
11947 case elfcpp::R_MIPS_CALL16:
11948 case elfcpp::R_MIPS16_CALL16:
11949 case elfcpp::R_MICROMIPS_CALL16:
11950 gold_assert(gsym != NULL);
11951 got_offset = target->got_section()->got_offset(gsym,
11952 GOT_TYPE_STANDARD,
11953 object);
11954 gp_offset = target->got_section()->gp_offset(got_offset, object);
11955 reloc_status = Reloc_funcs::relgot(view, gp_offset,
11956 calculate_only, &calculated_value);
11957 // TODO(sasa): We should also initialize update_got_entry
11958 // in other place swhere relgot is called.
11959 update_got_entry = changed_symbol_value;
11960 break;
11962 case elfcpp::R_MIPS_GOT16:
11963 case elfcpp::R_MIPS16_GOT16:
11964 case elfcpp::R_MICROMIPS_GOT16:
11965 if (gsym != NULL)
11967 got_offset = target->got_section()->got_offset(gsym,
11968 GOT_TYPE_STANDARD,
11969 object);
11970 gp_offset = target->got_section()->gp_offset(got_offset, object);
11971 reloc_status = Reloc_funcs::relgot(view, gp_offset,
11972 calculate_only,
11973 &calculated_value);
11975 else
11977 if (rel_type == elfcpp::SHT_RELA)
11978 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
11979 psymval, r_addend,
11980 extract_addend, 0,
11981 target,
11982 calculate_only,
11983 &calculated_value);
11984 else if (rel_type == elfcpp::SHT_REL)
11985 reloc_status = Reloc_funcs::relgot16_local(view, object,
11986 psymval, r_addend,
11987 extract_addend,
11988 r_types[i], r_sym);
11989 else
11990 gold_unreachable();
11992 update_got_entry = changed_symbol_value;
11993 break;
11995 case elfcpp::R_MIPS_TLS_GD:
11996 case elfcpp::R_MIPS16_TLS_GD:
11997 case elfcpp::R_MICROMIPS_TLS_GD:
11998 if (gsym != NULL)
11999 got_offset = target->got_section()->got_offset(gsym,
12000 GOT_TYPE_TLS_PAIR,
12001 object);
12002 else
12003 got_offset = target->got_section()->got_offset(r_sym,
12004 GOT_TYPE_TLS_PAIR,
12005 object, r_addend);
12006 gp_offset = target->got_section()->gp_offset(got_offset, object);
12007 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
12008 &calculated_value);
12009 break;
12011 case elfcpp::R_MIPS_TLS_GOTTPREL:
12012 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12013 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12014 if (gsym != NULL)
12015 got_offset = target->got_section()->got_offset(gsym,
12016 GOT_TYPE_TLS_OFFSET,
12017 object);
12018 else
12019 got_offset = target->got_section()->got_offset(r_sym,
12020 GOT_TYPE_TLS_OFFSET,
12021 object, r_addend);
12022 gp_offset = target->got_section()->gp_offset(got_offset, object);
12023 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
12024 &calculated_value);
12025 break;
12027 case elfcpp::R_MIPS_TLS_LDM:
12028 case elfcpp::R_MIPS16_TLS_LDM:
12029 case elfcpp::R_MICROMIPS_TLS_LDM:
12030 // Relocate the field with the offset of the GOT entry for
12031 // the module index.
12032 got_offset = target->got_section()->tls_ldm_offset(object);
12033 gp_offset = target->got_section()->gp_offset(got_offset, object);
12034 reloc_status = Reloc_funcs::relgot(view, gp_offset, calculate_only,
12035 &calculated_value);
12036 break;
12038 case elfcpp::R_MIPS_GOT_PAGE:
12039 case elfcpp::R_MICROMIPS_GOT_PAGE:
12040 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12041 r_addend, extract_addend,
12042 calculate_only,
12043 &calculated_value);
12044 break;
12046 case elfcpp::R_MIPS_GOT_OFST:
12047 case elfcpp::R_MICROMIPS_GOT_OFST:
12048 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12049 r_addend, extract_addend,
12050 local, calculate_only,
12051 &calculated_value);
12052 break;
12054 case elfcpp::R_MIPS_JALR:
12055 case elfcpp::R_MICROMIPS_JALR:
12056 // This relocation is only a hint. In some cases, we optimize
12057 // it into a bal instruction. But we don't try to optimize
12058 // when the symbol does not resolve locally.
12059 if (gsym == NULL
12060 || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12061 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12062 r_addend, extract_addend,
12063 cross_mode_jump, r_types[i],
12064 target->jalr_to_bal(),
12065 target->jr_to_b(),
12066 calculate_only,
12067 &calculated_value);
12068 break;
12070 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12071 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12072 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12073 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12074 elfcpp::DTP_OFFSET, r_addend,
12075 extract_addend, calculate_only,
12076 &calculated_value);
12077 break;
12078 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12079 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12080 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12081 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12082 elfcpp::DTP_OFFSET, r_addend,
12083 extract_addend, calculate_only,
12084 &calculated_value);
12085 break;
12086 case elfcpp::R_MIPS_TLS_DTPREL32:
12087 case elfcpp::R_MIPS_TLS_DTPREL64:
12088 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12089 elfcpp::DTP_OFFSET, r_addend,
12090 extract_addend, calculate_only,
12091 &calculated_value);
12092 break;
12093 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12094 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12095 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12096 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12097 elfcpp::TP_OFFSET, r_addend,
12098 extract_addend, calculate_only,
12099 &calculated_value);
12100 break;
12101 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12102 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12103 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12104 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12105 elfcpp::TP_OFFSET, r_addend,
12106 extract_addend, calculate_only,
12107 &calculated_value);
12108 break;
12109 case elfcpp::R_MIPS_TLS_TPREL32:
12110 case elfcpp::R_MIPS_TLS_TPREL64:
12111 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12112 elfcpp::TP_OFFSET, r_addend,
12113 extract_addend, calculate_only,
12114 &calculated_value);
12115 break;
12116 case elfcpp::R_MIPS_SUB:
12117 case elfcpp::R_MICROMIPS_SUB:
12118 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12119 extract_addend,
12120 calculate_only, &calculated_value);
12121 break;
12122 default:
12123 gold_error_at_location(relinfo, relnum, r_offset,
12124 _("unsupported reloc %u"), r_types[i]);
12125 break;
12128 if (update_got_entry)
12130 Mips_output_data_got<size, big_endian>* got = target->got_section();
12131 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12132 got->update_got_entry(got->get_primary_got_offset(mips_sym),
12133 psymval->value(object, 0));
12134 else
12135 got->update_got_entry(got_offset, psymval->value(object, 0));
12138 r_addend = calculated_value;
12141 bool jal_shuffle = jal_reloc(r_type) ? !parameters->options().relocatable()
12142 : false;
12143 Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12145 // Report any errors.
12146 switch (reloc_status)
12148 case Reloc_funcs::STATUS_OKAY:
12149 break;
12150 case Reloc_funcs::STATUS_OVERFLOW:
12151 gold_error_at_location(relinfo, relnum, r_offset,
12152 _("relocation overflow"));
12153 break;
12154 case Reloc_funcs::STATUS_BAD_RELOC:
12155 gold_error_at_location(relinfo, relnum, r_offset,
12156 _("unexpected opcode while processing relocation"));
12157 break;
12158 case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12159 gold_error_at_location(relinfo, relnum, r_offset,
12160 _("unaligned PC-relative relocation"));
12161 break;
12162 default:
12163 gold_unreachable();
12166 return true;
12169 // Get the Reference_flags for a particular relocation.
12171 template<int size, bool big_endian>
12173 Target_mips<size, big_endian>::Scan::get_reference_flags(
12174 unsigned int r_type)
12176 switch (r_type)
12178 case elfcpp::R_MIPS_NONE:
12179 // No symbol reference.
12180 return 0;
12182 case elfcpp::R_MIPS_16:
12183 case elfcpp::R_MIPS_32:
12184 case elfcpp::R_MIPS_64:
12185 case elfcpp::R_MIPS_HI16:
12186 case elfcpp::R_MIPS_LO16:
12187 case elfcpp::R_MIPS16_HI16:
12188 case elfcpp::R_MIPS16_LO16:
12189 case elfcpp::R_MICROMIPS_HI16:
12190 case elfcpp::R_MICROMIPS_LO16:
12191 return Symbol::ABSOLUTE_REF;
12193 case elfcpp::R_MIPS_26:
12194 case elfcpp::R_MIPS16_26:
12195 case elfcpp::R_MICROMIPS_26_S1:
12196 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12198 case elfcpp::R_MIPS_PC18_S3:
12199 case elfcpp::R_MIPS_PC19_S2:
12200 case elfcpp::R_MIPS_PCHI16:
12201 case elfcpp::R_MIPS_PCLO16:
12202 case elfcpp::R_MIPS_GPREL32:
12203 case elfcpp::R_MIPS_GPREL16:
12204 case elfcpp::R_MIPS_REL32:
12205 case elfcpp::R_MIPS16_GPREL:
12206 return Symbol::RELATIVE_REF;
12208 case elfcpp::R_MIPS_PC16:
12209 case elfcpp::R_MIPS_PC32:
12210 case elfcpp::R_MIPS_PC21_S2:
12211 case elfcpp::R_MIPS_PC26_S2:
12212 case elfcpp::R_MIPS_JALR:
12213 case elfcpp::R_MICROMIPS_JALR:
12214 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12216 case elfcpp::R_MIPS_GOT16:
12217 case elfcpp::R_MIPS_CALL16:
12218 case elfcpp::R_MIPS_GOT_DISP:
12219 case elfcpp::R_MIPS_GOT_HI16:
12220 case elfcpp::R_MIPS_GOT_LO16:
12221 case elfcpp::R_MIPS_CALL_HI16:
12222 case elfcpp::R_MIPS_CALL_LO16:
12223 case elfcpp::R_MIPS_LITERAL:
12224 case elfcpp::R_MIPS_GOT_PAGE:
12225 case elfcpp::R_MIPS_GOT_OFST:
12226 case elfcpp::R_MIPS16_GOT16:
12227 case elfcpp::R_MIPS16_CALL16:
12228 case elfcpp::R_MICROMIPS_GOT16:
12229 case elfcpp::R_MICROMIPS_CALL16:
12230 case elfcpp::R_MICROMIPS_GOT_HI16:
12231 case elfcpp::R_MICROMIPS_GOT_LO16:
12232 case elfcpp::R_MICROMIPS_CALL_HI16:
12233 case elfcpp::R_MICROMIPS_CALL_LO16:
12234 case elfcpp::R_MIPS_EH:
12235 // Absolute in GOT.
12236 return Symbol::RELATIVE_REF;
12238 case elfcpp::R_MIPS_TLS_DTPMOD32:
12239 case elfcpp::R_MIPS_TLS_DTPREL32:
12240 case elfcpp::R_MIPS_TLS_DTPMOD64:
12241 case elfcpp::R_MIPS_TLS_DTPREL64:
12242 case elfcpp::R_MIPS_TLS_GD:
12243 case elfcpp::R_MIPS_TLS_LDM:
12244 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12245 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12246 case elfcpp::R_MIPS_TLS_GOTTPREL:
12247 case elfcpp::R_MIPS_TLS_TPREL32:
12248 case elfcpp::R_MIPS_TLS_TPREL64:
12249 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12250 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12251 case elfcpp::R_MIPS16_TLS_GD:
12252 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12253 case elfcpp::R_MICROMIPS_TLS_GD:
12254 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12255 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12256 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12257 return Symbol::TLS_REF;
12259 case elfcpp::R_MIPS_COPY:
12260 case elfcpp::R_MIPS_JUMP_SLOT:
12261 default:
12262 gold_unreachable();
12263 // Not expected. We will give an error later.
12264 return 0;
12268 // Report an unsupported relocation against a local symbol.
12270 template<int size, bool big_endian>
12271 void
12272 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12273 Sized_relobj_file<size, big_endian>* object,
12274 unsigned int r_type)
12276 gold_error(_("%s: unsupported reloc %u against local symbol"),
12277 object->name().c_str(), r_type);
12280 // Report an unsupported relocation against a global symbol.
12282 template<int size, bool big_endian>
12283 void
12284 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12285 Sized_relobj_file<size, big_endian>* object,
12286 unsigned int r_type,
12287 Symbol* gsym)
12289 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12290 object->name().c_str(), r_type, gsym->demangled_name().c_str());
12293 // Return printable name for ABI.
12294 template<int size, bool big_endian>
12295 const char*
12296 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
12298 switch (e_flags & elfcpp::EF_MIPS_ABI)
12300 case 0:
12301 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12302 return "N32";
12303 else if (size == 64)
12304 return "64";
12305 else
12306 return "none";
12307 case elfcpp::E_MIPS_ABI_O32:
12308 return "O32";
12309 case elfcpp::E_MIPS_ABI_O64:
12310 return "O64";
12311 case elfcpp::E_MIPS_ABI_EABI32:
12312 return "EABI32";
12313 case elfcpp::E_MIPS_ABI_EABI64:
12314 return "EABI64";
12315 default:
12316 return "unknown abi";
12320 template<int size, bool big_endian>
12321 const char*
12322 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12324 switch (e_flags & elfcpp::EF_MIPS_MACH)
12326 case elfcpp::E_MIPS_MACH_3900:
12327 return "mips:3900";
12328 case elfcpp::E_MIPS_MACH_4010:
12329 return "mips:4010";
12330 case elfcpp::E_MIPS_MACH_4100:
12331 return "mips:4100";
12332 case elfcpp::E_MIPS_MACH_4111:
12333 return "mips:4111";
12334 case elfcpp::E_MIPS_MACH_4120:
12335 return "mips:4120";
12336 case elfcpp::E_MIPS_MACH_4650:
12337 return "mips:4650";
12338 case elfcpp::E_MIPS_MACH_5400:
12339 return "mips:5400";
12340 case elfcpp::E_MIPS_MACH_5500:
12341 return "mips:5500";
12342 case elfcpp::E_MIPS_MACH_5900:
12343 return "mips:5900";
12344 case elfcpp::E_MIPS_MACH_SB1:
12345 return "mips:sb1";
12346 case elfcpp::E_MIPS_MACH_9000:
12347 return "mips:9000";
12348 case elfcpp::E_MIPS_MACH_LS2E:
12349 return "mips:loongson_2e";
12350 case elfcpp::E_MIPS_MACH_LS2F:
12351 return "mips:loongson_2f";
12352 case elfcpp::E_MIPS_MACH_LS3A:
12353 return "mips:loongson_3a";
12354 case elfcpp::E_MIPS_MACH_OCTEON:
12355 return "mips:octeon";
12356 case elfcpp::E_MIPS_MACH_OCTEON2:
12357 return "mips:octeon2";
12358 case elfcpp::E_MIPS_MACH_OCTEON3:
12359 return "mips:octeon3";
12360 case elfcpp::E_MIPS_MACH_XLR:
12361 return "mips:xlr";
12362 default:
12363 switch (e_flags & elfcpp::EF_MIPS_ARCH)
12365 default:
12366 case elfcpp::E_MIPS_ARCH_1:
12367 return "mips:3000";
12369 case elfcpp::E_MIPS_ARCH_2:
12370 return "mips:6000";
12372 case elfcpp::E_MIPS_ARCH_3:
12373 return "mips:4000";
12375 case elfcpp::E_MIPS_ARCH_4:
12376 return "mips:8000";
12378 case elfcpp::E_MIPS_ARCH_5:
12379 return "mips:mips5";
12381 case elfcpp::E_MIPS_ARCH_32:
12382 return "mips:isa32";
12384 case elfcpp::E_MIPS_ARCH_64:
12385 return "mips:isa64";
12387 case elfcpp::E_MIPS_ARCH_32R2:
12388 return "mips:isa32r2";
12390 case elfcpp::E_MIPS_ARCH_32R6:
12391 return "mips:isa32r6";
12393 case elfcpp::E_MIPS_ARCH_64R2:
12394 return "mips:isa64r2";
12396 case elfcpp::E_MIPS_ARCH_64R6:
12397 return "mips:isa64r6";
12400 return "unknown CPU";
12403 template<int size, bool big_endian>
12404 const Target::Target_info Target_mips<size, big_endian>::mips_info =
12406 size, // size
12407 big_endian, // is_big_endian
12408 elfcpp::EM_MIPS, // machine_code
12409 true, // has_make_symbol
12410 false, // has_resolve
12411 false, // has_code_fill
12412 true, // is_default_stack_executable
12413 false, // can_icf_inline_merge_sections
12414 '\0', // wrap_char
12415 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
12416 0x400000, // default_text_segment_address
12417 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
12418 4 * 1024, // common_pagesize (overridable by -z common-page-size)
12419 false, // isolate_execinstr
12420 0, // rosegment_gap
12421 elfcpp::SHN_UNDEF, // small_common_shndx
12422 elfcpp::SHN_UNDEF, // large_common_shndx
12423 0, // small_common_section_flags
12424 0, // large_common_section_flags
12425 NULL, // attributes_section
12426 NULL, // attributes_vendor
12427 "__start", // entry_symbol_name
12428 32, // hash_entry_size
12431 template<int size, bool big_endian>
12432 class Target_mips_nacl : public Target_mips<size, big_endian>
12434 public:
12435 Target_mips_nacl()
12436 : Target_mips<size, big_endian>(&mips_nacl_info)
12439 private:
12440 static const Target::Target_info mips_nacl_info;
12443 template<int size, bool big_endian>
12444 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12446 size, // size
12447 big_endian, // is_big_endian
12448 elfcpp::EM_MIPS, // machine_code
12449 true, // has_make_symbol
12450 false, // has_resolve
12451 false, // has_code_fill
12452 true, // is_default_stack_executable
12453 false, // can_icf_inline_merge_sections
12454 '\0', // wrap_char
12455 "/lib/ld.so.1", // dynamic_linker
12456 0x20000, // default_text_segment_address
12457 0x10000, // abi_pagesize (overridable by -z max-page-size)
12458 0x10000, // common_pagesize (overridable by -z common-page-size)
12459 true, // isolate_execinstr
12460 0x10000000, // rosegment_gap
12461 elfcpp::SHN_UNDEF, // small_common_shndx
12462 elfcpp::SHN_UNDEF, // large_common_shndx
12463 0, // small_common_section_flags
12464 0, // large_common_section_flags
12465 NULL, // attributes_section
12466 NULL, // attributes_vendor
12467 "_start", // entry_symbol_name
12468 32, // hash_entry_size
12471 // Target selector for Mips. Note this is never instantiated directly.
12472 // It's only used in Target_selector_mips_nacl, below.
12474 template<int size, bool big_endian>
12475 class Target_selector_mips : public Target_selector
12477 public:
12478 Target_selector_mips()
12479 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12480 (size == 64 ?
12481 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12482 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12483 (size == 64 ?
12484 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12485 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
12488 Target* do_instantiate_target()
12489 { return new Target_mips<size, big_endian>(); }
12492 template<int size, bool big_endian>
12493 class Target_selector_mips_nacl
12494 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12495 Target_mips_nacl<size, big_endian> >
12497 public:
12498 Target_selector_mips_nacl()
12499 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12500 Target_mips_nacl<size, big_endian> >(
12501 // NaCl currently supports only MIPS32 little-endian.
12502 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12506 Target_selector_mips_nacl<32, true> target_selector_mips32;
12507 Target_selector_mips_nacl<32, false> target_selector_mips32el;
12508 Target_selector_mips_nacl<64, true> target_selector_mips64;
12509 Target_selector_mips_nacl<64, false> target_selector_mips64el;
12511 } // End anonymous namespace.